Skip to contentSkip to conversation

introduce read_poll_timeout

PATCH Superseded Dejin Zheng 2 participants 18 messages Mar 23, 2020

Subsystems (5)

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v6 cover introduce read_poll_timeout

This patch sets is introduce read_poll_timeout macro, it is an extension of readx_poll_timeout macro. the accessor function op just supports only one parameter in the readx_poll_timeout macro, but this macro can supports multiple variable parameters for it. so functions like phy_read(struct phy_device *phydev, u32 regnum) and phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum) can use this poll timeout framework.

the first patch introduce read_poll_timeout macro, and the second patch redefined readx_poll_timeout macro by read_poll_timeout(), and the other patches are examples using read_poll_timeout macro.

v5 -> v6:
- add some check to keep the code more similar in patch 8
v4 -> v5:
- add some msleep() before call phy_read_mmd_poll_timeout() to keep the code more similar in patch 6 and patch 9.
- add a patch of drop by v4, it can add msleep before call phy_read_poll_timeout() to keep the code more similar.
v3 -> v4:
- add 3 examples of using new functions.
- deal with precedence issues for parameter cond.
- drop a patch about phy_poll_reset() function.
v2 -> v3:

	- modify the parameter order of newly added functions.
	  phy_read_mmd_poll_timeout(val, cond, sleep_us, timeout_us, \
				     phydev, devaddr, regnum)
				||
				\/
	  phy_read_mmd_poll_timeout(phydev, devaddr regnum, val, cond, \
				    sleep_us, timeout_us)

	  phy_read_poll_timeout(val, cond, sleep_us, timeout_us, \
				phydev, regnum)
				||
				\/
	  phy_read_poll_timeout(phydev, regnum, val, cond, sleep_us, \
				timeout_us)

v1 -> v2:
- passed a phydev, device address and a reg to replace args... parameter in phy_read_mmd_poll_timeout() by Andrew Lunn 's suggestion in patch 3. Andrew Lunn <[email protected]>, Thanks very much for your help!
- also in patch 3, handle phy_read_mmd return an error(the return value < 0) in phy_read_mmd_poll_timeout(). Thanks Andrew again.
- in patch 6, pass a phydev and a reg to replace args... parameter in phy_read_poll_timeout(), and also handle the phy_read() function's return error.

Dejin Zheng (10):
iopoll: introduce read_poll_timeout macro
iopoll: redefined readx_poll_timeout macro to simplify the code
net: phy: introduce phy_read_mmd_poll_timeout macro
net: phy: bcm84881: use phy_read_mmd_poll_timeout() to simplify the
code
net: phy: aquantia: use phy_read_mmd_poll_timeout() to simplify the
code
net: phy: marvell10g: use phy_read_mmd_poll_timeout() to simplify the
code
net: phy: introduce phy_read_poll_timeout macro
net: phy: use phy_read_poll_timeout() to simplify the code
net: phy: smsc: use phy_read_poll_timeout() to simplify the code
net: phy: tja11xx: use phy_read_poll_timeout() to simplify the code

 drivers/net/phy/aquantia_main.c | 13 ++++--------
 drivers/net/phy/bcm84881.c      | 27 ++++---------------------
 drivers/net/phy/marvell10g.c    | 16 ++++++---------
 drivers/net/phy/nxp-tja11xx.c   | 16 +++------------
 drivers/net/phy/phy_device.c    | 17 ++++++----------
 drivers/net/phy/smsc.c          | 17 ++++++----------
 include/linux/iopoll.h          | 36 ++++++++++++++++++++++++++-------
 include/linux/phy.h             | 28 +++++++++++++++++++++++++
 8 files changed, 86 insertions(+), 84 deletions(-)
Signature · 1 line
2.25.0
#1 lore raw

PATCH v6 1/10 iopoll: introduce read_poll_timeout macro

this macro is an extension of readx_poll_timeout macro. the accessor function op just supports only one parameter in the readx_poll_timeout macro, but this macro can supports multiple variable parameters for it. so functions like phy_read(struct phy_device *phydev, u32 regnum) and phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum) can also use this poll timeout framework.

---
v5 -> v6:
- no changed
v4 -> v5:
- no changed
v3 -> v4:
- no changed
v2 -> v3:
- no changed
v1 -> v2:
- no changed

 include/linux/iopoll.h | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
