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