diff mbox series

[net] net: phy: mscc: Add error check when __phy_read() failed

Message ID 20220510035458.9804-1-wanjiabing@vivo.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series [net] net: phy: mscc: Add error check when __phy_read() failed | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for net
netdev/fixes_present success Fixes tag present in non-next series
netdev/subject_prefix success Link
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers success CCed 10 of 10 maintainers
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 32 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Jiabing Wan May 10, 2022, 3:54 a.m. UTC
Calling __phy_read() might return a negative error code. Use 'int'
to declare variables which call __phy_read() and also add error check
for them.

Fixes: fa164e40c53b ("net: phy: mscc: split the driver into separate files")
Signed-off-by: Wan Jiabing <wanjiabing@vivo.com>
---
 drivers/net/phy/mscc/mscc_macsec.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

Comments

Andrew Lunn May 10, 2022, 12:25 p.m. UTC | #1
On Tue, May 10, 2022 at 11:54:56AM +0800, Wan Jiabing wrote:
> Calling __phy_read() might return a negative error code. Use 'int'
> to declare variables which call __phy_read() and also add error check
> for them.

It would be good to add a comment here:

The numerous callers of vsc8584_macsec_phy_read() don't expect it to
fail. So don't return the error code from __phy_read(), but also don't
return random values if it does fail.

The commit message should try to answer any questions to reviewer
has. And when i first looked at the change, i thought this is wrong,
the error code is thrown away. But then i remembers our discussion. So
it is good to mention that in the commit message.

> Fixes: fa164e40c53b ("net: phy: mscc: split the driver into separate files")
> Signed-off-by: Wan Jiabing <wanjiabing@vivo.com>
> ---
>  drivers/net/phy/mscc/mscc_macsec.c | 11 ++++++++---
>  1 file changed, 8 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/net/phy/mscc/mscc_macsec.c b/drivers/net/phy/mscc/mscc_macsec.c
> index b7b2521c73fb..8a63e32fafa0 100644
> --- a/drivers/net/phy/mscc/mscc_macsec.c
> +++ b/drivers/net/phy/mscc/mscc_macsec.c
> @@ -22,9 +22,9 @@
>  static u32 vsc8584_macsec_phy_read(struct phy_device *phydev,
>  				   enum macsec_bank bank, u32 reg)
>  {
> -	u32 val, val_l = 0, val_h = 0;
>  	unsigned long deadline;
> -	int rc;
> +	int rc, val, val_l, val_h;
> +	u32 ret = 0;

Networking code uses "reverse christmas tree", meaning these lines
should be sorted, longest first, shortest last. So deadline needs to
come after val_h.

     Andrew
diff mbox series

Patch

diff --git a/drivers/net/phy/mscc/mscc_macsec.c b/drivers/net/phy/mscc/mscc_macsec.c
index b7b2521c73fb..8a63e32fafa0 100644
--- a/drivers/net/phy/mscc/mscc_macsec.c
+++ b/drivers/net/phy/mscc/mscc_macsec.c
@@ -22,9 +22,9 @@ 
 static u32 vsc8584_macsec_phy_read(struct phy_device *phydev,
 				   enum macsec_bank bank, u32 reg)
 {
-	u32 val, val_l = 0, val_h = 0;
 	unsigned long deadline;
-	int rc;
+	int rc, val, val_l, val_h;
+	u32 ret = 0;
 
 	rc = phy_select_page(phydev, MSCC_PHY_PAGE_MACSEC);
 	if (rc < 0)
@@ -47,15 +47,20 @@  static u32 vsc8584_macsec_phy_read(struct phy_device *phydev,
 	deadline = jiffies + msecs_to_jiffies(PROC_CMD_NCOMPLETED_TIMEOUT_MS);
 	do {
 		val = __phy_read(phydev, MSCC_EXT_PAGE_MACSEC_19);
+		if (val < 0)
+			goto failed;
 	} while (time_before(jiffies, deadline) && !(val & MSCC_PHY_MACSEC_19_CMD));
 
 	val_l = __phy_read(phydev, MSCC_EXT_PAGE_MACSEC_17);
 	val_h = __phy_read(phydev, MSCC_EXT_PAGE_MACSEC_18);
 
+	if (val_l > 0 && val_h > 0)
+		ret = (val_h << 16) | val_l;
+
 failed:
 	phy_restore_page(phydev, rc, rc);
 
-	return (val_h << 16) | val_l;
+	return ret;
 }
 
 static void vsc8584_macsec_phy_write(struct phy_device *phydev,