162306a36Sopenharmony_ci// SPDX-License-Identifier: (GPL-2.0 OR MIT)
262306a36Sopenharmony_ci/* Microsemi Ocelot Switch driver
362306a36Sopenharmony_ci * Copyright (c) 2019 Microsemi Corporation
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#include <linux/iopoll.h>
762306a36Sopenharmony_ci#include <linux/proc_fs.h>
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include <soc/mscc/ocelot_vcap.h>
1062306a36Sopenharmony_ci#include "ocelot_police.h"
1162306a36Sopenharmony_ci#include "ocelot_vcap.h"
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#define ENTRY_WIDTH 32
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_cienum vcap_sel {
1662306a36Sopenharmony_ci	VCAP_SEL_ENTRY = 0x1,
1762306a36Sopenharmony_ci	VCAP_SEL_ACTION = 0x2,
1862306a36Sopenharmony_ci	VCAP_SEL_COUNTER = 0x4,
1962306a36Sopenharmony_ci	VCAP_SEL_ALL = 0x7,
2062306a36Sopenharmony_ci};
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_cienum vcap_cmd {
2362306a36Sopenharmony_ci	VCAP_CMD_WRITE = 0, /* Copy from Cache to TCAM */
2462306a36Sopenharmony_ci	VCAP_CMD_READ = 1, /* Copy from TCAM to Cache */
2562306a36Sopenharmony_ci	VCAP_CMD_MOVE_UP = 2, /* Move <count> up */
2662306a36Sopenharmony_ci	VCAP_CMD_MOVE_DOWN = 3, /* Move <count> down */
2762306a36Sopenharmony_ci	VCAP_CMD_INITIALIZE = 4, /* Write all (from cache) */
2862306a36Sopenharmony_ci};
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci#define VCAP_ENTRY_WIDTH 12 /* Max entry width (32bit words) */
3162306a36Sopenharmony_ci#define VCAP_COUNTER_WIDTH 4 /* Max counter width (32bit words) */
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_cistruct vcap_data {
3462306a36Sopenharmony_ci	u32 entry[VCAP_ENTRY_WIDTH]; /* ENTRY_DAT */
3562306a36Sopenharmony_ci	u32 mask[VCAP_ENTRY_WIDTH]; /* MASK_DAT */
3662306a36Sopenharmony_ci	u32 action[VCAP_ENTRY_WIDTH]; /* ACTION_DAT */
3762306a36Sopenharmony_ci	u32 counter[VCAP_COUNTER_WIDTH]; /* CNT_DAT */
3862306a36Sopenharmony_ci	u32 tg; /* TG_DAT */
3962306a36Sopenharmony_ci	u32 type; /* Action type */
4062306a36Sopenharmony_ci	u32 tg_sw; /* Current type-group */
4162306a36Sopenharmony_ci	u32 cnt; /* Current counter */
4262306a36Sopenharmony_ci	u32 key_offset; /* Current entry offset */
4362306a36Sopenharmony_ci	u32 action_offset; /* Current action offset */
4462306a36Sopenharmony_ci	u32 counter_offset; /* Current counter offset */
4562306a36Sopenharmony_ci	u32 tg_value; /* Current type-group value */
4662306a36Sopenharmony_ci	u32 tg_mask; /* Current type-group mask */
4762306a36Sopenharmony_ci};
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_cistatic u32 vcap_read_update_ctrl(struct ocelot *ocelot,
5062306a36Sopenharmony_ci				 const struct vcap_props *vcap)
5162306a36Sopenharmony_ci{
5262306a36Sopenharmony_ci	return ocelot_target_read(ocelot, vcap->target, VCAP_CORE_UPDATE_CTRL);
5362306a36Sopenharmony_ci}
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_cistatic void vcap_cmd(struct ocelot *ocelot, const struct vcap_props *vcap,
5662306a36Sopenharmony_ci		     u16 ix, int cmd, int sel)
5762306a36Sopenharmony_ci{
5862306a36Sopenharmony_ci	u32 value = (VCAP_CORE_UPDATE_CTRL_UPDATE_CMD(cmd) |
5962306a36Sopenharmony_ci		     VCAP_CORE_UPDATE_CTRL_UPDATE_ADDR(ix) |
6062306a36Sopenharmony_ci		     VCAP_CORE_UPDATE_CTRL_UPDATE_SHOT);
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci	if ((sel & VCAP_SEL_ENTRY) && ix >= vcap->entry_count)
6362306a36Sopenharmony_ci		return;
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci	if (!(sel & VCAP_SEL_ENTRY))
6662306a36Sopenharmony_ci		value |= VCAP_CORE_UPDATE_CTRL_UPDATE_ENTRY_DIS;
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci	if (!(sel & VCAP_SEL_ACTION))
6962306a36Sopenharmony_ci		value |= VCAP_CORE_UPDATE_CTRL_UPDATE_ACTION_DIS;
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci	if (!(sel & VCAP_SEL_COUNTER))
7262306a36Sopenharmony_ci		value |= VCAP_CORE_UPDATE_CTRL_UPDATE_CNT_DIS;
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci	ocelot_target_write(ocelot, vcap->target, value, VCAP_CORE_UPDATE_CTRL);
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci	read_poll_timeout(vcap_read_update_ctrl, value,
7762306a36Sopenharmony_ci			  (value & VCAP_CORE_UPDATE_CTRL_UPDATE_SHOT) == 0,
7862306a36Sopenharmony_ci			  10, 100000, false, ocelot, vcap);
7962306a36Sopenharmony_ci}
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci/* Convert from 0-based row to VCAP entry row and run command */
8262306a36Sopenharmony_cistatic void vcap_row_cmd(struct ocelot *ocelot, const struct vcap_props *vcap,
8362306a36Sopenharmony_ci			 u32 row, int cmd, int sel)
8462306a36Sopenharmony_ci{
8562306a36Sopenharmony_ci	vcap_cmd(ocelot, vcap, vcap->entry_count - row - 1, cmd, sel);
8662306a36Sopenharmony_ci}
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_cistatic void vcap_entry2cache(struct ocelot *ocelot,
8962306a36Sopenharmony_ci			     const struct vcap_props *vcap,
9062306a36Sopenharmony_ci			     struct vcap_data *data)
9162306a36Sopenharmony_ci{
9262306a36Sopenharmony_ci	u32 entry_words, i;
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci	entry_words = DIV_ROUND_UP(vcap->entry_width, ENTRY_WIDTH);
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci	for (i = 0; i < entry_words; i++) {
9762306a36Sopenharmony_ci		ocelot_target_write_rix(ocelot, vcap->target, data->entry[i],
9862306a36Sopenharmony_ci					VCAP_CACHE_ENTRY_DAT, i);
9962306a36Sopenharmony_ci		ocelot_target_write_rix(ocelot, vcap->target, ~data->mask[i],
10062306a36Sopenharmony_ci					VCAP_CACHE_MASK_DAT, i);
10162306a36Sopenharmony_ci	}
10262306a36Sopenharmony_ci	ocelot_target_write(ocelot, vcap->target, data->tg, VCAP_CACHE_TG_DAT);
10362306a36Sopenharmony_ci}
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_cistatic void vcap_cache2entry(struct ocelot *ocelot,
10662306a36Sopenharmony_ci			     const struct vcap_props *vcap,
10762306a36Sopenharmony_ci			     struct vcap_data *data)
10862306a36Sopenharmony_ci{
10962306a36Sopenharmony_ci	u32 entry_words, i;
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci	entry_words = DIV_ROUND_UP(vcap->entry_width, ENTRY_WIDTH);
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci	for (i = 0; i < entry_words; i++) {
11462306a36Sopenharmony_ci		data->entry[i] = ocelot_target_read_rix(ocelot, vcap->target,
11562306a36Sopenharmony_ci							VCAP_CACHE_ENTRY_DAT, i);
11662306a36Sopenharmony_ci		// Invert mask
11762306a36Sopenharmony_ci		data->mask[i] = ~ocelot_target_read_rix(ocelot, vcap->target,
11862306a36Sopenharmony_ci							VCAP_CACHE_MASK_DAT, i);
11962306a36Sopenharmony_ci	}
12062306a36Sopenharmony_ci	data->tg = ocelot_target_read(ocelot, vcap->target, VCAP_CACHE_TG_DAT);
12162306a36Sopenharmony_ci}
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_cistatic void vcap_action2cache(struct ocelot *ocelot,
12462306a36Sopenharmony_ci			      const struct vcap_props *vcap,
12562306a36Sopenharmony_ci			      struct vcap_data *data)
12662306a36Sopenharmony_ci{
12762306a36Sopenharmony_ci	u32 action_words, mask;
12862306a36Sopenharmony_ci	int i, width;
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci	/* Encode action type */
13162306a36Sopenharmony_ci	width = vcap->action_type_width;
13262306a36Sopenharmony_ci	if (width) {
13362306a36Sopenharmony_ci		mask = GENMASK(width, 0);
13462306a36Sopenharmony_ci		data->action[0] = ((data->action[0] & ~mask) | data->type);
13562306a36Sopenharmony_ci	}
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci	action_words = DIV_ROUND_UP(vcap->action_width, ENTRY_WIDTH);
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci	for (i = 0; i < action_words; i++)
14062306a36Sopenharmony_ci		ocelot_target_write_rix(ocelot, vcap->target, data->action[i],
14162306a36Sopenharmony_ci					VCAP_CACHE_ACTION_DAT, i);
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci	for (i = 0; i < vcap->counter_words; i++)
14462306a36Sopenharmony_ci		ocelot_target_write_rix(ocelot, vcap->target, data->counter[i],
14562306a36Sopenharmony_ci					VCAP_CACHE_CNT_DAT, i);
14662306a36Sopenharmony_ci}
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_cistatic void vcap_cache2action(struct ocelot *ocelot,
14962306a36Sopenharmony_ci			      const struct vcap_props *vcap,
15062306a36Sopenharmony_ci			      struct vcap_data *data)
15162306a36Sopenharmony_ci{
15262306a36Sopenharmony_ci	u32 action_words;
15362306a36Sopenharmony_ci	int i, width;
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci	action_words = DIV_ROUND_UP(vcap->action_width, ENTRY_WIDTH);
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci	for (i = 0; i < action_words; i++)
15862306a36Sopenharmony_ci		data->action[i] = ocelot_target_read_rix(ocelot, vcap->target,
15962306a36Sopenharmony_ci							 VCAP_CACHE_ACTION_DAT,
16062306a36Sopenharmony_ci							 i);
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci	for (i = 0; i < vcap->counter_words; i++)
16362306a36Sopenharmony_ci		data->counter[i] = ocelot_target_read_rix(ocelot, vcap->target,
16462306a36Sopenharmony_ci							  VCAP_CACHE_CNT_DAT,
16562306a36Sopenharmony_ci							  i);
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci	/* Extract action type */
16862306a36Sopenharmony_ci	width = vcap->action_type_width;
16962306a36Sopenharmony_ci	data->type = (width ? (data->action[0] & GENMASK(width, 0)) : 0);
17062306a36Sopenharmony_ci}
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci/* Calculate offsets for entry */
17362306a36Sopenharmony_cistatic void vcap_data_offset_get(const struct vcap_props *vcap,
17462306a36Sopenharmony_ci				 struct vcap_data *data, int ix)
17562306a36Sopenharmony_ci{
17662306a36Sopenharmony_ci	int num_subwords_per_entry, num_subwords_per_action;
17762306a36Sopenharmony_ci	int i, col, offset, num_entries_per_row, base;
17862306a36Sopenharmony_ci	u32 width = vcap->tg_width;
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci	switch (data->tg_sw) {
18162306a36Sopenharmony_ci	case VCAP_TG_FULL:
18262306a36Sopenharmony_ci		num_entries_per_row = 1;
18362306a36Sopenharmony_ci		break;
18462306a36Sopenharmony_ci	case VCAP_TG_HALF:
18562306a36Sopenharmony_ci		num_entries_per_row = 2;
18662306a36Sopenharmony_ci		break;
18762306a36Sopenharmony_ci	case VCAP_TG_QUARTER:
18862306a36Sopenharmony_ci		num_entries_per_row = 4;
18962306a36Sopenharmony_ci		break;
19062306a36Sopenharmony_ci	default:
19162306a36Sopenharmony_ci		return;
19262306a36Sopenharmony_ci	}
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_ci	col = (ix % num_entries_per_row);
19562306a36Sopenharmony_ci	num_subwords_per_entry = (vcap->sw_count / num_entries_per_row);
19662306a36Sopenharmony_ci	base = (vcap->sw_count - col * num_subwords_per_entry -
19762306a36Sopenharmony_ci		num_subwords_per_entry);
19862306a36Sopenharmony_ci	data->tg_value = 0;
19962306a36Sopenharmony_ci	data->tg_mask = 0;
20062306a36Sopenharmony_ci	for (i = 0; i < num_subwords_per_entry; i++) {
20162306a36Sopenharmony_ci		offset = ((base + i) * width);
20262306a36Sopenharmony_ci		data->tg_value |= (data->tg_sw << offset);
20362306a36Sopenharmony_ci		data->tg_mask |= GENMASK(offset + width - 1, offset);
20462306a36Sopenharmony_ci	}
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ci	/* Calculate key/action/counter offsets */
20762306a36Sopenharmony_ci	col = (num_entries_per_row - col - 1);
20862306a36Sopenharmony_ci	data->key_offset = (base * vcap->entry_width) / vcap->sw_count;
20962306a36Sopenharmony_ci	data->counter_offset = (num_subwords_per_entry * col *
21062306a36Sopenharmony_ci				vcap->counter_width);
21162306a36Sopenharmony_ci	i = data->type;
21262306a36Sopenharmony_ci	width = vcap->action_table[i].width;
21362306a36Sopenharmony_ci	num_subwords_per_action = vcap->action_table[i].count;
21462306a36Sopenharmony_ci	data->action_offset = ((num_subwords_per_action * col * width) /
21562306a36Sopenharmony_ci				num_entries_per_row);
21662306a36Sopenharmony_ci	data->action_offset += vcap->action_type_width;
21762306a36Sopenharmony_ci}
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_cistatic void vcap_data_set(u32 *data, u32 offset, u32 len, u32 value)
22062306a36Sopenharmony_ci{
22162306a36Sopenharmony_ci	u32 i, v, m;
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ci	for (i = 0; i < len; i++, offset++) {
22462306a36Sopenharmony_ci		v = data[offset / ENTRY_WIDTH];
22562306a36Sopenharmony_ci		m = (1 << (offset % ENTRY_WIDTH));
22662306a36Sopenharmony_ci		if (value & (1 << i))
22762306a36Sopenharmony_ci			v |= m;
22862306a36Sopenharmony_ci		else
22962306a36Sopenharmony_ci			v &= ~m;
23062306a36Sopenharmony_ci		data[offset / ENTRY_WIDTH] = v;
23162306a36Sopenharmony_ci	}
23262306a36Sopenharmony_ci}
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_cistatic u32 vcap_data_get(u32 *data, u32 offset, u32 len)
23562306a36Sopenharmony_ci{
23662306a36Sopenharmony_ci	u32 i, v, m, value = 0;
23762306a36Sopenharmony_ci
23862306a36Sopenharmony_ci	for (i = 0; i < len; i++, offset++) {
23962306a36Sopenharmony_ci		v = data[offset / ENTRY_WIDTH];
24062306a36Sopenharmony_ci		m = (1 << (offset % ENTRY_WIDTH));
24162306a36Sopenharmony_ci		if (v & m)
24262306a36Sopenharmony_ci			value |= (1 << i);
24362306a36Sopenharmony_ci	}
24462306a36Sopenharmony_ci	return value;
24562306a36Sopenharmony_ci}
24662306a36Sopenharmony_ci
24762306a36Sopenharmony_cistatic void vcap_key_field_set(struct vcap_data *data, u32 offset, u32 width,
24862306a36Sopenharmony_ci			       u32 value, u32 mask)
24962306a36Sopenharmony_ci{
25062306a36Sopenharmony_ci	vcap_data_set(data->entry, offset + data->key_offset, width, value);
25162306a36Sopenharmony_ci	vcap_data_set(data->mask, offset + data->key_offset, width, mask);
25262306a36Sopenharmony_ci}
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_cistatic void vcap_key_set(const struct vcap_props *vcap, struct vcap_data *data,
25562306a36Sopenharmony_ci			 int field, u32 value, u32 mask)
25662306a36Sopenharmony_ci{
25762306a36Sopenharmony_ci	u32 offset = vcap->keys[field].offset;
25862306a36Sopenharmony_ci	u32 length = vcap->keys[field].length;
25962306a36Sopenharmony_ci
26062306a36Sopenharmony_ci	vcap_key_field_set(data, offset, length, value, mask);
26162306a36Sopenharmony_ci}
26262306a36Sopenharmony_ci
26362306a36Sopenharmony_cistatic void vcap_key_bytes_set(const struct vcap_props *vcap,
26462306a36Sopenharmony_ci			       struct vcap_data *data, int field,
26562306a36Sopenharmony_ci			       u8 *val, u8 *msk)
26662306a36Sopenharmony_ci{
26762306a36Sopenharmony_ci	u32 offset = vcap->keys[field].offset;
26862306a36Sopenharmony_ci	u32 count  = vcap->keys[field].length;
26962306a36Sopenharmony_ci	u32 i, j, n = 0, value = 0, mask = 0;
27062306a36Sopenharmony_ci
27162306a36Sopenharmony_ci	WARN_ON(count % 8);
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_ci	/* Data wider than 32 bits are split up in chunks of maximum 32 bits.
27462306a36Sopenharmony_ci	 * The 32 LSB of the data are written to the 32 MSB of the TCAM.
27562306a36Sopenharmony_ci	 */
27662306a36Sopenharmony_ci	offset += count;
27762306a36Sopenharmony_ci	count /= 8;
27862306a36Sopenharmony_ci
27962306a36Sopenharmony_ci	for (i = 0; i < count; i++) {
28062306a36Sopenharmony_ci		j = (count - i - 1);
28162306a36Sopenharmony_ci		value += (val[j] << n);
28262306a36Sopenharmony_ci		mask += (msk[j] << n);
28362306a36Sopenharmony_ci		n += 8;
28462306a36Sopenharmony_ci		if (n == ENTRY_WIDTH || (i + 1) == count) {
28562306a36Sopenharmony_ci			offset -= n;
28662306a36Sopenharmony_ci			vcap_key_field_set(data, offset, n, value, mask);
28762306a36Sopenharmony_ci			n = 0;
28862306a36Sopenharmony_ci			value = 0;
28962306a36Sopenharmony_ci			mask = 0;
29062306a36Sopenharmony_ci		}
29162306a36Sopenharmony_ci	}
29262306a36Sopenharmony_ci}
29362306a36Sopenharmony_ci
29462306a36Sopenharmony_cistatic void vcap_key_l4_port_set(const struct vcap_props *vcap,
29562306a36Sopenharmony_ci				 struct vcap_data *data, int field,
29662306a36Sopenharmony_ci				 struct ocelot_vcap_udp_tcp *port)
29762306a36Sopenharmony_ci{
29862306a36Sopenharmony_ci	u32 offset = vcap->keys[field].offset;
29962306a36Sopenharmony_ci	u32 length = vcap->keys[field].length;
30062306a36Sopenharmony_ci
30162306a36Sopenharmony_ci	WARN_ON(length != 16);
30262306a36Sopenharmony_ci
30362306a36Sopenharmony_ci	vcap_key_field_set(data, offset, length, port->value, port->mask);
30462306a36Sopenharmony_ci}
30562306a36Sopenharmony_ci
30662306a36Sopenharmony_cistatic void vcap_key_bit_set(const struct vcap_props *vcap,
30762306a36Sopenharmony_ci			     struct vcap_data *data, int field,
30862306a36Sopenharmony_ci			     enum ocelot_vcap_bit val)
30962306a36Sopenharmony_ci{
31062306a36Sopenharmony_ci	u32 value = (val == OCELOT_VCAP_BIT_1 ? 1 : 0);
31162306a36Sopenharmony_ci	u32 msk = (val == OCELOT_VCAP_BIT_ANY ? 0 : 1);
31262306a36Sopenharmony_ci	u32 offset = vcap->keys[field].offset;
31362306a36Sopenharmony_ci	u32 length = vcap->keys[field].length;
31462306a36Sopenharmony_ci
31562306a36Sopenharmony_ci	WARN_ON(length != 1);
31662306a36Sopenharmony_ci
31762306a36Sopenharmony_ci	vcap_key_field_set(data, offset, length, value, msk);
31862306a36Sopenharmony_ci}
31962306a36Sopenharmony_ci
32062306a36Sopenharmony_cistatic void vcap_action_set(const struct vcap_props *vcap,
32162306a36Sopenharmony_ci			    struct vcap_data *data, int field, u32 value)
32262306a36Sopenharmony_ci{
32362306a36Sopenharmony_ci	int offset = vcap->actions[field].offset;
32462306a36Sopenharmony_ci	int length = vcap->actions[field].length;
32562306a36Sopenharmony_ci
32662306a36Sopenharmony_ci	vcap_data_set(data->action, offset + data->action_offset, length,
32762306a36Sopenharmony_ci		      value);
32862306a36Sopenharmony_ci}
32962306a36Sopenharmony_ci
33062306a36Sopenharmony_cistatic void is2_action_set(struct ocelot *ocelot, struct vcap_data *data,
33162306a36Sopenharmony_ci			   struct ocelot_vcap_filter *filter)
33262306a36Sopenharmony_ci{
33362306a36Sopenharmony_ci	const struct vcap_props *vcap = &ocelot->vcap[VCAP_IS2];
33462306a36Sopenharmony_ci	struct ocelot_vcap_action *a = &filter->action;
33562306a36Sopenharmony_ci
33662306a36Sopenharmony_ci	vcap_action_set(vcap, data, VCAP_IS2_ACT_MASK_MODE, a->mask_mode);
33762306a36Sopenharmony_ci	vcap_action_set(vcap, data, VCAP_IS2_ACT_PORT_MASK, a->port_mask);
33862306a36Sopenharmony_ci	vcap_action_set(vcap, data, VCAP_IS2_ACT_MIRROR_ENA, a->mirror_ena);
33962306a36Sopenharmony_ci	vcap_action_set(vcap, data, VCAP_IS2_ACT_POLICE_ENA, a->police_ena);
34062306a36Sopenharmony_ci	vcap_action_set(vcap, data, VCAP_IS2_ACT_POLICE_IDX, a->pol_ix);
34162306a36Sopenharmony_ci	vcap_action_set(vcap, data, VCAP_IS2_ACT_CPU_QU_NUM, a->cpu_qu_num);
34262306a36Sopenharmony_ci	vcap_action_set(vcap, data, VCAP_IS2_ACT_CPU_COPY_ENA, a->cpu_copy_ena);
34362306a36Sopenharmony_ci}
34462306a36Sopenharmony_ci
34562306a36Sopenharmony_cistatic void is2_entry_set(struct ocelot *ocelot, int ix,
34662306a36Sopenharmony_ci			  struct ocelot_vcap_filter *filter)
34762306a36Sopenharmony_ci{
34862306a36Sopenharmony_ci	const struct vcap_props *vcap = &ocelot->vcap[VCAP_IS2];
34962306a36Sopenharmony_ci	struct ocelot_vcap_key_vlan *tag = &filter->vlan;
35062306a36Sopenharmony_ci	u32 val, msk, type, type_mask = 0xf, i, count;
35162306a36Sopenharmony_ci	struct ocelot_vcap_u64 payload;
35262306a36Sopenharmony_ci	struct vcap_data data;
35362306a36Sopenharmony_ci	int row = (ix / 2);
35462306a36Sopenharmony_ci
35562306a36Sopenharmony_ci	memset(&payload, 0, sizeof(payload));
35662306a36Sopenharmony_ci	memset(&data, 0, sizeof(data));
35762306a36Sopenharmony_ci
35862306a36Sopenharmony_ci	/* Read row */
35962306a36Sopenharmony_ci	vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_READ, VCAP_SEL_ALL);
36062306a36Sopenharmony_ci	vcap_cache2entry(ocelot, vcap, &data);
36162306a36Sopenharmony_ci	vcap_cache2action(ocelot, vcap, &data);
36262306a36Sopenharmony_ci
36362306a36Sopenharmony_ci	data.tg_sw = VCAP_TG_HALF;
36462306a36Sopenharmony_ci	vcap_data_offset_get(vcap, &data, ix);
36562306a36Sopenharmony_ci	data.tg = (data.tg & ~data.tg_mask);
36662306a36Sopenharmony_ci	if (filter->prio != 0)
36762306a36Sopenharmony_ci		data.tg |= data.tg_value;
36862306a36Sopenharmony_ci
36962306a36Sopenharmony_ci	data.type = IS2_ACTION_TYPE_NORMAL;
37062306a36Sopenharmony_ci
37162306a36Sopenharmony_ci	vcap_key_set(vcap, &data, VCAP_IS2_HK_PAG, filter->pag, 0xff);
37262306a36Sopenharmony_ci	vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_FIRST,
37362306a36Sopenharmony_ci			 (filter->lookup == 0) ? OCELOT_VCAP_BIT_1 :
37462306a36Sopenharmony_ci			 OCELOT_VCAP_BIT_0);
37562306a36Sopenharmony_ci	vcap_key_set(vcap, &data, VCAP_IS2_HK_IGR_PORT_MASK, 0,
37662306a36Sopenharmony_ci		     ~filter->ingress_port_mask);
37762306a36Sopenharmony_ci	vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_HOST_MATCH,
37862306a36Sopenharmony_ci			 OCELOT_VCAP_BIT_ANY);
37962306a36Sopenharmony_ci	vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L2_MC, filter->dmac_mc);
38062306a36Sopenharmony_ci	vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L2_BC, filter->dmac_bc);
38162306a36Sopenharmony_ci	vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_VLAN_TAGGED, tag->tagged);
38262306a36Sopenharmony_ci	vcap_key_set(vcap, &data, VCAP_IS2_HK_VID,
38362306a36Sopenharmony_ci		     tag->vid.value, tag->vid.mask);
38462306a36Sopenharmony_ci	vcap_key_set(vcap, &data, VCAP_IS2_HK_PCP,
38562306a36Sopenharmony_ci		     tag->pcp.value[0], tag->pcp.mask[0]);
38662306a36Sopenharmony_ci	vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_DEI, tag->dei);
38762306a36Sopenharmony_ci
38862306a36Sopenharmony_ci	switch (filter->key_type) {
38962306a36Sopenharmony_ci	case OCELOT_VCAP_KEY_ETYPE: {
39062306a36Sopenharmony_ci		struct ocelot_vcap_key_etype *etype = &filter->key.etype;
39162306a36Sopenharmony_ci
39262306a36Sopenharmony_ci		type = IS2_TYPE_ETYPE;
39362306a36Sopenharmony_ci		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_DMAC,
39462306a36Sopenharmony_ci				   etype->dmac.value, etype->dmac.mask);
39562306a36Sopenharmony_ci		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_SMAC,
39662306a36Sopenharmony_ci				   etype->smac.value, etype->smac.mask);
39762306a36Sopenharmony_ci		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_MAC_ETYPE_ETYPE,
39862306a36Sopenharmony_ci				   etype->etype.value, etype->etype.mask);
39962306a36Sopenharmony_ci		/* Clear unused bits */
40062306a36Sopenharmony_ci		vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD0,
40162306a36Sopenharmony_ci			     0, 0);
40262306a36Sopenharmony_ci		vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD1,
40362306a36Sopenharmony_ci			     0, 0);
40462306a36Sopenharmony_ci		vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD2,
40562306a36Sopenharmony_ci			     0, 0);
40662306a36Sopenharmony_ci		vcap_key_bytes_set(vcap, &data,
40762306a36Sopenharmony_ci				   VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD0,
40862306a36Sopenharmony_ci				   etype->data.value, etype->data.mask);
40962306a36Sopenharmony_ci		break;
41062306a36Sopenharmony_ci	}
41162306a36Sopenharmony_ci	case OCELOT_VCAP_KEY_LLC: {
41262306a36Sopenharmony_ci		struct ocelot_vcap_key_llc *llc = &filter->key.llc;
41362306a36Sopenharmony_ci
41462306a36Sopenharmony_ci		type = IS2_TYPE_LLC;
41562306a36Sopenharmony_ci		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_DMAC,
41662306a36Sopenharmony_ci				   llc->dmac.value, llc->dmac.mask);
41762306a36Sopenharmony_ci		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_SMAC,
41862306a36Sopenharmony_ci				   llc->smac.value, llc->smac.mask);
41962306a36Sopenharmony_ci		for (i = 0; i < 4; i++) {
42062306a36Sopenharmony_ci			payload.value[i] = llc->llc.value[i];
42162306a36Sopenharmony_ci			payload.mask[i] = llc->llc.mask[i];
42262306a36Sopenharmony_ci		}
42362306a36Sopenharmony_ci		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_MAC_LLC_L2_LLC,
42462306a36Sopenharmony_ci				   payload.value, payload.mask);
42562306a36Sopenharmony_ci		break;
42662306a36Sopenharmony_ci	}
42762306a36Sopenharmony_ci	case OCELOT_VCAP_KEY_SNAP: {
42862306a36Sopenharmony_ci		struct ocelot_vcap_key_snap *snap = &filter->key.snap;
42962306a36Sopenharmony_ci
43062306a36Sopenharmony_ci		type = IS2_TYPE_SNAP;
43162306a36Sopenharmony_ci		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_DMAC,
43262306a36Sopenharmony_ci				   snap->dmac.value, snap->dmac.mask);
43362306a36Sopenharmony_ci		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_SMAC,
43462306a36Sopenharmony_ci				   snap->smac.value, snap->smac.mask);
43562306a36Sopenharmony_ci		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_MAC_SNAP_L2_SNAP,
43662306a36Sopenharmony_ci				   filter->key.snap.snap.value,
43762306a36Sopenharmony_ci				   filter->key.snap.snap.mask);
43862306a36Sopenharmony_ci		break;
43962306a36Sopenharmony_ci	}
44062306a36Sopenharmony_ci	case OCELOT_VCAP_KEY_ARP: {
44162306a36Sopenharmony_ci		struct ocelot_vcap_key_arp *arp = &filter->key.arp;
44262306a36Sopenharmony_ci
44362306a36Sopenharmony_ci		type = IS2_TYPE_ARP;
44462306a36Sopenharmony_ci		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_MAC_ARP_SMAC,
44562306a36Sopenharmony_ci				   arp->smac.value, arp->smac.mask);
44662306a36Sopenharmony_ci		vcap_key_bit_set(vcap, &data,
44762306a36Sopenharmony_ci				 VCAP_IS2_HK_MAC_ARP_ADDR_SPACE_OK,
44862306a36Sopenharmony_ci				 arp->ethernet);
44962306a36Sopenharmony_ci		vcap_key_bit_set(vcap, &data,
45062306a36Sopenharmony_ci				 VCAP_IS2_HK_MAC_ARP_PROTO_SPACE_OK,
45162306a36Sopenharmony_ci				 arp->ip);
45262306a36Sopenharmony_ci		vcap_key_bit_set(vcap, &data,
45362306a36Sopenharmony_ci				 VCAP_IS2_HK_MAC_ARP_LEN_OK,
45462306a36Sopenharmony_ci				 arp->length);
45562306a36Sopenharmony_ci		vcap_key_bit_set(vcap, &data,
45662306a36Sopenharmony_ci				 VCAP_IS2_HK_MAC_ARP_TARGET_MATCH,
45762306a36Sopenharmony_ci				 arp->dmac_match);
45862306a36Sopenharmony_ci		vcap_key_bit_set(vcap, &data,
45962306a36Sopenharmony_ci				 VCAP_IS2_HK_MAC_ARP_SENDER_MATCH,
46062306a36Sopenharmony_ci				 arp->smac_match);
46162306a36Sopenharmony_ci		vcap_key_bit_set(vcap, &data,
46262306a36Sopenharmony_ci				 VCAP_IS2_HK_MAC_ARP_OPCODE_UNKNOWN,
46362306a36Sopenharmony_ci				 arp->unknown);
46462306a36Sopenharmony_ci
46562306a36Sopenharmony_ci		/* OPCODE is inverse, bit 0 is reply flag, bit 1 is RARP flag */
46662306a36Sopenharmony_ci		val = ((arp->req == OCELOT_VCAP_BIT_0 ? 1 : 0) |
46762306a36Sopenharmony_ci		       (arp->arp == OCELOT_VCAP_BIT_0 ? 2 : 0));
46862306a36Sopenharmony_ci		msk = ((arp->req == OCELOT_VCAP_BIT_ANY ? 0 : 1) |
46962306a36Sopenharmony_ci		       (arp->arp == OCELOT_VCAP_BIT_ANY ? 0 : 2));
47062306a36Sopenharmony_ci		vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ARP_OPCODE,
47162306a36Sopenharmony_ci			     val, msk);
47262306a36Sopenharmony_ci		vcap_key_bytes_set(vcap, &data,
47362306a36Sopenharmony_ci				   VCAP_IS2_HK_MAC_ARP_L3_IP4_DIP,
47462306a36Sopenharmony_ci				   arp->dip.value.addr, arp->dip.mask.addr);
47562306a36Sopenharmony_ci		vcap_key_bytes_set(vcap, &data,
47662306a36Sopenharmony_ci				   VCAP_IS2_HK_MAC_ARP_L3_IP4_SIP,
47762306a36Sopenharmony_ci				   arp->sip.value.addr, arp->sip.mask.addr);
47862306a36Sopenharmony_ci		vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ARP_DIP_EQ_SIP,
47962306a36Sopenharmony_ci			     0, 0);
48062306a36Sopenharmony_ci		break;
48162306a36Sopenharmony_ci	}
48262306a36Sopenharmony_ci	case OCELOT_VCAP_KEY_IPV4:
48362306a36Sopenharmony_ci	case OCELOT_VCAP_KEY_IPV6: {
48462306a36Sopenharmony_ci		enum ocelot_vcap_bit sip_eq_dip, sport_eq_dport, seq_zero, tcp;
48562306a36Sopenharmony_ci		enum ocelot_vcap_bit ttl, fragment, options, tcp_ack, tcp_urg;
48662306a36Sopenharmony_ci		enum ocelot_vcap_bit tcp_fin, tcp_syn, tcp_rst, tcp_psh;
48762306a36Sopenharmony_ci		struct ocelot_vcap_key_ipv4 *ipv4 = NULL;
48862306a36Sopenharmony_ci		struct ocelot_vcap_key_ipv6 *ipv6 = NULL;
48962306a36Sopenharmony_ci		struct ocelot_vcap_udp_tcp *sport, *dport;
49062306a36Sopenharmony_ci		struct ocelot_vcap_ipv4 sip, dip;
49162306a36Sopenharmony_ci		struct ocelot_vcap_u8 proto, ds;
49262306a36Sopenharmony_ci		struct ocelot_vcap_u48 *ip_data;
49362306a36Sopenharmony_ci
49462306a36Sopenharmony_ci		if (filter->key_type == OCELOT_VCAP_KEY_IPV4) {
49562306a36Sopenharmony_ci			ipv4 = &filter->key.ipv4;
49662306a36Sopenharmony_ci			ttl = ipv4->ttl;
49762306a36Sopenharmony_ci			fragment = ipv4->fragment;
49862306a36Sopenharmony_ci			options = ipv4->options;
49962306a36Sopenharmony_ci			proto = ipv4->proto;
50062306a36Sopenharmony_ci			ds = ipv4->ds;
50162306a36Sopenharmony_ci			ip_data = &ipv4->data;
50262306a36Sopenharmony_ci			sip = ipv4->sip;
50362306a36Sopenharmony_ci			dip = ipv4->dip;
50462306a36Sopenharmony_ci			sport = &ipv4->sport;
50562306a36Sopenharmony_ci			dport = &ipv4->dport;
50662306a36Sopenharmony_ci			tcp_fin = ipv4->tcp_fin;
50762306a36Sopenharmony_ci			tcp_syn = ipv4->tcp_syn;
50862306a36Sopenharmony_ci			tcp_rst = ipv4->tcp_rst;
50962306a36Sopenharmony_ci			tcp_psh = ipv4->tcp_psh;
51062306a36Sopenharmony_ci			tcp_ack = ipv4->tcp_ack;
51162306a36Sopenharmony_ci			tcp_urg = ipv4->tcp_urg;
51262306a36Sopenharmony_ci			sip_eq_dip = ipv4->sip_eq_dip;
51362306a36Sopenharmony_ci			sport_eq_dport = ipv4->sport_eq_dport;
51462306a36Sopenharmony_ci			seq_zero = ipv4->seq_zero;
51562306a36Sopenharmony_ci		} else {
51662306a36Sopenharmony_ci			ipv6 = &filter->key.ipv6;
51762306a36Sopenharmony_ci			ttl = ipv6->ttl;
51862306a36Sopenharmony_ci			fragment = OCELOT_VCAP_BIT_ANY;
51962306a36Sopenharmony_ci			options = OCELOT_VCAP_BIT_ANY;
52062306a36Sopenharmony_ci			proto = ipv6->proto;
52162306a36Sopenharmony_ci			ds = ipv6->ds;
52262306a36Sopenharmony_ci			ip_data = &ipv6->data;
52362306a36Sopenharmony_ci			for (i = 0; i < 8; i++) {
52462306a36Sopenharmony_ci				val = ipv6->sip.value[i + 8];
52562306a36Sopenharmony_ci				msk = ipv6->sip.mask[i + 8];
52662306a36Sopenharmony_ci				if (i < 4) {
52762306a36Sopenharmony_ci					dip.value.addr[i] = val;
52862306a36Sopenharmony_ci					dip.mask.addr[i] = msk;
52962306a36Sopenharmony_ci				} else {
53062306a36Sopenharmony_ci					sip.value.addr[i - 4] = val;
53162306a36Sopenharmony_ci					sip.mask.addr[i - 4] = msk;
53262306a36Sopenharmony_ci				}
53362306a36Sopenharmony_ci			}
53462306a36Sopenharmony_ci			sport = &ipv6->sport;
53562306a36Sopenharmony_ci			dport = &ipv6->dport;
53662306a36Sopenharmony_ci			tcp_fin = ipv6->tcp_fin;
53762306a36Sopenharmony_ci			tcp_syn = ipv6->tcp_syn;
53862306a36Sopenharmony_ci			tcp_rst = ipv6->tcp_rst;
53962306a36Sopenharmony_ci			tcp_psh = ipv6->tcp_psh;
54062306a36Sopenharmony_ci			tcp_ack = ipv6->tcp_ack;
54162306a36Sopenharmony_ci			tcp_urg = ipv6->tcp_urg;
54262306a36Sopenharmony_ci			sip_eq_dip = ipv6->sip_eq_dip;
54362306a36Sopenharmony_ci			sport_eq_dport = ipv6->sport_eq_dport;
54462306a36Sopenharmony_ci			seq_zero = ipv6->seq_zero;
54562306a36Sopenharmony_ci		}
54662306a36Sopenharmony_ci
54762306a36Sopenharmony_ci		vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_IP4,
54862306a36Sopenharmony_ci				 ipv4 ? OCELOT_VCAP_BIT_1 : OCELOT_VCAP_BIT_0);
54962306a36Sopenharmony_ci		vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L3_FRAGMENT,
55062306a36Sopenharmony_ci				 fragment);
55162306a36Sopenharmony_ci		vcap_key_set(vcap, &data, VCAP_IS2_HK_L3_FRAG_OFS_GT0, 0, 0);
55262306a36Sopenharmony_ci		vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L3_OPTIONS,
55362306a36Sopenharmony_ci				 options);
55462306a36Sopenharmony_ci		vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_IP4_L3_TTL_GT0,
55562306a36Sopenharmony_ci				 ttl);
55662306a36Sopenharmony_ci		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L3_TOS,
55762306a36Sopenharmony_ci				   ds.value, ds.mask);
55862306a36Sopenharmony_ci		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L3_IP4_DIP,
55962306a36Sopenharmony_ci				   dip.value.addr, dip.mask.addr);
56062306a36Sopenharmony_ci		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L3_IP4_SIP,
56162306a36Sopenharmony_ci				   sip.value.addr, sip.mask.addr);
56262306a36Sopenharmony_ci		vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_DIP_EQ_SIP,
56362306a36Sopenharmony_ci				 sip_eq_dip);
56462306a36Sopenharmony_ci		val = proto.value[0];
56562306a36Sopenharmony_ci		msk = proto.mask[0];
56662306a36Sopenharmony_ci		type = IS2_TYPE_IP_UDP_TCP;
56762306a36Sopenharmony_ci		if (msk == 0xff && (val == IPPROTO_TCP || val == IPPROTO_UDP)) {
56862306a36Sopenharmony_ci			/* UDP/TCP protocol match */
56962306a36Sopenharmony_ci			tcp = (val == IPPROTO_TCP ?
57062306a36Sopenharmony_ci			       OCELOT_VCAP_BIT_1 : OCELOT_VCAP_BIT_0);
57162306a36Sopenharmony_ci			vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_TCP, tcp);
57262306a36Sopenharmony_ci			vcap_key_l4_port_set(vcap, &data,
57362306a36Sopenharmony_ci					     VCAP_IS2_HK_L4_DPORT, dport);
57462306a36Sopenharmony_ci			vcap_key_l4_port_set(vcap, &data,
57562306a36Sopenharmony_ci					     VCAP_IS2_HK_L4_SPORT, sport);
57662306a36Sopenharmony_ci			vcap_key_set(vcap, &data, VCAP_IS2_HK_L4_RNG, 0, 0);
57762306a36Sopenharmony_ci			vcap_key_bit_set(vcap, &data,
57862306a36Sopenharmony_ci					 VCAP_IS2_HK_L4_SPORT_EQ_DPORT,
57962306a36Sopenharmony_ci					 sport_eq_dport);
58062306a36Sopenharmony_ci			vcap_key_bit_set(vcap, &data,
58162306a36Sopenharmony_ci					 VCAP_IS2_HK_L4_SEQUENCE_EQ0,
58262306a36Sopenharmony_ci					 seq_zero);
58362306a36Sopenharmony_ci			vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_FIN,
58462306a36Sopenharmony_ci					 tcp_fin);
58562306a36Sopenharmony_ci			vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_SYN,
58662306a36Sopenharmony_ci					 tcp_syn);
58762306a36Sopenharmony_ci			vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_RST,
58862306a36Sopenharmony_ci					 tcp_rst);
58962306a36Sopenharmony_ci			vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_PSH,
59062306a36Sopenharmony_ci					 tcp_psh);
59162306a36Sopenharmony_ci			vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_ACK,
59262306a36Sopenharmony_ci					 tcp_ack);
59362306a36Sopenharmony_ci			vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_URG,
59462306a36Sopenharmony_ci					 tcp_urg);
59562306a36Sopenharmony_ci			vcap_key_set(vcap, &data, VCAP_IS2_HK_L4_1588_DOM,
59662306a36Sopenharmony_ci				     0, 0);
59762306a36Sopenharmony_ci			vcap_key_set(vcap, &data, VCAP_IS2_HK_L4_1588_VER,
59862306a36Sopenharmony_ci				     0, 0);
59962306a36Sopenharmony_ci		} else {
60062306a36Sopenharmony_ci			if (msk == 0) {
60162306a36Sopenharmony_ci				/* Any IP protocol match */
60262306a36Sopenharmony_ci				type_mask = IS2_TYPE_MASK_IP_ANY;
60362306a36Sopenharmony_ci			} else {
60462306a36Sopenharmony_ci				/* Non-UDP/TCP protocol match */
60562306a36Sopenharmony_ci				type = IS2_TYPE_IP_OTHER;
60662306a36Sopenharmony_ci				for (i = 0; i < 6; i++) {
60762306a36Sopenharmony_ci					payload.value[i] = ip_data->value[i];
60862306a36Sopenharmony_ci					payload.mask[i] = ip_data->mask[i];
60962306a36Sopenharmony_ci				}
61062306a36Sopenharmony_ci			}
61162306a36Sopenharmony_ci			vcap_key_bytes_set(vcap, &data,
61262306a36Sopenharmony_ci					   VCAP_IS2_HK_IP4_L3_PROTO,
61362306a36Sopenharmony_ci					   proto.value, proto.mask);
61462306a36Sopenharmony_ci			vcap_key_bytes_set(vcap, &data,
61562306a36Sopenharmony_ci					   VCAP_IS2_HK_L3_PAYLOAD,
61662306a36Sopenharmony_ci					   payload.value, payload.mask);
61762306a36Sopenharmony_ci		}
61862306a36Sopenharmony_ci		break;
61962306a36Sopenharmony_ci	}
62062306a36Sopenharmony_ci	case OCELOT_VCAP_KEY_ANY:
62162306a36Sopenharmony_ci	default:
62262306a36Sopenharmony_ci		type = 0;
62362306a36Sopenharmony_ci		type_mask = 0;
62462306a36Sopenharmony_ci		count = vcap->entry_width / 2;
62562306a36Sopenharmony_ci		/* Iterate over the non-common part of the key and
62662306a36Sopenharmony_ci		 * clear entry data
62762306a36Sopenharmony_ci		 */
62862306a36Sopenharmony_ci		for (i = vcap->keys[VCAP_IS2_HK_L2_DMAC].offset;
62962306a36Sopenharmony_ci		     i < count; i += ENTRY_WIDTH) {
63062306a36Sopenharmony_ci			vcap_key_field_set(&data, i, min(32u, count - i), 0, 0);
63162306a36Sopenharmony_ci		}
63262306a36Sopenharmony_ci		break;
63362306a36Sopenharmony_ci	}
63462306a36Sopenharmony_ci
63562306a36Sopenharmony_ci	vcap_key_set(vcap, &data, VCAP_IS2_TYPE, type, type_mask);
63662306a36Sopenharmony_ci	is2_action_set(ocelot, &data, filter);
63762306a36Sopenharmony_ci	vcap_data_set(data.counter, data.counter_offset,
63862306a36Sopenharmony_ci		      vcap->counter_width, filter->stats.pkts);
63962306a36Sopenharmony_ci
64062306a36Sopenharmony_ci	/* Write row */
64162306a36Sopenharmony_ci	vcap_entry2cache(ocelot, vcap, &data);
64262306a36Sopenharmony_ci	vcap_action2cache(ocelot, vcap, &data);
64362306a36Sopenharmony_ci	vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_WRITE, VCAP_SEL_ALL);
64462306a36Sopenharmony_ci}
64562306a36Sopenharmony_ci
64662306a36Sopenharmony_cistatic void is1_action_set(struct ocelot *ocelot, struct vcap_data *data,
64762306a36Sopenharmony_ci			   const struct ocelot_vcap_filter *filter)
64862306a36Sopenharmony_ci{
64962306a36Sopenharmony_ci	const struct vcap_props *vcap = &ocelot->vcap[VCAP_IS1];
65062306a36Sopenharmony_ci	const struct ocelot_vcap_action *a = &filter->action;
65162306a36Sopenharmony_ci
65262306a36Sopenharmony_ci	vcap_action_set(vcap, data, VCAP_IS1_ACT_VID_REPLACE_ENA,
65362306a36Sopenharmony_ci			a->vid_replace_ena);
65462306a36Sopenharmony_ci	vcap_action_set(vcap, data, VCAP_IS1_ACT_VID_ADD_VAL, a->vid);
65562306a36Sopenharmony_ci	vcap_action_set(vcap, data, VCAP_IS1_ACT_VLAN_POP_CNT_ENA,
65662306a36Sopenharmony_ci			a->vlan_pop_cnt_ena);
65762306a36Sopenharmony_ci	vcap_action_set(vcap, data, VCAP_IS1_ACT_VLAN_POP_CNT,
65862306a36Sopenharmony_ci			a->vlan_pop_cnt);
65962306a36Sopenharmony_ci	vcap_action_set(vcap, data, VCAP_IS1_ACT_PCP_DEI_ENA, a->pcp_dei_ena);
66062306a36Sopenharmony_ci	vcap_action_set(vcap, data, VCAP_IS1_ACT_PCP_VAL, a->pcp);
66162306a36Sopenharmony_ci	vcap_action_set(vcap, data, VCAP_IS1_ACT_DEI_VAL, a->dei);
66262306a36Sopenharmony_ci	vcap_action_set(vcap, data, VCAP_IS1_ACT_QOS_ENA, a->qos_ena);
66362306a36Sopenharmony_ci	vcap_action_set(vcap, data, VCAP_IS1_ACT_QOS_VAL, a->qos_val);
66462306a36Sopenharmony_ci	vcap_action_set(vcap, data, VCAP_IS1_ACT_PAG_OVERRIDE_MASK,
66562306a36Sopenharmony_ci			a->pag_override_mask);
66662306a36Sopenharmony_ci	vcap_action_set(vcap, data, VCAP_IS1_ACT_PAG_VAL, a->pag_val);
66762306a36Sopenharmony_ci}
66862306a36Sopenharmony_ci
66962306a36Sopenharmony_cistatic void is1_entry_set(struct ocelot *ocelot, int ix,
67062306a36Sopenharmony_ci			  struct ocelot_vcap_filter *filter)
67162306a36Sopenharmony_ci{
67262306a36Sopenharmony_ci	const struct vcap_props *vcap = &ocelot->vcap[VCAP_IS1];
67362306a36Sopenharmony_ci	struct ocelot_vcap_key_vlan *tag = &filter->vlan;
67462306a36Sopenharmony_ci	struct vcap_data data;
67562306a36Sopenharmony_ci	int row = ix / 2;
67662306a36Sopenharmony_ci	u32 type;
67762306a36Sopenharmony_ci
67862306a36Sopenharmony_ci	memset(&data, 0, sizeof(data));
67962306a36Sopenharmony_ci
68062306a36Sopenharmony_ci	/* Read row */
68162306a36Sopenharmony_ci	vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_READ, VCAP_SEL_ALL);
68262306a36Sopenharmony_ci	vcap_cache2entry(ocelot, vcap, &data);
68362306a36Sopenharmony_ci	vcap_cache2action(ocelot, vcap, &data);
68462306a36Sopenharmony_ci
68562306a36Sopenharmony_ci	data.tg_sw = VCAP_TG_HALF;
68662306a36Sopenharmony_ci	data.type = IS1_ACTION_TYPE_NORMAL;
68762306a36Sopenharmony_ci	vcap_data_offset_get(vcap, &data, ix);
68862306a36Sopenharmony_ci	data.tg = (data.tg & ~data.tg_mask);
68962306a36Sopenharmony_ci	if (filter->prio != 0)
69062306a36Sopenharmony_ci		data.tg |= data.tg_value;
69162306a36Sopenharmony_ci
69262306a36Sopenharmony_ci	vcap_key_set(vcap, &data, VCAP_IS1_HK_LOOKUP, filter->lookup, 0x3);
69362306a36Sopenharmony_ci	vcap_key_set(vcap, &data, VCAP_IS1_HK_IGR_PORT_MASK, 0,
69462306a36Sopenharmony_ci		     ~filter->ingress_port_mask);
69562306a36Sopenharmony_ci	vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_L2_MC, filter->dmac_mc);
69662306a36Sopenharmony_ci	vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_L2_BC, filter->dmac_bc);
69762306a36Sopenharmony_ci	vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_VLAN_TAGGED, tag->tagged);
69862306a36Sopenharmony_ci	vcap_key_set(vcap, &data, VCAP_IS1_HK_VID,
69962306a36Sopenharmony_ci		     tag->vid.value, tag->vid.mask);
70062306a36Sopenharmony_ci	vcap_key_set(vcap, &data, VCAP_IS1_HK_PCP,
70162306a36Sopenharmony_ci		     tag->pcp.value[0], tag->pcp.mask[0]);
70262306a36Sopenharmony_ci	type = IS1_TYPE_S1_NORMAL;
70362306a36Sopenharmony_ci
70462306a36Sopenharmony_ci	switch (filter->key_type) {
70562306a36Sopenharmony_ci	case OCELOT_VCAP_KEY_ETYPE: {
70662306a36Sopenharmony_ci		struct ocelot_vcap_key_etype *etype = &filter->key.etype;
70762306a36Sopenharmony_ci
70862306a36Sopenharmony_ci		vcap_key_bytes_set(vcap, &data, VCAP_IS1_HK_L2_SMAC,
70962306a36Sopenharmony_ci				   etype->smac.value, etype->smac.mask);
71062306a36Sopenharmony_ci		vcap_key_bytes_set(vcap, &data, VCAP_IS1_HK_ETYPE,
71162306a36Sopenharmony_ci				   etype->etype.value, etype->etype.mask);
71262306a36Sopenharmony_ci		break;
71362306a36Sopenharmony_ci	}
71462306a36Sopenharmony_ci	case OCELOT_VCAP_KEY_IPV4: {
71562306a36Sopenharmony_ci		struct ocelot_vcap_key_ipv4 *ipv4 = &filter->key.ipv4;
71662306a36Sopenharmony_ci		struct ocelot_vcap_udp_tcp *sport = &ipv4->sport;
71762306a36Sopenharmony_ci		struct ocelot_vcap_udp_tcp *dport = &ipv4->dport;
71862306a36Sopenharmony_ci		enum ocelot_vcap_bit tcp_udp = OCELOT_VCAP_BIT_0;
71962306a36Sopenharmony_ci		struct ocelot_vcap_u8 proto = ipv4->proto;
72062306a36Sopenharmony_ci		struct ocelot_vcap_ipv4 sip = ipv4->sip;
72162306a36Sopenharmony_ci		u32 val, msk;
72262306a36Sopenharmony_ci
72362306a36Sopenharmony_ci		vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_IP_SNAP,
72462306a36Sopenharmony_ci				 OCELOT_VCAP_BIT_1);
72562306a36Sopenharmony_ci		vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_IP4,
72662306a36Sopenharmony_ci				 OCELOT_VCAP_BIT_1);
72762306a36Sopenharmony_ci		vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_ETYPE_LEN,
72862306a36Sopenharmony_ci				 OCELOT_VCAP_BIT_1);
72962306a36Sopenharmony_ci		vcap_key_bytes_set(vcap, &data, VCAP_IS1_HK_L3_IP4_SIP,
73062306a36Sopenharmony_ci				   sip.value.addr, sip.mask.addr);
73162306a36Sopenharmony_ci
73262306a36Sopenharmony_ci		val = proto.value[0];
73362306a36Sopenharmony_ci		msk = proto.mask[0];
73462306a36Sopenharmony_ci
73562306a36Sopenharmony_ci		if ((val == NEXTHDR_TCP || val == NEXTHDR_UDP) && msk == 0xff)
73662306a36Sopenharmony_ci			tcp_udp = OCELOT_VCAP_BIT_1;
73762306a36Sopenharmony_ci		vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_TCP_UDP, tcp_udp);
73862306a36Sopenharmony_ci
73962306a36Sopenharmony_ci		if (tcp_udp) {
74062306a36Sopenharmony_ci			enum ocelot_vcap_bit tcp = OCELOT_VCAP_BIT_0;
74162306a36Sopenharmony_ci
74262306a36Sopenharmony_ci			if (val == NEXTHDR_TCP)
74362306a36Sopenharmony_ci				tcp = OCELOT_VCAP_BIT_1;
74462306a36Sopenharmony_ci
74562306a36Sopenharmony_ci			vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_TCP, tcp);
74662306a36Sopenharmony_ci			vcap_key_l4_port_set(vcap, &data, VCAP_IS1_HK_L4_SPORT,
74762306a36Sopenharmony_ci					     sport);
74862306a36Sopenharmony_ci			/* Overloaded field */
74962306a36Sopenharmony_ci			vcap_key_l4_port_set(vcap, &data, VCAP_IS1_HK_ETYPE,
75062306a36Sopenharmony_ci					     dport);
75162306a36Sopenharmony_ci		} else {
75262306a36Sopenharmony_ci			/* IPv4 "other" frame */
75362306a36Sopenharmony_ci			struct ocelot_vcap_u16 etype = {0};
75462306a36Sopenharmony_ci
75562306a36Sopenharmony_ci			/* Overloaded field */
75662306a36Sopenharmony_ci			etype.value[0] = proto.value[0];
75762306a36Sopenharmony_ci			etype.mask[0] = proto.mask[0];
75862306a36Sopenharmony_ci
75962306a36Sopenharmony_ci			vcap_key_bytes_set(vcap, &data, VCAP_IS1_HK_ETYPE,
76062306a36Sopenharmony_ci					   etype.value, etype.mask);
76162306a36Sopenharmony_ci		}
76262306a36Sopenharmony_ci		break;
76362306a36Sopenharmony_ci	}
76462306a36Sopenharmony_ci	default:
76562306a36Sopenharmony_ci		break;
76662306a36Sopenharmony_ci	}
76762306a36Sopenharmony_ci	vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_TYPE,
76862306a36Sopenharmony_ci			 type ? OCELOT_VCAP_BIT_1 : OCELOT_VCAP_BIT_0);
76962306a36Sopenharmony_ci
77062306a36Sopenharmony_ci	is1_action_set(ocelot, &data, filter);
77162306a36Sopenharmony_ci	vcap_data_set(data.counter, data.counter_offset,
77262306a36Sopenharmony_ci		      vcap->counter_width, filter->stats.pkts);
77362306a36Sopenharmony_ci
77462306a36Sopenharmony_ci	/* Write row */
77562306a36Sopenharmony_ci	vcap_entry2cache(ocelot, vcap, &data);
77662306a36Sopenharmony_ci	vcap_action2cache(ocelot, vcap, &data);
77762306a36Sopenharmony_ci	vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_WRITE, VCAP_SEL_ALL);
77862306a36Sopenharmony_ci}
77962306a36Sopenharmony_ci
78062306a36Sopenharmony_cistatic void es0_action_set(struct ocelot *ocelot, struct vcap_data *data,
78162306a36Sopenharmony_ci			   const struct ocelot_vcap_filter *filter)
78262306a36Sopenharmony_ci{
78362306a36Sopenharmony_ci	const struct vcap_props *vcap = &ocelot->vcap[VCAP_ES0];
78462306a36Sopenharmony_ci	const struct ocelot_vcap_action *a = &filter->action;
78562306a36Sopenharmony_ci
78662306a36Sopenharmony_ci	vcap_action_set(vcap, data, VCAP_ES0_ACT_PUSH_OUTER_TAG,
78762306a36Sopenharmony_ci			a->push_outer_tag);
78862306a36Sopenharmony_ci	vcap_action_set(vcap, data, VCAP_ES0_ACT_PUSH_INNER_TAG,
78962306a36Sopenharmony_ci			a->push_inner_tag);
79062306a36Sopenharmony_ci	vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_A_TPID_SEL,
79162306a36Sopenharmony_ci			a->tag_a_tpid_sel);
79262306a36Sopenharmony_ci	vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_A_VID_SEL,
79362306a36Sopenharmony_ci			a->tag_a_vid_sel);
79462306a36Sopenharmony_ci	vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_A_PCP_SEL,
79562306a36Sopenharmony_ci			a->tag_a_pcp_sel);
79662306a36Sopenharmony_ci	vcap_action_set(vcap, data, VCAP_ES0_ACT_VID_A_VAL, a->vid_a_val);
79762306a36Sopenharmony_ci	vcap_action_set(vcap, data, VCAP_ES0_ACT_PCP_A_VAL, a->pcp_a_val);
79862306a36Sopenharmony_ci	vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_B_TPID_SEL,
79962306a36Sopenharmony_ci			a->tag_b_tpid_sel);
80062306a36Sopenharmony_ci	vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_B_VID_SEL,
80162306a36Sopenharmony_ci			a->tag_b_vid_sel);
80262306a36Sopenharmony_ci	vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_B_PCP_SEL,
80362306a36Sopenharmony_ci			a->tag_b_pcp_sel);
80462306a36Sopenharmony_ci	vcap_action_set(vcap, data, VCAP_ES0_ACT_VID_B_VAL, a->vid_b_val);
80562306a36Sopenharmony_ci	vcap_action_set(vcap, data, VCAP_ES0_ACT_PCP_B_VAL, a->pcp_b_val);
80662306a36Sopenharmony_ci}
80762306a36Sopenharmony_ci
80862306a36Sopenharmony_cistatic void es0_entry_set(struct ocelot *ocelot, int ix,
80962306a36Sopenharmony_ci			  struct ocelot_vcap_filter *filter)
81062306a36Sopenharmony_ci{
81162306a36Sopenharmony_ci	const struct vcap_props *vcap = &ocelot->vcap[VCAP_ES0];
81262306a36Sopenharmony_ci	struct ocelot_vcap_key_vlan *tag = &filter->vlan;
81362306a36Sopenharmony_ci	struct vcap_data data;
81462306a36Sopenharmony_ci	int row = ix;
81562306a36Sopenharmony_ci
81662306a36Sopenharmony_ci	memset(&data, 0, sizeof(data));
81762306a36Sopenharmony_ci
81862306a36Sopenharmony_ci	/* Read row */
81962306a36Sopenharmony_ci	vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_READ, VCAP_SEL_ALL);
82062306a36Sopenharmony_ci	vcap_cache2entry(ocelot, vcap, &data);
82162306a36Sopenharmony_ci	vcap_cache2action(ocelot, vcap, &data);
82262306a36Sopenharmony_ci
82362306a36Sopenharmony_ci	data.tg_sw = VCAP_TG_FULL;
82462306a36Sopenharmony_ci	data.type = ES0_ACTION_TYPE_NORMAL;
82562306a36Sopenharmony_ci	vcap_data_offset_get(vcap, &data, ix);
82662306a36Sopenharmony_ci	data.tg = (data.tg & ~data.tg_mask);
82762306a36Sopenharmony_ci	if (filter->prio != 0)
82862306a36Sopenharmony_ci		data.tg |= data.tg_value;
82962306a36Sopenharmony_ci
83062306a36Sopenharmony_ci	vcap_key_set(vcap, &data, VCAP_ES0_IGR_PORT, filter->ingress_port.value,
83162306a36Sopenharmony_ci		     filter->ingress_port.mask);
83262306a36Sopenharmony_ci	vcap_key_set(vcap, &data, VCAP_ES0_EGR_PORT, filter->egress_port.value,
83362306a36Sopenharmony_ci		     filter->egress_port.mask);
83462306a36Sopenharmony_ci	vcap_key_bit_set(vcap, &data, VCAP_ES0_L2_MC, filter->dmac_mc);
83562306a36Sopenharmony_ci	vcap_key_bit_set(vcap, &data, VCAP_ES0_L2_BC, filter->dmac_bc);
83662306a36Sopenharmony_ci	vcap_key_set(vcap, &data, VCAP_ES0_VID,
83762306a36Sopenharmony_ci		     tag->vid.value, tag->vid.mask);
83862306a36Sopenharmony_ci	vcap_key_set(vcap, &data, VCAP_ES0_PCP,
83962306a36Sopenharmony_ci		     tag->pcp.value[0], tag->pcp.mask[0]);
84062306a36Sopenharmony_ci
84162306a36Sopenharmony_ci	es0_action_set(ocelot, &data, filter);
84262306a36Sopenharmony_ci	vcap_data_set(data.counter, data.counter_offset,
84362306a36Sopenharmony_ci		      vcap->counter_width, filter->stats.pkts);
84462306a36Sopenharmony_ci
84562306a36Sopenharmony_ci	/* Write row */
84662306a36Sopenharmony_ci	vcap_entry2cache(ocelot, vcap, &data);
84762306a36Sopenharmony_ci	vcap_action2cache(ocelot, vcap, &data);
84862306a36Sopenharmony_ci	vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_WRITE, VCAP_SEL_ALL);
84962306a36Sopenharmony_ci}
85062306a36Sopenharmony_ci
85162306a36Sopenharmony_cistatic void vcap_entry_get(struct ocelot *ocelot, int ix,
85262306a36Sopenharmony_ci			   struct ocelot_vcap_filter *filter)
85362306a36Sopenharmony_ci{
85462306a36Sopenharmony_ci	const struct vcap_props *vcap = &ocelot->vcap[filter->block_id];
85562306a36Sopenharmony_ci	struct vcap_data data;
85662306a36Sopenharmony_ci	int row, count;
85762306a36Sopenharmony_ci	u32 cnt;
85862306a36Sopenharmony_ci
85962306a36Sopenharmony_ci	if (filter->block_id == VCAP_ES0)
86062306a36Sopenharmony_ci		data.tg_sw = VCAP_TG_FULL;
86162306a36Sopenharmony_ci	else
86262306a36Sopenharmony_ci		data.tg_sw = VCAP_TG_HALF;
86362306a36Sopenharmony_ci
86462306a36Sopenharmony_ci	count = (1 << (data.tg_sw - 1));
86562306a36Sopenharmony_ci	row = (ix / count);
86662306a36Sopenharmony_ci	vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_READ, VCAP_SEL_COUNTER);
86762306a36Sopenharmony_ci	vcap_cache2action(ocelot, vcap, &data);
86862306a36Sopenharmony_ci	vcap_data_offset_get(vcap, &data, ix);
86962306a36Sopenharmony_ci	cnt = vcap_data_get(data.counter, data.counter_offset,
87062306a36Sopenharmony_ci			    vcap->counter_width);
87162306a36Sopenharmony_ci
87262306a36Sopenharmony_ci	filter->stats.pkts = cnt;
87362306a36Sopenharmony_ci}
87462306a36Sopenharmony_ci
87562306a36Sopenharmony_cistatic void vcap_entry_set(struct ocelot *ocelot, int ix,
87662306a36Sopenharmony_ci			   struct ocelot_vcap_filter *filter)
87762306a36Sopenharmony_ci{
87862306a36Sopenharmony_ci	if (filter->block_id == VCAP_IS1)
87962306a36Sopenharmony_ci		return is1_entry_set(ocelot, ix, filter);
88062306a36Sopenharmony_ci	if (filter->block_id == VCAP_IS2)
88162306a36Sopenharmony_ci		return is2_entry_set(ocelot, ix, filter);
88262306a36Sopenharmony_ci	if (filter->block_id == VCAP_ES0)
88362306a36Sopenharmony_ci		return es0_entry_set(ocelot, ix, filter);
88462306a36Sopenharmony_ci}
88562306a36Sopenharmony_ci
88662306a36Sopenharmony_cistruct vcap_policer_entry {
88762306a36Sopenharmony_ci	struct list_head list;
88862306a36Sopenharmony_ci	refcount_t refcount;
88962306a36Sopenharmony_ci	u32 pol_ix;
89062306a36Sopenharmony_ci};
89162306a36Sopenharmony_ci
89262306a36Sopenharmony_ciint ocelot_vcap_policer_add(struct ocelot *ocelot, u32 pol_ix,
89362306a36Sopenharmony_ci			    struct ocelot_policer *pol)
89462306a36Sopenharmony_ci{
89562306a36Sopenharmony_ci	struct qos_policer_conf pp = { 0 };
89662306a36Sopenharmony_ci	struct vcap_policer_entry *tmp;
89762306a36Sopenharmony_ci	int ret;
89862306a36Sopenharmony_ci
89962306a36Sopenharmony_ci	if (!pol)
90062306a36Sopenharmony_ci		return -EINVAL;
90162306a36Sopenharmony_ci
90262306a36Sopenharmony_ci	pp.mode = MSCC_QOS_RATE_MODE_DATA;
90362306a36Sopenharmony_ci	pp.pir = pol->rate;
90462306a36Sopenharmony_ci	pp.pbs = pol->burst;
90562306a36Sopenharmony_ci
90662306a36Sopenharmony_ci	list_for_each_entry(tmp, &ocelot->vcap_pol.pol_list, list)
90762306a36Sopenharmony_ci		if (tmp->pol_ix == pol_ix) {
90862306a36Sopenharmony_ci			refcount_inc(&tmp->refcount);
90962306a36Sopenharmony_ci			return 0;
91062306a36Sopenharmony_ci		}
91162306a36Sopenharmony_ci
91262306a36Sopenharmony_ci	tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
91362306a36Sopenharmony_ci	if (!tmp)
91462306a36Sopenharmony_ci		return -ENOMEM;
91562306a36Sopenharmony_ci
91662306a36Sopenharmony_ci	ret = qos_policer_conf_set(ocelot, pol_ix, &pp);
91762306a36Sopenharmony_ci	if (ret) {
91862306a36Sopenharmony_ci		kfree(tmp);
91962306a36Sopenharmony_ci		return ret;
92062306a36Sopenharmony_ci	}
92162306a36Sopenharmony_ci
92262306a36Sopenharmony_ci	tmp->pol_ix = pol_ix;
92362306a36Sopenharmony_ci	refcount_set(&tmp->refcount, 1);
92462306a36Sopenharmony_ci	list_add_tail(&tmp->list, &ocelot->vcap_pol.pol_list);
92562306a36Sopenharmony_ci
92662306a36Sopenharmony_ci	return 0;
92762306a36Sopenharmony_ci}
92862306a36Sopenharmony_ciEXPORT_SYMBOL(ocelot_vcap_policer_add);
92962306a36Sopenharmony_ci
93062306a36Sopenharmony_ciint ocelot_vcap_policer_del(struct ocelot *ocelot, u32 pol_ix)
93162306a36Sopenharmony_ci{
93262306a36Sopenharmony_ci	struct qos_policer_conf pp = {0};
93362306a36Sopenharmony_ci	struct vcap_policer_entry *tmp, *n;
93462306a36Sopenharmony_ci	u8 z = 0;
93562306a36Sopenharmony_ci
93662306a36Sopenharmony_ci	list_for_each_entry_safe(tmp, n, &ocelot->vcap_pol.pol_list, list)
93762306a36Sopenharmony_ci		if (tmp->pol_ix == pol_ix) {
93862306a36Sopenharmony_ci			z = refcount_dec_and_test(&tmp->refcount);
93962306a36Sopenharmony_ci			if (z) {
94062306a36Sopenharmony_ci				list_del(&tmp->list);
94162306a36Sopenharmony_ci				kfree(tmp);
94262306a36Sopenharmony_ci			}
94362306a36Sopenharmony_ci		}
94462306a36Sopenharmony_ci
94562306a36Sopenharmony_ci	if (z) {
94662306a36Sopenharmony_ci		pp.mode = MSCC_QOS_RATE_MODE_DISABLED;
94762306a36Sopenharmony_ci		return qos_policer_conf_set(ocelot, pol_ix, &pp);
94862306a36Sopenharmony_ci	}
94962306a36Sopenharmony_ci
95062306a36Sopenharmony_ci	return 0;
95162306a36Sopenharmony_ci}
95262306a36Sopenharmony_ciEXPORT_SYMBOL(ocelot_vcap_policer_del);
95362306a36Sopenharmony_ci
95462306a36Sopenharmony_cistatic int
95562306a36Sopenharmony_ciocelot_vcap_filter_add_aux_resources(struct ocelot *ocelot,
95662306a36Sopenharmony_ci				     struct ocelot_vcap_filter *filter,
95762306a36Sopenharmony_ci				     struct netlink_ext_ack *extack)
95862306a36Sopenharmony_ci{
95962306a36Sopenharmony_ci	struct ocelot_mirror *m;
96062306a36Sopenharmony_ci	int ret;
96162306a36Sopenharmony_ci
96262306a36Sopenharmony_ci	if (filter->block_id == VCAP_IS2 && filter->action.mirror_ena) {
96362306a36Sopenharmony_ci		m = ocelot_mirror_get(ocelot, filter->egress_port.value,
96462306a36Sopenharmony_ci				      extack);
96562306a36Sopenharmony_ci		if (IS_ERR(m))
96662306a36Sopenharmony_ci			return PTR_ERR(m);
96762306a36Sopenharmony_ci	}
96862306a36Sopenharmony_ci
96962306a36Sopenharmony_ci	if (filter->block_id == VCAP_IS2 && filter->action.police_ena) {
97062306a36Sopenharmony_ci		ret = ocelot_vcap_policer_add(ocelot, filter->action.pol_ix,
97162306a36Sopenharmony_ci					      &filter->action.pol);
97262306a36Sopenharmony_ci		if (ret)
97362306a36Sopenharmony_ci			return ret;
97462306a36Sopenharmony_ci	}
97562306a36Sopenharmony_ci
97662306a36Sopenharmony_ci	return 0;
97762306a36Sopenharmony_ci}
97862306a36Sopenharmony_ci
97962306a36Sopenharmony_cistatic void
98062306a36Sopenharmony_ciocelot_vcap_filter_del_aux_resources(struct ocelot *ocelot,
98162306a36Sopenharmony_ci				     struct ocelot_vcap_filter *filter)
98262306a36Sopenharmony_ci{
98362306a36Sopenharmony_ci	if (filter->block_id == VCAP_IS2 && filter->action.police_ena)
98462306a36Sopenharmony_ci		ocelot_vcap_policer_del(ocelot, filter->action.pol_ix);
98562306a36Sopenharmony_ci
98662306a36Sopenharmony_ci	if (filter->block_id == VCAP_IS2 && filter->action.mirror_ena)
98762306a36Sopenharmony_ci		ocelot_mirror_put(ocelot);
98862306a36Sopenharmony_ci}
98962306a36Sopenharmony_ci
99062306a36Sopenharmony_cistatic int ocelot_vcap_filter_add_to_block(struct ocelot *ocelot,
99162306a36Sopenharmony_ci					   struct ocelot_vcap_block *block,
99262306a36Sopenharmony_ci					   struct ocelot_vcap_filter *filter,
99362306a36Sopenharmony_ci					   struct netlink_ext_ack *extack)
99462306a36Sopenharmony_ci{
99562306a36Sopenharmony_ci	struct list_head *pos = &block->rules;
99662306a36Sopenharmony_ci	struct ocelot_vcap_filter *tmp;
99762306a36Sopenharmony_ci	int ret;
99862306a36Sopenharmony_ci
99962306a36Sopenharmony_ci	ret = ocelot_vcap_filter_add_aux_resources(ocelot, filter, extack);
100062306a36Sopenharmony_ci	if (ret)
100162306a36Sopenharmony_ci		return ret;
100262306a36Sopenharmony_ci
100362306a36Sopenharmony_ci	block->count++;
100462306a36Sopenharmony_ci
100562306a36Sopenharmony_ci	list_for_each_entry(tmp, &block->rules, list) {
100662306a36Sopenharmony_ci		if (filter->prio < tmp->prio) {
100762306a36Sopenharmony_ci			pos = &tmp->list;
100862306a36Sopenharmony_ci			break;
100962306a36Sopenharmony_ci		}
101062306a36Sopenharmony_ci	}
101162306a36Sopenharmony_ci	list_add_tail(&filter->list, pos);
101262306a36Sopenharmony_ci
101362306a36Sopenharmony_ci	return 0;
101462306a36Sopenharmony_ci}
101562306a36Sopenharmony_ci
101662306a36Sopenharmony_cistatic bool ocelot_vcap_filter_equal(const struct ocelot_vcap_filter *a,
101762306a36Sopenharmony_ci				     const struct ocelot_vcap_filter *b)
101862306a36Sopenharmony_ci{
101962306a36Sopenharmony_ci	return !memcmp(&a->id, &b->id, sizeof(struct ocelot_vcap_id));
102062306a36Sopenharmony_ci}
102162306a36Sopenharmony_ci
102262306a36Sopenharmony_cistatic int ocelot_vcap_block_get_filter_index(struct ocelot_vcap_block *block,
102362306a36Sopenharmony_ci					      struct ocelot_vcap_filter *filter)
102462306a36Sopenharmony_ci{
102562306a36Sopenharmony_ci	struct ocelot_vcap_filter *tmp;
102662306a36Sopenharmony_ci	int index = 0;
102762306a36Sopenharmony_ci
102862306a36Sopenharmony_ci	list_for_each_entry(tmp, &block->rules, list) {
102962306a36Sopenharmony_ci		if (ocelot_vcap_filter_equal(filter, tmp))
103062306a36Sopenharmony_ci			return index;
103162306a36Sopenharmony_ci		index++;
103262306a36Sopenharmony_ci	}
103362306a36Sopenharmony_ci
103462306a36Sopenharmony_ci	return -ENOENT;
103562306a36Sopenharmony_ci}
103662306a36Sopenharmony_ci
103762306a36Sopenharmony_cistatic struct ocelot_vcap_filter*
103862306a36Sopenharmony_ciocelot_vcap_block_find_filter_by_index(struct ocelot_vcap_block *block,
103962306a36Sopenharmony_ci				       int index)
104062306a36Sopenharmony_ci{
104162306a36Sopenharmony_ci	struct ocelot_vcap_filter *tmp;
104262306a36Sopenharmony_ci	int i = 0;
104362306a36Sopenharmony_ci
104462306a36Sopenharmony_ci	list_for_each_entry(tmp, &block->rules, list) {
104562306a36Sopenharmony_ci		if (i == index)
104662306a36Sopenharmony_ci			return tmp;
104762306a36Sopenharmony_ci		++i;
104862306a36Sopenharmony_ci	}
104962306a36Sopenharmony_ci
105062306a36Sopenharmony_ci	return NULL;
105162306a36Sopenharmony_ci}
105262306a36Sopenharmony_ci
105362306a36Sopenharmony_cistruct ocelot_vcap_filter *
105462306a36Sopenharmony_ciocelot_vcap_block_find_filter_by_id(struct ocelot_vcap_block *block,
105562306a36Sopenharmony_ci				    unsigned long cookie, bool tc_offload)
105662306a36Sopenharmony_ci{
105762306a36Sopenharmony_ci	struct ocelot_vcap_filter *filter;
105862306a36Sopenharmony_ci
105962306a36Sopenharmony_ci	list_for_each_entry(filter, &block->rules, list)
106062306a36Sopenharmony_ci		if (filter->id.tc_offload == tc_offload &&
106162306a36Sopenharmony_ci		    filter->id.cookie == cookie)
106262306a36Sopenharmony_ci			return filter;
106362306a36Sopenharmony_ci
106462306a36Sopenharmony_ci	return NULL;
106562306a36Sopenharmony_ci}
106662306a36Sopenharmony_ciEXPORT_SYMBOL(ocelot_vcap_block_find_filter_by_id);
106762306a36Sopenharmony_ci
106862306a36Sopenharmony_ci/* If @on=false, then SNAP, ARP, IP and OAM frames will not match on keys based
106962306a36Sopenharmony_ci * on destination and source MAC addresses, but only on higher-level protocol
107062306a36Sopenharmony_ci * information. The only frame types to match on keys containing MAC addresses
107162306a36Sopenharmony_ci * in this case are non-SNAP, non-ARP, non-IP and non-OAM frames.
107262306a36Sopenharmony_ci *
107362306a36Sopenharmony_ci * If @on=true, then the above frame types (SNAP, ARP, IP and OAM) will match
107462306a36Sopenharmony_ci * on MAC_ETYPE keys such as destination and source MAC on this ingress port.
107562306a36Sopenharmony_ci * However the setting has the side effect of making these frames not matching
107662306a36Sopenharmony_ci * on any _other_ keys than MAC_ETYPE ones.
107762306a36Sopenharmony_ci */
107862306a36Sopenharmony_cistatic void ocelot_match_all_as_mac_etype(struct ocelot *ocelot, int port,
107962306a36Sopenharmony_ci					  int lookup, bool on)
108062306a36Sopenharmony_ci{
108162306a36Sopenharmony_ci	u32 val = 0;
108262306a36Sopenharmony_ci
108362306a36Sopenharmony_ci	if (on)
108462306a36Sopenharmony_ci		val = ANA_PORT_VCAP_S2_CFG_S2_SNAP_DIS(BIT(lookup)) |
108562306a36Sopenharmony_ci		      ANA_PORT_VCAP_S2_CFG_S2_ARP_DIS(BIT(lookup)) |
108662306a36Sopenharmony_ci		      ANA_PORT_VCAP_S2_CFG_S2_IP_TCPUDP_DIS(BIT(lookup)) |
108762306a36Sopenharmony_ci		      ANA_PORT_VCAP_S2_CFG_S2_IP_OTHER_DIS(BIT(lookup)) |
108862306a36Sopenharmony_ci		      ANA_PORT_VCAP_S2_CFG_S2_OAM_DIS(BIT(lookup));
108962306a36Sopenharmony_ci
109062306a36Sopenharmony_ci	ocelot_rmw_gix(ocelot, val,
109162306a36Sopenharmony_ci		       ANA_PORT_VCAP_S2_CFG_S2_SNAP_DIS(BIT(lookup)) |
109262306a36Sopenharmony_ci		       ANA_PORT_VCAP_S2_CFG_S2_ARP_DIS(BIT(lookup)) |
109362306a36Sopenharmony_ci		       ANA_PORT_VCAP_S2_CFG_S2_IP_TCPUDP_DIS(BIT(lookup)) |
109462306a36Sopenharmony_ci		       ANA_PORT_VCAP_S2_CFG_S2_IP_OTHER_DIS(BIT(lookup)) |
109562306a36Sopenharmony_ci		       ANA_PORT_VCAP_S2_CFG_S2_OAM_DIS(BIT(lookup)),
109662306a36Sopenharmony_ci		       ANA_PORT_VCAP_S2_CFG, port);
109762306a36Sopenharmony_ci}
109862306a36Sopenharmony_ci
109962306a36Sopenharmony_cistatic bool
110062306a36Sopenharmony_ciocelot_vcap_is_problematic_mac_etype(struct ocelot_vcap_filter *filter)
110162306a36Sopenharmony_ci{
110262306a36Sopenharmony_ci	u16 proto, mask;
110362306a36Sopenharmony_ci
110462306a36Sopenharmony_ci	if (filter->key_type != OCELOT_VCAP_KEY_ETYPE)
110562306a36Sopenharmony_ci		return false;
110662306a36Sopenharmony_ci
110762306a36Sopenharmony_ci	proto = ntohs(*(__be16 *)filter->key.etype.etype.value);
110862306a36Sopenharmony_ci	mask = ntohs(*(__be16 *)filter->key.etype.etype.mask);
110962306a36Sopenharmony_ci
111062306a36Sopenharmony_ci	/* ETH_P_ALL match, so all protocols below are included */
111162306a36Sopenharmony_ci	if (mask == 0)
111262306a36Sopenharmony_ci		return true;
111362306a36Sopenharmony_ci	if (proto == ETH_P_ARP)
111462306a36Sopenharmony_ci		return true;
111562306a36Sopenharmony_ci	if (proto == ETH_P_IP)
111662306a36Sopenharmony_ci		return true;
111762306a36Sopenharmony_ci	if (proto == ETH_P_IPV6)
111862306a36Sopenharmony_ci		return true;
111962306a36Sopenharmony_ci
112062306a36Sopenharmony_ci	return false;
112162306a36Sopenharmony_ci}
112262306a36Sopenharmony_ci
112362306a36Sopenharmony_cistatic bool
112462306a36Sopenharmony_ciocelot_vcap_is_problematic_non_mac_etype(struct ocelot_vcap_filter *filter)
112562306a36Sopenharmony_ci{
112662306a36Sopenharmony_ci	if (filter->key_type == OCELOT_VCAP_KEY_SNAP)
112762306a36Sopenharmony_ci		return true;
112862306a36Sopenharmony_ci	if (filter->key_type == OCELOT_VCAP_KEY_ARP)
112962306a36Sopenharmony_ci		return true;
113062306a36Sopenharmony_ci	if (filter->key_type == OCELOT_VCAP_KEY_IPV4)
113162306a36Sopenharmony_ci		return true;
113262306a36Sopenharmony_ci	if (filter->key_type == OCELOT_VCAP_KEY_IPV6)
113362306a36Sopenharmony_ci		return true;
113462306a36Sopenharmony_ci	return false;
113562306a36Sopenharmony_ci}
113662306a36Sopenharmony_ci
113762306a36Sopenharmony_cistatic bool
113862306a36Sopenharmony_ciocelot_exclusive_mac_etype_filter_rules(struct ocelot *ocelot,
113962306a36Sopenharmony_ci					struct ocelot_vcap_filter *filter)
114062306a36Sopenharmony_ci{
114162306a36Sopenharmony_ci	struct ocelot_vcap_block *block = &ocelot->block[filter->block_id];
114262306a36Sopenharmony_ci	struct ocelot_vcap_filter *tmp;
114362306a36Sopenharmony_ci	unsigned long port;
114462306a36Sopenharmony_ci	int i;
114562306a36Sopenharmony_ci
114662306a36Sopenharmony_ci	/* We only have the S2_IP_TCPUDP_DIS set of knobs for VCAP IS2 */
114762306a36Sopenharmony_ci	if (filter->block_id != VCAP_IS2)
114862306a36Sopenharmony_ci		return true;
114962306a36Sopenharmony_ci
115062306a36Sopenharmony_ci	if (ocelot_vcap_is_problematic_mac_etype(filter)) {
115162306a36Sopenharmony_ci		/* Search for any non-MAC_ETYPE rules on the port */
115262306a36Sopenharmony_ci		for (i = 0; i < block->count; i++) {
115362306a36Sopenharmony_ci			tmp = ocelot_vcap_block_find_filter_by_index(block, i);
115462306a36Sopenharmony_ci			if (tmp->ingress_port_mask & filter->ingress_port_mask &&
115562306a36Sopenharmony_ci			    tmp->lookup == filter->lookup &&
115662306a36Sopenharmony_ci			    ocelot_vcap_is_problematic_non_mac_etype(tmp))
115762306a36Sopenharmony_ci				return false;
115862306a36Sopenharmony_ci		}
115962306a36Sopenharmony_ci
116062306a36Sopenharmony_ci		for_each_set_bit(port, &filter->ingress_port_mask,
116162306a36Sopenharmony_ci				 ocelot->num_phys_ports)
116262306a36Sopenharmony_ci			ocelot_match_all_as_mac_etype(ocelot, port,
116362306a36Sopenharmony_ci						      filter->lookup, true);
116462306a36Sopenharmony_ci	} else if (ocelot_vcap_is_problematic_non_mac_etype(filter)) {
116562306a36Sopenharmony_ci		/* Search for any MAC_ETYPE rules on the port */
116662306a36Sopenharmony_ci		for (i = 0; i < block->count; i++) {
116762306a36Sopenharmony_ci			tmp = ocelot_vcap_block_find_filter_by_index(block, i);
116862306a36Sopenharmony_ci			if (tmp->ingress_port_mask & filter->ingress_port_mask &&
116962306a36Sopenharmony_ci			    tmp->lookup == filter->lookup &&
117062306a36Sopenharmony_ci			    ocelot_vcap_is_problematic_mac_etype(tmp))
117162306a36Sopenharmony_ci				return false;
117262306a36Sopenharmony_ci		}
117362306a36Sopenharmony_ci
117462306a36Sopenharmony_ci		for_each_set_bit(port, &filter->ingress_port_mask,
117562306a36Sopenharmony_ci				 ocelot->num_phys_ports)
117662306a36Sopenharmony_ci			ocelot_match_all_as_mac_etype(ocelot, port,
117762306a36Sopenharmony_ci						      filter->lookup, false);
117862306a36Sopenharmony_ci	}
117962306a36Sopenharmony_ci
118062306a36Sopenharmony_ci	return true;
118162306a36Sopenharmony_ci}
118262306a36Sopenharmony_ci
118362306a36Sopenharmony_ciint ocelot_vcap_filter_add(struct ocelot *ocelot,
118462306a36Sopenharmony_ci			   struct ocelot_vcap_filter *filter,
118562306a36Sopenharmony_ci			   struct netlink_ext_ack *extack)
118662306a36Sopenharmony_ci{
118762306a36Sopenharmony_ci	struct ocelot_vcap_block *block = &ocelot->block[filter->block_id];
118862306a36Sopenharmony_ci	int i, index, ret;
118962306a36Sopenharmony_ci
119062306a36Sopenharmony_ci	if (!ocelot_exclusive_mac_etype_filter_rules(ocelot, filter)) {
119162306a36Sopenharmony_ci		NL_SET_ERR_MSG_MOD(extack,
119262306a36Sopenharmony_ci				   "Cannot mix MAC_ETYPE with non-MAC_ETYPE rules, use the other IS2 lookup");
119362306a36Sopenharmony_ci		return -EBUSY;
119462306a36Sopenharmony_ci	}
119562306a36Sopenharmony_ci
119662306a36Sopenharmony_ci	/* Add filter to the linked list */
119762306a36Sopenharmony_ci	ret = ocelot_vcap_filter_add_to_block(ocelot, block, filter, extack);
119862306a36Sopenharmony_ci	if (ret)
119962306a36Sopenharmony_ci		return ret;
120062306a36Sopenharmony_ci
120162306a36Sopenharmony_ci	/* Get the index of the inserted filter */
120262306a36Sopenharmony_ci	index = ocelot_vcap_block_get_filter_index(block, filter);
120362306a36Sopenharmony_ci	if (index < 0)
120462306a36Sopenharmony_ci		return index;
120562306a36Sopenharmony_ci
120662306a36Sopenharmony_ci	/* Move down the rules to make place for the new filter */
120762306a36Sopenharmony_ci	for (i = block->count - 1; i > index; i--) {
120862306a36Sopenharmony_ci		struct ocelot_vcap_filter *tmp;
120962306a36Sopenharmony_ci
121062306a36Sopenharmony_ci		tmp = ocelot_vcap_block_find_filter_by_index(block, i);
121162306a36Sopenharmony_ci		/* Read back the filter's counters before moving it */
121262306a36Sopenharmony_ci		vcap_entry_get(ocelot, i - 1, tmp);
121362306a36Sopenharmony_ci		vcap_entry_set(ocelot, i, tmp);
121462306a36Sopenharmony_ci	}
121562306a36Sopenharmony_ci
121662306a36Sopenharmony_ci	/* Now insert the new filter */
121762306a36Sopenharmony_ci	vcap_entry_set(ocelot, index, filter);
121862306a36Sopenharmony_ci	return 0;
121962306a36Sopenharmony_ci}
122062306a36Sopenharmony_ciEXPORT_SYMBOL(ocelot_vcap_filter_add);
122162306a36Sopenharmony_ci
122262306a36Sopenharmony_cistatic void ocelot_vcap_block_remove_filter(struct ocelot *ocelot,
122362306a36Sopenharmony_ci					    struct ocelot_vcap_block *block,
122462306a36Sopenharmony_ci					    struct ocelot_vcap_filter *filter)
122562306a36Sopenharmony_ci{
122662306a36Sopenharmony_ci	struct ocelot_vcap_filter *tmp, *n;
122762306a36Sopenharmony_ci
122862306a36Sopenharmony_ci	list_for_each_entry_safe(tmp, n, &block->rules, list) {
122962306a36Sopenharmony_ci		if (ocelot_vcap_filter_equal(filter, tmp)) {
123062306a36Sopenharmony_ci			ocelot_vcap_filter_del_aux_resources(ocelot, tmp);
123162306a36Sopenharmony_ci			list_del(&tmp->list);
123262306a36Sopenharmony_ci			kfree(tmp);
123362306a36Sopenharmony_ci		}
123462306a36Sopenharmony_ci	}
123562306a36Sopenharmony_ci
123662306a36Sopenharmony_ci	block->count--;
123762306a36Sopenharmony_ci}
123862306a36Sopenharmony_ci
123962306a36Sopenharmony_ciint ocelot_vcap_filter_del(struct ocelot *ocelot,
124062306a36Sopenharmony_ci			   struct ocelot_vcap_filter *filter)
124162306a36Sopenharmony_ci{
124262306a36Sopenharmony_ci	struct ocelot_vcap_block *block = &ocelot->block[filter->block_id];
124362306a36Sopenharmony_ci	struct ocelot_vcap_filter del_filter;
124462306a36Sopenharmony_ci	int i, index;
124562306a36Sopenharmony_ci
124662306a36Sopenharmony_ci	/* Need to inherit the block_id so that vcap_entry_set()
124762306a36Sopenharmony_ci	 * does not get confused and knows where to install it.
124862306a36Sopenharmony_ci	 */
124962306a36Sopenharmony_ci	memset(&del_filter, 0, sizeof(del_filter));
125062306a36Sopenharmony_ci	del_filter.block_id = filter->block_id;
125162306a36Sopenharmony_ci
125262306a36Sopenharmony_ci	/* Gets index of the filter */
125362306a36Sopenharmony_ci	index = ocelot_vcap_block_get_filter_index(block, filter);
125462306a36Sopenharmony_ci	if (index < 0)
125562306a36Sopenharmony_ci		return index;
125662306a36Sopenharmony_ci
125762306a36Sopenharmony_ci	/* Delete filter */
125862306a36Sopenharmony_ci	ocelot_vcap_block_remove_filter(ocelot, block, filter);
125962306a36Sopenharmony_ci
126062306a36Sopenharmony_ci	/* Move up all the blocks over the deleted filter */
126162306a36Sopenharmony_ci	for (i = index; i < block->count; i++) {
126262306a36Sopenharmony_ci		struct ocelot_vcap_filter *tmp;
126362306a36Sopenharmony_ci
126462306a36Sopenharmony_ci		tmp = ocelot_vcap_block_find_filter_by_index(block, i);
126562306a36Sopenharmony_ci		/* Read back the filter's counters before moving it */
126662306a36Sopenharmony_ci		vcap_entry_get(ocelot, i + 1, tmp);
126762306a36Sopenharmony_ci		vcap_entry_set(ocelot, i, tmp);
126862306a36Sopenharmony_ci	}
126962306a36Sopenharmony_ci
127062306a36Sopenharmony_ci	/* Now delete the last filter, because it is duplicated */
127162306a36Sopenharmony_ci	vcap_entry_set(ocelot, block->count, &del_filter);
127262306a36Sopenharmony_ci
127362306a36Sopenharmony_ci	return 0;
127462306a36Sopenharmony_ci}
127562306a36Sopenharmony_ciEXPORT_SYMBOL(ocelot_vcap_filter_del);
127662306a36Sopenharmony_ci
127762306a36Sopenharmony_ciint ocelot_vcap_filter_replace(struct ocelot *ocelot,
127862306a36Sopenharmony_ci			       struct ocelot_vcap_filter *filter)
127962306a36Sopenharmony_ci{
128062306a36Sopenharmony_ci	struct ocelot_vcap_block *block = &ocelot->block[filter->block_id];
128162306a36Sopenharmony_ci	int index;
128262306a36Sopenharmony_ci
128362306a36Sopenharmony_ci	index = ocelot_vcap_block_get_filter_index(block, filter);
128462306a36Sopenharmony_ci	if (index < 0)
128562306a36Sopenharmony_ci		return index;
128662306a36Sopenharmony_ci
128762306a36Sopenharmony_ci	vcap_entry_set(ocelot, index, filter);
128862306a36Sopenharmony_ci
128962306a36Sopenharmony_ci	return 0;
129062306a36Sopenharmony_ci}
129162306a36Sopenharmony_ciEXPORT_SYMBOL(ocelot_vcap_filter_replace);
129262306a36Sopenharmony_ci
129362306a36Sopenharmony_ciint ocelot_vcap_filter_stats_update(struct ocelot *ocelot,
129462306a36Sopenharmony_ci				    struct ocelot_vcap_filter *filter)
129562306a36Sopenharmony_ci{
129662306a36Sopenharmony_ci	struct ocelot_vcap_block *block = &ocelot->block[filter->block_id];
129762306a36Sopenharmony_ci	struct ocelot_vcap_filter tmp;
129862306a36Sopenharmony_ci	int index;
129962306a36Sopenharmony_ci
130062306a36Sopenharmony_ci	index = ocelot_vcap_block_get_filter_index(block, filter);
130162306a36Sopenharmony_ci	if (index < 0)
130262306a36Sopenharmony_ci		return index;
130362306a36Sopenharmony_ci
130462306a36Sopenharmony_ci	vcap_entry_get(ocelot, index, filter);
130562306a36Sopenharmony_ci
130662306a36Sopenharmony_ci	/* After we get the result we need to clear the counters */
130762306a36Sopenharmony_ci	tmp = *filter;
130862306a36Sopenharmony_ci	tmp.stats.pkts = 0;
130962306a36Sopenharmony_ci	vcap_entry_set(ocelot, index, &tmp);
131062306a36Sopenharmony_ci
131162306a36Sopenharmony_ci	return 0;
131262306a36Sopenharmony_ci}
131362306a36Sopenharmony_ci
131462306a36Sopenharmony_cistatic void ocelot_vcap_init_one(struct ocelot *ocelot,
131562306a36Sopenharmony_ci				 const struct vcap_props *vcap)
131662306a36Sopenharmony_ci{
131762306a36Sopenharmony_ci	struct vcap_data data;
131862306a36Sopenharmony_ci
131962306a36Sopenharmony_ci	memset(&data, 0, sizeof(data));
132062306a36Sopenharmony_ci
132162306a36Sopenharmony_ci	vcap_entry2cache(ocelot, vcap, &data);
132262306a36Sopenharmony_ci	ocelot_target_write(ocelot, vcap->target, vcap->entry_count,
132362306a36Sopenharmony_ci			    VCAP_CORE_MV_CFG);
132462306a36Sopenharmony_ci	vcap_cmd(ocelot, vcap, 0, VCAP_CMD_INITIALIZE, VCAP_SEL_ENTRY);
132562306a36Sopenharmony_ci
132662306a36Sopenharmony_ci	vcap_action2cache(ocelot, vcap, &data);
132762306a36Sopenharmony_ci	ocelot_target_write(ocelot, vcap->target, vcap->action_count,
132862306a36Sopenharmony_ci			    VCAP_CORE_MV_CFG);
132962306a36Sopenharmony_ci	vcap_cmd(ocelot, vcap, 0, VCAP_CMD_INITIALIZE,
133062306a36Sopenharmony_ci		 VCAP_SEL_ACTION | VCAP_SEL_COUNTER);
133162306a36Sopenharmony_ci}
133262306a36Sopenharmony_ci
133362306a36Sopenharmony_cistatic void ocelot_vcap_detect_constants(struct ocelot *ocelot,
133462306a36Sopenharmony_ci					 struct vcap_props *vcap)
133562306a36Sopenharmony_ci{
133662306a36Sopenharmony_ci	int counter_memory_width;
133762306a36Sopenharmony_ci	int num_default_actions;
133862306a36Sopenharmony_ci	int version;
133962306a36Sopenharmony_ci
134062306a36Sopenharmony_ci	version = ocelot_target_read(ocelot, vcap->target,
134162306a36Sopenharmony_ci				     VCAP_CONST_VCAP_VER);
134262306a36Sopenharmony_ci	/* Only version 0 VCAP supported for now */
134362306a36Sopenharmony_ci	if (WARN_ON(version != 0))
134462306a36Sopenharmony_ci		return;
134562306a36Sopenharmony_ci
134662306a36Sopenharmony_ci	/* Width in bits of type-group field */
134762306a36Sopenharmony_ci	vcap->tg_width = ocelot_target_read(ocelot, vcap->target,
134862306a36Sopenharmony_ci					    VCAP_CONST_ENTRY_TG_WIDTH);
134962306a36Sopenharmony_ci	/* Number of subwords per TCAM row */
135062306a36Sopenharmony_ci	vcap->sw_count = ocelot_target_read(ocelot, vcap->target,
135162306a36Sopenharmony_ci					    VCAP_CONST_ENTRY_SWCNT);
135262306a36Sopenharmony_ci	/* Number of rows in TCAM. There can be this many full keys, or double
135362306a36Sopenharmony_ci	 * this number half keys, or 4 times this number quarter keys.
135462306a36Sopenharmony_ci	 */
135562306a36Sopenharmony_ci	vcap->entry_count = ocelot_target_read(ocelot, vcap->target,
135662306a36Sopenharmony_ci					       VCAP_CONST_ENTRY_CNT);
135762306a36Sopenharmony_ci	/* Assuming there are 4 subwords per TCAM row, their layout in the
135862306a36Sopenharmony_ci	 * actual TCAM (not in the cache) would be:
135962306a36Sopenharmony_ci	 *
136062306a36Sopenharmony_ci	 * |  SW 3  | TG 3 |  SW 2  | TG 2 |  SW 1  | TG 1 |  SW 0  | TG 0 |
136162306a36Sopenharmony_ci	 *
136262306a36Sopenharmony_ci	 * (where SW=subword and TG=Type-Group).
136362306a36Sopenharmony_ci	 *
136462306a36Sopenharmony_ci	 * What VCAP_CONST_ENTRY_CNT is giving us is the width of one full TCAM
136562306a36Sopenharmony_ci	 * row. But when software accesses the TCAM through the cache
136662306a36Sopenharmony_ci	 * registers, the Type-Group values are written through another set of
136762306a36Sopenharmony_ci	 * registers VCAP_TG_DAT, and therefore, it appears as though the 4
136862306a36Sopenharmony_ci	 * subwords are contiguous in the cache memory.
136962306a36Sopenharmony_ci	 * Important mention: regardless of the number of key entries per row
137062306a36Sopenharmony_ci	 * (and therefore of key size: 1 full key or 2 half keys or 4 quarter
137162306a36Sopenharmony_ci	 * keys), software always has to configure 4 Type-Group values. For
137262306a36Sopenharmony_ci	 * example, in the case of 1 full key, the driver needs to set all 4
137362306a36Sopenharmony_ci	 * Type-Group to be full key.
137462306a36Sopenharmony_ci	 *
137562306a36Sopenharmony_ci	 * For this reason, we need to fix up the value that the hardware is
137662306a36Sopenharmony_ci	 * giving us. We don't actually care about the width of the entry in
137762306a36Sopenharmony_ci	 * the TCAM. What we care about is the width of the entry in the cache
137862306a36Sopenharmony_ci	 * registers, which is how we get to interact with it. And since the
137962306a36Sopenharmony_ci	 * VCAP_ENTRY_DAT cache registers access only the subwords and not the
138062306a36Sopenharmony_ci	 * Type-Groups, this means we need to subtract the width of the
138162306a36Sopenharmony_ci	 * Type-Groups when packing and unpacking key entry data in a TCAM row.
138262306a36Sopenharmony_ci	 */
138362306a36Sopenharmony_ci	vcap->entry_width = ocelot_target_read(ocelot, vcap->target,
138462306a36Sopenharmony_ci					       VCAP_CONST_ENTRY_WIDTH);
138562306a36Sopenharmony_ci	vcap->entry_width -= vcap->tg_width * vcap->sw_count;
138662306a36Sopenharmony_ci	num_default_actions = ocelot_target_read(ocelot, vcap->target,
138762306a36Sopenharmony_ci						 VCAP_CONST_ACTION_DEF_CNT);
138862306a36Sopenharmony_ci	vcap->action_count = vcap->entry_count + num_default_actions;
138962306a36Sopenharmony_ci	vcap->action_width = ocelot_target_read(ocelot, vcap->target,
139062306a36Sopenharmony_ci						VCAP_CONST_ACTION_WIDTH);
139162306a36Sopenharmony_ci	/* The width of the counter memory, this is the complete width of all
139262306a36Sopenharmony_ci	 * counter-fields associated with one full-word entry. There is one
139362306a36Sopenharmony_ci	 * counter per entry sub-word (see CAP_CORE::ENTRY_SWCNT for number of
139462306a36Sopenharmony_ci	 * subwords.)
139562306a36Sopenharmony_ci	 */
139662306a36Sopenharmony_ci	vcap->counter_words = vcap->sw_count;
139762306a36Sopenharmony_ci	counter_memory_width = ocelot_target_read(ocelot, vcap->target,
139862306a36Sopenharmony_ci						  VCAP_CONST_CNT_WIDTH);
139962306a36Sopenharmony_ci	vcap->counter_width = counter_memory_width / vcap->counter_words;
140062306a36Sopenharmony_ci}
140162306a36Sopenharmony_ci
140262306a36Sopenharmony_ciint ocelot_vcap_init(struct ocelot *ocelot)
140362306a36Sopenharmony_ci{
140462306a36Sopenharmony_ci	struct qos_policer_conf cpu_drop = {
140562306a36Sopenharmony_ci		.mode = MSCC_QOS_RATE_MODE_DATA,
140662306a36Sopenharmony_ci	};
140762306a36Sopenharmony_ci	int ret, i;
140862306a36Sopenharmony_ci
140962306a36Sopenharmony_ci	/* Create a policer that will drop the frames for the cpu.
141062306a36Sopenharmony_ci	 * This policer will be used as action in the acl rules to drop
141162306a36Sopenharmony_ci	 * frames.
141262306a36Sopenharmony_ci	 */
141362306a36Sopenharmony_ci	ret = qos_policer_conf_set(ocelot, OCELOT_POLICER_DISCARD, &cpu_drop);
141462306a36Sopenharmony_ci	if (ret)
141562306a36Sopenharmony_ci		return ret;
141662306a36Sopenharmony_ci
141762306a36Sopenharmony_ci	for (i = 0; i < OCELOT_NUM_VCAP_BLOCKS; i++) {
141862306a36Sopenharmony_ci		struct ocelot_vcap_block *block = &ocelot->block[i];
141962306a36Sopenharmony_ci		struct vcap_props *vcap = &ocelot->vcap[i];
142062306a36Sopenharmony_ci
142162306a36Sopenharmony_ci		INIT_LIST_HEAD(&block->rules);
142262306a36Sopenharmony_ci
142362306a36Sopenharmony_ci		ocelot_vcap_detect_constants(ocelot, vcap);
142462306a36Sopenharmony_ci		ocelot_vcap_init_one(ocelot, vcap);
142562306a36Sopenharmony_ci	}
142662306a36Sopenharmony_ci
142762306a36Sopenharmony_ci	INIT_LIST_HEAD(&ocelot->dummy_rules);
142862306a36Sopenharmony_ci	INIT_LIST_HEAD(&ocelot->traps);
142962306a36Sopenharmony_ci	INIT_LIST_HEAD(&ocelot->vcap_pol.pol_list);
143062306a36Sopenharmony_ci
143162306a36Sopenharmony_ci	return 0;
143262306a36Sopenharmony_ci}
1433