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 
370c1c3993eSVladimir Oltean 	vcap_key_set(vcap, &data, VCAP_IS2_HK_PAG, 0, 0);
371c1c3993eSVladimir Oltean 	vcap_key_set(vcap, &data, VCAP_IS2_HK_IGR_PORT_MASK, 0,
372aae4e500SVladimir Oltean 		     ~filter->ingress_port_mask);
373c1c3993eSVladimir Oltean 	vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_FIRST, OCELOT_VCAP_BIT_ANY);
374c1c3993eSVladimir Oltean 	vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_HOST_MATCH,
3753c83654fSVladimir Oltean 			 OCELOT_VCAP_BIT_ANY);
376c1c3993eSVladimir Oltean 	vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L2_MC, filter->dmac_mc);
377c1c3993eSVladimir Oltean 	vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L2_BC, filter->dmac_bc);
378c1c3993eSVladimir Oltean 	vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_VLAN_TAGGED, tag->tagged);
379c1c3993eSVladimir Oltean 	vcap_key_set(vcap, &data, VCAP_IS2_HK_VID,
3803c83654fSVladimir Oltean 		     tag->vid.value, tag->vid.mask);
381c1c3993eSVladimir Oltean 	vcap_key_set(vcap, &data, VCAP_IS2_HK_PCP,
3823c83654fSVladimir Oltean 		     tag->pcp.value[0], tag->pcp.mask[0]);
383c1c3993eSVladimir Oltean 	vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_DEI, tag->dei);
3843c83654fSVladimir Oltean 
385aae4e500SVladimir Oltean 	switch (filter->key_type) {
386aae4e500SVladimir Oltean 	case OCELOT_VCAP_KEY_ETYPE: {
387aae4e500SVladimir Oltean 		struct ocelot_vcap_key_etype *etype = &filter->key.etype;
3883c83654fSVladimir Oltean 
3893c83654fSVladimir Oltean 		type = IS2_TYPE_ETYPE;
390c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_DMAC,
3913c83654fSVladimir Oltean 				   etype->dmac.value, etype->dmac.mask);
392c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_SMAC,
3933c83654fSVladimir Oltean 				   etype->smac.value, etype->smac.mask);
394c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_MAC_ETYPE_ETYPE,
3953c83654fSVladimir Oltean 				   etype->etype.value, etype->etype.mask);
3963c83654fSVladimir Oltean 		/* Clear unused bits */
397c1c3993eSVladimir Oltean 		vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD0,
3983c83654fSVladimir Oltean 			     0, 0);
399c1c3993eSVladimir Oltean 		vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD1,
4003c83654fSVladimir Oltean 			     0, 0);
401c1c3993eSVladimir Oltean 		vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD2,
4023c83654fSVladimir Oltean 			     0, 0);
403c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data,
4043c83654fSVladimir Oltean 				   VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD0,
4053c83654fSVladimir Oltean 				   etype->data.value, etype->data.mask);
4063c83654fSVladimir Oltean 		break;
4073c83654fSVladimir Oltean 	}
408aae4e500SVladimir Oltean 	case OCELOT_VCAP_KEY_LLC: {
409aae4e500SVladimir Oltean 		struct ocelot_vcap_key_llc *llc = &filter->key.llc;
4103c83654fSVladimir Oltean 
4113c83654fSVladimir Oltean 		type = IS2_TYPE_LLC;
412c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_DMAC,
4133c83654fSVladimir Oltean 				   llc->dmac.value, llc->dmac.mask);
414c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_SMAC,
4153c83654fSVladimir Oltean 				   llc->smac.value, llc->smac.mask);
4163c83654fSVladimir Oltean 		for (i = 0; i < 4; i++) {
4173c83654fSVladimir Oltean 			payload.value[i] = llc->llc.value[i];
4183c83654fSVladimir Oltean 			payload.mask[i] = llc->llc.mask[i];
4193c83654fSVladimir Oltean 		}
420c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_MAC_LLC_L2_LLC,
4213c83654fSVladimir Oltean 				   payload.value, payload.mask);
4223c83654fSVladimir Oltean 		break;
4233c83654fSVladimir Oltean 	}
424aae4e500SVladimir Oltean 	case OCELOT_VCAP_KEY_SNAP: {
425aae4e500SVladimir Oltean 		struct ocelot_vcap_key_snap *snap = &filter->key.snap;
4263c83654fSVladimir Oltean 
4273c83654fSVladimir Oltean 		type = IS2_TYPE_SNAP;
428c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_DMAC,
4293c83654fSVladimir Oltean 				   snap->dmac.value, snap->dmac.mask);
430c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_SMAC,
4313c83654fSVladimir Oltean 				   snap->smac.value, snap->smac.mask);
432c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_MAC_SNAP_L2_SNAP,
433aae4e500SVladimir Oltean 				   filter->key.snap.snap.value,
434aae4e500SVladimir Oltean 				   filter->key.snap.snap.mask);
4353c83654fSVladimir Oltean 		break;
4363c83654fSVladimir Oltean 	}
437aae4e500SVladimir Oltean 	case OCELOT_VCAP_KEY_ARP: {
438aae4e500SVladimir Oltean 		struct ocelot_vcap_key_arp *arp = &filter->key.arp;
4393c83654fSVladimir Oltean 
4403c83654fSVladimir Oltean 		type = IS2_TYPE_ARP;
441c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_MAC_ARP_SMAC,
4423c83654fSVladimir Oltean 				   arp->smac.value, arp->smac.mask);
443c1c3993eSVladimir Oltean 		vcap_key_bit_set(vcap, &data,
4443c83654fSVladimir Oltean 				 VCAP_IS2_HK_MAC_ARP_ADDR_SPACE_OK,
4453c83654fSVladimir Oltean 				 arp->ethernet);
446c1c3993eSVladimir Oltean 		vcap_key_bit_set(vcap, &data,
4473c83654fSVladimir Oltean 				 VCAP_IS2_HK_MAC_ARP_PROTO_SPACE_OK,
4483c83654fSVladimir Oltean 				 arp->ip);
449c1c3993eSVladimir Oltean 		vcap_key_bit_set(vcap, &data,
4503c83654fSVladimir Oltean 				 VCAP_IS2_HK_MAC_ARP_LEN_OK,
4513c83654fSVladimir Oltean 				 arp->length);
452c1c3993eSVladimir Oltean 		vcap_key_bit_set(vcap, &data,
4533c83654fSVladimir Oltean 				 VCAP_IS2_HK_MAC_ARP_TARGET_MATCH,
4543c83654fSVladimir Oltean 				 arp->dmac_match);
455c1c3993eSVladimir Oltean 		vcap_key_bit_set(vcap, &data,
4563c83654fSVladimir Oltean 				 VCAP_IS2_HK_MAC_ARP_SENDER_MATCH,
4573c83654fSVladimir Oltean 				 arp->smac_match);
458c1c3993eSVladimir Oltean 		vcap_key_bit_set(vcap, &data,
4593c83654fSVladimir Oltean 				 VCAP_IS2_HK_MAC_ARP_OPCODE_UNKNOWN,
4603c83654fSVladimir Oltean 				 arp->unknown);
4613c83654fSVladimir Oltean 
4623c83654fSVladimir Oltean 		/* OPCODE is inverse, bit 0 is reply flag, bit 1 is RARP flag */
4633c83654fSVladimir Oltean 		val = ((arp->req == OCELOT_VCAP_BIT_0 ? 1 : 0) |
4643c83654fSVladimir Oltean 		       (arp->arp == OCELOT_VCAP_BIT_0 ? 2 : 0));
4653c83654fSVladimir Oltean 		msk = ((arp->req == OCELOT_VCAP_BIT_ANY ? 0 : 1) |
4663c83654fSVladimir Oltean 		       (arp->arp == OCELOT_VCAP_BIT_ANY ? 0 : 2));
467c1c3993eSVladimir Oltean 		vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ARP_OPCODE,
4683c83654fSVladimir Oltean 			     val, msk);
469c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data,
4703c83654fSVladimir Oltean 				   VCAP_IS2_HK_MAC_ARP_L3_IP4_DIP,
4713c83654fSVladimir Oltean 				   arp->dip.value.addr, arp->dip.mask.addr);
472c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data,
4733c83654fSVladimir Oltean 				   VCAP_IS2_HK_MAC_ARP_L3_IP4_SIP,
4743c83654fSVladimir Oltean 				   arp->sip.value.addr, arp->sip.mask.addr);
475c1c3993eSVladimir Oltean 		vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ARP_DIP_EQ_SIP,
4763c83654fSVladimir Oltean 			     0, 0);
4773c83654fSVladimir Oltean 		break;
4783c83654fSVladimir Oltean 	}
479aae4e500SVladimir Oltean 	case OCELOT_VCAP_KEY_IPV4:
480aae4e500SVladimir Oltean 	case OCELOT_VCAP_KEY_IPV6: {
4813c83654fSVladimir Oltean 		enum ocelot_vcap_bit sip_eq_dip, sport_eq_dport, seq_zero, tcp;
4823c83654fSVladimir Oltean 		enum ocelot_vcap_bit ttl, fragment, options, tcp_ack, tcp_urg;
4833c83654fSVladimir Oltean 		enum ocelot_vcap_bit tcp_fin, tcp_syn, tcp_rst, tcp_psh;
484aae4e500SVladimir Oltean 		struct ocelot_vcap_key_ipv4 *ipv4 = NULL;
485aae4e500SVladimir Oltean 		struct ocelot_vcap_key_ipv6 *ipv6 = NULL;
4863c83654fSVladimir Oltean 		struct ocelot_vcap_udp_tcp *sport, *dport;
4873c83654fSVladimir Oltean 		struct ocelot_vcap_ipv4 sip, dip;
4883c83654fSVladimir Oltean 		struct ocelot_vcap_u8 proto, ds;
4893c83654fSVladimir Oltean 		struct ocelot_vcap_u48 *ip_data;
4903c83654fSVladimir Oltean 
491aae4e500SVladimir Oltean 		if (filter->key_type == OCELOT_VCAP_KEY_IPV4) {
492aae4e500SVladimir Oltean 			ipv4 = &filter->key.ipv4;
4933c83654fSVladimir Oltean 			ttl = ipv4->ttl;
4943c83654fSVladimir Oltean 			fragment = ipv4->fragment;
4953c83654fSVladimir Oltean 			options = ipv4->options;
4963c83654fSVladimir Oltean 			proto = ipv4->proto;
4973c83654fSVladimir Oltean 			ds = ipv4->ds;
4983c83654fSVladimir Oltean 			ip_data = &ipv4->data;
4993c83654fSVladimir Oltean 			sip = ipv4->sip;
5003c83654fSVladimir Oltean 			dip = ipv4->dip;
5013c83654fSVladimir Oltean 			sport = &ipv4->sport;
5023c83654fSVladimir Oltean 			dport = &ipv4->dport;
5033c83654fSVladimir Oltean 			tcp_fin = ipv4->tcp_fin;
5043c83654fSVladimir Oltean 			tcp_syn = ipv4->tcp_syn;
5053c83654fSVladimir Oltean 			tcp_rst = ipv4->tcp_rst;
5063c83654fSVladimir Oltean 			tcp_psh = ipv4->tcp_psh;
5073c83654fSVladimir Oltean 			tcp_ack = ipv4->tcp_ack;
5083c83654fSVladimir Oltean 			tcp_urg = ipv4->tcp_urg;
5093c83654fSVladimir Oltean 			sip_eq_dip = ipv4->sip_eq_dip;
5103c83654fSVladimir Oltean 			sport_eq_dport = ipv4->sport_eq_dport;
5113c83654fSVladimir Oltean 			seq_zero = ipv4->seq_zero;
5123c83654fSVladimir Oltean 		} else {
513aae4e500SVladimir Oltean 			ipv6 = &filter->key.ipv6;
5143c83654fSVladimir Oltean 			ttl = ipv6->ttl;
5153c83654fSVladimir Oltean 			fragment = OCELOT_VCAP_BIT_ANY;
5163c83654fSVladimir Oltean 			options = OCELOT_VCAP_BIT_ANY;
5173c83654fSVladimir Oltean 			proto = ipv6->proto;
5183c83654fSVladimir Oltean 			ds = ipv6->ds;
5193c83654fSVladimir Oltean 			ip_data = &ipv6->data;
5203c83654fSVladimir Oltean 			for (i = 0; i < 8; i++) {
5213c83654fSVladimir Oltean 				val = ipv6->sip.value[i + 8];
5223c83654fSVladimir Oltean 				msk = ipv6->sip.mask[i + 8];
5233c83654fSVladimir Oltean 				if (i < 4) {
5243c83654fSVladimir Oltean 					dip.value.addr[i] = val;
5253c83654fSVladimir Oltean 					dip.mask.addr[i] = msk;
5263c83654fSVladimir Oltean 				} else {
5273c83654fSVladimir Oltean 					sip.value.addr[i - 4] = val;
5283c83654fSVladimir Oltean 					sip.mask.addr[i - 4] = msk;
5293c83654fSVladimir Oltean 				}
5303c83654fSVladimir Oltean 			}
5313c83654fSVladimir Oltean 			sport = &ipv6->sport;
5323c83654fSVladimir Oltean 			dport = &ipv6->dport;
5333c83654fSVladimir Oltean 			tcp_fin = ipv6->tcp_fin;
5343c83654fSVladimir Oltean 			tcp_syn = ipv6->tcp_syn;
5353c83654fSVladimir Oltean 			tcp_rst = ipv6->tcp_rst;
5363c83654fSVladimir Oltean 			tcp_psh = ipv6->tcp_psh;
5373c83654fSVladimir Oltean 			tcp_ack = ipv6->tcp_ack;
5383c83654fSVladimir Oltean 			tcp_urg = ipv6->tcp_urg;
5393c83654fSVladimir Oltean 			sip_eq_dip = ipv6->sip_eq_dip;
5403c83654fSVladimir Oltean 			sport_eq_dport = ipv6->sport_eq_dport;
5413c83654fSVladimir Oltean 			seq_zero = ipv6->seq_zero;
5423c83654fSVladimir Oltean 		}
5433c83654fSVladimir Oltean 
544c1c3993eSVladimir Oltean 		vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_IP4,
5453c83654fSVladimir Oltean 				 ipv4 ? OCELOT_VCAP_BIT_1 : OCELOT_VCAP_BIT_0);
546c1c3993eSVladimir Oltean 		vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L3_FRAGMENT,
5473c83654fSVladimir Oltean 				 fragment);
548c1c3993eSVladimir Oltean 		vcap_key_set(vcap, &data, VCAP_IS2_HK_L3_FRAG_OFS_GT0, 0, 0);
549c1c3993eSVladimir Oltean 		vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L3_OPTIONS,
5503c83654fSVladimir Oltean 				 options);
551c1c3993eSVladimir Oltean 		vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_IP4_L3_TTL_GT0,
5523c83654fSVladimir Oltean 				 ttl);
553c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L3_TOS,
5543c83654fSVladimir Oltean 				   ds.value, ds.mask);
555c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L3_IP4_DIP,
5563c83654fSVladimir Oltean 				   dip.value.addr, dip.mask.addr);
557c1c3993eSVladimir Oltean 		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L3_IP4_SIP,
5583c83654fSVladimir Oltean 				   sip.value.addr, sip.mask.addr);
559c1c3993eSVladimir Oltean 		vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_DIP_EQ_SIP,
5603c83654fSVladimir Oltean 				 sip_eq_dip);
5613c83654fSVladimir Oltean 		val = proto.value[0];
5623c83654fSVladimir Oltean 		msk = proto.mask[0];
5633c83654fSVladimir Oltean 		type = IS2_TYPE_IP_UDP_TCP;
5643c83654fSVladimir Oltean 		if (msk == 0xff && (val == 6 || val == 17)) {
5653c83654fSVladimir Oltean 			/* UDP/TCP protocol match */
5663c83654fSVladimir Oltean 			tcp = (val == 6 ?
5673c83654fSVladimir Oltean 			       OCELOT_VCAP_BIT_1 : OCELOT_VCAP_BIT_0);
568c1c3993eSVladimir Oltean 			vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_TCP, tcp);
569c1c3993eSVladimir Oltean 			vcap_key_l4_port_set(vcap, &data,
5703c83654fSVladimir Oltean 					     VCAP_IS2_HK_L4_DPORT, dport);
571c1c3993eSVladimir Oltean 			vcap_key_l4_port_set(vcap, &data,
5723c83654fSVladimir Oltean 					     VCAP_IS2_HK_L4_SPORT, sport);
573c1c3993eSVladimir Oltean 			vcap_key_set(vcap, &data, VCAP_IS2_HK_L4_RNG, 0, 0);
574c1c3993eSVladimir Oltean 			vcap_key_bit_set(vcap, &data,
5753c83654fSVladimir Oltean 					 VCAP_IS2_HK_L4_SPORT_EQ_DPORT,
5763c83654fSVladimir Oltean 					 sport_eq_dport);
577c1c3993eSVladimir Oltean 			vcap_key_bit_set(vcap, &data,
5783c83654fSVladimir Oltean 					 VCAP_IS2_HK_L4_SEQUENCE_EQ0,
5793c83654fSVladimir Oltean 					 seq_zero);
580c1c3993eSVladimir Oltean 			vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_FIN,
5813c83654fSVladimir Oltean 					 tcp_fin);
582c1c3993eSVladimir Oltean 			vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_SYN,
5833c83654fSVladimir Oltean 					 tcp_syn);
584c1c3993eSVladimir Oltean 			vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_RST,
5853c83654fSVladimir Oltean 					 tcp_rst);
586c1c3993eSVladimir Oltean 			vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_PSH,
5873c83654fSVladimir Oltean 					 tcp_psh);
588c1c3993eSVladimir Oltean 			vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_ACK,
5893c83654fSVladimir Oltean 					 tcp_ack);
590c1c3993eSVladimir Oltean 			vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_URG,
5913c83654fSVladimir Oltean 					 tcp_urg);
592c1c3993eSVladimir Oltean 			vcap_key_set(vcap, &data, VCAP_IS2_HK_L4_1588_DOM,
5933c83654fSVladimir Oltean 				     0, 0);
594c1c3993eSVladimir Oltean 			vcap_key_set(vcap, &data, VCAP_IS2_HK_L4_1588_VER,
5953c83654fSVladimir Oltean 				     0, 0);
5963c83654fSVladimir Oltean 		} else {
5973c83654fSVladimir Oltean 			if (msk == 0) {
5983c83654fSVladimir Oltean 				/* Any IP protocol match */
5993c83654fSVladimir Oltean 				type_mask = IS2_TYPE_MASK_IP_ANY;
6003c83654fSVladimir Oltean 			} else {
6013c83654fSVladimir Oltean 				/* Non-UDP/TCP protocol match */
6023c83654fSVladimir Oltean 				type = IS2_TYPE_IP_OTHER;
6033c83654fSVladimir Oltean 				for (i = 0; i < 6; i++) {
6043c83654fSVladimir Oltean 					payload.value[i] = ip_data->value[i];
6053c83654fSVladimir Oltean 					payload.mask[i] = ip_data->mask[i];
6063c83654fSVladimir Oltean 				}
6073c83654fSVladimir Oltean 			}
608c1c3993eSVladimir Oltean 			vcap_key_bytes_set(vcap, &data,
6093c83654fSVladimir Oltean 					   VCAP_IS2_HK_IP4_L3_PROTO,
6103c83654fSVladimir Oltean 					   proto.value, proto.mask);
611c1c3993eSVladimir Oltean 			vcap_key_bytes_set(vcap, &data,
6123c83654fSVladimir Oltean 					   VCAP_IS2_HK_L3_PAYLOAD,
6133c83654fSVladimir Oltean 					   payload.value, payload.mask);
6143c83654fSVladimir Oltean 		}
6153c83654fSVladimir Oltean 		break;
6163c83654fSVladimir Oltean 	}
617aae4e500SVladimir Oltean 	case OCELOT_VCAP_KEY_ANY:
6183c83654fSVladimir Oltean 	default:
6193c83654fSVladimir Oltean 		type = 0;
6203c83654fSVladimir Oltean 		type_mask = 0;
621c1c3993eSVladimir Oltean 		count = vcap->entry_width / 2;
6223c83654fSVladimir Oltean 		/* Iterate over the non-common part of the key and
6233c83654fSVladimir Oltean 		 * clear entry data
6243c83654fSVladimir Oltean 		 */
625c1c3993eSVladimir Oltean 		for (i = vcap->keys[VCAP_IS2_HK_L2_DMAC].offset;
6263c83654fSVladimir Oltean 		     i < count; i += ENTRY_WIDTH) {
6273c83654fSVladimir Oltean 			vcap_key_field_set(&data, i, min(32u, count - i), 0, 0);
6283c83654fSVladimir Oltean 		}
6293c83654fSVladimir Oltean 		break;
6303c83654fSVladimir Oltean 	}
6313c83654fSVladimir Oltean 
632c1c3993eSVladimir Oltean 	vcap_key_set(vcap, &data, VCAP_IS2_TYPE, type, type_mask);
633aae4e500SVladimir Oltean 	is2_action_set(ocelot, &data, filter);
6343c83654fSVladimir Oltean 	vcap_data_set(data.counter, data.counter_offset,
635c1c3993eSVladimir Oltean 		      vcap->counter_width, filter->stats.pkts);
6363c83654fSVladimir Oltean 
6373c83654fSVladimir Oltean 	/* Write row */
638c1c3993eSVladimir Oltean 	vcap_entry2cache(ocelot, vcap, &data);
639c1c3993eSVladimir Oltean 	vcap_action2cache(ocelot, vcap, &data);
640c1c3993eSVladimir Oltean 	vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_WRITE, VCAP_SEL_ALL);
6413c83654fSVladimir Oltean }
6423c83654fSVladimir Oltean 
643c1c3993eSVladimir Oltean static void
644c1c3993eSVladimir Oltean vcap_entry_get(struct ocelot *ocelot, struct ocelot_vcap_filter *filter, int ix)
6453c83654fSVladimir Oltean {
646c1c3993eSVladimir Oltean 	const struct vcap_props *vcap = &ocelot->vcap[VCAP_IS2];
6473c83654fSVladimir Oltean 	struct vcap_data data;
648c1c3993eSVladimir Oltean 	int row, count;
6493c83654fSVladimir Oltean 	u32 cnt;
6503c83654fSVladimir Oltean 
6513c83654fSVladimir Oltean 	data.tg_sw = VCAP_TG_HALF;
652c1c3993eSVladimir Oltean 	count = (1 << (data.tg_sw - 1));
653c1c3993eSVladimir Oltean 	row = (ix / count);
654c1c3993eSVladimir Oltean 	vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_READ, VCAP_SEL_COUNTER);
655c1c3993eSVladimir Oltean 	vcap_cache2action(ocelot, vcap, &data);
656c1c3993eSVladimir Oltean 	vcap_data_offset_get(vcap, &data, ix);
6573c83654fSVladimir Oltean 	cnt = vcap_data_get(data.counter, data.counter_offset,
658c1c3993eSVladimir Oltean 			    vcap->counter_width);
6593c83654fSVladimir Oltean 
660aae4e500SVladimir Oltean 	filter->stats.pkts = cnt;
6613c83654fSVladimir Oltean }
6623c83654fSVladimir Oltean 
663c73b0ad3SVladimir Oltean static int ocelot_vcap_policer_add(struct ocelot *ocelot, u32 pol_ix,
664c73b0ad3SVladimir Oltean 				   struct ocelot_policer *pol)
665c73b0ad3SVladimir Oltean {
666c73b0ad3SVladimir Oltean 	struct qos_policer_conf pp = { 0 };
667c73b0ad3SVladimir Oltean 
668c73b0ad3SVladimir Oltean 	if (!pol)
669c73b0ad3SVladimir Oltean 		return -EINVAL;
670c73b0ad3SVladimir Oltean 
671c73b0ad3SVladimir Oltean 	pp.mode = MSCC_QOS_RATE_MODE_DATA;
672c73b0ad3SVladimir Oltean 	pp.pir = pol->rate;
673c73b0ad3SVladimir Oltean 	pp.pbs = pol->burst;
674c73b0ad3SVladimir Oltean 
675c73b0ad3SVladimir Oltean 	return qos_policer_conf_set(ocelot, 0, pol_ix, &pp);
676c73b0ad3SVladimir Oltean }
677c73b0ad3SVladimir Oltean 
678c73b0ad3SVladimir Oltean static void ocelot_vcap_policer_del(struct ocelot *ocelot,
679c73b0ad3SVladimir Oltean 				    struct ocelot_vcap_block *block,
680c73b0ad3SVladimir Oltean 				    u32 pol_ix)
681c73b0ad3SVladimir Oltean {
682c73b0ad3SVladimir Oltean 	struct ocelot_vcap_filter *filter;
683c73b0ad3SVladimir Oltean 	struct qos_policer_conf pp = {0};
684c73b0ad3SVladimir Oltean 	int index = -1;
685c73b0ad3SVladimir Oltean 
686c73b0ad3SVladimir Oltean 	if (pol_ix < block->pol_lpr)
687c73b0ad3SVladimir Oltean 		return;
688c73b0ad3SVladimir Oltean 
689c73b0ad3SVladimir Oltean 	list_for_each_entry(filter, &block->rules, list) {
690c73b0ad3SVladimir Oltean 		index++;
691ea9d1f30SVladimir Oltean 		if (filter->action.police_ena &&
692ea9d1f30SVladimir Oltean 		    filter->action.pol_ix < pol_ix) {
693ea9d1f30SVladimir Oltean 			filter->action.pol_ix += 1;
694ea9d1f30SVladimir Oltean 			ocelot_vcap_policer_add(ocelot, filter->action.pol_ix,
695ea9d1f30SVladimir Oltean 						&filter->action.pol);
696c73b0ad3SVladimir Oltean 			is2_entry_set(ocelot, index, filter);
697c73b0ad3SVladimir Oltean 		}
698c73b0ad3SVladimir Oltean 	}
699c73b0ad3SVladimir Oltean 
700c73b0ad3SVladimir Oltean 	pp.mode = MSCC_QOS_RATE_MODE_DISABLED;
701c73b0ad3SVladimir Oltean 	qos_policer_conf_set(ocelot, 0, pol_ix, &pp);
702c73b0ad3SVladimir Oltean 
703c73b0ad3SVladimir Oltean 	block->pol_lpr++;
704c73b0ad3SVladimir Oltean }
705c73b0ad3SVladimir Oltean 
706aae4e500SVladimir Oltean static void ocelot_vcap_filter_add_to_block(struct ocelot *ocelot,
707aae4e500SVladimir Oltean 					    struct ocelot_vcap_block *block,
708aae4e500SVladimir Oltean 					    struct ocelot_vcap_filter *filter)
7093c83654fSVladimir Oltean {
710aae4e500SVladimir Oltean 	struct ocelot_vcap_filter *tmp;
7113c83654fSVladimir Oltean 	struct list_head *pos, *n;
7123c83654fSVladimir Oltean 
713ea9d1f30SVladimir Oltean 	if (filter->action.police_ena) {
7143c83654fSVladimir Oltean 		block->pol_lpr--;
715ea9d1f30SVladimir Oltean 		filter->action.pol_ix = block->pol_lpr;
716ea9d1f30SVladimir Oltean 		ocelot_vcap_policer_add(ocelot, filter->action.pol_ix,
717ea9d1f30SVladimir Oltean 					&filter->action.pol);
7183c83654fSVladimir Oltean 	}
7193c83654fSVladimir Oltean 
7203c83654fSVladimir Oltean 	block->count++;
7213c83654fSVladimir Oltean 
7223c83654fSVladimir Oltean 	if (list_empty(&block->rules)) {
723aae4e500SVladimir Oltean 		list_add(&filter->list, &block->rules);
7243c83654fSVladimir Oltean 		return;
7253c83654fSVladimir Oltean 	}
7263c83654fSVladimir Oltean 
7273c83654fSVladimir Oltean 	list_for_each_safe(pos, n, &block->rules) {
728aae4e500SVladimir Oltean 		tmp = list_entry(pos, struct ocelot_vcap_filter, list);
729aae4e500SVladimir Oltean 		if (filter->prio < tmp->prio)
7303c83654fSVladimir Oltean 			break;
7313c83654fSVladimir Oltean 	}
732aae4e500SVladimir Oltean 	list_add(&filter->list, pos->prev);
7333c83654fSVladimir Oltean }
7343c83654fSVladimir Oltean 
735aae4e500SVladimir Oltean static int ocelot_vcap_block_get_filter_index(struct ocelot_vcap_block *block,
736aae4e500SVladimir Oltean 					      struct ocelot_vcap_filter *filter)
7373c83654fSVladimir Oltean {
738aae4e500SVladimir Oltean 	struct ocelot_vcap_filter *tmp;
739ed5672d8SXiaoliang Yang 	int index = 0;
7403c83654fSVladimir Oltean 
7413c83654fSVladimir Oltean 	list_for_each_entry(tmp, &block->rules, list) {
742aae4e500SVladimir Oltean 		if (filter->id == tmp->id)
7433c83654fSVladimir Oltean 			return index;
744ed5672d8SXiaoliang Yang 		index++;
745ed5672d8SXiaoliang Yang 	}
746ed5672d8SXiaoliang Yang 
747ed5672d8SXiaoliang Yang 	return -ENOENT;
7483c83654fSVladimir Oltean }
7493c83654fSVladimir Oltean 
750aae4e500SVladimir Oltean static struct ocelot_vcap_filter*
751085f5b91SVladimir Oltean ocelot_vcap_block_find_filter_by_index(struct ocelot_vcap_block *block,
752aae4e500SVladimir Oltean 				       int index)
7533c83654fSVladimir Oltean {
754aae4e500SVladimir Oltean 	struct ocelot_vcap_filter *tmp;
7553c83654fSVladimir Oltean 	int i = 0;
7563c83654fSVladimir Oltean 
7573c83654fSVladimir Oltean 	list_for_each_entry(tmp, &block->rules, list) {
7583c83654fSVladimir Oltean 		if (i == index)
7593c83654fSVladimir Oltean 			return tmp;
7603c83654fSVladimir Oltean 		++i;
7613c83654fSVladimir Oltean 	}
7623c83654fSVladimir Oltean 
7633c83654fSVladimir Oltean 	return NULL;
7643c83654fSVladimir Oltean }
7653c83654fSVladimir Oltean 
766085f5b91SVladimir Oltean struct ocelot_vcap_filter *
767085f5b91SVladimir Oltean ocelot_vcap_block_find_filter_by_id(struct ocelot_vcap_block *block, int id)
768085f5b91SVladimir Oltean {
769085f5b91SVladimir Oltean 	struct ocelot_vcap_filter *filter;
770085f5b91SVladimir Oltean 
771085f5b91SVladimir Oltean 	list_for_each_entry(filter, &block->rules, list)
772085f5b91SVladimir Oltean 		if (filter->id == id)
773085f5b91SVladimir Oltean 			return filter;
774085f5b91SVladimir Oltean 
775085f5b91SVladimir Oltean 	return NULL;
776085f5b91SVladimir Oltean }
777085f5b91SVladimir Oltean 
7783c83654fSVladimir Oltean /* If @on=false, then SNAP, ARP, IP and OAM frames will not match on keys based
7793c83654fSVladimir Oltean  * on destination and source MAC addresses, but only on higher-level protocol
7803c83654fSVladimir Oltean  * information. The only frame types to match on keys containing MAC addresses
7813c83654fSVladimir Oltean  * in this case are non-SNAP, non-ARP, non-IP and non-OAM frames.
7823c83654fSVladimir Oltean  *
7833c83654fSVladimir Oltean  * If @on=true, then the above frame types (SNAP, ARP, IP and OAM) will match
7843c83654fSVladimir Oltean  * on MAC_ETYPE keys such as destination and source MAC on this ingress port.
7853c83654fSVladimir Oltean  * However the setting has the side effect of making these frames not matching
7863c83654fSVladimir Oltean  * on any _other_ keys than MAC_ETYPE ones.
7873c83654fSVladimir Oltean  */
7883c83654fSVladimir Oltean static void ocelot_match_all_as_mac_etype(struct ocelot *ocelot, int port,
7893c83654fSVladimir Oltean 					  bool on)
7903c83654fSVladimir Oltean {
7913c83654fSVladimir Oltean 	u32 val = 0;
7923c83654fSVladimir Oltean 
7933c83654fSVladimir Oltean 	if (on)
7943c83654fSVladimir Oltean 		val = ANA_PORT_VCAP_S2_CFG_S2_SNAP_DIS(3) |
7953c83654fSVladimir Oltean 		      ANA_PORT_VCAP_S2_CFG_S2_ARP_DIS(3) |
7963c83654fSVladimir Oltean 		      ANA_PORT_VCAP_S2_CFG_S2_IP_TCPUDP_DIS(3) |
7973c83654fSVladimir Oltean 		      ANA_PORT_VCAP_S2_CFG_S2_IP_OTHER_DIS(3) |
7983c83654fSVladimir Oltean 		      ANA_PORT_VCAP_S2_CFG_S2_OAM_DIS(3);
7993c83654fSVladimir Oltean 
8003c83654fSVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
8013c83654fSVladimir Oltean 		       ANA_PORT_VCAP_S2_CFG_S2_SNAP_DIS_M |
8023c83654fSVladimir Oltean 		       ANA_PORT_VCAP_S2_CFG_S2_ARP_DIS_M |
8033c83654fSVladimir Oltean 		       ANA_PORT_VCAP_S2_CFG_S2_IP_TCPUDP_DIS_M |
8043c83654fSVladimir Oltean 		       ANA_PORT_VCAP_S2_CFG_S2_IP_OTHER_DIS_M |
8053c83654fSVladimir Oltean 		       ANA_PORT_VCAP_S2_CFG_S2_OAM_DIS_M,
8063c83654fSVladimir Oltean 		       ANA_PORT_VCAP_S2_CFG, port);
8073c83654fSVladimir Oltean }
8083c83654fSVladimir Oltean 
809aae4e500SVladimir Oltean static bool
810aae4e500SVladimir Oltean ocelot_vcap_is_problematic_mac_etype(struct ocelot_vcap_filter *filter)
8113c83654fSVladimir Oltean {
8123c83654fSVladimir Oltean 	u16 proto, mask;
8133c83654fSVladimir Oltean 
814aae4e500SVladimir Oltean 	if (filter->key_type != OCELOT_VCAP_KEY_ETYPE)
8153c83654fSVladimir Oltean 		return false;
8163c83654fSVladimir Oltean 
817aae4e500SVladimir Oltean 	proto = ntohs(*(__be16 *)filter->key.etype.etype.value);
818aae4e500SVladimir Oltean 	mask = ntohs(*(__be16 *)filter->key.etype.etype.mask);
8193c83654fSVladimir Oltean 
8203c83654fSVladimir Oltean 	/* ETH_P_ALL match, so all protocols below are included */
8213c83654fSVladimir Oltean 	if (mask == 0)
8223c83654fSVladimir Oltean 		return true;
8233c83654fSVladimir Oltean 	if (proto == ETH_P_ARP)
8243c83654fSVladimir Oltean 		return true;
8253c83654fSVladimir Oltean 	if (proto == ETH_P_IP)
8263c83654fSVladimir Oltean 		return true;
8273c83654fSVladimir Oltean 	if (proto == ETH_P_IPV6)
8283c83654fSVladimir Oltean 		return true;
8293c83654fSVladimir Oltean 
8303c83654fSVladimir Oltean 	return false;
8313c83654fSVladimir Oltean }
8323c83654fSVladimir Oltean 
833aae4e500SVladimir Oltean static bool
834aae4e500SVladimir Oltean ocelot_vcap_is_problematic_non_mac_etype(struct ocelot_vcap_filter *filter)
8353c83654fSVladimir Oltean {
836aae4e500SVladimir Oltean 	if (filter->key_type == OCELOT_VCAP_KEY_SNAP)
8373c83654fSVladimir Oltean 		return true;
838aae4e500SVladimir Oltean 	if (filter->key_type == OCELOT_VCAP_KEY_ARP)
8393c83654fSVladimir Oltean 		return true;
840aae4e500SVladimir Oltean 	if (filter->key_type == OCELOT_VCAP_KEY_IPV4)
8413c83654fSVladimir Oltean 		return true;
842aae4e500SVladimir Oltean 	if (filter->key_type == OCELOT_VCAP_KEY_IPV6)
8433c83654fSVladimir Oltean 		return true;
8443c83654fSVladimir Oltean 	return false;
8453c83654fSVladimir Oltean }
8463c83654fSVladimir Oltean 
847aae4e500SVladimir Oltean static bool
848aae4e500SVladimir Oltean ocelot_exclusive_mac_etype_filter_rules(struct ocelot *ocelot,
849aae4e500SVladimir Oltean 					struct ocelot_vcap_filter *filter)
8503c83654fSVladimir Oltean {
851aae4e500SVladimir Oltean 	struct ocelot_vcap_block *block = &ocelot->block;
852aae4e500SVladimir Oltean 	struct ocelot_vcap_filter *tmp;
8533c83654fSVladimir Oltean 	unsigned long port;
8543c83654fSVladimir Oltean 	int i;
8553c83654fSVladimir Oltean 
856aae4e500SVladimir Oltean 	if (ocelot_vcap_is_problematic_mac_etype(filter)) {
8573c83654fSVladimir Oltean 		/* Search for any non-MAC_ETYPE rules on the port */
8583c83654fSVladimir Oltean 		for (i = 0; i < block->count; i++) {
859085f5b91SVladimir Oltean 			tmp = ocelot_vcap_block_find_filter_by_index(block, i);
860aae4e500SVladimir Oltean 			if (tmp->ingress_port_mask & filter->ingress_port_mask &&
861aae4e500SVladimir Oltean 			    ocelot_vcap_is_problematic_non_mac_etype(tmp))
8623c83654fSVladimir Oltean 				return false;
8633c83654fSVladimir Oltean 		}
8643c83654fSVladimir Oltean 
865aae4e500SVladimir Oltean 		for_each_set_bit(port, &filter->ingress_port_mask,
8663c83654fSVladimir Oltean 				 ocelot->num_phys_ports)
8673c83654fSVladimir Oltean 			ocelot_match_all_as_mac_etype(ocelot, port, true);
868aae4e500SVladimir Oltean 	} else if (ocelot_vcap_is_problematic_non_mac_etype(filter)) {
8693c83654fSVladimir Oltean 		/* Search for any MAC_ETYPE rules on the port */
8703c83654fSVladimir Oltean 		for (i = 0; i < block->count; i++) {
871085f5b91SVladimir Oltean 			tmp = ocelot_vcap_block_find_filter_by_index(block, i);
872aae4e500SVladimir Oltean 			if (tmp->ingress_port_mask & filter->ingress_port_mask &&
873aae4e500SVladimir Oltean 			    ocelot_vcap_is_problematic_mac_etype(tmp))
8743c83654fSVladimir Oltean 				return false;
8753c83654fSVladimir Oltean 		}
8763c83654fSVladimir Oltean 
877aae4e500SVladimir Oltean 		for_each_set_bit(port, &filter->ingress_port_mask,
8783c83654fSVladimir Oltean 				 ocelot->num_phys_ports)
8793c83654fSVladimir Oltean 			ocelot_match_all_as_mac_etype(ocelot, port, false);
8803c83654fSVladimir Oltean 	}
8813c83654fSVladimir Oltean 
8823c83654fSVladimir Oltean 	return true;
8833c83654fSVladimir Oltean }
8843c83654fSVladimir Oltean 
885aae4e500SVladimir Oltean int ocelot_vcap_filter_add(struct ocelot *ocelot,
886aae4e500SVladimir Oltean 			   struct ocelot_vcap_filter *filter,
8873c83654fSVladimir Oltean 			   struct netlink_ext_ack *extack)
8883c83654fSVladimir Oltean {
889aae4e500SVladimir Oltean 	struct ocelot_vcap_block *block = &ocelot->block;
8903c83654fSVladimir Oltean 	int i, index;
8913c83654fSVladimir Oltean 
892aae4e500SVladimir Oltean 	if (!ocelot_exclusive_mac_etype_filter_rules(ocelot, filter)) {
8933c83654fSVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
8943c83654fSVladimir Oltean 				   "Cannot mix MAC_ETYPE with non-MAC_ETYPE rules");
8953c83654fSVladimir Oltean 		return -EBUSY;
8963c83654fSVladimir Oltean 	}
8973c83654fSVladimir Oltean 
898aae4e500SVladimir Oltean 	/* Add filter to the linked list */
899aae4e500SVladimir Oltean 	ocelot_vcap_filter_add_to_block(ocelot, block, filter);
9003c83654fSVladimir Oltean 
901aae4e500SVladimir Oltean 	/* Get the index of the inserted filter */
902aae4e500SVladimir Oltean 	index = ocelot_vcap_block_get_filter_index(block, filter);
903ed5672d8SXiaoliang Yang 	if (index < 0)
904ed5672d8SXiaoliang Yang 		return index;
9053c83654fSVladimir Oltean 
906aae4e500SVladimir Oltean 	/* Move down the rules to make place for the new filter */
9073c83654fSVladimir Oltean 	for (i = block->count - 1; i > index; i--) {
908aae4e500SVladimir Oltean 		struct ocelot_vcap_filter *tmp;
909aae4e500SVladimir Oltean 
910085f5b91SVladimir Oltean 		tmp = ocelot_vcap_block_find_filter_by_index(block, i);
911aae4e500SVladimir Oltean 		is2_entry_set(ocelot, i, tmp);
9123c83654fSVladimir Oltean 	}
9133c83654fSVladimir Oltean 
914aae4e500SVladimir Oltean 	/* Now insert the new filter */
915aae4e500SVladimir Oltean 	is2_entry_set(ocelot, index, filter);
9163c83654fSVladimir Oltean 	return 0;
9173c83654fSVladimir Oltean }
9183c83654fSVladimir Oltean 
919aae4e500SVladimir Oltean static void ocelot_vcap_block_remove_filter(struct ocelot *ocelot,
920aae4e500SVladimir Oltean 					    struct ocelot_vcap_block *block,
921aae4e500SVladimir Oltean 					    struct ocelot_vcap_filter *filter)
9223c83654fSVladimir Oltean {
923aae4e500SVladimir Oltean 	struct ocelot_vcap_filter *tmp;
9243c83654fSVladimir Oltean 	struct list_head *pos, *q;
9253c83654fSVladimir Oltean 
9263c83654fSVladimir Oltean 	list_for_each_safe(pos, q, &block->rules) {
927aae4e500SVladimir Oltean 		tmp = list_entry(pos, struct ocelot_vcap_filter, list);
928aae4e500SVladimir Oltean 		if (tmp->id == filter->id) {
929ea9d1f30SVladimir Oltean 			if (tmp->action.police_ena)
930c73b0ad3SVladimir Oltean 				ocelot_vcap_policer_del(ocelot, block,
931ea9d1f30SVladimir Oltean 							tmp->action.pol_ix);
9323c83654fSVladimir Oltean 
9333c83654fSVladimir Oltean 			list_del(pos);
9343c83654fSVladimir Oltean 			kfree(tmp);
9353c83654fSVladimir Oltean 		}
9363c83654fSVladimir Oltean 	}
9373c83654fSVladimir Oltean 
9383c83654fSVladimir Oltean 	block->count--;
9393c83654fSVladimir Oltean }
9403c83654fSVladimir Oltean 
941aae4e500SVladimir Oltean int ocelot_vcap_filter_del(struct ocelot *ocelot,
942aae4e500SVladimir Oltean 			   struct ocelot_vcap_filter *filter)
9433c83654fSVladimir Oltean {
944aae4e500SVladimir Oltean 	struct ocelot_vcap_block *block = &ocelot->block;
945aae4e500SVladimir Oltean 	struct ocelot_vcap_filter del_filter;
9463c83654fSVladimir Oltean 	int i, index;
9473c83654fSVladimir Oltean 
948aae4e500SVladimir Oltean 	memset(&del_filter, 0, sizeof(del_filter));
9493c83654fSVladimir Oltean 
950aae4e500SVladimir Oltean 	/* Gets index of the filter */
951aae4e500SVladimir Oltean 	index = ocelot_vcap_block_get_filter_index(block, filter);
952ed5672d8SXiaoliang Yang 	if (index < 0)
953ed5672d8SXiaoliang Yang 		return index;
9543c83654fSVladimir Oltean 
955aae4e500SVladimir Oltean 	/* Delete filter */
956aae4e500SVladimir Oltean 	ocelot_vcap_block_remove_filter(ocelot, block, filter);
9573c83654fSVladimir Oltean 
958aae4e500SVladimir Oltean 	/* Move up all the blocks over the deleted filter */
9593c83654fSVladimir Oltean 	for (i = index; i < block->count; i++) {
960aae4e500SVladimir Oltean 		struct ocelot_vcap_filter *tmp;
961aae4e500SVladimir Oltean 
962085f5b91SVladimir Oltean 		tmp = ocelot_vcap_block_find_filter_by_index(block, i);
963aae4e500SVladimir Oltean 		is2_entry_set(ocelot, i, tmp);
9643c83654fSVladimir Oltean 	}
9653c83654fSVladimir Oltean 
966aae4e500SVladimir Oltean 	/* Now delete the last filter, because it is duplicated */
967aae4e500SVladimir Oltean 	is2_entry_set(ocelot, block->count, &del_filter);
9683c83654fSVladimir Oltean 
9693c83654fSVladimir Oltean 	return 0;
9703c83654fSVladimir Oltean }
9713c83654fSVladimir Oltean 
972aae4e500SVladimir Oltean int ocelot_vcap_filter_stats_update(struct ocelot *ocelot,
973aae4e500SVladimir Oltean 				    struct ocelot_vcap_filter *filter)
9743c83654fSVladimir Oltean {
975aae4e500SVladimir Oltean 	struct ocelot_vcap_block *block = &ocelot->block;
97698642d1aSVladimir Oltean 	struct ocelot_vcap_filter tmp;
9773c83654fSVladimir Oltean 	int index;
9783c83654fSVladimir Oltean 
979aae4e500SVladimir Oltean 	index = ocelot_vcap_block_get_filter_index(block, filter);
980ed5672d8SXiaoliang Yang 	if (index < 0)
981ed5672d8SXiaoliang Yang 		return index;
982ed5672d8SXiaoliang Yang 
983c1c3993eSVladimir Oltean 	vcap_entry_get(ocelot, filter, index);
9843c83654fSVladimir Oltean 
9853c83654fSVladimir Oltean 	/* After we get the result we need to clear the counters */
98698642d1aSVladimir Oltean 	tmp = *filter;
98798642d1aSVladimir Oltean 	tmp.stats.pkts = 0;
98898642d1aSVladimir Oltean 	is2_entry_set(ocelot, index, &tmp);
9893c83654fSVladimir Oltean 
9903c83654fSVladimir Oltean 	return 0;
9913c83654fSVladimir Oltean }
9923c83654fSVladimir Oltean 
993c1c3993eSVladimir Oltean static void ocelot_vcap_init_one(struct ocelot *ocelot,
994c1c3993eSVladimir Oltean 				 const struct vcap_props *vcap)
9953c83654fSVladimir Oltean {
9963c83654fSVladimir Oltean 	struct vcap_data data;
9973c83654fSVladimir Oltean 
9983c83654fSVladimir Oltean 	memset(&data, 0, sizeof(data));
9993c83654fSVladimir Oltean 
1000c1c3993eSVladimir Oltean 	vcap_entry2cache(ocelot, vcap, &data);
1001c1c3993eSVladimir Oltean 	ocelot_target_write(ocelot, vcap->target, vcap->entry_count,
1002c1c3993eSVladimir Oltean 			    VCAP_CORE_MV_CFG);
1003c1c3993eSVladimir Oltean 	vcap_cmd(ocelot, vcap, 0, VCAP_CMD_INITIALIZE, VCAP_SEL_ENTRY);
10043c83654fSVladimir Oltean 
1005c1c3993eSVladimir Oltean 	vcap_action2cache(ocelot, vcap, &data);
1006c1c3993eSVladimir Oltean 	ocelot_target_write(ocelot, vcap->target, vcap->action_count,
1007c1c3993eSVladimir Oltean 			    VCAP_CORE_MV_CFG);
1008c1c3993eSVladimir Oltean 	vcap_cmd(ocelot, vcap, 0, VCAP_CMD_INITIALIZE,
10093c83654fSVladimir Oltean 		 VCAP_SEL_ACTION | VCAP_SEL_COUNTER);
1010c1c3993eSVladimir Oltean }
1011c1c3993eSVladimir Oltean 
101220968054SVladimir Oltean static void ocelot_vcap_detect_constants(struct ocelot *ocelot,
101320968054SVladimir Oltean 					 struct vcap_props *vcap)
101420968054SVladimir Oltean {
101520968054SVladimir Oltean 	int counter_memory_width;
101620968054SVladimir Oltean 	int num_default_actions;
101720968054SVladimir Oltean 	int version;
101820968054SVladimir Oltean 
101920968054SVladimir Oltean 	version = ocelot_target_read(ocelot, vcap->target,
102020968054SVladimir Oltean 				     VCAP_CONST_VCAP_VER);
102120968054SVladimir Oltean 	/* Only version 0 VCAP supported for now */
102220968054SVladimir Oltean 	if (WARN_ON(version != 0))
102320968054SVladimir Oltean 		return;
102420968054SVladimir Oltean 
102520968054SVladimir Oltean 	/* Width in bits of type-group field */
102620968054SVladimir Oltean 	vcap->tg_width = ocelot_target_read(ocelot, vcap->target,
102720968054SVladimir Oltean 					    VCAP_CONST_ENTRY_TG_WIDTH);
102820968054SVladimir Oltean 	/* Number of subwords per TCAM row */
102920968054SVladimir Oltean 	vcap->sw_count = ocelot_target_read(ocelot, vcap->target,
103020968054SVladimir Oltean 					    VCAP_CONST_ENTRY_SWCNT);
103120968054SVladimir Oltean 	/* Number of rows in TCAM. There can be this many full keys, or double
103220968054SVladimir Oltean 	 * this number half keys, or 4 times this number quarter keys.
103320968054SVladimir Oltean 	 */
103420968054SVladimir Oltean 	vcap->entry_count = ocelot_target_read(ocelot, vcap->target,
103520968054SVladimir Oltean 					       VCAP_CONST_ENTRY_CNT);
103620968054SVladimir Oltean 	/* Assuming there are 4 subwords per TCAM row, their layout in the
103720968054SVladimir Oltean 	 * actual TCAM (not in the cache) would be:
103820968054SVladimir Oltean 	 *
103920968054SVladimir Oltean 	 * |  SW 3  | TG 3 |  SW 2  | TG 2 |  SW 1  | TG 1 |  SW 0  | TG 0 |
104020968054SVladimir Oltean 	 *
104120968054SVladimir Oltean 	 * (where SW=subword and TG=Type-Group).
104220968054SVladimir Oltean 	 *
104320968054SVladimir Oltean 	 * What VCAP_CONST_ENTRY_CNT is giving us is the width of one full TCAM
104420968054SVladimir Oltean 	 * row. But when software accesses the TCAM through the cache
104520968054SVladimir Oltean 	 * registers, the Type-Group values are written through another set of
104620968054SVladimir Oltean 	 * registers VCAP_TG_DAT, and therefore, it appears as though the 4
104720968054SVladimir Oltean 	 * subwords are contiguous in the cache memory.
104820968054SVladimir Oltean 	 * Important mention: regardless of the number of key entries per row
104920968054SVladimir Oltean 	 * (and therefore of key size: 1 full key or 2 half keys or 4 quarter
105020968054SVladimir Oltean 	 * keys), software always has to configure 4 Type-Group values. For
105120968054SVladimir Oltean 	 * example, in the case of 1 full key, the driver needs to set all 4
105220968054SVladimir Oltean 	 * Type-Group to be full key.
105320968054SVladimir Oltean 	 *
105420968054SVladimir Oltean 	 * For this reason, we need to fix up the value that the hardware is
105520968054SVladimir Oltean 	 * giving us. We don't actually care about the width of the entry in
105620968054SVladimir Oltean 	 * the TCAM. What we care about is the width of the entry in the cache
105720968054SVladimir Oltean 	 * registers, which is how we get to interact with it. And since the
105820968054SVladimir Oltean 	 * VCAP_ENTRY_DAT cache registers access only the subwords and not the
105920968054SVladimir Oltean 	 * Type-Groups, this means we need to subtract the width of the
106020968054SVladimir Oltean 	 * Type-Groups when packing and unpacking key entry data in a TCAM row.
106120968054SVladimir Oltean 	 */
106220968054SVladimir Oltean 	vcap->entry_width = ocelot_target_read(ocelot, vcap->target,
106320968054SVladimir Oltean 					       VCAP_CONST_ENTRY_WIDTH);
106420968054SVladimir Oltean 	vcap->entry_width -= vcap->tg_width * vcap->sw_count;
106520968054SVladimir Oltean 	num_default_actions = ocelot_target_read(ocelot, vcap->target,
106620968054SVladimir Oltean 						 VCAP_CONST_ACTION_DEF_CNT);
106720968054SVladimir Oltean 	vcap->action_count = vcap->entry_count + num_default_actions;
106820968054SVladimir Oltean 	vcap->action_width = ocelot_target_read(ocelot, vcap->target,
106920968054SVladimir Oltean 						VCAP_CONST_ACTION_WIDTH);
107020968054SVladimir Oltean 	/* The width of the counter memory, this is the complete width of all
107120968054SVladimir Oltean 	 * counter-fields associated with one full-word entry. There is one
107220968054SVladimir Oltean 	 * counter per entry sub-word (see CAP_CORE::ENTRY_SWCNT for number of
107320968054SVladimir Oltean 	 * subwords.)
107420968054SVladimir Oltean 	 */
107520968054SVladimir Oltean 	vcap->counter_words = vcap->sw_count;
107620968054SVladimir Oltean 	counter_memory_width = ocelot_target_read(ocelot, vcap->target,
107720968054SVladimir Oltean 						  VCAP_CONST_CNT_WIDTH);
107820968054SVladimir Oltean 	vcap->counter_width = counter_memory_width / vcap->counter_words;
107920968054SVladimir Oltean }
108020968054SVladimir Oltean 
1081c1c3993eSVladimir Oltean int ocelot_vcap_init(struct ocelot *ocelot)
1082c1c3993eSVladimir Oltean {
1083c1c3993eSVladimir Oltean 	struct ocelot_vcap_block *block = &ocelot->block;
108420968054SVladimir Oltean 	int i;
10853c83654fSVladimir Oltean 
10863c83654fSVladimir Oltean 	/* Create a policer that will drop the frames for the cpu.
10873c83654fSVladimir Oltean 	 * This policer will be used as action in the acl rules to drop
10883c83654fSVladimir Oltean 	 * frames.
10893c83654fSVladimir Oltean 	 */
10903c83654fSVladimir Oltean 	ocelot_write_gix(ocelot, 0x299, ANA_POL_MODE_CFG,
10913c83654fSVladimir Oltean 			 OCELOT_POLICER_DISCARD);
10923c83654fSVladimir Oltean 	ocelot_write_gix(ocelot, 0x1, ANA_POL_PIR_CFG,
10933c83654fSVladimir Oltean 			 OCELOT_POLICER_DISCARD);
10943c83654fSVladimir Oltean 	ocelot_write_gix(ocelot, 0x3fffff, ANA_POL_PIR_STATE,
10953c83654fSVladimir Oltean 			 OCELOT_POLICER_DISCARD);
10963c83654fSVladimir Oltean 	ocelot_write_gix(ocelot, 0x0, ANA_POL_CIR_CFG,
10973c83654fSVladimir Oltean 			 OCELOT_POLICER_DISCARD);
10983c83654fSVladimir Oltean 	ocelot_write_gix(ocelot, 0x3fffff, ANA_POL_CIR_STATE,
10993c83654fSVladimir Oltean 			 OCELOT_POLICER_DISCARD);
11003c83654fSVladimir Oltean 
110120968054SVladimir Oltean 	for (i = 0; i < OCELOT_NUM_VCAP_BLOCKS; i++) {
110220968054SVladimir Oltean 		struct vcap_props *vcap = &ocelot->vcap[i];
110320968054SVladimir Oltean 
110420968054SVladimir Oltean 		ocelot_vcap_detect_constants(ocelot, vcap);
110520968054SVladimir Oltean 		ocelot_vcap_init_one(ocelot, vcap);
110620968054SVladimir Oltean 	}
110720968054SVladimir Oltean 
11083c83654fSVladimir Oltean 	block->pol_lpr = OCELOT_POLICER_DISCARD - 1;
11093c83654fSVladimir Oltean 
1110c1c3993eSVladimir Oltean 	INIT_LIST_HEAD(&block->rules);
11113c83654fSVladimir Oltean 
11123c83654fSVladimir Oltean 	return 0;
11133c83654fSVladimir Oltean }
1114