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		4
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 #define MTK_FOE_WINFO_BSS_V3		GENMASK(23, 16)
89 #define MTK_FOE_WINFO_WCID_V3		GENMASK(15, 0)
90 
91 #define MTK_FOE_WINFO_AMSDU_USR_INFO	GENMASK(15, 0)
92 #define MTK_FOE_WINFO_AMSDU_TID		GENMASK(19, 16)
93 #define MTK_FOE_WINFO_AMSDU_IS_FIXEDRATE	BIT(20)
94 #define MTK_FOE_WINFO_AMSDU_IS_PRIOR	BIT(21)
95 #define MTK_FOE_WINFO_AMSDU_IS_SP	BIT(22)
96 #define MTK_FOE_WINFO_AMSDU_HF		BIT(23)
97 #define MTK_FOE_WINFO_AMSDU_EN		BIT(24)
98 
99 enum {
100 	MTK_FOE_STATE_INVALID,
101 	MTK_FOE_STATE_UNBIND,
102 	MTK_FOE_STATE_BIND,
103 	MTK_FOE_STATE_FIN
104 };
105 
106 struct mtk_foe_mac_info {
107 	u16 vlan1;
108 	u16 etype;
109 
110 	u32 dest_mac_hi;
111 
112 	u16 vlan2;
113 	u16 dest_mac_lo;
114 
115 	u32 src_mac_hi;
116 
117 	u16 pppoe_id;
118 	u16 src_mac_lo;
119 
120 	/* netsys_v2 */
121 	u16 minfo;
122 	u16 winfo;
123 
124 	/* netsys_v3 */
125 	u32 w3info;
126 	u32 amsdu;
127 };
128 
129 /* software-only entry type */
130 struct mtk_foe_bridge {
131 	u8 dest_mac[ETH_ALEN];
132 	u8 src_mac[ETH_ALEN];
133 	u16 vlan;
134 
135 	struct {} key_end;
136 
137 	u32 ib2;
138 
139 	struct mtk_foe_mac_info l2;
140 };
141 
142 struct mtk_ipv4_tuple {
143 	u32 src_ip;
144 	u32 dest_ip;
145 	union {
146 		struct {
147 			u16 dest_port;
148 			u16 src_port;
149 		};
150 		struct {
151 			u8 protocol;
152 			u8 _pad[3]; /* fill with 0xa5a5a5 */
153 		};
154 		u32 ports;
155 	};
156 };
157 
158 struct mtk_foe_ipv4 {
159 	struct mtk_ipv4_tuple orig;
160 
161 	u32 ib2;
162 
163 	struct mtk_ipv4_tuple new;
164 
165 	u16 timestamp;
166 	u16 _rsv0[3];
167 
168 	u32 udf_tsid;
169 
170 	struct mtk_foe_mac_info l2;
171 };
172 
173 struct mtk_foe_ipv4_dslite {
174 	struct mtk_ipv4_tuple ip4;
175 
176 	u32 tunnel_src_ip[4];
177 	u32 tunnel_dest_ip[4];
178 
179 	u8 flow_label[3];
180 	u8 priority;
181 
182 	u32 udf_tsid;
183 
184 	u32 ib2;
185 
186 	struct mtk_foe_mac_info l2;
187 };
188 
189 struct mtk_foe_ipv6 {
190 	u32 src_ip[4];
191 	u32 dest_ip[4];
192 
193 	union {
194 		struct {
195 			u8 protocol;
196 			u8 _pad[3]; /* fill with 0xa5a5a5 */
197 		}; /* 3-tuple */
198 		struct {
199 			u16 dest_port;
200 			u16 src_port;
201 		}; /* 5-tuple */
202 		u32 ports;
203 	};
204 
205 	u32 _rsv[3];
206 
207 	u32 udf;
208 
209 	u32 ib2;
210 	struct mtk_foe_mac_info l2;
211 };
212 
213 struct mtk_foe_ipv6_6rd {
214 	u32 src_ip[4];
215 	u32 dest_ip[4];
216 	u16 dest_port;
217 	u16 src_port;
218 
219 	u32 tunnel_src_ip;
220 	u32 tunnel_dest_ip;
221 
222 	u16 hdr_csum;
223 	u8 dscp;
224 	u8 ttl;
225 
226 	u8 flag;
227 	u8 pad;
228 	u8 per_flow_6rd_id;
229 	u8 pad2;
230 
231 	u32 ib2;
232 	struct mtk_foe_mac_info l2;
233 };
234 
235 #define MTK_FOE_ENTRY_V1_SIZE	80
236 #define MTK_FOE_ENTRY_V2_SIZE	96
237 #define MTK_FOE_ENTRY_V3_SIZE	128
238 
239 struct mtk_foe_entry {
240 	u32 ib1;
241 
242 	union {
243 		struct mtk_foe_bridge bridge;
244 		struct mtk_foe_ipv4 ipv4;
245 		struct mtk_foe_ipv4_dslite dslite;
246 		struct mtk_foe_ipv6 ipv6;
247 		struct mtk_foe_ipv6_6rd ipv6_6rd;
248 		u32 data[31];
249 	};
250 };
251 
252 enum {
253 	MTK_PPE_CPU_REASON_TTL_EXCEEDED			= 0x02,
254 	MTK_PPE_CPU_REASON_OPTION_HEADER		= 0x03,
255 	MTK_PPE_CPU_REASON_NO_FLOW			= 0x07,
256 	MTK_PPE_CPU_REASON_IPV4_FRAG			= 0x08,
257 	MTK_PPE_CPU_REASON_IPV4_DSLITE_FRAG		= 0x09,
258 	MTK_PPE_CPU_REASON_IPV4_DSLITE_NO_TCP_UDP	= 0x0a,
259 	MTK_PPE_CPU_REASON_IPV6_6RD_NO_TCP_UDP		= 0x0b,
260 	MTK_PPE_CPU_REASON_TCP_FIN_SYN_RST		= 0x0c,
261 	MTK_PPE_CPU_REASON_UN_HIT			= 0x0d,
262 	MTK_PPE_CPU_REASON_HIT_UNBIND			= 0x0e,
263 	MTK_PPE_CPU_REASON_HIT_UNBIND_RATE_REACHED	= 0x0f,
264 	MTK_PPE_CPU_REASON_HIT_BIND_TCP_FIN		= 0x10,
265 	MTK_PPE_CPU_REASON_HIT_TTL_1			= 0x11,
266 	MTK_PPE_CPU_REASON_HIT_BIND_VLAN_VIOLATION	= 0x12,
267 	MTK_PPE_CPU_REASON_KEEPALIVE_UC_OLD_HDR		= 0x13,
268 	MTK_PPE_CPU_REASON_KEEPALIVE_MC_NEW_HDR		= 0x14,
269 	MTK_PPE_CPU_REASON_KEEPALIVE_DUP_OLD_HDR	= 0x15,
270 	MTK_PPE_CPU_REASON_HIT_BIND_FORCE_CPU		= 0x16,
271 	MTK_PPE_CPU_REASON_TUNNEL_OPTION_HEADER		= 0x17,
272 	MTK_PPE_CPU_REASON_MULTICAST_TO_CPU		= 0x18,
273 	MTK_PPE_CPU_REASON_MULTICAST_TO_GMAC1_CPU	= 0x19,
274 	MTK_PPE_CPU_REASON_HIT_PRE_BIND			= 0x1a,
275 	MTK_PPE_CPU_REASON_PACKET_SAMPLING		= 0x1b,
276 	MTK_PPE_CPU_REASON_EXCEED_MTU			= 0x1c,
277 	MTK_PPE_CPU_REASON_PPE_BYPASS			= 0x1e,
278 	MTK_PPE_CPU_REASON_INVALID			= 0x1f,
279 };
280 
281 enum {
282 	MTK_FLOW_TYPE_L4,
283 	MTK_FLOW_TYPE_L2,
284 	MTK_FLOW_TYPE_L2_SUBFLOW,
285 };
286 
287 struct mtk_flow_entry {
288 	union {
289 		struct hlist_node list;
290 		struct {
291 			struct rhash_head l2_node;
292 			struct hlist_head l2_flows;
293 		};
294 	};
295 	u8 type;
296 	s8 wed_index;
297 	u8 ppe_index;
298 	u16 hash;
299 	union {
300 		struct mtk_foe_entry data;
301 		struct {
302 			struct mtk_flow_entry *base_flow;
303 			struct hlist_node list;
304 		} l2_data;
305 	};
306 	struct rhash_head node;
307 	unsigned long cookie;
308 };
309 
310 struct mtk_mib_entry {
311 	u32	byt_cnt_l;
312 	u16	byt_cnt_h;
313 	u32	pkt_cnt_l;
314 	u8	pkt_cnt_h;
315 	u8	_rsv0;
316 	u32	_rsv1;
317 } __packed;
318 
319 struct mtk_foe_accounting {
320 	u64	bytes;
321 	u64	packets;
322 };
323 
324 struct mtk_ppe {
325 	struct mtk_eth *eth;
326 	struct device *dev;
327 	void __iomem *base;
328 	int version;
329 	char dirname[5];
330 	bool accounting;
331 
332 	void *foe_table;
333 	dma_addr_t foe_phys;
334 
335 	struct mtk_mib_entry *mib_table;
336 	dma_addr_t mib_phys;
337 
338 	u16 foe_check_time[MTK_PPE_ENTRIES];
339 	struct hlist_head *foe_flow;
340 
341 	struct rhashtable l2_flows;
342 
343 	void *acct_table;
344 };
345 
346 struct mtk_ppe *mtk_ppe_init(struct mtk_eth *eth, void __iomem *base, int index);
347 
348 void mtk_ppe_deinit(struct mtk_eth *eth);
349 void mtk_ppe_start(struct mtk_ppe *ppe);
350 int mtk_ppe_stop(struct mtk_ppe *ppe);
351 int mtk_ppe_prepare_reset(struct mtk_ppe *ppe);
352 
353 void __mtk_ppe_check_skb(struct mtk_ppe *ppe, struct sk_buff *skb, u16 hash);
354 
355 static inline void
mtk_ppe_check_skb(struct mtk_ppe * ppe,struct sk_buff * skb,u16 hash)356 mtk_ppe_check_skb(struct mtk_ppe *ppe, struct sk_buff *skb, u16 hash)
357 {
358 	u16 now, diff;
359 
360 	if (!ppe)
361 		return;
362 
363 	if (hash > MTK_PPE_HASH_MASK)
364 		return;
365 
366 	now = (u16)jiffies;
367 	diff = now - ppe->foe_check_time[hash];
368 	if (diff < HZ / 10)
369 		return;
370 
371 	ppe->foe_check_time[hash] = now;
372 	__mtk_ppe_check_skb(ppe, skb, hash);
373 }
374 
375 int mtk_foe_entry_prepare(struct mtk_eth *eth, struct mtk_foe_entry *entry,
376 			  int type, int l4proto, u8 pse_port, u8 *src_mac,
377 			  u8 *dest_mac);
378 int mtk_foe_entry_set_pse_port(struct mtk_eth *eth,
379 			       struct mtk_foe_entry *entry, u8 port);
380 int mtk_foe_entry_set_ipv4_tuple(struct mtk_eth *eth,
381 				 struct mtk_foe_entry *entry, bool orig,
382 				 __be32 src_addr, __be16 src_port,
383 				 __be32 dest_addr, __be16 dest_port);
384 int mtk_foe_entry_set_ipv6_tuple(struct mtk_eth *eth,
385 				 struct mtk_foe_entry *entry,
386 				 __be32 *src_addr, __be16 src_port,
387 				 __be32 *dest_addr, __be16 dest_port);
388 int mtk_foe_entry_set_dsa(struct mtk_eth *eth, struct mtk_foe_entry *entry,
389 			  int port);
390 int mtk_foe_entry_set_vlan(struct mtk_eth *eth, struct mtk_foe_entry *entry,
391 			   int vid);
392 int mtk_foe_entry_set_pppoe(struct mtk_eth *eth, struct mtk_foe_entry *entry,
393 			    int sid);
394 int mtk_foe_entry_set_wdma(struct mtk_eth *eth, struct mtk_foe_entry *entry,
395 			   int wdma_idx, int txq, int bss, int wcid,
396 			   bool amsdu_en);
397 int mtk_foe_entry_set_queue(struct mtk_eth *eth, struct mtk_foe_entry *entry,
398 			    unsigned int queue);
399 int mtk_foe_entry_commit(struct mtk_ppe *ppe, struct mtk_flow_entry *entry);
400 void mtk_foe_entry_clear(struct mtk_ppe *ppe, struct mtk_flow_entry *entry);
401 int mtk_foe_entry_idle_time(struct mtk_ppe *ppe, struct mtk_flow_entry *entry);
402 int mtk_ppe_debugfs_init(struct mtk_ppe *ppe, int index);
403 struct mtk_foe_accounting *mtk_foe_entry_get_mib(struct mtk_ppe *ppe, u32 index,
404 						 struct mtk_foe_accounting *diff);
405 
406 #endif
407