blob: a4a6d9541f6b3c49d32f3854f462b93a29b43d83 [file] [log] [blame]
developere5e687d2023-08-08 16:05:33 +08001--- a/drivers/net/ethernet/mediatek/mtk_eth_soc.c
2+++ b/drivers/net/ethernet/mediatek/mtk_eth_soc.c
3@@ -245,6 +245,9 @@ static const char * const mtk_clks_sourc
4 "top_netsys_warp_sel",
5 };
6
7+struct net_device *(*mtk_get_tnl_dev)(int tnl_idx) = NULL;
8+EXPORT_SYMBOL(mtk_get_tnl_dev);
9+
10 void mtk_w32(struct mtk_eth *eth, u32 val, unsigned reg)
11 {
12 __raw_writel(val, eth->base + reg);
13@@ -2089,6 +2092,7 @@ static int mtk_poll_rx(struct napi_struc
14 u64 addr64 = 0;
15 u8 *data, *new_data;
16 struct mtk_rx_dma_v2 *rxd, trxd;
17+ int tnl_idx = 0;
18 int done = 0;
19
20 if (unlikely(!ring))
21@@ -2132,11 +2136,20 @@ static int mtk_poll_rx(struct napi_struc
22 0 : RX_DMA_GET_SPORT(trxd.rxd4) - 1;
23 }
24
25- if (unlikely(mac < 0 || mac >= MTK_MAC_COUNT ||
26- !eth->netdev[mac]))
27- goto release_desc;
28+ tnl_idx = RX_DMA_GET_TOPS_CRSN(trxd.rxd6);
29+ if (mtk_get_tnl_dev && tnl_idx) {
30+ netdev = mtk_get_tnl_dev(tnl_idx);
31+ if (unlikely(IS_ERR(netdev)))
32+ netdev = NULL;
33+ }
34
35- netdev = eth->netdev[mac];
36+ if (!netdev) {
37+ if (unlikely(mac < 0 || mac >= MTK_MAC_COUNT ||
38+ !eth->netdev[mac]))
39+ goto release_desc;
40+
41+ netdev = eth->netdev[mac];
42+ }
43
44 if (unlikely(test_bit(MTK_RESETTING, &eth->state)))
45 goto release_desc;
46@@ -2221,6 +2234,8 @@ static int mtk_poll_rx(struct napi_struc
47 skb_hnat_alg(skb) = 0;
48 skb_hnat_filled(skb) = 0;
49 skb_hnat_magic_tag(skb) = HNAT_MAGIC_TAG;
50+ skb_hnat_set_tops(skb, 0);
51+ skb_hnat_set_is_decap(skb, 0);
52
53 if (skb_hnat_reason(skb) == HIT_BIND_FORCE_TO_CPU) {
54 trace_printk("[%s] reason=0x%x(force to CPU) from WAN to Ext\n",
55--- a/drivers/net/ethernet/mediatek/mtk_hnat/hnat.c
56+++ b/drivers/net/ethernet/mediatek/mtk_hnat/hnat.c
57@@ -43,6 +43,12 @@ void (*ppe_dev_register_hook)(struct net
58 EXPORT_SYMBOL(ppe_dev_register_hook);
59 void (*ppe_dev_unregister_hook)(struct net_device *dev) = NULL;
60 EXPORT_SYMBOL(ppe_dev_unregister_hook);
61+int (*mtk_tnl_encap_offload)(struct sk_buff *skb) = NULL;
62+EXPORT_SYMBOL(mtk_tnl_encap_offload);
63+int (*mtk_tnl_decap_offload)(struct sk_buff *skb) = NULL;
64+EXPORT_SYMBOL(mtk_tnl_decap_offload);
65+bool (*mtk_tnl_decap_offloadable)(struct sk_buff *skb) = NULL;
66+EXPORT_SYMBOL(mtk_tnl_decap_offloadable);
67
68 static void hnat_sma_build_entry(struct timer_list *t)
69 {
70@@ -53,6 +59,16 @@ static void hnat_sma_build_entry(struct
71 SMA, SMA_FWD_CPU_BUILD_ENTRY);
72 }
73
74+struct foe_entry *hnat_get_foe_entry(u32 ppe_id, u32 index)
75+{
76+ if (index == 0x7fff || index >= hnat_priv->foe_etry_num
77+ || ppe_id >= CFG_PPE_NUM)
78+ return ERR_PTR(-EINVAL);
79+
80+ return &hnat_priv->foe_table_cpu[ppe_id][index];
81+}
82+EXPORT_SYMBOL(hnat_get_foe_entry);
83+
84 void hnat_cache_ebl(int enable)
85 {
86 int i;
87@@ -63,6 +79,7 @@ void hnat_cache_ebl(int enable)
88 cr_set_field(hnat_priv->ppe_base[i] + PPE_CAH_CTRL, CAH_EN, enable);
89 }
90 }
91+EXPORT_SYMBOL(hnat_cache_ebl);
92
93 static void hnat_reset_timestamp(struct timer_list *t)
94 {
95--- a/drivers/net/ethernet/mediatek/mtk_hnat/hnat.h
96+++ b/drivers/net/ethernet/mediatek/mtk_hnat/hnat.h
97@@ -1085,6 +1085,8 @@ enum FoeIpAct {
98 #define NR_WDMA0_PORT 8
99 #define NR_WDMA1_PORT 9
100 #define NR_GMAC3_PORT 15
101+#define NR_TDMA_TPORT 4
102+#define NR_TDMA_QDMA_TPORT 5
103 #define LAN_DEV_NAME hnat_priv->lan
104 #define LAN2_DEV_NAME hnat_priv->lan2
105 #define IS_WAN(dev) \
106@@ -1208,6 +1210,8 @@ static inline bool hnat_dsa_is_enable(st
107 }
108 #endif
109
110+struct foe_entry *hnat_get_foe_entry(u32 ppe_id, u32 index);
111+
112 void hnat_deinit_debugfs(struct mtk_hnat *h);
113 int hnat_init_debugfs(struct mtk_hnat *h);
114 int hnat_register_nf_hooks(void);
115@@ -1224,6 +1228,9 @@ extern int qos_ul_toggle;
116 extern int hook_toggle;
117 extern int mape_toggle;
118 extern int qos_toggle;
119+extern int (*mtk_tnl_encap_offload)(struct sk_buff *skb);
120+extern int (*mtk_tnl_decap_offload)(struct sk_buff *skb);
121+extern bool (*mtk_tnl_decap_offloadable)(struct sk_buff *skb);
122
123 int ext_if_add(struct extdev_entry *ext_entry);
124 int ext_if_del(struct extdev_entry *ext_entry);
125--- a/drivers/net/ethernet/mediatek/mtk_hnat/hnat_nf_hook.c
126+++ b/drivers/net/ethernet/mediatek/mtk_hnat/hnat_nf_hook.c
127@@ -726,10 +726,14 @@ static unsigned int is_ppe_support_type(
128 case ETH_P_IP:
129 iph = ip_hdr(skb);
130
131- /* do not accelerate non tcp/udp traffic */
132- if ((iph->protocol == IPPROTO_TCP) ||
133+ if (mtk_tnl_decap_offloadable && mtk_tnl_decap_offloadable(skb)) {
134+ /* tunnel protocol is offloadable */
135+ skb_hnat_set_is_decap(skb, 1);
136+ return 1;
137+ } else if ((iph->protocol == IPPROTO_TCP) ||
138 (iph->protocol == IPPROTO_UDP) ||
139 (iph->protocol == IPPROTO_IPV6)) {
140+ /* do not accelerate non tcp/udp traffic */
141 return 1;
142 }
143
144@@ -846,6 +850,13 @@ mtk_hnat_ipv4_nf_pre_routing(void *priv,
145
146 hnat_set_head_frags(state, skb, -1, hnat_set_iif);
147
148+ if (skb_hnat_tops(skb) && skb_hnat_is_decap(skb)
149+ && is_magic_tag_valid(skb)
150+ && skb_hnat_iface(skb) == FOE_MAGIC_GE_VIRTUAL
151+ && mtk_tnl_decap_offload && mtk_tnl_decap_offload(skb)) {
152+ return NF_ACCEPT;
153+ }
154+
155 /*
156 * Avoid mistakenly binding of outer IP, ports in SW L2TP decap flow.
157 * In pre-routing, if dev is virtual iface, TOPS module is not loaded,
158@@ -922,6 +933,13 @@ mtk_hnat_br_nf_local_in(void *priv, stru
159
160 hnat_set_head_frags(state, skb, -1, hnat_set_iif);
161
162+ if (skb_hnat_tops(skb) && skb_hnat_is_decap(skb)
163+ && is_magic_tag_valid(skb)
164+ && skb_hnat_iface(skb) == FOE_MAGIC_GE_VIRTUAL
165+ && mtk_tnl_decap_offload && mtk_tnl_decap_offload(skb)) {
166+ return NF_ACCEPT;
167+ }
168+
169 pre_routing_print(skb, state->in, state->out, __func__);
170
171 if (unlikely(debug_level >= 7)) {
172@@ -1074,9 +1092,22 @@ static unsigned int hnat_ipv4_get_nextho
173 return -1;
174 }
175
176+ /*
177+ * if this packet is a tunnel packet and is about to construct
178+ * outer header, we must update its outer mac header pointer
179+ * before filling outer mac or it may screw up inner mac
180+ */
181+ if (skb_hnat_tops(skb) && skb_hnat_is_encap(skb)) {
182+ skb_push(skb, sizeof(struct ethhdr));
183+ skb_reset_mac_header(skb);
184+ }
185+
186 memcpy(eth_hdr(skb)->h_dest, neigh->ha, ETH_ALEN);
187 memcpy(eth_hdr(skb)->h_source, out->dev_addr, ETH_ALEN);
188
189+ if (skb_hnat_tops(skb) && skb_hnat_is_encap(skb))
190+ skb_pull(skb, sizeof(struct ethhdr));
191+
192 rcu_read_unlock_bh();
193
194 return 0;
195@@ -1202,6 +1233,37 @@ static struct ethhdr *get_ipv6_ipip_ethh
196 return eth;
197 }
198
199+static inline void hnat_get_filled_unbind_entry(struct sk_buff *skb,
200+ struct foe_entry *entry)
201+{
202+ if (unlikely(!skb || !entry))
203+ return;
204+
205+ memcpy(entry,
206+ &hnat_priv->foe_table_cpu[skb_hnat_ppe(skb)][skb_hnat_entry(skb)],
207+ sizeof(*entry));
208+}
209+
210+static inline void hnat_fill_offload_engine_entry(struct sk_buff *skb,
211+ struct foe_entry *entry)
212+{
213+#if defined(CONFIG_MEDIATEK_NETSYS_V3)
214+ if (skb_hnat_tops(skb) && skb_hnat_is_encap(skb)) {
215+ /*
216+ * if skb_hnat_tops(skb) is setup for encapsulation,
217+ * we fill in hnat tport and tops_entry for tunnel encapsulation
218+ * offloading
219+ */
220+ entry->ipv4_hnapt.tport_id = NR_TDMA_QDMA_TPORT;
221+ entry->ipv4_hnapt.tops_entry = skb_hnat_tops(skb);
222+ } else {
223+ return;
224+ }
225+
226+ entry->ipv4_hnapt.iblk2.qid = 12; /* offload engine use QID 12 */
227+#endif /* defined(CONFIG_MEDIATEK_NETSYS_V3) */
228+}
229+
230 static unsigned int skb_to_hnat_info(struct sk_buff *skb,
231 const struct net_device *dev,
232 struct foe_entry *foe,
233@@ -1237,6 +1299,11 @@ static unsigned int skb_to_hnat_info(str
234 if (whnat && is_hnat_pre_filled(foe))
235 return 0;
236
237+ if (skb_hnat_tops(skb) && !(hw_path->flags & FLOW_OFFLOAD_PATH_TNL)) {
238+ hnat_get_filled_unbind_entry(skb, &entry);
239+ goto hnat_entry_bind;
240+ }
241+
242 entry.bfib1.pkt_type = foe->udib1.pkt_type; /* Get packte type state*/
243 entry.bfib1.state = foe->udib1.state;
244
245@@ -1247,6 +1314,7 @@ static unsigned int skb_to_hnat_info(str
246 switch (ntohs(eth->h_proto)) {
247 case ETH_P_IP:
248 iph = ip_hdr(skb);
249+
250 switch (iph->protocol) {
251 case IPPROTO_UDP:
252 udp = 1;
253@@ -1628,6 +1696,10 @@ static unsigned int skb_to_hnat_info(str
254 /* Fill Layer2 Info.*/
255 entry = ppe_fill_L2_info(eth, entry, hw_path);
256
257+ if (skb_hnat_tops(skb) && hw_path->flags & FLOW_OFFLOAD_PATH_TNL)
258+ goto hnat_entry_skip_bind;
259+
260+hnat_entry_bind:
261 /* Fill Info Blk*/
262 entry = ppe_fill_info_blk(eth, entry, hw_path);
263
264@@ -1806,7 +1878,20 @@ static unsigned int skb_to_hnat_info(str
265 entry.ipv6_5t_route.act_dp |= UDF_HNAT_PRE_FILLED;
266 }
267
268+#if defined(CONFIG_MEDIATEK_NETSYS_V3)
269+ hnat_fill_offload_engine_entry(skb, &entry);
270+#endif
271+
272+hnat_entry_skip_bind:
273 wmb();
274+
275+ /*
276+ * final check before we write BIND info.
277+ * If this entry is already bound, we should not modify it right now
278+ */
279+ if (entry_hnat_is_bound(foe))
280+ return 0;
281+
282 memcpy(foe, &entry, sizeof(entry));
283 /*reset statistic for this entry*/
284 if (hnat_priv->data->per_flow_accounting &&
285@@ -1859,6 +1944,7 @@ int mtk_sw_nat_hook_tx(struct sk_buff *s
286 return NF_ACCEPT;
287
288 eth = eth_hdr(skb);
289+
290 memcpy(&bfib1_tx, &entry->bfib1, sizeof(entry->bfib1));
291
292 /*not bind multicast if PPE mcast not enable*/
293@@ -1878,6 +1964,12 @@ int mtk_sw_nat_hook_tx(struct sk_buff *s
294 switch ((int)bfib1_tx.pkt_type) {
295 case IPV4_HNAPT:
296 case IPV4_HNAT:
297+ /*
298+ * skip if packet is an encap tnl packet or it may
299+ * screw up inner mac header
300+ */
301+ if (skb_hnat_tops(skb) && skb_hnat_is_encap(skb))
302+ break;
303 entry->ipv4_hnapt.smac_hi = swab32(*((u32 *)eth->h_source));
304 entry->ipv4_hnapt.smac_lo = swab16(*((u16 *)&eth->h_source[4]));
305 break;
306@@ -2037,6 +2129,10 @@ int mtk_sw_nat_hook_tx(struct sk_buff *s
307 entry->ipv6_5t_route.iblk2.dp = gmac_no;
308 }
309
310+#if defined(CONFIG_MEDIATEK_NETSYS_V3)
311+ hnat_fill_offload_engine_entry(skb, entry);
312+#endif
313+
314 bfib1_tx.ttl = 1;
315 bfib1_tx.state = BIND;
316 wmb();
317@@ -2058,6 +2154,7 @@ int mtk_sw_nat_hook_rx(struct sk_buff *s
318 }
319
320 skb_hnat_alg(skb) = 0;
321+ skb_hnat_set_tops(skb, 0);
322 skb_hnat_magic_tag(skb) = HNAT_MAGIC_TAG;
323
324 if (skb_hnat_iface(skb) == FOE_MAGIC_WED0)
325@@ -2504,6 +2601,7 @@ static unsigned int mtk_hnat_nf_post_rou
326 struct flow_offload_hw_path hw_path = { .dev = (struct net_device*)out,
327 .virt_dev = (struct net_device*)out };
328 const struct net_device *arp_dev = out;
329+ bool is_virt_dev = false;
330
331 if (xlat_toggle && !mtk_464xlat_post_process(skb, out))
332 return 0;
333@@ -2524,10 +2622,18 @@ static unsigned int mtk_hnat_nf_post_rou
334
335 if (out->netdev_ops->ndo_flow_offload_check) {
336 out->netdev_ops->ndo_flow_offload_check(&hw_path);
337+
338 out = (IS_GMAC1_MODE) ? hw_path.virt_dev : hw_path.dev;
339+ if (hw_path.flags & FLOW_OFFLOAD_PATH_TNL && mtk_tnl_encap_offload)
340+ skb_hnat_set_tops(skb, hw_path.tnl_type + 1);
341 }
342
343 if (!IS_LAN_GRP(out) && !IS_WAN(out) && !IS_EXT(out))
344+ is_virt_dev = true;
345+
346+ if (is_virt_dev
347+ && !(skb_hnat_tops(skb) && skb_hnat_is_encap(skb)
348+ && (hw_path.flags & FLOW_OFFLOAD_PATH_TNL)))
349 return 0;
350
351 trace_printk("[%s] case hit, %x-->%s, reason=%x\n", __func__,
352@@ -2547,9 +2653,18 @@ static unsigned int mtk_hnat_nf_post_rou
353 if (fn && !mtk_hnat_accel_type(skb))
354 break;
355
356- if (fn && fn(skb, arp_dev, &hw_path))
357+ if (!is_virt_dev && fn && fn(skb, arp_dev, &hw_path))
358 break;
359
360+ /* skb_hnat_tops(skb) is updated in mtk_tnl_offload() */
361+ if (skb_hnat_tops(skb)) {
362+ if (skb_hnat_is_encap(skb) && !is_virt_dev
363+ && mtk_tnl_encap_offload && mtk_tnl_encap_offload(skb))
364+ break;
365+ if (skb_hnat_is_decap(skb))
366+ break;
367+ }
368+
369 skb_to_hnat_info(skb, out, entry, &hw_path);
370 break;
371 case HIT_BIND_KEEPALIVE_DUP_OLD_HDR:
372@@ -2820,7 +2935,7 @@ mtk_hnat_ipv4_nf_local_out(void *priv, s
373 if (iph->protocol == IPPROTO_IPV6) {
374 entry->udib1.pkt_type = IPV6_6RD;
375 hnat_set_head_frags(state, skb, 0, hnat_set_alg);
376- } else {
377+ } else if (!skb_hnat_tops(skb)) {
378 hnat_set_head_frags(state, skb, 1, hnat_set_alg);
379 }
380
381--- a/drivers/net/ethernet/mediatek/mtk_hnat/nf_hnat_mtk.h
382+++ b/drivers/net/ethernet/mediatek/mtk_hnat/nf_hnat_mtk.h
383@@ -44,7 +44,9 @@ struct hnat_desc {
384 u32 is_sp : 1;
385 u32 hf : 1;
386 u32 amsdu : 1;
387- u32 resv3 : 19;
388+ u32 tops : 6;
389+ u32 is_decap : 1;
390+ u32 resv3 : 12;
391 u32 magic_tag_protect : 16;
392 } __packed;
393 #elif defined(CONFIG_MEDIATEK_NETSYS_RX_V2)
394@@ -91,6 +93,19 @@ struct hnat_desc {
395 ((((skb_headroom(skb) >= FOE_INFO_LEN) ? 1 : 0)))
396
397 #define skb_hnat_info(skb) ((struct hnat_desc *)(skb->head))
398+#if defined(CONFIG_MEDIATEK_NETSYS_V3)
399+#define skb_hnat_tops(skb) (((struct hnat_desc *)((skb)->head))->tops)
400+#define skb_hnat_is_decap(skb) (((struct hnat_desc *)((skb)->head))->is_decap)
401+#define skb_hnat_is_encap(skb) (!skb_hnat_is_decap(skb))
402+#define skb_hnat_set_tops(skb, tops) ((skb_hnat_tops(skb)) = (tops))
403+#define skb_hnat_set_is_decap(skb, is_decap) ((skb_hnat_is_decap(skb)) = (is_decap))
404+#else /* !defined(CONFIG_MEDIATEK_NETSYS_V3) */
405+#define skb_hnat_tops(skb) (0)
406+#define skb_hnat_is_decap(skb) (0)
407+#define skb_hnat_is_encap(skb) (0)
408+#define skb_hnat_set_tops(skb, tops)
409+#define skb_hnat_set_is_decap(skb, is_decap)
410+#endif /* defined(CONFIG_MEDIATEK_NETSYS_V3) */
411 #define skb_hnat_magic(skb) (((struct hnat_desc *)(skb->head))->magic)
412 #define skb_hnat_reason(skb) (((struct hnat_desc *)(skb->head))->crsn)
413 #define skb_hnat_entry(skb) (((struct hnat_desc *)(skb->head))->entry)
414--- a/include/net/netfilter/nf_flow_table.h
415+++ b/include/net/netfilter/nf_flow_table.h
416@@ -98,10 +98,22 @@ struct flow_offload {
417 #define FLOW_OFFLOAD_PATH_6RD BIT(5)
418 #define FLOW_OFFLOAD_PATH_TNL BIT(6)
419
420+enum flow_offload_tnl {
421+ FLOW_OFFLOAD_TNL_GRETAP,
422+ FLOW_OFFLOAD_TNL_PPTP,
423+ FLOW_OFFLOAD_TNL_IP_L2TP,
424+ FLOW_OFFLOAD_TNL_UDP_L2TP_CTRL,
425+ FLOW_OFFLOAD_TNL_UDP_L2TP_DATA,
426+ FLOW_OFFLOAD_VXLAN,
427+ FLOW_OFFLOAD_NATT,
428+ __FLOW_OFFLOAD_MAX,
429+};
430+
431 struct flow_offload_hw_path {
432 struct net_device *dev;
433 struct net_device *virt_dev;
434 u32 flags;
435+ u32 tnl_type;
436
437 u8 eth_src[ETH_ALEN];
438 u8 eth_dest[ETH_ALEN];
439--- a/drivers/net/ethernet/mediatek/mtk_eth_soc.h
440+++ b/drivers/net/ethernet/mediatek/mtk_eth_soc.h
441@@ -1868,6 +1868,9 @@ extern const struct of_device_id of_mtk_
442 extern u32 mtk_hwlro_stats_ebl;
443 extern u32 dbg_show_level;
444
445+/* tunnel offload related */
446+extern struct net_device *(*mtk_get_tnl_dev)(int tnl_idx);
447+
448 /* read the hardware status register */
449 void mtk_stats_update_mac(struct mtk_mac *mac);
450