aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/netmap/if_ptnet.c
blob: 5fea3510dc62e0c792ba586c91fc038d2424772f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
/*-
 * Copyright (c) 2016, Vincenzo Maffione
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice unmodified, this list of conditions, and the following
 *    disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $FreeBSD$
 */

/* Driver for ptnet paravirtualized network device. */

#include <sys/cdefs.h>

#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/sockio.h>
#include <sys/mbuf.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/taskqueue.h>
#include <sys/smp.h>
#include <sys/time.h>
#include <machine/smp.h>

#include <vm/uma.h>
#include <vm/vm.h>
#include <vm/pmap.h>

#include <net/ethernet.h>
#include <net/if.h>
#include <net/if_var.h>
#include <net/if_arp.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include <net/if_media.h>
#include <net/if_vlan_var.h>
#include <net/bpf.h>

#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet6/ip6_var.h>
#include <netinet/udp.h>
#include <netinet/tcp.h>
#include <netinet/sctp.h>

#include <machine/bus.h>
#include <machine/resource.h>
#include <sys/bus.h>
#include <sys/rman.h>

#include <dev/pci/pcivar.h>
#include <dev/pci/pcireg.h>

#include "opt_inet.h"
#include "opt_inet6.h"

#include <sys/selinfo.h>
#include <net/netmap.h>
#include <dev/netmap/netmap_kern.h>
#include <net/netmap_virt.h>
#include <dev/netmap/netmap_mem2.h>
#include <dev/virtio/network/virtio_net.h>

#ifndef PTNET_CSB_ALLOC
#error "No support for on-device CSB"
#endif

#ifndef INET
#error "INET not defined, cannot support offloadings"
#endif

#if __FreeBSD_version >= 1100000
static uint64_t	ptnet_get_counter(if_t, ift_counter);
#else
typedef struct ifnet *if_t;
#define if_getsoftc(_ifp)   (_ifp)->if_softc
#endif

//#define PTNETMAP_STATS
//#define DEBUG
#ifdef DEBUG
#define DBG(x) x
#else   /* !DEBUG */
#define DBG(x)
#endif  /* !DEBUG */

extern int ptnet_vnet_hdr; /* Tunable parameter */

struct ptnet_softc;

struct ptnet_queue_stats {
	uint64_t	packets; /* if_[io]packets */
	uint64_t	bytes;	 /* if_[io]bytes */
	uint64_t	errors;	 /* if_[io]errors */
	uint64_t	iqdrops; /* if_iqdrops */
	uint64_t	mcasts;  /* if_[io]mcasts */
#ifdef PTNETMAP_STATS
	uint64_t	intrs;
	uint64_t	kicks;
#endif /* PTNETMAP_STATS */
};

struct ptnet_queue {
	struct ptnet_softc		*sc;
	struct				resource *irq;
	void				*cookie;
	int				kring_id;
	struct ptnet_ring		*ptring;
	unsigned int			kick;
	struct mtx			lock;
	struct buf_ring			*bufring; /* for TX queues */
	struct ptnet_queue_stats	stats;
#ifdef PTNETMAP_STATS
	struct ptnet_queue_stats	last_stats;
#endif /* PTNETMAP_STATS */
	struct taskqueue		*taskq;
	struct task			task;
	char				lock_name[16];
};

#define PTNET_Q_LOCK(_pq)	mtx_lock(&(_pq)->lock)
#define PTNET_Q_TRYLOCK(_pq)	mtx_trylock(&(_pq)->lock)
#define PTNET_Q_UNLOCK(_pq)	mtx_unlock(&(_pq)->lock)

struct ptnet_softc {
	device_t		dev;
	if_t			ifp;
	struct ifmedia		media;
	struct mtx		lock;
	char			lock_name[16];
	char			hwaddr[ETHER_ADDR_LEN];

	/* Mirror of PTFEAT register. */
	uint32_t		ptfeatures;
	unsigned int		vnet_hdr_len;

	/* PCI BARs support. */
	struct resource		*iomem;
	struct resource		*msix_mem;

	unsigned int		num_rings;
	unsigned int		num_tx_rings;
	struct ptnet_queue	*queues;
	struct ptnet_queue	*rxqueues;
	struct ptnet_csb	*csb;

	unsigned int		min_tx_space;

	struct netmap_pt_guest_adapter *ptna;

	struct callout		tick;
#ifdef PTNETMAP_STATS
	struct timeval		last_ts;
#endif /* PTNETMAP_STATS */
};

#define PTNET_CORE_LOCK(_sc)	mtx_lock(&(_sc)->lock)
#define PTNET_CORE_UNLOCK(_sc)	mtx_unlock(&(_sc)->lock)

static int	ptnet_probe(device_t);
static int	ptnet_attach(device_t);
static int	ptnet_detach(device_t);
static int	ptnet_suspend(device_t);
static int	ptnet_resume(device_t);
static int	ptnet_shutdown(device_t);

static void	ptnet_init(void *opaque);
static int	ptnet_ioctl(if_t ifp, u_long cmd, caddr_t data);
static int	ptnet_init_locked(struct ptnet_softc *sc);
static int	ptnet_stop(struct ptnet_softc *sc);
static int	ptnet_transmit(if_t ifp, struct mbuf *m);
static int	ptnet_drain_transmit_queue(struct ptnet_queue *pq,
					   unsigned int budget,
					   bool may_resched);
static void	ptnet_qflush(if_t ifp);
static void	ptnet_tx_task(void *context, int pending);

static int	ptnet_media_change(if_t ifp);
static void	ptnet_media_status(if_t ifp, struct ifmediareq *ifmr);
#ifdef PTNETMAP_STATS
static void	ptnet_tick(void *opaque);
#endif

static int	ptnet_irqs_init(struct ptnet_softc *sc);
static void	ptnet_irqs_fini(struct ptnet_softc *sc);

static uint32_t ptnet_nm_ptctl(if_t ifp, uint32_t cmd);
static int	ptnet_nm_config(struct netmap_adapter *na, unsigned *txr,
				unsigned *txd, unsigned *rxr, unsigned *rxd);
static void	ptnet_update_vnet_hdr(struct ptnet_softc *sc);
static int	ptnet_nm_register(struct netmap_adapter *na, int onoff);
static int	ptnet_nm_txsync(struct netmap_kring *kring, int flags);
static int	ptnet_nm_rxsync(struct netmap_kring *kring, int flags);

static void	ptnet_tx_intr(void *opaque);
static void	ptnet_rx_intr(void *opaque);

static unsigned	ptnet_rx_discard(struct netmap_kring *kring,
				 unsigned int head);
static int	ptnet_rx_eof(struct ptnet_queue *pq, unsigned int budget,
			     bool may_resched);
static void	ptnet_rx_task(void *context, int pending);

#ifdef DEVICE_POLLING
static poll_handler_t ptnet_poll;
#endif

static device_method_t ptnet_methods[] = {
	DEVMETHOD(device_probe,			ptnet_probe),
	DEVMETHOD(device_attach,		ptnet_attach),
	DEVMETHOD(device_detach,		ptnet_detach),
	DEVMETHOD(device_suspend,		ptnet_suspend),
	DEVMETHOD(device_resume,		ptnet_resume),
	DEVMETHOD(device_shutdown,		ptnet_shutdown),
	DEVMETHOD_END
};

static driver_t ptnet_driver = {
	"ptnet",
	ptnet_methods,
	sizeof(struct ptnet_softc)
};

/* We use (SI_ORDER_MIDDLE+2) here, see DEV_MODULE_ORDERED() invocation. */
static devclass_t ptnet_devclass;
DRIVER_MODULE_ORDERED(ptnet, pci, ptnet_driver, ptnet_devclass,
		      NULL, NULL, SI_ORDER_MIDDLE + 2);

static int
ptnet_probe(device_t dev)
{
	if (pci_get_vendor(dev) != PTNETMAP_PCI_VENDOR_ID ||
		pci_get_device(dev) != PTNETMAP_PCI_NETIF_ID) {
		return (ENXIO);
	}

	device_set_desc(dev, "ptnet network adapter");

	return (BUS_PROBE_DEFAULT);
}

static inline void ptnet_kick(struct ptnet_queue *pq)
{
#ifdef PTNETMAP_STATS
	pq->stats.kicks ++;
#endif /* PTNETMAP_STATS */
	bus_write_4(pq->sc->iomem, pq->kick, 0);
}

#define PTNET_BUF_RING_SIZE	4096
#define PTNET_RX_BUDGET		512
#define PTNET_RX_BATCH		1
#define PTNET_TX_BUDGET		512
#define PTNET_TX_BATCH		64
#define PTNET_HDR_SIZE		sizeof(struct virtio_net_hdr_mrg_rxbuf)
#define PTNET_MAX_PKT_SIZE	65536

#define PTNET_CSUM_OFFLOAD	(CSUM_TCP | CSUM_UDP | CSUM_SCTP)
#define PTNET_CSUM_OFFLOAD_IPV6	(CSUM_TCP_IPV6 | CSUM_UDP_IPV6 |\
				 CSUM_SCTP_IPV6)
#define PTNET_ALL_OFFLOAD	(CSUM_TSO | PTNET_CSUM_OFFLOAD |\
				 PTNET_CSUM_OFFLOAD_IPV6)

