1*1583ef11SJosh Hilke // SPDX-License-Identifier: GPL-2.0-only
2*1583ef11SJosh Hilke #include <unistd.h>
3*1583ef11SJosh Hilke #include <errno.h>
4*1583ef11SJosh Hilke #include <stdint.h>
5*1583ef11SJosh Hilke #include <linux/io.h>
6*1583ef11SJosh Hilke #include <linux/pci_regs.h>
7*1583ef11SJosh Hilke #include <linux/pci_ids.h>
8*1583ef11SJosh Hilke #include <linux/kernel.h>
9*1583ef11SJosh Hilke #include <linux/compiler.h>
10*1583ef11SJosh Hilke #include <asm/barrier.h>
11*1583ef11SJosh Hilke #include <linux/mii.h>
12*1583ef11SJosh Hilke #include <libvfio/vfio_pci_device.h>
13*1583ef11SJosh Hilke
14*1583ef11SJosh Hilke #include "e1000_regs.h"
15*1583ef11SJosh Hilke #include "e1000_defines.h"
16*1583ef11SJosh Hilke #include "e1000_82575.h"
17*1583ef11SJosh Hilke
18*1583ef11SJosh Hilke #define PCI_DEVICE_ID_INTEL_82576 0x10C9
19*1583ef11SJosh Hilke #define IGB_MAX_CHUNK_SIZE 1024
20*1583ef11SJosh Hilke #define MSIX_VECTOR 0
21*1583ef11SJosh Hilke #define MSIX_VECTOR_MASK (1 << MSIX_VECTOR)
22*1583ef11SJosh Hilke #define RING_SIZE 4096 /* Number of descriptors in ring */
23*1583ef11SJosh Hilke
24*1583ef11SJosh Hilke struct igb_tx_desc {
25*1583ef11SJosh Hilke union {
26*1583ef11SJosh Hilke struct {
27*1583ef11SJosh Hilke u64 buffer_addr; /* Address of descriptor's data buffer */
28*1583ef11SJosh Hilke u32 cmd_type_len; /* Command/Type/Length */
29*1583ef11SJosh Hilke u32 olinfo_status; /* Context/Buffer info */
30*1583ef11SJosh Hilke } read;
31*1583ef11SJosh Hilke
32*1583ef11SJosh Hilke struct {
33*1583ef11SJosh Hilke u64 rsvd; /* Reserved */
34*1583ef11SJosh Hilke u32 nxtseq_seed; /* Next sequence seed */
35*1583ef11SJosh Hilke u32 status; /* Descriptor status */
36*1583ef11SJosh Hilke } wb;
37*1583ef11SJosh Hilke };
38*1583ef11SJosh Hilke };
39*1583ef11SJosh Hilke
40*1583ef11SJosh Hilke struct igb_rx_desc {
41*1583ef11SJosh Hilke union {
42*1583ef11SJosh Hilke struct {
43*1583ef11SJosh Hilke u64 pkt_addr; /* Packet buffer address */
44*1583ef11SJosh Hilke u64 hdr_addr; /* Header buffer address */
45*1583ef11SJosh Hilke } read;
46*1583ef11SJosh Hilke struct {
47*1583ef11SJosh Hilke u16 pkt_info; /* RSS type, Packet type */
48*1583ef11SJosh Hilke u16 hdr_info; /* Split Head, buf len */
49*1583ef11SJosh Hilke u32 rss; /* RSS Hash */
50*1583ef11SJosh Hilke u32 status_error; /* ext status/error */
51*1583ef11SJosh Hilke u16 length; /* Packet length */
52*1583ef11SJosh Hilke u16 vlan; /* VLAN tag */
53*1583ef11SJosh Hilke } wb; /* writeback */
54*1583ef11SJosh Hilke };
55*1583ef11SJosh Hilke };
56*1583ef11SJosh Hilke
57*1583ef11SJosh Hilke struct igb {
58*1583ef11SJosh Hilke void *bar0;
59*1583ef11SJosh Hilke u32 tx_tail;
60*1583ef11SJosh Hilke u32 rx_tail;
61*1583ef11SJosh Hilke struct igb_tx_desc tx_ring[RING_SIZE] __attribute__((aligned(128)));
62*1583ef11SJosh Hilke struct igb_rx_desc rx_ring[RING_SIZE] __attribute__((aligned(128)));
63*1583ef11SJosh Hilke };
64*1583ef11SJosh Hilke
to_igb_state(struct vfio_pci_device * device)65*1583ef11SJosh Hilke static inline struct igb *to_igb_state(struct vfio_pci_device *device)
66*1583ef11SJosh Hilke {
67*1583ef11SJosh Hilke return (struct igb *)device->driver.region.vaddr;
68*1583ef11SJosh Hilke }
69*1583ef11SJosh Hilke
igb_write32(struct igb * igb,u32 reg,u32 val)70*1583ef11SJosh Hilke static inline void igb_write32(struct igb *igb, u32 reg, u32 val)
71*1583ef11SJosh Hilke {
72*1583ef11SJosh Hilke writel(val, igb->bar0 + reg);
73*1583ef11SJosh Hilke }
74*1583ef11SJosh Hilke
igb_read32(struct igb * igb,u32 reg)75*1583ef11SJosh Hilke static inline u32 igb_read32(struct igb *igb, u32 reg)
76*1583ef11SJosh Hilke {
77*1583ef11SJosh Hilke return readl(igb->bar0 + reg);
78*1583ef11SJosh Hilke }
79*1583ef11SJosh Hilke
igb_write_phy(struct igb * igb,u32 offset,u16 data)80*1583ef11SJosh Hilke static int igb_write_phy(struct igb *igb, u32 offset, u16 data)
81*1583ef11SJosh Hilke {
82*1583ef11SJosh Hilke u32 mdic;
83*1583ef11SJosh Hilke int i;
84*1583ef11SJosh Hilke
85*1583ef11SJosh Hilke /*
86*1583ef11SJosh Hilke * Write a PHY register over MDIO.
87*1583ef11SJosh Hilke *
88*1583ef11SJosh Hilke * A production driver would hold the SW/FW semaphore (SWSM.SWESMBI + the
89*1583ef11SJosh Hilke * SW_FW_SYNC PHY bit) across the MDIO transaction to serialize against the
90*1583ef11SJosh Hilke * device's management firmware. The selftest owns the assigned function
91*1583ef11SJosh Hilke * exclusively on a dedicated test device with no active manageability
92*1583ef11SJosh Hilke * contending for the PHY, so the sync is omitted; it should be added here
93*1583ef11SJosh Hilke * if this ever needs to run on a manageability-enabled NIC.
94*1583ef11SJosh Hilke */
95*1583ef11SJosh Hilke mdic = (((u32)data) |
96*1583ef11SJosh Hilke (offset << E1000_MDIC_REG_SHIFT) |
97*1583ef11SJosh Hilke (1 << E1000_MDIC_PHY_SHIFT) |
98*1583ef11SJosh Hilke E1000_MDIC_OP_WRITE);
99*1583ef11SJosh Hilke
100*1583ef11SJosh Hilke igb_write32(igb, E1000_MDIC, mdic);
101*1583ef11SJosh Hilke
102*1583ef11SJosh Hilke for (i = 0; i < 1000; i++) {
103*1583ef11SJosh Hilke usleep(50);
104*1583ef11SJosh Hilke mdic = igb_read32(igb, E1000_MDIC);
105*1583ef11SJosh Hilke if (mdic & E1000_MDIC_READY)
106*1583ef11SJosh Hilke break;
107*1583ef11SJosh Hilke }
108*1583ef11SJosh Hilke
109*1583ef11SJosh Hilke if (!(mdic & E1000_MDIC_READY))
110*1583ef11SJosh Hilke return -1;
111*1583ef11SJosh Hilke
112*1583ef11SJosh Hilke if (mdic & E1000_MDIC_ERROR)
113*1583ef11SJosh Hilke return -1;
114*1583ef11SJosh Hilke
115*1583ef11SJosh Hilke return 0;
116*1583ef11SJosh Hilke }
117*1583ef11SJosh Hilke
118*1583ef11SJosh Hilke /*
119*1583ef11SJosh Hilke * Configure the device for PHY internal loopback per 82576 datasheet
120*1583ef11SJosh Hilke * section 3.5.6.3.1. Force the PHY to 1Gb/s full duplex with loopback
121*1583ef11SJosh Hilke * enabled, then force the MAC link state to match. Internal loopback
122*1583ef11SJosh Hilke * wraps data at the end of the PHY datapath (section 3.5.6.3), so the
123*1583ef11SJosh Hilke * physical link state is irrelevant.
124*1583ef11SJosh Hilke *
125*1583ef11SJosh Hilke * Section 3.5.6.1 directs to "Use PHY Loopback instead of MAC Loopback
126*1583ef11SJosh Hilke * on the 82576", and section 3.5.6.2 states "MAC Loopback is not used
127*1583ef11SJosh Hilke * on this device." RCTL.LBM_MAC is still set elsewhere as a QEMU-only
128*1583ef11SJosh Hilke * accommodation; see the RCTL programming in the caller for the
129*1583ef11SJosh Hilke * rationale.
130*1583ef11SJosh Hilke */
igb_setup_loopback(struct igb * igb)131*1583ef11SJosh Hilke static void igb_setup_loopback(struct igb *igb)
132*1583ef11SJosh Hilke {
133*1583ef11SJosh Hilke u32 ctrl;
134*1583ef11SJosh Hilke int ret;
135*1583ef11SJosh Hilke
136*1583ef11SJosh Hilke /*
137*1583ef11SJosh Hilke * Kick the autoneg machinery solely to bring STATUS.LU up under
138*1583ef11SJosh Hilke * QEMU's igb emulation: QEMU only updates STATUS.LU via its
139*1583ef11SJosh Hilke * autoneg-done timer, and without LU set its receive path
140*1583ef11SJosh Hilke * (e1000x_hw_rx_enabled) drops every loopback frame. On real
141*1583ef11SJosh Hilke * hardware autoneg cannot complete before the next PHY write
142*1583ef11SJosh Hilke * below clears the autoneg-enable bit, so this is effectively a
143*1583ef11SJosh Hilke * no-op there.
144*1583ef11SJosh Hilke */
145*1583ef11SJosh Hilke (void)igb_write_phy(igb, MII_BMCR,
146*1583ef11SJosh Hilke BMCR_ANENABLE | BMCR_ANRESTART);
147*1583ef11SJosh Hilke
148*1583ef11SJosh Hilke /* PHY control: loopback + 1Gb/s full duplex, autoneg disabled. */
149*1583ef11SJosh Hilke ret = igb_write_phy(igb, MII_BMCR,
150*1583ef11SJosh Hilke BMCR_LOOPBACK |
151*1583ef11SJosh Hilke BMCR_SPEED1000 |
152*1583ef11SJosh Hilke BMCR_FULLDPLX);
153*1583ef11SJosh Hilke VFIO_ASSERT_EQ(ret, 0, "Failed to write PHY control register");
154*1583ef11SJosh Hilke
155*1583ef11SJosh Hilke /*
156*1583ef11SJosh Hilke * Brief delay before forcing the MAC, mirroring the kernel ethtool
157*1583ef11SJosh Hilke * selftest in igb_integrated_phy_loopback(). Not specified by the
158*1583ef11SJosh Hilke * datasheet, but empirically required by the kernel driver.
159*1583ef11SJosh Hilke */
160*1583ef11SJosh Hilke usleep(50000);
161*1583ef11SJosh Hilke
162*1583ef11SJosh Hilke /*
163*1583ef11SJosh Hilke * Force the MAC to 1Gb/s full duplex with link up. Without forcing
164*1583ef11SJosh Hilke * the link state the descriptor engine does not run, since the chip
165*1583ef11SJosh Hilke * normally waits for a real negotiated link.
166*1583ef11SJosh Hilke */
167*1583ef11SJosh Hilke ctrl = igb_read32(igb, E1000_CTRL);
168*1583ef11SJosh Hilke ctrl &= ~E1000_CTRL_SPD_SEL;
169*1583ef11SJosh Hilke ctrl |= E1000_CTRL_FRCSPD |
170*1583ef11SJosh Hilke E1000_CTRL_FRCDPX |
171*1583ef11SJosh Hilke E1000_CTRL_SPD_1000 |
172*1583ef11SJosh Hilke E1000_CTRL_FD |
173*1583ef11SJosh Hilke E1000_CTRL_SLU;
174*1583ef11SJosh Hilke igb_write32(igb, E1000_CTRL, ctrl);
175*1583ef11SJosh Hilke
176*1583ef11SJosh Hilke /*
177*1583ef11SJosh Hilke * Settling delay matching the kernel ethtool selftest's msleep(500)
178*1583ef11SJosh Hilke * at the tail of igb_integrated_phy_loopback(). Not specified by
179*1583ef11SJosh Hilke * the datasheet; empirical, and inherited from the kernel driver.
180*1583ef11SJosh Hilke */
181*1583ef11SJosh Hilke usleep(500000);
182*1583ef11SJosh Hilke }
183*1583ef11SJosh Hilke
igb_probe(struct vfio_pci_device * device)184*1583ef11SJosh Hilke static int igb_probe(struct vfio_pci_device *device)
185*1583ef11SJosh Hilke {
186*1583ef11SJosh Hilke if (!vfio_pci_device_match(device, PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82576))
187*1583ef11SJosh Hilke return -EINVAL;
188*1583ef11SJosh Hilke
189*1583ef11SJosh Hilke return 0;
190*1583ef11SJosh Hilke }
191*1583ef11SJosh Hilke
igb_reset(struct igb * igb)192*1583ef11SJosh Hilke static void igb_reset(struct igb *igb)
193*1583ef11SJosh Hilke {
194*1583ef11SJosh Hilke int retries = 20;
195*1583ef11SJosh Hilke
196*1583ef11SJosh Hilke igb_write32(igb, E1000_CTRL, igb_read32(igb, E1000_CTRL) | E1000_CTRL_RST);
197*1583ef11SJosh Hilke /*
198*1583ef11SJosh Hilke * Must wait at least 1 millisecond after setting the reset bit before
199*1583ef11SJosh Hilke * checking if this device is ready to be used (82576 datasheet section
200*1583ef11SJosh Hilke * 4.2.1.6.1). The delay also ensures the reset has taken effect and
201*1583ef11SJosh Hilke * cleared EECD.AUTO_RD before it is polled below.
202*1583ef11SJosh Hilke */
203*1583ef11SJosh Hilke usleep(1000);
204*1583ef11SJosh Hilke
205*1583ef11SJosh Hilke /*
206*1583ef11SJosh Hilke * Poll NVM Auto Read Done rather than CTRL.RST, matching
207*1583ef11SJosh Hilke * igb_get_auto_rd_done() in the igb driver: AUTO_RD implies both that
208*1583ef11SJosh Hilke * the reset completed and that the device finished re-reading its
209*1583ef11SJosh Hilke * configuration from NVM, which is what actually makes it usable.
210*1583ef11SJosh Hilke */
211*1583ef11SJosh Hilke while (retries-- > 0 && !(igb_read32(igb, E1000_EECD) & E1000_EECD_AUTO_RD))
212*1583ef11SJosh Hilke usleep(1000);
213*1583ef11SJosh Hilke
214*1583ef11SJosh Hilke /*
215*1583ef11SJosh Hilke * QEMU's igb emulation does not set E1000_EECD_AUTO_RD. If we timed out,
216*1583ef11SJosh Hilke * check if CTRL.RST is cleared, which is what QEMU uses to signal reset
217*1583ef11SJosh Hilke * completion.
218*1583ef11SJosh Hilke */
219*1583ef11SJosh Hilke if (retries < 0) {
220*1583ef11SJosh Hilke VFIO_ASSERT_EQ(igb_read32(igb, E1000_CTRL) & E1000_CTRL_RST, 0,
221*1583ef11SJosh Hilke "Device reset did not complete (CTRL.RST not cleared)");
222*1583ef11SJosh Hilke }
223*1583ef11SJosh Hilke
224*1583ef11SJosh Hilke igb_write32(igb, E1000_IMC, 0xFFFFFFFF);
225*1583ef11SJosh Hilke }
226*1583ef11SJosh Hilke
227*1583ef11SJosh Hilke /*
228*1583ef11SJosh Hilke * Program the device into a usable state. Split out of igb_init() so it
229*1583ef11SJosh Hilke * can be reused after a device reset to re-program the registers that
230*1583ef11SJosh Hilke * CTRL.RST clears. Expects bar0 to be mapped and MSI-X already enabled
231*1583ef11SJosh Hilke * via VFIO.
232*1583ef11SJosh Hilke */
igb_hw_init(struct vfio_pci_device * device)233*1583ef11SJosh Hilke static void igb_hw_init(struct vfio_pci_device *device)
234*1583ef11SJosh Hilke {
235*1583ef11SJosh Hilke struct igb *igb = to_igb_state(device);
236*1583ef11SJosh Hilke u64 iova_tx, iova_rx;
237*1583ef11SJosh Hilke u32 ctrl, rctl;
238*1583ef11SJosh Hilke u16 cmd_reg;
239*1583ef11SJosh Hilke int retries;
240*1583ef11SJosh Hilke
241*1583ef11SJosh Hilke iova_tx = to_iova(device, igb->tx_ring);
242*1583ef11SJosh Hilke iova_rx = to_iova(device, igb->rx_ring);
243*1583ef11SJosh Hilke
244*1583ef11SJosh Hilke
245*1583ef11SJosh Hilke
246*1583ef11SJosh Hilke /* Signal that the driver is loaded */
247*1583ef11SJosh Hilke ctrl = igb_read32(igb, E1000_CTRL_EXT);
248*1583ef11SJosh Hilke ctrl |= E1000_CTRL_EXT_DRV_LOAD;
249*1583ef11SJosh Hilke ctrl &= ~E1000_CTRL_EXT_LINK_MODE_MASK;
250*1583ef11SJosh Hilke igb_write32(igb, E1000_CTRL_EXT, ctrl);
251*1583ef11SJosh Hilke
252*1583ef11SJosh Hilke /* Enable PCI Bus Master. */
253*1583ef11SJosh Hilke cmd_reg = vfio_pci_config_readw(device, PCI_COMMAND);
254*1583ef11SJosh Hilke if ((cmd_reg & (PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY)) !=
255*1583ef11SJosh Hilke (PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY)) {
256*1583ef11SJosh Hilke cmd_reg |= (PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
257*1583ef11SJosh Hilke vfio_pci_config_writew(device, PCI_COMMAND, cmd_reg);
258*1583ef11SJosh Hilke }
259*1583ef11SJosh Hilke
260*1583ef11SJosh Hilke /* Configure PHY internal loopback for testing. */
261*1583ef11SJosh Hilke igb_setup_loopback(igb);
262*1583ef11SJosh Hilke
263*1583ef11SJosh Hilke /*
264*1583ef11SJosh Hilke * Disable DMA re-send on PCIe completion timeout (82576 datasheet
265*1583ef11SJosh Hilke * section 8.6.1, GCR.Completion_Timeout_Resend, bit 16). The
266*1583ef11SJosh Hilke * mix_and_match test intentionally submits descriptors targeting
267*1583ef11SJosh Hilke * unmapped IOVAs; with the default (set) value, the device keeps
268*1583ef11SJosh Hilke * retrying the failed read indefinitely, which keeps PCIe AER and
269*1583ef11SJosh Hilke * IOMMU error handling busy and interferes with reset recovery.
270*1583ef11SJosh Hilke */
271*1583ef11SJosh Hilke ctrl = igb_read32(igb, E1000_GCR);
272*1583ef11SJosh Hilke ctrl &= ~E1000_GCR_CMPL_TMOUT_RESEND;
273*1583ef11SJosh Hilke igb_write32(igb, E1000_GCR, ctrl);
274*1583ef11SJosh Hilke
275*1583ef11SJosh Hilke /* Configure TX and RX descriptor rings */
276*1583ef11SJosh Hilke igb_write32(igb, E1000_TDBAL(0), (u32)iova_tx);
277*1583ef11SJosh Hilke igb_write32(igb, E1000_TDBAH(0), (u32)(iova_tx >> 32));
278*1583ef11SJosh Hilke igb_write32(igb, E1000_TDLEN(0), RING_SIZE * sizeof(struct igb_tx_desc));
279*1583ef11SJosh Hilke igb_write32(igb, E1000_TDH(0), 0);
280*1583ef11SJosh Hilke igb_write32(igb, E1000_TDT(0), 0);
281*1583ef11SJosh Hilke igb_write32(igb, E1000_TXDCTL(0), E1000_TXDCTL_QUEUE_ENABLE);
282*1583ef11SJosh Hilke
283*1583ef11SJosh Hilke igb_write32(igb, E1000_RDBAL(0), (u32)iova_rx);
284*1583ef11SJosh Hilke igb_write32(igb, E1000_RDBAH(0), (u32)(iova_rx >> 32));
285*1583ef11SJosh Hilke igb_write32(igb, E1000_RDLEN(0), RING_SIZE * sizeof(struct igb_rx_desc));
286*1583ef11SJosh Hilke igb_write32(igb, E1000_RDH(0), 0);
287*1583ef11SJosh Hilke igb_write32(igb, E1000_RDT(0), 0);
288*1583ef11SJosh Hilke
289*1583ef11SJosh Hilke /*
290*1583ef11SJosh Hilke * Select the advanced one-buffer descriptor format. Per 82576
291*1583ef11SJosh Hilke * datasheet section 7.1.5.2: "SRRCTL[n].DESCTYPE must be set to a
292*1583ef11SJosh Hilke * value other than 000b for the 82576 to write back the special
293*1583ef11SJosh Hilke * descriptors." struct igb_rx_desc matches the advanced one-buffer
294*1583ef11SJosh Hilke * writeback layout (section 7.1.5.2), so polling rx.wb.status_error
295*1583ef11SJosh Hilke * requires this format. Section 8.10.2 specifies DESCTYPE[27:25].
296*1583ef11SJosh Hilke *
297*1583ef11SJosh Hilke * The direct write also zeroes SRRCTL.BSIZEPACKET, which is
298*1583ef11SJosh Hilke * intentional: per section 7.1.3.1 a zero BSIZEPACKET falls back to
299*1583ef11SJosh Hilke * the RCTL.BSIZE buffer size, whose reset default (00b) is 2048
300*1583ef11SJosh Hilke * bytes -- ample for the loopback frames here.
301*1583ef11SJosh Hilke */
302*1583ef11SJosh Hilke igb_write32(igb, E1000_SRRCTL(0), E1000_SRRCTL_DESCTYPE_ADV_ONEBUF);
303*1583ef11SJosh Hilke
304*1583ef11SJosh Hilke igb_write32(igb, E1000_RXDCTL(0), E1000_RXDCTL_QUEUE_ENABLE);
305*1583ef11SJosh Hilke
306*1583ef11SJosh Hilke /*
307*1583ef11SJosh Hilke * Enable Receiver and Transmitter. RCTL.LBM_MAC is set in addition
308*1583ef11SJosh Hilke * to PHY loopback as a QEMU-only accommodation: QEMU's emulated igb
309*1583ef11SJosh Hilke * does not honor PHY register 0 bit 14 (PHY internal loopback) and
310*1583ef11SJosh Hilke * relies on RCTL.LBM_MAC to wrap TX descriptors back to the RX
311*1583ef11SJosh Hilke * queue. Datasheet 8.10.1 (RCTL register) advises "When using the
312*1583ef11SJosh Hilke * internal PHY, LBM should remain set to 00b", so setting LBM_MAC
313*1583ef11SJosh Hilke * here deviates from datasheet guidance; empirically the bit has
314*1583ef11SJosh Hilke * no observable effect on real 82576 hardware because MAC loopback
315*1583ef11SJosh Hilke * is not implemented (datasheet 3.5.6.2). Setting both lets the
316*1583ef11SJosh Hilke * selftest work on both real hardware and QEMU without conditional
317*1583ef11SJosh Hilke * code paths.
318*1583ef11SJosh Hilke */
319*1583ef11SJosh Hilke rctl = E1000_RCTL_EN | /* Receiver Enable */
320*1583ef11SJosh Hilke E1000_RCTL_UPE | /* Unicast Promiscuous (for dummy MAC) */
321*1583ef11SJosh Hilke E1000_RCTL_MPE | /* Multicast Promiscuous */
322*1583ef11SJosh Hilke E1000_RCTL_BAM | /* Broadcast Accept Mode */
323*1583ef11SJosh Hilke E1000_RCTL_LBM_MAC | /* MAC Loopback - for QEMU emulation only */
324*1583ef11SJosh Hilke E1000_RCTL_SECRC; /* Strip CRC (needed for memcmp) */
325*1583ef11SJosh Hilke igb_write32(igb, E1000_RCTL, rctl);
326*1583ef11SJosh Hilke igb_write32(igb, E1000_TCTL, E1000_TCTL_EN | E1000_TCTL_PSP);
327*1583ef11SJosh Hilke
328*1583ef11SJosh Hilke /*
329*1583ef11SJosh Hilke * Wait for TX and RX queues to be enabled. Per the RXDCTL/TXDCTL
330*1583ef11SJosh Hilke * register definitions (8.10.10/8.12.13), the per-queue enable bit
331*1583ef11SJosh Hilke * "remains zero" until the global RCTL.RXEN/TCTL.TXEN are set, so
332*1583ef11SJosh Hilke * E1000_RCTL_EN and E1000_TCTL_EN must already be written above.
333*1583ef11SJosh Hilke */
334*1583ef11SJosh Hilke retries = 2000;
335*1583ef11SJosh Hilke while (retries-- > 0) {
336*1583ef11SJosh Hilke if ((igb_read32(igb, E1000_TXDCTL(0)) & E1000_TXDCTL_QUEUE_ENABLE) &&
337*1583ef11SJosh Hilke (igb_read32(igb, E1000_RXDCTL(0)) & E1000_RXDCTL_QUEUE_ENABLE))
338*1583ef11SJosh Hilke break;
339*1583ef11SJosh Hilke usleep(10);
340*1583ef11SJosh Hilke }
341*1583ef11SJosh Hilke VFIO_ASSERT_GE(retries, 0);
342*1583ef11SJosh Hilke
343*1583ef11SJosh Hilke /*
344*1583ef11SJosh Hilke * Program MSI-X interrupt routing per 82576 datasheet:
345*1583ef11SJosh Hilke *
346*1583ef11SJosh Hilke * GPIE (section 7.3.2.11, Table 7-47): set Multiple_MSIX (bit 4) to
347*1583ef11SJosh Hilke * route interrupt causes through IVAR mapping, and EIAME (bit 30)
348*1583ef11SJosh Hilke * to apply EIAM on MSI-X assertion (without EIAME, EIAM only
349*1583ef11SJosh Hilke * applies on EICR read/write).
350*1583ef11SJosh Hilke *
351*1583ef11SJosh Hilke * EIAC (section 8.8.5): enable auto-clear of EICR for vector 0.
352*1583ef11SJosh Hilke * Without auto-clear the cause stays set after delivery and the
353*1583ef11SJosh Hilke * test can see spurious interrupts on the next memcpy batch.
354*1583ef11SJosh Hilke *
355*1583ef11SJosh Hilke * EIAM (section 8.8.6): enable auto-mask of EIMS for vector 0 on
356*1583ef11SJosh Hilke * MSI-X assertion (effective because EIAME is set).
357*1583ef11SJosh Hilke *
358*1583ef11SJosh Hilke * IVAR (section 7.3.1.2, register definition in 8.8.13): map RX
359*1583ef11SJosh Hilke * cause 0 to MSI-X vector 0 and mark the entry valid.
360*1583ef11SJosh Hilke */
361*1583ef11SJosh Hilke igb_write32(igb, E1000_GPIE, E1000_GPIE_MSIX_MODE | E1000_GPIE_EIAME);
362*1583ef11SJosh Hilke igb_write32(igb, E1000_EIAC, MSIX_VECTOR_MASK);
363*1583ef11SJosh Hilke igb_write32(igb, E1000_EIAM, MSIX_VECTOR_MASK);
364*1583ef11SJosh Hilke
365*1583ef11SJosh Hilke /* Map vector 0 to interrupt cause 0 and mark it valid */
366*1583ef11SJosh Hilke igb_write32(igb, E1000_IVAR0, E1000_IVAR_VALID);
367*1583ef11SJosh Hilke
368*1583ef11SJosh Hilke /* Enable interrupts on vector 0 */
369*1583ef11SJosh Hilke igb_write32(igb, E1000_EIMS, MSIX_VECTOR_MASK);
370*1583ef11SJosh Hilke
371*1583ef11SJosh Hilke /* Initialize driver state and capability limits */
372*1583ef11SJosh Hilke igb->tx_tail = 0;
373*1583ef11SJosh Hilke igb->rx_tail = 0;
374*1583ef11SJosh Hilke
375*1583ef11SJosh Hilke device->driver.max_memcpy_size = IGB_MAX_CHUNK_SIZE;
376*1583ef11SJosh Hilke device->driver.max_memcpy_count = RING_SIZE - 1;
377*1583ef11SJosh Hilke device->driver.msi = MSIX_VECTOR;
378*1583ef11SJosh Hilke }
379*1583ef11SJosh Hilke
igb_init(struct vfio_pci_device * device)380*1583ef11SJosh Hilke static void igb_init(struct vfio_pci_device *device)
381*1583ef11SJosh Hilke {
382*1583ef11SJosh Hilke struct igb *igb = to_igb_state(device);
383*1583ef11SJosh Hilke
384*1583ef11SJosh Hilke VFIO_ASSERT_GE(device->driver.region.size, sizeof(struct igb));
385*1583ef11SJosh Hilke
386*1583ef11SJosh Hilke igb->bar0 = device->bars[0].vaddr;
387*1583ef11SJosh Hilke
388*1583ef11SJosh Hilke igb_reset(igb);
389*1583ef11SJosh Hilke
390*1583ef11SJosh Hilke /*
391*1583ef11SJosh Hilke * Enable MSI-X via VFIO before device-side register programming.
392*1583ef11SJosh Hilke * vfio_pci_msix_enable() only touches the VFIO IRQ machinery and the
393*1583ef11SJosh Hilke * PCI MSI-X capability via config space; it has no ordering
394*1583ef11SJosh Hilke * dependency on the device-side writes performed by igb_hw_init().
395*1583ef11SJosh Hilke * Placing it here keeps igb_hw_init() reusable from the reset
396*1583ef11SJosh Hilke * recovery path (which calls vfio_pci_irq_reenable() instead).
397*1583ef11SJosh Hilke */
398*1583ef11SJosh Hilke vfio_pci_msix_enable(device, MSIX_VECTOR, 1);
399*1583ef11SJosh Hilke
400*1583ef11SJosh Hilke igb_hw_init(device);
401*1583ef11SJosh Hilke }
402*1583ef11SJosh Hilke
igb_remove(struct vfio_pci_device * device)403*1583ef11SJosh Hilke static void igb_remove(struct vfio_pci_device *device)
404*1583ef11SJosh Hilke {
405*1583ef11SJosh Hilke struct igb *igb = to_igb_state(device);
406*1583ef11SJosh Hilke
407*1583ef11SJosh Hilke igb_write32(igb, E1000_RCTL, 0);
408*1583ef11SJosh Hilke igb_write32(igb, E1000_TCTL, 0);
409*1583ef11SJosh Hilke igb_reset(igb);
410*1583ef11SJosh Hilke
411*1583ef11SJosh Hilke vfio_pci_msix_disable(device);
412*1583ef11SJosh Hilke }
413*1583ef11SJosh Hilke
igb_irq_disable(struct igb * igb)414*1583ef11SJosh Hilke static void igb_irq_disable(struct igb *igb)
415*1583ef11SJosh Hilke {
416*1583ef11SJosh Hilke igb_write32(igb, E1000_EIMC, MSIX_VECTOR_MASK);
417*1583ef11SJosh Hilke }
418*1583ef11SJosh Hilke
igb_irq_enable(struct igb * igb)419*1583ef11SJosh Hilke static void igb_irq_enable(struct igb *igb)
420*1583ef11SJosh Hilke {
421*1583ef11SJosh Hilke igb_write32(igb, E1000_EIMS, MSIX_VECTOR_MASK);
422*1583ef11SJosh Hilke }
423*1583ef11SJosh Hilke
igb_irq_clear(struct igb * igb)424*1583ef11SJosh Hilke static void igb_irq_clear(struct igb *igb)
425*1583ef11SJosh Hilke {
426*1583ef11SJosh Hilke /*
427*1583ef11SJosh Hilke * Use write-to-clear (datasheet 7.3.4.2). In MSI-X mode with EIAC
428*1583ef11SJosh Hilke * programmed, section 8.8.5 explicitly states "If any bits are set
429*1583ef11SJosh Hilke * in EIAC, the EICR register should not be read", which rules out
430*1583ef11SJosh Hilke * the read-to-clear path in 7.3.4.3. Bits not in EIAC are still
431*1583ef11SJosh Hilke * cleared by writing 1.
432*1583ef11SJosh Hilke */
433*1583ef11SJosh Hilke igb_write32(igb, E1000_EICR, 0xFFFFFFFF);
434*1583ef11SJosh Hilke }
435*1583ef11SJosh Hilke
igb_memcpy_start(struct vfio_pci_device * device,iova_t src,iova_t dst,u64 size,u64 count)436*1583ef11SJosh Hilke static void igb_memcpy_start(struct vfio_pci_device *device, iova_t src,
437*1583ef11SJosh Hilke iova_t dst, u64 size, u64 count)
438*1583ef11SJosh Hilke {
439*1583ef11SJosh Hilke struct igb *igb = to_igb_state(device);
440*1583ef11SJosh Hilke struct igb_rx_desc *rx;
441*1583ef11SJosh Hilke struct igb_tx_desc *tx;
442*1583ef11SJosh Hilke u32 i;
443*1583ef11SJosh Hilke
444*1583ef11SJosh Hilke VFIO_ASSERT_GE(size, 60,
445*1583ef11SJosh Hilke "IGB driver requires memcpy size to be at least 60 bytes (Ethernet minimum payload size)");
446*1583ef11SJosh Hilke
447*1583ef11SJosh Hilke igb_irq_disable(igb);
448*1583ef11SJosh Hilke
449*1583ef11SJosh Hilke for (i = 0; i < count; i++) {
450*1583ef11SJosh Hilke tx = &igb->tx_ring[igb->tx_tail];
451*1583ef11SJosh Hilke rx = &igb->rx_ring[igb->rx_tail];
452*1583ef11SJosh Hilke
453*1583ef11SJosh Hilke memset(tx, 0, sizeof(struct igb_tx_desc));
454*1583ef11SJosh Hilke memset(rx, 0, sizeof(struct igb_rx_desc));
455*1583ef11SJosh Hilke
456*1583ef11SJosh Hilke rx->read.pkt_addr = cpu_to_le64(dst);
457*1583ef11SJosh Hilke rx->read.hdr_addr = cpu_to_le64(0);
458*1583ef11SJosh Hilke
459*1583ef11SJosh Hilke tx->read.buffer_addr = cpu_to_le64(src);
460*1583ef11SJosh Hilke /*
461*1583ef11SJosh Hilke * Build an advanced data descriptor per 82576 datasheet
462*1583ef11SJosh Hilke * section 7.2.2.3. DEXT marks the descriptor as advanced
463*1583ef11SJosh Hilke * (required by hardware); DTYP=data selects the data
464*1583ef11SJosh Hilke * descriptor; IFCS asks the MAC to append the Ethernet
465*1583ef11SJosh Hilke * FCS (without it the frame is dropped as malformed);
466*1583ef11SJosh Hilke * EOP marks end of packet. DTALEN is the buffer length
467*1583ef11SJosh Hilke * in bits 15:0 of cmd_type_len.
468*1583ef11SJosh Hilke */
469*1583ef11SJosh Hilke tx->read.cmd_type_len = cpu_to_le32((uint32_t)size |
470*1583ef11SJosh Hilke E1000_ADVTXD_DTYP_DATA |
471*1583ef11SJosh Hilke E1000_ADVTXD_DCMD_DEXT |
472*1583ef11SJosh Hilke E1000_ADVTXD_DCMD_IFCS |
473*1583ef11SJosh Hilke E1000_ADVTXD_DCMD_EOP);
474*1583ef11SJosh Hilke /*
475*1583ef11SJosh Hilke * PAYLEN (section 7.2.2.3.11) is the total payload size
476*1583ef11SJosh Hilke * in olinfo_status[31:14].
477*1583ef11SJosh Hilke */
478*1583ef11SJosh Hilke tx->read.olinfo_status =
479*1583ef11SJosh Hilke cpu_to_le32((uint32_t)size << E1000_ADVTXD_PAYLEN_SHIFT);
480*1583ef11SJosh Hilke
481*1583ef11SJosh Hilke igb->tx_tail = (igb->tx_tail + 1) % RING_SIZE;
482*1583ef11SJosh Hilke igb->rx_tail = (igb->rx_tail + 1) % RING_SIZE;
483*1583ef11SJosh Hilke }
484*1583ef11SJosh Hilke
485*1583ef11SJosh Hilke igb_write32(igb, E1000_RDT(0), igb->rx_tail);
486*1583ef11SJosh Hilke igb_write32(igb, E1000_TDT(0), igb->tx_tail);
487*1583ef11SJosh Hilke }
488*1583ef11SJosh Hilke
489*1583ef11SJosh Hilke /*
490*1583ef11SJosh Hilke * Reset the device via VFIO_DEVICE_RESET (PCIe FLR on the 82576) and
491*1583ef11SJosh Hilke * re-program it. VFIO_DEVICE_RESET tears down the kernel-side MSI-X
492*1583ef11SJosh Hilke * trigger but leaves user-side eventfds intact, so re-arm the trigger
493*1583ef11SJosh Hilke * via vfio_pci_irq_reenable() before reprogramming so any caller-cached
494*1583ef11SJosh Hilke * eventfd remains valid.
495*1583ef11SJosh Hilke *
496*1583ef11SJosh Hilke * FLR clears device-side state to power-on reset values (datasheet
497*1583ef11SJosh Hilke * 4.2.1.5.1: a PF FLR is "equivalent to a D0->D3->D0 transition"), so
498*1583ef11SJosh Hilke * EIMS and EICR come back as 0 from their register-defined initial
499*1583ef11SJosh Hilke * values, and igb_hw_init() resets tx_tail/rx_tail to 0. The next
500*1583ef11SJosh Hilke * igb_memcpy_start() will memset each descriptor it touches before
501*1583ef11SJosh Hilke * submission, so no explicit IMC/EICR writes or ring memsets are
502*1583ef11SJosh Hilke * needed here.
503*1583ef11SJosh Hilke */
igb_error_reset_and_reinit(struct vfio_pci_device * device)504*1583ef11SJosh Hilke static void igb_error_reset_and_reinit(struct vfio_pci_device *device)
505*1583ef11SJosh Hilke {
506*1583ef11SJosh Hilke vfio_pci_device_reset(device);
507*1583ef11SJosh Hilke vfio_pci_msix_reenable(device, MSIX_VECTOR, 1);
508*1583ef11SJosh Hilke igb_hw_init(device);
509*1583ef11SJosh Hilke }
510*1583ef11SJosh Hilke
igb_memcpy_wait(struct vfio_pci_device * device)511*1583ef11SJosh Hilke static int igb_memcpy_wait(struct vfio_pci_device *device)
512*1583ef11SJosh Hilke {
513*1583ef11SJosh Hilke struct igb *igb = to_igb_state(device);
514*1583ef11SJosh Hilke struct igb_rx_desc *rx;
515*1583ef11SJosh Hilke u32 status = 0;
516*1583ef11SJosh Hilke u32 prev_tail;
517*1583ef11SJosh Hilke int retries;
518*1583ef11SJosh Hilke
519*1583ef11SJosh Hilke prev_tail = (igb->rx_tail + RING_SIZE - 1) % RING_SIZE;
520*1583ef11SJosh Hilke rx = &igb->rx_ring[prev_tail];
521*1583ef11SJosh Hilke
522*1583ef11SJosh Hilke /*
523*1583ef11SJosh Hilke * Real 82576 hardware processes the descriptor ring at line rate.
524*1583ef11SJosh Hilke * max_memcpy_size = (RING_SIZE - 1) * IGB_MAX_CHUNK_SIZE ~= 4 MB,
525*1583ef11SJosh Hilke * split into 4095 1 KB frames. At 1 Gb/s (~125 MB/s) the worst
526*1583ef11SJosh Hilke * valid memcpy takes ~32 ms on the wire, plus per-frame preamble,
527*1583ef11SJosh Hilke * SFD, IFG and FCS overhead (~3%) and descriptor fetch/writeback
528*1583ef11SJosh Hilke * latency. Wait up to ~200 ms before declaring the device hung;
529*1583ef11SJosh Hilke * ~6x the line-rate floor leaves comfortable headroom for host
530*1583ef11SJosh Hilke * scheduling jitter while keeping the intentional invalid-DMA
531*1583ef11SJosh Hilke * tests bounded.
532*1583ef11SJosh Hilke */
533*1583ef11SJosh Hilke retries = 200;
534*1583ef11SJosh Hilke while (retries-- > 0) {
535*1583ef11SJosh Hilke status = le32_to_cpu(READ_ONCE(rx->wb.status_error));
536*1583ef11SJosh Hilke if (status & 1)
537*1583ef11SJosh Hilke break;
538*1583ef11SJosh Hilke usleep(1000);
539*1583ef11SJosh Hilke }
540*1583ef11SJosh Hilke
541*1583ef11SJosh Hilke if (status & 1)
542*1583ef11SJosh Hilke /*
543*1583ef11SJosh Hilke * Ensure the test code doesn't speculatively read the DMA
544*1583ef11SJosh Hilke * destination buffer before we have verified that the
545*1583ef11SJosh Hilke * descriptor writeback is complete.
546*1583ef11SJosh Hilke */
547*1583ef11SJosh Hilke rmb();
548*1583ef11SJosh Hilke
549*1583ef11SJosh Hilke igb_irq_clear(igb);
550*1583ef11SJosh Hilke
551*1583ef11SJosh Hilke igb_irq_enable(igb);
552*1583ef11SJosh Hilke
553*1583ef11SJosh Hilke if (status & 1)
554*1583ef11SJosh Hilke return 0;
555*1583ef11SJosh Hilke
556*1583ef11SJosh Hilke /*
557*1583ef11SJosh Hilke * The descriptor never completed. On real 82576 hardware this
558*1583ef11SJosh Hilke * typically follows a DMA-read fault from one of the intentional
559*1583ef11SJosh Hilke * unmapped-IOVA tests; the fault leaves the descriptor engine
560*1583ef11SJosh Hilke * unable to service subsequent valid descriptors. CTRL.RST alone
561*1583ef11SJosh Hilke * reinitializes the queue registers but leaves the engine wedged
562*1583ef11SJosh Hilke * for the current process, so a broader VFIO_DEVICE_RESET (FLR)
563*1583ef11SJosh Hilke * is required.
564*1583ef11SJosh Hilke */
565*1583ef11SJosh Hilke igb_error_reset_and_reinit(device);
566*1583ef11SJosh Hilke
567*1583ef11SJosh Hilke return -ETIMEDOUT;
568*1583ef11SJosh Hilke }
569*1583ef11SJosh Hilke
igb_send_msi(struct vfio_pci_device * device)570*1583ef11SJosh Hilke static void igb_send_msi(struct vfio_pci_device *device)
571*1583ef11SJosh Hilke {
572*1583ef11SJosh Hilke struct igb *igb = to_igb_state(device);
573*1583ef11SJosh Hilke
574*1583ef11SJosh Hilke igb_write32(igb, E1000_EICS, MSIX_VECTOR_MASK);
575*1583ef11SJosh Hilke }
576*1583ef11SJosh Hilke
577*1583ef11SJosh Hilke const struct vfio_pci_driver_ops igb_ops = {
578*1583ef11SJosh Hilke .name = "igb",
579*1583ef11SJosh Hilke .probe = igb_probe,
580*1583ef11SJosh Hilke .init = igb_init,
581*1583ef11SJosh Hilke .remove = igb_remove,
582*1583ef11SJosh Hilke .memcpy_start = igb_memcpy_start,
583*1583ef11SJosh Hilke .memcpy_wait = igb_memcpy_wait,
584*1583ef11SJosh Hilke .send_msi = igb_send_msi,
585*1583ef11SJosh Hilke };
586