Subject: Re: [PATCH net-next 9/9] net: phy: vitesse: implement downshift in vsc73xx phys

From: "Russell King (Oracle)" <[email-masked]>
Date: Tue, 30 Jul 2024 00:39:24 +0100

On Mon, Jul 29, 2024 at 11:06:15PM +0200, Pawel Dembicki wrote:
> +static int vsc73xx_get_downshift(struct phy_device *phydev, u8 *data)
> +{
> +	int page, val, cnt, enable;
> +
> +	page = phy_select_page(phydev, MII_VSC73XX_EXT_PAGE_1E);
> +	if (page < 0)
> +		return page;
> +
> +	val = __phy_read(phydev, MII_VSC73XX_PHY_CTRL_EXT3);
> +	if (val < 0)
> +		goto restore_page;
> +
> +	enable = FIELD_GET(MII_VSC73XX_PHY_CTRL_EXT3_DOWNSHIFT_EN, val);
> +	cnt = FIELD_GET(MII_VSC73XX_PHY_CTRL_EXT3_DOWNSHIFT_CNT, val) + 2;
> +
> +	*data = enable ? cnt : DOWNSHIFT_DEV_DISABLE;
> +
> +restore_page:
> +	phy_restore_page(phydev, page, val);
> +	return 0;

If you're going to use phy_select_page()..phy_restore_page(), please
read the documentation. If the __phy_read() fails, then you're
returning zero from this function, but leaving *data uninitialised.
Surely not what you want.

In any case, this complexity is unnecessary.

static int vsc73xx_get_downshift(struct phy_device *phydev, u8 *data)
{
	int val, enable, cnt;

	val = phy_read_paged(phydev, MII_VSC73XX_EXT_PAGE_1E,
			     MII_VSC73XX_PHY_CTRL_EXT3);
	if (val < 0)
		return val;

	enable = FIELD_GET(MII_VSC73XX_PHY_CTRL_EXT3_DOWNSHIFT_EN, val);
	cnt = FIELD_GET(MII_VSC73XX_PHY_CTRL_EXT3_DOWNSHIFT_CNT, val) + 2;

	*data = enable ? cnt : DOWNSHIFT_DEV_DISABLE;

	return 0;
}

is all that it needs.

> +}
> +
> +static int vsc73xx_set_downshift(struct phy_device *phydev, u8 cnt)
> +{
> +	int page, ret, val;
> +
> +	if (cnt > MII_VSC73XX_DOWNSHIFT_MAX)
> +		return -E2BIG;
> +	else if (cnt == MII_VSC73XX_DOWNSHIFT_INVAL)
> +		return -EINVAL;
> +
> +	page = phy_select_page(phydev, MII_VSC73XX_EXT_PAGE_1E);
> +	if (page < 0)
> +		return page;
> +
> +	if (!cnt) {
> +		ret = __phy_clear_bits(phydev, MII_VSC73XX_PHY_CTRL_EXT3,
> +				       MII_VSC73XX_PHY_CTRL_EXT3_DOWNSHIFT_EN);
> +	} else {
> +		val = MII_VSC73XX_PHY_CTRL_EXT3_DOWNSHIFT_EN |
> +		      FIELD_PREP(MII_VSC73XX_PHY_CTRL_EXT3_DOWNSHIFT_CNT,
> +				 cnt - 2);
> +
> +		ret = __phy_modify(phydev, MII_VSC73XX_PHY_CTRL_EXT3,
> +				   MII_VSC73XX_PHY_CTRL_EXT3_DOWNSHIFT_EN |
> +				   MII_VSC73XX_PHY_CTRL_EXT3_DOWNSHIFT_CNT,
> +				   val);
> +	}
> +
> +	ret = phy_restore_page(phydev, page, ret);
> +	if (ret < 0)
> +		return ret;

This is also needlessly over-complex.

	u16 mask, val;

	...

	mask = MII_VSC73XX_PHY_CTRL_EXT3_DOWNSHIFT_EN;

	if (!cnt) {
		val = 0;
	} else {
		mask |= MII_VSC73XX_PHY_CTRL_EXT3_DOWNSHIFT_CNT;
		val = MII_VSC73XX_PHY_CTRL_EXT3_DOWNSHIFT_EN |
		      FIELD_PREP(MII_VSC73XX_PHY_CTRL_EXT3_DOWNSHIFT_CNT,
				 cnt - 2);
	}

	ret = phy_modify_paged(phydev, MII_VSC73XX_EXT_PAGE_1E,
			       MII_VSC73XX_PHY_CTRL_EXT3, mask, val);
	if (ret < 0)
		return ret;

Would be equivalent to your code above, only simpler.

-- 
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!