1 // SPDX-License-Identifier: GPL-2.0-only
6 #include <linux/if_ether.h>
7 #include <linux/rhashtable.h>
9 #include <linux/ipv6.h>
10 #include <net/flow_offload.h>
11 #include <net/pkt_cls.h>
13 #include "mtk_eth_soc.h"
16 struct mtk_flow_data {
26 struct in6_addr src_addr;
27 struct in6_addr dst_addr;
47 static const struct rhashtable_params mtk_flow_ht_params = {
48 .head_offset = offsetof(struct mtk_flow_entry, node),
49 .key_offset = offsetof(struct mtk_flow_entry, cookie),
50 .key_len = sizeof(unsigned long),
51 .automatic_shrinking = true,
55 mtk_flow_set_ipv4_addr(struct mtk_eth *eth, struct mtk_foe_entry *foe,
56 struct mtk_flow_data *data, bool egress)
58 return mtk_foe_entry_set_ipv4_tuple(eth, foe, egress,
59 data->v4.src_addr, data->src_port,
60 data->v4.dst_addr, data->dst_port);
64 mtk_flow_set_ipv6_addr(struct mtk_eth *eth, struct mtk_foe_entry *foe,
65 struct mtk_flow_data *data)
67 return mtk_foe_entry_set_ipv6_tuple(eth, foe,
68 data->v6.src_addr.s6_addr32, data->src_port,
69 data->v6.dst_addr.s6_addr32, data->dst_port);
73 mtk_flow_offload_mangle_eth(const struct flow_action_entry *act, void *eth)
75 void *dest = eth + act->mangle.offset;
76 const void *src = &act->mangle.val;
78 if (act->mangle.offset > 8)
81 if (act->mangle.mask == 0xffff) {
86 memcpy(dest, src, act->mangle.mask ? 2 : 4);
90 mtk_flow_get_wdma_info(struct net_device *dev, const u8 *addr, struct mtk_wdma_info *info)
92 struct net_device_path_stack stack;
93 struct net_device_path *path;
99 if (!IS_ENABLED(CONFIG_NET_MEDIATEK_SOC_WED))
102 err = dev_fill_forward_path(dev, addr, &stack);
106 path = &stack.path[stack.num_paths - 1];
107 if (path->type != DEV_PATH_MTK_WDMA)
110 info->wdma_idx = path->mtk_wdma.wdma_idx;
111 info->queue = path->mtk_wdma.queue;
112 info->bss = path->mtk_wdma.bss;
113 info->wcid = path->mtk_wdma.wcid;
114 info->amsdu = path->mtk_wdma.amsdu;
121 mtk_flow_mangle_ports(const struct flow_action_entry *act,
122 struct mtk_flow_data *data)
124 u32 val = ntohl(act->mangle.val);
126 switch (act->mangle.offset) {
128 if (act->mangle.mask == ~htonl(0xffff))
129 data->dst_port = cpu_to_be16(val);
131 data->src_port = cpu_to_be16(val >> 16);
134 data->dst_port = cpu_to_be16(val);
144 mtk_flow_mangle_ipv4(const struct flow_action_entry *act,
145 struct mtk_flow_data *data)
149 switch (act->mangle.offset) {
150 case offsetof(struct iphdr, saddr):
151 dest = &data->v4.src_addr;
153 case offsetof(struct iphdr, daddr):
154 dest = &data->v4.dst_addr;
160 memcpy(dest, &act->mangle.val, sizeof(u32));
166 mtk_flow_get_dsa_port(struct net_device **dev)
168 #if IS_ENABLED(CONFIG_NET_DSA)
171 dp = dsa_port_from_netdev(*dev);
175 if (dp->cpu_dp->tag_ops->proto != DSA_TAG_PROTO_MTK)
178 *dev = dsa_port_to_conduit(dp);
187 mtk_flow_set_output_device(struct mtk_eth *eth, struct mtk_foe_entry *foe,
188 struct net_device *dev, const u8 *dest_mac,
191 struct mtk_wdma_info info = {};
192 int pse_port, dsa_port, queue;
194 if (mtk_flow_get_wdma_info(dev, dest_mac, &info) == 0) {
195 mtk_foe_entry_set_wdma(eth, foe, info.wdma_idx, info.queue,
196 info.bss, info.wcid, info.amsdu);
197 if (mtk_is_netsys_v2_or_greater(eth)) {
198 switch (info.wdma_idx) {
200 pse_port = PSE_WDMA0_PORT;
203 pse_port = PSE_WDMA1_PORT;
206 pse_port = PSE_WDMA2_PORT;
214 *wed_index = info.wdma_idx;
218 dsa_port = mtk_flow_get_dsa_port(&dev);
220 if (dev == eth->netdev[0])
221 pse_port = PSE_GDM1_PORT;
222 else if (dev == eth->netdev[1])
223 pse_port = PSE_GDM2_PORT;
224 else if (dev == eth->netdev[2])
225 pse_port = PSE_GDM3_PORT;
230 mtk_foe_entry_set_dsa(eth, foe, dsa_port);
231 queue = 3 + dsa_port;
233 queue = pse_port - 1;
235 mtk_foe_entry_set_queue(eth, foe, queue);
238 mtk_foe_entry_set_pse_port(eth, foe, pse_port);
244 mtk_flow_offload_replace(struct mtk_eth *eth, struct flow_cls_offload *f,
247 struct flow_rule *rule = flow_cls_offload_flow_rule(f);
248 struct net_device *idev = NULL, *odev = NULL;
249 struct flow_action_entry *act;
250 struct mtk_flow_data data = {};
251 struct mtk_foe_entry foe;
252 struct mtk_flow_entry *entry;
253 int offload_type = 0;
260 if (rhashtable_lookup(ð->flow_table, &f->cookie, mtk_flow_ht_params))
263 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_META)) {
264 struct flow_match_meta match;
266 flow_rule_match_meta(rule, &match);
267 if (mtk_is_netsys_v2_or_greater(eth)) {
268 idev = __dev_get_by_index(&init_net, match.key->ingress_ifindex);
269 if (idev && idev->netdev_ops == eth->netdev[0]->netdev_ops) {
270 struct mtk_mac *mac = netdev_priv(idev);
272 if (WARN_ON(mac->ppe_idx >= eth->soc->ppe_num))
275 ppe_index = mac->ppe_idx;
282 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
283 struct flow_match_control match;
285 flow_rule_match_control(rule, &match);
286 addr_type = match.key->addr_type;
288 if (flow_rule_has_control_flags(match.mask->flags,
295 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
296 struct flow_match_basic match;
298 flow_rule_match_basic(rule, &match);
299 l4proto = match.key->ip_proto;
306 offload_type = MTK_PPE_PKT_TYPE_BRIDGE;
307 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
308 struct flow_match_eth_addrs match;
310 flow_rule_match_eth_addrs(rule, &match);
311 memcpy(data.eth.h_dest, match.key->dst, ETH_ALEN);
312 memcpy(data.eth.h_source, match.key->src, ETH_ALEN);
317 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
318 struct flow_match_vlan match;
320 flow_rule_match_vlan(rule, &match);
322 if (match.key->vlan_tpid != cpu_to_be16(ETH_P_8021Q))
325 data.vlan_in = match.key->vlan_id;
328 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
329 offload_type = MTK_PPE_PKT_TYPE_IPV4_HNAPT;
331 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
332 offload_type = MTK_PPE_PKT_TYPE_IPV6_ROUTE_5T;
338 flow_action_for_each(i, act, &rule->action) {
340 case FLOW_ACTION_MANGLE:
341 if (offload_type == MTK_PPE_PKT_TYPE_BRIDGE)
343 if (act->mangle.htype == FLOW_ACT_MANGLE_HDR_TYPE_ETH)
344 mtk_flow_offload_mangle_eth(act, &data.eth);
346 case FLOW_ACTION_REDIRECT:
349 case FLOW_ACTION_CSUM:
351 case FLOW_ACTION_VLAN_PUSH:
352 if (data.vlan.num == 1 ||
353 act->vlan.proto != htons(ETH_P_8021Q))
356 data.vlan.id = act->vlan.vid;
357 data.vlan.proto = act->vlan.proto;
360 case FLOW_ACTION_VLAN_POP:
362 case FLOW_ACTION_PPPOE_PUSH:
363 if (data.pppoe.num == 1)
366 data.pppoe.sid = act->pppoe.sid;
374 if (!is_valid_ether_addr(data.eth.h_source) ||
375 !is_valid_ether_addr(data.eth.h_dest))
378 err = mtk_foe_entry_prepare(eth, &foe, offload_type, l4proto, 0,
379 data.eth.h_source, data.eth.h_dest);
383 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
384 struct flow_match_ports ports;
386 if (offload_type == MTK_PPE_PKT_TYPE_BRIDGE)
389 flow_rule_match_ports(rule, &ports);
390 data.src_port = ports.key->src;
391 data.dst_port = ports.key->dst;
392 } else if (offload_type != MTK_PPE_PKT_TYPE_BRIDGE) {
396 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
397 struct flow_match_ipv4_addrs addrs;
399 flow_rule_match_ipv4_addrs(rule, &addrs);
401 data.v4.src_addr = addrs.key->src;
402 data.v4.dst_addr = addrs.key->dst;
404 mtk_flow_set_ipv4_addr(eth, &foe, &data, false);
407 if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
408 struct flow_match_ipv6_addrs addrs;
410 flow_rule_match_ipv6_addrs(rule, &addrs);
412 data.v6.src_addr = addrs.key->src;
413 data.v6.dst_addr = addrs.key->dst;
415 mtk_flow_set_ipv6_addr(eth, &foe, &data);
418 flow_action_for_each(i, act, &rule->action) {
419 if (act->id != FLOW_ACTION_MANGLE)
422 if (offload_type == MTK_PPE_PKT_TYPE_BRIDGE)
425 switch (act->mangle.htype) {
426 case FLOW_ACT_MANGLE_HDR_TYPE_TCP:
427 case FLOW_ACT_MANGLE_HDR_TYPE_UDP:
428 err = mtk_flow_mangle_ports(act, &data);
430 case FLOW_ACT_MANGLE_HDR_TYPE_IP4:
431 err = mtk_flow_mangle_ipv4(act, &data);
433 case FLOW_ACT_MANGLE_HDR_TYPE_ETH:
434 /* handled earlier */
444 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
445 err = mtk_flow_set_ipv4_addr(eth, &foe, &data, true);
450 if (offload_type == MTK_PPE_PKT_TYPE_BRIDGE)
451 foe.bridge.vlan = data.vlan_in;
453 if (data.vlan.num == 1) {
454 if (data.vlan.proto != htons(ETH_P_8021Q))
457 mtk_foe_entry_set_vlan(eth, &foe, data.vlan.id);
459 if (data.pppoe.num == 1)
460 mtk_foe_entry_set_pppoe(eth, &foe, data.pppoe.sid);
462 err = mtk_flow_set_output_device(eth, &foe, odev, data.eth.h_dest,
467 if (wed_index >= 0 && (err = mtk_wed_flow_add(wed_index)) < 0)
470 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
474 entry->cookie = f->cookie;
475 memcpy(&entry->data, &foe, sizeof(entry->data));
476 entry->wed_index = wed_index;
477 entry->ppe_index = ppe_index;
479 err = mtk_foe_entry_commit(eth->ppe[entry->ppe_index], entry);
483 err = rhashtable_insert_fast(ð->flow_table, &entry->node,
491 mtk_foe_entry_clear(eth->ppe[entry->ppe_index], entry);
495 mtk_wed_flow_remove(wed_index);
500 mtk_flow_offload_destroy(struct mtk_eth *eth, struct flow_cls_offload *f)
502 struct mtk_flow_entry *entry;
504 entry = rhashtable_lookup(ð->flow_table, &f->cookie,
509 mtk_foe_entry_clear(eth->ppe[entry->ppe_index], entry);
510 rhashtable_remove_fast(ð->flow_table, &entry->node,
512 if (entry->wed_index >= 0)
513 mtk_wed_flow_remove(entry->wed_index);
520 mtk_flow_offload_stats(struct mtk_eth *eth, struct flow_cls_offload *f)
522 struct mtk_flow_entry *entry;
523 struct mtk_foe_accounting diff;
526 entry = rhashtable_lookup(ð->flow_table, &f->cookie,
531 idle = mtk_foe_entry_idle_time(eth->ppe[entry->ppe_index], entry);
532 f->stats.lastused = jiffies - idle * HZ;
534 if (entry->hash != 0xFFFF &&
535 mtk_foe_entry_get_mib(eth->ppe[entry->ppe_index], entry->hash,
537 f->stats.pkts += diff.packets;
538 f->stats.bytes += diff.bytes;
544 static DEFINE_MUTEX(mtk_flow_offload_mutex);
546 int mtk_flow_offload_cmd(struct mtk_eth *eth, struct flow_cls_offload *cls,
551 mutex_lock(&mtk_flow_offload_mutex);
552 switch (cls->command) {
553 case FLOW_CLS_REPLACE:
554 err = mtk_flow_offload_replace(eth, cls, ppe_index);
556 case FLOW_CLS_DESTROY:
557 err = mtk_flow_offload_destroy(eth, cls);
560 err = mtk_flow_offload_stats(eth, cls);
566 mutex_unlock(&mtk_flow_offload_mutex);
572 mtk_eth_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
574 struct flow_cls_offload *cls = type_data;
575 struct net_device *dev = cb_priv;
579 mac = netdev_priv(dev);
582 if (!tc_can_offload(dev))
585 if (type != TC_SETUP_CLSFLOWER)
588 return mtk_flow_offload_cmd(eth, cls, 0);
592 mtk_eth_setup_tc_block(struct net_device *dev, struct flow_block_offload *f)
594 struct mtk_mac *mac = netdev_priv(dev);
595 struct mtk_eth *eth = mac->hw;
596 static LIST_HEAD(block_cb_list);
597 struct flow_block_cb *block_cb;
600 if (!eth->soc->offload_version)
603 if (f->binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
606 cb = mtk_eth_setup_tc_block_cb;
607 f->driver_block_list = &block_cb_list;
609 switch (f->command) {
610 case FLOW_BLOCK_BIND:
611 block_cb = flow_block_cb_lookup(f->block, cb, dev);
613 flow_block_cb_incref(block_cb);
616 block_cb = flow_block_cb_alloc(cb, dev, dev, NULL);
617 if (IS_ERR(block_cb))
618 return PTR_ERR(block_cb);
620 flow_block_cb_incref(block_cb);
621 flow_block_cb_add(block_cb, f);
622 list_add_tail(&block_cb->driver_list, &block_cb_list);
624 case FLOW_BLOCK_UNBIND:
625 block_cb = flow_block_cb_lookup(f->block, cb, dev);
629 if (!flow_block_cb_decref(block_cb)) {
630 flow_block_cb_remove(block_cb, f);
631 list_del(&block_cb->driver_list);
639 int mtk_eth_setup_tc(struct net_device *dev, enum tc_setup_type type,
645 return mtk_eth_setup_tc_block(dev, type_data);
651 int mtk_eth_offload_init(struct mtk_eth *eth, u8 id)
653 if (!eth->ppe[id] || !eth->ppe[id]->foe_table)
655 return rhashtable_init(ð->flow_table, &mtk_flow_ht_params);