Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <linus.walleij@linaro.org>
Reviewed-by: Alvin Šipraga <alsi@bang-olufsen.dk>
Reviewed-by: Florian Fainelli <florian.fainelli@broadcom.com>
Signed-off-by: Paolo Abeni <pabeni@redhat.com>

authored by

Linus Walleij and committed by
Paolo Abeni
d577ca42 389119c8

+30 -18
+30 -18
drivers/net/dsa/realtek/rtl8366rb.c
··· 15 15 #include <linux/bitops.h> 16 16 #include <linux/etherdevice.h> 17 17 #include <linux/if_bridge.h> 18 + #include <linux/if_vlan.h> 18 19 #include <linux/interrupt.h> 19 20 #include <linux/irqdomain.h> 20 21 #include <linux/irqchip/chained_irq.h> ··· 930 929 if (ret) 931 930 return ret; 932 931 933 - /* Set maximum packet length to 1536 bytes */ 932 + /* Set default maximum packet length to 1536 bytes */ 934 933 ret = regmap_update_bits(priv->map, RTL8366RB_SGCR, 935 934 RTL8366RB_SGCR_MAX_LENGTH_MASK, 936 935 RTL8366RB_SGCR_MAX_LENGTH_1536); 937 936 if (ret) 938 937 return ret; 939 - for (i = 0; i < RTL8366RB_NUM_PORTS; i++) 940 - /* layer 2 size, see rtl8366rb_change_mtu() */ 941 - rb->max_mtu[i] = 1532; 938 + for (i = 0; i < RTL8366RB_NUM_PORTS; i++) { 939 + if (i == priv->cpu_port) 940 + /* CPU port need to also accept the tag */ 941 + rb->max_mtu[i] = ETH_DATA_LEN + RTL8366RB_CPU_TAG_SIZE; 942 + else 943 + rb->max_mtu[i] = ETH_DATA_LEN; 944 + } 942 945 943 946 /* Disable learning for all ports */ 944 947 ret = regmap_write(priv->map, RTL8366RB_PORT_LEARNDIS_CTRL, ··· 1447 1442 /* Roof out the MTU for the entire switch to the greatest 1448 1443 * common denominator: the biggest set for any one port will 1449 1444 * be the biggest MTU for the switch. 1450 - * 1451 - * The first setting, 1522 bytes, is max IP packet 1500 bytes, 1452 - * plus ethernet header, 1518 bytes, plus CPU tag, 4 bytes. 1453 - * This function should consider the parameter an SDU, so the 1454 - * MTU passed for this setting is 1518 bytes. The same logic 1455 - * of subtracting the DSA tag of 4 bytes apply to the other 1456 - * settings. 1457 1445 */ 1458 - max_mtu = 1518; 1446 + max_mtu = ETH_DATA_LEN; 1459 1447 for (i = 0; i < RTL8366RB_NUM_PORTS; i++) { 1460 1448 if (rb->max_mtu[i] > max_mtu) 1461 1449 max_mtu = rb->max_mtu[i]; 1462 1450 } 1463 - if (max_mtu <= 1518) 1451 + 1452 + /* Translate to layer 2 size. 1453 + * Add ethernet and (possible) VLAN headers, and checksum to the size. 1454 + * For ETH_DATA_LEN (1500 bytes) this will add up to 1522 bytes. 1455 + */ 1456 + max_mtu += VLAN_ETH_HLEN; 1457 + max_mtu += ETH_FCS_LEN; 1458 + 1459 + if (max_mtu <= 1522) 1464 1460 len = RTL8366RB_SGCR_MAX_LENGTH_1522; 1465 - else if (max_mtu > 1518 && max_mtu <= 1532) 1461 + else if (max_mtu > 1522 && max_mtu <= 1536) 1462 + /* This will be the most common default if using VLAN and 1463 + * CPU tagging on a port as both VLAN and CPU tag will 1464 + * result in 1518 + 4 + 4 = 1526 bytes. 1465 + */ 1466 1466 len = RTL8366RB_SGCR_MAX_LENGTH_1536; 1467 - else if (max_mtu > 1532 && max_mtu <= 1548) 1467 + else if (max_mtu > 1536 && max_mtu <= 1552) 1468 1468 len = RTL8366RB_SGCR_MAX_LENGTH_1552; 1469 1469 else 1470 1470 len = RTL8366RB_SGCR_MAX_LENGTH_16000; ··· 1481 1471 1482 1472 static int rtl8366rb_max_mtu(struct dsa_switch *ds, int port) 1483 1473 { 1484 - /* The max MTU is 16000 bytes, so we subtract the CPU tag 1485 - * and the max presented to the system is 15996 bytes. 1474 + /* The max MTU is 16000 bytes, so we subtract the ethernet 1475 + * headers with VLAN and checksum and arrive at 1476 + * 16000 - 18 - 4 = 15978. This does not include the CPU tag 1477 + * since that is added to the requested MTU by the DSA framework. 1486 1478 */ 1487 - return 15996; 1479 + return 16000 - VLAN_ETH_HLEN - ETH_FCS_LEN; 1488 1480 } 1489 1481 1490 1482 static int rtl8366rb_get_vlan_4k(struct realtek_priv *priv, u32 vid,