All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH net-next] net: dsa: mt7530: support setting ageing time
@ 2020-12-08  7:00 ` DENG Qingfang
  0 siblings, 0 replies; 12+ messages in thread
From: DENG Qingfang @ 2020-12-08  7:00 UTC (permalink / raw)
  To: netdev, linux-mediatek, Sean Wang, Landen Chao, Andrew Lunn,
	Vivien Didelot, Florian Fainelli, Vladimir Oltean,
	David S . Miller, Jakub Kicinski, Matthias Brugger, Russell King
  Cc: Frank Wunderlich

MT7530 has a global address age control register, so use it to set
ageing time.

The applied timer is (AGE_CNT + 1) * (AGE_UNIT + 1) seconds

Signed-off-by: DENG Qingfang <dqfext@gmail.com>
---
 drivers/net/dsa/mt7530.c | 41 ++++++++++++++++++++++++++++++++++++++++
 drivers/net/dsa/mt7530.h | 13 +++++++++++++
 2 files changed, 54 insertions(+)

diff --git a/drivers/net/dsa/mt7530.c b/drivers/net/dsa/mt7530.c
index 6408402a44f5..99bf8fed6536 100644
--- a/drivers/net/dsa/mt7530.c
+++ b/drivers/net/dsa/mt7530.c
@@ -870,6 +870,46 @@ mt7530_get_sset_count(struct dsa_switch *ds, int port, int sset)
 	return ARRAY_SIZE(mt7530_mib);
 }
 
+static int
+mt7530_set_ageing_time(struct dsa_switch *ds, unsigned int msecs)
+{
+	struct mt7530_priv *priv = ds->priv;
+	unsigned int secs = msecs / 1000;
+	unsigned int tmp_age_count;
+	unsigned int error = -1;
+	unsigned int age_count;
+	unsigned int age_unit;
+
+	/* Applied timer is (AGE_CNT + 1) * (AGE_UNIT + 1) seconds */
+	if (secs < 1 || secs > (AGE_CNT_MAX + 1) * (AGE_UNIT_MAX + 1))
+		return -ERANGE;
+
+	/* iterate through all possible age_count to find the closest pair */
+	for (tmp_age_count = 0; tmp_age_count <= AGE_CNT_MAX; ++tmp_age_count) {
+		unsigned int tmp_age_unit = secs / (tmp_age_count + 1) - 1;
+
+		if (tmp_age_unit <= AGE_UNIT_MAX) {
+			unsigned int tmp_error = secs -
+				(tmp_age_count + 1) * (tmp_age_unit + 1);
+
+			/* found a closer pair */
+			if (error > tmp_error) {
+				error = tmp_error;
+				age_count = tmp_age_count;
+				age_unit = tmp_age_unit;
+			}
+
+			/* found the exact match, so break the loop */
+			if (!error)
+				break;
+		}
+	}
+
+	mt7530_write(priv, MT7530_AAC, AGE_CNT(age_count) | AGE_UNIT(age_unit));
+
+	return 0;
+}
+
 static void mt7530_setup_port5(struct dsa_switch *ds, phy_interface_t interface)
 {
 	struct mt7530_priv *priv = ds->priv;
@@ -2564,6 +2604,7 @@ static const struct dsa_switch_ops mt7530_switch_ops = {
 	.phy_write		= mt753x_phy_write,
 	.get_ethtool_stats	= mt7530_get_ethtool_stats,
 	.get_sset_count		= mt7530_get_sset_count,
+	.set_ageing_time	= mt7530_set_ageing_time,
 	.port_enable		= mt7530_port_enable,
 	.port_disable		= mt7530_port_disable,
 	.port_change_mtu	= mt7530_port_change_mtu,
diff --git a/drivers/net/dsa/mt7530.h b/drivers/net/dsa/mt7530.h
index ee3523a7537e..32d8969b3ace 100644
--- a/drivers/net/dsa/mt7530.h
+++ b/drivers/net/dsa/mt7530.h
@@ -161,6 +161,19 @@ enum mt7530_vlan_egress_attr {
 	MT7530_VLAN_EGRESS_STACK = 3,
 };
 
+/* Register for address age control */
+#define MT7530_AAC			0xa0
+/* Disable ageing */
+#define  AGE_DIS			BIT(20)
+/* Age count */
+#define  AGE_CNT_MASK			GENMASK(19, 12)
+#define  AGE_CNT_MAX			0xff
+#define  AGE_CNT(x)			(AGE_CNT_MASK & ((x) << 12))
+/* Age unit */
+#define  AGE_UNIT_MASK			GENMASK(11, 0)
+#define  AGE_UNIT_MAX			0xfff
+#define  AGE_UNIT(x)			(AGE_UNIT_MASK & (x))
+
 /* Register for port STP state control */
 #define MT7530_SSP_P(x)			(0x2000 + ((x) * 0x100))
 #define  FID_PST(x)			((x) & 0x3)
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH net-next] net: dsa: mt7530: support setting ageing time
@ 2020-12-08  7:00 ` DENG Qingfang
  0 siblings, 0 replies; 12+ messages in thread
