aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/generic/pending-5.4/770-16-net-ethernet-mediatek-mtk_eth_soc-add-flow-offloadin.patch
blob: b0f06f603b36af86a921938ac807801de3b974c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
From: Felix Fietkau <nbd@nbd.name>
Date: Sun, 11 Oct 2020 22:28:32 +0200
Subject: [PATCH] net: ethernet: mediatek: mtk_eth_soc: add flow offloading
 support

Only supports IPv4 for now

Signed-off-by: Felix Fietkau <nbd@nbd.name>
---
 create mode 100644 drivers/net/ethernet/mediatek/mtk_offload.c
 create mode 100644 drivers/net/ethernet/mediatek/mtk_ppe_debugfs.c

--- a/drivers/net/ethernet/mediatek/Makefile
+++ b/drivers/net/ethernet/mediatek/Makefile
@@ -4,4 +4,4 @@
 #
 
 obj-$(CONFIG_NET_MEDIATEK_SOC)                 += mtk_eth.o
-mtk_eth-y := mtk_eth_soc.o mtk_sgmii.o mtk_eth_path.o mtk_ppe.o
+mtk_eth-y := mtk_eth_soc.o mtk_sgmii.o mtk_eth_path.o mtk_ppe.o mtk_ppe_debugfs.o mtk_offload.o
--- a/drivers/net/ethernet/mediatek/mtk_eth_soc.c
+++ b/drivers/net/ethernet/mediatek/mtk_eth_soc.c
@@ -20,6 +20,8 @@
 #include <linux/pinctrl/devinfo.h>
 #include <linux/phylink.h>
 #include <linux/jhash.h>
+#include <linux/netfilter.h>
+#include <net/netfilter/nf_flow_table.h>
 #include <net/dsa.h>
 
 #include "mtk_eth_soc.h"
@@ -1348,8 +1350,12 @@ static int mtk_poll_rx(struct napi_struc
 		    (trxd.rxd2 & RX_DMA_VTAG))
 			__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
 					       RX_DMA_VID(trxd.rxd3));
-		skb_record_rx_queue(skb, 0);
-		napi_gro_receive(napi, skb);
+		if (mtk_offload_check_rx(eth, skb, trxd.rxd4) == 0) {
+			skb_record_rx_queue(skb, 0);
+			napi_gro_receive(napi, skb);
+		} else {
+			dev_kfree_skb(skb);
+		}
 
 skip_rx:
 		ring->data[idx] = new_data;
@@ -2882,6 +2888,25 @@ static int mtk_set_rxnfc(struct net_devi
 	return ret;
 }
 
+static int
+mtk_flow_offload(enum flow_offload_type type, struct flow_offload *flow,
+		struct flow_offload_hw_path *src,
+		struct flow_offload_hw_path *dest)
+{
+	struct mtk_mac *mac = netdev_priv(src->dev);
+	struct mtk_eth *eth = mac->hw;
+
+	if (!eth->soc->offload_version)
+		return -EINVAL;
+
+	if (src->dev->base_addr != dest->dev->base_addr)
+		return -EINVAL;
+
+	mac = netdev_priv(src->dev);
+
+	return mtk_flow_offload_add(eth, type, flow, src, dest);
+}
+
 static const struct ethtool_ops mtk_ethtool_ops = {
 	.get_link_ksettings	= mtk_get_link_ksettings,
 	.set_link_ksettings	= mtk_set_link_ksettings,
@@ -2913,6 +2938,7 @@ static const struct net_device_ops mtk_n
 #ifdef CONFIG_NET_POLL_CONTROLLER
 	.ndo_poll_controller	= mtk_poll_controller,
 #endif
+	.ndo_flow_offload       = mtk_flow_offload,
 };
 
 static int mtk_add_mac(struct mtk_eth *eth, struct device_node *np)
@@ -3178,6 +3204,10 @@ static int mtk_probe(struct platform_dev
 				   eth->base + MTK_ETH_PPE_BASE, 2);
 		if (err)
 			goto err_free_dev;
