Linux Audio

Check our new training course

Embedded Linux training

Mar 31-Apr 8, 2025
Register
Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0+
   2/* Microchip VCAP API
   3 *
   4 * Copyright (c) 2022 Microchip Technology Inc. and its subsidiaries.
   5 */
   6
   7#include <linux/types.h>
   8
   9#include "vcap_api_private.h"
  10
  11static int keyfield_size_table[] = {
  12	[VCAP_FIELD_BIT]  = sizeof(struct vcap_u1_key),
  13	[VCAP_FIELD_U32]  = sizeof(struct vcap_u32_key),
  14	[VCAP_FIELD_U48]  = sizeof(struct vcap_u48_key),
  15	[VCAP_FIELD_U56]  = sizeof(struct vcap_u56_key),
  16	[VCAP_FIELD_U64]  = sizeof(struct vcap_u64_key),
  17	[VCAP_FIELD_U72]  = sizeof(struct vcap_u72_key),
  18	[VCAP_FIELD_U112] = sizeof(struct vcap_u112_key),
  19	[VCAP_FIELD_U128] = sizeof(struct vcap_u128_key),
  20};
  21
  22static int actionfield_size_table[] = {
  23	[VCAP_FIELD_BIT]  = sizeof(struct vcap_u1_action),
  24	[VCAP_FIELD_U32]  = sizeof(struct vcap_u32_action),
  25	[VCAP_FIELD_U48]  = sizeof(struct vcap_u48_action),
  26	[VCAP_FIELD_U56]  = sizeof(struct vcap_u56_action),
  27	[VCAP_FIELD_U64]  = sizeof(struct vcap_u64_action),
  28	[VCAP_FIELD_U72]  = sizeof(struct vcap_u72_action),
  29	[VCAP_FIELD_U112] = sizeof(struct vcap_u112_action),
  30	[VCAP_FIELD_U128] = sizeof(struct vcap_u128_action),
  31};
  32
  33/* Moving a rule in the VCAP address space */
  34struct vcap_rule_move {
  35	int addr; /* address to move */
  36	int offset; /* change in address */
  37	int count; /* blocksize of addresses to move */
  38};
  39
  40/* Stores the filter cookie and chain id that enabled the port */
  41struct vcap_enabled_port {
  42	struct list_head list; /* for insertion in enabled ports list */
  43	struct net_device *ndev;  /* the enabled port */
  44	unsigned long cookie; /* filter that enabled the port */
  45	int src_cid; /* source chain id */
  46	int dst_cid; /* destination chain id */
  47};
  48
  49void vcap_iter_set(struct vcap_stream_iter *itr, int sw_width,
  50		   const struct vcap_typegroup *tg, u32 offset)
  51{
  52	memset(itr, 0, sizeof(*itr));
  53	itr->offset = offset;
  54	itr->sw_width = sw_width;
  55	itr->regs_per_sw = DIV_ROUND_UP(sw_width, 32);
  56	itr->tg = tg;
  57}
  58
  59static void vcap_iter_skip_tg(struct vcap_stream_iter *itr)
  60{
  61	/* Compensate the field offset for preceding typegroups.
  62	 * A typegroup table ends with an all-zero terminator.
  63	 */
  64	while (itr->tg->width && itr->offset >= itr->tg->offset) {
  65		itr->offset += itr->tg->width;
  66		itr->tg++; /* next typegroup */
  67	}
  68}
  69
  70void vcap_iter_update(struct vcap_stream_iter *itr)
  71{
  72	int sw_idx, sw_bitpos;
  73
  74	/* Calculate the subword index and bitposition for current bit */
  75	sw_idx = itr->offset / itr->sw_width;
  76	sw_bitpos = itr->offset % itr->sw_width;
  77	/* Calculate the register index and bitposition for current bit */
  78	itr->reg_idx = (sw_idx * itr->regs_per_sw) + (sw_bitpos / 32);
  79	itr->reg_bitpos = sw_bitpos % 32;
  80}
  81
  82void vcap_iter_init(struct vcap_stream_iter *itr, int sw_width,
  83		    const struct vcap_typegroup *tg, u32 offset)
  84{
  85	vcap_iter_set(itr, sw_width, tg, offset);
  86	vcap_iter_skip_tg(itr);
  87	vcap_iter_update(itr);
  88}
  89
  90void vcap_iter_next(struct vcap_stream_iter *itr)
  91{
  92	itr->offset++;
  93	vcap_iter_skip_tg(itr);
  94	vcap_iter_update(itr);
  95}
  96
  97static void vcap_set_bit(u32 *stream, struct vcap_stream_iter *itr, bool value)
  98{
  99	u32 mask = BIT(itr->reg_bitpos);
 100	u32 *p = &stream[itr->reg_idx];
 101
 102	if (value)
 103		*p |= mask;
 104	else
 105		*p &= ~mask;
 106}
 107
 108static void vcap_encode_bit(u32 *stream, struct vcap_stream_iter *itr, bool val)
 109{
 110	/* When intersected by a type group field, stream the type group bits
 111	 * before continuing with the value bit
 112	 */
 113	while (itr->tg->width &&
 114	       itr->offset >= itr->tg->offset &&
 115	       itr->offset < itr->tg->offset + itr->tg->width) {
 116		int tg_bitpos = itr->tg->offset - itr->offset;
 117
 118		vcap_set_bit(stream, itr, (itr->tg->value >> tg_bitpos) & 0x1);
 119		itr->offset++;
 120		vcap_iter_update(itr);
 121	}
 122	vcap_set_bit(stream, itr, val);
 123}
 124
 125static void vcap_encode_field(u32 *stream, struct vcap_stream_iter *itr,
 126			      int width, const u8 *value)
 127{
 128	int idx;
 129
 130	/* Loop over the field value bits and add the value bits one by one to
 131	 * the output stream.
 132	 */
 133	for (idx = 0; idx < width; idx++) {
 134		u8 bidx = idx & GENMASK(2, 0);
 135
 136		/* Encode one field value bit */
 137		vcap_encode_bit(stream, itr, (value[idx / 8] >> bidx) & 0x1);
 138		vcap_iter_next(itr);
 139	}
 140}
 141
 142static void vcap_encode_typegroups(u32 *stream, int sw_width,
 143				   const struct vcap_typegroup *tg,
 144				   bool mask)
 145{
 146	struct vcap_stream_iter iter;
 147	int idx;
 148
 149	/* Mask bits must be set to zeros (inverted later when writing to the
 150	 * mask cache register), so that the mask typegroup bits consist of
 151	 * match-1 or match-0, or both
 152	 */
 153	vcap_iter_set(&iter, sw_width, tg, 0);
 154	while (iter.tg->width) {
 155		/* Set position to current typegroup bit */
 156		iter.offset = iter.tg->offset;
 157		vcap_iter_update(&iter);
 158		for (idx = 0; idx < iter.tg->width; idx++) {
 159			/* Iterate over current typegroup bits. Mask typegroup
 160			 * bits are always set
 161			 */
 162			if (mask)
 163				vcap_set_bit(stream, &iter, 0x1);
 164			else
 165				vcap_set_bit(stream, &iter,
 166					     (iter.tg->value >> idx) & 0x1);
 167			iter.offset++;
 168			vcap_iter_update(&iter);
 169		}
 170		iter.tg++; /* next typegroup */
 171	}
 172}
 173
 174static bool vcap_bitarray_zero(int width, u8 *value)
 175{
 176	int bytes = DIV_ROUND_UP(width, BITS_PER_BYTE);
 177	u8 total = 0, bmask = 0xff;
 178	int rwidth = width;
 179	int idx;
 180
 181	for (idx = 0; idx < bytes; ++idx, rwidth -= BITS_PER_BYTE) {
 182		if (rwidth && rwidth < BITS_PER_BYTE)
 183			bmask = (1 << rwidth) - 1;
 184		total += value[idx] & bmask;
 185	}
 186	return total == 0;
 187}
 188
 189static bool vcap_get_bit(u32 *stream, struct vcap_stream_iter *itr)
 190{
 191	u32 mask = BIT(itr->reg_bitpos);
 192	u32 *p = &stream[itr->reg_idx];
 193
 194	return !!(*p & mask);
 195}
 196
 197static void vcap_decode_field(u32 *stream, struct vcap_stream_iter *itr,
 198			      int width, u8 *value)
 199{
 200	int idx;
 201
 202	/* Loop over the field value bits and get the field bits and
 203	 * set them in the output value byte array
 204	 */
 205	for (idx = 0; idx < width; idx++) {
 206		u8 bidx = idx & 0x7;
 207
 208		/* Decode one field value bit */
 209		if (vcap_get_bit(stream, itr))
 210			*value |= 1 << bidx;
 211		vcap_iter_next(itr);
 212		if (bidx == 7)
 213			value++;
 214	}
 215}
 216
 217/* Verify that the type id in the stream matches the type id of the keyset */
 218static bool vcap_verify_keystream_keyset(struct vcap_control *vctrl,
 219					 enum vcap_type vt,
 220					 u32 *keystream,
 221					 u32 *mskstream,
 222					 enum vcap_keyfield_set keyset)
 223{
 224	const struct vcap_info *vcap = &vctrl->vcaps[vt];
 225	const struct vcap_field *typefld;
 226	const struct vcap_typegroup *tgt;
 227	const struct vcap_field *fields;
 228	struct vcap_stream_iter iter;
 229	const struct vcap_set *info;
 230	u32 value = 0;
 231	u32 mask = 0;
 232
 233	if (vcap_keyfield_count(vctrl, vt, keyset) == 0)
 234		return false;
 235
 236	info = vcap_keyfieldset(vctrl, vt, keyset);
 237	/* Check that the keyset is valid */
 238	if (!info)
 239		return false;
 240
 241	/* a type_id of value -1 means that there is no type field */
 242	if (info->type_id == (u8)-1)
 243		return true;
 244
 245	/* Get a valid typegroup for the specific keyset */
 246	tgt = vcap_keyfield_typegroup(vctrl, vt, keyset);
 247	if (!tgt)
 248		return false;
 249
 250	fields = vcap_keyfields(vctrl, vt, keyset);
 251	if (!fields)
 252		return false;
 253
 254	typefld = &fields[VCAP_KF_TYPE];
 255	vcap_iter_init(&iter, vcap->sw_width, tgt, typefld->offset);
 256	vcap_decode_field(mskstream, &iter, typefld->width, (u8 *)&mask);
 257	/* no type info if there are no mask bits */
 258	if (vcap_bitarray_zero(typefld->width, (u8 *)&mask))
 259		return false;
 260
 261	/* Get the value of the type field in the stream and compare to the
 262	 * one define in the vcap keyset
 263	 */
 264	vcap_iter_init(&iter, vcap->sw_width, tgt, typefld->offset);
 265	vcap_decode_field(keystream, &iter, typefld->width, (u8 *)&value);
 266
 267	return (value & mask) == (info->type_id & mask);
 268}
 269
 270/* Verify that the typegroup bits have the correct values */
 271static int vcap_verify_typegroups(u32 *stream, int sw_width,
 272				  const struct vcap_typegroup *tgt, bool mask,
 273				  int sw_max)
 274{
 275	struct vcap_stream_iter iter;
 276	int sw_cnt, idx;
 277
 278	vcap_iter_set(&iter, sw_width, tgt, 0);
 279	sw_cnt = 0;
 280	while (iter.tg->width) {
 281		u32 value = 0;
 282		u32 tg_value = iter.tg->value;
 283
 284		if (mask)
 285			tg_value = (1 << iter.tg->width) - 1;
 286		/* Set position to current typegroup bit */
 287		iter.offset = iter.tg->offset;
 288		vcap_iter_update(&iter);
 289		for (idx = 0; idx < iter.tg->width; idx++) {
 290			/* Decode one typegroup bit */
 291			if (vcap_get_bit(stream, &iter))
 292				value |= 1 << idx;
 293			iter.offset++;
 294			vcap_iter_update(&iter);
 295		}
 296		if (value != tg_value)
 297			return -EINVAL;
 298		iter.tg++; /* next typegroup */
 299		sw_cnt++;
 300		/* Stop checking more typegroups */
 301		if (sw_max && sw_cnt >= sw_max)
 302			break;
 303	}
 304	return 0;
 305}
 306
 307/* Find the subword width of the key typegroup that matches the stream data */
 308static int vcap_find_keystream_typegroup_sw(struct vcap_control *vctrl,
 309					    enum vcap_type vt, u32 *stream,
 310					    bool mask, int sw_max)
 311{
 312	const struct vcap_typegroup **tgt;
 313	int sw_idx, res;
 314
 315	tgt = vctrl->vcaps[vt].keyfield_set_typegroups;
 316	/* Try the longest subword match first */
 317	for (sw_idx = vctrl->vcaps[vt].sw_count; sw_idx >= 0; sw_idx--) {
 318		if (!tgt[sw_idx])
 319			continue;
 320
 321		res = vcap_verify_typegroups(stream, vctrl->vcaps[vt].sw_width,
 322					     tgt[sw_idx], mask, sw_max);
 323		if (res == 0)
 324			return sw_idx;
 325	}
 326	return -EINVAL;
 327}
 328
 329/* Verify that the typegroup information, subword count, keyset and type id
 330 * are in sync and correct, return the list of matchin keysets
 331 */
 332int
 333vcap_find_keystream_keysets(struct vcap_control *vctrl,
 334			    enum vcap_type vt,
 335			    u32 *keystream,
 336			    u32 *mskstream,
 337			    bool mask, int sw_max,
 338			    struct vcap_keyset_list *kslist)
 339{
 340	const struct vcap_set *keyfield_set;
 341	int sw_count, idx;
 342
 343	sw_count = vcap_find_keystream_typegroup_sw(vctrl, vt, keystream, mask,
 344						    sw_max);
 345	if (sw_count < 0)
 346		return sw_count;
 347
 348	keyfield_set = vctrl->vcaps[vt].keyfield_set;
 349	for (idx = 0; idx < vctrl->vcaps[vt].keyfield_set_size; ++idx) {
 350		if (keyfield_set[idx].sw_per_item != sw_count)
 351			continue;
 352
 353		if (vcap_verify_keystream_keyset(vctrl, vt, keystream,
 354						 mskstream, idx))
 355			vcap_keyset_list_add(kslist, idx);
 356	}
 357	if (kslist->cnt > 0)
 358		return 0;
 359	return -EINVAL;
 360}
 361EXPORT_SYMBOL_GPL(vcap_find_keystream_keysets);
 362
 363/* Read key data from a VCAP address and discover if there are any rule keysets
 364 * here
 365 */
 366int vcap_addr_keysets(struct vcap_control *vctrl,
 367		      struct net_device *ndev,
 368		      struct vcap_admin *admin,
 369		      int addr,
 370		      struct vcap_keyset_list *kslist)
 371{
 372	enum vcap_type vt = admin->vtype;
 373	int keyset_sw_regs, idx;
 374	u32 key = 0, mask = 0;
 375
 376	/* Read the cache at the specified address */
 377	keyset_sw_regs = DIV_ROUND_UP(vctrl->vcaps[vt].sw_width, 32);
 378	vctrl->ops->update(ndev, admin, VCAP_CMD_READ, VCAP_SEL_ALL, addr);
 379	vctrl->ops->cache_read(ndev, admin, VCAP_SEL_ENTRY, 0,
 380			       keyset_sw_regs);
 381	/* Skip uninitialized key/mask entries */
 382	for (idx = 0; idx < keyset_sw_regs; ++idx) {
 383		key |= ~admin->cache.keystream[idx];
 384		mask |= admin->cache.maskstream[idx];
 385	}
 386	if (key == 0 && mask == 0)
 387		return -EINVAL;
 388	/* Decode and locate the keysets */
 389	return vcap_find_keystream_keysets(vctrl, vt, admin->cache.keystream,
 390					   admin->cache.maskstream, false, 0,
 391					   kslist);
 392}
 393EXPORT_SYMBOL_GPL(vcap_addr_keysets);
 394
 395/* Return the list of keyfields for the keyset */
 396const struct vcap_field *vcap_keyfields(struct vcap_control *vctrl,
 397					enum vcap_type vt,
 398					enum vcap_keyfield_set keyset)
 399{
 400	/* Check that the keyset exists in the vcap keyset list */
 401	if (keyset >= vctrl->vcaps[vt].keyfield_set_size)
 402		return NULL;
 403	return vctrl->vcaps[vt].keyfield_set_map[keyset];
 404}
 405
 406/* Return the keyset information for the keyset */
 407const struct vcap_set *vcap_keyfieldset(struct vcap_control *vctrl,
 408					enum vcap_type vt,
 409					enum vcap_keyfield_set keyset)
 410{
 411	const struct vcap_set *kset;
 412
 413	/* Check that the keyset exists in the vcap keyset list */
 414	if (keyset >= vctrl->vcaps[vt].keyfield_set_size)
 415		return NULL;
 416	kset = &vctrl->vcaps[vt].keyfield_set[keyset];
 417	if (kset->sw_per_item == 0 || kset->sw_per_item > vctrl->vcaps[vt].sw_count)
 418		return NULL;
 419	return kset;
 420}
 421EXPORT_SYMBOL_GPL(vcap_keyfieldset);
 422
 423/* Return the typegroup table for the matching keyset (using subword size) */
 424const struct vcap_typegroup *
 425vcap_keyfield_typegroup(struct vcap_control *vctrl,
 426			enum vcap_type vt, enum vcap_keyfield_set keyset)
 427{
 428	const struct vcap_set *kset = vcap_keyfieldset(vctrl, vt, keyset);
 429
 430	/* Check that the keyset is valid */
 431	if (!kset)
 432		return NULL;
 433	return vctrl->vcaps[vt].keyfield_set_typegroups[kset->sw_per_item];
 434}
 435
 436/* Return the number of keyfields in the keyset */
 437int vcap_keyfield_count(struct vcap_control *vctrl,
 438			enum vcap_type vt, enum vcap_keyfield_set keyset)
 439{
 440	/* Check that the keyset exists in the vcap keyset list */
 441	if (keyset >= vctrl->vcaps[vt].keyfield_set_size)
 442		return 0;
 443	return vctrl->vcaps[vt].keyfield_set_map_size[keyset];
 444}
 445
 446static void vcap_encode_keyfield(struct vcap_rule_internal *ri,
 447				 const struct vcap_client_keyfield *kf,
 448				 const struct vcap_field *rf,
 449				 const struct vcap_typegroup *tgt)
 450{
 451	int sw_width = ri->vctrl->vcaps[ri->admin->vtype].sw_width;
 452	struct vcap_cache_data *cache = &ri->admin->cache;
 453	struct vcap_stream_iter iter;
 454	const u8 *value, *mask;
 455
 456	/* Encode the fields for the key and the mask in their respective
 457	 * streams, respecting the subword width.
 458	 */
 459	switch (kf->ctrl.type) {
 460	case VCAP_FIELD_BIT:
 461		value = &kf->data.u1.value;
 462		mask = &kf->data.u1.mask;
 463		break;
 464	case VCAP_FIELD_U32:
 465		value = (const u8 *)&kf->data.u32.value;
 466		mask = (const u8 *)&kf->data.u32.mask;
 467		break;
 468	case VCAP_FIELD_U48:
 469		value = kf->data.u48.value;
 470		mask = kf->data.u48.mask;
 471		break;
 472	case VCAP_FIELD_U56:
 473		value = kf->data.u56.value;
 474		mask = kf->data.u56.mask;
 475		break;
 476	case VCAP_FIELD_U64:
 477		value = kf->data.u64.value;
 478		mask = kf->data.u64.mask;
 479		break;
 480	case VCAP_FIELD_U72:
 481		value = kf->data.u72.value;
 482		mask = kf->data.u72.mask;
 483		break;
 484	case VCAP_FIELD_U112:
 485		value = kf->data.u112.value;
 486		mask = kf->data.u112.mask;
 487		break;
 488	case VCAP_FIELD_U128:
 489		value = kf->data.u128.value;
 490		mask = kf->data.u128.mask;
 491		break;
 492	}
 493	vcap_iter_init(&iter, sw_width, tgt, rf->offset);
 494	vcap_encode_field(cache->keystream, &iter, rf->width, value);
 495	vcap_iter_init(&iter, sw_width, tgt, rf->offset);
 496	vcap_encode_field(cache->maskstream, &iter, rf->width, mask);
 497}
 498
 499static void vcap_encode_keyfield_typegroups(struct vcap_control *vctrl,
 500					    struct vcap_rule_internal *ri,
 501					    const struct vcap_typegroup *tgt)
 502{
 503	int sw_width = vctrl->vcaps[ri->admin->vtype].sw_width;
 504	struct vcap_cache_data *cache = &ri->admin->cache;
 505
 506	/* Encode the typegroup bits for the key and the mask in their streams,
 507	 * respecting the subword width.
 508	 */
 509	vcap_encode_typegroups(cache->keystream, sw_width, tgt, false);
 510	vcap_encode_typegroups(cache->maskstream, sw_width, tgt, true);
 511}
 512
 513/* Copy data from src to dst but reverse the data in chunks of 32bits.
 514 * For example if src is 00:11:22:33:44:55 where 55 is LSB the dst will
 515 * have the value 22:33:44:55:00:11.
 516 */
 517static void vcap_copy_to_w32be(u8 *dst, const u8 *src, int size)
 518{
 519	for (int idx = 0; idx < size; ++idx) {
 520		int first_byte_index = 0;
 521		int nidx;
 522
 523		first_byte_index = size - (((idx >> 2) + 1) << 2);
 524		if (first_byte_index < 0)
 525			first_byte_index = 0;
 526		nidx = idx + first_byte_index - (idx & ~0x3);
 527		dst[nidx] = src[idx];
 528	}
 529}
 530
 531static void
 532vcap_copy_from_client_keyfield(struct vcap_rule *rule,
 533			       struct vcap_client_keyfield *dst,
 534			       const struct vcap_client_keyfield *src)
 535{
 536	struct vcap_rule_internal *ri = to_intrule(rule);
 537	const struct vcap_client_keyfield_data *sdata;
 538	struct vcap_client_keyfield_data *ddata;
 539	int size;
 540
 541	dst->ctrl.type = src->ctrl.type;
 542	dst->ctrl.key = src->ctrl.key;
 543	INIT_LIST_HEAD(&dst->ctrl.list);
 544	sdata = &src->data;
 545	ddata = &dst->data;
 546
 547	if (!ri->admin->w32be) {
 548		memcpy(ddata, sdata, sizeof(dst->data));
 549		return;
 550	}
 551
 552	size = keyfield_size_table[dst->ctrl.type] / 2;
 553
 554	switch (dst->ctrl.type) {
 555	case VCAP_FIELD_BIT:
 556	case VCAP_FIELD_U32:
 557		memcpy(ddata, sdata, sizeof(dst->data));
 558		break;
 559	case VCAP_FIELD_U48:
 560		vcap_copy_to_w32be(ddata->u48.value, src->data.u48.value, size);
 561		vcap_copy_to_w32be(ddata->u48.mask,  src->data.u48.mask, size);
 562		break;
 563	case VCAP_FIELD_U56:
 564		vcap_copy_to_w32be(ddata->u56.value, sdata->u56.value, size);
 565		vcap_copy_to_w32be(ddata->u56.mask,  sdata->u56.mask, size);
 566		break;
 567	case VCAP_FIELD_U64:
 568		vcap_copy_to_w32be(ddata->u64.value, sdata->u64.value, size);
 569		vcap_copy_to_w32be(ddata->u64.mask,  sdata->u64.mask, size);
 570		break;
 571	case VCAP_FIELD_U72:
 572		vcap_copy_to_w32be(ddata->u72.value, sdata->u72.value, size);
 573		vcap_copy_to_w32be(ddata->u72.mask,  sdata->u72.mask, size);
 574		break;
 575	case VCAP_FIELD_U112:
 576		vcap_copy_to_w32be(ddata->u112.value, sdata->u112.value, size);
 577		vcap_copy_to_w32be(ddata->u112.mask,  sdata->u112.mask, size);
 578		break;
 579	case VCAP_FIELD_U128:
 580		vcap_copy_to_w32be(ddata->u128.value, sdata->u128.value, size);
 581		vcap_copy_to_w32be(ddata->u128.mask,  sdata->u128.mask, size);
 582		break;
 583	}
 584}
 585
 586static void
 587vcap_copy_from_client_actionfield(struct vcap_rule *rule,
 588				  struct vcap_client_actionfield *dst,
 589				  const struct vcap_client_actionfield *src)
 590{
 591	struct vcap_rule_internal *ri = to_intrule(rule);
 592	const struct vcap_client_actionfield_data *sdata;
 593	struct vcap_client_actionfield_data *ddata;
 594	int size;
 595
 596	dst->ctrl.type = src->ctrl.type;
 597	dst->ctrl.action = src->ctrl.action;
 598	INIT_LIST_HEAD(&dst->ctrl.list);
 599	sdata = &src->data;
 600	ddata = &dst->data;
 601
 602	if (!ri->admin->w32be) {
 603		memcpy(ddata, sdata, sizeof(dst->data));
 604		return;
 605	}
 606
 607	size = actionfield_size_table[dst->ctrl.type];
 608
 609	switch (dst->ctrl.type) {
 610	case VCAP_FIELD_BIT:
 611	case VCAP_FIELD_U32:
 612		memcpy(ddata, sdata, sizeof(dst->data));
 613		break;
 614	case VCAP_FIELD_U48:
 615		vcap_copy_to_w32be(ddata->u48.value, sdata->u48.value, size);
 616		break;
 617	case VCAP_FIELD_U56:
 618		vcap_copy_to_w32be(ddata->u56.value, sdata->u56.value, size);
 619		break;
 620	case VCAP_FIELD_U64:
 621		vcap_copy_to_w32be(ddata->u64.value, sdata->u64.value, size);
 622		break;
 623	case VCAP_FIELD_U72:
 624		vcap_copy_to_w32be(ddata->u72.value, sdata->u72.value, size);
 625		break;
 626	case VCAP_FIELD_U112:
 627		vcap_copy_to_w32be(ddata->u112.value, sdata->u112.value, size);
 628		break;
 629	case VCAP_FIELD_U128:
 630		vcap_copy_to_w32be(ddata->u128.value, sdata->u128.value, size);
 631		break;
 632	}
 633}
 634
 635static int vcap_encode_rule_keyset(struct vcap_rule_internal *ri)
 636{
 637	const struct vcap_client_keyfield *ckf;
 638	const struct vcap_typegroup *tg_table;
 639	struct vcap_client_keyfield tempkf;
 640	const struct vcap_field *kf_table;
 641	int keyset_size;
 642
 643	/* Get a valid set of fields for the specific keyset */
 644	kf_table = vcap_keyfields(ri->vctrl, ri->admin->vtype, ri->data.keyset);
 645	if (!kf_table) {
 646		pr_err("%s:%d: no fields available for this keyset: %d\n",
 647		       __func__, __LINE__, ri->data.keyset);
 648		return -EINVAL;
 649	}
 650	/* Get a valid typegroup for the specific keyset */
 651	tg_table = vcap_keyfield_typegroup(ri->vctrl, ri->admin->vtype,
 652					   ri->data.keyset);
 653	if (!tg_table) {
 654		pr_err("%s:%d: no typegroups available for this keyset: %d\n",
 655		       __func__, __LINE__, ri->data.keyset);
 656		return -EINVAL;
 657	}
 658	/* Get a valid size for the specific keyset */
 659	keyset_size = vcap_keyfield_count(ri->vctrl, ri->admin->vtype,
 660					  ri->data.keyset);
 661	if (keyset_size == 0) {
 662		pr_err("%s:%d: zero field count for this keyset: %d\n",
 663		       __func__, __LINE__, ri->data.keyset);
 664		return -EINVAL;
 665	}
 666	/* Iterate over the keyfields (key, mask) in the rule
 667	 * and encode these bits
 668	 */
 669	if (list_empty(&ri->data.keyfields)) {
 670		pr_err("%s:%d: no keyfields in the rule\n", __func__, __LINE__);
 671		return -EINVAL;
 672	}
 673	list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list) {
 674		/* Check that the client entry exists in the keyset */
 675		if (ckf->ctrl.key >= keyset_size) {
 676			pr_err("%s:%d: key %d is not in vcap\n",
 677			       __func__, __LINE__, ckf->ctrl.key);
 678			return -EINVAL;
 679		}
 680		vcap_copy_from_client_keyfield(&ri->data, &tempkf, ckf);
 681		vcap_encode_keyfield(ri, &tempkf, &kf_table[ckf->ctrl.key],
 682				     tg_table);
 683	}
 684	/* Add typegroup bits to the key/mask bitstreams */
 685	vcap_encode_keyfield_typegroups(ri->vctrl, ri, tg_table);
 686	return 0;
 687}
 688
 689/* Return the list of actionfields for the actionset */
 690const struct vcap_field *
 691vcap_actionfields(struct vcap_control *vctrl,
 692		  enum vcap_type vt, enum vcap_actionfield_set actionset)
 693{
 694	/* Check that the actionset exists in the vcap actionset list */
 695	if (actionset >= vctrl->vcaps[vt].actionfield_set_size)
 696		return NULL;
 697	return vctrl->vcaps[vt].actionfield_set_map[actionset];
 698}
 699
 700const struct vcap_set *
 701vcap_actionfieldset(struct vcap_control *vctrl,
 702		    enum vcap_type vt, enum vcap_actionfield_set actionset)
 703{
 704	const struct vcap_set *aset;
 705
 706	/* Check that the actionset exists in the vcap actionset list */
 707	if (actionset >= vctrl->vcaps[vt].actionfield_set_size)
 708		return NULL;
 709	aset = &vctrl->vcaps[vt].actionfield_set[actionset];
 710	if (aset->sw_per_item == 0 || aset->sw_per_item > vctrl->vcaps[vt].sw_count)
 711		return NULL;
 712	return aset;
 713}
 714
 715/* Return the typegroup table for the matching actionset (using subword size) */
 716const struct vcap_typegroup *
 717vcap_actionfield_typegroup(struct vcap_control *vctrl,
 718			   enum vcap_type vt, enum vcap_actionfield_set actionset)
 719{
 720	const struct vcap_set *aset = vcap_actionfieldset(vctrl, vt, actionset);
 721
 722	/* Check that the actionset is valid */
 723	if (!aset)
 724		return NULL;
 725	return vctrl->vcaps[vt].actionfield_set_typegroups[aset->sw_per_item];
 726}
 727
 728/* Return the number of actionfields in the actionset */
 729int vcap_actionfield_count(struct vcap_control *vctrl,
 730			   enum vcap_type vt,
 731			   enum vcap_actionfield_set actionset)
 732{
 733	/* Check that the actionset exists in the vcap actionset list */
 734	if (actionset >= vctrl->vcaps[vt].actionfield_set_size)
 735		return 0;
 736	return vctrl->vcaps[vt].actionfield_set_map_size[actionset];
 737}
 738
 739static void vcap_encode_actionfield(struct vcap_rule_internal *ri,
 740				    const struct vcap_client_actionfield *af,
 741				    const struct vcap_field *rf,
 742				    const struct vcap_typegroup *tgt)
 743{
 744	int act_width = ri->vctrl->vcaps[ri->admin->vtype].act_width;
 745
 746	struct vcap_cache_data *cache = &ri->admin->cache;
 747	struct vcap_stream_iter iter;
 748	const u8 *value;
 749
 750	/* Encode the action field in the stream, respecting the subword width */
 751	switch (af->ctrl.type) {
 752	case VCAP_FIELD_BIT:
 753		value = &af->data.u1.value;
 754		break;
 755	case VCAP_FIELD_U32:
 756		value = (const u8 *)&af->data.u32.value;
 757		break;
 758	case VCAP_FIELD_U48:
 759		value = af->data.u48.value;
 760		break;
 761	case VCAP_FIELD_U56:
 762		value = af->data.u56.value;
 763		break;
 764	case VCAP_FIELD_U64:
 765		value = af->data.u64.value;
 766		break;
 767	case VCAP_FIELD_U72:
 768		value = af->data.u72.value;
 769		break;
 770	case VCAP_FIELD_U112:
 771		value = af->data.u112.value;
 772		break;
 773	case VCAP_FIELD_U128:
 774		value = af->data.u128.value;
 775		break;
 776	}
 777	vcap_iter_init(&iter, act_width, tgt, rf->offset);
 778	vcap_encode_field(cache->actionstream, &iter, rf->width, value);
 779}
 780
 781static void vcap_encode_actionfield_typegroups(struct vcap_rule_internal *ri,
 782					       const struct vcap_typegroup *tgt)
 783{
 784	int sw_width = ri->vctrl->vcaps[ri->admin->vtype].act_width;
 785	struct vcap_cache_data *cache = &ri->admin->cache;
 786
 787	/* Encode the typegroup bits for the actionstream respecting the subword
 788	 * width.
 789	 */
 790	vcap_encode_typegroups(cache->actionstream, sw_width, tgt, false);
 791}
 792
 793static int vcap_encode_rule_actionset(struct vcap_rule_internal *ri)
 794{
 795	const struct vcap_client_actionfield *caf;
 796	const struct vcap_typegroup *tg_table;
 797	struct vcap_client_actionfield tempaf;
 798	const struct vcap_field *af_table;
 799	int actionset_size;
 800
 801	/* Get a valid set of actionset fields for the specific actionset */
 802	af_table = vcap_actionfields(ri->vctrl, ri->admin->vtype,
 803				     ri->data.actionset);
 804	if (!af_table) {
 805		pr_err("%s:%d: no fields available for this actionset: %d\n",
 806		       __func__, __LINE__, ri->data.actionset);
 807		return -EINVAL;
 808	}
 809	/* Get a valid typegroup for the specific actionset */
 810	tg_table = vcap_actionfield_typegroup(ri->vctrl, ri->admin->vtype,
 811					      ri->data.actionset);
 812	if (!tg_table) {
 813		pr_err("%s:%d: no typegroups available for this actionset: %d\n",
 814		       __func__, __LINE__, ri->data.actionset);
 815		return -EINVAL;
 816	}
 817	/* Get a valid actionset size for the specific actionset */
 818	actionset_size = vcap_actionfield_count(ri->vctrl, ri->admin->vtype,
 819						ri->data.actionset);
 820	if (actionset_size == 0) {
 821		pr_err("%s:%d: zero field count for this actionset: %d\n",
 822		       __func__, __LINE__, ri->data.actionset);
 823		return -EINVAL;
 824	}
 825	/* Iterate over the actionfields in the rule
 826	 * and encode these bits
 827	 */
 828	if (list_empty(&ri->data.actionfields))
 829		pr_warn("%s:%d: no actionfields in the rule\n",
 830			__func__, __LINE__);
 831	list_for_each_entry(caf, &ri->data.actionfields, ctrl.list) {
 832		/* Check that the client action exists in the actionset */
 833		if (caf->ctrl.action >= actionset_size) {
 834			pr_err("%s:%d: action %d is not in vcap\n",
 835			       __func__, __LINE__, caf->ctrl.action);
 836			return -EINVAL;
 837		}
 838		vcap_copy_from_client_actionfield(&ri->data, &tempaf, caf);
 839		vcap_encode_actionfield(ri, &tempaf,
 840					&af_table[caf->ctrl.action], tg_table);
 841	}
 842	/* Add typegroup bits to the entry bitstreams */
 843	vcap_encode_actionfield_typegroups(ri, tg_table);
 844	return 0;
 845}
 846
 847static int vcap_encode_rule(struct vcap_rule_internal *ri)
 848{
 849	int err;
 850
 851	err = vcap_encode_rule_keyset(ri);
 852	if (err)
 853		return err;
 854	err = vcap_encode_rule_actionset(ri);
 855	if (err)
 856		return err;
 857	return 0;
 858}
 859
 860int vcap_api_check(struct vcap_control *ctrl)
 861{
 862	if (!ctrl) {
 863		pr_err("%s:%d: vcap control is missing\n", __func__, __LINE__);
 864		return -EINVAL;
 865	}
 866	if (!ctrl->ops || !ctrl->ops->validate_keyset ||
 867	    !ctrl->ops->add_default_fields || !ctrl->ops->cache_erase ||
 868	    !ctrl->ops->cache_write || !ctrl->ops->cache_read ||
 869	    !ctrl->ops->init || !ctrl->ops->update || !ctrl->ops->move ||
 870	    !ctrl->ops->port_info) {
 871		pr_err("%s:%d: client operations are missing\n",
 872		       __func__, __LINE__);
 873		return -ENOENT;
 874	}
 875	return 0;
 876}
 877
 878void vcap_erase_cache(struct vcap_rule_internal *ri)
 879{
 880	ri->vctrl->ops->cache_erase(ri->admin);
 881}
 882
 883/* Update the keyset for the rule */
 884int vcap_set_rule_set_keyset(struct vcap_rule *rule,
 885			     enum vcap_keyfield_set keyset)
 886{
 887	struct vcap_rule_internal *ri = to_intrule(rule);
 888	const struct vcap_set *kset;
 889	int sw_width;
 890
 891	kset = vcap_keyfieldset(ri->vctrl, ri->admin->vtype, keyset);
 892	/* Check that the keyset is valid */
 893	if (!kset)
 894		return -EINVAL;
 895	ri->keyset_sw = kset->sw_per_item;
 896	sw_width = ri->vctrl->vcaps[ri->admin->vtype].sw_width;
 897	ri->keyset_sw_regs = DIV_ROUND_UP(sw_width, 32);
 898	ri->data.keyset = keyset;
 899	return 0;
 900}
 901EXPORT_SYMBOL_GPL(vcap_set_rule_set_keyset);
 902
 903/* Update the actionset for the rule */
 904int vcap_set_rule_set_actionset(struct vcap_rule *rule,
 905				enum vcap_actionfield_set actionset)
 906{
 907	struct vcap_rule_internal *ri = to_intrule(rule);
 908	const struct vcap_set *aset;
 909	int act_width;
 910
 911	aset = vcap_actionfieldset(ri->vctrl, ri->admin->vtype, actionset);
 912	/* Check that the actionset is valid */
 913	if (!aset)
 914		return -EINVAL;
 915	ri->actionset_sw = aset->sw_per_item;
 916	act_width = ri->vctrl->vcaps[ri->admin->vtype].act_width;
 917	ri->actionset_sw_regs = DIV_ROUND_UP(act_width, 32);
 918	ri->data.actionset = actionset;
 919	return 0;
 920}
 921EXPORT_SYMBOL_GPL(vcap_set_rule_set_actionset);
 922
 923/* Check if a rule with this id exists */
 924static bool vcap_rule_exists(struct vcap_control *vctrl, u32 id)
 
 925{
 926	struct vcap_rule_internal *ri;
 927	struct vcap_admin *admin;
 928
 929	/* Look for the rule id in all vcaps */
 930	list_for_each_entry(admin, &vctrl->list, list)
 931		list_for_each_entry(ri, &admin->rules, list)
 932			if (ri->data.id == id)
 933				return true;
 934	return false;
 935}
 936
 937/* Find a rule with a provided rule id return a locked vcap */
 938static struct vcap_rule_internal *
 939vcap_get_locked_rule(struct vcap_control *vctrl, u32 id)
 940{
 941	struct vcap_rule_internal *ri;
 942	struct vcap_admin *admin;
 943
 944	/* Look for the rule id in all vcaps */
 945	list_for_each_entry(admin, &vctrl->list, list) {
 946		mutex_lock(&admin->lock);
 947		list_for_each_entry(ri, &admin->rules, list)
 948			if (ri->data.id == id)
 949				return ri;
 950		mutex_unlock(&admin->lock);
 951	}
 952	return NULL;
 953}
 954
 955/* Find a rule id with a provided cookie */
 956int vcap_lookup_rule_by_cookie(struct vcap_control *vctrl, u64 cookie)
 957{
 958	struct vcap_rule_internal *ri;
 959	struct vcap_admin *admin;
 960	int id = 0;
 961
 962	/* Look for the rule id in all vcaps */
 963	list_for_each_entry(admin, &vctrl->list, list) {
 964		mutex_lock(&admin->lock);
 965		list_for_each_entry(ri, &admin->rules, list) {
 966			if (ri->data.cookie == cookie) {
 967				id = ri->data.id;
 968				break;
 969			}
 970		}
 971		mutex_unlock(&admin->lock);
 972		if (id)
 973			return id;
 974	}
 975	return -ENOENT;
 976}
 977EXPORT_SYMBOL_GPL(vcap_lookup_rule_by_cookie);
 978
 979/* Get number of rules in a vcap instance lookup chain id range */
 980int vcap_admin_rule_count(struct vcap_admin *admin, int cid)
 981{
 982	int max_cid = roundup(cid + 1, VCAP_CID_LOOKUP_SIZE);
 983	int min_cid = rounddown(cid, VCAP_CID_LOOKUP_SIZE);
 984	struct vcap_rule_internal *elem;
 985	int count = 0;
 986
 987	list_for_each_entry(elem, &admin->rules, list) {
 988		mutex_lock(&admin->lock);
 989		if (elem->data.vcap_chain_id >= min_cid &&
 990		    elem->data.vcap_chain_id < max_cid)
 991			++count;
 992		mutex_unlock(&admin->lock);
 993	}
 994	return count;
 995}
 996EXPORT_SYMBOL_GPL(vcap_admin_rule_count);
 997
 998/* Make a copy of the rule, shallow or full */
 999static struct vcap_rule_internal *vcap_dup_rule(struct vcap_rule_internal *ri,
1000						bool full)
1001{
1002	struct vcap_client_actionfield *caf, *newcaf;
1003	struct vcap_client_keyfield *ckf, *newckf;
1004	struct vcap_rule_internal *duprule;
1005
1006	/* Allocate the client part */
1007	duprule = kzalloc(sizeof(*duprule), GFP_KERNEL);
1008	if (!duprule)
1009		return ERR_PTR(-ENOMEM);
1010	*duprule = *ri;
1011	/* Not inserted in the VCAP */
1012	INIT_LIST_HEAD(&duprule->list);
1013	/* No elements in these lists */
1014	INIT_LIST_HEAD(&duprule->data.keyfields);
1015	INIT_LIST_HEAD(&duprule->data.actionfields);
1016
1017	/* A full rule copy includes keys and actions */
1018	if (!full)
1019		return duprule;
1020
1021	list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list) {
1022		newckf = kmemdup(ckf, sizeof(*newckf), GFP_KERNEL);
1023		if (!newckf)
1024			goto err;
1025		list_add_tail(&newckf->ctrl.list, &duprule->data.keyfields);
1026	}
1027
1028	list_for_each_entry(caf, &ri->data.actionfields, ctrl.list) {
1029		newcaf = kmemdup(caf, sizeof(*newcaf), GFP_KERNEL);
1030		if (!newcaf)
1031			goto err;
1032		list_add_tail(&newcaf->ctrl.list, &duprule->data.actionfields);
1033	}
1034
1035	return duprule;
1036
1037err:
1038	list_for_each_entry_safe(ckf, newckf, &duprule->data.keyfields, ctrl.list) {
1039		list_del(&ckf->ctrl.list);
1040		kfree(ckf);
1041	}
1042
1043	list_for_each_entry_safe(caf, newcaf, &duprule->data.actionfields, ctrl.list) {
1044		list_del(&caf->ctrl.list);
1045		kfree(caf);
1046	}
1047
1048	kfree(duprule);
1049	return ERR_PTR(-ENOMEM);
1050}
1051
1052static void vcap_apply_width(u8 *dst, int width, int bytes)
1053{
1054	u8 bmask;
1055	int idx;
1056
1057	for (idx = 0; idx < bytes; idx++) {
1058		if (width > 0)
1059			if (width < 8)
1060				bmask = (1 << width) - 1;
1061			else
1062				bmask = ~0;
1063		else
1064			bmask = 0;
1065		dst[idx] &= bmask;
1066		width -= 8;
1067	}
1068}
1069
1070static void vcap_copy_from_w32be(u8 *dst, u8 *src, int size, int width)
1071{
1072	int idx, ridx, wstart, nidx;
1073	int tail_bytes = (((size + 4) >> 2) << 2) - size;
1074
1075	for (idx = 0, ridx = size - 1; idx < size; ++idx, --ridx) {
1076		wstart = (idx >> 2) << 2;
1077		nidx = wstart + 3 - (idx & 0x3);
1078		if (nidx >= size)
1079			nidx -= tail_bytes;
1080		dst[nidx] = src[ridx];
1081	}
1082
1083	vcap_apply_width(dst, width, size);
1084}
1085
1086static void vcap_copy_action_bit_field(struct vcap_u1_action *field, u8 *value)
1087{
1088	field->value = (*value) & 0x1;
1089}
1090
1091static void vcap_copy_limited_actionfield(u8 *dstvalue, u8 *srcvalue,
1092					  int width, int bytes)
1093{
1094	memcpy(dstvalue, srcvalue, bytes);
1095	vcap_apply_width(dstvalue, width, bytes);
1096}
1097
1098static void vcap_copy_to_client_actionfield(struct vcap_rule_internal *ri,
1099					    struct vcap_client_actionfield *field,
1100					    u8 *value, u16 width)
1101{
1102	int field_size = actionfield_size_table[field->ctrl.type];
1103
1104	if (ri->admin->w32be) {
1105		switch (field->ctrl.type) {
1106		case VCAP_FIELD_BIT:
1107			vcap_copy_action_bit_field(&field->data.u1, value);
1108			break;
1109		case VCAP_FIELD_U32:
1110			vcap_copy_limited_actionfield((u8 *)&field->data.u32.value,
1111						      value,
1112						      width, field_size);
1113			break;
1114		case VCAP_FIELD_U48:
1115			vcap_copy_from_w32be(field->data.u48.value, value,
1116					     field_size, width);
1117			break;
1118		case VCAP_FIELD_U56:
1119			vcap_copy_from_w32be(field->data.u56.value, value,
1120					     field_size, width);
1121			break;
1122		case VCAP_FIELD_U64:
1123			vcap_copy_from_w32be(field->data.u64.value, value,
1124					     field_size, width);
1125			break;
1126		case VCAP_FIELD_U72:
1127			vcap_copy_from_w32be(field->data.u72.value, value,
1128					     field_size, width);
1129			break;
1130		case VCAP_FIELD_U112:
1131			vcap_copy_from_w32be(field->data.u112.value, value,
1132					     field_size, width);
1133			break;
1134		case VCAP_FIELD_U128:
1135			vcap_copy_from_w32be(field->data.u128.value, value,
1136					     field_size, width);
1137			break;
1138		}
1139	} else {
1140		switch (field->ctrl.type) {
1141		case VCAP_FIELD_BIT:
1142			vcap_copy_action_bit_field(&field->data.u1, value);
1143			break;
1144		case VCAP_FIELD_U32:
1145			vcap_copy_limited_actionfield((u8 *)&field->data.u32.value,
1146						      value,
1147						      width, field_size);
1148			break;
1149		case VCAP_FIELD_U48:
1150			vcap_copy_limited_actionfield(field->data.u48.value,
1151						      value,
1152						      width, field_size);
1153			break;
1154		case VCAP_FIELD_U56:
1155			vcap_copy_limited_actionfield(field->data.u56.value,
1156						      value,
1157						      width, field_size);
1158			break;
1159		case VCAP_FIELD_U64:
1160			vcap_copy_limited_actionfield(field->data.u64.value,
1161						      value,
1162						      width, field_size);
1163			break;
1164		case VCAP_FIELD_U72:
1165			vcap_copy_limited_actionfield(field->data.u72.value,
1166						      value,
1167						      width, field_size);
1168			break;
1169		case VCAP_FIELD_U112:
1170			vcap_copy_limited_actionfield(field->data.u112.value,
1171						      value,
1172						      width, field_size);
1173			break;
1174		case VCAP_FIELD_U128:
1175			vcap_copy_limited_actionfield(field->data.u128.value,
1176						      value,
1177						      width, field_size);
1178			break;
1179		}
1180	}
1181}
1182
1183static void vcap_copy_key_bit_field(struct vcap_u1_key *field,
1184				    u8 *value, u8 *mask)
1185{
1186	field->value = (*value) & 0x1;
1187	field->mask = (*mask) & 0x1;
1188}
1189
1190static void vcap_copy_limited_keyfield(u8 *dstvalue, u8 *dstmask,
1191				       u8 *srcvalue, u8 *srcmask,
1192				       int width, int bytes)
1193{
1194	memcpy(dstvalue, srcvalue, bytes);
1195	vcap_apply_width(dstvalue, width, bytes);
1196	memcpy(dstmask, srcmask, bytes);
1197	vcap_apply_width(dstmask, width, bytes);
1198}
1199
1200static void vcap_copy_to_client_keyfield(struct vcap_rule_internal *ri,
1201					 struct vcap_client_keyfield *field,
1202					 u8 *value, u8 *mask, u16 width)
1203{
1204	int field_size = keyfield_size_table[field->ctrl.type] / 2;
1205
1206	if (ri->admin->w32be) {
1207		switch (field->ctrl.type) {
1208		case VCAP_FIELD_BIT:
1209			vcap_copy_key_bit_field(&field->data.u1, value, mask);
1210			break;
1211		case VCAP_FIELD_U32:
1212			vcap_copy_limited_keyfield((u8 *)&field->data.u32.value,
1213						   (u8 *)&field->data.u32.mask,
1214						   value, mask,
1215						   width, field_size);
1216			break;
1217		case VCAP_FIELD_U48:
1218			vcap_copy_from_w32be(field->data.u48.value, value,
1219					     field_size, width);
1220			vcap_copy_from_w32be(field->data.u48.mask,  mask,
1221					     field_size, width);
1222			break;
1223		case VCAP_FIELD_U56:
1224			vcap_copy_from_w32be(field->data.u56.value, value,
1225					     field_size, width);
1226			vcap_copy_from_w32be(field->data.u56.mask,  mask,
1227					     field_size, width);
1228			break;
1229		case VCAP_FIELD_U64:
1230			vcap_copy_from_w32be(field->data.u64.value, value,
1231					     field_size, width);
1232			vcap_copy_from_w32be(field->data.u64.mask,  mask,
1233					     field_size, width);
1234			break;
1235		case VCAP_FIELD_U72:
1236			vcap_copy_from_w32be(field->data.u72.value, value,
1237					     field_size, width);
1238			vcap_copy_from_w32be(field->data.u72.mask,  mask,
1239					     field_size, width);
1240			break;
1241		case VCAP_FIELD_U112:
1242			vcap_copy_from_w32be(field->data.u112.value, value,
1243					     field_size, width);
1244			vcap_copy_from_w32be(field->data.u112.mask,  mask,
1245					     field_size, width);
1246			break;
1247		case VCAP_FIELD_U128:
1248			vcap_copy_from_w32be(field->data.u128.value, value,
1249					     field_size, width);
1250			vcap_copy_from_w32be(field->data.u128.mask,  mask,
1251					     field_size, width);
1252			break;
1253		}
1254	} else {
1255		switch (field->ctrl.type) {
1256		case VCAP_FIELD_BIT:
1257			vcap_copy_key_bit_field(&field->data.u1, value, mask);
1258			break;
1259		case VCAP_FIELD_U32:
1260			vcap_copy_limited_keyfield((u8 *)&field->data.u32.value,
1261						   (u8 *)&field->data.u32.mask,
1262						   value, mask,
1263						   width, field_size);
1264			break;
1265		case VCAP_FIELD_U48:
1266			vcap_copy_limited_keyfield(field->data.u48.value,
1267						   field->data.u48.mask,
1268						   value, mask,
1269						   width, field_size);
1270			break;
1271		case VCAP_FIELD_U56:
1272			vcap_copy_limited_keyfield(field->data.u56.value,
1273						   field->data.u56.mask,
1274						   value, mask,
1275						   width, field_size);
1276			break;
1277		case VCAP_FIELD_U64:
1278			vcap_copy_limited_keyfield(field->data.u64.value,
1279						   field->data.u64.mask,
1280						   value, mask,
1281						   width, field_size);
1282			break;
1283		case VCAP_FIELD_U72:
1284			vcap_copy_limited_keyfield(field->data.u72.value,
1285						   field->data.u72.mask,
1286						   value, mask,
1287						   width, field_size);
1288			break;
1289		case VCAP_FIELD_U112:
1290			vcap_copy_limited_keyfield(field->data.u112.value,
1291						   field->data.u112.mask,
1292						   value, mask,
1293						   width, field_size);
1294			break;
1295		case VCAP_FIELD_U128:
1296			vcap_copy_limited_keyfield(field->data.u128.value,
1297						   field->data.u128.mask,
1298						   value, mask,
1299						   width, field_size);
1300			break;
1301		}
1302	}
1303}
1304
1305static void vcap_rule_alloc_keyfield(struct vcap_rule_internal *ri,
1306				     const struct vcap_field *keyfield,
1307				     enum vcap_key_field key,
1308				     u8 *value, u8 *mask)
1309{
1310	struct vcap_client_keyfield *field;
1311
1312	field = kzalloc(sizeof(*field), GFP_KERNEL);
1313	if (!field)
1314		return;
1315	INIT_LIST_HEAD(&field->ctrl.list);
1316	field->ctrl.key = key;
1317	field->ctrl.type = keyfield->type;
1318	vcap_copy_to_client_keyfield(ri, field, value, mask, keyfield->width);
1319	list_add_tail(&field->ctrl.list, &ri->data.keyfields);
1320}
1321
1322/* Read key data from a VCAP address and discover if there is a rule keyset
1323 * here
1324 */
1325static bool
1326vcap_verify_actionstream_actionset(struct vcap_control *vctrl,
1327				   enum vcap_type vt,
1328				   u32 *actionstream,
1329				   enum vcap_actionfield_set actionset)
1330{
1331	const struct vcap_typegroup *tgt;
1332	const struct vcap_field *fields;
1333	const struct vcap_set *info;
1334
1335	if (vcap_actionfield_count(vctrl, vt, actionset) == 0)
1336		return false;
1337
1338	info = vcap_actionfieldset(vctrl, vt, actionset);
1339	/* Check that the actionset is valid */
1340	if (!info)
1341		return false;
1342
1343	/* a type_id of value -1 means that there is no type field */
1344	if (info->type_id == (u8)-1)
1345		return true;
1346
1347	/* Get a valid typegroup for the specific actionset */
1348	tgt = vcap_actionfield_typegroup(vctrl, vt, actionset);
1349	if (!tgt)
1350		return false;
1351
1352	fields = vcap_actionfields(vctrl, vt, actionset);
1353	if (!fields)
1354		return false;
1355
1356	/* Later this will be expanded with a check of the type id */
1357	return true;
1358}
1359
1360/* Find the subword width of the action typegroup that matches the stream data
1361 */
1362static int vcap_find_actionstream_typegroup_sw(struct vcap_control *vctrl,
1363					       enum vcap_type vt, u32 *stream,
1364					       int sw_max)
1365{
1366	const struct vcap_typegroup **tgt;
1367	int sw_idx, res;
1368
1369	tgt = vctrl->vcaps[vt].actionfield_set_typegroups;
1370	/* Try the longest subword match first */
1371	for (sw_idx = vctrl->vcaps[vt].sw_count; sw_idx >= 0; sw_idx--) {
1372		if (!tgt[sw_idx])
1373			continue;
1374		res = vcap_verify_typegroups(stream, vctrl->vcaps[vt].act_width,
1375					     tgt[sw_idx], false, sw_max);
1376		if (res == 0)
1377			return sw_idx;
1378	}
1379	return -EINVAL;
1380}
1381
1382/* Verify that the typegroup information, subword count, actionset and type id
1383 * are in sync and correct, return the actionset
1384 */
1385static enum vcap_actionfield_set
1386vcap_find_actionstream_actionset(struct vcap_control *vctrl,
1387				 enum vcap_type vt,
1388				 u32 *stream,
1389				 int sw_max)
1390{
1391	const struct vcap_set *actionfield_set;
1392	int sw_count, idx;
1393	bool res;
1394
1395	sw_count = vcap_find_actionstream_typegroup_sw(vctrl, vt, stream,
1396						       sw_max);
1397	if (sw_count < 0)
1398		return sw_count;
1399
1400	actionfield_set = vctrl->vcaps[vt].actionfield_set;
1401	for (idx = 0; idx < vctrl->vcaps[vt].actionfield_set_size; ++idx) {
1402		if (actionfield_set[idx].sw_per_item != sw_count)
1403			continue;
1404
1405		res = vcap_verify_actionstream_actionset(vctrl, vt,
1406							 stream, idx);
1407		if (res)
1408			return idx;
1409	}
1410	return -EINVAL;
1411}
1412
1413/* Store action value in an element in a list for the client */
1414static void vcap_rule_alloc_actionfield(struct vcap_rule_internal *ri,
1415					const struct vcap_field *actionfield,
1416					enum vcap_action_field action,
1417					u8 *value)
1418{
1419	struct vcap_client_actionfield *field;
1420
1421	field = kzalloc(sizeof(*field), GFP_KERNEL);
1422	if (!field)
1423		return;
1424	INIT_LIST_HEAD(&field->ctrl.list);
1425	field->ctrl.action = action;
1426	field->ctrl.type = actionfield->type;
1427	vcap_copy_to_client_actionfield(ri, field, value, actionfield->width);
1428	list_add_tail(&field->ctrl.list, &ri->data.actionfields);
1429}
1430
1431static int vcap_decode_actionset(struct vcap_rule_internal *ri)
1432{
1433	struct vcap_control *vctrl = ri->vctrl;
1434	struct vcap_admin *admin = ri->admin;
1435	const struct vcap_field *actionfield;
1436	enum vcap_actionfield_set actionset;
1437	enum vcap_type vt = admin->vtype;
1438	const struct vcap_typegroup *tgt;
1439	struct vcap_stream_iter iter;
1440	int idx, res, actfield_count;
1441	u32 *actstream;
1442	u8 value[16];
1443
1444	actstream = admin->cache.actionstream;
1445	res = vcap_find_actionstream_actionset(vctrl, vt, actstream, 0);
1446	if (res < 0) {
1447		pr_err("%s:%d: could not find valid actionset: %d\n",
1448		       __func__, __LINE__, res);
1449		return -EINVAL;
1450	}
1451	actionset = res;
1452	actfield_count = vcap_actionfield_count(vctrl, vt, actionset);
1453	actionfield = vcap_actionfields(vctrl, vt, actionset);
1454	tgt = vcap_actionfield_typegroup(vctrl, vt, actionset);
1455	/* Start decoding the stream */
1456	for (idx = 0; idx < actfield_count; ++idx) {
1457		if (actionfield[idx].width <= 0)
1458			continue;
1459		/* Get the action */
1460		memset(value, 0, DIV_ROUND_UP(actionfield[idx].width, 8));
1461		vcap_iter_init(&iter, vctrl->vcaps[vt].act_width, tgt,
1462			       actionfield[idx].offset);
1463		vcap_decode_field(actstream, &iter, actionfield[idx].width,
1464				  value);
1465		/* Skip if no bits are set */
1466		if (vcap_bitarray_zero(actionfield[idx].width, value))
1467			continue;
1468		vcap_rule_alloc_actionfield(ri, &actionfield[idx], idx, value);
1469		/* Later the action id will also be checked */
1470	}
1471	return vcap_set_rule_set_actionset((struct vcap_rule *)ri, actionset);
1472}
1473
1474static int vcap_decode_keyset(struct vcap_rule_internal *ri)
1475{
1476	struct vcap_control *vctrl = ri->vctrl;
1477	struct vcap_stream_iter kiter, miter;
1478	struct vcap_admin *admin = ri->admin;
1479	enum vcap_keyfield_set keysets[10];
1480	const struct vcap_field *keyfield;
1481	enum vcap_type vt = admin->vtype;
1482	const struct vcap_typegroup *tgt;
1483	struct vcap_keyset_list matches;
1484	enum vcap_keyfield_set keyset;
1485	int idx, res, keyfield_count;
1486	u32 *maskstream;
1487	u32 *keystream;
1488	u8 value[16];
1489	u8 mask[16];
1490
1491	keystream = admin->cache.keystream;
1492	maskstream = admin->cache.maskstream;
1493	matches.keysets = keysets;
1494	matches.cnt = 0;
1495	matches.max = ARRAY_SIZE(keysets);
1496	res = vcap_find_keystream_keysets(vctrl, vt, keystream, maskstream,
1497					  false, 0, &matches);
1498	if (res < 0) {
1499		pr_err("%s:%d: could not find valid keysets: %d\n",
1500		       __func__, __LINE__, res);
1501		return -EINVAL;
1502	}
1503	keyset = matches.keysets[0];
1504	keyfield_count = vcap_keyfield_count(vctrl, vt, keyset);
1505	keyfield = vcap_keyfields(vctrl, vt, keyset);
1506	tgt = vcap_keyfield_typegroup(vctrl, vt, keyset);
1507	/* Start decoding the streams */
1508	for (idx = 0; idx < keyfield_count; ++idx) {
1509		if (keyfield[idx].width <= 0)
1510			continue;
1511		/* First get the mask */
1512		memset(mask, 0, DIV_ROUND_UP(keyfield[idx].width, 8));
1513		vcap_iter_init(&miter, vctrl->vcaps[vt].sw_width, tgt,
1514			       keyfield[idx].offset);
1515		vcap_decode_field(maskstream, &miter, keyfield[idx].width,
1516				  mask);
1517		/* Skip if no mask bits are set */
1518		if (vcap_bitarray_zero(keyfield[idx].width, mask))
1519			continue;
1520		/* Get the key */
1521		memset(value, 0, DIV_ROUND_UP(keyfield[idx].width, 8));
1522		vcap_iter_init(&kiter, vctrl->vcaps[vt].sw_width, tgt,
1523			       keyfield[idx].offset);
1524		vcap_decode_field(keystream, &kiter, keyfield[idx].width,
1525				  value);
1526		vcap_rule_alloc_keyfield(ri, &keyfield[idx], idx, value, mask);
1527	}
1528	return vcap_set_rule_set_keyset((struct vcap_rule *)ri, keyset);
1529}
1530
1531/* Read VCAP content into the VCAP cache */
1532static int vcap_read_rule(struct vcap_rule_internal *ri)
1533{
1534	struct vcap_admin *admin = ri->admin;
1535	int sw_idx, ent_idx = 0, act_idx = 0;
1536	u32 addr = ri->addr;
1537
1538	if (!ri->size || !ri->keyset_sw_regs || !ri->actionset_sw_regs) {
1539		pr_err("%s:%d: rule is empty\n", __func__, __LINE__);
1540		return -EINVAL;
1541	}
1542	vcap_erase_cache(ri);
1543	/* Use the values in the streams to read the VCAP cache */
1544	for (sw_idx = 0; sw_idx < ri->size; sw_idx++, addr++) {
1545		ri->vctrl->ops->update(ri->ndev, admin, VCAP_CMD_READ,
1546				       VCAP_SEL_ALL, addr);
1547		ri->vctrl->ops->cache_read(ri->ndev, admin,
1548					   VCAP_SEL_ENTRY, ent_idx,
1549					   ri->keyset_sw_regs);
1550		ri->vctrl->ops->cache_read(ri->ndev, admin,
1551					   VCAP_SEL_ACTION, act_idx,
1552					   ri->actionset_sw_regs);
1553		if (sw_idx == 0)
1554			ri->vctrl->ops->cache_read(ri->ndev, admin,
1555						   VCAP_SEL_COUNTER,
1556						   ri->counter_id, 0);
1557		ent_idx += ri->keyset_sw_regs;
1558		act_idx += ri->actionset_sw_regs;
1559	}
1560	return 0;
1561}
1562
1563/* Write VCAP cache content to the VCAP HW instance */
1564static int vcap_write_rule(struct vcap_rule_internal *ri)
1565{
1566	struct vcap_admin *admin = ri->admin;
1567	int sw_idx, ent_idx = 0, act_idx = 0;
1568	u32 addr = ri->addr;
1569
1570	if (!ri->size || !ri->keyset_sw_regs || !ri->actionset_sw_regs) {
1571		pr_err("%s:%d: rule is empty\n", __func__, __LINE__);
1572		return -EINVAL;
1573	}
1574	/* Use the values in the streams to write the VCAP cache */
1575	for (sw_idx = 0; sw_idx < ri->size; sw_idx++, addr++) {
1576		ri->vctrl->ops->cache_write(ri->ndev, admin,
1577					    VCAP_SEL_ENTRY, ent_idx,
1578					    ri->keyset_sw_regs);
1579		ri->vctrl->ops->cache_write(ri->ndev, admin,
1580					    VCAP_SEL_ACTION, act_idx,
1581					    ri->actionset_sw_regs);
1582		ri->vctrl->ops->update(ri->ndev, admin, VCAP_CMD_WRITE,
1583				       VCAP_SEL_ALL, addr);
1584		ent_idx += ri->keyset_sw_regs;
1585		act_idx += ri->actionset_sw_regs;
1586	}
1587	return 0;
1588}
1589
1590static int vcap_write_counter(struct vcap_rule_internal *ri,
1591			      struct vcap_counter *ctr)
1592{
1593	struct vcap_admin *admin = ri->admin;
1594
1595	admin->cache.counter = ctr->value;
1596	admin->cache.sticky = ctr->sticky;
1597	ri->vctrl->ops->cache_write(ri->ndev, admin, VCAP_SEL_COUNTER,
1598				    ri->counter_id, 0);
1599	ri->vctrl->ops->update(ri->ndev, admin, VCAP_CMD_WRITE,
1600			       VCAP_SEL_COUNTER, ri->addr);
1601	return 0;
1602}
1603
1604/* Convert a chain id to a VCAP lookup index */
1605int vcap_chain_id_to_lookup(struct vcap_admin *admin, int cur_cid)
1606{
1607	int lookup_first = admin->vinst * admin->lookups_per_instance;
1608	int lookup_last = lookup_first + admin->lookups_per_instance;
1609	int cid_next = admin->first_cid + VCAP_CID_LOOKUP_SIZE;
1610	int cid = admin->first_cid;
1611	int lookup;
1612
1613	for (lookup = lookup_first; lookup < lookup_last; ++lookup,
1614	     cid += VCAP_CID_LOOKUP_SIZE, cid_next += VCAP_CID_LOOKUP_SIZE)
1615		if (cur_cid >= cid && cur_cid < cid_next)
1616			return lookup;
1617	return 0;
1618}
1619EXPORT_SYMBOL_GPL(vcap_chain_id_to_lookup);
1620
1621/* Lookup a vcap instance using chain id */
1622struct vcap_admin *vcap_find_admin(struct vcap_control *vctrl, int cid)
1623{
1624	struct vcap_admin *admin;
1625
1626	if (vcap_api_check(vctrl))
1627		return NULL;
1628
1629	list_for_each_entry(admin, &vctrl->list, list) {
1630		if (cid >= admin->first_cid && cid <= admin->last_cid)
1631			return admin;
1632	}
1633	return NULL;
1634}
1635EXPORT_SYMBOL_GPL(vcap_find_admin);
1636
1637/* Is this the last admin instance ordered by chain id and direction */
1638static bool vcap_admin_is_last(struct vcap_control *vctrl,
1639			       struct vcap_admin *admin,
1640			       bool ingress)
1641{
1642	struct vcap_admin *iter, *last = NULL;
1643	int max_cid = 0;
1644
1645	list_for_each_entry(iter, &vctrl->list, list) {
1646		if (iter->first_cid > max_cid &&
1647		    iter->ingress == ingress) {
1648			last = iter;
1649			max_cid = iter->first_cid;
1650		}
1651	}
1652	if (!last)
1653		return false;
1654
1655	return admin == last;
1656}
1657
1658/* Calculate the value used for chaining VCAP rules */
1659int vcap_chain_offset(struct vcap_control *vctrl, int from_cid, int to_cid)
1660{
1661	int diff = to_cid - from_cid;
1662
1663	if (diff < 0) /* Wrong direction */
1664		return diff;
1665	to_cid %= VCAP_CID_LOOKUP_SIZE;
1666	if (to_cid == 0)  /* Destination aligned to a lookup == no chaining */
1667		return 0;
1668	diff %= VCAP_CID_LOOKUP_SIZE;  /* Limit to a value within a lookup */
1669	return diff;
1670}
1671EXPORT_SYMBOL_GPL(vcap_chain_offset);
1672
1673/* Is the next chain id in one of the following lookups
1674 * For now this does not support filters linked to other filters using
1675 * keys and actions. That will be added later.
1676 */
1677bool vcap_is_next_lookup(struct vcap_control *vctrl, int src_cid, int dst_cid)
1678{
1679	struct vcap_admin *admin;
1680	int next_cid;
1681
1682	if (vcap_api_check(vctrl))
 
1683		return false;
1684
1685	/* The offset must be at least one lookup so round up one chain */
1686	next_cid = roundup(src_cid + 1, VCAP_CID_LOOKUP_SIZE);
1687
1688	if (dst_cid < next_cid)
1689		return false;
1690
1691	admin = vcap_find_admin(vctrl, dst_cid);
1692	if (!admin)
1693		return false;
1694
1695	return true;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1696}
1697EXPORT_SYMBOL_GPL(vcap_is_next_lookup);
1698
1699/* Check if there is room for a new rule */
1700static int vcap_rule_space(struct vcap_admin *admin, int size)
1701{
1702	if (admin->last_used_addr - size < admin->first_valid_addr) {
1703		pr_err("%s:%d: No room for rule size: %u, %u\n",
1704		       __func__, __LINE__, size, admin->first_valid_addr);
1705		return -ENOSPC;
1706	}
1707	return 0;
1708}
1709
1710/* Add the keyset typefield to the list of rule keyfields */
1711static int vcap_add_type_keyfield(struct vcap_rule *rule)
1712{
1713	struct vcap_rule_internal *ri = to_intrule(rule);
1714	enum vcap_keyfield_set keyset = rule->keyset;
1715	enum vcap_type vt = ri->admin->vtype;
1716	const struct vcap_field *fields;
1717	const struct vcap_set *kset;
1718	int ret = -EINVAL;
1719
1720	kset = vcap_keyfieldset(ri->vctrl, vt, keyset);
1721	if (!kset)
1722		return ret;
1723	if (kset->type_id == (u8)-1)  /* No type field is needed */
1724		return 0;
1725
1726	fields = vcap_keyfields(ri->vctrl, vt, keyset);
1727	if (!fields)
1728		return -EINVAL;
1729	if (fields[VCAP_KF_TYPE].width > 1) {
1730		ret = vcap_rule_add_key_u32(rule, VCAP_KF_TYPE,
1731					    kset->type_id, 0xff);
1732	} else {
1733		if (kset->type_id)
1734			ret = vcap_rule_add_key_bit(rule, VCAP_KF_TYPE,
1735						    VCAP_BIT_1);
1736		else
1737			ret = vcap_rule_add_key_bit(rule, VCAP_KF_TYPE,
1738						    VCAP_BIT_0);
1739	}
1740	return 0;
1741}
1742
1743/* Add the actionset typefield to the list of rule actionfields */
1744static int vcap_add_type_actionfield(struct vcap_rule *rule)
1745{
1746	enum vcap_actionfield_set actionset = rule->actionset;
1747	struct vcap_rule_internal *ri = to_intrule(rule);
1748	enum vcap_type vt = ri->admin->vtype;
1749	const struct vcap_field *fields;
1750	const struct vcap_set *aset;
1751	int ret = -EINVAL;
1752
1753	aset = vcap_actionfieldset(ri->vctrl, vt, actionset);
1754	if (!aset)
1755		return ret;
1756	if (aset->type_id == (u8)-1)  /* No type field is needed */
1757		return 0;
1758
1759	fields = vcap_actionfields(ri->vctrl, vt, actionset);
1760	if (!fields)
1761		return -EINVAL;
1762	if (fields[VCAP_AF_TYPE].width > 1) {
1763		ret = vcap_rule_add_action_u32(rule, VCAP_AF_TYPE,
1764					       aset->type_id);
1765	} else {
1766		if (aset->type_id)
1767			ret = vcap_rule_add_action_bit(rule, VCAP_AF_TYPE,
1768						       VCAP_BIT_1);
1769		else
1770			ret = vcap_rule_add_action_bit(rule, VCAP_AF_TYPE,
1771						       VCAP_BIT_0);
1772	}
1773	return ret;
1774}
1775
1776/* Add a keyset to a keyset list */
1777bool vcap_keyset_list_add(struct vcap_keyset_list *keysetlist,
1778			  enum vcap_keyfield_set keyset)
1779{
1780	int idx;
1781
1782	if (keysetlist->cnt < keysetlist->max) {
1783		/* Avoid duplicates */
1784		for (idx = 0; idx < keysetlist->cnt; ++idx)
1785			if (keysetlist->keysets[idx] == keyset)
1786				return keysetlist->cnt < keysetlist->max;
1787		keysetlist->keysets[keysetlist->cnt++] = keyset;
1788	}
1789	return keysetlist->cnt < keysetlist->max;
1790}
1791EXPORT_SYMBOL_GPL(vcap_keyset_list_add);
1792
1793/* Add a actionset to a actionset list */
1794static bool vcap_actionset_list_add(struct vcap_actionset_list *actionsetlist,
1795				    enum vcap_actionfield_set actionset)
1796{
1797	int idx;
1798
1799	if (actionsetlist->cnt < actionsetlist->max) {
1800		/* Avoid duplicates */
1801		for (idx = 0; idx < actionsetlist->cnt; ++idx)
1802			if (actionsetlist->actionsets[idx] == actionset)
1803				return actionsetlist->cnt < actionsetlist->max;
1804		actionsetlist->actionsets[actionsetlist->cnt++] = actionset;
1805	}
1806	return actionsetlist->cnt < actionsetlist->max;
1807}
1808
1809/* map keyset id to a string with the keyset name */
1810const char *vcap_keyset_name(struct vcap_control *vctrl,
1811			     enum vcap_keyfield_set keyset)
1812{
1813	return vctrl->stats->keyfield_set_names[keyset];
1814}
1815EXPORT_SYMBOL_GPL(vcap_keyset_name);
1816
1817/* map key field id to a string with the key name */
1818const char *vcap_keyfield_name(struct vcap_control *vctrl,
1819			       enum vcap_key_field key)
1820{
1821	return vctrl->stats->keyfield_names[key];
1822}
1823EXPORT_SYMBOL_GPL(vcap_keyfield_name);
1824
1825/* map actionset id to a string with the actionset name */
1826const char *vcap_actionset_name(struct vcap_control *vctrl,
1827				enum vcap_actionfield_set actionset)
1828{
1829	return vctrl->stats->actionfield_set_names[actionset];
1830}
1831
1832/* map action field id to a string with the action name */
1833const char *vcap_actionfield_name(struct vcap_control *vctrl,
1834				  enum vcap_action_field action)
1835{
1836	return vctrl->stats->actionfield_names[action];
1837}
1838
1839/* Return the keyfield that matches a key in a keyset */
1840static const struct vcap_field *
1841vcap_find_keyset_keyfield(struct vcap_control *vctrl,
1842			  enum vcap_type vtype,
1843			  enum vcap_keyfield_set keyset,
1844			  enum vcap_key_field key)
1845{
1846	const struct vcap_field *fields;
1847	int idx, count;
1848
1849	fields = vcap_keyfields(vctrl, vtype, keyset);
1850	if (!fields)
1851		return NULL;
1852
1853	/* Iterate the keyfields of the keyset */
1854	count = vcap_keyfield_count(vctrl, vtype, keyset);
1855	for (idx = 0; idx < count; ++idx) {
1856		if (fields[idx].width == 0)
1857			continue;
1858
1859		if (key == idx)
1860			return &fields[idx];
1861	}
1862
1863	return NULL;
1864}
1865
1866/* Match a list of keys against the keysets available in a vcap type */
1867static bool _vcap_rule_find_keysets(struct vcap_rule_internal *ri,
1868				    struct vcap_keyset_list *matches)
1869{
1870	const struct vcap_client_keyfield *ckf;
1871	int keyset, found, keycount, map_size;
1872	const struct vcap_field **map;
1873	enum vcap_type vtype;
1874
1875	vtype = ri->admin->vtype;
1876	map = ri->vctrl->vcaps[vtype].keyfield_set_map;
1877	map_size = ri->vctrl->vcaps[vtype].keyfield_set_size;
1878
1879	/* Get a count of the keyfields we want to match */
1880	keycount = 0;
1881	list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list)
1882		++keycount;
1883
1884	matches->cnt = 0;
1885	/* Iterate the keysets of the VCAP */
1886	for (keyset = 0; keyset < map_size; ++keyset) {
1887		if (!map[keyset])
1888			continue;
1889
1890		/* Iterate the keys in the rule */
1891		found = 0;
1892		list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list)
1893			if (vcap_find_keyset_keyfield(ri->vctrl, vtype,
1894						      keyset, ckf->ctrl.key))
1895				++found;
1896
1897		/* Save the keyset if all keyfields were found */
1898		if (found == keycount)
1899			if (!vcap_keyset_list_add(matches, keyset))
1900				/* bail out when the quota is filled */
1901				break;
1902	}
1903
1904	return matches->cnt > 0;
1905}
1906
1907/* Match a list of keys against the keysets available in a vcap type */
1908bool vcap_rule_find_keysets(struct vcap_rule *rule,
1909			    struct vcap_keyset_list *matches)
1910{
1911	struct vcap_rule_internal *ri = to_intrule(rule);
1912
1913	return _vcap_rule_find_keysets(ri, matches);
1914}
1915EXPORT_SYMBOL_GPL(vcap_rule_find_keysets);
1916
1917/* Return the actionfield that matches a action in a actionset */
1918static const struct vcap_field *
1919vcap_find_actionset_actionfield(struct vcap_control *vctrl,
1920				enum vcap_type vtype,
1921				enum vcap_actionfield_set actionset,
1922				enum vcap_action_field action)
1923{
1924	const struct vcap_field *fields;
1925	int idx, count;
1926
1927	fields = vcap_actionfields(vctrl, vtype, actionset);
1928	if (!fields)
1929		return NULL;
1930
1931	/* Iterate the actionfields of the actionset */
1932	count = vcap_actionfield_count(vctrl, vtype, actionset);
1933	for (idx = 0; idx < count; ++idx) {
1934		if (fields[idx].width == 0)
1935			continue;
1936
1937		if (action == idx)
1938			return &fields[idx];
1939	}
1940
1941	return NULL;
1942}
1943
1944/* Match a list of actions against the actionsets available in a vcap type */
1945static bool vcap_rule_find_actionsets(struct vcap_rule_internal *ri,
1946				      struct vcap_actionset_list *matches)
1947{
1948	int actionset, found, actioncount, map_size;
1949	const struct vcap_client_actionfield *ckf;
1950	const struct vcap_field **map;
1951	enum vcap_type vtype;
1952
1953	vtype = ri->admin->vtype;
1954	map = ri->vctrl->vcaps[vtype].actionfield_set_map;
1955	map_size = ri->vctrl->vcaps[vtype].actionfield_set_size;
1956
1957	/* Get a count of the actionfields we want to match */
1958	actioncount = 0;
1959	list_for_each_entry(ckf, &ri->data.actionfields, ctrl.list)
1960		++actioncount;
1961
1962	matches->cnt = 0;
1963	/* Iterate the actionsets of the VCAP */
1964	for (actionset = 0; actionset < map_size; ++actionset) {
1965		if (!map[actionset])
1966			continue;
1967
1968		/* Iterate the actions in the rule */
1969		found = 0;
1970		list_for_each_entry(ckf, &ri->data.actionfields, ctrl.list)
1971			if (vcap_find_actionset_actionfield(ri->vctrl, vtype,
1972							    actionset,
1973							    ckf->ctrl.action))
1974				++found;
1975
1976		/* Save the actionset if all actionfields were found */
1977		if (found == actioncount)
1978			if (!vcap_actionset_list_add(matches, actionset))
1979				/* bail out when the quota is filled */
1980				break;
1981	}
1982
1983	return matches->cnt > 0;
1984}
1985
1986/* Validate a rule with respect to available port keys */
1987int vcap_val_rule(struct vcap_rule *rule, u16 l3_proto)
1988{
1989	struct vcap_rule_internal *ri = to_intrule(rule);
1990	struct vcap_keyset_list matches = {};
1991	enum vcap_keyfield_set keysets[10];
1992	int ret;
1993
1994	ret = vcap_api_check(ri->vctrl);
1995	if (ret)
1996		return ret;
1997	if (!ri->admin) {
1998		ri->data.exterr = VCAP_ERR_NO_ADMIN;
1999		return -EINVAL;
2000	}
2001	if (!ri->ndev) {
2002		ri->data.exterr = VCAP_ERR_NO_NETDEV;
2003		return -EINVAL;
2004	}
2005
2006	matches.keysets = keysets;
2007	matches.max = ARRAY_SIZE(keysets);
2008	if (ri->data.keyset == VCAP_KFS_NO_VALUE) {
2009		/* Iterate over rule keyfields and select keysets that fits */
2010		if (!_vcap_rule_find_keysets(ri, &matches)) {
2011			ri->data.exterr = VCAP_ERR_NO_KEYSET_MATCH;
2012			return -EINVAL;
2013		}
2014	} else {
2015		/* prepare for keyset validation */
2016		keysets[0] = ri->data.keyset;
2017		matches.cnt = 1;
2018	}
2019
2020	/* Pick a keyset that is supported in the port lookups */
2021	ret = ri->vctrl->ops->validate_keyset(ri->ndev, ri->admin, rule,
2022					      &matches, l3_proto);
2023	if (ret < 0) {
2024		pr_err("%s:%d: keyset validation failed: %d\n",
2025		       __func__, __LINE__, ret);
2026		ri->data.exterr = VCAP_ERR_NO_PORT_KEYSET_MATCH;
2027		return ret;
2028	}
2029	/* use the keyset that is supported in the port lookups */
2030	ret = vcap_set_rule_set_keyset(rule, ret);
2031	if (ret < 0) {
2032		pr_err("%s:%d: keyset was not updated: %d\n",
2033		       __func__, __LINE__, ret);
2034		return ret;
2035	}
2036	if (ri->data.actionset == VCAP_AFS_NO_VALUE) {
2037		struct vcap_actionset_list matches = {};
2038		enum vcap_actionfield_set actionsets[10];
2039
2040		matches.actionsets = actionsets;
2041		matches.max = ARRAY_SIZE(actionsets);
2042
2043		/* Find an actionset that fits the rule actions */
2044		if (!vcap_rule_find_actionsets(ri, &matches)) {
2045			ri->data.exterr = VCAP_ERR_NO_ACTIONSET_MATCH;
2046			return -EINVAL;
2047		}
2048		ret = vcap_set_rule_set_actionset(rule, actionsets[0]);
2049		if (ret < 0) {
2050			pr_err("%s:%d: actionset was not updated: %d\n",
2051			       __func__, __LINE__, ret);
2052			return ret;
2053		}
2054	}
2055	vcap_add_type_keyfield(rule);
2056	vcap_add_type_actionfield(rule);
2057	/* Add default fields to this rule */
2058	ri->vctrl->ops->add_default_fields(ri->ndev, ri->admin, rule);
2059
2060	/* Rule size is the maximum of the entry and action subword count */
2061	ri->size = max(ri->keyset_sw, ri->actionset_sw);
2062
2063	/* Finally check if there is room for the rule in the VCAP */
2064	return vcap_rule_space(ri->admin, ri->size);
2065}
2066EXPORT_SYMBOL_GPL(vcap_val_rule);
2067
2068/* Entries are sorted with increasing values of sort_key.
2069 * I.e. Lowest numerical sort_key is first in list.
2070 * In order to locate largest keys first in list we negate the key size with
2071 * (max_size - size).
2072 */
2073static u32 vcap_sort_key(u32 max_size, u32 size, u8 user, u16 prio)
2074{
2075	return ((max_size - size) << 24) | (user << 16) | prio;
2076}
2077
2078/* calculate the address of the next rule after this (lower address and prio) */
2079static u32 vcap_next_rule_addr(u32 addr, struct vcap_rule_internal *ri)
2080{
2081	return ((addr - ri->size) /  ri->size) * ri->size;
2082}
2083
2084/* Assign a unique rule id and autogenerate one if id == 0 */
2085static u32 vcap_set_rule_id(struct vcap_rule_internal *ri)
2086{
2087	if (ri->data.id != 0)
2088		return ri->data.id;
2089
2090	for (u32 next_id = 1; next_id < ~0; ++next_id) {
2091		if (!vcap_rule_exists(ri->vctrl, next_id)) {
2092			ri->data.id = next_id;
2093			break;
2094		}
2095	}
2096	return ri->data.id;
2097}
2098
2099static int vcap_insert_rule(struct vcap_rule_internal *ri,
2100			    struct vcap_rule_move *move)
2101{
2102	int sw_count = ri->vctrl->vcaps[ri->admin->vtype].sw_count;
2103	struct vcap_rule_internal *duprule, *iter, *elem = NULL;
2104	struct vcap_admin *admin = ri->admin;
2105	u32 addr;
2106
2107	ri->sort_key = vcap_sort_key(sw_count, ri->size, ri->data.user,
2108				     ri->data.priority);
2109
2110	/* Insert the new rule in the list of rule based on the sort key
2111	 * If the rule needs to be  inserted between existing rules then move
2112	 * these rules to make room for the new rule and update their start
2113	 * address.
2114	 */
2115	list_for_each_entry(iter, &admin->rules, list) {
2116		if (ri->sort_key < iter->sort_key) {
2117			elem = iter;
2118			break;
2119		}
2120	}
2121
2122	if (!elem) {
2123		ri->addr = vcap_next_rule_addr(admin->last_used_addr, ri);
2124		admin->last_used_addr = ri->addr;
2125
2126		/* Add a copy of the rule to the VCAP list */
2127		duprule = vcap_dup_rule(ri, ri->state == VCAP_RS_DISABLED);
2128		if (IS_ERR(duprule))
2129			return PTR_ERR(duprule);
2130
2131		list_add_tail(&duprule->list, &admin->rules);
2132		return 0;
2133	}
2134
2135	/* Reuse the space of the current rule */
2136	addr = elem->addr + elem->size;
2137	ri->addr = vcap_next_rule_addr(addr, ri);
2138	addr = ri->addr;
2139
2140	/* Add a copy of the rule to the VCAP list */
2141	duprule = vcap_dup_rule(ri, ri->state == VCAP_RS_DISABLED);
2142	if (IS_ERR(duprule))
2143		return PTR_ERR(duprule);
2144
2145	/* Add before the current entry */
2146	list_add_tail(&duprule->list, &elem->list);
2147
2148	/* Update the current rule */
2149	elem->addr = vcap_next_rule_addr(addr, elem);
2150	addr = elem->addr;
2151
2152	/* Update the address in the remaining rules in the list */
2153	list_for_each_entry_continue(elem, &admin->rules, list) {
2154		elem->addr = vcap_next_rule_addr(addr, elem);
2155		addr = elem->addr;
2156	}
2157
2158	/* Update the move info */
2159	move->addr = admin->last_used_addr;
2160	move->count = ri->addr - addr;
2161	move->offset = admin->last_used_addr - addr;
2162	admin->last_used_addr = addr;
2163	return 0;
2164}
2165
2166static void vcap_move_rules(struct vcap_rule_internal *ri,
2167			    struct vcap_rule_move *move)
2168{
2169	ri->vctrl->ops->move(ri->ndev, ri->admin, move->addr,
2170			 move->offset, move->count);
2171}
2172
2173/* Check if the chain is already used to enable a VCAP lookup for this port */
2174static bool vcap_is_chain_used(struct vcap_control *vctrl,
2175			       struct net_device *ndev, int src_cid)
2176{
2177	struct vcap_enabled_port *eport;
2178	struct vcap_admin *admin;
2179
2180	list_for_each_entry(admin, &vctrl->list, list)
2181		list_for_each_entry(eport, &admin->enabled, list)
2182			if (eport->src_cid == src_cid && eport->ndev == ndev)
2183				return true;
2184
2185	return false;
2186}
2187
2188/* Fetch the next chain in the enabled list for the port */
2189static int vcap_get_next_chain(struct vcap_control *vctrl,
2190			       struct net_device *ndev,
2191			       int dst_cid)
2192{
2193	struct vcap_enabled_port *eport;
2194	struct vcap_admin *admin;
2195
2196	list_for_each_entry(admin, &vctrl->list, list) {
2197		list_for_each_entry(eport, &admin->enabled, list) {
2198			if (eport->ndev != ndev)
2199				continue;
2200			if (eport->src_cid == dst_cid)
2201				return eport->dst_cid;
2202		}
2203	}
2204
2205	return 0;
2206}
2207
2208static bool vcap_path_exist(struct vcap_control *vctrl, struct net_device *ndev,
2209			    int dst_cid)
2210{
2211	int cid = rounddown(dst_cid, VCAP_CID_LOOKUP_SIZE);
2212	struct vcap_enabled_port *eport = NULL;
2213	struct vcap_enabled_port *elem;
2214	struct vcap_admin *admin;
2215	int tmp;
2216
2217	if (cid == 0) /* Chain zero is always available */
2218		return true;
2219
2220	/* Find first entry that starts from chain 0*/
2221	list_for_each_entry(admin, &vctrl->list, list) {
2222		list_for_each_entry(elem, &admin->enabled, list) {
2223			if (elem->src_cid == 0 && elem->ndev == ndev) {
2224				eport = elem;
2225				break;
2226			}
2227		}
2228		if (eport)
2229			break;
2230	}
2231
2232	if (!eport)
2233		return false;
2234
2235	tmp = eport->dst_cid;
2236	while (tmp != cid && tmp != 0)
2237		tmp = vcap_get_next_chain(vctrl, ndev, tmp);
2238
2239	return !!tmp;
2240}
2241
2242/* Internal clients can always store their rules in HW
2243 * External clients can store their rules if the chain is enabled all
2244 * the way from chain 0, otherwise the rule will be cached until
2245 * the chain is enabled.
2246 */
2247static void vcap_rule_set_state(struct vcap_rule_internal *ri)
2248{
2249	if (ri->data.user <= VCAP_USER_QOS)
2250		ri->state = VCAP_RS_PERMANENT;
2251	else if (vcap_path_exist(ri->vctrl, ri->ndev, ri->data.vcap_chain_id))
2252		ri->state = VCAP_RS_ENABLED;
2253	else
2254		ri->state = VCAP_RS_DISABLED;
2255}
2256
2257/* Encode and write a validated rule to the VCAP */
2258int vcap_add_rule(struct vcap_rule *rule)
2259{
2260	struct vcap_rule_internal *ri = to_intrule(rule);
2261	struct vcap_rule_move move = {0};
2262	struct vcap_counter ctr = {0};
2263	int ret;
2264
2265	ret = vcap_api_check(ri->vctrl);
2266	if (ret)
2267		return ret;
2268	/* Insert the new rule in the list of vcap rules */
2269	mutex_lock(&ri->admin->lock);
2270
2271	vcap_rule_set_state(ri);
2272	ret = vcap_insert_rule(ri, &move);
2273	if (ret < 0) {
2274		pr_err("%s:%d: could not insert rule in vcap list: %d\n",
2275		       __func__, __LINE__, ret);
2276		goto out;
2277	}
2278	if (move.count > 0)
2279		vcap_move_rules(ri, &move);
2280
2281	/* Set the counter to zero */
2282	ret = vcap_write_counter(ri, &ctr);
2283	if (ret)
2284		goto out;
2285
2286	if (ri->state == VCAP_RS_DISABLED) {
2287		/* Erase the rule area */
2288		ri->vctrl->ops->init(ri->ndev, ri->admin, ri->addr, ri->size);
2289		goto out;
2290	}
2291
2292	vcap_erase_cache(ri);
2293	ret = vcap_encode_rule(ri);
2294	if (ret) {
2295		pr_err("%s:%d: rule encoding error: %d\n", __func__, __LINE__, ret);
2296		goto out;
2297	}
2298
2299	ret = vcap_write_rule(ri);
2300	if (ret) {
2301		pr_err("%s:%d: rule write error: %d\n", __func__, __LINE__, ret);
2302		goto out;
2303	}
2304out:
2305	mutex_unlock(&ri->admin->lock);
2306	return ret;
2307}
2308EXPORT_SYMBOL_GPL(vcap_add_rule);
2309
2310/* Allocate a new rule with the provided arguments */
2311struct vcap_rule *vcap_alloc_rule(struct vcap_control *vctrl,
2312				  struct net_device *ndev, int vcap_chain_id,
2313				  enum vcap_user user, u16 priority,
2314				  u32 id)
2315{
2316	struct vcap_rule_internal *ri;
2317	struct vcap_admin *admin;
2318	int err, maxsize;
2319
2320	err = vcap_api_check(vctrl);
2321	if (err)
2322		return ERR_PTR(err);
2323	if (!ndev)
2324		return ERR_PTR(-ENODEV);
2325	/* Get the VCAP instance */
2326	admin = vcap_find_admin(vctrl, vcap_chain_id);
2327	if (!admin)
2328		return ERR_PTR(-ENOENT);
2329	/* Sanity check that this VCAP is supported on this platform */
2330	if (vctrl->vcaps[admin->vtype].rows == 0)
2331		return ERR_PTR(-EINVAL);
2332
2333	mutex_lock(&admin->lock);
2334	/* Check if a rule with this id already exists */
2335	if (vcap_rule_exists(vctrl, id)) {
2336		err = -EINVAL;
2337		goto out_unlock;
2338	}
2339
2340	/* Check if there is room for the rule in the block(s) of the VCAP */
2341	maxsize = vctrl->vcaps[admin->vtype].sw_count; /* worst case rule size */
2342	if (vcap_rule_space(admin, maxsize)) {
2343		err = -ENOSPC;
2344		goto out_unlock;
2345	}
2346
2347	/* Create a container for the rule and return it */
2348	ri = kzalloc(sizeof(*ri), GFP_KERNEL);
2349	if (!ri) {
2350		err = -ENOMEM;
2351		goto out_unlock;
2352	}
2353
2354	ri->data.vcap_chain_id = vcap_chain_id;
2355	ri->data.user = user;
2356	ri->data.priority = priority;
2357	ri->data.id = id;
2358	ri->data.keyset = VCAP_KFS_NO_VALUE;
2359	ri->data.actionset = VCAP_AFS_NO_VALUE;
2360	INIT_LIST_HEAD(&ri->list);
2361	INIT_LIST_HEAD(&ri->data.keyfields);
2362	INIT_LIST_HEAD(&ri->data.actionfields);
2363	ri->ndev = ndev;
2364	ri->admin = admin; /* refer to the vcap instance */
2365	ri->vctrl = vctrl; /* refer to the client */
2366
2367	if (vcap_set_rule_id(ri) == 0) {
2368		err = -EINVAL;
2369		goto out_free;
2370	}
2371
2372	mutex_unlock(&admin->lock);
2373	return (struct vcap_rule *)ri;
2374
2375out_free:
2376	kfree(ri);
2377out_unlock:
2378	mutex_unlock(&admin->lock);
2379	return ERR_PTR(err);
2380
2381}
2382EXPORT_SYMBOL_GPL(vcap_alloc_rule);
2383
2384/* Free mem of a rule owned by client after the rule as been added to the VCAP */
2385void vcap_free_rule(struct vcap_rule *rule)
2386{
2387	struct vcap_rule_internal *ri = to_intrule(rule);
2388	struct vcap_client_actionfield *caf, *next_caf;
2389	struct vcap_client_keyfield *ckf, *next_ckf;
2390
2391	/* Deallocate the list of keys and actions */
2392	list_for_each_entry_safe(ckf, next_ckf, &ri->data.keyfields, ctrl.list) {
2393		list_del(&ckf->ctrl.list);
2394		kfree(ckf);
2395	}
2396	list_for_each_entry_safe(caf, next_caf, &ri->data.actionfields, ctrl.list) {
2397		list_del(&caf->ctrl.list);
2398		kfree(caf);
2399	}
2400	/* Deallocate the rule */
2401	kfree(rule);
2402}
2403EXPORT_SYMBOL_GPL(vcap_free_rule);
2404
2405/* Decode a rule from the VCAP cache and return a copy */
2406struct vcap_rule *vcap_decode_rule(struct vcap_rule_internal *elem)
2407{
2408	struct vcap_rule_internal *ri;
2409	int err;
2410
2411	ri = vcap_dup_rule(elem, elem->state == VCAP_RS_DISABLED);
2412	if (IS_ERR(ri))
2413		return ERR_CAST(ri);
2414
2415	if (ri->state == VCAP_RS_DISABLED)
2416		goto out;
2417
2418	err = vcap_read_rule(ri);
2419	if (err)
2420		return ERR_PTR(err);
2421
2422	err = vcap_decode_keyset(ri);
2423	if (err)
2424		return ERR_PTR(err);
2425
2426	err = vcap_decode_actionset(ri);
2427	if (err)
2428		return ERR_PTR(err);
2429
2430out:
2431	return &ri->data;
2432}
2433
2434struct vcap_rule *vcap_get_rule(struct vcap_control *vctrl, u32 id)
2435{
2436	struct vcap_rule_internal *elem;
2437	struct vcap_rule *rule;
2438	int err;
2439
 
 
2440	err = vcap_api_check(vctrl);
2441	if (err)
2442		return ERR_PTR(err);
2443
2444	elem = vcap_get_locked_rule(vctrl, id);
2445	if (!elem)
2446		return ERR_PTR(-ENOENT);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2447
2448	rule = vcap_decode_rule(elem);
2449	mutex_unlock(&elem->admin->lock);
2450	return rule;
2451}
2452EXPORT_SYMBOL_GPL(vcap_get_rule);
2453
2454/* Update existing rule */
2455int vcap_mod_rule(struct vcap_rule *rule)
2456{
2457	struct vcap_rule_internal *ri = to_intrule(rule);
2458	struct vcap_counter ctr;
2459	int err;
2460
2461	err = vcap_api_check(ri->vctrl);
2462	if (err)
2463		return err;
2464
2465	if (!vcap_get_locked_rule(ri->vctrl, ri->data.id))
2466		return -ENOENT;
2467
2468	vcap_rule_set_state(ri);
2469	if (ri->state == VCAP_RS_DISABLED)
2470		goto out;
2471
2472	/* Encode the bitstreams to the VCAP cache */
2473	vcap_erase_cache(ri);
2474	err = vcap_encode_rule(ri);
2475	if (err)
2476		goto out;
2477
2478	err = vcap_write_rule(ri);
2479	if (err)
2480		goto out;
2481
2482	memset(&ctr, 0, sizeof(ctr));
2483	err =  vcap_write_counter(ri, &ctr);
 
 
2484
2485out:
2486	mutex_unlock(&ri->admin->lock);
2487	return err;
2488}
2489EXPORT_SYMBOL_GPL(vcap_mod_rule);
2490
2491/* Return the alignment offset for a new rule address */
2492static int vcap_valid_rule_move(struct vcap_rule_internal *el, int offset)
2493{
2494	return (el->addr + offset) % el->size;
2495}
2496
2497/* Update the rule address with an offset */
2498static void vcap_adjust_rule_addr(struct vcap_rule_internal *el, int offset)
2499{
2500	el->addr += offset;
2501}
2502
2503/* Rules needs to be moved to fill the gap of the deleted rule */
2504static int vcap_fill_rule_gap(struct vcap_rule_internal *ri)
2505{
2506	struct vcap_admin *admin = ri->admin;
2507	struct vcap_rule_internal *elem;
2508	struct vcap_rule_move move;
2509	int gap = 0, offset = 0;
2510
2511	/* If the first rule is deleted: Move other rules to the top */
2512	if (list_is_first(&ri->list, &admin->rules))
2513		offset = admin->last_valid_addr + 1 - ri->addr - ri->size;
2514
2515	/* Locate gaps between odd size rules and adjust the move */
2516	elem = ri;
2517	list_for_each_entry_continue(elem, &admin->rules, list)
2518		gap += vcap_valid_rule_move(elem, ri->size);
2519
2520	/* Update the address in the remaining rules in the list */
2521	elem = ri;
2522	list_for_each_entry_continue(elem, &admin->rules, list)
2523		vcap_adjust_rule_addr(elem, ri->size + gap + offset);
2524
2525	/* Update the move info */
2526	move.addr = admin->last_used_addr;
2527	move.count = ri->addr - admin->last_used_addr - gap;
2528	move.offset = -(ri->size + gap + offset);
2529
2530	/* Do the actual move operation */
2531	vcap_move_rules(ri, &move);
2532
2533	return gap + offset;
2534}
2535
2536/* Delete rule in a VCAP instance */
2537int vcap_del_rule(struct vcap_control *vctrl, struct net_device *ndev, u32 id)
2538{
2539	struct vcap_rule_internal *ri, *elem;
2540	struct vcap_admin *admin;
2541	int gap = 0, err;
2542
2543	/* This will later also handle rule moving */
2544	if (!ndev)
2545		return -ENODEV;
2546	err = vcap_api_check(vctrl);
2547	if (err)
2548		return err;
2549	/* Look for the rule id in all vcaps */
2550	ri = vcap_get_locked_rule(vctrl, id);
2551	if (!ri)
2552		return -ENOENT;
2553
2554	admin = ri->admin;
2555
2556	if (ri->addr > admin->last_used_addr)
2557		gap = vcap_fill_rule_gap(ri);
2558
2559	/* Delete the rule from the list of rules and the cache */
 
2560	list_del(&ri->list);
2561	vctrl->ops->init(ndev, admin, admin->last_used_addr, ri->size + gap);
2562	vcap_free_rule(&ri->data);
 
2563
2564	/* Update the last used address, set to default when no rules */
2565	if (list_empty(&admin->rules)) {
2566		admin->last_used_addr = admin->last_valid_addr + 1;
2567	} else {
2568		elem = list_last_entry(&admin->rules, struct vcap_rule_internal,
2569				       list);
2570		admin->last_used_addr = elem->addr;
2571	}
2572
2573	mutex_unlock(&admin->lock);
2574	return err;
2575}
2576EXPORT_SYMBOL_GPL(vcap_del_rule);
2577
2578/* Delete all rules in the VCAP instance */
2579int vcap_del_rules(struct vcap_control *vctrl, struct vcap_admin *admin)
2580{
2581	struct vcap_enabled_port *eport, *next_eport;
2582	struct vcap_rule_internal *ri, *next_ri;
2583	int ret = vcap_api_check(vctrl);
2584
2585	if (ret)
2586		return ret;
2587
2588	mutex_lock(&admin->lock);
2589	list_for_each_entry_safe(ri, next_ri, &admin->rules, list) {
2590		vctrl->ops->init(ri->ndev, admin, ri->addr, ri->size);
2591		list_del(&ri->list);
2592		vcap_free_rule(&ri->data);
2593	}
2594	admin->last_used_addr = admin->last_valid_addr;
2595
2596	/* Remove list of enabled ports */
2597	list_for_each_entry_safe(eport, next_eport, &admin->enabled, list) {
2598		list_del(&eport->list);
2599		kfree(eport);
2600	}
2601	mutex_unlock(&admin->lock);
2602
2603	return 0;
2604}
2605EXPORT_SYMBOL_GPL(vcap_del_rules);
2606
2607/* Find a client key field in a rule */
2608static struct vcap_client_keyfield *
2609vcap_find_keyfield(struct vcap_rule *rule, enum vcap_key_field key)
2610{
2611	struct vcap_rule_internal *ri = to_intrule(rule);
2612	struct vcap_client_keyfield *ckf;
2613
2614	list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list)
2615		if (ckf->ctrl.key == key)
2616			return ckf;
2617	return NULL;
2618}
2619
2620/* Find information on a key field in a rule */
2621const struct vcap_field *vcap_lookup_keyfield(struct vcap_rule *rule,
2622					      enum vcap_key_field key)
2623{
2624	struct vcap_rule_internal *ri = to_intrule(rule);
2625	enum vcap_keyfield_set keyset = rule->keyset;
2626	enum vcap_type vt = ri->admin->vtype;
2627	const struct vcap_field *fields;
2628
2629	if (keyset == VCAP_KFS_NO_VALUE)
2630		return NULL;
2631	fields = vcap_keyfields(ri->vctrl, vt, keyset);
2632	if (!fields)
2633		return NULL;
2634	return &fields[key];
2635}
2636EXPORT_SYMBOL_GPL(vcap_lookup_keyfield);
2637
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2638/* Check if the keyfield is already in the rule */
2639static bool vcap_keyfield_unique(struct vcap_rule *rule,
2640				 enum vcap_key_field key)
2641{
2642	struct vcap_rule_internal *ri = to_intrule(rule);
2643	const struct vcap_client_keyfield *ckf;
2644
2645	list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list)
2646		if (ckf->ctrl.key == key)
2647			return false;
2648	return true;
2649}
2650
2651/* Check if the keyfield is in the keyset */
2652static bool vcap_keyfield_match_keyset(struct vcap_rule *rule,
2653				       enum vcap_key_field key)
2654{
2655	struct vcap_rule_internal *ri = to_intrule(rule);
2656	enum vcap_keyfield_set keyset = rule->keyset;
2657	enum vcap_type vt = ri->admin->vtype;
2658	const struct vcap_field *fields;
2659
2660	/* the field is accepted if the rule has no keyset yet */
2661	if (keyset == VCAP_KFS_NO_VALUE)
2662		return true;
2663	fields = vcap_keyfields(ri->vctrl, vt, keyset);
2664	if (!fields)
2665		return false;
2666	/* if there is a width there is a way */
2667	return fields[key].width > 0;
2668}
2669
2670static int vcap_rule_add_key(struct vcap_rule *rule,
2671			     enum vcap_key_field key,
2672			     enum vcap_field_type ftype,
2673			     struct vcap_client_keyfield_data *data)
2674{
2675	struct vcap_rule_internal *ri = to_intrule(rule);
2676	struct vcap_client_keyfield *field;
2677
2678	if (!vcap_keyfield_unique(rule, key)) {
2679		pr_warn("%s:%d: keyfield %s is already in the rule\n",
2680			__func__, __LINE__,
2681			vcap_keyfield_name(ri->vctrl, key));
2682		return -EINVAL;
2683	}
2684
2685	if (!vcap_keyfield_match_keyset(rule, key)) {
2686		pr_err("%s:%d: keyfield %s does not belong in the rule keyset\n",
2687		       __func__, __LINE__,
2688		       vcap_keyfield_name(ri->vctrl, key));
2689		return -EINVAL;
2690	}
2691
2692	field = kzalloc(sizeof(*field), GFP_KERNEL);
2693	if (!field)
2694		return -ENOMEM;
2695	memcpy(&field->data, data, sizeof(field->data));
2696	field->ctrl.key = key;
2697	field->ctrl.type = ftype;
 
2698	list_add_tail(&field->ctrl.list, &rule->keyfields);
2699	return 0;
2700}
2701
2702static void vcap_rule_set_key_bitsize(struct vcap_u1_key *u1, enum vcap_bit val)
2703{
2704	switch (val) {
2705	case VCAP_BIT_0:
2706		u1->value = 0;
2707		u1->mask = 1;
2708		break;
2709	case VCAP_BIT_1:
2710		u1->value = 1;
2711		u1->mask = 1;
2712		break;
2713	case VCAP_BIT_ANY:
2714		u1->value = 0;
2715		u1->mask = 0;
2716		break;
2717	}
2718}
2719
2720/* Add a bit key with value and mask to the rule */
2721int vcap_rule_add_key_bit(struct vcap_rule *rule, enum vcap_key_field key,
2722			  enum vcap_bit val)
2723{
2724	struct vcap_client_keyfield_data data;
2725
2726	vcap_rule_set_key_bitsize(&data.u1, val);
2727	return vcap_rule_add_key(rule, key, VCAP_FIELD_BIT, &data);
2728}
2729EXPORT_SYMBOL_GPL(vcap_rule_add_key_bit);
2730
2731/* Add a 32 bit key field with value and mask to the rule */
2732int vcap_rule_add_key_u32(struct vcap_rule *rule, enum vcap_key_field key,
2733			  u32 value, u32 mask)
2734{
2735	struct vcap_client_keyfield_data data;
2736
2737	data.u32.value = value;
2738	data.u32.mask = mask;
2739	return vcap_rule_add_key(rule, key, VCAP_FIELD_U32, &data);
2740}
2741EXPORT_SYMBOL_GPL(vcap_rule_add_key_u32);
2742
2743/* Add a 48 bit key with value and mask to the rule */
2744int vcap_rule_add_key_u48(struct vcap_rule *rule, enum vcap_key_field key,
2745			  struct vcap_u48_key *fieldval)
2746{
2747	struct vcap_client_keyfield_data data;
2748
2749	memcpy(&data.u48, fieldval, sizeof(data.u48));
2750	return vcap_rule_add_key(rule, key, VCAP_FIELD_U48, &data);
2751}
2752EXPORT_SYMBOL_GPL(vcap_rule_add_key_u48);
2753
2754/* Add a 72 bit key with value and mask to the rule */
2755int vcap_rule_add_key_u72(struct vcap_rule *rule, enum vcap_key_field key,
2756			  struct vcap_u72_key *fieldval)
2757{
2758	struct vcap_client_keyfield_data data;
2759
2760	memcpy(&data.u72, fieldval, sizeof(data.u72));
2761	return vcap_rule_add_key(rule, key, VCAP_FIELD_U72, &data);
2762}
2763EXPORT_SYMBOL_GPL(vcap_rule_add_key_u72);
2764
2765/* Add a 128 bit key with value and mask to the rule */
2766int vcap_rule_add_key_u128(struct vcap_rule *rule, enum vcap_key_field key,
2767			   struct vcap_u128_key *fieldval)
2768{
2769	struct vcap_client_keyfield_data data;
2770
2771	memcpy(&data.u128, fieldval, sizeof(data.u128));
2772	return vcap_rule_add_key(rule, key, VCAP_FIELD_U128, &data);
2773}
2774EXPORT_SYMBOL_GPL(vcap_rule_add_key_u128);
2775
2776int vcap_rule_get_key_u32(struct vcap_rule *rule, enum vcap_key_field key,
2777			  u32 *value, u32 *mask)
2778{
2779	struct vcap_client_keyfield *ckf;
2780
2781	ckf = vcap_find_keyfield(rule, key);
2782	if (!ckf)
2783		return -ENOENT;
2784
2785	*value = ckf->data.u32.value;
2786	*mask = ckf->data.u32.mask;
2787
2788	return 0;
2789}
2790EXPORT_SYMBOL_GPL(vcap_rule_get_key_u32);
2791
2792/* Find a client action field in a rule */
2793struct vcap_client_actionfield *
2794vcap_find_actionfield(struct vcap_rule *rule, enum vcap_action_field act)
2795{
2796	struct vcap_rule_internal *ri = (struct vcap_rule_internal *)rule;
2797	struct vcap_client_actionfield *caf;
2798
2799	list_for_each_entry(caf, &ri->data.actionfields, ctrl.list)
2800		if (caf->ctrl.action == act)
2801			return caf;
2802	return NULL;
2803}
2804EXPORT_SYMBOL_GPL(vcap_find_actionfield);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2805
2806/* Check if the actionfield is already in the rule */
2807static bool vcap_actionfield_unique(struct vcap_rule *rule,
2808				    enum vcap_action_field act)
2809{
2810	struct vcap_rule_internal *ri = to_intrule(rule);
2811	const struct vcap_client_actionfield *caf;
2812
2813	list_for_each_entry(caf, &ri->data.actionfields, ctrl.list)
2814		if (caf->ctrl.action == act)
2815			return false;
2816	return true;
2817}
2818
2819/* Check if the actionfield is in the actionset */
2820static bool vcap_actionfield_match_actionset(struct vcap_rule *rule,
2821					     enum vcap_action_field action)
2822{
2823	enum vcap_actionfield_set actionset = rule->actionset;
2824	struct vcap_rule_internal *ri = to_intrule(rule);
2825	enum vcap_type vt = ri->admin->vtype;
2826	const struct vcap_field *fields;
2827
2828	/* the field is accepted if the rule has no actionset yet */
2829	if (actionset == VCAP_AFS_NO_VALUE)
2830		return true;
2831	fields = vcap_actionfields(ri->vctrl, vt, actionset);
2832	if (!fields)
2833		return false;
2834	/* if there is a width there is a way */
2835	return fields[action].width > 0;
2836}
2837
2838static int vcap_rule_add_action(struct vcap_rule *rule,
2839				enum vcap_action_field action,
2840				enum vcap_field_type ftype,
2841				struct vcap_client_actionfield_data *data)
2842{
2843	struct vcap_rule_internal *ri = to_intrule(rule);
2844	struct vcap_client_actionfield *field;
2845
2846	if (!vcap_actionfield_unique(rule, action)) {
2847		pr_warn("%s:%d: actionfield %s is already in the rule\n",
2848			__func__, __LINE__,
2849			vcap_actionfield_name(ri->vctrl, action));
2850		return -EINVAL;
2851	}
2852
2853	if (!vcap_actionfield_match_actionset(rule, action)) {
2854		pr_err("%s:%d: actionfield %s does not belong in the rule actionset\n",
2855		       __func__, __LINE__,
2856		       vcap_actionfield_name(ri->vctrl, action));
2857		return -EINVAL;
2858	}
2859
2860	field = kzalloc(sizeof(*field), GFP_KERNEL);
2861	if (!field)
2862		return -ENOMEM;
2863	memcpy(&field->data, data, sizeof(field->data));
2864	field->ctrl.action = action;
2865	field->ctrl.type = ftype;
 
2866	list_add_tail(&field->ctrl.list, &rule->actionfields);
2867	return 0;
2868}
2869
2870static void vcap_rule_set_action_bitsize(struct vcap_u1_action *u1,
2871					 enum vcap_bit val)
2872{
2873	switch (val) {
2874	case VCAP_BIT_0:
2875		u1->value = 0;
2876		break;
2877	case VCAP_BIT_1:
2878		u1->value = 1;
2879		break;
2880	case VCAP_BIT_ANY:
2881		u1->value = 0;
2882		break;
2883	}
2884}
2885
2886/* Add a bit action with value to the rule */
2887int vcap_rule_add_action_bit(struct vcap_rule *rule,
2888			     enum vcap_action_field action,
2889			     enum vcap_bit val)
2890{
2891	struct vcap_client_actionfield_data data;
2892
2893	vcap_rule_set_action_bitsize(&data.u1, val);
2894	return vcap_rule_add_action(rule, action, VCAP_FIELD_BIT, &data);
2895}
2896EXPORT_SYMBOL_GPL(vcap_rule_add_action_bit);
2897
2898/* Add a 32 bit action field with value to the rule */
2899int vcap_rule_add_action_u32(struct vcap_rule *rule,
2900			     enum vcap_action_field action,
2901			     u32 value)
2902{
2903	struct vcap_client_actionfield_data data;
2904
2905	data.u32.value = value;
2906	return vcap_rule_add_action(rule, action, VCAP_FIELD_U32, &data);
2907}
2908EXPORT_SYMBOL_GPL(vcap_rule_add_action_u32);
2909
2910static int vcap_read_counter(struct vcap_rule_internal *ri,
2911			     struct vcap_counter *ctr)
2912{
2913	struct vcap_admin *admin = ri->admin;
2914
2915	ri->vctrl->ops->update(ri->ndev, admin, VCAP_CMD_READ, VCAP_SEL_COUNTER,
2916			       ri->addr);
2917	ri->vctrl->ops->cache_read(ri->ndev, admin, VCAP_SEL_COUNTER,
2918				   ri->counter_id, 0);
2919	ctr->value = admin->cache.counter;
2920	ctr->sticky = admin->cache.sticky;
2921	return 0;
2922}
2923
2924/* Copy to host byte order */
2925void vcap_netbytes_copy(u8 *dst, u8 *src, int count)
2926{
2927	int idx;
2928
2929	for (idx = 0; idx < count; ++idx, ++dst)
2930		*dst = src[count - idx - 1];
2931}
2932EXPORT_SYMBOL_GPL(vcap_netbytes_copy);
2933
2934/* Convert validation error code into tc extact error message */
2935void vcap_set_tc_exterr(struct flow_cls_offload *fco, struct vcap_rule *vrule)
2936{
2937	switch (vrule->exterr) {
2938	case VCAP_ERR_NONE:
2939		break;
2940	case VCAP_ERR_NO_ADMIN:
2941		NL_SET_ERR_MSG_MOD(fco->common.extack,
2942				   "Missing VCAP instance");
2943		break;
2944	case VCAP_ERR_NO_NETDEV:
2945		NL_SET_ERR_MSG_MOD(fco->common.extack,
2946				   "Missing network interface");
2947		break;
2948	case VCAP_ERR_NO_KEYSET_MATCH:
2949		NL_SET_ERR_MSG_MOD(fco->common.extack,
2950				   "No keyset matched the filter keys");
2951		break;
2952	case VCAP_ERR_NO_ACTIONSET_MATCH:
2953		NL_SET_ERR_MSG_MOD(fco->common.extack,
2954				   "No actionset matched the filter actions");
2955		break;
2956	case VCAP_ERR_NO_PORT_KEYSET_MATCH:
2957		NL_SET_ERR_MSG_MOD(fco->common.extack,
2958				   "No port keyset matched the filter keys");
2959		break;
2960	}
2961}
2962EXPORT_SYMBOL_GPL(vcap_set_tc_exterr);
2963
2964/* Write a rule to VCAP HW to enable it */
2965static int vcap_enable_rule(struct vcap_rule_internal *ri)
2966{
2967	struct vcap_client_actionfield *af, *naf;
2968	struct vcap_client_keyfield *kf, *nkf;
2969	int err;
2970
2971	vcap_erase_cache(ri);
2972	err = vcap_encode_rule(ri);
2973	if (err)
2974		goto out;
2975	err = vcap_write_rule(ri);
2976	if (err)
2977		goto out;
2978
2979	/* Deallocate the list of keys and actions */
2980	list_for_each_entry_safe(kf, nkf, &ri->data.keyfields, ctrl.list) {
2981		list_del(&kf->ctrl.list);
2982		kfree(kf);
2983	}
2984	list_for_each_entry_safe(af, naf, &ri->data.actionfields, ctrl.list) {
2985		list_del(&af->ctrl.list);
2986		kfree(af);
2987	}
2988	ri->state = VCAP_RS_ENABLED;
2989out:
2990	return err;
2991}
2992
2993/* Enable all disabled rules for a specific chain/port in the VCAP HW */
2994static int vcap_enable_rules(struct vcap_control *vctrl,
2995			     struct net_device *ndev, int chain)
2996{
2997	int next_chain = chain + VCAP_CID_LOOKUP_SIZE;
2998	struct vcap_rule_internal *ri;
2999	struct vcap_admin *admin;
3000	int err = 0;
3001
3002	list_for_each_entry(admin, &vctrl->list, list) {
3003		if (!(chain >= admin->first_cid && chain <= admin->last_cid))
3004			continue;
3005
3006		/* Found the admin, now find the offloadable rules */
3007		mutex_lock(&admin->lock);
3008		list_for_each_entry(ri, &admin->rules, list) {
3009			/* Is the rule in the lookup defined by the chain */
3010			if (!(ri->data.vcap_chain_id >= chain &&
3011			      ri->data.vcap_chain_id < next_chain)) {
3012				continue;
3013			}
3014
3015			if (ri->ndev != ndev)
3016				continue;
3017
3018			if (ri->state != VCAP_RS_DISABLED)
3019				continue;
3020
3021			err = vcap_enable_rule(ri);
3022			if (err)
3023				break;
3024		}
3025		mutex_unlock(&admin->lock);
3026		if (err)
3027			break;
3028	}
3029	return err;
3030}
3031
3032/* Read and erase a rule from VCAP HW to disable it */
3033static int vcap_disable_rule(struct vcap_rule_internal *ri)
3034{
3035	int err;
3036
3037	err = vcap_read_rule(ri);
3038	if (err)
3039		return err;
3040	err = vcap_decode_keyset(ri);
3041	if (err)
3042		return err;
3043	err = vcap_decode_actionset(ri);
3044	if (err)
3045		return err;
3046
3047	ri->state = VCAP_RS_DISABLED;
3048	ri->vctrl->ops->init(ri->ndev, ri->admin, ri->addr, ri->size);
3049	return 0;
3050}
3051
3052/* Disable all enabled rules for a specific chain/port in the VCAP HW */
3053static int vcap_disable_rules(struct vcap_control *vctrl,
3054			      struct net_device *ndev, int chain)
3055{
3056	struct vcap_rule_internal *ri;
3057	struct vcap_admin *admin;
3058	int err = 0;
3059
3060	list_for_each_entry(admin, &vctrl->list, list) {
3061		if (!(chain >= admin->first_cid && chain <= admin->last_cid))
3062			continue;
3063
3064		/* Found the admin, now find the rules on the chain */
3065		mutex_lock(&admin->lock);
3066		list_for_each_entry(ri, &admin->rules, list) {
3067			if (ri->data.vcap_chain_id != chain)
3068				continue;
3069
3070			if (ri->ndev != ndev)
3071				continue;
3072
3073			if (ri->state != VCAP_RS_ENABLED)
3074				continue;
3075
3076			err = vcap_disable_rule(ri);
3077			if (err)
3078				break;
3079		}
3080		mutex_unlock(&admin->lock);
3081		if (err)
3082			break;
3083	}
3084	return err;
3085}
3086
3087/* Check if this port is already enabled for this VCAP instance */
3088static bool vcap_is_enabled(struct vcap_control *vctrl, struct net_device *ndev,
3089			    int dst_cid)
3090{
3091	struct vcap_enabled_port *eport;
3092	struct vcap_admin *admin;
3093
3094	list_for_each_entry(admin, &vctrl->list, list)
3095		list_for_each_entry(eport, &admin->enabled, list)
3096			if (eport->dst_cid == dst_cid && eport->ndev == ndev)
3097				return true;
3098
3099	return false;
3100}
3101
3102/* Enable this port and chain id in a VCAP instance */
3103static int vcap_enable(struct vcap_control *vctrl, struct net_device *ndev,
3104		       unsigned long cookie, int src_cid, int dst_cid)
3105{
3106	struct vcap_enabled_port *eport;
3107	struct vcap_admin *admin;
3108
3109	if (src_cid >= dst_cid)
3110		return -EFAULT;
3111
3112	admin = vcap_find_admin(vctrl, dst_cid);
3113	if (!admin)
3114		return -ENOENT;
3115
3116	eport = kzalloc(sizeof(*eport), GFP_KERNEL);
3117	if (!eport)
3118		return -ENOMEM;
3119
3120	eport->ndev = ndev;
3121	eport->cookie = cookie;
3122	eport->src_cid = src_cid;
3123	eport->dst_cid = dst_cid;
3124	mutex_lock(&admin->lock);
3125	list_add_tail(&eport->list, &admin->enabled);
3126	mutex_unlock(&admin->lock);
3127
3128	if (vcap_path_exist(vctrl, ndev, src_cid)) {
3129		/* Enable chained lookups */
3130		while (dst_cid) {
3131			admin = vcap_find_admin(vctrl, dst_cid);
3132			if (!admin)
3133				return -ENOENT;
3134
3135			vcap_enable_rules(vctrl, ndev, dst_cid);
3136			dst_cid = vcap_get_next_chain(vctrl, ndev, dst_cid);
3137		}
3138	}
3139	return 0;
3140}
3141
3142/* Disable this port and chain id for a VCAP instance */
3143static int vcap_disable(struct vcap_control *vctrl, struct net_device *ndev,
3144			unsigned long cookie)
3145{
3146	struct vcap_enabled_port *elem, *eport = NULL;
3147	struct vcap_admin *found = NULL, *admin;
3148	int dst_cid;
3149
3150	list_for_each_entry(admin, &vctrl->list, list) {
3151		list_for_each_entry(elem, &admin->enabled, list) {
3152			if (elem->cookie == cookie && elem->ndev == ndev) {
3153				eport = elem;
3154				found = admin;
3155				break;
3156			}
3157		}
3158		if (eport)
3159			break;
3160	}
3161
3162	if (!eport)
3163		return -ENOENT;
3164
3165	/* Disable chained lookups */
3166	dst_cid = eport->dst_cid;
3167	while (dst_cid) {
3168		admin = vcap_find_admin(vctrl, dst_cid);
3169		if (!admin)
3170			return -ENOENT;
3171
3172		vcap_disable_rules(vctrl, ndev, dst_cid);
3173		dst_cid = vcap_get_next_chain(vctrl, ndev, dst_cid);
3174	}
 
3175
3176	mutex_lock(&found->lock);
3177	list_del(&eport->list);
3178	mutex_unlock(&found->lock);
3179	kfree(eport);
3180	return 0;
3181}
3182
3183/* Enable/Disable the VCAP instance lookups */
3184int vcap_enable_lookups(struct vcap_control *vctrl, struct net_device *ndev,
3185			int src_cid, int dst_cid, unsigned long cookie,
3186			bool enable)
3187{
 
3188	int err;
3189
3190	err = vcap_api_check(vctrl);
3191	if (err)
3192		return err;
3193
3194	if (!ndev)
3195		return -ENODEV;
3196
3197	/* Source and destination must be the first chain in a lookup */
3198	if (src_cid % VCAP_CID_LOOKUP_SIZE)
3199		return -EFAULT;
3200	if (dst_cid % VCAP_CID_LOOKUP_SIZE)
 
 
 
 
 
3201		return -EFAULT;
3202
3203	if (enable) {
3204		if (vcap_is_enabled(vctrl, ndev, dst_cid))
 
 
 
 
3205			return -EADDRINUSE;
3206		if (vcap_is_chain_used(vctrl, ndev, src_cid))
3207			return -EADDRNOTAVAIL;
3208		err = vcap_enable(vctrl, ndev, cookie, src_cid, dst_cid);
3209	} else {
3210		err = vcap_disable(vctrl, ndev, cookie);
 
3211	}
 
3212
3213	return err;
3214}
3215EXPORT_SYMBOL_GPL(vcap_enable_lookups);
3216
3217/* Is this chain id the last lookup of all VCAPs */
3218bool vcap_is_last_chain(struct vcap_control *vctrl, int cid, bool ingress)
3219{
3220	struct vcap_admin *admin;
3221	int lookup;
3222
3223	if (vcap_api_check(vctrl))
3224		return false;
3225
3226	admin = vcap_find_admin(vctrl, cid);
3227	if (!admin)
3228		return false;
3229
3230	if (!vcap_admin_is_last(vctrl, admin, ingress))
3231		return false;
3232
3233	/* This must be the last lookup in this VCAP type */
3234	lookup = vcap_chain_id_to_lookup(admin, cid);
3235	return lookup == admin->lookups - 1;
3236}
3237EXPORT_SYMBOL_GPL(vcap_is_last_chain);
3238
3239/* Set a rule counter id (for certain vcaps only) */
3240void vcap_rule_set_counter_id(struct vcap_rule *rule, u32 counter_id)
3241{
3242	struct vcap_rule_internal *ri = to_intrule(rule);
3243
3244	ri->counter_id = counter_id;
3245}
3246EXPORT_SYMBOL_GPL(vcap_rule_set_counter_id);
3247
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3248int vcap_rule_set_counter(struct vcap_rule *rule, struct vcap_counter *ctr)
3249{
3250	struct vcap_rule_internal *ri = to_intrule(rule);
3251	int err;
3252
3253	err = vcap_api_check(ri->vctrl);
3254	if (err)
3255		return err;
3256	if (!ctr) {
3257		pr_err("%s:%d: counter is missing\n", __func__, __LINE__);
3258		return -EINVAL;
3259	}
3260
3261	mutex_lock(&ri->admin->lock);
3262	err = vcap_write_counter(ri, ctr);
3263	mutex_unlock(&ri->admin->lock);
3264
3265	return err;
3266}
3267EXPORT_SYMBOL_GPL(vcap_rule_set_counter);
3268
3269int vcap_rule_get_counter(struct vcap_rule *rule, struct vcap_counter *ctr)
3270{
3271	struct vcap_rule_internal *ri = to_intrule(rule);
3272	int err;
3273
3274	err = vcap_api_check(ri->vctrl);
3275	if (err)
3276		return err;
3277	if (!ctr) {
3278		pr_err("%s:%d: counter is missing\n", __func__, __LINE__);
3279		return -EINVAL;
3280	}
3281
3282	mutex_lock(&ri->admin->lock);
3283	err = vcap_read_counter(ri, ctr);
3284	mutex_unlock(&ri->admin->lock);
3285
3286	return err;
3287}
3288EXPORT_SYMBOL_GPL(vcap_rule_get_counter);
3289
3290/* Get a copy of a client key field */
3291static int vcap_rule_get_key(struct vcap_rule *rule,
3292			     enum vcap_key_field key,
3293			     struct vcap_client_keyfield *ckf)
3294{
3295	struct vcap_client_keyfield *field;
3296
3297	field = vcap_find_keyfield(rule, key);
3298	if (!field)
3299		return -EINVAL;
3300	memcpy(ckf, field, sizeof(*ckf));
3301	INIT_LIST_HEAD(&ckf->ctrl.list);
3302	return 0;
3303}
3304
3305/* Find a keyset having the same size as the provided rule, where the keyset
3306 * does not have a type id.
3307 */
3308static int vcap_rule_get_untyped_keyset(struct vcap_rule_internal *ri,
3309					struct vcap_keyset_list *matches)
3310{
3311	struct vcap_control *vctrl = ri->vctrl;
3312	enum vcap_type vt = ri->admin->vtype;
3313	const struct vcap_set *keyfield_set;
3314	int idx;
3315
3316	keyfield_set = vctrl->vcaps[vt].keyfield_set;
3317	for (idx = 0; idx < vctrl->vcaps[vt].keyfield_set_size; ++idx) {
3318		if (keyfield_set[idx].sw_per_item == ri->keyset_sw &&
3319		    keyfield_set[idx].type_id == (u8)-1) {
3320			vcap_keyset_list_add(matches, idx);
3321			return 0;
3322		}
3323	}
3324	return -EINVAL;
3325}
3326
3327/* Get the keysets that matches the rule key type/mask */
3328int vcap_rule_get_keysets(struct vcap_rule_internal *ri,
3329			  struct vcap_keyset_list *matches)
3330{
3331	struct vcap_control *vctrl = ri->vctrl;
3332	enum vcap_type vt = ri->admin->vtype;
3333	const struct vcap_set *keyfield_set;
3334	struct vcap_client_keyfield kf = {};
3335	u32 value, mask;
3336	int err, idx;
3337
3338	err = vcap_rule_get_key(&ri->data, VCAP_KF_TYPE, &kf);
3339	if (err)
3340		return vcap_rule_get_untyped_keyset(ri, matches);
3341
3342	if (kf.ctrl.type == VCAP_FIELD_BIT) {
3343		value = kf.data.u1.value;
3344		mask = kf.data.u1.mask;
3345	} else if (kf.ctrl.type == VCAP_FIELD_U32) {
3346		value = kf.data.u32.value;
3347		mask = kf.data.u32.mask;
3348	} else {
3349		return -EINVAL;
3350	}
3351
3352	keyfield_set = vctrl->vcaps[vt].keyfield_set;
3353	for (idx = 0; idx < vctrl->vcaps[vt].keyfield_set_size; ++idx) {
3354		if (keyfield_set[idx].sw_per_item != ri->keyset_sw)
3355			continue;
3356
3357		if (keyfield_set[idx].type_id == (u8)-1) {
3358			vcap_keyset_list_add(matches, idx);
3359			continue;
3360		}
3361
3362		if ((keyfield_set[idx].type_id & mask) == value)
3363			vcap_keyset_list_add(matches, idx);
3364	}
3365	if (matches->cnt > 0)
3366		return 0;
3367
3368	return -EINVAL;
3369}
3370
3371/* Collect packet counts from all rules with the same cookie */
3372int vcap_get_rule_count_by_cookie(struct vcap_control *vctrl,
3373				  struct vcap_counter *ctr, u64 cookie)
3374{
3375	struct vcap_rule_internal *ri;
3376	struct vcap_counter temp = {};
3377	struct vcap_admin *admin;
3378	int err;
3379
3380	err = vcap_api_check(vctrl);
3381	if (err)
3382		return err;
3383
3384	/* Iterate all rules in each VCAP instance */
3385	list_for_each_entry(admin, &vctrl->list, list) {
3386		mutex_lock(&admin->lock);
3387		list_for_each_entry(ri, &admin->rules, list) {
3388			if (ri->data.cookie != cookie)
3389				continue;
3390
3391			err = vcap_read_counter(ri, &temp);
3392			if (err)
3393				goto unlock;
3394			ctr->value += temp.value;
3395
3396			/* Reset the rule counter */
3397			temp.value = 0;
3398			temp.sticky = 0;
3399			err = vcap_write_counter(ri, &temp);
3400			if (err)
3401				goto unlock;
3402		}
3403		mutex_unlock(&admin->lock);
3404	}
3405	return err;
3406
3407unlock:
3408	mutex_unlock(&admin->lock);
3409	return err;
3410}
3411EXPORT_SYMBOL_GPL(vcap_get_rule_count_by_cookie);
3412
3413static int vcap_rule_mod_key(struct vcap_rule *rule,
3414			     enum vcap_key_field key,
3415			     enum vcap_field_type ftype,
3416			     struct vcap_client_keyfield_data *data)
3417{
3418	struct vcap_client_keyfield *field;
3419
3420	field = vcap_find_keyfield(rule, key);
3421	if (!field)
3422		return vcap_rule_add_key(rule, key, ftype, data);
3423	memcpy(&field->data, data, sizeof(field->data));
3424	return 0;
3425}
3426
3427/* Modify a 32 bit key field with value and mask in the rule */
3428int vcap_rule_mod_key_u32(struct vcap_rule *rule, enum vcap_key_field key,
3429			  u32 value, u32 mask)
3430{
3431	struct vcap_client_keyfield_data data;
3432
3433	data.u32.value = value;
3434	data.u32.mask = mask;
3435	return vcap_rule_mod_key(rule, key, VCAP_FIELD_U32, &data);
3436}
3437EXPORT_SYMBOL_GPL(vcap_rule_mod_key_u32);
3438
3439/* Remove a key field with value and mask in the rule */
3440int vcap_rule_rem_key(struct vcap_rule *rule, enum vcap_key_field key)
3441{
3442	struct vcap_rule_internal *ri = to_intrule(rule);
3443	struct vcap_client_keyfield *field;
3444
3445	field = vcap_find_keyfield(rule, key);
3446	if (!field) {
3447		pr_err("%s:%d: key %s is not in the rule\n",
3448		       __func__, __LINE__, vcap_keyfield_name(ri->vctrl, key));
3449		return -EINVAL;
3450	}
3451	/* Deallocate the key field */
3452	list_del(&field->ctrl.list);
3453	kfree(field);
3454	return 0;
3455}
3456EXPORT_SYMBOL_GPL(vcap_rule_rem_key);
3457
3458static int vcap_rule_mod_action(struct vcap_rule *rule,
3459				enum vcap_action_field action,
3460				enum vcap_field_type ftype,
3461				struct vcap_client_actionfield_data *data)
3462{
3463	struct vcap_client_actionfield *field;
3464
3465	field = vcap_find_actionfield(rule, action);
3466	if (!field)
3467		return vcap_rule_add_action(rule, action, ftype, data);
3468	memcpy(&field->data, data, sizeof(field->data));
3469	return 0;
3470}
3471
3472/* Modify a 32 bit action field with value in the rule */
3473int vcap_rule_mod_action_u32(struct vcap_rule *rule,
3474			     enum vcap_action_field action,
3475			     u32 value)
3476{
3477	struct vcap_client_actionfield_data data;
3478
3479	data.u32.value = value;
3480	return vcap_rule_mod_action(rule, action, VCAP_FIELD_U32, &data);
3481}
3482EXPORT_SYMBOL_GPL(vcap_rule_mod_action_u32);
3483
3484/* Drop keys in a keylist and any keys that are not supported by the keyset */
3485int vcap_filter_rule_keys(struct vcap_rule *rule,
3486			  enum vcap_key_field keylist[], int length,
3487			  bool drop_unsupported)
3488{
3489	struct vcap_rule_internal *ri = to_intrule(rule);
3490	struct vcap_client_keyfield *ckf, *next_ckf;
3491	const struct vcap_field *fields;
3492	enum vcap_key_field key;
3493	int err = 0;
3494	int idx;
3495
3496	if (length > 0) {
3497		err = -EEXIST;
3498		list_for_each_entry_safe(ckf, next_ckf,
3499					 &ri->data.keyfields, ctrl.list) {
3500			key = ckf->ctrl.key;
3501			for (idx = 0; idx < length; ++idx)
3502				if (key == keylist[idx]) {
3503					list_del(&ckf->ctrl.list);
3504					kfree(ckf);
3505					idx++;
3506					err = 0;
3507				}
3508		}
3509	}
3510	if (drop_unsupported) {
3511		err = -EEXIST;
3512		fields = vcap_keyfields(ri->vctrl, ri->admin->vtype,
3513					rule->keyset);
3514		if (!fields)
3515			return err;
3516		list_for_each_entry_safe(ckf, next_ckf,
3517					 &ri->data.keyfields, ctrl.list) {
3518			key = ckf->ctrl.key;
3519			if (fields[key].width == 0) {
3520				list_del(&ckf->ctrl.list);
3521				kfree(ckf);
3522				err = 0;
3523			}
3524		}
3525	}
3526	return err;
3527}
3528EXPORT_SYMBOL_GPL(vcap_filter_rule_keys);
3529
3530/* Select the keyset from the list that results in the smallest rule size */
3531enum vcap_keyfield_set
3532vcap_select_min_rule_keyset(struct vcap_control *vctrl,
3533			    enum vcap_type vtype,
3534			    struct vcap_keyset_list *kslist)
3535{
3536	enum vcap_keyfield_set ret = VCAP_KFS_NO_VALUE;
3537	const struct vcap_set *kset;
3538	int max = 100, idx;
3539
3540	for (idx = 0; idx < kslist->cnt; ++idx) {
3541		kset = vcap_keyfieldset(vctrl, vtype, kslist->keysets[idx]);
3542		if (!kset)
3543			continue;
3544		if (kset->sw_per_item >= max)
3545			continue;
3546		max = kset->sw_per_item;
3547		ret = kslist->keysets[idx];
3548	}
3549	return ret;
3550}
3551EXPORT_SYMBOL_GPL(vcap_select_min_rule_keyset);
3552
3553/* Make a full copy of an existing rule with a new rule id */
3554struct vcap_rule *vcap_copy_rule(struct vcap_rule *erule)
3555{
3556	struct vcap_rule_internal *ri = to_intrule(erule);
3557	struct vcap_client_actionfield *caf;
3558	struct vcap_client_keyfield *ckf;
3559	struct vcap_rule *rule;
3560	int err;
3561
3562	err = vcap_api_check(ri->vctrl);
3563	if (err)
3564		return ERR_PTR(err);
3565
3566	rule = vcap_alloc_rule(ri->vctrl, ri->ndev, ri->data.vcap_chain_id,
3567			       ri->data.user, ri->data.priority, 0);
3568	if (IS_ERR(rule))
3569		return rule;
3570
3571	list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list) {
3572		/* Add a key duplicate in the new rule */
3573		err = vcap_rule_add_key(rule,
3574					ckf->ctrl.key,
3575					ckf->ctrl.type,
3576					&ckf->data);
3577		if (err)
3578			goto err;
3579	}
3580
3581	list_for_each_entry(caf, &ri->data.actionfields, ctrl.list) {
3582		/* Add a action duplicate in the new rule */
3583		err = vcap_rule_add_action(rule,
3584					   caf->ctrl.action,
3585					   caf->ctrl.type,
3586					   &caf->data);
3587		if (err)
3588			goto err;
3589	}
3590	return rule;
3591err:
3592	vcap_free_rule(rule);
3593	return ERR_PTR(err);
3594}
3595EXPORT_SYMBOL_GPL(vcap_copy_rule);
3596
3597#ifdef CONFIG_VCAP_KUNIT_TEST
3598#include "vcap_api_kunit.c"
3599#endif
v6.2
   1// SPDX-License-Identifier: GPL-2.0+
   2/* Microchip VCAP API
   3 *
   4 * Copyright (c) 2022 Microchip Technology Inc. and its subsidiaries.
   5 */
   6
   7#include <linux/types.h>
   8
   9#include "vcap_api_private.h"
  10
  11static int keyfield_size_table[] = {
  12	[VCAP_FIELD_BIT]  = sizeof(struct vcap_u1_key),
  13	[VCAP_FIELD_U32]  = sizeof(struct vcap_u32_key),
  14	[VCAP_FIELD_U48]  = sizeof(struct vcap_u48_key),
  15	[VCAP_FIELD_U56]  = sizeof(struct vcap_u56_key),
  16	[VCAP_FIELD_U64]  = sizeof(struct vcap_u64_key),
  17	[VCAP_FIELD_U72]  = sizeof(struct vcap_u72_key),
  18	[VCAP_FIELD_U112] = sizeof(struct vcap_u112_key),
  19	[VCAP_FIELD_U128] = sizeof(struct vcap_u128_key),
  20};
  21
  22static int actionfield_size_table[] = {
  23	[VCAP_FIELD_BIT]  = sizeof(struct vcap_u1_action),
  24	[VCAP_FIELD_U32]  = sizeof(struct vcap_u32_action),
  25	[VCAP_FIELD_U48]  = sizeof(struct vcap_u48_action),
  26	[VCAP_FIELD_U56]  = sizeof(struct vcap_u56_action),
  27	[VCAP_FIELD_U64]  = sizeof(struct vcap_u64_action),
  28	[VCAP_FIELD_U72]  = sizeof(struct vcap_u72_action),
  29	[VCAP_FIELD_U112] = sizeof(struct vcap_u112_action),
  30	[VCAP_FIELD_U128] = sizeof(struct vcap_u128_action),
  31};
  32
  33/* Moving a rule in the VCAP address space */
  34struct vcap_rule_move {
  35	int addr; /* address to move */
  36	int offset; /* change in address */
  37	int count; /* blocksize of addresses to move */
  38};
  39
  40/* Stores the filter cookie that enabled the port */
  41struct vcap_enabled_port {
  42	struct list_head list; /* for insertion in enabled ports list */
  43	struct net_device *ndev;  /* the enabled port */
  44	unsigned long cookie; /* filter that enabled the port */
 
 
  45};
  46
  47void 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
  57static 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
  68void 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
  80void 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
  88void 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
  95static 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
 106static 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
 123static 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
 140static 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
 172static bool vcap_bitarray_zero(int width, u8 *value)
 173{
 174	int bytes = DIV_ROUND_UP(width, BITS_PER_BYTE);
 175	u8 total = 0, bmask = 0xff;
 176	int rwidth = width;
 177	int idx;
 178
 179	for (idx = 0; idx < bytes; ++idx, rwidth -= BITS_PER_BYTE) {
 180		if (rwidth && rwidth < BITS_PER_BYTE)
 181			bmask = (1 << rwidth) - 1;
 182		total += value[idx] & bmask;
 183	}
 184	return total == 0;
 185}
 186
 187static bool vcap_get_bit(u32 *stream, struct vcap_stream_iter *itr)
 188{
 189	u32 mask = BIT(itr->reg_bitpos);
 190	u32 *p = &stream[itr->reg_idx];
 191
 192	return !!(*p & mask);
 193}
 194
 195static void vcap_decode_field(u32 *stream, struct vcap_stream_iter *itr,
 196			      int width, u8 *value)
 197{
 198	int idx;
 199
 200	/* Loop over the field value bits and get the field bits and
 201	 * set them in the output value byte array
 202	 */
 203	for (idx = 0; idx < width; idx++) {
 204		u8 bidx = idx & 0x7;
 205
 206		/* Decode one field value bit */
 207		if (vcap_get_bit(stream, itr))
 208			*value |= 1 << bidx;
 209		vcap_iter_next(itr);
 210		if (bidx == 7)
 211			value++;
 212	}
 213}
 214
 215/* Verify that the type id in the stream matches the type id of the keyset */
 216static bool vcap_verify_keystream_keyset(struct vcap_control *vctrl,
 217					 enum vcap_type vt,
 218					 u32 *keystream,
 219					 u32 *mskstream,
 220					 enum vcap_keyfield_set keyset)
 221{
 222	const struct vcap_info *vcap = &vctrl->vcaps[vt];
 223	const struct vcap_field *typefld;
 224	const struct vcap_typegroup *tgt;
 225	const struct vcap_field *fields;
 226	struct vcap_stream_iter iter;
 227	const struct vcap_set *info;
 228	u32 value = 0;
 229	u32 mask = 0;
 230
 231	if (vcap_keyfield_count(vctrl, vt, keyset) == 0)
 232		return false;
 233
 234	info = vcap_keyfieldset(vctrl, vt, keyset);
 235	/* Check that the keyset is valid */
 236	if (!info)
 237		return false;
 238
 239	/* a type_id of value -1 means that there is no type field */
 240	if (info->type_id == (u8)-1)
 241		return true;
 242
 243	/* Get a valid typegroup for the specific keyset */
 244	tgt = vcap_keyfield_typegroup(vctrl, vt, keyset);
 245	if (!tgt)
 246		return false;
 247
 248	fields = vcap_keyfields(vctrl, vt, keyset);
 249	if (!fields)
 250		return false;
 251
 252	typefld = &fields[VCAP_KF_TYPE];
 253	vcap_iter_init(&iter, vcap->sw_width, tgt, typefld->offset);
 254	vcap_decode_field(mskstream, &iter, typefld->width, (u8 *)&mask);
 255	/* no type info if there are no mask bits */
 256	if (vcap_bitarray_zero(typefld->width, (u8 *)&mask))
 257		return false;
 258
 259	/* Get the value of the type field in the stream and compare to the
 260	 * one define in the vcap keyset
 261	 */
 262	vcap_iter_init(&iter, vcap->sw_width, tgt, typefld->offset);
 263	vcap_decode_field(keystream, &iter, typefld->width, (u8 *)&value);
 264
 265	return (value & mask) == (info->type_id & mask);
 266}
 267
 268/* Verify that the typegroup bits have the correct values */
 269static int vcap_verify_typegroups(u32 *stream, int sw_width,
 270				  const struct vcap_typegroup *tgt, bool mask,
 271				  int sw_max)
 272{
 273	struct vcap_stream_iter iter;
 274	int sw_cnt, idx;
 275
 276	vcap_iter_set(&iter, sw_width, tgt, 0);
 277	sw_cnt = 0;
 278	while (iter.tg->width) {
 279		u32 value = 0;
 280		u32 tg_value = iter.tg->value;
 281
 282		if (mask)
 283			tg_value = (1 << iter.tg->width) - 1;
 284		/* Set position to current typegroup bit */
 285		iter.offset = iter.tg->offset;
 286		vcap_iter_update(&iter);
 287		for (idx = 0; idx < iter.tg->width; idx++) {
 288			/* Decode one typegroup bit */
 289			if (vcap_get_bit(stream, &iter))
 290				value |= 1 << idx;
 291			iter.offset++;
 292			vcap_iter_update(&iter);
 293		}
 294		if (value != tg_value)
 295			return -EINVAL;
 296		iter.tg++; /* next typegroup */
 297		sw_cnt++;
 298		/* Stop checking more typegroups */
 299		if (sw_max && sw_cnt >= sw_max)
 300			break;
 301	}
 302	return 0;
 303}
 304
 305/* Find the subword width of the key typegroup that matches the stream data */
 306static int vcap_find_keystream_typegroup_sw(struct vcap_control *vctrl,
 307					    enum vcap_type vt, u32 *stream,
 308					    bool mask, int sw_max)
 309{
 310	const struct vcap_typegroup **tgt;
 311	int sw_idx, res;
 312
 313	tgt = vctrl->vcaps[vt].keyfield_set_typegroups;
 314	/* Try the longest subword match first */
 315	for (sw_idx = vctrl->vcaps[vt].sw_count; sw_idx >= 0; sw_idx--) {
 316		if (!tgt[sw_idx])
 317			continue;
 318
 319		res = vcap_verify_typegroups(stream, vctrl->vcaps[vt].sw_width,
 320					     tgt[sw_idx], mask, sw_max);
 321		if (res == 0)
 322			return sw_idx;
 323	}
 324	return -EINVAL;
 325}
 326
 327/* Verify that the typegroup information, subword count, keyset and type id
 328 * are in sync and correct, return the list of matchin keysets
 329 */
 330int
 331vcap_find_keystream_keysets(struct vcap_control *vctrl,
 332			    enum vcap_type vt,
 333			    u32 *keystream,
 334			    u32 *mskstream,
 335			    bool mask, int sw_max,
 336			    struct vcap_keyset_list *kslist)
 337{
 338	const struct vcap_set *keyfield_set;
 339	int sw_count, idx;
 340
 341	sw_count = vcap_find_keystream_typegroup_sw(vctrl, vt, keystream, mask,
 342						    sw_max);
 343	if (sw_count < 0)
 344		return sw_count;
 345
 346	keyfield_set = vctrl->vcaps[vt].keyfield_set;
 347	for (idx = 0; idx < vctrl->vcaps[vt].keyfield_set_size; ++idx) {
 348		if (keyfield_set[idx].sw_per_item != sw_count)
 349			continue;
 350
 351		if (vcap_verify_keystream_keyset(vctrl, vt, keystream,
 352						 mskstream, idx))
 353			vcap_keyset_list_add(kslist, idx);
 354	}
 355	if (kslist->cnt > 0)
 356		return 0;
 357	return -EINVAL;
 358}
 359EXPORT_SYMBOL_GPL(vcap_find_keystream_keysets);
 360
 361/* Read key data from a VCAP address and discover if there are any rule keysets
 362 * here
 363 */
 364int vcap_addr_keysets(struct vcap_control *vctrl,
 365		      struct net_device *ndev,
 366		      struct vcap_admin *admin,
 367		      int addr,
 368		      struct vcap_keyset_list *kslist)
 369{
 370	enum vcap_type vt = admin->vtype;
 371	int keyset_sw_regs, idx;
 372	u32 key = 0, mask = 0;
 373
 374	/* Read the cache at the specified address */
 375	keyset_sw_regs = DIV_ROUND_UP(vctrl->vcaps[vt].sw_width, 32);
 376	vctrl->ops->update(ndev, admin, VCAP_CMD_READ, VCAP_SEL_ALL, addr);
 377	vctrl->ops->cache_read(ndev, admin, VCAP_SEL_ENTRY, 0,
 378			       keyset_sw_regs);
 379	/* Skip uninitialized key/mask entries */
 380	for (idx = 0; idx < keyset_sw_regs; ++idx) {
 381		key |= ~admin->cache.keystream[idx];
 382		mask |= admin->cache.maskstream[idx];
 383	}
 384	if (key == 0 && mask == 0)
 385		return -EINVAL;
 386	/* Decode and locate the keysets */
 387	return vcap_find_keystream_keysets(vctrl, vt, admin->cache.keystream,
 388					   admin->cache.maskstream, false, 0,
 389					   kslist);
 390}
 391EXPORT_SYMBOL_GPL(vcap_addr_keysets);
 392
 393/* Return the list of keyfields for the keyset */
 394const struct vcap_field *vcap_keyfields(struct vcap_control *vctrl,
 395					enum vcap_type vt,
 396					enum vcap_keyfield_set keyset)
 397{
 398	/* Check that the keyset exists in the vcap keyset list */
 399	if (keyset >= vctrl->vcaps[vt].keyfield_set_size)
 400		return NULL;
 401	return vctrl->vcaps[vt].keyfield_set_map[keyset];
 402}
 403
 404/* Return the keyset information for the keyset */
 405const struct vcap_set *vcap_keyfieldset(struct vcap_control *vctrl,
 406					enum vcap_type vt,
 407					enum vcap_keyfield_set keyset)
 408{
 409	const struct vcap_set *kset;
 410
 411	/* Check that the keyset exists in the vcap keyset list */
 412	if (keyset >= vctrl->vcaps[vt].keyfield_set_size)
 413		return NULL;
 414	kset = &vctrl->vcaps[vt].keyfield_set[keyset];
 415	if (kset->sw_per_item == 0 || kset->sw_per_item > vctrl->vcaps[vt].sw_count)
 416		return NULL;
 417	return kset;
 418}
 419EXPORT_SYMBOL_GPL(vcap_keyfieldset);
 420
 421/* Return the typegroup table for the matching keyset (using subword size) */
 422const struct vcap_typegroup *
 423vcap_keyfield_typegroup(struct vcap_control *vctrl,
 424			enum vcap_type vt, enum vcap_keyfield_set keyset)
 425{
 426	const struct vcap_set *kset = vcap_keyfieldset(vctrl, vt, keyset);
 427
 428	/* Check that the keyset is valid */
 429	if (!kset)
 430		return NULL;
 431	return vctrl->vcaps[vt].keyfield_set_typegroups[kset->sw_per_item];
 432}
 433
 434/* Return the number of keyfields in the keyset */
 435int vcap_keyfield_count(struct vcap_control *vctrl,
 436			enum vcap_type vt, enum vcap_keyfield_set keyset)
 437{
 438	/* Check that the keyset exists in the vcap keyset list */
 439	if (keyset >= vctrl->vcaps[vt].keyfield_set_size)
 440		return 0;
 441	return vctrl->vcaps[vt].keyfield_set_map_size[keyset];
 442}
 443
 444static void vcap_encode_keyfield(struct vcap_rule_internal *ri,
 445				 const struct vcap_client_keyfield *kf,
 446				 const struct vcap_field *rf,
 447				 const struct vcap_typegroup *tgt)
 448{
 449	int sw_width = ri->vctrl->vcaps[ri->admin->vtype].sw_width;
 450	struct vcap_cache_data *cache = &ri->admin->cache;
 451	struct vcap_stream_iter iter;
 452	const u8 *value, *mask;
 453
 454	/* Encode the fields for the key and the mask in their respective
 455	 * streams, respecting the subword width.
 456	 */
 457	switch (kf->ctrl.type) {
 458	case VCAP_FIELD_BIT:
 459		value = &kf->data.u1.value;
 460		mask = &kf->data.u1.mask;
 461		break;
 462	case VCAP_FIELD_U32:
 463		value = (const u8 *)&kf->data.u32.value;
 464		mask = (const u8 *)&kf->data.u32.mask;
 465		break;
 466	case VCAP_FIELD_U48:
 467		value = kf->data.u48.value;
 468		mask = kf->data.u48.mask;
 469		break;
 470	case VCAP_FIELD_U56:
 471		value = kf->data.u56.value;
 472		mask = kf->data.u56.mask;
 473		break;
 474	case VCAP_FIELD_U64:
 475		value = kf->data.u64.value;
 476		mask = kf->data.u64.mask;
 477		break;
 478	case VCAP_FIELD_U72:
 479		value = kf->data.u72.value;
 480		mask = kf->data.u72.mask;
 481		break;
 482	case VCAP_FIELD_U112:
 483		value = kf->data.u112.value;
 484		mask = kf->data.u112.mask;
 485		break;
 486	case VCAP_FIELD_U128:
 487		value = kf->data.u128.value;
 488		mask = kf->data.u128.mask;
 489		break;
 490	}
 491	vcap_iter_init(&iter, sw_width, tgt, rf->offset);
 492	vcap_encode_field(cache->keystream, &iter, rf->width, value);
 493	vcap_iter_init(&iter, sw_width, tgt, rf->offset);
 494	vcap_encode_field(cache->maskstream, &iter, rf->width, mask);
 495}
 496
 497static void vcap_encode_keyfield_typegroups(struct vcap_control *vctrl,
 498					    struct vcap_rule_internal *ri,
 499					    const struct vcap_typegroup *tgt)
 500{
 501	int sw_width = vctrl->vcaps[ri->admin->vtype].sw_width;
 502	struct vcap_cache_data *cache = &ri->admin->cache;
 503
 504	/* Encode the typegroup bits for the key and the mask in their streams,
 505	 * respecting the subword width.
 506	 */
 507	vcap_encode_typegroups(cache->keystream, sw_width, tgt, false);
 508	vcap_encode_typegroups(cache->maskstream, sw_width, tgt, true);
 509}
 510
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 511static int vcap_encode_rule_keyset(struct vcap_rule_internal *ri)
 512{
 513	const struct vcap_client_keyfield *ckf;
 514	const struct vcap_typegroup *tg_table;
 
 515	const struct vcap_field *kf_table;
 516	int keyset_size;
 517
 518	/* Get a valid set of fields for the specific keyset */
 519	kf_table = vcap_keyfields(ri->vctrl, ri->admin->vtype, ri->data.keyset);
 520	if (!kf_table) {
 521		pr_err("%s:%d: no fields available for this keyset: %d\n",
 522		       __func__, __LINE__, ri->data.keyset);
 523		return -EINVAL;
 524	}
 525	/* Get a valid typegroup for the specific keyset */
 526	tg_table = vcap_keyfield_typegroup(ri->vctrl, ri->admin->vtype,
 527					   ri->data.keyset);
 528	if (!tg_table) {
 529		pr_err("%s:%d: no typegroups available for this keyset: %d\n",
 530		       __func__, __LINE__, ri->data.keyset);
 531		return -EINVAL;
 532	}
 533	/* Get a valid size for the specific keyset */
 534	keyset_size = vcap_keyfield_count(ri->vctrl, ri->admin->vtype,
 535					  ri->data.keyset);
 536	if (keyset_size == 0) {
 537		pr_err("%s:%d: zero field count for this keyset: %d\n",
 538		       __func__, __LINE__, ri->data.keyset);
 539		return -EINVAL;
 540	}
 541	/* Iterate over the keyfields (key, mask) in the rule
 542	 * and encode these bits
 543	 */
 544	if (list_empty(&ri->data.keyfields)) {
 545		pr_err("%s:%d: no keyfields in the rule\n", __func__, __LINE__);
 546		return -EINVAL;
 547	}
 548	list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list) {
 549		/* Check that the client entry exists in the keyset */
 550		if (ckf->ctrl.key >= keyset_size) {
 551			pr_err("%s:%d: key %d is not in vcap\n",
 552			       __func__, __LINE__, ckf->ctrl.key);
 553			return -EINVAL;
 554		}
 555		vcap_encode_keyfield(ri, ckf, &kf_table[ckf->ctrl.key], tg_table);
 
 
 556	}
 557	/* Add typegroup bits to the key/mask bitstreams */
 558	vcap_encode_keyfield_typegroups(ri->vctrl, ri, tg_table);
 559	return 0;
 560}
 561
 562/* Return the list of actionfields for the actionset */
 563const struct vcap_field *
 564vcap_actionfields(struct vcap_control *vctrl,
 565		  enum vcap_type vt, enum vcap_actionfield_set actionset)
 566{
 567	/* Check that the actionset exists in the vcap actionset list */
 568	if (actionset >= vctrl->vcaps[vt].actionfield_set_size)
 569		return NULL;
 570	return vctrl->vcaps[vt].actionfield_set_map[actionset];
 571}
 572
 573const struct vcap_set *
 574vcap_actionfieldset(struct vcap_control *vctrl,
 575		    enum vcap_type vt, enum vcap_actionfield_set actionset)
 576{
 577	const struct vcap_set *aset;
 578
 579	/* Check that the actionset exists in the vcap actionset list */
 580	if (actionset >= vctrl->vcaps[vt].actionfield_set_size)
 581		return NULL;
 582	aset = &vctrl->vcaps[vt].actionfield_set[actionset];
 583	if (aset->sw_per_item == 0 || aset->sw_per_item > vctrl->vcaps[vt].sw_count)
 584		return NULL;
 585	return aset;
 586}
 587
 588/* Return the typegroup table for the matching actionset (using subword size) */
 589const struct vcap_typegroup *
 590vcap_actionfield_typegroup(struct vcap_control *vctrl,
 591			   enum vcap_type vt, enum vcap_actionfield_set actionset)
 592{
 593	const struct vcap_set *aset = vcap_actionfieldset(vctrl, vt, actionset);
 594
 595	/* Check that the actionset is valid */
 596	if (!aset)
 597		return NULL;
 598	return vctrl->vcaps[vt].actionfield_set_typegroups[aset->sw_per_item];
 599}
 600
 601/* Return the number of actionfields in the actionset */
 602int vcap_actionfield_count(struct vcap_control *vctrl,
 603			   enum vcap_type vt,
 604			   enum vcap_actionfield_set actionset)
 605{
 606	/* Check that the actionset exists in the vcap actionset list */
 607	if (actionset >= vctrl->vcaps[vt].actionfield_set_size)
 608		return 0;
 609	return vctrl->vcaps[vt].actionfield_set_map_size[actionset];
 610}
 611
 612static void vcap_encode_actionfield(struct vcap_rule_internal *ri,
 613				    const struct vcap_client_actionfield *af,
 614				    const struct vcap_field *rf,
 615				    const struct vcap_typegroup *tgt)
 616{
 617	int act_width = ri->vctrl->vcaps[ri->admin->vtype].act_width;
 618
 619	struct vcap_cache_data *cache = &ri->admin->cache;
 620	struct vcap_stream_iter iter;
 621	const u8 *value;
 622
 623	/* Encode the action field in the stream, respecting the subword width */
 624	switch (af->ctrl.type) {
 625	case VCAP_FIELD_BIT:
 626		value = &af->data.u1.value;
 627		break;
 628	case VCAP_FIELD_U32:
 629		value = (const u8 *)&af->data.u32.value;
 630		break;
 631	case VCAP_FIELD_U48:
 632		value = af->data.u48.value;
 633		break;
 634	case VCAP_FIELD_U56:
 635		value = af->data.u56.value;
 636		break;
 637	case VCAP_FIELD_U64:
 638		value = af->data.u64.value;
 639		break;
 640	case VCAP_FIELD_U72:
 641		value = af->data.u72.value;
 642		break;
 643	case VCAP_FIELD_U112:
 644		value = af->data.u112.value;
 645		break;
 646	case VCAP_FIELD_U128:
 647		value = af->data.u128.value;
 648		break;
 649	}
 650	vcap_iter_init(&iter, act_width, tgt, rf->offset);
 651	vcap_encode_field(cache->actionstream, &iter, rf->width, value);
 652}
 653
 654static void vcap_encode_actionfield_typegroups(struct vcap_rule_internal *ri,
 655					       const struct vcap_typegroup *tgt)
 656{
 657	int sw_width = ri->vctrl->vcaps[ri->admin->vtype].act_width;
 658	struct vcap_cache_data *cache = &ri->admin->cache;
 659
 660	/* Encode the typegroup bits for the actionstream respecting the subword
 661	 * width.
 662	 */
 663	vcap_encode_typegroups(cache->actionstream, sw_width, tgt, false);
 664}
 665
 666static int vcap_encode_rule_actionset(struct vcap_rule_internal *ri)
 667{
 668	const struct vcap_client_actionfield *caf;
 669	const struct vcap_typegroup *tg_table;
 
 670	const struct vcap_field *af_table;
 671	int actionset_size;
 672
 673	/* Get a valid set of actionset fields for the specific actionset */
 674	af_table = vcap_actionfields(ri->vctrl, ri->admin->vtype,
 675				     ri->data.actionset);
 676	if (!af_table) {
 677		pr_err("%s:%d: no fields available for this actionset: %d\n",
 678		       __func__, __LINE__, ri->data.actionset);
 679		return -EINVAL;
 680	}
 681	/* Get a valid typegroup for the specific actionset */
 682	tg_table = vcap_actionfield_typegroup(ri->vctrl, ri->admin->vtype,
 683					      ri->data.actionset);
 684	if (!tg_table) {
 685		pr_err("%s:%d: no typegroups available for this actionset: %d\n",
 686		       __func__, __LINE__, ri->data.actionset);
 687		return -EINVAL;
 688	}
 689	/* Get a valid actionset size for the specific actionset */
 690	actionset_size = vcap_actionfield_count(ri->vctrl, ri->admin->vtype,
 691						ri->data.actionset);
 692	if (actionset_size == 0) {
 693		pr_err("%s:%d: zero field count for this actionset: %d\n",
 694		       __func__, __LINE__, ri->data.actionset);
 695		return -EINVAL;
 696	}
 697	/* Iterate over the actionfields in the rule
 698	 * and encode these bits
 699	 */
 700	if (list_empty(&ri->data.actionfields))
 701		pr_warn("%s:%d: no actionfields in the rule\n",
 702			__func__, __LINE__);
 703	list_for_each_entry(caf, &ri->data.actionfields, ctrl.list) {
 704		/* Check that the client action exists in the actionset */
 705		if (caf->ctrl.action >= actionset_size) {
 706			pr_err("%s:%d: action %d is not in vcap\n",
 707			       __func__, __LINE__, caf->ctrl.action);
 708			return -EINVAL;
 709		}
 710		vcap_encode_actionfield(ri, caf, &af_table[caf->ctrl.action],
 711					tg_table);
 
 712	}
 713	/* Add typegroup bits to the entry bitstreams */
 714	vcap_encode_actionfield_typegroups(ri, tg_table);
 715	return 0;
 716}
 717
 718static int vcap_encode_rule(struct vcap_rule_internal *ri)
 719{
 720	int err;
 721
 722	err = vcap_encode_rule_keyset(ri);
 723	if (err)
 724		return err;
 725	err = vcap_encode_rule_actionset(ri);
 726	if (err)
 727		return err;
 728	return 0;
 729}
 730
 731int vcap_api_check(struct vcap_control *ctrl)
 732{
 733	if (!ctrl) {
 734		pr_err("%s:%d: vcap control is missing\n", __func__, __LINE__);
 735		return -EINVAL;
 736	}
 737	if (!ctrl->ops || !ctrl->ops->validate_keyset ||
 738	    !ctrl->ops->add_default_fields || !ctrl->ops->cache_erase ||
 739	    !ctrl->ops->cache_write || !ctrl->ops->cache_read ||
 740	    !ctrl->ops->init || !ctrl->ops->update || !ctrl->ops->move ||
 741	    !ctrl->ops->port_info || !ctrl->ops->enable) {
 742		pr_err("%s:%d: client operations are missing\n",
 743		       __func__, __LINE__);
 744		return -ENOENT;
 745	}
 746	return 0;
 747}
 748
 749void vcap_erase_cache(struct vcap_rule_internal *ri)
 750{
 751	ri->vctrl->ops->cache_erase(ri->admin);
 752}
 753
 754/* Update the keyset for the rule */
 755int vcap_set_rule_set_keyset(struct vcap_rule *rule,
 756			     enum vcap_keyfield_set keyset)
 757{
 758	struct vcap_rule_internal *ri = to_intrule(rule);
 759	const struct vcap_set *kset;
 760	int sw_width;
 761
 762	kset = vcap_keyfieldset(ri->vctrl, ri->admin->vtype, keyset);
 763	/* Check that the keyset is valid */
 764	if (!kset)
 765		return -EINVAL;
 766	ri->keyset_sw = kset->sw_per_item;
 767	sw_width = ri->vctrl->vcaps[ri->admin->vtype].sw_width;
 768	ri->keyset_sw_regs = DIV_ROUND_UP(sw_width, 32);
 769	ri->data.keyset = keyset;
 770	return 0;
 771}
 772EXPORT_SYMBOL_GPL(vcap_set_rule_set_keyset);
 773
 774/* Update the actionset for the rule */
 775int vcap_set_rule_set_actionset(struct vcap_rule *rule,
 776				enum vcap_actionfield_set actionset)
 777{
 778	struct vcap_rule_internal *ri = to_intrule(rule);
 779	const struct vcap_set *aset;
 780	int act_width;
 781
 782	aset = vcap_actionfieldset(ri->vctrl, ri->admin->vtype, actionset);
 783	/* Check that the actionset is valid */
 784	if (!aset)
 785		return -EINVAL;
 786	ri->actionset_sw = aset->sw_per_item;
 787	act_width = ri->vctrl->vcaps[ri->admin->vtype].act_width;
 788	ri->actionset_sw_regs = DIV_ROUND_UP(act_width, 32);
 789	ri->data.actionset = actionset;
 790	return 0;
 791}
 792EXPORT_SYMBOL_GPL(vcap_set_rule_set_actionset);
 793
 794/* Find a rule with a provided rule id */
 795static struct vcap_rule_internal *vcap_lookup_rule(struct vcap_control *vctrl,
 796						   u32 id)
 797{
 798	struct vcap_rule_internal *ri;
 799	struct vcap_admin *admin;
 800
 801	/* Look for the rule id in all vcaps */
 802	list_for_each_entry(admin, &vctrl->list, list)
 803		list_for_each_entry(ri, &admin->rules, list)
 804			if (ri->data.id == id)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 805				return ri;
 
 
 806	return NULL;
 807}
 808
 809/* Find a rule id with a provided cookie */
 810int vcap_lookup_rule_by_cookie(struct vcap_control *vctrl, u64 cookie)
 811{
 812	struct vcap_rule_internal *ri;
 813	struct vcap_admin *admin;
 
 814
 815	/* Look for the rule id in all vcaps */
 816	list_for_each_entry(admin, &vctrl->list, list)
 817		list_for_each_entry(ri, &admin->rules, list)
 818			if (ri->data.cookie == cookie)
 819				return ri->data.id;
 
 
 
 
 
 
 
 
 820	return -ENOENT;
 821}
 822EXPORT_SYMBOL_GPL(vcap_lookup_rule_by_cookie);
 823
 824/* Make a shallow copy of the rule without the fields */
 825struct vcap_rule_internal *vcap_dup_rule(struct vcap_rule_internal *ri)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 826{
 
 
 827	struct vcap_rule_internal *duprule;
 828
 829	/* Allocate the client part */
 830	duprule = kzalloc(sizeof(*duprule), GFP_KERNEL);
 831	if (!duprule)
 832		return ERR_PTR(-ENOMEM);
 833	*duprule = *ri;
 834	/* Not inserted in the VCAP */
 835	INIT_LIST_HEAD(&duprule->list);
 836	/* No elements in these lists */
 837	INIT_LIST_HEAD(&duprule->data.keyfields);
 838	INIT_LIST_HEAD(&duprule->data.actionfields);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 839	return duprule;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 840}
 841
 842static void vcap_apply_width(u8 *dst, int width, int bytes)
 843{
 844	u8 bmask;
 845	int idx;
 846
 847	for (idx = 0; idx < bytes; idx++) {
 848		if (width > 0)
 849			if (width < 8)
 850				bmask = (1 << width) - 1;
 851			else
 852				bmask = ~0;
 853		else
 854			bmask = 0;
 855		dst[idx] &= bmask;
 856		width -= 8;
 857	}
 858}
 859
 860static void vcap_copy_from_w32be(u8 *dst, u8 *src, int size, int width)
 861{
 862	int idx, ridx, wstart, nidx;
 863	int tail_bytes = (((size + 4) >> 2) << 2) - size;
 864
 865	for (idx = 0, ridx = size - 1; idx < size; ++idx, --ridx) {
 866		wstart = (idx >> 2) << 2;
 867		nidx = wstart + 3 - (idx & 0x3);
 868		if (nidx >= size)
 869			nidx -= tail_bytes;
 870		dst[nidx] = src[ridx];
 871	}
 872
 873	vcap_apply_width(dst, width, size);
 874}
 875
 876static void vcap_copy_action_bit_field(struct vcap_u1_action *field, u8 *value)
 877{
 878	field->value = (*value) & 0x1;
 879}
 880
 881static void vcap_copy_limited_actionfield(u8 *dstvalue, u8 *srcvalue,
 882					  int width, int bytes)
 883{
 884	memcpy(dstvalue, srcvalue, bytes);
 885	vcap_apply_width(dstvalue, width, bytes);
 886}
 887
 888static void vcap_copy_to_client_actionfield(struct vcap_rule_internal *ri,
 889					    struct vcap_client_actionfield *field,
 890					    u8 *value, u16 width)
 891{
 892	int field_size = actionfield_size_table[field->ctrl.type];
 893
 894	if (ri->admin->w32be) {
 895		switch (field->ctrl.type) {
 896		case VCAP_FIELD_BIT:
 897			vcap_copy_action_bit_field(&field->data.u1, value);
 898			break;
 899		case VCAP_FIELD_U32:
 900			vcap_copy_limited_actionfield((u8 *)&field->data.u32.value,
 901						      value,
 902						      width, field_size);
 903			break;
 904		case VCAP_FIELD_U48:
 905			vcap_copy_from_w32be(field->data.u48.value, value,
 906					     field_size, width);
 907			break;
 908		case VCAP_FIELD_U56:
 909			vcap_copy_from_w32be(field->data.u56.value, value,
 910					     field_size, width);
 911			break;
 912		case VCAP_FIELD_U64:
 913			vcap_copy_from_w32be(field->data.u64.value, value,
 914					     field_size, width);
 915			break;
 916		case VCAP_FIELD_U72:
 917			vcap_copy_from_w32be(field->data.u72.value, value,
 918					     field_size, width);
 919			break;
 920		case VCAP_FIELD_U112:
 921			vcap_copy_from_w32be(field->data.u112.value, value,
 922					     field_size, width);
 923			break;
 924		case VCAP_FIELD_U128:
 925			vcap_copy_from_w32be(field->data.u128.value, value,
 926					     field_size, width);
 927			break;
 928		};
 929	} else {
 930		switch (field->ctrl.type) {
 931		case VCAP_FIELD_BIT:
 932			vcap_copy_action_bit_field(&field->data.u1, value);
 933			break;
 934		case VCAP_FIELD_U32:
 935			vcap_copy_limited_actionfield((u8 *)&field->data.u32.value,
 936						      value,
 937						      width, field_size);
 938			break;
 939		case VCAP_FIELD_U48:
 940			vcap_copy_limited_actionfield(field->data.u48.value,
 941						      value,
 942						      width, field_size);
 943			break;
 944		case VCAP_FIELD_U56:
 945			vcap_copy_limited_actionfield(field->data.u56.value,
 946						      value,
 947						      width, field_size);
 948			break;
 949		case VCAP_FIELD_U64:
 950			vcap_copy_limited_actionfield(field->data.u64.value,
 951						      value,
 952						      width, field_size);
 953			break;
 954		case VCAP_FIELD_U72:
 955			vcap_copy_limited_actionfield(field->data.u72.value,
 956						      value,
 957						      width, field_size);
 958			break;
 959		case VCAP_FIELD_U112:
 960			vcap_copy_limited_actionfield(field->data.u112.value,
 961						      value,
 962						      width, field_size);
 963			break;
 964		case VCAP_FIELD_U128:
 965			vcap_copy_limited_actionfield(field->data.u128.value,
 966						      value,
 967						      width, field_size);
 968			break;
 969		};
 970	}
 971}
 972
 973static void vcap_copy_key_bit_field(struct vcap_u1_key *field,
 974				    u8 *value, u8 *mask)
 975{
 976	field->value = (*value) & 0x1;
 977	field->mask = (*mask) & 0x1;
 978}
 979
 980static void vcap_copy_limited_keyfield(u8 *dstvalue, u8 *dstmask,
 981				       u8 *srcvalue, u8 *srcmask,
 982				       int width, int bytes)
 983{
 984	memcpy(dstvalue, srcvalue, bytes);
 985	vcap_apply_width(dstvalue, width, bytes);
 986	memcpy(dstmask, srcmask, bytes);
 987	vcap_apply_width(dstmask, width, bytes);
 988}
 989
 990static void vcap_copy_to_client_keyfield(struct vcap_rule_internal *ri,
 991					 struct vcap_client_keyfield *field,
 992					 u8 *value, u8 *mask, u16 width)
 993{
 994	int field_size = keyfield_size_table[field->ctrl.type] / 2;
 995
 996	if (ri->admin->w32be) {
 997		switch (field->ctrl.type) {
 998		case VCAP_FIELD_BIT:
 999			vcap_copy_key_bit_field(&field->data.u1, value, mask);
1000			break;
1001		case VCAP_FIELD_U32:
1002			vcap_copy_limited_keyfield((u8 *)&field->data.u32.value,
1003						   (u8 *)&field->data.u32.mask,
1004						   value, mask,
1005						   width, field_size);
1006			break;
1007		case VCAP_FIELD_U48:
1008			vcap_copy_from_w32be(field->data.u48.value, value,
1009					     field_size, width);
1010			vcap_copy_from_w32be(field->data.u48.mask,  mask,
1011					     field_size, width);
1012			break;
1013		case VCAP_FIELD_U56:
1014			vcap_copy_from_w32be(field->data.u56.value, value,
1015					     field_size, width);
1016			vcap_copy_from_w32be(field->data.u56.mask,  mask,
1017					     field_size, width);
1018			break;
1019		case VCAP_FIELD_U64:
1020			vcap_copy_from_w32be(field->data.u64.value, value,
1021					     field_size, width);
1022			vcap_copy_from_w32be(field->data.u64.mask,  mask,
1023					     field_size, width);
1024			break;
1025		case VCAP_FIELD_U72:
1026			vcap_copy_from_w32be(field->data.u72.value, value,
1027					     field_size, width);
1028			vcap_copy_from_w32be(field->data.u72.mask,  mask,
1029					     field_size, width);
1030			break;
1031		case VCAP_FIELD_U112:
1032			vcap_copy_from_w32be(field->data.u112.value, value,
1033					     field_size, width);
1034			vcap_copy_from_w32be(field->data.u112.mask,  mask,
1035					     field_size, width);
1036			break;
1037		case VCAP_FIELD_U128:
1038			vcap_copy_from_w32be(field->data.u128.value, value,
1039					     field_size, width);
1040			vcap_copy_from_w32be(field->data.u128.mask,  mask,
1041					     field_size, width);
1042			break;
1043		};
1044	} else {
1045		switch (field->ctrl.type) {
1046		case VCAP_FIELD_BIT:
1047			vcap_copy_key_bit_field(&field->data.u1, value, mask);
1048			break;
1049		case VCAP_FIELD_U32:
1050			vcap_copy_limited_keyfield((u8 *)&field->data.u32.value,
1051						   (u8 *)&field->data.u32.mask,
1052						   value, mask,
1053						   width, field_size);
1054			break;
1055		case VCAP_FIELD_U48:
1056			vcap_copy_limited_keyfield(field->data.u48.value,
1057						   field->data.u48.mask,
1058						   value, mask,
1059						   width, field_size);
1060			break;
1061		case VCAP_FIELD_U56:
1062			vcap_copy_limited_keyfield(field->data.u56.value,
1063						   field->data.u56.mask,
1064						   value, mask,
1065						   width, field_size);
1066			break;
1067		case VCAP_FIELD_U64:
1068			vcap_copy_limited_keyfield(field->data.u64.value,
1069						   field->data.u64.mask,
1070						   value, mask,
1071						   width, field_size);
1072			break;
1073		case VCAP_FIELD_U72:
1074			vcap_copy_limited_keyfield(field->data.u72.value,
1075						   field->data.u72.mask,
1076						   value, mask,
1077						   width, field_size);
1078			break;
1079		case VCAP_FIELD_U112:
1080			vcap_copy_limited_keyfield(field->data.u112.value,
1081						   field->data.u112.mask,
1082						   value, mask,
1083						   width, field_size);
1084			break;
1085		case VCAP_FIELD_U128:
1086			vcap_copy_limited_keyfield(field->data.u128.value,
1087						   field->data.u128.mask,
1088						   value, mask,
1089						   width, field_size);
1090			break;
1091		};
1092	}
1093}
1094
1095static void vcap_rule_alloc_keyfield(struct vcap_rule_internal *ri,
1096				     const struct vcap_field *keyfield,
1097				     enum vcap_key_field key,
1098				     u8 *value, u8 *mask)
1099{
1100	struct vcap_client_keyfield *field;
1101
1102	field = kzalloc(sizeof(*field), GFP_KERNEL);
1103	if (!field)
1104		return;
1105	INIT_LIST_HEAD(&field->ctrl.list);
1106	field->ctrl.key = key;
1107	field->ctrl.type = keyfield->type;
1108	vcap_copy_to_client_keyfield(ri, field, value, mask, keyfield->width);
1109	list_add_tail(&field->ctrl.list, &ri->data.keyfields);
1110}
1111
1112/* Read key data from a VCAP address and discover if there is a rule keyset
1113 * here
1114 */
1115static bool
1116vcap_verify_actionstream_actionset(struct vcap_control *vctrl,
1117				   enum vcap_type vt,
1118				   u32 *actionstream,
1119				   enum vcap_actionfield_set actionset)
1120{
1121	const struct vcap_typegroup *tgt;
1122	const struct vcap_field *fields;
1123	const struct vcap_set *info;
1124
1125	if (vcap_actionfield_count(vctrl, vt, actionset) == 0)
1126		return false;
1127
1128	info = vcap_actionfieldset(vctrl, vt, actionset);
1129	/* Check that the actionset is valid */
1130	if (!info)
1131		return false;
1132
1133	/* a type_id of value -1 means that there is no type field */
1134	if (info->type_id == (u8)-1)
1135		return true;
1136
1137	/* Get a valid typegroup for the specific actionset */
1138	tgt = vcap_actionfield_typegroup(vctrl, vt, actionset);
1139	if (!tgt)
1140		return false;
1141
1142	fields = vcap_actionfields(vctrl, vt, actionset);
1143	if (!fields)
1144		return false;
1145
1146	/* Later this will be expanded with a check of the type id */
1147	return true;
1148}
1149
1150/* Find the subword width of the action typegroup that matches the stream data
1151 */
1152static int vcap_find_actionstream_typegroup_sw(struct vcap_control *vctrl,
1153					       enum vcap_type vt, u32 *stream,
1154					       int sw_max)
1155{
1156	const struct vcap_typegroup **tgt;
1157	int sw_idx, res;
1158
1159	tgt = vctrl->vcaps[vt].actionfield_set_typegroups;
1160	/* Try the longest subword match first */
1161	for (sw_idx = vctrl->vcaps[vt].sw_count; sw_idx >= 0; sw_idx--) {
1162		if (!tgt[sw_idx])
1163			continue;
1164		res = vcap_verify_typegroups(stream, vctrl->vcaps[vt].act_width,
1165					     tgt[sw_idx], false, sw_max);
1166		if (res == 0)
1167			return sw_idx;
1168	}
1169	return -EINVAL;
1170}
1171
1172/* Verify that the typegroup information, subword count, actionset and type id
1173 * are in sync and correct, return the actionset
1174 */
1175static enum vcap_actionfield_set
1176vcap_find_actionstream_actionset(struct vcap_control *vctrl,
1177				 enum vcap_type vt,
1178				 u32 *stream,
1179				 int sw_max)
1180{
1181	const struct vcap_set *actionfield_set;
1182	int sw_count, idx;
1183	bool res;
1184
1185	sw_count = vcap_find_actionstream_typegroup_sw(vctrl, vt, stream,
1186						       sw_max);
1187	if (sw_count < 0)
1188		return sw_count;
1189
1190	actionfield_set = vctrl->vcaps[vt].actionfield_set;
1191	for (idx = 0; idx < vctrl->vcaps[vt].actionfield_set_size; ++idx) {
1192		if (actionfield_set[idx].sw_per_item != sw_count)
1193			continue;
1194
1195		res = vcap_verify_actionstream_actionset(vctrl, vt,
1196							 stream, idx);
1197		if (res)
1198			return idx;
1199	}
1200	return -EINVAL;
1201}
1202
1203/* Store action value in an element in a list for the client */
1204static void vcap_rule_alloc_actionfield(struct vcap_rule_internal *ri,
1205					const struct vcap_field *actionfield,
1206					enum vcap_action_field action,
1207					u8 *value)
1208{
1209	struct vcap_client_actionfield *field;
1210
1211	field = kzalloc(sizeof(*field), GFP_KERNEL);
1212	if (!field)
1213		return;
1214	INIT_LIST_HEAD(&field->ctrl.list);
1215	field->ctrl.action = action;
1216	field->ctrl.type = actionfield->type;
1217	vcap_copy_to_client_actionfield(ri, field, value, actionfield->width);
1218	list_add_tail(&field->ctrl.list, &ri->data.actionfields);
1219}
1220
1221static int vcap_decode_actionset(struct vcap_rule_internal *ri)
1222{
1223	struct vcap_control *vctrl = ri->vctrl;
1224	struct vcap_admin *admin = ri->admin;
1225	const struct vcap_field *actionfield;
1226	enum vcap_actionfield_set actionset;
1227	enum vcap_type vt = admin->vtype;
1228	const struct vcap_typegroup *tgt;
1229	struct vcap_stream_iter iter;
1230	int idx, res, actfield_count;
1231	u32 *actstream;
1232	u8 value[16];
1233
1234	actstream = admin->cache.actionstream;
1235	res = vcap_find_actionstream_actionset(vctrl, vt, actstream, 0);
1236	if (res < 0) {
1237		pr_err("%s:%d: could not find valid actionset: %d\n",
1238		       __func__, __LINE__, res);
1239		return -EINVAL;
1240	}
1241	actionset = res;
1242	actfield_count = vcap_actionfield_count(vctrl, vt, actionset);
1243	actionfield = vcap_actionfields(vctrl, vt, actionset);
1244	tgt = vcap_actionfield_typegroup(vctrl, vt, actionset);
1245	/* Start decoding the stream */
1246	for (idx = 0; idx < actfield_count; ++idx) {
1247		if (actionfield[idx].width <= 0)
1248			continue;
1249		/* Get the action */
1250		memset(value, 0, DIV_ROUND_UP(actionfield[idx].width, 8));
1251		vcap_iter_init(&iter, vctrl->vcaps[vt].act_width, tgt,
1252			       actionfield[idx].offset);
1253		vcap_decode_field(actstream, &iter, actionfield[idx].width,
1254				  value);
1255		/* Skip if no bits are set */
1256		if (vcap_bitarray_zero(actionfield[idx].width, value))
1257			continue;
1258		vcap_rule_alloc_actionfield(ri, &actionfield[idx], idx, value);
1259		/* Later the action id will also be checked */
1260	}
1261	return vcap_set_rule_set_actionset((struct vcap_rule *)ri, actionset);
1262}
1263
1264static int vcap_decode_keyset(struct vcap_rule_internal *ri)
1265{
1266	struct vcap_control *vctrl = ri->vctrl;
1267	struct vcap_stream_iter kiter, miter;
1268	struct vcap_admin *admin = ri->admin;
1269	enum vcap_keyfield_set keysets[10];
1270	const struct vcap_field *keyfield;
1271	enum vcap_type vt = admin->vtype;
1272	const struct vcap_typegroup *tgt;
1273	struct vcap_keyset_list matches;
1274	enum vcap_keyfield_set keyset;
1275	int idx, res, keyfield_count;
1276	u32 *maskstream;
1277	u32 *keystream;
1278	u8 value[16];
1279	u8 mask[16];
1280
1281	keystream = admin->cache.keystream;
1282	maskstream = admin->cache.maskstream;
1283	matches.keysets = keysets;
1284	matches.cnt = 0;
1285	matches.max = ARRAY_SIZE(keysets);
1286	res = vcap_find_keystream_keysets(vctrl, vt, keystream, maskstream,
1287					  false, 0, &matches);
1288	if (res < 0) {
1289		pr_err("%s:%d: could not find valid keysets: %d\n",
1290		       __func__, __LINE__, res);
1291		return -EINVAL;
1292	}
1293	keyset = matches.keysets[0];
1294	keyfield_count = vcap_keyfield_count(vctrl, vt, keyset);
1295	keyfield = vcap_keyfields(vctrl, vt, keyset);
1296	tgt = vcap_keyfield_typegroup(vctrl, vt, keyset);
1297	/* Start decoding the streams */
1298	for (idx = 0; idx < keyfield_count; ++idx) {
1299		if (keyfield[idx].width <= 0)
1300			continue;
1301		/* First get the mask */
1302		memset(mask, 0, DIV_ROUND_UP(keyfield[idx].width, 8));
1303		vcap_iter_init(&miter, vctrl->vcaps[vt].sw_width, tgt,
1304			       keyfield[idx].offset);
1305		vcap_decode_field(maskstream, &miter, keyfield[idx].width,
1306				  mask);
1307		/* Skip if no mask bits are set */
1308		if (vcap_bitarray_zero(keyfield[idx].width, mask))
1309			continue;
1310		/* Get the key */
1311		memset(value, 0, DIV_ROUND_UP(keyfield[idx].width, 8));
1312		vcap_iter_init(&kiter, vctrl->vcaps[vt].sw_width, tgt,
1313			       keyfield[idx].offset);
1314		vcap_decode_field(keystream, &kiter, keyfield[idx].width,
1315				  value);
1316		vcap_rule_alloc_keyfield(ri, &keyfield[idx], idx, value, mask);
1317	}
1318	return vcap_set_rule_set_keyset((struct vcap_rule *)ri, keyset);
1319}
1320
1321/* Read VCAP content into the VCAP cache */
1322static int vcap_read_rule(struct vcap_rule_internal *ri)
1323{
1324	struct vcap_admin *admin = ri->admin;
1325	int sw_idx, ent_idx = 0, act_idx = 0;
1326	u32 addr = ri->addr;
1327
1328	if (!ri->size || !ri->keyset_sw_regs || !ri->actionset_sw_regs) {
1329		pr_err("%s:%d: rule is empty\n", __func__, __LINE__);
1330		return -EINVAL;
1331	}
1332	vcap_erase_cache(ri);
1333	/* Use the values in the streams to read the VCAP cache */
1334	for (sw_idx = 0; sw_idx < ri->size; sw_idx++, addr++) {
1335		ri->vctrl->ops->update(ri->ndev, admin, VCAP_CMD_READ,
1336				       VCAP_SEL_ALL, addr);
1337		ri->vctrl->ops->cache_read(ri->ndev, admin,
1338					   VCAP_SEL_ENTRY, ent_idx,
1339					   ri->keyset_sw_regs);
1340		ri->vctrl->ops->cache_read(ri->ndev, admin,
1341					   VCAP_SEL_ACTION, act_idx,
1342					   ri->actionset_sw_regs);
1343		if (sw_idx == 0)
1344			ri->vctrl->ops->cache_read(ri->ndev, admin,
1345						   VCAP_SEL_COUNTER,
1346						   ri->counter_id, 0);
1347		ent_idx += ri->keyset_sw_regs;
1348		act_idx += ri->actionset_sw_regs;
1349	}
1350	return 0;
1351}
1352
1353/* Write VCAP cache content to the VCAP HW instance */
1354static int vcap_write_rule(struct vcap_rule_internal *ri)
1355{
1356	struct vcap_admin *admin = ri->admin;
1357	int sw_idx, ent_idx = 0, act_idx = 0;
1358	u32 addr = ri->addr;
1359
1360	if (!ri->size || !ri->keyset_sw_regs || !ri->actionset_sw_regs) {
1361		pr_err("%s:%d: rule is empty\n", __func__, __LINE__);
1362		return -EINVAL;
1363	}
1364	/* Use the values in the streams to write the VCAP cache */
1365	for (sw_idx = 0; sw_idx < ri->size; sw_idx++, addr++) {
1366		ri->vctrl->ops->cache_write(ri->ndev, admin,
1367					    VCAP_SEL_ENTRY, ent_idx,
1368					    ri->keyset_sw_regs);
1369		ri->vctrl->ops->cache_write(ri->ndev, admin,
1370					    VCAP_SEL_ACTION, act_idx,
1371					    ri->actionset_sw_regs);
1372		ri->vctrl->ops->update(ri->ndev, admin, VCAP_CMD_WRITE,
1373				       VCAP_SEL_ALL, addr);
1374		ent_idx += ri->keyset_sw_regs;
1375		act_idx += ri->actionset_sw_regs;
1376	}
1377	return 0;
1378}
1379
1380static int vcap_write_counter(struct vcap_rule_internal *ri,
1381			      struct vcap_counter *ctr)
1382{
1383	struct vcap_admin *admin = ri->admin;
1384
1385	admin->cache.counter = ctr->value;
1386	admin->cache.sticky = ctr->sticky;
1387	ri->vctrl->ops->cache_write(ri->ndev, admin, VCAP_SEL_COUNTER,
1388				    ri->counter_id, 0);
1389	ri->vctrl->ops->update(ri->ndev, admin, VCAP_CMD_WRITE,
1390			       VCAP_SEL_COUNTER, ri->addr);
1391	return 0;
1392}
1393
1394/* Convert a chain id to a VCAP lookup index */
1395int vcap_chain_id_to_lookup(struct vcap_admin *admin, int cur_cid)
1396{
1397	int lookup_first = admin->vinst * admin->lookups_per_instance;
1398	int lookup_last = lookup_first + admin->lookups_per_instance;
1399	int cid_next = admin->first_cid + VCAP_CID_LOOKUP_SIZE;
1400	int cid = admin->first_cid;
1401	int lookup;
1402
1403	for (lookup = lookup_first; lookup < lookup_last; ++lookup,
1404	     cid += VCAP_CID_LOOKUP_SIZE, cid_next += VCAP_CID_LOOKUP_SIZE)
1405		if (cur_cid >= cid && cur_cid < cid_next)
1406			return lookup;
1407	return 0;
1408}
1409EXPORT_SYMBOL_GPL(vcap_chain_id_to_lookup);
1410
1411/* Lookup a vcap instance using chain id */
1412struct vcap_admin *vcap_find_admin(struct vcap_control *vctrl, int cid)
1413{
1414	struct vcap_admin *admin;
1415
1416	if (vcap_api_check(vctrl))
1417		return NULL;
1418
1419	list_for_each_entry(admin, &vctrl->list, list) {
1420		if (cid >= admin->first_cid && cid <= admin->last_cid)
1421			return admin;
1422	}
1423	return NULL;
1424}
1425EXPORT_SYMBOL_GPL(vcap_find_admin);
1426
1427/* Is the next chain id in the following lookup, possible in another VCAP */
1428bool vcap_is_next_lookup(struct vcap_control *vctrl, int cur_cid, int next_cid)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1429{
1430	struct vcap_admin *admin, *next_admin;
1431	int lookup, next_lookup;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1432
1433	/* The offset must be at least one lookup */
1434	if (next_cid < cur_cid + VCAP_CID_LOOKUP_SIZE)
1435		return false;
1436
1437	if (vcap_api_check(vctrl))
 
 
 
1438		return false;
1439
1440	admin = vcap_find_admin(vctrl, cur_cid);
1441	if (!admin)
1442		return false;
1443
1444	/* If no VCAP contains the next chain, the next chain must be beyond
1445	 * the last chain in the current VCAP
1446	 */
1447	next_admin = vcap_find_admin(vctrl, next_cid);
1448	if (!next_admin)
1449		return next_cid > admin->last_cid;
1450
1451	lookup = vcap_chain_id_to_lookup(admin, cur_cid);
1452	next_lookup = vcap_chain_id_to_lookup(next_admin, next_cid);
1453
1454	/* Next lookup must be the following lookup */
1455	if (admin == next_admin || admin->vtype == next_admin->vtype)
1456		return next_lookup == lookup + 1;
1457
1458	/* Must be the first lookup in the next VCAP instance */
1459	return next_lookup == 0;
1460}
1461EXPORT_SYMBOL_GPL(vcap_is_next_lookup);
1462
1463/* Check if there is room for a new rule */
1464static int vcap_rule_space(struct vcap_admin *admin, int size)
1465{
1466	if (admin->last_used_addr - size < admin->first_valid_addr) {
1467		pr_err("%s:%d: No room for rule size: %u, %u\n",
1468		       __func__, __LINE__, size, admin->first_valid_addr);
1469		return -ENOSPC;
1470	}
1471	return 0;
1472}
1473
1474/* Add the keyset typefield to the list of rule keyfields */
1475static int vcap_add_type_keyfield(struct vcap_rule *rule)
1476{
1477	struct vcap_rule_internal *ri = to_intrule(rule);
1478	enum vcap_keyfield_set keyset = rule->keyset;
1479	enum vcap_type vt = ri->admin->vtype;
1480	const struct vcap_field *fields;
1481	const struct vcap_set *kset;
1482	int ret = -EINVAL;
1483
1484	kset = vcap_keyfieldset(ri->vctrl, vt, keyset);
1485	if (!kset)
1486		return ret;
1487	if (kset->type_id == (u8)-1)  /* No type field is needed */
1488		return 0;
1489
1490	fields = vcap_keyfields(ri->vctrl, vt, keyset);
1491	if (!fields)
1492		return -EINVAL;
1493	if (fields[VCAP_KF_TYPE].width > 1) {
1494		ret = vcap_rule_add_key_u32(rule, VCAP_KF_TYPE,
1495					    kset->type_id, 0xff);
1496	} else {
1497		if (kset->type_id)
1498			ret = vcap_rule_add_key_bit(rule, VCAP_KF_TYPE,
1499						    VCAP_BIT_1);
1500		else
1501			ret = vcap_rule_add_key_bit(rule, VCAP_KF_TYPE,
1502						    VCAP_BIT_0);
1503	}
1504	return 0;
1505}
1506
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1507/* Add a keyset to a keyset list */
1508bool vcap_keyset_list_add(struct vcap_keyset_list *keysetlist,
1509			  enum vcap_keyfield_set keyset)
1510{
1511	int idx;
1512
1513	if (keysetlist->cnt < keysetlist->max) {
1514		/* Avoid duplicates */
1515		for (idx = 0; idx < keysetlist->cnt; ++idx)
1516			if (keysetlist->keysets[idx] == keyset)
1517				return keysetlist->cnt < keysetlist->max;
1518		keysetlist->keysets[keysetlist->cnt++] = keyset;
1519	}
1520	return keysetlist->cnt < keysetlist->max;
1521}
1522EXPORT_SYMBOL_GPL(vcap_keyset_list_add);
1523
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1524/* map keyset id to a string with the keyset name */
1525const char *vcap_keyset_name(struct vcap_control *vctrl,
1526			     enum vcap_keyfield_set keyset)
1527{
1528	return vctrl->stats->keyfield_set_names[keyset];
1529}
1530EXPORT_SYMBOL_GPL(vcap_keyset_name);
1531
1532/* map key field id to a string with the key name */
1533const char *vcap_keyfield_name(struct vcap_control *vctrl,
1534			       enum vcap_key_field key)
1535{
1536	return vctrl->stats->keyfield_names[key];
1537}
1538EXPORT_SYMBOL_GPL(vcap_keyfield_name);
1539
1540/* map actionset id to a string with the actionset name */
1541const char *vcap_actionset_name(struct vcap_control *vctrl,
1542				enum vcap_actionfield_set actionset)
1543{
1544	return vctrl->stats->actionfield_set_names[actionset];
1545}
1546
1547/* map action field id to a string with the action name */
1548const char *vcap_actionfield_name(struct vcap_control *vctrl,
1549				  enum vcap_action_field action)
1550{
1551	return vctrl->stats->actionfield_names[action];
1552}
1553
1554/* Return the keyfield that matches a key in a keyset */
1555static const struct vcap_field *
1556vcap_find_keyset_keyfield(struct vcap_control *vctrl,
1557			  enum vcap_type vtype,
1558			  enum vcap_keyfield_set keyset,
1559			  enum vcap_key_field key)
1560{
1561	const struct vcap_field *fields;
1562	int idx, count;
1563
1564	fields = vcap_keyfields(vctrl, vtype, keyset);
1565	if (!fields)
1566		return NULL;
1567
1568	/* Iterate the keyfields of the keyset */
1569	count = vcap_keyfield_count(vctrl, vtype, keyset);
1570	for (idx = 0; idx < count; ++idx) {
1571		if (fields[idx].width == 0)
1572			continue;
1573
1574		if (key == idx)
1575			return &fields[idx];
1576	}
1577
1578	return NULL;
1579}
1580
1581/* Match a list of keys against the keysets available in a vcap type */
1582static bool _vcap_rule_find_keysets(struct vcap_rule_internal *ri,
1583				    struct vcap_keyset_list *matches)
1584{
1585	const struct vcap_client_keyfield *ckf;
1586	int keyset, found, keycount, map_size;
1587	const struct vcap_field **map;
1588	enum vcap_type vtype;
1589
1590	vtype = ri->admin->vtype;
1591	map = ri->vctrl->vcaps[vtype].keyfield_set_map;
1592	map_size = ri->vctrl->vcaps[vtype].keyfield_set_size;
1593
1594	/* Get a count of the keyfields we want to match */
1595	keycount = 0;
1596	list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list)
1597		++keycount;
1598
1599	matches->cnt = 0;
1600	/* Iterate the keysets of the VCAP */
1601	for (keyset = 0; keyset < map_size; ++keyset) {
1602		if (!map[keyset])
1603			continue;
1604
1605		/* Iterate the keys in the rule */
1606		found = 0;
1607		list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list)
1608			if (vcap_find_keyset_keyfield(ri->vctrl, vtype,
1609						      keyset, ckf->ctrl.key))
1610				++found;
1611
1612		/* Save the keyset if all keyfields were found */
1613		if (found == keycount)
1614			if (!vcap_keyset_list_add(matches, keyset))
1615				/* bail out when the quota is filled */
1616				break;
1617	}
1618
1619	return matches->cnt > 0;
1620}
1621
1622/* Match a list of keys against the keysets available in a vcap type */
1623bool vcap_rule_find_keysets(struct vcap_rule *rule,
1624			    struct vcap_keyset_list *matches)
1625{
1626	struct vcap_rule_internal *ri = to_intrule(rule);
1627
1628	return _vcap_rule_find_keysets(ri, matches);
1629}
1630EXPORT_SYMBOL_GPL(vcap_rule_find_keysets);
1631
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1632/* Validate a rule with respect to available port keys */
1633int vcap_val_rule(struct vcap_rule *rule, u16 l3_proto)
1634{
1635	struct vcap_rule_internal *ri = to_intrule(rule);
1636	struct vcap_keyset_list matches = {};
1637	enum vcap_keyfield_set keysets[10];
1638	int ret;
1639
1640	ret = vcap_api_check(ri->vctrl);
1641	if (ret)
1642		return ret;
1643	if (!ri->admin) {
1644		ri->data.exterr = VCAP_ERR_NO_ADMIN;
1645		return -EINVAL;
1646	}
1647	if (!ri->ndev) {
1648		ri->data.exterr = VCAP_ERR_NO_NETDEV;
1649		return -EINVAL;
1650	}
1651
1652	matches.keysets = keysets;
1653	matches.max = ARRAY_SIZE(keysets);
1654	if (ri->data.keyset == VCAP_KFS_NO_VALUE) {
1655		/* Iterate over rule keyfields and select keysets that fits */
1656		if (!_vcap_rule_find_keysets(ri, &matches)) {
1657			ri->data.exterr = VCAP_ERR_NO_KEYSET_MATCH;
1658			return -EINVAL;
1659		}
1660	} else {
1661		/* prepare for keyset validation */
1662		keysets[0] = ri->data.keyset;
1663		matches.cnt = 1;
1664	}
1665
1666	/* Pick a keyset that is supported in the port lookups */
1667	ret = ri->vctrl->ops->validate_keyset(ri->ndev, ri->admin, rule,
1668					      &matches, l3_proto);
1669	if (ret < 0) {
1670		pr_err("%s:%d: keyset validation failed: %d\n",
1671		       __func__, __LINE__, ret);
1672		ri->data.exterr = VCAP_ERR_NO_PORT_KEYSET_MATCH;
1673		return ret;
1674	}
1675	/* use the keyset that is supported in the port lookups */
1676	ret = vcap_set_rule_set_keyset(rule, ret);
1677	if (ret < 0) {
1678		pr_err("%s:%d: keyset was not updated: %d\n",
1679		       __func__, __LINE__, ret);
1680		return ret;
1681	}
1682	if (ri->data.actionset == VCAP_AFS_NO_VALUE) {
1683		/* Later also actionsets will be matched against actions in
1684		 * the rule, and the type will be set accordingly
1685		 */
1686		ri->data.exterr = VCAP_ERR_NO_ACTIONSET_MATCH;
1687		return -EINVAL;
 
 
 
 
 
 
 
 
 
 
 
 
1688	}
1689	vcap_add_type_keyfield(rule);
 
1690	/* Add default fields to this rule */
1691	ri->vctrl->ops->add_default_fields(ri->ndev, ri->admin, rule);
1692
1693	/* Rule size is the maximum of the entry and action subword count */
1694	ri->size = max(ri->keyset_sw, ri->actionset_sw);
1695
1696	/* Finally check if there is room for the rule in the VCAP */
1697	return vcap_rule_space(ri->admin, ri->size);
1698}
1699EXPORT_SYMBOL_GPL(vcap_val_rule);
1700
1701/* Entries are sorted with increasing values of sort_key.
1702 * I.e. Lowest numerical sort_key is first in list.
1703 * In order to locate largest keys first in list we negate the key size with
1704 * (max_size - size).
1705 */
1706static u32 vcap_sort_key(u32 max_size, u32 size, u8 user, u16 prio)
1707{
1708	return ((max_size - size) << 24) | (user << 16) | prio;
1709}
1710
1711/* calculate the address of the next rule after this (lower address and prio) */
1712static u32 vcap_next_rule_addr(u32 addr, struct vcap_rule_internal *ri)
1713{
1714	return ((addr - ri->size) /  ri->size) * ri->size;
1715}
1716
1717/* Assign a unique rule id and autogenerate one if id == 0 */
1718static u32 vcap_set_rule_id(struct vcap_rule_internal *ri)
1719{
1720	if (ri->data.id != 0)
1721		return ri->data.id;
1722
1723	for (u32 next_id = 1; next_id < ~0; ++next_id) {
1724		if (!vcap_lookup_rule(ri->vctrl, next_id)) {
1725			ri->data.id = next_id;
1726			break;
1727		}
1728	}
1729	return ri->data.id;
1730}
1731
1732static int vcap_insert_rule(struct vcap_rule_internal *ri,
1733			    struct vcap_rule_move *move)
1734{
1735	int sw_count = ri->vctrl->vcaps[ri->admin->vtype].sw_count;
1736	struct vcap_rule_internal *duprule, *iter, *elem = NULL;
1737	struct vcap_admin *admin = ri->admin;
1738	u32 addr;
1739
1740	ri->sort_key = vcap_sort_key(sw_count, ri->size, ri->data.user,
1741				     ri->data.priority);
1742
1743	/* Insert the new rule in the list of rule based on the sort key
1744	 * If the rule needs to be  inserted between existing rules then move
1745	 * these rules to make room for the new rule and update their start
1746	 * address.
1747	 */
1748	list_for_each_entry(iter, &admin->rules, list) {
1749		if (ri->sort_key < iter->sort_key) {
1750			elem = iter;
1751			break;
1752		}
1753	}
1754
1755	if (!elem) {
1756		ri->addr = vcap_next_rule_addr(admin->last_used_addr, ri);
1757		admin->last_used_addr = ri->addr;
1758
1759		/* Add a shallow copy of the rule to the VCAP list */
1760		duprule = vcap_dup_rule(ri);
1761		if (IS_ERR(duprule))
1762			return PTR_ERR(duprule);
1763
1764		list_add_tail(&duprule->list, &admin->rules);
1765		return 0;
1766	}
1767
1768	/* Reuse the space of the current rule */
1769	addr = elem->addr + elem->size;
1770	ri->addr = vcap_next_rule_addr(addr, ri);
1771	addr = ri->addr;
1772
1773	/* Add a shallow copy of the rule to the VCAP list */
1774	duprule = vcap_dup_rule(ri);
1775	if (IS_ERR(duprule))
1776		return PTR_ERR(duprule);
1777
1778	/* Add before the current entry */
1779	list_add_tail(&duprule->list, &elem->list);
1780
1781	/* Update the current rule */
1782	elem->addr = vcap_next_rule_addr(addr, elem);
1783	addr = elem->addr;
1784
1785	/* Update the address in the remaining rules in the list */
1786	list_for_each_entry_continue(elem, &admin->rules, list) {
1787		elem->addr = vcap_next_rule_addr(addr, elem);
1788		addr = elem->addr;
1789	}
1790
1791	/* Update the move info */
1792	move->addr = admin->last_used_addr;
1793	move->count = ri->addr - addr;
1794	move->offset = admin->last_used_addr - addr;
1795	admin->last_used_addr = addr;
1796	return 0;
1797}
1798
1799static void vcap_move_rules(struct vcap_rule_internal *ri,
1800			    struct vcap_rule_move *move)
1801{
1802	ri->vctrl->ops->move(ri->ndev, ri->admin, move->addr,
1803			 move->offset, move->count);
1804}
1805
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1806/* Encode and write a validated rule to the VCAP */
1807int vcap_add_rule(struct vcap_rule *rule)
1808{
1809	struct vcap_rule_internal *ri = to_intrule(rule);
1810	struct vcap_rule_move move = {0};
 
1811	int ret;
1812
1813	ret = vcap_api_check(ri->vctrl);
1814	if (ret)
1815		return ret;
1816	/* Insert the new rule in the list of vcap rules */
1817	mutex_lock(&ri->admin->lock);
 
 
1818	ret = vcap_insert_rule(ri, &move);
1819	if (ret < 0) {
1820		pr_err("%s:%d: could not insert rule in vcap list: %d\n",
1821		       __func__, __LINE__, ret);
1822		goto out;
1823	}
1824	if (move.count > 0)
1825		vcap_move_rules(ri, &move);
 
 
 
 
 
 
 
 
 
 
 
 
 
1826	ret = vcap_encode_rule(ri);
1827	if (ret) {
1828		pr_err("%s:%d: rule encoding error: %d\n", __func__, __LINE__, ret);
1829		goto out;
1830	}
1831
1832	ret = vcap_write_rule(ri);
1833	if (ret)
1834		pr_err("%s:%d: rule write error: %d\n", __func__, __LINE__, ret);
 
 
1835out:
1836	mutex_unlock(&ri->admin->lock);
1837	return ret;
1838}
1839EXPORT_SYMBOL_GPL(vcap_add_rule);
1840
1841/* Allocate a new rule with the provided arguments */
1842struct vcap_rule *vcap_alloc_rule(struct vcap_control *vctrl,
1843				  struct net_device *ndev, int vcap_chain_id,
1844				  enum vcap_user user, u16 priority,
1845				  u32 id)
1846{
1847	struct vcap_rule_internal *ri;
1848	struct vcap_admin *admin;
1849	int err, maxsize;
1850
1851	err = vcap_api_check(vctrl);
1852	if (err)
1853		return ERR_PTR(err);
1854	if (!ndev)
1855		return ERR_PTR(-ENODEV);
1856	/* Get the VCAP instance */
1857	admin = vcap_find_admin(vctrl, vcap_chain_id);
1858	if (!admin)
1859		return ERR_PTR(-ENOENT);
1860	/* Sanity check that this VCAP is supported on this platform */
1861	if (vctrl->vcaps[admin->vtype].rows == 0)
1862		return ERR_PTR(-EINVAL);
 
 
1863	/* Check if a rule with this id already exists */
1864	if (vcap_lookup_rule(vctrl, id))
1865		return ERR_PTR(-EEXIST);
 
 
 
1866	/* Check if there is room for the rule in the block(s) of the VCAP */
1867	maxsize = vctrl->vcaps[admin->vtype].sw_count; /* worst case rule size */
1868	if (vcap_rule_space(admin, maxsize))
1869		return ERR_PTR(-ENOSPC);
 
 
 
1870	/* Create a container for the rule and return it */
1871	ri = kzalloc(sizeof(*ri), GFP_KERNEL);
1872	if (!ri)
1873		return ERR_PTR(-ENOMEM);
 
 
 
1874	ri->data.vcap_chain_id = vcap_chain_id;
1875	ri->data.user = user;
1876	ri->data.priority = priority;
1877	ri->data.id = id;
1878	ri->data.keyset = VCAP_KFS_NO_VALUE;
1879	ri->data.actionset = VCAP_AFS_NO_VALUE;
1880	INIT_LIST_HEAD(&ri->list);
1881	INIT_LIST_HEAD(&ri->data.keyfields);
1882	INIT_LIST_HEAD(&ri->data.actionfields);
1883	ri->ndev = ndev;
1884	ri->admin = admin; /* refer to the vcap instance */
1885	ri->vctrl = vctrl; /* refer to the client */
1886	if (vcap_set_rule_id(ri) == 0)
 
 
1887		goto out_free;
1888	vcap_erase_cache(ri);
 
 
1889	return (struct vcap_rule *)ri;
1890
1891out_free:
1892	kfree(ri);
1893	return ERR_PTR(-EINVAL);
 
 
 
1894}
1895EXPORT_SYMBOL_GPL(vcap_alloc_rule);
1896
1897/* Free mem of a rule owned by client after the rule as been added to the VCAP */
1898void vcap_free_rule(struct vcap_rule *rule)
1899{
1900	struct vcap_rule_internal *ri = to_intrule(rule);
1901	struct vcap_client_actionfield *caf, *next_caf;
1902	struct vcap_client_keyfield *ckf, *next_ckf;
1903
1904	/* Deallocate the list of keys and actions */
1905	list_for_each_entry_safe(ckf, next_ckf, &ri->data.keyfields, ctrl.list) {
1906		list_del(&ckf->ctrl.list);
1907		kfree(ckf);
1908	}
1909	list_for_each_entry_safe(caf, next_caf, &ri->data.actionfields, ctrl.list) {
1910		list_del(&caf->ctrl.list);
1911		kfree(caf);
1912	}
1913	/* Deallocate the rule */
1914	kfree(rule);
1915}
1916EXPORT_SYMBOL_GPL(vcap_free_rule);
1917
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1918struct vcap_rule *vcap_get_rule(struct vcap_control *vctrl, u32 id)
1919{
1920	struct vcap_rule_internal *elem;
1921	struct vcap_rule_internal *ri;
1922	int err;
1923
1924	ri = NULL;
1925
1926	err = vcap_api_check(vctrl);
1927	if (err)
1928		return ERR_PTR(err);
1929	elem = vcap_lookup_rule(vctrl, id);
 
1930	if (!elem)
1931		return NULL;
1932	mutex_lock(&elem->admin->lock);
1933	ri = vcap_dup_rule(elem);
1934	if (IS_ERR(ri))
1935		goto unlock;
1936	err = vcap_read_rule(ri);
1937	if (err) {
1938		ri = ERR_PTR(err);
1939		goto unlock;
1940	}
1941	err = vcap_decode_keyset(ri);
1942	if (err) {
1943		ri = ERR_PTR(err);
1944		goto unlock;
1945	}
1946	err = vcap_decode_actionset(ri);
1947	if (err) {
1948		ri = ERR_PTR(err);
1949		goto unlock;
1950	}
1951
1952unlock:
1953	mutex_unlock(&elem->admin->lock);
1954	return (struct vcap_rule *)ri;
1955}
1956EXPORT_SYMBOL_GPL(vcap_get_rule);
1957
1958/* Update existing rule */
1959int vcap_mod_rule(struct vcap_rule *rule)
1960{
1961	struct vcap_rule_internal *ri = to_intrule(rule);
1962	struct vcap_counter ctr;
1963	int err;
1964
1965	err = vcap_api_check(ri->vctrl);
1966	if (err)
1967		return err;
1968
1969	if (!vcap_lookup_rule(ri->vctrl, ri->data.id))
1970		return -ENOENT;
1971
1972	mutex_lock(&ri->admin->lock);
 
 
 
1973	/* Encode the bitstreams to the VCAP cache */
1974	vcap_erase_cache(ri);
1975	err = vcap_encode_rule(ri);
1976	if (err)
1977		goto out;
1978
1979	err = vcap_write_rule(ri);
1980	if (err)
1981		goto out;
1982
1983	memset(&ctr, 0, sizeof(ctr));
1984	err =  vcap_write_counter(ri, &ctr);
1985	if (err)
1986		goto out;
1987
1988out:
1989	mutex_unlock(&ri->admin->lock);
1990	return err;
1991}
1992EXPORT_SYMBOL_GPL(vcap_mod_rule);
1993
1994/* Return the alignment offset for a new rule address */
1995static int vcap_valid_rule_move(struct vcap_rule_internal *el, int offset)
1996{
1997	return (el->addr + offset) % el->size;
1998}
1999
2000/* Update the rule address with an offset */
2001static void vcap_adjust_rule_addr(struct vcap_rule_internal *el, int offset)
2002{
2003	el->addr += offset;
2004}
2005
2006/* Rules needs to be moved to fill the gap of the deleted rule */
2007static int vcap_fill_rule_gap(struct vcap_rule_internal *ri)
2008{
2009	struct vcap_admin *admin = ri->admin;
2010	struct vcap_rule_internal *elem;
2011	struct vcap_rule_move move;
2012	int gap = 0, offset = 0;
2013
2014	/* If the first rule is deleted: Move other rules to the top */
2015	if (list_is_first(&ri->list, &admin->rules))
2016		offset = admin->last_valid_addr + 1 - ri->addr - ri->size;
2017
2018	/* Locate gaps between odd size rules and adjust the move */
2019	elem = ri;
2020	list_for_each_entry_continue(elem, &admin->rules, list)
2021		gap += vcap_valid_rule_move(elem, ri->size);
2022
2023	/* Update the address in the remaining rules in the list */
2024	elem = ri;
2025	list_for_each_entry_continue(elem, &admin->rules, list)
2026		vcap_adjust_rule_addr(elem, ri->size + gap + offset);
2027
2028	/* Update the move info */
2029	move.addr = admin->last_used_addr;
2030	move.count = ri->addr - admin->last_used_addr - gap;
2031	move.offset = -(ri->size + gap + offset);
2032
2033	/* Do the actual move operation */
2034	vcap_move_rules(ri, &move);
2035
2036	return gap + offset;
2037}
2038
2039/* Delete rule in a VCAP instance */
2040int vcap_del_rule(struct vcap_control *vctrl, struct net_device *ndev, u32 id)
2041{
2042	struct vcap_rule_internal *ri, *elem;
2043	struct vcap_admin *admin;
2044	int gap = 0, err;
2045
2046	/* This will later also handle rule moving */
2047	if (!ndev)
2048		return -ENODEV;
2049	err = vcap_api_check(vctrl);
2050	if (err)
2051		return err;
2052	/* Look for the rule id in all vcaps */
2053	ri = vcap_lookup_rule(vctrl, id);
2054	if (!ri)
2055		return -EINVAL;
 
2056	admin = ri->admin;
2057
2058	if (ri->addr > admin->last_used_addr)
2059		gap = vcap_fill_rule_gap(ri);
2060
2061	/* Delete the rule from the list of rules and the cache */
2062	mutex_lock(&admin->lock);
2063	list_del(&ri->list);
2064	vctrl->ops->init(ndev, admin, admin->last_used_addr, ri->size + gap);
2065	kfree(ri);
2066	mutex_unlock(&admin->lock);
2067
2068	/* Update the last used address, set to default when no rules */
2069	if (list_empty(&admin->rules)) {
2070		admin->last_used_addr = admin->last_valid_addr + 1;
2071	} else {
2072		elem = list_last_entry(&admin->rules, struct vcap_rule_internal,
2073				       list);
2074		admin->last_used_addr = elem->addr;
2075	}
2076	return 0;
 
 
2077}
2078EXPORT_SYMBOL_GPL(vcap_del_rule);
2079
2080/* Delete all rules in the VCAP instance */
2081int vcap_del_rules(struct vcap_control *vctrl, struct vcap_admin *admin)
2082{
2083	struct vcap_enabled_port *eport, *next_eport;
2084	struct vcap_rule_internal *ri, *next_ri;
2085	int ret = vcap_api_check(vctrl);
2086
2087	if (ret)
2088		return ret;
2089
2090	mutex_lock(&admin->lock);
2091	list_for_each_entry_safe(ri, next_ri, &admin->rules, list) {
2092		vctrl->ops->init(ri->ndev, admin, ri->addr, ri->size);
2093		list_del(&ri->list);
2094		kfree(ri);
2095	}
2096	admin->last_used_addr = admin->last_valid_addr;
2097
2098	/* Remove list of enabled ports */
2099	list_for_each_entry_safe(eport, next_eport, &admin->enabled, list) {
2100		list_del(&eport->list);
2101		kfree(eport);
2102	}
2103	mutex_unlock(&admin->lock);
2104
2105	return 0;
2106}
2107EXPORT_SYMBOL_GPL(vcap_del_rules);
2108
2109/* Find a client key field in a rule */
2110static struct vcap_client_keyfield *
2111vcap_find_keyfield(struct vcap_rule *rule, enum vcap_key_field key)
2112{
2113	struct vcap_rule_internal *ri = to_intrule(rule);
2114	struct vcap_client_keyfield *ckf;
2115
2116	list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list)
2117		if (ckf->ctrl.key == key)
2118			return ckf;
2119	return NULL;
2120}
2121
2122/* Find information on a key field in a rule */
2123const struct vcap_field *vcap_lookup_keyfield(struct vcap_rule *rule,
2124					      enum vcap_key_field key)
2125{
2126	struct vcap_rule_internal *ri = to_intrule(rule);
2127	enum vcap_keyfield_set keyset = rule->keyset;
2128	enum vcap_type vt = ri->admin->vtype;
2129	const struct vcap_field *fields;
2130
2131	if (keyset == VCAP_KFS_NO_VALUE)
2132		return NULL;
2133	fields = vcap_keyfields(ri->vctrl, vt, keyset);
2134	if (!fields)
2135		return NULL;
2136	return &fields[key];
2137}
2138EXPORT_SYMBOL_GPL(vcap_lookup_keyfield);
2139
2140/* Copy data from src to dst but reverse the data in chunks of 32bits.
2141 * For example if src is 00:11:22:33:44:55 where 55 is LSB the dst will
2142 * have the value 22:33:44:55:00:11.
2143 */
2144static void vcap_copy_to_w32be(u8 *dst, u8 *src, int size)
2145{
2146	for (int idx = 0; idx < size; ++idx) {
2147		int first_byte_index = 0;
2148		int nidx;
2149
2150		first_byte_index = size - (((idx >> 2) + 1) << 2);
2151		if (first_byte_index < 0)
2152			first_byte_index = 0;
2153		nidx = idx + first_byte_index - (idx & ~0x3);
2154		dst[nidx] = src[idx];
2155	}
2156}
2157
2158static void vcap_copy_from_client_keyfield(struct vcap_rule *rule,
2159					   struct vcap_client_keyfield *field,
2160					   struct vcap_client_keyfield_data *data)
2161{
2162	struct vcap_rule_internal *ri = to_intrule(rule);
2163	int size;
2164
2165	if (!ri->admin->w32be) {
2166		memcpy(&field->data, data, sizeof(field->data));
2167		return;
2168	}
2169
2170	size = keyfield_size_table[field->ctrl.type] / 2;
2171	switch (field->ctrl.type) {
2172	case VCAP_FIELD_BIT:
2173	case VCAP_FIELD_U32:
2174		memcpy(&field->data, data, sizeof(field->data));
2175		break;
2176	case VCAP_FIELD_U48:
2177		vcap_copy_to_w32be(field->data.u48.value, data->u48.value, size);
2178		vcap_copy_to_w32be(field->data.u48.mask,  data->u48.mask, size);
2179		break;
2180	case VCAP_FIELD_U56:
2181		vcap_copy_to_w32be(field->data.u56.value, data->u56.value, size);
2182		vcap_copy_to_w32be(field->data.u56.mask,  data->u56.mask, size);
2183		break;
2184	case VCAP_FIELD_U64:
2185		vcap_copy_to_w32be(field->data.u64.value, data->u64.value, size);
2186		vcap_copy_to_w32be(field->data.u64.mask,  data->u64.mask, size);
2187		break;
2188	case VCAP_FIELD_U72:
2189		vcap_copy_to_w32be(field->data.u72.value, data->u72.value, size);
2190		vcap_copy_to_w32be(field->data.u72.mask,  data->u72.mask, size);
2191		break;
2192	case VCAP_FIELD_U112:
2193		vcap_copy_to_w32be(field->data.u112.value, data->u112.value, size);
2194		vcap_copy_to_w32be(field->data.u112.mask,  data->u112.mask, size);
2195		break;
2196	case VCAP_FIELD_U128:
2197		vcap_copy_to_w32be(field->data.u128.value, data->u128.value, size);
2198		vcap_copy_to_w32be(field->data.u128.mask,  data->u128.mask, size);
2199		break;
2200	}
2201}
2202
2203/* Check if the keyfield is already in the rule */
2204static bool vcap_keyfield_unique(struct vcap_rule *rule,
2205				 enum vcap_key_field key)
2206{
2207	struct vcap_rule_internal *ri = to_intrule(rule);
2208	const struct vcap_client_keyfield *ckf;
2209
2210	list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list)
2211		if (ckf->ctrl.key == key)
2212			return false;
2213	return true;
2214}
2215
2216/* Check if the keyfield is in the keyset */
2217static bool vcap_keyfield_match_keyset(struct vcap_rule *rule,
2218				       enum vcap_key_field key)
2219{
2220	struct vcap_rule_internal *ri = to_intrule(rule);
2221	enum vcap_keyfield_set keyset = rule->keyset;
2222	enum vcap_type vt = ri->admin->vtype;
2223	const struct vcap_field *fields;
2224
2225	/* the field is accepted if the rule has no keyset yet */
2226	if (keyset == VCAP_KFS_NO_VALUE)
2227		return true;
2228	fields = vcap_keyfields(ri->vctrl, vt, keyset);
2229	if (!fields)
2230		return false;
2231	/* if there is a width there is a way */
2232	return fields[key].width > 0;
2233}
2234
2235static int vcap_rule_add_key(struct vcap_rule *rule,
2236			     enum vcap_key_field key,
2237			     enum vcap_field_type ftype,
2238			     struct vcap_client_keyfield_data *data)
2239{
2240	struct vcap_rule_internal *ri = to_intrule(rule);
2241	struct vcap_client_keyfield *field;
2242
2243	if (!vcap_keyfield_unique(rule, key)) {
2244		pr_warn("%s:%d: keyfield %s is already in the rule\n",
2245			__func__, __LINE__,
2246			vcap_keyfield_name(ri->vctrl, key));
2247		return -EINVAL;
2248	}
2249
2250	if (!vcap_keyfield_match_keyset(rule, key)) {
2251		pr_err("%s:%d: keyfield %s does not belong in the rule keyset\n",
2252		       __func__, __LINE__,
2253		       vcap_keyfield_name(ri->vctrl, key));
2254		return -EINVAL;
2255	}
2256
2257	field = kzalloc(sizeof(*field), GFP_KERNEL);
2258	if (!field)
2259		return -ENOMEM;
 
2260	field->ctrl.key = key;
2261	field->ctrl.type = ftype;
2262	vcap_copy_from_client_keyfield(rule, field, data);
2263	list_add_tail(&field->ctrl.list, &rule->keyfields);
2264	return 0;
2265}
2266
2267static void vcap_rule_set_key_bitsize(struct vcap_u1_key *u1, enum vcap_bit val)
2268{
2269	switch (val) {
2270	case VCAP_BIT_0:
2271		u1->value = 0;
2272		u1->mask = 1;
2273		break;
2274	case VCAP_BIT_1:
2275		u1->value = 1;
2276		u1->mask = 1;
2277		break;
2278	case VCAP_BIT_ANY:
2279		u1->value = 0;
2280		u1->mask = 0;
2281		break;
2282	}
2283}
2284
2285/* Add a bit key with value and mask to the rule */
2286int vcap_rule_add_key_bit(struct vcap_rule *rule, enum vcap_key_field key,
2287			  enum vcap_bit val)
2288{
2289	struct vcap_client_keyfield_data data;
2290
2291	vcap_rule_set_key_bitsize(&data.u1, val);
2292	return vcap_rule_add_key(rule, key, VCAP_FIELD_BIT, &data);
2293}
2294EXPORT_SYMBOL_GPL(vcap_rule_add_key_bit);
2295
2296/* Add a 32 bit key field with value and mask to the rule */
2297int vcap_rule_add_key_u32(struct vcap_rule *rule, enum vcap_key_field key,
2298			  u32 value, u32 mask)
2299{
2300	struct vcap_client_keyfield_data data;
2301
2302	data.u32.value = value;
2303	data.u32.mask = mask;
2304	return vcap_rule_add_key(rule, key, VCAP_FIELD_U32, &data);
2305}
2306EXPORT_SYMBOL_GPL(vcap_rule_add_key_u32);
2307
2308/* Add a 48 bit key with value and mask to the rule */
2309int vcap_rule_add_key_u48(struct vcap_rule *rule, enum vcap_key_field key,
2310			  struct vcap_u48_key *fieldval)
2311{
2312	struct vcap_client_keyfield_data data;
2313
2314	memcpy(&data.u48, fieldval, sizeof(data.u48));
2315	return vcap_rule_add_key(rule, key, VCAP_FIELD_U48, &data);
2316}
2317EXPORT_SYMBOL_GPL(vcap_rule_add_key_u48);
2318
2319/* Add a 72 bit key with value and mask to the rule */
2320int vcap_rule_add_key_u72(struct vcap_rule *rule, enum vcap_key_field key,
2321			  struct vcap_u72_key *fieldval)
2322{
2323	struct vcap_client_keyfield_data data;
2324
2325	memcpy(&data.u72, fieldval, sizeof(data.u72));
2326	return vcap_rule_add_key(rule, key, VCAP_FIELD_U72, &data);
2327}
2328EXPORT_SYMBOL_GPL(vcap_rule_add_key_u72);
2329
2330/* Add a 128 bit key with value and mask to the rule */
2331int vcap_rule_add_key_u128(struct vcap_rule *rule, enum vcap_key_field key,
2332			   struct vcap_u128_key *fieldval)
2333{
2334	struct vcap_client_keyfield_data data;
2335
2336	memcpy(&data.u128, fieldval, sizeof(data.u128));
2337	return vcap_rule_add_key(rule, key, VCAP_FIELD_U128, &data);
2338}
2339EXPORT_SYMBOL_GPL(vcap_rule_add_key_u128);
2340
2341int vcap_rule_get_key_u32(struct vcap_rule *rule, enum vcap_key_field key,
2342			  u32 *value, u32 *mask)
2343{
2344	struct vcap_client_keyfield *ckf;
2345
2346	ckf = vcap_find_keyfield(rule, key);
2347	if (!ckf)
2348		return -ENOENT;
2349
2350	*value = ckf->data.u32.value;
2351	*mask = ckf->data.u32.mask;
2352
2353	return 0;
2354}
2355EXPORT_SYMBOL_GPL(vcap_rule_get_key_u32);
2356
2357/* Find a client action field in a rule */
2358static struct vcap_client_actionfield *
2359vcap_find_actionfield(struct vcap_rule *rule, enum vcap_action_field act)
2360{
2361	struct vcap_rule_internal *ri = (struct vcap_rule_internal *)rule;
2362	struct vcap_client_actionfield *caf;
2363
2364	list_for_each_entry(caf, &ri->data.actionfields, ctrl.list)
2365		if (caf->ctrl.action == act)
2366			return caf;
2367	return NULL;
2368}
2369
2370static void vcap_copy_from_client_actionfield(struct vcap_rule *rule,
2371					      struct vcap_client_actionfield *field,
2372					      struct vcap_client_actionfield_data *data)
2373{
2374	struct vcap_rule_internal *ri = to_intrule(rule);
2375	int size;
2376
2377	if (!ri->admin->w32be) {
2378		memcpy(&field->data, data, sizeof(field->data));
2379		return;
2380	}
2381
2382	size = actionfield_size_table[field->ctrl.type];
2383	switch (field->ctrl.type) {
2384	case VCAP_FIELD_BIT:
2385	case VCAP_FIELD_U32:
2386		memcpy(&field->data, data, sizeof(field->data));
2387		break;
2388	case VCAP_FIELD_U48:
2389		vcap_copy_to_w32be(field->data.u48.value, data->u48.value, size);
2390		break;
2391	case VCAP_FIELD_U56:
2392		vcap_copy_to_w32be(field->data.u56.value, data->u56.value, size);
2393		break;
2394	case VCAP_FIELD_U64:
2395		vcap_copy_to_w32be(field->data.u64.value, data->u64.value, size);
2396		break;
2397	case VCAP_FIELD_U72:
2398		vcap_copy_to_w32be(field->data.u72.value, data->u72.value, size);
2399		break;
2400	case VCAP_FIELD_U112:
2401		vcap_copy_to_w32be(field->data.u112.value, data->u112.value, size);
2402		break;
2403	case VCAP_FIELD_U128:
2404		vcap_copy_to_w32be(field->data.u128.value, data->u128.value, size);
2405		break;
2406	}
2407}
2408
2409/* Check if the actionfield is already in the rule */
2410static bool vcap_actionfield_unique(struct vcap_rule *rule,
2411				    enum vcap_action_field act)
2412{
2413	struct vcap_rule_internal *ri = to_intrule(rule);
2414	const struct vcap_client_actionfield *caf;
2415
2416	list_for_each_entry(caf, &ri->data.actionfields, ctrl.list)
2417		if (caf->ctrl.action == act)
2418			return false;
2419	return true;
2420}
2421
2422/* Check if the actionfield is in the actionset */
2423static bool vcap_actionfield_match_actionset(struct vcap_rule *rule,
2424					     enum vcap_action_field action)
2425{
2426	enum vcap_actionfield_set actionset = rule->actionset;
2427	struct vcap_rule_internal *ri = to_intrule(rule);
2428	enum vcap_type vt = ri->admin->vtype;
2429	const struct vcap_field *fields;
2430
2431	/* the field is accepted if the rule has no actionset yet */
2432	if (actionset == VCAP_AFS_NO_VALUE)
2433		return true;
2434	fields = vcap_actionfields(ri->vctrl, vt, actionset);
2435	if (!fields)
2436		return false;
2437	/* if there is a width there is a way */
2438	return fields[action].width > 0;
2439}
2440
2441static int vcap_rule_add_action(struct vcap_rule *rule,
2442				enum vcap_action_field action,
2443				enum vcap_field_type ftype,
2444				struct vcap_client_actionfield_data *data)
2445{
2446	struct vcap_rule_internal *ri = to_intrule(rule);
2447	struct vcap_client_actionfield *field;
2448
2449	if (!vcap_actionfield_unique(rule, action)) {
2450		pr_warn("%s:%d: actionfield %s is already in the rule\n",
2451			__func__, __LINE__,
2452			vcap_actionfield_name(ri->vctrl, action));
2453		return -EINVAL;
2454	}
2455
2456	if (!vcap_actionfield_match_actionset(rule, action)) {
2457		pr_err("%s:%d: actionfield %s does not belong in the rule actionset\n",
2458		       __func__, __LINE__,
2459		       vcap_actionfield_name(ri->vctrl, action));
2460		return -EINVAL;
2461	}
2462
2463	field = kzalloc(sizeof(*field), GFP_KERNEL);
2464	if (!field)
2465		return -ENOMEM;
 
2466	field->ctrl.action = action;
2467	field->ctrl.type = ftype;
2468	vcap_copy_from_client_actionfield(rule, field, data);
2469	list_add_tail(&field->ctrl.list, &rule->actionfields);
2470	return 0;
2471}
2472
2473static void vcap_rule_set_action_bitsize(struct vcap_u1_action *u1,
2474					 enum vcap_bit val)
2475{
2476	switch (val) {
2477	case VCAP_BIT_0:
2478		u1->value = 0;
2479		break;
2480	case VCAP_BIT_1:
2481		u1->value = 1;
2482		break;
2483	case VCAP_BIT_ANY:
2484		u1->value = 0;
2485		break;
2486	}
2487}
2488
2489/* Add a bit action with value to the rule */
2490int vcap_rule_add_action_bit(struct vcap_rule *rule,
2491			     enum vcap_action_field action,
2492			     enum vcap_bit val)
2493{
2494	struct vcap_client_actionfield_data data;
2495
2496	vcap_rule_set_action_bitsize(&data.u1, val);
2497	return vcap_rule_add_action(rule, action, VCAP_FIELD_BIT, &data);
2498}
2499EXPORT_SYMBOL_GPL(vcap_rule_add_action_bit);
2500
2501/* Add a 32 bit action field with value to the rule */
2502int vcap_rule_add_action_u32(struct vcap_rule *rule,
2503			     enum vcap_action_field action,
2504			     u32 value)
2505{
2506	struct vcap_client_actionfield_data data;
2507
2508	data.u32.value = value;
2509	return vcap_rule_add_action(rule, action, VCAP_FIELD_U32, &data);
2510}
2511EXPORT_SYMBOL_GPL(vcap_rule_add_action_u32);
2512
2513static int vcap_read_counter(struct vcap_rule_internal *ri,
2514			     struct vcap_counter *ctr)
2515{
2516	struct vcap_admin *admin = ri->admin;
2517
2518	ri->vctrl->ops->update(ri->ndev, admin, VCAP_CMD_READ, VCAP_SEL_COUNTER,
2519			       ri->addr);
2520	ri->vctrl->ops->cache_read(ri->ndev, admin, VCAP_SEL_COUNTER,
2521				   ri->counter_id, 0);
2522	ctr->value = admin->cache.counter;
2523	ctr->sticky = admin->cache.sticky;
2524	return 0;
2525}
2526
2527/* Copy to host byte order */
2528void vcap_netbytes_copy(u8 *dst, u8 *src, int count)
2529{
2530	int idx;
2531
2532	for (idx = 0; idx < count; ++idx, ++dst)
2533		*dst = src[count - idx - 1];
2534}
2535EXPORT_SYMBOL_GPL(vcap_netbytes_copy);
2536
2537/* Convert validation error code into tc extact error message */
2538void vcap_set_tc_exterr(struct flow_cls_offload *fco, struct vcap_rule *vrule)
2539{
2540	switch (vrule->exterr) {
2541	case VCAP_ERR_NONE:
2542		break;
2543	case VCAP_ERR_NO_ADMIN:
2544		NL_SET_ERR_MSG_MOD(fco->common.extack,
2545				   "Missing VCAP instance");
2546		break;
2547	case VCAP_ERR_NO_NETDEV:
2548		NL_SET_ERR_MSG_MOD(fco->common.extack,
2549				   "Missing network interface");
2550		break;
2551	case VCAP_ERR_NO_KEYSET_MATCH:
2552		NL_SET_ERR_MSG_MOD(fco->common.extack,
2553				   "No keyset matched the filter keys");
2554		break;
2555	case VCAP_ERR_NO_ACTIONSET_MATCH:
2556		NL_SET_ERR_MSG_MOD(fco->common.extack,
2557				   "No actionset matched the filter actions");
2558		break;
2559	case VCAP_ERR_NO_PORT_KEYSET_MATCH:
2560		NL_SET_ERR_MSG_MOD(fco->common.extack,
2561				   "No port keyset matched the filter keys");
2562		break;
2563	}
2564}
2565EXPORT_SYMBOL_GPL(vcap_set_tc_exterr);
2566
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2567/* Check if this port is already enabled for this VCAP instance */
2568static bool vcap_is_enabled(struct vcap_admin *admin, struct net_device *ndev,
2569			    unsigned long cookie)
2570{
2571	struct vcap_enabled_port *eport;
 
2572
2573	list_for_each_entry(eport, &admin->enabled, list)
2574		if (eport->cookie == cookie || eport->ndev == ndev)
2575			return true;
 
2576
2577	return false;
2578}
2579
2580/* Enable this port for this VCAP instance */
2581static int vcap_enable(struct vcap_admin *admin, struct net_device *ndev,
2582		       unsigned long cookie)
2583{
2584	struct vcap_enabled_port *eport;
 
 
 
 
 
 
 
 
2585
2586	eport = kzalloc(sizeof(*eport), GFP_KERNEL);
2587	if (!eport)
2588		return -ENOMEM;
2589
2590	eport->ndev = ndev;
2591	eport->cookie = cookie;
 
 
 
2592	list_add_tail(&eport->list, &admin->enabled);
 
 
 
 
 
 
 
 
2593
 
 
 
 
2594	return 0;
2595}
2596
2597/* Disable this port for this VCAP instance */
2598static int vcap_disable(struct vcap_admin *admin, struct net_device *ndev,
2599			unsigned long cookie)
2600{
2601	struct vcap_enabled_port *eport;
 
 
2602
2603	list_for_each_entry(eport, &admin->enabled, list) {
2604		if (eport->cookie == cookie && eport->ndev == ndev) {
2605			list_del(&eport->list);
2606			kfree(eport);
2607			return 0;
 
 
2608		}
 
 
2609	}
2610
2611	return -ENOENT;
2612}
2613
2614/* Find the VCAP instance that enabled the port using a specific filter */
2615static struct vcap_admin *vcap_find_admin_by_cookie(struct vcap_control *vctrl,
2616						    unsigned long cookie)
2617{
2618	struct vcap_enabled_port *eport;
2619	struct vcap_admin *admin;
2620
2621	list_for_each_entry(admin, &vctrl->list, list)
2622		list_for_each_entry(eport, &admin->enabled, list)
2623			if (eport->cookie == cookie)
2624				return admin;
2625
2626	return NULL;
 
 
 
 
2627}
2628
2629/* Enable/Disable the VCAP instance lookups. Chain id 0 means disable */
2630int vcap_enable_lookups(struct vcap_control *vctrl, struct net_device *ndev,
2631			int chain_id, unsigned long cookie, bool enable)
 
