Home | History | Annotate | Line # | Download | only in ic
seeq8005.c revision 1.21
      1 /* $NetBSD: seeq8005.c,v 1.21 2001/04/05 22:55:46 bjh21 Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2000 Ben Harris
      5  * Copyright (c) 1995-1998 Mark Brinicombe
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by Mark Brinicombe
     19  *	for the NetBSD Project.
     20  * 4. The name of the company nor the name of the author may be used to
     21  *    endorse or promote products derived from this software without specific
     22  *    prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     25  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     26  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     27  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     28  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     29  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     30  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  */
     36 /*
     37  * seeq8005.c - SEEQ 8005 device driver
     38  */
     39 /*
     40  * This driver currently supports the following chip:
     41  * SEEQ 8005 Advanced Ethernet Data Link Controller
     42  * SEEQ 80C04 Ethernet Data Link Controller
     43  * SEEQ 80C04A AutoDUPLEX CMOS Ethernet Data Link Controller
     44  */
     45 /*
     46  * More information on the 8004 and 8005 AEDLC controllers can be found in
     47  * the SEEQ Technology Inc 1992 Data Comm Devices data book.
     48  *
     49  * This data book may no longer be available as these are rather old chips
     50  * (1991 - 1993)
     51  */
     52 /*
     53  * This driver is based on the arm32 ea(4) driver, hence the names of many
     54  * of the functions.
     55  */
     56 /*
     57  * Bugs/possible improvements:
     58  *	- Does not currently support DMA
     59  *	- Does not transmit multiple packets in one go
     60  *	- Does not support 8-bit busses
     61  */
     62 
     63 #include "opt_inet.h"
     64 #include "opt_ns.h"
     65 
     66 #include <sys/types.h>
     67 #include <sys/param.h>
     68 
     69 __RCSID("$NetBSD: seeq8005.c,v 1.21 2001/04/05 22:55:46 bjh21 Exp $");
     70 
     71 #include <sys/systm.h>
     72 #include <sys/endian.h>
     73 #include <sys/errno.h>
     74 #include <sys/ioctl.h>
     75 #include <sys/mbuf.h>
     76 #include <sys/socket.h>
     77 #include <sys/syslog.h>
     78 #include <sys/device.h>
     79 
     80 #include <net/if.h>
     81 #include <net/if_dl.h>
     82 #include <net/if_types.h>
     83 #include <net/if_ether.h>
     84 #include <net/if_media.h>
     85 
     86 #ifdef INET
     87 #include <netinet/in.h>
     88 #include <netinet/in_systm.h>
     89 #include <netinet/in_var.h>
     90 #include <netinet/ip.h>
     91 #include <netinet/if_inarp.h>
     92 #endif
     93 
     94 #ifdef NS
     95 #include <netns/ns.h>
     96 #include <netns/ns_if.h>
     97 #endif
     98 
     99 #include "bpfilter.h"
    100 #if NBPFILTER > 0
    101 #include <net/bpf.h>
    102 #include <net/bpfdesc.h>
    103 #endif
    104 
    105 #include <machine/bus.h>
    106 #include <machine/intr.h>
    107 
    108 #include <dev/ic/seeq8005reg.h>
    109 #include <dev/ic/seeq8005var.h>
    110 
    111 /*#define SEEQ_DEBUG*/
    112 
    113 /* for debugging convenience */
    114 #ifdef SEEQ8005_DEBUG
    115 #define SEEQ_DEBUG_MISC		1
    116 #define SEEQ_DEBUG_TX		2
    117 #define SEEQ_DEBUG_RX		4
    118 #define SEEQ_DEBUG_PKT		8
    119 #define SEEQ_DEBUG_TXINT	16
    120 #define SEEQ_DEBUG_RXINT	32
    121 int seeq8005_debug = 0;
    122 #define DPRINTF(f, x) { if (seeq8005_debug & (f)) printf x; }
    123 #else
    124 #define DPRINTF(f, x)
    125 #endif
    126 
    127 #define	SEEQ_TX_BUFFER_SIZE		0x800		/* (> MAX_ETHER_LEN) */
    128 
    129 /*
    130  * prototypes
    131  */
    132 
    133 static int ea_init(struct ifnet *);
    134 static int ea_ioctl(struct ifnet *, u_long, caddr_t);
    135 static void ea_start(struct ifnet *);
    136 static void ea_watchdog(struct ifnet *);
    137 static void ea_chipreset(struct seeq8005_softc *);
    138 static void ea_ramtest(struct seeq8005_softc *);
    139 static int ea_stoptx(struct seeq8005_softc *);
    140 static int ea_stoprx(struct seeq8005_softc *);
    141 static void ea_stop(struct ifnet *, int);
    142 static void ea_await_fifo_empty(struct seeq8005_softc *);
    143 static void ea_await_fifo_full(struct seeq8005_softc *);
    144 static void ea_writebuf(struct seeq8005_softc *, u_char *, int, size_t);
    145 static void ea_readbuf(struct seeq8005_softc *, u_char *, int, size_t);
    146 static void ea_select_buffer(struct seeq8005_softc *, int);
    147 static void ea_set_address(struct seeq8005_softc *, int, const u_int8_t *);
    148 static void ea_read(struct seeq8005_softc *, int, int);
    149 static struct mbuf *ea_get(struct seeq8005_softc *, int, int, struct ifnet *);
    150 static void ea_txint(struct seeq8005_softc *);
    151 static void ea_rxint(struct seeq8005_softc *);
    152 static void eatxpacket(struct seeq8005_softc *);
    153 static int ea_writembuf(struct seeq8005_softc *, struct mbuf *, int);
    154 static void ea_mc_reset(struct seeq8005_softc *);
    155 static void ea_mc_reset_8004(struct seeq8005_softc *);
    156 static void ea_mc_reset_8005(struct seeq8005_softc *);
    157 static int ea_mediachange(struct ifnet *);
    158 static void ea_mediastatus(struct ifnet *, struct ifmediareq *);
    159 
    160 
    161 /*
    162  * Attach chip.
    163  */
    164 
    165 void
    166 seeq8005_attach(struct seeq8005_softc *sc, const u_int8_t *myaddr, int *media,
    167     int nmedia, int defmedia)
    168 {
    169 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    170 	u_int id;
    171 
    172 	KASSERT(myaddr != NULL);
    173 	printf(" address %s", ether_sprintf(myaddr));
    174 
    175 	/* Stop the board. */
    176 
    177 	ea_chipreset(sc);
    178 
    179 	/* Get the product ID */
    180 
    181 	ea_select_buffer(sc, SEEQ_BUFCODE_PRODUCTID);
    182 	id = bus_space_read_2(sc->sc_iot, sc->sc_ioh, SEEQ_BUFWIN);
    183 
    184 	switch (id & SEEQ_PRODUCTID_MASK) {
    185 	case SEEQ_PRODUCTID_8004:
    186 		sc->sc_variant = SEEQ_8004;
    187 		switch (id & SEEQ_PRODUCTID_REV_MASK) {
    188 		case SEEQ_PRODUCTID_REV_80C04:
    189 			printf(", SEEQ 80C04\n");
    190 			break;
    191 		case SEEQ_PRODUCTID_REV_80C04A:
    192 			printf(", SEEQ 80C04A\n");
    193 			break;
    194 		default:
    195 			/* Unknown SEEQ 8004 variants */
    196 			printf(", SEEQ 8004 rev %x\n",
    197 			    id & SEEQ_PRODUCTID_REV_MASK);
    198 			break;
    199 		}
    200 		break;
    201 	default:	/* XXX */
    202 		sc->sc_variant = SEEQ_8005;
    203 		printf(", SEEQ 8005\n");
    204 		break;
    205 	}
    206 
    207 	/* Both the 8004 and 8005 are designed for 64K Buffer memory */
    208 	sc->sc_buffersize = SEEQ_MAX_BUFFER_SIZE;
    209 
    210 	/*
    211 	 * Set up tx and rx buffers.
    212 	 *
    213 	 * We use approximately a quarter of the packet memory for TX
    214 	 * buffers and the rest for RX buffers
    215 	 */
    216 	/* sc->sc_tx_bufs = sc->sc_buffersize / SEEQ_TX_BUFFER_SIZE / 4; */
    217 	sc->sc_tx_bufs = 1;
    218 	sc->sc_tx_bufsize = sc->sc_tx_bufs * SEEQ_TX_BUFFER_SIZE;
    219 	sc->sc_rx_bufsize = sc->sc_buffersize - sc->sc_tx_bufsize;
    220 	sc->sc_enabled = 0;
    221 
    222 	/* Test the RAM */
    223 	ea_ramtest(sc);
    224 
    225 	printf("%s: %dKB packet memory, txbuf=%dKB (%d buffers), rxbuf=%dKB",
    226 	    sc->sc_dev.dv_xname, sc->sc_buffersize >> 10,
    227 	    sc->sc_tx_bufsize >> 10, sc->sc_tx_bufs, sc->sc_rx_bufsize >> 10);
    228 
    229 	/* Initialise ifnet structure. */
    230 
    231 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    232 	ifp->if_softc = sc;
    233 	ifp->if_start = ea_start;
    234 	ifp->if_ioctl = ea_ioctl;
    235 	ifp->if_init = ea_init;
    236 	ifp->if_stop = ea_stop;
    237 	ifp->if_watchdog = ea_watchdog;
    238 	ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST | IFF_NOTRAILERS;
    239 	if (sc->sc_variant == SEEQ_8004)
    240 		ifp->if_flags |= IFF_SIMPLEX;
    241 	IFQ_SET_READY(&ifp->if_snd);
    242 
    243 	/* Initialize media goo. */
    244 	ifmedia_init(&sc->sc_media, 0, ea_mediachange, ea_mediastatus);
    245 	if (media != NULL) {
    246 		int i;
    247 
    248 		for (i = 0; i < nmedia; i++)
    249 			ifmedia_add(&sc->sc_media, media[i], 0, NULL);
    250 		ifmedia_set(&sc->sc_media, defmedia);
    251 	} else {
    252 		ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
    253 		ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
    254 	}
    255 
    256 	/* Now we can attach the interface. */
    257 
    258 	if_attach(ifp);
    259 	ether_ifattach(ifp, myaddr);
    260 
    261 	printf("\n");
    262 }
    263 
    264 /*
    265  * Media change callback.
    266  */
    267 static int
    268 ea_mediachange(struct ifnet *ifp)
    269 {
    270 	struct seeq8005_softc *sc = ifp->if_softc;
    271 
    272 	if (sc->sc_mediachange)
    273 		return ((*sc->sc_mediachange)(sc));
    274 	return (EINVAL);
    275 }
    276 
    277 /*
    278  * Media status callback.
    279  */
    280 static void
    281 ea_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
    282 {
    283 	struct seeq8005_softc *sc = ifp->if_softc;
    284 
    285 	if (sc->sc_enabled == 0) {
    286 		ifmr->ifm_active = IFM_ETHER | IFM_NONE;
    287 		ifmr->ifm_status = 0;
    288 		return;
    289 	}
    290 
    291 	if (sc->sc_mediastatus)
    292 		(*sc->sc_mediastatus)(sc, ifmr);
    293 }
    294 
    295 /*
    296  * Test the RAM on the ethernet card.
    297  */
    298 
    299 void
    300 ea_ramtest(struct seeq8005_softc *sc)
    301 {
    302 	bus_space_tag_t iot = sc->sc_iot;
    303 	bus_space_handle_t ioh = sc->sc_ioh;
    304 	int loop;
    305 	u_int sum = 0;
    306 
    307 	/*
    308 	 * Test the buffer memory on the board.
    309 	 * Write simple pattens to it and read them back.
    310 	 */
    311 
    312 	/* Set up the whole buffer RAM for writing */
    313 
    314 	ea_select_buffer(sc, SEEQ_BUFCODE_TX_EAP);
    315 	bus_space_write_2(iot, ioh, SEEQ_BUFWIN, (SEEQ_MAX_BUFFER_SIZE >> 8) - 1);
    316 	bus_space_write_2(iot, ioh, SEEQ_TX_PTR, 0x0000);
    317 	bus_space_write_2(iot, ioh, SEEQ_RX_PTR, SEEQ_MAX_BUFFER_SIZE - 2);
    318 
    319 #define SEEQ_RAMTEST_LOOP(value)						\
    320 do {									\
    321 	/* Set the write start address and write a pattern */		\
    322 	ea_writebuf(sc, NULL, 0x0000, 0);				\
    323 	for (loop = 0; loop < SEEQ_MAX_BUFFER_SIZE; loop += 2)		\
    324 		bus_space_write_2(iot, ioh, SEEQ_BUFWIN, (value));	\
    325 									\
    326 	/* Set the read start address and verify the pattern */		\
    327 	ea_readbuf(sc, NULL, 0x0000, 0);				\
    328 	for (loop = 0; loop < SEEQ_MAX_BUFFER_SIZE; loop += 2)		\
    329 		if (bus_space_read_2(iot, ioh, SEEQ_BUFWIN) != (value)) \
    330 			++sum;						\
    331 } while (/*CONSTCOND*/0)
    332 
    333 	SEEQ_RAMTEST_LOOP(loop);
    334 	SEEQ_RAMTEST_LOOP(loop ^ (SEEQ_MAX_BUFFER_SIZE - 1));
    335 	SEEQ_RAMTEST_LOOP(0xaa55);
    336 	SEEQ_RAMTEST_LOOP(0x55aa);
    337 
    338 	/* Report */
    339 
    340 	if (sum > 0)
    341 		printf("%s: buffer RAM failed self test, %d faults\n",
    342 		       sc->sc_dev.dv_xname, sum);
    343 }
    344 
    345 
    346 /*
    347  * Stop the tx interface.
    348  *
    349  * Returns 0 if the tx was already stopped or 1 if it was active
    350  */
    351 
    352 static int
    353 ea_stoptx(struct seeq8005_softc *sc)
    354 {
    355 	bus_space_tag_t iot = sc->sc_iot;
    356 	bus_space_handle_t ioh = sc->sc_ioh;
    357 	int timeout;
    358 	int status;
    359 
    360 	DPRINTF(SEEQ_DEBUG_TX, ("ea_stoptx()\n"));
    361 
    362 	sc->sc_enabled = 0;
    363 
    364 	status = bus_space_read_2(iot, ioh, SEEQ_STATUS);
    365 	if (!(status & SEEQ_STATUS_TX_ON))
    366 		return 0;
    367 
    368 	/* Stop any tx and wait for confirmation */
    369 	bus_space_write_2(iot, ioh, SEEQ_COMMAND,
    370 			  sc->sc_command | SEEQ_CMD_TX_OFF);
    371 
    372 	timeout = 20000;
    373 	do {
    374 		status = bus_space_read_2(iot, ioh, SEEQ_STATUS);
    375 		delay(1);
    376 	} while ((status & SEEQ_STATUS_TX_ON) && --timeout > 0);
    377  	if (timeout == 0)
    378 		log(LOG_ERR, "%s: timeout waiting for tx termination\n",
    379 		    sc->sc_dev.dv_xname);
    380 
    381 	/* Clear any pending tx interrupt */
    382 	bus_space_write_2(iot, ioh, SEEQ_COMMAND,
    383 		   sc->sc_command | SEEQ_CMD_TX_INTACK);
    384 	return 1;
    385 }
    386 
    387 
    388 /*
    389  * Stop the rx interface.
    390  *
    391  * Returns 0 if the tx was already stopped or 1 if it was active
    392  */
    393 
    394 static int
    395 ea_stoprx(struct seeq8005_softc *sc)
    396 {
    397 	bus_space_tag_t iot = sc->sc_iot;
    398 	bus_space_handle_t ioh = sc->sc_ioh;
    399 	int timeout;
    400 	int status;
    401 
    402 	DPRINTF(SEEQ_DEBUG_RX, ("ea_stoprx()\n"));
    403 
    404 	status = bus_space_read_2(iot, ioh, SEEQ_STATUS);
    405 	if (!(status & SEEQ_STATUS_RX_ON))
    406 		return 0;
    407 
    408 	/* Stop any rx and wait for confirmation */
    409 
    410 	bus_space_write_2(iot, ioh, SEEQ_COMMAND,
    411 			  sc->sc_command | SEEQ_CMD_RX_OFF);
    412 
    413 	timeout = 20000;
    414 	do {
    415 		status = bus_space_read_2(iot, ioh, SEEQ_STATUS);
    416 	} while ((status & SEEQ_STATUS_RX_ON) && --timeout > 0);
    417 	if (timeout == 0)
    418 		log(LOG_ERR, "%s: timeout waiting for rx termination\n",
    419 		    sc->sc_dev.dv_xname);
    420 
    421 	/* Clear any pending rx interrupt */
    422 
    423 	bus_space_write_2(iot, ioh, SEEQ_COMMAND,
    424 		   sc->sc_command | SEEQ_CMD_RX_INTACK);
    425 	return 1;
    426 }
    427 
    428 
    429 /*
    430  * Stop interface.
    431  * Stop all IO and shut the interface down
    432  */
    433 
    434 static void
    435 ea_stop(struct ifnet *ifp, int disable)
    436 {
    437 	struct seeq8005_softc *sc = ifp->if_softc;
    438 	bus_space_tag_t iot = sc->sc_iot;
    439 	bus_space_handle_t ioh = sc->sc_ioh;
    440 
    441 	DPRINTF(SEEQ_DEBUG_MISC, ("ea_stop()\n"));
    442 
    443 	/* Stop all IO */
    444 	ea_stoptx(sc);
    445 	ea_stoprx(sc);
    446 
    447 	/* Disable rx and tx interrupts */
    448 	sc->sc_command &= (SEEQ_CMD_RX_INTEN | SEEQ_CMD_TX_INTEN);
    449 
    450 	/* Clear any pending interrupts */
    451 	bus_space_write_2(iot, ioh, SEEQ_COMMAND,
    452 			  sc->sc_command | SEEQ_CMD_RX_INTACK |
    453 			  SEEQ_CMD_TX_INTACK | SEEQ_CMD_DMA_INTACK |
    454 			  SEEQ_CMD_BW_INTACK);
    455 
    456 	if (sc->sc_variant == SEEQ_8004) {
    457 		/* Put the chip to sleep */
    458 		ea_select_buffer(sc, SEEQ_BUFCODE_CONFIG3);
    459 		bus_space_write_2(iot, ioh, SEEQ_BUFWIN,
    460 		    sc->sc_config3 | SEEQ_CFG3_SLEEP);
    461 	}
    462 
    463 	/* Cancel any watchdog timer */
    464        	sc->sc_ethercom.ec_if.if_timer = 0;
    465 }
    466 
    467 
    468 /*
    469  * Reset the chip
    470  * Following this the software registers are reset
    471  */
    472 
    473 static void
    474 ea_chipreset(struct seeq8005_softc *sc)
    475 {
    476 	bus_space_tag_t iot = sc->sc_iot;
    477 	bus_space_handle_t ioh = sc->sc_ioh;
    478 
    479 	DPRINTF(SEEQ_DEBUG_MISC, ("ea_chipreset()\n"));
    480 
    481 	/* Reset the controller. Min of 4us delay here */
    482 
    483 	bus_space_write_2(iot, ioh, SEEQ_CONFIG2, SEEQ_CFG2_RESET);
    484 	delay(4);
    485 
    486 	sc->sc_command = 0;
    487 	sc->sc_config1 = 0;
    488 	sc->sc_config2 = 0;
    489 	sc->sc_config3 = 0;
    490 }
    491 
    492 
    493 /*
    494  * If the DMA FIFO's in write mode, wait for it to empty.  Needed when
    495  * switching the FIFO from write to read.  We also use it when changing
    496  * the address for writes.
    497  */
    498 static void
    499 ea_await_fifo_empty(struct seeq8005_softc *sc)
    500 {
    501 	bus_space_tag_t iot = sc->sc_iot;
    502 	bus_space_handle_t ioh = sc->sc_ioh;
    503 	int timeout;
    504 
    505 	timeout = 20000;
    506 	if ((bus_space_read_2(iot, ioh, SEEQ_STATUS) &
    507 	     SEEQ_STATUS_FIFO_DIR) != 0)
    508 		return; /* FIFO is reading anyway. */
    509 	while (--timeout > 0)
    510 		if (bus_space_read_2(iot, ioh, SEEQ_STATUS) &
    511 		    SEEQ_STATUS_FIFO_EMPTY)
    512 			return;
    513 	log(LOG_ERR, "%s: DMA FIFO failed to empty\n", sc->sc_dev.dv_xname);
    514 }
    515 
    516 /*
    517  * Wait for the DMA FIFO to fill before reading from it.
    518  */
    519 static void
    520 ea_await_fifo_full(struct seeq8005_softc *sc)
    521 {
    522 	bus_space_tag_t iot = sc->sc_iot;
    523 	bus_space_handle_t ioh = sc->sc_ioh;
    524 	int timeout;
    525 
    526 	timeout = 20000;
    527 	while (--timeout > 0)
    528 		if (bus_space_read_2(iot, ioh, SEEQ_STATUS) &
    529 		    SEEQ_STATUS_FIFO_FULL)
    530 			return;
    531 	log(LOG_ERR, "%s: DMA FIFO failed to fill\n", sc->sc_dev.dv_xname);
    532 }
    533 
    534 /*
    535  * write to the buffer memory on the interface
    536  *
    537  * The buffer address is set to ADDR.
    538  * If len != 0 then data is copied from the address starting at buf
    539  * to the interface buffer.
    540  * BUF must be usable as a u_int16_t *.
    541  * If LEN is odd, it must be safe to overwrite one extra byte.
    542  */
    543 
    544 static void
    545 ea_writebuf(struct seeq8005_softc *sc, u_char *buf, int addr, size_t len)
    546 {
    547 	bus_space_tag_t iot = sc->sc_iot;
    548 	bus_space_handle_t ioh = sc->sc_ioh;
    549 
    550 	DPRINTF(SEEQ_DEBUG_MISC, ("writebuf: st=%04x\n",
    551 	    bus_space_read_2(iot, ioh, SEEQ_STATUS)));
    552 
    553 #ifdef DIAGNOSTIC
    554 	if (__predict_false(!ALIGNED_POINTER(buf, u_int16_t)))
    555 		panic("%s: unaligned writebuf", sc->sc_dev.dv_xname);
    556 	if (__predict_false(addr >= SEEQ_MAX_BUFFER_SIZE))
    557 		panic("%s: writebuf out of range", sc->sc_dev.dv_xname);
    558 #endif
    559 
    560 	/* Assume that copying too much is safe. */
    561 	if (len % 2 != 0)
    562 		len++;
    563 
    564 	if (addr != -1) {
    565 		ea_await_fifo_empty(sc);
    566 
    567 		ea_select_buffer(sc, SEEQ_BUFCODE_LOCAL_MEM);
    568 		bus_space_write_2(iot, ioh, SEEQ_COMMAND,
    569 		    sc->sc_command | SEEQ_CMD_FIFO_WRITE);
    570 		bus_space_write_2(iot, ioh, SEEQ_DMA_ADDR, addr);
    571 	}
    572 
    573 	if (len > 0)
    574 		bus_space_write_multi_2(iot, ioh, SEEQ_BUFWIN,
    575 					(u_int16_t *)buf, len / 2);
    576 	/* Leave FIFO to empty in the background */
    577 }
    578 
    579 
    580 /*
    581  * read from the buffer memory on the interface
    582  *
    583  * The buffer address is set to ADDR.
    584  * If len != 0 then data is copied from the interface buffer to the
    585  * address starting at buf.
    586  * BUF must be usable as a u_int16_t *.
    587  * If LEN is odd, it must be safe to overwrite one extra byte.
    588  */
    589 
    590 static void
    591 ea_readbuf(struct seeq8005_softc *sc, u_char *buf, int addr, size_t len)
    592 {
    593 	bus_space_tag_t iot = sc->sc_iot;
    594 	bus_space_handle_t ioh = sc->sc_ioh;
    595 	int runup;
    596 
    597 	DPRINTF(SEEQ_DEBUG_MISC, ("readbuf: st=%04x addr=%04x len=%d\n",
    598 	    bus_space_read_2(iot, ioh, SEEQ_STATUS), addr, len));
    599 
    600 #ifdef DIAGNOSTIC
    601 	if (__predict_false(!ALIGNED_POINTER(buf, u_int16_t)))
    602 		panic("%s: unaligned readbuf", sc->sc_dev.dv_xname);
    603 	if (__predict_false(addr >= SEEQ_MAX_BUFFER_SIZE))
    604 		panic("%s: readbuf out of range", sc->sc_dev.dv_xname);
    605 #endif
    606 
    607 	/* Assume that copying too much is safe. */
    608 	if (len % 2 != 0)
    609 		len++;
    610 
    611 	if (addr != -1) {
    612 		/*
    613 		 * SEEQ 80C04 bug:
    614 		 * Starting reading from certain addresses seems to cause
    615 		 * us to get bogus results, so we avoid them.
    616 		 */
    617 		runup = 0;
    618 		if (sc->sc_variant == SEEQ_8004 &&
    619 		    ((addr & 0x00ff) == 0x00ea ||
    620 		     (addr & 0x00ff) == 0x00ee ||
    621 		     (addr & 0x00ff) == 0x00f0))
    622 			runup = (addr & 0x00ff) - 0x00e8;
    623 
    624 		ea_await_fifo_empty(sc);
    625 
    626 		ea_select_buffer(sc, SEEQ_BUFCODE_LOCAL_MEM);
    627 
    628 		/*
    629 		 * 80C04 bug workaround.  I found this in the old arm32 "eb"
    630 		 * driver.  I've no idea what it does, but it seems to stop
    631 		 * the chip mangling data so often.
    632 		 */
    633 		bus_space_write_2(iot, ioh, SEEQ_COMMAND,
    634 		    sc->sc_command | SEEQ_CMD_FIFO_WRITE);
    635 		ea_await_fifo_empty(sc);
    636 
    637 		bus_space_write_2(iot, ioh, SEEQ_DMA_ADDR, addr - runup);
    638 		bus_space_write_2(iot, ioh, SEEQ_COMMAND,
    639 		    sc->sc_command | SEEQ_CMD_FIFO_READ);
    640 
    641 		ea_await_fifo_full(sc);
    642 		while (runup > 0) {
    643 			(void)bus_space_read_2(iot, ioh, SEEQ_BUFWIN);
    644 			runup -= 2;
    645 		}
    646 	}
    647 
    648 	if (len > 0)
    649 		bus_space_read_multi_2(iot, ioh, SEEQ_BUFWIN,
    650 				       (u_int16_t *)buf, len / 2);
    651 }
    652 
    653 static void
    654 ea_select_buffer(struct seeq8005_softc *sc, int bufcode)
    655 {
    656 
    657 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, SEEQ_CONFIG1,
    658 			  sc->sc_config1 | bufcode);
    659 }
    660 
    661 /* Must be called at splnet */
    662 static void
    663 ea_set_address(struct seeq8005_softc *sc, int which, u_int8_t const *ea)
    664 {
    665 	int i;
    666 
    667 	ea_select_buffer(sc, SEEQ_BUFCODE_STATION_ADDR0 + which);
    668 	for (i = 0; i < ETHER_ADDR_LEN; ++i)
    669 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, SEEQ_BUFWIN,
    670 				  ea[i]);
    671 }
    672 
    673 /*
    674  * Initialize interface.
    675  *
    676  * This should leave the interface in a state for packet reception and
    677  * transmission.
    678  */
    679 
    680 static int
    681 ea_init(struct ifnet *ifp)
    682 {
    683 	struct seeq8005_softc *sc = ifp->if_softc;
    684 	bus_space_tag_t iot = sc->sc_iot;
    685 	bus_space_handle_t ioh = sc->sc_ioh;
    686 	int s;
    687 
    688 	DPRINTF(SEEQ_DEBUG_MISC, ("ea_init()\n"));
    689 
    690 	s = splnet();
    691 
    692 	/* First, reset the board. */
    693 
    694 	ea_chipreset(sc);
    695 
    696 	/* Set up defaults for the registers */
    697 
    698 	sc->sc_command = 0;
    699 	sc->sc_config1 = 0;
    700 #if BYTE_ORDER == BIG_ENDIAN
    701 	sc->sc_config2 = SEEQ_CFG2_BYTESWAP;
    702 #else
    703 	sc->sc_config2 = 0;
    704 #endif
    705 	sc->sc_config3 = 0;
    706 
    707 	bus_space_write_2(iot, ioh, SEEQ_COMMAND, sc->sc_command);
    708 	bus_space_write_2(iot, ioh, SEEQ_CONFIG1, sc->sc_config1);
    709 	bus_space_write_2(iot, ioh, SEEQ_CONFIG2, sc->sc_config2);
    710 	if (sc->sc_variant == SEEQ_8004) {
    711 		ea_select_buffer(sc, SEEQ_BUFCODE_CONFIG3);
    712 		bus_space_write_2(iot, ioh, SEEQ_BUFWIN, sc->sc_config3);
    713 	}
    714 
    715 	/* Write the station address - the receiver must be off */
    716 	ea_set_address(sc, 0, LLADDR(ifp->if_sadl));
    717 
    718 	/* Split board memory into Rx and Tx. */
    719 	ea_select_buffer(sc, SEEQ_BUFCODE_TX_EAP);
    720 	bus_space_write_2(iot, ioh, SEEQ_BUFWIN, (sc->sc_tx_bufsize>> 8) - 1);
    721 
    722 	if (sc->sc_variant == SEEQ_8004)
    723 		sc->sc_config2 |= SEEQ_CFG2_RX_TX_DISABLE;
    724 
    725 	/* Configure rx. */
    726 	ea_mc_reset(sc);
    727 	if (ifp->if_flags & IFF_PROMISC)
    728 		sc->sc_config1 = SEEQ_CFG1_PROMISCUOUS;
    729 	else if ((ifp->if_flags & IFF_ALLMULTI) || sc->sc_variant == SEEQ_8004)
    730 		sc->sc_config1 = SEEQ_CFG1_MULTICAST;
    731 	else
    732 		sc->sc_config1 = SEEQ_CFG1_BROADCAST;
    733 	sc->sc_config1 |= SEEQ_CFG1_STATION_ADDR0;
    734 	bus_space_write_2(iot, ioh, SEEQ_CONFIG1, sc->sc_config1);
    735 
    736 	/* Setup the Rx pointers */
    737 	sc->sc_rx_ptr = sc->sc_tx_bufsize;
    738 
    739 	bus_space_write_2(iot, ioh, SEEQ_RX_PTR, sc->sc_rx_ptr);
    740 	bus_space_write_2(iot, ioh, SEEQ_RX_END, sc->sc_rx_ptr >> 8);
    741 
    742 
    743 	/* Place a NULL header at the beginning of the receive area */
    744 	ea_writebuf(sc, NULL, sc->sc_rx_ptr, 0);
    745 
    746 	bus_space_write_2(iot, ioh, SEEQ_BUFWIN, 0x0000);
    747 	bus_space_write_2(iot, ioh, SEEQ_BUFWIN, 0x0000);
    748 
    749 
    750 	/* Configure TX. */
    751 	DPRINTF(SEEQ_DEBUG_MISC, ("Configuring tx...\n"));
    752 
    753 	bus_space_write_2(iot, ioh, SEEQ_TX_PTR, 0x0000);
    754 
    755 	sc->sc_config2 |= SEEQ_CFG2_OUTPUT;
    756 	bus_space_write_2(iot, ioh, SEEQ_CONFIG2, sc->sc_config2);
    757 
    758 	/* Reset tx buffer pointers */
    759 	sc->sc_tx_cur = 0;
    760 	sc->sc_tx_used = 0;
    761 	sc->sc_tx_next = 0;
    762 
    763 	/* Place a NULL header at the beginning of the transmit area */
    764 	ea_writebuf(sc, NULL, 0x0000, 0);
    765 
    766 	bus_space_write_2(iot, ioh, SEEQ_BUFWIN, 0x0000);
    767 	bus_space_write_2(iot, ioh, SEEQ_BUFWIN, 0x0000);
    768 
    769 	sc->sc_command |= SEEQ_CMD_TX_INTEN;
    770 	bus_space_write_2(iot, ioh, SEEQ_COMMAND, sc->sc_command);
    771 
    772 	/* Turn on Rx */
    773 	sc->sc_command |= SEEQ_CMD_RX_INTEN;
    774 	bus_space_write_2(iot, ioh, SEEQ_COMMAND,
    775 			  sc->sc_command | SEEQ_CMD_RX_ON);
    776 
    777 	/* TX_ON gets set by ea_txpacket when there's something to transmit. */
    778 
    779 
    780 	/* Set flags appropriately. */
    781 	ifp->if_flags |= IFF_RUNNING;
    782 	ifp->if_flags &= ~IFF_OACTIVE;
    783 	sc->sc_enabled = 1;
    784 
    785 	/* And start output. */
    786 	ea_start(ifp);
    787 
    788 	splx(s);
    789 	return 0;
    790 }
    791 
    792 /*
    793  * Start output on interface. Get datagrams from the queue and output them,
    794  * giving the receiver a chance between datagrams. Call only from splnet or
    795  * interrupt level!
    796  */
    797 
    798 static void
    799 ea_start(struct ifnet *ifp)
    800 {
    801 	struct seeq8005_softc *sc = ifp->if_softc;
    802 	int s;
    803 
    804 	s = splnet();
    805 	DPRINTF(SEEQ_DEBUG_TX, ("ea_start()...\n"));
    806 
    807 	/*
    808 	 * Don't do anything if output is active.  seeq8005intr() will call
    809 	 * us (actually eatxpacket()) back when the card's ready for more
    810 	 * frames.
    811 	 */
    812 	if (ifp->if_flags & IFF_OACTIVE)
    813 		return;
    814 
    815 	/* Mark interface as output active */
    816 
    817 	ifp->if_flags |= IFF_OACTIVE;
    818 
    819 	/* tx packets */
    820 
    821 	eatxpacket(sc);
    822 	splx(s);
    823 }
    824 
    825 
    826 /*
    827  * Transfer a packet to the interface buffer and start transmission
    828  *
    829  * Called at splnet()
    830  */
    831 
    832 void
    833 eatxpacket(struct seeq8005_softc *sc)
    834 {
    835 	bus_space_tag_t iot = sc->sc_iot;
    836 	bus_space_handle_t ioh = sc->sc_ioh;
    837 	struct mbuf *m0;
    838 	struct ifnet *ifp;
    839 
    840 	ifp = &sc->sc_ethercom.ec_if;
    841 
    842 	/* Dequeue the next packet. */
    843 	IFQ_DEQUEUE(&ifp->if_snd, m0);
    844 
    845 	/* If there's nothing to send, return. */
    846 	if (!m0) {
    847 		ifp->if_flags &= ~IFF_OACTIVE;
    848 		sc->sc_config2 |= SEEQ_CFG2_OUTPUT;
    849 		bus_space_write_2(iot, ioh, SEEQ_CONFIG2, sc->sc_config2);
    850 		DPRINTF(SEEQ_DEBUG_TX, ("tx finished\n"));
    851 		return;
    852 	}
    853 
    854 #if NBPFILTER > 0
    855 	/* Give the packet to the bpf, if any. */
    856 	if (ifp->if_bpf)
    857 		bpf_mtap(ifp->if_bpf, m0);
    858 #endif
    859 
    860 	DPRINTF(SEEQ_DEBUG_TX, ("Tx new packet\n"));
    861 
    862 	sc->sc_config2 &= ~SEEQ_CFG2_OUTPUT;
    863 	bus_space_write_2(iot, ioh, SEEQ_CONFIG2, sc->sc_config2);
    864 
    865 	ea_writembuf(sc, m0, 0x0000);
    866 	m_freem(m0);
    867 
    868 	bus_space_write_2(iot, ioh, SEEQ_TX_PTR, 0x0000);
    869 
    870 	/* Now transmit the datagram. */
    871 	bus_space_write_2(iot, ioh, SEEQ_COMMAND,
    872 			  sc->sc_command | SEEQ_CMD_TX_ON);
    873 
    874 	/* Make sure we notice if the chip goes silent on us. */
    875 	ifp->if_timer = 5;
    876 
    877 	DPRINTF(SEEQ_DEBUG_TX,
    878 	    ("st=%04x\n", bus_space_read_2(iot, ioh, SEEQ_STATUS)));
    879 	DPRINTF(SEEQ_DEBUG_TX, ("tx: queued\n"));
    880 }
    881 
    882 /*
    883  * Copy a packet from an mbuf to the transmit buffer on the card.
    884  *
    885  * Puts a valid Tx header at the start of the packet, and a null header at
    886  * the end.
    887  */
    888 static int
    889 ea_writembuf(struct seeq8005_softc *sc, struct mbuf *m0, int bufstart)
    890 {
    891 	struct mbuf *m;
    892 	int len, nextpacket;
    893 	u_int8_t hdr[4];
    894 
    895 	/*
    896 	 * Copy the datagram to the packet buffer.
    897 	 */
    898 	ea_writebuf(sc, NULL, bufstart + 4, 0);
    899 
    900 	len = 0;
    901 	for (m = m0; m; m = m->m_next) {
    902 		if (m->m_len == 0)
    903 			continue;
    904 		ea_writebuf(sc, mtod(m, caddr_t), -1, m->m_len);
    905 		len += m->m_len;
    906 	}
    907 
    908 	/* If packet size is odd round up to the next 16 bit boundry */
    909 	if (len % 2)
    910 		++len;
    911 
    912 	len = max(len, ETHER_MIN_LEN);
    913 
    914 	ea_writebuf(sc, NULL, bufstart + 4 + len, 0);
    915 	/* Follow it with a NULL packet header */
    916 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, SEEQ_BUFWIN, 0x0000);
    917 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, SEEQ_BUFWIN, 0x0000);
    918 
    919 	/* Ok we now have a packet len bytes long in our packet buffer */
    920 	DPRINTF(SEEQ_DEBUG_TX, ("ea_writembuf: length=%d\n", len));
    921 
    922 	/* Write the packet header */
    923 	nextpacket = len + 4;
    924 	hdr[0] = (nextpacket >> 8) & 0xff;
    925 	hdr[1] = nextpacket & 0xff;
    926 	hdr[2] = SEEQ_PKTCMD_TX | SEEQ_PKTCMD_DATA_FOLLOWS |
    927 		SEEQ_TXCMD_XMIT_SUCCESS_INT | SEEQ_TXCMD_COLLISION_INT;
    928 	hdr[3] = 0; /* Status byte -- will be update by hardware. */
    929 	ea_writebuf(sc, hdr, 0x0000, 4);
    930 
    931 	return len;
    932 }
    933 
    934 /*
    935  * Ethernet controller interrupt.
    936  */
    937 
    938 int
    939 seeq8005intr(void *arg)
    940 {
    941 	struct seeq8005_softc *sc = arg;
    942 	bus_space_tag_t iot = sc->sc_iot;
    943 	bus_space_handle_t ioh = sc->sc_ioh;
    944 	int status, handled;
    945 
    946 	handled = 0;
    947 
    948 	/* Get the controller status */
    949 	status = bus_space_read_2(iot, ioh, SEEQ_STATUS);
    950 
    951 	/* Tx interrupt ? */
    952 	if (status & SEEQ_STATUS_TX_INT) {
    953 		handled = 1;
    954 
    955 		/* Acknowledge the interrupt */
    956 		bus_space_write_2(iot, ioh, SEEQ_COMMAND,
    957 				  sc->sc_command | SEEQ_CMD_TX_INTACK);
    958 
    959 		ea_txint(sc);
    960 	}
    961 
    962 
    963 	/* Rx interrupt ? */
    964 	if (status & SEEQ_STATUS_RX_INT) {
    965 		handled = 1;
    966 
    967 		/* Acknowledge the interrupt */
    968 		bus_space_write_2(iot, ioh, SEEQ_COMMAND,
    969 				  sc->sc_command | SEEQ_CMD_RX_INTACK);
    970 
    971 		/* Processes the received packets */
    972 		ea_rxint(sc);
    973 	}
    974 
    975 	return handled;
    976 }
    977 
    978 static void
    979 ea_txint(struct seeq8005_softc *sc)
    980 {
    981 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    982 	bus_space_tag_t iot = sc->sc_iot;
    983 	bus_space_handle_t ioh = sc->sc_ioh;
    984 	u_int8_t txhdr[4];
    985 	u_int txstatus;
    986 
    987 	ea_readbuf(sc, txhdr, 0x0000, 4);
    988 
    989 	DPRINTF(SEEQ_DEBUG_TX, ("txstatus=%02x %02x %02x %02x\n",
    990 	    txhdr[0], txhdr[1], txhdr[2], txhdr[3]));
    991 	txstatus = txhdr[3];
    992 
    993 	/*
    994 	 * If SEEQ_TXSTAT_COLLISION is set then we received at least
    995 	 * one collision. On the 8004 we can find out exactly how many
    996 	 * collisions occurred.
    997 	 *
    998 	 * The SEEQ_PKTSTAT_DONE will be set if the transmission has
    999 	 * completed.
   1000 	 *
   1001 	 * If SEEQ_TXSTAT_COLLISION16 is set then 16 collisions
   1002 	 * occurred and the packet transmission was aborted.
   1003 	 * This situation is untested as present.
   1004 	 *
   1005 	 * The SEEQ_TXSTAT_BABBLE should never be set and is untested
   1006 	 * as we should never xmit oversized packets.
   1007 	 */
   1008 	if (txstatus & SEEQ_TXSTAT_COLLISION) {
   1009 		switch (sc->sc_variant) {
   1010 		case SEEQ_8004: {
   1011 			int colls;
   1012 
   1013 			/*
   1014 			 * The 8004 contains a 4 bit collision count
   1015 			 * in the status register.
   1016 			 */
   1017 
   1018 			/* This appears to be broken on 80C04.AE */
   1019 /*			ifp->if_collisions +=
   1020 			    (txstatus >> SEEQ_TXSTAT_COLLISIONS_SHIFT)
   1021 			    & SEEQ_TXSTAT_COLLISION_MASK;*/
   1022 
   1023 			/* Use the TX Collision register */
   1024 			ea_select_buffer(sc, SEEQ_BUFCODE_TX_COLLS);
   1025 			colls = bus_space_read_1(iot, ioh, SEEQ_BUFWIN);
   1026 			ifp->if_collisions += colls;
   1027 			break;
   1028 		}
   1029 		case SEEQ_8005:
   1030 			/* We known there was at least 1 collision */
   1031 			ifp->if_collisions++;
   1032 			break;
   1033 		}
   1034 	} else if (txstatus & SEEQ_TXSTAT_COLLISION16) {
   1035 		printf("seeq_intr: col16 %x\n", txstatus);
   1036 		ifp->if_collisions += 16;
   1037 		ifp->if_oerrors++;
   1038 	} else if (txstatus & SEEQ_TXSTAT_BABBLE) {
   1039 		ifp->if_oerrors++;
   1040 	}
   1041 
   1042 	/* Have we completed transmission on the packet ? */
   1043 	if (txstatus & SEEQ_PKTSTAT_DONE) {
   1044 		/* Clear watchdog timer. */
   1045 		ifp->if_timer = 0;
   1046 		ifp->if_flags &= ~IFF_OACTIVE;
   1047 
   1048 		/* Update stats */
   1049 		ifp->if_opackets++;
   1050 
   1051 		/* Tx next packet */
   1052 
   1053 		eatxpacket(sc);
   1054 	}
   1055 }
   1056 
   1057 void
   1058 ea_rxint(struct seeq8005_softc *sc)
   1059 {
   1060 	bus_space_tag_t iot = sc->sc_iot;
   1061 	bus_space_handle_t ioh = sc->sc_ioh;
   1062 	u_int addr;
   1063 	int len;
   1064 	int ctrl;
   1065 	int ptr;
   1066 	int pack;
   1067 	int status;
   1068 	u_int8_t rxhdr[4];
   1069 	struct ifnet *ifp;
   1070 
   1071 	ifp = &sc->sc_ethercom.ec_if;
   1072 
   1073 
   1074 	/* We start from the last rx pointer position */
   1075 	addr = sc->sc_rx_ptr;
   1076 	sc->sc_config2 &= ~SEEQ_CFG2_OUTPUT;
   1077 	bus_space_write_2(iot, ioh, SEEQ_CONFIG2, sc->sc_config2);
   1078 
   1079 	do {
   1080 		/* Read rx header */
   1081 		ea_readbuf(sc, rxhdr, addr, 4);
   1082 
   1083 		/* Split the packet header */
   1084 		ptr = (rxhdr[0] << 8) | rxhdr[1];
   1085 		ctrl = rxhdr[2];
   1086 		status = rxhdr[3];
   1087 
   1088 		DPRINTF(SEEQ_DEBUG_RX,
   1089 		    ("addr=%04x ptr=%04x ctrl=%02x status=%02x\n",
   1090 			addr, ptr, ctrl, status));
   1091 
   1092 		/* Zero packet ptr ? then must be null header so exit */
   1093 		if (ptr == 0) break;
   1094 
   1095 		/* Sanity-check the next-packet pointer and flags. */
   1096 		if (__predict_false(ptr < sc->sc_tx_bufsize ||
   1097 		    (ctrl & SEEQ_PKTCMD_TX))) {
   1098 			++ifp->if_ierrors;
   1099 			log(LOG_ERR,
   1100 			    "%s: Rx chain corrupt at %04x (ptr = %04x)\n",
   1101 			    sc->sc_dev.dv_xname, addr, ptr);
   1102 			ea_init(ifp);
   1103 			return;
   1104 		}
   1105 
   1106 		/* Get packet length */
   1107        		len = (ptr - addr) - 4;
   1108 
   1109 		if (len < 0)
   1110 			len += sc->sc_rx_bufsize;
   1111 		DPRINTF(SEEQ_DEBUG_RX, ("len=%04x\n", len));
   1112 
   1113 		/* Has the packet rx completed ? if not then exit */
   1114 		if ((status & SEEQ_PKTSTAT_DONE) == 0)
   1115 			break;
   1116 
   1117 		/*
   1118 		 * Did we have any errors? then note error and go to
   1119 		 * next packet
   1120 		 */
   1121 		if (__predict_false(status & SEEQ_RXSTAT_ERROR_MASK)) {
   1122 			++ifp->if_ierrors;
   1123 			/* XXX oversize packets may be OK */
   1124 			log(LOG_WARNING,
   1125 			    "%s: rx packet error at %04x (err=%02x)\n",
   1126 			    sc->sc_dev.dv_xname, addr, status & 0x0f);
   1127 			/* XXX shouldn't need to reset if it's genuine. */
   1128 			ea_init(ifp);
   1129 			return;
   1130 		}
   1131 		/*
   1132 		 * Is the packet too big ? - this will probably be trapped
   1133 		 * above as a receive error.  If it's not, this is indicative
   1134 		 * of buffer corruption.
   1135 		 */
   1136 		if (__predict_false(len > (ETHER_MAX_LEN - ETHER_CRC_LEN))) {
   1137 			++ifp->if_ierrors;
   1138 			log(LOG_ERR,
   1139 			    "%s: rx packet size error at %04x (len=%d)\n",
   1140 			    sc->sc_dev.dv_xname, addr, len);
   1141 			sc->sc_config2 |= SEEQ_CFG2_OUTPUT;
   1142 			bus_space_write_2(iot, ioh, SEEQ_CONFIG2,
   1143 					  sc->sc_config2);
   1144 			ea_init(ifp);
   1145 			return;
   1146 		}
   1147 
   1148 		ifp->if_ipackets++;
   1149 		/* Pass data up to upper levels. */
   1150 		ea_read(sc, addr + 4, len);
   1151 
   1152 		addr = ptr;
   1153 		++pack;
   1154 	} while (len != 0);
   1155 
   1156 	sc->sc_config2 |= SEEQ_CFG2_OUTPUT;
   1157 	bus_space_write_2(iot, ioh, SEEQ_CONFIG2, sc->sc_config2);
   1158 
   1159 	DPRINTF(SEEQ_DEBUG_RX, ("new rx ptr=%04x\n", addr));
   1160 
   1161 	/* Store new rx pointer */
   1162 	sc->sc_rx_ptr = addr;
   1163 	bus_space_write_2(iot, ioh, SEEQ_RX_END, sc->sc_rx_ptr >> 8);
   1164 
   1165 	/* Make sure the receiver is on */
   1166 	bus_space_write_2(iot, ioh, SEEQ_COMMAND,
   1167 			  sc->sc_command | SEEQ_CMD_RX_ON);
   1168 }
   1169 
   1170 
   1171 /*
   1172  * Pass a packet up to the higher levels.
   1173  */
   1174 
   1175 static void
   1176 ea_read(struct seeq8005_softc *sc, int addr, int len)
   1177 {
   1178 	struct mbuf *m;
   1179 	struct ifnet *ifp;
   1180 
   1181 	ifp = &sc->sc_ethercom.ec_if;
   1182 
   1183 	/* Pull packet off interface. */
   1184 	m = ea_get(sc, addr, len, ifp);
   1185 	if (m == 0)
   1186 		return;
   1187 
   1188 #if NBPFILTER > 0
   1189 	/*
   1190 	 * Check if there's a BPF listener on this interface.
   1191 	 * If so, hand off the raw packet to bpf.
   1192 	 */
   1193 	if (ifp->if_bpf)
   1194 		bpf_mtap(ifp->if_bpf, m);
   1195 #endif
   1196 
   1197 	(*ifp->if_input)(ifp, m);
   1198 }
   1199 
   1200 /*
   1201  * Pull read data off a interface.  Len is length of data, with local net
   1202  * header stripped.  We copy the data into mbufs.  When full cluster sized
   1203  * units are present we copy into clusters.
   1204  */
   1205 
   1206 struct mbuf *
   1207 ea_get(struct seeq8005_softc *sc, int addr, int totlen, struct ifnet *ifp)
   1208 {
   1209         struct mbuf *top, **mp, *m;
   1210         int len;
   1211         u_int cp, epkt;
   1212 
   1213         cp = addr;
   1214         epkt = cp + totlen;
   1215 
   1216         MGETHDR(m, M_DONTWAIT, MT_DATA);
   1217         if (m == 0)
   1218                 return 0;
   1219         m->m_pkthdr.rcvif = ifp;
   1220         m->m_pkthdr.len = totlen;
   1221         m->m_len = MHLEN;
   1222         top = 0;
   1223         mp = &top;
   1224 
   1225         while (totlen > 0) {
   1226                 if (top) {
   1227                         MGET(m, M_DONTWAIT, MT_DATA);
   1228                         if (m == 0) {
   1229                                 m_freem(top);
   1230                                 return 0;
   1231                         }
   1232                         m->m_len = MLEN;
   1233                 }
   1234                 len = min(totlen, epkt - cp);
   1235                 if (len >= MINCLSIZE) {
   1236                         MCLGET(m, M_DONTWAIT);
   1237                         if (m->m_flags & M_EXT)
   1238                                 m->m_len = len = min(len, MCLBYTES);
   1239                         else
   1240                                 len = m->m_len;
   1241                 } else {
   1242                         /*
   1243                          * Place initial small packet/header at end of mbuf.
   1244                          */
   1245                         if (len < m->m_len) {
   1246                                 if (top == 0 && len + max_linkhdr <= m->m_len)
   1247                                         m->m_data += max_linkhdr;
   1248                                 m->m_len = len;
   1249                         } else
   1250                                 len = m->m_len;
   1251                 }
   1252 		if (top == 0) {
   1253 			/* Make sure the payload is aligned */
   1254 			caddr_t newdata = (caddr_t)
   1255 			    ALIGN(m->m_data + sizeof(struct ether_header)) -
   1256 			    sizeof(struct ether_header);
   1257 			len -= newdata - m->m_data;
   1258 			m->m_len = len;
   1259 			m->m_data = newdata;
   1260 		}
   1261                 ea_readbuf(sc, mtod(m, u_char *),
   1262 		    cp < SEEQ_MAX_BUFFER_SIZE ? cp : cp - sc->sc_rx_bufsize,
   1263 		    len);
   1264                 cp += len;
   1265                 *mp = m;
   1266                 mp = &m->m_next;
   1267                 totlen -= len;
   1268                 if (cp == epkt)
   1269                         cp = addr;
   1270         }
   1271 
   1272         return top;
   1273 }
   1274 
   1275 /*
   1276  * Process an ioctl request.  Mostly boilerplate.
   1277  */
   1278 static int
   1279 ea_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
   1280 {
   1281 	struct seeq8005_softc *sc = ifp->if_softc;
   1282 	int s, error = 0;
   1283 
   1284 	s = splnet();
   1285 	switch (cmd) {
   1286 
   1287 	default:
   1288 		error = ether_ioctl(ifp, cmd, data);
   1289 		if (error == ENETRESET) {
   1290 			/*
   1291 			 * Multicast list has changed; set the hardware filter
   1292 			 * accordingly.
   1293 			 */
   1294 			ea_mc_reset(sc);
   1295 			error = 0;
   1296 		}
   1297 		break;
   1298 	}
   1299 
   1300 	splx(s);
   1301 	return error;
   1302 }
   1303 
   1304 /* Must be called at splnet() */
   1305 
   1306 static void
   1307 ea_mc_reset(struct seeq8005_softc *sc)
   1308 {
   1309 
   1310 	switch (sc->sc_variant) {
   1311 	case SEEQ_8004:
   1312 		ea_mc_reset_8004(sc);
   1313 		return;
   1314 	case SEEQ_8005:
   1315 		ea_mc_reset_8005(sc);
   1316 		return;
   1317 	}
   1318 }
   1319 
   1320 static void
   1321 ea_mc_reset_8004(struct seeq8005_softc *sc)
   1322 {
   1323 	struct ethercom *ec = &sc->sc_ethercom;
   1324 	struct ifnet *ifp = &ec->ec_if;
   1325 	struct ether_multi *enm;
   1326 	u_int8_t *cp, c;
   1327 	u_int32_t crc;
   1328 	int i, len;
   1329 	struct ether_multistep step;
   1330 	u_int8_t af[8];
   1331 
   1332 	/*
   1333 	 * Set up multicast address filter by passing all multicast addresses
   1334 	 * through a crc generator, and then using bits 2 - 7 as an index
   1335 	 * into the 64 bit logical address filter.  The high order bits
   1336 	 * selects the word, while the rest of the bits select the bit within
   1337 	 * the word.
   1338 	 */
   1339 
   1340 	if (ifp->if_flags & IFF_PROMISC) {
   1341 		ifp->if_flags |= IFF_ALLMULTI;
   1342 		for (i = 0; i < 8; i++)
   1343 			af[i] = 0xff;
   1344 		return;
   1345 	}
   1346 	for (i = 0; i < 8; i++)
   1347 		af[i] = 0;
   1348 	ETHER_FIRST_MULTI(step, ec, enm);
   1349 	while (enm != NULL) {
   1350 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
   1351 		    sizeof(enm->enm_addrlo)) != 0) {
   1352 			/*
   1353 			 * We must listen to a range of multicast addresses.
   1354 			 * For now, just accept all multicasts, rather than
   1355 			 * trying to set only those filter bits needed to match
   1356 			 * the range.  (At this time, the only use of address
   1357 			 * ranges is for IP multicast routing, for which the
   1358 			 * range is big enough to require all bits set.)
   1359 			 */
   1360 			ifp->if_flags |= IFF_ALLMULTI;
   1361 			for (i = 0; i < 8; i++)
   1362 				af[i] = 0xff;
   1363 			break;
   1364 		}
   1365 		cp = enm->enm_addrlo;
   1366 		crc = 0xffffffff;
   1367 		for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
   1368 			c = *cp++;
   1369 			for (i = 8; --i >= 0;) {
   1370 				if (((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01)) {
   1371 					crc <<= 1;
   1372 					crc ^= 0x04c11db6 | 1;
   1373 				} else
   1374 					crc <<= 1;
   1375 				c >>= 1;
   1376 			}
   1377 		}
   1378 		/* Just want the 6 most significant bits. */
   1379 		crc = (crc >> 2) & 0x3f;
   1380 
   1381 		/* Turn on the corresponding bit in the filter. */
   1382 		af[crc >> 3] |= 1 << (crc & 0x7);
   1383 
   1384 		ETHER_NEXT_MULTI(step, enm);
   1385 	}
   1386 	ifp->if_flags &= ~IFF_ALLMULTI;
   1387 
   1388 	ea_select_buffer(sc, SEEQ_BUFCODE_MULTICAST);
   1389 		for (i = 0; i < 8; ++i)
   1390 			bus_space_write_1(sc->sc_iot, sc->sc_ioh,
   1391 			    SEEQ_BUFWIN, af[i]);
   1392 }
   1393 
   1394 static void
   1395 ea_mc_reset_8005(struct seeq8005_softc *sc)
   1396 {
   1397 	struct ether_multi *enm;
   1398 	struct ether_multistep step;
   1399 	int naddr, maxaddrs;
   1400 
   1401 	naddr = 0;
   1402 	maxaddrs = 5;
   1403 	ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
   1404 	while (enm != NULL) {
   1405 		/* Have we got space? */
   1406 		if (naddr >= maxaddrs ||
   1407 		    bcmp(enm->enm_addrlo, enm->enm_addrhi, 6) != 0) {
   1408 			sc->sc_ethercom.ec_if.if_flags |= IFF_ALLMULTI;
   1409 			ea_ioctl(&sc->sc_ethercom.ec_if, SIOCSIFFLAGS, NULL);
   1410 			return;
   1411 		}
   1412 		ea_set_address(sc, 1 + naddr, enm->enm_addrlo);
   1413 		sc->sc_config1 |= SEEQ_CFG1_STATION_ADDR1 << naddr;
   1414 		naddr++;
   1415 		ETHER_NEXT_MULTI(step, enm);
   1416 	}
   1417 	for (; naddr < maxaddrs; naddr++)
   1418 		sc->sc_config1 &= ~(SEEQ_CFG1_STATION_ADDR1 << naddr);
   1419 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, SEEQ_CONFIG1,
   1420 			  sc->sc_config1);
   1421 }
   1422 
   1423 /*
   1424  * Device timeout routine.
   1425  */
   1426 
   1427 static void
   1428 ea_watchdog(struct ifnet *ifp)
   1429 {
   1430 	struct seeq8005_softc *sc = ifp->if_softc;
   1431 
   1432 	log(LOG_ERR, "%s: lost Tx interrupt (status = 0x%04x)\n",
   1433 	    sc->sc_dev.dv_xname,
   1434 	    bus_space_read_2(sc->sc_iot, sc->sc_ioh, SEEQ_STATUS));
   1435 	ifp->if_oerrors++;
   1436 
   1437 	/* Kick the interface */
   1438 
   1439 	ea_init(ifp);
   1440 
   1441 	ifp->if_timer = 0;
   1442 }
   1443 
   1444 /* End of if_ea.c */
   1445