Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F177976283
D62044.1795696984.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
53 KB
Referenced Files
None
Subscribers
None
D62044.1795696984.diff
View Options
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,1852 @@
+/*-
+ * 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 <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>
+
+#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_NRAR 24
+#define IGB_MAXFRAME 16384
+#define IGB_MINFRAME 60 /* without the FCS */
+#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 (their offsets in BAR 0) */
+#define R_CTRL 0x0000
+#define R_STATUS 0x0008
+#define R_EECD 0x0010
+#define R_EERD 0x0014
+#define R_CTRL_EXT 0x0018
+#define R_MDIC 0x0020
+#define R_VET 0x0038
+#define R_ICR 0x00c0
+#define R_ICS 0x00c8
+#define R_IMS 0x00d0
+#define R_IMC 0x00d8
+#define R_IAM 0x00e0
+#define R_RCTL 0x0100
+#define R_TCTL 0x0400
+#define R_EEMNGCTL 0x1010
+#define R_GPIE 0x1514
+#define R_EICS 0x1520
+#define R_EIMS 0x1524
+#define R_EIMC 0x1528
+#define R_EIAC 0x152c
+#define R_EIAM 0x1530
+#define R_EICR 0x1580
+#define R_EITR0 0x1680 /* per vector; bits 14:2: usec */
+/* 8 words: a byte each for the queues RX n, TX n, RX n+8, TX n+8 */
+#define R_IVAR0 0x1700
+#define R_IVAR_MISC 0x1740 /* byte 1: the vector of ICR's causes */
+#define R_RXCSUM 0x5000
+#define R_MRQC 0x5818
+#define R_RETA 0x5c00 /* 128 bytes: the queue of a hash */
+#define R_RSSRK 0x5c80 /* the hash's key, 40 bytes */
+#define R_STATS 0x4000 /* the statistics, cleared by reading */
+#define R_STATS_END 0x4140
+#define R_MPC 0x4010
+#define R_PRC64 0x405c /* six words: received, by length */
+#define R_GPRC 0x4074
+#define R_BPRC 0x4078
+#define R_MPRC 0x407c
+#define R_GPTC 0x4080
+#define R_GORCL 0x4088
+#define R_GOTCL 0x4090
+#define R_RNBC 0x40a0
+#define R_TORL 0x40c0
+#define R_TOTL 0x40c8
+#define R_TPR 0x40d0
+#define R_TPT 0x40d4
+#define R_PTC64 0x40d8 /* six words: frames sent by length */
+#define R_MPTC 0x40f0
+#define R_BPTC 0x40f4
+#define R_TSCTC 0x40f8 /* TCP segmentation contexts */
+#define R_MTA 0x5200 /* 128 words */
+#define R_RAL0 0x5400 /* receive addresses 0-15, 8 bytes */
+#define R_RAL16 0x54e0 /* 16-23 */
+#define R_VFTA 0x5600 /* 128 words */
+#define R_RXQ 0xc000 /* a receive queue: 0x40 bytes each */
+#define R_TXQ 0xe000 /* a transmit queue's block */
+/* in a queue's block */
+#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) (R_RXQ + (q) * 0x40 + (r))
+#define TXQ(q, r) (R_TXQ + (q) * 0x40 + (r))
+
+#define CTRL_FD 0x00000001
+#define CTRL_GIO_MDIS 0x00000004
+#define CTRL_SLU 0x00000040
+#define CTRL_RST 0x04000000
+#define CTRL_DEV_RST 0x20000000
+#define CTRL_VME 0x40000000
+#define CTRL_PHY_RST 0x80000000
+#define STATUS_FD 0x00000001
+#define STATUS_LU 0x00000002
+#define STATUS_1000 0x00000080
+#define STATUS_GIO_MEN 0x00080000
+#define EECD_PRES 0x00000100
+#define EECD_AUTO_RD 0x00000200
+#define EECD_SIZE_256 (2 << 11) /* 1 << (2 + 6) words */
+#define EERD_START 0x00000001
+#define EERD_DONE 0x00000002
+#define MDIC_OP_WRITE 0x04000000
+#define MDIC_OP_READ 0x08000000
+#define MDIC_READY 0x10000000
+#define MDIC_IE 0x20000000
+#define MDIC_ERROR 0x40000000
+#define ICR_TXDW 0x00000001
+#define ICR_LSC 0x00000004
+#define ICR_RXO 0x00000040
+#define ICR_RXDW 0x00000080
+#define ICR_MDAC 0x00000200
+#define ICR_INTA 0x80000000
+#define RCTL_EN 0x00000002
+#define RCTL_UPE 0x00000008
+#define RCTL_MPE 0x00000010
+#define RCTL_LPE 0x00000020
+#define RCTL_BAM 0x00008000
+#define RCTL_VFE 0x00040000
+#define RCTL_SECRC 0x04000000
+#define TCTL_EN 0x00000002
+#define EEMNGCTL_CFG_DONE0 0x00040000
+#define GPIE_MSIX_MODE 0x00000010
+#define GPIE_EIAME 0x40000000
+#define IVAR_VALID 0x80
+#define MRQC_RSS 0x00000002 /* in bits 2:0 */
+#define MRQC_IPV4_TCP 0x00010000
+#define MRQC_IPV4 0x00020000
+#define MRQC_IPV6 0x00100000
+#define MRQC_IPV6_TCP 0x00200000
+#define MRQC_IPV4_UDP 0x00400000
+#define MRQC_IPV6_UDP 0x00800000
+/*
+ * an advanced receive descriptor's first word: the hash's type, then the
+ * packet's
+ */
+#define RSSTYPE_IPV4_TCP 1
+#define RSSTYPE_IPV4 2
+#define RSSTYPE_IPV6_TCP 3
+#define RSSTYPE_IPV6 5
+#define RSSTYPE_IPV4_UDP 7
+#define RSSTYPE_IPV6_UDP 8
+#define PKTTYPE_IPV4 0x0010
+#define PKTTYPE_IPV6 0x0040
+#define PKTTYPE_TCP 0x0100
+#define PKTTYPE_UDP 0x0200
+#define RAH_AV 0x80000000
+#define RXCSUM_IPOFLD 0x00000100
+#define RXCSUM_TUOFLD 0x00000200
+#define DCTL_ENABLE 0x02000000
+#define SRRCTL_DESCTYPE(v) (((v) >> 25) & 7)
+#define SRRCTL_DROP_EN 0x80000000
+#define PHY_CR_POWER_DOWN 0x0800
+#define PHY_CR_RESTART_AN 0x0200
+#define PHY_SR_UP 0x796d /* link up, autonegotiation complete */
+#define PHY_SR_DOWN 0x7949
+
+/*
+ * the transmit descriptors' third word (the legacy command byte is its top
+ * byte)
+ */
+#define TXD_EOP 0x01000000
+#define TXD_IFCS 0x02000000
+#define TXD_IC 0x04000000 /* legacy: insert a checksum at CSO */
+#define TXD_RS 0x08000000
+#define TXD_DEXT 0x20000000
+#define TXD_VLE 0x40000000
+#define TXD_TSE 0x80000000 /* advanced */
+#define TXD_DTYP(v) (((v) >> 20) & 0xf)
+#define TXD_DTYP_CTXT 2
+#define TXD_STAT_DD 0x00000001 /* in the fourth word, both formats */
+#define TXD_POPTS_IXSM 0x00000100 /* in the fourth word, advanced */
+#define TXD_POPTS_TXSM 0x00000200
+#define TXC_TUCMD_IPV4 0x00000400
+#define TXC_TUCMD_L4T(v) (((v) >> 11) & 3)
+#define TXC_L4T_UDP 0
+#define TXC_L4T_TCP 1
+/* the receive descriptors' status */
+#define RXD_DD 0x01
+#define RXD_EOP 0x02
+#define RXD_IXSM 0x04 /* legacy: no checksum was computed */
+#define RXD_VP 0x08
+#define RXD_UDPCS 0x10 /* the UDP checksum was verified */
+#define RXD_L4CS 0x20 /* TCP or UDP checksum checked */
+#define RXD_IPCS 0x40 /* IPv4 header checksum checked */
+/* ... and was wrong: the errors byte (advanced descriptors: bits 31:24) */
+#define RXD_ERR_L4E 0x20
+#define RXD_ERR_IPE 0x40
+
+/* 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;
+ /* RXD_IPCS, RXD_L4CS, RXD_UDPCS; RXD_ERR_* */
+ 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, R_ICR) & REG(sc, R_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, R_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, R_GPIE) & 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, R_EICR) &= ~(bit & REG(sc, R_EIAC));
+ if (REG(sc, R_GPIE) & GPIE_EIAME)
+ REG(sc, R_EIMS) &= ~(bit & REG(sc, R_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)((REG(sc, R_EITR0 + v * 4) >> 2) & 0x1fff) * 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, R_EICR) |= bits;
+ fire = REG(sc, R_EICR) & REG(sc, R_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, R_ICR) |= bits;
+ if (igb_msix(sc)) {
+ iv = REG(sc, R_IVAR_MISC) >> 8;
+ if ((bits & REG(sc, R_IMS)) && (iv & 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 ? ICR_RXDW : ICR_TXDW);
+ return;
+ }
+ REG(sc, R_ICR) |= rx ? ICR_RXDW : ICR_TXDW;
+ iv = REG(sc, R_IVAR0 + (q & 7) * 4) >>
+ ((q < 8 ? 0 : 16) + (rx ? 0 : 8));
+ if (iv & 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 */
+ w[0x08] = 0x8257; /* the board's part number, "825760-001" */
+ w[0x09] = 0x6001;
+ w[0x0a] = 0x0000; /* initialization control 1 */
+ w[0x0b] = pci_get_cfgdata16(sc->pi, PCIR_SUBDEV_0);
+ w[0x0c] = pci_get_cfgdata16(sc->pi, PCIR_SUBVEND_0);
+ w[0x0d] = pci_get_cfgdata16(sc->pi, PCIR_DEVICE);
+ w[0x0e] = pci_get_cfgdata16(sc->pi, PCIR_VENDOR);
+ /* initialization control 2: no flow control by default */
+ w[0x0f] = 0x0000;
+ w[0x24] = 0x0000; /* initialization control 3, port A */
+ w[0x37] = 0xffff; /* no alternate MAC address */
+ for (sum = 0, i = 0; i < 0x3f; i++)
+ sum += w[i];
+ w[0x3f] = 0xbaba - 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[0] = 0x1140;
+ p[1] = PHY_SR_DOWN; /* status: igb_link_update() */
+ p[2] = 0x02a8; /* IGP04E1000 */
+ p[3] = 0x0391;
+ p[4] = 0x0de1; /* what we advertise */
+ p[5] = 0xcde1; /* and the partner */
+ p[6] = 0x0007;
+ p[9] = 0x0e00; /* 1000BASE-T control */
+ p[15] = 0x3000; /* extended status */
+}
+
+/*
+ * 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[0] & PHY_CR_POWER_DOWN);
+
+ p[1] = up ? PHY_SR_UP : PHY_SR_DOWN;
+ /* 1000BASE-T status: both receivers fine, the partner does 1000 */
+ p[10] = up ? 0x7c00 : 0;
+ /* the IGP's port status: 1000, full duplex, link */
+ p[17] = up ? 0xc600 : 0;
+ if (up == sc->s.link)
+ return;
+ sc->s.link = up;
+ DPRINTF("link %s", up ? "up" : "down");
+ if (!quiet)
+ igb_cause(sc, ICR_LSC);
+}
+
+static void
+igb_mdic(struct igb_softc *sc, uint32_t val)
+{
+ int reg = (val >> 16) & 0x1f, phy = (val >> 21) & 0x1f;
+
+ val &= ~(MDIC_READY | MDIC_ERROR);
+ if (phy != 1)
+ val |= MDIC_ERROR;
+ else if (val & MDIC_OP_READ)
+ val = (val & 0xffff0000) | sc->s.phy[reg];
+ else if (val & MDIC_OP_WRITE) {
+ uint16_t d = val;
+
+ switch (reg) {
+ case 0:
+ /*
+ * reset and restart of the negotiation are done at once
+ */
+ sc->s.phy[0] = d & ~(0x8000 | PHY_CR_RESTART_AN);
+ igb_link_update(sc, 0);
+ break;
+ case 1: case 2: case 3: case 5: case 6: case 10: case 15:
+ break; /* read only */
+ default:
+ sc->s.phy[reg] = d;
+ }
+ }
+ REG(sc, R_MDIC) = val | MDIC_READY;
+ if (val & MDIC_IE)
+ igb_cause(sc, 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, R_CTRL) = CTRL_FD | CTRL_SLU;
+ REG(sc, R_EECD) = EECD_PRES | EECD_AUTO_RD | EECD_SIZE_256;
+ REG(sc, R_EEMNGCTL) = EEMNGCTL_CFG_DONE0;
+ REG(sc, R_VET) = 0x8100;
+ /*
+ * the chip loads the address from the NVM into the first receive
+ * address
+ */
+ REG(sc, R_RAL0) = s->nvm[0] | (uint32_t)s->nvm[1] << 16;
+ REG(sc, R_RAL0 + 4) = s->nvm[2] | RAH_AV;
+ /* queue 0 is on after a reset */
+ REG(sc, RXQ(0, Q_DCTL)) = DCTL_ENABLE;
+ REG(sc, TXQ(0, Q_DCTL)) = DCTL_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 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 < 14)
+ return;
+ if (vle) {
+ tag[0] = REG(sc, R_VET) >> 8;
+ tag[1] = REG(sc, R_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 TXD_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, vle = (t->cmd & TXD_VLE) != 0;
+ uint32_t seq, seed;
+ uint16_t id;
+
+ if (!t->adv) {
+ if ((t->cmd & TXD_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;
+ ip4 = (t->ctx.tucmd & TXC_TUCMD_IPV4) != 0;
+ tcp = TXC_TUCMD_L4T(t->ctx.tucmd) == TXC_L4T_TCP;
+ if (!(t->cmd & TXD_TSE)) {
+ if ((t->olinfo & TXD_POPTS_IXSM) && ip4 && iplen >= 20 &&
+ maclen + iplen <= t->len)
+ igb_ip4_checksum(p + maclen, iplen);
+ if ((t->olinfo & TXD_POPTS_TXSM) &&
+ TXC_TUCMD_L4T(t->ctx.tucmd) <= TXC_L4T_TCP &&
+ 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] &= ~0x09;
+ 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, R_TCTL) & TCTL_EN) ||
+ !(REG(sc, qb + Q_DCTL) & DCTL_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] & TXD_DEXT) && TXD_DTYP(w[2]) == TXD_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] & TXD_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] & TXD_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] & TXD_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, R_PTC64 + igb_len_bucket(t.len + 4) * 4, 1);
+ igb_stat(sc, R_GPTC, 1);
+ igb_stat(sc, R_TPT, 1);
+ igb_stat64(sc, R_GOTCL, t.len + 4);
+ igb_stat64(sc, R_TOTL, t.len + 4);
+ if (sc->txbuf[0] & 1)
+ igb_stat(sc, memcmp(sc->txbuf,
+ "\xff\xff\xff\xff\xff\xff", 6) == 0 ?
+ R_BPTC : R_MPTC, 1);
+ if (t.cmd & TXD_TSE)
+ igb_stat(sc, R_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] & TXD_RS))
+ continue;
+ memcpy(&st, d + 12, 4);
+ st |= 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, ®(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, R_RCTL), h, off;
+ int i;
+
+ if (memcmp(p, "\xff\xff\xff\xff\xff\xff", 6) == 0) {
+ if (rctl & RCTL_BAM)
+ return (1);
+ } else if ((p[0] & 1) ? (rctl & RCTL_MPE) : (rctl & RCTL_UPE))
+ return (1);
+ for (i = 0; i < IGB_NRAR; i++) {
+ off = i < 16 ? R_RAL0 + i * 8 : R_RAL16 + (i - 16) * 8;
+ if ((REG(sc, off + 4) & RAH_AV) &&
+ memcmp(p, ®(sc, off), 4) == 0 &&
+ p[4] == (REG(sc, off + 4) & 0xff) &&
+ p[5] == ((REG(sc, off + 4) >> 8) & 0xff))
+ return (1);
+ }
+ if (p[0] & 1) {
+ /*
+ * the multicast table: 12 bits of the address, which ones
+ * RCTL.MO says
+ */
+ switch ((rctl >> 12) & 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, R_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, R_MRQC);
+ const uint8_t *ip, *l4 = NULL;
+ uint8_t in[36];
+ size_t off = 14, n = 0, hl;
+ int type = 0, proto = -1, v6 = 0;
+
+ *hash = 0;
+ *info = 0;
+ if (len >= 18 && p[12] == 0x81 && p[13] == 0x00)
+ off = 18;
+ ip = p + off;
+ if (p[off - 2] == 0x08 && p[off - 1] == 0x00 && len >= off + 20 &&
+ (ip[0] >> 4) == 4) {
+ hl = (ip[0] & 0xf) * 4;
+ *info = 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 (p[off - 2] == 0x86 && p[off - 1] == 0xdd &&
+ len >= off + 40) {
+ v6 = 1;
+ *info = 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 == 6)
+ *info |= PKTTYPE_TCP;
+ else if (proto == 17)
+ *info |= PKTTYPE_UDP;
+ if ((mrqc & 7) != MRQC_RSS)
+ return (0);
+ if (proto == 6 && (mrqc & (v6 ? MRQC_IPV6_TCP : MRQC_IPV4_TCP)))
+ type = v6 ? RSSTYPE_IPV6_TCP : RSSTYPE_IPV4_TCP;
+ else if (proto == 17 && (mrqc & (v6 ? MRQC_IPV6_UDP : MRQC_IPV4_UDP)))
+ type = v6 ? RSSTYPE_IPV6_UDP : RSSTYPE_IPV4_UDP;
+ else if (mrqc & (v6 ? MRQC_IPV6 : MRQC_IPV4))
+ type = v6 ? RSSTYPE_IPV6 : RSSTYPE_IPV4;
+ else
+ return (0);
+ if (type != RSSTYPE_IPV4 && type != RSSTYPE_IPV6) {
+ memcpy(in + n, l4, 4);
+ n += 4;
+ }
+ *hash = igb_toeplitz((const uint8_t *)®(sc, R_RSSRK), in, n);
+ *info |= type;
+ return (((const uint8_t *)®(sc, R_RETA))[*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, R_RXCSUM), sum;
+ const uint8_t *ip, *l4;
+ size_t off = 14, hl, l4len;
+ int proto;
+
+ if (!(rxcsum & (RXCSUM_IPOFLD | RXCSUM_TUOFLD)))
+ return;
+ if (len >= 18 && p[12] == 0x81 && p[13] == 0x00)
+ off = 18;
+ ip = p + off;
+ if (p[off - 2] == 0x08 && p[off - 1] == 0x00 && 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 & RXCSUM_IPOFLD) {
+ r->csum |= RXD_IPCS;
+ if (igb_fold(igb_sum(ip, hl, 0)) != 0xffff)
+ r->cerr |= 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 (p[off - 2] == 0x86 && p[off - 1] == 0xdd &&
+ 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 & RXCSUM_TUOFLD))
+ return;
+ if (proto == 6 && l4len >= 20)
+ r->csum |= RXD_L4CS;
+ else if (proto == 17 && l4len >= 8 && (l4[6] | l4[7]) != 0)
+ /* a UDP checksum of 0: there is none */
+ r->csum |= RXD_L4CS | RXD_UDPCS;
+ else
+ return;
+ if (igb_fold(igb_sum(l4, l4len, sum)) != 0xffff)
+ r->cerr |= RXD_ERR_L4E;
+}
+
+/*
+ * 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, R_RCTL);
+
+ memset(r, 0, sizeof(*r));
+ if (len < 14 || !igb_rx_filter(sc, p))
+ return (0);
+ r->bcast = memcmp(p, "\xff\xff\xff\xff\xff\xff", 6) == 0;
+ r->mcast = (p[0] & 1) && !r->bcast;
+ if (len >= 18 && (p[12] << 8 | p[13]) == (REG(sc, R_VET) & 0xffff)) {
+ r->vlan = p[14] << 8 | p[15];
+ if ((rctl & RCTL_VFE) &&
+ !(REG(sc, R_VFTA + ((r->vlan & 0xfff) >> 5) * 4) &
+ (1u << (r->vlan & 31))))
+ return (0);
+ /* the tag goes into the descriptor */
+ if (REG(sc, R_CTRL) & 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 & RCTL_SECRC)) {
+ memset(p + len, 0, 4);
+ len += 4;
+ }
+ if (len > ((rctl & RCTL_LPE) ? IGB_MAXFRAME : 1522u))
+ 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, R_MPC, 1);
+ igb_stat(sc, R_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, R_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 & 0x7f) ? (srrctl & 0x7f) << 10 :
+ 2048u >> ((rctl >> 16) & 3);
+ adv = SRRCTL_DESCTYPE(srrctl) != 0;
+ n = howmany(len, bufsz);
+ if (size == 0 || !(REG(sc, qb + Q_DCTL) & DCTL_ENABLE) || n >= size)
+ 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 = RXD_DD;
+ if (i == n - 1)
+ st |= RXD_EOP | (r->vp ? RXD_VP : 0) | r->csum;
+ 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 : RXD_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 & RCTL_SECRC))
+ len -= 4;
+ igb_stat(sc, R_PRC64 + igb_len_bucket(len + 4) * 4, 1);
+ igb_stat(sc, R_GPRC, 1);
+ igb_stat(sc, R_TPR, 1);
+ igb_stat64(sc, R_GORCL, len + 4);
+ igb_stat64(sc, R_TORL, len + 4);
+ if (r->bcast)
+ igb_stat(sc, R_BPRC, 1);
+ else if (r->mcast)
+ igb_stat(sc, R_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, R_RCTL) & 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, 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, R_RCTL) & 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)) & 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, 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, R_EICR) & REG(sc, R_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 R_STATUS:
+ return ((sc->s.link ? STATUS_FD | STATUS_LU | STATUS_1000 : 0) |
+ ((REG(sc, R_CTRL) & CTRL_GIO_MDIS) ? 0 : STATUS_GIO_MEN));
+ case R_EECD:
+ return (REG(sc, off) | EECD_PRES | EECD_AUTO_RD |
+ EECD_SIZE_256);
+ case R_ICR:
+ /*
+ * reading clears it when nothing is masked or an interrupt is
+ * asserted
+ */
+ val = REG(sc, R_ICR);
+ if (val & REG(sc, R_IMS)) {
+ val |= ICR_INTA;
+ REG(sc, R_IMS) &= ~REG(sc, R_IAM);
+ }
+ if (REG(sc, R_IMS) == 0 || (val & ICR_INTA)) {
+ REG(sc, R_ICR) = 0;
+ igb_irq_update(sc, 0);
+ }
+ return (val);
+ case R_ICS:
+ case R_IMC:
+ case R_EICS:
+ case R_EIMC:
+ return (0);
+ case R_EICR:
+ val = REG(sc, R_EICR);
+ if (!igb_msix(sc))
+ REG(sc, R_EICR) = 0;
+ return (val);
+ }
+ val = REG(sc, off);
+ if (off >= R_STATS && off < R_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 R_CTRL:
+ if (val & (CTRL_RST | CTRL_DEV_RST)) {
+ DPRINTF("reset");
+ igb_reset(sc, 0);
+ return;
+ }
+ REG(sc, off) = val & ~CTRL_PHY_RST;
+ return;
+ case R_STATUS:
+ return;
+ case R_EERD:
+ if (val & EERD_START) {
+ uint32_t a = (val >> 2) & 0x3fff;
+
+ val = (val & 0xfffc) | EERD_DONE | (uint32_t)(a <
+ IGB_NVM_WORDS ? sc->s.nvm[a] : 0xffff) << 16;
+ }
+ REG(sc, off) = val;
+ return;
+ case R_MDIC:
+ igb_mdic(sc, val);
+ return;
+ case R_ICR:
+ REG(sc, R_ICR) &= ~val;
+ igb_irq_update(sc, 0);
+ return;
+ case R_ICS:
+ igb_cause(sc, val & ~ICR_INTA);
+ return;
+ case R_IMS:
+ REG(sc, R_IMS) |= val;
+ igb_irq_update(sc, REG(sc, R_ICR) & val);
+ return;
+ case R_IMC:
+ REG(sc, R_IMS) &= ~val;
+ igb_irq_update(sc, 0);
+ return;
+ case R_EICR:
+ REG(sc, R_EICR) &= ~val;
+ return;
+ case R_EICS:
+ igb_eicr_set(sc, val);
+ return;
+ case R_EIMS:
+ REG(sc, R_EIMS) |= val;
+ /* what waited behind the mask goes out now */
+ igb_eicr_set(sc, 0);
+ return;
+ case R_EIMC:
+ REG(sc, R_EIMS) &= ~val;
+ return;
+ case R_TCTL:
+ REG(sc, off) = val;
+ igb_tx_kick(sc);
+ return;
+ case R_RCTL:
+ REG(sc, off) = val;
+ igb_rx_retry(sc, 0);
+ return;
+ }
+ if (off >= R_STATS && off < R_STATS_END)
+ return;
+ if (off >= R_RXQ && off < R_RXQ + 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 >= R_TXQ && off < R_TXQ + 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 */
+ { R_RXQ, R_RXQ + IGB_NQ * 0x40 },
+ { R_TXQ, R_TXQ + 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(®(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
Details
Attached
Mime Type
text/plain
Expires
Thu, Nov 26, 12:43 PM (22 h, 53 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42013504
Default Alt Text
D62044.1795696984.diff (53 KB)
Attached To
Mode
D62044: bhyve: Add an Intel 82576 (igb) network device
Attached
Detach File
Event Timeline