From: DENG Qingfang @ 2020-12-08  7:00 UTC (permalink / raw)
  To: netdev, linux-mediatek, Sean Wang, Landen Chao, Andrew Lunn,
	Vivien Didelot, Florian Fainelli, Vladimir Oltean,
	David S . Miller, Jakub Kicinski, Matthias Brugger, Russell King
  Cc: Frank Wunderlich

MT7530 has a global address age control register, so use it to set
ageing time.

The applied timer is (AGE_CNT + 1) * (AGE_UNIT + 1) seconds

Signed-off-by: DENG Qingfang <dqfext@gmail.com>
---
 drivers/net/dsa/mt7530.c | 41 ++++++++++++++++++++++++++++++++++++++++
 drivers/net/dsa/mt7530.h | 13 +++++++++++++
 2 files changed, 54 insertions(+)

diff --git a/drivers/net/dsa/mt7530.c b/drivers/net/dsa/mt7530.c
index 6408402a44f5..99bf8fed6536 100644
--- a/drivers/net/dsa/mt7530.c
+++ b/drivers/net/dsa/mt7530.c
@@ -870,6 +870,46 @@ mt7530_get_sset_count(struct dsa_switch *ds, int port, int sset)
 	return ARRAY_SIZE(mt7530_mib);
 }
 
+static int
+mt7530_set_ageing_time(struct dsa_switch *ds, unsigned int msecs)
+{
+	struct mt7530_priv *priv = ds->priv;
+	unsigned int secs = msecs / 1000;
+	unsigned int tmp_age_count;
+	unsigned int error = -1;
+	unsigned int age_count;
+	unsigned int age_unit;
+
+	/* Applied timer is (AGE_CNT + 1) * (AGE_UNIT + 1) seconds */
+	if (secs < 1 || secs > (AGE_CNT_MAX + 1) * (AGE_UNIT_MAX + 1))
+		return -ERANGE;
+
+	/* iterate through all possible age_count to find the closest pair */
+	for (tmp_age_count = 0; tmp_age_count <= AGE_CNT_MAX; ++tmp_age_count) {
+		unsigned int tmp_age_unit = secs / (tmp_age_count + 1) - 1;
+
+		if (tmp_age_unit <= AGE_UNIT_MAX) {
+			unsigned int tmp_error = secs -
+				(tmp_age_count + 1) * (tmp_age_unit + 1);
+
+			/* found a closer pair */
+			if (error > tmp_error) {
+				error = tmp_error;
+				age_count = tmp_age_count;
+				age_unit = tmp_age_unit;
+			}
+
+			/* found the exact match, so break the loop */
+			if (!error)
+				break;
+		}
+	}
+
+	mt7530_write(priv, MT7530_AAC, AGE_CNT(age_count) | AGE_UNIT(age_unit));
+
+	return 0;
+}
+
 static void mt7530_setup_port5(struct dsa_switch *ds, phy_interface_t interface)
 {
 	struct mt7530_priv *priv = ds->priv;
@@ -2564,6 +2604,7 @@ static const struct dsa_switch_ops mt7530_switch_ops = {
 	.phy_write		= mt753x_phy_write,
 	.get_ethtool_stats	= mt7530_get_ethtool_stats,
 	.get_sset_count		= mt7530_get_sset_count,
+	.set_ageing_time	= mt7530_set_ageing_time,
 	.port_enable		= mt7530_port_enable,
 	.port_disable		= mt7530_port_disable,
 	.port_change_mtu	= mt7530_port_change_mtu,
diff --git a/drivers/net/dsa/mt7530.h b/drivers/net/dsa/mt7530.h
index ee3523a7537e..32d8969b3ace 100644
--- a/drivers/net/dsa/mt7530.h
+++ b/drivers/net/dsa/mt7530.h
@@ -161,6 +161,19 @@ enum mt7530_vlan_egress_attr {
 	MT7530_VLAN_EGRESS_STACK = 3,
 };
 
+/* Register for address age control */
+#define MT7530_AAC			0xa0
+/* Disable ageing */
+#define  AGE_DIS			BIT(20)
+/* Age count */
+#define  AGE_CNT_MASK			GENMASK(19, 12)
+#define  AGE_CNT_MAX			0xff
+#define  AGE_CNT(x)			(AGE_CNT_MASK & ((x) << 12))
+/* Age unit */
+#define  AGE_UNIT_MASK			GENMASK(11, 0)
+#define  AGE_UNIT_MAX			0xfff
+#define  AGE_UNIT(x)			(AGE_UNIT_MASK & (x))
+
 /* Register for port STP state control */
 #define MT7530_SSP_P(x)			(0x2000 + ((x) * 0x100))
 #define  FID_PST(x)			((x) & 0x3)
-- 
2.25.1


_______________________________________________
Linux-mediatek mailing list
Linux-mediatek@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-mediatek

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH net-next] net: dsa: mt7530: support setting ageing time
  2020-12-08  7:00 ` DENG Qingfang
@ 2020-12-08 14:17   ` Andrew Lunn
  -1 siblings, 0 replies; 12+ messages in thread
From: Andrew Lunn @ 2020-12-08 14:17 UTC (permalink / raw)
  To: DENG Qingfang
  Cc: netdev, linux-mediatek, Sean Wang, Landen Chao, Vivien Didelot,
	Florian Fainelli, Vladimir Oltean, David S . Miller,
	Jakub Kicinski, Matthias Brugger, Russell King, Frank Wunderlich

On Tue, Dec 08, 2020 at 03:00:28PM +0800, DENG Qingfang wrote:
> MT7530 has a global address age control register, so use it to set
> ageing time.
> 
> The applied timer is (AGE_CNT + 1) * (AGE_UNIT + 1) seconds
> 
> Signed-off-by: DENG Qingfang <dqfext@gmail.com>

Reviewed-by: Andrew Lunn <andrew@lunn.ch>

    Andrew

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH net-next] net: dsa: mt7530: support setting ageing time
@ 2020-12-08 14:17   ` Andrew Lunn
  0 siblings, 0 replies; 12+ messages in thread
From: Andrew Lunn @ 2020-12-08 14:17 UTC (permalink / raw)
  To: DENG Qingfang
  Cc: Landen Chao, Florian Fainelli, Frank Wunderlich, netdev,
	Sean Wang, Russell King, David S . Miller, linux-mediatek,
	Matthias Brugger, Jakub Kicinski, Vladimir Oltean,
	Vivien Didelot

On Tue, Dec 08, 2020 at 03:00:28PM +0800, DENG Qingfang wrote:
> MT7530 has a global address age control register, so use it to set
> ageing time.
> 
> The applied timer is (AGE_CNT + 1) * (AGE_UNIT + 1) seconds
> 
> Signed-off-by: DENG Qingfang <dqfext@gmail.com>

Reviewed-by: Andrew Lunn <andrew@lunn.ch>

    Andrew

_______________________________________________
Linux-mediatek mailing list
Linux-mediatek@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-mediatek

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH net-next] net: dsa: mt7530: support setting ageing time
  2020-12-08  7:00 ` DENG Qingfang
