net: ibm: emac: fix the fixed-link PHY setup
Subsystems (2)
v2 cover net: ibm: emac: fix the fixed-link PHY setup
From: Linkui Xiao <[email protected]>
EMAC never attaches the fixed-link PHY, so it is missing from the netdev's PHY link topology and ETHTOOL_MSG_PHY_GET crashes. Attaching it also means it has to be detached again, and it exposes a second bug: since commit d99b408ed8e5 nothing fills in the phy_device status, so the driver has been programming the MAC as 10 Mbit/s half duplex for every fixed link.
Both are fixed here, in the order that keeps the series bisectable.
Cross compiled for powerpc out of ppc44x_defconfig with W=1. No hardware, so no runtime test.
Note per Documentation/process/generated-content.rst: the changes were developed with an AI coding assistant; the analysis, build testing and review are mine.
Linkui Xiao (2):
net: ibm: emac: attach the fixed-link PHY with of_phy_connect()
net: ibm: emac: read the fixed-link status before adjusting the link
drivers/net/ethernet/ibm/emac/core.c | 31 +++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-)
---
v2:
- rewrote the commit message by hand and kept it to the reasons for the change (Andrew Lunn)
- explain why emac_adjust_link() is still called by hand (Andrew Lunn)
- disconnect the PHY in emac_remove() and on probe failure, and deregister the fixed PHY when of_phy_connect() fails (Andrew Lunn, Sashiko AI review)
- new 2/2: read the link status so emac_adjust_link() no longer copies
SPEED_UNKNOWN into dev->phy (Sashiko AI review)
- rebased: 4dd10fbbeb9c ("treewide: Convert devm_kmalloc() family to the devm_kmalloc_obj() family") changed emac/core.c since v1
v1: https://lore.kernel.org/netdev/[email protected]/
Signature · 1 line
2.25.1
PATCH v2 1/2 net: ibm: emac: attach the fixed-link PHY with of_phy_connect()
From: Linkui Xiao <[email protected]>
emac_init_phy() sets ndev->phydev by hand for a fixed link, so phy_attach_direct() never runs: the PHY stays out of the netdev's PHY link topology, and ETHTOOL_MSG_PHY_GET then dereferences a NULL link_topo in phy_prepare_data().
Use of_phy_connect(), as gianfar, fec, davinci_emac and nixge do, and as this driver already does for the phy-handle case. It attaches the PHY and drops the of_phy_find_device() reference, so put_device() goes away. It only installs emac_adjust_link() as phydev->adjust_link though, and the state machine is not started for a fixed link, so the explicit call that copies the link parameters into dev->phy stays.
Attaching also means detaching. Without phy_disconnect(), unbind leaves phydev->attached_dev pointing at a freed netdev and the next probe fails with -EBUSY from phy_attach_direct(). This fixes the phy-handle path as well, which calls phy_start() but never phy_stop().
--- drivers/net/ethernet/ibm/emac/core.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-)
drivers/net/ethernet/ibm/emac/core.c +19 -5
@@ -2673,11 +2673,16 @@ static int emac_init_phy(struct emac_instance *dev)return res; res =of_phy_register_fixed_link (np); - ndev->phydev =of_phy_find_device (np); -if (res || !ndev->phydev) -return res ? res : -EINVAL ; -emac_adjust_link (dev->ndev); -put_device (&ndev->phydev->mdio.dev); +if (res) +return res; + +if (!of_phy_connect (ndev, np, &emac_adjust_link,0 , + dev->phy_mode)) { +of_phy_deregister_fixed_link (np); +return -EINVAL ; + } + +emac_adjust_link (ndev); }return 0 ; }
@@ -3190,6 +3195,10 @@ static int emac_probe(struct platform_device *ofdev)/* I have a bad feeling about this ... */ err_detach_tah: +if (ndev->phydev) +phy_disconnect (ndev->phydev); +if (of_phy_is_fixed_link (np)) +of_phy_deregister_fixed_link (np);if (emac_has_feature (dev,EMAC_FTR_HAS_TAH ))tah_detach (dev->tah_dev, dev->tah_port); err_detach_rgmii:
@@ -3223,6 +3232,11 @@ static void emac_remove(struct platform_device *ofdev)cancel_work_sync (&dev->reset_work); +if (dev->ndev->phydev) +phy_disconnect (dev->ndev->phydev); +if (of_phy_is_fixed_link (ofdev->dev.of_node)) +of_phy_deregister_fixed_link (ofdev->dev.of_node); +if (emac_has_feature (dev,EMAC_FTR_HAS_TAH ))tah_detach (dev->tah_dev, dev->tah_port);if (emac_has_feature (dev,EMAC_FTR_HAS_RGMII ))
Signature · 1 line
2.25.1
[email protected]
AI reviewRe: [PATCH net v2 1/2] net: ibm: emac: attach the fixed-link PHY with of_phy_connect()
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Critical: 0 · High: 1 · Medium: 0 · Low: 0
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [High] When the driver is unbound, emac_remove() calls phy_disconnect() only after unregister_netdev() has finished.
--
net: ibm: emac: attach the fixed-link PHY with of_phy_connect()
For the fixed-link case, this patch stops setting ndev->phydev by hand in emac_init_phy() and attaches the PHY with of_phy_connect() instead. It also adds phy_disconnect() and of_phy_deregister_fixed_link() calls to the emac_probe() error path and to emac_remove(), so the PHY is detached on unbind.
Quoted 4 lines
drivers/net/ethernet/ibm/emac/core.c +0
-0
[ ... ]
drivers/net/ethernet/ibm/emac/core.c static void emac_remove(struct platform_device *ofdev) · line 3236
Quoted 6 lines
drivers/net/ethernet/ibm/emac/core.c +2
-0@@ -3223,6 +3232,11 @@ static void emac_remove(struct platform_device *ofdev)cancel_work_sync (&dev->reset_work); +if (dev->ndev->phydev) +phy_disconnect (dev->ndev->phydev);
[Severity: High]
Can this phy_disconnect() run against a netdev that is already unregistered? Earlier in emac_remove() we have:
unregister_netdev(dev->ndev); cancel_work_sync(&dev->reset_work);
On the phy-handle path, emac_mdio_init_phy() calls phy_start() at probe time. emac_close() only calls netif_carrier_off() and never phy_stop(). So the phylib state machine keeps running while the interface is down, and phy_link_change() can turn carrier back on.
Suppose carrier is on when the driver is unbound. That happens if the interface was never opened, or if the link flapped after close. The sequence is then:
emac_remove()
unregister_netdev()
netdev_run_todo() /* NETREG_UNREGISTERED, waits for refs */
phy_disconnect()
phy_stop()
_phy_state_machine() /* PHY_HALTED, phydev->link == 1 */
phy_link_down()
phy_link_change(false)
netif_carrier_off()
linkwatch_fire_event()
linkwatch_add_event()
netdev_hold()netif_carrier_off() only skips NETREG_UNINITIALIZED:
if (!test_and_set_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
if (dev->reg_state == NETREG_UNINITIALIZED)
return;
atomic_inc(&dev->carrier_down_count);
linkwatch_fire_event(dev);
}The unregistered netdev therefore lands on lweventlist, and it holds a reference taken after netdev_run_todo() has already waited for all references to drop.
When emac_remove() returns, devres frees the netdev that came from devm_alloc_etherdev(). Later, __linkwatch_run_queue() still uses it:
dev = list_first_entry(&wrk, struct net_device, link_watch_list); list_del_init(&dev->link_watch_list); if (!netif_device_present(dev) ||
Wouldn't that be a use-after-free of the net_device? The same thing can happen if the running state machine reports any link change between unregister_netdev() and phy_disconnect().
The commit message says:
This fixes the phy-handle path as well, which calls phy_start() but never phy_stop().
Could the PHY be stopped or disconnected before unregister_netdev() instead? Another option would be to move phy_start() and phy_stop() into ndo_open and ndo_stop.
Quoted 5 lines
drivers/net/ethernet/ibm/emac/core.c +3
-0+if (of_phy_is_fixed_link (ofdev->dev.of_node)) +of_phy_deregister_fixed_link (ofdev->dev.of_node); +if (emac_has_feature (dev,EMAC_FTR_HAS_TAH ))tah_detach (dev->tah_dev, dev->tah_port);
[ ... ]
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261123093201.770552-1-xiaolinkui%40126.com
PATCH v2 2/2 net: ibm: emac: read the fixed-link status before adjusting the link
In reply to Linkui XiaoFrom: Linkui Xiao <[email protected]>
For a fixed link emac_init_phy() calls emac_adjust_link() once to copy the link parameters from the phy_device into dev->phy, and never starts the PHY state machine.
That worked while fixed_phy_register() copied the device tree status into the phy_device itself. Commit d99b408ed8e5 ("net: phy: fixed_phy: remove not needed initialization of phy_device members") dropped that, expecting the state machine to fill the members in once the PHY is started. EMAC does not start it, so emac_adjust_link() now copies SPEED_UNKNOWN and DUPLEX_UNKNOWN, and emac_configure() programs the MAC for 10 Mbit/s half duplex with flow control off, whatever the fixed-link property says.
Read the status once after attaching, as emac_mdio_read_link() already does for the phy-handle path. The PHY is emulated, so genphy_read_status() resolves speed, duplex and pause from the registers swphy generates out of the device tree status.
--- drivers/net/ethernet/ibm/emac/core.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/net/ethernet/ibm/emac/core.c +9 -2
@@ -2677,9 +2677,16 @@ static int emac_init_phy(struct emac_instance *dev)return res;if (!of_phy_connect (ndev, np, &emac_adjust_link,0 , - dev->phy_mode)) { + dev->phy_mode)) + res = -EINVAL ; +else + res =phy_read_status (ndev->phydev); + +if (res) { +if (ndev->phydev) +phy_disconnect (ndev->phydev);of_phy_deregister_fixed_link (np); -return -EINVAL ; +return res; }emac_adjust_link (ndev);
Signature · 1 line
2.25.1
Re: [PATCH net v2 2/2] net: ibm: emac: read the fixed-link status before adjusting the link
drivers/net/ethernet/ibm/emac/core.c static int emac_init_phy(struct emac_instance *dev) · line 2683
Quoted 6 lines
drivers/net/ethernet/ibm/emac/core.c +4
-1if (!of_phy_connect (ndev, np, &emac_adjust_link,0 , - dev->phy_mode)) { + dev->phy_mode)) + res = -EINVAL ; +else + res =phy_read_status (ndev->phydev);
As i said in the comments to the previous version, this is wrong.
Andrew
---
pw-bot: cr
Re: [PATCH net v2 2/2] net: ibm: emac: read the fixed-link status before adjusting the link
Hi Andrew,
Thanks for the review. Both of your mails arrived after v2 went out, so 2/2 still carried the phy_read_status() that the other one rules out.
Quoted 1 line
As i said in the comments to the previous version, this is wrong.
2/2 is dropped in v3 and the series is one patch again, keeping the phy_disconnect() / of_phy_deregister_fixed_link() teardown from v2 1/2. The link status is now read by the phylib state machine rather than by the driver.
Answered in more detail in my reply to your other mail.
Thanks,
Linkui
[email protected]
AI reviewRe: [PATCH net v2 2/2] net: ibm: emac: read the fixed-link status before adjusting the link
In reply to Linkui XiaoThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Critical: 0 · High: 1 · Medium: 0 · Low: 0
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [High] The fix does not cover 10 Mbit/s full-duplex fixed links.
--
net: ibm: emac: read the fixed-link status before adjusting the link
For a fixed link, emac_init_phy() never starts the PHY state machine, so after d99b408ed8e5 emac_adjust_link() copied SPEED_UNKNOWN/DUPLEX_UNKNOWN into dev->phy. This patch calls phy_read_status() once after of_phy_connect(), so the emulated swphy registers fill in speed, duplex and pause. It also disconnects the PHY if the read fails.
drivers/net/ethernet/ibm/emac/core.c static int emac_init_phy(struct emac_instance *dev) · line 2683
Quoted 13 lines
drivers/net/ethernet/ibm/emac/core.c +4
-1@@ -2677,9 +2677,16 @@ static int emac_init_phy(struct emac_instance *dev)return res;if (!of_phy_connect (ndev, np, &emac_adjust_link,0 , - dev->phy_mode)) { + dev->phy_mode)) + res = -EINVAL ; +else + res =phy_read_status (ndev->phydev);
[Severity: High]
Does this resolve a 10 Mbit/s full duplex fixed link? The commit message says:
The PHY is emulated, so genphy_read_status() resolves speed, duplex and pause from the registers swphy generates out of the device tree status.
For 10 Mbit the swphy tables in drivers/net/phy/swphy.c never set the 10 Mbit BMSR ability bits. Only the LPA register gets them:
static const struct swmii_regs speed[] = {
[SWMII_SPEED_10] = {
.lpa = LPA_10FULL | LPA_10HALF,
},
...static const struct swmii_regs duplex[] = {
[SWMII_DUPLEX_HALF] = {
.bmsr = BMSR_ESTATEN | BMSR_100HALF,
...
[SWMII_DUPLEX_FULL] = {
.bmsr = BMSR_ESTATEN | BMSR_100FULL,
...genphy_read_abilities() builds phydev->supported from BMSR only, so no 10baseT mode ends up in supported or advertising. Then, in phy_read_status()->genphy_read_status():
genphy_read_lpa()
lp_advertising gets 10baseT_Full plus the pause bits
phy_resolve_aneg_linkmode()
lp_advertising & advertising has no speed mode, so phy_caps_lookup_by_linkmode() returns NULL and speed/duplex stay SPEED_UNKNOWN/DUPLEX_UNKNOWN
phy_resolve_aneg_pause()
only runs for DUPLEX_FULL, so pause stays 0
phy_read_status() still returns 0, so the error path is skipped, and emac_adjust_link() copies DUPLEX_UNKNOWN and pause = 0 into dev->phy.
emac_configure() only sets full duplex and flow control when duplex is DUPLEX_FULL:
else if (dev->phy.duplex == DUPLEX_FULL)
mr1 |= EMAC_MR1_FDE | EMAC_MR1_MWSW_001;
...
if (!emac_has_feature(dev, EMAC_FTR_NO_FLOW_CONTROL_40x) &&
dev->phy.duplex == DUPLEX_FULL) {
if (dev->phy.pause)
mr1 |= EMAC_MR1_EIFC | EMAC_MR1_APP;So for a fixed-link node with speed 10 and full-duplex, wouldn't the MAC still be programmed half duplex with flow control off? That is the symptom the commit message describes, and it worked before d99b408ed8e5. 100 and 1000 Mbit links resolve correctly. 10 Mbit half duplex only comes out right because the unknown values fall back to 10/half.
Could swphy report the 10 Mbit abilities in BMSR? Alternatively, could the PHY-less path take speed, duplex and pause directly from the fixed-link status, or at least check that phydev->speed and phydev->duplex were resolved before using them?
Quoted 10 lines
drivers/net/ethernet/ibm/emac/core.c +5
-1+ +if (res) { +if (ndev->phydev) +phy_disconnect (ndev->phydev);of_phy_deregister_fixed_link (np); -return -EINVAL ; +return res; }emac_adjust_link (ndev);
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261123093201.770552-1-xiaolinkui%40126.com