include/linux/iopoll.h +40 -0
index 35e15dfd4155..7d44a2e20267 100644
@@ -13,6 +13,46 @@
 #include <linux/errno.h>
 #include <linux/io.h>
 
+/**
+ * read_poll_timeout - Periodically poll an address until a condition is
+ *			met or a timeout occurs
+ * @op: accessor function (takes @args as its arguments)
+ * @val: Variable to read the value into
+ * @cond: Break condition (usually involving @val)
+ * @sleep_us: Maximum time to sleep between reads in us (0
+ *            tight-loops).  Should be less than ~20ms since usleep_range
+ *            is used (see Documentation/timers/timers-howto.rst).
+ * @timeout_us: Timeout in us, 0 means never timeout
+ * @args: arguments for @op poll
+ *
+ * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
+ * case, the last read value at @args is stored in @val. Must not
+ * be called from atomic context if sleep_us or timeout_us are used.
+ *
+ * When available, you'll probably want to use one of the specialized
+ * macros defined below rather than this macro directly.
+ */
+#define read_poll_timeout(op, val, cond, sleep_us, timeout_us, args...)	\
+({ \
+	u64 __timeout_us = (timeout_us); \
+	unsigned long __sleep_us = (sleep_us); \
+	ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \
+	might_sleep_if((__sleep_us) != 0); \
+	for (;;) { \
+		(val) = op(args); \
+		if (cond) \
+			break; \
+		if (__timeout_us && \
+		    ktime_compare(ktime_get(), __timeout) > 0) { \
+			(val) = op(args); \
+			break; \
+		} \
+		if (__sleep_us) \
+			usleep_range((__sleep_us >> 2) + 1, __sleep_us); \
+	} \
+	(cond) ? 0 : -ETIMEDOUT; \
+})
+
 /**
  * readx_poll_timeout - Periodically poll an address until a condition is met or a timeout occurs
  * @op: accessor function (takes @addr as its only argument)
Signature · 1 line
2.25.0
#2 lore raw

PATCH v6 2/10 iopoll: redefined readx_poll_timeout macro to simplify the code

In reply to Dejin Zheng

redefined readx_poll_timeout macro by read_poll_timeout to simplify the code.

---
v5 -> v6:
- no changed
v4 -> v5:
- no changed
v3 -> v4:
- no changed
v2 -> v3:
- no changed
v1 -> v2:
- no changed

 include/linux/iopoll.h | 20 +-------------------
 1 file changed, 1 insertion(+), 19 deletions(-)
include/linux/iopoll.h +1 -19
index 7d44a2e20267..29c016cd6249 100644
@@ -72,25 +72,7 @@
  * macros defined below rather than this macro directly.
  */
 #define readx_poll_timeout(op, addr, val, cond, sleep_us, timeout_us)	\
-({ \
-	u64 __timeout_us = (timeout_us); \
-	unsigned long __sleep_us = (sleep_us); \
-	ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \
-	might_sleep_if((__sleep_us) != 0); \
-	for (;;) { \
-		(val) = op(addr); \
-		if (cond) \
-			break; \
-		if (__timeout_us && \
-		    ktime_compare(ktime_get(), __timeout) > 0) { \
-			(val) = op(addr); \
-			break; \
-		} \
-		if (__sleep_us) \
-			usleep_range((__sleep_us >> 2) + 1, __sleep_us); \
-	} \
-	(cond) ? 0 : -ETIMEDOUT; \
-})
+	read_poll_timeout(op, val, cond, sleep_us, timeout_us, addr)
 
 /**
  * readx_poll_timeout_atomic - Periodically poll an address until a condition is met or a timeout occurs
Signature · 1 line
2.25.0
#3 lore raw

PATCH v6 3/10 net: phy: introduce phy_read_mmd_poll_timeout macro

In reply to Dejin Zheng

it is sometimes necessary to poll a phy register by phy_read_mmd() function until its value satisfies some condition. introduce phy_read_mmd_poll_timeout() macros that do this.

---
v5 -> v6:
- no changed
v4 -> v5:
- no changed
v3 -> v4:
- deal with precedence issues for parameter cond.
v2 -> v3:

	- modify the parameter order of newly added functions.
	  phy_read_mmd_poll_timeout(val, cond, sleep_us, timeout_us, \
				     phydev, devaddr, regnum)
				||
				\/
	  phy_read_mmd_poll_timeout(phydev, devaddr regnum, val, cond, \
				    sleep_us, timeout_us)

v1 -> v2:
- passed a phydev, device address and a reg to replace args... parameter in phy_read_mmd_poll_timeout() by Andrew Lunn 's suggestion. Andrew Lunn <[email protected]>, Thanks very much for your help!
- handle phy_read_mmd return an error(the return value < 0) in phy_read_mmd_poll_timeout(). Thanks Andrew again.

 include/linux/phy.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
include/linux/phy.h +15 -0
index 36d9dea04016..42a5ec9288d5 100644
@@ -24,6 +24,7 @@
 #include <linux/mod_devicetable.h>
 #include <linux/u64_stats_sync.h>
 #include <linux/irqreturn.h>
+#include <linux/iopoll.h>
 
 #include <linux/atomic.h>
 
@@ -784,6 +785,20 @@ static inline int __phy_modify_changed(struct phy_device *phydev, u32 regnum,
  */
 int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum);
 
