ocelot_vcap.c 40.4 KB
Newer Older
1 2 3 4 5 6 7 8
// SPDX-License-Identifier: (GPL-2.0 OR MIT)
/* Microsemi Ocelot Switch driver
 * Copyright (c) 2019 Microsemi Corporation
 */

#include <linux/iopoll.h>
#include <linux/proc_fs.h>

9
#include <soc/mscc/ocelot_vcap.h>
10
#include "ocelot_police.h"
11
#include "ocelot_vcap.h"
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

#define ENTRY_WIDTH 32

enum vcap_sel {
	VCAP_SEL_ENTRY = 0x1,
	VCAP_SEL_ACTION = 0x2,
	VCAP_SEL_COUNTER = 0x4,
	VCAP_SEL_ALL = 0x7,
};

enum vcap_cmd {
	VCAP_CMD_WRITE = 0, /* Copy from Cache to TCAM */
	VCAP_CMD_READ = 1, /* Copy from TCAM to Cache */
	VCAP_CMD_MOVE_UP = 2, /* Move <count> up */
	VCAP_CMD_MOVE_DOWN = 3, /* Move <count> down */
	VCAP_CMD_INITIALIZE = 4, /* Write all (from cache) */
};

#define VCAP_ENTRY_WIDTH 12 /* Max entry width (32bit words) */
#define VCAP_COUNTER_WIDTH 4 /* Max counter width (32bit words) */

struct vcap_data {
	u32 entry[VCAP_ENTRY_WIDTH]; /* ENTRY_DAT */
	u32 mask[VCAP_ENTRY_WIDTH]; /* MASK_DAT */
	u32 action[VCAP_ENTRY_WIDTH]; /* ACTION_DAT */
	u32 counter[VCAP_COUNTER_WIDTH]; /* CNT_DAT */
	u32 tg; /* TG_DAT */
	u32 type; /* Action type */
	u32 tg_sw; /* Current type-group */
	u32 cnt; /* Current counter */
	u32 key_offset; /* Current entry offset */
	u32 action_offset; /* Current action offset */
	u32 counter_offset; /* Current counter offset */
	u32 tg_value; /* Current type-group value */
	u32 tg_mask; /* Current type-group mask */
47
};
48

49 50
static u32 vcap_read_update_ctrl(struct ocelot *ocelot,
				 const struct vcap_props *vcap)
51
{
52
	return ocelot_target_read(ocelot, vcap->target, VCAP_CORE_UPDATE_CTRL);
53 54
}

55 56
static void vcap_cmd(struct ocelot *ocelot, const struct vcap_props *vcap,
		     u16 ix, int cmd, int sel)
57
{
58 59 60
	u32 value = (VCAP_CORE_UPDATE_CTRL_UPDATE_CMD(cmd) |
		     VCAP_CORE_UPDATE_CTRL_UPDATE_ADDR(ix) |
		     VCAP_CORE_UPDATE_CTRL_UPDATE_SHOT);
61

62
	if ((sel & VCAP_SEL_ENTRY) && ix >= vcap->entry_count)
63 64 65
		return;

	if (!(sel & VCAP_SEL_ENTRY))
66
		value |= VCAP_CORE_UPDATE_CTRL_UPDATE_ENTRY_DIS;
67 68

	if (!(sel & VCAP_SEL_ACTION))
69
		value |= VCAP_CORE_UPDATE_CTRL_UPDATE_ACTION_DIS;
70 71

	if (!(sel & VCAP_SEL_COUNTER))
72 73 74
		value |= VCAP_CORE_UPDATE_CTRL_UPDATE_CNT_DIS;

	ocelot_target_write(ocelot, vcap->target, value, VCAP_CORE_UPDATE_CTRL);
75

76 77 78
	read_poll_timeout(vcap_read_update_ctrl, value,
			  (value & VCAP_CORE_UPDATE_CTRL_UPDATE_SHOT) == 0,
			  10, 100000, false, ocelot, vcap);
79 80 81
}

/* Convert from 0-based row to VCAP entry row and run command */
82 83
static void vcap_row_cmd(struct ocelot *ocelot, const struct vcap_props *vcap,
			 u32 row, int cmd, int sel)
84
{
85
	vcap_cmd(ocelot, vcap, vcap->entry_count - row - 1, cmd, sel);
86 87
}

88 89 90
static void vcap_entry2cache(struct ocelot *ocelot,
			     const struct vcap_props *vcap,
			     struct vcap_data *data)
91
{
92 93
	u32 entry_words, i;

94
	entry_words = DIV_ROUND_UP(vcap->entry_width, ENTRY_WIDTH);
95

96
	for (i = 0; i < entry_words; i++) {
97 98 99 100
		ocelot_target_write_rix(ocelot, vcap->target, data->entry[i],
					VCAP_CACHE_ENTRY_DAT, i);
		ocelot_target_write_rix(ocelot, vcap->target, ~data->mask[i],
					VCAP_CACHE_MASK_DAT, i);
101
	}
102
	ocelot_target_write(ocelot, vcap->target, data->tg, VCAP_CACHE_TG_DAT);
103 104
}

105 106 107
static void vcap_cache2entry(struct ocelot *ocelot,
			     const struct vcap_props *vcap,
			     struct vcap_data *data)
108
{
109 110
	u32 entry_words, i;

111
	entry_words = DIV_ROUND_UP(vcap->entry_width, ENTRY_WIDTH);
112

113
	for (i = 0; i < entry_words; i++) {
114 115
		data->entry[i] = ocelot_target_read_rix(ocelot, vcap->target,
							VCAP_CACHE_ENTRY_DAT, i);
116
		// Invert mask
117 118
		data->mask[i] = ~ocelot_target_read_rix(ocelot, vcap->target,
							VCAP_CACHE_MASK_DAT, i);
119
	}
120
	data->tg = ocelot_target_read(ocelot, vcap->target, VCAP_CACHE_TG_DAT);
121 122
}

123 124 125
static void vcap_action2cache(struct ocelot *ocelot,
			      const struct vcap_props *vcap,
			      struct vcap_data *data)
126
{
127 128
	u32 action_words, mask;
	int i, width;
129 130

	/* Encode action type */
131
	width = vcap->action_type_width;
132 133 134 135 136
	if (width) {
		mask = GENMASK(width, 0);
		data->action[0] = ((data->action[0] & ~mask) | data->type);
	}

137
	action_words = DIV_ROUND_UP(vcap->action_width, ENTRY_WIDTH);
138

139
	for (i = 0; i < action_words; i++)
140 141
		ocelot_target_write_rix(ocelot, vcap->target, data->action[i],
					VCAP_CACHE_ACTION_DAT, i);
142

143 144 145
	for (i = 0; i < vcap->counter_words; i++)
		ocelot_target_write_rix(ocelot, vcap->target, data->counter[i],
					VCAP_CACHE_CNT_DAT, i);
146 147
}

148 149 150
static void vcap_cache2action(struct ocelot *ocelot,
			      const struct vcap_props *vcap,
			      struct vcap_data *data)
151
{
152 153
	u32 action_words;
	int i, width;
154

155
	action_words = DIV_ROUND_UP(vcap->action_width, ENTRY_WIDTH);
156

157
	for (i = 0; i < action_words; i++)
158 159 160
		data->action[i] = ocelot_target_read_rix(ocelot, vcap->target,
							 VCAP_CACHE_ACTION_DAT,
							 i);
161

162 163 164 165
	for (i = 0; i < vcap->counter_words; i++)
		data->counter[i] = ocelot_target_read_rix(ocelot, vcap->target,
							  VCAP_CACHE_CNT_DAT,
							  i);
166 167

	/* Extract action type */
168
	width = vcap->action_type_width;
169 170 171 172
	data->type = (width ? (data->action[0] & GENMASK(width, 0)) : 0);
}

