diff mbox

[v2,1/4] e1000: emulate auto-negotiation during external link status change

Message ID 1403193336-32101-2-git-send-email-somlo@cmu.edu
State New
Headers show

Commit Message

Gabriel L. Somlo June 19, 2014, 3:55 p.m. UTC
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:
  - unset PHY status reg. AUTONEG_COMPLETE during link_down()
  - set PHY status reg. AUTONEG_COMPLETE during autoneg_timer() only
    if we actually brought the link up.
  - group all checks for "can we, and should we autonegotiate?"
    together for more clarity.

Signed-off-by: Gabriel Somlo <somlo@cmu.edu>
Reviewed-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
---
 hw/net/e1000.c | 35 +++++++++++++++++++----------------
 1 file changed, 19 insertions(+), 16 deletions(-)

Comments

Michael S. Tsirkin June 19, 2014, 4:26 p.m. UTC | #1
On Thu, Jun 19, 2014 at 11:55:33AM -0400, Gabriel L. Somlo wrote:
> 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:
>   - unset PHY status reg. AUTONEG_COMPLETE during link_down()
>   - set PHY status reg. AUTONEG_COMPLETE during autoneg_timer() only
>     if we actually brought the link up.
>   - group all checks for "can we, and should we autonegotiate?"
>     together for more clarity.
> 
> Signed-off-by: Gabriel Somlo <somlo@cmu.edu>
> Reviewed-by: Alexander Graf <agraf@suse.de>
> Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>

Now you did this cleanup, let's have a function
to check autonegotiation in a single place.

Can be patch on top.

> ---
>  hw/net/e1000.c | 35 +++++++++++++++++++----------------
>  1 file changed, 19 insertions(+), 16 deletions(-)
> 
> diff --git a/hw/net/e1000.c b/hw/net/e1000.c
> index 57bdffd..9c6af06 100644
> --- a/hw/net/e1000.c
> +++ b/hw/net/e1000.c
> @@ -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,16 @@ e1000_set_link_status(NetClientState *nc)
>      if (nc->link_down) {
>          e1000_link_down(s);
>      } else {
> -        e1000_link_up(s);
> +        if (s->compat_flags & E1000_FLAG_AUTONEG &&
> +            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)
> @@ -1279,16 +1288,13 @@ static void e1000_pre_save(void *opaque)
>          e1000_mit_timer(s);
>      }
>  
> -    if (!(s->compat_flags & E1000_FLAG_AUTONEG)) {
> -        return;
> -    }
> -
>      /*
> -     * If link is down and auto-negotiation is ongoing, complete
> -     * auto-negotiation immediately.  This allows is to look at
> -     * MII_SR_AUTONEG_COMPLETE to infer link status on load.
> +     * If link is down and auto-negotiation is supported and ongoing,
> +     * complete auto-negotiation immediately. This allows us to look
> +     * at MII_SR_AUTONEG_COMPLETE to infer link status on load.
>       */
>      if (nc->link_down &&
> +        s->compat_flags & E1000_FLAG_AUTONEG &&
>          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;
> @@ -1313,11 +1319,8 @@ static int e1000_post_load(void *opaque, int version_id)
>       * Alternatively, restart link negotiation if it was in progress. */
>      nc->link_down = (s->mac_reg[STATUS] & E1000_STATUS_LU) == 0;
>  
> -    if (!(s->compat_flags & E1000_FLAG_AUTONEG)) {
> -        return 0;
> -    }
> -
> -    if (s->phy_reg[PHY_CTRL] & MII_CR_AUTO_NEG_EN &&
> +    if (s->compat_flags & E1000_FLAG_AUTONEG &&
> +        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)) {
>          nc->link_down = false;
> -- 
> 1.9.3
diff mbox

Patch

diff --git a/hw/net/e1000.c b/hw/net/e1000.c
index 57bdffd..9c6af06 100644
--- a/hw/net/e1000.c
+++ b/hw/net/e1000.c
@@ -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,16 @@  e1000_set_link_status(NetClientState *nc)
     if (nc->link_down) {
         e1000_link_down(s);
     } else {
-        e1000_link_up(s);
+        if (s->compat_flags & E1000_FLAG_AUTONEG &&
+            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)
@@ -1279,16 +1288,13 @@  static void e1000_pre_save(void *opaque)
         e1000_mit_timer(s);
     }
 
-    if (!(s->compat_flags & E1000_FLAG_AUTONEG)) {
-        return;
-    }
-
     /*
-     * If link is down and auto-negotiation is ongoing, complete
-     * auto-negotiation immediately.  This allows is to look at
-     * MII_SR_AUTONEG_COMPLETE to infer link status on load.
+     * If link is down and auto-negotiation is supported and ongoing,
+     * complete auto-negotiation immediately. This allows us to look
+     * at MII_SR_AUTONEG_COMPLETE to infer link status on load.
      */
     if (nc->link_down &&
+        s->compat_flags & E1000_FLAG_AUTONEG &&
         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;
@@ -1313,11 +1319,8 @@  static int e1000_post_load(void *opaque, int version_id)
      * Alternatively, restart link negotiation if it was in progress. */
     nc->link_down = (s->mac_reg[STATUS] & E1000_STATUS_LU) == 0;
 
-    if (!(s->compat_flags & E1000_FLAG_AUTONEG)) {
-        return 0;
-    }
-
-    if (s->phy_reg[PHY_CTRL] & MII_CR_AUTO_NEG_EN &&
+    if (s->compat_flags & E1000_FLAG_AUTONEG &&
+        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)) {
         nc->link_down = false;