static int
ptnet_attach(device_t dev)
{
	uint32_t ptfeatures = 0;
	unsigned int num_rx_rings, num_tx_rings;
	struct netmap_adapter na_arg;
	unsigned int nifp_offset;
	struct ptnet_softc *sc;
	if_t ifp;
	uint32_t macreg;
	int err, rid;
	int i;

	sc = device_get_softc(dev);
	sc->dev = dev;

	/* Setup PCI resources. */
	pci_enable_busmaster(dev);

	rid = PCIR_BAR(PTNETMAP_IO_PCI_BAR);
	sc->iomem = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
					   RF_ACTIVE);
	if (sc->iomem == NULL) {
		device_printf(dev, "Failed to map I/O BAR\n");
		return (ENXIO);
	}

	/* Negotiate features with the hypervisor. */
	if (ptnet_vnet_hdr) {
		ptfeatures |= PTNETMAP_F_VNET_HDR;
	}
	bus_write_4(sc->iomem, PTNET_IO_PTFEAT, ptfeatures); /* wanted */
	ptfeatures = bus_read_4(sc->iomem, PTNET_IO_PTFEAT); /* acked */
	sc->ptfeatures = ptfeatures;

	/* Allocate CSB and carry out CSB allocation protocol (CSBBAH first,
	 * then CSBBAL). */
	sc->csb = malloc(sizeof(struct ptnet_csb), M_DEVBUF,
			 M_NOWAIT | M_ZERO);
	if (sc->csb == NULL) {
		device_printf(dev, "Failed to allocate CSB\n");
		err = ENOMEM;
		goto err_path;
	}

	{
		/*
		 * We use uint64_t rather than vm_paddr_t since we
		 * need 64 bit addresses even on 32 bit platforms.
		 */
		uint64_t paddr = vtophys(sc->csb);

		bus_write_4(sc->iomem, PTNET_IO_CSBBAH,
			    (paddr >> 32) & 0xffffffff);
		bus_write_4(sc->iomem, PTNET_IO_CSBBAL, paddr & 0xffffffff);
	}

	num_tx_rings = bus_read_4(sc->iomem, PTNET_IO_NUM_TX_RINGS);
	num_rx_rings = bus_read_4(sc->iomem, PTNET_IO_NUM_RX_RINGS);
	sc->num_rings = num_tx_rings + num_rx_rings;
	sc->num_tx_rings = num_tx_rings;

	/* Allocate and initialize per-queue data structures. */
	sc->queues = mallocarray(sc->num_rings, sizeof(struct ptnet_queue),
			    M_DEVBUF, M_NOWAIT | M_ZERO);
	if (sc->queues == NULL) {
		err = ENOMEM;
		goto err_path;
	}
	sc->rxqueues = sc->queues + num_tx_rings;

	for (i = 0; i < sc->num_rings; i++) {
		struct ptnet_queue *pq = sc->queues + i;

		pq->sc = sc;
		pq->kring_id = i;
		pq->kick = PTNET_IO_KICK_BASE + 4 * i;
		pq->ptring = sc->csb->rings + i;
		snprintf(pq->lock_name, sizeof(pq->lock_name), "%s-%d",
			 device_get_nameunit(dev), i);
		mtx_init(&pq->lock, pq->lock_name, NULL, MTX_DEF);
		if (i >= num_tx_rings) {
			/* RX queue: fix kring_id. */
			pq->kring_id -= num_tx_rings;
		} else {
			/* TX queue: allocate buf_ring. */
			pq->bufring = buf_ring_alloc(PTNET_BUF_RING_SIZE,
						M_DEVBUF, M_NOWAIT, &pq->lock);
			if (pq->bufring == NULL) {
				err = ENOMEM;
				goto err_path;
			}
		}
	}

	sc->min_tx_space = 64; /* Safe initial value. */

	err = ptnet_irqs_init(sc);
	if (err) {
		goto err_path;
	}

	/* Setup Ethernet interface. */
	sc->ifp = ifp = if_alloc(IFT_ETHER);
	if (ifp == NULL) {
		device_printf(dev, "Failed to allocate ifnet\n");
		err = ENOMEM;
		goto err_path;
	}

	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
	ifp->if_baudrate = IF_Gbps(10);
	ifp->if_softc = sc;
	ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX;
	ifp->if_init = ptnet_init;
	ifp->if_ioctl = ptnet_ioctl;
#if __FreeBSD_version >= 1100000
	ifp->if_get_counter = ptnet_get_counter;
#endif
	ifp->if_transmit = ptnet_transmit;
	ifp->if_qflush = ptnet_qflush;

	ifmedia_init(&sc->media, IFM_IMASK, ptnet_media_change,
		     ptnet_media_status);
	ifmedia_add(&sc->media, IFM_ETHER | IFM_10G_T | IFM_FDX, 0, NULL);
	ifmedia_set(&sc->media, IFM_ETHER | IFM_10G_T | IFM_FDX);

	macreg = bus_read_4(sc->iomem, PTNET_IO_MAC_HI);
	sc->hwaddr[0] = (macreg >> 8) & 0xff;
	sc->hwaddr[1] = macreg & 0xff;
	macreg = bus_read_4(sc->iomem, PTNET_IO_MAC_LO);
	sc->hwaddr[2] = (macreg >> 24) & 0xff;
	sc->hwaddr[3] = (macreg >> 16) & 0xff;
	sc->hwaddr[4] = (macreg >> 8) & 0xff;
	sc->hwaddr[5] = macreg & 0xff;

	ether_ifattach(ifp, sc->hwaddr);

	ifp->if_hdrlen = sizeof(struct ether_vlan_header);
	ifp->if_capabilities |= IFCAP_JUMBO_MTU | IFCAP_VLAN_MTU;

	if (sc->ptfeatures & PTNETMAP_F_VNET_HDR) {
		/* Similarly to what the vtnet driver does, we can emulate
		 * VLAN offloadings by inserting and removing the 802.1Q
		 * header during transmit and receive. We are then able
		 * to do checksum offloading of VLAN frames. */
		ifp->if_capabilities |= IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6
					| IFCAP_VLAN_HWCSUM
					| IFCAP_TSO | IFCAP_LRO
					| IFCAP_VLAN_HWTSO
					| IFCAP_VLAN_HWTAGGING;
	}

	ifp->if_capenable = ifp->if_capabilities;
#ifdef DEVICE_POLLING
	/* Don't enable polling by default. */
	ifp->if_capabilities |= IFCAP_POLLING;
#endif
	snprintf(sc->lock_name, sizeof(sc->lock_name),
		 "%s", device_get_nameunit(dev));
	mtx_init(&sc->lock, sc->lock_name, "ptnet core lock", MTX_DEF);
	callout_init_mtx(&sc->tick, &sc->lock, 0);

	/* Prepare a netmap_adapter struct instance to do netmap_attach(). */
	nifp_offset = bus_read_4(sc->iomem, PTNET_IO_NIFP_OFS);
	memset(&na_arg, 0, sizeof(na_arg));
	na_arg.ifp = ifp;
	na_arg.num_tx_desc = bus_read_4(sc->iomem, PTNET_IO_NUM_TX_SLOTS);
	na_arg.num_rx_desc = bus_read_4(sc->iomem, PTNET_IO_NUM_RX_SLOTS);
	na_arg.num_tx_rings = num_tx_rings;
	na_arg.num_rx_rings = num_rx_rings;
	na_arg.nm_config = ptnet_nm_config;
	na_arg.nm_krings_create = ptnet_nm_krings_create;
	na_arg.nm_krings_delete = ptnet_nm_krings_delete;
	na_arg.nm_dtor = ptnet_nm_dtor;
	na_arg.nm_register = ptnet_nm_register;
	na_arg.nm_txsync = ptnet_nm_txsync;
	na_arg.nm_rxsync = ptnet_nm_rxsync;

	netmap_pt_guest_attach(&na_arg, sc->csb, nifp_offset,
                                bus_read_4(sc->iomem, PTNET_IO_HOSTMEMID));

	/* Now a netmap adapter for this ifp has been allocated, and it
	 * can be accessed through NA(ifp). We also have to initialize the CSB
	 * pointer. */
	sc->ptna = (struct netmap_pt_guest_adapter *)NA(ifp);

	/* If virtio-net header was negotiated, set the virt_hdr_len field in
	 * the netmap adapter, to inform users that this netmap adapter requires
	 * the application to deal with the headers. */
	ptnet_update_vnet_hdr(sc);

	device_printf(dev, "%s() completed\n", __func__);

	return (0);

err_path:
	ptnet_detach(dev);
	return err;
}

static int
ptnet_detach(device_t dev)
{
	struct ptnet_softc *sc = device_get_softc(dev);
	int i;

#ifdef DEVICE_POLLING
	if (sc->ifp->if_capenable & IFCAP_POLLING) {
		ether_poll_deregister(sc->ifp);
	}
#endif
	callout_drain(&sc->tick);

	if (sc->queues) {
		/* Drain taskqueues before calling if_detach. */
		for (i = 0; i < sc->num_rings; i++) {
			struct ptnet_queue *pq = sc->queues + i;

			if (pq->taskq) {
				taskqueue_drain(pq->taskq, &pq->task);
			}
		}
	}

	if (sc->ifp) {
		ether_ifdetach(sc->ifp);

		/* Uninitialize netmap adapters for this device. */
		netmap_detach(sc->ifp);

		ifmedia_removeall(&sc->media);
		if_free(sc->ifp);
		sc->ifp = NULL;
	}

	ptnet_irqs_fini(sc);

	if (sc->csb) {
		bus_write_4(sc->iomem, PTNET_IO_CSBBAH, 0);
		bus_write_4(sc->iomem, PTNET_IO_CSBBAL, 0);
		free(sc->csb, M_DEVBUF);
		sc->csb = NULL;
	}

	if (sc->queues) {
		for (i = 0; i < sc->num_rings; i++) {
			struct ptnet_queue *pq = sc->queues + i;

			if (mtx_initialized(&pq->lock)) {
				mtx_destroy(&pq->lock);
			}
			if (pq->bufring != NULL) {
				buf_ring_free(pq->bufring, M_DEVBUF);
			}
		}
		free(sc->queues, M_DEVBUF);
		sc->queues = NULL;
	}

	if (sc->iomem) {
		bus_release_resource(dev, SYS_RES_IOPORT,
				     PCIR_BAR(PTNETMAP_IO_PCI_BAR), sc->iomem);
		sc->iomem = NULL;
	}

	mtx_destroy(&sc->lock);

	device_printf(dev, "%s() completed\n", __func__);

	return (0);
}

