Commit c3b99db8 authored by David S. Miller's avatar David S. Miller

Merge branch 'hns-phy-loopback'

Lin Yun Sheng says:

====================
Add loopback support in phy_driver and hns ethtool fix

This Patch Set add set_loopback in phy_driver and use it to setup loopback
when doing ethtool phy self_test.

Patch V8:
	Respin the Patch based on net-next

Patch V7:
	1. Add comment why resume the phy in hns_nic_config_phy_loopback.
	2. Fix a typo error in patch description.

Patch V6:
	Fix Or'ing error code in __lb_setup.

Patch V5:
	Removing non loopback related code change.

Patch V4:
	1. Remove c45 checking
	2. Add -ENOTSUPP when function pointer is null,
	   take mutex in phy_loopback.

Patch V3:
	Calling phy_loopback enable and disable in pair in hns mac driver.

Patch V2:
	1. Add phy_loopback in phy_device.c.
	2. Do error checking and do the read and write once in
	   genphy_loopback.
	3. Remove gen10g_loopback in phy_device.c.

Patch V1:
	Initial Submit
====================
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parents 6992c6c5 67cd9a99
...@@ -360,6 +360,7 @@ enum hnae_loop { ...@@ -360,6 +360,7 @@ enum hnae_loop {
MAC_INTERNALLOOP_MAC = 0, MAC_INTERNALLOOP_MAC = 0,
MAC_INTERNALLOOP_SERDES, MAC_INTERNALLOOP_SERDES,
MAC_INTERNALLOOP_PHY, MAC_INTERNALLOOP_PHY,
MAC_LOOP_PHY_NONE,
MAC_LOOP_NONE, MAC_LOOP_NONE,
}; };
......
...@@ -259,67 +259,27 @@ static const char hns_nic_test_strs[][ETH_GSTRING_LEN] = { ...@@ -259,67 +259,27 @@ static const char hns_nic_test_strs[][ETH_GSTRING_LEN] = {
static int hns_nic_config_phy_loopback(struct phy_device *phy_dev, u8 en) static int hns_nic_config_phy_loopback(struct phy_device *phy_dev, u8 en)
{ {
#define COPPER_CONTROL_REG 0 int err;
#define PHY_POWER_DOWN BIT(11)
#define PHY_LOOP_BACK BIT(14)
u16 val = 0;
if (phy_dev->is_c45) /* c45 branch adding for XGE PHY */
return -ENOTSUPP;
if (en) { if (en) {
/* speed : 1000M */ /* Doing phy loopback in offline state, phy resuming is
phy_write(phy_dev, HNS_PHY_PAGE_REG, 2); * needed to power up the device.
phy_write(phy_dev, 21, 0x1046); */
err = phy_resume(phy_dev);
phy_write(phy_dev, HNS_PHY_PAGE_REG, 0); if (err)
/* Force Master */ goto out;
phy_write(phy_dev, 9, 0x1F00);
err = phy_loopback(phy_dev, true);
/* Soft-reset */
phy_write(phy_dev, 0, 0x9140);
/* If autoneg disabled,two soft-reset operations */
phy_write(phy_dev, 0, 0x9140);
phy_write(phy_dev, HNS_PHY_PAGE_REG, 0xFA);
/* Default is 0x0400 */
phy_write(phy_dev, 1, 0x418);
/* Force 1000M Link, Default is 0x0200 */
phy_write(phy_dev, 7, 0x20C);
/* Powerup Fiber */
phy_write(phy_dev, HNS_PHY_PAGE_REG, 1);
val = phy_read(phy_dev, COPPER_CONTROL_REG);
val &= ~PHY_POWER_DOWN;
phy_write(phy_dev, COPPER_CONTROL_REG, val);
/* Enable Phy Loopback */
phy_write(phy_dev, HNS_PHY_PAGE_REG, 0);
val = phy_read(phy_dev, COPPER_CONTROL_REG);
val |= PHY_LOOP_BACK;
val &= ~PHY_POWER_DOWN;
phy_write(phy_dev, COPPER_CONTROL_REG, val);
} else { } else {
phy_write(phy_dev, HNS_PHY_PAGE_REG, 0xFA); err = phy_loopback(phy_dev, false);
phy_write(phy_dev, 1, 0x400); if (err)
phy_write(phy_dev, 7, 0x200); goto out;
phy_write(phy_dev, HNS_PHY_PAGE_REG, 1); err = phy_suspend(phy_dev);
val = phy_read(phy_dev, COPPER_CONTROL_REG);
val |= PHY_POWER_DOWN;
phy_write(phy_dev, COPPER_CONTROL_REG, val);
phy_write(phy_dev, HNS_PHY_PAGE_REG, 0);
phy_write(phy_dev, 9, 0xF00);
val = phy_read(phy_dev, COPPER_CONTROL_REG);
val &= ~PHY_LOOP_BACK;
val |= PHY_POWER_DOWN;
phy_write(phy_dev, COPPER_CONTROL_REG, val);
} }
return 0;
out:
return err;
} }
static int __lb_setup(struct net_device *ndev, static int __lb_setup(struct net_device *ndev,
...@@ -332,10 +292,9 @@ static int __lb_setup(struct net_device *ndev, ...@@ -332,10 +292,9 @@ static int __lb_setup(struct net_device *ndev,
switch (loop) { switch (loop) {
case MAC_INTERNALLOOP_PHY: case MAC_INTERNALLOOP_PHY:
if ((phy_dev) && (!phy_dev->is_c45)) {
ret = hns_nic_config_phy_loopback(phy_dev, 0x1); ret = hns_nic_config_phy_loopback(phy_dev, 0x1);
ret |= h->dev->ops->set_loopback(h, loop, 0x1); if (!ret)
} ret = h->dev->ops->set_loopback(h, loop, 0x1);
break; break;
case MAC_INTERNALLOOP_MAC: case MAC_INTERNALLOOP_MAC:
if ((h->dev->ops->set_loopback) && if ((h->dev->ops->set_loopback) &&
...@@ -346,16 +305,16 @@ static int __lb_setup(struct net_device *ndev, ...@@ -346,16 +305,16 @@ static int __lb_setup(struct net_device *ndev,
if (h->dev->ops->set_loopback) if (h->dev->ops->set_loopback)
ret = h->dev->ops->set_loopback(h, loop, 0x1); ret = h->dev->ops->set_loopback(h, loop, 0x1);
break; break;
case MAC_LOOP_PHY_NONE:
ret = hns_nic_config_phy_loopback(phy_dev, 0x0);
case MAC_LOOP_NONE: case MAC_LOOP_NONE:
if ((phy_dev) && (!phy_dev->is_c45)) if (!ret && h->dev->ops->set_loopback) {
ret |= hns_nic_config_phy_loopback(phy_dev, 0x0);
if (h->dev->ops->set_loopback) {
if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII) if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII)
ret |= h->dev->ops->set_loopback(h, ret = h->dev->ops->set_loopback(h,
MAC_INTERNALLOOP_MAC, 0x0); MAC_INTERNALLOOP_MAC, 0x0);
ret |= h->dev->ops->set_loopback(h, if (!ret)
ret = h->dev->ops->set_loopback(h,
MAC_INTERNALLOOP_SERDES, 0x0); MAC_INTERNALLOOP_SERDES, 0x0);
} }
break; break;
...@@ -582,12 +541,15 @@ static int __lb_run_test(struct net_device *ndev, ...@@ -582,12 +541,15 @@ static int __lb_run_test(struct net_device *ndev,
return ret_val; return ret_val;
} }
static int __lb_down(struct net_device *ndev) static int __lb_down(struct net_device *ndev, enum hnae_loop loop)
{ {
struct hns_nic_priv *priv = netdev_priv(ndev); struct hns_nic_priv *priv = netdev_priv(ndev);
struct hnae_handle *h = priv->ae_handle; struct hnae_handle *h = priv->ae_handle;
int ret; int ret;
if (loop == MAC_INTERNALLOOP_PHY)
ret = __lb_setup(ndev, MAC_LOOP_PHY_NONE);
else
ret = __lb_setup(ndev, MAC_LOOP_NONE); ret = __lb_setup(ndev, MAC_LOOP_NONE);
if (ret) if (ret)
netdev_err(ndev, "%s: __lb_setup return error(%d)!\n", netdev_err(ndev, "%s: __lb_setup return error(%d)!\n",
...@@ -644,7 +606,8 @@ static void hns_nic_self_test(struct net_device *ndev, ...@@ -644,7 +606,8 @@ static void hns_nic_self_test(struct net_device *ndev,
if (!data[test_index]) { if (!data[test_index]) {
data[test_index] = __lb_run_test( data[test_index] = __lb_run_test(
ndev, (enum hnae_loop)st_param[i][0]); ndev, (enum hnae_loop)st_param[i][0]);
(void)__lb_down(ndev); (void)__lb_down(ndev,
(enum hnae_loop)st_param[i][0]);
} }
if (data[test_index]) if (data[test_index])
......
...@@ -2171,6 +2171,7 @@ static struct phy_driver marvell_drivers[] = { ...@@ -2171,6 +2171,7 @@ static struct phy_driver marvell_drivers[] = {
.get_sset_count = marvell_get_sset_count, .get_sset_count = marvell_get_sset_count,
.get_strings = marvell_get_strings, .get_strings = marvell_get_strings,
.get_stats = marvell_get_stats, .get_stats = marvell_get_stats,
.set_loopback = genphy_loopback,
}, },
{ {
.phy_id = MARVELL_PHY_ID_88E1540, .phy_id = MARVELL_PHY_ID_88E1540,
......
...@@ -1136,6 +1136,39 @@ int phy_resume(struct phy_device *phydev) ...@@ -1136,6 +1136,39 @@ int phy_resume(struct phy_device *phydev)
} }
EXPORT_SYMBOL(phy_resume); EXPORT_SYMBOL(phy_resume);
int phy_loopback(struct phy_device *phydev, bool enable)
{
struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
int ret = 0;
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 && phydrv->set_loopback)
ret = phydrv->set_loopback(phydev, enable);
else
ret = -EOPNOTSUPP;
if (ret)
goto out;
phydev->loopback_enabled = enable;
out:
mutex_unlock(&phydev->lock);
return ret;
}
EXPORT_SYMBOL(phy_loopback);
/* Generic PHY support and helper functions */ /* Generic PHY support and helper functions */
/** /**
...@@ -1584,6 +1617,23 @@ int genphy_resume(struct phy_device *phydev) ...@@ -1584,6 +1617,23 @@ int genphy_resume(struct phy_device *phydev)
} }
EXPORT_SYMBOL(genphy_resume); EXPORT_SYMBOL(genphy_resume);
int genphy_loopback(struct phy_device *phydev, bool enable)
{
int value;
value = phy_read(phydev, MII_BMCR);
if (value < 0)
return value;
if (enable)
value |= BMCR_LOOPBACK;
else
value &= ~BMCR_LOOPBACK;
return phy_write(phydev, MII_BMCR, value);
}
EXPORT_SYMBOL(genphy_loopback);
static int __set_phy_supported(struct phy_device *phydev, u32 max_speed) static int __set_phy_supported(struct phy_device *phydev, u32 max_speed)
{ {
/* The default values for phydev->supported are provided by the PHY /* The default values for phydev->supported are provided by the PHY
...@@ -1829,6 +1879,7 @@ static struct phy_driver genphy_driver = { ...@@ -1829,6 +1879,7 @@ static struct phy_driver genphy_driver = {
.read_status = genphy_read_status, .read_status = genphy_read_status,
.suspend = genphy_suspend, .suspend = genphy_suspend,
.resume = genphy_resume, .resume = genphy_resume,
.set_loopback = genphy_loopback,
}; };
static int __init phy_init(void) static int __init phy_init(void)
......
...@@ -372,6 +372,7 @@ struct phy_c45_device_ids { ...@@ -372,6 +372,7 @@ struct phy_c45_device_ids {
* has_fixups: Set to true if this phy has fixups/quirks. * has_fixups: Set to true if this phy has fixups/quirks.
* suspended: Set to true if this phy has been suspended successfully. * suspended: Set to true if this phy has been suspended successfully.
* sysfs_links: Internal boolean tracking sysfs symbolic links setup/removal. * sysfs_links: Internal boolean tracking sysfs symbolic links setup/removal.
* loopback_enabled: Set true if this phy has been loopbacked successfully.
* state: state of the PHY for management purposes * state: state of the PHY for management purposes
* dev_flags: Device-specific flags used by the PHY driver. * dev_flags: Device-specific flags used by the PHY driver.
* link_timeout: The number of timer firings to wait before the * link_timeout: The number of timer firings to wait before the
...@@ -409,6 +410,7 @@ struct phy_device { ...@@ -409,6 +410,7 @@ struct phy_device {
bool has_fixups; bool has_fixups;
bool suspended; bool suspended;
bool sysfs_links; bool sysfs_links;
bool loopback_enabled;
enum phy_state state; enum phy_state state;
...@@ -648,6 +650,7 @@ struct phy_driver { ...@@ -648,6 +650,7 @@ struct phy_driver {
int (*set_tunable)(struct phy_device *dev, int (*set_tunable)(struct phy_device *dev,
struct ethtool_tunable *tuna, struct ethtool_tunable *tuna,
const void *data); const void *data);
int (*set_loopback)(struct phy_device *dev, bool enable);
}; };
#define to_phy_driver(d) container_of(to_mdio_common_driver(d), \ #define to_phy_driver(d) container_of(to_mdio_common_driver(d), \
struct phy_driver, mdiodrv) struct phy_driver, mdiodrv)
...@@ -793,6 +796,7 @@ void phy_device_remove(struct phy_device *phydev); ...@@ -793,6 +796,7 @@ void phy_device_remove(struct phy_device *phydev);
int phy_init_hw(struct phy_device *phydev); int phy_init_hw(struct phy_device *phydev);
int phy_suspend(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);
struct phy_device *phy_attach(struct net_device *dev, const char *bus_id, struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
phy_interface_t interface); phy_interface_t interface);
struct phy_device *phy_find_first(struct mii_bus *bus); struct phy_device *phy_find_first(struct mii_bus *bus);
...@@ -847,6 +851,7 @@ int genphy_update_link(struct phy_device *phydev); ...@@ -847,6 +851,7 @@ int genphy_update_link(struct phy_device *phydev);
int genphy_read_status(struct phy_device *phydev); int genphy_read_status(struct phy_device *phydev);
int genphy_suspend(struct phy_device *phydev); int genphy_suspend(struct phy_device *phydev);
int genphy_resume(struct phy_device *phydev); int genphy_resume(struct phy_device *phydev);
int genphy_loopback(struct phy_device *phydev, bool enable);
int genphy_soft_reset(struct phy_device *phydev); int genphy_soft_reset(struct phy_device *phydev);
static inline int genphy_no_soft_reset(struct phy_device *phydev) static inline int genphy_no_soft_reset(struct phy_device *phydev)
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment