1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /* Copyright (C) 2022 Microchip Technology Inc. and its subsidiaries.
3  * Microchip VCAP API
4  */
5 
6 #ifndef __VCAP_API__
7 #define __VCAP_API__
8 
9 #include <linux/types.h>
10 #include <linux/list.h>
11 #include <linux/netdevice.h>
12 
13 /* Use the generated API model */
14 #include "vcap_ag_api.h"
15 
16 #define VCAP_CID_LOOKUP_SIZE          100000 /* Chains in a lookup */
17 #define VCAP_CID_INGRESS_L0          1000000 /* Ingress Stage 1 Lookup 0 */
18 #define VCAP_CID_INGRESS_L1          1100000 /* Ingress Stage 1 Lookup 1 */
19 #define VCAP_CID_INGRESS_L2          1200000 /* Ingress Stage 1 Lookup 2 */
20 #define VCAP_CID_INGRESS_L3          1300000 /* Ingress Stage 1 Lookup 3 */
21 #define VCAP_CID_INGRESS_L4          1400000 /* Ingress Stage 1 Lookup 4 */
22 #define VCAP_CID_INGRESS_L5          1500000 /* Ingress Stage 1 Lookup 5 */
23 
24 #define VCAP_CID_PREROUTING_IPV6     3000000 /* Prerouting Stage */
25 #define VCAP_CID_PREROUTING          6000000 /* Prerouting Stage */
26 
27 #define VCAP_CID_INGRESS_STAGE2_L0   8000000 /* Ingress Stage 2 Lookup 0 */
28 #define VCAP_CID_INGRESS_STAGE2_L1   8100000 /* Ingress Stage 2 Lookup 1 */
29 #define VCAP_CID_INGRESS_STAGE2_L2   8200000 /* Ingress Stage 2 Lookup 2 */
30 #define VCAP_CID_INGRESS_STAGE2_L3   8300000 /* Ingress Stage 2 Lookup 3 */
31 
32 #define VCAP_CID_EGRESS_L0           10000000 /* Egress Lookup 0 */
33 #define VCAP_CID_EGRESS_L1           10100000 /* Egress Lookup 1 */
34 
35 #define VCAP_CID_EGRESS_STAGE2_L0    20000000 /* Egress Stage 2 Lookup 0 */
36 #define VCAP_CID_EGRESS_STAGE2_L1    20100000 /* Egress Stage 2 Lookup 1 */
37 
38 /* Known users of the VCAP API */
39 enum vcap_user {
40 	VCAP_USER_PTP,
41 	VCAP_USER_MRP,
42 	VCAP_USER_CFM,
43 	VCAP_USER_VLAN,
44 	VCAP_USER_QOS,
45 	VCAP_USER_VCAP_UTIL,
46 	VCAP_USER_TC,
47 	VCAP_USER_TC_EXTRA,
48 
49 	/* add new users above here */
50 
51 	/* used to define VCAP_USER_MAX below */
52 	__VCAP_USER_AFTER_LAST,
53 	VCAP_USER_MAX = __VCAP_USER_AFTER_LAST - 1,
54 };
55 
56 /* VCAP information used for displaying data */
57 struct vcap_statistics {
58 	char *name;
59 	int count;
60 	const char * const *keyfield_set_names;
61 	const char * const *actionfield_set_names;
62 	const char * const *keyfield_names;
63 	const char * const *actionfield_names;
64 };
65 
66 /* VCAP key/action field type, position and width */
67 struct vcap_field {
68 	u16 type;
69 	u16 width;
70 	u16 offset;
71 };
72 
73 /* VCAP keyset or actionset type and width */
74 struct vcap_set {
75 	u8 type_id;
76 	u8 sw_per_item;
77 	u8 sw_cnt;
78 };
79 
80 /* VCAP typegroup position and bitvalue */
81 struct vcap_typegroup {
82 	u16 offset;
83 	u16 width;
84 	u16 value;
85 };
86 
87 /* VCAP model data */
88 struct vcap_info {
89 	char *name; /* user-friendly name */
90 	u16 rows; /* number of row in instance */
91 	u16 sw_count; /* maximum subwords used per rule */
92 	u16 sw_width; /* bits per subword in a keyset */
93 	u16 sticky_width; /* sticky bits per rule */
94 	u16 act_width;  /* bits per subword in an actionset */
95 	u16 default_cnt; /* number of default rules */
96 	u16 require_cnt_dis; /* not used */
97 	u16 version; /* vcap rtl version */
98 	const struct vcap_set *keyfield_set; /* keysets */
99 	int keyfield_set_size; /* number of keysets */
100 	const struct vcap_set *actionfield_set; /* actionsets */
101 	int actionfield_set_size; /* number of actionsets */
102 	/* map of keys per keyset */
103 	const struct vcap_field **keyfield_set_map;
104 	/* number of entries in the above map */
105 	int *keyfield_set_map_size;
106 	/* map of actions per actionset */
107 	const struct vcap_field **actionfield_set_map;
108 	/* number of entries in the above map */
109 	int *actionfield_set_map_size;
110 	/* map of keyset typegroups per subword size */
111 	const struct vcap_typegroup **keyfield_set_typegroups;
112 	/* map of actionset typegroups per subword size */
113 	const struct vcap_typegroup **actionfield_set_typegroups;
114 };
115 
116 enum vcap_field_type {
117 	VCAP_FIELD_BIT,
118 	VCAP_FIELD_U32,
119 	VCAP_FIELD_U48,
120 	VCAP_FIELD_U56,
121 	VCAP_FIELD_U64,
122 	VCAP_FIELD_U72,
123 	VCAP_FIELD_U112,
124 	VCAP_FIELD_U128,
125 };
126 
127 /* VCAP rule data towards the VCAP cache */
128 struct vcap_cache_data {
129 	u32 *keystream;
130 	u32 *maskstream;
131 	u32 *actionstream;
132 	u32 counter;
133 	bool sticky;
134 };
135 
136 /* Selects which part of the rule must be updated */
137 enum vcap_selection {
138 	VCAP_SEL_ENTRY = 0x01,
139 	VCAP_SEL_ACTION = 0x02,
140 	VCAP_SEL_COUNTER = 0x04,
141 	VCAP_SEL_ALL = 0xff,
142 };
143 
144 /* Commands towards the VCAP cache */
145 enum vcap_command {
146 	VCAP_CMD_WRITE = 0,
147 	VCAP_CMD_READ = 1,
148 	VCAP_CMD_MOVE_DOWN = 2,
149 	VCAP_CMD_MOVE_UP = 3,
150 	VCAP_CMD_INITIALIZE = 4,
151 };
152 
153 enum vcap_rule_error {
154 	VCAP_ERR_NONE = 0,  /* No known error */
155 	VCAP_ERR_NO_ADMIN,  /* No admin instance */
156 	VCAP_ERR_NO_NETDEV,  /* No netdev instance */
157 	VCAP_ERR_NO_KEYSET_MATCH, /* No keyset matched the rule keys */
158 	VCAP_ERR_NO_ACTIONSET_MATCH, /* No actionset matched the rule actions */
159 	VCAP_ERR_NO_PORT_KEYSET_MATCH, /* No port keyset matched the rule keys */
160 };
161 
162 /* Administration of each VCAP instance */
163 struct vcap_admin {
164 	struct list_head list; /* for insertion in vcap_control */
165 	struct list_head rules; /* list of rules */
166 	struct list_head enabled; /* list of enabled ports */
167 	struct mutex lock; /* control access to rules */
168 	enum vcap_type vtype;  /* type of vcap */
169 	int vinst; /* instance number within the same type */
170 	int first_cid; /* first chain id in this vcap */
171 	int last_cid; /* last chain id in this vcap */
172 	int tgt_inst; /* hardware instance number */
173 	int lookups; /* number of lookups in this vcap type */
174 	int lookups_per_instance; /* number of lookups in this instance */
175 	int last_valid_addr; /* top of address range to be used */
176 	int first_valid_addr; /* bottom of address range to be used */
177 	int last_used_addr;  /* address of lowest added rule */
178 	bool w32be; /* vcap uses "32bit-word big-endian" encoding */
179 	struct vcap_cache_data cache; /* encoded rule data */
180 };
181 
182 /* Client supplied VCAP rule data */
183 struct vcap_rule {
184 	int vcap_chain_id; /* chain used for this rule */
185 	enum vcap_user user; /* rule owner */
186 	u16 priority;
187 	u32 id;  /* vcap rule id, must be unique, 0 will auto-generate a value */
188 	u64 cookie;  /* used by the client to identify the rule */
189 	struct list_head keyfields;  /* list of vcap_client_keyfield */
190 	struct list_head actionfields;  /* list of vcap_client_actionfield */
191 	enum vcap_keyfield_set keyset; /* keyset used: may be derived from fields */
192 	enum vcap_actionfield_set actionset; /* actionset used: may be derived from fields */
193 	enum vcap_rule_error exterr; /* extended error - used by TC */
194 	u64 client; /* space for client defined data */
195 };
196 
197 /* List of keysets */
198 struct vcap_keyset_list {
199 	int max; /* size of the keyset list */
200 	int cnt; /* count of keysets actually in the list */
201 	enum vcap_keyfield_set *keysets; /* the list of keysets */
202 };
203 
204 /* Client output printf-like function with destination */
205 struct vcap_output_print {
206 	__printf(2, 3)
207 	void (*prf)(void *out, const char *fmt, ...);
208 	void *dst;
209 };
210 
211 /* Client supplied VCAP callback operations */
212 struct vcap_operations {
213 	/* validate port keyset operation */
214 	enum vcap_keyfield_set (*validate_keyset)
215 		(struct net_device *ndev,
216 		 struct vcap_admin *admin,
217 		 struct vcap_rule *rule,
218 		 struct vcap_keyset_list *kslist,
219 		 u16 l3_proto);
220 	/* add default rule fields for the selected keyset operations */
221 	void (*add_default_fields)
222 		(struct net_device *ndev,
223 		 struct vcap_admin *admin,
224 		 struct vcap_rule *rule);
225 	/* cache operations */
226 	void (*cache_erase)
227 		(struct vcap_admin *admin);
228 	void (*cache_write)
229 		(struct net_device *ndev,
230 		 struct vcap_admin *admin,
231 		 enum vcap_selection sel,
232 		 u32 idx, u32 count);
233 	void (*cache_read)
234 		(struct net_device *ndev,
235 		 struct vcap_admin *admin,
236 		 enum vcap_selection sel,
237 		 u32 idx,
238 		 u32 count);
239 	/* block operations */
240 	void (*init)
241 		(struct net_device *ndev,
242 		 struct vcap_admin *admin,
243 		 u32 addr,
244 		 u32 count);
245 	void (*update)
246 		(struct net_device *ndev,
247 		 struct vcap_admin *admin,
248 		 enum vcap_command cmd,
249 		 enum vcap_selection sel,
250 		 u32 addr);
251 	void (*move)
252 		(struct net_device *ndev,
253 		 struct vcap_admin *admin,
254 		 u32 addr,
255 		 int offset,
256 		 int count);
257 	/* informational */
258 	int (*port_info)
259 		(struct net_device *ndev,
260 		 struct vcap_admin *admin,
261 		 struct vcap_output_print *out);
262 	/* enable/disable the lookups in a vcap instance */
263 	int (*enable)
264 		(struct net_device *ndev,
265 		 struct vcap_admin *admin,
266 		 bool enable);
267 };
268 
269 /* VCAP API Client control interface */
270 struct vcap_control {
271 	struct vcap_operations *ops;  /* client supplied operations */
272 	const struct vcap_info *vcaps; /* client supplied vcap models */
273 	const struct vcap_statistics *stats; /* client supplied vcap stats */
274 	struct list_head list; /* list of vcap instances */
275 };
276 
277 /* Set client control interface on the API */
278 int vcap_api_set_client(struct vcap_control *vctrl);
279 
280 #endif /* __VCAP_API__ */
281