static int
ptnet_suspend(device_t dev)
{
	struct ptnet_softc *sc;

	sc = device_get_softc(dev);
	(void)sc;

	return (0);
}

static int
ptnet_resume(device_t dev)
{
	struct ptnet_softc *sc;

	sc = device_get_softc(dev);
	(void)sc;

	return (0);
}

static int
ptnet_shutdown(device_t dev)
{
	/*
	 * Suspend already does all of what we need to
	 * do here; we just never expect to be resumed.
	 */
	return (ptnet_suspend(dev));
}

static int
ptnet_irqs_init(struct ptnet_softc *sc)
{
	int rid = PCIR_BAR(PTNETMAP_MSIX_PCI_BAR);
	int nvecs = sc->num_rings;
	device_t dev = sc->dev;
	int err = ENOSPC;
	int cpu_cur;
	int i;

	if (pci_find_cap(dev, PCIY_MSIX, NULL) != 0)  {
		device_printf(dev, "Could not find MSI-X capability\n");
		return (ENXIO);
	}

	sc->msix_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
					      &rid, RF_ACTIVE);
	if (sc->msix_mem == NULL) {
		device_printf(dev, "Failed to allocate MSIX PCI BAR\n");
		return (ENXIO);
	}

	if (pci_msix_count(dev) < nvecs) {
		device_printf(dev, "Not enough MSI-X vectors\n");
		goto err_path;
	}

	err = pci_alloc_msix(dev, &nvecs);
	if (err) {
		device_printf(dev, "Failed to allocate MSI-X vectors\n");
		goto err_path;
	}

	for (i = 0; i < nvecs; i++) {
		struct ptnet_queue *pq = sc->queues + i;

		rid = i + 1;
		pq->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
						 RF_ACTIVE);
		if (pq->irq == NULL) {
			device_printf(dev, "Failed to allocate interrupt "
					   "for queue #%d\n", i);
			err = ENOSPC;
			goto err_path;
		}
	}

	cpu_cur = CPU_FIRST();
	for (i = 0; i < nvecs; i++) {
		struct ptnet_queue *pq = sc->queues + i;
		void (*handler)(void *) = ptnet_tx_intr;

		if (i >= sc->num_tx_rings) {
			handler = ptnet_rx_intr;
		}
		err = bus_setup_intr(dev, pq->irq, INTR_TYPE_NET | INTR_MPSAFE,
				     NULL /* intr_filter */, handler,
				     pq, &pq->cookie);
		if (err) {
			device_printf(dev, "Failed to register intr handler "
					   "for queue #%d\n", i);
			goto err_path;
		}

		bus_describe_intr(dev, pq->irq, pq->cookie, "q%d", i);
#if 0
		bus_bind_intr(sc->dev, pq->irq, cpu_cur);
#endif
		cpu_cur = CPU_NEXT(cpu_cur);
	}

	device_printf(dev, "Allocated %d MSI-X vectors\n", nvecs);

	cpu_cur = CPU_FIRST();
	for (i = 0; i < nvecs; i++) {
		struct ptnet_queue *pq = sc->queues + i;
		static void (*handler)(void *context, int pending);

		handler = (i < sc->num_tx_rings) ? ptnet_tx_task : ptnet_rx_task;

		TASK_INIT(&pq->task, 0, handler, pq);
		pq->taskq = taskqueue_create_fast("ptnet_queue", M_NOWAIT,
					taskqueue_thread_enqueue, &pq->taskq);
		taskqueue_start_threads(&pq->taskq, 1, PI_NET, "%s-pq-%d",
					device_get_nameunit(sc->dev), cpu_cur);
		cpu_cur = CPU_NEXT(cpu_cur);
	}

	return 0;
err_path:
	ptnet_irqs_fini(sc);
	return err;
}

static void
ptnet_irqs_fini(struct ptnet_softc *sc)
{
	device_t dev = sc->dev;
	int i;

	for (i = 0; i < sc->num_rings; i++) {
		struct ptnet_queue *pq = sc->queues + i;

		if (pq->taskq) {
			taskqueue_free(pq->taskq);
			pq->taskq = NULL;
		}

		if (pq->cookie) {
			bus_teardown_intr(dev, pq->irq, pq->cookie);
			pq->cookie = NULL;
		}

		if (pq->irq) {
			bus_release_resource(dev, SYS_RES_IRQ, i + 1, pq->irq);
			pq->irq = NULL;
		}
	}

	if (sc->msix_mem) {
		pci_release_msi(dev);

		bus_release_resource(dev, SYS_RES_MEMORY,
				     PCIR_BAR(PTNETMAP_MSIX_PCI_BAR),
				     sc->msix_mem);
		sc->msix_mem = NULL;
	}
}

static void
ptnet_init(void *opaque)
{
	struct ptnet_softc *sc = opaque;

	PTNET_CORE_LOCK(sc);
	ptnet_init_locked(sc);
	PTNET_CORE_UNLOCK(sc);
}

static int
ptnet_ioctl(if_t ifp, u_long cmd, caddr_t data)
{
	struct ptnet_softc *sc = if_getsoftc(ifp);
	device_t dev = sc->dev;
	struct ifreq *ifr = (struct ifreq *)data;
	int mask, err = 0;

	switch (cmd) {
	case SIOCSIFFLAGS:
		device_printf(dev, "SIOCSIFFLAGS %x\n", ifp->if_flags);
		PTNET_CORE_LOCK(sc);
		if (ifp->if_flags & IFF_UP) {
			/* Network stack wants the iff to be up. */
			err = ptnet_init_locked(sc);
		} else {
			/* Network stack wants the iff to be down. */
			err = ptnet_stop(sc);
		}
		/* We don't need to do nothing to support IFF_PROMISC,
		 * since that is managed by the backend port. */
		PTNET_CORE_UNLOCK(sc);
		break;

	case SIOCSIFCAP:
		device_printf(dev, "SIOCSIFCAP %x %x\n",
			      ifr->ifr_reqcap, ifp->if_capenable);
		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
#ifdef DEVICE_POLLING
		if (mask & IFCAP_POLLING) {
			struct ptnet_queue *pq;
			int i;

			if (ifr->ifr_reqcap & IFCAP_POLLING) {
				err = ether_poll_register(ptnet_poll, ifp);
				if (err) {
					break;
				}
				/* Stop queues and sync with taskqueues. */
				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
				for (i = 0; i < sc->num_rings; i++) {
					pq = sc-> queues + i;
					/* Make sure the worker sees the
					 * IFF_DRV_RUNNING down. */
					PTNET_Q_LOCK(pq);
					pq->ptring->guest_need_kick = 0;
					PTNET_Q_UNLOCK(pq);
					/* Wait for rescheduling to finish. */
					if (pq->taskq) {
						taskqueue_drain(pq->taskq,
								&pq->task);
					}
				}
				ifp->if_drv_flags |= IFF_DRV_RUNNING;
			} else {
				err = ether_poll_deregister(ifp);
				for (i = 0; i < sc->num_rings; i++) {
					pq = sc-> queues + i;
					PTNET_Q_LOCK(pq);
					pq->ptring->guest_need_kick = 1;
					PTNET_Q_UNLOCK(pq);
				}
			}
		}
#endif  /* DEVICE_POLLING */
		ifp->if_capenable = ifr->ifr_reqcap;
		break;

	case SIOCSIFMTU:
		/* We support any reasonable MTU. */
		if (ifr->ifr_mtu < ETHERMIN ||
				ifr->ifr_mtu > PTNET_MAX_PKT_SIZE) {
			err = EINVAL;
		} else {
			PTNET_CORE_LOCK(sc);
			ifp->if_mtu = ifr->ifr_mtu;
			PTNET_CORE_UNLOCK(sc);
		}
		break;

	case SIOCSIFMEDIA:
	case SIOCGIFMEDIA:
		err = ifmedia_ioctl(ifp, ifr, &sc->media, cmd);
		break;

	default:
		err = ether_ioctl(ifp, cmd, data);
		break;
	}

	return err;
}

