| 1 | /* |
| 2 | * Copyright (c) 1994 Herb Peyerl <hpeyerl@novatel.ca> |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. All advertising materials mentioning features or use of this software |
| 14 | * must display the following acknowledgement: |
| 15 | * This product includes software developed by Herb Peyerl. |
| 16 | * 4. The name of Herb Peyerl may not be used to endorse or promote products |
| 17 | * derived from this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | * |
| 30 | * $FreeBSD: src/sys/dev/vx/if_vx.c,v 1.25.2.6 2002/02/13 00:43:10 dillon Exp $ |
| 31 | * $DragonFly: src/sys/dev/netif/vx/if_vx.c,v 1.21 2005/07/01 20:18:39 joerg Exp $ |
| 32 | * |
| 33 | */ |
| 34 | |
| 35 | /* |
| 36 | * Created from if_ep.c driver by Fred Gray (fgray@rice.edu) to support |
| 37 | * the 3c590 family. |
| 38 | */ |
| 39 | |
| 40 | /* |
| 41 | * Modified from the FreeBSD 1.1.5.1 version by: |
| 42 | * Andres Vega Garcia |
| 43 | * INRIA - Sophia Antipolis, France |
| 44 | * avega@sophia.inria.fr |
| 45 | */ |
| 46 | |
| 47 | /* |
| 48 | * Promiscuous mode added and interrupt logic slightly changed |
| 49 | * to reduce the number of adapter failures. Transceiver select |
| 50 | * logic changed to use value from EEPROM. Autoconfiguration |
| 51 | * features added. |
| 52 | * Done by: |
| 53 | * Serge Babkin |
| 54 | * Chelindbank (Chelyabinsk, Russia) |
| 55 | * babkin@hq.icb.chel.su |
| 56 | */ |
| 57 | |
| 58 | #include <sys/param.h> |
| 59 | #include <sys/bus.h> |
| 60 | #include <sys/systm.h> |
| 61 | #include <sys/sockio.h> |
| 62 | #include <sys/malloc.h> |
| 63 | #include <sys/mbuf.h> |
| 64 | #include <sys/socket.h> |
| 65 | #include <sys/linker_set.h> |
| 66 | #include <sys/module.h> |
| 67 | #include <sys/thread2.h> |
| 68 | |
| 69 | #include <net/if.h> |
| 70 | #include <net/ifq_var.h> |
| 71 | |
| 72 | #include <net/ethernet.h> |
| 73 | #include <net/if_arp.h> |
| 74 | |
| 75 | #include <machine/bus_pio.h> |
| 76 | #include <machine/bus.h> |
| 77 | |
| 78 | #include <net/bpf.h> |
| 79 | |
| 80 | #include "if_vxreg.h" |
| 81 | |
| 82 | DECLARE_DUMMY_MODULE(if_vx); |
| 83 | |
| 84 | static struct connector_entry { |
| 85 | int bit; |
| 86 | const char *name; |
| 87 | } conn_tab[VX_CONNECTORS] = { |
| 88 | #define CONNECTOR_UTP 0 |
| 89 | { 0x08, "utp"}, |
| 90 | #define CONNECTOR_AUI 1 |
| 91 | { 0x20, "aui"}, |
| 92 | /* dummy */ |
| 93 | { 0, "???"}, |
| 94 | #define CONNECTOR_BNC 3 |
| 95 | { 0x10, "bnc"}, |
| 96 | #define CONNECTOR_TX 4 |
| 97 | { 0x02, "tx"}, |
| 98 | #define CONNECTOR_FX 5 |
| 99 | { 0x04, "fx"}, |
| 100 | #define CONNECTOR_MII 6 |
| 101 | { 0x40, "mii"}, |
| 102 | { 0, "???"} |
| 103 | }; |
| 104 | |
| 105 | /* int vxattach (struct vx_softc *); */ |
| 106 | static void vxtxstat (struct vx_softc *); |
| 107 | static int vxstatus (struct vx_softc *); |
| 108 | static void vxinit (void *); |
| 109 | static int vxioctl (struct ifnet *, u_long, caddr_t, struct ucred *); |
| 110 | static void vxstart (struct ifnet *ifp); |
| 111 | static void vxwatchdog (struct ifnet *); |
| 112 | static void vxreset (struct vx_softc *); |
| 113 | /* void vxstop (struct vx_softc *); */ |
| 114 | static void vxread (struct vx_softc *); |
| 115 | static struct mbuf *vxget (struct vx_softc *, u_int); |
| 116 | static void vxmbuffill (void *); |
| 117 | static void vxmbufempty (struct vx_softc *); |
| 118 | static void vxsetfilter (struct vx_softc *); |
| 119 | static void vxgetlink (struct vx_softc *); |
| 120 | static void vxsetlink (struct vx_softc *); |
| 121 | /* int vxbusyeeprom (struct vx_softc *); */ |
| 122 | |
| 123 | int |
| 124 | vxattach(device_t dev) |
| 125 | { |
| 126 | struct vx_softc *sc; |
| 127 | struct ifnet *ifp; |
| 128 | int i; |
| 129 | |
| 130 | sc = device_get_softc(dev); |
| 131 | |
| 132 | callout_init(&sc->vx_timer); |
| 133 | GO_WINDOW(0); |
| 134 | CSR_WRITE_2(sc, VX_COMMAND, GLOBAL_RESET); |
| 135 | VX_BUSY_WAIT; |
| 136 | |
| 137 | ifp = &sc->arpcom.ac_if; |
| 138 | if_initname(ifp, device_get_name(dev), device_get_unit(dev)); |
| 139 | |
| 140 | vxgetlink(sc); |
| 141 | |
| 142 | /* |
| 143 | * Read the station address from the eeprom |
| 144 | */ |
| 145 | GO_WINDOW(0); |
| 146 | for (i = 0; i < 3; i++) { |
| 147 | int x; |
| 148 | if (vxbusyeeprom(sc)) |
| 149 | return 0; |
| 150 | CSR_WRITE_2(sc, VX_W0_EEPROM_COMMAND, EEPROM_CMD_RD |
| 151 | | (EEPROM_OEM_ADDR_0 + i)); |
| 152 | if (vxbusyeeprom(sc)) |
| 153 | return 0; |
| 154 | x = CSR_READ_2(sc, VX_W0_EEPROM_DATA); |
| 155 | sc->arpcom.ac_enaddr[(i << 1)] = x >> 8; |
| 156 | sc->arpcom.ac_enaddr[(i << 1) + 1] = x; |
| 157 | } |
| 158 | |
| 159 | ifp->if_mtu = ETHERMTU; |
| 160 | ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN); |
| 161 | ifq_set_ready(&ifp->if_snd); |
| 162 | ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; |
| 163 | ifp->if_start = vxstart; |
| 164 | ifp->if_ioctl = vxioctl; |
| 165 | ifp->if_init = vxinit; |
| 166 | ifp->if_watchdog = vxwatchdog; |
| 167 | ifp->if_softc = sc; |
| 168 | |
| 169 | ether_ifattach(ifp, sc->arpcom.ac_enaddr); |
| 170 | |
| 171 | sc->tx_start_thresh = 20; /* probably a good starting point. */ |
| 172 | |
| 173 | vxstop(sc); |
| 174 | |
| 175 | return 1; |
| 176 | } |
| 177 | |
| 178 | |
| 179 | |
| 180 | /* |
| 181 | * The order in here seems important. Otherwise we may not receive |
| 182 | * interrupts. ?! |
| 183 | */ |
| 184 | static void |
| 185 | vxinit(xsc) |
| 186 | void *xsc; |
| 187 | { |
| 188 | struct vx_softc *sc = (struct vx_softc *) xsc; |
| 189 | struct ifnet *ifp = &sc->arpcom.ac_if; |
| 190 | int i; |
| 191 | |
| 192 | VX_BUSY_WAIT; |
| 193 | |
| 194 | GO_WINDOW(2); |
| 195 | |
| 196 | for (i = 0; i < 6; i++) /* Reload the ether_addr. */ |
| 197 | CSR_WRITE_1(sc, VX_W2_ADDR_0 + i, sc->arpcom.ac_enaddr[i]); |
| 198 | |
| 199 | CSR_WRITE_2(sc, VX_COMMAND, RX_RESET); |
| 200 | VX_BUSY_WAIT; |
| 201 | CSR_WRITE_2(sc, VX_COMMAND, TX_RESET); |
| 202 | VX_BUSY_WAIT; |
| 203 | |
| 204 | GO_WINDOW(1); /* Window 1 is operating window */ |
| 205 | for (i = 0; i < 31; i++) |
| 206 | CSR_READ_1(sc, VX_W1_TX_STATUS); |
| 207 | |
| 208 | CSR_WRITE_2(sc, VX_COMMAND,SET_RD_0_MASK | S_CARD_FAILURE | |
| 209 | S_RX_COMPLETE | S_TX_COMPLETE | S_TX_AVAIL); |
| 210 | CSR_WRITE_2(sc, VX_COMMAND,SET_INTR_MASK | S_CARD_FAILURE | |
| 211 | S_RX_COMPLETE | S_TX_COMPLETE | S_TX_AVAIL); |
| 212 | |
| 213 | /* |
| 214 | * Attempt to get rid of any stray interrupts that occured during |
| 215 | * configuration. On the i386 this isn't possible because one may |
| 216 | * already be queued. However, a single stray interrupt is |
| 217 | * unimportant. |
| 218 | */ |
| 219 | CSR_WRITE_2(sc, VX_COMMAND, ACK_INTR | 0xff); |
| 220 | |
| 221 | vxsetfilter(sc); |
| 222 | vxsetlink(sc); |
| 223 | |
| 224 | CSR_WRITE_2(sc, VX_COMMAND, RX_ENABLE); |
| 225 | CSR_WRITE_2(sc, VX_COMMAND, TX_ENABLE); |
| 226 | |
| 227 | vxmbuffill((caddr_t) sc); |
| 228 | |
| 229 | /* Interface is now `running', with no output active. */ |
| 230 | ifp->if_flags |= IFF_RUNNING; |
| 231 | ifp->if_flags &= ~IFF_OACTIVE; |
| 232 | |
| 233 | /* Attempt to start output, if any. */ |
| 234 | vxstart(ifp); |
| 235 | } |
| 236 | |
| 237 | static void |
| 238 | vxsetfilter(sc) |
| 239 | struct vx_softc *sc; |
| 240 | { |
| 241 | struct ifnet *ifp = &sc->arpcom.ac_if; |
| 242 | |
| 243 | GO_WINDOW(1); /* Window 1 is operating window */ |
| 244 | CSR_WRITE_2(sc, VX_COMMAND, SET_RX_FILTER | FIL_INDIVIDUAL | FIL_BRDCST | |
| 245 | FIL_MULTICAST | |
| 246 | ((ifp->if_flags & IFF_PROMISC) ? FIL_PROMISC : 0 )); |
| 247 | } |
| 248 | |
| 249 | static void |
| 250 | vxgetlink(sc) |
| 251 | struct vx_softc *sc; |
| 252 | { |
| 253 | int n, k; |
| 254 | |
| 255 | GO_WINDOW(3); |
| 256 | sc->vx_connectors = CSR_READ_2(sc, VX_W3_RESET_OPT) & 0x7f; |
| 257 | for (n = 0, k = 0; k < VX_CONNECTORS; k++) { |
| 258 | if (sc->vx_connectors & conn_tab[k].bit) { |
| 259 | if (n == 0) |
| 260 | if_printf(&sc->arpcom.ac_if, "%s", conn_tab[k].name); |
| 261 | else |
| 262 | printf("/%s", conn_tab[k].name); |
| 263 | n++; |
| 264 | } |
| 265 | } |
| 266 | if (n == 0) { |
| 267 | if_printf(&sc->arpcom.ac_if, "no connectors!\n"); |
| 268 | return; |
| 269 | } |
| 270 | GO_WINDOW(3); |
| 271 | sc->vx_connector = (CSR_READ_4(sc, VX_W3_INTERNAL_CFG) |
| 272 | & INTERNAL_CONNECTOR_MASK) |
| 273 | >> INTERNAL_CONNECTOR_BITS; |
| 274 | if (sc->vx_connector & 0x10) { |
| 275 | sc->vx_connector &= 0x0f; |
| 276 | printf("[*%s*]", conn_tab[(int)sc->vx_connector].name); |
| 277 | printf(": disable 'auto select' with DOS util!\n"); |
| 278 | } else { |
| 279 | printf("[*%s*]\n", conn_tab[(int)sc->vx_connector].name); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | static void |
| 284 | vxsetlink(sc) |
| 285 | struct vx_softc *sc; |
| 286 | { |
| 287 | struct ifnet *ifp = &sc->arpcom.ac_if; |
| 288 | int i, j, k; |
| 289 | const char *reason, *warning; |
| 290 | static short prev_flags; |
| 291 | static char prev_conn = -1; |
| 292 | |
| 293 | if (prev_conn == -1) { |
| 294 | prev_conn = sc->vx_connector; |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * S.B. |
| 299 | * |
| 300 | * Now behavior was slightly changed: |
| 301 | * |
| 302 | * if any of flags link[0-2] is used and its connector is |
| 303 | * physically present the following connectors are used: |
| 304 | * |
| 305 | * link0 - AUI * highest precedence |
| 306 | * link1 - BNC |
| 307 | * link2 - UTP * lowest precedence |
| 308 | * |
| 309 | * If none of them is specified then |
| 310 | * connector specified in the EEPROM is used |
| 311 | * (if present on card or UTP if not). |
| 312 | */ |
| 313 | |
| 314 | i = sc->vx_connector; /* default in EEPROM */ |
| 315 | reason = "default"; |
| 316 | warning = 0; |
| 317 | |
| 318 | if (ifp->if_flags & IFF_LINK0) { |
| 319 | if (sc->vx_connectors & conn_tab[CONNECTOR_AUI].bit) { |
| 320 | i = CONNECTOR_AUI; |
| 321 | reason = "link0"; |
| 322 | } else { |
| 323 | warning = "aui not present! (link0)"; |
| 324 | } |
| 325 | } else if (ifp->if_flags & IFF_LINK1) { |
| 326 | if (sc->vx_connectors & conn_tab[CONNECTOR_BNC].bit) { |
| 327 | i = CONNECTOR_BNC; |
| 328 | reason = "link1"; |
| 329 | } else { |
| 330 | warning = "bnc not present! (link1)"; |
| 331 | } |
| 332 | } else if (ifp->if_flags & IFF_LINK2) { |
| 333 | if (sc->vx_connectors & conn_tab[CONNECTOR_UTP].bit) { |
| 334 | i = CONNECTOR_UTP; |
| 335 | reason = "link2"; |
| 336 | } else { |
| 337 | warning = "utp not present! (link2)"; |
| 338 | } |
| 339 | } else if ((sc->vx_connectors & conn_tab[(int)sc->vx_connector].bit) == 0) { |
| 340 | warning = "strange connector type in EEPROM."; |
| 341 | reason = "forced"; |
| 342 | i = CONNECTOR_UTP; |
| 343 | } |
| 344 | |
| 345 | /* Avoid unnecessary message. */ |
| 346 | k = (prev_flags ^ ifp->if_flags) & (IFF_LINK0 | IFF_LINK1 | IFF_LINK2); |
| 347 | if ((k != 0) || (prev_conn != i)) { |
| 348 | if (warning != 0) { |
| 349 | if_printf(ifp, "warning: %s\n", warning); |
| 350 | } |
| 351 | if_printf(ifp, "selected %s. (%s)\n", conn_tab[i].name, reason); |
| 352 | } |
| 353 | |
| 354 | /* Set the selected connector. */ |
| 355 | GO_WINDOW(3); |
| 356 | j = CSR_READ_4(sc, VX_W3_INTERNAL_CFG) & ~INTERNAL_CONNECTOR_MASK; |
| 357 | CSR_WRITE_4(sc, VX_W3_INTERNAL_CFG, j | (i <<INTERNAL_CONNECTOR_BITS)); |
| 358 | |
| 359 | /* First, disable all. */ |
| 360 | CSR_WRITE_2(sc,VX_COMMAND, STOP_TRANSCEIVER); |
| 361 | DELAY(800); |
| 362 | GO_WINDOW(4); |
| 363 | CSR_WRITE_2(sc, VX_W4_MEDIA_TYPE, 0); |
| 364 | |
| 365 | /* Second, enable the selected one. */ |
| 366 | switch(i) { |
| 367 | case CONNECTOR_UTP: |
| 368 | GO_WINDOW(4); |
| 369 | CSR_WRITE_2(sc, VX_W4_MEDIA_TYPE, ENABLE_UTP); |
| 370 | break; |
| 371 | case CONNECTOR_BNC: |
| 372 | CSR_WRITE_2(sc, VX_COMMAND, START_TRANSCEIVER); |
| 373 | DELAY(800); |
| 374 | break; |
| 375 | case CONNECTOR_TX: |
| 376 | case CONNECTOR_FX: |
| 377 | GO_WINDOW(4); |
| 378 | CSR_WRITE_2(sc, VX_W4_MEDIA_TYPE, LINKBEAT_ENABLE); |
| 379 | break; |
| 380 | default: /* AUI and MII fall here */ |
| 381 | break; |
| 382 | } |
| 383 | GO_WINDOW(1); |
| 384 | |
| 385 | prev_flags = ifp->if_flags; |
| 386 | prev_conn = i; |
| 387 | } |
| 388 | |
| 389 | static void |
| 390 | vxstart(ifp) |
| 391 | struct ifnet *ifp; |
| 392 | { |
| 393 | struct vx_softc *sc = ifp->if_softc; |
| 394 | struct mbuf *m0; |
| 395 | int len, pad; |
| 396 | |
| 397 | /* Don't transmit if interface is busy or not running */ |
| 398 | if ((sc->arpcom.ac_if.if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING) |
| 399 | return; |
| 400 | |
| 401 | startagain: |
| 402 | /* Sneak a peek at the next packet */ |
| 403 | m0 = ifq_poll(&ifp->if_snd); |
| 404 | if (m0 == NULL) |
| 405 | return; |
| 406 | /* We need to use m->m_pkthdr.len, so require the header */ |
| 407 | if ((m0->m_flags & M_PKTHDR) == 0) |
| 408 | panic("vxstart: no header mbuf"); |
| 409 | len = m0->m_pkthdr.len; |
| 410 | |
| 411 | pad = (4 - len) & 3; |
| 412 | |
| 413 | /* |
| 414 | * The 3c509 automatically pads short packets to minimum ethernet length, |
| 415 | * but we drop packets that are too large. Perhaps we should truncate |
| 416 | * them instead? |
| 417 | */ |
| 418 | if (len + pad > ETHER_MAX_LEN) { |
| 419 | /* packet is obviously too large: toss it */ |
| 420 | ++ifp->if_oerrors; |
| 421 | m0 = ifq_dequeue(&ifp->if_snd); |
| 422 | m_freem(m0); |
| 423 | goto readcheck; |
| 424 | } |
| 425 | VX_BUSY_WAIT; |
| 426 | if (CSR_READ_2(sc, VX_W1_FREE_TX) < len + pad + 4) { |
| 427 | CSR_WRITE_2(sc, VX_COMMAND, SET_TX_AVAIL_THRESH | ((len + pad + 4) >> 2)); |
| 428 | /* not enough room in FIFO */ |
| 429 | if (CSR_READ_2(sc, VX_W1_FREE_TX) < len + pad + 4) { /* make sure */ |
| 430 | ifp->if_flags |= IFF_OACTIVE; |
| 431 | ifp->if_timer = 1; |
| 432 | return; |
| 433 | } |
| 434 | } |
| 435 | CSR_WRITE_2(sc, VX_COMMAND, SET_TX_AVAIL_THRESH | (8188 >> 2)); |
| 436 | m0 = ifq_dequeue(&ifp->if_snd); |
| 437 | |
| 438 | VX_BUSY_WAIT; |
| 439 | CSR_WRITE_2(sc, VX_COMMAND, SET_TX_START_THRESH | |
| 440 | ((len / 4 + sc->tx_start_thresh) >> 2)); |
| 441 | |
| 442 | BPF_MTAP(&sc->arpcom.ac_if, m0); |
| 443 | |
| 444 | /* |
| 445 | * Do the output in a critical section so that an interrupt from another device |
| 446 | * won't cause a FIFO underrun. |
| 447 | */ |
| 448 | crit_enter(); |
| 449 | |
| 450 | CSR_WRITE_4(sc, VX_W1_TX_PIO_WR_1, len | TX_INDICATE); |
| 451 | |
| 452 | while (m0) { |
| 453 | if (m0->m_len > 3) |
| 454 | bus_space_write_multi_4(sc->vx_btag, sc->vx_bhandle, |
| 455 | VX_W1_TX_PIO_WR_1, |
| 456 | (u_int32_t *)mtod(m0, caddr_t), m0->m_len / 4); |
| 457 | if (m0->m_len & 3) |
| 458 | bus_space_write_multi_1(sc->vx_btag, sc->vx_bhandle, |
| 459 | VX_W1_TX_PIO_WR_1, |
| 460 | mtod(m0, caddr_t) + (m0->m_len & ~3), m0->m_len & 3); |
| 461 | m0 = m_free(m0); |
| 462 | } |
| 463 | while (pad--) |
| 464 | CSR_WRITE_1(sc, VX_W1_TX_PIO_WR_1, 0); /* Padding */ |
| 465 | |
| 466 | crit_exit(); |
| 467 | |
| 468 | ++ifp->if_opackets; |
| 469 | ifp->if_timer = 1; |
| 470 | |
| 471 | readcheck: |
| 472 | if ((CSR_READ_2(sc, VX_W1_RX_STATUS) & ERR_INCOMPLETE) == 0) { |
| 473 | /* We received a complete packet. */ |
| 474 | |
| 475 | if ((CSR_READ_2(sc, VX_STATUS) & S_INTR_LATCH) != 0) { |
| 476 | /* Got an interrupt, return so that it gets serviced. */ |
| 477 | return; |
| 478 | } |
| 479 | /* |
| 480 | * No interrupt, read the packet and continue |
| 481 | * Is this supposed to happen? Is my motherboard |
| 482 | * completely busted? |
| 483 | */ |
| 484 | vxread(sc); |
| 485 | } else { |
| 486 | /* Check if we are stuck and reset [see XXX comment] */ |
| 487 | if (vxstatus(sc)) { |
| 488 | if (ifp->if_flags & IFF_DEBUG) |
| 489 | if_printf(ifp, "adapter reset\n"); |
| 490 | vxreset(sc); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | goto startagain; |
| 495 | } |
| 496 | |
| 497 | /* |
| 498 | * XXX: The 3c509 card can get in a mode where both the fifo status bit |
| 499 | * FIFOS_RX_OVERRUN and the status bit ERR_INCOMPLETE are set |
| 500 | * We detect this situation and we reset the adapter. |
| 501 | * It happens at times when there is a lot of broadcast traffic |
| 502 | * on the cable (once in a blue moon). |
| 503 | */ |
| 504 | static int |
| 505 | vxstatus(sc) |
| 506 | struct vx_softc *sc; |
| 507 | { |
| 508 | int fifost; |
| 509 | |
| 510 | /* |
| 511 | * Check the FIFO status and act accordingly |
| 512 | */ |
| 513 | GO_WINDOW(4); |
| 514 | fifost = CSR_READ_2(sc, VX_W4_FIFO_DIAG); |
| 515 | GO_WINDOW(1); |
| 516 | |
| 517 | if (fifost & FIFOS_RX_UNDERRUN) { |
| 518 | if (sc->arpcom.ac_if.if_flags & IFF_DEBUG) |
| 519 | if_printf(&sc->arpcom.ac_if, "RX underrun\n"); |
| 520 | vxreset(sc); |
| 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | if (fifost & FIFOS_RX_STATUS_OVERRUN) { |
| 525 | if (sc->arpcom.ac_if.if_flags & IFF_DEBUG) |
| 526 | if_printf(&sc->arpcom.ac_if, "RX Status overrun\n"); |
| 527 | return 1; |
| 528 | } |
| 529 | |
| 530 | if (fifost & FIFOS_RX_OVERRUN) { |
| 531 | if (sc->arpcom.ac_if.if_flags & IFF_DEBUG) |
| 532 | if_printf(&sc->arpcom.ac_if, "RX overrun\n"); |
| 533 | return 1; |
| 534 | } |
| 535 | |
| 536 | if (fifost & FIFOS_TX_OVERRUN) { |
| 537 | if (sc->arpcom.ac_if.if_flags & IFF_DEBUG) |
| 538 | if_printf(&sc->arpcom.ac_if, "TX overrun\n"); |
| 539 | vxreset(sc); |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | return 0; |
| 544 | } |
| 545 | |
| 546 | static void |
| 547 | vxtxstat(sc) |
| 548 | struct vx_softc *sc; |
| 549 | { |
| 550 | int i; |
| 551 | |
| 552 | /* |
| 553 | * We need to read+write TX_STATUS until we get a 0 status |
| 554 | * in order to turn off the interrupt flag. |
| 555 | */ |
| 556 | while ((i = CSR_READ_1(sc, VX_W1_TX_STATUS)) & TXS_COMPLETE) { |
| 557 | CSR_WRITE_1(sc, VX_W1_TX_STATUS, 0x0); |
| 558 | |
| 559 | if (i & TXS_JABBER) { |
| 560 | ++sc->arpcom.ac_if.if_oerrors; |
| 561 | if (sc->arpcom.ac_if.if_flags & IFF_DEBUG) |
| 562 | if_printf(&sc->arpcom.ac_if, "jabber (%x)\n", i); |
| 563 | vxreset(sc); |
| 564 | } else if (i & TXS_UNDERRUN) { |
| 565 | ++sc->arpcom.ac_if.if_oerrors; |
| 566 | if (sc->arpcom.ac_if.if_flags & IFF_DEBUG) { |
| 567 | if_printf(&sc->arpcom.ac_if, "fifo underrun (%x) @%d\n", |
| 568 | i, sc->tx_start_thresh); |
| 569 | } |
| 570 | if (sc->tx_succ_ok < 100) |
| 571 | sc->tx_start_thresh = min(ETHER_MAX_LEN, sc->tx_start_thresh + 20); |
| 572 | sc->tx_succ_ok = 0; |
| 573 | vxreset(sc); |
| 574 | } else if (i & TXS_MAX_COLLISION) { |
| 575 | ++sc->arpcom.ac_if.if_collisions; |
| 576 | CSR_WRITE_2(sc, VX_COMMAND, TX_ENABLE); |
| 577 | sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE; |
| 578 | } else |
| 579 | sc->tx_succ_ok = (sc->tx_succ_ok+1) & 127; |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | void |
| 584 | vxintr(voidsc) |
| 585 | void *voidsc; |
| 586 | { |
| 587 | short status; |
| 588 | struct vx_softc *sc = voidsc; |
| 589 | struct ifnet *ifp = &sc->arpcom.ac_if; |
| 590 | |
| 591 | for (;;) { |
| 592 | CSR_WRITE_2(sc, VX_COMMAND, C_INTR_LATCH); |
| 593 | |
| 594 | status = CSR_READ_2(sc, VX_STATUS); |
| 595 | |
| 596 | if ((status & (S_TX_COMPLETE | S_TX_AVAIL | |
| 597 | S_RX_COMPLETE | S_CARD_FAILURE)) == 0) |
| 598 | break; |
| 599 | |
| 600 | /* |
| 601 | * Acknowledge any interrupts. It's important that we do this |
| 602 | * first, since there would otherwise be a race condition. |
| 603 | * Due to the i386 interrupt queueing, we may get spurious |
| 604 | * interrupts occasionally. |
| 605 | */ |
| 606 | CSR_WRITE_2(sc, VX_COMMAND, ACK_INTR | status); |
| 607 | |
| 608 | if (status & S_RX_COMPLETE) |
| 609 | vxread(sc); |
| 610 | if (status & S_TX_AVAIL) { |
| 611 | ifp->if_timer = 0; |
| 612 | sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE; |
| 613 | vxstart(&sc->arpcom.ac_if); |
| 614 | } |
| 615 | if (status & S_CARD_FAILURE) { |
| 616 | if_printf(ifp, "adapter failure (%x)\n", status); |
| 617 | ifp->if_timer = 0; |
| 618 | vxreset(sc); |
| 619 | return; |
| 620 | } |
| 621 | if (status & S_TX_COMPLETE) { |
| 622 | ifp->if_timer = 0; |
| 623 | vxtxstat(sc); |
| 624 | vxstart(ifp); |
| 625 | } |
| 626 | } |
| 627 | /* no more interrupts */ |
| 628 | } |
| 629 | |
| 630 | static void |
| 631 | vxread(sc) |
| 632 | struct vx_softc *sc; |
| 633 | { |
| 634 | struct ifnet *ifp = &sc->arpcom.ac_if; |
| 635 | struct mbuf *m; |
| 636 | struct ether_header *eh; |
| 637 | u_int len; |
| 638 | |
| 639 | len = CSR_READ_2(sc, VX_W1_RX_STATUS); |
| 640 | |
| 641 | again: |
| 642 | |
| 643 | if (ifp->if_flags & IFF_DEBUG) { |
| 644 | int err = len & ERR_MASK; |
| 645 | const char *s = NULL; |
| 646 | |
| 647 | if (len & ERR_INCOMPLETE) |
| 648 | s = "incomplete packet"; |
| 649 | else if (err == ERR_OVERRUN) |
| 650 | s = "packet overrun"; |
| 651 | else if (err == ERR_RUNT) |
| 652 | s = "runt packet"; |
| 653 | else if (err == ERR_ALIGNMENT) |
| 654 | s = "bad alignment"; |
| 655 | else if (err == ERR_CRC) |
| 656 | s = "bad crc"; |
| 657 | else if (err == ERR_OVERSIZE) |
| 658 | s = "oversized packet"; |
| 659 | else if (err == ERR_DRIBBLE) |
| 660 | s = "dribble bits"; |
| 661 | |
| 662 | if (s) |
| 663 | if_printf(ifp, "%s\n", s); |
| 664 | } |
| 665 | |
| 666 | if (len & ERR_INCOMPLETE) |
| 667 | return; |
| 668 | |
| 669 | if (len & ERR_RX) { |
| 670 | ++ifp->if_ierrors; |
| 671 | goto abort; |
| 672 | } |
| 673 | |
| 674 | len &= RX_BYTES_MASK; /* Lower 11 bits = RX bytes. */ |
| 675 | |
| 676 | /* Pull packet off interface. */ |
| 677 | m = vxget(sc, len); |
| 678 | if (m == 0) { |
| 679 | ifp->if_ierrors++; |
| 680 | goto abort; |
| 681 | } |
| 682 | |
| 683 | ++ifp->if_ipackets; |
| 684 | |
| 685 | /* We assume the header fit entirely in one mbuf. */ |
| 686 | eh = mtod(m, struct ether_header *); |
| 687 | |
| 688 | /* |
| 689 | * XXX: Some cards seem to be in promiscous mode all the time. |
| 690 | * we need to make sure we only get our own stuff always. |
| 691 | * bleah! |
| 692 | */ |
| 693 | |
| 694 | if ((eh->ether_dhost[0] & 1) == 0 /* !mcast and !bcast */ |
| 695 | && bcmp(eh->ether_dhost, sc->arpcom.ac_enaddr, ETHER_ADDR_LEN) != 0) { |
| 696 | m_freem(m); |
| 697 | return; |
| 698 | } |
| 699 | |
| 700 | (*ifp->if_input)(ifp, m); |
| 701 | |
| 702 | /* |
| 703 | * In periods of high traffic we can actually receive enough |
| 704 | * packets so that the fifo overrun bit will be set at this point, |
| 705 | * even though we just read a packet. In this case we |
| 706 | * are not going to receive any more interrupts. We check for |
| 707 | * this condition and read again until the fifo is not full. |
| 708 | * We could simplify this test by not using vxstatus(), but |
| 709 | * rechecking the RX_STATUS register directly. This test could |
| 710 | * result in unnecessary looping in cases where there is a new |
| 711 | * packet but the fifo is not full, but it will not fix the |
| 712 | * stuck behavior. |
| 713 | * |
| 714 | * Even with this improvement, we still get packet overrun errors |
| 715 | * which are hurting performance. Maybe when I get some more time |
| 716 | * I'll modify vxread() so that it can handle RX_EARLY interrupts. |
| 717 | */ |
| 718 | if (vxstatus(sc)) { |
| 719 | len = CSR_READ_2(sc, VX_W1_RX_STATUS); |
| 720 | /* Check if we are stuck and reset [see XXX comment] */ |
| 721 | if (len & ERR_INCOMPLETE) { |
| 722 | if (ifp->if_flags & IFF_DEBUG) |
| 723 | if_printf(ifp, "adapter reset\n"); |
| 724 | vxreset(sc); |
| 725 | return; |
| 726 | } |
| 727 | goto again; |
| 728 | } |
| 729 | |
| 730 | return; |
| 731 | |
| 732 | abort: |
| 733 | CSR_WRITE_2(sc, VX_COMMAND, RX_DISCARD_TOP_PACK); |
| 734 | } |
| 735 | |
| 736 | static struct mbuf * |
| 737 | vxget(sc, totlen) |
| 738 | struct vx_softc *sc; |
| 739 | u_int totlen; |
| 740 | { |
| 741 | struct ifnet *ifp = &sc->arpcom.ac_if; |
| 742 | struct mbuf *top, **mp, *m; |
| 743 | int len; |
| 744 | |
| 745 | m = sc->mb[sc->next_mb]; |
| 746 | sc->mb[sc->next_mb] = 0; |
| 747 | if (m == 0) { |
| 748 | MGETHDR(m, MB_DONTWAIT, MT_DATA); |
| 749 | if (m == 0) |
| 750 | return 0; |
| 751 | } else { |
| 752 | /* If the queue is no longer full, refill. */ |
| 753 | if (sc->last_mb == sc->next_mb && sc->buffill_pending == 0) { |
| 754 | callout_reset(&sc->vx_timer, 1, vxmbuffill, sc); |
| 755 | sc->buffill_pending = 1; |
| 756 | } |
| 757 | /* Convert one of our saved mbuf's. */ |
| 758 | sc->next_mb = (sc->next_mb + 1) % MAX_MBS; |
| 759 | m->m_data = m->m_pktdat; |
| 760 | m->m_flags = M_PKTHDR; |
| 761 | bzero(&m->m_pkthdr, sizeof(m->m_pkthdr)); |
| 762 | } |
| 763 | m->m_pkthdr.rcvif = ifp; |
| 764 | m->m_pkthdr.len = totlen; |
| 765 | len = MHLEN; |
| 766 | top = 0; |
| 767 | mp = ⊤ |
| 768 | |
| 769 | /* |
| 770 | * We read the packet in a critical section so that an interrupt from another |
| 771 | * device doesn't cause the card's buffer to overflow while we're |
| 772 | * reading it. We may still lose packets at other times. |
| 773 | */ |
| 774 | crit_enter(); |
| 775 | |
| 776 | /* |
| 777 | * Since we don't set allowLargePackets bit in MacControl register, |
| 778 | * we can assume that totlen <= 1500bytes. |
| 779 | * The while loop will be performed iff we have a packet with |
| 780 | * MLEN < m_len < MINCLSIZE. |
| 781 | */ |
| 782 | while (totlen > 0) { |
| 783 | if (top) { |
| 784 | m = sc->mb[sc->next_mb]; |
| 785 | sc->mb[sc->next_mb] = 0; |
| 786 | if (m == 0) { |
| 787 | MGET(m, MB_DONTWAIT, MT_DATA); |
| 788 | if (m == 0) { |
| 789 | crit_exit(); |
| 790 | m_freem(top); |
| 791 | return 0; |
| 792 | } |
| 793 | } else { |
| 794 | sc->next_mb = (sc->next_mb + 1) % MAX_MBS; |
| 795 | } |
| 796 | len = MLEN; |
| 797 | } |
| 798 | if (totlen >= MINCLSIZE) { |
| 799 | MCLGET(m, MB_DONTWAIT); |
| 800 | if (m->m_flags & M_EXT) |
| 801 | len = MCLBYTES; |
| 802 | } |
| 803 | len = min(totlen, len); |
| 804 | if (len > 3) |
| 805 | bus_space_read_multi_4(sc->vx_btag, sc->vx_bhandle, |
| 806 | VX_W1_RX_PIO_RD_1, mtod(m, u_int32_t *), len / 4); |
| 807 | if (len & 3) { |
| 808 | bus_space_read_multi_1(sc->vx_btag, sc->vx_bhandle, |
| 809 | VX_W1_RX_PIO_RD_1, mtod(m, u_int8_t *) + (len & ~3), |
| 810 | len & 3); |
| 811 | } |
| 812 | m->m_len = len; |
| 813 | totlen -= len; |
| 814 | *mp = m; |
| 815 | mp = &m->m_next; |
| 816 | } |
| 817 | |
| 818 | CSR_WRITE_2(sc, VX_COMMAND, RX_DISCARD_TOP_PACK); |
| 819 | |
| 820 | crit_exit(); |
| 821 | |
| 822 | return top; |
| 823 | } |
| 824 | |
| 825 | |
| 826 | static int |
| 827 | vxioctl(ifp, cmd, data, cr) |
| 828 | struct ifnet *ifp; |
| 829 | u_long cmd; |
| 830 | caddr_t data; |
| 831 | struct ucred *cr; |
| 832 | { |
| 833 | struct vx_softc *sc = ifp->if_softc; |
| 834 | struct ifreq *ifr = (struct ifreq *) data; |
| 835 | int error = 0; |
| 836 | |
| 837 | crit_enter(); |
| 838 | |
| 839 | switch (cmd) { |
| 840 | case SIOCSIFFLAGS: |
| 841 | if ((ifp->if_flags & IFF_UP) == 0 && |
| 842 | (ifp->if_flags & IFF_RUNNING) != 0) { |
| 843 | /* |
| 844 | * If interface is marked up and it is stopped, then |
| 845 | * start it. |
| 846 | */ |
| 847 | vxstop(sc); |
| 848 | ifp->if_flags &= ~IFF_RUNNING; |
| 849 | } else if ((ifp->if_flags & IFF_UP) != 0 && |
| 850 | (ifp->if_flags & IFF_RUNNING) == 0) { |
| 851 | /* |
| 852 | * If interface is marked up and it is stopped, then |
| 853 | * start it. |
| 854 | */ |
| 855 | vxinit(sc); |
| 856 | } else { |
| 857 | /* |
| 858 | * deal with flags changes: |
| 859 | * IFF_MULTICAST, IFF_PROMISC, |
| 860 | * IFF_LINK0, IFF_LINK1, |
| 861 | */ |
| 862 | vxsetfilter(sc); |
| 863 | vxsetlink(sc); |
| 864 | } |
| 865 | break; |
| 866 | |
| 867 | case SIOCSIFMTU: |
| 868 | /* |
| 869 | * Set the interface MTU. |
| 870 | */ |
| 871 | if (ifr->ifr_mtu > ETHERMTU) { |
| 872 | error = EINVAL; |
| 873 | } else { |
| 874 | ifp->if_mtu = ifr->ifr_mtu; |
| 875 | } |
| 876 | break; |
| 877 | |
| 878 | case SIOCADDMULTI: |
| 879 | case SIOCDELMULTI: |
| 880 | /* |
| 881 | * Multicast list has changed; set the hardware filter |
| 882 | * accordingly. |
| 883 | */ |
| 884 | vxreset(sc); |
| 885 | error = 0; |
| 886 | break; |
| 887 | |
| 888 | |
| 889 | default: |
| 890 | ether_ioctl(ifp, cmd, data); |
| 891 | break; |
| 892 | } |
| 893 | |
| 894 | crit_exit(); |
| 895 | |
| 896 | return (error); |
| 897 | } |
| 898 | |
| 899 | static void |
| 900 | vxreset(sc) |
| 901 | struct vx_softc *sc; |
| 902 | { |
| 903 | |
| 904 | crit_enter(); |
| 905 | |
| 906 | vxstop(sc); |
| 907 | vxinit(sc); |
| 908 | crit_exit(); |
| 909 | } |
| 910 | |
| 911 | static void |
| 912 | vxwatchdog(ifp) |
| 913 | struct ifnet *ifp; |
| 914 | { |
| 915 | struct vx_softc *sc = ifp->if_softc; |
| 916 | |
| 917 | if (ifp->if_flags & IFF_DEBUG) |
| 918 | if_printf(ifp, "device timeout\n"); |
| 919 | ifp->if_flags &= ~IFF_OACTIVE; |
| 920 | vxstart(ifp); |
| 921 | vxintr(sc); |
| 922 | } |
| 923 | |
| 924 | void |
| 925 | vxstop(sc) |
| 926 | struct vx_softc *sc; |
| 927 | { |
| 928 | struct ifnet *ifp = &sc->arpcom.ac_if; |
| 929 | |
| 930 | ifp->if_timer = 0; |
| 931 | |
| 932 | CSR_WRITE_2(sc, VX_COMMAND, RX_DISABLE); |
| 933 | CSR_WRITE_2(sc, VX_COMMAND, RX_DISCARD_TOP_PACK); |
| 934 | VX_BUSY_WAIT; |
| 935 | CSR_WRITE_2(sc, VX_COMMAND, TX_DISABLE); |
| 936 | CSR_WRITE_2(sc, VX_COMMAND, STOP_TRANSCEIVER); |
| 937 | DELAY(800); |
| 938 | CSR_WRITE_2(sc, VX_COMMAND, RX_RESET); |
| 939 | VX_BUSY_WAIT; |
| 940 | CSR_WRITE_2(sc, VX_COMMAND, TX_RESET); |
| 941 | VX_BUSY_WAIT; |
| 942 | CSR_WRITE_2(sc, VX_COMMAND, C_INTR_LATCH); |
| 943 | CSR_WRITE_2(sc, VX_COMMAND, SET_RD_0_MASK); |
| 944 | CSR_WRITE_2(sc, VX_COMMAND, SET_INTR_MASK); |
| 945 | CSR_WRITE_2(sc, VX_COMMAND, SET_RX_FILTER); |
| 946 | |
| 947 | vxmbufempty(sc); |
| 948 | } |
| 949 | |
| 950 | int |
| 951 | vxbusyeeprom(sc) |
| 952 | struct vx_softc *sc; |
| 953 | { |
| 954 | int j, i = 100; |
| 955 | |
| 956 | while (i--) { |
| 957 | j = CSR_READ_2(sc, VX_W0_EEPROM_COMMAND); |
| 958 | if (j & EEPROM_BUSY) |
| 959 | DELAY(100); |
| 960 | else |
| 961 | break; |
| 962 | } |
| 963 | if (!i) { |
| 964 | if_printf(&sc->arpcom.ac_if, "eeprom failed to come ready\n"); |
| 965 | return (1); |
| 966 | } |
| 967 | return (0); |
| 968 | } |
| 969 | |
| 970 | static void |
| 971 | vxmbuffill(sp) |
| 972 | void *sp; |
| 973 | { |
| 974 | struct vx_softc *sc = (struct vx_softc *) sp; |
| 975 | int i; |
| 976 | |
| 977 | crit_enter(); |
| 978 | i = sc->last_mb; |
| 979 | do { |
| 980 | if (sc->mb[i] == NULL) |
| 981 | MGET(sc->mb[i], MB_DONTWAIT, MT_DATA); |
| 982 | if (sc->mb[i] == NULL) |
| 983 | break; |
| 984 | i = (i + 1) % MAX_MBS; |
| 985 | } while (i != sc->next_mb); |
| 986 | sc->last_mb = i; |
| 987 | /* If the queue was not filled, try again. */ |
| 988 | if (sc->last_mb != sc->next_mb) { |
| 989 | callout_reset(&sc->vx_timer, 1, vxmbuffill, sc); |
| 990 | sc->buffill_pending = 1; |
| 991 | } else { |
| 992 | sc->buffill_pending = 0; |
| 993 | } |
| 994 | crit_exit(); |
| 995 | } |
| 996 | |
| 997 | static void |
| 998 | vxmbufempty(sc) |
| 999 | struct vx_softc *sc; |
| 1000 | { |
| 1001 | int i; |
| 1002 | |
| 1003 | crit_enter(); |
| 1004 | for (i = 0; i < MAX_MBS; i++) { |
| 1005 | if (sc->mb[i]) { |
| 1006 | m_freem(sc->mb[i]); |
| 1007 | sc->mb[i] = NULL; |
| 1008 | } |
| 1009 | } |
| 1010 | sc->last_mb = sc->next_mb = 0; |
| 1011 | if (sc->buffill_pending != 0) |
| 1012 | callout_stop(&sc->vx_timer); |
| 1013 | crit_exit(); |
| 1014 | } |