blob: f0ea6a22dae2404983e8857cb4f472bc4406f989 [file] [log] [blame]
developerfd40db22021-04-29 10:08:25 +08001/* This program is free software; you can redistribute it and/or modify
2 * it under the terms of the GNU General Public License as published by
3 * the Free Software Foundation; version 2 of the License
4 *
5 * This program is distributed in the hope that it will be useful,
6 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8 * GNU General Public License for more details.
9 *
10 * Copyright (C) 2014-2016 Sean Wang <sean.wang@mediatek.com>
11 * Copyright (C) 2016-2017 John Crispin <blogic@openwrt.org>
12 */
13
14#include <linux/debugfs.h>
15#include <linux/string.h>
16#include <linux/if.h>
17#include <linux/if_ether.h>
18#include <net/netevent.h>
19#include <linux/mod_devicetable.h>
20#include "hnat_mcast.h"
21
22/*--------------------------------------------------------------------------*/
23/* Register Offset*/
24/*--------------------------------------------------------------------------*/
25#define PPE_GLO_CFG 0x00
26#define PPE_FLOW_CFG 0x04
27#define PPE_IP_PROT_CHK 0x08
28#define PPE_IP_PROT_0 0x0C
29#define PPE_IP_PROT_1 0x10
30#define PPE_IP_PROT_2 0x14
31#define PPE_IP_PROT_3 0x18
32#define PPE_TB_CFG 0x1C
33#define PPE_TB_BASE 0x20
34#define PPE_TB_USED 0x24
35#define PPE_BNDR 0x28
36#define PPE_BIND_LMT_0 0x2C
37#define PPE_BIND_LMT_1 0x30
38#define PPE_KA 0x34
39#define PPE_UNB_AGE 0x38
40#define PPE_BND_AGE_0 0x3C
41#define PPE_BND_AGE_1 0x40
42#define PPE_HASH_SEED 0x44
43#define PPE_DFT_CPORT 0x48
44#define PPE_DFT_CPORT1 0x4C
45#define PPE_MCAST_PPSE 0x84
46#define PPE_MCAST_L_0 0x88
47#define PPE_MCAST_H_0 0x8C
48#define PPE_MCAST_L_1 0x90
49#define PPE_MCAST_H_1 0x94
50#define PPE_MCAST_L_2 0x98
51#define PPE_MCAST_H_2 0x9C
52#define PPE_MCAST_L_3 0xA0
53#define PPE_MCAST_H_3 0xA4
54#define PPE_MCAST_L_4 0xA8
55#define PPE_MCAST_H_4 0xAC
56#define PPE_MCAST_L_5 0xB0
57#define PPE_MCAST_H_5 0xB4
58#define PPE_MCAST_L_6 0xBC
59#define PPE_MCAST_H_6 0xC0
60#define PPE_MCAST_L_7 0xC4
61#define PPE_MCAST_H_7 0xC8
62#define PPE_MCAST_L_8 0xCC
63#define PPE_MCAST_H_8 0xD0
64#define PPE_MCAST_L_9 0xD4
65#define PPE_MCAST_H_9 0xD8
66#define PPE_MCAST_L_A 0xDC
67#define PPE_MCAST_H_A 0xE0
68#define PPE_MCAST_L_B 0xE4
69#define PPE_MCAST_H_B 0xE8
70#define PPE_MCAST_L_C 0xEC
71#define PPE_MCAST_H_C 0xF0
72#define PPE_MCAST_L_D 0xF4
73#define PPE_MCAST_H_D 0xF8
74#define PPE_MCAST_L_E 0xFC
75#define PPE_MCAST_H_E 0xE0
76#define PPE_MCAST_L_F 0x100
77#define PPE_MCAST_H_F 0x104
78#define PPE_MCAST_L_10 0xC00
79#define PPE_MCAST_H_10 0xC04
80#define PPE_MTU_DRP 0x108
81#define PPE_MTU_VLYR_0 0x10C
82#define PPE_MTU_VLYR_1 0x110
83#define PPE_MTU_VLYR_2 0x114
84#define PPE_VPM_TPID 0x118
85#define PPE_CAH_CTRL 0x120
86#define PPE_CAH_TAG_SRH 0x124
87#define PPE_CAH_LINE_RW 0x128
88#define PPE_CAH_WDATA 0x12C
89#define PPE_CAH_RDATA 0x130
90
91#define PPE_MIB_CFG 0X134
92#define PPE_MIB_TB_BASE 0X138
93#define PPE_MIB_SER_CR 0X13C
94#define PPE_MIB_SER_R0 0X140
95#define PPE_MIB_SER_R1 0X144
96#define PPE_MIB_SER_R2 0X148
97#define PPE_MIB_CAH_CTRL 0X150
98#define PPE_MIB_CAH_TAG_SRH 0X154
99#define PPE_MIB_CAH_LINE_RW 0X158
100#define PPE_MIB_CAH_WDATA 0X15C
101#define PPE_MIB_CAH_RDATA 0X160
102#define PPE_SBW_CTRL 0x174
103
104#define GDMA1_FWD_CFG 0x500
105#define GDMA2_FWD_CFG 0x1500
106
developer24948202021-11-24 17:38:27 +0800107/* QDMA Tx queue configuration */
developer70cdf6e2021-12-07 18:58:35 +0800108#define QTX_CFG(x) (QDMA_BASE + ((x) * 0x10))
109#define QTX_CFG_HW_RESV_CNT_OFFSET (8)
110#define QTX_CFG_SW_RESV_CNT_OFFSET (0)
111
112#define QTX_SCH(x) (QDMA_BASE + 0x4 + ((x) * 0x10))
113#define QTX_SCH_MIN_RATE_EN BIT(27)
114#define QTX_SCH_MAX_RATE_EN BIT(11)
115#define QTX_SCH_MIN_RATE_MAN_OFFSET (20)
116#define QTX_SCH_MIN_RATE_EXP_OFFSET (16)
117#define QTX_SCH_MAX_RATE_WGHT_OFFSET (12)
118#define QTX_SCH_MAX_RATE_MAN_OFFSET (4)
119#define QTX_SCH_MAX_RATE_EXP_OFFSET (0)
developer24948202021-11-24 17:38:27 +0800120
121/* QDMA Tx scheduler configuration */
developer70cdf6e2021-12-07 18:58:35 +0800122#define QDMA_PAGE (QDMA_BASE + 0x1f0)
123#define QDMA_TX_2SCH_BASE (QDMA_BASE + 0x214)
124#define QTX_MIB_IF (QDMA_BASE + 0x2bc)
125#define QDMA_TX_4SCH_BASE(x) (QDMA_BASE + 0x398 + (((x) >> 1) * 0x4))
developer34028fb2022-01-11 13:51:29 +0800126#define QDMA_TX_SCH_WFQ_EN BIT(15)
developerfd40db22021-04-29 10:08:25 +0800127
128/*--------------------------------------------------------------------------*/
129/* Register Mask*/
130/*--------------------------------------------------------------------------*/
131/* PPE_TB_CFG mask */
132#define TB_ETRY_NUM (0x7 << 0) /* RW */
133#define TB_ENTRY_SIZE (0x1 << 3) /* RW */
134#define SMA (0x3 << 4) /* RW */
135#define NTU_AGE (0x1 << 7) /* RW */
136#define UNBD_AGE (0x1 << 8) /* RW */
137#define TCP_AGE (0x1 << 9) /* RW */
138#define UDP_AGE (0x1 << 10) /* RW */
139#define FIN_AGE (0x1 << 11) /* RW */
140#define KA_CFG (0x3 << 12)
141#define HASH_MODE (0x3 << 14) /* RW */
142#define SCAN_MODE (0x3 << 16) /* RW */
143#define XMODE (0x3 << 18) /* RW */
developer8051e042022-04-08 13:26:36 +0800144#define TICK_SEL (0x1 << 24) /* RW */
145
developerfd40db22021-04-29 10:08:25 +0800146
147/*PPE_CAH_CTRL mask*/
148#define CAH_EN (0x1 << 0) /* RW */
149#define CAH_X_MODE (0x1 << 9) /* RW */
150
151/*PPE_UNB_AGE mask*/
152#define UNB_DLTA (0xff << 0) /* RW */
153#define UNB_MNP (0xffff << 16) /* RW */
154
155/*PPE_BND_AGE_0 mask*/
156#define UDP_DLTA (0xffff << 0) /* RW */
157#define NTU_DLTA (0xffff << 16) /* RW */
158
159/*PPE_BND_AGE_1 mask*/
160#define TCP_DLTA (0xffff << 0) /* RW */
161#define FIN_DLTA (0xffff << 16) /* RW */
162
163/*PPE_KA mask*/
164#define KA_T (0xffff << 0) /* RW */
165#define TCP_KA (0xff << 16) /* RW */
166#define UDP_KA (0xff << 24) /* RW */
167
168/*PPE_BIND_LMT_0 mask*/
169#define QURT_LMT (0x3ff << 0) /* RW */
170#define HALF_LMT (0x3ff << 16) /* RW */
171
172/*PPE_BIND_LMT_1 mask*/
173#define FULL_LMT (0x3fff << 0) /* RW */
174#define NTU_KA (0xff << 16) /* RW */
175
176/*PPE_BNDR mask*/
177#define BIND_RATE (0xffff << 0) /* RW */
178#define PBND_RD_PRD (0xffff << 16) /* RW */
179
180/*PPE_GLO_CFG mask*/
181#define PPE_EN (0x1 << 0) /* RW */
182#define TTL0_DRP (0x1 << 4) /* RW */
183#define MCAST_TB_EN (0x1 << 7) /* RW */
184#define MCAST_HASH (0x3 << 12) /* RW */
185
186#define MC_P3_PPSE (0xf << 12) /* RW */
187#define MC_P2_PPSE (0xf << 8) /* RW */
188#define MC_P1_PPSE (0xf << 4) /* RW */
189#define MC_P0_PPSE (0xf << 0) /* RW */
190
191#define MIB_EN (0x1 << 0) /* RW */
192#define MIB_READ_CLEAR (0X1 << 1) /* RW */
193#define MIB_CAH_EN (0X1 << 0) /* RW */
194
195/*GDMA_FWD_CFG mask */
196#define GDM_UFRC_MASK (0x7 << 12) /* RW */
197#define GDM_BFRC_MASK (0x7 << 8) /*RW*/
198#define GDM_MFRC_MASK (0x7 << 4) /*RW*/
199#define GDM_OFRC_MASK (0x7 << 0) /*RW*/
200#define GDM_ALL_FRC_MASK \
201 (GDM_UFRC_MASK | GDM_BFRC_MASK | GDM_MFRC_MASK | GDM_OFRC_MASK)
202
203/*QDMA_PAGE mask*/
204#define QTX_CFG_PAGE (0xf << 0) /* RW */
205
206/*QTX_MIB_IF mask*/
207#define MIB_ON_QTX_CFG (0x1 << 31) /* RW */
208#define VQTX_MIB_EN (0x1 << 28) /* RW */
209
210/*--------------------------------------------------------------------------*/
211/* Descriptor Structure */
212/*--------------------------------------------------------------------------*/
213#if defined(CONFIG_MEDIATEK_NETSYS_V2)
214struct hnat_unbind_info_blk {
215 u32 time_stamp : 8;
216 u32 sp : 4;
217 u32 pcnt : 8;
218 u32 ilgf : 1;
219 u32 mc : 1;
220 u32 preb : 1;
221 u32 pkt_type : 5;
222 u32 state : 2;
223 u32 udp : 1;
224 u32 sta : 1; /* static entry */
225} __packed;
226
227struct hnat_bind_info_blk {
228 u32 time_stamp : 8;
229 u32 sp : 4;
230 u32 mc : 1;
231 u32 ka : 1; /* keep alive */
232 u32 vlan_layer : 3;
233 u32 psn : 1; /* egress packet has PPPoE session */
234 u32 vpm : 1; /* 0:ethertype remark, 1:0x8100(CR default) */
235 u32 ps : 1; /* packet sampling */
236 u32 cah : 1; /* cacheable flag */
237 u32 rmt : 1; /* remove tunnel ip header (6rd/dslite only) */
238 u32 ttl : 1;
239 u32 pkt_type : 5;
240 u32 state : 2;
241 u32 udp : 1;
242 u32 sta : 1; /* static entry */
243} __packed;
244
245struct hnat_info_blk2 {
246 u32 qid : 7; /* QID in Qos Port */
247 u32 port_mg : 1;
248 u32 fqos : 1; /* force to PSE QoS port */
249 u32 dp : 4; /* force to PSE port x */
250 u32 mcast : 1; /* multicast this packet to CPU */
251 u32 pcpl : 1; /* OSBN */
252 u32 mibf : 1;
253 u32 alen : 1;
254 u32 rxid : 2;
255 u32 winfoi : 1;
256 u32 port_ag : 4;
257 u32 dscp : 8; /* DSCP value */
258} __packed;
259
260struct hnat_winfo {
261 u32 bssid : 6; /* WiFi Bssidx */
262 u32 wcid : 10; /* WiFi wtable Idx */
263} __packed;
264
265#else
266struct hnat_unbind_info_blk {
267 u32 time_stamp : 8;
268 u32 pcnt : 16; /* packet count */
269 u32 preb : 1;
270 u32 pkt_type : 3;
271 u32 state : 2;
272 u32 udp : 1;
273 u32 sta : 1; /* static entry */
274} __packed;
275
276struct hnat_bind_info_blk {
277 u32 time_stamp : 15;
278 u32 ka : 1; /* keep alive */
279 u32 vlan_layer : 3;
280 u32 psn : 1; /* egress packet has PPPoE session */
281 u32 vpm : 1; /* 0:ethertype remark, 1:0x8100(CR default) */
282 u32 ps : 1; /* packet sampling */
283 u32 cah : 1; /* cacheable flag */
284 u32 rmt : 1; /* remove tunnel ip header (6rd/dslite only) */
285 u32 ttl : 1;
286 u32 pkt_type : 3;
287 u32 state : 2;
288 u32 udp : 1;
289 u32 sta : 1; /* static entry */
290} __packed;
291
292struct hnat_info_blk2 {
293 u32 qid : 4; /* QID in Qos Port */
294 u32 fqos : 1; /* force to PSE QoS port */
295 u32 dp : 3; /* force to PSE port x
296 * 0:PSE,1:GSW, 2:GMAC,4:PPE,5:QDMA,7=DROP
297 */
298 u32 mcast : 1; /* multicast this packet to CPU */
299 u32 pcpl : 1; /* OSBN */
300 u32 mibf : 1; /* 0:off 1:on PPE MIB counter */
301 u32 alen : 1; /* 0:post 1:pre packet length in accounting */
302 u32 port_mg : 6; /* port meter group */
303 u32 port_ag : 6; /* port account group */
304 u32 dscp : 8; /* DSCP value */
305} __packed;
306
307struct hnat_winfo {
308 u32 bssid : 6; /* WiFi Bssidx */
309 u32 wcid : 8; /* WiFi wtable Idx */
310 u32 rxid : 2; /* WiFi Ring idx */
311} __packed;
312#endif
313
314/* info blk2 for WHNAT */
315struct hnat_info_blk2_whnat {
316 u32 qid : 4; /* QID[3:0] in Qos Port */
317 u32 fqos : 1; /* force to PSE QoS port */
318 u32 dp : 3; /* force to PSE port x
319 * 0:PSE,1:GSW, 2:GMAC,4:PPE,5:QDMA,7=DROP
320 */
321 u32 mcast : 1; /* multicast this packet to CPU */
322 u32 pcpl : 1; /* OSBN */
323 u32 mibf : 1; /* 0:off 1:on PPE MIB counter */
324 u32 alen : 1; /* 0:post 1:pre packet length in accounting */
325 u32 qid2 : 2; /* QID[5:4] in Qos Port */
326 u32 resv : 2;
327 u32 wdmaid : 1; /* 0:to pcie0 dev 1:to pcie1 dev */
328 u32 winfoi : 1; /* 0:off 1:on Wi-Fi hwnat support */
329 u32 port_ag : 6; /* port account group */
330 u32 dscp : 8; /* DSCP value */
331} __packed;
332
333struct hnat_ipv4_hnapt {
334 union {
335 struct hnat_bind_info_blk bfib1;
336 struct hnat_unbind_info_blk udib1;
337 u32 info_blk1;
338 };
339 u32 sip;
340 u32 dip;
341 u16 dport;
342 u16 sport;
343 union {
344 struct hnat_info_blk2 iblk2;
345 struct hnat_info_blk2_whnat iblk2w;
346 u32 info_blk2;
347 };
348 u32 new_sip;
349 u32 new_dip;
350 u16 new_dport;
351 u16 new_sport;
352 u16 m_timestamp; /* For mcast*/
353 u16 resv1;
354 u32 resv2;
355 u32 resv3 : 26;
356 u32 act_dp : 6; /* UDF */
357 u16 vlan1;
358 u16 etype;
359 u32 dmac_hi;
360 union {
361#if !defined(CONFIG_MEDIATEK_NETSYS_V2)
362 struct hnat_winfo winfo;
363#endif
364 u16 vlan2;
365 };
366 u16 dmac_lo;
367 u32 smac_hi;
368 u16 pppoe_id;
369 u16 smac_lo;
370#if defined(CONFIG_MEDIATEK_NETSYS_V2)
371 u16 minfo;
372 struct hnat_winfo winfo;
373#endif
374} __packed;
375
376struct hnat_ipv4_dslite {
377 union {
378 struct hnat_bind_info_blk bfib1;
379 struct hnat_unbind_info_blk udib1;
380 u32 info_blk1;
381 };
382 u32 sip;
383 u32 dip;
384 u16 dport;
385 u16 sport;
386
387 u32 tunnel_sipv6_0;
388 u32 tunnel_sipv6_1;
389 u32 tunnel_sipv6_2;
390 u32 tunnel_sipv6_3;
391
392 u32 tunnel_dipv6_0;
393 u32 tunnel_dipv6_1;
394 u32 tunnel_dipv6_2;
395 u32 tunnel_dipv6_3;
396
397 u8 flow_lbl[3]; /* in order to consist with Linux kernel (should be 20bits) */
398 u8 priority; /* in order to consist with Linux kernel (should be 8bits) */
399 u32 hop_limit : 8;
400 u32 resv2 : 18;
401 u32 act_dp : 6; /* UDF */
402
403 union {
404 struct hnat_info_blk2 iblk2;
405 struct hnat_info_blk2_whnat iblk2w;
406 u32 info_blk2;
407 };
408
409 u16 vlan1;
410 u16 etype;
411 u32 dmac_hi;
412 union {
413#if !defined(CONFIG_MEDIATEK_NETSYS_V2)
414 struct hnat_winfo winfo;
415#endif
416 u16 vlan2;
417 };
418 u16 dmac_lo;
419 u32 smac_hi;
420 u16 pppoe_id;
421 u16 smac_lo;
422#if defined(CONFIG_MEDIATEK_NETSYS_V2)
423 u16 minfo;
424 struct hnat_winfo winfo;
425 u32 new_sip;
426 u32 new_dip;
427 u16 new_dport;
428 u16 new_sport;
429#endif
430} __packed;
431
432struct hnat_ipv6_3t_route {
433 union {
434 struct hnat_bind_info_blk bfib1;
435 struct hnat_unbind_info_blk udib1;
436 u32 info_blk1;
437 };
438 u32 ipv6_sip0;
439 u32 ipv6_sip1;
440 u32 ipv6_sip2;
441 u32 ipv6_sip3;
442 u32 ipv6_dip0;
443 u32 ipv6_dip1;
444 u32 ipv6_dip2;
445 u32 ipv6_dip3;
446 u32 prot : 8;
developer729f0272021-06-09 17:28:38 +0800447 u32 hph : 24; /* hash placeholder */
developerfd40db22021-04-29 10:08:25 +0800448
449 u32 resv1;
450 u32 resv2;
451 u32 resv3;
452 u32 resv4 : 26;
453 u32 act_dp : 6; /* UDF */
454
455 union {
456 struct hnat_info_blk2 iblk2;
457 struct hnat_info_blk2_whnat iblk2w;
458 u32 info_blk2;
459 };
460 u16 vlan1;
461 u16 etype;
462 u32 dmac_hi;
463 union {
464#if !defined(CONFIG_MEDIATEK_NETSYS_V2)
465 struct hnat_winfo winfo;
466#endif
467 u16 vlan2;
468 };
469 u16 dmac_lo;
470 u32 smac_hi;
471 u16 pppoe_id;
472 u16 smac_lo;
473#if defined(CONFIG_MEDIATEK_NETSYS_V2)
474 u16 minfo;
475 struct hnat_winfo winfo;
476#endif
477} __packed;
478
479struct hnat_ipv6_5t_route {
480 union {
481 struct hnat_bind_info_blk bfib1;
482 struct hnat_unbind_info_blk udib1;
483 u32 info_blk1;
484 };
485 u32 ipv6_sip0;
486 u32 ipv6_sip1;
487 u32 ipv6_sip2;
488 u32 ipv6_sip3;
489 u32 ipv6_dip0;
490 u32 ipv6_dip1;
491 u32 ipv6_dip2;
492 u32 ipv6_dip3;
493 u16 dport;
494 u16 sport;
495
496 u32 resv1;
497 u32 resv2;
498 u32 resv3;
499 u32 resv4 : 26;
500 u32 act_dp : 6; /* UDF */
501
502 union {
503 struct hnat_info_blk2 iblk2;
504 struct hnat_info_blk2_whnat iblk2w;
505 u32 info_blk2;
506 };
507
508 u16 vlan1;
509 u16 etype;
510 u32 dmac_hi;
511 union {
512#if !defined(CONFIG_MEDIATEK_NETSYS_V2)
513 struct hnat_winfo winfo;
514#endif
515 u16 vlan2;
516 };
517 u16 dmac_lo;
518 u32 smac_hi;
519 u16 pppoe_id;
520 u16 smac_lo;
521#if defined(CONFIG_MEDIATEK_NETSYS_V2)
522 u16 minfo;
523 struct hnat_winfo winfo;
524#endif
525} __packed;
526
527struct hnat_ipv6_6rd {
528 union {
529 struct hnat_bind_info_blk bfib1;
530 struct hnat_unbind_info_blk udib1;
531 u32 info_blk1;
532 };
533 u32 ipv6_sip0;
534 u32 ipv6_sip1;
535 u32 ipv6_sip2;
536 u32 ipv6_sip3;
537 u32 ipv6_dip0;
538 u32 ipv6_dip1;
539 u32 ipv6_dip2;
540 u32 ipv6_dip3;
541 u16 dport;
542 u16 sport;
543
544 u32 tunnel_sipv4;
545 u32 tunnel_dipv4;
546 u32 hdr_chksum : 16;
547 u32 dscp : 8;
548 u32 ttl : 8;
549 u32 flag : 3;
550 u32 resv1 : 13;
551 u32 per_flow_6rd_id : 1;
552 u32 resv2 : 9;
553 u32 act_dp : 6; /* UDF */
554
555 union {
556 struct hnat_info_blk2 iblk2;
557 struct hnat_info_blk2_whnat iblk2w;
558 u32 info_blk2;
559 };
560
561 u16 vlan1;
562 u16 etype;
563 u32 dmac_hi;
564 union {
565#if !defined(CONFIG_MEDIATEK_NETSYS_V2)
566 struct hnat_winfo winfo;
567#endif
568 u16 vlan2;
569 };
570 u16 dmac_lo;
571 u32 smac_hi;
572 u16 pppoe_id;
573 u16 smac_lo;
574#if defined(CONFIG_MEDIATEK_NETSYS_V2)
575 u16 minfo;
576 struct hnat_winfo winfo;
577 u32 resv3;
578 u32 resv4;
579 u16 new_dport;
580 u16 new_sport;
581#endif
582} __packed;
583
584struct foe_entry {
585 union {
586 struct hnat_unbind_info_blk udib1;
587 struct hnat_bind_info_blk bfib1;
588 struct hnat_ipv4_hnapt ipv4_hnapt;
589 struct hnat_ipv4_dslite ipv4_dslite;
590 struct hnat_ipv6_3t_route ipv6_3t_route;
591 struct hnat_ipv6_5t_route ipv6_5t_route;
592 struct hnat_ipv6_6rd ipv6_6rd;
593 };
594};
595
596/* If user wants to change default FOE entry number, both DEF_ETRY_NUM and
597 * DEF_ETRY_NUM_CFG need to be modified.
598 */
599#define DEF_ETRY_NUM 8192
developerbc53e5f2021-05-21 10:07:17 +0800600/* feasible values : 32768, 16384, 8192, 4096, 2048, 1024 */
developerfd40db22021-04-29 10:08:25 +0800601#define DEF_ETRY_NUM_CFG TABLE_8K
developerbc53e5f2021-05-21 10:07:17 +0800602/* corresponding values : TABLE_32K, TABLE_16K, TABLE_8K, TABLE_4K, TABLE_2K,
603 * TABLE_1K
604 */
developerfd40db22021-04-29 10:08:25 +0800605#define MAX_EXT_DEVS (0x3fU)
606#define MAX_IF_NUM 64
607
developer471f6562021-05-10 20:48:34 +0800608#if defined(CONFIG_MEDIATEK_NETSYS_V2)
609#define MAX_PPE_NUM 2
610#else
611#define MAX_PPE_NUM 1
612#endif
613#define CFG_PPE_NUM (hnat_priv->ppe_num)
614
developerfd40db22021-04-29 10:08:25 +0800615struct mib_entry {
616 u32 byt_cnt_l;
617 u16 byt_cnt_h;
618 u32 pkt_cnt_l;
619 u8 pkt_cnt_h;
620 u8 resv0;
621 u32 resv1;
622} __packed;
623
624struct hnat_accounting {
625 u64 bytes;
626 u64 packets;
627};
628
629enum mtk_hnat_version {
630 MTK_HNAT_V1 = 1, /* version 1: mt7621, mt7623 */
631 MTK_HNAT_V2, /* version 2: mt7622 */
632 MTK_HNAT_V3, /* version 3: mt7629 */
633 MTK_HNAT_V4, /* version 4: mt7986 */
634};
635
636struct mtk_hnat_data {
637 u8 num_of_sch;
638 bool whnat;
639 bool per_flow_accounting;
640 bool mcast;
641 enum mtk_hnat_version version;
642};
643
644struct mtk_hnat {
645 struct device *dev;
646 void __iomem *fe_base;
developer471f6562021-05-10 20:48:34 +0800647 void __iomem *ppe_base[MAX_PPE_NUM];
648 struct foe_entry *foe_table_cpu[MAX_PPE_NUM];
649 dma_addr_t foe_table_dev[MAX_PPE_NUM];
developerfd40db22021-04-29 10:08:25 +0800650 u8 enable;
651 u8 enable1;
652 struct dentry *root;
developer471f6562021-05-10 20:48:34 +0800653 struct debugfs_regset32 *regset[MAX_PPE_NUM];
developerfd40db22021-04-29 10:08:25 +0800654
developer471f6562021-05-10 20:48:34 +0800655 struct mib_entry *foe_mib_cpu[MAX_PPE_NUM];
656 dma_addr_t foe_mib_dev[MAX_PPE_NUM];
657 struct hnat_accounting *acct[MAX_PPE_NUM];
developerfd40db22021-04-29 10:08:25 +0800658 const struct mtk_hnat_data *data;
659
660 /*devices we plays for*/
661 char wan[IFNAMSIZ];
662 char lan[IFNAMSIZ];
663 char ppd[IFNAMSIZ];
664 u16 lvid;
665 u16 wvid;
666
667 struct reset_control *rstc;
668
developer471f6562021-05-10 20:48:34 +0800669 u8 ppe_num;
developerfd40db22021-04-29 10:08:25 +0800670 u8 gmac_num;
671 u8 wan_dsa_port;
672 struct ppe_mcast_table *pmcast;
673
674 u32 foe_etry_num;
developer8051e042022-04-08 13:26:36 +0800675 u32 etry_num_cfg;
developerfd40db22021-04-29 10:08:25 +0800676 struct net_device *g_ppdev;
developer8c9c0d02021-06-18 16:15:37 +0800677 struct net_device *g_wandev;
developerfd40db22021-04-29 10:08:25 +0800678 struct net_device *wifi_hook_if[MAX_IF_NUM];
679 struct extdev_entry *ext_if[MAX_EXT_DEVS];
680 struct timer_list hnat_sma_build_entry_timer;
681 struct timer_list hnat_reset_timestamp_timer;
682 struct timer_list hnat_mcast_check_timer;
developer30a47682021-11-02 17:06:14 +0800683 bool nf_stat_en;
developerfd40db22021-04-29 10:08:25 +0800684};
685
686struct extdev_entry {
687 char name[IFNAMSIZ];
688 struct net_device *dev;
689};
690
691struct tcpudphdr {
692 __be16 src;
693 __be16 dst;
694};
695
696enum FoeEntryState { INVALID = 0, UNBIND = 1, BIND = 2, FIN = 3 };
697
698enum FoeIpAct {
699 IPV4_HNAPT = 0,
700 IPV4_HNAT = 1,
701 IPV4_DSLITE = 3,
702 IPV6_3T_ROUTE = 4,
703 IPV6_5T_ROUTE = 5,
704 IPV6_6RD = 7,
705#if defined(CONFIG_MEDIATEK_NETSYS_V2)
706 IPV4_MAP_T = 8,
707 IPV4_MAP_E = 9,
708#else
709 IPV4_MAP_T = 6,
710 IPV4_MAP_E = 6,
711#endif
712};
713
714/*--------------------------------------------------------------------------*/
715/* Common Definition*/
716/*--------------------------------------------------------------------------*/
717
718#define HNAT_SW_VER "1.1.0"
719#define HASH_SEED_KEY 0x12345678
720
721/*PPE_TB_CFG value*/
722#define ENTRY_80B 1
723#define ENTRY_64B 0
724#define TABLE_1K 0
725#define TABLE_2K 1
726#define TABLE_4K 2
727#define TABLE_8K 3
728#define TABLE_16K 4
developerbc53e5f2021-05-21 10:07:17 +0800729#define TABLE_32K 5
developerfd40db22021-04-29 10:08:25 +0800730#define SMA_DROP 0 /* Drop the packet */
731#define SMA_DROP2 1 /* Drop the packet */
732#define SMA_ONLY_FWD_CPU 2 /* Only Forward to CPU */
733#define SMA_FWD_CPU_BUILD_ENTRY 3 /* Forward to CPU and build new FOE entry */
734#define HASH_MODE_0 0
735#define HASH_MODE_1 1
736#define HASH_MODE_2 2
737#define HASH_MODE_3 3
738
739/*PPE_FLOW_CFG*/
740#define BIT_FUC_FOE BIT(2)
741#define BIT_FMC_FOE BIT(1)
742#define BIT_FBC_FOE BIT(0)
743#define BIT_UDP_IP4F_NAT_EN BIT(7) /*Enable IPv4 fragment + UDP packet NAT*/
744#define BIT_IPV6_3T_ROUTE_EN BIT(8)
745#define BIT_IPV6_5T_ROUTE_EN BIT(9)
746#define BIT_IPV6_6RD_EN BIT(10)
747#define BIT_IPV4_NAT_EN BIT(12)
748#define BIT_IPV4_NAPT_EN BIT(13)
749#define BIT_IPV4_DSL_EN BIT(14)
750#define BIT_MIB_BUSY BIT(16)
751#define BIT_IPV4_NAT_FRAG_EN BIT(17)
752#define BIT_IPV4_HASH_GREK BIT(19)
753#define BIT_IPV6_HASH_GREK BIT(20)
754#define BIT_IPV4_MAPE_EN BIT(21)
755#define BIT_IPV4_MAPT_EN BIT(22)
756
757/*GDMA_FWD_CFG value*/
developer471f6562021-05-10 20:48:34 +0800758#define BITS_GDM_UFRC_P_PPE (NR_PPE0_PORT << 12)
759#define BITS_GDM_BFRC_P_PPE (NR_PPE0_PORT << 8)
760#define BITS_GDM_MFRC_P_PPE (NR_PPE0_PORT << 4)
761#define BITS_GDM_OFRC_P_PPE (NR_PPE0_PORT << 0)
developerfd40db22021-04-29 10:08:25 +0800762#define BITS_GDM_ALL_FRC_P_PPE \
763 (BITS_GDM_UFRC_P_PPE | BITS_GDM_BFRC_P_PPE | BITS_GDM_MFRC_P_PPE | \
764 BITS_GDM_OFRC_P_PPE)
765
766#define BITS_GDM_UFRC_P_CPU_PDMA (NR_PDMA_PORT << 12)
767#define BITS_GDM_BFRC_P_CPU_PDMA (NR_PDMA_PORT << 8)
768#define BITS_GDM_MFRC_P_CPU_PDMA (NR_PDMA_PORT << 4)
769#define BITS_GDM_OFRC_P_CPU_PDMA (NR_PDMA_PORT << 0)
770#define BITS_GDM_ALL_FRC_P_CPU_PDMA \
771 (BITS_GDM_UFRC_P_CPU_PDMA | BITS_GDM_BFRC_P_CPU_PDMA | \
772 BITS_GDM_MFRC_P_CPU_PDMA | BITS_GDM_OFRC_P_CPU_PDMA)
773
774#define BITS_GDM_UFRC_P_CPU_QDMA (NR_QDMA_PORT << 12)
775#define BITS_GDM_BFRC_P_CPU_QDMA (NR_QDMA_PORT << 8)
776#define BITS_GDM_MFRC_P_CPU_QDMA (NR_QDMA_PORT << 4)
777#define BITS_GDM_OFRC_P_CPU_QDMA (NR_QDMA_PORT << 0)
778#define BITS_GDM_ALL_FRC_P_CPU_QDMA \
779 (BITS_GDM_UFRC_P_CPU_QDMA | BITS_GDM_BFRC_P_CPU_QDMA | \
780 BITS_GDM_MFRC_P_CPU_QDMA | BITS_GDM_OFRC_P_CPU_QDMA)
781
782#define BITS_GDM_UFRC_P_DISCARD (NR_DISCARD << 12)
783#define BITS_GDM_BFRC_P_DISCARD (NR_DISCARD << 8)
784#define BITS_GDM_MFRC_P_DISCARD (NR_DISCARD << 4)
785#define BITS_GDM_OFRC_P_DISCARD (NR_DISCARD << 0)
786#define BITS_GDM_ALL_FRC_P_DISCARD \
787 (BITS_GDM_UFRC_P_DISCARD | BITS_GDM_BFRC_P_DISCARD | \
788 BITS_GDM_MFRC_P_DISCARD | BITS_GDM_OFRC_P_DISCARD)
789
790#define hnat_is_enabled(hnat_priv) (hnat_priv->enable)
791#define hnat_enabled(hnat_priv) (hnat_priv->enable = 1)
792#define hnat_disabled(hnat_priv) (hnat_priv->enable = 0)
793#define hnat_is_enabled1(hnat_priv) (hnat_priv->enable1)
794#define hnat_enabled1(hnat_priv) (hnat_priv->enable1 = 1)
795#define hnat_disabled1(hnat_priv) (hnat_priv->enable1 = 0)
796
797#define entry_hnat_is_bound(e) (e->bfib1.state == BIND)
798#define entry_hnat_state(e) (e->bfib1.state)
799
800#define skb_hnat_is_hashed(skb) \
801 (skb_hnat_entry(skb) != 0x3fff && skb_hnat_entry(skb) < hnat_priv->foe_etry_num)
802#define FROM_GE_LAN(skb) (skb_hnat_iface(skb) == FOE_MAGIC_GE_LAN)
803#define FROM_GE_WAN(skb) (skb_hnat_iface(skb) == FOE_MAGIC_GE_WAN)
804#define FROM_GE_PPD(skb) (skb_hnat_iface(skb) == FOE_MAGIC_GE_PPD)
805#define FROM_GE_VIRTUAL(skb) (skb_hnat_iface(skb) == FOE_MAGIC_GE_VIRTUAL)
806#define FROM_EXT(skb) (skb_hnat_iface(skb) == FOE_MAGIC_EXT)
developere567ad32021-05-25 17:16:17 +0800807#define FROM_WED(skb) ((skb_hnat_iface(skb) == FOE_MAGIC_WED0) || \
808 (skb_hnat_iface(skb) == FOE_MAGIC_WED1))
developerfd40db22021-04-29 10:08:25 +0800809#define FOE_MAGIC_GE_LAN 0x1
810#define FOE_MAGIC_GE_WAN 0x2
811#define FOE_MAGIC_EXT 0x3
812#define FOE_MAGIC_GE_VIRTUAL 0x4
813#define FOE_MAGIC_GE_PPD 0x5
developere567ad32021-05-25 17:16:17 +0800814#define FOE_MAGIC_WED0 0x78
815#define FOE_MAGIC_WED1 0x79
developerfd40db22021-04-29 10:08:25 +0800816#define FOE_INVALID 0xf
817#define index6b(i) (0x3fU - i)
818
819#define IPV4_HNAPT 0
820#define IPV4_HNAT 1
821#define IP_FORMAT(addr) \
822 (((unsigned char *)&addr)[3], ((unsigned char *)&addr)[2], \
823 ((unsigned char *)&addr)[1], ((unsigned char *)&addr)[0])
824
825/*PSE Ports*/
826#define NR_PDMA_PORT 0
827#define NR_GMAC1_PORT 1
828#define NR_GMAC2_PORT 2
developer471f6562021-05-10 20:48:34 +0800829#if defined(CONFIG_MEDIATEK_NETSYS_V2)
830#define NR_WHNAT_WDMA_PORT EINVAL
831#define NR_PPE0_PORT 3
832#define NR_PPE1_PORT 4
833#else
developerfd40db22021-04-29 10:08:25 +0800834#define NR_WHNAT_WDMA_PORT 3
developer471f6562021-05-10 20:48:34 +0800835#define NR_PPE0_PORT 4
836#endif
developerfd40db22021-04-29 10:08:25 +0800837#define NR_QDMA_PORT 5
838#define NR_DISCARD 7
839#define NR_WDMA0_PORT 8
840#define NR_WDMA1_PORT 9
841#define LAN_DEV_NAME hnat_priv->lan
842#define IS_WAN(dev) \
843 (!strncmp((dev)->name, hnat_priv->wan, strlen(hnat_priv->wan)))
844#define IS_LAN(dev) (!strncmp(dev->name, LAN_DEV_NAME, strlen(LAN_DEV_NAME)))
845#define IS_BR(dev) (!strncmp(dev->name, "br", 2))
846#define IS_WHNAT(dev) \
847 ((hnat_priv->data->whnat && \
848 (get_wifi_hook_if_index_from_dev(dev) != 0)) ? 1 : 0)
849#define IS_EXT(dev) ((get_index_from_dev(dev) != 0) ? 1 : 0)
850#define IS_PPD(dev) (!strcmp(dev->name, hnat_priv->ppd))
851#define IS_IPV4_HNAPT(x) (((x)->bfib1.pkt_type == IPV4_HNAPT) ? 1 : 0)
852#define IS_IPV4_HNAT(x) (((x)->bfib1.pkt_type == IPV4_HNAT) ? 1 : 0)
853#define IS_IPV4_GRP(x) (IS_IPV4_HNAPT(x) | IS_IPV4_HNAT(x))
854#define IS_IPV4_DSLITE(x) (((x)->bfib1.pkt_type == IPV4_DSLITE) ? 1 : 0)
855#define IS_IPV4_MAPE(x) (((x)->bfib1.pkt_type == IPV4_MAP_E) ? 1 : 0)
856#define IS_IPV4_MAPT(x) (((x)->bfib1.pkt_type == IPV4_MAP_T) ? 1 : 0)
857#define IS_IPV6_3T_ROUTE(x) (((x)->bfib1.pkt_type == IPV6_3T_ROUTE) ? 1 : 0)
858#define IS_IPV6_5T_ROUTE(x) (((x)->bfib1.pkt_type == IPV6_5T_ROUTE) ? 1 : 0)
859#define IS_IPV6_6RD(x) (((x)->bfib1.pkt_type == IPV6_6RD) ? 1 : 0)
860#define IS_IPV6_GRP(x) \
861 (IS_IPV6_3T_ROUTE(x) | IS_IPV6_5T_ROUTE(x) | IS_IPV6_6RD(x) | \
862 IS_IPV4_DSLITE(x) | IS_IPV4_MAPE(x) | IS_IPV4_MAPT(x))
863#define IS_BOND_MODE (!strncmp(LAN_DEV_NAME, "bond", 4))
864#define IS_GMAC1_MODE ((hnat_priv->gmac_num == 1) ? 1 : 0)
developeraf07fad2021-11-19 17:53:42 +0800865#define IS_HQOS_MODE (qos_toggle == 1)
866#define IS_PPPQ_MODE (qos_toggle == 2) /* Per Port Per Queue */
developer70cdf6e2021-12-07 18:58:35 +0800867#define MAX_PPPQ_PORT_NUM 6
developerfd40db22021-04-29 10:08:25 +0800868
869#define es(entry) (entry_state[entry->bfib1.state])
870#define ei(entry, end) (hnat_priv->foe_etry_num - (int)(end - entry))
871#define pt(entry) (packet_type[entry->ipv4_hnapt.bfib1.pkt_type])
872#define ipv4_smac(mac, e) \
873 ({ \
874 mac[0] = e->ipv4_hnapt.smac_hi[3]; \
875 mac[1] = e->ipv4_hnapt.smac_hi[2]; \
876 mac[2] = e->ipv4_hnapt.smac_hi[1]; \
877 mac[3] = e->ipv4_hnapt.smac_hi[0]; \
878 mac[4] = e->ipv4_hnapt.smac_lo[1]; \
879 mac[5] = e->ipv4_hnapt.smac_lo[0]; \
880 })
881#define ipv4_dmac(mac, e) \
882 ({ \
883 mac[0] = e->ipv4_hnapt.dmac_hi[3]; \
884 mac[1] = e->ipv4_hnapt.dmac_hi[2]; \
885 mac[2] = e->ipv4_hnapt.dmac_hi[1]; \
886 mac[3] = e->ipv4_hnapt.dmac_hi[0]; \
887 mac[4] = e->ipv4_hnapt.dmac_lo[1]; \
888 mac[5] = e->ipv4_hnapt.dmac_lo[0]; \
889 })
890
891#define IS_DSA_LAN(dev) (!strncmp(dev->name, "lan", 3))
892#define IS_DSA_WAN(dev) (!strncmp(dev->name, "wan", 3))
893#define NONE_DSA_PORT 0xff
894#define MAX_CRSN_NUM 32
895#define IPV6_HDR_LEN 40
896
897/*QDMA_PAGE value*/
898#define NUM_OF_Q_PER_PAGE 16
899
900/*IPv6 Header*/
901#ifndef NEXTHDR_IPIP
902#define NEXTHDR_IPIP 4
903#endif
904
905extern const struct of_device_id of_hnat_match[];
906extern struct mtk_hnat *hnat_priv;
907
908#if defined(CONFIG_NET_DSA_MT7530)
developeraf07fad2021-11-19 17:53:42 +0800909u32 hnat_dsa_fill_stag(const struct net_device *netdev,
910 struct foe_entry *entry,
911 struct flow_offload_hw_path *hw_path,
912 u16 eth_proto, int mape);
developerfd40db22021-04-29 10:08:25 +0800913
914static inline bool hnat_dsa_is_enable(struct mtk_hnat *priv)
915{
916 return (priv->wan_dsa_port != NONE_DSA_PORT);
917}
918#else
developeraf07fad2021-11-19 17:53:42 +0800919static inline u32 hnat_dsa_fill_stag(const struct net_device *netdev,
920 struct foe_entry *entry,
921 struct flow_offload_hw_path *hw_path,
922 u16 eth_proto, int mape)
developerfd40db22021-04-29 10:08:25 +0800923{
924}
925
926static inline bool hnat_dsa_is_enable(struct mtk_hnat *priv)
927{
928 return false;
929}
930#endif
931
932void hnat_deinit_debugfs(struct mtk_hnat *h);
933int hnat_init_debugfs(struct mtk_hnat *h);
934int hnat_register_nf_hooks(void);
935void hnat_unregister_nf_hooks(void);
936int whnat_adjust_nf_hooks(void);
937int mtk_hqos_ptype_cb(struct sk_buff *skb, struct net_device *dev,
938 struct packet_type *pt, struct net_device *unused);
939extern int dbg_cpu_reason;
940extern int debug_level;
941extern int hook_toggle;
942extern int mape_toggle;
developeraf07fad2021-11-19 17:53:42 +0800943extern int qos_toggle;
developerfd40db22021-04-29 10:08:25 +0800944
945int ext_if_add(struct extdev_entry *ext_entry);
946int ext_if_del(struct extdev_entry *ext_entry);
947void cr_set_field(void __iomem *reg, u32 field, u32 val);
948int mtk_sw_nat_hook_tx(struct sk_buff *skb, int gmac_no);
949int mtk_sw_nat_hook_rx(struct sk_buff *skb);
950void mtk_ppe_dev_register_hook(struct net_device *dev);
951void mtk_ppe_dev_unregister_hook(struct net_device *dev);
952int nf_hnat_netdevice_event(struct notifier_block *unused, unsigned long event,
953 void *ptr);
954int nf_hnat_netevent_handler(struct notifier_block *unused, unsigned long event,
955 void *ptr);
956uint32_t foe_dump_pkt(struct sk_buff *skb);
957uint32_t hnat_cpu_reason_cnt(struct sk_buff *skb);
958int hnat_enable_hook(void);
959int hnat_disable_hook(void);
960void hnat_cache_ebl(int enable);
developer70cdf6e2021-12-07 18:58:35 +0800961void hnat_qos_shaper_ebl(u32 id, u32 enable);
developerfd40db22021-04-29 10:08:25 +0800962void set_gmac_ppe_fwd(int gmac_no, int enable);
developer4c32b7a2021-11-13 16:46:43 +0800963int entry_detail(u32 ppe_id, int index);
developer731b98f2021-09-17 17:44:37 +0800964int entry_delete_by_mac(u8 *mac);
developer4c32b7a2021-11-13 16:46:43 +0800965int entry_delete(u32 ppe_id, int index);
developer8051e042022-04-08 13:26:36 +0800966int hnat_warm_init(void);
967
developer4c32b7a2021-11-13 16:46:43 +0800968struct hnat_accounting *hnat_get_count(struct mtk_hnat *h, u32 ppe_id,
developer30a47682021-11-02 17:06:14 +0800969 u32 index, struct hnat_accounting *diff);
developerfd40db22021-04-29 10:08:25 +0800970
971static inline u16 foe_timestamp(struct mtk_hnat *h)
972{
973 return (readl(hnat_priv->fe_base + 0x0010)) & 0xffff;
974}