Skip to contentSkip to conversation

e1000: link auto-negotiation fixes

PATCH Superseded Gabriel L. Somlo 5 participants 13 messages Jun 16, 2014 – Jun 19, 2014

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover e1000: link auto-negotiation fixes

This series contains a few fixes and improvements in the emulation of link auto-negotiation:

- use auto-negotiation when the link is bounced externally (e.g. via set_link <foo> down/up on the qemu monitor command line).

- allow mii_tool on linux access to all the phy registers and flags it requires in order to report a successfully auto-negotiated link.

- inject LSC interrupt upon successful link auto-negotiation (required by stock OS X e1000 driver).

See additional prose following commit logs in 2/3 (making up excuses for checkpatch error) and 3/3 (further thoughts on OS X vs. Linux/Windows re. auto-negotiation and LSC injection).

Thanks much,
Gabriel

Gabriel L. Somlo (3):
e1000: emulate auto-negotiation during external link status change
e1000: improve auto-negotiation reporting via mii-tool
e1000: signal guest on successful link auto-negotiation

 hw/net/e1000.c      | 24 +++++++++++++++++++-----
 hw/net/e1000_regs.h |  3 +++
 2 files changed, 22 insertions(+), 5 deletions(-)
Signature · 1 line
1.9.3
#1 lore raw

PATCH 1/3 e1000: emulate auto-negotiation during external link status change

This patch emulates auto-negotiation when the network link status is modified externally (i.e. via "set_link <id> off/on").

Also, a couple of cleanup items regarding the PHY status register:
- unset AUTONEG_COMPLETE during link_down()
- set AUTONEG_COMPLETE during autoneg_timer() only if we actually brought the link up.

---
 hw/net/e1000.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
hw/net/e1000.c +12 -4
index 57bdffd..f0070ca 100644
@@ -175,6 +175,7 @@ e1000_link_down(E1000State *s)
 {
     s->mac_reg[STATUS] &= ~E1000_STATUS_LU;
     s->phy_reg[PHY_STATUS] &= ~MII_SR_LINK_STATUS;
+    s->phy_reg[PHY_STATUS] &= ~MII_SR_AUTONEG_COMPLETE;
 }
 
 static void
@@ -197,7 +198,6 @@ set_phy_ctrl(E1000State *s, int index, uint16_t val)
     }
     if ((val & MII_CR_AUTO_NEG_EN) && (val & MII_CR_RESTART_AUTO_NEG)) {
         e1000_link_down(s);
-        s->phy_reg[PHY_STATUS] &= ~MII_SR_AUTONEG_COMPLETE;
         DBGOUT(PHY, "Start link auto negotiation\n");
         timer_mod(s->autoneg_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 500);
     }
@@ -209,9 +209,9 @@ e1000_autoneg_timer(void *opaque)
     E1000State *s = opaque;
     if (!qemu_get_queue(s->nic)->link_down) {
         e1000_link_up(s);
+        s->phy_reg[PHY_STATUS] |= MII_SR_AUTONEG_COMPLETE;
+        DBGOUT(PHY, "Auto negotiation is completed\n");
     }
-    s->phy_reg[PHY_STATUS] |= MII_SR_AUTONEG_COMPLETE;
-    DBGOUT(PHY, "Auto negotiation is completed\n");
 }
 
 static void (*phyreg_writeops[])(E1000State *, int, uint16_t) = {
@@ -853,7 +853,15 @@ e1000_set_link_status(NetClientState *nc)
     if (nc->link_down) {
         e1000_link_down(s);
     } else {
-        e1000_link_up(s);
+        if (s->phy_reg[PHY_CTRL] & MII_CR_AUTO_NEG_EN &&
+            s->phy_reg[PHY_CTRL] & MII_CR_RESTART_AUTO_NEG &&
+            !(s->phy_reg[PHY_STATUS] & MII_SR_AUTONEG_COMPLETE)) {
+            /* emulate auto-negotiation if supported */
+            timer_mod(s->autoneg_timer,
+                      qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 500);
+        } else {
+            e1000_link_up(s);
+        }
     }
 
     if (s->mac_reg[STATUS] != old_status)
Signature · 1 line
1.9.3
#2 lore raw

Re: [Qemu-devel] [PATCH v1 1/3] e1000: emulate auto-negotiation during external link status change

Quoted 46 lines · Gabriel L. Somlo

This patch emulates auto-negotiation when the network link status is modified externally (i.e. via "set_link <id> off/on").

Also, a couple of cleanup items regarding the PHY status register:
- unset AUTONEG_COMPLETE during link_down()
- set AUTONEG_COMPLETE during autoneg_timer() only if we actually brought the link up.

---
 hw/net/e1000.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
hw/net/e1000.c +3 -3
index 57bdffd..f0070ca 100644
@@ -175,6 +175,7 @@ e1000_link_down(E1000State *s)
 {
     s->mac_reg[STATUS] &= ~E1000_STATUS_LU;
     s->phy_reg[PHY_STATUS] &= ~MII_SR_LINK_STATUS;
+    s->phy_reg[PHY_STATUS] &= ~MII_SR_AUTONEG_COMPLETE;
 }
 
 static void
@@ -197,7 +198,6 @@ set_phy_ctrl(E1000State *s, int index, uint16_t val)
     }
     if ((val & MII_CR_AUTO_NEG_EN) && (val & MII_CR_RESTART_AUTO_NEG)) {
         e1000_link_down(s);
-        s->phy_reg[PHY_STATUS] &= ~MII_SR_AUTONEG_COMPLETE;
         DBGOUT(PHY, "Start link auto negotiation\n");
         timer_mod(s->autoneg_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 500);
     }
