Commit c104366f269ac7cacea6627d937675d80921706a

Henrik Nordstrom 2014-06-18T16:41:03

KnCMiner driver generation 2 Note: Requires generation 2 code in the FPGA controller.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
diff --git a/Makefile.am b/Makefile.am
index 4127368..dd0127c 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -65,7 +65,8 @@ cgminer_SOURCES += driver-avalon.c driver-avalon.h
 endif
 
 if HAS_KNC
-cgminer_SOURCES += driver-knc-spi-fpga.c
+cgminer_SOURCES += driver-knc.c knc-asic.c knc-asic.h knc-transport.h knc-transport-spi.c
+cgminer_LDADD   += -lz
 endif
 
 if HAS_BFLSC
diff --git a/driver-knc-spi-fpga.c b/driver-knc-spi-fpga.c
deleted file mode 100644
index c91b378..0000000
--- a/driver-knc-spi-fpga.c
+++ /dev/null
@@ -1,904 +0,0 @@
-/*
- * cgminer driver for KnCminer devices
- *
- * Copyright 2013 Con Kolivas <kernel@kolivas.org>
- * Copyright 2013 KnCminer
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the Free
- * Software Foundation; either version 3 of the License, or (at your option)
- * any later version.  See COPYING for more details.
- */
-
-#include <stdlib.h>
-#include <assert.h>
-#include <fcntl.h>
-#include <limits.h>
-#include <unistd.h>
-#include <sys/ioctl.h>
-#include <linux/types.h>
-#include <linux/spi/spidev.h>
-
-#include "logging.h"
-#include "miner.h"
-
-#define MAX_SPIS		1
-#define	MAX_BYTES_IN_SPI_XSFER	4096
-/* /dev/spidevB.C, where B = bus, C = chipselect */
-#define SPI_DEVICE_TEMPLATE	"/dev/spidev%d.%d"
-#define SPI_MODE		(SPI_CPHA | SPI_CPOL | SPI_CS_HIGH)
-#define SPI_BITS_PER_WORD	32
-#define SPI_MAX_SPEED		3000000
-#define SPI_DELAY_USECS		0
-/* Max number of ASICs permitted on one SPI device */
-#define MAX_ASICS		6
-#define CORES_PER_ASIC	192
-
-/* How many hardware errors in a row before disabling the core */
-#define HW_ERR_LIMIT		10
-#define DISA_ERR_LIMIT		3
-
-#define MAX_ACTIVE_WORKS	(192 * 2 * 6 * 2)
-
-#define WORK_MIDSTATE_WORDS	8
-#define WORK_DATA_WORDS		3
-
-#define WORK_STALE_US		60000000
-
-#define	SECONDS_IN_MINUTE	60
-
-/* Keep core disabled for no longer than 15 minutes */
-#define CORE_DISA_PERIOD_US	(15 * SECONDS_IN_MINUTE * 1000000)
-
-/* DP = Disable Policy */
-bool opt_knc_DP_checkworkid = false;
-bool opt_knc_DP_disable_permanently = false;
-
-struct spidev_context {
-	int fd;
-	uint32_t speed;
-	uint16_t delay;
-	uint8_t mode;
-	uint8_t bits;
-};
-
-struct spi_request {
-#define	CMD_NOP		0
-#define	CMD_GET_VERSION	1
-#define	CMD_SUBMIT_WORK	2
-#define	CMD_FLUSH_QUEUE	3
-
-#define	WORK_ID_MASK	0x7FFF
-
-#if (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
-	uint32_t cmd		:4;
-	uint32_t rsvd		:1; /* set to zero */
-	uint32_t queue_id	:12;
-	uint32_t work_id	:15;
-#else
-	uint32_t work_id	:15;
-	uint32_t queue_id	:12;
-	uint32_t rsvd		:1; /* set to zero */
-	uint32_t cmd		:4;
-#endif
-	uint32_t midstate[WORK_MIDSTATE_WORDS];
-	uint32_t data[WORK_DATA_WORDS];
-};
-
-struct spi_response {
-#define	RESPONSE_TYPE_NOP		0
-#define	RESPONSE_TYPE_NONCE_FOUND	1
-#define	RESPONSE_TYPE_WORK_DONE		2
-#if (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
-	uint32_t type		:2;
-	uint32_t asic		:3;
-	uint32_t queue_id	:12;
-	uint32_t work_id	:15;
-#else
-	uint32_t work_id	:15;
-	uint32_t queue_id	:12;
-	uint32_t asic		:3;
-	uint32_t type		:2;
-#endif
-	uint32_t nonce;
-	uint32_t core;
-};
-
-#define MAX_REQUESTS_IN_BATCH	( MAX_BYTES_IN_SPI_XSFER /	\
-				  sizeof(struct spi_request)	\
-				)
-
-static struct spi_request spi_txbuf[MAX_REQUESTS_IN_BATCH];
-
-#define MAX_RESPONSES_IN_BATCH	( (sizeof(spi_txbuf) - 12) /	\
-				   sizeof(struct spi_response)	\
-				)
-
-struct spi_rx_t {
-#if (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
-	uint32_t rsvd_1			:31;
-	uint32_t response_queue_full	:1;
-#else
-	uint32_t response_queue_full	:1;
-	uint32_t rsvd_1			:31;
-#endif
-#if (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
-	uint32_t rsvd_2			:16;
-	uint32_t works_accepted		:16;
-#else
-	uint32_t works_accepted		:16;
-	uint32_t rsvd_2			:16;
-#endif
-	uint32_t rsvd_3;
-	struct spi_response responses[MAX_RESPONSES_IN_BATCH];
-};
-
-static struct spi_rx_t spi_rxbuf;
-
-struct active_work {
-	struct work *work;
-	uint32_t work_id;
-	struct timeval begin;
-};
-
-struct core_disa_data {
-	struct timeval disa_begin;
-	uint8_t asic;
-	uint8_t core;
-};
-
-struct knc_state {
-	struct spidev_context *ctx;
-	int devices;
-	uint32_t salt;
-	uint32_t next_work_id;
-
-	/* read - last read item, next is at (read + 1) mod BUFSIZE
-	 * write - next write item, last written at (write - 1) mod BUFSIZE
-	 *  When buffer is empty, read + 1 == write
-	 *  Buffer full condition: read == write
-	 */
-	int read_q, write_q;
-#define KNC_QUEUED_BUFFER_SIZE	(MAX_REQUESTS_IN_BATCH + 1)
-	struct active_work queued_fifo[KNC_QUEUED_BUFFER_SIZE];
-
-	int read_a, write_a;
-#define KNC_ACTIVE_BUFFER_SIZE	(MAX_ACTIVE_WORKS + 1)
-	struct active_work active_fifo[KNC_ACTIVE_BUFFER_SIZE];
-
-	uint8_t hwerrs[MAX_ASICS * 256];
-	uint8_t disa_cnt[MAX_ASICS * 256];
-	uint32_t hwerr_work_id[MAX_ASICS * 256];
-	int read_d, write_d;
-#define KNC_DISA_CORES_SIZE	(MAX_ASICS * 256)
-	struct core_disa_data disa_cores_fifo[KNC_DISA_CORES_SIZE];
-
-	/* Local stats */
-#define	KNC_MINUTES_IN_STATS_BUFFER	60
-	unsigned int last_hour_shares[MAX_ASICS][256][KNC_MINUTES_IN_STATS_BUFFER + 1];
-	unsigned int last_hour_hwerrs[MAX_ASICS][256][KNC_MINUTES_IN_STATS_BUFFER + 1];
-	unsigned int last_hour_shares_index[MAX_ASICS][256];
-	unsigned int last_hour_hwerrs_index[MAX_ASICS][256];
-
-	pthread_mutex_t lock;
-};
-
-static inline bool knc_queued_fifo_full(struct knc_state *knc)
-{
-	return (knc->read_q == knc->write_q);
-}
-
-static inline bool knc_active_fifo_full(struct knc_state *knc)
-{
-	return (knc->read_a == knc->write_a);
-}
-
-static inline void knc_queued_fifo_inc_idx(int *idx)
-{
-	if (unlikely(*idx >= ((int)KNC_QUEUED_BUFFER_SIZE - 1)))
-		*idx = 0;
-	else
-		++(*idx);
-}
-
-static inline void knc_active_fifo_inc_idx(int *idx)
-{
-	if (unlikely(*idx >= (KNC_ACTIVE_BUFFER_SIZE - 1)))
-		*idx = 0;
-	else
-		++(*idx);
-}
-
-static inline void knc_disa_cores_fifo_inc_idx(int *idx)
-{
-	if (unlikely(*idx >= (KNC_DISA_CORES_SIZE - 1)))
-		*idx = 0;
-	else
-		++(*idx);
-}
-
-/* Find SPI device with index idx, init it */
-static struct spidev_context *spi_new(int idx)
-{
-	struct spidev_context *ctx;
-	char dev_fname[PATH_MAX];
-
-	if (NULL == (ctx = malloc(sizeof(struct spidev_context)))) {
-		applog(LOG_ERR, "KnC spi: Out of memory");
-		goto l_exit_error;
-	}
-	ctx->mode = SPI_MODE;
-	ctx->bits = SPI_BITS_PER_WORD;
-	ctx->speed = SPI_MAX_SPEED;
-	ctx->delay = SPI_DELAY_USECS;
-
-	ctx->fd = -1;
-
-	sprintf(dev_fname, SPI_DEVICE_TEMPLATE,
-		idx, /* bus */
-		0    /* chipselect */
-	       );
-	if (0 > (ctx->fd = open(dev_fname, O_RDWR))) {
-		applog(LOG_ERR, "KnC spi: Can not open SPI device %s: %m",
-		       dev_fname);
-		goto l_free_exit_error;
-	}
-
-	/*
-	 * spi mode
-	 */
-	if (0 > ioctl(ctx->fd, SPI_IOC_WR_MODE, &ctx->mode))
-		goto l_ioctl_error;
-	if (0 > ioctl(ctx->fd, SPI_IOC_RD_MODE, &ctx->mode))
-		goto l_ioctl_error;
-
-	/*
-	 * bits per word
-	 */
-	if (0 > ioctl(ctx->fd, SPI_IOC_WR_BITS_PER_WORD, &ctx->bits))
-		goto l_ioctl_error;
-	if (0 > ioctl(ctx->fd, SPI_IOC_RD_BITS_PER_WORD, &ctx->bits))
-		goto l_ioctl_error;
-
-	/*
-	 * max speed hz
-	 */
-	if (0 > ioctl(ctx->fd, SPI_IOC_WR_MAX_SPEED_HZ, &ctx->speed))
-		goto l_ioctl_error;
-	if (0 > ioctl(ctx->fd, SPI_IOC_RD_MAX_SPEED_HZ, &ctx->speed))
-		goto l_ioctl_error;
-
-	applog(LOG_INFO, "KnC spi: device %s uses mode %hhu, bits %hhu, speed %u",
-	       dev_fname, ctx->mode, ctx->bits, ctx->speed);
-
-	return ctx;
-
-l_ioctl_error:
-	applog(LOG_ERR, "KnC spi: ioctl error on SPI device %s: %m", dev_fname);
-	close(ctx->fd);
-l_free_exit_error:
-	free(ctx);
-l_exit_error:
-	return NULL;
-}
-
-static void spi_free(struct spidev_context *ctx)
-{
-	if (NULL == ctx)
-		return;
-
-	close(ctx->fd);
-	free(ctx);
-}
-
-static int spi_transfer(struct spidev_context *ctx, uint8_t *txbuf,
-			uint8_t *rxbuf, int len)
-{
-	struct spi_ioc_transfer xfr;
-	int ret;
-
-	memset(rxbuf, 0xff, len);
-
-	ret = len;
-
-	xfr.tx_buf = (unsigned long)txbuf;
-	xfr.rx_buf = (unsigned long)rxbuf;
-	xfr.len = len;
-	xfr.speed_hz = ctx->speed;
-	xfr.delay_usecs = ctx->delay;
-	xfr.bits_per_word = ctx->bits;
-	xfr.cs_change = 0;
-	xfr.pad = 0;
-
-	if (1 > (ret = ioctl(ctx->fd, SPI_IOC_MESSAGE(1), &xfr)))
-		applog(LOG_ERR, "KnC spi xfer: ioctl error on SPI device: %m");
-
-	return ret;
-}
-
-static void stats_zero_data_if_curindex_updated(unsigned int *data, unsigned int *index, unsigned int cur_index)
-{
-	if (cur_index != *index) {
-		unsigned int i;
-		for (i = (*index + 1) % (KNC_MINUTES_IN_STATS_BUFFER + 1);
-			 i != cur_index;
-			 i = ((i + 1 ) % (KNC_MINUTES_IN_STATS_BUFFER + 1)))
-			data[i] = 0;
-		data[cur_index] = 0;
-		*index = cur_index;
-	}
-}
-
-static void stats_update(unsigned int *data, unsigned int *index, unsigned int cur_index)
-{
-	stats_zero_data_if_curindex_updated(data, index, cur_index);
-	++(data[cur_index]);
-}
-
-static unsigned int get_accumulated_stats(unsigned int *data, unsigned int *index, unsigned int cur_index)
-{
-	unsigned int res, i;
-
-	stats_zero_data_if_curindex_updated(data, index, cur_index);
-
-	res = 0;
-	for (i = 0; i < (KNC_MINUTES_IN_STATS_BUFFER + 1); ++i) {
-		if (i != cur_index)
-			res += data[i];
-	}
-
-	return res;
-}
-
-static inline void stats_good_share(struct knc_state *knc, uint32_t asic, uint32_t core, struct timespec *ts)
-{
-	if ((asic >= MAX_ASICS) || (core >= 256))
-		return;
-	unsigned int cur_minute = (ts->tv_sec / SECONDS_IN_MINUTE) % (KNC_MINUTES_IN_STATS_BUFFER + 1);
-	stats_update(knc->last_hour_shares[asic][core], &(knc->last_hour_shares_index[asic][core]), cur_minute);
-}
-
-static inline void stats_bad_share(struct knc_state *knc, uint32_t asic, uint32_t core, struct timespec *ts)
-{
-	if ((asic >= MAX_ASICS) || (core >= 256))
-		return;
-	unsigned int cur_minute = (ts->tv_sec / SECONDS_IN_MINUTE) % (KNC_MINUTES_IN_STATS_BUFFER + 1);
-	stats_update(knc->last_hour_hwerrs[asic][core], &(knc->last_hour_hwerrs_index[asic][core]), cur_minute);
-}
-
-static inline unsigned int get_hour_shares(struct knc_state *knc, uint32_t asic, uint32_t core, struct timespec *ts)
-{
-	if ((asic >= MAX_ASICS) || (core >= 256))
-		return 0;
-	unsigned int cur_minute = (ts->tv_sec / SECONDS_IN_MINUTE) % (KNC_MINUTES_IN_STATS_BUFFER + 1);
-	return get_accumulated_stats(knc->last_hour_shares[asic][core], &(knc->last_hour_shares_index[asic][core]), cur_minute);
-}
-
-static inline unsigned int get_hour_errors(struct knc_state *knc, uint32_t asic, uint32_t core, struct timespec *ts)
-{
-	if ((asic >= MAX_ASICS) || (core >= 256))
-		return 0;
-	unsigned int cur_minute = (ts->tv_sec / SECONDS_IN_MINUTE) % (KNC_MINUTES_IN_STATS_BUFFER + 1);
-	return get_accumulated_stats(knc->last_hour_hwerrs[asic][core], &(knc->last_hour_hwerrs_index[asic][core]), cur_minute);
-}
-
-static struct api_data *knc_api_stats(struct cgpu_info *cgpu)
-{
-	struct knc_state *knc = cgpu->device_data;
-	struct api_data *root = NULL;
-	unsigned int cursize;
-	int asic, core, n;
-	char buf[4096];
-	struct timespec ts_now;
-
-	clock_gettime(CLOCK_MONOTONIC, &ts_now);
-
-	for (asic = 0; asic < MAX_ASICS; ++asic) {
-		char asic_name[128];
-		snprintf(asic_name, sizeof(asic_name), "asic_%d_shares", asic + 1);
-		cursize = 0;
-		for (core = 0; core < CORES_PER_ASIC; ++core) {
-			unsigned int shares = get_hour_shares(knc, asic, core, &ts_now);
-			n = snprintf(buf + cursize, sizeof(buf) - cursize, "%d,", shares);
-			cursize += n;
-			if (sizeof(buf) < cursize) {
-				cursize = sizeof(buf);
-				break;
-			}
-		}
-		if (0 < cursize)
-			buf[cursize - 1] = '\0'; /* last comma */
-		root = api_add_string(root, asic_name, buf, true);
-
-		snprintf(asic_name, sizeof(asic_name), "asic_%d_hwerrs", asic + 1);
-		cursize = 0;
-		for (core = 0; core < CORES_PER_ASIC; ++core) {
-			unsigned int errors = get_hour_errors(knc, asic, core, &ts_now);
-			n = snprintf(buf + cursize, sizeof(buf) - cursize, "%d,", errors);
-			cursize += n;
-			if (sizeof(buf) < cursize) {
-				cursize = sizeof(buf);
-				break;
-			}
-		}
-		if (0 < cursize)
-			buf[cursize - 1] = '\0'; /* last comma */
-		root = api_add_string(root, asic_name, buf, true);
-	}
-
-	return root;
-}
-
-static void disable_core(uint8_t asic, uint8_t core)
-{
-	char str[256];
-
-	snprintf(str, sizeof(str), "i2cset -y 2 0x2%hhu %hhu 0", asic, core);
-	if (0 != WEXITSTATUS(system(str)))
-		applog(LOG_ERR, "KnC: system call failed");
-}
-
-static void enable_core(uint8_t asic, uint8_t core)
-{
-	char str[256];
-
-	snprintf(str, sizeof(str), "i2cset -y 2 0x2%hhu %hhu 1", asic, core);
-	if (0 != WEXITSTATUS(system(str)))
-		applog(LOG_ERR, "KnC: system call failed");
-}
-
-static int64_t timediff(const struct timeval *a, const struct timeval *b)
-{
-	struct timeval diff;
-
-	timersub(a, b, &diff);
-
-	return diff.tv_sec * 1000000 + diff.tv_usec;
-}
-
-static void knc_check_disabled_cores(struct knc_state *knc)
-{
-	struct core_disa_data *core;
-	int next_read_d, cidx;
-	struct timeval now;
-	int64_t us;
-
-	next_read_d = knc->read_d;
-	knc_disa_cores_fifo_inc_idx(&next_read_d);
-	if (next_read_d == knc->write_d)
-		return; /* queue empty */
-
-	core = &knc->disa_cores_fifo[next_read_d];
-	gettimeofday(&now, NULL);
-	us = timediff(&now, &core->disa_begin);
-	if ((us >= 0) && (us < CORE_DISA_PERIOD_US))
-		return; /* latest disabled core still not expired */
-
-	cidx = core->asic * 256 + core->core;
-	enable_core(core->asic, core->core);
-	knc->hwerrs[cidx] = 0;
-	applog(LOG_NOTICE,
-	       "KnC: core %u-%u was enabled back from disabled state",
-	       core->asic, core->core);
-	knc->read_d = next_read_d;
-}
-
-static void knc_work_from_queue_to_spi(struct knc_state *knc,
-				       struct active_work *q_work,
-				       struct spi_request *spi_req, uint32_t work_id)
-{
-	uint32_t *buf_from, *buf_to;
-	int i;
-
-	spi_req->cmd = CMD_SUBMIT_WORK;
-	spi_req->queue_id = 0; /* at the moment we have one and only queue #0 */
-	spi_req->work_id = (work_id ^ knc->salt) & WORK_ID_MASK;
-	q_work->work_id = spi_req->work_id;
-	buf_to = spi_req->midstate;
-	buf_from = (uint32_t *)q_work->work->midstate;
-
-	for (i = 0; i < WORK_MIDSTATE_WORDS; ++i)
-		buf_to[i] = le32toh(buf_from[8 - i - 1]);
-	buf_to = spi_req->data;
-	buf_from = (uint32_t *)&(q_work->work->data[16 * 4]);
-
-	for (i = 0; i < WORK_DATA_WORDS; ++i)
-		buf_to[i] = le32toh(buf_from[3 - i - 1]);
-}
-
-static int64_t knc_process_response(struct thr_info *thr, struct cgpu_info *cgpu,
-				    struct spi_rx_t *rxbuf)
-{
-	struct knc_state *knc = cgpu->device_data;
-	int submitted, successful, i, num_sent;
-	int next_read_q, next_read_a;
-	struct timeval now;
-	struct timespec ts_now;
-	struct work *work;
-	int64_t us;
-
-	num_sent = knc->write_q - knc->read_q - 1;
-	if (knc->write_q <= knc->read_q)
-		num_sent += KNC_QUEUED_BUFFER_SIZE;
-
-	knc->next_work_id += rxbuf->works_accepted;
-
-	/* Actually process SPI response */
-	if (rxbuf->works_accepted) {
-		applog(LOG_DEBUG, "KnC spi: raw response %08X %08X",
-		       ((uint32_t *)rxbuf)[0], ((uint32_t *)rxbuf)[1]);
-		applog(LOG_DEBUG,
-		       "KnC spi: response, accepted %u (from %u), full %u",
-		       rxbuf->works_accepted, num_sent,
-		       rxbuf->response_queue_full);
-	}
-	/* move works_accepted number of items from queued_fifo to active_fifo */
-	gettimeofday(&now, NULL);
-	clock_gettime(CLOCK_MONOTONIC, &ts_now);
-	submitted = 0;
-
-	for (i = 0; i < rxbuf->works_accepted; ++i) {
-		next_read_q = knc->read_q;
-		knc_queued_fifo_inc_idx(&next_read_q);
-		if ((next_read_q == knc->write_q) || knc_active_fifo_full(knc))
-			break;
-
-		memcpy(&knc->active_fifo[knc->write_a],
-		       &knc->queued_fifo[next_read_q],
-		       sizeof(struct active_work));
-		knc->active_fifo[knc->write_a].begin = now;
-		knc->queued_fifo[next_read_q].work = NULL;
-		knc->read_q = next_read_q;
-		knc_active_fifo_inc_idx(&knc->write_a);
-		++submitted;
-	}
-	if (submitted != rxbuf->works_accepted) {
-		applog(LOG_ERR,
-		       "KnC: accepted by FPGA %u works, but only %d submitted",
-		       rxbuf->works_accepted, submitted);
-	}
-
-	/* check for completed works and calculated nonces */
-	gettimeofday(&now, NULL);
-	successful = 0;
-
-	for (i = 0; i < (int)MAX_RESPONSES_IN_BATCH; ++i) {
-		if ((rxbuf->responses[i].type != RESPONSE_TYPE_NONCE_FOUND) &&
-		    (rxbuf->responses[i].type != RESPONSE_TYPE_WORK_DONE))
-			continue;
-
-		applog(LOG_DEBUG, "KnC spi: raw response %08X %08X",
-		       ((uint32_t *)&rxbuf->responses[i])[0],
-		       ((uint32_t *)&rxbuf->responses[i])[1]);
-		applog(LOG_DEBUG, "KnC spi: response, T:%u C:%u-%u Q:%u W:%u",
-		       rxbuf->responses[i].type,
-		       rxbuf->responses[i].asic, rxbuf->responses[i].core,
-		       rxbuf->responses[i].queue_id,
-		       rxbuf->responses[i].work_id);
-		/* Find active work with matching ID */
-		next_read_a = knc->read_a;
-		knc_active_fifo_inc_idx(&next_read_a);
-
-		while (next_read_a != knc->write_a) {
-			if (knc->active_fifo[next_read_a].work_id ==
-			    rxbuf->responses[i].work_id)
-				break;
-
-			/* check for stale works */
-			us = timediff(&now,
-				      &knc->active_fifo[next_read_a].begin);
-			if ((us < 0) || (us >= WORK_STALE_US)) {
-				applog(LOG_DEBUG,
-				       "KnC spi: remove stale work %u",
-				       knc->active_fifo[next_read_a].work_id);
-				work = knc->active_fifo[next_read_a].work;
-				knc_active_fifo_inc_idx(&knc->read_a);
-				work_completed(cgpu, work);
-				if (next_read_a != knc->read_a) {
-					memcpy(&(knc->active_fifo[next_read_a]),
-					       &(knc->active_fifo[knc->read_a]),
-					       sizeof(struct active_work));
-				}
-				knc->active_fifo[knc->read_a].work = NULL;
-			}
-
-			knc_active_fifo_inc_idx(&next_read_a);
-		}
-		if (next_read_a == knc->write_a)
-			continue;
-
-		applog(LOG_DEBUG, "KnC spi: response work %u found",
-		       rxbuf->responses[i].work_id);
-		work = knc->active_fifo[next_read_a].work;
-
-		if (rxbuf->responses[i].type == RESPONSE_TYPE_NONCE_FOUND) {
-			if (NULL != thr) {
-				int cidx = rxbuf->responses[i].asic * 256 +
-					   rxbuf->responses[i].core;
-
-				if (submit_nonce(thr, work,
-						 rxbuf->responses[i].nonce)) {
-					stats_good_share(knc, rxbuf->responses[i].asic, rxbuf->responses[i].core, &ts_now);
-					if (cidx < (int)sizeof(knc->hwerrs)) {
-						knc->hwerrs[cidx] = 0;
-						knc->disa_cnt[cidx] = 0;
-						knc->hwerr_work_id[cidx] = 0xFFFFFFFF;
-					}
-					successful++;
-				} else  {
-					stats_bad_share(knc, rxbuf->responses[i].asic, rxbuf->responses[i].core, &ts_now);
-					bool process_hwerr = (cidx < (int)sizeof(knc->hwerrs));
-					if (process_hwerr && opt_knc_DP_checkworkid &&
-					    (knc->hwerr_work_id[cidx] == rxbuf->responses[i].work_id))
-						process_hwerr = false;
-					if (process_hwerr) {
-						knc->hwerr_work_id[cidx] = rxbuf->responses[i].work_id;
-						if (++(knc->hwerrs[cidx]) >= HW_ERR_LIMIT) {
-						    struct core_disa_data *core;
-
-						    core = &knc->disa_cores_fifo[knc->write_d];
-						    core->disa_begin = now;
-						    core->asic = rxbuf->responses[i].asic;
-						    core->core = rxbuf->responses[i].core;
-						    disable_core(core->asic, core->core);
-						    if (opt_knc_DP_disable_permanently &&
-							(++(knc->disa_cnt[cidx]) >= DISA_ERR_LIMIT)) {
-							    applog(LOG_WARNING,
-			"KnC: core %u-%u was disabled permanently", core->asic, core->core);
-						    } else {
-							    applog(LOG_WARNING,
-			"KnC: core %u-%u was disabled due to %u HW errors in a row",
-								   core->asic, core->core, HW_ERR_LIMIT);
-							    knc_disa_cores_fifo_inc_idx(&knc->write_d);
-						    }
-						}
-					}
-				};
-			}
-			continue;
-		}
-
-		/* Work completed */
-		knc_active_fifo_inc_idx(&knc->read_a);
-		work_completed(cgpu, work);
-		if (next_read_a != knc->read_a) {
-			memcpy(&(knc->active_fifo[next_read_a]),
-			       &(knc->active_fifo[knc->read_a]),
-			       sizeof(struct active_work));
-		}
-		knc->active_fifo[knc->read_a].work = NULL;
-	}
-
-	return ((uint64_t)successful) * 0x100000000UL;
-}
-
-/* Send flush command via SPI */
-static int _internal_knc_flush_fpga(struct knc_state *knc)
-{
-	int len;
-
-	spi_txbuf[0].cmd = CMD_FLUSH_QUEUE;
-	spi_txbuf[0].queue_id = 0; /* at the moment we have one and only queue #0 */
-	len = spi_transfer(knc->ctx, (uint8_t *)spi_txbuf,
-			   (uint8_t *)&spi_rxbuf, sizeof(struct spi_request));
-	if (len != sizeof(struct spi_request))
-		return -1;
-
-	len /= sizeof(struct spi_response);
-
-	return len;
-}
-
-static bool knc_detect_one(struct spidev_context *ctx)
-{
-	/* Scan device for ASICs */
-	int chip_id, devices = 0;
-	struct cgpu_info *cgpu;
-	struct knc_state *knc;
-
-	for (chip_id = 0; chip_id < MAX_ASICS; ++chip_id) {
-		/* TODO: perform the ASIC test/detection */
-		++devices;
-	}
-
-	if (!devices) {
-		applog(LOG_INFO, "SPI detected, but not KnCminer ASICs");
-		return false;
-	}
-
-	applog(LOG_INFO, "Found a KnC miner with %d ASICs", devices);
-
-	cgpu = calloc(1, sizeof(*cgpu));
-	knc = calloc(1, sizeof(*knc));
-	if (!cgpu || !knc) {
-		applog(LOG_ERR, "KnC miner detected, but failed to allocate memory");
-		return false;
-	}
-
-	knc->ctx = ctx;
-	knc->devices = devices;
-	knc->read_q = 0;
-	knc->write_q = 1;
-	knc->read_a = 0;
-	knc->write_a = 1;
-	knc->read_d = 0;
-	knc->write_d = 1;
-	knc->salt = rand();
-	mutex_init(&knc->lock);
-
-	memset(knc->hwerr_work_id, 0xFF, sizeof(knc->hwerr_work_id));
-
-	_internal_knc_flush_fpga(knc);
-
-	cgpu->drv = &knc_drv;
-	cgpu->name = "KnCminer";
-	cgpu->threads = 1;	// .. perhaps our number of devices?
-
-	cgpu->device_data = knc;
-	add_cgpu(cgpu);
-
-	return true;
-}
-
-// http://www.concentric.net/~Ttwang/tech/inthash.htm
-static unsigned long mix(unsigned long a, unsigned long b, unsigned long c)
-{
-	a = a - b;  a = a - c;  a = a ^ (c >> 13);
-	b = b - c;  b = b - a;  b = b ^ (a << 8);
-	c = c - a;  c = c - b;  c = c ^ (b >> 13);
-	a = a - b;  a = a - c;  a = a ^ (c >> 12);
-	b = b - c;  b = b - a;  b = b ^ (a << 16);
-	c = c - a;  c = c - b;  c = c ^ (b >> 5);
-	a = a - b;  a = a - c;  a = a ^ (c >> 3);
-	b = b - c;  b = b - a;  b = b ^ (a << 10);
-	c = c - a;  c = c - b;  c = c ^ (b >> 15);
-
-	return c;
-}
-
-/* Probe devices and register with add_cgpu */
-void knc_detect(bool __maybe_unused hotplug)
-{
-	int idx;
-
-	srand(mix(clock(), time(NULL), getpid()));
-
-	/* Loop through all possible SPI interfaces */
-	for (idx = 0; idx < MAX_SPIS; ++idx) {
-		struct spidev_context *ctx = spi_new(idx + 1);
-
-		if (ctx != NULL) {
-			if (!knc_detect_one(ctx))
-				spi_free(ctx);
-		}
-	}
-}
-
-/* return value is number of nonces that have been checked since
- * previous call
- */
-static int64_t knc_scanwork(struct thr_info *thr)
-{
-	struct cgpu_info *cgpu = thr->cgpu;
-	struct knc_state *knc = cgpu->device_data;
-	int len, num, next_read_q;
-	int64_t ret;
-
-	applog(LOG_DEBUG, "KnC running scanwork");
-
-	knc_check_disabled_cores(knc);
-
-	num = 0;
-
-	mutex_lock(&knc->lock);
-	/* Prepare tx buffer */
-	memset(spi_txbuf, 0, sizeof(spi_txbuf));
-	next_read_q = knc->read_q;
-	knc_queued_fifo_inc_idx(&next_read_q);
-
-	while (next_read_q != knc->write_q) {
-		knc_work_from_queue_to_spi(knc, &knc->queued_fifo[next_read_q],
-					   &spi_txbuf[num], knc->next_work_id + num);
-		knc_queued_fifo_inc_idx(&next_read_q);
-		++num;
-	}
-	/* knc->read_q is advanced in knc_process_response, not here.
-	 * knc->next_work_id is advanced in knc_process_response as well,
-	 *   because only after SPI response we know how many works were actually
-	 *   consumed by FPGA.
-	 */
-
-	len = spi_transfer(knc->ctx, (uint8_t *)spi_txbuf,
-			   (uint8_t *)&spi_rxbuf, sizeof(spi_txbuf));
-	if (len != sizeof(spi_rxbuf)) {
-		ret = -1;
-		goto out_unlock;
-	}
-
-	applog(LOG_DEBUG, "KnC spi: %d works in request", num);
-
-	ret = knc_process_response(thr, cgpu, &spi_rxbuf);
-out_unlock:
-	mutex_unlock(&knc->lock);
-
-	return ret;
-}
-
-static bool knc_queue_full(struct cgpu_info *cgpu)
-{
-	struct knc_state *knc = cgpu->device_data;
-	int queue_full = false;
-	struct work *work;
-
-	applog(LOG_DEBUG, "KnC running queue full");
-
-	mutex_lock(&knc->lock);
-	if (knc_queued_fifo_full(knc)) {
-		queue_full = true;
-		goto out_unlock;
-	}
-	work = get_queued(cgpu);
-	if (!work)
-		goto out_unlock;
-	knc->queued_fifo[knc->write_q].work = work;
-	knc_queued_fifo_inc_idx(&(knc->write_q));
-	if (knc_queued_fifo_full(knc))
-		queue_full = true;
-out_unlock:
-	mutex_unlock(&knc->lock);
-
-	return queue_full;
-}
-
-static void knc_flush_work(struct cgpu_info *cgpu)
-{
-	struct knc_state *knc = cgpu->device_data;
-	int len, next_read_q, next_read_a;
-	struct work *work;
-
-	applog(LOG_ERR, "KnC running flushwork");
-
-	mutex_lock(&knc->lock);
-	/* Drain queued works */
-	next_read_q = knc->read_q;
-	knc_queued_fifo_inc_idx(&next_read_q);
-
-	while (next_read_q != knc->write_q) {
-		work = knc->queued_fifo[next_read_q].work;
-		work_completed(cgpu, work);
-		knc->queued_fifo[next_read_q].work = NULL;
-		knc->read_q = next_read_q;
-		knc_queued_fifo_inc_idx(&next_read_q);
-	}
-
-	/* Drain active works */
-	next_read_a = knc->read_a;
-	knc_active_fifo_inc_idx(&next_read_a);
-
-	while (next_read_a != knc->write_a) {
-		work = knc->active_fifo[next_read_a].work;
-		work_completed(cgpu, work);
-		knc->active_fifo[next_read_a].work = NULL;
-		knc->read_a = next_read_a;
-		knc_active_fifo_inc_idx(&next_read_a);
-	}
-
-	len = _internal_knc_flush_fpga(knc);
-	if (len > 0)
-		knc_process_response(NULL, cgpu, &spi_rxbuf);
-	mutex_unlock(&knc->lock);
-}
-
-struct device_drv knc_drv = {
-	.drv_id = DRIVER_knc,
-	.dname = "KnCminer",
-	.name = "KnC",
-	.drv_detect = knc_detect,	// Probe for devices, add with add_cgpu
-
-	.hash_work = hash_queued_work,
-	.scanwork = knc_scanwork,
-	.queue_full = knc_queue_full,
-	.flush_work = knc_flush_work,
-
-	.get_api_stats = knc_api_stats,
-};
diff --git a/driver-knc.c b/driver-knc.c
new file mode 100644
index 0000000..f98a60f
--- /dev/null
+++ b/driver-knc.c
@@ -0,0 +1,584 @@
+/*
+ * cgminer driver for KnCminer devices
+ *
+ * Copyright 2014 KnCminer
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at your option)
+ * any later version.  See COPYING for more details.
+ */
+
+#include <stdlib.h>
+#include <assert.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
+#include <sys/time.h>
+#include <linux/types.h>
+#include <linux/spi/spidev.h>
+
+#include <zlib.h>
+
+#include "logging.h"
+#include "miner.h"
+#include "knc-transport.h"
+#include "knc-asic.h"
+
+#define MAX_ASICS               6
+#define DIES_PER_ASIC           4
+#define MAX_CORES_PER_DIE       360
+#define WORKS_PER_CORE          2
+
+#define CORE_ERROR_LIMIT	30
+#define CORE_ERROR_INTERVAL	30
+#define CORE_ERROR_DISABLE_TIME	5*60
+#define CORE_SUBMIT_MIN_TIME	2
+#define CORE_TIMEOUT		10
+
+static struct timeval now;
+static const struct timeval core_check_interval = {
+	CORE_ERROR_INTERVAL, 0
+};
+static const struct timeval core_disable_interval = {
+	CORE_ERROR_DISABLE_TIME, 0
+};
+static const struct timeval core_submit_interval = {
+	CORE_SUBMIT_MIN_TIME, 0
+};
+static const struct timeval core_timeout_interval = {
+	CORE_TIMEOUT, 0
+};
+
+struct knc_die;
+
+struct knc_core_state {
+	int generation;
+	int core;
+	struct knc_die *die;
+	struct {
+		int slot;
+		struct work *work;
+	} workslot[WORKS_PER_CORE];
+	struct {
+		int slot;
+		uint32_t nonce;
+	} seen_nonces[5];
+	struct {
+		int slot;
+		uint32_t nonce;
+	} last_nonce;
+	uint32_t works;
+	uint32_t shares;
+	uint32_t errors;
+	uint32_t completed;
+	int last_slot;
+	uint32_t errors_now;
+	struct timeval disabled_until;
+	struct timeval hold_work_until;
+	struct timeval timeout;
+};
+
+struct knc_state;
+
+struct knc_die {
+	int channel;
+	int die;
+	int version;
+	int cores;
+	struct knc_state *knc;
+	struct knc_core_state *core;
+};
+
+struct knc_state {
+	struct cgpu_info *cgpu;
+	void *ctx;
+	int generation;    /* work/block generation, incremented on each flush invalidating older works */
+	int channel[MAX_ASICS];
+	int dies;
+	struct knc_die die[MAX_ASICS*DIES_PER_ASIC];
+	int cores;
+	int scan_adjust_core;
+	int startup;
+	/* Statistics */
+	uint64_t shares;		/* diff1 shares reported by hardware */
+	uint64_t works;			/* Work units submitted */
+	uint64_t completed;		/* Work units completed */
+	uint64_t errors;		/* Hardware & communication errors */
+	struct timeval next_error_interval;
+	/* End of statistics */
+	struct knc_core_state core[];
+};
+
+int opt_knc_device_idx = 0;
+int opt_knc_device_bus = -1;
+char *knc_log_file = NULL;
+
+static bool knc_detect_one(void *ctx)
+{
+	/* Scan device for ASICs */
+	int channel, die, cores = 0, core;
+	struct cgpu_info *cgpu;
+	struct knc_state *knc;
+	struct knc_die_info die_info[MAX_ASICS][DIES_PER_ASIC];
+
+	memset(die_info, 0, sizeof(die_info));
+
+	/* Send GETINFO to each die to detect if it is usable */
+	for (channel = 0; channel < MAX_ASICS; channel++) {
+		if (!knc_trnsp_asic_detect(ctx, channel))
+			continue;
+		for (die = 0; die < DIES_PER_ASIC; die++) {
+		    if (knc_detect_die(ctx, channel, die, &die_info[channel][die]) == 0)
+			cores += die_info[channel][die].cores;
+		}
+	}
+
+	if (!cores) {
+		applog(LOG_NOTICE, "no KnCminer cores found");
+		return false;
+	}
+
+	applog(LOG_ERR, "Found a KnC miner with %d cores", cores);
+
+	cgpu = calloc(1, sizeof(*cgpu));
+	knc = calloc(1, sizeof(*knc) + cores * sizeof(struct knc_core_state));
+	if (!cgpu || !knc) {
+		applog(LOG_ERR, "KnC miner detected, but failed to allocate memory");
+		return false;
+	}
+
+	knc->cgpu = cgpu;
+	knc->ctx = ctx;
+	knc->generation = 1;
+
+	/* Index all cores */
+	int dies = 0;
+	cores = 0;
+	struct knc_core_state *pcore = knc->core;
+	for (channel = 0; channel < MAX_ASICS; channel++) {
+		for (die = 0; die < DIES_PER_ASIC; die++) {
+			if (die_info[channel][die].cores) {
+				knc->channel[channel] = 1;
+				knc->die[dies].channel = channel;
+				knc->die[dies].die = die;
+				knc->die[dies].version = die_info[channel][die].version;
+				knc->die[dies].cores = die_info[channel][die].cores;
+				knc->die[dies].core = pcore;
+				knc->die[dies].knc = knc;
+				for (core = 0; core < knc->die[dies].cores; core++) {
+					knc->die[dies].core[core].die = &knc->die[dies];
+					knc->die[dies].core[core].core = core;
+				}
+				cores += knc->die[dies].cores;
+				pcore += knc->die[dies].cores;
+				dies++;
+				
+			}
+		}
+	}
+	knc->dies = dies;
+	knc->cores = cores;
+	knc->startup = 2;
+
+	cgpu->drv = &knc_drv;
+	cgpu->name = "KnCminer";
+	cgpu->threads = 1;
+
+	cgpu->device_data = knc;
+
+	add_cgpu(cgpu);
+
+	return true;
+}
+
+/* Probe devices and register with add_cgpu */
+void knc_detect(bool __maybe_unused hotplug)
+{
+	void *ctx = knc_trnsp_new(opt_knc_device_idx);
+
+	if (ctx != NULL) {
+		if (!knc_detect_one(ctx))
+			knc_trnsp_free(ctx);
+	}
+}
+
+/* Core helper functions */
+static int knc_core_hold_work(struct knc_core_state *core)
+{
+	return timercmp(&core->hold_work_until, &now, >);
+}
+
+static int knc_core_need_work(struct knc_core_state *core)
+{
+	return !knc_core_hold_work(core) && !core->workslot[1].work;
+}
+
+static int knc_core_disabled(struct knc_core_state *core)
+{
+	return timercmp(&core->disabled_until, &now, >);
+}
+
+static int knc_core_next_slot(struct knc_core_state *core)
+{
+	int slot = core->last_slot + 1;
+	if (slot >= 15)
+		slot = 1;
+	core->last_slot = slot;
+	return slot;
+}
+
+static void knc_core_failure(struct knc_core_state *core)
+{
+	core->errors++;
+	core->errors_now++;
+	if (knc_core_disabled(core))
+		return;
+	if (core->errors_now > CORE_ERROR_LIMIT) {
+		applog(LOG_ERR, "KnC: %d.%d.%d disabled for %d seconds due to repeated hardware errors",
+			core->die->channel, core->die->die, core->core, core_disable_interval.tv_sec);
+		timeradd(&now, &core_disable_interval, &core->disabled_until);
+	}
+}
+
+static int knc_core_handle_nonce(struct thr_info *thr, struct knc_core_state *core, int slot, uint32_t nonce)
+{
+	int i;
+	if (!slot)
+		return;
+	core->last_nonce.slot = slot;
+	core->last_nonce.nonce = nonce;
+	if (core->die->knc->startup)
+		return;
+	for (i = 0; i < WORKS_PER_CORE; i++) {
+		if (slot == core->workslot[i].slot && core->workslot[i].work) {
+			applog(LOG_INFO, "KnC: %d.%d.%d found nonce %08x", core->die->channel, core->die->die, core->core, nonce);
+			if (submit_nonce(thr, core->workslot[i].work, nonce)) {
+				/* Good share */
+				core->shares++;
+				core->die->knc->shares++;
+			} else {
+				applog(LOG_INFO, "KnC: %d.%d.%d hwerror nonce %08x", core->die->channel, core->die->die, core->core, nonce);
+				/* Bad share */
+				knc_core_failure(core);
+			}
+		}
+	}
+}
+
+static int knc_core_process_report(struct thr_info *thr, struct knc_core_state *core, uint8_t *report)
+{
+	int n_nonces = core->die->version == KNC_VERSION_NEPTUNE ? 5 : 1;
+	struct {
+		int slot;
+		uint32_t nonce;
+	} nonces[5];
+	int n;
+	for (n = 0; n < n_nonces; n++) {
+		int slot = report[1+1+0+(1+4)*n]&0x0f;
+		uint32_t nonce = report[1+1+1+(1+4)*n] << 24 |
+				report[1+1+2+(1+4)*n] << 16 |
+				report[1+1+3+(1+4)*n] << 8 |
+				report[1+1+4+(1+4)*n] << 0;
+		if (core->last_nonce.slot == slot && core->last_nonce.nonce == nonce)
+			break;
+		nonces[n].slot = slot;
+		nonces[n].nonce = nonce;
+	}
+	while(n-- > 0) {
+		knc_core_handle_nonce(thr, core, nonces[n].slot, nonces[n].nonce);
+	}
+
+	int active_slot = report[2] >> 4;
+	if (active_slot && core->workslot[1].slot == active_slot) {
+		/* Core switched to next work */
+		if (core->workslot[0].work) {
+			core->die->knc->completed++;
+			core->completed++;
+			applog(LOG_INFO, "KnC: Work completed on core %d.%d.%d!", core->die->channel, core->die->die, core->core);
+			free_work(core->workslot[0].work);
+		}
+		core->workslot[0] = core->workslot[1];
+		core->workslot[1].work = NULL;
+		core->workslot[1].slot = 0;
+	}
+
+	return 0;
+}
+
+static int knc_core_send_work(struct thr_info *thr, struct knc_core_state *core, struct work *work, bool clean)
+{
+	struct knc_state *knc = core->die->knc;
+	struct cgpu_info *cgpu = knc->cgpu;
+	int request_length = 4 + 1 + 6*4 + 3*4 + 8*4;
+	uint8_t request[request_length];
+	int response_length = 1 + 1 + (1 + 4) * 5;
+	uint8_t response[response_length];
+	int status;
+
+	int slot = knc_core_next_slot(core);
+	if (slot < 0)
+		goto error;
+
+	applog(LOG_INFO, "KnC setwork%s %d.%d.%d slot %x", clean ? " CLEAN" : "", core->die->channel, core->die->die, core->core, slot);
+	if (!clean && !knc_core_need_work(core))
+		goto error;
+
+	switch(core->die->version) {
+	case KNC_VERSION_JUPITER:
+		if (clean) {
+			/* Double halt to get rid of any previous queued work */
+			request_length = knc_prepare_jupiter_halt(request, core->die->die, core->core);
+			knc_syncronous_transfer(knc->ctx, core->die->channel, request_length, request, 0, NULL);
+			knc_syncronous_transfer(knc->ctx, core->die->channel, request_length, request, 0, NULL);
+		}
+		request_length = knc_prepare_jupiter_setwork(request, core->die->die, core->core, slot, work);
+		knc_syncronous_transfer(knc->ctx, core->die->channel, request_length, request, 0, NULL);
+		break;
+	case KNC_VERSION_NEPTUNE:
+		request_length = knc_prepare_neptune_setwork(request, core->die->die, core->core, slot, work, clean);
+		status = knc_syncronous_transfer(knc->ctx, core->die->channel, request_length, request, response_length, response);
+		if (status != KNC_ACCEPTED) {
+			applog(LOG_INFO, "KnC: Communication error %x", status);
+			goto error;
+		}
+		knc_core_process_report(thr, core, response);
+		break;
+	default:
+		goto error;
+	}
+
+	core->workslot[1].work = work;
+	core->workslot[1].slot = slot;
+	core->generation = knc->generation;
+	core->works++;
+
+	timeradd(&now, &core_submit_interval, &core->hold_work_until);
+	timeradd(&now, &core_timeout_interval, &core->timeout);
+
+	return 0;
+
+error:
+	applog(LOG_NOTICE, "KnC: %d.%d.%d Failed to setwork (%d)",
+			core->die->channel, core->die->die, core->core, core->errors_now);
+	if (core->generation != ~0) {
+		core->generation = ~0;	/* Flush it, We are likely out of sync */
+	} else {
+		knc_core_failure(core);
+	}
+	free_work(work);
+	return -1;
+}
+
+static int knc_core_get_report(struct thr_info *thr, struct knc_core_state *core)
+{
+	struct knc_state *knc = core->die->knc;
+	struct cgpu_info *cgpu = knc->cgpu;
+	int request_length = 4;
+	uint8_t request[request_length];
+	int response_length = 1 + 1 + (1 + 4) * 5;
+	uint8_t response[response_length];
+	int status;
+
+	request_length = knc_prepare_report(request, core->die->die, core->core);
+
+	switch(core->die->version) {
+	case KNC_VERSION_JUPITER:
+		response_length = 1 + 1 + (1 + 4);
+		knc_syncronous_transfer(knc->ctx, core->die->channel, request_length, request, response_length, response);
+		knc_core_process_report(thr, core, response);
+		return 0;
+	case KNC_VERSION_NEPTUNE:
+		status = knc_syncronous_transfer(knc->ctx, core->die->channel, request_length, request, response_length, response);
+		if (status != 0)
+		    break;
+		knc_core_process_report(thr, core, response);
+		return 0;
+	}
+
+error:
+	applog(LOG_NOTICE, "KnC: Failed to scan work report");
+	knc_core_failure(core);
+	return -1;
+}
+
+/* return value is number of nonces that have been checked since
+ * previous call
+ */
+static int64_t knc_scanwork(struct thr_info *thr)
+{
+#define KNC_COUNT_UNIT shares
+	struct cgpu_info *cgpu = thr->cgpu;
+	struct knc_state *knc = cgpu->device_data;
+	int64_t ret = 0;
+	uint32_t last_count = knc->KNC_COUNT_UNIT;
+
+	applog(LOG_DEBUG, "KnC running scanwork");
+
+	gettimeofday(&now, NULL);
+
+	knc_trnsp_periodic_check(knc->ctx);
+
+	int i;
+
+	if (timercmp(&knc->next_error_interval, &now, >)) {
+		/* Reset hw error limiter every check interval */
+		timeradd(&now, &core_check_interval, &knc->next_error_interval);
+		for (i = 0; i < knc->cores; i++) {
+			struct knc_core_state *core = &knc->core[i];
+			core->errors_now = 0;
+		}
+	}
+
+	for (i = 0; i < knc->cores; i++) {
+		bool clean = false;
+		struct knc_core_state *core = &knc->core[i];
+		if (core->generation != knc->generation || timercmp(&core->timeout, &now, <)) {
+			/* clean set state, forget everything */
+			clean = true;
+			int slot;
+			for (slot = 0; slot < WORKS_PER_CORE; slot ++) {
+				if (core->workslot[slot].work)
+					free_work(core->workslot[slot].work);
+			}
+			core->hold_work_until = now;
+		}
+		if (knc_core_disabled(core))
+			continue;
+		if (i == knc->scan_adjust_core) {
+			/* TODO: Do a forced submit to even out work generation over time.
+			 * but don't forget scheduled works until the new one gets active
+			 */
+		}
+		if (knc_core_need_work(core)) {
+			struct work *work = get_work(thr, thr->id);
+			knc_core_send_work(thr, core, work, clean);
+		} else {
+			knc_core_get_report(thr, core);
+		}
+	}
+	if (knc->startup)
+		knc->startup--;
+
+	if (knc->scan_adjust_core < knc->cores)
+		knc->scan_adjust_core++;
+
+	return (int64_t)(knc->KNC_COUNT_UNIT - last_count) * 0x100000000UL;
+}
+
+static void knc_flush_work(struct cgpu_info *cgpu)
+{
+	struct knc_state *knc = cgpu->device_data;
+
+	applog(LOG_INFO, "KnC running flushwork");
+
+	knc->generation++;
+	knc->scan_adjust_core=0;
+	if (!knc->generation)
+		knc->generation++;
+}
+
+static void knc_zero_stats(struct cgpu_info *cgpu)
+{
+	int core;
+	struct knc_state *knc = cgpu->device_data;
+	for (core = 0; core < knc->cores; core++) {
+		knc->completed = 0;
+		knc->core[core].errors = 0;
+		knc->core[core].shares = 0;
+		knc->core[core].completed = 0;
+	}
+}
+
+static struct api_data *knc_api_stats(struct cgpu_info *cgpu)
+{
+	struct knc_state *knc = cgpu->device_data;
+	struct api_data *root = NULL;
+	unsigned int cursize;
+	int asic, core, n;
+	char label[256];
+
+	root = api_add_int(root, "dies", &knc->dies, 1);
+	root = api_add_int(root, "cores", &knc->cores, 1);
+	root = api_add_uint64(root, "shares", &knc->shares, 1);
+	root = api_add_uint64(root, "works", &knc->works, 1);
+	root = api_add_uint64(root, "completed", &knc->completed, 1);
+	root = api_add_uint64(root, "errors", &knc->errors, 1);
+
+	/* Active cores */
+	int active = knc->cores;
+	for (core = 0; core < knc->cores; core++) {
+		if (knc_core_disabled(&knc->core[core]))
+			active -= 1;
+	}
+	root = api_add_int(root, "active", &active, 1);
+
+	/* Per ASIC/die data */
+	for (n = 0; n < knc->dies; n++) {
+		struct knc_die *die = &knc->die[n];
+
+#define knc_api_die_string(name, value) do { \
+	snprintf(label, sizeof(label), "%d.%d.%s", die->channel, die->die, name); \
+	root = api_add_string(root, label, value, 1); \
+	} while(0)
+#define knc_api_die_int(name, value) do { \
+	snprintf(label, sizeof(label), "%d.%d.%s", die->channel, die->die, name); \
+	uint64_t v = value; \
+	root = api_add_uint64(root, label, &v, 1); \
+	} while(0)
+
+		/* Model */
+		{
+			char *model = "?";
+			switch(die->version) {
+			case KNC_VERSION_JUPITER:
+				model = "Jupiter";
+				break;
+			case KNC_VERSION_NEPTUNE:
+				model = "Neptune";
+				break;
+			}
+			knc_api_die_string("model", model);
+			knc_api_die_int("cores", die->cores);
+		}
+
+		/* Core based stats */
+		{
+			int active = 0;
+			uint64_t errors = 0;
+			uint64_t shares = 0;
+			uint64_t works = 0;
+			uint64_t completed = 0;
+			char coremap[die->cores+1];
+
+			/* core map */
+			for (core = 0; core < die->cores; core++) {
+				coremap[core] = knc_core_disabled(&die->core[core]) ? '0' : '1';
+				works += die->core[core].works;
+				shares += die->core[core].shares;
+				errors += die->core[core].errors;
+				completed += die->core[core].completed;
+			}
+			knc_api_die_string("coremap", coremap);
+			knc_api_die_int("errors", errors);
+			knc_api_die_int("shares", shares);
+			knc_api_die_int("works", works);
+			knc_api_die_int("completed", completed);
+		}
+	}
+
+	return root;
+}
+
+struct device_drv knc_drv = {
+	.drv_id = DRIVER_knc,
+	.dname = "KnCminer Neptune",
+	.name = "KnC",
+	.drv_detect = knc_detect,
+	.hash_work = hash_driver_work,
+	.flush_work = knc_flush_work,
+	.scanwork = knc_scanwork,
+	.zero_stats = knc_zero_stats,
+	.get_api_stats = knc_api_stats,
+};
diff --git a/knc-asic.c b/knc-asic.c
new file mode 100644
index 0000000..8d35e97
--- /dev/null
+++ b/knc-asic.c
@@ -0,0 +1,391 @@
+/*
+ * library for KnCminer devices
+ *
+ * Copyright 2014 KnCminer
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at your option)
+ * any later version.  See COPYING for more details.
+ */
+
+#include <stdlib.h>
+#include <assert.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
+#include <linux/types.h>
+#include <linux/spi/spidev.h>
+#include <stdint.h>
+#include <string.h>
+
+#include <zlib.h>
+
+#include "miner.h"
+#include "logging.h"
+
+#include "knc-transport.h"
+
+#include "knc-asic.h"
+
+
+#define MAX_ASICS               6
+#define DIES_PER_ASIC           4
+#define MAX_CORES_PER_DIE       360
+#define WORKS_PER_CORE          2
+
+/* ASIC Command codes */
+#define	ASIC_CMD_GETINFO             0x80
+#define ASIC_CMD_SETWORK             0x81
+#define ASIC_CMD_SETWORK_CLEAN       0x83        /* Neptune */
+#define ASIC_CMD_HALT                0x83        /* Jupiter */
+#define ASIC_CMD_REPORT              0x82
+
+#define ASIC_ACK_CRC                    (1<<5)
+#define ASIC_ACK_ACCEPT                 (1<<2)
+#define ASIC_ACK_MASK                   (~(ASIC_ACK_CRC|ASIC_ACK_ACCEPT))
+#define ASIC_ACK_MATCH                  ((1<<7)|(1<<0))
+
+#define ASIC_VERSION_JUPITER            0xa001
+#define ASIC_VERSION_NEPTUNE            0xa002
+
+/* ASIC Command structure
+ * command      8 bits
+ * chip         8 bits
+ * core         16 bits
+ * data         [command dependent]
+ * CRC32        32 bits (Neptune)
+ *
+ * ASIC response starts immediately after core address bits.
+ *
+ * response data
+ * CRC32        32 bits (Neptune)
+ * STATUS       8 bits   1 0 ~CRC_OK 0 0 ACCEPTED_WORK 0 1 (Neptune)
+ *
+ * Requests
+ *
+ * SETWORK (Jupiter)
+ * midstate     256 bits
+ * data         96 bits
+ *
+ * SETWORK/SETWORK_CLEAN (Neptune)
+ * slot | 0xf0  8 bits
+ * precalc_midstate  192 bits
+ * precalc_data 96 bits
+ * midstate     256 bits
+ *
+ * Returns REPORT response on Neptune
+ *
+ * Responses
+ *
+ * GETINFO
+ *
+ * (core field unused)
+ *
+ * cores        16 bits
+ * version      16 bits
+ * reserved     32 bits         (Neptune)
+ * reserved     32 bits         (Neptune)
+ * reserved     cores * 2 bits  (Neptune) rounded up to bytes
+ *
+ * REPORT
+ *
+ * reserved     2 bits
+ * next_state   1 bit   next work state loaded
+ * state        1 bit   hashing  (0 on Jupiter)
+ * next_slot    4 bit   slot id of next work state (0 on Jupiter)
+ * progress     8 bits  upper 8 bits of nonce counter
+ * active_slot  4 bits  slot id of current work state
+ * nonce_slot   4 bits  slot id of found nonce
+ * nonce        32 bits
+ * 
+ * reserved     4 bits
+ * nonce_slot   4 bits
+ * nonce        32 bits
+ *
+ * repeat for 5 nonce entries in total on Neptune
+ * Jupiter only has first nonce entry
+ */
+
+// Precalculate first 3 rounds of SHA256 - as much as possible	
+// Macro routines copied from sha2.c
+static void knc_prepare_neptune_work(unsigned char *out, struct work *work) {
+        const uint8_t *midstate = work->midstate;
+        const uint8_t *data = work->data + 16*4;
+
+#ifndef GET_ULONG_BE
+#define GET_ULONG_BE(b,i)                             \
+		(( (uint32_t) (b)[(i)    ] << 24 )	\
+                | ( (uint32_t) (b)[(i) + 1] << 16 )	\
+                | ( (uint32_t) (b)[(i) + 2] <<  8 )	\
+                | ( (uint32_t) (b)[(i) + 3]       ))
+#endif
+
+#ifndef GET_ULONG_LE
+#define GET_ULONG_LE(b,i)                             \
+		(( (uint32_t) (b)[(i) + 3] << 24 )	\
+                | ( (uint32_t) (b)[(i) + 2] << 16 )	\
+                | ( (uint32_t) (b)[(i) + 1] <<  8 )	\
+                | ( (uint32_t) (b)[(i) + 0]       ))
+#endif
+
+#ifndef PUT_ULONG_BE
+#define PUT_ULONG_BE(n,b,i)                             \
+	{						\
+		(b)[(i)    ] = (unsigned char) ( (n) >> 24 );	\
+		(b)[(i) + 1] = (unsigned char) ( (n) >> 16 );	\
+		(b)[(i) + 2] = (unsigned char) ( (n) >>  8 );	\
+		(b)[(i) + 3] = (unsigned char) ( (n)       );	\
+	}
+#endif
+
+#ifndef PUT_ULONG_LE
+#define PUT_ULONG_LE(n,b,i)                             \
+	{						\
+		(b)[(i) + 3] = (unsigned char) ( (n) >> 24 );	\
+		(b)[(i) + 2] = (unsigned char) ( (n) >> 16 );	\
+		(b)[(i) + 1] = (unsigned char) ( (n) >>  8 );	\
+		(b)[(i) + 0] = (unsigned char) ( (n)       );	\
+	}
+#endif
+
+#define  SHR(x,n) ((x & 0xFFFFFFFF) >> n)
+#define ROTR(x,n) (SHR(x,n) | (x << (32 - n)))
+
+#define S0(x) (ROTR(x, 7) ^ ROTR(x,18) ^  SHR(x, 3))
+#define S1(x) (ROTR(x,17) ^ ROTR(x,19) ^  SHR(x,10))
+
+#define S2(x) (ROTR(x, 2) ^ ROTR(x,13) ^ ROTR(x,22))
+#define S3(x) (ROTR(x, 6) ^ ROTR(x,11) ^ ROTR(x,25))
+
+#define F0(x,y,z) ((x & y) | (z & (x | y)))
+#define F1(x,y,z) (z ^ (x & (y ^ z)))
+
+#define R(t)                                    \
+(                                               \
+    W[t] = S1(W[t -  2]) + W[t -  7] +          \
+           S0(W[t - 15]) + W[t - 16]            \
+)
+
+#define P(a,b,c,d,e,f,g,h,x,K)                  \
+	{					\
+		temp1 = h + S3(e) + F1(e,f,g) + K + x;	\
+		temp2 = S2(a) + F0(a,b,c);		\
+		d += temp1; h = temp1 + temp2;		\
+	}
+
+    uint32_t temp1, temp2, W[16+3];
+    uint32_t A, B, C, D, E, F, G, H;
+
+    W[0] = GET_ULONG_LE(data,  0*4 );
+    W[1] = GET_ULONG_LE(data,  1*4 );
+    W[2] = GET_ULONG_LE(data,  2*4 );
+    W[3] = 0;                 // since S0(0)==0, this must be 0. S0(nonce) is added in hardware.
+    W[4] = 0x80000000;
+    W[5] = 0;
+    W[6] = 0;
+    W[7] = 0;
+    W[8] = 0;
+    W[9] = 0;
+    W[10] = 0;
+    W[11] = 0;
+    W[12] = 0;
+    W[13] = 0;
+    W[14] = 0;
+    W[15] = 0x00000280;
+    R(16);  // Expand W 14, 9, 1, 0
+    R(17);  //          15, 10, 2, 1
+    R(18);  //          16, 11, 3, 2
+
+    A = GET_ULONG_LE(midstate, 0*4 );
+    B = GET_ULONG_LE(midstate, 1*4 );
+    C = GET_ULONG_LE(midstate, 2*4 );
+    D = GET_ULONG_LE(midstate, 3*4 );
+    E = GET_ULONG_LE(midstate, 4*4 );
+    F = GET_ULONG_LE(midstate, 5*4 );
+    G = GET_ULONG_LE(midstate, 6*4 );
+    H = GET_ULONG_LE(midstate, 7*4 );
+
+    uint32_t D_ = D, H_ = H;
+    P( A, B, C, D_, E, F, G, H_, W[ 0], 0x428A2F98 );
+    uint32_t C_ = C, G_ = G;
+    P( H_, A, B, C_, D_, E, F, G_, W[ 1], 0x71374491 );
+    uint32_t B_ = B, F_ = F;
+    P( G_, H_, A, B_, C_, D_, E, F_, W[ 2], 0xB5C0FBCF );
+
+    PUT_ULONG_BE( D_, out, 0*4 );
+    PUT_ULONG_BE( C_, out, 1*4 );
+    PUT_ULONG_BE( B_, out, 2*4 );
+    PUT_ULONG_BE( H_, out, 3*4 );
+    PUT_ULONG_BE( G_, out, 4*4 );
+    PUT_ULONG_BE( F_, out, 5*4 );
+    PUT_ULONG_BE( W[18], out, 6*4 );  // This is partial S0(nonce) added by hardware
+    PUT_ULONG_BE( W[17], out, 7*4 );
+    PUT_ULONG_BE( W[16], out, 8*4 );
+    PUT_ULONG_BE( H, out, 9*4 );
+    PUT_ULONG_BE( G, out, 10*4 );
+    PUT_ULONG_BE( F, out, 11*4 );
+    PUT_ULONG_BE( E, out, 12*4 );
+    PUT_ULONG_BE( D, out, 13*4 );
+    PUT_ULONG_BE( C, out, 14*4 );
+    PUT_ULONG_BE( B, out, 15*4 );
+    PUT_ULONG_BE( A, out, 16*4 );
+}
+
+static void knc_prepare_jupiter_work(unsigned char *out, struct work *work) {
+        int i;
+        for (i = 0; i < 8 * 4; i++)
+                out[i] = work->midstate[8 * 4 - i - 1];
+        for (i = 0; i < 3 * 4; i++)
+                out[8 * 4 + i] = work->data[16 * 4 + 3 * 4 - i - 1];
+}
+
+static void knc_prepare_core_command(uint8_t *request, int command, int die, int core)
+{
+	request[0] = command;
+	request[1] = die;
+	request[2] = core >> 8;
+	request[3] = core & 0xff;
+}
+
+int knc_prepare_report(uint8_t *request, int die, int core)
+{
+	knc_prepare_core_command(request, ASIC_CMD_REPORT, die, core);
+	return 4;
+}
+
+int knc_prepare_neptune_setwork(uint8_t *request, int die, int core, int slot, struct work *work, int clean)
+{
+	if (!clean)
+		knc_prepare_core_command(request, ASIC_CMD_SETWORK, die, core);
+	else
+		knc_prepare_core_command(request, ASIC_CMD_SETWORK_CLEAN, die, core);
+	request[4] = slot | 0xf0;
+	if (work)
+		knc_prepare_neptune_work(request + 4 + 1, work);
+	else
+		memset(request + 4 + 1, 0, 6*4 + 3*4 + 8*4);
+	return 4 + 1 + 6*4 + 3*4 + 8*4;
+}
+
+int knc_prepare_jupiter_setwork(uint8_t *request, int die, int core, int slot, struct work *work)
+{
+	knc_prepare_core_command(request, ASIC_CMD_SETWORK, die, core);
+	request[4] = slot | 0xf0;
+	if (work)
+		knc_prepare_jupiter_work(request + 4 + 1, work);
+	else
+		memset(request + 4 + 1, 0, 8*4 + 3*4);
+	return 4 + 1 + 8*4 + 3*4;
+}
+
+int knc_prepare_jupiter_halt(uint8_t *request, int die, int core)
+{
+	knc_prepare_core_command(request, ASIC_CMD_HALT, die, core);
+	return 4;
+}
+
+int knc_prepare_neptune_halt(uint8_t *request, int die, int core)
+{
+	knc_prepare_core_command(request, ASIC_CMD_HALT, die, core);
+	request[4] = 0 | 0xf0;
+	memset(request + 4 + 1, 0, 6*4 + 3*4 + 8*4);
+	return 4 + 1 + 6*4 + 3*4 + 8*4;
+}
+
+void knc_prepare_neptune_message(int request_length, const uint8_t *request, uint8_t *buffer)
+{
+    uint32_t crc;
+    memcpy(buffer, request, request_length);
+    buffer += request_length;
+    crc = crc32(0, Z_NULL, 0);
+    crc = crc32(crc, request, request_length);
+    PUT_ULONG_BE(crc, buffer, 0);
+}
+
+int knc_prepare_transfer(uint8_t *txbuf, int offset, int size, int channel, int request_length, const uint8_t *request, int response_length)
+{
+        int msglen = MAX(request_length, 4 + response_length ) + 4 + 1 + 3;
+        int len = 2 + msglen;
+	txbuf += offset;
+
+	if (len + offset > size) {
+		applog(LOG_DEBUG, "KnC SPI buffer full");
+		return -1;
+	}
+	txbuf[0] = 1 << 7 | (channel+1) << 4 | (msglen * 8) >> 8;
+	txbuf[1] = (msglen * 8);
+	knc_prepare_neptune_message(request_length, request, txbuf+2);
+
+	return len;
+}
+
+int knc_process_reply(uint8_t *rxbuf, int len, uint8_t *response, int response_length)
+{
+    int ret = 0;
+    if (response_length > 0) {
+        uint32_t crc, recv_crc;
+	crc = crc32(0, Z_NULL, 0);
+        crc = crc32(crc, rxbuf + 2 + 4, response_length);
+	recv_crc = GET_ULONG_BE(rxbuf + 2 + 4, response_length);
+	if (crc != recv_crc)
+                ret |= KNC_ERR_CRC;
+	memcpy(response, rxbuf + 2 + 4, response_length);
+    }
+    uint8_t ack = rxbuf[len - 4]; /* 2 + MAX(4 + response_length, request_length) + 4; */
+
+    if ((ack & ASIC_ACK_MASK) != ASIC_ACK_MATCH)
+        ret |= KNC_ERR_ACK;
+    if ((ack & ASIC_ACK_CRC))
+        ret |= KNC_ERR_CRCACK;
+    if ((ack & ASIC_ACK_ACCEPT))
+        ret |= KNC_ACCEPTED;
+    return ret;
+}
+
+int knc_syncronous_transfer(void *ctx, int channel, int request_length, const uint8_t *request, int response_length, uint8_t *response)
+{
+    /* FPGA control, request header, request body/response, CRC(4), ACK(1), EXTRA(3) */
+    int msglen = MAX(request_length, 4 + response_length ) + 4 + 1 + 3;
+    int len = 2 + msglen;
+    uint8_t txbuf[len];
+    uint8_t rxbuf[len];
+    memset(txbuf, 0, len);
+    knc_prepare_transfer(txbuf, 0, len, channel, request_length, request, response_length);
+    knc_trnsp_transfer(ctx, txbuf, rxbuf, len);
+
+    return knc_process_reply(rxbuf, len, response, response_length);
+
+}
+
+int knc_detect_die(void *ctx, int channel, int die, struct knc_die_info *die_info)
+{
+	uint8_t get_info[4] = { ASIC_CMD_GETINFO, die, 0, 0 };
+	int response_len = 2 + 2 + 4 + 4 + (MAX_CORES_PER_DIE*2 + 7) / 8;
+	uint8_t response[response_len];
+	int status = knc_syncronous_transfer(ctx, channel, 4, get_info, response_len, response);
+	int cores_in_die = response[0]<<8 | response[1];
+	int version = response[2]<<8 | response[3];
+	if (version == ASIC_VERSION_NEPTUNE && cores_in_die < MAX_CORES_PER_DIE) {
+		applog(LOG_DEBUG, "KnC %d-%d: Looks like a NEPTUNE die with %d cores", channel, die, cores_in_die);
+		/* Try again with right response size */
+		response_len = 2 + 2 + 4 + 4 + (cores_in_die*2 + 7) / 8;
+		status = knc_syncronous_transfer(ctx, channel, 4, get_info, response_len, response);
+	}
+	if (version == ASIC_VERSION_JUPITER) {
+		applog(LOG_INFO, "KnC %d-%d: Found JUPITER die with %d cores", channel, die, cores_in_die);
+		die_info->version = KNC_VERSION_JUPITER;
+		die_info->cores = cores_in_die;
+		return 0;
+	} else if (version == ASIC_VERSION_NEPTUNE && status == 0) {
+		applog(LOG_INFO, "KnC %d-%d: Found NEPTUNE die with %d cores", channel, die, cores_in_die);
+		die_info->version = KNC_VERSION_NEPTUNE;
+		die_info->cores = cores_in_die;
+		return 0;
+	} else {
+		applog(LOG_DEBUG, "KnC %d-%d: No KnC chip found", channel, die);
+		return -1;
+	}
+}
+
diff --git a/knc-asic.h b/knc-asic.h
new file mode 100644
index 0000000..d478f92
--- /dev/null
+++ b/knc-asic.h
@@ -0,0 +1,33 @@
+#ifndef _CGMINER_NEPTUNE_H
+#define _CGMINER_NEPTUNE_H
+
+#include <stdint.h>
+#include "miner.h"
+
+struct knc_die_info {
+	enum {
+		KNC_VERSION_UNKNOWN = 0,
+		KNC_VERSION_JUPITER,
+		KNC_VERSION_NEPTUNE
+	} version;
+	int cores;
+};
+
+int knc_prepare_report(uint8_t *request, int die, int core);
+int knc_prepare_neptune_setwork(uint8_t *request, int die, int core, int slot, struct work *work, int clean);
+int knc_prepare_jupiter_setwork(uint8_t *request, int die, int core, int slot, struct work *work);
+int knc_prepare_jupiter_halt(uint8_t *request, int die, int core);
+int knc_prepare_neptune_halt(uint8_t *request, int die, int core);
+
+void knc_prepare_neptune_message(int request_length, const uint8_t *request, uint8_t *buffer);
+
+#define KNC_ACCEPTED    (1<<0)
+#define KNC_ERR_CRC     (1<<1)
+#define KNC_ERR_ACK     (1<<2)
+#define KNC_ERR_CRCACK  (1<<3)
+
+int knc_syncronous_transfer(void *ctx, int channel, int request_length, const uint8_t *request, int response_length, uint8_t *response);
+
+int knc_detect_die(void *ctx, int channel, int die, struct knc_die_info *die_info);
+
+#endif
diff --git a/knc-transport-spi.c b/knc-transport-spi.c
new file mode 100644
index 0000000..5cefdf6
--- /dev/null
+++ b/knc-transport-spi.c
@@ -0,0 +1,148 @@
+/*
+ * Direct SPI transport layer for KnCminer Jupiters
+ *
+ * Copyright 2014 KnCminer
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at your option)
+ * any later version.  See COPYING for more details.
+ */
+
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <linux/spi/spidev.h>
+
+#include "logging.h"
+#include "miner.h"
+#include "hexdump.c"
+#include "knc-transport.h"
+
+#define SPI_DEVICE_TEMPLATE	"/dev/spidev%d.%d"
+#define SPI_MODE		(SPI_CPHA | SPI_CPOL | SPI_CS_HIGH)
+#define SPI_BITS_PER_WORD	8
+#define SPI_MAX_SPEED		3000000
+#define SPI_DELAY_USECS		0
+
+struct spidev_context {
+	int fd;
+	uint32_t speed;
+	uint16_t delay;
+	uint8_t mode;
+	uint8_t bits;
+};
+
+/* Init SPI transport */
+void *knc_trnsp_new(int dev_idx)
+{
+	struct spidev_context *ctx;
+	char dev_name[PATH_MAX];
+
+	if (NULL == (ctx = malloc(sizeof(struct spidev_context)))) {
+		applog(LOG_ERR, "KnC transport: Out of memory");
+		goto l_exit_error;
+	}
+	ctx->mode = SPI_MODE;
+	ctx->bits = SPI_BITS_PER_WORD;
+	ctx->speed = SPI_MAX_SPEED;
+	ctx->delay = SPI_DELAY_USECS;
+
+	ctx->fd = -1;
+	sprintf(dev_name, SPI_DEVICE_TEMPLATE,
+		dev_idx + 1, /* bus */
+		0    /* chipselect */
+	       );
+	if (0 > (ctx->fd = open(dev_name, O_RDWR))) {
+		applog(LOG_ERR, "KnC transport: Can not open SPI device %s: %m",
+		       dev_name);
+		goto l_free_exit_error;
+	}
+
+	/*
+	 * spi mode
+	 */
+	if (0 > ioctl(ctx->fd, SPI_IOC_WR_MODE, &ctx->mode))
+		goto l_ioctl_error;
+	if (0 > ioctl(ctx->fd, SPI_IOC_RD_MODE, &ctx->mode))
+		goto l_ioctl_error;
+
+	/*
+	 * bits per word
+	 */
+	if (0 > ioctl(ctx->fd, SPI_IOC_WR_BITS_PER_WORD, &ctx->bits))
+		goto l_ioctl_error;
+	if (0 > ioctl(ctx->fd, SPI_IOC_RD_BITS_PER_WORD, &ctx->bits))
+		goto l_ioctl_error;
+
+	/*
+	 * max speed hz
+	 */
+	if (0 > ioctl(ctx->fd, SPI_IOC_WR_MAX_SPEED_HZ, &ctx->speed))
+		goto l_ioctl_error;
+	if (0 > ioctl(ctx->fd, SPI_IOC_RD_MAX_SPEED_HZ, &ctx->speed))
+		goto l_ioctl_error;
+
+	applog(LOG_INFO, "KnC transport: SPI device %s uses mode %hhu, bits %hhu, speed %u",
+	       dev_name, ctx->mode, ctx->bits, ctx->speed);
+
+	return ctx;
+
+l_ioctl_error:
+	applog(LOG_ERR, "KnC transport: ioctl error on SPI device %s: %m", dev_name);
+	close(ctx->fd);
+l_free_exit_error:
+	free(ctx);
+l_exit_error:
+	return NULL;
+}
+
+void knc_trnsp_free(void *opaque_ctx)
+{
+	struct spidev_context *ctx = opaque_ctx;
+
+	if (NULL == ctx)
+		return;
+
+	close(ctx->fd);
+	free(ctx);
+}
+
+int knc_trnsp_transfer(void *opaque_ctx, uint8_t *txbuf, uint8_t *rxbuf, int len)
+{
+	struct spidev_context *ctx = opaque_ctx;
+	struct spi_ioc_transfer xfr;
+	int ret;
+
+	memset(rxbuf, 0xff, len);
+
+	ret = len;
+
+	xfr.tx_buf = (unsigned long)txbuf;
+	xfr.rx_buf = (unsigned long)rxbuf;
+	xfr.len = len;
+	xfr.speed_hz = ctx->speed;
+	xfr.delay_usecs = ctx->delay;
+	xfr.bits_per_word = ctx->bits;
+	xfr.cs_change = 0;
+	xfr.pad = 0;
+
+        applog(LOG_DEBUG, "KnC spi:");
+        hexdump(txbuf, len);
+	if (1 > (ret = ioctl(ctx->fd, SPI_IOC_MESSAGE(1), &xfr)))
+		applog(LOG_ERR, "KnC spi xfer: ioctl error on SPI device: %m");
+        hexdump(rxbuf, len);
+
+	return ret;
+}
+
+bool knc_trnsp_asic_detect(void *opaque_ctx, int chip_id)
+{
+	return true;
+}
+
+void knc_trnsp_periodic_check(void *opaque_ctx)
+{
+	return;
+}
+
diff --git a/knc-transport.h b/knc-transport.h
new file mode 100644
index 0000000..3db9110
--- /dev/null
+++ b/knc-transport.h
@@ -0,0 +1,23 @@
+/*
+ * Transport layer interface for KnCminer devices
+ *
+ * Copyright 2014 KnCminer
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at your option)
+ * any later version.  See COPYING for more details.
+ */
+
+#define	MAX_ASICS		6
+#define	NUM_DIES_IN_ASIC	4
+#define	CORES_IN_DIE		48
+#define	CORES_PER_ASIC		(NUM_DIES_IN_ASIC * CORES_IN_DIE)
+
+#define	MAX_BYTES_IN_SPI_XSFER	4096
+
+void *knc_trnsp_new(int dev_idx);
+void knc_trnsp_free(void *opaque_ctx);
+int knc_trnsp_transfer(void *opaque_ctx, uint8_t *txbuf, uint8_t *rxbuf, int len);
+bool knc_trnsp_asic_detect(void *opaque_ctx, int chip_id);
+void knc_trnsp_periodic_check(void *opaque_ctx);