13c83654fSVladimir Oltean // SPDX-License-Identifier: (GPL-2.0 OR MIT)
23c83654fSVladimir Oltean /* Microsemi Ocelot Switch driver
33c83654fSVladimir Oltean  * Copyright (c) 2019 Microsemi Corporation
43c83654fSVladimir Oltean  */
53c83654fSVladimir Oltean 
63c83654fSVladimir Oltean #include <linux/iopoll.h>
73c83654fSVladimir Oltean #include <linux/proc_fs.h>
83c83654fSVladimir Oltean 
93c83654fSVladimir Oltean #include <soc/mscc/ocelot_vcap.h>
103c83654fSVladimir Oltean #include "ocelot_police.h"
113c83654fSVladimir Oltean #include "ocelot_vcap.h"
123c83654fSVladimir Oltean 
133c83654fSVladimir Oltean #define ENTRY_WIDTH 32
143c83654fSVladimir Oltean 
153c83654fSVladimir Oltean enum vcap_sel {
163c83654fSVladimir Oltean 	VCAP_SEL_ENTRY = 0x1,
173c83654fSVladimir Oltean 	VCAP_SEL_ACTION = 0x2,
183c83654fSVladimir Oltean 	VCAP_SEL_COUNTER = 0x4,
193c83654fSVladimir Oltean 	VCAP_SEL_ALL = 0x7,
203c83654fSVladimir Oltean };
213c83654fSVladimir Oltean 
223c83654fSVladimir Oltean enum vcap_cmd {
233c83654fSVladimir Oltean 	VCAP_CMD_WRITE = 0, /* Copy from Cache to TCAM */
243c83654fSVladimir Oltean 	VCAP_CMD_READ = 1, /* Copy from TCAM to Cache */
253c83654fSVladimir Oltean 	VCAP_CMD_MOVE_UP = 2, /* Move <count> up */
263c83654fSVladimir Oltean 	VCAP_CMD_MOVE_DOWN = 3, /* Move <count> down */
273c83654fSVladimir Oltean 	VCAP_CMD_INITIALIZE = 4, /* Write all (from cache) */
283c83654fSVladimir Oltean };
293c83654fSVladimir Oltean 
303c83654fSVladimir Oltean #define VCAP_ENTRY_WIDTH 12 /* Max entry width (32bit words) */
313c83654fSVladimir Oltean #define VCAP_COUNTER_WIDTH 4 /* Max counter width (32bit words) */
323c83654fSVladimir Oltean 
333c83654fSVladimir Oltean struct vcap_data {
343c83654fSVladimir Oltean 	u32 entry[VCAP_ENTRY_WIDTH]; /* ENTRY_DAT */
353c83654fSVladimir Oltean 	u32 mask[VCAP_ENTRY_WIDTH]; /* MASK_DAT */
363c83654fSVladimir Oltean 	u32 action[VCAP_ENTRY_WIDTH]; /* ACTION_DAT */
373c83654fSVladimir Oltean 	u32 counter[VCAP_COUNTER_WIDTH]; /* CNT_DAT */
383c83654fSVladimir Oltean 	u32 tg; /* TG_DAT */
393c83654fSVladimir Oltean 	u32 type; /* Action type */
403c83654fSVladimir Oltean 	u32 tg_sw; /* Current type-group */
413c83654fSVladimir Oltean 	u32 cnt; /* Current counter */
423c83654fSVladimir Oltean 	u32 key_offset; /* Current entry offset */
433c83654fSVladimir Oltean 	u32 action_offset; /* Current action offset */
443c83654fSVladimir Oltean 	u32 counter_offset; /* Current counter offset */
453c83654fSVladimir Oltean 	u32 tg_value; /* Current type-group value */
463c83654fSVladimir Oltean 	u32 tg_mask; /* Current type-group mask */
473c83654fSVladimir Oltean };
483c83654fSVladimir Oltean 
49c1c3993eSVladimir Oltean static u32 vcap_read_update_ctrl(struct ocelot *ocelot,
50c1c3993eSVladimir Oltean 				 const struct vcap_props *vcap)
513c83654fSVladimir Oltean {
52c1c3993eSVladimir Oltean 	return ocelot_target_read(ocelot, vcap->target, VCAP_CORE_UPDATE_CTRL);
533c83654fSVladimir Oltean }
543c83654fSVladimir Oltean 
55c1c3993eSVladimir Oltean static void vcap_cmd(struct ocelot *ocelot, const struct vcap_props *vcap,
56c1c3993eSVladimir Oltean 		     u16 ix, int cmd, int sel)
573c83654fSVladimir Oltean {
58c1c3993eSVladimir Oltean 	u32 value = (VCAP_CORE_UPDATE_CTRL_UPDATE_CMD(cmd) |
59c1c3993eSVladimir Oltean 		     VCAP_CORE_UPDATE_CTRL_UPDATE_ADDR(ix) |
60c1c3993eSVladimir Oltean 		     VCAP_CORE_UPDATE_CTRL_UPDATE_SHOT);
613c83654fSVladimir Oltean 
62c1c3993eSVladimir Oltean 	if ((sel & VCAP_SEL_ENTRY) && ix >= vcap->entry_count)
633c83654fSVladimir Oltean 		return;
643c83654fSVladimir Oltean 
653c83654fSVladimir Oltean 	if (!(sel & VCAP_SEL_ENTRY))
66c1c3993eSVladimir Oltean 		value |= VCAP_CORE_UPDATE_CTRL_UPDATE_ENTRY_DIS;
673c83654fSVladimir Oltean 
683c83654fSVladimir Oltean 	if (!(sel & VCAP_SEL_ACTION))
69c1c3993eSVladimir Oltean 		value |= VCAP_CORE_UPDATE_CTRL_UPDATE_ACTION_DIS;
703c83654fSVladimir Oltean 
713c83654fSVladimir Oltean 	if (!(sel & VCAP_SEL_COUNTER))
72c1c3993eSVladimir Oltean 		value |= VCAP_CORE_UPDATE_CTRL_UPDATE_CNT_DIS;
733c83654fSVladimir Oltean 
74c1c3993eSVladimir Oltean 	ocelot_target_write(ocelot, vcap->target, value, VCAP_CORE_UPDATE_CTRL);
75c1c3993eSVladimir Oltean 
76c1c3993eSVladimir Oltean 	read_poll_timeout(vcap_read_update_ctrl, value,
77c1c3993eSVladimir Oltean 			  (value & VCAP_CORE_UPDATE_CTRL_UPDATE_SHOT) == 0,
78c1c3993eSVladimir Oltean 			  10, 100000, false, ocelot, vcap);
793c83654fSVladimir Oltean }
803c83654fSVladimir Oltean 
813c83654fSVladimir Oltean /* Convert from 0-based row to VCAP entry row and run command */
82c1c3993eSVladimir Oltean static void vcap_row_cmd(struct ocelot *ocelot, const struct vcap_props *vcap,
83c1c3993eSVladimir Oltean 			 u32 row, int cmd, int sel)
843c83654fSVladimir Oltean {
85c1c3993eSVladimir Oltean 	vcap_cmd(ocelot, vcap, vcap->entry_count - row - 1, cmd, sel);
863c83654fSVladimir Oltean }
873c83654fSVladimir Oltean 
88c1c3993eSVladimir Oltean static void vcap_entry2cache(struct ocelot *ocelot,
89c1c3993eSVladimir Oltean 			     const struct vcap_props *vcap,
90c1c3993eSVladimir Oltean 			     struct vcap_data *data)
913c83654fSVladimir Oltean {
923c83654fSVladimir Oltean 	u32 entry_words, i;
933c83654fSVladimir Oltean 
94c1c3993eSVladimir Oltean 	entry_words = DIV_ROUND_UP(vcap->entry_width, ENTRY_WIDTH);
953c83654fSVladimir Oltean 
963c83654fSVladimir Oltean 	for (i = 0; i < entry_words; i++) {
97c1c3993eSVladimir Oltean 		ocelot_target_write_rix(ocelot, vcap->target, data->entry[i],
98c1c3993eSVladimir Oltean 					VCAP_CACHE_ENTRY_DAT, i);
99c1c3993eSVladimir Oltean 		ocelot_target_write_rix(ocelot, vcap->target, ~data->mask[i],
100c1c3993eSVladimir Oltean 					VCAP_CACHE_MASK_DAT, i);
1013c83654fSVladimir Oltean 	}
102c1c3993eSVladimir Oltean 	ocelot_target_write(ocelot, vcap->target, data->tg, VCAP_CACHE_TG_DAT);
1033c83654fSVladimir Oltean }
1043c83654fSVladimir Oltean 
105c1c3993eSVladimir Oltean static void vcap_cache2entry(struct ocelot *ocelot,
106c1c3993eSVladimir Oltean 			     const struct vcap_props *vcap,
107c1c3993eSVladimir Oltean 			     struct vcap_data *data)
1083c83654fSVladimir Oltean {
1093c83654fSVladimir Oltean 	u32 entry_words, i;
1103c83654fSVladimir Oltean 
111c1c3993eSVladimir Oltean 	entry_words = DIV_ROUND_UP(vcap->entry_width, ENTRY_WIDTH);
1123c83654fSVladimir Oltean 
1133c83654fSVladimir Oltean 	for (i = 0; i < entry_words; i++) {
114c1c3993eSVladimir Oltean 		data->entry[i] = ocelot_target_read_rix(ocelot, vcap->target,
115c1c3993eSVladimir Oltean 							VCAP_CACHE_ENTRY_DAT, i);
1163c83654fSVladimir Oltean 		// Invert mask
117c1c3993eSVladimir Oltean 		data->mask[i] = ~ocelot_target_read_rix(ocelot, vcap->target,
118c1c3993eSVladimir Oltean 							VCAP_CACHE_MASK_DAT, i);
1193c83654fSVladimir Oltean 	}
120c1c3993eSVladimir Oltean 	data->tg = ocelot_target_read(ocelot, vcap->target, VCAP_CACHE_TG_DAT);
1213c83654fSVladimir Oltean }
1223c83654fSVladimir Oltean 
123c1c3993eSVladimir Oltean static void vcap_action2cache(struct ocelot *ocelot,
124c1c3993eSVladimir Oltean 			      const struct vcap_props *vcap,
125c1c3993eSVladimir Oltean 			      struct vcap_data *data)
1263c83654fSVladimir Oltean {
1273c83654fSVladimir Oltean 	u32 action_words, mask;
1283c83654fSVladimir Oltean 	int i, width;
1293c83654fSVladimir Oltean 
1303c83654fSVladimir Oltean 	/* Encode action type */
131c1c3993eSVladimir Oltean 	width = vcap->action_type_width;
1323c83654fSVladimir Oltean 	if (width) {
1333c83654fSVladimir Oltean 		mask = GENMASK(width, 0);
1343c83654fSVladimir Oltean 		data->action[0] = ((data->action[0] & ~mask) | data->type);
1353c83654fSVladimir Oltean 	}
1363c83654fSVladimir Oltean 
137c1c3993eSVladimir Oltean 	action_words = DIV_ROUND_UP(vcap->action_width, ENTRY_WIDTH);
1383c83654fSVladimir Oltean 
1393c83654fSVladimir Oltean 	for (i = 0; i < action_words; i++)
140c1c3993eSVladimir Oltean 		ocelot_target_write_rix(ocelot, vcap->target, data->action[i],
141c1c3993eSVladimir Oltean 					VCAP_CACHE_ACTION_DAT, i);
1423c83654fSVladimir Oltean 
143c1c3993eSVladimir Oltean 	for (i = 0; i < vcap->counter_words; i++)
144c1c3993eSVladimir Oltean 		ocelot_target_write_rix(ocelot, vcap->target, data->counter[i],
145c1c3993eSVladimir Oltean 					VCAP_CACHE_CNT_DAT, i);
1463c83654fSVladimir Oltean }
1473c83654fSVladimir Oltean 
148c1c3993eSVladimir Oltean static void vcap_cache2action(struct ocelot *ocelot,
149c1c3993eSVladimir Oltean 			      const struct vcap_props *vcap,
150c1c3993eSVladimir Oltean 			      struct vcap_data *data)
1513c83654fSVladimir Oltean {
1523c83654fSVladimir Oltean 	u32 action_words;
1533c83654fSVladimir Oltean 	int i, width;
1543c83654fSVladimir Oltean 
155c1c3993eSVladimir Oltean 	action_words = DIV_ROUND_UP(vcap->action_width, ENTRY_WIDTH);
1563c83654fSVladimir Oltean 
1573c83654fSVladimir Oltean 	for (i = 0; i < action_words; i++)
158c1c3993eSVladimir Oltean 		data->action[i] = ocelot_target_read_rix(ocelot, vcap->target,
159c1c3993eSVladimir Oltean 							 VCAP_CACHE_ACTION_DAT,
1603c83654fSVladimir Oltean 							 i);
1613c83654fSVladimir Oltean 
162c1c3993eSVladimir Oltean 	for (i = 0; i < vcap->counter_words; i++)
163c1c3993eSVladimir Oltean 		data->counter[i] = ocelot_target_read_rix(ocelot, vcap->target,
164c1c3993eSVladimir Oltean 							  VCAP_CACHE_CNT_DAT,
165c1c3993eSVladimir Oltean 							  i);
1663c83654fSVladimir Oltean 
1673c83654fSVladimir Oltean 	/* Extract action type */
168c1c3993eSVladimir Oltean 	width = vcap->action_type_width;
1693c83654fSVladimir Oltean 	data->type = (width ? (data->action[0] & GENMASK(width, 0)) : 0);
1703c83654fSVladimir Oltean }
1713c83654fSVladimir Oltean 
1723c83654fSVladimir Oltean /* Calculate offsets for entry */
173c1c3993eSVladimir Oltean static void vcap_data_offset_get(const struct vcap_props *vcap,
174c1c3993eSVladimir Oltean 				 struct vcap_data *data, int ix)
1753c83654fSVladimir Oltean {
17664294263SVladimir Oltean 	int num_subwords_per_entry, num_subwords_per_action;
17764294263SVladimir Oltean 	int i, col, offset, num_entries_per_row, base;
178c1c3993eSVladimir Oltean 	u32 width = vcap->tg_width;
1793c83654fSVladimir Oltean 
180e6ae7c50SXiaoliang Yang 	switch (data->tg_sw) {
181e6ae7c50SXiaoliang Yang 	case VCAP_TG_FULL:
1825963083aSVladimir Oltean 		num_entries_per_row = 1;
183e6ae7c50SXiaoliang Yang 		break;
184e6ae7c50SXiaoliang Yang 	case VCAP_TG_HALF:
1855963083aSVladimir Oltean 		num_entries_per_row = 2;
186e6ae7c50SXiaoliang Yang 		break;
187e6ae7c50SXiaoliang Yang 	case VCAP_TG_QUARTER:
1885963083aSVladimir Oltean 		num_entries_per_row = 4;
189e6ae7c50SXiaoliang Yang 		break;
190e6ae7c50SXiaoliang Yang 	default:
191e6ae7c50SXiaoliang Yang 		return;
192e6ae7c50SXiaoliang Yang 	}
193e6ae7c50SXiaoliang Yang 
1945963083aSVladimir Oltean 	col = (ix % num_entries_per_row);
19564294263SVladimir Oltean 	num_subwords_per_entry = (vcap->sw_count / num_entries_per_row);
19664294263SVladimir Oltean 	base = (vcap->sw_count - col * num_subwords_per_entry -
19764294263SVladimir Oltean 		num_subwords_per_entry);
1983c83654fSVladimir Oltean 	data->tg_value = 0;
1993c83654fSVladimir Oltean 	data->tg_mask = 0;
20064294263SVladimir Oltean 	for (i = 0; i < num_subwords_per_entry; i++) {
2013c83654fSVladimir Oltean 		offset = ((base + i) * width);
2023c83654fSVladimir Oltean 		data->tg_value |= (data->tg_sw << offset);
2033c83654fSVladimir Oltean 		data->tg_mask |= GENMASK(offset + width - 1, offset);
2043c83654fSVladimir Oltean 	}
2053c83654fSVladimir Oltean 
2063c83654fSVladimir Oltean 	/* Calculate key/action/counter offsets */
2075963083aSVladimir Oltean 	col = (num_entries_per_row - col - 1);
208c1c3993eSVladimir Oltean 	data->key_offset = (base * vcap->entry_width) / vcap->sw_count;
20964294263SVladimir Oltean 	data->counter_offset = (num_subwords_per_entry * col *
21064294263SVladimir Oltean 				vcap->counter_width);
2113c83654fSVladimir Oltean 	i = data->type;
212c1c3993eSVladimir Oltean 	width = vcap->action_table[i].width;
21364294263SVladimir Oltean 	num_subwords_per_action = vcap->action_table[i].count;
21464294263SVladimir Oltean 	data->action_offset = ((num_subwords_per_action * col * width) /
21564294263SVladimir Oltean 				num_entries_per_row);
21664294263SVladimir Oltean 	data->action_offset += vcap->action_type_width;
2173c83654fSVladimir Oltean }
2183c83654fSVladimir Oltean 
2193c83654fSVladimir Oltean static void vcap_data_set(u32 *data, u32 offset, u32 len, u32 value)
2203c83654fSVladimir Oltean {
2213c83654fSVladimir Oltean 	u32 i, v, m;
2223c83654fSVladimir Oltean 
2233c83654fSVladimir Oltean 	for (i = 0; i < len; i++, offset++) {
2243c83654fSVladimir Oltean 		v = data[offset / ENTRY_WIDTH];
2253c83654fSVladimir Oltean 		m = (1 << (offset % ENTRY_WIDTH));
2263c83654fSVladimir Oltean 		if (value & (1 << i))
2273c83654fSVladimir Oltean 			v |= m;
2283c83654fSVladimir Oltean 		else
2293c83654fSVladimir Oltean 			v &= ~m;
2303c83654fSVladimir Oltean 		data[offset / ENTRY_WIDTH] = v;
2313c83654fSVladimir Oltean 	}
2323c83654fSVladimir Oltean }
2333c83654fSVladimir Oltean 
2343c83654fSVladimir Oltean static u32 vcap_data_get(u32 *data, u32 offset, u32 len)
2353c83654fSVladimir Oltean {
2363c83654fSVladimir Oltean 	u32 i, v, m, value = 0;
2373c83654fSVladimir Oltean 
2383c83654fSVladimir Oltean 	for (i = 0; i < len; i++, offset++) {
2393c83654fSVladimir Oltean 		v = data[offset / ENTRY_WIDTH];
2403c83654fSVladimir Oltean 		m = (1 << (offset % ENTRY_WIDTH));
2413c83654fSVladimir Oltean 		if (v & m)
2423c83654fSVladimir Oltean 			value |= (1 << i);
2433c83654fSVladimir Oltean 	}
2443c83654fSVladimir Oltean 	return value;
2453c83654fSVladimir Oltean }
2463c83654fSVladimir Oltean 
2473c83654fSVladimir Oltean static void vcap_key_field_set(struct vcap_data *data, u32 offset, u32 width,
2483c83654fSVladimir Oltean 			       u32 value, u32 mask)
2493c83654fSVladimir Oltean {
2503c83654fSVladimir Oltean 	vcap_data_set(data->entry, offset + data->key_offset, width, value);
2513c83654fSVladimir Oltean 	vcap_data_set(data->mask, offset + data->key_offset, width, mask);
2523c83654fSVladimir Oltean }
2533c83654fSVladimir Oltean 
254c1c3993eSVladimir Oltean static void vcap_key_set(const struct vcap_props *vcap, struct vcap_data *data,
255c1c3993eSVladimir Oltean 			 int field, u32 value, u32 mask)
2563c83654fSVladimir Oltean {
257c1c3993eSVladimir Oltean 	u32 offset = vcap->keys[field].offset;
258c1c3993eSVladimir Oltean 	u32 length = vcap->keys[field].length;
2593c83654fSVladimir Oltean 
2603c83654fSVladimir Oltean 	vcap_key_field_set(data, offset, length, value, mask);
2613c83654fSVladimir Oltean }
2623c83654fSVladimir Oltean 
263c1c3993eSVladimir Oltean static void vcap_key_bytes_set(const struct vcap_props *vcap,
264c1c3993eSVladimir Oltean 			       struct vcap_data *data, int field,
2653c83654fSVladimir Oltean 			       u8 *val, u8 *msk)
2663c83654fSVladimir Oltean {
267c1c3993eSVladimir Oltean 	u32 offset = vcap->keys[field].offset;
268c1c3993eSVladimir Oltean 	u32 count  = vcap->keys[field].length;
2693c83654fSVladimir Oltean 	u32 i, j, n = 0, value = 0, mask = 0;
2703c83654fSVladimir Oltean 
2713c83654fSVladimir Oltean 	WARN_ON(count % 8);
2723c83654fSVladimir Oltean 
2733c83654fSVladimir Oltean 	/* Data wider than 32 bits are split up in chunks of maximum 32 bits.
2743c83654fSVladimir Oltean 	 * The 32 LSB of the data are written to the 32 MSB of the TCAM.
2753c83654fSVladimir Oltean 	 */
2763c83654fSVladimir Oltean 	offset += count;
2773c83654fSVladimir Oltean 	count /= 8;
2783c83654fSVladimir Oltean 
2793c83654fSVladimir Oltean 	for (i = 0; i < count; i++) {
2803c83654fSVladimir Oltean 		j = (count - i - 1);
2813c83654fSVladimir Oltean 		value += (val[j] << n);
2823c83654fSVladimir Oltean 		mask += (msk[j] << n);
2833c83654fSVladimir Oltean 		n += 8;
2843c83654fSVladimir Oltean 		if (n == ENTRY_WIDTH || (i + 1) == count) {
2853c83654fSVladimir Oltean 			offset -= n;
2863c83654fSVladimir Oltean 			vcap_key_field_set(data, offset, n, value, mask);
2873c83654fSVladimir Oltean 			n = 0;
2883c83654fSVladimir Oltean 			value = 0;
2893c83654fSVladimir Oltean 			mask = 0;
2903c83654fSVladimir Oltean 		}
2913c83654fSVladimir Oltean 	}
2923c83654fSVladimir Oltean }
2933c83654fSVladimir Oltean 
294c1c3993eSVladimir Oltean static void vcap_key_l4_port_set(const struct vcap_props *vcap,
295c1c3993eSVladimir Oltean 				 struct vcap_data *data, int field,
2963c83654fSVladimir Oltean 				 struct ocelot_vcap_udp_tcp *port)
2973c83654fSVladimir Oltean {
298c1c3993eSVladimir Oltean 	u32 offset = vcap->keys[field].offset;
299c1c3993eSVladimir Oltean 	u32 length = vcap->keys[field].length;
3003c83654fSVladimir Oltean 
3013c83654fSVladimir Oltean 	WARN_ON(length != 16);
3023c83654fSVladimir Oltean 
3033c83654fSVladimir Oltean 	vcap_key_field_set(data, offset, length, port->value, port->mask);
3043c83654fSVladimir Oltean }
3053c83654fSVladimir Oltean 
306c1c3993eSVladimir Oltean static void vcap_key_bit_set(const struct vcap_props *vcap,
307c1c3993eSVladimir Oltean 			     struct vcap_data *data, int field,
3083c83654fSVladimir Oltean 			     enum ocelot_vcap_bit val)
3093c83654fSVladimir Oltean {
3103c83654fSVladimir Oltean 	u32 value = (val == OCELOT_VCAP_BIT_1 ? 1 : 0);
3113c83654fSVladimir Oltean 	u32 msk = (val == OCELOT_VCAP_BIT_ANY ? 0 : 1);
312c1c3993eSVladimir Oltean 	u32 offset = vcap->keys[field].offset;
313c1c3993eSVladimir Oltean 	u32 length = vcap->keys[field].length;
3143c83654fSVladimir Oltean 
3153c83654fSVladimir Oltean 	WARN_ON(length != 1);
3163c83654fSVladimir Oltean 
3173c83654fSVladimir Oltean 	vcap_key_field_set(data, offset, length, value, msk);
3183c83654fSVladimir Oltean }
3193c83654fSVladimir Oltean 
320c1c3993eSVladimir Oltean static void vcap_action_set(const struct vcap_props *vcap,
321c1c3993eSVladimir Oltean 			    struct vcap_data *data, int field, u32 value)
3223c83654fSVladimir Oltean {
323c1c3993eSVladimir Oltean 	int offset = vcap->actions[field].offset;
324c1c3993eSVladimir Oltean 	int length = vcap->actions[field].length;
3253c83654fSVladimir Oltean 
3263c83654fSVladimir Oltean 	vcap_data_set(data->action, offset + data->action_offset, length,
3273c83654fSVladimir Oltean 		      value);
3283c83654fSVladimir Oltean }
3293c83654fSVladimir Oltean 
3303c83654fSVladimir Oltean static void is2_action_set(struct ocelot *ocelot, struct vcap_data *data,
331aae4e500SVladimir Oltean 			   struct ocelot_vcap_filter *filter)
3323c83654fSVladimir Oltean {
333c1c3993eSVladimir Oltean 	const struct vcap_props *vcap = &ocelot->vcap[VCAP_IS2];
334ea9d1f30SVladimir Oltean 	struct ocelot_vcap_action *a = &filter->action;
335c1c3993eSVladimir Oltean 
336ea9d1f30SVladimir Oltean 	vcap_action_set(vcap, data, VCAP_IS2_ACT_MASK_MODE, a->mask_mode);
337ea9d1f30SVladimir Oltean 	vcap_action_set(vcap, data, VCAP_IS2_ACT_PORT_MASK, a->port_mask);
338*f2a0e216SVladimir Oltean 	vcap_action_set(vcap, data, VCAP_IS2_ACT_MIRROR_ENA, a->mirror_ena);
339ea9d1f30SVladimir Oltean 	vcap_action_set(vcap, data, VCAP_IS2_ACT_POLICE_ENA, a->police_ena);
340ea9d1f30SVladimir Oltean 	vcap_action_set(vcap, data, VCAP_IS2_ACT_POLICE_IDX, a->pol_ix);
341ea9d1f30SVladimir Oltean 	vcap_action_set(vcap, data, VCAP_IS2_ACT_CPU_QU_NUM, a->cpu_qu_num);
342ea9d1f30SVladimir Oltean 	vcap_action_set(vcap, data, VCAP_IS2_ACT_CPU_COPY_ENA, a->cpu_copy_ena);
3433c83654fSVladimir Oltean }
3443c83654fSVladimir Oltean 
3453c83654fSVladimir Oltean static void is2_entry_set(struct ocelot *ocelot, int ix,
346aae4e500SVladimir Oltean 			  struct ocelot_vcap_filter *filter)
3473c83654fSVladimir Oltean {
348c1c3993eSVladimir Oltean 	const struct vcap_props *vcap = &ocelot->vcap[VCAP_IS2];
349aae4e500SVladimir Oltean 	struct ocelot_vcap_key_vlan *tag = &filter->vlan;
3503c83654fSVladimir Oltean 	u32 val, msk, type, type_mask = 0xf, i, count;
3513c83654fSVladimir Oltean 	struct ocelot_vcap_u64 payload;
3523c83654fSVladimir Oltean 	struct vcap_data data;
3533c83654fSVladimir Oltean 	int row = (ix / 2);
3543c83654fSVladimir Oltean 
3553c83654fSVladimir Oltean 	memset(&payload, 0, sizeof(payload));
3563c83654fSVladimir Oltean 	memset(&data, 0, sizeof(data));
3573c83654fSVladimir Oltean 
3583c83654fSVladimir Oltean 	/* Read row */
359c1c3993eSVladimir Oltean 	vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_READ, VCAP_SEL_ALL);
360c1c3993eSVladimir Oltean 	vcap_cache2entry(ocelot, vcap, &data);
361c1c3993eSVladimir Oltean 	vcap_cache2action(ocelot, vcap, &data);
3623c83654fSVladimir Oltean 
3633c83654fSVladimir Oltean 	data.tg_sw = VCAP_TG_HALF;
364c1c3993eSVladimir Oltean 	vcap_data_offset_get(vcap, &data, ix);
3653c83654fSVladimir Oltean 	data.tg = (data.tg & ~data.tg_mask);
366aae4e500SVladimir Oltean 	if (filter->prio != 0)
3673c83654fSVladimir Oltean 		data.tg |= data.tg_value;
3683c83654fSVladimir Oltean 
3693c83654fSVladimir Oltean 	data.type = IS2_ACTION_TYPE_NORMAL;
3703c83654fSVladimir Oltean 
371226e9cd8SVladimir Oltean 	vcap_key_set(vcap, &data, VCAP_IS2_HK_PAG, filter->pag, 0xff);
372226e9cd8SVladimir Oltean 	vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_FIRST,
373226e9cd8SVladimir Oltean 			 (filter->lookup == 0) ? OCELOT_VCAP_BIT_1 :
374226e9cd8SVladimir Oltean 			 OCELOT_VCAP_BIT_0);
375c1c3993eSVladimir Oltean 	vcap_key_set(vcap, &data, VCAP_IS2_HK_IGR_PORT_MASK, 0,
376aae4e500SVladimir Oltean 		     ~filter->ingress_port_mask);
377c1c3993eSVladimir Oltean 	vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_FIRST, OCELOT_VCAP_BIT_ANY);
378c1c3993eSVladimir Oltean 	vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_HOST_MATCH,
3793c83654fSVladimir Oltean 			 OCELOT_VCAP_BIT_ANY);
380c1c3993eSVladimir Oltean 	vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L2_MC, filter->dmac_mc);
381c1c3993eSVladimir Oltean 	vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L2_BC, filter->dmac_bc);
382c1c3993eSVladimir Oltean 	vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_VLAN_TAGGED, tag->tagged);
383c1c3993eSVladimir Oltean 	vcap_key_set(vcap, &data, VCAP_IS2_HK_VID,
3843c83654fSVladimir Oltean 		     tag->vid.value, tag->vid.mask);
385c1c3993eSVladimir Oltean 	vcap_key_set(vcap, &data, VCAP_IS2_HK_PCP,
3863c83654fSVladimir Oltean 		     tag->pcp.value[0], tag->pcp.mask[0]);
387c1c3993eSVladimir Oltean 	vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_DEI, tag->dei);
3883c83654fSVladimir Oltean 
389aae4e500SVladimir Oltean 	switch (filter->key_type) {
390aae4e500SVladimir Oltean 	case OCELOT_VCAP_KEY_ETYPE: {
391aae4e500SVladimir Oltean 		struct ocelot_vcap_key_etype *etype = &filter->key.etype;
3923c83654fSVladimir Oltean 
3933c83654fSVladimir Oltean 		type = IS2_TYPE_ETYPE;
394c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_DMAC,
3953c83654fSVladimir Oltean 				   etype->dmac.value, etype->dmac.mask);
396c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_SMAC,
3973c83654fSVladimir Oltean 				   etype->smac.value, etype->smac.mask);
398c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_MAC_ETYPE_ETYPE,
3993c83654fSVladimir Oltean 				   etype->etype.value, etype->etype.mask);
4003c83654fSVladimir Oltean 		/* Clear unused bits */
401c1c3993eSVladimir Oltean 		vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD0,
4023c83654fSVladimir Oltean 			     0, 0);
403c1c3993eSVladimir Oltean 		vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD1,
4043c83654fSVladimir Oltean 			     0, 0);
405c1c3993eSVladimir Oltean 		vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD2,
4063c83654fSVladimir Oltean 			     0, 0);
407c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data,
4083c83654fSVladimir Oltean 				   VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD0,
4093c83654fSVladimir Oltean 				   etype->data.value, etype->data.mask);
4103c83654fSVladimir Oltean 		break;
4113c83654fSVladimir Oltean 	}
412aae4e500SVladimir Oltean 	case OCELOT_VCAP_KEY_LLC: {
413aae4e500SVladimir Oltean 		struct ocelot_vcap_key_llc *llc = &filter->key.llc;
4143c83654fSVladimir Oltean 
4153c83654fSVladimir Oltean 		type = IS2_TYPE_LLC;
416c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_DMAC,
4173c83654fSVladimir Oltean 				   llc->dmac.value, llc->dmac.mask);
418c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_SMAC,
4193c83654fSVladimir Oltean 				   llc->smac.value, llc->smac.mask);
4203c83654fSVladimir Oltean 		for (i = 0; i < 4; i++) {
4213c83654fSVladimir Oltean 			payload.value[i] = llc->llc.value[i];
4223c83654fSVladimir Oltean 			payload.mask[i] = llc->llc.mask[i];
4233c83654fSVladimir Oltean 		}
424c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_MAC_LLC_L2_LLC,
4253c83654fSVladimir Oltean 				   payload.value, payload.mask);
4263c83654fSVladimir Oltean 		break;
4273c83654fSVladimir Oltean 	}
428aae4e500SVladimir Oltean 	case OCELOT_VCAP_KEY_SNAP: {
429aae4e500SVladimir Oltean 		struct ocelot_vcap_key_snap *snap = &filter->key.snap;
4303c83654fSVladimir Oltean 
4313c83654fSVladimir Oltean 		type = IS2_TYPE_SNAP;
432c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_DMAC,
4333c83654fSVladimir Oltean 				   snap->dmac.value, snap->dmac.mask);
434c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_SMAC,
4353c83654fSVladimir Oltean 				   snap->smac.value, snap->smac.mask);
436c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_MAC_SNAP_L2_SNAP,
437aae4e500SVladimir Oltean 				   filter->key.snap.snap.value,
438aae4e500SVladimir Oltean 				   filter->key.snap.snap.mask);
4393c83654fSVladimir Oltean 		break;
4403c83654fSVladimir Oltean 	}
441aae4e500SVladimir Oltean 	case OCELOT_VCAP_KEY_ARP: {
442aae4e500SVladimir Oltean 		struct ocelot_vcap_key_arp *arp = &filter->key.arp;
4433c83654fSVladimir Oltean 
4443c83654fSVladimir Oltean 		type = IS2_TYPE_ARP;
445c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_MAC_ARP_SMAC,
4463c83654fSVladimir Oltean 				   arp->smac.value, arp->smac.mask);
447c1c3993eSVladimir Oltean 		vcap_key_bit_set(vcap, &data,
4483c83654fSVladimir Oltean 				 VCAP_IS2_HK_MAC_ARP_ADDR_SPACE_OK,
4493c83654fSVladimir Oltean 				 arp->ethernet);
450c1c3993eSVladimir Oltean 		vcap_key_bit_set(vcap, &data,
4513c83654fSVladimir Oltean 				 VCAP_IS2_HK_MAC_ARP_PROTO_SPACE_OK,
4523c83654fSVladimir Oltean 				 arp->ip);
453c1c3993eSVladimir Oltean 		vcap_key_bit_set(vcap, &data,
4543c83654fSVladimir Oltean 				 VCAP_IS2_HK_MAC_ARP_LEN_OK,
4553c83654fSVladimir Oltean 				 arp->length);
456c1c3993eSVladimir Oltean 		vcap_key_bit_set(vcap, &data,
4573c83654fSVladimir Oltean 				 VCAP_IS2_HK_MAC_ARP_TARGET_MATCH,
4583c83654fSVladimir Oltean 				 arp->dmac_match);
459c1c3993eSVladimir Oltean 		vcap_key_bit_set(vcap, &data,
4603c83654fSVladimir Oltean 				 VCAP_IS2_HK_MAC_ARP_SENDER_MATCH,
4613c83654fSVladimir Oltean 				 arp->smac_match);
462c1c3993eSVladimir Oltean 		vcap_key_bit_set(vcap, &data,
4633c83654fSVladimir Oltean 				 VCAP_IS2_HK_MAC_ARP_OPCODE_UNKNOWN,
4643c83654fSVladimir Oltean 				 arp->unknown);
4653c83654fSVladimir Oltean 
4663c83654fSVladimir Oltean 		/* OPCODE is inverse, bit 0 is reply flag, bit 1 is RARP flag */
4673c83654fSVladimir Oltean 		val = ((arp->req == OCELOT_VCAP_BIT_0 ? 1 : 0) |
4683c83654fSVladimir Oltean 		       (arp->arp == OCELOT_VCAP_BIT_0 ? 2 : 0));
4693c83654fSVladimir Oltean 		msk = ((arp->req == OCELOT_VCAP_BIT_ANY ? 0 : 1) |
4703c83654fSVladimir Oltean 		       (arp->arp == OCELOT_VCAP_BIT_ANY ? 0 : 2));
471c1c3993eSVladimir Oltean 		vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ARP_OPCODE,
4723c83654fSVladimir Oltean 			     val, msk);
473c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data,
4743c83654fSVladimir Oltean 				   VCAP_IS2_HK_MAC_ARP_L3_IP4_DIP,
4753c83654fSVladimir Oltean 				   arp->dip.value.addr, arp->dip.mask.addr);
476c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data,
4773c83654fSVladimir Oltean 				   VCAP_IS2_HK_MAC_ARP_L3_IP4_SIP,
4783c83654fSVladimir Oltean 				   arp->sip.value.addr, arp->sip.mask.addr);
479c1c3993eSVladimir Oltean 		vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ARP_DIP_EQ_SIP,
4803c83654fSVladimir Oltean 			     0, 0);
4813c83654fSVladimir Oltean 		break;
4823c83654fSVladimir Oltean 	}
483aae4e500SVladimir Oltean 	case OCELOT_VCAP_KEY_IPV4:
484aae4e500SVladimir Oltean 	case OCELOT_VCAP_KEY_IPV6: {
4853c83654fSVladimir Oltean 		enum ocelot_vcap_bit sip_eq_dip, sport_eq_dport, seq_zero, tcp;
4863c83654fSVladimir Oltean 		enum ocelot_vcap_bit ttl, fragment, options, tcp_ack, tcp_urg;
4873c83654fSVladimir Oltean 		enum ocelot_vcap_bit tcp_fin, tcp_syn, tcp_rst, tcp_psh;
488aae4e500SVladimir Oltean 		struct ocelot_vcap_key_ipv4 *ipv4 = NULL;
489aae4e500SVladimir Oltean 		struct ocelot_vcap_key_ipv6 *ipv6 = NULL;
4903c83654fSVladimir Oltean 		struct ocelot_vcap_udp_tcp *sport, *dport;
4913c83654fSVladimir Oltean 		struct ocelot_vcap_ipv4 sip, dip;
4923c83654fSVladimir Oltean 		struct ocelot_vcap_u8 proto, ds;
4933c83654fSVladimir Oltean 		struct ocelot_vcap_u48 *ip_data;
4943c83654fSVladimir Oltean 
495aae4e500SVladimir Oltean 		if (filter->key_type == OCELOT_VCAP_KEY_IPV4) {
496aae4e500SVladimir Oltean 			ipv4 = &filter->key.ipv4;
4973c83654fSVladimir Oltean 			ttl = ipv4->ttl;
4983c83654fSVladimir Oltean 			fragment = ipv4->fragment;
4993c83654fSVladimir Oltean 			options = ipv4->options;
5003c83654fSVladimir Oltean 			proto = ipv4->proto;
5013c83654fSVladimir Oltean 			ds = ipv4->ds;
5023c83654fSVladimir Oltean 			ip_data = &ipv4->data;
5033c83654fSVladimir Oltean 			sip = ipv4->sip;
5043c83654fSVladimir Oltean 			dip = ipv4->dip;
5053c83654fSVladimir Oltean 			sport = &ipv4->sport;
5063c83654fSVladimir Oltean 			dport = &ipv4->dport;
5073c83654fSVladimir Oltean 			tcp_fin = ipv4->tcp_fin;
5083c83654fSVladimir Oltean 			tcp_syn = ipv4->tcp_syn;
5093c83654fSVladimir Oltean 			tcp_rst = ipv4->tcp_rst;
5103c83654fSVladimir Oltean 			tcp_psh = ipv4->tcp_psh;
5113c83654fSVladimir Oltean 			tcp_ack = ipv4->tcp_ack;
5123c83654fSVladimir Oltean 			tcp_urg = ipv4->tcp_urg;
5133c83654fSVladimir Oltean 			sip_eq_dip = ipv4->sip_eq_dip;
5143c83654fSVladimir Oltean 			sport_eq_dport = ipv4->sport_eq_dport;
5153c83654fSVladimir Oltean 			seq_zero = ipv4->seq_zero;
5163c83654fSVladimir Oltean 		} else {
517aae4e500SVladimir Oltean 			ipv6 = &filter->key.ipv6;
5183c83654fSVladimir Oltean 			ttl = ipv6->ttl;
5193c83654fSVladimir Oltean 			fragment = OCELOT_VCAP_BIT_ANY;
5203c83654fSVladimir Oltean 			options = OCELOT_VCAP_BIT_ANY;
5213c83654fSVladimir Oltean 			proto = ipv6->proto;
5223c83654fSVladimir Oltean 			ds = ipv6->ds;
5233c83654fSVladimir Oltean 			ip_data = &ipv6->data;
5243c83654fSVladimir Oltean 			for (i = 0; i < 8; i++) {
5253c83654fSVladimir Oltean 				val = ipv6->sip.value[i + 8];
5263c83654fSVladimir Oltean 				msk = ipv6->sip.mask[i + 8];
5273c83654fSVladimir Oltean 				if (i < 4) {
5283c83654fSVladimir Oltean 					dip.value.addr[i] = val;
5293c83654fSVladimir Oltean 					dip.mask.addr[i] = msk;
5303c83654fSVladimir Oltean 				} else {
5313c83654fSVladimir Oltean 					sip.value.addr[i - 4] = val;
5323c83654fSVladimir Oltean 					sip.mask.addr[i - 4] = msk;
5333c83654fSVladimir Oltean 				}
5343c83654fSVladimir Oltean 			}
5353c83654fSVladimir Oltean 			sport = &ipv6->sport;
5363c83654fSVladimir Oltean 			dport = &ipv6->dport;
5373c83654fSVladimir Oltean 			tcp_fin = ipv6->tcp_fin;
5383c83654fSVladimir Oltean 			tcp_syn = ipv6->tcp_syn;
5393c83654fSVladimir Oltean 			tcp_rst = ipv6->tcp_rst;
5403c83654fSVladimir Oltean 			tcp_psh = ipv6->tcp_psh;
5413c83654fSVladimir Oltean 			tcp_ack = ipv6->tcp_ack;
5423c83654fSVladimir Oltean 			tcp_urg = ipv6->tcp_urg;
5433c83654fSVladimir Oltean 			sip_eq_dip = ipv6->sip_eq_dip;
5443c83654fSVladimir Oltean 			sport_eq_dport = ipv6->sport_eq_dport;
5453c83654fSVladimir Oltean 			seq_zero = ipv6->seq_zero;
5463c83654fSVladimir Oltean 		}
5473c83654fSVladimir Oltean 
548c1c3993eSVladimir Oltean 		vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_IP4,
5493c83654fSVladimir Oltean 				 ipv4 ? OCELOT_VCAP_BIT_1 : OCELOT_VCAP_BIT_0);
550c1c3993eSVladimir Oltean 		vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L3_FRAGMENT,
5513c83654fSVladimir Oltean 				 fragment);
552c1c3993eSVladimir Oltean 		vcap_key_set(vcap, &data, VCAP_IS2_HK_L3_FRAG_OFS_GT0, 0, 0);
553c1c3993eSVladimir Oltean 		vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L3_OPTIONS,
5543c83654fSVladimir Oltean 				 options);
555c1c3993eSVladimir Oltean 		vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_IP4_L3_TTL_GT0,
5563c83654fSVladimir Oltean 				 ttl);
557c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L3_TOS,
5583c83654fSVladimir Oltean 				   ds.value, ds.mask);
559c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L3_IP4_DIP,
5603c83654fSVladimir Oltean 				   dip.value.addr, dip.mask.addr);
561c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L3_IP4_SIP,
5623c83654fSVladimir Oltean 				   sip.value.addr, sip.mask.addr);
563c1c3993eSVladimir Oltean 		vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_DIP_EQ_SIP,
5643c83654fSVladimir Oltean 				 sip_eq_dip);
5653c83654fSVladimir Oltean 		val = proto.value[0];
5663c83654fSVladimir Oltean 		msk = proto.mask[0];
5673c83654fSVladimir Oltean 		type = IS2_TYPE_IP_UDP_TCP;
568c3cde44fSVladimir Oltean 		if (msk == 0xff && (val == IPPROTO_TCP || val == IPPROTO_UDP)) {
5693c83654fSVladimir Oltean 			/* UDP/TCP protocol match */
570c3cde44fSVladimir Oltean 			tcp = (val == IPPROTO_TCP ?
5713c83654fSVladimir Oltean 			       OCELOT_VCAP_BIT_1 : OCELOT_VCAP_BIT_0);
572c1c3993eSVladimir Oltean 			vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_TCP, tcp);
573c1c3993eSVladimir Oltean 			vcap_key_l4_port_set(vcap, &data,
5743c83654fSVladimir Oltean 					     VCAP_IS2_HK_L4_DPORT, dport);
575c1c3993eSVladimir Oltean 			vcap_key_l4_port_set(vcap, &data,
5763c83654fSVladimir Oltean 					     VCAP_IS2_HK_L4_SPORT, sport);
577c1c3993eSVladimir Oltean 			vcap_key_set(vcap, &data, VCAP_IS2_HK_L4_RNG, 0, 0);
578c1c3993eSVladimir Oltean 			vcap_key_bit_set(vcap, &data,
5793c83654fSVladimir Oltean 					 VCAP_IS2_HK_L4_SPORT_EQ_DPORT,
5803c83654fSVladimir Oltean 					 sport_eq_dport);
581c1c3993eSVladimir Oltean 			vcap_key_bit_set(vcap, &data,
5823c83654fSVladimir Oltean 					 VCAP_IS2_HK_L4_SEQUENCE_EQ0,
5833c83654fSVladimir Oltean 					 seq_zero);
584c1c3993eSVladimir Oltean 			vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_FIN,
5853c83654fSVladimir Oltean 					 tcp_fin);
586c1c3993eSVladimir Oltean 			vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_SYN,
5873c83654fSVladimir Oltean 					 tcp_syn);
588c1c3993eSVladimir Oltean 			vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_RST,
5893c83654fSVladimir Oltean 					 tcp_rst);
590c1c3993eSVladimir Oltean 			vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_PSH,
5913c83654fSVladimir Oltean 					 tcp_psh);
592c1c3993eSVladimir Oltean 			vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_ACK,
5933c83654fSVladimir Oltean 					 tcp_ack);
594c1c3993eSVladimir Oltean 			vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_URG,
5953c83654fSVladimir Oltean 					 tcp_urg);
596c1c3993eSVladimir Oltean 			vcap_key_set(vcap, &data, VCAP_IS2_HK_L4_1588_DOM,
5973c83654fSVladimir Oltean 				     0, 0);
598c1c3993eSVladimir Oltean 			vcap_key_set(vcap, &data, VCAP_IS2_HK_L4_1588_VER,
5993c83654fSVladimir Oltean 				     0, 0);
6003c83654fSVladimir Oltean 		} else {
6013c83654fSVladimir Oltean 			if (msk == 0) {
6023c83654fSVladimir Oltean 				/* Any IP protocol match */
6033c83654fSVladimir Oltean 				type_mask = IS2_TYPE_MASK_IP_ANY;
6043c83654fSVladimir Oltean 			} else {
6053c83654fSVladimir Oltean 				/* Non-UDP/TCP protocol match */
6063c83654fSVladimir Oltean 				type = IS2_TYPE_IP_OTHER;
6073c83654fSVladimir Oltean 				for (i = 0; i < 6; i++) {
6083c83654fSVladimir Oltean 					payload.value[i] = ip_data->value[i];
6093c83654fSVladimir Oltean 					payload.mask[i] = ip_data->mask[i];
6103c83654fSVladimir Oltean 				}
6113c83654fSVladimir Oltean 			}
612c1c3993eSVladimir Oltean 			vcap_key_bytes_set(vcap, &data,
6133c83654fSVladimir Oltean 					   VCAP_IS2_HK_IP4_L3_PROTO,
6143c83654fSVladimir Oltean 					   proto.value, proto.mask);
615c1c3993eSVladimir Oltean 			vcap_key_bytes_set(vcap, &data,
6163c83654fSVladimir Oltean 					   VCAP_IS2_HK_L3_PAYLOAD,
6173c83654fSVladimir Oltean 					   payload.value, payload.mask);
6183c83654fSVladimir Oltean 		}
6193c83654fSVladimir Oltean 		break;
6203c83654fSVladimir Oltean 	}
621aae4e500SVladimir Oltean 	case OCELOT_VCAP_KEY_ANY:
6223c83654fSVladimir Oltean 	default:
6233c83654fSVladimir Oltean 		type = 0;
6243c83654fSVladimir Oltean 		type_mask = 0;
625c1c3993eSVladimir Oltean 		count = vcap->entry_width / 2;
6263c83654fSVladimir Oltean 		/* Iterate over the non-common part of the key and
6273c83654fSVladimir Oltean 		 * clear entry data
6283c83654fSVladimir Oltean 		 */
629c1c3993eSVladimir Oltean 		for (i = vcap->keys[VCAP_IS2_HK_L2_DMAC].offset;
6303c83654fSVladimir Oltean 		     i < count; i += ENTRY_WIDTH) {
6313c83654fSVladimir Oltean 			vcap_key_field_set(&data, i, min(32u, count - i), 0, 0);
6323c83654fSVladimir Oltean 		}
6333c83654fSVladimir Oltean 		break;
6343c83654fSVladimir Oltean 	}
6353c83654fSVladimir Oltean 
636c1c3993eSVladimir Oltean 	vcap_key_set(vcap, &data, VCAP_IS2_TYPE, type, type_mask);
637aae4e500SVladimir Oltean 	is2_action_set(ocelot, &data, filter);
6383c83654fSVladimir Oltean 	vcap_data_set(data.counter, data.counter_offset,
639c1c3993eSVladimir Oltean 		      vcap->counter_width, filter->stats.pkts);
6403c83654fSVladimir Oltean 
6413c83654fSVladimir Oltean 	/* Write row */
642c1c3993eSVladimir Oltean 	vcap_entry2cache(ocelot, vcap, &data);
643c1c3993eSVladimir Oltean 	vcap_action2cache(ocelot, vcap, &data);
644c1c3993eSVladimir Oltean 	vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_WRITE, VCAP_SEL_ALL);
6453c83654fSVladimir Oltean }
6463c83654fSVladimir Oltean 
64775944fdaSXiaoliang Yang static void is1_action_set(struct ocelot *ocelot, struct vcap_data *data,
64875944fdaSXiaoliang Yang 			   const struct ocelot_vcap_filter *filter)
64975944fdaSXiaoliang Yang {
65075944fdaSXiaoliang Yang 	const struct vcap_props *vcap = &ocelot->vcap[VCAP_IS1];
65175944fdaSXiaoliang Yang 	const struct ocelot_vcap_action *a = &filter->action;
65275944fdaSXiaoliang Yang 
65375944fdaSXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_IS1_ACT_VID_REPLACE_ENA,
65475944fdaSXiaoliang Yang 			a->vid_replace_ena);
65575944fdaSXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_IS1_ACT_VID_ADD_VAL, a->vid);
65675944fdaSXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_IS1_ACT_VLAN_POP_CNT_ENA,
65775944fdaSXiaoliang Yang 			a->vlan_pop_cnt_ena);
65875944fdaSXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_IS1_ACT_VLAN_POP_CNT,
65975944fdaSXiaoliang Yang 			a->vlan_pop_cnt);
66075944fdaSXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_IS1_ACT_PCP_DEI_ENA, a->pcp_dei_ena);
66175944fdaSXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_IS1_ACT_PCP_VAL, a->pcp);
66275944fdaSXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_IS1_ACT_DEI_VAL, a->dei);
66375944fdaSXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_IS1_ACT_QOS_ENA, a->qos_ena);
66475944fdaSXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_IS1_ACT_QOS_VAL, a->qos_val);
66575944fdaSXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_IS1_ACT_PAG_OVERRIDE_MASK,
66675944fdaSXiaoliang Yang 			a->pag_override_mask);
66775944fdaSXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_IS1_ACT_PAG_VAL, a->pag_val);
66875944fdaSXiaoliang Yang }
66975944fdaSXiaoliang Yang 
67075944fdaSXiaoliang Yang static void is1_entry_set(struct ocelot *ocelot, int ix,
67175944fdaSXiaoliang Yang 			  struct ocelot_vcap_filter *filter)
67275944fdaSXiaoliang Yang {
67375944fdaSXiaoliang Yang 	const struct vcap_props *vcap = &ocelot->vcap[VCAP_IS1];
67475944fdaSXiaoliang Yang 	struct ocelot_vcap_key_vlan *tag = &filter->vlan;
67575944fdaSXiaoliang Yang 	struct ocelot_vcap_u64 payload;
67675944fdaSXiaoliang Yang 	struct vcap_data data;
67775944fdaSXiaoliang Yang 	int row = ix / 2;
67875944fdaSXiaoliang Yang 	u32 type;
67975944fdaSXiaoliang Yang 
68075944fdaSXiaoliang Yang 	memset(&payload, 0, sizeof(payload));
68175944fdaSXiaoliang Yang 	memset(&data, 0, sizeof(data));
68275944fdaSXiaoliang Yang 
68375944fdaSXiaoliang Yang 	/* Read row */
68475944fdaSXiaoliang Yang 	vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_READ, VCAP_SEL_ALL);
68575944fdaSXiaoliang Yang 	vcap_cache2entry(ocelot, vcap, &data);
68675944fdaSXiaoliang Yang 	vcap_cache2action(ocelot, vcap, &data);
68775944fdaSXiaoliang Yang 
68875944fdaSXiaoliang Yang 	data.tg_sw = VCAP_TG_HALF;
68975944fdaSXiaoliang Yang 	data.type = IS1_ACTION_TYPE_NORMAL;
69075944fdaSXiaoliang Yang 	vcap_data_offset_get(vcap, &data, ix);
69175944fdaSXiaoliang Yang 	data.tg = (data.tg & ~data.tg_mask);
69275944fdaSXiaoliang Yang 	if (filter->prio != 0)
69375944fdaSXiaoliang Yang 		data.tg |= data.tg_value;
69475944fdaSXiaoliang Yang 
695226e9cd8SVladimir Oltean 	vcap_key_set(vcap, &data, VCAP_IS1_HK_LOOKUP, filter->lookup, 0x3);
69675944fdaSXiaoliang Yang 	vcap_key_set(vcap, &data, VCAP_IS1_HK_IGR_PORT_MASK, 0,
69775944fdaSXiaoliang Yang 		     ~filter->ingress_port_mask);
69875944fdaSXiaoliang Yang 	vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_L2_MC, filter->dmac_mc);
69975944fdaSXiaoliang Yang 	vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_L2_BC, filter->dmac_bc);
70075944fdaSXiaoliang Yang 	vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_VLAN_TAGGED, tag->tagged);
70175944fdaSXiaoliang Yang 	vcap_key_set(vcap, &data, VCAP_IS1_HK_VID,
70275944fdaSXiaoliang Yang 		     tag->vid.value, tag->vid.mask);
70375944fdaSXiaoliang Yang 	vcap_key_set(vcap, &data, VCAP_IS1_HK_PCP,
70475944fdaSXiaoliang Yang 		     tag->pcp.value[0], tag->pcp.mask[0]);
70575944fdaSXiaoliang Yang 	type = IS1_TYPE_S1_NORMAL;
70675944fdaSXiaoliang Yang 
70775944fdaSXiaoliang Yang 	switch (filter->key_type) {
70875944fdaSXiaoliang Yang 	case OCELOT_VCAP_KEY_ETYPE: {
70975944fdaSXiaoliang Yang 		struct ocelot_vcap_key_etype *etype = &filter->key.etype;
71075944fdaSXiaoliang Yang 
71175944fdaSXiaoliang Yang 		vcap_key_bytes_set(vcap, &data, VCAP_IS1_HK_L2_SMAC,
71275944fdaSXiaoliang Yang 				   etype->smac.value, etype->smac.mask);
71375944fdaSXiaoliang Yang 		vcap_key_bytes_set(vcap, &data, VCAP_IS1_HK_ETYPE,
71475944fdaSXiaoliang Yang 				   etype->etype.value, etype->etype.mask);
71575944fdaSXiaoliang Yang 		break;
71675944fdaSXiaoliang Yang 	}
71775944fdaSXiaoliang Yang 	case OCELOT_VCAP_KEY_IPV4: {
71875944fdaSXiaoliang Yang 		struct ocelot_vcap_key_ipv4 *ipv4 = &filter->key.ipv4;
71975944fdaSXiaoliang Yang 		struct ocelot_vcap_udp_tcp *sport = &ipv4->sport;
72075944fdaSXiaoliang Yang 		struct ocelot_vcap_udp_tcp *dport = &ipv4->dport;
72175944fdaSXiaoliang Yang 		enum ocelot_vcap_bit tcp_udp = OCELOT_VCAP_BIT_0;
72275944fdaSXiaoliang Yang 		struct ocelot_vcap_u8 proto = ipv4->proto;
72375944fdaSXiaoliang Yang 		struct ocelot_vcap_ipv4 sip = ipv4->sip;
72475944fdaSXiaoliang Yang 		u32 val, msk;
72575944fdaSXiaoliang Yang 
72675944fdaSXiaoliang Yang 		vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_IP_SNAP,
72775944fdaSXiaoliang Yang 				 OCELOT_VCAP_BIT_1);
72875944fdaSXiaoliang Yang 		vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_IP4,
72975944fdaSXiaoliang Yang 				 OCELOT_VCAP_BIT_1);
73075944fdaSXiaoliang Yang 		vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_ETYPE_LEN,
73175944fdaSXiaoliang Yang 				 OCELOT_VCAP_BIT_1);
73275944fdaSXiaoliang Yang 		vcap_key_bytes_set(vcap, &data, VCAP_IS1_HK_L3_IP4_SIP,
73375944fdaSXiaoliang Yang 				   sip.value.addr, sip.mask.addr);
73475944fdaSXiaoliang Yang 
73575944fdaSXiaoliang Yang 		val = proto.value[0];
73675944fdaSXiaoliang Yang 		msk = proto.mask[0];
73775944fdaSXiaoliang Yang 
73875944fdaSXiaoliang Yang 		if ((val == NEXTHDR_TCP || val == NEXTHDR_UDP) && msk == 0xff)
73975944fdaSXiaoliang Yang 			tcp_udp = OCELOT_VCAP_BIT_1;
74075944fdaSXiaoliang Yang 		vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_TCP_UDP, tcp_udp);
74175944fdaSXiaoliang Yang 
74275944fdaSXiaoliang Yang 		if (tcp_udp) {
74375944fdaSXiaoliang Yang 			enum ocelot_vcap_bit tcp = OCELOT_VCAP_BIT_0;
74475944fdaSXiaoliang Yang 
74575944fdaSXiaoliang Yang 			if (val == NEXTHDR_TCP)
74675944fdaSXiaoliang Yang 				tcp = OCELOT_VCAP_BIT_1;
74775944fdaSXiaoliang Yang 
74875944fdaSXiaoliang Yang 			vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_TCP, tcp);
74975944fdaSXiaoliang Yang 			vcap_key_l4_port_set(vcap, &data, VCAP_IS1_HK_L4_SPORT,
75075944fdaSXiaoliang Yang 					     sport);
75175944fdaSXiaoliang Yang 			/* Overloaded field */
75275944fdaSXiaoliang Yang 			vcap_key_l4_port_set(vcap, &data, VCAP_IS1_HK_ETYPE,
75375944fdaSXiaoliang Yang 					     dport);
75475944fdaSXiaoliang Yang 		} else {
75575944fdaSXiaoliang Yang 			/* IPv4 "other" frame */
75675944fdaSXiaoliang Yang 			struct ocelot_vcap_u16 etype = {0};
75775944fdaSXiaoliang Yang 
75875944fdaSXiaoliang Yang 			/* Overloaded field */
75975944fdaSXiaoliang Yang 			etype.value[0] = proto.value[0];
76075944fdaSXiaoliang Yang 			etype.mask[0] = proto.mask[0];
76175944fdaSXiaoliang Yang 
76275944fdaSXiaoliang Yang 			vcap_key_bytes_set(vcap, &data, VCAP_IS1_HK_ETYPE,
76375944fdaSXiaoliang Yang 					   etype.value, etype.mask);
76475944fdaSXiaoliang Yang 		}
765fdeadd6eSGustavo A. R. Silva 		break;
76675944fdaSXiaoliang Yang 	}
76775944fdaSXiaoliang Yang 	default:
76875944fdaSXiaoliang Yang 		break;
76975944fdaSXiaoliang Yang 	}
77075944fdaSXiaoliang Yang 	vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_TYPE,
77175944fdaSXiaoliang Yang 			 type ? OCELOT_VCAP_BIT_1 : OCELOT_VCAP_BIT_0);
77275944fdaSXiaoliang Yang 
77375944fdaSXiaoliang Yang 	is1_action_set(ocelot, &data, filter);
77475944fdaSXiaoliang Yang 	vcap_data_set(data.counter, data.counter_offset,
77575944fdaSXiaoliang Yang 		      vcap->counter_width, filter->stats.pkts);
77675944fdaSXiaoliang Yang 
77775944fdaSXiaoliang Yang 	/* Write row */
77875944fdaSXiaoliang Yang 	vcap_entry2cache(ocelot, vcap, &data);
77975944fdaSXiaoliang Yang 	vcap_action2cache(ocelot, vcap, &data);
78075944fdaSXiaoliang Yang 	vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_WRITE, VCAP_SEL_ALL);
78175944fdaSXiaoliang Yang }
78275944fdaSXiaoliang Yang 
7832f17c050SXiaoliang Yang static void es0_action_set(struct ocelot *ocelot, struct vcap_data *data,
7842f17c050SXiaoliang Yang 			   const struct ocelot_vcap_filter *filter)
7852f17c050SXiaoliang Yang {
7862f17c050SXiaoliang Yang 	const struct vcap_props *vcap = &ocelot->vcap[VCAP_ES0];
7872f17c050SXiaoliang Yang 	const struct ocelot_vcap_action *a = &filter->action;
7882f17c050SXiaoliang Yang 
7892f17c050SXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_ES0_ACT_PUSH_OUTER_TAG,
7902f17c050SXiaoliang Yang 			a->push_outer_tag);
7912f17c050SXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_ES0_ACT_PUSH_INNER_TAG,
7922f17c050SXiaoliang Yang 			a->push_inner_tag);
7932f17c050SXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_A_TPID_SEL,
7942f17c050SXiaoliang Yang 			a->tag_a_tpid_sel);
7952f17c050SXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_A_VID_SEL,
7962f17c050SXiaoliang Yang 			a->tag_a_vid_sel);
7972f17c050SXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_A_PCP_SEL,
7982f17c050SXiaoliang Yang 			a->tag_a_pcp_sel);
7992f17c050SXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_ES0_ACT_VID_A_VAL, a->vid_a_val);
8002f17c050SXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_ES0_ACT_PCP_A_VAL, a->pcp_a_val);
8012f17c050SXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_B_TPID_SEL,
8022f17c050SXiaoliang Yang 			a->tag_b_tpid_sel);
8032f17c050SXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_B_VID_SEL,
8042f17c050SXiaoliang Yang 			a->tag_b_vid_sel);
8052f17c050SXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_B_PCP_SEL,
8062f17c050SXiaoliang Yang 			a->tag_b_pcp_sel);
8072f17c050SXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_ES0_ACT_VID_B_VAL, a->vid_b_val);
8082f17c050SXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_ES0_ACT_PCP_B_VAL, a->pcp_b_val);
8092f17c050SXiaoliang Yang }
8102f17c050SXiaoliang Yang 
8112f17c050SXiaoliang Yang static void es0_entry_set(struct ocelot *ocelot, int ix,
8122f17c050SXiaoliang Yang 			  struct ocelot_vcap_filter *filter)
8132f17c050SXiaoliang Yang {
8142f17c050SXiaoliang Yang 	const struct vcap_props *vcap = &ocelot->vcap[VCAP_ES0];
8152f17c050SXiaoliang Yang 	struct ocelot_vcap_key_vlan *tag = &filter->vlan;
8162f17c050SXiaoliang Yang 	struct ocelot_vcap_u64 payload;
8172f17c050SXiaoliang Yang 	struct vcap_data data;
8182f17c050SXiaoliang Yang 	int row = ix;
8192f17c050SXiaoliang Yang 
8202f17c050SXiaoliang Yang 	memset(&payload, 0, sizeof(payload));
8212f17c050SXiaoliang Yang 	memset(&data, 0, sizeof(data));
8222f17c050SXiaoliang Yang 
8232f17c050SXiaoliang Yang 	/* Read row */
8242f17c050SXiaoliang Yang 	vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_READ, VCAP_SEL_ALL);
8252f17c050SXiaoliang Yang 	vcap_cache2entry(ocelot, vcap, &data);
8262f17c050SXiaoliang Yang 	vcap_cache2action(ocelot, vcap, &data);
8272f17c050SXiaoliang Yang 
8282f17c050SXiaoliang Yang 	data.tg_sw = VCAP_TG_FULL;
8292f17c050SXiaoliang Yang 	data.type = ES0_ACTION_TYPE_NORMAL;
8302f17c050SXiaoliang Yang 	vcap_data_offset_get(vcap, &data, ix);
8312f17c050SXiaoliang Yang 	data.tg = (data.tg & ~data.tg_mask);
8322f17c050SXiaoliang Yang 	if (filter->prio != 0)
8332f17c050SXiaoliang Yang 		data.tg |= data.tg_value;
8342f17c050SXiaoliang Yang 
8352f17c050SXiaoliang Yang 	vcap_key_set(vcap, &data, VCAP_ES0_IGR_PORT, filter->ingress_port.value,
8362f17c050SXiaoliang Yang 		     filter->ingress_port.mask);
8372f17c050SXiaoliang Yang 	vcap_key_set(vcap, &data, VCAP_ES0_EGR_PORT, filter->egress_port.value,
8382f17c050SXiaoliang Yang 		     filter->egress_port.mask);
8392f17c050SXiaoliang Yang 	vcap_key_bit_set(vcap, &data, VCAP_ES0_L2_MC, filter->dmac_mc);
8402f17c050SXiaoliang Yang 	vcap_key_bit_set(vcap, &data, VCAP_ES0_L2_BC, filter->dmac_bc);
8412f17c050SXiaoliang Yang 	vcap_key_set(vcap, &data, VCAP_ES0_VID,
8422f17c050SXiaoliang Yang 		     tag->vid.value, tag->vid.mask);
8432f17c050SXiaoliang Yang 	vcap_key_set(vcap, &data, VCAP_ES0_PCP,
8442f17c050SXiaoliang Yang 		     tag->pcp.value[0], tag->pcp.mask[0]);
8452f17c050SXiaoliang Yang 
8462f17c050SXiaoliang Yang 	es0_action_set(ocelot, &data, filter);
8472f17c050SXiaoliang Yang 	vcap_data_set(data.counter, data.counter_offset,
8482f17c050SXiaoliang Yang 		      vcap->counter_width, filter->stats.pkts);
8492f17c050SXiaoliang Yang 
8502f17c050SXiaoliang Yang 	/* Write row */
8512f17c050SXiaoliang Yang 	vcap_entry2cache(ocelot, vcap, &data);
8522f17c050SXiaoliang Yang 	vcap_action2cache(ocelot, vcap, &data);
8532f17c050SXiaoliang Yang 	vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_WRITE, VCAP_SEL_ALL);
8542f17c050SXiaoliang Yang }
8552f17c050SXiaoliang Yang 
8561397a2ebSVladimir Oltean static void vcap_entry_get(struct ocelot *ocelot, int ix,
8571397a2ebSVladimir Oltean 			   struct ocelot_vcap_filter *filter)
8583c83654fSVladimir Oltean {
8591397a2ebSVladimir Oltean 	const struct vcap_props *vcap = &ocelot->vcap[filter->block_id];
8603c83654fSVladimir Oltean 	struct vcap_data data;
861c1c3993eSVladimir Oltean 	int row, count;
8623c83654fSVladimir Oltean 	u32 cnt;
8633c83654fSVladimir Oltean 
8642f17c050SXiaoliang Yang 	if (filter->block_id == VCAP_ES0)
8652f17c050SXiaoliang Yang 		data.tg_sw = VCAP_TG_FULL;
8662f17c050SXiaoliang Yang 	else
8673c83654fSVladimir Oltean 		data.tg_sw = VCAP_TG_HALF;
8682f17c050SXiaoliang Yang 
869c1c3993eSVladimir Oltean 	count = (1 << (data.tg_sw - 1));
870c1c3993eSVladimir Oltean 	row = (ix / count);
871c1c3993eSVladimir Oltean 	vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_READ, VCAP_SEL_COUNTER);
872c1c3993eSVladimir Oltean 	vcap_cache2action(ocelot, vcap, &data);
873c1c3993eSVladimir Oltean 	vcap_data_offset_get(vcap, &data, ix);
8743c83654fSVladimir Oltean 	cnt = vcap_data_get(data.counter, data.counter_offset,
875c1c3993eSVladimir Oltean 			    vcap->counter_width);
8763c83654fSVladimir Oltean 
877aae4e500SVladimir Oltean 	filter->stats.pkts = cnt;
8783c83654fSVladimir Oltean }
8793c83654fSVladimir Oltean 
8801397a2ebSVladimir Oltean static void vcap_entry_set(struct ocelot *ocelot, int ix,
8811397a2ebSVladimir Oltean 			   struct ocelot_vcap_filter *filter)
8821397a2ebSVladimir Oltean {
88375944fdaSXiaoliang Yang 	if (filter->block_id == VCAP_IS1)
88475944fdaSXiaoliang Yang 		return is1_entry_set(ocelot, ix, filter);
8851397a2ebSVladimir Oltean 	if (filter->block_id == VCAP_IS2)
8861397a2ebSVladimir Oltean 		return is2_entry_set(ocelot, ix, filter);
8872f17c050SXiaoliang Yang 	if (filter->block_id == VCAP_ES0)
8882f17c050SXiaoliang Yang 		return es0_entry_set(ocelot, ix, filter);
8891397a2ebSVladimir Oltean }
8901397a2ebSVladimir Oltean 
89177043c37SXiaoliang Yang struct vcap_policer_entry {
89277043c37SXiaoliang Yang 	struct list_head list;
89377043c37SXiaoliang Yang 	refcount_t refcount;
89477043c37SXiaoliang Yang 	u32 pol_ix;
89577043c37SXiaoliang Yang };
89677043c37SXiaoliang Yang 
89777043c37SXiaoliang Yang int ocelot_vcap_policer_add(struct ocelot *ocelot, u32 pol_ix,
898c73b0ad3SVladimir Oltean 			    struct ocelot_policer *pol)
899c73b0ad3SVladimir Oltean {
900c73b0ad3SVladimir Oltean 	struct qos_policer_conf pp = { 0 };
90177043c37SXiaoliang Yang 	struct vcap_policer_entry *tmp;
90277043c37SXiaoliang Yang 	int ret;
903c73b0ad3SVladimir Oltean 
904c73b0ad3SVladimir Oltean 	if (!pol)
905c73b0ad3SVladimir Oltean 		return -EINVAL;
906c73b0ad3SVladimir Oltean 
907c73b0ad3SVladimir Oltean 	pp.mode = MSCC_QOS_RATE_MODE_DATA;
908c73b0ad3SVladimir Oltean 	pp.pir = pol->rate;
909c73b0ad3SVladimir Oltean 	pp.pbs = pol->burst;
910c73b0ad3SVladimir Oltean 
91177043c37SXiaoliang Yang 	list_for_each_entry(tmp, &ocelot->vcap_pol.pol_list, list)
91277043c37SXiaoliang Yang 		if (tmp->pol_ix == pol_ix) {
91377043c37SXiaoliang Yang 			refcount_inc(&tmp->refcount);
91477043c37SXiaoliang Yang 			return 0;
91577043c37SXiaoliang Yang 		}
91677043c37SXiaoliang Yang 
91777043c37SXiaoliang Yang 	tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
91877043c37SXiaoliang Yang 	if (!tmp)
91977043c37SXiaoliang Yang 		return -ENOMEM;
92077043c37SXiaoliang Yang 
92177043c37SXiaoliang Yang 	ret = qos_policer_conf_set(ocelot, 0, pol_ix, &pp);
92277043c37SXiaoliang Yang 	if (ret) {
92377043c37SXiaoliang Yang 		kfree(tmp);
92477043c37SXiaoliang Yang 		return ret;
92577043c37SXiaoliang Yang 	}
92677043c37SXiaoliang Yang 
92777043c37SXiaoliang Yang 	tmp->pol_ix = pol_ix;
92877043c37SXiaoliang Yang 	refcount_set(&tmp->refcount, 1);
92977043c37SXiaoliang Yang 	list_add_tail(&tmp->list, &ocelot->vcap_pol.pol_list);
93077043c37SXiaoliang Yang 
93177043c37SXiaoliang Yang 	return 0;
93277043c37SXiaoliang Yang }
93377043c37SXiaoliang Yang EXPORT_SYMBOL(ocelot_vcap_policer_add);
93477043c37SXiaoliang Yang 
93577043c37SXiaoliang Yang int ocelot_vcap_policer_del(struct ocelot *ocelot, u32 pol_ix)
93677043c37SXiaoliang Yang {
93777043c37SXiaoliang Yang 	struct qos_policer_conf pp = {0};
93877043c37SXiaoliang Yang 	struct vcap_policer_entry *tmp, *n;
93977043c37SXiaoliang Yang 	u8 z = 0;
94077043c37SXiaoliang Yang 
94177043c37SXiaoliang Yang 	list_for_each_entry_safe(tmp, n, &ocelot->vcap_pol.pol_list, list)
94277043c37SXiaoliang Yang 		if (tmp->pol_ix == pol_ix) {
94377043c37SXiaoliang Yang 			z = refcount_dec_and_test(&tmp->refcount);
94477043c37SXiaoliang Yang 			if (z) {
94577043c37SXiaoliang Yang 				list_del(&tmp->list);
94677043c37SXiaoliang Yang 				kfree(tmp);
94777043c37SXiaoliang Yang 			}
94877043c37SXiaoliang Yang 		}
94977043c37SXiaoliang Yang 
95077043c37SXiaoliang Yang 	if (z) {
95177043c37SXiaoliang Yang 		pp.mode = MSCC_QOS_RATE_MODE_DISABLED;
952c73b0ad3SVladimir Oltean 		return qos_policer_conf_set(ocelot, 0, pol_ix, &pp);
953c73b0ad3SVladimir Oltean 	}
954c73b0ad3SVladimir Oltean 
95577043c37SXiaoliang Yang 	return 0;
956c73b0ad3SVladimir Oltean }
95777043c37SXiaoliang Yang EXPORT_SYMBOL(ocelot_vcap_policer_del);
958c73b0ad3SVladimir Oltean 
959c3d427eaSVladimir Oltean static int
960c3d427eaSVladimir Oltean ocelot_vcap_filter_add_aux_resources(struct ocelot *ocelot,
961c3d427eaSVladimir Oltean 				     struct ocelot_vcap_filter *filter,
962c3d427eaSVladimir Oltean 				     struct netlink_ext_ack *extack)
9633c83654fSVladimir Oltean {
964*f2a0e216SVladimir Oltean 	struct ocelot_mirror *m;
96577043c37SXiaoliang Yang 	int ret;
9663c83654fSVladimir Oltean 
967*f2a0e216SVladimir Oltean 	if (filter->block_id == VCAP_IS2 && filter->action.mirror_ena) {
968*f2a0e216SVladimir Oltean 		m = ocelot_mirror_get(ocelot, filter->egress_port.value,
969*f2a0e216SVladimir Oltean 				      extack);
970*f2a0e216SVladimir Oltean 		if (IS_ERR(m))
971*f2a0e216SVladimir Oltean 			return PTR_ERR(m);
972*f2a0e216SVladimir Oltean 	}
973*f2a0e216SVladimir Oltean 
9741397a2ebSVladimir Oltean 	if (filter->block_id == VCAP_IS2 && filter->action.police_ena) {
97577043c37SXiaoliang Yang 		ret = ocelot_vcap_policer_add(ocelot, filter->action.pol_ix,
976ea9d1f30SVladimir Oltean 					      &filter->action.pol);
97777043c37SXiaoliang Yang 		if (ret)
97877043c37SXiaoliang Yang 			return ret;
9793c83654fSVladimir Oltean 	}
9803c83654fSVladimir Oltean 
981c3d427eaSVladimir Oltean 	return 0;
982c3d427eaSVladimir Oltean }
983c3d427eaSVladimir Oltean 
984c3d427eaSVladimir Oltean static void
985c3d427eaSVladimir Oltean ocelot_vcap_filter_del_aux_resources(struct ocelot *ocelot,
986c3d427eaSVladimir Oltean 				     struct ocelot_vcap_filter *filter)
987c3d427eaSVladimir Oltean {
988c3d427eaSVladimir Oltean 	if (filter->block_id == VCAP_IS2 && filter->action.police_ena)
989c3d427eaSVladimir Oltean 		ocelot_vcap_policer_del(ocelot, filter->action.pol_ix);
990*f2a0e216SVladimir Oltean 
991*f2a0e216SVladimir Oltean 	if (filter->block_id == VCAP_IS2 && filter->action.mirror_ena)
992*f2a0e216SVladimir Oltean 		ocelot_mirror_put(ocelot);
993c3d427eaSVladimir Oltean }
994c3d427eaSVladimir Oltean 
995c3d427eaSVladimir Oltean static int ocelot_vcap_filter_add_to_block(struct ocelot *ocelot,
996c3d427eaSVladimir Oltean 					   struct ocelot_vcap_block *block,
997c3d427eaSVladimir Oltean 					   struct ocelot_vcap_filter *filter,
998c3d427eaSVladimir Oltean 					   struct netlink_ext_ack *extack)
999c3d427eaSVladimir Oltean {
1000c3d427eaSVladimir Oltean 	struct ocelot_vcap_filter *tmp;
1001c3d427eaSVladimir Oltean 	struct list_head *pos, *n;
1002c3d427eaSVladimir Oltean 	int ret;
1003c3d427eaSVladimir Oltean 
1004c3d427eaSVladimir Oltean 	ret = ocelot_vcap_filter_add_aux_resources(ocelot, filter, extack);
1005c3d427eaSVladimir Oltean 	if (ret)
1006c3d427eaSVladimir Oltean 		return ret;
1007c3d427eaSVladimir Oltean 
10083c83654fSVladimir Oltean 	block->count++;
10093c83654fSVladimir Oltean 
10103c83654fSVladimir Oltean 	if (list_empty(&block->rules)) {
1011aae4e500SVladimir Oltean 		list_add(&filter->list, &block->rules);
101277043c37SXiaoliang Yang 		return 0;
10133c83654fSVladimir Oltean 	}
10143c83654fSVladimir Oltean 
10153c83654fSVladimir Oltean 	list_for_each_safe(pos, n, &block->rules) {
1016aae4e500SVladimir Oltean 		tmp = list_entry(pos, struct ocelot_vcap_filter, list);
1017aae4e500SVladimir Oltean 		if (filter->prio < tmp->prio)
10183c83654fSVladimir Oltean 			break;
10193c83654fSVladimir Oltean 	}
1020aae4e500SVladimir Oltean 	list_add(&filter->list, pos->prev);
102177043c37SXiaoliang Yang 
102277043c37SXiaoliang Yang 	return 0;
10233c83654fSVladimir Oltean }
10243c83654fSVladimir Oltean 
102550c6cc5bSVladimir Oltean static bool ocelot_vcap_filter_equal(const struct ocelot_vcap_filter *a,
102650c6cc5bSVladimir Oltean 				     const struct ocelot_vcap_filter *b)
102750c6cc5bSVladimir Oltean {
102850c6cc5bSVladimir Oltean 	return !memcmp(&a->id, &b->id, sizeof(struct ocelot_vcap_id));
102950c6cc5bSVladimir Oltean }
103050c6cc5bSVladimir Oltean 
1031aae4e500SVladimir Oltean static int ocelot_vcap_block_get_filter_index(struct ocelot_vcap_block *block,
1032aae4e500SVladimir Oltean 					      struct ocelot_vcap_filter *filter)
10333c83654fSVladimir Oltean {
1034aae4e500SVladimir Oltean 	struct ocelot_vcap_filter *tmp;
1035ed5672d8SXiaoliang Yang 	int index = 0;
10363c83654fSVladimir Oltean 
10373c83654fSVladimir Oltean 	list_for_each_entry(tmp, &block->rules, list) {
103850c6cc5bSVladimir Oltean 		if (ocelot_vcap_filter_equal(filter, tmp))
10393c83654fSVladimir Oltean 			return index;
1040ed5672d8SXiaoliang Yang 		index++;
1041ed5672d8SXiaoliang Yang 	}
1042ed5672d8SXiaoliang Yang 
1043ed5672d8SXiaoliang Yang 	return -ENOENT;
10443c83654fSVladimir Oltean }
10453c83654fSVladimir Oltean 
1046aae4e500SVladimir Oltean static struct ocelot_vcap_filter*
1047085f5b91SVladimir Oltean ocelot_vcap_block_find_filter_by_index(struct ocelot_vcap_block *block,
1048aae4e500SVladimir Oltean 				       int index)
10493c83654fSVladimir Oltean {
1050aae4e500SVladimir Oltean 	struct ocelot_vcap_filter *tmp;
10513c83654fSVladimir Oltean 	int i = 0;
10523c83654fSVladimir Oltean 
10533c83654fSVladimir Oltean 	list_for_each_entry(tmp, &block->rules, list) {
10543c83654fSVladimir Oltean 		if (i == index)
10553c83654fSVladimir Oltean 			return tmp;
10563c83654fSVladimir Oltean 		++i;
10573c83654fSVladimir Oltean 	}
10583c83654fSVladimir Oltean 
10593c83654fSVladimir Oltean 	return NULL;
10603c83654fSVladimir Oltean }
10613c83654fSVladimir Oltean 
1062085f5b91SVladimir Oltean struct ocelot_vcap_filter *
1063019d9329SVladimir Oltean ocelot_vcap_block_find_filter_by_id(struct ocelot_vcap_block *block,
1064019d9329SVladimir Oltean 				    unsigned long cookie, bool tc_offload)
1065085f5b91SVladimir Oltean {
1066085f5b91SVladimir Oltean 	struct ocelot_vcap_filter *filter;
1067085f5b91SVladimir Oltean 
1068085f5b91SVladimir Oltean 	list_for_each_entry(filter, &block->rules, list)
106950c6cc5bSVladimir Oltean 		if (filter->id.tc_offload == tc_offload &&
107050c6cc5bSVladimir Oltean 		    filter->id.cookie == cookie)
1071085f5b91SVladimir Oltean 			return filter;
1072085f5b91SVladimir Oltean 
1073085f5b91SVladimir Oltean 	return NULL;
1074085f5b91SVladimir Oltean }
1075e21268efSVladimir Oltean EXPORT_SYMBOL(ocelot_vcap_block_find_filter_by_id);
1076085f5b91SVladimir Oltean 
10773c83654fSVladimir Oltean /* If @on=false, then SNAP, ARP, IP and OAM frames will not match on keys based
10783c83654fSVladimir Oltean  * on destination and source MAC addresses, but only on higher-level protocol
10793c83654fSVladimir Oltean  * information. The only frame types to match on keys containing MAC addresses
10803c83654fSVladimir Oltean  * in this case are non-SNAP, non-ARP, non-IP and non-OAM frames.
10813c83654fSVladimir Oltean  *
10823c83654fSVladimir Oltean  * If @on=true, then the above frame types (SNAP, ARP, IP and OAM) will match
10833c83654fSVladimir Oltean  * on MAC_ETYPE keys such as destination and source MAC on this ingress port.
10843c83654fSVladimir Oltean  * However the setting has the side effect of making these frames not matching
10853c83654fSVladimir Oltean  * on any _other_ keys than MAC_ETYPE ones.
10863c83654fSVladimir Oltean  */
10873c83654fSVladimir Oltean static void ocelot_match_all_as_mac_etype(struct ocelot *ocelot, int port,
1088f854e6f6SVladimir Oltean 					  int lookup, bool on)
10893c83654fSVladimir Oltean {
10903c83654fSVladimir Oltean 	u32 val = 0;
10913c83654fSVladimir Oltean 
10923c83654fSVladimir Oltean 	if (on)
1093f854e6f6SVladimir Oltean 		val = ANA_PORT_VCAP_S2_CFG_S2_SNAP_DIS(BIT(lookup)) |
1094f854e6f6SVladimir Oltean 		      ANA_PORT_VCAP_S2_CFG_S2_ARP_DIS(BIT(lookup)) |
1095f854e6f6SVladimir Oltean 		      ANA_PORT_VCAP_S2_CFG_S2_IP_TCPUDP_DIS(BIT(lookup)) |
1096f854e6f6SVladimir Oltean 		      ANA_PORT_VCAP_S2_CFG_S2_IP_OTHER_DIS(BIT(lookup)) |
1097f854e6f6SVladimir Oltean 		      ANA_PORT_VCAP_S2_CFG_S2_OAM_DIS(BIT(lookup));
10983c83654fSVladimir Oltean 
10993c83654fSVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
1100f854e6f6SVladimir Oltean 		       ANA_PORT_VCAP_S2_CFG_S2_SNAP_DIS(BIT(lookup)) |
1101f854e6f6SVladimir Oltean 		       ANA_PORT_VCAP_S2_CFG_S2_ARP_DIS(BIT(lookup)) |
1102f854e6f6SVladimir Oltean 		       ANA_PORT_VCAP_S2_CFG_S2_IP_TCPUDP_DIS(BIT(lookup)) |
1103f854e6f6SVladimir Oltean 		       ANA_PORT_VCAP_S2_CFG_S2_IP_OTHER_DIS(BIT(lookup)) |
1104f854e6f6SVladimir Oltean 		       ANA_PORT_VCAP_S2_CFG_S2_OAM_DIS(BIT(lookup)),
11053c83654fSVladimir Oltean 		       ANA_PORT_VCAP_S2_CFG, port);
11063c83654fSVladimir Oltean }
11073c83654fSVladimir Oltean 
1108aae4e500SVladimir Oltean static bool
1109aae4e500SVladimir Oltean ocelot_vcap_is_problematic_mac_etype(struct ocelot_vcap_filter *filter)
11103c83654fSVladimir Oltean {
11113c83654fSVladimir Oltean 	u16 proto, mask;
11123c83654fSVladimir Oltean 
1113aae4e500SVladimir Oltean 	if (filter->key_type != OCELOT_VCAP_KEY_ETYPE)
11143c83654fSVladimir Oltean 		return false;
11153c83654fSVladimir Oltean 
1116aae4e500SVladimir Oltean 	proto = ntohs(*(__be16 *)filter->key.etype.etype.value);
1117aae4e500SVladimir Oltean 	mask = ntohs(*(__be16 *)filter->key.etype.etype.mask);
11183c83654fSVladimir Oltean 
11193c83654fSVladimir Oltean 	/* ETH_P_ALL match, so all protocols below are included */
11203c83654fSVladimir Oltean 	if (mask == 0)
11213c83654fSVladimir Oltean 		return true;
11223c83654fSVladimir Oltean 	if (proto == ETH_P_ARP)
11233c83654fSVladimir Oltean 		return true;
11243c83654fSVladimir Oltean 	if (proto == ETH_P_IP)
11253c83654fSVladimir Oltean 		return true;
11263c83654fSVladimir Oltean 	if (proto == ETH_P_IPV6)
11273c83654fSVladimir Oltean 		return true;
11283c83654fSVladimir Oltean 
11293c83654fSVladimir Oltean 	return false;
11303c83654fSVladimir Oltean }
11313c83654fSVladimir Oltean 
1132aae4e500SVladimir Oltean static bool
1133aae4e500SVladimir Oltean ocelot_vcap_is_problematic_non_mac_etype(struct ocelot_vcap_filter *filter)
11343c83654fSVladimir Oltean {
1135aae4e500SVladimir Oltean 	if (filter->key_type == OCELOT_VCAP_KEY_SNAP)
11363c83654fSVladimir Oltean 		return true;
1137aae4e500SVladimir Oltean 	if (filter->key_type == OCELOT_VCAP_KEY_ARP)
11383c83654fSVladimir Oltean 		return true;
1139aae4e500SVladimir Oltean 	if (filter->key_type == OCELOT_VCAP_KEY_IPV4)
11403c83654fSVladimir Oltean 		return true;
1141aae4e500SVladimir Oltean 	if (filter->key_type == OCELOT_VCAP_KEY_IPV6)
11423c83654fSVladimir Oltean 		return true;
11433c83654fSVladimir Oltean 	return false;
11443c83654fSVladimir Oltean }
11453c83654fSVladimir Oltean 
1146aae4e500SVladimir Oltean static bool
1147aae4e500SVladimir Oltean ocelot_exclusive_mac_etype_filter_rules(struct ocelot *ocelot,
1148aae4e500SVladimir Oltean 					struct ocelot_vcap_filter *filter)
11493c83654fSVladimir Oltean {
11501397a2ebSVladimir Oltean 	struct ocelot_vcap_block *block = &ocelot->block[filter->block_id];
1151aae4e500SVladimir Oltean 	struct ocelot_vcap_filter *tmp;
11523c83654fSVladimir Oltean 	unsigned long port;
11533c83654fSVladimir Oltean 	int i;
11543c83654fSVladimir Oltean 
1155f854e6f6SVladimir Oltean 	/* We only have the S2_IP_TCPUDP_DIS set of knobs for VCAP IS2 */
1156f854e6f6SVladimir Oltean 	if (filter->block_id != VCAP_IS2)
1157f854e6f6SVladimir Oltean 		return true;
1158f854e6f6SVladimir Oltean 
1159aae4e500SVladimir Oltean 	if (ocelot_vcap_is_problematic_mac_etype(filter)) {
11603c83654fSVladimir Oltean 		/* Search for any non-MAC_ETYPE rules on the port */
11613c83654fSVladimir Oltean 		for (i = 0; i < block->count; i++) {
1162085f5b91SVladimir Oltean 			tmp = ocelot_vcap_block_find_filter_by_index(block, i);
1163aae4e500SVladimir Oltean 			if (tmp->ingress_port_mask & filter->ingress_port_mask &&
1164f854e6f6SVladimir Oltean 			    tmp->lookup == filter->lookup &&
1165aae4e500SVladimir Oltean 			    ocelot_vcap_is_problematic_non_mac_etype(tmp))
11663c83654fSVladimir Oltean 				return false;
11673c83654fSVladimir Oltean 		}
11683c83654fSVladimir Oltean 
1169aae4e500SVladimir Oltean 		for_each_set_bit(port, &filter->ingress_port_mask,
11703c83654fSVladimir Oltean 				 ocelot->num_phys_ports)
1171f854e6f6SVladimir Oltean 			ocelot_match_all_as_mac_etype(ocelot, port,
1172f854e6f6SVladimir Oltean 						      filter->lookup, true);
1173aae4e500SVladimir Oltean 	} else if (ocelot_vcap_is_problematic_non_mac_etype(filter)) {
11743c83654fSVladimir Oltean 		/* Search for any MAC_ETYPE rules on the port */
11753c83654fSVladimir Oltean 		for (i = 0; i < block->count; i++) {
1176085f5b91SVladimir Oltean 			tmp = ocelot_vcap_block_find_filter_by_index(block, i);
1177aae4e500SVladimir Oltean 			if (tmp->ingress_port_mask & filter->ingress_port_mask &&
1178f854e6f6SVladimir Oltean 			    tmp->lookup == filter->lookup &&
1179aae4e500SVladimir Oltean 			    ocelot_vcap_is_problematic_mac_etype(tmp))
11803c83654fSVladimir Oltean 				return false;
11813c83654fSVladimir Oltean 		}
11823c83654fSVladimir Oltean 
1183aae4e500SVladimir Oltean 		for_each_set_bit(port, &filter->ingress_port_mask,
11843c83654fSVladimir Oltean 				 ocelot->num_phys_ports)
1185f854e6f6SVladimir Oltean 			ocelot_match_all_as_mac_etype(ocelot, port,
1186f854e6f6SVladimir Oltean 						      filter->lookup, false);
11873c83654fSVladimir Oltean 	}
11883c83654fSVladimir Oltean 
11893c83654fSVladimir Oltean 	return true;
11903c83654fSVladimir Oltean }
11913c83654fSVladimir Oltean 
1192aae4e500SVladimir Oltean int ocelot_vcap_filter_add(struct ocelot *ocelot,
1193aae4e500SVladimir Oltean 			   struct ocelot_vcap_filter *filter,
11943c83654fSVladimir Oltean 			   struct netlink_ext_ack *extack)
11953c83654fSVladimir Oltean {
11961397a2ebSVladimir Oltean 	struct ocelot_vcap_block *block = &ocelot->block[filter->block_id];
119777043c37SXiaoliang Yang 	int i, index, ret;
11983c83654fSVladimir Oltean 
1199aae4e500SVladimir Oltean 	if (!ocelot_exclusive_mac_etype_filter_rules(ocelot, filter)) {
12003c83654fSVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
1201f854e6f6SVladimir Oltean 				   "Cannot mix MAC_ETYPE with non-MAC_ETYPE rules, use the other IS2 lookup");
12023c83654fSVladimir Oltean 		return -EBUSY;
12033c83654fSVladimir Oltean 	}
12043c83654fSVladimir Oltean 
1205aae4e500SVladimir Oltean 	/* Add filter to the linked list */
1206c3d427eaSVladimir Oltean 	ret = ocelot_vcap_filter_add_to_block(ocelot, block, filter, extack);
120777043c37SXiaoliang Yang 	if (ret)
120877043c37SXiaoliang Yang 		return ret;
12093c83654fSVladimir Oltean 
1210aae4e500SVladimir Oltean 	/* Get the index of the inserted filter */
1211aae4e500SVladimir Oltean 	index = ocelot_vcap_block_get_filter_index(block, filter);
1212ed5672d8SXiaoliang Yang 	if (index < 0)
1213ed5672d8SXiaoliang Yang 		return index;
12143c83654fSVladimir Oltean 
1215aae4e500SVladimir Oltean 	/* Move down the rules to make place for the new filter */
12163c83654fSVladimir Oltean 	for (i = block->count - 1; i > index; i--) {
1217aae4e500SVladimir Oltean 		struct ocelot_vcap_filter *tmp;
1218aae4e500SVladimir Oltean 
1219085f5b91SVladimir Oltean 		tmp = ocelot_vcap_block_find_filter_by_index(block, i);
12201397a2ebSVladimir Oltean 		vcap_entry_set(ocelot, i, tmp);
12213c83654fSVladimir Oltean 	}
12223c83654fSVladimir Oltean 
1223aae4e500SVladimir Oltean 	/* Now insert the new filter */
12241397a2ebSVladimir Oltean 	vcap_entry_set(ocelot, index, filter);
12253c83654fSVladimir Oltean 	return 0;
12263c83654fSVladimir Oltean }
12270e9bb4e9SVladimir Oltean EXPORT_SYMBOL(ocelot_vcap_filter_add);
12283c83654fSVladimir Oltean 
1229aae4e500SVladimir Oltean static void ocelot_vcap_block_remove_filter(struct ocelot *ocelot,
1230aae4e500SVladimir Oltean 					    struct ocelot_vcap_block *block,
1231aae4e500SVladimir Oltean 					    struct ocelot_vcap_filter *filter)
12323c83654fSVladimir Oltean {
1233c5a0edaeSVladimir Oltean 	struct ocelot_vcap_filter *tmp, *n;
12343c83654fSVladimir Oltean 
1235c5a0edaeSVladimir Oltean 	list_for_each_entry_safe(tmp, n, &block->rules, list) {
123650c6cc5bSVladimir Oltean 		if (ocelot_vcap_filter_equal(filter, tmp)) {
1237c3d427eaSVladimir Oltean 			ocelot_vcap_filter_del_aux_resources(ocelot, tmp);
1238c5a0edaeSVladimir Oltean 			list_del(&tmp->list);
12393c83654fSVladimir Oltean 			kfree(tmp);
12403c83654fSVladimir Oltean 		}
12413c83654fSVladimir Oltean 	}
12423c83654fSVladimir Oltean 
12433c83654fSVladimir Oltean 	block->count--;
12443c83654fSVladimir Oltean }
12453c83654fSVladimir Oltean 
1246aae4e500SVladimir Oltean int ocelot_vcap_filter_del(struct ocelot *ocelot,
1247aae4e500SVladimir Oltean 			   struct ocelot_vcap_filter *filter)
12483c83654fSVladimir Oltean {
12491397a2ebSVladimir Oltean 	struct ocelot_vcap_block *block = &ocelot->block[filter->block_id];
1250aae4e500SVladimir Oltean 	struct ocelot_vcap_filter del_filter;
12513c83654fSVladimir Oltean 	int i, index;
12523c83654fSVladimir Oltean 
1253aae4e500SVladimir Oltean 	memset(&del_filter, 0, sizeof(del_filter));
12543c83654fSVladimir Oltean 
1255aae4e500SVladimir Oltean 	/* Gets index of the filter */
1256aae4e500SVladimir Oltean 	index = ocelot_vcap_block_get_filter_index(block, filter);
1257ed5672d8SXiaoliang Yang 	if (index < 0)
1258ed5672d8SXiaoliang Yang 		return index;
12593c83654fSVladimir Oltean 
1260aae4e500SVladimir Oltean 	/* Delete filter */
1261aae4e500SVladimir Oltean 	ocelot_vcap_block_remove_filter(ocelot, block, filter);
12623c83654fSVladimir Oltean 
1263aae4e500SVladimir Oltean 	/* Move up all the blocks over the deleted filter */
12643c83654fSVladimir Oltean 	for (i = index; i < block->count; i++) {
1265aae4e500SVladimir Oltean 		struct ocelot_vcap_filter *tmp;
1266aae4e500SVladimir Oltean 
1267085f5b91SVladimir Oltean 		tmp = ocelot_vcap_block_find_filter_by_index(block, i);
12681397a2ebSVladimir Oltean 		vcap_entry_set(ocelot, i, tmp);
12693c83654fSVladimir Oltean 	}
12703c83654fSVladimir Oltean 
1271aae4e500SVladimir Oltean 	/* Now delete the last filter, because it is duplicated */
12721397a2ebSVladimir Oltean 	vcap_entry_set(ocelot, block->count, &del_filter);
12733c83654fSVladimir Oltean 
12743c83654fSVladimir Oltean 	return 0;
12753c83654fSVladimir Oltean }
12760e9bb4e9SVladimir Oltean EXPORT_SYMBOL(ocelot_vcap_filter_del);
12773c83654fSVladimir Oltean 
127895706be1SVladimir Oltean int ocelot_vcap_filter_replace(struct ocelot *ocelot,
127995706be1SVladimir Oltean 			       struct ocelot_vcap_filter *filter)
128095706be1SVladimir Oltean {
128195706be1SVladimir Oltean 	struct ocelot_vcap_block *block = &ocelot->block[filter->block_id];
128295706be1SVladimir Oltean 	int index;
128395706be1SVladimir Oltean 
128495706be1SVladimir Oltean 	index = ocelot_vcap_block_get_filter_index(block, filter);
128595706be1SVladimir Oltean 	if (index < 0)
128695706be1SVladimir Oltean 		return index;
128795706be1SVladimir Oltean 
128895706be1SVladimir Oltean 	vcap_entry_set(ocelot, index, filter);
128995706be1SVladimir Oltean 
129095706be1SVladimir Oltean 	return 0;
129195706be1SVladimir Oltean }
129295706be1SVladimir Oltean EXPORT_SYMBOL(ocelot_vcap_filter_replace);
129395706be1SVladimir Oltean 
1294aae4e500SVladimir Oltean int ocelot_vcap_filter_stats_update(struct ocelot *ocelot,
1295aae4e500SVladimir Oltean 				    struct ocelot_vcap_filter *filter)
12963c83654fSVladimir Oltean {
12971397a2ebSVladimir Oltean 	struct ocelot_vcap_block *block = &ocelot->block[filter->block_id];
129898642d1aSVladimir Oltean 	struct ocelot_vcap_filter tmp;
12993c83654fSVladimir Oltean 	int index;
13003c83654fSVladimir Oltean 
1301aae4e500SVladimir Oltean 	index = ocelot_vcap_block_get_filter_index(block, filter);
1302ed5672d8SXiaoliang Yang 	if (index < 0)
1303ed5672d8SXiaoliang Yang 		return index;
1304ed5672d8SXiaoliang Yang 
13051397a2ebSVladimir Oltean 	vcap_entry_get(ocelot, index, filter);
13063c83654fSVladimir Oltean 
13073c83654fSVladimir Oltean 	/* After we get the result we need to clear the counters */
130898642d1aSVladimir Oltean 	tmp = *filter;
130998642d1aSVladimir Oltean 	tmp.stats.pkts = 0;
13101397a2ebSVladimir Oltean 	vcap_entry_set(ocelot, index, &tmp);
13113c83654fSVladimir Oltean 
13123c83654fSVladimir Oltean 	return 0;
13133c83654fSVladimir Oltean }
13143c83654fSVladimir Oltean 
1315c1c3993eSVladimir Oltean static void ocelot_vcap_init_one(struct ocelot *ocelot,
1316c1c3993eSVladimir Oltean 				 const struct vcap_props *vcap)
13173c83654fSVladimir Oltean {
13183c83654fSVladimir Oltean 	struct vcap_data data;
13193c83654fSVladimir Oltean 
13203c83654fSVladimir Oltean 	memset(&data, 0, sizeof(data));
13213c83654fSVladimir Oltean 
1322c1c3993eSVladimir Oltean 	vcap_entry2cache(ocelot, vcap, &data);
1323c1c3993eSVladimir Oltean 	ocelot_target_write(ocelot, vcap->target, vcap->entry_count,
1324c1c3993eSVladimir Oltean 			    VCAP_CORE_MV_CFG);
1325c1c3993eSVladimir Oltean 	vcap_cmd(ocelot, vcap, 0, VCAP_CMD_INITIALIZE, VCAP_SEL_ENTRY);
13263c83654fSVladimir Oltean 
1327c1c3993eSVladimir Oltean 	vcap_action2cache(ocelot, vcap, &data);
1328c1c3993eSVladimir Oltean 	ocelot_target_write(ocelot, vcap->target, vcap->action_count,
1329c1c3993eSVladimir Oltean 			    VCAP_CORE_MV_CFG);
1330c1c3993eSVladimir Oltean 	vcap_cmd(ocelot, vcap, 0, VCAP_CMD_INITIALIZE,
13313c83654fSVladimir Oltean 		 VCAP_SEL_ACTION | VCAP_SEL_COUNTER);
1332c1c3993eSVladimir Oltean }
1333c1c3993eSVladimir Oltean 
133420968054SVladimir Oltean static void ocelot_vcap_detect_constants(struct ocelot *ocelot,
133520968054SVladimir Oltean 					 struct vcap_props *vcap)
133620968054SVladimir Oltean {
133720968054SVladimir Oltean 	int counter_memory_width;
133820968054SVladimir Oltean 	int num_default_actions;
133920968054SVladimir Oltean 	int version;
134020968054SVladimir Oltean 
134120968054SVladimir Oltean 	version = ocelot_target_read(ocelot, vcap->target,
134220968054SVladimir Oltean 				     VCAP_CONST_VCAP_VER);
134320968054SVladimir Oltean 	/* Only version 0 VCAP supported for now */
134420968054SVladimir Oltean 	if (WARN_ON(version != 0))
134520968054SVladimir Oltean 		return;
134620968054SVladimir Oltean 
134720968054SVladimir Oltean 	/* Width in bits of type-group field */
134820968054SVladimir Oltean 	vcap->tg_width = ocelot_target_read(ocelot, vcap->target,
134920968054SVladimir Oltean 					    VCAP_CONST_ENTRY_TG_WIDTH);
135020968054SVladimir Oltean 	/* Number of subwords per TCAM row */
135120968054SVladimir Oltean 	vcap->sw_count = ocelot_target_read(ocelot, vcap->target,
135220968054SVladimir Oltean 					    VCAP_CONST_ENTRY_SWCNT);
135320968054SVladimir Oltean 	/* Number of rows in TCAM. There can be this many full keys, or double
135420968054SVladimir Oltean 	 * this number half keys, or 4 times this number quarter keys.
135520968054SVladimir Oltean 	 */
135620968054SVladimir Oltean 	vcap->entry_count = ocelot_target_read(ocelot, vcap->target,
135720968054SVladimir Oltean 					       VCAP_CONST_ENTRY_CNT);
135820968054SVladimir Oltean 	/* Assuming there are 4 subwords per TCAM row, their layout in the
135920968054SVladimir Oltean 	 * actual TCAM (not in the cache) would be:
136020968054SVladimir Oltean 	 *
136120968054SVladimir Oltean 	 * |  SW 3  | TG 3 |  SW 2  | TG 2 |  SW 1  | TG 1 |  SW 0  | TG 0 |
136220968054SVladimir Oltean 	 *
136320968054SVladimir Oltean 	 * (where SW=subword and TG=Type-Group).
136420968054SVladimir Oltean 	 *
136520968054SVladimir Oltean 	 * What VCAP_CONST_ENTRY_CNT is giving us is the width of one full TCAM
136620968054SVladimir Oltean 	 * row. But when software accesses the TCAM through the cache
136720968054SVladimir Oltean 	 * registers, the Type-Group values are written through another set of
136820968054SVladimir Oltean 	 * registers VCAP_TG_DAT, and therefore, it appears as though the 4
136920968054SVladimir Oltean 	 * subwords are contiguous in the cache memory.
137020968054SVladimir Oltean 	 * Important mention: regardless of the number of key entries per row
137120968054SVladimir Oltean 	 * (and therefore of key size: 1 full key or 2 half keys or 4 quarter
137220968054SVladimir Oltean 	 * keys), software always has to configure 4 Type-Group values. For
137320968054SVladimir Oltean 	 * example, in the case of 1 full key, the driver needs to set all 4
137420968054SVladimir Oltean 	 * Type-Group to be full key.
137520968054SVladimir Oltean 	 *
137620968054SVladimir Oltean 	 * For this reason, we need to fix up the value that the hardware is
137720968054SVladimir Oltean 	 * giving us. We don't actually care about the width of the entry in
137820968054SVladimir Oltean 	 * the TCAM. What we care about is the width of the entry in the cache
137920968054SVladimir Oltean 	 * registers, which is how we get to interact with it. And since the
138020968054SVladimir Oltean 	 * VCAP_ENTRY_DAT cache registers access only the subwords and not the
138120968054SVladimir Oltean 	 * Type-Groups, this means we need to subtract the width of the
138220968054SVladimir Oltean 	 * Type-Groups when packing and unpacking key entry data in a TCAM row.
138320968054SVladimir Oltean 	 */
138420968054SVladimir Oltean 	vcap->entry_width = ocelot_target_read(ocelot, vcap->target,
138520968054SVladimir Oltean 					       VCAP_CONST_ENTRY_WIDTH);
138620968054SVladimir Oltean 	vcap->entry_width -= vcap->tg_width * vcap->sw_count;
138720968054SVladimir Oltean 	num_default_actions = ocelot_target_read(ocelot, vcap->target,
138820968054SVladimir Oltean 						 VCAP_CONST_ACTION_DEF_CNT);
138920968054SVladimir Oltean 	vcap->action_count = vcap->entry_count + num_default_actions;
139020968054SVladimir Oltean 	vcap->action_width = ocelot_target_read(ocelot, vcap->target,
139120968054SVladimir Oltean 						VCAP_CONST_ACTION_WIDTH);
139220968054SVladimir Oltean 	/* The width of the counter memory, this is the complete width of all
139320968054SVladimir Oltean 	 * counter-fields associated with one full-word entry. There is one
139420968054SVladimir Oltean 	 * counter per entry sub-word (see CAP_CORE::ENTRY_SWCNT for number of
139520968054SVladimir Oltean 	 * subwords.)
139620968054SVladimir Oltean 	 */
139720968054SVladimir Oltean 	vcap->counter_words = vcap->sw_count;
139820968054SVladimir Oltean 	counter_memory_width = ocelot_target_read(ocelot, vcap->target,
139920968054SVladimir Oltean 						  VCAP_CONST_CNT_WIDTH);
140020968054SVladimir Oltean 	vcap->counter_width = counter_memory_width / vcap->counter_words;
140120968054SVladimir Oltean }
140220968054SVladimir Oltean 
1403c1c3993eSVladimir Oltean int ocelot_vcap_init(struct ocelot *ocelot)
1404c1c3993eSVladimir Oltean {
140520968054SVladimir Oltean 	int i;
14063c83654fSVladimir Oltean 
14073c83654fSVladimir Oltean 	/* Create a policer that will drop the frames for the cpu.
14083c83654fSVladimir Oltean 	 * This policer will be used as action in the acl rules to drop
14093c83654fSVladimir Oltean 	 * frames.
14103c83654fSVladimir Oltean 	 */
14113c83654fSVladimir Oltean 	ocelot_write_gix(ocelot, 0x299, ANA_POL_MODE_CFG,
14123c83654fSVladimir Oltean 			 OCELOT_POLICER_DISCARD);
14133c83654fSVladimir Oltean 	ocelot_write_gix(ocelot, 0x1, ANA_POL_PIR_CFG,
14143c83654fSVladimir Oltean 			 OCELOT_POLICER_DISCARD);
14153c83654fSVladimir Oltean 	ocelot_write_gix(ocelot, 0x3fffff, ANA_POL_PIR_STATE,
14163c83654fSVladimir Oltean 			 OCELOT_POLICER_DISCARD);
14173c83654fSVladimir Oltean 	ocelot_write_gix(ocelot, 0x0, ANA_POL_CIR_CFG,
14183c83654fSVladimir Oltean 			 OCELOT_POLICER_DISCARD);
14193c83654fSVladimir Oltean 	ocelot_write_gix(ocelot, 0x3fffff, ANA_POL_CIR_STATE,
14203c83654fSVladimir Oltean 			 OCELOT_POLICER_DISCARD);
14213c83654fSVladimir Oltean 
142220968054SVladimir Oltean 	for (i = 0; i < OCELOT_NUM_VCAP_BLOCKS; i++) {
14231397a2ebSVladimir Oltean 		struct ocelot_vcap_block *block = &ocelot->block[i];
142420968054SVladimir Oltean 		struct vcap_props *vcap = &ocelot->vcap[i];
142520968054SVladimir Oltean 
14261397a2ebSVladimir Oltean 		INIT_LIST_HEAD(&block->rules);
14271397a2ebSVladimir Oltean 
142820968054SVladimir Oltean 		ocelot_vcap_detect_constants(ocelot, vcap);
142920968054SVladimir Oltean 		ocelot_vcap_init_one(ocelot, vcap);
143020968054SVladimir Oltean 	}
143120968054SVladimir Oltean 
14321397a2ebSVladimir Oltean 	INIT_LIST_HEAD(&ocelot->dummy_rules);
1433e42bd4edSVladimir Oltean 	INIT_LIST_HEAD(&ocelot->traps);
143477043c37SXiaoliang Yang 	INIT_LIST_HEAD(&ocelot->vcap_pol.pol_list);
14353c83654fSVladimir Oltean 
14363c83654fSVladimir Oltean 	return 0;
14373c83654fSVladimir Oltean }
1438