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