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 struct mtk_foe_entry {
220 	u32 ib1;
221 
222 	union {
223 		struct mtk_foe_bridge bridge;
224 		struct mtk_foe_ipv4 ipv4;
225 		struct mtk_foe_ipv4_dslite dslite;
226 		struct mtk_foe_ipv6 ipv6;
227 		struct mtk_foe_ipv6_6rd ipv6_6rd;
228 		u32 data[23];
229 	};
230 };
231 
232 enum {
233 	MTK_PPE_CPU_REASON_TTL_EXCEEDED			= 0x02,
234 	MTK_PPE_CPU_REASON_OPTION_HEADER		= 0x03,
235 	MTK_PPE_CPU_REASON_NO_FLOW			= 0x07,
236 	MTK_PPE_CPU_REASON_IPV4_FRAG			= 0x08,
237 	MTK_PPE_CPU_REASON_IPV4_DSLITE_FRAG		= 0x09,
238 	MTK_PPE_CPU_REASON_IPV4_DSLITE_NO_TCP_UDP	= 0x0a,
239 	MTK_PPE_CPU_REASON_IPV6_6RD_NO_TCP_UDP		= 0x0b,
240 	MTK_PPE_CPU_REASON_TCP_FIN_SYN_RST		= 0x0c,
241 	MTK_PPE_CPU_REASON_UN_HIT			= 0x0d,
242 	MTK_PPE_CPU_REASON_HIT_UNBIND			= 0x0e,
243 	MTK_PPE_CPU_REASON_HIT_UNBIND_RATE_REACHED	= 0x0f,
244 	MTK_PPE_CPU_REASON_HIT_BIND_TCP_FIN		= 0x10,
245 	MTK_PPE_CPU_REASON_HIT_TTL_1			= 0x11,
246 	MTK_PPE_CPU_REASON_HIT_BIND_VLAN_VIOLATION	= 0x12,
247 	MTK_PPE_CPU_REASON_KEEPALIVE_UC_OLD_HDR		= 0x13,
248 	MTK_PPE_CPU_REASON_KEEPALIVE_MC_NEW_HDR		= 0x14,
249 	MTK_PPE_CPU_REASON_KEEPALIVE_DUP_OLD_HDR	= 0x15,
250 	MTK_PPE_CPU_REASON_HIT_BIND_FORCE_CPU		= 0x16,
251 	MTK_PPE_CPU_REASON_TUNNEL_OPTION_HEADER		= 0x17,
252 	MTK_PPE_CPU_REASON_MULTICAST_TO_CPU		= 0x18,
253 	MTK_PPE_CPU_REASON_MULTICAST_TO_GMAC1_CPU	= 0x19,
254 	MTK_PPE_CPU_REASON_HIT_PRE_BIND			= 0x1a,
255 	MTK_PPE_CPU_REASON_PACKET_SAMPLING		= 0x1b,
256 	MTK_PPE_CPU_REASON_EXCEED_MTU			= 0x1c,
257 	MTK_PPE_CPU_REASON_PPE_BYPASS			= 0x1e,
258 	MTK_PPE_CPU_REASON_INVALID			= 0x1f,
259 };
260 
261 enum {
262 	MTK_FLOW_TYPE_L4,
263 	MTK_FLOW_TYPE_L2,
264 	MTK_FLOW_TYPE_L2_SUBFLOW,
265 };
266 
267 struct mtk_flow_entry {
268 	union {
269 		struct hlist_node list;
270 		struct {
271 			struct rhash_head l2_node;
272 			struct hlist_head l2_flows;
273 		};
274 	};
275 	u8 type;
276 	s8 wed_index;
277 	u8 ppe_index;
278 	u16 hash;
279 	union {
280 		struct mtk_foe_entry data;
281 		struct {
282 			struct mtk_flow_entry *base_flow;
283 			struct hlist_node list;
284 		} l2_data;
285 	};
286 	struct rhash_head node;
287 	unsigned long cookie;
288 };
289 
290 struct mtk_mib_entry {
291 	u32	byt_cnt_l;
292 	u16	byt_cnt_h;
293 	u32	pkt_cnt_l;
294 	u8	pkt_cnt_h;
295 	u8	_rsv0;
296 	u32	_rsv1;
297 } __packed;
298 
299 struct mtk_foe_accounting {
300 	u64	bytes;
301 	u64	packets;
302 };
303 
304 struct mtk_ppe {
305 	struct mtk_eth *eth;
306 	struct device *dev;
307 	void __iomem *base;
308 	int version;
309 	char dirname[5];
310 	bool accounting;
311 
312 	void *foe_table;
313 	dma_addr_t foe_phys;
314 
315 	struct mtk_mib_entry *mib_table;
316 	dma_addr_t mib_phys;
317 
318 	u16 foe_check_time[MTK_PPE_ENTRIES];
319 	struct hlist_head *foe_flow;
320 
321 	struct rhashtable l2_flows;
322 
323 	void *acct_table;
324 };
325 
326 struct mtk_ppe *mtk_ppe_init(struct mtk_eth *eth, void __iomem *base, int index);
327 
328 void mtk_ppe_deinit(struct mtk_eth *eth);
329 void mtk_ppe_start(struct mtk_ppe *ppe);
330 int mtk_ppe_stop(struct mtk_ppe *ppe);
331 int mtk_ppe_prepare_reset(struct mtk_ppe *ppe);
332 
333 void __mtk_ppe_check_skb(struct mtk_ppe *ppe, struct sk_buff *skb, u16 hash);
334 
335 static inline void
336 mtk_ppe_check_skb(struct mtk_ppe *ppe, struct sk_buff *skb, u16 hash)
337 {
338 	u16 now, diff;
339 
340 	if (!ppe)
341 		return;
342 
343 	if (hash > MTK_PPE_HASH_MASK)
344 		return;
345 
346 	now = (u16)jiffies;
347 	diff = now - ppe->foe_check_time[hash];
348 	if (diff < HZ / 10)
349 		return;
350 
351 	ppe->foe_check_time[hash] = now;
352 	__mtk_ppe_check_skb(ppe, skb, hash);
353 }
354 
355 int mtk_foe_entry_prepare(struct mtk_eth *eth, struct mtk_foe_entry *entry,
356 			  int type, int l4proto, u8 pse_port, u8 *src_mac,
357 			  u8 *dest_mac);
358 int mtk_foe_entry_set_pse_port(struct mtk_eth *eth,
359 			       struct mtk_foe_entry *entry, u8 port);
360 int mtk_foe_entry_set_ipv4_tuple(struct mtk_eth *eth,
361 				 struct mtk_foe_entry *entry, bool orig,
362 				 __be32 src_addr, __be16 src_port,
363 				 __be32 dest_addr, __be16 dest_port);
364 int mtk_foe_entry_set_ipv6_tuple(struct mtk_eth *eth,
365 				 struct mtk_foe_entry *entry,
366 				 __be32 *src_addr, __be16 src_port,
367 				 __be32 *dest_addr, __be16 dest_port);
368 int mtk_foe_entry_set_dsa(struct mtk_eth *eth, struct mtk_foe_entry *entry,
369 			  int port);
370 int mtk_foe_entry_set_vlan(struct mtk_eth *eth, struct mtk_foe_entry *entry,
371 			   int vid);
372 int mtk_foe_entry_set_pppoe(struct mtk_eth *eth, struct mtk_foe_entry *entry,
373 			    int sid);
374 int mtk_foe_entry_set_wdma(struct mtk_eth *eth, struct mtk_foe_entry *entry,
375 			   int wdma_idx, int txq, int bss, int wcid);
376 int mtk_foe_entry_set_queue(struct mtk_eth *eth, struct mtk_foe_entry *entry,
377 			    unsigned int queue);
378 int mtk_foe_entry_commit(struct mtk_ppe *ppe, struct mtk_flow_entry *entry);
379 void mtk_foe_entry_clear(struct mtk_ppe *ppe, struct mtk_flow_entry *entry);
380 int mtk_foe_entry_idle_time(struct mtk_ppe *ppe, struct mtk_flow_entry *entry);
381 int mtk_ppe_debugfs_init(struct mtk_ppe *ppe, int index);
382 struct mtk_foe_accounting *mtk_foe_entry_get_mib(struct mtk_ppe *ppe, u32 index,
383 						 struct mtk_foe_accounting *diff);
384 
385 #endif
386