+#define phy_read_mmd_poll_timeout(phydev, devaddr, regnum, val, cond, \
+				  sleep_us, timeout_us) \
+({ \
+	int ret = 0; \
+	ret = read_poll_timeout(phy_read_mmd, val, (cond) || val < 0, \
+				sleep_us, timeout_us, phydev, devaddr, \
+				regnum); \
+	if (val <  0) \
+		ret = val; \
+	if (ret) \
+		phydev_err(phydev, "%s failed: %d\n", __func__, ret); \
+	ret; \
+})
+
 /**
  * __phy_read_mmd - Convenience function for reading a register
  * from an MMD on a given PHY.
Signature · 1 line
2.25.0
Reviewed-by on v6 3/10
#4 lore raw

Re: [PATCH net-next v6 03/10] net: phy: introduce phy_read_mmd_poll_timeout macro

Quoted 51 lines · Dejin Zheng

it is sometimes necessary to poll a phy register by phy_read_mmd() function until its value satisfies some condition. introduce phy_read_mmd_poll_timeout() macros that do this.

---
v5 -> v6:
- no changed
v4 -> v5:
- no changed
v3 -> v4:
- deal with precedence issues for parameter cond.
v2 -> v3:

	- modify the parameter order of newly added functions.
	  phy_read_mmd_poll_timeout(val, cond, sleep_us, timeout_us, \
				     phydev, devaddr, regnum)
				||
				\/
	  phy_read_mmd_poll_timeout(phydev, devaddr regnum, val, cond, \
				    sleep_us, timeout_us)

v1 -> v2:
- passed a phydev, device address and a reg to replace args... parameter in phy_read_mmd_poll_timeout() by Andrew Lunn 's suggestion. Andrew Lunn <[email protected]>, Thanks very much for your help!
- handle phy_read_mmd return an error(the return value < 0) in phy_read_mmd_poll_timeout(). Thanks Andrew again.

 include/linux/phy.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
include/linux/phy.h +2 -0
index 36d9dea04016..42a5ec9288d5 100644
@@ -24,6 +24,7 @@
 #include <linux/mod_devicetable.h>
 #include <linux/u64_stats_sync.h>
 #include <linux/irqreturn.h>
+#include <linux/iopoll.h>
 
 #include <linux/atomic.h>
 
@@ -784,6 +785,20 @@ static inline int __phy_modify_changed(struct phy_device *phydev, u32 regnum,
  */
 int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum);
 
+#define phy_read_mmd_poll_timeout(phydev, devaddr, regnum, val, cond, \

