Skip to contentSkip to conversation

Consolidate lan9118 phy implementations

PATCH In review Bernhard Beschow 3 participants 15 messages Nov 2, 2024 – Dec 2, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 cover Consolidate lan9118 phy implementations

hw/net/imx_fec and hw/net/lan9118 implement the same Ethernet PHY with similar but not quite the same code. This series consolidates the implementations into one to fix code duplication. It then continues to make the code more readable by reusing some existing constants.

Having a dedicated module for the PHY allows it to be reused in even further device models.

Testing done:
* Run my usual sabrelite load and verify that network works.
* Build and run Buildroot's qemu_arm_vexpress_defconfig which includes lan9118 and check on serial console that IP address gets assigned.

v3:
* Don't add SOFT reset, use device-specific method instead (Peter)
* Reuse MII constants for MII_ANLPAR register which was previously missed
* Fix off-by-one error in MII_ANLPAR register which prevented MII constants to be reused

v2:
* QOM'ify + mention boards whose migratability will be broken (Peter)

Bernhard Beschow (5):
hw/net/lan9118: Extract lan9118_phy
hw/net/lan9118_phy: Reuse in imx_fec and consolidate implementations hw/net/lan9118_phy: Fix off-by-one error in MII_ANLPAR register hw/net/lan9118_phy: Reuse MII constants hw/net/lan9118_phy: Add missing 100 mbps full duplex advertisement

 include/hw/net/imx_fec.h     |   9 +-
 include/hw/net/lan9118_phy.h |  37 ++++++
 include/hw/net/mii.h         |   6 +
 hw/net/imx_fec.c             | 146 +++--------------------
 hw/net/lan9118.c             | 137 ++++-----------------
 hw/net/lan9118_phy.c         | 222 +++++++++++++++++++++++++++++++++++
 hw/net/Kconfig               |   5 +
 hw/net/meson.build           |   1 +
 hw/net/trace-events          |  10 +-
 9 files changed, 318 insertions(+), 255 deletions(-)
 create mode 100644 include/hw/net/lan9118_phy.h
 create mode 100644 hw/net/lan9118_phy.c
Signature · 1 line
2.47.0
#1 lore raw

PATCH v3 1/5 hw/net/lan9118: Extract lan9118_phy

A very similar implementation of the same device exists in imx_fec. Prepare for a common implementation by extracting a device model into its own files.

Some migration state has been moved into the new device model which breaks migration compatibility for the following machines:
* smdkc210
* realview-*
* vexpress-*
* kzm
* mps2-*

While breaking migration ABI, fix the size of the MII registers to be 16 bit, as defined by IEEE 802.3u.

---
 include/hw/net/lan9118_phy.h |  37 ++++++++
 hw/net/lan9118.c             | 137 +++++-----------------------
 hw/net/lan9118_phy.c         | 169 +++++++++++++++++++++++++++++++++++
 hw/net/Kconfig               |   4 +
 hw/net/meson.build           |   1 +
 5 files changed, 233 insertions(+), 115 deletions(-)
 create mode 100644 include/hw/net/lan9118_phy.h
 create mode 100644 hw/net/lan9118_phy.c