@ 2020-12-08 20:56   ` Vladimir Oltean
  -1 siblings, 0 replies; 12+ messages in thread
From: Vladimir Oltean @ 2020-12-08 20:56 UTC (permalink / raw)
  To: DENG Qingfang
  Cc: netdev, linux-mediatek, Sean Wang, Landen Chao, Andrew Lunn,
	Vivien Didelot, Florian Fainelli, David S . Miller,
	Jakub Kicinski, Matthias Brugger, Russell King, Frank Wunderlich

On Tue, Dec 08, 2020 at 03:00:28PM +0800, DENG Qingfang wrote:
> MT7530 has a global address age control register, so use it to set
> ageing time.
> 
> The applied timer is (AGE_CNT + 1) * (AGE_UNIT + 1) seconds
> 
> Signed-off-by: DENG Qingfang <dqfext@gmail.com>
> ---
>  drivers/net/dsa/mt7530.c | 41 ++++++++++++++++++++++++++++++++++++++++
>  drivers/net/dsa/mt7530.h | 13 +++++++++++++
>  2 files changed, 54 insertions(+)
> 
> diff --git a/drivers/net/dsa/mt7530.c b/drivers/net/dsa/mt7530.c
> index 6408402a44f5..99bf8fed6536 100644
> --- a/drivers/net/dsa/mt7530.c
> +++ b/drivers/net/dsa/mt7530.c
> @@ -870,6 +870,46 @@ mt7530_get_sset_count(struct dsa_switch *ds, int port, int sset)
>  	return ARRAY_SIZE(mt7530_mib);
>  }
>  
> +static int
> +mt7530_set_ageing_time(struct dsa_switch *ds, unsigned int msecs)
> +{
> +	struct mt7530_priv *priv = ds->priv;
> +	unsigned int secs = msecs / 1000;
> +	unsigned int tmp_age_count;
> +	unsigned int error = -1;
> +	unsigned int age_count;
> +	unsigned int age_unit;
> +
> +	/* Applied timer is (AGE_CNT + 1) * (AGE_UNIT + 1) seconds */
> +	if (secs < 1 || secs > (AGE_CNT_MAX + 1) * (AGE_UNIT_MAX + 1))
> +		return -ERANGE;
> +
> +	/* iterate through all possible age_count to find the closest pair */
> +	for (tmp_age_count = 0; tmp_age_count <= AGE_CNT_MAX; ++tmp_age_count) {
> +		unsigned int tmp_age_unit = secs / (tmp_age_count + 1) - 1;
> +
> +		if (tmp_age_unit <= AGE_UNIT_MAX) {
> +			unsigned int tmp_error = secs -
> +				(tmp_age_count + 1) * (tmp_age_unit + 1);
> +
> +			/* found a closer pair */
> +			if (error > tmp_error) {
> +				error = tmp_error;
> +				age_count = tmp_age_count;
> +				age_unit = tmp_age_unit;
> +			}

I feel that the error calculation is just snake oil. This would be enough:

		if (tmp_age_unit <= AGE_UNIT_MAX)
			break;

Explanation:

You are given a number X, and must find A and B such that the error
E = |(A x B) - X| should be minimum, with
1 <= A <= A_max
1 <= B <= B_max

It is logical to try with A=1 first. If X / A <= B_max, then of course,
B = X / 1, and the error E is 0.

If that doesn't work out, and B > B_max, then you go to A=2. That gives
you another B = X / 2, and the error E is 1. You check again if B <=
B_max. If it is, that's your answer. B = X / 2, with an error E of 1.

You get my point. Iterating ascendingly through A, and calculating B as
X / A, already gives you the smallest error as soon as it satisfies the
B <= B_max requirement.

> +
> +			/* found the exact match, so break the loop */
> +			if (!error)
> +				break;
> +		}
> +	}
> +
> +	mt7530_write(priv, MT7530_AAC, AGE_CNT(age_count) | AGE_UNIT(age_unit));
> +
> +	return 0;
> +}
> +
>  static void mt7530_setup_port5(struct dsa_switch *ds, phy_interface_t interface)
>  {
>  	struct mt7530_priv *priv = ds->priv;
> @@ -2564,6 +2604,7 @@ static const struct dsa_switch_ops mt7530_switch_ops = {
>  	.phy_write		= mt753x_phy_write,
>  	.get_ethtool_stats	= mt7530_get_ethtool_stats,
>  	.get_sset_count		= mt7530_get_sset_count,
> +	.set_ageing_time	= mt7530_set_ageing_time,
>  	.port_enable		= mt7530_port_enable,
>  	.port_disable		= mt7530_port_disable,
>  	.port_change_mtu	= mt7530_port_change_mtu,
> diff --git a/drivers/net/dsa/mt7530.h b/drivers/net/dsa/mt7530.h
> index ee3523a7537e..32d8969b3ace 100644
> --- a/drivers/net/dsa/mt7530.h
> +++ b/drivers/net/dsa/mt7530.h
> @@ -161,6 +161,19 @@ enum mt7530_vlan_egress_attr {
>  	MT7530_VLAN_EGRESS_STACK = 3,
>  };
>  
> +/* Register for address age control */
> +#define MT7530_AAC			0xa0
> +/* Disable ageing */
> +#define  AGE_DIS			BIT(20)
> +/* Age count */
> +#define  AGE_CNT_MASK			GENMASK(19, 12)
> +#define  AGE_CNT_MAX			0xff
> +#define  AGE_CNT(x)			(AGE_CNT_MASK & ((x) << 12))
> +/* Age unit */
> +#define  AGE_UNIT_MASK			GENMASK(11, 0)
> +#define  AGE_UNIT_MAX			0xfff
> +#define  AGE_UNIT(x)			(AGE_UNIT_MASK & (x))
> +
>  /* Register for port STP state control */
>  #define MT7530_SSP_P(x)			(0x2000 + ((x) * 0x100))
>  #define  FID_PST(x)			((x) & 0x3)
> -- 
> 2.25.1
> 

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH net-next] net: dsa: mt7530: support setting ageing time
@ 2020-12-08 20:56   ` Vladimir Oltean
  0 siblings, 0 replies; 12+ messages in thread