include/linux/phy.h static inline int __phy_modify_changed(struct phy_device *phydev, u32 regnum, · line 798

Quoted 10 lines
include/linux/phy.h +10 -0
+				  sleep_us, timeout_us) \
+({ \
+	int ret = 0; \
+	ret = read_poll_timeout(phy_read_mmd, val, (cond) || val < 0, \
+				sleep_us, timeout_us, phydev, devaddr, \
+				regnum); \
+	if (val <  0) \
+		ret = val; \
+	if (ret) \
+		phydev_err(phydev, "%s failed: %d\n", __func__, ret); \

Likewise, you may consider declaring ret as __ret to avoid any possible shadowing/re-declaration. With that:

Signature · 1 line
Florian
#5 lore raw

PATCH v6 4/10 net: phy: bcm84881: use phy_read_mmd_poll_timeout() to simplify the code

In reply to Dejin Zheng

use phy_read_mmd_poll_timeout() to replace the poll codes for simplify bcm84881_wait_init() function.

---
v5 -> v6:
- no changed
v4 -> v5:
- no changed
v3 -> v4:
- no changed
v2 -> v3:
- adapt to it after modifying the parameter order of the newly added function
v1 -> v2:
- remove the handle of phy_read_mmd's return error.

 drivers/net/phy/bcm84881.c | 27 ++++-----------------------
 1 file changed, 4 insertions(+), 23 deletions(-)
drivers/net/phy/bcm84881.c +4 -23
index 14d55a77eb28..b70be775909c 100644
@@ -22,30 +22,11 @@ enum {
 
 static int bcm84881_wait_init(struct phy_device *phydev)
 {
-	unsigned int tries = 20;
-	int ret, val;
-
-	do {
-		val = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_CTRL1);
-		if (val < 0) {
-			ret = val;
-			break;
-		}
-		if (!(val & MDIO_CTRL1_RESET)) {
-			ret = 0;
-			break;
-		}
-		if (!--tries) {
-			ret = -ETIMEDOUT;
-			break;
-		}
-		msleep(100);
-	} while (1);
+	int val;
 
-	if (ret)
-		phydev_err(phydev, "%s failed: %d\n", __func__, ret);
-
-	return ret;
+	return phy_read_mmd_poll_timeout(phydev, MDIO_MMD_PMAPMD, MDIO_CTRL1,
+					 val, !(val & MDIO_CTRL1_RESET),
+					 100000, 2000000);
 }
 
 static int bcm84881_config_init(struct phy_device *phydev)
Signature · 1 line
2.25.0
#7 lore raw

PATCH v6 5/10 net: phy: aquantia: use phy_read_mmd_poll_timeout() to simplify the code

In reply to Dejin Zheng

use phy_read_mmd_poll_timeout() to replace the poll codes for simplify aqr107_wait_reset_complete() function.

---
v5 -> v6:
- no changed
v4 -> v5:
- no changed
v3 -> v4:
- no changed
v2 -> v3:
- adapt to it after modifying the parameter order of the newly added function
v1 -> v2:
- remove the handle of phy_read_mmd's return error.

 drivers/net/phy/aquantia_main.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
drivers/net/phy/aquantia_main.c +4 -9
index 31927b2c7d5a..db3e20938729 100644
@@ -451,16 +451,11 @@ static int aqr107_set_tunable(struct phy_device *phydev,
  */
 static int aqr107_wait_reset_complete(struct phy_device *phydev)
 {
-	int val, retries = 100;
-
-	do {
-		val = phy_read_mmd(phydev, MDIO_MMD_VEND1, VEND1_GLOBAL_FW_ID);
-		if (val < 0)
-			return val;
-		msleep(20);
-	} while (!val && --retries);
+	int val;
 
-	return val ? 0 : -ETIMEDOUT;
+	return phy_read_mmd_poll_timeout(phydev, MDIO_MMD_VEND1,
+					 VEND1_GLOBAL_FW_ID, val,
+					 val != 0, 20000, 2000000);
 }
 
 static void aqr107_chip_info(struct phy_device *phydev)
Signature · 1 line
2.25.0
#9 lore raw

PATCH v6 6/10 net: phy: marvell10g: use phy_read_mmd_poll_timeout() to simplify the code

In reply to Dejin Zheng

use phy_read_mmd_poll_timeout() to replace the poll codes for simplify mv3310_reset() function.

it should be add msleep(5) before call phy_read_mmd_poll_timeout() to keep the code more similar, but it will report that warning, so modify it to msleep(20).

./scripts/checkpatch.pl v5-0006-net-phy-marvell10g-use-phy_read_mmd_poll_timeout-.patch
WARNING: msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst #41: FILE: drivers/net/phy/marvell10g.c:251:
+ msleep(5);

---
v5 -> v6:
- no changed.
v4 -> v5:
- add msleep() to before call phy_read_mmd_poll_timeout() to keep the code more similar.
v3 -> v4:
- add this patch by Andrew's suggestion. Thanks Andrew!

 drivers/net/phy/marvell10g.c | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)
drivers/net/phy/marvell10g.c +6 -10
index 7e05b92504f0..c0fb8391c75b 100644
@@ -241,22 +241,18 @@ static int mv3310_power_up(struct phy_device *phydev)
 
 static int mv3310_reset(struct phy_device *phydev, u32 unit)
 {
-	int retries, val, err;
+	int val, err;
 
 	err = phy_modify_mmd(phydev, MDIO_MMD_PCS, unit + MDIO_CTRL1,
 			     MDIO_CTRL1_RESET, MDIO_CTRL1_RESET);
 	if (err < 0)
 		return err;
 
-	retries = 20;
-	do {
-		msleep(5);
-		val = phy_read_mmd(phydev, MDIO_MMD_PCS, unit + MDIO_CTRL1);
-		if (val < 0)
-			return val;
-	} while (val & MDIO_CTRL1_RESET && --retries);
-
-	return val & MDIO_CTRL1_RESET ? -ETIMEDOUT : 0;
+	msleep(20);
+	return phy_read_mmd_poll_timeout(phydev, MDIO_MMD_PCS,
+					 unit + MDIO_CTRL1, val,
+					 !(val & MDIO_CTRL1_RESET),
+					 5000, 80000);
 }
 
 static int mv3310_get_edpd(struct phy_device *phydev, u16 *edpd)
Signature · 1 line
2.25.0
#10 lore raw

PATCH v6 7/10 net: phy: introduce phy_read_poll_timeout macro

In reply to Dejin Zheng

it is sometimes necessary to poll a phy register by phy_read() function until its value satisfies some condition. introduce phy_read_poll_timeout() macros that do this.

---
v5 -> v6:
- no changed.
v4 -> v5:
- no changed.
v3 -> v4:
- deal with precedence issues for parameter cond.
v2 -> v3:

	- modify the parameter order of newly added functions.
	  phy_read_poll_timeout(val, cond, sleep_us, timeout_us, \
				phydev, regnum)
				||
				\/
	  phy_read_poll_timeout(phydev, regnum, val, cond, sleep_us, \
				timeout_us)

v1 -> v2:
- pass a phydev and a regnum to replace args... parameter in the phy_read_poll_timeout(), and also handle the phy_read() function's return error.

 include/linux/phy.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)
include/linux/phy.h +13 -0
index 42a5ec9288d5..f2e0aea13a2f 100644
@@ -714,6 +714,19 @@ static inline int phy_read(struct phy_device *phydev, u32 regnum)
 	return mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, regnum);
 }
 
