[PATCH RESEND net-next v2] net: phy: Add c45_phy_ids sysfs directory entry

Yajun Deng posted 1 patch 4 months ago
There is a newer version of this series
.../ABI/testing/sysfs-class-net-phydev        | 10 +++
drivers/net/phy/phy_device.c                  | 62 ++++++++++++++++++-
2 files changed, 70 insertions(+), 2 deletions(-)
[PATCH RESEND net-next v2] net: phy: Add c45_phy_ids sysfs directory entry
Posted by Yajun Deng 4 months ago
The phy_id field only shows the PHY ID of the C22 device, and the C45
device did not store its PHY ID in this field.

Add a new phy_mmd_group, and export the mmd<n>_device_id for the C45
device. These files are invisible to the C22 device.

Signed-off-by: Yajun Deng <yajun.deng@linux.dev>
---
v2: Only one value for per file and invisible to the C22 device.
v1: https://lore.kernel.org/all/20250523132606.2814-1-yajun.deng@linux.dev/
---
 .../ABI/testing/sysfs-class-net-phydev        | 10 +++
 drivers/net/phy/phy_device.c                  | 62 ++++++++++++++++++-
 2 files changed, 70 insertions(+), 2 deletions(-)

diff --git a/Documentation/ABI/testing/sysfs-class-net-phydev b/Documentation/ABI/testing/sysfs-class-net-phydev
index ac722dd5e694..31615c59bff9 100644
--- a/Documentation/ABI/testing/sysfs-class-net-phydev
+++ b/Documentation/ABI/testing/sysfs-class-net-phydev
@@ -26,6 +26,16 @@ Description:
 		This ID is used to match the device with the appropriate
 		driver.
 
+What:		/sys/class/mdio_bus/<bus>/<device>/c45_phy_ids/mmd<n>_device_id
+Date:		June 2025
+KernelVersion:	6.17
+Contact:	netdev@vger.kernel.org
+Description:
+		This attribute contains the 32-bit PHY Identifier as reported
+		by the device during bus enumeration, encoded in hexadecimal.
+		These C45 IDs are used to match the device with the appropriate
+		driver. These files are invisible to the C22 device.
+
 What:		/sys/class/mdio_bus/<bus>/<device>/phy_interface
 Date:		February 2014
 KernelVersion:	3.15
diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
index 73f9cb2e2844..5e5bac8d8651 100644
--- a/drivers/net/phy/phy_device.c
+++ b/drivers/net/phy/phy_device.c
@@ -645,11 +645,69 @@ static struct attribute *phy_dev_attrs[] = {
 	&dev_attr_phy_dev_flags.attr,
 	NULL,
 };