From: Vladimir Oltean @ 2020-12-08 20:56 UTC (permalink / raw)
  To: DENG Qingfang
  Cc: Andrew Lunn, Landen Chao, Florian Fainelli, Frank Wunderlich,
	netdev, Sean Wang, Russell King, David S . Miller,
	linux-mediatek, Matthias Brugger, Jakub Kicinski, Vivien Didelot

On Tue, Dec 08, 2020 at 03:00:28PM +0800, DENG Qingfang wrote:
> MT7530 has a global address age control register, so use it to set
> ageing time.
> 
> The applied timer is (AGE_CNT + 1) * (AGE_UNIT + 1) seconds
> 
> Signed-off-by: DENG Qingfang <dqfext@gmail.com>
> ---
>  drivers/net/dsa/mt7530.c | 41 ++++++++++++++++++++++++++++++++++++++++
>  drivers/net/dsa/mt7530.h | 13 +++++++++++++
>  2 files changed, 54 insertions(+)
> 
> diff --git a/drivers/net/dsa/mt7530.c b/drivers/net/dsa/mt7530.c
> index 6408402a44f5..99bf8fed6536 100644
> --- a/drivers/net/dsa/mt7530.c
> +++ b/drivers/net/dsa/mt7530.c
> @@ -870,6 +870,46 @@ mt7530_get_sset_count(struct dsa_switch *ds, int port, int sset)
>  	return ARRAY_SIZE(mt7530_mib);
>  }
>  
> +static int
> +mt7530_set_ageing_time(struct dsa_switch *ds, unsigned int msecs)
> +{
> +	struct mt7530_priv *priv = ds->priv;
> +	unsigned int secs = msecs / 1000;
> +	unsigned int tmp_age_count;
> +	unsigned int error = -1;
> +	unsigned int age_count;
> +	unsigned int age_unit;
> +
> +	/* Applied timer is (AGE_CNT + 1) * (AGE_UNIT + 1) seconds */
> +	if (secs < 1 || secs > (AGE_CNT_MAX + 1) * (AGE_UNIT_MAX + 1))
> +		return -ERANGE;
> +
> +	/* iterate through all possible age_count to find the closest pair */
> +	for (tmp_age_count = 0; tmp_age_count <= AGE_CNT_MAX; ++tmp_age_count) {
> +		unsigned int tmp_age_unit = secs / (tmp_age_count + 1) - 1;
> +
> +		if (tmp_age_unit <= AGE_UNIT_MAX) {
> +			unsigned int tmp_error = secs -
> +				(tmp_age_count + 1) * (tmp_age_unit + 1);
> +
> +			/* found a closer pair */
> +			if (error > tmp_error) {
> +				error = tmp_error;
> +				age_count = tmp_age_count;
> +				age_unit = tmp_age_unit;
> +			}

I feel that the error calculation is just snake oil. This would be enough:

		if (tmp_age_unit <= AGE_UNIT_MAX)
			break;

Explanation:

You are given a number X, and must find A and B such that the error
E = |(A x B) - X| should be minimum, with
1 <= A <= A_max
1 <= B <= B_max

It is logical to try with A=1 first. If X / A <= B_max, then of course,
B = X / 1, and the error E is 0.

If that doesn't work out, and B > B_max, then you go to A=2. That gives
you another B = X / 2, and the error E is 1. You check again if B <=
B_max. If it is, that's your answer. B = X / 2, with an error E of 1.

You get my point. Iterating ascendingly through A, and calculating B as
X / A, already gives you the smallest error as soon as it satisfies the
B <= B_max requirement.

> +
> +			/* found the exact match, so break the loop */
> +			if (!error)
> +				break;
> +		}
> +	}
> +
> +	mt7530_write(priv, MT7530_AAC, AGE_CNT(age_count) | AGE_UNIT(age_unit));
> +
> +	return 0;
> +}
> +
>  static void mt7530_setup_port5(struct dsa_switch *ds, phy_interface_t interface)
>  {
>  	struct mt7530_priv *priv = ds->priv;
> @@ -2564,6 +2604,7 @@ static const struct dsa_switch_ops mt7530_switch_ops = {
>  	.phy_write		= mt753x_phy_write,
>  	.get_ethtool_stats	= mt7530_get_ethtool_stats,
>  	.get_sset_count		= mt7530_get_sset_count,
> +	.set_ageing_time	= mt7530_set_ageing_time,
>  	.port_enable		= mt7530_port_enable,
>  	.port_disable		= mt7530_port_disable,
>  	.port_change_mtu	= mt7530_port_change_mtu,
> diff --git a/drivers/net/dsa/mt7530.h b/drivers/net/dsa/mt7530.h
> index ee3523a7537e..32d8969b3ace 100644
> --- a/drivers/net/dsa/mt7530.h
> +++ b/drivers/net/dsa/mt7530.h
> @@ -161,6 +161,19 @@ enum mt7530_vlan_egress_attr {
>  	MT7530_VLAN_EGRESS_STACK = 3,
>  };
>  
> +/* Register for address age control */
> +#define MT7530_AAC			0xa0
> +/* Disable ageing */
> +#define  AGE_DIS			BIT(20)
> +/* Age count */
> +#define  AGE_CNT_MASK			GENMASK(19, 12)
> +#define  AGE_CNT_MAX			0xff
> +#define  AGE_CNT(x)			(AGE_CNT_MASK & ((x) << 12))
> +/* Age unit */
> +#define  AGE_UNIT_MASK			GENMASK(11, 0)
> +#define  AGE_UNIT_MAX			0xfff
> +#define  AGE_UNIT(x)			(AGE_UNIT_MASK & (x))
> +
>  /* Register for port STP state control */
>  #define MT7530_SSP_P(x)			(0x2000 + ((x) * 0x100))
>  #define  FID_PST(x)			((x) & 0x3)
> -- 
> 2.25.1
> 

_______________________________________________
Linux-mediatek mailing list
Linux-mediatek@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-mediatek

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH net-next] net: dsa: mt7530: support setting ageing time
  2020-12-08 20:56   ` Vladimir Oltean
@ 2020-12-08 21:19     ` Vladimir Oltean
  -1 siblings, 0 replies; 12+ messages in thread
From: Vladimir Oltean @ 2020-12-08 21:19 UTC (permalink / raw)
  To: DENG Qingfang
  Cc: netdev, linux-mediatek, Sean Wang, Landen Chao, Andrew Lunn,
	Vivien Didelot, Florian Fainelli, David S . Miller,
	Jakub Kicinski, Matthias Brugger, Russell King, Frank Wunderlich

On Tue, Dec 08, 2020 at 10:56:21PM +0200, Vladimir Oltean wrote:
> On Tue, Dec 08, 2020 at 03:00:28PM +0800, DENG Qingfang wrote:
> > MT7530 has a global address age control register, so use it to set
> > ageing time.
> > 
> > The applied timer is (AGE_CNT + 1) * (AGE_UNIT + 1) seconds
> > 
> > Signed-off-by: DENG Qingfang <dqfext@gmail.com>
> > ---
> >  drivers/net/dsa/mt7530.c | 41 ++++++++++++++++++++++++++++++++++++++++
> >  drivers/net/dsa/mt7530.h | 13 +++++++++++++
> >  2 files changed, 54 insertions(+)
> > 
> > diff --git a/drivers/net/dsa/mt7530.c b/drivers/net/dsa/mt7530.c
> > index 6408402a44f5..99bf8fed6536 100644
> > --- a/drivers/net/dsa/mt7530.c
> > +++ b/drivers/net/dsa/mt7530.c
> > @@ -870,6 +870,46 @@ mt7530_get_sset_count(struct dsa_switch *ds, int port, int sset)
> >  	return ARRAY_SIZE(mt7530_mib);
> >  }
> >  
> > +static int
> > +mt7530_set_ageing_time(struct dsa_switch *ds, unsigned int msecs)
> > +{
> > +	struct mt7530_priv *priv = ds->priv;
> > +	unsigned int secs = msecs / 1000;
> > +	unsigned int tmp_age_count;
> > +	unsigned int error = -1;
> > +	unsigned int age_count;
> > +	unsigned int age_unit;
> > +
> > +	/* Applied timer is (AGE_CNT + 1) * (AGE_UNIT + 1) seconds */
> > +	if (secs < 1 || secs > (AGE_CNT_MAX + 1) * (AGE_UNIT_MAX + 1))
> > +		return -ERANGE;
> > +
> > +	/* iterate through all possible age_count to find the closest pair */
> > +	for (tmp_age_count = 0; tmp_age_count <= AGE_CNT_MAX; ++tmp_age_count) {
> > +		unsigned int tmp_age_unit = secs / (tmp_age_count + 1) - 1;
> > +
> > +		if (tmp_age_unit <= AGE_UNIT_MAX) {
> > +			unsigned int tmp_error = secs -
> > +				(tmp_age_count + 1) * (tmp_age_unit + 1);
> > +
> > +			/* found a closer pair */
> > +			if (error > tmp_error) {
> > +				error = tmp_error;
> > +				age_count = tmp_age_count;
> > +				age_unit = tmp_age_unit;
> > +			}
> 
> I feel that the error calculation is just snake oil. This would be enough:
> 
> 		if (tmp_age_unit <= AGE_UNIT_MAX)
> 			break;
> 
> Explanation:
> 
> You are given a number X, and must find A and B such that the error
> E = |(A x B) - X| should be minimum, with
> 1 <= A <= A_max
> 1 <= B <= B_max
> 
> It is logical to try with A=1 first. If X / A <= B_max, then of course,
> B = X / 1, and the error E is 0.
> 
> If that doesn't work out, and B > B_max, then you go to A=2. That gives
> you another B = X / 2, and the error E is 1. You check again if B <=
> B_max. If it is, that's your answer. B = X / 2, with an error E of 1.
> 
> You get my point. Iterating ascendingly through A, and calculating B as
> X / A, already gives you the smallest error as soon as it satisfies the
> B <= B_max requirement.
> 
> > +
> > +			/* found the exact match, so break the loop */
> > +			if (!error)
> > +				break;
> > +		}
> > +	}
> > +
> > +	mt7530_write(priv, MT7530_AAC, AGE_CNT(age_count) | AGE_UNIT(age_unit));
> > +
> > +	return 0;
> > +}

