net: phy: Add sysfs attribute for PHY c45 identifiers.
Subsystems (3)
PATCH v4 1/1 net: phy: Add sysfs attribute for PHY c45 identifiers.
If a phydevice use c45, its phy_id property is always 0, so this adds a c45_ids sysfs attribute group contains mmd id attributes from mmd0 to mmd31 to MDIO devices. Note that only mmd with valid value will exist. This attribute group can be useful when debugging problems related to phy drivers.
Likes this:
/sys/bus/mdio_bus/devices/mdio-bus:05/c45_ids/mmd1 /sys/bus/mdio_bus/devices/mdio-bus:05/c45_ids/mmd2
...
/sys/bus/mdio_bus/devices/mdio-bus:05/c45_ids/mmd31
---
V3 -> V4: Only mmd with valid value will exist.
V2 -> V3: Use the most efficient implementation.
V1 -> V2: putting all 32 values in a subdirectory, one file per MMD
.../ABI/testing/sysfs-class-net-phydev | 10 ++ drivers/net/phy/phy_device.c | 123 +++++++++++++++++- 2 files changed, 132 insertions(+), 1 deletion(-)
Documentation/ABI/testing/sysfs-class-net-phydev +10 -0
@@ -63,3 +63,13 @@ Description: only used internally by the kernel and their placement are not meant to be stable across kernel versions. This is intended for facilitating the debugging of PHY drivers. + +What: /sys/class/mdio_bus/<bus>/<device>/c45_ids/mmd<n> +Date: November 2023 +KernelVersion: 6.4 +Contact: [email protected] +Description: + This attribute group c45_ids contains mmd id attributes from mmd0 to mmd31 + as reported by the device during bus enumeration, encoded in hexadecimal. + Note that only mmd with valid value will exist. This ID is used to match + the device with the appropriate driver.
drivers/net/phy/phy_device.c +122 -1
129 lines not on this page · raw message
Signature · 1 line
2.34.1
Russell King (Oracle)
reviewerdrivers/net/phy/phy_device.c static struct attribute *phy_dev_attrs[] = { · line 707
Quoted 4 lines · Jianhui Zhao
drivers/net/phy/phy_device.c +4
-0+static umode_t phy_dev_c45_visible (struct kobject *kobj,struct attribute *attr,int foo) +{ +struct phy_c45_devid_attribute *devattr = + (struct phy_c45_devid_attribute *)container_of(attr, struct device_attribute, attr);
1. (struct phy_c45_devid_attribute *) cast is not required.
2. we now have two places that we convert the attribute to a phy_c45_devid_attribute, it's time for a macro to do that.
drivers/net/phy/phy_device.c static struct attribute *phy_dev_attrs[] = { · line 710
Quoted 3 lines
drivers/net/phy/phy_device.c +3
-0+struct phy_device *phydev =to_phy_device (kobj_to_dev (kobj)); + +if (!phydev->is_c45 || phydev->c45_ids.device_ids[devattr->index] ==0xffffffff )
if (!phydev->is_c45 || phydev->c45_ids.device_ids[devattr->index] == 0xffffffff)
And lastly... please don't be so quick to post a new version.
Particularly the bit about "Do not post a new version of the code if the discussion about the previous version is still ongoing, unless directly instructed by a reviewer." is relevent.
You have not given enough time for Andrew to respond to my suggestion which I invited him to (by "Andrew, any opinions?"), instead rushing out v4 that implements my suggestion without first waiting to see if Andrew agrees with it.
This seems to be becoming common place, so I think it's about time I created a vim macro to insert a boilerplate explaining this process in every review. :(
Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/ FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!
PATCH v4 1/1 net: phy: Add sysfs attribute for PHY c45 identifiers.
In reply to Jianhui ZhaoReview
Quoted 5 lines
(quoted diff) +4
-0+ struct phy_c45_devid_attribute *devattr = + container_of(attr, struct phy_c45_devid_attribute, attr); + struct phy_c45_devid_attribute *devattr = + (struct phy_c45_devid_attribute *)container_of(attr, struct device_attribute, attr);
The two conversions is not same.
One is convert "struct device_attribute" to "struct phy_c45_devid_attribute", and another one is convert "struct attribute" to "struct phy_c45_devid_attribute". The second one must cast "struct device_attribute" returned from container_of to "struct phy_c45_devid_attribute".
Russell King (Oracle)
reviewerQuoted 7 lines · Jianhui Zhao
Review
Quoted 5 lines
(quoted diff) +4
-0+ struct phy_c45_devid_attribute *devattr = + container_of(attr, struct phy_c45_devid_attribute, attr); + struct phy_c45_devid_attribute *devattr = + (struct phy_c45_devid_attribute *)container_of(attr, struct device_attribute, attr);The two conversions is not same.
You're right...
Quoted 4 lines
One is convert "struct
device_attribute" to "structphy_c45_devid_attribute", and another one is convert "struct attribute" to "structphy_c45_devid_attribute". The second one must cast "structdevice_attribute" returned fromcontainer_ofto "structphy_c45_devid_attribute".
... but this isn't entirely correct.
Doing it properly:
struct phy_c45_devid_attribute *devattr = container_of(attr, struct phy_c45_devid_attribute, attr.attr);
Number one rule with C programming: if you have to cast, you're probably doing something wrong, so always look to see if there's an alternative solution that doesn't involve casts.
Casts are one of the reasons why programming errors happen - it stops the compiler being able to perform proper type checking. Always avoid them as much as possible.
Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/ FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!
PATCH v4 1/1 net: phy: Add sysfs attribute for PHY c45 identifiers.
In reply to Jianhui ZhaoQuoted 4 lines
Doing it properly:
struct
phy_c45_devid_attribute*devattr =
container_of(attr, struct phy_c45_devid_attribute, attr.attr);
Thanks for the tip!