xref: /freebsd/sys/dev/etherswitch/ip17x/ip17x_vlans.c (revision e796cc77c586c2955b2f3940dbf4991b31e8d289)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2013 Luiz Otavio O Souza.
5  * Copyright (c) 2011-2012 Stefan Bethke.
6  * Copyright (c) 2012 Adrian Chadd.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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
28  * SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  */
32 
33 #include <sys/param.h>
34 #include <sys/bus.h>
35 #include <sys/errno.h>
36 #include <sys/kernel.h>
37 #include <sys/lock.h>
38 #include <sys/mutex.h>
39 #include <sys/systm.h>
40 #include <sys/socket.h>
41 
42 #include <net/if.h>
43 
44 #include <dev/mii/mii.h>
45 
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>
52 
53 #include "mdio_if.h"
54 #include "miibus_if.h"
55 #include "etherswitch_if.h"
56 
57 /*
58  * Reset vlans to default state.
59  */
60 int
61 ip17x_reset_vlans(struct ip17x_softc *sc, uint32_t vlan_mode)
62 {
63 	struct ip17x_vlan *v;
64 	int i, j, phy;
65 
66 	/* Do not add or strip vlan tags on any port. */
67 	sc->addtag = 0;
68 	sc->striptag = 0;
69 
70 	/* Reset all vlan data. */
71 	memset(sc->vlan, 0, sizeof(sc->vlan));
72 	memset(sc->pvid, 0, sizeof(uint32_t) * sc->numports);
73 
74 	if (vlan_mode == ETHERSWITCH_VLAN_PORT) {
75 
76 		/* Initialize port based vlans. */
77 		for (i = 0, phy = 0; phy < MII_NPHY; phy++) {
78 			if (((1 << phy) & sc->phymask) == 0)
79 				continue;
80 			v = &sc->vlan[i];
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)
85 					continue;
86 				v->ports |= (1 << j);
87 			}
88 		}
89 
90 	} else if (vlan_mode == ETHERSWITCH_VLAN_DOT1Q) {
91 
92 		/*
93 		 * Setup vlan 1 as PVID for all switch ports.  Add all ports as
94 		 * members of vlan 1.
95 		 */
96 		v = &sc->vlan[0];
97 		v->vlanid = 1 | ETHERSWITCH_VID_VALID;
98 		/* Set PVID to 1 for everyone. */
99 		for (i = 0; i < sc->numports; i++)
100 			sc->pvid[i] = 1;
101 		for (i = 0; i < MII_NPHY; i++) {
102 			if ((sc->phymask & (1 << i)) == 0)
103 				continue;
104 			v->ports |= (1 << i);
105 		}
106 	}
107 
108 	return (0);
109 }
110 
111 int
112 ip17x_getvgroup(device_t dev, etherswitch_vlangroup_t *vg)
113 {
114 	struct ip17x_softc *sc;
115 	uint32_t port;
116 	int i;
117 
118 	sc = device_get_softc(dev);
119 
120 	/* Vlan ID. */
121 	vg->es_vid = sc->vlan[vg->es_vlangroup].vlanid;
122 
123 	/* Member Ports. */
124 	vg->es_member_ports = 0;
125 	for (i = 0; i < MII_NPHY; i++) {
126 		if ((sc->phymask & (1 << i)) == 0)
127 			continue;
128 		if ((sc->vlan[vg->es_vlangroup].ports & (1 << i)) == 0)
129 			continue;
130 		port = sc->phyport[i];
131 		vg->es_member_ports |= (1 << port);
132 	}
133 
134 	/* Not supported. */
135 	vg->es_untagged_ports = vg->es_member_ports;
136 	vg->es_fid = 0;
137 
138 	return (0);
139 }
140 
141 int
142 ip17x_setvgroup(device_t dev, etherswitch_vlangroup_t *vg)
143 {
144 	struct ip17x_softc *sc;
145 	uint32_t phy;
146 	int i;
147 
148 	sc = device_get_softc(dev);
149 
150 	/* Check VLAN mode. */
151 	if (sc->vlan_mode == 0)
152 		return (EINVAL);
153 
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)
157 		return (EINVAL);
158 
159 	/* Vlan ID. */
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))
167 				return (EINVAL);
168 		}
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));
175 		}
176 		sc->vlan[vg->es_vlangroup].vlanid |= ETHERSWITCH_VID_VALID;
177 	}
178 
179 	/* Member Ports. */
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)
183 			continue;
184 		phy = sc->portphy[i];
185 		sc->vlan[vg->es_vlangroup].ports |= (1 << phy);
186 	}
187 
188 	return (sc->hal.ip17x_hw_setup(sc));
189 }
190