d1f82215caef480e639db99787d267a32a7669d5
[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 @@ -1960,23 +1961,27 @@ static int mtk_poll_rx(struct napi_struc
26 if (reason == MTK_PPE_CPU_REASON_HIT_UNBIND_RATE_REACHED)
27 mtk_ppe_check_skb(eth->ppe[0], skb, hash);
28
29 - if (netdev->features & NETIF_F_HW_VLAN_CTAG_RX) {
30 - if (MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2)) {
31 - if (trxd.rxd3 & RX_DMA_VTAG_V2)
32 - __vlan_hwaccel_put_tag(skb,
33 - htons(RX_DMA_VPID(trxd.rxd4)),
34 - RX_DMA_VID(trxd.rxd4));
35 - } else if (trxd.rxd2 & RX_DMA_VTAG) {
36 - __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
37 - RX_DMA_VID(trxd.rxd3));
38 - }
39 + if (MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2)) {
40 + if (trxd.rxd3 & RX_DMA_VTAG_V2)
41 + __vlan_hwaccel_put_tag(skb,
42 + htons(RX_DMA_VPID(trxd.rxd4)),
43 + RX_DMA_VID(trxd.rxd4));
44 + } else if (trxd.rxd2 & RX_DMA_VTAG) {
45 + __vlan_hwaccel_put_tag(skb, htons(RX_DMA_VPID(trxd.rxd3)),
46 + RX_DMA_VID(trxd.rxd3));
47 + }
48 +
49 + /* When using VLAN untagging in combination with DSA, the
50 + * hardware treats the MTK special tag as a VLAN and untags it.
51 + */
52 + if (skb_vlan_tag_present(skb) && netdev_uses_dsa(netdev)) {
53 + unsigned int port = ntohs(skb->vlan_proto) & GENMASK(2, 0);
54 +
55 + if (port < ARRAY_SIZE(eth->dsa_meta) &&
56 + eth->dsa_meta[port])
57 + skb_dst_set_noref(skb, &eth->dsa_meta[port]->dst);
58
59 - /* If the device is attached to a dsa switch, the special
60 - * tag inserted in VLAN field by hw switch can * be offloaded
61 - * by RX HW VLAN offload. Clear vlan info.
62 - */
63 - if (netdev_uses_dsa(netdev))
64 - __vlan_hwaccel_clear_tag(skb);
65 + __vlan_hwaccel_clear_tag(skb);
66 }
67
68 skb_record_rx_queue(skb, 0);
69 @@ -2793,15 +2798,19 @@ static netdev_features_t mtk_fix_feature
70
71 static int mtk_set_features(struct net_device *dev, netdev_features_t features)
72 {
73 - int err = 0;
74 -
75 - if (!((dev->features ^ features) & NETIF_F_LRO))
76 - return 0;
77 + struct mtk_mac *mac = netdev_priv(dev);
78 + struct mtk_eth *eth = mac->hw;
79 + netdev_features_t diff = dev->features ^ features;
80
81 - if (!(features & NETIF_F_LRO))
82 + if ((diff & NETIF_F_LRO) && !(features & NETIF_F_LRO))
83 mtk_hwlro_netdev_disable(dev);
84
85 - return err;
86 + /* Set RX VLAN offloading */
87 + if (diff & NETIF_F_HW_VLAN_CTAG_RX)
88 + mtk_w32(eth, !!(features & NETIF_F_HW_VLAN_CTAG_RX),
89 + MTK_CDMP_EG_CTRL);
90 +
91 + return 0;
92 }
93
94 /* wait for DMA to finish whatever it is doing before we start using it again */
95 @@ -3083,11 +3092,45 @@ found:
96 return NOTIFY_DONE;
97 }
98
99 +static bool mtk_uses_dsa(struct net_device *dev)
100 +{
101 +#if IS_ENABLED(CONFIG_NET_DSA)
102 + return netdev_uses_dsa(dev) &&
103 + dev->dsa_ptr->tag_ops->proto == DSA_TAG_PROTO_MTK;
104 +#else
105 + return false;
106 +#endif
107 +}
108 +
109 static int mtk_open(struct net_device *dev)
110 {
111 struct mtk_mac *mac = netdev_priv(dev);
112 struct mtk_eth *eth = mac->hw;
113 - int err;
114 + int i, err;
115 +
116 + if (mtk_uses_dsa(dev)) {
117 + for (i = 0; i < ARRAY_SIZE(eth->dsa_meta); i++) {
118 + struct metadata_dst *md_dst = eth->dsa_meta[i];
119 +
120 + if (md_dst)
121 + continue;
122 +
123 + md_dst = metadata_dst_alloc(0, METADATA_HW_PORT_MUX,
124 + GFP_KERNEL);
125 + if (!md_dst)
126 + return -ENOMEM;
127 +
128 + md_dst->u.port_info.port_id = i;
129 + eth->dsa_meta[i] = md_dst;
130 + }
131 + } else {
132 + /* Hardware special tag parsing needs to be disabled if at least
133 + * one MAC does not use DSA.
134 + */
135 + u32 val = mtk_r32(eth, MTK_CDMP_IG_CTRL);
136 + val &= ~MTK_CDMP_STAG_EN;
137 + mtk_w32(eth, val, MTK_CDMP_IG_CTRL);
138 + }
139
140 err = phylink_of_phy_connect(mac->phylink, mac->of_node, 0);
141 if (err) {
142 @@ -3417,6 +3460,10 @@ static int mtk_hw_init(struct mtk_eth *e
143 */
144 val = mtk_r32(eth, MTK_CDMQ_IG_CTRL);
145 mtk_w32(eth, val | MTK_CDMQ_STAG_EN, MTK_CDMQ_IG_CTRL);
146 + if (!MTK_HAS_CAPS(eth->soc->caps, MTK_NETSYS_V2)) {
147 + val = mtk_r32(eth, MTK_CDMP_IG_CTRL);
148 + mtk_w32(eth, val | MTK_CDMP_STAG_EN, MTK_CDMP_IG_CTRL);
149 + }
150
151 /* Enable RX VLan Offloading */
152 mtk_w32(eth, 1, MTK_CDMP_EG_CTRL);
153 @@ -3634,6 +3681,12 @@ static int mtk_free_dev(struct mtk_eth *
154 free_netdev(eth->netdev[i]);
155 }
156
157 + for (i = 0; i < ARRAY_SIZE(eth->dsa_meta); i++) {
158 + if (!eth->dsa_meta[i])
159 + break;
160 + metadata_dst_free(eth->dsa_meta[i]);
161 + }
162 +
163 return 0;
164 }
165
166 --- a/drivers/net/ethernet/mediatek/mtk_eth_soc.h
167 +++ b/drivers/net/ethernet/mediatek/mtk_eth_soc.h
168 @@ -22,6 +22,9 @@
169 #include <linux/bpf_trace.h>
170 #include "mtk_ppe.h"
171
172 +#define MTK_MAX_DSA_PORTS 7
173 +#define MTK_DSA_PORT_MASK GENMASK(2, 0)
174 +
175 #define MTK_QDMA_NUM_QUEUES 16
176 #define MTK_QDMA_PAGE_SIZE 2048
177 #define MTK_MAX_RX_LENGTH 1536
178 @@ -93,6 +96,9 @@
179 #define MTK_CDMQ_IG_CTRL 0x1400
180 #define MTK_CDMQ_STAG_EN BIT(0)
181
182 +/* CDMQ Exgress Control Register */
183 +#define MTK_CDMQ_EG_CTRL 0x1404
184 +
185 /* CDMP Ingress Control Register */
186 #define MTK_CDMP_IG_CTRL 0x400
187 #define MTK_CDMP_STAG_EN BIT(0)
188 @@ -1140,6 +1146,8 @@ struct mtk_eth {
189
190 int ip_align;
191
192 + struct metadata_dst *dsa_meta[MTK_MAX_DSA_PORTS];
193 +
194 struct mtk_ppe *ppe[2];
195 struct rhashtable flow_table;
196