include/hw/net/lan9118_phy.h new file +37 -0
new file mode 100644
index 0000000000..af12fc33d5
@@ -0,0 +1,37 @@
+/*
+ * SMSC LAN9118 PHY emulation
+ *
+ * Copyright (c) 2009 CodeSourcery, LLC.
+ * Written by Paul Brook
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#ifndef HW_NET_LAN9118_PHY_H
+#define HW_NET_LAN9118_PHY_H
+
+#include "qom/object.h"
+#include "hw/sysbus.h"
+
+#define TYPE_LAN9118_PHY "lan9118-phy"
+OBJECT_DECLARE_SIMPLE_TYPE(Lan9118PhyState, LAN9118_PHY)
+
+typedef struct Lan9118PhyState {
+    SysBusDevice parent_obj;
+
+    uint16_t status;
+    uint16_t control;
+    uint16_t advertise;
+    uint16_t ints;
+    uint16_t int_mask;
+    qemu_irq irq;
+    bool link_down;
+} Lan9118PhyState;
+
+void lan9118_phy_update_link(Lan9118PhyState *s, bool link_down);
+void lan9118_phy_reset(Lan9118PhyState *s);
+uint16_t lan9118_phy_read(Lan9118PhyState *s, int reg);
+void lan9118_phy_write(Lan9118PhyState *s, int reg, uint16_t val);
+
+#endif
hw/net/lan9118.c +22 -115

219 lines not on this page · raw message

hw/net/lan9118_phy.c new file +169 -0

169 lines not on this page · raw message

hw/net/Kconfig +4 -0
index 7fcc0d7faa..6b2ff2f937 100644
@@ -62,8 +62,12 @@ config VMXNET3_PCI
 config SMC91C111
     bool
 
+config LAN9118_PHY
+    bool
+
 config LAN9118
     bool
+    select LAN9118_PHY
     select PTIMER
 
 config NE2000_ISA
hw/net/meson.build +1 -0
index 00a9e9dd51..3bb5d749a8 100644
@@ -19,6 +19,7 @@ system_ss.add(when: 'CONFIG_VMXNET3_PCI', if_true: files('vmxnet3.c'))
 
 system_ss.add(when: 'CONFIG_SMC91C111', if_true: files('smc91c111.c'))
 system_ss.add(when: 'CONFIG_LAN9118', if_true: files('lan9118.c'))
+system_ss.add(when: 'CONFIG_LAN9118_PHY', if_true: files('lan9118_phy.c'))
 system_ss.add(when: 'CONFIG_NE2000_ISA', if_true: files('ne2000-isa.c'))
 system_ss.add(when: 'CONFIG_OPENCORES_ETH', if_true: files('opencores_eth.c'))
 system_ss.add(when: 'CONFIG_XGMAC', if_true: files('xgmac.c'))
Signature · 1 line
2.47.0
Tested-by on v3 1/5
#2 lore raw

Re: [PATCH v3 1/5] hw/net/lan9118: Extract lan9118_phy

Quoted 11 lines · Bernhard Beschow

A very similar implementation of the same device exists in imx_fec. Prepare for a common implementation by extracting a device model into its own files.

Some migration state has been moved into the new device model which breaks migration compatibility for the following machines:
* smdkc210
* realview-*
* vexpress-*
* kzm
* mps2-*

Quoted 4 lines

While breaking migration ABI, fix the size of the MII registers to be 16 bit, as defined by IEEE 802.3u.

Reviewed-by on v3 1/5
#3 lore raw

Re: [PATCH v3 1/5] hw/net/lan9118: Extract lan9118_phy

In reply to Bernhard Beschow
Quoted 12 lines · Bernhard Beschow

A very similar implementation of the same device exists in imx_fec. Prepare for a common implementation by extracting a device model into its own files.

Some migration state has been moved into the new device model which breaks migration compatibility for the following machines:
* smdkc210
* realview-*
* vexpress-*
* kzm
* mps2-*

Quoted 4 lines

While breaking migration ABI, fix the size of the MII registers to be 16 bit, as defined by IEEE 802.3u.

thanks
-- PMM

#4 lore raw

PATCH v3 2/5 hw/net/lan9118_phy: Reuse in imx_fec and consolidate implementations

In reply to Bernhard Beschow

imx_fec models the same PHY as lan9118_phy. The code is almost the same with imx_fec having more logging and tracing. Merge these improvements into lan9118_phy and reuse in imx_fec to fix the code duplication.

Some migration state how resides in the new device model which breaks migration compatibility for the following machines:
* imx25-pdk
* sabrelite
* mcimx7d-sabre
* mcimx6ul-evk

---
 include/hw/net/imx_fec.h |   9 ++-
 hw/net/imx_fec.c         | 146 ++++-----------------------------------
 hw/net/lan9118_phy.c     |  82 ++++++++++++++++------
 hw/net/Kconfig           |   1 +
 hw/net/trace-events      |  10 +--
 5 files changed, 85 insertions(+), 163 deletions(-)
include/hw/net/imx_fec.h +4 -5
index 2d13290c78..83b21637ee 100644
@@ -31,6 +31,8 @@ OBJECT_DECLARE_SIMPLE_TYPE(IMXFECState, IMX_FEC)
 #define TYPE_IMX_ENET "imx.enet"
 
 #include "hw/sysbus.h"
+#include "hw/net/lan9118_phy.h"
+#include "hw/irq.h"
 #include "net/net.h"
 
 #define ENET_EIR               1
@@ -264,11 +266,8 @@ struct IMXFECState {
     uint32_t tx_descriptor[ENET_TX_RING_NUM];
     uint32_t tx_ring_num;
 
-    uint32_t phy_status;
-    uint32_t phy_control;
-    uint32_t phy_advertise;
-    uint32_t phy_int;
-    uint32_t phy_int_mask;
+    Lan9118PhyState mii;
+    IRQState mii_irq;
     uint32_t phy_num;
     bool phy_connected;
     struct IMXFECState *phy_consumer;
hw/net/imx_fec.c +15 -131

201 lines not on this page · raw message

hw/net/lan9118_phy.c +59 -23

153 lines not on this page · raw message

hw/net/Kconfig +1 -0
index 6b2ff2f937..7f80218d10 100644
@@ -93,6 +93,7 @@ config ALLWINNER_SUN8I_EMAC
 
 config IMX_FEC
     bool
+    select LAN9118_PHY
 
 config CADENCE
     bool
hw/net/trace-events +6 -4
index 91a3d0c054..e694d5823a 100644
@@ -10,6 +10,12 @@ allwinner_sun8i_emac_set_link(bool active) "Set link: active=%u"
 allwinner_sun8i_emac_read(uint64_t offset, uint64_t val) "MMIO read: offset=0x%" PRIx64 " value=0x%" PRIx64
 allwinner_sun8i_emac_write(uint64_t offset, uint64_t val) "MMIO write: offset=0x%" PRIx64 " value=0x%" PRIx64
 
+# lan9118_phy.c
+lan9118_phy_read(uint16_t val, int reg) "[0x%02x] -> 0x%04" PRIx16
+lan9118_phy_write(uint16_t val, int reg) "[0x%02x] <- 0x%04" PRIx16
+lan9118_phy_update_link(const char *s) "%s"
+lan9118_phy_reset(void) ""
+
 # lance.c
 lance_mem_readw(uint64_t addr, uint32_t ret) "addr=0x%"PRIx64"val=0x%04x"
 lance_mem_writew(uint64_t addr, uint32_t val) "addr=0x%"PRIx64"val=0x%04x"
@@ -428,12 +434,8 @@ i82596_set_multicast(uint16_t count) "Added %d multicast entries"
 i82596_channel_attention(void *s) "%p: Received CHANNEL ATTENTION"
 
 # imx_fec.c
-imx_phy_read(uint32_t val, int phy, int reg) "0x%04"PRIx32" <= phy[%d].reg[%d]"
 imx_phy_read_num(int phy, int configured) "read request from unconfigured phy %d (configured %d)"
-imx_phy_write(uint32_t val, int phy, int reg) "0x%04"PRIx32" => phy[%d].reg[%d]"
 imx_phy_write_num(int phy, int configured) "write request to unconfigured phy %d (configured %d)"
-imx_phy_update_link(const char *s) "%s"
-imx_phy_reset(void) ""
 imx_fec_read_bd(uint64_t addr, int flags, int len, int data) "tx_bd 0x%"PRIx64" flags 0x%04x len %d data 0x%08x"
 imx_enet_read_bd(uint64_t addr, int flags, int len, int data, int options, int status) "tx_bd 0x%"PRIx64" flags 0x%04x len %d data 0x%08x option 0x%04x status 0x%04x"
 imx_eth_tx_bd_busy(void) "tx_bd ran out of descriptors to transmit"
Signature · 1 line
2.47.0
Tested-by on v3 2/5
#5 lore raw

Re: [PATCH v3 2/5] hw/net/lan9118_phy: Reuse in imx_fec and consolidate implementations

Quoted 4 lines · Bernhard Beschow

imx_fec models the same PHY as lan9118_phy. The code is almost the same with imx_fec having more logging and tracing. Merge these improvements into lan9118_phy and reuse in imx_fec to fix the code duplication.

Quoted 8 lines

Some migration state how resides in the new device model which breaks migration compatibility for the following machines:
* imx25-pdk
* sabrelite
* mcimx7d-sabre
* mcimx6ul-evk

Reviewed-by on v3 2/5
#6 lore raw

Re: [PATCH v3 2/5] hw/net/lan9118_phy: Reuse in imx_fec and consolidate implementations

In reply to Bernhard Beschow
Quoted 5 lines · Bernhard Beschow

imx_fec models the same PHY as lan9118_phy. The code is almost the same with imx_fec having more logging and tracing. Merge these improvements into lan9118_phy and reuse in imx_fec to fix the code duplication.

Quoted 8 lines

Some migration state how resides in the new device model which breaks migration compatibility for the following machines:
* imx25-pdk
* sabrelite
* mcimx7d-sabre
* mcimx6ul-evk

thanks
-- PMM

#7 lore raw

PATCH v3 3/5 hw/net/lan9118_phy: Fix off-by-one error in MII_ANLPAR register

In reply to Bernhard Beschow

Turns 0x70 into 0xe0 (== 0x70 << 1) which adds the missing MII_ANLPAR_TX and fixes the MSB of selector field to be zero, as specified in the datasheet.

Fixes 2a424990170b "LAN9118 emulation"
---
 hw/net/lan9118_phy.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/net/lan9118_phy.c +1 -1
index d2dcd732ac..d8fc9ddd3a 100644
@@ -54,7 +54,7 @@ uint16_t lan9118_phy_read(Lan9118PhyState *s, int reg)
         val = s->advertise;
         break;
     case 5: /* Auto-neg Link Partner Ability */
