Skip to contentSkip to conversation

Consolidate lan9118 phy implementations

PATCH Superseded Bernhard Beschow 2 participants 13 messages Oct 5, 2024 – Oct 16, 2024

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

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.

Bernhard Beschow (4):
hw/net/lan9118: Extract lan9118_phy
hw/net/lan9118_phy: Reuse in imx_fec and consolidate implementations hw/net/lan9118_phy: Reuse MII constants hw/net/lan9118_phy: Add missing 100 mbps full duplex advertisement

 include/hw/net/imx_fec.h     |   7 +-
 include/hw/net/lan9118_phy.h |  31 +++++++
 include/hw/net/mii.h         |   6 ++
 hw/net/imx_fec.c             | 141 +++--------------------------
 hw/net/lan9118.c             | 133 +++++----------------------
 hw/net/lan9118_phy.c         | 168 +++++++++++++++++++++++++++++++++++
 hw/net/Kconfig               |   5 ++
 hw/net/meson.build           |   1 +
 hw/net/trace-events          |  10 ++-
 9 files changed, 254 insertions(+), 248 deletions(-)
 create mode 100644 include/hw/net/lan9118_phy.h
 create mode 100644 hw/net/lan9118_phy.c
Signature · 1 line
2.46.2
#1 lore raw

PATCH 1/4 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 the code into its own files.

---
 include/hw/net/lan9118_phy.h |  31 ++++++++
 hw/net/lan9118.c             | 133 ++++++-----------------------------
 hw/net/lan9118_phy.c         | 117 ++++++++++++++++++++++++++++++
 hw/net/Kconfig               |   4 ++
 hw/net/meson.build           |   1 +
 5 files changed, 174 insertions(+), 112 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 +31 -0
