b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* Copyright (C) 2020 Felix Fietkau <nbd@nbd.name> */ |
| 3 | |
| 4 | #ifndef __MTK_PPE_H |
| 5 | #define __MTK_PPE_H |
| 6 | |
| 7 | #include <linux/kernel.h> |
| 8 | #include <linux/bitfield.h> |
| 9 | |
| 10 | #define MTK_ETH_PPE_BASE 0xc00 |
| 11 | |
| 12 | #define MTK_PPE_ENTRIES_SHIFT 3 |
| 13 | #define MTK_PPE_ENTRIES (1024 << MTK_PPE_ENTRIES_SHIFT) |
| 14 | #define MTK_PPE_HASH_MASK (MTK_PPE_ENTRIES - 1) |
| 15 | |
| 16 | #define MTK_FOE_IB1_UNBIND_TIMESTAMP GENMASK(7, 0) |
| 17 | #define MTK_FOE_IB1_UNBIND_PACKETS GENMASK(23, 8) |
| 18 | #define MTK_FOE_IB1_UNBIND_PREBIND BIT(24) |
| 19 | |
| 20 | #define MTK_FOE_IB1_BIND_TIMESTAMP GENMASK(14, 0) |
| 21 | #define MTK_FOE_IB1_BIND_KEEPALIVE BIT(15) |
| 22 | #define MTK_FOE_IB1_BIND_VLAN_LAYER GENMASK(18, 16) |
| 23 | #define MTK_FOE_IB1_BIND_PPPOE BIT(19) |
| 24 | #define MTK_FOE_IB1_BIND_VLAN_TAG BIT(20) |
| 25 | #define MTK_FOE_IB1_BIND_PKT_SAMPLE BIT(21) |
| 26 | #define MTK_FOE_IB1_BIND_CACHE BIT(22) |
| 27 | #define MTK_FOE_IB1_BIND_TUNNEL_DECAP BIT(23) |
| 28 | #define MTK_FOE_IB1_BIND_TTL BIT(24) |
| 29 | |
| 30 | #define MTK_FOE_IB1_PACKET_TYPE GENMASK(27, 25) |
| 31 | #define MTK_FOE_IB1_STATE GENMASK(29, 28) |
| 32 | #define MTK_FOE_IB1_UDP BIT(30) |
| 33 | #define MTK_FOE_IB1_STATIC BIT(31) |
| 34 | |
| 35 | enum { |
| 36 | MTK_PPE_PKT_TYPE_IPV4_HNAPT = 0, |
| 37 | MTK_PPE_PKT_TYPE_IPV4_ROUTE = 1, |
| 38 | MTK_PPE_PKT_TYPE_BRIDGE = 2, |
| 39 | MTK_PPE_PKT_TYPE_IPV4_DSLITE = 3, |
| 40 | MTK_PPE_PKT_TYPE_IPV6_ROUTE_3T = 4, |
| 41 | MTK_PPE_PKT_TYPE_IPV6_ROUTE_5T = 5, |
| 42 | MTK_PPE_PKT_TYPE_IPV6_6RD = 7, |
| 43 | }; |
| 44 | |
| 45 | #define MTK_FOE_IB2_QID GENMASK(3, 0) |
| 46 | #define MTK_FOE_IB2_PSE_QOS BIT(4) |
| 47 | #define MTK_FOE_IB2_DEST_PORT GENMASK(7, 5) |
| 48 | #define MTK_FOE_IB2_MULTICAST BIT(8) |
| 49 | |
| 50 | #define MTK_FOE_IB2_WHNAT_QID2 GENMASK(13, 12) |
| 51 | #define MTK_FOE_IB2_WHNAT_DEVIDX BIT(16) |
| 52 | #define MTK_FOE_IB2_WHNAT_NAT BIT(17) |
| 53 | |
| 54 | #define MTK_FOE_IB2_PORT_MG GENMASK(17, 12) |
| 55 | |
| 56 | #define MTK_FOE_IB2_PORT_AG GENMASK(23, 18) |
| 57 | |
| 58 | #define MTK_FOE_IB2_DSCP GENMASK(31, 24) |
| 59 | |
| 60 | #define MTK_FOE_VLAN2_WHNAT_BSS GEMMASK(5, 0) |
| 61 | #define MTK_FOE_VLAN2_WHNAT_WCID GENMASK(13, 6) |
| 62 | #define MTK_FOE_VLAN2_WHNAT_RING GENMASK(15, 14) |
| 63 | |
| 64 | enum { |
| 65 | MTK_FOE_STATE_INVALID, |
| 66 | MTK_FOE_STATE_UNBIND, |
| 67 | MTK_FOE_STATE_BIND, |
| 68 | MTK_FOE_STATE_FIN |
| 69 | }; |
| 70 | |
| 71 | struct mtk_foe_mac_info { |
| 72 | u16 vlan1; |
| 73 | u16 etype; |
| 74 | |
| 75 | u32 dest_mac_hi; |
| 76 | |
| 77 | u16 vlan2; |
| 78 | u16 dest_mac_lo; |
| 79 | |
| 80 | u32 src_mac_hi; |
| 81 | |
| 82 | u16 pppoe_id; |
| 83 | u16 src_mac_lo; |
| 84 | }; |
| 85 | |
| 86 | struct mtk_foe_bridge { |
| 87 | u32 dest_mac_hi; |
| 88 | |
| 89 | u16 src_mac_lo; |
| 90 | u16 dest_mac_lo; |
| 91 | |
| 92 | u32 src_mac_hi; |
| 93 | |
| 94 | u32 ib2; |
| 95 | |
| 96 | u32 _rsv[5]; |
| 97 | |
| 98 | u32 udf_tsid; |
| 99 | struct mtk_foe_mac_info l2; |
| 100 | }; |
| 101 | |
| 102 | struct mtk_ipv4_tuple { |
| 103 | u32 src_ip; |
| 104 | u32 dest_ip; |
| 105 | union { |
| 106 | struct { |
| 107 | u16 dest_port; |
| 108 | u16 src_port; |
| 109 | }; |
| 110 | struct { |
| 111 | u8 protocol; |
| 112 | u8 _pad[3]; /* fill with 0xa5a5a5 */ |
| 113 | }; |
| 114 | u32 ports; |
| 115 | }; |
| 116 | }; |
| 117 | |
| 118 | struct mtk_foe_ipv4 { |
| 119 | struct mtk_ipv4_tuple orig; |
| 120 | |
| 121 | u32 ib2; |
| 122 | |
| 123 | struct mtk_ipv4_tuple new; |
| 124 | |
| 125 | u16 timestamp; |
| 126 | u16 _rsv0[3]; |
| 127 | |
| 128 | u32 udf_tsid; |
| 129 | |
| 130 | struct mtk_foe_mac_info l2; |
| 131 | }; |
| 132 | |
| 133 | struct mtk_foe_ipv4_dslite { |
| 134 | struct mtk_ipv4_tuple ip4; |
| 135 | |
| 136 | u32 tunnel_src_ip[4]; |
| 137 | u32 tunnel_dest_ip[4]; |
| 138 | |
| 139 | u8 flow_label[3]; |
| 140 | u8 priority; |
| 141 | |
| 142 | u32 udf_tsid; |
| 143 | |
| 144 | u32 ib2; |
| 145 | |
| 146 | struct mtk_foe_mac_info l2; |
| 147 | }; |
| 148 | |
| 149 | struct mtk_foe_ipv6 { |
| 150 | u32 src_ip[4]; |
| 151 | u32 dest_ip[4]; |
| 152 | |
| 153 | union { |
| 154 | struct { |
| 155 | u8 protocol; |
| 156 | u8 _pad[3]; /* fill with 0xa5a5a5 */ |
| 157 | }; /* 3-tuple */ |
| 158 | struct { |
| 159 | u16 dest_port; |
| 160 | u16 src_port; |
| 161 | }; /* 5-tuple */ |
| 162 | u32 ports; |
| 163 | }; |
| 164 | |
| 165 | u32 _rsv[3]; |
| 166 | |
| 167 | u32 udf; |
| 168 | |
| 169 | u32 ib2; |
| 170 | struct mtk_foe_mac_info l2; |
| 171 | }; |
| 172 | |
| 173 | struct mtk_foe_ipv6_6rd { |
| 174 | u32 src_ip[4]; |
| 175 | u32 dest_ip[4]; |
| 176 | u16 dest_port; |
| 177 | u16 src_port; |
| 178 | |
| 179 | u32 tunnel_src_ip; |
| 180 | u32 tunnel_dest_ip; |
| 181 | |
| 182 | u16 hdr_csum; |
| 183 | u8 dscp; |
| 184 | u8 ttl; |
| 185 | |
| 186 | u8 flag; |
| 187 | u8 pad; |
| 188 | u8 per_flow_6rd_id; |
| 189 | u8 pad2; |
| 190 | |
| 191 | u32 ib2; |
| 192 | struct mtk_foe_mac_info l2; |
| 193 | }; |
| 194 | |
| 195 | struct mtk_foe_entry { |
| 196 | u32 ib1; |
| 197 | |
| 198 | union { |
| 199 | struct mtk_foe_bridge bridge; |
| 200 | struct mtk_foe_ipv4 ipv4; |
| 201 | struct mtk_foe_ipv4_dslite dslite; |
| 202 | struct mtk_foe_ipv6 ipv6; |
| 203 | struct mtk_foe_ipv6_6rd ipv6_6rd; |
| 204 | u32 data[19]; |
| 205 | }; |
| 206 | }; |
| 207 | |
| 208 | enum { |
| 209 | MTK_PPE_CPU_REASON_TTL_EXCEEDED = 0x02, |
| 210 | MTK_PPE_CPU_REASON_OPTION_HEADER = 0x03, |
| 211 | MTK_PPE_CPU_REASON_NO_FLOW = 0x07, |
| 212 | MTK_PPE_CPU_REASON_IPV4_FRAG = 0x08, |
| 213 | MTK_PPE_CPU_REASON_IPV4_DSLITE_FRAG = 0x09, |
| 214 | MTK_PPE_CPU_REASON_IPV4_DSLITE_NO_TCP_UDP = 0x0a, |
| 215 | MTK_PPE_CPU_REASON_IPV6_6RD_NO_TCP_UDP = 0x0b, |
| 216 | MTK_PPE_CPU_REASON_TCP_FIN_SYN_RST = 0x0c, |
| 217 | MTK_PPE_CPU_REASON_UN_HIT = 0x0d, |
| 218 | MTK_PPE_CPU_REASON_HIT_UNBIND = 0x0e, |
| 219 | MTK_PPE_CPU_REASON_HIT_UNBIND_RATE_REACHED = 0x0f, |
| 220 | MTK_PPE_CPU_REASON_HIT_BIND_TCP_FIN = 0x10, |
| 221 | MTK_PPE_CPU_REASON_HIT_TTL_1 = 0x11, |
| 222 | MTK_PPE_CPU_REASON_HIT_BIND_VLAN_VIOLATION = 0x12, |
| 223 | MTK_PPE_CPU_REASON_KEEPALIVE_UC_OLD_HDR = 0x13, |
| 224 | MTK_PPE_CPU_REASON_KEEPALIVE_MC_NEW_HDR = 0x14, |
| 225 | MTK_PPE_CPU_REASON_KEEPALIVE_DUP_OLD_HDR = 0x15, |
| 226 | MTK_PPE_CPU_REASON_HIT_BIND_FORCE_CPU = 0x16, |
| 227 | MTK_PPE_CPU_REASON_TUNNEL_OPTION_HEADER = 0x17, |
| 228 | MTK_PPE_CPU_REASON_MULTICAST_TO_CPU = 0x18, |
| 229 | MTK_PPE_CPU_REASON_MULTICAST_TO_GMAC1_CPU = 0x19, |
| 230 | MTK_PPE_CPU_REASON_HIT_PRE_BIND = 0x1a, |
| 231 | MTK_PPE_CPU_REASON_PACKET_SAMPLING = 0x1b, |
| 232 | MTK_PPE_CPU_REASON_EXCEED_MTU = 0x1c, |
| 233 | MTK_PPE_CPU_REASON_PPE_BYPASS = 0x1e, |
| 234 | MTK_PPE_CPU_REASON_INVALID = 0x1f, |
| 235 | }; |
| 236 | |
| 237 | struct mtk_ppe { |
| 238 | struct device *dev; |
| 239 | void __iomem *base; |
| 240 | int version; |
| 241 | |
| 242 | struct mtk_foe_entry *foe_table; |
| 243 | dma_addr_t foe_phys; |
| 244 | |
| 245 | void *acct_table; |
| 246 | }; |
| 247 | |
| 248 | int mtk_ppe_init(struct mtk_ppe *ppe, struct device *dev, void __iomem *base, |
| 249 | int version); |
| 250 | int mtk_ppe_start(struct mtk_ppe *ppe); |
| 251 | int mtk_ppe_stop(struct mtk_ppe *ppe); |
| 252 | |
| 253 | static inline void |
| 254 | mtk_foe_entry_clear(struct mtk_ppe *ppe, u16 hash) |
| 255 | { |
| 256 | ppe->foe_table[hash].ib1 = 0; |
| 257 | dma_wmb(); |
| 258 | } |
| 259 | |
| 260 | int mtk_foe_entry_prepare(struct mtk_foe_entry *entry, int type, int l4proto, |
| 261 | u8 pse_port, u8 *src_mac, u8 *dest_mac); |
| 262 | int mtk_foe_entry_set_ipv4_tuple(struct mtk_foe_entry *entry, bool orig, |
| 263 | __be32 src_addr, __be16 src_port, |
| 264 | __be32 dest_addr, __be16 dest_port); |
| 265 | int mtk_foe_entry_set_ipv6_tuple(struct mtk_foe_entry *entry, |
| 266 | __be32 *src_addr, __be16 src_port, |
| 267 | __be32 *dest_addr, __be16 dest_port); |
| 268 | int mtk_foe_entry_set_dsa(struct mtk_foe_entry *entry, int port); |
| 269 | int mtk_foe_entry_set_vlan(struct mtk_foe_entry *entry, int vid); |
| 270 | int mtk_foe_entry_set_pppoe(struct mtk_foe_entry *entry, int sid); |
| 271 | int mtk_foe_entry_commit(struct mtk_ppe *ppe, struct mtk_foe_entry *entry, |
| 272 | u16 timestamp); |
| 273 | |
| 274 | /* internal */ |
| 275 | int mtk_ppe_debugfs_init(struct mtk_ppe *ppe); |
| 276 | |
| 277 | #endif |