Skip to content

Commit 323f990

Browse files
linuswNipaLocal
authored andcommitted
net: dsa: realtek: Rewrite RTL8366RB MTU handling
The MTU callbacks are in layer 1 size, so for example 1500 bytes is a normal setting. Cache this size, and only add the layer 2 framing right before choosing the setting. On the CPU port this will however include the DSA tag since this is transmitted from the parent ethernet interface! Add the layer 2 overhead such as ethernet and VLAN framing and FCS before selecting the size in the register. This will make the code easier to understand. The rtl8366rb_max_mtu() callback returns a bogus MTU just subtracting the CPU tag, which is the only thing we should NOT subtract. Return the correct layer 1 max MTU after removing headers and checksum. Signed-off-by: Linus Walleij <[email protected]> Reviewed-by: Alvin Šipraga <[email protected]> Reviewed-by: Florian Fainelli <[email protected]> Signed-off-by: NipaLocal <nipa@local>
1 parent 2507f04 commit 323f990

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

drivers/net/dsa/realtek/rtl8366rb.c

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <linux/bitops.h>
1616
#include <linux/etherdevice.h>
1717
#include <linux/if_bridge.h>
18+
#include <linux/if_vlan.h>
1819
#include <linux/interrupt.h>
1920
#include <linux/irqdomain.h>
2021
#include <linux/irqchip/chained_irq.h>
@@ -929,15 +930,19 @@ static int rtl8366rb_setup(struct dsa_switch *ds)
929930
if (ret)
930931
return ret;
931932

932-
/* Set maximum packet length to 1536 bytes */
933+
/* Set default maximum packet length to 1536 bytes */
933934
ret = regmap_update_bits(priv->map, RTL8366RB_SGCR,
934935
RTL8366RB_SGCR_MAX_LENGTH_MASK,
935936
RTL8366RB_SGCR_MAX_LENGTH_1536);
936937
if (ret)
937938
return ret;
938-
for (i = 0; i < RTL8366RB_NUM_PORTS; i++)
939-
/* layer 2 size, see rtl8366rb_change_mtu() */
940-
rb->max_mtu[i] = 1532;
939+
for (i = 0; i < RTL8366RB_NUM_PORTS; i++) {
940+
if (i == priv->cpu_port)
941+
/* CPU port need to also accept the tag */
942+
rb->max_mtu[i] = ETH_DATA_LEN + RTL8366RB_CPU_TAG_SIZE;
943+
else
944+
rb->max_mtu[i] = ETH_DATA_LEN;
945+
}
941946

942947
/* Disable learning for all ports */
943948
ret = regmap_write(priv->map, RTL8366RB_PORT_LEARNDIS_CTRL,
@@ -1442,24 +1447,29 @@ static int rtl8366rb_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
14421447
/* Roof out the MTU for the entire switch to the greatest
14431448
* common denominator: the biggest set for any one port will
14441449
* be the biggest MTU for the switch.
1445-
*
1446-
* The first setting, 1522 bytes, is max IP packet 1500 bytes,
1447-
* plus ethernet header, 1518 bytes, plus CPU tag, 4 bytes.
1448-
* This function should consider the parameter an SDU, so the
1449-
* MTU passed for this setting is 1518 bytes. The same logic
1450-
* of subtracting the DSA tag of 4 bytes apply to the other
1451-
* settings.
14521450
*/
1453-
max_mtu = 1518;
1451+
max_mtu = ETH_DATA_LEN;
14541452
for (i = 0; i < RTL8366RB_NUM_PORTS; i++) {
14551453
if (rb->max_mtu[i] > max_mtu)
14561454
max_mtu = rb->max_mtu[i];
14571455
}
1458-
if (max_mtu <= 1518)
1456+
1457+
/* Translate to layer 2 size.
1458+
* Add ethernet and (possible) VLAN headers, and checksum to the size.
1459+
* For ETH_DATA_LEN (1500 bytes) this will add up to 1522 bytes.
1460+
*/
1461+
max_mtu += VLAN_ETH_HLEN;
1462+
max_mtu += ETH_FCS_LEN;
1463+
1464+
if (max_mtu <= 1522)
14591465
len = RTL8366RB_SGCR_MAX_LENGTH_1522;
1460-
else if (max_mtu > 1518 && max_mtu <= 1532)
1466+
else if (max_mtu > 1522 && max_mtu <= 1536)
1467+
/* This will be the most common default if using VLAN and
1468+
* CPU tagging on a port as both VLAN and CPU tag will
1469+
* result in 1518 + 4 + 4 = 1526 bytes.
1470+
*/
14611471
len = RTL8366RB_SGCR_MAX_LENGTH_1536;
1462-
else if (max_mtu > 1532 && max_mtu <= 1548)
1472+
else if (max_mtu > 1536 && max_mtu <= 1552)
14631473
len = RTL8366RB_SGCR_MAX_LENGTH_1552;
14641474
else
14651475
len = RTL8366RB_SGCR_MAX_LENGTH_16000;
@@ -1471,10 +1481,12 @@ static int rtl8366rb_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
14711481

14721482
static int rtl8366rb_max_mtu(struct dsa_switch *ds, int port)
14731483
{
1474-
/* The max MTU is 16000 bytes, so we subtract the CPU tag
1475-
* and the max presented to the system is 15996 bytes.
1484+
/* The max MTU is 16000 bytes, so we subtract the ethernet
1485+
* headers with VLAN and checksum and arrive at
1486+
* 16000 - 18 - 4 = 15978. This does not include the CPU tag
1487+
* since that is added to the requested MTU by the DSA framework.
14761488
*/
1477-
return 15996;
1489+
return 16000 - VLAN_ETH_HLEN - ETH_FCS_LEN;
14781490
}
14791491

14801492
static int rtl8366rb_get_vlan_4k(struct realtek_priv *priv, u32 vid,

0 commit comments

Comments
 (0)