static int
ptnet_init_locked(struct ptnet_softc *sc)
{
	if_t ifp = sc->ifp;
	struct netmap_adapter *na_dr = &sc->ptna->dr.up;
	struct netmap_adapter *na_nm = &sc->ptna->hwup.up;
	unsigned int nm_buf_size;
	int ret;

	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
		return 0; /* nothing to do */
	}

	device_printf(sc->dev, "%s\n", __func__);

	/* Translate offload capabilities according to if_capenable. */
	ifp->if_hwassist = 0;
	if (ifp->if_capenable & IFCAP_TXCSUM)
		ifp->if_hwassist |= PTNET_CSUM_OFFLOAD;
	if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
		ifp->if_hwassist |= PTNET_CSUM_OFFLOAD_IPV6;
	if (ifp->if_capenable & IFCAP_TSO4)
		ifp->if_hwassist |= CSUM_IP_TSO;
	if (ifp->if_capenable & IFCAP_TSO6)
		ifp->if_hwassist |= CSUM_IP6_TSO;

	/*
	 * Prepare the interface for netmap mode access.
	 */
	netmap_update_config(na_dr);

	ret = netmap_mem_finalize(na_dr->nm_mem, na_dr);
	if (ret) {
		device_printf(sc->dev, "netmap_mem_finalize() failed\n");
		return ret;
	}

	if (sc->ptna->backend_regifs == 0) {
		ret = ptnet_nm_krings_create(na_nm);
		if (ret) {
			device_printf(sc->dev, "ptnet_nm_krings_create() "
					       "failed\n");
			goto err_mem_finalize;
		}

		ret = netmap_mem_rings_create(na_dr);
		if (ret) {
			device_printf(sc->dev, "netmap_mem_rings_create() "
					       "failed\n");
			goto err_rings_create;
		}

		ret = netmap_mem_get_lut(na_dr->nm_mem, &na_dr->na_lut);
		if (ret) {
			device_printf(sc->dev, "netmap_mem_get_lut() "
					       "failed\n");
			goto err_get_lut;
		}
	}

	ret = ptnet_nm_register(na_dr, 1 /* on */);
	if (ret) {
		goto err_register;
	}

	nm_buf_size = NETMAP_BUF_SIZE(na_dr);

	KASSERT(nm_buf_size > 0, ("Invalid netmap buffer size"));
	sc->min_tx_space = PTNET_MAX_PKT_SIZE / nm_buf_size + 2;
	device_printf(sc->dev, "%s: min_tx_space = %u\n", __func__,
		      sc->min_tx_space);
#ifdef PTNETMAP_STATS
	callout_reset(&sc->tick, hz, ptnet_tick, sc);
#endif

	ifp->if_drv_flags |= IFF_DRV_RUNNING;

	return 0;

err_register:
	memset(&na_dr->na_lut, 0, sizeof(na_dr->na_lut));
err_get_lut:
	netmap_mem_rings_delete(na_dr);
err_rings_create:
	ptnet_nm_krings_delete(na_nm);
err_mem_finalize:
	netmap_mem_deref(na_dr->nm_mem, na_dr);

	return ret;
}

/* To be called under core lock. */
static int
ptnet_stop(struct ptnet_softc *sc)
{
	if_t ifp = sc->ifp;
	struct netmap_adapter *na_dr = &sc->ptna->dr.up;
	struct netmap_adapter *na_nm = &sc->ptna->hwup.up;
	int i;

	device_printf(sc->dev, "%s\n", __func__);

	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
		return 0; /* nothing to do */
	}

	/* Clear the driver-ready flag, and synchronize with all the queues,
	 * so that after this loop we are sure nobody is working anymore with
	 * the device. This scheme is taken from the vtnet driver. */
	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
	callout_stop(&sc->tick);
	for (i = 0; i < sc->num_rings; i++) {
		PTNET_Q_LOCK(sc->queues + i);
		PTNET_Q_UNLOCK(sc->queues + i);
	}

	ptnet_nm_register(na_dr, 0 /* off */);

	if (sc->ptna->backend_regifs == 0) {
		netmap_mem_rings_delete(na_dr);
		ptnet_nm_krings_delete(na_nm);
	}
	netmap_mem_deref(na_dr->nm_mem, na_dr);

	return 0;
}

static void
ptnet_qflush(if_t ifp)
{
	struct ptnet_softc *sc = if_getsoftc(ifp);
	int i;

	/* Flush all the bufrings and do the interface flush. */
	for (i = 0; i < sc->num_rings; i++) {
		struct ptnet_queue *pq = sc->queues + i;
		struct mbuf *m;

		PTNET_Q_LOCK(pq);
		if (pq->bufring) {
			while ((m = buf_ring_dequeue_sc(pq->bufring))) {
				m_freem(m);
			}
		}
		PTNET_Q_UNLOCK(pq);
	}

	if_qflush(ifp);
}

static int
ptnet_media_change(if_t ifp)
{
	struct ptnet_softc *sc = if_getsoftc(ifp);
	struct ifmedia *ifm = &sc->media;

	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER) {
		return EINVAL;
	}

	return 0;
}

#if __FreeBSD_version >= 1100000
static uint64_t
ptnet_get_counter(if_t ifp, ift_counter cnt)
{
	struct ptnet_softc *sc = if_getsoftc(ifp);
	struct ptnet_queue_stats stats[2];
	int i;

	/* Accumulate statistics over the queues. */
	memset(stats, 0, sizeof(stats));
	for (i = 0; i < sc->num_rings; i++) {
		struct ptnet_queue *pq = sc->queues + i;
		int idx = (i < sc->num_tx_rings) ? 0 : 1;

		stats[idx].packets	+= pq->stats.packets;
		stats[idx].bytes	+= pq->stats.bytes;
		stats[idx].errors	+= pq->stats.errors;
		stats[idx].iqdrops	+= pq->stats.iqdrops;
		stats[idx].mcasts	+= pq->stats.mcasts;
	}

	switch (cnt) {
	case IFCOUNTER_IPACKETS:
		return (stats[1].packets);
	case IFCOUNTER_IQDROPS:
		return (stats[1].iqdrops);
	case IFCOUNTER_IERRORS:
		return (stats[1].errors);
	case IFCOUNTER_OPACKETS:
		return (stats[0].packets);
	case IFCOUNTER_OBYTES:
		return (stats[0].bytes);
	case IFCOUNTER_OMCASTS:
		return (stats[0].mcasts);
	default:
		return (if_get_counter_default(ifp, cnt));
	}
}
#endif


#ifdef PTNETMAP_STATS
/* Called under core lock. */
static void
ptnet_tick(void *opaque)
{
	struct ptnet_softc *sc = opaque;
	int i;

	for (i = 0; i < sc->num_rings; i++) {
		struct ptnet_queue *pq = sc->queues + i;
		struct ptnet_queue_stats cur = pq->stats;
		struct timeval now;
		unsigned int delta;

		microtime(&now);
		delta = now.tv_usec - sc->last_ts.tv_usec +
			(now.tv_sec - sc->last_ts.tv_sec) * 1000000;
		delta /= 1000; /* in milliseconds */

		if (delta == 0)
			continue;

		device_printf(sc->dev, "#%d[%u ms]:pkts %lu, kicks %lu, "
			      "intr %lu\n", i, delta,
			      (cur.packets - pq->last_stats.packets),
			      (cur.kicks - pq->last_stats.kicks),
			      (cur.intrs - pq->last_stats.intrs));
		pq->last_stats = cur;
	}
	microtime(&sc->last_ts);
	callout_schedule(&sc->tick, hz);
}
#endif /* PTNETMAP_STATS */

static void
ptnet_media_status(if_t ifp, struct ifmediareq *ifmr)
{
	/* We are always active, as the backend netmap port is
	 * always open in netmap mode. */
	ifmr->ifm_status = IFM_AVALID | IFM_ACTIVE;
	ifmr->ifm_active = IFM_ETHER | IFM_10G_T | IFM_FDX;
}

static uint32_t
ptnet_nm_ptctl(if_t ifp, uint32_t cmd)
{
	struct ptnet_softc *sc = if_getsoftc(ifp);
	/*
	 * Write a command and read back error status,
	 * with zero meaning success.
	 */
	bus_write_4(sc->iomem, PTNET_IO_PTCTL, cmd);
	return bus_read_4(sc->iomem, PTNET_IO_PTCTL);
}

static int
ptnet_nm_config(struct netmap_adapter *na, unsigned *txr, unsigned *txd,
		unsigned *rxr, unsigned *rxd)
{
	struct ptnet_softc *sc = if_getsoftc(na->ifp);

	*txr = bus_read_4(sc->iomem, PTNET_IO_NUM_TX_RINGS);
	*rxr = bus_read_4(sc->iomem, PTNET_IO_NUM_RX_RINGS);
	*txd = bus_read_4(sc->iomem, PTNET_IO_NUM_TX_SLOTS);
	*rxd = bus_read_4(sc->iomem, PTNET_IO_NUM_RX_SLOTS);

	device_printf(sc->dev, "txr %u, rxr %u, txd %u, rxd %u\n",
		      *txr, *rxr, *txd, *rxd);

	return 0;
}

static void
ptnet_sync_from_csb(struct ptnet_softc *sc, struct netmap_adapter *na)
{
	int i;

	/* Sync krings from the host, reading from
	 * CSB. */
	for (i = 0; i < sc->num_rings; i++) {
		struct ptnet_ring *ptring = sc->queues[i].ptring;
		struct netmap_kring *kring;

		if (i < na->num_tx_rings) {
			kring = na->tx_rings + i;
		} else {
			kring = na->rx_rings + i - na->num_tx_rings;
		}
		kring->rhead = kring->ring->head = ptring->head;
		kring->rcur = kring->ring->cur = ptring->cur;
		kring->nr_hwcur = ptring->hwcur;
		kring->nr_hwtail = kring->rtail =
			kring->ring->tail = ptring->hwtail;

		ND("%d,%d: csb {hc %u h %u c %u ht %u}", t, i,
		   ptring->hwcur, ptring->head, ptring->cur,
		   ptring->hwtail);
		ND("%d,%d: kring {hc %u rh %u rc %u h %u c %u ht %u rt %u t %u}",
		   t, i, kring->nr_hwcur, kring->rhead, kring->rcur,
		   kring->ring->head, kring->ring->cur, kring->nr_hwtail,
		   kring->rtail, kring->ring->tail);
	}
}