@@ -209,9 +209,9 @@ e1000_autoneg_timer(void *opaque)
     E1000State *s = opaque;
     if (!qemu_get_queue(s->nic)->link_down) {
         e1000_link_up(s);
+        s->phy_reg[PHY_STATUS] |= MII_SR_AUTONEG_COMPLETE;
+        DBGOUT(PHY, "Auto negotiation is completed\n");
     }
-    s->phy_reg[PHY_STATUS] |= MII_SR_AUTONEG_COMPLETE;
-    DBGOUT(PHY, "Auto negotiation is completed\n");
 }
 
 static void (*phyreg_writeops[])(E1000State *, int, uint16_t) = {

hw/net/e1000.c e1000_set_link_status(NetClientState *nc) · line 858

Quoted 7 lines
hw/net/e1000.c +3 -1
@@ -853,7 +853,15 @@ e1000_set_link_status(NetClientState *nc)
     if (nc->link_down) {
         e1000_link_down(s);
     } else {
-        e1000_link_up(s);
+        if (s->phy_reg[PHY_CTRL] & MII_CR_AUTO_NEG_EN &&
+            s->phy_reg[PHY_CTRL] & MII_CR_RESTART_AUTO_NEG &&
+            !(s->phy_reg[PHY_STATUS] & MII_SR_AUTONEG_COMPLETE)) {

I think we also must test E1000_FLAG_AUTONEG in s->compat_flags. No?

Quoted 11 lines
hw/net/e1000.c +6 -0
+            /* emulate auto-negotiation if supported */
+            timer_mod(s->autoneg_timer,
+                      qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 500);
+        } else {
+            e1000_link_up(s);
+        }
     }
 
     if (s->mac_reg[STATUS] != old_status)
Signature · 1 line
1.9.3
#3 lore raw

PATCH 2/3 e1000: improve auto-negotiation reporting via mii-tool

In reply to Gabriel L. Somlo

Using mii-tool (on F20-live), the following output is produced:

SIOCGMIIREG on ens3 failed: Input/output error
ens3: no autonegotiation, 1000baseT-FD flow-control, link ok

The first line (SIOCGMIIREG error) is due to mii-tool's inability to read the PHY auto-negotiation expansion register.
On the second line, "no autonegotiation" is wrong, and caused by the absence of a flag in the link partner ability register which would indicate that our link partner has acked us. This flag not documented in the Intel e1000 manual (bit shown as "reserved"), but mii-tool uses it as LPA_LPACK from /usr/include/linux/mii.h.

This patch adds read access to PHY_AUTONEG_EXP and defines the link partner ack flag, allowing mii-tool to generate output as normally expected:

ens3: negotiated 1000baseT-FD flow-control, link ok

---

Checkpatch generates two errors for this patch, related to where PHY_AUTONEG_EXP is added to phy_regcap:

ERROR: code indent should never use tabs
#56: FILE: hw/net/e1000.c:232:
+ [PHY_ID2] = PHY_R,^I^I[M88E1000_PHY_SPEC_STATUS] = PHY_R,$

ERROR: space prohibited before open square bracket '[' #56: FILE: hw/net/e1000.c:232:
+ [PHY_ID2] = PHY_R, [M88E1000_PHY_SPEC_STATUS] = PHY_R,

total: 2 errors, 0 warnings, 32 lines checked

I merely added a comma at the end of the [PHY_ID2]... line, so I can add the entry for PHY_AUTONEG_EXP. Tabs are there in all previous lines, so not much of a choice unless we want to rat-hole into replacing tabs everywhere first...

Thanks,
Gabriel

 hw/net/e1000.c      | 5 ++++-
 hw/net/e1000_regs.h | 3 +++
 2 files changed, 7 insertions(+), 1 deletion(-)
hw/net/e1000.c +4 -1
index f0070ca..04e4044 100644
@@ -176,6 +176,7 @@ e1000_link_down(E1000State *s)
     s->mac_reg[STATUS] &= ~E1000_STATUS_LU;
     s->phy_reg[PHY_STATUS] &= ~MII_SR_LINK_STATUS;
     s->phy_reg[PHY_STATUS] &= ~MII_SR_AUTONEG_COMPLETE;
+    s->phy_reg[PHY_LP_ABILITY] &= ~MII_LPAR_LPACK;
 }
 
 static void
