1bf21cd93STycho Nightingale /*-
2*32640292SAndy Fiddaman  * SPDX-License-Identifier: BSD-2-Clause
34c87aefeSPatrick Mooney  *
4bf21cd93STycho Nightingale  * Copyright (c) 2011 NetApp, Inc.
5bf21cd93STycho Nightingale  * All rights reserved.
6bf21cd93STycho Nightingale  *
7bf21cd93STycho Nightingale  * Redistribution and use in source and binary forms, with or without
8bf21cd93STycho Nightingale  * modification, are permitted provided that the following conditions
9bf21cd93STycho Nightingale  * are met:
10bf21cd93STycho Nightingale  * 1. Redistributions of source code must retain the above copyright
11bf21cd93STycho Nightingale  *    notice, this list of conditions and the following disclaimer.
12bf21cd93STycho Nightingale  * 2. Redistributions in binary form must reproduce the above copyright
13bf21cd93STycho Nightingale  *    notice, this list of conditions and the following disclaimer in the
14bf21cd93STycho Nightingale  *    documentation and/or other materials provided with the distribution.
15bf21cd93STycho Nightingale  *
16bf21cd93STycho Nightingale  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17bf21cd93STycho Nightingale  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18bf21cd93STycho Nightingale  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19bf21cd93STycho Nightingale  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20bf21cd93STycho Nightingale  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21bf21cd93STycho Nightingale  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22bf21cd93STycho Nightingale  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23bf21cd93STycho Nightingale  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24bf21cd93STycho Nightingale  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25bf21cd93STycho Nightingale  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26bf21cd93STycho Nightingale  * SUCH DAMAGE.
27bf21cd93STycho Nightingale  */
28bf21cd93STycho Nightingale 
29bf21cd93STycho Nightingale #include <sys/cdefs.h>
30bf21cd93STycho Nightingale 
31bf21cd93STycho Nightingale #include <sys/param.h>
32bf21cd93STycho Nightingale #include <sys/linker_set.h>
33bf21cd93STycho Nightingale #include <sys/select.h>
34bf21cd93STycho Nightingale #include <sys/uio.h>
35bf21cd93STycho Nightingale #include <sys/ioctl.h>
36bf21cd93STycho Nightingale #include <net/ethernet.h>
37069b2ef0SAndy Fiddaman #include <net/if.h> /* IFNAMSIZ */
38bf21cd93STycho Nightingale 
394c87aefeSPatrick Mooney #include <err.h>
40bf21cd93STycho Nightingale #include <errno.h>
41bf21cd93STycho Nightingale #include <fcntl.h>
42bf21cd93STycho Nightingale #include <stdio.h>
43bf21cd93STycho Nightingale #include <stdlib.h>
44bf21cd93STycho Nightingale #include <stdint.h>
45bf21cd93STycho Nightingale #include <string.h>
46bf21cd93STycho Nightingale #include <strings.h>
47bf21cd93STycho Nightingale #include <unistd.h>
48bf21cd93STycho Nightingale #include <assert.h>
49bf21cd93STycho Nightingale #include <pthread.h>
50bf21cd93STycho Nightingale #include <pthread_np.h>
51bf21cd93STycho Nightingale 
52bf21cd93STycho Nightingale #include "bhyverun.h"
532b948146SAndy Fiddaman #include "config.h"
54154972afSPatrick Mooney #include "debug.h"
55bf21cd93STycho Nightingale #include "pci_emul.h"
56bf21cd93STycho Nightingale #include "mevent.h"
57bf21cd93STycho Nightingale #include "virtio.h"
5884659b24SMichael Zeller #include "net_utils.h"
59069b2ef0SAndy Fiddaman #include "net_backends.h"
60069b2ef0SAndy Fiddaman #include "iov.h"
61bf21cd93STycho Nightingale 
62bf21cd93STycho Nightingale #define VTNET_RINGSZ	1024
63bf21cd93STycho Nightingale 
644c87aefeSPatrick Mooney #define VTNET_MAXSEGS	256
65bf21cd93STycho Nightingale 
66069b2ef0SAndy Fiddaman #define VTNET_MAX_PKT_LEN	(65536 + 64)
67069b2ef0SAndy Fiddaman 
68069b2ef0SAndy Fiddaman #define VTNET_MIN_MTU	ETHERMIN
69069b2ef0SAndy Fiddaman #define VTNET_MAX_MTU	65535
70bf21cd93STycho Nightingale 
71bf21cd93STycho Nightingale #define VTNET_S_HOSTCAPS      \
72069b2ef0SAndy Fiddaman   ( VIRTIO_NET_F_MAC | VIRTIO_NET_F_STATUS | \
734c87aefeSPatrick Mooney     VIRTIO_F_NOTIFY_ON_EMPTY | VIRTIO_RING_F_INDIRECT_DESC)
74bf21cd93STycho Nightingale 
75bf21cd93STycho Nightingale /*
76bf21cd93STycho Nightingale  * PCI config-space "registers"
77bf21cd93STycho Nightingale  */
78bf21cd93STycho Nightingale struct virtio_net_config {
79bf21cd93STycho Nightingale 	uint8_t  mac[6];
80bf21cd93STycho Nightingale 	uint16_t status;
81154972afSPatrick Mooney 	uint16_t max_virtqueue_pairs;
82154972afSPatrick Mooney 	uint16_t mtu;
83bf21cd93STycho Nightingale } __packed;
84bf21cd93STycho Nightingale 
85bf21cd93STycho Nightingale /*
86bf21cd93STycho Nightingale  * Queue definitions.
87bf21cd93STycho Nightingale  */
88bf21cd93STycho Nightingale #define VTNET_RXQ	0
89bf21cd93STycho Nightingale #define VTNET_TXQ	1
90bf21cd93STycho Nightingale #define VTNET_CTLQ	2	/* NB: not yet supported */
91bf21cd93STycho Nightingale 
92bf21cd93STycho Nightingale #define VTNET_MAXQ	3
93bf21cd93STycho Nightingale 
94bf21cd93STycho Nightingale /*
95bf21cd93STycho Nightingale  * Debug printf
96bf21cd93STycho Nightingale  */
97bf21cd93STycho Nightingale static int pci_vtnet_debug;
98154972afSPatrick Mooney #define DPRINTF(params) if (pci_vtnet_debug) PRINTLN params
99154972afSPatrick Mooney #define WPRINTF(params) PRINTLN params
100bf21cd93STycho Nightingale 
101bf21cd93STycho Nightingale /*
102bf21cd93STycho Nightingale  * Per-device softc
103bf21cd93STycho Nightingale  */
104bf21cd93STycho Nightingale struct pci_vtnet_softc {
105bf21cd93STycho Nightingale 	struct virtio_softc vsc_vs;
106bf21cd93STycho Nightingale 	struct vqueue_info vsc_queues[VTNET_MAXQ - 1];
107bf21cd93STycho Nightingale 	pthread_mutex_t vsc_mtx;
108bf21cd93STycho Nightingale 
109069b2ef0SAndy Fiddaman 	net_backend_t	*vsc_be;
110069b2ef0SAndy Fiddaman 
111069b2ef0SAndy Fiddaman 	bool    features_negotiated;	/* protected by rx_mtx */
1124c87aefeSPatrick Mooney 
11384659b24SMichael Zeller 	int		resetting;	/* protected by tx_mtx */
114bf21cd93STycho Nightingale 
1154c87aefeSPatrick Mooney 	uint64_t	vsc_features;	/* negotiated features */
1166dc98349SAndy Fiddaman 
117bf21cd93STycho Nightingale 	pthread_mutex_t	rx_mtx;
1184c87aefeSPatrick Mooney 	int		rx_merge;	/* merged rx bufs in use */
119bf21cd93STycho Nightingale 
120bf21cd93STycho Nightingale 	pthread_t 	tx_tid;
121bf21cd93STycho Nightingale 	pthread_mutex_t	tx_mtx;
122bf21cd93STycho Nightingale 	pthread_cond_t	tx_cond;
123bf21cd93STycho Nightingale 	int		tx_in_progress;
1244c87aefeSPatrick Mooney 
125069b2ef0SAndy Fiddaman 	size_t		vhdrlen;
126069b2ef0SAndy Fiddaman 	size_t		be_vhdrlen;
127069b2ef0SAndy Fiddaman 
128069b2ef0SAndy Fiddaman 	struct virtio_net_config vsc_config;
129069b2ef0SAndy Fiddaman 	struct virtio_consts vsc_consts;
130bf21cd93STycho Nightingale };
131bf21cd93STycho Nightingale 
132bf21cd93STycho Nightingale static void pci_vtnet_reset(void *);
133bf21cd93STycho Nightingale /* static void pci_vtnet_notify(void *, struct vqueue_info *); */
134bf21cd93STycho Nightingale static int pci_vtnet_cfgread(void *, int, int, uint32_t *);
135bf21cd93STycho Nightingale static int pci_vtnet_cfgwrite(void *, int, int, uint32_t);
1364c87aefeSPatrick Mooney static void pci_vtnet_neg_features(void *, uint64_t);
137bf21cd93STycho Nightingale 
138bf21cd93STycho Nightingale static struct virtio_consts vtnet_vi_consts = {
13959d65d31SAndy Fiddaman 	.vc_name =	"vtnet",
14059d65d31SAndy Fiddaman 	.vc_nvq =	VTNET_MAXQ - 1,
14159d65d31SAndy Fiddaman 	.vc_cfgsize =	sizeof(struct virtio_net_config),
14259d65d31SAndy Fiddaman 	.vc_reset =	pci_vtnet_reset,
14359d65d31SAndy Fiddaman 	.vc_cfgread =	pci_vtnet_cfgread,
14459d65d31SAndy Fiddaman 	.vc_cfgwrite =	pci_vtnet_cfgwrite,
14559d65d31SAndy Fiddaman 	.vc_apply_features = pci_vtnet_neg_features,
14659d65d31SAndy Fiddaman 	.vc_hv_caps =	VTNET_S_HOSTCAPS,
147bf21cd93STycho Nightingale };
148bf21cd93STycho Nightingale 
149bf21cd93STycho Nightingale static void
pci_vtnet_reset(void * vsc)15084659b24SMichael Zeller pci_vtnet_reset(void *vsc)
151bf21cd93STycho Nightingale {
15284659b24SMichael Zeller 	struct pci_vtnet_softc *sc = vsc;
15384659b24SMichael Zeller 
154154972afSPatrick Mooney 	DPRINTF(("vtnet: device reset requested !"));
15584659b24SMichael Zeller 
15684659b24SMichael Zeller 	/* Acquire the RX lock to block RX processing. */
15784659b24SMichael Zeller 	pthread_mutex_lock(&sc->rx_mtx);
158bf21cd93STycho Nightingale 
159069b2ef0SAndy Fiddaman 	/*
160069b2ef0SAndy Fiddaman 	 * Make sure receive operation is disabled at least until we
161069b2ef0SAndy Fiddaman 	 * re-negotiate the features, since receive operation depends
162069b2ef0SAndy Fiddaman 	 * on the value of sc->rx_merge and the header length, which
163069b2ef0SAndy Fiddaman 	 * are both set in pci_vtnet_neg_features().
164069b2ef0SAndy Fiddaman 	 * Receive operation will be enabled again once the guest adds
165069b2ef0SAndy Fiddaman 	 * the first receive buffers and kicks us.
166069b2ef0SAndy Fiddaman 	 */
1676960cd89SAndy Fiddaman 	sc->features_negotiated = false;
168069b2ef0SAndy Fiddaman 	netbe_rx_disable(sc->vsc_be);
1696960cd89SAndy Fiddaman 
17084659b24SMichael Zeller 	/* Set sc->resetting and give a chance to the TX thread to stop. */
171bf21cd93STycho Nightingale 	pthread_mutex_lock(&sc->tx_mtx);
17284659b24SMichael Zeller 	sc->resetting = 1;
173bf21cd93STycho Nightingale 	while (sc->tx_in_progress) {
174bf21cd93STycho Nightingale 		pthread_mutex_unlock(&sc->tx_mtx);
175bf21cd93STycho Nightingale 		usleep(10000);
176bf21cd93STycho Nightingale 		pthread_mutex_lock(&sc->tx_mtx);
177bf21cd93STycho Nightingale 	}
178bf21cd93STycho Nightingale 
17984659b24SMichael Zeller 	/*
18084659b24SMichael Zeller 	 * Now reset rings, MSI-X vectors, and negotiated capabilities.
18184659b24SMichael Zeller 	 * Do that with the TX lock held, since we need to reset
18284659b24SMichael Zeller 	 * sc->resetting.
18384659b24SMichael Zeller 	 */
184bf21cd93STycho Nightingale 	vi_reset_dev(&sc->vsc_vs);
185bf21cd93STycho Nightingale 
186bf21cd93STycho Nightingale 	sc->resetting = 0;
18784659b24SMichael Zeller 	pthread_mutex_unlock(&sc->tx_mtx);
18884659b24SMichael Zeller 	pthread_mutex_unlock(&sc->rx_mtx);
189bf21cd93STycho Nightingale }
190bf21cd93STycho Nightingale 
1914c87aefeSPatrick Mooney static __inline struct iovec *
iov_trim_hdr(struct iovec * iov,int * iovcnt,unsigned int hlen)192069b2ef0SAndy Fiddaman iov_trim_hdr(struct iovec *iov, int *iovcnt, unsigned int hlen)
1934c87aefeSPatrick Mooney {
1944c87aefeSPatrick Mooney 	struct iovec *riov;
1954c87aefeSPatrick Mooney 
196069b2ef0SAndy Fiddaman 	if (iov[0].iov_len < hlen) {
197069b2ef0SAndy Fiddaman 		/*
198069b2ef0SAndy Fiddaman 		 * Not enough header space in the first fragment.
199069b2ef0SAndy Fiddaman 		 * That's not ok for us.
200069b2ef0SAndy Fiddaman 		 */
201069b2ef0SAndy Fiddaman 		return NULL;
202069b2ef0SAndy Fiddaman 	}
2034c87aefeSPatrick Mooney 
204069b2ef0SAndy Fiddaman 	iov[0].iov_len -= hlen;
2054c87aefeSPatrick Mooney 	if (iov[0].iov_len == 0) {
206069b2ef0SAndy Fiddaman 		*iovcnt -= 1;
207069b2ef0SAndy Fiddaman 		if (*iovcnt == 0) {
208069b2ef0SAndy Fiddaman 			/*
209069b2ef0SAndy Fiddaman 			 * Only space for the header. That's not
210069b2ef0SAndy Fiddaman 			 * enough for us.
211069b2ef0SAndy Fiddaman 			 */
212069b2ef0SAndy Fiddaman 			return NULL;
213069b2ef0SAndy Fiddaman 		}
2144c87aefeSPatrick Mooney 		riov = &iov[1];
2154c87aefeSPatrick Mooney 	} else {
216069b2ef0SAndy Fiddaman 		iov[0].iov_base = (void *)((uintptr_t)iov[0].iov_base + hlen);
2174c87aefeSPatrick Mooney 		riov = &iov[0];
2184c87aefeSPatrick Mooney 	}
2194c87aefeSPatrick Mooney 
2204c87aefeSPatrick Mooney 	return (riov);
2214c87aefeSPatrick Mooney }
222bf21cd93STycho Nightingale 
223069b2ef0SAndy Fiddaman struct virtio_mrg_rxbuf_info {
224069b2ef0SAndy Fiddaman 	uint16_t idx;
225069b2ef0SAndy Fiddaman 	uint16_t pad;
226069b2ef0SAndy Fiddaman 	uint32_t len;
227069b2ef0SAndy Fiddaman };
228069b2ef0SAndy Fiddaman 
229bf21cd93STycho Nightingale static void
pci_vtnet_rx(struct pci_vtnet_softc * sc)230069b2ef0SAndy Fiddaman pci_vtnet_rx(struct pci_vtnet_softc *sc)
231bf21cd93STycho Nightingale {
232069b2ef0SAndy Fiddaman 	int prepend_hdr_len = sc->vhdrlen - sc->be_vhdrlen;
233069b2ef0SAndy Fiddaman 	struct virtio_mrg_rxbuf_info info[VTNET_MAXSEGS];
234069b2ef0SAndy Fiddaman 	struct iovec iov[VTNET_MAXSEGS + 1];
235bf21cd93STycho Nightingale 	struct vqueue_info *vq;
236b0de25cbSAndy Fiddaman 	struct vi_req req;
237bf21cd93STycho Nightingale 
238069b2ef0SAndy Fiddaman 	vq = &sc->vsc_queues[VTNET_RXQ];
239bf21cd93STycho Nightingale 
2406960cd89SAndy Fiddaman 	/* Features must be negotiated */
2416960cd89SAndy Fiddaman 	if (!sc->features_negotiated) {
2426960cd89SAndy Fiddaman 		return;
2436960cd89SAndy Fiddaman 	}
2446960cd89SAndy Fiddaman 
245069b2ef0SAndy Fiddaman 	for (;;) {
246069b2ef0SAndy Fiddaman 		struct virtio_net_rxhdr *hdr;
247069b2ef0SAndy Fiddaman 		uint32_t riov_bytes;
248069b2ef0SAndy Fiddaman 		struct iovec *riov;
249069b2ef0SAndy Fiddaman 		uint32_t ulen;
250069b2ef0SAndy Fiddaman 		int riov_len;
251069b2ef0SAndy Fiddaman 		int n_chains;
252069b2ef0SAndy Fiddaman 		ssize_t rlen;
253069b2ef0SAndy Fiddaman 		ssize_t plen;
254069b2ef0SAndy Fiddaman 
255069b2ef0SAndy Fiddaman 		plen = netbe_peek_recvlen(sc->vsc_be);
256069b2ef0SAndy Fiddaman 		if (plen <= 0) {
257069b2ef0SAndy Fiddaman 			/*
258069b2ef0SAndy Fiddaman 			 * No more packets (plen == 0), or backend errored
259069b2ef0SAndy Fiddaman 			 * (plen < 0). Interrupt if needed and stop.
260069b2ef0SAndy Fiddaman 			 */
261069b2ef0SAndy Fiddaman 			vq_endchains(vq, /*used_all_avail=*/0);
262069b2ef0SAndy Fiddaman 			return;
263069b2ef0SAndy Fiddaman 		}
264069b2ef0SAndy Fiddaman 		plen += prepend_hdr_len;
265bf21cd93STycho Nightingale 
266bf21cd93STycho Nightingale 		/*
267069b2ef0SAndy Fiddaman 		 * Get a descriptor chain to store the next ingress
268069b2ef0SAndy Fiddaman 		 * packet. In case of mergeable rx buffers, get as
269069b2ef0SAndy Fiddaman 		 * many chains as necessary in order to make room
270069b2ef0SAndy Fiddaman 		 * for plen bytes.
271bf21cd93STycho Nightingale 		 */
272069b2ef0SAndy Fiddaman 		riov_bytes = 0;
273069b2ef0SAndy Fiddaman 		riov_len = 0;
274069b2ef0SAndy Fiddaman 		riov = iov;
275069b2ef0SAndy Fiddaman 		n_chains = 0;
276069b2ef0SAndy Fiddaman 		do {
277b0de25cbSAndy Fiddaman 			int n = vq_getchain(vq, riov, VTNET_MAXSEGS - riov_len,
278b0de25cbSAndy Fiddaman 			    &req);
279b0de25cbSAndy Fiddaman 			info[n_chains].idx = req.idx;
280069b2ef0SAndy Fiddaman 
281069b2ef0SAndy Fiddaman 			if (n == 0) {
282069b2ef0SAndy Fiddaman 				/*
283069b2ef0SAndy Fiddaman 				 * No rx buffers. Enable RX kicks and double
284069b2ef0SAndy Fiddaman 				 * check.
285069b2ef0SAndy Fiddaman 				 */
286069b2ef0SAndy Fiddaman 				vq_kick_enable(vq);
287069b2ef0SAndy Fiddaman 				if (!vq_has_descs(vq)) {
288069b2ef0SAndy Fiddaman 					/*
289069b2ef0SAndy Fiddaman 					 * Still no buffers. Return the unused
290069b2ef0SAndy Fiddaman 					 * chains (if any), interrupt if needed
291069b2ef0SAndy Fiddaman 					 * (including for NOTIFY_ON_EMPTY), and
292069b2ef0SAndy Fiddaman 					 * disable the backend until the next
293069b2ef0SAndy Fiddaman 					 * kick.
294069b2ef0SAndy Fiddaman 					 */
295069b2ef0SAndy Fiddaman 					vq_retchains(vq, n_chains);
296069b2ef0SAndy Fiddaman 					vq_endchains(vq, /*used_all_avail=*/1);
297069b2ef0SAndy Fiddaman 					netbe_rx_disable(sc->vsc_be);
298069b2ef0SAndy Fiddaman 					return;
299069b2ef0SAndy Fiddaman 				}
300069b2ef0SAndy Fiddaman 
301069b2ef0SAndy Fiddaman 				/* More rx buffers found, so keep going. */
302069b2ef0SAndy Fiddaman 				vq_kick_disable(vq);
303069b2ef0SAndy Fiddaman 				continue;
304069b2ef0SAndy Fiddaman 			}
305069b2ef0SAndy Fiddaman #ifndef __FreeBSD__
306069b2ef0SAndy Fiddaman 			if (n == -1) {
307069b2ef0SAndy Fiddaman 				/*
308069b2ef0SAndy Fiddaman 				 * An error from vq_getchain() means that
309069b2ef0SAndy Fiddaman 				 * an invalid descriptor was found.
310069b2ef0SAndy Fiddaman 				 */
311069b2ef0SAndy Fiddaman 				vq_retchains(vq, n_chains);
312069b2ef0SAndy Fiddaman 				vq_endchains(vq, /*used_all_avail=*/0);
313069b2ef0SAndy Fiddaman 				return;
314069b2ef0SAndy Fiddaman 			}
315bf21cd93STycho Nightingale #endif
316069b2ef0SAndy Fiddaman 			assert(n >= 1 && riov_len + n <= VTNET_MAXSEGS);
317069b2ef0SAndy Fiddaman 			riov_len += n;
318069b2ef0SAndy Fiddaman 			if (!sc->rx_merge) {
319069b2ef0SAndy Fiddaman 				n_chains = 1;
320069b2ef0SAndy Fiddaman 				break;
321069b2ef0SAndy Fiddaman 			}
322069b2ef0SAndy Fiddaman #ifndef __FreeBSD__
323069b2ef0SAndy Fiddaman 			size_t c = count_iov(riov, n);
324069b2ef0SAndy Fiddaman 			if (c > UINT32_MAX) {
325069b2ef0SAndy Fiddaman 				vq_retchains(vq, n_chains);
326069b2ef0SAndy Fiddaman 				vq_endchains(vq, /*used_all_avail=*/0);
327069b2ef0SAndy Fiddaman 				return;
328069b2ef0SAndy Fiddaman 			}
329069b2ef0SAndy Fiddaman 			info[n_chains].len = (uint32_t)c;
330069b2ef0SAndy Fiddaman #else
331069b2ef0SAndy Fiddaman 			info[n_chains].len = (uint32_t)count_iov(riov, n);
332069b2ef0SAndy Fiddaman #endif
333069b2ef0SAndy Fiddaman 			riov_bytes += info[n_chains].len;
334069b2ef0SAndy Fiddaman 			riov += n;
335069b2ef0SAndy Fiddaman 			n_chains++;
336069b2ef0SAndy Fiddaman 		} while (riov_bytes < plen && riov_len < VTNET_MAXSEGS);
337bf21cd93STycho Nightingale 
338069b2ef0SAndy Fiddaman 		riov = iov;
339069b2ef0SAndy Fiddaman #ifdef __FreeBSD__
340069b2ef0SAndy Fiddaman 		hdr = riov[0].iov_base;
341bf21cd93STycho Nightingale #else
342069b2ef0SAndy Fiddaman 		hdr = (struct virtio_net_rxhdr *)riov[0].iov_base;
343bf21cd93STycho Nightingale #endif
344069b2ef0SAndy Fiddaman 		if (prepend_hdr_len > 0) {
3454c87aefeSPatrick Mooney 			/*
346069b2ef0SAndy Fiddaman 			 * The frontend uses a virtio-net header, but the
347069b2ef0SAndy Fiddaman 			 * backend does not. We need to prepend a zeroed
348069b2ef0SAndy Fiddaman 			 * header.
3494c87aefeSPatrick Mooney 			 */
350069b2ef0SAndy Fiddaman 			riov = iov_trim_hdr(riov, &riov_len, prepend_hdr_len);
351069b2ef0SAndy Fiddaman 			if (riov == NULL) {
352069b2ef0SAndy Fiddaman 				/*
353069b2ef0SAndy Fiddaman 				 * The first collected chain is nonsensical,
354069b2ef0SAndy Fiddaman 				 * as it is not even enough to store the
355069b2ef0SAndy Fiddaman 				 * virtio-net header. Just drop it.
356069b2ef0SAndy Fiddaman 				 */
357069b2ef0SAndy Fiddaman 				vq_relchain(vq, info[0].idx, 0);
358069b2ef0SAndy Fiddaman 				vq_retchains(vq, n_chains - 1);
359069b2ef0SAndy Fiddaman 				continue;
360069b2ef0SAndy Fiddaman 			}
361069b2ef0SAndy Fiddaman 			memset(hdr, 0, prepend_hdr_len);
3624c87aefeSPatrick Mooney 		}
3634c87aefeSPatrick Mooney 
364069b2ef0SAndy Fiddaman 		rlen = netbe_recv(sc->vsc_be, riov, riov_len);
365069b2ef0SAndy Fiddaman 		if (rlen != plen - prepend_hdr_len) {
366069b2ef0SAndy Fiddaman 			/*
367069b2ef0SAndy Fiddaman 			 * If this happens it means there is something
368069b2ef0SAndy Fiddaman 			 * wrong with the backend (e.g., some other
369069b2ef0SAndy Fiddaman 			 * process is stealing our packets).
370069b2ef0SAndy Fiddaman 			 */
371069b2ef0SAndy Fiddaman 			WPRINTF(("netbe_recv: expected %zd bytes, "
372069b2ef0SAndy Fiddaman 				"got %zd", plen - prepend_hdr_len, rlen));
373069b2ef0SAndy Fiddaman 			vq_retchains(vq, n_chains);
3744c87aefeSPatrick Mooney 			continue;
3754c87aefeSPatrick Mooney 		}
3764c87aefeSPatrick Mooney 
377069b2ef0SAndy Fiddaman 		ulen = (uint32_t)plen;
3784c87aefeSPatrick Mooney 
379069b2ef0SAndy Fiddaman 		/*
380069b2ef0SAndy Fiddaman 		 * Publish the used buffers to the guest, reporting the
381069b2ef0SAndy Fiddaman 		 * number of bytes that we wrote.
382069b2ef0SAndy Fiddaman 		 */
383069b2ef0SAndy Fiddaman 		if (!sc->rx_merge) {
384069b2ef0SAndy Fiddaman 			vq_relchain(vq, info[0].idx, ulen);
385069b2ef0SAndy Fiddaman 		} else {
386069b2ef0SAndy Fiddaman 			uint32_t iolen;
387069b2ef0SAndy Fiddaman 			int i = 0;
388069b2ef0SAndy Fiddaman 
389069b2ef0SAndy Fiddaman 			do {
390069b2ef0SAndy Fiddaman 				iolen = info[i].len;
391069b2ef0SAndy Fiddaman 				if (iolen > ulen) {
392069b2ef0SAndy Fiddaman 					iolen = ulen;
393069b2ef0SAndy Fiddaman 				}
394069b2ef0SAndy Fiddaman 				vq_relchain_prepare(vq, info[i].idx, iolen);
395069b2ef0SAndy Fiddaman 				ulen -= iolen;
396069b2ef0SAndy Fiddaman 				i++;
397069b2ef0SAndy Fiddaman 			} while (ulen > 0);
398069b2ef0SAndy Fiddaman 
399069b2ef0SAndy Fiddaman 			hdr->vrh_bufs = i;
400069b2ef0SAndy Fiddaman 			vq_relchain_publish(vq);
401069b2ef0SAndy Fiddaman 			assert(i == n_chains);
4024c87aefeSPatrick Mooney 		}
4034c87aefeSPatrick Mooney 	}
4044c87aefeSPatrick Mooney 
4054c87aefeSPatrick Mooney }
4064c87aefeSPatrick Mooney 
4074c87aefeSPatrick Mooney /*
408069b2ef0SAndy Fiddaman  * Called when there is read activity on the backend file descriptor.
409069b2ef0SAndy Fiddaman  * Each buffer posted by the guest is assumed to be able to contain
410069b2ef0SAndy Fiddaman  * an entire ethernet frame + rx header.
4114c87aefeSPatrick Mooney  */
4124c87aefeSPatrick Mooney static void
pci_vtnet_rx_callback(int fd __unused,enum ev_type type __unused,void * param)41359d65d31SAndy Fiddaman pci_vtnet_rx_callback(int fd __unused, enum ev_type type __unused, void *param)
414bf21cd93STycho Nightingale {
415bf21cd93STycho Nightingale 	struct pci_vtnet_softc *sc = param;
416bf21cd93STycho Nightingale 
417bf21cd93STycho Nightingale 	pthread_mutex_lock(&sc->rx_mtx);
418069b2ef0SAndy Fiddaman 	pci_vtnet_rx(sc);
419bf21cd93STycho Nightingale 	pthread_mutex_unlock(&sc->rx_mtx);
420bf21cd93STycho Nightingale 
421bf21cd93STycho Nightingale }
422bf21cd93STycho Nightingale 
423069b2ef0SAndy Fiddaman /* Called on RX kick. */
424bf21cd93STycho Nightingale static void
pci_vtnet_ping_rxq(void * vsc,struct vqueue_info * vq)425bf21cd93STycho Nightingale pci_vtnet_ping_rxq(void *vsc, struct vqueue_info *vq)
426bf21cd93STycho Nightingale {
427bf21cd93STycho Nightingale 	struct pci_vtnet_softc *sc = vsc;
428bf21cd93STycho Nightingale 
429bf21cd93STycho Nightingale 	/*
430154972afSPatrick Mooney 	 * A qnotify means that the rx process can now begin.
4316960cd89SAndy Fiddaman 	 * Enable RX only if features are negotiated.
432bf21cd93STycho Nightingale 	 */
4336960cd89SAndy Fiddaman 	pthread_mutex_lock(&sc->rx_mtx);
434069b2ef0SAndy Fiddaman 	if (!sc->features_negotiated) {
435069b2ef0SAndy Fiddaman 		pthread_mutex_unlock(&sc->rx_mtx);
436069b2ef0SAndy Fiddaman 		return;
437bf21cd93STycho Nightingale 	}
438069b2ef0SAndy Fiddaman 
439069b2ef0SAndy Fiddaman 	vq_kick_disable(vq);
440069b2ef0SAndy Fiddaman 	netbe_rx_enable(sc->vsc_be);
4416960cd89SAndy Fiddaman 	pthread_mutex_unlock(&sc->rx_mtx);
442bf21cd93STycho Nightingale }
443bf21cd93STycho Nightingale 
444069b2ef0SAndy Fiddaman /* TX virtqueue processing, called by the TX thread. */
445bf21cd93STycho Nightingale static void
pci_vtnet_proctx(struct pci_vtnet_softc * sc,struct vqueue_info * vq)446bf21cd93STycho Nightingale pci_vtnet_proctx(struct pci_vtnet_softc *sc, struct vqueue_info *vq)
447bf21cd93STycho Nightingale {
448bf21cd93STycho Nightingale 	struct iovec iov[VTNET_MAXSEGS + 1];
449069b2ef0SAndy Fiddaman 	struct iovec *siov = iov;
450b0de25cbSAndy Fiddaman 	struct vi_req req;
451069b2ef0SAndy Fiddaman 	ssize_t len;
452069b2ef0SAndy Fiddaman 	int n;
453bf21cd93STycho Nightingale 
454bf21cd93STycho Nightingale 	/*
455069b2ef0SAndy Fiddaman 	 * Obtain chain of descriptors. The first descriptor also
456069b2ef0SAndy Fiddaman 	 * contains the virtio-net header.
457bf21cd93STycho Nightingale 	 */
458b0de25cbSAndy Fiddaman 	n = vq_getchain(vq, iov, VTNET_MAXSEGS, &req);
459bf21cd93STycho Nightingale 	assert(n >= 1 && n <= VTNET_MAXSEGS);
460069b2ef0SAndy Fiddaman 
461069b2ef0SAndy Fiddaman 	if (sc->vhdrlen != sc->be_vhdrlen) {
462069b2ef0SAndy Fiddaman 		/*
463069b2ef0SAndy Fiddaman 		 * The frontend uses a virtio-net header, but the backend
464069b2ef0SAndy Fiddaman 		 * does not. We simply strip the header and ignore it, as
465069b2ef0SAndy Fiddaman 		 * it should be zero-filled.
466069b2ef0SAndy Fiddaman 		 */
467069b2ef0SAndy Fiddaman 		siov = iov_trim_hdr(siov, &n, sc->vhdrlen);
468bf21cd93STycho Nightingale 	}
469bf21cd93STycho Nightingale 
470069b2ef0SAndy Fiddaman 	if (siov == NULL) {
471069b2ef0SAndy Fiddaman 		/* The chain is nonsensical. Just drop it. */
472069b2ef0SAndy Fiddaman 		len = 0;
473069b2ef0SAndy Fiddaman 	} else {
474069b2ef0SAndy Fiddaman 		len = netbe_send(sc->vsc_be, siov, n);
475069b2ef0SAndy Fiddaman 		if (len < 0) {
476069b2ef0SAndy Fiddaman 			/*
477069b2ef0SAndy Fiddaman 			 * If send failed, report that 0 bytes
478069b2ef0SAndy Fiddaman 			 * were read.
479069b2ef0SAndy Fiddaman 			 */
480069b2ef0SAndy Fiddaman 			len = 0;
481069b2ef0SAndy Fiddaman 		}
482069b2ef0SAndy Fiddaman 	}
483bf21cd93STycho Nightingale 
484069b2ef0SAndy Fiddaman 	/*
485069b2ef0SAndy Fiddaman 	 * Return the processed chain to the guest, reporting
486069b2ef0SAndy Fiddaman 	 * the number of bytes that we read.
487069b2ef0SAndy Fiddaman 	 */
488b0de25cbSAndy Fiddaman 	vq_relchain(vq, req.idx, len);
489bf21cd93STycho Nightingale }
490bf21cd93STycho Nightingale 
491069b2ef0SAndy Fiddaman /* Called on TX kick. */
492bf21cd93STycho Nightingale static void
pci_vtnet_ping_txq(void * vsc,struct vqueue_info * vq)493bf21cd93STycho Nightingale pci_vtnet_ping_txq(void *vsc, struct vqueue_info *vq)
494bf21cd93STycho Nightingale {
495bf21cd93STycho Nightingale 	struct pci_vtnet_softc *sc = vsc;
496bf21cd93STycho Nightingale 
497bf21cd93STycho Nightingale 	/*
498bf21cd93STycho Nightingale 	 * Any ring entries to process?
499bf21cd93STycho Nightingale 	 */
500bf21cd93STycho Nightingale 	if (!vq_has_descs(vq))
501bf21cd93STycho Nightingale 		return;
502bf21cd93STycho Nightingale 
503bf21cd93STycho Nightingale 	/* Signal the tx thread for processing */
504bf21cd93STycho Nightingale 	pthread_mutex_lock(&sc->tx_mtx);
50584659b24SMichael Zeller 	vq_kick_disable(vq);
506bf21cd93STycho Nightingale 	if (sc->tx_in_progress == 0)
507bf21cd93STycho Nightingale 		pthread_cond_signal(&sc->tx_cond);
508bf21cd93STycho Nightingale 	pthread_mutex_unlock(&sc->tx_mtx);
509bf21cd93STycho Nightingale }
510bf21cd93STycho Nightingale 
511bf21cd93STycho Nightingale /*
512bf21cd93STycho Nightingale  * Thread which will handle processing of TX desc
513bf21cd93STycho Nightingale  */
514bf21cd93STycho Nightingale static void *
pci_vtnet_tx_thread(void * param)515bf21cd93STycho Nightingale pci_vtnet_tx_thread(void *param)
516bf21cd93STycho Nightingale {
517bf21cd93STycho Nightingale 	struct pci_vtnet_softc *sc = param;
518bf21cd93STycho Nightingale 	struct vqueue_info *vq;
5194c87aefeSPatrick Mooney 	int error;
520bf21cd93STycho Nightingale 
521bf21cd93STycho Nightingale 	vq = &sc->vsc_queues[VTNET_TXQ];
522bf21cd93STycho Nightingale 
523bf21cd93STycho Nightingale 	/*
524bf21cd93STycho Nightingale 	 * Let us wait till the tx queue pointers get initialised &
525bf21cd93STycho Nightingale 	 * first tx signaled
526bf21cd93STycho Nightingale 	 */
527bf21cd93STycho Nightingale 	pthread_mutex_lock(&sc->tx_mtx);
528bf21cd93STycho Nightingale 	error = pthread_cond_wait(&sc->tx_cond, &sc->tx_mtx);
529bf21cd93STycho Nightingale 	assert(error == 0);
530bf21cd93STycho Nightingale 
531bf21cd93STycho Nightingale 	for (;;) {
532bf21cd93STycho Nightingale 		/* note - tx mutex is locked here */
5334c87aefeSPatrick Mooney 		while (sc->resetting || !vq_has_descs(vq)) {
53484659b24SMichael Zeller 			vq_kick_enable(vq);
5354c87aefeSPatrick Mooney 			if (!sc->resetting && vq_has_descs(vq))
5364c87aefeSPatrick Mooney 				break;
5374c87aefeSPatrick Mooney 
5384c87aefeSPatrick Mooney 			sc->tx_in_progress = 0;
5394c87aefeSPatrick Mooney 			error = pthread_cond_wait(&sc->tx_cond, &sc->tx_mtx);
5404c87aefeSPatrick Mooney 			assert(error == 0);
5414c87aefeSPatrick Mooney 		}
54284659b24SMichael Zeller 		vq_kick_disable(vq);
543bf21cd93STycho Nightingale 		sc->tx_in_progress = 1;
544bf21cd93STycho Nightingale 		pthread_mutex_unlock(&sc->tx_mtx);
545bf21cd93STycho Nightingale 
546bf21cd93STycho Nightingale 		do {
547bf21cd93STycho Nightingale 			/*
548bf21cd93STycho Nightingale 			 * Run through entries, placing them into
549bf21cd93STycho Nightingale 			 * iovecs and sending when an end-of-packet
550bf21cd93STycho Nightingale 			 * is found
551bf21cd93STycho Nightingale 			 */
552bf21cd93STycho Nightingale 			pci_vtnet_proctx(sc, vq);
553bf21cd93STycho Nightingale 		} while (vq_has_descs(vq));
554bf21cd93STycho Nightingale 
555bf21cd93STycho Nightingale 		/*
556bf21cd93STycho Nightingale 		 * Generate an interrupt if needed.
557bf21cd93STycho Nightingale 		 */
558069b2ef0SAndy Fiddaman 		vq_endchains(vq, /*used_all_avail=*/1);
559bf21cd93STycho Nightingale 
560bf21cd93STycho Nightingale 		pthread_mutex_lock(&sc->tx_mtx);
561bf21cd93STycho Nightingale 	}
562069b2ef0SAndy Fiddaman #ifndef __FreeBSD__
5634c87aefeSPatrick Mooney 	return (NULL);
564069b2ef0SAndy Fiddaman #endif
565bf21cd93STycho Nightingale }
566bf21cd93STycho Nightingale 
567069b2ef0SAndy Fiddaman #ifdef notyet
568bf21cd93STycho Nightingale static void
pci_vtnet_ping_ctlq(void * vsc,struct vqueue_info * vq)569bf21cd93STycho Nightingale pci_vtnet_ping_ctlq(void *vsc, struct vqueue_info *vq)
570bf21cd93STycho Nightingale {
571bf21cd93STycho Nightingale 
572154972afSPatrick Mooney 	DPRINTF(("vtnet: control qnotify!"));
573bf21cd93STycho Nightingale }
574bf21cd93STycho Nightingale #endif
575bf21cd93STycho Nightingale 
576bf21cd93STycho Nightingale static int
pci_vtnet_init(struct pci_devinst * pi,nvlist_t * nvl)577*32640292SAndy Fiddaman pci_vtnet_init(struct pci_devinst *pi, nvlist_t *nvl)
578bf21cd93STycho Nightingale {
579bf21cd93STycho Nightingale 	struct pci_vtnet_softc *sc;
5802b948146SAndy Fiddaman 	const char *value;
581154972afSPatrick Mooney 	char tname[MAXCOMLEN + 1];
582154972afSPatrick Mooney 	unsigned long mtu = ETHERMTU;
5832b948146SAndy Fiddaman 	int err;
584bf21cd93STycho Nightingale 
585154972afSPatrick Mooney 	/*
586154972afSPatrick Mooney 	 * Allocate data structures for further virtio initializations.
587154972afSPatrick Mooney 	 * sc also contains a copy of vtnet_vi_consts, since capabilities
588154972afSPatrick Mooney 	 * change depending on the backend.
589154972afSPatrick Mooney 	 */
5904c87aefeSPatrick Mooney 	sc = calloc(1, sizeof(struct pci_vtnet_softc));
591bf21cd93STycho Nightingale 
592154972afSPatrick Mooney 	sc->vsc_consts = vtnet_vi_consts;
593bf21cd93STycho Nightingale 	pthread_mutex_init(&sc->vsc_mtx, NULL);
594bf21cd93STycho Nightingale 
595bf21cd93STycho Nightingale 	sc->vsc_queues[VTNET_RXQ].vq_qsize = VTNET_RINGSZ;
596bf21cd93STycho Nightingale 	sc->vsc_queues[VTNET_RXQ].vq_notify = pci_vtnet_ping_rxq;
597bf21cd93STycho Nightingale 	sc->vsc_queues[VTNET_TXQ].vq_qsize = VTNET_RINGSZ;
598bf21cd93STycho Nightingale 	sc->vsc_queues[VTNET_TXQ].vq_notify = pci_vtnet_ping_txq;
599154972afSPatrick Mooney #ifdef notyet
600bf21cd93STycho Nightingale 	sc->vsc_queues[VTNET_CTLQ].vq_qsize = VTNET_RINGSZ;
601bf21cd93STycho Nightingale         sc->vsc_queues[VTNET_CTLQ].vq_notify = pci_vtnet_ping_ctlq;
602bf21cd93STycho Nightingale #endif
603bf21cd93STycho Nightingale 
6042b948146SAndy Fiddaman 	value = get_config_value_node(nvl, "mac");
6052b948146SAndy Fiddaman 	if (value != NULL) {
6062b948146SAndy Fiddaman 		err = net_parsemac(value, sc->vsc_config.mac);
6072b948146SAndy Fiddaman 		if (err) {
6082b948146SAndy Fiddaman 			free(sc);
6092b948146SAndy Fiddaman 			return (err);
610bf21cd93STycho Nightingale 		}
6112b948146SAndy Fiddaman 	} else
6122b948146SAndy Fiddaman 		net_genmac(pi, sc->vsc_config.mac);
613154972afSPatrick Mooney 
6142b948146SAndy Fiddaman 	value = get_config_value_node(nvl, "mtu");
6152b948146SAndy Fiddaman 	if (value != NULL) {
6162b948146SAndy Fiddaman 		err = net_parsemtu(value, &mtu);
617154972afSPatrick Mooney 		if (err) {
618154972afSPatrick Mooney 			free(sc);
619154972afSPatrick Mooney 			return (err);
620154972afSPatrick Mooney 		}
621069b2ef0SAndy Fiddaman 
6222b948146SAndy Fiddaman 		if (mtu < VTNET_MIN_MTU || mtu > VTNET_MAX_MTU) {
6232b948146SAndy Fiddaman 			err = EINVAL;
6242b948146SAndy Fiddaman 			errno = EINVAL;
6252b948146SAndy Fiddaman 			free(sc);
6262b948146SAndy Fiddaman 			return (err);
6272b948146SAndy Fiddaman 		}
6282b948146SAndy Fiddaman 		sc->vsc_consts.vc_hv_caps |= VIRTIO_NET_F_MTU;
6292b948146SAndy Fiddaman 	}
630069b2ef0SAndy Fiddaman 	sc->vsc_config.mtu = mtu;
631154972afSPatrick Mooney 
6322b948146SAndy Fiddaman 	/* Permit interfaces without a configured backend. */
6332b948146SAndy Fiddaman 	if (get_config_value_node(nvl, "backend") != NULL) {
6342b948146SAndy Fiddaman 		err = netbe_init(&sc->vsc_be, nvl, pci_vtnet_rx_callback, sc);
635154972afSPatrick Mooney 		if (err) {
636154972afSPatrick Mooney 			free(sc);
637154972afSPatrick Mooney 			return (err);
638154972afSPatrick Mooney 		}
639069b2ef0SAndy Fiddaman #ifndef __FreeBSD__
640069b2ef0SAndy Fiddaman 		size_t buflen = sizeof (sc->vsc_config.mac);
641069b2ef0SAndy Fiddaman 
642069b2ef0SAndy Fiddaman 		err = netbe_get_mac(sc->vsc_be, sc->vsc_config.mac, &buflen);
643069b2ef0SAndy Fiddaman 		if (err != 0) {
644069b2ef0SAndy Fiddaman 		       free(sc);
645069b2ef0SAndy Fiddaman 		       return (err);
646069b2ef0SAndy Fiddaman 		}
6474c87aefeSPatrick Mooney #endif
648bf21cd93STycho Nightingale 	}
649bf21cd93STycho Nightingale 
6502b948146SAndy Fiddaman 	sc->vsc_consts.vc_hv_caps |= VIRTIO_NET_F_MRG_RXBUF |
6512b948146SAndy Fiddaman 	    netbe_get_cap(sc->vsc_be);
652bf21cd93STycho Nightingale 
6536dc98349SAndy Fiddaman 	/*
654154972afSPatrick Mooney 	 * Since we do not actually support multiqueue,
6556dc98349SAndy Fiddaman 	 * set the maximum virtqueue pairs to 1.
656154972afSPatrick Mooney 	 */
657154972afSPatrick Mooney 	sc->vsc_config.max_virtqueue_pairs = 1;
658154972afSPatrick Mooney 
659bf21cd93STycho Nightingale 	/* initialize config space */
660bf21cd93STycho Nightingale 	pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_NET);
661bf21cd93STycho Nightingale 	pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR);
662bf21cd93STycho Nightingale 	pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_NETWORK);
6632b948146SAndy Fiddaman 	pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_ID_NETWORK);
6644c87aefeSPatrick Mooney 	pci_set_cfgdata16(pi, PCIR_SUBVEND_0, VIRTIO_VENDOR);
665bf21cd93STycho Nightingale 
6662b948146SAndy Fiddaman 	/* Link is always up. */
6672b948146SAndy Fiddaman 	sc->vsc_config.status = 1;
6686dc98349SAndy Fiddaman 
669069b2ef0SAndy Fiddaman 	vi_softc_linkup(&sc->vsc_vs, &sc->vsc_consts, sc, pi, sc->vsc_queues);
670069b2ef0SAndy Fiddaman 	sc->vsc_vs.vs_mtx = &sc->vsc_mtx;
671154972afSPatrick Mooney 
672bf21cd93STycho Nightingale 	/* use BAR 1 to map MSI-X table and PBA, if we're using MSI-X */
673069b2ef0SAndy Fiddaman 	if (vi_intr_init(&sc->vsc_vs, 1, fbsdrun_virtio_msix())) {
674069b2ef0SAndy Fiddaman 		free(sc);
675bf21cd93STycho Nightingale 		return (1);
676069b2ef0SAndy Fiddaman 	}
677bf21cd93STycho Nightingale 
678bf21cd93STycho Nightingale 	/* use BAR 0 to map config regs in IO space */
679bf21cd93STycho Nightingale 	vi_set_io_bar(&sc->vsc_vs, 0);
680bf21cd93STycho Nightingale 
681bf21cd93STycho Nightingale 	sc->resetting = 0;
682bf21cd93STycho Nightingale 
683069b2ef0SAndy Fiddaman 	sc->rx_merge = 0;
684069b2ef0SAndy Fiddaman 	sc->vhdrlen = sizeof(struct virtio_net_rxhdr) - 2;
6856dc98349SAndy Fiddaman 	pthread_mutex_init(&sc->rx_mtx, NULL);
686bf21cd93STycho Nightingale 
6876dc98349SAndy Fiddaman 	/*
688bf21cd93STycho Nightingale 	 * Initialize tx semaphore & spawn TX processing thread.
689bf21cd93STycho Nightingale 	 * As of now, only one thread for TX desc processing is
6906dc98349SAndy Fiddaman 	 * spawned.
691bf21cd93STycho Nightingale 	 */
692bf21cd93STycho Nightingale 	sc->tx_in_progress = 0;
693bf21cd93STycho Nightingale 	pthread_mutex_init(&sc->tx_mtx, NULL);
694bf21cd93STycho Nightingale 	pthread_cond_init(&sc->tx_cond, NULL);
695bf21cd93STycho Nightingale 	pthread_create(&sc->tx_tid, NULL, pci_vtnet_tx_thread, (void *)sc);
6964c87aefeSPatrick Mooney 	snprintf(tname, sizeof(tname), "vtnet-%d:%d tx", pi->pi_slot,
6974c87aefeSPatrick Mooney 	    pi->pi_func);
6984c87aefeSPatrick Mooney 	pthread_set_name_np(sc->tx_tid, tname);
699bf21cd93STycho Nightingale 
700bf21cd93STycho Nightingale 	return (0);
701bf21cd93STycho Nightingale }
702bf21cd93STycho Nightingale 
703bf21cd93STycho Nightingale static int
pci_vtnet_cfgwrite(void * vsc,int offset,int size,uint32_t value)704bf21cd93STycho Nightingale pci_vtnet_cfgwrite(void *vsc, int offset, int size, uint32_t value)
705bf21cd93STycho Nightingale {
706bf21cd93STycho Nightingale 	struct pci_vtnet_softc *sc = vsc;
707bf21cd93STycho Nightingale 	void *ptr;
708bf21cd93STycho Nightingale 
709069b2ef0SAndy Fiddaman 	if (offset < (int)sizeof(sc->vsc_config.mac)) {
710069b2ef0SAndy Fiddaman 		assert(offset + size <= (int)sizeof(sc->vsc_config.mac));
711bf21cd93STycho Nightingale 		/*
712bf21cd93STycho Nightingale 		 * The driver is allowed to change the MAC address
713bf21cd93STycho Nightingale 		 */
714bf21cd93STycho Nightingale 		ptr = &sc->vsc_config.mac[offset];
715bf21cd93STycho Nightingale 		memcpy(ptr, &value, size);
716bf21cd93STycho Nightingale 	} else {
7174c87aefeSPatrick Mooney 		/* silently ignore other writes */
718154972afSPatrick Mooney 		DPRINTF(("vtnet: write to readonly reg %d", offset));
719bf21cd93STycho Nightingale 	}
7204c87aefeSPatrick Mooney 
721bf21cd93STycho Nightingale 	return (0);
722bf21cd93STycho Nightingale }
723bf21cd93STycho Nightingale 
724bf21cd93STycho Nightingale static int
pci_vtnet_cfgread(void * vsc,int offset,int size,uint32_t * retval)725bf21cd93STycho Nightingale pci_vtnet_cfgread(void *vsc, int offset, int size, uint32_t *retval)
726bf21cd93STycho Nightingale {
727bf21cd93STycho Nightingale 	struct pci_vtnet_softc *sc = vsc;
728bf21cd93STycho Nightingale 	void *ptr;
729bf21cd93STycho Nightingale 
730bf21cd93STycho Nightingale 	ptr = (uint8_t *)&sc->vsc_config + offset;
731bf21cd93STycho Nightingale 	memcpy(retval, ptr, size);
732bf21cd93STycho Nightingale 	return (0);
733bf21cd93STycho Nightingale }
734bf21cd93STycho Nightingale 
7354c87aefeSPatrick Mooney static void
pci_vtnet_neg_features(void * vsc,uint64_t negotiated_features)7364c87aefeSPatrick Mooney pci_vtnet_neg_features(void *vsc, uint64_t negotiated_features)
7374c87aefeSPatrick Mooney {
7384c87aefeSPatrick Mooney 	struct pci_vtnet_softc *sc = vsc;
7394c87aefeSPatrick Mooney 
7404c87aefeSPatrick Mooney 	sc->vsc_features = negotiated_features;
7414c87aefeSPatrick Mooney 
742069b2ef0SAndy Fiddaman 	if (negotiated_features & VIRTIO_NET_F_MRG_RXBUF) {
743069b2ef0SAndy Fiddaman 		sc->vhdrlen = sizeof(struct virtio_net_rxhdr);
744069b2ef0SAndy Fiddaman 		sc->rx_merge = 1;
745069b2ef0SAndy Fiddaman 	} else {
746069b2ef0SAndy Fiddaman 		/*
747069b2ef0SAndy Fiddaman 		 * Without mergeable rx buffers, virtio-net header is 2
748069b2ef0SAndy Fiddaman 		 * bytes shorter than sizeof(struct virtio_net_rxhdr).
749069b2ef0SAndy Fiddaman 		 */
750069b2ef0SAndy Fiddaman 		sc->vhdrlen = sizeof(struct virtio_net_rxhdr) - 2;
7514c87aefeSPatrick Mooney 		sc->rx_merge = 0;
7524c87aefeSPatrick Mooney 	}
7536960cd89SAndy Fiddaman 
754069b2ef0SAndy Fiddaman 	/* Tell the backend to enable some capabilities it has advertised. */
755069b2ef0SAndy Fiddaman 	netbe_set_cap(sc->vsc_be, negotiated_features, sc->vhdrlen);
756069b2ef0SAndy Fiddaman 	sc->be_vhdrlen = netbe_get_vnet_hdr_len(sc->vsc_be);
757069b2ef0SAndy Fiddaman 	assert(sc->be_vhdrlen == 0 || sc->be_vhdrlen == sc->vhdrlen);
758069b2ef0SAndy Fiddaman 
7596960cd89SAndy Fiddaman 	pthread_mutex_lock(&sc->rx_mtx);
7606960cd89SAndy Fiddaman 	sc->features_negotiated = true;
7616960cd89SAndy Fiddaman 	pthread_mutex_unlock(&sc->rx_mtx);
7624c87aefeSPatrick Mooney }
7634c87aefeSPatrick Mooney 
7644f3f3e9aSAndy Fiddaman static const struct pci_devemu pci_de_vnet = {
765bf21cd93STycho Nightingale 	.pe_emu = 	"virtio-net",
766bf21cd93STycho Nightingale 	.pe_init =	pci_vtnet_init,
7672b948146SAndy Fiddaman 	.pe_legacy_config = netbe_legacy_config,
768bf21cd93STycho Nightingale 	.pe_barwrite =	vi_pci_write,
769069b2ef0SAndy Fiddaman 	.pe_barread =	vi_pci_read,
770bf21cd93STycho Nightingale };
771bf21cd93STycho Nightingale PCI_EMUL_SET(pci_de_vnet);
772