static void
ptnet_update_vnet_hdr(struct ptnet_softc *sc)
{
	unsigned int wanted_hdr_len = ptnet_vnet_hdr ? PTNET_HDR_SIZE : 0;

	bus_write_4(sc->iomem, PTNET_IO_VNET_HDR_LEN, wanted_hdr_len);
	sc->vnet_hdr_len = bus_read_4(sc->iomem, PTNET_IO_VNET_HDR_LEN);
	sc->ptna->hwup.up.virt_hdr_len = sc->vnet_hdr_len;
}

static int
ptnet_nm_register(struct netmap_adapter *na, int onoff)
{
	/* device-specific */
	if_t ifp = na->ifp;
	struct ptnet_softc *sc = if_getsoftc(ifp);
	int native = (na == &sc->ptna->hwup.up);
	struct ptnet_queue *pq;
	enum txrx t;
	int ret = 0;
	int i;

	if (!onoff) {
		sc->ptna->backend_regifs--;
	}

	/* If this is the last netmap client, guest interrupt enable flags may
	 * be in arbitrary state. Since these flags are going to be used also
	 * by the netdevice driver, we have to make sure to start with
	 * notifications enabled. Also, schedule NAPI to flush pending packets
	 * in the RX rings, since we will not receive further interrupts
	 * until these will be processed. */
	if (native && !onoff && na->active_fds == 0) {
		D("Exit netmap mode, re-enable interrupts");
		for (i = 0; i < sc->num_rings; i++) {
			pq = sc->queues + i;
			pq->ptring->guest_need_kick = 1;
		}
	}

	if (onoff) {
		if (sc->ptna->backend_regifs == 0) {
			/* Initialize notification enable fields in the CSB. */
			for (i = 0; i < sc->num_rings; i++) {
				pq = sc->queues + i;
				pq->ptring->host_need_kick = 1;
				pq->ptring->guest_need_kick =
					(!(ifp->if_capenable & IFCAP_POLLING)
						&& i >= sc->num_tx_rings);
			}

			/* Set the virtio-net header length. */
			ptnet_update_vnet_hdr(sc);

			/* Make sure the host adapter passed through is ready
			 * for txsync/rxsync. */
			ret = ptnet_nm_ptctl(ifp, PTNETMAP_PTCTL_CREATE);
			if (ret) {
				return ret;
			}
		}

		/* Sync from CSB must be done after REGIF PTCTL. Skip this
		 * step only if this is a netmap client and it is not the
		 * first one. */
		if ((!native && sc->ptna->backend_regifs == 0) ||
				(native && na->active_fds == 0)) {
			ptnet_sync_from_csb(sc, na);
		}

		/* If not native, don't call nm_set_native_flags, since we don't want
		 * to replace if_transmit method, nor set NAF_NETMAP_ON */
		if (native) {
			for_rx_tx(t) {
				for (i = 0; i <= nma_get_nrings(na, t); i++) {
					struct netmap_kring *kring = &NMR(na, t)[i];

					if (nm_kring_pending_on(kring)) {
						kring->nr_mode = NKR_NETMAP_ON;
					}
				}
			}
			nm_set_native_flags(na);
		}

	} else {
		if (native) {
			nm_clear_native_flags(na);
			for_rx_tx(t) {
				for (i = 0; i <= nma_get_nrings(na, t); i++) {
					struct netmap_kring *kring = &NMR(na, t)[i];

					if (nm_kring_pending_off(kring)) {
						kring->nr_mode = NKR_NETMAP_OFF;
					}
				}
			}
		}

		/* Sync from CSB must be done before UNREGIF PTCTL, on the last
		 * netmap client. */
		if (native && na->active_fds == 0) {
			ptnet_sync_from_csb(sc, na);
		}

		if (sc->ptna->backend_regifs == 0) {
			ret = ptnet_nm_ptctl(ifp, PTNETMAP_PTCTL_DELETE);
		}
	}

	if (onoff) {
		sc->ptna->backend_regifs++;
	}

	return ret;
}

static int
ptnet_nm_txsync(struct netmap_kring *kring, int flags)
{
	struct ptnet_softc *sc = if_getsoftc(kring->na->ifp);
	struct ptnet_queue *pq = sc->queues + kring->ring_id;
	bool notify;

	notify = netmap_pt_guest_txsync(pq->ptring, kring, flags);
	if (notify) {
		ptnet_kick(pq);
	}

	return 0;
}

static int
ptnet_nm_rxsync(struct netmap_kring *kring, int flags)
{
	struct ptnet_softc *sc = if_getsoftc(kring->na->ifp);
	struct ptnet_queue *pq = sc->rxqueues + kring->ring_id;
	bool notify;

	notify = netmap_pt_guest_rxsync(pq->ptring, kring, flags);
	if (notify) {
		ptnet_kick(pq);
	}

	return 0;
}

static void
ptnet_tx_intr(void *opaque)
{
	struct ptnet_queue *pq = opaque;
	struct ptnet_softc *sc = pq->sc;

	DBG(device_printf(sc->dev, "Tx interrupt #%d\n", pq->kring_id));
#ifdef PTNETMAP_STATS
	pq->stats.intrs ++;
#endif /* PTNETMAP_STATS */

	if (netmap_tx_irq(sc->ifp, pq->kring_id) != NM_IRQ_PASS) {
		return;
	}

	/* Schedule the tasqueue to flush process transmissions requests.
	 * However, vtnet, if_em and if_igb just call ptnet_transmit() here,
	 * at least when using MSI-X interrupts. The if_em driver, instead
	 * schedule taskqueue when using legacy interrupts. */
	taskqueue_enqueue(pq->taskq, &pq->task);
}

static void
ptnet_rx_intr(void *opaque)
{
	struct ptnet_queue *pq = opaque;
	struct ptnet_softc *sc = pq->sc;
	unsigned int unused;

	DBG(device_printf(sc->dev, "Rx interrupt #%d\n", pq->kring_id));
#ifdef PTNETMAP_STATS
	pq->stats.intrs ++;
#endif /* PTNETMAP_STATS */

	if (netmap_rx_irq(sc->ifp, pq->kring_id, &unused) != NM_IRQ_PASS) {
		return;
	}

	/* Like vtnet, if_igb and if_em drivers when using MSI-X interrupts,
	 * receive-side processing is executed directly in the interrupt
	 * service routine. Alternatively, we may schedule the taskqueue. */
	ptnet_rx_eof(pq, PTNET_RX_BUDGET, true);
}

/* The following offloadings-related functions are taken from the vtnet
 * driver, but the same functionality is required for the ptnet driver.
 * As a temporary solution, I copied this code from vtnet and I started
 * to generalize it (taking away driver-specific statistic accounting),
 * making as little modifications as possible.
 * In the future we need to share these functions between vtnet and ptnet.
 */
static int
ptnet_tx_offload_ctx(struct mbuf *m, int *etype, int *proto, int *start)
{
	struct ether_vlan_header *evh;
	int offset;

	evh = mtod(m, struct ether_vlan_header *);
	if (evh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
		/* BMV: We should handle nested VLAN tags too. */
		*etype = ntohs(evh->evl_proto);
		offset = sizeof(struct ether_vlan_header);
	} else {
		*etype = ntohs(evh->evl_encap_proto);
		offset = sizeof(struct ether_header);
	}

	switch (*etype) {
#if defined(INET)
	case ETHERTYPE_IP: {
		struct ip *ip, iphdr;
		if (__predict_false(m->m_len < offset + sizeof(struct ip))) {
			m_copydata(m, offset, sizeof(struct ip),
			    (caddr_t) &iphdr);
			ip = &iphdr;
		} else
			ip = (struct ip *)(m->m_data + offset);
		*proto = ip->ip_p;
		*start = offset + (ip->ip_hl << 2);
		break;
	}
#endif
#if defined(INET6)
	case ETHERTYPE_IPV6:
		*proto = -1;
		*start = ip6_lasthdr(m, offset, IPPROTO_IPV6, proto);
		/* Assert the network stack sent us a valid packet. */
		KASSERT(*start > offset,
		    ("%s: mbuf %p start %d offset %d proto %d", __func__, m,
		    *start, offset, *proto));
		break;
#endif
	default:
		/* Here we should increment the tx_csum_bad_ethtype counter. */
		return (EINVAL);
	}

	return (0);
}

