e1000: link auto-negotiation emulation

Indeed, there's nothing else except for the time spent on the
negotiation needs to be emulated. This is needed for resuming windows
guest from hibernation, as without a proper delay, qemu would send the
packet too early ( guest even does not have a proper intr handler),
which could lead windows guest hang.

This patch first introduces an array of function pointers to make it
possible to emulate per-register write behavior. Then traps the
PHY_CTRL register write and when guest want to restart the link auto
negotiation, we would down the link and mark the auto negotiation in
progress in PHY_STATUS register. After time, a timer with 500 ms (
which is the minimum timeout of auto-negotation specified in 802.3
spec). The link would be up when timer expired.

Test with resuming windows guest plus flood ping and linux ethtool
linkstatus test.

Signed-off-by: Jason Wang <jasowang@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
This commit is contained in:
Jason Wang 2012-03-22 18:02:24 +08:00 committed by Michael S. Tsirkin
parent f9c1cdf492
commit b9d03e352c

View File

@ -125,6 +125,8 @@ typedef struct E1000State_st {
uint16_t reading; uint16_t reading;
uint32_t old_eecd; uint32_t old_eecd;
} eecd_state; } eecd_state;
QEMUTimer *autoneg_timer;
} E1000State; } E1000State;
#define defreg(x) x = (E1000_##x>>2) #define defreg(x) x = (E1000_##x>>2)
@ -156,6 +158,34 @@ e1000_link_up(E1000State *s)
s->phy_reg[PHY_STATUS] |= MII_SR_LINK_STATUS; s->phy_reg[PHY_STATUS] |= MII_SR_LINK_STATUS;
} }
static void
set_phy_ctrl(E1000State *s, int index, uint16_t val)
{
if ((val & MII_CR_AUTO_NEG_EN) && (val & MII_CR_RESTART_AUTO_NEG)) {
s->nic->nc.link_down = true;
e1000_link_down(s);
s->phy_reg[PHY_STATUS] &= ~MII_SR_AUTONEG_COMPLETE;
DBGOUT(PHY, "Start link auto negotiation\n");
qemu_mod_timer(s->autoneg_timer, qemu_get_clock_ms(vm_clock) + 500);
}
}
static void
e1000_autoneg_timer(void *opaque)
{
E1000State *s = opaque;
s->nic->nc.link_down = false;
e1000_link_up(s);
s->phy_reg[PHY_STATUS] |= MII_SR_AUTONEG_COMPLETE;
DBGOUT(PHY, "Auto negotiation is completed\n");
}
static void (*phyreg_writeops[])(E1000State *, int, uint16_t) = {
[PHY_CTRL] = set_phy_ctrl,
};
enum { NPHYWRITEOPS = ARRAY_SIZE(phyreg_writeops) };
enum { PHY_R = 1, PHY_W = 2, PHY_RW = PHY_R | PHY_W }; enum { PHY_R = 1, PHY_W = 2, PHY_RW = PHY_R | PHY_W };
static const char phy_regcap[0x20] = { static const char phy_regcap[0x20] = {
[PHY_STATUS] = PHY_R, [M88E1000_EXT_PHY_SPEC_CTRL] = PHY_RW, [PHY_STATUS] = PHY_R, [M88E1000_EXT_PHY_SPEC_CTRL] = PHY_RW,
@ -167,7 +197,8 @@ static const char phy_regcap[0x20] = {
}; };
static const uint16_t phy_reg_init[] = { static const uint16_t phy_reg_init[] = {
[PHY_CTRL] = 0x1140, [PHY_STATUS] = 0x796d, // link initially up [PHY_CTRL] = 0x1140,
[PHY_STATUS] = 0x794d, /* link initially up with not completed autoneg */
[PHY_ID1] = 0x141, [PHY_ID2] = PHY_ID2_INIT, [PHY_ID1] = 0x141, [PHY_ID2] = PHY_ID2_INIT,
[PHY_1000T_CTRL] = 0x0e00, [M88E1000_PHY_SPEC_CTRL] = 0x360, [PHY_1000T_CTRL] = 0x0e00, [M88E1000_PHY_SPEC_CTRL] = 0x360,
[M88E1000_EXT_PHY_SPEC_CTRL] = 0x0d60, [PHY_AUTONEG_ADV] = 0xde1, [M88E1000_EXT_PHY_SPEC_CTRL] = 0x0d60, [PHY_AUTONEG_ADV] = 0xde1,
@ -234,6 +265,7 @@ static void e1000_reset(void *opaque)
{ {
E1000State *d = opaque; E1000State *d = opaque;
qemu_del_timer(d->autoneg_timer);
memset(d->phy_reg, 0, sizeof d->phy_reg); memset(d->phy_reg, 0, sizeof d->phy_reg);
memmove(d->phy_reg, phy_reg_init, sizeof phy_reg_init); memmove(d->phy_reg, phy_reg_init, sizeof phy_reg_init);
memset(d->mac_reg, 0, sizeof d->mac_reg); memset(d->mac_reg, 0, sizeof d->mac_reg);
@ -283,8 +315,12 @@ set_mdic(E1000State *s, int index, uint32_t val)
if (!(phy_regcap[addr] & PHY_W)) { if (!(phy_regcap[addr] & PHY_W)) {
DBGOUT(MDIC, "MDIC write reg %x unhandled\n", addr); DBGOUT(MDIC, "MDIC write reg %x unhandled\n", addr);
val |= E1000_MDIC_ERROR; val |= E1000_MDIC_ERROR;
} else } else {
if (addr < NPHYWRITEOPS && phyreg_writeops[addr]) {
phyreg_writeops[addr](s, index, data);
}
s->phy_reg[addr] = data; s->phy_reg[addr] = data;
}
} }
s->mac_reg[MDIC] = val | E1000_MDIC_READY; s->mac_reg[MDIC] = val | E1000_MDIC_READY;
@ -965,6 +1001,7 @@ static void (*macreg_writeops[])(E1000State *, int, uint32_t) = {
[MTA ... MTA+127] = &mac_writereg, [MTA ... MTA+127] = &mac_writereg,
[VFTA ... VFTA+127] = &mac_writereg, [VFTA ... VFTA+127] = &mac_writereg,
}; };
enum { NWRITEOPS = ARRAY_SIZE(macreg_writeops) }; enum { NWRITEOPS = ARRAY_SIZE(macreg_writeops) };
static void static void
@ -1158,6 +1195,8 @@ pci_e1000_uninit(PCIDevice *dev)
{ {
E1000State *d = DO_UPCAST(E1000State, dev, dev); E1000State *d = DO_UPCAST(E1000State, dev, dev);
qemu_del_timer(d->autoneg_timer);
qemu_free_timer(d->autoneg_timer);
memory_region_destroy(&d->mmio); memory_region_destroy(&d->mmio);
memory_region_destroy(&d->io); memory_region_destroy(&d->io);
qemu_del_vlan_client(&d->nic->nc); qemu_del_vlan_client(&d->nic->nc);
@ -1212,6 +1251,8 @@ static int pci_e1000_init(PCIDevice *pci_dev)
add_boot_device_path(d->conf.bootindex, &pci_dev->qdev, "/ethernet-phy@0"); add_boot_device_path(d->conf.bootindex, &pci_dev->qdev, "/ethernet-phy@0");
d->autoneg_timer = qemu_new_timer_ms(vm_clock, e1000_autoneg_timer, d);
return 0; return 0;
} }