/* Calculate offsets for entry */
173 174
static void vcap_data_offset_get(const struct vcap_props *vcap,
				 struct vcap_data *data, int ix)
175
{
176 177
	int num_subwords_per_entry, num_subwords_per_action;
	int i, col, offset, num_entries_per_row, base;
178
	u32 width = vcap->tg_width;
179

180 181
	switch (data->tg_sw) {
	case VCAP_TG_FULL:
182
		num_entries_per_row = 1;
183 184
		break;
	case VCAP_TG_HALF:
185
		num_entries_per_row = 2;
186 187
		break;
	case VCAP_TG_QUARTER:
188
		num_entries_per_row = 4;
189 190 191 192 193
		break;
	default:
		return;
	}

194
	col = (ix % num_entries_per_row);
195 196 197
	num_subwords_per_entry = (vcap->sw_count / num_entries_per_row);
	base = (vcap->sw_count - col * num_subwords_per_entry -
		num_subwords_per_entry);
198 199
	data->tg_value = 0;
	data->tg_mask = 0;
200
	for (i = 0; i < num_subwords_per_entry; i++) {
201 202 203 204 205 206
		offset = ((base + i) * width);
		data->tg_value |= (data->tg_sw << offset);
		data->tg_mask |= GENMASK(offset + width - 1, offset);
	}

	/* Calculate key/action/counter offsets */
207
	col = (num_entries_per_row - col - 1);
208
	data->key_offset = (base * vcap->entry_width) / vcap->sw_count;
209 210
	data->counter_offset = (num_subwords_per_entry * col *
				vcap->counter_width);
211
	i = data->type;
212
	width = vcap->action_table[i].width;
213 214 215 216
	num_subwords_per_action = vcap->action_table[i].count;
	data->action_offset = ((num_subwords_per_action * col * width) /
				num_entries_per_row);
	data->action_offset += vcap->action_type_width;
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
}

static void vcap_data_set(u32 *data, u32 offset, u32 len, u32 value)
{
	u32 i, v, m;

	for (i = 0; i < len; i++, offset++) {
		v = data[offset / ENTRY_WIDTH];
		m = (1 << (offset % ENTRY_WIDTH));
		if (value & (1 << i))
			v |= m;
		else
			v &= ~m;
		data[offset / ENTRY_WIDTH] = v;
	}
}

static u32 vcap_data_get(u32 *data, u32 offset, u32 len)
{
	u32 i, v, m, value = 0;

	for (i = 0; i < len; i++, offset++) {
		v = data[offset / ENTRY_WIDTH];
		m = (1 << (offset % ENTRY_WIDTH));
		if (v & m)
			value |= (1 << i);
	}
	return value;
}

247 248
static void vcap_key_field_set(struct vcap_data *data, u32 offset, u32 width,
			       u32 value, u32 mask)
249 250 251 252 253
{
	vcap_data_set(data->entry, offset + data->key_offset, width, value);
	vcap_data_set(data->mask, offset + data->key_offset, width, mask);
}

254 255
static void vcap_key_set(const struct vcap_props *vcap, struct vcap_data *data,
			 int field, u32 value, u32 mask)
256
{
257 258
	u32 offset = vcap->keys[field].offset;
	u32 length = vcap->keys[field].length;
259 260 261 262

	vcap_key_field_set(data, offset, length, value, mask);
}

263 264
static void vcap_key_bytes_set(const struct vcap_props *vcap,
			       struct vcap_data *data, int field,
265
			       u8 *val, u8 *msk)
266
{
267 268
	u32 offset = vcap->keys[field].offset;
	u32 count  = vcap->keys[field].length;
269 270
	u32 i, j, n = 0, value = 0, mask = 0;

271 272
	WARN_ON(count % 8);

273 274 275
	/* Data wider than 32 bits are split up in chunks of maximum 32 bits.
	 * The 32 LSB of the data are written to the 32 MSB of the TCAM.
	 */
276 277 278
	offset += count;
	count /= 8;

279 280 281 282 283 284 285
	for (i = 0; i < count; i++) {
		j = (count - i - 1);
		value += (val[j] << n);
		mask += (msk[j] << n);
		n += 8;
		if (n == ENTRY_WIDTH || (i + 1) == count) {
			offset -= n;
286
			vcap_key_field_set(data, offset, n, value, mask);
287 288 289 290 291 292 293
			n = 0;
			value = 0;
			mask = 0;
		}
	}
}

294 295
static void vcap_key_l4_port_set(const struct vcap_props *vcap,
				 struct vcap_data *data, int field,
296 297
				 struct ocelot_vcap_udp_tcp *port)
{
298 299
	u32 offset = vcap->keys[field].offset;
	u32 length = vcap->keys[field].length;
300 301 302 303

	WARN_ON(length != 16);

	vcap_key_field_set(data, offset, length, port->value, port->mask);
304 305
}

306 307
static void vcap_key_bit_set(const struct vcap_props *vcap,
			     struct vcap_data *data, int field,
308 309
			     enum ocelot_vcap_bit val)
{
310 311
	u32 value = (val == OCELOT_VCAP_BIT_1 ? 1 : 0);
	u32 msk = (val == OCELOT_VCAP_BIT_ANY ? 0 : 1);
312 313
	u32 offset = vcap->keys[field].offset;
	u32 length = vcap->keys[field].length;
314

315
	WARN_ON(length != 1);
316

317
	vcap_key_field_set(data, offset, length, value, msk);
318 319
}

320 321
static void vcap_action_set(const struct vcap_props *vcap,
			    struct vcap_data *data, int field, u32 value)
322
{
323 324
	int offset = vcap->actions[field].offset;
	int length = vcap->actions[field].length;
325 326 327 328

	vcap_data_set(data->action, offset + data->action_offset, length,
		      value);
}
329

330
static void is2_action_set(struct ocelot *ocelot, struct vcap_data *data,
331
			   struct ocelot_vcap_filter *filter)
332
{
333
	const struct vcap_props *vcap = &ocelot->vcap[VCAP_IS2];
334 335 336 337 338 339 340 341
	struct ocelot_vcap_action *a = &filter->action;

	vcap_action_set(vcap, data, VCAP_IS2_ACT_MASK_MODE, a->mask_mode);
	vcap_action_set(vcap, data, VCAP_IS2_ACT_PORT_MASK, a->port_mask);
	vcap_action_set(vcap, data, VCAP_IS2_ACT_POLICE_ENA, a->police_ena);
	vcap_action_set(vcap, data, VCAP_IS2_ACT_POLICE_IDX, a->pol_ix);
	vcap_action_set(vcap, data, VCAP_IS2_ACT_CPU_QU_NUM, a->cpu_qu_num);
	vcap_action_set(vcap, data, VCAP_IS2_ACT_CPU_COPY_ENA, a->cpu_copy_ena);
342 343 344
}

static void is2_entry_set(struct ocelot *ocelot, int ix,
345
			  struct ocelot_vcap_filter *filter)
