162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/* Copyright (C) 2020 Felix Fietkau <nbd@nbd.name> */
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci#include <linux/kernel.h>
562306a36Sopenharmony_ci#include <linux/io.h>
662306a36Sopenharmony_ci#include <linux/iopoll.h>
762306a36Sopenharmony_ci#include <linux/etherdevice.h>
862306a36Sopenharmony_ci#include <linux/platform_device.h>
962306a36Sopenharmony_ci#include <linux/if_ether.h>
1062306a36Sopenharmony_ci#include <linux/if_vlan.h>
1162306a36Sopenharmony_ci#include <net/dst_metadata.h>
1262306a36Sopenharmony_ci#include <net/dsa.h>
1362306a36Sopenharmony_ci#include "mtk_eth_soc.h"
1462306a36Sopenharmony_ci#include "mtk_ppe.h"
1562306a36Sopenharmony_ci#include "mtk_ppe_regs.h"
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_cistatic DEFINE_SPINLOCK(ppe_lock);
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_cistatic const struct rhashtable_params mtk_flow_l2_ht_params = {
2062306a36Sopenharmony_ci	.head_offset = offsetof(struct mtk_flow_entry, l2_node),
2162306a36Sopenharmony_ci	.key_offset = offsetof(struct mtk_flow_entry, data.bridge),
2262306a36Sopenharmony_ci	.key_len = offsetof(struct mtk_foe_bridge, key_end),
2362306a36Sopenharmony_ci	.automatic_shrinking = true,
2462306a36Sopenharmony_ci};
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_cistatic void ppe_w32(struct mtk_ppe *ppe, u32 reg, u32 val)
2762306a36Sopenharmony_ci{
2862306a36Sopenharmony_ci	writel(val, ppe->base + reg);
2962306a36Sopenharmony_ci}
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_cistatic u32 ppe_r32(struct mtk_ppe *ppe, u32 reg)
3262306a36Sopenharmony_ci{
3362306a36Sopenharmony_ci	return readl(ppe->base + reg);
3462306a36Sopenharmony_ci}
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_cistatic u32 ppe_m32(struct mtk_ppe *ppe, u32 reg, u32 mask, u32 set)
3762306a36Sopenharmony_ci{
3862306a36Sopenharmony_ci	u32 val;
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci	val = ppe_r32(ppe, reg);
4162306a36Sopenharmony_ci	val &= ~mask;
4262306a36Sopenharmony_ci	val |= set;
4362306a36Sopenharmony_ci	ppe_w32(ppe, reg, val);
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci	return val;
4662306a36Sopenharmony_ci}
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_cistatic u32 ppe_set(struct mtk_ppe *ppe, u32 reg, u32 val)
4962306a36Sopenharmony_ci{
5062306a36Sopenharmony_ci	return ppe_m32(ppe, reg, 0, val);
5162306a36Sopenharmony_ci}
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_cistatic u32 ppe_clear(struct mtk_ppe *ppe, u32 reg, u32 val)
5462306a36Sopenharmony_ci{
5562306a36Sopenharmony_ci	return ppe_m32(ppe, reg, val, 0);
5662306a36Sopenharmony_ci}
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_cistatic u32 mtk_eth_timestamp(struct mtk_eth *eth)
5962306a36Sopenharmony_ci{
6062306a36Sopenharmony_ci	return mtk_r32(eth, 0x0010) & mtk_get_ib1_ts_mask(eth);
6162306a36Sopenharmony_ci}
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_cistatic int mtk_ppe_wait_busy(struct mtk_ppe *ppe)
6462306a36Sopenharmony_ci{
6562306a36Sopenharmony_ci	int ret;
6662306a36Sopenharmony_ci	u32 val;
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci	ret = readl_poll_timeout(ppe->base + MTK_PPE_GLO_CFG, val,
6962306a36Sopenharmony_ci				 !(val & MTK_PPE_GLO_CFG_BUSY),
7062306a36Sopenharmony_ci				 20, MTK_PPE_WAIT_TIMEOUT_US);
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci	if (ret)
7362306a36Sopenharmony_ci		dev_err(ppe->dev, "PPE table busy");
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci	return ret;
7662306a36Sopenharmony_ci}
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_cistatic int mtk_ppe_mib_wait_busy(struct mtk_ppe *ppe)
7962306a36Sopenharmony_ci{
8062306a36Sopenharmony_ci	int ret;
8162306a36Sopenharmony_ci	u32 val;
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci	ret = readl_poll_timeout(ppe->base + MTK_PPE_MIB_SER_CR, val,
8462306a36Sopenharmony_ci				 !(val & MTK_PPE_MIB_SER_CR_ST),
8562306a36Sopenharmony_ci				 20, MTK_PPE_WAIT_TIMEOUT_US);
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci	if (ret)
8862306a36Sopenharmony_ci		dev_err(ppe->dev, "MIB table busy");
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci	return ret;
9162306a36Sopenharmony_ci}
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_cistatic int mtk_mib_entry_read(struct mtk_ppe *ppe, u16 index, u64 *bytes, u64 *packets)
9462306a36Sopenharmony_ci{
9562306a36Sopenharmony_ci	u32 val, cnt_r0, cnt_r1, cnt_r2;
9662306a36Sopenharmony_ci	int ret;
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci	val = FIELD_PREP(MTK_PPE_MIB_SER_CR_ADDR, index) | MTK_PPE_MIB_SER_CR_ST;
9962306a36Sopenharmony_ci	ppe_w32(ppe, MTK_PPE_MIB_SER_CR, val);
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci	ret = mtk_ppe_mib_wait_busy(ppe);
10262306a36Sopenharmony_ci	if (ret)
10362306a36Sopenharmony_ci		return ret;
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci	cnt_r0 = readl(ppe->base + MTK_PPE_MIB_SER_R0);
10662306a36Sopenharmony_ci	cnt_r1 = readl(ppe->base + MTK_PPE_MIB_SER_R1);
10762306a36Sopenharmony_ci	cnt_r2 = readl(ppe->base + MTK_PPE_MIB_SER_R2);
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci	if (mtk_is_netsys_v3_or_greater(ppe->eth)) {
11062306a36Sopenharmony_ci		/* 64 bit for each counter */
11162306a36Sopenharmony_ci		u32 cnt_r3 = readl(ppe->base + MTK_PPE_MIB_SER_R3);
11262306a36Sopenharmony_ci		*bytes = ((u64)cnt_r1 << 32) | cnt_r0;
11362306a36Sopenharmony_ci		*packets = ((u64)cnt_r3 << 32) | cnt_r2;
11462306a36Sopenharmony_ci	} else {
11562306a36Sopenharmony_ci		/* 48 bit byte counter, 40 bit packet counter */
11662306a36Sopenharmony_ci		u32 byte_cnt_low = FIELD_GET(MTK_PPE_MIB_SER_R0_BYTE_CNT_LOW, cnt_r0);
11762306a36Sopenharmony_ci		u32 byte_cnt_high = FIELD_GET(MTK_PPE_MIB_SER_R1_BYTE_CNT_HIGH, cnt_r1);
11862306a36Sopenharmony_ci		u32 pkt_cnt_low = FIELD_GET(MTK_PPE_MIB_SER_R1_PKT_CNT_LOW, cnt_r1);
11962306a36Sopenharmony_ci		u32 pkt_cnt_high = FIELD_GET(MTK_PPE_MIB_SER_R2_PKT_CNT_HIGH, cnt_r2);
12062306a36Sopenharmony_ci		*bytes = ((u64)byte_cnt_high << 32) | byte_cnt_low;
12162306a36Sopenharmony_ci		*packets = ((u64)pkt_cnt_high << 16) | pkt_cnt_low;
12262306a36Sopenharmony_ci	}
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci	return 0;
12562306a36Sopenharmony_ci}
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_cistatic void mtk_ppe_cache_clear(struct mtk_ppe *ppe)
12862306a36Sopenharmony_ci{
12962306a36Sopenharmony_ci	ppe_set(ppe, MTK_PPE_CACHE_CTL, MTK_PPE_CACHE_CTL_CLEAR);
13062306a36Sopenharmony_ci	ppe_clear(ppe, MTK_PPE_CACHE_CTL, MTK_PPE_CACHE_CTL_CLEAR);
13162306a36Sopenharmony_ci}
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_cistatic void mtk_ppe_cache_enable(struct mtk_ppe *ppe, bool enable)
13462306a36Sopenharmony_ci{
13562306a36Sopenharmony_ci	mtk_ppe_cache_clear(ppe);
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci	ppe_m32(ppe, MTK_PPE_CACHE_CTL, MTK_PPE_CACHE_CTL_EN,
13862306a36Sopenharmony_ci		enable * MTK_PPE_CACHE_CTL_EN);
13962306a36Sopenharmony_ci}
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_cistatic u32 mtk_ppe_hash_entry(struct mtk_eth *eth, struct mtk_foe_entry *e)
14262306a36Sopenharmony_ci{
14362306a36Sopenharmony_ci	u32 hv1, hv2, hv3;
14462306a36Sopenharmony_ci	u32 hash;
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci	switch (mtk_get_ib1_pkt_type(eth, e->ib1)) {
14762306a36Sopenharmony_ci		case MTK_PPE_PKT_TYPE_IPV4_ROUTE:
14862306a36Sopenharmony_ci		case MTK_PPE_PKT_TYPE_IPV4_HNAPT:
14962306a36Sopenharmony_ci			hv1 = e->ipv4.orig.ports;
15062306a36Sopenharmony_ci			hv2 = e->ipv4.orig.dest_ip;
15162306a36Sopenharmony_ci			hv3 = e->ipv4.orig.src_ip;
15262306a36Sopenharmony_ci			break;
15362306a36Sopenharmony_ci		case MTK_PPE_PKT_TYPE_IPV6_ROUTE_3T:
15462306a36Sopenharmony_ci		case MTK_PPE_PKT_TYPE_IPV6_ROUTE_5T:
15562306a36Sopenharmony_ci			hv1 = e->ipv6.src_ip[3] ^ e->ipv6.dest_ip[3];
15662306a36Sopenharmony_ci			hv1 ^= e->ipv6.ports;
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci			hv2 = e->ipv6.src_ip[2] ^ e->ipv6.dest_ip[2];
15962306a36Sopenharmony_ci			hv2 ^= e->ipv6.dest_ip[0];
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci			hv3 = e->ipv6.src_ip[1] ^ e->ipv6.dest_ip[1];
16262306a36Sopenharmony_ci			hv3 ^= e->ipv6.src_ip[0];
16362306a36Sopenharmony_ci			break;
16462306a36Sopenharmony_ci		case MTK_PPE_PKT_TYPE_IPV4_DSLITE:
16562306a36Sopenharmony_ci		case MTK_PPE_PKT_TYPE_IPV6_6RD:
16662306a36Sopenharmony_ci		default:
16762306a36Sopenharmony_ci			WARN_ON_ONCE(1);
16862306a36Sopenharmony_ci			return MTK_PPE_HASH_MASK;
16962306a36Sopenharmony_ci	}
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ci	hash = (hv1 & hv2) | ((~hv1) & hv3);
17262306a36Sopenharmony_ci	hash = (hash >> 24) | ((hash & 0xffffff) << 8);
17362306a36Sopenharmony_ci	hash ^= hv1 ^ hv2 ^ hv3;
17462306a36Sopenharmony_ci	hash ^= hash >> 16;
17562306a36Sopenharmony_ci	hash <<= (ffs(eth->soc->hash_offset) - 1);
17662306a36Sopenharmony_ci	hash &= MTK_PPE_ENTRIES - 1;
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci	return hash;
17962306a36Sopenharmony_ci}
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_cistatic inline struct mtk_foe_mac_info *
18262306a36Sopenharmony_cimtk_foe_entry_l2(struct mtk_eth *eth, struct mtk_foe_entry *entry)
18362306a36Sopenharmony_ci{
18462306a36Sopenharmony_ci	int type = mtk_get_ib1_pkt_type(eth, entry->ib1);
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci	if (type == MTK_PPE_PKT_TYPE_BRIDGE)
18762306a36Sopenharmony_ci		return &entry->bridge.l2;
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci	if (type >= MTK_PPE_PKT_TYPE_IPV4_DSLITE)
19062306a36Sopenharmony_ci		return &entry->ipv6.l2;
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ci	return &entry->ipv4.l2;
19362306a36Sopenharmony_ci}
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_cistatic inline u32 *
19662306a36Sopenharmony_cimtk_foe_entry_ib2(struct mtk_eth *eth, struct mtk_foe_entry *entry)
19762306a36Sopenharmony_ci{
19862306a36Sopenharmony_ci	int type = mtk_get_ib1_pkt_type(eth, entry->ib1);
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ci	if (type == MTK_PPE_PKT_TYPE_BRIDGE)
20162306a36Sopenharmony_ci		return &entry->bridge.ib2;
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ci	if (type >= MTK_PPE_PKT_TYPE_IPV4_DSLITE)
20462306a36Sopenharmony_ci		return &entry->ipv6.ib2;
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ci	return &entry->ipv4.ib2;
20762306a36Sopenharmony_ci}
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_ciint mtk_foe_entry_prepare(struct mtk_eth *eth, struct mtk_foe_entry *entry,
21062306a36Sopenharmony_ci			  int type, int l4proto, u8 pse_port, u8 *src_mac,
21162306a36Sopenharmony_ci			  u8 *dest_mac)
21262306a36Sopenharmony_ci{
21362306a36Sopenharmony_ci	struct mtk_foe_mac_info *l2;
21462306a36Sopenharmony_ci	u32 ports_pad, val;
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ci	memset(entry, 0, sizeof(*entry));
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ci	if (mtk_is_netsys_v2_or_greater(eth)) {
21962306a36Sopenharmony_ci		val = FIELD_PREP(MTK_FOE_IB1_STATE, MTK_FOE_STATE_BIND) |
22062306a36Sopenharmony_ci		      FIELD_PREP(MTK_FOE_IB1_PACKET_TYPE_V2, type) |
22162306a36Sopenharmony_ci		      FIELD_PREP(MTK_FOE_IB1_UDP, l4proto == IPPROTO_UDP) |
22262306a36Sopenharmony_ci		      MTK_FOE_IB1_BIND_CACHE_V2 | MTK_FOE_IB1_BIND_TTL_V2;
22362306a36Sopenharmony_ci		entry->ib1 = val;
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ci		val = FIELD_PREP(MTK_FOE_IB2_DEST_PORT_V2, pse_port) |
22662306a36Sopenharmony_ci		      FIELD_PREP(MTK_FOE_IB2_PORT_AG_V2, 0xf);
22762306a36Sopenharmony_ci	} else {
22862306a36Sopenharmony_ci		int port_mg = eth->soc->offload_version > 1 ? 0 : 0x3f;
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_ci		val = FIELD_PREP(MTK_FOE_IB1_STATE, MTK_FOE_STATE_BIND) |
23162306a36Sopenharmony_ci		      FIELD_PREP(MTK_FOE_IB1_PACKET_TYPE, type) |
23262306a36Sopenharmony_ci		      FIELD_PREP(MTK_FOE_IB1_UDP, l4proto == IPPROTO_UDP) |
23362306a36Sopenharmony_ci		      MTK_FOE_IB1_BIND_CACHE | MTK_FOE_IB1_BIND_TTL;
23462306a36Sopenharmony_ci		entry->ib1 = val;
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_ci		val = FIELD_PREP(MTK_FOE_IB2_DEST_PORT, pse_port) |
23762306a36Sopenharmony_ci		      FIELD_PREP(MTK_FOE_IB2_PORT_MG, port_mg) |
23862306a36Sopenharmony_ci		      FIELD_PREP(MTK_FOE_IB2_PORT_AG, 0x1f);
23962306a36Sopenharmony_ci	}
24062306a36Sopenharmony_ci
24162306a36Sopenharmony_ci	if (is_multicast_ether_addr(dest_mac))
24262306a36Sopenharmony_ci		val |= mtk_get_ib2_multicast_mask(eth);
24362306a36Sopenharmony_ci
24462306a36Sopenharmony_ci	ports_pad = 0xa5a5a500 | (l4proto & 0xff);
24562306a36Sopenharmony_ci	if (type == MTK_PPE_PKT_TYPE_IPV4_ROUTE)
24662306a36Sopenharmony_ci		entry->ipv4.orig.ports = ports_pad;
24762306a36Sopenharmony_ci	if (type == MTK_PPE_PKT_TYPE_IPV6_ROUTE_3T)
24862306a36Sopenharmony_ci		entry->ipv6.ports = ports_pad;
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_ci	if (type == MTK_PPE_PKT_TYPE_BRIDGE) {
25162306a36Sopenharmony_ci		ether_addr_copy(entry->bridge.src_mac, src_mac);
25262306a36Sopenharmony_ci		ether_addr_copy(entry->bridge.dest_mac, dest_mac);
25362306a36Sopenharmony_ci		entry->bridge.ib2 = val;
25462306a36Sopenharmony_ci		l2 = &entry->bridge.l2;
25562306a36Sopenharmony_ci	} else if (type >= MTK_PPE_PKT_TYPE_IPV4_DSLITE) {
25662306a36Sopenharmony_ci		entry->ipv6.ib2 = val;
25762306a36Sopenharmony_ci		l2 = &entry->ipv6.l2;
25862306a36Sopenharmony_ci	} else {
25962306a36Sopenharmony_ci		entry->ipv4.ib2 = val;
26062306a36Sopenharmony_ci		l2 = &entry->ipv4.l2;
26162306a36Sopenharmony_ci	}
26262306a36Sopenharmony_ci
26362306a36Sopenharmony_ci	l2->dest_mac_hi = get_unaligned_be32(dest_mac);
26462306a36Sopenharmony_ci	l2->dest_mac_lo = get_unaligned_be16(dest_mac + 4);
26562306a36Sopenharmony_ci	l2->src_mac_hi = get_unaligned_be32(src_mac);
26662306a36Sopenharmony_ci	l2->src_mac_lo = get_unaligned_be16(src_mac + 4);
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_ci	if (type >= MTK_PPE_PKT_TYPE_IPV6_ROUTE_3T)
26962306a36Sopenharmony_ci		l2->etype = ETH_P_IPV6;
27062306a36Sopenharmony_ci	else
27162306a36Sopenharmony_ci		l2->etype = ETH_P_IP;
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_ci	return 0;
27462306a36Sopenharmony_ci}
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_ciint mtk_foe_entry_set_pse_port(struct mtk_eth *eth,
27762306a36Sopenharmony_ci			       struct mtk_foe_entry *entry, u8 port)
27862306a36Sopenharmony_ci{
27962306a36Sopenharmony_ci	u32 *ib2 = mtk_foe_entry_ib2(eth, entry);
28062306a36Sopenharmony_ci	u32 val = *ib2;
28162306a36Sopenharmony_ci
28262306a36Sopenharmony_ci	if (mtk_is_netsys_v2_or_greater(eth)) {
28362306a36Sopenharmony_ci		val &= ~MTK_FOE_IB2_DEST_PORT_V2;
28462306a36Sopenharmony_ci		val |= FIELD_PREP(MTK_FOE_IB2_DEST_PORT_V2, port);
28562306a36Sopenharmony_ci	} else {
28662306a36Sopenharmony_ci		val &= ~MTK_FOE_IB2_DEST_PORT;
28762306a36Sopenharmony_ci		val |= FIELD_PREP(MTK_FOE_IB2_DEST_PORT, port);
28862306a36Sopenharmony_ci	}
28962306a36Sopenharmony_ci	*ib2 = val;
29062306a36Sopenharmony_ci
29162306a36Sopenharmony_ci	return 0;
29262306a36Sopenharmony_ci}
29362306a36Sopenharmony_ci
29462306a36Sopenharmony_ciint mtk_foe_entry_set_ipv4_tuple(struct mtk_eth *eth,
29562306a36Sopenharmony_ci				 struct mtk_foe_entry *entry, bool egress,
29662306a36Sopenharmony_ci				 __be32 src_addr, __be16 src_port,
29762306a36Sopenharmony_ci				 __be32 dest_addr, __be16 dest_port)
29862306a36Sopenharmony_ci{
29962306a36Sopenharmony_ci	int type = mtk_get_ib1_pkt_type(eth, entry->ib1);
30062306a36Sopenharmony_ci	struct mtk_ipv4_tuple *t;
30162306a36Sopenharmony_ci
30262306a36Sopenharmony_ci	switch (type) {
30362306a36Sopenharmony_ci	case MTK_PPE_PKT_TYPE_IPV4_HNAPT:
30462306a36Sopenharmony_ci		if (egress) {
30562306a36Sopenharmony_ci			t = &entry->ipv4.new;
30662306a36Sopenharmony_ci			break;
30762306a36Sopenharmony_ci		}
30862306a36Sopenharmony_ci		fallthrough;
30962306a36Sopenharmony_ci	case MTK_PPE_PKT_TYPE_IPV4_DSLITE:
31062306a36Sopenharmony_ci	case MTK_PPE_PKT_TYPE_IPV4_ROUTE:
31162306a36Sopenharmony_ci		t = &entry->ipv4.orig;
31262306a36Sopenharmony_ci		break;
31362306a36Sopenharmony_ci	case MTK_PPE_PKT_TYPE_IPV6_6RD:
31462306a36Sopenharmony_ci		entry->ipv6_6rd.tunnel_src_ip = be32_to_cpu(src_addr);
31562306a36Sopenharmony_ci		entry->ipv6_6rd.tunnel_dest_ip = be32_to_cpu(dest_addr);
31662306a36Sopenharmony_ci		return 0;
31762306a36Sopenharmony_ci	default:
31862306a36Sopenharmony_ci		WARN_ON_ONCE(1);
31962306a36Sopenharmony_ci		return -EINVAL;
32062306a36Sopenharmony_ci	}
32162306a36Sopenharmony_ci
32262306a36Sopenharmony_ci	t->src_ip = be32_to_cpu(src_addr);
32362306a36Sopenharmony_ci	t->dest_ip = be32_to_cpu(dest_addr);
32462306a36Sopenharmony_ci
32562306a36Sopenharmony_ci	if (type == MTK_PPE_PKT_TYPE_IPV4_ROUTE)
32662306a36Sopenharmony_ci		return 0;
32762306a36Sopenharmony_ci
32862306a36Sopenharmony_ci	t->src_port = be16_to_cpu(src_port);
32962306a36Sopenharmony_ci	t->dest_port = be16_to_cpu(dest_port);
33062306a36Sopenharmony_ci
33162306a36Sopenharmony_ci	return 0;
33262306a36Sopenharmony_ci}
33362306a36Sopenharmony_ci
33462306a36Sopenharmony_ciint mtk_foe_entry_set_ipv6_tuple(struct mtk_eth *eth,
33562306a36Sopenharmony_ci				 struct mtk_foe_entry *entry,
33662306a36Sopenharmony_ci				 __be32 *src_addr, __be16 src_port,
33762306a36Sopenharmony_ci				 __be32 *dest_addr, __be16 dest_port)
33862306a36Sopenharmony_ci{
33962306a36Sopenharmony_ci	int type = mtk_get_ib1_pkt_type(eth, entry->ib1);
34062306a36Sopenharmony_ci	u32 *src, *dest;
34162306a36Sopenharmony_ci	int i;
34262306a36Sopenharmony_ci
34362306a36Sopenharmony_ci	switch (type) {
34462306a36Sopenharmony_ci	case MTK_PPE_PKT_TYPE_IPV4_DSLITE:
34562306a36Sopenharmony_ci		src = entry->dslite.tunnel_src_ip;
34662306a36Sopenharmony_ci		dest = entry->dslite.tunnel_dest_ip;
34762306a36Sopenharmony_ci		break;
34862306a36Sopenharmony_ci	case MTK_PPE_PKT_TYPE_IPV6_ROUTE_5T:
34962306a36Sopenharmony_ci	case MTK_PPE_PKT_TYPE_IPV6_6RD:
35062306a36Sopenharmony_ci		entry->ipv6.src_port = be16_to_cpu(src_port);
35162306a36Sopenharmony_ci		entry->ipv6.dest_port = be16_to_cpu(dest_port);
35262306a36Sopenharmony_ci		fallthrough;
35362306a36Sopenharmony_ci	case MTK_PPE_PKT_TYPE_IPV6_ROUTE_3T:
35462306a36Sopenharmony_ci		src = entry->ipv6.src_ip;
35562306a36Sopenharmony_ci		dest = entry->ipv6.dest_ip;
35662306a36Sopenharmony_ci		break;
35762306a36Sopenharmony_ci	default:
35862306a36Sopenharmony_ci		WARN_ON_ONCE(1);
35962306a36Sopenharmony_ci		return -EINVAL;
36062306a36Sopenharmony_ci	}
36162306a36Sopenharmony_ci
36262306a36Sopenharmony_ci	for (i = 0; i < 4; i++)
36362306a36Sopenharmony_ci		src[i] = be32_to_cpu(src_addr[i]);
36462306a36Sopenharmony_ci	for (i = 0; i < 4; i++)
36562306a36Sopenharmony_ci		dest[i] = be32_to_cpu(dest_addr[i]);
36662306a36Sopenharmony_ci
36762306a36Sopenharmony_ci	return 0;
36862306a36Sopenharmony_ci}
36962306a36Sopenharmony_ci
37062306a36Sopenharmony_ciint mtk_foe_entry_set_dsa(struct mtk_eth *eth, struct mtk_foe_entry *entry,
37162306a36Sopenharmony_ci			  int port)
37262306a36Sopenharmony_ci{
37362306a36Sopenharmony_ci	struct mtk_foe_mac_info *l2 = mtk_foe_entry_l2(eth, entry);
37462306a36Sopenharmony_ci
37562306a36Sopenharmony_ci	l2->etype = BIT(port);
37662306a36Sopenharmony_ci
37762306a36Sopenharmony_ci	if (!(entry->ib1 & mtk_get_ib1_vlan_layer_mask(eth)))
37862306a36Sopenharmony_ci		entry->ib1 |= mtk_prep_ib1_vlan_layer(eth, 1);
37962306a36Sopenharmony_ci	else
38062306a36Sopenharmony_ci		l2->etype |= BIT(8);
38162306a36Sopenharmony_ci
38262306a36Sopenharmony_ci	entry->ib1 &= ~mtk_get_ib1_vlan_tag_mask(eth);
38362306a36Sopenharmony_ci
38462306a36Sopenharmony_ci	return 0;
38562306a36Sopenharmony_ci}
38662306a36Sopenharmony_ci
38762306a36Sopenharmony_ciint mtk_foe_entry_set_vlan(struct mtk_eth *eth, struct mtk_foe_entry *entry,
38862306a36Sopenharmony_ci			   int vid)
38962306a36Sopenharmony_ci{
39062306a36Sopenharmony_ci	struct mtk_foe_mac_info *l2 = mtk_foe_entry_l2(eth, entry);
39162306a36Sopenharmony_ci
39262306a36Sopenharmony_ci	switch (mtk_get_ib1_vlan_layer(eth, entry->ib1)) {
39362306a36Sopenharmony_ci	case 0:
39462306a36Sopenharmony_ci		entry->ib1 |= mtk_get_ib1_vlan_tag_mask(eth) |
39562306a36Sopenharmony_ci			      mtk_prep_ib1_vlan_layer(eth, 1);
39662306a36Sopenharmony_ci		l2->vlan1 = vid;
39762306a36Sopenharmony_ci		return 0;
39862306a36Sopenharmony_ci	case 1:
39962306a36Sopenharmony_ci		if (!(entry->ib1 & mtk_get_ib1_vlan_tag_mask(eth))) {
40062306a36Sopenharmony_ci			l2->vlan1 = vid;
40162306a36Sopenharmony_ci			l2->etype |= BIT(8);
40262306a36Sopenharmony_ci		} else {
40362306a36Sopenharmony_ci			l2->vlan2 = vid;
40462306a36Sopenharmony_ci			entry->ib1 += mtk_prep_ib1_vlan_layer(eth, 1);
40562306a36Sopenharmony_ci		}
40662306a36Sopenharmony_ci		return 0;
40762306a36Sopenharmony_ci	default:
40862306a36Sopenharmony_ci		return -ENOSPC;
40962306a36Sopenharmony_ci	}
41062306a36Sopenharmony_ci}
41162306a36Sopenharmony_ci
41262306a36Sopenharmony_ciint mtk_foe_entry_set_pppoe(struct mtk_eth *eth, struct mtk_foe_entry *entry,
41362306a36Sopenharmony_ci			    int sid)
41462306a36Sopenharmony_ci{
41562306a36Sopenharmony_ci	struct mtk_foe_mac_info *l2 = mtk_foe_entry_l2(eth, entry);
41662306a36Sopenharmony_ci
41762306a36Sopenharmony_ci	if (!(entry->ib1 & mtk_get_ib1_vlan_layer_mask(eth)) ||
41862306a36Sopenharmony_ci	    (entry->ib1 & mtk_get_ib1_vlan_tag_mask(eth)))
41962306a36Sopenharmony_ci		l2->etype = ETH_P_PPP_SES;
42062306a36Sopenharmony_ci
42162306a36Sopenharmony_ci	entry->ib1 |= mtk_get_ib1_ppoe_mask(eth);
42262306a36Sopenharmony_ci	l2->pppoe_id = sid;
42362306a36Sopenharmony_ci
42462306a36Sopenharmony_ci	return 0;
42562306a36Sopenharmony_ci}
42662306a36Sopenharmony_ci
42762306a36Sopenharmony_ciint mtk_foe_entry_set_wdma(struct mtk_eth *eth, struct mtk_foe_entry *entry,
42862306a36Sopenharmony_ci			   int wdma_idx, int txq, int bss, int wcid)
42962306a36Sopenharmony_ci{
43062306a36Sopenharmony_ci	struct mtk_foe_mac_info *l2 = mtk_foe_entry_l2(eth, entry);
43162306a36Sopenharmony_ci	u32 *ib2 = mtk_foe_entry_ib2(eth, entry);
43262306a36Sopenharmony_ci
43362306a36Sopenharmony_ci	switch (eth->soc->version) {
43462306a36Sopenharmony_ci	case 3:
43562306a36Sopenharmony_ci		*ib2 &= ~MTK_FOE_IB2_PORT_MG_V2;
43662306a36Sopenharmony_ci		*ib2 |=  FIELD_PREP(MTK_FOE_IB2_RX_IDX, txq) |
43762306a36Sopenharmony_ci			 MTK_FOE_IB2_WDMA_WINFO_V2;
43862306a36Sopenharmony_ci		l2->w3info = FIELD_PREP(MTK_FOE_WINFO_WCID_V3, wcid) |
43962306a36Sopenharmony_ci			     FIELD_PREP(MTK_FOE_WINFO_BSS_V3, bss);
44062306a36Sopenharmony_ci		break;
44162306a36Sopenharmony_ci	case 2:
44262306a36Sopenharmony_ci		*ib2 &= ~MTK_FOE_IB2_PORT_MG_V2;
44362306a36Sopenharmony_ci		*ib2 |=  FIELD_PREP(MTK_FOE_IB2_RX_IDX, txq) |
44462306a36Sopenharmony_ci			 MTK_FOE_IB2_WDMA_WINFO_V2;
44562306a36Sopenharmony_ci		l2->winfo = FIELD_PREP(MTK_FOE_WINFO_WCID, wcid) |
44662306a36Sopenharmony_ci			    FIELD_PREP(MTK_FOE_WINFO_BSS, bss);
44762306a36Sopenharmony_ci		break;
44862306a36Sopenharmony_ci	default:
44962306a36Sopenharmony_ci		*ib2 &= ~MTK_FOE_IB2_PORT_MG;
45062306a36Sopenharmony_ci		*ib2 |= MTK_FOE_IB2_WDMA_WINFO;
45162306a36Sopenharmony_ci		if (wdma_idx)
45262306a36Sopenharmony_ci			*ib2 |= MTK_FOE_IB2_WDMA_DEVIDX;
45362306a36Sopenharmony_ci		l2->vlan2 = FIELD_PREP(MTK_FOE_VLAN2_WINFO_BSS, bss) |
45462306a36Sopenharmony_ci			    FIELD_PREP(MTK_FOE_VLAN2_WINFO_WCID, wcid) |
45562306a36Sopenharmony_ci			    FIELD_PREP(MTK_FOE_VLAN2_WINFO_RING, txq);
45662306a36Sopenharmony_ci		break;
45762306a36Sopenharmony_ci	}
45862306a36Sopenharmony_ci
45962306a36Sopenharmony_ci	return 0;
46062306a36Sopenharmony_ci}
46162306a36Sopenharmony_ci
46262306a36Sopenharmony_ciint mtk_foe_entry_set_queue(struct mtk_eth *eth, struct mtk_foe_entry *entry,
46362306a36Sopenharmony_ci			    unsigned int queue)
46462306a36Sopenharmony_ci{
46562306a36Sopenharmony_ci	u32 *ib2 = mtk_foe_entry_ib2(eth, entry);
46662306a36Sopenharmony_ci
46762306a36Sopenharmony_ci	if (mtk_is_netsys_v2_or_greater(eth)) {
46862306a36Sopenharmony_ci		*ib2 &= ~MTK_FOE_IB2_QID_V2;
46962306a36Sopenharmony_ci		*ib2 |= FIELD_PREP(MTK_FOE_IB2_QID_V2, queue);
47062306a36Sopenharmony_ci		*ib2 |= MTK_FOE_IB2_PSE_QOS_V2;
47162306a36Sopenharmony_ci	} else {
47262306a36Sopenharmony_ci		*ib2 &= ~MTK_FOE_IB2_QID;
47362306a36Sopenharmony_ci		*ib2 |= FIELD_PREP(MTK_FOE_IB2_QID, queue);
47462306a36Sopenharmony_ci		*ib2 |= MTK_FOE_IB2_PSE_QOS;
47562306a36Sopenharmony_ci	}
47662306a36Sopenharmony_ci
47762306a36Sopenharmony_ci	return 0;
47862306a36Sopenharmony_ci}
47962306a36Sopenharmony_ci
48062306a36Sopenharmony_cistatic bool
48162306a36Sopenharmony_cimtk_flow_entry_match(struct mtk_eth *eth, struct mtk_flow_entry *entry,
48262306a36Sopenharmony_ci		     struct mtk_foe_entry *data)
48362306a36Sopenharmony_ci{
48462306a36Sopenharmony_ci	int type, len;
48562306a36Sopenharmony_ci
48662306a36Sopenharmony_ci	if ((data->ib1 ^ entry->data.ib1) & MTK_FOE_IB1_UDP)
48762306a36Sopenharmony_ci		return false;
48862306a36Sopenharmony_ci
48962306a36Sopenharmony_ci	type = mtk_get_ib1_pkt_type(eth, entry->data.ib1);
49062306a36Sopenharmony_ci	if (type > MTK_PPE_PKT_TYPE_IPV4_DSLITE)
49162306a36Sopenharmony_ci		len = offsetof(struct mtk_foe_entry, ipv6._rsv);
49262306a36Sopenharmony_ci	else
49362306a36Sopenharmony_ci		len = offsetof(struct mtk_foe_entry, ipv4.ib2);
49462306a36Sopenharmony_ci
49562306a36Sopenharmony_ci	return !memcmp(&entry->data.data, &data->data, len - 4);
49662306a36Sopenharmony_ci}
49762306a36Sopenharmony_ci
49862306a36Sopenharmony_cistatic void
49962306a36Sopenharmony_ci__mtk_foe_entry_clear(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
50062306a36Sopenharmony_ci{
50162306a36Sopenharmony_ci	struct hlist_head *head;
50262306a36Sopenharmony_ci	struct hlist_node *tmp;
50362306a36Sopenharmony_ci
50462306a36Sopenharmony_ci	if (entry->type == MTK_FLOW_TYPE_L2) {
50562306a36Sopenharmony_ci		rhashtable_remove_fast(&ppe->l2_flows, &entry->l2_node,
50662306a36Sopenharmony_ci				       mtk_flow_l2_ht_params);
50762306a36Sopenharmony_ci
50862306a36Sopenharmony_ci		head = &entry->l2_flows;
50962306a36Sopenharmony_ci		hlist_for_each_entry_safe(entry, tmp, head, l2_data.list)
51062306a36Sopenharmony_ci			__mtk_foe_entry_clear(ppe, entry);
51162306a36Sopenharmony_ci		return;
51262306a36Sopenharmony_ci	}
51362306a36Sopenharmony_ci
51462306a36Sopenharmony_ci	hlist_del_init(&entry->list);
51562306a36Sopenharmony_ci	if (entry->hash != 0xffff) {
51662306a36Sopenharmony_ci		struct mtk_foe_entry *hwe = mtk_foe_get_entry(ppe, entry->hash);
51762306a36Sopenharmony_ci
51862306a36Sopenharmony_ci		hwe->ib1 &= ~MTK_FOE_IB1_STATE;
51962306a36Sopenharmony_ci		hwe->ib1 |= FIELD_PREP(MTK_FOE_IB1_STATE, MTK_FOE_STATE_INVALID);
52062306a36Sopenharmony_ci		dma_wmb();
52162306a36Sopenharmony_ci		mtk_ppe_cache_clear(ppe);
52262306a36Sopenharmony_ci
52362306a36Sopenharmony_ci		if (ppe->accounting) {
52462306a36Sopenharmony_ci			struct mtk_foe_accounting *acct;
52562306a36Sopenharmony_ci
52662306a36Sopenharmony_ci			acct = ppe->acct_table + entry->hash * sizeof(*acct);
52762306a36Sopenharmony_ci			acct->packets = 0;
52862306a36Sopenharmony_ci			acct->bytes = 0;
52962306a36Sopenharmony_ci		}
53062306a36Sopenharmony_ci	}
53162306a36Sopenharmony_ci	entry->hash = 0xffff;
53262306a36Sopenharmony_ci
53362306a36Sopenharmony_ci	if (entry->type != MTK_FLOW_TYPE_L2_SUBFLOW)
53462306a36Sopenharmony_ci		return;
53562306a36Sopenharmony_ci
53662306a36Sopenharmony_ci	hlist_del_init(&entry->l2_data.list);
53762306a36Sopenharmony_ci	kfree(entry);
53862306a36Sopenharmony_ci}
53962306a36Sopenharmony_ci
54062306a36Sopenharmony_cistatic int __mtk_foe_entry_idle_time(struct mtk_ppe *ppe, u32 ib1)
54162306a36Sopenharmony_ci{
54262306a36Sopenharmony_ci	u32 ib1_ts_mask = mtk_get_ib1_ts_mask(ppe->eth);
54362306a36Sopenharmony_ci	u16 now = mtk_eth_timestamp(ppe->eth);
54462306a36Sopenharmony_ci	u16 timestamp = ib1 & ib1_ts_mask;
54562306a36Sopenharmony_ci
54662306a36Sopenharmony_ci	if (timestamp > now)
54762306a36Sopenharmony_ci		return ib1_ts_mask + 1 - timestamp + now;
54862306a36Sopenharmony_ci	else
54962306a36Sopenharmony_ci		return now - timestamp;
55062306a36Sopenharmony_ci}
55162306a36Sopenharmony_ci
55262306a36Sopenharmony_cistatic void
55362306a36Sopenharmony_cimtk_flow_entry_update_l2(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
55462306a36Sopenharmony_ci{
55562306a36Sopenharmony_ci	u32 ib1_ts_mask = mtk_get_ib1_ts_mask(ppe->eth);
55662306a36Sopenharmony_ci	struct mtk_flow_entry *cur;
55762306a36Sopenharmony_ci	struct mtk_foe_entry *hwe;
55862306a36Sopenharmony_ci	struct hlist_node *tmp;
55962306a36Sopenharmony_ci	int idle;
56062306a36Sopenharmony_ci
56162306a36Sopenharmony_ci	idle = __mtk_foe_entry_idle_time(ppe, entry->data.ib1);
56262306a36Sopenharmony_ci	hlist_for_each_entry_safe(cur, tmp, &entry->l2_flows, l2_data.list) {
56362306a36Sopenharmony_ci		int cur_idle;
56462306a36Sopenharmony_ci		u32 ib1;
56562306a36Sopenharmony_ci
56662306a36Sopenharmony_ci		hwe = mtk_foe_get_entry(ppe, cur->hash);
56762306a36Sopenharmony_ci		ib1 = READ_ONCE(hwe->ib1);
56862306a36Sopenharmony_ci
56962306a36Sopenharmony_ci		if (FIELD_GET(MTK_FOE_IB1_STATE, ib1) != MTK_FOE_STATE_BIND) {
57062306a36Sopenharmony_ci			cur->hash = 0xffff;
57162306a36Sopenharmony_ci			__mtk_foe_entry_clear(ppe, cur);
57262306a36Sopenharmony_ci			continue;
57362306a36Sopenharmony_ci		}
57462306a36Sopenharmony_ci
57562306a36Sopenharmony_ci		cur_idle = __mtk_foe_entry_idle_time(ppe, ib1);
57662306a36Sopenharmony_ci		if (cur_idle >= idle)
57762306a36Sopenharmony_ci			continue;
57862306a36Sopenharmony_ci
57962306a36Sopenharmony_ci		idle = cur_idle;
58062306a36Sopenharmony_ci		entry->data.ib1 &= ~ib1_ts_mask;
58162306a36Sopenharmony_ci		entry->data.ib1 |= hwe->ib1 & ib1_ts_mask;
58262306a36Sopenharmony_ci	}
58362306a36Sopenharmony_ci}
58462306a36Sopenharmony_ci
58562306a36Sopenharmony_cistatic void
58662306a36Sopenharmony_cimtk_flow_entry_update(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
58762306a36Sopenharmony_ci{
58862306a36Sopenharmony_ci	struct mtk_foe_entry foe = {};
58962306a36Sopenharmony_ci	struct mtk_foe_entry *hwe;
59062306a36Sopenharmony_ci
59162306a36Sopenharmony_ci	spin_lock_bh(&ppe_lock);
59262306a36Sopenharmony_ci
59362306a36Sopenharmony_ci	if (entry->type == MTK_FLOW_TYPE_L2) {
59462306a36Sopenharmony_ci		mtk_flow_entry_update_l2(ppe, entry);
59562306a36Sopenharmony_ci		goto out;
59662306a36Sopenharmony_ci	}
59762306a36Sopenharmony_ci
59862306a36Sopenharmony_ci	if (entry->hash == 0xffff)
59962306a36Sopenharmony_ci		goto out;
60062306a36Sopenharmony_ci
60162306a36Sopenharmony_ci	hwe = mtk_foe_get_entry(ppe, entry->hash);
60262306a36Sopenharmony_ci	memcpy(&foe, hwe, ppe->eth->soc->foe_entry_size);
60362306a36Sopenharmony_ci	if (!mtk_flow_entry_match(ppe->eth, entry, &foe)) {
60462306a36Sopenharmony_ci		entry->hash = 0xffff;
60562306a36Sopenharmony_ci		goto out;
60662306a36Sopenharmony_ci	}
60762306a36Sopenharmony_ci
60862306a36Sopenharmony_ci	entry->data.ib1 = foe.ib1;
60962306a36Sopenharmony_ci
61062306a36Sopenharmony_ciout:
61162306a36Sopenharmony_ci	spin_unlock_bh(&ppe_lock);
61262306a36Sopenharmony_ci}
61362306a36Sopenharmony_ci
61462306a36Sopenharmony_cistatic void
61562306a36Sopenharmony_ci__mtk_foe_entry_commit(struct mtk_ppe *ppe, struct mtk_foe_entry *entry,
61662306a36Sopenharmony_ci		       u16 hash)
61762306a36Sopenharmony_ci{
61862306a36Sopenharmony_ci	struct mtk_eth *eth = ppe->eth;
61962306a36Sopenharmony_ci	u16 timestamp = mtk_eth_timestamp(eth);
62062306a36Sopenharmony_ci	struct mtk_foe_entry *hwe;
62162306a36Sopenharmony_ci	u32 val;
62262306a36Sopenharmony_ci
62362306a36Sopenharmony_ci	if (mtk_is_netsys_v2_or_greater(eth)) {
62462306a36Sopenharmony_ci		entry->ib1 &= ~MTK_FOE_IB1_BIND_TIMESTAMP_V2;
62562306a36Sopenharmony_ci		entry->ib1 |= FIELD_PREP(MTK_FOE_IB1_BIND_TIMESTAMP_V2,
62662306a36Sopenharmony_ci					 timestamp);
62762306a36Sopenharmony_ci	} else {
62862306a36Sopenharmony_ci		entry->ib1 &= ~MTK_FOE_IB1_BIND_TIMESTAMP;
62962306a36Sopenharmony_ci		entry->ib1 |= FIELD_PREP(MTK_FOE_IB1_BIND_TIMESTAMP,
63062306a36Sopenharmony_ci					 timestamp);
63162306a36Sopenharmony_ci	}
63262306a36Sopenharmony_ci
63362306a36Sopenharmony_ci	hwe = mtk_foe_get_entry(ppe, hash);
63462306a36Sopenharmony_ci	memcpy(&hwe->data, &entry->data, eth->soc->foe_entry_size - sizeof(hwe->ib1));
63562306a36Sopenharmony_ci	wmb();
63662306a36Sopenharmony_ci	hwe->ib1 = entry->ib1;
63762306a36Sopenharmony_ci
63862306a36Sopenharmony_ci	if (ppe->accounting) {
63962306a36Sopenharmony_ci		if (mtk_is_netsys_v2_or_greater(eth))
64062306a36Sopenharmony_ci			val = MTK_FOE_IB2_MIB_CNT_V2;
64162306a36Sopenharmony_ci		else
64262306a36Sopenharmony_ci			val = MTK_FOE_IB2_MIB_CNT;
64362306a36Sopenharmony_ci		*mtk_foe_entry_ib2(eth, hwe) |= val;
64462306a36Sopenharmony_ci	}
64562306a36Sopenharmony_ci
64662306a36Sopenharmony_ci	dma_wmb();
64762306a36Sopenharmony_ci
64862306a36Sopenharmony_ci	mtk_ppe_cache_clear(ppe);
64962306a36Sopenharmony_ci}
65062306a36Sopenharmony_ci
65162306a36Sopenharmony_civoid mtk_foe_entry_clear(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
65262306a36Sopenharmony_ci{
65362306a36Sopenharmony_ci	spin_lock_bh(&ppe_lock);
65462306a36Sopenharmony_ci	__mtk_foe_entry_clear(ppe, entry);
65562306a36Sopenharmony_ci	spin_unlock_bh(&ppe_lock);
65662306a36Sopenharmony_ci}
65762306a36Sopenharmony_ci
65862306a36Sopenharmony_cistatic int
65962306a36Sopenharmony_cimtk_foe_entry_commit_l2(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
66062306a36Sopenharmony_ci{
66162306a36Sopenharmony_ci	struct mtk_flow_entry *prev;
66262306a36Sopenharmony_ci
66362306a36Sopenharmony_ci	entry->type = MTK_FLOW_TYPE_L2;
66462306a36Sopenharmony_ci
66562306a36Sopenharmony_ci	prev = rhashtable_lookup_get_insert_fast(&ppe->l2_flows, &entry->l2_node,
66662306a36Sopenharmony_ci						 mtk_flow_l2_ht_params);
66762306a36Sopenharmony_ci	if (likely(!prev))
66862306a36Sopenharmony_ci		return 0;
66962306a36Sopenharmony_ci
67062306a36Sopenharmony_ci	if (IS_ERR(prev))
67162306a36Sopenharmony_ci		return PTR_ERR(prev);
67262306a36Sopenharmony_ci
67362306a36Sopenharmony_ci	return rhashtable_replace_fast(&ppe->l2_flows, &prev->l2_node,
67462306a36Sopenharmony_ci				       &entry->l2_node, mtk_flow_l2_ht_params);
67562306a36Sopenharmony_ci}
67662306a36Sopenharmony_ci
67762306a36Sopenharmony_ciint mtk_foe_entry_commit(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
67862306a36Sopenharmony_ci{
67962306a36Sopenharmony_ci	const struct mtk_soc_data *soc = ppe->eth->soc;
68062306a36Sopenharmony_ci	int type = mtk_get_ib1_pkt_type(ppe->eth, entry->data.ib1);
68162306a36Sopenharmony_ci	u32 hash;
68262306a36Sopenharmony_ci
68362306a36Sopenharmony_ci	if (type == MTK_PPE_PKT_TYPE_BRIDGE)
68462306a36Sopenharmony_ci		return mtk_foe_entry_commit_l2(ppe, entry);
68562306a36Sopenharmony_ci
68662306a36Sopenharmony_ci	hash = mtk_ppe_hash_entry(ppe->eth, &entry->data);
68762306a36Sopenharmony_ci	entry->hash = 0xffff;
68862306a36Sopenharmony_ci	spin_lock_bh(&ppe_lock);
68962306a36Sopenharmony_ci	hlist_add_head(&entry->list, &ppe->foe_flow[hash / soc->hash_offset]);
69062306a36Sopenharmony_ci	spin_unlock_bh(&ppe_lock);
69162306a36Sopenharmony_ci
69262306a36Sopenharmony_ci	return 0;
69362306a36Sopenharmony_ci}
69462306a36Sopenharmony_ci
69562306a36Sopenharmony_cistatic void
69662306a36Sopenharmony_cimtk_foe_entry_commit_subflow(struct mtk_ppe *ppe, struct mtk_flow_entry *entry,
69762306a36Sopenharmony_ci			     u16 hash)
69862306a36Sopenharmony_ci{
69962306a36Sopenharmony_ci	const struct mtk_soc_data *soc = ppe->eth->soc;
70062306a36Sopenharmony_ci	struct mtk_flow_entry *flow_info;
70162306a36Sopenharmony_ci	struct mtk_foe_entry foe = {}, *hwe;
70262306a36Sopenharmony_ci	struct mtk_foe_mac_info *l2;
70362306a36Sopenharmony_ci	u32 ib1_mask = mtk_get_ib1_pkt_type_mask(ppe->eth) | MTK_FOE_IB1_UDP;
70462306a36Sopenharmony_ci	int type;
70562306a36Sopenharmony_ci
70662306a36Sopenharmony_ci	flow_info = kzalloc(sizeof(*flow_info), GFP_ATOMIC);
70762306a36Sopenharmony_ci	if (!flow_info)
70862306a36Sopenharmony_ci		return;
70962306a36Sopenharmony_ci
71062306a36Sopenharmony_ci	flow_info->l2_data.base_flow = entry;
71162306a36Sopenharmony_ci	flow_info->type = MTK_FLOW_TYPE_L2_SUBFLOW;
71262306a36Sopenharmony_ci	flow_info->hash = hash;
71362306a36Sopenharmony_ci	hlist_add_head(&flow_info->list,
71462306a36Sopenharmony_ci		       &ppe->foe_flow[hash / soc->hash_offset]);
71562306a36Sopenharmony_ci	hlist_add_head(&flow_info->l2_data.list, &entry->l2_flows);
71662306a36Sopenharmony_ci
71762306a36Sopenharmony_ci	hwe = mtk_foe_get_entry(ppe, hash);
71862306a36Sopenharmony_ci	memcpy(&foe, hwe, soc->foe_entry_size);
71962306a36Sopenharmony_ci	foe.ib1 &= ib1_mask;
72062306a36Sopenharmony_ci	foe.ib1 |= entry->data.ib1 & ~ib1_mask;
72162306a36Sopenharmony_ci
72262306a36Sopenharmony_ci	l2 = mtk_foe_entry_l2(ppe->eth, &foe);
72362306a36Sopenharmony_ci	memcpy(l2, &entry->data.bridge.l2, sizeof(*l2));
72462306a36Sopenharmony_ci
72562306a36Sopenharmony_ci	type = mtk_get_ib1_pkt_type(ppe->eth, foe.ib1);
72662306a36Sopenharmony_ci	if (type == MTK_PPE_PKT_TYPE_IPV4_HNAPT)
72762306a36Sopenharmony_ci		memcpy(&foe.ipv4.new, &foe.ipv4.orig, sizeof(foe.ipv4.new));
72862306a36Sopenharmony_ci	else if (type >= MTK_PPE_PKT_TYPE_IPV6_ROUTE_3T && l2->etype == ETH_P_IP)
72962306a36Sopenharmony_ci		l2->etype = ETH_P_IPV6;
73062306a36Sopenharmony_ci
73162306a36Sopenharmony_ci	*mtk_foe_entry_ib2(ppe->eth, &foe) = entry->data.bridge.ib2;
73262306a36Sopenharmony_ci
73362306a36Sopenharmony_ci	__mtk_foe_entry_commit(ppe, &foe, hash);
73462306a36Sopenharmony_ci}
73562306a36Sopenharmony_ci
73662306a36Sopenharmony_civoid __mtk_ppe_check_skb(struct mtk_ppe *ppe, struct sk_buff *skb, u16 hash)
73762306a36Sopenharmony_ci{
73862306a36Sopenharmony_ci	const struct mtk_soc_data *soc = ppe->eth->soc;
73962306a36Sopenharmony_ci	struct hlist_head *head = &ppe->foe_flow[hash / soc->hash_offset];
74062306a36Sopenharmony_ci	struct mtk_foe_entry *hwe = mtk_foe_get_entry(ppe, hash);
74162306a36Sopenharmony_ci	struct mtk_flow_entry *entry;
74262306a36Sopenharmony_ci	struct mtk_foe_bridge key = {};
74362306a36Sopenharmony_ci	struct hlist_node *n;
74462306a36Sopenharmony_ci	struct ethhdr *eh;
74562306a36Sopenharmony_ci	bool found = false;
74662306a36Sopenharmony_ci	u8 *tag;
74762306a36Sopenharmony_ci
74862306a36Sopenharmony_ci	spin_lock_bh(&ppe_lock);
74962306a36Sopenharmony_ci
75062306a36Sopenharmony_ci	if (FIELD_GET(MTK_FOE_IB1_STATE, hwe->ib1) == MTK_FOE_STATE_BIND)
75162306a36Sopenharmony_ci		goto out;
75262306a36Sopenharmony_ci
75362306a36Sopenharmony_ci	hlist_for_each_entry_safe(entry, n, head, list) {
75462306a36Sopenharmony_ci		if (entry->type == MTK_FLOW_TYPE_L2_SUBFLOW) {
75562306a36Sopenharmony_ci			if (unlikely(FIELD_GET(MTK_FOE_IB1_STATE, hwe->ib1) ==
75662306a36Sopenharmony_ci				     MTK_FOE_STATE_BIND))
75762306a36Sopenharmony_ci				continue;
75862306a36Sopenharmony_ci
75962306a36Sopenharmony_ci			entry->hash = 0xffff;
76062306a36Sopenharmony_ci			__mtk_foe_entry_clear(ppe, entry);
76162306a36Sopenharmony_ci			continue;
76262306a36Sopenharmony_ci		}
76362306a36Sopenharmony_ci
76462306a36Sopenharmony_ci		if (found || !mtk_flow_entry_match(ppe->eth, entry, hwe)) {
76562306a36Sopenharmony_ci			if (entry->hash != 0xffff)
76662306a36Sopenharmony_ci				entry->hash = 0xffff;
76762306a36Sopenharmony_ci			continue;
76862306a36Sopenharmony_ci		}
76962306a36Sopenharmony_ci
77062306a36Sopenharmony_ci		entry->hash = hash;
77162306a36Sopenharmony_ci		__mtk_foe_entry_commit(ppe, &entry->data, hash);
77262306a36Sopenharmony_ci		found = true;
77362306a36Sopenharmony_ci	}
77462306a36Sopenharmony_ci
77562306a36Sopenharmony_ci	if (found)
77662306a36Sopenharmony_ci		goto out;
77762306a36Sopenharmony_ci
77862306a36Sopenharmony_ci	eh = eth_hdr(skb);
77962306a36Sopenharmony_ci	ether_addr_copy(key.dest_mac, eh->h_dest);
78062306a36Sopenharmony_ci	ether_addr_copy(key.src_mac, eh->h_source);
78162306a36Sopenharmony_ci	tag = skb->data - 2;
78262306a36Sopenharmony_ci	key.vlan = 0;
78362306a36Sopenharmony_ci	switch (skb->protocol) {
78462306a36Sopenharmony_ci#if IS_ENABLED(CONFIG_NET_DSA)
78562306a36Sopenharmony_ci	case htons(ETH_P_XDSA):
78662306a36Sopenharmony_ci		if (!netdev_uses_dsa(skb->dev) ||
78762306a36Sopenharmony_ci		    skb->dev->dsa_ptr->tag_ops->proto != DSA_TAG_PROTO_MTK)
78862306a36Sopenharmony_ci			goto out;
78962306a36Sopenharmony_ci
79062306a36Sopenharmony_ci		if (!skb_metadata_dst(skb))
79162306a36Sopenharmony_ci			tag += 4;
79262306a36Sopenharmony_ci
79362306a36Sopenharmony_ci		if (get_unaligned_be16(tag) != ETH_P_8021Q)
79462306a36Sopenharmony_ci			break;
79562306a36Sopenharmony_ci
79662306a36Sopenharmony_ci		fallthrough;
79762306a36Sopenharmony_ci#endif
79862306a36Sopenharmony_ci	case htons(ETH_P_8021Q):
79962306a36Sopenharmony_ci		key.vlan = get_unaligned_be16(tag + 2) & VLAN_VID_MASK;
80062306a36Sopenharmony_ci		break;
80162306a36Sopenharmony_ci	default:
80262306a36Sopenharmony_ci		break;
80362306a36Sopenharmony_ci	}
80462306a36Sopenharmony_ci
80562306a36Sopenharmony_ci	entry = rhashtable_lookup_fast(&ppe->l2_flows, &key, mtk_flow_l2_ht_params);
80662306a36Sopenharmony_ci	if (!entry)
80762306a36Sopenharmony_ci		goto out;
80862306a36Sopenharmony_ci
80962306a36Sopenharmony_ci	mtk_foe_entry_commit_subflow(ppe, entry, hash);
81062306a36Sopenharmony_ci
81162306a36Sopenharmony_ciout:
81262306a36Sopenharmony_ci	spin_unlock_bh(&ppe_lock);
81362306a36Sopenharmony_ci}
81462306a36Sopenharmony_ci
81562306a36Sopenharmony_ciint mtk_foe_entry_idle_time(struct mtk_ppe *ppe, struct mtk_flow_entry *entry)
81662306a36Sopenharmony_ci{
81762306a36Sopenharmony_ci	mtk_flow_entry_update(ppe, entry);
81862306a36Sopenharmony_ci
81962306a36Sopenharmony_ci	return __mtk_foe_entry_idle_time(ppe, entry->data.ib1);
82062306a36Sopenharmony_ci}
82162306a36Sopenharmony_ci
82262306a36Sopenharmony_ciint mtk_ppe_prepare_reset(struct mtk_ppe *ppe)
82362306a36Sopenharmony_ci{
82462306a36Sopenharmony_ci	if (!ppe)
82562306a36Sopenharmony_ci		return -EINVAL;
82662306a36Sopenharmony_ci
82762306a36Sopenharmony_ci	/* disable KA */
82862306a36Sopenharmony_ci	ppe_clear(ppe, MTK_PPE_TB_CFG, MTK_PPE_TB_CFG_KEEPALIVE);
82962306a36Sopenharmony_ci	ppe_clear(ppe, MTK_PPE_BIND_LMT1, MTK_PPE_NTU_KEEPALIVE);
83062306a36Sopenharmony_ci	ppe_w32(ppe, MTK_PPE_KEEPALIVE, 0);
83162306a36Sopenharmony_ci	usleep_range(10000, 11000);
83262306a36Sopenharmony_ci
83362306a36Sopenharmony_ci	/* set KA timer to maximum */
83462306a36Sopenharmony_ci	ppe_set(ppe, MTK_PPE_BIND_LMT1, MTK_PPE_NTU_KEEPALIVE);
83562306a36Sopenharmony_ci	ppe_w32(ppe, MTK_PPE_KEEPALIVE, 0xffffffff);
83662306a36Sopenharmony_ci
83762306a36Sopenharmony_ci	/* set KA tick select */
83862306a36Sopenharmony_ci	ppe_set(ppe, MTK_PPE_TB_CFG, MTK_PPE_TB_TICK_SEL);
83962306a36Sopenharmony_ci	ppe_set(ppe, MTK_PPE_TB_CFG, MTK_PPE_TB_CFG_KEEPALIVE);
84062306a36Sopenharmony_ci	usleep_range(10000, 11000);
84162306a36Sopenharmony_ci
84262306a36Sopenharmony_ci	/* disable scan mode */
84362306a36Sopenharmony_ci	ppe_clear(ppe, MTK_PPE_TB_CFG, MTK_PPE_TB_CFG_SCAN_MODE);
84462306a36Sopenharmony_ci	usleep_range(10000, 11000);
84562306a36Sopenharmony_ci
84662306a36Sopenharmony_ci	return mtk_ppe_wait_busy(ppe);
84762306a36Sopenharmony_ci}
84862306a36Sopenharmony_ci
84962306a36Sopenharmony_cistruct mtk_foe_accounting *mtk_foe_entry_get_mib(struct mtk_ppe *ppe, u32 index,
85062306a36Sopenharmony_ci						 struct mtk_foe_accounting *diff)
85162306a36Sopenharmony_ci{
85262306a36Sopenharmony_ci	struct mtk_foe_accounting *acct;
85362306a36Sopenharmony_ci	int size = sizeof(struct mtk_foe_accounting);
85462306a36Sopenharmony_ci	u64 bytes, packets;
85562306a36Sopenharmony_ci
85662306a36Sopenharmony_ci	if (!ppe->accounting)
85762306a36Sopenharmony_ci		return NULL;
85862306a36Sopenharmony_ci
85962306a36Sopenharmony_ci	if (mtk_mib_entry_read(ppe, index, &bytes, &packets))
86062306a36Sopenharmony_ci		return NULL;
86162306a36Sopenharmony_ci
86262306a36Sopenharmony_ci	acct = ppe->acct_table + index * size;
86362306a36Sopenharmony_ci
86462306a36Sopenharmony_ci	acct->bytes += bytes;
86562306a36Sopenharmony_ci	acct->packets += packets;
86662306a36Sopenharmony_ci
86762306a36Sopenharmony_ci	if (diff) {
86862306a36Sopenharmony_ci		diff->bytes = bytes;
86962306a36Sopenharmony_ci		diff->packets = packets;
87062306a36Sopenharmony_ci	}
87162306a36Sopenharmony_ci
87262306a36Sopenharmony_ci	return acct;
87362306a36Sopenharmony_ci}
87462306a36Sopenharmony_ci
87562306a36Sopenharmony_cistruct mtk_ppe *mtk_ppe_init(struct mtk_eth *eth, void __iomem *base, int index)
87662306a36Sopenharmony_ci{
87762306a36Sopenharmony_ci	bool accounting = eth->soc->has_accounting;
87862306a36Sopenharmony_ci	const struct mtk_soc_data *soc = eth->soc;
87962306a36Sopenharmony_ci	struct mtk_foe_accounting *acct;
88062306a36Sopenharmony_ci	struct device *dev = eth->dev;
88162306a36Sopenharmony_ci	struct mtk_mib_entry *mib;
88262306a36Sopenharmony_ci	struct mtk_ppe *ppe;
88362306a36Sopenharmony_ci	u32 foe_flow_size;
88462306a36Sopenharmony_ci	void *foe;
88562306a36Sopenharmony_ci
88662306a36Sopenharmony_ci	ppe = devm_kzalloc(dev, sizeof(*ppe), GFP_KERNEL);
88762306a36Sopenharmony_ci	if (!ppe)
88862306a36Sopenharmony_ci		return NULL;
88962306a36Sopenharmony_ci
89062306a36Sopenharmony_ci	rhashtable_init(&ppe->l2_flows, &mtk_flow_l2_ht_params);
89162306a36Sopenharmony_ci
89262306a36Sopenharmony_ci	/* need to allocate a separate device, since it PPE DMA access is
89362306a36Sopenharmony_ci	 * not coherent.
89462306a36Sopenharmony_ci	 */
89562306a36Sopenharmony_ci	ppe->base = base;
89662306a36Sopenharmony_ci	ppe->eth = eth;
89762306a36Sopenharmony_ci	ppe->dev = dev;
89862306a36Sopenharmony_ci	ppe->version = eth->soc->offload_version;
89962306a36Sopenharmony_ci	ppe->accounting = accounting;
90062306a36Sopenharmony_ci
90162306a36Sopenharmony_ci	foe = dmam_alloc_coherent(ppe->dev,
90262306a36Sopenharmony_ci				  MTK_PPE_ENTRIES * soc->foe_entry_size,
90362306a36Sopenharmony_ci				  &ppe->foe_phys, GFP_KERNEL);
90462306a36Sopenharmony_ci	if (!foe)
90562306a36Sopenharmony_ci		goto err_free_l2_flows;
90662306a36Sopenharmony_ci
90762306a36Sopenharmony_ci	ppe->foe_table = foe;
90862306a36Sopenharmony_ci
90962306a36Sopenharmony_ci	foe_flow_size = (MTK_PPE_ENTRIES / soc->hash_offset) *
91062306a36Sopenharmony_ci			sizeof(*ppe->foe_flow);
91162306a36Sopenharmony_ci	ppe->foe_flow = devm_kzalloc(dev, foe_flow_size, GFP_KERNEL);
91262306a36Sopenharmony_ci	if (!ppe->foe_flow)
91362306a36Sopenharmony_ci		goto err_free_l2_flows;
91462306a36Sopenharmony_ci
91562306a36Sopenharmony_ci	if (accounting) {
91662306a36Sopenharmony_ci		mib = dmam_alloc_coherent(ppe->dev, MTK_PPE_ENTRIES * sizeof(*mib),
91762306a36Sopenharmony_ci					  &ppe->mib_phys, GFP_KERNEL);
91862306a36Sopenharmony_ci		if (!mib)
91962306a36Sopenharmony_ci			return NULL;
92062306a36Sopenharmony_ci
92162306a36Sopenharmony_ci		ppe->mib_table = mib;
92262306a36Sopenharmony_ci
92362306a36Sopenharmony_ci		acct = devm_kzalloc(dev, MTK_PPE_ENTRIES * sizeof(*acct),
92462306a36Sopenharmony_ci				    GFP_KERNEL);
92562306a36Sopenharmony_ci
92662306a36Sopenharmony_ci		if (!acct)
92762306a36Sopenharmony_ci			return NULL;
92862306a36Sopenharmony_ci
92962306a36Sopenharmony_ci		ppe->acct_table = acct;
93062306a36Sopenharmony_ci	}
93162306a36Sopenharmony_ci
93262306a36Sopenharmony_ci	mtk_ppe_debugfs_init(ppe, index);
93362306a36Sopenharmony_ci
93462306a36Sopenharmony_ci	return ppe;
93562306a36Sopenharmony_ci
93662306a36Sopenharmony_cierr_free_l2_flows:
93762306a36Sopenharmony_ci	rhashtable_destroy(&ppe->l2_flows);
93862306a36Sopenharmony_ci	return NULL;
93962306a36Sopenharmony_ci}
94062306a36Sopenharmony_ci
94162306a36Sopenharmony_civoid mtk_ppe_deinit(struct mtk_eth *eth)
94262306a36Sopenharmony_ci{
94362306a36Sopenharmony_ci	int i;
94462306a36Sopenharmony_ci
94562306a36Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(eth->ppe); i++) {
94662306a36Sopenharmony_ci		if (!eth->ppe[i])
94762306a36Sopenharmony_ci			return;
94862306a36Sopenharmony_ci		rhashtable_destroy(&eth->ppe[i]->l2_flows);
94962306a36Sopenharmony_ci	}
95062306a36Sopenharmony_ci}
95162306a36Sopenharmony_ci
95262306a36Sopenharmony_cistatic void mtk_ppe_init_foe_table(struct mtk_ppe *ppe)
95362306a36Sopenharmony_ci{
95462306a36Sopenharmony_ci	static const u8 skip[] = { 12, 25, 38, 51, 76, 89, 102 };
95562306a36Sopenharmony_ci	int i, k;
95662306a36Sopenharmony_ci
95762306a36Sopenharmony_ci	memset(ppe->foe_table, 0,
95862306a36Sopenharmony_ci	       MTK_PPE_ENTRIES * ppe->eth->soc->foe_entry_size);
95962306a36Sopenharmony_ci
96062306a36Sopenharmony_ci	if (!IS_ENABLED(CONFIG_SOC_MT7621))
96162306a36Sopenharmony_ci		return;
96262306a36Sopenharmony_ci
96362306a36Sopenharmony_ci	/* skip all entries that cross the 1024 byte boundary */
96462306a36Sopenharmony_ci	for (i = 0; i < MTK_PPE_ENTRIES; i += 128) {
96562306a36Sopenharmony_ci		for (k = 0; k < ARRAY_SIZE(skip); k++) {
96662306a36Sopenharmony_ci			struct mtk_foe_entry *hwe;
96762306a36Sopenharmony_ci
96862306a36Sopenharmony_ci			hwe = mtk_foe_get_entry(ppe, i + skip[k]);
96962306a36Sopenharmony_ci			hwe->ib1 |= MTK_FOE_IB1_STATIC;
97062306a36Sopenharmony_ci		}
97162306a36Sopenharmony_ci	}
97262306a36Sopenharmony_ci}
97362306a36Sopenharmony_ci
97462306a36Sopenharmony_civoid mtk_ppe_start(struct mtk_ppe *ppe)
97562306a36Sopenharmony_ci{
97662306a36Sopenharmony_ci	u32 val;
97762306a36Sopenharmony_ci
97862306a36Sopenharmony_ci	if (!ppe)
97962306a36Sopenharmony_ci		return;
98062306a36Sopenharmony_ci
98162306a36Sopenharmony_ci	mtk_ppe_init_foe_table(ppe);
98262306a36Sopenharmony_ci	ppe_w32(ppe, MTK_PPE_TB_BASE, ppe->foe_phys);
98362306a36Sopenharmony_ci
98462306a36Sopenharmony_ci	val = MTK_PPE_TB_CFG_AGE_NON_L4 |
98562306a36Sopenharmony_ci	      MTK_PPE_TB_CFG_AGE_UNBIND |
98662306a36Sopenharmony_ci	      MTK_PPE_TB_CFG_AGE_TCP |
98762306a36Sopenharmony_ci	      MTK_PPE_TB_CFG_AGE_UDP |
98862306a36Sopenharmony_ci	      MTK_PPE_TB_CFG_AGE_TCP_FIN |
98962306a36Sopenharmony_ci	      FIELD_PREP(MTK_PPE_TB_CFG_SEARCH_MISS,
99062306a36Sopenharmony_ci			 MTK_PPE_SEARCH_MISS_ACTION_FORWARD_BUILD) |
99162306a36Sopenharmony_ci	      FIELD_PREP(MTK_PPE_TB_CFG_KEEPALIVE,
99262306a36Sopenharmony_ci			 MTK_PPE_KEEPALIVE_DISABLE) |
99362306a36Sopenharmony_ci	      FIELD_PREP(MTK_PPE_TB_CFG_HASH_MODE, 1) |
99462306a36Sopenharmony_ci	      FIELD_PREP(MTK_PPE_TB_CFG_SCAN_MODE,
99562306a36Sopenharmony_ci			 MTK_PPE_SCAN_MODE_CHECK_AGE) |
99662306a36Sopenharmony_ci	      FIELD_PREP(MTK_PPE_TB_CFG_ENTRY_NUM,
99762306a36Sopenharmony_ci			 MTK_PPE_ENTRIES_SHIFT);
99862306a36Sopenharmony_ci	if (mtk_is_netsys_v2_or_greater(ppe->eth))
99962306a36Sopenharmony_ci		val |= MTK_PPE_TB_CFG_INFO_SEL;
100062306a36Sopenharmony_ci	if (!mtk_is_netsys_v3_or_greater(ppe->eth))
100162306a36Sopenharmony_ci		val |= MTK_PPE_TB_CFG_ENTRY_80B;
100262306a36Sopenharmony_ci	ppe_w32(ppe, MTK_PPE_TB_CFG, val);
100362306a36Sopenharmony_ci
100462306a36Sopenharmony_ci	ppe_w32(ppe, MTK_PPE_IP_PROTO_CHK,
100562306a36Sopenharmony_ci		MTK_PPE_IP_PROTO_CHK_IPV4 | MTK_PPE_IP_PROTO_CHK_IPV6);
100662306a36Sopenharmony_ci
100762306a36Sopenharmony_ci	mtk_ppe_cache_enable(ppe, true);
100862306a36Sopenharmony_ci
100962306a36Sopenharmony_ci	val = MTK_PPE_FLOW_CFG_IP6_3T_ROUTE |
101062306a36Sopenharmony_ci	      MTK_PPE_FLOW_CFG_IP6_5T_ROUTE |
101162306a36Sopenharmony_ci	      MTK_PPE_FLOW_CFG_IP6_6RD |
101262306a36Sopenharmony_ci	      MTK_PPE_FLOW_CFG_IP4_NAT |
101362306a36Sopenharmony_ci	      MTK_PPE_FLOW_CFG_IP4_NAPT |
101462306a36Sopenharmony_ci	      MTK_PPE_FLOW_CFG_IP4_DSLITE |
101562306a36Sopenharmony_ci	      MTK_PPE_FLOW_CFG_IP4_NAT_FRAG;
101662306a36Sopenharmony_ci	if (mtk_is_netsys_v2_or_greater(ppe->eth))
101762306a36Sopenharmony_ci		val |= MTK_PPE_MD_TOAP_BYP_CRSN0 |
101862306a36Sopenharmony_ci		       MTK_PPE_MD_TOAP_BYP_CRSN1 |
101962306a36Sopenharmony_ci		       MTK_PPE_MD_TOAP_BYP_CRSN2 |
102062306a36Sopenharmony_ci		       MTK_PPE_FLOW_CFG_IP4_HASH_GRE_KEY;
102162306a36Sopenharmony_ci	else
102262306a36Sopenharmony_ci		val |= MTK_PPE_FLOW_CFG_IP4_TCP_FRAG |
102362306a36Sopenharmony_ci		       MTK_PPE_FLOW_CFG_IP4_UDP_FRAG;
102462306a36Sopenharmony_ci	ppe_w32(ppe, MTK_PPE_FLOW_CFG, val);
102562306a36Sopenharmony_ci
102662306a36Sopenharmony_ci	val = FIELD_PREP(MTK_PPE_UNBIND_AGE_MIN_PACKETS, 1000) |
102762306a36Sopenharmony_ci	      FIELD_PREP(MTK_PPE_UNBIND_AGE_DELTA, 3);
102862306a36Sopenharmony_ci	ppe_w32(ppe, MTK_PPE_UNBIND_AGE, val);
102962306a36Sopenharmony_ci
103062306a36Sopenharmony_ci	val = FIELD_PREP(MTK_PPE_BIND_AGE0_DELTA_UDP, 12) |
103162306a36Sopenharmony_ci	      FIELD_PREP(MTK_PPE_BIND_AGE0_DELTA_NON_L4, 1);
103262306a36Sopenharmony_ci	ppe_w32(ppe, MTK_PPE_BIND_AGE0, val);
103362306a36Sopenharmony_ci
103462306a36Sopenharmony_ci	val = FIELD_PREP(MTK_PPE_BIND_AGE1_DELTA_TCP_FIN, 1) |
103562306a36Sopenharmony_ci	      FIELD_PREP(MTK_PPE_BIND_AGE1_DELTA_TCP, 7);
103662306a36Sopenharmony_ci	ppe_w32(ppe, MTK_PPE_BIND_AGE1, val);
103762306a36Sopenharmony_ci
103862306a36Sopenharmony_ci	val = MTK_PPE_BIND_LIMIT0_QUARTER | MTK_PPE_BIND_LIMIT0_HALF;
103962306a36Sopenharmony_ci	ppe_w32(ppe, MTK_PPE_BIND_LIMIT0, val);
104062306a36Sopenharmony_ci
104162306a36Sopenharmony_ci	val = MTK_PPE_BIND_LIMIT1_FULL |
104262306a36Sopenharmony_ci	      FIELD_PREP(MTK_PPE_BIND_LIMIT1_NON_L4, 1);
104362306a36Sopenharmony_ci	ppe_w32(ppe, MTK_PPE_BIND_LIMIT1, val);
104462306a36Sopenharmony_ci
104562306a36Sopenharmony_ci	val = FIELD_PREP(MTK_PPE_BIND_RATE_BIND, 30) |
104662306a36Sopenharmony_ci	      FIELD_PREP(MTK_PPE_BIND_RATE_PREBIND, 1);
104762306a36Sopenharmony_ci	ppe_w32(ppe, MTK_PPE_BIND_RATE, val);
104862306a36Sopenharmony_ci
104962306a36Sopenharmony_ci	/* enable PPE */
105062306a36Sopenharmony_ci	val = MTK_PPE_GLO_CFG_EN |
105162306a36Sopenharmony_ci	      MTK_PPE_GLO_CFG_IP4_L4_CS_DROP |
105262306a36Sopenharmony_ci	      MTK_PPE_GLO_CFG_IP4_CS_DROP |
105362306a36Sopenharmony_ci	      MTK_PPE_GLO_CFG_FLOW_DROP_UPDATE;
105462306a36Sopenharmony_ci	ppe_w32(ppe, MTK_PPE_GLO_CFG, val);
105562306a36Sopenharmony_ci
105662306a36Sopenharmony_ci	ppe_w32(ppe, MTK_PPE_DEFAULT_CPU_PORT, 0);
105762306a36Sopenharmony_ci
105862306a36Sopenharmony_ci	if (mtk_is_netsys_v2_or_greater(ppe->eth)) {
105962306a36Sopenharmony_ci		ppe_w32(ppe, MTK_PPE_DEFAULT_CPU_PORT1, 0xcb777);
106062306a36Sopenharmony_ci		ppe_w32(ppe, MTK_PPE_SBW_CTRL, 0x7f);
106162306a36Sopenharmony_ci	}
106262306a36Sopenharmony_ci
106362306a36Sopenharmony_ci	if (ppe->accounting && ppe->mib_phys) {
106462306a36Sopenharmony_ci		ppe_w32(ppe, MTK_PPE_MIB_TB_BASE, ppe->mib_phys);
106562306a36Sopenharmony_ci		ppe_m32(ppe, MTK_PPE_MIB_CFG, MTK_PPE_MIB_CFG_EN,
106662306a36Sopenharmony_ci			MTK_PPE_MIB_CFG_EN);
106762306a36Sopenharmony_ci		ppe_m32(ppe, MTK_PPE_MIB_CFG, MTK_PPE_MIB_CFG_RD_CLR,
106862306a36Sopenharmony_ci			MTK_PPE_MIB_CFG_RD_CLR);
106962306a36Sopenharmony_ci		ppe_m32(ppe, MTK_PPE_MIB_CACHE_CTL, MTK_PPE_MIB_CACHE_CTL_EN,
107062306a36Sopenharmony_ci			MTK_PPE_MIB_CFG_RD_CLR);
107162306a36Sopenharmony_ci	}
107262306a36Sopenharmony_ci}
107362306a36Sopenharmony_ci
107462306a36Sopenharmony_ciint mtk_ppe_stop(struct mtk_ppe *ppe)
107562306a36Sopenharmony_ci{
107662306a36Sopenharmony_ci	u32 val;
107762306a36Sopenharmony_ci	int i;
107862306a36Sopenharmony_ci
107962306a36Sopenharmony_ci	if (!ppe)
108062306a36Sopenharmony_ci		return 0;
108162306a36Sopenharmony_ci
108262306a36Sopenharmony_ci	for (i = 0; i < MTK_PPE_ENTRIES; i++) {
108362306a36Sopenharmony_ci		struct mtk_foe_entry *hwe = mtk_foe_get_entry(ppe, i);
108462306a36Sopenharmony_ci
108562306a36Sopenharmony_ci		hwe->ib1 = FIELD_PREP(MTK_FOE_IB1_STATE,
108662306a36Sopenharmony_ci				      MTK_FOE_STATE_INVALID);
108762306a36Sopenharmony_ci	}
108862306a36Sopenharmony_ci
108962306a36Sopenharmony_ci	mtk_ppe_cache_enable(ppe, false);
109062306a36Sopenharmony_ci
109162306a36Sopenharmony_ci	/* disable aging */
109262306a36Sopenharmony_ci	val = MTK_PPE_TB_CFG_AGE_NON_L4 |
109362306a36Sopenharmony_ci	      MTK_PPE_TB_CFG_AGE_UNBIND |
109462306a36Sopenharmony_ci	      MTK_PPE_TB_CFG_AGE_TCP |
109562306a36Sopenharmony_ci	      MTK_PPE_TB_CFG_AGE_UDP |
109662306a36Sopenharmony_ci	      MTK_PPE_TB_CFG_AGE_TCP_FIN |
109762306a36Sopenharmony_ci		  MTK_PPE_TB_CFG_SCAN_MODE;
109862306a36Sopenharmony_ci	ppe_clear(ppe, MTK_PPE_TB_CFG, val);
109962306a36Sopenharmony_ci
110062306a36Sopenharmony_ci	if (mtk_ppe_wait_busy(ppe))
110162306a36Sopenharmony_ci		return -ETIMEDOUT;
110262306a36Sopenharmony_ci
110362306a36Sopenharmony_ci	/* disable offload engine */
110462306a36Sopenharmony_ci	ppe_clear(ppe, MTK_PPE_GLO_CFG, MTK_PPE_GLO_CFG_EN);
110562306a36Sopenharmony_ci	ppe_w32(ppe, MTK_PPE_FLOW_CFG, 0);
110662306a36Sopenharmony_ci
110762306a36Sopenharmony_ci	return 0;
110862306a36Sopenharmony_ci}
1109