+#define phy_read_poll_timeout(phydev, regnum, val, cond, sleep_us, timeout_us) \
+({ \
+	int ret = 0; \
+	ret = read_poll_timeout(phy_read, val, (cond) || val < 0, sleep_us, \
+				timeout_us, phydev, regnum); \
+	if (val <  0) \
+		ret = val; \
+	if (ret) \
+		phydev_err(phydev, "%s failed: %d\n", __func__, ret); \
+	ret; \
+})
+
+
 /**
  * __phy_read - convenience function for reading a given PHY register
  * @phydev: the phy_device struct
Signature · 1 line
2.25.0
#11 lore raw

Re: [PATCH net-next v6 07/10] net: phy: introduce phy_read_poll_timeout macro

Quoted 37 lines · Dejin Zheng

it is sometimes necessary to poll a phy register by phy_read() function until its value satisfies some condition. introduce phy_read_poll_timeout() macros that do this.

---
v5 -> v6:
- no changed.
v4 -> v5:
- no changed.
v3 -> v4:
- deal with precedence issues for parameter cond.
v2 -> v3:

	- modify the parameter order of newly added functions.
	  phy_read_poll_timeout(val, cond, sleep_us, timeout_us, \
				phydev, regnum)
				||
				\/
	  phy_read_poll_timeout(phydev, regnum, val, cond, sleep_us, \
				timeout_us)

v1 -> v2:
- pass a phydev and a regnum to replace args... parameter in the phy_read_poll_timeout(), and also handle the phy_read() function's return error.

 include/linux/phy.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)
include/linux/phy.h +0 -0
index 42a5ec9288d5..f2e0aea13a2f 100644
@@ -714,6 +714,19 @@ static inline int phy_read(struct phy_device *phydev, u32 regnum)
 	return mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, regnum);
 }

include/linux/phy.h static inline int phy_read(struct phy_device *phydev, u32 regnum) · line 725

Quoted 10 lines
include/linux/phy.h +9 -0
 
+#define phy_read_poll_timeout(phydev, regnum, val, cond, sleep_us, timeout_us) \
+({ \
+	int ret = 0; \
+	ret = read_poll_timeout(phy_read, val, (cond) || val < 0, sleep_us, \
+				timeout_us, phydev, regnum); \
+	if (val <  0) \
+		ret = val; \
+	if (ret) \
+		phydev_err(phydev, "%s failed: %d\n", __func__, ret); \> +	ret; \

Those variable names are likely going to be clashing with existing variables within a function, I would recommend you prefix with double
underscores: __ret, __val to avoid any variable re-declaration or shadowing.

Signature · 1 line
Florian
#12 lore raw

PATCH v6 8/10 net: phy: use phy_read_poll_timeout() to simplify the code

In reply to Dejin Zheng

use phy_read_poll_timeout() to replace the poll codes for simplify the code in phy_poll_reset() function.

---
v5 -> v6:
- add some check for keep the code more similar.
v4 -> v5:
- it can add msleep before call phy_read_poll_timeout() to keep the code more similar. so add it.
v3 -> v4:
- drop it.
v2 -> v3:
- adapt to it after modifying the parameter order of the newly added function
v1 -> v2:
- remove the handle of phy_read()'s return error.

 drivers/net/phy/phy_device.c | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)
drivers/net/phy/phy_device.c +6 -11
index a585faf8b844..eb1b177a9878 100644
@@ -1059,18 +1059,13 @@ EXPORT_SYMBOL(phy_disconnect);
 static int phy_poll_reset(struct phy_device *phydev)
 {
 	/* Poll until the reset bit clears (50ms per retry == 0.6 sec) */