346
{
347
	const struct vcap_props *vcap = &ocelot->vcap[VCAP_IS2];
348
	struct ocelot_vcap_key_vlan *tag = &filter->vlan;
349
	u32 val, msk, type, type_mask = 0xf, i, count;
350 351
	struct ocelot_vcap_u64 payload;
	struct vcap_data data;
352 353
	int row = (ix / 2);

354 355 356
	memset(&payload, 0, sizeof(payload));
	memset(&data, 0, sizeof(data));

357
	/* Read row */
358 359 360
	vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_READ, VCAP_SEL_ALL);
	vcap_cache2entry(ocelot, vcap, &data);
	vcap_cache2action(ocelot, vcap, &data);
361 362

	data.tg_sw = VCAP_TG_HALF;
363
	vcap_data_offset_get(vcap, &data, ix);
364
	data.tg = (data.tg & ~data.tg_mask);
365
	if (filter->prio != 0)
366 367 368 369
		data.tg |= data.tg_value;

	data.type = IS2_ACTION_TYPE_NORMAL;

370 371 372 373
	vcap_key_set(vcap, &data, VCAP_IS2_HK_PAG, filter->pag, 0xff);
	vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_FIRST,
			 (filter->lookup == 0) ? OCELOT_VCAP_BIT_1 :
			 OCELOT_VCAP_BIT_0);
374
	vcap_key_set(vcap, &data, VCAP_IS2_HK_IGR_PORT_MASK, 0,
375
		     ~filter->ingress_port_mask);
376 377
	vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_FIRST, OCELOT_VCAP_BIT_ANY);
	vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_HOST_MATCH,
378
			 OCELOT_VCAP_BIT_ANY);
379 380 381 382
	vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L2_MC, filter->dmac_mc);
	vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L2_BC, filter->dmac_bc);
	vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_VLAN_TAGGED, tag->tagged);
	vcap_key_set(vcap, &data, VCAP_IS2_HK_VID,
383
		     tag->vid.value, tag->vid.mask);
384
	vcap_key_set(vcap, &data, VCAP_IS2_HK_PCP,
385
		     tag->pcp.value[0], tag->pcp.mask[0]);
386
	vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_DEI, tag->dei);
387

388 389 390
	switch (filter->key_type) {
	case OCELOT_VCAP_KEY_ETYPE: {
		struct ocelot_vcap_key_etype *etype = &filter->key.etype;
391 392

		type = IS2_TYPE_ETYPE;
393
		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_DMAC,
394
				   etype->dmac.value, etype->dmac.mask);
395
		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_SMAC,
396
				   etype->smac.value, etype->smac.mask);
397
		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_MAC_ETYPE_ETYPE,
398 399
				   etype->etype.value, etype->etype.mask);
		/* Clear unused bits */
400
		vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD0,
401
			     0, 0);
402
		vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD1,
403
			     0, 0);
404
		vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD2,
405
			     0, 0);
406
		vcap_key_bytes_set(vcap, &data,
407 408
				   VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD0,
				   etype->data.value, etype->data.mask);
409 410
		break;
	}
411 412
	case OCELOT_VCAP_KEY_LLC: {
		struct ocelot_vcap_key_llc *llc = &filter->key.llc;
413 414

		type = IS2_TYPE_LLC;
415
		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_DMAC,
416
				   llc->dmac.value, llc->dmac.mask);
417
		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_SMAC,
418
				   llc->smac.value, llc->smac.mask);
419 420 421 422
		for (i = 0; i < 4; i++) {
			payload.value[i] = llc->llc.value[i];
			payload.mask[i] = llc->llc.mask[i];
		}
423
		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_MAC_LLC_L2_LLC,
424
				   payload.value, payload.mask);
425 426
		break;
	}
427 428
	case OCELOT_VCAP_KEY_SNAP: {
		struct ocelot_vcap_key_snap *snap = &filter->key.snap;
429 430

		type = IS2_TYPE_SNAP;
431
		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_DMAC,
432
				   snap->dmac.value, snap->dmac.mask);
433
		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_SMAC,
434
				   snap->smac.value, snap->smac.mask);
435
		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_MAC_SNAP_L2_SNAP,
436 437
				   filter->key.snap.snap.value,
				   filter->key.snap.snap.mask);
438 439
		break;
	}
440 441
	case OCELOT_VCAP_KEY_ARP: {
		struct ocelot_vcap_key_arp *arp = &filter->key.arp;
442 443

		type = IS2_TYPE_ARP;
444
		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_MAC_ARP_SMAC,
445
				   arp->smac.value, arp->smac.mask);
446
		vcap_key_bit_set(vcap, &data,
447 448
				 VCAP_IS2_HK_MAC_ARP_ADDR_SPACE_OK,
				 arp->ethernet);
449
		vcap_key_bit_set(vcap, &data,
450 451
				 VCAP_IS2_HK_MAC_ARP_PROTO_SPACE_OK,
				 arp->ip);
452
		vcap_key_bit_set(vcap, &data,
453 454
				 VCAP_IS2_HK_MAC_ARP_LEN_OK,
				 arp->length);
455
		vcap_key_bit_set(vcap, &data,
456 457
				 VCAP_IS2_HK_MAC_ARP_TARGET_MATCH,
				 arp->dmac_match);
458
		vcap_key_bit_set(vcap, &data,
459 460
				 VCAP_IS2_HK_MAC_ARP_SENDER_MATCH,
				 arp->smac_match);
461
		vcap_key_bit_set(vcap, &data,
462 463
				 VCAP_IS2_HK_MAC_ARP_OPCODE_UNKNOWN,
				 arp->unknown);
464 465 466 467 468 469

		/* OPCODE is inverse, bit 0 is reply flag, bit 1 is RARP flag */
		val = ((arp->req == OCELOT_VCAP_BIT_0 ? 1 : 0) |
		       (arp->arp == OCELOT_VCAP_BIT_0 ? 2 : 0));
		msk = ((arp->req == OCELOT_VCAP_BIT_ANY ? 0 : 1) |
		       (arp->arp == OCELOT_VCAP_BIT_ANY ? 0 : 2));
470
		vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ARP_OPCODE,
471
			     val, msk);
472
		vcap_key_bytes_set(vcap, &data,
473 474
				   VCAP_IS2_HK_MAC_ARP_L3_IP4_DIP,
				   arp->dip.value.addr, arp->dip.mask.addr);
475
		vcap_key_bytes_set(vcap, &data,
476 477
				   VCAP_IS2_HK_MAC_ARP_L3_IP4_SIP,
				   arp->sip.value.addr, arp->sip.mask.addr);
478
		vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ARP_DIP_EQ_SIP,
479
			     0, 0);
480 481
		break;
	}
