Page MenuHomeFreeBSD

D62044.1795687216.diff
No OneTemporary

Authored By
Unknown
Size
52 KB
Referenced Files
None
Subscribers
None

D62044.1795687216.diff

diff --git a/usr.sbin/bhyve/Makefile b/usr.sbin/bhyve/Makefile
--- a/usr.sbin/bhyve/Makefile
+++ b/usr.sbin/bhyve/Makefile
@@ -45,6 +45,7 @@
pci_emul.c \
pci_hda.c \
pci_hostbridge.c \
+ pci_igb.c \
pci_irq.c \
pci_nvme.c \
pci_uart.c \
diff --git a/usr.sbin/bhyve/bhyve.8 b/usr.sbin/bhyve/bhyve.8
--- a/usr.sbin/bhyve/bhyve.8
+++ b/usr.sbin/bhyve/bhyve.8
@@ -26,7 +26,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.Dd November 19, 2026
+.Dd November 25, 2026
.Dt BHYVE 8
.Os
.Sh NAME
@@ -519,6 +519,8 @@
AHCI controller attached to a SATA hard drive.
.It Cm e1000
Intel e82545 network interface.
+.It Cm igb
+Intel 82576 network interface.
.It Cm uart
PCI 16550 serial device.
.It Cm lpc
diff --git a/usr.sbin/bhyve/bhyve_config.5 b/usr.sbin/bhyve/bhyve_config.5
--- a/usr.sbin/bhyve/bhyve_config.5
+++ b/usr.sbin/bhyve/bhyve_config.5
@@ -24,7 +24,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.Dd September 17, 2026
+.Dd November 25, 2026
.Dt BHYVE_CONFIG 5
.Os
.Sh NAME
@@ -343,6 +343,8 @@
AHCI storage controller.
.It Li e1000
Intel e82545 network interface.
+.It Li igb
+Intel 82576 network interface.
.It Li fbuf
VGA framebuffer device attached to VNC server.
.It Li lpc
@@ -590,6 +592,26 @@
the device name.
If specified, it must be a unicast MAC address.
.El
+.Ss igb Settings
+In addition to the network backend settings,
+Intel 82576 network interfaces support the following variables:
+.Bl -column "Name" "MAC address" "generated"
+.It Sy Name Ta Sy Format Ta Sy Default Ta Sy Description
+.It Va mac Ta MAC address Ta generated Ta
+MAC address.
+If not specified, the MAC address is derived from a fixed OUI, and the
+remaining bytes from an MD5 hash of the slot and function numbers and
+the device name.
+If specified, it must be a unicast MAC address.
+.It Va link Ta string Ta up Ta
+The state of the link when the guest starts:
+.Li up
+or
+.Li down .
+.It Va msix Ta bool Ta true Ta
+Offer MSI-X to the guest.
+Without it the guest uses MSI or INTx and a single pair of queues.
+.El
.Ss Frame Buffer Settings
.Bl -column "password" "unix:my/unix.sock" "127.0.0.1:5900"
.It Sy Name Ta Sy Format Ta Sy Default Ta Sy Description
diff --git a/usr.sbin/bhyve/pci_igb.c b/usr.sbin/bhyve/pci_igb.c
new file mode 100644
--- /dev/null
+++ b/usr.sbin/bhyve/pci_igb.c
@@ -0,0 +1,1782 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2026 Wanpeng Qian <wanpengqian@gmail.com>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * An Intel 82576 Gigabit Ethernet controller (8086:10c9, the "igb" family),
+ * written from Intel's 82576EB data sheet and from what the driver in
+ * sys/dev/e1000 does with the chip. Windows 8 and later have a driver for it
+ * in the box, and unlike the 82545 (pci_e82545.c) it has several queues,
+ * MSI-X and interrupt moderation per vector:
+ *
+ * -s N,igb,tapN[,mac=xx:xx:xx:xx:xx:xx][,link=down][,msix=false]
+ *
+ * What is here: the registers in BAR 0 (128 KB, memory), reset, the NVM
+ * through EERD, the internal PHY through MDIC, 16 receive and 16 transmit
+ * queues with legacy and advanced descriptors, transmit head write-back,
+ * receive side scaling (MRQC, RETA, RSSRK: the Toeplitz hash picks the
+ * queue), the address filters (RAR, MTA, VLAN), checksum insertion and TCP
+ * segmentation in software, verification of received IPv4, TCP and UDP
+ * checksums when RXCSUM asks for it, the interrupts (INTx or MSI with
+ * ICR/IMS; MSI-X with 10 vectors, IVAR, EICR/EIMS/EIAC/EIAM; the table is
+ * BAR 3), EITR, the statistics, and snapshots with pause and resume.
+ *
+ * The link is up (1000 Mb/s, full duplex) unless the guest powers the PHY
+ * down or the device was started with link=down; a change raises LSC.
+ *
+ * Not there: SR-IOV, VMDq, IEEE 1588, flow control, wake-up, manageability.
+ * Their registers, like every register the device does not know, are plain
+ * storage: a driver reads back what it wrote.
+ *
+ * The device's state is in struct igb_state; the descriptor rings are in
+ * the guest's memory. Frames go out on the device's transmit thread, which
+ * the guest wakes by writing a queue's TDT; they come in on the event
+ * thread. A frame for whose queue the guest has no descriptor waits, and
+ * the backend with it, until the guest gives some, at most 10 ms if the
+ * queue's SRRCTL says to drop; then it is dropped (MPC). A packet is taken
+ * from a transmit ring only when all of its descriptors are there, so nothing
+ * of a half-read packet has to be kept across a snapshot.
+ */
+
+#include <sys/param.h>
+#include <sys/uio.h>
+
+#include <net/ethernet.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+
+#include <machine/vmm_snapshot.h>
+
+#include <dev/pci/pcireg.h>
+
+#include <errno.h>
+#include <pthread.h>
+#include <pthread_np.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <time.h>
+#include <unistd.h>
+
+/* e1000_82575.h is written with the driver's types. */
+struct e1000_hw;
+typedef int32_t s32;
+typedef uint8_t u8;
+typedef uint16_t u16;
+typedef uint32_t u32;
+typedef uint64_t u64;
+typedef uint32_t __le32;
+typedef uint64_t __le64;
+
+#include "e1000_regs.h"
+#include "e1000_defines.h"
+#include "e1000_82575.h"
+
+#include "bhyverun.h"
+#include "config.h"
+#include "debug.h"
+#include "mevent.h"
+#include "net_backends.h"
+#include "net_utils.h"
+#include "pci_emul.h"
+
+#define IGB_BAR_SIZE 0x20000
+#define IGB_NREGS (IGB_BAR_SIZE / 4)
+#define IGB_NQ 16
+#define IGB_NCTX 8 /* a context descriptor's IDX: 3 bits */
+#define IGB_NVM_WORDS 256
+#define IGB_MAXFRAME 16384
+#define IGB_MINFRAME (ETHER_MIN_LEN - ETHER_CRC_LEN)
+#define IGB_VLAN_HDR_LEN (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN)
+#define IGB_TXBUF (65536 + 1024) /* a TSO packet and its headers */
+#define IGB_TX_MAXDESC 128 /* descriptors of one packet */
+#define IGB_STATE_VERSION 2
+/* A full ring with SRRCTL.DROP_EN: how long a frame waits for room. */
+#define IGB_RX_HOLD_NS 10000000
+#define IGB_MSIX_BAR 3
+#define IGB_MSIX_VECS 10
+
+/*
+ * The registers and their bits have the names of the driver's headers. What
+ * follows is what those headers do not have.
+ */
+
+/*
+ * A queue's registers, 0x40 bytes for each queue. This is the 82576's place
+ * for them, and where the device keeps them; the headers' macros give the
+ * 82575's place for the first four queues (igb_canon()).
+ */
+#define IGB_RXQ_BASE 0x0c000
+#define IGB_TXQ_BASE 0x0e000
+#define Q_BAL 0x00
+#define Q_BAH 0x04
+#define Q_LEN 0x08
+#define Q_SRRCTL 0x0c /* receive */
+#define Q_HEAD 0x10
+#define Q_TAIL 0x18
+#define Q_DCTL 0x28
+#define Q_WBAL 0x38 /* head write-back; bit 0: on */
+#define Q_WBAH 0x3c
+#define RXQ(q, r) (IGB_RXQ_BASE + (q) * 0x40 + (r))
+#define TXQ(q, r) (IGB_TXQ_BASE + (q) * 0x40 + (r))
+
+/* The statistics are E1000_CRCERRS up to here; reading clears them. */
+#define IGB_STATS_END 0x04140
+
+#define IGB_EECD_SIZE (2 << E1000_EECD_SIZE_EX_SHIFT) /* 256 words */
+#define IGB_MDIC_INT_EN 0x20000000
+#define IGB_ICR_MDAC 0x00000200 /* an MDIC access completed */
+#define IGB_EITR_USEC(v) (((v) >> 2) & 0x1fff)
+#define IGB_PHY_SR_UP 0x796d /* link up, autonegotiation complete */
+#define IGB_PHY_SR_DOWN 0x7949
+#define IGB_PHY_PORT_STATUS 0x11 /* the IGP's own status register */
+
+/* An advanced transmit descriptor: fields without a name in the headers. */
+#define IGB_ADVTXD_DTYP_MASK 0x00f00000
+#define IGB_ADVTXD_POPTS_SHIFT 8 /* in the fourth word */
+#define IGB_ADVTXD_TUCMD_SHIFT 9 /* in a context's third word */
+#define IGB_ADV_TUCMD_L4T_MASK 0x0c
+
+/* 1: resets, link changes and what is odd; 2: every register access too. */
+static int igb_debug = 0;
+#define DPRINTF(...) do { \
+ if (igb_debug) \
+ EPRINTLN("igb: " __VA_ARGS__); \
+} while (0)
+
+/* An advanced context descriptor, as the guest wrote it. */
+struct igb_txctx {
+ uint32_t vlan_macip;
+ uint32_t seqnum;
+ uint32_t tucmd;
+ uint32_t mss_l4len;
+};
+
+/* What goes into a snapshot (of regs: igb_snap_ranges). */
+struct igb_state {
+ uint32_t lintr; /* INTx is asserted */
+ /* The cable is in (link=down at the start: it is not). */
+ uint32_t cable;
+ /* The link is up: the cable is in and the PHY has power. */
+ uint32_t link;
+ uint32_t regs[IGB_NREGS];
+ uint16_t nvm[IGB_NVM_WORDS];
+ uint16_t phy[32];
+ struct igb_txctx ctx[IGB_NQ][IGB_NCTX];
+};
+
+struct igb_rxpkt { /* a received frame, ready for its ring */
+ uint8_t *p;
+ size_t len;
+ uint32_t hash, info;
+ uint16_t vlan;
+ /* E1000_RXD_STAT_*CS and E1000_RXD_ERR_* of the frame. */
+ uint8_t csum, cerr;
+ int vp, q, bcast, mcast;
+};
+
+struct igb_softc {
+ struct pci_devinst *pi;
+ pthread_mutex_t mtx;
+ /* For the transmit thread, and for who waits for it. */
+ pthread_cond_t tx_cond;
+ /* For the timer thread (CLOCK_MONOTONIC). */
+ pthread_cond_t tmr_cond;
+ pthread_t tx_tid, tmr_tid;
+ net_backend_t *be;
+ /* EITR: no interrupt of the vector before. */
+ uint64_t itr_next[IGB_MSIX_VECS];
+ /* Vectors with a cause that wait for that time. */
+ uint32_t itr_pending;
+ /* A frame that waits for descriptors, in rxbuf. */
+ struct igb_rxpkt held;
+ int rx_held;
+ /* 0: until the guest gives descriptors. */
+ uint64_t rx_hold_until;
+ int paused;
+ int tx_kick;
+ /* The transmit thread is sending, without the lock. */
+ int tx_busy;
+ /* Its: a packet from a ring, a TSO segment of it. */
+ uint8_t *txbuf, *segbuf;
+ /* Resets so far: a ring from before one is gone. */
+ uint32_t epoch;
+ uint8_t mac[6];
+ uint8_t *rxbuf; /* the event thread's */
+ struct igb_state s;
+};
+
+#define REG(sc, off) ((sc)->s.regs[(off) >> 2])
+
+static void
+igb_stat(struct igb_softc *sc, uint32_t off, uint64_t n)
+{
+ REG(sc, off) += n;
+}
+
+/* A 64-bit octet counter: the low word, then the high one. */
+static void
+igb_stat64(struct igb_softc *sc, uint32_t off, uint64_t n)
+{
+ uint64_t v;
+
+ v = (REG(sc, off) | (uint64_t)REG(sc, off + 4) << 32) + n;
+ REG(sc, off) = v;
+ REG(sc, off + 4) = v >> 32;
+}
+
+/*
+ * The interrupt: ICR's causes that IMS lets through. With MSI a message goes
+ * out for every new cause; INTx stays asserted until the guest has cleared or
+ * masked them all.
+ */
+static void
+igb_irq_update(struct igb_softc *sc, uint32_t newbits)
+{
+ int want = (REG(sc, E1000_ICR) & REG(sc, E1000_IMS)) != 0;
+
+ if (pci_msi_enabled(sc->pi)) {
+ if (sc->s.lintr) {
+ pci_lintr_deassert(sc->pi);
+ sc->s.lintr = 0;
+ }
+ if (newbits & REG(sc, E1000_IMS))
+ pci_generate_msi(sc->pi, 0);
+ return;
+ }
+ if (want && !sc->s.lintr)
+ pci_lintr_assert(sc->pi);
+ else if (!want && sc->s.lintr)
+ pci_lintr_deassert(sc->pi);
+ sc->s.lintr = want;
+}
+
+/*
+ * MSI-X: EICR's bit n is vector n. A bit that EIMS lets through sends the
+ * message; EIAC then clears the cause and, with GPIE.EIAME, EIAM masks the
+ * vector until the driver unmasks it.
+ */
+static int
+igb_msix(struct igb_softc *sc)
+{
+ return (pci_msix_enabled(sc->pi) &&
+ (REG(sc, E1000_GPIE) & E1000_GPIE_MSIX_MODE));
+}
+
+static uint64_t
+igb_now(void)
+{
+ struct timespec ts;
+
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ return ((uint64_t)ts.tv_sec * 1000000000 + ts.tv_nsec);
+}
+
+static void
+igb_msix_deliver(struct igb_softc *sc, int v, uint64_t now)
+{
+ uint32_t bit = 1u << v;
+
+ pci_generate_msix(sc->pi, v);
+ REG(sc, E1000_EICR) &= ~(bit & REG(sc, E1000_EIAC));
+ if (REG(sc, E1000_GPIE) & E1000_GPIE_EIAME)
+ REG(sc, E1000_EIMS) &= ~(bit & REG(sc, E1000_EIAM));
+ sc->itr_pending &= ~bit;
+ /*
+ * EITR: the next interrupt of this vector is that many microseconds
+ * away at least.
+ */
+ sc->itr_next[v] = now +
+ (uint64_t)IGB_EITR_USEC(REG(sc, E1000_EITR(v))) * 1000;
+}
+
+static void
+igb_eicr_set(struct igb_softc *sc, uint32_t bits)
+{
+ uint32_t fire;
+ uint64_t now;
+ int v, wake = 0;
+
+ REG(sc, E1000_EICR) |= bits;
+ fire = REG(sc, E1000_EICR) & REG(sc, E1000_EIMS);
+ if (fire == 0 || !igb_msix(sc))
+ return;
+ now = igb_now();
+ for (v = 0; v < IGB_MSIX_VECS; v++) {
+ if (!(fire & (1u << v)))
+ continue;
+ if (now >= sc->itr_next[v])
+ igb_msix_deliver(sc, v, now);
+ else if (!(sc->itr_pending & (1u << v))) {
+ /* The timer thread sends it. */
+ sc->itr_pending |= 1u << v;
+ wake = 1;
+ }
+ }
+ if (wake)
+ pthread_cond_signal(&sc->tmr_cond);
+}
+
+static void
+igb_cause(struct igb_softc *sc, uint32_t bits)
+{
+ uint32_t iv;
+
+ REG(sc, E1000_ICR) |= bits;
+ if (igb_msix(sc)) {
+ iv = REG(sc, E1000_IVAR_MISC) >> 8;
+ if ((bits & REG(sc, E1000_IMS)) && (iv & E1000_IVAR_VALID))
+ igb_eicr_set(sc, 1u << (iv & 0x1f));
+ return;
+ }
+ igb_irq_update(sc, bits);
+}
+
+/* Queue q has received (rx) or sent: its vector with MSI-X, else ICR. */
+static void
+igb_cause_q(struct igb_softc *sc, int rx, int q)
+{
+ uint32_t iv;
+
+ if (!igb_msix(sc)) {
+ igb_cause(sc, rx ? E1000_ICR_RXT0 : E1000_ICR_TXDW);
+ return;
+ }
+ REG(sc, E1000_ICR) |= rx ? E1000_ICR_RXT0 : E1000_ICR_TXDW;
+ iv = REG(sc, E1000_IVAR0 + (q & 7) * 4) >>
+ ((q < 8 ? 0 : 16) + (rx ? 0 : 8));
+ if (iv & E1000_IVAR_VALID)
+ igb_eicr_set(sc, 1u << (iv & 0x1f));
+}
+
+/*
+ * The NVM: the words the data sheet's map names, the rest erased, and the
+ * checksum that makes words 0x00-0x3f add up to 0xbaba.
+ */
+static void
+igb_nvm_init(struct igb_softc *sc)
+{
+ uint16_t *w = sc->s.nvm, sum;
+ int i;
+
+ for (i = 0; i < IGB_NVM_WORDS; i++)
+ w[i] = 0xffff;
+ w[0x00] = sc->mac[0] | sc->mac[1] << 8;
+ w[0x01] = sc->mac[2] | sc->mac[3] << 8;
+ w[0x02] = sc->mac[4] | sc->mac[5] << 8;
+ w[0x03] = 0x0000; /* compatibility */
+ w[0x04] = 0xffff; /* the LEDs: the driver's defaults */
+ w[0x05] = 0x1000; /* the image's version */
+ /* The board's part number, "825760-001". */
+ w[NVM_PBA_OFFSET_0] = 0x8257;
+ w[NVM_PBA_OFFSET_1] = 0x6001;
+ w[0x0a] = 0x0000; /* initialization control 1 */
+ w[NVM_SUB_DEV_ID] = pci_get_cfgdata16(sc->pi, PCIR_SUBDEV_0);
+ w[NVM_SUB_VEN_ID] = pci_get_cfgdata16(sc->pi, PCIR_SUBVEND_0);
+ w[NVM_DEV_ID] = pci_get_cfgdata16(sc->pi, PCIR_DEVICE);
+ w[NVM_VEN_ID] = pci_get_cfgdata16(sc->pi, PCIR_VENDOR);
+ /* Initialization control 2: no flow control by default. */
+ w[NVM_INIT_CONTROL2_REG] = 0x0000;
+ w[0x24] = 0x0000; /* initialization control 3, port A */
+ w[NVM_ALT_MAC_ADDR_PTR] = 0xffff; /* no alternate MAC address */
+ for (sum = 0, i = 0; i < NVM_CHECKSUM_REG; i++)
+ sum += w[i];
+ w[NVM_CHECKSUM_REG] = NVM_SUM - sum;
+}
+
+/*
+ * The internal PHY, at address 1: an IGP with its link up and the negotiation
+ * done.
+ */
+static void
+igb_phy_init(struct igb_softc *sc)
+{
+ uint16_t *p = sc->s.phy;
+
+ memset(p, 0, sizeof(sc->s.phy));
+ /* Control: autonegotiation, full duplex, 1000. */
+ p[PHY_CONTROL] = 0x1140;
+ p[PHY_STATUS] = IGB_PHY_SR_DOWN; /* igb_link_update() */
+ p[PHY_ID1] = IGP04E1000_E_PHY_ID >> 16;
+ p[PHY_ID2] = IGP04E1000_E_PHY_ID & 0xffff;
+ p[PHY_AUTONEG_ADV] = 0x0de1; /* what we advertise */
+ p[PHY_LP_ABILITY] = 0xcde1; /* and the partner */
+ p[PHY_AUTONEG_EXP] = 0x0007;
+ p[PHY_1000T_CTRL] = 0x0e00;
+ p[PHY_EXT_STATUS] = 0x3000;
+}
+
+/*
+ * The link is up when the cable is in and the PHY has power. A change shows in
+ * STATUS and in the PHY's registers, and raises LSC (not in a reset: quiet).
+ */
+static void
+igb_link_update(struct igb_softc *sc, int quiet)
+{
+ uint16_t *p = sc->s.phy;
+ uint32_t up = sc->s.cable && !(p[PHY_CONTROL] & MII_CR_POWER_DOWN);
+
+ p[PHY_STATUS] = up ? IGB_PHY_SR_UP : IGB_PHY_SR_DOWN;
+ /* 1000BASE-T status: both receivers fine, the partner does 1000. */
+ p[PHY_1000T_STATUS] = up ? 0x7c00 : 0;
+ /* The IGP's port status: 1000, full duplex, link. */
+ p[IGB_PHY_PORT_STATUS] = up ? 0xc600 : 0;
+ if (up == sc->s.link)
+ return;
+ sc->s.link = up;
+ DPRINTF("link %s", up ? "up" : "down");
+ if (!quiet)
+ igb_cause(sc, E1000_ICR_LSC);
+}
+
+static void
+igb_mdic(struct igb_softc *sc, uint32_t val)
+{
+ int reg = (val & E1000_MDIC_REG_MASK) >> E1000_MDIC_REG_SHIFT;
+ int phy = (val & E1000_MDIC_PHY_MASK) >> E1000_MDIC_PHY_SHIFT;
+
+ val &= ~(E1000_MDIC_READY | E1000_MDIC_ERROR);
+ if (phy != 1)
+ val |= E1000_MDIC_ERROR;
+ else if (val & E1000_MDIC_OP_READ)
+ val = (val & 0xffff0000) | sc->s.phy[reg];
+ else if (val & E1000_MDIC_OP_WRITE) {
+ uint16_t d = val;
+
+ switch (reg) {
+ case PHY_CONTROL:
+ /*
+ * Reset and restart of the negotiation are done at
+ * once.
+ */
+ sc->s.phy[PHY_CONTROL] = d &
+ ~(MII_CR_RESET | MII_CR_RESTART_AUTO_NEG);
+ igb_link_update(sc, 0);
+ break;
+ case PHY_STATUS:
+ case PHY_ID1:
+ case PHY_ID2:
+ case PHY_LP_ABILITY:
+ case PHY_AUTONEG_EXP:
+ case PHY_1000T_STATUS:
+ case PHY_EXT_STATUS:
+ break; /* read only */
+ default:
+ sc->s.phy[reg] = d;
+ }
+ }
+ REG(sc, E1000_MDIC) = val | E1000_MDIC_READY;
+ if (val & IGB_MDIC_INT_EN)
+ igb_cause(sc, IGB_ICR_MDAC);
+}
+
+/* What a reset through CTRL does; hard: bhyve starts. */
+static void
+igb_reset(struct igb_softc *sc, int hard)
+{
+ struct igb_state *s = &sc->s;
+
+ sc->epoch++;
+ memset(s->regs, 0, sizeof(s->regs));
+ memset(s->ctx, 0, sizeof(s->ctx));
+ memset(sc->itr_next, 0, sizeof(sc->itr_next));
+ sc->itr_pending = 0;
+ if (sc->rx_held) {
+ sc->rx_held = 0;
+ if (!sc->paused)
+ netbe_rx_enable(sc->be);
+ }
+ if (hard)
+ igb_nvm_init(sc);
+ igb_phy_init(sc);
+ igb_link_update(sc, 1);
+ REG(sc, E1000_CTRL) = E1000_CTRL_FD | E1000_CTRL_SLU;
+ REG(sc, E1000_EECD) = E1000_EECD_PRES | E1000_EECD_AUTO_RD |
+ IGB_EECD_SIZE;
+ REG(sc, E1000_EEMNGCTL) = E1000_NVM_CFG_DONE_PORT_0;
+ REG(sc, E1000_VET) = ETHERTYPE_VLAN;
+ /*
+ * The chip loads the address from the NVM into the first receive
+ * address.
+ */
+ REG(sc, E1000_RAL(0)) = s->nvm[0] | (uint32_t)s->nvm[1] << 16;
+ REG(sc, E1000_RAH(0)) = s->nvm[2] | E1000_RAH_AV;
+ /* Queue 0 is on after a reset. */
+ REG(sc, RXQ(0, Q_DCTL)) = E1000_RXDCTL_QUEUE_ENABLE;
+ REG(sc, TXQ(0, Q_DCTL)) = E1000_TXDCTL_QUEUE_ENABLE;
+ igb_irq_update(sc, 0);
+}
+
+/*
+ * Checksums: sums of big-endian 16-bit words.
+ */
+static uint32_t
+igb_sum(const uint8_t *p, size_t len, uint32_t sum)
+{
+ for (; len > 1; p += 2, len -= 2)
+ sum += p[0] << 8 | p[1];
+ if (len)
+ sum += p[0] << 8;
+ return (sum);
+}
+
+static uint16_t
+igb_fold(uint32_t sum)
+{
+ while (sum >> 16)
+ sum = (sum & 0xffff) + (sum >> 16);
+ return (sum);
+}
+
+static uint16_t
+igb_get16(const uint8_t *p)
+{
+ return (p[0] << 8 | p[1]);
+}
+
+static void
+igb_put16(uint8_t *p, uint16_t v)
+{
+ p[0] = v >> 8;
+ p[1] = v;
+}
+
+static void
+igb_ip4_checksum(uint8_t *ip, size_t hlen)
+{
+ ip[10] = ip[11] = 0;
+ igb_put16(ip + 10, ~igb_fold(igb_sum(ip, hlen, 0)));
+}
+
+/*
+ * The checksum of what starts at l4 (len bytes), into the field at l4 + off.
+ * The field already holds the pseudo header's sum, as the drivers of this chip
+ * leave it there.
+ */
+static void
+igb_l4_checksum(uint8_t *l4, size_t len, int off, int udp)
+{
+ uint16_t c = ~igb_fold(igb_sum(l4, len, 0));
+
+ if (udp && c == 0)
+ c = 0xffff;
+ igb_put16(l4 + off, c);
+}
+
+/*
+ * Transmit.
+ */
+static void
+igb_send(struct igb_softc *sc, uint8_t *p, size_t len, int vle, uint16_t vlan)
+{
+ static const uint8_t pad[IGB_MINFRAME];
+ uint8_t tag[4];
+ struct iovec iov[4];
+ int n = 0;
+
+ if (len < ETHER_HDR_LEN)
+ return;
+ if (vle) {
+ tag[0] = REG(sc, E1000_VET) >> 8;
+ tag[1] = REG(sc, E1000_VET);
+ tag[2] = vlan >> 8;
+ tag[3] = vlan;
+ iov[n].iov_base = p;
+ iov[n++].iov_len = 12;
+ iov[n].iov_base = tag;
+ iov[n++].iov_len = 4;
+ iov[n].iov_base = p + 12;
+ iov[n++].iov_len = len - 12;
+ len += 4;
+ } else {
+ iov[n].iov_base = p;
+ iov[n++].iov_len = len;
+ }
+ if (len < IGB_MINFRAME) {
+ iov[n].iov_base = __DECONST(void *, pad);
+ iov[n++].iov_len = IGB_MINFRAME - len;
+ }
+ (void)netbe_send(sc->be, iov, n);
+}
+
+/* A packet taken from a ring, for the sending without the lock. */
+struct igb_txpkt {
+ size_t len;
+ struct igb_txctx ctx;
+ uint32_t cmd; /* of all its descriptors */
+ uint32_t olinfo; /* advanced: of the first */
+ int adv;
+ uint8_t cso, css; /* legacy, with E1000_TXD_CMD_IC */
+ uint16_t vlan;
+};
+
+static void
+igb_tx_finish(struct igb_softc *sc, struct igb_txpkt *t)
+{
+ uint8_t *p = sc->txbuf, *s = sc->segbuf, *ip, *l4;
+ size_t maclen, iplen, l4len, hdr, mss, off, chunk, left;
+ int ip4, tcp, udp, vle = (t->cmd & E1000_TXD_CMD_VLE) != 0;
+ uint32_t seq, seed, tucmd, popts;
+ uint16_t id;
+
+ if (!t->adv) {
+ if ((t->cmd & E1000_TXD_CMD_IC) && t->cso + 2u <= t->len &&
+ t->css < t->len)
+ igb_put16(p + t->cso, ~igb_fold(igb_sum(p + t->css,
+ t->len - t->css, 0)));
+ igb_send(sc, p, t->len, vle, t->vlan);
+ return;
+ }
+ iplen = t->ctx.vlan_macip & 0x1ff;
+ maclen = (t->ctx.vlan_macip >> 9) & 0x7f;
+ l4len = (t->ctx.mss_l4len >> 8) & 0xff;
+ mss = t->ctx.mss_l4len >> 16;
+ tucmd = t->ctx.tucmd >> IGB_ADVTXD_TUCMD_SHIFT;
+ ip4 = (tucmd & E1000_ADV_TUCMD_IPV4) != 0;
+ tcp = (tucmd & IGB_ADV_TUCMD_L4T_MASK) == E1000_ADV_TUCMD_L4T_TCP;
+ udp = (tucmd & IGB_ADV_TUCMD_L4T_MASK) == E1000_ADV_TUCMD_L4T_UDP;
+ popts = t->olinfo >> IGB_ADVTXD_POPTS_SHIFT;
+ if (!(t->cmd & E1000_ADVTXD_DCMD_TSE)) {
+ if ((popts & E1000_TXD_POPTS_IXSM) && ip4 && iplen >= 20 &&
+ maclen + iplen <= t->len)
+ igb_ip4_checksum(p + maclen, iplen);
+ if ((popts & E1000_TXD_POPTS_TXSM) && (tcp || udp) &&
+ maclen + iplen + (tcp ? 18 : 8) <= t->len)
+ igb_l4_checksum(p + maclen + iplen,
+ t->len - maclen - iplen, tcp ? 16 : 6, !tcp);
+ igb_send(sc, p, t->len, vle, t->vlan);
+ return;
+ }
+ /*
+ * TCP segmentation: the headers once per segment, with their lengths,
+ * sequence, flags.
+ */
+ hdr = maclen + iplen + l4len;
+ if (!tcp || mss == 0 || l4len < 20 || iplen < (ip4 ? 20u : 40u) ||
+ hdr >= t->len || hdr + mss > IGB_TXBUF) {
+ DPRINTF("TSO dropped: headers %zu+%zu+%zu, mss %zu, %zu bytes",
+ maclen, iplen, l4len, mss, t->len);
+ return;
+ }
+ ip = s + maclen;
+ l4 = ip + iplen;
+ seq = (uint32_t)p[hdr - l4len + 4] << 24 | p[hdr - l4len + 5] << 16 |
+ p[hdr - l4len + 6] << 8 | p[hdr - l4len + 7];
+ id = p[maclen + 4] << 8 | p[maclen + 5];
+ /*
+ * The checksum field holds the pseudo header's sum without the length.
+ */
+ seed = p[hdr - l4len + 16] << 8 | p[hdr - l4len + 17];
+ for (off = 0, left = t->len - hdr; left > 0;
+ off += chunk, left -= chunk) {
+ chunk = MIN(left, mss);
+ memcpy(s, p, hdr);
+ memcpy(s + hdr, p + hdr + off, chunk);
+ if (ip4) {
+ igb_put16(ip + 2, iplen + l4len + chunk);
+ igb_put16(ip + 4, id++);
+ igb_ip4_checksum(ip, iplen);
+ } else
+ igb_put16(ip + 4, iplen - 40 + l4len + chunk);
+ igb_put16(l4 + 4, (seq + off) >> 16);
+ igb_put16(l4 + 6, seq + off);
+ if (left > chunk)
+ /* FIN and PSH only in the last one. */
+ l4[13] &= ~(TH_FIN | TH_PUSH);
+ igb_put16(l4 + 16, igb_fold(seed + l4len + chunk));
+ igb_l4_checksum(l4, l4len + chunk, 16, 0);
+ igb_send(sc, s, hdr + chunk, vle, t->vlan);
+ }
+}
+
+static uint8_t *
+igb_desc(struct igb_softc *sc, uint32_t qbase, uint32_t idx)
+{
+ uint64_t base;
+
+ base = REG(sc, qbase + Q_BAL) | (uint64_t)REG(sc, qbase + Q_BAH) << 32;
+ return (paddr_guest2host(sc->pi->pi_vmctx,
+ base + (uint64_t)idx * 16, 16));
+}
+
+/*
+ * The statistics by a frame's length (with the FCS): 64, -127, -255, -511,
+ * -1023, longer.
+ */
+static int
+igb_len_bucket(size_t len)
+{
+ return (len <= 64 ? 0 : len < 128 ? 1 : len < 256 ? 2 :
+ len < 512 ? 3 : len < 1024 ? 4 : 5);
+}
+
+/*
+ * Takes a context descriptor or a whole packet from the queue's ring; 0: there
+ * is none (yet). Called with the lock. Only the descriptors are read with it:
+ * the packet is copied out of the guest's memory, completed and sent without
+ * the lock (the vCPUs need it for every register), and the descriptors are
+ * given back afterwards. Nobody else touches the transmit rings; after a reset
+ * in between this one is left alone.
+ */
+static int
+igb_tx_one(struct igb_softc *sc, int q)
+{
+ uint32_t qb = TXQ(q, 0), size, head, tail, i, n, w[4], st, epoch;
+ struct {
+ uint64_t addr;
+ uint16_t len;
+ } seg[IGB_TX_MAXDESC];
+ struct igb_txpkt t;
+ uint8_t *d, *b;
+ uint64_t addr;
+ int eop, wb, bad, headwb, link;
+
+ size = REG(sc, qb + Q_LEN) / 16;
+ if (size == 0 || !(REG(sc, E1000_TCTL) & E1000_TCTL_EN) ||
+ !(REG(sc, qb + Q_DCTL) & E1000_TXDCTL_QUEUE_ENABLE))
+ return (0);
+ head = REG(sc, qb + Q_HEAD) % size;
+ tail = REG(sc, qb + Q_TAIL) % size;
+ if (head == tail)
+ return (0);
+ if ((d = igb_desc(sc, qb, head)) == NULL) {
+ DPRINTF("tx queue %d: the ring is not in memory", q);
+ REG(sc, qb + Q_HEAD) = tail;
+ return (0);
+ }
+ memcpy(w, d, 16);
+ if ((w[2] & E1000_TXD_CMD_DEXT) &&
+ (w[2] & IGB_ADVTXD_DTYP_MASK) == E1000_ADVTXD_DTYP_CTXT) {
+ struct igb_txctx *c;
+
+ c = &sc->s.ctx[q][(w[3] >> 4) & (IGB_NCTX - 1)];
+
+ c->vlan_macip = w[0];
+ c->seqnum = w[1];
+ c->tucmd = w[2];
+ c->mss_l4len = w[3];
+ REG(sc, qb + Q_HEAD) = (head + 1) % size;
+ return (1);
+ }
+ /* Is the whole packet there? */
+ for (eop = 0, n = 0, i = head; i != tail && !eop;
+ i = (i + 1) % size, n++) {
+ if ((d = igb_desc(sc, qb, i)) == NULL)
+ break;
+ memcpy(w, d, 16);
+ eop = (w[2] & E1000_TXD_CMD_EOP) != 0;
+ }
+ if (!eop)
+ return (0);
+ memset(&t, 0, sizeof(t));
+ bad = n > IGB_TX_MAXDESC;
+ for (wb = 0, i = 0; i < n; i++) {
+ d = igb_desc(sc, qb, (head + i) % size);
+ memcpy(w, d, 16);
+ if (i == 0) {
+ t.adv = (w[2] & E1000_TXD_CMD_DEXT) != 0;
+ t.olinfo = w[3];
+ t.cso = w[2] >> 16;
+ t.css = w[3] >> 8;
+ if (t.adv)
+ t.ctx = sc->s.ctx[q][(w[3] >> 4) &
+ (IGB_NCTX - 1)];
+ }
+ t.cmd |= w[2];
+ if (!t.adv)
+ t.vlan = w[3] >> 16;
+ if (w[2] & E1000_TXD_CMD_RS)
+ wb = 1;
+ if (!bad) {
+ seg[i].addr = w[0] | (uint64_t)w[1] << 32;
+ seg[i].len = w[2] & 0xffff;
+ }
+ }
+ if (t.adv)
+ t.vlan = t.ctx.vlan_macip >> 16;
+ /*
+ * Head write-back (Windows' driver turns it on): the driver does not
+ * look for DD in the descriptors, the chip stores the ring's head at
+ * the address in TDWBA instead.
+ */
+ headwb = REG(sc, qb + Q_WBAL) & 1;
+ addr = (REG(sc, qb + Q_WBAL) & ~3u) |
+ (uint64_t)REG(sc, qb + Q_WBAH) << 32;
+ link = sc->s.link;
+ epoch = sc->epoch;
+
+ sc->tx_busy = 1;
+ pthread_mutex_unlock(&sc->mtx);
+ for (i = 0; i < n && !bad; i++) {
+ if (seg[i].len == 0)
+ continue;
+ b = paddr_guest2host(sc->pi->pi_vmctx, seg[i].addr, seg[i].len);
+ if (b == NULL || t.len + seg[i].len > IGB_TXBUF)
+ bad = 1;
+ else {
+ memcpy(sc->txbuf + t.len, b, seg[i].len);
+ t.len += seg[i].len;
+ }
+ }
+ if (!bad && link)
+ igb_tx_finish(sc, &t);
+ pthread_mutex_lock(&sc->mtx);
+ sc->tx_busy = 0;
+ pthread_cond_broadcast(&sc->tx_cond);
+
+ if (epoch != sc->epoch)
+ /* Reset meanwhile: the ring and the counters are new. */
+ return (1);
+ if (bad)
+ DPRINTF("tx queue %d: a packet of %u descriptors dropped", q,
+ n);
+ else if (link) {
+ igb_stat(sc, E1000_PTC64 + igb_len_bucket(t.len + 4) * 4, 1);
+ igb_stat(sc, E1000_GPTC, 1);
+ igb_stat(sc, E1000_TPT, 1);
+ igb_stat64(sc, E1000_GOTCL, t.len + 4);
+ igb_stat64(sc, E1000_TOTL, t.len + 4);
+ if (ETHER_IS_MULTICAST(sc->txbuf))
+ igb_stat(sc, ETHER_IS_BROADCAST(sc->txbuf) ?
+ E1000_BPTC : E1000_MPTC, 1);
+ if (t.cmd & E1000_ADVTXD_DCMD_TSE)
+ igb_stat(sc, E1000_TSCTC, 1);
+ }
+ for (i = 0; i < n && !headwb; i++) {
+ if ((d = igb_desc(sc, qb, (head + i) % size)) == NULL)
+ continue;
+ memcpy(w, d, 16);
+ if (!(w[2] & E1000_TXD_CMD_RS))
+ continue;
+ memcpy(&st, d + 12, 4);
+ st |= E1000_TXD_STAT_DD;
+ memcpy(d + 12, &st, 4);
+ }
+ REG(sc, qb + Q_HEAD) = (head + n) % size;
+ if (headwb && wb) {
+ b = paddr_guest2host(sc->pi->pi_vmctx, addr, 4);
+ if (b != NULL)
+ memcpy(b, &REG(sc, qb + Q_HEAD), 4);
+ }
+ if (wb)
+ igb_cause_q(sc, 0, q);
+ return (1);
+}
+
+/*
+ * One thread sends for all the queues. A thread per queue was tried and was
+ * slower: with four streams the threads spent their time waiting for each other
+ * at the backend.
+ */
+static void *
+igb_tx_thread(void *arg)
+{
+ struct igb_softc *sc = arg;
+ int q, did;
+
+ pthread_mutex_lock(&sc->mtx);
+ for (;;) {
+ while (sc->paused || !sc->tx_kick)
+ pthread_cond_wait(&sc->tx_cond, &sc->mtx);
+ sc->tx_kick = 0;
+ do {
+ for (did = 0, q = 0; q < IGB_NQ && !sc->paused; q++)
+ did |= igb_tx_one(sc, q);
+ } while (did && !sc->paused);
+ }
+ return (NULL);
+}
+
+static void
+igb_tx_kick(struct igb_softc *sc)
+{
+ sc->tx_kick = 1;
+ pthread_cond_broadcast(&sc->tx_cond);
+}
+
+/*
+ * Receive.
+ */
+static int
+igb_rx_filter(struct igb_softc *sc, const uint8_t *p)
+{
+ uint32_t rctl = REG(sc, E1000_RCTL), h, ral, rah;
+ int i;
+
+ if (ETHER_IS_BROADCAST(p)) {
+ if (rctl & E1000_RCTL_BAM)
+ return (1);
+ } else if (ETHER_IS_MULTICAST(p) ? (rctl & E1000_RCTL_MPE) :
+ (rctl & E1000_RCTL_UPE))
+ return (1);
+ for (i = 0; i < E1000_RAR_ENTRIES_82576; i++) {
+ ral = REG(sc, E1000_RAL(i));
+ rah = REG(sc, E1000_RAH(i));
+ if ((rah & E1000_RAH_AV) && memcmp(p, &ral, 4) == 0 &&
+ p[4] == (rah & 0xff) && p[5] == ((rah >> 8) & 0xff))
+ return (1);
+ }
+ if (ETHER_IS_MULTICAST(p)) {
+ /*
+ * The multicast table: 12 bits of the address, which ones
+ * RCTL.MO says.
+ */
+ switch ((rctl >> E1000_RCTL_MO_SHIFT) & 3) {
+ case 0:
+ h = p[4] >> 4 | p[5] << 4;
+ break;
+ case 1:
+ h = p[4] >> 3 | p[5] << 5;
+ break;
+ case 2:
+ h = p[4] >> 2 | p[5] << 6;
+ break;
+ default:
+ h = p[4] | p[5] << 8;
+ break;
+ }
+ h &= 0xfff;
+ if (REG(sc, E1000_MTA + (h >> 5) * 4) & (1u << (h & 31)))
+ return (1);
+ }
+ return (0);
+}
+
+/*
+ * Toeplitz: for every 1 bit of the input, the 32 bits of the key that start at
+ * its place.
+ */
+static uint32_t
+igb_toeplitz(const uint8_t *key, const uint8_t *in, size_t len)
+{
+ uint32_t hash = 0, k;
+ size_t i;
+ int b;
+
+ k = (uint32_t)key[0] << 24 | key[1] << 16 | key[2] << 8 | key[3];
+ for (i = 0; i < len; i++)
+ for (b = 7; b >= 0; b--) {
+ if (in[i] & (1 << b))
+ hash ^= k;
+ k = k << 1 | ((key[i + 4] >> b) & 1);
+ }
+ return (hash);
+}
+
+/*
+ * What the frame is (the descriptor's packet type), and with RSS on its hash,
+ * the hash's type and the queue RETA gives for it. Without RSS, or for what
+ * MRQC does not hash: queue 0.
+ */
+static int
+igb_rss(struct igb_softc *sc, const uint8_t *p, size_t len, uint32_t *hash,
+ uint32_t *info)
+{
+ uint32_t mrqc = REG(sc, E1000_MRQC);
+ const uint8_t *ip, *l4 = NULL;
+ uint8_t in[36];
+ size_t off = ETHER_HDR_LEN, n = 0, hl;
+ int type = 0, proto = -1, v6 = 0, ports = 0;
+
+ *hash = 0;
+ *info = 0;
+ if (len >= IGB_VLAN_HDR_LEN && igb_get16(p + 12) == ETHERTYPE_VLAN)
+ off = IGB_VLAN_HDR_LEN;
+ ip = p + off;
+ if (igb_get16(p + off - 2) == ETHERTYPE_IP && len >= off + 20 &&
+ (ip[0] >> 4) == 4) {
+ hl = (ip[0] & 0xf) * 4;
+ *info = E1000_RXDADV_PKTTYPE_IPV4;
+ memcpy(in, ip + 12, 8);
+ n = 8;
+ if (!(ip[6] & 0x3f) && ip[7] == 0 && hl >= 20 &&
+ len >= off + hl + 4) {
+ /* Not a fragment: the ports are there. */
+ proto = ip[9];
+ l4 = ip + hl;
+ }
+ } else if (igb_get16(p + off - 2) == ETHERTYPE_IPV6 &&
+ len >= off + 40) {
+ v6 = 1;
+ *info = E1000_RXDADV_PKTTYPE_IPV6;
+ memcpy(in, ip + 8, 32);
+ n = 32;
+ if (len >= off + 44) {
+ /* Extension headers: hashed as plain IPv6. */
+ proto = ip[6];
+ l4 = ip + 40;
+ }
+ } else
+ return (0);
+ if (proto == IPPROTO_TCP)
+ *info |= E1000_RXDADV_PKTTYPE_TCP;
+ else if (proto == IPPROTO_UDP)
+ *info |= E1000_RXDADV_PKTTYPE_UDP;
+ if ((mrqc & 7) != E1000_MRQC_ENABLE_RSS_MQ)
+ return (0);
+ if (proto == IPPROTO_TCP && (mrqc & (v6 ?
+ E1000_MRQC_RSS_FIELD_IPV6_TCP : E1000_MRQC_RSS_FIELD_IPV4_TCP))) {
+ type = v6 ? E1000_RXDADV_RSSTYPE_IPV6_TCP :
+ E1000_RXDADV_RSSTYPE_IPV4_TCP;
+ ports = 1;
+ } else if (proto == IPPROTO_UDP && (mrqc & (v6 ?
+ E1000_MRQC_RSS_FIELD_IPV6_UDP : E1000_MRQC_RSS_FIELD_IPV4_UDP))) {
+ type = v6 ? E1000_RXDADV_RSSTYPE_IPV6_UDP :
+ E1000_RXDADV_RSSTYPE_IPV4_UDP;
+ ports = 1;
+ } else if (mrqc & (v6 ? E1000_MRQC_RSS_FIELD_IPV6 :
+ E1000_MRQC_RSS_FIELD_IPV4))
+ type = v6 ? E1000_RXDADV_RSSTYPE_IPV6 :
+ E1000_RXDADV_RSSTYPE_IPV4;
+ else
+ return (0);
+ if (ports) {
+ memcpy(in + n, l4, 4);
+ n += 4;
+ }
+ *hash = igb_toeplitz((const uint8_t *)&REG(sc, E1000_RSSRK(0)), in, n);
+ *info |= type;
+ return (((const uint8_t *)&REG(sc, E1000_RETA(0)))[*hash & 0x7f] &
+ (IGB_NQ - 1));
+}
+
+/*
+ * What the chip verifies of a received frame when RXCSUM says so: an IPv4
+ * header's checksum, and the TCP or UDP checksum of a packet that is not a
+ * fragment (IPv6: without extension headers). The lengths are the IP header's:
+ * the frame may be padded.
+ */
+static void
+igb_rx_checksum(struct igb_softc *sc, const uint8_t *p, size_t len,
+ struct igb_rxpkt *r)
+{
+ uint32_t rxcsum = REG(sc, E1000_RXCSUM), sum;
+ const uint8_t *ip, *l4;
+ size_t off = ETHER_HDR_LEN, hl, l4len;
+ int proto;
+
+ if (!(rxcsum & (E1000_RXCSUM_IPOFL | E1000_RXCSUM_TUOFL)))
+ return;
+ if (len >= IGB_VLAN_HDR_LEN && igb_get16(p + 12) == ETHERTYPE_VLAN)
+ off = IGB_VLAN_HDR_LEN;
+ ip = p + off;
+ if (igb_get16(p + off - 2) == ETHERTYPE_IP && len >= off + 20 &&
+ (ip[0] >> 4) == 4) {
+ hl = (ip[0] & 0xf) * 4;
+ l4len = ip[2] << 8 | ip[3];
+ if (hl < 20 || l4len < hl || off + l4len > len)
+ return;
+ l4len -= hl;
+ if (rxcsum & E1000_RXCSUM_IPOFL) {
+ r->csum |= E1000_RXD_STAT_IPCS;
+ if (igb_fold(igb_sum(ip, hl, 0)) != 0xffff)
+ r->cerr |= E1000_RXD_ERR_IPE;
+ }
+ if ((ip[6] & 0x3f) || ip[7] != 0)
+ return; /* a fragment */
+ proto = ip[9];
+ l4 = ip + hl;
+ sum = igb_sum(ip + 12, 8, proto + l4len);
+ } else if (igb_get16(p + off - 2) == ETHERTYPE_IPV6 &&
+ len >= off + 40) {
+ l4len = ip[4] << 8 | ip[5];
+ if (off + 40 + l4len > len)
+ return;
+ proto = ip[6];
+ l4 = ip + 40;
+ sum = igb_sum(ip + 8, 32, proto + l4len);
+ } else
+ return;
+ if (!(rxcsum & E1000_RXCSUM_TUOFL))
+ return;
+ if (proto == IPPROTO_TCP && l4len >= 20)
+ r->csum |= E1000_RXD_STAT_TCPCS;
+ else if (proto == IPPROTO_UDP && l4len >= 8 &&
+ (l4[6] | l4[7]) != 0)
+ /* A UDP checksum of 0: there is none. */
+ r->csum |= E1000_RXD_STAT_TCPCS | E1000_RXD_STAT_UDPCS;
+ else
+ return;
+ if (igb_fold(igb_sum(l4, l4len, sum)) != 0xffff)
+ r->cerr |= E1000_RXD_ERR_TCPE;
+}
+
+/*
+ * A frame from the backend (len bytes, without the FCS): the filters, the VLAN
+ * tag, the padding, its queue. 0: it is not for the guest.
+ */
+static int
+igb_rx_prepare(struct igb_softc *sc, uint8_t *p, size_t len,
+ struct igb_rxpkt *r)
+{
+ uint32_t rctl = REG(sc, E1000_RCTL);
+
+ memset(r, 0, sizeof(*r));
+ if (len < ETHER_HDR_LEN || !igb_rx_filter(sc, p))
+ return (0);
+ r->bcast = ETHER_IS_BROADCAST(p);
+ r->mcast = ETHER_IS_MULTICAST(p) && !r->bcast;
+ if (len >= IGB_VLAN_HDR_LEN &&
+ igb_get16(p + 12) == (REG(sc, E1000_VET) & 0xffff)) {
+ r->vlan = p[14] << 8 | p[15];
+ if ((rctl & E1000_RCTL_VFE) &&
+ !(REG(sc, E1000_VFTA + ((r->vlan & 0xfff) >> 5) * 4) &
+ (1u << (r->vlan & 31))))
+ return (0);
+ /* The tag goes into the descriptor. */
+ if (REG(sc, E1000_CTRL) & E1000_CTRL_VME) {
+ memmove(p + 4, p, 12);
+ p += 4;
+ len -= 4;
+ r->vp = 1;
+ }
+ }
+ if (len < IGB_MINFRAME) {
+ memset(p + len, 0, IGB_MINFRAME - len);
+ len = IGB_MINFRAME;
+ }
+ /* The guest wants the FCS: four bytes of nothing. */
+ if (!(rctl & E1000_RCTL_SECRC)) {
+ memset(p + len, 0, 4);
+ len += 4;
+ }
+ if (len > ((rctl & E1000_RCTL_LPE) ? IGB_MAXFRAME :
+ (size_t)ETHER_MAX_LEN + ETHER_VLAN_ENCAP_LEN))
+ return (0);
+ r->p = p;
+ r->len = len;
+ r->q = igb_rss(sc, p, len, &r->hash, &r->info);
+ igb_rx_checksum(sc, p, len, r);
+ return (1);
+}
+
+static void
+igb_rx_missed(struct igb_softc *sc)
+{
+ igb_stat(sc, E1000_MPC, 1);
+ igb_stat(sc, E1000_RNBC, 1);
+}
+
+/*
+ * Into its queue's ring. 0: it is there; EAGAIN: the ring is full; ENXIO: there
+ * is no ring.
+ */
+static int
+igb_rx_deliver(struct igb_softc *sc, struct igb_rxpkt *r)
+{
+ uint32_t qb = RXQ(r->q, 0), rctl = REG(sc, E1000_RCTL), srrctl, size;
+ uint32_t head, tail, n, i, st;
+ size_t bufsz, chunk, off, len = r->len;
+ uint8_t *d, *b, *p = r->p;
+ uint64_t addr;
+ int adv;
+
+ size = REG(sc, qb + Q_LEN) / 16;
+ srrctl = REG(sc, qb + Q_SRRCTL);
+ bufsz = srrctl & E1000_SRRCTL_BSIZEPKT_MASK;
+ if (bufsz != 0)
+ bufsz <<= E1000_SRRCTL_BSIZEPKT_SHIFT;
+ else
+ bufsz = 2048u >> ((rctl >> 16) & 3);
+ adv = (srrctl & E1000_SRRCTL_DESCTYPE_MASK) != 0;
+ n = howmany(len, bufsz);
+ if (size == 0 || n >= size ||
+ !(REG(sc, qb + Q_DCTL) & E1000_RXDCTL_QUEUE_ENABLE))
+ return (ENXIO);
+ head = REG(sc, qb + Q_HEAD) % size;
+ tail = REG(sc, qb + Q_TAIL) % size;
+ if ((tail + size - head) % size < n)
+ return (EAGAIN);
+ for (off = 0, i = 0; i < n; i++, off += chunk) {
+ chunk = MIN(len - off, bufsz);
+ d = igb_desc(sc, qb, (head + i) % size);
+ if (d == NULL)
+ return (ENXIO);
+ memcpy(&addr, d, 8);
+ b = paddr_guest2host(sc->pi->pi_vmctx, addr, chunk);
+ if (b == NULL)
+ return (ENXIO);
+ memcpy(b, p + off, chunk);
+ st = E1000_RXD_STAT_DD;
+ if (i == n - 1)
+ st |= E1000_RXD_STAT_EOP | r->csum |
+ (r->vp ? E1000_RXD_STAT_VP : 0);
+ if (adv) {
+ if (i == n - 1)
+ st |= (uint32_t)r->cerr << 24;
+ memset(d, 0, 8);
+ if (i == n - 1) {
+ d[0] = r->info;
+ d[1] = r->info >> 8;
+ memcpy(d + 4, &r->hash, 4);
+ }
+ memcpy(d + 8, &st, 4);
+ d[12] = chunk;
+ d[13] = chunk >> 8;
+ d[14] = r->vp ? r->vlan : 0;
+ d[15] = r->vp ? r->vlan >> 8 : 0;
+ } else {
+ d[8] = chunk;
+ d[9] = chunk >> 8;
+ d[10] = d[11] = 0;
+ d[12] = st | (r->csum ? 0 : E1000_RXD_STAT_IXSM);
+ d[13] = i == n - 1 ? r->cerr : 0;
+ d[14] = r->vp ? r->vlan : 0;
+ d[15] = r->vp ? r->vlan >> 8 : 0;
+ }
+ }
+ REG(sc, qb + Q_HEAD) = (head + n) % size;
+ if (!(rctl & E1000_RCTL_SECRC))
+ len -= 4;
+ igb_stat(sc, E1000_PRC64 + igb_len_bucket(len + 4) * 4, 1);
+ igb_stat(sc, E1000_GPRC, 1);
+ igb_stat(sc, E1000_TPR, 1);
+ igb_stat64(sc, E1000_GORCL, len + 4);
+ igb_stat64(sc, E1000_TORL, len + 4);
+ if (r->bcast)
+ igb_stat(sc, E1000_BPRC, 1);
+ else if (r->mcast)
+ igb_stat(sc, E1000_MPRC, 1);
+ return (0);
+}
+
+/*
+ * The frame that waits for descriptors (igb_rx_callback) is tried again: the
+ * guest moved a tail or changed what receives. force: its time is over, it is
+ * dropped if it still has no room. When it is gone the backend's frames come in
+ * again.
+ */
+static void
+igb_rx_retry(struct igb_softc *sc, int force)
+{
+ int e;
+
+ if (!sc->rx_held)
+ return;
+ e = (REG(sc, E1000_RCTL) & E1000_RCTL_EN) && sc->s.link ?
+ igb_rx_deliver(sc, &sc->held) : ENXIO;
+ if (e == EAGAIN && !force)
+ return;
+ sc->rx_held = 0;
+ if (e == 0)
+ igb_cause_q(sc, 1, sc->held.q);
+ else {
+ igb_rx_missed(sc);
+ igb_cause(sc, E1000_ICR_RXO);
+ }
+ if (!sc->paused)
+ netbe_rx_enable(sc->be);
+}
+
+static void
+igb_rx_callback(int fd __unused, enum ev_type type __unused, void *param)
+{
+ struct igb_softc *sc = param;
+ struct igb_rxpkt r;
+ struct iovec iov;
+ uint32_t queues = 0;
+ ssize_t len;
+ int lim, q, e, miss = 0;
+
+ pthread_mutex_lock(&sc->mtx);
+ /* The backend's events are off; a last one. */
+ if (sc->paused || sc->rx_held) {
+ pthread_mutex_unlock(&sc->mtx);
+ return;
+ }
+ if (!(REG(sc, E1000_RCTL) & E1000_RCTL_EN) || !sc->s.link) {
+ while (netbe_rx_discard(sc->be) > 0)
+ ;
+ pthread_mutex_unlock(&sc->mtx);
+ return;
+ }
+ for (lim = 256; lim > 0; lim--) {
+ /* Behind the frame there is room for the padding and the FCS */
+ iov.iov_base = sc->rxbuf;
+ iov.iov_len = IGB_MAXFRAME;
+ len = netbe_recv(sc->be, &iov, 1);
+ if (len <= 0)
+ break;
+ if (!igb_rx_prepare(sc, sc->rxbuf, len, &r))
+ continue;
+ e = igb_rx_deliver(sc, &r);
+ if (e == 0)
+ queues |= 1u << r.q;
+ else if (e == EAGAIN) {
+ /*
+ * The ring is full. The frame waits here and the others
+ * in the backend, which holds back who sends them:
+ * until the guest moves a tail, or for a while if the
+ * queue is to drop (SRRCTL.DROP_EN: a driver that has
+ * stopped serving this queue must not stop the rest).
+ */
+ sc->held = r;
+ sc->rx_held = 1;
+ sc->rx_hold_until = 0;
+ if (REG(sc, RXQ(r.q, Q_SRRCTL)) & E1000_SRRCTL_DROP_EN)
+ sc->rx_hold_until = igb_now() + IGB_RX_HOLD_NS;
+ netbe_rx_disable(sc->be);
+ if (sc->rx_hold_until != 0)
+ pthread_cond_signal(&sc->tmr_cond);
+ break;
+ } else {
+ igb_rx_missed(sc);
+ miss = 1;
+ }
+ }
+ for (q = 0; q < IGB_NQ; q++)
+ if (queues & (1u << q))
+ igb_cause_q(sc, 1, q);
+ if (miss)
+ igb_cause(sc, E1000_ICR_RXO);
+ pthread_mutex_unlock(&sc->mtx);
+}
+
+/*
+ * The timer thread: the interrupts EITR holds back, and the end of a held
+ * frame's wait.
+ */
+static void *
+igb_tmr_thread(void *arg)
+{
+ struct igb_softc *sc = arg;
+ struct timespec ts;
+ uint64_t now, next;
+ int v;
+
+ pthread_mutex_lock(&sc->mtx);
+ for (;;) {
+ next = 0;
+ if (!sc->paused) {
+ for (v = 0; v < IGB_MSIX_VECS; v++)
+ if ((sc->itr_pending & (1u << v)) &&
+ (next == 0 || sc->itr_next[v] < next))
+ next = MAX(sc->itr_next[v], 1);
+ if (sc->rx_held && sc->rx_hold_until != 0 &&
+ (next == 0 || sc->rx_hold_until < next))
+ next = sc->rx_hold_until;
+ }
+ if (next == 0) {
+ pthread_cond_wait(&sc->tmr_cond, &sc->mtx);
+ continue;
+ }
+ now = igb_now();
+ if (now < next) {
+ ts.tv_sec = next / 1000000000;
+ ts.tv_nsec = next % 1000000000;
+ pthread_cond_timedwait(&sc->tmr_cond, &sc->mtx, &ts);
+ continue;
+ }
+ for (v = 0; v < IGB_MSIX_VECS; v++) {
+ if (!(sc->itr_pending & (1u << v)) ||
+ sc->itr_next[v] > now)
+ continue;
+ if ((REG(sc, E1000_EICR) & REG(sc, E1000_EIMS) &
+ (1u << v)) && igb_msix(sc))
+ igb_msix_deliver(sc, v, now);
+ else
+ /* Cleared or masked meanwhile. */
+ sc->itr_pending &= ~(1u << v);
+ }
+ if (sc->rx_held && sc->rx_hold_until != 0 &&
+ sc->rx_hold_until <= now)
+ igb_rx_retry(sc, 1);
+ }
+ return (NULL);
+}
+
+/*
+ * BAR 0. The first four queues' registers are also at the 82575's places
+ * (0x2800 and 0x3800, 0x100 apart), which is where the drivers address them.
+ */
+static uint32_t
+igb_canon(uint32_t off)
+{
+ if (off >= 0x2800 && off < 0x2c00 && (off & 0xff) < 0x40)
+ return (RXQ((off - 0x2800) >> 8, off & 0x3f));
+ if (off >= 0x3800 && off < 0x3c00 && (off & 0xff) < 0x40)
+ return (TXQ((off - 0x3800) >> 8, off & 0x3f));
+ return (off);
+}
+
+static uint32_t
+igb_reg_read(struct igb_softc *sc, uint32_t off)
+{
+ uint32_t val;
+
+ switch (off) {
+ case E1000_STATUS:
+ val = 0;
+ if (sc->s.link)
+ val |= E1000_STATUS_FD | E1000_STATUS_LU |
+ E1000_STATUS_SPEED_1000;
+ if (!(REG(sc, E1000_CTRL) & E1000_CTRL_GIO_MASTER_DISABLE))
+ val |= E1000_STATUS_GIO_MASTER_ENABLE;
+ return (val);
+ case E1000_EECD:
+ return (REG(sc, off) | E1000_EECD_PRES | E1000_EECD_AUTO_RD |
+ IGB_EECD_SIZE);
+ case E1000_ICR:
+ /*
+ * Reading clears it when nothing is masked or an interrupt is
+ * asserted.
+ */
+ val = REG(sc, E1000_ICR);
+ if (val & REG(sc, E1000_IMS)) {
+ val |= E1000_ICR_INT_ASSERTED;
+ REG(sc, E1000_IMS) &= ~REG(sc, E1000_IAM);
+ }
+ if (REG(sc, E1000_IMS) == 0 || (val & E1000_ICR_INT_ASSERTED)) {
+ REG(sc, E1000_ICR) = 0;
+ igb_irq_update(sc, 0);
+ }
+ return (val);
+ case E1000_ICS:
+ case E1000_IMC:
+ case E1000_EICS:
+ case E1000_EIMC:
+ return (0);
+ case E1000_EICR:
+ val = REG(sc, E1000_EICR);
+ if (!igb_msix(sc))
+ REG(sc, E1000_EICR) = 0;
+ return (val);
+ }
+ val = REG(sc, off);
+ if (off >= E1000_CRCERRS && off < IGB_STATS_END)
+ REG(sc, off) = 0;
+ return (val);
+}
+
+static void
+igb_reg_write(struct igb_softc *sc, uint32_t off, uint32_t val)
+{
+ switch (off) {
+ case E1000_CTRL:
+ if (val & (E1000_CTRL_RST | E1000_CTRL_DEV_RST)) {
+ DPRINTF("reset");
+ igb_reset(sc, 0);
+ return;
+ }
+ REG(sc, off) = val & ~E1000_CTRL_PHY_RST;
+ return;
+ case E1000_STATUS:
+ return;
+ case E1000_EERD:
+ if (val & E1000_NVM_RW_REG_START) {
+ uint32_t a = (val >> E1000_NVM_RW_ADDR_SHIFT) & 0x3fff;
+
+ val = (val & 0xfffc) | E1000_NVM_RW_REG_DONE |
+ (uint32_t)(a < IGB_NVM_WORDS ? sc->s.nvm[a] :
+ 0xffff) << E1000_NVM_RW_REG_DATA;
+ }
+ REG(sc, off) = val;
+ return;
+ case E1000_MDIC:
+ igb_mdic(sc, val);
+ return;
+ case E1000_ICR:
+ REG(sc, E1000_ICR) &= ~val;
+ igb_irq_update(sc, 0);
+ return;
+ case E1000_ICS:
+ igb_cause(sc, val & ~E1000_ICR_INT_ASSERTED);
+ return;
+ case E1000_IMS:
+ REG(sc, E1000_IMS) |= val;
+ igb_irq_update(sc, REG(sc, E1000_ICR) & val);
+ return;
+ case E1000_IMC:
+ REG(sc, E1000_IMS) &= ~val;
+ igb_irq_update(sc, 0);
+ return;
+ case E1000_EICR:
+ REG(sc, E1000_EICR) &= ~val;
+ return;
+ case E1000_EICS:
+ igb_eicr_set(sc, val);
+ return;
+ case E1000_EIMS:
+ REG(sc, E1000_EIMS) |= val;
+ /* What waited behind the mask goes out now. */
+ igb_eicr_set(sc, 0);
+ return;
+ case E1000_EIMC:
+ REG(sc, E1000_EIMS) &= ~val;
+ return;
+ case E1000_TCTL:
+ REG(sc, off) = val;
+ igb_tx_kick(sc);
+ return;
+ case E1000_RCTL:
+ REG(sc, off) = val;
+ igb_rx_retry(sc, 0);
+ return;
+ }
+ if (off >= E1000_CRCERRS && off < IGB_STATS_END)
+ return;
+ if (off >= IGB_RXQ_BASE && off < IGB_RXQ_BASE + IGB_NQ * 0x40) {
+ if ((off & 0x3f) == Q_HEAD || (off & 0x3f) == Q_TAIL)
+ val &= 0xffff;
+ REG(sc, off) = val;
+ if ((off & 0x3f) == Q_TAIL || (off & 0x3f) == Q_DCTL)
+ igb_rx_retry(sc, 0);
+ return;
+ }
+ if (off >= IGB_TXQ_BASE && off < IGB_TXQ_BASE + IGB_NQ * 0x40) {
+ if ((off & 0x3f) == Q_HEAD || (off & 0x3f) == Q_TAIL)
+ val &= 0xffff;
+ REG(sc, off) = val;
+ if ((off & 0x3f) == Q_TAIL || (off & 0x3f) == Q_DCTL)
+ igb_tx_kick(sc);
+ return;
+ }
+ REG(sc, off) = val;
+}
+
+static uint64_t
+igb_read(struct pci_devinst *pi, int baridx, uint64_t offset, int size)
+{
+ struct igb_softc *sc = pi->pi_arg;
+ uint64_t val;
+
+ if (baridx == pci_msix_table_bar(pi) ||
+ baridx == pci_msix_pba_bar(pi))
+ return (pci_emul_msix_tread(pi, offset, size));
+ if (baridx != 0 || offset + size > IGB_BAR_SIZE)
+ return (0xffffffff);
+ pthread_mutex_lock(&sc->mtx);
+ val = igb_reg_read(sc, igb_canon(offset & ~3));
+ pthread_mutex_unlock(&sc->mtx);
+ if (size != 4 || (offset & 3)) {
+ DPRINTF("read of %d bytes at %#jx", size, (uintmax_t)offset);
+ val >>= (offset & 3) * 8;
+ if (size < 4)
+ val &= (1ull << (size * 8)) - 1;
+ }
+ if (igb_debug > 1)
+ EPRINTLN("igb: %#jx (%d) -> %#jx", (uintmax_t)offset, size,
+ (uintmax_t)val);
+ return (val);
+}
+
+static void
+igb_write(struct pci_devinst *pi, int baridx, uint64_t offset, int size,
+ uint64_t value)
+{
+ struct igb_softc *sc = pi->pi_arg;
+
+ if (baridx == pci_msix_table_bar(pi) ||
+ baridx == pci_msix_pba_bar(pi)) {
+ pci_emul_msix_twrite(pi, offset, size, value);
+ return;
+ }
+ if (igb_debug > 1)
+ EPRINTLN("igb: %#jx (%d) <- %#jx", (uintmax_t)offset, size,
+ (uintmax_t)value);
+ if (baridx != 0 || offset + size > IGB_BAR_SIZE)
+ return;
+ if (size != 4 || (offset & 3)) {
+ DPRINTF("write of %d bytes at %#jx ignored", size,
+ (uintmax_t)offset);
+ return;
+ }
+ pthread_mutex_lock(&sc->mtx);
+ igb_reg_write(sc, igb_canon(offset), value);
+ pthread_mutex_unlock(&sc->mtx);
+}
+
+static int
+igb_init(struct pci_devinst *pi, nvlist_t *nvl)
+{
+ struct igb_softc *sc;
+ const char *mac, *link;
+ pthread_condattr_t ca;
+ char name[16];
+
+ sc = calloc(1, sizeof(*sc));
+ if (sc == NULL)
+ return (-1);
+ sc->rxbuf = malloc(IGB_MAXFRAME + 64);
+ sc->txbuf = malloc(IGB_TXBUF);
+ sc->segbuf = malloc(IGB_TXBUF);
+ if (sc->rxbuf == NULL || sc->txbuf == NULL || sc->segbuf == NULL)
+ return (-1);
+ sc->pi = pi;
+ pi->pi_arg = sc;
+ pthread_mutex_init(&sc->mtx, NULL);
+ pthread_cond_init(&sc->tx_cond, NULL);
+ pthread_condattr_init(&ca);
+ pthread_condattr_setclock(&ca, CLOCK_MONOTONIC);
+ pthread_cond_init(&sc->tmr_cond, &ca);
+ pthread_condattr_destroy(&ca);
+ /* link=down: started with the cable out. */
+ link = get_config_value_node(nvl, "link");
+ sc->s.cable = link == NULL || strcmp(link, "down") != 0;
+
+ pci_set_cfgdata16(pi, PCIR_VENDOR, 0x8086);
+ pci_set_cfgdata16(pi, PCIR_DEVICE, 0x10c9);
+ pci_set_cfgdata8(pi, PCIR_REVID, 0x01);
+ pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_NETWORK);
+ pci_set_cfgdata8(pi, PCIR_SUBCLASS, PCIS_NETWORK_ETHERNET);
+ pci_set_cfgdata16(pi, PCIR_SUBVEND_0, 0x8086);
+ /* Gigabit ET Dual Port Server Adapter. */
+ pci_set_cfgdata16(pi, PCIR_SUBDEV_0, 0xa03c);
+ pci_emul_add_pciecap(pi, PCIEM_TYPE_ENDPOINT);
+ pci_emul_add_msicap(pi, 1);
+ pci_emul_alloc_bar(pi, 0, PCIBAR_MEM32, IGB_BAR_SIZE);
+ if (get_config_bool_node_default(nvl, "msix", true) &&
+ pci_emul_add_msixcap(pi, IGB_MSIX_VECS, IGB_MSIX_BAR) != 0)
+ return (-1);
+ pci_lintr_request(pi);
+
+ mac = get_config_value_node(nvl, "mac");
+ if (mac != NULL) {
+ if (net_parsemac(mac, sc->mac) != 0)
+ return (-1);
+ } else
+ net_genmac(pi, sc->mac);
+
+ igb_reset(sc, 1);
+ if (netbe_init(&sc->be, nvl, igb_rx_callback, sc) != 0)
+ return (-1);
+ pthread_create(&sc->tx_tid, NULL, igb_tx_thread, sc);
+ snprintf(name, sizeof(name), "igb-%d:%d tx", pi->pi_slot, pi->pi_func);
+ pthread_set_name_np(sc->tx_tid, name);
+ pthread_create(&sc->tmr_tid, NULL, igb_tmr_thread, sc);
+ snprintf(name, sizeof(name), "igb-%d:%d tmr", pi->pi_slot, pi->pi_func);
+ pthread_set_name_np(sc->tmr_tid, name);
+ netbe_rx_enable(sc->be);
+ return (0);
+}
+
+#ifdef BHYVE_SNAPSHOT
+/*
+ * The registers with a meaning; the rest of BAR 0 reads as zero after a
+ * restore.
+ */
+static const struct {
+ uint32_t start, end;
+} igb_snap_ranges[] = {
+ /* General, interrupts, statistics, filters, RSS. */
+ { 0x0000, 0x6000 },
+ { 0xa000, 0xa200 }, /* the unicast hash table */
+ { IGB_RXQ_BASE, IGB_RXQ_BASE + IGB_NQ * 0x40 },
+ { IGB_TXQ_BASE, IGB_TXQ_BASE + IGB_NQ * 0x40 },
+};
+
+static int
+igb_snapshot(struct vm_snapshot_meta *meta)
+{
+ struct pci_devinst *pi = meta->dev_data;
+ struct igb_softc *sc = pi->pi_arg;
+ struct igb_state *s = &sc->s;
+ uint32_t version = IGB_STATE_VERSION;
+ int ret;
+
+ SNAPSHOT_VAR_OR_LEAVE(version, meta, ret, done);
+ if (meta->op == VM_SNAPSHOT_RESTORE) {
+ if (version != IGB_STATE_VERSION) {
+ EPRINTLN("igb: the snapshot's state has version %u, "
+ "this bhyve reads %d", version, IGB_STATE_VERSION);
+ ret = EINVAL;
+ goto done;
+ }
+ memset(s->regs, 0, sizeof(s->regs));
+ }
+ SNAPSHOT_VAR_OR_LEAVE(s->lintr, meta, ret, done);
+ SNAPSHOT_VAR_OR_LEAVE(s->cable, meta, ret, done);
+ SNAPSHOT_VAR_OR_LEAVE(s->link, meta, ret, done);
+ SNAPSHOT_BUF_OR_LEAVE(s->nvm, sizeof(s->nvm), meta, ret, done);
+ SNAPSHOT_BUF_OR_LEAVE(s->phy, sizeof(s->phy), meta, ret, done);
+ SNAPSHOT_BUF_OR_LEAVE(s->ctx, sizeof(s->ctx), meta, ret, done);
+ for (size_t i = 0; i < nitems(igb_snap_ranges); i++)
+ SNAPSHOT_BUF_OR_LEAVE(&REG(sc, igb_snap_ranges[i].start),
+ igb_snap_ranges[i].end - igb_snap_ranges[i].start,
+ meta, ret, done);
+ if (meta->op == VM_SNAPSHOT_RESTORE) {
+ memset(sc->itr_next, 0, sizeof(sc->itr_next));
+ sc->itr_pending = 0;
+ sc->rx_held = 0;
+ }
+done:
+ return (ret);
+}
+
+/*
+ * Nothing of the device runs between pause and resume: the rings and the state
+ * stand still.
+ */
+static int
+igb_pause(struct pci_devinst *pi)
+{
+ struct igb_softc *sc = pi->pi_arg;
+
+ netbe_rx_disable(sc->be);
+ pthread_mutex_lock(&sc->mtx);
+ sc->paused = 1;
+ /* A frame that waited for descriptors is lost. */
+ if (sc->rx_held) {
+ sc->rx_held = 0;
+ igb_rx_missed(sc);
+ }
+ /* Their causes stay in EICR: resume sends them. */
+ sc->itr_pending = 0;
+ while (sc->tx_busy)
+ pthread_cond_wait(&sc->tx_cond, &sc->mtx);
+ pthread_mutex_unlock(&sc->mtx);
+ return (0);
+}
+
+static int
+igb_resume(struct pci_devinst *pi)
+{
+ struct igb_softc *sc = pi->pi_arg;
+
+ pthread_mutex_lock(&sc->mtx);
+ sc->paused = 0;
+ igb_tx_kick(sc); /* what the guest had queued goes out now */
+ igb_eicr_set(sc, 0);
+ pthread_mutex_unlock(&sc->mtx);
+ netbe_rx_enable(sc->be);
+ return (0);
+}
+#endif
+
+static const struct pci_devemu pci_de_igb = {
+ .pe_emu = "igb",
+ .pe_init = igb_init,
+ .pe_legacy_config = netbe_legacy_config,
+ .pe_barwrite = igb_write,
+ .pe_barread = igb_read,
+#ifdef BHYVE_SNAPSHOT
+ .pe_snapshot = igb_snapshot,
+ .pe_pause = igb_pause,
+ .pe_resume = igb_resume,
+#endif
+};
+PCI_EMUL_SET(pci_de_igb);

File Metadata

Mime Type
text/plain
Expires
Thu, Nov 26, 10:00 AM (20 h, 18 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42011012
Default Alt Text
D62044.1795687216.diff (52 KB)

Event Timeline