-	unsigned int retries = 12;
-	int ret;
-
-	do {
-		msleep(50);
-		ret = phy_read(phydev, MII_BMCR);
-		if (ret < 0)
-			return ret;
-	} while (ret & BMCR_RESET && --retries);
-	if (ret & BMCR_RESET)
-		return -ETIMEDOUT;
+	int ret, val;
 
+	msleep(50);
+	ret = phy_read_poll_timeout(phydev, MII_BMCR, val, !(val & BMCR_RESET),
+				    50000, 550000);
+	if (ret)
+		return ret;
 	/* Some chips (smsc911x) may still need up to another 1ms after the
 	 * BMCR_RESET bit is cleared before they are usable.
 	 */
Signature · 1 line
2.25.0
#13 lore raw

Re: [PATCH net-next v6 08/10] net: phy: use phy_read_poll_timeout() to simplify the code

Quoted 38 lines · Dejin Zheng

use phy_read_poll_timeout() to replace the poll codes for simplify the code in phy_poll_reset() function.

---
v5 -> v6:
- add some check for keep the code more similar.
v4 -> v5:
- it can add msleep before call phy_read_poll_timeout() to keep the code more similar. so add it.
v3 -> v4:
- drop it.
v2 -> v3:
- adapt to it after modifying the parameter order of the newly added function
v1 -> v2:
- remove the handle of phy_read()'s return error.

 drivers/net/phy/phy_device.c | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)
drivers/net/phy/phy_device.c +0 -8
index a585faf8b844..eb1b177a9878 100644
@@ -1059,18 +1059,13 @@ EXPORT_SYMBOL(phy_disconnect);
 static int phy_poll_reset(struct phy_device *phydev)
 {
 	/* Poll until the reset bit clears (50ms per retry == 0.6 sec) */
-	unsigned int retries = 12;
-	int ret;
-
-	do {
-		msleep(50);
-		ret = phy_read(phydev, MII_BMCR);
-		if (ret < 0)
-			return ret;

drivers/net/phy/phy_device.c EXPORT_SYMBOL(phy_disconnect); · line 1068

Quoted 10 lines
drivers/net/phy/phy_device.c +6 -3
-	} while (ret & BMCR_RESET && --retries);
-	if (ret & BMCR_RESET)
-		return -ETIMEDOUT;
+	int ret, val;
 
+	msleep(50);
+	ret = phy_read_poll_timeout(phydev, MII_BMCR, val, !(val & BMCR_RESET),
+				    50000, 550000);
+	if (ret)
+		return ret;

You may consider creating a special variant of phy_read_poll_timeout() which sleeps before checking the condition, since there are at least two occurrences of this here and within marvell10g.c?

Signature · 1 line
Florian
#14 lore raw

PATCH v6 9/10 net: phy: smsc: use phy_read_poll_timeout() to simplify the code

In reply to Dejin Zheng

use phy_read_poll_timeout() to replace the poll codes for simplify lan87xx_read_status() function.

it should be add msleep(10) before call phy_read_poll_timeout() to keep the code more similar, but it will report that warning, so modify it to msleep(20).

./scripts/checkpatch.pl v5-0009-net-phy-smsc-use-phy_read_poll_timeout-to-simplif.patch
WARNING: msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst #42: FILE: drivers/net/phy/smsc.c:126:
+ msleep(10);

---
v5 -> v6:
- no changed.
v4 -> v5:
- add msleep before phy_read_poll_timeout() to keep the code more similar
v3 -> v4:
- add this patch by Andrew's suggestion. Thanks Andrew!

 drivers/net/phy/smsc.c | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)