+
+		err = mtk_flow_offload_init(eth);
+		if (err)
+			goto err_free_dev;
 	}
 
 	for (i = 0; i < MTK_MAX_DEVS; i++) {
--- a/drivers/net/ethernet/mediatek/mtk_eth_soc.h
+++ b/drivers/net/ethernet/mediatek/mtk_eth_soc.h
@@ -949,6 +949,7 @@ struct mtk_eth {
 	int				ip_align;
 
 	struct mtk_ppe			ppe;
+	struct flow_offload __rcu       **foe_flow_table;
 };
 
 /* struct mtk_mac -	the structure that holds the info about the MACs of the
@@ -993,4 +994,12 @@ int mtk_gmac_sgmii_path_setup(struct mtk
 int mtk_gmac_gephy_path_setup(struct mtk_eth *eth, int mac_id);
 int mtk_gmac_rgmii_path_setup(struct mtk_eth *eth, int mac_id);
 
+int mtk_flow_offload_init(struct mtk_eth *eth);
+int mtk_flow_offload_add(struct mtk_eth *eth,
+			 enum flow_offload_type type,
+			 struct flow_offload *flow,
+			 struct flow_offload_hw_path *src,
+			 struct flow_offload_hw_path *dest);
+int mtk_offload_check_rx(struct mtk_eth *eth, struct sk_buff *skb, u32 rxd4);
+
 #endif /* MTK_ETH_H */
--- /dev/null
+++ b/drivers/net/ethernet/mediatek/mtk_offload.c
@@ -0,0 +1,146 @@
+/*   This program is free software; you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation; version 2 of the License
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   Copyright (C) 2018 John Crispin <john@phrozen.org>
+ */
+
+#include <net/netfilter/nf_flow_table.h>
+#include "mtk_eth_soc.h"
+
+static int
+mtk_offload_prepare_v4(struct mtk_eth *eth, struct mtk_foe_entry *entry,
+		       struct flow_offload_tuple *s_tuple,
+		       struct flow_offload_tuple *d_tuple,
+		       struct flow_offload_hw_path *src,
+		       struct flow_offload_hw_path *dest)
+{
+	int dest_port = 1;
+
+	if (dest->dev == eth->netdev[1])
+	    dest_port = 2;
+
+	mtk_foe_entry_prepare(entry, MTK_PPE_PKT_TYPE_IPV4_HNAPT, s_tuple->l4proto,
+			      dest_port, dest->eth_src, dest->eth_dest);
+	mtk_foe_entry_set_ipv4_tuple(entry, false,
+				     s_tuple->src_v4.s_addr, s_tuple->src_port,
+				     s_tuple->dst_v4.s_addr, s_tuple->dst_port);
+	mtk_foe_entry_set_ipv4_tuple(entry, true,
+				     d_tuple->dst_v4.s_addr, d_tuple->dst_port,
+				     d_tuple->src_v4.s_addr, d_tuple->src_port);
+
+	if (dest->flags & FLOW_OFFLOAD_PATH_PPPOE)
+		mtk_foe_entry_set_pppoe(entry, dest->pppoe_sid);
+
+	if (dest->flags & FLOW_OFFLOAD_PATH_VLAN)
+		mtk_foe_entry_set_vlan(entry, dest->vlan_id);
+
+	if (dest->flags & FLOW_OFFLOAD_PATH_DSA)
+		mtk_foe_entry_set_dsa(entry, dest->dsa_port);
+
+	return 0;
+}
+
+int mtk_flow_offload_add(struct mtk_eth *eth,
+			 enum flow_offload_type type,
+			 struct flow_offload *flow,
+			 struct flow_offload_hw_path *src,
+			 struct flow_offload_hw_path *dest)
+{
+	struct flow_offload_tuple *otuple = &flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple;
+	struct flow_offload_tuple *rtuple = &flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple;
+	struct mtk_foe_entry orig, reply;
+	u32 ohash, rhash, timestamp;
+
+	if (otuple->l4proto != IPPROTO_TCP && otuple->l4proto != IPPROTO_UDP)
+		return -EINVAL;
+
+	if (type == FLOW_OFFLOAD_DEL) {
+		ohash = (unsigned long)flow->priv;
+		rhash = ohash >> 16;
+		ohash &= 0xffff;
+		mtk_foe_entry_clear(&eth->ppe, ohash);
+		mtk_foe_entry_clear(&eth->ppe, rhash);
+		rcu_assign_pointer(eth->foe_flow_table[ohash], NULL);
+		rcu_assign_pointer(eth->foe_flow_table[rhash], NULL);
+		synchronize_rcu();
+
+		return 0;
+	}
+
+	switch (otuple->l3proto) {
+	case AF_INET:
+		if (mtk_offload_prepare_v4(eth, &orig, otuple, rtuple, src, dest) ||
+		    mtk_offload_prepare_v4(eth, &reply, rtuple, otuple, dest, src))
+			return -EINVAL;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	timestamp = mtk_r32(eth, 0x0010);
+
+	ohash = mtk_foe_entry_commit(&eth->ppe, &orig, timestamp);
+	if (ohash < 0)
+		return -EINVAL;
+
+	rhash = mtk_foe_entry_commit(&eth->ppe, &reply, timestamp);
+	if (rhash < 0) {
+		mtk_foe_entry_clear(&eth->ppe, ohash);
+		return -EINVAL;
+	}
+
+	rcu_assign_pointer(eth->foe_flow_table[ohash], flow);
+	rcu_assign_pointer(eth->foe_flow_table[rhash], flow);
+
+	ohash |= rhash << 16;
+	flow->priv = (void *)(unsigned long)ohash;
+
+	return 0;
+}
+
+static void mtk_offload_keepalive(struct mtk_eth *eth, unsigned int hash)
+{
+	struct flow_offload *flow;
+
+	rcu_read_lock();
+	flow = rcu_dereference(eth->foe_flow_table[hash]);
+	if (flow)
+		flow->timeout = jiffies + 30 * HZ;
+	rcu_read_unlock();
+}
+
+int mtk_offload_check_rx(struct mtk_eth *eth, struct sk_buff *skb, u32 rxd4)
+{
+	unsigned int hash;
+
+	switch (FIELD_GET(MTK_RXD4_PPE_CPU_REASON, rxd4)) {
+	case MTK_PPE_CPU_REASON_KEEPALIVE_UC_OLD_HDR:
+	case MTK_PPE_CPU_REASON_KEEPALIVE_MC_NEW_HDR:
+	case MTK_PPE_CPU_REASON_KEEPALIVE_DUP_OLD_HDR:
+		hash = FIELD_GET(MTK_RXD4_FOE_ENTRY, rxd4);
+		mtk_offload_keepalive(eth, hash);
+		return -1;
+	case MTK_PPE_CPU_REASON_PACKET_SAMPLING:
+		return -1;
+	default:
+		return 0;
+	}
+}
+
+int mtk_flow_offload_init(struct mtk_eth *eth)
+{
+	eth->foe_flow_table = devm_kcalloc(eth->dev, MTK_PPE_ENTRIES,
+					   sizeof(*eth->foe_flow_table),
+					   GFP_KERNEL);
+
+	if (!eth->foe_flow_table)
+		return -ENOMEM;
+
+	return 0;
+}
--- a/drivers/net/ethernet/mediatek/mtk_ppe.c
+++ b/drivers/net/ethernet/mediatek/mtk_ppe.c
@@ -375,6 +375,8 @@ int mtk_ppe_init(struct mtk_ppe *ppe, st
 
 	ppe->foe_table = foe;
 
+	mtk_ppe_debugfs_init(ppe);
+
 	return 0;
 }
 
--- a/drivers/net/ethernet/mediatek/mtk_ppe.h
+++ b/drivers/net/ethernet/mediatek/mtk_ppe.h
@@ -271,4 +271,7 @@ int mtk_foe_entry_set_pppoe(struct mtk_f
 int mtk_foe_entry_commit(struct mtk_ppe *ppe, struct mtk_foe_entry *entry,
 			 u16 timestamp);
 
+/* internal */
+int mtk_ppe_debugfs_init(struct mtk_ppe *ppe);
+
 #endif
--- /dev/null
+++ b/drivers/net/ethernet/mediatek/mtk_ppe_debugfs.c
@@ -0,0 +1,114 @@
+/*   This program is free software; you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation; version 2 of the License
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   Copyright (C) 2014-2016 Sean Wang <sean.wang@mediatek.com>
+ *   Copyright (C) 2016-2017 John Crispin <blogic@openwrt.org>
+ *   Copyright (C) 2020 Felix Fietkau <nbd@nbd.name>
+ */
+
+#include <linux/kernel.h>
+#include <linux/debugfs.h>
+#include "mtk_eth_soc.h"
+
+static const char *mtk_foe_entry_state_str[] = {
+	"INVALID",
+	"UNBIND",
+	"BIND",
+	"FIN"
+};
+
+static const char *mtk_foe_packet_type_str[] = {
+	"IPV4_HNAPT",
+	"IPV4_HNAT",
+	"IPV6_1T_ROUTE",
+	"IPV4_DSLITE",
+	"IPV6_3T_ROUTE",
+	"IPV6_5T_ROUTE",
+	"IPV6_6RD",
+};
+
+#define es(entry)		(mtk_foe_entry_state_str[FIELD_GET(MTK_FOE_IB1_STATE, entry->ib1)])
+//#define ei(entry, end)		(MTK_PPE_TBL_SZ - (int)(end - entry))
+#define pt(entry)		(mtk_foe_packet_type_str[FIELD_GET(MTK_FOE_IB1_PACKET_TYPE, entry->ib1)])
+
+static int mtk_ppe_debugfs_foe_show(struct seq_file *m, void *private)
+{
+	struct mtk_ppe *ppe = m->private;
+	int i, count;
+
+	for (i = 0, count = 0; i < MTK_PPE_ENTRIES; i++) {
+		struct mtk_foe_entry *entry = &ppe->foe_table[i];
+
+		if (!FIELD_GET(MTK_FOE_IB1_STATE, entry->ib1))
+			continue;
+
+		if (FIELD_GET(MTK_FOE_IB1_PACKET_TYPE, entry->ib1) ==
+		    MTK_PPE_PKT_TYPE_IPV4_HNAPT) {
+			struct mtk_foe_ipv4 *ip4 = &entry->ipv4;
+			struct mtk_foe_mac_info *l2 = &ip4->l2;
+
+			__be32 saddr = htonl(ip4->orig.src_ip);
+			__be32 daddr = htonl(ip4->orig.dest_ip);
+			__be32 nsaddr = htonl(ip4->new.src_ip);
+			__be32 ndaddr = htonl(ip4->new.dest_ip);
+			unsigned char h_dest[ETH_ALEN];
+			unsigned char h_source[ETH_ALEN];
+
+			*((__be32 *) h_source) = htonl(l2->src_mac_hi);
+			*((__be16*) &h_source[4]) = htons(l2->src_mac_lo);
+			*((__be32*) h_dest) = htonl(l2->dest_mac_hi);
+			*((__be16*) &h_dest[4]) = htons(l2->dest_mac_lo);
+			seq_printf(m,
+				   "(%x)0x%05x|state=%s|type=%s|"
+				   "%pI4:%d->%pI4:%d=>%pI4:%d->%pI4:%d|%pM=>%pM|"
+				   "etype=0x%04x|info1=0x%x|info2=0x%x|"
+				   "vlan1=%d|vlan2=%d\n",
+				   count, i, es(entry), pt(entry),
+				   &saddr, ip4->orig.src_port,
+				   &daddr, ip4->orig.dest_port,
+				   &nsaddr, ip4->new.src_port,
+				   &ndaddr, ip4->new.dest_port,
+				   h_source, h_dest,
+				   ntohs(l2->etype),
+				   entry->ib1,
+				   ip4->ib2,
+				   l2->vlan1,
+				   l2->vlan2);
+			count++;
+		} else
+			seq_printf(m, "0x%05x state=%s\n", count, es(entry));
+	}
+
+	return 0;
+}
+
+static int mtk_ppe_debugfs_foe_open(struct inode *inode, struct file *file)
+{
+	return single_open(file, mtk_ppe_debugfs_foe_show, inode->i_private);
+}
+
+static const struct file_operations mtk_ppe_debugfs_foe_fops = {
+	.open = mtk_ppe_debugfs_foe_open,
+	.read = seq_read,
+	.llseek = seq_lseek,
+	.release = single_release,
+};
+
+int mtk_ppe_debugfs_init(struct mtk_ppe *ppe)
+{
+	struct dentry *root;
+
+	root = debugfs_create_dir("mtk_ppe", NULL);
+	if (!root)
+		return -ENOMEM;
+
+	debugfs_create_file("entries", S_IRUGO, root, ppe, &mtk_ppe_debugfs_foe_fops);
+
+	return 0;
+}