xref: /linux/drivers/net/ethernet/microchip/vcap/vcap_api.c (revision 91ec2035134982b98fab0609a9fd8480e8217dc1)
1 // SPDX-License-Identifier: GPL-2.0+
2 /* Microchip VCAP API
3  *
4  * Copyright (c) 2022 Microchip Technology Inc. and its subsidiaries.
5  */
6 
7 #include <linux/types.h>
8 
9 #include "vcap_api_private.h"
10 
11 static int keyfield_size_table[] = {
12 	[VCAP_FIELD_BIT]  = sizeof(struct vcap_u1_key),
13 	[VCAP_FIELD_U32]  = sizeof(struct vcap_u32_key),
14 	[VCAP_FIELD_U48]  = sizeof(struct vcap_u48_key),
15 	[VCAP_FIELD_U56]  = sizeof(struct vcap_u56_key),
16 	[VCAP_FIELD_U64]  = sizeof(struct vcap_u64_key),
17 	[VCAP_FIELD_U72]  = sizeof(struct vcap_u72_key),
18 	[VCAP_FIELD_U112] = sizeof(struct vcap_u112_key),
19 	[VCAP_FIELD_U128] = sizeof(struct vcap_u128_key),
20 };
21 
22 static int actionfield_size_table[] = {
23 	[VCAP_FIELD_BIT]  = sizeof(struct vcap_u1_action),
24 	[VCAP_FIELD_U32]  = sizeof(struct vcap_u32_action),
25 	[VCAP_FIELD_U48]  = sizeof(struct vcap_u48_action),
26 	[VCAP_FIELD_U56]  = sizeof(struct vcap_u56_action),
27 	[VCAP_FIELD_U64]  = sizeof(struct vcap_u64_action),
28 	[VCAP_FIELD_U72]  = sizeof(struct vcap_u72_action),
29 	[VCAP_FIELD_U112] = sizeof(struct vcap_u112_action),
30 	[VCAP_FIELD_U128] = sizeof(struct vcap_u128_action),
31 };
32 
33 /* Moving a rule in the VCAP address space */
34 struct vcap_rule_move {
35 	int addr; /* address to move */
36 	int offset; /* change in address */
37 	int count; /* blocksize of addresses to move */
38 };
39 
40 /* Stores the filter cookie and chain id that enabled the port */
41 struct vcap_enabled_port {
42 	struct list_head list; /* for insertion in enabled ports list */
43 	struct net_device *ndev;  /* the enabled port */
44 	unsigned long cookie; /* filter that enabled the port */
45 	int src_cid; /* source chain id */
46 	int dst_cid; /* destination chain id */
47 };
48 
vcap_iter_set(struct vcap_stream_iter * itr,int sw_width,const struct vcap_typegroup * tg,u32 offset)49 void vcap_iter_set(struct vcap_stream_iter *itr, int sw_width,
50 		   const struct vcap_typegroup *tg, u32 offset)
51 {
52 	memset(itr, 0, sizeof(*itr));
53 	itr->offset = offset;
54 	itr->sw_width = sw_width;
55 	itr->regs_per_sw = DIV_ROUND_UP(sw_width, 32);
56 	itr->tg = tg;
57 }
58 
vcap_iter_skip_tg(struct vcap_stream_iter * itr)59 static void vcap_iter_skip_tg(struct vcap_stream_iter *itr)
60 {
61 	/* Compensate the field offset for preceding typegroups.
62 	 * A typegroup table ends with an all-zero terminator.
63 	 */
64 	while (itr->tg->width && itr->offset >= itr->tg->offset) {
65 		itr->offset += itr->tg->width;
66 		itr->tg++; /* next typegroup */
67 	}
68 }
69 
vcap_iter_update(struct vcap_stream_iter * itr)70 void vcap_iter_update(struct vcap_stream_iter *itr)
71 {
72 	int sw_idx, sw_bitpos;
73 
74 	/* Calculate the subword index and bitposition for current bit */
75 	sw_idx = itr->offset / itr->sw_width;
76 	sw_bitpos = itr->offset % itr->sw_width;
77 	/* Calculate the register index and bitposition for current bit */
78 	itr->reg_idx = (sw_idx * itr->regs_per_sw) + (sw_bitpos / 32);
79 	itr->reg_bitpos = sw_bitpos % 32;
80 }
81 
vcap_iter_init(struct vcap_stream_iter * itr,int sw_width,const struct vcap_typegroup * tg,u32 offset)82 void vcap_iter_init(struct vcap_stream_iter *itr, int sw_width,
83 		    const struct vcap_typegroup *tg, u32 offset)
84 {
85 	vcap_iter_set(itr, sw_width, tg, offset);
86 	vcap_iter_skip_tg(itr);
87 	vcap_iter_update(itr);
88 }
89 
vcap_iter_next(struct vcap_stream_iter * itr)90 void vcap_iter_next(struct vcap_stream_iter *itr)
91 {
92 	itr->offset++;
93 	vcap_iter_skip_tg(itr);
94 	vcap_iter_update(itr);
95 }
96 
vcap_set_bit(u32 * stream,struct vcap_stream_iter * itr,bool value)97 static void vcap_set_bit(u32 *stream, struct vcap_stream_iter *itr, bool value)
98 {
99 	u32 mask = BIT(itr->reg_bitpos);
100 	u32 *p = &stream[itr->reg_idx];
101 
102 	if (value)
103 		*p |= mask;
104 	else
105 		*p &= ~mask;
106 }
107 
vcap_encode_bit(u32 * stream,struct vcap_stream_iter * itr,bool val)108 static void vcap_encode_bit(u32 *stream, struct vcap_stream_iter *itr, bool val)
109 {
110 	/* When intersected by a type group field, stream the type group bits
111 	 * before continuing with the value bit
112 	 */
113 	while (itr->tg->width &&
114 	       itr->offset >= itr->tg->offset &&
115 	       itr->offset < itr->tg->offset + itr->tg->width) {
116 		int tg_bitpos = itr->tg->offset - itr->offset;
117 
118 		vcap_set_bit(stream, itr, (itr->tg->value >> tg_bitpos) & 0x1);
119 		itr->offset++;
120 		vcap_iter_update(itr);
121 	}
122 	vcap_set_bit(stream, itr, val);
123 }
124 
vcap_encode_field(u32 * stream,struct vcap_stream_iter * itr,int width,const u8 * value)125 static void vcap_encode_field(u32 *stream, struct vcap_stream_iter *itr,
126 			      int width, const u8 *value)
127 {
128 	int idx;
129 
130 	/* Loop over the field value bits and add the value bits one by one to
131 	 * the output stream.
132 	 */
133 	for (idx = 0; idx < width; idx++) {
134 		u8 bidx = idx & GENMASK(2, 0);
135 
136 		/* Encode one field value bit */
137 		vcap_encode_bit(stream, itr, (value[idx / 8] >> bidx) & 0x1);
138 		vcap_iter_next(itr);
139 	}
140 }
141 
vcap_encode_typegroups(u32 * stream,int sw_width,const struct vcap_typegroup * tg,bool mask)142 static void vcap_encode_typegroups(u32 *stream, int sw_width,
143 				   const struct vcap_typegroup *tg,
144 				   bool mask)
145 {
146 	struct vcap_stream_iter iter;
147 	int idx;
148 
149 	/* Mask bits must be set to zeros (inverted later when writing to the
150 	 * mask cache register), so that the mask typegroup bits consist of
151 	 * match-1 or match-0, or both
152 	 */
153 	vcap_iter_set(&iter, sw_width, tg, 0);
154 	while (iter.tg->width) {
155 		/* Set position to current typegroup bit */
156 		iter.offset = iter.tg->offset;
157 		vcap_iter_update(&iter);
158 		for (idx = 0; idx < iter.tg->width; idx++) {
159 			/* Iterate over current typegroup bits. Mask typegroup
160 			 * bits are always set
161 			 */
162 			if (mask)
163 				vcap_set_bit(stream, &iter, 0x1);
164 			else
165 				vcap_set_bit(stream, &iter,
166 					     (iter.tg->value >> idx) & 0x1);
167 			iter.offset++;
168 			vcap_iter_update(&iter);
169 		}
170 		iter.tg++; /* next typegroup */
171 	}
172 }
173 
vcap_bitarray_zero(int width,u8 * value)174 static bool vcap_bitarray_zero(int width, u8 *value)
175 {
176 	int bytes = DIV_ROUND_UP(width, BITS_PER_BYTE);
177 	u8 total = 0, bmask = 0xff;
178 	int rwidth = width;
179 	int idx;
180 
181 	for (idx = 0; idx < bytes; ++idx, rwidth -= BITS_PER_BYTE) {
182 		if (rwidth && rwidth < BITS_PER_BYTE)
183 			bmask = (1 << rwidth) - 1;
184 		total += value[idx] & bmask;
185 	}
186 	return total == 0;
187 }
188 
vcap_get_bit(u32 * stream,struct vcap_stream_iter * itr)189 static bool vcap_get_bit(u32 *stream, struct vcap_stream_iter *itr)
190 {
191 	u32 mask = BIT(itr->reg_bitpos);
192 	u32 *p = &stream[itr->reg_idx];
193 
194 	return !!(*p & mask);
195 }
196 
vcap_decode_field(u32 * stream,struct vcap_stream_iter * itr,int width,u8 * value)197 static void vcap_decode_field(u32 *stream, struct vcap_stream_iter *itr,
198 			      int width, u8 *value)
199 {
200 	int idx;
201 
202 	/* Loop over the field value bits and get the field bits and
203 	 * set them in the output value byte array
204 	 */
205 	for (idx = 0; idx < width; idx++) {
206 		u8 bidx = idx & 0x7;
207 
208 		/* Decode one field value bit */
209 		if (vcap_get_bit(stream, itr))
210 			*value |= 1 << bidx;
211 		vcap_iter_next(itr);
212 		if (bidx == 7)
213 			value++;
214 	}
215 }
216 
217 /* Verify that the type id in the stream matches the type id of the keyset */
vcap_verify_keystream_keyset(struct vcap_control * vctrl,enum vcap_type vt,u32 * keystream,u32 * mskstream,enum vcap_keyfield_set keyset)218 static bool vcap_verify_keystream_keyset(struct vcap_control *vctrl,
219 					 enum vcap_type vt,
220 					 u32 *keystream,
221 					 u32 *mskstream,
222 					 enum vcap_keyfield_set keyset)
223 {
224 	const struct vcap_info *vcap = &vctrl->vcaps[vt];
225 	const struct vcap_field *typefld;
226 	const struct vcap_typegroup *tgt;
227 	const struct vcap_field *fields;
228 	struct vcap_stream_iter iter;
229 	const struct vcap_set *info;
230 	u32 value = 0;
231 	u32 mask = 0;
232 
233 	if (vcap_keyfield_count(vctrl, vt, keyset) == 0)
234 		return false;
235 
236 	info = vcap_keyfieldset(vctrl, vt, keyset);
237 	/* Check that the keyset is valid */
238 	if (!info)
239 		return false;
240 
241 	/* a type_id of value -1 means that there is no type field */
242 	if (info->type_id == (u8)-1)
243 		return true;
244 
245 	/* Get a valid typegroup for the specific keyset */
246 	tgt = vcap_keyfield_typegroup(vctrl, vt, keyset);
247 	if (!tgt)
248 		return false;
249 
250 	fields = vcap_keyfields(vctrl, vt, keyset);
251 	if (!fields)
252 		return false;
253 
254 	typefld = &fields[VCAP_KF_TYPE];
255 	vcap_iter_init(&iter, vcap->sw_width, tgt, typefld->offset);
256 	vcap_decode_field(mskstream, &iter, typefld->width, (u8 *)&mask);
257 	/* no type info if there are no mask bits */
258 	if (vcap_bitarray_zero(typefld->width, (u8 *)&mask))
259 		return false;
260 
261 	/* Get the value of the type field in the stream and compare to the
262 	 * one define in the vcap keyset
263 	 */
264 	vcap_iter_init(&iter, vcap->sw_width, tgt, typefld->offset);
265 	vcap_decode_field(keystream, &iter, typefld->width, (u8 *)&value);
266 
267 	return (value & mask) == (info->type_id & mask);
268 }
269 
270 /* Verify that the typegroup bits have the correct values */
vcap_verify_typegroups(u32 * stream,int sw_width,const struct vcap_typegroup * tgt,bool mask,int sw_max)271 static int vcap_verify_typegroups(u32 *stream, int sw_width,
272 				  const struct vcap_typegroup *tgt, bool mask,
273 				  int sw_max)
274 {
275 	struct vcap_stream_iter iter;
276 	int sw_cnt, idx;
277 
278 	vcap_iter_set(&iter, sw_width, tgt, 0);
279 	sw_cnt = 0;
280 	while (iter.tg->width) {
281 		u32 value = 0;
282 		u32 tg_value = iter.tg->value;
283 
284 		if (mask)
285 			tg_value = (1 << iter.tg->width) - 1;
286 		/* Set position to current typegroup bit */
287 		iter.offset = iter.tg->offset;
288 		vcap_iter_update(&iter);
289 		for (idx = 0; idx < iter.tg->width; idx++) {
290 			/* Decode one typegroup bit */
291 			if (vcap_get_bit(stream, &iter))
292 				value |= 1 << idx;
293 			iter.offset++;
294 			vcap_iter_update(&iter);
295 		}
296 		if (value != tg_value)
297 			return -EINVAL;
298 		iter.tg++; /* next typegroup */
299 		sw_cnt++;
300 		/* Stop checking more typegroups */
301 		if (sw_max && sw_cnt >= sw_max)
302 			break;
303 	}
304 	return 0;
305 }
306 
307 /* Find the subword width of the key typegroup that matches the stream data */
vcap_find_keystream_typegroup_sw(struct vcap_control * vctrl,enum vcap_type vt,u32 * stream,bool mask,int sw_max)308 static int vcap_find_keystream_typegroup_sw(struct vcap_control *vctrl,
309 					    enum vcap_type vt, u32 *stream,
310 					    bool mask, int sw_max)
311 {
312 	const struct vcap_typegroup **tgt;
313 	int sw_idx, res;
314 
315 	tgt = vctrl->vcaps[vt].keyfield_set_typegroups;
316 	/* Try the longest subword match first */
317 	for (sw_idx = vctrl->vcaps[vt].sw_count; sw_idx >= 0; sw_idx--) {
318 		if (!tgt[sw_idx])
319 			continue;
320 
321 		res = vcap_verify_typegroups(stream, vctrl->vcaps[vt].sw_width,
322 					     tgt[sw_idx], mask, sw_max);
323 		if (res == 0)
324 			return sw_idx;
325 	}
326 	return -EINVAL;
327 }
328 
329 /* Verify that the typegroup information, subword count, keyset and type id
330  * are in sync and correct, return the list of matching keysets
331  */
332 int
vcap_find_keystream_keysets(struct vcap_control * vctrl,enum vcap_type vt,u32 * keystream,u32 * mskstream,bool mask,int sw_max,struct vcap_keyset_list * kslist)333 vcap_find_keystream_keysets(struct vcap_control *vctrl,
334 			    enum vcap_type vt,
335 			    u32 *keystream,
336 			    u32 *mskstream,
337 			    bool mask, int sw_max,
338 			    struct vcap_keyset_list *kslist)
339 {
340 	const struct vcap_set *keyfield_set;
341 	int sw_count, idx;
342 
343 	sw_count = vcap_find_keystream_typegroup_sw(vctrl, vt, keystream, mask,
344 						    sw_max);
345 	if (sw_count < 0)
346 		return sw_count;
347 
348 	keyfield_set = vctrl->vcaps[vt].keyfield_set;
349 	for (idx = 0; idx < vctrl->vcaps[vt].keyfield_set_size; ++idx) {
350 		if (keyfield_set[idx].sw_per_item != sw_count)
351 			continue;
352 
353 		if (vcap_verify_keystream_keyset(vctrl, vt, keystream,
354 						 mskstream, idx))
355 			vcap_keyset_list_add(kslist, idx);
356 	}
357 	if (kslist->cnt > 0)
358 		return 0;
359 	return -EINVAL;
360 }
361 EXPORT_SYMBOL_GPL(vcap_find_keystream_keysets);
362 
363 /* Read key data from a VCAP address and discover if there are any rule keysets
364  * here
365  */
vcap_addr_keysets(struct vcap_control * vctrl,struct net_device * ndev,struct vcap_admin * admin,int addr,struct vcap_keyset_list * kslist)366 int vcap_addr_keysets(struct vcap_control *vctrl,
367 		      struct net_device *ndev,
368 		      struct vcap_admin *admin,
369 		      int addr,
370 		      struct vcap_keyset_list *kslist)
371 {
372 	enum vcap_type vt = admin->vtype;
373 	int keyset_sw_regs, idx;
374 	u32 key = 0, mask = 0;
375 
376 	/* Read the cache at the specified address */
377 	keyset_sw_regs = DIV_ROUND_UP(vctrl->vcaps[vt].sw_width, 32);
378 	vctrl->ops->update(ndev, admin, VCAP_CMD_READ, VCAP_SEL_ALL, addr);
379 	vctrl->ops->cache_read(ndev, admin, VCAP_SEL_ENTRY, 0,
380 			       keyset_sw_regs);
381 	/* Skip uninitialized key/mask entries */
382 	for (idx = 0; idx < keyset_sw_regs; ++idx) {
383 		key |= ~admin->cache.keystream[idx];
384 		mask |= admin->cache.maskstream[idx];
385 	}
386 	if (key == 0 && mask == 0)
387 		return -EINVAL;
388 	/* Decode and locate the keysets */
389 	return vcap_find_keystream_keysets(vctrl, vt, admin->cache.keystream,
390 					   admin->cache.maskstream, false, 0,
391 					   kslist);
392 }
393 EXPORT_SYMBOL_GPL(vcap_addr_keysets);
394 
395 /* Return the list of keyfields for the keyset */
vcap_keyfields(struct vcap_control * vctrl,enum vcap_type vt,enum vcap_keyfield_set keyset)396 const struct vcap_field *vcap_keyfields(struct vcap_control *vctrl,
397 					enum vcap_type vt,
398 					enum vcap_keyfield_set keyset)
399 {
400 	/* Check that the keyset exists in the vcap keyset list */
401 	if (keyset >= vctrl->vcaps[vt].keyfield_set_size)
402 		return NULL;
403 	return vctrl->vcaps[vt].keyfield_set_map[keyset];
404 }
405 
406 /* Return the keyset information for the keyset */
vcap_keyfieldset(struct vcap_control * vctrl,enum vcap_type vt,enum vcap_keyfield_set keyset)407 const struct vcap_set *vcap_keyfieldset(struct vcap_control *vctrl,
408 					enum vcap_type vt,
409 					enum vcap_keyfield_set keyset)
410 {
411 	const struct vcap_set *kset;
412 
413 	/* Check that the keyset exists in the vcap keyset list */
414 	if (keyset >= vctrl->vcaps[vt].keyfield_set_size)
415 		return NULL;
416 	kset = &vctrl->vcaps[vt].keyfield_set[keyset];
417 	if (kset->sw_per_item == 0 || kset->sw_per_item > vctrl->vcaps[vt].sw_count)
418 		return NULL;
419 	return kset;
420 }
421 EXPORT_SYMBOL_GPL(vcap_keyfieldset);
422 
423 /* Return the typegroup table for the matching keyset (using subword size) */
424 const struct vcap_typegroup *
vcap_keyfield_typegroup(struct vcap_control * vctrl,enum vcap_type vt,enum vcap_keyfield_set keyset)425 vcap_keyfield_typegroup(struct vcap_control *vctrl,
426 			enum vcap_type vt, enum vcap_keyfield_set keyset)
427 {
428 	const struct vcap_set *kset = vcap_keyfieldset(vctrl, vt, keyset);
429 
430 	/* Check that the keyset is valid */
431 	if (!kset)
432 		return NULL;
433 	return vctrl->vcaps[vt].keyfield_set_typegroups[kset->sw_per_item];
434 }
435 
436 /* Return the number of keyfields in the keyset */
vcap_keyfield_count(struct vcap_control * vctrl,enum vcap_type vt,enum vcap_keyfield_set keyset)437 int vcap_keyfield_count(struct vcap_control *vctrl,
438 			enum vcap_type vt, enum vcap_keyfield_set keyset)
439 {
440 	/* Check that the keyset exists in the vcap keyset list */
441 	if (keyset >= vctrl->vcaps[vt].keyfield_set_size)
442 		return 0;
443 	return vctrl->vcaps[vt].keyfield_set_map_size[keyset];
444 }
445 
vcap_encode_keyfield(struct vcap_rule_internal * ri,const struct vcap_client_keyfield * kf,const struct vcap_field * rf,const struct vcap_typegroup * tgt)446 static void vcap_encode_keyfield(struct vcap_rule_internal *ri,
447 				 const struct vcap_client_keyfield *kf,
448 				 const struct vcap_field *rf,
449 				 const struct vcap_typegroup *tgt)
450 {
451 	int sw_width = ri->vctrl->vcaps[ri->admin->vtype].sw_width;
452 	struct vcap_cache_data *cache = &ri->admin->cache;
453 	struct vcap_stream_iter iter;
454 	const u8 *value, *mask;
455 
456 	/* Encode the fields for the key and the mask in their respective
457 	 * streams, respecting the subword width.
458 	 */
459 	switch (kf->ctrl.type) {
460 	case VCAP_FIELD_BIT:
461 		value = &kf->data.u1.value;
462 		mask = &kf->data.u1.mask;
463 		break;
464 	case VCAP_FIELD_U32:
465 		value = (const u8 *)&kf->data.u32.value;
466 		mask = (const u8 *)&kf->data.u32.mask;
467 		break;
468 	case VCAP_FIELD_U48:
469 		value = kf->data.u48.value;
470 		mask = kf->data.u48.mask;
471 		break;
472 	case VCAP_FIELD_U56:
473 		value = kf->data.u56.value;
474 		mask = kf->data.u56.mask;
475 		break;
476 	case VCAP_FIELD_U64:
477 		value = kf->data.u64.value;
478 		mask = kf->data.u64.mask;
479 		break;
480 	case VCAP_FIELD_U72:
481 		value = kf->data.u72.value;
482 		mask = kf->data.u72.mask;
483 		break;
484 	case VCAP_FIELD_U112:
485 		value = kf->data.u112.value;
486 		mask = kf->data.u112.mask;
487 		break;
488 	case VCAP_FIELD_U128:
489 		value = kf->data.u128.value;
490 		mask = kf->data.u128.mask;
491 		break;
492 	}
493 	vcap_iter_init(&iter, sw_width, tgt, rf->offset);
494 	vcap_encode_field(cache->keystream, &iter, rf->width, value);
495 	vcap_iter_init(&iter, sw_width, tgt, rf->offset);
496 	vcap_encode_field(cache->maskstream, &iter, rf->width, mask);
497 }
498 
vcap_encode_keyfield_typegroups(struct vcap_control * vctrl,struct vcap_rule_internal * ri,const struct vcap_typegroup * tgt)499 static void vcap_encode_keyfield_typegroups(struct vcap_control *vctrl,
500 					    struct vcap_rule_internal *ri,
501 					    const struct vcap_typegroup *tgt)
502 {
503 	int sw_width = vctrl->vcaps[ri->admin->vtype].sw_width;
504 	struct vcap_cache_data *cache = &ri->admin->cache;
505 
506 	/* Encode the typegroup bits for the key and the mask in their streams,
507 	 * respecting the subword width.
508 	 */
509 	vcap_encode_typegroups(cache->keystream, sw_width, tgt, false);
510 	vcap_encode_typegroups(cache->maskstream, sw_width, tgt, true);
511 }
512 
513 /* Copy data from src to dst but reverse the data in chunks of 32bits.
514  * For example if src is 00:11:22:33:44:55 where 55 is LSB the dst will
515  * have the value 22:33:44:55:00:11.
516  */
vcap_copy_to_w32be(u8 * dst,const u8 * src,int size)517 static void vcap_copy_to_w32be(u8 *dst, const u8 *src, int size)
518 {
519 	for (int idx = 0; idx < size; ++idx) {
520 		int first_byte_index = 0;
521 		int nidx;
522 
523 		first_byte_index = size - (((idx >> 2) + 1) << 2);
524 		if (first_byte_index < 0)
525 			first_byte_index = 0;
526 		nidx = idx + first_byte_index - (idx & ~0x3);
527 		dst[nidx] = src[idx];
528 	}
529 }
530 
531 static void
vcap_copy_from_client_keyfield(struct vcap_rule * rule,struct vcap_client_keyfield * dst,const struct vcap_client_keyfield * src)532 vcap_copy_from_client_keyfield(struct vcap_rule *rule,
533 			       struct vcap_client_keyfield *dst,
534 			       const struct vcap_client_keyfield *src)
535 {
536 	struct vcap_rule_internal *ri = to_intrule(rule);
537 	const struct vcap_client_keyfield_data *sdata;
538 	struct vcap_client_keyfield_data *ddata;
539 	int size;
540 
541 	dst->ctrl.type = src->ctrl.type;
542 	dst->ctrl.key = src->ctrl.key;
543 	INIT_LIST_HEAD(&dst->ctrl.list);
544 	sdata = &src->data;
545 	ddata = &dst->data;
546 
547 	if (!ri->admin->w32be) {
548 		memcpy(ddata, sdata, sizeof(dst->data));
549 		return;
550 	}
551 
552 	size = keyfield_size_table[dst->ctrl.type] / 2;
553 
554 	switch (dst->ctrl.type) {
555 	case VCAP_FIELD_BIT:
556 	case VCAP_FIELD_U32:
557 		memcpy(ddata, sdata, sizeof(dst->data));
558 		break;
559 	case VCAP_FIELD_U48:
560 		vcap_copy_to_w32be(ddata->u48.value, src->data.u48.value, size);
561 		vcap_copy_to_w32be(ddata->u48.mask,  src->data.u48.mask, size);
562 		break;
563 	case VCAP_FIELD_U56:
564 		vcap_copy_to_w32be(ddata->u56.value, sdata->u56.value, size);
565 		vcap_copy_to_w32be(ddata->u56.mask,  sdata->u56.mask, size);
566 		break;
567 	case VCAP_FIELD_U64:
568 		vcap_copy_to_w32be(ddata->u64.value, sdata->u64.value, size);
569 		vcap_copy_to_w32be(ddata->u64.mask,  sdata->u64.mask, size);
570 		break;
571 	case VCAP_FIELD_U72:
572 		vcap_copy_to_w32be(ddata->u72.value, sdata->u72.value, size);
573 		vcap_copy_to_w32be(ddata->u72.mask,  sdata->u72.mask, size);
574 		break;
575 	case VCAP_FIELD_U112:
576 		vcap_copy_to_w32be(ddata->u112.value, sdata->u112.value, size);
577 		vcap_copy_to_w32be(ddata->u112.mask,  sdata->u112.mask, size);
578 		break;
579 	case VCAP_FIELD_U128:
580 		vcap_copy_to_w32be(ddata->u128.value, sdata->u128.value, size);
581 		vcap_copy_to_w32be(ddata->u128.mask,  sdata->u128.mask, size);
582 		break;
583 	}
584 }
585 
586 static void
vcap_copy_from_client_actionfield(struct vcap_rule * rule,struct vcap_client_actionfield * dst,const struct vcap_client_actionfield * src)587 vcap_copy_from_client_actionfield(struct vcap_rule *rule,
588 				  struct vcap_client_actionfield *dst,
589 				  const struct vcap_client_actionfield *src)
590 {
591 	struct vcap_rule_internal *ri = to_intrule(rule);
592 	const struct vcap_client_actionfield_data *sdata;
593 	struct vcap_client_actionfield_data *ddata;
594 	int size;
595 
596 	dst->ctrl.type = src->ctrl.type;
597 	dst->ctrl.action = src->ctrl.action;
598 	INIT_LIST_HEAD(&dst->ctrl.list);
599 	sdata = &src->data;
600 	ddata = &dst->data;
601 
602 	if (!ri->admin->w32be) {
603 		memcpy(ddata, sdata, sizeof(dst->data));
604 		return;
605 	}
606 
607 	size = actionfield_size_table[dst->ctrl.type];
608 
609 	switch (dst->ctrl.type) {
610 	case VCAP_FIELD_BIT:
611 	case VCAP_FIELD_U32:
612 		memcpy(ddata, sdata, sizeof(dst->data));
613 		break;
614 	case VCAP_FIELD_U48:
615 		vcap_copy_to_w32be(ddata->u48.value, sdata->u48.value, size);
616 		break;
617 	case VCAP_FIELD_U56:
618 		vcap_copy_to_w32be(ddata->u56.value, sdata->u56.value, size);
619 		break;
620 	case VCAP_FIELD_U64:
621 		vcap_copy_to_w32be(ddata->u64.value, sdata->u64.value, size);
622 		break;
623 	case VCAP_FIELD_U72:
624 		vcap_copy_to_w32be(ddata->u72.value, sdata->u72.value, size);
625 		break;
626 	case VCAP_FIELD_U112:
627 		vcap_copy_to_w32be(ddata->u112.value, sdata->u112.value, size);
628 		break;
629 	case VCAP_FIELD_U128:
630 		vcap_copy_to_w32be(ddata->u128.value, sdata->u128.value, size);
631 		break;
632 	}
633 }
634 
vcap_encode_rule_keyset(struct vcap_rule_internal * ri)635 static int vcap_encode_rule_keyset(struct vcap_rule_internal *ri)
636 {
637 	const struct vcap_client_keyfield *ckf;
638 	const struct vcap_typegroup *tg_table;
639 	struct vcap_client_keyfield tempkf;
640 	const struct vcap_field *kf_table;
641 	int keyset_size;
642 
643 	/* Get a valid set of fields for the specific keyset */
644 	kf_table = vcap_keyfields(ri->vctrl, ri->admin->vtype, ri->data.keyset);
645 	if (!kf_table) {
646 		pr_err("%s:%d: no fields available for this keyset: %d\n",
647 		       __func__, __LINE__, ri->data.keyset);
648 		return -EINVAL;
649 	}
650 	/* Get a valid typegroup for the specific keyset */
651 	tg_table = vcap_keyfield_typegroup(ri->vctrl, ri->admin->vtype,
652 					   ri->data.keyset);
653 	if (!tg_table) {
654 		pr_err("%s:%d: no typegroups available for this keyset: %d\n",
655 		       __func__, __LINE__, ri->data.keyset);
656 		return -EINVAL;
657 	}
658 	/* Get a valid size for the specific keyset */
659 	keyset_size = vcap_keyfield_count(ri->vctrl, ri->admin->vtype,
660 					  ri->data.keyset);
661 	if (keyset_size == 0) {
662 		pr_err("%s:%d: zero field count for this keyset: %d\n",
663 		       __func__, __LINE__, ri->data.keyset);
664 		return -EINVAL;
665 	}
666 	/* Iterate over the keyfields (key, mask) in the rule
667 	 * and encode these bits
668 	 */
669 	if (list_empty(&ri->data.keyfields)) {
670 		pr_err("%s:%d: no keyfields in the rule\n", __func__, __LINE__);
671 		return -EINVAL;
672 	}
673 	list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list) {
674 		/* Check that the client entry exists in the keyset */
675 		if (ckf->ctrl.key >= keyset_size) {
676 			pr_err("%s:%d: key %d is not in vcap\n",
677 			       __func__, __LINE__, ckf->ctrl.key);
678 			return -EINVAL;
679 		}
680 		vcap_copy_from_client_keyfield(&ri->data, &tempkf, ckf);
681 		vcap_encode_keyfield(ri, &tempkf, &kf_table[ckf->ctrl.key],
682 				     tg_table);
683 	}
684 	/* Add typegroup bits to the key/mask bitstreams */
685 	vcap_encode_keyfield_typegroups(ri->vctrl, ri, tg_table);
686 	return 0;
687 }
688 
689 /* Return the list of actionfields for the actionset */
690 const struct vcap_field *
vcap_actionfields(struct vcap_control * vctrl,enum vcap_type vt,enum vcap_actionfield_set actionset)691 vcap_actionfields(struct vcap_control *vctrl,
692 		  enum vcap_type vt, enum vcap_actionfield_set actionset)
693 {
694 	/* Check that the actionset exists in the vcap actionset list */
695 	if (actionset >= vctrl->vcaps[vt].actionfield_set_size)
696 		return NULL;
697 	return vctrl->vcaps[vt].actionfield_set_map[actionset];
698 }
699 
700 const struct vcap_set *
vcap_actionfieldset(struct vcap_control * vctrl,enum vcap_type vt,enum vcap_actionfield_set actionset)701 vcap_actionfieldset(struct vcap_control *vctrl,
702 		    enum vcap_type vt, enum vcap_actionfield_set actionset)
703 {
704 	const struct vcap_set *aset;
705 
706 	/* Check that the actionset exists in the vcap actionset list */
707 	if (actionset >= vctrl->vcaps[vt].actionfield_set_size)
708 		return NULL;
709 	aset = &vctrl->vcaps[vt].actionfield_set[actionset];
710 	if (aset->sw_per_item == 0 || aset->sw_per_item > vctrl->vcaps[vt].sw_count)
711 		return NULL;
712 	return aset;
713 }
714 
715 /* Return the typegroup table for the matching actionset (using subword size) */
716 const struct vcap_typegroup *
vcap_actionfield_typegroup(struct vcap_control * vctrl,enum vcap_type vt,enum vcap_actionfield_set actionset)717 vcap_actionfield_typegroup(struct vcap_control *vctrl,
718 			   enum vcap_type vt, enum vcap_actionfield_set actionset)
719 {
720 	const struct vcap_set *aset = vcap_actionfieldset(vctrl, vt, actionset);
721 
722 	/* Check that the actionset is valid */
723 	if (!aset)
724 		return NULL;
725 	return vctrl->vcaps[vt].actionfield_set_typegroups[aset->sw_per_item];
726 }
727 
728 /* Return the number of actionfields in the actionset */
vcap_actionfield_count(struct vcap_control * vctrl,enum vcap_type vt,enum vcap_actionfield_set actionset)729 int vcap_actionfield_count(struct vcap_control *vctrl,
730 			   enum vcap_type vt,
731 			   enum vcap_actionfield_set actionset)
732 {
733 	/* Check that the actionset exists in the vcap actionset list */
734 	if (actionset >= vctrl->vcaps[vt].actionfield_set_size)
735 		return 0;
736 	return vctrl->vcaps[vt].actionfield_set_map_size[actionset];
737 }
738 
vcap_encode_actionfield(struct vcap_rule_internal * ri,const struct vcap_client_actionfield * af,const struct vcap_field * rf,const struct vcap_typegroup * tgt)739 static void vcap_encode_actionfield(struct vcap_rule_internal *ri,
740 				    const struct vcap_client_actionfield *af,
741 				    const struct vcap_field *rf,
742 				    const struct vcap_typegroup *tgt)
743 {
744 	int act_width = ri->vctrl->vcaps[ri->admin->vtype].act_width;
745 
746 	struct vcap_cache_data *cache = &ri->admin->cache;
747 	struct vcap_stream_iter iter;
748 	const u8 *value;
749 
750 	/* Encode the action field in the stream, respecting the subword width */
751 	switch (af->ctrl.type) {
752 	case VCAP_FIELD_BIT:
753 		value = &af->data.u1.value;
754 		break;
755 	case VCAP_FIELD_U32:
756 		value = (const u8 *)&af->data.u32.value;
757 		break;
758 	case VCAP_FIELD_U48:
759 		value = af->data.u48.value;
760 		break;
761 	case VCAP_FIELD_U56:
762 		value = af->data.u56.value;
763 		break;
764 	case VCAP_FIELD_U64:
765 		value = af->data.u64.value;
766 		break;
767 	case VCAP_FIELD_U72:
768 		value = af->data.u72.value;
769 		break;
770 	case VCAP_FIELD_U112:
771 		value = af->data.u112.value;
772 		break;
773 	case VCAP_FIELD_U128:
774 		value = af->data.u128.value;
775 		break;
776 	}
777 	vcap_iter_init(&iter, act_width, tgt, rf->offset);
778 	vcap_encode_field(cache->actionstream, &iter, rf->width, value);
779 }
780 
vcap_encode_actionfield_typegroups(struct vcap_rule_internal * ri,const struct vcap_typegroup * tgt)781 static void vcap_encode_actionfield_typegroups(struct vcap_rule_internal *ri,
782 					       const struct vcap_typegroup *tgt)
783 {
784 	int sw_width = ri->vctrl->vcaps[ri->admin->vtype].act_width;
785 	struct vcap_cache_data *cache = &ri->admin->cache;
786 
787 	/* Encode the typegroup bits for the actionstream respecting the subword
788 	 * width.
789 	 */
790 	vcap_encode_typegroups(cache->actionstream, sw_width, tgt, false);
791 }
792 
vcap_encode_rule_actionset(struct vcap_rule_internal * ri)793 static int vcap_encode_rule_actionset(struct vcap_rule_internal *ri)
794 {
795 	const struct vcap_client_actionfield *caf;
796 	const struct vcap_typegroup *tg_table;
797 	struct vcap_client_actionfield tempaf;
798 	const struct vcap_field *af_table;
799 	int actionset_size;
800 
801 	/* Get a valid set of actionset fields for the specific actionset */
802 	af_table = vcap_actionfields(ri->vctrl, ri->admin->vtype,
803 				     ri->data.actionset);
804 	if (!af_table) {
805 		pr_err("%s:%d: no fields available for this actionset: %d\n",
806 		       __func__, __LINE__, ri->data.actionset);
807 		return -EINVAL;
808 	}
809 	/* Get a valid typegroup for the specific actionset */
810 	tg_table = vcap_actionfield_typegroup(ri->vctrl, ri->admin->vtype,
811 					      ri->data.actionset);
812 	if (!tg_table) {
813 		pr_err("%s:%d: no typegroups available for this actionset: %d\n",
814 		       __func__, __LINE__, ri->data.actionset);
815 		return -EINVAL;
816 	}
817 	/* Get a valid actionset size for the specific actionset */
818 	actionset_size = vcap_actionfield_count(ri->vctrl, ri->admin->vtype,
819 						ri->data.actionset);
820 	if (actionset_size == 0) {
821 		pr_err("%s:%d: zero field count for this actionset: %d\n",
822 		       __func__, __LINE__, ri->data.actionset);
823 		return -EINVAL;
824 	}
825 	/* Iterate over the actionfields in the rule
826 	 * and encode these bits
827 	 */
828 	if (list_empty(&ri->data.actionfields))
829 		pr_warn("%s:%d: no actionfields in the rule\n",
830 			__func__, __LINE__);
831 	list_for_each_entry(caf, &ri->data.actionfields, ctrl.list) {
832 		/* Check that the client action exists in the actionset */
833 		if (caf->ctrl.action >= actionset_size) {
834 			pr_err("%s:%d: action %d is not in vcap\n",
835 			       __func__, __LINE__, caf->ctrl.action);
836 			return -EINVAL;
837 		}
838 		vcap_copy_from_client_actionfield(&ri->data, &tempaf, caf);
839 		vcap_encode_actionfield(ri, &tempaf,
840 					&af_table[caf->ctrl.action], tg_table);
841 	}
842 	/* Add typegroup bits to the entry bitstreams */
843 	vcap_encode_actionfield_typegroups(ri, tg_table);
844 	return 0;
845 }
846 
vcap_encode_rule(struct vcap_rule_internal * ri)847 static int vcap_encode_rule(struct vcap_rule_internal *ri)
848 {
849 	int err;
850 
851 	err = vcap_encode_rule_keyset(ri);
852 	if (err)
853 		return err;
854 	err = vcap_encode_rule_actionset(ri);
855 	if (err)
856 		return err;
857 	return 0;
858 }
859 
vcap_api_check(struct vcap_control * ctrl)860 int vcap_api_check(struct vcap_control *ctrl)
861 {
862 	if (!ctrl) {
863 		pr_err("%s:%d: vcap control is missing\n", __func__, __LINE__);
864 		return -EINVAL;
865 	}
866 	if (!ctrl->ops || !ctrl->ops->validate_keyset ||
867 	    !ctrl->ops->add_default_fields || !ctrl->ops->cache_erase ||
868 	    !ctrl->ops->cache_write || !ctrl->ops->cache_read ||
869 	    !ctrl->ops->init || !ctrl->ops->update || !ctrl->ops->move ||
870 	    !ctrl->ops->port_info) {
871 		pr_err("%s:%d: client operations are missing\n",
872 		       __func__, __LINE__);
873 		return -ENOENT;
874 	}
875 	return 0;
876 }
877 
vcap_erase_cache(struct vcap_rule_internal * ri)878 void vcap_erase_cache(struct vcap_rule_internal *ri)
879 {
880 	ri->vctrl->ops->cache_erase(ri->admin);
881 }
882 
883 /* Update the keyset for the rule */
vcap_set_rule_set_keyset(struct vcap_rule * rule,enum vcap_keyfield_set keyset)884 int vcap_set_rule_set_keyset(struct vcap_rule *rule,
885 			     enum vcap_keyfield_set keyset)
886 {
887 	struct vcap_rule_internal *ri = to_intrule(rule);
888 	const struct vcap_set *kset;
889 	int sw_width;
890 
891 	kset = vcap_keyfieldset(ri->vctrl, ri->admin->vtype, keyset);
892 	/* Check that the keyset is valid */
893 	if (!kset)
894 		return -EINVAL;
895 	ri->keyset_sw = kset->sw_per_item;
896 	sw_width = ri->vctrl->vcaps[ri->admin->vtype].sw_width;
897 	ri->keyset_sw_regs = DIV_ROUND_UP(sw_width, 32);
898 	ri->data.keyset = keyset;
899 	return 0;
900 }
901 EXPORT_SYMBOL_GPL(vcap_set_rule_set_keyset);
902 
903 /* Update the actionset for the rule */
vcap_set_rule_set_actionset(struct vcap_rule * rule,enum vcap_actionfield_set actionset)904 int vcap_set_rule_set_actionset(struct vcap_rule *rule,
905 				enum vcap_actionfield_set actionset)
906 {
907 	struct vcap_rule_internal *ri = to_intrule(rule);
908 	const struct vcap_set *aset;
909 	int act_width;
910 
911 	aset = vcap_actionfieldset(ri->vctrl, ri->admin->vtype, actionset);
912 	/* Check that the actionset is valid */
913 	if (!aset)
914 		return -EINVAL;
915 	ri->actionset_sw = aset->sw_per_item;
916 	act_width = ri->vctrl->vcaps[ri->admin->vtype].act_width;
917 	ri->actionset_sw_regs = DIV_ROUND_UP(act_width, 32);
918 	ri->data.actionset = actionset;
919 	return 0;
920 }
921 EXPORT_SYMBOL_GPL(vcap_set_rule_set_actionset);
922 
923 /* Check if a rule with this id exists */
vcap_rule_exists(struct vcap_control * vctrl,u32 id)924 static bool vcap_rule_exists(struct vcap_control *vctrl, u32 id)
925 {
926 	struct vcap_rule_internal *ri;
927 	struct vcap_admin *admin;
928 
929 	/* Look for the rule id in all vcaps */
930 	list_for_each_entry(admin, &vctrl->list, list)
931 		list_for_each_entry(ri, &admin->rules, list)
932 			if (ri->data.id == id)
933 				return true;
934 	return false;
935 }
936 
vcap_lock(struct vcap_admin * admin)937 void vcap_lock(struct vcap_admin *admin)
938 {
939 	mutex_lock(&admin->vctrl->lock);
940 }
941 
vcap_unlock(struct vcap_admin * admin)942 void vcap_unlock(struct vcap_admin *admin)
943 {
944 	mutex_unlock(&admin->vctrl->lock);
945 }
946 
947 /* Find a rule with a provided rule id return a locked vcap */
948 static struct vcap_rule_internal *
vcap_get_locked_rule(struct vcap_control * vctrl,u32 id)949 vcap_get_locked_rule(struct vcap_control *vctrl, u32 id)
950 {
951 	struct vcap_rule_internal *ri;
952 	struct vcap_admin *admin;
953 
954 	/* Look for the rule id in all vcaps */
955 	list_for_each_entry(admin, &vctrl->list, list) {
956 		vcap_lock(admin);
957 		list_for_each_entry(ri, &admin->rules, list)
958 			if (ri->data.id == id)
959 				return ri;
960 		vcap_unlock(admin);
961 	}
962 	return NULL;
963 }
964 
965 /* Find a rule id with a provided cookie */
vcap_lookup_rule_by_cookie(struct vcap_control * vctrl,u64 cookie)966 int vcap_lookup_rule_by_cookie(struct vcap_control *vctrl, u64 cookie)
967 {
968 	struct vcap_rule_internal *ri;
969 	struct vcap_admin *admin;
970 	int id = 0;
971 
972 	/* Look for the rule id in all vcaps */
973 	list_for_each_entry(admin, &vctrl->list, list) {
974 		vcap_lock(admin);
975 		list_for_each_entry(ri, &admin->rules, list) {
976 			if (ri->data.cookie == cookie) {
977 				id = ri->data.id;
978 				break;
979 			}
980 		}
981 		vcap_unlock(admin);
982 		if (id)
983 			return id;
984 	}
985 	return -ENOENT;
986 }
987 EXPORT_SYMBOL_GPL(vcap_lookup_rule_by_cookie);
988 
989 /* Get number of rules in a vcap instance lookup chain id range */
vcap_admin_rule_count(struct vcap_admin * admin,int cid)990 int vcap_admin_rule_count(struct vcap_admin *admin, int cid)
991 {
992 	int max_cid = roundup(cid + 1, VCAP_CID_LOOKUP_SIZE);
993 	int min_cid = rounddown(cid, VCAP_CID_LOOKUP_SIZE);
994 	struct vcap_rule_internal *elem;
995 	int count = 0;
996 
997 	list_for_each_entry(elem, &admin->rules, list) {
998 		vcap_lock(admin);
999 		if (elem->data.vcap_chain_id >= min_cid &&
1000 		    elem->data.vcap_chain_id < max_cid)
1001 			++count;
1002 		vcap_unlock(admin);
1003 	}
1004 	return count;
1005 }
1006 EXPORT_SYMBOL_GPL(vcap_admin_rule_count);
1007 
1008 /* Make a copy of the rule, shallow or full */
vcap_dup_rule(struct vcap_rule_internal * ri,bool full)1009 static struct vcap_rule_internal *vcap_dup_rule(struct vcap_rule_internal *ri,
1010 						bool full)
1011 {
1012 	struct vcap_client_actionfield *caf, *newcaf;
1013 	struct vcap_client_keyfield *ckf, *newckf;
1014 	struct vcap_rule_internal *duprule;
1015 
1016 	/* Allocate the client part */
1017 	duprule = kzalloc_obj(*duprule);
1018 	if (!duprule)
1019 		return ERR_PTR(-ENOMEM);
1020 	*duprule = *ri;
1021 	/* Not inserted in the VCAP */
1022 	INIT_LIST_HEAD(&duprule->list);
1023 	/* No elements in these lists */
1024 	INIT_LIST_HEAD(&duprule->data.keyfields);
1025 	INIT_LIST_HEAD(&duprule->data.actionfields);
1026 
1027 	/* A full rule copy includes keys and actions */
1028 	if (!full)
1029 		return duprule;
1030 
1031 	list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list) {
1032 		newckf = kmemdup(ckf, sizeof(*newckf), GFP_KERNEL);
1033 		if (!newckf)
1034 			goto err;
1035 		list_add_tail(&newckf->ctrl.list, &duprule->data.keyfields);
1036 	}
1037 
1038 	list_for_each_entry(caf, &ri->data.actionfields, ctrl.list) {
1039 		newcaf = kmemdup(caf, sizeof(*newcaf), GFP_KERNEL);
1040 		if (!newcaf)
1041 			goto err;
1042 		list_add_tail(&newcaf->ctrl.list, &duprule->data.actionfields);
1043 	}
1044 
1045 	return duprule;
1046 
1047 err:
1048 	list_for_each_entry_safe(ckf, newckf, &duprule->data.keyfields, ctrl.list) {
1049 		list_del(&ckf->ctrl.list);
1050 		kfree(ckf);
1051 	}
1052 
1053 	list_for_each_entry_safe(caf, newcaf, &duprule->data.actionfields, ctrl.list) {
1054 		list_del(&caf->ctrl.list);
1055 		kfree(caf);
1056 	}
1057 
1058 	kfree(duprule);
1059 	return ERR_PTR(-ENOMEM);
1060 }
1061 
vcap_apply_width(u8 * dst,int width,int bytes)1062 static void vcap_apply_width(u8 *dst, int width, int bytes)
1063 {
1064 	u8 bmask;
1065 	int idx;
1066 
1067 	for (idx = 0; idx < bytes; idx++) {
1068 		if (width > 0)
1069 			if (width < 8)
1070 				bmask = (1 << width) - 1;
1071 			else
1072 				bmask = ~0;
1073 		else
1074 			bmask = 0;
1075 		dst[idx] &= bmask;
1076 		width -= 8;
1077 	}
1078 }
1079 
vcap_copy_from_w32be(u8 * dst,u8 * src,int size,int width)1080 static void vcap_copy_from_w32be(u8 *dst, u8 *src, int size, int width)
1081 {
1082 	int idx, ridx, wstart, nidx;
1083 	int tail_bytes = (((size + 4) >> 2) << 2) - size;
1084 
1085 	for (idx = 0, ridx = size - 1; idx < size; ++idx, --ridx) {
1086 		wstart = (idx >> 2) << 2;
1087 		nidx = wstart + 3 - (idx & 0x3);
1088 		if (nidx >= size)
1089 			nidx -= tail_bytes;
1090 		dst[nidx] = src[ridx];
1091 	}
1092 
1093 	vcap_apply_width(dst, width, size);
1094 }
1095 
vcap_copy_action_bit_field(struct vcap_u1_action * field,u8 * value)1096 static void vcap_copy_action_bit_field(struct vcap_u1_action *field, u8 *value)
1097 {
1098 	field->value = (*value) & 0x1;
1099 }
1100 
vcap_copy_limited_actionfield(u8 * dstvalue,u8 * srcvalue,int width,int bytes)1101 static void vcap_copy_limited_actionfield(u8 *dstvalue, u8 *srcvalue,
1102 					  int width, int bytes)
1103 {
1104 	memcpy(dstvalue, srcvalue, bytes);
1105 	vcap_apply_width(dstvalue, width, bytes);
1106 }
1107 
vcap_copy_to_client_actionfield(struct vcap_rule_internal * ri,struct vcap_client_actionfield * field,u8 * value,u16 width)1108 static void vcap_copy_to_client_actionfield(struct vcap_rule_internal *ri,
1109 					    struct vcap_client_actionfield *field,
1110 					    u8 *value, u16 width)
1111 {
1112 	int field_size = actionfield_size_table[field->ctrl.type];
1113 
1114 	if (ri->admin->w32be) {
1115 		switch (field->ctrl.type) {
1116 		case VCAP_FIELD_BIT:
1117 			vcap_copy_action_bit_field(&field->data.u1, value);
1118 			break;
1119 		case VCAP_FIELD_U32:
1120 			vcap_copy_limited_actionfield((u8 *)&field->data.u32.value,
1121 						      value,
1122 						      width, field_size);
1123 			break;
1124 		case VCAP_FIELD_U48:
1125 			vcap_copy_from_w32be(field->data.u48.value, value,
1126 					     field_size, width);
1127 			break;
1128 		case VCAP_FIELD_U56:
1129 			vcap_copy_from_w32be(field->data.u56.value, value,
1130 					     field_size, width);
1131 			break;
1132 		case VCAP_FIELD_U64:
1133 			vcap_copy_from_w32be(field->data.u64.value, value,
1134 					     field_size, width);
1135 			break;
1136 		case VCAP_FIELD_U72:
1137 			vcap_copy_from_w32be(field->data.u72.value, value,
1138 					     field_size, width);
1139 			break;
1140 		case VCAP_FIELD_U112:
1141 			vcap_copy_from_w32be(field->data.u112.value, value,
1142 					     field_size, width);
1143 			break;
1144 		case VCAP_FIELD_U128:
1145 			vcap_copy_from_w32be(field->data.u128.value, value,
1146 					     field_size, width);
1147 			break;
1148 		}
1149 	} else {
1150 		switch (field->ctrl.type) {
1151 		case VCAP_FIELD_BIT:
1152 			vcap_copy_action_bit_field(&field->data.u1, value);
1153 			break;
1154 		case VCAP_FIELD_U32:
1155 			vcap_copy_limited_actionfield((u8 *)&field->data.u32.value,
1156 						      value,
1157 						      width, field_size);
1158 			break;
1159 		case VCAP_FIELD_U48:
1160 			vcap_copy_limited_actionfield(field->data.u48.value,
1161 						      value,
1162 						      width, field_size);
1163 			break;
1164 		case VCAP_FIELD_U56:
1165 			vcap_copy_limited_actionfield(field->data.u56.value,
1166 						      value,
1167 						      width, field_size);
1168 			break;
1169 		case VCAP_FIELD_U64:
1170 			vcap_copy_limited_actionfield(field->data.u64.value,
1171 						      value,
1172 						      width, field_size);
1173 			break;
1174 		case VCAP_FIELD_U72:
1175 			vcap_copy_limited_actionfield(field->data.u72.value,
1176 						      value,
1177 						      width, field_size);
1178 			break;
1179 		case VCAP_FIELD_U112:
1180 			vcap_copy_limited_actionfield(field->data.u112.value,
1181 						      value,
1182 						      width, field_size);
1183 			break;
1184 		case VCAP_FIELD_U128:
1185 			vcap_copy_limited_actionfield(field->data.u128.value,
1186 						      value,
1187 						      width, field_size);
1188 			break;
1189 		}
1190 	}
1191 }
1192 
vcap_copy_key_bit_field(struct vcap_u1_key * field,u8 * value,u8 * mask)1193 static void vcap_copy_key_bit_field(struct vcap_u1_key *field,
1194 				    u8 *value, u8 *mask)
1195 {
1196 	field->value = (*value) & 0x1;
1197 	field->mask = (*mask) & 0x1;
1198 }
1199 
vcap_copy_limited_keyfield(u8 * dstvalue,u8 * dstmask,u8 * srcvalue,u8 * srcmask,int width,int bytes)1200 static void vcap_copy_limited_keyfield(u8 *dstvalue, u8 *dstmask,
1201 				       u8 *srcvalue, u8 *srcmask,
1202 				       int width, int bytes)
1203 {
1204 	memcpy(dstvalue, srcvalue, bytes);
1205 	vcap_apply_width(dstvalue, width, bytes);
1206 	memcpy(dstmask, srcmask, bytes);
1207 	vcap_apply_width(dstmask, width, bytes);
1208 }
1209 
vcap_copy_to_client_keyfield(struct vcap_rule_internal * ri,struct vcap_client_keyfield * field,u8 * value,u8 * mask,u16 width)1210 static void vcap_copy_to_client_keyfield(struct vcap_rule_internal *ri,
1211 					 struct vcap_client_keyfield *field,
1212 					 u8 *value, u8 *mask, u16 width)
1213 {
1214 	int field_size = keyfield_size_table[field->ctrl.type] / 2;
1215 
1216 	if (ri->admin->w32be) {
1217 		switch (field->ctrl.type) {
1218 		case VCAP_FIELD_BIT:
1219 			vcap_copy_key_bit_field(&field->data.u1, value, mask);
1220 			break;
1221 		case VCAP_FIELD_U32:
1222 			vcap_copy_limited_keyfield((u8 *)&field->data.u32.value,
1223 						   (u8 *)&field->data.u32.mask,
1224 						   value, mask,
1225 						   width, field_size);
1226 			break;
1227 		case VCAP_FIELD_U48:
1228 			vcap_copy_from_w32be(field->data.u48.value, value,
1229 					     field_size, width);
1230 			vcap_copy_from_w32be(field->data.u48.mask,  mask,
1231 					     field_size, width);
1232 			break;
1233 		case VCAP_FIELD_U56:
1234 			vcap_copy_from_w32be(field->data.u56.value, value,
1235 					     field_size, width);
1236 			vcap_copy_from_w32be(field->data.u56.mask,  mask,
1237 					     field_size, width);
1238 			break;
1239 		case VCAP_FIELD_U64:
1240 			vcap_copy_from_w32be(field->data.u64.value, value,
1241 					     field_size, width);
1242 			vcap_copy_from_w32be(field->data.u64.mask,  mask,
1243 					     field_size, width);
1244 			break;
1245 		case VCAP_FIELD_U72:
1246 			vcap_copy_from_w32be(field->data.u72.value, value,
1247 					     field_size, width);
1248 			vcap_copy_from_w32be(field->data.u72.mask,  mask,
1249 					     field_size, width);
1250 			break;
1251 		case VCAP_FIELD_U112:
1252 			vcap_copy_from_w32be(field->data.u112.value, value,
1253 					     field_size, width);
1254 			vcap_copy_from_w32be(field->data.u112.mask,  mask,
1255 					     field_size, width);
1256 			break;
1257 		case VCAP_FIELD_U128:
1258 			vcap_copy_from_w32be(field->data.u128.value, value,
1259 					     field_size, width);
1260 			vcap_copy_from_w32be(field->data.u128.mask,  mask,
1261 					     field_size, width);
1262 			break;
1263 		}
1264 	} else {
1265 		switch (field->ctrl.type) {
1266 		case VCAP_FIELD_BIT:
1267 			vcap_copy_key_bit_field(&field->data.u1, value, mask);
1268 			break;
1269 		case VCAP_FIELD_U32:
1270 			vcap_copy_limited_keyfield((u8 *)&field->data.u32.value,
1271 						   (u8 *)&field->data.u32.mask,
1272 						   value, mask,
1273 						   width, field_size);
1274 			break;
1275 		case VCAP_FIELD_U48:
1276 			vcap_copy_limited_keyfield(field->data.u48.value,
1277 						   field->data.u48.mask,
1278 						   value, mask,
1279 						   width, field_size);
1280 			break;
1281 		case VCAP_FIELD_U56:
1282 			vcap_copy_limited_keyfield(field->data.u56.value,
1283 						   field->data.u56.mask,
1284 						   value, mask,
1285 						   width, field_size);
1286 			break;
1287 		case VCAP_FIELD_U64:
1288 			vcap_copy_limited_keyfield(field->data.u64.value,
1289 						   field->data.u64.mask,
1290 						   value, mask,
1291 						   width, field_size);
1292 			break;
1293 		case VCAP_FIELD_U72:
1294 			vcap_copy_limited_keyfield(field->data.u72.value,
1295 						   field->data.u72.mask,
1296 						   value, mask,
1297 						   width, field_size);
1298 			break;
1299 		case VCAP_FIELD_U112:
1300 			vcap_copy_limited_keyfield(field->data.u112.value,
1301 						   field->data.u112.mask,
1302 						   value, mask,
1303 						   width, field_size);
1304 			break;
1305 		case VCAP_FIELD_U128:
1306 			vcap_copy_limited_keyfield(field->data.u128.value,
1307 						   field->data.u128.mask,
1308 						   value, mask,
1309 						   width, field_size);
1310 			break;
1311 		}
1312 	}
1313 }
1314 
vcap_rule_alloc_keyfield(struct vcap_rule_internal * ri,const struct vcap_field * keyfield,enum vcap_key_field key,u8 * value,u8 * mask)1315 static void vcap_rule_alloc_keyfield(struct vcap_rule_internal *ri,
1316 				     const struct vcap_field *keyfield,
1317 				     enum vcap_key_field key,
1318 				     u8 *value, u8 *mask)
1319 {
1320 	struct vcap_client_keyfield *field;
1321 
1322 	field = kzalloc_obj(*field);
1323 	if (!field)
1324 		return;
1325 	INIT_LIST_HEAD(&field->ctrl.list);
1326 	field->ctrl.key = key;
1327 	field->ctrl.type = keyfield->type;
1328 	vcap_copy_to_client_keyfield(ri, field, value, mask, keyfield->width);
1329 	list_add_tail(&field->ctrl.list, &ri->data.keyfields);
1330 }
1331 
1332 /* Read key data from a VCAP address and discover if there is a rule keyset
1333  * here
1334  */
1335 static bool
vcap_verify_actionstream_actionset(struct vcap_control * vctrl,enum vcap_type vt,u32 * actionstream,enum vcap_actionfield_set actionset)1336 vcap_verify_actionstream_actionset(struct vcap_control *vctrl,
1337 				   enum vcap_type vt,
1338 				   u32 *actionstream,
1339 				   enum vcap_actionfield_set actionset)
1340 {
1341 	const struct vcap_typegroup *tgt;
1342 	const struct vcap_field *fields;
1343 	const struct vcap_set *info;
1344 
1345 	if (vcap_actionfield_count(vctrl, vt, actionset) == 0)
1346 		return false;
1347 
1348 	info = vcap_actionfieldset(vctrl, vt, actionset);
1349 	/* Check that the actionset is valid */
1350 	if (!info)
1351 		return false;
1352 
1353 	/* a type_id of value -1 means that there is no type field */
1354 	if (info->type_id == (u8)-1)
1355 		return true;
1356 
1357 	/* Get a valid typegroup for the specific actionset */
1358 	tgt = vcap_actionfield_typegroup(vctrl, vt, actionset);
1359 	if (!tgt)
1360 		return false;
1361 
1362 	fields = vcap_actionfields(vctrl, vt, actionset);
1363 	if (!fields)
1364 		return false;
1365 
1366 	/* Later this will be expanded with a check of the type id */
1367 	return true;
1368 }
1369 
1370 /* Find the subword width of the action typegroup that matches the stream data
1371  */
vcap_find_actionstream_typegroup_sw(struct vcap_control * vctrl,enum vcap_type vt,u32 * stream,int sw_max)1372 static int vcap_find_actionstream_typegroup_sw(struct vcap_control *vctrl,
1373 					       enum vcap_type vt, u32 *stream,
1374 					       int sw_max)
1375 {
1376 	const struct vcap_typegroup **tgt;
1377 	int sw_idx, res;
1378 
1379 	tgt = vctrl->vcaps[vt].actionfield_set_typegroups;
1380 	/* Try the longest subword match first */
1381 	for (sw_idx = vctrl->vcaps[vt].sw_count; sw_idx >= 0; sw_idx--) {
1382 		if (!tgt[sw_idx])
1383 			continue;
1384 		res = vcap_verify_typegroups(stream, vctrl->vcaps[vt].act_width,
1385 					     tgt[sw_idx], false, sw_max);
1386 		if (res == 0)
1387 			return sw_idx;
1388 	}
1389 	return -EINVAL;
1390 }
1391 
1392 /* Verify that the typegroup information, subword count, actionset and type id
1393  * are in sync and correct, return the actionset
1394  */
1395 static enum vcap_actionfield_set
vcap_find_actionstream_actionset(struct vcap_control * vctrl,enum vcap_type vt,u32 * stream,int sw_max)1396 vcap_find_actionstream_actionset(struct vcap_control *vctrl,
1397 				 enum vcap_type vt,
1398 				 u32 *stream,
1399 				 int sw_max)
1400 {
1401 	const struct vcap_set *actionfield_set;
1402 	int sw_count, idx;
1403 	bool res;
1404 
1405 	sw_count = vcap_find_actionstream_typegroup_sw(vctrl, vt, stream,
1406 						       sw_max);
1407 	if (sw_count < 0)
1408 		return sw_count;
1409 
1410 	actionfield_set = vctrl->vcaps[vt].actionfield_set;
1411 	for (idx = 0; idx < vctrl->vcaps[vt].actionfield_set_size; ++idx) {
1412 		if (actionfield_set[idx].sw_per_item != sw_count)
1413 			continue;
1414 
1415 		res = vcap_verify_actionstream_actionset(vctrl, vt,
1416 							 stream, idx);
1417 		if (res)
1418 			return idx;
1419 	}
1420 	return -EINVAL;
1421 }
1422 
1423 /* Store action value in an element in a list for the client */
vcap_rule_alloc_actionfield(struct vcap_rule_internal * ri,const struct vcap_field * actionfield,enum vcap_action_field action,u8 * value)1424 static void vcap_rule_alloc_actionfield(struct vcap_rule_internal *ri,
1425 					const struct vcap_field *actionfield,
1426 					enum vcap_action_field action,
1427 					u8 *value)
1428 {
1429 	struct vcap_client_actionfield *field;
1430 
1431 	field = kzalloc_obj(*field);
1432 	if (!field)
1433 		return;
1434 	INIT_LIST_HEAD(&field->ctrl.list);
1435 	field->ctrl.action = action;
1436 	field->ctrl.type = actionfield->type;
1437 	vcap_copy_to_client_actionfield(ri, field, value, actionfield->width);
1438 	list_add_tail(&field->ctrl.list, &ri->data.actionfields);
1439 }
1440 
vcap_decode_actionset(struct vcap_rule_internal * ri)1441 static int vcap_decode_actionset(struct vcap_rule_internal *ri)
1442 {
1443 	struct vcap_control *vctrl = ri->vctrl;
1444 	struct vcap_admin *admin = ri->admin;
1445 	const struct vcap_field *actionfield;
1446 	enum vcap_actionfield_set actionset;
1447 	enum vcap_type vt = admin->vtype;
1448 	const struct vcap_typegroup *tgt;
1449 	struct vcap_stream_iter iter;
1450 	int idx, res, actfield_count;
1451 	u32 *actstream;
1452 	u8 value[16];
1453 
1454 	actstream = admin->cache.actionstream;
1455 	res = vcap_find_actionstream_actionset(vctrl, vt, actstream, 0);
1456 	if (res < 0) {
1457 		pr_err("%s:%d: could not find valid actionset: %d\n",
1458 		       __func__, __LINE__, res);
1459 		return -EINVAL;
1460 	}
1461 	actionset = res;
1462 	actfield_count = vcap_actionfield_count(vctrl, vt, actionset);
1463 	actionfield = vcap_actionfields(vctrl, vt, actionset);
1464 	tgt = vcap_actionfield_typegroup(vctrl, vt, actionset);
1465 	/* Start decoding the stream */
1466 	for (idx = 0; idx < actfield_count; ++idx) {
1467 		if (actionfield[idx].width <= 0)
1468 			continue;
1469 		/* Get the action */
1470 		memset(value, 0, DIV_ROUND_UP(actionfield[idx].width, 8));
1471 		vcap_iter_init(&iter, vctrl->vcaps[vt].act_width, tgt,
1472 			       actionfield[idx].offset);
1473 		vcap_decode_field(actstream, &iter, actionfield[idx].width,
1474 				  value);
1475 		/* Skip if no bits are set */
1476 		if (vcap_bitarray_zero(actionfield[idx].width, value))
1477 			continue;
1478 		vcap_rule_alloc_actionfield(ri, &actionfield[idx], idx, value);
1479 		/* Later the action id will also be checked */
1480 	}
1481 	return vcap_set_rule_set_actionset((struct vcap_rule *)ri, actionset);
1482 }
1483 
vcap_decode_keyset(struct vcap_rule_internal * ri)1484 static int vcap_decode_keyset(struct vcap_rule_internal *ri)
1485 {
1486 	struct vcap_control *vctrl = ri->vctrl;
1487 	struct vcap_stream_iter kiter, miter;
1488 	struct vcap_admin *admin = ri->admin;
1489 	enum vcap_keyfield_set keysets[10];
1490 	const struct vcap_field *keyfield;
1491 	enum vcap_type vt = admin->vtype;
1492 	const struct vcap_typegroup *tgt;
1493 	struct vcap_keyset_list matches;
1494 	enum vcap_keyfield_set keyset;
1495 	int idx, res, keyfield_count;
1496 	u32 *maskstream;
1497 	u32 *keystream;
1498 	u8 value[16];
1499 	u8 mask[16];
1500 
1501 	keystream = admin->cache.keystream;
1502 	maskstream = admin->cache.maskstream;
1503 	matches.keysets = keysets;
1504 	matches.cnt = 0;
1505 	matches.max = ARRAY_SIZE(keysets);
1506 	res = vcap_find_keystream_keysets(vctrl, vt, keystream, maskstream,
1507 					  false, 0, &matches);
1508 	if (res < 0) {
1509 		pr_err("%s:%d: could not find valid keysets: %d\n",
1510 		       __func__, __LINE__, res);
1511 		return -EINVAL;
1512 	}
1513 	keyset = matches.keysets[0];
1514 	keyfield_count = vcap_keyfield_count(vctrl, vt, keyset);
1515 	keyfield = vcap_keyfields(vctrl, vt, keyset);
1516 	tgt = vcap_keyfield_typegroup(vctrl, vt, keyset);
1517 	/* Start decoding the streams */
1518 	for (idx = 0; idx < keyfield_count; ++idx) {
1519 		if (keyfield[idx].width <= 0)
1520 			continue;
1521 		/* First get the mask */
1522 		memset(mask, 0, DIV_ROUND_UP(keyfield[idx].width, 8));
1523 		vcap_iter_init(&miter, vctrl->vcaps[vt].sw_width, tgt,
1524 			       keyfield[idx].offset);
1525 		vcap_decode_field(maskstream, &miter, keyfield[idx].width,
1526 				  mask);
1527 		/* Skip if no mask bits are set */
1528 		if (vcap_bitarray_zero(keyfield[idx].width, mask))
1529 			continue;
1530 		/* Get the key */
1531 		memset(value, 0, DIV_ROUND_UP(keyfield[idx].width, 8));
1532 		vcap_iter_init(&kiter, vctrl->vcaps[vt].sw_width, tgt,
1533 			       keyfield[idx].offset);
1534 		vcap_decode_field(keystream, &kiter, keyfield[idx].width,
1535 				  value);
1536 		vcap_rule_alloc_keyfield(ri, &keyfield[idx], idx, value, mask);
1537 	}
1538 	return vcap_set_rule_set_keyset((struct vcap_rule *)ri, keyset);
1539 }
1540 
1541 /* Read VCAP content into the VCAP cache */
vcap_read_rule(struct vcap_rule_internal * ri)1542 static int vcap_read_rule(struct vcap_rule_internal *ri)
1543 {
1544 	struct vcap_admin *admin = ri->admin;
1545 	int sw_idx, ent_idx = 0, act_idx = 0;
1546 	u32 addr = ri->addr;
1547 
1548 	if (!ri->size || !ri->keyset_sw_regs || !ri->actionset_sw_regs) {
1549 		pr_err("%s:%d: rule is empty\n", __func__, __LINE__);
1550 		return -EINVAL;
1551 	}
1552 	vcap_erase_cache(ri);
1553 	/* Use the values in the streams to read the VCAP cache */
1554 	for (sw_idx = 0; sw_idx < ri->size; sw_idx++, addr++) {
1555 		ri->vctrl->ops->update(ri->ndev, admin, VCAP_CMD_READ,
1556 				       VCAP_SEL_ALL, addr);
1557 		ri->vctrl->ops->cache_read(ri->ndev, admin,
1558 					   VCAP_SEL_ENTRY, ent_idx,
1559 					   ri->keyset_sw_regs);
1560 		ri->vctrl->ops->cache_read(ri->ndev, admin,
1561 					   VCAP_SEL_ACTION, act_idx,
1562 					   ri->actionset_sw_regs);
1563 		if (sw_idx == 0)
1564 			ri->vctrl->ops->cache_read(ri->ndev, admin,
1565 						   VCAP_SEL_COUNTER,
1566 						   ri->counter_id, 0);
1567 		ent_idx += ri->keyset_sw_regs;
1568 		act_idx += ri->actionset_sw_regs;
1569 	}
1570 	return 0;
1571 }
1572 
1573 /* Write VCAP cache content to the VCAP HW instance */
vcap_write_rule(struct vcap_rule_internal * ri)1574 static int vcap_write_rule(struct vcap_rule_internal *ri)
1575 {
1576 	struct vcap_admin *admin = ri->admin;
1577 	int sw_idx, ent_idx = 0, act_idx = 0;
1578 	u32 addr = ri->addr;
1579 
1580 	if (!ri->size || !ri->keyset_sw_regs || !ri->actionset_sw_regs) {
1581 		pr_err("%s:%d: rule is empty\n", __func__, __LINE__);
1582 		return -EINVAL;
1583 	}
1584 	/* Use the values in the streams to write the VCAP cache */
1585 	for (sw_idx = 0; sw_idx < ri->size; sw_idx++, addr++) {
1586 		ri->vctrl->ops->cache_write(ri->ndev, admin,
1587 					    VCAP_SEL_ENTRY, ent_idx,
1588 					    ri->keyset_sw_regs);
1589 		ri->vctrl->ops->cache_write(ri->ndev, admin,
1590 					    VCAP_SEL_ACTION, act_idx,
1591 					    ri->actionset_sw_regs);
1592 		ri->vctrl->ops->update(ri->ndev, admin, VCAP_CMD_WRITE,
1593 				       VCAP_SEL_ALL, addr);
1594 		ent_idx += ri->keyset_sw_regs;
1595 		act_idx += ri->actionset_sw_regs;
1596 	}
1597 	return 0;
1598 }
1599 
vcap_write_counter(struct vcap_rule_internal * ri,struct vcap_counter * ctr)1600 static int vcap_write_counter(struct vcap_rule_internal *ri,
1601 			      struct vcap_counter *ctr)
1602 {
1603 	struct vcap_admin *admin = ri->admin;
1604 
1605 	admin->cache.counter = ctr->value;
1606 	admin->cache.sticky = ctr->sticky;
1607 	ri->vctrl->ops->cache_write(ri->ndev, admin, VCAP_SEL_COUNTER,
1608 				    ri->counter_id, 0);
1609 	ri->vctrl->ops->update(ri->ndev, admin, VCAP_CMD_WRITE,
1610 			       VCAP_SEL_COUNTER, ri->addr);
1611 	return 0;
1612 }
1613 
1614 /* Convert a chain id to a VCAP lookup index */
vcap_chain_id_to_lookup(struct vcap_admin * admin,int cur_cid)1615 int vcap_chain_id_to_lookup(struct vcap_admin *admin, int cur_cid)
1616 {
1617 	int lookup_first = admin->vinst * admin->lookups_per_instance;
1618 	int lookup_last = lookup_first + admin->lookups_per_instance;
1619 	int cid_next = admin->first_cid + VCAP_CID_LOOKUP_SIZE;
1620 	int cid = admin->first_cid;
1621 	int lookup;
1622 
1623 	for (lookup = lookup_first; lookup < lookup_last; ++lookup,
1624 	     cid += VCAP_CID_LOOKUP_SIZE, cid_next += VCAP_CID_LOOKUP_SIZE)
1625 		if (cur_cid >= cid && cur_cid < cid_next)
1626 			return lookup;
1627 	return 0;
1628 }
1629 EXPORT_SYMBOL_GPL(vcap_chain_id_to_lookup);
1630 
1631 /* Lookup a vcap instance using chain id */
vcap_find_admin(struct vcap_control * vctrl,int cid)1632 struct vcap_admin *vcap_find_admin(struct vcap_control *vctrl, int cid)
1633 {
1634 	struct vcap_admin *admin;
1635 
1636 	if (vcap_api_check(vctrl))
1637 		return NULL;
1638 
1639 	list_for_each_entry(admin, &vctrl->list, list) {
1640 		if (cid >= admin->first_cid && cid <= admin->last_cid)
1641 			return admin;
1642 	}
1643 	return NULL;
1644 }
1645 EXPORT_SYMBOL_GPL(vcap_find_admin);
1646 
1647 /* Is this the last admin instance ordered by chain id and direction */
vcap_admin_is_last(struct vcap_control * vctrl,struct vcap_admin * admin,bool ingress)1648 static bool vcap_admin_is_last(struct vcap_control *vctrl,
1649 			       struct vcap_admin *admin,
1650 			       bool ingress)
1651 {
1652 	struct vcap_admin *iter, *last = NULL;
1653 	int max_cid = 0;
1654 
1655 	list_for_each_entry(iter, &vctrl->list, list) {
1656 		if (iter->first_cid > max_cid &&
1657 		    iter->ingress == ingress) {
1658 			last = iter;
1659 			max_cid = iter->first_cid;
1660 		}
1661 	}
1662 	if (!last)
1663 		return false;
1664 
1665 	return admin == last;
1666 }
1667 
1668 /* Calculate the value used for chaining VCAP rules */
vcap_chain_offset(struct vcap_control * vctrl,int from_cid,int to_cid)1669 int vcap_chain_offset(struct vcap_control *vctrl, int from_cid, int to_cid)
1670 {
1671 	int diff = to_cid - from_cid;
1672 
1673 	if (diff < 0) /* Wrong direction */
1674 		return diff;
1675 	to_cid %= VCAP_CID_LOOKUP_SIZE;
1676 	if (to_cid == 0)  /* Destination aligned to a lookup == no chaining */
1677 		return 0;
1678 	diff %= VCAP_CID_LOOKUP_SIZE;  /* Limit to a value within a lookup */
1679 	return diff;
1680 }
1681 EXPORT_SYMBOL_GPL(vcap_chain_offset);
1682 
1683 /* Is the next chain id in one of the following lookups
1684  * For now this does not support filters linked to other filters using
1685  * keys and actions. That will be added later.
1686  */
vcap_is_next_lookup(struct vcap_control * vctrl,int src_cid,int dst_cid)1687 bool vcap_is_next_lookup(struct vcap_control *vctrl, int src_cid, int dst_cid)
1688 {
1689 	struct vcap_admin *admin;
1690 	int next_cid;
1691 
1692 	if (vcap_api_check(vctrl))
1693 		return false;
1694 
1695 	/* The offset must be at least one lookup so round up one chain */
1696 	next_cid = roundup(src_cid + 1, VCAP_CID_LOOKUP_SIZE);
1697 
1698 	if (dst_cid < next_cid)
1699 		return false;
1700 
1701 	admin = vcap_find_admin(vctrl, dst_cid);
1702 	if (!admin)
1703 		return false;
1704 
1705 	return true;
1706 }
1707 EXPORT_SYMBOL_GPL(vcap_is_next_lookup);
1708 
1709 /* Check if there is room for a new rule */
vcap_rule_space(struct vcap_admin * admin,int size)1710 static int vcap_rule_space(struct vcap_admin *admin, int size)
1711 {
1712 	if (admin->last_used_addr - size < admin->first_valid_addr) {
1713 		pr_err("%s:%d: No room for rule size: %u, %u\n",
1714 		       __func__, __LINE__, size, admin->first_valid_addr);
1715 		return -ENOSPC;
1716 	}
1717 	return 0;
1718 }
1719 
1720 /* Add the keyset typefield to the list of rule keyfields */
vcap_add_type_keyfield(struct vcap_rule * rule)1721 static int vcap_add_type_keyfield(struct vcap_rule *rule)
1722 {
1723 	struct vcap_rule_internal *ri = to_intrule(rule);
1724 	enum vcap_keyfield_set keyset = rule->keyset;
1725 	enum vcap_type vt = ri->admin->vtype;
1726 	const struct vcap_field *fields;
1727 	const struct vcap_set *kset;
1728 	int ret = -EINVAL;
1729 
1730 	kset = vcap_keyfieldset(ri->vctrl, vt, keyset);
1731 	if (!kset)
1732 		return ret;
1733 	if (kset->type_id == (u8)-1)  /* No type field is needed */
1734 		return 0;
1735 
1736 	fields = vcap_keyfields(ri->vctrl, vt, keyset);
1737 	if (!fields)
1738 		return -EINVAL;
1739 	if (fields[VCAP_KF_TYPE].width > 1) {
1740 		ret = vcap_rule_add_key_u32(rule, VCAP_KF_TYPE,
1741 					    kset->type_id, 0xff);
1742 	} else {
1743 		if (kset->type_id)
1744 			ret = vcap_rule_add_key_bit(rule, VCAP_KF_TYPE,
1745 						    VCAP_BIT_1);
1746 		else
1747 			ret = vcap_rule_add_key_bit(rule, VCAP_KF_TYPE,
1748 						    VCAP_BIT_0);
1749 	}
1750 	return 0;
1751 }
1752 
1753 /* Add the actionset typefield to the list of rule actionfields */
vcap_add_type_actionfield(struct vcap_rule * rule)1754 static int vcap_add_type_actionfield(struct vcap_rule *rule)
1755 {
1756 	enum vcap_actionfield_set actionset = rule->actionset;
1757 	struct vcap_rule_internal *ri = to_intrule(rule);
1758 	enum vcap_type vt = ri->admin->vtype;
1759 	const struct vcap_field *fields;
1760 	const struct vcap_set *aset;
1761 	int ret = -EINVAL;
1762 
1763 	aset = vcap_actionfieldset(ri->vctrl, vt, actionset);
1764 	if (!aset)
1765 		return ret;
1766 	if (aset->type_id == (u8)-1)  /* No type field is needed */
1767 		return 0;
1768 
1769 	fields = vcap_actionfields(ri->vctrl, vt, actionset);
1770 	if (!fields)
1771 		return -EINVAL;
1772 	if (fields[VCAP_AF_TYPE].width > 1) {
1773 		ret = vcap_rule_add_action_u32(rule, VCAP_AF_TYPE,
1774 					       aset->type_id);
1775 	} else {
1776 		if (aset->type_id)
1777 			ret = vcap_rule_add_action_bit(rule, VCAP_AF_TYPE,
1778 						       VCAP_BIT_1);
1779 		else
1780 			ret = vcap_rule_add_action_bit(rule, VCAP_AF_TYPE,
1781 						       VCAP_BIT_0);
1782 	}
1783 	return ret;
1784 }
1785 
1786 /* Add a keyset to a keyset list */
vcap_keyset_list_add(struct vcap_keyset_list * keysetlist,enum vcap_keyfield_set keyset)1787 bool vcap_keyset_list_add(struct vcap_keyset_list *keysetlist,
1788 			  enum vcap_keyfield_set keyset)
1789 {
1790 	int idx;
1791 
1792 	if (keysetlist->cnt < keysetlist->max) {
1793 		/* Avoid duplicates */
1794 		for (idx = 0; idx < keysetlist->cnt; ++idx)
1795 			if (keysetlist->keysets[idx] == keyset)
1796 				return keysetlist->cnt < keysetlist->max;
1797 		keysetlist->keysets[keysetlist->cnt++] = keyset;
1798 	}
1799 	return keysetlist->cnt < keysetlist->max;
1800 }
1801 EXPORT_SYMBOL_GPL(vcap_keyset_list_add);
1802 
1803 /* Add a actionset to a actionset list */
vcap_actionset_list_add(struct vcap_actionset_list * actionsetlist,enum vcap_actionfield_set actionset)1804 static bool vcap_actionset_list_add(struct vcap_actionset_list *actionsetlist,
1805 				    enum vcap_actionfield_set actionset)
1806 {
1807 	int idx;
1808 
1809 	if (actionsetlist->cnt < actionsetlist->max) {
1810 		/* Avoid duplicates */
1811 		for (idx = 0; idx < actionsetlist->cnt; ++idx)
1812 			if (actionsetlist->actionsets[idx] == actionset)
1813 				return actionsetlist->cnt < actionsetlist->max;
1814 		actionsetlist->actionsets[actionsetlist->cnt++] = actionset;
1815 	}
1816 	return actionsetlist->cnt < actionsetlist->max;
1817 }
1818 
1819 /* map keyset id to a string with the keyset name */
vcap_keyset_name(struct vcap_control * vctrl,enum vcap_keyfield_set keyset)1820 const char *vcap_keyset_name(struct vcap_control *vctrl,
1821 			     enum vcap_keyfield_set keyset)
1822 {
1823 	return vctrl->stats->keyfield_set_names[keyset];
1824 }
1825 EXPORT_SYMBOL_GPL(vcap_keyset_name);
1826 
1827 /* map key field id to a string with the key name */
vcap_keyfield_name(struct vcap_control * vctrl,enum vcap_key_field key)1828 const char *vcap_keyfield_name(struct vcap_control *vctrl,
1829 			       enum vcap_key_field key)
1830 {
1831 	return vctrl->stats->keyfield_names[key];
1832 }
1833 EXPORT_SYMBOL_GPL(vcap_keyfield_name);
1834 
1835 /* map actionset id to a string with the actionset name */
vcap_actionset_name(struct vcap_control * vctrl,enum vcap_actionfield_set actionset)1836 const char *vcap_actionset_name(struct vcap_control *vctrl,
1837 				enum vcap_actionfield_set actionset)
1838 {
1839 	return vctrl->stats->actionfield_set_names[actionset];
1840 }
1841 
1842 /* map action field id to a string with the action name */
vcap_actionfield_name(struct vcap_control * vctrl,enum vcap_action_field action)1843 const char *vcap_actionfield_name(struct vcap_control *vctrl,
1844 				  enum vcap_action_field action)
1845 {
1846 	return vctrl->stats->actionfield_names[action];
1847 }
1848 
1849 /* Return the keyfield that matches a key in a keyset */
1850 static const struct vcap_field *
vcap_find_keyset_keyfield(struct vcap_control * vctrl,enum vcap_type vtype,enum vcap_keyfield_set keyset,enum vcap_key_field key)1851 vcap_find_keyset_keyfield(struct vcap_control *vctrl,
1852 			  enum vcap_type vtype,
1853 			  enum vcap_keyfield_set keyset,
1854 			  enum vcap_key_field key)
1855 {
1856 	const struct vcap_field *fields;
1857 	int idx, count;
1858 
1859 	fields = vcap_keyfields(vctrl, vtype, keyset);
1860 	if (!fields)
1861 		return NULL;
1862 
1863 	/* Iterate the keyfields of the keyset */
1864 	count = vcap_keyfield_count(vctrl, vtype, keyset);
1865 	for (idx = 0; idx < count; ++idx) {
1866 		if (fields[idx].width == 0)
1867 			continue;
1868 
1869 		if (key == idx)
1870 			return &fields[idx];
1871 	}
1872 
1873 	return NULL;
1874 }
1875 
1876 /* Match a list of keys against the keysets available in a vcap type */
_vcap_rule_find_keysets(struct vcap_rule_internal * ri,struct vcap_keyset_list * matches)1877 static bool _vcap_rule_find_keysets(struct vcap_rule_internal *ri,
1878 				    struct vcap_keyset_list *matches)
1879 {
1880 	const struct vcap_client_keyfield *ckf;
1881 	int keyset, found, keycount, map_size;
1882 	const struct vcap_field **map;
1883 	enum vcap_type vtype;
1884 
1885 	vtype = ri->admin->vtype;
1886 	map = ri->vctrl->vcaps[vtype].keyfield_set_map;
1887 	map_size = ri->vctrl->vcaps[vtype].keyfield_set_size;
1888 
1889 	/* Get a count of the keyfields we want to match */
1890 	keycount = 0;
1891 	list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list)
1892 		++keycount;
1893 
1894 	matches->cnt = 0;
1895 	/* Iterate the keysets of the VCAP */
1896 	for (keyset = 0; keyset < map_size; ++keyset) {
1897 		if (!map[keyset])
1898 			continue;
1899 
1900 		/* Iterate the keys in the rule */
1901 		found = 0;
1902 		list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list)
1903 			if (vcap_find_keyset_keyfield(ri->vctrl, vtype,
1904 						      keyset, ckf->ctrl.key))
1905 				++found;
1906 
1907 		/* Save the keyset if all keyfields were found */
1908 		if (found == keycount)
1909 			if (!vcap_keyset_list_add(matches, keyset))
1910 				/* bail out when the quota is filled */
1911 				break;
1912 	}
1913 
1914 	return matches->cnt > 0;
1915 }
1916 
1917 /* Match a list of keys against the keysets available in a vcap type */
vcap_rule_find_keysets(struct vcap_rule * rule,struct vcap_keyset_list * matches)1918 bool vcap_rule_find_keysets(struct vcap_rule *rule,
1919 			    struct vcap_keyset_list *matches)
1920 {
1921 	struct vcap_rule_internal *ri = to_intrule(rule);
1922 
1923 	return _vcap_rule_find_keysets(ri, matches);
1924 }
1925 EXPORT_SYMBOL_GPL(vcap_rule_find_keysets);
1926 
1927 /* Return the actionfield that matches a action in a actionset */
1928 static const struct vcap_field *
vcap_find_actionset_actionfield(struct vcap_control * vctrl,enum vcap_type vtype,enum vcap_actionfield_set actionset,enum vcap_action_field action)1929 vcap_find_actionset_actionfield(struct vcap_control *vctrl,
1930 				enum vcap_type vtype,
1931 				enum vcap_actionfield_set actionset,
1932 				enum vcap_action_field action)
1933 {
1934 	const struct vcap_field *fields;
1935 	int idx, count;
1936 
1937 	fields = vcap_actionfields(vctrl, vtype, actionset);
1938 	if (!fields)
1939 		return NULL;
1940 
1941 	/* Iterate the actionfields of the actionset */
1942 	count = vcap_actionfield_count(vctrl, vtype, actionset);
1943 	for (idx = 0; idx < count; ++idx) {
1944 		if (fields[idx].width == 0)
1945 			continue;
1946 
1947 		if (action == idx)
1948 			return &fields[idx];
1949 	}
1950 
1951 	return NULL;
1952 }
1953 
1954 /* Match a list of actions against the actionsets available in a vcap type */
vcap_rule_find_actionsets(struct vcap_rule_internal * ri,struct vcap_actionset_list * matches)1955 static bool vcap_rule_find_actionsets(struct vcap_rule_internal *ri,
1956 				      struct vcap_actionset_list *matches)
1957 {
1958 	int actionset, found, actioncount, map_size;
1959 	const struct vcap_client_actionfield *ckf;
1960 	const struct vcap_field **map;
1961 	enum vcap_type vtype;
1962 
1963 	vtype = ri->admin->vtype;
1964 	map = ri->vctrl->vcaps[vtype].actionfield_set_map;
1965 	map_size = ri->vctrl->vcaps[vtype].actionfield_set_size;
1966 
1967 	/* Get a count of the actionfields we want to match */
1968 	actioncount = 0;
1969 	list_for_each_entry(ckf, &ri->data.actionfields, ctrl.list)
1970 		++actioncount;
1971 
1972 	matches->cnt = 0;
1973 	/* Iterate the actionsets of the VCAP */
1974 	for (actionset = 0; actionset < map_size; ++actionset) {
1975 		if (!map[actionset])
1976 			continue;
1977 
1978 		/* Iterate the actions in the rule */
1979 		found = 0;
1980 		list_for_each_entry(ckf, &ri->data.actionfields, ctrl.list)
1981 			if (vcap_find_actionset_actionfield(ri->vctrl, vtype,
1982 							    actionset,
1983 							    ckf->ctrl.action))
1984 				++found;
1985 
1986 		/* Save the actionset if all actionfields were found */
1987 		if (found == actioncount)
1988 			if (!vcap_actionset_list_add(matches, actionset))
1989 				/* bail out when the quota is filled */
1990 				break;
1991 	}
1992 
1993 	return matches->cnt > 0;
1994 }
1995 
1996 /* Validate a rule with respect to available port keys */
vcap_val_rule(struct vcap_rule * rule,u16 l3_proto)1997 int vcap_val_rule(struct vcap_rule *rule, u16 l3_proto)
1998 {
1999 	struct vcap_rule_internal *ri = to_intrule(rule);
2000 	struct vcap_keyset_list matches = {};
2001 	enum vcap_keyfield_set keysets[10];
2002 	int ret;
2003 
2004 	ret = vcap_api_check(ri->vctrl);
2005 	if (ret)
2006 		return ret;
2007 	if (!ri->admin) {
2008 		ri->data.exterr = VCAP_ERR_NO_ADMIN;
2009 		return -EINVAL;
2010 	}
2011 	if (!ri->ndev) {
2012 		ri->data.exterr = VCAP_ERR_NO_NETDEV;
2013 		return -EINVAL;
2014 	}
2015 
2016 	matches.keysets = keysets;
2017 	matches.max = ARRAY_SIZE(keysets);
2018 	if (ri->data.keyset == VCAP_KFS_NO_VALUE) {
2019 		/* Iterate over rule keyfields and select keysets that fits */
2020 		if (!_vcap_rule_find_keysets(ri, &matches)) {
2021 			ri->data.exterr = VCAP_ERR_NO_KEYSET_MATCH;
2022 			return -EINVAL;
2023 		}
2024 	} else {
2025 		/* prepare for keyset validation */
2026 		keysets[0] = ri->data.keyset;
2027 		matches.cnt = 1;
2028 	}
2029 
2030 	/* Pick a keyset that is supported in the port lookups */
2031 	ret = ri->vctrl->ops->validate_keyset(ri->ndev, ri->admin, rule,
2032 					      &matches, l3_proto);
2033 	if (ret < 0) {
2034 		pr_err("%s:%d: keyset validation failed: %d\n",
2035 		       __func__, __LINE__, ret);
2036 		ri->data.exterr = VCAP_ERR_NO_PORT_KEYSET_MATCH;
2037 		return ret;
2038 	}
2039 	/* use the keyset that is supported in the port lookups */
2040 	ret = vcap_set_rule_set_keyset(rule, ret);
2041 	if (ret < 0) {
2042 		pr_err("%s:%d: keyset was not updated: %d\n",
2043 		       __func__, __LINE__, ret);
2044 		return ret;
2045 	}
2046 	if (ri->data.actionset == VCAP_AFS_NO_VALUE) {
2047 		struct vcap_actionset_list matches = {};
2048 		enum vcap_actionfield_set actionsets[10];
2049 
2050 		matches.actionsets = actionsets;
2051 		matches.max = ARRAY_SIZE(actionsets);
2052 
2053 		/* Find an actionset that fits the rule actions */
2054 		if (!vcap_rule_find_actionsets(ri, &matches)) {
2055 			ri->data.exterr = VCAP_ERR_NO_ACTIONSET_MATCH;
2056 			return -EINVAL;
2057 		}
2058 		ret = vcap_set_rule_set_actionset(rule, actionsets[0]);
2059 		if (ret < 0) {
2060 			pr_err("%s:%d: actionset was not updated: %d\n",
2061 			       __func__, __LINE__, ret);
2062 			return ret;
2063 		}
2064 	}
2065 	vcap_add_type_keyfield(rule);
2066 	vcap_add_type_actionfield(rule);
2067 	/* Add default fields to this rule */
2068 	ri->vctrl->ops->add_default_fields(ri->ndev, ri->admin, rule);
2069 
2070 	/* Rule size is the maximum of the entry and action subword count */
2071 	ri->size = max(ri->keyset_sw, ri->actionset_sw);
2072 
2073 	/* Finally check if there is room for the rule in the VCAP */
2074 	return vcap_rule_space(ri->admin, ri->size);
2075 }
2076 EXPORT_SYMBOL_GPL(vcap_val_rule);
2077 
2078 /* Entries are sorted with increasing values of sort_key.
2079  * I.e. Lowest numerical sort_key is first in list.
2080  * In order to locate largest keys first in list we negate the key size with
2081  * (max_size - size).
2082  */
vcap_sort_key(u32 max_size,u32 size,u8 user,u16 prio)2083 static u32 vcap_sort_key(u32 max_size, u32 size, u8 user, u16 prio)
2084 {
2085 	return ((max_size - size) << 24) | (user << 16) | prio;
2086 }
2087 
2088 /* calculate the address of the next rule after this (lower address and prio) */
vcap_next_rule_addr(u32 addr,struct vcap_rule_internal * ri)2089 static u32 vcap_next_rule_addr(u32 addr, struct vcap_rule_internal *ri)
2090 {
2091 	return ((addr - ri->size) /  ri->size) * ri->size;
2092 }
2093 
2094 /* Assign a unique rule id and autogenerate one if id == 0 */
vcap_set_rule_id(struct vcap_rule_internal * ri)2095 static u32 vcap_set_rule_id(struct vcap_rule_internal *ri)
2096 {
2097 	if (ri->data.id != 0)
2098 		return ri->data.id;
2099 
2100 	for (u32 next_id = 1; next_id < ~0; ++next_id) {
2101 		if (!vcap_rule_exists(ri->vctrl, next_id)) {
2102 			ri->data.id = next_id;
2103 			break;
2104 		}
2105 	}
2106 	return ri->data.id;
2107 }
2108 
vcap_insert_rule(struct vcap_rule_internal * ri,struct vcap_rule_move * move)2109 static int vcap_insert_rule(struct vcap_rule_internal *ri,
2110 			    struct vcap_rule_move *move)
2111 {
2112 	int sw_count = ri->vctrl->vcaps[ri->admin->vtype].sw_count;
2113 	struct vcap_rule_internal *duprule, *iter, *elem = NULL;
2114 	struct vcap_admin *admin = ri->admin;
2115 	u32 addr;
2116 
2117 	ri->sort_key = vcap_sort_key(sw_count, ri->size, ri->data.user,
2118 				     ri->data.priority);
2119 
2120 	/* Insert the new rule in the list of rule based on the sort key
2121 	 * If the rule needs to be  inserted between existing rules then move
2122 	 * these rules to make room for the new rule and update their start
2123 	 * address.
2124 	 */
2125 	list_for_each_entry(iter, &admin->rules, list) {
2126 		if (ri->sort_key < iter->sort_key) {
2127 			elem = iter;
2128 			break;
2129 		}
2130 	}
2131 
2132 	if (!elem) {
2133 		ri->addr = vcap_next_rule_addr(admin->last_used_addr, ri);
2134 		admin->last_used_addr = ri->addr;
2135 
2136 		/* Add a copy of the rule to the VCAP list */
2137 		duprule = vcap_dup_rule(ri, ri->state == VCAP_RS_DISABLED);
2138 		if (IS_ERR(duprule))
2139 			return PTR_ERR(duprule);
2140 
2141 		list_add_tail(&duprule->list, &admin->rules);
2142 		return 0;
2143 	}
2144 
2145 	/* Reuse the space of the current rule */
2146 	addr = elem->addr + elem->size;
2147 	ri->addr = vcap_next_rule_addr(addr, ri);
2148 	addr = ri->addr;
2149 
2150 	/* Add a copy of the rule to the VCAP list */
2151 	duprule = vcap_dup_rule(ri, ri->state == VCAP_RS_DISABLED);
2152 	if (IS_ERR(duprule))
2153 		return PTR_ERR(duprule);
2154 
2155 	/* Add before the current entry */
2156 	list_add_tail(&duprule->list, &elem->list);
2157 
2158 	/* Update the current rule */
2159 	elem->addr = vcap_next_rule_addr(addr, elem);
2160 	addr = elem->addr;
2161 
2162 	/* Update the address in the remaining rules in the list */
2163 	list_for_each_entry_continue(elem, &admin->rules, list) {
2164 		elem->addr = vcap_next_rule_addr(addr, elem);
2165 		addr = elem->addr;
2166 	}
2167 
2168 	/* Update the move info */
2169 	move->addr = admin->last_used_addr;
2170 	move->count = ri->addr - addr;
2171 	move->offset = admin->last_used_addr - addr;
2172 	admin->last_used_addr = addr;
2173 	return 0;
2174 }
2175 
vcap_move_rules(struct vcap_rule_internal * ri,struct vcap_rule_move * move)2176 static void vcap_move_rules(struct vcap_rule_internal *ri,
2177 			    struct vcap_rule_move *move)
2178 {
2179 	ri->vctrl->ops->move(ri->ndev, ri->admin, move->addr,
2180 			 move->offset, move->count);
2181 }
2182 
2183 /* Check if the chain is already used to enable a VCAP lookup for this port */
vcap_is_chain_used(struct vcap_control * vctrl,struct net_device * ndev,int src_cid)2184 static bool vcap_is_chain_used(struct vcap_control *vctrl,
2185 			       struct net_device *ndev, int src_cid)
2186 {
2187 	struct vcap_enabled_port *eport;
2188 	struct vcap_admin *admin;
2189 
2190 	list_for_each_entry(admin, &vctrl->list, list)
2191 		list_for_each_entry(eport, &admin->enabled, list)
2192 			if (eport->src_cid == src_cid && eport->ndev == ndev)
2193 				return true;
2194 
2195 	return false;
2196 }
2197 
2198 /* Fetch the next chain in the enabled list for the port */
vcap_get_next_chain(struct vcap_control * vctrl,struct net_device * ndev,int dst_cid)2199 static int vcap_get_next_chain(struct vcap_control *vctrl,
2200 			       struct net_device *ndev,
2201 			       int dst_cid)
2202 {
2203 	struct vcap_enabled_port *eport;
2204 	struct vcap_admin *admin;
2205 
2206 	list_for_each_entry(admin, &vctrl->list, list) {
2207 		list_for_each_entry(eport, &admin->enabled, list) {
2208 			if (eport->ndev != ndev)
2209 				continue;
2210 			if (eport->src_cid == dst_cid)
2211 				return eport->dst_cid;
2212 		}
2213 	}
2214 
2215 	return 0;
2216 }
2217 
vcap_path_exist(struct vcap_control * vctrl,struct net_device * ndev,int dst_cid)2218 static bool vcap_path_exist(struct vcap_control *vctrl, struct net_device *ndev,
2219 			    int dst_cid)
2220 {
2221 	int cid = rounddown(dst_cid, VCAP_CID_LOOKUP_SIZE);
2222 	struct vcap_enabled_port *eport = NULL;
2223 	struct vcap_enabled_port *elem;
2224 	struct vcap_admin *admin;
2225 	int tmp;
2226 
2227 	if (cid == 0) /* Chain zero is always available */
2228 		return true;
2229 
2230 	/* Find first entry that starts from chain 0*/
2231 	list_for_each_entry(admin, &vctrl->list, list) {
2232 		list_for_each_entry(elem, &admin->enabled, list) {
2233 			if (elem->src_cid == 0 && elem->ndev == ndev) {
2234 				eport = elem;
2235 				break;
2236 			}
2237 		}
2238 		if (eport)
2239 			break;
2240 	}
2241 
2242 	if (!eport)
2243 		return false;
2244 
2245 	tmp = eport->dst_cid;
2246 	while (tmp != cid && tmp != 0)
2247 		tmp = vcap_get_next_chain(vctrl, ndev, tmp);
2248 
2249 	return !!tmp;
2250 }
2251 
2252 /* Internal clients can always store their rules in HW
2253  * External clients can store their rules if the chain is enabled all
2254  * the way from chain 0, otherwise the rule will be cached until
2255  * the chain is enabled.
2256  */
vcap_rule_set_state(struct vcap_rule_internal * ri)2257 static void vcap_rule_set_state(struct vcap_rule_internal *ri)
2258 {
2259 	if (ri->data.user <= VCAP_USER_QOS)
2260 		ri->state = VCAP_RS_PERMANENT;
2261 	else if (vcap_path_exist(ri->vctrl, ri->ndev, ri->data.vcap_chain_id))
2262 		ri->state = VCAP_RS_ENABLED;
2263 	else
2264 		ri->state = VCAP_RS_DISABLED;
2265 }
2266 
2267 /* Encode and write a validated rule to the VCAP */
vcap_add_rule(struct vcap_rule * rule)2268 int vcap_add_rule(struct vcap_rule *rule)
2269 {
2270 	struct vcap_rule_internal *ri = to_intrule(rule);
2271 	struct vcap_rule_move move = {0};
2272 	struct vcap_counter ctr = {0};
2273 	int ret;
2274 
2275 	ret = vcap_api_check(ri->vctrl);
2276 	if (ret)
2277 		return ret;
2278 	/* Insert the new rule in the list of vcap rules */
2279 	vcap_lock(ri->admin);
2280 
2281 	vcap_rule_set_state(ri);
2282 	ret = vcap_insert_rule(ri, &move);
2283 	if (ret < 0) {
2284 		pr_err("%s:%d: could not insert rule in vcap list: %d\n",
2285 		       __func__, __LINE__, ret);
2286 		goto out;
2287 	}
2288 	if (move.count > 0)
2289 		vcap_move_rules(ri, &move);
2290 
2291 	/* Set the counter to zero */
2292 	ret = vcap_write_counter(ri, &ctr);
2293 	if (ret)
2294 		goto out;
2295 
2296 	if (ri->state == VCAP_RS_DISABLED) {
2297 		/* Erase the rule area */
2298 		ri->vctrl->ops->init(ri->ndev, ri->admin, ri->addr, ri->size);
2299 		goto out;
2300 	}
2301 
2302 	vcap_erase_cache(ri);
2303 	ret = vcap_encode_rule(ri);
2304 	if (ret) {
2305 		pr_err("%s:%d: rule encoding error: %d\n", __func__, __LINE__, ret);
2306 		goto out;
2307 	}
2308 
2309 	ret = vcap_write_rule(ri);
2310 	if (ret) {
2311 		pr_err("%s:%d: rule write error: %d\n", __func__, __LINE__, ret);
2312 		goto out;
2313 	}
2314 out:
2315 	vcap_unlock(ri->admin);
2316 	return ret;
2317 }
2318 EXPORT_SYMBOL_GPL(vcap_add_rule);
2319 
2320 /* Allocate a new rule with the provided arguments */
vcap_alloc_rule(struct vcap_control * vctrl,struct net_device * ndev,int vcap_chain_id,enum vcap_user user,u16 priority,u32 id)2321 struct vcap_rule *vcap_alloc_rule(struct vcap_control *vctrl,
2322 				  struct net_device *ndev, int vcap_chain_id,
2323 				  enum vcap_user user, u16 priority,
2324 				  u32 id)
2325 {
2326 	struct vcap_rule_internal *ri;
2327 	struct vcap_admin *admin;
2328 	int err, maxsize;
2329 
2330 	err = vcap_api_check(vctrl);
2331 	if (err)
2332 		return ERR_PTR(err);
2333 	if (!ndev)
2334 		return ERR_PTR(-ENODEV);
2335 	/* Get the VCAP instance */
2336 	admin = vcap_find_admin(vctrl, vcap_chain_id);
2337 	if (!admin)
2338 		return ERR_PTR(-ENOENT);
2339 	/* Sanity check that this VCAP is supported on this platform */
2340 	if (vctrl->vcaps[admin->vtype].rows == 0)
2341 		return ERR_PTR(-EINVAL);
2342 
2343 	vcap_lock(admin);
2344 	/* Check if a rule with this id already exists */
2345 	if (vcap_rule_exists(vctrl, id)) {
2346 		err = -EINVAL;
2347 		goto out_unlock;
2348 	}
2349 
2350 	/* Check if there is room for the rule in the block(s) of the VCAP */
2351 	maxsize = vctrl->vcaps[admin->vtype].sw_count; /* worst case rule size */
2352 	if (vcap_rule_space(admin, maxsize)) {
2353 		err = -ENOSPC;
2354 		goto out_unlock;
2355 	}
2356 
2357 	/* Create a container for the rule and return it */
2358 	ri = kzalloc_obj(*ri);
2359 	if (!ri) {
2360 		err = -ENOMEM;
2361 		goto out_unlock;
2362 	}
2363 
2364 	ri->data.vcap_chain_id = vcap_chain_id;
2365 	ri->data.user = user;
2366 	ri->data.priority = priority;
2367 	ri->data.id = id;
2368 	ri->data.keyset = VCAP_KFS_NO_VALUE;
2369 	ri->data.actionset = VCAP_AFS_NO_VALUE;
2370 	INIT_LIST_HEAD(&ri->list);
2371 	INIT_LIST_HEAD(&ri->data.keyfields);
2372 	INIT_LIST_HEAD(&ri->data.actionfields);
2373 	ri->ndev = ndev;
2374 	ri->admin = admin; /* refer to the vcap instance */
2375 	ri->vctrl = vctrl; /* refer to the client */
2376 
2377 	if (vcap_set_rule_id(ri) == 0) {
2378 		err = -EINVAL;
2379 		goto out_free;
2380 	}
2381 
2382 	vcap_unlock(admin);
2383 	return (struct vcap_rule *)ri;
2384 
2385 out_free:
2386 	kfree(ri);
2387 out_unlock:
2388 	vcap_unlock(admin);
2389 	return ERR_PTR(err);
2390 
2391 }
2392 EXPORT_SYMBOL_GPL(vcap_alloc_rule);
2393 
2394 /* Free mem of a rule owned by client after the rule as been added to the VCAP */
vcap_free_rule(struct vcap_rule * rule)2395 void vcap_free_rule(struct vcap_rule *rule)
2396 {
2397 	struct vcap_rule_internal *ri = to_intrule(rule);
2398 	struct vcap_client_actionfield *caf, *next_caf;
2399 	struct vcap_client_keyfield *ckf, *next_ckf;
2400 
2401 	/* Deallocate the list of keys and actions */
2402 	list_for_each_entry_safe(ckf, next_ckf, &ri->data.keyfields, ctrl.list) {
2403 		list_del(&ckf->ctrl.list);
2404 		kfree(ckf);
2405 	}
2406 	list_for_each_entry_safe(caf, next_caf, &ri->data.actionfields, ctrl.list) {
2407 		list_del(&caf->ctrl.list);
2408 		kfree(caf);
2409 	}
2410 	/* Deallocate the rule */
2411 	kfree(rule);
2412 }
2413 EXPORT_SYMBOL_GPL(vcap_free_rule);
2414 
2415 /* Decode a rule from the VCAP cache and return a copy */
vcap_decode_rule(struct vcap_rule_internal * elem)2416 struct vcap_rule *vcap_decode_rule(struct vcap_rule_internal *elem)
2417 {
2418 	struct vcap_rule_internal *ri;
2419 	int err;
2420 
2421 	ri = vcap_dup_rule(elem, elem->state == VCAP_RS_DISABLED);
2422 	if (IS_ERR(ri))
2423 		return ERR_CAST(ri);
2424 
2425 	if (ri->state == VCAP_RS_DISABLED)
2426 		goto out;
2427 
2428 	err = vcap_read_rule(ri);
2429 	if (err)
2430 		goto err_free_rule;
2431 
2432 	err = vcap_decode_keyset(ri);
2433 	if (err)
2434 		goto err_free_rule;
2435 
2436 	err = vcap_decode_actionset(ri);
2437 	if (err)
2438 		goto err_free_rule;
2439 
2440 out:
2441 	return &ri->data;
2442 err_free_rule:
2443 	vcap_free_rule(&ri->data);
2444 	return ERR_PTR(err);
2445 }
2446 
vcap_get_rule(struct vcap_control * vctrl,u32 id)2447 struct vcap_rule *vcap_get_rule(struct vcap_control *vctrl, u32 id)
2448 {
2449 	struct vcap_rule_internal *elem;
2450 	struct vcap_rule *rule;
2451 	int err;
2452 
2453 	err = vcap_api_check(vctrl);
2454 	if (err)
2455 		return ERR_PTR(err);
2456 
2457 	elem = vcap_get_locked_rule(vctrl, id);
2458 	if (!elem)
2459 		return ERR_PTR(-ENOENT);
2460 
2461 	rule = vcap_decode_rule(elem);
2462 	vcap_unlock(elem->admin);
2463 	return rule;
2464 }
2465 EXPORT_SYMBOL_GPL(vcap_get_rule);
2466 
2467 /* Update existing rule */
vcap_mod_rule(struct vcap_rule * rule)2468 int vcap_mod_rule(struct vcap_rule *rule)
2469 {
2470 	struct vcap_rule_internal *ri = to_intrule(rule);
2471 	struct vcap_counter ctr;
2472 	int err;
2473 
2474 	err = vcap_api_check(ri->vctrl);
2475 	if (err)
2476 		return err;
2477 
2478 	if (!vcap_get_locked_rule(ri->vctrl, ri->data.id))
2479 		return -ENOENT;
2480 
2481 	vcap_rule_set_state(ri);
2482 	if (ri->state == VCAP_RS_DISABLED)
2483 		goto out;
2484 
2485 	/* Encode the bitstreams to the VCAP cache */
2486 	vcap_erase_cache(ri);
2487 	err = vcap_encode_rule(ri);
2488 	if (err)
2489 		goto out;
2490 
2491 	err = vcap_write_rule(ri);
2492 	if (err)
2493 		goto out;
2494 
2495 	memset(&ctr, 0, sizeof(ctr));
2496 	err =  vcap_write_counter(ri, &ctr);
2497 
2498 out:
2499 	vcap_unlock(ri->admin);
2500 	return err;
2501 }
2502 EXPORT_SYMBOL_GPL(vcap_mod_rule);
2503 
2504 /* Return the alignment offset for a new rule address */
vcap_valid_rule_move(struct vcap_rule_internal * el,int offset)2505 static int vcap_valid_rule_move(struct vcap_rule_internal *el, int offset)
2506 {
2507 	return (el->addr + offset) % el->size;
2508 }
2509 
2510 /* Update the rule address with an offset */
vcap_adjust_rule_addr(struct vcap_rule_internal * el,int offset)2511 static void vcap_adjust_rule_addr(struct vcap_rule_internal *el, int offset)
2512 {
2513 	el->addr += offset;
2514 }
2515 
2516 /* Rules needs to be moved to fill the gap of the deleted rule */
vcap_fill_rule_gap(struct vcap_rule_internal * ri)2517 static int vcap_fill_rule_gap(struct vcap_rule_internal *ri)
2518 {
2519 	struct vcap_admin *admin = ri->admin;
2520 	struct vcap_rule_internal *elem;
2521 	struct vcap_rule_move move;
2522 	int gap = 0, offset = 0;
2523 
2524 	/* If the first rule is deleted: Move other rules to the top */
2525 	if (list_is_first(&ri->list, &admin->rules))
2526 		offset = admin->last_valid_addr + 1 - ri->addr - ri->size;
2527 
2528 	/* Locate gaps between odd size rules and adjust the move */
2529 	elem = ri;
2530 	list_for_each_entry_continue(elem, &admin->rules, list)
2531 		gap += vcap_valid_rule_move(elem, ri->size);
2532 
2533 	/* Update the address in the remaining rules in the list */
2534 	elem = ri;
2535 	list_for_each_entry_continue(elem, &admin->rules, list)
2536 		vcap_adjust_rule_addr(elem, ri->size + gap + offset);
2537 
2538 	/* Update the move info */
2539 	move.addr = admin->last_used_addr;
2540 	move.count = ri->addr - admin->last_used_addr - gap;
2541 	move.offset = -(ri->size + gap + offset);
2542 
2543 	/* Do the actual move operation */
2544 	vcap_move_rules(ri, &move);
2545 
2546 	return gap + offset;
2547 }
2548 
2549 /* Delete rule in a VCAP instance */
vcap_del_rule(struct vcap_control * vctrl,struct net_device * ndev,u32 id)2550 int vcap_del_rule(struct vcap_control *vctrl, struct net_device *ndev, u32 id)
2551 {
2552 	struct vcap_rule_internal *ri, *elem;
2553 	struct vcap_admin *admin;
2554 	int gap = 0, err;
2555 
2556 	/* This will later also handle rule moving */
2557 	if (!ndev)
2558 		return -ENODEV;
2559 	err = vcap_api_check(vctrl);
2560 	if (err)
2561 		return err;
2562 	/* Look for the rule id in all vcaps */
2563 	ri = vcap_get_locked_rule(vctrl, id);
2564 	if (!ri)
2565 		return -ENOENT;
2566 
2567 	admin = ri->admin;
2568 
2569 	if (ri->addr > admin->last_used_addr)
2570 		gap = vcap_fill_rule_gap(ri);
2571 
2572 	/* Delete the rule from the list of rules and the cache */
2573 	list_del(&ri->list);
2574 	vctrl->ops->init(ndev, admin, admin->last_used_addr, ri->size + gap);
2575 	vcap_free_rule(&ri->data);
2576 
2577 	/* Update the last used address, set to default when no rules */
2578 	if (list_empty(&admin->rules)) {
2579 		admin->last_used_addr = admin->last_valid_addr + 1;
2580 	} else {
2581 		elem = list_last_entry(&admin->rules, struct vcap_rule_internal,
2582 				       list);
2583 		admin->last_used_addr = elem->addr;
2584 	}
2585 
2586 	vcap_unlock(admin);
2587 	return err;
2588 }
2589 EXPORT_SYMBOL_GPL(vcap_del_rule);
2590 
2591 /* Delete all rules in the VCAP instance */
vcap_del_rules(struct vcap_control * vctrl,struct vcap_admin * admin)2592 int vcap_del_rules(struct vcap_control *vctrl, struct vcap_admin *admin)
2593 {
2594 	struct vcap_enabled_port *eport, *next_eport;
2595 	struct vcap_rule_internal *ri, *next_ri;
2596 	int ret = vcap_api_check(vctrl);
2597 
2598 	if (ret)
2599 		return ret;
2600 
2601 	vcap_lock(admin);
2602 	list_for_each_entry_safe(ri, next_ri, &admin->rules, list) {
2603 		vctrl->ops->init(ri->ndev, admin, ri->addr, ri->size);
2604 		list_del(&ri->list);
2605 		vcap_free_rule(&ri->data);
2606 	}
2607 	admin->last_used_addr = admin->last_valid_addr;
2608 
2609 	/* Remove list of enabled ports */
2610 	list_for_each_entry_safe(eport, next_eport, &admin->enabled, list) {
2611 		list_del(&eport->list);
2612 		kfree(eport);
2613 	}
2614 	vcap_unlock(admin);
2615 
2616 	return 0;
2617 }
2618 EXPORT_SYMBOL_GPL(vcap_del_rules);
2619 
2620 /* Find a client key field in a rule */
2621 static struct vcap_client_keyfield *
vcap_find_keyfield(struct vcap_rule * rule,enum vcap_key_field key)2622 vcap_find_keyfield(struct vcap_rule *rule, enum vcap_key_field key)
2623 {
2624 	struct vcap_rule_internal *ri = to_intrule(rule);
2625 	struct vcap_client_keyfield *ckf;
2626 
2627 	list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list)
2628 		if (ckf->ctrl.key == key)
2629 			return ckf;
2630 	return NULL;
2631 }
2632 
2633 /* Find information on a key field in a rule */
vcap_lookup_keyfield(struct vcap_rule * rule,enum vcap_key_field key)2634 const struct vcap_field *vcap_lookup_keyfield(struct vcap_rule *rule,
2635 					      enum vcap_key_field key)
2636 {
2637 	struct vcap_rule_internal *ri = to_intrule(rule);
2638 	enum vcap_keyfield_set keyset = rule->keyset;
2639 	enum vcap_type vt = ri->admin->vtype;
2640 	const struct vcap_field *fields;
2641 
2642 	if (keyset == VCAP_KFS_NO_VALUE)
2643 		return NULL;
2644 	fields = vcap_keyfields(ri->vctrl, vt, keyset);
2645 	if (!fields)
2646 		return NULL;
2647 	return &fields[key];
2648 }
2649 EXPORT_SYMBOL_GPL(vcap_lookup_keyfield);
2650 
2651 /* Check if the keyfield is already in the rule */
vcap_keyfield_unique(struct vcap_rule * rule,enum vcap_key_field key)2652 static bool vcap_keyfield_unique(struct vcap_rule *rule,
2653 				 enum vcap_key_field key)
2654 {
2655 	struct vcap_rule_internal *ri = to_intrule(rule);
2656 	const struct vcap_client_keyfield *ckf;
2657 
2658 	list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list)
2659 		if (ckf->ctrl.key == key)
2660 			return false;
2661 	return true;
2662 }
2663 
2664 /* Check if the keyfield is in the keyset */
vcap_keyfield_match_keyset(struct vcap_rule * rule,enum vcap_key_field key)2665 static bool vcap_keyfield_match_keyset(struct vcap_rule *rule,
2666 				       enum vcap_key_field key)
2667 {
2668 	struct vcap_rule_internal *ri = to_intrule(rule);
2669 	enum vcap_keyfield_set keyset = rule->keyset;
2670 	enum vcap_type vt = ri->admin->vtype;
2671 	const struct vcap_field *fields;
2672 
2673 	/* the field is accepted if the rule has no keyset yet */
2674 	if (keyset == VCAP_KFS_NO_VALUE)
2675 		return true;
2676 	fields = vcap_keyfields(ri->vctrl, vt, keyset);
2677 	if (!fields)
2678 		return false;
2679 	/* if there is a width there is a way */
2680 	return fields[key].width > 0;
2681 }
2682 
vcap_rule_add_key(struct vcap_rule * rule,enum vcap_key_field key,enum vcap_field_type ftype,struct vcap_client_keyfield_data * data)2683 static int vcap_rule_add_key(struct vcap_rule *rule,
2684 			     enum vcap_key_field key,
2685 			     enum vcap_field_type ftype,
2686 			     struct vcap_client_keyfield_data *data)
2687 {
2688 	struct vcap_rule_internal *ri = to_intrule(rule);
2689 	struct vcap_client_keyfield *field;
2690 
2691 	if (!vcap_keyfield_unique(rule, key)) {
2692 		pr_warn("%s:%d: keyfield %s is already in the rule\n",
2693 			__func__, __LINE__,
2694 			vcap_keyfield_name(ri->vctrl, key));
2695 		return -EINVAL;
2696 	}
2697 
2698 	if (!vcap_keyfield_match_keyset(rule, key)) {
2699 		pr_err("%s:%d: keyfield %s does not belong in the rule keyset\n",
2700 		       __func__, __LINE__,
2701 		       vcap_keyfield_name(ri->vctrl, key));
2702 		return -EINVAL;
2703 	}
2704 
2705 	field = kzalloc_obj(*field);
2706 	if (!field)
2707 		return -ENOMEM;
2708 	memcpy(&field->data, data, sizeof(field->data));
2709 	field->ctrl.key = key;
2710 	field->ctrl.type = ftype;
2711 	list_add_tail(&field->ctrl.list, &rule->keyfields);
2712 	return 0;
2713 }
2714 
vcap_rule_set_key_bitsize(struct vcap_u1_key * u1,enum vcap_bit val)2715 static void vcap_rule_set_key_bitsize(struct vcap_u1_key *u1, enum vcap_bit val)
2716 {
2717 	switch (val) {
2718 	case VCAP_BIT_0:
2719 		u1->value = 0;
2720 		u1->mask = 1;
2721 		break;
2722 	case VCAP_BIT_1:
2723 		u1->value = 1;
2724 		u1->mask = 1;
2725 		break;
2726 	case VCAP_BIT_ANY:
2727 		u1->value = 0;
2728 		u1->mask = 0;
2729 		break;
2730 	}
2731 }
2732 
2733 /* Add a bit key with value and mask to the rule */
vcap_rule_add_key_bit(struct vcap_rule * rule,enum vcap_key_field key,enum vcap_bit val)2734 int vcap_rule_add_key_bit(struct vcap_rule *rule, enum vcap_key_field key,
2735 			  enum vcap_bit val)
2736 {
2737 	struct vcap_client_keyfield_data data;
2738 
2739 	vcap_rule_set_key_bitsize(&data.u1, val);
2740 	return vcap_rule_add_key(rule, key, VCAP_FIELD_BIT, &data);
2741 }
2742 EXPORT_SYMBOL_GPL(vcap_rule_add_key_bit);
2743 
2744 /* Add a 32 bit key field with value and mask to the rule */
vcap_rule_add_key_u32(struct vcap_rule * rule,enum vcap_key_field key,u32 value,u32 mask)2745 int vcap_rule_add_key_u32(struct vcap_rule *rule, enum vcap_key_field key,
2746 			  u32 value, u32 mask)
2747 {
2748 	struct vcap_client_keyfield_data data;
2749 
2750 	data.u32.value = value;
2751 	data.u32.mask = mask;
2752 	return vcap_rule_add_key(rule, key, VCAP_FIELD_U32, &data);
2753 }
2754 EXPORT_SYMBOL_GPL(vcap_rule_add_key_u32);
2755 
2756 /* Add a 48 bit key with value and mask to the rule */
vcap_rule_add_key_u48(struct vcap_rule * rule,enum vcap_key_field key,struct vcap_u48_key * fieldval)2757 int vcap_rule_add_key_u48(struct vcap_rule *rule, enum vcap_key_field key,
2758 			  struct vcap_u48_key *fieldval)
2759 {
2760 	struct vcap_client_keyfield_data data;
2761 
2762 	memcpy(&data.u48, fieldval, sizeof(data.u48));
2763 	return vcap_rule_add_key(rule, key, VCAP_FIELD_U48, &data);
2764 }
2765 EXPORT_SYMBOL_GPL(vcap_rule_add_key_u48);
2766 
2767 /* Add a 72 bit key with value and mask to the rule */
vcap_rule_add_key_u72(struct vcap_rule * rule,enum vcap_key_field key,struct vcap_u72_key * fieldval)2768 int vcap_rule_add_key_u72(struct vcap_rule *rule, enum vcap_key_field key,
2769 			  struct vcap_u72_key *fieldval)
2770 {
2771 	struct vcap_client_keyfield_data data;
2772 
2773 	memcpy(&data.u72, fieldval, sizeof(data.u72));
2774 	return vcap_rule_add_key(rule, key, VCAP_FIELD_U72, &data);
2775 }
2776 EXPORT_SYMBOL_GPL(vcap_rule_add_key_u72);
2777 
2778 /* Add a 128 bit key with value and mask to the rule */
vcap_rule_add_key_u128(struct vcap_rule * rule,enum vcap_key_field key,struct vcap_u128_key * fieldval)2779 int vcap_rule_add_key_u128(struct vcap_rule *rule, enum vcap_key_field key,
2780 			   struct vcap_u128_key *fieldval)
2781 {
2782 	struct vcap_client_keyfield_data data;
2783 
2784 	memcpy(&data.u128, fieldval, sizeof(data.u128));
2785 	return vcap_rule_add_key(rule, key, VCAP_FIELD_U128, &data);
2786 }
2787 EXPORT_SYMBOL_GPL(vcap_rule_add_key_u128);
2788 
vcap_rule_get_key_u32(struct vcap_rule * rule,enum vcap_key_field key,u32 * value,u32 * mask)2789 int vcap_rule_get_key_u32(struct vcap_rule *rule, enum vcap_key_field key,
2790 			  u32 *value, u32 *mask)
2791 {
2792 	struct vcap_client_keyfield *ckf;
2793 
2794 	ckf = vcap_find_keyfield(rule, key);
2795 	if (!ckf)
2796 		return -ENOENT;
2797 
2798 	*value = ckf->data.u32.value;
2799 	*mask = ckf->data.u32.mask;
2800 
2801 	return 0;
2802 }
2803 EXPORT_SYMBOL_GPL(vcap_rule_get_key_u32);
2804 
2805 /* Find a client action field in a rule */
2806 struct vcap_client_actionfield *
vcap_find_actionfield(struct vcap_rule * rule,enum vcap_action_field act)2807 vcap_find_actionfield(struct vcap_rule *rule, enum vcap_action_field act)
2808 {
2809 	struct vcap_rule_internal *ri = (struct vcap_rule_internal *)rule;
2810 	struct vcap_client_actionfield *caf;
2811 
2812 	list_for_each_entry(caf, &ri->data.actionfields, ctrl.list)
2813 		if (caf->ctrl.action == act)
2814 			return caf;
2815 	return NULL;
2816 }
2817 EXPORT_SYMBOL_GPL(vcap_find_actionfield);
2818 
2819 /* Check if the actionfield is already in the rule */
vcap_actionfield_unique(struct vcap_rule * rule,enum vcap_action_field act)2820 static bool vcap_actionfield_unique(struct vcap_rule *rule,
2821 				    enum vcap_action_field act)
2822 {
2823 	struct vcap_rule_internal *ri = to_intrule(rule);
2824 	const struct vcap_client_actionfield *caf;
2825 
2826 	list_for_each_entry(caf, &ri->data.actionfields, ctrl.list)
2827 		if (caf->ctrl.action == act)
2828 			return false;
2829 	return true;
2830 }
2831 
2832 /* Check if the actionfield is in the actionset */
vcap_actionfield_match_actionset(struct vcap_rule * rule,enum vcap_action_field action)2833 static bool vcap_actionfield_match_actionset(struct vcap_rule *rule,
2834 					     enum vcap_action_field action)
2835 {
2836 	enum vcap_actionfield_set actionset = rule->actionset;
2837 	struct vcap_rule_internal *ri = to_intrule(rule);
2838 	enum vcap_type vt = ri->admin->vtype;
2839 	const struct vcap_field *fields;
2840 
2841 	/* the field is accepted if the rule has no actionset yet */
2842 	if (actionset == VCAP_AFS_NO_VALUE)
2843 		return true;
2844 	fields = vcap_actionfields(ri->vctrl, vt, actionset);
2845 	if (!fields)
2846 		return false;
2847 	/* if there is a width there is a way */
2848 	return fields[action].width > 0;
2849 }
2850 
vcap_rule_add_action(struct vcap_rule * rule,enum vcap_action_field action,enum vcap_field_type ftype,struct vcap_client_actionfield_data * data)2851 static int vcap_rule_add_action(struct vcap_rule *rule,
2852 				enum vcap_action_field action,
2853 				enum vcap_field_type ftype,
2854 				struct vcap_client_actionfield_data *data)
2855 {
2856 	struct vcap_rule_internal *ri = to_intrule(rule);
2857 	struct vcap_client_actionfield *field;
2858 
2859 	if (!vcap_actionfield_unique(rule, action)) {
2860 		pr_warn("%s:%d: actionfield %s is already in the rule\n",
2861 			__func__, __LINE__,
2862 			vcap_actionfield_name(ri->vctrl, action));
2863 		return -EINVAL;
2864 	}
2865 
2866 	if (!vcap_actionfield_match_actionset(rule, action)) {
2867 		pr_err("%s:%d: actionfield %s does not belong in the rule actionset\n",
2868 		       __func__, __LINE__,
2869 		       vcap_actionfield_name(ri->vctrl, action));
2870 		return -EINVAL;
2871 	}
2872 
2873 	field = kzalloc_obj(*field);
2874 	if (!field)
2875 		return -ENOMEM;
2876 	memcpy(&field->data, data, sizeof(field->data));
2877 	field->ctrl.action = action;
2878 	field->ctrl.type = ftype;
2879 	list_add_tail(&field->ctrl.list, &rule->actionfields);
2880 	return 0;
2881 }
2882 
vcap_rule_set_action_bitsize(struct vcap_u1_action * u1,enum vcap_bit val)2883 static void vcap_rule_set_action_bitsize(struct vcap_u1_action *u1,
2884 					 enum vcap_bit val)
2885 {
2886 	switch (val) {
2887 	case VCAP_BIT_0:
2888 		u1->value = 0;
2889 		break;
2890 	case VCAP_BIT_1:
2891 		u1->value = 1;
2892 		break;
2893 	case VCAP_BIT_ANY:
2894 		u1->value = 0;
2895 		break;
2896 	}
2897 }
2898 
2899 /* Add a bit action with value to the rule */
vcap_rule_add_action_bit(struct vcap_rule * rule,enum vcap_action_field action,enum vcap_bit val)2900 int vcap_rule_add_action_bit(struct vcap_rule *rule,
2901 			     enum vcap_action_field action,
2902 			     enum vcap_bit val)
2903 {
2904 	struct vcap_client_actionfield_data data;
2905 
2906 	vcap_rule_set_action_bitsize(&data.u1, val);
2907 	return vcap_rule_add_action(rule, action, VCAP_FIELD_BIT, &data);
2908 }
2909 EXPORT_SYMBOL_GPL(vcap_rule_add_action_bit);
2910 
2911 /* Add a 32 bit action field with value to the rule */
vcap_rule_add_action_u32(struct vcap_rule * rule,enum vcap_action_field action,u32 value)2912 int vcap_rule_add_action_u32(struct vcap_rule *rule,
2913 			     enum vcap_action_field action,
2914 			     u32 value)
2915 {
2916 	struct vcap_client_actionfield_data data;
2917 
2918 	data.u32.value = value;
2919 	return vcap_rule_add_action(rule, action, VCAP_FIELD_U32, &data);
2920 }
2921 EXPORT_SYMBOL_GPL(vcap_rule_add_action_u32);
2922 
2923 /* Add a 72 bit action field with value to the rule */
vcap_rule_add_action_u72(struct vcap_rule * rule,enum vcap_action_field action,struct vcap_u72_action * fieldval)2924 int vcap_rule_add_action_u72(struct vcap_rule *rule,
2925 			     enum vcap_action_field action,
2926 			     struct vcap_u72_action *fieldval)
2927 {
2928 	struct vcap_client_actionfield_data data;
2929 
2930 	memcpy(&data.u72, fieldval, sizeof(data.u72));
2931 	return vcap_rule_add_action(rule, action, VCAP_FIELD_U72, &data);
2932 }
2933 EXPORT_SYMBOL_GPL(vcap_rule_add_action_u72);
2934 
vcap_read_counter(struct vcap_rule_internal * ri,struct vcap_counter * ctr)2935 static int vcap_read_counter(struct vcap_rule_internal *ri,
2936 			     struct vcap_counter *ctr)
2937 {
2938 	struct vcap_admin *admin = ri->admin;
2939 
2940 	ri->vctrl->ops->update(ri->ndev, admin, VCAP_CMD_READ, VCAP_SEL_COUNTER,
2941 			       ri->addr);
2942 	ri->vctrl->ops->cache_read(ri->ndev, admin, VCAP_SEL_COUNTER,
2943 				   ri->counter_id, 0);
2944 	ctr->value = admin->cache.counter;
2945 	ctr->sticky = admin->cache.sticky;
2946 	return 0;
2947 }
2948 
2949 /* Copy to host byte order */
vcap_netbytes_copy(u8 * dst,u8 * src,int count)2950 void vcap_netbytes_copy(u8 *dst, u8 *src, int count)
2951 {
2952 	int idx;
2953 
2954 	for (idx = 0; idx < count; ++idx, ++dst)
2955 		*dst = src[count - idx - 1];
2956 }
2957 EXPORT_SYMBOL_GPL(vcap_netbytes_copy);
2958 
2959 /* Convert validation error code into tc extack error message */
vcap_set_tc_exterr(struct flow_cls_offload * fco,struct vcap_rule * vrule)2960 void vcap_set_tc_exterr(struct flow_cls_offload *fco, struct vcap_rule *vrule)
2961 {
2962 	switch (vrule->exterr) {
2963 	case VCAP_ERR_NONE:
2964 		break;
2965 	case VCAP_ERR_NO_ADMIN:
2966 		NL_SET_ERR_MSG_MOD(fco->common.extack,
2967 				   "Missing VCAP instance");
2968 		break;
2969 	case VCAP_ERR_NO_NETDEV:
2970 		NL_SET_ERR_MSG_MOD(fco->common.extack,
2971 				   "Missing network interface");
2972 		break;
2973 	case VCAP_ERR_NO_KEYSET_MATCH:
2974 		NL_SET_ERR_MSG_MOD(fco->common.extack,
2975 				   "No keyset matched the filter keys");
2976 		break;
2977 	case VCAP_ERR_NO_ACTIONSET_MATCH:
2978 		NL_SET_ERR_MSG_MOD(fco->common.extack,
2979 				   "No actionset matched the filter actions");
2980 		break;
2981 	case VCAP_ERR_NO_PORT_KEYSET_MATCH:
2982 		NL_SET_ERR_MSG_MOD(fco->common.extack,
2983 				   "No port keyset matched the filter keys");
2984 		break;
2985 	}
2986 }
2987 EXPORT_SYMBOL_GPL(vcap_set_tc_exterr);
2988 
2989 /* Write a rule to VCAP HW to enable it */
vcap_enable_rule(struct vcap_rule_internal * ri)2990 static int vcap_enable_rule(struct vcap_rule_internal *ri)
2991 {
2992 	struct vcap_client_actionfield *af, *naf;
2993 	struct vcap_client_keyfield *kf, *nkf;
2994 	int err;
2995 
2996 	vcap_erase_cache(ri);
2997 	err = vcap_encode_rule(ri);
2998 	if (err)
2999 		goto out;
3000 	err = vcap_write_rule(ri);
3001 	if (err)
3002 		goto out;
3003 
3004 	/* Deallocate the list of keys and actions */
3005 	list_for_each_entry_safe(kf, nkf, &ri->data.keyfields, ctrl.list) {
3006 		list_del(&kf->ctrl.list);
3007 		kfree(kf);
3008 	}
3009 	list_for_each_entry_safe(af, naf, &ri->data.actionfields, ctrl.list) {
3010 		list_del(&af->ctrl.list);
3011 		kfree(af);
3012 	}
3013 	ri->state = VCAP_RS_ENABLED;
3014 out:
3015 	return err;
3016 }
3017 
3018 /* Enable all disabled rules for a specific chain/port in the VCAP HW */
vcap_enable_rules(struct vcap_control * vctrl,struct net_device * ndev,int chain)3019 static int vcap_enable_rules(struct vcap_control *vctrl,
3020 			     struct net_device *ndev, int chain)
3021 {
3022 	int next_chain = chain + VCAP_CID_LOOKUP_SIZE;
3023 	struct vcap_rule_internal *ri;
3024 	struct vcap_admin *admin;
3025 	int err = 0;
3026 
3027 	list_for_each_entry(admin, &vctrl->list, list) {
3028 		if (!(chain >= admin->first_cid && chain <= admin->last_cid))
3029 			continue;
3030 
3031 		/* Found the admin, now find the offloadable rules */
3032 		vcap_lock(admin);
3033 		list_for_each_entry(ri, &admin->rules, list) {
3034 			/* Is the rule in the lookup defined by the chain */
3035 			if (!(ri->data.vcap_chain_id >= chain &&
3036 			      ri->data.vcap_chain_id < next_chain)) {
3037 				continue;
3038 			}
3039 
3040 			if (ri->ndev != ndev)
3041 				continue;
3042 
3043 			if (ri->state != VCAP_RS_DISABLED)
3044 				continue;
3045 
3046 			err = vcap_enable_rule(ri);
3047 			if (err)
3048 				break;
3049 		}
3050 		vcap_unlock(admin);
3051 		if (err)
3052 			break;
3053 	}
3054 	return err;
3055 }
3056 
3057 /* Read and erase a rule from VCAP HW to disable it */
vcap_disable_rule(struct vcap_rule_internal * ri)3058 static int vcap_disable_rule(struct vcap_rule_internal *ri)
3059 {
3060 	int err;
3061 
3062 	err = vcap_read_rule(ri);
3063 	if (err)
3064 		return err;
3065 	err = vcap_decode_keyset(ri);
3066 	if (err)
3067 		return err;
3068 	err = vcap_decode_actionset(ri);
3069 	if (err)
3070 		return err;
3071 
3072 	ri->state = VCAP_RS_DISABLED;
3073 	ri->vctrl->ops->init(ri->ndev, ri->admin, ri->addr, ri->size);
3074 	return 0;
3075 }
3076 
3077 /* Disable all enabled rules for a specific chain/port in the VCAP HW */
vcap_disable_rules(struct vcap_control * vctrl,struct net_device * ndev,int chain)3078 static int vcap_disable_rules(struct vcap_control *vctrl,
3079 			      struct net_device *ndev, int chain)
3080 {
3081 	struct vcap_rule_internal *ri;
3082 	struct vcap_admin *admin;
3083 	int err = 0;
3084 
3085 	list_for_each_entry(admin, &vctrl->list, list) {
3086 		if (!(chain >= admin->first_cid && chain <= admin->last_cid))
3087 			continue;
3088 
3089 		/* Found the admin, now find the rules on the chain */
3090 		vcap_lock(admin);
3091 		list_for_each_entry(ri, &admin->rules, list) {
3092 			if (ri->data.vcap_chain_id != chain)
3093 				continue;
3094 
3095 			if (ri->ndev != ndev)
3096 				continue;
3097 
3098 			if (ri->state != VCAP_RS_ENABLED)
3099 				continue;
3100 
3101 			err = vcap_disable_rule(ri);
3102 			if (err)
3103 				break;
3104 		}
3105 		vcap_unlock(admin);
3106 		if (err)
3107 			break;
3108 	}
3109 	return err;
3110 }
3111 
3112 /* Check if this port is already enabled for this VCAP instance */
vcap_is_enabled(struct vcap_control * vctrl,struct net_device * ndev,int dst_cid)3113 static bool vcap_is_enabled(struct vcap_control *vctrl, struct net_device *ndev,
3114 			    int dst_cid)
3115 {
3116 	struct vcap_enabled_port *eport;
3117 	struct vcap_admin *admin;
3118 
3119 	list_for_each_entry(admin, &vctrl->list, list)
3120 		list_for_each_entry(eport, &admin->enabled, list)
3121 			if (eport->dst_cid == dst_cid && eport->ndev == ndev)
3122 				return true;
3123 
3124 	return false;
3125 }
3126 
3127 /* Enable this port and chain id in a VCAP instance */
vcap_enable(struct vcap_control * vctrl,struct net_device * ndev,unsigned long cookie,int src_cid,int dst_cid)3128 static int vcap_enable(struct vcap_control *vctrl, struct net_device *ndev,
3129 		       unsigned long cookie, int src_cid, int dst_cid)
3130 {
3131 	struct vcap_enabled_port *eport;
3132 	struct vcap_admin *admin;
3133 
3134 	if (src_cid >= dst_cid)
3135 		return -EFAULT;
3136 
3137 	admin = vcap_find_admin(vctrl, dst_cid);
3138 	if (!admin)
3139 		return -ENOENT;
3140 
3141 	eport = kzalloc_obj(*eport);
3142 	if (!eport)
3143 		return -ENOMEM;
3144 
3145 	eport->ndev = ndev;
3146 	eport->cookie = cookie;
3147 	eport->src_cid = src_cid;
3148 	eport->dst_cid = dst_cid;
3149 	vcap_lock(admin);
3150 	list_add_tail(&eport->list, &admin->enabled);
3151 	vcap_unlock(admin);
3152 
3153 	if (vcap_path_exist(vctrl, ndev, src_cid)) {
3154 		/* Enable chained lookups */
3155 		while (dst_cid) {
3156 			admin = vcap_find_admin(vctrl, dst_cid);
3157 			if (!admin)
3158 				return -ENOENT;
3159 
3160 			vcap_enable_rules(vctrl, ndev, dst_cid);
3161 			dst_cid = vcap_get_next_chain(vctrl, ndev, dst_cid);
3162 		}
3163 	}
3164 	return 0;
3165 }
3166 
3167 /* Disable this port and chain id for a VCAP instance */
vcap_disable(struct vcap_control * vctrl,struct net_device * ndev,unsigned long cookie)3168 static int vcap_disable(struct vcap_control *vctrl, struct net_device *ndev,
3169 			unsigned long cookie)
3170 {
3171 	struct vcap_enabled_port *elem, *eport = NULL;
3172 	struct vcap_admin *found = NULL, *admin;
3173 	int dst_cid;
3174 
3175 	list_for_each_entry(admin, &vctrl->list, list) {
3176 		list_for_each_entry(elem, &admin->enabled, list) {
3177 			if (elem->cookie == cookie && elem->ndev == ndev) {
3178 				eport = elem;
3179 				found = admin;
3180 				break;
3181 			}
3182 		}
3183 		if (eport)
3184 			break;
3185 	}
3186 
3187 	if (!eport)
3188 		return -ENOENT;
3189 
3190 	/* Disable chained lookups */
3191 	dst_cid = eport->dst_cid;
3192 	while (dst_cid) {
3193 		admin = vcap_find_admin(vctrl, dst_cid);
3194 		if (!admin)
3195 			return -ENOENT;
3196 
3197 		vcap_disable_rules(vctrl, ndev, dst_cid);
3198 		dst_cid = vcap_get_next_chain(vctrl, ndev, dst_cid);
3199 	}
3200 
3201 	vcap_lock(found);
3202 	list_del(&eport->list);
3203 	vcap_unlock(found);
3204 	kfree(eport);
3205 	return 0;
3206 }
3207 
3208 /* Enable/Disable the VCAP instance lookups */
vcap_enable_lookups(struct vcap_control * vctrl,struct net_device * ndev,int src_cid,int dst_cid,unsigned long cookie,bool enable)3209 int vcap_enable_lookups(struct vcap_control *vctrl, struct net_device *ndev,
3210 			int src_cid, int dst_cid, unsigned long cookie,
3211 			bool enable)
3212 {
3213 	int err;
3214 
3215 	err = vcap_api_check(vctrl);
3216 	if (err)
3217 		return err;
3218 
3219 	if (!ndev)
3220 		return -ENODEV;
3221 
3222 	/* Source and destination must be the first chain in a lookup */
3223 	if (src_cid % VCAP_CID_LOOKUP_SIZE)
3224 		return -EFAULT;
3225 	if (dst_cid % VCAP_CID_LOOKUP_SIZE)
3226 		return -EFAULT;
3227 
3228 	if (enable) {
3229 		if (vcap_is_enabled(vctrl, ndev, dst_cid))
3230 			return -EADDRINUSE;
3231 		if (vcap_is_chain_used(vctrl, ndev, src_cid))
3232 			return -EADDRNOTAVAIL;
3233 		err = vcap_enable(vctrl, ndev, cookie, src_cid, dst_cid);
3234 	} else {
3235 		err = vcap_disable(vctrl, ndev, cookie);
3236 	}
3237 
3238 	return err;
3239 }
3240 EXPORT_SYMBOL_GPL(vcap_enable_lookups);
3241 
3242 /* Is this chain id the last lookup of all VCAPs */
vcap_is_last_chain(struct vcap_control * vctrl,int cid,bool ingress)3243 bool vcap_is_last_chain(struct vcap_control *vctrl, int cid, bool ingress)
3244 {
3245 	struct vcap_admin *admin;
3246 	int lookup;
3247 
3248 	if (vcap_api_check(vctrl))
3249 		return false;
3250 
3251 	admin = vcap_find_admin(vctrl, cid);
3252 	if (!admin)
3253 		return false;
3254 
3255 	if (!vcap_admin_is_last(vctrl, admin, ingress))
3256 		return false;
3257 
3258 	/* This must be the last lookup in this VCAP type */
3259 	lookup = vcap_chain_id_to_lookup(admin, cid);
3260 	return lookup == admin->lookups - 1;
3261 }
3262 EXPORT_SYMBOL_GPL(vcap_is_last_chain);
3263 
3264 /* Set a rule counter id (for certain vcaps only) */
vcap_rule_set_counter_id(struct vcap_rule * rule,u32 counter_id)3265 void vcap_rule_set_counter_id(struct vcap_rule *rule, u32 counter_id)
3266 {
3267 	struct vcap_rule_internal *ri = to_intrule(rule);
3268 
3269 	ri->counter_id = counter_id;
3270 }
3271 EXPORT_SYMBOL_GPL(vcap_rule_set_counter_id);
3272 
vcap_rule_set_counter(struct vcap_rule * rule,struct vcap_counter * ctr)3273 int vcap_rule_set_counter(struct vcap_rule *rule, struct vcap_counter *ctr)
3274 {
3275 	struct vcap_rule_internal *ri = to_intrule(rule);
3276 	int err;
3277 
3278 	err = vcap_api_check(ri->vctrl);
3279 	if (err)
3280 		return err;
3281 	if (!ctr) {
3282 		pr_err("%s:%d: counter is missing\n", __func__, __LINE__);
3283 		return -EINVAL;
3284 	}
3285 
3286 	vcap_lock(ri->admin);
3287 	err = vcap_write_counter(ri, ctr);
3288 	vcap_unlock(ri->admin);
3289 
3290 	return err;
3291 }
3292 EXPORT_SYMBOL_GPL(vcap_rule_set_counter);
3293 
vcap_rule_get_counter(struct vcap_rule * rule,struct vcap_counter * ctr)3294 int vcap_rule_get_counter(struct vcap_rule *rule, struct vcap_counter *ctr)
3295 {
3296 	struct vcap_rule_internal *ri = to_intrule(rule);
3297 	int err;
3298 
3299 	err = vcap_api_check(ri->vctrl);
3300 	if (err)
3301 		return err;
3302 	if (!ctr) {
3303 		pr_err("%s:%d: counter is missing\n", __func__, __LINE__);
3304 		return -EINVAL;
3305 	}
3306 
3307 	vcap_lock(ri->admin);
3308 	err = vcap_read_counter(ri, ctr);
3309 	vcap_unlock(ri->admin);
3310 
3311 	return err;
3312 }
3313 EXPORT_SYMBOL_GPL(vcap_rule_get_counter);
3314 
3315 /* Get a copy of a client key field */
vcap_rule_get_key(struct vcap_rule * rule,enum vcap_key_field key,struct vcap_client_keyfield * ckf)3316 static int vcap_rule_get_key(struct vcap_rule *rule,
3317 			     enum vcap_key_field key,
3318 			     struct vcap_client_keyfield *ckf)
3319 {
3320 	struct vcap_client_keyfield *field;
3321 
3322 	field = vcap_find_keyfield(rule, key);
3323 	if (!field)
3324 		return -EINVAL;
3325 	memcpy(ckf, field, sizeof(*ckf));
3326 	INIT_LIST_HEAD(&ckf->ctrl.list);
3327 	return 0;
3328 }
3329 
3330 /* Find a keyset having the same size as the provided rule, where the keyset
3331  * does not have a type id.
3332  */
vcap_rule_get_untyped_keyset(struct vcap_rule_internal * ri,struct vcap_keyset_list * matches)3333 static int vcap_rule_get_untyped_keyset(struct vcap_rule_internal *ri,
3334 					struct vcap_keyset_list *matches)
3335 {
3336 	struct vcap_control *vctrl = ri->vctrl;
3337 	enum vcap_type vt = ri->admin->vtype;
3338 	const struct vcap_set *keyfield_set;
3339 	int idx;
3340 
3341 	keyfield_set = vctrl->vcaps[vt].keyfield_set;
3342 	for (idx = 0; idx < vctrl->vcaps[vt].keyfield_set_size; ++idx) {
3343 		if (keyfield_set[idx].sw_per_item == ri->keyset_sw &&
3344 		    keyfield_set[idx].type_id == (u8)-1) {
3345 			vcap_keyset_list_add(matches, idx);
3346 			return 0;
3347 		}
3348 	}
3349 	return -EINVAL;
3350 }
3351 
3352 /* Get the keysets that matches the rule key type/mask */
vcap_rule_get_keysets(struct vcap_rule_internal * ri,struct vcap_keyset_list * matches)3353 int vcap_rule_get_keysets(struct vcap_rule_internal *ri,
3354 			  struct vcap_keyset_list *matches)
3355 {
3356 	struct vcap_control *vctrl = ri->vctrl;
3357 	enum vcap_type vt = ri->admin->vtype;
3358 	const struct vcap_set *keyfield_set;
3359 	struct vcap_client_keyfield kf = {};
3360 	u32 value, mask;
3361 	int err, idx;
3362 
3363 	err = vcap_rule_get_key(&ri->data, VCAP_KF_TYPE, &kf);
3364 	if (err)
3365 		return vcap_rule_get_untyped_keyset(ri, matches);
3366 
3367 	if (kf.ctrl.type == VCAP_FIELD_BIT) {
3368 		value = kf.data.u1.value;
3369 		mask = kf.data.u1.mask;
3370 	} else if (kf.ctrl.type == VCAP_FIELD_U32) {
3371 		value = kf.data.u32.value;
3372 		mask = kf.data.u32.mask;
3373 	} else {
3374 		return -EINVAL;
3375 	}
3376 
3377 	keyfield_set = vctrl->vcaps[vt].keyfield_set;
3378 	for (idx = 0; idx < vctrl->vcaps[vt].keyfield_set_size; ++idx) {
3379 		if (keyfield_set[idx].sw_per_item != ri->keyset_sw)
3380 			continue;
3381 
3382 		if (keyfield_set[idx].type_id == (u8)-1) {
3383 			vcap_keyset_list_add(matches, idx);
3384 			continue;
3385 		}
3386 
3387 		if ((keyfield_set[idx].type_id & mask) == value)
3388 			vcap_keyset_list_add(matches, idx);
3389 	}
3390 	if (matches->cnt > 0)
3391 		return 0;
3392 
3393 	return -EINVAL;
3394 }
3395 
3396 /* Collect packet counts from all rules with the same cookie */
vcap_get_rule_count_by_cookie(struct vcap_control * vctrl,struct vcap_counter * ctr,u64 cookie)3397 int vcap_get_rule_count_by_cookie(struct vcap_control *vctrl,
3398 				  struct vcap_counter *ctr, u64 cookie)
3399 {
3400 	struct vcap_rule_internal *ri;
3401 	struct vcap_counter temp = {};
3402 	struct vcap_admin *admin;
3403 	int err;
3404 
3405 	err = vcap_api_check(vctrl);
3406 	if (err)
3407 		return err;
3408 
3409 	/* Iterate all rules in each VCAP instance */
3410 	list_for_each_entry(admin, &vctrl->list, list) {
3411 		vcap_lock(admin);
3412 		list_for_each_entry(ri, &admin->rules, list) {
3413 			if (ri->data.cookie != cookie)
3414 				continue;
3415 
3416 			err = vcap_read_counter(ri, &temp);
3417 			if (err)
3418 				goto unlock;
3419 			ctr->value += temp.value;
3420 
3421 			/* Reset the rule counter */
3422 			temp.value = 0;
3423 			temp.sticky = 0;
3424 			err = vcap_write_counter(ri, &temp);
3425 			if (err)
3426 				goto unlock;
3427 		}
3428 		vcap_unlock(admin);
3429 	}
3430 	return err;
3431 
3432 unlock:
3433 	vcap_unlock(admin);
3434 	return err;
3435 }
3436 EXPORT_SYMBOL_GPL(vcap_get_rule_count_by_cookie);
3437 
vcap_rule_mod_key(struct vcap_rule * rule,enum vcap_key_field key,enum vcap_field_type ftype,struct vcap_client_keyfield_data * data)3438 static int vcap_rule_mod_key(struct vcap_rule *rule,
3439 			     enum vcap_key_field key,
3440 			     enum vcap_field_type ftype,
3441 			     struct vcap_client_keyfield_data *data)
3442 {
3443 	struct vcap_client_keyfield *field;
3444 
3445 	field = vcap_find_keyfield(rule, key);
3446 	if (!field)
3447 		return vcap_rule_add_key(rule, key, ftype, data);
3448 	memcpy(&field->data, data, sizeof(field->data));
3449 	return 0;
3450 }
3451 
3452 /* Modify a 32 bit key field with value and mask in the rule */
vcap_rule_mod_key_u32(struct vcap_rule * rule,enum vcap_key_field key,u32 value,u32 mask)3453 int vcap_rule_mod_key_u32(struct vcap_rule *rule, enum vcap_key_field key,
3454 			  u32 value, u32 mask)
3455 {
3456 	struct vcap_client_keyfield_data data;
3457 
3458 	data.u32.value = value;
3459 	data.u32.mask = mask;
3460 	return vcap_rule_mod_key(rule, key, VCAP_FIELD_U32, &data);
3461 }
3462 EXPORT_SYMBOL_GPL(vcap_rule_mod_key_u32);
3463 
3464 /* Remove a key field with value and mask in the rule */
vcap_rule_rem_key(struct vcap_rule * rule,enum vcap_key_field key)3465 int vcap_rule_rem_key(struct vcap_rule *rule, enum vcap_key_field key)
3466 {
3467 	struct vcap_rule_internal *ri = to_intrule(rule);
3468 	struct vcap_client_keyfield *field;
3469 
3470 	field = vcap_find_keyfield(rule, key);
3471 	if (!field) {
3472 		pr_err("%s:%d: key %s is not in the rule\n",
3473 		       __func__, __LINE__, vcap_keyfield_name(ri->vctrl, key));
3474 		return -EINVAL;
3475 	}
3476 	/* Deallocate the key field */
3477 	list_del(&field->ctrl.list);
3478 	kfree(field);
3479 	return 0;
3480 }
3481 EXPORT_SYMBOL_GPL(vcap_rule_rem_key);
3482 
vcap_rule_mod_action(struct vcap_rule * rule,enum vcap_action_field action,enum vcap_field_type ftype,struct vcap_client_actionfield_data * data)3483 static int vcap_rule_mod_action(struct vcap_rule *rule,
3484 				enum vcap_action_field action,
3485 				enum vcap_field_type ftype,
3486 				struct vcap_client_actionfield_data *data)
3487 {
3488 	struct vcap_client_actionfield *field;
3489 
3490 	field = vcap_find_actionfield(rule, action);
3491 	if (!field)
3492 		return vcap_rule_add_action(rule, action, ftype, data);
3493 	memcpy(&field->data, data, sizeof(field->data));
3494 	return 0;
3495 }
3496 
3497 /* Modify a 32 bit action field with value in the rule */
vcap_rule_mod_action_u32(struct vcap_rule * rule,enum vcap_action_field action,u32 value)3498 int vcap_rule_mod_action_u32(struct vcap_rule *rule,
3499 			     enum vcap_action_field action,
3500 			     u32 value)
3501 {
3502 	struct vcap_client_actionfield_data data;
3503 
3504 	data.u32.value = value;
3505 	return vcap_rule_mod_action(rule, action, VCAP_FIELD_U32, &data);
3506 }
3507 EXPORT_SYMBOL_GPL(vcap_rule_mod_action_u32);
3508 
3509 /* Drop keys in a keylist and any keys that are not supported by the keyset */
vcap_filter_rule_keys(struct vcap_rule * rule,enum vcap_key_field keylist[],int length,bool drop_unsupported)3510 int vcap_filter_rule_keys(struct vcap_rule *rule,
3511 			  enum vcap_key_field keylist[], int length,
3512 			  bool drop_unsupported)
3513 {
3514 	struct vcap_rule_internal *ri = to_intrule(rule);
3515 	struct vcap_client_keyfield *ckf, *next_ckf;
3516 	const struct vcap_field *fields;
3517 	enum vcap_key_field key;
3518 	int err = 0;
3519 	int idx;
3520 
3521 	if (length > 0) {
3522 		err = -EEXIST;
3523 		list_for_each_entry_safe(ckf, next_ckf,
3524 					 &ri->data.keyfields, ctrl.list) {
3525 			key = ckf->ctrl.key;
3526 			for (idx = 0; idx < length; ++idx)
3527 				if (key == keylist[idx]) {
3528 					list_del(&ckf->ctrl.list);
3529 					kfree(ckf);
3530 					idx++;
3531 					err = 0;
3532 				}
3533 		}
3534 	}
3535 	if (drop_unsupported) {
3536 		err = -EEXIST;
3537 		fields = vcap_keyfields(ri->vctrl, ri->admin->vtype,
3538 					rule->keyset);
3539 		if (!fields)
3540 			return err;
3541 		list_for_each_entry_safe(ckf, next_ckf,
3542 					 &ri->data.keyfields, ctrl.list) {
3543 			key = ckf->ctrl.key;
3544 			if (fields[key].width == 0) {
3545 				list_del(&ckf->ctrl.list);
3546 				kfree(ckf);
3547 				err = 0;
3548 			}
3549 		}
3550 	}
3551 	return err;
3552 }
3553 EXPORT_SYMBOL_GPL(vcap_filter_rule_keys);
3554 
3555 /* Select the keyset from the list that results in the smallest rule size */
3556 enum vcap_keyfield_set
vcap_select_min_rule_keyset(struct vcap_control * vctrl,enum vcap_type vtype,struct vcap_keyset_list * kslist)3557 vcap_select_min_rule_keyset(struct vcap_control *vctrl,
3558 			    enum vcap_type vtype,
3559 			    struct vcap_keyset_list *kslist)
3560 {
3561 	enum vcap_keyfield_set ret = VCAP_KFS_NO_VALUE;
3562 	const struct vcap_set *kset;
3563 	int max = 100, idx;
3564 
3565 	for (idx = 0; idx < kslist->cnt; ++idx) {
3566 		kset = vcap_keyfieldset(vctrl, vtype, kslist->keysets[idx]);
3567 		if (!kset)
3568 			continue;
3569 		if (kset->sw_per_item >= max)
3570 			continue;
3571 		max = kset->sw_per_item;
3572 		ret = kslist->keysets[idx];
3573 	}
3574 	return ret;
3575 }
3576 EXPORT_SYMBOL_GPL(vcap_select_min_rule_keyset);
3577 
3578 /* Make a full copy of an existing rule with a new rule id */
vcap_copy_rule(struct vcap_rule * erule)3579 struct vcap_rule *vcap_copy_rule(struct vcap_rule *erule)
3580 {
3581 	struct vcap_rule_internal *ri = to_intrule(erule);
3582 	struct vcap_client_actionfield *caf;
3583 	struct vcap_client_keyfield *ckf;
3584 	struct vcap_rule *rule;
3585 	int err;
3586 
3587 	err = vcap_api_check(ri->vctrl);
3588 	if (err)
3589 		return ERR_PTR(err);
3590 
3591 	rule = vcap_alloc_rule(ri->vctrl, ri->ndev, ri->data.vcap_chain_id,
3592 			       ri->data.user, ri->data.priority, 0);
3593 	if (IS_ERR(rule))
3594 		return rule;
3595 
3596 	list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list) {
3597 		/* Add a key duplicate in the new rule */
3598 		err = vcap_rule_add_key(rule,
3599 					ckf->ctrl.key,
3600 					ckf->ctrl.type,
3601 					&ckf->data);
3602 		if (err)
3603 			goto err;
3604 	}
3605 
3606 	list_for_each_entry(caf, &ri->data.actionfields, ctrl.list) {
3607 		/* Add a action duplicate in the new rule */
3608 		err = vcap_rule_add_action(rule,
3609 					   caf->ctrl.action,
3610 					   caf->ctrl.type,
3611 					   &caf->data);
3612 		if (err)
3613 			goto err;
3614 	}
3615 	return rule;
3616 err:
3617 	vcap_free_rule(rule);
3618 	return ERR_PTR(err);
3619 }
3620 EXPORT_SYMBOL_GPL(vcap_copy_rule);
3621 
3622 #ifdef CONFIG_VCAP_KUNIT_TEST
3623 #include "vcap_api_kunit.c"
3624 #endif
3625