drivers/net/phy/smsc.c +6 -11
index b73298250793..f888523086ed 100644
@@ -112,8 +112,6 @@ static int lan87xx_read_status(struct phy_device *phydev)
 	int err = genphy_read_status(phydev);
 
 	if (!phydev->link && priv->energy_enable) {
-		int i;
-
 		/* Disable EDPD to wake up PHY */
 		int rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
 		if (rc < 0)
@@ -125,15 +123,12 @@ static int lan87xx_read_status(struct phy_device *phydev)
 			return rc;
 
 		/* Wait max 640 ms to detect energy */
-		for (i = 0; i < 64; i++) {
-			/* Sleep to allow link test pulses to be sent */
-			msleep(10);
-			rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
-			if (rc < 0)
-				return rc;
-			if (rc & MII_LAN83C185_ENERGYON)
-				break;
-		}
+		msleep(20);
+		phy_read_poll_timeout(phydev, MII_LAN83C185_CTRL_STATUS, rc,
+				      rc & MII_LAN83C185_ENERGYON, 10000,
+				      620000);
+		if (rc < 0)
+			return rc;
 
 		/* Re-enable EDPD */
 		rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
Signature · 1 line
2.25.0
Reviewed-by on v6 9/10
#15 lore raw

Re: [PATCH net-next v6 09/10] net: phy: smsc: use phy_read_poll_timeout() to simplify the code

Quoted 13 lines · Dejin Zheng

use phy_read_poll_timeout() to replace the poll codes for simplify lan87xx_read_status() function.

it should be add msleep(10) before call phy_read_poll_timeout() to keep the code more similar, but it will report that warning, so modify it to msleep(20).

./scripts/checkpatch.pl v5-0009-net-phy-smsc-use-phy_read_poll_timeout-to-simplif.patch
WARNING: msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst #42: FILE: drivers/net/phy/smsc.c:126:

(quoted diff) +1 -0
+		msleep(10);
Quoted 2 lines
Signature · 1 line
Florian
#16 lore raw

PATCH v6 10/10 net: phy: tja11xx: use phy_read_poll_timeout() to simplify the code

In reply to Dejin Zheng

use phy_read_poll_timeout() to replace the poll codes for simplify tja11xx_check() function.

---
v5 -> v6:
- no changed.
v4 -> v5:
- no changed.
v3 -> v4:
- add this patch by Andrew's suggestion. Thanks Andrew!

 drivers/net/phy/nxp-tja11xx.c | 16 +++-------------
 1 file changed, 3 insertions(+), 13 deletions(-)
drivers/net/phy/nxp-tja11xx.c +3 -13
index b705d0bd798b..32ef32a4af3c 100644
@@ -72,20 +72,10 @@ static struct tja11xx_phy_stats tja11xx_hw_stats[] = {
 
 static int tja11xx_check(struct phy_device *phydev, u8 reg, u16 mask, u16 set)
 {
-	int i, ret;
-
-	for (i = 0; i < 200; i++) {
-		ret = phy_read(phydev, reg);
-		if (ret < 0)
-			return ret;
-
-		if ((ret & mask) == set)
-			return 0;
-
-		usleep_range(100, 150);
-	}
+	int val;
 
-	return -ETIMEDOUT;
+	return phy_read_poll_timeout(phydev, reg, val, (val & mask) == set,
+				     150, 30000);
 }
 
 static int phy_modify_check(struct phy_device *phydev, u8 reg,
Signature · 1 line
2.25.0
Reviewed-by on v6 10/10
#17 lore raw

Re: [PATCH net-next v6 10/10] net: phy: tja11xx: use phy_read_poll_timeout() to simplify the code

Quoted 6 lines · Dejin Zheng

use phy_read_poll_timeout() to replace the poll codes for simplify tja11xx_check() function.

Signature · 1 line
Florian