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 #ifndef WITHOUT_CAPSICUM
36 #include <sys/capsicum.h>
38 #include <sys/linker_set.h>
39 #include <sys/select.h>
41 #include <sys/ioctl.h>
42 #include <machine/atomic.h>
43 #include <net/ethernet.h>
44 #ifndef NETMAP_WITH_LIBS
45 #define NETMAP_WITH_LIBS
47 #include <net/netmap_user.h>
61 #include <pthread_np.h>
69 #define VTNET_RINGSZ 1024
71 #define VTNET_MAXSEGS 256
74 * Host capabilities. Note that we only offer a few of these.
76 #define VIRTIO_NET_F_CSUM (1 << 0) /* host handles partial cksum */
77 #define VIRTIO_NET_F_GUEST_CSUM (1 << 1) /* guest handles partial cksum */
78 #define VIRTIO_NET_F_MAC (1 << 5) /* host supplies MAC */
79 #define VIRTIO_NET_F_GSO_DEPREC (1 << 6) /* deprecated: host handles GSO */
80 #define VIRTIO_NET_F_GUEST_TSO4 (1 << 7) /* guest can rcv TSOv4 */
81 #define VIRTIO_NET_F_GUEST_TSO6 (1 << 8) /* guest can rcv TSOv6 */
82 #define VIRTIO_NET_F_GUEST_ECN (1 << 9) /* guest can rcv TSO with ECN */
83 #define VIRTIO_NET_F_GUEST_UFO (1 << 10) /* guest can rcv UFO */
84 #define VIRTIO_NET_F_HOST_TSO4 (1 << 11) /* host can rcv TSOv4 */
85 #define VIRTIO_NET_F_HOST_TSO6 (1 << 12) /* host can rcv TSOv6 */
86 #define VIRTIO_NET_F_HOST_ECN (1 << 13) /* host can rcv TSO with ECN */
87 #define VIRTIO_NET_F_HOST_UFO (1 << 14) /* host can rcv UFO */
88 #define VIRTIO_NET_F_MRG_RXBUF (1 << 15) /* host can merge RX buffers */
89 #define VIRTIO_NET_F_STATUS (1 << 16) /* config status field available */
90 #define VIRTIO_NET_F_CTRL_VQ (1 << 17) /* control channel available */
91 #define VIRTIO_NET_F_CTRL_RX (1 << 18) /* control channel RX mode support */
92 #define VIRTIO_NET_F_CTRL_VLAN (1 << 19) /* control channel VLAN filtering */
93 #define VIRTIO_NET_F_GUEST_ANNOUNCE \
94 (1 << 21) /* guest can send gratuitous pkts */
96 #define VTNET_S_HOSTCAPS \
97 ( VIRTIO_NET_F_MAC | VIRTIO_NET_F_MRG_RXBUF | VIRTIO_NET_F_STATUS | \
98 VIRTIO_F_NOTIFY_ON_EMPTY | VIRTIO_RING_F_INDIRECT_DESC)
101 * PCI config-space "registers"
103 struct virtio_net_config {
113 #define VTNET_CTLQ 2 /* NB: not yet supported */
118 * Fixed network header size
120 struct virtio_net_rxhdr {
122 uint8_t vrh_gso_type;
123 uint16_t vrh_hdr_len;
124 uint16_t vrh_gso_size;
125 uint16_t vrh_csum_start;
126 uint16_t vrh_csum_offset;
133 static int pci_vtnet_debug;
134 #define DPRINTF(params) if (pci_vtnet_debug) printf params
135 #define WPRINTF(params) printf params
140 struct pci_vtnet_softc {
141 struct virtio_softc vsc_vs;
142 struct vqueue_info vsc_queues[VTNET_MAXQ - 1];
143 pthread_mutex_t vsc_mtx;
144 struct mevent *vsc_mevp;
147 struct nm_desc *vsc_nmd;
150 int resetting; /* protected by tx_mtx */
152 uint64_t vsc_features; /* negotiated features */
154 struct virtio_net_config vsc_config;
156 pthread_mutex_t rx_mtx;
158 int rx_merge; /* merged rx bufs in use */
161 pthread_mutex_t tx_mtx;
162 pthread_cond_t tx_cond;
165 void (*pci_vtnet_rx)(struct pci_vtnet_softc *sc);
166 void (*pci_vtnet_tx)(struct pci_vtnet_softc *sc, struct iovec *iov,
167 int iovcnt, int len);
170 static void pci_vtnet_reset(void *);
171 /* static void pci_vtnet_notify(void *, struct vqueue_info *); */
172 static int pci_vtnet_cfgread(void *, int, int, uint32_t *);
173 static int pci_vtnet_cfgwrite(void *, int, int, uint32_t);
174 static void pci_vtnet_neg_features(void *, uint64_t);
176 static struct virtio_consts vtnet_vi_consts = {
177 "vtnet", /* our name */
178 VTNET_MAXQ - 1, /* we currently support 2 virtqueues */
179 sizeof(struct virtio_net_config), /* config reg size */
180 pci_vtnet_reset, /* reset */
181 NULL, /* device-wide qnotify -- not used */
182 pci_vtnet_cfgread, /* read PCI config */
183 pci_vtnet_cfgwrite, /* write PCI config */
184 pci_vtnet_neg_features, /* apply negotiated features */
185 VTNET_S_HOSTCAPS, /* our capabilities */
189 pci_vtnet_reset(void *vsc)
191 struct pci_vtnet_softc *sc = vsc;
193 DPRINTF(("vtnet: device reset requested !\n"));
195 /* Acquire the RX lock to block RX processing. */
196 pthread_mutex_lock(&sc->rx_mtx);
198 /* Set sc->resetting and give a chance to the TX thread to stop. */
199 pthread_mutex_lock(&sc->tx_mtx);
201 while (sc->tx_in_progress) {
202 pthread_mutex_unlock(&sc->tx_mtx);
204 pthread_mutex_lock(&sc->tx_mtx);
207 sc->vsc_rx_ready = 0;
209 sc->rx_vhdrlen = sizeof(struct virtio_net_rxhdr);
212 * Now reset rings, MSI-X vectors, and negotiated capabilities.
213 * Do that with the TX lock held, since we need to reset
216 vi_reset_dev(&sc->vsc_vs);
219 pthread_mutex_unlock(&sc->tx_mtx);
220 pthread_mutex_unlock(&sc->rx_mtx);
224 * Called to send a buffer chain out to the tap device
227 pci_vtnet_tap_tx(struct pci_vtnet_softc *sc, struct iovec *iov, int iovcnt,
230 static char pad[60]; /* all zero bytes */
232 if (sc->vsc_tapfd == -1)
236 * If the length is < 60, pad out to that and add the
237 * extra zero'd segment to the iov. It is guaranteed that
238 * there is always an extra iov available by the caller.
241 iov[iovcnt].iov_base = pad;
242 iov[iovcnt].iov_len = 60 - len;
245 (void) writev(sc->vsc_tapfd, iov, iovcnt);
249 * Called when there is read activity on the tap file descriptor.
250 * Each buffer posted by the guest is assumed to be able to contain
251 * an entire ethernet frame + rx header.
252 * MP note: the dummybuf is only used for discarding frames, so there
253 * is no need for it to be per-vtnet or locked.
255 static uint8_t dummybuf[2048];
257 static __inline struct iovec *
258 rx_iov_trim(struct iovec *iov, int *niov, int tlen)
262 /* XXX short-cut: assume first segment is >= tlen */
263 assert(iov[0].iov_len >= tlen);
265 iov[0].iov_len -= tlen;
266 if (iov[0].iov_len == 0) {
271 iov[0].iov_base = (void *)((uintptr_t)iov[0].iov_base + tlen);
279 pci_vtnet_tap_rx(struct pci_vtnet_softc *sc)
281 struct iovec iov[VTNET_MAXSEGS], *riov;
282 struct vqueue_info *vq;
288 * Should never be called without a valid tap fd
290 assert(sc->vsc_tapfd != -1);
293 * But, will be called when the rx ring hasn't yet
296 if (!sc->vsc_rx_ready) {
298 * Drop the packet and try later.
300 (void) read(sc->vsc_tapfd, dummybuf, sizeof(dummybuf));
305 * Check for available rx buffers
307 vq = &sc->vsc_queues[VTNET_RXQ];
308 if (!vq_has_descs(vq)) {
310 * Drop the packet and try later. Interrupt on
311 * empty, if that's negotiated.
313 (void) read(sc->vsc_tapfd, dummybuf, sizeof(dummybuf));
320 * Get descriptor chain.
322 n = vq_getchain(vq, &idx, iov, VTNET_MAXSEGS, NULL);
323 assert(n >= 1 && n <= VTNET_MAXSEGS);
326 * Get a pointer to the rx header, and use the
327 * data immediately following it for the packet buffer.
329 vrx = iov[0].iov_base;
330 riov = rx_iov_trim(iov, &n, sc->rx_vhdrlen);
332 len = readv(sc->vsc_tapfd, riov, n);
334 if (len < 0 && errno == EWOULDBLOCK) {
336 * No more packets, but still some avail ring
337 * entries. Interrupt if needed/appropriate.
345 * The only valid field in the rx packet header is the
346 * number of buffers if merged rx bufs were negotiated.
348 memset(vrx, 0, sc->rx_vhdrlen);
351 struct virtio_net_rxhdr *vrxh;
358 * Release this chain and handle more chains.
360 vq_relchain(vq, idx, len + sc->rx_vhdrlen);
361 } while (vq_has_descs(vq));
363 /* Interrupt if needed, including for NOTIFY_ON_EMPTY. */
368 pci_vtnet_netmap_writev(struct nm_desc *nmd, struct iovec *iov, int iovcnt)
373 for (r = nmd->cur_tx_ring; ; ) {
374 struct netmap_ring *ring = NETMAP_TXRING(nmd->nifp, r);
378 if (nm_ring_empty(ring)) {
380 if (r > nmd->last_tx_ring)
381 r = nmd->first_tx_ring;
382 if (r == nmd->cur_tx_ring)
387 idx = ring->slot[cur].buf_idx;
388 buf = NETMAP_BUF(ring, idx);
390 for (i = 0; i < iovcnt; i++) {
391 if (len + iov[i].iov_len > 2048)
393 memcpy(&buf[len], iov[i].iov_base, iov[i].iov_len);
394 len += iov[i].iov_len;
396 ring->slot[cur].len = len;
397 ring->head = ring->cur = nm_ring_next(ring, cur);
398 nmd->cur_tx_ring = r;
399 ioctl(nmd->fd, NIOCTXSYNC, NULL);
407 pci_vtnet_netmap_readv(struct nm_desc *nmd, struct iovec *iov, int iovcnt)
413 for (r = nmd->cur_rx_ring; ; ) {
414 struct netmap_ring *ring = NETMAP_RXRING(nmd->nifp, r);
419 if (nm_ring_empty(ring)) {
421 if (r > nmd->last_rx_ring)
422 r = nmd->first_rx_ring;
423 if (r == nmd->cur_rx_ring)
428 idx = ring->slot[cur].buf_idx;
429 buf = NETMAP_BUF(ring, idx);
430 left = ring->slot[cur].len;
432 for (i = 0; i < iovcnt && left > 0; i++) {
433 if (iov[i].iov_len > left)
434 iov[i].iov_len = left;
435 memcpy(iov[i].iov_base, &buf[len], iov[i].iov_len);
436 len += iov[i].iov_len;
437 left -= iov[i].iov_len;
439 ring->head = ring->cur = nm_ring_next(ring, cur);
440 nmd->cur_rx_ring = r;
441 ioctl(nmd->fd, NIOCRXSYNC, NULL);
444 for (; i < iovcnt; i++)
451 * Called to send a buffer chain out to the vale port
454 pci_vtnet_netmap_tx(struct pci_vtnet_softc *sc, struct iovec *iov, int iovcnt,
457 static char pad[60]; /* all zero bytes */
459 if (sc->vsc_nmd == NULL)
463 * If the length is < 60, pad out to that and add the
464 * extra zero'd segment to the iov. It is guaranteed that
465 * there is always an extra iov available by the caller.
468 iov[iovcnt].iov_base = pad;
469 iov[iovcnt].iov_len = 60 - len;
472 (void) pci_vtnet_netmap_writev(sc->vsc_nmd, iov, iovcnt);
476 pci_vtnet_netmap_rx(struct pci_vtnet_softc *sc)
478 struct iovec iov[VTNET_MAXSEGS], *riov;
479 struct vqueue_info *vq;
485 * Should never be called without a valid netmap descriptor
487 assert(sc->vsc_nmd != NULL);
490 * But, will be called when the rx ring hasn't yet
493 if (!sc->vsc_rx_ready) {
495 * Drop the packet and try later.
497 (void) nm_nextpkt(sc->vsc_nmd, (void *)dummybuf);
502 * Check for available rx buffers
504 vq = &sc->vsc_queues[VTNET_RXQ];
505 if (!vq_has_descs(vq)) {
507 * Drop the packet and try later. Interrupt on
508 * empty, if that's negotiated.
510 (void) nm_nextpkt(sc->vsc_nmd, (void *)dummybuf);
517 * Get descriptor chain.
519 n = vq_getchain(vq, &idx, iov, VTNET_MAXSEGS, NULL);
520 assert(n >= 1 && n <= VTNET_MAXSEGS);
523 * Get a pointer to the rx header, and use the
524 * data immediately following it for the packet buffer.
526 vrx = iov[0].iov_base;
527 riov = rx_iov_trim(iov, &n, sc->rx_vhdrlen);
529 len = pci_vtnet_netmap_readv(sc->vsc_nmd, riov, n);
533 * No more packets, but still some avail ring
534 * entries. Interrupt if needed/appropriate.
542 * The only valid field in the rx packet header is the
543 * number of buffers if merged rx bufs were negotiated.
545 memset(vrx, 0, sc->rx_vhdrlen);
548 struct virtio_net_rxhdr *vrxh;
555 * Release this chain and handle more chains.
557 vq_relchain(vq, idx, len + sc->rx_vhdrlen);
558 } while (vq_has_descs(vq));
560 /* Interrupt if needed, including for NOTIFY_ON_EMPTY. */
565 pci_vtnet_rx_callback(int fd, enum ev_type type, void *param)
567 struct pci_vtnet_softc *sc = param;
569 pthread_mutex_lock(&sc->rx_mtx);
570 sc->pci_vtnet_rx(sc);
571 pthread_mutex_unlock(&sc->rx_mtx);
576 pci_vtnet_ping_rxq(void *vsc, struct vqueue_info *vq)
578 struct pci_vtnet_softc *sc = vsc;
581 * A qnotify means that the rx process can now begin
583 if (sc->vsc_rx_ready == 0) {
584 sc->vsc_rx_ready = 1;
585 vq->vq_used->vu_flags |= VRING_USED_F_NO_NOTIFY;
590 pci_vtnet_proctx(struct pci_vtnet_softc *sc, struct vqueue_info *vq)
592 struct iovec iov[VTNET_MAXSEGS + 1];
598 * Obtain chain of descriptors. The first one is
599 * really the header descriptor, so we need to sum
600 * up two lengths: packet length and transfer length.
602 n = vq_getchain(vq, &idx, iov, VTNET_MAXSEGS, NULL);
603 assert(n >= 1 && n <= VTNET_MAXSEGS);
605 tlen = iov[0].iov_len;
606 for (i = 1; i < n; i++) {
607 plen += iov[i].iov_len;
608 tlen += iov[i].iov_len;
611 DPRINTF(("virtio: packet send, %d bytes, %d segs\n\r", plen, n));
612 sc->pci_vtnet_tx(sc, &iov[1], n - 1, plen);
614 /* chain is processed, release it and set tlen */
615 vq_relchain(vq, idx, tlen);
619 pci_vtnet_ping_txq(void *vsc, struct vqueue_info *vq)
621 struct pci_vtnet_softc *sc = vsc;
624 * Any ring entries to process?
626 if (!vq_has_descs(vq))
629 /* Signal the tx thread for processing */
630 pthread_mutex_lock(&sc->tx_mtx);
631 vq->vq_used->vu_flags |= VRING_USED_F_NO_NOTIFY;
632 if (sc->tx_in_progress == 0)
633 pthread_cond_signal(&sc->tx_cond);
634 pthread_mutex_unlock(&sc->tx_mtx);
638 * Thread which will handle processing of TX desc
641 pci_vtnet_tx_thread(void *param)
643 struct pci_vtnet_softc *sc = param;
644 struct vqueue_info *vq;
647 vq = &sc->vsc_queues[VTNET_TXQ];
650 * Let us wait till the tx queue pointers get initialised &
653 pthread_mutex_lock(&sc->tx_mtx);
654 error = pthread_cond_wait(&sc->tx_cond, &sc->tx_mtx);
658 /* note - tx mutex is locked here */
659 while (sc->resetting || !vq_has_descs(vq)) {
660 vq->vq_used->vu_flags &= ~VRING_USED_F_NO_NOTIFY;
662 if (!sc->resetting && vq_has_descs(vq))
665 sc->tx_in_progress = 0;
666 error = pthread_cond_wait(&sc->tx_cond, &sc->tx_mtx);
669 vq->vq_used->vu_flags |= VRING_USED_F_NO_NOTIFY;
670 sc->tx_in_progress = 1;
671 pthread_mutex_unlock(&sc->tx_mtx);
675 * Run through entries, placing them into
676 * iovecs and sending when an end-of-packet
679 pci_vtnet_proctx(sc, vq);
680 } while (vq_has_descs(vq));
683 * Generate an interrupt if needed.
687 pthread_mutex_lock(&sc->tx_mtx);
693 pci_vtnet_ping_ctlq(void *vsc, struct vqueue_info *vq)
696 DPRINTF(("vtnet: control qnotify!\n\r"));
701 pci_vtnet_parsemac(char *mac_str, uint8_t *mac_addr)
703 struct ether_addr *ea;
705 char zero_addr[ETHER_ADDR_LEN] = { 0, 0, 0, 0, 0, 0 };
707 tmpstr = strsep(&mac_str,"=");
709 if ((mac_str != NULL) && (!strcmp(tmpstr,"mac"))) {
710 ea = ether_aton(mac_str);
712 if (ea == NULL || ETHER_IS_MULTICAST(ea->octet) ||
713 memcmp(ea->octet, zero_addr, ETHER_ADDR_LEN) == 0) {
714 fprintf(stderr, "Invalid MAC %s\n", mac_str);
717 memcpy(mac_addr, ea->octet, ETHER_ADDR_LEN);
724 pci_vtnet_tap_setup(struct pci_vtnet_softc *sc, char *devname)
727 #ifndef WITHOUT_CAPSICUM
731 strcpy(tbuf, "/dev/");
732 strlcat(tbuf, devname, sizeof(tbuf));
734 sc->pci_vtnet_rx = pci_vtnet_tap_rx;
735 sc->pci_vtnet_tx = pci_vtnet_tap_tx;
737 sc->vsc_tapfd = open(tbuf, O_RDWR);
738 if (sc->vsc_tapfd == -1) {
739 WPRINTF(("open of tap device %s failed\n", tbuf));
744 * Set non-blocking and register for read
745 * notifications with the event loop
748 if (ioctl(sc->vsc_tapfd, FIONBIO, &opt) < 0) {
749 WPRINTF(("tap device O_NONBLOCK failed\n"));
750 close(sc->vsc_tapfd);
754 #ifndef WITHOUT_CAPSICUM
755 cap_rights_init(&rights, CAP_EVENT, CAP_READ, CAP_WRITE);
756 if (cap_rights_limit(sc->vsc_tapfd, &rights) == -1 && errno != ENOSYS)
757 errx(EX_OSERR, "Unable to apply rights for sandbox");
760 sc->vsc_mevp = mevent_add(sc->vsc_tapfd,
762 pci_vtnet_rx_callback,
764 if (sc->vsc_mevp == NULL) {
765 WPRINTF(("Could not register event\n"));
766 close(sc->vsc_tapfd);
772 pci_vtnet_netmap_setup(struct pci_vtnet_softc *sc, char *ifname)
774 sc->pci_vtnet_rx = pci_vtnet_netmap_rx;
775 sc->pci_vtnet_tx = pci_vtnet_netmap_tx;
777 sc->vsc_nmd = nm_open(ifname, NULL, 0, 0);
778 if (sc->vsc_nmd == NULL) {
779 WPRINTF(("open of netmap device %s failed\n", ifname));
783 sc->vsc_mevp = mevent_add(sc->vsc_nmd->fd,
785 pci_vtnet_rx_callback,
787 if (sc->vsc_mevp == NULL) {
788 WPRINTF(("Could not register event\n"));
789 nm_close(sc->vsc_nmd);
795 pci_vtnet_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
798 unsigned char digest[16];
800 char tname[MAXCOMLEN + 1];
801 struct pci_vtnet_softc *sc;
806 sc = calloc(1, sizeof(struct pci_vtnet_softc));
808 pthread_mutex_init(&sc->vsc_mtx, NULL);
810 vi_softc_linkup(&sc->vsc_vs, &vtnet_vi_consts, sc, pi, sc->vsc_queues);
811 sc->vsc_vs.vs_mtx = &sc->vsc_mtx;
813 sc->vsc_queues[VTNET_RXQ].vq_qsize = VTNET_RINGSZ;
814 sc->vsc_queues[VTNET_RXQ].vq_notify = pci_vtnet_ping_rxq;
815 sc->vsc_queues[VTNET_TXQ].vq_qsize = VTNET_RINGSZ;
816 sc->vsc_queues[VTNET_TXQ].vq_notify = pci_vtnet_ping_txq;
818 sc->vsc_queues[VTNET_CTLQ].vq_qsize = VTNET_RINGSZ;
819 sc->vsc_queues[VTNET_CTLQ].vq_notify = pci_vtnet_ping_ctlq;
823 * Attempt to open the tap device and read the MAC address
832 devname = vtopts = strdup(opts);
833 (void) strsep(&vtopts, ",");
835 if (vtopts != NULL) {
836 err = pci_vtnet_parsemac(vtopts, sc->vsc_config.mac);
844 if (strncmp(devname, "vale", 4) == 0)
845 pci_vtnet_netmap_setup(sc, devname);
846 if (strncmp(devname, "tap", 3) == 0 ||
847 strncmp(devname, "vmnet", 5) == 0)
848 pci_vtnet_tap_setup(sc, devname);
854 * The default MAC address is the standard NetApp OUI of 00-a0-98,
855 * followed by an MD5 of the PCI slot/func number and dev name
858 snprintf(nstr, sizeof(nstr), "%d-%d-%s", pi->pi_slot,
859 pi->pi_func, vmname);
862 MD5Update(&mdctx, nstr, strlen(nstr));
863 MD5Final(digest, &mdctx);
865 sc->vsc_config.mac[0] = 0x00;
866 sc->vsc_config.mac[1] = 0xa0;
867 sc->vsc_config.mac[2] = 0x98;
868 sc->vsc_config.mac[3] = digest[0];
869 sc->vsc_config.mac[4] = digest[1];
870 sc->vsc_config.mac[5] = digest[2];
873 /* initialize config space */
874 pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_NET);
875 pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR);
876 pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_NETWORK);
877 pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_TYPE_NET);
878 pci_set_cfgdata16(pi, PCIR_SUBVEND_0, VIRTIO_VENDOR);
880 /* Link is up if we managed to open tap device or vale port. */
881 sc->vsc_config.status = (opts == NULL || sc->vsc_tapfd >= 0 ||
882 sc->vsc_nmd != NULL);
884 /* use BAR 1 to map MSI-X table and PBA, if we're using MSI-X */
885 if (vi_intr_init(&sc->vsc_vs, 1, fbsdrun_virtio_msix()))
888 /* use BAR 0 to map config regs in IO space */
889 vi_set_io_bar(&sc->vsc_vs, 0);
894 sc->rx_vhdrlen = sizeof(struct virtio_net_rxhdr);
895 pthread_mutex_init(&sc->rx_mtx, NULL);
898 * Initialize tx semaphore & spawn TX processing thread.
899 * As of now, only one thread for TX desc processing is
902 sc->tx_in_progress = 0;
903 pthread_mutex_init(&sc->tx_mtx, NULL);
904 pthread_cond_init(&sc->tx_cond, NULL);
905 pthread_create(&sc->tx_tid, NULL, pci_vtnet_tx_thread, (void *)sc);
906 snprintf(tname, sizeof(tname), "vtnet-%d:%d tx", pi->pi_slot,
908 pthread_set_name_np(sc->tx_tid, tname);
914 pci_vtnet_cfgwrite(void *vsc, int offset, int size, uint32_t value)
916 struct pci_vtnet_softc *sc = vsc;
920 assert(offset + size <= 6);
922 * The driver is allowed to change the MAC address
924 ptr = &sc->vsc_config.mac[offset];
925 memcpy(ptr, &value, size);
927 /* silently ignore other writes */
928 DPRINTF(("vtnet: write to readonly reg %d\n\r", offset));
935 pci_vtnet_cfgread(void *vsc, int offset, int size, uint32_t *retval)
937 struct pci_vtnet_softc *sc = vsc;
940 ptr = (uint8_t *)&sc->vsc_config + offset;
941 memcpy(retval, ptr, size);
946 pci_vtnet_neg_features(void *vsc, uint64_t negotiated_features)
948 struct pci_vtnet_softc *sc = vsc;
950 sc->vsc_features = negotiated_features;
952 if (!(sc->vsc_features & VIRTIO_NET_F_MRG_RXBUF)) {
954 /* non-merge rx header is 2 bytes shorter */
959 struct pci_devemu pci_de_vnet = {
960 .pe_emu = "virtio-net",
961 .pe_init = pci_vtnet_init,
962 .pe_barwrite = vi_pci_write,
963 .pe_barread = vi_pci_read
965 PCI_EMUL_SET(pci_de_vnet);