482 483
	case OCELOT_VCAP_KEY_IPV4:
	case OCELOT_VCAP_KEY_IPV6: {
484 485 486
		enum ocelot_vcap_bit sip_eq_dip, sport_eq_dport, seq_zero, tcp;
		enum ocelot_vcap_bit ttl, fragment, options, tcp_ack, tcp_urg;
		enum ocelot_vcap_bit tcp_fin, tcp_syn, tcp_rst, tcp_psh;
487 488
		struct ocelot_vcap_key_ipv4 *ipv4 = NULL;
		struct ocelot_vcap_key_ipv6 *ipv6 = NULL;
489 490 491 492 493
		struct ocelot_vcap_udp_tcp *sport, *dport;
		struct ocelot_vcap_ipv4 sip, dip;
		struct ocelot_vcap_u8 proto, ds;
		struct ocelot_vcap_u48 *ip_data;

494 495
		if (filter->key_type == OCELOT_VCAP_KEY_IPV4) {
			ipv4 = &filter->key.ipv4;
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
			ttl = ipv4->ttl;
			fragment = ipv4->fragment;
			options = ipv4->options;
			proto = ipv4->proto;
			ds = ipv4->ds;
			ip_data = &ipv4->data;
			sip = ipv4->sip;
			dip = ipv4->dip;
			sport = &ipv4->sport;
			dport = &ipv4->dport;
			tcp_fin = ipv4->tcp_fin;
			tcp_syn = ipv4->tcp_syn;
			tcp_rst = ipv4->tcp_rst;
			tcp_psh = ipv4->tcp_psh;
			tcp_ack = ipv4->tcp_ack;
			tcp_urg = ipv4->tcp_urg;
			sip_eq_dip = ipv4->sip_eq_dip;
			sport_eq_dport = ipv4->sport_eq_dport;
			seq_zero = ipv4->seq_zero;
		} else {
516
			ipv6 = &filter->key.ipv6;
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
			ttl = ipv6->ttl;
			fragment = OCELOT_VCAP_BIT_ANY;
			options = OCELOT_VCAP_BIT_ANY;
			proto = ipv6->proto;
			ds = ipv6->ds;
			ip_data = &ipv6->data;
			for (i = 0; i < 8; i++) {
				val = ipv6->sip.value[i + 8];
				msk = ipv6->sip.mask[i + 8];
				if (i < 4) {
					dip.value.addr[i] = val;
					dip.mask.addr[i] = msk;
				} else {
					sip.value.addr[i - 4] = val;
					sip.mask.addr[i - 4] = msk;
				}
			}
			sport = &ipv6->sport;
			dport = &ipv6->dport;
			tcp_fin = ipv6->tcp_fin;
			tcp_syn = ipv6->tcp_syn;
			tcp_rst = ipv6->tcp_rst;
			tcp_psh = ipv6->tcp_psh;
			tcp_ack = ipv6->tcp_ack;
			tcp_urg = ipv6->tcp_urg;
			sip_eq_dip = ipv6->sip_eq_dip;
			sport_eq_dport = ipv6->sport_eq_dport;
			seq_zero = ipv6->seq_zero;
		}

547
		vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_IP4,
548
				 ipv4 ? OCELOT_VCAP_BIT_1 : OCELOT_VCAP_BIT_0);
549
		vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L3_FRAGMENT,
550
				 fragment);
551 552
		vcap_key_set(vcap, &data, VCAP_IS2_HK_L3_FRAG_OFS_GT0, 0, 0);
		vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L3_OPTIONS,
553
				 options);
554
		vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_IP4_L3_TTL_GT0,
555
				 ttl);
556
		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L3_TOS,
557
				   ds.value, ds.mask);
558
		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L3_IP4_DIP,
559
				   dip.value.addr, dip.mask.addr);
560
		vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L3_IP4_SIP,
561
				   sip.value.addr, sip.mask.addr);
562
		vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_DIP_EQ_SIP,
563
				 sip_eq_dip);
564 565 566 567 568 569 570
		val = proto.value[0];
		msk = proto.mask[0];
		type = IS2_TYPE_IP_UDP_TCP;
		if (msk == 0xff && (val == 6 || val == 17)) {
			/* UDP/TCP protocol match */
			tcp = (val == 6 ?
			       OCELOT_VCAP_BIT_1 : OCELOT_VCAP_BIT_0);
571 572
			vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_TCP, tcp);
			vcap_key_l4_port_set(vcap, &data,
573
					     VCAP_IS2_HK_L4_DPORT, dport);
574
			vcap_key_l4_port_set(vcap, &data,
575
					     VCAP_IS2_HK_L4_SPORT, sport);
576 577
			vcap_key_set(vcap, &data, VCAP_IS2_HK_L4_RNG, 0, 0);
			vcap_key_bit_set(vcap, &data,
578
					 VCAP_IS2_HK_L4_SPORT_EQ_DPORT,
579
					 sport_eq_dport);
580
			vcap_key_bit_set(vcap, &data,
581 582
					 VCAP_IS2_HK_L4_SEQUENCE_EQ0,
					 seq_zero);
583
			vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_FIN,
584
					 tcp_fin);
585
			vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_SYN,
586
					 tcp_syn);
587
			vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_RST,
588
					 tcp_rst);
589
			vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_PSH,
590
					 tcp_psh);
591
			vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_ACK,
592
					 tcp_ack);
593
			vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_URG,
594
					 tcp_urg);
595
			vcap_key_set(vcap, &data, VCAP_IS2_HK_L4_1588_DOM,
596
				     0, 0);
597
			vcap_key_set(vcap, &data, VCAP_IS2_HK_L4_1588_VER,
598
				     0, 0);
599 600 601 602 603 604 605 606 607 608 609 610
		} else {
			if (msk == 0) {
				/* Any IP protocol match */
				type_mask = IS2_TYPE_MASK_IP_ANY;
			} else {
				/* Non-UDP/TCP protocol match */
				type = IS2_TYPE_IP_OTHER;
				for (i = 0; i < 6; i++) {
					payload.value[i] = ip_data->value[i];
					payload.mask[i] = ip_data->mask[i];
				}
			}
611
			vcap_key_bytes_set(vcap, &data,
612 613
					   VCAP_IS2_HK_IP4_L3_PROTO,
					   proto.value, proto.mask);
614
			vcap_key_bytes_set(vcap, &data,
615 616
					   VCAP_IS2_HK_L3_PAYLOAD,
					   payload.value, payload.mask);
617 618 619
		}
		break;
	}
620
	case OCELOT_VCAP_KEY_ANY:
621 622 623
	default:
		type = 0;
		type_mask = 0;
624
		count = vcap->entry_width / 2;
625 626 627
		/* Iterate over the non-common part of the key and
		 * clear entry data
		 */
628
		for (i = vcap->keys[VCAP_IS2_HK_L2_DMAC].offset;
629 630
		     i < count; i += ENTRY_WIDTH) {
			vcap_key_field_set(&data, i, min(32u, count - i), 0, 0);
631 632 633 634
		}
		break;
	}

635
	vcap_key_set(vcap, &data, VCAP_IS2_TYPE, type, type_mask);
636
	is2_action_set(ocelot, &data, filter);
637
	vcap_data_set(data.counter, data.counter_offset,
638
		      vcap->counter_width, filter->stats.pkts);
639 640

	/* Write row */
641 642 643
	vcap_entry2cache(ocelot, vcap, &data);
	vcap_action2cache(ocelot, vcap, &data);
	vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_WRITE, VCAP_SEL_ALL);
644 645
}

646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
static void is1_action_set(struct ocelot *ocelot, struct vcap_data *data,
			   const struct ocelot_vcap_filter *filter)
{
	const struct vcap_props *vcap = &ocelot->vcap[VCAP_IS1];
	const struct ocelot_vcap_action *a = &filter->action;

	vcap_action_set(vcap, data, VCAP_IS1_ACT_VID_REPLACE_ENA,
			a->vid_replace_ena);
	vcap_action_set(vcap, data, VCAP_IS1_ACT_VID_ADD_VAL, a->vid);
	vcap_action_set(vcap, data, VCAP_IS1_ACT_VLAN_POP_CNT_ENA,
			a->vlan_pop_cnt_ena);
	vcap_action_set(vcap, data, VCAP_IS1_ACT_VLAN_POP_CNT,
			a->vlan_pop_cnt);
	vcap_action_set(vcap, data, VCAP_IS1_ACT_PCP_DEI_ENA, a->pcp_dei_ena);
	vcap_action_set(vcap, data, VCAP_IS1_ACT_PCP_VAL, a->pcp);
	vcap_action_set(vcap, data, VCAP_IS1_ACT_DEI_VAL, a->dei);
	vcap_action_set(vcap, data, VCAP_IS1_ACT_QOS_ENA, a->qos_ena);
	vcap_action_set(vcap, data, VCAP_IS1_ACT_QOS_VAL, a->qos_val);
	vcap_action_set(vcap, data, VCAP_IS1_ACT_PAG_OVERRIDE_MASK,
			a->pag_override_mask);
	vcap_action_set(vcap, data, VCAP_IS1_ACT_PAG_VAL, a->pag_val);
}

