Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4B83439E6F1 for ; Wed, 8 Jul 2026 23:19:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783552744; cv=none; b=Eof0I7JX8Q6RCoCY9L4nMfkcNnn78OsFxtfY1fpKHHoNjKB3xD71/JMgsmT37z4LCKslf16pTl1D0CYKwDjFMseAf+FFkvAuWW9n7KOJbZwBuPGupqpxAHRvdjX3ZRKcNjdCDFkMx0vf1kx28D7edgdl6LaYeZj+71sSl2zT8tQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783552744; c=relaxed/simple; bh=6hpTMNbQSx7gRAc2kPTAF6ia8Et3x/wMIvaglfoe6GI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=p546w24O7cXuIH1dSJkddbV32SLHTGlNHjeHNyX3CTUtY+5Uznji73W3kGDiwHeynMzKzRN/lGa9OEECKqpeP6yfwOThuUbG719VP6DUblI4mHZ6toXxhZ3uYw0LlJFwywsc3mZQsRGD3tBAHd6q1oE5oQHzDhygK6CiWJaS0u4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=RhYM0efy; arc=none smtp.client-ip=209.85.215.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jrhilke.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="RhYM0efy" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-ca124bf0189so244543a12.0 for ; Wed, 08 Jul 2026 16:19:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1783552742; x=1784157542; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=zdUA0zVJLNgMIak63rjrfgEe/Wk2tDdx62EEzowofes=; b=RhYM0efyj1rZxztqN/PyliDdOJ8xzjkO4I8Ko1qkCLOmHAK8ZLCsPv6Gk+0W9xkYYx O/VFwJaOA4yghiGWfN3U8Qhg7PbwwW9IJilk3D3d9dPoGFXlWNGZ7KlZ+EJQfOlbULHO 44sWYoaM7yOPmcTBiAvt0Vox5SaFhSV8saWod6jkl0lNs+BD7LAnoe7M+h6n/8giZwiU I9YkY4uM4SQyxYjQfnu+Vi+zYRNZINX279gCpBS0L5a0U/R185vegwiRNTZVd2QvlVJK mh6BHsZsWnug6zPgZ94diLmeQzm2fJ8q46SNGmwiLqzA9Ka64yOBSL141IKazepm2N5L qXhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783552742; x=1784157542; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=zdUA0zVJLNgMIak63rjrfgEe/Wk2tDdx62EEzowofes=; b=SLvrUxe6xMlAeMFADD4TEoMz2Rk9i+5/lmg1RisIN+snHOQoPLn7XIY3wW6gozx6gl UyhWxiSb3lyLUdhlnKJ/tUalDYhnv5/FeCetVw3We0XgYLvEIH5hQGf+DrAypQGIPeE0 VnaK1kFb7qubdTP6eMQPZGcbjO/BK+4fdQOdOBOV2B1/rTTs2gI2YuDfabRPs8YeldYu UEwjkqIgYYLE6+TJAV6GPB3PnwAwleLXPyif/+2X9CFV6UgdMFsO2/XQ5q5hH9MXeaRi vpH13YlJhRhl/z7/4E4pnxsSGrgVnVp2CNa76zNruoNb1POUBRzT8hhFZ6EtTh/mTJf0 Tu9w== X-Forwarded-Encrypted: i=1; AHgh+RpB+cEYlUWOMyb8UlDghWSrwcgu07TEAzSg00Rzh8c1l7q7SgvJCK5MhdRQalF9JddTQRE=@vger.kernel.org X-Gm-Message-State: AOJu0YxUdi+Uq1vYZGzdnKznWp+GuTRvw783/nPO+2ZEvdzqhQPGRIYm jBlenu7Z7cIDFziNwfa4OCs7q1OmEActMvTlabM3MWLuZ8LJXOiVRJ6cbl62cckY43I/d9TCT2z jcQWRUG0K X-Received: from pgcc16.prod.google.com ([2002:a63:1c10:0:b0:c8b:2706:11a7]) (user=jrhilke job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:6b87:b0:3bf:ee42:192 with SMTP id adf61e73a8af0-3c0d670d81amr361600637.6.1783552741359; Wed, 08 Jul 2026 16:19:01 -0700 (PDT) Date: Wed, 08 Jul 2026 23:18:49 +0000 In-Reply-To: <20260708-igb_v3_b4-v3-0-1272650ca614@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260708-igb_v3_b4-v3-0-1272650ca614@google.com> X-Mailer: b4 0.14.3 Message-ID: <20260708-igb_v3_b4-v3-2-1272650ca614@google.com> Subject: [PATCH v3 2/9] vfio: selftests: igb: Use PHY internal loopback on 82576 From: Josh Hilke To: David Matlack , Alex Williamson Cc: Shuah Khan , linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, Vipin Sharma , Josh Hilke , Alex Williamson Content-Type: text/plain; charset="utf-8" From: Alex Williamson The submitted driver waits for PHY autonegotiation and then enables MAC loopback via RCTL.LBM_MAC. QEMU's emulated igb tolerates this, but the 82576 datasheet rejects the MAC loopback path on real hardware: Section 3.5.6.1 (Loopback Support / General): "Use PHY Loopback instead of MAC Loopback on the 82576." Section 3.5.6.2 (MAC Loopback): "MAC Loopback is not used on this device." Section 3.5.6.3.1 (Setting the 82576 to PHY loopback Mode): set PHY control register bits 8 (duplex), 14 (loopback), clear bit 12 (autoneg enable), set the speed via bits 6 and 13. For 1Gb/s the register value is 0x4140. Section 8.10.1 (RCTL register): "When using the internal PHY, LBM should remain set to 00b and the PHY instead configured for loopback through the MDIO interface." Replace igb_phy_setup_autoneg() with igb_setup_loopback() which: - writes PHY register 0 with LOOPBACK | SPEED_1000 | FULL_DUPLEX - forces the MAC into 1Gb/s full duplex via CTRL.FRCSPD, CTRL.FRCDPX, CTRL.SPD_1000, CTRL.FD, CTRL.SLU; without forcing the MAC link state, the descriptor engine does not run on real hardware PHY internal loopback (section 3.5.6.3) wraps data at the end of the PHY datapath before the MDI, so the physical link state and cable speed are irrelevant. This matches the kernel ethtool selftest path in igb_integrated_phy_loopback() (drivers/net/ethernet/intel/igb/igb_ethtool.c). QEMU's igb emulation drives STATUS.LU exclusively from its autoneg-done timer or a network-backend link-state change; the guest cannot set STATUS.LU through CTRL.SLU. Its receive path checks STATUS.LU (e1000x_hw_rx_enabled in hw/net/e1000x_common.c) and drops every loopback frame until LU is set. Issue a one-shot autoneg-restart PHY write at the top of igb_setup_loopback() to kick the timer; the subsequent PHY write clears autoneg-enable, so on real hardware autoneg never starts and the write is a no-op. QEMU's igb also does not honor PHY register 0 bit 14 (PHY internal loopback) and relies on RCTL.LBM_MAC to wrap TX descriptors back to the RX queue. Datasheet 8.10.1 advises that LBM remain 00b when using the internal PHY, but empirically setting LBM_MAC has no observable effect on real 82576 (MAC loopback is not implemented per 3.5.6.2), so set it alongside PHY loopback as the actual loopback mechanism under QEMU. With these two QEMU-only accommodations the selftest works in both environments without environment-specific code paths. The submitted driver also wrote bit 14 (link disable) to PHY register 16 (port control). Datasheet 3.5.6.3.1 describes this as required for 10/100Mb/s but explicitly "not a must for 1G", and the kernel ethtool selftest omits it. It is dropped here. Flagging the removal in case a future regression bisects to this commit. Remove igb_read_phy() and the PHY status macros it served, which become unused: IGB_PHY_STATUS_REG_OFFSET, IGB_PHY_STATUS_AN_COMP. Keep IGB_PHY_CTRL_AN_ENABLE and IGB_PHY_CTRL_AN_RESTART for the QEMU-only autoneg kick; add the PHY ctrl loopback and CTRL force-link macros. Assisted-by: Claude:claude-opus-4-7 Signed-off-by: Alex Williamson --- tools/testing/selftests/vfio/lib/drivers/igb/igb.c | 122 +++++++++++++-------- 1 file changed, 74 insertions(+), 48 deletions(-) diff --git a/tools/testing/selftests/vfio/lib/drivers/igb/igb.c b/tools/testing/selftests/vfio/lib/drivers/igb/igb.c index 339ca88b9c55..58c6aceea63a 100644 --- a/tools/testing/selftests/vfio/lib/drivers/igb/igb.c +++ b/tools/testing/selftests/vfio/lib/drivers/igb/igb.c @@ -103,57 +103,70 @@ static int igb_write_phy(struct igb *igb, u32 offset, u16 data) return 0; } -static int igb_read_phy(struct igb *igb, u32 offset, u16 *data) +/* + * Configure the device for PHY internal loopback per 82576 datasheet + * section 3.5.6.3.1. Force the PHY to 1Gb/s full duplex with loopback + * enabled, then force the MAC link state to match. Internal loopback + * wraps data at the end of the PHY datapath (section 3.5.6.3), so the + * physical link state is irrelevant. + * + * Section 3.5.6.1 directs to "Use PHY Loopback instead of MAC Loopback + * on the 82576", and section 3.5.6.2 states "MAC Loopback is not used + * on this device." RCTL.LBM_MAC is still set elsewhere as a QEMU-only + * accommodation; see the RCTL programming in the caller for the + * rationale. + */ +static void igb_setup_loopback(struct igb *igb) { - u32 mdic; - int i; - - mdic = ((offset << E1000_MDIC_REG_SHIFT) | - (1 << E1000_MDIC_PHY_SHIFT) | - E1000_MDIC_OP_READ); - - igb_write32(igb, E1000_MDIC, mdic); - - for (i = 0; i < 1000; i++) { - usleep(50); - mdic = igb_read32(igb, E1000_MDIC); - if (mdic & E1000_MDIC_READY) - break; - } - - if (!(mdic & E1000_MDIC_READY)) - return -1; - - if (mdic & E1000_MDIC_ERROR) - return -1; - - *data = (u16)mdic; - return 0; -} - -static void igb_phy_setup_autoneg(struct igb *igb) -{ - int timeout_ms = 1000; - bool success = false; - u16 phy_status; + u32 ctrl; int ret; - int i; - /* Trigger auto-negotiation */ - ret = igb_write_phy(igb, MII_BMCR, + /* + * Kick the autoneg machinery solely to bring STATUS.LU up under + * QEMU's igb emulation: QEMU only updates STATUS.LU via its + * autoneg-done timer, and without LU set its receive path + * (e1000x_hw_rx_enabled) drops every loopback frame. On real + * hardware autoneg cannot complete before the next PHY write + * below clears the autoneg-enable bit, so this is effectively a + * no-op there. + */ + (void)igb_write_phy(igb, MII_BMCR, BMCR_ANENABLE | BMCR_ANRESTART); + + /* PHY control: loopback + 1Gb/s full duplex, autoneg disabled. */ + ret = igb_write_phy(igb, MII_BMCR, + BMCR_LOOPBACK | + BMCR_SPEED1000 | + BMCR_FULLDPLX); VFIO_ASSERT_EQ(ret, 0, "Failed to write PHY control register"); - for (i = 0; i < timeout_ms; i++) { - if (igb_read_phy(igb, MII_BMSR, &phy_status) == 0) { - success = !!(phy_status & BMSR_ANEGCOMPLETE); - if (success) - break; - } - usleep(1000); - } + /* + * Brief delay before forcing the MAC, mirroring the kernel ethtool + * selftest in igb_integrated_phy_loopback(). Not specified by the + * datasheet, but empirically required by the kernel driver. + */ + usleep(50000); + + /* + * Force the MAC to 1Gb/s full duplex with link up. Without forcing + * the link state the descriptor engine does not run, since the chip + * normally waits for a real negotiated link. + */ + ctrl = igb_read32(igb, E1000_CTRL); + ctrl &= ~E1000_CTRL_SPD_SEL; + ctrl |= E1000_CTRL_FRCSPD | + E1000_CTRL_FRCDPX | + E1000_CTRL_SPD_1000 | + E1000_CTRL_FD | + E1000_CTRL_SLU; + igb_write32(igb, E1000_CTRL, ctrl); - VFIO_ASSERT_TRUE(success, "Auto-negotiation did not complete in time"); + /* + * Settling delay matching the kernel ethtool selftest's msleep(500) + * at the tail of igb_integrated_phy_loopback(). Not specified by + * the datasheet; empirical, and inherited from the kernel driver. + */ + usleep(500000); } static int igb_probe(struct vfio_pci_device *device) @@ -180,6 +193,7 @@ static void igb_init(struct vfio_pci_device *device) iova_tx = to_iova(device, igb->tx_ring); iova_rx = to_iova(device, igb->rx_ring); + /* Reset device and disable all interrupts */ igb_write32(igb, E1000_CTRL, igb_read32(igb, E1000_CTRL) | E1000_CTRL_RST); /* * Must wait at least 1 millisecond after setting the reset bit before @@ -205,8 +219,8 @@ static void igb_init(struct vfio_pci_device *device) vfio_pci_config_writew(device, PCI_COMMAND, cmd_reg); } - /* Trigger autonegotiation. This enables IGB to transmit data. */ - igb_phy_setup_autoneg(igb); + /* Configure PHY internal loopback for testing. */ + igb_setup_loopback(igb); /* Configure TX and RX descriptor rings */ igb_write32(igb, E1000_TDBAL(0), (u32)iova_tx); @@ -232,10 +246,22 @@ static void igb_init(struct vfio_pci_device *device) usleep(10); } - /* Enable Receiver and Transmitter */ + /* + * Enable Receiver and Transmitter. RCTL.LBM_MAC is set in addition + * to PHY loopback as a QEMU-only accommodation: QEMU's emulated igb + * does not honor PHY register 0 bit 14 (PHY internal loopback) and + * relies on RCTL.LBM_MAC to wrap TX descriptors back to the RX + * queue. Datasheet 8.10.1 (RCTL register) advises "When using the + * internal PHY, LBM should remain set to 00b", so setting LBM_MAC + * here deviates from datasheet guidance; empirically the bit has + * no observable effect on real 82576 hardware because MAC loopback + * is not implemented (datasheet 3.5.6.2). Setting both lets the + * selftest work on both real hardware and QEMU without conditional + * code paths. + */ rctl = E1000_RCTL_EN | /* Receiver Enable */ E1000_RCTL_UPE | /* Unicast Promiscuous (for dummy MAC) */ - E1000_RCTL_LBM_MAC | /* MAC Loopback Mode */ + E1000_RCTL_LBM_MAC | /* MAC Loopback - for QEMU emulation only */ E1000_RCTL_SECRC; /* Strip CRC (needed for memcmp) */ igb_write32(igb, E1000_RCTL, rctl); igb_write32(igb, E1000_TCTL, E1000_TCTL_EN); -- 2.55.0.795.g602f6c329a-goog