1 From: Felix Fietkau <nbd@nbd.name>
2 Date: Wed, 23 Feb 2022 10:56:34 +0100
3 Subject: [PATCH] net: ethernet: mtk_eth_soc: support creating mac
4 address based offload entries
6 This will be used to implement a limited form of bridge offloading.
7 Since the hardware does not support flow table entries with just source
8 and destination MAC address, the driver has to emulate it.
10 The hardware automatically creates entries entries for incoming flows, even
11 when they are bridged instead of routed, and reports when packets for these
12 flows have reached the minimum PPS rate for offloading.
14 After this happens, we look up the L2 flow offload entry based on the MAC
15 header and fill in the output routing information in the flow table.
16 The dynamically created per-flow entries are automatically removed when
17 either the hardware flowtable entry expires, is replaced, or if the offload
18 rule they belong to is removed
20 Signed-off-by: Felix Fietkau <nbd@nbd.name>
23 --- a/drivers/net/ethernet/mediatek/mtk_ppe.c
24 +++ b/drivers/net/ethernet/mediatek/mtk_ppe.c
26 #include <linux/iopoll.h>
27 #include <linux/etherdevice.h>
28 #include <linux/platform_device.h>
29 +#include <linux/if_ether.h>
30 +#include <linux/if_vlan.h>
32 #include "mtk_eth_soc.h"
34 #include "mtk_ppe_regs.h"
36 static DEFINE_SPINLOCK(ppe_lock);
38 +static const struct rhashtable_params mtk_flow_l2_ht_params = {
39 + .head_offset = offsetof(struct mtk_flow_entry, l2_node),
40 + .key_offset = offsetof(struct mtk_flow_entry, data.bridge),
41 + .key_len = offsetof(struct mtk_foe_bridge, key_end),
42 + .automatic_shrinking = true,
45 static void ppe_w32(struct mtk_ppe *ppe, u32 reg, u32 val)
47 writel(val, ppe->base + reg);
48 @@ -123,6 +133,9 @@ mtk_foe_entry_l2(struct mtk_foe_entry *e
50 int type = FIELD_GET(MTK_FOE_IB1_PACKET_TYPE, entry->ib1);
52 + if (type == MTK_PPE_PKT_TYPE_BRIDGE)
53 + return &entry->bridge.l2;
55 if (type >= MTK_PPE_PKT_TYPE_IPV4_DSLITE)
56 return &entry->ipv6.l2;
58 @@ -134,6 +147,9 @@ mtk_foe_entry_ib2(struct mtk_foe_entry *
60 int type = FIELD_GET(MTK_FOE_IB1_PACKET_TYPE, entry->ib1);
62 + if (type == MTK_PPE_PKT_TYPE_BRIDGE)
63 + return &entry->bridge.ib2;
65 if (type >= MTK_PPE_PKT_TYPE_IPV4_DSLITE)
66 return &entry->ipv6.ib2;
68 @@ -168,7 +184,12 @@ int mtk_foe_entry_prepare(struct mtk_foe
69 if (type == MTK_PPE_PKT_TYPE_IPV6_ROUTE_3T)
70 entry->ipv6.ports = ports_pad;
72 - if (type >= MTK_PPE_PKT_TYPE_IPV4_DSLITE) {
73 + if (type == MTK_PPE_PKT_TYPE_BRIDGE) {
74 + ether_addr_copy(entry->bridge.src_mac, src_mac);
75 + ether_addr_copy(entry->bridge.dest_mac, dest_mac);
76 + entry->bridge.ib2 = val;
77 + l2 = &entry->bridge.l2;
78 + } else if (type >= MTK_PPE_PKT_TYPE_IPV4_DSLITE) {
79 entry->ipv6.ib2 = val;
82 @@ -372,12 +393,96 @@ mtk_flow_entry_match(struct mtk_flow_ent
86 +__mtk_foe_entry_clear(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
88 + struct hlist_head *head;
89 + struct hlist_node *tmp;
91 + if (entry->type == MTK_FLOW_TYPE_L2) {
92 + rhashtable_remove_fast(&ppe->l2_flows, &entry->l2_node,
93 + mtk_flow_l2_ht_params);
95 + head = &entry->l2_flows;
96 + hlist_for_each_entry_safe(entry, tmp, head, l2_data.list)
97 + __mtk_foe_entry_clear(ppe, entry);
101 + hlist_del_init(&entry->list);
102 + if (entry->hash != 0xffff) {
103 + ppe->foe_table[entry->hash].ib1 &= ~MTK_FOE_IB1_STATE;
104 + ppe->foe_table[entry->hash].ib1 |= FIELD_PREP(MTK_FOE_IB1_STATE,
105 + MTK_FOE_STATE_BIND);
108 + entry->hash = 0xffff;
110 + if (entry->type != MTK_FLOW_TYPE_L2_SUBFLOW)
113 + hlist_del_init(&entry->l2_data.list);
117 +static int __mtk_foe_entry_idle_time(struct mtk_ppe *ppe, u32 ib1)
122 + now = mtk_eth_timestamp(ppe->eth) & MTK_FOE_IB1_BIND_TIMESTAMP;
123 + timestamp = ib1 & MTK_FOE_IB1_BIND_TIMESTAMP;
125 + if (timestamp > now)
126 + return MTK_FOE_IB1_BIND_TIMESTAMP + 1 - timestamp + now;
128 + return now - timestamp;
132 +mtk_flow_entry_update_l2(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
134 + struct mtk_flow_entry *cur;
135 + struct mtk_foe_entry *hwe;
136 + struct hlist_node *tmp;
139 + idle = __mtk_foe_entry_idle_time(ppe, entry->data.ib1);
140 + hlist_for_each_entry_safe(cur, tmp, &entry->l2_flows, l2_data.list) {
144 + hwe = &ppe->foe_table[cur->hash];
145 + ib1 = READ_ONCE(hwe->ib1);
147 + if (FIELD_GET(MTK_FOE_IB1_STATE, ib1) != MTK_FOE_STATE_BIND) {
148 + cur->hash = 0xffff;
149 + __mtk_foe_entry_clear(ppe, cur);
153 + cur_idle = __mtk_foe_entry_idle_time(ppe, ib1);
154 + if (cur_idle >= idle)
158 + entry->data.ib1 &= ~MTK_FOE_IB1_BIND_TIMESTAMP;
159 + entry->data.ib1 |= hwe->ib1 & MTK_FOE_IB1_BIND_TIMESTAMP;
164 mtk_flow_entry_update(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
166 struct mtk_foe_entry *hwe;
167 struct mtk_foe_entry foe;
169 spin_lock_bh(&ppe_lock);
171 + if (entry->type == MTK_FLOW_TYPE_L2) {
172 + mtk_flow_entry_update_l2(ppe, entry);
176 if (entry->hash == 0xffff)
179 @@ -419,21 +524,28 @@ __mtk_foe_entry_commit(struct mtk_ppe *p
180 void mtk_foe_entry_clear(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
182 spin_lock_bh(&ppe_lock);
183 - hlist_del_init(&entry->list);
184 - if (entry->hash != 0xffff) {
185 - ppe->foe_table[entry->hash].ib1 &= ~MTK_FOE_IB1_STATE;
186 - ppe->foe_table[entry->hash].ib1 |= FIELD_PREP(MTK_FOE_IB1_STATE,
187 - MTK_FOE_STATE_BIND);
190 - entry->hash = 0xffff;
191 + __mtk_foe_entry_clear(ppe, entry);
192 spin_unlock_bh(&ppe_lock);
196 +mtk_foe_entry_commit_l2(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
198 + entry->type = MTK_FLOW_TYPE_L2;
200 + return rhashtable_insert_fast(&ppe->l2_flows, &entry->l2_node,
201 + mtk_flow_l2_ht_params);
204 int mtk_foe_entry_commit(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
206 - u32 hash = mtk_ppe_hash_entry(&entry->data);
207 + int type = FIELD_GET(MTK_FOE_IB1_PACKET_TYPE, entry->data.ib1);
210 + if (type == MTK_PPE_PKT_TYPE_BRIDGE)
211 + return mtk_foe_entry_commit_l2(ppe, entry);
213 + hash = mtk_ppe_hash_entry(&entry->data);
214 entry->hash = 0xffff;
215 spin_lock_bh(&ppe_lock);
216 hlist_add_head(&entry->list, &ppe->foe_flow[hash / 2]);
217 @@ -442,18 +554,72 @@ int mtk_foe_entry_commit(struct mtk_ppe
222 +mtk_foe_entry_commit_subflow(struct mtk_ppe *ppe, struct mtk_flow_entry *entry,
225 + struct mtk_flow_entry *flow_info;
226 + struct mtk_foe_entry foe, *hwe;
227 + struct mtk_foe_mac_info *l2;
228 + u32 ib1_mask = MTK_FOE_IB1_PACKET_TYPE | MTK_FOE_IB1_UDP;
231 + flow_info = kzalloc(offsetof(struct mtk_flow_entry, l2_data.end),
236 + flow_info->l2_data.base_flow = entry;
237 + flow_info->type = MTK_FLOW_TYPE_L2_SUBFLOW;
238 + flow_info->hash = hash;
239 + hlist_add_head(&flow_info->list, &ppe->foe_flow[hash / 2]);
240 + hlist_add_head(&flow_info->l2_data.list, &entry->l2_flows);
242 + hwe = &ppe->foe_table[hash];
243 + memcpy(&foe, hwe, sizeof(foe));
244 + foe.ib1 &= ib1_mask;
245 + foe.ib1 |= entry->data.ib1 & ~ib1_mask;
247 + l2 = mtk_foe_entry_l2(&foe);
248 + memcpy(l2, &entry->data.bridge.l2, sizeof(*l2));
250 + type = FIELD_GET(MTK_FOE_IB1_PACKET_TYPE, foe.ib1);
251 + if (type == MTK_PPE_PKT_TYPE_IPV4_HNAPT)
252 + memcpy(&foe.ipv4.new, &foe.ipv4.orig, sizeof(foe.ipv4.new));
253 + else if (type >= MTK_PPE_PKT_TYPE_IPV6_ROUTE_3T && l2->etype == ETH_P_IP)
254 + l2->etype = ETH_P_IPV6;
256 + *mtk_foe_entry_ib2(&foe) = entry->data.bridge.ib2;
258 + __mtk_foe_entry_commit(ppe, &foe, hash);
261 void __mtk_ppe_check_skb(struct mtk_ppe *ppe, struct sk_buff *skb, u16 hash)
263 struct hlist_head *head = &ppe->foe_flow[hash / 2];
264 - struct mtk_flow_entry *entry;
265 struct mtk_foe_entry *hwe = &ppe->foe_table[hash];
266 + struct mtk_flow_entry *entry;
267 + struct mtk_foe_bridge key = {};
271 - if (hlist_empty(head))
275 spin_lock_bh(&ppe_lock);
277 + if (FIELD_GET(MTK_FOE_IB1_STATE, hwe->ib1) == MTK_FOE_STATE_BIND)
280 hlist_for_each_entry(entry, head, list) {
281 + if (entry->type == MTK_FLOW_TYPE_L2_SUBFLOW) {
282 + if (unlikely(FIELD_GET(MTK_FOE_IB1_STATE, hwe->ib1) ==
283 + MTK_FOE_STATE_BIND))
286 + entry->hash = 0xffff;
287 + __mtk_foe_entry_clear(ppe, entry);
291 if (found || !mtk_flow_entry_match(entry, hwe)) {
292 if (entry->hash != 0xffff)
293 entry->hash = 0xffff;
294 @@ -464,21 +630,50 @@ void __mtk_ppe_check_skb(struct mtk_ppe
295 __mtk_foe_entry_commit(ppe, &entry->data, hash);
303 + ether_addr_copy(key.dest_mac, eh->h_dest);
304 + ether_addr_copy(key.src_mac, eh->h_source);
305 + tag = skb->data - 2;
307 + switch (skb->protocol) {
308 +#if IS_ENABLED(CONFIG_NET_DSA)
309 + case htons(ETH_P_XDSA):
310 + if (!netdev_uses_dsa(skb->dev) ||
311 + skb->dev->dsa_ptr->tag_ops->proto != DSA_TAG_PROTO_MTK)
315 + if (get_unaligned_be16(tag) != ETH_P_8021Q)
320 + case htons(ETH_P_8021Q):
321 + key.vlan = get_unaligned_be16(tag + 2) & VLAN_VID_MASK;
327 + entry = rhashtable_lookup_fast(&ppe->l2_flows, &key, mtk_flow_l2_ht_params);
331 + mtk_foe_entry_commit_subflow(ppe, entry, hash);
334 spin_unlock_bh(&ppe_lock);
337 int mtk_foe_entry_idle_time(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
339 - u16 now = mtk_eth_timestamp(ppe->eth) & MTK_FOE_IB1_BIND_TIMESTAMP;
342 mtk_flow_entry_update(ppe, entry);
343 - timestamp = entry->data.ib1 & MTK_FOE_IB1_BIND_TIMESTAMP;
345 - if (timestamp > now)
346 - return MTK_FOE_IB1_BIND_TIMESTAMP + 1 - timestamp + now;
348 - return now - timestamp;
349 + return __mtk_foe_entry_idle_time(ppe, entry->data.ib1);
352 struct mtk_ppe *mtk_ppe_init(struct mtk_eth *eth, void __iomem *base,
353 @@ -492,6 +687,8 @@ struct mtk_ppe *mtk_ppe_init(struct mtk_
357 + rhashtable_init(&ppe->l2_flows, &mtk_flow_l2_ht_params);
359 /* need to allocate a separate device, since it PPE DMA access is
362 --- a/drivers/net/ethernet/mediatek/mtk_ppe.h
363 +++ b/drivers/net/ethernet/mediatek/mtk_ppe.h
366 #include <linux/kernel.h>
367 #include <linux/bitfield.h>
368 +#include <linux/rhashtable.h>
370 #define MTK_ETH_PPE_BASE 0xc00
372 @@ -84,19 +85,16 @@ struct mtk_foe_mac_info {
376 +/* software-only entry type */
377 struct mtk_foe_bridge {
382 + u8 dest_mac[ETH_ALEN];
383 + u8 src_mac[ETH_ALEN];
394 struct mtk_foe_mac_info l2;
397 @@ -235,13 +233,33 @@ enum {
398 MTK_PPE_CPU_REASON_INVALID = 0x1f,
404 + MTK_FLOW_TYPE_L2_SUBFLOW,
407 struct mtk_flow_entry {
409 + struct hlist_node list;
411 + struct rhash_head l2_node;
412 + struct hlist_head l2_flows;
419 + struct mtk_foe_entry data;
421 + struct mtk_flow_entry *base_flow;
422 + struct hlist_node list;
426 struct rhash_head node;
427 - struct hlist_node list;
428 unsigned long cookie;
429 - struct mtk_foe_entry data;
435 @@ -256,6 +274,8 @@ struct mtk_ppe {
436 u16 foe_check_time[MTK_PPE_ENTRIES];
437 struct hlist_head foe_flow[MTK_PPE_ENTRIES / 2];
439 + struct rhashtable l2_flows;
444 --- a/drivers/net/ethernet/mediatek/mtk_ppe_offload.c
445 +++ b/drivers/net/ethernet/mediatek/mtk_ppe_offload.c
446 @@ -31,6 +31,8 @@ struct mtk_flow_data {
455 @@ -257,9 +259,45 @@ mtk_flow_offload_replace(struct mtk_eth
459 + switch (addr_type) {
461 + offload_type = MTK_PPE_PKT_TYPE_BRIDGE;
462 + if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
463 + struct flow_match_eth_addrs match;
465 + flow_rule_match_eth_addrs(rule, &match);
466 + memcpy(data.eth.h_dest, match.key->dst, ETH_ALEN);
467 + memcpy(data.eth.h_source, match.key->src, ETH_ALEN);
469 + return -EOPNOTSUPP;
472 + if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
473 + struct flow_match_vlan match;
475 + flow_rule_match_vlan(rule, &match);
477 + if (match.key->vlan_tpid != cpu_to_be16(ETH_P_8021Q))
478 + return -EOPNOTSUPP;
480 + data.vlan_in = match.key->vlan_id;
483 + case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
484 + offload_type = MTK_PPE_PKT_TYPE_IPV4_HNAPT;
486 + case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
487 + offload_type = MTK_PPE_PKT_TYPE_IPV6_ROUTE_5T;
490 + return -EOPNOTSUPP;
493 flow_action_for_each(i, act, &rule->action) {
495 case FLOW_ACTION_MANGLE:
496 + if (offload_type == MTK_PPE_PKT_TYPE_BRIDGE)
497 + return -EOPNOTSUPP;
498 if (act->mangle.htype == FLOW_ACT_MANGLE_HDR_TYPE_ETH)
499 mtk_flow_offload_mangle_eth(act, &data.eth);
501 @@ -291,17 +329,6 @@ mtk_flow_offload_replace(struct mtk_eth
505 - switch (addr_type) {
506 - case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
507 - offload_type = MTK_PPE_PKT_TYPE_IPV4_HNAPT;
509 - case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
510 - offload_type = MTK_PPE_PKT_TYPE_IPV6_ROUTE_5T;
513 - return -EOPNOTSUPP;
516 if (!is_valid_ether_addr(data.eth.h_source) ||
517 !is_valid_ether_addr(data.eth.h_dest))
519 @@ -315,10 +342,13 @@ mtk_flow_offload_replace(struct mtk_eth
520 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
521 struct flow_match_ports ports;
523 + if (offload_type == MTK_PPE_PKT_TYPE_BRIDGE)
524 + return -EOPNOTSUPP;
526 flow_rule_match_ports(rule, &ports);
527 data.src_port = ports.key->src;
528 data.dst_port = ports.key->dst;
530 + } else if (offload_type != MTK_PPE_PKT_TYPE_BRIDGE) {
534 @@ -348,6 +378,9 @@ mtk_flow_offload_replace(struct mtk_eth
535 if (act->id != FLOW_ACTION_MANGLE)
538 + if (offload_type == MTK_PPE_PKT_TYPE_BRIDGE)
539 + return -EOPNOTSUPP;
541 switch (act->mangle.htype) {
542 case FLOW_ACT_MANGLE_HDR_TYPE_TCP:
543 case FLOW_ACT_MANGLE_HDR_TYPE_UDP:
544 @@ -373,6 +406,9 @@ mtk_flow_offload_replace(struct mtk_eth
548 + if (offload_type == MTK_PPE_PKT_TYPE_BRIDGE)
549 + foe.bridge.vlan = data.vlan_in;
551 if (data.vlan.num == 1) {
552 if (data.vlan.proto != htons(ETH_P_8021Q))