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