1 From: Felix Fietkau <nbd@nbd.name>
2 Date: Sat, 5 Feb 2022 18:29:22 +0100
3 Subject: [PATCH] net: ethernet: mtk_eth_soc: implement flow offloading
6 This allows hardware flow offloading from Ethernet to WLAN on MT7622 SoC
8 Co-developed-by: Lorenzo Bianconi <lorenzo@kernel.org>
9 Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>
10 Signed-off-by: Felix Fietkau <nbd@nbd.name>
13 --- a/drivers/net/ethernet/mediatek/mtk_ppe.c
14 +++ b/drivers/net/ethernet/mediatek/mtk_ppe.c
15 @@ -329,6 +329,24 @@ int mtk_foe_entry_set_pppoe(struct mtk_f
19 +int mtk_foe_entry_set_wdma(struct mtk_foe_entry *entry, int wdma_idx, int txq,
22 + struct mtk_foe_mac_info *l2 = mtk_foe_entry_l2(entry);
23 + u32 *ib2 = mtk_foe_entry_ib2(entry);
25 + *ib2 &= ~MTK_FOE_IB2_PORT_MG;
26 + *ib2 |= MTK_FOE_IB2_WDMA_WINFO;
28 + *ib2 |= MTK_FOE_IB2_WDMA_DEVIDX;
30 + l2->vlan2 = FIELD_PREP(MTK_FOE_VLAN2_WINFO_BSS, bss) |
31 + FIELD_PREP(MTK_FOE_VLAN2_WINFO_WCID, wcid) |
32 + FIELD_PREP(MTK_FOE_VLAN2_WINFO_RING, txq);
37 static inline bool mtk_foe_entry_usable(struct mtk_foe_entry *entry)
39 return !(entry->ib1 & MTK_FOE_IB1_STATIC) &&
40 --- a/drivers/net/ethernet/mediatek/mtk_ppe.h
41 +++ b/drivers/net/ethernet/mediatek/mtk_ppe.h
42 @@ -48,9 +48,9 @@ enum {
43 #define MTK_FOE_IB2_DEST_PORT GENMASK(7, 5)
44 #define MTK_FOE_IB2_MULTICAST BIT(8)
46 -#define MTK_FOE_IB2_WHNAT_QID2 GENMASK(13, 12)
47 -#define MTK_FOE_IB2_WHNAT_DEVIDX BIT(16)
48 -#define MTK_FOE_IB2_WHNAT_NAT BIT(17)
49 +#define MTK_FOE_IB2_WDMA_QID2 GENMASK(13, 12)
50 +#define MTK_FOE_IB2_WDMA_DEVIDX BIT(16)
51 +#define MTK_FOE_IB2_WDMA_WINFO BIT(17)
53 #define MTK_FOE_IB2_PORT_MG GENMASK(17, 12)
55 @@ -58,9 +58,9 @@ enum {
57 #define MTK_FOE_IB2_DSCP GENMASK(31, 24)
59 -#define MTK_FOE_VLAN2_WHNAT_BSS GEMMASK(5, 0)
60 -#define MTK_FOE_VLAN2_WHNAT_WCID GENMASK(13, 6)
61 -#define MTK_FOE_VLAN2_WHNAT_RING GENMASK(15, 14)
62 +#define MTK_FOE_VLAN2_WINFO_BSS GENMASK(5, 0)
63 +#define MTK_FOE_VLAN2_WINFO_WCID GENMASK(13, 6)
64 +#define MTK_FOE_VLAN2_WINFO_RING GENMASK(15, 14)
67 MTK_FOE_STATE_INVALID,
68 @@ -281,6 +281,8 @@ int mtk_foe_entry_set_ipv6_tuple(struct
69 int mtk_foe_entry_set_dsa(struct mtk_foe_entry *entry, int port);
70 int mtk_foe_entry_set_vlan(struct mtk_foe_entry *entry, int vid);
71 int mtk_foe_entry_set_pppoe(struct mtk_foe_entry *entry, int sid);
72 +int mtk_foe_entry_set_wdma(struct mtk_foe_entry *entry, int wdma_idx, int txq,
74 int mtk_foe_entry_commit(struct mtk_ppe *ppe, struct mtk_foe_entry *entry,
76 int mtk_ppe_debugfs_init(struct mtk_ppe *ppe);
77 --- a/drivers/net/ethernet/mediatek/mtk_ppe_offload.c
78 +++ b/drivers/net/ethernet/mediatek/mtk_ppe_offload.c
80 #include <net/pkt_cls.h>
82 #include "mtk_eth_soc.h"
85 struct mtk_flow_data {
87 @@ -39,6 +40,7 @@ struct mtk_flow_entry {
88 struct rhash_head node;
94 static const struct rhashtable_params mtk_flow_ht_params = {
95 @@ -80,6 +82,35 @@ mtk_flow_offload_mangle_eth(const struct
96 memcpy(dest, src, act->mangle.mask ? 2 : 4);
100 +mtk_flow_get_wdma_info(struct net_device *dev, const u8 *addr, struct mtk_wdma_info *info)
102 + struct net_device_path_ctx ctx = {
106 + struct net_device_path path = {};
108 + if (!IS_ENABLED(CONFIG_NET_MEDIATEK_SOC_WED))
111 + if (!dev->netdev_ops->ndo_fill_forward_path)
114 + if (dev->netdev_ops->ndo_fill_forward_path(&ctx, &path))
117 + if (path.type != DEV_PATH_MTK_WDMA)
120 + info->wdma_idx = path.mtk_wdma.wdma_idx;
121 + info->queue = path.mtk_wdma.queue;
122 + info->bss = path.mtk_wdma.bss;
123 + info->wcid = path.mtk_wdma.wcid;
130 mtk_flow_mangle_ports(const struct flow_action_entry *act,
131 @@ -149,10 +180,20 @@ mtk_flow_get_dsa_port(struct net_device
134 mtk_flow_set_output_device(struct mtk_eth *eth, struct mtk_foe_entry *foe,
135 - struct net_device *dev)
136 + struct net_device *dev, const u8 *dest_mac,
139 + struct mtk_wdma_info info = {};
140 int pse_port, dsa_port;
142 + if (mtk_flow_get_wdma_info(dev, dest_mac, &info) == 0) {
143 + mtk_foe_entry_set_wdma(foe, info.wdma_idx, info.queue, info.bss,
146 + *wed_index = info.wdma_idx;
150 dsa_port = mtk_flow_get_dsa_port(&dev);
152 mtk_foe_entry_set_dsa(foe, dsa_port);
153 @@ -164,6 +205,7 @@ mtk_flow_set_output_device(struct mtk_et
158 mtk_foe_entry_set_pse_port(foe, pse_port);
161 @@ -179,6 +221,7 @@ mtk_flow_offload_replace(struct mtk_eth
162 struct net_device *odev = NULL;
163 struct mtk_flow_entry *entry;
164 int offload_type = 0;
165 + int wed_index = -1;
169 @@ -326,10 +369,14 @@ mtk_flow_offload_replace(struct mtk_eth
170 if (data.pppoe.num == 1)
171 mtk_foe_entry_set_pppoe(&foe, data.pppoe.sid);
173 - err = mtk_flow_set_output_device(eth, &foe, odev);
174 + err = mtk_flow_set_output_device(eth, &foe, odev, data.eth.h_dest,
179 + if (wed_index >= 0 && (err = mtk_wed_flow_add(wed_index)) < 0)
182 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
185 @@ -343,6 +390,7 @@ mtk_flow_offload_replace(struct mtk_eth
189 + entry->wed_index = wed_index;
190 err = rhashtable_insert_fast(ð->flow_table, &entry->node,
193 @@ -353,6 +401,8 @@ clear_flow:
194 mtk_foe_entry_clear(ð->ppe, hash);
197 + if (wed_index >= 0)
198 + mtk_wed_flow_remove(wed_index);
202 @@ -369,6 +419,8 @@ mtk_flow_offload_destroy(struct mtk_eth
203 mtk_foe_entry_clear(ð->ppe, entry->hash);
204 rhashtable_remove_fast(ð->flow_table, &entry->node,
206 + if (entry->wed_index >= 0)
207 + mtk_wed_flow_remove(entry->wed_index);
211 --- a/drivers/net/ethernet/mediatek/mtk_wed.h
212 +++ b/drivers/net/ethernet/mediatek/mtk_wed.h
214 #include <linux/soc/mediatek/mtk_wed.h>
215 #include <linux/debugfs.h>
216 #include <linux/regmap.h>
217 +#include <linux/netdevice.h>
221 @@ -27,6 +28,12 @@ struct mtk_wed_hw {
225 +struct mtk_wdma_info {
232 #ifdef CONFIG_NET_MEDIATEK_SOC_WED
234 --- a/include/linux/netdevice.h
235 +++ b/include/linux/netdevice.h
236 @@ -875,6 +875,7 @@ enum net_device_path_type {
243 struct net_device_path {
244 @@ -900,6 +901,12 @@ struct net_device_path {
259 @@ -769,6 +769,10 @@ int dev_fill_forward_path(const struct n
260 if (WARN_ON_ONCE(last_dev == ctx.dev))
267 path = dev_fwd_path(stack);