@@ -209,6 +210,7 @@ e1000_autoneg_timer(void *opaque)
     E1000State *s = opaque;
     if (!qemu_get_queue(s->nic)->link_down) {
         e1000_link_up(s);
+        s->phy_reg[PHY_LP_ABILITY] |= MII_LPAR_LPACK;
         s->phy_reg[PHY_STATUS] |= MII_SR_AUTONEG_COMPLETE;
         DBGOUT(PHY, "Auto negotiation is completed\n");
     }
@@ -227,7 +229,8 @@ static const char phy_regcap[0x20] = {
     [PHY_CTRL] = PHY_RW,	[PHY_1000T_CTRL] = PHY_RW,
     [PHY_LP_ABILITY] = PHY_R,	[PHY_1000T_STATUS] = PHY_R,
     [PHY_AUTONEG_ADV] = PHY_RW,	[M88E1000_RX_ERR_CNTR] = PHY_R,
-    [PHY_ID2] = PHY_R,		[M88E1000_PHY_SPEC_STATUS] = PHY_R
+    [PHY_ID2] = PHY_R,		[M88E1000_PHY_SPEC_STATUS] = PHY_R,
+    [PHY_AUTONEG_EXP] = PHY_R,
 };
 
 /* PHY_ID2 documented in 8254x_GBe_SDM.pdf, pp. 250 */
hw/net/e1000_regs.h +3 -0
index 13ac671..60b96aa 100644
@@ -384,6 +384,9 @@
 #define MII_SR_100X_FD_CAPS      0x4000	/* 100X  Full Duplex Capable */
 #define MII_SR_100T4_CAPS        0x8000	/* 100T4 Capable */
 
+/* PHY Link Partner Ability Register */
+#define MII_LPAR_LPACK           0x4000 /* Acked by link partner */
+
 /* Interrupt Cause Read */
 #define E1000_ICR_TXDW          0x00000001 /* Transmit desc written back */
 #define E1000_ICR_TXQE          0x00000002 /* Transmit Queue empty */
Signature · 1 line
1.9.3
#4 lore raw

PATCH 3/3 e1000: signal guest on successful link auto-negotiation

In reply to Gabriel L. Somlo

Generate a link status change interrupt once link auto-netotiation is successfully completed. This does not affect Linux and Windows (XP and 7 tested) in any way, but is needed by the stock OS X driver (AppleIntel8254XEthernet.kext), which would otherwise fail to notice the link status change event.

---

While Windows and Linux e1000 drivers (and, apparently, the open-source AppleIntelE1000.kext from SourceForge) don't need an LSC interrupt to detect when the link comes on (maybe because they're polling for it, I'd have to look through the source to be 100% sure), Apple's stock, "out of the box" e1000 driver (AppleIntel8254XEthernet.kext) will not become aware of the link status change unless we send it an interrupt.

I believe it's not an unreasonable thing to do, and it's definitely not something that happens in the BIOS, as I previously (mistakenly) thought...

As I mentioned in the commit blurb above, Linux and Windows don't mind the additional LSC interrupt (not that they should, but just making sure... :).

Finally, it may be better to simply move e1000_autoneg_timer() to somewhere *after* set_ics() in the source file, to avoid the forward-reference, but I figured I'd keep the visual noise to a minimum for now...

Thanks,
Gabriel

 hw/net/e1000.c | 3 +++
 1 file changed, 3 insertions(+)
hw/net/e1000.c +3 -0
index 04e4044..0c9b472 100644
@@ -204,6 +204,8 @@ set_phy_ctrl(E1000State *s, int index, uint16_t val)
     }
 }
 
