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