2022-02-21 09:49:26

by Oleksij Rempel

[permalink] [raw]
Subject: [PATCH net-next v1 1/1] net: dsa: microchip: ksz9477: implement MTU configuration

This chips supports two ways to configure max MTU size:
- by setting SW_LEGAL_PACKET_DISABLE bit: if this bit is 0 allowed packed size
will be between 64 and bytes 1518. If this bit is 1, it will accept
packets up to 2000 bytes.
- by setting SW_JUMBO_PACKET bit. If this bit is set, the chip will
ignore SW_LEGAL_PACKET_DISABLE value and use REG_SW_MTU__2 register to
configure MTU size.

Current driver has disabled SW_JUMBO_PACKET bit and activates
SW_LEGAL_PACKET_DISABLE. So the switch will pass all packets up to 2000 without
any way to configure it.

By providing port_change_mtu we are switch to SW_JUMBO_PACKET way and will
be able to configure MTU up to ~9000.

Signed-off-by: Oleksij Rempel <[email protected]>
---
drivers/net/dsa/microchip/ksz9477.c | 42 +++++++++++++++++++++++--
drivers/net/dsa/microchip/ksz9477_reg.h | 4 +++
drivers/net/dsa/microchip/ksz_common.h | 1 +
3 files changed, 45 insertions(+), 2 deletions(-)

diff --git a/drivers/net/dsa/microchip/ksz9477.c b/drivers/net/dsa/microchip/ksz9477.c
index 18ffc8ded7ee..ae5ce7e71d4c 100644
--- a/drivers/net/dsa/microchip/ksz9477.c
+++ b/drivers/net/dsa/microchip/ksz9477.c
@@ -11,6 +11,7 @@
#include <linux/platform_data/microchip-ksz.h>
#include <linux/phy.h>
#include <linux/if_bridge.h>
+#include <linux/if_vlan.h>
#include <net/dsa.h>
#include <net/switchdev.h>

@@ -182,6 +183,35 @@ static void ksz9477_port_cfg32(struct ksz_device *dev, int port, int offset,
bits, set ? bits : 0);
}

