Skip to content

Commit

Permalink
net: phy: Support speed selection for PHY loopback
Browse files Browse the repository at this point in the history
phy_loopback() leaves it to the PHY driver to select the speed of the
loopback mode. Thus, the speed of the loopback mode depends on the PHY
driver in use.

Add support for speed selection to phy_loopback() to enable loopback
with defined speeds. Ensure that link up is signaled if speed changes
as speed is not allowed to change during link up. Link down and up is
necessary for a new speed.

Signed-off-by: Gerhard Engleder <gerhard@engleder-embedded.com>
Link: https://patch.msgid.link/20250312203010.47429-3-gerhard@engleder-embedded.com
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
  • Loading branch information
Gerhard Engleder authored and Paolo Abeni committed Mar 20, 2025
1 parent 45456e3 commit 0d60fd5
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 48 deletions.
2 changes: 1 addition & 1 deletion drivers/net/ethernet/engleder/tsnep_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ static int tsnep_phy_loopback(struct tsnep_adapter *adapter, bool enable)
{
int retval;

retval = phy_loopback(adapter->phydev, enable);
retval = phy_loopback(adapter->phydev, enable, 0);

/* PHY link state change is not signaled if loopback is enabled, it
* would delay a working loopback anyway, let's ensure that loopback
Expand Down
4 changes: 2 additions & 2 deletions drivers/net/ethernet/hisilicon/hns/hns_ethtool.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,9 @@ static int hns_nic_config_phy_loopback(struct phy_device *phy_dev, u8 en)
if (err)
goto out;

err = phy_loopback(phy_dev, true);
err = phy_loopback(phy_dev, true, 0);
} else {
err = phy_loopback(phy_dev, false);
err = phy_loopback(phy_dev, false, 0);
if (err)
goto out;

Expand Down
4 changes: 2 additions & 2 deletions drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7875,15 +7875,15 @@ static int hclge_enable_phy_loopback(struct hclge_dev *hdev,
if (ret)
return ret;

return phy_loopback(phydev, true);
return phy_loopback(phydev, true, 0);
}

static int hclge_disable_phy_loopback(struct hclge_dev *hdev,
struct phy_device *phydev)
{
int ret;

ret = phy_loopback(phydev, false);
ret = phy_loopback(phydev, false, 0);
if (ret)
return ret;

Expand Down
2 changes: 1 addition & 1 deletion drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_mdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ void hclge_mac_start_phy(struct hclge_dev *hdev)
if (!phydev)
return;

phy_loopback(phydev, false);
phy_loopback(phydev, false, 0);

phy_start(phydev);
}
Expand Down
8 changes: 4 additions & 4 deletions drivers/net/ethernet/stmicro/stmmac/stmmac_selftests.c
Original file line number Diff line number Diff line change
Expand Up @@ -382,14 +382,14 @@ static int stmmac_test_phy_loopback(struct stmmac_priv *priv)
if (!priv->dev->phydev)
return -EOPNOTSUPP;

ret = phy_loopback(priv->dev->phydev, true);
ret = phy_loopback(priv->dev->phydev, true, 0);
if (ret)
return ret;

attr.dst = priv->dev->dev_addr;
ret = __stmmac_test_loopback(priv, &attr);

phy_loopback(priv->dev->phydev, false);
phy_loopback(priv->dev->phydev, false, 0);
return ret;
}

Expand Down Expand Up @@ -1985,7 +1985,7 @@ void stmmac_selftest_run(struct net_device *dev,
case STMMAC_LOOPBACK_PHY:
ret = -EOPNOTSUPP;
if (dev->phydev)
ret = phy_loopback(dev->phydev, true);
ret = phy_loopback(dev->phydev, true, 0);
if (!ret)
break;
fallthrough;
Expand Down Expand Up @@ -2018,7 +2018,7 @@ void stmmac_selftest_run(struct net_device *dev,
case STMMAC_LOOPBACK_PHY:
ret = -EOPNOTSUPP;
if (dev->phydev)
ret = phy_loopback(dev->phydev, false);
ret = phy_loopback(dev->phydev, false, 0);
if (!ret)
break;
fallthrough;
Expand Down
87 changes: 87 additions & 0 deletions drivers/net/phy/phy.c
Original file line number Diff line number Diff line change
Expand Up @@ -1707,6 +1707,93 @@ void phy_mac_interrupt(struct phy_device *phydev)
}
EXPORT_SYMBOL(phy_mac_interrupt);

/**
* phy_loopback - Configure loopback mode of PHY
* @phydev: target phy_device struct
* @enable: enable or disable loopback mode
* @speed: enable loopback mode with speed
*
* Configure loopback mode of PHY and signal link down and link up if speed is
* changing.
*
* Return: 0 on success, negative error code on failure.
*/
int phy_loopback(struct phy_device *phydev, bool enable, int speed)
{
bool link_up = false;
int ret = 0;

if (!phydev->drv)
return -EIO;

mutex_lock(&phydev->lock);

if (enable && phydev->loopback_enabled) {
ret = -EBUSY;
goto out;
}

if (!enable && !phydev->loopback_enabled) {
ret = -EINVAL;
goto out;
}

if (enable) {
/*
* Link up is signaled with a defined speed. If speed changes,
* then first link down and after that link up needs to be
* signaled.
*/
if (phydev->link && phydev->state == PHY_RUNNING) {
/* link is up and signaled */
if (speed && phydev->speed != speed) {
/* signal link down and up for new speed */
phydev->link = false;
phydev->state = PHY_NOLINK;
phy_link_down(phydev);

link_up = true;
}
} else {
/* link is not signaled */
if (speed) {
/* signal link up for new speed */
link_up = true;
}
}
}

if (phydev->drv->set_loopback)
ret = phydev->drv->set_loopback(phydev, enable, speed);
else
ret = genphy_loopback(phydev, enable, speed);

if (ret) {
if (enable) {
/* try to restore link if enabling loopback fails */
if (phydev->drv->set_loopback)
phydev->drv->set_loopback(phydev, false, 0);
else
genphy_loopback(phydev, false, 0);
}

goto out;
}

if (link_up) {
phydev->link = true;
phydev->state = PHY_RUNNING;
phy_link_up(phydev);
}

phydev->loopback_enabled = enable;

out:
mutex_unlock(&phydev->lock);
return ret;
}
EXPORT_SYMBOL(phy_loopback);

/**
* phy_eee_tx_clock_stop_capable() - indicate whether the MAC can stop tx clock
* @phydev: target phy_device struct
Expand Down
35 changes: 0 additions & 35 deletions drivers/net/phy/phy_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -1818,41 +1818,6 @@ int phy_resume(struct phy_device *phydev)
}
EXPORT_SYMBOL(phy_resume);

int phy_loopback(struct phy_device *phydev, bool enable)
{
int ret = 0;

if (!phydev->drv)
return -EIO;

mutex_lock(&phydev->lock);

if (enable && phydev->loopback_enabled) {
ret = -EBUSY;
goto out;
}

if (!enable && !phydev->loopback_enabled) {
ret = -EINVAL;
goto out;
}

if (phydev->drv->set_loopback)
ret = phydev->drv->set_loopback(phydev, enable, 0);
else
ret = genphy_loopback(phydev, enable, 0);

if (ret)
goto out;

phydev->loopback_enabled = enable;

out:
mutex_unlock(&phydev->lock);
return ret;
}
EXPORT_SYMBOL(phy_loopback);

/**
* phy_reset_after_clk_enable - perform a PHY reset if needed
* @phydev: target phy_device struct
Expand Down
2 changes: 1 addition & 1 deletion include/linux/phy.h
Original file line number Diff line number Diff line change
Expand Up @@ -1808,7 +1808,7 @@ int phy_init_hw(struct phy_device *phydev);
int phy_suspend(struct phy_device *phydev);
int phy_resume(struct phy_device *phydev);
int __phy_resume(struct phy_device *phydev);
int phy_loopback(struct phy_device *phydev, bool enable);
int phy_loopback(struct phy_device *phydev, bool enable, int speed);
int phy_sfp_connect_phy(void *upstream, struct phy_device *phy);
void phy_sfp_disconnect_phy(void *upstream, struct phy_device *phy);
void phy_sfp_attach(void *upstream, struct sfp_bus *bus);
Expand Down
4 changes: 2 additions & 2 deletions net/core/selftests.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,15 +299,15 @@ static int net_test_phy_loopback_enable(struct net_device *ndev)
if (!ndev->phydev)
return -EOPNOTSUPP;

return phy_loopback(ndev->phydev, true);
return phy_loopback(ndev->phydev, true, 0);
}

static int net_test_phy_loopback_disable(struct net_device *ndev)
{
if (!ndev->phydev)
return -EOPNOTSUPP;

return phy_loopback(ndev->phydev, false);
return phy_loopback(ndev->phydev, false, 0);
}

static int net_test_phy_loopback_udp(struct net_device *ndev)
Expand Down

0 comments on commit 0d60fd5

Please sign in to comment.