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 #include <linux/rhashtable.h> 10 11 #define MTK_PPE_ENTRIES_SHIFT 3 12 #define MTK_PPE_ENTRIES (1024 << MTK_PPE_ENTRIES_SHIFT) 13 #define MTK_PPE_HASH_MASK (MTK_PPE_ENTRIES - 1) 14 #define MTK_PPE_WAIT_TIMEOUT_US 1000000 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 /* CONFIG_MEDIATEK_NETSYS_V2 */ 36 #define MTK_FOE_IB1_BIND_TIMESTAMP_V2 GENMASK(7, 0) 37 #define MTK_FOE_IB1_BIND_VLAN_LAYER_V2 GENMASK(16, 14) 38 #define MTK_FOE_IB1_BIND_PPPOE_V2 BIT(17) 39 #define MTK_FOE_IB1_BIND_VLAN_TAG_V2 BIT(18) 40 #define MTK_FOE_IB1_BIND_CACHE_V2 BIT(20) 41 #define MTK_FOE_IB1_BIND_TTL_V2 BIT(22) 42 #define MTK_FOE_IB1_PACKET_TYPE_V2 GENMASK(27, 23) 43 44 enum { 45 MTK_PPE_PKT_TYPE_IPV4_HNAPT = 0, 46 MTK_PPE_PKT_TYPE_IPV4_ROUTE = 1, 47 MTK_PPE_PKT_TYPE_BRIDGE = 2, 48 MTK_PPE_PKT_TYPE_IPV4_DSLITE = 3, 49 MTK_PPE_PKT_TYPE_IPV6_ROUTE_3T = 4, 50 MTK_PPE_PKT_TYPE_IPV6_ROUTE_5T = 5, 51 MTK_PPE_PKT_TYPE_IPV6_6RD = 7, 52 }; 53 54 #define MTK_FOE_IB2_QID GENMASK(3, 0) 55 #define MTK_FOE_IB2_PSE_QOS BIT(4) 56 #define MTK_FOE_IB2_DEST_PORT GENMASK(7, 5) 57 #define MTK_FOE_IB2_MULTICAST BIT(8) 58 #define MTK_FOE_IB2_MIB_CNT BIT(10) 59 60 #define MTK_FOE_IB2_WDMA_QID2 GENMASK(13, 12) 61 #define MTK_FOE_IB2_MIB_CNT_V2 BIT(15) 62 #define MTK_FOE_IB2_WDMA_DEVIDX BIT(16) 63 #define MTK_FOE_IB2_WDMA_WINFO BIT(17) 64 65 #define MTK_FOE_IB2_PORT_MG GENMASK(17, 12) 66 67 #define MTK_FOE_IB2_RX_IDX GENMASK(18, 17) 68 #define MTK_FOE_IB2_PORT_AG GENMASK(23, 18) 69 70 #define MTK_FOE_IB2_DSCP GENMASK(31, 24) 71 72 /* CONFIG_MEDIATEK_NETSYS_V2 */ 73 #define MTK_FOE_IB2_QID_V2 GENMASK(6, 0) 74 #define MTK_FOE_IB2_PORT_MG_V2 BIT(7) 75 #define MTK_FOE_IB2_PSE_QOS_V2 BIT(8) 76 #define MTK_FOE_IB2_DEST_PORT_V2 GENMASK(12, 9) 77 #define MTK_FOE_IB2_MULTICAST_V2 BIT(13) 78 #define MTK_FOE_IB2_WDMA_WINFO_V2 BIT(19) 79 #define MTK_FOE_IB2_PORT_AG_V2 GENMASK(23, 20) 80 81 #define MTK_FOE_VLAN2_WINFO_BSS GENMASK(5, 0) 82 #define MTK_FOE_VLAN2_WINFO_WCID GENMASK(13, 6) 83 #define MTK_FOE_VLAN2_WINFO_RING GENMASK(15, 14) 84 85 #define MTK_FOE_WINFO_BSS GENMASK(5, 0) 86 #define MTK_FOE_WINFO_WCID GENMASK(15, 6) 87 88 enum { 89 MTK_FOE_STATE_INVALID, 90 MTK_FOE_STATE_UNBIND, 91 MTK_FOE_STATE_BIND, 92 MTK_FOE_STATE_FIN 93 }; 94 95 struct mtk_foe_mac_info { 96 u16 vlan1; 97 u16 etype; 98 99 u32 dest_mac_hi; 100 101 u16 vlan2; 102 u16 dest_mac_lo; 103 104 u32 src_mac_hi; 105 106 u16 pppoe_id; 107 u16 src_mac_lo; 108 109 u16 minfo; 110 u16 winfo; 111 }; 112 113 /* software-only entry type */ 114 struct mtk_foe_bridge { 115 u8 dest_mac[ETH_ALEN]; 116 u8 src_mac[ETH_ALEN]; 117 u16 vlan; 118 119 struct {} key_end; 120 121 u32 ib2; 122 123 struct mtk_foe_mac_info l2; 124 }; 125 126 struct mtk_ipv4_tuple { 127 u32 src_ip; 128 u32 dest_ip; 129 union { 130 struct { 131 u16 dest_port; 132 u16 src_port; 133 }; 134 struct { 135 u8 protocol; 136 u8 _pad[3]; /* fill with 0xa5a5a5 */ 137 }; 138 u32 ports; 139 }; 140 }; 141 142 struct mtk_foe_ipv4 { 143 struct mtk_ipv4_tuple orig; 144 145 u32 ib2; 146 147 struct mtk_ipv4_tuple new; 148 149 u16 timestamp; 150 u16 _rsv0[3]; 151 152 u32 udf_tsid; 153 154 struct mtk_foe_mac_info l2; 155 }; 156 157 struct mtk_foe_ipv4_dslite { 158 struct mtk_ipv4_tuple ip4; 159 160 u32 tunnel_src_ip[4]; 161 u32 tunnel_dest_ip[4]; 162 163 u8 flow_label[3]; 164 u8 priority; 165 166 u32 udf_tsid; 167 168 u32 ib2; 169 170 struct mtk_foe_mac_info l2; 171 }; 172 173 struct mtk_foe_ipv6 { 174 u32 src_ip[4]; 175 u32 dest_ip[4]; 176 177 union { 178 struct { 179 u8 protocol; 180 u8 _pad[3]; /* fill with 0xa5a5a5 */ 181 }; /* 3-tuple */ 182 struct { 183 u16 dest_port; 184 u16 src_port; 185 }; /* 5-tuple */ 186 u32 ports; 187 }; 188 189 u32 _rsv[3]; 190 191 u32 udf; 192 193 u32 ib2; 194 struct mtk_foe_mac_info l2; 195 }; 196 197 struct mtk_foe_ipv6_6rd { 198 u32 src_ip[4]; 199 u32 dest_ip[4]; 200 u16 dest_port; 201 u16 src_port; 202 203 u32 tunnel_src_ip; 204 u32 tunnel_dest_ip; 205 206 u16 hdr_csum; 207 u8 dscp; 208 u8 ttl; 209 210 u8 flag; 211 u8 pad; 212 u8 per_flow_6rd_id; 213 u8 pad2; 214 215 u32 ib2; 216 struct mtk_foe_mac_info l2; 217 }; 218 219 #define MTK_FOE_ENTRY_V1_SIZE 80 220 #define MTK_FOE_ENTRY_V2_SIZE 96 221 222 struct mtk_foe_entry { 223 u32 ib1; 224 225 union { 226 struct mtk_foe_bridge bridge; 227 struct mtk_foe_ipv4 ipv4; 228 struct mtk_foe_ipv4_dslite dslite; 229 struct mtk_foe_ipv6 ipv6; 230 struct mtk_foe_ipv6_6rd ipv6_6rd; 231 u32 data[23]; 232 }; 233 }; 234 235 enum { 236 MTK_PPE_CPU_REASON_TTL_EXCEEDED = 0x02, 237 MTK_PPE_CPU_REASON_OPTION_HEADER = 0x03, 238 MTK_PPE_CPU_REASON_NO_FLOW = 0x07, 239 MTK_PPE_CPU_REASON_IPV4_FRAG = 0x08, 240 MTK_PPE_CPU_REASON_IPV4_DSLITE_FRAG = 0x09, 241 MTK_PPE_CPU_REASON_IPV4_DSLITE_NO_TCP_UDP = 0x0a, 242 MTK_PPE_CPU_REASON_IPV6_6RD_NO_TCP_UDP = 0x0b, 243 MTK_PPE_CPU_REASON_TCP_FIN_SYN_RST = 0x0c, 244 MTK_PPE_CPU_REASON_UN_HIT = 0x0d, 245 MTK_PPE_CPU_REASON_HIT_UNBIND = 0x0e, 246 MTK_PPE_CPU_REASON_HIT_UNBIND_RATE_REACHED = 0x0f, 247 MTK_PPE_CPU_REASON_HIT_BIND_TCP_FIN = 0x10, 248 MTK_PPE_CPU_REASON_HIT_TTL_1 = 0x11, 249 MTK_PPE_CPU_REASON_HIT_BIND_VLAN_VIOLATION = 0x12, 250 MTK_PPE_CPU_REASON_KEEPALIVE_UC_OLD_HDR = 0x13, 251 MTK_PPE_CPU_REASON_KEEPALIVE_MC_NEW_HDR = 0x14, 252 MTK_PPE_CPU_REASON_KEEPALIVE_DUP_OLD_HDR = 0x15, 253 MTK_PPE_CPU_REASON_HIT_BIND_FORCE_CPU = 0x16, 254 MTK_PPE_CPU_REASON_TUNNEL_OPTION_HEADER = 0x17, 255 MTK_PPE_CPU_REASON_MULTICAST_TO_CPU = 0x18, 256 MTK_PPE_CPU_REASON_MULTICAST_TO_GMAC1_CPU = 0x19, 257 MTK_PPE_CPU_REASON_HIT_PRE_BIND = 0x1a, 258 MTK_PPE_CPU_REASON_PACKET_SAMPLING = 0x1b, 259 MTK_PPE_CPU_REASON_EXCEED_MTU = 0x1c, 260 MTK_PPE_CPU_REASON_PPE_BYPASS = 0x1e, 261 MTK_PPE_CPU_REASON_INVALID = 0x1f, 262 }; 263 264 enum { 265 MTK_FLOW_TYPE_L4, 266 MTK_FLOW_TYPE_L2, 267 MTK_FLOW_TYPE_L2_SUBFLOW, 268 }; 269 270 struct mtk_flow_entry { 271 union { 272 struct hlist_node list; 273 struct { 274 struct rhash_head l2_node; 275 struct hlist_head l2_flows; 276 }; 277 }; 278 u8 type; 279 s8 wed_index; 280 u8 ppe_index; 281 u16 hash; 282 union { 283 struct mtk_foe_entry data; 284 struct { 285 struct mtk_flow_entry *base_flow; 286 struct hlist_node list; 287 } l2_data; 288 }; 289 struct rhash_head node; 290 unsigned long cookie; 291 }; 292 293 struct mtk_mib_entry { 294 u32 byt_cnt_l; 295 u16 byt_cnt_h; 296 u32 pkt_cnt_l; 297 u8 pkt_cnt_h; 298 u8 _rsv0; 299 u32 _rsv1; 300 } __packed; 301 302 struct mtk_foe_accounting { 303 u64 bytes; 304 u64 packets; 305 }; 306 307 struct mtk_ppe { 308 struct mtk_eth *eth; 309 struct device *dev; 310 void __iomem *base; 311 int version; 312 char dirname[5]; 313 bool accounting; 314 315 void *foe_table; 316 dma_addr_t foe_phys; 317 318 struct mtk_mib_entry *mib_table; 319 dma_addr_t mib_phys; 320 321 u16 foe_check_time[MTK_PPE_ENTRIES]; 322 struct hlist_head *foe_flow; 323 324 struct rhashtable l2_flows; 325 326 void *acct_table; 327 }; 328 329 struct mtk_ppe *mtk_ppe_init(struct mtk_eth *eth, void __iomem *base, int index); 330 331 void mtk_ppe_deinit(struct mtk_eth *eth); 332 void mtk_ppe_start(struct mtk_ppe *ppe); 333 int mtk_ppe_stop(struct mtk_ppe *ppe); 334 int mtk_ppe_prepare_reset(struct mtk_ppe *ppe); 335 336 void __mtk_ppe_check_skb(struct mtk_ppe *ppe, struct sk_buff *skb, u16 hash); 337 338 static inline void 339 mtk_ppe_check_skb(struct mtk_ppe *ppe, struct sk_buff *skb, u16 hash) 340 { 341 u16 now, diff; 342 343 if (!ppe) 344 return; 345 346 if (hash > MTK_PPE_HASH_MASK) 347 return; 348 349 now = (u16)jiffies; 350 diff = now - ppe->foe_check_time[hash]; 351 if (diff < HZ / 10) 352 return; 353 354 ppe->foe_check_time[hash] = now; 355 __mtk_ppe_check_skb(ppe, skb, hash); 356 } 357 358 int mtk_foe_entry_prepare(struct mtk_eth *eth, struct mtk_foe_entry *entry, 359 int type, int l4proto, u8 pse_port, u8 *src_mac, 360 u8 *dest_mac); 361 int mtk_foe_entry_set_pse_port(struct mtk_eth *eth, 362 struct mtk_foe_entry *entry, u8 port); 363 int mtk_foe_entry_set_ipv4_tuple(struct mtk_eth *eth, 364 struct mtk_foe_entry *entry, bool orig, 365 __be32 src_addr, __be16 src_port, 366 __be32 dest_addr, __be16 dest_port); 367 int mtk_foe_entry_set_ipv6_tuple(struct mtk_eth *eth, 368 struct mtk_foe_entry *entry, 369 __be32 *src_addr, __be16 src_port, 370 __be32 *dest_addr, __be16 dest_port); 371 int mtk_foe_entry_set_dsa(struct mtk_eth *eth, struct mtk_foe_entry *entry, 372 int port); 373 int mtk_foe_entry_set_vlan(struct mtk_eth *eth, struct mtk_foe_entry *entry, 374 int vid); 375 int mtk_foe_entry_set_pppoe(struct mtk_eth *eth, struct mtk_foe_entry *entry, 376 int sid); 377 int mtk_foe_entry_set_wdma(struct mtk_eth *eth, struct mtk_foe_entry *entry, 378 int wdma_idx, int txq, int bss, int wcid); 379 int mtk_foe_entry_set_queue(struct mtk_eth *eth, struct mtk_foe_entry *entry, 380 unsigned int queue); 381 int mtk_foe_entry_commit(struct mtk_ppe *ppe, struct mtk_flow_entry *entry); 382 void mtk_foe_entry_clear(struct mtk_ppe *ppe, struct mtk_flow_entry *entry); 383 int mtk_foe_entry_idle_time(struct mtk_ppe *ppe, struct mtk_flow_entry *entry); 384 int mtk_ppe_debugfs_init(struct mtk_ppe *ppe, int index); 385 struct mtk_foe_accounting *mtk_foe_entry_get_mib(struct mtk_ppe *ppe, u32 index, 386 struct mtk_foe_accounting *diff); 387 388 #endif 389