Thinking more about it, it's not snake oil but is actually correct.
Where I was wrong was the division giving you a certain error, but it
actually only gives a maximum error. Other, larger, divisors can still
give you an error of 0.
If the number you're searching for, X, is, say, a multiple of 3 but not
of 2, and is:
B_max * 1 < X < B_max * 2
then exiting the loop at A=2 would give you an error E=1. But if you
continued the loop, then A=3 would have yielded an error E=0, because X
is a multiple of 3 but not of 2. I should probably go and refresh my
basic arithmetic.

Reviewed-by: Vladimir Oltean <olteanv@gmail.com>

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH net-next] net: dsa: mt7530: support setting ageing time
@ 2020-12-08 21:19     ` Vladimir Oltean
  0 siblings, 0 replies; 12+ messages in thread
From: Vladimir Oltean @ 2020-12-08 21:19 UTC (permalink / raw)
  To: DENG Qingfang
  Cc: Andrew Lunn, Landen Chao, Florian Fainelli, Frank Wunderlich,
	netdev, Sean Wang, Russell King, David S . Miller,
	linux-mediatek, Matthias Brugger, Jakub Kicinski, Vivien Didelot

On Tue, Dec 08, 2020 at 10:56:21PM +0200, Vladimir Oltean wrote:
> On Tue, Dec 08, 2020 at 03:00:28PM +0800, DENG Qingfang wrote:
> > MT7530 has a global address age control register, so use it to set
> > ageing time.
> > 
> > The applied timer is (AGE_CNT + 1) * (AGE_UNIT + 1) seconds
> > 
> > Signed-off-by: DENG Qingfang <dqfext@gmail.com>
> > ---
> >  drivers/net/dsa/mt7530.c | 41 ++++++++++++++++++++++++++++++++++++++++
> >  drivers/net/dsa/mt7530.h | 13 +++++++++++++
> >  2 files changed, 54 insertions(+)
> > 
> > diff --git a/drivers/net/dsa/mt7530.c b/drivers/net/dsa/mt7530.c
> > index 6408402a44f5..99bf8fed6536 100644
> > --- a/drivers/net/dsa/mt7530.c
> > +++ b/drivers/net/dsa/mt7530.c
> > @@ -870,6 +870,46 @@ mt7530_get_sset_count(struct dsa_switch *ds, int port, int sset)
> >  	return ARRAY_SIZE(mt7530_mib);
> >  }
> >  
> > +static int
> > +mt7530_set_ageing_time(struct dsa_switch *ds, unsigned int msecs)
> > +{
> > +	struct mt7530_priv *priv = ds->priv;
> > +	unsigned int secs = msecs / 1000;
> > +	unsigned int tmp_age_count;
> > +	unsigned int error = -1;
> > +	unsigned int age_count;
> > +	unsigned int age_unit;
> > +
> > +	/* Applied timer is (AGE_CNT + 1) * (AGE_UNIT + 1) seconds */
> > +	if (secs < 1 || secs > (AGE_CNT_MAX + 1) * (AGE_UNIT_MAX + 1))
> > +		return -ERANGE;
> > +
> > +	/* iterate through all possible age_count to find the closest pair */
> > +	for (tmp_age_count = 0; tmp_age_count <= AGE_CNT_MAX; ++tmp_age_count) {
> > +		unsigned int tmp_age_unit = secs / (tmp_age_count + 1) - 1;
> > +
> > +		if (tmp_age_unit <= AGE_UNIT_MAX) {
> > +			unsigned int tmp_error = secs -
> > +				(tmp_age_count + 1) * (tmp_age_unit + 1);
> > +
> > +			/* found a closer pair */
> > +			if (error > tmp_error) {
> > +				error = tmp_error;
> > +				age_count = tmp_age_count;
> > +				age_unit = tmp_age_unit;
> > +			}
> 
> I feel that the error calculation is just snake oil. This would be enough:
> 
> 		if (tmp_age_unit <= AGE_UNIT_MAX)
> 			break;
> 
> Explanation:
> 
> You are given a number X, and must find A and B such that the error
> E = |(A x B) - X| should be minimum, with
> 1 <= A <= A_max
> 1 <= B <= B_max
> 
> It is logical to try with A=1 first. If X / A <= B_max, then of course,
> B = X / 1, and the error E is 0.
> 
> If that doesn't work out, and B > B_max, then you go to A=2. That gives
> you another B = X / 2, and the error E is 1. You check again if B <=
> B_max. If it is, that's your answer. B = X / 2, with an error E of 1.
> 
> You get my point. Iterating ascendingly through A, and calculating B as
> X / A, already gives you the smallest error as soon as it satisfies the
> B <= B_max requirement.
> 
> > +
> > +			/* found the exact match, so break the loop */
> > +			if (!error)
> > +				break;
> > +		}
> > +	}
> > +
> > +	mt7530_write(priv, MT7530_AAC, AGE_CNT(age_count) | AGE_UNIT(age_unit));
> > +
> > +	return 0;
> > +}

Thinking more about it, it's not snake oil but is actually correct.
Where I was wrong was the division giving you a certain error, but it
actually only gives a maximum error. Other, larger, divisors can still
give you an error of 0.
If the number you're searching for, X, is, say, a multiple of 3 but not
of 2, and is:
B_max * 1 < X < B_max * 2
then exiting the loop at A=2 would give you an error E=1. But if you
continued the loop, then A=3 would have yielded an error E=0, because X
is a multiple of 3 but not of 2. I should probably go and refresh my
basic arithmetic.

Reviewed-by: Vladimir Oltean <olteanv@gmail.com>

_______________________________________________
Linux-mediatek mailing list
Linux-mediatek@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-mediatek

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH net-next] net: dsa: mt7530: support setting ageing time
  2020-12-08  7:00 ` DENG Qingfang
