1 // SPDX-License-Identifier: GPL-2.0
3 * Texas Instruments N-Port Ethernet Switch Address Lookup Engine
5 * Copyright (C) 2012 Texas Instruments
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>
17 #include <linux/stat.h>
18 #include <linux/sysfs.h>
19 #include <linux/etherdevice.h>
23 #define BITMASK(bits) (BIT(bits) - 1)
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
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
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)))
48 #define AM65_CPSW_ALE_THREAD_DEF_REG 0x134
51 #define ALE_AGING_TIMER_MASK GENMASK(23, 0)
53 #define ALE_RATE_LIMIT_MIN_PPS 1000
56 * struct ale_entry_fld - The ALE tbl entry field description
57 * @start_bit: field start bit
58 * @num_bits: field bit length
61 struct ale_entry_fld {
68 CPSW_ALE_F_STATUS_REG = BIT(0), /* Status register present */
69 CPSW_ALE_F_HW_AUTOAGING = BIT(1), /* HW auto aging */
75 * struct cpsw_ale_dev_id - The ALE version/SoC specific configuration
76 * @dev_id: ALE version/SoC id
77 * @features: features supported by ALE
78 * @tbl_entries: number of ALE entries
79 * @major_ver_mask: mask of ALE Major Version Value in ALE_IDVER reg.
80 * @nu_switch_ale: NU Switch ALE
81 * @vlan_entry_tbl: ALE vlan entry fields description tbl
83 struct cpsw_ale_dev_id {
89 const struct ale_entry_fld *vlan_entry_tbl;
92 #define ALE_TABLE_WRITE BIT(31)
94 #define ALE_TYPE_FREE 0
95 #define ALE_TYPE_ADDR 1
96 #define ALE_TYPE_VLAN 2
97 #define ALE_TYPE_VLAN_ADDR 3
99 #define ALE_UCAST_PERSISTANT 0
100 #define ALE_UCAST_UNTOUCHED 1
101 #define ALE_UCAST_OUI 2
102 #define ALE_UCAST_TOUCHED 3
104 #define ALE_TABLE_SIZE_MULTIPLIER 1024
105 #define ALE_STATUS_SIZE_MASK 0x1f
107 static inline int cpsw_ale_get_field(u32 *ale_entry, u32 start, u32 bits)
113 idx2 = (start + bits - 1) / 32;
114 /* Check if bits to be fetched exceed a word */
116 idx2 = 2 - idx2; /* flip */
117 hi_val = ale_entry[idx2] << ((idx2 * 32) - start);
120 idx = 2 - idx; /* flip */
121 return (hi_val + (ale_entry[idx] >> start)) & BITMASK(bits);
124 static inline void cpsw_ale_set_field(u32 *ale_entry, u32 start, u32 bits,
129 value &= BITMASK(bits);
131 idx2 = (start + bits - 1) / 32;
132 /* Check if bits to be set exceed a word */
134 idx2 = 2 - idx2; /* flip */
135 ale_entry[idx2] &= ~(BITMASK(bits + start - (idx2 * 32)));
136 ale_entry[idx2] |= (value >> ((idx2 * 32) - start));
139 idx = 2 - idx; /* flip */
140 ale_entry[idx] &= ~(BITMASK(bits) << start);
141 ale_entry[idx] |= (value << start);
144 #define DEFINE_ALE_FIELD(name, start, bits) \
145 static inline int cpsw_ale_get_##name(u32 *ale_entry) \
147 return cpsw_ale_get_field(ale_entry, start, bits); \
149 static inline void cpsw_ale_set_##name(u32 *ale_entry, u32 value) \
151 cpsw_ale_set_field(ale_entry, start, bits, value); \
154 #define DEFINE_ALE_FIELD1(name, start) \
155 static inline int cpsw_ale_get_##name(u32 *ale_entry, u32 bits) \
157 return cpsw_ale_get_field(ale_entry, start, bits); \
159 static inline void cpsw_ale_set_##name(u32 *ale_entry, u32 value, \
162 cpsw_ale_set_field(ale_entry, start, bits, value); \
166 ALE_ENT_VID_MEMBER_LIST = 0,
167 ALE_ENT_VID_UNREG_MCAST_MSK,
168 ALE_ENT_VID_REG_MCAST_MSK,
169 ALE_ENT_VID_FORCE_UNTAGGED_MSK,
170 ALE_ENT_VID_UNREG_MCAST_IDX,
171 ALE_ENT_VID_REG_MCAST_IDX,
175 #define ALE_FLD_ALLOWED BIT(0)
176 #define ALE_FLD_SIZE_PORT_MASK_BITS BIT(1)
177 #define ALE_FLD_SIZE_PORT_NUM_BITS BIT(2)
179 #define ALE_ENTRY_FLD(id, start, bits) \
181 .start_bit = start, \
183 .flags = ALE_FLD_ALLOWED, \
186 #define ALE_ENTRY_FLD_DYN_MSK_SIZE(id, start) \
188 .start_bit = start, \
190 .flags = ALE_FLD_ALLOWED | \
191 ALE_FLD_SIZE_PORT_MASK_BITS, \
194 /* dm814x, am3/am4/am5, k2hk */
195 static const struct ale_entry_fld vlan_entry_cpsw[ALE_ENT_VID_LAST] = {
196 ALE_ENTRY_FLD(ALE_ENT_VID_MEMBER_LIST, 0, 3),
197 ALE_ENTRY_FLD(ALE_ENT_VID_UNREG_MCAST_MSK, 8, 3),
198 ALE_ENTRY_FLD(ALE_ENT_VID_REG_MCAST_MSK, 16, 3),
199 ALE_ENTRY_FLD(ALE_ENT_VID_FORCE_UNTAGGED_MSK, 24, 3),
202 /* k2e/k2l, k3 am65/j721e cpsw2g */
203 static const struct ale_entry_fld vlan_entry_nu[ALE_ENT_VID_LAST] = {
204 ALE_ENTRY_FLD_DYN_MSK_SIZE(ALE_ENT_VID_MEMBER_LIST, 0),
205 ALE_ENTRY_FLD(ALE_ENT_VID_UNREG_MCAST_IDX, 20, 3),
206 ALE_ENTRY_FLD_DYN_MSK_SIZE(ALE_ENT_VID_FORCE_UNTAGGED_MSK, 24),
207 ALE_ENTRY_FLD(ALE_ENT_VID_REG_MCAST_IDX, 44, 3),
210 /* K3 j721e/j7200 cpsw9g/5g, am64x cpsw3g */
211 static const struct ale_entry_fld vlan_entry_k3_cpswxg[] = {
212 ALE_ENTRY_FLD_DYN_MSK_SIZE(ALE_ENT_VID_MEMBER_LIST, 0),
213 ALE_ENTRY_FLD_DYN_MSK_SIZE(ALE_ENT_VID_UNREG_MCAST_MSK, 12),
214 ALE_ENTRY_FLD_DYN_MSK_SIZE(ALE_ENT_VID_FORCE_UNTAGGED_MSK, 24),
215 ALE_ENTRY_FLD_DYN_MSK_SIZE(ALE_ENT_VID_REG_MCAST_MSK, 36),
218 DEFINE_ALE_FIELD(entry_type, 60, 2)
219 DEFINE_ALE_FIELD(vlan_id, 48, 12)
220 DEFINE_ALE_FIELD(mcast_state, 62, 2)
221 DEFINE_ALE_FIELD1(port_mask, 66)
222 DEFINE_ALE_FIELD(super, 65, 1)
223 DEFINE_ALE_FIELD(ucast_type, 62, 2)
224 DEFINE_ALE_FIELD1(port_num, 66)
225 DEFINE_ALE_FIELD(blocked, 65, 1)
226 DEFINE_ALE_FIELD(secure, 64, 1)
227 DEFINE_ALE_FIELD(mcast, 40, 1)
229 #define NU_VLAN_UNREG_MCAST_IDX 1
231 static int cpsw_ale_entry_get_fld(struct cpsw_ale *ale,
233 const struct ale_entry_fld *entry_tbl,
236 const struct ale_entry_fld *entry_fld;
239 if (!ale || !ale_entry)
242 entry_fld = &entry_tbl[fld_id];
243 if (!(entry_fld->flags & ALE_FLD_ALLOWED)) {
244 dev_err(ale->params.dev, "get: wrong ale fld id %d\n", fld_id);
248 bits = entry_fld->num_bits;
249 if (entry_fld->flags & ALE_FLD_SIZE_PORT_MASK_BITS)
250 bits = ale->port_mask_bits;
252 return cpsw_ale_get_field(ale_entry, entry_fld->start_bit, bits);
255 static void cpsw_ale_entry_set_fld(struct cpsw_ale *ale,
257 const struct ale_entry_fld *entry_tbl,
261 const struct ale_entry_fld *entry_fld;
264 if (!ale || !ale_entry)
267 entry_fld = &entry_tbl[fld_id];
268 if (!(entry_fld->flags & ALE_FLD_ALLOWED)) {
269 dev_err(ale->params.dev, "set: wrong ale fld id %d\n", fld_id);
273 bits = entry_fld->num_bits;
274 if (entry_fld->flags & ALE_FLD_SIZE_PORT_MASK_BITS)
275 bits = ale->port_mask_bits;
277 cpsw_ale_set_field(ale_entry, entry_fld->start_bit, bits, value);
280 static int cpsw_ale_vlan_get_fld(struct cpsw_ale *ale,
284 return cpsw_ale_entry_get_fld(ale, ale_entry,
285 ale->vlan_entry_tbl, fld_id);
288 static void cpsw_ale_vlan_set_fld(struct cpsw_ale *ale,
293 cpsw_ale_entry_set_fld(ale, ale_entry,
294 ale->vlan_entry_tbl, fld_id, value);
297 /* The MAC address field in the ALE entry cannot be macroized as above */
298 static inline void cpsw_ale_get_addr(u32 *ale_entry, u8 *addr)
302 for (i = 0; i < 6; i++)
303 addr[i] = cpsw_ale_get_field(ale_entry, 40 - 8*i, 8);
306 static inline void cpsw_ale_set_addr(u32 *ale_entry, const u8 *addr)
310 for (i = 0; i < 6; i++)
311 cpsw_ale_set_field(ale_entry, 40 - 8*i, 8, addr[i]);
314 static int cpsw_ale_read(struct cpsw_ale *ale, int idx, u32 *ale_entry)
318 WARN_ON(idx > ale->params.ale_entries);
320 writel_relaxed(idx, ale->params.ale_regs + ALE_TABLE_CONTROL);
322 for (i = 0; i < ALE_ENTRY_WORDS; i++)
323 ale_entry[i] = readl_relaxed(ale->params.ale_regs +
329 static int cpsw_ale_write(struct cpsw_ale *ale, int idx, u32 *ale_entry)
333 WARN_ON(idx > ale->params.ale_entries);
335 for (i = 0; i < ALE_ENTRY_WORDS; i++)
336 writel_relaxed(ale_entry[i], ale->params.ale_regs +
339 writel_relaxed(idx | ALE_TABLE_WRITE, ale->params.ale_regs +
345 static int cpsw_ale_match_addr(struct cpsw_ale *ale, const u8 *addr, u16 vid)
347 u32 ale_entry[ALE_ENTRY_WORDS];
350 for (idx = 0; idx < ale->params.ale_entries; idx++) {
353 cpsw_ale_read(ale, idx, ale_entry);
354 type = cpsw_ale_get_entry_type(ale_entry);
355 if (type != ALE_TYPE_ADDR && type != ALE_TYPE_VLAN_ADDR)
357 if (cpsw_ale_get_vlan_id(ale_entry) != vid)
359 cpsw_ale_get_addr(ale_entry, entry_addr);
360 if (ether_addr_equal(entry_addr, addr))
366 static int cpsw_ale_match_vlan(struct cpsw_ale *ale, u16 vid)
368 u32 ale_entry[ALE_ENTRY_WORDS];
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_VLAN)
376 if (cpsw_ale_get_vlan_id(ale_entry) == vid)
382 static int cpsw_ale_match_free(struct cpsw_ale *ale)
384 u32 ale_entry[ALE_ENTRY_WORDS];
387 for (idx = 0; idx < ale->params.ale_entries; idx++) {
388 cpsw_ale_read(ale, idx, ale_entry);
389 type = cpsw_ale_get_entry_type(ale_entry);
390 if (type == ALE_TYPE_FREE)
396 static int cpsw_ale_find_ageable(struct cpsw_ale *ale)
398 u32 ale_entry[ALE_ENTRY_WORDS];
401 for (idx = 0; idx < ale->params.ale_entries; idx++) {
402 cpsw_ale_read(ale, idx, ale_entry);
403 type = cpsw_ale_get_entry_type(ale_entry);
404 if (type != ALE_TYPE_ADDR && type != ALE_TYPE_VLAN_ADDR)
406 if (cpsw_ale_get_mcast(ale_entry))
408 type = cpsw_ale_get_ucast_type(ale_entry);
409 if (type != ALE_UCAST_PERSISTANT &&
410 type != ALE_UCAST_OUI)
416 static void cpsw_ale_flush_mcast(struct cpsw_ale *ale, u32 *ale_entry,
421 mask = cpsw_ale_get_port_mask(ale_entry,
422 ale->port_mask_bits);
423 if ((mask & port_mask) == 0)
424 return; /* ports dont intersect, not interested */
427 /* free if only remaining port is host port */
429 cpsw_ale_set_port_mask(ale_entry, mask,
430 ale->port_mask_bits);
432 cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
435 int cpsw_ale_flush_multicast(struct cpsw_ale *ale, int port_mask, int vid)
437 u32 ale_entry[ALE_ENTRY_WORDS];
440 for (idx = 0; idx < ale->params.ale_entries; idx++) {
441 cpsw_ale_read(ale, idx, ale_entry);
442 ret = cpsw_ale_get_entry_type(ale_entry);
443 if (ret != ALE_TYPE_ADDR && ret != ALE_TYPE_VLAN_ADDR)
446 /* if vid passed is -1 then remove all multicast entry from
447 * the table irrespective of vlan id, if a valid vlan id is
448 * passed then remove only multicast added to that vlan id.
449 * if vlan id doesn't match then move on to next entry.
451 if (vid != -1 && cpsw_ale_get_vlan_id(ale_entry) != vid)
454 if (cpsw_ale_get_mcast(ale_entry)) {
457 if (cpsw_ale_get_super(ale_entry))
460 cpsw_ale_get_addr(ale_entry, addr);
461 if (!is_broadcast_ether_addr(addr))
462 cpsw_ale_flush_mcast(ale, ale_entry, port_mask);
465 cpsw_ale_write(ale, idx, ale_entry);
470 static inline void cpsw_ale_set_vlan_entry_type(u32 *ale_entry,
473 if (flags & ALE_VLAN) {
474 cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_VLAN_ADDR);
475 cpsw_ale_set_vlan_id(ale_entry, vid);
477 cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_ADDR);
481 int cpsw_ale_add_ucast(struct cpsw_ale *ale, const u8 *addr, int port,
484 u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
487 cpsw_ale_set_vlan_entry_type(ale_entry, flags, vid);
489 cpsw_ale_set_addr(ale_entry, addr);
490 cpsw_ale_set_ucast_type(ale_entry, ALE_UCAST_PERSISTANT);
491 cpsw_ale_set_secure(ale_entry, (flags & ALE_SECURE) ? 1 : 0);
492 cpsw_ale_set_blocked(ale_entry, (flags & ALE_BLOCKED) ? 1 : 0);
493 cpsw_ale_set_port_num(ale_entry, port, ale->port_num_bits);
495 idx = cpsw_ale_match_addr(ale, addr, (flags & ALE_VLAN) ? vid : 0);
497 idx = cpsw_ale_match_free(ale);
499 idx = cpsw_ale_find_ageable(ale);
503 cpsw_ale_write(ale, idx, ale_entry);
507 int cpsw_ale_del_ucast(struct cpsw_ale *ale, const u8 *addr, int port,
510 u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
513 idx = cpsw_ale_match_addr(ale, addr, (flags & ALE_VLAN) ? vid : 0);
517 cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
518 cpsw_ale_write(ale, idx, ale_entry);
522 int cpsw_ale_add_mcast(struct cpsw_ale *ale, const u8 *addr, int port_mask,
523 int flags, u16 vid, int mcast_state)
525 u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
528 idx = cpsw_ale_match_addr(ale, addr, (flags & ALE_VLAN) ? vid : 0);
530 cpsw_ale_read(ale, idx, ale_entry);
532 cpsw_ale_set_vlan_entry_type(ale_entry, flags, vid);
534 cpsw_ale_set_addr(ale_entry, addr);
535 cpsw_ale_set_super(ale_entry, (flags & ALE_SUPER) ? 1 : 0);
536 cpsw_ale_set_mcast_state(ale_entry, mcast_state);
538 mask = cpsw_ale_get_port_mask(ale_entry,
539 ale->port_mask_bits);
541 cpsw_ale_set_port_mask(ale_entry, port_mask,
542 ale->port_mask_bits);
545 idx = cpsw_ale_match_free(ale);
547 idx = cpsw_ale_find_ageable(ale);
551 cpsw_ale_write(ale, idx, ale_entry);
555 int cpsw_ale_del_mcast(struct cpsw_ale *ale, const u8 *addr, int port_mask,
558 u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
559 int mcast_members = 0;
562 idx = cpsw_ale_match_addr(ale, addr, (flags & ALE_VLAN) ? vid : 0);
566 cpsw_ale_read(ale, idx, ale_entry);
569 mcast_members = cpsw_ale_get_port_mask(ale_entry,
570 ale->port_mask_bits);
571 mcast_members &= ~port_mask;
575 cpsw_ale_set_port_mask(ale_entry, mcast_members,
576 ale->port_mask_bits);
578 cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
580 cpsw_ale_write(ale, idx, ale_entry);
584 /* ALE NetCP NU switch specific vlan functions */
585 static void cpsw_ale_set_vlan_mcast(struct cpsw_ale *ale, u32 *ale_entry,
586 int reg_mcast, int unreg_mcast)
590 /* Set VLAN registered multicast flood mask */
591 idx = cpsw_ale_vlan_get_fld(ale, ale_entry,
592 ALE_ENT_VID_REG_MCAST_IDX);
593 writel(reg_mcast, ale->params.ale_regs + ALE_VLAN_MASK_MUX(idx));
595 /* Set VLAN unregistered multicast flood mask */
596 idx = cpsw_ale_vlan_get_fld(ale, ale_entry,
597 ALE_ENT_VID_UNREG_MCAST_IDX);
598 writel(unreg_mcast, ale->params.ale_regs + ALE_VLAN_MASK_MUX(idx));
601 static void cpsw_ale_set_vlan_untag(struct cpsw_ale *ale, u32 *ale_entry,
602 u16 vid, int untag_mask)
604 cpsw_ale_vlan_set_fld(ale, ale_entry,
605 ALE_ENT_VID_FORCE_UNTAGGED_MSK,
607 if (untag_mask & ALE_PORT_HOST)
608 bitmap_set(ale->p0_untag_vid_mask, vid, 1);
610 bitmap_clear(ale->p0_untag_vid_mask, vid, 1);
613 int cpsw_ale_add_vlan(struct cpsw_ale *ale, u16 vid, int port_mask, int untag,
614 int reg_mcast, int unreg_mcast)
616 u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
619 idx = cpsw_ale_match_vlan(ale, vid);
621 cpsw_ale_read(ale, idx, ale_entry);
623 cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_VLAN);
624 cpsw_ale_set_vlan_id(ale_entry, vid);
625 cpsw_ale_set_vlan_untag(ale, ale_entry, vid, untag);
627 if (!ale->params.nu_switch_ale) {
628 cpsw_ale_vlan_set_fld(ale, ale_entry,
629 ALE_ENT_VID_REG_MCAST_MSK, reg_mcast);
630 cpsw_ale_vlan_set_fld(ale, ale_entry,
631 ALE_ENT_VID_UNREG_MCAST_MSK, unreg_mcast);
633 cpsw_ale_vlan_set_fld(ale, ale_entry,
634 ALE_ENT_VID_UNREG_MCAST_IDX,
635 NU_VLAN_UNREG_MCAST_IDX);
636 cpsw_ale_set_vlan_mcast(ale, ale_entry, reg_mcast, unreg_mcast);
639 cpsw_ale_vlan_set_fld(ale, ale_entry,
640 ALE_ENT_VID_MEMBER_LIST, port_mask);
643 idx = cpsw_ale_match_free(ale);
645 idx = cpsw_ale_find_ageable(ale);
649 cpsw_ale_write(ale, idx, ale_entry);
653 static void cpsw_ale_vlan_del_modify_int(struct cpsw_ale *ale, u32 *ale_entry,
654 u16 vid, int port_mask)
656 int reg_mcast, unreg_mcast;
659 members = cpsw_ale_vlan_get_fld(ale, ale_entry,
660 ALE_ENT_VID_MEMBER_LIST);
661 members &= ~port_mask;
663 cpsw_ale_set_vlan_untag(ale, ale_entry, vid, 0);
664 cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
668 untag = cpsw_ale_vlan_get_fld(ale, ale_entry,
669 ALE_ENT_VID_FORCE_UNTAGGED_MSK);
670 reg_mcast = cpsw_ale_vlan_get_fld(ale, ale_entry,
671 ALE_ENT_VID_REG_MCAST_MSK);
672 unreg_mcast = cpsw_ale_vlan_get_fld(ale, ale_entry,
673 ALE_ENT_VID_UNREG_MCAST_MSK);
675 reg_mcast &= members;
676 unreg_mcast &= members;
678 cpsw_ale_set_vlan_untag(ale, ale_entry, vid, untag);
680 if (!ale->params.nu_switch_ale) {
681 cpsw_ale_vlan_set_fld(ale, ale_entry,
682 ALE_ENT_VID_REG_MCAST_MSK, reg_mcast);
683 cpsw_ale_vlan_set_fld(ale, ale_entry,
684 ALE_ENT_VID_UNREG_MCAST_MSK, unreg_mcast);
686 cpsw_ale_set_vlan_mcast(ale, ale_entry, reg_mcast,
689 cpsw_ale_vlan_set_fld(ale, ale_entry,
690 ALE_ENT_VID_MEMBER_LIST, members);
693 int cpsw_ale_vlan_del_modify(struct cpsw_ale *ale, u16 vid, int port_mask)
695 u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
698 idx = cpsw_ale_match_vlan(ale, vid);
702 cpsw_ale_read(ale, idx, ale_entry);
704 cpsw_ale_vlan_del_modify_int(ale, ale_entry, vid, port_mask);
705 cpsw_ale_write(ale, idx, ale_entry);
710 int cpsw_ale_del_vlan(struct cpsw_ale *ale, u16 vid, int port_mask)
712 u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
715 idx = cpsw_ale_match_vlan(ale, vid);
719 cpsw_ale_read(ale, idx, ale_entry);
721 /* if !port_mask - force remove VLAN (legacy).
722 * Check if there are other VLAN members ports
723 * if no - remove VLAN.
724 * if yes it means same VLAN was added to >1 port in multi port mode, so
725 * remove port_mask ports from VLAN ALE entry excluding Host port.
727 members = cpsw_ale_vlan_get_fld(ale, ale_entry, ALE_ENT_VID_MEMBER_LIST);
728 members &= ~port_mask;
730 if (!port_mask || !members) {
731 /* last port or force remove - remove VLAN */
732 cpsw_ale_set_vlan_untag(ale, ale_entry, vid, 0);
733 cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
735 port_mask &= ~ALE_PORT_HOST;
736 cpsw_ale_vlan_del_modify_int(ale, ale_entry, vid, port_mask);
739 cpsw_ale_write(ale, idx, ale_entry);
744 int cpsw_ale_vlan_add_modify(struct cpsw_ale *ale, u16 vid, int port_mask,
745 int untag_mask, int reg_mask, int unreg_mask)
747 u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
748 int reg_mcast_members, unreg_mcast_members;
749 int vlan_members, untag_members;
752 idx = cpsw_ale_match_vlan(ale, vid);
754 cpsw_ale_read(ale, idx, ale_entry);
756 vlan_members = cpsw_ale_vlan_get_fld(ale, ale_entry,
757 ALE_ENT_VID_MEMBER_LIST);
758 reg_mcast_members = cpsw_ale_vlan_get_fld(ale, ale_entry,
759 ALE_ENT_VID_REG_MCAST_MSK);
760 unreg_mcast_members =
761 cpsw_ale_vlan_get_fld(ale, ale_entry,
762 ALE_ENT_VID_UNREG_MCAST_MSK);
763 untag_members = cpsw_ale_vlan_get_fld(ale, ale_entry,
764 ALE_ENT_VID_FORCE_UNTAGGED_MSK);
766 vlan_members |= port_mask;
767 untag_members = (untag_members & ~port_mask) | untag_mask;
768 reg_mcast_members = (reg_mcast_members & ~port_mask) | reg_mask;
769 unreg_mcast_members = (unreg_mcast_members & ~port_mask) | unreg_mask;
771 ret = cpsw_ale_add_vlan(ale, vid, vlan_members, untag_members,
772 reg_mcast_members, unreg_mcast_members);
774 dev_err(ale->params.dev, "Unable to add vlan\n");
777 dev_dbg(ale->params.dev, "port mask 0x%x untag 0x%x\n", vlan_members,
783 void cpsw_ale_set_unreg_mcast(struct cpsw_ale *ale, int unreg_mcast_mask,
786 u32 ale_entry[ALE_ENTRY_WORDS];
787 int unreg_members = 0;
790 for (idx = 0; idx < ale->params.ale_entries; idx++) {
791 cpsw_ale_read(ale, idx, ale_entry);
792 type = cpsw_ale_get_entry_type(ale_entry);
793 if (type != ALE_TYPE_VLAN)
797 cpsw_ale_vlan_get_fld(ale, ale_entry,
798 ALE_ENT_VID_UNREG_MCAST_MSK);
800 unreg_members |= unreg_mcast_mask;
802 unreg_members &= ~unreg_mcast_mask;
803 cpsw_ale_vlan_set_fld(ale, ale_entry,
804 ALE_ENT_VID_UNREG_MCAST_MSK,
806 cpsw_ale_write(ale, idx, ale_entry);
810 static void cpsw_ale_vlan_set_unreg_mcast(struct cpsw_ale *ale, u32 *ale_entry,
815 unreg_mcast = cpsw_ale_vlan_get_fld(ale, ale_entry,
816 ALE_ENT_VID_UNREG_MCAST_MSK);
818 unreg_mcast |= ALE_PORT_HOST;
820 unreg_mcast &= ~ALE_PORT_HOST;
822 cpsw_ale_vlan_set_fld(ale, ale_entry,
823 ALE_ENT_VID_UNREG_MCAST_MSK, unreg_mcast);
827 cpsw_ale_vlan_set_unreg_mcast_idx(struct cpsw_ale *ale, u32 *ale_entry,
833 idx = cpsw_ale_vlan_get_fld(ale, ale_entry,
834 ALE_ENT_VID_UNREG_MCAST_IDX);
836 unreg_mcast = readl(ale->params.ale_regs + ALE_VLAN_MASK_MUX(idx));
839 unreg_mcast |= ALE_PORT_HOST;
841 unreg_mcast &= ~ALE_PORT_HOST;
843 writel(unreg_mcast, ale->params.ale_regs + ALE_VLAN_MASK_MUX(idx));
846 void cpsw_ale_set_allmulti(struct cpsw_ale *ale, int allmulti, int port)
848 u32 ale_entry[ALE_ENTRY_WORDS];
851 for (idx = 0; idx < ale->params.ale_entries; idx++) {
854 cpsw_ale_read(ale, idx, ale_entry);
855 type = cpsw_ale_get_entry_type(ale_entry);
856 if (type != ALE_TYPE_VLAN)
859 vlan_members = cpsw_ale_vlan_get_fld(ale, ale_entry,
860 ALE_ENT_VID_MEMBER_LIST);
862 if (port != -1 && !(vlan_members & BIT(port)))
865 if (!ale->params.nu_switch_ale)
866 cpsw_ale_vlan_set_unreg_mcast(ale, ale_entry, allmulti);
868 cpsw_ale_vlan_set_unreg_mcast_idx(ale, ale_entry,
871 cpsw_ale_write(ale, idx, ale_entry);
875 struct ale_control_info {
877 int offset, port_offset;
878 int shift, port_shift;
882 static struct ale_control_info ale_controls[ALE_NUM_CONTROLS] = {
885 .offset = ALE_CONTROL,
893 .offset = ALE_CONTROL,
901 .offset = ALE_CONTROL,
907 [ALE_P0_UNI_FLOOD] = {
908 .name = "port0_unicast_flood",
909 .offset = ALE_CONTROL,
915 [ALE_VLAN_NOLEARN] = {
916 .name = "vlan_nolearn",
917 .offset = ALE_CONTROL,
923 [ALE_NO_PORT_VLAN] = {
924 .name = "no_port_vlan",
925 .offset = ALE_CONTROL,
933 .offset = ALE_CONTROL,
941 .offset = ALE_CONTROL,
947 [ALE_RATE_LIMIT_TX] = {
948 .name = "rate_limit_tx",
949 .offset = ALE_CONTROL,
956 .name = "vlan_aware",
957 .offset = ALE_CONTROL,
963 [ALE_AUTH_ENABLE] = {
964 .name = "auth_enable",
965 .offset = ALE_CONTROL,
972 .name = "rate_limit",
973 .offset = ALE_CONTROL,
980 .name = "port_state",
981 .offset = ALE_PORTCTL,
987 [ALE_PORT_DROP_UNTAGGED] = {
988 .name = "drop_untagged",
989 .offset = ALE_PORTCTL,
995 [ALE_PORT_DROP_UNKNOWN_VLAN] = {
996 .name = "drop_unknown",
997 .offset = ALE_PORTCTL,
1003 [ALE_PORT_NOLEARN] = {
1005 .offset = ALE_PORTCTL,
1011 [ALE_PORT_NO_SA_UPDATE] = {
1012 .name = "no_source_update",
1013 .offset = ALE_PORTCTL,
1019 [ALE_PORT_MACONLY] = {
1020 .name = "mac_only_port_mode",
1021 .offset = ALE_PORTCTL,
1027 [ALE_PORT_MACONLY_CAF] = {
1028 .name = "mac_only_port_caf",
1029 .offset = ALE_PORTCTL,
1035 [ALE_PORT_MCAST_LIMIT] = {
1036 .name = "mcast_limit",
1037 .offset = ALE_PORTCTL,
1043 [ALE_PORT_BCAST_LIMIT] = {
1044 .name = "bcast_limit",
1045 .offset = ALE_PORTCTL,
1051 [ALE_PORT_UNKNOWN_VLAN_MEMBER] = {
1052 .name = "unknown_vlan_member",
1053 .offset = ALE_UNKNOWNVLAN,
1059 [ALE_PORT_UNKNOWN_MCAST_FLOOD] = {
1060 .name = "unknown_mcast_flood",
1061 .offset = ALE_UNKNOWNVLAN,
1067 [ALE_PORT_UNKNOWN_REG_MCAST_FLOOD] = {
1068 .name = "unknown_reg_flood",
1069 .offset = ALE_UNKNOWNVLAN,
1075 [ALE_PORT_UNTAGGED_EGRESS] = {
1076 .name = "untagged_egress",
1077 .offset = ALE_UNKNOWNVLAN,
1083 [ALE_DEFAULT_THREAD_ID] = {
1084 .name = "default_thread_id",
1085 .offset = AM65_CPSW_ALE_THREAD_DEF_REG,
1091 [ALE_DEFAULT_THREAD_ENABLE] = {
1092 .name = "default_thread_id_enable",
1093 .offset = AM65_CPSW_ALE_THREAD_DEF_REG,
1101 int cpsw_ale_control_set(struct cpsw_ale *ale, int port, int control,
1104 const struct ale_control_info *info;
1108 if (control < 0 || control >= ARRAY_SIZE(ale_controls))
1111 info = &ale_controls[control];
1112 if (info->port_offset == 0 && info->port_shift == 0)
1113 port = 0; /* global, port is a dont care */
1115 if (port < 0 || port >= ale->params.ale_ports)
1118 mask = BITMASK(info->bits);
1122 offset = info->offset + (port * info->port_offset);
1123 shift = info->shift + (port * info->port_shift);
1125 tmp = readl_relaxed(ale->params.ale_regs + offset);
1126 tmp = (tmp & ~(mask << shift)) | (value << shift);
1127 writel_relaxed(tmp, ale->params.ale_regs + offset);
1132 int cpsw_ale_control_get(struct cpsw_ale *ale, int port, int control)
1134 const struct ale_control_info *info;
1138 if (control < 0 || control >= ARRAY_SIZE(ale_controls))
1141 info = &ale_controls[control];
1142 if (info->port_offset == 0 && info->port_shift == 0)
1143 port = 0; /* global, port is a dont care */
1145 if (port < 0 || port >= ale->params.ale_ports)
1148 offset = info->offset + (port * info->port_offset);
1149 shift = info->shift + (port * info->port_shift);
1151 tmp = readl_relaxed(ale->params.ale_regs + offset) >> shift;
1152 return tmp & BITMASK(info->bits);
1155 int cpsw_ale_rx_ratelimit_mc(struct cpsw_ale *ale, int port, unsigned int ratelimit_pps)
1158 int val = ratelimit_pps / ALE_RATE_LIMIT_MIN_PPS;
1159 u32 remainder = ratelimit_pps % ALE_RATE_LIMIT_MIN_PPS;
1161 if (ratelimit_pps && !val) {
1162 dev_err(ale->params.dev, "ALE MC port:%d ratelimit min value 1000pps\n", port);
1167 dev_info(ale->params.dev, "ALE port:%d MC ratelimit set to %dpps (requested %d)\n",
1168 port, ratelimit_pps - remainder, ratelimit_pps);
1170 cpsw_ale_control_set(ale, port, ALE_PORT_MCAST_LIMIT, val);
1172 dev_dbg(ale->params.dev, "ALE port:%d MC ratelimit set %d\n",
1173 port, val * ALE_RATE_LIMIT_MIN_PPS);
1177 int cpsw_ale_rx_ratelimit_bc(struct cpsw_ale *ale, int port, unsigned int ratelimit_pps)
1180 int val = ratelimit_pps / ALE_RATE_LIMIT_MIN_PPS;
1181 u32 remainder = ratelimit_pps % ALE_RATE_LIMIT_MIN_PPS;
1183 if (ratelimit_pps && !val) {
1184 dev_err(ale->params.dev, "ALE port:%d BC ratelimit min value 1000pps\n", port);
1189 dev_info(ale->params.dev, "ALE port:%d BC ratelimit set to %dpps (requested %d)\n",
1190 port, ratelimit_pps - remainder, ratelimit_pps);
1192 cpsw_ale_control_set(ale, port, ALE_PORT_BCAST_LIMIT, val);
1194 dev_dbg(ale->params.dev, "ALE port:%d BC ratelimit set %d\n",
1195 port, val * ALE_RATE_LIMIT_MIN_PPS);
1199 static void cpsw_ale_timer(struct timer_list *t)
1201 struct cpsw_ale *ale = from_timer(ale, t, timer);
1203 cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
1206 ale->timer.expires = jiffies + ale->ageout;
1207 add_timer(&ale->timer);
1211 static void cpsw_ale_hw_aging_timer_start(struct cpsw_ale *ale)
1215 aging_timer = ale->params.bus_freq / 1000000;
1216 aging_timer *= ale->params.ale_ageout;
1218 if (aging_timer & ~ALE_AGING_TIMER_MASK) {
1219 aging_timer = ALE_AGING_TIMER_MASK;
1220 dev_warn(ale->params.dev,
1221 "ALE aging timer overflow, set to max\n");
1224 writel(aging_timer, ale->params.ale_regs + ALE_AGING_TIMER);
1227 static void cpsw_ale_hw_aging_timer_stop(struct cpsw_ale *ale)
1229 writel(0, ale->params.ale_regs + ALE_AGING_TIMER);
1232 static void cpsw_ale_aging_start(struct cpsw_ale *ale)
1234 if (!ale->params.ale_ageout)
1237 if (ale->features & CPSW_ALE_F_HW_AUTOAGING) {
1238 cpsw_ale_hw_aging_timer_start(ale);
1242 timer_setup(&ale->timer, cpsw_ale_timer, 0);
1243 ale->timer.expires = jiffies + ale->ageout;
1244 add_timer(&ale->timer);
1247 static void cpsw_ale_aging_stop(struct cpsw_ale *ale)
1249 if (!ale->params.ale_ageout)
1252 if (ale->features & CPSW_ALE_F_HW_AUTOAGING) {
1253 cpsw_ale_hw_aging_timer_stop(ale);
1257 del_timer_sync(&ale->timer);
1260 void cpsw_ale_start(struct cpsw_ale *ale)
1262 unsigned long ale_prescale;
1264 /* configure Broadcast and Multicast Rate Limit
1265 * number_of_packets = (Fclk / ALE_PRESCALE) * port.BCAST/MCAST_LIMIT
1266 * ALE_PRESCALE width is 19bit and min value 0x10
1267 * port.BCAST/MCAST_LIMIT is 8bit
1269 * For multi port configuration support the ALE_PRESCALE is configured to 1ms interval,
1270 * which allows to configure port.BCAST/MCAST_LIMIT per port and achieve:
1271 * min number_of_packets = 1000 when port.BCAST/MCAST_LIMIT = 1
1272 * max number_of_packets = 1000 * 255 = 255000 when port.BCAST/MCAST_LIMIT = 0xFF
1274 ale_prescale = ale->params.bus_freq / ALE_RATE_LIMIT_MIN_PPS;
1275 writel((u32)ale_prescale, ale->params.ale_regs + ALE_PRESCALE);
1277 /* Allow MC/BC rate limiting globally.
1278 * The actual Rate Limit cfg enabled per-port by port.BCAST/MCAST_LIMIT
1280 cpsw_ale_control_set(ale, 0, ALE_RATE_LIMIT, 1);
1282 cpsw_ale_control_set(ale, 0, ALE_ENABLE, 1);
1283 cpsw_ale_control_set(ale, 0, ALE_CLEAR, 1);
1285 cpsw_ale_aging_start(ale);
1288 void cpsw_ale_stop(struct cpsw_ale *ale)
1290 cpsw_ale_aging_stop(ale);
1291 cpsw_ale_control_set(ale, 0, ALE_CLEAR, 1);
1292 cpsw_ale_control_set(ale, 0, ALE_ENABLE, 0);
1295 static const struct cpsw_ale_dev_id cpsw_ale_id_match[] = {
1297 /* am3/4/5, dra7. dm814x, 66ak2hk-gbe */
1299 .tbl_entries = 1024,
1300 .major_ver_mask = 0xff,
1301 .vlan_entry_tbl = vlan_entry_cpsw,
1305 .dev_id = "66ak2h-xgbe",
1306 .tbl_entries = 2048,
1307 .major_ver_mask = 0xff,
1308 .vlan_entry_tbl = vlan_entry_cpsw,
1311 .dev_id = "66ak2el",
1312 .features = CPSW_ALE_F_STATUS_REG,
1313 .major_ver_mask = 0x7,
1314 .nu_switch_ale = true,
1315 .vlan_entry_tbl = vlan_entry_nu,
1319 .features = CPSW_ALE_F_STATUS_REG,
1321 .major_ver_mask = 0x7,
1322 .nu_switch_ale = true,
1323 .vlan_entry_tbl = vlan_entry_nu,
1326 .dev_id = "am65x-cpsw2g",
1327 .features = CPSW_ALE_F_STATUS_REG | CPSW_ALE_F_HW_AUTOAGING,
1329 .major_ver_mask = 0x7,
1330 .nu_switch_ale = true,
1331 .vlan_entry_tbl = vlan_entry_nu,
1334 .dev_id = "j721e-cpswxg",
1335 .features = CPSW_ALE_F_STATUS_REG | CPSW_ALE_F_HW_AUTOAGING,
1336 .major_ver_mask = 0x7,
1337 .vlan_entry_tbl = vlan_entry_k3_cpswxg,
1340 .dev_id = "am64-cpswxg",
1341 .features = CPSW_ALE_F_STATUS_REG | CPSW_ALE_F_HW_AUTOAGING,
1342 .major_ver_mask = 0x7,
1343 .vlan_entry_tbl = vlan_entry_k3_cpswxg,
1350 cpsw_ale_dev_id *cpsw_ale_match_id(const struct cpsw_ale_dev_id *id,
1356 while (id->dev_id) {
1357 if (strcmp(dev_id, id->dev_id) == 0)
1364 struct cpsw_ale *cpsw_ale_create(struct cpsw_ale_params *params)
1366 const struct cpsw_ale_dev_id *ale_dev_id;
1367 struct cpsw_ale *ale;
1368 u32 rev, ale_entries;
1370 ale_dev_id = cpsw_ale_match_id(cpsw_ale_id_match, params->dev_id);
1372 return ERR_PTR(-EINVAL);
1374 params->ale_entries = ale_dev_id->tbl_entries;
1375 params->major_ver_mask = ale_dev_id->major_ver_mask;
1376 params->nu_switch_ale = ale_dev_id->nu_switch_ale;
1378 ale = devm_kzalloc(params->dev, sizeof(*ale), GFP_KERNEL);
1380 return ERR_PTR(-ENOMEM);
1382 ale->p0_untag_vid_mask = devm_bitmap_zalloc(params->dev, VLAN_N_VID,
1384 if (!ale->p0_untag_vid_mask)
1385 return ERR_PTR(-ENOMEM);
1387 ale->params = *params;
1388 ale->ageout = ale->params.ale_ageout * HZ;
1389 ale->features = ale_dev_id->features;
1390 ale->vlan_entry_tbl = ale_dev_id->vlan_entry_tbl;
1392 rev = readl_relaxed(ale->params.ale_regs + ALE_IDVER);
1394 (ALE_VERSION_MAJOR(rev, ale->params.major_ver_mask) << 8) |
1395 ALE_VERSION_MINOR(rev);
1396 dev_info(ale->params.dev, "initialized cpsw ale version %d.%d\n",
1397 ALE_VERSION_MAJOR(rev, ale->params.major_ver_mask),
1398 ALE_VERSION_MINOR(rev));
1400 if (ale->features & CPSW_ALE_F_STATUS_REG &&
1401 !ale->params.ale_entries) {
1403 readl_relaxed(ale->params.ale_regs + ALE_STATUS) &
1404 ALE_STATUS_SIZE_MASK;
1405 /* ALE available on newer NetCP switches has introduced
1406 * a register, ALE_STATUS, to indicate the size of ALE
1407 * table which shows the size as a multiple of 1024 entries.
1408 * For these, params.ale_entries will be set to zero. So
1409 * read the register and update the value of ale_entries.
1410 * return error if ale_entries is zero in ALE_STATUS.
1413 return ERR_PTR(-EINVAL);
1415 ale_entries *= ALE_TABLE_SIZE_MULTIPLIER;
1416 ale->params.ale_entries = ale_entries;
1418 dev_info(ale->params.dev,
1419 "ALE Table size %ld\n", ale->params.ale_entries);
1421 /* set default bits for existing h/w */
1422 ale->port_mask_bits = ale->params.ale_ports;
1423 ale->port_num_bits = order_base_2(ale->params.ale_ports);
1424 ale->vlan_field_bits = ale->params.ale_ports;
1426 /* Set defaults override for ALE on NetCP NU switch and for version
1429 if (ale->params.nu_switch_ale) {
1430 /* Separate registers for unknown vlan configuration.
1431 * Also there are N bits, where N is number of ale
1432 * ports and shift value should be 0
1434 ale_controls[ALE_PORT_UNKNOWN_VLAN_MEMBER].bits =
1435 ale->params.ale_ports;
1436 ale_controls[ALE_PORT_UNKNOWN_VLAN_MEMBER].offset =
1437 ALE_UNKNOWNVLAN_MEMBER;
1438 ale_controls[ALE_PORT_UNKNOWN_MCAST_FLOOD].bits =
1439 ale->params.ale_ports;
1440 ale_controls[ALE_PORT_UNKNOWN_MCAST_FLOOD].shift = 0;
1441 ale_controls[ALE_PORT_UNKNOWN_MCAST_FLOOD].offset =
1442 ALE_UNKNOWNVLAN_UNREG_MCAST_FLOOD;
1443 ale_controls[ALE_PORT_UNKNOWN_REG_MCAST_FLOOD].bits =
1444 ale->params.ale_ports;
1445 ale_controls[ALE_PORT_UNKNOWN_REG_MCAST_FLOOD].shift = 0;
1446 ale_controls[ALE_PORT_UNKNOWN_REG_MCAST_FLOOD].offset =
1447 ALE_UNKNOWNVLAN_REG_MCAST_FLOOD;
1448 ale_controls[ALE_PORT_UNTAGGED_EGRESS].bits =
1449 ale->params.ale_ports;
1450 ale_controls[ALE_PORT_UNTAGGED_EGRESS].shift = 0;
1451 ale_controls[ALE_PORT_UNTAGGED_EGRESS].offset =
1452 ALE_UNKNOWNVLAN_FORCE_UNTAG_EGRESS;
1455 cpsw_ale_control_set(ale, 0, ALE_CLEAR, 1);
1459 void cpsw_ale_dump(struct cpsw_ale *ale, u32 *data)
1463 for (i = 0; i < ale->params.ale_entries; i++) {
1464 cpsw_ale_read(ale, i, data);
1465 data += ALE_ENTRY_WORDS;
1469 void cpsw_ale_restore(struct cpsw_ale *ale, u32 *data)
1473 for (i = 0; i < ale->params.ale_entries; i++) {
1474 cpsw_ale_write(ale, i, data);
1475 data += ALE_ENTRY_WORDS;
1479 u32 cpsw_ale_get_num_entries(struct cpsw_ale *ale)
1481 return ale ? ale->params.ale_entries : 0;