mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Paolo Abeni <pabeni@redhat.com>
To: Tristram.Ha@microchip.com,
	Arun Ramadoss <arun.ramadoss@microchip.com>,
	 Woojung Huh <woojung.huh@microchip.com>,
	Andrew Lunn <andrew@lunn.ch>,
	Vivien Didelot <vivien.didelot@gmail.com>,
	Florian Fainelli <f.fainelli@gmail.com>,
	 Vladimir Oltean <olteanv@gmail.com>
Cc: "David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	 Jakub Kicinski <kuba@kernel.org>,
	UNGLinuxDriver@microchip.com, netdev@vger.kernel.org,
	 linux-kernel@vger.kernel.org
Subject: Re: [PATCH net] net: dsa: microchip: fix KSZ9477 set_ageing_time function
Date: Thu, 30 May 2024 11:13:00 +0200	[thread overview]
Message-ID: <4a467adcdb3ca8e272bd3ae1be54272610aabc9b.camel@redhat.com> (raw)
In-Reply-To: <1716932192-3555-1-git-send-email-Tristram.Ha@microchip.com>

On Tue, 2024-05-28 at 14:36 -0700, Tristram.Ha@microchip.com wrote:
> From: Tristram Ha <tristram.ha@microchip.com>
> 
> The aging count is not a simple 11-bit value but comprises a 3-bit
> multiplier and an 8-bit second count.  The code tries to find a set of
> values with result close to the specifying value.
> 
> Note LAN937X has similar operation but provides an option to use
> millisecond instead of second so there will be a separate fix in the
> future.
> 
> Fixes: 2c119d9982b1 ("net: dsa: microchip: add the support for set_ageing_time")
> Signed-off-by: Tristram Ha <tristram.ha@microchip.com>
> ---
>  drivers/net/dsa/microchip/ksz9477.c     | 64 +++++++++++++++++++++----
>  drivers/net/dsa/microchip/ksz9477_reg.h |  1 -
>  2 files changed, 54 insertions(+), 11 deletions(-)
> 
> diff --git a/drivers/net/dsa/microchip/ksz9477.c b/drivers/net/dsa/microchip/ksz9477.c
> index f8ad7833f5d9..1af11aee3119 100644
> --- a/drivers/net/dsa/microchip/ksz9477.c
> +++ b/drivers/net/dsa/microchip/ksz9477.c
> @@ -1099,26 +1099,70 @@ void ksz9477_get_caps(struct ksz_device *dev, int port,
>  int ksz9477_set_ageing_time(struct ksz_device *dev, unsigned int msecs)
>  {
>  	u32 secs = msecs / 1000;
> +	u8 first, last, mult, i;
> +	int min, ret;
> +	int diff[8];
>  	u8 value;
>  	u8 data;
> -	int ret;
>  
> -	value = FIELD_GET(SW_AGE_PERIOD_7_0_M, secs);
> +	/* The aging timer comprises a 3-bit multiplier and an 8-bit second
> +	 * value.  Either of them cannot be zero.  The maximum timer is then
> +	 * 7 * 255 = 1785.
> +	 */
> +	if (!secs)
> +		secs = 1;
>  
> -	ret = ksz_write8(dev, REG_SW_LUE_CTRL_3, value);
> +	ret = ksz_read8(dev, REG_SW_LUE_CTRL_0, &value);
>  	if (ret < 0)
>  		return ret;
>  
> -	data = FIELD_GET(SW_AGE_PERIOD_10_8_M, secs);
> +	/* Check whether there is need to update the multiplier. */
> +	mult = FIELD_GET(SW_AGE_CNT_M, value);
> +	if (mult > 0) {
> +		/* Try to use the same multiplier already in the register. */
> +		min = secs / mult;
> +		if (min <= 0xff && min * mult == secs)
> +			return ksz_write8(dev, REG_SW_LUE_CTRL_3, min);
> +	}
>  
> -	ret = ksz_read8(dev, REG_SW_LUE_CTRL_0, &value);
> -	if (ret < 0)
> -		return ret;
> +	/* Return error if too large. */
> +	if (secs > 7 * 0xff)
> +		return -EINVAL;
> +
> +	/* Find out which combination of multiplier * value results in a timer
> +	 * value close to the specified timer value.
> +	 */
> +	first = (secs + 0xfe) / 0xff;
> +	for (i = first; i <= 7; i++) {
> +		min = secs / i;
> +		diff[i] = secs - i * min;
> +		if (!diff[i]) {
> +			i++;
> +			break;
> +		}
> +	}
> +
> +	last = i;
> +	min = 0xff;
> +	for (i = last - 1; i >= first; i--) {
> +		if (diff[i] < min) {
> +			data = i;
> +			min = diff[i];
> +		}
> +		if (!min)
> +			break;
> +	}

Is the additional accuracy worthy the added complexity WRT:

	mult = DIV_ROUND_UP(secs, 0xff);

?

Paolo


  reply	other threads:[~2024-05-30  9:13 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-28 21:36 Tristram.Ha
2024-05-30  9:13 ` Paolo Abeni [this message]
2024-05-30 23:06   ` Tristram.Ha
2024-05-31  0:21     ` Andrew Lunn

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4a467adcdb3ca8e272bd3ae1be54272610aabc9b.camel@redhat.com \
    --to=pabeni@redhat.com \
    --cc=Tristram.Ha@microchip.com \
    --cc=UNGLinuxDriver@microchip.com \
    --cc=andrew@lunn.ch \
    --cc=arun.ramadoss@microchip.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=f.fainelli@gmail.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=olteanv@gmail.com \
    --cc=vivien.didelot@gmail.com \
    --cc=woojung.huh@microchip.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®