1 /*- 2 * Copyright (c) 2013 Luiz Otavio O Souza. 3 * Copyright (c) 2011-2012 Stefan Bethke. 4 * Copyright (c) 2012 Adrian Chadd. 5 * Copyright (C) 2008 Patrick Horn. 6 * Copyright (C) 2008, 2010 Martin Mares. 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/kernel.h> 36 #include <sys/lock.h> 37 #include <sys/mutex.h> 38 #include <sys/systm.h> 39 #include <sys/socket.h> 40 41 #include <net/if.h> 42 43 #include <dev/mii/mii.h> 44 45 #include <dev/etherswitch/etherswitch.h> 46 #include <dev/etherswitch/ip17x/ip17x_phy.h> 47 #include <dev/etherswitch/ip17x/ip17x_reg.h> 48 #include <dev/etherswitch/ip17x/ip17x_var.h> 49 #include <dev/etherswitch/ip17x/ip17x_vlans.h> 50 #include <dev/etherswitch/ip17x/ip175d.h> 51 52 /* 53 * IP175D specific functions. 54 */ 55 56 /* 57 * Reset the switch to default state. 58 */ 59 static int 60 ip175d_reset(struct ip17x_softc *sc) 61 { 62 63 /* Reset all the switch settings. */ 64 ip17x_writephy(sc->sc_dev, IP175D_RESET_PHY, IP175D_RESET_REG, 0x175d); 65 DELAY(2); 66 67 /* Disable the special tagging mode. */ 68 ip17x_updatephy(sc->sc_dev, 21, 22, 0x3, 0x0); 69 70 /* Set 802.1q protocol type. */ 71 ip17x_writephy(sc->sc_dev, 22, 3, 0x8100); 72 73 return (0); 74 } 75 76 /* 77 * Set the Switch configuration. 78 */ 79 static int 80 ip175d_hw_setup(struct ip17x_softc *sc) 81 { 82 struct ip17x_vlan *v; 83 uint32_t ports[IP17X_MAX_VLANS]; 84 uint32_t addtag[IP17X_MAX_VLANS]; 85 uint32_t striptag[IP17X_MAX_VLANS]; 86 uint32_t vlan_mask; 87 int i, j; 88 89 vlan_mask = 0; 90 for (i = 0; i < IP17X_MAX_VLANS; i++) { 91 92 ports[i] = 0; 93 addtag[i] = 0; 94 striptag[i] = 0; 95 96 v = &sc->vlan[i]; 97 if (v->vlanid == 0 || sc->vlan_mode == 0) { 98 /* Vlangroup disabled. Reset the filter. */ 99 ip17x_writephy(sc->sc_dev, 22, 14 + i, i + 1); 100 ports[i] = 0x3f; 101 continue; 102 } 103 104 vlan_mask |= (1 << i); 105 ports[i] = v->ports; 106 107 /* Setup the filter, write the VLAN id. */ 108 ip17x_writephy(sc->sc_dev, 22, 14 + i, v->vlanid); 109 110 for (j = 0; j < MII_NPHY; j++) { 111 if ((ports[i] & (1 << j)) == 0) 112 continue; 113 if (sc->addtag & (1 << j)) 114 addtag[i] |= (1 << j); 115 if (sc->striptag & (1 << j)) 116 striptag[i] |= (1 << j); 117 } 118 } 119 120 /* Write the port masks, tag adds and removals. */ 121 for (i = 0; i < IP17X_MAX_VLANS / 2; i++) { 122 ip17x_writephy(sc->sc_dev, 23, i, 123 ports[2 * i] | (ports[2 * i + 1] << 8)); 124 ip17x_writephy(sc->sc_dev, 23, i + 8, 125 addtag[2 * i] | (addtag[2 * i + 1] << 8)); 126 ip17x_writephy(sc->sc_dev, 23, i + 16, 127 striptag[2 * i] | (striptag[2 * i + 1] << 8)); 128 } 129 130 /* Write the in use vlan mask. */ 131 ip17x_writephy(sc->sc_dev, 22, 10, vlan_mask); 132 133 /* Write the PVID of each port. */ 134 for (i = 0; i < sc->numports; i++) 135 ip17x_writephy(sc->sc_dev, 22, 4 + i, sc->pvid[i]); 136 137 return (0); 138 } 139 140 /* 141 * Set the switch VLAN mode. 142 */ 143 static int 144 ip175d_set_vlan_mode(struct ip17x_softc *sc, uint32_t mode) 145 { 146 147 switch (mode) { 148 case ETHERSWITCH_VLAN_DOT1Q: 149 /* 150 * VLAN classification rules: tag-based VLANs, 151 * use VID to classify, drop packets that cannot 152 * be classified. 153 */ 154 ip17x_updatephy(sc->sc_dev, 22, 0, 0x3fff, 0x003f); 155 sc->vlan_mode = mode; 156 break; 157 case ETHERSWITCH_VLAN_PORT: 158 sc->vlan_mode = mode; 159 /* fallthrough */ 160 default: 161 /* 162 * VLAN classification rules: everything off & 163 * clear table. 164 */ 165 ip17x_updatephy(sc->sc_dev, 22, 0, 0xbfff, 0x8000); 166 sc->vlan_mode = 0; 167 break; 168 }; 169 170 if (sc->vlan_mode != 0) { 171 /* 172 * Ingress rules: CFI=1 dropped, null VID is untagged, VID=1 passed, 173 * VID=0xfff discarded, admin both tagged and untagged, ingress 174 * filters enabled. 175 */ 176 ip17x_updatephy(sc->sc_dev, 22, 1, 0x0fff, 0x0c3f); 177 178 /* Egress rules: IGMP processing off, keep VLAN header off. */ 179 ip17x_updatephy(sc->sc_dev, 22, 2, 0x0fff, 0x0000); 180 } else { 181 ip17x_updatephy(sc->sc_dev, 22, 1, 0x0fff, 0x043f); 182 ip17x_updatephy(sc->sc_dev, 22, 2, 0x0fff, 0x0020); 183 } 184 185 /* Reset vlans. */ 186 ip17x_reset_vlans(sc, sc->vlan_mode); 187 188 /* Update switch configuration. */ 189 ip175d_hw_setup(sc); 190 191 return (0); 192 } 193 194 /* 195 * Get the switch VLAN mode. 196 */ 197 static int 198 ip175d_get_vlan_mode(struct ip17x_softc *sc) 199 { 200 201 return (sc->vlan_mode); 202 } 203 204 void 205 ip175d_attach(struct ip17x_softc *sc) 206 { 207 208 sc->hal.ip17x_reset = ip175d_reset; 209 sc->hal.ip17x_hw_setup = ip175d_hw_setup; 210 sc->hal.ip17x_get_vlan_mode = ip175d_get_vlan_mode; 211 sc->hal.ip17x_set_vlan_mode = ip175d_set_vlan_mode; 212 213 /* Defaults for IP175C. */ 214 sc->cpuport = IP175X_CPU_PORT; 215 sc->numports = IP175X_NUM_PORTS; 216 sc->info.es_vlan_caps = ETHERSWITCH_VLAN_DOT1Q; 217 218 device_printf(sc->sc_dev, "type: IP175D\n"); 219 } 220