@ 2020-12-08 22:01   ` Florian Fainelli
  -1 siblings, 0 replies; 12+ messages in thread
From: Florian Fainelli @ 2020-12-08 22:01 UTC (permalink / raw)
  To: DENG Qingfang, netdev, linux-mediatek, Sean Wang, Landen Chao,
	Andrew Lunn, Vivien Didelot, Vladimir Oltean, David S . Miller,
	Jakub Kicinski, Matthias Brugger, Russell King
  Cc: Frank Wunderlich

On 12/7/20 11:00 PM, DENG Qingfang wrote:
> MT7530 has a global address age control register, so use it to set
> ageing time.
> 
> The applied timer is (AGE_CNT + 1) * (AGE_UNIT + 1) seconds
> 
> Signed-off-by: DENG Qingfang <dqfext@gmail.com>

Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
-- 
Florian

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH net-next] net: dsa: mt7530: support setting ageing time
@ 2020-12-08 22:01   ` Florian Fainelli
  0 siblings, 0 replies; 12+ messages in thread
From: Florian Fainelli @ 2020-12-08 22:01 UTC (permalink / raw)
  To: DENG Qingfang, netdev, linux-mediatek, Sean Wang, Landen Chao,
	Andrew Lunn, Vivien Didelot, Vladimir Oltean, David S . Miller,
	Jakub Kicinski, Matthias Brugger, Russell King
  Cc: Frank Wunderlich

