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