Linux Audio

Check our new training course

Loading...
v6.13.7
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Texas Instruments N-Port Ethernet Switch Address Lookup Engine
   4 *
   5 * Copyright (C) 2012 Texas Instruments
   6 *
   7 */
   8#include <linux/bitmap.h>
   9#include <linux/if_vlan.h>
  10#include <linux/kernel.h>
  11#include <linux/module.h>
  12#include <linux/platform_device.h>
  13#include <linux/regmap.h>
  14#include <linux/seq_file.h>
  15#include <linux/slab.h>
  16#include <linux/err.h>
  17#include <linux/io.h>
  18#include <linux/stat.h>
  19#include <linux/sysfs.h>
  20#include <linux/etherdevice.h>
  21
  22#include "cpsw_ale.h"
  23
  24#define BITMASK(bits)		(BIT(bits) - 1)
  25
  26#define ALE_VERSION_MAJOR(rev, mask) (((rev) >> 8) & (mask))
  27#define ALE_VERSION_MINOR(rev)	(rev & 0xff)
  28#define ALE_VERSION_1R3		0x0103
  29#define ALE_VERSION_1R4		0x0104
  30
  31/* ALE Registers */
  32#define ALE_IDVER		0x00
  33#define ALE_STATUS		0x04
  34#define ALE_CONTROL		0x08
  35#define ALE_PRESCALE		0x10
  36#define ALE_AGING_TIMER		0x14
  37#define ALE_UNKNOWNVLAN		0x18
  38#define ALE_TABLE_CONTROL	0x20
  39#define ALE_TABLE		0x34
  40#define ALE_PORTCTL		0x40
  41
  42/* ALE NetCP NU switch specific Registers */
  43#define ALE_UNKNOWNVLAN_MEMBER			0x90
  44#define ALE_UNKNOWNVLAN_UNREG_MCAST_FLOOD	0x94
  45#define ALE_UNKNOWNVLAN_REG_MCAST_FLOOD		0x98
  46#define ALE_UNKNOWNVLAN_FORCE_UNTAG_EGRESS	0x9C
  47#define ALE_VLAN_MASK_MUX(reg)			(0xc0 + (0x4 * (reg)))
  48
  49#define ALE_POLICER_PORT_OUI		0x100
  50#define ALE_POLICER_DA_SA		0x104
  51#define ALE_POLICER_VLAN		0x108
  52#define ALE_POLICER_ETHERTYPE_IPSA	0x10c
  53#define ALE_POLICER_IPDA		0x110
  54#define ALE_POLICER_PIR			0x118
  55#define ALE_POLICER_CIR			0x11c
  56#define ALE_POLICER_TBL_CTL		0x120
  57#define ALE_POLICER_CTL			0x124
  58#define ALE_POLICER_TEST_CTL		0x128
  59#define ALE_POLICER_HIT_STATUS		0x12c
  60#define ALE_THREAD_DEF			0x134
  61#define ALE_THREAD_CTL			0x138
  62#define ALE_THREAD_VAL			0x13c
  63
  64#define ALE_POLICER_TBL_WRITE_ENABLE	BIT(31)
  65#define ALE_POLICER_TBL_INDEX_MASK	GENMASK(4, 0)
  66
  67#define AM65_CPSW_ALE_THREAD_DEF_REG 0x134
  68
  69/* ALE_AGING_TIMER */
  70#define ALE_AGING_TIMER_MASK	GENMASK(23, 0)
  71
  72#define ALE_RATE_LIMIT_MIN_PPS 1000
  73
  74/**
  75 * struct ale_entry_fld - The ALE tbl entry field description
  76 * @start_bit: field start bit
  77 * @num_bits: field bit length
  78 * @flags: field flags
  79 */
  80struct ale_entry_fld {
  81	u8 start_bit;
  82	u8 num_bits;
  83	u8 flags;
  84};
  85
  86enum {
  87	CPSW_ALE_F_STATUS_REG = BIT(0), /* Status register present */
  88	CPSW_ALE_F_HW_AUTOAGING = BIT(1), /* HW auto aging */
  89
  90	CPSW_ALE_F_COUNT
  91};
  92
  93/**
  94 * struct cpsw_ale_dev_id - The ALE version/SoC specific configuration
  95 * @dev_id: ALE version/SoC id
  96 * @features: features supported by ALE
  97 * @tbl_entries: number of ALE entries
  98 * @reg_fields: pointer to array of register field configuration
  99 * @num_fields: number of fields in the reg_fields array
 100 * @nu_switch_ale: NU Switch ALE
 101 * @vlan_entry_tbl: ALE vlan entry fields description tbl
 102 */
 103struct cpsw_ale_dev_id {
 104	const char *dev_id;
 105	u32 features;
 106	u32 tbl_entries;
 107	const struct reg_field *reg_fields;
 108	int num_fields;
 109	bool nu_switch_ale;
 110	const struct ale_entry_fld *vlan_entry_tbl;
 111};
 112
 113#define ALE_TABLE_WRITE		BIT(31)
 114
 115#define ALE_TYPE_FREE			0
 116#define ALE_TYPE_ADDR			1
 117#define ALE_TYPE_VLAN			2
 118#define ALE_TYPE_VLAN_ADDR		3
 119
 120#define ALE_UCAST_PERSISTANT		0
 121#define ALE_UCAST_UNTOUCHED		1
 122#define ALE_UCAST_OUI			2
 123#define ALE_UCAST_TOUCHED		3
 124
 125#define ALE_TABLE_SIZE_MULTIPLIER	1024
 126#define ALE_POLICER_SIZE_MULTIPLIER	8
 127
 128static inline int cpsw_ale_get_field(u32 *ale_entry, u32 start, u32 bits)
 129{
 130	int idx, idx2, index;
 131	u32 hi_val = 0;
 132
 133	idx    = start / 32;
 134	idx2 = (start + bits - 1) / 32;
 135	/* Check if bits to be fetched exceed a word */
 136	if (idx != idx2) {
 137		index = 2 - idx2; /* flip */
 138		hi_val = ale_entry[index] << ((idx2 * 32) - start);
 139	}
 140	start -= idx * 32;
 141	idx    = 2 - idx; /* flip */
 142	return (hi_val + (ale_entry[idx] >> start)) & BITMASK(bits);
 143}
 144
 145static inline void cpsw_ale_set_field(u32 *ale_entry, u32 start, u32 bits,
 146				      u32 value)
 147{
 148	int idx, idx2, index;
 149
 150	value &= BITMASK(bits);
 151	idx = start / 32;
 152	idx2 = (start + bits - 1) / 32;
 153	/* Check if bits to be set exceed a word */
 154	if (idx != idx2) {
 155		index = 2 - idx2; /* flip */
 156		ale_entry[index] &= ~(BITMASK(bits + start - (idx2 * 32)));
 157		ale_entry[index] |= (value >> ((idx2 * 32) - start));
 158	}
 159	start -= idx * 32;
 160	idx = 2 - idx; /* flip */
 161	ale_entry[idx] &= ~(BITMASK(bits) << start);
 162	ale_entry[idx] |=  (value << start);
 163}
 164
 165#define DEFINE_ALE_FIELD_GET(name, start, bits)				\
 166static inline int cpsw_ale_get_##name(u32 *ale_entry)			\
 167{									\
 168	return cpsw_ale_get_field(ale_entry, start, bits);		\
 169}
 170
 171#define DEFINE_ALE_FIELD_SET(name, start, bits)				\
 172static inline void cpsw_ale_set_##name(u32 *ale_entry, u32 value)	\
 173{									\
 174	cpsw_ale_set_field(ale_entry, start, bits, value);		\
 175}
 176
 177#define DEFINE_ALE_FIELD(name, start, bits)				\
 178DEFINE_ALE_FIELD_GET(name, start, bits)					\
 179DEFINE_ALE_FIELD_SET(name, start, bits)
 180
 181#define DEFINE_ALE_FIELD1_GET(name, start)				\
 182static inline int cpsw_ale_get_##name(u32 *ale_entry, u32 bits)		\
 183{									\
 184	return cpsw_ale_get_field(ale_entry, start, bits);		\
 185}
 186
 187#define DEFINE_ALE_FIELD1_SET(name, start)				\
 188static inline void cpsw_ale_set_##name(u32 *ale_entry, u32 value,	\
 189		u32 bits)						\
 190{									\
 191	cpsw_ale_set_field(ale_entry, start, bits, value);		\
 192}
 193
 194#define DEFINE_ALE_FIELD1(name, start)					\
 195DEFINE_ALE_FIELD1_GET(name, start)					\
 196DEFINE_ALE_FIELD1_SET(name, start)
 197
 198enum {
 199	ALE_ENT_VID_MEMBER_LIST = 0,
 200	ALE_ENT_VID_UNREG_MCAST_MSK,
 201	ALE_ENT_VID_REG_MCAST_MSK,
 202	ALE_ENT_VID_FORCE_UNTAGGED_MSK,
 203	ALE_ENT_VID_UNREG_MCAST_IDX,
 204	ALE_ENT_VID_REG_MCAST_IDX,
 205	ALE_ENT_VID_LAST,
 206};
 207
 208#define ALE_FLD_ALLOWED			BIT(0)
 209#define ALE_FLD_SIZE_PORT_MASK_BITS	BIT(1)
 210#define ALE_FLD_SIZE_PORT_NUM_BITS	BIT(2)
 211
 212#define ALE_ENTRY_FLD(id, start, bits)	\
 213[id] = {				\
 214	.start_bit = start,		\
 215	.num_bits = bits,		\
 216	.flags = ALE_FLD_ALLOWED,	\
 217}
 218
 219#define ALE_ENTRY_FLD_DYN_MSK_SIZE(id, start)	\
 220[id] = {					\
 221	.start_bit = start,			\
 222	.num_bits = 0,				\
 223	.flags = ALE_FLD_ALLOWED |		\
 224		 ALE_FLD_SIZE_PORT_MASK_BITS,	\
 225}
 226
 227/* dm814x, am3/am4/am5, k2hk */
 228static const struct ale_entry_fld vlan_entry_cpsw[ALE_ENT_VID_LAST] = {
 229	ALE_ENTRY_FLD(ALE_ENT_VID_MEMBER_LIST, 0, 3),
 230	ALE_ENTRY_FLD(ALE_ENT_VID_UNREG_MCAST_MSK, 8, 3),
 231	ALE_ENTRY_FLD(ALE_ENT_VID_REG_MCAST_MSK, 16, 3),
 232	ALE_ENTRY_FLD(ALE_ENT_VID_FORCE_UNTAGGED_MSK, 24, 3),
 233};
 234
 235/* k2e/k2l, k3 am65/j721e cpsw2g  */
 236static const struct ale_entry_fld vlan_entry_nu[ALE_ENT_VID_LAST] = {
 237	ALE_ENTRY_FLD_DYN_MSK_SIZE(ALE_ENT_VID_MEMBER_LIST, 0),
 238	ALE_ENTRY_FLD(ALE_ENT_VID_UNREG_MCAST_IDX, 20, 3),
 239	ALE_ENTRY_FLD_DYN_MSK_SIZE(ALE_ENT_VID_FORCE_UNTAGGED_MSK, 24),
 240	ALE_ENTRY_FLD(ALE_ENT_VID_REG_MCAST_IDX, 44, 3),
 241};
 242
 243/* K3 j721e/j7200 cpsw9g/5g, am64x cpsw3g  */
 244static const struct ale_entry_fld vlan_entry_k3_cpswxg[] = {
 245	ALE_ENTRY_FLD_DYN_MSK_SIZE(ALE_ENT_VID_MEMBER_LIST, 0),
 246	ALE_ENTRY_FLD_DYN_MSK_SIZE(ALE_ENT_VID_UNREG_MCAST_MSK, 12),
 247	ALE_ENTRY_FLD_DYN_MSK_SIZE(ALE_ENT_VID_FORCE_UNTAGGED_MSK, 24),
 248	ALE_ENTRY_FLD_DYN_MSK_SIZE(ALE_ENT_VID_REG_MCAST_MSK, 36),
 249};
 250
 251DEFINE_ALE_FIELD(entry_type,		60,	2)
 252DEFINE_ALE_FIELD(vlan_id,		48,	12)
 253DEFINE_ALE_FIELD_SET(mcast_state,	62,	2)
 254DEFINE_ALE_FIELD1(port_mask,		66)
 255DEFINE_ALE_FIELD(super,			65,	1)
 256DEFINE_ALE_FIELD(ucast_type,		62,     2)
 257DEFINE_ALE_FIELD1_SET(port_num,		66)
 258DEFINE_ALE_FIELD_SET(blocked,		65,     1)
 259DEFINE_ALE_FIELD_SET(secure,		64,     1)
 260DEFINE_ALE_FIELD_GET(mcast,		40,	1)
 261
 262#define NU_VLAN_UNREG_MCAST_IDX	1
 263
 264static int cpsw_ale_entry_get_fld(struct cpsw_ale *ale,
 265				  u32 *ale_entry,
 266				  const struct ale_entry_fld *entry_tbl,
 267				  int fld_id)
 268{
 269	const struct ale_entry_fld *entry_fld;
 270	u32 bits;
 271
 272	if (!ale || !ale_entry)
 273		return -EINVAL;
 274
 275	entry_fld = &entry_tbl[fld_id];
 276	if (!(entry_fld->flags & ALE_FLD_ALLOWED)) {
 277		dev_err(ale->params.dev, "get: wrong ale fld id %d\n", fld_id);
 278		return -ENOENT;
 279	}
 280
 281	bits = entry_fld->num_bits;
 282	if (entry_fld->flags & ALE_FLD_SIZE_PORT_MASK_BITS)
 283		bits = ale->port_mask_bits;
 284
 285	return cpsw_ale_get_field(ale_entry, entry_fld->start_bit, bits);
 286}
 287
 288static void cpsw_ale_entry_set_fld(struct cpsw_ale *ale,
 289				   u32 *ale_entry,
 290				   const struct ale_entry_fld *entry_tbl,
 291				   int fld_id,
 292				   u32 value)
 293{
 294	const struct ale_entry_fld *entry_fld;
 295	u32 bits;
 296
 297	if (!ale || !ale_entry)
 298		return;
 299
 300	entry_fld = &entry_tbl[fld_id];
 301	if (!(entry_fld->flags & ALE_FLD_ALLOWED)) {
 302		dev_err(ale->params.dev, "set: wrong ale fld id %d\n", fld_id);
 303		return;
 304	}
 305
 306	bits = entry_fld->num_bits;
 307	if (entry_fld->flags & ALE_FLD_SIZE_PORT_MASK_BITS)
 308		bits = ale->port_mask_bits;
 309
 310	cpsw_ale_set_field(ale_entry, entry_fld->start_bit, bits, value);
 311}
 312
 313static int cpsw_ale_vlan_get_fld(struct cpsw_ale *ale,
 314				 u32 *ale_entry,
 315				 int fld_id)
 316{
 317	return cpsw_ale_entry_get_fld(ale, ale_entry,
 318				      ale->vlan_entry_tbl, fld_id);
 319}
 320
 321static void cpsw_ale_vlan_set_fld(struct cpsw_ale *ale,
 322				  u32 *ale_entry,
 323				  int fld_id,
 324				  u32 value)
 325{
 326	cpsw_ale_entry_set_fld(ale, ale_entry,
 327			       ale->vlan_entry_tbl, fld_id, value);
 328}
 329
 330/* The MAC address field in the ALE entry cannot be macroized as above */
 331static inline void cpsw_ale_get_addr(u32 *ale_entry, u8 *addr)
 332{
 333	int i;
 334
 335	for (i = 0; i < 6; i++)
 336		addr[i] = cpsw_ale_get_field(ale_entry, 40 - 8*i, 8);
 337}
 338
 339static inline void cpsw_ale_set_addr(u32 *ale_entry, const u8 *addr)
 340{
 341	int i;
 342
 343	for (i = 0; i < 6; i++)
 344		cpsw_ale_set_field(ale_entry, 40 - 8*i, 8, addr[i]);
 345}
 346
 347static int cpsw_ale_read(struct cpsw_ale *ale, int idx, u32 *ale_entry)
 348{
 349	int i;
 350
 351	WARN_ON(idx > ale->params.ale_entries);
 352
 353	writel_relaxed(idx, ale->params.ale_regs + ALE_TABLE_CONTROL);
 354
 355	for (i = 0; i < ALE_ENTRY_WORDS; i++)
 356		ale_entry[i] = readl_relaxed(ale->params.ale_regs +
 357					     ALE_TABLE + 4 * i);
 358
 359	return idx;
 360}
 361
 362static int cpsw_ale_write(struct cpsw_ale *ale, int idx, u32 *ale_entry)
 363{
 364	int i;
 365
 366	WARN_ON(idx > ale->params.ale_entries);
 367
 368	for (i = 0; i < ALE_ENTRY_WORDS; i++)
 369		writel_relaxed(ale_entry[i], ale->params.ale_regs +
 370			       ALE_TABLE + 4 * i);
 371
 372	writel_relaxed(idx | ALE_TABLE_WRITE, ale->params.ale_regs +
 373		       ALE_TABLE_CONTROL);
 374
 375	return idx;
 376}
 377
 378static int cpsw_ale_match_addr(struct cpsw_ale *ale, const u8 *addr, u16 vid)
 379{
 380	u32 ale_entry[ALE_ENTRY_WORDS];
 381	int type, idx;
 382
 383	for (idx = 0; idx < ale->params.ale_entries; idx++) {
 384		u8 entry_addr[6];
 385
 386		cpsw_ale_read(ale, idx, ale_entry);
 387		type = cpsw_ale_get_entry_type(ale_entry);
 388		if (type != ALE_TYPE_ADDR && type != ALE_TYPE_VLAN_ADDR)
 389			continue;
 390		if (cpsw_ale_get_vlan_id(ale_entry) != vid)
 391			continue;
 392		cpsw_ale_get_addr(ale_entry, entry_addr);
 393		if (ether_addr_equal(entry_addr, addr))
 394			return idx;
 395	}
 396	return -ENOENT;
 397}
 398
 399static int cpsw_ale_match_vlan(struct cpsw_ale *ale, u16 vid)
 400{
 401	u32 ale_entry[ALE_ENTRY_WORDS];
 402	int type, idx;
 403
 404	for (idx = 0; idx < ale->params.ale_entries; idx++) {
 405		cpsw_ale_read(ale, idx, ale_entry);
 406		type = cpsw_ale_get_entry_type(ale_entry);
 407		if (type != ALE_TYPE_VLAN)
 408			continue;
 409		if (cpsw_ale_get_vlan_id(ale_entry) == vid)
 410			return idx;
 411	}
 412	return -ENOENT;
 413}
 414
 415static int cpsw_ale_match_free(struct cpsw_ale *ale)
 416{
 417	u32 ale_entry[ALE_ENTRY_WORDS];
 418	int type, idx;
 419
 420	for (idx = 0; idx < ale->params.ale_entries; idx++) {
 421		cpsw_ale_read(ale, idx, ale_entry);
 422		type = cpsw_ale_get_entry_type(ale_entry);
 423		if (type == ALE_TYPE_FREE)
 424			return idx;
 425	}
 426	return -ENOENT;
 427}
 428
 429static int cpsw_ale_find_ageable(struct cpsw_ale *ale)
 430{
 431	u32 ale_entry[ALE_ENTRY_WORDS];
 432	int type, idx;
 433
 434	for (idx = 0; idx < ale->params.ale_entries; idx++) {
 435		cpsw_ale_read(ale, idx, ale_entry);
 436		type = cpsw_ale_get_entry_type(ale_entry);
 437		if (type != ALE_TYPE_ADDR && type != ALE_TYPE_VLAN_ADDR)
 438			continue;
 439		if (cpsw_ale_get_mcast(ale_entry))
 440			continue;
 441		type = cpsw_ale_get_ucast_type(ale_entry);
 442		if (type != ALE_UCAST_PERSISTANT &&
 443		    type != ALE_UCAST_OUI)
 444			return idx;
 445	}
 446	return -ENOENT;
 447}
 448
 449static void cpsw_ale_flush_mcast(struct cpsw_ale *ale, u32 *ale_entry,
 450				 int port_mask)
 451{
 452	int mask;
 453
 454	mask = cpsw_ale_get_port_mask(ale_entry,
 455				      ale->port_mask_bits);
 456	if ((mask & port_mask) == 0)
 457		return; /* ports dont intersect, not interested */
 458	mask &= ~port_mask;
 459
 460	/* free if only remaining port is host port */
 461	if (mask)
 462		cpsw_ale_set_port_mask(ale_entry, mask,
 463				       ale->port_mask_bits);
 464	else
 465		cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
 466}
 467
 468int cpsw_ale_flush_multicast(struct cpsw_ale *ale, int port_mask, int vid)
 469{
 470	u32 ale_entry[ALE_ENTRY_WORDS];
 471	int ret, idx;
 472
 473	for (idx = 0; idx < ale->params.ale_entries; idx++) {
 474		cpsw_ale_read(ale, idx, ale_entry);
 475		ret = cpsw_ale_get_entry_type(ale_entry);
 476		if (ret != ALE_TYPE_ADDR && ret != ALE_TYPE_VLAN_ADDR)
 477			continue;
 478
 479		/* if vid passed is -1 then remove all multicast entry from
 480		 * the table irrespective of vlan id, if a valid vlan id is
 481		 * passed then remove only multicast added to that vlan id.
 482		 * if vlan id doesn't match then move on to next entry.
 483		 */
 484		if (vid != -1 && cpsw_ale_get_vlan_id(ale_entry) != vid)
 485			continue;
 486
 487		if (cpsw_ale_get_mcast(ale_entry)) {
 488			u8 addr[6];
 489
 490			if (cpsw_ale_get_super(ale_entry))
 491				continue;
 492
 493			cpsw_ale_get_addr(ale_entry, addr);
 494			if (!is_broadcast_ether_addr(addr))
 495				cpsw_ale_flush_mcast(ale, ale_entry, port_mask);
 496		}
 497
 498		cpsw_ale_write(ale, idx, ale_entry);
 499	}
 500	return 0;
 501}
 502
 503static inline void cpsw_ale_set_vlan_entry_type(u32 *ale_entry,
 504						int flags, u16 vid)
 505{
 506	if (flags & ALE_VLAN) {
 507		cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_VLAN_ADDR);
 508		cpsw_ale_set_vlan_id(ale_entry, vid);
 509	} else {
 510		cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_ADDR);
 511	}
 512}
 513
 514int cpsw_ale_add_ucast(struct cpsw_ale *ale, const u8 *addr, int port,
 515		       int flags, u16 vid)
 516{
 517	u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
 518	int idx;
 519
 520	cpsw_ale_set_vlan_entry_type(ale_entry, flags, vid);
 521
 522	cpsw_ale_set_addr(ale_entry, addr);
 523	cpsw_ale_set_ucast_type(ale_entry, ALE_UCAST_PERSISTANT);
 524	cpsw_ale_set_secure(ale_entry, (flags & ALE_SECURE) ? 1 : 0);
 525	cpsw_ale_set_blocked(ale_entry, (flags & ALE_BLOCKED) ? 1 : 0);
 526	cpsw_ale_set_port_num(ale_entry, port, ale->port_num_bits);
 527
 528	idx = cpsw_ale_match_addr(ale, addr, (flags & ALE_VLAN) ? vid : 0);
 529	if (idx < 0)
 530		idx = cpsw_ale_match_free(ale);
 531	if (idx < 0)
 532		idx = cpsw_ale_find_ageable(ale);
 533	if (idx < 0)
 534		return -ENOMEM;
 535
 536	cpsw_ale_write(ale, idx, ale_entry);
 537	return 0;
 538}
 539
 540int cpsw_ale_del_ucast(struct cpsw_ale *ale, const u8 *addr, int port,
 541		       int flags, u16 vid)
 542{
 543	u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
 544	int idx;
 545
 546	idx = cpsw_ale_match_addr(ale, addr, (flags & ALE_VLAN) ? vid : 0);
 547	if (idx < 0)
 548		return -ENOENT;
 549
 550	cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
 551	cpsw_ale_write(ale, idx, ale_entry);
 552	return 0;
 553}
 554
 555int cpsw_ale_add_mcast(struct cpsw_ale *ale, const u8 *addr, int port_mask,
 556		       int flags, u16 vid, int mcast_state)
 557{
 558	u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
 559	int idx, mask;
 560
 561	idx = cpsw_ale_match_addr(ale, addr, (flags & ALE_VLAN) ? vid : 0);
 562	if (idx >= 0)
 563		cpsw_ale_read(ale, idx, ale_entry);
 564
 565	cpsw_ale_set_vlan_entry_type(ale_entry, flags, vid);
 566
 567	cpsw_ale_set_addr(ale_entry, addr);
 568	cpsw_ale_set_super(ale_entry, (flags & ALE_SUPER) ? 1 : 0);
 569	cpsw_ale_set_mcast_state(ale_entry, mcast_state);
 570
 571	mask = cpsw_ale_get_port_mask(ale_entry,
 572				      ale->port_mask_bits);
 573	port_mask |= mask;
 574	cpsw_ale_set_port_mask(ale_entry, port_mask,
 575			       ale->port_mask_bits);
 576
 577	if (idx < 0)
 578		idx = cpsw_ale_match_free(ale);
 579	if (idx < 0)
 580		idx = cpsw_ale_find_ageable(ale);
 581	if (idx < 0)
 582		return -ENOMEM;
 583
 584	cpsw_ale_write(ale, idx, ale_entry);
 585	return 0;
 586}
 587
 588int cpsw_ale_del_mcast(struct cpsw_ale *ale, const u8 *addr, int port_mask,
 589		       int flags, u16 vid)
 590{
 591	u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
 592	int mcast_members = 0;
 593	int idx;
 594
 595	idx = cpsw_ale_match_addr(ale, addr, (flags & ALE_VLAN) ? vid : 0);
 596	if (idx < 0)
 597		return -ENOENT;
 598
 599	cpsw_ale_read(ale, idx, ale_entry);
 600
 601	if (port_mask) {
 602		mcast_members = cpsw_ale_get_port_mask(ale_entry,
 603						       ale->port_mask_bits);
 604		mcast_members &= ~port_mask;
 605	}
 606
 607	if (mcast_members)
 608		cpsw_ale_set_port_mask(ale_entry, mcast_members,
 609				       ale->port_mask_bits);
 610	else
 611		cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
 612
 613	cpsw_ale_write(ale, idx, ale_entry);
 614	return 0;
 615}
 616
 617/* ALE NetCP NU switch specific vlan functions */
 618static void cpsw_ale_set_vlan_mcast(struct cpsw_ale *ale, u32 *ale_entry,
 619				    int reg_mcast, int unreg_mcast)
 620{
 621	int idx;
 622
 623	/* Set VLAN registered multicast flood mask */
 624	idx = cpsw_ale_vlan_get_fld(ale, ale_entry,
 625				    ALE_ENT_VID_REG_MCAST_IDX);
 626	writel(reg_mcast, ale->params.ale_regs + ALE_VLAN_MASK_MUX(idx));
 627
 628	/* Set VLAN unregistered multicast flood mask */
 629	idx = cpsw_ale_vlan_get_fld(ale, ale_entry,
 630				    ALE_ENT_VID_UNREG_MCAST_IDX);
 631	writel(unreg_mcast, ale->params.ale_regs + ALE_VLAN_MASK_MUX(idx));
 632}
 633
 634static void cpsw_ale_set_vlan_untag(struct cpsw_ale *ale, u32 *ale_entry,
 635				    u16 vid, int untag_mask)
 636{
 637	cpsw_ale_vlan_set_fld(ale, ale_entry,
 638			      ALE_ENT_VID_FORCE_UNTAGGED_MSK,
 639			      untag_mask);
 640	if (untag_mask & ALE_PORT_HOST)
 641		bitmap_set(ale->p0_untag_vid_mask, vid, 1);
 642	else
 643		bitmap_clear(ale->p0_untag_vid_mask, vid, 1);
 644}
 645
 646int cpsw_ale_add_vlan(struct cpsw_ale *ale, u16 vid, int port_mask, int untag,
 647		      int reg_mcast, int unreg_mcast)
 648{
 649	u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
 650	int idx;
 651
 652	idx = cpsw_ale_match_vlan(ale, vid);
 653	if (idx >= 0)
 654		cpsw_ale_read(ale, idx, ale_entry);
 655
 656	cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_VLAN);
 657	cpsw_ale_set_vlan_id(ale_entry, vid);
 658	cpsw_ale_set_vlan_untag(ale, ale_entry, vid, untag);
 659
 660	if (!ale->params.nu_switch_ale) {
 661		cpsw_ale_vlan_set_fld(ale, ale_entry,
 662				      ALE_ENT_VID_REG_MCAST_MSK, reg_mcast);
 663		cpsw_ale_vlan_set_fld(ale, ale_entry,
 664				      ALE_ENT_VID_UNREG_MCAST_MSK, unreg_mcast);
 665	} else {
 666		cpsw_ale_vlan_set_fld(ale, ale_entry,
 667				      ALE_ENT_VID_UNREG_MCAST_IDX,
 668				      NU_VLAN_UNREG_MCAST_IDX);
 669		cpsw_ale_set_vlan_mcast(ale, ale_entry, reg_mcast, unreg_mcast);
 670	}
 671
 672	cpsw_ale_vlan_set_fld(ale, ale_entry,
 673			      ALE_ENT_VID_MEMBER_LIST, port_mask);
 674
 675	if (idx < 0)
 676		idx = cpsw_ale_match_free(ale);
 677	if (idx < 0)
 678		idx = cpsw_ale_find_ageable(ale);
 679	if (idx < 0)
 680		return -ENOMEM;
 681
 682	cpsw_ale_write(ale, idx, ale_entry);
 683	return 0;
 684}
 685
 686static void cpsw_ale_vlan_del_modify_int(struct cpsw_ale *ale,  u32 *ale_entry,
 687					 u16 vid, int port_mask)
 688{
 689	int reg_mcast, unreg_mcast;
 690	int members, untag;
 691
 692	members = cpsw_ale_vlan_get_fld(ale, ale_entry,
 693					ALE_ENT_VID_MEMBER_LIST);
 694	members &= ~port_mask;
 695	if (!members) {
 696		cpsw_ale_set_vlan_untag(ale, ale_entry, vid, 0);
 697		cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
 698		return;
 699	}
 700
 701	untag = cpsw_ale_vlan_get_fld(ale, ale_entry,
 702				      ALE_ENT_VID_FORCE_UNTAGGED_MSK);
 703	reg_mcast = cpsw_ale_vlan_get_fld(ale, ale_entry,
 704					  ALE_ENT_VID_REG_MCAST_MSK);
 705	unreg_mcast = cpsw_ale_vlan_get_fld(ale, ale_entry,
 706					    ALE_ENT_VID_UNREG_MCAST_MSK);
 707	untag &= members;
 708	reg_mcast &= members;
 709	unreg_mcast &= members;
 710
 711	cpsw_ale_set_vlan_untag(ale, ale_entry, vid, untag);
 712
 713	if (!ale->params.nu_switch_ale) {
 714		cpsw_ale_vlan_set_fld(ale, ale_entry,
 715				      ALE_ENT_VID_REG_MCAST_MSK, reg_mcast);
 716		cpsw_ale_vlan_set_fld(ale, ale_entry,
 717				      ALE_ENT_VID_UNREG_MCAST_MSK, unreg_mcast);
 718	} else {
 719		cpsw_ale_set_vlan_mcast(ale, ale_entry, reg_mcast,
 720					unreg_mcast);
 721	}
 722	cpsw_ale_vlan_set_fld(ale, ale_entry,
 723			      ALE_ENT_VID_MEMBER_LIST, members);
 724}
 725
 726int cpsw_ale_vlan_del_modify(struct cpsw_ale *ale, u16 vid, int port_mask)
 727{
 728	u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
 729	int idx;
 730
 731	idx = cpsw_ale_match_vlan(ale, vid);
 732	if (idx < 0)
 733		return -ENOENT;
 734
 735	cpsw_ale_read(ale, idx, ale_entry);
 736
 737	cpsw_ale_vlan_del_modify_int(ale, ale_entry, vid, port_mask);
 738	cpsw_ale_write(ale, idx, ale_entry);
 739
 740	return 0;
 741}
 742
 743int cpsw_ale_del_vlan(struct cpsw_ale *ale, u16 vid, int port_mask)
 744{
 745	u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
 746	int members, idx;
 747
 748	idx = cpsw_ale_match_vlan(ale, vid);
 749	if (idx < 0)
 750		return -ENOENT;
 751
 752	cpsw_ale_read(ale, idx, ale_entry);
 753
 754	/* if !port_mask - force remove VLAN (legacy).
 755	 * Check if there are other VLAN members ports
 756	 * if no - remove VLAN.
 757	 * if yes it means same VLAN was added to >1 port in multi port mode, so
 758	 * remove port_mask ports from VLAN ALE entry excluding Host port.
 759	 */
 760	members = cpsw_ale_vlan_get_fld(ale, ale_entry, ALE_ENT_VID_MEMBER_LIST);
 761	members &= ~port_mask;
 762
 763	if (!port_mask || !members) {
 764		/* last port or force remove - remove VLAN */
 765		cpsw_ale_set_vlan_untag(ale, ale_entry, vid, 0);
 766		cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
 767	} else {
 768		port_mask &= ~ALE_PORT_HOST;
 769		cpsw_ale_vlan_del_modify_int(ale, ale_entry, vid, port_mask);
 770	}
 771
 772	cpsw_ale_write(ale, idx, ale_entry);
 773
 774	return 0;
 775}
 776
 777int cpsw_ale_vlan_add_modify(struct cpsw_ale *ale, u16 vid, int port_mask,
 778			     int untag_mask, int reg_mask, int unreg_mask)
 779{
 780	u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
 781	int reg_mcast_members, unreg_mcast_members;
 782	int vlan_members, untag_members;
 783	int idx, ret = 0;
 784
 785	idx = cpsw_ale_match_vlan(ale, vid);
 786	if (idx >= 0)
 787		cpsw_ale_read(ale, idx, ale_entry);
 788
 789	vlan_members = cpsw_ale_vlan_get_fld(ale, ale_entry,
 790					     ALE_ENT_VID_MEMBER_LIST);
 791	reg_mcast_members = cpsw_ale_vlan_get_fld(ale, ale_entry,
 792						  ALE_ENT_VID_REG_MCAST_MSK);
 793	unreg_mcast_members =
 794		cpsw_ale_vlan_get_fld(ale, ale_entry,
 795				      ALE_ENT_VID_UNREG_MCAST_MSK);
 796	untag_members = cpsw_ale_vlan_get_fld(ale, ale_entry,
 797					      ALE_ENT_VID_FORCE_UNTAGGED_MSK);
 798
 799	vlan_members |= port_mask;
 800	untag_members = (untag_members & ~port_mask) | untag_mask;
 801	reg_mcast_members = (reg_mcast_members & ~port_mask) | reg_mask;
 802	unreg_mcast_members = (unreg_mcast_members & ~port_mask) | unreg_mask;
 803
 804	ret = cpsw_ale_add_vlan(ale, vid, vlan_members, untag_members,
 805				reg_mcast_members, unreg_mcast_members);
 806	if (ret) {
 807		dev_err(ale->params.dev, "Unable to add vlan\n");
 808		return ret;
 809	}
 810	dev_dbg(ale->params.dev, "port mask 0x%x untag 0x%x\n", vlan_members,
 811		untag_mask);
 812
 813	return ret;
 814}
 815
 816void cpsw_ale_set_unreg_mcast(struct cpsw_ale *ale, int unreg_mcast_mask,
 817			      bool add)
 818{
 819	u32 ale_entry[ALE_ENTRY_WORDS];
 820	int unreg_members = 0;
 821	int type, idx;
 822
 823	for (idx = 0; idx < ale->params.ale_entries; idx++) {
 824		cpsw_ale_read(ale, idx, ale_entry);
 825		type = cpsw_ale_get_entry_type(ale_entry);
 826		if (type != ALE_TYPE_VLAN)
 827			continue;
 828
 829		unreg_members =
 830			cpsw_ale_vlan_get_fld(ale, ale_entry,
 831					      ALE_ENT_VID_UNREG_MCAST_MSK);
 832		if (add)
 833			unreg_members |= unreg_mcast_mask;
 834		else
 835			unreg_members &= ~unreg_mcast_mask;
 836		cpsw_ale_vlan_set_fld(ale, ale_entry,
 837				      ALE_ENT_VID_UNREG_MCAST_MSK,
 838				      unreg_members);
 839		cpsw_ale_write(ale, idx, ale_entry);
 840	}
 841}
 842
 843static void cpsw_ale_vlan_set_unreg_mcast(struct cpsw_ale *ale, u32 *ale_entry,
 844					  int allmulti)
 845{
 846	int unreg_mcast;
 847
 848	unreg_mcast = cpsw_ale_vlan_get_fld(ale, ale_entry,
 849					    ALE_ENT_VID_UNREG_MCAST_MSK);
 850	if (allmulti)
 851		unreg_mcast |= ALE_PORT_HOST;
 852	else
 853		unreg_mcast &= ~ALE_PORT_HOST;
 854
 855	cpsw_ale_vlan_set_fld(ale, ale_entry,
 856			      ALE_ENT_VID_UNREG_MCAST_MSK, unreg_mcast);
 857}
 858
 859static void
 860cpsw_ale_vlan_set_unreg_mcast_idx(struct cpsw_ale *ale, u32 *ale_entry,
 861				  int allmulti)
 862{
 863	int unreg_mcast;
 864	int idx;
 865
 866	idx = cpsw_ale_vlan_get_fld(ale, ale_entry,
 867				    ALE_ENT_VID_UNREG_MCAST_IDX);
 868
 869	unreg_mcast = readl(ale->params.ale_regs + ALE_VLAN_MASK_MUX(idx));
 870
 871	if (allmulti)
 872		unreg_mcast |= ALE_PORT_HOST;
 873	else
 874		unreg_mcast &= ~ALE_PORT_HOST;
 875
 876	writel(unreg_mcast, ale->params.ale_regs + ALE_VLAN_MASK_MUX(idx));
 877}
 878
 879void cpsw_ale_set_allmulti(struct cpsw_ale *ale, int allmulti, int port)
 880{
 881	u32 ale_entry[ALE_ENTRY_WORDS];
 882	int type, idx;
 883
 884	for (idx = 0; idx < ale->params.ale_entries; idx++) {
 885		int vlan_members;
 886
 887		cpsw_ale_read(ale, idx, ale_entry);
 888		type = cpsw_ale_get_entry_type(ale_entry);
 889		if (type != ALE_TYPE_VLAN)
 890			continue;
 891
 892		vlan_members = cpsw_ale_vlan_get_fld(ale, ale_entry,
 893						     ALE_ENT_VID_MEMBER_LIST);
 894
 895		if (port != -1 && !(vlan_members & BIT(port)))
 896			continue;
 897
 898		if (!ale->params.nu_switch_ale)
 899			cpsw_ale_vlan_set_unreg_mcast(ale, ale_entry, allmulti);
 900		else
 901			cpsw_ale_vlan_set_unreg_mcast_idx(ale, ale_entry,
 902							  allmulti);
 903
 904		cpsw_ale_write(ale, idx, ale_entry);
 905	}
 906}
 907
 908struct ale_control_info {
 909	const char	*name;
 910	int		offset, port_offset;
 911	int		shift, port_shift;
 912	int		bits;
 913};
 914
 915static struct ale_control_info ale_controls[ALE_NUM_CONTROLS] = {
 916	[ALE_ENABLE]		= {
 917		.name		= "enable",
 918		.offset		= ALE_CONTROL,
 919		.port_offset	= 0,
 920		.shift		= 31,
 921		.port_shift	= 0,
 922		.bits		= 1,
 923	},
 924	[ALE_CLEAR]		= {
 925		.name		= "clear",
 926		.offset		= ALE_CONTROL,
 927		.port_offset	= 0,
 928		.shift		= 30,
 929		.port_shift	= 0,
 930		.bits		= 1,
 931	},
 932	[ALE_AGEOUT]		= {
 933		.name		= "ageout",
 934		.offset		= ALE_CONTROL,
 935		.port_offset	= 0,
 936		.shift		= 29,
 937		.port_shift	= 0,
 938		.bits		= 1,
 939	},
 940	[ALE_P0_UNI_FLOOD]	= {
 941		.name		= "port0_unicast_flood",
 942		.offset		= ALE_CONTROL,
 943		.port_offset	= 0,
 944		.shift		= 8,
 945		.port_shift	= 0,
 946		.bits		= 1,
 947	},
 948	[ALE_VLAN_NOLEARN]	= {
 949		.name		= "vlan_nolearn",
 950		.offset		= ALE_CONTROL,
 951		.port_offset	= 0,
 952		.shift		= 7,
 953		.port_shift	= 0,
 954		.bits		= 1,
 955	},
 956	[ALE_NO_PORT_VLAN]	= {
 957		.name		= "no_port_vlan",
 958		.offset		= ALE_CONTROL,
 959		.port_offset	= 0,
 960		.shift		= 6,
 961		.port_shift	= 0,
 962		.bits		= 1,
 963	},
 964	[ALE_OUI_DENY]		= {
 965		.name		= "oui_deny",
 966		.offset		= ALE_CONTROL,
 967		.port_offset	= 0,
 968		.shift		= 5,
 969		.port_shift	= 0,
 970		.bits		= 1,
 971	},
 972	[ALE_BYPASS]		= {
 973		.name		= "bypass",
 974		.offset		= ALE_CONTROL,
 975		.port_offset	= 0,
 976		.shift		= 4,
 977		.port_shift	= 0,
 978		.bits		= 1,
 979	},
 980	[ALE_RATE_LIMIT_TX]	= {
 981		.name		= "rate_limit_tx",
 982		.offset		= ALE_CONTROL,
 983		.port_offset	= 0,
 984		.shift		= 3,
 985		.port_shift	= 0,
 986		.bits		= 1,
 987	},
 988	[ALE_VLAN_AWARE]	= {
 989		.name		= "vlan_aware",
 990		.offset		= ALE_CONTROL,
 991		.port_offset	= 0,
 992		.shift		= 2,
 993		.port_shift	= 0,
 994		.bits		= 1,
 995	},
 996	[ALE_AUTH_ENABLE]	= {
 997		.name		= "auth_enable",
 998		.offset		= ALE_CONTROL,
 999		.port_offset	= 0,
1000		.shift		= 1,
1001		.port_shift	= 0,
1002		.bits		= 1,
1003	},
1004	[ALE_RATE_LIMIT]	= {
1005		.name		= "rate_limit",
1006		.offset		= ALE_CONTROL,
1007		.port_offset	= 0,
1008		.shift		= 0,
1009		.port_shift	= 0,
1010		.bits		= 1,
1011	},
1012	[ALE_PORT_STATE]	= {
1013		.name		= "port_state",
1014		.offset		= ALE_PORTCTL,
1015		.port_offset	= 4,
1016		.shift		= 0,
1017		.port_shift	= 0,
1018		.bits		= 2,
1019	},
1020	[ALE_PORT_DROP_UNTAGGED] = {
1021		.name		= "drop_untagged",
1022		.offset		= ALE_PORTCTL,
1023		.port_offset	= 4,
1024		.shift		= 2,
1025		.port_shift	= 0,
1026		.bits		= 1,
1027	},
1028	[ALE_PORT_DROP_UNKNOWN_VLAN] = {
1029		.name		= "drop_unknown",
1030		.offset		= ALE_PORTCTL,
1031		.port_offset	= 4,
1032		.shift		= 3,
1033		.port_shift	= 0,
1034		.bits		= 1,
1035	},
1036	[ALE_PORT_NOLEARN]	= {
1037		.name		= "nolearn",
1038		.offset		= ALE_PORTCTL,
1039		.port_offset	= 4,
1040		.shift		= 4,
1041		.port_shift	= 0,
1042		.bits		= 1,
1043	},
1044	[ALE_PORT_NO_SA_UPDATE]	= {
1045		.name		= "no_source_update",
1046		.offset		= ALE_PORTCTL,
1047		.port_offset	= 4,
1048		.shift		= 5,
1049		.port_shift	= 0,
1050		.bits		= 1,
1051	},
1052	[ALE_PORT_MACONLY]	= {
1053		.name		= "mac_only_port_mode",
1054		.offset		= ALE_PORTCTL,
1055		.port_offset	= 4,
1056		.shift		= 11,
1057		.port_shift	= 0,
1058		.bits		= 1,
1059	},
1060	[ALE_PORT_MACONLY_CAF]	= {
1061		.name		= "mac_only_port_caf",
1062		.offset		= ALE_PORTCTL,
1063		.port_offset	= 4,
1064		.shift		= 13,
1065		.port_shift	= 0,
1066		.bits		= 1,
1067	},
1068	[ALE_PORT_MCAST_LIMIT]	= {
1069		.name		= "mcast_limit",
1070		.offset		= ALE_PORTCTL,
1071		.port_offset	= 4,
1072		.shift		= 16,
1073		.port_shift	= 0,
1074		.bits		= 8,
1075	},
1076	[ALE_PORT_BCAST_LIMIT]	= {
1077		.name		= "bcast_limit",
1078		.offset		= ALE_PORTCTL,
1079		.port_offset	= 4,
1080		.shift		= 24,
1081		.port_shift	= 0,
1082		.bits		= 8,
1083	},
1084	[ALE_PORT_UNKNOWN_VLAN_MEMBER] = {
1085		.name		= "unknown_vlan_member",
1086		.offset		= ALE_UNKNOWNVLAN,
1087		.port_offset	= 0,
1088		.shift		= 0,
1089		.port_shift	= 0,
1090		.bits		= 6,
1091	},
1092	[ALE_PORT_UNKNOWN_MCAST_FLOOD] = {
1093		.name		= "unknown_mcast_flood",
1094		.offset		= ALE_UNKNOWNVLAN,
1095		.port_offset	= 0,
1096		.shift		= 8,
1097		.port_shift	= 0,
1098		.bits		= 6,
1099	},
1100	[ALE_PORT_UNKNOWN_REG_MCAST_FLOOD] = {
1101		.name		= "unknown_reg_flood",
1102		.offset		= ALE_UNKNOWNVLAN,
1103		.port_offset	= 0,
1104		.shift		= 16,
1105		.port_shift	= 0,
1106		.bits		= 6,
1107	},
1108	[ALE_PORT_UNTAGGED_EGRESS] = {
1109		.name		= "untagged_egress",
1110		.offset		= ALE_UNKNOWNVLAN,
1111		.port_offset	= 0,
1112		.shift		= 24,
1113		.port_shift	= 0,
1114		.bits		= 6,
1115	},
1116	[ALE_DEFAULT_THREAD_ID] = {
1117		.name		= "default_thread_id",
1118		.offset		= AM65_CPSW_ALE_THREAD_DEF_REG,
1119		.port_offset	= 0,
1120		.shift		= 0,
1121		.port_shift	= 0,
1122		.bits		= 6,
1123	},
1124	[ALE_DEFAULT_THREAD_ENABLE] = {
1125		.name		= "default_thread_id_enable",
1126		.offset		= AM65_CPSW_ALE_THREAD_DEF_REG,
1127		.port_offset	= 0,
1128		.shift		= 15,
1129		.port_shift	= 0,
1130		.bits		= 1,
1131	},
1132};
1133
1134int cpsw_ale_control_set(struct cpsw_ale *ale, int port, int control,
1135			 int value)
1136{
1137	const struct ale_control_info *info;
1138	int offset, shift;
1139	u32 tmp, mask;
1140
1141	if (control < 0 || control >= ARRAY_SIZE(ale_controls))
1142		return -EINVAL;
1143
1144	info = &ale_controls[control];
1145	if (info->port_offset == 0 && info->port_shift == 0)
1146		port = 0; /* global, port is a dont care */
1147
1148	if (port < 0 || port >= ale->params.ale_ports)
1149		return -EINVAL;
1150
1151	mask = BITMASK(info->bits);
1152	if (value & ~mask)
1153		return -EINVAL;
1154
1155	offset = info->offset + (port * info->port_offset);
1156	shift  = info->shift  + (port * info->port_shift);
1157
1158	tmp = readl_relaxed(ale->params.ale_regs + offset);
1159	tmp = (tmp & ~(mask << shift)) | (value << shift);
1160	writel_relaxed(tmp, ale->params.ale_regs + offset);
1161
1162	return 0;
1163}
1164
1165int cpsw_ale_control_get(struct cpsw_ale *ale, int port, int control)
1166{
1167	const struct ale_control_info *info;
1168	int offset, shift;
1169	u32 tmp;
1170
1171	if (control < 0 || control >= ARRAY_SIZE(ale_controls))
1172		return -EINVAL;
1173
1174	info = &ale_controls[control];
1175	if (info->port_offset == 0 && info->port_shift == 0)
1176		port = 0; /* global, port is a dont care */
1177
1178	if (port < 0 || port >= ale->params.ale_ports)
1179		return -EINVAL;
1180
1181	offset = info->offset + (port * info->port_offset);
1182	shift  = info->shift  + (port * info->port_shift);
1183
1184	tmp = readl_relaxed(ale->params.ale_regs + offset) >> shift;
1185	return tmp & BITMASK(info->bits);
1186}
1187
1188int cpsw_ale_rx_ratelimit_mc(struct cpsw_ale *ale, int port, unsigned int ratelimit_pps)
1189
1190{
1191	int val = ratelimit_pps / ALE_RATE_LIMIT_MIN_PPS;
1192	u32 remainder = ratelimit_pps % ALE_RATE_LIMIT_MIN_PPS;
1193
1194	if (ratelimit_pps && !val) {
1195		dev_err(ale->params.dev, "ALE MC port:%d ratelimit min value 1000pps\n", port);
1196		return -EINVAL;
1197	}
1198
1199	if (remainder)
1200		dev_info(ale->params.dev, "ALE port:%d MC ratelimit set to %dpps (requested %d)\n",
1201			 port, ratelimit_pps - remainder, ratelimit_pps);
1202
1203	cpsw_ale_control_set(ale, port, ALE_PORT_MCAST_LIMIT, val);
1204
1205	dev_dbg(ale->params.dev, "ALE port:%d MC ratelimit set %d\n",
1206		port, val * ALE_RATE_LIMIT_MIN_PPS);
1207	return 0;
1208}
1209
1210int cpsw_ale_rx_ratelimit_bc(struct cpsw_ale *ale, int port, unsigned int ratelimit_pps)
1211
1212{
1213	int val = ratelimit_pps / ALE_RATE_LIMIT_MIN_PPS;
1214	u32 remainder = ratelimit_pps % ALE_RATE_LIMIT_MIN_PPS;
1215
1216	if (ratelimit_pps && !val) {
1217		dev_err(ale->params.dev, "ALE port:%d BC ratelimit min value 1000pps\n", port);
1218		return -EINVAL;
1219	}
1220
1221	if (remainder)
1222		dev_info(ale->params.dev, "ALE port:%d BC ratelimit set to %dpps (requested %d)\n",
1223			 port, ratelimit_pps - remainder, ratelimit_pps);
1224
1225	cpsw_ale_control_set(ale, port, ALE_PORT_BCAST_LIMIT, val);
1226
1227	dev_dbg(ale->params.dev, "ALE port:%d BC ratelimit set %d\n",
1228		port, val * ALE_RATE_LIMIT_MIN_PPS);
1229	return 0;
1230}
1231
1232static void cpsw_ale_timer(struct timer_list *t)
1233{
1234	struct cpsw_ale *ale = from_timer(ale, t, timer);
1235
1236	cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
1237
1238	if (ale->ageout) {
1239		ale->timer.expires = jiffies + ale->ageout;
1240		add_timer(&ale->timer);
1241	}
1242}
1243
1244static void cpsw_ale_hw_aging_timer_start(struct cpsw_ale *ale)
1245{
1246	u32 aging_timer;
1247
1248	aging_timer = ale->params.bus_freq / 1000000;
1249	aging_timer *= ale->params.ale_ageout;
1250
1251	if (aging_timer & ~ALE_AGING_TIMER_MASK) {
1252		aging_timer = ALE_AGING_TIMER_MASK;
1253		dev_warn(ale->params.dev,
1254			 "ALE aging timer overflow, set to max\n");
1255	}
1256
1257	writel(aging_timer, ale->params.ale_regs + ALE_AGING_TIMER);
1258}
1259
1260static void cpsw_ale_hw_aging_timer_stop(struct cpsw_ale *ale)
1261{
1262	writel(0, ale->params.ale_regs + ALE_AGING_TIMER);
1263}
1264
1265static void cpsw_ale_aging_start(struct cpsw_ale *ale)
1266{
1267	if (!ale->params.ale_ageout)
1268		return;
1269
1270	if (ale->features & CPSW_ALE_F_HW_AUTOAGING) {
1271		cpsw_ale_hw_aging_timer_start(ale);
1272		return;
1273	}
1274
1275	timer_setup(&ale->timer, cpsw_ale_timer, 0);
1276	ale->timer.expires = jiffies + ale->ageout;
1277	add_timer(&ale->timer);
1278}
1279
1280static void cpsw_ale_aging_stop(struct cpsw_ale *ale)
1281{
1282	if (!ale->params.ale_ageout)
1283		return;
1284
1285	if (ale->features & CPSW_ALE_F_HW_AUTOAGING) {
1286		cpsw_ale_hw_aging_timer_stop(ale);
1287		return;
1288	}
1289
1290	del_timer_sync(&ale->timer);
1291}
1292
1293void cpsw_ale_start(struct cpsw_ale *ale)
1294{
1295	unsigned long ale_prescale;
1296
1297	/* configure Broadcast and Multicast Rate Limit
1298	 * number_of_packets = (Fclk / ALE_PRESCALE) * port.BCAST/MCAST_LIMIT
1299	 * ALE_PRESCALE width is 19bit and min value 0x10
1300	 * port.BCAST/MCAST_LIMIT is 8bit
1301	 *
1302	 * For multi port configuration support the ALE_PRESCALE is configured to 1ms interval,
1303	 * which allows to configure port.BCAST/MCAST_LIMIT per port and achieve:
1304	 * min number_of_packets = 1000 when port.BCAST/MCAST_LIMIT = 1
1305	 * max number_of_packets = 1000 * 255 = 255000 when port.BCAST/MCAST_LIMIT = 0xFF
1306	 */
1307	ale_prescale = ale->params.bus_freq / ALE_RATE_LIMIT_MIN_PPS;
1308	writel((u32)ale_prescale, ale->params.ale_regs + ALE_PRESCALE);
1309
1310	/* Allow MC/BC rate limiting globally.
1311	 * The actual Rate Limit cfg enabled per-port by port.BCAST/MCAST_LIMIT
1312	 */
1313	cpsw_ale_control_set(ale, 0, ALE_RATE_LIMIT, 1);
1314
1315	cpsw_ale_control_set(ale, 0, ALE_ENABLE, 1);
1316	cpsw_ale_control_set(ale, 0, ALE_CLEAR, 1);
1317
1318	cpsw_ale_aging_start(ale);
1319}
1320
1321void cpsw_ale_stop(struct cpsw_ale *ale)
1322{
1323	cpsw_ale_aging_stop(ale);
1324	cpsw_ale_control_set(ale, 0, ALE_CLEAR, 1);
1325	cpsw_ale_control_set(ale, 0, ALE_ENABLE, 0);
1326}
1327
1328static const struct reg_field ale_fields_cpsw[] = {
1329	/* CPSW_ALE_IDVER_REG */
1330	[MINOR_VER]	= REG_FIELD(ALE_IDVER, 0, 7),
1331	[MAJOR_VER]	= REG_FIELD(ALE_IDVER, 8, 15),
1332};
1333
1334static const struct reg_field ale_fields_cpsw_nu[] = {
1335	/* CPSW_ALE_IDVER_REG */
1336	[MINOR_VER]	= REG_FIELD(ALE_IDVER, 0, 7),
1337	[MAJOR_VER]	= REG_FIELD(ALE_IDVER, 8, 10),
1338	/* CPSW_ALE_STATUS_REG */
1339	[ALE_ENTRIES]	= REG_FIELD(ALE_STATUS, 0, 7),
1340	[ALE_POLICERS]	= REG_FIELD(ALE_STATUS, 8, 15),
1341	/* CPSW_ALE_POLICER_PORT_OUI_REG */
1342	[POL_PORT_MEN]	= REG_FIELD(ALE_POLICER_PORT_OUI, 31, 31),
1343	[POL_TRUNK_ID]	= REG_FIELD(ALE_POLICER_PORT_OUI, 30, 30),
1344	[POL_PORT_NUM]	= REG_FIELD(ALE_POLICER_PORT_OUI, 25, 25),
1345	[POL_PRI_MEN]	= REG_FIELD(ALE_POLICER_PORT_OUI, 19, 19),
1346	[POL_PRI_VAL]	= REG_FIELD(ALE_POLICER_PORT_OUI, 16, 18),
1347	[POL_OUI_MEN]	= REG_FIELD(ALE_POLICER_PORT_OUI, 15, 15),
1348	[POL_OUI_INDEX]	= REG_FIELD(ALE_POLICER_PORT_OUI, 0, 5),
1349
1350	/* CPSW_ALE_POLICER_DA_SA_REG */
1351	[POL_DST_MEN]	= REG_FIELD(ALE_POLICER_DA_SA, 31, 31),
1352	[POL_DST_INDEX]	= REG_FIELD(ALE_POLICER_DA_SA, 16, 21),
1353	[POL_SRC_MEN]	= REG_FIELD(ALE_POLICER_DA_SA, 15, 15),
1354	[POL_SRC_INDEX]	= REG_FIELD(ALE_POLICER_DA_SA, 0, 5),
1355
1356	/* CPSW_ALE_POLICER_VLAN_REG */
1357	[POL_OVLAN_MEN]		= REG_FIELD(ALE_POLICER_VLAN, 31, 31),
1358	[POL_OVLAN_INDEX]	= REG_FIELD(ALE_POLICER_VLAN, 16, 21),
1359	[POL_IVLAN_MEN]		= REG_FIELD(ALE_POLICER_VLAN, 15, 15),
1360	[POL_IVLAN_INDEX]	= REG_FIELD(ALE_POLICER_VLAN, 0, 5),
1361
1362	/* CPSW_ALE_POLICER_ETHERTYPE_IPSA_REG */
1363	[POL_ETHERTYPE_MEN]	= REG_FIELD(ALE_POLICER_ETHERTYPE_IPSA, 31, 31),
1364	[POL_ETHERTYPE_INDEX]	= REG_FIELD(ALE_POLICER_ETHERTYPE_IPSA, 16, 21),
1365	[POL_IPSRC_MEN]		= REG_FIELD(ALE_POLICER_ETHERTYPE_IPSA, 15, 15),
1366	[POL_IPSRC_INDEX]	= REG_FIELD(ALE_POLICER_ETHERTYPE_IPSA, 0, 5),
1367
1368	/* CPSW_ALE_POLICER_IPDA_REG */
1369	[POL_IPDST_MEN]		= REG_FIELD(ALE_POLICER_IPDA, 31, 31),
1370	[POL_IPDST_INDEX]	= REG_FIELD(ALE_POLICER_IPDA, 16, 21),
1371
1372	/* CPSW_ALE_POLICER_TBL_CTL_REG */
1373	/**
1374	 * REG_FIELDS not defined for this as fields cannot be correctly
1375	 * used independently
1376	 */
1377
1378	/* CPSW_ALE_POLICER_CTL_REG */
1379	[POL_EN]		= REG_FIELD(ALE_POLICER_CTL, 31, 31),
1380	[POL_RED_DROP_EN]	= REG_FIELD(ALE_POLICER_CTL, 29, 29),
1381	[POL_YELLOW_DROP_EN]	= REG_FIELD(ALE_POLICER_CTL, 28, 28),
1382	[POL_YELLOW_THRESH]	= REG_FIELD(ALE_POLICER_CTL, 24, 26),
1383	[POL_POL_MATCH_MODE]	= REG_FIELD(ALE_POLICER_CTL, 22, 23),
1384	[POL_PRIORITY_THREAD_EN] = REG_FIELD(ALE_POLICER_CTL, 21, 21),
1385	[POL_MAC_ONLY_DEF_DIS]	= REG_FIELD(ALE_POLICER_CTL, 20, 20),
1386
1387	/* CPSW_ALE_POLICER_TEST_CTL_REG */
1388	[POL_TEST_CLR]		= REG_FIELD(ALE_POLICER_TEST_CTL, 31, 31),
1389	[POL_TEST_CLR_RED]	= REG_FIELD(ALE_POLICER_TEST_CTL, 30, 30),
1390	[POL_TEST_CLR_YELLOW]	= REG_FIELD(ALE_POLICER_TEST_CTL, 29, 29),
1391	[POL_TEST_CLR_SELECTED]	= REG_FIELD(ALE_POLICER_TEST_CTL, 28, 28),
1392	[POL_TEST_ENTRY]	= REG_FIELD(ALE_POLICER_TEST_CTL, 0, 4),
1393
1394	/* CPSW_ALE_POLICER_HIT_STATUS_REG */
1395	[POL_STATUS_HIT]	= REG_FIELD(ALE_POLICER_HIT_STATUS, 31, 31),
1396	[POL_STATUS_HIT_RED]	= REG_FIELD(ALE_POLICER_HIT_STATUS, 30, 30),
1397	[POL_STATUS_HIT_YELLOW]	= REG_FIELD(ALE_POLICER_HIT_STATUS, 29, 29),
1398
1399	/* CPSW_ALE_THREAD_DEF_REG */
1400	[ALE_DEFAULT_THREAD_EN]		= REG_FIELD(ALE_THREAD_DEF, 15, 15),
1401	[ALE_DEFAULT_THREAD_VAL]	= REG_FIELD(ALE_THREAD_DEF, 0, 5),
1402
1403	/* CPSW_ALE_THREAD_CTL_REG */
1404	[ALE_THREAD_CLASS_INDEX] = REG_FIELD(ALE_THREAD_CTL, 0, 4),
1405
1406	/* CPSW_ALE_THREAD_VAL_REG */
1407	[ALE_THREAD_ENABLE]	= REG_FIELD(ALE_THREAD_VAL, 15, 15),
1408	[ALE_THREAD_VALUE]	= REG_FIELD(ALE_THREAD_VAL, 0, 5),
1409};
1410
1411static const struct cpsw_ale_dev_id cpsw_ale_id_match[] = {
1412	{
1413		/* am3/4/5, dra7. dm814x, 66ak2hk-gbe */
1414		.dev_id = "cpsw",
1415		.tbl_entries = 1024,
1416		.reg_fields = ale_fields_cpsw,
1417		.num_fields = ARRAY_SIZE(ale_fields_cpsw),
1418		.vlan_entry_tbl = vlan_entry_cpsw,
1419	},
1420	{
1421		/* 66ak2h_xgbe */
1422		.dev_id = "66ak2h-xgbe",
1423		.tbl_entries = 2048,
1424		.reg_fields = ale_fields_cpsw,
1425		.num_fields = ARRAY_SIZE(ale_fields_cpsw),
1426		.vlan_entry_tbl = vlan_entry_cpsw,
1427	},
1428	{
1429		.dev_id = "66ak2el",
1430		.features = CPSW_ALE_F_STATUS_REG,
1431		.reg_fields = ale_fields_cpsw_nu,
1432		.num_fields = ARRAY_SIZE(ale_fields_cpsw_nu),
1433		.nu_switch_ale = true,
1434		.vlan_entry_tbl = vlan_entry_nu,
1435	},
1436	{
1437		.dev_id = "66ak2g",
1438		.features = CPSW_ALE_F_STATUS_REG,
1439		.tbl_entries = 64,
1440		.reg_fields = ale_fields_cpsw_nu,
1441		.num_fields = ARRAY_SIZE(ale_fields_cpsw_nu),
1442		.nu_switch_ale = true,
1443		.vlan_entry_tbl = vlan_entry_nu,
1444	},
1445	{
1446		.dev_id = "am65x-cpsw2g",
1447		.features = CPSW_ALE_F_STATUS_REG | CPSW_ALE_F_HW_AUTOAGING,
1448		.tbl_entries = 64,
1449		.reg_fields = ale_fields_cpsw_nu,
1450		.num_fields = ARRAY_SIZE(ale_fields_cpsw_nu),
1451		.nu_switch_ale = true,
1452		.vlan_entry_tbl = vlan_entry_nu,
1453	},
1454	{
1455		.dev_id = "j721e-cpswxg",
1456		.features = CPSW_ALE_F_STATUS_REG | CPSW_ALE_F_HW_AUTOAGING,
1457		.reg_fields = ale_fields_cpsw_nu,
1458		.num_fields = ARRAY_SIZE(ale_fields_cpsw_nu),
1459		.vlan_entry_tbl = vlan_entry_k3_cpswxg,
1460	},
1461	{
1462		.dev_id = "am64-cpswxg",
1463		.features = CPSW_ALE_F_STATUS_REG | CPSW_ALE_F_HW_AUTOAGING,
1464		.reg_fields = ale_fields_cpsw_nu,
1465		.num_fields = ARRAY_SIZE(ale_fields_cpsw_nu),
1466		.vlan_entry_tbl = vlan_entry_k3_cpswxg,
1467		.tbl_entries = 512,
1468	},
1469	{ },
1470};
1471
1472static const struct
1473cpsw_ale_dev_id *cpsw_ale_match_id(const struct cpsw_ale_dev_id *id,
1474				   const char *dev_id)
1475{
1476	if (!dev_id)
1477		return NULL;
1478
1479	while (id->dev_id) {
1480		if (strcmp(dev_id, id->dev_id) == 0)
1481			return id;
1482		id++;
1483	}
1484	return NULL;
1485}
1486
1487static const struct regmap_config ale_regmap_cfg = {
1488	.reg_bits = 32,
1489	.val_bits = 32,
1490	.reg_stride = 4,
1491	.name = "cpsw-ale",
1492};
1493
1494static int cpsw_ale_regfield_init(struct cpsw_ale *ale)
1495{
1496	const struct reg_field *reg_fields = ale->params.reg_fields;
1497	struct device *dev = ale->params.dev;
1498	struct regmap *regmap = ale->regmap;
1499	int i;
1500
1501	for (i = 0; i < ale->params.num_fields; i++) {
1502		ale->fields[i] = devm_regmap_field_alloc(dev, regmap,
1503							 reg_fields[i]);
1504		if (IS_ERR(ale->fields[i])) {
1505			dev_err(dev, "Unable to allocate regmap field %d\n", i);
1506			return PTR_ERR(ale->fields[i]);
1507		}
1508	}
1509
1510	return 0;
1511}
1512
1513struct cpsw_ale *cpsw_ale_create(struct cpsw_ale_params *params)
1514{
1515	u32 ale_entries, rev_major, rev_minor, policers;
1516	const struct cpsw_ale_dev_id *ale_dev_id;
1517	struct cpsw_ale *ale;
1518	int ret;
1519
1520	ale_dev_id = cpsw_ale_match_id(cpsw_ale_id_match, params->dev_id);
1521	if (!ale_dev_id)
1522		return ERR_PTR(-EINVAL);
1523
1524	params->ale_entries = ale_dev_id->tbl_entries;
 
1525	params->nu_switch_ale = ale_dev_id->nu_switch_ale;
1526	params->reg_fields = ale_dev_id->reg_fields;
1527	params->num_fields = ale_dev_id->num_fields;
1528
1529	ale = devm_kzalloc(params->dev, sizeof(*ale), GFP_KERNEL);
1530	if (!ale)
1531		return ERR_PTR(-ENOMEM);
1532	ale->regmap = devm_regmap_init_mmio(params->dev, params->ale_regs,
1533					    &ale_regmap_cfg);
1534	if (IS_ERR(ale->regmap)) {
1535		dev_err(params->dev, "Couldn't create CPSW ALE regmap\n");
1536		return ERR_PTR(-ENOMEM);
1537	}
1538
1539	ale->params = *params;
1540	ret = cpsw_ale_regfield_init(ale);
1541	if (ret)
1542		return ERR_PTR(ret);
1543
1544	ale->p0_untag_vid_mask = devm_bitmap_zalloc(params->dev, VLAN_N_VID,
1545						    GFP_KERNEL);
 
 
1546	if (!ale->p0_untag_vid_mask)
1547		return ERR_PTR(-ENOMEM);
1548
 
1549	ale->ageout = ale->params.ale_ageout * HZ;
1550	ale->features = ale_dev_id->features;
1551	ale->vlan_entry_tbl = ale_dev_id->vlan_entry_tbl;
1552
1553	regmap_field_read(ale->fields[MINOR_VER], &rev_minor);
1554	regmap_field_read(ale->fields[MAJOR_VER], &rev_major);
1555	ale->version = rev_major << 8 | rev_minor;
 
1556	dev_info(ale->params.dev, "initialized cpsw ale version %d.%d\n",
1557		 rev_major, rev_minor);
 
1558
1559	if (ale->features & CPSW_ALE_F_STATUS_REG &&
1560	    !ale->params.ale_entries) {
1561		regmap_field_read(ale->fields[ALE_ENTRIES], &ale_entries);
 
 
1562		/* ALE available on newer NetCP switches has introduced
1563		 * a register, ALE_STATUS, to indicate the size of ALE
1564		 * table which shows the size as a multiple of 1024 entries.
1565		 * For these, params.ale_entries will be set to zero. So
1566		 * read the register and update the value of ale_entries.
1567		 * return error if ale_entries is zero in ALE_STATUS.
1568		 */
1569		if (!ale_entries)
1570			return ERR_PTR(-EINVAL);
1571
1572		ale_entries *= ALE_TABLE_SIZE_MULTIPLIER;
1573		ale->params.ale_entries = ale_entries;
1574	}
1575
1576	if (ale->features & CPSW_ALE_F_STATUS_REG &&
1577	    !ale->params.num_policers) {
1578		regmap_field_read(ale->fields[ALE_POLICERS], &policers);
1579		if (!policers)
1580			return ERR_PTR(-EINVAL);
1581
1582		policers *= ALE_POLICER_SIZE_MULTIPLIER;
1583		ale->params.num_policers = policers;
1584	}
1585
1586	dev_info(ale->params.dev,
1587		 "ALE Table size %ld, Policers %ld\n", ale->params.ale_entries,
1588		 ale->params.num_policers);
1589
1590	/* set default bits for existing h/w */
1591	ale->port_mask_bits = ale->params.ale_ports;
1592	ale->port_num_bits = order_base_2(ale->params.ale_ports);
1593	ale->vlan_field_bits = ale->params.ale_ports;
1594
1595	/* Set defaults override for ALE on NetCP NU switch and for version
1596	 * 1R3
1597	 */
1598	if (ale->params.nu_switch_ale) {
1599		/* Separate registers for unknown vlan configuration.
1600		 * Also there are N bits, where N is number of ale
1601		 * ports and shift value should be 0
1602		 */
1603		ale_controls[ALE_PORT_UNKNOWN_VLAN_MEMBER].bits =
1604					ale->params.ale_ports;
1605		ale_controls[ALE_PORT_UNKNOWN_VLAN_MEMBER].offset =
1606					ALE_UNKNOWNVLAN_MEMBER;
1607		ale_controls[ALE_PORT_UNKNOWN_MCAST_FLOOD].bits =
1608					ale->params.ale_ports;
1609		ale_controls[ALE_PORT_UNKNOWN_MCAST_FLOOD].shift = 0;
1610		ale_controls[ALE_PORT_UNKNOWN_MCAST_FLOOD].offset =
1611					ALE_UNKNOWNVLAN_UNREG_MCAST_FLOOD;
1612		ale_controls[ALE_PORT_UNKNOWN_REG_MCAST_FLOOD].bits =
1613					ale->params.ale_ports;
1614		ale_controls[ALE_PORT_UNKNOWN_REG_MCAST_FLOOD].shift = 0;
1615		ale_controls[ALE_PORT_UNKNOWN_REG_MCAST_FLOOD].offset =
1616					ALE_UNKNOWNVLAN_REG_MCAST_FLOOD;
1617		ale_controls[ALE_PORT_UNTAGGED_EGRESS].bits =
1618					ale->params.ale_ports;
1619		ale_controls[ALE_PORT_UNTAGGED_EGRESS].shift = 0;
1620		ale_controls[ALE_PORT_UNTAGGED_EGRESS].offset =
1621					ALE_UNKNOWNVLAN_FORCE_UNTAG_EGRESS;
1622	}
1623
1624	cpsw_ale_control_set(ale, 0, ALE_CLEAR, 1);
1625	return ale;
1626}
1627
1628void cpsw_ale_dump(struct cpsw_ale *ale, u32 *data)
1629{
1630	int i;
1631
1632	for (i = 0; i < ale->params.ale_entries; i++) {
1633		cpsw_ale_read(ale, i, data);
1634		data += ALE_ENTRY_WORDS;
1635	}
1636}
1637
1638void cpsw_ale_restore(struct cpsw_ale *ale, u32 *data)
1639{
1640	int i;
1641
1642	for (i = 0; i < ale->params.ale_entries; i++) {
1643		cpsw_ale_write(ale, i, data);
1644		data += ALE_ENTRY_WORDS;
1645	}
1646}
1647
1648u32 cpsw_ale_get_num_entries(struct cpsw_ale *ale)
1649{
1650	return ale ? ale->params.ale_entries : 0;
1651}
1652
1653/* Reads the specified policer index into ALE POLICER registers */
1654static void cpsw_ale_policer_read_idx(struct cpsw_ale *ale, u32 idx)
1655{
1656	idx &= ALE_POLICER_TBL_INDEX_MASK;
1657	writel_relaxed(idx, ale->params.ale_regs + ALE_POLICER_TBL_CTL);
1658}
1659
1660/* Writes the ALE POLICER registers into the specified policer index */
1661static void cpsw_ale_policer_write_idx(struct cpsw_ale *ale, u32 idx)
1662{
1663	idx &= ALE_POLICER_TBL_INDEX_MASK;
1664	idx |= ALE_POLICER_TBL_WRITE_ENABLE;
1665	writel_relaxed(idx, ale->params.ale_regs + ALE_POLICER_TBL_CTL);
1666}
1667
1668/* enables/disables the custom thread value for the specified policer index */
1669static void cpsw_ale_policer_thread_idx_enable(struct cpsw_ale *ale, u32 idx,
1670					       u32 thread_id, bool enable)
1671{
1672	regmap_field_write(ale->fields[ALE_THREAD_CLASS_INDEX], idx);
1673	regmap_field_write(ale->fields[ALE_THREAD_VALUE], thread_id);
1674	regmap_field_write(ale->fields[ALE_THREAD_ENABLE], enable ? 1 : 0);
1675}
1676
1677/* Disable all policer entries and thread mappings */
1678static void cpsw_ale_policer_reset(struct cpsw_ale *ale)
1679{
1680	int i;
1681
1682	for (i = 0; i < ale->params.num_policers ; i++) {
1683		cpsw_ale_policer_read_idx(ale, i);
1684		regmap_field_write(ale->fields[POL_PORT_MEN], 0);
1685		regmap_field_write(ale->fields[POL_PRI_MEN], 0);
1686		regmap_field_write(ale->fields[POL_OUI_MEN], 0);
1687		regmap_field_write(ale->fields[POL_DST_MEN], 0);
1688		regmap_field_write(ale->fields[POL_SRC_MEN], 0);
1689		regmap_field_write(ale->fields[POL_OVLAN_MEN], 0);
1690		regmap_field_write(ale->fields[POL_IVLAN_MEN], 0);
1691		regmap_field_write(ale->fields[POL_ETHERTYPE_MEN], 0);
1692		regmap_field_write(ale->fields[POL_IPSRC_MEN], 0);
1693		regmap_field_write(ale->fields[POL_IPDST_MEN], 0);
1694		regmap_field_write(ale->fields[POL_EN], 0);
1695		regmap_field_write(ale->fields[POL_RED_DROP_EN], 0);
1696		regmap_field_write(ale->fields[POL_YELLOW_DROP_EN], 0);
1697		regmap_field_write(ale->fields[POL_PRIORITY_THREAD_EN], 0);
1698
1699		cpsw_ale_policer_thread_idx_enable(ale, i, 0, 0);
1700	}
1701}
1702
1703/* Default classifier is to map 8 user priorities to N receive channels */
1704void cpsw_ale_classifier_setup_default(struct cpsw_ale *ale, int num_rx_ch)
1705{
1706	int pri, idx;
1707
1708	/* Reference:
1709	 * IEEE802.1Q-2014, Standard for Local and metropolitan area networks
1710	 *    Table I-2 - Traffic type acronyms
1711	 *    Table I-3 - Defining traffic types
1712	 * Section I.4 Traffic types and priority values, states:
1713	 * "0 is thus used both for default priority and for Best Effort, and
1714	 *  Background is associated with a priority value of 1. This means
1715	 * that the value 1 effectively communicates a lower priority than 0."
1716	 *
1717	 * In the table below, Priority Code Point (PCP) 0 is assigned
1718	 * to a higher priority thread than PCP 1 wherever possible.
1719	 * The table maps which thread the PCP traffic needs to be
1720	 * sent to for a given number of threads (RX channels). Upper threads
1721	 * have higher priority.
1722	 * e.g. if number of threads is 8 then user priority 0 will map to
1723	 * pri_thread_map[8-1][0] i.e. thread 1
1724	 */
1725
1726	int pri_thread_map[8][8] = {   /* BK,BE,EE,CA,VI,VO,IC,NC */
1727					{ 0, 0, 0, 0, 0, 0, 0, 0, },
1728					{ 0, 0, 0, 0, 1, 1, 1, 1, },
1729					{ 0, 0, 0, 0, 1, 1, 2, 2, },
1730					{ 0, 0, 1, 1, 2, 2, 3, 3, },
1731					{ 0, 0, 1, 1, 2, 2, 3, 4, },
1732					{ 1, 0, 2, 2, 3, 3, 4, 5, },
1733					{ 1, 0, 2, 3, 4, 4, 5, 6, },
1734					{ 1, 0, 2, 3, 4, 5, 6, 7 } };
1735
1736	cpsw_ale_policer_reset(ale);
1737
1738	/* use first 8 classifiers to map 8 (DSCP/PCP) priorities to channels */
1739	for (pri = 0; pri < 8; pri++) {
1740		idx = pri;
1741
1742		/* Classifier 'idx' match on priority 'pri' */
1743		cpsw_ale_policer_read_idx(ale, idx);
1744		regmap_field_write(ale->fields[POL_PRI_VAL], pri);
1745		regmap_field_write(ale->fields[POL_PRI_MEN], 1);
1746		cpsw_ale_policer_write_idx(ale, idx);
1747
1748		/* Map Classifier 'idx' to thread provided by the map */
1749		cpsw_ale_policer_thread_idx_enable(ale, idx,
1750						   pri_thread_map[num_rx_ch - 1][pri],
1751						   1);
1752	}
1753}
v5.14.15
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Texas Instruments N-Port Ethernet Switch Address Lookup Engine
   4 *
   5 * Copyright (C) 2012 Texas Instruments
   6 *
   7 */
   8#include <linux/bitmap.h>
   9#include <linux/if_vlan.h>
  10#include <linux/kernel.h>
  11#include <linux/module.h>
  12#include <linux/platform_device.h>
 
  13#include <linux/seq_file.h>
  14#include <linux/slab.h>
  15#include <linux/err.h>
  16#include <linux/io.h>
  17#include <linux/stat.h>
  18#include <linux/sysfs.h>
  19#include <linux/etherdevice.h>
  20
  21#include "cpsw_ale.h"
  22
  23#define BITMASK(bits)		(BIT(bits) - 1)
  24
  25#define ALE_VERSION_MAJOR(rev, mask) (((rev) >> 8) & (mask))
  26#define ALE_VERSION_MINOR(rev)	(rev & 0xff)
  27#define ALE_VERSION_1R3		0x0103
  28#define ALE_VERSION_1R4		0x0104
  29
  30/* ALE Registers */
  31#define ALE_IDVER		0x00
  32#define ALE_STATUS		0x04
  33#define ALE_CONTROL		0x08
  34#define ALE_PRESCALE		0x10
  35#define ALE_AGING_TIMER		0x14
  36#define ALE_UNKNOWNVLAN		0x18
  37#define ALE_TABLE_CONTROL	0x20
  38#define ALE_TABLE		0x34
  39#define ALE_PORTCTL		0x40
  40
  41/* ALE NetCP NU switch specific Registers */
  42#define ALE_UNKNOWNVLAN_MEMBER			0x90
  43#define ALE_UNKNOWNVLAN_UNREG_MCAST_FLOOD	0x94
  44#define ALE_UNKNOWNVLAN_REG_MCAST_FLOOD		0x98
  45#define ALE_UNKNOWNVLAN_FORCE_UNTAG_EGRESS	0x9C
  46#define ALE_VLAN_MASK_MUX(reg)			(0xc0 + (0x4 * (reg)))
  47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  48#define AM65_CPSW_ALE_THREAD_DEF_REG 0x134
  49
  50/* ALE_AGING_TIMER */
  51#define ALE_AGING_TIMER_MASK	GENMASK(23, 0)
  52
 
 
  53/**
  54 * struct ale_entry_fld - The ALE tbl entry field description
  55 * @start_bit: field start bit
  56 * @num_bits: field bit length
  57 * @flags: field flags
  58 */
  59struct ale_entry_fld {
  60	u8 start_bit;
  61	u8 num_bits;
  62	u8 flags;
  63};
  64
  65enum {
  66	CPSW_ALE_F_STATUS_REG = BIT(0), /* Status register present */
  67	CPSW_ALE_F_HW_AUTOAGING = BIT(1), /* HW auto aging */
  68
  69	CPSW_ALE_F_COUNT
  70};
  71
  72/**
  73 * struct cpsw_ale_dev_id - The ALE version/SoC specific configuration
  74 * @dev_id: ALE version/SoC id
  75 * @features: features supported by ALE
  76 * @tbl_entries: number of ALE entries
  77 * @major_ver_mask: mask of ALE Major Version Value in ALE_IDVER reg.
 
  78 * @nu_switch_ale: NU Switch ALE
  79 * @vlan_entry_tbl: ALE vlan entry fields description tbl
  80 */
  81struct cpsw_ale_dev_id {
  82	const char *dev_id;
  83	u32 features;
  84	u32 tbl_entries;
  85	u32 major_ver_mask;
 
  86	bool nu_switch_ale;
  87	const struct ale_entry_fld *vlan_entry_tbl;
  88};
  89
  90#define ALE_TABLE_WRITE		BIT(31)
  91
  92#define ALE_TYPE_FREE			0
  93#define ALE_TYPE_ADDR			1
  94#define ALE_TYPE_VLAN			2
  95#define ALE_TYPE_VLAN_ADDR		3
  96
  97#define ALE_UCAST_PERSISTANT		0
  98#define ALE_UCAST_UNTOUCHED		1
  99#define ALE_UCAST_OUI			2
 100#define ALE_UCAST_TOUCHED		3
 101
 102#define ALE_TABLE_SIZE_MULTIPLIER	1024
 103#define ALE_STATUS_SIZE_MASK		0x1f
 104
 105static inline int cpsw_ale_get_field(u32 *ale_entry, u32 start, u32 bits)
 106{
 107	int idx;
 
 108
 109	idx    = start / 32;
 
 
 
 
 
 
 110	start -= idx * 32;
 111	idx    = 2 - idx; /* flip */
 112	return (ale_entry[idx] >> start) & BITMASK(bits);
 113}
 114
 115static inline void cpsw_ale_set_field(u32 *ale_entry, u32 start, u32 bits,
 116				      u32 value)
 117{
 118	int idx;
 119
 120	value &= BITMASK(bits);
 121	idx    = start / 32;
 
 
 
 
 
 
 
 122	start -= idx * 32;
 123	idx    = 2 - idx; /* flip */
 124	ale_entry[idx] &= ~(BITMASK(bits) << start);
 125	ale_entry[idx] |=  (value << start);
 126}
 127
 128#define DEFINE_ALE_FIELD(name, start, bits)				\
 129static inline int cpsw_ale_get_##name(u32 *ale_entry)			\
 130{									\
 131	return cpsw_ale_get_field(ale_entry, start, bits);		\
 132}									\
 
 
 133static inline void cpsw_ale_set_##name(u32 *ale_entry, u32 value)	\
 134{									\
 135	cpsw_ale_set_field(ale_entry, start, bits, value);		\
 136}
 137
 138#define DEFINE_ALE_FIELD1(name, start)					\
 
 
 
 
 139static inline int cpsw_ale_get_##name(u32 *ale_entry, u32 bits)		\
 140{									\
 141	return cpsw_ale_get_field(ale_entry, start, bits);		\
 142}									\
 
 
 143static inline void cpsw_ale_set_##name(u32 *ale_entry, u32 value,	\
 144		u32 bits)						\
 145{									\
 146	cpsw_ale_set_field(ale_entry, start, bits, value);		\
 147}
 148
 
 
 
 
 149enum {
 150	ALE_ENT_VID_MEMBER_LIST = 0,
 151	ALE_ENT_VID_UNREG_MCAST_MSK,
 152	ALE_ENT_VID_REG_MCAST_MSK,
 153	ALE_ENT_VID_FORCE_UNTAGGED_MSK,
 154	ALE_ENT_VID_UNREG_MCAST_IDX,
 155	ALE_ENT_VID_REG_MCAST_IDX,
 156	ALE_ENT_VID_LAST,
 157};
 158
 159#define ALE_FLD_ALLOWED			BIT(0)
 160#define ALE_FLD_SIZE_PORT_MASK_BITS	BIT(1)
 161#define ALE_FLD_SIZE_PORT_NUM_BITS	BIT(2)
 162
 163#define ALE_ENTRY_FLD(id, start, bits)	\
 164[id] = {				\
 165	.start_bit = start,		\
 166	.num_bits = bits,		\
 167	.flags = ALE_FLD_ALLOWED,	\
 168}
 169
 170#define ALE_ENTRY_FLD_DYN_MSK_SIZE(id, start)	\
 171[id] = {					\
 172	.start_bit = start,			\
 173	.num_bits = 0,				\
 174	.flags = ALE_FLD_ALLOWED |		\
 175		 ALE_FLD_SIZE_PORT_MASK_BITS,	\
 176}
 177
 178/* dm814x, am3/am4/am5, k2hk */
 179static const struct ale_entry_fld vlan_entry_cpsw[ALE_ENT_VID_LAST] = {
 180	ALE_ENTRY_FLD(ALE_ENT_VID_MEMBER_LIST, 0, 3),
 181	ALE_ENTRY_FLD(ALE_ENT_VID_UNREG_MCAST_MSK, 8, 3),
 182	ALE_ENTRY_FLD(ALE_ENT_VID_REG_MCAST_MSK, 16, 3),
 183	ALE_ENTRY_FLD(ALE_ENT_VID_FORCE_UNTAGGED_MSK, 24, 3),
 184};
 185
 186/* k2e/k2l, k3 am65/j721e cpsw2g  */
 187static const struct ale_entry_fld vlan_entry_nu[ALE_ENT_VID_LAST] = {
 188	ALE_ENTRY_FLD_DYN_MSK_SIZE(ALE_ENT_VID_MEMBER_LIST, 0),
 189	ALE_ENTRY_FLD(ALE_ENT_VID_UNREG_MCAST_IDX, 20, 3),
 190	ALE_ENTRY_FLD_DYN_MSK_SIZE(ALE_ENT_VID_FORCE_UNTAGGED_MSK, 24),
 191	ALE_ENTRY_FLD(ALE_ENT_VID_REG_MCAST_IDX, 44, 3),
 192};
 193
 194/* K3 j721e/j7200 cpsw9g/5g, am64x cpsw3g  */
 195static const struct ale_entry_fld vlan_entry_k3_cpswxg[] = {
 196	ALE_ENTRY_FLD_DYN_MSK_SIZE(ALE_ENT_VID_MEMBER_LIST, 0),
 197	ALE_ENTRY_FLD_DYN_MSK_SIZE(ALE_ENT_VID_UNREG_MCAST_MSK, 12),
 198	ALE_ENTRY_FLD_DYN_MSK_SIZE(ALE_ENT_VID_FORCE_UNTAGGED_MSK, 24),
 199	ALE_ENTRY_FLD_DYN_MSK_SIZE(ALE_ENT_VID_REG_MCAST_MSK, 36),
 200};
 201
 202DEFINE_ALE_FIELD(entry_type,		60,	2)
 203DEFINE_ALE_FIELD(vlan_id,		48,	12)
 204DEFINE_ALE_FIELD(mcast_state,		62,	2)
 205DEFINE_ALE_FIELD1(port_mask,		66)
 206DEFINE_ALE_FIELD(super,			65,	1)
 207DEFINE_ALE_FIELD(ucast_type,		62,     2)
 208DEFINE_ALE_FIELD1(port_num,		66)
 209DEFINE_ALE_FIELD(blocked,		65,     1)
 210DEFINE_ALE_FIELD(secure,		64,     1)
 211DEFINE_ALE_FIELD(mcast,			40,	1)
 212
 213#define NU_VLAN_UNREG_MCAST_IDX	1
 214
 215static int cpsw_ale_entry_get_fld(struct cpsw_ale *ale,
 216				  u32 *ale_entry,
 217				  const struct ale_entry_fld *entry_tbl,
 218				  int fld_id)
 219{
 220	const struct ale_entry_fld *entry_fld;
 221	u32 bits;
 222
 223	if (!ale || !ale_entry)
 224		return -EINVAL;
 225
 226	entry_fld = &entry_tbl[fld_id];
 227	if (!(entry_fld->flags & ALE_FLD_ALLOWED)) {
 228		dev_err(ale->params.dev, "get: wrong ale fld id %d\n", fld_id);
 229		return -ENOENT;
 230	}
 231
 232	bits = entry_fld->num_bits;
 233	if (entry_fld->flags & ALE_FLD_SIZE_PORT_MASK_BITS)
 234		bits = ale->port_mask_bits;
 235
 236	return cpsw_ale_get_field(ale_entry, entry_fld->start_bit, bits);
 237}
 238
 239static void cpsw_ale_entry_set_fld(struct cpsw_ale *ale,
 240				   u32 *ale_entry,
 241				   const struct ale_entry_fld *entry_tbl,
 242				   int fld_id,
 243				   u32 value)
 244{
 245	const struct ale_entry_fld *entry_fld;
 246	u32 bits;
 247
 248	if (!ale || !ale_entry)
 249		return;
 250
 251	entry_fld = &entry_tbl[fld_id];
 252	if (!(entry_fld->flags & ALE_FLD_ALLOWED)) {
 253		dev_err(ale->params.dev, "set: wrong ale fld id %d\n", fld_id);
 254		return;
 255	}
 256
 257	bits = entry_fld->num_bits;
 258	if (entry_fld->flags & ALE_FLD_SIZE_PORT_MASK_BITS)
 259		bits = ale->port_mask_bits;
 260
 261	cpsw_ale_set_field(ale_entry, entry_fld->start_bit, bits, value);
 262}
 263
 264static int cpsw_ale_vlan_get_fld(struct cpsw_ale *ale,
 265				 u32 *ale_entry,
 266				 int fld_id)
 267{
 268	return cpsw_ale_entry_get_fld(ale, ale_entry,
 269				      ale->vlan_entry_tbl, fld_id);
 270}
 271
 272static void cpsw_ale_vlan_set_fld(struct cpsw_ale *ale,
 273				  u32 *ale_entry,
 274				  int fld_id,
 275				  u32 value)
 276{
 277	cpsw_ale_entry_set_fld(ale, ale_entry,
 278			       ale->vlan_entry_tbl, fld_id, value);
 279}
 280
 281/* The MAC address field in the ALE entry cannot be macroized as above */
 282static inline void cpsw_ale_get_addr(u32 *ale_entry, u8 *addr)
 283{
 284	int i;
 285
 286	for (i = 0; i < 6; i++)
 287		addr[i] = cpsw_ale_get_field(ale_entry, 40 - 8*i, 8);
 288}
 289
 290static inline void cpsw_ale_set_addr(u32 *ale_entry, const u8 *addr)
 291{
 292	int i;
 293
 294	for (i = 0; i < 6; i++)
 295		cpsw_ale_set_field(ale_entry, 40 - 8*i, 8, addr[i]);
 296}
 297
 298static int cpsw_ale_read(struct cpsw_ale *ale, int idx, u32 *ale_entry)
 299{
 300	int i;
 301
 302	WARN_ON(idx > ale->params.ale_entries);
 303
 304	writel_relaxed(idx, ale->params.ale_regs + ALE_TABLE_CONTROL);
 305
 306	for (i = 0; i < ALE_ENTRY_WORDS; i++)
 307		ale_entry[i] = readl_relaxed(ale->params.ale_regs +
 308					     ALE_TABLE + 4 * i);
 309
 310	return idx;
 311}
 312
 313static int cpsw_ale_write(struct cpsw_ale *ale, int idx, u32 *ale_entry)
 314{
 315	int i;
 316
 317	WARN_ON(idx > ale->params.ale_entries);
 318
 319	for (i = 0; i < ALE_ENTRY_WORDS; i++)
 320		writel_relaxed(ale_entry[i], ale->params.ale_regs +
 321			       ALE_TABLE + 4 * i);
 322
 323	writel_relaxed(idx | ALE_TABLE_WRITE, ale->params.ale_regs +
 324		       ALE_TABLE_CONTROL);
 325
 326	return idx;
 327}
 328
 329static int cpsw_ale_match_addr(struct cpsw_ale *ale, const u8 *addr, u16 vid)
 330{
 331	u32 ale_entry[ALE_ENTRY_WORDS];
 332	int type, idx;
 333
 334	for (idx = 0; idx < ale->params.ale_entries; idx++) {
 335		u8 entry_addr[6];
 336
 337		cpsw_ale_read(ale, idx, ale_entry);
 338		type = cpsw_ale_get_entry_type(ale_entry);
 339		if (type != ALE_TYPE_ADDR && type != ALE_TYPE_VLAN_ADDR)
 340			continue;
 341		if (cpsw_ale_get_vlan_id(ale_entry) != vid)
 342			continue;
 343		cpsw_ale_get_addr(ale_entry, entry_addr);
 344		if (ether_addr_equal(entry_addr, addr))
 345			return idx;
 346	}
 347	return -ENOENT;
 348}
 349
 350static int cpsw_ale_match_vlan(struct cpsw_ale *ale, u16 vid)
 351{
 352	u32 ale_entry[ALE_ENTRY_WORDS];
 353	int type, idx;
 354
 355	for (idx = 0; idx < ale->params.ale_entries; idx++) {
 356		cpsw_ale_read(ale, idx, ale_entry);
 357		type = cpsw_ale_get_entry_type(ale_entry);
 358		if (type != ALE_TYPE_VLAN)
 359			continue;
 360		if (cpsw_ale_get_vlan_id(ale_entry) == vid)
 361			return idx;
 362	}
 363	return -ENOENT;
 364}
 365
 366static int cpsw_ale_match_free(struct cpsw_ale *ale)
 367{
 368	u32 ale_entry[ALE_ENTRY_WORDS];
 369	int type, idx;
 370
 371	for (idx = 0; idx < ale->params.ale_entries; idx++) {
 372		cpsw_ale_read(ale, idx, ale_entry);
 373		type = cpsw_ale_get_entry_type(ale_entry);
 374		if (type == ALE_TYPE_FREE)
 375			return idx;
 376	}
 377	return -ENOENT;
 378}
 379
 380static int cpsw_ale_find_ageable(struct cpsw_ale *ale)
 381{
 382	u32 ale_entry[ALE_ENTRY_WORDS];
 383	int type, idx;
 384
 385	for (idx = 0; idx < ale->params.ale_entries; idx++) {
 386		cpsw_ale_read(ale, idx, ale_entry);
 387		type = cpsw_ale_get_entry_type(ale_entry);
 388		if (type != ALE_TYPE_ADDR && type != ALE_TYPE_VLAN_ADDR)
 389			continue;
 390		if (cpsw_ale_get_mcast(ale_entry))
 391			continue;
 392		type = cpsw_ale_get_ucast_type(ale_entry);
 393		if (type != ALE_UCAST_PERSISTANT &&
 394		    type != ALE_UCAST_OUI)
 395			return idx;
 396	}
 397	return -ENOENT;
 398}
 399
 400static void cpsw_ale_flush_mcast(struct cpsw_ale *ale, u32 *ale_entry,
 401				 int port_mask)
 402{
 403	int mask;
 404
 405	mask = cpsw_ale_get_port_mask(ale_entry,
 406				      ale->port_mask_bits);
 407	if ((mask & port_mask) == 0)
 408		return; /* ports dont intersect, not interested */
 409	mask &= ~port_mask;
 410
 411	/* free if only remaining port is host port */
 412	if (mask)
 413		cpsw_ale_set_port_mask(ale_entry, mask,
 414				       ale->port_mask_bits);
 415	else
 416		cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
 417}
 418
 419int cpsw_ale_flush_multicast(struct cpsw_ale *ale, int port_mask, int vid)
 420{
 421	u32 ale_entry[ALE_ENTRY_WORDS];
 422	int ret, idx;
 423
 424	for (idx = 0; idx < ale->params.ale_entries; idx++) {
 425		cpsw_ale_read(ale, idx, ale_entry);
 426		ret = cpsw_ale_get_entry_type(ale_entry);
 427		if (ret != ALE_TYPE_ADDR && ret != ALE_TYPE_VLAN_ADDR)
 428			continue;
 429
 430		/* if vid passed is -1 then remove all multicast entry from
 431		 * the table irrespective of vlan id, if a valid vlan id is
 432		 * passed then remove only multicast added to that vlan id.
 433		 * if vlan id doesn't match then move on to next entry.
 434		 */
 435		if (vid != -1 && cpsw_ale_get_vlan_id(ale_entry) != vid)
 436			continue;
 437
 438		if (cpsw_ale_get_mcast(ale_entry)) {
 439			u8 addr[6];
 440
 441			if (cpsw_ale_get_super(ale_entry))
 442				continue;
 443
 444			cpsw_ale_get_addr(ale_entry, addr);
 445			if (!is_broadcast_ether_addr(addr))
 446				cpsw_ale_flush_mcast(ale, ale_entry, port_mask);
 447		}
 448
 449		cpsw_ale_write(ale, idx, ale_entry);
 450	}
 451	return 0;
 452}
 453
 454static inline void cpsw_ale_set_vlan_entry_type(u32 *ale_entry,
 455						int flags, u16 vid)
 456{
 457	if (flags & ALE_VLAN) {
 458		cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_VLAN_ADDR);
 459		cpsw_ale_set_vlan_id(ale_entry, vid);
 460	} else {
 461		cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_ADDR);
 462	}
 463}
 464
 465int cpsw_ale_add_ucast(struct cpsw_ale *ale, const u8 *addr, int port,
 466		       int flags, u16 vid)
 467{
 468	u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
 469	int idx;
 470
 471	cpsw_ale_set_vlan_entry_type(ale_entry, flags, vid);
 472
 473	cpsw_ale_set_addr(ale_entry, addr);
 474	cpsw_ale_set_ucast_type(ale_entry, ALE_UCAST_PERSISTANT);
 475	cpsw_ale_set_secure(ale_entry, (flags & ALE_SECURE) ? 1 : 0);
 476	cpsw_ale_set_blocked(ale_entry, (flags & ALE_BLOCKED) ? 1 : 0);
 477	cpsw_ale_set_port_num(ale_entry, port, ale->port_num_bits);
 478
 479	idx = cpsw_ale_match_addr(ale, addr, (flags & ALE_VLAN) ? vid : 0);
 480	if (idx < 0)
 481		idx = cpsw_ale_match_free(ale);
 482	if (idx < 0)
 483		idx = cpsw_ale_find_ageable(ale);
 484	if (idx < 0)
 485		return -ENOMEM;
 486
 487	cpsw_ale_write(ale, idx, ale_entry);
 488	return 0;
 489}
 490
 491int cpsw_ale_del_ucast(struct cpsw_ale *ale, const u8 *addr, int port,
 492		       int flags, u16 vid)
 493{
 494	u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
 495	int idx;
 496
 497	idx = cpsw_ale_match_addr(ale, addr, (flags & ALE_VLAN) ? vid : 0);
 498	if (idx < 0)
 499		return -ENOENT;
 500
 501	cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
 502	cpsw_ale_write(ale, idx, ale_entry);
 503	return 0;
 504}
 505
 506int cpsw_ale_add_mcast(struct cpsw_ale *ale, const u8 *addr, int port_mask,
 507		       int flags, u16 vid, int mcast_state)
 508{
 509	u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
 510	int idx, mask;
 511
 512	idx = cpsw_ale_match_addr(ale, addr, (flags & ALE_VLAN) ? vid : 0);
 513	if (idx >= 0)
 514		cpsw_ale_read(ale, idx, ale_entry);
 515
 516	cpsw_ale_set_vlan_entry_type(ale_entry, flags, vid);
 517
 518	cpsw_ale_set_addr(ale_entry, addr);
 519	cpsw_ale_set_super(ale_entry, (flags & ALE_SUPER) ? 1 : 0);
 520	cpsw_ale_set_mcast_state(ale_entry, mcast_state);
 521
 522	mask = cpsw_ale_get_port_mask(ale_entry,
 523				      ale->port_mask_bits);
 524	port_mask |= mask;
 525	cpsw_ale_set_port_mask(ale_entry, port_mask,
 526			       ale->port_mask_bits);
 527
 528	if (idx < 0)
 529		idx = cpsw_ale_match_free(ale);
 530	if (idx < 0)
 531		idx = cpsw_ale_find_ageable(ale);
 532	if (idx < 0)
 533		return -ENOMEM;
 534
 535	cpsw_ale_write(ale, idx, ale_entry);
 536	return 0;
 537}
 538
 539int cpsw_ale_del_mcast(struct cpsw_ale *ale, const u8 *addr, int port_mask,
 540		       int flags, u16 vid)
 541{
 542	u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
 543	int mcast_members = 0;
 544	int idx;
 545
 546	idx = cpsw_ale_match_addr(ale, addr, (flags & ALE_VLAN) ? vid : 0);
 547	if (idx < 0)
 548		return -ENOENT;
 549
 550	cpsw_ale_read(ale, idx, ale_entry);
 551
 552	if (port_mask) {
 553		mcast_members = cpsw_ale_get_port_mask(ale_entry,
 554						       ale->port_mask_bits);
 555		mcast_members &= ~port_mask;
 556	}
 557
 558	if (mcast_members)
 559		cpsw_ale_set_port_mask(ale_entry, mcast_members,
 560				       ale->port_mask_bits);
 561	else
 562		cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
 563
 564	cpsw_ale_write(ale, idx, ale_entry);
 565	return 0;
 566}
 567
 568/* ALE NetCP NU switch specific vlan functions */
 569static void cpsw_ale_set_vlan_mcast(struct cpsw_ale *ale, u32 *ale_entry,
 570				    int reg_mcast, int unreg_mcast)
 571{
 572	int idx;
 573
 574	/* Set VLAN registered multicast flood mask */
 575	idx = cpsw_ale_vlan_get_fld(ale, ale_entry,
 576				    ALE_ENT_VID_REG_MCAST_IDX);
 577	writel(reg_mcast, ale->params.ale_regs + ALE_VLAN_MASK_MUX(idx));
 578
 579	/* Set VLAN unregistered multicast flood mask */
 580	idx = cpsw_ale_vlan_get_fld(ale, ale_entry,
 581				    ALE_ENT_VID_UNREG_MCAST_IDX);
 582	writel(unreg_mcast, ale->params.ale_regs + ALE_VLAN_MASK_MUX(idx));
 583}
 584
 585static void cpsw_ale_set_vlan_untag(struct cpsw_ale *ale, u32 *ale_entry,
 586				    u16 vid, int untag_mask)
 587{
 588	cpsw_ale_vlan_set_fld(ale, ale_entry,
 589			      ALE_ENT_VID_FORCE_UNTAGGED_MSK,
 590			      untag_mask);
 591	if (untag_mask & ALE_PORT_HOST)
 592		bitmap_set(ale->p0_untag_vid_mask, vid, 1);
 593	else
 594		bitmap_clear(ale->p0_untag_vid_mask, vid, 1);
 595}
 596
 597int cpsw_ale_add_vlan(struct cpsw_ale *ale, u16 vid, int port_mask, int untag,
 598		      int reg_mcast, int unreg_mcast)
 599{
 600	u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
 601	int idx;
 602
 603	idx = cpsw_ale_match_vlan(ale, vid);
 604	if (idx >= 0)
 605		cpsw_ale_read(ale, idx, ale_entry);
 606
 607	cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_VLAN);
 608	cpsw_ale_set_vlan_id(ale_entry, vid);
 609	cpsw_ale_set_vlan_untag(ale, ale_entry, vid, untag);
 610
 611	if (!ale->params.nu_switch_ale) {
 612		cpsw_ale_vlan_set_fld(ale, ale_entry,
 613				      ALE_ENT_VID_REG_MCAST_MSK, reg_mcast);
 614		cpsw_ale_vlan_set_fld(ale, ale_entry,
 615				      ALE_ENT_VID_UNREG_MCAST_MSK, unreg_mcast);
 616	} else {
 617		cpsw_ale_vlan_set_fld(ale, ale_entry,
 618				      ALE_ENT_VID_UNREG_MCAST_IDX,
 619				      NU_VLAN_UNREG_MCAST_IDX);
 620		cpsw_ale_set_vlan_mcast(ale, ale_entry, reg_mcast, unreg_mcast);
 621	}
 622
 623	cpsw_ale_vlan_set_fld(ale, ale_entry,
 624			      ALE_ENT_VID_MEMBER_LIST, port_mask);
 625
 626	if (idx < 0)
 627		idx = cpsw_ale_match_free(ale);
 628	if (idx < 0)
 629		idx = cpsw_ale_find_ageable(ale);
 630	if (idx < 0)
 631		return -ENOMEM;
 632
 633	cpsw_ale_write(ale, idx, ale_entry);
 634	return 0;
 635}
 636
 637static void cpsw_ale_vlan_del_modify_int(struct cpsw_ale *ale,  u32 *ale_entry,
 638					 u16 vid, int port_mask)
 639{
 640	int reg_mcast, unreg_mcast;
 641	int members, untag;
 642
 643	members = cpsw_ale_vlan_get_fld(ale, ale_entry,
 644					ALE_ENT_VID_MEMBER_LIST);
 645	members &= ~port_mask;
 646	if (!members) {
 647		cpsw_ale_set_vlan_untag(ale, ale_entry, vid, 0);
 648		cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
 649		return;
 650	}
 651
 652	untag = cpsw_ale_vlan_get_fld(ale, ale_entry,
 653				      ALE_ENT_VID_FORCE_UNTAGGED_MSK);
 654	reg_mcast = cpsw_ale_vlan_get_fld(ale, ale_entry,
 655					  ALE_ENT_VID_REG_MCAST_MSK);
 656	unreg_mcast = cpsw_ale_vlan_get_fld(ale, ale_entry,
 657					    ALE_ENT_VID_UNREG_MCAST_MSK);
 658	untag &= members;
 659	reg_mcast &= members;
 660	unreg_mcast &= members;
 661
 662	cpsw_ale_set_vlan_untag(ale, ale_entry, vid, untag);
 663
 664	if (!ale->params.nu_switch_ale) {
 665		cpsw_ale_vlan_set_fld(ale, ale_entry,
 666				      ALE_ENT_VID_REG_MCAST_MSK, reg_mcast);
 667		cpsw_ale_vlan_set_fld(ale, ale_entry,
 668				      ALE_ENT_VID_UNREG_MCAST_MSK, unreg_mcast);
 669	} else {
 670		cpsw_ale_set_vlan_mcast(ale, ale_entry, reg_mcast,
 671					unreg_mcast);
 672	}
 673	cpsw_ale_vlan_set_fld(ale, ale_entry,
 674			      ALE_ENT_VID_MEMBER_LIST, members);
 675}
 676
 677int cpsw_ale_vlan_del_modify(struct cpsw_ale *ale, u16 vid, int port_mask)
 678{
 679	u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
 680	int idx;
 681
 682	idx = cpsw_ale_match_vlan(ale, vid);
 683	if (idx < 0)
 684		return -ENOENT;
 685
 686	cpsw_ale_read(ale, idx, ale_entry);
 687
 688	cpsw_ale_vlan_del_modify_int(ale, ale_entry, vid, port_mask);
 689	cpsw_ale_write(ale, idx, ale_entry);
 690
 691	return 0;
 692}
 693
 694int cpsw_ale_del_vlan(struct cpsw_ale *ale, u16 vid, int port_mask)
 695{
 696	u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
 697	int members, idx;
 698
 699	idx = cpsw_ale_match_vlan(ale, vid);
 700	if (idx < 0)
 701		return -ENOENT;
 702
 703	cpsw_ale_read(ale, idx, ale_entry);
 704
 705	/* if !port_mask - force remove VLAN (legacy).
 706	 * Check if there are other VLAN members ports
 707	 * if no - remove VLAN.
 708	 * if yes it means same VLAN was added to >1 port in multi port mode, so
 709	 * remove port_mask ports from VLAN ALE entry excluding Host port.
 710	 */
 711	members = cpsw_ale_vlan_get_fld(ale, ale_entry, ALE_ENT_VID_MEMBER_LIST);
 712	members &= ~port_mask;
 713
 714	if (!port_mask || !members) {
 715		/* last port or force remove - remove VLAN */
 716		cpsw_ale_set_vlan_untag(ale, ale_entry, vid, 0);
 717		cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
 718	} else {
 719		port_mask &= ~ALE_PORT_HOST;
 720		cpsw_ale_vlan_del_modify_int(ale, ale_entry, vid, port_mask);
 721	}
 722
 723	cpsw_ale_write(ale, idx, ale_entry);
 724
 725	return 0;
 726}
 727
 728int cpsw_ale_vlan_add_modify(struct cpsw_ale *ale, u16 vid, int port_mask,
 729			     int untag_mask, int reg_mask, int unreg_mask)
 730{
 731	u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
 732	int reg_mcast_members, unreg_mcast_members;
 733	int vlan_members, untag_members;
 734	int idx, ret = 0;
 735
 736	idx = cpsw_ale_match_vlan(ale, vid);
 737	if (idx >= 0)
 738		cpsw_ale_read(ale, idx, ale_entry);
 739
 740	vlan_members = cpsw_ale_vlan_get_fld(ale, ale_entry,
 741					     ALE_ENT_VID_MEMBER_LIST);
 742	reg_mcast_members = cpsw_ale_vlan_get_fld(ale, ale_entry,
 743						  ALE_ENT_VID_REG_MCAST_MSK);
 744	unreg_mcast_members =
 745		cpsw_ale_vlan_get_fld(ale, ale_entry,
 746				      ALE_ENT_VID_UNREG_MCAST_MSK);
 747	untag_members = cpsw_ale_vlan_get_fld(ale, ale_entry,
 748					      ALE_ENT_VID_FORCE_UNTAGGED_MSK);
 749
 750	vlan_members |= port_mask;
 751	untag_members = (untag_members & ~port_mask) | untag_mask;
 752	reg_mcast_members = (reg_mcast_members & ~port_mask) | reg_mask;
 753	unreg_mcast_members = (unreg_mcast_members & ~port_mask) | unreg_mask;
 754
 755	ret = cpsw_ale_add_vlan(ale, vid, vlan_members, untag_members,
 756				reg_mcast_members, unreg_mcast_members);
 757	if (ret) {
 758		dev_err(ale->params.dev, "Unable to add vlan\n");
 759		return ret;
 760	}
 761	dev_dbg(ale->params.dev, "port mask 0x%x untag 0x%x\n", vlan_members,
 762		untag_mask);
 763
 764	return ret;
 765}
 766
 767void cpsw_ale_set_unreg_mcast(struct cpsw_ale *ale, int unreg_mcast_mask,
 768			      bool add)
 769{
 770	u32 ale_entry[ALE_ENTRY_WORDS];
 771	int unreg_members = 0;
 772	int type, idx;
 773
 774	for (idx = 0; idx < ale->params.ale_entries; idx++) {
 775		cpsw_ale_read(ale, idx, ale_entry);
 776		type = cpsw_ale_get_entry_type(ale_entry);
 777		if (type != ALE_TYPE_VLAN)
 778			continue;
 779
 780		unreg_members =
 781			cpsw_ale_vlan_get_fld(ale, ale_entry,
 782					      ALE_ENT_VID_UNREG_MCAST_MSK);
 783		if (add)
 784			unreg_members |= unreg_mcast_mask;
 785		else
 786			unreg_members &= ~unreg_mcast_mask;
 787		cpsw_ale_vlan_set_fld(ale, ale_entry,
 788				      ALE_ENT_VID_UNREG_MCAST_MSK,
 789				      unreg_members);
 790		cpsw_ale_write(ale, idx, ale_entry);
 791	}
 792}
 793
 794static void cpsw_ale_vlan_set_unreg_mcast(struct cpsw_ale *ale, u32 *ale_entry,
 795					  int allmulti)
 796{
 797	int unreg_mcast;
 798
 799	unreg_mcast = cpsw_ale_vlan_get_fld(ale, ale_entry,
 800					    ALE_ENT_VID_UNREG_MCAST_MSK);
 801	if (allmulti)
 802		unreg_mcast |= ALE_PORT_HOST;
 803	else
 804		unreg_mcast &= ~ALE_PORT_HOST;
 805
 806	cpsw_ale_vlan_set_fld(ale, ale_entry,
 807			      ALE_ENT_VID_UNREG_MCAST_MSK, unreg_mcast);
 808}
 809
 810static void
 811cpsw_ale_vlan_set_unreg_mcast_idx(struct cpsw_ale *ale, u32 *ale_entry,
 812				  int allmulti)
 813{
 814	int unreg_mcast;
 815	int idx;
 816
 817	idx = cpsw_ale_vlan_get_fld(ale, ale_entry,
 818				    ALE_ENT_VID_UNREG_MCAST_IDX);
 819
 820	unreg_mcast = readl(ale->params.ale_regs + ALE_VLAN_MASK_MUX(idx));
 821
 822	if (allmulti)
 823		unreg_mcast |= ALE_PORT_HOST;
 824	else
 825		unreg_mcast &= ~ALE_PORT_HOST;
 826
 827	writel(unreg_mcast, ale->params.ale_regs + ALE_VLAN_MASK_MUX(idx));
 828}
 829
 830void cpsw_ale_set_allmulti(struct cpsw_ale *ale, int allmulti, int port)
 831{
 832	u32 ale_entry[ALE_ENTRY_WORDS];
 833	int type, idx;
 834
 835	for (idx = 0; idx < ale->params.ale_entries; idx++) {
 836		int vlan_members;
 837
 838		cpsw_ale_read(ale, idx, ale_entry);
 839		type = cpsw_ale_get_entry_type(ale_entry);
 840		if (type != ALE_TYPE_VLAN)
 841			continue;
 842
 843		vlan_members = cpsw_ale_vlan_get_fld(ale, ale_entry,
 844						     ALE_ENT_VID_MEMBER_LIST);
 845
 846		if (port != -1 && !(vlan_members & BIT(port)))
 847			continue;
 848
 849		if (!ale->params.nu_switch_ale)
 850			cpsw_ale_vlan_set_unreg_mcast(ale, ale_entry, allmulti);
 851		else
 852			cpsw_ale_vlan_set_unreg_mcast_idx(ale, ale_entry,
 853							  allmulti);
 854
 855		cpsw_ale_write(ale, idx, ale_entry);
 856	}
 857}
 858
 859struct ale_control_info {
 860	const char	*name;
 861	int		offset, port_offset;
 862	int		shift, port_shift;
 863	int		bits;
 864};
 865
 866static struct ale_control_info ale_controls[ALE_NUM_CONTROLS] = {
 867	[ALE_ENABLE]		= {
 868		.name		= "enable",
 869		.offset		= ALE_CONTROL,
 870		.port_offset	= 0,
 871		.shift		= 31,
 872		.port_shift	= 0,
 873		.bits		= 1,
 874	},
 875	[ALE_CLEAR]		= {
 876		.name		= "clear",
 877		.offset		= ALE_CONTROL,
 878		.port_offset	= 0,
 879		.shift		= 30,
 880		.port_shift	= 0,
 881		.bits		= 1,
 882	},
 883	[ALE_AGEOUT]		= {
 884		.name		= "ageout",
 885		.offset		= ALE_CONTROL,
 886		.port_offset	= 0,
 887		.shift		= 29,
 888		.port_shift	= 0,
 889		.bits		= 1,
 890	},
 891	[ALE_P0_UNI_FLOOD]	= {
 892		.name		= "port0_unicast_flood",
 893		.offset		= ALE_CONTROL,
 894		.port_offset	= 0,
 895		.shift		= 8,
 896		.port_shift	= 0,
 897		.bits		= 1,
 898	},
 899	[ALE_VLAN_NOLEARN]	= {
 900		.name		= "vlan_nolearn",
 901		.offset		= ALE_CONTROL,
 902		.port_offset	= 0,
 903		.shift		= 7,
 904		.port_shift	= 0,
 905		.bits		= 1,
 906	},
 907	[ALE_NO_PORT_VLAN]	= {
 908		.name		= "no_port_vlan",
 909		.offset		= ALE_CONTROL,
 910		.port_offset	= 0,
 911		.shift		= 6,
 912		.port_shift	= 0,
 913		.bits		= 1,
 914	},
 915	[ALE_OUI_DENY]		= {
 916		.name		= "oui_deny",
 917		.offset		= ALE_CONTROL,
 918		.port_offset	= 0,
 919		.shift		= 5,
 920		.port_shift	= 0,
 921		.bits		= 1,
 922	},
 923	[ALE_BYPASS]		= {
 924		.name		= "bypass",
 925		.offset		= ALE_CONTROL,
 926		.port_offset	= 0,
 927		.shift		= 4,
 928		.port_shift	= 0,
 929		.bits		= 1,
 930	},
 931	[ALE_RATE_LIMIT_TX]	= {
 932		.name		= "rate_limit_tx",
 933		.offset		= ALE_CONTROL,
 934		.port_offset	= 0,
 935		.shift		= 3,
 936		.port_shift	= 0,
 937		.bits		= 1,
 938	},
 939	[ALE_VLAN_AWARE]	= {
 940		.name		= "vlan_aware",
 941		.offset		= ALE_CONTROL,
 942		.port_offset	= 0,
 943		.shift		= 2,
 944		.port_shift	= 0,
 945		.bits		= 1,
 946	},
 947	[ALE_AUTH_ENABLE]	= {
 948		.name		= "auth_enable",
 949		.offset		= ALE_CONTROL,
 950		.port_offset	= 0,
 951		.shift		= 1,
 952		.port_shift	= 0,
 953		.bits		= 1,
 954	},
 955	[ALE_RATE_LIMIT]	= {
 956		.name		= "rate_limit",
 957		.offset		= ALE_CONTROL,
 958		.port_offset	= 0,
 959		.shift		= 0,
 960		.port_shift	= 0,
 961		.bits		= 1,
 962	},
 963	[ALE_PORT_STATE]	= {
 964		.name		= "port_state",
 965		.offset		= ALE_PORTCTL,
 966		.port_offset	= 4,
 967		.shift		= 0,
 968		.port_shift	= 0,
 969		.bits		= 2,
 970	},
 971	[ALE_PORT_DROP_UNTAGGED] = {
 972		.name		= "drop_untagged",
 973		.offset		= ALE_PORTCTL,
 974		.port_offset	= 4,
 975		.shift		= 2,
 976		.port_shift	= 0,
 977		.bits		= 1,
 978	},
 979	[ALE_PORT_DROP_UNKNOWN_VLAN] = {
 980		.name		= "drop_unknown",
 981		.offset		= ALE_PORTCTL,
 982		.port_offset	= 4,
 983		.shift		= 3,
 984		.port_shift	= 0,
 985		.bits		= 1,
 986	},
 987	[ALE_PORT_NOLEARN]	= {
 988		.name		= "nolearn",
 989		.offset		= ALE_PORTCTL,
 990		.port_offset	= 4,
 991		.shift		= 4,
 992		.port_shift	= 0,
 993		.bits		= 1,
 994	},
 995	[ALE_PORT_NO_SA_UPDATE]	= {
 996		.name		= "no_source_update",
 997		.offset		= ALE_PORTCTL,
 998		.port_offset	= 4,
 999		.shift		= 5,
1000		.port_shift	= 0,
1001		.bits		= 1,
1002	},
1003	[ALE_PORT_MACONLY]	= {
1004		.name		= "mac_only_port_mode",
1005		.offset		= ALE_PORTCTL,
1006		.port_offset	= 4,
1007		.shift		= 11,
1008		.port_shift	= 0,
1009		.bits		= 1,
1010	},
1011	[ALE_PORT_MACONLY_CAF]	= {
1012		.name		= "mac_only_port_caf",
1013		.offset		= ALE_PORTCTL,
1014		.port_offset	= 4,
1015		.shift		= 13,
1016		.port_shift	= 0,
1017		.bits		= 1,
1018	},
1019	[ALE_PORT_MCAST_LIMIT]	= {
1020		.name		= "mcast_limit",
1021		.offset		= ALE_PORTCTL,
1022		.port_offset	= 4,
1023		.shift		= 16,
1024		.port_shift	= 0,
1025		.bits		= 8,
1026	},
1027	[ALE_PORT_BCAST_LIMIT]	= {
1028		.name		= "bcast_limit",
1029		.offset		= ALE_PORTCTL,
1030		.port_offset	= 4,
1031		.shift		= 24,
1032		.port_shift	= 0,
1033		.bits		= 8,
1034	},
1035	[ALE_PORT_UNKNOWN_VLAN_MEMBER] = {
1036		.name		= "unknown_vlan_member",
1037		.offset		= ALE_UNKNOWNVLAN,
1038		.port_offset	= 0,
1039		.shift		= 0,
1040		.port_shift	= 0,
1041		.bits		= 6,
1042	},
1043	[ALE_PORT_UNKNOWN_MCAST_FLOOD] = {
1044		.name		= "unknown_mcast_flood",
1045		.offset		= ALE_UNKNOWNVLAN,
1046		.port_offset	= 0,
1047		.shift		= 8,
1048		.port_shift	= 0,
1049		.bits		= 6,
1050	},
1051	[ALE_PORT_UNKNOWN_REG_MCAST_FLOOD] = {
1052		.name		= "unknown_reg_flood",
1053		.offset		= ALE_UNKNOWNVLAN,
1054		.port_offset	= 0,
1055		.shift		= 16,
1056		.port_shift	= 0,
1057		.bits		= 6,
1058	},
1059	[ALE_PORT_UNTAGGED_EGRESS] = {
1060		.name		= "untagged_egress",
1061		.offset		= ALE_UNKNOWNVLAN,
1062		.port_offset	= 0,
1063		.shift		= 24,
1064		.port_shift	= 0,
1065		.bits		= 6,
1066	},
1067	[ALE_DEFAULT_THREAD_ID] = {
1068		.name		= "default_thread_id",
1069		.offset		= AM65_CPSW_ALE_THREAD_DEF_REG,
1070		.port_offset	= 0,
1071		.shift		= 0,
1072		.port_shift	= 0,
1073		.bits		= 6,
1074	},
1075	[ALE_DEFAULT_THREAD_ENABLE] = {
1076		.name		= "default_thread_id_enable",
1077		.offset		= AM65_CPSW_ALE_THREAD_DEF_REG,
1078		.port_offset	= 0,
1079		.shift		= 15,
1080		.port_shift	= 0,
1081		.bits		= 1,
1082	},
1083};
1084
1085int cpsw_ale_control_set(struct cpsw_ale *ale, int port, int control,
1086			 int value)
1087{
1088	const struct ale_control_info *info;
1089	int offset, shift;
1090	u32 tmp, mask;
1091
1092	if (control < 0 || control >= ARRAY_SIZE(ale_controls))
1093		return -EINVAL;
1094
1095	info = &ale_controls[control];
1096	if (info->port_offset == 0 && info->port_shift == 0)
1097		port = 0; /* global, port is a dont care */
1098
1099	if (port < 0 || port >= ale->params.ale_ports)
1100		return -EINVAL;
1101
1102	mask = BITMASK(info->bits);
1103	if (value & ~mask)
1104		return -EINVAL;
1105
1106	offset = info->offset + (port * info->port_offset);
1107	shift  = info->shift  + (port * info->port_shift);
1108
1109	tmp = readl_relaxed(ale->params.ale_regs + offset);
1110	tmp = (tmp & ~(mask << shift)) | (value << shift);
1111	writel_relaxed(tmp, ale->params.ale_regs + offset);
1112
1113	return 0;
1114}
1115
1116int cpsw_ale_control_get(struct cpsw_ale *ale, int port, int control)
1117{
1118	const struct ale_control_info *info;
1119	int offset, shift;
1120	u32 tmp;
1121
1122	if (control < 0 || control >= ARRAY_SIZE(ale_controls))
1123		return -EINVAL;
1124
1125	info = &ale_controls[control];
1126	if (info->port_offset == 0 && info->port_shift == 0)
1127		port = 0; /* global, port is a dont care */
1128
1129	if (port < 0 || port >= ale->params.ale_ports)
1130		return -EINVAL;
1131
1132	offset = info->offset + (port * info->port_offset);
1133	shift  = info->shift  + (port * info->port_shift);
1134
1135	tmp = readl_relaxed(ale->params.ale_regs + offset) >> shift;
1136	return tmp & BITMASK(info->bits);
1137}
1138
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1139static void cpsw_ale_timer(struct timer_list *t)
1140{
1141	struct cpsw_ale *ale = from_timer(ale, t, timer);
1142
1143	cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
1144
1145	if (ale->ageout) {
1146		ale->timer.expires = jiffies + ale->ageout;
1147		add_timer(&ale->timer);
1148	}
1149}
1150
1151static void cpsw_ale_hw_aging_timer_start(struct cpsw_ale *ale)
1152{
1153	u32 aging_timer;
1154
1155	aging_timer = ale->params.bus_freq / 1000000;
1156	aging_timer *= ale->params.ale_ageout;
1157
1158	if (aging_timer & ~ALE_AGING_TIMER_MASK) {
1159		aging_timer = ALE_AGING_TIMER_MASK;
1160		dev_warn(ale->params.dev,
1161			 "ALE aging timer overflow, set to max\n");
1162	}
1163
1164	writel(aging_timer, ale->params.ale_regs + ALE_AGING_TIMER);
1165}
1166
1167static void cpsw_ale_hw_aging_timer_stop(struct cpsw_ale *ale)
1168{
1169	writel(0, ale->params.ale_regs + ALE_AGING_TIMER);
1170}
1171
1172static void cpsw_ale_aging_start(struct cpsw_ale *ale)
1173{
1174	if (!ale->params.ale_ageout)
1175		return;
1176
1177	if (ale->features & CPSW_ALE_F_HW_AUTOAGING) {
1178		cpsw_ale_hw_aging_timer_start(ale);
1179		return;
1180	}
1181
1182	timer_setup(&ale->timer, cpsw_ale_timer, 0);
1183	ale->timer.expires = jiffies + ale->ageout;
1184	add_timer(&ale->timer);
1185}
1186
1187static void cpsw_ale_aging_stop(struct cpsw_ale *ale)
1188{
1189	if (!ale->params.ale_ageout)
1190		return;
1191
1192	if (ale->features & CPSW_ALE_F_HW_AUTOAGING) {
1193		cpsw_ale_hw_aging_timer_stop(ale);
1194		return;
1195	}
1196
1197	del_timer_sync(&ale->timer);
1198}
1199
1200void cpsw_ale_start(struct cpsw_ale *ale)
1201{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1202	cpsw_ale_control_set(ale, 0, ALE_ENABLE, 1);
1203	cpsw_ale_control_set(ale, 0, ALE_CLEAR, 1);
1204
1205	cpsw_ale_aging_start(ale);
1206}
1207
1208void cpsw_ale_stop(struct cpsw_ale *ale)
1209{
1210	cpsw_ale_aging_stop(ale);
1211	cpsw_ale_control_set(ale, 0, ALE_CLEAR, 1);
1212	cpsw_ale_control_set(ale, 0, ALE_ENABLE, 0);
1213}
1214
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1215static const struct cpsw_ale_dev_id cpsw_ale_id_match[] = {
1216	{
1217		/* am3/4/5, dra7. dm814x, 66ak2hk-gbe */
1218		.dev_id = "cpsw",
1219		.tbl_entries = 1024,
1220		.major_ver_mask = 0xff,
 
1221		.vlan_entry_tbl = vlan_entry_cpsw,
1222	},
1223	{
1224		/* 66ak2h_xgbe */
1225		.dev_id = "66ak2h-xgbe",
1226		.tbl_entries = 2048,
1227		.major_ver_mask = 0xff,
 
1228		.vlan_entry_tbl = vlan_entry_cpsw,
1229	},
1230	{
1231		.dev_id = "66ak2el",
1232		.features = CPSW_ALE_F_STATUS_REG,
1233		.major_ver_mask = 0x7,
 
1234		.nu_switch_ale = true,
1235		.vlan_entry_tbl = vlan_entry_nu,
1236	},
1237	{
1238		.dev_id = "66ak2g",
1239		.features = CPSW_ALE_F_STATUS_REG,
1240		.tbl_entries = 64,
1241		.major_ver_mask = 0x7,
 
1242		.nu_switch_ale = true,
1243		.vlan_entry_tbl = vlan_entry_nu,
1244	},
1245	{
1246		.dev_id = "am65x-cpsw2g",
1247		.features = CPSW_ALE_F_STATUS_REG | CPSW_ALE_F_HW_AUTOAGING,
1248		.tbl_entries = 64,
1249		.major_ver_mask = 0x7,
 
1250		.nu_switch_ale = true,
1251		.vlan_entry_tbl = vlan_entry_nu,
1252	},
1253	{
1254		.dev_id = "j721e-cpswxg",
1255		.features = CPSW_ALE_F_STATUS_REG | CPSW_ALE_F_HW_AUTOAGING,
1256		.major_ver_mask = 0x7,
 
1257		.vlan_entry_tbl = vlan_entry_k3_cpswxg,
1258	},
1259	{
1260		.dev_id = "am64-cpswxg",
1261		.features = CPSW_ALE_F_STATUS_REG | CPSW_ALE_F_HW_AUTOAGING,
1262		.major_ver_mask = 0x7,
 
1263		.vlan_entry_tbl = vlan_entry_k3_cpswxg,
1264		.tbl_entries = 512,
1265	},
1266	{ },
1267};
1268
1269static const struct
1270cpsw_ale_dev_id *cpsw_ale_match_id(const struct cpsw_ale_dev_id *id,
1271				   const char *dev_id)
1272{
1273	if (!dev_id)
1274		return NULL;
1275
1276	while (id->dev_id) {
1277		if (strcmp(dev_id, id->dev_id) == 0)
1278			return id;
1279		id++;
1280	}
1281	return NULL;
1282}
1283
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1284struct cpsw_ale *cpsw_ale_create(struct cpsw_ale_params *params)
1285{
 
1286	const struct cpsw_ale_dev_id *ale_dev_id;
1287	struct cpsw_ale *ale;
1288	u32 rev, ale_entries;
1289
1290	ale_dev_id = cpsw_ale_match_id(cpsw_ale_id_match, params->dev_id);
1291	if (!ale_dev_id)
1292		return ERR_PTR(-EINVAL);
1293
1294	params->ale_entries = ale_dev_id->tbl_entries;
1295	params->major_ver_mask = ale_dev_id->major_ver_mask;
1296	params->nu_switch_ale = ale_dev_id->nu_switch_ale;
 
 
1297
1298	ale = devm_kzalloc(params->dev, sizeof(*ale), GFP_KERNEL);
1299	if (!ale)
1300		return ERR_PTR(-ENOMEM);
 
 
 
 
 
 
 
 
 
 
 
1301
1302	ale->p0_untag_vid_mask =
1303		devm_kmalloc_array(params->dev, BITS_TO_LONGS(VLAN_N_VID),
1304				   sizeof(unsigned long),
1305				   GFP_KERNEL);
1306	if (!ale->p0_untag_vid_mask)
1307		return ERR_PTR(-ENOMEM);
1308
1309	ale->params = *params;
1310	ale->ageout = ale->params.ale_ageout * HZ;
1311	ale->features = ale_dev_id->features;
1312	ale->vlan_entry_tbl = ale_dev_id->vlan_entry_tbl;
1313
1314	rev = readl_relaxed(ale->params.ale_regs + ALE_IDVER);
1315	ale->version =
1316		(ALE_VERSION_MAJOR(rev, ale->params.major_ver_mask) << 8) |
1317		 ALE_VERSION_MINOR(rev);
1318	dev_info(ale->params.dev, "initialized cpsw ale version %d.%d\n",
1319		 ALE_VERSION_MAJOR(rev, ale->params.major_ver_mask),
1320		 ALE_VERSION_MINOR(rev));
1321
1322	if (ale->features & CPSW_ALE_F_STATUS_REG &&
1323	    !ale->params.ale_entries) {
1324		ale_entries =
1325			readl_relaxed(ale->params.ale_regs + ALE_STATUS) &
1326			ALE_STATUS_SIZE_MASK;
1327		/* ALE available on newer NetCP switches has introduced
1328		 * a register, ALE_STATUS, to indicate the size of ALE
1329		 * table which shows the size as a multiple of 1024 entries.
1330		 * For these, params.ale_entries will be set to zero. So
1331		 * read the register and update the value of ale_entries.
1332		 * return error if ale_entries is zero in ALE_STATUS.
1333		 */
1334		if (!ale_entries)
1335			return ERR_PTR(-EINVAL);
1336
1337		ale_entries *= ALE_TABLE_SIZE_MULTIPLIER;
1338		ale->params.ale_entries = ale_entries;
1339	}
 
 
 
 
 
 
 
 
 
 
 
1340	dev_info(ale->params.dev,
1341		 "ALE Table size %ld\n", ale->params.ale_entries);
 
1342
1343	/* set default bits for existing h/w */
1344	ale->port_mask_bits = ale->params.ale_ports;
1345	ale->port_num_bits = order_base_2(ale->params.ale_ports);
1346	ale->vlan_field_bits = ale->params.ale_ports;
1347
1348	/* Set defaults override for ALE on NetCP NU switch and for version
1349	 * 1R3
1350	 */
1351	if (ale->params.nu_switch_ale) {
1352		/* Separate registers for unknown vlan configuration.
1353		 * Also there are N bits, where N is number of ale
1354		 * ports and shift value should be 0
1355		 */
1356		ale_controls[ALE_PORT_UNKNOWN_VLAN_MEMBER].bits =
1357					ale->params.ale_ports;
1358		ale_controls[ALE_PORT_UNKNOWN_VLAN_MEMBER].offset =
1359					ALE_UNKNOWNVLAN_MEMBER;
1360		ale_controls[ALE_PORT_UNKNOWN_MCAST_FLOOD].bits =
1361					ale->params.ale_ports;
1362		ale_controls[ALE_PORT_UNKNOWN_MCAST_FLOOD].shift = 0;
1363		ale_controls[ALE_PORT_UNKNOWN_MCAST_FLOOD].offset =
1364					ALE_UNKNOWNVLAN_UNREG_MCAST_FLOOD;
1365		ale_controls[ALE_PORT_UNKNOWN_REG_MCAST_FLOOD].bits =
1366					ale->params.ale_ports;
1367		ale_controls[ALE_PORT_UNKNOWN_REG_MCAST_FLOOD].shift = 0;
1368		ale_controls[ALE_PORT_UNKNOWN_REG_MCAST_FLOOD].offset =
1369					ALE_UNKNOWNVLAN_REG_MCAST_FLOOD;
1370		ale_controls[ALE_PORT_UNTAGGED_EGRESS].bits =
1371					ale->params.ale_ports;
1372		ale_controls[ALE_PORT_UNTAGGED_EGRESS].shift = 0;
1373		ale_controls[ALE_PORT_UNTAGGED_EGRESS].offset =
1374					ALE_UNKNOWNVLAN_FORCE_UNTAG_EGRESS;
1375	}
1376
1377	cpsw_ale_control_set(ale, 0, ALE_CLEAR, 1);
1378	return ale;
1379}
1380
1381void cpsw_ale_dump(struct cpsw_ale *ale, u32 *data)
1382{
1383	int i;
1384
1385	for (i = 0; i < ale->params.ale_entries; i++) {
1386		cpsw_ale_read(ale, i, data);
1387		data += ALE_ENTRY_WORDS;
1388	}
1389}
1390
 
 
 
 
 
 
 
 
 
 
1391u32 cpsw_ale_get_num_entries(struct cpsw_ale *ale)
1392{
1393	return ale ? ale->params.ale_entries : 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1394}