-        val = 0x0f71;
+        val = 0x0fe1;
         break;
     case 6: /* Auto-neg Expansion */
         val = 1;
Signature · 1 line
2.47.0
Tested-by on v3 3/5
#8 lore raw

Re: [PATCH v3 3/5] hw/net/lan9118_phy: Fix off-by-one error in MII_ANLPAR register

Quoted 5 lines · Bernhard Beschow

Turns 0x70 into 0xe0 (== 0x70 << 1) which adds the missing MII_ANLPAR_TX and fixes the MSB of selector field to be zero, as specified in the datasheet.

Fixes 2a424990170b "LAN9118 emulation"
Reviewed-by on v3 3/5
#9 lore raw

Re: [PATCH v3 3/5] hw/net/lan9118_phy: Fix off-by-one error in MII_ANLPAR register

In reply to Bernhard Beschow
Quoted 15 lines · Bernhard Beschow

Turns 0x70 into 0xe0 (== 0x70 << 1) which adds the missing MII_ANLPAR_TX and fixes the MSB of selector field to be zero, as specified in the datasheet.

Fixes 2a424990170b "LAN9118 emulation"
---
 hw/net/lan9118_phy.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/net/lan9118_phy.c +0 -0
index d2dcd732ac..d8fc9ddd3a 100644

