xref: /freebsd/sys/dev/netmap/if_ptnet.c (revision 0957b409)
1 /*-
2  * Copyright (c) 2016, Vincenzo Maffione
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 /* Driver for ptnet paravirtualized network device. */
30 
31 #include <sys/cdefs.h>
32 
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/sockio.h>
38 #include <sys/mbuf.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/socket.h>
42 #include <sys/sysctl.h>
43 #include <sys/lock.h>
44 #include <sys/mutex.h>
45 #include <sys/taskqueue.h>
46 #include <sys/smp.h>
47 #include <sys/time.h>
48 #include <machine/smp.h>
49 
50 #include <vm/uma.h>
51 #include <vm/vm.h>
52 #include <vm/pmap.h>
53 
54 #include <net/ethernet.h>
55 #include <net/if.h>
56 #include <net/if_var.h>
57 #include <net/if_arp.h>
58 #include <net/if_dl.h>
59 #include <net/if_types.h>
60 #include <net/if_media.h>
61 #include <net/if_vlan_var.h>
62 #include <net/bpf.h>
63 
64 #include <netinet/in_systm.h>
65 #include <netinet/in.h>
66 #include <netinet/ip.h>
67 #include <netinet/ip6.h>
68 #include <netinet6/ip6_var.h>
69 #include <netinet/udp.h>
70 #include <netinet/tcp.h>
71 #include <netinet/sctp.h>
72 
73 #include <machine/bus.h>
74 #include <machine/resource.h>
75 #include <sys/bus.h>
76 #include <sys/rman.h>
77 
78 #include <dev/pci/pcivar.h>
79 #include <dev/pci/pcireg.h>
80 
81 #include "opt_inet.h"
82 #include "opt_inet6.h"
83 
84 #include <sys/selinfo.h>
85 #include <net/netmap.h>
86 #include <dev/netmap/netmap_kern.h>
87 #include <net/netmap_virt.h>
88 #include <dev/netmap/netmap_mem2.h>
89 #include <dev/virtio/network/virtio_net.h>
90 
91 #ifndef INET
92 #error "INET not defined, cannot support offloadings"
93 #endif
94 
95 #if __FreeBSD_version >= 1100000
96 static uint64_t	ptnet_get_counter(if_t, ift_counter);
97 #else
98 typedef struct ifnet *if_t;
99 #define if_getsoftc(_ifp)   (_ifp)->if_softc
100 #endif
101 
102 //#define PTNETMAP_STATS
103 //#define DEBUG
104 #ifdef DEBUG
105 #define DBG(x) x
106 #else   /* !DEBUG */
107 #define DBG(x)
108 #endif  /* !DEBUG */
109 
110 extern int ptnet_vnet_hdr; /* Tunable parameter */
111 
112 struct ptnet_softc;
113 
114 struct ptnet_queue_stats {
115 	uint64_t	packets; /* if_[io]packets */
116 	uint64_t	bytes;	 /* if_[io]bytes */
117 	uint64_t	errors;	 /* if_[io]errors */
118 	uint64_t	iqdrops; /* if_iqdrops */
119 	uint64_t	mcasts;  /* if_[io]mcasts */
120 #ifdef PTNETMAP_STATS
121 	uint64_t	intrs;
122 	uint64_t	kicks;
123 #endif /* PTNETMAP_STATS */
124 };
125 
126 struct ptnet_queue {
127 	struct ptnet_softc		*sc;
128 	struct				resource *irq;
129 	void				*cookie;
130 	int				kring_id;
131 	struct nm_csb_atok		*atok;
132 	struct nm_csb_ktoa		*ktoa;
133 	unsigned int			kick;
134 	struct mtx			lock;
135 	struct buf_ring			*bufring; /* for TX queues */
136 	struct ptnet_queue_stats	stats;
137 #ifdef PTNETMAP_STATS
138 	struct ptnet_queue_stats	last_stats;
139 #endif /* PTNETMAP_STATS */
140 	struct taskqueue		*taskq;
141 	struct task			task;
142 	char				lock_name[16];
143 };
144 
145 #define PTNET_Q_LOCK(_pq)	mtx_lock(&(_pq)->lock)
146 #define PTNET_Q_TRYLOCK(_pq)	mtx_trylock(&(_pq)->lock)
147 #define PTNET_Q_UNLOCK(_pq)	mtx_unlock(&(_pq)->lock)
148 
149 struct ptnet_softc {
150 	device_t		dev;
151 	if_t			ifp;
152 	struct ifmedia		media;
153 	struct mtx		lock;
154 	char			lock_name[16];
155 	char			hwaddr[ETHER_ADDR_LEN];
156 
157 	/* Mirror of PTFEAT register. */
158 	uint32_t		ptfeatures;
159 	unsigned int		vnet_hdr_len;
160 
161 	/* PCI BARs support. */
162 	struct resource		*iomem;
163 	struct resource		*msix_mem;
164 
165 	unsigned int		num_rings;
166 	unsigned int		num_tx_rings;
167 	struct ptnet_queue	*queues;
168 	struct ptnet_queue	*rxqueues;
169 	struct nm_csb_atok	*csb_gh;
170 	struct nm_csb_ktoa	*csb_hg;
171 
172 	unsigned int		min_tx_space;
173 
174 	struct netmap_pt_guest_adapter *ptna;
175 
176 	struct callout		tick;
177 #ifdef PTNETMAP_STATS
178 	struct timeval		last_ts;
179 #endif /* PTNETMAP_STATS */
180 };
181 
182 #define PTNET_CORE_LOCK(_sc)	mtx_lock(&(_sc)->lock)
183 #define PTNET_CORE_UNLOCK(_sc)	mtx_unlock(&(_sc)->lock)
184 
185 static int	ptnet_probe(device_t);
186 static int	ptnet_attach(device_t);
187 static int	ptnet_detach(device_t);
188 static int	ptnet_suspend(device_t);
189 static int	ptnet_resume(device_t);
190 static int	ptnet_shutdown(device_t);
191 
192 static void	ptnet_init(void *opaque);
193 static int	ptnet_ioctl(if_t ifp, u_long cmd, caddr_t data);
194 static int	ptnet_init_locked(struct ptnet_softc *sc);
195 static int	ptnet_stop(struct ptnet_softc *sc);
196 static int	ptnet_transmit(if_t ifp, struct mbuf *m);
197 static int	ptnet_drain_transmit_queue(struct ptnet_queue *pq,
198 					   unsigned int budget,
199 					   bool may_resched);
200 static void	ptnet_qflush(if_t ifp);
201 static void	ptnet_tx_task(void *context, int pending);
202 
203 static int	ptnet_media_change(if_t ifp);
204 static void	ptnet_media_status(if_t ifp, struct ifmediareq *ifmr);
205 #ifdef PTNETMAP_STATS
206 static void	ptnet_tick(void *opaque);
207 #endif
208 
209 static int	ptnet_irqs_init(struct ptnet_softc *sc);
210 static void	ptnet_irqs_fini(struct ptnet_softc *sc);
211 
212 static uint32_t ptnet_nm_ptctl(struct ptnet_softc *sc, uint32_t cmd);
213 static int      ptnet_nm_config(struct netmap_adapter *na,
214 				struct nm_config_info *info);
215 static void	ptnet_update_vnet_hdr(struct ptnet_softc *sc);
216 static int	ptnet_nm_register(struct netmap_adapter *na, int onoff);
217 static int	ptnet_nm_txsync(struct netmap_kring *kring, int flags);
218 static int	ptnet_nm_rxsync(struct netmap_kring *kring, int flags);
219 static void	ptnet_nm_intr(struct netmap_adapter *na, int onoff);
220 
221 static void	ptnet_tx_intr(void *opaque);
222 static void	ptnet_rx_intr(void *opaque);
223 
224 static unsigned	ptnet_rx_discard(struct netmap_kring *kring,
225 				 unsigned int head);
226 static int	ptnet_rx_eof(struct ptnet_queue *pq, unsigned int budget,
227 			     bool may_resched);
228 static void	ptnet_rx_task(void *context, int pending);
229 
230 #ifdef DEVICE_POLLING
231 static poll_handler_t ptnet_poll;
232 #endif
233 
234 static device_method_t ptnet_methods[] = {
235 	DEVMETHOD(device_probe,			ptnet_probe),
236 	DEVMETHOD(device_attach,		ptnet_attach),
237 	DEVMETHOD(device_detach,		ptnet_detach),
238 	DEVMETHOD(device_suspend,		ptnet_suspend),
239 	DEVMETHOD(device_resume,		ptnet_resume),
240 	DEVMETHOD(device_shutdown,		ptnet_shutdown),
241 	DEVMETHOD_END
242 };
243 
244 static driver_t ptnet_driver = {
245 	"ptnet",
246 	ptnet_methods,
247 	sizeof(struct ptnet_softc)
248 };
249 
250 /* We use (SI_ORDER_MIDDLE+2) here, see DEV_MODULE_ORDERED() invocation. */
251 static devclass_t ptnet_devclass;
252 DRIVER_MODULE_ORDERED(ptnet, pci, ptnet_driver, ptnet_devclass,
253 		      NULL, NULL, SI_ORDER_MIDDLE + 2);
254 
255 static int
256 ptnet_probe(device_t dev)
257 {
258 	if (pci_get_vendor(dev) != PTNETMAP_PCI_VENDOR_ID ||
259 		pci_get_device(dev) != PTNETMAP_PCI_NETIF_ID) {
260 		return (ENXIO);
261 	}
262 
263 	device_set_desc(dev, "ptnet network adapter");
264 
265 	return (BUS_PROBE_DEFAULT);
266 }
267 
268 static inline void ptnet_kick(struct ptnet_queue *pq)
269 {
270 #ifdef PTNETMAP_STATS
271 	pq->stats.kicks ++;
272 #endif /* PTNETMAP_STATS */
273 	bus_write_4(pq->sc->iomem, pq->kick, 0);
274 }
275 
276 #define PTNET_BUF_RING_SIZE	4096
277 #define PTNET_RX_BUDGET		512
278 #define PTNET_RX_BATCH		1
279 #define PTNET_TX_BUDGET		512
280 #define PTNET_TX_BATCH		64
281 #define PTNET_HDR_SIZE		sizeof(struct virtio_net_hdr_mrg_rxbuf)
282 #define PTNET_MAX_PKT_SIZE	65536
283 
284 #define PTNET_CSUM_OFFLOAD	(CSUM_TCP | CSUM_UDP | CSUM_SCTP)
285 #define PTNET_CSUM_OFFLOAD_IPV6	(CSUM_TCP_IPV6 | CSUM_UDP_IPV6 |\
286 				 CSUM_SCTP_IPV6)
287 #define PTNET_ALL_OFFLOAD	(CSUM_TSO | PTNET_CSUM_OFFLOAD |\
288 				 PTNET_CSUM_OFFLOAD_IPV6)
289 
290 static int
291 ptnet_attach(device_t dev)
292 {
293 	uint32_t ptfeatures = 0;
294 	unsigned int num_rx_rings, num_tx_rings;
295 	struct netmap_adapter na_arg;
296 	unsigned int nifp_offset;
297 	struct ptnet_softc *sc;
298 	if_t ifp;
299 	uint32_t macreg;
300 	int err, rid;
301 	int i;
302 
303 	sc = device_get_softc(dev);
304 	sc->dev = dev;
305 
306 	/* Setup PCI resources. */
307 	pci_enable_busmaster(dev);
308 
309 	rid = PCIR_BAR(PTNETMAP_IO_PCI_BAR);
310 	sc->iomem = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
311 					   RF_ACTIVE);
312 	if (sc->iomem == NULL) {
313 		device_printf(dev, "Failed to map I/O BAR\n");
314 		return (ENXIO);
315 	}
316 
317 	/* Negotiate features with the hypervisor. */
318 	if (ptnet_vnet_hdr) {
319 		ptfeatures |= PTNETMAP_F_VNET_HDR;
320 	}
321 	bus_write_4(sc->iomem, PTNET_IO_PTFEAT, ptfeatures); /* wanted */
322 	ptfeatures = bus_read_4(sc->iomem, PTNET_IO_PTFEAT); /* acked */
323 	sc->ptfeatures = ptfeatures;
324 
325 	num_tx_rings = bus_read_4(sc->iomem, PTNET_IO_NUM_TX_RINGS);
326 	num_rx_rings = bus_read_4(sc->iomem, PTNET_IO_NUM_RX_RINGS);
327 	sc->num_rings = num_tx_rings + num_rx_rings;
328 	sc->num_tx_rings = num_tx_rings;
329 
330 	if (sc->num_rings * sizeof(struct nm_csb_atok) > PAGE_SIZE) {
331 		device_printf(dev, "CSB cannot handle that many rings (%u)\n",
332 				sc->num_rings);
333 		err = ENOMEM;
334 		goto err_path;
335 	}
336 
337 	/* Allocate CSB and carry out CSB allocation protocol. */
338 	sc->csb_gh = contigmalloc(2*PAGE_SIZE, M_DEVBUF, M_NOWAIT | M_ZERO,
339 				  (size_t)0, -1UL, PAGE_SIZE, 0);
340 	if (sc->csb_gh == NULL) {
341 		device_printf(dev, "Failed to allocate CSB\n");
342 		err = ENOMEM;
343 		goto err_path;
344 	}
345 	sc->csb_hg = (struct nm_csb_ktoa *)(((char *)sc->csb_gh) + PAGE_SIZE);
346 
347 	{
348 		/*
349 		 * We use uint64_t rather than vm_paddr_t since we
350 		 * need 64 bit addresses even on 32 bit platforms.
351 		 */
352 		uint64_t paddr = vtophys(sc->csb_gh);
353 
354 		/* CSB allocation protocol: write to BAH first, then
355 		 * to BAL (for both GH and HG sections). */
356 		bus_write_4(sc->iomem, PTNET_IO_CSB_GH_BAH,
357 				(paddr >> 32) & 0xffffffff);
358 		bus_write_4(sc->iomem, PTNET_IO_CSB_GH_BAL,
359 				paddr & 0xffffffff);
360 		paddr = vtophys(sc->csb_hg);
361 		bus_write_4(sc->iomem, PTNET_IO_CSB_HG_BAH,
362 				(paddr >> 32) & 0xffffffff);
363 		bus_write_4(sc->iomem, PTNET_IO_CSB_HG_BAL,
364 				paddr & 0xffffffff);
365 	}
366 
367 	/* Allocate and initialize per-queue data structures. */
368 	sc->queues = malloc(sizeof(struct ptnet_queue) * sc->num_rings,
369 			    M_DEVBUF, M_NOWAIT | M_ZERO);
370 	if (sc->queues == NULL) {
371 		err = ENOMEM;
372 		goto err_path;
373 	}
374 	sc->rxqueues = sc->queues + num_tx_rings;
375 
376 	for (i = 0; i < sc->num_rings; i++) {
377 		struct ptnet_queue *pq = sc->queues + i;
378 
379 		pq->sc = sc;
380 		pq->kring_id = i;
381 		pq->kick = PTNET_IO_KICK_BASE + 4 * i;
382 		pq->atok = sc->csb_gh + i;
383 		pq->ktoa = sc->csb_hg + i;
384 		snprintf(pq->lock_name, sizeof(pq->lock_name), "%s-%d",
385 			 device_get_nameunit(dev), i);
386 		mtx_init(&pq->lock, pq->lock_name, NULL, MTX_DEF);
387 		if (i >= num_tx_rings) {
388 			/* RX queue: fix kring_id. */
389 			pq->kring_id -= num_tx_rings;
390 		} else {
391 			/* TX queue: allocate buf_ring. */
392 			pq->bufring = buf_ring_alloc(PTNET_BUF_RING_SIZE,
393 						M_DEVBUF, M_NOWAIT, &pq->lock);
394 			if (pq->bufring == NULL) {
395 				err = ENOMEM;
396 				goto err_path;
397 			}
398 		}
399 	}
400 
401 	sc->min_tx_space = 64; /* Safe initial value. */
402 
403 	err = ptnet_irqs_init(sc);
404 	if (err) {
405 		goto err_path;
406 	}
407 
408 	/* Setup Ethernet interface. */
409 	sc->ifp = ifp = if_alloc(IFT_ETHER);
410 	if (ifp == NULL) {
411 		device_printf(dev, "Failed to allocate ifnet\n");
412 		err = ENOMEM;
413 		goto err_path;
414 	}
415 
416 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
417 	ifp->if_baudrate = IF_Gbps(10);
418 	ifp->if_softc = sc;
419 	ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX;
420 	ifp->if_init = ptnet_init;
421 	ifp->if_ioctl = ptnet_ioctl;
422 #if __FreeBSD_version >= 1100000
423 	ifp->if_get_counter = ptnet_get_counter;
424 #endif
425 	ifp->if_transmit = ptnet_transmit;
426 	ifp->if_qflush = ptnet_qflush;
427 
428 	ifmedia_init(&sc->media, IFM_IMASK, ptnet_media_change,
429 		     ptnet_media_status);
430 	ifmedia_add(&sc->media, IFM_ETHER | IFM_10G_T | IFM_FDX, 0, NULL);
431 	ifmedia_set(&sc->media, IFM_ETHER | IFM_10G_T | IFM_FDX);
432 
433 	macreg = bus_read_4(sc->iomem, PTNET_IO_MAC_HI);
434 	sc->hwaddr[0] = (macreg >> 8) & 0xff;
435 	sc->hwaddr[1] = macreg & 0xff;
436 	macreg = bus_read_4(sc->iomem, PTNET_IO_MAC_LO);
437 	sc->hwaddr[2] = (macreg >> 24) & 0xff;
438 	sc->hwaddr[3] = (macreg >> 16) & 0xff;
439 	sc->hwaddr[4] = (macreg >> 8) & 0xff;
440 	sc->hwaddr[5] = macreg & 0xff;
441 
442 	ether_ifattach(ifp, sc->hwaddr);
443 
444 	ifp->if_hdrlen = sizeof(struct ether_vlan_header);
445 	ifp->if_capabilities |= IFCAP_JUMBO_MTU | IFCAP_VLAN_MTU;
446 
447 	if (sc->ptfeatures & PTNETMAP_F_VNET_HDR) {
448 		/* Similarly to what the vtnet driver does, we can emulate
449 		 * VLAN offloadings by inserting and removing the 802.1Q
450 		 * header during transmit and receive. We are then able
451 		 * to do checksum offloading of VLAN frames. */
452 		ifp->if_capabilities |= IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6
453 					| IFCAP_VLAN_HWCSUM
454 					| IFCAP_TSO | IFCAP_LRO
455 					| IFCAP_VLAN_HWTSO
456 					| IFCAP_VLAN_HWTAGGING;
457 	}
458 
459 	ifp->if_capenable = ifp->if_capabilities;
460 #ifdef DEVICE_POLLING
461 	/* Don't enable polling by default. */
462 	ifp->if_capabilities |= IFCAP_POLLING;
463 #endif
464 	snprintf(sc->lock_name, sizeof(sc->lock_name),
465 		 "%s", device_get_nameunit(dev));
466 	mtx_init(&sc->lock, sc->lock_name, "ptnet core lock", MTX_DEF);
467 	callout_init_mtx(&sc->tick, &sc->lock, 0);
468 
469 	/* Prepare a netmap_adapter struct instance to do netmap_attach(). */
470 	nifp_offset = bus_read_4(sc->iomem, PTNET_IO_NIFP_OFS);
471 	memset(&na_arg, 0, sizeof(na_arg));
472 	na_arg.ifp = ifp;
473 	na_arg.num_tx_desc = bus_read_4(sc->iomem, PTNET_IO_NUM_TX_SLOTS);
474 	na_arg.num_rx_desc = bus_read_4(sc->iomem, PTNET_IO_NUM_RX_SLOTS);
475 	na_arg.num_tx_rings = num_tx_rings;
476 	na_arg.num_rx_rings = num_rx_rings;
477 	na_arg.nm_config = ptnet_nm_config;
478 	na_arg.nm_krings_create = ptnet_nm_krings_create;
479 	na_arg.nm_krings_delete = ptnet_nm_krings_delete;
480 	na_arg.nm_dtor = ptnet_nm_dtor;
481 	na_arg.nm_intr = ptnet_nm_intr;
482 	na_arg.nm_register = ptnet_nm_register;
483 	na_arg.nm_txsync = ptnet_nm_txsync;
484 	na_arg.nm_rxsync = ptnet_nm_rxsync;
485 
486 	netmap_pt_guest_attach(&na_arg, nifp_offset,
487                                 bus_read_4(sc->iomem, PTNET_IO_HOSTMEMID));
488 
489 	/* Now a netmap adapter for this ifp has been allocated, and it
490 	 * can be accessed through NA(ifp). We also have to initialize the CSB
491 	 * pointer. */
492 	sc->ptna = (struct netmap_pt_guest_adapter *)NA(ifp);
493 
494 	/* If virtio-net header was negotiated, set the virt_hdr_len field in
495 	 * the netmap adapter, to inform users that this netmap adapter requires
496 	 * the application to deal with the headers. */
497 	ptnet_update_vnet_hdr(sc);
498 
499 	device_printf(dev, "%s() completed\n", __func__);
500 
501 	return (0);
502 
503 err_path:
504 	ptnet_detach(dev);
505 	return err;
506 }
507 
508 /* Stop host sync-kloop if it was running. */
509 static void
510 ptnet_device_shutdown(struct ptnet_softc *sc)
511 {
512 	ptnet_nm_ptctl(sc, PTNETMAP_PTCTL_DELETE);
513 	bus_write_4(sc->iomem, PTNET_IO_CSB_GH_BAH, 0);
514 	bus_write_4(sc->iomem, PTNET_IO_CSB_GH_BAL, 0);
515 	bus_write_4(sc->iomem, PTNET_IO_CSB_HG_BAH, 0);
516 	bus_write_4(sc->iomem, PTNET_IO_CSB_HG_BAL, 0);
517 }
518 
519 static int
520 ptnet_detach(device_t dev)
521 {
522 	struct ptnet_softc *sc = device_get_softc(dev);
523 	int i;
524 
525 	ptnet_device_shutdown(sc);
526 
527 #ifdef DEVICE_POLLING
528 	if (sc->ifp->if_capenable & IFCAP_POLLING) {
529 		ether_poll_deregister(sc->ifp);
530 	}
531 #endif
532 	callout_drain(&sc->tick);
533 
534 	if (sc->queues) {
535 		/* Drain taskqueues before calling if_detach. */
536 		for (i = 0; i < sc->num_rings; i++) {
537 			struct ptnet_queue *pq = sc->queues + i;
538 
539 			if (pq->taskq) {
540 				taskqueue_drain(pq->taskq, &pq->task);
541 			}
542 		}
543 	}
544 
545 	if (sc->ifp) {
546 		ether_ifdetach(sc->ifp);
547 
548 		/* Uninitialize netmap adapters for this device. */
549 		netmap_detach(sc->ifp);
550 
551 		ifmedia_removeall(&sc->media);
552 		if_free(sc->ifp);
553 		sc->ifp = NULL;
554 	}
555 
556 	ptnet_irqs_fini(sc);
557 
558 	if (sc->csb_gh) {
559 		contigfree(sc->csb_gh, 2*PAGE_SIZE, M_DEVBUF);
560 		sc->csb_gh = NULL;
561 		sc->csb_hg = NULL;
562 	}
563 
564 	if (sc->queues) {
565 		for (i = 0; i < sc->num_rings; i++) {
566 			struct ptnet_queue *pq = sc->queues + i;
567 
568 			if (mtx_initialized(&pq->lock)) {
569 				mtx_destroy(&pq->lock);
570 			}
571 			if (pq->bufring != NULL) {
572 				buf_ring_free(pq->bufring, M_DEVBUF);
573 			}
574 		}
575 		free(sc->queues, M_DEVBUF);
576 		sc->queues = NULL;
577 	}
578 
579 	if (sc->iomem) {
580 		bus_release_resource(dev, SYS_RES_IOPORT,
581 				     PCIR_BAR(PTNETMAP_IO_PCI_BAR), sc->iomem);
582 		sc->iomem = NULL;
583 	}
584 
585 	mtx_destroy(&sc->lock);
586 
587 	device_printf(dev, "%s() completed\n", __func__);
588 
589 	return (0);
590 }
591 
592 static int
593 ptnet_suspend(device_t dev)
594 {
595 	struct ptnet_softc *sc = device_get_softc(dev);
596 
597 	(void)sc;
598 
599 	return (0);
600 }
601 
602 static int
603 ptnet_resume(device_t dev)
604 {
605 	struct ptnet_softc *sc = device_get_softc(dev);
606 
607 	(void)sc;
608 
609 	return (0);
610 }
611 
612 static int
613 ptnet_shutdown(device_t dev)
614 {
615 	struct ptnet_softc *sc = device_get_softc(dev);
616 
617 	ptnet_device_shutdown(sc);
618 
619 	return (0);
620 }
621 
622 static int
623 ptnet_irqs_init(struct ptnet_softc *sc)
624 {
625 	int rid = PCIR_BAR(PTNETMAP_MSIX_PCI_BAR);
626 	int nvecs = sc->num_rings;
627 	device_t dev = sc->dev;
628 	int err = ENOSPC;
629 	int cpu_cur;
630 	int i;
631 
632 	if (pci_find_cap(dev, PCIY_MSIX, NULL) != 0)  {
633 		device_printf(dev, "Could not find MSI-X capability\n");
634 		return (ENXIO);
635 	}
636 
637 	sc->msix_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
638 					      &rid, RF_ACTIVE);
639 	if (sc->msix_mem == NULL) {
640 		device_printf(dev, "Failed to allocate MSIX PCI BAR\n");
641 		return (ENXIO);
642 	}
643 
644 	if (pci_msix_count(dev) < nvecs) {
645 		device_printf(dev, "Not enough MSI-X vectors\n");
646 		goto err_path;
647 	}
648 
649 	err = pci_alloc_msix(dev, &nvecs);
650 	if (err) {
651 		device_printf(dev, "Failed to allocate MSI-X vectors\n");
652 		goto err_path;
653 	}
654 
655 	for (i = 0; i < nvecs; i++) {
656 		struct ptnet_queue *pq = sc->queues + i;
657 
658 		rid = i + 1;
659 		pq->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
660 						 RF_ACTIVE);
661 		if (pq->irq == NULL) {
662 			device_printf(dev, "Failed to allocate interrupt "
663 					   "for queue #%d\n", i);
664 			err = ENOSPC;
665 			goto err_path;
666 		}
667 	}
668 
669 	cpu_cur = CPU_FIRST();
670 	for (i = 0; i < nvecs; i++) {
671 		struct ptnet_queue *pq = sc->queues + i;
672 		void (*handler)(void *) = ptnet_tx_intr;
673 
674 		if (i >= sc->num_tx_rings) {
675 			handler = ptnet_rx_intr;
676 		}
677 		err = bus_setup_intr(dev, pq->irq, INTR_TYPE_NET | INTR_MPSAFE,
678 				     NULL /* intr_filter */, handler,
679 				     pq, &pq->cookie);
680 		if (err) {
681 			device_printf(dev, "Failed to register intr handler "
682 					   "for queue #%d\n", i);
683 			goto err_path;
684 		}
685 
686 		bus_describe_intr(dev, pq->irq, pq->cookie, "q%d", i);
687 #if 0
688 		bus_bind_intr(sc->dev, pq->irq, cpu_cur);
689 #endif
690 		cpu_cur = CPU_NEXT(cpu_cur);
691 	}
692 
693 	device_printf(dev, "Allocated %d MSI-X vectors\n", nvecs);
694 
695 	cpu_cur = CPU_FIRST();
696 	for (i = 0; i < nvecs; i++) {
697 		struct ptnet_queue *pq = sc->queues + i;
698 		static void (*handler)(void *context, int pending);
699 
700 		handler = (i < sc->num_tx_rings) ? ptnet_tx_task : ptnet_rx_task;
701 
702 		TASK_INIT(&pq->task, 0, handler, pq);
703 		pq->taskq = taskqueue_create_fast("ptnet_queue", M_NOWAIT,
704 					taskqueue_thread_enqueue, &pq->taskq);
705 		taskqueue_start_threads(&pq->taskq, 1, PI_NET, "%s-pq-%d",
706 					device_get_nameunit(sc->dev), cpu_cur);
707 		cpu_cur = CPU_NEXT(cpu_cur);
708 	}
709 
710 	return 0;
711 err_path:
712 	ptnet_irqs_fini(sc);
713 	return err;
714 }
715 
716 static void
717 ptnet_irqs_fini(struct ptnet_softc *sc)
718 {
719 	device_t dev = sc->dev;
720 	int i;
721 
722 	for (i = 0; i < sc->num_rings; i++) {
723 		struct ptnet_queue *pq = sc->queues + i;
724 
725 		if (pq->taskq) {
726 			taskqueue_free(pq->taskq);
727 			pq->taskq = NULL;
728 		}
729 
730 		if (pq->cookie) {
731 			bus_teardown_intr(dev, pq->irq, pq->cookie);
732 			pq->cookie = NULL;
733 		}
734 
735 		if (pq->irq) {
736 			bus_release_resource(dev, SYS_RES_IRQ, i + 1, pq->irq);
737 			pq->irq = NULL;
738 		}
739 	}
740 
741 	if (sc->msix_mem) {
742 		pci_release_msi(dev);
743 
744 		bus_release_resource(dev, SYS_RES_MEMORY,
745 				     PCIR_BAR(PTNETMAP_MSIX_PCI_BAR),
746 				     sc->msix_mem);
747 		sc->msix_mem = NULL;
748 	}
749 }
750 
751 static void
752 ptnet_init(void *opaque)
753 {
754 	struct ptnet_softc *sc = opaque;
755 
756 	PTNET_CORE_LOCK(sc);
757 	ptnet_init_locked(sc);
758 	PTNET_CORE_UNLOCK(sc);
759 }
760 
761 static int
762 ptnet_ioctl(if_t ifp, u_long cmd, caddr_t data)
763 {
764 	struct ptnet_softc *sc = if_getsoftc(ifp);
765 	device_t dev = sc->dev;
766 	struct ifreq *ifr = (struct ifreq *)data;
767 	int mask __unused, err = 0;
768 
769 	switch (cmd) {
770 	case SIOCSIFFLAGS:
771 		device_printf(dev, "SIOCSIFFLAGS %x\n", ifp->if_flags);
772 		PTNET_CORE_LOCK(sc);
773 		if (ifp->if_flags & IFF_UP) {
774 			/* Network stack wants the iff to be up. */
775 			err = ptnet_init_locked(sc);
776 		} else {
777 			/* Network stack wants the iff to be down. */
778 			err = ptnet_stop(sc);
779 		}
780 		/* We don't need to do nothing to support IFF_PROMISC,
781 		 * since that is managed by the backend port. */
782 		PTNET_CORE_UNLOCK(sc);
783 		break;
784 
785 	case SIOCSIFCAP:
786 		device_printf(dev, "SIOCSIFCAP %x %x\n",
787 			      ifr->ifr_reqcap, ifp->if_capenable);
788 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
789 #ifdef DEVICE_POLLING
790 		if (mask & IFCAP_POLLING) {
791 			struct ptnet_queue *pq;
792 			int i;
793 
794 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
795 				err = ether_poll_register(ptnet_poll, ifp);
796 				if (err) {
797 					break;
798 				}
799 				/* Stop queues and sync with taskqueues. */
800 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
801 				for (i = 0; i < sc->num_rings; i++) {
802 					pq = sc-> queues + i;
803 					/* Make sure the worker sees the
804 					 * IFF_DRV_RUNNING down. */
805 					PTNET_Q_LOCK(pq);
806 					pq->atok->appl_need_kick = 0;
807 					PTNET_Q_UNLOCK(pq);
808 					/* Wait for rescheduling to finish. */
809 					if (pq->taskq) {
810 						taskqueue_drain(pq->taskq,
811 								&pq->task);
812 					}
813 				}
814 				ifp->if_drv_flags |= IFF_DRV_RUNNING;
815 			} else {
816 				err = ether_poll_deregister(ifp);
817 				for (i = 0; i < sc->num_rings; i++) {
818 					pq = sc-> queues + i;
819 					PTNET_Q_LOCK(pq);
820 					pq->atok->appl_need_kick = 1;
821 					PTNET_Q_UNLOCK(pq);
822 				}
823 			}
824 		}
825 #endif  /* DEVICE_POLLING */
826 		ifp->if_capenable = ifr->ifr_reqcap;
827 		break;
828 
829 	case SIOCSIFMTU:
830 		/* We support any reasonable MTU. */
831 		if (ifr->ifr_mtu < ETHERMIN ||
832 				ifr->ifr_mtu > PTNET_MAX_PKT_SIZE) {
833 			err = EINVAL;
834 		} else {
835 			PTNET_CORE_LOCK(sc);
836 			ifp->if_mtu = ifr->ifr_mtu;
837 			PTNET_CORE_UNLOCK(sc);
838 		}
839 		break;
840 
841 	case SIOCSIFMEDIA:
842 	case SIOCGIFMEDIA:
843 		err = ifmedia_ioctl(ifp, ifr, &sc->media, cmd);
844 		break;
845 
846 	default:
847 		err = ether_ioctl(ifp, cmd, data);
848 		break;
849 	}
850 
851 	return err;
852 }
853 
854 static int
855 ptnet_init_locked(struct ptnet_softc *sc)
856 {
857 	if_t ifp = sc->ifp;
858 	struct netmap_adapter *na_dr = &sc->ptna->dr.up;
859 	struct netmap_adapter *na_nm = &sc->ptna->hwup.up;
860 	unsigned int nm_buf_size;
861 	int ret;
862 
863 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
864 		return 0; /* nothing to do */
865 	}
866 
867 	device_printf(sc->dev, "%s\n", __func__);
868 
869 	/* Translate offload capabilities according to if_capenable. */
870 	ifp->if_hwassist = 0;
871 	if (ifp->if_capenable & IFCAP_TXCSUM)
872 		ifp->if_hwassist |= PTNET_CSUM_OFFLOAD;
873 	if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
874 		ifp->if_hwassist |= PTNET_CSUM_OFFLOAD_IPV6;
875 	if (ifp->if_capenable & IFCAP_TSO4)
876 		ifp->if_hwassist |= CSUM_IP_TSO;
877 	if (ifp->if_capenable & IFCAP_TSO6)
878 		ifp->if_hwassist |= CSUM_IP6_TSO;
879 
880 	/*
881 	 * Prepare the interface for netmap mode access.
882 	 */
883 	netmap_update_config(na_dr);
884 
885 	ret = netmap_mem_finalize(na_dr->nm_mem, na_dr);
886 	if (ret) {
887 		device_printf(sc->dev, "netmap_mem_finalize() failed\n");
888 		return ret;
889 	}
890 
891 	if (sc->ptna->backend_users == 0) {
892 		ret = ptnet_nm_krings_create(na_nm);
893 		if (ret) {
894 			device_printf(sc->dev, "ptnet_nm_krings_create() "
895 					       "failed\n");
896 			goto err_mem_finalize;
897 		}
898 
899 		ret = netmap_mem_rings_create(na_dr);
900 		if (ret) {
901 			device_printf(sc->dev, "netmap_mem_rings_create() "
902 					       "failed\n");
903 			goto err_rings_create;
904 		}
905 
906 		ret = netmap_mem_get_lut(na_dr->nm_mem, &na_dr->na_lut);
907 		if (ret) {
908 			device_printf(sc->dev, "netmap_mem_get_lut() "
909 					       "failed\n");
910 			goto err_get_lut;
911 		}
912 	}
913 
914 	ret = ptnet_nm_register(na_dr, 1 /* on */);
915 	if (ret) {
916 		goto err_register;
917 	}
918 
919 	nm_buf_size = NETMAP_BUF_SIZE(na_dr);
920 
921 	KASSERT(nm_buf_size > 0, ("Invalid netmap buffer size"));
922 	sc->min_tx_space = PTNET_MAX_PKT_SIZE / nm_buf_size + 2;
923 	device_printf(sc->dev, "%s: min_tx_space = %u\n", __func__,
924 		      sc->min_tx_space);
925 #ifdef PTNETMAP_STATS
926 	callout_reset(&sc->tick, hz, ptnet_tick, sc);
927 #endif
928 
929 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
930 
931 	return 0;
932 
933 err_register:
934 	memset(&na_dr->na_lut, 0, sizeof(na_dr->na_lut));
935 err_get_lut:
936 	netmap_mem_rings_delete(na_dr);
937 err_rings_create:
938 	ptnet_nm_krings_delete(na_nm);
939 err_mem_finalize:
940 	netmap_mem_deref(na_dr->nm_mem, na_dr);
941 
942 	return ret;
943 }
944 
945 /* To be called under core lock. */
946 static int
947 ptnet_stop(struct ptnet_softc *sc)
948 {
949 	if_t ifp = sc->ifp;
950 	struct netmap_adapter *na_dr = &sc->ptna->dr.up;
951 	struct netmap_adapter *na_nm = &sc->ptna->hwup.up;
952 	int i;
953 
954 	device_printf(sc->dev, "%s\n", __func__);
955 
956 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
957 		return 0; /* nothing to do */
958 	}
959 
960 	/* Clear the driver-ready flag, and synchronize with all the queues,
961 	 * so that after this loop we are sure nobody is working anymore with
962 	 * the device. This scheme is taken from the vtnet driver. */
963 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
964 	callout_stop(&sc->tick);
965 	for (i = 0; i < sc->num_rings; i++) {
966 		PTNET_Q_LOCK(sc->queues + i);
967 		PTNET_Q_UNLOCK(sc->queues + i);
968 	}
969 
970 	ptnet_nm_register(na_dr, 0 /* off */);
971 
972 	if (sc->ptna->backend_users == 0) {
973 		netmap_mem_rings_delete(na_dr);
974 		ptnet_nm_krings_delete(na_nm);
975 	}
976 	netmap_mem_deref(na_dr->nm_mem, na_dr);
977 
978 	return 0;
979 }
980 
981 static void
982 ptnet_qflush(if_t ifp)
983 {
984 	struct ptnet_softc *sc = if_getsoftc(ifp);
985 	int i;
986 
987 	/* Flush all the bufrings and do the interface flush. */
988 	for (i = 0; i < sc->num_rings; i++) {
989 		struct ptnet_queue *pq = sc->queues + i;
990 		struct mbuf *m;
991 
992 		PTNET_Q_LOCK(pq);
993 		if (pq->bufring) {
994 			while ((m = buf_ring_dequeue_sc(pq->bufring))) {
995 				m_freem(m);
996 			}
997 		}
998 		PTNET_Q_UNLOCK(pq);
999 	}
1000 
1001 	if_qflush(ifp);
1002 }
1003 
1004 static int
1005 ptnet_media_change(if_t ifp)
1006 {
1007 	struct ptnet_softc *sc = if_getsoftc(ifp);
1008 	struct ifmedia *ifm = &sc->media;
1009 
1010 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER) {
1011 		return EINVAL;
1012 	}
1013 
1014 	return 0;
1015 }
1016 
1017 #if __FreeBSD_version >= 1100000
1018 static uint64_t
1019 ptnet_get_counter(if_t ifp, ift_counter cnt)
1020 {
1021 	struct ptnet_softc *sc = if_getsoftc(ifp);
1022 	struct ptnet_queue_stats stats[2];
1023 	int i;
1024 
1025 	/* Accumulate statistics over the queues. */
1026 	memset(stats, 0, sizeof(stats));
1027 	for (i = 0; i < sc->num_rings; i++) {
1028 		struct ptnet_queue *pq = sc->queues + i;
1029 		int idx = (i < sc->num_tx_rings) ? 0 : 1;
1030 
1031 		stats[idx].packets	+= pq->stats.packets;
1032 		stats[idx].bytes	+= pq->stats.bytes;
1033 		stats[idx].errors	+= pq->stats.errors;
1034 		stats[idx].iqdrops	+= pq->stats.iqdrops;
1035 		stats[idx].mcasts	+= pq->stats.mcasts;
1036 	}
1037 
1038 	switch (cnt) {
1039 	case IFCOUNTER_IPACKETS:
1040 		return (stats[1].packets);
1041 	case IFCOUNTER_IQDROPS:
1042 		return (stats[1].iqdrops);
1043 	case IFCOUNTER_IERRORS:
1044 		return (stats[1].errors);
1045 	case IFCOUNTER_OPACKETS:
1046 		return (stats[0].packets);
1047 	case IFCOUNTER_OBYTES:
1048 		return (stats[0].bytes);
1049 	case IFCOUNTER_OMCASTS:
1050 		return (stats[0].mcasts);
1051 	default:
1052 		return (if_get_counter_default(ifp, cnt));
1053 	}
1054 }
1055 #endif
1056 
1057 
1058 #ifdef PTNETMAP_STATS
1059 /* Called under core lock. */
1060 static void
1061 ptnet_tick(void *opaque)
1062 {
1063 	struct ptnet_softc *sc = opaque;
1064 	int i;
1065 
1066 	for (i = 0; i < sc->num_rings; i++) {
1067 		struct ptnet_queue *pq = sc->queues + i;
1068 		struct ptnet_queue_stats cur = pq->stats;
1069 		struct timeval now;
1070 		unsigned int delta;
1071 
1072 		microtime(&now);
1073 		delta = now.tv_usec - sc->last_ts.tv_usec +
1074 			(now.tv_sec - sc->last_ts.tv_sec) * 1000000;
1075 		delta /= 1000; /* in milliseconds */
1076 
1077 		if (delta == 0)
1078 			continue;
1079 
1080 		device_printf(sc->dev, "#%d[%u ms]:pkts %lu, kicks %lu, "
1081 			      "intr %lu\n", i, delta,
1082 			      (cur.packets - pq->last_stats.packets),
1083 			      (cur.kicks - pq->last_stats.kicks),
1084 			      (cur.intrs - pq->last_stats.intrs));
1085 		pq->last_stats = cur;
1086 	}
1087 	microtime(&sc->last_ts);
1088 	callout_schedule(&sc->tick, hz);
1089 }
1090 #endif /* PTNETMAP_STATS */
1091 
1092 static void
1093 ptnet_media_status(if_t ifp, struct ifmediareq *ifmr)
1094 {
1095 	/* We are always active, as the backend netmap port is
1096 	 * always open in netmap mode. */
1097 	ifmr->ifm_status = IFM_AVALID | IFM_ACTIVE;
1098 	ifmr->ifm_active = IFM_ETHER | IFM_10G_T | IFM_FDX;
1099 }
1100 
1101 static uint32_t
1102 ptnet_nm_ptctl(struct ptnet_softc *sc, uint32_t cmd)
1103 {
1104 	/*
1105 	 * Write a command and read back error status,
1106 	 * with zero meaning success.
1107 	 */
1108 	bus_write_4(sc->iomem, PTNET_IO_PTCTL, cmd);
1109 	return bus_read_4(sc->iomem, PTNET_IO_PTCTL);
1110 }
1111 
1112 static int
1113 ptnet_nm_config(struct netmap_adapter *na, struct nm_config_info *info)
1114 {
1115 	struct ptnet_softc *sc = if_getsoftc(na->ifp);
1116 
1117 	info->num_tx_rings = bus_read_4(sc->iomem, PTNET_IO_NUM_TX_RINGS);
1118 	info->num_rx_rings = bus_read_4(sc->iomem, PTNET_IO_NUM_RX_RINGS);
1119 	info->num_tx_descs = bus_read_4(sc->iomem, PTNET_IO_NUM_TX_SLOTS);
1120 	info->num_rx_descs = bus_read_4(sc->iomem, PTNET_IO_NUM_RX_SLOTS);
1121 	info->rx_buf_maxsize = NETMAP_BUF_SIZE(na);
1122 
1123 	device_printf(sc->dev, "txr %u, rxr %u, txd %u, rxd %u, rxbufsz %u\n",
1124 			info->num_tx_rings, info->num_rx_rings,
1125 			info->num_tx_descs, info->num_rx_descs,
1126 			info->rx_buf_maxsize);
1127 
1128 	return 0;
1129 }
1130 
1131 static void
1132 ptnet_sync_from_csb(struct ptnet_softc *sc, struct netmap_adapter *na)
1133 {
1134 	int i;
1135 
1136 	/* Sync krings from the host, reading from
1137 	 * CSB. */
1138 	for (i = 0; i < sc->num_rings; i++) {
1139 		struct nm_csb_atok *atok = sc->queues[i].atok;
1140 		struct nm_csb_ktoa *ktoa = sc->queues[i].ktoa;
1141 		struct netmap_kring *kring;
1142 
1143 		if (i < na->num_tx_rings) {
1144 			kring = na->tx_rings[i];
1145 		} else {
1146 			kring = na->rx_rings[i - na->num_tx_rings];
1147 		}
1148 		kring->rhead = kring->ring->head = atok->head;
1149 		kring->rcur = kring->ring->cur = atok->cur;
1150 		kring->nr_hwcur = ktoa->hwcur;
1151 		kring->nr_hwtail = kring->rtail =
1152 			kring->ring->tail = ktoa->hwtail;
1153 
1154 		nm_prdis("%d,%d: csb {hc %u h %u c %u ht %u}", t, i,
1155 		   ktoa->hwcur, atok->head, atok->cur,
1156 		   ktoa->hwtail);
1157 		nm_prdis("%d,%d: kring {hc %u rh %u rc %u h %u c %u ht %u rt %u t %u}",
1158 		   t, i, kring->nr_hwcur, kring->rhead, kring->rcur,
1159 		   kring->ring->head, kring->ring->cur, kring->nr_hwtail,
1160 		   kring->rtail, kring->ring->tail);
1161 	}
1162 }
1163 
1164 static void
1165 ptnet_update_vnet_hdr(struct ptnet_softc *sc)
1166 {
1167 	unsigned int wanted_hdr_len = ptnet_vnet_hdr ? PTNET_HDR_SIZE : 0;
1168 
1169 	bus_write_4(sc->iomem, PTNET_IO_VNET_HDR_LEN, wanted_hdr_len);
1170 	sc->vnet_hdr_len = bus_read_4(sc->iomem, PTNET_IO_VNET_HDR_LEN);
1171 	sc->ptna->hwup.up.virt_hdr_len = sc->vnet_hdr_len;
1172 }
1173 
1174 static int
1175 ptnet_nm_register(struct netmap_adapter *na, int onoff)
1176 {
1177 	/* device-specific */
1178 	if_t ifp = na->ifp;
1179 	struct ptnet_softc *sc = if_getsoftc(ifp);
1180 	int native = (na == &sc->ptna->hwup.up);
1181 	struct ptnet_queue *pq;
1182 	int ret = 0;
1183 	int i;
1184 
1185 	if (!onoff) {
1186 		sc->ptna->backend_users--;
1187 	}
1188 
1189 	/* If this is the last netmap client, guest interrupt enable flags may
1190 	 * be in arbitrary state. Since these flags are going to be used also
1191 	 * by the netdevice driver, we have to make sure to start with
1192 	 * notifications enabled. Also, schedule NAPI to flush pending packets
1193 	 * in the RX rings, since we will not receive further interrupts
1194 	 * until these will be processed. */
1195 	if (native && !onoff && na->active_fds == 0) {
1196 		nm_prinf("Exit netmap mode, re-enable interrupts");
1197 		for (i = 0; i < sc->num_rings; i++) {
1198 			pq = sc->queues + i;
1199 			pq->atok->appl_need_kick = 1;
1200 		}
1201 	}
1202 
1203 	if (onoff) {
1204 		if (sc->ptna->backend_users == 0) {
1205 			/* Initialize notification enable fields in the CSB. */
1206 			for (i = 0; i < sc->num_rings; i++) {
1207 				pq = sc->queues + i;
1208 				pq->ktoa->kern_need_kick = 1;
1209 				pq->atok->appl_need_kick =
1210 					(!(ifp->if_capenable & IFCAP_POLLING)
1211 						&& i >= sc->num_tx_rings);
1212 			}
1213 
1214 			/* Set the virtio-net header length. */
1215 			ptnet_update_vnet_hdr(sc);
1216 
1217 			/* Make sure the host adapter passed through is ready
1218 			 * for txsync/rxsync. */
1219 			ret = ptnet_nm_ptctl(sc, PTNETMAP_PTCTL_CREATE);
1220 			if (ret) {
1221 				return ret;
1222 			}
1223 
1224 			/* Align the guest krings and rings to the state stored
1225 			 * in the CSB. */
1226 			ptnet_sync_from_csb(sc, na);
1227 		}
1228 
1229 		/* If not native, don't call nm_set_native_flags, since we don't want
1230 		 * to replace if_transmit method, nor set NAF_NETMAP_ON */
1231 		if (native) {
1232 			netmap_krings_mode_commit(na, onoff);
1233 			nm_set_native_flags(na);
1234 		}
1235 
1236 	} else {
1237 		if (native) {
1238 			nm_clear_native_flags(na);
1239 			netmap_krings_mode_commit(na, onoff);
1240 		}
1241 
1242 		if (sc->ptna->backend_users == 0) {
1243 			ret = ptnet_nm_ptctl(sc, PTNETMAP_PTCTL_DELETE);
1244 		}
1245 	}
1246 
1247 	if (onoff) {
1248 		sc->ptna->backend_users++;
1249 	}
1250 
1251 	return ret;
1252 }
1253 
1254 static int
1255 ptnet_nm_txsync(struct netmap_kring *kring, int flags)
1256 {
1257 	struct ptnet_softc *sc = if_getsoftc(kring->na->ifp);
1258 	struct ptnet_queue *pq = sc->queues + kring->ring_id;
1259 	bool notify;
1260 
1261 	notify = netmap_pt_guest_txsync(pq->atok, pq->ktoa, kring, flags);
1262 	if (notify) {
1263 		ptnet_kick(pq);
1264 	}
1265 
1266 	return 0;
1267 }
1268 
1269 static int
1270 ptnet_nm_rxsync(struct netmap_kring *kring, int flags)
1271 {
1272 	struct ptnet_softc *sc = if_getsoftc(kring->na->ifp);
1273 	struct ptnet_queue *pq = sc->rxqueues + kring->ring_id;
1274 	bool notify;
1275 
1276 	notify = netmap_pt_guest_rxsync(pq->atok, pq->ktoa, kring, flags);
1277 	if (notify) {
1278 		ptnet_kick(pq);
1279 	}
1280 
1281 	return 0;
1282 }
1283 
1284 static void
1285 ptnet_nm_intr(struct netmap_adapter *na, int onoff)
1286 {
1287 	struct ptnet_softc *sc = if_getsoftc(na->ifp);
1288 	int i;
1289 
1290 	for (i = 0; i < sc->num_rings; i++) {
1291 		struct ptnet_queue *pq = sc->queues + i;
1292 		pq->atok->appl_need_kick = onoff;
1293 	}
1294 }
1295 
1296 static void
1297 ptnet_tx_intr(void *opaque)
1298 {
1299 	struct ptnet_queue *pq = opaque;
1300 	struct ptnet_softc *sc = pq->sc;
1301 
1302 	DBG(device_printf(sc->dev, "Tx interrupt #%d\n", pq->kring_id));
1303 #ifdef PTNETMAP_STATS
1304 	pq->stats.intrs ++;
1305 #endif /* PTNETMAP_STATS */
1306 
1307 	if (netmap_tx_irq(sc->ifp, pq->kring_id) != NM_IRQ_PASS) {
1308 		return;
1309 	}
1310 
1311 	/* Schedule the tasqueue to flush process transmissions requests.
1312 	 * However, vtnet, if_em and if_igb just call ptnet_transmit() here,
1313 	 * at least when using MSI-X interrupts. The if_em driver, instead
1314 	 * schedule taskqueue when using legacy interrupts. */
1315 	taskqueue_enqueue(pq->taskq, &pq->task);
1316 }
1317 
1318 static void
1319 ptnet_rx_intr(void *opaque)
1320 {
1321 	struct ptnet_queue *pq = opaque;
1322 	struct ptnet_softc *sc = pq->sc;
1323 	unsigned int unused;
1324 
1325 	DBG(device_printf(sc->dev, "Rx interrupt #%d\n", pq->kring_id));
1326 #ifdef PTNETMAP_STATS
1327 	pq->stats.intrs ++;
1328 #endif /* PTNETMAP_STATS */
1329 
1330 	if (netmap_rx_irq(sc->ifp, pq->kring_id, &unused) != NM_IRQ_PASS) {
1331 		return;
1332 	}
1333 
1334 	/* Like vtnet, if_igb and if_em drivers when using MSI-X interrupts,
1335 	 * receive-side processing is executed directly in the interrupt
1336 	 * service routine. Alternatively, we may schedule the taskqueue. */
1337 	ptnet_rx_eof(pq, PTNET_RX_BUDGET, true);
1338 }
1339 
1340 /* The following offloadings-related functions are taken from the vtnet
1341  * driver, but the same functionality is required for the ptnet driver.
1342  * As a temporary solution, I copied this code from vtnet and I started
1343  * to generalize it (taking away driver-specific statistic accounting),
1344  * making as little modifications as possible.
1345  * In the future we need to share these functions between vtnet and ptnet.
1346  */
1347 static int
1348 ptnet_tx_offload_ctx(struct mbuf *m, int *etype, int *proto, int *start)
1349 {
1350 	struct ether_vlan_header *evh;
1351 	int offset;
1352 
1353 	evh = mtod(m, struct ether_vlan_header *);
1354 	if (evh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
1355 		/* BMV: We should handle nested VLAN tags too. */
1356 		*etype = ntohs(evh->evl_proto);
1357 		offset = sizeof(struct ether_vlan_header);
1358 	} else {
1359 		*etype = ntohs(evh->evl_encap_proto);
1360 		offset = sizeof(struct ether_header);
1361 	}
1362 
1363 	switch (*etype) {
1364 #if defined(INET)
1365 	case ETHERTYPE_IP: {
1366 		struct ip *ip, iphdr;
1367 		if (__predict_false(m->m_len < offset + sizeof(struct ip))) {
1368 			m_copydata(m, offset, sizeof(struct ip),
1369 			    (caddr_t) &iphdr);
1370 			ip = &iphdr;
1371 		} else
1372 			ip = (struct ip *)(m->m_data + offset);
1373 		*proto = ip->ip_p;
1374 		*start = offset + (ip->ip_hl << 2);
1375 		break;
1376 	}
1377 #endif
1378 #if defined(INET6)
1379 	case ETHERTYPE_IPV6:
1380 		*proto = -1;
1381 		*start = ip6_lasthdr(m, offset, IPPROTO_IPV6, proto);
1382 		/* Assert the network stack sent us a valid packet. */
1383 		KASSERT(*start > offset,
1384 		    ("%s: mbuf %p start %d offset %d proto %d", __func__, m,
1385 		    *start, offset, *proto));
1386 		break;
1387 #endif
1388 	default:
1389 		/* Here we should increment the tx_csum_bad_ethtype counter. */
1390 		return (EINVAL);
1391 	}
1392 
1393 	return (0);
1394 }
1395 
1396 static int
1397 ptnet_tx_offload_tso(if_t ifp, struct mbuf *m, int eth_type,
1398 		     int offset, bool allow_ecn, struct virtio_net_hdr *hdr)
1399 {
1400 	static struct timeval lastecn;
1401 	static int curecn;
1402 	struct tcphdr *tcp, tcphdr;
1403 
1404 	if (__predict_false(m->m_len < offset + sizeof(struct tcphdr))) {
1405 		m_copydata(m, offset, sizeof(struct tcphdr), (caddr_t) &tcphdr);
1406 		tcp = &tcphdr;
1407 	} else
1408 		tcp = (struct tcphdr *)(m->m_data + offset);
1409 
1410 	hdr->hdr_len = offset + (tcp->th_off << 2);
1411 	hdr->gso_size = m->m_pkthdr.tso_segsz;
1412 	hdr->gso_type = eth_type == ETHERTYPE_IP ? VIRTIO_NET_HDR_GSO_TCPV4 :
1413 	    VIRTIO_NET_HDR_GSO_TCPV6;
1414 
1415 	if (tcp->th_flags & TH_CWR) {
1416 		/*
1417 		 * Drop if VIRTIO_NET_F_HOST_ECN was not negotiated. In FreeBSD,
1418 		 * ECN support is not on a per-interface basis, but globally via
1419 		 * the net.inet.tcp.ecn.enable sysctl knob. The default is off.
1420 		 */
1421 		if (!allow_ecn) {
1422 			if (ppsratecheck(&lastecn, &curecn, 1))
1423 				if_printf(ifp,
1424 				    "TSO with ECN not negotiated with host\n");
1425 			return (ENOTSUP);
1426 		}
1427 		hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1428 	}
1429 
1430 	/* Here we should increment tx_tso counter. */
1431 
1432 	return (0);
1433 }
1434 
1435 static struct mbuf *
1436 ptnet_tx_offload(if_t ifp, struct mbuf *m, bool allow_ecn,
1437 		 struct virtio_net_hdr *hdr)
1438 {
1439 	int flags, etype, csum_start, proto, error;
1440 
1441 	flags = m->m_pkthdr.csum_flags;
1442 
1443 	error = ptnet_tx_offload_ctx(m, &etype, &proto, &csum_start);
1444 	if (error)
1445 		goto drop;
1446 
1447 	if ((etype == ETHERTYPE_IP && flags & PTNET_CSUM_OFFLOAD) ||
1448 	    (etype == ETHERTYPE_IPV6 && flags & PTNET_CSUM_OFFLOAD_IPV6)) {
1449 		/*
1450 		 * We could compare the IP protocol vs the CSUM_ flag too,
1451 		 * but that really should not be necessary.
1452 		 */
1453 		hdr->flags |= VIRTIO_NET_HDR_F_NEEDS_CSUM;
1454 		hdr->csum_start = csum_start;
1455 		hdr->csum_offset = m->m_pkthdr.csum_data;
1456 		/* Here we should increment the tx_csum counter. */
1457 	}
1458 
1459 	if (flags & CSUM_TSO) {
1460 		if (__predict_false(proto != IPPROTO_TCP)) {
1461 			/* Likely failed to correctly parse the mbuf.
1462 			 * Here we should increment the tx_tso_not_tcp
1463 			 * counter. */
1464 			goto drop;
1465 		}
1466 
1467 		KASSERT(hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM,
1468 		    ("%s: mbuf %p TSO without checksum offload %#x",
1469 		    __func__, m, flags));
1470 
1471 		error = ptnet_tx_offload_tso(ifp, m, etype, csum_start,
1472 					     allow_ecn, hdr);
1473 		if (error)
1474 			goto drop;
1475 	}
1476 
1477 	return (m);
1478 
1479 drop:
1480 	m_freem(m);
1481 	return (NULL);
1482 }
1483 
1484 static void
1485 ptnet_vlan_tag_remove(struct mbuf *m)
1486 {
1487 	struct ether_vlan_header *evh;
1488 
1489 	evh = mtod(m, struct ether_vlan_header *);
1490 	m->m_pkthdr.ether_vtag = ntohs(evh->evl_tag);
1491 	m->m_flags |= M_VLANTAG;
1492 
1493 	/* Strip the 802.1Q header. */
1494 	bcopy((char *) evh, (char *) evh + ETHER_VLAN_ENCAP_LEN,
1495 	    ETHER_HDR_LEN - ETHER_TYPE_LEN);
1496 	m_adj(m, ETHER_VLAN_ENCAP_LEN);
1497 }
1498 
1499 /*
1500  * Use the checksum offset in the VirtIO header to set the
1501  * correct CSUM_* flags.
1502  */
1503 static int
1504 ptnet_rx_csum_by_offset(struct mbuf *m, uint16_t eth_type, int ip_start,
1505 			struct virtio_net_hdr *hdr)
1506 {
1507 #if defined(INET) || defined(INET6)
1508 	int offset = hdr->csum_start + hdr->csum_offset;
1509 #endif
1510 
1511 	/* Only do a basic sanity check on the offset. */
1512 	switch (eth_type) {
1513 #if defined(INET)
1514 	case ETHERTYPE_IP:
1515 		if (__predict_false(offset < ip_start + sizeof(struct ip)))
1516 			return (1);
1517 		break;
1518 #endif
1519 #if defined(INET6)
1520 	case ETHERTYPE_IPV6:
1521 		if (__predict_false(offset < ip_start + sizeof(struct ip6_hdr)))
1522 			return (1);
1523 		break;
1524 #endif
1525 	default:
1526 		/* Here we should increment the rx_csum_bad_ethtype counter. */
1527 		return (1);
1528 	}
1529 
1530 	/*
1531 	 * Use the offset to determine the appropriate CSUM_* flags. This is
1532 	 * a bit dirty, but we can get by with it since the checksum offsets
1533 	 * happen to be different. We assume the host host does not do IPv4
1534 	 * header checksum offloading.
1535 	 */
1536 	switch (hdr->csum_offset) {
1537 	case offsetof(struct udphdr, uh_sum):
1538 	case offsetof(struct tcphdr, th_sum):
1539 		m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1540 		m->m_pkthdr.csum_data = 0xFFFF;
1541 		break;
1542 	case offsetof(struct sctphdr, checksum):
1543 		m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1544 		break;
1545 	default:
1546 		/* Here we should increment the rx_csum_bad_offset counter. */
1547 		return (1);
1548 	}
1549 
1550 	return (0);
1551 }
1552 
1553 static int
1554 ptnet_rx_csum_by_parse(struct mbuf *m, uint16_t eth_type, int ip_start,
1555 		       struct virtio_net_hdr *hdr)
1556 {
1557 	int offset, proto;
1558 
1559 	switch (eth_type) {
1560 #if defined(INET)
1561 	case ETHERTYPE_IP: {
1562 		struct ip *ip;
1563 		if (__predict_false(m->m_len < ip_start + sizeof(struct ip)))
1564 			return (1);
1565 		ip = (struct ip *)(m->m_data + ip_start);
1566 		proto = ip->ip_p;
1567 		offset = ip_start + (ip->ip_hl << 2);
1568 		break;
1569 	}
1570 #endif
1571 #if defined(INET6)
1572 	case ETHERTYPE_IPV6:
1573 		if (__predict_false(m->m_len < ip_start +
1574 		    sizeof(struct ip6_hdr)))
1575 			return (1);
1576 		offset = ip6_lasthdr(m, ip_start, IPPROTO_IPV6, &proto);
1577 		if (__predict_false(offset < 0))
1578 			return (1);
1579 		break;
1580 #endif
1581 	default:
1582 		/* Here we should increment the rx_csum_bad_ethtype counter. */
1583 		return (1);
1584 	}
1585 
1586 	switch (proto) {
1587 	case IPPROTO_TCP:
1588 		if (__predict_false(m->m_len < offset + sizeof(struct tcphdr)))
1589 			return (1);
1590 		m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1591 		m->m_pkthdr.csum_data = 0xFFFF;
1592 		break;
1593 	case IPPROTO_UDP:
1594 		if (__predict_false(m->m_len < offset + sizeof(struct udphdr)))
1595 			return (1);
1596 		m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1597 		m->m_pkthdr.csum_data = 0xFFFF;
1598 		break;
1599 	case IPPROTO_SCTP:
1600 		if (__predict_false(m->m_len < offset + sizeof(struct sctphdr)))
1601 			return (1);
1602 		m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1603 		break;
1604 	default:
1605 		/*
1606 		 * For the remaining protocols, FreeBSD does not support
1607 		 * checksum offloading, so the checksum will be recomputed.
1608 		 */
1609 #if 0
1610 		if_printf(ifp, "cksum offload of unsupported "
1611 		    "protocol eth_type=%#x proto=%d csum_start=%d "
1612 		    "csum_offset=%d\n", __func__, eth_type, proto,
1613 		    hdr->csum_start, hdr->csum_offset);
1614 #endif
1615 		break;
1616 	}
1617 
1618 	return (0);
1619 }
1620 
1621 /*
1622  * Set the appropriate CSUM_* flags. Unfortunately, the information
1623  * provided is not directly useful to us. The VirtIO header gives the
1624  * offset of the checksum, which is all Linux needs, but this is not
1625  * how FreeBSD does things. We are forced to peek inside the packet
1626  * a bit.
1627  *
1628  * It would be nice if VirtIO gave us the L4 protocol or if FreeBSD
1629  * could accept the offsets and let the stack figure it out.
1630  */
1631 static int
1632 ptnet_rx_csum(struct mbuf *m, struct virtio_net_hdr *hdr)
1633 {
1634 	struct ether_header *eh;
1635 	struct ether_vlan_header *evh;
1636 	uint16_t eth_type;
1637 	int offset, error;
1638 
1639 	eh = mtod(m, struct ether_header *);
1640 	eth_type = ntohs(eh->ether_type);
1641 	if (eth_type == ETHERTYPE_VLAN) {
1642 		/* BMV: We should handle nested VLAN tags too. */
1643 		evh = mtod(m, struct ether_vlan_header *);
1644 		eth_type = ntohs(evh->evl_proto);
1645 		offset = sizeof(struct ether_vlan_header);
1646 	} else
1647 		offset = sizeof(struct ether_header);
1648 
1649 	if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
1650 		error = ptnet_rx_csum_by_offset(m, eth_type, offset, hdr);
1651 	else
1652 		error = ptnet_rx_csum_by_parse(m, eth_type, offset, hdr);
1653 
1654 	return (error);
1655 }
1656 /* End of offloading-related functions to be shared with vtnet. */
1657 
1658 static void
1659 ptnet_ring_update(struct ptnet_queue *pq, struct netmap_kring *kring,
1660 		  unsigned int head, unsigned int sync_flags)
1661 {
1662 	struct netmap_ring *ring = kring->ring;
1663 	struct nm_csb_atok *atok = pq->atok;
1664 	struct nm_csb_ktoa *ktoa = pq->ktoa;
1665 
1666 	/* Some packets have been pushed to the netmap ring. We have
1667 	 * to tell the host to process the new packets, updating cur
1668 	 * and head in the CSB. */
1669 	ring->head = ring->cur = head;
1670 
1671 	/* Mimic nm_txsync_prologue/nm_rxsync_prologue. */
1672 	kring->rcur = kring->rhead = head;
1673 
1674 	nm_sync_kloop_appl_write(atok, kring->rcur, kring->rhead);
1675 
1676 	/* Kick the host if needed. */
1677 	if (NM_ACCESS_ONCE(ktoa->kern_need_kick)) {
1678 		atok->sync_flags = sync_flags;
1679 		ptnet_kick(pq);
1680 	}
1681 }
1682 
1683 #define PTNET_TX_NOSPACE(_h, _k, _min)	\
1684 	((((_h) < (_k)->rtail) ? 0 : (_k)->nkr_num_slots) + \
1685 		(_k)->rtail - (_h)) < (_min)
1686 
1687 /* This function may be called by the network stack, or by
1688  * by the taskqueue thread. */
1689 static int
1690 ptnet_drain_transmit_queue(struct ptnet_queue *pq, unsigned int budget,
1691 			   bool may_resched)
1692 {
1693 	struct ptnet_softc *sc = pq->sc;
1694 	bool have_vnet_hdr = sc->vnet_hdr_len;
1695 	struct netmap_adapter *na = &sc->ptna->dr.up;
1696 	if_t ifp = sc->ifp;
1697 	unsigned int batch_count = 0;
1698 	struct nm_csb_atok *atok;
1699 	struct nm_csb_ktoa *ktoa;
1700 	struct netmap_kring *kring;
1701 	struct netmap_ring *ring;
1702 	struct netmap_slot *slot;
1703 	unsigned int count = 0;
1704 	unsigned int minspace;
1705 	unsigned int head;
1706 	unsigned int lim;
1707 	struct mbuf *mhead;
1708 	struct mbuf *mf;
1709 	int nmbuf_bytes;
1710 	uint8_t *nmbuf;
1711 
1712 	if (!PTNET_Q_TRYLOCK(pq)) {
1713 		/* We failed to acquire the lock, schedule the taskqueue. */
1714 		nm_prlim(1, "Deferring TX work");
1715 		if (may_resched) {
1716 			taskqueue_enqueue(pq->taskq, &pq->task);
1717 		}
1718 
1719 		return 0;
1720 	}
1721 
1722 	if (unlikely(!(ifp->if_drv_flags & IFF_DRV_RUNNING))) {
1723 		PTNET_Q_UNLOCK(pq);
1724 		nm_prlim(1, "Interface is down");
1725 		return ENETDOWN;
1726 	}
1727 
1728 	atok = pq->atok;
1729 	ktoa = pq->ktoa;
1730 	kring = na->tx_rings[pq->kring_id];
1731 	ring = kring->ring;
1732 	lim = kring->nkr_num_slots - 1;
1733 	head = ring->head;
1734 	minspace = sc->min_tx_space;
1735 
1736 	while (count < budget) {
1737 		if (PTNET_TX_NOSPACE(head, kring, minspace)) {
1738 			/* We ran out of slot, let's see if the host has
1739 			 * freed up some, by reading hwcur and hwtail from
1740 			 * the CSB. */
1741 			ptnet_sync_tail(ktoa, kring);
1742 
1743 			if (PTNET_TX_NOSPACE(head, kring, minspace)) {
1744 				/* Still no slots available. Reactivate the
1745 				 * interrupts so that we can be notified
1746 				 * when some free slots are made available by
1747 				 * the host. */
1748 				atok->appl_need_kick = 1;
1749 
1750 				/* Double check. We need a full barrier to
1751 				 * prevent the store to atok->appl_need_kick
1752 				 * to be reordered with the load from
1753 				 * ktoa->hwcur and ktoa->hwtail (store-load
1754 				 * barrier). */
1755 				nm_stld_barrier();
1756 				ptnet_sync_tail(ktoa, kring);
1757 				if (likely(PTNET_TX_NOSPACE(head, kring,
1758 							    minspace))) {
1759 					break;
1760 				}
1761 
1762 				nm_prlim(1, "Found more slots by doublecheck");
1763 				/* More slots were freed before reactivating
1764 				 * the interrupts. */
1765 				atok->appl_need_kick = 0;
1766 			}
1767 		}
1768 
1769 		mhead = drbr_peek(ifp, pq->bufring);
1770 		if (!mhead) {
1771 			break;
1772 		}
1773 
1774 		/* Initialize transmission state variables. */
1775 		slot = ring->slot + head;
1776 		nmbuf = NMB(na, slot);
1777 		nmbuf_bytes = 0;
1778 
1779 		/* If needed, prepare the virtio-net header at the beginning
1780 		 * of the first slot. */
1781 		if (have_vnet_hdr) {
1782 			struct virtio_net_hdr *vh =
1783 					(struct virtio_net_hdr *)nmbuf;
1784 
1785 			/* For performance, we could replace this memset() with
1786 			 * two 8-bytes-wide writes. */
1787 			memset(nmbuf, 0, PTNET_HDR_SIZE);
1788 			if (mhead->m_pkthdr.csum_flags & PTNET_ALL_OFFLOAD) {
1789 				mhead = ptnet_tx_offload(ifp, mhead, false,
1790 							 vh);
1791 				if (unlikely(!mhead)) {
1792 					/* Packet dropped because errors
1793 					 * occurred while preparing the vnet
1794 					 * header. Let's go ahead with the next
1795 					 * packet. */
1796 					pq->stats.errors ++;
1797 					drbr_advance(ifp, pq->bufring);
1798 					continue;
1799 				}
1800 			}
1801 			nm_prdis(1, "%s: [csum_flags %lX] vnet hdr: flags %x "
1802 			      "csum_start %u csum_ofs %u hdr_len = %u "
1803 			      "gso_size %u gso_type %x", __func__,
1804 			      mhead->m_pkthdr.csum_flags, vh->flags,
1805 			      vh->csum_start, vh->csum_offset, vh->hdr_len,
1806 			      vh->gso_size, vh->gso_type);
1807 
1808 			nmbuf += PTNET_HDR_SIZE;
1809 			nmbuf_bytes += PTNET_HDR_SIZE;
1810 		}
1811 
1812 		for (mf = mhead; mf; mf = mf->m_next) {
1813 			uint8_t *mdata = mf->m_data;
1814 			int mlen = mf->m_len;
1815 
1816 			for (;;) {
1817 				int copy = NETMAP_BUF_SIZE(na) - nmbuf_bytes;
1818 
1819 				if (mlen < copy) {
1820 					copy = mlen;
1821 				}
1822 				memcpy(nmbuf, mdata, copy);
1823 
1824 				mdata += copy;
1825 				mlen -= copy;
1826 				nmbuf += copy;
1827 				nmbuf_bytes += copy;
1828 
1829 				if (!mlen) {
1830 					break;
1831 				}
1832 
1833 				slot->len = nmbuf_bytes;
1834 				slot->flags = NS_MOREFRAG;
1835 
1836 				head = nm_next(head, lim);
1837 				KASSERT(head != ring->tail,
1838 					("Unexpectedly run out of TX space"));
1839 				slot = ring->slot + head;
1840 				nmbuf = NMB(na, slot);
1841 				nmbuf_bytes = 0;
1842 			}
1843 		}
1844 
1845 		/* Complete last slot and update head. */
1846 		slot->len = nmbuf_bytes;
1847 		slot->flags = 0;
1848 		head = nm_next(head, lim);
1849 
1850 		/* Consume the packet just processed. */
1851 		drbr_advance(ifp, pq->bufring);
1852 
1853 		/* Copy the packet to listeners. */
1854 		ETHER_BPF_MTAP(ifp, mhead);
1855 
1856 		pq->stats.packets ++;
1857 		pq->stats.bytes += mhead->m_pkthdr.len;
1858 		if (mhead->m_flags & M_MCAST) {
1859 			pq->stats.mcasts ++;
1860 		}
1861 
1862 		m_freem(mhead);
1863 
1864 		count ++;
1865 		if (++batch_count == PTNET_TX_BATCH) {
1866 			ptnet_ring_update(pq, kring, head, NAF_FORCE_RECLAIM);
1867 			batch_count = 0;
1868 		}
1869 	}
1870 
1871 	if (batch_count) {
1872 		ptnet_ring_update(pq, kring, head, NAF_FORCE_RECLAIM);
1873 	}
1874 
1875 	if (count >= budget && may_resched) {
1876 		DBG(nm_prlim(1, "out of budget: resched, %d mbufs pending\n",
1877 					drbr_inuse(ifp, pq->bufring)));
1878 		taskqueue_enqueue(pq->taskq, &pq->task);
1879 	}
1880 
1881 	PTNET_Q_UNLOCK(pq);
1882 
1883 	return count;
1884 }
1885 
1886 static int
1887 ptnet_transmit(if_t ifp, struct mbuf *m)
1888 {
1889 	struct ptnet_softc *sc = if_getsoftc(ifp);
1890 	struct ptnet_queue *pq;
1891 	unsigned int queue_idx;
1892 	int err;
1893 
1894 	DBG(device_printf(sc->dev, "transmit %p\n", m));
1895 
1896 	/* Insert 802.1Q header if needed. */
1897 	if (m->m_flags & M_VLANTAG) {
1898 		m = ether_vlanencap(m, m->m_pkthdr.ether_vtag);
1899 		if (m == NULL) {
1900 			return ENOBUFS;
1901 		}
1902 		m->m_flags &= ~M_VLANTAG;
1903 	}
1904 
1905 	/* Get the flow-id if available. */
1906 	queue_idx = (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) ?
1907 		    m->m_pkthdr.flowid : curcpu;
1908 
1909 	if (unlikely(queue_idx >= sc->num_tx_rings)) {
1910 		queue_idx %= sc->num_tx_rings;
1911 	}
1912 
1913 	pq = sc->queues + queue_idx;
1914 
1915 	err = drbr_enqueue(ifp, pq->bufring, m);
1916 	if (err) {
1917 		/* ENOBUFS when the bufring is full */
1918 		nm_prlim(1, "%s: drbr_enqueue() failed %d\n",
1919 			__func__, err);
1920 		pq->stats.errors ++;
1921 		return err;
1922 	}
1923 
1924 	if (ifp->if_capenable & IFCAP_POLLING) {
1925 		/* If polling is on, the transmit queues will be
1926 		 * drained by the poller. */
1927 		return 0;
1928 	}
1929 
1930 	err = ptnet_drain_transmit_queue(pq, PTNET_TX_BUDGET, true);
1931 
1932 	return (err < 0) ? err : 0;
1933 }
1934 
1935 static unsigned int
1936 ptnet_rx_discard(struct netmap_kring *kring, unsigned int head)
1937 {
1938 	struct netmap_ring *ring = kring->ring;
1939 	struct netmap_slot *slot = ring->slot + head;
1940 
1941 	for (;;) {
1942 		head = nm_next(head, kring->nkr_num_slots - 1);
1943 		if (!(slot->flags & NS_MOREFRAG) || head == ring->tail) {
1944 			break;
1945 		}
1946 		slot = ring->slot + head;
1947 	}
1948 
1949 	return head;
1950 }
1951 
1952 static inline struct mbuf *
1953 ptnet_rx_slot(struct mbuf *mtail, uint8_t *nmbuf, unsigned int nmbuf_len)
1954 {
1955 	uint8_t *mdata = mtod(mtail, uint8_t *) + mtail->m_len;
1956 
1957 	do {
1958 		unsigned int copy;
1959 
1960 		if (mtail->m_len == MCLBYTES) {
1961 			struct mbuf *mf;
1962 
1963 			mf = m_getcl(M_NOWAIT, MT_DATA, 0);
1964 			if (unlikely(!mf)) {
1965 				return NULL;
1966 			}
1967 
1968 			mtail->m_next = mf;
1969 			mtail = mf;
1970 			mdata = mtod(mtail, uint8_t *);
1971 			mtail->m_len = 0;
1972 		}
1973 
1974 		copy = MCLBYTES - mtail->m_len;
1975 		if (nmbuf_len < copy) {
1976 			copy = nmbuf_len;
1977 		}
1978 
1979 		memcpy(mdata, nmbuf, copy);
1980 
1981 		nmbuf += copy;
1982 		nmbuf_len -= copy;
1983 		mdata += copy;
1984 		mtail->m_len += copy;
1985 	} while (nmbuf_len);
1986 
1987 	return mtail;
1988 }
1989 
1990 static int
1991 ptnet_rx_eof(struct ptnet_queue *pq, unsigned int budget, bool may_resched)
1992 {
1993 	struct ptnet_softc *sc = pq->sc;
1994 	bool have_vnet_hdr = sc->vnet_hdr_len;
1995 	struct nm_csb_atok *atok = pq->atok;
1996 	struct nm_csb_ktoa *ktoa = pq->ktoa;
1997 	struct netmap_adapter *na = &sc->ptna->dr.up;
1998 	struct netmap_kring *kring = na->rx_rings[pq->kring_id];
1999 	struct netmap_ring *ring = kring->ring;
2000 	unsigned int const lim = kring->nkr_num_slots - 1;
2001 	unsigned int batch_count = 0;
2002 	if_t ifp = sc->ifp;
2003 	unsigned int count = 0;
2004 	uint32_t head;
2005 
2006 	PTNET_Q_LOCK(pq);
2007 
2008 	if (unlikely(!(ifp->if_drv_flags & IFF_DRV_RUNNING))) {
2009 		goto unlock;
2010 	}
2011 
2012 	kring->nr_kflags &= ~NKR_PENDINTR;
2013 
2014 	head = ring->head;
2015 	while (count < budget) {
2016 		uint32_t prev_head = head;
2017 		struct mbuf *mhead, *mtail;
2018 		struct virtio_net_hdr *vh;
2019 		struct netmap_slot *slot;
2020 		unsigned int nmbuf_len;
2021 		uint8_t *nmbuf;
2022 		int deliver = 1; /* the mbuf to the network stack. */
2023 host_sync:
2024 		if (head == ring->tail) {
2025 			/* We ran out of slot, let's see if the host has
2026 			 * added some, by reading hwcur and hwtail from
2027 			 * the CSB. */
2028 			ptnet_sync_tail(ktoa, kring);
2029 
2030 			if (head == ring->tail) {
2031 				/* Still no slots available. Reactivate
2032 				 * interrupts as they were disabled by the
2033 				 * host thread right before issuing the
2034 				 * last interrupt. */
2035 				atok->appl_need_kick = 1;
2036 
2037 				/* Double check for more completed RX slots.
2038 				 * We need a full barrier to prevent the store
2039 				 * to atok->appl_need_kick to be reordered with
2040 				 * the load from ktoa->hwcur and ktoa->hwtail
2041 				 * (store-load barrier). */
2042 				nm_stld_barrier();
2043 				ptnet_sync_tail(ktoa, kring);
2044 				if (likely(head == ring->tail)) {
2045 					break;
2046 				}
2047 				atok->appl_need_kick = 0;
2048 			}
2049 		}
2050 
2051 		/* Initialize ring state variables, possibly grabbing the
2052 		 * virtio-net header. */
2053 		slot = ring->slot + head;
2054 		nmbuf = NMB(na, slot);
2055 		nmbuf_len = slot->len;
2056 
2057 		vh = (struct virtio_net_hdr *)nmbuf;
2058 		if (have_vnet_hdr) {
2059 			if (unlikely(nmbuf_len < PTNET_HDR_SIZE)) {
2060 				/* There is no good reason why host should
2061 				 * put the header in multiple netmap slots.
2062 				 * If this is the case, discard. */
2063 				nm_prlim(1, "Fragmented vnet-hdr: dropping");
2064 				head = ptnet_rx_discard(kring, head);
2065 				pq->stats.iqdrops ++;
2066 				deliver = 0;
2067 				goto skip;
2068 			}
2069 			nm_prdis(1, "%s: vnet hdr: flags %x csum_start %u "
2070 			      "csum_ofs %u hdr_len = %u gso_size %u "
2071 			      "gso_type %x", __func__, vh->flags,
2072 			      vh->csum_start, vh->csum_offset, vh->hdr_len,
2073 			      vh->gso_size, vh->gso_type);
2074 			nmbuf += PTNET_HDR_SIZE;
2075 			nmbuf_len -= PTNET_HDR_SIZE;
2076 		}
2077 
2078 		/* Allocate the head of a new mbuf chain.
2079 		 * We use m_getcl() to allocate an mbuf with standard cluster
2080 		 * size (MCLBYTES). In the future we could use m_getjcl()
2081 		 * to choose different sizes. */
2082 		mhead = mtail = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2083 		if (unlikely(mhead == NULL)) {
2084 			device_printf(sc->dev, "%s: failed to allocate mbuf "
2085 				      "head\n", __func__);
2086 			pq->stats.errors ++;
2087 			break;
2088 		}
2089 
2090 		/* Initialize the mbuf state variables. */
2091 		mhead->m_pkthdr.len = nmbuf_len;
2092 		mtail->m_len = 0;
2093 
2094 		/* Scan all the netmap slots containing the current packet. */
2095 		for (;;) {
2096 			DBG(device_printf(sc->dev, "%s: h %u t %u rcv frag "
2097 					  "len %u, flags %u\n", __func__,
2098 					  head, ring->tail, slot->len,
2099 					  slot->flags));
2100 
2101 			mtail = ptnet_rx_slot(mtail, nmbuf, nmbuf_len);
2102 			if (unlikely(!mtail)) {
2103 				/* Ouch. We ran out of memory while processing
2104 				 * a packet. We have to restore the previous
2105 				 * head position, free the mbuf chain, and
2106 				 * schedule the taskqueue to give the packet
2107 				 * another chance. */
2108 				device_printf(sc->dev, "%s: failed to allocate"
2109 					" mbuf frag, reset head %u --> %u\n",
2110 					__func__, head, prev_head);
2111 				head = prev_head;
2112 				m_freem(mhead);
2113 				pq->stats.errors ++;
2114 				if (may_resched) {
2115 					taskqueue_enqueue(pq->taskq,
2116 							  &pq->task);
2117 				}
2118 				goto escape;
2119 			}
2120 
2121 			/* We have to increment head irrespective of the
2122 			 * NS_MOREFRAG being set or not. */
2123 			head = nm_next(head, lim);
2124 
2125 			if (!(slot->flags & NS_MOREFRAG)) {
2126 				break;
2127 			}
2128 
2129 			if (unlikely(head == ring->tail)) {
2130 				/* The very last slot prepared by the host has
2131 				 * the NS_MOREFRAG set. Drop it and continue
2132 				 * the outer cycle (to do the double-check). */
2133 				nm_prlim(1, "Incomplete packet: dropping");
2134 				m_freem(mhead);
2135 				pq->stats.iqdrops ++;
2136 				goto host_sync;
2137 			}
2138 
2139 			slot = ring->slot + head;
2140 			nmbuf = NMB(na, slot);
2141 			nmbuf_len = slot->len;
2142 			mhead->m_pkthdr.len += nmbuf_len;
2143 		}
2144 
2145 		mhead->m_pkthdr.rcvif = ifp;
2146 		mhead->m_pkthdr.csum_flags = 0;
2147 
2148 		/* Store the queue idx in the packet header. */
2149 		mhead->m_pkthdr.flowid = pq->kring_id;
2150 		M_HASHTYPE_SET(mhead, M_HASHTYPE_OPAQUE);
2151 
2152 		if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) {
2153 			struct ether_header *eh;
2154 
2155 			eh = mtod(mhead, struct ether_header *);
2156 			if (eh->ether_type == htons(ETHERTYPE_VLAN)) {
2157 				ptnet_vlan_tag_remove(mhead);
2158 				/*
2159 				 * With the 802.1Q header removed, update the
2160 				 * checksum starting location accordingly.
2161 				 */
2162 				if (vh->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
2163 					vh->csum_start -= ETHER_VLAN_ENCAP_LEN;
2164 			}
2165 		}
2166 
2167 		if (have_vnet_hdr && (vh->flags & (VIRTIO_NET_HDR_F_NEEDS_CSUM
2168 					| VIRTIO_NET_HDR_F_DATA_VALID))) {
2169 			if (unlikely(ptnet_rx_csum(mhead, vh))) {
2170 				m_freem(mhead);
2171 				nm_prlim(1, "Csum offload error: dropping");
2172 				pq->stats.iqdrops ++;
2173 				deliver = 0;
2174 			}
2175 		}
2176 
2177 skip:
2178 		count ++;
2179 		if (++batch_count >= PTNET_RX_BATCH) {
2180 			/* Some packets have been (or will be) pushed to the network
2181 			 * stack. We need to update the CSB to tell the host about
2182 			 * the new ring->cur and ring->head (RX buffer refill). */
2183 			ptnet_ring_update(pq, kring, head, NAF_FORCE_READ);
2184 			batch_count = 0;
2185 		}
2186 
2187 		if (likely(deliver))  {
2188 			pq->stats.packets ++;
2189 			pq->stats.bytes += mhead->m_pkthdr.len;
2190 
2191 			PTNET_Q_UNLOCK(pq);
2192 			(*ifp->if_input)(ifp, mhead);
2193 			PTNET_Q_LOCK(pq);
2194 			/* The ring->head index (and related indices) are
2195 			 * updated under pq lock by ptnet_ring_update().
2196 			 * Since we dropped the lock to call if_input(), we
2197 			 * must reload ring->head and restart processing the
2198 			 * ring from there. */
2199 			head = ring->head;
2200 
2201 			if (unlikely(!(ifp->if_drv_flags & IFF_DRV_RUNNING))) {
2202 				/* The interface has gone down while we didn't
2203 				 * have the lock. Stop any processing and exit. */
2204 				goto unlock;
2205 			}
2206 		}
2207 	}
2208 escape:
2209 	if (batch_count) {
2210 		ptnet_ring_update(pq, kring, head, NAF_FORCE_READ);
2211 
2212 	}
2213 
2214 	if (count >= budget && may_resched) {
2215 		/* If we ran out of budget or the double-check found new
2216 		 * slots to process, schedule the taskqueue. */
2217 		DBG(nm_prlim(1, "out of budget: resched h %u t %u\n",
2218 					head, ring->tail));
2219 		taskqueue_enqueue(pq->taskq, &pq->task);
2220 	}
2221 unlock:
2222 	PTNET_Q_UNLOCK(pq);
2223 
2224 	return count;
2225 }
2226 
2227 static void
2228 ptnet_rx_task(void *context, int pending)
2229 {
2230 	struct ptnet_queue *pq = context;
2231 
2232 	DBG(nm_prlim(1, "%s: pq #%u\n", __func__, pq->kring_id));
2233 	ptnet_rx_eof(pq, PTNET_RX_BUDGET, true);
2234 }
2235 
2236 static void
2237 ptnet_tx_task(void *context, int pending)
2238 {
2239 	struct ptnet_queue *pq = context;
2240 
2241 	DBG(nm_prlim(1, "%s: pq #%u\n", __func__, pq->kring_id));
2242 	ptnet_drain_transmit_queue(pq, PTNET_TX_BUDGET, true);
2243 }
2244 
2245 #ifdef DEVICE_POLLING
2246 /* We don't need to handle differently POLL_AND_CHECK_STATUS and
2247  * POLL_ONLY, since we don't have an Interrupt Status Register. */
2248 static int
2249 ptnet_poll(if_t ifp, enum poll_cmd cmd, int budget)
2250 {
2251 	struct ptnet_softc *sc = if_getsoftc(ifp);
2252 	unsigned int queue_budget;
2253 	unsigned int count = 0;
2254 	bool borrow = false;
2255 	int i;
2256 
2257 	KASSERT(sc->num_rings > 0, ("Found no queues in while polling ptnet"));
2258 	queue_budget = MAX(budget / sc->num_rings, 1);
2259 	nm_prlim(1, "Per-queue budget is %d", queue_budget);
2260 
2261 	while (budget) {
2262 		unsigned int rcnt = 0;
2263 
2264 		for (i = 0; i < sc->num_rings; i++) {
2265 			struct ptnet_queue *pq = sc->queues + i;
2266 
2267 			if (borrow) {
2268 				queue_budget = MIN(queue_budget, budget);
2269 				if (queue_budget == 0) {
2270 					break;
2271 				}
2272 			}
2273 
2274 			if (i < sc->num_tx_rings) {
2275 				rcnt += ptnet_drain_transmit_queue(pq,
2276 						   queue_budget, false);
2277 			} else {
2278 				rcnt += ptnet_rx_eof(pq, queue_budget,
2279 						      false);
2280 			}
2281 		}
2282 
2283 		if (!rcnt) {
2284 			/* A scan of the queues gave no result, we can
2285 			 * stop here. */
2286 			break;
2287 		}
2288 
2289 		if (rcnt > budget) {
2290 			/* This may happen when initial budget < sc->num_rings,
2291 			 * since one packet budget is given to each queue
2292 			 * anyway. Just pretend we didn't eat "so much". */
2293 			rcnt = budget;
2294 		}
2295 		count += rcnt;
2296 		budget -= rcnt;
2297 		borrow = true;
2298 	}
2299 
2300 
2301 	return count;
2302 }
2303 #endif /* DEVICE_POLLING */
2304