Home | History | Annotate | Line # | Download | only in ic
elink3.c revision 1.14
      1  1.14       cjs /*	$NetBSD: elink3.c,v 1.14 1996/12/07 08:33:07 cjs Exp $	*/
      2   1.1   thorpej 
      3   1.1   thorpej /*
      4   1.6   thorpej  * Copyright (c) 1994 Herb Peyerl <hpeyerl (at) beer.org>
      5   1.1   thorpej  * All rights reserved.
      6   1.1   thorpej  *
      7   1.1   thorpej  * Redistribution and use in source and binary forms, with or without
      8   1.1   thorpej  * modification, are permitted provided that the following conditions
      9   1.1   thorpej  * are met:
     10   1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     11   1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     12   1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     14   1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     15   1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     16   1.1   thorpej  *    must display the following acknowledgement:
     17   1.1   thorpej  *      This product includes software developed by Herb Peyerl.
     18   1.1   thorpej  * 4. The name of Herb Peyerl may not be used to endorse or promote products
     19   1.1   thorpej  *    derived from this software without specific prior written permission.
     20   1.1   thorpej  *
     21   1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22   1.1   thorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23   1.1   thorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24   1.1   thorpej  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25   1.1   thorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26   1.1   thorpej  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27   1.1   thorpej  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28   1.1   thorpej  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29   1.1   thorpej  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30   1.1   thorpej  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31   1.1   thorpej  */
     32   1.1   thorpej 
     33   1.1   thorpej #include "bpfilter.h"
     34   1.1   thorpej 
     35   1.1   thorpej #include <sys/param.h>
     36   1.3  christos #include <sys/systm.h>
     37   1.1   thorpej #include <sys/mbuf.h>
     38   1.1   thorpej #include <sys/socket.h>
     39   1.1   thorpej #include <sys/ioctl.h>
     40   1.1   thorpej #include <sys/errno.h>
     41   1.1   thorpej #include <sys/syslog.h>
     42   1.1   thorpej #include <sys/select.h>
     43   1.1   thorpej #include <sys/device.h>
     44   1.1   thorpej 
     45   1.1   thorpej #include <net/if.h>
     46   1.1   thorpej #include <net/netisr.h>
     47   1.1   thorpej #include <net/if_dl.h>
     48   1.1   thorpej #include <net/if_types.h>
     49   1.1   thorpej #include <net/netisr.h>
     50   1.1   thorpej 
     51   1.1   thorpej #ifdef INET
     52   1.1   thorpej #include <netinet/in.h>
     53   1.1   thorpej #include <netinet/in_systm.h>
     54   1.1   thorpej #include <netinet/in_var.h>
     55   1.1   thorpej #include <netinet/ip.h>
     56   1.1   thorpej #include <netinet/if_ether.h>
     57   1.1   thorpej #endif
     58   1.1   thorpej 
     59   1.1   thorpej #ifdef NS
     60   1.1   thorpej #include <netns/ns.h>
     61   1.1   thorpej #include <netns/ns_if.h>
     62   1.1   thorpej #endif
     63   1.1   thorpej 
     64   1.1   thorpej #if NBPFILTER > 0
     65   1.1   thorpej #include <net/bpf.h>
     66   1.1   thorpej #include <net/bpfdesc.h>
     67   1.1   thorpej #endif
     68   1.1   thorpej 
     69   1.1   thorpej #include <machine/cpu.h>
     70   1.2   thorpej #include <machine/bus.h>
     71   1.7   thorpej #include <machine/intr.h>
     72   1.1   thorpej 
     73   1.1   thorpej #include <dev/ic/elink3var.h>
     74   1.1   thorpej #include <dev/ic/elink3reg.h>
     75   1.1   thorpej 
     76   1.1   thorpej #define ETHER_MIN_LEN	64
     77   1.1   thorpej #define ETHER_MAX_LEN   1518
     78   1.1   thorpej #define ETHER_ADDR_LEN  6
     79   1.1   thorpej 
     80   1.1   thorpej struct cfdriver ep_cd = {
     81   1.1   thorpej 	NULL, "ep", DV_IFNET
     82   1.1   thorpej };
     83   1.1   thorpej 
     84   1.3  christos static void eptxstat __P((struct ep_softc *));
     85   1.1   thorpej static int epstatus __P((struct ep_softc *));
     86   1.1   thorpej void epinit __P((struct ep_softc *));
     87   1.1   thorpej int epioctl __P((struct ifnet *, u_long, caddr_t));
     88   1.1   thorpej void epstart __P((struct ifnet *));
     89   1.5   thorpej void epwatchdog __P((struct ifnet *));
     90   1.1   thorpej void epreset __P((struct ep_softc *));
     91   1.1   thorpej void epread __P((struct ep_softc *));
     92   1.1   thorpej struct mbuf *epget __P((struct ep_softc *, int));
     93   1.3  christos void epmbuffill __P((void *));
     94   1.1   thorpej void epmbufempty __P((struct ep_softc *));
     95   1.1   thorpej void epsetfilter __P((struct ep_softc *));
     96   1.1   thorpej void epsetlink __P((struct ep_softc *));
     97   1.1   thorpej 
     98   1.1   thorpej static int epbusyeeprom __P((struct ep_softc *));
     99   1.1   thorpej 
    100   1.1   thorpej void
    101   1.1   thorpej epconfig(sc, conn)
    102   1.1   thorpej 	struct ep_softc *sc;
    103   1.7   thorpej 	u_int16_t conn;
    104   1.1   thorpej {
    105   1.5   thorpej 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    106  1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    107  1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    108   1.7   thorpej 	u_int16_t i;
    109  1.14       cjs 	u_int32_t r;
    110   1.1   thorpej 
    111  1.14       cjs 	printf("%s: ", sc->sc_dev.dv_xname);
    112  1.14       cjs 
    113  1.14       cjs 	/* print RAM size */
    114  1.14       cjs 	GO_WINDOW(3);
    115  1.14       cjs 	r = bus_space_read_2(iot, ioh, EP_W3_INTERNAL_CONFIG);
    116  1.14       cjs 	printf("%dKB %s-wide RAM, ", 8 << (r & 0x07),
    117  1.14       cjs 	    (r & 0x08) ? "word" : "byte");
    118  1.14       cjs 	GO_WINDOW(0);
    119  1.14       cjs 
    120  1.14       cjs 	/* determine connectors available */
    121   1.1   thorpej 	sc->ep_connectors = 0;
    122   1.1   thorpej 	if (conn & IS_AUI) {
    123  1.10  christos 		printf("aui");
    124   1.1   thorpej 		sc->ep_connectors |= AUI;
    125   1.1   thorpej 	}
    126   1.1   thorpej 	if (conn & IS_BNC) {
    127   1.1   thorpej 		if (sc->ep_connectors)
    128  1.10  christos 			printf("/");
    129  1.10  christos 		printf("bnc");
    130   1.1   thorpej 		sc->ep_connectors |= BNC;
    131   1.1   thorpej 	}
    132   1.1   thorpej 	if (conn & IS_UTP) {
    133   1.1   thorpej 		if (sc->ep_connectors)
    134  1.10  christos 			printf("/");
    135  1.10  christos 		printf("utp");
    136   1.1   thorpej 		sc->ep_connectors |= UTP;
    137   1.1   thorpej 	}
    138   1.1   thorpej 	if (!sc->ep_connectors)
    139  1.10  christos 		printf("no connectors!");
    140   1.1   thorpej 
    141   1.1   thorpej 	/*
    142   1.1   thorpej 	 * Read the station address from the eeprom
    143   1.1   thorpej 	 */
    144   1.1   thorpej 	for (i = 0; i < 3; i++) {
    145   1.7   thorpej 		u_int16_t x;
    146   1.1   thorpej 		if (epbusyeeprom(sc))
    147   1.1   thorpej 			return;
    148  1.11   thorpej 		bus_space_write_2(iot, ioh, EP_W0_EEPROM_COMMAND,
    149  1.11   thorpej 		    READ_EEPROM | i);
    150   1.1   thorpej 		if (epbusyeeprom(sc))
    151   1.1   thorpej 			return;
    152  1.11   thorpej 		x = bus_space_read_2(iot, ioh, EP_W0_EEPROM_DATA);
    153   1.1   thorpej 		sc->sc_arpcom.ac_enaddr[(i << 1)] = x >> 8;
    154   1.1   thorpej 		sc->sc_arpcom.ac_enaddr[(i << 1) + 1] = x;
    155   1.1   thorpej 	}
    156   1.1   thorpej 
    157  1.14       cjs 	printf(", address %s\n", ether_sprintf(sc->sc_arpcom.ac_enaddr));
    158   1.1   thorpej 
    159  1.12  jonathan 	/*
    160  1.12  jonathan 	 * Vortex-based (3c59x, eisa)? and Boomerang (3c900)cards allow
    161  1.12  jonathan 	 * FDDI-sized (4500) byte packets.  Commands only take an 11-bit
    162  1.12  jonathan 	 * parameter, and  11 bits isn't enough to hold a full-size pkt length.
    163  1.12  jonathan 	 * Commands to these cards implicitly upshift a packet size
    164  1.12  jonathan 	 * or threshold by 2 bits.
    165  1.12  jonathan 	 * To detect  cards with large-packet support, we probe by setting
    166  1.12  jonathan 	 * the transmit threshold register, then change windows and
    167  1.12  jonathan 	 * read back the threshold register directly, and see if the
    168  1.12  jonathan 	 * threshold value was shifted or not.
    169  1.12  jonathan 	 */
    170  1.12  jonathan 	bus_space_write_2(iot, ioh, EP_COMMAND,
    171  1.13  jonathan 			  SET_TX_AVAIL_THRESH | EP_LARGEWIN_PROBE );
    172  1.12  jonathan 	GO_WINDOW(5);
    173  1.12  jonathan 	i = bus_space_read_2(iot, ioh, EP_W5_TX_AVAIL_THRESH);
    174  1.12  jonathan 	GO_WINDOW(1);
    175  1.12  jonathan 	switch (i)  {
    176  1.13  jonathan 	case EP_LARGEWIN_PROBE:
    177  1.13  jonathan 	case (EP_LARGEWIN_PROBE & EP_LARGEWIN_MASK):
    178  1.12  jonathan 		sc->ep_pktlenshift = 0;
    179  1.12  jonathan 		break;
    180  1.12  jonathan 
    181  1.13  jonathan 	case (EP_LARGEWIN_PROBE << 2):
    182  1.12  jonathan 		sc->ep_pktlenshift = 2;
    183  1.12  jonathan 		break;
    184  1.12  jonathan 
    185  1.12  jonathan 	default:
    186  1.14       cjs 		printf("%s: wrote %d to TX_AVAIL_THRESH, read back %d. "
    187  1.14       cjs 		    "Interface disabled\n",
    188  1.12  jonathan 		    sc->sc_dev.dv_xname, EP_THRESH_DISABLE, (int) i);
    189  1.12  jonathan 		return;
    190  1.12  jonathan 	}
    191  1.12  jonathan 	/*
    192  1.12  jonathan 	 * Ensure Tx-available interrupts are enabled for
    193  1.12  jonathan 	 * start the interface.
    194  1.12  jonathan 	 * XXX should be in epinit().
    195  1.12  jonathan 	 */
    196  1.12  jonathan 	bus_space_write_2(iot, ioh, EP_COMMAND,
    197  1.12  jonathan 	    SET_TX_AVAIL_THRESH | (1600 >> sc->ep_pktlenshift));
    198  1.12  jonathan 
    199   1.5   thorpej 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    200   1.5   thorpej 	ifp->if_softc = sc;
    201   1.1   thorpej 	ifp->if_start = epstart;
    202   1.1   thorpej 	ifp->if_ioctl = epioctl;
    203   1.1   thorpej 	ifp->if_watchdog = epwatchdog;
    204   1.1   thorpej 	ifp->if_flags =
    205   1.1   thorpej 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    206   1.1   thorpej 
    207   1.1   thorpej 	if_attach(ifp);
    208   1.1   thorpej 	ether_ifattach(ifp);
    209   1.1   thorpej 
    210   1.1   thorpej #if NBPFILTER > 0
    211   1.1   thorpej 	bpfattach(&sc->sc_arpcom.ac_if.if_bpf, ifp, DLT_EN10MB,
    212   1.1   thorpej 		  sizeof(struct ether_header));
    213   1.1   thorpej #endif
    214   1.1   thorpej 
    215   1.1   thorpej 	sc->tx_start_thresh = 20;	/* probably a good starting point. */
    216  1.12  jonathan 
    217  1.12  jonathan #if 0
    218  1.12  jonathan 	/* XXX */
    219  1.12  jonathan 	bus_space_write_2(iot, ioh, EP_COMMAND, RX_RESET);
    220  1.12  jonathan 	bus_space_write_2(iot, ioh, EP_COMMAND, TX_RESET);
    221  1.12  jonathan #else
    222  1.12  jonathan 
    223  1.12  jonathan 	epinit(sc);		/*XXX fix up after probe */
    224  1.12  jonathan 	DELAY(20000);
    225  1.12  jonathan 	epstop(sc);		/*XXX reset after probe, stop interface. */
    226  1.12  jonathan 	DELAY(20000);
    227  1.12  jonathan #endif
    228   1.1   thorpej }
    229   1.1   thorpej 
    230   1.1   thorpej /*
    231   1.1   thorpej  * The order in here seems important. Otherwise we may not receive
    232   1.1   thorpej  * interrupts. ?!
    233   1.1   thorpej  */
    234   1.1   thorpej void
    235   1.1   thorpej epinit(sc)
    236   1.1   thorpej 	register struct ep_softc *sc;
    237   1.1   thorpej {
    238   1.1   thorpej 	register struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    239  1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    240  1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    241   1.1   thorpej 	int i;
    242   1.1   thorpej 
    243  1.11   thorpej 	while (bus_space_read_2(iot, ioh, EP_STATUS) & S_COMMAND_IN_PROGRESS)
    244   1.1   thorpej 		;
    245   1.1   thorpej 
    246   1.1   thorpej 	if (sc->bustype != EP_BUS_PCI) {
    247   1.1   thorpej 		GO_WINDOW(0);
    248  1.11   thorpej 		bus_space_write_2(iot, ioh, EP_W0_CONFIG_CTRL, 0);
    249  1.11   thorpej 		bus_space_write_2(iot, ioh, EP_W0_CONFIG_CTRL, ENABLE_DRQ_IRQ);
    250   1.1   thorpej 	}
    251   1.1   thorpej 
    252   1.1   thorpej 	if (sc->bustype == EP_BUS_PCMCIA) {
    253   1.1   thorpej #ifdef EP_COAX_DEFAULT
    254  1.11   thorpej 		bus_space_write_2(iot, ioh, EP_W0_ADDRESS_CFG,3<<14);
    255   1.1   thorpej #else
    256  1.11   thorpej 		bus_space_write_2(iot, ioh, EP_W0_ADDRESS_CFG,0<<14);
    257   1.1   thorpej #endif
    258  1.11   thorpej 		bus_space_write_2(iot, ioh, EP_W0_RESOURCE_CFG, 0x3f00);
    259   1.1   thorpej 	}
    260   1.1   thorpej 
    261   1.1   thorpej 	GO_WINDOW(2);
    262   1.1   thorpej 	for (i = 0; i < 6; i++)	/* Reload the ether_addr. */
    263  1.11   thorpej 		bus_space_write_1(iot, ioh, EP_W2_ADDR_0 + i,
    264   1.2   thorpej 		    sc->sc_arpcom.ac_enaddr[i]);
    265   1.8  christos 
    266  1.12  jonathan 	/*
    267  1.12  jonathan 	 * Reset the station-address receive filter.
    268  1.12  jonathan 	 * A bug workaround for busmastering  (Vortex, Demon) cards.
    269  1.12  jonathan 	 */
    270  1.12  jonathan 	for (i = 0; i < 6; i++)
    271  1.12  jonathan 		bus_space_write_1(iot, ioh, EP_W2_RECVMASK_0 + i, 0);
    272   1.1   thorpej 
    273  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, RX_RESET);
    274  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, TX_RESET);
    275   1.1   thorpej 
    276   1.1   thorpej 	GO_WINDOW(1);		/* Window 1 is operating window */
    277   1.1   thorpej 	for (i = 0; i < 31; i++)
    278  1.11   thorpej 		bus_space_read_1(iot, ioh, EP_W1_TX_STATUS);
    279   1.1   thorpej 
    280  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, SET_RD_0_MASK | S_CARD_FAILURE |
    281   1.1   thorpej 				S_RX_COMPLETE | S_TX_COMPLETE | S_TX_AVAIL);
    282  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, SET_INTR_MASK | S_CARD_FAILURE |
    283   1.1   thorpej 				S_RX_COMPLETE | S_TX_COMPLETE | S_TX_AVAIL);
    284   1.1   thorpej 
    285   1.1   thorpej 	/*
    286   1.1   thorpej 	 * Attempt to get rid of any stray interrupts that occured during
    287   1.1   thorpej 	 * configuration.  On the i386 this isn't possible because one may
    288   1.1   thorpej 	 * already be queued.  However, a single stray interrupt is
    289   1.1   thorpej 	 * unimportant.
    290   1.1   thorpej 	 */
    291  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, ACK_INTR | 0xff);
    292   1.1   thorpej 
    293   1.1   thorpej 	epsetfilter(sc);
    294   1.1   thorpej 	epsetlink(sc);
    295   1.1   thorpej 
    296  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, RX_ENABLE);
    297  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, TX_ENABLE);
    298   1.1   thorpej 
    299   1.1   thorpej 	epmbuffill(sc);
    300   1.1   thorpej 
    301   1.1   thorpej 	/* Interface is now `running', with no output active. */
    302   1.1   thorpej 	ifp->if_flags |= IFF_RUNNING;
    303   1.1   thorpej 	ifp->if_flags &= ~IFF_OACTIVE;
    304   1.1   thorpej 
    305   1.1   thorpej 	/* Attempt to start output, if any. */
    306   1.1   thorpej 	epstart(ifp);
    307   1.1   thorpej }
    308   1.1   thorpej 
    309   1.1   thorpej void
    310   1.1   thorpej epsetfilter(sc)
    311   1.1   thorpej 	register struct ep_softc *sc;
    312   1.1   thorpej {
    313   1.1   thorpej 	register struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    314   1.1   thorpej 
    315   1.1   thorpej 	GO_WINDOW(1);		/* Window 1 is operating window */
    316  1.11   thorpej 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, EP_COMMAND, SET_RX_FILTER |
    317   1.1   thorpej 	    FIL_INDIVIDUAL | FIL_BRDCST |
    318   1.1   thorpej 	    ((ifp->if_flags & IFF_MULTICAST) ? FIL_MULTICAST : 0 ) |
    319   1.1   thorpej 	    ((ifp->if_flags & IFF_PROMISC) ? FIL_PROMISC : 0 ));
    320   1.1   thorpej }
    321   1.1   thorpej 
    322   1.1   thorpej void
    323   1.1   thorpej epsetlink(sc)
    324   1.1   thorpej 	register struct ep_softc *sc;
    325   1.1   thorpej {
    326   1.1   thorpej 	register struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    327  1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    328  1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    329   1.1   thorpej 
    330   1.1   thorpej 	/*
    331   1.1   thorpej 	 * you can `ifconfig (link0|-link0) ep0' to get the following
    332   1.1   thorpej 	 * behaviour:
    333   1.1   thorpej 	 *	-link0	disable AUI/UTP. enable BNC.
    334   1.1   thorpej 	 *	link0	disable BNC. enable AUI.
    335   1.1   thorpej 	 *	link1	if the card has a UTP connector, and link0 is
    336   1.1   thorpej 	 *		set too, then you get the UTP port.
    337   1.1   thorpej 	 */
    338   1.1   thorpej 	GO_WINDOW(4);
    339  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_W4_MEDIA_TYPE, DISABLE_UTP);
    340   1.1   thorpej 	if (!(ifp->if_flags & IFF_LINK0) && (sc->ep_connectors & BNC)) {
    341   1.1   thorpej 		if (sc->bustype == EP_BUS_PCMCIA) {
    342   1.1   thorpej 			GO_WINDOW(0);
    343  1.11   thorpej 			bus_space_write_2(iot, ioh, EP_W0_ADDRESS_CFG,3<<14);
    344   1.1   thorpej 			GO_WINDOW(1);
    345   1.1   thorpej 		}
    346  1.11   thorpej 		bus_space_write_2(iot, ioh, EP_COMMAND, START_TRANSCEIVER);
    347   1.1   thorpej 		delay(1000);
    348   1.1   thorpej 	}
    349   1.1   thorpej 	if (ifp->if_flags & IFF_LINK0) {
    350  1.11   thorpej 		bus_space_write_2(iot, ioh, EP_COMMAND, STOP_TRANSCEIVER);
    351   1.1   thorpej 		delay(1000);
    352   1.1   thorpej 		if ((ifp->if_flags & IFF_LINK1) && (sc->ep_connectors & UTP)) {
    353   1.1   thorpej 			if (sc->bustype == EP_BUS_PCMCIA) {
    354   1.1   thorpej 				GO_WINDOW(0);
    355  1.11   thorpej 				bus_space_write_2(iot, ioh,
    356   1.2   thorpej 				    EP_W0_ADDRESS_CFG,0<<14);
    357   1.1   thorpej 				GO_WINDOW(4);
    358   1.1   thorpej 			}
    359  1.11   thorpej 			bus_space_write_2(iot, ioh, EP_W4_MEDIA_TYPE, ENABLE_UTP);
    360   1.1   thorpej 		}
    361   1.1   thorpej 	}
    362   1.1   thorpej 	GO_WINDOW(1);
    363   1.1   thorpej }
    364   1.1   thorpej 
    365   1.1   thorpej /*
    366   1.1   thorpej  * Start outputting on the interface.
    367   1.1   thorpej  * Always called as splnet().
    368   1.1   thorpej  */
    369   1.1   thorpej void
    370   1.1   thorpej epstart(ifp)
    371   1.1   thorpej 	struct ifnet *ifp;
    372   1.1   thorpej {
    373   1.5   thorpej 	register struct ep_softc *sc = ifp->if_softc;
    374  1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    375  1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    376   1.1   thorpej 	struct mbuf *m, *m0;
    377   1.1   thorpej 	int sh, len, pad;
    378   1.1   thorpej 
    379   1.1   thorpej 	/* Don't transmit if interface is busy or not running */
    380   1.1   thorpej 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
    381   1.1   thorpej 		return;
    382   1.1   thorpej 
    383   1.1   thorpej startagain:
    384   1.1   thorpej 	/* Sneak a peek at the next packet */
    385   1.1   thorpej 	m0 = ifp->if_snd.ifq_head;
    386   1.1   thorpej 	if (m0 == 0)
    387   1.1   thorpej 		return;
    388   1.1   thorpej 
    389   1.1   thorpej 	/* We need to use m->m_pkthdr.len, so require the header */
    390   1.1   thorpej 	if ((m0->m_flags & M_PKTHDR) == 0)
    391   1.1   thorpej 		panic("epstart: no header mbuf");
    392   1.1   thorpej 	len = m0->m_pkthdr.len;
    393   1.1   thorpej 
    394   1.1   thorpej 	pad = (4 - len) & 3;
    395   1.1   thorpej 
    396   1.1   thorpej 	/*
    397   1.1   thorpej 	 * The 3c509 automatically pads short packets to minimum ethernet
    398   1.1   thorpej 	 * length, but we drop packets that are too large. Perhaps we should
    399   1.1   thorpej 	 * truncate them instead?
    400   1.1   thorpej 	 */
    401   1.1   thorpej 	if (len + pad > ETHER_MAX_LEN) {
    402   1.1   thorpej 		/* packet is obviously too large: toss it */
    403   1.1   thorpej 		++ifp->if_oerrors;
    404   1.1   thorpej 		IF_DEQUEUE(&ifp->if_snd, m0);
    405   1.1   thorpej 		m_freem(m0);
    406   1.1   thorpej 		goto readcheck;
    407   1.1   thorpej 	}
    408   1.1   thorpej 
    409  1.11   thorpej 	if (bus_space_read_2(iot, ioh, EP_W1_FREE_TX) < len + pad + 4) {
    410  1.11   thorpej 		bus_space_write_2(iot, ioh, EP_COMMAND,
    411  1.12  jonathan 		    SET_TX_AVAIL_THRESH |
    412  1.12  jonathan 		    ((len + pad + 4) >> sc->ep_pktlenshift));
    413   1.1   thorpej 		/* not enough room in FIFO */
    414   1.1   thorpej 		ifp->if_flags |= IFF_OACTIVE;
    415   1.1   thorpej 		return;
    416   1.1   thorpej 	} else {
    417  1.11   thorpej 		bus_space_write_2(iot, ioh, EP_COMMAND,
    418  1.12  jonathan 		    SET_TX_AVAIL_THRESH | EP_THRESH_DISABLE );
    419   1.1   thorpej 	}
    420   1.1   thorpej 
    421   1.1   thorpej 	IF_DEQUEUE(&ifp->if_snd, m0);
    422   1.1   thorpej 	if (m0 == 0)		/* not really needed */
    423   1.1   thorpej 		return;
    424   1.1   thorpej 
    425  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, SET_TX_START_THRESH |
    426  1.12  jonathan 	    ((len / 4 + sc->tx_start_thresh) /* >> sc->ep_pktlenshift*/) );
    427   1.1   thorpej 
    428   1.1   thorpej #if NBPFILTER > 0
    429   1.1   thorpej 	if (ifp->if_bpf)
    430   1.1   thorpej 		bpf_mtap(ifp->if_bpf, m0);
    431   1.1   thorpej #endif
    432   1.1   thorpej 
    433   1.1   thorpej 	/*
    434   1.1   thorpej 	 * Do the output at splhigh() so that an interrupt from another device
    435   1.1   thorpej 	 * won't cause a FIFO underrun.
    436   1.1   thorpej 	 */
    437   1.1   thorpej 	sh = splhigh();
    438   1.1   thorpej 
    439  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_W1_TX_PIO_WR_1, len);
    440  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_W1_TX_PIO_WR_1,
    441   1.2   thorpej 	    0xffff);	/* Second dword meaningless */
    442   1.1   thorpej 	if (EP_IS_BUS_32(sc->bustype)) {
    443   1.1   thorpej 		for (m = m0; m; ) {
    444  1.14       cjs 			if (m->m_len > 3)  {
    445  1.14       cjs 				/* align our reads from core */
    446  1.14       cjs 				if (mtod(m, u_long) & 3)  {
    447  1.14       cjs 					u_long count =
    448  1.14       cjs 					    4 - (mtod(m, u_long) & 3);
    449  1.14       cjs 					bus_space_write_multi_1(iot, ioh,
    450  1.14       cjs 					    EP_W1_TX_PIO_WR_1,
    451  1.14       cjs 					    mtod(m, u_int8_t *), count);
    452  1.14       cjs 					m->m_data =
    453  1.14       cjs 					    (void *)(mtod(m, u_long) + count);
    454  1.14       cjs 					m->m_len -= count;
    455  1.14       cjs 				}
    456  1.11   thorpej 				bus_space_write_multi_4(iot, ioh,
    457  1.14       cjs 				    EP_W1_TX_PIO_WR_1,
    458  1.14       cjs 				    mtod(m, u_int32_t *), m->m_len >> 2);
    459  1.14       cjs 				m->m_data = (void *)(mtod(m, u_long) +
    460  1.14       cjs 					(u_long)(m->m_len & ~3));
    461  1.14       cjs 				m->m_len -= m->m_len & ~3;
    462  1.14       cjs 			}
    463  1.14       cjs 			if (m->m_len)  {
    464  1.11   thorpej 				bus_space_write_multi_1(iot, ioh,
    465   1.2   thorpej 				    EP_W1_TX_PIO_WR_1,
    466  1.14       cjs 				    mtod(m, u_int8_t *), m->m_len);
    467  1.14       cjs 			}
    468   1.1   thorpej 			MFREE(m, m0);
    469   1.1   thorpej 			m = m0;
    470   1.1   thorpej 		}
    471   1.1   thorpej 	} else {
    472   1.1   thorpej 		for (m = m0; m; ) {
    473  1.14       cjs 			if (m->m_len > 1)  {
    474  1.14       cjs 				if (mtod(m, u_long) & 1)  {
    475  1.14       cjs 					bus_space_write_1(iot, ioh,
    476  1.14       cjs 					    EP_W1_TX_PIO_WR_1,
    477  1.14       cjs 					    *(mtod(m, u_int8_t *)));
    478  1.14       cjs 					m->m_data =
    479  1.14       cjs 					    (void *)(mtod(m, u_long) + 1);
    480  1.14       cjs 					m->m_len -= 1;
    481  1.14       cjs 				}
    482  1.11   thorpej 				bus_space_write_multi_2(iot, ioh,
    483   1.2   thorpej 				    EP_W1_TX_PIO_WR_1, mtod(m, u_int16_t *),
    484  1.14       cjs 				    m->m_len >> 1);
    485  1.14       cjs 			}
    486  1.14       cjs 			if (m->m_len & 1)  {
    487  1.11   thorpej 				bus_space_write_1(iot, ioh, EP_W1_TX_PIO_WR_1,
    488   1.2   thorpej 				     *(mtod(m, u_int8_t *) + m->m_len - 1));
    489  1.14       cjs 			}
    490   1.1   thorpej 			MFREE(m, m0);
    491   1.1   thorpej 			m = m0;
    492   1.1   thorpej 		}
    493   1.1   thorpej 	}
    494   1.1   thorpej 	while (pad--)
    495  1.11   thorpej 		bus_space_write_1(iot, ioh, EP_W1_TX_PIO_WR_1, 0);
    496   1.1   thorpej 
    497   1.1   thorpej 	splx(sh);
    498   1.1   thorpej 
    499   1.1   thorpej 	++ifp->if_opackets;
    500   1.1   thorpej 
    501   1.1   thorpej readcheck:
    502  1.11   thorpej 	if ((bus_space_read_2(iot, ioh, EP_W1_RX_STATUS) & ERR_INCOMPLETE) == 0) {
    503   1.1   thorpej 		/* We received a complete packet. */
    504  1.11   thorpej 		u_int16_t status = bus_space_read_2(iot, ioh, EP_STATUS);
    505   1.1   thorpej 
    506   1.1   thorpej 		if ((status & S_INTR_LATCH) == 0) {
    507   1.1   thorpej 			/*
    508   1.1   thorpej 			 * No interrupt, read the packet and continue
    509   1.1   thorpej 			 * Is  this supposed to happen? Is my motherboard
    510   1.1   thorpej 			 * completely busted?
    511   1.1   thorpej 			 */
    512   1.1   thorpej 			epread(sc);
    513   1.1   thorpej 		}
    514   1.1   thorpej 		else
    515   1.1   thorpej 			/* Got an interrupt, return so that it gets serviced. */
    516   1.1   thorpej 			return;
    517   1.1   thorpej 	}
    518   1.1   thorpej 	else {
    519   1.1   thorpej 		/* Check if we are stuck and reset [see XXX comment] */
    520   1.1   thorpej 		if (epstatus(sc)) {
    521   1.1   thorpej 			if (ifp->if_flags & IFF_DEBUG)
    522  1.10  christos 				printf("%s: adapter reset\n",
    523   1.9  christos 				    sc->sc_dev.dv_xname);
    524   1.1   thorpej 			epreset(sc);
    525   1.1   thorpej 		}
    526   1.1   thorpej 	}
    527   1.1   thorpej 
    528   1.1   thorpej 	goto startagain;
    529   1.1   thorpej }
    530   1.1   thorpej 
    531   1.1   thorpej 
    532   1.1   thorpej /*
    533   1.1   thorpej  * XXX: The 3c509 card can get in a mode where both the fifo status bit
    534   1.1   thorpej  *	FIFOS_RX_OVERRUN and the status bit ERR_INCOMPLETE are set
    535   1.1   thorpej  *	We detect this situation and we reset the adapter.
    536   1.1   thorpej  *	It happens at times when there is a lot of broadcast traffic
    537   1.1   thorpej  *	on the cable (once in a blue moon).
    538   1.1   thorpej  */
    539   1.1   thorpej static int
    540   1.1   thorpej epstatus(sc)
    541   1.1   thorpej 	register struct ep_softc *sc;
    542   1.1   thorpej {
    543  1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    544  1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    545   1.7   thorpej 	u_int16_t fifost;
    546   1.1   thorpej 
    547   1.1   thorpej 	/*
    548   1.1   thorpej 	 * Check the FIFO status and act accordingly
    549   1.1   thorpej 	 */
    550   1.1   thorpej 	GO_WINDOW(4);
    551  1.11   thorpej 	fifost = bus_space_read_2(iot, ioh, EP_W4_FIFO_DIAG);
    552   1.1   thorpej 	GO_WINDOW(1);
    553   1.1   thorpej 
    554   1.1   thorpej 	if (fifost & FIFOS_RX_UNDERRUN) {
    555   1.1   thorpej 		if (sc->sc_arpcom.ac_if.if_flags & IFF_DEBUG)
    556  1.10  christos 			printf("%s: RX underrun\n", sc->sc_dev.dv_xname);
    557   1.1   thorpej 		epreset(sc);
    558   1.1   thorpej 		return 0;
    559   1.1   thorpej 	}
    560   1.1   thorpej 
    561   1.1   thorpej 	if (fifost & FIFOS_RX_STATUS_OVERRUN) {
    562   1.1   thorpej 		if (sc->sc_arpcom.ac_if.if_flags & IFF_DEBUG)
    563  1.10  christos 			printf("%s: RX Status overrun\n", sc->sc_dev.dv_xname);
    564   1.1   thorpej 		return 1;
    565   1.1   thorpej 	}
    566   1.1   thorpej 
    567   1.1   thorpej 	if (fifost & FIFOS_RX_OVERRUN) {
    568   1.1   thorpej 		if (sc->sc_arpcom.ac_if.if_flags & IFF_DEBUG)
    569  1.10  christos 			printf("%s: RX overrun\n", sc->sc_dev.dv_xname);
    570   1.1   thorpej 		return 1;
    571   1.1   thorpej 	}
    572   1.1   thorpej 
    573   1.1   thorpej 	if (fifost & FIFOS_TX_OVERRUN) {
    574   1.1   thorpej 		if (sc->sc_arpcom.ac_if.if_flags & IFF_DEBUG)
    575  1.10  christos 			printf("%s: TX overrun\n", sc->sc_dev.dv_xname);
    576   1.1   thorpej 		epreset(sc);
    577   1.1   thorpej 		return 0;
    578   1.1   thorpej 	}
    579   1.1   thorpej 
    580   1.1   thorpej 	return 0;
    581   1.1   thorpej }
    582   1.1   thorpej 
    583   1.1   thorpej 
    584   1.1   thorpej static void
    585   1.1   thorpej eptxstat(sc)
    586   1.1   thorpej 	register struct ep_softc *sc;
    587   1.1   thorpej {
    588  1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    589  1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    590   1.1   thorpej 	int i;
    591   1.1   thorpej 
    592   1.1   thorpej 	/*
    593   1.1   thorpej 	 * We need to read+write TX_STATUS until we get a 0 status
    594   1.1   thorpej 	 * in order to turn off the interrupt flag.
    595   1.1   thorpej 	 */
    596  1.11   thorpej 	while ((i = bus_space_read_1(iot, ioh, EP_W1_TX_STATUS)) & TXS_COMPLETE) {
    597  1.11   thorpej 		bus_space_write_1(iot, ioh, EP_W1_TX_STATUS, 0x0);
    598   1.1   thorpej 
    599   1.1   thorpej 		if (i & TXS_JABBER) {
    600   1.1   thorpej 			++sc->sc_arpcom.ac_if.if_oerrors;
    601   1.1   thorpej 			if (sc->sc_arpcom.ac_if.if_flags & IFF_DEBUG)
    602  1.10  christos 				printf("%s: jabber (%x)\n",
    603   1.1   thorpej 				       sc->sc_dev.dv_xname, i);
    604   1.1   thorpej 			epreset(sc);
    605   1.1   thorpej 		} else if (i & TXS_UNDERRUN) {
    606   1.1   thorpej 			++sc->sc_arpcom.ac_if.if_oerrors;
    607   1.1   thorpej 			if (sc->sc_arpcom.ac_if.if_flags & IFF_DEBUG)
    608  1.10  christos 				printf("%s: fifo underrun (%x) @%d\n",
    609   1.1   thorpej 				       sc->sc_dev.dv_xname, i,
    610   1.1   thorpej 				       sc->tx_start_thresh);
    611   1.1   thorpej 			if (sc->tx_succ_ok < 100)
    612   1.1   thorpej 				    sc->tx_start_thresh = min(ETHER_MAX_LEN,
    613   1.1   thorpej 					    sc->tx_start_thresh + 20);
    614   1.1   thorpej 			sc->tx_succ_ok = 0;
    615   1.1   thorpej 			epreset(sc);
    616   1.1   thorpej 		} else if (i & TXS_MAX_COLLISION) {
    617   1.1   thorpej 			++sc->sc_arpcom.ac_if.if_collisions;
    618  1.11   thorpej 			bus_space_write_2(iot, ioh, EP_COMMAND, TX_ENABLE);
    619   1.1   thorpej 			sc->sc_arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
    620   1.1   thorpej 		} else
    621   1.1   thorpej 			sc->tx_succ_ok = (sc->tx_succ_ok+1) & 127;
    622   1.1   thorpej 	}
    623   1.1   thorpej }
    624   1.1   thorpej 
    625   1.1   thorpej int
    626   1.1   thorpej epintr(arg)
    627   1.1   thorpej 	void *arg;
    628   1.1   thorpej {
    629   1.1   thorpej 	register struct ep_softc *sc = arg;
    630  1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    631  1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    632   1.1   thorpej 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    633   1.7   thorpej 	u_int16_t status;
    634   1.1   thorpej 	int ret = 0;
    635   1.1   thorpej 
    636   1.1   thorpej 	for (;;) {
    637  1.11   thorpej 		bus_space_write_2(iot, ioh, EP_COMMAND, C_INTR_LATCH);
    638   1.1   thorpej 
    639  1.11   thorpej 		status = bus_space_read_2(iot, ioh, EP_STATUS);
    640   1.1   thorpej 
    641   1.1   thorpej 		if ((status & (S_TX_COMPLETE | S_TX_AVAIL |
    642   1.1   thorpej 			       S_RX_COMPLETE | S_CARD_FAILURE)) == 0)
    643   1.1   thorpej 			break;
    644   1.1   thorpej 
    645   1.1   thorpej 		ret = 1;
    646   1.1   thorpej 
    647   1.1   thorpej 		/*
    648   1.1   thorpej 		 * Acknowledge any interrupts.  It's important that we do this
    649   1.1   thorpej 		 * first, since there would otherwise be a race condition.
    650   1.1   thorpej 		 * Due to the i386 interrupt queueing, we may get spurious
    651   1.1   thorpej 		 * interrupts occasionally.
    652   1.1   thorpej 		 */
    653  1.11   thorpej 		bus_space_write_2(iot, ioh, EP_COMMAND, ACK_INTR | status);
    654   1.1   thorpej 
    655   1.1   thorpej 		if (status & S_RX_COMPLETE)
    656   1.1   thorpej 			epread(sc);
    657   1.1   thorpej 		if (status & S_TX_AVAIL) {
    658   1.1   thorpej 			sc->sc_arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
    659   1.1   thorpej 			epstart(&sc->sc_arpcom.ac_if);
    660   1.1   thorpej 		}
    661   1.1   thorpej 		if (status & S_CARD_FAILURE) {
    662  1.10  christos 			printf("%s: adapter failure (%x)\n",
    663   1.9  christos 			    sc->sc_dev.dv_xname, status);
    664   1.1   thorpej 			epreset(sc);
    665   1.1   thorpej 			return (1);
    666   1.1   thorpej 		}
    667   1.1   thorpej 		if (status & S_TX_COMPLETE) {
    668   1.1   thorpej 			eptxstat(sc);
    669   1.1   thorpej 			epstart(ifp);
    670   1.1   thorpej 		}
    671   1.1   thorpej 	}
    672   1.1   thorpej 
    673   1.1   thorpej 	/* no more interrupts */
    674   1.1   thorpej 	return (ret);
    675   1.1   thorpej }
    676   1.1   thorpej 
    677   1.1   thorpej void
    678   1.1   thorpej epread(sc)
    679   1.1   thorpej 	register struct ep_softc *sc;
    680   1.1   thorpej {
    681  1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    682  1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    683   1.1   thorpej 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    684   1.1   thorpej 	struct mbuf *m;
    685   1.1   thorpej 	struct ether_header *eh;
    686   1.1   thorpej 	int len;
    687   1.1   thorpej 
    688  1.11   thorpej 	len = bus_space_read_2(iot, ioh, EP_W1_RX_STATUS);
    689   1.1   thorpej 
    690   1.1   thorpej again:
    691   1.1   thorpej 	if (ifp->if_flags & IFF_DEBUG) {
    692   1.1   thorpej 		int err = len & ERR_MASK;
    693   1.1   thorpej 		char *s = NULL;
    694   1.1   thorpej 
    695   1.1   thorpej 		if (len & ERR_INCOMPLETE)
    696   1.1   thorpej 			s = "incomplete packet";
    697   1.1   thorpej 		else if (err == ERR_OVERRUN)
    698   1.1   thorpej 			s = "packet overrun";
    699   1.1   thorpej 		else if (err == ERR_RUNT)
    700   1.1   thorpej 			s = "runt packet";
    701   1.1   thorpej 		else if (err == ERR_ALIGNMENT)
    702   1.1   thorpej 			s = "bad alignment";
    703   1.1   thorpej 		else if (err == ERR_CRC)
    704   1.1   thorpej 			s = "bad crc";
    705   1.1   thorpej 		else if (err == ERR_OVERSIZE)
    706   1.1   thorpej 			s = "oversized packet";
    707   1.1   thorpej 		else if (err == ERR_DRIBBLE)
    708   1.1   thorpej 			s = "dribble bits";
    709   1.1   thorpej 
    710   1.1   thorpej 		if (s)
    711  1.10  christos 			printf("%s: %s\n", sc->sc_dev.dv_xname, s);
    712   1.1   thorpej 	}
    713   1.1   thorpej 
    714   1.1   thorpej 	if (len & ERR_INCOMPLETE)
    715   1.1   thorpej 		return;
    716   1.1   thorpej 
    717   1.1   thorpej 	if (len & ERR_RX) {
    718   1.1   thorpej 		++ifp->if_ierrors;
    719   1.1   thorpej 		goto abort;
    720   1.1   thorpej 	}
    721   1.1   thorpej 
    722   1.1   thorpej 	len &= RX_BYTES_MASK;	/* Lower 11 bits = RX bytes. */
    723   1.1   thorpej 
    724   1.1   thorpej 	/* Pull packet off interface. */
    725   1.1   thorpej 	m = epget(sc, len);
    726   1.1   thorpej 	if (m == 0) {
    727   1.1   thorpej 		ifp->if_ierrors++;
    728   1.1   thorpej 		goto abort;
    729   1.1   thorpej 	}
    730   1.1   thorpej 
    731   1.1   thorpej 	++ifp->if_ipackets;
    732   1.1   thorpej 
    733   1.1   thorpej 	/* We assume the header fit entirely in one mbuf. */
    734   1.1   thorpej 	eh = mtod(m, struct ether_header *);
    735   1.1   thorpej 
    736   1.1   thorpej #if NBPFILTER > 0
    737   1.1   thorpej 	/*
    738   1.1   thorpej 	 * Check if there's a BPF listener on this interface.
    739   1.1   thorpej 	 * If so, hand off the raw packet to BPF.
    740   1.1   thorpej 	 */
    741   1.1   thorpej 	if (ifp->if_bpf) {
    742   1.1   thorpej 		bpf_mtap(ifp->if_bpf, m);
    743   1.1   thorpej 
    744   1.1   thorpej 		/*
    745   1.1   thorpej 		 * Note that the interface cannot be in promiscuous mode if
    746   1.1   thorpej 		 * there are no BPF listeners.  And if we are in promiscuous
    747   1.1   thorpej 		 * mode, we have to check if this packet is really ours.
    748   1.1   thorpej 		 */
    749   1.1   thorpej 		if ((ifp->if_flags & IFF_PROMISC) &&
    750   1.1   thorpej 		    (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
    751   1.1   thorpej 		    bcmp(eh->ether_dhost, sc->sc_arpcom.ac_enaddr,
    752   1.1   thorpej 			    sizeof(eh->ether_dhost)) != 0) {
    753   1.1   thorpej 			m_freem(m);
    754   1.1   thorpej 			return;
    755   1.1   thorpej 		}
    756   1.1   thorpej 	}
    757   1.1   thorpej #endif
    758   1.1   thorpej 
    759   1.1   thorpej 	/* We assume the header fit entirely in one mbuf. */
    760   1.1   thorpej 	m_adj(m, sizeof(struct ether_header));
    761   1.1   thorpej 	ether_input(ifp, eh, m);
    762   1.1   thorpej 
    763   1.1   thorpej 	/*
    764   1.1   thorpej 	 * In periods of high traffic we can actually receive enough
    765   1.1   thorpej 	 * packets so that the fifo overrun bit will be set at this point,
    766   1.1   thorpej 	 * even though we just read a packet. In this case we
    767   1.1   thorpej 	 * are not going to receive any more interrupts. We check for
    768   1.1   thorpej 	 * this condition and read again until the fifo is not full.
    769   1.1   thorpej 	 * We could simplify this test by not using epstatus(), but
    770   1.1   thorpej 	 * rechecking the RX_STATUS register directly. This test could
    771   1.1   thorpej 	 * result in unnecessary looping in cases where there is a new
    772   1.1   thorpej 	 * packet but the fifo is not full, but it will not fix the
    773   1.1   thorpej 	 * stuck behavior.
    774   1.1   thorpej 	 *
    775   1.1   thorpej 	 * Even with this improvement, we still get packet overrun errors
    776   1.1   thorpej 	 * which are hurting performance. Maybe when I get some more time
    777   1.1   thorpej 	 * I'll modify epread() so that it can handle RX_EARLY interrupts.
    778   1.1   thorpej 	 */
    779   1.1   thorpej 	if (epstatus(sc)) {
    780  1.11   thorpej 		len = bus_space_read_2(iot, ioh, EP_W1_RX_STATUS);
    781   1.1   thorpej 		/* Check if we are stuck and reset [see XXX comment] */
    782   1.1   thorpej 		if (len & ERR_INCOMPLETE) {
    783   1.1   thorpej 			if (ifp->if_flags & IFF_DEBUG)
    784  1.10  christos 				printf("%s: adapter reset\n",
    785   1.9  christos 				    sc->sc_dev.dv_xname);
    786   1.1   thorpej 			epreset(sc);
    787   1.1   thorpej 			return;
    788   1.1   thorpej 		}
    789   1.1   thorpej 		goto again;
    790   1.1   thorpej 	}
    791   1.1   thorpej 
    792   1.1   thorpej 	return;
    793   1.1   thorpej 
    794   1.1   thorpej abort:
    795  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, RX_DISCARD_TOP_PACK);
    796  1.11   thorpej 	while (bus_space_read_2(iot, ioh, EP_STATUS) & S_COMMAND_IN_PROGRESS)
    797   1.1   thorpej 		;
    798   1.1   thorpej }
    799   1.1   thorpej 
    800   1.1   thorpej struct mbuf *
    801   1.1   thorpej epget(sc, totlen)
    802   1.1   thorpej 	struct ep_softc *sc;
    803   1.1   thorpej 	int totlen;
    804   1.1   thorpej {
    805  1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    806  1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    807   1.1   thorpej 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    808   1.1   thorpej 	struct mbuf *top, **mp, *m;
    809  1.14       cjs 	int len, remaining;
    810   1.1   thorpej 	int sh;
    811   1.1   thorpej 
    812   1.1   thorpej 	m = sc->mb[sc->next_mb];
    813   1.1   thorpej 	sc->mb[sc->next_mb] = 0;
    814   1.1   thorpej 	if (m == 0) {
    815   1.1   thorpej 		MGETHDR(m, M_DONTWAIT, MT_DATA);
    816   1.1   thorpej 		if (m == 0)
    817   1.1   thorpej 			return 0;
    818   1.1   thorpej 	} else {
    819   1.1   thorpej 		/* If the queue is no longer full, refill. */
    820   1.1   thorpej 		if (sc->last_mb == sc->next_mb)
    821   1.1   thorpej 			timeout(epmbuffill, sc, 1);
    822   1.1   thorpej 		/* Convert one of our saved mbuf's. */
    823   1.1   thorpej 		sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
    824   1.1   thorpej 		m->m_data = m->m_pktdat;
    825   1.1   thorpej 		m->m_flags = M_PKTHDR;
    826   1.1   thorpej 	}
    827   1.1   thorpej 	m->m_pkthdr.rcvif = ifp;
    828   1.1   thorpej 	m->m_pkthdr.len = totlen;
    829   1.1   thorpej 	len = MHLEN;
    830   1.1   thorpej 	top = 0;
    831   1.1   thorpej 	mp = &top;
    832   1.1   thorpej 
    833   1.1   thorpej 	/*
    834   1.1   thorpej 	 * We read the packet at splhigh() so that an interrupt from another
    835   1.1   thorpej 	 * device doesn't cause the card's buffer to overflow while we're
    836   1.1   thorpej 	 * reading it.  We may still lose packets at other times.
    837   1.1   thorpej 	 */
    838   1.1   thorpej 	sh = splhigh();
    839   1.1   thorpej 
    840   1.1   thorpej 	while (totlen > 0) {
    841   1.1   thorpej 		if (top) {
    842   1.1   thorpej 			m = sc->mb[sc->next_mb];
    843   1.1   thorpej 			sc->mb[sc->next_mb] = 0;
    844   1.1   thorpej 			if (m == 0) {
    845   1.1   thorpej 				MGET(m, M_DONTWAIT, MT_DATA);
    846   1.1   thorpej 				if (m == 0) {
    847   1.1   thorpej 					splx(sh);
    848   1.1   thorpej 					m_freem(top);
    849   1.1   thorpej 					return 0;
    850   1.1   thorpej 				}
    851   1.1   thorpej 			} else {
    852   1.1   thorpej 				sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
    853   1.1   thorpej 			}
    854   1.1   thorpej 			len = MLEN;
    855   1.1   thorpej 		}
    856   1.1   thorpej 		if (totlen >= MINCLSIZE) {
    857   1.1   thorpej 			MCLGET(m, M_DONTWAIT);
    858   1.1   thorpej 			if (m->m_flags & M_EXT)
    859   1.1   thorpej 				len = MCLBYTES;
    860   1.1   thorpej 		}
    861  1.14       cjs 		if (EP_IS_BUS_32(sc->bustype) )  {
    862  1.14       cjs 			u_long pad;
    863  1.14       cjs 			if (top == 0)  {
    864  1.14       cjs 			    /* align the struct ip header */
    865  1.14       cjs 			    pad = ALIGN(sizeof(struct ether_header))
    866  1.14       cjs 				 - sizeof(struct ether_header);
    867  1.14       cjs 			} else {
    868  1.14       cjs 			    /* XXX do we really need this? */
    869  1.14       cjs 			    pad = ALIGN(m->m_data) - (u_long) m->m_data;
    870  1.14       cjs 			}
    871  1.14       cjs 			m->m_data += pad;
    872  1.14       cjs 			len -= pad;
    873  1.14       cjs 		}
    874  1.14       cjs 		remaining = len = min(totlen, len);
    875   1.1   thorpej 		if (EP_IS_BUS_32(sc->bustype)) {
    876  1.14       cjs 			u_long offset = mtod(m, u_long);
    877  1.14       cjs 			/*
    878  1.14       cjs 			 * Read bytes up to the point where we are aligned.
    879  1.14       cjs 			 * (We can align to 4 bytes, rather than ALIGNBYTES,
    880  1.14       cjs 			 * here because we're later reading 4-byte chunks.)
    881  1.14       cjs 			 */
    882  1.14       cjs 			if ((remaining > 3) && (offset & 3))  {
    883  1.14       cjs 				int count = (4 - (offset & 3));
    884  1.14       cjs 				bus_space_read_multi_1(iot, ioh,
    885  1.14       cjs 				    EP_W1_RX_PIO_RD_1,
    886  1.14       cjs 				    (u_int8_t *) offset, count);
    887  1.14       cjs 				offset += count;
    888  1.14       cjs 				remaining -= count;
    889  1.14       cjs 			}
    890  1.14       cjs 			if (remaining > 3) {
    891  1.11   thorpej 				bus_space_read_multi_4(iot, ioh,
    892  1.14       cjs 				    EP_W1_RX_PIO_RD_1,
    893  1.14       cjs 				    (u_int32_t *) offset, remaining >> 2);
    894  1.14       cjs 				offset += remaining & ~3;
    895  1.14       cjs 				remaining &= 3;
    896  1.14       cjs 			}
    897  1.14       cjs 			if (remaining)  {
    898  1.11   thorpej 				bus_space_read_multi_1(iot, ioh,
    899  1.14       cjs 				    EP_W1_RX_PIO_RD_1,
    900  1.14       cjs 				    (u_int8_t *) offset, remaining);
    901  1.14       cjs 			}
    902   1.1   thorpej 		} else {
    903  1.14       cjs 			u_long offset = mtod(m, u_long);
    904  1.14       cjs 			if ((remaining > 1) && (offset & 1))  {
    905  1.14       cjs 				bus_space_read_multi_1(iot, ioh,
    906  1.14       cjs 				    EP_W1_RX_PIO_RD_1,
    907  1.14       cjs 				    (u_int8_t *) offset, 1);
    908  1.14       cjs 				remaining -= 1;
    909  1.14       cjs 				offset += 1;
    910  1.14       cjs 			}
    911  1.14       cjs 			if (remaining > 1) {
    912  1.11   thorpej 				bus_space_read_multi_2(iot, ioh,
    913  1.14       cjs 				    EP_W1_RX_PIO_RD_1,
    914  1.14       cjs 				    (u_int16_t *) offset, remaining >> 1);
    915  1.14       cjs 				offset += remaining & ~1;
    916  1.14       cjs 			}
    917  1.14       cjs 			if (remaining & 1)  {
    918  1.14       cjs 				bus_space_read_multi_1(iot, ioh,
    919  1.14       cjs 				    EP_W1_RX_PIO_RD_1,
    920  1.14       cjs 				    (u_int8_t *) offset, remaining & 1);
    921  1.14       cjs 			}
    922   1.1   thorpej 		}
    923   1.1   thorpej 		m->m_len = len;
    924   1.1   thorpej 		totlen -= len;
    925   1.1   thorpej 		*mp = m;
    926   1.1   thorpej 		mp = &m->m_next;
    927   1.1   thorpej 	}
    928   1.1   thorpej 
    929  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, RX_DISCARD_TOP_PACK);
    930  1.11   thorpej 	while (bus_space_read_2(iot, ioh, EP_STATUS) & S_COMMAND_IN_PROGRESS)
    931   1.1   thorpej 		;
    932   1.1   thorpej 
    933   1.1   thorpej 	splx(sh);
    934   1.1   thorpej 
    935   1.1   thorpej 	return top;
    936   1.1   thorpej }
    937   1.1   thorpej 
    938   1.1   thorpej int
    939   1.1   thorpej epioctl(ifp, cmd, data)
    940   1.1   thorpej 	register struct ifnet *ifp;
    941   1.1   thorpej 	u_long cmd;
    942   1.1   thorpej 	caddr_t data;
    943   1.1   thorpej {
    944   1.5   thorpej 	struct ep_softc *sc = ifp->if_softc;
    945   1.1   thorpej 	struct ifaddr *ifa = (struct ifaddr *)data;
    946   1.1   thorpej 	struct ifreq *ifr = (struct ifreq *)data;
    947   1.1   thorpej 	int s, error = 0;
    948   1.1   thorpej 
    949   1.1   thorpej 	s = splnet();
    950   1.1   thorpej 
    951   1.1   thorpej 	switch (cmd) {
    952   1.1   thorpej 
    953   1.1   thorpej 	case SIOCSIFADDR:
    954   1.1   thorpej 		ifp->if_flags |= IFF_UP;
    955   1.1   thorpej 
    956   1.1   thorpej 		switch (ifa->ifa_addr->sa_family) {
    957   1.1   thorpej #ifdef INET
    958   1.1   thorpej 		case AF_INET:
    959   1.1   thorpej 			epinit(sc);
    960   1.1   thorpej 			arp_ifinit(&sc->sc_arpcom, ifa);
    961   1.1   thorpej 			break;
    962   1.1   thorpej #endif
    963   1.1   thorpej #ifdef NS
    964   1.1   thorpej 		case AF_NS:
    965   1.1   thorpej 		    {
    966   1.1   thorpej 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    967   1.1   thorpej 
    968   1.1   thorpej 			if (ns_nullhost(*ina))
    969   1.1   thorpej 				ina->x_host =
    970   1.1   thorpej 				    *(union ns_host *)(sc->sc_arpcom.ac_enaddr);
    971   1.1   thorpej 			else
    972   1.1   thorpej 				bcopy(ina->x_host.c_host,
    973   1.1   thorpej 				    sc->sc_arpcom.ac_enaddr,
    974   1.1   thorpej 				    sizeof(sc->sc_arpcom.ac_enaddr));
    975   1.1   thorpej 			/* Set new address. */
    976   1.1   thorpej 			epinit(sc);
    977   1.1   thorpej 			break;
    978   1.1   thorpej 		    }
    979   1.1   thorpej #endif
    980   1.1   thorpej 		default:
    981   1.1   thorpej 			epinit(sc);
    982   1.1   thorpej 			break;
    983   1.1   thorpej 		}
    984   1.1   thorpej 		break;
    985   1.1   thorpej 
    986   1.1   thorpej 	case SIOCSIFFLAGS:
    987   1.1   thorpej 		if ((ifp->if_flags & IFF_UP) == 0 &&
    988   1.1   thorpej 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    989   1.1   thorpej 			/*
    990   1.1   thorpej 			 * If interface is marked down and it is running, then
    991   1.1   thorpej 			 * stop it.
    992   1.1   thorpej 			 */
    993   1.1   thorpej 			epstop(sc);
    994   1.1   thorpej 			ifp->if_flags &= ~IFF_RUNNING;
    995   1.1   thorpej 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    996   1.1   thorpej 			   (ifp->if_flags & IFF_RUNNING) == 0) {
    997   1.1   thorpej 			/*
    998   1.1   thorpej 			 * If interface is marked up and it is stopped, then
    999   1.1   thorpej 			 * start it.
   1000   1.1   thorpej 			 */
   1001   1.1   thorpej 			epinit(sc);
   1002   1.1   thorpej 		} else {
   1003   1.1   thorpej 			/*
   1004   1.1   thorpej 			 * deal with flags changes:
   1005   1.1   thorpej 			 * IFF_MULTICAST, IFF_PROMISC,
   1006   1.1   thorpej 			 * IFF_LINK0, IFF_LINK1,
   1007   1.1   thorpej 			 */
   1008   1.1   thorpej 			epsetfilter(sc);
   1009   1.1   thorpej 			epsetlink(sc);
   1010   1.1   thorpej 		}
   1011   1.1   thorpej 		break;
   1012   1.1   thorpej 
   1013   1.1   thorpej 	case SIOCADDMULTI:
   1014   1.1   thorpej 	case SIOCDELMULTI:
   1015   1.1   thorpej 		error = (cmd == SIOCADDMULTI) ?
   1016   1.1   thorpej 		    ether_addmulti(ifr, &sc->sc_arpcom) :
   1017   1.1   thorpej 		    ether_delmulti(ifr, &sc->sc_arpcom);
   1018   1.1   thorpej 
   1019   1.1   thorpej 		if (error == ENETRESET) {
   1020   1.1   thorpej 			/*
   1021   1.1   thorpej 			 * Multicast list has changed; set the hardware filter
   1022   1.1   thorpej 			 * accordingly.
   1023   1.1   thorpej 			 */
   1024   1.1   thorpej 			epreset(sc);
   1025   1.1   thorpej 			error = 0;
   1026   1.1   thorpej 		}
   1027   1.1   thorpej 		break;
   1028   1.1   thorpej 
   1029   1.1   thorpej 	default:
   1030   1.1   thorpej 		error = EINVAL;
   1031   1.1   thorpej 		break;
   1032   1.1   thorpej 	}
   1033   1.1   thorpej 
   1034   1.1   thorpej 	splx(s);
   1035   1.1   thorpej 	return (error);
   1036   1.1   thorpej }
   1037   1.1   thorpej 
   1038   1.1   thorpej void
   1039   1.1   thorpej epreset(sc)
   1040   1.1   thorpej 	struct ep_softc *sc;
   1041   1.1   thorpej {
   1042   1.1   thorpej 	int s;
   1043   1.1   thorpej 
   1044   1.1   thorpej 	s = splnet();
   1045   1.1   thorpej 	epstop(sc);
   1046   1.1   thorpej 	epinit(sc);
   1047   1.1   thorpej 	splx(s);
   1048   1.1   thorpej }
   1049   1.1   thorpej 
   1050   1.1   thorpej void
   1051   1.5   thorpej epwatchdog(ifp)
   1052   1.5   thorpej 	struct ifnet *ifp;
   1053   1.1   thorpej {
   1054   1.5   thorpej 	struct ep_softc *sc = ifp->if_softc;
   1055   1.1   thorpej 
   1056   1.1   thorpej 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
   1057   1.1   thorpej 	++sc->sc_arpcom.ac_if.if_oerrors;
   1058   1.1   thorpej 
   1059   1.1   thorpej 	epreset(sc);
   1060   1.1   thorpej }
   1061   1.1   thorpej 
   1062   1.1   thorpej void
   1063   1.1   thorpej epstop(sc)
   1064   1.1   thorpej 	register struct ep_softc *sc;
   1065   1.1   thorpej {
   1066  1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
   1067  1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
   1068   1.1   thorpej 
   1069  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, RX_DISABLE);
   1070  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, RX_DISCARD_TOP_PACK);
   1071  1.11   thorpej 	while (bus_space_read_2(iot, ioh, EP_STATUS) & S_COMMAND_IN_PROGRESS)
   1072   1.1   thorpej 		;
   1073  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, TX_DISABLE);
   1074  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, STOP_TRANSCEIVER);
   1075  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, RX_RESET);
   1076  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, TX_RESET);
   1077  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, C_INTR_LATCH);
   1078  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, SET_RD_0_MASK);
   1079  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, SET_INTR_MASK);
   1080  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, SET_RX_FILTER);
   1081   1.1   thorpej 
   1082   1.1   thorpej 	epmbufempty(sc);
   1083   1.1   thorpej }
   1084   1.1   thorpej 
   1085   1.1   thorpej /*
   1086   1.1   thorpej  * We get eeprom data from the id_port given an offset into the
   1087   1.1   thorpej  * eeprom.  Basically; after the ID_sequence is sent to all of
   1088   1.1   thorpej  * the cards; they enter the ID_CMD state where they will accept
   1089   1.1   thorpej  * command requests. 0x80-0xbf loads the eeprom data.  We then
   1090   1.1   thorpej  * read the port 16 times and with every read; the cards check
   1091   1.1   thorpej  * for contention (ie: if one card writes a 0 bit and another
   1092   1.1   thorpej  * writes a 1 bit then the host sees a 0. At the end of the cycle;
   1093   1.1   thorpej  * each card compares the data on the bus; if there is a difference
   1094   1.1   thorpej  * then that card goes into ID_WAIT state again). In the meantime;
   1095   1.1   thorpej  * one bit of data is returned in the AX register which is conveniently
   1096  1.11   thorpej  * returned to us by bus_space_read_1().  Hence; we read 16 times getting one
   1097   1.1   thorpej  * bit of data with each read.
   1098   1.2   thorpej  *
   1099   1.2   thorpej  * NOTE: the caller must provide an i/o handle for ELINK_ID_PORT!
   1100   1.1   thorpej  */
   1101   1.2   thorpej u_int16_t
   1102  1.11   thorpej epreadeeprom(iot, ioh, offset)
   1103  1.11   thorpej 	bus_space_tag_t iot;
   1104  1.11   thorpej 	bus_space_handle_t ioh;
   1105   1.2   thorpej 	int offset;
   1106   1.1   thorpej {
   1107   1.2   thorpej 	u_int16_t data = 0;
   1108   1.2   thorpej 	int i;
   1109   1.1   thorpej 
   1110  1.11   thorpej 	bus_space_write_1(iot, ioh, 0, 0x80 + offset);
   1111   1.1   thorpej 	delay(1000);
   1112   1.1   thorpej 	for (i = 0; i < 16; i++)
   1113  1.11   thorpej 		data = (data << 1) | (bus_space_read_2(iot, ioh, 0) & 1);
   1114   1.1   thorpej 	return (data);
   1115   1.1   thorpej }
   1116   1.1   thorpej 
   1117   1.1   thorpej static int
   1118   1.1   thorpej epbusyeeprom(sc)
   1119   1.1   thorpej 	struct ep_softc *sc;
   1120   1.1   thorpej {
   1121  1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
   1122  1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
   1123   1.1   thorpej 	int i = 100, j;
   1124   1.1   thorpej 
   1125   1.1   thorpej 	if (sc->bustype == EP_BUS_PCMCIA) {
   1126   1.1   thorpej 		delay(1000);
   1127   1.1   thorpej 		return 0;
   1128   1.1   thorpej 	}
   1129   1.1   thorpej 
   1130   1.1   thorpej 	while (i--) {
   1131  1.11   thorpej 		j = bus_space_read_2(iot, ioh, EP_W0_EEPROM_COMMAND);
   1132   1.1   thorpej 		if (j & EEPROM_BUSY)
   1133   1.1   thorpej 			delay(100);
   1134   1.1   thorpej 		else
   1135   1.1   thorpej 			break;
   1136   1.1   thorpej 	}
   1137   1.1   thorpej 	if (!i) {
   1138  1.10  christos 		printf("\n%s: eeprom failed to come ready\n",
   1139   1.1   thorpej 		    sc->sc_dev.dv_xname);
   1140   1.1   thorpej 		return (1);
   1141   1.1   thorpej 	}
   1142   1.1   thorpej 	if (j & EEPROM_TST_MODE) {
   1143  1.10  christos 		printf("\n%s: erase pencil mark, or disable plug-n-play mode!\n",
   1144   1.1   thorpej 		    sc->sc_dev.dv_xname);
   1145   1.1   thorpej 		return (1);
   1146   1.1   thorpej 	}
   1147   1.1   thorpej 	return (0);
   1148   1.1   thorpej }
   1149   1.1   thorpej 
   1150   1.1   thorpej void
   1151   1.3  christos epmbuffill(v)
   1152   1.3  christos 	void *v;
   1153   1.1   thorpej {
   1154   1.3  christos 	struct ep_softc *sc = v;
   1155   1.1   thorpej 	int s, i;
   1156   1.1   thorpej 
   1157   1.1   thorpej 	s = splnet();
   1158   1.1   thorpej 	i = sc->last_mb;
   1159   1.1   thorpej 	do {
   1160   1.1   thorpej 		if (sc->mb[i] == NULL)
   1161   1.1   thorpej 			MGET(sc->mb[i], M_DONTWAIT, MT_DATA);
   1162   1.1   thorpej 		if (sc->mb[i] == NULL)
   1163   1.1   thorpej 			break;
   1164   1.1   thorpej 		i = (i + 1) % MAX_MBS;
   1165   1.1   thorpej 	} while (i != sc->next_mb);
   1166   1.1   thorpej 	sc->last_mb = i;
   1167   1.1   thorpej 	/* If the queue was not filled, try again. */
   1168   1.1   thorpej 	if (sc->last_mb != sc->next_mb)
   1169   1.1   thorpej 		timeout(epmbuffill, sc, 1);
   1170   1.1   thorpej 	splx(s);
   1171   1.1   thorpej }
   1172   1.1   thorpej 
   1173   1.1   thorpej void
   1174   1.1   thorpej epmbufempty(sc)
   1175   1.1   thorpej 	struct ep_softc *sc;
   1176   1.1   thorpej {
   1177   1.1   thorpej 	int s, i;
   1178   1.1   thorpej 
   1179   1.1   thorpej 	s = splnet();
   1180   1.1   thorpej 	for (i = 0; i<MAX_MBS; i++) {
   1181   1.1   thorpej 		if (sc->mb[i]) {
   1182   1.1   thorpej 			m_freem(sc->mb[i]);
   1183   1.1   thorpej 			sc->mb[i] = NULL;
   1184   1.1   thorpej 		}
   1185   1.1   thorpej 	}
   1186   1.1   thorpej 	sc->last_mb = sc->next_mb = 0;
   1187   1.1   thorpej 	untimeout(epmbuffill, sc);
   1188   1.1   thorpej 	splx(s);
   1189   1.1   thorpej }
   1190