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