On 12/7/20 11:00 PM, DENG Qingfang wrote:
> MT7530 has a global address age control register, so use it to set
> ageing time.
> 
> The applied timer is (AGE_CNT + 1) * (AGE_UNIT + 1) seconds
> 
> Signed-off-by: DENG Qingfang <dqfext@gmail.com>

Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
-- 
Florian

_______________________________________________
Linux-mediatek mailing list
Linux-mediatek@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-mediatek

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH net-next] net: dsa: mt7530: support setting ageing time
  2020-12-08  7:00 ` DENG Qingfang
@ 2020-12-09  0:19   ` David Miller
  -1 siblings, 0 replies; 12+ messages in thread
From: David Miller @ 2020-12-09  0:19 UTC (permalink / raw)
  To: dqfext
  Cc: netdev, linux-mediatek, sean.wang, Landen.Chao, andrew,
	vivien.didelot, f.fainelli, olteanv, kuba, matthias.bgg, linux,
	frank-w

From: DENG Qingfang <dqfext@gmail.com>
Date: Tue,  8 Dec 2020 15:00:28 +0800

> MT7530 has a global address age control register, so use it to set
> ageing time.
> 
> The applied timer is (AGE_CNT + 1) * (AGE_UNIT + 1) seconds
> 
> Signed-off-by: DENG Qingfang <dqfext@gmail.com>

