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