new file mode 100644
index 0000000000..50e3559b12
@@ -0,0 +1,31 @@
+/*
+ * 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 "hw/irq.h"
+
+typedef struct Lan9118PhyState {
+    uint32_t status;
+    uint32_t control;
+    uint32_t advertise;
+    uint32_t ints;
+    uint32_t int_mask;
+    IRQState irq;
+    bool link_down;
+} Lan9118PhyState;
+
+void lan9118_phy_update_link(Lan9118PhyState *s, bool link_down);
+void lan9118_phy_reset(Lan9118PhyState *s);
+uint32_t lan9118_phy_read(Lan9118PhyState *s, int reg);
+void lan9118_phy_write(Lan9118PhyState *s, int reg, uint32_t val);
+
+#endif
hw/net/lan9118.c +21 -112

209 lines not on this page · raw message

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

117 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.46.2
#2 lore raw

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

Quoted 35 lines · Bernhard Beschow

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

---
 include/hw/net/lan9118_phy.h |  31 ++++++++
 hw/net/lan9118.c             | 133 ++++++-----------------------------
 hw/net/lan9118_phy.c         | 117 ++++++++++++++++++++++++++++++
 hw/net/Kconfig               |   4 ++
 hw/net/meson.build           |   1 +
 5 files changed, 174 insertions(+), 112 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 +14 -0
new file mode 100644
index 0000000000..50e3559b12
@@ -0,0 +1,31 @@
+/*
+ * 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 "hw/irq.h"

include/hw/net/lan9118_phy.h line 24

Quoted 10 lines
include/hw/net/lan9118_phy.h new file +10 -0
+
+typedef struct Lan9118PhyState {
+    uint32_t status;
+    uint32_t control;
+    uint32_t advertise;
+    uint32_t ints;
+    uint32_t int_mask;
+    IRQState irq;
+    bool link_down;
+} Lan9118PhyState;

This takes state that was in a QOM object, and moves it into something that's kind of a device but not a QOM object. I think we should avoid that, because at some point somebody's going to have to QOMify this.

Making this a QOM device is a bit awkward for migration compatibility, unfortunately.

thanks
-- PMM

#3 lore raw

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

Am 14. Oktober 2024 12:47:52 UTC schrieb Peter Maydell <[email protected]>:

Quoted 52 lines · Bernhard Beschow
Quoted 35 lines

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

---
 include/hw/net/lan9118_phy.h |  31 ++++++++
 hw/net/lan9118.c             | 133 ++++++-----------------------------
 hw/net/lan9118_phy.c         | 117 ++++++++++++++++++++++++++++++
 hw/net/Kconfig               |   4 ++
 hw/net/meson.build           |   1 +
 5 files changed, 174 insertions(+), 112 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 +14 -0
new file mode 100644
index 0000000000..50e3559b12
@@ -0,0 +1,31 @@
+/*
+ * 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 "hw/irq.h"

include/hw/net/lan9118_phy.h line 24

Quoted 10 lines
include/hw/net/lan9118_phy.h new file +10 -0
+
+typedef struct Lan9118PhyState {
+    uint32_t status;
+    uint32_t control;
+    uint32_t advertise;
+    uint32_t ints;
+    uint32_t int_mask;
+    IRQState irq;
+    bool link_down;
+} Lan9118PhyState;

This takes state that was in a QOM object, and moves it into something that's kind of a device but not a QOM object. I think we should avoid that, because at some point somebody's going to have to QOMify this.

Quoted 2 lines

Making this a QOM device is a bit awkward for migration compatibility, unfortunately.

Do we care about migration compatibility here? Or is it sufficient to check the version? In the latter case I could QOMify it.

Best regards,
Bernhard

Quoted 3 lines

thanks
-- PMM

#4 lore raw

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

Quoted 19 lines · Bernhard Beschow

Am 14. Oktober 2024 12:47:52 UTC schrieb Peter Maydell <[email protected]>:

Quoted 15 lines

include/hw/net/lan9118_phy.h line 24

Quoted 9 lines
include/hw/net/lan9118_phy.h new file +9 -0
+typedef struct Lan9118PhyState {
+    uint32_t status;
+    uint32_t control;
+    uint32_t advertise;
+    uint32_t ints;
+    uint32_t int_mask;
+    IRQState irq;
+    bool link_down;
+} Lan9118PhyState;

This takes state that was in a QOM object, and moves it into something that's kind of a device but not a QOM object. I think we should avoid that, because at some point somebody's going to have to QOMify this.

Quoted 6 lines
Quoted 2 lines

Making this a QOM device is a bit awkward for migration compatibility, unfortunately.

Do we care about migration compatibility here? Or is it sufficient to check the version? In the latter case I could QOMify it.

Doing a quick grep it looks like the lan9118 is only used in a set of Arm boards and none of them are ones where we care about migration across versions. So I think we're ok to break compat with a version-bump. We should mention the affected boards in the commit message.

-- PMM

#5 lore raw

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

Am 15. Oktober 2024 09:27:40 UTC schrieb Peter Maydell <[email protected]>:

Quoted 23 lines · Bernhard Beschow
Quoted 22 lines

Am 14. Oktober 2024 12:47:52 UTC schrieb Peter Maydell <[email protected]>:

Quoted 17 lines

include/hw/net/lan9118_phy.h line 24

Quoted 9 lines
include/hw/net/lan9118_phy.h new file +9 -0
+typedef struct Lan9118PhyState {
+    uint32_t status;
+    uint32_t control;
+    uint32_t advertise;
+    uint32_t ints;
+    uint32_t int_mask;
+    IRQState irq;
+    bool link_down;
+} Lan9118PhyState;

This takes state that was in a QOM object, and moves it into something that's kind of a device but not a QOM object. I think we should avoid that, because at some point somebody's going to have to QOMify this.

Making this a QOM device is a bit awkward for migration compatibility, unfortunately.

Quoted 8 lines
Quoted 3 lines

Do we care about migration compatibility here? Or is it sufficient to check the version? In the latter case I could QOMify it.

Doing a quick grep it looks like the lan9118 is only used in a set of Arm boards and none of them are ones where we care about migration across versions.

Four i.mx boards using imx_fec will also be affected. None is versioned afaics.

Quoted 3 lines

So I think we're
ok to break compat with a version-bump. We should mention the affected boards in the commit message.

Will do.

Thanks,
Bernhard

Quoted 2 lines

-- PMM

#6 lore raw

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

In reply to Peter Maydell

Am 15. Oktober 2024 09:27:40 UTC schrieb Peter Maydell <[email protected]>:

Quoted 28 lines · Bernhard Beschow
Quoted 19 lines

Am 14. Oktober 2024 12:47:52 UTC schrieb Peter Maydell <[email protected]>:

Quoted 15 lines

include/hw/net/lan9118_phy.h line 24

Quoted 9 lines
include/hw/net/lan9118_phy.h new file +9 -0
+typedef struct Lan9118PhyState {
+    uint32_t status;
+    uint32_t control;
+    uint32_t advertise;
+    uint32_t ints;
+    uint32_t int_mask;
+    IRQState irq;
+    bool link_down;
+} Lan9118PhyState;

This takes state that was in a QOM object, and moves it into something that's kind of a device but not a QOM object. I think we should avoid that, because at some point somebody's going to have to QOMify this.

Quoted 6 lines
Quoted 2 lines

Making this a QOM device is a bit awkward for migration compatibility, unfortunately.

Do we care about migration compatibility here? Or is it sufficient to check the version? In the latter case I could QOMify it.

Quoted 5 lines

Doing a quick grep it looks like the lan9118 is only used in a set of Arm boards and none of them are ones where we care about migration across versions. So I think we're ok to break compat with a version-bump. We should mention the affected boards in the commit message.

V2 sent: <https://lore.kernel.org/qemu-devel/[email protected]/>

Best regards,
Bernhard

Quoted 2 lines

-- PMM

#7 lore raw

PATCH 2/4 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.

---
 include/hw/net/imx_fec.h |   7 +-
 hw/net/imx_fec.c         | 141 ++++-----------------------------------
 hw/net/lan9118_phy.c     |  80 ++++++++++++++++------
 hw/net/Kconfig           |   1 +
 hw/net/trace-events      |  10 +--
 5 files changed, 81 insertions(+), 158 deletions(-)
include/hw/net/imx_fec.h +2 -5
index 2d13290c78..5f1d30f56b 100644
@@ -31,6 +31,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(IMXFECState, IMX_FEC)
 #define TYPE_IMX_ENET "imx.enet"
 
 #include "hw/sysbus.h"
+#include "hw/net/lan9118_phy.h"
 #include "net/net.h"
 
 #define ENET_EIR               1
@@ -264,11 +265,7 @@ 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;
     uint32_t phy_num;
     bool phy_connected;
     struct IMXFECState *phy_consumer;
hw/net/imx_fec.c +14 -127

192 lines not on this page · raw message

hw/net/lan9118_phy.c +58 -22

143 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 4c6687923e..b8e0076833 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(uint32_t val, int reg) "[0x%02x] -> 0x%04" PRIx32
+lan9118_phy_write(uint32_t val, int reg) "[0x%02x] <- 0x%04" PRIx32
+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"
@@ -426,12 +432,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.46.2
#8 lore raw

PATCH 3/4 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 | 59 +++++++++++++++++++++++++++-----------------
 2 files changed, 43 insertions(+), 22 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 +37 -22

108 lines not on this page · raw message

Signature · 1 line
2.46.2
#10 lore raw

PATCH 4/4 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 1c2f30c81a..4815a4da35 100644
@@ -145,8 +145,8 @@ void lan9118_phy_write(Lan9118PhyState *s, int reg, uint32_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.46.2
Reviewed-by on v1 4/4
#11 lore raw

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

Quoted 15 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.

---
 hw/net/lan9118_phy.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/net/lan9118_phy.c +0 -0
index 1c2f30c81a..4815a4da35 100644

hw/net/lan9118_phy.c void lan9118_phy_write(Lan9118PhyState *s, int reg, uint32_t val) · line 151

Quoted 9 lines
hw/net/lan9118_phy.c +2 -2
@@ -145,8 +145,8 @@ void lan9118_phy_write(Lan9118PhyState *s, int reg, uint32_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;

thanks
-- PMM

#12 lore raw
In reply to Bernhard Beschow

Am 5. Oktober 2024 20:57:44 UTC schrieb Bernhard Beschow <[email protected]>:

Quoted 6 lines

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

Quoted 8 lines

reusing some existing constants.

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

device models.

Ping

Quoted 45 lines

Bernhard Beschow (4):

hw/net/lan9118: Extract lan9118_phy

hw/net/lan9118_phy: Reuse in imx_fec and consolidate implementations

hw/net/lan9118_phy: Reuse MII constants

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

include/hw/net/imx_fec.h | 7 +-

include/hw/net/lan9118_phy.h | 31 +++++++

include/hw/net/mii.h | 6 ++

hw/net/imx_fec.c | 141 +++--------------------------

hw/net/lan9118.c | 133 +++++----------------------

hw/net/lan9118_phy.c | 168 +++++++++++++++++++++++++++++++++++

hw/net/Kconfig | 5 ++

hw/net/meson.build | 1 +

hw/net/trace-events | 10 ++-

9 files changed, 254 insertions(+), 248 deletions(-)

create mode 100644 include/hw/net/lan9118_phy.h

create mode 100644 hw/net/lan9118_phy.c

-- >
2.46.2