2 * Copyright (c) 2013 Luiz Otavio O Souza.
3 * Copyright (c) 2011-2012 Stefan Bethke.
4 * Copyright (c) 2012 Adrian Chadd.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/param.h>
33 #include <sys/errno.h>
34 #include <sys/kernel.h>
36 #include <sys/mutex.h>
37 #include <sys/systm.h>
38 #include <sys/socket.h>
42 #include <dev/mii/mii.h>
44 #include <dev/etherswitch/etherswitch.h>
45 #include <dev/etherswitch/ip17x/ip17x_phy.h>
46 #include <dev/etherswitch/ip17x/ip17x_reg.h>
47 #include <dev/etherswitch/ip17x/ip17x_var.h>
48 #include <dev/etherswitch/ip17x/ip17x_vlans.h>
49 #include <dev/etherswitch/ip17x/ip175c.h>
52 #include "miibus_if.h"
53 #include "etherswitch_if.h"
56 * Reset vlans to default state.
59 ip17x_reset_vlans(struct ip17x_softc *sc, uint32_t vlan_mode)
64 /* Do not add or strip vlan tags on any port. */
68 /* Reset all vlan data. */
69 memset(sc->vlan, 0, sizeof(sc->vlan));
70 memset(sc->pvid, 0, sizeof(uint32_t) * sc->numports);
72 if (vlan_mode == ETHERSWITCH_VLAN_PORT) {
74 /* Initialize port based vlans. */
75 for (i = 0, phy = 0; phy < MII_NPHY; phy++) {
76 if (((1 << phy) & sc->phymask) == 0)
79 v->vlanid = i++ | ETHERSWITCH_VID_VALID;
80 v->ports = (1 << sc->cpuport);
81 for (j = 0; j < MII_NPHY; j++) {
82 if (((1 << j) & sc->phymask) == 0)
88 } else if (vlan_mode == ETHERSWITCH_VLAN_DOT1Q) {
91 * Setup vlan 1 as PVID for all switch ports. Add all ports as
95 v->vlanid = 1 | ETHERSWITCH_VID_VALID;
96 /* Set PVID to 1 for everyone. */
97 for (i = 0; i < sc->numports; i++)
99 for (i = 0; i < MII_NPHY; i++) {
100 if ((sc->phymask & (1 << i)) == 0)
102 v->ports |= (1 << i);
110 ip17x_getvgroup(device_t dev, etherswitch_vlangroup_t *vg)
112 struct ip17x_softc *sc;
116 sc = device_get_softc(dev);
119 vg->es_vid = sc->vlan[vg->es_vlangroup].vlanid;
122 vg->es_member_ports = 0;
123 for (i = 0; i < MII_NPHY; i++) {
124 if ((sc->phymask & (1 << i)) == 0)
126 if ((sc->vlan[vg->es_vlangroup].ports & (1 << i)) == 0)
128 port = sc->phyport[i];
129 vg->es_member_ports |= (1 << port);
133 vg->es_untagged_ports = vg->es_member_ports;
140 ip17x_setvgroup(device_t dev, etherswitch_vlangroup_t *vg)
142 struct ip17x_softc *sc;
146 sc = device_get_softc(dev);
148 /* Check VLAN mode. */
149 if (sc->vlan_mode == 0)
152 /* IP175C don't support VLAN IDs > 15. */
153 if (IP17X_IS_SWITCH(sc, IP175C) &&
154 (vg->es_vid & ETHERSWITCH_VID_MASK) > IP175C_LAST_VLAN)
158 if (sc->vlan_mode == ETHERSWITCH_VLAN_DOT1Q) {
159 for (i = 0; i < sc->info.es_nvlangroups; i++) {
160 /* Is this Vlan ID already set in another vlangroup ? */
161 if (i != vg->es_vlangroup &&
162 sc->vlan[i].vlanid & ETHERSWITCH_VID_VALID &&
163 (sc->vlan[i].vlanid & ETHERSWITCH_VID_MASK) ==
164 (vg->es_vid & ETHERSWITCH_VID_MASK))
167 sc->vlan[vg->es_vlangroup].vlanid = vg->es_vid &
168 ETHERSWITCH_VID_MASK;
169 /* Setting the vlanid to zero disables the vlangroup. */
170 if (sc->vlan[vg->es_vlangroup].vlanid == 0) {
171 sc->vlan[vg->es_vlangroup].ports = 0;
172 return (sc->hal.ip17x_hw_setup(sc));
174 sc->vlan[vg->es_vlangroup].vlanid |= ETHERSWITCH_VID_VALID;
178 sc->vlan[vg->es_vlangroup].ports = 0;
179 for (i = 0; i < sc->numports; i++) {
180 if ((vg->es_member_ports & (1 << i)) == 0)
182 phy = sc->portphy[i];
183 sc->vlan[vg->es_vlangroup].ports |= (1 << phy);
186 return (sc->hal.ip17x_hw_setup(sc));