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 * Copyright (C) 2008 Patrick Horn. 8 * Copyright (C) 2008, 2010 Martin Mares. 9 * All rights reserved. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * $FreeBSD$ 33 */ 34 35 #include <sys/param.h> 36 #include <sys/bus.h> 37 #include <sys/kernel.h> 38 #include <sys/lock.h> 39 #include <sys/mutex.h> 40 #include <sys/systm.h> 41 #include <sys/socket.h> 42 43 #include <net/if.h> 44 45 #include <dev/mii/mii.h> 46 47 #include <dev/etherswitch/etherswitch.h> 48 #include <dev/etherswitch/ip17x/ip17x_phy.h> 49 #include <dev/etherswitch/ip17x/ip17x_reg.h> 50 #include <dev/etherswitch/ip17x/ip17x_var.h> 51 #include <dev/etherswitch/ip17x/ip17x_vlans.h> 52 #include <dev/etherswitch/ip17x/ip175d.h> 53 54 /* 55 * IP175D specific functions. 56 */ 57 58 /* 59 * Reset the switch to default state. 60 */ 61 static int 62 ip175d_reset(struct ip17x_softc *sc) 63 { 64 65 /* Reset all the switch settings. */ 66 ip17x_writephy(sc->sc_dev, IP175D_RESET_PHY, IP175D_RESET_REG, 0x175d); 67 DELAY(2000); 68 69 /* Disable the special tagging mode. */ 70 ip17x_updatephy(sc->sc_dev, 21, 22, 0x3, 0x0); 71 72 /* Set 802.1q protocol type. */ 73 ip17x_writephy(sc->sc_dev, 22, 3, 0x8100); 74 75 return (0); 76 } 77 78 /* 79 * Set the Switch configuration. 80 */ 81 static int 82 ip175d_hw_setup(struct ip17x_softc *sc) 83 { 84 struct ip17x_vlan *v; 85 uint32_t ports[IP17X_MAX_VLANS]; 86 uint32_t addtag[IP17X_MAX_VLANS]; 87 uint32_t striptag[IP17X_MAX_VLANS]; 88 uint32_t vlan_mask; 89 int i, j; 90 91 vlan_mask = 0; 92 for (i = 0; i < IP17X_MAX_VLANS; i++) { 93 94 ports[i] = 0; 95 addtag[i] = 0; 96 striptag[i] = 0; 97 98 v = &sc->vlan[i]; 99 if ((v->vlanid & ETHERSWITCH_VID_VALID) == 0 || 100 sc->vlan_mode == 0) { 101 /* Vlangroup disabled. Reset the filter. */ 102 ip17x_writephy(sc->sc_dev, 22, 14 + i, i + 1); 103 ports[i] = 0x3f; 104 continue; 105 } 106 107 vlan_mask |= (1 << i); 108 ports[i] = v->ports; 109 110 /* Setup the filter, write the VLAN id. */ 111 ip17x_writephy(sc->sc_dev, 22, 14 + i, 112 v->vlanid & ETHERSWITCH_VID_MASK); 113 114 for (j = 0; j < MII_NPHY; j++) { 115 if ((ports[i] & (1 << j)) == 0) 116 continue; 117 if (sc->addtag & (1 << j)) 118 addtag[i] |= (1 << j); 119 if (sc->striptag & (1 << j)) 120 striptag[i] |= (1 << j); 121 } 122 } 123 124 /* Write the port masks, tag adds and removals. */ 125 for (i = 0; i < IP17X_MAX_VLANS / 2; i++) { 126 ip17x_writephy(sc->sc_dev, 23, i, 127 ports[2 * i] | (ports[2 * i + 1] << 8)); 128 ip17x_writephy(sc->sc_dev, 23, i + 8, 129 addtag[2 * i] | (addtag[2 * i + 1] << 8)); 130 ip17x_writephy(sc->sc_dev, 23, i + 16, 131 striptag[2 * i] | (striptag[2 * i + 1] << 8)); 132 } 133 134 /* Write the in use vlan mask. */ 135 ip17x_writephy(sc->sc_dev, 22, 10, vlan_mask); 136 137 /* Write the PVID of each port. */ 138 for (i = 0; i < sc->numports; i++) 139 ip17x_writephy(sc->sc_dev, 22, 4 + i, sc->pvid[i]); 140 141 return (0); 142 } 143 144 /* 145 * Set the switch VLAN mode. 146 */ 147 static int 148 ip175d_set_vlan_mode(struct ip17x_softc *sc, uint32_t mode) 149 { 150 151 switch (mode) { 152 case ETHERSWITCH_VLAN_DOT1Q: 153 /* 154 * VLAN classification rules: tag-based VLANs, 155 * use VID to classify, drop packets that cannot 156 * be classified. 157 */ 158 ip17x_updatephy(sc->sc_dev, 22, 0, 0x3fff, 0x003f); 159 sc->vlan_mode = mode; 160 break; 161 case ETHERSWITCH_VLAN_PORT: 162 sc->vlan_mode = mode; 163 /* fallthrough */ 164 default: 165 /* 166 * VLAN classification rules: everything off & 167 * clear table. 168 */ 169 ip17x_updatephy(sc->sc_dev, 22, 0, 0xbfff, 0x8000); 170 sc->vlan_mode = 0; 171 break; 172 } 173 174 if (sc->vlan_mode != 0) { 175 /* 176 * Ingress rules: CFI=1 dropped, null VID is untagged, VID=1 passed, 177 * VID=0xfff discarded, admin both tagged and untagged, ingress 178 * filters enabled. 179 */ 180 ip17x_updatephy(sc->sc_dev, 22, 1, 0x0fff, 0x0c3f); 181 182 /* Egress rules: IGMP processing off, keep VLAN header off. */ 183 ip17x_updatephy(sc->sc_dev, 22, 2, 0x0fff, 0x0000); 184 } else { 185 ip17x_updatephy(sc->sc_dev, 22, 1, 0x0fff, 0x043f); 186 ip17x_updatephy(sc->sc_dev, 22, 2, 0x0fff, 0x0020); 187 } 188 189 /* Reset vlans. */ 190 ip17x_reset_vlans(sc, sc->vlan_mode); 191 192 /* Update switch configuration. */ 193 ip175d_hw_setup(sc); 194 195 return (0); 196 } 197 198 /* 199 * Get the switch VLAN mode. 200 */ 201 static int 202 ip175d_get_vlan_mode(struct ip17x_softc *sc) 203 { 204 205 return (sc->vlan_mode); 206 } 207 208 void 209 ip175d_attach(struct ip17x_softc *sc) 210 { 211 212 sc->hal.ip17x_reset = ip175d_reset; 213 sc->hal.ip17x_hw_setup = ip175d_hw_setup; 214 sc->hal.ip17x_get_vlan_mode = ip175d_get_vlan_mode; 215 sc->hal.ip17x_set_vlan_mode = ip175d_set_vlan_mode; 216 217 /* Defaults for IP175C. */ 218 sc->cpuport = IP175X_CPU_PORT; 219 sc->numports = IP175X_NUM_PORTS; 220 sc->info.es_vlan_caps = ETHERSWITCH_VLAN_DOT1Q; 221 222 device_printf(sc->sc_dev, "type: IP175D\n"); 223 } 224