static int
ptnet_tx_offload_tso(if_t ifp, struct mbuf *m, int eth_type,
		     int offset, bool allow_ecn, struct virtio_net_hdr *hdr)
{
	static struct timeval lastecn;
	static int curecn;
	struct tcphdr *tcp, tcphdr;

	if (__predict_false(m->m_len < offset + sizeof(struct tcphdr))) {
		m_copydata(m, offset, sizeof(struct tcphdr), (caddr_t) &tcphdr);
		tcp = &tcphdr;
	} else
		tcp = (struct tcphdr *)(m->m_data + offset);

	hdr->hdr_len = offset + (tcp->th_off << 2);
	hdr->gso_size = m->m_pkthdr.tso_segsz;
	hdr->gso_type = eth_type == ETHERTYPE_IP ? VIRTIO_NET_HDR_GSO_TCPV4 :
	    VIRTIO_NET_HDR_GSO_TCPV6;

	if (tcp->th_flags & TH_CWR) {
		/*
		 * Drop if VIRTIO_NET_F_HOST_ECN was not negotiated. In FreeBSD,
		 * ECN support is not on a per-interface basis, but globally via
		 * the net.inet.tcp.ecn.enable sysctl knob. The default is off.
		 */
		if (!allow_ecn) {
			if (ppsratecheck(&lastecn, &curecn, 1))
				if_printf(ifp,
				    "TSO with ECN not negotiated with host\n");
			return (ENOTSUP);
		}
		hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
	}

	/* Here we should increment tx_tso counter. */

	return (0);
}

static struct mbuf *
ptnet_tx_offload(if_t ifp, struct mbuf *m, bool allow_ecn,
		 struct virtio_net_hdr *hdr)
{
	int flags, etype, csum_start, proto, error;

	flags = m->m_pkthdr.csum_flags;

	error = ptnet_tx_offload_ctx(m, &etype, &proto, &csum_start);
	if (error)
		goto drop;

	if ((etype == ETHERTYPE_IP && flags & PTNET_CSUM_OFFLOAD) ||
	    (etype == ETHERTYPE_IPV6 && flags & PTNET_CSUM_OFFLOAD_IPV6)) {
		/*
		 * We could compare the IP protocol vs the CSUM_ flag too,
		 * but that really should not be necessary.
		 */
		hdr->flags |= VIRTIO_NET_HDR_F_NEEDS_CSUM;
		hdr->csum_start = csum_start;
		hdr->csum_offset = m->m_pkthdr.csum_data;
		/* Here we should increment the tx_csum counter. */
	}

	if (flags & CSUM_TSO) {
		if (__predict_false(proto != IPPROTO_TCP)) {
			/* Likely failed to correctly parse the mbuf.
			 * Here we should increment the tx_tso_not_tcp
			 * counter. */
			goto drop;
		}

		KASSERT(hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM,
		    ("%s: mbuf %p TSO without checksum offload %#x",
		    __func__, m, flags));

		error = ptnet_tx_offload_tso(ifp, m, etype, csum_start,
					     allow_ecn, hdr);
		if (error)
			goto drop;
	}

	return (m);

drop:
	m_freem(m);
	return (NULL);
}

static void
ptnet_vlan_tag_remove(struct mbuf *m)
{
	struct ether_vlan_header *evh;

	evh = mtod(m, struct ether_vlan_header *);
	m->m_pkthdr.ether_vtag = ntohs(evh->evl_tag);
	m->m_flags |= M_VLANTAG;

	/* Strip the 802.1Q header. */
	bcopy((char *) evh, (char *) evh + ETHER_VLAN_ENCAP_LEN,
	    ETHER_HDR_LEN - ETHER_TYPE_LEN);
	m_adj(m, ETHER_VLAN_ENCAP_LEN);
}

/*
 * Use the checksum offset in the VirtIO header to set the
 * correct CSUM_* flags.
 */
static int
ptnet_rx_csum_by_offset(struct mbuf *m, uint16_t eth_type, int ip_start,
			struct virtio_net_hdr *hdr)
{
#if defined(INET) || defined(INET6)
	int offset = hdr->csum_start + hdr->csum_offset;
#endif

	/* Only do a basic sanity check on the offset. */
	switch (eth_type) {
#if defined(INET)
	case ETHERTYPE_IP:
		if (__predict_false(offset < ip_start + sizeof(struct ip)))
			return (1);
		break;
#endif
#if defined(INET6)
	case ETHERTYPE_IPV6:
		if (__predict_false(offset < ip_start + sizeof(struct ip6_hdr)))
			return (1);
		break;
#endif
	default:
		/* Here we should increment the rx_csum_bad_ethtype counter. */
		return (1);
	}

	/*
	 * Use the offset to determine the appropriate CSUM_* flags. This is
	 * a bit dirty, but we can get by with it since the checksum offsets
	 * happen to be different. We assume the host host does not do IPv4
	 * header checksum offloading.
	 */
	switch (hdr->csum_offset) {
	case offsetof(struct udphdr, uh_sum):
	case offsetof(struct tcphdr, th_sum):
		m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
		m->m_pkthdr.csum_data = 0xFFFF;
		break;
	case offsetof(struct sctphdr, checksum):
		m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
		break;
	default:
		/* Here we should increment the rx_csum_bad_offset counter. */
		return (1);
	}

	return (0);
}

static int
ptnet_rx_csum_by_parse(struct mbuf *m, uint16_t eth_type, int ip_start,
		       struct virtio_net_hdr *hdr)
{
	int offset, proto;

	switch (eth_type) {
#if defined(INET)
	case ETHERTYPE_IP: {
		struct ip *ip;
		if (__predict_false(m->m_len < ip_start + sizeof(struct ip)))
			return (1);
		ip = (struct ip *)(m->m_data + ip_start);
		proto = ip->ip_p;
		offset = ip_start + (ip->ip_hl << 2);
		break;
	}
#endif
#if defined(INET6)
	case ETHERTYPE_IPV6:
		if (__predict_false(m->m_len < ip_start +
		    sizeof(struct ip6_hdr)))
			return (1);
		offset = ip6_lasthdr(m, ip_start, IPPROTO_IPV6, &proto);
		if (__predict_false(offset < 0))
			return (1);
		break;
#endif
	default:
		/* Here we should increment the rx_csum_bad_ethtype counter. */
		return (1);
	}

	switch (proto) {
	case IPPROTO_TCP:
		if (__predict_false(m->m_len < offset + sizeof(struct tcphdr)))
			return (1);
		m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
		m->m_pkthdr.csum_data = 0xFFFF;
		break;
	case IPPROTO_UDP:
		if (__predict_false(m->m_len < offset + sizeof(struct udphdr)))
			return (1);
		m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
		m->m_pkthdr.csum_data = 0xFFFF;
		break;
	case IPPROTO_SCTP:
		if (__predict_false(m->m_len < offset + sizeof(struct sctphdr)))
			return (1);
		m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
		break;
	default:
		/*
		 * For the remaining protocols, FreeBSD does not support
		 * checksum offloading, so the checksum will be recomputed.
		 */
#if 0
		if_printf(ifp, "cksum offload of unsupported "
		    "protocol eth_type=%#x proto=%d csum_start=%d "
		    "csum_offset=%d\n", __func__, eth_type, proto,
		    hdr->csum_start, hdr->csum_offset);
#endif
		break;
	}

	return (0);
}

/*
 * Set the appropriate CSUM_* flags. Unfortunately, the information
 * provided is not directly useful to us. The VirtIO header gives the
 * offset of the checksum, which is all Linux needs, but this is not
 * how FreeBSD does things. We are forced to peek inside the packet
 * a bit.
 *
 * It would be nice if VirtIO gave us the L4 protocol or if FreeBSD
 * could accept the offsets and let the stack figure it out.
 */
static int
ptnet_rx_csum(struct mbuf *m, struct virtio_net_hdr *hdr)
{
	struct ether_header *eh;
	struct ether_vlan_header *evh;
	uint16_t eth_type;
	int offset, error;

	eh = mtod(m, struct ether_header *);
	eth_type = ntohs(eh->ether_type);
	if (eth_type == ETHERTYPE_VLAN) {
		/* BMV: We should handle nested VLAN tags too. */
		evh = mtod(m, struct ether_vlan_header *);
		eth_type = ntohs(evh->evl_proto);
		offset = sizeof(struct ether_vlan_header);
	} else
		offset = sizeof(struct ether_header);

	if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
		error = ptnet_rx_csum_by_offset(m, eth_type, offset, hdr);
	else
		error = ptnet_rx_csum_by_parse(m, eth_type, offset, hdr);

	return (error);
}
/* End of offloading-related functions to be shared with vtnet. */

static inline void
ptnet_sync_tail(struct ptnet_ring *ptring, struct netmap_kring *kring)
{
	struct netmap_ring *ring = kring->ring;

	/* Update hwcur and hwtail as known by the host. */
        ptnetmap_guest_read_kring_csb(ptring, kring);

	/* nm_sync_finalize */
	ring->tail = kring->rtail = kring->nr_hwtail;
}

static void
ptnet_ring_update(struct ptnet_queue *pq, struct netmap_kring *kring,
		  unsigned int head, unsigned int sync_flags)
{
	struct netmap_ring *ring = kring->ring;
	struct ptnet_ring *ptring = pq->ptring;

	/* Some packets have been pushed to the netmap ring. We have
	 * to tell the host to process the new packets, updating cur
	 * and head in the CSB. */
	ring->head = ring->cur = head;

	/* Mimic nm_txsync_prologue/nm_rxsync_prologue. */
	kring->rcur = kring->rhead = head;

	ptnetmap_guest_write_kring_csb(ptring, kring->rcur, kring->rhead);

	/* Kick the host if needed. */
	if (NM_ACCESS_ONCE(ptring->host_need_kick)) {
		ptring->sync_flags = sync_flags;
		ptnet_kick(pq);
	}
}

