2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011 NetApp, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/linker_set.h>
36 #include <sys/select.h>
38 #include <sys/ioctl.h>
39 #include <net/ethernet.h>
40 #include <net/if.h> /* IFNAMSIZ */
53 #include <pthread_np.h>
59 #include "net_utils.h"
60 #include "net_backends.h"
62 #define VTNET_RINGSZ 1024
64 #define VTNET_MAXSEGS 256
66 #define VTNET_S_HOSTCAPS \
67 ( VIRTIO_NET_F_MAC | VIRTIO_NET_F_STATUS | \
68 VIRTIO_F_NOTIFY_ON_EMPTY | VIRTIO_RING_F_INDIRECT_DESC)
71 * PCI config-space "registers"
73 struct virtio_net_config {
83 #define VTNET_CTLQ 2 /* NB: not yet supported */
90 static int pci_vtnet_debug;
91 #define DPRINTF(params) if (pci_vtnet_debug) printf params
92 #define WPRINTF(params) printf params
97 struct pci_vtnet_softc {
98 struct virtio_softc vsc_vs;
99 struct vqueue_info vsc_queues[VTNET_MAXQ - 1];
100 pthread_mutex_t vsc_mtx;
102 net_backend_t *vsc_be;
105 int resetting; /* protected by tx_mtx */
107 uint64_t vsc_features; /* negotiated features */
109 pthread_mutex_t rx_mtx;
110 unsigned int rx_vhdrlen;
111 int rx_merge; /* merged rx bufs in use */
114 pthread_mutex_t tx_mtx;
115 pthread_cond_t tx_cond;
118 struct virtio_net_config vsc_config;
119 struct virtio_consts vsc_consts;
122 static void pci_vtnet_reset(void *);
123 /* static void pci_vtnet_notify(void *, struct vqueue_info *); */
124 static int pci_vtnet_cfgread(void *, int, int, uint32_t *);
125 static int pci_vtnet_cfgwrite(void *, int, int, uint32_t);
126 static void pci_vtnet_neg_features(void *, uint64_t);
128 static struct virtio_consts vtnet_vi_consts = {
129 "vtnet", /* our name */
130 VTNET_MAXQ - 1, /* we currently support 2 virtqueues */
131 sizeof(struct virtio_net_config), /* config reg size */
132 pci_vtnet_reset, /* reset */
133 NULL, /* device-wide qnotify -- not used */
134 pci_vtnet_cfgread, /* read PCI config */
135 pci_vtnet_cfgwrite, /* write PCI config */
136 pci_vtnet_neg_features, /* apply negotiated features */
137 VTNET_S_HOSTCAPS, /* our capabilities */
141 pci_vtnet_reset(void *vsc)
143 struct pci_vtnet_softc *sc = vsc;
145 DPRINTF(("vtnet: device reset requested !\n"));
147 /* Acquire the RX lock to block RX processing. */
148 pthread_mutex_lock(&sc->rx_mtx);
150 /* Set sc->resetting and give a chance to the TX thread to stop. */
151 pthread_mutex_lock(&sc->tx_mtx);
153 while (sc->tx_in_progress) {
154 pthread_mutex_unlock(&sc->tx_mtx);
156 pthread_mutex_lock(&sc->tx_mtx);
159 sc->vsc_rx_ready = 0;
161 sc->rx_vhdrlen = sizeof(struct virtio_net_rxhdr);
164 * Now reset rings, MSI-X vectors, and negotiated capabilities.
165 * Do that with the TX lock held, since we need to reset
168 vi_reset_dev(&sc->vsc_vs);
171 pthread_mutex_unlock(&sc->tx_mtx);
172 pthread_mutex_unlock(&sc->rx_mtx);
176 pci_vtnet_rx(struct pci_vtnet_softc *sc)
178 struct iovec iov[VTNET_MAXSEGS + 1];
179 struct vqueue_info *vq;
183 if (!sc->vsc_rx_ready) {
185 * The rx ring has not yet been set up.
186 * Drop the packet and try later.
188 netbe_rx_discard(sc->vsc_be);
193 * Check for available rx buffers
195 vq = &sc->vsc_queues[VTNET_RXQ];
196 if (!vq_has_descs(vq)) {
198 * No available rx buffers. Drop the packet and try later.
199 * Interrupt on empty, if that's negotiated.
201 netbe_rx_discard(sc->vsc_be);
202 vq_endchains(vq, /*used_all_avail=*/1);
208 * Get descriptor chain.
210 n = vq_getchain(vq, &idx, iov, VTNET_MAXSEGS, NULL);
211 assert(n >= 1 && n <= VTNET_MAXSEGS);
213 len = netbe_recv(sc->vsc_be, iov, n);
217 * No more packets (len == 0), or backend errored
218 * (err < 0). Return unused available buffers.
221 /* Interrupt if needed/appropriate and stop. */
222 vq_endchains(vq, /*used_all_avail=*/0);
226 /* Publish the info to the guest */
227 vq_relchain(vq, idx, (uint32_t)len);
228 } while (vq_has_descs(vq));
230 /* Interrupt if needed, including for NOTIFY_ON_EMPTY. */
231 vq_endchains(vq, /*used_all_avail=*/1);
235 * Called when there is read activity on the backend file descriptor.
236 * Each buffer posted by the guest is assumed to be able to contain
237 * an entire ethernet frame + rx header.
240 pci_vtnet_rx_callback(int fd, enum ev_type type, void *param)
242 struct pci_vtnet_softc *sc = param;
244 pthread_mutex_lock(&sc->rx_mtx);
246 pthread_mutex_unlock(&sc->rx_mtx);
250 /* Called on RX kick. */
252 pci_vtnet_ping_rxq(void *vsc, struct vqueue_info *vq)
254 struct pci_vtnet_softc *sc = vsc;
257 * A qnotify means that the rx process can now begin
259 pthread_mutex_lock(&sc->rx_mtx);
260 if (sc->vsc_rx_ready == 0) {
261 sc->vsc_rx_ready = 1;
264 pthread_mutex_unlock(&sc->rx_mtx);
267 /* TX virtqueue processing, called by the TX thread. */
269 pci_vtnet_proctx(struct pci_vtnet_softc *sc, struct vqueue_info *vq)
271 struct iovec iov[VTNET_MAXSEGS + 1];
277 * Obtain chain of descriptors. The first descriptor also
278 * contains the virtio-net header.
280 n = vq_getchain(vq, &idx, iov, VTNET_MAXSEGS, NULL);
281 assert(n >= 1 && n <= VTNET_MAXSEGS);
283 len = netbe_send(sc->vsc_be, iov, n);
285 /* chain is processed, release it and set len */
286 vq_relchain(vq, idx, len > 0 ? len : 0);
289 /* Called on TX kick. */
291 pci_vtnet_ping_txq(void *vsc, struct vqueue_info *vq)
293 struct pci_vtnet_softc *sc = vsc;
296 * Any ring entries to process?
298 if (!vq_has_descs(vq))
301 /* Signal the tx thread for processing */
302 pthread_mutex_lock(&sc->tx_mtx);
304 if (sc->tx_in_progress == 0)
305 pthread_cond_signal(&sc->tx_cond);
306 pthread_mutex_unlock(&sc->tx_mtx);
310 * Thread which will handle processing of TX desc
313 pci_vtnet_tx_thread(void *param)
315 struct pci_vtnet_softc *sc = param;
316 struct vqueue_info *vq;
319 vq = &sc->vsc_queues[VTNET_TXQ];
322 * Let us wait till the tx queue pointers get initialised &
325 pthread_mutex_lock(&sc->tx_mtx);
326 error = pthread_cond_wait(&sc->tx_cond, &sc->tx_mtx);
330 /* note - tx mutex is locked here */
331 while (sc->resetting || !vq_has_descs(vq)) {
333 if (!sc->resetting && vq_has_descs(vq))
336 sc->tx_in_progress = 0;
337 error = pthread_cond_wait(&sc->tx_cond, &sc->tx_mtx);
341 sc->tx_in_progress = 1;
342 pthread_mutex_unlock(&sc->tx_mtx);
346 * Run through entries, placing them into
347 * iovecs and sending when an end-of-packet
350 pci_vtnet_proctx(sc, vq);
351 } while (vq_has_descs(vq));
354 * Generate an interrupt if needed.
356 vq_endchains(vq, /*used_all_avail=*/1);
358 pthread_mutex_lock(&sc->tx_mtx);
364 pci_vtnet_ping_ctlq(void *vsc, struct vqueue_info *vq)
367 DPRINTF(("vtnet: control qnotify!\n\r"));
372 pci_vtnet_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
374 struct pci_vtnet_softc *sc;
375 char tname[MAXCOMLEN + 1];
379 * Allocate data structures for further virtio initializations.
380 * sc also contains a copy of vtnet_vi_consts, since capabilities
381 * change depending on the backend.
383 sc = calloc(1, sizeof(struct pci_vtnet_softc));
385 sc->vsc_consts = vtnet_vi_consts;
386 pthread_mutex_init(&sc->vsc_mtx, NULL);
388 sc->vsc_queues[VTNET_RXQ].vq_qsize = VTNET_RINGSZ;
389 sc->vsc_queues[VTNET_RXQ].vq_notify = pci_vtnet_ping_rxq;
390 sc->vsc_queues[VTNET_TXQ].vq_qsize = VTNET_RINGSZ;
391 sc->vsc_queues[VTNET_TXQ].vq_notify = pci_vtnet_ping_txq;
393 sc->vsc_queues[VTNET_CTLQ].vq_qsize = VTNET_RINGSZ;
394 sc->vsc_queues[VTNET_CTLQ].vq_notify = pci_vtnet_ping_ctlq;
398 * Attempt to open the backend device and read the MAC address
407 devname = vtopts = strdup(opts);
408 (void) strsep(&vtopts, ",");
410 if (vtopts != NULL) {
411 err = net_parsemac(vtopts, sc->vsc_config.mac);
419 err = netbe_init(&sc->vsc_be, devname, pci_vtnet_rx_callback,
424 sc->vsc_consts.vc_hv_caps |= netbe_get_cap(sc->vsc_be);
428 net_genmac(pi, sc->vsc_config.mac);
431 /* initialize config space */
432 pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_NET);
433 pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR);
434 pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_NETWORK);
435 pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_TYPE_NET);
436 pci_set_cfgdata16(pi, PCIR_SUBVEND_0, VIRTIO_VENDOR);
438 /* Link is up if we managed to open backend device. */
439 sc->vsc_config.status = (opts == NULL || sc->vsc_be);
441 vi_softc_linkup(&sc->vsc_vs, &sc->vsc_consts, sc, pi, sc->vsc_queues);
442 sc->vsc_vs.vs_mtx = &sc->vsc_mtx;
444 /* use BAR 1 to map MSI-X table and PBA, if we're using MSI-X */
445 if (vi_intr_init(&sc->vsc_vs, 1, fbsdrun_virtio_msix()))
448 /* use BAR 0 to map config regs in IO space */
449 vi_set_io_bar(&sc->vsc_vs, 0);
454 sc->rx_vhdrlen = sizeof(struct virtio_net_rxhdr);
455 pthread_mutex_init(&sc->rx_mtx, NULL);
458 * Initialize tx semaphore & spawn TX processing thread.
459 * As of now, only one thread for TX desc processing is
462 sc->tx_in_progress = 0;
463 pthread_mutex_init(&sc->tx_mtx, NULL);
464 pthread_cond_init(&sc->tx_cond, NULL);
465 pthread_create(&sc->tx_tid, NULL, pci_vtnet_tx_thread, (void *)sc);
466 snprintf(tname, sizeof(tname), "vtnet-%d:%d tx", pi->pi_slot,
468 pthread_set_name_np(sc->tx_tid, tname);
474 pci_vtnet_cfgwrite(void *vsc, int offset, int size, uint32_t value)
476 struct pci_vtnet_softc *sc = vsc;
479 if (offset < (int)sizeof(sc->vsc_config.mac)) {
480 assert(offset + size <= (int)sizeof(sc->vsc_config.mac));
482 * The driver is allowed to change the MAC address
484 ptr = &sc->vsc_config.mac[offset];
485 memcpy(ptr, &value, size);
487 /* silently ignore other writes */
488 DPRINTF(("vtnet: write to readonly reg %d\n\r", offset));
495 pci_vtnet_cfgread(void *vsc, int offset, int size, uint32_t *retval)
497 struct pci_vtnet_softc *sc = vsc;
500 ptr = (uint8_t *)&sc->vsc_config + offset;
501 memcpy(retval, ptr, size);
506 pci_vtnet_neg_features(void *vsc, uint64_t negotiated_features)
508 struct pci_vtnet_softc *sc = vsc;
510 sc->vsc_features = negotiated_features;
512 if (!(negotiated_features & VIRTIO_NET_F_MRG_RXBUF)) {
514 /* Without mergeable rx buffers, virtio-net header is 2
515 * bytes shorter than sizeof(struct virtio_net_rxhdr). */
516 sc->rx_vhdrlen = sizeof(struct virtio_net_rxhdr) - 2;
519 /* Tell the backend to enable some capabilities it has advertised. */
520 netbe_set_cap(sc->vsc_be, negotiated_features, sc->rx_vhdrlen);
523 static struct pci_devemu pci_de_vnet = {
524 .pe_emu = "virtio-net",
525 .pe_init = pci_vtnet_init,
526 .pe_barwrite = vi_pci_write,
527 .pe_barread = vi_pci_read
529 PCI_EMUL_SET(pci_de_vnet);