+static void set_ics(E1000State *s, int index, uint32_t val);
+
 static void
 e1000_autoneg_timer(void *opaque)
 {
@@ -213,6 +215,7 @@ e1000_autoneg_timer(void *opaque)
         s->phy_reg[PHY_LP_ABILITY] |= MII_LPAR_LPACK;
         s->phy_reg[PHY_STATUS] |= MII_SR_AUTONEG_COMPLETE;
         DBGOUT(PHY, "Auto negotiation is completed\n");
+        set_ics(s, 0, E1000_ICS_LSC); /* signal link status change to guest */
     }
 }
 
Signature · 1 line
1.9.3
Reviewed-by on the v1 series
#5 lore raw
In reply to Gabriel L. Somlo
Quoted 9 lines · Gabriel L. Somlo

This series contains a few fixes and improvements in the emulation of link auto-negotiation:

- use auto-negotiation when the link is bounced externally (e.g. via set_link <foo> down/up on the qemu monitor command line).

- allow mii_tool on linux access to all the phy registers and flags it requires in order to report a successfully auto-negotiated link.

Quoted 6 lines

- inject LSC interrupt upon successful link auto-negotiation (required by stock OS X e1000 driver).

See additional prose following commit logs in 2/3 (making up excuses for checkpatch error) and 3/3 (further thoughts on OS X vs. Linux/Windows re. auto-negotiation and LSC injection).

Looks very reasonable and straight forward to me.

Alex

#6 lore raw

Il 17/06/2014 00:35, Alexander Graf ha scritto:

Quoted 14 lines · Gabriel L. Somlo
Quoted 12 lines

This series contains a few fixes and improvements in the emulation of link auto-negotiation:

- use auto-negotiation when the link is bounced externally (e.g. via set_link <foo> down/up on the qemu monitor command line).

- allow mii_tool on linux access to all the phy registers and flags it requires in order to report a successfully auto-negotiated link.

- inject LSC interrupt upon successful link auto-negotiation (required by stock OS X e1000 driver).

Quoted 7 lines
Quoted 3 lines

See additional prose following commit logs in 2/3 (making up excuses for checkpatch error) and 3/3 (further thoughts on OS X vs. Linux/Windows re. auto-negotiation and LSC injection).

Looks very reasonable and straight forward to me.

Would you pick it up and send a pull request for it?

Paolo

#7 lore raw
Quoted 23 lines

Am 17.06.2014 um 06:31 schrieb Paolo Bonzini <[email protected]>:

Il 17/06/2014 00:35, Alexander Graf ha scritto:

Quoted 18 lines
Quoted 17 lines · Gabriel L. Somlo

This series contains a few fixes and improvements in the emulation of link auto-negotiation:

- use auto-negotiation when the link is bounced externally (e.g. via set_link <foo> down/up on the qemu monitor command line).

- allow mii_tool on linux access to all the phy registers and flags it requires in order to report a successfully auto-negotiated link.

- inject LSC interrupt upon successful link auto-negotiation (required by stock OS X e1000 driver).

See additional prose following commit logs in 2/3 (making up excuses for checkpatch error) and 3/3 (further thoughts on OS X vs. Linux/Windows re. auto-negotiation and LSC injection).

Quoted 5 lines
Quoted 3 lines

Looks very reasonable and straight forward to me.

Would you pick it up and send a pull request for it?

Is e1000 / networking unmaintained?

Alex

#8 lore raw

Il 17/06/2014 08:33, Alexander Graf ha scritto:

Quoted 6 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines

Looks very reasonable and straight forward to me.

Would you pick it up and send a pull request for it?

Is e1000 / networking unmaintained?

Doh, it's obviously Stefan's area.

Paolo

#9 lore raw
In reply to Alexander Graf
Quoted 25 lines · Alexander Graf
Quoted 25 lines

Am 17.06.2014 um 06:31 schrieb Paolo Bonzini <[email protected]>:

Il 17/06/2014 00:35, Alexander Graf ha scritto:

Quoted 20 lines
Quoted 11 lines · Gabriel L. Somlo

This series contains a few fixes and improvements in the emulation of link auto-negotiation:

- use auto-negotiation when the link is bounced externally (e.g. via set_link <foo> down/up on the qemu monitor command line).

- allow mii_tool on linux access to all the phy registers and flags it requires in order to report a successfully auto-negotiated link.

Quoted 6 lines

- inject LSC interrupt upon successful link auto-negotiation (required by stock OS X e1000 driver).

