blob: 961aa030f9835d880b36151f76cebf09fa0721fd [file] [log] [blame]
developere5e687d2023-08-08 16:05:33 +08001/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * Copyright (c) 2023 MediaTek Inc. All Rights Reserved.
4 *
5 * Author: Ren-Ting Wang <ren-ting.wang@mediatek.com>
6 */
7
8#ifndef _TOPS_TUNNEL_H_
9#define _TOPS_TUNNEL_H_
10
11#include <linux/bitmap.h>
12#include <linux/hashtable.h>
13#include <linux/if_ether.h>
14#include <linux/ip.h>
15#include <linux/kthread.h>
16#include <linux/netdevice.h>
17#include <linux/refcount.h>
18#include <linux/spinlock.h>
19#include <linux/types.h>
20
21#include "protocol/l2tp/l2tp.h"
22
23/* tunnel info status */
24#define TNL_STA_UNINIT (BIT(TNL_STATUS_UNINIT))
25#define TNL_STA_INIT (BIT(TNL_STATUS_INIT))
26#define TNL_STA_QUEUED (BIT(TNL_STATUS_QUEUED))
27#define TNL_STA_UPDATING (BIT(TNL_STATUS_UPDATING))
28#define TNL_STA_UPDATED (BIT(TNL_STATUS_UPDATED))
29#define TNL_STA_DIP_UPDATE (BIT(TNL_STATUS_DIP_UPDATE))
30#define TNL_STA_DELETING (BIT(TNL_STATUS_DELETING))
31
32/* tunnel params flags */
33#define TNL_DECAP_ENABLE (BIT(TNL_PARAMS_DECAP_ENABLE_BIT))
34#define TNL_ENCAP_ENABLE (BIT(TNL_PARAMS_ENCAP_ENABLE_BIT))
35
36/* tunnel info flags */
37#define TNL_INFO_DEBUG (BIT(TNL_INFO_DEBUG_BIT))
38
39struct tops_tnl_info;
40struct tops_tnl_params;
41
42/*
43 * tops_crsn
44 * TOPS_CRSN_TNL_ID_START
45 * TOPS_CRSN_TNL_ID_END
46 * APMCU checks whether tops_crsn is in this range to know if this packet
47 * was processed by TOPS previously.
48 */
49enum tops_crsn {
50 TOPS_CRSN_IGNORE = 0x00,
51 TOPS_CRSN_TNL_ID_START = 0x10,
52 TOPS_CRSN_TNL_ID_END = 0x2F,
53};
54
55enum tops_entry_type {
56 TOPS_ENTRY_NONE = 0,
57 TOPS_ENTRY_GRETAP,
58 TOPS_ENTRY_PPTP,
59 TOPS_ENTRY_IP_L2TP,
60 TOPS_ENTRY_UDP_L2TP_CTRL,
61 TOPS_ENTRY_UDP_L2TP_DATA = 5,
62 TOPS_ENTRY_VXLAN,
63 TOPS_ENTRY_NATT,
64 TOPS_ENTRY_CAPWAP_CTRL,
65 TOPS_ENTRY_CAPWAP_DATA,
66 TOPS_ENTRY_CAPWAP_DTLS = 10,
67 TOPS_ENTRY_IPSEC_ESP,
68 TOPS_ENTRY_IPSEC_AH,
69
70 __TOPS_ENTRY_MAX = CONFIG_TOPS_TNL_NUM,
71};
72
73enum tops_tunnel_mbox_cmd {
74 TOPS_TNL_MBOX_CMD_RESV,
75 TOPS_TNL_START_ADDR_SYNC,
76
77 __TOPS_TNL_MBOX_CMD_MAX,
78};
79
80enum tunnel_ctrl_event {
81 TUNNEL_CTRL_EVENT_NULL,
82 TUNNEL_CTRL_EVENT_NEW,
83 TUNNEL_CTRL_EVENT_DEL,
84 TUNNEL_CTRL_EVENT_DIP_UPDATE,
85
86 __TUNNEL_CTRL_EVENT_MAX,
87};
88
89enum tnl_status {
90 TNL_STATUS_UNINIT,
91 TNL_STATUS_INIT,
92 TNL_STATUS_QUEUED,
93 TNL_STATUS_UPDATING,
94 TNL_STATUS_UPDATED,
95 TNL_STATUS_DIP_UPDATE,
96 TNL_STATUS_DELETING,
97
98 __TNL_STATUS_MAX,
99};
100
101enum tops_tnl_params_flag {
102 TNL_PARAMS_DECAP_ENABLE_BIT,
103 TNL_PARAMS_ENCAP_ENABLE_BIT,
104};
105
106enum tops_tnl_info_flag {
107 TNL_INFO_DEBUG_BIT,
108};
109
110/* record outer tunnel header data for HW offloading */
111struct tops_tnl_params {
112 u8 daddr[ETH_ALEN];
113 u8 saddr[ETH_ALEN];
114 __be32 dip;
115 __be32 sip;
116 __be16 dport;
117 __be16 sport;
118 u16 protocol;
119 u8 tops_entry_proto;
120 u8 flag; /* bit: enum tops_tnl_params_flag */
121 union {
122 struct l2tp_param l2tp; /* 4B */
123 } priv;
124} __packed __aligned(16);
125
126struct tops_tnl_info {
127 struct tops_tnl_params tnl_params;
128 struct tops_tnl_params cache;
129 struct list_head sync_node;
130 struct hlist_node hlist;
131 struct net_device *dev;
132 struct timer_list taging;
133 spinlock_t lock;
134 u32 tnl_idx;
135 u32 status;
136 u32 flag; /* bit: enum tops_tnl_info_flag */
137 refcount_t refcnt;
138} __aligned(16);
139
140struct tops_tnl_type {
141 const char *type_name;
142 int (*tnl_decap_param_setup)(struct sk_buff *skb,
143 struct tops_tnl_params *tnl_params);
144 int (*tnl_encap_param_setup)(struct sk_buff *skb,
145 struct tops_tnl_params *tnl_params);
146 int (*tnl_debug_param_setup)(const char *buf, int *ofs,
147 struct tops_tnl_params *tnl_params);
148 int (*tnl_dump_param)(char *buf, struct tops_tnl_params *tnl_params);
149 bool (*tnl_info_match)(struct tops_tnl_params *params1,
150 struct tops_tnl_params *params2);
151 bool (*tnl_decap_offloadable)(struct sk_buff *skb);
152 enum tops_entry_type tops_entry;
153 bool has_inner_eth;
154};
155
156void mtk_tops_tnl_info_submit_no_tnl_lock(struct tops_tnl_info *tnl_info);
157void mtk_tops_tnl_info_submit(struct tops_tnl_info *tnl_info);
158struct tops_tnl_info *mtk_tops_tnl_info_find(struct tops_tnl_params *tnl_params);
159struct tops_tnl_info *mtk_tops_tnl_info_alloc(void);
160void mtk_tops_tnl_info_hash(struct tops_tnl_info *tnl_info);
161
162int mtk_tops_tnl_offload_init(struct platform_device *pdev);
163void mtk_tops_tnl_offload_deinit(struct platform_device *pdev);
164int mtk_tops_tnl_offload_proto_setup(struct platform_device *pdev);
165void mtk_tops_tnl_offload_proto_teardown(struct platform_device *pdev);
166void mtk_tops_tnl_offload_flush(void);
167void mtk_tops_tnl_offload_recover(void);
168void mtk_tops_tnl_offload_netdev_down(struct net_device *ndev);
169
170struct tops_tnl_type *mtk_tops_tnl_type_get_by_name(const char *name);
171int mtk_tops_tnl_type_register(struct tops_tnl_type *tnl_type);
172void mtk_tops_tnl_type_unregister(struct tops_tnl_type *tnl_type);
173#endif /* _TOPS_TUNNEL_H_ */