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 	enum vcap_type vtype;  /* type of vcap */
170 	int vinst; /* instance number within the same type */
171 	int first_cid; /* first chain id in this vcap */
172 	int last_cid; /* last chain id in this vcap */
173 	int tgt_inst; /* hardware instance number */
174 	int lookups; /* number of lookups in this vcap type */
175 	int lookups_per_instance; /* number of lookups in this instance */
176 	int last_valid_addr; /* top of address range to be used */
177 	int first_valid_addr; /* bottom of address range to be used */
178 	int last_used_addr;  /* address of lowest added rule */
179 	bool w32be; /* vcap uses "32bit-word big-endian" encoding */
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 /* Client supplied VCAP callback operations */
206 struct vcap_operations {
207 	/* validate port keyset operation */
208 	enum vcap_keyfield_set (*validate_keyset)
209 		(struct net_device *ndev,
210 		 struct vcap_admin *admin,
211 		 struct vcap_rule *rule,
212 		 struct vcap_keyset_list *kslist,
213 		 u16 l3_proto);
214 	/* add default rule fields for the selected keyset operations */
215 	void (*add_default_fields)
216 		(struct net_device *ndev,
217 		 struct vcap_admin *admin,
218 		 struct vcap_rule *rule);
219 	/* cache operations */
220 	void (*cache_erase)
221 		(struct vcap_admin *admin);
222 	void (*cache_write)
223 		(struct net_device *ndev,
224 		 struct vcap_admin *admin,
225 		 enum vcap_selection sel,
226 		 u32 idx, u32 count);
227 	void (*cache_read)
228 		(struct net_device *ndev,
229 		 struct vcap_admin *admin,
230 		 enum vcap_selection sel,
231 		 u32 idx,
232 		 u32 count);
233 	/* block operations */
234 	void (*init)
235 		(struct net_device *ndev,
236 		 struct vcap_admin *admin,
237 		 u32 addr,
238 		 u32 count);
239 	void (*update)
240 		(struct net_device *ndev,
241 		 struct vcap_admin *admin,
242 		 enum vcap_command cmd,
243 		 enum vcap_selection sel,
244 		 u32 addr);
245 	void (*move)
246 		(struct net_device *ndev,
247 		 struct vcap_admin *admin,
248 		 u32 addr,
249 		 int offset,
250 		 int count);
251 	/* informational */
252 	int (*port_info)
253 		(struct net_device *ndev,
254 		 enum vcap_type vtype,
255 		 int (*pf)(void *out, int arg, const char *fmt, ...),
256 		 void *out,
257 		 int arg);
258 };
259 
260 /* VCAP API Client control interface */
261 struct vcap_control {
262 	u32 rule_id; /* last used rule id (unique across VCAP instances) */
263 	struct vcap_operations *ops;  /* client supplied operations */
264 	const struct vcap_info *vcaps; /* client supplied vcap models */
265 	const struct vcap_statistics *stats; /* client supplied vcap stats */
266 	struct list_head list; /* list of vcap instances */
267 };
268 
269 /* Set client control interface on the API */
270 int vcap_api_set_client(struct vcap_control *vctrl);
271 
272 #endif /* __VCAP_API__ */
273