See additional prose following commit logs in 2/3 (making up excuses for checkpatch error) and 3/3 (further thoughts on OS X vs. Linux/Windows re. auto-negotiation and LSC injection).

Looks very reasonable and straight forward to me.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Would you pick it up and send a pull request for it?

Is e1000 / networking unmaintained?

Alex

It isn't - both me and Stefan merge patches there. It hasn't been a full day since patch was posted, and I was unwell yesterday.
Why the query? Is there any urgency?

#10 lore raw
Quoted 24 lines · Michael S. Tsirkin
Quoted 23 lines · Alexander Graf
Quoted 23 lines

Am 17.06.2014 um 06:31 schrieb Paolo Bonzini <[email protected]>:

Il 17/06/2014 00:35, Alexander Graf ha scritto:

Quoted 19 lines
Quoted 10 lines · Gabriel L. Somlo

This series contains a few fixes and improvements in the emulation of link auto-negotiation:

- use auto-negotiation when the link is bounced externally (e.g. via set_link <foo> down/up on the qemu monitor command line).

- allow mii_tool on linux access to all the phy registers and flags it requires in order to report a successfully auto-negotiated link.

Quoted 6 lines

- inject LSC interrupt upon successful link auto-negotiation (required by stock OS X e1000 driver).

See additional prose following commit logs in 2/3 (making up excuses for checkpatch error) and 3/3 (further thoughts on OS X vs. Linux/Windows re. auto-negotiation and LSC injection).

Looks very reasonable and straight forward to me.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Would you pick it up and send a pull request for it?

Is e1000 / networking unmaintained?

Alex

It isn't - both me and Stefan merge patches there. It hasn't been a full day since patch was posted, and I was unwell yesterday.
Why the query? Is there any urgency?

No urgency at all, probably just a misunderstanding on Paolos side that I'm reviewing non-PPC patches for a change ;).

Alex

Reviewed-by on the v1 series
#11 lore raw
In reply to Gabriel L. Somlo
Quoted 19 lines · Gabriel L. Somlo

This series contains a few fixes and improvements in the emulation of link auto-negotiation:

- use auto-negotiation when the link is bounced externally (e.g. via set_link <foo> down/up on the qemu monitor command line).

- allow mii_tool on linux access to all the phy registers and flags it requires in order to report a successfully auto-negotiated link.

- inject LSC interrupt upon successful link auto-negotiation (required by stock OS X e1000 driver).

See additional prose following commit logs in 2/3 (making up excuses for checkpatch error) and 3/3 (further thoughts on OS X vs. Linux/Windows re. auto-negotiation and LSC injection).

Thanks much,
Gabriel

Quoted 8 lines

Gabriel L. Somlo (3):
e1000: emulate auto-negotiation during external link status change
e1000: improve auto-negotiation reporting via mii-tool
e1000: signal guest on successful link auto-negotiation

 hw/net/e1000.c      | 24 +++++++++++++++++++-----
 hw/net/e1000_regs.h |  3 +++
 2 files changed, 22 insertions(+), 5 deletions(-)

From my limited e1000 understanding and some checking against the datasheet:

Since Michael Tsirkin's attention has been drawn, I'll leave this series for him to review and merge.

#12 lore raw
In reply to Gabriel L. Somlo
Quoted 12 lines · Gabriel L. Somlo

This series contains a few fixes and improvements in the emulation of link auto-negotiation:

- use auto-negotiation when the link is bounced externally (e.g. via set_link <foo> down/up on the qemu monitor command line).

- allow mii_tool on linux access to all the phy registers and flags it requires in order to report a successfully auto-negotiated link.

- inject LSC interrupt upon successful link auto-negotiation (required by stock OS X e1000 driver).

Quoted 6 lines

See additional prose following commit logs in 2/3 (making up excuses for checkpatch error) and 3/3 (further thoughts on OS X vs. Linux/Windows re. auto-negotiation and LSC injection).

Thanks much,
Gabriel

Looks good to me.
We should be careful to bring link immediately up and not to run the timer if running in legacy pc compatibility mode:

QEMU 1.3 does not support link auto-negotiation emulation, so if we migrate during auto negotiation, after migration the link will be down.

Quoted 11 lines

Gabriel L. Somlo (3):
e1000: emulate auto-negotiation during external link status change
e1000: improve auto-negotiation reporting via mii-tool
e1000: signal guest on successful link auto-negotiation

 hw/net/e1000.c      | 24 +++++++++++++++++++-----
 hw/net/e1000_regs.h |  3 +++
 2 files changed, 22 insertions(+), 5 deletions(-)
Signature · 1 line
1.9.3