#define PTNET_TX_NOSPACE(_h, _k, _min)	\
	((((_h) < (_k)->rtail) ? 0 : (_k)->nkr_num_slots) + \
		(_k)->rtail - (_h)) < (_min)

/* This function may be called by the network stack, or by
 * by the taskqueue thread. */
static int
ptnet_drain_transmit_queue(struct ptnet_queue *pq, unsigned int budget,
			   bool may_resched)
{
	struct ptnet_softc *sc = pq->sc;
	bool have_vnet_hdr = sc->vnet_hdr_len;
	struct netmap_adapter *na = &sc->ptna->dr.up;
	if_t ifp = sc->ifp;
	unsigned int batch_count = 0;
	struct ptnet_ring *ptring;
	struct netmap_kring *kring;
	struct netmap_ring *ring;
	struct netmap_slot *slot;
	unsigned int count = 0;
	unsigned int minspace;
	unsigned int head;
	unsigned int lim;
	struct mbuf *mhead;
	struct mbuf *mf;
	int nmbuf_bytes;
	uint8_t *nmbuf;

	if (!PTNET_Q_TRYLOCK(pq)) {
		/* We failed to acquire the lock, schedule the taskqueue. */
		RD(1, "Deferring TX work");
		if (may_resched) {
			taskqueue_enqueue(pq->taskq, &pq->task);
		}

		return 0;
	}

	if (unlikely(!(ifp->if_drv_flags & IFF_DRV_RUNNING))) {
		PTNET_Q_UNLOCK(pq);
		RD(1, "Interface is down");
		return ENETDOWN;
	}

	ptring = pq->ptring;
	kring = na->tx_rings + pq->kring_id;
	ring = kring->ring;
	lim = kring->nkr_num_slots - 1;
	head = ring->head;
	minspace = sc->min_tx_space;

	while (count < budget) {
		if (PTNET_TX_NOSPACE(head, kring, minspace)) {
			/* We ran out of slot, let's see if the host has
			 * freed up some, by reading hwcur and hwtail from
			 * the CSB. */
			ptnet_sync_tail(ptring, kring);

			if (PTNET_TX_NOSPACE(head, kring, minspace)) {
				/* Still no slots available. Reactivate the
				 * interrupts so that we can be notified
				 * when some free slots are made available by
				 * the host. */
				ptring->guest_need_kick = 1;

				/* Double-check. */
				ptnet_sync_tail(ptring, kring);
				if (likely(PTNET_TX_NOSPACE(head, kring,
							    minspace))) {
					break;
				}

				RD(1, "Found more slots by doublecheck");
				/* More slots were freed before reactivating
				 * the interrupts. */
				ptring->guest_need_kick = 0;
			}
		}

		mhead = drbr_peek(ifp, pq->bufring);
		if (!mhead) {
			break;
		}

		/* Initialize transmission state variables. */
		slot = ring->slot + head;
		nmbuf = NMB(na, slot);
		nmbuf_bytes = 0;

		/* If needed, prepare the virtio-net header at the beginning
		 * of the first slot. */
		if (have_vnet_hdr) {
			struct virtio_net_hdr *vh =
					(struct virtio_net_hdr *)nmbuf;

			/* For performance, we could replace this memset() with
			 * two 8-bytes-wide writes. */
			memset(nmbuf, 0, PTNET_HDR_SIZE);
			if (mhead->m_pkthdr.csum_flags & PTNET_ALL_OFFLOAD) {
				mhead = ptnet_tx_offload(ifp, mhead, false,
							 vh);
				if (unlikely(!mhead)) {
					/* Packet dropped because errors
					 * occurred while preparing the vnet
					 * header. Let's go ahead with the next
					 * packet. */
					pq->stats.errors ++;
					drbr_advance(ifp, pq->bufring);
					continue;
				}
			}
			ND(1, "%s: [csum_flags %lX] vnet hdr: flags %x "
			      "csum_start %u csum_ofs %u hdr_len = %u "
			      "gso_size %u gso_type %x", __func__,
			      mhead->m_pkthdr.csum_flags, vh->flags,
			      vh->csum_start, vh->csum_offset, vh->hdr_len,
			      vh->gso_size, vh->gso_type);

			nmbuf += PTNET_HDR_SIZE;
			nmbuf_bytes += PTNET_HDR_SIZE;
		}

		for (mf = mhead; mf; mf = mf->m_next) {
			uint8_t *mdata = mf->m_data;
			int mlen = mf->m_len;

			for (;;) {
				int copy = NETMAP_BUF_SIZE(na) - nmbuf_bytes;

				if (mlen < copy) {
					copy = mlen;
				}
				memcpy(nmbuf, mdata, copy);

				mdata += copy;
				mlen -= copy;
				nmbuf += copy;
				nmbuf_bytes += copy;

				if (!mlen) {
					break;
				}

				slot->len = nmbuf_bytes;
				slot->flags = NS_MOREFRAG;

				head = nm_next(head, lim);
				KASSERT(head != ring->tail,
					("Unexpectedly run out of TX space"));
				slot = ring->slot + head;
				nmbuf = NMB(na, slot);
				nmbuf_bytes = 0;
			}
		}

		/* Complete last slot and update head. */
		slot->len = nmbuf_bytes;
		slot->flags = 0;
		head = nm_next(head, lim);

		/* Consume the packet just processed. */
		drbr_advance(ifp, pq->bufring);

		/* Copy the packet to listeners. */
		ETHER_BPF_MTAP(ifp, mhead);

		pq->stats.packets ++;
		pq->stats.bytes += mhead->m_pkthdr.len;
		if (mhead->m_flags & M_MCAST) {
			pq->stats.mcasts ++;
		}

		m_freem(mhead);

		count ++;
		if (++batch_count == PTNET_TX_BATCH) {
			ptnet_ring_update(pq, kring, head, NAF_FORCE_RECLAIM);
			batch_count = 0;
		}
	}

	if (batch_count) {
		ptnet_ring_update(pq, kring, head, NAF_FORCE_RECLAIM);
	}

	if (count >= budget && may_resched) {
		DBG(RD(1, "out of budget: resched, %d mbufs pending\n",
					drbr_inuse(ifp, pq->bufring)));
		taskqueue_enqueue(pq->taskq, &pq->task);
	}

	PTNET_Q_UNLOCK(pq);

	return count;
}

static int
ptnet_transmit(if_t ifp, struct mbuf *m)
{
	struct ptnet_softc *sc = if_getsoftc(ifp);
	struct ptnet_queue *pq;
	unsigned int queue_idx;
	int err;

	DBG(device_printf(sc->dev, "transmit %p\n", m));

	/* Insert 802.1Q header if needed. */
	if (m->m_flags & M_VLANTAG) {
		m = ether_vlanencap(m, m->m_pkthdr.ether_vtag);
		if (m == NULL) {
			return ENOBUFS;
		}
		m->m_flags &= ~M_VLANTAG;
	}

	/* Get the flow-id if available. */
	queue_idx = (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) ?
		    m->m_pkthdr.flowid : curcpu;

	if (unlikely(queue_idx >= sc->num_tx_rings)) {
		queue_idx %= sc->num_tx_rings;
	}

	pq = sc->queues + queue_idx;

	err = drbr_enqueue(ifp, pq->bufring, m);
	if (err) {
		/* ENOBUFS when the bufring is full */
		RD(1, "%s: drbr_enqueue() failed %d\n",
			__func__, err);
		pq->stats.errors ++;
		return err;
	}

	if (ifp->if_capenable & IFCAP_POLLING) {
		/* If polling is on, the transmit queues will be
		 * drained by the poller. */
		return 0;
	}

	err = ptnet_drain_transmit_queue(pq, PTNET_TX_BUDGET, true);

	return (err < 0) ? err : 0;
}

static unsigned int
ptnet_rx_discard(struct netmap_kring *kring, unsigned int head)
{
	struct netmap_ring *ring = kring->ring;
	struct netmap_slot *slot = ring->slot + head;

	for (;;) {
		head = nm_next(head, kring->nkr_num_slots - 1);
		if (!(slot->flags & NS_MOREFRAG) || head == ring->tail) {
			break;
		}
		slot = ring->slot + head;
	}

	return head;
}

static inline struct mbuf *
ptnet_rx_slot(struct mbuf *mtail, uint8_t *nmbuf, unsigned int nmbuf_len)
{
	uint8_t *mdata = mtod(mtail, uint8_t *) + mtail->m_len;

	do {
		unsigned int copy;

		if (mtail->m_len == MCLBYTES) {
			struct mbuf *mf;

			mf = m_getcl(M_NOWAIT, MT_DATA, 0);
			if (unlikely(!mf)) {
				return NULL;
			}

			mtail->m_next = mf;
			mtail = mf;
			mdata = mtod(mtail, uint8_t *);
			mtail->m_len = 0;
		}

		copy = MCLBYTES - mtail->m_len;
		if (nmbuf_len < copy) {
			copy = nmbuf_len;
		}

		memcpy(mdata, nmbuf, copy);

		nmbuf += copy;
		nmbuf_len -= copy;
		mdata += copy;
		mtail->m_len += copy;
	} while (nmbuf_len);

	return mtail;
}

