xref: /openbmc/linux/drivers/net/ethernet/microchip/vcap/vcap_api.c (revision f2bb566f5c977ff010baaa9e5e14d9a75b06e5f2)
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 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 };
46 
47 void vcap_iter_set(struct vcap_stream_iter *itr, int sw_width,
48 		   const struct vcap_typegroup *tg, u32 offset)
49 {
50 	memset(itr, 0, sizeof(*itr));
51 	itr->offset = offset;
52 	itr->sw_width = sw_width;
53 	itr->regs_per_sw = DIV_ROUND_UP(sw_width, 32);
54 	itr->tg = tg;
55 }
56 
57 static void vcap_iter_skip_tg(struct vcap_stream_iter *itr)
58 {
59 	/* Compensate the field offset for preceding typegroups.
60 	 * A typegroup table ends with an all-zero terminator.
61 	 */
62 	while (itr->tg->width && itr->offset >= itr->tg->offset) {
63 		itr->offset += itr->tg->width;
64 		itr->tg++; /* next typegroup */
65 	}
66 }
67 
68 void vcap_iter_update(struct vcap_stream_iter *itr)
69 {
70 	int sw_idx, sw_bitpos;
71 
72 	/* Calculate the subword index and bitposition for current bit */
73 	sw_idx = itr->offset / itr->sw_width;
74 	sw_bitpos = itr->offset % itr->sw_width;
75 	/* Calculate the register index and bitposition for current bit */
76 	itr->reg_idx = (sw_idx * itr->regs_per_sw) + (sw_bitpos / 32);
77 	itr->reg_bitpos = sw_bitpos % 32;
78 }
79 
80 void vcap_iter_init(struct vcap_stream_iter *itr, int sw_width,
81 		    const struct vcap_typegroup *tg, u32 offset)
82 {
83 	vcap_iter_set(itr, sw_width, tg, offset);
84 	vcap_iter_skip_tg(itr);
85 	vcap_iter_update(itr);
86 }
87 
88 void vcap_iter_next(struct vcap_stream_iter *itr)
89 {
90 	itr->offset++;
91 	vcap_iter_skip_tg(itr);
92 	vcap_iter_update(itr);
93 }
94 
95 static void vcap_set_bit(u32 *stream, struct vcap_stream_iter *itr, bool value)
96 {
97 	u32 mask = BIT(itr->reg_bitpos);
98 	u32 *p = &stream[itr->reg_idx];
99 
100 	if (value)
101 		*p |= mask;
102 	else
103 		*p &= ~mask;
104 }
105 
106 static void vcap_encode_bit(u32 *stream, struct vcap_stream_iter *itr, bool val)
107 {
108 	/* When intersected by a type group field, stream the type group bits
109 	 * before continuing with the value bit
110 	 */
111 	while (itr->tg->width &&
112 	       itr->offset >= itr->tg->offset &&
113 	       itr->offset < itr->tg->offset + itr->tg->width) {
114 		int tg_bitpos = itr->tg->offset - itr->offset;
115 
116 		vcap_set_bit(stream, itr, (itr->tg->value >> tg_bitpos) & 0x1);
117 		itr->offset++;
118 		vcap_iter_update(itr);
119 	}
120 	vcap_set_bit(stream, itr, val);
121 }
122 
123 static void vcap_encode_field(u32 *stream, struct vcap_stream_iter *itr,
124 			      int width, const u8 *value)
125 {
126 	int idx;
127 
128 	/* Loop over the field value bits and add the value bits one by one to
129 	 * the output stream.
130 	 */
131 	for (idx = 0; idx < width; idx++) {
132 		u8 bidx = idx & GENMASK(2, 0);
133 
134 		/* Encode one field value bit */
135 		vcap_encode_bit(stream, itr, (value[idx / 8] >> bidx) & 0x1);
136 		vcap_iter_next(itr);
137 	}
138 }
139 
140 static void vcap_encode_typegroups(u32 *stream, int sw_width,
141 				   const struct vcap_typegroup *tg,
142 				   bool mask)
143 {
144 	struct vcap_stream_iter iter;
145 	int idx;
146 
147 	/* Mask bits must be set to zeros (inverted later when writing to the
148 	 * mask cache register), so that the mask typegroup bits consist of
149 	 * match-1 or match-0, or both
150 	 */
151 	vcap_iter_set(&iter, sw_width, tg, 0);
152 	while (iter.tg->width) {
153 		/* Set position to current typegroup bit */
154 		iter.offset = iter.tg->offset;
155 		vcap_iter_update(&iter);
156 		for (idx = 0; idx < iter.tg->width; idx++) {
157 			/* Iterate over current typegroup bits. Mask typegroup
158 			 * bits are always set
159 			 */
160 			if (mask)
161 				vcap_set_bit(stream, &iter, 0x1);
162 			else
163 				vcap_set_bit(stream, &iter,
164 					     (iter.tg->value >> idx) & 0x1);
165 			iter.offset++;
166 			vcap_iter_update(&iter);
167 		}
168 		iter.tg++; /* next typegroup */
169 	}
170 }
171 
172 /* Return the list of keyfields for the keyset */
173 const struct vcap_field *vcap_keyfields(struct vcap_control *vctrl,
174 					enum vcap_type vt,
175 					enum vcap_keyfield_set keyset)
176 {
177 	/* Check that the keyset exists in the vcap keyset list */
178 	if (keyset >= vctrl->vcaps[vt].keyfield_set_size)
179 		return NULL;
180 	return vctrl->vcaps[vt].keyfield_set_map[keyset];
181 }
182 
183 /* Return the keyset information for the keyset */
184 const struct vcap_set *vcap_keyfieldset(struct vcap_control *vctrl,
185 					enum vcap_type vt,
186 					enum vcap_keyfield_set keyset)
187 {
188 	const struct vcap_set *kset;
189 
190 	/* Check that the keyset exists in the vcap keyset list */
191 	if (keyset >= vctrl->vcaps[vt].keyfield_set_size)
192 		return NULL;
193 	kset = &vctrl->vcaps[vt].keyfield_set[keyset];
194 	if (kset->sw_per_item == 0 || kset->sw_per_item > vctrl->vcaps[vt].sw_count)
195 		return NULL;
196 	return kset;
197 }
198 EXPORT_SYMBOL_GPL(vcap_keyfieldset);
199 
200 /* Return the typegroup table for the matching keyset (using subword size) */
201 const struct vcap_typegroup *
202 vcap_keyfield_typegroup(struct vcap_control *vctrl,
203 			enum vcap_type vt, enum vcap_keyfield_set keyset)
204 {
205 	const struct vcap_set *kset = vcap_keyfieldset(vctrl, vt, keyset);
206 
207 	/* Check that the keyset is valid */
208 	if (!kset)
209 		return NULL;
210 	return vctrl->vcaps[vt].keyfield_set_typegroups[kset->sw_per_item];
211 }
212 
213 /* Return the number of keyfields in the keyset */
214 int vcap_keyfield_count(struct vcap_control *vctrl,
215 			enum vcap_type vt, enum vcap_keyfield_set keyset)
216 {
217 	/* Check that the keyset exists in the vcap keyset list */
218 	if (keyset >= vctrl->vcaps[vt].keyfield_set_size)
219 		return 0;
220 	return vctrl->vcaps[vt].keyfield_set_map_size[keyset];
221 }
222 
223 static void vcap_encode_keyfield(struct vcap_rule_internal *ri,
224 				 const struct vcap_client_keyfield *kf,
225 				 const struct vcap_field *rf,
226 				 const struct vcap_typegroup *tgt)
227 {
228 	int sw_width = ri->vctrl->vcaps[ri->admin->vtype].sw_width;
229 	struct vcap_cache_data *cache = &ri->admin->cache;
230 	struct vcap_stream_iter iter;
231 	const u8 *value, *mask;
232 
233 	/* Encode the fields for the key and the mask in their respective
234 	 * streams, respecting the subword width.
235 	 */
236 	switch (kf->ctrl.type) {
237 	case VCAP_FIELD_BIT:
238 		value = &kf->data.u1.value;
239 		mask = &kf->data.u1.mask;
240 		break;
241 	case VCAP_FIELD_U32:
242 		value = (const u8 *)&kf->data.u32.value;
243 		mask = (const u8 *)&kf->data.u32.mask;
244 		break;
245 	case VCAP_FIELD_U48:
246 		value = kf->data.u48.value;
247 		mask = kf->data.u48.mask;
248 		break;
249 	case VCAP_FIELD_U56:
250 		value = kf->data.u56.value;
251 		mask = kf->data.u56.mask;
252 		break;
253 	case VCAP_FIELD_U64:
254 		value = kf->data.u64.value;
255 		mask = kf->data.u64.mask;
256 		break;
257 	case VCAP_FIELD_U72:
258 		value = kf->data.u72.value;
259 		mask = kf->data.u72.mask;
260 		break;
261 	case VCAP_FIELD_U112:
262 		value = kf->data.u112.value;
263 		mask = kf->data.u112.mask;
264 		break;
265 	case VCAP_FIELD_U128:
266 		value = kf->data.u128.value;
267 		mask = kf->data.u128.mask;
268 		break;
269 	}
270 	vcap_iter_init(&iter, sw_width, tgt, rf->offset);
271 	vcap_encode_field(cache->keystream, &iter, rf->width, value);
272 	vcap_iter_init(&iter, sw_width, tgt, rf->offset);
273 	vcap_encode_field(cache->maskstream, &iter, rf->width, mask);
274 }
275 
276 static void vcap_encode_keyfield_typegroups(struct vcap_control *vctrl,
277 					    struct vcap_rule_internal *ri,
278 					    const struct vcap_typegroup *tgt)
279 {
280 	int sw_width = vctrl->vcaps[ri->admin->vtype].sw_width;
281 	struct vcap_cache_data *cache = &ri->admin->cache;
282 
283 	/* Encode the typegroup bits for the key and the mask in their streams,
284 	 * respecting the subword width.
285 	 */
286 	vcap_encode_typegroups(cache->keystream, sw_width, tgt, false);
287 	vcap_encode_typegroups(cache->maskstream, sw_width, tgt, true);
288 }
289 
290 static int vcap_encode_rule_keyset(struct vcap_rule_internal *ri)
291 {
292 	const struct vcap_client_keyfield *ckf;
293 	const struct vcap_typegroup *tg_table;
294 	const struct vcap_field *kf_table;
295 	int keyset_size;
296 
297 	/* Get a valid set of fields for the specific keyset */
298 	kf_table = vcap_keyfields(ri->vctrl, ri->admin->vtype, ri->data.keyset);
299 	if (!kf_table) {
300 		pr_err("%s:%d: no fields available for this keyset: %d\n",
301 		       __func__, __LINE__, ri->data.keyset);
302 		return -EINVAL;
303 	}
304 	/* Get a valid typegroup for the specific keyset */
305 	tg_table = vcap_keyfield_typegroup(ri->vctrl, ri->admin->vtype,
306 					   ri->data.keyset);
307 	if (!tg_table) {
308 		pr_err("%s:%d: no typegroups available for this keyset: %d\n",
309 		       __func__, __LINE__, ri->data.keyset);
310 		return -EINVAL;
311 	}
312 	/* Get a valid size for the specific keyset */
313 	keyset_size = vcap_keyfield_count(ri->vctrl, ri->admin->vtype,
314 					  ri->data.keyset);
315 	if (keyset_size == 0) {
316 		pr_err("%s:%d: zero field count for this keyset: %d\n",
317 		       __func__, __LINE__, ri->data.keyset);
318 		return -EINVAL;
319 	}
320 	/* Iterate over the keyfields (key, mask) in the rule
321 	 * and encode these bits
322 	 */
323 	if (list_empty(&ri->data.keyfields)) {
324 		pr_err("%s:%d: no keyfields in the rule\n", __func__, __LINE__);
325 		return -EINVAL;
326 	}
327 	list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list) {
328 		/* Check that the client entry exists in the keyset */
329 		if (ckf->ctrl.key >= keyset_size) {
330 			pr_err("%s:%d: key %d is not in vcap\n",
331 			       __func__, __LINE__, ckf->ctrl.key);
332 			return -EINVAL;
333 		}
334 		vcap_encode_keyfield(ri, ckf, &kf_table[ckf->ctrl.key], tg_table);
335 	}
336 	/* Add typegroup bits to the key/mask bitstreams */
337 	vcap_encode_keyfield_typegroups(ri->vctrl, ri, tg_table);
338 	return 0;
339 }
340 
341 /* Return the list of actionfields for the actionset */
342 const struct vcap_field *
343 vcap_actionfields(struct vcap_control *vctrl,
344 		  enum vcap_type vt, enum vcap_actionfield_set actionset)
345 {
346 	/* Check that the actionset exists in the vcap actionset list */
347 	if (actionset >= vctrl->vcaps[vt].actionfield_set_size)
348 		return NULL;
349 	return vctrl->vcaps[vt].actionfield_set_map[actionset];
350 }
351 
352 const struct vcap_set *
353 vcap_actionfieldset(struct vcap_control *vctrl,
354 		    enum vcap_type vt, enum vcap_actionfield_set actionset)
355 {
356 	const struct vcap_set *aset;
357 
358 	/* Check that the actionset exists in the vcap actionset list */
359 	if (actionset >= vctrl->vcaps[vt].actionfield_set_size)
360 		return NULL;
361 	aset = &vctrl->vcaps[vt].actionfield_set[actionset];
362 	if (aset->sw_per_item == 0 || aset->sw_per_item > vctrl->vcaps[vt].sw_count)
363 		return NULL;
364 	return aset;
365 }
366 
367 /* Return the typegroup table for the matching actionset (using subword size) */
368 const struct vcap_typegroup *
369 vcap_actionfield_typegroup(struct vcap_control *vctrl,
370 			   enum vcap_type vt, enum vcap_actionfield_set actionset)
371 {
372 	const struct vcap_set *aset = vcap_actionfieldset(vctrl, vt, actionset);
373 
374 	/* Check that the actionset is valid */
375 	if (!aset)
376 		return NULL;
377 	return vctrl->vcaps[vt].actionfield_set_typegroups[aset->sw_per_item];
378 }
379 
380 /* Return the number of actionfields in the actionset */
381 int vcap_actionfield_count(struct vcap_control *vctrl,
382 			   enum vcap_type vt,
383 			   enum vcap_actionfield_set actionset)
384 {
385 	/* Check that the actionset exists in the vcap actionset list */
386 	if (actionset >= vctrl->vcaps[vt].actionfield_set_size)
387 		return 0;
388 	return vctrl->vcaps[vt].actionfield_set_map_size[actionset];
389 }
390 
391 static void vcap_encode_actionfield(struct vcap_rule_internal *ri,
392 				    const struct vcap_client_actionfield *af,
393 				    const struct vcap_field *rf,
394 				    const struct vcap_typegroup *tgt)
395 {
396 	int act_width = ri->vctrl->vcaps[ri->admin->vtype].act_width;
397 
398 	struct vcap_cache_data *cache = &ri->admin->cache;
399 	struct vcap_stream_iter iter;
400 	const u8 *value;
401 
402 	/* Encode the action field in the stream, respecting the subword width */
403 	switch (af->ctrl.type) {
404 	case VCAP_FIELD_BIT:
405 		value = &af->data.u1.value;
406 		break;
407 	case VCAP_FIELD_U32:
408 		value = (const u8 *)&af->data.u32.value;
409 		break;
410 	case VCAP_FIELD_U48:
411 		value = af->data.u48.value;
412 		break;
413 	case VCAP_FIELD_U56:
414 		value = af->data.u56.value;
415 		break;
416 	case VCAP_FIELD_U64:
417 		value = af->data.u64.value;
418 		break;
419 	case VCAP_FIELD_U72:
420 		value = af->data.u72.value;
421 		break;
422 	case VCAP_FIELD_U112:
423 		value = af->data.u112.value;
424 		break;
425 	case VCAP_FIELD_U128:
426 		value = af->data.u128.value;
427 		break;
428 	}
429 	vcap_iter_init(&iter, act_width, tgt, rf->offset);
430 	vcap_encode_field(cache->actionstream, &iter, rf->width, value);
431 }
432 
433 static void vcap_encode_actionfield_typegroups(struct vcap_rule_internal *ri,
434 					       const struct vcap_typegroup *tgt)
435 {
436 	int sw_width = ri->vctrl->vcaps[ri->admin->vtype].act_width;
437 	struct vcap_cache_data *cache = &ri->admin->cache;
438 
439 	/* Encode the typegroup bits for the actionstream respecting the subword
440 	 * width.
441 	 */
442 	vcap_encode_typegroups(cache->actionstream, sw_width, tgt, false);
443 }
444 
445 static int vcap_encode_rule_actionset(struct vcap_rule_internal *ri)
446 {
447 	const struct vcap_client_actionfield *caf;
448 	const struct vcap_typegroup *tg_table;
449 	const struct vcap_field *af_table;
450 	int actionset_size;
451 
452 	/* Get a valid set of actionset fields for the specific actionset */
453 	af_table = vcap_actionfields(ri->vctrl, ri->admin->vtype,
454 				     ri->data.actionset);
455 	if (!af_table) {
456 		pr_err("%s:%d: no fields available for this actionset: %d\n",
457 		       __func__, __LINE__, ri->data.actionset);
458 		return -EINVAL;
459 	}
460 	/* Get a valid typegroup for the specific actionset */
461 	tg_table = vcap_actionfield_typegroup(ri->vctrl, ri->admin->vtype,
462 					      ri->data.actionset);
463 	if (!tg_table) {
464 		pr_err("%s:%d: no typegroups available for this actionset: %d\n",
465 		       __func__, __LINE__, ri->data.actionset);
466 		return -EINVAL;
467 	}
468 	/* Get a valid actionset size for the specific actionset */
469 	actionset_size = vcap_actionfield_count(ri->vctrl, ri->admin->vtype,
470 						ri->data.actionset);
471 	if (actionset_size == 0) {
472 		pr_err("%s:%d: zero field count for this actionset: %d\n",
473 		       __func__, __LINE__, ri->data.actionset);
474 		return -EINVAL;
475 	}
476 	/* Iterate over the actionfields in the rule
477 	 * and encode these bits
478 	 */
479 	if (list_empty(&ri->data.actionfields))
480 		pr_warn("%s:%d: no actionfields in the rule\n",
481 			__func__, __LINE__);
482 	list_for_each_entry(caf, &ri->data.actionfields, ctrl.list) {
483 		/* Check that the client action exists in the actionset */
484 		if (caf->ctrl.action >= actionset_size) {
485 			pr_err("%s:%d: action %d is not in vcap\n",
486 			       __func__, __LINE__, caf->ctrl.action);
487 			return -EINVAL;
488 		}
489 		vcap_encode_actionfield(ri, caf, &af_table[caf->ctrl.action],
490 					tg_table);
491 	}
492 	/* Add typegroup bits to the entry bitstreams */
493 	vcap_encode_actionfield_typegroups(ri, tg_table);
494 	return 0;
495 }
496 
497 static int vcap_encode_rule(struct vcap_rule_internal *ri)
498 {
499 	int err;
500 
501 	err = vcap_encode_rule_keyset(ri);
502 	if (err)
503 		return err;
504 	err = vcap_encode_rule_actionset(ri);
505 	if (err)
506 		return err;
507 	return 0;
508 }
509 
510 int vcap_api_check(struct vcap_control *ctrl)
511 {
512 	if (!ctrl) {
513 		pr_err("%s:%d: vcap control is missing\n", __func__, __LINE__);
514 		return -EINVAL;
515 	}
516 	if (!ctrl->ops || !ctrl->ops->validate_keyset ||
517 	    !ctrl->ops->add_default_fields || !ctrl->ops->cache_erase ||
518 	    !ctrl->ops->cache_write || !ctrl->ops->cache_read ||
519 	    !ctrl->ops->init || !ctrl->ops->update || !ctrl->ops->move ||
520 	    !ctrl->ops->port_info || !ctrl->ops->enable) {
521 		pr_err("%s:%d: client operations are missing\n",
522 		       __func__, __LINE__);
523 		return -ENOENT;
524 	}
525 	return 0;
526 }
527 
528 void vcap_erase_cache(struct vcap_rule_internal *ri)
529 {
530 	ri->vctrl->ops->cache_erase(ri->admin);
531 }
532 
533 /* Update the keyset for the rule */
534 int vcap_set_rule_set_keyset(struct vcap_rule *rule,
535 			     enum vcap_keyfield_set keyset)
536 {
537 	struct vcap_rule_internal *ri = to_intrule(rule);
538 	const struct vcap_set *kset;
539 	int sw_width;
540 
541 	kset = vcap_keyfieldset(ri->vctrl, ri->admin->vtype, keyset);
542 	/* Check that the keyset is valid */
543 	if (!kset)
544 		return -EINVAL;
545 	ri->keyset_sw = kset->sw_per_item;
546 	sw_width = ri->vctrl->vcaps[ri->admin->vtype].sw_width;
547 	ri->keyset_sw_regs = DIV_ROUND_UP(sw_width, 32);
548 	ri->data.keyset = keyset;
549 	return 0;
550 }
551 EXPORT_SYMBOL_GPL(vcap_set_rule_set_keyset);
552 
553 /* Update the actionset for the rule */
554 int vcap_set_rule_set_actionset(struct vcap_rule *rule,
555 				enum vcap_actionfield_set actionset)
556 {
557 	struct vcap_rule_internal *ri = to_intrule(rule);
558 	const struct vcap_set *aset;
559 	int act_width;
560 
561 	aset = vcap_actionfieldset(ri->vctrl, ri->admin->vtype, actionset);
562 	/* Check that the actionset is valid */
563 	if (!aset)
564 		return -EINVAL;
565 	ri->actionset_sw = aset->sw_per_item;
566 	act_width = ri->vctrl->vcaps[ri->admin->vtype].act_width;
567 	ri->actionset_sw_regs = DIV_ROUND_UP(act_width, 32);
568 	ri->data.actionset = actionset;
569 	return 0;
570 }
571 EXPORT_SYMBOL_GPL(vcap_set_rule_set_actionset);
572 
573 /* Find a rule with a provided rule id */
574 static struct vcap_rule_internal *vcap_lookup_rule(struct vcap_control *vctrl,
575 						   u32 id)
576 {
577 	struct vcap_rule_internal *ri;
578 	struct vcap_admin *admin;
579 
580 	/* Look for the rule id in all vcaps */
581 	list_for_each_entry(admin, &vctrl->list, list)
582 		list_for_each_entry(ri, &admin->rules, list)
583 			if (ri->data.id == id)
584 				return ri;
585 	return NULL;
586 }
587 
588 /* Find a rule id with a provided cookie */
589 int vcap_lookup_rule_by_cookie(struct vcap_control *vctrl, u64 cookie)
590 {
591 	struct vcap_rule_internal *ri;
592 	struct vcap_admin *admin;
593 
594 	/* Look for the rule id in all vcaps */
595 	list_for_each_entry(admin, &vctrl->list, list)
596 		list_for_each_entry(ri, &admin->rules, list)
597 			if (ri->data.cookie == cookie)
598 				return ri->data.id;
599 	return -ENOENT;
600 }
601 EXPORT_SYMBOL_GPL(vcap_lookup_rule_by_cookie);
602 
603 /* Make a shallow copy of the rule without the fields */
604 struct vcap_rule_internal *vcap_dup_rule(struct vcap_rule_internal *ri)
605 {
606 	struct vcap_rule_internal *duprule;
607 
608 	/* Allocate the client part */
609 	duprule = kzalloc(sizeof(*duprule), GFP_KERNEL);
610 	if (!duprule)
611 		return ERR_PTR(-ENOMEM);
612 	*duprule = *ri;
613 	/* Not inserted in the VCAP */
614 	INIT_LIST_HEAD(&duprule->list);
615 	/* No elements in these lists */
616 	INIT_LIST_HEAD(&duprule->data.keyfields);
617 	INIT_LIST_HEAD(&duprule->data.actionfields);
618 	return duprule;
619 }
620 
621 /* Write VCAP cache content to the VCAP HW instance */
622 static int vcap_write_rule(struct vcap_rule_internal *ri)
623 {
624 	struct vcap_admin *admin = ri->admin;
625 	int sw_idx, ent_idx = 0, act_idx = 0;
626 	u32 addr = ri->addr;
627 
628 	if (!ri->size || !ri->keyset_sw_regs || !ri->actionset_sw_regs) {
629 		pr_err("%s:%d: rule is empty\n", __func__, __LINE__);
630 		return -EINVAL;
631 	}
632 	/* Use the values in the streams to write the VCAP cache */
633 	for (sw_idx = 0; sw_idx < ri->size; sw_idx++, addr++) {
634 		ri->vctrl->ops->cache_write(ri->ndev, admin,
635 					    VCAP_SEL_ENTRY, ent_idx,
636 					    ri->keyset_sw_regs);
637 		ri->vctrl->ops->cache_write(ri->ndev, admin,
638 					    VCAP_SEL_ACTION, act_idx,
639 					    ri->actionset_sw_regs);
640 		ri->vctrl->ops->update(ri->ndev, admin, VCAP_CMD_WRITE,
641 				       VCAP_SEL_ALL, addr);
642 		ent_idx += ri->keyset_sw_regs;
643 		act_idx += ri->actionset_sw_regs;
644 	}
645 	return 0;
646 }
647 
648 static int vcap_write_counter(struct vcap_rule_internal *ri,
649 			      struct vcap_counter *ctr)
650 {
651 	struct vcap_admin *admin = ri->admin;
652 
653 	admin->cache.counter = ctr->value;
654 	admin->cache.sticky = ctr->sticky;
655 	ri->vctrl->ops->cache_write(ri->ndev, admin, VCAP_SEL_COUNTER,
656 				    ri->counter_id, 0);
657 	ri->vctrl->ops->update(ri->ndev, admin, VCAP_CMD_WRITE,
658 			       VCAP_SEL_COUNTER, ri->addr);
659 	return 0;
660 }
661 
662 /* Convert a chain id to a VCAP lookup index */
663 int vcap_chain_id_to_lookup(struct vcap_admin *admin, int cur_cid)
664 {
665 	int lookup_first = admin->vinst * admin->lookups_per_instance;
666 	int lookup_last = lookup_first + admin->lookups_per_instance;
667 	int cid_next = admin->first_cid + VCAP_CID_LOOKUP_SIZE;
668 	int cid = admin->first_cid;
669 	int lookup;
670 
671 	for (lookup = lookup_first; lookup < lookup_last; ++lookup,
672 	     cid += VCAP_CID_LOOKUP_SIZE, cid_next += VCAP_CID_LOOKUP_SIZE)
673 		if (cur_cid >= cid && cur_cid < cid_next)
674 			return lookup;
675 	return 0;
676 }
677 EXPORT_SYMBOL_GPL(vcap_chain_id_to_lookup);
678 
679 /* Lookup a vcap instance using chain id */
680 struct vcap_admin *vcap_find_admin(struct vcap_control *vctrl, int cid)
681 {
682 	struct vcap_admin *admin;
683 
684 	if (vcap_api_check(vctrl))
685 		return NULL;
686 
687 	list_for_each_entry(admin, &vctrl->list, list) {
688 		if (cid >= admin->first_cid && cid <= admin->last_cid)
689 			return admin;
690 	}
691 	return NULL;
692 }
693 EXPORT_SYMBOL_GPL(vcap_find_admin);
694 
695 /* Is the next chain id in the following lookup, possible in another VCAP */
696 bool vcap_is_next_lookup(struct vcap_control *vctrl, int cur_cid, int next_cid)
697 {
698 	struct vcap_admin *admin, *next_admin;
699 	int lookup, next_lookup;
700 
701 	/* The offset must be at least one lookup */
702 	if (next_cid < cur_cid + VCAP_CID_LOOKUP_SIZE)
703 		return false;
704 
705 	if (vcap_api_check(vctrl))
706 		return false;
707 
708 	admin = vcap_find_admin(vctrl, cur_cid);
709 	if (!admin)
710 		return false;
711 
712 	/* If no VCAP contains the next chain, the next chain must be beyond
713 	 * the last chain in the current VCAP
714 	 */
715 	next_admin = vcap_find_admin(vctrl, next_cid);
716 	if (!next_admin)
717 		return next_cid > admin->last_cid;
718 
719 	lookup = vcap_chain_id_to_lookup(admin, cur_cid);
720 	next_lookup = vcap_chain_id_to_lookup(next_admin, next_cid);
721 
722 	/* Next lookup must be the following lookup */
723 	if (admin == next_admin || admin->vtype == next_admin->vtype)
724 		return next_lookup == lookup + 1;
725 
726 	/* Must be the first lookup in the next VCAP instance */
727 	return next_lookup == 0;
728 }
729 EXPORT_SYMBOL_GPL(vcap_is_next_lookup);
730 
731 /* Check if there is room for a new rule */
732 static int vcap_rule_space(struct vcap_admin *admin, int size)
733 {
734 	if (admin->last_used_addr - size < admin->first_valid_addr) {
735 		pr_err("%s:%d: No room for rule size: %u, %u\n",
736 		       __func__, __LINE__, size, admin->first_valid_addr);
737 		return -ENOSPC;
738 	}
739 	return 0;
740 }
741 
742 /* Add the keyset typefield to the list of rule keyfields */
743 static int vcap_add_type_keyfield(struct vcap_rule *rule)
744 {
745 	struct vcap_rule_internal *ri = to_intrule(rule);
746 	enum vcap_keyfield_set keyset = rule->keyset;
747 	enum vcap_type vt = ri->admin->vtype;
748 	const struct vcap_field *fields;
749 	const struct vcap_set *kset;
750 	int ret = -EINVAL;
751 
752 	kset = vcap_keyfieldset(ri->vctrl, vt, keyset);
753 	if (!kset)
754 		return ret;
755 	if (kset->type_id == (u8)-1)  /* No type field is needed */
756 		return 0;
757 
758 	fields = vcap_keyfields(ri->vctrl, vt, keyset);
759 	if (!fields)
760 		return -EINVAL;
761 	if (fields[VCAP_KF_TYPE].width > 1) {
762 		ret = vcap_rule_add_key_u32(rule, VCAP_KF_TYPE,
763 					    kset->type_id, 0xff);
764 	} else {
765 		if (kset->type_id)
766 			ret = vcap_rule_add_key_bit(rule, VCAP_KF_TYPE,
767 						    VCAP_BIT_1);
768 		else
769 			ret = vcap_rule_add_key_bit(rule, VCAP_KF_TYPE,
770 						    VCAP_BIT_0);
771 	}
772 	return 0;
773 }
774 
775 /* Add a keyset to a keyset list */
776 bool vcap_keyset_list_add(struct vcap_keyset_list *keysetlist,
777 			  enum vcap_keyfield_set keyset)
778 {
779 	int idx;
780 
781 	if (keysetlist->cnt < keysetlist->max) {
782 		/* Avoid duplicates */
783 		for (idx = 0; idx < keysetlist->cnt; ++idx)
784 			if (keysetlist->keysets[idx] == keyset)
785 				return keysetlist->cnt < keysetlist->max;
786 		keysetlist->keysets[keysetlist->cnt++] = keyset;
787 	}
788 	return keysetlist->cnt < keysetlist->max;
789 }
790 EXPORT_SYMBOL_GPL(vcap_keyset_list_add);
791 
792 /* map keyset id to a string with the keyset name */
793 const char *vcap_keyset_name(struct vcap_control *vctrl,
794 			     enum vcap_keyfield_set keyset)
795 {
796 	return vctrl->stats->keyfield_set_names[keyset];
797 }
798 EXPORT_SYMBOL_GPL(vcap_keyset_name);
799 
800 /* map key field id to a string with the key name */
801 const char *vcap_keyfield_name(struct vcap_control *vctrl,
802 			       enum vcap_key_field key)
803 {
804 	return vctrl->stats->keyfield_names[key];
805 }
806 EXPORT_SYMBOL_GPL(vcap_keyfield_name);
807 
808 /* map actionset id to a string with the actionset name */
809 const char *vcap_actionset_name(struct vcap_control *vctrl,
810 				enum vcap_actionfield_set actionset)
811 {
812 	return vctrl->stats->actionfield_set_names[actionset];
813 }
814 
815 /* map action field id to a string with the action name */
816 const char *vcap_actionfield_name(struct vcap_control *vctrl,
817 				  enum vcap_action_field action)
818 {
819 	return vctrl->stats->actionfield_names[action];
820 }
821 
822 /* Return the keyfield that matches a key in a keyset */
823 static const struct vcap_field *
824 vcap_find_keyset_keyfield(struct vcap_control *vctrl,
825 			  enum vcap_type vtype,
826 			  enum vcap_keyfield_set keyset,
827 			  enum vcap_key_field key)
828 {
829 	const struct vcap_field *fields;
830 	int idx, count;
831 
832 	fields = vcap_keyfields(vctrl, vtype, keyset);
833 	if (!fields)
834 		return NULL;
835 
836 	/* Iterate the keyfields of the keyset */
837 	count = vcap_keyfield_count(vctrl, vtype, keyset);
838 	for (idx = 0; idx < count; ++idx) {
839 		if (fields[idx].width == 0)
840 			continue;
841 
842 		if (key == idx)
843 			return &fields[idx];
844 	}
845 
846 	return NULL;
847 }
848 
849 /* Match a list of keys against the keysets available in a vcap type */
850 static bool _vcap_rule_find_keysets(struct vcap_rule_internal *ri,
851 				    struct vcap_keyset_list *matches)
852 {
853 	const struct vcap_client_keyfield *ckf;
854 	int keyset, found, keycount, map_size;
855 	const struct vcap_field **map;
856 	enum vcap_type vtype;
857 
858 	vtype = ri->admin->vtype;
859 	map = ri->vctrl->vcaps[vtype].keyfield_set_map;
860 	map_size = ri->vctrl->vcaps[vtype].keyfield_set_size;
861 
862 	/* Get a count of the keyfields we want to match */
863 	keycount = 0;
864 	list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list)
865 		++keycount;
866 
867 	matches->cnt = 0;
868 	/* Iterate the keysets of the VCAP */
869 	for (keyset = 0; keyset < map_size; ++keyset) {
870 		if (!map[keyset])
871 			continue;
872 
873 		/* Iterate the keys in the rule */
874 		found = 0;
875 		list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list)
876 			if (vcap_find_keyset_keyfield(ri->vctrl, vtype,
877 						      keyset, ckf->ctrl.key))
878 				++found;
879 
880 		/* Save the keyset if all keyfields were found */
881 		if (found == keycount)
882 			if (!vcap_keyset_list_add(matches, keyset))
883 				/* bail out when the quota is filled */
884 				break;
885 	}
886 
887 	return matches->cnt > 0;
888 }
889 
890 /* Match a list of keys against the keysets available in a vcap type */
891 bool vcap_rule_find_keysets(struct vcap_rule *rule,
892 			    struct vcap_keyset_list *matches)
893 {
894 	struct vcap_rule_internal *ri = to_intrule(rule);
895 
896 	return _vcap_rule_find_keysets(ri, matches);
897 }
898 EXPORT_SYMBOL_GPL(vcap_rule_find_keysets);
899 
900 /* Validate a rule with respect to available port keys */
901 int vcap_val_rule(struct vcap_rule *rule, u16 l3_proto)
902 {
903 	struct vcap_rule_internal *ri = to_intrule(rule);
904 	struct vcap_keyset_list matches = {};
905 	enum vcap_keyfield_set keysets[10];
906 	int ret;
907 
908 	ret = vcap_api_check(ri->vctrl);
909 	if (ret)
910 		return ret;
911 	if (!ri->admin) {
912 		ri->data.exterr = VCAP_ERR_NO_ADMIN;
913 		return -EINVAL;
914 	}
915 	if (!ri->ndev) {
916 		ri->data.exterr = VCAP_ERR_NO_NETDEV;
917 		return -EINVAL;
918 	}
919 
920 	matches.keysets = keysets;
921 	matches.max = ARRAY_SIZE(keysets);
922 	if (ri->data.keyset == VCAP_KFS_NO_VALUE) {
923 		/* Iterate over rule keyfields and select keysets that fits */
924 		if (!_vcap_rule_find_keysets(ri, &matches)) {
925 			ri->data.exterr = VCAP_ERR_NO_KEYSET_MATCH;
926 			return -EINVAL;
927 		}
928 	} else {
929 		/* prepare for keyset validation */
930 		keysets[0] = ri->data.keyset;
931 		matches.cnt = 1;
932 	}
933 
934 	/* Pick a keyset that is supported in the port lookups */
935 	ret = ri->vctrl->ops->validate_keyset(ri->ndev, ri->admin, rule,
936 					      &matches, l3_proto);
937 	if (ret < 0) {
938 		pr_err("%s:%d: keyset validation failed: %d\n",
939 		       __func__, __LINE__, ret);
940 		ri->data.exterr = VCAP_ERR_NO_PORT_KEYSET_MATCH;
941 		return ret;
942 	}
943 	/* use the keyset that is supported in the port lookups */
944 	ret = vcap_set_rule_set_keyset(rule, ret);
945 	if (ret < 0) {
946 		pr_err("%s:%d: keyset was not updated: %d\n",
947 		       __func__, __LINE__, ret);
948 		return ret;
949 	}
950 	if (ri->data.actionset == VCAP_AFS_NO_VALUE) {
951 		/* Later also actionsets will be matched against actions in
952 		 * the rule, and the type will be set accordingly
953 		 */
954 		ri->data.exterr = VCAP_ERR_NO_ACTIONSET_MATCH;
955 		return -EINVAL;
956 	}
957 	vcap_add_type_keyfield(rule);
958 	/* Add default fields to this rule */
959 	ri->vctrl->ops->add_default_fields(ri->ndev, ri->admin, rule);
960 
961 	/* Rule size is the maximum of the entry and action subword count */
962 	ri->size = max(ri->keyset_sw, ri->actionset_sw);
963 
964 	/* Finally check if there is room for the rule in the VCAP */
965 	return vcap_rule_space(ri->admin, ri->size);
966 }
967 EXPORT_SYMBOL_GPL(vcap_val_rule);
968 
969 /* Entries are sorted with increasing values of sort_key.
970  * I.e. Lowest numerical sort_key is first in list.
971  * In order to locate largest keys first in list we negate the key size with
972  * (max_size - size).
973  */
974 static u32 vcap_sort_key(u32 max_size, u32 size, u8 user, u16 prio)
975 {
976 	return ((max_size - size) << 24) | (user << 16) | prio;
977 }
978 
979 /* calculate the address of the next rule after this (lower address and prio) */
980 static u32 vcap_next_rule_addr(u32 addr, struct vcap_rule_internal *ri)
981 {
982 	return ((addr - ri->size) /  ri->size) * ri->size;
983 }
984 
985 /* Assign a unique rule id and autogenerate one if id == 0 */
986 static u32 vcap_set_rule_id(struct vcap_rule_internal *ri)
987 {
988 	u32 next_id;
989 
990 	if (ri->data.id != 0)
991 		return ri->data.id;
992 
993 	next_id = ri->vctrl->rule_id + 1;
994 
995 	for (next_id = ri->vctrl->rule_id + 1; next_id < ~0; ++next_id) {
996 		if (!vcap_lookup_rule(ri->vctrl, next_id)) {
997 			ri->data.id = next_id;
998 			ri->vctrl->rule_id = next_id;
999 			break;
1000 		}
1001 	}
1002 	return ri->data.id;
1003 }
1004 
1005 static int vcap_insert_rule(struct vcap_rule_internal *ri,
1006 			    struct vcap_rule_move *move)
1007 {
1008 	int sw_count = ri->vctrl->vcaps[ri->admin->vtype].sw_count;
1009 	struct vcap_rule_internal *duprule, *iter, *elem = NULL;
1010 	struct vcap_admin *admin = ri->admin;
1011 	u32 addr;
1012 
1013 	ri->sort_key = vcap_sort_key(sw_count, ri->size, ri->data.user,
1014 				     ri->data.priority);
1015 
1016 	/* Insert the new rule in the list of rule based on the sort key
1017 	 * If the rule needs to be  inserted between existing rules then move
1018 	 * these rules to make room for the new rule and update their start
1019 	 * address.
1020 	 */
1021 	list_for_each_entry(iter, &admin->rules, list) {
1022 		if (ri->sort_key < iter->sort_key) {
1023 			elem = iter;
1024 			break;
1025 		}
1026 	}
1027 
1028 	if (!elem) {
1029 		ri->addr = vcap_next_rule_addr(admin->last_used_addr, ri);
1030 		admin->last_used_addr = ri->addr;
1031 
1032 		/* Add a shallow copy of the rule to the VCAP list */
1033 		duprule = vcap_dup_rule(ri);
1034 		if (IS_ERR(duprule))
1035 			return PTR_ERR(duprule);
1036 
1037 		list_add_tail(&duprule->list, &admin->rules);
1038 		return 0;
1039 	}
1040 
1041 	/* Reuse the space of the current rule */
1042 	addr = elem->addr + elem->size;
1043 	ri->addr = vcap_next_rule_addr(addr, ri);
1044 	addr = ri->addr;
1045 
1046 	/* Add a shallow copy of the rule to the VCAP list */
1047 	duprule = vcap_dup_rule(ri);
1048 	if (IS_ERR(duprule))
1049 		return PTR_ERR(duprule);
1050 
1051 	/* Add before the current entry */
1052 	list_add_tail(&duprule->list, &elem->list);
1053 
1054 	/* Update the current rule */
1055 	elem->addr = vcap_next_rule_addr(addr, elem);
1056 	addr = elem->addr;
1057 
1058 	/* Update the address in the remaining rules in the list */
1059 	list_for_each_entry_continue(elem, &admin->rules, list) {
1060 		elem->addr = vcap_next_rule_addr(addr, elem);
1061 		addr = elem->addr;
1062 	}
1063 
1064 	/* Update the move info */
1065 	move->addr = admin->last_used_addr;
1066 	move->count = ri->addr - addr;
1067 	move->offset = admin->last_used_addr - addr;
1068 	admin->last_used_addr = addr;
1069 	return 0;
1070 }
1071 
1072 static void vcap_move_rules(struct vcap_rule_internal *ri,
1073 			    struct vcap_rule_move *move)
1074 {
1075 	ri->vctrl->ops->move(ri->ndev, ri->admin, move->addr,
1076 			 move->offset, move->count);
1077 }
1078 
1079 /* Encode and write a validated rule to the VCAP */
1080 int vcap_add_rule(struct vcap_rule *rule)
1081 {
1082 	struct vcap_rule_internal *ri = to_intrule(rule);
1083 	struct vcap_rule_move move = {0};
1084 	int ret;
1085 
1086 	ret = vcap_api_check(ri->vctrl);
1087 	if (ret)
1088 		return ret;
1089 	/* Insert the new rule in the list of vcap rules */
1090 	mutex_lock(&ri->admin->lock);
1091 	ret = vcap_insert_rule(ri, &move);
1092 	if (ret < 0) {
1093 		pr_err("%s:%d: could not insert rule in vcap list: %d\n",
1094 		       __func__, __LINE__, ret);
1095 		goto out;
1096 	}
1097 	if (move.count > 0)
1098 		vcap_move_rules(ri, &move);
1099 	ret = vcap_encode_rule(ri);
1100 	if (ret) {
1101 		pr_err("%s:%d: rule encoding error: %d\n", __func__, __LINE__, ret);
1102 		goto out;
1103 	}
1104 
1105 	ret = vcap_write_rule(ri);
1106 	if (ret)
1107 		pr_err("%s:%d: rule write error: %d\n", __func__, __LINE__, ret);
1108 out:
1109 	mutex_unlock(&ri->admin->lock);
1110 	return ret;
1111 }
1112 EXPORT_SYMBOL_GPL(vcap_add_rule);
1113 
1114 /* Allocate a new rule with the provided arguments */
1115 struct vcap_rule *vcap_alloc_rule(struct vcap_control *vctrl,
1116 				  struct net_device *ndev, int vcap_chain_id,
1117 				  enum vcap_user user, u16 priority,
1118 				  u32 id)
1119 {
1120 	struct vcap_rule_internal *ri;
1121 	struct vcap_admin *admin;
1122 	int err, maxsize;
1123 
1124 	err = vcap_api_check(vctrl);
1125 	if (err)
1126 		return ERR_PTR(err);
1127 	if (!ndev)
1128 		return ERR_PTR(-ENODEV);
1129 	/* Get the VCAP instance */
1130 	admin = vcap_find_admin(vctrl, vcap_chain_id);
1131 	if (!admin)
1132 		return ERR_PTR(-ENOENT);
1133 	/* Sanity check that this VCAP is supported on this platform */
1134 	if (vctrl->vcaps[admin->vtype].rows == 0)
1135 		return ERR_PTR(-EINVAL);
1136 	/* Check if a rule with this id already exists */
1137 	if (vcap_lookup_rule(vctrl, id))
1138 		return ERR_PTR(-EEXIST);
1139 	/* Check if there is room for the rule in the block(s) of the VCAP */
1140 	maxsize = vctrl->vcaps[admin->vtype].sw_count; /* worst case rule size */
1141 	if (vcap_rule_space(admin, maxsize))
1142 		return ERR_PTR(-ENOSPC);
1143 	/* Create a container for the rule and return it */
1144 	ri = kzalloc(sizeof(*ri), GFP_KERNEL);
1145 	if (!ri)
1146 		return ERR_PTR(-ENOMEM);
1147 	ri->data.vcap_chain_id = vcap_chain_id;
1148 	ri->data.user = user;
1149 	ri->data.priority = priority;
1150 	ri->data.id = id;
1151 	ri->data.keyset = VCAP_KFS_NO_VALUE;
1152 	ri->data.actionset = VCAP_AFS_NO_VALUE;
1153 	INIT_LIST_HEAD(&ri->list);
1154 	INIT_LIST_HEAD(&ri->data.keyfields);
1155 	INIT_LIST_HEAD(&ri->data.actionfields);
1156 	ri->ndev = ndev;
1157 	ri->admin = admin; /* refer to the vcap instance */
1158 	ri->vctrl = vctrl; /* refer to the client */
1159 	if (vcap_set_rule_id(ri) == 0)
1160 		goto out_free;
1161 	vcap_erase_cache(ri);
1162 	return (struct vcap_rule *)ri;
1163 
1164 out_free:
1165 	kfree(ri);
1166 	return ERR_PTR(-EINVAL);
1167 }
1168 EXPORT_SYMBOL_GPL(vcap_alloc_rule);
1169 
1170 /* Free mem of a rule owned by client after the rule as been added to the VCAP */
1171 void vcap_free_rule(struct vcap_rule *rule)
1172 {
1173 	struct vcap_rule_internal *ri = to_intrule(rule);
1174 	struct vcap_client_actionfield *caf, *next_caf;
1175 	struct vcap_client_keyfield *ckf, *next_ckf;
1176 
1177 	/* Deallocate the list of keys and actions */
1178 	list_for_each_entry_safe(ckf, next_ckf, &ri->data.keyfields, ctrl.list) {
1179 		list_del(&ckf->ctrl.list);
1180 		kfree(ckf);
1181 	}
1182 	list_for_each_entry_safe(caf, next_caf, &ri->data.actionfields, ctrl.list) {
1183 		list_del(&caf->ctrl.list);
1184 		kfree(caf);
1185 	}
1186 	/* Deallocate the rule */
1187 	kfree(rule);
1188 }
1189 EXPORT_SYMBOL_GPL(vcap_free_rule);
1190 
1191 /* Return the alignment offset for a new rule address */
1192 static int vcap_valid_rule_move(struct vcap_rule_internal *el, int offset)
1193 {
1194 	return (el->addr + offset) % el->size;
1195 }
1196 
1197 /* Update the rule address with an offset */
1198 static void vcap_adjust_rule_addr(struct vcap_rule_internal *el, int offset)
1199 {
1200 	el->addr += offset;
1201 }
1202 
1203 /* Rules needs to be moved to fill the gap of the deleted rule */
1204 static int vcap_fill_rule_gap(struct vcap_rule_internal *ri)
1205 {
1206 	struct vcap_admin *admin = ri->admin;
1207 	struct vcap_rule_internal *elem;
1208 	struct vcap_rule_move move;
1209 	int gap = 0, offset = 0;
1210 
1211 	/* If the first rule is deleted: Move other rules to the top */
1212 	if (list_is_first(&ri->list, &admin->rules))
1213 		offset = admin->last_valid_addr + 1 - ri->addr - ri->size;
1214 
1215 	/* Locate gaps between odd size rules and adjust the move */
1216 	elem = ri;
1217 	list_for_each_entry_continue(elem, &admin->rules, list)
1218 		gap += vcap_valid_rule_move(elem, ri->size);
1219 
1220 	/* Update the address in the remaining rules in the list */
1221 	elem = ri;
1222 	list_for_each_entry_continue(elem, &admin->rules, list)
1223 		vcap_adjust_rule_addr(elem, ri->size + gap + offset);
1224 
1225 	/* Update the move info */
1226 	move.addr = admin->last_used_addr;
1227 	move.count = ri->addr - admin->last_used_addr - gap;
1228 	move.offset = -(ri->size + gap + offset);
1229 
1230 	/* Do the actual move operation */
1231 	vcap_move_rules(ri, &move);
1232 
1233 	return gap + offset;
1234 }
1235 
1236 /* Delete rule in a VCAP instance */
1237 int vcap_del_rule(struct vcap_control *vctrl, struct net_device *ndev, u32 id)
1238 {
1239 	struct vcap_rule_internal *ri, *elem;
1240 	struct vcap_admin *admin;
1241 	int gap = 0, err;
1242 
1243 	/* This will later also handle rule moving */
1244 	if (!ndev)
1245 		return -ENODEV;
1246 	err = vcap_api_check(vctrl);
1247 	if (err)
1248 		return err;
1249 	/* Look for the rule id in all vcaps */
1250 	ri = vcap_lookup_rule(vctrl, id);
1251 	if (!ri)
1252 		return -EINVAL;
1253 	admin = ri->admin;
1254 
1255 	if (ri->addr > admin->last_used_addr)
1256 		gap = vcap_fill_rule_gap(ri);
1257 
1258 	/* Delete the rule from the list of rules and the cache */
1259 	mutex_lock(&admin->lock);
1260 	list_del(&ri->list);
1261 	vctrl->ops->init(ndev, admin, admin->last_used_addr, ri->size + gap);
1262 	kfree(ri);
1263 	mutex_unlock(&admin->lock);
1264 
1265 	/* Update the last used address, set to default when no rules */
1266 	if (list_empty(&admin->rules)) {
1267 		admin->last_used_addr = admin->last_valid_addr + 1;
1268 	} else {
1269 		elem = list_last_entry(&admin->rules, struct vcap_rule_internal,
1270 				       list);
1271 		admin->last_used_addr = elem->addr;
1272 	}
1273 	return 0;
1274 }
1275 EXPORT_SYMBOL_GPL(vcap_del_rule);
1276 
1277 /* Delete all rules in the VCAP instance */
1278 int vcap_del_rules(struct vcap_control *vctrl, struct vcap_admin *admin)
1279 {
1280 	struct vcap_enabled_port *eport, *next_eport;
1281 	struct vcap_rule_internal *ri, *next_ri;
1282 	int ret = vcap_api_check(vctrl);
1283 
1284 	if (ret)
1285 		return ret;
1286 
1287 	mutex_lock(&admin->lock);
1288 	list_for_each_entry_safe(ri, next_ri, &admin->rules, list) {
1289 		vctrl->ops->init(ri->ndev, admin, ri->addr, ri->size);
1290 		list_del(&ri->list);
1291 		kfree(ri);
1292 	}
1293 	admin->last_used_addr = admin->last_valid_addr;
1294 
1295 	/* Remove list of enabled ports */
1296 	list_for_each_entry_safe(eport, next_eport, &admin->enabled, list) {
1297 		list_del(&eport->list);
1298 		kfree(eport);
1299 	}
1300 	mutex_unlock(&admin->lock);
1301 
1302 	return 0;
1303 }
1304 EXPORT_SYMBOL_GPL(vcap_del_rules);
1305 
1306 /* Find a client key field in a rule */
1307 static struct vcap_client_keyfield *
1308 vcap_find_keyfield(struct vcap_rule *rule, enum vcap_key_field key)
1309 {
1310 	struct vcap_rule_internal *ri = to_intrule(rule);
1311 	struct vcap_client_keyfield *ckf;
1312 
1313 	list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list)
1314 		if (ckf->ctrl.key == key)
1315 			return ckf;
1316 	return NULL;
1317 }
1318 
1319 /* Find information on a key field in a rule */
1320 const struct vcap_field *vcap_lookup_keyfield(struct vcap_rule *rule,
1321 					      enum vcap_key_field key)
1322 {
1323 	struct vcap_rule_internal *ri = to_intrule(rule);
1324 	enum vcap_keyfield_set keyset = rule->keyset;
1325 	enum vcap_type vt = ri->admin->vtype;
1326 	const struct vcap_field *fields;
1327 
1328 	if (keyset == VCAP_KFS_NO_VALUE)
1329 		return NULL;
1330 	fields = vcap_keyfields(ri->vctrl, vt, keyset);
1331 	if (!fields)
1332 		return NULL;
1333 	return &fields[key];
1334 }
1335 EXPORT_SYMBOL_GPL(vcap_lookup_keyfield);
1336 
1337 /* Copy data from src to dst but reverse the data in chunks of 32bits.
1338  * For example if src is 00:11:22:33:44:55 where 55 is LSB the dst will
1339  * have the value 22:33:44:55:00:11.
1340  */
1341 static void vcap_copy_to_w32be(u8 *dst, u8 *src, int size)
1342 {
1343 	for (int idx = 0; idx < size; ++idx) {
1344 		int first_byte_index = 0;
1345 		int nidx;
1346 
1347 		first_byte_index = size - (((idx >> 2) + 1) << 2);
1348 		if (first_byte_index < 0)
1349 			first_byte_index = 0;
1350 		nidx = idx + first_byte_index - (idx & ~0x3);
1351 		dst[nidx] = src[idx];
1352 	}
1353 }
1354 
1355 static void vcap_copy_from_client_keyfield(struct vcap_rule *rule,
1356 					   struct vcap_client_keyfield *field,
1357 					   struct vcap_client_keyfield_data *data)
1358 {
1359 	struct vcap_rule_internal *ri = to_intrule(rule);
1360 	int size;
1361 
1362 	if (!ri->admin->w32be) {
1363 		memcpy(&field->data, data, sizeof(field->data));
1364 		return;
1365 	}
1366 
1367 	size = keyfield_size_table[field->ctrl.type] / 2;
1368 	switch (field->ctrl.type) {
1369 	case VCAP_FIELD_BIT:
1370 	case VCAP_FIELD_U32:
1371 		memcpy(&field->data, data, sizeof(field->data));
1372 		break;
1373 	case VCAP_FIELD_U48:
1374 		vcap_copy_to_w32be(field->data.u48.value, data->u48.value, size);
1375 		vcap_copy_to_w32be(field->data.u48.mask,  data->u48.mask, size);
1376 		break;
1377 	case VCAP_FIELD_U56:
1378 		vcap_copy_to_w32be(field->data.u56.value, data->u56.value, size);
1379 		vcap_copy_to_w32be(field->data.u56.mask,  data->u56.mask, size);
1380 		break;
1381 	case VCAP_FIELD_U64:
1382 		vcap_copy_to_w32be(field->data.u64.value, data->u64.value, size);
1383 		vcap_copy_to_w32be(field->data.u64.mask,  data->u64.mask, size);
1384 		break;
1385 	case VCAP_FIELD_U72:
1386 		vcap_copy_to_w32be(field->data.u72.value, data->u72.value, size);
1387 		vcap_copy_to_w32be(field->data.u72.mask,  data->u72.mask, size);
1388 		break;
1389 	case VCAP_FIELD_U112:
1390 		vcap_copy_to_w32be(field->data.u112.value, data->u112.value, size);
1391 		vcap_copy_to_w32be(field->data.u112.mask,  data->u112.mask, size);
1392 		break;
1393 	case VCAP_FIELD_U128:
1394 		vcap_copy_to_w32be(field->data.u128.value, data->u128.value, size);
1395 		vcap_copy_to_w32be(field->data.u128.mask,  data->u128.mask, size);
1396 		break;
1397 	};
1398 }
1399 
1400 /* Check if the keyfield is already in the rule */
1401 static bool vcap_keyfield_unique(struct vcap_rule *rule,
1402 				 enum vcap_key_field key)
1403 {
1404 	struct vcap_rule_internal *ri = to_intrule(rule);
1405 	const struct vcap_client_keyfield *ckf;
1406 
1407 	list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list)
1408 		if (ckf->ctrl.key == key)
1409 			return false;
1410 	return true;
1411 }
1412 
1413 /* Check if the keyfield is in the keyset */
1414 static bool vcap_keyfield_match_keyset(struct vcap_rule *rule,
1415 				       enum vcap_key_field key)
1416 {
1417 	struct vcap_rule_internal *ri = to_intrule(rule);
1418 	enum vcap_keyfield_set keyset = rule->keyset;
1419 	enum vcap_type vt = ri->admin->vtype;
1420 	const struct vcap_field *fields;
1421 
1422 	/* the field is accepted if the rule has no keyset yet */
1423 	if (keyset == VCAP_KFS_NO_VALUE)
1424 		return true;
1425 	fields = vcap_keyfields(ri->vctrl, vt, keyset);
1426 	if (!fields)
1427 		return false;
1428 	/* if there is a width there is a way */
1429 	return fields[key].width > 0;
1430 }
1431 
1432 static int vcap_rule_add_key(struct vcap_rule *rule,
1433 			     enum vcap_key_field key,
1434 			     enum vcap_field_type ftype,
1435 			     struct vcap_client_keyfield_data *data)
1436 {
1437 	struct vcap_rule_internal *ri = to_intrule(rule);
1438 	struct vcap_client_keyfield *field;
1439 
1440 	if (!vcap_keyfield_unique(rule, key)) {
1441 		pr_warn("%s:%d: keyfield %s is already in the rule\n",
1442 			__func__, __LINE__,
1443 			vcap_keyfield_name(ri->vctrl, key));
1444 		return -EINVAL;
1445 	}
1446 
1447 	if (!vcap_keyfield_match_keyset(rule, key)) {
1448 		pr_err("%s:%d: keyfield %s does not belong in the rule keyset\n",
1449 		       __func__, __LINE__,
1450 		       vcap_keyfield_name(ri->vctrl, key));
1451 		return -EINVAL;
1452 	}
1453 
1454 	field = kzalloc(sizeof(*field), GFP_KERNEL);
1455 	if (!field)
1456 		return -ENOMEM;
1457 	field->ctrl.key = key;
1458 	field->ctrl.type = ftype;
1459 	vcap_copy_from_client_keyfield(rule, field, data);
1460 	list_add_tail(&field->ctrl.list, &rule->keyfields);
1461 	return 0;
1462 }
1463 
1464 static void vcap_rule_set_key_bitsize(struct vcap_u1_key *u1, enum vcap_bit val)
1465 {
1466 	switch (val) {
1467 	case VCAP_BIT_0:
1468 		u1->value = 0;
1469 		u1->mask = 1;
1470 		break;
1471 	case VCAP_BIT_1:
1472 		u1->value = 1;
1473 		u1->mask = 1;
1474 		break;
1475 	case VCAP_BIT_ANY:
1476 		u1->value = 0;
1477 		u1->mask = 0;
1478 		break;
1479 	}
1480 }
1481 
1482 /* Add a bit key with value and mask to the rule */
1483 int vcap_rule_add_key_bit(struct vcap_rule *rule, enum vcap_key_field key,
1484 			  enum vcap_bit val)
1485 {
1486 	struct vcap_client_keyfield_data data;
1487 
1488 	vcap_rule_set_key_bitsize(&data.u1, val);
1489 	return vcap_rule_add_key(rule, key, VCAP_FIELD_BIT, &data);
1490 }
1491 EXPORT_SYMBOL_GPL(vcap_rule_add_key_bit);
1492 
1493 /* Add a 32 bit key field with value and mask to the rule */
1494 int vcap_rule_add_key_u32(struct vcap_rule *rule, enum vcap_key_field key,
1495 			  u32 value, u32 mask)
1496 {
1497 	struct vcap_client_keyfield_data data;
1498 
1499 	data.u32.value = value;
1500 	data.u32.mask = mask;
1501 	return vcap_rule_add_key(rule, key, VCAP_FIELD_U32, &data);
1502 }
1503 EXPORT_SYMBOL_GPL(vcap_rule_add_key_u32);
1504 
1505 /* Add a 48 bit key with value and mask to the rule */
1506 int vcap_rule_add_key_u48(struct vcap_rule *rule, enum vcap_key_field key,
1507 			  struct vcap_u48_key *fieldval)
1508 {
1509 	struct vcap_client_keyfield_data data;
1510 
1511 	memcpy(&data.u48, fieldval, sizeof(data.u48));
1512 	return vcap_rule_add_key(rule, key, VCAP_FIELD_U48, &data);
1513 }
1514 EXPORT_SYMBOL_GPL(vcap_rule_add_key_u48);
1515 
1516 /* Add a 72 bit key with value and mask to the rule */
1517 int vcap_rule_add_key_u72(struct vcap_rule *rule, enum vcap_key_field key,
1518 			  struct vcap_u72_key *fieldval)
1519 {
1520 	struct vcap_client_keyfield_data data;
1521 
1522 	memcpy(&data.u72, fieldval, sizeof(data.u72));
1523 	return vcap_rule_add_key(rule, key, VCAP_FIELD_U72, &data);
1524 }
1525 EXPORT_SYMBOL_GPL(vcap_rule_add_key_u72);
1526 
1527 /* Add a 128 bit key with value and mask to the rule */
1528 int vcap_rule_add_key_u128(struct vcap_rule *rule, enum vcap_key_field key,
1529 			   struct vcap_u128_key *fieldval)
1530 {
1531 	struct vcap_client_keyfield_data data;
1532 
1533 	memcpy(&data.u128, fieldval, sizeof(data.u128));
1534 	return vcap_rule_add_key(rule, key, VCAP_FIELD_U128, &data);
1535 }
1536 EXPORT_SYMBOL_GPL(vcap_rule_add_key_u128);
1537 
1538 /* Find a client action field in a rule */
1539 static struct vcap_client_actionfield *
1540 vcap_find_actionfield(struct vcap_rule *rule, enum vcap_action_field act)
1541 {
1542 	struct vcap_rule_internal *ri = (struct vcap_rule_internal *)rule;
1543 	struct vcap_client_actionfield *caf;
1544 
1545 	list_for_each_entry(caf, &ri->data.actionfields, ctrl.list)
1546 		if (caf->ctrl.action == act)
1547 			return caf;
1548 	return NULL;
1549 }
1550 
1551 static void vcap_copy_from_client_actionfield(struct vcap_rule *rule,
1552 					      struct vcap_client_actionfield *field,
1553 					      struct vcap_client_actionfield_data *data)
1554 {
1555 	struct vcap_rule_internal *ri = to_intrule(rule);
1556 	int size;
1557 
1558 	if (!ri->admin->w32be) {
1559 		memcpy(&field->data, data, sizeof(field->data));
1560 		return;
1561 	}
1562 
1563 	size = actionfield_size_table[field->ctrl.type];
1564 	switch (field->ctrl.type) {
1565 	case VCAP_FIELD_BIT:
1566 	case VCAP_FIELD_U32:
1567 		memcpy(&field->data, data, sizeof(field->data));
1568 		break;
1569 	case VCAP_FIELD_U48:
1570 		vcap_copy_to_w32be(field->data.u48.value, data->u48.value, size);
1571 		break;
1572 	case VCAP_FIELD_U56:
1573 		vcap_copy_to_w32be(field->data.u56.value, data->u56.value, size);
1574 		break;
1575 	case VCAP_FIELD_U64:
1576 		vcap_copy_to_w32be(field->data.u64.value, data->u64.value, size);
1577 		break;
1578 	case VCAP_FIELD_U72:
1579 		vcap_copy_to_w32be(field->data.u72.value, data->u72.value, size);
1580 		break;
1581 	case VCAP_FIELD_U112:
1582 		vcap_copy_to_w32be(field->data.u112.value, data->u112.value, size);
1583 		break;
1584 	case VCAP_FIELD_U128:
1585 		vcap_copy_to_w32be(field->data.u128.value, data->u128.value, size);
1586 		break;
1587 	};
1588 }
1589 
1590 /* Check if the actionfield is already in the rule */
1591 static bool vcap_actionfield_unique(struct vcap_rule *rule,
1592 				    enum vcap_action_field act)
1593 {
1594 	struct vcap_rule_internal *ri = to_intrule(rule);
1595 	const struct vcap_client_actionfield *caf;
1596 
1597 	list_for_each_entry(caf, &ri->data.actionfields, ctrl.list)
1598 		if (caf->ctrl.action == act)
1599 			return false;
1600 	return true;
1601 }
1602 
1603 /* Check if the actionfield is in the actionset */
1604 static bool vcap_actionfield_match_actionset(struct vcap_rule *rule,
1605 					     enum vcap_action_field action)
1606 {
1607 	enum vcap_actionfield_set actionset = rule->actionset;
1608 	struct vcap_rule_internal *ri = to_intrule(rule);
1609 	enum vcap_type vt = ri->admin->vtype;
1610 	const struct vcap_field *fields;
1611 
1612 	/* the field is accepted if the rule has no actionset yet */
1613 	if (actionset == VCAP_AFS_NO_VALUE)
1614 		return true;
1615 	fields = vcap_actionfields(ri->vctrl, vt, actionset);
1616 	if (!fields)
1617 		return false;
1618 	/* if there is a width there is a way */
1619 	return fields[action].width > 0;
1620 }
1621 
1622 static int vcap_rule_add_action(struct vcap_rule *rule,
1623 				enum vcap_action_field action,
1624 				enum vcap_field_type ftype,
1625 				struct vcap_client_actionfield_data *data)
1626 {
1627 	struct vcap_rule_internal *ri = to_intrule(rule);
1628 	struct vcap_client_actionfield *field;
1629 
1630 	if (!vcap_actionfield_unique(rule, action)) {
1631 		pr_warn("%s:%d: actionfield %s is already in the rule\n",
1632 			__func__, __LINE__,
1633 			vcap_actionfield_name(ri->vctrl, action));
1634 		return -EINVAL;
1635 	}
1636 
1637 	if (!vcap_actionfield_match_actionset(rule, action)) {
1638 		pr_err("%s:%d: actionfield %s does not belong in the rule actionset\n",
1639 		       __func__, __LINE__,
1640 		       vcap_actionfield_name(ri->vctrl, action));
1641 		return -EINVAL;
1642 	}
1643 
1644 	field = kzalloc(sizeof(*field), GFP_KERNEL);
1645 	if (!field)
1646 		return -ENOMEM;
1647 	field->ctrl.action = action;
1648 	field->ctrl.type = ftype;
1649 	vcap_copy_from_client_actionfield(rule, field, data);
1650 	list_add_tail(&field->ctrl.list, &rule->actionfields);
1651 	return 0;
1652 }
1653 
1654 static void vcap_rule_set_action_bitsize(struct vcap_u1_action *u1,
1655 					 enum vcap_bit val)
1656 {
1657 	switch (val) {
1658 	case VCAP_BIT_0:
1659 		u1->value = 0;
1660 		break;
1661 	case VCAP_BIT_1:
1662 		u1->value = 1;
1663 		break;
1664 	case VCAP_BIT_ANY:
1665 		u1->value = 0;
1666 		break;
1667 	}
1668 }
1669 
1670 /* Add a bit action with value to the rule */
1671 int vcap_rule_add_action_bit(struct vcap_rule *rule,
1672 			     enum vcap_action_field action,
1673 			     enum vcap_bit val)
1674 {
1675 	struct vcap_client_actionfield_data data;
1676 
1677 	vcap_rule_set_action_bitsize(&data.u1, val);
1678 	return vcap_rule_add_action(rule, action, VCAP_FIELD_BIT, &data);
1679 }
1680 EXPORT_SYMBOL_GPL(vcap_rule_add_action_bit);
1681 
1682 /* Add a 32 bit action field with value to the rule */
1683 int vcap_rule_add_action_u32(struct vcap_rule *rule,
1684 			     enum vcap_action_field action,
1685 			     u32 value)
1686 {
1687 	struct vcap_client_actionfield_data data;
1688 
1689 	data.u32.value = value;
1690 	return vcap_rule_add_action(rule, action, VCAP_FIELD_U32, &data);
1691 }
1692 EXPORT_SYMBOL_GPL(vcap_rule_add_action_u32);
1693 
1694 static int vcap_read_counter(struct vcap_rule_internal *ri,
1695 			     struct vcap_counter *ctr)
1696 {
1697 	struct vcap_admin *admin = ri->admin;
1698 
1699 	ri->vctrl->ops->update(ri->ndev, admin, VCAP_CMD_READ, VCAP_SEL_COUNTER,
1700 			       ri->addr);
1701 	ri->vctrl->ops->cache_read(ri->ndev, admin, VCAP_SEL_COUNTER,
1702 				   ri->counter_id, 0);
1703 	ctr->value = admin->cache.counter;
1704 	ctr->sticky = admin->cache.sticky;
1705 	return 0;
1706 }
1707 
1708 /* Copy to host byte order */
1709 void vcap_netbytes_copy(u8 *dst, u8 *src, int count)
1710 {
1711 	int idx;
1712 
1713 	for (idx = 0; idx < count; ++idx, ++dst)
1714 		*dst = src[count - idx - 1];
1715 }
1716 EXPORT_SYMBOL_GPL(vcap_netbytes_copy);
1717 
1718 /* Convert validation error code into tc extact error message */
1719 void vcap_set_tc_exterr(struct flow_cls_offload *fco, struct vcap_rule *vrule)
1720 {
1721 	switch (vrule->exterr) {
1722 	case VCAP_ERR_NONE:
1723 		break;
1724 	case VCAP_ERR_NO_ADMIN:
1725 		NL_SET_ERR_MSG_MOD(fco->common.extack,
1726 				   "Missing VCAP instance");
1727 		break;
1728 	case VCAP_ERR_NO_NETDEV:
1729 		NL_SET_ERR_MSG_MOD(fco->common.extack,
1730 				   "Missing network interface");
1731 		break;
1732 	case VCAP_ERR_NO_KEYSET_MATCH:
1733 		NL_SET_ERR_MSG_MOD(fco->common.extack,
1734 				   "No keyset matched the filter keys");
1735 		break;
1736 	case VCAP_ERR_NO_ACTIONSET_MATCH:
1737 		NL_SET_ERR_MSG_MOD(fco->common.extack,
1738 				   "No actionset matched the filter actions");
1739 		break;
1740 	case VCAP_ERR_NO_PORT_KEYSET_MATCH:
1741 		NL_SET_ERR_MSG_MOD(fco->common.extack,
1742 				   "No port keyset matched the filter keys");
1743 		break;
1744 	}
1745 }
1746 EXPORT_SYMBOL_GPL(vcap_set_tc_exterr);
1747 
1748 /* Check if this port is already enabled for this VCAP instance */
1749 static bool vcap_is_enabled(struct vcap_admin *admin, struct net_device *ndev,
1750 			    unsigned long cookie)
1751 {
1752 	struct vcap_enabled_port *eport;
1753 
1754 	list_for_each_entry(eport, &admin->enabled, list)
1755 		if (eport->cookie == cookie || eport->ndev == ndev)
1756 			return true;
1757 
1758 	return false;
1759 }
1760 
1761 /* Enable this port for this VCAP instance */
1762 static int vcap_enable(struct vcap_admin *admin, struct net_device *ndev,
1763 		       unsigned long cookie)
1764 {
1765 	struct vcap_enabled_port *eport;
1766 
1767 	eport = kzalloc(sizeof(*eport), GFP_KERNEL);
1768 	if (!eport)
1769 		return -ENOMEM;
1770 
1771 	eport->ndev = ndev;
1772 	eport->cookie = cookie;
1773 	list_add_tail(&eport->list, &admin->enabled);
1774 
1775 	return 0;
1776 }
1777 
1778 /* Disable this port for this VCAP instance */
1779 static int vcap_disable(struct vcap_admin *admin, struct net_device *ndev,
1780 			unsigned long cookie)
1781 {
1782 	struct vcap_enabled_port *eport;
1783 
1784 	list_for_each_entry(eport, &admin->enabled, list) {
1785 		if (eport->cookie == cookie && eport->ndev == ndev) {
1786 			list_del(&eport->list);
1787 			kfree(eport);
1788 			return 0;
1789 		}
1790 	}
1791 
1792 	return -ENOENT;
1793 }
1794 
1795 /* Find the VCAP instance that enabled the port using a specific filter */
1796 static struct vcap_admin *vcap_find_admin_by_cookie(struct vcap_control *vctrl,
1797 						    unsigned long cookie)
1798 {
1799 	struct vcap_enabled_port *eport;
1800 	struct vcap_admin *admin;
1801 
1802 	list_for_each_entry(admin, &vctrl->list, list)
1803 		list_for_each_entry(eport, &admin->enabled, list)
1804 			if (eport->cookie == cookie)
1805 				return admin;
1806 
1807 	return NULL;
1808 }
1809 
1810 /* Enable/Disable the VCAP instance lookups. Chain id 0 means disable */
1811 int vcap_enable_lookups(struct vcap_control *vctrl, struct net_device *ndev,
1812 			int chain_id, unsigned long cookie, bool enable)
1813 {
1814 	struct vcap_admin *admin;
1815 	int err;
1816 
1817 	err = vcap_api_check(vctrl);
1818 	if (err)
1819 		return err;
1820 
1821 	if (!ndev)
1822 		return -ENODEV;
1823 
1824 	if (chain_id)
1825 		admin = vcap_find_admin(vctrl, chain_id);
1826 	else
1827 		admin = vcap_find_admin_by_cookie(vctrl, cookie);
1828 	if (!admin)
1829 		return -ENOENT;
1830 
1831 	/* first instance and first chain */
1832 	if (admin->vinst || chain_id > admin->first_cid)
1833 		return -EFAULT;
1834 
1835 	err = vctrl->ops->enable(ndev, admin, enable);
1836 	if (err)
1837 		return err;
1838 
1839 	if (chain_id) {
1840 		if (vcap_is_enabled(admin, ndev, cookie))
1841 			return -EADDRINUSE;
1842 		mutex_lock(&admin->lock);
1843 		vcap_enable(admin, ndev, cookie);
1844 	} else {
1845 		mutex_lock(&admin->lock);
1846 		vcap_disable(admin, ndev, cookie);
1847 	}
1848 	mutex_unlock(&admin->lock);
1849 
1850 	return 0;
1851 }
1852 EXPORT_SYMBOL_GPL(vcap_enable_lookups);
1853 
1854 /* Set a rule counter id (for certain vcaps only) */
1855 void vcap_rule_set_counter_id(struct vcap_rule *rule, u32 counter_id)
1856 {
1857 	struct vcap_rule_internal *ri = to_intrule(rule);
1858 
1859 	ri->counter_id = counter_id;
1860 }
1861 EXPORT_SYMBOL_GPL(vcap_rule_set_counter_id);
1862 
1863 /* Provide all rules via a callback interface */
1864 int vcap_rule_iter(struct vcap_control *vctrl,
1865 		   int (*callback)(void *, struct vcap_rule *), void *arg)
1866 {
1867 	struct vcap_rule_internal *ri;
1868 	struct vcap_admin *admin;
1869 	int ret;
1870 
1871 	ret = vcap_api_check(vctrl);
1872 	if (ret)
1873 		return ret;
1874 
1875 	/* Iterate all rules in each VCAP instance */
1876 	list_for_each_entry(admin, &vctrl->list, list) {
1877 		list_for_each_entry(ri, &admin->rules, list) {
1878 			ret = callback(arg, &ri->data);
1879 			if (ret)
1880 				return ret;
1881 		}
1882 	}
1883 
1884 	return 0;
1885 }
1886 EXPORT_SYMBOL_GPL(vcap_rule_iter);
1887 
1888 int vcap_rule_set_counter(struct vcap_rule *rule, struct vcap_counter *ctr)
1889 {
1890 	struct vcap_rule_internal *ri = to_intrule(rule);
1891 	int err;
1892 
1893 	err = vcap_api_check(ri->vctrl);
1894 	if (err)
1895 		return err;
1896 	if (!ctr) {
1897 		pr_err("%s:%d: counter is missing\n", __func__, __LINE__);
1898 		return -EINVAL;
1899 	}
1900 	return vcap_write_counter(ri, ctr);
1901 }
1902 EXPORT_SYMBOL_GPL(vcap_rule_set_counter);
1903 
1904 int vcap_rule_get_counter(struct vcap_rule *rule, struct vcap_counter *ctr)
1905 {
1906 	struct vcap_rule_internal *ri = to_intrule(rule);
1907 	int err;
1908 
1909 	err = vcap_api_check(ri->vctrl);
1910 	if (err)
1911 		return err;
1912 	if (!ctr) {
1913 		pr_err("%s:%d: counter is missing\n", __func__, __LINE__);
1914 		return -EINVAL;
1915 	}
1916 	return vcap_read_counter(ri, ctr);
1917 }
1918 EXPORT_SYMBOL_GPL(vcap_rule_get_counter);
1919 
1920 static int vcap_rule_mod_key(struct vcap_rule *rule,
1921 			     enum vcap_key_field key,
1922 			     enum vcap_field_type ftype,
1923 			     struct vcap_client_keyfield_data *data)
1924 {
1925 	struct vcap_client_keyfield *field;
1926 
1927 	field = vcap_find_keyfield(rule, key);
1928 	if (!field)
1929 		return vcap_rule_add_key(rule, key, ftype, data);
1930 	vcap_copy_from_client_keyfield(rule, field, data);
1931 	return 0;
1932 }
1933 
1934 /* Modify a 32 bit key field with value and mask in the rule */
1935 int vcap_rule_mod_key_u32(struct vcap_rule *rule, enum vcap_key_field key,
1936 			  u32 value, u32 mask)
1937 {
1938 	struct vcap_client_keyfield_data data;
1939 
1940 	data.u32.value = value;
1941 	data.u32.mask = mask;
1942 	return vcap_rule_mod_key(rule, key, VCAP_FIELD_U32, &data);
1943 }
1944 EXPORT_SYMBOL_GPL(vcap_rule_mod_key_u32);
1945 
1946 static int vcap_rule_mod_action(struct vcap_rule *rule,
1947 				enum vcap_action_field action,
1948 				enum vcap_field_type ftype,
1949 				struct vcap_client_actionfield_data *data)
1950 {
1951 	struct vcap_client_actionfield *field;
1952 
1953 	field = vcap_find_actionfield(rule, action);
1954 	if (!field)
1955 		return vcap_rule_add_action(rule, action, ftype, data);
1956 	vcap_copy_from_client_actionfield(rule, field, data);
1957 	return 0;
1958 }
1959 
1960 /* Modify a 32 bit action field with value in the rule */
1961 int vcap_rule_mod_action_u32(struct vcap_rule *rule,
1962 			     enum vcap_action_field action,
1963 			     u32 value)
1964 {
1965 	struct vcap_client_actionfield_data data;
1966 
1967 	data.u32.value = value;
1968 	return vcap_rule_mod_action(rule, action, VCAP_FIELD_U32, &data);
1969 }
1970 EXPORT_SYMBOL_GPL(vcap_rule_mod_action_u32);
1971 
1972 /* Drop keys in a keylist and any keys that are not supported by the keyset */
1973 int vcap_filter_rule_keys(struct vcap_rule *rule,
1974 			  enum vcap_key_field keylist[], int length,
1975 			  bool drop_unsupported)
1976 {
1977 	struct vcap_rule_internal *ri = to_intrule(rule);
1978 	struct vcap_client_keyfield *ckf, *next_ckf;
1979 	const struct vcap_field *fields;
1980 	enum vcap_key_field key;
1981 	int err = 0;
1982 	int idx;
1983 
1984 	if (length > 0) {
1985 		err = -EEXIST;
1986 		list_for_each_entry_safe(ckf, next_ckf,
1987 					 &ri->data.keyfields, ctrl.list) {
1988 			key = ckf->ctrl.key;
1989 			for (idx = 0; idx < length; ++idx)
1990 				if (key == keylist[idx]) {
1991 					list_del(&ckf->ctrl.list);
1992 					kfree(ckf);
1993 					idx++;
1994 					err = 0;
1995 				}
1996 		}
1997 	}
1998 	if (drop_unsupported) {
1999 		err = -EEXIST;
2000 		fields = vcap_keyfields(ri->vctrl, ri->admin->vtype,
2001 					rule->keyset);
2002 		if (!fields)
2003 			return err;
2004 		list_for_each_entry_safe(ckf, next_ckf,
2005 					 &ri->data.keyfields, ctrl.list) {
2006 			key = ckf->ctrl.key;
2007 			if (fields[key].width == 0) {
2008 				list_del(&ckf->ctrl.list);
2009 				kfree(ckf);
2010 				err = 0;
2011 			}
2012 		}
2013 	}
2014 	return err;
2015 }
2016 EXPORT_SYMBOL_GPL(vcap_filter_rule_keys);
2017 
2018 /* Make a full copy of an existing rule with a new rule id */
2019 struct vcap_rule *vcap_copy_rule(struct vcap_rule *erule)
2020 {
2021 	struct vcap_rule_internal *ri = to_intrule(erule);
2022 	struct vcap_client_actionfield *caf;
2023 	struct vcap_client_keyfield *ckf;
2024 	struct vcap_rule *rule;
2025 	int err;
2026 
2027 	err = vcap_api_check(ri->vctrl);
2028 	if (err)
2029 		return ERR_PTR(err);
2030 
2031 	rule = vcap_alloc_rule(ri->vctrl, ri->ndev, ri->data.vcap_chain_id,
2032 			       ri->data.user, ri->data.priority, 0);
2033 	if (IS_ERR(rule))
2034 		return rule;
2035 
2036 	list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list) {
2037 		/* Add a key duplicate in the new rule */
2038 		err = vcap_rule_add_key(rule,
2039 					ckf->ctrl.key,
2040 					ckf->ctrl.type,
2041 					&ckf->data);
2042 		if (err)
2043 			goto err;
2044 	}
2045 
2046 	list_for_each_entry(caf, &ri->data.actionfields, ctrl.list) {
2047 		/* Add a action duplicate in the new rule */
2048 		err = vcap_rule_add_action(rule,
2049 					   caf->ctrl.action,
2050 					   caf->ctrl.type,
2051 					   &caf->data);
2052 		if (err)
2053 			goto err;
2054 	}
2055 	return rule;
2056 err:
2057 	vcap_free_rule(rule);
2058 	return ERR_PTR(err);
2059 }
2060 EXPORT_SYMBOL_GPL(vcap_copy_rule);
2061 
2062 #ifdef CONFIG_VCAP_KUNIT_TEST
2063 #include "vcap_api_kunit.c"
2064 #endif
2065