static void is1_entry_set(struct ocelot *ocelot, int ix,
			  struct ocelot_vcap_filter *filter)
{
	const struct vcap_props *vcap = &ocelot->vcap[VCAP_IS1];
	struct ocelot_vcap_key_vlan *tag = &filter->vlan;
	struct ocelot_vcap_u64 payload;
	struct vcap_data data;
	int row = ix / 2;
	u32 type;

	memset(&payload, 0, sizeof(payload));
	memset(&data, 0, sizeof(data));

	/* Read row */
	vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_READ, VCAP_SEL_ALL);
	vcap_cache2entry(ocelot, vcap, &data);
	vcap_cache2action(ocelot, vcap, &data);

	data.tg_sw = VCAP_TG_HALF;
	data.type = IS1_ACTION_TYPE_NORMAL;
	vcap_data_offset_get(vcap, &data, ix);
	data.tg = (data.tg & ~data.tg_mask);
	if (filter->prio != 0)
		data.tg |= data.tg_value;

694
	vcap_key_set(vcap, &data, VCAP_IS1_HK_LOOKUP, filter->lookup, 0x3);
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
	vcap_key_set(vcap, &data, VCAP_IS1_HK_IGR_PORT_MASK, 0,
		     ~filter->ingress_port_mask);
	vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_L2_MC, filter->dmac_mc);
	vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_L2_BC, filter->dmac_bc);
	vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_VLAN_TAGGED, tag->tagged);
	vcap_key_set(vcap, &data, VCAP_IS1_HK_VID,
		     tag->vid.value, tag->vid.mask);
	vcap_key_set(vcap, &data, VCAP_IS1_HK_PCP,
		     tag->pcp.value[0], tag->pcp.mask[0]);
	type = IS1_TYPE_S1_NORMAL;

	switch (filter->key_type) {
	case OCELOT_VCAP_KEY_ETYPE: {
		struct ocelot_vcap_key_etype *etype = &filter->key.etype;

		vcap_key_bytes_set(vcap, &data, VCAP_IS1_HK_L2_SMAC,
				   etype->smac.value, etype->smac.mask);
		vcap_key_bytes_set(vcap, &data, VCAP_IS1_HK_ETYPE,
				   etype->etype.value, etype->etype.mask);
		break;
	}
	case OCELOT_VCAP_KEY_IPV4: {
		struct ocelot_vcap_key_ipv4 *ipv4 = &filter->key.ipv4;
		struct ocelot_vcap_udp_tcp *sport = &ipv4->sport;
		struct ocelot_vcap_udp_tcp *dport = &ipv4->dport;
		enum ocelot_vcap_bit tcp_udp = OCELOT_VCAP_BIT_0;
		struct ocelot_vcap_u8 proto = ipv4->proto;
		struct ocelot_vcap_ipv4 sip = ipv4->sip;
		u32 val, msk;

		vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_IP_SNAP,
				 OCELOT_VCAP_BIT_1);
		vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_IP4,
				 OCELOT_VCAP_BIT_1);
		vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_ETYPE_LEN,
				 OCELOT_VCAP_BIT_1);
		vcap_key_bytes_set(vcap, &data, VCAP_IS1_HK_L3_IP4_SIP,
				   sip.value.addr, sip.mask.addr);

		val = proto.value[0];
		msk = proto.mask[0];

		if ((val == NEXTHDR_TCP || val == NEXTHDR_UDP) && msk == 0xff)
			tcp_udp = OCELOT_VCAP_BIT_1;
		vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_TCP_UDP, tcp_udp);

		if (tcp_udp) {
			enum ocelot_vcap_bit tcp = OCELOT_VCAP_BIT_0;

			if (val == NEXTHDR_TCP)
				tcp = OCELOT_VCAP_BIT_1;

			vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_TCP, tcp);
			vcap_key_l4_port_set(vcap, &data, VCAP_IS1_HK_L4_SPORT,
					     sport);
			/* Overloaded field */
			vcap_key_l4_port_set(vcap, &data, VCAP_IS1_HK_ETYPE,
					     dport);
		} else {
			/* IPv4 "other" frame */
			struct ocelot_vcap_u16 etype = {0};

			/* Overloaded field */
			etype.value[0] = proto.value[0];
			etype.mask[0] = proto.mask[0];

			vcap_key_bytes_set(vcap, &data, VCAP_IS1_HK_ETYPE,
					   etype.value, etype.mask);
		}
	}
	default:
		break;
	}
	vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_TYPE,
			 type ? OCELOT_VCAP_BIT_1 : OCELOT_VCAP_BIT_0);

	is1_action_set(ocelot, &data, filter);
	vcap_data_set(data.counter, data.counter_offset,
		      vcap->counter_width, filter->stats.pkts);

	/* Write row */
	vcap_entry2cache(ocelot, vcap, &data);
	vcap_action2cache(ocelot, vcap, &data);
	vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_WRITE, VCAP_SEL_ALL);
}

781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
static void es0_action_set(struct ocelot *ocelot, struct vcap_data *data,
			   const struct ocelot_vcap_filter *filter)
{
	const struct vcap_props *vcap = &ocelot->vcap[VCAP_ES0];
	const struct ocelot_vcap_action *a = &filter->action;

	vcap_action_set(vcap, data, VCAP_ES0_ACT_PUSH_OUTER_TAG,
			a->push_outer_tag);
	vcap_action_set(vcap, data, VCAP_ES0_ACT_PUSH_INNER_TAG,
			a->push_inner_tag);
	vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_A_TPID_SEL,
			a->tag_a_tpid_sel);
	vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_A_VID_SEL,
			a->tag_a_vid_sel);
	vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_A_PCP_SEL,
			a->tag_a_pcp_sel);
	vcap_action_set(vcap, data, VCAP_ES0_ACT_VID_A_VAL, a->vid_a_val);
	vcap_action_set(vcap, data, VCAP_ES0_ACT_PCP_A_VAL, a->pcp_a_val);
	vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_B_TPID_SEL,
			a->tag_b_tpid_sel);
	vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_B_VID_SEL,
			a->tag_b_vid_sel);
	vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_B_PCP_SEL,
			a->tag_b_pcp_sel);
	vcap_action_set(vcap, data, VCAP_ES0_ACT_VID_B_VAL, a->vid_b_val);
	vcap_action_set(vcap, data, VCAP_ES0_ACT_PCP_B_VAL, a->pcp_b_val);
}

static void es0_entry_set(struct ocelot *ocelot, int ix,
			  struct ocelot_vcap_filter *filter)
{
	const struct vcap_props *vcap = &ocelot->vcap[VCAP_ES0];
	struct ocelot_vcap_key_vlan *tag = &filter->vlan;
	struct ocelot_vcap_u64 payload;
	struct vcap_data data;
	int row = ix;