static int
ptnet_rx_eof(struct ptnet_queue *pq, unsigned int budget, bool may_resched)
{
	struct ptnet_softc *sc = pq->sc;
	bool have_vnet_hdr = sc->vnet_hdr_len;
	struct ptnet_ring *ptring = pq->ptring;
	struct netmap_adapter *na = &sc->ptna->dr.up;
	struct netmap_kring *kring = na->rx_rings + pq->kring_id;
	struct netmap_ring *ring = kring->ring;
	unsigned int const lim = kring->nkr_num_slots - 1;
	unsigned int head = ring->head;
	unsigned int batch_count = 0;
	if_t ifp = sc->ifp;
	unsigned int count = 0;

	PTNET_Q_LOCK(pq);

	if (unlikely(!(ifp->if_drv_flags & IFF_DRV_RUNNING))) {
		goto unlock;
	}

	kring->nr_kflags &= ~NKR_PENDINTR;

	while (count < budget) {
		unsigned int prev_head = head;
		struct mbuf *mhead, *mtail;
		struct virtio_net_hdr *vh;
		struct netmap_slot *slot;
		unsigned int nmbuf_len;
		uint8_t *nmbuf;
host_sync:
		if (head == ring->tail) {
			/* We ran out of slot, let's see if the host has
			 * added some, by reading hwcur and hwtail from
			 * the CSB. */
			ptnet_sync_tail(ptring, kring);

			if (head == ring->tail) {
				/* Still no slots available. Reactivate
				 * interrupts as they were disabled by the
				 * host thread right before issuing the
				 * last interrupt. */
				ptring->guest_need_kick = 1;

				/* Double-check. */
				ptnet_sync_tail(ptring, kring);
				if (likely(head == ring->tail)) {
					break;
				}
				ptring->guest_need_kick = 0;
			}
		}

		/* Initialize ring state variables, possibly grabbing the
		 * virtio-net header. */
		slot = ring->slot + head;
		nmbuf = NMB(na, slot);
		nmbuf_len = slot->len;

		vh = (struct virtio_net_hdr *)nmbuf;
		if (have_vnet_hdr) {
			if (unlikely(nmbuf_len < PTNET_HDR_SIZE)) {
				/* There is no good reason why host should
				 * put the header in multiple netmap slots.
				 * If this is the case, discard. */
				RD(1, "Fragmented vnet-hdr: dropping");
				head = ptnet_rx_discard(kring, head);
				pq->stats.iqdrops ++;
				goto skip;
			}
			ND(1, "%s: vnet hdr: flags %x csum_start %u "
			      "csum_ofs %u hdr_len = %u gso_size %u "
			      "gso_type %x", __func__, vh->flags,
			      vh->csum_start, vh->csum_offset, vh->hdr_len,
			      vh->gso_size, vh->gso_type);
			nmbuf += PTNET_HDR_SIZE;
			nmbuf_len -= PTNET_HDR_SIZE;
		}

		/* Allocate the head of a new mbuf chain.
		 * We use m_getcl() to allocate an mbuf with standard cluster
		 * size (MCLBYTES). In the future we could use m_getjcl()
		 * to choose different sizes. */
		mhead = mtail = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
		if (unlikely(mhead == NULL)) {
			device_printf(sc->dev, "%s: failed to allocate mbuf "
				      "head\n", __func__);
			pq->stats.errors ++;
			break;
		}

		/* Initialize the mbuf state variables. */
		mhead->m_pkthdr.len = nmbuf_len;
		mtail->m_len = 0;

		/* Scan all the netmap slots containing the current packet. */
		for (;;) {
			DBG(device_printf(sc->dev, "%s: h %u t %u rcv frag "
					  "len %u, flags %u\n", __func__,
					  head, ring->tail, slot->len,
					  slot->flags));

			mtail = ptnet_rx_slot(mtail, nmbuf, nmbuf_len);
			if (unlikely(!mtail)) {
				/* Ouch. We ran out of memory while processing
				 * a packet. We have to restore the previous
				 * head position, free the mbuf chain, and
				 * schedule the taskqueue to give the packet
				 * another chance. */
				device_printf(sc->dev, "%s: failed to allocate"
					" mbuf frag, reset head %u --> %u\n",
					__func__, head, prev_head);
				head = prev_head;
				m_freem(mhead);
				pq->stats.errors ++;
				if (may_resched) {
					taskqueue_enqueue(pq->taskq,
							  &pq->task);
				}
				goto escape;
			}

			/* We have to increment head irrespective of the
			 * NS_MOREFRAG being set or not. */
			head = nm_next(head, lim);

			if (!(slot->flags & NS_MOREFRAG)) {
				break;
			}

			if (unlikely(head == ring->tail)) {
				/* The very last slot prepared by the host has
				 * the NS_MOREFRAG set. Drop it and continue
				 * the outer cycle (to do the double-check). */
				RD(1, "Incomplete packet: dropping");
				m_freem(mhead);
				pq->stats.iqdrops ++;
				goto host_sync;
			}

			slot = ring->slot + head;
			nmbuf = NMB(na, slot);
			nmbuf_len = slot->len;
			mhead->m_pkthdr.len += nmbuf_len;
		}

		mhead->m_pkthdr.rcvif = ifp;
		mhead->m_pkthdr.csum_flags = 0;

		/* Store the queue idx in the packet header. */
		mhead->m_pkthdr.flowid = pq->kring_id;
		M_HASHTYPE_SET(mhead, M_HASHTYPE_OPAQUE);

		if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) {
			struct ether_header *eh;

			eh = mtod(mhead, struct ether_header *);
			if (eh->ether_type == htons(ETHERTYPE_VLAN)) {
				ptnet_vlan_tag_remove(mhead);
				/*
				 * With the 802.1Q header removed, update the
				 * checksum starting location accordingly.
				 */
				if (vh->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
					vh->csum_start -= ETHER_VLAN_ENCAP_LEN;
			}
		}

		if (have_vnet_hdr && (vh->flags & (VIRTIO_NET_HDR_F_NEEDS_CSUM
					| VIRTIO_NET_HDR_F_DATA_VALID))) {
			if (unlikely(ptnet_rx_csum(mhead, vh))) {
				m_freem(mhead);
				RD(1, "Csum offload error: dropping");
				pq->stats.iqdrops ++;
				goto skip;
			}
		}

		pq->stats.packets ++;
		pq->stats.bytes += mhead->m_pkthdr.len;

		PTNET_Q_UNLOCK(pq);
		(*ifp->if_input)(ifp, mhead);
		PTNET_Q_LOCK(pq);

		if (unlikely(!(ifp->if_drv_flags & IFF_DRV_RUNNING))) {
			/* The interface has gone down while we didn't
			 * have the lock. Stop any processing and exit. */
			goto unlock;
		}
skip:
		count ++;
		if (++batch_count == PTNET_RX_BATCH) {
			/* Some packets have been pushed to the network stack.
			 * We need to update the CSB to tell the host about the new
			 * ring->cur and ring->head (RX buffer refill). */
			ptnet_ring_update(pq, kring, head, NAF_FORCE_READ);
			batch_count = 0;
		}
	}
escape:
	if (batch_count) {
		ptnet_ring_update(pq, kring, head, NAF_FORCE_READ);

	}

	if (count >= budget && may_resched) {
		/* If we ran out of budget or the double-check found new
		 * slots to process, schedule the taskqueue. */
		DBG(RD(1, "out of budget: resched h %u t %u\n",
					head, ring->tail));
		taskqueue_enqueue(pq->taskq, &pq->task);
	}
unlock:
	PTNET_Q_UNLOCK(pq);

	return count;
}

static void
ptnet_rx_task(void *context, int pending)
{
	struct ptnet_queue *pq = context;

	DBG(RD(1, "%s: pq #%u\n", __func__, pq->kring_id));
	ptnet_rx_eof(pq, PTNET_RX_BUDGET, true);
}

static void
ptnet_tx_task(void *context, int pending)
{
	struct ptnet_queue *pq = context;

	DBG(RD(1, "%s: pq #%u\n", __func__, pq->kring_id));
	ptnet_drain_transmit_queue(pq, PTNET_TX_BUDGET, true);
}

#ifdef DEVICE_POLLING
/* We don't need to handle differently POLL_AND_CHECK_STATUS and
 * POLL_ONLY, since we don't have an Interrupt Status Register. */
static int
ptnet_poll(if_t ifp, enum poll_cmd cmd, int budget)
{
	struct ptnet_softc *sc = if_getsoftc(ifp);
	unsigned int queue_budget;
	unsigned int count = 0;
	bool borrow = false;
	int i;

	KASSERT(sc->num_rings > 0, ("Found no queues in while polling ptnet"));
	queue_budget = MAX(budget / sc->num_rings, 1);
	RD(1, "Per-queue budget is %d", queue_budget);

	while (budget) {
		unsigned int rcnt = 0;

		for (i = 0; i < sc->num_rings; i++) {
			struct ptnet_queue *pq = sc->queues + i;

			if (borrow) {
				queue_budget = MIN(queue_budget, budget);
				if (queue_budget == 0) {
					break;
				}
			}

			if (i < sc->num_tx_rings) {
				rcnt += ptnet_drain_transmit_queue(pq,
						   queue_budget, false);
			} else {
				rcnt += ptnet_rx_eof(pq, queue_budget,
						      false);
			}
		}

		if (!rcnt) {
			/* A scan of the queues gave no result, we can
			 * stop here. */
			break;
		}

		if (rcnt > budget) {
			/* This may happen when initial budget < sc->num_rings,
			 * since one packet budget is given to each queue
			 * anyway. Just pretend we didn't eat "so much". */
			rcnt = budget;
		}
		count += rcnt;
		budget -= rcnt;
		borrow = true;
	}


	return count;
}
#endif /* DEVICE_POLLING */