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);
338ea9d1f30SVladimir Oltean 	vcap_action_set(vcap, data, VCAP_IS2_ACT_POLICE_ENA, a->police_ena);
339ea9d1f30SVladimir Oltean 	vcap_action_set(vcap, data, VCAP_IS2_ACT_POLICE_IDX, a->pol_ix);
340ea9d1f30SVladimir Oltean 	vcap_action_set(vcap, data, VCAP_IS2_ACT_CPU_QU_NUM, a->cpu_qu_num);
341ea9d1f30SVladimir Oltean 	vcap_action_set(vcap, data, VCAP_IS2_ACT_CPU_COPY_ENA, a->cpu_copy_ena);
3423c83654fSVladimir Oltean }
3433c83654fSVladimir Oltean 
3443c83654fSVladimir Oltean static void is2_entry_set(struct ocelot *ocelot, int ix,
345aae4e500SVladimir Oltean 			  struct ocelot_vcap_filter *filter)
3463c83654fSVladimir Oltean {
347c1c3993eSVladimir Oltean 	const struct vcap_props *vcap = &ocelot->vcap[VCAP_IS2];
348aae4e500SVladimir Oltean 	struct ocelot_vcap_key_vlan *tag = &filter->vlan;
3493c83654fSVladimir Oltean 	u32 val, msk, type, type_mask = 0xf, i, count;
3503c83654fSVladimir Oltean 	struct ocelot_vcap_u64 payload;
3513c83654fSVladimir Oltean 	struct vcap_data data;
3523c83654fSVladimir Oltean 	int row = (ix / 2);
3533c83654fSVladimir Oltean 
3543c83654fSVladimir Oltean 	memset(&payload, 0, sizeof(payload));
3553c83654fSVladimir Oltean 	memset(&data, 0, sizeof(data));
3563c83654fSVladimir Oltean 
3573c83654fSVladimir Oltean 	/* Read row */
358c1c3993eSVladimir Oltean 	vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_READ, VCAP_SEL_ALL);
359c1c3993eSVladimir Oltean 	vcap_cache2entry(ocelot, vcap, &data);
360c1c3993eSVladimir Oltean 	vcap_cache2action(ocelot, vcap, &data);
3613c83654fSVladimir Oltean 
3623c83654fSVladimir Oltean 	data.tg_sw = VCAP_TG_HALF;
363c1c3993eSVladimir Oltean 	vcap_data_offset_get(vcap, &data, ix);
3643c83654fSVladimir Oltean 	data.tg = (data.tg & ~data.tg_mask);
365aae4e500SVladimir Oltean 	if (filter->prio != 0)
3663c83654fSVladimir Oltean 		data.tg |= data.tg_value;
3673c83654fSVladimir Oltean 
3683c83654fSVladimir Oltean 	data.type = IS2_ACTION_TYPE_NORMAL;
3693c83654fSVladimir Oltean 
370226e9cd8SVladimir Oltean 	vcap_key_set(vcap, &data, VCAP_IS2_HK_PAG, filter->pag, 0xff);
371226e9cd8SVladimir Oltean 	vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_FIRST,
372226e9cd8SVladimir Oltean 			 (filter->lookup == 0) ? OCELOT_VCAP_BIT_1 :
373226e9cd8SVladimir Oltean 			 OCELOT_VCAP_BIT_0);
374c1c3993eSVladimir Oltean 	vcap_key_set(vcap, &data, VCAP_IS2_HK_IGR_PORT_MASK, 0,
375aae4e500SVladimir Oltean 		     ~filter->ingress_port_mask);
376c1c3993eSVladimir Oltean 	vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_FIRST, OCELOT_VCAP_BIT_ANY);
377c1c3993eSVladimir Oltean 	vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_HOST_MATCH,
3783c83654fSVladimir Oltean 			 OCELOT_VCAP_BIT_ANY);
379c1c3993eSVladimir Oltean 	vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L2_MC, filter->dmac_mc);
380c1c3993eSVladimir Oltean 	vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L2_BC, filter->dmac_bc);
381c1c3993eSVladimir Oltean 	vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_VLAN_TAGGED, tag->tagged);
382c1c3993eSVladimir Oltean 	vcap_key_set(vcap, &data, VCAP_IS2_HK_VID,
3833c83654fSVladimir Oltean 		     tag->vid.value, tag->vid.mask);
384c1c3993eSVladimir Oltean 	vcap_key_set(vcap, &data, VCAP_IS2_HK_PCP,
3853c83654fSVladimir Oltean 		     tag->pcp.value[0], tag->pcp.mask[0]);
386c1c3993eSVladimir Oltean 	vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_DEI, tag->dei);
3873c83654fSVladimir Oltean 
388aae4e500SVladimir Oltean 	switch (filter->key_type) {
389aae4e500SVladimir Oltean 	case OCELOT_VCAP_KEY_ETYPE: {
390aae4e500SVladimir Oltean 		struct ocelot_vcap_key_etype *etype = &filter->key.etype;
3913c83654fSVladimir Oltean 
3923c83654fSVladimir Oltean 		type = IS2_TYPE_ETYPE;
393c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_DMAC,
3943c83654fSVladimir Oltean 				   etype->dmac.value, etype->dmac.mask);
395c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_SMAC,
3963c83654fSVladimir Oltean 				   etype->smac.value, etype->smac.mask);
397c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_MAC_ETYPE_ETYPE,
3983c83654fSVladimir Oltean 				   etype->etype.value, etype->etype.mask);
3993c83654fSVladimir Oltean 		/* Clear unused bits */
400c1c3993eSVladimir Oltean 		vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD0,
4013c83654fSVladimir Oltean 			     0, 0);
402c1c3993eSVladimir Oltean 		vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD1,
4033c83654fSVladimir Oltean 			     0, 0);
404c1c3993eSVladimir Oltean 		vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD2,
4053c83654fSVladimir Oltean 			     0, 0);
406c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data,
4073c83654fSVladimir Oltean 				   VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD0,
4083c83654fSVladimir Oltean 				   etype->data.value, etype->data.mask);
4093c83654fSVladimir Oltean 		break;
4103c83654fSVladimir Oltean 	}
411aae4e500SVladimir Oltean 	case OCELOT_VCAP_KEY_LLC: {
412aae4e500SVladimir Oltean 		struct ocelot_vcap_key_llc *llc = &filter->key.llc;
4133c83654fSVladimir Oltean 
4143c83654fSVladimir Oltean 		type = IS2_TYPE_LLC;
415c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_DMAC,
4163c83654fSVladimir Oltean 				   llc->dmac.value, llc->dmac.mask);
417c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_SMAC,
4183c83654fSVladimir Oltean 				   llc->smac.value, llc->smac.mask);
4193c83654fSVladimir Oltean 		for (i = 0; i < 4; i++) {
4203c83654fSVladimir Oltean 			payload.value[i] = llc->llc.value[i];
4213c83654fSVladimir Oltean 			payload.mask[i] = llc->llc.mask[i];
4223c83654fSVladimir Oltean 		}
423c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_MAC_LLC_L2_LLC,
4243c83654fSVladimir Oltean 				   payload.value, payload.mask);
4253c83654fSVladimir Oltean 		break;
4263c83654fSVladimir Oltean 	}
427aae4e500SVladimir Oltean 	case OCELOT_VCAP_KEY_SNAP: {
428aae4e500SVladimir Oltean 		struct ocelot_vcap_key_snap *snap = &filter->key.snap;
4293c83654fSVladimir Oltean 
4303c83654fSVladimir Oltean 		type = IS2_TYPE_SNAP;
431c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_DMAC,
4323c83654fSVladimir Oltean 				   snap->dmac.value, snap->dmac.mask);
433c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_SMAC,
4343c83654fSVladimir Oltean 				   snap->smac.value, snap->smac.mask);
435c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_MAC_SNAP_L2_SNAP,
436aae4e500SVladimir Oltean 				   filter->key.snap.snap.value,
437aae4e500SVladimir Oltean 				   filter->key.snap.snap.mask);
4383c83654fSVladimir Oltean 		break;
4393c83654fSVladimir Oltean 	}
440aae4e500SVladimir Oltean 	case OCELOT_VCAP_KEY_ARP: {
441aae4e500SVladimir Oltean 		struct ocelot_vcap_key_arp *arp = &filter->key.arp;
4423c83654fSVladimir Oltean 
4433c83654fSVladimir Oltean 		type = IS2_TYPE_ARP;
444c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_MAC_ARP_SMAC,
4453c83654fSVladimir Oltean 				   arp->smac.value, arp->smac.mask);
446c1c3993eSVladimir Oltean 		vcap_key_bit_set(vcap, &data,
4473c83654fSVladimir Oltean 				 VCAP_IS2_HK_MAC_ARP_ADDR_SPACE_OK,
4483c83654fSVladimir Oltean 				 arp->ethernet);
449c1c3993eSVladimir Oltean 		vcap_key_bit_set(vcap, &data,
4503c83654fSVladimir Oltean 				 VCAP_IS2_HK_MAC_ARP_PROTO_SPACE_OK,
4513c83654fSVladimir Oltean 				 arp->ip);
452c1c3993eSVladimir Oltean 		vcap_key_bit_set(vcap, &data,
4533c83654fSVladimir Oltean 				 VCAP_IS2_HK_MAC_ARP_LEN_OK,
4543c83654fSVladimir Oltean 				 arp->length);
455c1c3993eSVladimir Oltean 		vcap_key_bit_set(vcap, &data,
4563c83654fSVladimir Oltean 				 VCAP_IS2_HK_MAC_ARP_TARGET_MATCH,
4573c83654fSVladimir Oltean 				 arp->dmac_match);
458c1c3993eSVladimir Oltean 		vcap_key_bit_set(vcap, &data,
4593c83654fSVladimir Oltean 				 VCAP_IS2_HK_MAC_ARP_SENDER_MATCH,
4603c83654fSVladimir Oltean 				 arp->smac_match);
461c1c3993eSVladimir Oltean 		vcap_key_bit_set(vcap, &data,
4623c83654fSVladimir Oltean 				 VCAP_IS2_HK_MAC_ARP_OPCODE_UNKNOWN,
4633c83654fSVladimir Oltean 				 arp->unknown);
4643c83654fSVladimir Oltean 
4653c83654fSVladimir Oltean 		/* OPCODE is inverse, bit 0 is reply flag, bit 1 is RARP flag */
4663c83654fSVladimir Oltean 		val = ((arp->req == OCELOT_VCAP_BIT_0 ? 1 : 0) |
4673c83654fSVladimir Oltean 		       (arp->arp == OCELOT_VCAP_BIT_0 ? 2 : 0));
4683c83654fSVladimir Oltean 		msk = ((arp->req == OCELOT_VCAP_BIT_ANY ? 0 : 1) |
4693c83654fSVladimir Oltean 		       (arp->arp == OCELOT_VCAP_BIT_ANY ? 0 : 2));
470c1c3993eSVladimir Oltean 		vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ARP_OPCODE,
4713c83654fSVladimir Oltean 			     val, msk);
472c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data,
4733c83654fSVladimir Oltean 				   VCAP_IS2_HK_MAC_ARP_L3_IP4_DIP,
4743c83654fSVladimir Oltean 				   arp->dip.value.addr, arp->dip.mask.addr);
475c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data,
4763c83654fSVladimir Oltean 				   VCAP_IS2_HK_MAC_ARP_L3_IP4_SIP,
4773c83654fSVladimir Oltean 				   arp->sip.value.addr, arp->sip.mask.addr);
478c1c3993eSVladimir Oltean 		vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ARP_DIP_EQ_SIP,
4793c83654fSVladimir Oltean 			     0, 0);
4803c83654fSVladimir Oltean 		break;
4813c83654fSVladimir Oltean 	}
482aae4e500SVladimir Oltean 	case OCELOT_VCAP_KEY_IPV4:
483aae4e500SVladimir Oltean 	case OCELOT_VCAP_KEY_IPV6: {
4843c83654fSVladimir Oltean 		enum ocelot_vcap_bit sip_eq_dip, sport_eq_dport, seq_zero, tcp;
4853c83654fSVladimir Oltean 		enum ocelot_vcap_bit ttl, fragment, options, tcp_ack, tcp_urg;
4863c83654fSVladimir Oltean 		enum ocelot_vcap_bit tcp_fin, tcp_syn, tcp_rst, tcp_psh;
487aae4e500SVladimir Oltean 		struct ocelot_vcap_key_ipv4 *ipv4 = NULL;
488aae4e500SVladimir Oltean 		struct ocelot_vcap_key_ipv6 *ipv6 = NULL;
4893c83654fSVladimir Oltean 		struct ocelot_vcap_udp_tcp *sport, *dport;
4903c83654fSVladimir Oltean 		struct ocelot_vcap_ipv4 sip, dip;
4913c83654fSVladimir Oltean 		struct ocelot_vcap_u8 proto, ds;
4923c83654fSVladimir Oltean 		struct ocelot_vcap_u48 *ip_data;
4933c83654fSVladimir Oltean 
494aae4e500SVladimir Oltean 		if (filter->key_type == OCELOT_VCAP_KEY_IPV4) {
495aae4e500SVladimir Oltean 			ipv4 = &filter->key.ipv4;
4963c83654fSVladimir Oltean 			ttl = ipv4->ttl;
4973c83654fSVladimir Oltean 			fragment = ipv4->fragment;
4983c83654fSVladimir Oltean 			options = ipv4->options;
4993c83654fSVladimir Oltean 			proto = ipv4->proto;
5003c83654fSVladimir Oltean 			ds = ipv4->ds;
5013c83654fSVladimir Oltean 			ip_data = &ipv4->data;
5023c83654fSVladimir Oltean 			sip = ipv4->sip;
5033c83654fSVladimir Oltean 			dip = ipv4->dip;
5043c83654fSVladimir Oltean 			sport = &ipv4->sport;
5053c83654fSVladimir Oltean 			dport = &ipv4->dport;
5063c83654fSVladimir Oltean 			tcp_fin = ipv4->tcp_fin;
5073c83654fSVladimir Oltean 			tcp_syn = ipv4->tcp_syn;
5083c83654fSVladimir Oltean 			tcp_rst = ipv4->tcp_rst;
5093c83654fSVladimir Oltean 			tcp_psh = ipv4->tcp_psh;
5103c83654fSVladimir Oltean 			tcp_ack = ipv4->tcp_ack;
5113c83654fSVladimir Oltean 			tcp_urg = ipv4->tcp_urg;
5123c83654fSVladimir Oltean 			sip_eq_dip = ipv4->sip_eq_dip;
5133c83654fSVladimir Oltean 			sport_eq_dport = ipv4->sport_eq_dport;
5143c83654fSVladimir Oltean 			seq_zero = ipv4->seq_zero;
5153c83654fSVladimir Oltean 		} else {
516aae4e500SVladimir Oltean 			ipv6 = &filter->key.ipv6;
5173c83654fSVladimir Oltean 			ttl = ipv6->ttl;
5183c83654fSVladimir Oltean 			fragment = OCELOT_VCAP_BIT_ANY;
5193c83654fSVladimir Oltean 			options = OCELOT_VCAP_BIT_ANY;
5203c83654fSVladimir Oltean 			proto = ipv6->proto;
5213c83654fSVladimir Oltean 			ds = ipv6->ds;
5223c83654fSVladimir Oltean 			ip_data = &ipv6->data;
5233c83654fSVladimir Oltean 			for (i = 0; i < 8; i++) {
5243c83654fSVladimir Oltean 				val = ipv6->sip.value[i + 8];
5253c83654fSVladimir Oltean 				msk = ipv6->sip.mask[i + 8];
5263c83654fSVladimir Oltean 				if (i < 4) {
5273c83654fSVladimir Oltean 					dip.value.addr[i] = val;
5283c83654fSVladimir Oltean 					dip.mask.addr[i] = msk;
5293c83654fSVladimir Oltean 				} else {
5303c83654fSVladimir Oltean 					sip.value.addr[i - 4] = val;
5313c83654fSVladimir Oltean 					sip.mask.addr[i - 4] = msk;
5323c83654fSVladimir Oltean 				}
5333c83654fSVladimir Oltean 			}
5343c83654fSVladimir Oltean 			sport = &ipv6->sport;
5353c83654fSVladimir Oltean 			dport = &ipv6->dport;
5363c83654fSVladimir Oltean 			tcp_fin = ipv6->tcp_fin;
5373c83654fSVladimir Oltean 			tcp_syn = ipv6->tcp_syn;
5383c83654fSVladimir Oltean 			tcp_rst = ipv6->tcp_rst;
5393c83654fSVladimir Oltean 			tcp_psh = ipv6->tcp_psh;
5403c83654fSVladimir Oltean 			tcp_ack = ipv6->tcp_ack;
5413c83654fSVladimir Oltean 			tcp_urg = ipv6->tcp_urg;
5423c83654fSVladimir Oltean 			sip_eq_dip = ipv6->sip_eq_dip;
5433c83654fSVladimir Oltean 			sport_eq_dport = ipv6->sport_eq_dport;
5443c83654fSVladimir Oltean 			seq_zero = ipv6->seq_zero;
5453c83654fSVladimir Oltean 		}
5463c83654fSVladimir Oltean 
547c1c3993eSVladimir Oltean 		vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_IP4,
5483c83654fSVladimir Oltean 				 ipv4 ? OCELOT_VCAP_BIT_1 : OCELOT_VCAP_BIT_0);
549c1c3993eSVladimir Oltean 		vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L3_FRAGMENT,
5503c83654fSVladimir Oltean 				 fragment);
551c1c3993eSVladimir Oltean 		vcap_key_set(vcap, &data, VCAP_IS2_HK_L3_FRAG_OFS_GT0, 0, 0);
552c1c3993eSVladimir Oltean 		vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L3_OPTIONS,
5533c83654fSVladimir Oltean 				 options);
554c1c3993eSVladimir Oltean 		vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_IP4_L3_TTL_GT0,
5553c83654fSVladimir Oltean 				 ttl);
556c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L3_TOS,
5573c83654fSVladimir Oltean 				   ds.value, ds.mask);
558c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L3_IP4_DIP,
5593c83654fSVladimir Oltean 				   dip.value.addr, dip.mask.addr);
560c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L3_IP4_SIP,
5613c83654fSVladimir Oltean 				   sip.value.addr, sip.mask.addr);
562c1c3993eSVladimir Oltean 		vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_DIP_EQ_SIP,
5633c83654fSVladimir Oltean 				 sip_eq_dip);
5643c83654fSVladimir Oltean 		val = proto.value[0];
5653c83654fSVladimir Oltean 		msk = proto.mask[0];
5663c83654fSVladimir Oltean 		type = IS2_TYPE_IP_UDP_TCP;
5673c83654fSVladimir Oltean 		if (msk == 0xff && (val == 6 || val == 17)) {
5683c83654fSVladimir Oltean 			/* UDP/TCP protocol match */
5693c83654fSVladimir Oltean 			tcp = (val == 6 ?
5703c83654fSVladimir Oltean 			       OCELOT_VCAP_BIT_1 : OCELOT_VCAP_BIT_0);
571c1c3993eSVladimir Oltean 			vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_TCP, tcp);
572c1c3993eSVladimir Oltean 			vcap_key_l4_port_set(vcap, &data,
5733c83654fSVladimir Oltean 					     VCAP_IS2_HK_L4_DPORT, dport);
574c1c3993eSVladimir Oltean 			vcap_key_l4_port_set(vcap, &data,
5753c83654fSVladimir Oltean 					     VCAP_IS2_HK_L4_SPORT, sport);
576c1c3993eSVladimir Oltean 			vcap_key_set(vcap, &data, VCAP_IS2_HK_L4_RNG, 0, 0);
577c1c3993eSVladimir Oltean 			vcap_key_bit_set(vcap, &data,
5783c83654fSVladimir Oltean 					 VCAP_IS2_HK_L4_SPORT_EQ_DPORT,
5793c83654fSVladimir Oltean 					 sport_eq_dport);
580c1c3993eSVladimir Oltean 			vcap_key_bit_set(vcap, &data,
5813c83654fSVladimir Oltean 					 VCAP_IS2_HK_L4_SEQUENCE_EQ0,
5823c83654fSVladimir Oltean 					 seq_zero);
583c1c3993eSVladimir Oltean 			vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_FIN,
5843c83654fSVladimir Oltean 					 tcp_fin);
585c1c3993eSVladimir Oltean 			vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_SYN,
5863c83654fSVladimir Oltean 					 tcp_syn);
587c1c3993eSVladimir Oltean 			vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_RST,
5883c83654fSVladimir Oltean 					 tcp_rst);
589c1c3993eSVladimir Oltean 			vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_PSH,
5903c83654fSVladimir Oltean 					 tcp_psh);
591c1c3993eSVladimir Oltean 			vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_ACK,
5923c83654fSVladimir Oltean 					 tcp_ack);
593c1c3993eSVladimir Oltean 			vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_URG,
5943c83654fSVladimir Oltean 					 tcp_urg);
595c1c3993eSVladimir Oltean 			vcap_key_set(vcap, &data, VCAP_IS2_HK_L4_1588_DOM,
5963c83654fSVladimir Oltean 				     0, 0);
597c1c3993eSVladimir Oltean 			vcap_key_set(vcap, &data, VCAP_IS2_HK_L4_1588_VER,
5983c83654fSVladimir Oltean 				     0, 0);
5993c83654fSVladimir Oltean 		} else {
6003c83654fSVladimir Oltean 			if (msk == 0) {
6013c83654fSVladimir Oltean 				/* Any IP protocol match */
6023c83654fSVladimir Oltean 				type_mask = IS2_TYPE_MASK_IP_ANY;
6033c83654fSVladimir Oltean 			} else {
6043c83654fSVladimir Oltean 				/* Non-UDP/TCP protocol match */
6053c83654fSVladimir Oltean 				type = IS2_TYPE_IP_OTHER;
6063c83654fSVladimir Oltean 				for (i = 0; i < 6; i++) {
6073c83654fSVladimir Oltean 					payload.value[i] = ip_data->value[i];
6083c83654fSVladimir Oltean 					payload.mask[i] = ip_data->mask[i];
6093c83654fSVladimir Oltean 				}
6103c83654fSVladimir Oltean 			}
611c1c3993eSVladimir Oltean 			vcap_key_bytes_set(vcap, &data,
6123c83654fSVladimir Oltean 					   VCAP_IS2_HK_IP4_L3_PROTO,
6133c83654fSVladimir Oltean 					   proto.value, proto.mask);
614c1c3993eSVladimir Oltean 			vcap_key_bytes_set(vcap, &data,
6153c83654fSVladimir Oltean 					   VCAP_IS2_HK_L3_PAYLOAD,
6163c83654fSVladimir Oltean 					   payload.value, payload.mask);
6173c83654fSVladimir Oltean 		}
6183c83654fSVladimir Oltean 		break;
6193c83654fSVladimir Oltean 	}
620aae4e500SVladimir Oltean 	case OCELOT_VCAP_KEY_ANY:
6213c83654fSVladimir Oltean 	default:
6223c83654fSVladimir Oltean 		type = 0;
6233c83654fSVladimir Oltean 		type_mask = 0;
624c1c3993eSVladimir Oltean 		count = vcap->entry_width / 2;
6253c83654fSVladimir Oltean 		/* Iterate over the non-common part of the key and
6263c83654fSVladimir Oltean 		 * clear entry data
6273c83654fSVladimir Oltean 		 */
628c1c3993eSVladimir Oltean 		for (i = vcap->keys[VCAP_IS2_HK_L2_DMAC].offset;
6293c83654fSVladimir Oltean 		     i < count; i += ENTRY_WIDTH) {
6303c83654fSVladimir Oltean 			vcap_key_field_set(&data, i, min(32u, count - i), 0, 0);
6313c83654fSVladimir Oltean 		}
6323c83654fSVladimir Oltean 		break;
6333c83654fSVladimir Oltean 	}
6343c83654fSVladimir Oltean 
635c1c3993eSVladimir Oltean 	vcap_key_set(vcap, &data, VCAP_IS2_TYPE, type, type_mask);
636aae4e500SVladimir Oltean 	is2_action_set(ocelot, &data, filter);
6373c83654fSVladimir Oltean 	vcap_data_set(data.counter, data.counter_offset,
638c1c3993eSVladimir Oltean 		      vcap->counter_width, filter->stats.pkts);
6393c83654fSVladimir Oltean 
6403c83654fSVladimir Oltean 	/* Write row */
641c1c3993eSVladimir Oltean 	vcap_entry2cache(ocelot, vcap, &data);
642c1c3993eSVladimir Oltean 	vcap_action2cache(ocelot, vcap, &data);
643c1c3993eSVladimir Oltean 	vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_WRITE, VCAP_SEL_ALL);
6443c83654fSVladimir Oltean }
6453c83654fSVladimir Oltean 
64675944fdaSXiaoliang Yang static void is1_action_set(struct ocelot *ocelot, struct vcap_data *data,
64775944fdaSXiaoliang Yang 			   const struct ocelot_vcap_filter *filter)
64875944fdaSXiaoliang Yang {
64975944fdaSXiaoliang Yang 	const struct vcap_props *vcap = &ocelot->vcap[VCAP_IS1];
65075944fdaSXiaoliang Yang 	const struct ocelot_vcap_action *a = &filter->action;
65175944fdaSXiaoliang Yang 
65275944fdaSXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_IS1_ACT_VID_REPLACE_ENA,
65375944fdaSXiaoliang Yang 			a->vid_replace_ena);
65475944fdaSXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_IS1_ACT_VID_ADD_VAL, a->vid);
65575944fdaSXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_IS1_ACT_VLAN_POP_CNT_ENA,
65675944fdaSXiaoliang Yang 			a->vlan_pop_cnt_ena);
65775944fdaSXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_IS1_ACT_VLAN_POP_CNT,
65875944fdaSXiaoliang Yang 			a->vlan_pop_cnt);
65975944fdaSXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_IS1_ACT_PCP_DEI_ENA, a->pcp_dei_ena);
66075944fdaSXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_IS1_ACT_PCP_VAL, a->pcp);
66175944fdaSXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_IS1_ACT_DEI_VAL, a->dei);
66275944fdaSXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_IS1_ACT_QOS_ENA, a->qos_ena);
66375944fdaSXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_IS1_ACT_QOS_VAL, a->qos_val);
66475944fdaSXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_IS1_ACT_PAG_OVERRIDE_MASK,
66575944fdaSXiaoliang Yang 			a->pag_override_mask);
66675944fdaSXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_IS1_ACT_PAG_VAL, a->pag_val);
66775944fdaSXiaoliang Yang }
66875944fdaSXiaoliang Yang 
66975944fdaSXiaoliang Yang static void is1_entry_set(struct ocelot *ocelot, int ix,
67075944fdaSXiaoliang Yang 			  struct ocelot_vcap_filter *filter)
67175944fdaSXiaoliang Yang {
67275944fdaSXiaoliang Yang 	const struct vcap_props *vcap = &ocelot->vcap[VCAP_IS1];
67375944fdaSXiaoliang Yang 	struct ocelot_vcap_key_vlan *tag = &filter->vlan;
67475944fdaSXiaoliang Yang 	struct ocelot_vcap_u64 payload;
67575944fdaSXiaoliang Yang 	struct vcap_data data;
67675944fdaSXiaoliang Yang 	int row = ix / 2;
67775944fdaSXiaoliang Yang 	u32 type;
67875944fdaSXiaoliang Yang 
67975944fdaSXiaoliang Yang 	memset(&payload, 0, sizeof(payload));
68075944fdaSXiaoliang Yang 	memset(&data, 0, sizeof(data));
68175944fdaSXiaoliang Yang 
68275944fdaSXiaoliang Yang 	/* Read row */
68375944fdaSXiaoliang Yang 	vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_READ, VCAP_SEL_ALL);
68475944fdaSXiaoliang Yang 	vcap_cache2entry(ocelot, vcap, &data);
68575944fdaSXiaoliang Yang 	vcap_cache2action(ocelot, vcap, &data);
68675944fdaSXiaoliang Yang 
68775944fdaSXiaoliang Yang 	data.tg_sw = VCAP_TG_HALF;
68875944fdaSXiaoliang Yang 	data.type = IS1_ACTION_TYPE_NORMAL;
68975944fdaSXiaoliang Yang 	vcap_data_offset_get(vcap, &data, ix);
69075944fdaSXiaoliang Yang 	data.tg = (data.tg & ~data.tg_mask);
69175944fdaSXiaoliang Yang 	if (filter->prio != 0)
69275944fdaSXiaoliang Yang 		data.tg |= data.tg_value;
69375944fdaSXiaoliang Yang 
694226e9cd8SVladimir Oltean 	vcap_key_set(vcap, &data, VCAP_IS1_HK_LOOKUP, filter->lookup, 0x3);
69575944fdaSXiaoliang Yang 	vcap_key_set(vcap, &data, VCAP_IS1_HK_IGR_PORT_MASK, 0,
69675944fdaSXiaoliang Yang 		     ~filter->ingress_port_mask);
69775944fdaSXiaoliang Yang 	vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_L2_MC, filter->dmac_mc);
69875944fdaSXiaoliang Yang 	vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_L2_BC, filter->dmac_bc);
69975944fdaSXiaoliang Yang 	vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_VLAN_TAGGED, tag->tagged);
70075944fdaSXiaoliang Yang 	vcap_key_set(vcap, &data, VCAP_IS1_HK_VID,
70175944fdaSXiaoliang Yang 		     tag->vid.value, tag->vid.mask);
70275944fdaSXiaoliang Yang 	vcap_key_set(vcap, &data, VCAP_IS1_HK_PCP,
70375944fdaSXiaoliang Yang 		     tag->pcp.value[0], tag->pcp.mask[0]);
70475944fdaSXiaoliang Yang 	type = IS1_TYPE_S1_NORMAL;
70575944fdaSXiaoliang Yang 
70675944fdaSXiaoliang Yang 	switch (filter->key_type) {
70775944fdaSXiaoliang Yang 	case OCELOT_VCAP_KEY_ETYPE: {
70875944fdaSXiaoliang Yang 		struct ocelot_vcap_key_etype *etype = &filter->key.etype;
70975944fdaSXiaoliang Yang 
71075944fdaSXiaoliang Yang 		vcap_key_bytes_set(vcap, &data, VCAP_IS1_HK_L2_SMAC,
71175944fdaSXiaoliang Yang 				   etype->smac.value, etype->smac.mask);
71275944fdaSXiaoliang Yang 		vcap_key_bytes_set(vcap, &data, VCAP_IS1_HK_ETYPE,
71375944fdaSXiaoliang Yang 				   etype->etype.value, etype->etype.mask);
71475944fdaSXiaoliang Yang 		break;
71575944fdaSXiaoliang Yang 	}
71675944fdaSXiaoliang Yang 	case OCELOT_VCAP_KEY_IPV4: {
71775944fdaSXiaoliang Yang 		struct ocelot_vcap_key_ipv4 *ipv4 = &filter->key.ipv4;
71875944fdaSXiaoliang Yang 		struct ocelot_vcap_udp_tcp *sport = &ipv4->sport;
71975944fdaSXiaoliang Yang 		struct ocelot_vcap_udp_tcp *dport = &ipv4->dport;
72075944fdaSXiaoliang Yang 		enum ocelot_vcap_bit tcp_udp = OCELOT_VCAP_BIT_0;
72175944fdaSXiaoliang Yang 		struct ocelot_vcap_u8 proto = ipv4->proto;
72275944fdaSXiaoliang Yang 		struct ocelot_vcap_ipv4 sip = ipv4->sip;
72375944fdaSXiaoliang Yang 		u32 val, msk;
72475944fdaSXiaoliang Yang 
72575944fdaSXiaoliang Yang 		vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_IP_SNAP,
72675944fdaSXiaoliang Yang 				 OCELOT_VCAP_BIT_1);
72775944fdaSXiaoliang Yang 		vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_IP4,
72875944fdaSXiaoliang Yang 				 OCELOT_VCAP_BIT_1);
72975944fdaSXiaoliang Yang 		vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_ETYPE_LEN,
73075944fdaSXiaoliang Yang 				 OCELOT_VCAP_BIT_1);
73175944fdaSXiaoliang Yang 		vcap_key_bytes_set(vcap, &data, VCAP_IS1_HK_L3_IP4_SIP,
73275944fdaSXiaoliang Yang 				   sip.value.addr, sip.mask.addr);
73375944fdaSXiaoliang Yang 
73475944fdaSXiaoliang Yang 		val = proto.value[0];
73575944fdaSXiaoliang Yang 		msk = proto.mask[0];
73675944fdaSXiaoliang Yang 
73775944fdaSXiaoliang Yang 		if ((val == NEXTHDR_TCP || val == NEXTHDR_UDP) && msk == 0xff)
73875944fdaSXiaoliang Yang 			tcp_udp = OCELOT_VCAP_BIT_1;
73975944fdaSXiaoliang Yang 		vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_TCP_UDP, tcp_udp);
74075944fdaSXiaoliang Yang 
74175944fdaSXiaoliang Yang 		if (tcp_udp) {
74275944fdaSXiaoliang Yang 			enum ocelot_vcap_bit tcp = OCELOT_VCAP_BIT_0;
74375944fdaSXiaoliang Yang 
74475944fdaSXiaoliang Yang 			if (val == NEXTHDR_TCP)
74575944fdaSXiaoliang Yang 				tcp = OCELOT_VCAP_BIT_1;
74675944fdaSXiaoliang Yang 
74775944fdaSXiaoliang Yang 			vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_TCP, tcp);
74875944fdaSXiaoliang Yang 			vcap_key_l4_port_set(vcap, &data, VCAP_IS1_HK_L4_SPORT,
74975944fdaSXiaoliang Yang 					     sport);
75075944fdaSXiaoliang Yang 			/* Overloaded field */
75175944fdaSXiaoliang Yang 			vcap_key_l4_port_set(vcap, &data, VCAP_IS1_HK_ETYPE,
75275944fdaSXiaoliang Yang 					     dport);
75375944fdaSXiaoliang Yang 		} else {
75475944fdaSXiaoliang Yang 			/* IPv4 "other" frame */
75575944fdaSXiaoliang Yang 			struct ocelot_vcap_u16 etype = {0};
75675944fdaSXiaoliang Yang 
75775944fdaSXiaoliang Yang 			/* Overloaded field */
75875944fdaSXiaoliang Yang 			etype.value[0] = proto.value[0];
75975944fdaSXiaoliang Yang 			etype.mask[0] = proto.mask[0];
76075944fdaSXiaoliang Yang 
76175944fdaSXiaoliang Yang 			vcap_key_bytes_set(vcap, &data, VCAP_IS1_HK_ETYPE,
76275944fdaSXiaoliang Yang 					   etype.value, etype.mask);
76375944fdaSXiaoliang Yang 		}
76475944fdaSXiaoliang Yang 	}
76575944fdaSXiaoliang Yang 	default:
76675944fdaSXiaoliang Yang 		break;
76775944fdaSXiaoliang Yang 	}
76875944fdaSXiaoliang Yang 	vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_TYPE,
76975944fdaSXiaoliang Yang 			 type ? OCELOT_VCAP_BIT_1 : OCELOT_VCAP_BIT_0);
77075944fdaSXiaoliang Yang 
77175944fdaSXiaoliang Yang 	is1_action_set(ocelot, &data, filter);
77275944fdaSXiaoliang Yang 	vcap_data_set(data.counter, data.counter_offset,
77375944fdaSXiaoliang Yang 		      vcap->counter_width, filter->stats.pkts);
77475944fdaSXiaoliang Yang 
77575944fdaSXiaoliang Yang 	/* Write row */
77675944fdaSXiaoliang Yang 	vcap_entry2cache(ocelot, vcap, &data);
77775944fdaSXiaoliang Yang 	vcap_action2cache(ocelot, vcap, &data);
77875944fdaSXiaoliang Yang 	vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_WRITE, VCAP_SEL_ALL);
77975944fdaSXiaoliang Yang }
78075944fdaSXiaoliang Yang 
7812f17c050SXiaoliang Yang static void es0_action_set(struct ocelot *ocelot, struct vcap_data *data,
7822f17c050SXiaoliang Yang 			   const struct ocelot_vcap_filter *filter)
7832f17c050SXiaoliang Yang {
7842f17c050SXiaoliang Yang 	const struct vcap_props *vcap = &ocelot->vcap[VCAP_ES0];
7852f17c050SXiaoliang Yang 	const struct ocelot_vcap_action *a = &filter->action;
7862f17c050SXiaoliang Yang 
7872f17c050SXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_ES0_ACT_PUSH_OUTER_TAG,
7882f17c050SXiaoliang Yang 			a->push_outer_tag);
7892f17c050SXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_ES0_ACT_PUSH_INNER_TAG,
7902f17c050SXiaoliang Yang 			a->push_inner_tag);
7912f17c050SXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_A_TPID_SEL,
7922f17c050SXiaoliang Yang 			a->tag_a_tpid_sel);
7932f17c050SXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_A_VID_SEL,
7942f17c050SXiaoliang Yang 			a->tag_a_vid_sel);
7952f17c050SXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_A_PCP_SEL,
7962f17c050SXiaoliang Yang 			a->tag_a_pcp_sel);
7972f17c050SXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_ES0_ACT_VID_A_VAL, a->vid_a_val);
7982f17c050SXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_ES0_ACT_PCP_A_VAL, a->pcp_a_val);
7992f17c050SXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_B_TPID_SEL,
8002f17c050SXiaoliang Yang 			a->tag_b_tpid_sel);
8012f17c050SXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_B_VID_SEL,
8022f17c050SXiaoliang Yang 			a->tag_b_vid_sel);
8032f17c050SXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_B_PCP_SEL,
8042f17c050SXiaoliang Yang 			a->tag_b_pcp_sel);
8052f17c050SXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_ES0_ACT_VID_B_VAL, a->vid_b_val);
8062f17c050SXiaoliang Yang 	vcap_action_set(vcap, data, VCAP_ES0_ACT_PCP_B_VAL, a->pcp_b_val);
8072f17c050SXiaoliang Yang }
8082f17c050SXiaoliang Yang 
8092f17c050SXiaoliang Yang static void es0_entry_set(struct ocelot *ocelot, int ix,
8102f17c050SXiaoliang Yang 			  struct ocelot_vcap_filter *filter)
8112f17c050SXiaoliang Yang {
8122f17c050SXiaoliang Yang 	const struct vcap_props *vcap = &ocelot->vcap[VCAP_ES0];
8132f17c050SXiaoliang Yang 	struct ocelot_vcap_key_vlan *tag = &filter->vlan;
8142f17c050SXiaoliang Yang 	struct ocelot_vcap_u64 payload;
8152f17c050SXiaoliang Yang 	struct vcap_data data;
8162f17c050SXiaoliang Yang 	int row = ix;
8172f17c050SXiaoliang Yang 
8182f17c050SXiaoliang Yang 	memset(&payload, 0, sizeof(payload));
8192f17c050SXiaoliang Yang 	memset(&data, 0, sizeof(data));
8202f17c050SXiaoliang Yang 
8212f17c050SXiaoliang Yang 	/* Read row */
8222f17c050SXiaoliang Yang 	vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_READ, VCAP_SEL_ALL);
8232f17c050SXiaoliang Yang 	vcap_cache2entry(ocelot, vcap, &data);
8242f17c050SXiaoliang Yang 	vcap_cache2action(ocelot, vcap, &data);
8252f17c050SXiaoliang Yang 
8262f17c050SXiaoliang Yang 	data.tg_sw = VCAP_TG_FULL;
8272f17c050SXiaoliang Yang 	data.type = ES0_ACTION_TYPE_NORMAL;
8282f17c050SXiaoliang Yang 	vcap_data_offset_get(vcap, &data, ix);
8292f17c050SXiaoliang Yang 	data.tg = (data.tg & ~data.tg_mask);
8302f17c050SXiaoliang Yang 	if (filter->prio != 0)
8312f17c050SXiaoliang Yang 		data.tg |= data.tg_value;
8322f17c050SXiaoliang Yang 
8332f17c050SXiaoliang Yang 	vcap_key_set(vcap, &data, VCAP_ES0_IGR_PORT, filter->ingress_port.value,
8342f17c050SXiaoliang Yang 		     filter->ingress_port.mask);
8352f17c050SXiaoliang Yang 	vcap_key_set(vcap, &data, VCAP_ES0_EGR_PORT, filter->egress_port.value,
8362f17c050SXiaoliang Yang 		     filter->egress_port.mask);
8372f17c050SXiaoliang Yang 	vcap_key_bit_set(vcap, &data, VCAP_ES0_L2_MC, filter->dmac_mc);
8382f17c050SXiaoliang Yang 	vcap_key_bit_set(vcap, &data, VCAP_ES0_L2_BC, filter->dmac_bc);
8392f17c050SXiaoliang Yang 	vcap_key_set(vcap, &data, VCAP_ES0_VID,
8402f17c050SXiaoliang Yang 		     tag->vid.value, tag->vid.mask);
8412f17c050SXiaoliang Yang 	vcap_key_set(vcap, &data, VCAP_ES0_PCP,
8422f17c050SXiaoliang Yang 		     tag->pcp.value[0], tag->pcp.mask[0]);
8432f17c050SXiaoliang Yang 
8442f17c050SXiaoliang Yang 	es0_action_set(ocelot, &data, filter);
8452f17c050SXiaoliang Yang 	vcap_data_set(data.counter, data.counter_offset,
8462f17c050SXiaoliang Yang 		      vcap->counter_width, filter->stats.pkts);
8472f17c050SXiaoliang Yang 
8482f17c050SXiaoliang Yang 	/* Write row */
8492f17c050SXiaoliang Yang 	vcap_entry2cache(ocelot, vcap, &data);
8502f17c050SXiaoliang Yang 	vcap_action2cache(ocelot, vcap, &data);
8512f17c050SXiaoliang Yang 	vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_WRITE, VCAP_SEL_ALL);
8522f17c050SXiaoliang Yang }
8532f17c050SXiaoliang Yang 
8541397a2ebSVladimir Oltean static void vcap_entry_get(struct ocelot *ocelot, int ix,
8551397a2ebSVladimir Oltean 			   struct ocelot_vcap_filter *filter)
8563c83654fSVladimir Oltean {
8571397a2ebSVladimir Oltean 	const struct vcap_props *vcap = &ocelot->vcap[filter->block_id];
8583c83654fSVladimir Oltean 	struct vcap_data data;
859c1c3993eSVladimir Oltean 	int row, count;
8603c83654fSVladimir Oltean 	u32 cnt;
8613c83654fSVladimir Oltean 
8622f17c050SXiaoliang Yang 	if (filter->block_id == VCAP_ES0)
8632f17c050SXiaoliang Yang 		data.tg_sw = VCAP_TG_FULL;
8642f17c050SXiaoliang Yang 	else
8653c83654fSVladimir Oltean 		data.tg_sw = VCAP_TG_HALF;
8662f17c050SXiaoliang Yang 
867c1c3993eSVladimir Oltean 	count = (1 << (data.tg_sw - 1));
868c1c3993eSVladimir Oltean 	row = (ix / count);
869c1c3993eSVladimir Oltean 	vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_READ, VCAP_SEL_COUNTER);
870c1c3993eSVladimir Oltean 	vcap_cache2action(ocelot, vcap, &data);
871c1c3993eSVladimir Oltean 	vcap_data_offset_get(vcap, &data, ix);
8723c83654fSVladimir Oltean 	cnt = vcap_data_get(data.counter, data.counter_offset,
873c1c3993eSVladimir Oltean 			    vcap->counter_width);
8743c83654fSVladimir Oltean 
875aae4e500SVladimir Oltean 	filter->stats.pkts = cnt;
8763c83654fSVladimir Oltean }
8773c83654fSVladimir Oltean 
8781397a2ebSVladimir Oltean static void vcap_entry_set(struct ocelot *ocelot, int ix,
8791397a2ebSVladimir Oltean 			   struct ocelot_vcap_filter *filter)
8801397a2ebSVladimir Oltean {
88175944fdaSXiaoliang Yang 	if (filter->block_id == VCAP_IS1)
88275944fdaSXiaoliang Yang 		return is1_entry_set(ocelot, ix, filter);
8831397a2ebSVladimir Oltean 	if (filter->block_id == VCAP_IS2)
8841397a2ebSVladimir Oltean 		return is2_entry_set(ocelot, ix, filter);
8852f17c050SXiaoliang Yang 	if (filter->block_id == VCAP_ES0)
8862f17c050SXiaoliang Yang 		return es0_entry_set(ocelot, ix, filter);
8871397a2ebSVladimir Oltean }
8881397a2ebSVladimir Oltean 
889c73b0ad3SVladimir Oltean static int ocelot_vcap_policer_add(struct ocelot *ocelot, u32 pol_ix,
890c73b0ad3SVladimir Oltean 				   struct ocelot_policer *pol)
891c73b0ad3SVladimir Oltean {
892c73b0ad3SVladimir Oltean 	struct qos_policer_conf pp = { 0 };
893c73b0ad3SVladimir Oltean 
894c73b0ad3SVladimir Oltean 	if (!pol)
895c73b0ad3SVladimir Oltean 		return -EINVAL;
896c73b0ad3SVladimir Oltean 
897c73b0ad3SVladimir Oltean 	pp.mode = MSCC_QOS_RATE_MODE_DATA;
898c73b0ad3SVladimir Oltean 	pp.pir = pol->rate;
899c73b0ad3SVladimir Oltean 	pp.pbs = pol->burst;
900c73b0ad3SVladimir Oltean 
901c73b0ad3SVladimir Oltean 	return qos_policer_conf_set(ocelot, 0, pol_ix, &pp);
902c73b0ad3SVladimir Oltean }
903c73b0ad3SVladimir Oltean 
904c73b0ad3SVladimir Oltean static void ocelot_vcap_policer_del(struct ocelot *ocelot,
905c73b0ad3SVladimir Oltean 				    struct ocelot_vcap_block *block,
906c73b0ad3SVladimir Oltean 				    u32 pol_ix)
907c73b0ad3SVladimir Oltean {
908c73b0ad3SVladimir Oltean 	struct ocelot_vcap_filter *filter;
909c73b0ad3SVladimir Oltean 	struct qos_policer_conf pp = {0};
910c73b0ad3SVladimir Oltean 	int index = -1;
911c73b0ad3SVladimir Oltean 
912c73b0ad3SVladimir Oltean 	if (pol_ix < block->pol_lpr)
913c73b0ad3SVladimir Oltean 		return;
914c73b0ad3SVladimir Oltean 
915c73b0ad3SVladimir Oltean 	list_for_each_entry(filter, &block->rules, list) {
916c73b0ad3SVladimir Oltean 		index++;
9171397a2ebSVladimir Oltean 		if (filter->block_id == VCAP_IS2 &&
9181397a2ebSVladimir Oltean 		    filter->action.police_ena &&
919ea9d1f30SVladimir Oltean 		    filter->action.pol_ix < pol_ix) {
920ea9d1f30SVladimir Oltean 			filter->action.pol_ix += 1;
921ea9d1f30SVladimir Oltean 			ocelot_vcap_policer_add(ocelot, filter->action.pol_ix,
922ea9d1f30SVladimir Oltean 						&filter->action.pol);
923c73b0ad3SVladimir Oltean 			is2_entry_set(ocelot, index, filter);
924c73b0ad3SVladimir Oltean 		}
925c73b0ad3SVladimir Oltean 	}
926c73b0ad3SVladimir Oltean 
927c73b0ad3SVladimir Oltean 	pp.mode = MSCC_QOS_RATE_MODE_DISABLED;
928c73b0ad3SVladimir Oltean 	qos_policer_conf_set(ocelot, 0, pol_ix, &pp);
929c73b0ad3SVladimir Oltean 
930c73b0ad3SVladimir Oltean 	block->pol_lpr++;
931c73b0ad3SVladimir Oltean }
932c73b0ad3SVladimir Oltean 
933aae4e500SVladimir Oltean static void ocelot_vcap_filter_add_to_block(struct ocelot *ocelot,
934aae4e500SVladimir Oltean 					    struct ocelot_vcap_block *block,
935aae4e500SVladimir Oltean 					    struct ocelot_vcap_filter *filter)
9363c83654fSVladimir Oltean {
937aae4e500SVladimir Oltean 	struct ocelot_vcap_filter *tmp;
9383c83654fSVladimir Oltean 	struct list_head *pos, *n;
9393c83654fSVladimir Oltean 
9401397a2ebSVladimir Oltean 	if (filter->block_id == VCAP_IS2 && filter->action.police_ena) {
9413c83654fSVladimir Oltean 		block->pol_lpr--;
942ea9d1f30SVladimir Oltean 		filter->action.pol_ix = block->pol_lpr;
943ea9d1f30SVladimir Oltean 		ocelot_vcap_policer_add(ocelot, filter->action.pol_ix,
944ea9d1f30SVladimir Oltean 					&filter->action.pol);
9453c83654fSVladimir Oltean 	}
9463c83654fSVladimir Oltean 
9473c83654fSVladimir Oltean 	block->count++;
9483c83654fSVladimir Oltean 
9493c83654fSVladimir Oltean 	if (list_empty(&block->rules)) {
950aae4e500SVladimir Oltean 		list_add(&filter->list, &block->rules);
9513c83654fSVladimir Oltean 		return;
9523c83654fSVladimir Oltean 	}
9533c83654fSVladimir Oltean 
9543c83654fSVladimir Oltean 	list_for_each_safe(pos, n, &block->rules) {
955aae4e500SVladimir Oltean 		tmp = list_entry(pos, struct ocelot_vcap_filter, list);
956aae4e500SVladimir Oltean 		if (filter->prio < tmp->prio)
9573c83654fSVladimir Oltean 			break;
9583c83654fSVladimir Oltean 	}
959aae4e500SVladimir Oltean 	list_add(&filter->list, pos->prev);
9603c83654fSVladimir Oltean }
9613c83654fSVladimir Oltean 
962aae4e500SVladimir Oltean static int ocelot_vcap_block_get_filter_index(struct ocelot_vcap_block *block,
963aae4e500SVladimir Oltean 					      struct ocelot_vcap_filter *filter)
9643c83654fSVladimir Oltean {
965aae4e500SVladimir Oltean 	struct ocelot_vcap_filter *tmp;
966ed5672d8SXiaoliang Yang 	int index = 0;
9673c83654fSVladimir Oltean 
9683c83654fSVladimir Oltean 	list_for_each_entry(tmp, &block->rules, list) {
969aae4e500SVladimir Oltean 		if (filter->id == tmp->id)
9703c83654fSVladimir Oltean 			return index;
971ed5672d8SXiaoliang Yang 		index++;
972ed5672d8SXiaoliang Yang 	}
973ed5672d8SXiaoliang Yang 
974ed5672d8SXiaoliang Yang 	return -ENOENT;
9753c83654fSVladimir Oltean }
9763c83654fSVladimir Oltean 
977aae4e500SVladimir Oltean static struct ocelot_vcap_filter*
978085f5b91SVladimir Oltean ocelot_vcap_block_find_filter_by_index(struct ocelot_vcap_block *block,
979aae4e500SVladimir Oltean 				       int index)
9803c83654fSVladimir Oltean {
981aae4e500SVladimir Oltean 	struct ocelot_vcap_filter *tmp;
9823c83654fSVladimir Oltean 	int i = 0;
9833c83654fSVladimir Oltean 
9843c83654fSVladimir Oltean 	list_for_each_entry(tmp, &block->rules, list) {
9853c83654fSVladimir Oltean 		if (i == index)
9863c83654fSVladimir Oltean 			return tmp;
9873c83654fSVladimir Oltean 		++i;
9883c83654fSVladimir Oltean 	}
9893c83654fSVladimir Oltean 
9903c83654fSVladimir Oltean 	return NULL;
9913c83654fSVladimir Oltean }
9923c83654fSVladimir Oltean 
993085f5b91SVladimir Oltean struct ocelot_vcap_filter *
994085f5b91SVladimir Oltean ocelot_vcap_block_find_filter_by_id(struct ocelot_vcap_block *block, int id)
995085f5b91SVladimir Oltean {
996085f5b91SVladimir Oltean 	struct ocelot_vcap_filter *filter;
997085f5b91SVladimir Oltean 
998085f5b91SVladimir Oltean 	list_for_each_entry(filter, &block->rules, list)
999085f5b91SVladimir Oltean 		if (filter->id == id)
1000085f5b91SVladimir Oltean 			return filter;
1001085f5b91SVladimir Oltean 
1002085f5b91SVladimir Oltean 	return NULL;
1003085f5b91SVladimir Oltean }
1004085f5b91SVladimir Oltean 
10053c83654fSVladimir Oltean /* If @on=false, then SNAP, ARP, IP and OAM frames will not match on keys based
10063c83654fSVladimir Oltean  * on destination and source MAC addresses, but only on higher-level protocol
10073c83654fSVladimir Oltean  * information. The only frame types to match on keys containing MAC addresses
10083c83654fSVladimir Oltean  * in this case are non-SNAP, non-ARP, non-IP and non-OAM frames.
10093c83654fSVladimir Oltean  *
10103c83654fSVladimir Oltean  * If @on=true, then the above frame types (SNAP, ARP, IP and OAM) will match
10113c83654fSVladimir Oltean  * on MAC_ETYPE keys such as destination and source MAC on this ingress port.
10123c83654fSVladimir Oltean  * However the setting has the side effect of making these frames not matching
10133c83654fSVladimir Oltean  * on any _other_ keys than MAC_ETYPE ones.
10143c83654fSVladimir Oltean  */
10153c83654fSVladimir Oltean static void ocelot_match_all_as_mac_etype(struct ocelot *ocelot, int port,
10163c83654fSVladimir Oltean 					  bool on)
10173c83654fSVladimir Oltean {
10183c83654fSVladimir Oltean 	u32 val = 0;
10193c83654fSVladimir Oltean 
10203c83654fSVladimir Oltean 	if (on)
10213c83654fSVladimir Oltean 		val = ANA_PORT_VCAP_S2_CFG_S2_SNAP_DIS(3) |
10223c83654fSVladimir Oltean 		      ANA_PORT_VCAP_S2_CFG_S2_ARP_DIS(3) |
10233c83654fSVladimir Oltean 		      ANA_PORT_VCAP_S2_CFG_S2_IP_TCPUDP_DIS(3) |
10243c83654fSVladimir Oltean 		      ANA_PORT_VCAP_S2_CFG_S2_IP_OTHER_DIS(3) |
10253c83654fSVladimir Oltean 		      ANA_PORT_VCAP_S2_CFG_S2_OAM_DIS(3);
10263c83654fSVladimir Oltean 
10273c83654fSVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
10283c83654fSVladimir Oltean 		       ANA_PORT_VCAP_S2_CFG_S2_SNAP_DIS_M |
10293c83654fSVladimir Oltean 		       ANA_PORT_VCAP_S2_CFG_S2_ARP_DIS_M |
10303c83654fSVladimir Oltean 		       ANA_PORT_VCAP_S2_CFG_S2_IP_TCPUDP_DIS_M |
10313c83654fSVladimir Oltean 		       ANA_PORT_VCAP_S2_CFG_S2_IP_OTHER_DIS_M |
10323c83654fSVladimir Oltean 		       ANA_PORT_VCAP_S2_CFG_S2_OAM_DIS_M,
10333c83654fSVladimir Oltean 		       ANA_PORT_VCAP_S2_CFG, port);
10343c83654fSVladimir Oltean }
10353c83654fSVladimir Oltean 
1036aae4e500SVladimir Oltean static bool
1037aae4e500SVladimir Oltean ocelot_vcap_is_problematic_mac_etype(struct ocelot_vcap_filter *filter)
10383c83654fSVladimir Oltean {
10393c83654fSVladimir Oltean 	u16 proto, mask;
10403c83654fSVladimir Oltean 
1041aae4e500SVladimir Oltean 	if (filter->key_type != OCELOT_VCAP_KEY_ETYPE)
10423c83654fSVladimir Oltean 		return false;
10433c83654fSVladimir Oltean 
1044aae4e500SVladimir Oltean 	proto = ntohs(*(__be16 *)filter->key.etype.etype.value);
1045aae4e500SVladimir Oltean 	mask = ntohs(*(__be16 *)filter->key.etype.etype.mask);
10463c83654fSVladimir Oltean 
10473c83654fSVladimir Oltean 	/* ETH_P_ALL match, so all protocols below are included */
10483c83654fSVladimir Oltean 	if (mask == 0)
10493c83654fSVladimir Oltean 		return true;
10503c83654fSVladimir Oltean 	if (proto == ETH_P_ARP)
10513c83654fSVladimir Oltean 		return true;
10523c83654fSVladimir Oltean 	if (proto == ETH_P_IP)
10533c83654fSVladimir Oltean 		return true;
10543c83654fSVladimir Oltean 	if (proto == ETH_P_IPV6)
10553c83654fSVladimir Oltean 		return true;
10563c83654fSVladimir Oltean 
10573c83654fSVladimir Oltean 	return false;
10583c83654fSVladimir Oltean }
10593c83654fSVladimir Oltean 
1060aae4e500SVladimir Oltean static bool
1061aae4e500SVladimir Oltean ocelot_vcap_is_problematic_non_mac_etype(struct ocelot_vcap_filter *filter)
10623c83654fSVladimir Oltean {
1063aae4e500SVladimir Oltean 	if (filter->key_type == OCELOT_VCAP_KEY_SNAP)
10643c83654fSVladimir Oltean 		return true;
1065aae4e500SVladimir Oltean 	if (filter->key_type == OCELOT_VCAP_KEY_ARP)
10663c83654fSVladimir Oltean 		return true;
1067aae4e500SVladimir Oltean 	if (filter->key_type == OCELOT_VCAP_KEY_IPV4)
10683c83654fSVladimir Oltean 		return true;
1069aae4e500SVladimir Oltean 	if (filter->key_type == OCELOT_VCAP_KEY_IPV6)
10703c83654fSVladimir Oltean 		return true;
10713c83654fSVladimir Oltean 	return false;
10723c83654fSVladimir Oltean }
10733c83654fSVladimir Oltean 
1074aae4e500SVladimir Oltean static bool
1075aae4e500SVladimir Oltean ocelot_exclusive_mac_etype_filter_rules(struct ocelot *ocelot,
1076aae4e500SVladimir Oltean 					struct ocelot_vcap_filter *filter)
10773c83654fSVladimir Oltean {
10781397a2ebSVladimir Oltean 	struct ocelot_vcap_block *block = &ocelot->block[filter->block_id];
1079aae4e500SVladimir Oltean 	struct ocelot_vcap_filter *tmp;
10803c83654fSVladimir Oltean 	unsigned long port;
10813c83654fSVladimir Oltean 	int i;
10823c83654fSVladimir Oltean 
1083aae4e500SVladimir Oltean 	if (ocelot_vcap_is_problematic_mac_etype(filter)) {
10843c83654fSVladimir Oltean 		/* Search for any non-MAC_ETYPE rules on the port */
10853c83654fSVladimir Oltean 		for (i = 0; i < block->count; i++) {
1086085f5b91SVladimir Oltean 			tmp = ocelot_vcap_block_find_filter_by_index(block, i);
1087aae4e500SVladimir Oltean 			if (tmp->ingress_port_mask & filter->ingress_port_mask &&
1088aae4e500SVladimir Oltean 			    ocelot_vcap_is_problematic_non_mac_etype(tmp))
10893c83654fSVladimir Oltean 				return false;
10903c83654fSVladimir Oltean 		}
10913c83654fSVladimir Oltean 
1092aae4e500SVladimir Oltean 		for_each_set_bit(port, &filter->ingress_port_mask,
10933c83654fSVladimir Oltean 				 ocelot->num_phys_ports)
10943c83654fSVladimir Oltean 			ocelot_match_all_as_mac_etype(ocelot, port, true);
1095aae4e500SVladimir Oltean 	} else if (ocelot_vcap_is_problematic_non_mac_etype(filter)) {
10963c83654fSVladimir Oltean 		/* Search for any MAC_ETYPE rules on the port */
10973c83654fSVladimir Oltean 		for (i = 0; i < block->count; i++) {
1098085f5b91SVladimir Oltean 			tmp = ocelot_vcap_block_find_filter_by_index(block, i);
1099aae4e500SVladimir Oltean 			if (tmp->ingress_port_mask & filter->ingress_port_mask &&
1100aae4e500SVladimir Oltean 			    ocelot_vcap_is_problematic_mac_etype(tmp))
11013c83654fSVladimir Oltean 				return false;
11023c83654fSVladimir Oltean 		}
11033c83654fSVladimir Oltean 
1104aae4e500SVladimir Oltean 		for_each_set_bit(port, &filter->ingress_port_mask,
11053c83654fSVladimir Oltean 				 ocelot->num_phys_ports)
11063c83654fSVladimir Oltean 			ocelot_match_all_as_mac_etype(ocelot, port, false);
11073c83654fSVladimir Oltean 	}
11083c83654fSVladimir Oltean 
11093c83654fSVladimir Oltean 	return true;
11103c83654fSVladimir Oltean }
11113c83654fSVladimir Oltean 
1112aae4e500SVladimir Oltean int ocelot_vcap_filter_add(struct ocelot *ocelot,
1113aae4e500SVladimir Oltean 			   struct ocelot_vcap_filter *filter,
11143c83654fSVladimir Oltean 			   struct netlink_ext_ack *extack)
11153c83654fSVladimir Oltean {
11161397a2ebSVladimir Oltean 	struct ocelot_vcap_block *block = &ocelot->block[filter->block_id];
11173c83654fSVladimir Oltean 	int i, index;
11183c83654fSVladimir Oltean 
1119aae4e500SVladimir Oltean 	if (!ocelot_exclusive_mac_etype_filter_rules(ocelot, filter)) {
11203c83654fSVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
11213c83654fSVladimir Oltean 				   "Cannot mix MAC_ETYPE with non-MAC_ETYPE rules");
11223c83654fSVladimir Oltean 		return -EBUSY;
11233c83654fSVladimir Oltean 	}
11243c83654fSVladimir Oltean 
1125aae4e500SVladimir Oltean 	/* Add filter to the linked list */
1126aae4e500SVladimir Oltean 	ocelot_vcap_filter_add_to_block(ocelot, block, filter);
11273c83654fSVladimir Oltean 
1128aae4e500SVladimir Oltean 	/* Get the index of the inserted filter */
1129aae4e500SVladimir Oltean 	index = ocelot_vcap_block_get_filter_index(block, filter);
1130ed5672d8SXiaoliang Yang 	if (index < 0)
1131ed5672d8SXiaoliang Yang 		return index;
11323c83654fSVladimir Oltean 
1133aae4e500SVladimir Oltean 	/* Move down the rules to make place for the new filter */
11343c83654fSVladimir Oltean 	for (i = block->count - 1; i > index; i--) {
1135aae4e500SVladimir Oltean 		struct ocelot_vcap_filter *tmp;
1136aae4e500SVladimir Oltean 
1137085f5b91SVladimir Oltean 		tmp = ocelot_vcap_block_find_filter_by_index(block, i);
11381397a2ebSVladimir Oltean 		vcap_entry_set(ocelot, i, tmp);
11393c83654fSVladimir Oltean 	}
11403c83654fSVladimir Oltean 
1141aae4e500SVladimir Oltean 	/* Now insert the new filter */
11421397a2ebSVladimir Oltean 	vcap_entry_set(ocelot, index, filter);
11433c83654fSVladimir Oltean 	return 0;
11443c83654fSVladimir Oltean }
11453c83654fSVladimir Oltean 
1146aae4e500SVladimir Oltean static void ocelot_vcap_block_remove_filter(struct ocelot *ocelot,
1147aae4e500SVladimir Oltean 					    struct ocelot_vcap_block *block,
1148aae4e500SVladimir Oltean 					    struct ocelot_vcap_filter *filter)
11493c83654fSVladimir Oltean {
1150aae4e500SVladimir Oltean 	struct ocelot_vcap_filter *tmp;
11513c83654fSVladimir Oltean 	struct list_head *pos, *q;
11523c83654fSVladimir Oltean 
11533c83654fSVladimir Oltean 	list_for_each_safe(pos, q, &block->rules) {
1154aae4e500SVladimir Oltean 		tmp = list_entry(pos, struct ocelot_vcap_filter, list);
1155aae4e500SVladimir Oltean 		if (tmp->id == filter->id) {
11561397a2ebSVladimir Oltean 			if (tmp->block_id == VCAP_IS2 &&
11571397a2ebSVladimir Oltean 			    tmp->action.police_ena)
1158c73b0ad3SVladimir Oltean 				ocelot_vcap_policer_del(ocelot, block,
1159ea9d1f30SVladimir Oltean 							tmp->action.pol_ix);
11603c83654fSVladimir Oltean 
11613c83654fSVladimir Oltean 			list_del(pos);
11623c83654fSVladimir Oltean 			kfree(tmp);
11633c83654fSVladimir Oltean 		}
11643c83654fSVladimir Oltean 	}
11653c83654fSVladimir Oltean 
11663c83654fSVladimir Oltean 	block->count--;
11673c83654fSVladimir Oltean }
11683c83654fSVladimir Oltean 
1169aae4e500SVladimir Oltean int ocelot_vcap_filter_del(struct ocelot *ocelot,
1170aae4e500SVladimir Oltean 			   struct ocelot_vcap_filter *filter)
11713c83654fSVladimir Oltean {
11721397a2ebSVladimir Oltean 	struct ocelot_vcap_block *block = &ocelot->block[filter->block_id];
1173aae4e500SVladimir Oltean 	struct ocelot_vcap_filter del_filter;
11743c83654fSVladimir Oltean 	int i, index;
11753c83654fSVladimir Oltean 
1176aae4e500SVladimir Oltean 	memset(&del_filter, 0, sizeof(del_filter));
11773c83654fSVladimir Oltean 
1178aae4e500SVladimir Oltean 	/* Gets index of the filter */
1179aae4e500SVladimir Oltean 	index = ocelot_vcap_block_get_filter_index(block, filter);
1180ed5672d8SXiaoliang Yang 	if (index < 0)
1181ed5672d8SXiaoliang Yang 		return index;
11823c83654fSVladimir Oltean 
1183aae4e500SVladimir Oltean 	/* Delete filter */
1184aae4e500SVladimir Oltean 	ocelot_vcap_block_remove_filter(ocelot, block, filter);
11853c83654fSVladimir Oltean 
1186aae4e500SVladimir Oltean 	/* Move up all the blocks over the deleted filter */
11873c83654fSVladimir Oltean 	for (i = index; i < block->count; i++) {
1188aae4e500SVladimir Oltean 		struct ocelot_vcap_filter *tmp;
1189aae4e500SVladimir Oltean 
1190085f5b91SVladimir Oltean 		tmp = ocelot_vcap_block_find_filter_by_index(block, i);
11911397a2ebSVladimir Oltean 		vcap_entry_set(ocelot, i, tmp);
11923c83654fSVladimir Oltean 	}
11933c83654fSVladimir Oltean 
1194aae4e500SVladimir Oltean 	/* Now delete the last filter, because it is duplicated */
11951397a2ebSVladimir Oltean 	vcap_entry_set(ocelot, block->count, &del_filter);
11963c83654fSVladimir Oltean 
11973c83654fSVladimir Oltean 	return 0;
11983c83654fSVladimir Oltean }
11993c83654fSVladimir Oltean 
1200aae4e500SVladimir Oltean int ocelot_vcap_filter_stats_update(struct ocelot *ocelot,
1201aae4e500SVladimir Oltean 				    struct ocelot_vcap_filter *filter)
12023c83654fSVladimir Oltean {
12031397a2ebSVladimir Oltean 	struct ocelot_vcap_block *block = &ocelot->block[filter->block_id];
120498642d1aSVladimir Oltean 	struct ocelot_vcap_filter tmp;
12053c83654fSVladimir Oltean 	int index;
12063c83654fSVladimir Oltean 
1207aae4e500SVladimir Oltean 	index = ocelot_vcap_block_get_filter_index(block, filter);
1208ed5672d8SXiaoliang Yang 	if (index < 0)
1209ed5672d8SXiaoliang Yang 		return index;
1210ed5672d8SXiaoliang Yang 
12111397a2ebSVladimir Oltean 	vcap_entry_get(ocelot, index, filter);
12123c83654fSVladimir Oltean 
12133c83654fSVladimir Oltean 	/* After we get the result we need to clear the counters */
121498642d1aSVladimir Oltean 	tmp = *filter;
121598642d1aSVladimir Oltean 	tmp.stats.pkts = 0;
12161397a2ebSVladimir Oltean 	vcap_entry_set(ocelot, index, &tmp);
12173c83654fSVladimir Oltean 
12183c83654fSVladimir Oltean 	return 0;
12193c83654fSVladimir Oltean }
12203c83654fSVladimir Oltean 
1221c1c3993eSVladimir Oltean static void ocelot_vcap_init_one(struct ocelot *ocelot,
1222c1c3993eSVladimir Oltean 				 const struct vcap_props *vcap)
12233c83654fSVladimir Oltean {
12243c83654fSVladimir Oltean 	struct vcap_data data;
12253c83654fSVladimir Oltean 
12263c83654fSVladimir Oltean 	memset(&data, 0, sizeof(data));
12273c83654fSVladimir Oltean 
1228c1c3993eSVladimir Oltean 	vcap_entry2cache(ocelot, vcap, &data);
1229c1c3993eSVladimir Oltean 	ocelot_target_write(ocelot, vcap->target, vcap->entry_count,
1230c1c3993eSVladimir Oltean 			    VCAP_CORE_MV_CFG);
1231c1c3993eSVladimir Oltean 	vcap_cmd(ocelot, vcap, 0, VCAP_CMD_INITIALIZE, VCAP_SEL_ENTRY);
12323c83654fSVladimir Oltean 
1233c1c3993eSVladimir Oltean 	vcap_action2cache(ocelot, vcap, &data);
1234c1c3993eSVladimir Oltean 	ocelot_target_write(ocelot, vcap->target, vcap->action_count,
1235c1c3993eSVladimir Oltean 			    VCAP_CORE_MV_CFG);
1236c1c3993eSVladimir Oltean 	vcap_cmd(ocelot, vcap, 0, VCAP_CMD_INITIALIZE,
12373c83654fSVladimir Oltean 		 VCAP_SEL_ACTION | VCAP_SEL_COUNTER);
1238c1c3993eSVladimir Oltean }
1239c1c3993eSVladimir Oltean 
124020968054SVladimir Oltean static void ocelot_vcap_detect_constants(struct ocelot *ocelot,
124120968054SVladimir Oltean 					 struct vcap_props *vcap)
124220968054SVladimir Oltean {
124320968054SVladimir Oltean 	int counter_memory_width;
124420968054SVladimir Oltean 	int num_default_actions;
124520968054SVladimir Oltean 	int version;
124620968054SVladimir Oltean 
124720968054SVladimir Oltean 	version = ocelot_target_read(ocelot, vcap->target,
124820968054SVladimir Oltean 				     VCAP_CONST_VCAP_VER);
124920968054SVladimir Oltean 	/* Only version 0 VCAP supported for now */
125020968054SVladimir Oltean 	if (WARN_ON(version != 0))
125120968054SVladimir Oltean 		return;
125220968054SVladimir Oltean 
125320968054SVladimir Oltean 	/* Width in bits of type-group field */
125420968054SVladimir Oltean 	vcap->tg_width = ocelot_target_read(ocelot, vcap->target,
125520968054SVladimir Oltean 					    VCAP_CONST_ENTRY_TG_WIDTH);
125620968054SVladimir Oltean 	/* Number of subwords per TCAM row */
125720968054SVladimir Oltean 	vcap->sw_count = ocelot_target_read(ocelot, vcap->target,
125820968054SVladimir Oltean 					    VCAP_CONST_ENTRY_SWCNT);
125920968054SVladimir Oltean 	/* Number of rows in TCAM. There can be this many full keys, or double
126020968054SVladimir Oltean 	 * this number half keys, or 4 times this number quarter keys.
126120968054SVladimir Oltean 	 */
126220968054SVladimir Oltean 	vcap->entry_count = ocelot_target_read(ocelot, vcap->target,
126320968054SVladimir Oltean 					       VCAP_CONST_ENTRY_CNT);
126420968054SVladimir Oltean 	/* Assuming there are 4 subwords per TCAM row, their layout in the
126520968054SVladimir Oltean 	 * actual TCAM (not in the cache) would be:
126620968054SVladimir Oltean 	 *
126720968054SVladimir Oltean 	 * |  SW 3  | TG 3 |  SW 2  | TG 2 |  SW 1  | TG 1 |  SW 0  | TG 0 |
126820968054SVladimir Oltean 	 *
126920968054SVladimir Oltean 	 * (where SW=subword and TG=Type-Group).
127020968054SVladimir Oltean 	 *
127120968054SVladimir Oltean 	 * What VCAP_CONST_ENTRY_CNT is giving us is the width of one full TCAM
127220968054SVladimir Oltean 	 * row. But when software accesses the TCAM through the cache
127320968054SVladimir Oltean 	 * registers, the Type-Group values are written through another set of
127420968054SVladimir Oltean 	 * registers VCAP_TG_DAT, and therefore, it appears as though the 4
127520968054SVladimir Oltean 	 * subwords are contiguous in the cache memory.
127620968054SVladimir Oltean 	 * Important mention: regardless of the number of key entries per row
127720968054SVladimir Oltean 	 * (and therefore of key size: 1 full key or 2 half keys or 4 quarter
127820968054SVladimir Oltean 	 * keys), software always has to configure 4 Type-Group values. For
127920968054SVladimir Oltean 	 * example, in the case of 1 full key, the driver needs to set all 4
128020968054SVladimir Oltean 	 * Type-Group to be full key.
128120968054SVladimir Oltean 	 *
128220968054SVladimir Oltean 	 * For this reason, we need to fix up the value that the hardware is
128320968054SVladimir Oltean 	 * giving us. We don't actually care about the width of the entry in
128420968054SVladimir Oltean 	 * the TCAM. What we care about is the width of the entry in the cache
128520968054SVladimir Oltean 	 * registers, which is how we get to interact with it. And since the
128620968054SVladimir Oltean 	 * VCAP_ENTRY_DAT cache registers access only the subwords and not the
128720968054SVladimir Oltean 	 * Type-Groups, this means we need to subtract the width of the
128820968054SVladimir Oltean 	 * Type-Groups when packing and unpacking key entry data in a TCAM row.
128920968054SVladimir Oltean 	 */
129020968054SVladimir Oltean 	vcap->entry_width = ocelot_target_read(ocelot, vcap->target,
129120968054SVladimir Oltean 					       VCAP_CONST_ENTRY_WIDTH);
129220968054SVladimir Oltean 	vcap->entry_width -= vcap->tg_width * vcap->sw_count;
129320968054SVladimir Oltean 	num_default_actions = ocelot_target_read(ocelot, vcap->target,
129420968054SVladimir Oltean 						 VCAP_CONST_ACTION_DEF_CNT);
129520968054SVladimir Oltean 	vcap->action_count = vcap->entry_count + num_default_actions;
129620968054SVladimir Oltean 	vcap->action_width = ocelot_target_read(ocelot, vcap->target,
129720968054SVladimir Oltean 						VCAP_CONST_ACTION_WIDTH);
129820968054SVladimir Oltean 	/* The width of the counter memory, this is the complete width of all
129920968054SVladimir Oltean 	 * counter-fields associated with one full-word entry. There is one
130020968054SVladimir Oltean 	 * counter per entry sub-word (see CAP_CORE::ENTRY_SWCNT for number of
130120968054SVladimir Oltean 	 * subwords.)
130220968054SVladimir Oltean 	 */
130320968054SVladimir Oltean 	vcap->counter_words = vcap->sw_count;
130420968054SVladimir Oltean 	counter_memory_width = ocelot_target_read(ocelot, vcap->target,
130520968054SVladimir Oltean 						  VCAP_CONST_CNT_WIDTH);
130620968054SVladimir Oltean 	vcap->counter_width = counter_memory_width / vcap->counter_words;
130720968054SVladimir Oltean }
130820968054SVladimir Oltean 
1309c1c3993eSVladimir Oltean int ocelot_vcap_init(struct ocelot *ocelot)
1310c1c3993eSVladimir Oltean {
131120968054SVladimir Oltean 	int i;
13123c83654fSVladimir Oltean 
13133c83654fSVladimir Oltean 	/* Create a policer that will drop the frames for the cpu.
13143c83654fSVladimir Oltean 	 * This policer will be used as action in the acl rules to drop
13153c83654fSVladimir Oltean 	 * frames.
13163c83654fSVladimir Oltean 	 */
13173c83654fSVladimir Oltean 	ocelot_write_gix(ocelot, 0x299, ANA_POL_MODE_CFG,
13183c83654fSVladimir Oltean 			 OCELOT_POLICER_DISCARD);
13193c83654fSVladimir Oltean 	ocelot_write_gix(ocelot, 0x1, ANA_POL_PIR_CFG,
13203c83654fSVladimir Oltean 			 OCELOT_POLICER_DISCARD);
13213c83654fSVladimir Oltean 	ocelot_write_gix(ocelot, 0x3fffff, ANA_POL_PIR_STATE,
13223c83654fSVladimir Oltean 			 OCELOT_POLICER_DISCARD);
13233c83654fSVladimir Oltean 	ocelot_write_gix(ocelot, 0x0, ANA_POL_CIR_CFG,
13243c83654fSVladimir Oltean 			 OCELOT_POLICER_DISCARD);
13253c83654fSVladimir Oltean 	ocelot_write_gix(ocelot, 0x3fffff, ANA_POL_CIR_STATE,
13263c83654fSVladimir Oltean 			 OCELOT_POLICER_DISCARD);
13273c83654fSVladimir Oltean 
132820968054SVladimir Oltean 	for (i = 0; i < OCELOT_NUM_VCAP_BLOCKS; i++) {
13291397a2ebSVladimir Oltean 		struct ocelot_vcap_block *block = &ocelot->block[i];
133020968054SVladimir Oltean 		struct vcap_props *vcap = &ocelot->vcap[i];
133120968054SVladimir Oltean 
13321397a2ebSVladimir Oltean 		INIT_LIST_HEAD(&block->rules);
13331397a2ebSVladimir Oltean 		block->pol_lpr = OCELOT_POLICER_DISCARD - 1;
13341397a2ebSVladimir Oltean 
133520968054SVladimir Oltean 		ocelot_vcap_detect_constants(ocelot, vcap);
133620968054SVladimir Oltean 		ocelot_vcap_init_one(ocelot, vcap);
133720968054SVladimir Oltean 	}
133820968054SVladimir Oltean 
13391397a2ebSVladimir Oltean 	INIT_LIST_HEAD(&ocelot->dummy_rules);
13403c83654fSVladimir Oltean 
13413c83654fSVladimir Oltean 	return 0;
13423c83654fSVladimir Oltean }
1343