if_ep.c revision 127135
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 31#include <sys/cdefs.h> 32__FBSDID("$FreeBSD: head/sys/dev/ep/if_ep.c 127135 2004-03-17 17:50:55Z njl $"); 33 34/* 35 * Modified from the FreeBSD 1.1.5.1 version by: 36 * Andres Vega Garcia 37 * INRIA - Sophia Antipolis, France 38 * avega@sophia.inria.fr 39 */ 40 41/* 42 * Promiscuous mode added and interrupt logic slightly changed 43 * to reduce the number of adapter failures. Transceiver select 44 * logic changed to use value from EEPROM. Autoconfiguration 45 * features added. 46 * Done by: 47 * Serge Babkin 48 * Chelindbank (Chelyabinsk, Russia) 49 * babkin@hq.icb.chel.su 50 */ 51 52/* 53 * Pccard support for 3C589 by: 54 * HAMADA Naoki 55 * nao@tom-yam.or.jp 56 */ 57 58/* 59 * MAINTAINER: Matthew N. Dodd <winter@jurai.net> 60 * <mdodd@FreeBSD.org> 61 */ 62 63#include <sys/param.h> 64#include <sys/systm.h> 65#include <sys/mbuf.h> 66#include <sys/socket.h> 67#include <sys/sockio.h> 68#include <sys/bus.h> 69 70#include <machine/bus.h> 71#include <machine/resource.h> 72#include <sys/rman.h> 73 74#include <net/if.h> 75#include <net/if_arp.h> 76#include <net/if_media.h> 77#include <net/ethernet.h> 78#include <net/bpf.h> 79 80#include <dev/ep/if_epreg.h> 81#include <dev/ep/if_epvar.h> 82 83/* Exported variables */ 84devclass_t ep_devclass; 85 86static int ep_media2if_media[] = 87{IFM_10_T, IFM_10_5, IFM_NONE, IFM_10_2, IFM_NONE}; 88 89/* if functions */ 90static void epinit(void *); 91static int epioctl(struct ifnet *, u_long, caddr_t); 92static void epstart(struct ifnet *); 93static void epwatchdog(struct ifnet *); 94 95static void epstart_locked(struct ifnet *); 96static void epinit_locked(struct ep_softc *); 97 98/* if_media functions */ 99static int ep_ifmedia_upd(struct ifnet *); 100static void ep_ifmedia_sts(struct ifnet *, struct ifmediareq *); 101 102static void epstop(struct ep_softc *); 103static void epread(struct ep_softc *); 104static int eeprom_rdy(struct ep_softc *); 105 106#define EP_FTST(sc, f) (sc->stat & (f)) 107#define EP_FSET(sc, f) (sc->stat |= (f)) 108#define EP_FRST(sc, f) (sc->stat &= ~(f)) 109 110static int 111eeprom_rdy(struct ep_softc *sc) 112{ 113 int i; 114 115 for (i = 0; is_eeprom_busy(sc) && i < MAX_EEPROMBUSY; i++) 116 DELAY(100); 117 118 if (i >= MAX_EEPROMBUSY) { 119 printf("ep%d: eeprom failed to come ready.\n", sc->unit); 120 return (ENXIO); 121 } 122 123 return (0); 124} 125 126/* 127 * get_e: gets a 16 bits word from the EEPROM. we must have set the window 128 * before 129 */ 130int 131get_e(struct ep_softc *sc, u_int16_t offset, u_int16_t *result) 132{ 133 134 if (eeprom_rdy(sc)) 135 return (ENXIO); 136 137 CSR_WRITE_2(sc, EP_W0_EEPROM_COMMAND, 138 (EEPROM_CMD_RD << sc->epb.cmd_off) | offset); 139 140 if (eeprom_rdy(sc)) 141 return (ENXIO); 142 143 (*result) = CSR_READ_2(sc, EP_W0_EEPROM_DATA); 144 145 return (0); 146} 147 148int 149ep_get_macaddr(struct ep_softc *sc, u_char *addr) 150{ 151 int i; 152 u_int16_t result; 153 int error; 154 u_int16_t *macaddr; 155 156 macaddr = (u_int16_t *) addr; 157 158 GO_WINDOW(sc, 0); 159 for (i = EEPROM_NODE_ADDR_0; i <= EEPROM_NODE_ADDR_2; i++) { 160 error = get_e(sc, i, &result); 161 if (error) 162 return (error); 163 macaddr[i] = htons(result); 164 } 165 166 return (0); 167} 168 169int 170ep_alloc(device_t dev) 171{ 172 struct ep_softc *sc = device_get_softc(dev); 173 int rid; 174 int error = 0; 175 u_int16_t result; 176 177 rid = 0; 178 sc->iobase = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, 179 RF_ACTIVE); 180 if (!sc->iobase) { 181 device_printf(dev, "No I/O space?!\n"); 182 error = ENXIO; 183 goto bad; 184 } 185 rid = 0; 186 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE); 187 if (!sc->irq) { 188 device_printf(dev, "No irq?!\n"); 189 error = ENXIO; 190 goto bad; 191 } 192 sc->dev = dev; 193 sc->unit = device_get_unit(dev); 194 sc->stat = 0; /* 16 bit access */ 195 196 sc->bst = rman_get_bustag(sc->iobase); 197 sc->bsh = rman_get_bushandle(sc->iobase); 198 199 sc->ep_connectors = 0; 200 sc->ep_connector = 0; 201 202 GO_WINDOW(sc, 0); 203 sc->epb.cmd_off = 0; 204 205 error = get_e(sc, EEPROM_PROD_ID, &result); 206 if (error) 207 goto bad; 208 sc->epb.prod_id = result; 209 210 error = get_e(sc, EEPROM_RESOURCE_CFG, &result); 211 if (error) 212 goto bad; 213 sc->epb.res_cfg = result; 214 215bad: 216 if (error != 0) 217 ep_free(dev); 218 return (error); 219} 220 221void 222ep_get_media(struct ep_softc *sc) 223{ 224 u_int16_t config; 225 226 GO_WINDOW(sc, 0); 227 config = CSR_READ_2(sc, EP_W0_CONFIG_CTRL); 228 if (config & IS_AUI) 229 sc->ep_connectors |= AUI; 230 if (config & IS_BNC) 231 sc->ep_connectors |= BNC; 232 if (config & IS_UTP) 233 sc->ep_connectors |= UTP; 234 235 if (!(sc->ep_connectors & 7)) 236 if (bootverbose) 237 device_printf(sc->dev, "no connectors!\n"); 238 239 /* 240 * This works for most of the cards so we'll do it here. 241 * The cards that require something different can override 242 * this later on. 243 */ 244 sc->ep_connector = CSR_READ_2(sc, EP_W0_ADDRESS_CFG) >> ACF_CONNECTOR_BITS; 245} 246 247void 248ep_free(device_t dev) 249{ 250 struct ep_softc *sc = device_get_softc(dev); 251 252 if (sc->ep_intrhand) 253 bus_teardown_intr(dev, sc->irq, sc->ep_intrhand); 254 if (sc->iobase) 255 bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->iobase); 256 if (sc->irq) 257 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq); 258} 259 260int 261ep_attach(struct ep_softc *sc) 262{ 263 struct ifnet *ifp = NULL; 264 struct ifmedia *ifm = NULL; 265 u_short *p; 266 int i; 267 int attached; 268 int error; 269 270 sc->gone = 0; 271 EP_LOCK_INIT(sc); 272 error = ep_get_macaddr(sc, (u_char *)&sc->arpcom.ac_enaddr); 273 if (error) { 274 device_printf(sc->dev, "Unable to get Ethernet address!\n"); 275 EP_LOCK_DESTORY(sc); 276 return (ENXIO); 277 } 278 /* 279 * Setup the station address 280 */ 281 p = (u_short *)&sc->arpcom.ac_enaddr; 282 GO_WINDOW(sc, 2); 283 for (i = 0; i < 3; i++) 284 CSR_WRITE_2(sc, EP_W2_ADDR_0 + (i * 2), ntohs(p[i])); 285 286 ifp = &sc->arpcom.ac_if; 287 attached = (ifp->if_softc != 0); 288 289 ifp->if_softc = sc; 290 if_initname(ifp, device_get_name(sc->dev), device_get_unit(sc->dev)); 291 ifp->if_mtu = ETHERMTU; 292 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 293 ifp->if_output = ether_output; 294 ifp->if_start = epstart; 295 ifp->if_ioctl = epioctl; 296 ifp->if_watchdog = epwatchdog; 297 ifp->if_init = epinit; 298 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN; 299 300 if (!sc->epb.mii_trans) { 301 ifmedia_init(&sc->ifmedia, 0, ep_ifmedia_upd, ep_ifmedia_sts); 302 303 if (sc->ep_connectors & AUI) 304 ifmedia_add(&sc->ifmedia, 305 IFM_ETHER | IFM_10_5, 0, NULL); 306 if (sc->ep_connectors & UTP) 307 ifmedia_add(&sc->ifmedia, 308 IFM_ETHER | IFM_10_T, 0, NULL); 309 if (sc->ep_connectors & BNC) 310 ifmedia_add(&sc->ifmedia, 311 IFM_ETHER | IFM_10_2, 0, NULL); 312 if (!sc->ep_connectors) 313 ifmedia_add(&sc->ifmedia, 314 IFM_ETHER | IFM_NONE, 0, NULL); 315 316 ifmedia_set(&sc->ifmedia, 317 IFM_ETHER | ep_media2if_media[sc->ep_connector]); 318 319 ifm = &sc->ifmedia; 320 ifm->ifm_media = ifm->ifm_cur->ifm_media; 321 ep_ifmedia_upd(ifp); 322 } 323 if (!attached) 324 ether_ifattach(ifp, sc->arpcom.ac_enaddr); 325 326#ifdef EP_LOCAL_STATS 327 sc->rx_no_first = sc->rx_no_mbuf = sc->rx_bpf_disc = 328 sc->rx_overrunf = sc->rx_overrunl = sc->tx_underrun = 0; 329#endif 330 EP_FSET(sc, F_RX_FIRST); 331 sc->top = sc->mcur = 0; 332 333 epstop(sc); 334 335 return (0); 336} 337 338int 339ep_detach(device_t dev) 340{ 341 struct ep_softc *sc; 342 struct ifnet *ifp; 343 344 sc = device_get_softc(dev); 345 EP_ASSERT_UNLOCKED(sc); 346 ifp = &sc->arpcom.ac_if; 347 348 if (sc->gone) { 349 device_printf(dev, "already unloaded\n"); 350 return (0); 351 } 352 if (bus_child_present(dev)) 353 epstop(sc); 354 355 ifp->if_flags &= ~IFF_RUNNING; 356 ether_ifdetach(ifp); 357 358 sc->gone = 1; 359 ep_free(dev); 360 EP_LOCK_DESTORY(sc); 361 362 return (0); 363} 364 365static void 366epinit(void *xsc) 367{ 368 struct ep_softc *sc = xsc; 369 EP_LOCK(sc); 370 epinit_locked(sc); 371 EP_UNLOCK(sc); 372} 373 374/* 375 * The order in here seems important. Otherwise we may not receive 376 * interrupts. ?! 377 */ 378static void 379epinit_locked(struct ep_softc *sc) 380{ 381 struct ifnet *ifp = &sc->arpcom.ac_if; 382 int i; 383 384 if (sc->gone) 385 return; 386 387 EP_ASSERT_LOCKED(sc); 388 EP_BUSY_WAIT(sc); 389 390 GO_WINDOW(sc, 0); 391 CSR_WRITE_2(sc, EP_COMMAND, STOP_TRANSCEIVER); 392 GO_WINDOW(sc, 4); 393 CSR_WRITE_2(sc, EP_W4_MEDIA_TYPE, DISABLE_UTP); 394 GO_WINDOW(sc, 0); 395 396 /* Disable the card */ 397 CSR_WRITE_2(sc, EP_W0_CONFIG_CTRL, 0); 398 399 /* Enable the card */ 400 CSR_WRITE_2(sc, EP_W0_CONFIG_CTRL, ENABLE_DRQ_IRQ); 401 402 GO_WINDOW(sc, 2); 403 404 /* Reload the ether_addr. */ 405 for (i = 0; i < 6; i++) 406 CSR_WRITE_1(sc, EP_W2_ADDR_0 + i, sc->arpcom.ac_enaddr[i]); 407 408 CSR_WRITE_2(sc, EP_COMMAND, RX_RESET); 409 CSR_WRITE_2(sc, EP_COMMAND, TX_RESET); 410 EP_BUSY_WAIT(sc); 411 412 /* Window 1 is operating window */ 413 GO_WINDOW(sc, 1); 414 for (i = 0; i < 31; i++) 415 CSR_READ_1(sc, EP_W1_TX_STATUS); 416 417 /* get rid of stray intr's */ 418 CSR_WRITE_2(sc, EP_COMMAND, ACK_INTR | 0xff); 419 420 CSR_WRITE_2(sc, EP_COMMAND, SET_RD_0_MASK | S_5_INTS); 421 422 CSR_WRITE_2(sc, EP_COMMAND, SET_INTR_MASK | S_5_INTS); 423 424 if (ifp->if_flags & IFF_PROMISC) 425 CSR_WRITE_2(sc, EP_COMMAND, SET_RX_FILTER | FIL_INDIVIDUAL | 426 FIL_MULTICAST | FIL_BRDCST | FIL_PROMISC); 427 else 428 CSR_WRITE_2(sc, EP_COMMAND, SET_RX_FILTER | FIL_INDIVIDUAL | 429 FIL_MULTICAST | FIL_BRDCST); 430 431 if (!sc->epb.mii_trans) 432 ep_ifmedia_upd(ifp); 433 434 CSR_WRITE_2(sc, EP_COMMAND, RX_ENABLE); 435 CSR_WRITE_2(sc, EP_COMMAND, TX_ENABLE); 436 437 ifp->if_flags |= IFF_RUNNING; 438 ifp->if_flags &= ~IFF_OACTIVE; /* just in case */ 439 440#ifdef EP_LOCAL_STATS 441 sc->rx_no_first = sc->rx_no_mbuf = 442 sc->rx_overrunf = sc->rx_overrunl = sc->tx_underrun = 0; 443#endif 444 EP_FSET(sc, F_RX_FIRST); 445 if (sc->top) { 446 m_freem(sc->top); 447 sc->top = sc->mcur = 0; 448 } 449 CSR_WRITE_2(sc, EP_COMMAND, SET_RX_EARLY_THRESH | RX_INIT_EARLY_THRESH); 450 CSR_WRITE_2(sc, EP_COMMAND, SET_TX_START_THRESH | 16); 451 452 /* 453 * Store up a bunch of mbuf's for use later. (MAX_MBS). 454 * First we free up any that we had in case we're being 455 * called from intr or somewhere else. 456 */ 457 458 GO_WINDOW(sc, 1); 459 epstart_locked(ifp); 460} 461 462static void 463epstart(struct ifnet *ifp) 464{ 465 struct ep_softc *sc; 466 sc = ifp->if_softc; 467 EP_LOCK(sc); 468 epstart_locked(ifp); 469 EP_UNLOCK(sc); 470} 471 472static void 473epstart_locked(struct ifnet *ifp) 474{ 475 struct ep_softc *sc; 476 u_int len; 477 struct mbuf *m, *m0; 478 int pad; 479 480 sc = ifp->if_softc; 481 if (sc->gone) 482 return; 483 EP_ASSERT_LOCKED(sc); 484 EP_BUSY_WAIT(sc); 485 if (ifp->if_flags & IFF_OACTIVE) 486 return; 487startagain: 488 /* Sneak a peek at the next packet */ 489 IF_DEQUEUE(&ifp->if_snd, m0); 490 if (m0 == NULL) 491 return; 492 for (len = 0, m = m0; m != NULL; m = m->m_next) 493 len += m->m_len; 494 495 pad = (4 - len) & 3; 496 497 /* 498 * The 3c509 automatically pads short packets to minimum 499 * ethernet length, but we drop packets that are too large. 500 * Perhaps we should truncate them instead? 501 */ 502 if (len + pad > ETHER_MAX_LEN) { 503 /* packet is obviously too large: toss it */ 504 ifp->if_oerrors++; 505 m_freem(m0); 506 goto readcheck; 507 } 508 if (CSR_READ_2(sc, EP_W1_FREE_TX) < len + pad + 4) { 509 /* no room in FIFO */ 510 CSR_WRITE_2(sc, EP_COMMAND, SET_TX_AVAIL_THRESH | (len + pad + 4)); 511 /* make sure */ 512 if (CSR_READ_2(sc, EP_W1_FREE_TX) < len + pad + 4) { 513 ifp->if_flags |= IFF_OACTIVE; 514 IF_PREPEND(&ifp->if_snd, m0); 515 goto done; 516 } 517 } else 518 CSR_WRITE_2(sc, EP_COMMAND, 519 SET_TX_AVAIL_THRESH | EP_THRESH_DISABLE); 520 521 /* XXX 4.x and earlier would splhigh here */ 522 523 CSR_WRITE_2(sc, EP_W1_TX_PIO_WR_1, len); 524 /* Second dword meaningless */ 525 CSR_WRITE_2(sc, EP_W1_TX_PIO_WR_1, 0x0); 526 527 if (EP_FTST(sc, F_ACCESS_32_BITS)) { 528 for (m = m0; m != NULL; m = m->m_next) { 529 if (m->m_len > 3) 530 CSR_WRITE_MULTI_4(sc, EP_W1_TX_PIO_WR_1, 531 mtod(m, uint32_t *), m->m_len / 4); 532 if (m->m_len & 3) 533 CSR_WRITE_MULTI_1(sc, EP_W1_TX_PIO_WR_1, 534 mtod(m, uint8_t *)+(m->m_len & (~3)), 535 m->m_len & 3); 536 } 537 } else { 538 for (m = m0; m != NULL; m = m->m_next) { 539 if (m->m_len > 1) 540 CSR_WRITE_MULTI_2(sc, EP_W1_TX_PIO_WR_1, 541 mtod(m, uint16_t *), m->m_len / 2); 542 if (m->m_len & 1) 543 CSR_WRITE_1(sc, EP_W1_TX_PIO_WR_1, 544 *(mtod(m, uint8_t *)+m->m_len - 1)); 545 } 546 } 547 548 while (pad--) 549 CSR_WRITE_1(sc, EP_W1_TX_PIO_WR_1, 0); /* Padding */ 550 551 /* XXX and drop splhigh here */ 552 553 BPF_MTAP(ifp, m0); 554 555 ifp->if_timer = 2; 556 ifp->if_opackets++; 557 m_freem(m0); 558 559 /* 560 * Is another packet coming in? We don't want to overflow 561 * the tiny RX fifo. 562 */ 563readcheck: 564 if (CSR_READ_2(sc, EP_W1_RX_STATUS) & RX_BYTES_MASK) { 565 /* 566 * we check if we have packets left, in that case 567 * we prepare to come back later 568 */ 569 if (ifp->if_snd.ifq_head) 570 CSR_WRITE_2(sc, EP_COMMAND, SET_TX_AVAIL_THRESH | 8); 571 goto done; 572 } 573 goto startagain; 574done:; 575 return; 576} 577 578void 579ep_intr(void *arg) 580{ 581 struct ep_softc *sc; 582 int status; 583 struct ifnet *ifp; 584 585 sc = (struct ep_softc *) arg; 586 EP_LOCK(sc); 587 /* XXX 4.x splbio'd here to reduce interruptability */ 588 589 /* 590 * quick fix: Try to detect an interrupt when the card goes away. 591 */ 592 if (sc->gone || CSR_READ_2(sc, EP_STATUS) == 0xffff) { 593 EP_UNLOCK(sc); 594 return; 595 } 596 ifp = &sc->arpcom.ac_if; 597 598 CSR_WRITE_2(sc, EP_COMMAND, SET_INTR_MASK); /* disable all Ints */ 599 600rescan: 601 602 while ((status = CSR_READ_2(sc, EP_STATUS)) & S_5_INTS) { 603 604 /* first acknowledge all interrupt sources */ 605 CSR_WRITE_2(sc, EP_COMMAND, ACK_INTR | (status & S_MASK)); 606 607 if (status & (S_RX_COMPLETE | S_RX_EARLY)) 608 epread(sc); 609 if (status & S_TX_AVAIL) { 610 /* we need ACK */ 611 ifp->if_timer = 0; 612 ifp->if_flags &= ~IFF_OACTIVE; 613 GO_WINDOW(sc, 1); 614 CSR_READ_2(sc, EP_W1_FREE_TX); 615 epstart_locked(ifp); 616 } 617 if (status & S_CARD_FAILURE) { 618 ifp->if_timer = 0; 619#ifdef EP_LOCAL_STATS 620 printf("\nep%d:\n\tStatus: %x\n", sc->unit, status); 621 GO_WINDOW(sc, 4); 622 printf("\tFIFO Diagnostic: %x\n", 623 CSR_READ_2(sc, EP_W4_FIFO_DIAG)); 624 printf("\tStat: %x\n", sc->stat); 625 printf("\tIpackets=%d, Opackets=%d\n", 626 ifp->if_ipackets, ifp->if_opackets); 627 printf("\tNOF=%d, NOMB=%d, RXOF=%d, RXOL=%d, TXU=%d\n", 628 sc->rx_no_first, sc->rx_no_mbuf, sc->rx_overrunf, 629 sc->rx_overrunl, sc->tx_underrun); 630#else 631 632#ifdef DIAGNOSTIC 633 printf("ep%d: Status: %x (input buffer overflow)\n", 634 sc->unit, status); 635#else 636 ++ifp->if_ierrors; 637#endif 638 639#endif 640 epinit_locked(sc); 641 EP_UNLOCK(sc); 642 return; 643 } 644 if (status & S_TX_COMPLETE) { 645 ifp->if_timer = 0; 646 /* 647 * We need ACK. We do it at the end. 648 * 649 * We need to read TX_STATUS until we get a 650 * 0 status in order to turn off the interrupt flag. 651 */ 652 while ((status = CSR_READ_1(sc, EP_W1_TX_STATUS)) & 653 TXS_COMPLETE) { 654 if (status & TXS_SUCCES_INTR_REQ); 655 else if (status & 656 (TXS_UNDERRUN | TXS_JABBER | 657 TXS_MAX_COLLISION)) { 658 CSR_WRITE_2(sc, EP_COMMAND, TX_RESET); 659 if (status & TXS_UNDERRUN) { 660#ifdef EP_LOCAL_STATS 661 sc->tx_underrun++; 662#endif 663 } else { 664 if (status & TXS_JABBER); 665 else 666 ++ifp->if_collisions; 667 /* TXS_MAX_COLLISION 668 * we shouldn't get 669 * here 670 */ 671 } 672 ++ifp->if_oerrors; 673 CSR_WRITE_2(sc, EP_COMMAND, TX_ENABLE); 674 /* 675 * To have a tx_avail_int but giving 676 * the chance to the Reception 677 */ 678 if (ifp->if_snd.ifq_head) 679 CSR_WRITE_2(sc, EP_COMMAND, 680 SET_TX_AVAIL_THRESH | 8); 681 } 682 /* pops up the next status */ 683 CSR_WRITE_1(sc, EP_W1_TX_STATUS, 0x0); 684 } /* while */ 685 ifp->if_flags &= ~IFF_OACTIVE; 686 GO_WINDOW(sc, 1); 687 CSR_READ_2(sc, EP_W1_FREE_TX); 688 epstart_locked(ifp); 689 } /* end TX_COMPLETE */ 690 } 691 692 CSR_WRITE_2(sc, EP_COMMAND, C_INTR_LATCH); /* ACK int Latch */ 693 694 if ((status = CSR_READ_2(sc, EP_STATUS)) & S_5_INTS) 695 goto rescan; 696 697 /* re-enable Ints */ 698 CSR_WRITE_2(sc, EP_COMMAND, SET_INTR_MASK | S_5_INTS); 699 EP_UNLOCK(sc); 700} 701 702static void 703epread(struct ep_softc *sc) 704{ 705 struct mbuf *top, *mcur, *m; 706 struct ifnet *ifp; 707 int lenthisone; 708 short rx_fifo2, status; 709 short rx_fifo; 710 711/* XXX Must be called with sc locked */ 712 713 ifp = &sc->arpcom.ac_if; 714 status = CSR_READ_2(sc, EP_W1_RX_STATUS); 715 716read_again: 717 718 if (status & ERR_RX) { 719 ++ifp->if_ierrors; 720 if (status & ERR_RX_OVERRUN) { 721 /* 722 * We can think the rx latency is actually 723 * greather than we expect 724 */ 725#ifdef EP_LOCAL_STATS 726 if (EP_FTST(sc, F_RX_FIRST)) 727 sc->rx_overrunf++; 728 else 729 sc->rx_overrunl++; 730#endif 731 } 732 goto out; 733 } 734 rx_fifo = rx_fifo2 = status & RX_BYTES_MASK; 735 736 if (EP_FTST(sc, F_RX_FIRST)) { 737 MGETHDR(m, M_DONTWAIT, MT_DATA); 738 if (!m) 739 goto out; 740 if (rx_fifo >= MINCLSIZE) 741 MCLGET(m, M_DONTWAIT); 742 sc->top = sc->mcur = top = m; 743#define EROUND ((sizeof(struct ether_header) + 3) & ~3) 744#define EOFF (EROUND - sizeof(struct ether_header)) 745 top->m_data += EOFF; 746 747 /* Read what should be the header. */ 748 CSR_READ_MULTI_2(sc, EP_W1_RX_PIO_RD_1, 749 mtod(top, uint16_t *), sizeof(struct ether_header) / 2); 750 top->m_len = sizeof(struct ether_header); 751 rx_fifo -= sizeof(struct ether_header); 752 sc->cur_len = rx_fifo2; 753 } else { 754 /* come here if we didn't have a complete packet last time */ 755 top = sc->top; 756 m = sc->mcur; 757 sc->cur_len += rx_fifo2; 758 } 759 760 /* Reads what is left in the RX FIFO */ 761 while (rx_fifo > 0) { 762 lenthisone = min(rx_fifo, M_TRAILINGSPACE(m)); 763 if (lenthisone == 0) { /* no room in this one */ 764 mcur = m; 765 MGET(m, M_DONTWAIT, MT_DATA); 766 if (!m) 767 goto out; 768 if (rx_fifo >= MINCLSIZE) 769 MCLGET(m, M_DONTWAIT); 770 m->m_len = 0; 771 mcur->m_next = m; 772 lenthisone = min(rx_fifo, M_TRAILINGSPACE(m)); 773 } 774 if (EP_FTST(sc, F_ACCESS_32_BITS)) { 775 /* default for EISA configured cards */ 776 CSR_READ_MULTI_4(sc, EP_W1_RX_PIO_RD_1, 777 (uint32_t *)(mtod(m, caddr_t)+m->m_len), 778 lenthisone / 4); 779 m->m_len += (lenthisone & ~3); 780 if (lenthisone & 3) 781 CSR_READ_MULTI_1(sc, EP_W1_RX_PIO_RD_1, 782 mtod(m, caddr_t)+m->m_len, lenthisone & 3); 783 m->m_len += (lenthisone & 3); 784 } else { 785 CSR_READ_MULTI_2(sc, EP_W1_RX_PIO_RD_1, 786 (uint16_t *)(mtod(m, caddr_t)+m->m_len), 787 lenthisone / 2); 788 m->m_len += lenthisone; 789 if (lenthisone & 1) 790 *(mtod(m, caddr_t)+m->m_len - 1) = 791 CSR_READ_1(sc, EP_W1_RX_PIO_RD_1); 792 } 793 rx_fifo -= lenthisone; 794 } 795 796 if (status & ERR_RX_INCOMPLETE) { 797 /* we haven't received the complete packet */ 798 sc->mcur = m; 799#ifdef EP_LOCAL_STATS 800 /* to know how often we come here */ 801 sc->rx_no_first++; 802#endif 803 EP_FRST(sc, F_RX_FIRST); 804 status = CSR_READ_2(sc, EP_W1_RX_STATUS); 805 if (!status & ERR_RX_INCOMPLETE) { 806 /* 807 * We see if by now, the packet has completly 808 * arrived 809 */ 810 goto read_again; 811 } 812 CSR_WRITE_2(sc, EP_COMMAND, 813 SET_RX_EARLY_THRESH | RX_NEXT_EARLY_THRESH); 814 return; 815 } 816 CSR_WRITE_2(sc, EP_COMMAND, RX_DISCARD_TOP_PACK); 817 ++ifp->if_ipackets; 818 EP_FSET(sc, F_RX_FIRST); 819 top->m_pkthdr.rcvif = &sc->arpcom.ac_if; 820 top->m_pkthdr.len = sc->cur_len; 821 822 /* 823 * Drop locks before calling if_input() since it may re-enter 824 * ep_start() in the netisr case. This would result in a 825 * lock reversal. Better performance might be obtained by 826 * chaining all packets received, dropping the lock, and then 827 * calling if_input() on each one. 828 */ 829 EP_UNLOCK(sc); 830 (*ifp->if_input) (ifp, top); 831 EP_LOCK(sc); 832 sc->top = 0; 833 EP_BUSY_WAIT(sc); 834 CSR_WRITE_2(sc, EP_COMMAND, SET_RX_EARLY_THRESH | RX_INIT_EARLY_THRESH); 835 return; 836 837out: 838 CSR_WRITE_2(sc, EP_COMMAND, RX_DISCARD_TOP_PACK); 839 if (sc->top) { 840 m_freem(sc->top); 841 sc->top = 0; 842#ifdef EP_LOCAL_STATS 843 sc->rx_no_mbuf++; 844#endif 845 } 846 EP_FSET(sc, F_RX_FIRST); 847 EP_BUSY_WAIT(sc); 848 CSR_WRITE_2(sc, EP_COMMAND, SET_RX_EARLY_THRESH | RX_INIT_EARLY_THRESH); 849} 850 851static int 852ep_ifmedia_upd(struct ifnet *ifp) 853{ 854 struct ep_softc *sc = ifp->if_softc; 855 int i = 0, j; 856 857 GO_WINDOW(sc, 0); 858 CSR_WRITE_2(sc, EP_COMMAND, STOP_TRANSCEIVER); 859 GO_WINDOW(sc, 4); 860 CSR_WRITE_2(sc, EP_W4_MEDIA_TYPE, DISABLE_UTP); 861 GO_WINDOW(sc, 0); 862 863 switch (IFM_SUBTYPE(sc->ifmedia.ifm_media)) { 864 case IFM_10_T: 865 if (sc->ep_connectors & UTP) { 866 i = ACF_CONNECTOR_UTP; 867 GO_WINDOW(sc, 4); 868 CSR_WRITE_2(sc, EP_W4_MEDIA_TYPE, ENABLE_UTP); 869 } 870 break; 871 case IFM_10_2: 872 if (sc->ep_connectors & BNC) { 873 i = ACF_CONNECTOR_BNC; 874 CSR_WRITE_2(sc, EP_COMMAND, START_TRANSCEIVER); 875 DELAY(DELAY_MULTIPLE * 1000); 876 } 877 break; 878 case IFM_10_5: 879 if (sc->ep_connectors & AUI) 880 i = ACF_CONNECTOR_AUI; 881 break; 882 default: 883 i = sc->ep_connector; 884 device_printf(sc->dev, 885 "strange connector type in EEPROM: assuming AUI\n"); 886 } 887 888 GO_WINDOW(sc, 0); 889 j = CSR_READ_2(sc, EP_W0_ADDRESS_CFG) & 0x3fff; 890 CSR_WRITE_2(sc, EP_W0_ADDRESS_CFG, j | (i << ACF_CONNECTOR_BITS)); 891 892 return (0); 893} 894 895static void 896ep_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 897{ 898 struct ep_softc *sc = ifp->if_softc; 899 900 ifmr->ifm_active = sc->ifmedia.ifm_media; 901} 902 903static int 904epioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 905{ 906 struct ep_softc *sc = ifp->if_softc; 907 struct ifreq *ifr = (struct ifreq *) data; 908 int error = 0; 909 910 switch (cmd) { 911 case SIOCSIFFLAGS: 912 EP_LOCK(sc); 913 if (((ifp->if_flags & IFF_UP) == 0) && 914 (ifp->if_flags & IFF_RUNNING)) { 915 ifp->if_flags &= ~IFF_RUNNING; 916 epstop(sc); 917 } else 918 /* reinitialize card on any parameter change */ 919 epinit_locked(sc); 920 EP_UNLOCK(sc); 921 break; 922#ifdef notdef 923 case SIOCGHWADDR: 924 bcopy((caddr_t)sc->sc_addr, (caddr_t)&ifr->ifr_data, 925 sizeof(sc->sc_addr)); 926 break; 927#endif 928 case SIOCADDMULTI: 929 case SIOCDELMULTI: 930 /* 931 * The Etherlink III has no programmable multicast 932 * filter. We always initialize the card to be 933 * promiscuous to multicast, since we're always a 934 * member of the ALL-SYSTEMS group, so there's no 935 * need to process SIOC*MULTI requests. 936 */ 937 error = 0; 938 break; 939 case SIOCSIFMEDIA: 940 case SIOCGIFMEDIA: 941 if (!sc->epb.mii_trans) 942 error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, cmd); 943 else 944 error = EINVAL; 945 break; 946 default: 947 error = ether_ioctl(ifp, cmd, data); 948 break; 949 } 950 return (error); 951} 952 953static void 954epwatchdog(struct ifnet *ifp) 955{ 956 struct ep_softc *sc = ifp->if_softc; 957 958 if (sc->gone) 959 return; 960 ifp->if_flags &= ~IFF_OACTIVE; 961 epstart(ifp); 962 ep_intr(ifp->if_softc); 963} 964 965static void 966epstop(struct ep_softc *sc) 967{ 968 if (sc->gone) 969 return; 970 CSR_WRITE_2(sc, EP_COMMAND, RX_DISABLE); 971 CSR_WRITE_2(sc, EP_COMMAND, RX_DISCARD_TOP_PACK); 972 EP_BUSY_WAIT(sc); 973 974 CSR_WRITE_2(sc, EP_COMMAND, TX_DISABLE); 975 CSR_WRITE_2(sc, EP_COMMAND, STOP_TRANSCEIVER); 976 DELAY(800); 977 978 CSR_WRITE_2(sc, EP_COMMAND, RX_RESET); 979 EP_BUSY_WAIT(sc); 980 CSR_WRITE_2(sc, EP_COMMAND, TX_RESET); 981 EP_BUSY_WAIT(sc); 982 983 CSR_WRITE_2(sc, EP_COMMAND, C_INTR_LATCH); 984 CSR_WRITE_2(sc, EP_COMMAND, SET_RD_0_MASK); 985 CSR_WRITE_2(sc, EP_COMMAND, SET_INTR_MASK); 986 CSR_WRITE_2(sc, EP_COMMAND, SET_RX_FILTER); 987} 988