Home | History | Annotate | Line # | Download | only in ic
elink3.c revision 1.11
      1  1.11   thorpej /*	$NetBSD: elink3.c,v 1.11 1996/10/21 22:34:21 thorpej 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.1   thorpej 
    110   1.1   thorpej 	sc->ep_connectors = 0;
    111  1.10  christos 	printf("%s: ", sc->sc_dev.dv_xname);
    112   1.1   thorpej 	if (conn & IS_AUI) {
    113  1.10  christos 		printf("aui");
    114   1.1   thorpej 		sc->ep_connectors |= AUI;
    115   1.1   thorpej 	}
    116   1.1   thorpej 	if (conn & IS_BNC) {
    117   1.1   thorpej 		if (sc->ep_connectors)
    118  1.10  christos 			printf("/");
    119  1.10  christos 		printf("bnc");
    120   1.1   thorpej 		sc->ep_connectors |= BNC;
    121   1.1   thorpej 	}
    122   1.1   thorpej 	if (conn & IS_UTP) {
    123   1.1   thorpej 		if (sc->ep_connectors)
    124  1.10  christos 			printf("/");
    125  1.10  christos 		printf("utp");
    126   1.1   thorpej 		sc->ep_connectors |= UTP;
    127   1.1   thorpej 	}
    128   1.1   thorpej 	if (!sc->ep_connectors)
    129  1.10  christos 		printf("no connectors!");
    130   1.1   thorpej 
    131   1.1   thorpej 	/*
    132   1.1   thorpej 	 * Read the station address from the eeprom
    133   1.1   thorpej 	 */
    134   1.1   thorpej 	for (i = 0; i < 3; i++) {
    135   1.7   thorpej 		u_int16_t x;
    136   1.1   thorpej 		if (epbusyeeprom(sc))
    137   1.1   thorpej 			return;
    138  1.11   thorpej 		bus_space_write_2(iot, ioh, EP_W0_EEPROM_COMMAND,
    139  1.11   thorpej 		    READ_EEPROM | i);
    140   1.1   thorpej 		if (epbusyeeprom(sc))
    141   1.1   thorpej 			return;
    142  1.11   thorpej 		x = bus_space_read_2(iot, ioh, EP_W0_EEPROM_DATA);
    143   1.1   thorpej 		sc->sc_arpcom.ac_enaddr[(i << 1)] = x >> 8;
    144   1.1   thorpej 		sc->sc_arpcom.ac_enaddr[(i << 1) + 1] = x;
    145   1.1   thorpej 	}
    146   1.1   thorpej 
    147  1.10  christos 	printf(" address %s\n", ether_sprintf(sc->sc_arpcom.ac_enaddr));
    148   1.1   thorpej 
    149   1.5   thorpej 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    150   1.5   thorpej 	ifp->if_softc = sc;
    151   1.1   thorpej 	ifp->if_start = epstart;
    152   1.1   thorpej 	ifp->if_ioctl = epioctl;
    153   1.1   thorpej 	ifp->if_watchdog = epwatchdog;
    154   1.1   thorpej 	ifp->if_flags =
    155   1.1   thorpej 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    156   1.1   thorpej 
    157   1.1   thorpej 	if_attach(ifp);
    158   1.1   thorpej 	ether_ifattach(ifp);
    159   1.1   thorpej 
    160   1.1   thorpej #if NBPFILTER > 0
    161   1.1   thorpej 	bpfattach(&sc->sc_arpcom.ac_if.if_bpf, ifp, DLT_EN10MB,
    162   1.1   thorpej 		  sizeof(struct ether_header));
    163   1.1   thorpej #endif
    164   1.1   thorpej 
    165   1.1   thorpej 	sc->tx_start_thresh = 20;	/* probably a good starting point. */
    166   1.1   thorpej }
    167   1.1   thorpej 
    168   1.1   thorpej /*
    169   1.1   thorpej  * The order in here seems important. Otherwise we may not receive
    170   1.1   thorpej  * interrupts. ?!
    171   1.1   thorpej  */
    172   1.1   thorpej void
    173   1.1   thorpej epinit(sc)
    174   1.1   thorpej 	register struct ep_softc *sc;
    175   1.1   thorpej {
    176   1.1   thorpej 	register struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    177  1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    178  1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    179   1.1   thorpej 	int i;
    180   1.1   thorpej 
    181  1.11   thorpej 	while (bus_space_read_2(iot, ioh, EP_STATUS) & S_COMMAND_IN_PROGRESS)
    182   1.1   thorpej 		;
    183   1.1   thorpej 
    184   1.1   thorpej 	if (sc->bustype != EP_BUS_PCI) {
    185   1.1   thorpej 		GO_WINDOW(0);
    186  1.11   thorpej 		bus_space_write_2(iot, ioh, EP_W0_CONFIG_CTRL, 0);
    187  1.11   thorpej 		bus_space_write_2(iot, ioh, EP_W0_CONFIG_CTRL, ENABLE_DRQ_IRQ);
    188   1.1   thorpej 	}
    189   1.1   thorpej 
    190   1.1   thorpej 	if (sc->bustype == EP_BUS_PCMCIA) {
    191   1.1   thorpej #ifdef EP_COAX_DEFAULT
    192  1.11   thorpej 		bus_space_write_2(iot, ioh, EP_W0_ADDRESS_CFG,3<<14);
    193   1.1   thorpej #else
    194  1.11   thorpej 		bus_space_write_2(iot, ioh, EP_W0_ADDRESS_CFG,0<<14);
    195   1.1   thorpej #endif
    196  1.11   thorpej 		bus_space_write_2(iot, ioh, EP_W0_RESOURCE_CFG, 0x3f00);
    197   1.1   thorpej 	}
    198   1.1   thorpej 
    199   1.1   thorpej 	GO_WINDOW(2);
    200   1.1   thorpej 	for (i = 0; i < 6; i++)	/* Reload the ether_addr. */
    201  1.11   thorpej 		bus_space_write_1(iot, ioh, EP_W2_ADDR_0 + i,
    202   1.2   thorpej 		    sc->sc_arpcom.ac_enaddr[i]);
    203   1.8  christos 
    204   1.8  christos 	if (sc->bustype == EP_BUS_PCI || sc->bustype == EP_BUS_EISA)
    205   1.8  christos 		/* Reset the station-address receive filter */
    206   1.8  christos 		for (i = 0; i < 6; i++)
    207  1.11   thorpej 			bus_space_write_1(iot, ioh, EP_W2_RECVMASK_0 + i, 0);
    208   1.1   thorpej 
    209  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, RX_RESET);
    210  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, TX_RESET);
    211   1.1   thorpej 
    212   1.1   thorpej 	GO_WINDOW(1);		/* Window 1 is operating window */
    213   1.1   thorpej 	for (i = 0; i < 31; i++)
    214  1.11   thorpej 		bus_space_read_1(iot, ioh, EP_W1_TX_STATUS);
    215   1.1   thorpej 
    216  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, SET_RD_0_MASK | S_CARD_FAILURE |
    217   1.1   thorpej 				S_RX_COMPLETE | S_TX_COMPLETE | S_TX_AVAIL);
    218  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, SET_INTR_MASK | S_CARD_FAILURE |
    219   1.1   thorpej 				S_RX_COMPLETE | S_TX_COMPLETE | S_TX_AVAIL);
    220   1.1   thorpej 
    221   1.1   thorpej 	/*
    222   1.1   thorpej 	 * Attempt to get rid of any stray interrupts that occured during
    223   1.1   thorpej 	 * configuration.  On the i386 this isn't possible because one may
    224   1.1   thorpej 	 * already be queued.  However, a single stray interrupt is
    225   1.1   thorpej 	 * unimportant.
    226   1.1   thorpej 	 */
    227  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, ACK_INTR | 0xff);
    228   1.1   thorpej 
    229   1.1   thorpej 	epsetfilter(sc);
    230   1.1   thorpej 	epsetlink(sc);
    231   1.1   thorpej 
    232  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, RX_ENABLE);
    233  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, TX_ENABLE);
    234   1.1   thorpej 
    235   1.1   thorpej 	epmbuffill(sc);
    236   1.1   thorpej 
    237   1.1   thorpej 	/* Interface is now `running', with no output active. */
    238   1.1   thorpej 	ifp->if_flags |= IFF_RUNNING;
    239   1.1   thorpej 	ifp->if_flags &= ~IFF_OACTIVE;
    240   1.1   thorpej 
    241   1.1   thorpej 	/* Attempt to start output, if any. */
    242   1.1   thorpej 	epstart(ifp);
    243   1.1   thorpej }
    244   1.1   thorpej 
    245   1.1   thorpej void
    246   1.1   thorpej epsetfilter(sc)
    247   1.1   thorpej 	register struct ep_softc *sc;
    248   1.1   thorpej {
    249   1.1   thorpej 	register struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    250   1.1   thorpej 
    251   1.1   thorpej 	GO_WINDOW(1);		/* Window 1 is operating window */
    252  1.11   thorpej 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, EP_COMMAND, SET_RX_FILTER |
    253   1.1   thorpej 	    FIL_INDIVIDUAL | FIL_BRDCST |
    254   1.1   thorpej 	    ((ifp->if_flags & IFF_MULTICAST) ? FIL_MULTICAST : 0 ) |
    255   1.1   thorpej 	    ((ifp->if_flags & IFF_PROMISC) ? FIL_PROMISC : 0 ));
    256   1.1   thorpej }
    257   1.1   thorpej 
    258   1.1   thorpej void
    259   1.1   thorpej epsetlink(sc)
    260   1.1   thorpej 	register struct ep_softc *sc;
    261   1.1   thorpej {
    262   1.1   thorpej 	register struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    263  1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    264  1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    265   1.1   thorpej 
    266   1.1   thorpej 	/*
    267   1.1   thorpej 	 * you can `ifconfig (link0|-link0) ep0' to get the following
    268   1.1   thorpej 	 * behaviour:
    269   1.1   thorpej 	 *	-link0	disable AUI/UTP. enable BNC.
    270   1.1   thorpej 	 *	link0	disable BNC. enable AUI.
    271   1.1   thorpej 	 *	link1	if the card has a UTP connector, and link0 is
    272   1.1   thorpej 	 *		set too, then you get the UTP port.
    273   1.1   thorpej 	 */
    274   1.1   thorpej 	GO_WINDOW(4);
    275  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_W4_MEDIA_TYPE, DISABLE_UTP);
    276   1.1   thorpej 	if (!(ifp->if_flags & IFF_LINK0) && (sc->ep_connectors & BNC)) {
    277   1.1   thorpej 		if (sc->bustype == EP_BUS_PCMCIA) {
    278   1.1   thorpej 			GO_WINDOW(0);
    279  1.11   thorpej 			bus_space_write_2(iot, ioh, EP_W0_ADDRESS_CFG,3<<14);
    280   1.1   thorpej 			GO_WINDOW(1);
    281   1.1   thorpej 		}
    282  1.11   thorpej 		bus_space_write_2(iot, ioh, EP_COMMAND, START_TRANSCEIVER);
    283   1.1   thorpej 		delay(1000);
    284   1.1   thorpej 	}
    285   1.1   thorpej 	if (ifp->if_flags & IFF_LINK0) {
    286  1.11   thorpej 		bus_space_write_2(iot, ioh, EP_COMMAND, STOP_TRANSCEIVER);
    287   1.1   thorpej 		delay(1000);
    288   1.1   thorpej 		if ((ifp->if_flags & IFF_LINK1) && (sc->ep_connectors & UTP)) {
    289   1.1   thorpej 			if (sc->bustype == EP_BUS_PCMCIA) {
    290   1.1   thorpej 				GO_WINDOW(0);
    291  1.11   thorpej 				bus_space_write_2(iot, ioh,
    292   1.2   thorpej 				    EP_W0_ADDRESS_CFG,0<<14);
    293   1.1   thorpej 				GO_WINDOW(4);
    294   1.1   thorpej 			}
    295  1.11   thorpej 			bus_space_write_2(iot, ioh, EP_W4_MEDIA_TYPE, ENABLE_UTP);
    296   1.1   thorpej 		}
    297   1.1   thorpej 	}
    298   1.1   thorpej 	GO_WINDOW(1);
    299   1.1   thorpej }
    300   1.1   thorpej 
    301   1.1   thorpej /*
    302   1.1   thorpej  * Start outputting on the interface.
    303   1.1   thorpej  * Always called as splnet().
    304   1.1   thorpej  */
    305   1.1   thorpej void
    306   1.1   thorpej epstart(ifp)
    307   1.1   thorpej 	struct ifnet *ifp;
    308   1.1   thorpej {
    309   1.5   thorpej 	register struct ep_softc *sc = ifp->if_softc;
    310  1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    311  1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    312   1.1   thorpej 	struct mbuf *m, *m0;
    313   1.1   thorpej 	int sh, len, pad;
    314   1.1   thorpej 
    315   1.1   thorpej 	/* Don't transmit if interface is busy or not running */
    316   1.1   thorpej 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
    317   1.1   thorpej 		return;
    318   1.1   thorpej 
    319   1.1   thorpej startagain:
    320   1.1   thorpej 	/* Sneak a peek at the next packet */
    321   1.1   thorpej 	m0 = ifp->if_snd.ifq_head;
    322   1.1   thorpej 	if (m0 == 0)
    323   1.1   thorpej 		return;
    324   1.1   thorpej 
    325   1.1   thorpej 	/* We need to use m->m_pkthdr.len, so require the header */
    326   1.1   thorpej 	if ((m0->m_flags & M_PKTHDR) == 0)
    327   1.1   thorpej 		panic("epstart: no header mbuf");
    328   1.1   thorpej 	len = m0->m_pkthdr.len;
    329   1.1   thorpej 
    330   1.1   thorpej 	pad = (4 - len) & 3;
    331   1.1   thorpej 
    332   1.1   thorpej 	/*
    333   1.1   thorpej 	 * The 3c509 automatically pads short packets to minimum ethernet
    334   1.1   thorpej 	 * length, but we drop packets that are too large. Perhaps we should
    335   1.1   thorpej 	 * truncate them instead?
    336   1.1   thorpej 	 */
    337   1.1   thorpej 	if (len + pad > ETHER_MAX_LEN) {
    338   1.1   thorpej 		/* packet is obviously too large: toss it */
    339   1.1   thorpej 		++ifp->if_oerrors;
    340   1.1   thorpej 		IF_DEQUEUE(&ifp->if_snd, m0);
    341   1.1   thorpej 		m_freem(m0);
    342   1.1   thorpej 		goto readcheck;
    343   1.1   thorpej 	}
    344   1.1   thorpej 
    345  1.11   thorpej 	if (bus_space_read_2(iot, ioh, EP_W1_FREE_TX) < len + pad + 4) {
    346  1.11   thorpej 		bus_space_write_2(iot, ioh, EP_COMMAND,
    347   1.2   thorpej 		    SET_TX_AVAIL_THRESH | (len + pad + 4));
    348   1.1   thorpej 		/* not enough room in FIFO */
    349   1.1   thorpej 		ifp->if_flags |= IFF_OACTIVE;
    350   1.1   thorpej 		return;
    351   1.1   thorpej 	} else {
    352  1.11   thorpej 		bus_space_write_2(iot, ioh, EP_COMMAND,
    353   1.2   thorpej 		    SET_TX_AVAIL_THRESH | 2044);
    354   1.1   thorpej 	}
    355   1.1   thorpej 
    356   1.1   thorpej 	IF_DEQUEUE(&ifp->if_snd, m0);
    357   1.1   thorpej 	if (m0 == 0)		/* not really needed */
    358   1.1   thorpej 		return;
    359   1.1   thorpej 
    360  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, SET_TX_START_THRESH |
    361   1.1   thorpej 	    (len / 4 + sc->tx_start_thresh));
    362   1.1   thorpej 
    363   1.1   thorpej #if NBPFILTER > 0
    364   1.1   thorpej 	if (ifp->if_bpf)
    365   1.1   thorpej 		bpf_mtap(ifp->if_bpf, m0);
    366   1.1   thorpej #endif
    367   1.1   thorpej 
    368   1.1   thorpej 	/*
    369   1.1   thorpej 	 * Do the output at splhigh() so that an interrupt from another device
    370   1.1   thorpej 	 * won't cause a FIFO underrun.
    371   1.1   thorpej 	 */
    372   1.1   thorpej 	sh = splhigh();
    373   1.1   thorpej 
    374  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_W1_TX_PIO_WR_1, len);
    375  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_W1_TX_PIO_WR_1,
    376   1.2   thorpej 	    0xffff);	/* Second dword meaningless */
    377   1.1   thorpej 	if (EP_IS_BUS_32(sc->bustype)) {
    378   1.1   thorpej 		for (m = m0; m; ) {
    379   1.1   thorpej 			if (m->m_len > 3)
    380  1.11   thorpej 				bus_space_write_multi_4(iot, ioh,
    381   1.2   thorpej 				    EP_W1_TX_PIO_WR_1, mtod(m, u_int32_t *),
    382   1.2   thorpej 				    m->m_len / 4);
    383   1.1   thorpej 			if (m->m_len & 3)
    384  1.11   thorpej 				bus_space_write_multi_1(iot, ioh,
    385   1.2   thorpej 				    EP_W1_TX_PIO_WR_1,
    386   1.2   thorpej 				    mtod(m, u_int8_t *) + (m->m_len & ~3),
    387   1.2   thorpej 				    m->m_len & 3);
    388   1.1   thorpej 			MFREE(m, m0);
    389   1.1   thorpej 			m = m0;
    390   1.1   thorpej 		}
    391   1.1   thorpej 	} else {
    392   1.1   thorpej 		for (m = m0; m; ) {
    393   1.1   thorpej 			if (m->m_len > 1)
    394  1.11   thorpej 				bus_space_write_multi_2(iot, ioh,
    395   1.2   thorpej 				    EP_W1_TX_PIO_WR_1, mtod(m, u_int16_t *),
    396   1.2   thorpej 				    m->m_len / 2);
    397   1.1   thorpej 			if (m->m_len & 1)
    398  1.11   thorpej 				bus_space_write_1(iot, ioh, EP_W1_TX_PIO_WR_1,
    399   1.2   thorpej 				     *(mtod(m, u_int8_t *) + m->m_len - 1));
    400   1.1   thorpej 			MFREE(m, m0);
    401   1.1   thorpej 			m = m0;
    402   1.1   thorpej 		}
    403   1.1   thorpej 	}
    404   1.1   thorpej 	while (pad--)
    405  1.11   thorpej 		bus_space_write_1(iot, ioh, EP_W1_TX_PIO_WR_1, 0);
    406   1.1   thorpej 
    407   1.1   thorpej 	splx(sh);
    408   1.1   thorpej 
    409   1.1   thorpej 	++ifp->if_opackets;
    410   1.1   thorpej 
    411   1.1   thorpej readcheck:
    412  1.11   thorpej 	if ((bus_space_read_2(iot, ioh, EP_W1_RX_STATUS) & ERR_INCOMPLETE) == 0) {
    413   1.1   thorpej 		/* We received a complete packet. */
    414  1.11   thorpej 		u_int16_t status = bus_space_read_2(iot, ioh, EP_STATUS);
    415   1.1   thorpej 
    416   1.1   thorpej 		if ((status & S_INTR_LATCH) == 0) {
    417   1.1   thorpej 			/*
    418   1.1   thorpej 			 * No interrupt, read the packet and continue
    419   1.1   thorpej 			 * Is  this supposed to happen? Is my motherboard
    420   1.1   thorpej 			 * completely busted?
    421   1.1   thorpej 			 */
    422   1.1   thorpej 			epread(sc);
    423   1.1   thorpej 		}
    424   1.1   thorpej 		else
    425   1.1   thorpej 			/* Got an interrupt, return so that it gets serviced. */
    426   1.1   thorpej 			return;
    427   1.1   thorpej 	}
    428   1.1   thorpej 	else {
    429   1.1   thorpej 		/* Check if we are stuck and reset [see XXX comment] */
    430   1.1   thorpej 		if (epstatus(sc)) {
    431   1.1   thorpej 			if (ifp->if_flags & IFF_DEBUG)
    432  1.10  christos 				printf("%s: adapter reset\n",
    433   1.9  christos 				    sc->sc_dev.dv_xname);
    434   1.1   thorpej 			epreset(sc);
    435   1.1   thorpej 		}
    436   1.1   thorpej 	}
    437   1.1   thorpej 
    438   1.1   thorpej 	goto startagain;
    439   1.1   thorpej }
    440   1.1   thorpej 
    441   1.1   thorpej 
    442   1.1   thorpej /*
    443   1.1   thorpej  * XXX: The 3c509 card can get in a mode where both the fifo status bit
    444   1.1   thorpej  *	FIFOS_RX_OVERRUN and the status bit ERR_INCOMPLETE are set
    445   1.1   thorpej  *	We detect this situation and we reset the adapter.
    446   1.1   thorpej  *	It happens at times when there is a lot of broadcast traffic
    447   1.1   thorpej  *	on the cable (once in a blue moon).
    448   1.1   thorpej  */
    449   1.1   thorpej static int
    450   1.1   thorpej epstatus(sc)
    451   1.1   thorpej 	register struct ep_softc *sc;
    452   1.1   thorpej {
    453  1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    454  1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    455   1.7   thorpej 	u_int16_t fifost;
    456   1.1   thorpej 
    457   1.1   thorpej 	/*
    458   1.1   thorpej 	 * Check the FIFO status and act accordingly
    459   1.1   thorpej 	 */
    460   1.1   thorpej 	GO_WINDOW(4);
    461  1.11   thorpej 	fifost = bus_space_read_2(iot, ioh, EP_W4_FIFO_DIAG);
    462   1.1   thorpej 	GO_WINDOW(1);
    463   1.1   thorpej 
    464   1.1   thorpej 	if (fifost & FIFOS_RX_UNDERRUN) {
    465   1.1   thorpej 		if (sc->sc_arpcom.ac_if.if_flags & IFF_DEBUG)
    466  1.10  christos 			printf("%s: RX underrun\n", sc->sc_dev.dv_xname);
    467   1.1   thorpej 		epreset(sc);
    468   1.1   thorpej 		return 0;
    469   1.1   thorpej 	}
    470   1.1   thorpej 
    471   1.1   thorpej 	if (fifost & FIFOS_RX_STATUS_OVERRUN) {
    472   1.1   thorpej 		if (sc->sc_arpcom.ac_if.if_flags & IFF_DEBUG)
    473  1.10  christos 			printf("%s: RX Status overrun\n", sc->sc_dev.dv_xname);
    474   1.1   thorpej 		return 1;
    475   1.1   thorpej 	}
    476   1.1   thorpej 
    477   1.1   thorpej 	if (fifost & FIFOS_RX_OVERRUN) {
    478   1.1   thorpej 		if (sc->sc_arpcom.ac_if.if_flags & IFF_DEBUG)
    479  1.10  christos 			printf("%s: RX overrun\n", sc->sc_dev.dv_xname);
    480   1.1   thorpej 		return 1;
    481   1.1   thorpej 	}
    482   1.1   thorpej 
    483   1.1   thorpej 	if (fifost & FIFOS_TX_OVERRUN) {
    484   1.1   thorpej 		if (sc->sc_arpcom.ac_if.if_flags & IFF_DEBUG)
    485  1.10  christos 			printf("%s: TX overrun\n", sc->sc_dev.dv_xname);
    486   1.1   thorpej 		epreset(sc);
    487   1.1   thorpej 		return 0;
    488   1.1   thorpej 	}
    489   1.1   thorpej 
    490   1.1   thorpej 	return 0;
    491   1.1   thorpej }
    492   1.1   thorpej 
    493   1.1   thorpej 
    494   1.1   thorpej static void
    495   1.1   thorpej eptxstat(sc)
    496   1.1   thorpej 	register struct ep_softc *sc;
    497   1.1   thorpej {
    498  1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    499  1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    500   1.1   thorpej 	int i;
    501   1.1   thorpej 
    502   1.1   thorpej 	/*
    503   1.1   thorpej 	 * We need to read+write TX_STATUS until we get a 0 status
    504   1.1   thorpej 	 * in order to turn off the interrupt flag.
    505   1.1   thorpej 	 */
    506  1.11   thorpej 	while ((i = bus_space_read_1(iot, ioh, EP_W1_TX_STATUS)) & TXS_COMPLETE) {
    507  1.11   thorpej 		bus_space_write_1(iot, ioh, EP_W1_TX_STATUS, 0x0);
    508   1.1   thorpej 
    509   1.1   thorpej 		if (i & TXS_JABBER) {
    510   1.1   thorpej 			++sc->sc_arpcom.ac_if.if_oerrors;
    511   1.1   thorpej 			if (sc->sc_arpcom.ac_if.if_flags & IFF_DEBUG)
    512  1.10  christos 				printf("%s: jabber (%x)\n",
    513   1.1   thorpej 				       sc->sc_dev.dv_xname, i);
    514   1.1   thorpej 			epreset(sc);
    515   1.1   thorpej 		} else if (i & TXS_UNDERRUN) {
    516   1.1   thorpej 			++sc->sc_arpcom.ac_if.if_oerrors;
    517   1.1   thorpej 			if (sc->sc_arpcom.ac_if.if_flags & IFF_DEBUG)
    518  1.10  christos 				printf("%s: fifo underrun (%x) @%d\n",
    519   1.1   thorpej 				       sc->sc_dev.dv_xname, i,
    520   1.1   thorpej 				       sc->tx_start_thresh);
    521   1.1   thorpej 			if (sc->tx_succ_ok < 100)
    522   1.1   thorpej 				    sc->tx_start_thresh = min(ETHER_MAX_LEN,
    523   1.1   thorpej 					    sc->tx_start_thresh + 20);
    524   1.1   thorpej 			sc->tx_succ_ok = 0;
    525   1.1   thorpej 			epreset(sc);
    526   1.1   thorpej 		} else if (i & TXS_MAX_COLLISION) {
    527   1.1   thorpej 			++sc->sc_arpcom.ac_if.if_collisions;
    528  1.11   thorpej 			bus_space_write_2(iot, ioh, EP_COMMAND, TX_ENABLE);
    529   1.1   thorpej 			sc->sc_arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
    530   1.1   thorpej 		} else
    531   1.1   thorpej 			sc->tx_succ_ok = (sc->tx_succ_ok+1) & 127;
    532   1.1   thorpej 	}
    533   1.1   thorpej }
    534   1.1   thorpej 
    535   1.1   thorpej int
    536   1.1   thorpej epintr(arg)
    537   1.1   thorpej 	void *arg;
    538   1.1   thorpej {
    539   1.1   thorpej 	register struct ep_softc *sc = arg;
    540  1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    541  1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    542   1.1   thorpej 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    543   1.7   thorpej 	u_int16_t status;
    544   1.1   thorpej 	int ret = 0;
    545   1.1   thorpej 
    546   1.1   thorpej 	for (;;) {
    547  1.11   thorpej 		bus_space_write_2(iot, ioh, EP_COMMAND, C_INTR_LATCH);
    548   1.1   thorpej 
    549  1.11   thorpej 		status = bus_space_read_2(iot, ioh, EP_STATUS);
    550   1.1   thorpej 
    551   1.1   thorpej 		if ((status & (S_TX_COMPLETE | S_TX_AVAIL |
    552   1.1   thorpej 			       S_RX_COMPLETE | S_CARD_FAILURE)) == 0)
    553   1.1   thorpej 			break;
    554   1.1   thorpej 
    555   1.1   thorpej 		ret = 1;
    556   1.1   thorpej 
    557   1.1   thorpej 		/*
    558   1.1   thorpej 		 * Acknowledge any interrupts.  It's important that we do this
    559   1.1   thorpej 		 * first, since there would otherwise be a race condition.
    560   1.1   thorpej 		 * Due to the i386 interrupt queueing, we may get spurious
    561   1.1   thorpej 		 * interrupts occasionally.
    562   1.1   thorpej 		 */
    563  1.11   thorpej 		bus_space_write_2(iot, ioh, EP_COMMAND, ACK_INTR | status);
    564   1.1   thorpej 
    565   1.1   thorpej 		if (status & S_RX_COMPLETE)
    566   1.1   thorpej 			epread(sc);
    567   1.1   thorpej 		if (status & S_TX_AVAIL) {
    568   1.1   thorpej 			sc->sc_arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
    569   1.1   thorpej 			epstart(&sc->sc_arpcom.ac_if);
    570   1.1   thorpej 		}
    571   1.1   thorpej 		if (status & S_CARD_FAILURE) {
    572  1.10  christos 			printf("%s: adapter failure (%x)\n",
    573   1.9  christos 			    sc->sc_dev.dv_xname, status);
    574   1.1   thorpej 			epreset(sc);
    575   1.1   thorpej 			return (1);
    576   1.1   thorpej 		}
    577   1.1   thorpej 		if (status & S_TX_COMPLETE) {
    578   1.1   thorpej 			eptxstat(sc);
    579   1.1   thorpej 			epstart(ifp);
    580   1.1   thorpej 		}
    581   1.1   thorpej 	}
    582   1.1   thorpej 
    583   1.1   thorpej 	/* no more interrupts */
    584   1.1   thorpej 	return (ret);
    585   1.1   thorpej }
    586   1.1   thorpej 
    587   1.1   thorpej void
    588   1.1   thorpej epread(sc)
    589   1.1   thorpej 	register struct ep_softc *sc;
    590   1.1   thorpej {
    591  1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    592  1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    593   1.1   thorpej 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    594   1.1   thorpej 	struct mbuf *m;
    595   1.1   thorpej 	struct ether_header *eh;
    596   1.1   thorpej 	int len;
    597   1.1   thorpej 
    598  1.11   thorpej 	len = bus_space_read_2(iot, ioh, EP_W1_RX_STATUS);
    599   1.1   thorpej 
    600   1.1   thorpej again:
    601   1.1   thorpej 	if (ifp->if_flags & IFF_DEBUG) {
    602   1.1   thorpej 		int err = len & ERR_MASK;
    603   1.1   thorpej 		char *s = NULL;
    604   1.1   thorpej 
    605   1.1   thorpej 		if (len & ERR_INCOMPLETE)
    606   1.1   thorpej 			s = "incomplete packet";
    607   1.1   thorpej 		else if (err == ERR_OVERRUN)
    608   1.1   thorpej 			s = "packet overrun";
    609   1.1   thorpej 		else if (err == ERR_RUNT)
    610   1.1   thorpej 			s = "runt packet";
    611   1.1   thorpej 		else if (err == ERR_ALIGNMENT)
    612   1.1   thorpej 			s = "bad alignment";
    613   1.1   thorpej 		else if (err == ERR_CRC)
    614   1.1   thorpej 			s = "bad crc";
    615   1.1   thorpej 		else if (err == ERR_OVERSIZE)
    616   1.1   thorpej 			s = "oversized packet";
    617   1.1   thorpej 		else if (err == ERR_DRIBBLE)
    618   1.1   thorpej 			s = "dribble bits";
    619   1.1   thorpej 
    620   1.1   thorpej 		if (s)
    621  1.10  christos 			printf("%s: %s\n", sc->sc_dev.dv_xname, s);
    622   1.1   thorpej 	}
    623   1.1   thorpej 
    624   1.1   thorpej 	if (len & ERR_INCOMPLETE)
    625   1.1   thorpej 		return;
    626   1.1   thorpej 
    627   1.1   thorpej 	if (len & ERR_RX) {
    628   1.1   thorpej 		++ifp->if_ierrors;
    629   1.1   thorpej 		goto abort;
    630   1.1   thorpej 	}
    631   1.1   thorpej 
    632   1.1   thorpej 	len &= RX_BYTES_MASK;	/* Lower 11 bits = RX bytes. */
    633   1.1   thorpej 
    634   1.1   thorpej 	/* Pull packet off interface. */
    635   1.1   thorpej 	m = epget(sc, len);
    636   1.1   thorpej 	if (m == 0) {
    637   1.1   thorpej 		ifp->if_ierrors++;
    638   1.1   thorpej 		goto abort;
    639   1.1   thorpej 	}
    640   1.1   thorpej 
    641   1.1   thorpej 	++ifp->if_ipackets;
    642   1.1   thorpej 
    643   1.1   thorpej 	/* We assume the header fit entirely in one mbuf. */
    644   1.1   thorpej 	eh = mtod(m, struct ether_header *);
    645   1.1   thorpej 
    646   1.1   thorpej #if NBPFILTER > 0
    647   1.1   thorpej 	/*
    648   1.1   thorpej 	 * Check if there's a BPF listener on this interface.
    649   1.1   thorpej 	 * If so, hand off the raw packet to BPF.
    650   1.1   thorpej 	 */
    651   1.1   thorpej 	if (ifp->if_bpf) {
    652   1.1   thorpej 		bpf_mtap(ifp->if_bpf, m);
    653   1.1   thorpej 
    654   1.1   thorpej 		/*
    655   1.1   thorpej 		 * Note that the interface cannot be in promiscuous mode if
    656   1.1   thorpej 		 * there are no BPF listeners.  And if we are in promiscuous
    657   1.1   thorpej 		 * mode, we have to check if this packet is really ours.
    658   1.1   thorpej 		 */
    659   1.1   thorpej 		if ((ifp->if_flags & IFF_PROMISC) &&
    660   1.1   thorpej 		    (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
    661   1.1   thorpej 		    bcmp(eh->ether_dhost, sc->sc_arpcom.ac_enaddr,
    662   1.1   thorpej 			    sizeof(eh->ether_dhost)) != 0) {
    663   1.1   thorpej 			m_freem(m);
    664   1.1   thorpej 			return;
    665   1.1   thorpej 		}
    666   1.1   thorpej 	}
    667   1.1   thorpej #endif
    668   1.1   thorpej 
    669   1.1   thorpej 	/* We assume the header fit entirely in one mbuf. */
    670   1.1   thorpej 	m_adj(m, sizeof(struct ether_header));
    671   1.1   thorpej 	ether_input(ifp, eh, m);
    672   1.1   thorpej 
    673   1.1   thorpej 	/*
    674   1.1   thorpej 	 * In periods of high traffic we can actually receive enough
    675   1.1   thorpej 	 * packets so that the fifo overrun bit will be set at this point,
    676   1.1   thorpej 	 * even though we just read a packet. In this case we
    677   1.1   thorpej 	 * are not going to receive any more interrupts. We check for
    678   1.1   thorpej 	 * this condition and read again until the fifo is not full.
    679   1.1   thorpej 	 * We could simplify this test by not using epstatus(), but
    680   1.1   thorpej 	 * rechecking the RX_STATUS register directly. This test could
    681   1.1   thorpej 	 * result in unnecessary looping in cases where there is a new
    682   1.1   thorpej 	 * packet but the fifo is not full, but it will not fix the
    683   1.1   thorpej 	 * stuck behavior.
    684   1.1   thorpej 	 *
    685   1.1   thorpej 	 * Even with this improvement, we still get packet overrun errors
    686   1.1   thorpej 	 * which are hurting performance. Maybe when I get some more time
    687   1.1   thorpej 	 * I'll modify epread() so that it can handle RX_EARLY interrupts.
    688   1.1   thorpej 	 */
    689   1.1   thorpej 	if (epstatus(sc)) {
    690  1.11   thorpej 		len = bus_space_read_2(iot, ioh, EP_W1_RX_STATUS);
    691   1.1   thorpej 		/* Check if we are stuck and reset [see XXX comment] */
    692   1.1   thorpej 		if (len & ERR_INCOMPLETE) {
    693   1.1   thorpej 			if (ifp->if_flags & IFF_DEBUG)
    694  1.10  christos 				printf("%s: adapter reset\n",
    695   1.9  christos 				    sc->sc_dev.dv_xname);
    696   1.1   thorpej 			epreset(sc);
    697   1.1   thorpej 			return;
    698   1.1   thorpej 		}
    699   1.1   thorpej 		goto again;
    700   1.1   thorpej 	}
    701   1.1   thorpej 
    702   1.1   thorpej 	return;
    703   1.1   thorpej 
    704   1.1   thorpej abort:
    705  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, RX_DISCARD_TOP_PACK);
    706  1.11   thorpej 	while (bus_space_read_2(iot, ioh, EP_STATUS) & S_COMMAND_IN_PROGRESS)
    707   1.1   thorpej 		;
    708   1.1   thorpej }
    709   1.1   thorpej 
    710   1.1   thorpej struct mbuf *
    711   1.1   thorpej epget(sc, totlen)
    712   1.1   thorpej 	struct ep_softc *sc;
    713   1.1   thorpej 	int totlen;
    714   1.1   thorpej {
    715  1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    716  1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    717   1.1   thorpej 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    718   1.1   thorpej 	struct mbuf *top, **mp, *m;
    719   1.1   thorpej 	int len;
    720   1.1   thorpej 	int sh;
    721   1.1   thorpej 
    722   1.1   thorpej 	m = sc->mb[sc->next_mb];
    723   1.1   thorpej 	sc->mb[sc->next_mb] = 0;
    724   1.1   thorpej 	if (m == 0) {
    725   1.1   thorpej 		MGETHDR(m, M_DONTWAIT, MT_DATA);
    726   1.1   thorpej 		if (m == 0)
    727   1.1   thorpej 			return 0;
    728   1.1   thorpej 	} else {
    729   1.1   thorpej 		/* If the queue is no longer full, refill. */
    730   1.1   thorpej 		if (sc->last_mb == sc->next_mb)
    731   1.1   thorpej 			timeout(epmbuffill, sc, 1);
    732   1.1   thorpej 		/* Convert one of our saved mbuf's. */
    733   1.1   thorpej 		sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
    734   1.1   thorpej 		m->m_data = m->m_pktdat;
    735   1.1   thorpej 		m->m_flags = M_PKTHDR;
    736   1.1   thorpej 	}
    737   1.1   thorpej 	m->m_pkthdr.rcvif = ifp;
    738   1.1   thorpej 	m->m_pkthdr.len = totlen;
    739   1.1   thorpej 	len = MHLEN;
    740   1.1   thorpej 	top = 0;
    741   1.1   thorpej 	mp = &top;
    742   1.1   thorpej 
    743   1.1   thorpej 	/*
    744   1.1   thorpej 	 * We read the packet at splhigh() so that an interrupt from another
    745   1.1   thorpej 	 * device doesn't cause the card's buffer to overflow while we're
    746   1.1   thorpej 	 * reading it.  We may still lose packets at other times.
    747   1.1   thorpej 	 */
    748   1.1   thorpej 	sh = splhigh();
    749   1.1   thorpej 
    750   1.1   thorpej 	while (totlen > 0) {
    751   1.1   thorpej 		if (top) {
    752   1.1   thorpej 			m = sc->mb[sc->next_mb];
    753   1.1   thorpej 			sc->mb[sc->next_mb] = 0;
    754   1.1   thorpej 			if (m == 0) {
    755   1.1   thorpej 				MGET(m, M_DONTWAIT, MT_DATA);
    756   1.1   thorpej 				if (m == 0) {
    757   1.1   thorpej 					splx(sh);
    758   1.1   thorpej 					m_freem(top);
    759   1.1   thorpej 					return 0;
    760   1.1   thorpej 				}
    761   1.1   thorpej 			} else {
    762   1.1   thorpej 				sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
    763   1.1   thorpej 			}
    764   1.1   thorpej 			len = MLEN;
    765   1.1   thorpej 		}
    766   1.1   thorpej 		if (totlen >= MINCLSIZE) {
    767   1.1   thorpej 			MCLGET(m, M_DONTWAIT);
    768   1.1   thorpej 			if (m->m_flags & M_EXT)
    769   1.1   thorpej 				len = MCLBYTES;
    770   1.1   thorpej 		}
    771   1.1   thorpej 		len = min(totlen, len);
    772   1.1   thorpej 		if (EP_IS_BUS_32(sc->bustype)) {
    773   1.1   thorpej 			if (len > 3) {
    774   1.1   thorpej 				len &= ~3;
    775  1.11   thorpej 				bus_space_read_multi_4(iot, ioh,
    776   1.2   thorpej 				    EP_W1_RX_PIO_RD_1, mtod(m, u_int32_t *),
    777   1.2   thorpej 				    len / 4);
    778   1.1   thorpej 			} else
    779  1.11   thorpej 				bus_space_read_multi_1(iot, ioh,
    780   1.2   thorpej 				    EP_W1_RX_PIO_RD_1, mtod(m, u_int8_t *),
    781   1.2   thorpej 				    len);
    782   1.1   thorpej 		} else {
    783   1.1   thorpej 			if (len > 1) {
    784   1.1   thorpej 				len &= ~1;
    785  1.11   thorpej 				bus_space_read_multi_2(iot, ioh,
    786   1.2   thorpej 				    EP_W1_RX_PIO_RD_1, mtod(m, u_int16_t *),
    787   1.2   thorpej 				    len / 2);
    788   1.1   thorpej 			} else
    789   1.2   thorpej 				*(mtod(m, u_int8_t *)) =
    790  1.11   thorpej 				    bus_space_read_1(iot, ioh, EP_W1_RX_PIO_RD_1);
    791   1.1   thorpej 		}
    792   1.1   thorpej 		m->m_len = len;
    793   1.1   thorpej 		totlen -= len;
    794   1.1   thorpej 		*mp = m;
    795   1.1   thorpej 		mp = &m->m_next;
    796   1.1   thorpej 	}
    797   1.1   thorpej 
    798  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, RX_DISCARD_TOP_PACK);
    799  1.11   thorpej 	while (bus_space_read_2(iot, ioh, EP_STATUS) & S_COMMAND_IN_PROGRESS)
    800   1.1   thorpej 		;
    801   1.1   thorpej 
    802   1.1   thorpej 	splx(sh);
    803   1.1   thorpej 
    804   1.1   thorpej 	return top;
    805   1.1   thorpej }
    806   1.1   thorpej 
    807   1.1   thorpej int
    808   1.1   thorpej epioctl(ifp, cmd, data)
    809   1.1   thorpej 	register struct ifnet *ifp;
    810   1.1   thorpej 	u_long cmd;
    811   1.1   thorpej 	caddr_t data;
    812   1.1   thorpej {
    813   1.5   thorpej 	struct ep_softc *sc = ifp->if_softc;
    814   1.1   thorpej 	struct ifaddr *ifa = (struct ifaddr *)data;
    815   1.1   thorpej 	struct ifreq *ifr = (struct ifreq *)data;
    816   1.1   thorpej 	int s, error = 0;
    817   1.1   thorpej 
    818   1.1   thorpej 	s = splnet();
    819   1.1   thorpej 
    820   1.1   thorpej 	switch (cmd) {
    821   1.1   thorpej 
    822   1.1   thorpej 	case SIOCSIFADDR:
    823   1.1   thorpej 		ifp->if_flags |= IFF_UP;
    824   1.1   thorpej 
    825   1.1   thorpej 		switch (ifa->ifa_addr->sa_family) {
    826   1.1   thorpej #ifdef INET
    827   1.1   thorpej 		case AF_INET:
    828   1.1   thorpej 			epinit(sc);
    829   1.1   thorpej 			arp_ifinit(&sc->sc_arpcom, ifa);
    830   1.1   thorpej 			break;
    831   1.1   thorpej #endif
    832   1.1   thorpej #ifdef NS
    833   1.1   thorpej 		case AF_NS:
    834   1.1   thorpej 		    {
    835   1.1   thorpej 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    836   1.1   thorpej 
    837   1.1   thorpej 			if (ns_nullhost(*ina))
    838   1.1   thorpej 				ina->x_host =
    839   1.1   thorpej 				    *(union ns_host *)(sc->sc_arpcom.ac_enaddr);
    840   1.1   thorpej 			else
    841   1.1   thorpej 				bcopy(ina->x_host.c_host,
    842   1.1   thorpej 				    sc->sc_arpcom.ac_enaddr,
    843   1.1   thorpej 				    sizeof(sc->sc_arpcom.ac_enaddr));
    844   1.1   thorpej 			/* Set new address. */
    845   1.1   thorpej 			epinit(sc);
    846   1.1   thorpej 			break;
    847   1.1   thorpej 		    }
    848   1.1   thorpej #endif
    849   1.1   thorpej 		default:
    850   1.1   thorpej 			epinit(sc);
    851   1.1   thorpej 			break;
    852   1.1   thorpej 		}
    853   1.1   thorpej 		break;
    854   1.1   thorpej 
    855   1.1   thorpej 	case SIOCSIFFLAGS:
    856   1.1   thorpej 		if ((ifp->if_flags & IFF_UP) == 0 &&
    857   1.1   thorpej 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    858   1.1   thorpej 			/*
    859   1.1   thorpej 			 * If interface is marked down and it is running, then
    860   1.1   thorpej 			 * stop it.
    861   1.1   thorpej 			 */
    862   1.1   thorpej 			epstop(sc);
    863   1.1   thorpej 			ifp->if_flags &= ~IFF_RUNNING;
    864   1.1   thorpej 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    865   1.1   thorpej 			   (ifp->if_flags & IFF_RUNNING) == 0) {
    866   1.1   thorpej 			/*
    867   1.1   thorpej 			 * If interface is marked up and it is stopped, then
    868   1.1   thorpej 			 * start it.
    869   1.1   thorpej 			 */
    870   1.1   thorpej 			epinit(sc);
    871   1.1   thorpej 		} else {
    872   1.1   thorpej 			/*
    873   1.1   thorpej 			 * deal with flags changes:
    874   1.1   thorpej 			 * IFF_MULTICAST, IFF_PROMISC,
    875   1.1   thorpej 			 * IFF_LINK0, IFF_LINK1,
    876   1.1   thorpej 			 */
    877   1.1   thorpej 			epsetfilter(sc);
    878   1.1   thorpej 			epsetlink(sc);
    879   1.1   thorpej 		}
    880   1.1   thorpej 		break;
    881   1.1   thorpej 
    882   1.1   thorpej 	case SIOCADDMULTI:
    883   1.1   thorpej 	case SIOCDELMULTI:
    884   1.1   thorpej 		error = (cmd == SIOCADDMULTI) ?
    885   1.1   thorpej 		    ether_addmulti(ifr, &sc->sc_arpcom) :
    886   1.1   thorpej 		    ether_delmulti(ifr, &sc->sc_arpcom);
    887   1.1   thorpej 
    888   1.1   thorpej 		if (error == ENETRESET) {
    889   1.1   thorpej 			/*
    890   1.1   thorpej 			 * Multicast list has changed; set the hardware filter
    891   1.1   thorpej 			 * accordingly.
    892   1.1   thorpej 			 */
    893   1.1   thorpej 			epreset(sc);
    894   1.1   thorpej 			error = 0;
    895   1.1   thorpej 		}
    896   1.1   thorpej 		break;
    897   1.1   thorpej 
    898   1.1   thorpej 	default:
    899   1.1   thorpej 		error = EINVAL;
    900   1.1   thorpej 		break;
    901   1.1   thorpej 	}
    902   1.1   thorpej 
    903   1.1   thorpej 	splx(s);
    904   1.1   thorpej 	return (error);
    905   1.1   thorpej }
    906   1.1   thorpej 
    907   1.1   thorpej void
    908   1.1   thorpej epreset(sc)
    909   1.1   thorpej 	struct ep_softc *sc;
    910   1.1   thorpej {
    911   1.1   thorpej 	int s;
    912   1.1   thorpej 
    913   1.1   thorpej 	s = splnet();
    914   1.1   thorpej 	epstop(sc);
    915   1.1   thorpej 	epinit(sc);
    916   1.1   thorpej 	splx(s);
    917   1.1   thorpej }
    918   1.1   thorpej 
    919   1.1   thorpej void
    920   1.5   thorpej epwatchdog(ifp)
    921   1.5   thorpej 	struct ifnet *ifp;
    922   1.1   thorpej {
    923   1.5   thorpej 	struct ep_softc *sc = ifp->if_softc;
    924   1.1   thorpej 
    925   1.1   thorpej 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
    926   1.1   thorpej 	++sc->sc_arpcom.ac_if.if_oerrors;
    927   1.1   thorpej 
    928   1.1   thorpej 	epreset(sc);
    929   1.1   thorpej }
    930   1.1   thorpej 
    931   1.1   thorpej void
    932   1.1   thorpej epstop(sc)
    933   1.1   thorpej 	register struct ep_softc *sc;
    934   1.1   thorpej {
    935  1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    936  1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    937   1.1   thorpej 
    938  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, RX_DISABLE);
    939  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, RX_DISCARD_TOP_PACK);
    940  1.11   thorpej 	while (bus_space_read_2(iot, ioh, EP_STATUS) & S_COMMAND_IN_PROGRESS)
    941   1.1   thorpej 		;
    942  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, TX_DISABLE);
    943  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, STOP_TRANSCEIVER);
    944  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, RX_RESET);
    945  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, TX_RESET);
    946  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, C_INTR_LATCH);
    947  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, SET_RD_0_MASK);
    948  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, SET_INTR_MASK);
    949  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, SET_RX_FILTER);
    950   1.1   thorpej 
    951   1.1   thorpej 	epmbufempty(sc);
    952   1.1   thorpej }
    953   1.1   thorpej 
    954   1.1   thorpej /*
    955   1.1   thorpej  * We get eeprom data from the id_port given an offset into the
    956   1.1   thorpej  * eeprom.  Basically; after the ID_sequence is sent to all of
    957   1.1   thorpej  * the cards; they enter the ID_CMD state where they will accept
    958   1.1   thorpej  * command requests. 0x80-0xbf loads the eeprom data.  We then
    959   1.1   thorpej  * read the port 16 times and with every read; the cards check
    960   1.1   thorpej  * for contention (ie: if one card writes a 0 bit and another
    961   1.1   thorpej  * writes a 1 bit then the host sees a 0. At the end of the cycle;
    962   1.1   thorpej  * each card compares the data on the bus; if there is a difference
    963   1.1   thorpej  * then that card goes into ID_WAIT state again). In the meantime;
    964   1.1   thorpej  * one bit of data is returned in the AX register which is conveniently
    965  1.11   thorpej  * returned to us by bus_space_read_1().  Hence; we read 16 times getting one
    966   1.1   thorpej  * bit of data with each read.
    967   1.2   thorpej  *
    968   1.2   thorpej  * NOTE: the caller must provide an i/o handle for ELINK_ID_PORT!
    969   1.1   thorpej  */
    970   1.2   thorpej u_int16_t
    971  1.11   thorpej epreadeeprom(iot, ioh, offset)
    972  1.11   thorpej 	bus_space_tag_t iot;
    973  1.11   thorpej 	bus_space_handle_t ioh;
    974   1.2   thorpej 	int offset;
    975   1.1   thorpej {
    976   1.2   thorpej 	u_int16_t data = 0;
    977   1.2   thorpej 	int i;
    978   1.1   thorpej 
    979  1.11   thorpej 	bus_space_write_1(iot, ioh, 0, 0x80 + offset);
    980   1.1   thorpej 	delay(1000);
    981   1.1   thorpej 	for (i = 0; i < 16; i++)
    982  1.11   thorpej 		data = (data << 1) | (bus_space_read_2(iot, ioh, 0) & 1);
    983   1.1   thorpej 	return (data);
    984   1.1   thorpej }
    985   1.1   thorpej 
    986   1.1   thorpej static int
    987   1.1   thorpej epbusyeeprom(sc)
    988   1.1   thorpej 	struct ep_softc *sc;
    989   1.1   thorpej {
    990  1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    991  1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    992   1.1   thorpej 	int i = 100, j;
    993   1.1   thorpej 
    994   1.1   thorpej 	if (sc->bustype == EP_BUS_PCMCIA) {
    995   1.1   thorpej 		delay(1000);
    996   1.1   thorpej 		return 0;
    997   1.1   thorpej 	}
    998   1.1   thorpej 
    999   1.1   thorpej 	while (i--) {
   1000  1.11   thorpej 		j = bus_space_read_2(iot, ioh, EP_W0_EEPROM_COMMAND);
   1001   1.1   thorpej 		if (j & EEPROM_BUSY)
   1002   1.1   thorpej 			delay(100);
   1003   1.1   thorpej 		else
   1004   1.1   thorpej 			break;
   1005   1.1   thorpej 	}
   1006   1.1   thorpej 	if (!i) {
   1007  1.10  christos 		printf("\n%s: eeprom failed to come ready\n",
   1008   1.1   thorpej 		    sc->sc_dev.dv_xname);
   1009   1.1   thorpej 		return (1);
   1010   1.1   thorpej 	}
   1011   1.1   thorpej 	if (j & EEPROM_TST_MODE) {
   1012  1.10  christos 		printf("\n%s: erase pencil mark, or disable plug-n-play mode!\n",
   1013   1.1   thorpej 		    sc->sc_dev.dv_xname);
   1014   1.1   thorpej 		return (1);
   1015   1.1   thorpej 	}
   1016   1.1   thorpej 	return (0);
   1017   1.1   thorpej }
   1018   1.1   thorpej 
   1019   1.1   thorpej void
   1020   1.3  christos epmbuffill(v)
   1021   1.3  christos 	void *v;
   1022   1.1   thorpej {
   1023   1.3  christos 	struct ep_softc *sc = v;
   1024   1.1   thorpej 	int s, i;
   1025   1.1   thorpej 
   1026   1.1   thorpej 	s = splnet();
   1027   1.1   thorpej 	i = sc->last_mb;
   1028   1.1   thorpej 	do {
   1029   1.1   thorpej 		if (sc->mb[i] == NULL)
   1030   1.1   thorpej 			MGET(sc->mb[i], M_DONTWAIT, MT_DATA);
   1031   1.1   thorpej 		if (sc->mb[i] == NULL)
   1032   1.1   thorpej 			break;
   1033   1.1   thorpej 		i = (i + 1) % MAX_MBS;
   1034   1.1   thorpej 	} while (i != sc->next_mb);
   1035   1.1   thorpej 	sc->last_mb = i;
   1036   1.1   thorpej 	/* If the queue was not filled, try again. */
   1037   1.1   thorpej 	if (sc->last_mb != sc->next_mb)
   1038   1.1   thorpej 		timeout(epmbuffill, sc, 1);
   1039   1.1   thorpej 	splx(s);
   1040   1.1   thorpej }
   1041   1.1   thorpej 
   1042   1.1   thorpej void
   1043   1.1   thorpej epmbufempty(sc)
   1044   1.1   thorpej 	struct ep_softc *sc;
   1045   1.1   thorpej {
   1046   1.1   thorpej 	int s, i;
   1047   1.1   thorpej 
   1048   1.1   thorpej 	s = splnet();
   1049   1.1   thorpej 	for (i = 0; i<MAX_MBS; i++) {
   1050   1.1   thorpej 		if (sc->mb[i]) {
   1051   1.1   thorpej 			m_freem(sc->mb[i]);
   1052   1.1   thorpej 			sc->mb[i] = NULL;
   1053   1.1   thorpej 		}
   1054   1.1   thorpej 	}
   1055   1.1   thorpej 	sc->last_mb = sc->next_mb = 0;
   1056   1.1   thorpej 	untimeout(epmbuffill, sc);
   1057   1.1   thorpej 	splx(s);
   1058   1.1   thorpej }
   1059