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