hw/net/lan9118_phy.c uint16_t lan9118_phy_read(Lan9118PhyState *s, int reg) · line 60

Quoted 8 lines
hw/net/lan9118_phy.c +1 -1
@@ -54,7 +54,7 @@ uint16_t lan9118_phy_read(Lan9118PhyState *s, int reg)
         val = s->advertise;
         break;
     case 5: /* Auto-neg Link Partner Ability */
-        val = 0x0f71;
+        val = 0x0fe1;
         break;
     case 6: /* Auto-neg Expansion */
         val = 1;

thanks
-- PMM

#10 lore raw

PATCH v3 4/5 hw/net/lan9118_phy: Reuse MII constants

In reply to Bernhard Beschow

Prefer named constants over magic values for better readability.

---
 include/hw/net/mii.h |  6 +++++
 hw/net/lan9118_phy.c | 63 ++++++++++++++++++++++++++++----------------
 2 files changed, 46 insertions(+), 23 deletions(-)
include/hw/net/mii.h +6 -0
index f7feddac9b..55bf7c92a1 100644
@@ -71,6 +71,7 @@
 #define MII_BMSR_JABBER     (1 << 1)  /* Jabber detected */
 #define MII_BMSR_EXTCAP     (1 << 0)  /* Ext-reg capability */
 
