Add MAC table support, and dsa fdb callback integration. The mactable is keyed on (MAC,VID) and each row has 4 buckets. A mac table entry typically points to a PGID index, the first 9 of which represent a front port. Enable FDB isolation and assisted learning on the CPU port. FDB entries without a VID are installed in the reserved VID of their bridge, or in HOST_PVID for standalone ports. The FDB dump reports locked entries as static. A CPU learn into a row whose four buckets all hold locked or IP multicast entries is dropped by the hardware, and only flagged in ANA_ANEVENTS.CPU_LEARN_FAILED. Report it as -ENOSPC. Signed-off-by: Jens Emil Schulz Østergaard --- Changes in v2: - Clear ANA_ANEVENTS.CPU_LEARN_FAILED before each CPU learn instead of after a failed one and in lan9645x_mac_init(). --- drivers/net/dsa/microchip/lan9645x/Makefile | 1 + drivers/net/dsa/microchip/lan9645x/lan9645x_mac.c | 264 +++++++++++++++++++++ drivers/net/dsa/microchip/lan9645x/lan9645x_main.c | 118 +++++++++ drivers/net/dsa/microchip/lan9645x/lan9645x_main.h | 42 ++++ 4 files changed, 425 insertions(+) diff --git a/drivers/net/dsa/microchip/lan9645x/Makefile b/drivers/net/dsa/microchip/lan9645x/Makefile index e049114b3563..70815edca5b9 100644 --- a/drivers/net/dsa/microchip/lan9645x/Makefile +++ b/drivers/net/dsa/microchip/lan9645x/Makefile @@ -2,6 +2,7 @@ obj-$(CONFIG_NET_DSA_MICROCHIP_LAN9645X) += mchp-lan9645x.o mchp-lan9645x-objs := \ + lan9645x_mac.o \ lan9645x_main.o \ lan9645x_npi.o \ lan9645x_phylink.o \ diff --git a/drivers/net/dsa/microchip/lan9645x/lan9645x_mac.c b/drivers/net/dsa/microchip/lan9645x/lan9645x_mac.c new file mode 100644 index 000000000000..21676cc84bc9 --- /dev/null +++ b/drivers/net/dsa/microchip/lan9645x/lan9645x_mac.c @@ -0,0 +1,264 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* Copyright (C) 2026 Microchip Technology Inc. + */ + +#include "lan9645x_main.h" + +#define CMD_IDLE 0 +#define CMD_LEARN 1 +#define CMD_FORGET 2 +#define CMD_AGE 3 +#define CMD_GET_NEXT 4 +#define CMD_INIT 5 +#define CMD_READ 6 +#define CMD_WRITE 7 +#define CMD_SYNC_GET_NEXT 8 + +/* The chip has 2048 * 4 = 8192 total entries in the mac table */ +#define MACT_ROWS (FIELD_MAX(ANA_MACTINDX_M_INDEX) + 1) +#define MACT_BUCKETS (FIELD_MAX(ANA_MACTINDX_BUCKET) + 1) +#define MACT_ENTRIES (MACT_ROWS * MACT_BUCKETS) +#define MACT_GET_NEXT_MAX (MACT_ENTRIES + 1) + +static int lan9645x_mac_wait_for_completion(struct lan9645x *lan9645x, + u32 *maca) +{ + u32 val = 0; + int err; + + lockdep_assert_held(&lan9645x->mact_lock); + + err = lan9645x_rd_poll_timeout(lan9645x, ANA_MACACCESS, val, + ANA_MACACCESS_MAC_TABLE_CMD_GET(val) == + CMD_IDLE); + if (err) + return err; + + if (maca) + *maca = val; + + return 0; +} + +static void lan9645x_mac_select(struct lan9645x *lan9645x, + const unsigned char mac[ETH_ALEN], + unsigned int vid) +{ + u64 maddr = ether_addr_to_u64(mac); + + lockdep_assert_held(&lan9645x->mact_lock); + + lan_wr(ANA_MACHDATA_VID_SET(vid) | + ANA_MACHDATA_MACHDATA_SET(upper_32_bits(maddr)), + lan9645x, + ANA_MACHDATA); + + lan_wr(lower_32_bits(maddr), + lan9645x, + ANA_MACLDATA); +} + +static int __lan9645x_mact_forget(struct lan9645x *lan9645x, + const unsigned char mac[ETH_ALEN], + unsigned int vid, + enum macaccess_entry_type type) +{ + lockdep_assert_held(&lan9645x->mact_lock); + + lan9645x_mac_select(lan9645x, mac, vid); + + lan_wr(ANA_MACACCESS_ENTRYTYPE_SET(type) | + ANA_MACACCESS_MAC_TABLE_CMD_SET(CMD_FORGET), + lan9645x, + ANA_MACACCESS); + + return lan9645x_mac_wait_for_completion(lan9645x, NULL); +} + +int lan9645x_mact_forget(struct lan9645x *lan9645x, + const unsigned char mac[ETH_ALEN], unsigned int vid, + enum macaccess_entry_type type) +{ + int err; + + mutex_lock(&lan9645x->mact_lock); + err = __lan9645x_mact_forget(lan9645x, mac, vid, type); + mutex_unlock(&lan9645x->mact_lock); + + return err; +} + +static int __lan9645x_mact_learn(struct lan9645x *lan9645x, int port, + const unsigned char mac[ETH_ALEN], + unsigned int vid, + enum macaccess_entry_type type, + bool cpu_copy) +{ + int err; + + lockdep_assert_held(&lan9645x->mact_lock); + + lan9645x_mac_select(lan9645x, mac, vid); + lan_wr(ANA_ANEVENTS_CPU_LEARN_FAILED, lan9645x, ANA_ANEVENTS); + + lan_wr(ANA_MACACCESS_VALID_SET(1) | + ANA_MACACCESS_DEST_IDX_SET(port) | + ANA_MACACCESS_MAC_CPU_COPY_SET(cpu_copy) | + ANA_MACACCESS_ENTRYTYPE_SET(type) | + ANA_MACACCESS_MAC_TABLE_CMD_SET(CMD_LEARN), + lan9645x, ANA_MACACCESS); + + err = lan9645x_mac_wait_for_completion(lan9645x, NULL); + if (err) + return err; + + if (ANA_ANEVENTS_CPU_LEARN_FAILED_GET(lan_rd(lan9645x, ANA_ANEVENTS))) + return -ENOSPC; + + return 0; +} + +int lan9645x_mact_learn(struct lan9645x *lan9645x, int port, + const unsigned char mac[ETH_ALEN], unsigned int vid, + enum macaccess_entry_type type, bool cpu_copy) +{ + int err; + + mutex_lock(&lan9645x->mact_lock); + err = __lan9645x_mact_learn(lan9645x, port, mac, vid, type, cpu_copy); + mutex_unlock(&lan9645x->mact_lock); + + return err; +} + +int lan9645x_mact_flush(struct lan9645x *lan9645x, int port) +{ + int err; + + mutex_lock(&lan9645x->mact_lock); + /* MAC table entries with dst index matching port are aged on scan. */ + lan_wr(ANA_ANAGEFIL_PID_EN_SET(1) | + ANA_ANAGEFIL_PID_VAL_SET(port), + lan9645x, ANA_ANAGEFIL); + + /* Flushing requires two scans. First sets AGE_FLAG=1, second removes + * entries with AGE_FLAG=1. + */ + lan_wr(ANA_MACACCESS_MAC_TABLE_CMD_SET(CMD_AGE), + lan9645x, + ANA_MACACCESS); + + err = lan9645x_mac_wait_for_completion(lan9645x, NULL); + if (err) + goto mact_unlock; + + lan_wr(ANA_MACACCESS_MAC_TABLE_CMD_SET(CMD_AGE), + lan9645x, + ANA_MACACCESS); + + err = lan9645x_mac_wait_for_completion(lan9645x, NULL); + +mact_unlock: + lan_wr(0, lan9645x, ANA_ANAGEFIL); + mutex_unlock(&lan9645x->mact_lock); + return err; +} + +int lan9645x_mac_init(struct lan9645x *lan9645x) +{ + u32 val; + int err; + + /* Clear the MAC table */ + lan_wr(ANA_MACACCESS_MAC_TABLE_CMD_SET(CMD_INIT), lan9645x, + ANA_MACACCESS); + + err = lan9645x_rd_poll_timeout(lan9645x, ANA_MACACCESS, val, + ANA_MACACCESS_MAC_TABLE_CMD_GET(val) == + CMD_IDLE); + if (err) { + dev_err(lan9645x->dev, "MAC table clear timeout\n"); + return err; + } + + mutex_init(&lan9645x->mact_lock); + return 0; +} + +void lan9645x_mac_deinit(struct lan9645x *lan9645x) +{ + mutex_destroy(&lan9645x->mact_lock); +} + +int lan9645x_mact_dsa_dump(struct lan9645x *lan9645x, int port, + dsa_fdb_dump_cb_t *cb, void *data) +{ + u8 mac[ETH_ALEN] __aligned(2); + u32 mach, macl, maca; + int err = 0; + u64 addr; + int iter; + u16 vid; + u8 type; + + mutex_lock(&lan9645x->mact_lock); + + lan_wr(ANA_ANAGEFIL_PID_EN_SET(1) | + ANA_ANAGEFIL_PID_VAL_SET(port), + lan9645x, ANA_ANAGEFIL); + + lan_wr(0, lan9645x, ANA_MACHDATA); + lan_wr(0, lan9645x, ANA_MACLDATA); + + type = ENTRYTYPE_NORMAL; + + for (iter = 0; iter < MACT_GET_NEXT_MAX; iter++) { + /* CMD_GET_NEXT returns the smallest entry ordered above the one + * left in the registers. + * We rely on mach, macl and type being set correctly in + * the registers from previous round, vis a vis the GET_NEXT + * semantics, so locking entire loop is important. + */ + lan_wr(ANA_MACACCESS_MAC_TABLE_CMD_SET(CMD_GET_NEXT) | + ANA_MACACCESS_ENTRYTYPE_SET(type), + lan9645x, ANA_MACACCESS); + + err = lan9645x_mac_wait_for_completion(lan9645x, &maca); + if (err) + break; + + if (ANA_MACACCESS_VALID_GET(maca) == 0) + break; + + type = ANA_MACACCESS_ENTRYTYPE_GET(maca); + mach = lan_rd(lan9645x, ANA_MACHDATA); + macl = lan_rd(lan9645x, ANA_MACLDATA); + + if (ANA_MACACCESS_DEST_IDX_GET(maca) == port && + (type == ENTRYTYPE_NORMAL || type == ENTRYTYPE_LOCKED)) { + addr = (u64)ANA_MACHDATA_MACHDATA_GET(mach) << 32 | + macl; + u64_to_ether_addr(addr, mac); + vid = ANA_MACHDATA_VID_GET(mach); + if (vid > VLAN_MAX) + vid = 0; + + err = cb(mac, vid, type == ENTRYTYPE_LOCKED, data); + if (err) + break; + } + } + + if (iter == MACT_GET_NEXT_MAX) { + dev_err(lan9645x->dev, + "MAC table walk on port %d did not terminate\n", port); + err = -EIO; + } + + /* Remove aging filters */ + lan_wr(0, lan9645x, ANA_ANAGEFIL); + + mutex_unlock(&lan9645x->mact_lock); + + return err; +} diff --git a/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c b/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c index b48359095574..c425153b4701 100644 --- a/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c +++ b/drivers/net/dsa/microchip/lan9645x/lan9645x_main.c @@ -71,6 +71,7 @@ static void lan9645x_teardown(struct dsa_switch *ds) { struct lan9645x *lan9645x = ds->priv; + lan9645x_mac_deinit(lan9645x); mutex_destroy(&lan9645x->fwd_domain_lock); lan9645x_npi_port_deinit(lan9645x, lan9645x->npi); } @@ -162,6 +163,9 @@ static int lan9645x_setup(struct dsa_switch *ds) mutex_init(&lan9645x->fwd_domain_lock); err = lan9645x_vlan_init(lan9645x); + if (err) + goto err_mutex; + err = lan9645x_mac_init(lan9645x); if (err) goto err_mutex; @@ -287,6 +291,8 @@ static int lan9645x_setup(struct dsa_switch *ds) ANA_PGID(PGID_BC)); ds->mtu_enforcement_ingress = true; + ds->assisted_learning_on_cpu_port = true; + ds->fdb_isolation = true; dev_info(lan9645x->dev, "SKU features: max_ports=%d\n", @@ -361,6 +367,112 @@ static int lan9645x_port_vlan_del(struct dsa_switch *ds, int port, return err; } +static void lan9645x_port_fast_age(struct dsa_switch *ds, int port) +{ + int err; + + err = lan9645x_mact_flush(ds->priv, port); + if (err) + dev_err(ds->dev, "Flushing MAC table on port %d returned %pe\n", + port, ERR_PTR(err)); +} + +static int lan9645x_fdb_dump(struct dsa_switch *ds, int port, + dsa_fdb_dump_cb_t *cb, void *data) +{ + return lan9645x_mact_dsa_dump(ds->priv, port, cb, data); +} + +static u16 lan9645x_fdb_vid(u16 vid, const struct dsa_db *db) +{ + if (vid) + return vid; + + return lan9645x_vlan_unaware_pvid(db->type == DSA_DB_BRIDGE ? + db->bridge.num : 0); +} + +static bool lan9645x_db_equal(const struct dsa_db *a, const struct dsa_db *b) +{ + if (a->type != b->type) + return false; + + switch (a->type) { + case DSA_DB_PORT: + return a->dp == b->dp; + case DSA_DB_LAG: + return a->lag.dev == b->lag.dev; + case DSA_DB_BRIDGE: + return a->bridge.num == b->bridge.num; + } + + return false; +} + +/* All host addresses point at the CPU, so host addresses of different + * databases with the same address and hardware VID are one MAC table + * entry. DSA only counts references within one database, so keep the + * entry while another database still uses it. dsa_fdb_present_in_other_db() + * compares the VID given by the bridge, not the hardware VID. VID 0 is + * installed in a different reserved VID for each VLAN-unaware bridge, so + * it would merge their host addresses. It also only compares databases of + * the same type, which misses a VLAN upper of a port and a bridge in the + * same VLAN. + */ +static bool lan9645x_host_addr_shared(struct dsa_port *dp, + const struct list_head *addrs, + const unsigned char *addr, u16 vid, + const struct dsa_db *db) +{ + u16 hw_vid = lan9645x_fdb_vid(vid, db); + struct dsa_mac_addr *a; + + lockdep_assert_held(&dp->addr_lists_lock); + + list_for_each_entry(a, addrs, list) { + if (ether_addr_equal(a->addr, addr) && + !lan9645x_db_equal(&a->db, db) && + lan9645x_fdb_vid(a->vid, &a->db) == hw_vid) + return true; + } + + return false; +} + +static int lan9645x_fdb_add(struct dsa_switch *ds, int port, + const unsigned char *addr, u16 vid, + struct dsa_db db) +{ + struct dsa_port *dp = dsa_to_port(ds, port); + struct lan9645x *lan9645x = ds->priv; + int dest; + + if (dsa_port_is_cpu(dp) && + lan9645x_host_addr_shared(dp, &dp->fdbs, addr, vid, &db)) + return 0; + + dest = dsa_port_is_cpu(dp) ? PGID_CPU : port; + + return lan9645x_mact_learn(lan9645x, dest, addr, + lan9645x_fdb_vid(vid, &db), + ENTRYTYPE_LOCKED, false); +} + +static int lan9645x_fdb_del(struct dsa_switch *ds, int port, + const unsigned char *addr, u16 vid, + struct dsa_db db) +{ + struct dsa_port *dp = dsa_to_port(ds, port); + struct lan9645x *lan9645x = ds->priv; + + if (dsa_port_is_cpu(dp) && + lan9645x_host_addr_shared(dp, &dp->fdbs, addr, vid, &db)) + return 0; + + return lan9645x_mact_forget(lan9645x, addr, lan9645x_fdb_vid(vid, &db), + ENTRYTYPE_LOCKED); +} + static const struct dsa_switch_ops lan9645x_switch_ops = { .get_tag_protocol = lan9645x_get_tag_protocol, @@ -379,6 +491,12 @@ static const struct dsa_switch_ops lan9645x_switch_ops = { .port_vlan_filtering = lan9645x_port_vlan_filtering, .port_vlan_add = lan9645x_port_vlan_add, .port_vlan_del = lan9645x_port_vlan_del, + + /* MAC table integration */ + .port_fast_age = lan9645x_port_fast_age, + .port_fdb_dump = lan9645x_fdb_dump, + .port_fdb_add = lan9645x_fdb_add, + .port_fdb_del = lan9645x_fdb_del, }; static int lan9645x_request_target_regmaps(struct lan9645x *lan9645x) diff --git a/drivers/net/dsa/microchip/lan9645x/lan9645x_main.h b/drivers/net/dsa/microchip/lan9645x/lan9645x_main.h index 613cfcbccd44..eb8752e5da7c 100644 --- a/drivers/net/dsa/microchip/lan9645x/lan9645x_main.h +++ b/drivers/net/dsa/microchip/lan9645x/lan9645x_main.h @@ -170,6 +170,19 @@ struct lan9645x_vlan { untagged: 9; /* ports 0-8 */ }; +/* MAC table entry types. + * ENTRYTYPE_NORMAL is subject to aging. + * ENTRYTYPE_LOCKED is not subject to aging. + * ENTRYTYPE_MACV4 is not subject to aging. For IPv4 multicast. + * ENTRYTYPE_MACV6 is not subject to aging. For IPv6 multicast. + */ +enum macaccess_entry_type { + ENTRYTYPE_NORMAL = 0, + ENTRYTYPE_LOCKED, + ENTRYTYPE_MACV4, + ENTRYTYPE_MACV6, +}; + struct lan9645x { struct device *dev; struct dsa_switch *ds; @@ -183,6 +196,7 @@ struct lan9645x { /* lock forwarding configuration and vlan table */ struct mutex fwd_domain_lock; + struct mutex mact_lock; /* serialize mac table register access */ int num_port_dis; @@ -277,6 +291,21 @@ static inline u32 lan_rel_addr(enum lan9645x_target t, int tinst, int tcnt, rcnt, rwidth); } +static inline u32 lan_rd(struct lan9645x *lan9645x, enum lan9645x_target t, + int tinst, int tcnt, int gbase, int ginst, + int gcnt, int gwidth, int raddr, int rinst, + int rcnt, int rwidth) +{ + u32 addr, val = 0; + + addr = lan_rel_addr(t, tinst, tcnt, gbase, ginst, gcnt, gwidth, + raddr, rinst, rcnt, rwidth); + + WARN_ON_ONCE(regmap_read(lan_tgt2rmap(lan9645x, t, tinst), addr, &val)); + + return val; +} + static inline struct regmap *lan_rmap(struct lan9645x *lan9645x, enum lan9645x_target t, int tinst, int tcnt, int gbase, int ginst, @@ -340,4 +369,17 @@ int lan9645x_vlan_port_add_vlan(struct lan9645x_port *p, u16 vid, bool pvid, int lan9645x_vlan_port_del_vlan(struct lan9645x_port *p, u16 vid); void lan9645x_vlan_set_hostmode(struct lan9645x_port *p); +/* MAC table: lan9645x_mac.c */ +int lan9645x_mact_flush(struct lan9645x *lan9645x, int port); +int lan9645x_mact_learn(struct lan9645x *lan9645x, int port, + const unsigned char mac[ETH_ALEN], unsigned int vid, + enum macaccess_entry_type type, bool cpu_copy); +int lan9645x_mact_forget(struct lan9645x *lan9645x, + const unsigned char mac[ETH_ALEN], unsigned int vid, + enum macaccess_entry_type type); +int lan9645x_mac_init(struct lan9645x *lan9645x); +void lan9645x_mac_deinit(struct lan9645x *lan9645x); +int lan9645x_mact_dsa_dump(struct lan9645x *lan9645x, int port, + dsa_fdb_dump_cb_t *cb, void *data); + #endif /* __LAN9645X_MAIN_H__ */ -- 2.52.0