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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 */
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 */
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 */
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 matchin keysets
331  */
332 int
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  */
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 */
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 */
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 *
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 */
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 
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 
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  */
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
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
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 
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 *
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 *
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 *
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 */
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 
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 
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 
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 
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 
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 
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 */
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 */
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 /* Find a rule with a provided rule id */
924 static struct vcap_rule_internal *vcap_lookup_rule(struct vcap_control *vctrl,
925 						   u32 id)
926 {
927 	struct vcap_rule_internal *ri;
928 	struct vcap_admin *admin;
929 
930 	/* Look for the rule id in all vcaps */
931 	list_for_each_entry(admin, &vctrl->list, list)
932 		list_for_each_entry(ri, &admin->rules, list)
933 			if (ri->data.id == id)
934 				return ri;
935 	return NULL;
936 }
937 
938 /* Find a rule id with a provided cookie */
939 int vcap_lookup_rule_by_cookie(struct vcap_control *vctrl, u64 cookie)
940 {
941 	struct vcap_rule_internal *ri;
942 	struct vcap_admin *admin;
943 
944 	/* Look for the rule id in all vcaps */
945 	list_for_each_entry(admin, &vctrl->list, list)
946 		list_for_each_entry(ri, &admin->rules, list)
947 			if (ri->data.cookie == cookie)
948 				return ri->data.id;
949 	return -ENOENT;
950 }
951 EXPORT_SYMBOL_GPL(vcap_lookup_rule_by_cookie);
952 
953 /* Make a copy of the rule, shallow or full */
954 static struct vcap_rule_internal *vcap_dup_rule(struct vcap_rule_internal *ri,
955 						bool full)
956 {
957 	struct vcap_client_actionfield *caf, *newcaf;
958 	struct vcap_client_keyfield *ckf, *newckf;
959 	struct vcap_rule_internal *duprule;
960 
961 	/* Allocate the client part */
962 	duprule = kzalloc(sizeof(*duprule), GFP_KERNEL);
963 	if (!duprule)
964 		return ERR_PTR(-ENOMEM);
965 	*duprule = *ri;
966 	/* Not inserted in the VCAP */
967 	INIT_LIST_HEAD(&duprule->list);
968 	/* No elements in these lists */
969 	INIT_LIST_HEAD(&duprule->data.keyfields);
970 	INIT_LIST_HEAD(&duprule->data.actionfields);
971 
972 	/* A full rule copy includes keys and actions */
973 	if (!full)
974 		return duprule;
975 
976 	list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list) {
977 		newckf = kzalloc(sizeof(*newckf), GFP_KERNEL);
978 		if (!newckf)
979 			return ERR_PTR(-ENOMEM);
980 		memcpy(newckf, ckf, sizeof(*newckf));
981 		list_add_tail(&newckf->ctrl.list, &duprule->data.keyfields);
982 	}
983 
984 	list_for_each_entry(caf, &ri->data.actionfields, ctrl.list) {
985 		newcaf = kzalloc(sizeof(*newcaf), GFP_KERNEL);
986 		if (!newcaf)
987 			return ERR_PTR(-ENOMEM);
988 		memcpy(newcaf, caf, sizeof(*newcaf));
989 		list_add_tail(&newcaf->ctrl.list, &duprule->data.actionfields);
990 	}
991 
992 	return duprule;
993 }
994 
995 static void vcap_apply_width(u8 *dst, int width, int bytes)
996 {
997 	u8 bmask;
998 	int idx;
999 
1000 	for (idx = 0; idx < bytes; idx++) {
1001 		if (width > 0)
1002 			if (width < 8)
1003 				bmask = (1 << width) - 1;
1004 			else
1005 				bmask = ~0;
1006 		else
1007 			bmask = 0;
1008 		dst[idx] &= bmask;
1009 		width -= 8;
1010 	}
1011 }
1012 
1013 static void vcap_copy_from_w32be(u8 *dst, u8 *src, int size, int width)
1014 {
1015 	int idx, ridx, wstart, nidx;
1016 	int tail_bytes = (((size + 4) >> 2) << 2) - size;
1017 
1018 	for (idx = 0, ridx = size - 1; idx < size; ++idx, --ridx) {
1019 		wstart = (idx >> 2) << 2;
1020 		nidx = wstart + 3 - (idx & 0x3);
1021 		if (nidx >= size)
1022 			nidx -= tail_bytes;
1023 		dst[nidx] = src[ridx];
1024 	}
1025 
1026 	vcap_apply_width(dst, width, size);
1027 }
1028 
1029 static void vcap_copy_action_bit_field(struct vcap_u1_action *field, u8 *value)
1030 {
1031 	field->value = (*value) & 0x1;
1032 }
1033 
1034 static void vcap_copy_limited_actionfield(u8 *dstvalue, u8 *srcvalue,
1035 					  int width, int bytes)
1036 {
1037 	memcpy(dstvalue, srcvalue, bytes);
1038 	vcap_apply_width(dstvalue, width, bytes);
1039 }
1040 
1041 static void vcap_copy_to_client_actionfield(struct vcap_rule_internal *ri,
1042 					    struct vcap_client_actionfield *field,
1043 					    u8 *value, u16 width)
1044 {
1045 	int field_size = actionfield_size_table[field->ctrl.type];
1046 
1047 	if (ri->admin->w32be) {
1048 		switch (field->ctrl.type) {
1049 		case VCAP_FIELD_BIT:
1050 			vcap_copy_action_bit_field(&field->data.u1, value);
1051 			break;
1052 		case VCAP_FIELD_U32:
1053 			vcap_copy_limited_actionfield((u8 *)&field->data.u32.value,
1054 						      value,
1055 						      width, field_size);
1056 			break;
1057 		case VCAP_FIELD_U48:
1058 			vcap_copy_from_w32be(field->data.u48.value, value,
1059 					     field_size, width);
1060 			break;
1061 		case VCAP_FIELD_U56:
1062 			vcap_copy_from_w32be(field->data.u56.value, value,
1063 					     field_size, width);
1064 			break;
1065 		case VCAP_FIELD_U64:
1066 			vcap_copy_from_w32be(field->data.u64.value, value,
1067 					     field_size, width);
1068 			break;
1069 		case VCAP_FIELD_U72:
1070 			vcap_copy_from_w32be(field->data.u72.value, value,
1071 					     field_size, width);
1072 			break;
1073 		case VCAP_FIELD_U112:
1074 			vcap_copy_from_w32be(field->data.u112.value, value,
1075 					     field_size, width);
1076 			break;
1077 		case VCAP_FIELD_U128:
1078 			vcap_copy_from_w32be(field->data.u128.value, value,
1079 					     field_size, width);
1080 			break;
1081 		};
1082 	} else {
1083 		switch (field->ctrl.type) {
1084 		case VCAP_FIELD_BIT:
1085 			vcap_copy_action_bit_field(&field->data.u1, value);
1086 			break;
1087 		case VCAP_FIELD_U32:
1088 			vcap_copy_limited_actionfield((u8 *)&field->data.u32.value,
1089 						      value,
1090 						      width, field_size);
1091 			break;
1092 		case VCAP_FIELD_U48:
1093 			vcap_copy_limited_actionfield(field->data.u48.value,
1094 						      value,
1095 						      width, field_size);
1096 			break;
1097 		case VCAP_FIELD_U56:
1098 			vcap_copy_limited_actionfield(field->data.u56.value,
1099 						      value,
1100 						      width, field_size);
1101 			break;
1102 		case VCAP_FIELD_U64:
1103 			vcap_copy_limited_actionfield(field->data.u64.value,
1104 						      value,
1105 						      width, field_size);
1106 			break;
1107 		case VCAP_FIELD_U72:
1108 			vcap_copy_limited_actionfield(field->data.u72.value,
1109 						      value,
1110 						      width, field_size);
1111 			break;
1112 		case VCAP_FIELD_U112:
1113 			vcap_copy_limited_actionfield(field->data.u112.value,
1114 						      value,
1115 						      width, field_size);
1116 			break;
1117 		case VCAP_FIELD_U128:
1118 			vcap_copy_limited_actionfield(field->data.u128.value,
1119 						      value,
1120 						      width, field_size);
1121 			break;
1122 		};
1123 	}
1124 }
1125 
1126 static void vcap_copy_key_bit_field(struct vcap_u1_key *field,
1127 				    u8 *value, u8 *mask)
1128 {
1129 	field->value = (*value) & 0x1;
1130 	field->mask = (*mask) & 0x1;
1131 }
1132 
1133 static void vcap_copy_limited_keyfield(u8 *dstvalue, u8 *dstmask,
1134 				       u8 *srcvalue, u8 *srcmask,
1135 				       int width, int bytes)
1136 {
1137 	memcpy(dstvalue, srcvalue, bytes);
1138 	vcap_apply_width(dstvalue, width, bytes);
1139 	memcpy(dstmask, srcmask, bytes);
1140 	vcap_apply_width(dstmask, width, bytes);
1141 }
1142 
1143 static void vcap_copy_to_client_keyfield(struct vcap_rule_internal *ri,
1144 					 struct vcap_client_keyfield *field,
1145 					 u8 *value, u8 *mask, u16 width)
1146 {
1147 	int field_size = keyfield_size_table[field->ctrl.type] / 2;
1148 
1149 	if (ri->admin->w32be) {
1150 		switch (field->ctrl.type) {
1151 		case VCAP_FIELD_BIT:
1152 			vcap_copy_key_bit_field(&field->data.u1, value, mask);
1153 			break;
1154 		case VCAP_FIELD_U32:
1155 			vcap_copy_limited_keyfield((u8 *)&field->data.u32.value,
1156 						   (u8 *)&field->data.u32.mask,
1157 						   value, mask,
1158 						   width, field_size);
1159 			break;
1160 		case VCAP_FIELD_U48:
1161 			vcap_copy_from_w32be(field->data.u48.value, value,
1162 					     field_size, width);
1163 			vcap_copy_from_w32be(field->data.u48.mask,  mask,
1164 					     field_size, width);
1165 			break;
1166 		case VCAP_FIELD_U56:
1167 			vcap_copy_from_w32be(field->data.u56.value, value,
1168 					     field_size, width);
1169 			vcap_copy_from_w32be(field->data.u56.mask,  mask,
1170 					     field_size, width);
1171 			break;
1172 		case VCAP_FIELD_U64:
1173 			vcap_copy_from_w32be(field->data.u64.value, value,
1174 					     field_size, width);
1175 			vcap_copy_from_w32be(field->data.u64.mask,  mask,
1176 					     field_size, width);
1177 			break;
1178 		case VCAP_FIELD_U72:
1179 			vcap_copy_from_w32be(field->data.u72.value, value,
1180 					     field_size, width);
1181 			vcap_copy_from_w32be(field->data.u72.mask,  mask,
1182 					     field_size, width);
1183 			break;
1184 		case VCAP_FIELD_U112:
1185 			vcap_copy_from_w32be(field->data.u112.value, value,
1186 					     field_size, width);
1187 			vcap_copy_from_w32be(field->data.u112.mask,  mask,
1188 					     field_size, width);
1189 			break;
1190 		case VCAP_FIELD_U128:
1191 			vcap_copy_from_w32be(field->data.u128.value, value,
1192 					     field_size, width);
1193 			vcap_copy_from_w32be(field->data.u128.mask,  mask,
1194 					     field_size, width);
1195 			break;
1196 		};
1197 	} else {
1198 		switch (field->ctrl.type) {
1199 		case VCAP_FIELD_BIT:
1200 			vcap_copy_key_bit_field(&field->data.u1, value, mask);
1201 			break;
1202 		case VCAP_FIELD_U32:
1203 			vcap_copy_limited_keyfield((u8 *)&field->data.u32.value,
1204 						   (u8 *)&field->data.u32.mask,
1205 						   value, mask,
1206 						   width, field_size);
1207 			break;
1208 		case VCAP_FIELD_U48:
1209 			vcap_copy_limited_keyfield(field->data.u48.value,
1210 						   field->data.u48.mask,
1211 						   value, mask,
1212 						   width, field_size);
1213 			break;
1214 		case VCAP_FIELD_U56:
1215 			vcap_copy_limited_keyfield(field->data.u56.value,
1216 						   field->data.u56.mask,
1217 						   value, mask,
1218 						   width, field_size);
1219 			break;
1220 		case VCAP_FIELD_U64:
1221 			vcap_copy_limited_keyfield(field->data.u64.value,
1222 						   field->data.u64.mask,
1223 						   value, mask,
1224 						   width, field_size);
1225 			break;
1226 		case VCAP_FIELD_U72:
1227 			vcap_copy_limited_keyfield(field->data.u72.value,
1228 						   field->data.u72.mask,
1229 						   value, mask,
1230 						   width, field_size);
1231 			break;
1232 		case VCAP_FIELD_U112:
1233 			vcap_copy_limited_keyfield(field->data.u112.value,
1234 						   field->data.u112.mask,
1235 						   value, mask,
1236 						   width, field_size);
1237 			break;
1238 		case VCAP_FIELD_U128:
1239 			vcap_copy_limited_keyfield(field->data.u128.value,
1240 						   field->data.u128.mask,
1241 						   value, mask,
1242 						   width, field_size);
1243 			break;
1244 		};
1245 	}
1246 }
1247 
1248 static void vcap_rule_alloc_keyfield(struct vcap_rule_internal *ri,
1249 				     const struct vcap_field *keyfield,
1250 				     enum vcap_key_field key,
1251 				     u8 *value, u8 *mask)
1252 {
1253 	struct vcap_client_keyfield *field;
1254 
1255 	field = kzalloc(sizeof(*field), GFP_KERNEL);
1256 	if (!field)
1257 		return;
1258 	INIT_LIST_HEAD(&field->ctrl.list);
1259 	field->ctrl.key = key;
1260 	field->ctrl.type = keyfield->type;
1261 	vcap_copy_to_client_keyfield(ri, field, value, mask, keyfield->width);
1262 	list_add_tail(&field->ctrl.list, &ri->data.keyfields);
1263 }
1264 
1265 /* Read key data from a VCAP address and discover if there is a rule keyset
1266  * here
1267  */
1268 static bool
1269 vcap_verify_actionstream_actionset(struct vcap_control *vctrl,
1270 				   enum vcap_type vt,
1271 				   u32 *actionstream,
1272 				   enum vcap_actionfield_set actionset)
1273 {
1274 	const struct vcap_typegroup *tgt;
1275 	const struct vcap_field *fields;
1276 	const struct vcap_set *info;
1277 
1278 	if (vcap_actionfield_count(vctrl, vt, actionset) == 0)
1279 		return false;
1280 
1281 	info = vcap_actionfieldset(vctrl, vt, actionset);
1282 	/* Check that the actionset is valid */
1283 	if (!info)
1284 		return false;
1285 
1286 	/* a type_id of value -1 means that there is no type field */
1287 	if (info->type_id == (u8)-1)
1288 		return true;
1289 
1290 	/* Get a valid typegroup for the specific actionset */
1291 	tgt = vcap_actionfield_typegroup(vctrl, vt, actionset);
1292 	if (!tgt)
1293 		return false;
1294 
1295 	fields = vcap_actionfields(vctrl, vt, actionset);
1296 	if (!fields)
1297 		return false;
1298 
1299 	/* Later this will be expanded with a check of the type id */
1300 	return true;
1301 }
1302 
1303 /* Find the subword width of the action typegroup that matches the stream data
1304  */
1305 static int vcap_find_actionstream_typegroup_sw(struct vcap_control *vctrl,
1306 					       enum vcap_type vt, u32 *stream,
1307 					       int sw_max)
1308 {
1309 	const struct vcap_typegroup **tgt;
1310 	int sw_idx, res;
1311 
1312 	tgt = vctrl->vcaps[vt].actionfield_set_typegroups;
1313 	/* Try the longest subword match first */
1314 	for (sw_idx = vctrl->vcaps[vt].sw_count; sw_idx >= 0; sw_idx--) {
1315 		if (!tgt[sw_idx])
1316 			continue;
1317 		res = vcap_verify_typegroups(stream, vctrl->vcaps[vt].act_width,
1318 					     tgt[sw_idx], false, sw_max);
1319 		if (res == 0)
1320 			return sw_idx;
1321 	}
1322 	return -EINVAL;
1323 }
1324 
1325 /* Verify that the typegroup information, subword count, actionset and type id
1326  * are in sync and correct, return the actionset
1327  */
1328 static enum vcap_actionfield_set
1329 vcap_find_actionstream_actionset(struct vcap_control *vctrl,
1330 				 enum vcap_type vt,
1331 				 u32 *stream,
1332 				 int sw_max)
1333 {
1334 	const struct vcap_set *actionfield_set;
1335 	int sw_count, idx;
1336 	bool res;
1337 
1338 	sw_count = vcap_find_actionstream_typegroup_sw(vctrl, vt, stream,
1339 						       sw_max);
1340 	if (sw_count < 0)
1341 		return sw_count;
1342 
1343 	actionfield_set = vctrl->vcaps[vt].actionfield_set;
1344 	for (idx = 0; idx < vctrl->vcaps[vt].actionfield_set_size; ++idx) {
1345 		if (actionfield_set[idx].sw_per_item != sw_count)
1346 			continue;
1347 
1348 		res = vcap_verify_actionstream_actionset(vctrl, vt,
1349 							 stream, idx);
1350 		if (res)
1351 			return idx;
1352 	}
1353 	return -EINVAL;
1354 }
1355 
1356 /* Store action value in an element in a list for the client */
1357 static void vcap_rule_alloc_actionfield(struct vcap_rule_internal *ri,
1358 					const struct vcap_field *actionfield,
1359 					enum vcap_action_field action,
1360 					u8 *value)
1361 {
1362 	struct vcap_client_actionfield *field;
1363 
1364 	field = kzalloc(sizeof(*field), GFP_KERNEL);
1365 	if (!field)
1366 		return;
1367 	INIT_LIST_HEAD(&field->ctrl.list);
1368 	field->ctrl.action = action;
1369 	field->ctrl.type = actionfield->type;
1370 	vcap_copy_to_client_actionfield(ri, field, value, actionfield->width);
1371 	list_add_tail(&field->ctrl.list, &ri->data.actionfields);
1372 }
1373 
1374 static int vcap_decode_actionset(struct vcap_rule_internal *ri)
1375 {
1376 	struct vcap_control *vctrl = ri->vctrl;
1377 	struct vcap_admin *admin = ri->admin;
1378 	const struct vcap_field *actionfield;
1379 	enum vcap_actionfield_set actionset;
1380 	enum vcap_type vt = admin->vtype;
1381 	const struct vcap_typegroup *tgt;
1382 	struct vcap_stream_iter iter;
1383 	int idx, res, actfield_count;
1384 	u32 *actstream;
1385 	u8 value[16];
1386 
1387 	actstream = admin->cache.actionstream;
1388 	res = vcap_find_actionstream_actionset(vctrl, vt, actstream, 0);
1389 	if (res < 0) {
1390 		pr_err("%s:%d: could not find valid actionset: %d\n",
1391 		       __func__, __LINE__, res);
1392 		return -EINVAL;
1393 	}
1394 	actionset = res;
1395 	actfield_count = vcap_actionfield_count(vctrl, vt, actionset);
1396 	actionfield = vcap_actionfields(vctrl, vt, actionset);
1397 	tgt = vcap_actionfield_typegroup(vctrl, vt, actionset);
1398 	/* Start decoding the stream */
1399 	for (idx = 0; idx < actfield_count; ++idx) {
1400 		if (actionfield[idx].width <= 0)
1401 			continue;
1402 		/* Get the action */
1403 		memset(value, 0, DIV_ROUND_UP(actionfield[idx].width, 8));
1404 		vcap_iter_init(&iter, vctrl->vcaps[vt].act_width, tgt,
1405 			       actionfield[idx].offset);
1406 		vcap_decode_field(actstream, &iter, actionfield[idx].width,
1407 				  value);
1408 		/* Skip if no bits are set */
1409 		if (vcap_bitarray_zero(actionfield[idx].width, value))
1410 			continue;
1411 		vcap_rule_alloc_actionfield(ri, &actionfield[idx], idx, value);
1412 		/* Later the action id will also be checked */
1413 	}
1414 	return vcap_set_rule_set_actionset((struct vcap_rule *)ri, actionset);
1415 }
1416 
1417 static int vcap_decode_keyset(struct vcap_rule_internal *ri)
1418 {
1419 	struct vcap_control *vctrl = ri->vctrl;
1420 	struct vcap_stream_iter kiter, miter;
1421 	struct vcap_admin *admin = ri->admin;
1422 	enum vcap_keyfield_set keysets[10];
1423 	const struct vcap_field *keyfield;
1424 	enum vcap_type vt = admin->vtype;
1425 	const struct vcap_typegroup *tgt;
1426 	struct vcap_keyset_list matches;
1427 	enum vcap_keyfield_set keyset;
1428 	int idx, res, keyfield_count;
1429 	u32 *maskstream;
1430 	u32 *keystream;
1431 	u8 value[16];
1432 	u8 mask[16];
1433 
1434 	keystream = admin->cache.keystream;
1435 	maskstream = admin->cache.maskstream;
1436 	matches.keysets = keysets;
1437 	matches.cnt = 0;
1438 	matches.max = ARRAY_SIZE(keysets);
1439 	res = vcap_find_keystream_keysets(vctrl, vt, keystream, maskstream,
1440 					  false, 0, &matches);
1441 	if (res < 0) {
1442 		pr_err("%s:%d: could not find valid keysets: %d\n",
1443 		       __func__, __LINE__, res);
1444 		return -EINVAL;
1445 	}
1446 	keyset = matches.keysets[0];
1447 	keyfield_count = vcap_keyfield_count(vctrl, vt, keyset);
1448 	keyfield = vcap_keyfields(vctrl, vt, keyset);
1449 	tgt = vcap_keyfield_typegroup(vctrl, vt, keyset);
1450 	/* Start decoding the streams */
1451 	for (idx = 0; idx < keyfield_count; ++idx) {
1452 		if (keyfield[idx].width <= 0)
1453 			continue;
1454 		/* First get the mask */
1455 		memset(mask, 0, DIV_ROUND_UP(keyfield[idx].width, 8));
1456 		vcap_iter_init(&miter, vctrl->vcaps[vt].sw_width, tgt,
1457 			       keyfield[idx].offset);
1458 		vcap_decode_field(maskstream, &miter, keyfield[idx].width,
1459 				  mask);
1460 		/* Skip if no mask bits are set */
1461 		if (vcap_bitarray_zero(keyfield[idx].width, mask))
1462 			continue;
1463 		/* Get the key */
1464 		memset(value, 0, DIV_ROUND_UP(keyfield[idx].width, 8));
1465 		vcap_iter_init(&kiter, vctrl->vcaps[vt].sw_width, tgt,
1466 			       keyfield[idx].offset);
1467 		vcap_decode_field(keystream, &kiter, keyfield[idx].width,
1468 				  value);
1469 		vcap_rule_alloc_keyfield(ri, &keyfield[idx], idx, value, mask);
1470 	}
1471 	return vcap_set_rule_set_keyset((struct vcap_rule *)ri, keyset);
1472 }
1473 
1474 /* Read VCAP content into the VCAP cache */
1475 static int vcap_read_rule(struct vcap_rule_internal *ri)
1476 {
1477 	struct vcap_admin *admin = ri->admin;
1478 	int sw_idx, ent_idx = 0, act_idx = 0;
1479 	u32 addr = ri->addr;
1480 
1481 	if (!ri->size || !ri->keyset_sw_regs || !ri->actionset_sw_regs) {
1482 		pr_err("%s:%d: rule is empty\n", __func__, __LINE__);
1483 		return -EINVAL;
1484 	}
1485 	vcap_erase_cache(ri);
1486 	/* Use the values in the streams to read the VCAP cache */
1487 	for (sw_idx = 0; sw_idx < ri->size; sw_idx++, addr++) {
1488 		ri->vctrl->ops->update(ri->ndev, admin, VCAP_CMD_READ,
1489 				       VCAP_SEL_ALL, addr);
1490 		ri->vctrl->ops->cache_read(ri->ndev, admin,
1491 					   VCAP_SEL_ENTRY, ent_idx,
1492 					   ri->keyset_sw_regs);
1493 		ri->vctrl->ops->cache_read(ri->ndev, admin,
1494 					   VCAP_SEL_ACTION, act_idx,
1495 					   ri->actionset_sw_regs);
1496 		if (sw_idx == 0)
1497 			ri->vctrl->ops->cache_read(ri->ndev, admin,
1498 						   VCAP_SEL_COUNTER,
1499 						   ri->counter_id, 0);
1500 		ent_idx += ri->keyset_sw_regs;
1501 		act_idx += ri->actionset_sw_regs;
1502 	}
1503 	return 0;
1504 }
1505 
1506 /* Write VCAP cache content to the VCAP HW instance */
1507 static int vcap_write_rule(struct vcap_rule_internal *ri)
1508 {
1509 	struct vcap_admin *admin = ri->admin;
1510 	int sw_idx, ent_idx = 0, act_idx = 0;
1511 	u32 addr = ri->addr;
1512 
1513 	if (!ri->size || !ri->keyset_sw_regs || !ri->actionset_sw_regs) {
1514 		pr_err("%s:%d: rule is empty\n", __func__, __LINE__);
1515 		return -EINVAL;
1516 	}
1517 	/* Use the values in the streams to write the VCAP cache */
1518 	for (sw_idx = 0; sw_idx < ri->size; sw_idx++, addr++) {
1519 		ri->vctrl->ops->cache_write(ri->ndev, admin,
1520 					    VCAP_SEL_ENTRY, ent_idx,
1521 					    ri->keyset_sw_regs);
1522 		ri->vctrl->ops->cache_write(ri->ndev, admin,
1523 					    VCAP_SEL_ACTION, act_idx,
1524 					    ri->actionset_sw_regs);
1525 		ri->vctrl->ops->update(ri->ndev, admin, VCAP_CMD_WRITE,
1526 				       VCAP_SEL_ALL, addr);
1527 		ent_idx += ri->keyset_sw_regs;
1528 		act_idx += ri->actionset_sw_regs;
1529 	}
1530 	return 0;
1531 }
1532 
1533 static int vcap_write_counter(struct vcap_rule_internal *ri,
1534 			      struct vcap_counter *ctr)
1535 {
1536 	struct vcap_admin *admin = ri->admin;
1537 
1538 	admin->cache.counter = ctr->value;
1539 	admin->cache.sticky = ctr->sticky;
1540 	ri->vctrl->ops->cache_write(ri->ndev, admin, VCAP_SEL_COUNTER,
1541 				    ri->counter_id, 0);
1542 	ri->vctrl->ops->update(ri->ndev, admin, VCAP_CMD_WRITE,
1543 			       VCAP_SEL_COUNTER, ri->addr);
1544 	return 0;
1545 }
1546 
1547 /* Convert a chain id to a VCAP lookup index */
1548 int vcap_chain_id_to_lookup(struct vcap_admin *admin, int cur_cid)
1549 {
1550 	int lookup_first = admin->vinst * admin->lookups_per_instance;
1551 	int lookup_last = lookup_first + admin->lookups_per_instance;
1552 	int cid_next = admin->first_cid + VCAP_CID_LOOKUP_SIZE;
1553 	int cid = admin->first_cid;
1554 	int lookup;
1555 
1556 	for (lookup = lookup_first; lookup < lookup_last; ++lookup,
1557 	     cid += VCAP_CID_LOOKUP_SIZE, cid_next += VCAP_CID_LOOKUP_SIZE)
1558 		if (cur_cid >= cid && cur_cid < cid_next)
1559 			return lookup;
1560 	return 0;
1561 }
1562 EXPORT_SYMBOL_GPL(vcap_chain_id_to_lookup);
1563 
1564 /* Lookup a vcap instance using chain id */
1565 struct vcap_admin *vcap_find_admin(struct vcap_control *vctrl, int cid)
1566 {
1567 	struct vcap_admin *admin;
1568 
1569 	if (vcap_api_check(vctrl))
1570 		return NULL;
1571 
1572 	list_for_each_entry(admin, &vctrl->list, list) {
1573 		if (cid >= admin->first_cid && cid <= admin->last_cid)
1574 			return admin;
1575 	}
1576 	return NULL;
1577 }
1578 EXPORT_SYMBOL_GPL(vcap_find_admin);
1579 
1580 /* Is the next chain id in one of the following lookups
1581  * For now this does not support filters linked to other filters using
1582  * keys and actions. That will be added later.
1583  */
1584 bool vcap_is_next_lookup(struct vcap_control *vctrl, int src_cid, int dst_cid)
1585 {
1586 	struct vcap_admin *admin;
1587 	int next_cid;
1588 
1589 	if (vcap_api_check(vctrl))
1590 		return false;
1591 
1592 	/* The offset must be at least one lookup, round up */
1593 	next_cid = src_cid + VCAP_CID_LOOKUP_SIZE;
1594 	next_cid /= VCAP_CID_LOOKUP_SIZE;
1595 	next_cid *= VCAP_CID_LOOKUP_SIZE;
1596 
1597 	if (dst_cid < next_cid)
1598 		return false;
1599 
1600 	admin = vcap_find_admin(vctrl, dst_cid);
1601 	if (!admin)
1602 		return false;
1603 
1604 	return true;
1605 }
1606 EXPORT_SYMBOL_GPL(vcap_is_next_lookup);
1607 
1608 /* Check if there is room for a new rule */
1609 static int vcap_rule_space(struct vcap_admin *admin, int size)
1610 {
1611 	if (admin->last_used_addr - size < admin->first_valid_addr) {
1612 		pr_err("%s:%d: No room for rule size: %u, %u\n",
1613 		       __func__, __LINE__, size, admin->first_valid_addr);
1614 		return -ENOSPC;
1615 	}
1616 	return 0;
1617 }
1618 
1619 /* Add the keyset typefield to the list of rule keyfields */
1620 static int vcap_add_type_keyfield(struct vcap_rule *rule)
1621 {
1622 	struct vcap_rule_internal *ri = to_intrule(rule);
1623 	enum vcap_keyfield_set keyset = rule->keyset;
1624 	enum vcap_type vt = ri->admin->vtype;
1625 	const struct vcap_field *fields;
1626 	const struct vcap_set *kset;
1627 	int ret = -EINVAL;
1628 
1629 	kset = vcap_keyfieldset(ri->vctrl, vt, keyset);
1630 	if (!kset)
1631 		return ret;
1632 	if (kset->type_id == (u8)-1)  /* No type field is needed */
1633 		return 0;
1634 
1635 	fields = vcap_keyfields(ri->vctrl, vt, keyset);
1636 	if (!fields)
1637 		return -EINVAL;
1638 	if (fields[VCAP_KF_TYPE].width > 1) {
1639 		ret = vcap_rule_add_key_u32(rule, VCAP_KF_TYPE,
1640 					    kset->type_id, 0xff);
1641 	} else {
1642 		if (kset->type_id)
1643 			ret = vcap_rule_add_key_bit(rule, VCAP_KF_TYPE,
1644 						    VCAP_BIT_1);
1645 		else
1646 			ret = vcap_rule_add_key_bit(rule, VCAP_KF_TYPE,
1647 						    VCAP_BIT_0);
1648 	}
1649 	return 0;
1650 }
1651 
1652 /* Add a keyset to a keyset list */
1653 bool vcap_keyset_list_add(struct vcap_keyset_list *keysetlist,
1654 			  enum vcap_keyfield_set keyset)
1655 {
1656 	int idx;
1657 
1658 	if (keysetlist->cnt < keysetlist->max) {
1659 		/* Avoid duplicates */
1660 		for (idx = 0; idx < keysetlist->cnt; ++idx)
1661 			if (keysetlist->keysets[idx] == keyset)
1662 				return keysetlist->cnt < keysetlist->max;
1663 		keysetlist->keysets[keysetlist->cnt++] = keyset;
1664 	}
1665 	return keysetlist->cnt < keysetlist->max;
1666 }
1667 EXPORT_SYMBOL_GPL(vcap_keyset_list_add);
1668 
1669 /* map keyset id to a string with the keyset name */
1670 const char *vcap_keyset_name(struct vcap_control *vctrl,
1671 			     enum vcap_keyfield_set keyset)
1672 {
1673 	return vctrl->stats->keyfield_set_names[keyset];
1674 }
1675 EXPORT_SYMBOL_GPL(vcap_keyset_name);
1676 
1677 /* map key field id to a string with the key name */
1678 const char *vcap_keyfield_name(struct vcap_control *vctrl,
1679 			       enum vcap_key_field key)
1680 {
1681 	return vctrl->stats->keyfield_names[key];
1682 }
1683 EXPORT_SYMBOL_GPL(vcap_keyfield_name);
1684 
1685 /* map actionset id to a string with the actionset name */
1686 const char *vcap_actionset_name(struct vcap_control *vctrl,
1687 				enum vcap_actionfield_set actionset)
1688 {
1689 	return vctrl->stats->actionfield_set_names[actionset];
1690 }
1691 
1692 /* map action field id to a string with the action name */
1693 const char *vcap_actionfield_name(struct vcap_control *vctrl,
1694 				  enum vcap_action_field action)
1695 {
1696 	return vctrl->stats->actionfield_names[action];
1697 }
1698 
1699 /* Return the keyfield that matches a key in a keyset */
1700 static const struct vcap_field *
1701 vcap_find_keyset_keyfield(struct vcap_control *vctrl,
1702 			  enum vcap_type vtype,
1703 			  enum vcap_keyfield_set keyset,
1704 			  enum vcap_key_field key)
1705 {
1706 	const struct vcap_field *fields;
1707 	int idx, count;
1708 
1709 	fields = vcap_keyfields(vctrl, vtype, keyset);
1710 	if (!fields)
1711 		return NULL;
1712 
1713 	/* Iterate the keyfields of the keyset */
1714 	count = vcap_keyfield_count(vctrl, vtype, keyset);
1715 	for (idx = 0; idx < count; ++idx) {
1716 		if (fields[idx].width == 0)
1717 			continue;
1718 
1719 		if (key == idx)
1720 			return &fields[idx];
1721 	}
1722 
1723 	return NULL;
1724 }
1725 
1726 /* Match a list of keys against the keysets available in a vcap type */
1727 static bool _vcap_rule_find_keysets(struct vcap_rule_internal *ri,
1728 				    struct vcap_keyset_list *matches)
1729 {
1730 	const struct vcap_client_keyfield *ckf;
1731 	int keyset, found, keycount, map_size;
1732 	const struct vcap_field **map;
1733 	enum vcap_type vtype;
1734 
1735 	vtype = ri->admin->vtype;
1736 	map = ri->vctrl->vcaps[vtype].keyfield_set_map;
1737 	map_size = ri->vctrl->vcaps[vtype].keyfield_set_size;
1738 
1739 	/* Get a count of the keyfields we want to match */
1740 	keycount = 0;
1741 	list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list)
1742 		++keycount;
1743 
1744 	matches->cnt = 0;
1745 	/* Iterate the keysets of the VCAP */
1746 	for (keyset = 0; keyset < map_size; ++keyset) {
1747 		if (!map[keyset])
1748 			continue;
1749 
1750 		/* Iterate the keys in the rule */
1751 		found = 0;
1752 		list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list)
1753 			if (vcap_find_keyset_keyfield(ri->vctrl, vtype,
1754 						      keyset, ckf->ctrl.key))
1755 				++found;
1756 
1757 		/* Save the keyset if all keyfields were found */
1758 		if (found == keycount)
1759 			if (!vcap_keyset_list_add(matches, keyset))
1760 				/* bail out when the quota is filled */
1761 				break;
1762 	}
1763 
1764 	return matches->cnt > 0;
1765 }
1766 
1767 /* Match a list of keys against the keysets available in a vcap type */
1768 bool vcap_rule_find_keysets(struct vcap_rule *rule,
1769 			    struct vcap_keyset_list *matches)
1770 {
1771 	struct vcap_rule_internal *ri = to_intrule(rule);
1772 
1773 	return _vcap_rule_find_keysets(ri, matches);
1774 }
1775 EXPORT_SYMBOL_GPL(vcap_rule_find_keysets);
1776 
1777 /* Validate a rule with respect to available port keys */
1778 int vcap_val_rule(struct vcap_rule *rule, u16 l3_proto)
1779 {
1780 	struct vcap_rule_internal *ri = to_intrule(rule);
1781 	struct vcap_keyset_list matches = {};
1782 	enum vcap_keyfield_set keysets[10];
1783 	int ret;
1784 
1785 	ret = vcap_api_check(ri->vctrl);
1786 	if (ret)
1787 		return ret;
1788 	if (!ri->admin) {
1789 		ri->data.exterr = VCAP_ERR_NO_ADMIN;
1790 		return -EINVAL;
1791 	}
1792 	if (!ri->ndev) {
1793 		ri->data.exterr = VCAP_ERR_NO_NETDEV;
1794 		return -EINVAL;
1795 	}
1796 
1797 	matches.keysets = keysets;
1798 	matches.max = ARRAY_SIZE(keysets);
1799 	if (ri->data.keyset == VCAP_KFS_NO_VALUE) {
1800 		/* Iterate over rule keyfields and select keysets that fits */
1801 		if (!_vcap_rule_find_keysets(ri, &matches)) {
1802 			ri->data.exterr = VCAP_ERR_NO_KEYSET_MATCH;
1803 			return -EINVAL;
1804 		}
1805 	} else {
1806 		/* prepare for keyset validation */
1807 		keysets[0] = ri->data.keyset;
1808 		matches.cnt = 1;
1809 	}
1810 
1811 	/* Pick a keyset that is supported in the port lookups */
1812 	ret = ri->vctrl->ops->validate_keyset(ri->ndev, ri->admin, rule,
1813 					      &matches, l3_proto);
1814 	if (ret < 0) {
1815 		pr_err("%s:%d: keyset validation failed: %d\n",
1816 		       __func__, __LINE__, ret);
1817 		ri->data.exterr = VCAP_ERR_NO_PORT_KEYSET_MATCH;
1818 		return ret;
1819 	}
1820 	/* use the keyset that is supported in the port lookups */
1821 	ret = vcap_set_rule_set_keyset(rule, ret);
1822 	if (ret < 0) {
1823 		pr_err("%s:%d: keyset was not updated: %d\n",
1824 		       __func__, __LINE__, ret);
1825 		return ret;
1826 	}
1827 	if (ri->data.actionset == VCAP_AFS_NO_VALUE) {
1828 		/* Later also actionsets will be matched against actions in
1829 		 * the rule, and the type will be set accordingly
1830 		 */
1831 		ri->data.exterr = VCAP_ERR_NO_ACTIONSET_MATCH;
1832 		return -EINVAL;
1833 	}
1834 	vcap_add_type_keyfield(rule);
1835 	/* Add default fields to this rule */
1836 	ri->vctrl->ops->add_default_fields(ri->ndev, ri->admin, rule);
1837 
1838 	/* Rule size is the maximum of the entry and action subword count */
1839 	ri->size = max(ri->keyset_sw, ri->actionset_sw);
1840 
1841 	/* Finally check if there is room for the rule in the VCAP */
1842 	return vcap_rule_space(ri->admin, ri->size);
1843 }
1844 EXPORT_SYMBOL_GPL(vcap_val_rule);
1845 
1846 /* Entries are sorted with increasing values of sort_key.
1847  * I.e. Lowest numerical sort_key is first in list.
1848  * In order to locate largest keys first in list we negate the key size with
1849  * (max_size - size).
1850  */
1851 static u32 vcap_sort_key(u32 max_size, u32 size, u8 user, u16 prio)
1852 {
1853 	return ((max_size - size) << 24) | (user << 16) | prio;
1854 }
1855 
1856 /* calculate the address of the next rule after this (lower address and prio) */
1857 static u32 vcap_next_rule_addr(u32 addr, struct vcap_rule_internal *ri)
1858 {
1859 	return ((addr - ri->size) /  ri->size) * ri->size;
1860 }
1861 
1862 /* Assign a unique rule id and autogenerate one if id == 0 */
1863 static u32 vcap_set_rule_id(struct vcap_rule_internal *ri)
1864 {
1865 	if (ri->data.id != 0)
1866 		return ri->data.id;
1867 
1868 	for (u32 next_id = 1; next_id < ~0; ++next_id) {
1869 		if (!vcap_lookup_rule(ri->vctrl, next_id)) {
1870 			ri->data.id = next_id;
1871 			break;
1872 		}
1873 	}
1874 	return ri->data.id;
1875 }
1876 
1877 static int vcap_insert_rule(struct vcap_rule_internal *ri,
1878 			    struct vcap_rule_move *move)
1879 {
1880 	int sw_count = ri->vctrl->vcaps[ri->admin->vtype].sw_count;
1881 	struct vcap_rule_internal *duprule, *iter, *elem = NULL;
1882 	struct vcap_admin *admin = ri->admin;
1883 	u32 addr;
1884 
1885 	ri->sort_key = vcap_sort_key(sw_count, ri->size, ri->data.user,
1886 				     ri->data.priority);
1887 
1888 	/* Insert the new rule in the list of rule based on the sort key
1889 	 * If the rule needs to be  inserted between existing rules then move
1890 	 * these rules to make room for the new rule and update their start
1891 	 * address.
1892 	 */
1893 	list_for_each_entry(iter, &admin->rules, list) {
1894 		if (ri->sort_key < iter->sort_key) {
1895 			elem = iter;
1896 			break;
1897 		}
1898 	}
1899 
1900 	if (!elem) {
1901 		ri->addr = vcap_next_rule_addr(admin->last_used_addr, ri);
1902 		admin->last_used_addr = ri->addr;
1903 
1904 		/* Add a copy of the rule to the VCAP list */
1905 		duprule = vcap_dup_rule(ri, ri->state == VCAP_RS_DISABLED);
1906 		if (IS_ERR(duprule))
1907 			return PTR_ERR(duprule);
1908 
1909 		list_add_tail(&duprule->list, &admin->rules);
1910 		return 0;
1911 	}
1912 
1913 	/* Reuse the space of the current rule */
1914 	addr = elem->addr + elem->size;
1915 	ri->addr = vcap_next_rule_addr(addr, ri);
1916 	addr = ri->addr;
1917 
1918 	/* Add a copy of the rule to the VCAP list */
1919 	duprule = vcap_dup_rule(ri, ri->state == VCAP_RS_DISABLED);
1920 	if (IS_ERR(duprule))
1921 		return PTR_ERR(duprule);
1922 
1923 	/* Add before the current entry */
1924 	list_add_tail(&duprule->list, &elem->list);
1925 
1926 	/* Update the current rule */
1927 	elem->addr = vcap_next_rule_addr(addr, elem);
1928 	addr = elem->addr;
1929 
1930 	/* Update the address in the remaining rules in the list */
1931 	list_for_each_entry_continue(elem, &admin->rules, list) {
1932 		elem->addr = vcap_next_rule_addr(addr, elem);
1933 		addr = elem->addr;
1934 	}
1935 
1936 	/* Update the move info */
1937 	move->addr = admin->last_used_addr;
1938 	move->count = ri->addr - addr;
1939 	move->offset = admin->last_used_addr - addr;
1940 	admin->last_used_addr = addr;
1941 	return 0;
1942 }
1943 
1944 static void vcap_move_rules(struct vcap_rule_internal *ri,
1945 			    struct vcap_rule_move *move)
1946 {
1947 	ri->vctrl->ops->move(ri->ndev, ri->admin, move->addr,
1948 			 move->offset, move->count);
1949 }
1950 
1951 /* Check if the chain is already used to enable a VCAP lookup for this port */
1952 static bool vcap_is_chain_used(struct vcap_control *vctrl,
1953 			       struct net_device *ndev, int src_cid)
1954 {
1955 	struct vcap_enabled_port *eport;
1956 	struct vcap_admin *admin;
1957 
1958 	list_for_each_entry(admin, &vctrl->list, list)
1959 		list_for_each_entry(eport, &admin->enabled, list)
1960 			if (eport->src_cid == src_cid && eport->ndev == ndev)
1961 				return true;
1962 
1963 	return false;
1964 }
1965 
1966 /* Fetch the next chain in the enabled list for the port */
1967 static int vcap_get_next_chain(struct vcap_control *vctrl,
1968 			       struct net_device *ndev,
1969 			       int dst_cid)
1970 {
1971 	struct vcap_enabled_port *eport;
1972 	struct vcap_admin *admin;
1973 
1974 	list_for_each_entry(admin, &vctrl->list, list) {
1975 		list_for_each_entry(eport, &admin->enabled, list) {
1976 			if (eport->ndev != ndev)
1977 				continue;
1978 			if (eport->src_cid == dst_cid)
1979 				return eport->dst_cid;
1980 		}
1981 	}
1982 
1983 	return 0;
1984 }
1985 
1986 static bool vcap_path_exist(struct vcap_control *vctrl, struct net_device *ndev,
1987 			    int dst_cid)
1988 {
1989 	struct vcap_enabled_port *eport, *elem;
1990 	struct vcap_admin *admin;
1991 	int tmp;
1992 
1993 	/* Find first entry that starts from chain 0*/
1994 	list_for_each_entry(admin, &vctrl->list, list) {
1995 		list_for_each_entry(elem, &admin->enabled, list) {
1996 			if (elem->src_cid == 0 && elem->ndev == ndev) {
1997 				eport = elem;
1998 				break;
1999 			}
2000 		}
2001 		if (eport)
2002 			break;
2003 	}
2004 
2005 	if (!eport)
2006 		return false;
2007 
2008 	tmp = eport->dst_cid;
2009 	while (tmp != dst_cid && tmp != 0)
2010 		tmp = vcap_get_next_chain(vctrl, ndev, tmp);
2011 
2012 	return !!tmp;
2013 }
2014 
2015 /* Internal clients can always store their rules in HW
2016  * External clients can store their rules if the chain is enabled all
2017  * the way from chain 0, otherwise the rule will be cached until
2018  * the chain is enabled.
2019  */
2020 static void vcap_rule_set_state(struct vcap_rule_internal *ri)
2021 {
2022 	if (ri->data.user <= VCAP_USER_QOS)
2023 		ri->state = VCAP_RS_PERMANENT;
2024 	else if (vcap_path_exist(ri->vctrl, ri->ndev, ri->data.vcap_chain_id))
2025 		ri->state = VCAP_RS_ENABLED;
2026 	else
2027 		ri->state = VCAP_RS_DISABLED;
2028 	/* For now always store directly in HW */
2029 	ri->state = VCAP_RS_PERMANENT;
2030 }
2031 
2032 /* Encode and write a validated rule to the VCAP */
2033 int vcap_add_rule(struct vcap_rule *rule)
2034 {
2035 	struct vcap_rule_internal *ri = to_intrule(rule);
2036 	struct vcap_rule_move move = {0};
2037 	struct vcap_counter ctr = {0};
2038 	int ret;
2039 
2040 	ret = vcap_api_check(ri->vctrl);
2041 	if (ret)
2042 		return ret;
2043 	/* Insert the new rule in the list of vcap rules */
2044 	mutex_lock(&ri->admin->lock);
2045 
2046 	vcap_rule_set_state(ri);
2047 	ret = vcap_insert_rule(ri, &move);
2048 	if (ret < 0) {
2049 		pr_err("%s:%d: could not insert rule in vcap list: %d\n",
2050 		       __func__, __LINE__, ret);
2051 		goto out;
2052 	}
2053 	if (move.count > 0)
2054 		vcap_move_rules(ri, &move);
2055 
2056 	if (ri->state == VCAP_RS_DISABLED) {
2057 		/* Erase the rule area */
2058 		ri->vctrl->ops->init(ri->ndev, ri->admin, ri->addr, ri->size);
2059 		goto out;
2060 	}
2061 
2062 	vcap_erase_cache(ri);
2063 	ret = vcap_encode_rule(ri);
2064 	if (ret) {
2065 		pr_err("%s:%d: rule encoding error: %d\n", __func__, __LINE__, ret);
2066 		goto out;
2067 	}
2068 
2069 	ret = vcap_write_rule(ri);
2070 	if (ret) {
2071 		pr_err("%s:%d: rule write error: %d\n", __func__, __LINE__, ret);
2072 		goto out;
2073 	}
2074 	/* Set the counter to zero */
2075 	ret = vcap_write_counter(ri, &ctr);
2076 out:
2077 	mutex_unlock(&ri->admin->lock);
2078 	return ret;
2079 }
2080 EXPORT_SYMBOL_GPL(vcap_add_rule);
2081 
2082 /* Allocate a new rule with the provided arguments */
2083 struct vcap_rule *vcap_alloc_rule(struct vcap_control *vctrl,
2084 				  struct net_device *ndev, int vcap_chain_id,
2085 				  enum vcap_user user, u16 priority,
2086 				  u32 id)
2087 {
2088 	struct vcap_rule_internal *ri;
2089 	struct vcap_admin *admin;
2090 	int err, maxsize;
2091 
2092 	err = vcap_api_check(vctrl);
2093 	if (err)
2094 		return ERR_PTR(err);
2095 	if (!ndev)
2096 		return ERR_PTR(-ENODEV);
2097 	/* Get the VCAP instance */
2098 	admin = vcap_find_admin(vctrl, vcap_chain_id);
2099 	if (!admin)
2100 		return ERR_PTR(-ENOENT);
2101 	/* Sanity check that this VCAP is supported on this platform */
2102 	if (vctrl->vcaps[admin->vtype].rows == 0)
2103 		return ERR_PTR(-EINVAL);
2104 	/* Check if a rule with this id already exists */
2105 	if (vcap_lookup_rule(vctrl, id))
2106 		return ERR_PTR(-EEXIST);
2107 	/* Check if there is room for the rule in the block(s) of the VCAP */
2108 	maxsize = vctrl->vcaps[admin->vtype].sw_count; /* worst case rule size */
2109 	if (vcap_rule_space(admin, maxsize))
2110 		return ERR_PTR(-ENOSPC);
2111 	/* Create a container for the rule and return it */
2112 	ri = kzalloc(sizeof(*ri), GFP_KERNEL);
2113 	if (!ri)
2114 		return ERR_PTR(-ENOMEM);
2115 	ri->data.vcap_chain_id = vcap_chain_id;
2116 	ri->data.user = user;
2117 	ri->data.priority = priority;
2118 	ri->data.id = id;
2119 	ri->data.keyset = VCAP_KFS_NO_VALUE;
2120 	ri->data.actionset = VCAP_AFS_NO_VALUE;
2121 	INIT_LIST_HEAD(&ri->list);
2122 	INIT_LIST_HEAD(&ri->data.keyfields);
2123 	INIT_LIST_HEAD(&ri->data.actionfields);
2124 	ri->ndev = ndev;
2125 	ri->admin = admin; /* refer to the vcap instance */
2126 	ri->vctrl = vctrl; /* refer to the client */
2127 	if (vcap_set_rule_id(ri) == 0)
2128 		goto out_free;
2129 	return (struct vcap_rule *)ri;
2130 
2131 out_free:
2132 	kfree(ri);
2133 	return ERR_PTR(-EINVAL);
2134 }
2135 EXPORT_SYMBOL_GPL(vcap_alloc_rule);
2136 
2137 /* Free mem of a rule owned by client after the rule as been added to the VCAP */
2138 void vcap_free_rule(struct vcap_rule *rule)
2139 {
2140 	struct vcap_rule_internal *ri = to_intrule(rule);
2141 	struct vcap_client_actionfield *caf, *next_caf;
2142 	struct vcap_client_keyfield *ckf, *next_ckf;
2143 
2144 	/* Deallocate the list of keys and actions */
2145 	list_for_each_entry_safe(ckf, next_ckf, &ri->data.keyfields, ctrl.list) {
2146 		list_del(&ckf->ctrl.list);
2147 		kfree(ckf);
2148 	}
2149 	list_for_each_entry_safe(caf, next_caf, &ri->data.actionfields, ctrl.list) {
2150 		list_del(&caf->ctrl.list);
2151 		kfree(caf);
2152 	}
2153 	/* Deallocate the rule */
2154 	kfree(rule);
2155 }
2156 EXPORT_SYMBOL_GPL(vcap_free_rule);
2157 
2158 struct vcap_rule *vcap_get_rule(struct vcap_control *vctrl, u32 id)
2159 {
2160 	struct vcap_rule_internal *elem;
2161 	struct vcap_rule_internal *ri;
2162 	int err;
2163 
2164 	ri = NULL;
2165 
2166 	err = vcap_api_check(vctrl);
2167 	if (err)
2168 		return ERR_PTR(err);
2169 	elem = vcap_lookup_rule(vctrl, id);
2170 	if (!elem)
2171 		return NULL;
2172 	mutex_lock(&elem->admin->lock);
2173 	ri = vcap_dup_rule(elem, elem->state == VCAP_RS_DISABLED);
2174 	if (IS_ERR(ri))
2175 		goto unlock;
2176 
2177 	if (ri->state == VCAP_RS_DISABLED)
2178 		goto unlock;
2179 
2180 	err = vcap_read_rule(ri);
2181 	if (err) {
2182 		ri = ERR_PTR(err);
2183 		goto unlock;
2184 	}
2185 	err = vcap_decode_keyset(ri);
2186 	if (err) {
2187 		ri = ERR_PTR(err);
2188 		goto unlock;
2189 	}
2190 	err = vcap_decode_actionset(ri);
2191 	if (err) {
2192 		ri = ERR_PTR(err);
2193 		goto unlock;
2194 	}
2195 
2196 unlock:
2197 	mutex_unlock(&elem->admin->lock);
2198 	return (struct vcap_rule *)ri;
2199 }
2200 EXPORT_SYMBOL_GPL(vcap_get_rule);
2201 
2202 /* Update existing rule */
2203 int vcap_mod_rule(struct vcap_rule *rule)
2204 {
2205 	struct vcap_rule_internal *ri = to_intrule(rule);
2206 	struct vcap_counter ctr;
2207 	int err;
2208 
2209 	err = vcap_api_check(ri->vctrl);
2210 	if (err)
2211 		return err;
2212 
2213 	if (!vcap_lookup_rule(ri->vctrl, ri->data.id))
2214 		return -ENOENT;
2215 
2216 	mutex_lock(&ri->admin->lock);
2217 
2218 	vcap_rule_set_state(ri);
2219 	if (ri->state == VCAP_RS_DISABLED)
2220 		goto out;
2221 
2222 	/* Encode the bitstreams to the VCAP cache */
2223 	vcap_erase_cache(ri);
2224 	err = vcap_encode_rule(ri);
2225 	if (err)
2226 		goto out;
2227 
2228 	err = vcap_write_rule(ri);
2229 	if (err)
2230 		goto out;
2231 
2232 	memset(&ctr, 0, sizeof(ctr));
2233 	err =  vcap_write_counter(ri, &ctr);
2234 	if (err)
2235 		goto out;
2236 
2237 out:
2238 	mutex_unlock(&ri->admin->lock);
2239 	return err;
2240 }
2241 EXPORT_SYMBOL_GPL(vcap_mod_rule);
2242 
2243 /* Return the alignment offset for a new rule address */
2244 static int vcap_valid_rule_move(struct vcap_rule_internal *el, int offset)
2245 {
2246 	return (el->addr + offset) % el->size;
2247 }
2248 
2249 /* Update the rule address with an offset */
2250 static void vcap_adjust_rule_addr(struct vcap_rule_internal *el, int offset)
2251 {
2252 	el->addr += offset;
2253 }
2254 
2255 /* Rules needs to be moved to fill the gap of the deleted rule */
2256 static int vcap_fill_rule_gap(struct vcap_rule_internal *ri)
2257 {
2258 	struct vcap_admin *admin = ri->admin;
2259 	struct vcap_rule_internal *elem;
2260 	struct vcap_rule_move move;
2261 	int gap = 0, offset = 0;
2262 
2263 	/* If the first rule is deleted: Move other rules to the top */
2264 	if (list_is_first(&ri->list, &admin->rules))
2265 		offset = admin->last_valid_addr + 1 - ri->addr - ri->size;
2266 
2267 	/* Locate gaps between odd size rules and adjust the move */
2268 	elem = ri;
2269 	list_for_each_entry_continue(elem, &admin->rules, list)
2270 		gap += vcap_valid_rule_move(elem, ri->size);
2271 
2272 	/* Update the address in the remaining rules in the list */
2273 	elem = ri;
2274 	list_for_each_entry_continue(elem, &admin->rules, list)
2275 		vcap_adjust_rule_addr(elem, ri->size + gap + offset);
2276 
2277 	/* Update the move info */
2278 	move.addr = admin->last_used_addr;
2279 	move.count = ri->addr - admin->last_used_addr - gap;
2280 	move.offset = -(ri->size + gap + offset);
2281 
2282 	/* Do the actual move operation */
2283 	vcap_move_rules(ri, &move);
2284 
2285 	return gap + offset;
2286 }
2287 
2288 /* Delete rule in a VCAP instance */
2289 int vcap_del_rule(struct vcap_control *vctrl, struct net_device *ndev, u32 id)
2290 {
2291 	struct vcap_rule_internal *ri, *elem;
2292 	struct vcap_admin *admin;
2293 	int gap = 0, err;
2294 
2295 	/* This will later also handle rule moving */
2296 	if (!ndev)
2297 		return -ENODEV;
2298 	err = vcap_api_check(vctrl);
2299 	if (err)
2300 		return err;
2301 	/* Look for the rule id in all vcaps */
2302 	ri = vcap_lookup_rule(vctrl, id);
2303 	if (!ri)
2304 		return -EINVAL;
2305 	admin = ri->admin;
2306 
2307 	if (ri->addr > admin->last_used_addr)
2308 		gap = vcap_fill_rule_gap(ri);
2309 
2310 	/* Delete the rule from the list of rules and the cache */
2311 	mutex_lock(&admin->lock);
2312 	list_del(&ri->list);
2313 	vctrl->ops->init(ndev, admin, admin->last_used_addr, ri->size + gap);
2314 	vcap_free_rule(&ri->data);
2315 	mutex_unlock(&admin->lock);
2316 
2317 	/* Update the last used address, set to default when no rules */
2318 	if (list_empty(&admin->rules)) {
2319 		admin->last_used_addr = admin->last_valid_addr + 1;
2320 	} else {
2321 		elem = list_last_entry(&admin->rules, struct vcap_rule_internal,
2322 				       list);
2323 		admin->last_used_addr = elem->addr;
2324 	}
2325 	return 0;
2326 }
2327 EXPORT_SYMBOL_GPL(vcap_del_rule);
2328 
2329 /* Delete all rules in the VCAP instance */
2330 int vcap_del_rules(struct vcap_control *vctrl, struct vcap_admin *admin)
2331 {
2332 	struct vcap_enabled_port *eport, *next_eport;
2333 	struct vcap_rule_internal *ri, *next_ri;
2334 	int ret = vcap_api_check(vctrl);
2335 
2336 	if (ret)
2337 		return ret;
2338 
2339 	mutex_lock(&admin->lock);
2340 	list_for_each_entry_safe(ri, next_ri, &admin->rules, list) {
2341 		vctrl->ops->init(ri->ndev, admin, ri->addr, ri->size);
2342 		list_del(&ri->list);
2343 		vcap_free_rule(&ri->data);
2344 	}
2345 	admin->last_used_addr = admin->last_valid_addr;
2346 
2347 	/* Remove list of enabled ports */
2348 	list_for_each_entry_safe(eport, next_eport, &admin->enabled, list) {
2349 		list_del(&eport->list);
2350 		kfree(eport);
2351 	}
2352 	mutex_unlock(&admin->lock);
2353 
2354 	return 0;
2355 }
2356 EXPORT_SYMBOL_GPL(vcap_del_rules);
2357 
2358 /* Find a client key field in a rule */
2359 static struct vcap_client_keyfield *
2360 vcap_find_keyfield(struct vcap_rule *rule, enum vcap_key_field key)
2361 {
2362 	struct vcap_rule_internal *ri = to_intrule(rule);
2363 	struct vcap_client_keyfield *ckf;
2364 
2365 	list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list)
2366 		if (ckf->ctrl.key == key)
2367 			return ckf;
2368 	return NULL;
2369 }
2370 
2371 /* Find information on a key field in a rule */
2372 const struct vcap_field *vcap_lookup_keyfield(struct vcap_rule *rule,
2373 					      enum vcap_key_field key)
2374 {
2375 	struct vcap_rule_internal *ri = to_intrule(rule);
2376 	enum vcap_keyfield_set keyset = rule->keyset;
2377 	enum vcap_type vt = ri->admin->vtype;
2378 	const struct vcap_field *fields;
2379 
2380 	if (keyset == VCAP_KFS_NO_VALUE)
2381 		return NULL;
2382 	fields = vcap_keyfields(ri->vctrl, vt, keyset);
2383 	if (!fields)
2384 		return NULL;
2385 	return &fields[key];
2386 }
2387 EXPORT_SYMBOL_GPL(vcap_lookup_keyfield);
2388 
2389 /* Check if the keyfield is already in the rule */
2390 static bool vcap_keyfield_unique(struct vcap_rule *rule,
2391 				 enum vcap_key_field key)
2392 {
2393 	struct vcap_rule_internal *ri = to_intrule(rule);
2394 	const struct vcap_client_keyfield *ckf;
2395 
2396 	list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list)
2397 		if (ckf->ctrl.key == key)
2398 			return false;
2399 	return true;
2400 }
2401 
2402 /* Check if the keyfield is in the keyset */
2403 static bool vcap_keyfield_match_keyset(struct vcap_rule *rule,
2404 				       enum vcap_key_field key)
2405 {
2406 	struct vcap_rule_internal *ri = to_intrule(rule);
2407 	enum vcap_keyfield_set keyset = rule->keyset;
2408 	enum vcap_type vt = ri->admin->vtype;
2409 	const struct vcap_field *fields;
2410 
2411 	/* the field is accepted if the rule has no keyset yet */
2412 	if (keyset == VCAP_KFS_NO_VALUE)
2413 		return true;
2414 	fields = vcap_keyfields(ri->vctrl, vt, keyset);
2415 	if (!fields)
2416 		return false;
2417 	/* if there is a width there is a way */
2418 	return fields[key].width > 0;
2419 }
2420 
2421 static int vcap_rule_add_key(struct vcap_rule *rule,
2422 			     enum vcap_key_field key,
2423 			     enum vcap_field_type ftype,
2424 			     struct vcap_client_keyfield_data *data)
2425 {
2426 	struct vcap_rule_internal *ri = to_intrule(rule);
2427 	struct vcap_client_keyfield *field;
2428 
2429 	if (!vcap_keyfield_unique(rule, key)) {
2430 		pr_warn("%s:%d: keyfield %s is already in the rule\n",
2431 			__func__, __LINE__,
2432 			vcap_keyfield_name(ri->vctrl, key));
2433 		return -EINVAL;
2434 	}
2435 
2436 	if (!vcap_keyfield_match_keyset(rule, key)) {
2437 		pr_err("%s:%d: keyfield %s does not belong in the rule keyset\n",
2438 		       __func__, __LINE__,
2439 		       vcap_keyfield_name(ri->vctrl, key));
2440 		return -EINVAL;
2441 	}
2442 
2443 	field = kzalloc(sizeof(*field), GFP_KERNEL);
2444 	if (!field)
2445 		return -ENOMEM;
2446 	memcpy(&field->data, data, sizeof(field->data));
2447 	field->ctrl.key = key;
2448 	field->ctrl.type = ftype;
2449 	list_add_tail(&field->ctrl.list, &rule->keyfields);
2450 	return 0;
2451 }
2452 
2453 static void vcap_rule_set_key_bitsize(struct vcap_u1_key *u1, enum vcap_bit val)
2454 {
2455 	switch (val) {
2456 	case VCAP_BIT_0:
2457 		u1->value = 0;
2458 		u1->mask = 1;
2459 		break;
2460 	case VCAP_BIT_1:
2461 		u1->value = 1;
2462 		u1->mask = 1;
2463 		break;
2464 	case VCAP_BIT_ANY:
2465 		u1->value = 0;
2466 		u1->mask = 0;
2467 		break;
2468 	}
2469 }
2470 
2471 /* Add a bit key with value and mask to the rule */
2472 int vcap_rule_add_key_bit(struct vcap_rule *rule, enum vcap_key_field key,
2473 			  enum vcap_bit val)
2474 {
2475 	struct vcap_client_keyfield_data data;
2476 
2477 	vcap_rule_set_key_bitsize(&data.u1, val);
2478 	return vcap_rule_add_key(rule, key, VCAP_FIELD_BIT, &data);
2479 }
2480 EXPORT_SYMBOL_GPL(vcap_rule_add_key_bit);
2481 
2482 /* Add a 32 bit key field with value and mask to the rule */
2483 int vcap_rule_add_key_u32(struct vcap_rule *rule, enum vcap_key_field key,
2484 			  u32 value, u32 mask)
2485 {
2486 	struct vcap_client_keyfield_data data;
2487 
2488 	data.u32.value = value;
2489 	data.u32.mask = mask;
2490 	return vcap_rule_add_key(rule, key, VCAP_FIELD_U32, &data);
2491 }
2492 EXPORT_SYMBOL_GPL(vcap_rule_add_key_u32);
2493 
2494 /* Add a 48 bit key with value and mask to the rule */
2495 int vcap_rule_add_key_u48(struct vcap_rule *rule, enum vcap_key_field key,
2496 			  struct vcap_u48_key *fieldval)
2497 {
2498 	struct vcap_client_keyfield_data data;
2499 
2500 	memcpy(&data.u48, fieldval, sizeof(data.u48));
2501 	return vcap_rule_add_key(rule, key, VCAP_FIELD_U48, &data);
2502 }
2503 EXPORT_SYMBOL_GPL(vcap_rule_add_key_u48);
2504 
2505 /* Add a 72 bit key with value and mask to the rule */
2506 int vcap_rule_add_key_u72(struct vcap_rule *rule, enum vcap_key_field key,
2507 			  struct vcap_u72_key *fieldval)
2508 {
2509 	struct vcap_client_keyfield_data data;
2510 
2511 	memcpy(&data.u72, fieldval, sizeof(data.u72));
2512 	return vcap_rule_add_key(rule, key, VCAP_FIELD_U72, &data);
2513 }
2514 EXPORT_SYMBOL_GPL(vcap_rule_add_key_u72);
2515 
2516 /* Add a 128 bit key with value and mask to the rule */
2517 int vcap_rule_add_key_u128(struct vcap_rule *rule, enum vcap_key_field key,
2518 			   struct vcap_u128_key *fieldval)
2519 {
2520 	struct vcap_client_keyfield_data data;
2521 
2522 	memcpy(&data.u128, fieldval, sizeof(data.u128));
2523 	return vcap_rule_add_key(rule, key, VCAP_FIELD_U128, &data);
2524 }
2525 EXPORT_SYMBOL_GPL(vcap_rule_add_key_u128);
2526 
2527 int vcap_rule_get_key_u32(struct vcap_rule *rule, enum vcap_key_field key,
2528 			  u32 *value, u32 *mask)
2529 {
2530 	struct vcap_client_keyfield *ckf;
2531 
2532 	ckf = vcap_find_keyfield(rule, key);
2533 	if (!ckf)
2534 		return -ENOENT;
2535 
2536 	*value = ckf->data.u32.value;
2537 	*mask = ckf->data.u32.mask;
2538 
2539 	return 0;
2540 }
2541 EXPORT_SYMBOL_GPL(vcap_rule_get_key_u32);
2542 
2543 /* Find a client action field in a rule */
2544 static struct vcap_client_actionfield *
2545 vcap_find_actionfield(struct vcap_rule *rule, enum vcap_action_field act)
2546 {
2547 	struct vcap_rule_internal *ri = (struct vcap_rule_internal *)rule;
2548 	struct vcap_client_actionfield *caf;
2549 
2550 	list_for_each_entry(caf, &ri->data.actionfields, ctrl.list)
2551 		if (caf->ctrl.action == act)
2552 			return caf;
2553 	return NULL;
2554 }
2555 
2556 /* Check if the actionfield is already in the rule */
2557 static bool vcap_actionfield_unique(struct vcap_rule *rule,
2558 				    enum vcap_action_field act)
2559 {
2560 	struct vcap_rule_internal *ri = to_intrule(rule);
2561 	const struct vcap_client_actionfield *caf;
2562 
2563 	list_for_each_entry(caf, &ri->data.actionfields, ctrl.list)
2564 		if (caf->ctrl.action == act)
2565 			return false;
2566 	return true;
2567 }
2568 
2569 /* Check if the actionfield is in the actionset */
2570 static bool vcap_actionfield_match_actionset(struct vcap_rule *rule,
2571 					     enum vcap_action_field action)
2572 {
2573 	enum vcap_actionfield_set actionset = rule->actionset;
2574 	struct vcap_rule_internal *ri = to_intrule(rule);
2575 	enum vcap_type vt = ri->admin->vtype;
2576 	const struct vcap_field *fields;
2577 
2578 	/* the field is accepted if the rule has no actionset yet */
2579 	if (actionset == VCAP_AFS_NO_VALUE)
2580 		return true;
2581 	fields = vcap_actionfields(ri->vctrl, vt, actionset);
2582 	if (!fields)
2583 		return false;
2584 	/* if there is a width there is a way */
2585 	return fields[action].width > 0;
2586 }
2587 
2588 static int vcap_rule_add_action(struct vcap_rule *rule,
2589 				enum vcap_action_field action,
2590 				enum vcap_field_type ftype,
2591 				struct vcap_client_actionfield_data *data)
2592 {
2593 	struct vcap_rule_internal *ri = to_intrule(rule);
2594 	struct vcap_client_actionfield *field;
2595 
2596 	if (!vcap_actionfield_unique(rule, action)) {
2597 		pr_warn("%s:%d: actionfield %s is already in the rule\n",
2598 			__func__, __LINE__,
2599 			vcap_actionfield_name(ri->vctrl, action));
2600 		return -EINVAL;
2601 	}
2602 
2603 	if (!vcap_actionfield_match_actionset(rule, action)) {
2604 		pr_err("%s:%d: actionfield %s does not belong in the rule actionset\n",
2605 		       __func__, __LINE__,
2606 		       vcap_actionfield_name(ri->vctrl, action));
2607 		return -EINVAL;
2608 	}
2609 
2610 	field = kzalloc(sizeof(*field), GFP_KERNEL);
2611 	if (!field)
2612 		return -ENOMEM;
2613 	memcpy(&field->data, data, sizeof(field->data));
2614 	field->ctrl.action = action;
2615 	field->ctrl.type = ftype;
2616 	list_add_tail(&field->ctrl.list, &rule->actionfields);
2617 	return 0;
2618 }
2619 
2620 static void vcap_rule_set_action_bitsize(struct vcap_u1_action *u1,
2621 					 enum vcap_bit val)
2622 {
2623 	switch (val) {
2624 	case VCAP_BIT_0:
2625 		u1->value = 0;
2626 		break;
2627 	case VCAP_BIT_1:
2628 		u1->value = 1;
2629 		break;
2630 	case VCAP_BIT_ANY:
2631 		u1->value = 0;
2632 		break;
2633 	}
2634 }
2635 
2636 /* Add a bit action with value to the rule */
2637 int vcap_rule_add_action_bit(struct vcap_rule *rule,
2638 			     enum vcap_action_field action,
2639 			     enum vcap_bit val)
2640 {
2641 	struct vcap_client_actionfield_data data;
2642 
2643 	vcap_rule_set_action_bitsize(&data.u1, val);
2644 	return vcap_rule_add_action(rule, action, VCAP_FIELD_BIT, &data);
2645 }
2646 EXPORT_SYMBOL_GPL(vcap_rule_add_action_bit);
2647 
2648 /* Add a 32 bit action field with value to the rule */
2649 int vcap_rule_add_action_u32(struct vcap_rule *rule,
2650 			     enum vcap_action_field action,
2651 			     u32 value)
2652 {
2653 	struct vcap_client_actionfield_data data;
2654 
2655 	data.u32.value = value;
2656 	return vcap_rule_add_action(rule, action, VCAP_FIELD_U32, &data);
2657 }
2658 EXPORT_SYMBOL_GPL(vcap_rule_add_action_u32);
2659 
2660 static int vcap_read_counter(struct vcap_rule_internal *ri,
2661 			     struct vcap_counter *ctr)
2662 {
2663 	struct vcap_admin *admin = ri->admin;
2664 
2665 	ri->vctrl->ops->update(ri->ndev, admin, VCAP_CMD_READ, VCAP_SEL_COUNTER,
2666 			       ri->addr);
2667 	ri->vctrl->ops->cache_read(ri->ndev, admin, VCAP_SEL_COUNTER,
2668 				   ri->counter_id, 0);
2669 	ctr->value = admin->cache.counter;
2670 	ctr->sticky = admin->cache.sticky;
2671 	return 0;
2672 }
2673 
2674 /* Copy to host byte order */
2675 void vcap_netbytes_copy(u8 *dst, u8 *src, int count)
2676 {
2677 	int idx;
2678 
2679 	for (idx = 0; idx < count; ++idx, ++dst)
2680 		*dst = src[count - idx - 1];
2681 }
2682 EXPORT_SYMBOL_GPL(vcap_netbytes_copy);
2683 
2684 /* Convert validation error code into tc extact error message */
2685 void vcap_set_tc_exterr(struct flow_cls_offload *fco, struct vcap_rule *vrule)
2686 {
2687 	switch (vrule->exterr) {
2688 	case VCAP_ERR_NONE:
2689 		break;
2690 	case VCAP_ERR_NO_ADMIN:
2691 		NL_SET_ERR_MSG_MOD(fco->common.extack,
2692 				   "Missing VCAP instance");
2693 		break;
2694 	case VCAP_ERR_NO_NETDEV:
2695 		NL_SET_ERR_MSG_MOD(fco->common.extack,
2696 				   "Missing network interface");
2697 		break;
2698 	case VCAP_ERR_NO_KEYSET_MATCH:
2699 		NL_SET_ERR_MSG_MOD(fco->common.extack,
2700 				   "No keyset matched the filter keys");
2701 		break;
2702 	case VCAP_ERR_NO_ACTIONSET_MATCH:
2703 		NL_SET_ERR_MSG_MOD(fco->common.extack,
2704 				   "No actionset matched the filter actions");
2705 		break;
2706 	case VCAP_ERR_NO_PORT_KEYSET_MATCH:
2707 		NL_SET_ERR_MSG_MOD(fco->common.extack,
2708 				   "No port keyset matched the filter keys");
2709 		break;
2710 	}
2711 }
2712 EXPORT_SYMBOL_GPL(vcap_set_tc_exterr);
2713 
2714 /* Check if this port is already enabled for this VCAP instance */
2715 static bool vcap_is_enabled(struct vcap_control *vctrl, struct net_device *ndev,
2716 			    int dst_cid)
2717 {
2718 	struct vcap_enabled_port *eport;
2719 	struct vcap_admin *admin;
2720 
2721 	list_for_each_entry(admin, &vctrl->list, list)
2722 		list_for_each_entry(eport, &admin->enabled, list)
2723 			if (eport->dst_cid == dst_cid && eport->ndev == ndev)
2724 				return true;
2725 
2726 	return false;
2727 }
2728 
2729 /* Enable this port and chain id in a VCAP instance */
2730 static int vcap_enable(struct vcap_control *vctrl, struct net_device *ndev,
2731 		       unsigned long cookie, int src_cid, int dst_cid)
2732 {
2733 	struct vcap_enabled_port *eport;
2734 	struct vcap_admin *admin;
2735 
2736 	if (src_cid >= dst_cid)
2737 		return -EFAULT;
2738 
2739 	admin = vcap_find_admin(vctrl, dst_cid);
2740 	if (!admin)
2741 		return -ENOENT;
2742 
2743 	eport = kzalloc(sizeof(*eport), GFP_KERNEL);
2744 	if (!eport)
2745 		return -ENOMEM;
2746 
2747 	eport->ndev = ndev;
2748 	eport->cookie = cookie;
2749 	eport->src_cid = src_cid;
2750 	eport->dst_cid = dst_cid;
2751 	mutex_lock(&admin->lock);
2752 	list_add_tail(&eport->list, &admin->enabled);
2753 	mutex_unlock(&admin->lock);
2754 
2755 	return 0;
2756 }
2757 
2758 /* Disable this port and chain id for a VCAP instance */
2759 static int vcap_disable(struct vcap_control *vctrl, struct net_device *ndev,
2760 			unsigned long cookie)
2761 {
2762 	struct vcap_enabled_port *elem, *eport = NULL;
2763 	struct vcap_admin *found = NULL, *admin;
2764 
2765 	list_for_each_entry(admin, &vctrl->list, list) {
2766 		list_for_each_entry(elem, &admin->enabled, list) {
2767 			if (elem->cookie == cookie && elem->ndev == ndev) {
2768 				eport = elem;
2769 				found = admin;
2770 				break;
2771 			}
2772 		}
2773 		if (eport)
2774 			break;
2775 	}
2776 
2777 	if (!eport)
2778 		return -ENOENT;
2779 
2780 	mutex_lock(&found->lock);
2781 	list_del(&eport->list);
2782 	mutex_unlock(&found->lock);
2783 	kfree(eport);
2784 	return 0;
2785 }
2786 
2787 /* Enable/Disable the VCAP instance lookups */
2788 int vcap_enable_lookups(struct vcap_control *vctrl, struct net_device *ndev,
2789 			int src_cid, int dst_cid, unsigned long cookie,
2790 			bool enable)
2791 {
2792 	int err;
2793 
2794 	err = vcap_api_check(vctrl);
2795 	if (err)
2796 		return err;
2797 
2798 	if (!ndev)
2799 		return -ENODEV;
2800 
2801 	/* Source and destination must be the first chain in a lookup */
2802 	if (src_cid % VCAP_CID_LOOKUP_SIZE)
2803 		return -EFAULT;
2804 	if (dst_cid % VCAP_CID_LOOKUP_SIZE)
2805 		return -EFAULT;
2806 
2807 	if (enable) {
2808 		if (vcap_is_enabled(vctrl, ndev, dst_cid))
2809 			return -EADDRINUSE;
2810 		if (vcap_is_chain_used(vctrl, ndev, src_cid))
2811 			return -EADDRNOTAVAIL;
2812 		err = vcap_enable(vctrl, ndev, cookie, src_cid, dst_cid);
2813 	} else {
2814 		err = vcap_disable(vctrl, ndev, cookie);
2815 	}
2816 
2817 	return err;
2818 }
2819 EXPORT_SYMBOL_GPL(vcap_enable_lookups);
2820 
2821 /* Is this chain id the last lookup of all VCAPs */
2822 bool vcap_is_last_chain(struct vcap_control *vctrl, int cid)
2823 {
2824 	struct vcap_admin *admin;
2825 	int lookup;
2826 
2827 	if (vcap_api_check(vctrl))
2828 		return false;
2829 
2830 	admin = vcap_find_admin(vctrl, cid);
2831 	if (!admin)
2832 		return false;
2833 
2834 	/* This must be the last lookup in this VCAP type */
2835 	lookup = vcap_chain_id_to_lookup(admin, cid);
2836 	return lookup == admin->lookups - 1;
2837 }
2838 EXPORT_SYMBOL_GPL(vcap_is_last_chain);
2839 
2840 /* Set a rule counter id (for certain vcaps only) */
2841 void vcap_rule_set_counter_id(struct vcap_rule *rule, u32 counter_id)
2842 {
2843 	struct vcap_rule_internal *ri = to_intrule(rule);
2844 
2845 	ri->counter_id = counter_id;
2846 }
2847 EXPORT_SYMBOL_GPL(vcap_rule_set_counter_id);
2848 
2849 /* Provide all rules via a callback interface */
2850 int vcap_rule_iter(struct vcap_control *vctrl,
2851 		   int (*callback)(void *, struct vcap_rule *), void *arg)
2852 {
2853 	struct vcap_rule_internal *ri;
2854 	struct vcap_admin *admin;
2855 	int ret;
2856 
2857 	ret = vcap_api_check(vctrl);
2858 	if (ret)
2859 		return ret;
2860 
2861 	/* Iterate all rules in each VCAP instance */
2862 	list_for_each_entry(admin, &vctrl->list, list) {
2863 		list_for_each_entry(ri, &admin->rules, list) {
2864 			ret = callback(arg, &ri->data);
2865 			if (ret)
2866 				return ret;
2867 		}
2868 	}
2869 
2870 	return 0;
2871 }
2872 EXPORT_SYMBOL_GPL(vcap_rule_iter);
2873 
2874 int vcap_rule_set_counter(struct vcap_rule *rule, struct vcap_counter *ctr)
2875 {
2876 	struct vcap_rule_internal *ri = to_intrule(rule);
2877 	int err;
2878 
2879 	err = vcap_api_check(ri->vctrl);
2880 	if (err)
2881 		return err;
2882 	if (!ctr) {
2883 		pr_err("%s:%d: counter is missing\n", __func__, __LINE__);
2884 		return -EINVAL;
2885 	}
2886 	return vcap_write_counter(ri, ctr);
2887 }
2888 EXPORT_SYMBOL_GPL(vcap_rule_set_counter);
2889 
2890 int vcap_rule_get_counter(struct vcap_rule *rule, struct vcap_counter *ctr)
2891 {
2892 	struct vcap_rule_internal *ri = to_intrule(rule);
2893 	int err;
2894 
2895 	err = vcap_api_check(ri->vctrl);
2896 	if (err)
2897 		return err;
2898 	if (!ctr) {
2899 		pr_err("%s:%d: counter is missing\n", __func__, __LINE__);
2900 		return -EINVAL;
2901 	}
2902 	return vcap_read_counter(ri, ctr);
2903 }
2904 EXPORT_SYMBOL_GPL(vcap_rule_get_counter);
2905 
2906 static int vcap_rule_mod_key(struct vcap_rule *rule,
2907 			     enum vcap_key_field key,
2908 			     enum vcap_field_type ftype,
2909 			     struct vcap_client_keyfield_data *data)
2910 {
2911 	struct vcap_client_keyfield *field;
2912 
2913 	field = vcap_find_keyfield(rule, key);
2914 	if (!field)
2915 		return vcap_rule_add_key(rule, key, ftype, data);
2916 	memcpy(&field->data, data, sizeof(field->data));
2917 	return 0;
2918 }
2919 
2920 /* Modify a 32 bit key field with value and mask in the rule */
2921 int vcap_rule_mod_key_u32(struct vcap_rule *rule, enum vcap_key_field key,
2922 			  u32 value, u32 mask)
2923 {
2924 	struct vcap_client_keyfield_data data;
2925 
2926 	data.u32.value = value;
2927 	data.u32.mask = mask;
2928 	return vcap_rule_mod_key(rule, key, VCAP_FIELD_U32, &data);
2929 }
2930 EXPORT_SYMBOL_GPL(vcap_rule_mod_key_u32);
2931 
2932 static int vcap_rule_mod_action(struct vcap_rule *rule,
2933 				enum vcap_action_field action,
2934 				enum vcap_field_type ftype,
2935 				struct vcap_client_actionfield_data *data)
2936 {
2937 	struct vcap_client_actionfield *field;
2938 
2939 	field = vcap_find_actionfield(rule, action);
2940 	if (!field)
2941 		return vcap_rule_add_action(rule, action, ftype, data);
2942 	memcpy(&field->data, data, sizeof(field->data));
2943 	return 0;
2944 }
2945 
2946 /* Modify a 32 bit action field with value in the rule */
2947 int vcap_rule_mod_action_u32(struct vcap_rule *rule,
2948 			     enum vcap_action_field action,
2949 			     u32 value)
2950 {
2951 	struct vcap_client_actionfield_data data;
2952 
2953 	data.u32.value = value;
2954 	return vcap_rule_mod_action(rule, action, VCAP_FIELD_U32, &data);
2955 }
2956 EXPORT_SYMBOL_GPL(vcap_rule_mod_action_u32);
2957 
2958 /* Drop keys in a keylist and any keys that are not supported by the keyset */
2959 int vcap_filter_rule_keys(struct vcap_rule *rule,
2960 			  enum vcap_key_field keylist[], int length,
2961 			  bool drop_unsupported)
2962 {
2963 	struct vcap_rule_internal *ri = to_intrule(rule);
2964 	struct vcap_client_keyfield *ckf, *next_ckf;
2965 	const struct vcap_field *fields;
2966 	enum vcap_key_field key;
2967 	int err = 0;
2968 	int idx;
2969 
2970 	if (length > 0) {
2971 		err = -EEXIST;
2972 		list_for_each_entry_safe(ckf, next_ckf,
2973 					 &ri->data.keyfields, ctrl.list) {
2974 			key = ckf->ctrl.key;
2975 			for (idx = 0; idx < length; ++idx)
2976 				if (key == keylist[idx]) {
2977 					list_del(&ckf->ctrl.list);
2978 					kfree(ckf);
2979 					idx++;
2980 					err = 0;
2981 				}
2982 		}
2983 	}
2984 	if (drop_unsupported) {
2985 		err = -EEXIST;
2986 		fields = vcap_keyfields(ri->vctrl, ri->admin->vtype,
2987 					rule->keyset);
2988 		if (!fields)
2989 			return err;
2990 		list_for_each_entry_safe(ckf, next_ckf,
2991 					 &ri->data.keyfields, ctrl.list) {
2992 			key = ckf->ctrl.key;
2993 			if (fields[key].width == 0) {
2994 				list_del(&ckf->ctrl.list);
2995 				kfree(ckf);
2996 				err = 0;
2997 			}
2998 		}
2999 	}
3000 	return err;
3001 }
3002 EXPORT_SYMBOL_GPL(vcap_filter_rule_keys);
3003 
3004 /* Make a full copy of an existing rule with a new rule id */
3005 struct vcap_rule *vcap_copy_rule(struct vcap_rule *erule)
3006 {
3007 	struct vcap_rule_internal *ri = to_intrule(erule);
3008 	struct vcap_client_actionfield *caf;
3009 	struct vcap_client_keyfield *ckf;
3010 	struct vcap_rule *rule;
3011 	int err;
3012 
3013 	err = vcap_api_check(ri->vctrl);
3014 	if (err)
3015 		return ERR_PTR(err);
3016 
3017 	rule = vcap_alloc_rule(ri->vctrl, ri->ndev, ri->data.vcap_chain_id,
3018 			       ri->data.user, ri->data.priority, 0);
3019 	if (IS_ERR(rule))
3020 		return rule;
3021 
3022 	list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list) {
3023 		/* Add a key duplicate in the new rule */
3024 		err = vcap_rule_add_key(rule,
3025 					ckf->ctrl.key,
3026 					ckf->ctrl.type,
3027 					&ckf->data);
3028 		if (err)
3029 			goto err;
3030 	}
3031 
3032 	list_for_each_entry(caf, &ri->data.actionfields, ctrl.list) {
3033 		/* Add a action duplicate in the new rule */
3034 		err = vcap_rule_add_action(rule,
3035 					   caf->ctrl.action,
3036 					   caf->ctrl.type,
3037 					   &caf->data);
3038 		if (err)
3039 			goto err;
3040 	}
3041 	return rule;
3042 err:
3043 	vcap_free_rule(rule);
3044 	return ERR_PTR(err);
3045 }
3046 EXPORT_SYMBOL_GPL(vcap_copy_rule);
3047 
3048 #ifdef CONFIG_VCAP_KUNIT_TEST
3049 #include "vcap_api_kunit.c"
3050 #endif
3051