ip17x_vlans.c revision 262848
1117395Skan/*- 2117395Skan * Copyright (c) 2013 Luiz Otavio O Souza. 3117395Skan * Copyright (c) 2011-2012 Stefan Bethke. 4117395Skan * Copyright (c) 2012 Adrian Chadd. 5117395Skan * All rights reserved. 6117395Skan * 718334Speter * Redistribution and use in source and binary forms, with or without 8117395Skan * modification, are permitted provided that the following conditions 918334Speter * are met: 1052284Sobrien * 1. Redistributions of source code must retain the above copyright 1190075Sobrien * notice, this list of conditions and the following disclaimer. 1290075Sobrien * 2. Redistributions in binary form must reproduce the above copyright 1352284Sobrien * notice, this list of conditions and the following disclaimer in the 1452284Sobrien * documentation and/or other materials provided with the distribution. 15117395Skan * 16117395Skan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17117395Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18117395Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19117395Skan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20117395Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21117395Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2218334Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2318334Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2418334Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2518334Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2618334Speter * SUCH DAMAGE. 2718334Speter * 2818334Speter * $FreeBSD: stable/10/sys/dev/etherswitch/ip17x/ip17x_vlans.c 262848 2014-03-06 13:15:53Z brueffer $ 2918334Speter */ 3018334Speter 3118334Speter#include <sys/param.h> 3218334Speter#include <sys/bus.h> 3318334Speter#include <sys/errno.h> 3418334Speter#include <sys/kernel.h> 3518334Speter#include <sys/lock.h> 3618334Speter#include <sys/mutex.h> 3718334Speter#include <sys/systm.h> 3818334Speter#include <sys/socket.h> 3950397Sobrien 4050397Sobrien#include <net/if.h> 4150397Sobrien 4250397Sobrien#include <dev/mii/mii.h> 4350397Sobrien 4450397Sobrien#include <dev/etherswitch/etherswitch.h> 4550397Sobrien#include <dev/etherswitch/ip17x/ip17x_phy.h> 4618334Speter#include <dev/etherswitch/ip17x/ip17x_reg.h> 4718334Speter#include <dev/etherswitch/ip17x/ip17x_var.h> 4818334Speter#include <dev/etherswitch/ip17x/ip17x_vlans.h> 4918334Speter#include <dev/etherswitch/ip17x/ip175c.h> 5018334Speter 5118334Speter#include "mdio_if.h" 5218334Speter#include "miibus_if.h" 5318334Speter#include "etherswitch_if.h" 5418334Speter 5518334Speter/* 5618334Speter * Reset vlans to default state. 5718334Speter */ 5818334Speterint 5918334Speterip17x_reset_vlans(struct ip17x_softc *sc, uint32_t vlan_mode) 6018334Speter{ 6150397Sobrien struct ip17x_vlan *v; 6250397Sobrien int i, j, phy; 6350397Sobrien 6450397Sobrien /* Do not add or strip vlan tags on any port. */ 65117395Skan sc->addtag = 0; 66117395Skan sc->striptag = 0; 67117395Skan 68117395Skan /* Reset all vlan data. */ 69117395Skan memset(sc->vlan, 0, sizeof(sc->vlan)); 70117395Skan memset(sc->pvid, 0, sizeof(uint32_t) * sc->numports); 71117395Skan 72132718Skan if (vlan_mode == ETHERSWITCH_VLAN_PORT) { 73 74 /* Initialize port based vlans. */ 75 for (i = 0, phy = 0; phy < MII_NPHY; phy++) { 76 if (((1 << phy) & sc->phymask) == 0) 77 continue; 78 v = &sc->vlan[i]; 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) 83 continue; 84 v->ports |= (1 << j); 85 } 86 } 87 88 } else if (vlan_mode == ETHERSWITCH_VLAN_DOT1Q) { 89 90 /* 91 * Setup vlan 1 as PVID for all switch ports. Add all ports as 92 * members of vlan 1. 93 */ 94 v = &sc->vlan[0]; 95 v->vlanid = 1 | ETHERSWITCH_VID_VALID; 96 /* Set PVID to 1 for everyone. */ 97 for (i = 0; i < sc->numports; i++) 98 sc->pvid[i] = 1; 99 for (i = 0; i < MII_NPHY; i++) { 100 if ((sc->phymask & (1 << i)) == 0) 101 continue; 102 v->ports |= (1 << i); 103 } 104 } 105 106 return (0); 107} 108 109int 110ip17x_getvgroup(device_t dev, etherswitch_vlangroup_t *vg) 111{ 112 struct ip17x_softc *sc; 113 uint32_t port; 114 int i; 115 116 sc = device_get_softc(dev); 117 118 /* Vlan ID. */ 119 vg->es_vid = sc->vlan[vg->es_vlangroup].vlanid; 120 121 /* Member Ports. */ 122 vg->es_member_ports = 0; 123 for (i = 0; i < MII_NPHY; i++) { 124 if ((sc->phymask & (1 << i)) == 0) 125 continue; 126 if ((sc->vlan[vg->es_vlangroup].ports & (1 << i)) == 0) 127 continue; 128 port = sc->phyport[i]; 129 vg->es_member_ports |= (1 << port); 130 } 131 132 /* Not supported. */ 133 vg->es_untagged_ports = vg->es_member_ports; 134 vg->es_fid = 0; 135 136 return (0); 137} 138 139int 140ip17x_setvgroup(device_t dev, etherswitch_vlangroup_t *vg) 141{ 142 struct ip17x_softc *sc; 143 uint32_t phy; 144 int i; 145 146 sc = device_get_softc(dev); 147 148 /* Check VLAN mode. */ 149 if (sc->vlan_mode == 0) 150 return (EINVAL); 151 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) 155 return (EINVAL); 156 157 /* Vlan ID. */ 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)) 165 return (EINVAL); 166 } 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)); 173 } 174 sc->vlan[vg->es_vlangroup].vlanid |= ETHERSWITCH_VID_VALID; 175 } 176 177 /* Member Ports. */ 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) 181 continue; 182 phy = sc->portphy[i]; 183 sc->vlan[vg->es_vlangroup].ports |= (1 << phy); 184 } 185 186 return (sc->hal.ip17x_hw_setup(sc)); 187} 188