1 // SPDX-License-Identifier: GPL-2.0
2 /* Realtek SMI library helpers for the RTL8366x variants
3 * RTL8366RB and RTL8366S
5 * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
6 * Copyright (C) 2009-2010 Gabor Juhos <juhosg@openwrt.org>
7 * Copyright (C) 2010 Antti Seppälä <a.seppala@gmail.com>
8 * Copyright (C) 2010 Roman Yeryomin <roman@advem.lv>
9 * Copyright (C) 2011 Colin Leitner <colin.leitner@googlemail.com>
11 #include <linux/if_bridge.h>
14 #include "realtek-smi-core.h"
16 int rtl8366_mc_is_used(struct realtek_smi *smi, int mc_index, int *used)
22 for (i = 0; i < smi->num_ports; i++) {
25 ret = smi->ops->get_mc_index(smi, 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 * @smi: 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_smi *smi, 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 < smi->num_vlan_mc; i++) {
56 ret = smi->ops->get_vlan_mc(smi, i, vlanmc);
58 dev_err(smi->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 < smi->num_vlan_mc; i++) {
69 ret = smi->ops->get_vlan_mc(smi, i, vlanmc);
71 dev_err(smi->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 = smi->ops->get_vlan_4k(smi, vid, &vlan4k);
80 dev_err(smi->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 = smi->ops->set_vlan_mc(smi, i, vlanmc);
91 dev_err(smi->dev, "unable to set/update VLAN MC %d for VID %d\n",
96 dev_dbg(smi->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 < smi->num_vlan_mc; i++) {
106 ret = rtl8366_mc_is_used(smi, i, &used);
111 /* Update the entry from the 4K table */
112 ret = smi->ops->get_vlan_4k(smi, vid, &vlan4k);
117 vlanmc->member = vlan4k.member;
118 vlanmc->untag = vlan4k.untag;
119 vlanmc->fid = vlan4k.fid;
120 ret = smi->ops->set_vlan_mc(smi, i, vlanmc);
122 dev_err(smi->dev, "unable to set/update VLAN MC %d for VID %d\n",
126 dev_dbg(smi->dev, "recycled MC at index %i for VID %d\n",
132 dev_err(smi->dev, "all VLAN member configurations are in use\n");
136 int rtl8366_set_vlan(struct realtek_smi *smi, int vid, u32 member,
139 struct rtl8366_vlan_mc vlanmc;
140 struct rtl8366_vlan_4k vlan4k;
144 if (!smi->ops->is_vlan_valid(smi, vid))
148 "setting VLAN%d 4k members: 0x%02x, untagged: 0x%02x\n",
151 /* Update the 4K table */
152 ret = smi->ops->get_vlan_4k(smi, vid, &vlan4k);
156 vlan4k.member |= member;
157 vlan4k.untag |= untag;
159 ret = smi->ops->set_vlan_4k(smi, &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(smi, vid, &vlanmc);
173 /* Update the MC entry */
174 vlanmc.member |= member;
175 vlanmc.untag |= untag;
178 /* Commit updates to the MC entry */
179 ret = smi->ops->set_vlan_mc(smi, mc, &vlanmc);
181 dev_err(smi->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_smi *smi, unsigned int port,
195 struct rtl8366_vlan_mc vlanmc;
199 if (!smi->ops->is_vlan_valid(smi, vid))
202 /* Find or allocate a member config for this VID */
203 ret = rtl8366_obtain_mc(smi, vid, &vlanmc);
208 ret = smi->ops->set_mc_index(smi, port, mc);
210 dev_err(smi->dev, "set PVID: failed to set MC index %d for port %d\n",
215 dev_dbg(smi->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_smi *smi, 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 = smi->ops->enable_vlan(smi, true);
236 smi->vlan_enabled = true;
239 ret = smi->ops->enable_vlan4k(smi, enable);
243 smi->vlan4k_enabled = enable;
246 EXPORT_SYMBOL_GPL(rtl8366_enable_vlan4k);
248 int rtl8366_enable_vlan(struct realtek_smi *smi, bool enable)
252 ret = smi->ops->enable_vlan(smi, enable);
256 smi->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 smi->vlan4k_enabled = false;
263 ret = smi->ops->enable_vlan4k(smi, false);
268 EXPORT_SYMBOL_GPL(rtl8366_enable_vlan);
270 int rtl8366_reset_vlan(struct realtek_smi *smi)
272 struct rtl8366_vlan_mc vlanmc;
276 rtl8366_enable_vlan(smi, false);
277 rtl8366_enable_vlan4k(smi, false);
279 /* Clear the 16 VLAN member configurations */
285 for (i = 0; i < smi->num_vlan_mc; i++) {
286 ret = smi->ops->set_vlan_mc(smi, i, &vlanmc);
293 EXPORT_SYMBOL_GPL(rtl8366_reset_vlan);
295 int rtl8366_init_vlan(struct realtek_smi *smi)
300 ret = rtl8366_reset_vlan(smi);
304 /* Loop over the available ports, for each port, associate
305 * it with the VLAN (port+1)
307 for (port = 0; port < smi->num_ports; port++) {
310 if (port == smi->cpu_port)
311 /* For the CPU port, make all ports members of this
314 mask = GENMASK((int)smi->num_ports - 1, 0);
316 /* For all other ports, enable itself plus the
319 mask = BIT(port) | BIT(smi->cpu_port);
321 /* For each port, set the port as member of VLAN (port+1)
322 * and untagged, except for the CPU port: the CPU port (5) is
323 * member of VLAN 6 and so are ALL the other ports as well.
324 * Use filter 0 (no filter).
326 dev_info(smi->dev, "VLAN%d port mask for port %d, %08x\n",
327 (port + 1), port, mask);
328 ret = rtl8366_set_vlan(smi, (port + 1), mask, mask, 0);
332 dev_info(smi->dev, "VLAN%d port %d, PVID set to %d\n",
333 (port + 1), port, (port + 1));
334 ret = rtl8366_set_pvid(smi, port, (port + 1));
339 return rtl8366_enable_vlan(smi, true);
341 EXPORT_SYMBOL_GPL(rtl8366_init_vlan);
343 int rtl8366_vlan_filtering(struct dsa_switch *ds, int port, bool vlan_filtering,
344 struct switchdev_trans *trans)
346 struct realtek_smi *smi = ds->priv;
347 struct rtl8366_vlan_4k vlan4k;
350 /* Use VLAN nr port + 1 since VLAN0 is not valid */
351 if (switchdev_trans_ph_prepare(trans)) {
352 if (!smi->ops->is_vlan_valid(smi, port + 1))
358 dev_info(smi->dev, "%s filtering on port %d\n",
359 vlan_filtering ? "enable" : "disable",
363 * The hardware support filter ID (FID) 0..7, I have no clue how to
364 * support this in the driver when the callback only says on/off.
366 ret = smi->ops->get_vlan_4k(smi, port + 1, &vlan4k);
370 /* Just set the filter to FID 1 for now then */
371 ret = rtl8366_set_vlan(smi, port + 1,
380 EXPORT_SYMBOL_GPL(rtl8366_vlan_filtering);
382 int rtl8366_vlan_prepare(struct dsa_switch *ds, int port,
383 const struct switchdev_obj_port_vlan *vlan)
385 struct realtek_smi *smi = ds->priv;
389 for (vid = vlan->vid_begin; vid < vlan->vid_end; vid++)
390 if (!smi->ops->is_vlan_valid(smi, vid))
393 dev_info(smi->dev, "prepare VLANs %04x..%04x\n",
394 vlan->vid_begin, vlan->vid_end);
396 /* Enable VLAN in the hardware
397 * FIXME: what's with this 4k business?
398 * Just rtl8366_enable_vlan() seems inconclusive.
400 ret = rtl8366_enable_vlan4k(smi, true);
406 EXPORT_SYMBOL_GPL(rtl8366_vlan_prepare);
408 void rtl8366_vlan_add(struct dsa_switch *ds, int port,
409 const struct switchdev_obj_port_vlan *vlan)
411 bool untagged = !!(vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED);
412 bool pvid = !!(vlan->flags & BRIDGE_VLAN_INFO_PVID);
413 struct realtek_smi *smi = ds->priv;
419 for (vid = vlan->vid_begin; vid < vlan->vid_end; vid++)
420 if (!smi->ops->is_vlan_valid(smi, vid))
423 dev_info(smi->dev, "add VLAN %d on port %d, %s, %s\n",
426 untagged ? "untagged" : "tagged",
427 pvid ? " PVID" : "no PVID");
429 if (dsa_is_dsa_port(ds, port) || dsa_is_cpu_port(ds, port))
430 dev_err(smi->dev, "port is DSA or CPU port\n");
432 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
438 ret = rtl8366_set_vlan(smi, vid, member, untag, 0);
441 "failed to set up VLAN %04x",
447 ret = rtl8366_set_pvid(smi, port, vid);
450 "failed to set PVID on port %d to VLAN %04x",
454 dev_dbg(smi->dev, "VLAN add: added VLAN %d with PVID on port %d\n",
458 EXPORT_SYMBOL_GPL(rtl8366_vlan_add);
460 int rtl8366_vlan_del(struct dsa_switch *ds, int port,
461 const struct switchdev_obj_port_vlan *vlan)
463 struct realtek_smi *smi = ds->priv;
467 dev_info(smi->dev, "del VLAN on port %d\n", port);
469 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
472 dev_info(smi->dev, "del VLAN %04x\n", vid);
474 for (i = 0; i < smi->num_vlan_mc; i++) {
475 struct rtl8366_vlan_mc vlanmc;
477 ret = smi->ops->get_vlan_mc(smi, i, &vlanmc);
481 if (vid == vlanmc.vid) {
482 /* Remove this port from the VLAN */
483 vlanmc.member &= ~BIT(port);
484 vlanmc.untag &= ~BIT(port);
486 * If no ports are members of this VLAN
487 * anymore then clear the whole member
488 * config so it can be reused.
490 if (!vlanmc.member && vlanmc.untag) {
495 ret = smi->ops->set_vlan_mc(smi, i, &vlanmc);
498 "failed to remove VLAN %04x\n",
509 EXPORT_SYMBOL_GPL(rtl8366_vlan_del);
511 void rtl8366_get_strings(struct dsa_switch *ds, int port, u32 stringset,
514 struct realtek_smi *smi = ds->priv;
515 struct rtl8366_mib_counter *mib;
518 if (port >= smi->num_ports)
521 for (i = 0; i < smi->num_mib_counters; i++) {
522 mib = &smi->mib_counters[i];
523 strncpy(data + i * ETH_GSTRING_LEN,
524 mib->name, ETH_GSTRING_LEN);
527 EXPORT_SYMBOL_GPL(rtl8366_get_strings);
529 int rtl8366_get_sset_count(struct dsa_switch *ds, int port, int sset)
531 struct realtek_smi *smi = ds->priv;
533 /* We only support SS_STATS */
534 if (sset != ETH_SS_STATS)
536 if (port >= smi->num_ports)
539 return smi->num_mib_counters;
541 EXPORT_SYMBOL_GPL(rtl8366_get_sset_count);
543 void rtl8366_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *data)
545 struct realtek_smi *smi = ds->priv;
549 if (port >= smi->num_ports)
552 for (i = 0; i < smi->num_mib_counters; i++) {
553 struct rtl8366_mib_counter *mib;
556 mib = &smi->mib_counters[i];
557 ret = smi->ops->get_mib_counter(smi, port, mib, &mibvalue);
559 dev_err(smi->dev, "error reading MIB counter %s\n",
565 EXPORT_SYMBOL_GPL(rtl8366_get_ethtool_stats);