RTL8366RB is the sole user of the VLAN callbacks in struct realtek_ops. RTL8365MB handles VLAN configuration through its own DSA switch operations. The abstraction is useless, the ASICs differ too much. Move the RTL8366 VLAN helpers and table entry types into rtl8366rb.c. Call the register helpers directly and wire the driver-local VLAN callbacks into the DSA switch operations. Keep the 4K mode flag in the chip data and use the fixed size of the RTL8366RB member table. Signed-off-by: Linus Walleij <linu…@kernel.org> --- drivers/net/dsa/realtek/realtek.h | 48 ---- drivers/net/dsa/realtek/rtl8366-core.c | 379 ----------------------------- drivers/net/dsa/realtek/rtl8366rb.c | 419 +++++++++++++++++++++++++++++++-- drivers/net/dsa/realtek/rtl8366rb.h | 2 + 4 files changed, 401 insertions(+), 447 deletions(-) diff --git a/drivers/net/dsa/realtek/realtek.h b/drivers/net/dsa/realtek/realtek.h index cfe56d30f5cb..977a5183141c 100644 --- a/drivers/net/dsa/realtek/realtek.h +++ b/drivers/net/dsa/realtek/realtek.h @@ -27,24 +27,6 @@ struct rtl8366_mib_counter { const char *name; }; -/* - * struct rtl8366_vlan_mc - Virtual LAN member configuration - */ -struct rtl8366_vlan_mc { - u16 vid; - u16 untag; - u16 member; - u8 fid; - u8 priority; -}; - -struct rtl8366_vlan_4k { - u16 vid; - u16 untag; - u16 member; - u8 fid; -}; - struct realtek_fdb_entry { u8 mac_addr[ETH_ALEN]; u16 vid; @@ -87,16 +69,12 @@ struct realtek_priv { unsigned int cpu_port; unsigned int num_ports; - unsigned int num_vlan_mc; unsigned int num_mib_counters; struct rtl8366_mib_counter *mib_counters; const struct realtek_ops *ops; int (*write_reg_noack)(void *ctx, u32 addr, u32 data); - int vlan_enabled; - int vlan4k_enabled; - void *chip_data; /* Per-chip extra variant data */ }; @@ -121,19 +99,6 @@ struct realtek_ops { int port, struct rtl8366_mib_counter *mib, u64 *mibvalue); - int (*get_vlan_mc)(struct realtek_priv *priv, u32 index, - struct rtl8366_vlan_mc *vlanmc); - int (*set_vlan_mc)(struct realtek_priv *priv, u32 index, - const struct rtl8366_vlan_mc *vlanmc); - int (*get_vlan_4k)(struct realtek_priv *priv, u32 vid, - struct rtl8366_vlan_4k *vlan4k); - int (*set_vlan_4k)(struct realtek_priv *priv, - const struct rtl8366_vlan_4k *vlan4k); - int (*get_mc_index)(struct realtek_priv *priv, int port, int *val); - int (*set_mc_index)(struct realtek_priv *priv, int port, int index); - bool (*is_vlan_valid)(struct realtek_priv *priv, unsigned int vlan); - int (*enable_vlan)(struct realtek_priv *priv, bool enable); - int (*enable_vlan4k)(struct realtek_priv *priv, bool enable); int (*enable_port)(struct realtek_priv *priv, int port, bool enable); int (*port_add_isolation)(struct realtek_priv *priv, int port, u32 mask); @@ -208,19 +173,6 @@ struct realtek_variant { }; /* RTL8366 library helpers */ -int rtl8366_mc_is_used(struct realtek_priv *priv, int mc_index, int *used); -int rtl8366_set_vlan(struct realtek_priv *priv, int vid, u32 member, - u32 untag, u32 fid); -int rtl8366_set_pvid(struct realtek_priv *priv, unsigned int port, - unsigned int vid); -int rtl8366_enable_vlan4k(struct realtek_priv *priv, bool enable); -int rtl8366_enable_vlan(struct realtek_priv *priv, bool enable); -int rtl8366_reset_vlan(struct realtek_priv *priv); -int rtl8366_vlan_add(struct dsa_switch *ds, int port, - const struct switchdev_obj_port_vlan *vlan, - struct netlink_ext_ack *extack); -int rtl8366_vlan_del(struct dsa_switch *ds, int port, - const struct switchdev_obj_port_vlan *vlan); void rtl8366_get_strings(struct dsa_switch *ds, int port, u32 stringset, uint8_t *data); int rtl8366_get_sset_count(struct dsa_switch *ds, int port, int sset); diff --git a/drivers/net/dsa/realtek/rtl8366-core.c b/drivers/net/dsa/realtek/rtl8366-core.c index 047feeed96a2..d969e29f99c2 100644 --- a/drivers/net/dsa/realtek/rtl8366-core.c +++ b/drivers/net/dsa/realtek/rtl8366-core.c @@ -8,389 +8,10 @@ * Copyright (C) 2010 Roman Yeryomin <rom…@advem.lv> * Copyright (C) 2011 Colin Leitner <coli…@googlemail.com> */ -#include <linux/if_bridge.h> #include <net/dsa.h> #include "realtek.h" -int rtl8366_mc_is_used(struct realtek_priv *priv, int mc_index, int *used) -{ - int ret; - int i; - - *used = 0; - for (i = 0; i < priv->num_ports; i++) { - int index = 0; - - ret = priv->ops->get_mc_index(priv, i, &index); - if (ret) - return ret; - - if (mc_index == index) { - *used = 1; - break; - } - } - - return 0; -} -EXPORT_SYMBOL_NS_GPL(rtl8366_mc_is_used, "REALTEK_DSA"); - -/** - * rtl8366_obtain_mc() - retrieve or allocate a VLAN member configuration - * @priv: the Realtek SMI device instance - * @vid: the VLAN ID to look up or allocate - * @vlanmc: the pointer will be assigned to a pointer to a valid member config - * if successful - * @return: index of a new member config or negative error number - */ -static int rtl8366_obtain_mc(struct realtek_priv *priv, int vid, - struct rtl8366_vlan_mc *vlanmc) -{ - struct rtl8366_vlan_4k vlan4k; - int ret; - int i; - - /* Try to find an existing member config entry for this VID */ - for (i = 0; i < priv->num_vlan_mc; i++) { - ret = priv->ops->get_vlan_mc(priv, i, vlanmc); - if (ret) { - dev_err(priv->dev, "error searching for VLAN MC %d for VID %d\n", - i, vid); - return ret; - } - - if (vid == vlanmc->vid) - return i; - } - - /* We have no MC entry for this VID, try to find an empty one */ - for (i = 0; i < priv->num_vlan_mc; i++) { - ret = priv->ops->get_vlan_mc(priv, i, vlanmc); - if (ret) { - dev_err(priv->dev, "error searching for VLAN MC %d for VID %d\n", - i, vid); - return ret; - } - - if (vlanmc->vid == 0 && vlanmc->member == 0) { - /* Update the entry from the 4K table */ - ret = priv->ops->get_vlan_4k(priv, vid, &vlan4k); - if (ret) { - dev_err(priv->dev, "error looking for 4K VLAN MC %d for VID %d\n", - i, vid); - return ret; - } - - vlanmc->vid = vid; - vlanmc->member = vlan4k.member; - vlanmc->untag = vlan4k.untag; - vlanmc->fid = vlan4k.fid; - ret = priv->ops->set_vlan_mc(priv, i, vlanmc); - if (ret) { - dev_err(priv->dev, "unable to set/update VLAN MC %d for VID %d\n", - i, vid); - return ret; - } - - dev_dbg(priv->dev, "created new MC at index %d for VID %d\n", - i, vid); - return i; - } - } - - /* MC table is full, try to find an unused entry and replace it */ - for (i = 0; i < priv->num_vlan_mc; i++) { - int used; - - ret = rtl8366_mc_is_used(priv, i, &used); - if (ret) - return ret; - - if (!used) { - /* Update the entry from the 4K table */ - ret = priv->ops->get_vlan_4k(priv, vid, &vlan4k); - if (ret) - return ret; - - vlanmc->vid = vid; - vlanmc->member = vlan4k.member; - vlanmc->untag = vlan4k.untag; - vlanmc->fid = vlan4k.fid; - ret = priv->ops->set_vlan_mc(priv, i, vlanmc); - if (ret) { - dev_err(priv->dev, "unable to set/update VLAN MC %d for VID %d\n", - i, vid); - return ret; - } - dev_dbg(priv->dev, "recycled MC at index %i for VID %d\n", - i, vid); - return i; - } - } - - dev_err(priv->dev, "all VLAN member configurations are in use\n"); - return -ENOSPC; -} - -int rtl8366_set_vlan(struct realtek_priv *priv, int vid, u32 member, - u32 untag, u32 fid) -{ - struct rtl8366_vlan_mc vlanmc; - struct rtl8366_vlan_4k vlan4k; - int mc; - int ret; - - if (!priv->ops->is_vlan_valid(priv, vid)) - return -EINVAL; - - dev_dbg(priv->dev, - "setting VLAN%d 4k members: 0x%02x, untagged: 0x%02x\n", - vid, member, untag); - - /* Update the 4K table */ - ret = priv->ops->get_vlan_4k(priv, vid, &vlan4k); - if (ret) - return ret; - - vlan4k.member |= member; - vlan4k.untag |= untag; - vlan4k.fid = fid; - ret = priv->ops->set_vlan_4k(priv, &vlan4k); - if (ret) - return ret; - - dev_dbg(priv->dev, - "resulting VLAN%d 4k members: 0x%02x, untagged: 0x%02x\n", - vid, vlan4k.member, vlan4k.untag); - - /* Find or allocate a member config for this VID */ - ret = rtl8366_obtain_mc(priv, vid, &vlanmc); - if (ret < 0) - return ret; - mc = ret; - - /* Update the MC entry */ - vlanmc.member |= member; - vlanmc.untag |= untag; - vlanmc.fid = fid; - - /* Commit updates to the MC entry */ - ret = priv->ops->set_vlan_mc(priv, mc, &vlanmc); - if (ret) - dev_err(priv->dev, "failed to commit changes to VLAN MC index %d for VID %d\n", - mc, vid); - else - dev_dbg(priv->dev, - "resulting VLAN%d MC members: 0x%02x, untagged: 0x%02x\n", - vid, vlanmc.member, vlanmc.untag); - - return ret; -} -EXPORT_SYMBOL_NS_GPL(rtl8366_set_vlan, "REALTEK_DSA"); - -int rtl8366_set_pvid(struct realtek_priv *priv, unsigned int port, - unsigned int vid) -{ - struct rtl8366_vlan_mc vlanmc; - int mc; - int ret; - - if (!priv->ops->is_vlan_valid(priv, vid)) - return -EINVAL; - - /* Find or allocate a member config for this VID */ - ret = rtl8366_obtain_mc(priv, vid, &vlanmc); - if (ret < 0) - return ret; - mc = ret; - - ret = priv->ops->set_mc_index(priv, port, mc); - if (ret) { - dev_err(priv->dev, "set PVID: failed to set MC index %d for port %d\n", - mc, port); - return ret; - } - - dev_dbg(priv->dev, "set PVID: the PVID for port %d set to %d using existing MC index %d\n", - port, vid, mc); - - return 0; -} -EXPORT_SYMBOL_NS_GPL(rtl8366_set_pvid, "REALTEK_DSA"); - -int rtl8366_enable_vlan4k(struct realtek_priv *priv, bool enable) -{ - int ret; - - /* To enable 4k VLAN, ordinary VLAN must be enabled first, - * but if we disable 4k VLAN it is fine to leave ordinary - * VLAN enabled. - */ - if (enable) { - /* Make sure VLAN is ON */ - ret = priv->ops->enable_vlan(priv, true); - if (ret) - return ret; - - priv->vlan_enabled = true; - } - - ret = priv->ops->enable_vlan4k(priv, enable); - if (ret) - return ret; - - priv->vlan4k_enabled = enable; - return 0; -} -EXPORT_SYMBOL_NS_GPL(rtl8366_enable_vlan4k, "REALTEK_DSA"); - -int rtl8366_enable_vlan(struct realtek_priv *priv, bool enable) -{ - int ret; - - ret = priv->ops->enable_vlan(priv, enable); - if (ret) - return ret; - - priv->vlan_enabled = enable; - - /* If we turn VLAN off, make sure that we turn off - * 4k VLAN as well, if that happened to be on. - */ - if (!enable) { - priv->vlan4k_enabled = false; - ret = priv->ops->enable_vlan4k(priv, false); - } - - return ret; -} -EXPORT_SYMBOL_NS_GPL(rtl8366_enable_vlan, "REALTEK_DSA"); - -int rtl8366_reset_vlan(struct realtek_priv *priv) -{ - struct rtl8366_vlan_mc vlanmc; - int ret; - int i; - - rtl8366_enable_vlan(priv, false); - rtl8366_enable_vlan4k(priv, false); - - /* Clear the 16 VLAN member configurations */ - vlanmc.vid = 0; - vlanmc.priority = 0; - vlanmc.member = 0; - vlanmc.untag = 0; - vlanmc.fid = 0; - for (i = 0; i < priv->num_vlan_mc; i++) { - ret = priv->ops->set_vlan_mc(priv, i, &vlanmc); - if (ret) - return ret; - } - - return 0; -} -EXPORT_SYMBOL_NS_GPL(rtl8366_reset_vlan, "REALTEK_DSA"); - -int rtl8366_vlan_add(struct dsa_switch *ds, int port, - const struct switchdev_obj_port_vlan *vlan, - struct netlink_ext_ack *extack) -{ - bool untagged = !!(vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED); - bool pvid = !!(vlan->flags & BRIDGE_VLAN_INFO_PVID); - struct realtek_priv *priv = ds->priv; - u32 member = 0; - u32 untag = 0; - int ret; - - if (!priv->ops->is_vlan_valid(priv, vlan->vid)) { - NL_SET_ERR_MSG_MOD(extack, "VLAN ID not valid"); - return -EINVAL; - } - - /* Enable VLAN in the hardware - * FIXME: what's with this 4k business? - * Just rtl8366_enable_vlan() seems inconclusive. - */ - ret = rtl8366_enable_vlan4k(priv, true); - if (ret) { - NL_SET_ERR_MSG_MOD(extack, "Failed to enable VLAN 4K"); - return ret; - } - - dev_dbg(priv->dev, "add VLAN %d on port %d, %s, %s\n", - vlan->vid, port, untagged ? "untagged" : "tagged", - pvid ? "PVID" : "no PVID"); - - member |= BIT(port); - - if (untagged) - untag |= BIT(port); - - ret = rtl8366_set_vlan(priv, vlan->vid, member, untag, 0); - if (ret) { - dev_err(priv->dev, "failed to set up VLAN %04x", vlan->vid); - return ret; - } - - if (!pvid) - return 0; - - ret = rtl8366_set_pvid(priv, port, vlan->vid); - if (ret) { - dev_err(priv->dev, "failed to set PVID on port %d to VLAN %04x", - port, vlan->vid); - return ret; - } - - return 0; -} -EXPORT_SYMBOL_NS_GPL(rtl8366_vlan_add, "REALTEK_DSA"); - -int rtl8366_vlan_del(struct dsa_switch *ds, int port, - const struct switchdev_obj_port_vlan *vlan) -{ - struct realtek_priv *priv = ds->priv; - int ret, i; - - dev_dbg(priv->dev, "del VLAN %d on port %d\n", vlan->vid, port); - - for (i = 0; i < priv->num_vlan_mc; i++) { - struct rtl8366_vlan_mc vlanmc; - - ret = priv->ops->get_vlan_mc(priv, i, &vlanmc); - if (ret) - return ret; - - if (vlan->vid == vlanmc.vid) { - /* Remove this port from the VLAN */ - vlanmc.member &= ~BIT(port); - vlanmc.untag &= ~BIT(port); - /* - * If no ports are members of this VLAN - * anymore then clear the whole member - * config so it can be reused. - */ - if (!vlanmc.member) { - vlanmc.vid = 0; - vlanmc.priority = 0; - vlanmc.fid = 0; - } - ret = priv->ops->set_vlan_mc(priv, i, &vlanmc); - if (ret) { - dev_err(priv->dev, - "failed to remove VLAN %04x\n", - vlan->vid); - return ret; - } - break; - } - } - - return 0; -} -EXPORT_SYMBOL_NS_GPL(rtl8366_vlan_del, "REALTEK_DSA"); - void rtl8366_get_strings(struct dsa_switch *ds, int port, u32 stringset, uint8_t *data) { diff --git a/drivers/net/dsa/realtek/rtl8366rb.c b/drivers/net/dsa/realtek/rtl8366rb.c index 093a06c599d4..2e6ce1261a8c 100644 --- a/drivers/net/dsa/realtek/rtl8366rb.c +++ b/drivers/net/dsa/realtek/rtl8366rb.c @@ -863,6 +863,8 @@ static int rtl8366rb_port_set_learning(struct realtek_priv *priv, int port, BIT(port), enable ? 0 : BIT(port)); } +static int rtl8366rb_reset_vlan(struct realtek_priv *priv); + static int rtl8366rb_setup(struct dsa_switch *ds) { struct realtek_priv *priv = ds->priv; @@ -1110,7 +1112,7 @@ static int rtl8366rb_setup(struct dsa_switch *ds) return ret; } - ret = rtl8366_reset_vlan(priv); + ret = rtl8366rb_reset_vlan(priv); if (ret) return ret; @@ -1419,14 +1421,32 @@ static int rtl8366rb_max_mtu(struct dsa_switch *ds, int port) return 16000 - VLAN_ETH_HLEN - ETH_FCS_LEN; } +/* + * struct rtl8366rb_vlan_mc - Virtual LAN member configuration + */ +struct rtl8366rb_vlan_mc { + u16 vid; + u16 untag; + u16 member; + u8 fid; + u8 priority; +}; + +struct rtl8366rb_vlan_4k { + u16 vid; + u16 untag; + u16 member; + u8 fid; +}; + static int rtl8366rb_get_vlan_4k(struct realtek_priv *priv, u32 vid, - struct rtl8366_vlan_4k *vlan4k) + struct rtl8366rb_vlan_4k *vlan4k) { u32 data[3]; int ret; int i; - memset(vlan4k, '\0', sizeof(struct rtl8366_vlan_4k)); + memset(vlan4k, '\0', sizeof(struct rtl8366rb_vlan_4k)); if (vid >= RTL8366RB_NUM_VIDS) return -EINVAL; @@ -1461,7 +1481,7 @@ static int rtl8366rb_get_vlan_4k(struct realtek_priv *priv, u32 vid, } static int rtl8366rb_set_vlan_4k(struct realtek_priv *priv, - const struct rtl8366_vlan_4k *vlan4k) + const struct rtl8366rb_vlan_4k *vlan4k) { u32 data[3]; int ret; @@ -1495,13 +1515,13 @@ static int rtl8366rb_set_vlan_4k(struct realtek_priv *priv, } static int rtl8366rb_get_vlan_mc(struct realtek_priv *priv, u32 index, - struct rtl8366_vlan_mc *vlanmc) + struct rtl8366rb_vlan_mc *vlanmc) { u32 data[3]; int ret; int i; - memset(vlanmc, '\0', sizeof(struct rtl8366_vlan_mc)); + memset(vlanmc, '\0', sizeof(struct rtl8366rb_vlan_mc)); if (index >= RTL8366RB_NUM_VLANS) return -EINVAL; @@ -1526,7 +1546,7 @@ static int rtl8366rb_get_vlan_mc(struct realtek_priv *priv, u32 index, } static int rtl8366rb_set_vlan_mc(struct realtek_priv *priv, u32 index, - const struct rtl8366_vlan_mc *vlanmc) + const struct rtl8366rb_vlan_mc *vlanmc) { u32 data[3]; int ret; @@ -1613,9 +1633,10 @@ static int rtl8366rb_set_mc_index(struct realtek_priv *priv, int port, int index static bool rtl8366rb_is_vlan_valid(struct realtek_priv *priv, unsigned int vlan) { + struct rtl8366rb *rb = priv->chip_data; unsigned int max = RTL8366RB_NUM_VLANS - 1; - if (priv->vlan4k_enabled) + if (rb->vlan4k_enabled) max = RTL8366RB_NUM_VIDS - 1; if (vlan > max) @@ -1640,6 +1661,374 @@ static int rtl8366rb_enable_vlan4k(struct realtek_priv *priv, bool enable) enable ? RTL8366RB_SGCR_EN_VLAN_4KTB : 0); } +static int rtl8366rb_mc_is_used(struct realtek_priv *priv, int mc_index, int *used) +{ + int ret; + int i; + + *used = 0; + for (i = 0; i < priv->num_ports; i++) { + int index = 0; + + ret = rtl8366rb_get_mc_index(priv, i, &index); + if (ret) + return ret; + + if (mc_index == index) { + *used = 1; + break; + } + } + + return 0; +} + +/** + * rtl8366rb_obtain_mc() - retrieve or allocate a VLAN member configuration + * @priv: the Realtek SMI device instance + * @vid: the VLAN ID to look up or allocate + * @vlanmc: member configuration populated on success + * + * Return: member configuration index or a negative error number + */ +static int rtl8366rb_obtain_mc(struct realtek_priv *priv, int vid, + struct rtl8366rb_vlan_mc *vlanmc) +{ + struct rtl8366rb_vlan_4k vlan4k; + int ret; + int i; + + /* Try to find an existing member config entry for this VID */ + for (i = 0; i < RTL8366RB_NUM_VLANS; i++) { + ret = rtl8366rb_get_vlan_mc(priv, i, vlanmc); + if (ret) { + dev_err(priv->dev, "error searching for VLAN MC %d for VID %d\n", + i, vid); + return ret; + } + + if (vid == vlanmc->vid) + return i; + } + + /* We have no MC entry for this VID, try to find an empty one */ + for (i = 0; i < RTL8366RB_NUM_VLANS; i++) { + ret = rtl8366rb_get_vlan_mc(priv, i, vlanmc); + if (ret) { + dev_err(priv->dev, "error searching for VLAN MC %d for VID %d\n", + i, vid); + return ret; + } + + if (vlanmc->vid == 0 && vlanmc->member == 0) { + /* Update the entry from the 4K table */ + ret = rtl8366rb_get_vlan_4k(priv, vid, &vlan4k); + if (ret) { + dev_err(priv->dev, "error looking for 4K VLAN MC %d for VID %d\n", + i, vid); + return ret; + } + + vlanmc->vid = vid; + vlanmc->member = vlan4k.member; + vlanmc->untag = vlan4k.untag; + vlanmc->fid = vlan4k.fid; + ret = rtl8366rb_set_vlan_mc(priv, i, vlanmc); + if (ret) { + dev_err(priv->dev, "unable to set/update VLAN MC %d for VID %d\n", + i, vid); + return ret; + } + + dev_dbg(priv->dev, "created new MC at index %d for VID %d\n", + i, vid); + return i; + } + } + + /* MC table is full, try to find an unused entry and replace it */ + for (i = 0; i < RTL8366RB_NUM_VLANS; i++) { + int used; + + ret = rtl8366rb_mc_is_used(priv, i, &used); + if (ret) + return ret; + + if (!used) { + /* Update the entry from the 4K table */ + ret = rtl8366rb_get_vlan_4k(priv, vid, &vlan4k); + if (ret) + return ret; + + vlanmc->vid = vid; + vlanmc->member = vlan4k.member; + vlanmc->untag = vlan4k.untag; + vlanmc->fid = vlan4k.fid; + ret = rtl8366rb_set_vlan_mc(priv, i, vlanmc); + if (ret) { + dev_err(priv->dev, "unable to set/update VLAN MC %d for VID %d\n", + i, vid); + return ret; + } + dev_dbg(priv->dev, "recycled MC at index %i for VID %d\n", + i, vid); + return i; + } + } + + dev_err(priv->dev, "all VLAN member configurations are in use\n"); + return -ENOSPC; +} + +static int rtl8366rb_set_vlan(struct realtek_priv *priv, int vid, u32 member, + u32 untag, u32 fid) +{ + struct rtl8366rb_vlan_mc vlanmc; + struct rtl8366rb_vlan_4k vlan4k; + int mc; + int ret; + + if (!rtl8366rb_is_vlan_valid(priv, vid)) + return -EINVAL; + + dev_dbg(priv->dev, + "setting VLAN%d 4k members: 0x%02x, untagged: 0x%02x\n", + vid, member, untag); + + /* Update the 4K table */ + ret = rtl8366rb_get_vlan_4k(priv, vid, &vlan4k); + if (ret) + return ret; + + vlan4k.member |= member; + vlan4k.untag |= untag; + vlan4k.fid = fid; + ret = rtl8366rb_set_vlan_4k(priv, &vlan4k); + if (ret) + return ret; + + dev_dbg(priv->dev, + "resulting VLAN%d 4k members: 0x%02x, untagged: 0x%02x\n", + vid, vlan4k.member, vlan4k.untag); + + /* Find or allocate a member config for this VID */ + ret = rtl8366rb_obtain_mc(priv, vid, &vlanmc); + if (ret < 0) + return ret; + mc = ret; + + /* Update the MC entry */ + vlanmc.member |= member; + vlanmc.untag |= untag; + vlanmc.fid = fid; + + /* Commit updates to the MC entry */ + ret = rtl8366rb_set_vlan_mc(priv, mc, &vlanmc); + if (ret) + dev_err(priv->dev, "failed to commit changes to VLAN MC index %d for VID %d\n", + mc, vid); + else + dev_dbg(priv->dev, + "resulting VLAN%d MC members: 0x%02x, untagged: 0x%02x\n", + vid, vlanmc.member, vlanmc.untag); + + return ret; +} + +static int rtl8366rb_set_pvid(struct realtek_priv *priv, unsigned int port, + unsigned int vid) +{ + struct rtl8366rb_vlan_mc vlanmc; + int mc; + int ret; + + if (!rtl8366rb_is_vlan_valid(priv, vid)) + return -EINVAL; + + /* Find or allocate a member config for this VID */ + ret = rtl8366rb_obtain_mc(priv, vid, &vlanmc); + if (ret < 0) + return ret; + mc = ret; + + ret = rtl8366rb_set_mc_index(priv, port, mc); + if (ret) { + dev_err(priv->dev, "set PVID: failed to set MC index %d for port %d\n", + mc, port); + return ret; + } + + dev_dbg(priv->dev, "set PVID: the PVID for port %d set to %d using existing MC index %d\n", + port, vid, mc); + + return 0; +} + +static int rtl8366rb_set_vlan4k_enabled(struct realtek_priv *priv, bool enable) +{ + struct rtl8366rb *rb = priv->chip_data; + int ret; + + /* To enable 4k VLAN, ordinary VLAN must be enabled first, + * but if we disable 4k VLAN it is fine to leave ordinary + * VLAN enabled. + */ + if (enable) { + /* Make sure VLAN is ON */ + ret = rtl8366rb_enable_vlan(priv, true); + if (ret) + return ret; + } + + ret = rtl8366rb_enable_vlan4k(priv, enable); + if (ret) + return ret; + + rb->vlan4k_enabled = enable; + return 0; +} + +static int rtl8366rb_set_vlan_enabled(struct realtek_priv *priv, bool enable) +{ + struct rtl8366rb *rb = priv->chip_data; + int ret; + + ret = rtl8366rb_enable_vlan(priv, enable); + if (ret) + return ret; + + /* If we turn VLAN off, make sure that we turn off + * 4k VLAN as well, if that happened to be on. + */ + if (!enable) { + rb->vlan4k_enabled = false; + ret = rtl8366rb_enable_vlan4k(priv, false); + } + + return ret; +} + +static int rtl8366rb_reset_vlan(struct realtek_priv *priv) +{ + struct rtl8366rb_vlan_mc vlanmc; + int ret; + int i; + + rtl8366rb_set_vlan_enabled(priv, false); + rtl8366rb_set_vlan4k_enabled(priv, false); + + /* Clear the 16 VLAN member configurations */ + vlanmc.vid = 0; + vlanmc.priority = 0; + vlanmc.member = 0; + vlanmc.untag = 0; + vlanmc.fid = 0; + for (i = 0; i < RTL8366RB_NUM_VLANS; i++) { + ret = rtl8366rb_set_vlan_mc(priv, i, &vlanmc); + if (ret) + return ret; + } + + return 0; +} + +static int rtl8366rb_vlan_add(struct dsa_switch *ds, int port, + const struct switchdev_obj_port_vlan *vlan, + struct netlink_ext_ack *extack) +{ + bool untagged = !!(vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED); + bool pvid = !!(vlan->flags & BRIDGE_VLAN_INFO_PVID); + struct realtek_priv *priv = ds->priv; + u32 member = 0; + u32 untag = 0; + int ret; + + if (!rtl8366rb_is_vlan_valid(priv, vlan->vid)) { + NL_SET_ERR_MSG_MOD(extack, "VLAN ID not valid"); + return -EINVAL; + } + + /* Enable VLAN in the hardware + * FIXME: what's with this 4k business? + * Just enabling ordinary VLAN mode seems inconclusive. + */ + ret = rtl8366rb_set_vlan4k_enabled(priv, true); + if (ret) { + NL_SET_ERR_MSG_MOD(extack, "Failed to enable VLAN 4K"); + return ret; + } + + dev_dbg(priv->dev, "add VLAN %d on port %d, %s, %s\n", + vlan->vid, port, untagged ? "untagged" : "tagged", + pvid ? "PVID" : "no PVID"); + + member |= BIT(port); + + if (untagged) + untag |= BIT(port); + + ret = rtl8366rb_set_vlan(priv, vlan->vid, member, untag, 0); + if (ret) { + dev_err(priv->dev, "failed to set up VLAN %04x", vlan->vid); + return ret; + } + + if (!pvid) + return 0; + + ret = rtl8366rb_set_pvid(priv, port, vlan->vid); + if (ret) { + dev_err(priv->dev, "failed to set PVID on port %d to VLAN %04x", + port, vlan->vid); + return ret; + } + + return 0; +} + +static int rtl8366rb_vlan_del(struct dsa_switch *ds, int port, + const struct switchdev_obj_port_vlan *vlan) +{ + struct realtek_priv *priv = ds->priv; + int ret, i; + + dev_dbg(priv->dev, "del VLAN %d on port %d\n", vlan->vid, port); + + for (i = 0; i < RTL8366RB_NUM_VLANS; i++) { + struct rtl8366rb_vlan_mc vlanmc; + + ret = rtl8366rb_get_vlan_mc(priv, i, &vlanmc); + if (ret) + return ret; + + if (vlan->vid == vlanmc.vid) { + /* Remove this port from the VLAN */ + vlanmc.member &= ~BIT(port); + vlanmc.untag &= ~BIT(port); + /* + * If no ports are members of this VLAN + * anymore then clear the whole member + * config so it can be reused. + */ + if (!vlanmc.member) { + vlanmc.vid = 0; + vlanmc.priority = 0; + vlanmc.fid = 0; + } + ret = rtl8366rb_set_vlan_mc(priv, i, &vlanmc); + if (ret) { + dev_err(priv->dev, + "failed to remove VLAN %04x\n", + vlan->vid); + return ret; + } + break; + } + } + + return 0; +} + static int rtl8366rb_phy_read(struct realtek_priv *priv, int phy, int regnum) { u32 val; @@ -1761,7 +2150,6 @@ static int rtl8366rb_detect(struct realtek_priv *priv) dev_info(dev, "found an RTL8366RB switch\n"); priv->cpu_port = RTL8366RB_PORT_NUM_CPU; priv->num_ports = RTL8366RB_NUM_PORTS; - priv->num_vlan_mc = RTL8366RB_NUM_VLANS; priv->mib_counters = rtl8366rb_mib_counters; priv->num_mib_counters = ARRAY_SIZE(rtl8366rb_mib_counters); break; @@ -1794,8 +2182,8 @@ static const struct dsa_switch_ops rtl8366rb_switch_ops = { .port_bridge_join = rtl83xx_port_bridge_join, .port_bridge_leave = rtl83xx_port_bridge_leave, .port_vlan_filtering = rtl8366rb_vlan_filtering, - .port_vlan_add = rtl8366_vlan_add, - .port_vlan_del = rtl8366_vlan_del, + .port_vlan_add = rtl8366rb_vlan_add, + .port_vlan_del = rtl8366rb_vlan_del, .port_enable = rtl8366rb_port_enable, .port_disable = rtl8366rb_port_disable, .port_pre_bridge_flags = rtl8366rb_port_pre_bridge_flags, @@ -1810,16 +2198,7 @@ static const struct dsa_switch_ops rtl8366rb_switch_ops = { static const struct realtek_ops rtl8366rb_ops = { .detect = rtl8366rb_detect, - .get_vlan_mc = rtl8366rb_get_vlan_mc, - .set_vlan_mc = rtl8366rb_set_vlan_mc, - .get_vlan_4k = rtl8366rb_get_vlan_4k, - .set_vlan_4k = rtl8366rb_set_vlan_4k, - .get_mc_index = rtl8366rb_get_mc_index, - .set_mc_index = rtl8366rb_set_mc_index, .get_mib_counter = rtl8366rb_get_mib_counter, - .is_vlan_valid = rtl8366rb_is_vlan_valid, - .enable_vlan = rtl8366rb_enable_vlan, - .enable_vlan4k = rtl8366rb_enable_vlan4k, .port_add_isolation = rtl8366rb_port_add_isolation, .port_remove_isolation = rtl8366rb_port_remove_isolation, .port_set_learning = rtl8366rb_port_set_learning, diff --git a/drivers/net/dsa/realtek/rtl8366rb.h b/drivers/net/dsa/realtek/rtl8366rb.h index 685ff3275faa..2d7637029dca 100644 --- a/drivers/net/dsa/realtek/rtl8366rb.h +++ b/drivers/net/dsa/realtek/rtl8366rb.h @@ -89,11 +89,13 @@ static inline int rtl8366rb_setup_leds(struct realtek_priv *priv) * struct rtl8366rb - RTL8366RB-specific data * @max_mtu: per-port max MTU setting * @pvid_enabled: if PVID is set for respective port + * @vlan4k_enabled: whether 4K VLAN mode is enabled * @leds: per-port and per-ledgroup led info */ struct rtl8366rb { unsigned int max_mtu[RTL8366RB_NUM_PORTS]; bool pvid_enabled[RTL8366RB_NUM_PORTS]; + bool vlan4k_enabled; #if IS_ENABLED(CONFIG_NET_DSA_REALTEK_RTL8366RB_LEDS) struct rtl8366rb_led leds[RTL8366RB_NUM_PORTS][RTL8366RB_NUM_LEDGROUPS]; #endif --- base-commit: fe4597b13a535ebf999053d2ed9993ec534be192 change-id: 20261202-rtl8366rb-cleanup-40310858bd4e Best regards, -- Linus Walleij <linu…@kernel.org>