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