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 };
29 
30 /* Moving a rule in the VCAP address space */
31 struct vcap_rule_move {
32 	int addr; /* address to move */
33 	int offset; /* change in address */
34 	int count; /* blocksize of addresses to move */
35 };
36 
37 /* Bit iterator for the VCAP cache streams */
38 struct vcap_stream_iter {
39 	u32 offset; /* bit offset from the stream start */
40 	u32 sw_width; /* subword width in bits */
41 	u32 regs_per_sw; /* registers per subword */
42 	u32 reg_idx; /* current register index */
43 	u32 reg_bitpos; /* bit offset in current register */
44 	const struct vcap_typegroup *tg; /* current typegroup */
45 };
46 
47 static 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 static 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 static 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 static 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 static 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 static 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 
199 /* Return the typegroup table for the matching keyset (using subword size) */
200 static const struct vcap_typegroup *
201 vcap_keyfield_typegroup(struct vcap_control *vctrl,
202 			enum vcap_type vt, enum vcap_keyfield_set keyset)
203 {
204 	const struct vcap_set *kset = vcap_keyfieldset(vctrl, vt, keyset);
205 
206 	/* Check that the keyset is valid */
207 	if (!kset)
208 		return NULL;
209 	return vctrl->vcaps[vt].keyfield_set_typegroups[kset->sw_per_item];
210 }
211 
212 /* Return the number of keyfields in the keyset */
213 static int vcap_keyfield_count(struct vcap_control *vctrl,
214 			       enum vcap_type vt, enum vcap_keyfield_set keyset)
215 {
216 	/* Check that the keyset exists in the vcap keyset list */
217 	if (keyset >= vctrl->vcaps[vt].keyfield_set_size)
218 		return 0;
219 	return vctrl->vcaps[vt].keyfield_set_map_size[keyset];
220 }
221 
222 static void vcap_encode_keyfield(struct vcap_rule_internal *ri,
223 				 const struct vcap_client_keyfield *kf,
224 				 const struct vcap_field *rf,
225 				 const struct vcap_typegroup *tgt)
226 {
227 	int sw_width = ri->vctrl->vcaps[ri->admin->vtype].sw_width;
228 	struct vcap_cache_data *cache = &ri->admin->cache;
229 	struct vcap_stream_iter iter;
230 	const u8 *value, *mask;
231 
232 	/* Encode the fields for the key and the mask in their respective
233 	 * streams, respecting the subword width.
234 	 */
235 	switch (kf->ctrl.type) {
236 	case VCAP_FIELD_BIT:
237 		value = &kf->data.u1.value;
238 		mask = &kf->data.u1.mask;
239 		break;
240 	case VCAP_FIELD_U32:
241 		value = (const u8 *)&kf->data.u32.value;
242 		mask = (const u8 *)&kf->data.u32.mask;
243 		break;
244 	case VCAP_FIELD_U48:
245 		value = kf->data.u48.value;
246 		mask = kf->data.u48.mask;
247 		break;
248 	case VCAP_FIELD_U56:
249 		value = kf->data.u56.value;
250 		mask = kf->data.u56.mask;
251 		break;
252 	case VCAP_FIELD_U64:
253 		value = kf->data.u64.value;
254 		mask = kf->data.u64.mask;
255 		break;
256 	case VCAP_FIELD_U72:
257 		value = kf->data.u72.value;
258 		mask = kf->data.u72.mask;
259 		break;
260 	case VCAP_FIELD_U112:
261 		value = kf->data.u112.value;
262 		mask = kf->data.u112.mask;
263 		break;
264 	case VCAP_FIELD_U128:
265 		value = kf->data.u128.value;
266 		mask = kf->data.u128.mask;
267 		break;
268 	}
269 	vcap_iter_init(&iter, sw_width, tgt, rf->offset);
270 	vcap_encode_field(cache->keystream, &iter, rf->width, value);
271 	vcap_iter_init(&iter, sw_width, tgt, rf->offset);
272 	vcap_encode_field(cache->maskstream, &iter, rf->width, mask);
273 }
274 
275 static void vcap_encode_keyfield_typegroups(struct vcap_control *vctrl,
276 					    struct vcap_rule_internal *ri,
277 					    const struct vcap_typegroup *tgt)
278 {
279 	int sw_width = vctrl->vcaps[ri->admin->vtype].sw_width;
280 	struct vcap_cache_data *cache = &ri->admin->cache;
281 
282 	/* Encode the typegroup bits for the key and the mask in their streams,
283 	 * respecting the subword width.
284 	 */
285 	vcap_encode_typegroups(cache->keystream, sw_width, tgt, false);
286 	vcap_encode_typegroups(cache->maskstream, sw_width, tgt, true);
287 }
288 
289 static int vcap_encode_rule_keyset(struct vcap_rule_internal *ri)
290 {
291 	const struct vcap_client_keyfield *ckf;
292 	const struct vcap_typegroup *tg_table;
293 	const struct vcap_field *kf_table;
294 	int keyset_size;
295 
296 	/* Get a valid set of fields for the specific keyset */
297 	kf_table = vcap_keyfields(ri->vctrl, ri->admin->vtype, ri->data.keyset);
298 	if (!kf_table) {
299 		pr_err("%s:%d: no fields available for this keyset: %d\n",
300 		       __func__, __LINE__, ri->data.keyset);
301 		return -EINVAL;
302 	}
303 	/* Get a valid typegroup for the specific keyset */
304 	tg_table = vcap_keyfield_typegroup(ri->vctrl, ri->admin->vtype,
305 					   ri->data.keyset);
306 	if (!tg_table) {
307 		pr_err("%s:%d: no typegroups available for this keyset: %d\n",
308 		       __func__, __LINE__, ri->data.keyset);
309 		return -EINVAL;
310 	}
311 	/* Get a valid size for the specific keyset */
312 	keyset_size = vcap_keyfield_count(ri->vctrl, ri->admin->vtype,
313 					  ri->data.keyset);
314 	if (keyset_size == 0) {
315 		pr_err("%s:%d: zero field count for this keyset: %d\n",
316 		       __func__, __LINE__, ri->data.keyset);
317 		return -EINVAL;
318 	}
319 	/* Iterate over the keyfields (key, mask) in the rule
320 	 * and encode these bits
321 	 */
322 	if (list_empty(&ri->data.keyfields)) {
323 		pr_err("%s:%d: no keyfields in the rule\n", __func__, __LINE__);
324 		return -EINVAL;
325 	}
326 	list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list) {
327 		/* Check that the client entry exists in the keyset */
328 		if (ckf->ctrl.key >= keyset_size) {
329 			pr_err("%s:%d: key %d is not in vcap\n",
330 			       __func__, __LINE__, ckf->ctrl.key);
331 			return -EINVAL;
332 		}
333 		vcap_encode_keyfield(ri, ckf, &kf_table[ckf->ctrl.key], tg_table);
334 	}
335 	/* Add typegroup bits to the key/mask bitstreams */
336 	vcap_encode_keyfield_typegroups(ri->vctrl, ri, tg_table);
337 	return 0;
338 }
339 
340 /* Return the list of actionfields for the actionset */
341 static const struct vcap_field *
342 vcap_actionfields(struct vcap_control *vctrl,
343 		  enum vcap_type vt, enum vcap_actionfield_set actionset)
344 {
345 	/* Check that the actionset exists in the vcap actionset list */
346 	if (actionset >= vctrl->vcaps[vt].actionfield_set_size)
347 		return NULL;
348 	return vctrl->vcaps[vt].actionfield_set_map[actionset];
349 }
350 
351 static const struct vcap_set *
352 vcap_actionfieldset(struct vcap_control *vctrl,
353 		    enum vcap_type vt, enum vcap_actionfield_set actionset)
354 {
355 	const struct vcap_set *aset;
356 
357 	/* Check that the actionset exists in the vcap actionset list */
358 	if (actionset >= vctrl->vcaps[vt].actionfield_set_size)
359 		return NULL;
360 	aset = &vctrl->vcaps[vt].actionfield_set[actionset];
361 	if (aset->sw_per_item == 0 || aset->sw_per_item > vctrl->vcaps[vt].sw_count)
362 		return NULL;
363 	return aset;
364 }
365 
366 /* Return the typegroup table for the matching actionset (using subword size) */
367 static const struct vcap_typegroup *
368 vcap_actionfield_typegroup(struct vcap_control *vctrl,
369 			   enum vcap_type vt, enum vcap_actionfield_set actionset)
370 {
371 	const struct vcap_set *aset = vcap_actionfieldset(vctrl, vt, actionset);
372 
373 	/* Check that the actionset is valid */
374 	if (!aset)
375 		return NULL;
376 	return vctrl->vcaps[vt].actionfield_set_typegroups[aset->sw_per_item];
377 }
378 
379 /* Return the number of actionfields in the actionset */
380 static int vcap_actionfield_count(struct vcap_control *vctrl,
381 				  enum vcap_type vt,
382 				  enum vcap_actionfield_set actionset)
383 {
384 	/* Check that the actionset exists in the vcap actionset list */
385 	if (actionset >= vctrl->vcaps[vt].actionfield_set_size)
386 		return 0;
387 	return vctrl->vcaps[vt].actionfield_set_map_size[actionset];
388 }
389 
390 static void vcap_encode_actionfield(struct vcap_rule_internal *ri,
391 				    const struct vcap_client_actionfield *af,
392 				    const struct vcap_field *rf,
393 				    const struct vcap_typegroup *tgt)
394 {
395 	int act_width = ri->vctrl->vcaps[ri->admin->vtype].act_width;
396 
397 	struct vcap_cache_data *cache = &ri->admin->cache;
398 	struct vcap_stream_iter iter;
399 	const u8 *value;
400 
401 	/* Encode the action field in the stream, respecting the subword width */
402 	switch (af->ctrl.type) {
403 	case VCAP_FIELD_BIT:
404 		value = &af->data.u1.value;
405 		break;
406 	case VCAP_FIELD_U32:
407 		value = (const u8 *)&af->data.u32.value;
408 		break;
409 	case VCAP_FIELD_U48:
410 		value = af->data.u48.value;
411 		break;
412 	case VCAP_FIELD_U56:
413 		value = af->data.u56.value;
414 		break;
415 	case VCAP_FIELD_U64:
416 		value = af->data.u64.value;
417 		break;
418 	case VCAP_FIELD_U72:
419 		value = af->data.u72.value;
420 		break;
421 	case VCAP_FIELD_U112:
422 		value = af->data.u112.value;
423 		break;
424 	case VCAP_FIELD_U128:
425 		value = af->data.u128.value;
426 		break;
427 	}
428 	vcap_iter_init(&iter, act_width, tgt, rf->offset);
429 	vcap_encode_field(cache->actionstream, &iter, rf->width, value);
430 }
431 
432 static void vcap_encode_actionfield_typegroups(struct vcap_rule_internal *ri,
433 					       const struct vcap_typegroup *tgt)
434 {
435 	int sw_width = ri->vctrl->vcaps[ri->admin->vtype].act_width;
436 	struct vcap_cache_data *cache = &ri->admin->cache;
437 
438 	/* Encode the typegroup bits for the actionstream respecting the subword
439 	 * width.
440 	 */
441 	vcap_encode_typegroups(cache->actionstream, sw_width, tgt, false);
442 }
443 
444 static int vcap_encode_rule_actionset(struct vcap_rule_internal *ri)
445 {
446 	const struct vcap_client_actionfield *caf;
447 	const struct vcap_typegroup *tg_table;
448 	const struct vcap_field *af_table;
449 	int actionset_size;
450 
451 	/* Get a valid set of actionset fields for the specific actionset */
452 	af_table = vcap_actionfields(ri->vctrl, ri->admin->vtype,
453 				     ri->data.actionset);
454 	if (!af_table) {
455 		pr_err("%s:%d: no fields available for this actionset: %d\n",
456 		       __func__, __LINE__, ri->data.actionset);
457 		return -EINVAL;
458 	}
459 	/* Get a valid typegroup for the specific actionset */
460 	tg_table = vcap_actionfield_typegroup(ri->vctrl, ri->admin->vtype,
461 					      ri->data.actionset);
462 	if (!tg_table) {
463 		pr_err("%s:%d: no typegroups available for this actionset: %d\n",
464 		       __func__, __LINE__, ri->data.actionset);
465 		return -EINVAL;
466 	}
467 	/* Get a valid actionset size for the specific actionset */
468 	actionset_size = vcap_actionfield_count(ri->vctrl, ri->admin->vtype,
469 						ri->data.actionset);
470 	if (actionset_size == 0) {
471 		pr_err("%s:%d: zero field count for this actionset: %d\n",
472 		       __func__, __LINE__, ri->data.actionset);
473 		return -EINVAL;
474 	}
475 	/* Iterate over the actionfields in the rule
476 	 * and encode these bits
477 	 */
478 	if (list_empty(&ri->data.actionfields))
479 		pr_warn("%s:%d: no actionfields in the rule\n",
480 			__func__, __LINE__);
481 	list_for_each_entry(caf, &ri->data.actionfields, ctrl.list) {
482 		/* Check that the client action exists in the actionset */
483 		if (caf->ctrl.action >= actionset_size) {
484 			pr_err("%s:%d: action %d is not in vcap\n",
485 			       __func__, __LINE__, caf->ctrl.action);
486 			return -EINVAL;
487 		}
488 		vcap_encode_actionfield(ri, caf, &af_table[caf->ctrl.action],
489 					tg_table);
490 	}
491 	/* Add typegroup bits to the entry bitstreams */
492 	vcap_encode_actionfield_typegroups(ri, tg_table);
493 	return 0;
494 }
495 
496 static int vcap_encode_rule(struct vcap_rule_internal *ri)
497 {
498 	int err;
499 
500 	err = vcap_encode_rule_keyset(ri);
501 	if (err)
502 		return err;
503 	err = vcap_encode_rule_actionset(ri);
504 	if (err)
505 		return err;
506 	return 0;
507 }
508 
509 static int vcap_api_check(struct vcap_control *ctrl)
510 {
511 	if (!ctrl) {
512 		pr_err("%s:%d: vcap control is missing\n", __func__, __LINE__);
513 		return -EINVAL;
514 	}
515 	if (!ctrl->ops || !ctrl->ops->validate_keyset ||
516 	    !ctrl->ops->add_default_fields || !ctrl->ops->cache_erase ||
517 	    !ctrl->ops->cache_write || !ctrl->ops->cache_read ||
518 	    !ctrl->ops->init || !ctrl->ops->update || !ctrl->ops->move ||
519 	    !ctrl->ops->port_info) {
520 		pr_err("%s:%d: client operations are missing\n",
521 		       __func__, __LINE__);
522 		return -ENOENT;
523 	}
524 	return 0;
525 }
526 
527 static void vcap_erase_cache(struct vcap_rule_internal *ri)
528 {
529 	ri->vctrl->ops->cache_erase(ri->admin);
530 }
531 
532 /* Update the keyset for the rule */
533 int vcap_set_rule_set_keyset(struct vcap_rule *rule,
534 			     enum vcap_keyfield_set keyset)
535 {
536 	struct vcap_rule_internal *ri = to_intrule(rule);
537 	const struct vcap_set *kset;
538 	int sw_width;
539 
540 	kset = vcap_keyfieldset(ri->vctrl, ri->admin->vtype, keyset);
541 	/* Check that the keyset is valid */
542 	if (!kset)
543 		return -EINVAL;
544 	ri->keyset_sw = kset->sw_per_item;
545 	sw_width = ri->vctrl->vcaps[ri->admin->vtype].sw_width;
546 	ri->keyset_sw_regs = DIV_ROUND_UP(sw_width, 32);
547 	ri->data.keyset = keyset;
548 	return 0;
549 }
550 EXPORT_SYMBOL_GPL(vcap_set_rule_set_keyset);
551 
552 /* Update the actionset for the rule */
553 int vcap_set_rule_set_actionset(struct vcap_rule *rule,
554 				enum vcap_actionfield_set actionset)
555 {
556 	struct vcap_rule_internal *ri = to_intrule(rule);
557 	const struct vcap_set *aset;
558 	int act_width;
559 
560 	aset = vcap_actionfieldset(ri->vctrl, ri->admin->vtype, actionset);
561 	/* Check that the actionset is valid */
562 	if (!aset)
563 		return -EINVAL;
564 	ri->actionset_sw = aset->sw_per_item;
565 	act_width = ri->vctrl->vcaps[ri->admin->vtype].act_width;
566 	ri->actionset_sw_regs = DIV_ROUND_UP(act_width, 32);
567 	ri->data.actionset = actionset;
568 	return 0;
569 }
570 EXPORT_SYMBOL_GPL(vcap_set_rule_set_actionset);
571 
572 /* Find a rule with a provided rule id */
573 static struct vcap_rule_internal *vcap_lookup_rule(struct vcap_control *vctrl,
574 						   u32 id)
575 {
576 	struct vcap_rule_internal *ri;
577 	struct vcap_admin *admin;
578 
579 	/* Look for the rule id in all vcaps */
580 	list_for_each_entry(admin, &vctrl->list, list)
581 		list_for_each_entry(ri, &admin->rules, list)
582 			if (ri->data.id == id)
583 				return ri;
584 	return NULL;
585 }
586 
587 /* Find a rule id with a provided cookie */
588 int vcap_lookup_rule_by_cookie(struct vcap_control *vctrl, u64 cookie)
589 {
590 	struct vcap_rule_internal *ri;
591 	struct vcap_admin *admin;
592 
593 	/* Look for the rule id in all vcaps */
594 	list_for_each_entry(admin, &vctrl->list, list)
595 		list_for_each_entry(ri, &admin->rules, list)
596 			if (ri->data.cookie == cookie)
597 				return ri->data.id;
598 	return -ENOENT;
599 }
600 EXPORT_SYMBOL_GPL(vcap_lookup_rule_by_cookie);
601 
602 /* Make a shallow copy of the rule without the fields */
603 static struct vcap_rule_internal *vcap_dup_rule(struct vcap_rule_internal *ri)
604 {
605 	struct vcap_rule_internal *duprule;
606 
607 	/* Allocate the client part */
608 	duprule = kzalloc(sizeof(*duprule), GFP_KERNEL);
609 	if (!duprule)
610 		return ERR_PTR(-ENOMEM);
611 	*duprule = *ri;
612 	/* Not inserted in the VCAP */
613 	INIT_LIST_HEAD(&duprule->list);
614 	/* No elements in these lists */
615 	INIT_LIST_HEAD(&duprule->data.keyfields);
616 	INIT_LIST_HEAD(&duprule->data.actionfields);
617 	return duprule;
618 }
619 
620 /* Write VCAP cache content to the VCAP HW instance */
621 static int vcap_write_rule(struct vcap_rule_internal *ri)
622 {
623 	struct vcap_admin *admin = ri->admin;
624 	int sw_idx, ent_idx = 0, act_idx = 0;
625 	u32 addr = ri->addr;
626 
627 	if (!ri->size || !ri->keyset_sw_regs || !ri->actionset_sw_regs) {
628 		pr_err("%s:%d: rule is empty\n", __func__, __LINE__);
629 		return -EINVAL;
630 	}
631 	/* Use the values in the streams to write the VCAP cache */
632 	for (sw_idx = 0; sw_idx < ri->size; sw_idx++, addr++) {
633 		ri->vctrl->ops->cache_write(ri->ndev, admin,
634 					    VCAP_SEL_ENTRY, ent_idx,
635 					    ri->keyset_sw_regs);
636 		ri->vctrl->ops->cache_write(ri->ndev, admin,
637 					    VCAP_SEL_ACTION, act_idx,
638 					    ri->actionset_sw_regs);
639 		ri->vctrl->ops->update(ri->ndev, admin, VCAP_CMD_WRITE,
640 				       VCAP_SEL_ALL, addr);
641 		ent_idx += ri->keyset_sw_regs;
642 		act_idx += ri->actionset_sw_regs;
643 	}
644 	return 0;
645 }
646 
647 /* Lookup a vcap instance using chain id */
648 struct vcap_admin *vcap_find_admin(struct vcap_control *vctrl, int cid)
649 {
650 	struct vcap_admin *admin;
651 
652 	if (vcap_api_check(vctrl))
653 		return NULL;
654 
655 	list_for_each_entry(admin, &vctrl->list, list) {
656 		if (cid >= admin->first_cid && cid <= admin->last_cid)
657 			return admin;
658 	}
659 	return NULL;
660 }
661 EXPORT_SYMBOL_GPL(vcap_find_admin);
662 
663 /* Check if there is room for a new rule */
664 static int vcap_rule_space(struct vcap_admin *admin, int size)
665 {
666 	if (admin->last_used_addr - size < admin->first_valid_addr) {
667 		pr_err("%s:%d: No room for rule size: %u, %u\n",
668 		       __func__, __LINE__, size, admin->first_valid_addr);
669 		return -ENOSPC;
670 	}
671 	return 0;
672 }
673 
674 /* Add the keyset typefield to the list of rule keyfields */
675 static int vcap_add_type_keyfield(struct vcap_rule *rule)
676 {
677 	struct vcap_rule_internal *ri = to_intrule(rule);
678 	enum vcap_keyfield_set keyset = rule->keyset;
679 	enum vcap_type vt = ri->admin->vtype;
680 	const struct vcap_field *fields;
681 	const struct vcap_set *kset;
682 	int ret = -EINVAL;
683 
684 	kset = vcap_keyfieldset(ri->vctrl, vt, keyset);
685 	if (!kset)
686 		return ret;
687 	if (kset->type_id == (u8)-1)  /* No type field is needed */
688 		return 0;
689 
690 	fields = vcap_keyfields(ri->vctrl, vt, keyset);
691 	if (!fields)
692 		return -EINVAL;
693 	if (fields[VCAP_KF_TYPE].width > 1) {
694 		ret = vcap_rule_add_key_u32(rule, VCAP_KF_TYPE,
695 					    kset->type_id, 0xff);
696 	} else {
697 		if (kset->type_id)
698 			ret = vcap_rule_add_key_bit(rule, VCAP_KF_TYPE,
699 						    VCAP_BIT_1);
700 		else
701 			ret = vcap_rule_add_key_bit(rule, VCAP_KF_TYPE,
702 						    VCAP_BIT_0);
703 	}
704 	return 0;
705 }
706 
707 /* Validate a rule with respect to available port keys */
708 int vcap_val_rule(struct vcap_rule *rule, u16 l3_proto)
709 {
710 	struct vcap_rule_internal *ri = to_intrule(rule);
711 	enum vcap_keyfield_set keysets[10];
712 	struct vcap_keyset_list kslist;
713 	int ret;
714 
715 	/* This validation will be much expanded later */
716 	ret = vcap_api_check(ri->vctrl);
717 	if (ret)
718 		return ret;
719 	if (!ri->admin) {
720 		ri->data.exterr = VCAP_ERR_NO_ADMIN;
721 		return -EINVAL;
722 	}
723 	if (!ri->ndev) {
724 		ri->data.exterr = VCAP_ERR_NO_NETDEV;
725 		return -EINVAL;
726 	}
727 	if (ri->data.keyset == VCAP_KFS_NO_VALUE) {
728 		ri->data.exterr = VCAP_ERR_NO_KEYSET_MATCH;
729 		return -EINVAL;
730 	}
731 	/* prepare for keyset validation */
732 	keysets[0] = ri->data.keyset;
733 	kslist.keysets = keysets;
734 	kslist.cnt = 1;
735 	/* Pick a keyset that is supported in the port lookups */
736 	ret = ri->vctrl->ops->validate_keyset(ri->ndev, ri->admin, rule, &kslist,
737 					      l3_proto);
738 	if (ret < 0) {
739 		pr_err("%s:%d: keyset validation failed: %d\n",
740 		       __func__, __LINE__, ret);
741 		ri->data.exterr = VCAP_ERR_NO_PORT_KEYSET_MATCH;
742 		return ret;
743 	}
744 	if (ri->data.actionset == VCAP_AFS_NO_VALUE) {
745 		ri->data.exterr = VCAP_ERR_NO_ACTIONSET_MATCH;
746 		return -EINVAL;
747 	}
748 	vcap_add_type_keyfield(rule);
749 	/* Add default fields to this rule */
750 	ri->vctrl->ops->add_default_fields(ri->ndev, ri->admin, rule);
751 
752 	/* Rule size is the maximum of the entry and action subword count */
753 	ri->size = max(ri->keyset_sw, ri->actionset_sw);
754 
755 	/* Finally check if there is room for the rule in the VCAP */
756 	return vcap_rule_space(ri->admin, ri->size);
757 }
758 EXPORT_SYMBOL_GPL(vcap_val_rule);
759 
760 /* calculate the address of the next rule after this (lower address and prio) */
761 static u32 vcap_next_rule_addr(u32 addr, struct vcap_rule_internal *ri)
762 {
763 	return ((addr - ri->size) /  ri->size) * ri->size;
764 }
765 
766 /* Assign a unique rule id and autogenerate one if id == 0 */
767 static u32 vcap_set_rule_id(struct vcap_rule_internal *ri)
768 {
769 	u32 next_id;
770 
771 	if (ri->data.id != 0)
772 		return ri->data.id;
773 
774 	next_id = ri->vctrl->rule_id + 1;
775 
776 	for (next_id = ri->vctrl->rule_id + 1; next_id < ~0; ++next_id) {
777 		if (!vcap_lookup_rule(ri->vctrl, next_id)) {
778 			ri->data.id = next_id;
779 			ri->vctrl->rule_id = next_id;
780 			break;
781 		}
782 	}
783 	return ri->data.id;
784 }
785 
786 static int vcap_insert_rule(struct vcap_rule_internal *ri,
787 			    struct vcap_rule_move *move)
788 {
789 	struct vcap_admin *admin = ri->admin;
790 	struct vcap_rule_internal *duprule;
791 
792 	/* Only support appending rules for now */
793 	ri->addr = vcap_next_rule_addr(admin->last_used_addr, ri);
794 	admin->last_used_addr = ri->addr;
795 	/* Add a shallow copy of the rule to the VCAP list */
796 	duprule = vcap_dup_rule(ri);
797 	if (IS_ERR(duprule))
798 		return PTR_ERR(duprule);
799 	list_add_tail(&duprule->list, &admin->rules);
800 	return 0;
801 }
802 
803 static void vcap_move_rules(struct vcap_rule_internal *ri,
804 			    struct vcap_rule_move *move)
805 {
806 	ri->vctrl->ops->move(ri->ndev, ri->admin, move->addr,
807 			 move->offset, move->count);
808 }
809 
810 /* Encode and write a validated rule to the VCAP */
811 int vcap_add_rule(struct vcap_rule *rule)
812 {
813 	struct vcap_rule_internal *ri = to_intrule(rule);
814 	struct vcap_rule_move move = {0};
815 	int ret;
816 
817 	ret = vcap_api_check(ri->vctrl);
818 	if (ret)
819 		return ret;
820 	/* Insert the new rule in the list of vcap rules */
821 	ret = vcap_insert_rule(ri, &move);
822 	if (ret < 0) {
823 		pr_err("%s:%d: could not insert rule in vcap list: %d\n",
824 		       __func__, __LINE__, ret);
825 		goto out;
826 	}
827 	if (move.count > 0)
828 		vcap_move_rules(ri, &move);
829 	ret = vcap_encode_rule(ri);
830 	if (ret) {
831 		pr_err("%s:%d: rule encoding error: %d\n", __func__, __LINE__, ret);
832 		goto out;
833 	}
834 
835 	ret = vcap_write_rule(ri);
836 	if (ret)
837 		pr_err("%s:%d: rule write error: %d\n", __func__, __LINE__, ret);
838 out:
839 	return ret;
840 }
841 EXPORT_SYMBOL_GPL(vcap_add_rule);
842 
843 /* Allocate a new rule with the provided arguments */
844 struct vcap_rule *vcap_alloc_rule(struct vcap_control *vctrl,
845 				  struct net_device *ndev, int vcap_chain_id,
846 				  enum vcap_user user, u16 priority,
847 				  u32 id)
848 {
849 	struct vcap_rule_internal *ri;
850 	struct vcap_admin *admin;
851 	int maxsize;
852 
853 	if (!ndev)
854 		return ERR_PTR(-ENODEV);
855 	/* Get the VCAP instance */
856 	admin = vcap_find_admin(vctrl, vcap_chain_id);
857 	if (!admin)
858 		return ERR_PTR(-ENOENT);
859 	/* Sanity check that this VCAP is supported on this platform */
860 	if (vctrl->vcaps[admin->vtype].rows == 0)
861 		return ERR_PTR(-EINVAL);
862 	/* Check if a rule with this id already exists */
863 	if (vcap_lookup_rule(vctrl, id))
864 		return ERR_PTR(-EEXIST);
865 	/* Check if there is room for the rule in the block(s) of the VCAP */
866 	maxsize = vctrl->vcaps[admin->vtype].sw_count; /* worst case rule size */
867 	if (vcap_rule_space(admin, maxsize))
868 		return ERR_PTR(-ENOSPC);
869 	/* Create a container for the rule and return it */
870 	ri = kzalloc(sizeof(*ri), GFP_KERNEL);
871 	if (!ri)
872 		return ERR_PTR(-ENOMEM);
873 	ri->data.vcap_chain_id = vcap_chain_id;
874 	ri->data.user = user;
875 	ri->data.priority = priority;
876 	ri->data.id = id;
877 	ri->data.keyset = VCAP_KFS_NO_VALUE;
878 	ri->data.actionset = VCAP_AFS_NO_VALUE;
879 	INIT_LIST_HEAD(&ri->list);
880 	INIT_LIST_HEAD(&ri->data.keyfields);
881 	INIT_LIST_HEAD(&ri->data.actionfields);
882 	ri->ndev = ndev;
883 	ri->admin = admin; /* refer to the vcap instance */
884 	ri->vctrl = vctrl; /* refer to the client */
885 	if (vcap_set_rule_id(ri) == 0)
886 		goto out_free;
887 	vcap_erase_cache(ri);
888 	return (struct vcap_rule *)ri;
889 
890 out_free:
891 	kfree(ri);
892 	return ERR_PTR(-EINVAL);
893 }
894 EXPORT_SYMBOL_GPL(vcap_alloc_rule);
895 
896 /* Free mem of a rule owned by client after the rule as been added to the VCAP */
897 void vcap_free_rule(struct vcap_rule *rule)
898 {
899 	struct vcap_rule_internal *ri = to_intrule(rule);
900 	struct vcap_client_actionfield *caf, *next_caf;
901 	struct vcap_client_keyfield *ckf, *next_ckf;
902 
903 	/* Deallocate the list of keys and actions */
904 	list_for_each_entry_safe(ckf, next_ckf, &ri->data.keyfields, ctrl.list) {
905 		list_del(&ckf->ctrl.list);
906 		kfree(ckf);
907 	}
908 	list_for_each_entry_safe(caf, next_caf, &ri->data.actionfields, ctrl.list) {
909 		list_del(&caf->ctrl.list);
910 		kfree(caf);
911 	}
912 	/* Deallocate the rule */
913 	kfree(rule);
914 }
915 EXPORT_SYMBOL_GPL(vcap_free_rule);
916 
917 /* Delete rule in a VCAP instance */
918 int vcap_del_rule(struct vcap_control *vctrl, struct net_device *ndev, u32 id)
919 {
920 	struct vcap_rule_internal *ri, *elem;
921 	struct vcap_admin *admin;
922 	int err;
923 
924 	/* This will later also handle rule moving */
925 	if (!ndev)
926 		return -ENODEV;
927 	err = vcap_api_check(vctrl);
928 	if (err)
929 		return err;
930 	/* Look for the rule id in all vcaps */
931 	ri = vcap_lookup_rule(vctrl, id);
932 	if (!ri)
933 		return -EINVAL;
934 	admin = ri->admin;
935 	list_del(&ri->list);
936 
937 	/* delete the rule in the cache */
938 	vctrl->ops->init(ndev, admin, ri->addr, ri->size);
939 	if (list_empty(&admin->rules)) {
940 		admin->last_used_addr = admin->last_valid_addr;
941 	} else {
942 		/* update the address range end marker from the last rule in the list */
943 		elem = list_last_entry(&admin->rules, struct vcap_rule_internal, list);
944 		admin->last_used_addr = elem->addr;
945 	}
946 	kfree(ri);
947 	return 0;
948 }
949 EXPORT_SYMBOL_GPL(vcap_del_rule);
950 
951 /* Delete all rules in the VCAP instance */
952 int vcap_del_rules(struct vcap_control *vctrl, struct vcap_admin *admin)
953 {
954 	struct vcap_rule_internal *ri, *next_ri;
955 	int ret = vcap_api_check(vctrl);
956 
957 	if (ret)
958 		return ret;
959 	list_for_each_entry_safe(ri, next_ri, &admin->rules, list) {
960 		vctrl->ops->init(ri->ndev, admin, ri->addr, ri->size);
961 		list_del(&ri->list);
962 		kfree(ri);
963 	}
964 	admin->last_used_addr = admin->last_valid_addr;
965 	return 0;
966 }
967 EXPORT_SYMBOL_GPL(vcap_del_rules);
968 
969 /* Find information on a key field in a rule */
970 const struct vcap_field *vcap_lookup_keyfield(struct vcap_rule *rule,
971 					      enum vcap_key_field key)
972 {
973 	struct vcap_rule_internal *ri = to_intrule(rule);
974 	enum vcap_keyfield_set keyset = rule->keyset;
975 	enum vcap_type vt = ri->admin->vtype;
976 	const struct vcap_field *fields;
977 
978 	if (keyset == VCAP_KFS_NO_VALUE)
979 		return NULL;
980 	fields = vcap_keyfields(ri->vctrl, vt, keyset);
981 	if (!fields)
982 		return NULL;
983 	return &fields[key];
984 }
985 EXPORT_SYMBOL_GPL(vcap_lookup_keyfield);
986 
987 static void vcap_copy_from_client_keyfield(struct vcap_rule *rule,
988 					   struct vcap_client_keyfield *field,
989 					   struct vcap_client_keyfield_data *data)
990 {
991 	/* This will be expanded later to handle different vcap memory layouts */
992 	memcpy(&field->data, data, sizeof(field->data));
993 }
994 
995 static int vcap_rule_add_key(struct vcap_rule *rule,
996 			     enum vcap_key_field key,
997 			     enum vcap_field_type ftype,
998 			     struct vcap_client_keyfield_data *data)
999 {
1000 	struct vcap_client_keyfield *field;
1001 
1002 	/* More validation will be added here later */
1003 	field = kzalloc(sizeof(*field), GFP_KERNEL);
1004 	if (!field)
1005 		return -ENOMEM;
1006 	field->ctrl.key = key;
1007 	field->ctrl.type = ftype;
1008 	vcap_copy_from_client_keyfield(rule, field, data);
1009 	list_add_tail(&field->ctrl.list, &rule->keyfields);
1010 	return 0;
1011 }
1012 
1013 static void vcap_rule_set_key_bitsize(struct vcap_u1_key *u1, enum vcap_bit val)
1014 {
1015 	switch (val) {
1016 	case VCAP_BIT_0:
1017 		u1->value = 0;
1018 		u1->mask = 1;
1019 		break;
1020 	case VCAP_BIT_1:
1021 		u1->value = 1;
1022 		u1->mask = 1;
1023 		break;
1024 	case VCAP_BIT_ANY:
1025 		u1->value = 0;
1026 		u1->mask = 0;
1027 		break;
1028 	}
1029 }
1030 
1031 /* Add a bit key with value and mask to the rule */
1032 int vcap_rule_add_key_bit(struct vcap_rule *rule, enum vcap_key_field key,
1033 			  enum vcap_bit val)
1034 {
1035 	struct vcap_client_keyfield_data data;
1036 
1037 	vcap_rule_set_key_bitsize(&data.u1, val);
1038 	return vcap_rule_add_key(rule, key, VCAP_FIELD_BIT, &data);
1039 }
1040 EXPORT_SYMBOL_GPL(vcap_rule_add_key_bit);
1041 
1042 /* Add a 32 bit key field with value and mask to the rule */
1043 int vcap_rule_add_key_u32(struct vcap_rule *rule, enum vcap_key_field key,
1044 			  u32 value, u32 mask)
1045 {
1046 	struct vcap_client_keyfield_data data;
1047 
1048 	data.u32.value = value;
1049 	data.u32.mask = mask;
1050 	return vcap_rule_add_key(rule, key, VCAP_FIELD_U32, &data);
1051 }
1052 EXPORT_SYMBOL_GPL(vcap_rule_add_key_u32);
1053 
1054 /* Add a 48 bit key with value and mask to the rule */
1055 int vcap_rule_add_key_u48(struct vcap_rule *rule, enum vcap_key_field key,
1056 			  struct vcap_u48_key *fieldval)
1057 {
1058 	struct vcap_client_keyfield_data data;
1059 
1060 	memcpy(&data.u48, fieldval, sizeof(data.u48));
1061 	return vcap_rule_add_key(rule, key, VCAP_FIELD_U48, &data);
1062 }
1063 EXPORT_SYMBOL_GPL(vcap_rule_add_key_u48);
1064 
1065 /* Add a 72 bit key with value and mask to the rule */
1066 int vcap_rule_add_key_u72(struct vcap_rule *rule, enum vcap_key_field key,
1067 			  struct vcap_u72_key *fieldval)
1068 {
1069 	struct vcap_client_keyfield_data data;
1070 
1071 	memcpy(&data.u72, fieldval, sizeof(data.u72));
1072 	return vcap_rule_add_key(rule, key, VCAP_FIELD_U72, &data);
1073 }
1074 EXPORT_SYMBOL_GPL(vcap_rule_add_key_u72);
1075 
1076 static void vcap_copy_from_client_actionfield(struct vcap_rule *rule,
1077 					      struct vcap_client_actionfield *field,
1078 					      struct vcap_client_actionfield_data *data)
1079 {
1080 	/* This will be expanded later to handle different vcap memory layouts */
1081 	memcpy(&field->data, data, sizeof(field->data));
1082 }
1083 
1084 static int vcap_rule_add_action(struct vcap_rule *rule,
1085 				enum vcap_action_field action,
1086 				enum vcap_field_type ftype,
1087 				struct vcap_client_actionfield_data *data)
1088 {
1089 	struct vcap_client_actionfield *field;
1090 
1091 	/* More validation will be added here later */
1092 	field = kzalloc(sizeof(*field), GFP_KERNEL);
1093 	if (!field)
1094 		return -ENOMEM;
1095 	field->ctrl.action = action;
1096 	field->ctrl.type = ftype;
1097 	vcap_copy_from_client_actionfield(rule, field, data);
1098 	list_add_tail(&field->ctrl.list, &rule->actionfields);
1099 	return 0;
1100 }
1101 
1102 static void vcap_rule_set_action_bitsize(struct vcap_u1_action *u1,
1103 					 enum vcap_bit val)
1104 {
1105 	switch (val) {
1106 	case VCAP_BIT_0:
1107 		u1->value = 0;
1108 		break;
1109 	case VCAP_BIT_1:
1110 		u1->value = 1;
1111 		break;
1112 	case VCAP_BIT_ANY:
1113 		u1->value = 0;
1114 		break;
1115 	}
1116 }
1117 
1118 /* Add a bit action with value to the rule */
1119 int vcap_rule_add_action_bit(struct vcap_rule *rule,
1120 			     enum vcap_action_field action,
1121 			     enum vcap_bit val)
1122 {
1123 	struct vcap_client_actionfield_data data;
1124 
1125 	vcap_rule_set_action_bitsize(&data.u1, val);
1126 	return vcap_rule_add_action(rule, action, VCAP_FIELD_BIT, &data);
1127 }
1128 EXPORT_SYMBOL_GPL(vcap_rule_add_action_bit);
1129 
1130 /* Add a 32 bit action field with value to the rule */
1131 int vcap_rule_add_action_u32(struct vcap_rule *rule,
1132 			     enum vcap_action_field action,
1133 			     u32 value)
1134 {
1135 	struct vcap_client_actionfield_data data;
1136 
1137 	data.u32.value = value;
1138 	return vcap_rule_add_action(rule, action, VCAP_FIELD_U32, &data);
1139 }
1140 EXPORT_SYMBOL_GPL(vcap_rule_add_action_u32);
1141 
1142 /* Copy to host byte order */
1143 void vcap_netbytes_copy(u8 *dst, u8 *src, int count)
1144 {
1145 	int idx;
1146 
1147 	for (idx = 0; idx < count; ++idx, ++dst)
1148 		*dst = src[count - idx - 1];
1149 }
1150 EXPORT_SYMBOL_GPL(vcap_netbytes_copy);
1151 
1152 /* Convert validation error code into tc extact error message */
1153 void vcap_set_tc_exterr(struct flow_cls_offload *fco, struct vcap_rule *vrule)
1154 {
1155 	switch (vrule->exterr) {
1156 	case VCAP_ERR_NONE:
1157 		break;
1158 	case VCAP_ERR_NO_ADMIN:
1159 		NL_SET_ERR_MSG_MOD(fco->common.extack,
1160 				   "Missing VCAP instance");
1161 		break;
1162 	case VCAP_ERR_NO_NETDEV:
1163 		NL_SET_ERR_MSG_MOD(fco->common.extack,
1164 				   "Missing network interface");
1165 		break;
1166 	case VCAP_ERR_NO_KEYSET_MATCH:
1167 		NL_SET_ERR_MSG_MOD(fco->common.extack,
1168 				   "No keyset matched the filter keys");
1169 		break;
1170 	case VCAP_ERR_NO_ACTIONSET_MATCH:
1171 		NL_SET_ERR_MSG_MOD(fco->common.extack,
1172 				   "No actionset matched the filter actions");
1173 		break;
1174 	case VCAP_ERR_NO_PORT_KEYSET_MATCH:
1175 		NL_SET_ERR_MSG_MOD(fco->common.extack,
1176 				   "No port keyset matched the filter keys");
1177 		break;
1178 	}
1179 }
1180 EXPORT_SYMBOL_GPL(vcap_set_tc_exterr);
1181 
1182 #ifdef CONFIG_VCAP_KUNIT_TEST
1183 #include "vcap_api_kunit.c"
1184 #endif
1185