1 // SPDX-License-Identifier: GPL-2.0
2 /* Realtek SMI library helpers for the RTL8366x variants
3 * RTL8366RB and RTL8366S
11 #include <linux/if_bridge.h>
16 int rtl8366_mc_is_used(struct realtek_priv *priv, int mc_index, int *used)
22 for (i = 0; i < priv->num_ports; i++) {
25 ret = priv->ops->get_mc_index(priv, i, &index);
29 if (mc_index == index) {
37 EXPORT_SYMBOL_GPL(rtl8366_mc_is_used);
40 * rtl8366_obtain_mc() - retrieve or allocate a VLAN member configuration
41 * @priv: the Realtek SMI device instance
42 * @vid: the VLAN ID to look up or allocate
43 * @vlanmc: the pointer will be assigned to a pointer to a valid member config
45 * @return: index of a new member config or negative error number
47 static int rtl8366_obtain_mc(struct realtek_priv *priv, int vid,
48 struct rtl8366_vlan_mc *vlanmc)
50 struct rtl8366_vlan_4k vlan4k;
54 /* Try to find an existing member config entry for this VID */
55 for (i = 0; i < priv->num_vlan_mc; i++) {
56 ret = priv->ops->get_vlan_mc(priv, i, vlanmc);
58 dev_err(priv->dev, "error searching for VLAN MC %d for VID %d\n",
63 if (vid == vlanmc->vid)
67 /* We have no MC entry for this VID, try to find an empty one */
68 for (i = 0; i < priv->num_vlan_mc; i++) {
69 ret = priv->ops->get_vlan_mc(priv, i, vlanmc);
71 dev_err(priv->dev, "error searching for VLAN MC %d for VID %d\n",
76 if (vlanmc->vid == 0 && vlanmc->member == 0) {
77 /* Update the entry from the 4K table */
78 ret = priv->ops->get_vlan_4k(priv, vid, &vlan4k);
80 dev_err(priv->dev, "error looking for 4K VLAN MC %d for VID %d\n",
86 vlanmc->member = vlan4k.member;
87 vlanmc->untag = vlan4k.untag;
88 vlanmc->fid = vlan4k.fid;
89 ret = priv->ops->set_vlan_mc(priv, i, vlanmc);
91 dev_err(priv->dev, "unable to set/update VLAN MC %d for VID %d\n",
96 dev_dbg(priv->dev, "created new MC at index %d for VID %d\n",
102 /* MC table is full, try to find an unused entry and replace it */
103 for (i = 0; i < priv->num_vlan_mc; i++) {
106 ret = rtl8366_mc_is_used(priv, i, &used);
111 /* Update the entry from the 4K table */
112 ret = priv->ops->get_vlan_4k(priv, vid, &vlan4k);
117 vlanmc->member = vlan4k.member;
118 vlanmc->untag = vlan4k.untag;
119 vlanmc->fid = vlan4k.fid;
120 ret = priv->ops->set_vlan_mc(priv, i, vlanmc);
122 dev_err(priv->dev, "unable to set/update VLAN MC %d for VID %d\n",
126 dev_dbg(priv->dev, "recycled MC at index %i for VID %d\n",
132 dev_err(priv->dev, "all VLAN member configurations are in use\n");
136 int rtl8366_set_vlan(struct realtek_priv *priv, int vid, u32 member,
139 struct rtl8366_vlan_mc vlanmc;
140 struct rtl8366_vlan_4k vlan4k;
144 if (!priv->ops->is_vlan_valid(priv, vid))
148 "setting VLAN%d 4k members: 0x%02x, untagged: 0x%02x\n",
151 /* Update the 4K table */
152 ret = priv->ops->get_vlan_4k(priv, vid, &vlan4k);
156 vlan4k.member |= member;
157 vlan4k.untag |= untag;
159 ret = priv->ops->set_vlan_4k(priv, &vlan4k);
164 "resulting VLAN%d 4k members: 0x%02x, untagged: 0x%02x\n",
165 vid, vlan4k.member, vlan4k.untag);
167 /* Find or allocate a member config for this VID */
168 ret = rtl8366_obtain_mc(priv, vid, &vlanmc);
173 /* Update the MC entry */
174 vlanmc.member |= member;
175 vlanmc.untag |= untag;
178 /* Commit updates to the MC entry */
179 ret = priv->ops->set_vlan_mc(priv, mc, &vlanmc);
181 dev_err(priv->dev, "failed to commit changes to VLAN MC index %d for VID %d\n",
185 "resulting VLAN%d MC members: 0x%02x, untagged: 0x%02x\n",
186 vid, vlanmc.member, vlanmc.untag);
190 EXPORT_SYMBOL_GPL(rtl8366_set_vlan);
192 int rtl8366_set_pvid(struct realtek_priv *priv, unsigned int port,
195 struct rtl8366_vlan_mc vlanmc;
199 if (!priv->ops->is_vlan_valid(priv, vid))
202 /* Find or allocate a member config for this VID */
203 ret = rtl8366_obtain_mc(priv, vid, &vlanmc);
208 ret = priv->ops->set_mc_index(priv, port, mc);
210 dev_err(priv->dev, "set PVID: failed to set MC index %d for port %d\n",
215 dev_dbg(priv->dev, "set PVID: the PVID for port %d set to %d using existing MC index %d\n",
220 EXPORT_SYMBOL_GPL(rtl8366_set_pvid);
222 int rtl8366_enable_vlan4k(struct realtek_priv *priv, bool enable)
226 /* To enable 4k VLAN, ordinary VLAN must be enabled first,
227 * but if we disable 4k VLAN it is fine to leave ordinary
231 /* Make sure VLAN is ON */
232 ret = priv->ops->enable_vlan(priv, true);
236 priv->vlan_enabled = true;
239 ret = priv->ops->enable_vlan4k(priv, enable);
243 priv->vlan4k_enabled = enable;
246 EXPORT_SYMBOL_GPL(rtl8366_enable_vlan4k);
248 int rtl8366_enable_vlan(struct realtek_priv *priv, bool enable)
252 ret = priv->ops->enable_vlan(priv, enable);
256 priv->vlan_enabled = enable;
258 /* If we turn VLAN off, make sure that we turn off
259 * 4k VLAN as well, if that happened to be on.
262 priv->vlan4k_enabled = false;
263 ret = priv->ops->enable_vlan4k(priv, false);
268 EXPORT_SYMBOL_GPL(rtl8366_enable_vlan);
270 int rtl8366_reset_vlan(struct realtek_priv *priv)
272 struct rtl8366_vlan_mc vlanmc;
276 rtl8366_enable_vlan(priv, false);
277 rtl8366_enable_vlan4k(priv, false);
279 /* Clear the 16 VLAN member configurations */
285 for (i = 0; i < priv->num_vlan_mc; i++) {
286 ret = priv->ops->set_vlan_mc(priv, i, &vlanmc);
293 EXPORT_SYMBOL_GPL(rtl8366_reset_vlan);
295 int rtl8366_vlan_add(struct dsa_switch *ds, int port,
296 const struct switchdev_obj_port_vlan *vlan,
297 struct netlink_ext_ack *extack)
299 bool untagged = !!(vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED);
300 bool pvid = !!(vlan->flags & BRIDGE_VLAN_INFO_PVID);
301 struct realtek_priv *priv = ds->priv;
306 if (!priv->ops->is_vlan_valid(priv, vlan->vid)) {
307 NL_SET_ERR_MSG_MOD(extack, "VLAN ID not valid");
311 /* Enable VLAN in the hardware
312 * FIXME: what's with this 4k business?
313 * Just rtl8366_enable_vlan() seems inconclusive.
315 ret = rtl8366_enable_vlan4k(priv, true);
317 NL_SET_ERR_MSG_MOD(extack, "Failed to enable VLAN 4K");
321 dev_dbg(priv->dev, "add VLAN %d on port %d, %s, %s\n",
322 vlan->vid, port, untagged ? "untagged" : "tagged",
323 pvid ? "PVID" : "no PVID");
330 ret = rtl8366_set_vlan(priv, vlan->vid, member, untag, 0);
332 dev_err(priv->dev, "failed to set up VLAN %04x", vlan->vid);
339 ret = rtl8366_set_pvid(priv, port, vlan->vid);
341 dev_err(priv->dev, "failed to set PVID on port %d to VLAN %04x",
348 EXPORT_SYMBOL_GPL(rtl8366_vlan_add);
350 int rtl8366_vlan_del(struct dsa_switch *ds, int port,
351 const struct switchdev_obj_port_vlan *vlan)
353 struct realtek_priv *priv = ds->priv;
356 dev_dbg(priv->dev, "del VLAN %d on port %d\n", vlan->vid, port);
358 for (i = 0; i < priv->num_vlan_mc; i++) {
359 struct rtl8366_vlan_mc vlanmc;
361 ret = priv->ops->get_vlan_mc(priv, i, &vlanmc);
365 if (vlan->vid == vlanmc.vid) {
366 /* Remove this port from the VLAN */
367 vlanmc.member &= ~BIT(port);
368 vlanmc.untag &= ~BIT(port);
370 * If no ports are members of this VLAN
371 * anymore then clear the whole member
372 * config so it can be reused.
374 if (!vlanmc.member) {
379 ret = priv->ops->set_vlan_mc(priv, i, &vlanmc);
382 "failed to remove VLAN %04x\n",
392 EXPORT_SYMBOL_GPL(rtl8366_vlan_del);
394 void rtl8366_get_strings(struct dsa_switch *ds, int port, u32 stringset,
397 struct realtek_priv *priv = ds->priv;
398 struct rtl8366_mib_counter *mib;
401 if (port >= priv->num_ports)
404 for (i = 0; i < priv->num_mib_counters; i++) {
405 mib = &priv->mib_counters[i];
406 strncpy(data + i * ETH_GSTRING_LEN,
407 mib->name, ETH_GSTRING_LEN);
410 EXPORT_SYMBOL_GPL(rtl8366_get_strings);
412 int rtl8366_get_sset_count(struct dsa_switch *ds, int port, int sset)
414 struct realtek_priv *priv = ds->priv;
416 /* We only support SS_STATS */
417 if (sset != ETH_SS_STATS)
419 if (port >= priv->num_ports)
422 return priv->num_mib_counters;
424 EXPORT_SYMBOL_GPL(rtl8366_get_sset_count);
426 void rtl8366_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *data)
428 struct realtek_priv *priv = ds->priv;
432 if (port >= priv->num_ports)
435 for (i = 0; i < priv->num_mib_counters; i++) {
436 struct rtl8366_mib_counter *mib;
439 mib = &priv->mib_counters[i];
440 ret = priv->ops->get_mib_counter(priv, port, mib, &mibvalue);
442 dev_err(priv->dev, "error reading MIB counter %s\n",
448 EXPORT_SYMBOL_GPL(rtl8366_get_ethtool_stats);