Home | History | Annotate | Line # | Download | only in ic
seeq8005.c revision 1.6.2.3
      1  1.6.2.3  bouyer /* $NetBSD: seeq8005.c,v 1.6.2.3 2000/11/22 16:03:30 bouyer Exp $ */
      2  1.6.2.2  bouyer 
      3  1.6.2.2  bouyer /*
      4  1.6.2.2  bouyer  * Copyright (c) 2000 Ben Harris
      5  1.6.2.2  bouyer  * Copyright (c) 1995 Mark Brinicombe
      6  1.6.2.2  bouyer  * All rights reserved.
      7  1.6.2.2  bouyer  *
      8  1.6.2.2  bouyer  * Redistribution and use in source and binary forms, with or without
      9  1.6.2.2  bouyer  * modification, are permitted provided that the following conditions
     10  1.6.2.2  bouyer  * are met:
     11  1.6.2.2  bouyer  * 1. Redistributions of source code must retain the above copyright
     12  1.6.2.2  bouyer  *    notice, this list of conditions and the following disclaimer.
     13  1.6.2.2  bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.6.2.2  bouyer  *    notice, this list of conditions and the following disclaimer in the
     15  1.6.2.2  bouyer  *    documentation and/or other materials provided with the distribution.
     16  1.6.2.2  bouyer  * 3. All advertising materials mentioning features or use of this software
     17  1.6.2.2  bouyer  *    must display the following acknowledgement:
     18  1.6.2.2  bouyer  *	This product includes software developed by Mark Brinicombe.
     19  1.6.2.2  bouyer  * 4. The name of the company nor the name of the author may be used to
     20  1.6.2.2  bouyer  *    endorse or promote products derived from this software without specific
     21  1.6.2.2  bouyer  *    prior written permission.
     22  1.6.2.2  bouyer  *
     23  1.6.2.2  bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     24  1.6.2.2  bouyer  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     25  1.6.2.2  bouyer  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  1.6.2.2  bouyer  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     27  1.6.2.2  bouyer  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     28  1.6.2.2  bouyer  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     29  1.6.2.2  bouyer  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.6.2.2  bouyer  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.6.2.2  bouyer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.6.2.2  bouyer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.6.2.2  bouyer  * SUCH DAMAGE.
     34  1.6.2.2  bouyer  */
     35  1.6.2.2  bouyer /*
     36  1.6.2.2  bouyer  * seeq8005.c - SEEQ 8005 device driver
     37  1.6.2.2  bouyer  */
     38  1.6.2.2  bouyer /*
     39  1.6.2.2  bouyer  * This driver currently supports the following chip:
     40  1.6.2.2  bouyer  * SEEQ 8005 Advanced Ethernet Data Link Controller
     41  1.6.2.2  bouyer  */
     42  1.6.2.2  bouyer /*
     43  1.6.2.2  bouyer  * This driver is based on the arm32 ea(4) driver, hence the names of many
     44  1.6.2.2  bouyer  * of the functions.
     45  1.6.2.2  bouyer  */
     46  1.6.2.2  bouyer /*
     47  1.6.2.2  bouyer  * Bugs/possible improvements:
     48  1.6.2.2  bouyer  *	- Does not currently support DMA
     49  1.6.2.2  bouyer  *	- Does not currently support multicasts
     50  1.6.2.2  bouyer  *	- Does not transmit multiple packets in one go
     51  1.6.2.2  bouyer  *	- Does not support big-endian hosts
     52  1.6.2.2  bouyer  *	- Does not support 8-bit busses
     53  1.6.2.2  bouyer  */
     54  1.6.2.2  bouyer 
     55  1.6.2.2  bouyer #include "opt_inet.h"
     56  1.6.2.2  bouyer #include "opt_ns.h"
     57  1.6.2.2  bouyer 
     58  1.6.2.2  bouyer #include <sys/types.h>
     59  1.6.2.2  bouyer #include <sys/param.h>
     60  1.6.2.2  bouyer 
     61  1.6.2.3  bouyer __RCSID("$NetBSD: seeq8005.c,v 1.6.2.3 2000/11/22 16:03:30 bouyer Exp $");
     62  1.6.2.2  bouyer 
     63  1.6.2.2  bouyer #include <sys/systm.h>
     64  1.6.2.2  bouyer #include <sys/endian.h>
     65  1.6.2.2  bouyer #include <sys/errno.h>
     66  1.6.2.2  bouyer #include <sys/ioctl.h>
     67  1.6.2.2  bouyer #include <sys/mbuf.h>
     68  1.6.2.2  bouyer #include <sys/socket.h>
     69  1.6.2.2  bouyer #include <sys/syslog.h>
     70  1.6.2.2  bouyer #include <sys/device.h>
     71  1.6.2.2  bouyer 
     72  1.6.2.2  bouyer #include <net/if.h>
     73  1.6.2.2  bouyer #include <net/if_dl.h>
     74  1.6.2.2  bouyer #include <net/if_types.h>
     75  1.6.2.2  bouyer #include <net/if_ether.h>
     76  1.6.2.2  bouyer 
     77  1.6.2.2  bouyer #ifdef INET
     78  1.6.2.2  bouyer #include <netinet/in.h>
     79  1.6.2.2  bouyer #include <netinet/in_systm.h>
     80  1.6.2.2  bouyer #include <netinet/in_var.h>
     81  1.6.2.2  bouyer #include <netinet/ip.h>
     82  1.6.2.2  bouyer #include <netinet/if_inarp.h>
     83  1.6.2.2  bouyer #endif
     84  1.6.2.2  bouyer 
     85  1.6.2.2  bouyer #ifdef NS
     86  1.6.2.2  bouyer #include <netns/ns.h>
     87  1.6.2.2  bouyer #include <netns/ns_if.h>
     88  1.6.2.2  bouyer #endif
     89  1.6.2.2  bouyer 
     90  1.6.2.2  bouyer #include "bpfilter.h"
     91  1.6.2.2  bouyer #if NBPFILTER > 0
     92  1.6.2.2  bouyer #include <net/bpf.h>
     93  1.6.2.2  bouyer #include <net/bpfdesc.h>
     94  1.6.2.2  bouyer #endif
     95  1.6.2.2  bouyer 
     96  1.6.2.2  bouyer #include <machine/bus.h>
     97  1.6.2.2  bouyer #include <machine/intr.h>
     98  1.6.2.2  bouyer 
     99  1.6.2.2  bouyer #include <dev/ic/seeq8005reg.h>
    100  1.6.2.2  bouyer #include <dev/ic/seeq8005var.h>
    101  1.6.2.2  bouyer 
    102  1.6.2.2  bouyer #ifndef EA_TIMEOUT
    103  1.6.2.2  bouyer #define EA_TIMEOUT	60
    104  1.6.2.2  bouyer #endif
    105  1.6.2.2  bouyer 
    106  1.6.2.2  bouyer #define EA_TX_BUFFER_SIZE	0x4000
    107  1.6.2.2  bouyer #define EA_RX_BUFFER_SIZE	0xC000
    108  1.6.2.2  bouyer 
    109  1.6.2.2  bouyer /*#define EA_TX_DEBUG*/
    110  1.6.2.2  bouyer /*#define EA_RX_DEBUG*/
    111  1.6.2.2  bouyer /*#define EA_DEBUG*/
    112  1.6.2.2  bouyer /*#define EA_PACKET_DEBUG*/
    113  1.6.2.2  bouyer 
    114  1.6.2.2  bouyer /* for debugging convenience */
    115  1.6.2.2  bouyer #ifdef EA_DEBUG
    116  1.6.2.2  bouyer #define dprintf(x) printf x
    117  1.6.2.2  bouyer #else
    118  1.6.2.2  bouyer #define dprintf(x)
    119  1.6.2.2  bouyer #endif
    120  1.6.2.2  bouyer 
    121  1.6.2.2  bouyer /*
    122  1.6.2.2  bouyer  * prototypes
    123  1.6.2.2  bouyer  */
    124  1.6.2.2  bouyer 
    125  1.6.2.3  bouyer static int ea_init(struct ifnet *);
    126  1.6.2.2  bouyer static int ea_ioctl(struct ifnet *, u_long, caddr_t);
    127  1.6.2.2  bouyer static void ea_start(struct ifnet *);
    128  1.6.2.2  bouyer static void ea_watchdog(struct ifnet *);
    129  1.6.2.2  bouyer static void ea_chipreset(struct seeq8005_softc *);
    130  1.6.2.2  bouyer static void ea_ramtest(struct seeq8005_softc *);
    131  1.6.2.2  bouyer static int ea_stoptx(struct seeq8005_softc *);
    132  1.6.2.2  bouyer static int ea_stoprx(struct seeq8005_softc *);
    133  1.6.2.3  bouyer static void ea_stop(struct ifnet *, int);
    134  1.6.2.2  bouyer static void ea_await_fifo_empty(struct seeq8005_softc *);
    135  1.6.2.2  bouyer static void ea_await_fifo_full(struct seeq8005_softc *);
    136  1.6.2.2  bouyer static void ea_writebuf(struct seeq8005_softc *, u_char *, u_int, size_t);
    137  1.6.2.2  bouyer static void ea_readbuf(struct seeq8005_softc *, u_char *, u_int, size_t);
    138  1.6.2.2  bouyer static void ea_select_buffer(struct seeq8005_softc *, int);
    139  1.6.2.3  bouyer static void ea_set_address(struct seeq8005_softc *, int, const u_int8_t *);
    140  1.6.2.2  bouyer static void earead(struct seeq8005_softc *, int, int);
    141  1.6.2.2  bouyer static struct mbuf *eaget(struct seeq8005_softc *, int, int, struct ifnet *);
    142  1.6.2.2  bouyer static void eagetpackets(struct seeq8005_softc *);
    143  1.6.2.2  bouyer static void eatxpacket(struct seeq8005_softc *);
    144  1.6.2.3  bouyer static void ea_mc_reset(struct seeq8005_softc *);
    145  1.6.2.2  bouyer 
    146  1.6.2.2  bouyer 
    147  1.6.2.2  bouyer #ifdef EA_PACKET_DEBUG
    148  1.6.2.2  bouyer void ea_dump_buffer(struct seeq8005_softc *, int);
    149  1.6.2.2  bouyer #endif
    150  1.6.2.2  bouyer 
    151  1.6.2.2  bouyer 
    152  1.6.2.2  bouyer #ifdef EA_PACKET_DEBUG
    153  1.6.2.2  bouyer /*
    154  1.6.2.2  bouyer  * Dump the interface buffer
    155  1.6.2.2  bouyer  */
    156  1.6.2.2  bouyer 
    157  1.6.2.2  bouyer void
    158  1.6.2.2  bouyer ea_dump_buffer(struct seeq8005_softc *sc, u_int offset)
    159  1.6.2.2  bouyer {
    160  1.6.2.2  bouyer 	bus_space_tag_t iot = sc->sc_iot;
    161  1.6.2.2  bouyer 	bus_space_handle_t ioh = sc->sc_ioh;
    162  1.6.2.2  bouyer 	u_int addr;
    163  1.6.2.3  bouyer 	int loop, ctrl, ptr;
    164  1.6.2.2  bouyer 	size_t size;
    165  1.6.2.2  bouyer 
    166  1.6.2.2  bouyer 	addr = offset;
    167  1.6.2.2  bouyer 
    168  1.6.2.2  bouyer 	do {
    169  1.6.2.2  bouyer 		bus_space_write_2(iot, ioh, EA_8005_COMMAND,
    170  1.6.2.2  bouyer 				 sc->sc_command | EA_CMD_FIFO_READ);
    171  1.6.2.2  bouyer 		bus_space_write_2(iot, ioh, EA_8005_CONFIG1,
    172  1.6.2.2  bouyer 				  sc->sc_config1 | EA_BUFCODE_LOCAL_MEM);
    173  1.6.2.2  bouyer 		bus_space_write_2(iot, ioh, EA_8005_DMA_ADDR, addr);
    174  1.6.2.2  bouyer 
    175  1.6.2.2  bouyer 		ptr = bus_space_read_2(iot, ioh, EA_8005_BUFWIN);
    176  1.6.2.2  bouyer 		ctrl = bus_space_read_2(iot, ioh, EA_8005_BUFWIN);
    177  1.6.2.2  bouyer 		ptr = ((ptr & 0xff) << 8) | ((ptr >> 8) & 0xff);
    178  1.6.2.2  bouyer 
    179  1.6.2.2  bouyer 		if (ptr == 0) break;
    180  1.6.2.2  bouyer 		size = ptr - addr;
    181  1.6.2.2  bouyer 
    182  1.6.2.2  bouyer 		printf("addr=%04x size=%04x ", addr, size);
    183  1.6.2.2  bouyer 		printf("cmd=%02x st=%02x\n", ctrl & 0xff, ctrl >> 8);
    184  1.6.2.2  bouyer 
    185  1.6.2.2  bouyer 		for (loop = 0; loop < size - 4; loop += 2)
    186  1.6.2.2  bouyer 			printf("%04x ",
    187  1.6.2.2  bouyer 			       bus_space_read_2(iot, ioh, EA_8005_BUFWIN));
    188  1.6.2.2  bouyer 		printf("\n");
    189  1.6.2.2  bouyer 		addr = ptr;
    190  1.6.2.2  bouyer 	} while (size != 0);
    191  1.6.2.2  bouyer }
    192  1.6.2.2  bouyer #endif
    193  1.6.2.2  bouyer 
    194  1.6.2.2  bouyer /*
    195  1.6.2.2  bouyer  * Attach chip.
    196  1.6.2.2  bouyer  */
    197  1.6.2.2  bouyer 
    198  1.6.2.2  bouyer void
    199  1.6.2.2  bouyer seeq8005_attach(struct seeq8005_softc *sc, const u_int8_t *myaddr)
    200  1.6.2.2  bouyer {
    201  1.6.2.2  bouyer 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    202  1.6.2.2  bouyer 	u_int id;
    203  1.6.2.2  bouyer 
    204  1.6.2.2  bouyer 	printf(" address %s", ether_sprintf(myaddr));
    205  1.6.2.2  bouyer 
    206  1.6.2.2  bouyer 	/* Stop the board. */
    207  1.6.2.2  bouyer 
    208  1.6.2.2  bouyer 	ea_chipreset(sc);
    209  1.6.2.2  bouyer 
    210  1.6.2.2  bouyer 	/* Get the product ID */
    211  1.6.2.2  bouyer 
    212  1.6.2.2  bouyer 	ea_select_buffer(sc, EA_BUFCODE_PRODUCTID);
    213  1.6.2.2  bouyer 	id = bus_space_read_2(sc->sc_iot, sc->sc_ioh, EA_8005_BUFWIN);
    214  1.6.2.2  bouyer 
    215  1.6.2.2  bouyer 	if ((id & 0xf0) == 0xa0) {
    216  1.6.2.2  bouyer 		sc->sc_flags |= SEEQ8005_80C04;
    217  1.6.2.2  bouyer 		printf(", SEEQ 80C04 rev %02x", id);
    218  1.6.2.2  bouyer 	} else
    219  1.6.2.2  bouyer 		printf(", SEEQ 8005");
    220  1.6.2.2  bouyer 
    221  1.6.2.2  bouyer 	/* Initialise ifnet structure. */
    222  1.6.2.2  bouyer 
    223  1.6.2.2  bouyer 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    224  1.6.2.2  bouyer 	ifp->if_softc = sc;
    225  1.6.2.2  bouyer 	ifp->if_start = ea_start;
    226  1.6.2.2  bouyer 	ifp->if_ioctl = ea_ioctl;
    227  1.6.2.3  bouyer 	ifp->if_init = ea_init;
    228  1.6.2.3  bouyer 	ifp->if_stop = ea_stop;
    229  1.6.2.2  bouyer 	ifp->if_watchdog = ea_watchdog;
    230  1.6.2.3  bouyer 	ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST | IFF_NOTRAILERS;
    231  1.6.2.2  bouyer 
    232  1.6.2.2  bouyer 	/* Now we can attach the interface. */
    233  1.6.2.2  bouyer 
    234  1.6.2.2  bouyer 	if_attach(ifp);
    235  1.6.2.2  bouyer 	ether_ifattach(ifp, myaddr);
    236  1.6.2.2  bouyer 
    237  1.6.2.2  bouyer 	printf("\n");
    238  1.6.2.2  bouyer 
    239  1.6.2.2  bouyer 	/* Test the RAM */
    240  1.6.2.2  bouyer 	ea_ramtest(sc);
    241  1.6.2.2  bouyer }
    242  1.6.2.2  bouyer 
    243  1.6.2.2  bouyer 
    244  1.6.2.2  bouyer /*
    245  1.6.2.2  bouyer  * Test the RAM on the ethernet card.
    246  1.6.2.2  bouyer  */
    247  1.6.2.2  bouyer 
    248  1.6.2.2  bouyer void
    249  1.6.2.2  bouyer ea_ramtest(struct seeq8005_softc *sc)
    250  1.6.2.2  bouyer {
    251  1.6.2.2  bouyer 	bus_space_tag_t iot = sc->sc_iot;
    252  1.6.2.2  bouyer 	bus_space_handle_t ioh = sc->sc_ioh;
    253  1.6.2.2  bouyer 	int loop;
    254  1.6.2.2  bouyer 	u_int sum = 0;
    255  1.6.2.2  bouyer 
    256  1.6.2.2  bouyer /*	dprintf(("ea_ramtest()\n"));*/
    257  1.6.2.2  bouyer 
    258  1.6.2.2  bouyer 	/*
    259  1.6.2.2  bouyer 	 * Test the buffer memory on the board.
    260  1.6.2.2  bouyer 	 * Write simple pattens to it and read them back.
    261  1.6.2.2  bouyer 	 */
    262  1.6.2.2  bouyer 
    263  1.6.2.2  bouyer 	/* Set up the whole buffer RAM for writing */
    264  1.6.2.2  bouyer 
    265  1.6.2.2  bouyer 	ea_select_buffer(sc, EA_BUFCODE_TX_EAP);
    266  1.6.2.2  bouyer 	bus_space_write_2(iot, ioh, EA_8005_BUFWIN, (EA_BUFFER_SIZE >> 8) - 1);
    267  1.6.2.2  bouyer 	bus_space_write_2(iot, ioh, EA_8005_TX_PTR, 0x0000);
    268  1.6.2.2  bouyer 	bus_space_write_2(iot, ioh, EA_8005_RX_PTR, EA_BUFFER_SIZE - 2);
    269  1.6.2.2  bouyer 
    270  1.6.2.2  bouyer #define EA_RAMTEST_LOOP(value)						\
    271  1.6.2.2  bouyer do {									\
    272  1.6.2.2  bouyer 	/* Set the write start address and write a pattern */		\
    273  1.6.2.2  bouyer 	ea_writebuf(sc, NULL, 0x0000, 0);				\
    274  1.6.2.2  bouyer 	for (loop = 0; loop < EA_BUFFER_SIZE; loop += 2)		\
    275  1.6.2.2  bouyer 		bus_space_write_2(iot, ioh, EA_8005_BUFWIN, (value));	\
    276  1.6.2.2  bouyer 									\
    277  1.6.2.2  bouyer 	/* Set the read start address and verify the pattern */		\
    278  1.6.2.2  bouyer 	ea_readbuf(sc, NULL, 0x0000, 0);				\
    279  1.6.2.2  bouyer 	for (loop = 0; loop < EA_BUFFER_SIZE; loop += 2)		\
    280  1.6.2.2  bouyer 		if (bus_space_read_2(iot, ioh, EA_8005_BUFWIN) != (value)) \
    281  1.6.2.2  bouyer 			++sum;						\
    282  1.6.2.2  bouyer 	if (sum != 0)							\
    283  1.6.2.2  bouyer 		dprintf(("sum=%d\n", sum));				\
    284  1.6.2.2  bouyer } while (/*CONSTCOND*/0)
    285  1.6.2.2  bouyer 
    286  1.6.2.2  bouyer 	EA_RAMTEST_LOOP(loop);
    287  1.6.2.2  bouyer 	EA_RAMTEST_LOOP(loop ^ (EA_BUFFER_SIZE - 1));
    288  1.6.2.2  bouyer 	EA_RAMTEST_LOOP(0xaa55);
    289  1.6.2.2  bouyer 	EA_RAMTEST_LOOP(0x55aa);
    290  1.6.2.2  bouyer 
    291  1.6.2.2  bouyer 	/* Report */
    292  1.6.2.2  bouyer 
    293  1.6.2.2  bouyer 	if (sum > 0)
    294  1.6.2.2  bouyer 		printf("%s: buffer RAM failed self test, %d faults\n",
    295  1.6.2.2  bouyer 		       sc->sc_dev.dv_xname, sum);
    296  1.6.2.2  bouyer }
    297  1.6.2.2  bouyer 
    298  1.6.2.2  bouyer 
    299  1.6.2.2  bouyer /*
    300  1.6.2.2  bouyer  * Stop the tx interface.
    301  1.6.2.2  bouyer  *
    302  1.6.2.2  bouyer  * Returns 0 if the tx was already stopped or 1 if it was active
    303  1.6.2.2  bouyer  */
    304  1.6.2.2  bouyer 
    305  1.6.2.2  bouyer static int
    306  1.6.2.2  bouyer ea_stoptx(struct seeq8005_softc *sc)
    307  1.6.2.2  bouyer {
    308  1.6.2.2  bouyer 	bus_space_tag_t iot = sc->sc_iot;
    309  1.6.2.2  bouyer 	bus_space_handle_t ioh = sc->sc_ioh;
    310  1.6.2.2  bouyer 	int timeout;
    311  1.6.2.2  bouyer 	int status;
    312  1.6.2.2  bouyer 
    313  1.6.2.2  bouyer 	dprintf(("ea_stoptx()\n"));
    314  1.6.2.2  bouyer 
    315  1.6.2.2  bouyer 	status = bus_space_read_2(iot, ioh, EA_8005_STATUS);
    316  1.6.2.2  bouyer 	if (!(status & EA_STATUS_TX_ON))
    317  1.6.2.2  bouyer 		return 0;
    318  1.6.2.2  bouyer 
    319  1.6.2.2  bouyer 	/* Stop any tx and wait for confirmation */
    320  1.6.2.2  bouyer 	bus_space_write_2(iot, ioh, EA_8005_COMMAND,
    321  1.6.2.2  bouyer 			  sc->sc_command | EA_CMD_TX_OFF);
    322  1.6.2.2  bouyer 
    323  1.6.2.2  bouyer 	timeout = 20000;
    324  1.6.2.2  bouyer 	do {
    325  1.6.2.2  bouyer 		status = bus_space_read_2(iot, ioh, EA_8005_STATUS);
    326  1.6.2.2  bouyer 	} while ((status & EA_STATUS_TX_ON) && --timeout > 0);
    327  1.6.2.2  bouyer 	if (timeout == 0)
    328  1.6.2.2  bouyer 		dprintf(("ea_stoptx: timeout waiting for tx termination\n"));
    329  1.6.2.2  bouyer 
    330  1.6.2.2  bouyer 	/* Clear any pending tx interrupt */
    331  1.6.2.2  bouyer 	bus_space_write_2(iot, ioh, EA_8005_COMMAND,
    332  1.6.2.2  bouyer 		   sc->sc_command | EA_CMD_TX_INTACK);
    333  1.6.2.2  bouyer 	return 1;
    334  1.6.2.2  bouyer }
    335  1.6.2.2  bouyer 
    336  1.6.2.2  bouyer 
    337  1.6.2.2  bouyer /*
    338  1.6.2.2  bouyer  * Stop the rx interface.
    339  1.6.2.2  bouyer  *
    340  1.6.2.2  bouyer  * Returns 0 if the tx was already stopped or 1 if it was active
    341  1.6.2.2  bouyer  */
    342  1.6.2.2  bouyer 
    343  1.6.2.2  bouyer static int
    344  1.6.2.2  bouyer ea_stoprx(struct seeq8005_softc *sc)
    345  1.6.2.2  bouyer {
    346  1.6.2.2  bouyer 	bus_space_tag_t iot = sc->sc_iot;
    347  1.6.2.2  bouyer 	bus_space_handle_t ioh = sc->sc_ioh;
    348  1.6.2.2  bouyer 	int timeout;
    349  1.6.2.2  bouyer 	int status;
    350  1.6.2.2  bouyer 
    351  1.6.2.2  bouyer 	dprintf(("ea_stoprx()\n"));
    352  1.6.2.2  bouyer 
    353  1.6.2.2  bouyer 	status = bus_space_read_2(iot, ioh, EA_8005_STATUS);
    354  1.6.2.2  bouyer 	if (!(status & EA_STATUS_RX_ON))
    355  1.6.2.2  bouyer 		return 0;
    356  1.6.2.2  bouyer 
    357  1.6.2.2  bouyer 	/* Stop any rx and wait for confirmation */
    358  1.6.2.2  bouyer 
    359  1.6.2.2  bouyer 	bus_space_write_2(iot, ioh, EA_8005_COMMAND,
    360  1.6.2.2  bouyer 			  sc->sc_command | EA_CMD_RX_OFF);
    361  1.6.2.2  bouyer 
    362  1.6.2.2  bouyer 	timeout = 20000;
    363  1.6.2.2  bouyer 	do {
    364  1.6.2.2  bouyer 		status = bus_space_read_2(iot, ioh, EA_8005_STATUS);
    365  1.6.2.2  bouyer 	} while ((status & EA_STATUS_RX_ON) && --timeout > 0);
    366  1.6.2.2  bouyer 	if (timeout == 0)
    367  1.6.2.2  bouyer 		dprintf(("ea_stoprx: timeout waiting for rx termination\n"));
    368  1.6.2.2  bouyer 
    369  1.6.2.2  bouyer 	/* Clear any pending rx interrupt */
    370  1.6.2.2  bouyer 
    371  1.6.2.2  bouyer 	bus_space_write_2(iot, ioh, EA_8005_COMMAND,
    372  1.6.2.2  bouyer 		   sc->sc_command | EA_CMD_RX_INTACK);
    373  1.6.2.2  bouyer 	return 1;
    374  1.6.2.2  bouyer }
    375  1.6.2.2  bouyer 
    376  1.6.2.2  bouyer 
    377  1.6.2.2  bouyer /*
    378  1.6.2.2  bouyer  * Stop interface.
    379  1.6.2.2  bouyer  * Stop all IO and shut the interface down
    380  1.6.2.2  bouyer  */
    381  1.6.2.2  bouyer 
    382  1.6.2.2  bouyer static void
    383  1.6.2.3  bouyer ea_stop(struct ifnet *ifp, int disable)
    384  1.6.2.2  bouyer {
    385  1.6.2.3  bouyer 	struct seeq8005_softc *sc = ifp->if_softc;
    386  1.6.2.2  bouyer 	bus_space_tag_t iot = sc->sc_iot;
    387  1.6.2.2  bouyer 	bus_space_handle_t ioh = sc->sc_ioh;
    388  1.6.2.2  bouyer 
    389  1.6.2.2  bouyer 	dprintf(("ea_stop()\n"));
    390  1.6.2.2  bouyer 
    391  1.6.2.2  bouyer 	/* Stop all IO */
    392  1.6.2.2  bouyer 	ea_stoptx(sc);
    393  1.6.2.2  bouyer 	ea_stoprx(sc);
    394  1.6.2.2  bouyer 
    395  1.6.2.2  bouyer 	/* Disable rx and tx interrupts */
    396  1.6.2.2  bouyer 	sc->sc_command &= (EA_CMD_RX_INTEN | EA_CMD_TX_INTEN);
    397  1.6.2.2  bouyer 
    398  1.6.2.2  bouyer 	/* Clear any pending interrupts */
    399  1.6.2.2  bouyer 	bus_space_write_2(iot, ioh, EA_8005_COMMAND,
    400  1.6.2.2  bouyer 			  sc->sc_command | EA_CMD_RX_INTACK |
    401  1.6.2.2  bouyer 			  EA_CMD_TX_INTACK | EA_CMD_DMA_INTACK |
    402  1.6.2.2  bouyer 			  EA_CMD_BW_INTACK);
    403  1.6.2.2  bouyer 	dprintf(("st=%08x", bus_space_read_2(iot, ioh, EA_8005_STATUS)));
    404  1.6.2.2  bouyer 
    405  1.6.2.2  bouyer 	/* Cancel any watchdog timer */
    406  1.6.2.2  bouyer        	sc->sc_ethercom.ec_if.if_timer = 0;
    407  1.6.2.2  bouyer }
    408  1.6.2.2  bouyer 
    409  1.6.2.2  bouyer 
    410  1.6.2.2  bouyer /*
    411  1.6.2.2  bouyer  * Reset the chip
    412  1.6.2.2  bouyer  * Following this the software registers are reset
    413  1.6.2.2  bouyer  */
    414  1.6.2.2  bouyer 
    415  1.6.2.2  bouyer static void
    416  1.6.2.2  bouyer ea_chipreset(struct seeq8005_softc *sc)
    417  1.6.2.2  bouyer {
    418  1.6.2.2  bouyer 	bus_space_tag_t iot = sc->sc_iot;
    419  1.6.2.2  bouyer 	bus_space_handle_t ioh = sc->sc_ioh;
    420  1.6.2.2  bouyer 
    421  1.6.2.2  bouyer 	dprintf(("ea_chipreset()\n"));
    422  1.6.2.2  bouyer 
    423  1.6.2.2  bouyer 	/* Reset the controller. Min of 4us delay here */
    424  1.6.2.2  bouyer 
    425  1.6.2.2  bouyer 	bus_space_write_2(iot, ioh, EA_8005_CONFIG2, EA_CFG2_RESET);
    426  1.6.2.2  bouyer 	delay(4);
    427  1.6.2.2  bouyer 
    428  1.6.2.2  bouyer 	sc->sc_command = 0;
    429  1.6.2.2  bouyer 	sc->sc_config1 = 0;
    430  1.6.2.2  bouyer 	sc->sc_config2 = 0;
    431  1.6.2.2  bouyer }
    432  1.6.2.2  bouyer 
    433  1.6.2.2  bouyer 
    434  1.6.2.2  bouyer /*
    435  1.6.2.2  bouyer  * If the DMA FIFO's in write mode, wait for it to empty.  Needed when
    436  1.6.2.2  bouyer  * switching the FIFO from write to read.  We also use it when changing
    437  1.6.2.2  bouyer  * the address for writes.
    438  1.6.2.2  bouyer  */
    439  1.6.2.2  bouyer static void
    440  1.6.2.2  bouyer ea_await_fifo_empty(struct seeq8005_softc *sc)
    441  1.6.2.2  bouyer {
    442  1.6.2.2  bouyer 	bus_space_tag_t iot = sc->sc_iot;
    443  1.6.2.2  bouyer 	bus_space_handle_t ioh = sc->sc_ioh;
    444  1.6.2.2  bouyer 	int timeout;
    445  1.6.2.2  bouyer 
    446  1.6.2.2  bouyer 	timeout = 20000;
    447  1.6.2.2  bouyer 	if ((bus_space_read_2(iot, ioh, EA_8005_STATUS) &
    448  1.6.2.2  bouyer 	     EA_STATUS_FIFO_DIR) != 0)
    449  1.6.2.2  bouyer 		return; /* FIFO is reading anyway. */
    450  1.6.2.2  bouyer 	while ((bus_space_read_2(iot, ioh, EA_8005_STATUS) &
    451  1.6.2.2  bouyer 		EA_STATUS_FIFO_EMPTY) == 0 &&
    452  1.6.2.2  bouyer 	       --timeout > 0)
    453  1.6.2.2  bouyer 		continue;
    454  1.6.2.2  bouyer }
    455  1.6.2.2  bouyer 
    456  1.6.2.2  bouyer /*
    457  1.6.2.2  bouyer  * Wait for the DMA FIFO to fill before reading from it.
    458  1.6.2.2  bouyer  */
    459  1.6.2.2  bouyer static void
    460  1.6.2.2  bouyer ea_await_fifo_full(struct seeq8005_softc *sc)
    461  1.6.2.2  bouyer {
    462  1.6.2.2  bouyer 	bus_space_tag_t iot = sc->sc_iot;
    463  1.6.2.2  bouyer 	bus_space_handle_t ioh = sc->sc_ioh;
    464  1.6.2.2  bouyer 	int timeout;
    465  1.6.2.2  bouyer 
    466  1.6.2.2  bouyer 	timeout = 20000;
    467  1.6.2.2  bouyer 	while ((bus_space_read_2(iot, ioh, EA_8005_STATUS) &
    468  1.6.2.2  bouyer 		EA_STATUS_FIFO_FULL) == 0 &&
    469  1.6.2.2  bouyer 	       --timeout > 0)
    470  1.6.2.2  bouyer 		continue;
    471  1.6.2.2  bouyer }
    472  1.6.2.2  bouyer 
    473  1.6.2.2  bouyer /*
    474  1.6.2.2  bouyer  * write to the buffer memory on the interface
    475  1.6.2.2  bouyer  *
    476  1.6.2.2  bouyer  * The buffer address is set to ADDR.
    477  1.6.2.2  bouyer  * If len != 0 then data is copied from the address starting at buf
    478  1.6.2.2  bouyer  * to the interface buffer.
    479  1.6.2.2  bouyer  * BUF must be usable as a u_int16_t *.
    480  1.6.2.2  bouyer  * If LEN is odd, it must be safe to overwrite one extra byte.
    481  1.6.2.2  bouyer  */
    482  1.6.2.2  bouyer 
    483  1.6.2.2  bouyer static void
    484  1.6.2.2  bouyer ea_writebuf(struct seeq8005_softc *sc, u_char *buf, u_int addr, size_t len)
    485  1.6.2.2  bouyer {
    486  1.6.2.2  bouyer 	bus_space_tag_t iot = sc->sc_iot;
    487  1.6.2.2  bouyer 	bus_space_handle_t ioh = sc->sc_ioh;
    488  1.6.2.2  bouyer 
    489  1.6.2.2  bouyer 	dprintf(("writebuf: st=%04x\n",
    490  1.6.2.2  bouyer 		 bus_space_read_2(iot, ioh, EA_8005_STATUS)));
    491  1.6.2.2  bouyer 
    492  1.6.2.2  bouyer #ifdef DIAGNOSTIC
    493  1.6.2.2  bouyer 	if (__predict_false(!ALIGNED_POINTER(buf, u_int16_t)))
    494  1.6.2.2  bouyer 		panic("%s: unaligned writebuf", sc->sc_dev.dv_xname);
    495  1.6.2.2  bouyer #endif
    496  1.6.2.2  bouyer 	if (__predict_false(addr >= EA_BUFFER_SIZE))
    497  1.6.2.2  bouyer 		panic("%s: writebuf out of range", sc->sc_dev.dv_xname);
    498  1.6.2.2  bouyer 
    499  1.6.2.2  bouyer 	/* Assume that copying too much is safe. */
    500  1.6.2.2  bouyer 	if (len % 2 != 0)
    501  1.6.2.2  bouyer 		len++;
    502  1.6.2.2  bouyer 
    503  1.6.2.2  bouyer 	ea_await_fifo_empty(sc);
    504  1.6.2.2  bouyer 
    505  1.6.2.2  bouyer 	ea_select_buffer(sc, EA_BUFCODE_LOCAL_MEM);
    506  1.6.2.2  bouyer 	bus_space_write_2(iot, ioh, EA_8005_COMMAND,
    507  1.6.2.2  bouyer 			  sc->sc_command | EA_CMD_FIFO_WRITE);
    508  1.6.2.2  bouyer        	bus_space_write_2(iot, ioh, EA_8005_DMA_ADDR, addr);
    509  1.6.2.2  bouyer 
    510  1.6.2.2  bouyer 	if (len > 0)
    511  1.6.2.2  bouyer 		bus_space_write_multi_2(iot, ioh, EA_8005_BUFWIN,
    512  1.6.2.2  bouyer 					(u_int16_t *)buf, len / 2);
    513  1.6.2.2  bouyer 	/* Leave FIFO to empty in the background */
    514  1.6.2.2  bouyer }
    515  1.6.2.2  bouyer 
    516  1.6.2.2  bouyer 
    517  1.6.2.2  bouyer /*
    518  1.6.2.2  bouyer  * read from the buffer memory on the interface
    519  1.6.2.2  bouyer  *
    520  1.6.2.2  bouyer  * The buffer address is set to ADDR.
    521  1.6.2.2  bouyer  * If len != 0 then data is copied from the interface buffer to the
    522  1.6.2.2  bouyer  * address starting at buf.
    523  1.6.2.2  bouyer  * BUF must be usable as a u_int16_t *.
    524  1.6.2.2  bouyer  * If LEN is odd, it must be safe to overwrite one extra byte.
    525  1.6.2.2  bouyer  */
    526  1.6.2.2  bouyer 
    527  1.6.2.2  bouyer static void
    528  1.6.2.2  bouyer ea_readbuf(struct seeq8005_softc *sc, u_char *buf, u_int addr, size_t len)
    529  1.6.2.2  bouyer {
    530  1.6.2.2  bouyer 
    531  1.6.2.2  bouyer 	bus_space_tag_t iot = sc->sc_iot;
    532  1.6.2.2  bouyer 	bus_space_handle_t ioh = sc->sc_ioh;
    533  1.6.2.2  bouyer 
    534  1.6.2.2  bouyer 	dprintf(("readbuf: st=%04x addr=%04x len=%d\n",
    535  1.6.2.2  bouyer 		 bus_space_read_2(iot, ioh, EA_8005_STATUS), addr, len));
    536  1.6.2.2  bouyer 
    537  1.6.2.2  bouyer #ifdef DIAGNOSTIC
    538  1.6.2.2  bouyer 	if (!ALIGNED_POINTER(buf, u_int16_t))
    539  1.6.2.2  bouyer 		panic("%s: unaligned readbuf", sc->sc_dev.dv_xname);
    540  1.6.2.2  bouyer #endif
    541  1.6.2.2  bouyer 	if (addr >= EA_BUFFER_SIZE)
    542  1.6.2.2  bouyer 		panic("%s: writebuf out of range", sc->sc_dev.dv_xname);
    543  1.6.2.2  bouyer 
    544  1.6.2.2  bouyer 	/* Assume that copying too much is safe. */
    545  1.6.2.2  bouyer 	if (len % 2 != 0)
    546  1.6.2.2  bouyer 		len++;
    547  1.6.2.2  bouyer 
    548  1.6.2.2  bouyer 	ea_await_fifo_empty(sc);
    549  1.6.2.2  bouyer 
    550  1.6.2.2  bouyer 	ea_select_buffer(sc, EA_BUFCODE_LOCAL_MEM);
    551  1.6.2.2  bouyer 	bus_space_write_2(iot, ioh, EA_8005_DMA_ADDR, addr);
    552  1.6.2.2  bouyer 	bus_space_write_2(iot, ioh, EA_8005_COMMAND,
    553  1.6.2.2  bouyer 			  sc->sc_command | EA_CMD_FIFO_READ);
    554  1.6.2.2  bouyer 
    555  1.6.2.2  bouyer 	ea_await_fifo_full(sc);
    556  1.6.2.2  bouyer 
    557  1.6.2.2  bouyer 	if (len > 0)
    558  1.6.2.2  bouyer 		bus_space_read_multi_2(iot, ioh, EA_8005_BUFWIN,
    559  1.6.2.2  bouyer 				       (u_int16_t *)buf, len / 2);
    560  1.6.2.2  bouyer }
    561  1.6.2.2  bouyer 
    562  1.6.2.2  bouyer static void
    563  1.6.2.2  bouyer ea_select_buffer(struct seeq8005_softc *sc, int bufcode)
    564  1.6.2.2  bouyer {
    565  1.6.2.2  bouyer 
    566  1.6.2.2  bouyer 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, EA_8005_CONFIG1,
    567  1.6.2.2  bouyer 			  sc->sc_config1 | bufcode);
    568  1.6.2.2  bouyer }
    569  1.6.2.2  bouyer 
    570  1.6.2.3  bouyer /* Must be called at splnet */
    571  1.6.2.3  bouyer static void
    572  1.6.2.3  bouyer ea_set_address(struct seeq8005_softc *sc, int which, u_int8_t const *ea)
    573  1.6.2.3  bouyer {
    574  1.6.2.3  bouyer 	int i;
    575  1.6.2.3  bouyer 
    576  1.6.2.3  bouyer 	ea_select_buffer(sc, EA_BUFCODE_STATION_ADDR0 + which);
    577  1.6.2.3  bouyer 	for (i = 0; i < ETHER_ADDR_LEN; ++i)
    578  1.6.2.3  bouyer 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, EA_8005_BUFWIN,
    579  1.6.2.3  bouyer 				  ea[i]);
    580  1.6.2.3  bouyer }
    581  1.6.2.3  bouyer 
    582  1.6.2.2  bouyer /*
    583  1.6.2.2  bouyer  * Initialize interface.
    584  1.6.2.2  bouyer  *
    585  1.6.2.2  bouyer  * This should leave the interface in a state for packet reception and
    586  1.6.2.2  bouyer  * transmission.
    587  1.6.2.2  bouyer  */
    588  1.6.2.2  bouyer 
    589  1.6.2.2  bouyer static int
    590  1.6.2.3  bouyer ea_init(struct ifnet *ifp)
    591  1.6.2.2  bouyer {
    592  1.6.2.3  bouyer 	struct seeq8005_softc *sc = ifp->if_softc;
    593  1.6.2.2  bouyer 	bus_space_tag_t iot = sc->sc_iot;
    594  1.6.2.2  bouyer 	bus_space_handle_t ioh = sc->sc_ioh;
    595  1.6.2.3  bouyer 	int s;
    596  1.6.2.2  bouyer 
    597  1.6.2.2  bouyer 	dprintf(("ea_init()\n"));
    598  1.6.2.2  bouyer 
    599  1.6.2.2  bouyer 	s = splnet();
    600  1.6.2.2  bouyer 
    601  1.6.2.2  bouyer 	/* First, reset the board. */
    602  1.6.2.2  bouyer 
    603  1.6.2.2  bouyer 	ea_chipreset(sc);
    604  1.6.2.2  bouyer 
    605  1.6.2.2  bouyer 	/* Set up defaults for the registers */
    606  1.6.2.2  bouyer 
    607  1.6.2.2  bouyer 	sc->sc_command = 0x00;
    608  1.6.2.2  bouyer 	sc->sc_config1 = 0x00; /* XXX DMA settings? */
    609  1.6.2.2  bouyer #if BYTE_ORDER == BIG_ENDIAN
    610  1.6.2.2  bouyer 	sc->sc_config2 = EA_CFG2_BYTESWAP
    611  1.6.2.2  bouyer #else
    612  1.6.2.2  bouyer 	sc->sc_config2 = 0;
    613  1.6.2.2  bouyer #endif
    614  1.6.2.2  bouyer 
    615  1.6.2.2  bouyer 	bus_space_write_2(iot, ioh, EA_8005_COMMAND, sc->sc_command);
    616  1.6.2.2  bouyer 	bus_space_write_2(iot, ioh, EA_8005_CONFIG1, sc->sc_config1);
    617  1.6.2.2  bouyer 	bus_space_write_2(iot, ioh, EA_8005_CONFIG2, sc->sc_config2);
    618  1.6.2.2  bouyer 
    619  1.6.2.2  bouyer 	/* Split board memory into Rx and Tx. */
    620  1.6.2.2  bouyer 	ea_select_buffer(sc, EA_BUFCODE_TX_EAP);
    621  1.6.2.2  bouyer 	bus_space_write_2(iot, ioh, EA_8005_BUFWIN,
    622  1.6.2.2  bouyer 			  (EA_TX_BUFFER_SIZE >> 8) - 1);
    623  1.6.2.2  bouyer 
    624  1.6.2.2  bouyer 	/* Write the station address - the receiver must be off */
    625  1.6.2.3  bouyer 	ea_set_address(sc, 0, LLADDR(ifp->if_sadl));
    626  1.6.2.2  bouyer 
    627  1.6.2.2  bouyer 	/* Configure rx. */
    628  1.6.2.2  bouyer 	dprintf(("Configuring rx...\n"));
    629  1.6.2.2  bouyer 	if (ifp->if_flags & IFF_PROMISC)
    630  1.6.2.2  bouyer 		sc->sc_config1 = EA_CFG1_PROMISCUOUS;
    631  1.6.2.2  bouyer 	else
    632  1.6.2.2  bouyer 		sc->sc_config1 = EA_CFG1_BROADCAST;
    633  1.6.2.2  bouyer 	sc->sc_config1 |= EA_CFG1_STATION_ADDR0;
    634  1.6.2.2  bouyer 	bus_space_write_2(iot, ioh, EA_8005_CONFIG1, sc->sc_config1);
    635  1.6.2.2  bouyer 
    636  1.6.2.2  bouyer 	/* Setup the Rx pointers */
    637  1.6.2.2  bouyer 	sc->sc_rx_ptr = EA_TX_BUFFER_SIZE;
    638  1.6.2.2  bouyer 
    639  1.6.2.2  bouyer 	bus_space_write_2(iot, ioh, EA_8005_RX_PTR, sc->sc_rx_ptr);
    640  1.6.2.2  bouyer 	bus_space_write_2(iot, ioh, EA_8005_RX_END, sc->sc_rx_ptr >> 8);
    641  1.6.2.2  bouyer 
    642  1.6.2.2  bouyer 
    643  1.6.2.2  bouyer 	/* Place a NULL header at the beginning of the receive area */
    644  1.6.2.2  bouyer 	ea_writebuf(sc, NULL, sc->sc_rx_ptr, 0);
    645  1.6.2.2  bouyer 
    646  1.6.2.2  bouyer 	bus_space_write_2(iot, ioh, EA_8005_BUFWIN, 0x0000);
    647  1.6.2.2  bouyer 	bus_space_write_2(iot, ioh, EA_8005_BUFWIN, 0x0000);
    648  1.6.2.2  bouyer 
    649  1.6.2.2  bouyer 
    650  1.6.2.2  bouyer 	/* Turn on Rx */
    651  1.6.2.2  bouyer 	sc->sc_command |= EA_CMD_RX_INTEN;
    652  1.6.2.2  bouyer 	bus_space_write_2(iot, ioh, EA_8005_COMMAND,
    653  1.6.2.2  bouyer 			  sc->sc_command | EA_CMD_RX_ON);
    654  1.6.2.2  bouyer 
    655  1.6.2.2  bouyer 
    656  1.6.2.2  bouyer 	/* Configure TX. */
    657  1.6.2.2  bouyer 	dprintf(("Configuring tx...\n"));
    658  1.6.2.2  bouyer 
    659  1.6.2.2  bouyer 	bus_space_write_2(iot, ioh, EA_8005_TX_PTR, 0x0000);
    660  1.6.2.2  bouyer 
    661  1.6.2.2  bouyer 	sc->sc_config2 |= EA_CFG2_OUTPUT;
    662  1.6.2.2  bouyer 	bus_space_write_2(iot, ioh, EA_8005_CONFIG2, sc->sc_config2);
    663  1.6.2.2  bouyer 
    664  1.6.2.2  bouyer 
    665  1.6.2.2  bouyer 	/* Place a NULL header at the beginning of the transmit area */
    666  1.6.2.2  bouyer 	ea_writebuf(sc, NULL, 0x0000, 0);
    667  1.6.2.2  bouyer 
    668  1.6.2.2  bouyer 	bus_space_write_2(iot, ioh, EA_8005_BUFWIN, 0x0000);
    669  1.6.2.2  bouyer 	bus_space_write_2(iot, ioh, EA_8005_BUFWIN, 0x0000);
    670  1.6.2.2  bouyer 
    671  1.6.2.2  bouyer 	sc->sc_command |= EA_CMD_TX_INTEN;
    672  1.6.2.2  bouyer 	bus_space_write_2(iot, ioh, EA_8005_COMMAND, sc->sc_command);
    673  1.6.2.2  bouyer 
    674  1.6.2.2  bouyer 	/* TX_ON gets set by ea_txpacket when there's something to transmit. */
    675  1.6.2.2  bouyer 
    676  1.6.2.2  bouyer 
    677  1.6.2.2  bouyer 	/* Set flags appropriately. */
    678  1.6.2.2  bouyer 	ifp->if_flags |= IFF_RUNNING;
    679  1.6.2.2  bouyer 	ifp->if_flags &= ~IFF_OACTIVE;
    680  1.6.2.2  bouyer 
    681  1.6.2.2  bouyer 	dprintf(("init: st=%04x\n",
    682  1.6.2.2  bouyer 		 bus_space_read_2(iot, ioh, EA_8005_STATUS)));
    683  1.6.2.2  bouyer 
    684  1.6.2.2  bouyer 
    685  1.6.2.2  bouyer 	/* And start output. */
    686  1.6.2.2  bouyer 	ea_start(ifp);
    687  1.6.2.2  bouyer 
    688  1.6.2.2  bouyer 	splx(s);
    689  1.6.2.2  bouyer 	return 0;
    690  1.6.2.2  bouyer }
    691  1.6.2.2  bouyer 
    692  1.6.2.2  bouyer 
    693  1.6.2.2  bouyer /*
    694  1.6.2.2  bouyer  * Start output on interface. Get datagrams from the queue and output them,
    695  1.6.2.2  bouyer  * giving the receiver a chance between datagrams. Call only from splnet or
    696  1.6.2.2  bouyer  * interrupt level!
    697  1.6.2.2  bouyer  */
    698  1.6.2.2  bouyer 
    699  1.6.2.2  bouyer static void
    700  1.6.2.2  bouyer ea_start(struct ifnet *ifp)
    701  1.6.2.2  bouyer {
    702  1.6.2.2  bouyer 	struct seeq8005_softc *sc = ifp->if_softc;
    703  1.6.2.2  bouyer 	int s;
    704  1.6.2.2  bouyer 
    705  1.6.2.2  bouyer 	s = splnet();
    706  1.6.2.2  bouyer #ifdef EA_TX_DEBUG
    707  1.6.2.2  bouyer 	dprintf(("ea_start()...\n"));
    708  1.6.2.2  bouyer #endif
    709  1.6.2.2  bouyer 
    710  1.6.2.2  bouyer 	/* Don't do anything if output is active. */
    711  1.6.2.2  bouyer 
    712  1.6.2.2  bouyer 	if (ifp->if_flags & IFF_OACTIVE)
    713  1.6.2.2  bouyer 		return;
    714  1.6.2.2  bouyer 
    715  1.6.2.2  bouyer 	/* Mark interface as output active */
    716  1.6.2.2  bouyer 
    717  1.6.2.2  bouyer 	ifp->if_flags |= IFF_OACTIVE;
    718  1.6.2.2  bouyer 
    719  1.6.2.2  bouyer 	/* tx packets */
    720  1.6.2.2  bouyer 
    721  1.6.2.2  bouyer 	eatxpacket(sc);
    722  1.6.2.2  bouyer 	splx(s);
    723  1.6.2.2  bouyer }
    724  1.6.2.2  bouyer 
    725  1.6.2.2  bouyer 
    726  1.6.2.2  bouyer /*
    727  1.6.2.2  bouyer  * Transfer a packet to the interface buffer and start transmission
    728  1.6.2.2  bouyer  *
    729  1.6.2.2  bouyer  * Called at splnet()
    730  1.6.2.2  bouyer  */
    731  1.6.2.2  bouyer 
    732  1.6.2.2  bouyer void
    733  1.6.2.2  bouyer eatxpacket(struct seeq8005_softc *sc)
    734  1.6.2.2  bouyer {
    735  1.6.2.2  bouyer 	bus_space_tag_t iot = sc->sc_iot;
    736  1.6.2.2  bouyer 	bus_space_handle_t ioh = sc->sc_ioh;
    737  1.6.2.2  bouyer 	struct mbuf *m, *m0;
    738  1.6.2.2  bouyer 	struct ifnet *ifp;
    739  1.6.2.2  bouyer 	int len, nextpacket;
    740  1.6.2.2  bouyer 	u_int8_t hdr[4];
    741  1.6.2.2  bouyer 
    742  1.6.2.2  bouyer 	ifp = &sc->sc_ethercom.ec_if;
    743  1.6.2.2  bouyer 
    744  1.6.2.2  bouyer 	/* Dequeue the next packet. */
    745  1.6.2.2  bouyer 	IF_DEQUEUE(&ifp->if_snd, m0);
    746  1.6.2.2  bouyer 
    747  1.6.2.2  bouyer 	/* If there's nothing to send, return. */
    748  1.6.2.2  bouyer 	if (!m0) {
    749  1.6.2.2  bouyer 		ifp->if_flags &= ~IFF_OACTIVE;
    750  1.6.2.2  bouyer 		sc->sc_config2 |= EA_CFG2_OUTPUT;
    751  1.6.2.2  bouyer 		bus_space_write_2(iot, ioh, EA_8005_CONFIG2, sc->sc_config2);
    752  1.6.2.2  bouyer #ifdef EA_TX_DEBUG
    753  1.6.2.2  bouyer 		dprintf(("tx finished\n"));
    754  1.6.2.2  bouyer #endif
    755  1.6.2.2  bouyer 		return;
    756  1.6.2.2  bouyer 	}
    757  1.6.2.2  bouyer 
    758  1.6.2.2  bouyer #if NBPFILTER > 0
    759  1.6.2.2  bouyer 	/* Give the packet to the bpf, if any. */
    760  1.6.2.2  bouyer 	if (ifp->if_bpf)
    761  1.6.2.2  bouyer 		bpf_mtap(ifp->if_bpf, m0);
    762  1.6.2.2  bouyer #endif
    763  1.6.2.2  bouyer 
    764  1.6.2.2  bouyer #ifdef EA_TX_DEBUG
    765  1.6.2.2  bouyer 	dprintf(("Tx new packet\n"));
    766  1.6.2.2  bouyer #endif
    767  1.6.2.2  bouyer 
    768  1.6.2.2  bouyer 	sc->sc_config2 &= ~EA_CFG2_OUTPUT;
    769  1.6.2.2  bouyer 	bus_space_write_2(iot, ioh, EA_8005_CONFIG2, sc->sc_config2);
    770  1.6.2.2  bouyer 
    771  1.6.2.2  bouyer 	/*
    772  1.6.2.2  bouyer 	 * Copy the frame to the start of the transmit area on the card,
    773  1.6.2.2  bouyer 	 * leaving four bytes for the transmit header.
    774  1.6.2.2  bouyer 	 */
    775  1.6.2.2  bouyer 	len = 0;
    776  1.6.2.2  bouyer 	for (m = m0; m; m = m->m_next) {
    777  1.6.2.2  bouyer 		if (m->m_len == 0)
    778  1.6.2.2  bouyer 			continue;
    779  1.6.2.2  bouyer 		ea_writebuf(sc, mtod(m, caddr_t), 4 + len, m->m_len);
    780  1.6.2.2  bouyer 		len += m->m_len;
    781  1.6.2.2  bouyer 	}
    782  1.6.2.2  bouyer 	m_freem(m0);
    783  1.6.2.2  bouyer 
    784  1.6.2.2  bouyer 
    785  1.6.2.2  bouyer 	/* If packet size is odd round up to the next 16 bit boundry */
    786  1.6.2.2  bouyer 	if (len % 2)
    787  1.6.2.2  bouyer 		++len;
    788  1.6.2.2  bouyer 
    789  1.6.2.2  bouyer 	len = max(len, ETHER_MIN_LEN);
    790  1.6.2.2  bouyer 
    791  1.6.2.2  bouyer 	if (len > (ETHER_MAX_LEN - ETHER_CRC_LEN))
    792  1.6.2.2  bouyer 		log(LOG_WARNING, "%s: oversize packet = %d bytes\n",
    793  1.6.2.2  bouyer 		    sc->sc_dev.dv_xname, len);
    794  1.6.2.2  bouyer 
    795  1.6.2.2  bouyer #if 0 /*def EA_TX_DEBUG*/
    796  1.6.2.2  bouyer 	dprintf(("ea: xfr pkt length=%d...\n", len));
    797  1.6.2.2  bouyer 
    798  1.6.2.2  bouyer 	dprintf(("%s-->", ether_sprintf(sc->sc_pktbuf+6)));
    799  1.6.2.2  bouyer 	dprintf(("%s\n", ether_sprintf(sc->sc_pktbuf)));
    800  1.6.2.2  bouyer #endif
    801  1.6.2.2  bouyer 
    802  1.6.2.2  bouyer /*	dprintf(("st=%04x\n", bus_space_read_2(iot, ioh, EA_8005_STATUS)));*/
    803  1.6.2.2  bouyer 
    804  1.6.2.2  bouyer 	/* Follow it with a NULL packet header */
    805  1.6.2.2  bouyer 	bus_space_write_2(iot, ioh, EA_8005_BUFWIN, 0x0000);
    806  1.6.2.2  bouyer 	bus_space_write_2(iot, ioh, EA_8005_BUFWIN, 0x0000);
    807  1.6.2.2  bouyer 
    808  1.6.2.2  bouyer 
    809  1.6.2.2  bouyer 	/* Write the packet header */
    810  1.6.2.2  bouyer 
    811  1.6.2.2  bouyer 	nextpacket = len + 4;
    812  1.6.2.2  bouyer 	hdr[0] = (nextpacket >> 8) & 0xff;
    813  1.6.2.2  bouyer 	hdr[1] = nextpacket & 0xff;
    814  1.6.2.2  bouyer 	hdr[2] = EA_PKTHDR_TX | EA_PKTHDR_DATA_FOLLOWS |
    815  1.6.2.2  bouyer 		EA_TXHDR_XMIT_SUCCESS_INT | EA_TXHDR_COLLISION_INT;
    816  1.6.2.2  bouyer 	hdr[3] = 0; /* Status byte -- will be update by hardware. */
    817  1.6.2.2  bouyer 	ea_writebuf(sc, hdr, 0x0000, 4);
    818  1.6.2.2  bouyer 
    819  1.6.2.2  bouyer 	bus_space_write_2(iot, ioh, EA_8005_TX_PTR, 0x0000);
    820  1.6.2.2  bouyer 
    821  1.6.2.2  bouyer /*	dprintf(("st=%04x\n", bus_space_read_2(iot, ioh, EA_8005_STATUS)));*/
    822  1.6.2.2  bouyer 
    823  1.6.2.2  bouyer #ifdef EA_PACKET_DEBUG
    824  1.6.2.2  bouyer 	ea_dump_buffer(sc, 0);
    825  1.6.2.2  bouyer #endif
    826  1.6.2.2  bouyer 
    827  1.6.2.2  bouyer 
    828  1.6.2.2  bouyer 	/* Now transmit the datagram. */
    829  1.6.2.2  bouyer /*	dprintf(("st=%04x\n", bus_space_read_2(iot, ioh, EA_8005_STATUS)));*/
    830  1.6.2.2  bouyer 	bus_space_write_2(iot, ioh, EA_8005_COMMAND,
    831  1.6.2.2  bouyer 			  sc->sc_command | EA_CMD_TX_ON);
    832  1.6.2.2  bouyer #ifdef EA_TX_DEBUG
    833  1.6.2.2  bouyer 	dprintf(("st=%04x\n", bus_space_read_2(iot, ioh, EA_8005_STATUS)));
    834  1.6.2.2  bouyer 	dprintf(("tx: queued\n"));
    835  1.6.2.2  bouyer #endif
    836  1.6.2.2  bouyer }
    837  1.6.2.2  bouyer 
    838  1.6.2.2  bouyer 
    839  1.6.2.2  bouyer /*
    840  1.6.2.2  bouyer  * Ethernet controller interrupt.
    841  1.6.2.2  bouyer  */
    842  1.6.2.2  bouyer 
    843  1.6.2.2  bouyer int
    844  1.6.2.2  bouyer seeq8005intr(void *arg)
    845  1.6.2.2  bouyer {
    846  1.6.2.2  bouyer 	struct seeq8005_softc *sc = arg;
    847  1.6.2.2  bouyer 	bus_space_tag_t iot = sc->sc_iot;
    848  1.6.2.2  bouyer 	bus_space_handle_t ioh = sc->sc_ioh;
    849  1.6.2.2  bouyer 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    850  1.6.2.2  bouyer 	int status, s, handled;
    851  1.6.2.2  bouyer 	u_int8_t txhdr[4];
    852  1.6.2.2  bouyer 	u_int txstatus;
    853  1.6.2.2  bouyer 
    854  1.6.2.2  bouyer 	handled = 0;
    855  1.6.2.2  bouyer 	dprintf(("eaintr: "));
    856  1.6.2.2  bouyer 
    857  1.6.2.2  bouyer 
    858  1.6.2.2  bouyer 	/* Get the controller status */
    859  1.6.2.2  bouyer 	status = bus_space_read_2(iot, ioh, EA_8005_STATUS);
    860  1.6.2.2  bouyer         dprintf(("st=%04x ", status));
    861  1.6.2.2  bouyer 
    862  1.6.2.2  bouyer 
    863  1.6.2.2  bouyer 	/* Tx interrupt ? */
    864  1.6.2.2  bouyer 	if (status & EA_STATUS_TX_INT) {
    865  1.6.2.2  bouyer 		dprintf(("txint "));
    866  1.6.2.2  bouyer 		handled = 1;
    867  1.6.2.2  bouyer 
    868  1.6.2.2  bouyer 		/* Acknowledge the interrupt */
    869  1.6.2.2  bouyer 		bus_space_write_2(iot, ioh, EA_8005_COMMAND,
    870  1.6.2.2  bouyer 				  sc->sc_command | EA_CMD_TX_INTACK);
    871  1.6.2.2  bouyer 
    872  1.6.2.2  bouyer 		ea_readbuf(sc, txhdr, 0x0000, 4);
    873  1.6.2.2  bouyer 
    874  1.6.2.2  bouyer #ifdef EA_TX_DEBUG
    875  1.6.2.2  bouyer 		dprintf(("txstatus=%02x %02x %02x %02x\n",
    876  1.6.2.2  bouyer 			 txhdr[0], txhdr[1], txhdr[2], txhdr[3]));
    877  1.6.2.2  bouyer #endif
    878  1.6.2.2  bouyer 		txstatus = txhdr[3];
    879  1.6.2.2  bouyer 
    880  1.6.2.2  bouyer 		/*
    881  1.6.2.2  bouyer 		 * Did it succeed ? Did we collide ?
    882  1.6.2.2  bouyer 		 *
    883  1.6.2.2  bouyer 		 * The exact proceedure here is not clear. We should get
    884  1.6.2.2  bouyer 		 * an interrupt on a sucessfull tx or on a collision.
    885  1.6.2.2  bouyer 		 * The done flag is set after successfull tx or 16 collisions
    886  1.6.2.2  bouyer 		 * We should thus get a interrupt for each of collision
    887  1.6.2.2  bouyer 		 * and the done bit should not be set. However it does appear
    888  1.6.2.2  bouyer 		 * to be set at the same time as the collision bit ...
    889  1.6.2.2  bouyer 		 *
    890  1.6.2.2  bouyer 		 * So we will count collisions and output errors and will
    891  1.6.2.2  bouyer 		 * assume that if the done bit is set the packet was
    892  1.6.2.2  bouyer 		 * transmitted. Stats may be wrong if 16 collisions occur on
    893  1.6.2.2  bouyer 		 * a packet as the done flag should be set but the packet
    894  1.6.2.2  bouyer 		 * may not have been transmitted. so the output count might
    895  1.6.2.2  bouyer 		 * not require incrementing if the 16 collisions flags is
    896  1.6.2.2  bouyer 		 * set. I don;t know abou this until it happens.
    897  1.6.2.2  bouyer 		 */
    898  1.6.2.2  bouyer 
    899  1.6.2.2  bouyer 		if (txstatus & EA_TXHDR_COLLISION)
    900  1.6.2.2  bouyer 			ifp->if_collisions++;
    901  1.6.2.2  bouyer 		else if (txstatus & EA_TXHDR_ERROR_MASK)
    902  1.6.2.2  bouyer 			ifp->if_oerrors++;
    903  1.6.2.2  bouyer 
    904  1.6.2.2  bouyer #if 0
    905  1.6.2.2  bouyer 		if (txstatus & EA_TXHDR_ERROR_MASK)
    906  1.6.2.2  bouyer 			log(LOG_WARNING, "tx packet error =%02x\n", txstatus);
    907  1.6.2.2  bouyer #endif
    908  1.6.2.2  bouyer 
    909  1.6.2.2  bouyer 		if (txstatus & EA_PKTHDR_DONE) {
    910  1.6.2.2  bouyer 			ifp->if_opackets++;
    911  1.6.2.2  bouyer 
    912  1.6.2.2  bouyer 			/* Tx next packet */
    913  1.6.2.2  bouyer 
    914  1.6.2.2  bouyer 			s = splnet();
    915  1.6.2.2  bouyer 			eatxpacket(sc);
    916  1.6.2.2  bouyer 			splx(s);
    917  1.6.2.2  bouyer 		}
    918  1.6.2.2  bouyer 	}
    919  1.6.2.2  bouyer 
    920  1.6.2.2  bouyer 
    921  1.6.2.2  bouyer 	/* Rx interrupt ? */
    922  1.6.2.2  bouyer 	if (status & EA_STATUS_RX_INT) {
    923  1.6.2.2  bouyer 		dprintf(("rxint "));
    924  1.6.2.2  bouyer 		handled = 1;
    925  1.6.2.2  bouyer 
    926  1.6.2.2  bouyer 		/* Acknowledge the interrupt */
    927  1.6.2.2  bouyer 		bus_space_write_2(iot, ioh, EA_8005_COMMAND,
    928  1.6.2.2  bouyer 				  sc->sc_command | EA_CMD_RX_INTACK);
    929  1.6.2.2  bouyer 
    930  1.6.2.2  bouyer 		/* Install a watchdog timer needed atm to fixed rx lockups */
    931  1.6.2.2  bouyer 		ifp->if_timer = EA_TIMEOUT;
    932  1.6.2.2  bouyer 
    933  1.6.2.2  bouyer 		/* Processes the received packets */
    934  1.6.2.2  bouyer 		eagetpackets(sc);
    935  1.6.2.2  bouyer 
    936  1.6.2.2  bouyer 
    937  1.6.2.2  bouyer #if 0
    938  1.6.2.2  bouyer 		/* Make sure the receiver is on */
    939  1.6.2.2  bouyer 		if ((status & EA_STATUS_RX_ON) == 0) {
    940  1.6.2.2  bouyer 			bus_space_write_2(iot, ioh, EA_8005_COMMAND,
    941  1.6.2.2  bouyer 					  sc->sc_command | EA_CMD_RX_ON);
    942  1.6.2.2  bouyer 			printf("rxintr: rx is off st=%04x\n",status);
    943  1.6.2.2  bouyer 		}
    944  1.6.2.2  bouyer #endif
    945  1.6.2.2  bouyer 	}
    946  1.6.2.2  bouyer 
    947  1.6.2.2  bouyer #ifdef EA_DEBUG
    948  1.6.2.2  bouyer 	status = bus_space_read_2(iot, ioh, EA_8005_STATUS);
    949  1.6.2.2  bouyer         dprintf(("st=%04x\n", status));
    950  1.6.2.2  bouyer #endif
    951  1.6.2.2  bouyer 
    952  1.6.2.2  bouyer 	return handled;
    953  1.6.2.2  bouyer }
    954  1.6.2.2  bouyer 
    955  1.6.2.2  bouyer 
    956  1.6.2.2  bouyer void
    957  1.6.2.2  bouyer eagetpackets(struct seeq8005_softc *sc)
    958  1.6.2.2  bouyer {
    959  1.6.2.2  bouyer 	bus_space_tag_t iot = sc->sc_iot;
    960  1.6.2.2  bouyer 	bus_space_handle_t ioh = sc->sc_ioh;
    961  1.6.2.2  bouyer 	u_int addr;
    962  1.6.2.2  bouyer 	int len;
    963  1.6.2.2  bouyer 	int ctrl;
    964  1.6.2.2  bouyer 	int ptr;
    965  1.6.2.2  bouyer 	int pack;
    966  1.6.2.2  bouyer 	int status;
    967  1.6.2.2  bouyer 	u_int8_t rxhdr[4];
    968  1.6.2.2  bouyer 	struct ifnet *ifp;
    969  1.6.2.2  bouyer 
    970  1.6.2.2  bouyer 	ifp = &sc->sc_ethercom.ec_if;
    971  1.6.2.2  bouyer 
    972  1.6.2.2  bouyer 
    973  1.6.2.2  bouyer 	/* We start from the last rx pointer position */
    974  1.6.2.2  bouyer 	addr = sc->sc_rx_ptr;
    975  1.6.2.2  bouyer 	sc->sc_config2 &= ~EA_CFG2_OUTPUT;
    976  1.6.2.2  bouyer 	bus_space_write_2(iot, ioh, EA_8005_CONFIG2, sc->sc_config2);
    977  1.6.2.2  bouyer 
    978  1.6.2.2  bouyer 	do {
    979  1.6.2.2  bouyer 		/* Read rx header */
    980  1.6.2.2  bouyer 		ea_readbuf(sc, rxhdr, addr, 4);
    981  1.6.2.2  bouyer 
    982  1.6.2.2  bouyer 		/* Split the packet header */
    983  1.6.2.2  bouyer 		ptr = (rxhdr[0] << 8) | rxhdr[1];
    984  1.6.2.2  bouyer 		ctrl = rxhdr[2];
    985  1.6.2.2  bouyer 		status = rxhdr[3];
    986  1.6.2.2  bouyer 
    987  1.6.2.2  bouyer #ifdef EA_RX_DEBUG
    988  1.6.2.2  bouyer 		dprintf(("addr=%04x ptr=%04x ctrl=%02x status=%02x\n",
    989  1.6.2.2  bouyer 			 addr, ptr, ctrl, status));
    990  1.6.2.2  bouyer #endif
    991  1.6.2.2  bouyer 
    992  1.6.2.2  bouyer 
    993  1.6.2.2  bouyer 		/* Zero packet ptr ? then must be null header so exit */
    994  1.6.2.2  bouyer 		if (ptr == 0) break;
    995  1.6.2.2  bouyer 
    996  1.6.2.2  bouyer 
    997  1.6.2.2  bouyer 		/* Get packet length */
    998  1.6.2.2  bouyer        		len = (ptr - addr) - 4;
    999  1.6.2.2  bouyer 
   1000  1.6.2.2  bouyer 		if (len < 0)
   1001  1.6.2.2  bouyer 			len += EA_RX_BUFFER_SIZE;
   1002  1.6.2.2  bouyer 
   1003  1.6.2.2  bouyer #ifdef EA_RX_DEBUG
   1004  1.6.2.2  bouyer 		dprintf(("len=%04x\n", len));
   1005  1.6.2.2  bouyer #endif
   1006  1.6.2.2  bouyer 
   1007  1.6.2.2  bouyer 
   1008  1.6.2.2  bouyer 		/* Has the packet rx completed ? if not then exit */
   1009  1.6.2.2  bouyer 		if ((status & EA_PKTHDR_DONE) == 0)
   1010  1.6.2.2  bouyer 			break;
   1011  1.6.2.2  bouyer 
   1012  1.6.2.2  bouyer 		/*
   1013  1.6.2.2  bouyer 		 * Did we have any errors? then note error and go to
   1014  1.6.2.2  bouyer 		 * next packet
   1015  1.6.2.2  bouyer 		 */
   1016  1.6.2.2  bouyer 		if (__predict_false(status & 0x0f)) {
   1017  1.6.2.2  bouyer 			++ifp->if_ierrors;
   1018  1.6.2.2  bouyer 			log(LOG_WARNING,
   1019  1.6.2.2  bouyer 			    "%s: rx packet error (%02x) - dropping packet\n",
   1020  1.6.2.2  bouyer 			    sc->sc_dev.dv_xname, status & 0x0f);
   1021  1.6.2.2  bouyer 			sc->sc_config2 |= EA_CFG2_OUTPUT;
   1022  1.6.2.2  bouyer 			bus_space_write_2(iot, ioh, EA_8005_CONFIG2,
   1023  1.6.2.2  bouyer 					  sc->sc_config2);
   1024  1.6.2.3  bouyer 			ea_init(ifp);
   1025  1.6.2.2  bouyer 			return;
   1026  1.6.2.2  bouyer 		}
   1027  1.6.2.2  bouyer 
   1028  1.6.2.2  bouyer 		/*
   1029  1.6.2.2  bouyer 		 * Is the packet too big ? - this will probably be trapped
   1030  1.6.2.2  bouyer 		 * above as a receive error
   1031  1.6.2.2  bouyer 		 */
   1032  1.6.2.2  bouyer 		if (__predict_false(len > (ETHER_MAX_LEN - ETHER_CRC_LEN))) {
   1033  1.6.2.2  bouyer 			++ifp->if_ierrors;
   1034  1.6.2.2  bouyer 			log(LOG_WARNING, "%s: rx packet size error len=%d\n",
   1035  1.6.2.2  bouyer 			    sc->sc_dev.dv_xname, len);
   1036  1.6.2.2  bouyer 			sc->sc_config2 |= EA_CFG2_OUTPUT;
   1037  1.6.2.2  bouyer 			bus_space_write_2(iot, ioh, EA_8005_CONFIG2,
   1038  1.6.2.2  bouyer 					  sc->sc_config2);
   1039  1.6.2.3  bouyer 			ea_init(ifp);
   1040  1.6.2.2  bouyer 			return;
   1041  1.6.2.2  bouyer 		}
   1042  1.6.2.2  bouyer 
   1043  1.6.2.2  bouyer 		ifp->if_ipackets++;
   1044  1.6.2.2  bouyer 		/* Pass data up to upper levels. */
   1045  1.6.2.2  bouyer 		earead(sc, addr + 4, len);
   1046  1.6.2.2  bouyer 
   1047  1.6.2.2  bouyer 		addr = ptr;
   1048  1.6.2.2  bouyer 		++pack;
   1049  1.6.2.2  bouyer 	} while (len != 0);
   1050  1.6.2.2  bouyer 
   1051  1.6.2.2  bouyer 	sc->sc_config2 |= EA_CFG2_OUTPUT;
   1052  1.6.2.2  bouyer 	bus_space_write_2(iot, ioh, EA_8005_CONFIG2, sc->sc_config2);
   1053  1.6.2.2  bouyer 
   1054  1.6.2.2  bouyer #ifdef EA_RX_DEBUG
   1055  1.6.2.2  bouyer 	dprintf(("new rx ptr=%04x\n", addr));
   1056  1.6.2.2  bouyer #endif
   1057  1.6.2.2  bouyer 
   1058  1.6.2.2  bouyer 
   1059  1.6.2.2  bouyer 	/* Store new rx pointer */
   1060  1.6.2.2  bouyer 	sc->sc_rx_ptr = addr;
   1061  1.6.2.2  bouyer 	bus_space_write_2(iot, ioh, EA_8005_RX_END, sc->sc_rx_ptr >> 8);
   1062  1.6.2.2  bouyer 
   1063  1.6.2.2  bouyer 	/* Make sure the receiver is on */
   1064  1.6.2.2  bouyer 	bus_space_write_2(iot, ioh, EA_8005_COMMAND,
   1065  1.6.2.2  bouyer 			  sc->sc_command | EA_CMD_RX_ON);
   1066  1.6.2.2  bouyer 
   1067  1.6.2.2  bouyer }
   1068  1.6.2.2  bouyer 
   1069  1.6.2.2  bouyer 
   1070  1.6.2.2  bouyer /*
   1071  1.6.2.2  bouyer  * Pass a packet up to the higher levels.
   1072  1.6.2.2  bouyer  */
   1073  1.6.2.2  bouyer 
   1074  1.6.2.2  bouyer static void
   1075  1.6.2.2  bouyer earead(struct seeq8005_softc *sc, int addr, int len)
   1076  1.6.2.2  bouyer {
   1077  1.6.2.2  bouyer 	struct mbuf *m;
   1078  1.6.2.2  bouyer 	struct ifnet *ifp;
   1079  1.6.2.2  bouyer 
   1080  1.6.2.2  bouyer 	ifp = &sc->sc_ethercom.ec_if;
   1081  1.6.2.2  bouyer 
   1082  1.6.2.2  bouyer 	/* Pull packet off interface. */
   1083  1.6.2.2  bouyer 	m = eaget(sc, addr, len, ifp);
   1084  1.6.2.2  bouyer 	if (m == 0)
   1085  1.6.2.2  bouyer 		return;
   1086  1.6.2.2  bouyer 
   1087  1.6.2.2  bouyer #ifdef EA_RX_DEBUG
   1088  1.6.2.2  bouyer 	dprintf(("%s-->", ether_sprintf(eh->ether_shost)));
   1089  1.6.2.2  bouyer 	dprintf(("%s\n", ether_sprintf(eh->ether_dhost)));
   1090  1.6.2.2  bouyer #endif
   1091  1.6.2.2  bouyer 
   1092  1.6.2.2  bouyer #if NBPFILTER > 0
   1093  1.6.2.2  bouyer 	/*
   1094  1.6.2.2  bouyer 	 * Check if there's a BPF listener on this interface.
   1095  1.6.2.2  bouyer 	 * If so, hand off the raw packet to bpf.
   1096  1.6.2.2  bouyer 	 */
   1097  1.6.2.2  bouyer 	if (ifp->if_bpf)
   1098  1.6.2.2  bouyer 		bpf_mtap(ifp->if_bpf, m);
   1099  1.6.2.2  bouyer #endif
   1100  1.6.2.2  bouyer 
   1101  1.6.2.2  bouyer 	(*ifp->if_input)(ifp, m);
   1102  1.6.2.2  bouyer }
   1103  1.6.2.2  bouyer 
   1104  1.6.2.2  bouyer /*
   1105  1.6.2.2  bouyer  * Pull read data off a interface.  Len is length of data, with local net
   1106  1.6.2.2  bouyer  * header stripped.  We copy the data into mbufs.  When full cluster sized
   1107  1.6.2.2  bouyer  * units are present we copy into clusters.
   1108  1.6.2.2  bouyer  */
   1109  1.6.2.2  bouyer 
   1110  1.6.2.2  bouyer struct mbuf *
   1111  1.6.2.2  bouyer eaget(struct seeq8005_softc *sc, int addr, int totlen, struct ifnet *ifp)
   1112  1.6.2.2  bouyer {
   1113  1.6.2.2  bouyer         struct mbuf *top, **mp, *m;
   1114  1.6.2.2  bouyer         int len;
   1115  1.6.2.2  bouyer         u_int cp, epkt;
   1116  1.6.2.2  bouyer 
   1117  1.6.2.2  bouyer         cp = addr;
   1118  1.6.2.2  bouyer         epkt = cp + totlen;
   1119  1.6.2.2  bouyer 
   1120  1.6.2.2  bouyer         MGETHDR(m, M_DONTWAIT, MT_DATA);
   1121  1.6.2.2  bouyer         if (m == 0)
   1122  1.6.2.2  bouyer                 return 0;
   1123  1.6.2.2  bouyer         m->m_pkthdr.rcvif = ifp;
   1124  1.6.2.2  bouyer         m->m_pkthdr.len = totlen;
   1125  1.6.2.2  bouyer         m->m_len = MHLEN;
   1126  1.6.2.2  bouyer         top = 0;
   1127  1.6.2.2  bouyer         mp = &top;
   1128  1.6.2.2  bouyer 
   1129  1.6.2.2  bouyer         while (totlen > 0) {
   1130  1.6.2.2  bouyer                 if (top) {
   1131  1.6.2.2  bouyer                         MGET(m, M_DONTWAIT, MT_DATA);
   1132  1.6.2.2  bouyer                         if (m == 0) {
   1133  1.6.2.2  bouyer                                 m_freem(top);
   1134  1.6.2.2  bouyer                                 return 0;
   1135  1.6.2.2  bouyer                         }
   1136  1.6.2.2  bouyer                         m->m_len = MLEN;
   1137  1.6.2.2  bouyer                 }
   1138  1.6.2.2  bouyer                 len = min(totlen, epkt - cp);
   1139  1.6.2.2  bouyer                 if (len >= MINCLSIZE) {
   1140  1.6.2.2  bouyer                         MCLGET(m, M_DONTWAIT);
   1141  1.6.2.2  bouyer                         if (m->m_flags & M_EXT)
   1142  1.6.2.2  bouyer                                 m->m_len = len = min(len, MCLBYTES);
   1143  1.6.2.2  bouyer                         else
   1144  1.6.2.2  bouyer                                 len = m->m_len;
   1145  1.6.2.2  bouyer                 } else {
   1146  1.6.2.2  bouyer                         /*
   1147  1.6.2.2  bouyer                          * Place initial small packet/header at end of mbuf.
   1148  1.6.2.2  bouyer                          */
   1149  1.6.2.2  bouyer                         if (len < m->m_len) {
   1150  1.6.2.2  bouyer                                 if (top == 0 && len + max_linkhdr <= m->m_len)
   1151  1.6.2.2  bouyer                                         m->m_data += max_linkhdr;
   1152  1.6.2.2  bouyer                                 m->m_len = len;
   1153  1.6.2.2  bouyer                         } else
   1154  1.6.2.2  bouyer                                 len = m->m_len;
   1155  1.6.2.2  bouyer                 }
   1156  1.6.2.2  bouyer 		if (top == 0) {
   1157  1.6.2.2  bouyer 			/* Make sure the payload is aligned */
   1158  1.6.2.2  bouyer 			caddr_t newdata = (caddr_t)
   1159  1.6.2.2  bouyer 			    ALIGN(m->m_data + sizeof(struct ether_header)) -
   1160  1.6.2.2  bouyer 			    sizeof(struct ether_header);
   1161  1.6.2.2  bouyer 			len -= newdata - m->m_data;
   1162  1.6.2.2  bouyer 			m->m_len = len;
   1163  1.6.2.2  bouyer 			m->m_data = newdata;
   1164  1.6.2.2  bouyer 		}
   1165  1.6.2.2  bouyer                 ea_readbuf(sc, mtod(m, u_char *),
   1166  1.6.2.2  bouyer 			   cp < EA_BUFFER_SIZE ? cp : cp - EA_RX_BUFFER_SIZE,
   1167  1.6.2.2  bouyer 			   len);
   1168  1.6.2.2  bouyer                 cp += len;
   1169  1.6.2.2  bouyer                 *mp = m;
   1170  1.6.2.2  bouyer                 mp = &m->m_next;
   1171  1.6.2.2  bouyer                 totlen -= len;
   1172  1.6.2.2  bouyer                 if (cp == epkt)
   1173  1.6.2.2  bouyer                         cp = addr;
   1174  1.6.2.2  bouyer         }
   1175  1.6.2.2  bouyer 
   1176  1.6.2.2  bouyer         return top;
   1177  1.6.2.2  bouyer }
   1178  1.6.2.2  bouyer 
   1179  1.6.2.2  bouyer /*
   1180  1.6.2.2  bouyer  * Process an ioctl request.  Mostly boilerplate.
   1181  1.6.2.2  bouyer  */
   1182  1.6.2.2  bouyer static int
   1183  1.6.2.2  bouyer ea_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
   1184  1.6.2.2  bouyer {
   1185  1.6.2.2  bouyer 	struct seeq8005_softc *sc = ifp->if_softc;
   1186  1.6.2.2  bouyer 	int s, error = 0;
   1187  1.6.2.2  bouyer 
   1188  1.6.2.2  bouyer 	s = splnet();
   1189  1.6.2.2  bouyer 	switch (cmd) {
   1190  1.6.2.2  bouyer 
   1191  1.6.2.3  bouyer 	default:
   1192  1.6.2.3  bouyer 		error = ether_ioctl(ifp, cmd, data);
   1193  1.6.2.3  bouyer 		if (error == ENETRESET) {
   1194  1.6.2.2  bouyer 			/*
   1195  1.6.2.3  bouyer 			 * Multicast list has changed; set the hardware filter
   1196  1.6.2.3  bouyer 			 * accordingly.
   1197  1.6.2.2  bouyer 			 */
   1198  1.6.2.3  bouyer 			ea_mc_reset(sc);
   1199  1.6.2.3  bouyer 			error = 0;
   1200  1.6.2.2  bouyer 		}
   1201  1.6.2.2  bouyer 		break;
   1202  1.6.2.2  bouyer 	}
   1203  1.6.2.2  bouyer 
   1204  1.6.2.2  bouyer 	splx(s);
   1205  1.6.2.2  bouyer 	return error;
   1206  1.6.2.2  bouyer }
   1207  1.6.2.2  bouyer 
   1208  1.6.2.3  bouyer /* Must be called at splnet() */
   1209  1.6.2.3  bouyer static void
   1210  1.6.2.3  bouyer ea_mc_reset(struct seeq8005_softc *sc)
   1211  1.6.2.3  bouyer {
   1212  1.6.2.3  bouyer 	struct ether_multi *enm;
   1213  1.6.2.3  bouyer 	struct ether_multistep step;
   1214  1.6.2.3  bouyer 	int naddr, maxaddrs;
   1215  1.6.2.3  bouyer 
   1216  1.6.2.3  bouyer 	naddr = 0;
   1217  1.6.2.3  bouyer 	maxaddrs = (sc->sc_flags & SEEQ8005_80C04) ? 5 : 0;
   1218  1.6.2.3  bouyer 	ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
   1219  1.6.2.3  bouyer 	while (enm != NULL) {
   1220  1.6.2.3  bouyer 		/* Have we got space? */
   1221  1.6.2.3  bouyer 		if (naddr >= maxaddrs ||
   1222  1.6.2.3  bouyer 		    bcmp(enm->enm_addrlo, enm->enm_addrhi, 6) != 0) {
   1223  1.6.2.3  bouyer 			sc->sc_ethercom.ec_if.if_flags |= IFF_ALLMULTI;
   1224  1.6.2.3  bouyer 			ea_ioctl(&sc->sc_ethercom.ec_if, SIOCSIFFLAGS, NULL);
   1225  1.6.2.3  bouyer 			return;
   1226  1.6.2.3  bouyer 		}
   1227  1.6.2.3  bouyer 		ea_set_address(sc, naddr, enm->enm_addrlo);
   1228  1.6.2.3  bouyer 		sc->sc_config1 |= EA_CFG1_STATION_ADDR0 << naddr;
   1229  1.6.2.3  bouyer 		naddr++;
   1230  1.6.2.3  bouyer 		ETHER_NEXT_MULTI(step, enm);
   1231  1.6.2.3  bouyer 	}
   1232  1.6.2.3  bouyer 	for (; naddr < maxaddrs; naddr++)
   1233  1.6.2.3  bouyer 		sc->sc_config1 &= ~(EA_CFG1_STATION_ADDR0 << naddr);
   1234  1.6.2.3  bouyer 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, EA_8005_CONFIG1,
   1235  1.6.2.3  bouyer 			  sc->sc_config1);
   1236  1.6.2.3  bouyer }
   1237  1.6.2.3  bouyer 
   1238  1.6.2.2  bouyer /*
   1239  1.6.2.2  bouyer  * Device timeout routine.
   1240  1.6.2.2  bouyer  *
   1241  1.6.2.2  bouyer  * Ok I am not sure exactly how the device timeout should work....
   1242  1.6.2.2  bouyer  * Currently what will happens is that that the device timeout is only
   1243  1.6.2.2  bouyer  * set when a packet it received. This indicates we are on an active
   1244  1.6.2.2  bouyer  * network and thus we should expect more packets. If non arrive in
   1245  1.6.2.2  bouyer  * in the timeout period then we reinitialise as we may have jammed.
   1246  1.6.2.2  bouyer  * We zero the timeout at this point so that we don't end up with
   1247  1.6.2.2  bouyer  * an endless stream of timeouts if the network goes down.
   1248  1.6.2.2  bouyer  */
   1249  1.6.2.2  bouyer 
   1250  1.6.2.2  bouyer static void
   1251  1.6.2.2  bouyer ea_watchdog(struct ifnet *ifp)
   1252  1.6.2.2  bouyer {
   1253  1.6.2.2  bouyer 	struct seeq8005_softc *sc = ifp->if_softc;
   1254  1.6.2.2  bouyer 
   1255  1.6.2.2  bouyer 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
   1256  1.6.2.2  bouyer 	ifp->if_oerrors++;
   1257  1.6.2.2  bouyer 	dprintf(("ea_watchdog: "));
   1258  1.6.2.2  bouyer 	dprintf(("st=%04x\n",
   1259  1.6.2.2  bouyer 		 bus_space_read_2(sc->sc_iot, sc->sc_ioh, EA_8005_STATUS)));
   1260  1.6.2.2  bouyer 
   1261  1.6.2.2  bouyer 	/* Kick the interface */
   1262  1.6.2.2  bouyer 
   1263  1.6.2.3  bouyer 	ea_init(ifp);
   1264  1.6.2.2  bouyer 
   1265  1.6.2.2  bouyer /*	ifp->if_timer = EA_TIMEOUT;*/
   1266  1.6.2.2  bouyer 	ifp->if_timer = 0;
   1267  1.6.2.2  bouyer }
   1268  1.6.2.2  bouyer 
   1269  1.6.2.2  bouyer /* End of if_ea.c */
   1270