kernel: fix vlan parsing issue in mediatek ethernet driver
[openwrt/staging/noltari.git] / target / linux / generic / pending-5.15 / 732-09-net-ethernet-mtk_eth_soc-fix-VLAN-rx-hardware-accele.patch
1 From: Felix Fietkau <nbd@nbd.name>
2 Date: Fri, 28 Oct 2022 11:01:12 +0200
3 Subject: [PATCH] net: ethernet: mtk_eth_soc: fix VLAN rx hardware
4 acceleration
5
6 - enable VLAN untagging for PDMA rx
7 - make it possible to disable the feature via ethtool
8 - pass VLAN tag to the DSA driver
9 - untag special tag on PDMA only if no non-DSA devices are in use
10 - disable special tag untagging on 7986 for now, since it's not working yet
11
12 Signed-off-by: Felix Fietkau <nbd@nbd.name>
13 ---
14
15 --- a/drivers/net/ethernet/mediatek/mtk_eth_soc.c
16 +++ b/drivers/net/ethernet/mediatek/mtk_eth_soc.c
17 @@ -23,6 +23,7 @@
18 #include <linux/jhash.h>
19 #include <linux/bitfield.h>
20 #include <net/dsa.h>
21 +#include <net/dst_metadata.h>
22
23 #include "mtk_eth_soc.h"
24 #include "mtk_wed.h"
25 @@ -1967,16 +1968,22 @@ static int mtk_poll_rx(struct napi_struc
26 htons(RX_DMA_VPID(trxd.rxd4)),
27 RX_DMA_VID(trxd.rxd4));
28 } else if (trxd.rxd2 & RX_DMA_VTAG) {
29 - __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
30 + __vlan_hwaccel_put_tag(skb, htons(RX_DMA_VPID(trxd.rxd3)),
31 RX_DMA_VID(trxd.rxd3));
32 }
33 + }
34 +
35 + /* When using VLAN untagging in combination with DSA, the
36 + * hardware treats the MTK special tag as a VLAN and untags it.
37 + */
38 + if (skb_vlan_tag_present(skb) && netdev_uses_dsa(netdev)) {
39 + unsigned int port = ntohs(skb->vlan_proto) & GENMASK(2, 0);
40
41 - /* If the device is attached to a dsa switch, the special
42 - * tag inserted in VLAN field by hw switch can * be offloaded
43 - * by RX HW VLAN offload. Clear vlan info.
44 - */
45 - if (netdev_uses_dsa(netdev))
46 - __vlan_hwaccel_clear_tag(skb);
47 + if (port < ARRAY_SIZE(eth->dsa_meta) &&
48 + eth->dsa_meta[port])
49 + skb_dst_set_noref(skb, &eth->dsa_meta[port]->dst);
50 +
51 + __vlan_hwaccel_clear_tag(skb);
52 }
53
54 skb_record_rx_queue(skb, 0);
55 @@ -2793,15 +2800,25 @@ static netdev_features_t mtk_fix_feature
56
57 static int mtk_set_features(struct net_device *dev, netdev_features_t features)
58 {
59 - int err = 0;
60 -
61 - if (!((dev->features ^ features) & NETIF_F_LRO))
62 - return 0;
63 + struct mtk_mac *mac = netdev_priv(dev);
64 + struct mtk_eth *eth = mac->hw;
65 + netdev_features_t diff = dev->features ^ features;
66 + int i;
67
68 - if (!(features & NETIF_F_LRO))
69 + if ((diff & NETIF_F_LRO) && !(features & NETIF_F_LRO))
70 mtk_hwlro_netdev_disable(dev);
71
72 - return err;
73 + /* Set RX VLAN offloading */
74 + if (diff & NETIF_F_HW_VLAN_CTAG_RX)
75 + mtk_w32(eth, !!(features & NETIF_F_HW_VLAN_CTAG_RX),
76 + MTK_CDMP_EG_CTRL);
77 +
78 + /* sync features with other MAC */
79 + for (i = 0; i < MTK_MAC_COUNT; i++)
80 + if (eth->netdev[i] && eth->netdev[i] != dev)
81 + eth->netdev[i]->features = features;
82 +
83 + return 0;
84 }
85
86 /* wait for DMA to finish whatever it is doing before we start using it again */
87 @@ -3083,11 +3100,45 @@ found:
88 return NOTIFY_DONE;
89 }
90
91 +static bool mtk_uses_dsa(struct net_device *dev)
92 +{
93 +#if IS_ENABLED(CONFIG_NET_DSA)
94 + return netdev_uses_dsa(dev) &&
95 + dev->dsa_ptr->tag_ops->proto == DSA_TAG_PROTO_MTK;
96 +#else
97 + return false;
98 +#endif
99 +}
100 +
101 static int mtk_open(struct net_device *dev)
102 {
103 struct mtk_mac *mac = netdev_priv(dev);
104 struct mtk_eth *eth = mac->hw;
105 - int err;
106 + int i, err;
107 +
108 + if (mtk_uses_dsa(dev)) {
109 + for (i = 0; i < ARRAY_SIZE(eth->dsa_meta); i++) {
110 + struct metadata_dst *md_dst = eth->dsa_meta[i];
111 +
112 + if (md_dst)
113 + continue;
114 +
115 + md_dst = metadata_dst_alloc(0, METADATA_HW_PORT_MUX,
116 + GFP_KERNEL);
117 + if (!md_dst)
118 + return -ENOMEM;
119 +
120 + md_dst->u.port_info.port_id = i;
121 + eth->dsa_meta[i] = md_dst;
122 + }
123 + } else {
124 + /* Hardware special tag parsing needs to be disabled if at least
125 + * one MAC does not use DSA.
126 + */
127 + u32 val = mtk_r32(eth, MTK_CDMP_IG_CTRL);
128 + val &= ~MTK_CDMP_STAG_EN;
129 + mtk_w32(eth, val, MTK_CDMP_IG_CTRL);
130 + }
131
132 err = phylink_of_phy_connect(mac->phylink, mac->of_node, 0);
133 if (err) {
134 @@ -3417,6 +3468,10 @@ static int mtk_hw_init(struct mtk_eth *e
135 */
136 val = mtk_r32(eth, MTK_CDMQ_IG_CTRL);
137 mtk_w32(eth, val | MTK_CDMQ_STAG_EN, MTK_CDMQ_IG_CTRL);
138 + if (!MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2)) {
139 + val = mtk_r32(eth, MTK_CDMP_IG_CTRL);
140 + mtk_w32(eth, val | MTK_CDMP_STAG_EN, MTK_CDMP_IG_CTRL);
141 + }
142
143 /* Enable RX VLan Offloading */
144 mtk_w32(eth, 1, MTK_CDMP_EG_CTRL);
145 @@ -3634,6 +3689,12 @@ static int mtk_free_dev(struct mtk_eth *
146 free_netdev(eth->netdev[i]);
147 }
148
149 + for (i = 0; i < ARRAY_SIZE(eth->dsa_meta); i++) {
150 + if (!eth->dsa_meta[i])
151 + break;
152 + metadata_dst_free(eth->dsa_meta[i]);
153 + }
154 +
155 return 0;
156 }
157
158 --- a/drivers/net/ethernet/mediatek/mtk_eth_soc.h
159 +++ b/drivers/net/ethernet/mediatek/mtk_eth_soc.h
160 @@ -22,6 +22,9 @@
161 #include <linux/bpf_trace.h>
162 #include "mtk_ppe.h"
163
164 +#define MTK_MAX_DSA_PORTS 7
165 +#define MTK_DSA_PORT_MASK GENMASK(2, 0)
166 +
167 #define MTK_QDMA_NUM_QUEUES 16
168 #define MTK_QDMA_PAGE_SIZE 2048
169 #define MTK_MAX_RX_LENGTH 1536
170 @@ -93,6 +96,9 @@
171 #define MTK_CDMQ_IG_CTRL 0x1400
172 #define MTK_CDMQ_STAG_EN BIT(0)
173
174 +/* CDMQ Exgress Control Register */
175 +#define MTK_CDMQ_EG_CTRL 0x1404
176 +
177 /* CDMP Ingress Control Register */
178 #define MTK_CDMP_IG_CTRL 0x400
179 #define MTK_CDMP_STAG_EN BIT(0)
180 @@ -1140,6 +1146,8 @@ struct mtk_eth {
181
182 int ip_align;
183
184 + struct metadata_dst *dsa_meta[MTK_MAX_DSA_PORTS];
185 +
186 struct mtk_ppe *ppe[2];
187 struct rhashtable flow_table;
188