+#define MII_ANAR_RFAULT     (1 << 13) /* Say we can detect faults */
 #define MII_ANAR_PAUSE_ASYM (1 << 11) /* Try for asymmetric pause */
 #define MII_ANAR_PAUSE      (1 << 10) /* Try for pause */
 #define MII_ANAR_TXFD       (1 << 8)
@@ -78,6 +79,7 @@
 #define MII_ANAR_10FD       (1 << 6)
 #define MII_ANAR_10         (1 << 5)
 #define MII_ANAR_CSMACD     (1 << 0)
+#define MII_ANAR_SELECT     (0x001f)  /* Selector bits */
 
 #define MII_ANLPAR_ACK      (1 << 14)
 #define MII_ANLPAR_PAUSEASY (1 << 11) /* can pause asymmetrically */
@@ -112,6 +114,10 @@
 #define RTL8201CP_PHYID1    0x0000
 #define RTL8201CP_PHYID2    0x8201
 
+/* SMSC LAN9118 */
+#define SMSCLAN9118_PHYID1  0x0007
+#define SMSCLAN9118_PHYID2  0xc0d1
+
 /* RealTek 8211E */
 #define RTL8211E_PHYID1     0x001c
 #define RTL8211E_PHYID2     0xc915
hw/net/lan9118_phy.c +40 -23

111 lines not on this page · raw message

Signature · 1 line
2.47.0
#12 lore raw

PATCH v3 5/5 hw/net/lan9118_phy: Add missing 100 mbps full duplex advertisement

In reply to Bernhard Beschow

The real device advertises this mode and the device model already advertises 100 mbps half duplex and 10 mbps full+half duplex. So advertise this mode to make the model more realistic.

---
 hw/net/lan9118_phy.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/net/lan9118_phy.c +2 -2
index 874dae4155..5c53a4a1e3 100644
@@ -110,8 +110,8 @@ void lan9118_phy_write(Lan9118PhyState *s, int reg, uint16_t val)
         break;
     case MII_ANAR:
         s->advertise = (val & (MII_ANAR_RFAULT | MII_ANAR_PAUSE_ASYM |
-                               MII_ANAR_PAUSE | MII_ANAR_10FD | MII_ANAR_10 |
-                               MII_ANAR_SELECT))
+                               MII_ANAR_PAUSE | MII_ANAR_TXFD | MII_ANAR_10FD |
+                               MII_ANAR_10 | MII_ANAR_SELECT))
                      | MII_ANAR_TX;
         break;
     case 30: /* Interrupt mask */
Signature · 1 line
2.47.0
Tested-by on v3 5/5
#13 lore raw

Re: [PATCH v3 5/5] hw/net/lan9118_phy: Add missing 100 mbps full duplex advertisement

Quoted 6 lines · Bernhard Beschow

The real device advertises this mode and the device model already advertises 100 mbps half duplex and 10 mbps full+half duplex. So advertise this mode to make the model more realistic.

#14 lore raw
In reply to Bernhard Beschow
Quoted 14 lines · Bernhard Beschow

hw/net/imx_fec and hw/net/lan9118 implement the same Ethernet PHY with similar but not quite the same code. This series consolidates the implementations into one to fix code duplication. It then continues to make the code more readable by reusing some existing constants.

Having a dedicated module for the PHY allows it to be reused in even further device models.

Testing done:
* Run my usual sabrelite load and verify that network works.
* Build and run Buildroot's qemu_arm_vexpress_defconfig which includes lan9118 and check on serial console that IP address gets assigned.

Quoted 8 lines

v3:
* Don't add SOFT reset, use device-specific method instead (Peter)
* Reuse MII constants for MII_ANLPAR register which was previously missed
* Fix off-by-one error in MII_ANLPAR register which prevented MII constants to be reused

v2:
* QOM'ify + mention boards whose migratability will be broken (Peter)

I've added this series to my list of patches to add to target-arm.next once 9.2 is released and we reopen the tree for 10.0.

thanks
-- PMM