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 	enum vcap_type vtype;  /* type of vcap */
171 	int vinst; /* instance number within the same type */
172 	int first_cid; /* first chain id in this vcap */
173 	int last_cid; /* last chain id in this vcap */
174 	int tgt_inst; /* hardware instance number */
175 	int lookups; /* number of lookups in this vcap type */
176 	int lookups_per_instance; /* number of lookups in this instance */
177 	int last_valid_addr; /* top of address range to be used */
178 	int first_valid_addr; /* bottom of address range to be used */
179 	int last_used_addr;  /* address of lowest added rule */
180 	bool w32be; /* vcap uses "32bit-word big-endian" encoding */
181 	struct vcap_cache_data cache; /* encoded rule data */
182 };
183 
184 /* Client supplied VCAP rule data */
185 struct vcap_rule {
186 	int vcap_chain_id; /* chain used for this rule */
187 	enum vcap_user user; /* rule owner */
188 	u16 priority;
189 	u32 id;  /* vcap rule id, must be unique, 0 will auto-generate a value */
190 	u64 cookie;  /* used by the client to identify the rule */
191 	struct list_head keyfields;  /* list of vcap_client_keyfield */
192 	struct list_head actionfields;  /* list of vcap_client_actionfield */
193 	enum vcap_keyfield_set keyset; /* keyset used: may be derived from fields */
194 	enum vcap_actionfield_set actionset; /* actionset used: may be derived from fields */
195 	enum vcap_rule_error exterr; /* extended error - used by TC */
196 	u64 client; /* space for client defined data */
197 };
198 
199 /* List of keysets */
200 struct vcap_keyset_list {
201 	int max; /* size of the keyset list */
202 	int cnt; /* count of keysets actually in the list */
203 	enum vcap_keyfield_set *keysets; /* the list of keysets */
204 };
205 
206 /* Client supplied VCAP callback operations */
207 struct vcap_operations {
208 	/* validate port keyset operation */
209 	enum vcap_keyfield_set (*validate_keyset)
210 		(struct net_device *ndev,
211 		 struct vcap_admin *admin,
212 		 struct vcap_rule *rule,
213 		 struct vcap_keyset_list *kslist,
214 		 u16 l3_proto);
215 	/* add default rule fields for the selected keyset operations */
216 	void (*add_default_fields)
217 		(struct net_device *ndev,
218 		 struct vcap_admin *admin,
219 		 struct vcap_rule *rule);
220 	/* cache operations */
221 	void (*cache_erase)
222 		(struct vcap_admin *admin);
223 	void (*cache_write)
224 		(struct net_device *ndev,
225 		 struct vcap_admin *admin,
226 		 enum vcap_selection sel,
227 		 u32 idx, u32 count);
228 	void (*cache_read)
229 		(struct net_device *ndev,
230 		 struct vcap_admin *admin,
231 		 enum vcap_selection sel,
232 		 u32 idx,
233 		 u32 count);
234 	/* block operations */
235 	void (*init)
236 		(struct net_device *ndev,
237 		 struct vcap_admin *admin,
238 		 u32 addr,
239 		 u32 count);
240 	void (*update)
241 		(struct net_device *ndev,
242 		 struct vcap_admin *admin,
243 		 enum vcap_command cmd,
244 		 enum vcap_selection sel,
245 		 u32 addr);
246 	void (*move)
247 		(struct net_device *ndev,
248 		 struct vcap_admin *admin,
249 		 u32 addr,
250 		 int offset,
251 		 int count);
252 	/* informational */
253 	int (*port_info)
254 		(struct net_device *ndev,
255 		 enum vcap_type vtype,
256 		 int (*pf)(void *out, int arg, const char *fmt, ...),
257 		 void *out,
258 		 int arg);
259 	/* enable/disable the lookups in a vcap instance */
260 	int (*enable)
261 		(struct net_device *ndev,
262 		 struct vcap_admin *admin,
263 		 bool enable);
264 };
265 
266 /* VCAP API Client control interface */
267 struct vcap_control {
268 	u32 rule_id; /* last used rule id (unique across VCAP instances) */
269 	struct vcap_operations *ops;  /* client supplied operations */
270 	const struct vcap_info *vcaps; /* client supplied vcap models */
271 	const struct vcap_statistics *stats; /* client supplied vcap stats */
272 	struct list_head list; /* list of vcap instances */
273 };
274 
275 /* Set client control interface on the API */
276 int vcap_api_set_client(struct vcap_control *vctrl);
277 
278 #endif /* __VCAP_API__ */
279