1366f6083SPeter Grehan /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni *
4366f6083SPeter Grehan * Copyright (c) 2011 NetApp, Inc.
5366f6083SPeter Grehan * All rights reserved.
6366f6083SPeter Grehan *
7366f6083SPeter Grehan * Redistribution and use in source and binary forms, with or without
8366f6083SPeter Grehan * modification, are permitted provided that the following conditions
9366f6083SPeter Grehan * are met:
10366f6083SPeter Grehan * 1. Redistributions of source code must retain the above copyright
11366f6083SPeter Grehan * notice, this list of conditions and the following disclaimer.
12366f6083SPeter Grehan * 2. Redistributions in binary form must reproduce the above copyright
13366f6083SPeter Grehan * notice, this list of conditions and the following disclaimer in the
14366f6083SPeter Grehan * documentation and/or other materials provided with the distribution.
15366f6083SPeter Grehan *
16366f6083SPeter Grehan * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17366f6083SPeter Grehan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18366f6083SPeter Grehan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19366f6083SPeter Grehan * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20366f6083SPeter Grehan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21366f6083SPeter Grehan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22366f6083SPeter Grehan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23366f6083SPeter Grehan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24366f6083SPeter Grehan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25366f6083SPeter Grehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26366f6083SPeter Grehan * SUCH DAMAGE.
27366f6083SPeter Grehan */
28366f6083SPeter Grehan
29366f6083SPeter Grehan #include <sys/param.h>
30366f6083SPeter Grehan #include <sys/linker_set.h>
31366f6083SPeter Grehan #include <sys/select.h>
32366f6083SPeter Grehan #include <sys/uio.h>
33366f6083SPeter Grehan #include <sys/ioctl.h>
34483d953aSJohn Baldwin #include <machine/vmm_snapshot.h>
35a38e2a64SPeter Grehan #include <net/ethernet.h>
360ff7076bSVincenzo Maffione #include <net/if.h> /* IFNAMSIZ */
37366f6083SPeter Grehan
3800ef17beSBartek Rutkowski #include <err.h>
39366f6083SPeter Grehan #include <errno.h>
40366f6083SPeter Grehan #include <fcntl.h>
41366f6083SPeter Grehan #include <stdio.h>
42366f6083SPeter Grehan #include <stdlib.h>
43366f6083SPeter Grehan #include <stdint.h>
44366f6083SPeter Grehan #include <string.h>
45366f6083SPeter Grehan #include <strings.h>
46366f6083SPeter Grehan #include <unistd.h>
47366f6083SPeter Grehan #include <assert.h>
48366f6083SPeter Grehan #include <pthread.h>
49199fee4eSPeter Grehan #include <pthread_np.h>
50366f6083SPeter Grehan
51e285ef8dSPeter Grehan #include "bhyverun.h"
52621b5090SJohn Baldwin #include "config.h"
53332eff95SVincenzo Maffione #include "debug.h"
54366f6083SPeter Grehan #include "pci_emul.h"
55366f6083SPeter Grehan #include "mevent.h"
56366f6083SPeter Grehan #include "virtio.h"
574f7c3b7bSVincenzo Maffione #include "net_utils.h"
580ff7076bSVincenzo Maffione #include "net_backends.h"
59d55e0373SVincenzo Maffione #include "iov.h"
60366f6083SPeter Grehan
61199fee4eSPeter Grehan #define VTNET_RINGSZ 1024
62366f6083SPeter Grehan
631e306308SPeter Grehan #define VTNET_MAXSEGS 256
64366f6083SPeter Grehan
65d55e0373SVincenzo Maffione #define VTNET_MAX_PKT_LEN (65536 + 64)
66d55e0373SVincenzo Maffione
671ff57e3aSAleksandr Fedorov #define VTNET_MIN_MTU ETHERMIN
681ff57e3aSAleksandr Fedorov #define VTNET_MAX_MTU 65535
691ff57e3aSAleksandr Fedorov
70ba41c3c1SPeter Grehan #define VTNET_S_HOSTCAPS \
710ff7076bSVincenzo Maffione ( VIRTIO_NET_F_MAC | VIRTIO_NET_F_STATUS | \
721e306308SPeter Grehan VIRTIO_F_NOTIFY_ON_EMPTY | VIRTIO_RING_F_INDIRECT_DESC)
73366f6083SPeter Grehan
74366f6083SPeter Grehan /*
75ba41c3c1SPeter Grehan * PCI config-space "registers"
76366f6083SPeter Grehan */
77ba41c3c1SPeter Grehan struct virtio_net_config {
78ba41c3c1SPeter Grehan uint8_t mac[6];
79ba41c3c1SPeter Grehan uint16_t status;
801ff57e3aSAleksandr Fedorov uint16_t max_virtqueue_pairs;
811ff57e3aSAleksandr Fedorov uint16_t mtu;
82ba41c3c1SPeter Grehan } __packed;
83366f6083SPeter Grehan
84366f6083SPeter Grehan /*
85366f6083SPeter Grehan * Queue definitions.
86366f6083SPeter Grehan */
87366f6083SPeter Grehan #define VTNET_RXQ 0
88366f6083SPeter Grehan #define VTNET_TXQ 1
89ba41c3c1SPeter Grehan #define VTNET_CTLQ 2 /* NB: not yet supported */
90366f6083SPeter Grehan
91366f6083SPeter Grehan #define VTNET_MAXQ 3
92366f6083SPeter Grehan
93366f6083SPeter Grehan /*
94366f6083SPeter Grehan * Debug printf
95366f6083SPeter Grehan */
96366f6083SPeter Grehan static int pci_vtnet_debug;
97332eff95SVincenzo Maffione #define DPRINTF(params) if (pci_vtnet_debug) PRINTLN params
98332eff95SVincenzo Maffione #define WPRINTF(params) PRINTLN params
99366f6083SPeter Grehan
100366f6083SPeter Grehan /*
101366f6083SPeter Grehan * Per-device softc
102366f6083SPeter Grehan */
103366f6083SPeter Grehan struct pci_vtnet_softc {
104ba41c3c1SPeter Grehan struct virtio_softc vsc_vs;
105ba41c3c1SPeter Grehan struct vqueue_info vsc_queues[VTNET_MAXQ - 1];
106366f6083SPeter Grehan pthread_mutex_t vsc_mtx;
107366f6083SPeter Grehan
1080ff7076bSVincenzo Maffione net_backend_t *vsc_be;
109b6020475SGeorge V. Neville-Neil
110ea57b2d7SAleksandr Fedorov bool features_negotiated; /* protected by rx_mtx */
111ea57b2d7SAleksandr Fedorov
112f3b1307eSVincenzo Maffione int resetting; /* protected by tx_mtx */
113366f6083SPeter Grehan
11482560f19SPeter Grehan uint64_t vsc_features; /* negotiated features */
11582560f19SPeter Grehan
1162a80be7bSNeel Natu pthread_mutex_t rx_mtx;
11782560f19SPeter Grehan int rx_merge; /* merged rx bufs in use */
1182a80be7bSNeel Natu
119199fee4eSPeter Grehan pthread_t tx_tid;
120199fee4eSPeter Grehan pthread_mutex_t tx_mtx;
121199fee4eSPeter Grehan pthread_cond_t tx_cond;
1222a80be7bSNeel Natu int tx_in_progress;
123b6020475SGeorge V. Neville-Neil
12466c662b0SVincenzo Maffione size_t vhdrlen;
12566c662b0SVincenzo Maffione size_t be_vhdrlen;
12666c662b0SVincenzo Maffione
1270ff7076bSVincenzo Maffione struct virtio_net_config vsc_config;
1280ff7076bSVincenzo Maffione struct virtio_consts vsc_consts;
129366f6083SPeter Grehan };
130366f6083SPeter Grehan
131ba41c3c1SPeter Grehan static void pci_vtnet_reset(void *);
132ba41c3c1SPeter Grehan /* static void pci_vtnet_notify(void *, struct vqueue_info *); */
133ba41c3c1SPeter Grehan static int pci_vtnet_cfgread(void *, int, int, uint32_t *);
134ba41c3c1SPeter Grehan static int pci_vtnet_cfgwrite(void *, int, int, uint32_t);
13582560f19SPeter Grehan static void pci_vtnet_neg_features(void *, uint64_t);
136483d953aSJohn Baldwin #ifdef BHYVE_SNAPSHOT
137483d953aSJohn Baldwin static void pci_vtnet_pause(void *);
138483d953aSJohn Baldwin static void pci_vtnet_resume(void *);
139483d953aSJohn Baldwin static int pci_vtnet_snapshot(void *, struct vm_snapshot_meta *);
140483d953aSJohn Baldwin #endif
141c9b4e987SNeel Natu
142ba41c3c1SPeter Grehan static struct virtio_consts vtnet_vi_consts = {
1436cb26162SMark Johnston .vc_name = "vtnet",
1446cb26162SMark Johnston .vc_nvq = VTNET_MAXQ - 1,
1456cb26162SMark Johnston .vc_cfgsize = sizeof(struct virtio_net_config),
1466cb26162SMark Johnston .vc_reset = pci_vtnet_reset,
1476cb26162SMark Johnston .vc_cfgread = pci_vtnet_cfgread,
1486cb26162SMark Johnston .vc_cfgwrite = pci_vtnet_cfgwrite,
1496cb26162SMark Johnston .vc_apply_features = pci_vtnet_neg_features,
1506cb26162SMark Johnston .vc_hv_caps = VTNET_S_HOSTCAPS,
151483d953aSJohn Baldwin #ifdef BHYVE_SNAPSHOT
1526cb26162SMark Johnston .vc_pause = pci_vtnet_pause,
1536cb26162SMark Johnston .vc_resume = pci_vtnet_resume,
1546cb26162SMark Johnston .vc_snapshot = pci_vtnet_snapshot,
155483d953aSJohn Baldwin #endif
156ba41c3c1SPeter Grehan };
15731f78e49SPeter Grehan
15831f78e49SPeter Grehan static void
pci_vtnet_reset(void * vsc)159ba41c3c1SPeter Grehan pci_vtnet_reset(void *vsc)
160366f6083SPeter Grehan {
161ba41c3c1SPeter Grehan struct pci_vtnet_softc *sc = vsc;
16231f78e49SPeter Grehan
163332eff95SVincenzo Maffione DPRINTF(("vtnet: device reset requested !"));
164199fee4eSPeter Grehan
165f3b1307eSVincenzo Maffione /* Acquire the RX lock to block RX processing. */
166f3b1307eSVincenzo Maffione pthread_mutex_lock(&sc->rx_mtx);
167199fee4eSPeter Grehan
168d70b2069SVincenzo Maffione /*
169d70b2069SVincenzo Maffione * Make sure receive operation is disabled at least until we
170d70b2069SVincenzo Maffione * re-negotiate the features, since receive operation depends
171d70b2069SVincenzo Maffione * on the value of sc->rx_merge and the header length, which
172d70b2069SVincenzo Maffione * are both set in pci_vtnet_neg_features().
173d70b2069SVincenzo Maffione * Receive operation will be enabled again once the guest adds
174d70b2069SVincenzo Maffione * the first receive buffers and kicks us.
175d70b2069SVincenzo Maffione */
176ea57b2d7SAleksandr Fedorov sc->features_negotiated = false;
177d70b2069SVincenzo Maffione netbe_rx_disable(sc->vsc_be);
178d70b2069SVincenzo Maffione
179f3b1307eSVincenzo Maffione /* Set sc->resetting and give a chance to the TX thread to stop. */
180f3b1307eSVincenzo Maffione pthread_mutex_lock(&sc->tx_mtx);
181f3b1307eSVincenzo Maffione sc->resetting = 1;
182f3b1307eSVincenzo Maffione while (sc->tx_in_progress) {
183f3b1307eSVincenzo Maffione pthread_mutex_unlock(&sc->tx_mtx);
184f3b1307eSVincenzo Maffione usleep(10000);
185f3b1307eSVincenzo Maffione pthread_mutex_lock(&sc->tx_mtx);
186f3b1307eSVincenzo Maffione }
187199fee4eSPeter Grehan
188f3b1307eSVincenzo Maffione /*
189f3b1307eSVincenzo Maffione * Now reset rings, MSI-X vectors, and negotiated capabilities.
190f3b1307eSVincenzo Maffione * Do that with the TX lock held, since we need to reset
191f3b1307eSVincenzo Maffione * sc->resetting.
192f3b1307eSVincenzo Maffione */
193ba41c3c1SPeter Grehan vi_reset_dev(&sc->vsc_vs);
1943b207d1eSNeel Natu
195199fee4eSPeter Grehan sc->resetting = 0;
196f3b1307eSVincenzo Maffione pthread_mutex_unlock(&sc->tx_mtx);
197f3b1307eSVincenzo Maffione pthread_mutex_unlock(&sc->rx_mtx);
198366f6083SPeter Grehan }
199366f6083SPeter Grehan
20066c662b0SVincenzo Maffione static __inline struct iovec *
iov_trim_hdr(struct iovec * iov,int * iovcnt,unsigned int hlen)20166c662b0SVincenzo Maffione iov_trim_hdr(struct iovec *iov, int *iovcnt, unsigned int hlen)
20266c662b0SVincenzo Maffione {
20366c662b0SVincenzo Maffione struct iovec *riov;
20466c662b0SVincenzo Maffione
20566c662b0SVincenzo Maffione if (iov[0].iov_len < hlen) {
20666c662b0SVincenzo Maffione /*
20766c662b0SVincenzo Maffione * Not enough header space in the first fragment.
20866c662b0SVincenzo Maffione * That's not ok for us.
20966c662b0SVincenzo Maffione */
21066c662b0SVincenzo Maffione return NULL;
21166c662b0SVincenzo Maffione }
21266c662b0SVincenzo Maffione
21366c662b0SVincenzo Maffione iov[0].iov_len -= hlen;
21466c662b0SVincenzo Maffione if (iov[0].iov_len == 0) {
21566c662b0SVincenzo Maffione *iovcnt -= 1;
21666c662b0SVincenzo Maffione if (*iovcnt == 0) {
21766c662b0SVincenzo Maffione /*
21866c662b0SVincenzo Maffione * Only space for the header. That's not
21966c662b0SVincenzo Maffione * enough for us.
22066c662b0SVincenzo Maffione */
22166c662b0SVincenzo Maffione return NULL;
22266c662b0SVincenzo Maffione }
22366c662b0SVincenzo Maffione riov = &iov[1];
22466c662b0SVincenzo Maffione } else {
22566c662b0SVincenzo Maffione iov[0].iov_base = (void *)((uintptr_t)iov[0].iov_base + hlen);
22666c662b0SVincenzo Maffione riov = &iov[0];
22766c662b0SVincenzo Maffione }
22866c662b0SVincenzo Maffione
22966c662b0SVincenzo Maffione return (riov);
23066c662b0SVincenzo Maffione }
23166c662b0SVincenzo Maffione
232d55e0373SVincenzo Maffione struct virtio_mrg_rxbuf_info {
233d55e0373SVincenzo Maffione uint16_t idx;
234d55e0373SVincenzo Maffione uint16_t pad;
235d55e0373SVincenzo Maffione uint32_t len;
236d55e0373SVincenzo Maffione };
237d55e0373SVincenzo Maffione
238366f6083SPeter Grehan static void
pci_vtnet_rx(struct pci_vtnet_softc * sc)2390ff7076bSVincenzo Maffione pci_vtnet_rx(struct pci_vtnet_softc *sc)
240366f6083SPeter Grehan {
24166c662b0SVincenzo Maffione int prepend_hdr_len = sc->vhdrlen - sc->be_vhdrlen;
242d55e0373SVincenzo Maffione struct virtio_mrg_rxbuf_info info[VTNET_MAXSEGS];
2430ff7076bSVincenzo Maffione struct iovec iov[VTNET_MAXSEGS + 1];
2440ff7076bSVincenzo Maffione struct vqueue_info *vq;
245b0139127SKa Ho Ng struct vi_req req;
246366f6083SPeter Grehan
2470ff7076bSVincenzo Maffione vq = &sc->vsc_queues[VTNET_RXQ];
248ea57b2d7SAleksandr Fedorov
249ea57b2d7SAleksandr Fedorov /* Features must be negotiated */
250ea57b2d7SAleksandr Fedorov if (!sc->features_negotiated) {
251ea57b2d7SAleksandr Fedorov return;
252ea57b2d7SAleksandr Fedorov }
253ea57b2d7SAleksandr Fedorov
2543e11768eSVincenzo Maffione for (;;) {
25566c662b0SVincenzo Maffione struct virtio_net_rxhdr *hdr;
256f92bb8c1SVincenzo Maffione uint32_t riov_bytes;
257f92bb8c1SVincenzo Maffione struct iovec *riov;
258f92bb8c1SVincenzo Maffione uint32_t ulen;
259f92bb8c1SVincenzo Maffione int riov_len;
260f92bb8c1SVincenzo Maffione int n_chains;
261f92bb8c1SVincenzo Maffione ssize_t rlen;
262f92bb8c1SVincenzo Maffione ssize_t plen;
263f92bb8c1SVincenzo Maffione
264f92bb8c1SVincenzo Maffione plen = netbe_peek_recvlen(sc->vsc_be);
265f92bb8c1SVincenzo Maffione if (plen <= 0) {
266f92bb8c1SVincenzo Maffione /*
267f92bb8c1SVincenzo Maffione * No more packets (plen == 0), or backend errored
268f92bb8c1SVincenzo Maffione * (plen < 0). Interrupt if needed and stop.
269f92bb8c1SVincenzo Maffione */
270f92bb8c1SVincenzo Maffione vq_endchains(vq, /*used_all_avail=*/0);
271f92bb8c1SVincenzo Maffione return;
272f92bb8c1SVincenzo Maffione }
273f92bb8c1SVincenzo Maffione plen += prepend_hdr_len;
27466c662b0SVincenzo Maffione
2753e11768eSVincenzo Maffione /*
276d55e0373SVincenzo Maffione * Get a descriptor chain to store the next ingress
277d55e0373SVincenzo Maffione * packet. In case of mergeable rx buffers, get as
278d55e0373SVincenzo Maffione * many chains as necessary in order to make room
279f92bb8c1SVincenzo Maffione * for plen bytes.
2803e11768eSVincenzo Maffione */
28166c662b0SVincenzo Maffione riov_bytes = 0;
28266c662b0SVincenzo Maffione riov_len = 0;
28366c662b0SVincenzo Maffione riov = iov;
284d55e0373SVincenzo Maffione n_chains = 0;
285d55e0373SVincenzo Maffione do {
286b0139127SKa Ho Ng int n = vq_getchain(vq, riov, VTNET_MAXSEGS - riov_len,
287b0139127SKa Ho Ng &req);
288b0139127SKa Ho Ng info[n_chains].idx = req.idx;
289d55e0373SVincenzo Maffione
290d55e0373SVincenzo Maffione if (n == 0) {
291d55e0373SVincenzo Maffione /*
292d55e0373SVincenzo Maffione * No rx buffers. Enable RX kicks and double
293d55e0373SVincenzo Maffione * check.
294d55e0373SVincenzo Maffione */
2953e11768eSVincenzo Maffione vq_kick_enable(vq);
2960ff7076bSVincenzo Maffione if (!vq_has_descs(vq)) {
2970ff7076bSVincenzo Maffione /*
298d55e0373SVincenzo Maffione * Still no buffers. Return the unused
299d55e0373SVincenzo Maffione * chains (if any), interrupt if needed
3003e11768eSVincenzo Maffione * (including for NOTIFY_ON_EMPTY), and
301d55e0373SVincenzo Maffione * disable the backend until the next
302d55e0373SVincenzo Maffione * kick.
3030ff7076bSVincenzo Maffione */
304d55e0373SVincenzo Maffione vq_retchains(vq, n_chains);
3050ff7076bSVincenzo Maffione vq_endchains(vq, /*used_all_avail=*/1);
3063e11768eSVincenzo Maffione netbe_rx_disable(sc->vsc_be);
3070ff7076bSVincenzo Maffione return;
3080ff7076bSVincenzo Maffione }
3090ff7076bSVincenzo Maffione
3103e11768eSVincenzo Maffione /* More rx buffers found, so keep going. */
3113e11768eSVincenzo Maffione vq_kick_disable(vq);
312d55e0373SVincenzo Maffione continue;
3133e11768eSVincenzo Maffione }
31466c662b0SVincenzo Maffione assert(n >= 1 && riov_len + n <= VTNET_MAXSEGS);
31566c662b0SVincenzo Maffione riov_len += n;
316d55e0373SVincenzo Maffione if (!sc->rx_merge) {
317d55e0373SVincenzo Maffione n_chains = 1;
318d55e0373SVincenzo Maffione break;
319d55e0373SVincenzo Maffione }
32066c662b0SVincenzo Maffione info[n_chains].len = (uint32_t)count_iov(riov, n);
32166c662b0SVincenzo Maffione riov_bytes += info[n_chains].len;
32266c662b0SVincenzo Maffione riov += n;
323d55e0373SVincenzo Maffione n_chains++;
324f92bb8c1SVincenzo Maffione } while (riov_bytes < plen && riov_len < VTNET_MAXSEGS);
3253e11768eSVincenzo Maffione
32666c662b0SVincenzo Maffione riov = iov;
32766c662b0SVincenzo Maffione hdr = riov[0].iov_base;
32866c662b0SVincenzo Maffione if (prepend_hdr_len > 0) {
32966c662b0SVincenzo Maffione /*
33066c662b0SVincenzo Maffione * The frontend uses a virtio-net header, but the
33166c662b0SVincenzo Maffione * backend does not. We need to prepend a zeroed
33266c662b0SVincenzo Maffione * header.
33366c662b0SVincenzo Maffione */
33466c662b0SVincenzo Maffione riov = iov_trim_hdr(riov, &riov_len, prepend_hdr_len);
33566c662b0SVincenzo Maffione if (riov == NULL) {
33666c662b0SVincenzo Maffione /*
33766c662b0SVincenzo Maffione * The first collected chain is nonsensical,
33866c662b0SVincenzo Maffione * as it is not even enough to store the
33966c662b0SVincenzo Maffione * virtio-net header. Just drop it.
34066c662b0SVincenzo Maffione */
34166c662b0SVincenzo Maffione vq_relchain(vq, info[0].idx, 0);
34266c662b0SVincenzo Maffione vq_retchains(vq, n_chains - 1);
34366c662b0SVincenzo Maffione continue;
34466c662b0SVincenzo Maffione }
34566c662b0SVincenzo Maffione memset(hdr, 0, prepend_hdr_len);
34666c662b0SVincenzo Maffione }
34766c662b0SVincenzo Maffione
348f92bb8c1SVincenzo Maffione rlen = netbe_recv(sc->vsc_be, riov, riov_len);
349f92bb8c1SVincenzo Maffione if (rlen != plen - prepend_hdr_len) {
3500ff7076bSVincenzo Maffione /*
351f92bb8c1SVincenzo Maffione * If this happens it means there is something
352f92bb8c1SVincenzo Maffione * wrong with the backend (e.g., some other
353f92bb8c1SVincenzo Maffione * process is stealing our packets).
3540ff7076bSVincenzo Maffione */
355f92bb8c1SVincenzo Maffione WPRINTF(("netbe_recv: expected %zd bytes, "
356f92bb8c1SVincenzo Maffione "got %zd", plen - prepend_hdr_len, rlen));
357d55e0373SVincenzo Maffione vq_retchains(vq, n_chains);
358f92bb8c1SVincenzo Maffione continue;
3590ff7076bSVincenzo Maffione }
3600ff7076bSVincenzo Maffione
361f92bb8c1SVincenzo Maffione ulen = (uint32_t)plen;
362d55e0373SVincenzo Maffione
36366c662b0SVincenzo Maffione /*
36466c662b0SVincenzo Maffione * Publish the used buffers to the guest, reporting the
36566c662b0SVincenzo Maffione * number of bytes that we wrote.
36666c662b0SVincenzo Maffione */
367d55e0373SVincenzo Maffione if (!sc->rx_merge) {
368d55e0373SVincenzo Maffione vq_relchain(vq, info[0].idx, ulen);
369d55e0373SVincenzo Maffione } else {
370d55e0373SVincenzo Maffione uint32_t iolen;
371d55e0373SVincenzo Maffione int i = 0;
372d55e0373SVincenzo Maffione
373d55e0373SVincenzo Maffione do {
374d55e0373SVincenzo Maffione iolen = info[i].len;
375d55e0373SVincenzo Maffione if (iolen > ulen) {
376d55e0373SVincenzo Maffione iolen = ulen;
377d55e0373SVincenzo Maffione }
378d55e0373SVincenzo Maffione vq_relchain_prepare(vq, info[i].idx, iolen);
379d55e0373SVincenzo Maffione ulen -= iolen;
380d55e0373SVincenzo Maffione i++;
381d55e0373SVincenzo Maffione } while (ulen > 0);
382d55e0373SVincenzo Maffione
383d55e0373SVincenzo Maffione hdr->vrh_bufs = i;
384d55e0373SVincenzo Maffione vq_relchain_publish(vq);
385f92bb8c1SVincenzo Maffione assert(i == n_chains);
386d55e0373SVincenzo Maffione }
3873e11768eSVincenzo Maffione }
3880ff7076bSVincenzo Maffione
3890ff7076bSVincenzo Maffione }
3900ff7076bSVincenzo Maffione
3910ff7076bSVincenzo Maffione /*
3920ff7076bSVincenzo Maffione * Called when there is read activity on the backend file descriptor.
393366f6083SPeter Grehan * Each buffer posted by the guest is assumed to be able to contain
394366f6083SPeter Grehan * an entire ethernet frame + rx header.
395366f6083SPeter Grehan */
396b6020475SGeorge V. Neville-Neil static void
pci_vtnet_rx_callback(int fd __unused,enum ev_type type __unused,void * param)39798d920d9SMark Johnston pci_vtnet_rx_callback(int fd __unused, enum ev_type type __unused, void *param)
398366f6083SPeter Grehan {
399366f6083SPeter Grehan struct pci_vtnet_softc *sc = param;
400366f6083SPeter Grehan
4012a80be7bSNeel Natu pthread_mutex_lock(&sc->rx_mtx);
4020ff7076bSVincenzo Maffione pci_vtnet_rx(sc);
4032a80be7bSNeel Natu pthread_mutex_unlock(&sc->rx_mtx);
404366f6083SPeter Grehan
405366f6083SPeter Grehan }
406366f6083SPeter Grehan
4070ff7076bSVincenzo Maffione /* Called on RX kick. */
408366f6083SPeter Grehan static void
pci_vtnet_ping_rxq(void * vsc,struct vqueue_info * vq)409ba41c3c1SPeter Grehan pci_vtnet_ping_rxq(void *vsc, struct vqueue_info *vq)
410366f6083SPeter Grehan {
411ba41c3c1SPeter Grehan struct pci_vtnet_softc *sc = vsc;
412ba41c3c1SPeter Grehan
413366f6083SPeter Grehan /*
4143e11768eSVincenzo Maffione * A qnotify means that the rx process can now begin.
415ea57b2d7SAleksandr Fedorov * Enable RX only if features are negotiated.
416366f6083SPeter Grehan */
4175c2b348aSVincenzo Maffione pthread_mutex_lock(&sc->rx_mtx);
418ea57b2d7SAleksandr Fedorov if (!sc->features_negotiated) {
419ea57b2d7SAleksandr Fedorov pthread_mutex_unlock(&sc->rx_mtx);
420ea57b2d7SAleksandr Fedorov return;
421ea57b2d7SAleksandr Fedorov }
422ea57b2d7SAleksandr Fedorov
42317e9052cSVincenzo Maffione vq_kick_disable(vq);
4243e11768eSVincenzo Maffione netbe_rx_enable(sc->vsc_be);
4255c2b348aSVincenzo Maffione pthread_mutex_unlock(&sc->rx_mtx);
426366f6083SPeter Grehan }
427366f6083SPeter Grehan
4280ff7076bSVincenzo Maffione /* TX virtqueue processing, called by the TX thread. */
429366f6083SPeter Grehan static void
pci_vtnet_proctx(struct pci_vtnet_softc * sc,struct vqueue_info * vq)430ba41c3c1SPeter Grehan pci_vtnet_proctx(struct pci_vtnet_softc *sc, struct vqueue_info *vq)
431366f6083SPeter Grehan {
432366f6083SPeter Grehan struct iovec iov[VTNET_MAXSEGS + 1];
43366c662b0SVincenzo Maffione struct iovec *siov = iov;
434b0139127SKa Ho Ng struct vi_req req;
4350ff7076bSVincenzo Maffione ssize_t len;
4360ff7076bSVincenzo Maffione int n;
437366f6083SPeter Grehan
438366f6083SPeter Grehan /*
4390ff7076bSVincenzo Maffione * Obtain chain of descriptors. The first descriptor also
4400ff7076bSVincenzo Maffione * contains the virtio-net header.
441366f6083SPeter Grehan */
442b0139127SKa Ho Ng n = vq_getchain(vq, iov, VTNET_MAXSEGS, &req);
443ba41c3c1SPeter Grehan assert(n >= 1 && n <= VTNET_MAXSEGS);
4440ff7076bSVincenzo Maffione
44566c662b0SVincenzo Maffione if (sc->vhdrlen != sc->be_vhdrlen) {
44666c662b0SVincenzo Maffione /*
44766c662b0SVincenzo Maffione * The frontend uses a virtio-net header, but the backend
44866c662b0SVincenzo Maffione * does not. We simply strip the header and ignore it, as
44966c662b0SVincenzo Maffione * it should be zero-filled.
45066c662b0SVincenzo Maffione */
45166c662b0SVincenzo Maffione siov = iov_trim_hdr(siov, &n, sc->vhdrlen);
45266c662b0SVincenzo Maffione }
4530ff7076bSVincenzo Maffione
45466c662b0SVincenzo Maffione if (siov == NULL) {
45566c662b0SVincenzo Maffione /* The chain is nonsensical. Just drop it. */
45666c662b0SVincenzo Maffione len = 0;
45766c662b0SVincenzo Maffione } else {
45866c662b0SVincenzo Maffione len = netbe_send(sc->vsc_be, siov, n);
45966c662b0SVincenzo Maffione if (len < 0) {
46066c662b0SVincenzo Maffione /*
46166c662b0SVincenzo Maffione * If send failed, report that 0 bytes
46266c662b0SVincenzo Maffione * were read.
46366c662b0SVincenzo Maffione */
46466c662b0SVincenzo Maffione len = 0;
46566c662b0SVincenzo Maffione }
46666c662b0SVincenzo Maffione }
46766c662b0SVincenzo Maffione
46866c662b0SVincenzo Maffione /*
46966c662b0SVincenzo Maffione * Return the processed chain to the guest, reporting
47066c662b0SVincenzo Maffione * the number of bytes that we read.
47166c662b0SVincenzo Maffione */
472b0139127SKa Ho Ng vq_relchain(vq, req.idx, len);
473366f6083SPeter Grehan }
474366f6083SPeter Grehan
4750ff7076bSVincenzo Maffione /* Called on TX kick. */
476366f6083SPeter Grehan static void
pci_vtnet_ping_txq(void * vsc,struct vqueue_info * vq)477ba41c3c1SPeter Grehan pci_vtnet_ping_txq(void *vsc, struct vqueue_info *vq)
478366f6083SPeter Grehan {
479ba41c3c1SPeter Grehan struct pci_vtnet_softc *sc = vsc;
480366f6083SPeter Grehan
481366f6083SPeter Grehan /*
482ba41c3c1SPeter Grehan * Any ring entries to process?
483366f6083SPeter Grehan */
484ba41c3c1SPeter Grehan if (!vq_has_descs(vq))
485366f6083SPeter Grehan return;
486366f6083SPeter Grehan
487199fee4eSPeter Grehan /* Signal the tx thread for processing */
488199fee4eSPeter Grehan pthread_mutex_lock(&sc->tx_mtx);
48917e9052cSVincenzo Maffione vq_kick_disable(vq);
490199fee4eSPeter Grehan if (sc->tx_in_progress == 0)
491199fee4eSPeter Grehan pthread_cond_signal(&sc->tx_cond);
492199fee4eSPeter Grehan pthread_mutex_unlock(&sc->tx_mtx);
493199fee4eSPeter Grehan }
494199fee4eSPeter Grehan
495366f6083SPeter Grehan /*
496199fee4eSPeter Grehan * Thread which will handle processing of TX desc
497199fee4eSPeter Grehan */
498199fee4eSPeter Grehan static void *
pci_vtnet_tx_thread(void * param)499199fee4eSPeter Grehan pci_vtnet_tx_thread(void *param)
500199fee4eSPeter Grehan {
501ba41c3c1SPeter Grehan struct pci_vtnet_softc *sc = param;
502ba41c3c1SPeter Grehan struct vqueue_info *vq;
50379f1cdb4SAlexander Motin int error;
504199fee4eSPeter Grehan
505ba41c3c1SPeter Grehan vq = &sc->vsc_queues[VTNET_TXQ];
506199fee4eSPeter Grehan
507199fee4eSPeter Grehan /*
508199fee4eSPeter Grehan * Let us wait till the tx queue pointers get initialised &
509199fee4eSPeter Grehan * first tx signaled
510199fee4eSPeter Grehan */
511199fee4eSPeter Grehan pthread_mutex_lock(&sc->tx_mtx);
512199fee4eSPeter Grehan error = pthread_cond_wait(&sc->tx_cond, &sc->tx_mtx);
513199fee4eSPeter Grehan assert(error == 0);
514199fee4eSPeter Grehan
515199fee4eSPeter Grehan for (;;) {
516ba41c3c1SPeter Grehan /* note - tx mutex is locked here */
51779f1cdb4SAlexander Motin while (sc->resetting || !vq_has_descs(vq)) {
51817e9052cSVincenzo Maffione vq_kick_enable(vq);
51979f1cdb4SAlexander Motin if (!sc->resetting && vq_has_descs(vq))
52079f1cdb4SAlexander Motin break;
521199fee4eSPeter Grehan
522199fee4eSPeter Grehan sc->tx_in_progress = 0;
52379f1cdb4SAlexander Motin error = pthread_cond_wait(&sc->tx_cond, &sc->tx_mtx);
524199fee4eSPeter Grehan assert(error == 0);
525199fee4eSPeter Grehan }
52617e9052cSVincenzo Maffione vq_kick_disable(vq);
527199fee4eSPeter Grehan sc->tx_in_progress = 1;
528199fee4eSPeter Grehan pthread_mutex_unlock(&sc->tx_mtx);
529199fee4eSPeter Grehan
530ba41c3c1SPeter Grehan do {
531199fee4eSPeter Grehan /*
532ba41c3c1SPeter Grehan * Run through entries, placing them into
533ba41c3c1SPeter Grehan * iovecs and sending when an end-of-packet
534ba41c3c1SPeter Grehan * is found
535366f6083SPeter Grehan */
536ba41c3c1SPeter Grehan pci_vtnet_proctx(sc, vq);
537ba41c3c1SPeter Grehan } while (vq_has_descs(vq));
538b1f31245SNeel Natu
539b1f31245SNeel Natu /*
540b1f31245SNeel Natu * Generate an interrupt if needed.
541b1f31245SNeel Natu */
5420ff7076bSVincenzo Maffione vq_endchains(vq, /*used_all_avail=*/1);
543ba41c3c1SPeter Grehan
544ba41c3c1SPeter Grehan pthread_mutex_lock(&sc->tx_mtx);
545199fee4eSPeter Grehan }
546366f6083SPeter Grehan }
547366f6083SPeter Grehan
548ba41c3c1SPeter Grehan #ifdef notyet
549366f6083SPeter Grehan static void
pci_vtnet_ping_ctlq(void * vsc,struct vqueue_info * vq)550ba41c3c1SPeter Grehan pci_vtnet_ping_ctlq(void *vsc, struct vqueue_info *vq)
551366f6083SPeter Grehan {
552366f6083SPeter Grehan
553332eff95SVincenzo Maffione DPRINTF(("vtnet: control qnotify!"));
554366f6083SPeter Grehan }
555ba41c3c1SPeter Grehan #endif
556366f6083SPeter Grehan
557a38e2a64SPeter Grehan static int
pci_vtnet_init(struct pci_devinst * pi,nvlist_t * nvl)5586a284cacSJohn Baldwin pci_vtnet_init(struct pci_devinst *pi, nvlist_t *nvl)
559366f6083SPeter Grehan {
560366f6083SPeter Grehan struct pci_vtnet_softc *sc;
561621b5090SJohn Baldwin const char *value;
5620ff7076bSVincenzo Maffione char tname[MAXCOMLEN + 1];
5631ff57e3aSAleksandr Fedorov unsigned long mtu = ETHERMTU;
564621b5090SJohn Baldwin int err;
565366f6083SPeter Grehan
5660ff7076bSVincenzo Maffione /*
5670ff7076bSVincenzo Maffione * Allocate data structures for further virtio initializations.
5680ff7076bSVincenzo Maffione * sc also contains a copy of vtnet_vi_consts, since capabilities
5690ff7076bSVincenzo Maffione * change depending on the backend.
5700ff7076bSVincenzo Maffione */
571994f858aSXin LI sc = calloc(1, sizeof(struct pci_vtnet_softc));
572366f6083SPeter Grehan
5730ff7076bSVincenzo Maffione sc->vsc_consts = vtnet_vi_consts;
574366f6083SPeter Grehan pthread_mutex_init(&sc->vsc_mtx, NULL);
575366f6083SPeter Grehan
576ba41c3c1SPeter Grehan sc->vsc_queues[VTNET_RXQ].vq_qsize = VTNET_RINGSZ;
577ba41c3c1SPeter Grehan sc->vsc_queues[VTNET_RXQ].vq_notify = pci_vtnet_ping_rxq;
578ba41c3c1SPeter Grehan sc->vsc_queues[VTNET_TXQ].vq_qsize = VTNET_RINGSZ;
579ba41c3c1SPeter Grehan sc->vsc_queues[VTNET_TXQ].vq_notify = pci_vtnet_ping_txq;
580ba41c3c1SPeter Grehan #ifdef notyet
581ba41c3c1SPeter Grehan sc->vsc_queues[VTNET_CTLQ].vq_qsize = VTNET_RINGSZ;
582ba41c3c1SPeter Grehan sc->vsc_queues[VTNET_CTLQ].vq_notify = pci_vtnet_ping_ctlq;
583ba41c3c1SPeter Grehan #endif
584ba41c3c1SPeter Grehan
585621b5090SJohn Baldwin value = get_config_value_node(nvl, "mac");
586621b5090SJohn Baldwin if (value != NULL) {
58766c662b0SVincenzo Maffione err = net_parsemac(value, sc->vsc_config.mac);
588621b5090SJohn Baldwin if (err) {
589621b5090SJohn Baldwin free(sc);
590621b5090SJohn Baldwin return (err);
591621b5090SJohn Baldwin }
592621b5090SJohn Baldwin } else
593621b5090SJohn Baldwin net_genmac(pi, sc->vsc_config.mac);
594621b5090SJohn Baldwin
595621b5090SJohn Baldwin value = get_config_value_node(nvl, "mtu");
596621b5090SJohn Baldwin if (value != NULL) {
5971ff57e3aSAleksandr Fedorov err = net_parsemtu(value, &mtu);
598621b5090SJohn Baldwin if (err) {
599621b5090SJohn Baldwin free(sc);
600621b5090SJohn Baldwin return (err);
601621b5090SJohn Baldwin }
6021ff57e3aSAleksandr Fedorov
6031ff57e3aSAleksandr Fedorov if (mtu < VTNET_MIN_MTU || mtu > VTNET_MAX_MTU) {
6041ff57e3aSAleksandr Fedorov err = EINVAL;
6051ff57e3aSAleksandr Fedorov errno = EINVAL;
606c7cb7db8SSean Chittenden free(sc);
607a38e2a64SPeter Grehan return (err);
608a38e2a64SPeter Grehan }
6091ff57e3aSAleksandr Fedorov sc->vsc_consts.vc_hv_caps |= VIRTIO_NET_F_MTU;
6101ff57e3aSAleksandr Fedorov }
611621b5090SJohn Baldwin sc->vsc_config.mtu = mtu;
612621b5090SJohn Baldwin
613621b5090SJohn Baldwin /* Permit interfaces without a configured backend. */
614621b5090SJohn Baldwin if (get_config_value_node(nvl, "backend") != NULL) {
615621b5090SJohn Baldwin err = netbe_init(&sc->vsc_be, nvl, pci_vtnet_rx_callback, sc);
616621b5090SJohn Baldwin if (err) {
617621b5090SJohn Baldwin free(sc);
618621b5090SJohn Baldwin return (err);
619621b5090SJohn Baldwin }
620621b5090SJohn Baldwin }
621621b5090SJohn Baldwin
622621b5090SJohn Baldwin sc->vsc_consts.vc_hv_caps |= VIRTIO_NET_F_MRG_RXBUF |
623621b5090SJohn Baldwin netbe_get_cap(sc->vsc_be);
6241ff57e3aSAleksandr Fedorov
6251ff57e3aSAleksandr Fedorov /*
6261ff57e3aSAleksandr Fedorov * Since we do not actually support multiqueue,
6271ff57e3aSAleksandr Fedorov * set the maximum virtqueue pairs to 1.
6281ff57e3aSAleksandr Fedorov */
6291ff57e3aSAleksandr Fedorov sc->vsc_config.max_virtqueue_pairs = 1;
6301ff57e3aSAleksandr Fedorov
631366f6083SPeter Grehan /* initialize config space */
632366f6083SPeter Grehan pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_NET);
633366f6083SPeter Grehan pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR);
634366f6083SPeter Grehan pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_NETWORK);
63554ac6f72SKa Ho Ng pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_ID_NETWORK);
636604b5210SPeter Grehan pci_set_cfgdata16(pi, PCIR_SUBVEND_0, VIRTIO_VENDOR);
637c9b4e987SNeel Natu
638621b5090SJohn Baldwin /* Link is always up. */
639621b5090SJohn Baldwin sc->vsc_config.status = 1;
6400ff7076bSVincenzo Maffione
6410ff7076bSVincenzo Maffione vi_softc_linkup(&sc->vsc_vs, &sc->vsc_consts, sc, pi, sc->vsc_queues);
6420ff7076bSVincenzo Maffione sc->vsc_vs.vs_mtx = &sc->vsc_mtx;
643c9b4e987SNeel Natu
644ba41c3c1SPeter Grehan /* use BAR 1 to map MSI-X table and PBA, if we're using MSI-X */
645c7cb7db8SSean Chittenden if (vi_intr_init(&sc->vsc_vs, 1, fbsdrun_virtio_msix())) {
646c7cb7db8SSean Chittenden free(sc);
647c9b4e987SNeel Natu return (1);
648c7cb7db8SSean Chittenden }
649c9b4e987SNeel Natu
650ba41c3c1SPeter Grehan /* use BAR 0 to map config regs in IO space */
651ba41c3c1SPeter Grehan vi_set_io_bar(&sc->vsc_vs, 0);
652366f6083SPeter Grehan
6532a80be7bSNeel Natu sc->resetting = 0;
6542a80be7bSNeel Natu
655d70b2069SVincenzo Maffione sc->rx_merge = 0;
65666c662b0SVincenzo Maffione sc->vhdrlen = sizeof(struct virtio_net_rxhdr) - 2;
6572a80be7bSNeel Natu pthread_mutex_init(&sc->rx_mtx, NULL);
6582a80be7bSNeel Natu
659199fee4eSPeter Grehan /*
660ba41c3c1SPeter Grehan * Initialize tx semaphore & spawn TX processing thread.
661199fee4eSPeter Grehan * As of now, only one thread for TX desc processing is
662199fee4eSPeter Grehan * spawned.
663199fee4eSPeter Grehan */
664199fee4eSPeter Grehan sc->tx_in_progress = 0;
665199fee4eSPeter Grehan pthread_mutex_init(&sc->tx_mtx, NULL);
666199fee4eSPeter Grehan pthread_cond_init(&sc->tx_cond, NULL);
667199fee4eSPeter Grehan pthread_create(&sc->tx_tid, NULL, pci_vtnet_tx_thread, (void *)sc);
6687f5487acSPeter Grehan snprintf(tname, sizeof(tname), "vtnet-%d:%d tx", pi->pi_slot,
6697f5487acSPeter Grehan pi->pi_func);
670199fee4eSPeter Grehan pthread_set_name_np(sc->tx_tid, tname);
671199fee4eSPeter Grehan
672366f6083SPeter Grehan return (0);
673366f6083SPeter Grehan }
674366f6083SPeter Grehan
675ba41c3c1SPeter Grehan static int
pci_vtnet_cfgwrite(void * vsc,int offset,int size,uint32_t value)676ba41c3c1SPeter Grehan pci_vtnet_cfgwrite(void *vsc, int offset, int size, uint32_t value)
677c9b4e987SNeel Natu {
678ba41c3c1SPeter Grehan struct pci_vtnet_softc *sc = vsc;
6796214e48cSPeter Grehan void *ptr;
680366f6083SPeter Grehan
6810ff7076bSVincenzo Maffione if (offset < (int)sizeof(sc->vsc_config.mac)) {
6820ff7076bSVincenzo Maffione assert(offset + size <= (int)sizeof(sc->vsc_config.mac));
683366f6083SPeter Grehan /*
684366f6083SPeter Grehan * The driver is allowed to change the MAC address
685366f6083SPeter Grehan */
686ba41c3c1SPeter Grehan ptr = &sc->vsc_config.mac[offset];
687ba41c3c1SPeter Grehan memcpy(ptr, &value, size);
6886214e48cSPeter Grehan } else {
68982560f19SPeter Grehan /* silently ignore other writes */
690332eff95SVincenzo Maffione DPRINTF(("vtnet: write to readonly reg %d", offset));
6916214e48cSPeter Grehan }
69282560f19SPeter Grehan
693366f6083SPeter Grehan return (0);
694366f6083SPeter Grehan }
695366f6083SPeter Grehan
696ba41c3c1SPeter Grehan static int
pci_vtnet_cfgread(void * vsc,int offset,int size,uint32_t * retval)697ba41c3c1SPeter Grehan pci_vtnet_cfgread(void *vsc, int offset, int size, uint32_t *retval)
698ba41c3c1SPeter Grehan {
699ba41c3c1SPeter Grehan struct pci_vtnet_softc *sc = vsc;
700ba41c3c1SPeter Grehan void *ptr;
701366f6083SPeter Grehan
702ba41c3c1SPeter Grehan ptr = (uint8_t *)&sc->vsc_config + offset;
703ba41c3c1SPeter Grehan memcpy(retval, ptr, size);
704ba41c3c1SPeter Grehan return (0);
705366f6083SPeter Grehan }
706366f6083SPeter Grehan
70782560f19SPeter Grehan static void
pci_vtnet_neg_features(void * vsc,uint64_t negotiated_features)70882560f19SPeter Grehan pci_vtnet_neg_features(void *vsc, uint64_t negotiated_features)
70982560f19SPeter Grehan {
71082560f19SPeter Grehan struct pci_vtnet_softc *sc = vsc;
71182560f19SPeter Grehan
71282560f19SPeter Grehan sc->vsc_features = negotiated_features;
71382560f19SPeter Grehan
714d70b2069SVincenzo Maffione if (negotiated_features & VIRTIO_NET_F_MRG_RXBUF) {
71566c662b0SVincenzo Maffione sc->vhdrlen = sizeof(struct virtio_net_rxhdr);
716d70b2069SVincenzo Maffione sc->rx_merge = 1;
717d70b2069SVincenzo Maffione } else {
718d70b2069SVincenzo Maffione /*
719d70b2069SVincenzo Maffione * Without mergeable rx buffers, virtio-net header is 2
720d70b2069SVincenzo Maffione * bytes shorter than sizeof(struct virtio_net_rxhdr).
721d70b2069SVincenzo Maffione */
72266c662b0SVincenzo Maffione sc->vhdrlen = sizeof(struct virtio_net_rxhdr) - 2;
72382560f19SPeter Grehan sc->rx_merge = 0;
72482560f19SPeter Grehan }
72582560f19SPeter Grehan
7260ff7076bSVincenzo Maffione /* Tell the backend to enable some capabilities it has advertised. */
72766c662b0SVincenzo Maffione netbe_set_cap(sc->vsc_be, negotiated_features, sc->vhdrlen);
72866c662b0SVincenzo Maffione sc->be_vhdrlen = netbe_get_vnet_hdr_len(sc->vsc_be);
72966c662b0SVincenzo Maffione assert(sc->be_vhdrlen == 0 || sc->be_vhdrlen == sc->vhdrlen);
730ea57b2d7SAleksandr Fedorov
731ea57b2d7SAleksandr Fedorov pthread_mutex_lock(&sc->rx_mtx);
732ea57b2d7SAleksandr Fedorov sc->features_negotiated = true;
733ea57b2d7SAleksandr Fedorov pthread_mutex_unlock(&sc->rx_mtx);
7340ff7076bSVincenzo Maffione }
7350ff7076bSVincenzo Maffione
736483d953aSJohn Baldwin #ifdef BHYVE_SNAPSHOT
737483d953aSJohn Baldwin static void
pci_vtnet_pause(void * vsc)738483d953aSJohn Baldwin pci_vtnet_pause(void *vsc)
739483d953aSJohn Baldwin {
740483d953aSJohn Baldwin struct pci_vtnet_softc *sc = vsc;
741483d953aSJohn Baldwin
742483d953aSJohn Baldwin DPRINTF(("vtnet: device pause requested !\n"));
743483d953aSJohn Baldwin
744483d953aSJohn Baldwin /* Acquire the RX lock to block RX processing. */
745483d953aSJohn Baldwin pthread_mutex_lock(&sc->rx_mtx);
746483d953aSJohn Baldwin
747483d953aSJohn Baldwin /* Wait for the transmit thread to finish its processing. */
748483d953aSJohn Baldwin pthread_mutex_lock(&sc->tx_mtx);
749483d953aSJohn Baldwin while (sc->tx_in_progress) {
750483d953aSJohn Baldwin pthread_mutex_unlock(&sc->tx_mtx);
751483d953aSJohn Baldwin usleep(10000);
752483d953aSJohn Baldwin pthread_mutex_lock(&sc->tx_mtx);
753483d953aSJohn Baldwin }
754483d953aSJohn Baldwin }
755483d953aSJohn Baldwin
756483d953aSJohn Baldwin static void
pci_vtnet_resume(void * vsc)757483d953aSJohn Baldwin pci_vtnet_resume(void *vsc)
758483d953aSJohn Baldwin {
759483d953aSJohn Baldwin struct pci_vtnet_softc *sc = vsc;
760483d953aSJohn Baldwin
761483d953aSJohn Baldwin DPRINTF(("vtnet: device resume requested !\n"));
762483d953aSJohn Baldwin
763483d953aSJohn Baldwin pthread_mutex_unlock(&sc->tx_mtx);
764483d953aSJohn Baldwin /* The RX lock should have been acquired in vtnet_pause. */
765483d953aSJohn Baldwin pthread_mutex_unlock(&sc->rx_mtx);
766483d953aSJohn Baldwin }
767483d953aSJohn Baldwin
768483d953aSJohn Baldwin static int
pci_vtnet_snapshot(void * vsc,struct vm_snapshot_meta * meta)769483d953aSJohn Baldwin pci_vtnet_snapshot(void *vsc, struct vm_snapshot_meta *meta)
770483d953aSJohn Baldwin {
771483d953aSJohn Baldwin int ret;
772483d953aSJohn Baldwin struct pci_vtnet_softc *sc = vsc;
773483d953aSJohn Baldwin
774483d953aSJohn Baldwin DPRINTF(("vtnet: device snapshot requested !\n"));
775483d953aSJohn Baldwin
776483d953aSJohn Baldwin /*
777483d953aSJohn Baldwin * Queues and consts should have been saved by the more generic
778483d953aSJohn Baldwin * vi_pci_snapshot function. We need to save only our features and
779483d953aSJohn Baldwin * config.
780483d953aSJohn Baldwin */
781483d953aSJohn Baldwin
782483d953aSJohn Baldwin SNAPSHOT_VAR_OR_LEAVE(sc->vsc_features, meta, ret, done);
7833b5e5ce8SVitaliy Gusev SNAPSHOT_VAR_OR_LEAVE(sc->features_negotiated, meta, ret, done);
784483d953aSJohn Baldwin
785*0dc159ceSElyes Haouas /* Force reapply negotiated features at restore time */
7863b5e5ce8SVitaliy Gusev if (meta->op == VM_SNAPSHOT_RESTORE &&
7873b5e5ce8SVitaliy Gusev sc->features_negotiated) {
788483d953aSJohn Baldwin pci_vtnet_neg_features(sc, sc->vsc_features);
789483d953aSJohn Baldwin netbe_rx_enable(sc->vsc_be);
790483d953aSJohn Baldwin }
791483d953aSJohn Baldwin
792483d953aSJohn Baldwin SNAPSHOT_VAR_OR_LEAVE(sc->vsc_config, meta, ret, done);
793483d953aSJohn Baldwin SNAPSHOT_VAR_OR_LEAVE(sc->rx_merge, meta, ret, done);
794483d953aSJohn Baldwin
795483d953aSJohn Baldwin SNAPSHOT_VAR_OR_LEAVE(sc->vhdrlen, meta, ret, done);
796483d953aSJohn Baldwin SNAPSHOT_VAR_OR_LEAVE(sc->be_vhdrlen, meta, ret, done);
797483d953aSJohn Baldwin
798483d953aSJohn Baldwin done:
799483d953aSJohn Baldwin return (ret);
800483d953aSJohn Baldwin }
801483d953aSJohn Baldwin #endif
802483d953aSJohn Baldwin
80337045dfaSMark Johnston static const struct pci_devemu pci_de_vnet = {
804366f6083SPeter Grehan .pe_emu = "virtio-net",
805366f6083SPeter Grehan .pe_init = pci_vtnet_init,
806621b5090SJohn Baldwin .pe_legacy_config = netbe_legacy_config,
807ba41c3c1SPeter Grehan .pe_barwrite = vi_pci_write,
808483d953aSJohn Baldwin .pe_barread = vi_pci_read,
809483d953aSJohn Baldwin #ifdef BHYVE_SNAPSHOT
810483d953aSJohn Baldwin .pe_snapshot = vi_pci_snapshot,
811483d953aSJohn Baldwin .pe_pause = vi_pci_pause,
812483d953aSJohn Baldwin .pe_resume = vi_pci_resume,
813483d953aSJohn Baldwin #endif
814366f6083SPeter Grehan };
815366f6083SPeter Grehan PCI_EMUL_SET(pci_de_vnet);
816