	memset(&payload, 0, sizeof(payload));
	memset(&data, 0, sizeof(data));

	/* Read row */
	vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_READ, VCAP_SEL_ALL);
	vcap_cache2entry(ocelot, vcap, &data);
	vcap_cache2action(ocelot, vcap, &data);

	data.tg_sw = VCAP_TG_FULL;
	data.type = ES0_ACTION_TYPE_NORMAL;
	vcap_data_offset_get(vcap, &data, ix);
	data.tg = (data.tg & ~data.tg_mask);
	if (filter->prio != 0)
		data.tg |= data.tg_value;

	vcap_key_set(vcap, &data, VCAP_ES0_IGR_PORT, filter->ingress_port.value,
		     filter->ingress_port.mask);
	vcap_key_set(vcap, &data, VCAP_ES0_EGR_PORT, filter->egress_port.value,
		     filter->egress_port.mask);
	vcap_key_bit_set(vcap, &data, VCAP_ES0_L2_MC, filter->dmac_mc);
	vcap_key_bit_set(vcap, &data, VCAP_ES0_L2_BC, filter->dmac_bc);
	vcap_key_set(vcap, &data, VCAP_ES0_VID,
		     tag->vid.value, tag->vid.mask);
	vcap_key_set(vcap, &data, VCAP_ES0_PCP,
		     tag->pcp.value[0], tag->pcp.mask[0]);

	es0_action_set(ocelot, &data, filter);
	vcap_data_set(data.counter, data.counter_offset,
		      vcap->counter_width, filter->stats.pkts);

	/* Write row */
	vcap_entry2cache(ocelot, vcap, &data);
	vcap_action2cache(ocelot, vcap, &data);
	vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_WRITE, VCAP_SEL_ALL);
}

854 855
static void vcap_entry_get(struct ocelot *ocelot, int ix,
			   struct ocelot_vcap_filter *filter)
856
{
857
	const struct vcap_props *vcap = &ocelot->vcap[filter->block_id];
858
	struct vcap_data data;
859
	int row, count;
860 861
	u32 cnt;

862 863 864 865 866
	if (filter->block_id == VCAP_ES0)
		data.tg_sw = VCAP_TG_FULL;
	else
		data.tg_sw = VCAP_TG_HALF;

867 868 869 870 871
	count = (1 << (data.tg_sw - 1));
	row = (ix / count);
	vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_READ, VCAP_SEL_COUNTER);
	vcap_cache2action(ocelot, vcap, &data);
	vcap_data_offset_get(vcap, &data, ix);
872
	cnt = vcap_data_get(data.counter, data.counter_offset,
873
			    vcap->counter_width);
874

875
	filter->stats.pkts = cnt;
876 877
}

878 879 880
static void vcap_entry_set(struct ocelot *ocelot, int ix,
			   struct ocelot_vcap_filter *filter)
{
881 882
	if (filter->block_id == VCAP_IS1)
		return is1_entry_set(ocelot, ix, filter);
883 884
	if (filter->block_id == VCAP_IS2)
		return is2_entry_set(ocelot, ix, filter);
885 886
	if (filter->block_id == VCAP_ES0)
		return es0_entry_set(ocelot, ix, filter);
887 888
}

889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
static int ocelot_vcap_policer_add(struct ocelot *ocelot, u32 pol_ix,
				   struct ocelot_policer *pol)
{
	struct qos_policer_conf pp = { 0 };

	if (!pol)
		return -EINVAL;

	pp.mode = MSCC_QOS_RATE_MODE_DATA;
	pp.pir = pol->rate;
	pp.pbs = pol->burst;

	return qos_policer_conf_set(ocelot, 0, pol_ix, &pp);
}

static void ocelot_vcap_policer_del(struct ocelot *ocelot,
				    struct ocelot_vcap_block *block,
				    u32 pol_ix)
{
	struct ocelot_vcap_filter *filter;
	struct qos_policer_conf pp = {0};
	int index = -1;

	if (pol_ix < block->pol_lpr)
		return;

	list_for_each_entry(filter, &block->rules, list) {
		index++;
917 918
		if (filter->block_id == VCAP_IS2 &&
		    filter->action.police_ena &&
919 920 921 922
		    filter->action.pol_ix < pol_ix) {
			filter->action.pol_ix += 1;
			ocelot_vcap_policer_add(ocelot, filter->action.pol_ix,
						&filter->action.pol);
923 924 925 926 927 928 929 930 931 932
			is2_entry_set(ocelot, index, filter);
		}
	}

	pp.mode = MSCC_QOS_RATE_MODE_DISABLED;
	qos_policer_conf_set(ocelot, 0, pol_ix, &pp);

	block->pol_lpr++;
}

933 934 935
static void ocelot_vcap_filter_add_to_block(struct ocelot *ocelot,
					    struct ocelot_vcap_block *block,
					    struct ocelot_vcap_filter *filter)
936
{
937
	struct ocelot_vcap_filter *tmp;
938 939
	struct list_head *pos, *n;

940
	if (filter->block_id == VCAP_IS2 && filter->action.police_ena) {
941
		block->pol_lpr--;
942 943 944
		filter->action.pol_ix = block->pol_lpr;
		ocelot_vcap_policer_add(ocelot, filter->action.pol_ix,
					&filter->action.pol);
945 946
	}

947 948 949
	block->count++;

	if (list_empty(&block->rules)) {
950
		list_add(&filter->list, &block->rules);
951 952 953 954
		return;
	}

	list_for_each_safe(pos, n, &block->rules) {
955 956
		tmp = list_entry(pos, struct ocelot_vcap_filter, list);
		if (filter->prio < tmp->prio)
957 958
			break;
	}
959
	list_add(&filter->list, pos->prev);
960 961
}

962 963
static int ocelot_vcap_block_get_filter_index(struct ocelot_vcap_block *block,
					      struct ocelot_vcap_filter *filter)
964
{
965
	struct ocelot_vcap_filter *tmp;
966
	int index = 0;
967 968

	list_for_each_entry(tmp, &block->rules, list) {
969
		if (filter->id == tmp->id)
970 971
			return index;
		index++;
972
	}
973 974

	return -ENOENT;
975 976
}

977
static struct ocelot_vcap_filter*
978 979
ocelot_vcap_block_find_filter_by_index(struct ocelot_vcap_block *block,
				       int index)
980
{
981
	struct ocelot_vcap_filter *tmp;
982 983 984 985 986 987 988 989 990 991 992
	int i = 0;

	list_for_each_entry(tmp, &block->rules, list) {
		if (i == index)
			return tmp;
		++i;
	}

	return NULL;
}

993 994 995 996 997 998 999 1000 1001 1002 1003 1004
struct ocelot_vcap_filter *
ocelot_vcap_block_find_filter_by_id(struct ocelot_vcap_block *block, int id)
{
	struct ocelot_vcap_filter *filter;

	list_for_each_entry(filter, &block->rules, list)
		if (filter->id == id)
			return filter;

	return NULL;
}

1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
/* If @on=false, then SNAP, ARP, IP and OAM frames will not match on keys based
 * on destination and source MAC addresses, but only on higher-level protocol
 * information. The only frame types to match on keys containing MAC addresses
 * in this case are non-SNAP, non-ARP, non-IP and non-OAM frames.
 *
 * If @on=true, then the above frame types (SNAP, ARP, IP and OAM) will match
 * on MAC_ETYPE keys such as destination and source MAC on this ingress port.
 * However the setting has the side effect of making these frames not matching
 * on any _other_ keys than MAC_ETYPE ones.
 */
