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 	bool ingress; /* chain traffic direction */
180 	struct vcap_cache_data cache; /* encoded rule data */
181 };
182 
183 /* Client supplied VCAP rule data */
184 struct vcap_rule {
185 	int vcap_chain_id; /* chain used for this rule */
186 	enum vcap_user user; /* rule owner */
187 	u16 priority;
188 	u32 id;  /* vcap rule id, must be unique, 0 will auto-generate a value */
189 	u64 cookie;  /* used by the client to identify the rule */
190 	struct list_head keyfields;  /* list of vcap_client_keyfield */
191 	struct list_head actionfields;  /* list of vcap_client_actionfield */
192 	enum vcap_keyfield_set keyset; /* keyset used: may be derived from fields */
193 	enum vcap_actionfield_set actionset; /* actionset used: may be derived from fields */
194 	enum vcap_rule_error exterr; /* extended error - used by TC */
195 	u64 client; /* space for client defined data */
196 };
197 
198 /* List of keysets */
199 struct vcap_keyset_list {
200 	int max; /* size of the keyset list */
201 	int cnt; /* count of keysets actually in the list */
202 	enum vcap_keyfield_set *keysets; /* the list of keysets */
203 };
204 
205 /* List of actionsets */
206 struct vcap_actionset_list {
207 	int max; /* size of the actionset list */
208 	int cnt; /* count of actionsets actually in the list */
209 	enum vcap_actionfield_set *actionsets; /* the list of actionsets */
210 };
211 
212 /* Client output printf-like function with destination */
213 struct vcap_output_print {
214 	__printf(2, 3)
215 	void (*prf)(void *out, const char *fmt, ...);
216 	void *dst;
217 };
218 
219 /* Client supplied VCAP callback operations */
220 struct vcap_operations {
221 	/* validate port keyset operation */
222 	enum vcap_keyfield_set (*validate_keyset)
223 		(struct net_device *ndev,
224 		 struct vcap_admin *admin,
225 		 struct vcap_rule *rule,
226 		 struct vcap_keyset_list *kslist,
227 		 u16 l3_proto);
228 	/* add default rule fields for the selected keyset operations */
229 	void (*add_default_fields)
230 		(struct net_device *ndev,
231 		 struct vcap_admin *admin,
232 		 struct vcap_rule *rule);
233 	/* cache operations */
234 	void (*cache_erase)
235 		(struct vcap_admin *admin);
236 	void (*cache_write)
237 		(struct net_device *ndev,
238 		 struct vcap_admin *admin,
239 		 enum vcap_selection sel,
240 		 u32 idx, u32 count);
241 	void (*cache_read)
242 		(struct net_device *ndev,
243 		 struct vcap_admin *admin,
244 		 enum vcap_selection sel,
245 		 u32 idx,
246 		 u32 count);
247 	/* block operations */
248 	void (*init)
249 		(struct net_device *ndev,
250 		 struct vcap_admin *admin,
251 		 u32 addr,
252 		 u32 count);
253 	void (*update)
254 		(struct net_device *ndev,
255 		 struct vcap_admin *admin,
256 		 enum vcap_command cmd,
257 		 enum vcap_selection sel,
258 		 u32 addr);
259 	void (*move)
260 		(struct net_device *ndev,
261 		 struct vcap_admin *admin,
262 		 u32 addr,
263 		 int offset,
264 		 int count);
265 	/* informational */
266 	int (*port_info)
267 		(struct net_device *ndev,
268 		 struct vcap_admin *admin,
269 		 struct vcap_output_print *out);
270 };
271 
272 /* VCAP API Client control interface */
273 struct vcap_control {
274 	struct vcap_operations *ops;  /* client supplied operations */
275 	const struct vcap_info *vcaps; /* client supplied vcap models */
276 	const struct vcap_statistics *stats; /* client supplied vcap stats */
277 	struct list_head list; /* list of vcap instances */
278 };
279 
280 /* Set client control interface on the API */
281 int vcap_api_set_client(struct vcap_control *vctrl);
282 
283 #endif /* __VCAP_API__ */
284