Applied, thanks

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH net-next] net: dsa: mt7530: support setting ageing time
@ 2020-12-09  0:19   ` David Miller
  0 siblings, 0 replies; 12+ messages in thread
From: David Miller @ 2020-12-09  0:19 UTC (permalink / raw)
  To: dqfext
  Cc: andrew, Landen.Chao, f.fainelli, frank-w, netdev, sean.wang,
	linux, linux-mediatek, matthias.bgg, kuba, olteanv,
	vivien.didelot

From: DENG Qingfang <dqfext@gmail.com>
Date: Tue,  8 Dec 2020 15:00:28 +0800

> MT7530 has a global address age control register, so use it to set
> ageing time.
> 
> The applied timer is (AGE_CNT + 1) * (AGE_UNIT + 1) seconds
> 
> Signed-off-by: DENG Qingfang <dqfext@gmail.com>

Applied, thanks

_______________________________________________
Linux-mediatek mailing list
Linux-mediatek@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-mediatek

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2020-12-09  0:29 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-08  7:00 [PATCH net-next] net: dsa: mt7530: support setting ageing time DENG Qingfang
2020-12-08  7:00 ` DENG Qingfang
2020-12-08 14:17 ` Andrew Lunn
2020-12-08 14:17   ` Andrew Lunn
2020-12-08 20:56 ` Vladimir Oltean
2020-12-08 20:56   ` Vladimir Oltean
2020-12-08 21:19   ` Vladimir Oltean
2020-12-08 21:19     ` Vladimir Oltean
2020-12-08 22:01 ` Florian Fainelli
2020-12-08 22:01   ` Florian Fainelli
2020-12-09  0:19 ` David Miller
2020-12-09  0:19   ` David Miller

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.