+static int ksz9477_change_mtu(struct dsa_switch *ds, int port, int mtu)
+{
+ struct ksz_device *dev = ds->priv;
+ u16 new_mtu, max_mtu = 0;
+ int i;
+
+ new_mtu = mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN;
+
+ if (dsa_is_cpu_port(ds, port))
+ new_mtu += KSZ9477_INGRESS_TAG_LEN;
+
+ /* Cache the per-port MTU setting */
+ dev->ports[port].max_mtu = new_mtu;
+
+ for (i = 0; i < dev->port_cnt; i++) {
+ if (dev->ports[i].max_mtu > max_mtu)
+ max_mtu = dev->ports[i].max_mtu;
+ }
+
+ return regmap_update_bits(dev->regmap[1], REG_SW_MTU__2,
+ REG_SW_MTU_MASK, max_mtu);
+}
+
+static int ksz9477_max_mtu(struct dsa_switch *ds, int port)
+{
+ return KSZ9477_MAX_MTU - ETH_HLEN - ETH_FCS_LEN - VLAN_HLEN -
+ KSZ9477_INGRESS_TAG_LEN;
+}
+
static int ksz9477_wait_vlan_ctrl_ready(struct ksz_device *dev)
{
unsigned int val;
@@ -1412,8 +1442,14 @@ static int ksz9477_setup(struct dsa_switch *ds)
/* Do not work correctly with tail tagging. */
ksz_cfg(dev, REG_SW_MAC_CTRL_0, SW_CHECK_LENGTH, false);

- /* accept packet up to 2000bytes */
- ksz_cfg(dev, REG_SW_MAC_CTRL_1, SW_LEGAL_PACKET_DISABLE, true);
+ /* Enable REG_SW_MTU__2 reg by setting SW_JUMBO_PACKET */
+ ksz_cfg(dev, REG_SW_MAC_CTRL_1, SW_JUMBO_PACKET, true);
+
+ /* Now we can configure default MTU value */
+ ret = regmap_update_bits(dev->regmap[1], REG_SW_MTU__2, REG_SW_MTU_MASK,
+ VLAN_ETH_FRAME_LEN + ETH_FCS_LEN);
+ if (ret)
+ return ret;

ksz9477_config_cpu_port(ds);

@@ -1460,6 +1496,8 @@ static const struct dsa_switch_ops ksz9477_switch_ops = {
.port_mirror_add = ksz9477_port_mirror_add,
.port_mirror_del = ksz9477_port_mirror_del,
.get_stats64 = ksz9477_get_stats64,
+ .port_change_mtu = ksz9477_change_mtu,
+ .port_max_mtu = ksz9477_max_mtu,
};

static u32 ksz9477_get_port_addr(int port, int offset)
diff --git a/drivers/net/dsa/microchip/ksz9477_reg.h b/drivers/net/dsa/microchip/ksz9477_reg.h
index 16939f29faa5..34c368d3676c 100644
--- a/drivers/net/dsa/microchip/ksz9477_reg.h
+++ b/drivers/net/dsa/microchip/ksz9477_reg.h
@@ -176,6 +176,7 @@
#define REG_SW_MAC_ADDR_5 0x0307

#define REG_SW_MTU__2 0x0308
+#define REG_SW_MTU_MASK GENMASK(13, 0)

#define REG_SW_ISP_TPID__2 0x030A

@@ -1662,4 +1663,7 @@
/* 148,800 frames * 67 ms / 100 */
#define BROADCAST_STORM_VALUE 9969

+#define KSZ9477_INGRESS_TAG_LEN 2
+#define KSZ9477_MAX_MTU 9000
+
#endif /* KSZ9477_REGS_H */
diff --git a/drivers/net/dsa/microchip/ksz_common.h b/drivers/net/dsa/microchip/ksz_common.h
index c6fa487fb006..edf4a442de57 100644
--- a/drivers/net/dsa/microchip/ksz_common.h
+++ b/drivers/net/dsa/microchip/ksz_common.h
@@ -41,6 +41,7 @@ struct ksz_port {

struct ksz_port_mib mib;
phy_interface_t interface;
+ unsigned int max_mtu;
};

struct ksz_device {
--
2.30.2


2022-02-23 12:19:15

by Jakub Kicinski

[permalink] [raw]
Subject: Re: [PATCH net-next v1 1/1] net: dsa: microchip: ksz9477: implement MTU configuration

On Mon, 21 Feb 2022 09:43:28 +0100 Oleksij Rempel wrote:
> This chips supports two ways to configure max MTU size:
> - by setting SW_LEGAL_PACKET_DISABLE bit: if this bit is 0 allowed packed size
> will be between 64 and bytes 1518. If this bit is 1, it will accept
> packets up to 2000 bytes.
> - by setting SW_JUMBO_PACKET bit. If this bit is set, the chip will
> ignore SW_LEGAL_PACKET_DISABLE value and use REG_SW_MTU__2 register to
> configure MTU size.
>
> Current driver has disabled SW_JUMBO_PACKET bit and activates
> SW_LEGAL_PACKET_DISABLE. So the switch will pass all packets up to 2000 without
> any way to configure it.
>
> By providing port_change_mtu we are switch to SW_JUMBO_PACKET way and will
> be able to configure MTU up to ~9000.

And it has no negative side affects to always have jumbo enabled?
Maybe the internal buffer will be carved up in a different way?

> +static int ksz9477_change_mtu(struct dsa_switch *ds, int port, int mtu)
> +{
> + struct ksz_device *dev = ds->priv;
> + u16 new_mtu, max_mtu = 0;
> + int i;
> +
> + new_mtu = mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN;
> +
> + if (dsa_is_cpu_port(ds, port))
> + new_mtu += KSZ9477_INGRESS_TAG_LEN;
> +
> + /* Cache the per-port MTU setting */
> + dev->ports[port].max_mtu = new_mtu;
> +
> + for (i = 0; i < dev->port_cnt; i++) {
> + if (dev->ports[i].max_mtu > max_mtu)
> + max_mtu = dev->ports[i].max_mtu;
> + }

nit:

for (...)
max_mtu = max(max_mtu, dev->ports[i].max_mtu)

> @@ -41,6 +41,7 @@ struct ksz_port {
>
> struct ksz_port_mib mib;
> phy_interface_t interface;
> + unsigned int max_mtu;
> };

max_mtu already has two meanings in this patch, let's call this
max_frame or max_len etc, instead of adding a third meaning.

2022-02-23 13:40:20

by Oleksij Rempel

[permalink] [raw]
Subject: Re: [PATCH net-next v1 1/1] net: dsa: microchip: ksz9477: implement MTU configuration

On Tue, Feb 22, 2022 at 04:52:17PM -0800, Jakub Kicinski wrote:
> On Mon, 21 Feb 2022 09:43:28 +0100 Oleksij Rempel wrote:
> > This chips supports two ways to configure max MTU size:
> > - by setting SW_LEGAL_PACKET_DISABLE bit: if this bit is 0 allowed packed size
> > will be between 64 and bytes 1518. If this bit is 1, it will accept
> > packets up to 2000 bytes.
> > - by setting SW_JUMBO_PACKET bit. If this bit is set, the chip will
> > ignore SW_LEGAL_PACKET_DISABLE value and use REG_SW_MTU__2 register to
> > configure MTU size.
> >
> > Current driver has disabled SW_JUMBO_PACKET bit and activates
> > SW_LEGAL_PACKET_DISABLE. So the switch will pass all packets up to 2000 without
> > any way to configure it.
> >
> > By providing port_change_mtu we are switch to SW_JUMBO_PACKET way and will
> > be able to configure MTU up to ~9000.
>
> And it has no negative side affects to always have jumbo enabled?
> Maybe the internal buffer will be carved up in a different way?

Hm I tested it with iperf3 on 4 of 7 ports on this switch without
noticeable changes before and after this patch. My setup looks as
following:

----`
| /- iperf -s
= port0
| \- iperf3 -c 172.17.0.11 -t 100
|
|
= port1 ... same as port0, ... -c 172.17.0.10
= port2 ... same as port0, ... -c 172.17.0.13
= port3 ... same as port0, ... -c 172.17.0.12

Each port was sending and receiving. Each instance reported 938 Mbits/sec

> > +static int ksz9477_change_mtu(struct dsa_switch *ds, int port, int mtu)
> > +{
> > + struct ksz_device *dev = ds->priv;
> > + u16 new_mtu, max_mtu = 0;
> > + int i;
> > +
> > + new_mtu = mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN;
> > +
> > + if (dsa_is_cpu_port(ds, port))
> > + new_mtu += KSZ9477_INGRESS_TAG_LEN;
> > +
> > + /* Cache the per-port MTU setting */
> > + dev->ports[port].max_mtu = new_mtu;
> > +
> > + for (i = 0; i < dev->port_cnt; i++) {
> > + if (dev->ports[i].max_mtu > max_mtu)
> > + max_mtu = dev->ports[i].max_mtu;
> > + }
>
> nit:
>
> for (...)
> max_mtu = max(max_mtu, dev->ports[i].max_mtu)
>
> > @@ -41,6 +41,7 @@ struct ksz_port {
> >
> > struct ksz_port_mib mib;
> > phy_interface_t interface;
> > + unsigned int max_mtu;
> > };
>
> max_mtu already has two meanings in this patch, let's call this
> max_frame or max_len etc, instead of adding a third meaning.

Ok, thx. Will update the patch.

Regards,
Oleksij
--
Pengutronix e.K. | |
Steuerwalder Str. 21 | http://www.pengutronix.de/ |
31137 Hildesheim, Germany | Phone: +49-5121-206917-0 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |