net: stmmac: Cleanup rx coalescing computation when using RIWT
Subsystems (4)
Maxime Chevallier
maintainercover net: stmmac: Cleanup rx coalescing computation when using RIWT
Currently when configuring interrupt coalescing on devices that relies on the Receive Interrupt Watchdog Timer feature of dwmac, the computation of the RIWT timings leads to off-by-one values when reporting the timings back to userspace.
RIWT works by arming a watchdog timer upon receiving frames with the RI bit not set in the descriptor. The timer duration is expressed in units of 256 stmmac clock ticks, and therefore requires a bit of computation to derive it :
riwt = (rx_usecs * n_clk_ticks_per_usec) / 256
and conversely
rx_usecs = (riwt * 256) / n_clk_ticks_per_usec
This computation as-is leads to a consistent off-by-one when setting then getting back the rx-usecs value due to rounding errors (by truncation):
ethtool -C eth1 rx-usecs 42
ethtool -c eth1
-> reports rx-usecs: 41
Let's use DIV_ROUND_CLOSEST instead for the computations. It does have one side effect, the accepted boundaries for rx-usecs also shifts by one now, going from [16us, 246us] to [15us, 245us]. For that reason, I'm not targeting the net tree here, and it's overall a very small issue.
Maxime
Maxime Chevallier (2):
net: stmmac: ethtool: Comment the magic numbers in RIWT computation
net: stmmac: ethtool: Address off-by-one when reading the coal
rx-usecs
drivers/net/ethernet/stmicro/stmmac/stmmac_ethtool.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
Signature · 1 line
2.55.0
Maxime Chevallier
maintainerPATCH 1/2 net: stmmac: ethtool: Comment the magic numbers in RIWT computation
Receive Interrupt Watchdog Timer is an RX interrupt coalescing mechanism used by some variants of dwmac. It allows waiting a bit before triggering the rx interrupts, allowing for batch processing.
The RIWT is configured with a granularity of 256 stmmac clk ticks. Let's add a comment for that and wrap the raw "1000000" into USEC_PER_SEC, as we're computing "how many clock cycles in one microsec" with that step.
--- drivers/net/ethernet/stmicro/stmmac/stmmac_ethtool.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/net/ethernet/stmicro/stmmac/stmmac_ethtool.c +5 -2
@@ -759,7 +759,10 @@ static u32 stmmac_usec2riwt(u32 usec, struct stmmac_priv *priv)return 0 ; } -return (usec * (clk /1000000 )) /256 ; +/* Receive Interrupt Watchdog Timer (riwt) has a resolution of 256 +* ticks. +*/ +return (usec * (clk /USEC_PER_SEC )) /256 ; }static u32 stmmac_riwt2usec (u32 riwt,struct stmmac_priv *priv)
@@ -772,7 +775,7 @@ static u32 stmmac_riwt2usec(u32 riwt, struct stmmac_priv *priv)return 0 ; } -return (riwt *256 ) / (clk /1000000 ); +return (riwt *256 ) / (clk /USEC_PER_SEC ); } static int __stmmac_get_coalesce(struct net_device *dev,
Signature · 1 line
2.55.0
Re: [PATCH net-next 1/2] net: stmmac: ethtool: Comment the magic numbers in RIWT computation
Quoted 9 lines · Maxime Chevallier
Receive Interrupt Watchdog Timer is an RX interrupt coalescing mechanism used by some variants of dwmac. It allows waiting a bit before triggering the rx interrupts, allowing for batch processing.
The RIWT is configured with a granularity of 256 stmmac clk ticks. Let's add a comment for that and wrap the raw "1000000" into
USEC_PER_SEC, as we're computing "how many clock cycles in one microsec" with that step.Signed-off-by Maxime Chevallier <[email protected]>
Andrew
Maxime Chevallier
maintainerPATCH 2/2 net: stmmac: ethtool: Address off-by-one when reading the coal rx-usecs
In reply to Maxime ChevallierWhen reading the rx-usecs coalescing parameters on a dwmac variant that uses the RIWT for RX interrupt coalescing, we convert the riwt value to usecs :
- One riwt cycle is 256 clock ticks, we compute how many ticks in $riwt cycles
- divide that by how many ticks in a microsecond, and we get the rx-usecs.
The opposite computation is done when setting the rx-usecs param.
Because of the 256 ratio, we're subjected to off-by-one errors in the value read-back, which can be reliably measured on i.mx8MP :
$ ethtool -C eth1 rx-usecs 102
$ ethtool -c eth1 Coalesce parameters for eth1: [...] rx-usecs: 101
Let's be more explicit about the rounding for the riwt to usec computations by using DIV_ROUND_CLOSEST, which solves the off-by-one.
This does change the boundaries of accepted rx-usecs parameters, as the previously accepted values were in the 16-246 us range, and now fall into the 15-245 range on imx8mp.
--- drivers/net/ethernet/stmicro/stmmac/stmmac_ethtool.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/ethernet/stmicro/stmmac/stmmac_ethtool.c +2 -2
@@ -762,7 +762,7 @@ static u32 stmmac_usec2riwt(u32 usec, struct stmmac_priv *priv)/* Receive Interrupt Watchdog Timer (riwt) has a resolution of 256 * ticks. */ -return (usec * (clk /USEC_PER_SEC )) /256 ; +return DIV_ROUND_CLOSEST (usec * (clk /USEC_PER_SEC ),256 ); }static u32 stmmac_riwt2usec (u32 riwt,struct stmmac_priv *priv)
@@ -775,7 +775,7 @@ static u32 stmmac_riwt2usec(u32 riwt, struct stmmac_priv *priv)return 0 ; } -return (riwt *256 ) / (clk /USEC_PER_SEC ); +return DIV_ROUND_CLOSEST (riwt *256 , clk /USEC_PER_SEC ); } static int __stmmac_get_coalesce(struct net_device *dev,
Signature · 1 line
2.55.0
Re: [PATCH net-next 2/2] net: stmmac: ethtool: Address off-by-one when reading the coal rx-usecs
Quoted 3 lines
This does change the boundaries of accepted rx-usecs parameters, as the previously accepted values were in the 16-246 us range, and now fall into the 15-245 range on imx8mp.
With this change in place, what happens when trying to set 246? EINVAL?
Andrew
Maxime Chevallier
maintainerRe: [PATCH net-next 2/2] net: stmmac: ethtool: Address off-by-one when reading the coal rx-usecs
Hi Andrew,
Quoted 6 lines · Andrew Lunn
Quoted 3 lines
This does change the boundaries of accepted rx-usecs parameters, as the previously accepted values were in the 16-246 us range, and now fall into the 15-245 range on imx8mp.
With this change in place, what happens when trying to set 246? EINVAL?
Yes indeed :
# ethtool -C eth1 rx-usecs 246 netlink error: Invalid argument
Maxime
Andrew Lunn
Reviewed-byRe: [PATCH net-next 2/2] net: stmmac: ethtool: Address off-by-one when reading the coal rx-usecs
Quoted 7 lines · Maxime Chevallier
Hi Andrew,
Quoted 4 lines · Andrew Lunn
Quoted 3 lines
This does change the boundaries of accepted rx-usecs parameters, as the previously accepted values were in the 16-246 us range, and now fall into the 15-245 range on imx8mp.
Quoted 7 lines
Quoted 2 lines
With this change in place, what happens when trying to set 246? EINVAL?
Yes indeed :
# ethtool -C eth1 rx-usecs 246 netlink error: Invalid argument
I guess nobody will report a regression, but if they do, we can map 246 to 245. But until somebody does report it:
Andrew
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 11 lines · On Sun, 2 Aug 2026 13:40:12 +0200 you wrote:
Currently when configuring interrupt coalescing on devices that relies on the Receive Interrupt Watchdog Timer feature of dwmac, the computation of the RIWT timings leads to off-by-one values when reporting the timings back to userspace.
RIWT works by arming a watchdog timer upon receiving frames with the RI bit not set in the descriptor. The timer duration is expressed in units of 256 stmmac clock ticks, and therefore requires a bit of computation to derive it :
[...]
Here is the summary with links:
- [net-next,1/2] net: stmmac: ethtool: Comment the magic numbers in RIWT computation
https://git.kernel.org/netdev/net-next/c/b6a89c8ef34f - [net-next,2/2] net: stmmac: ethtool: Address off-by-one when reading the coal rx-usecs https://git.kernel.org/netdev/net-next/c/ae88f78bc4eb
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html