static void ocelot_match_all_as_mac_etype(struct ocelot *ocelot, int port,
					  bool on)
{
	u32 val = 0;

	if (on)
		val = ANA_PORT_VCAP_S2_CFG_S2_SNAP_DIS(3) |
		      ANA_PORT_VCAP_S2_CFG_S2_ARP_DIS(3) |
		      ANA_PORT_VCAP_S2_CFG_S2_IP_TCPUDP_DIS(3) |
		      ANA_PORT_VCAP_S2_CFG_S2_IP_OTHER_DIS(3) |
		      ANA_PORT_VCAP_S2_CFG_S2_OAM_DIS(3);

	ocelot_rmw_gix(ocelot, val,
		       ANA_PORT_VCAP_S2_CFG_S2_SNAP_DIS_M |
		       ANA_PORT_VCAP_S2_CFG_S2_ARP_DIS_M |
		       ANA_PORT_VCAP_S2_CFG_S2_IP_TCPUDP_DIS_M |
		       ANA_PORT_VCAP_S2_CFG_S2_IP_OTHER_DIS_M |
		       ANA_PORT_VCAP_S2_CFG_S2_OAM_DIS_M,
		       ANA_PORT_VCAP_S2_CFG, port);
}

1036 1037
static bool
ocelot_vcap_is_problematic_mac_etype(struct ocelot_vcap_filter *filter)
1038
{
1039 1040
	u16 proto, mask;

1041
	if (filter->key_type != OCELOT_VCAP_KEY_ETYPE)
1042
		return false;
1043

1044 1045
	proto = ntohs(*(__be16 *)filter->key.etype.etype.value);
	mask = ntohs(*(__be16 *)filter->key.etype.etype.mask);
1046 1047 1048

	/* ETH_P_ALL match, so all protocols below are included */
	if (mask == 0)
1049
		return true;
1050
	if (proto == ETH_P_ARP)
1051
		return true;
1052 1053 1054 1055 1056
	if (proto == ETH_P_IP)
		return true;
	if (proto == ETH_P_IPV6)
		return true;

1057 1058 1059
	return false;
}

1060 1061
static bool
ocelot_vcap_is_problematic_non_mac_etype(struct ocelot_vcap_filter *filter)
1062
{
1063
	if (filter->key_type == OCELOT_VCAP_KEY_SNAP)
1064
		return true;
1065
	if (filter->key_type == OCELOT_VCAP_KEY_ARP)
1066
		return true;
1067
	if (filter->key_type == OCELOT_VCAP_KEY_IPV4)
1068
		return true;
1069
	if (filter->key_type == OCELOT_VCAP_KEY_IPV6)
1070 1071 1072 1073
		return true;
	return false;
}

1074 1075 1076
static bool
ocelot_exclusive_mac_etype_filter_rules(struct ocelot *ocelot,
					struct ocelot_vcap_filter *filter)
1077
{
1078
	struct ocelot_vcap_block *block = &ocelot->block[filter->block_id];
1079
	struct ocelot_vcap_filter *tmp;
1080 1081 1082
	unsigned long port;
	int i;

1083
	if (ocelot_vcap_is_problematic_mac_etype(filter)) {
1084 1085
		/* Search for any non-MAC_ETYPE rules on the port */
		for (i = 0; i < block->count; i++) {
1086
			tmp = ocelot_vcap_block_find_filter_by_index(block, i);
1087 1088
			if (tmp->ingress_port_mask & filter->ingress_port_mask &&
			    ocelot_vcap_is_problematic_non_mac_etype(tmp))
1089 1090 1091
				return false;
		}

1092
		for_each_set_bit(port, &filter->ingress_port_mask,
1093 1094
				 ocelot->num_phys_ports)
			ocelot_match_all_as_mac_etype(ocelot, port, true);
1095
	} else if (ocelot_vcap_is_problematic_non_mac_etype(filter)) {
1096 1097
		/* Search for any MAC_ETYPE rules on the port */
		for (i = 0; i < block->count; i++) {
1098
			tmp = ocelot_vcap_block_find_filter_by_index(block, i);
1099 1100
			if (tmp->ingress_port_mask & filter->ingress_port_mask &&
			    ocelot_vcap_is_problematic_mac_etype(tmp))
1101 1102 1103
				return false;
		}

1104
		for_each_set_bit(port, &filter->ingress_port_mask,
1105 1106 1107 1108 1109 1110 1111
				 ocelot->num_phys_ports)
			ocelot_match_all_as_mac_etype(ocelot, port, false);
	}

	return true;
}

1112 1113 1114
int ocelot_vcap_filter_add(struct ocelot *ocelot,
			   struct ocelot_vcap_filter *filter,
			   struct netlink_ext_ack *extack)
1115
{
1116
	struct ocelot_vcap_block *block = &ocelot->block[filter->block_id];
1117 1118
	int i, index;

1119
	if (!ocelot_exclusive_mac_etype_filter_rules(ocelot, filter)) {
1120 1121 1122 1123 1124
		NL_SET_ERR_MSG_MOD(extack,
				   "Cannot mix MAC_ETYPE with non-MAC_ETYPE rules");
		return -EBUSY;
	}

1125 1126
	/* Add filter to the linked list */
	ocelot_vcap_filter_add_to_block(ocelot, block, filter);
1127

1128 1129
	/* Get the index of the inserted filter */
	index = ocelot_vcap_block_get_filter_index(block, filter);
1130 1131
	if (index < 0)
		return index;
1132

1133
	/* Move down the rules to make place for the new filter */
1134
	for (i = block->count - 1; i > index; i--) {
1135 1136
		struct ocelot_vcap_filter *tmp;

1137
		tmp = ocelot_vcap_block_find_filter_by_index(block, i);
1138
		vcap_entry_set(ocelot, i, tmp);
1139 1140
	}

1141
	/* Now insert the new filter */
1142
	vcap_entry_set(ocelot, index, filter);
1143 1144 1145
	return 0;
}

1146 1147 1148
static void ocelot_vcap_block_remove_filter(struct ocelot *ocelot,
					    struct ocelot_vcap_block *block,
					    struct ocelot_vcap_filter *filter)
1149
{
1150
	struct ocelot_vcap_filter *tmp;
1151 1152 1153
	struct list_head *pos, *q;

	list_for_each_safe(pos, q, &block->rules) {
1154 1155
		tmp = list_entry(pos, struct ocelot_vcap_filter, list);
		if (tmp->id == filter->id) {
1156 1157
			if (tmp->block_id == VCAP_IS2 &&
			    tmp->action.police_ena)
1158
				ocelot_vcap_policer_del(ocelot, block,
1159
							tmp->action.pol_ix);
1160

1161 1162 1163 1164 1165 1166 1167 1168
			list_del(pos);
			kfree(tmp);
		}
	}

	block->count--;
}

1169 1170
int ocelot_vcap_filter_del(struct ocelot *ocelot,
			   struct ocelot_vcap_filter *filter)
