7e2b300429e0f484a156a95f42f0006596e8eea7
[openwrt/openwrt.git] / target / linux / generic / backport-5.10 / 762-v5.11-net-dsa-mt7530-support-setting-MTU.patch
1 From 9470174e7581e75a8ebd78964997314dfc2e706c Mon Sep 17 00:00:00 2001
2 From: DENG Qingfang <dqfext@gmail.com>
3 Date: Tue, 3 Nov 2020 13:06:18 +0800
4 Subject: [PATCH] net: dsa: mt7530: support setting MTU
5
6 MT7530/7531 has a global RX packet length register, which can be used
7 to set MTU.
8
9 Supported packet length values are 1522 (1518 if untagged), 1536,
10 1552, and multiple of 1024 (from 2048 to 15360).
11
12 Signed-off-by: DENG Qingfang <dqfext@gmail.com>
13 Link: https://lore.kernel.org/r/20201103050618.11419-1-dqfext@gmail.com
14 Signed-off-by: Jakub Kicinski <kuba@kernel.org>
15 ---
16 drivers/net/dsa/mt7530.c | 49 ++++++++++++++++++++++++++++++++++++++++
17 drivers/net/dsa/mt7530.h | 12 ++++++++++
18 2 files changed, 61 insertions(+)
19
20 --- a/drivers/net/dsa/mt7530.c
21 +++ b/drivers/net/dsa/mt7530.c
22 @@ -1015,6 +1015,53 @@ mt7530_port_disable(struct dsa_switch *d
23 mutex_unlock(&priv->reg_mutex);
24 }
25
26 +static int
27 +mt7530_port_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
28 +{
29 + struct mt7530_priv *priv = ds->priv;
30 + struct mii_bus *bus = priv->bus;
31 + int length;
32 + u32 val;
33 +
34 + /* When a new MTU is set, DSA always set the CPU port's MTU to the
35 + * largest MTU of the slave ports. Because the switch only has a global
36 + * RX length register, only allowing CPU port here is enough.
37 + */
38 + if (!dsa_is_cpu_port(ds, port))
39 + return 0;
40 +
41 + mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
42 +
43 + val = mt7530_mii_read(priv, MT7530_GMACCR);
44 + val &= ~MAX_RX_PKT_LEN_MASK;
45 +
46 + /* RX length also includes Ethernet header, MTK tag, and FCS length */
47 + length = new_mtu + ETH_HLEN + MTK_HDR_LEN + ETH_FCS_LEN;
48 + if (length <= 1522) {
49 + val |= MAX_RX_PKT_LEN_1522;
50 + } else if (length <= 1536) {
51 + val |= MAX_RX_PKT_LEN_1536;
52 + } else if (length <= 1552) {
53 + val |= MAX_RX_PKT_LEN_1552;
54 + } else {
55 + val &= ~MAX_RX_JUMBO_MASK;
56 + val |= MAX_RX_JUMBO(DIV_ROUND_UP(length, 1024));
57 + val |= MAX_RX_PKT_LEN_JUMBO;
58 + }
59 +
60 + mt7530_mii_write(priv, MT7530_GMACCR, val);
61 +
62 + mutex_unlock(&bus->mdio_lock);
63 +
64 + return 0;
65 +}
66 +
67 +static int
68 +mt7530_port_max_mtu(struct dsa_switch *ds, int port)
69 +{
70 + return MT7530_MAX_MTU;
71 +}
72 +
73 static void
74 mt7530_stp_state_set(struct dsa_switch *ds, int port, u8 state)
75 {
76 @@ -2653,6 +2700,8 @@ static const struct dsa_switch_ops mt753
77 .get_sset_count = mt7530_get_sset_count,
78 .port_enable = mt7530_port_enable,
79 .port_disable = mt7530_port_disable,
80 + .port_change_mtu = mt7530_port_change_mtu,
81 + .port_max_mtu = mt7530_port_max_mtu,
82 .port_stp_state_set = mt7530_stp_state_set,
83 .port_bridge_join = mt7530_port_bridge_join,
84 .port_bridge_leave = mt7530_port_bridge_leave,
85 --- a/drivers/net/dsa/mt7530.h
86 +++ b/drivers/net/dsa/mt7530.h
87 @@ -11,6 +11,9 @@
88 #define MT7530_NUM_FDB_RECORDS 2048
89 #define MT7530_ALL_MEMBERS 0xff
90
91 +#define MTK_HDR_LEN 4
92 +#define MT7530_MAX_MTU (15 * 1024 - ETH_HLEN - ETH_FCS_LEN - MTK_HDR_LEN)
93 +
94 enum mt753x_id {
95 ID_MT7530 = 0,
96 ID_MT7621 = 1,
97 @@ -301,6 +304,15 @@ enum mt7530_vlan_port_attr {
98 #define MT7531_DBG_CNT(x) (0x3018 + (x) * 0x100)
99 #define MT7531_DIS_CLR BIT(31)
100
101 +#define MT7530_GMACCR 0x30e0
102 +#define MAX_RX_JUMBO(x) ((x) << 2)
103 +#define MAX_RX_JUMBO_MASK GENMASK(5, 2)
104 +#define MAX_RX_PKT_LEN_MASK GENMASK(1, 0)
105 +#define MAX_RX_PKT_LEN_1522 0x0
106 +#define MAX_RX_PKT_LEN_1536 0x1
107 +#define MAX_RX_PKT_LEN_1552 0x2
108 +#define MAX_RX_PKT_LEN_JUMBO 0x3
109 +
110 /* Register for MIB */
111 #define MT7530_PORT_MIB_COUNTER(x) (0x4000 + (x) * 0x100)
112 #define MT7530_MIB_CCR 0x4fe0