2632{
2633	struct vcap_admin *admin;
2634	int err;
2635
2636	err = vcap_api_check(vctrl);
2637	if (err)
2638		return err;
2639
2640	if (!ndev)
2641		return -ENODEV;
2642
2643	if (chain_id)
2644		admin = vcap_find_admin(vctrl, chain_id);
2645	else
2646		admin = vcap_find_admin_by_cookie(vctrl, cookie);
2647	if (!admin)
2648		return -ENOENT;
2649
2650	/* first instance and first chain */
2651	if (admin->vinst || chain_id > admin->first_cid)
2652		return -EFAULT;
2653
2654	err = vctrl->ops->enable(ndev, admin, enable);
2655	if (err)
2656		return err;
2657
2658	if (chain_id) {
2659		if (vcap_is_enabled(admin, ndev, cookie))
2660			return -EADDRINUSE;
2661		mutex_lock(&admin->lock);
2662		vcap_enable(admin, ndev, cookie);
 
2663	} else {
2664		mutex_lock(&admin->lock);
2665		vcap_disable(admin, ndev, cookie);
2666	}
2667	mutex_unlock(&admin->lock);
2668
2669	return 0;
2670}
2671EXPORT_SYMBOL_GPL(vcap_enable_lookups);
2672
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2673/* Set a rule counter id (for certain vcaps only) */
2674void vcap_rule_set_counter_id(struct vcap_rule *rule, u32 counter_id)
2675{
2676	struct vcap_rule_internal *ri = to_intrule(rule);
2677
2678	ri->counter_id = counter_id;
2679}
2680EXPORT_SYMBOL_GPL(vcap_rule_set_counter_id);
2681
2682/* Provide all rules via a callback interface */
2683int vcap_rule_iter(struct vcap_control *vctrl,
2684		   int (*callback)(void *, struct vcap_rule *), void *arg)
2685{
2686	struct vcap_rule_internal *ri;
2687	struct vcap_admin *admin;
2688	int ret;
2689
2690	ret = vcap_api_check(vctrl);
2691	if (ret)
2692		return ret;
2693
2694	/* Iterate all rules in each VCAP instance */
2695	list_for_each_entry(admin, &vctrl->list, list) {
2696		list_for_each_entry(ri, &admin->rules, list) {
2697			ret = callback(arg, &ri->data);
2698			if (ret)
2699				return ret;
2700		}
2701	}
2702
2703	return 0;
2704}
2705EXPORT_SYMBOL_GPL(vcap_rule_iter);
2706
2707int vcap_rule_set_counter(struct vcap_rule *rule, struct vcap_counter *ctr)
2708{
2709	struct vcap_rule_internal *ri = to_intrule(rule);
2710	int err;
2711
2712	err = vcap_api_check(ri->vctrl);
2713	if (err)
2714		return err;
2715	if (!ctr) {
2716		pr_err("%s:%d: counter is missing\n", __func__, __LINE__);
2717		return -EINVAL;
2718	}
2719	return vcap_write_counter(ri, ctr);
 
 
 
 
 
2720}
2721EXPORT_SYMBOL_GPL(vcap_rule_set_counter);
2722
2723int vcap_rule_get_counter(struct vcap_rule *rule, struct vcap_counter *ctr)
2724{
2725	struct vcap_rule_internal *ri = to_intrule(rule);
2726	int err;
2727
2728	err = vcap_api_check(ri->vctrl);
2729	if (err)
2730		return err;
2731	if (!ctr) {
2732		pr_err("%s:%d: counter is missing\n", __func__, __LINE__);
2733		return -EINVAL;
2734	}
2735	return vcap_read_counter(ri, ctr);
 
 
 
 
 
2736}
2737EXPORT_SYMBOL_GPL(vcap_rule_get_counter);
2738
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2739static int vcap_rule_mod_key(struct vcap_rule *rule,
2740			     enum vcap_key_field key,
2741			     enum vcap_field_type ftype,
2742			     struct vcap_client_keyfield_data *data)
2743{
2744	struct vcap_client_keyfield *field;
2745
2746	field = vcap_find_keyfield(rule, key);
2747	if (!field)
2748		return vcap_rule_add_key(rule, key, ftype, data);
2749	vcap_copy_from_client_keyfield(rule, field, data);
2750	return 0;
2751}
2752
2753/* Modify a 32 bit key field with value and mask in the rule */
2754int vcap_rule_mod_key_u32(struct vcap_rule *rule, enum vcap_key_field key,
2755			  u32 value, u32 mask)
2756{
2757	struct vcap_client_keyfield_data data;
2758
2759	data.u32.value = value;
2760	data.u32.mask = mask;
2761	return vcap_rule_mod_key(rule, key, VCAP_FIELD_U32, &data);
2762}
2763EXPORT_SYMBOL_GPL(vcap_rule_mod_key_u32);
2764
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2765static int vcap_rule_mod_action(struct vcap_rule *rule,
2766				enum vcap_action_field action,
2767				enum vcap_field_type ftype,
2768				struct vcap_client_actionfield_data *data)
2769{
2770	struct vcap_client_actionfield *field;
2771
2772	field = vcap_find_actionfield(rule, action);
2773	if (!field)
2774		return vcap_rule_add_action(rule, action, ftype, data);
2775	vcap_copy_from_client_actionfield(rule, field, data);
2776	return 0;
2777}
2778
2779/* Modify a 32 bit action field with value in the rule */
2780int vcap_rule_mod_action_u32(struct vcap_rule *rule,
2781			     enum vcap_action_field action,
2782			     u32 value)
2783{
2784	struct vcap_client_actionfield_data data;
2785
2786	data.u32.value = value;
2787	return vcap_rule_mod_action(rule, action, VCAP_FIELD_U32, &data);
2788}
2789EXPORT_SYMBOL_GPL(vcap_rule_mod_action_u32);
2790
2791/* Drop keys in a keylist and any keys that are not supported by the keyset */
2792int vcap_filter_rule_keys(struct vcap_rule *rule,
2793			  enum vcap_key_field keylist[], int length,
2794			  bool drop_unsupported)
2795{
2796	struct vcap_rule_internal *ri = to_intrule(rule);
2797	struct vcap_client_keyfield *ckf, *next_ckf;
2798	const struct vcap_field *fields;
2799	enum vcap_key_field key;
2800	int err = 0;
2801	int idx;
2802
2803	if (length > 0) {
2804		err = -EEXIST;
2805		list_for_each_entry_safe(ckf, next_ckf,
2806					 &ri->data.keyfields, ctrl.list) {
2807			key = ckf->ctrl.key;
2808			for (idx = 0; idx < length; ++idx)
2809				if (key == keylist[idx]) {
2810					list_del(&ckf->ctrl.list);
2811					kfree(ckf);
2812					idx++;
2813					err = 0;
2814				}
2815		}
2816	}
2817	if (drop_unsupported) {
2818		err = -EEXIST;
2819		fields = vcap_keyfields(ri->vctrl, ri->admin->vtype,
2820					rule->keyset);
2821		if (!fields)
2822			return err;
2823		list_for_each_entry_safe(ckf, next_ckf,
2824					 &ri->data.keyfields, ctrl.list) {
2825			key = ckf->ctrl.key;
2826			if (fields[key].width == 0) {
2827				list_del(&ckf->ctrl.list);
2828				kfree(ckf);
2829				err = 0;
2830			}
2831		}
2832	}
2833	return err;
2834}
2835EXPORT_SYMBOL_GPL(vcap_filter_rule_keys);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2836
2837/* Make a full copy of an existing rule with a new rule id */
2838struct vcap_rule *vcap_copy_rule(struct vcap_rule *erule)
2839{
2840	struct vcap_rule_internal *ri = to_intrule(erule);
2841	struct vcap_client_actionfield *caf;
2842	struct vcap_client_keyfield *ckf;
2843	struct vcap_rule *rule;
2844	int err;
2845
2846	err = vcap_api_check(ri->vctrl);
2847	if (err)
2848		return ERR_PTR(err);
2849
2850	rule = vcap_alloc_rule(ri->vctrl, ri->ndev, ri->data.vcap_chain_id,
2851			       ri->data.user, ri->data.priority, 0);
2852	if (IS_ERR(rule))
2853		return rule;
2854
2855	list_for_each_entry(ckf, &ri->data.keyfields, ctrl.list) {
2856		/* Add a key duplicate in the new rule */
2857		err = vcap_rule_add_key(rule,
2858					ckf->ctrl.key,
2859					ckf->ctrl.type,
2860					&ckf->data);
2861		if (err)
2862			goto err;
2863	}
2864
2865	list_for_each_entry(caf, &ri->data.actionfields, ctrl.list) {
2866		/* Add a action duplicate in the new rule */
2867		err = vcap_rule_add_action(rule,
2868					   caf->ctrl.action,
2869					   caf->ctrl.type,
2870					   &caf->data);
2871		if (err)
2872			goto err;
2873	}
2874	return rule;
2875err:
2876	vcap_free_rule(rule);
2877	return ERR_PTR(err);
2878}
2879EXPORT_SYMBOL_GPL(vcap_copy_rule);
2880
2881#ifdef CONFIG_VCAP_KUNIT_TEST
2882#include "vcap_api_kunit.c"
2883#endif