1171
{
1172
	struct ocelot_vcap_block *block = &ocelot->block[filter->block_id];
1173
	struct ocelot_vcap_filter del_filter;
1174 1175
	int i, index;

1176
	memset(&del_filter, 0, sizeof(del_filter));
1177

1178 1179
	/* Gets index of the filter */
	index = ocelot_vcap_block_get_filter_index(block, filter);
1180 1181
	if (index < 0)
		return index;
1182

1183 1184
	/* Delete filter */
	ocelot_vcap_block_remove_filter(ocelot, block, filter);
1185

1186
	/* Move up all the blocks over the deleted filter */
1187
	for (i = index; i < block->count; i++) {
1188 1189
		struct ocelot_vcap_filter *tmp;

1190
		tmp = ocelot_vcap_block_find_filter_by_index(block, i);
1191
		vcap_entry_set(ocelot, i, tmp);
1192 1193
	}

1194
	/* Now delete the last filter, because it is duplicated */
1195
	vcap_entry_set(ocelot, block->count, &del_filter);
1196 1197 1198 1199

	return 0;
}

1200 1201
int ocelot_vcap_filter_stats_update(struct ocelot *ocelot,
				    struct ocelot_vcap_filter *filter)
1202
{
1203
	struct ocelot_vcap_block *block = &ocelot->block[filter->block_id];
1204
	struct ocelot_vcap_filter tmp;
1205 1206
	int index;

1207
	index = ocelot_vcap_block_get_filter_index(block, filter);
1208 1209 1210
	if (index < 0)
		return index;

1211
	vcap_entry_get(ocelot, index, filter);
1212 1213

	/* After we get the result we need to clear the counters */
1214 1215
	tmp = *filter;
	tmp.stats.pkts = 0;
1216
	vcap_entry_set(ocelot, index, &tmp);
1217 1218 1219 1220

	return 0;
}

1221 1222
static void ocelot_vcap_init_one(struct ocelot *ocelot,
				 const struct vcap_props *vcap)
1223
{
1224
	struct vcap_data data;
1225

1226
	memset(&data, 0, sizeof(data));
1227

1228 1229 1230 1231
	vcap_entry2cache(ocelot, vcap, &data);
	ocelot_target_write(ocelot, vcap->target, vcap->entry_count,
			    VCAP_CORE_MV_CFG);
	vcap_cmd(ocelot, vcap, 0, VCAP_CMD_INITIALIZE, VCAP_SEL_ENTRY);
1232

1233 1234 1235 1236
	vcap_action2cache(ocelot, vcap, &data);
	ocelot_target_write(ocelot, vcap->target, vcap->action_count,
			    VCAP_CORE_MV_CFG);
	vcap_cmd(ocelot, vcap, 0, VCAP_CMD_INITIALIZE,
1237
		 VCAP_SEL_ACTION | VCAP_SEL_COUNTER);
1238 1239
}

1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
static void ocelot_vcap_detect_constants(struct ocelot *ocelot,
					 struct vcap_props *vcap)
{
	int counter_memory_width;
	int num_default_actions;
	int version;

	version = ocelot_target_read(ocelot, vcap->target,
				     VCAP_CONST_VCAP_VER);
	/* Only version 0 VCAP supported for now */
	if (WARN_ON(version != 0))
		return;

	/* Width in bits of type-group field */
	vcap->tg_width = ocelot_target_read(ocelot, vcap->target,
					    VCAP_CONST_ENTRY_TG_WIDTH);
	/* Number of subwords per TCAM row */
	vcap->sw_count = ocelot_target_read(ocelot, vcap->target,
					    VCAP_CONST_ENTRY_SWCNT);
	/* Number of rows in TCAM. There can be this many full keys, or double
	 * this number half keys, or 4 times this number quarter keys.
	 */
	vcap->entry_count = ocelot_target_read(ocelot, vcap->target,
					       VCAP_CONST_ENTRY_CNT);
	/* Assuming there are 4 subwords per TCAM row, their layout in the
	 * actual TCAM (not in the cache) would be:
	 *
	 * |  SW 3  | TG 3 |  SW 2  | TG 2 |  SW 1  | TG 1 |  SW 0  | TG 0 |
	 *
	 * (where SW=subword and TG=Type-Group).
	 *
	 * What VCAP_CONST_ENTRY_CNT is giving us is the width of one full TCAM
	 * row. But when software accesses the TCAM through the cache
	 * registers, the Type-Group values are written through another set of
	 * registers VCAP_TG_DAT, and therefore, it appears as though the 4
	 * subwords are contiguous in the cache memory.
	 * Important mention: regardless of the number of key entries per row
	 * (and therefore of key size: 1 full key or 2 half keys or 4 quarter
	 * keys), software always has to configure 4 Type-Group values. For
	 * example, in the case of 1 full key, the driver needs to set all 4
	 * Type-Group to be full key.
	 *
	 * For this reason, we need to fix up the value that the hardware is
	 * giving us. We don't actually care about the width of the entry in
	 * the TCAM. What we care about is the width of the entry in the cache
	 * registers, which is how we get to interact with it. And since the
	 * VCAP_ENTRY_DAT cache registers access only the subwords and not the
	 * Type-Groups, this means we need to subtract the width of the
	 * Type-Groups when packing and unpacking key entry data in a TCAM row.
	 */
	vcap->entry_width = ocelot_target_read(ocelot, vcap->target,
					       VCAP_CONST_ENTRY_WIDTH);
	vcap->entry_width -= vcap->tg_width * vcap->sw_count;
	num_default_actions = ocelot_target_read(ocelot, vcap->target,
						 VCAP_CONST_ACTION_DEF_CNT);
	vcap->action_count = vcap->entry_count + num_default_actions;
	vcap->action_width = ocelot_target_read(ocelot, vcap->target,
						VCAP_CONST_ACTION_WIDTH);
	/* The width of the counter memory, this is the complete width of all
	 * counter-fields associated with one full-word entry. There is one
	 * counter per entry sub-word (see CAP_CORE::ENTRY_SWCNT for number of
	 * subwords.)
	 */
	vcap->counter_words = vcap->sw_count;
	counter_memory_width = ocelot_target_read(ocelot, vcap->target,
						  VCAP_CONST_CNT_WIDTH);
	vcap->counter_width = counter_memory_width / vcap->counter_words;
}

1309 1310
int ocelot_vcap_init(struct ocelot *ocelot)
{
1311
	int i;
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327

	/* Create a policer that will drop the frames for the cpu.
	 * This policer will be used as action in the acl rules to drop
	 * frames.
	 */
	ocelot_write_gix(ocelot, 0x299, ANA_POL_MODE_CFG,
			 OCELOT_POLICER_DISCARD);
	ocelot_write_gix(ocelot, 0x1, ANA_POL_PIR_CFG,
			 OCELOT_POLICER_DISCARD);
	ocelot_write_gix(ocelot, 0x3fffff, ANA_POL_PIR_STATE,
			 OCELOT_POLICER_DISCARD);
	ocelot_write_gix(ocelot, 0x0, ANA_POL_CIR_CFG,
			 OCELOT_POLICER_DISCARD);
	ocelot_write_gix(ocelot, 0x3fffff, ANA_POL_CIR_STATE,
			 OCELOT_POLICER_DISCARD);

1328
	for (i = 0; i < OCELOT_NUM_VCAP_BLOCKS; i++) {
1329
		struct ocelot_vcap_block *block = &ocelot->block[i];
1330 1331
		struct vcap_props *vcap = &ocelot->vcap[i];

1332 1333 1334
		INIT_LIST_HEAD(&block->rules);
		block->pol_lpr = OCELOT_POLICER_DISCARD - 1;

1335 1336 1337 1338
		ocelot_vcap_detect_constants(ocelot, vcap);
		ocelot_vcap_init_one(ocelot, vcap);
	}

1339
	INIT_LIST_HEAD(&ocelot->dummy_rules);
1340 1341 1342

	return 0;
}