-ATTRIBUTE_GROUPS(phy_dev);
+
+static const struct attribute_group phy_dev_group = {
+	.attrs = phy_dev_attrs,
+};
+
+#define MMD_INDICES \
+	_(1) _(2) _(3) _(4) _(5) _(6) _(7) _(8) \
+	_(9) _(10) _(11) _(12) _(13) _(14) _(15) _(16) \
+	_(17) _(18) _(19) _(20) _(21) _(22) _(23) _(24) \
+	_(25) _(26) _(27) _(28) _(29) _(30) _(31)
+
+#define MMD_DEVICE_ID_ATTR(n) \
+static ssize_t mmd##n##_device_id_show(struct device *dev, \
+				struct device_attribute *attr, char *buf) \
+{ \
+	struct phy_device *phydev = to_phy_device(dev); \
+	return sysfs_emit(buf, "0x%.8lx\n", \
+			 (unsigned long)phydev->c45_ids.device_ids[n]); \
+} \
+static DEVICE_ATTR_RO(mmd##n##_device_id)
+
+#define _(x) MMD_DEVICE_ID_ATTR(x);
+MMD_INDICES
+#undef _
+
+static struct attribute *phy_mmd_attrs[] = {
+	#define _(x) &dev_attr_mmd##x##_device_id.attr,
+	MMD_INDICES
+	#undef _
+	NULL
+};
+
+static umode_t phy_mmd_is_visible(struct kobject *kobj,
+				  struct attribute *attr, int index)
+{
+	struct device *dev = kobj_to_dev(kobj);
+	struct phy_device *phydev = to_phy_device(dev);
+	const int i = index + 1;
+
+	if (!phydev->is_c45)
+		return 0;
+	if (i >= ARRAY_SIZE(phydev->c45_ids.device_ids) ||
+	    phydev->c45_ids.device_ids[i] == 0xffffffff)
+		return 0;
+
+	return attr->mode;
+}
+
+static const struct attribute_group phy_mmd_group = {
+	.name = "c45_phy_ids",
+	.attrs = phy_mmd_attrs,
+	.is_visible = phy_mmd_is_visible,
+};
+
+static const struct attribute_group *phy_device_groups[] = {
+	&phy_dev_group,
+	&phy_mmd_group,
+	NULL,
+};
 
 static const struct device_type mdio_bus_phy_type = {
 	.name = "PHY",
-	.groups = phy_dev_groups,
+	.groups = phy_device_groups,
 	.release = phy_device_release,
 	.pm = pm_ptr(&mdio_bus_phy_pm_ops),
 };
-- 
2.25.1
Re: [PATCH RESEND net-next v2] net: phy: Add c45_phy_ids sysfs directory entry
Posted by Andrew Lunn 4 months ago
> +#define MMD_INDICES \
> +	_(1) _(2) _(3) _(4) _(5) _(6) _(7) _(8) \
> +	_(9) _(10) _(11) _(12) _(13) _(14) _(15) _(16) \
> +	_(17) _(18) _(19) _(20) _(21) _(22) _(23) _(24) \
> +	_(25) _(26) _(27) _(28) _(29) _(30) _(31)

Is 0 not valid?

> +#define MMD_DEVICE_ID_ATTR(n) \
> +static ssize_t mmd##n##_device_id_show(struct device *dev, \
> +				struct device_attribute *attr, char *buf) \
> +{ \
> +	struct phy_device *phydev = to_phy_device(dev); \
> +	return sysfs_emit(buf, "0x%.8lx\n", \
> +			 (unsigned long)phydev->c45_ids.device_ids[n]); \
> +} \
> +static DEVICE_ATTR_RO(mmd##n##_device_id)

This macro magic i can follow, you see this quite a bit in the kernel.

> +
> +#define _(x) MMD_DEVICE_ID_ATTR(x);
> +MMD_INDICES
> +#undef _
> +
> +static struct attribute *phy_mmd_attrs[] = {
> +	#define _(x) &dev_attr_mmd##x##_device_id.attr,
> +	MMD_INDICES
> +	#undef _
> +	NULL
> +};

If i squint at this enough, i can work it out, but generally a much
more readable KISS approach is taken, of just invoking a macro 32
times. See mdio_bus.c as an example.

    Andrew

---
pw-bot: cr
Re: [PATCH RESEND net-next v2] net: phy: Add c45_phy_ids sysfs directory entry
Posted by Yajun Deng 3 months, 4 weeks ago
June 12, 2025 at 11:31 PM, "Andrew Lunn" <andrew@lunn.ch> wrote:



> 
> > 
> > +#define MMD_INDICES \
> > 
> >  + _(1) _(2) _(3) _(4) _(5) _(6) _(7) _(8) \
> > 
> >  + _(9) _(10) _(11) _(12) _(13) _(14) _(15) _(16) \
> > 
> >  + _(17) _(18) _(19) _(20) _(21) _(22) _(23) _(24) \
> > 
> >  + _(25) _(26) _(27) _(28) _(29) _(30) _(31)
> > 
> 
> Is 0 not valid?
> 

Yes, the MMD starts at 1. See "include/uapi/linux/mdio.h".

> > 
> > +#define MMD_DEVICE_ID_ATTR(n) \
> > 
> >  +static ssize_t mmd##n##_device_id_show(struct device *dev, \
> > 
> >  + struct device_attribute *attr, char *buf) \
> > 
> >  +{ \
> > 
> >  + struct phy_device *phydev = to_phy_device(dev); \
> > 
> >  + return sysfs_emit(buf, "0x%.8lx\n", \
> > 
> >  + (unsigned long)phydev->c45_ids.device_ids[n]); \
> > 
> >  +} \
> > 
> >  +static DEVICE_ATTR_RO(mmd##n##_device_id)
> > 
> 
> This macro magic i can follow, you see this quite a bit in the kernel.
> 

Okay.

> > 
> > +
> > 
> >  +#define _(x) MMD_DEVICE_ID_ATTR(x);
> > 
> >  +MMD_INDICES
> > 
> >  +#undef _
> > 
> >  +
> > 
> >  +static struct attribute *phy_mmd_attrs[] = {
> > 
> >  + #define _(x) &dev_attr_mmd##x##_device_id.attr,
> > 
> >  + MMD_INDICES
> > 
> >  + #undef _
> > 
> >  + NULL
> > 
> >  +};
> > 
> 
> If i squint at this enough, i can work it out, but generally a much
> 
> more readable KISS approach is taken, of just invoking a macro 32
> 
> times. See mdio_bus.c as an example.
> 

Okay.

>  Andrew
> 
> ---
> 
> pw-bot: cr
>