2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2013 Luiz Otavio O Souza.
5 * Copyright (c) 2011-2012 Stefan Bethke.
6 * Copyright (c) 2012 Adrian Chadd.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/param.h>
35 #include <sys/errno.h>
36 #include <sys/kernel.h>
38 #include <sys/mutex.h>
39 #include <sys/systm.h>
40 #include <sys/socket.h>
44 #include <dev/mii/mii.h>
46 #include <dev/etherswitch/etherswitch.h>
47 #include <dev/etherswitch/ip17x/ip17x_phy.h>
48 #include <dev/etherswitch/ip17x/ip17x_reg.h>
49 #include <dev/etherswitch/ip17x/ip17x_var.h>
50 #include <dev/etherswitch/ip17x/ip17x_vlans.h>
51 #include <dev/etherswitch/ip17x/ip175c.h>
54 #include "miibus_if.h"
55 #include "etherswitch_if.h"
58 * Reset vlans to default state.
61 ip17x_reset_vlans(struct ip17x_softc *sc, uint32_t vlan_mode)
66 /* Do not add or strip vlan tags on any port. */
70 /* Reset all vlan data. */
71 memset(sc->vlan, 0, sizeof(sc->vlan));
72 memset(sc->pvid, 0, sizeof(uint32_t) * sc->numports);
74 if (vlan_mode == ETHERSWITCH_VLAN_PORT) {
76 /* Initialize port based vlans. */
77 for (i = 0, phy = 0; phy < MII_NPHY; phy++) {
78 if (((1 << phy) & sc->phymask) == 0)
81 v->vlanid = i++ | ETHERSWITCH_VID_VALID;
82 v->ports = (1 << sc->cpuport);
83 for (j = 0; j < MII_NPHY; j++) {
84 if (((1 << j) & sc->phymask) == 0)
90 } else if (vlan_mode == ETHERSWITCH_VLAN_DOT1Q) {
93 * Setup vlan 1 as PVID for all switch ports. Add all ports as
97 v->vlanid = 1 | ETHERSWITCH_VID_VALID;
98 /* Set PVID to 1 for everyone. */
99 for (i = 0; i < sc->numports; i++)
101 for (i = 0; i < MII_NPHY; i++) {
102 if ((sc->phymask & (1 << i)) == 0)
104 v->ports |= (1 << i);
112 ip17x_getvgroup(device_t dev, etherswitch_vlangroup_t *vg)
114 struct ip17x_softc *sc;
118 sc = device_get_softc(dev);
121 vg->es_vid = sc->vlan[vg->es_vlangroup].vlanid;
124 vg->es_member_ports = 0;
125 for (i = 0; i < MII_NPHY; i++) {
126 if ((sc->phymask & (1 << i)) == 0)
128 if ((sc->vlan[vg->es_vlangroup].ports & (1 << i)) == 0)
130 port = sc->phyport[i];
131 vg->es_member_ports |= (1 << port);
135 vg->es_untagged_ports = vg->es_member_ports;
142 ip17x_setvgroup(device_t dev, etherswitch_vlangroup_t *vg)
144 struct ip17x_softc *sc;
148 sc = device_get_softc(dev);
150 /* Check VLAN mode. */
151 if (sc->vlan_mode == 0)
154 /* IP175C don't support VLAN IDs > 15. */
155 if (IP17X_IS_SWITCH(sc, IP175C) &&
156 (vg->es_vid & ETHERSWITCH_VID_MASK) > IP175C_LAST_VLAN)
160 if (sc->vlan_mode == ETHERSWITCH_VLAN_DOT1Q) {
161 for (i = 0; i < sc->info.es_nvlangroups; i++) {
162 /* Is this Vlan ID already set in another vlangroup ? */
163 if (i != vg->es_vlangroup &&
164 sc->vlan[i].vlanid & ETHERSWITCH_VID_VALID &&
165 (sc->vlan[i].vlanid & ETHERSWITCH_VID_MASK) ==
166 (vg->es_vid & ETHERSWITCH_VID_MASK))
169 sc->vlan[vg->es_vlangroup].vlanid = vg->es_vid &
170 ETHERSWITCH_VID_MASK;
171 /* Setting the vlanid to zero disables the vlangroup. */
172 if (sc->vlan[vg->es_vlangroup].vlanid == 0) {
173 sc->vlan[vg->es_vlangroup].ports = 0;
174 return (sc->hal.ip17x_hw_setup(sc));
176 sc->vlan[vg->es_vlangroup].vlanid |= ETHERSWITCH_VID_VALID;
180 sc->vlan[vg->es_vlangroup].ports = 0;
181 for (i = 0; i < sc->numports; i++) {
182 if ((vg->es_member_ports & (1 << i)) == 0)
184 phy = sc->portphy[i];
185 sc->vlan[vg->es_vlangroup].ports |= (1 << phy);
188 return (sc->hal.ip17x_hw_setup(sc));