Home | History | Annotate | Line # | Download | only in dev
if_ae.c revision 1.58
      1 /*	$NetBSD: if_ae.c,v 1.58 1997/03/15 18:09:56 is Exp $	*/
      2 
      3 /*
      4  * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
      5  * adapters.
      6  *
      7  * Copyright (c) 1994, 1995 Charles M. Hannum.  All rights reserved.
      8  *
      9  * Copyright (C) 1993, David Greenman.  This software may be used, modified,
     10  * copied, distributed, and sold, in both source and binary form provided that
     11  * the above copyright and these terms are retained.  Under no circumstances is
     12  * the author responsible for the proper functioning of this software, nor does
     13  * the author assume any responsibility for damages incurred with its use.
     14  *
     15  * Adapted for MacBSD by Brad Parker <brad (at) fcr.com>.
     16  *
     17  * Currently supports:
     18  *	Apples NB Ethernet card
     19  *	Interlan A310 Nubus Ethernet card
     20  *	Cayman Systems GatorCard
     21  *	Asante MacCon II/E
     22  */
     23 
     24 #include "bpfilter.h"
     25 
     26 #include <sys/param.h>
     27 #include <sys/systm.h>
     28 #include <sys/errno.h>
     29 #include <sys/ioctl.h>
     30 #include <sys/mbuf.h>
     31 #include <sys/socket.h>
     32 #include <sys/syslog.h>
     33 #include <sys/device.h>
     34 
     35 #include <net/if.h>
     36 #include <net/if_dl.h>
     37 #include <net/if_types.h>
     38 #include <net/if_ether.h>
     39 
     40 #ifdef INET
     41 #include <netinet/in.h>
     42 #include <netinet/in_systm.h>
     43 #include <netinet/in_var.h>
     44 #include <netinet/ip.h>
     45 #include <netinet/if_inarp.h>
     46 #endif
     47 
     48 #ifdef NS
     49 #include <netns/ns.h>
     50 #include <netns/ns_if.h>
     51 #endif
     52 
     53 #if NBPFILTER > 0
     54 #include <net/bpf.h>
     55 #include <net/bpfdesc.h>
     56 #endif
     57 
     58 #include <machine/bus.h>
     59 #include <machine/viareg.h>
     60 
     61 #include <dev/ic/dp8390reg.h>
     62 #include "if_aereg.h"
     63 #include "if_aevar.h"
     64 
     65 #define inline	/* XXX for debugging porpoises */
     66 
     67 static inline void ae_rint __P((struct ae_softc *));
     68 static inline void ae_xmit __P((struct ae_softc *));
     69 static inline int ae_ring_copy __P((struct ae_softc *, int, caddr_t, int));
     70 
     71 #define	ETHER_MIN_LEN	64
     72 #define ETHER_MAX_LEN	1518
     73 #define	ETHER_ADDR_LEN	6
     74 
     75 #define REG_MAP(sc, reg)	((sc)->regs_rev ? (0x0f-(reg))<<2 : (reg)<<2)
     76 #define NIC_GET(sc, reg)	(bus_space_read_1((sc)->sc_regt,	\
     77 				    (sc)->sc_regh,			\
     78 				    (REG_MAP(sc, reg))))
     79 #define NIC_PUT(sc, reg, val)	(bus_space_write_1((sc)->sc_regt,	\
     80 				    (sc)->sc_regh,			\
     81 				    (REG_MAP(sc, reg)), (val)))
     82 
     83 struct cfdriver ae_cd = {
     84 	NULL, "ae", DV_IFNET
     85 };
     86 
     87 int
     88 ae_size_card_memory(bst, bsh, ofs)
     89 	bus_space_tag_t bst;
     90 	bus_space_handle_t bsh;
     91 	int ofs;
     92 {
     93 	int i1, i2, i3, i4;
     94 
     95 	/*
     96 	 * banks; also assume it will generally mirror in upper banks
     97 	 * if not installed.
     98 	 */
     99 	i1 = (8192 * 0);
    100 	i2 = (8192 * 1);
    101 	i3 = (8192 * 2);
    102 	i4 = (8192 * 3);
    103 
    104 	bus_space_write_2(bst, bsh, ofs + i1, 0x1111);
    105 	bus_space_write_2(bst, bsh, ofs + i2, 0x2222);
    106 	bus_space_write_2(bst, bsh, ofs + i3, 0x3333);
    107 	bus_space_write_2(bst, bsh, ofs + i4, 0x4444);
    108 
    109 	if (bus_space_read_2(bst, bsh, ofs + i1) == 0x1111 &&
    110 	    bus_space_read_2(bst, bsh, ofs + i2) == 0x2222 &&
    111 	    bus_space_read_2(bst, bsh, ofs + i3) == 0x3333 &&
    112 	    bus_space_read_2(bst, bsh, ofs + i4) == 0x4444)
    113 		return 8192 * 4;
    114 
    115 	if ((bus_space_read_2(bst, bsh, ofs + i1) == 0x1111 &&
    116 	    bus_space_read_2(bst, bsh, ofs + i2) == 0x2222) ||
    117 	    (bus_space_read_2(bst, bsh, ofs + i1) == 0x3333 &&
    118 	    bus_space_read_2(bst, bsh, ofs + i2) == 0x4444))
    119 		return 8192 * 2;
    120 
    121 	if (bus_space_read_2(bst, bsh, ofs + i1) == 0x1111 ||
    122 	    bus_space_read_2(bst, bsh, ofs + i1) == 0x4444)
    123 		return 8192;
    124 
    125 	return 0;
    126 }
    127 
    128 /*
    129  * Do bus-independent setup.
    130  */
    131 int
    132 aesetup(sc, lladdr)
    133 	struct ae_softc *sc;
    134 	u_int8_t *lladdr;
    135 {
    136 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    137 	int i;
    138 
    139 	sc->cr_proto = ED_CR_RD2;
    140 
    141 	/* Allocate one xmit buffer if < 16k, two buffers otherwise. */
    142 	if ((sc->mem_size < 16384) ||
    143 	    (sc->sc_flags & AE_FLAGS_NO_DOUBLE_BUFFERING))
    144 		sc->txb_cnt = 1;
    145 	else
    146 		sc->txb_cnt = 2;
    147 
    148 	sc->tx_page_start = 0;
    149 	sc->rec_page_start = sc->tx_page_start + sc->txb_cnt * ED_TXBUF_SIZE;
    150 	sc->rec_page_stop = sc->tx_page_start + (sc->mem_size >> ED_PAGE_SHIFT);
    151 	sc->mem_ring = sc->rec_page_start << ED_PAGE_SHIFT;
    152 
    153 	/* Now zero memory and verify that it is clear. */
    154 	bus_space_set_region_2(sc->sc_buft, sc->sc_bufh,
    155 	    0, 0, sc->mem_size / 2);
    156 
    157 	for (i = 0; i < sc->mem_size; ++i) {
    158 		if (bus_space_read_1(sc->sc_buft, sc->sc_bufh, i)) {
    159 printf(": failed to clear shared memory - check configuration\n");
    160 			return 1;
    161 		}
    162 	}
    163 
    164 	/* Set interface to stopped condition (reset). */
    165 	aestop(sc);
    166 
    167 	/* Initialize ifnet structure. */
    168 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    169 	ifp->if_softc = sc;
    170 	ifp->if_start = aestart;
    171 	ifp->if_ioctl = aeioctl;
    172 	if (!ifp->if_watchdog)
    173 		ifp->if_watchdog = aewatchdog;
    174 	ifp->if_flags =
    175 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    176 
    177 	/* Attach the interface. */
    178 	if_attach(ifp);
    179 	ether_ifattach(ifp, lladdr);
    180 
    181 	/* Print additional info when attached. */
    182 	printf(": address %s, ", ether_sprintf(lladdr));
    183 
    184 	printf("type %s, %dKB memory\n", sc->type_str, sc->mem_size / 1024);
    185 
    186 #if NBPFILTER > 0
    187 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    188 #endif
    189 
    190 	return 0;
    191 }
    192 
    193 /*
    194  * Reset interface.
    195  */
    196 void
    197 aereset(sc)
    198 	struct ae_softc *sc;
    199 {
    200 	int     s;
    201 
    202 	s = splnet();
    203 	aestop(sc);
    204 	aeinit(sc);
    205 	splx(s);
    206 }
    207 
    208 /*
    209  * Take interface offline.
    210  */
    211 void
    212 aestop(sc)
    213 	struct ae_softc *sc;
    214 {
    215 	int     n = 5000;
    216 
    217 	/* Stop everything on the interface, and select page 0 registers. */
    218 	NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
    219 
    220 	/*
    221 	 * Wait for interface to enter stopped state, but limit # of checks to
    222 	 * 'n' (about 5ms).  It shouldn't even take 5us on modern DS8390's, but
    223 	 * just in case it's an old one.
    224 	 */
    225 	while (((NIC_GET(sc, ED_P0_ISR) & ED_ISR_RST) == 0) && --n);
    226 }
    227 
    228 /*
    229  * Device timeout/watchdog routine.  Entered if the device neglects to generate
    230  * an interrupt after a transmit has been started on it.
    231  */
    232 
    233 void
    234 aewatchdog(ifp)
    235 	struct ifnet *ifp;
    236 {
    237 	struct ae_softc *sc = ifp->if_softc;
    238 
    239 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
    240 	++sc->sc_arpcom.ac_if.if_oerrors;
    241 
    242 	aereset(sc);
    243 }
    244 
    245 /*
    246  * Initialize device.
    247  */
    248 void
    249 aeinit(sc)
    250 	struct ae_softc *sc;
    251 {
    252 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    253 	int     i;
    254 	u_char  mcaf[8];
    255 
    256 	/*
    257 	 * Initialize the NIC in the exact order outlined in the NS manual.
    258 	 * This init procedure is "mandatory"...don't change what or when
    259 	 * things happen.
    260 	 */
    261 
    262 	/* Reset transmitter flags. */
    263 	ifp->if_timer = 0;
    264 
    265 	sc->txb_inuse = 0;
    266 	sc->txb_new = 0;
    267 	sc->txb_next_tx = 0;
    268 
    269 	/* Set interface for page 0, remote DMA complete, stopped. */
    270 	NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
    271 
    272 	if (sc->use16bit) {
    273 		/*
    274 		 * Set FIFO threshold to 8, No auto-init Remote DMA, byte
    275 		 * order=80x86, word-wide DMA xfers,
    276 		 */
    277 		NIC_PUT(sc, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_WTS | ED_DCR_LS);
    278 	} else {
    279 		/* Same as above, but byte-wide DMA xfers. */
    280 		NIC_PUT(sc, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS);
    281 	}
    282 
    283 	/* Clear remote byte count registers. */
    284 	NIC_PUT(sc, ED_P0_RBCR0, 0);
    285 	NIC_PUT(sc, ED_P0_RBCR1, 0);
    286 
    287 	/* Tell RCR to do nothing for now. */
    288 	NIC_PUT(sc, ED_P0_RCR, ED_RCR_MON);
    289 
    290 	/* Place NIC in internal loopback mode. */
    291 	NIC_PUT(sc, ED_P0_TCR, ED_TCR_LB0);
    292 
    293 	/* Initialize receive buffer ring. */
    294 	NIC_PUT(sc, ED_P0_TPSR, sc->rec_page_start);
    295 	NIC_PUT(sc, ED_P0_PSTART, sc->rec_page_start);
    296 
    297 	NIC_PUT(sc, ED_P0_PSTOP, sc->rec_page_stop);
    298 	NIC_PUT(sc, ED_P0_BNRY, sc->rec_page_start);
    299 
    300 	/*
    301 	 * Clear all interrupts.  A '1' in each bit position clears the
    302 	 * corresponding flag.
    303 	 */
    304 	NIC_PUT(sc, ED_P0_ISR, 0xff);
    305 
    306 	/*
    307 	 * Enable the following interrupts: receive/transmit complete,
    308 	 * receive/transmit error, and Receiver OverWrite.
    309 	 *
    310 	 * Counter overflow and Remote DMA complete are *not* enabled.
    311 	 */
    312 	NIC_PUT(sc, ED_P0_IMR,
    313 	    ED_IMR_PRXE | ED_IMR_PTXE | ED_IMR_RXEE | ED_IMR_TXEE |
    314 	    ED_IMR_OVWE);
    315 
    316 	/* Program command register for page 1. */
    317 	NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STP);
    318 
    319 	/* Copy out our station address. */
    320 	for (i = 0; i < ETHER_ADDR_LEN; ++i)
    321 		NIC_PUT(sc, ED_P1_PAR0 + i, sc->sc_arpcom.ac_enaddr[i]);
    322 
    323 	/* Set multicast filter on chip. */
    324 	ae_getmcaf(&sc->sc_arpcom, mcaf);
    325 	for (i = 0; i < 8; i++)
    326 		NIC_PUT(sc, ED_P1_MAR0 + i, mcaf[i]);
    327 
    328 	/*
    329 	 * Set current page pointer to one page after the boundary pointer, as
    330 	 * recommended in the National manual.
    331 	 */
    332 	sc->next_packet = sc->rec_page_start + 1;
    333 	NIC_PUT(sc, ED_P1_CURR, sc->next_packet);
    334 
    335 	/* Program command register for page 0. */
    336 	NIC_PUT(sc, ED_P1_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
    337 
    338 	i = ED_RCR_AB | ED_RCR_AM;
    339 	if (ifp->if_flags & IFF_PROMISC) {
    340 		/*
    341 		 * Set promiscuous mode.  Multicast filter was set earlier so
    342 		 * that we should receive all multicast packets.
    343 		 */
    344 		i |= ED_RCR_PRO | ED_RCR_AR | ED_RCR_SEP;
    345 	}
    346 	NIC_PUT(sc, ED_P0_RCR, i);
    347 
    348 	/* Take interface out of loopback. */
    349 	NIC_PUT(sc, ED_P0_TCR, 0);
    350 
    351 	/* Fire up the interface. */
    352 	NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
    353 
    354 	/* Set 'running' flag, and clear output active flag. */
    355 	ifp->if_flags |= IFF_RUNNING;
    356 	ifp->if_flags &= ~IFF_OACTIVE;
    357 
    358 	/* ...and attempt to start output. */
    359 	aestart(ifp);
    360 }
    361 
    362 /*
    363  * This routine actually starts the transmission on the interface.
    364  */
    365 static inline void
    366 ae_xmit(sc)
    367 	struct ae_softc *sc;
    368 {
    369 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    370 	u_short len;
    371 
    372 	len = sc->txb_len[sc->txb_next_tx];
    373 
    374 	/* Set NIC for page 0 register access. */
    375 	NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
    376 
    377 	/* Set TX buffer start page. */
    378 	NIC_PUT(sc, ED_P0_TPSR, sc->tx_page_start +
    379 	    sc->txb_next_tx * ED_TXBUF_SIZE);
    380 
    381 	/* Set TX length. */
    382 	NIC_PUT(sc, ED_P0_TBCR0, len);
    383 	NIC_PUT(sc, ED_P0_TBCR1, len >> 8);
    384 
    385 	/* Set page 0, remote DMA complete, transmit packet, and *start*. */
    386 	NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_TXP | ED_CR_STA);
    387 
    388 	/* Point to next transmit buffer slot and wrap if necessary. */
    389 	sc->txb_next_tx++;
    390 	if (sc->txb_next_tx == sc->txb_cnt)
    391 		sc->txb_next_tx = 0;
    392 
    393 	/* Set a timer just in case we never hear from the board again. */
    394 	ifp->if_timer = 2;
    395 }
    396 
    397 /*
    398  * Start output on interface.
    399  * We make two assumptions here:
    400  *  1) that the current priority is set to splnet _before_ this code
    401  *     is called *and* is returned to the appropriate priority after
    402  *     return
    403  *  2) that the IFF_OACTIVE flag is checked before this code is called
    404  *     (i.e. that the output part of the interface is idle)
    405  */
    406 void
    407 aestart(ifp)
    408 	struct ifnet *ifp;
    409 {
    410 	struct ae_softc *sc = ifp->if_softc;
    411 	struct mbuf *m0;
    412 	int buffer;
    413 	int len;
    414 
    415 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    416 		return;
    417 
    418 outloop:
    419 	/* See if there is room to put another packet in the buffer. */
    420 	if (sc->txb_inuse == sc->txb_cnt) {
    421 		/* No room.  Indicate this to the outside world and exit. */
    422 		ifp->if_flags |= IFF_OACTIVE;
    423 		return;
    424 	}
    425 	IF_DEQUEUE(&ifp->if_snd, m0);
    426 	if (m0 == 0)
    427 		return;
    428 
    429 	/* We need to use m->m_pkthdr.len, so require the header */
    430 	if ((m0->m_flags & M_PKTHDR) == 0)
    431 		panic("aestart: no header mbuf");
    432 
    433 #if NBPFILTER > 0
    434 	/* Tap off here if there is a BPF listener. */
    435 	if (ifp->if_bpf)
    436 		bpf_mtap(ifp->if_bpf, m0);
    437 #endif
    438 
    439 	/* txb_new points to next open buffer slot. */
    440 	buffer = (sc->txb_new * ED_TXBUF_SIZE) << ED_PAGE_SHIFT;
    441 
    442 	len = ae_put(sc, m0, buffer);
    443 #if DIAGNOSTIC
    444 	if (len != m0->m_pkthdr.len)
    445 		printf("aestart: len %d != m0->m_pkthdr.len %d.\n",
    446 			len, m0->m_pkthdr.len);
    447 #endif
    448 	len = m0->m_pkthdr.len;
    449 
    450 	m_freem(m0);
    451 	sc->txb_len[sc->txb_new] = max(len, ETHER_MIN_LEN);
    452 
    453 	/* Start the first packet transmitting. */
    454 	if (sc->txb_inuse == 0)
    455 		ae_xmit(sc);
    456 
    457 	/* Point to next buffer slot and wrap if necessary. */
    458 	if (++sc->txb_new == sc->txb_cnt)
    459 		sc->txb_new = 0;
    460 
    461 	sc->txb_inuse++;
    462 
    463 	/* Loop back to the top to possibly buffer more packets. */
    464 	goto outloop;
    465 }
    466 
    467 /*
    468  * Ethernet interface receiver interrupt.
    469  */
    470 static inline void
    471 ae_rint(sc)
    472 	struct ae_softc *sc;
    473 {
    474 	u_char  boundary, current;
    475 	u_short len;
    476 	u_char  nlen;
    477 	u_int8_t *lenp;
    478 	struct ae_ring packet_hdr;
    479 	int packet_ptr;
    480 
    481 loop:
    482 	/* Set NIC to page 1 registers to get 'current' pointer. */
    483 	NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STA);
    484 
    485 	/*
    486 	 * 'sc->next_packet' is the logical beginning of the ring-buffer - i.e.
    487 	 * it points to where new data has been buffered.  The 'CURR' (current)
    488 	 * register points to the logical end of the ring-buffer - i.e. it
    489 	 * points to where additional new data will be added.  We loop here
    490 	 * until the logical beginning equals the logical end (or in other
    491 	 * words, until the ring-buffer is empty).
    492 	 */
    493 	current = NIC_GET(sc, ED_P1_CURR);
    494 	if (sc->next_packet == current)
    495 		return;
    496 
    497 	/* Set NIC to page 0 registers to update boundary register. */
    498 	NIC_PUT(sc, ED_P1_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
    499 
    500 	do {
    501 		/* Get pointer to this buffer's header structure. */
    502 		packet_ptr = sc->mem_ring +
    503 		    ((sc->next_packet - sc->rec_page_start) << ED_PAGE_SHIFT);
    504 
    505 		/*
    506 		 * The byte count includes a 4 byte header that was added by
    507 		 * the NIC.
    508 		 */
    509 		bus_space_read_region_1(sc->sc_buft, sc->sc_bufh,
    510 		    packet_ptr, &packet_hdr, sizeof(struct ae_ring));
    511 		lenp = (u_int8_t *)&packet_hdr.count; /* sigh. */
    512 		len = lenp[0] | (lenp[1] << 8);
    513 		packet_hdr.count = len;
    514 
    515 		/*
    516 		 * Try do deal with old, buggy chips that sometimes duplicate
    517 		 * the low byte of the length into the high byte.  We do this
    518 		 * by simply ignoring the high byte of the length and always
    519 		 * recalculating it.
    520 		 *
    521 		 * NOTE: sc->next_packet is pointing at the current packet.
    522 		 */
    523 		if (packet_hdr.next_packet >= sc->next_packet)
    524 			nlen = (packet_hdr.next_packet - sc->next_packet);
    525 		else
    526 			nlen = ((packet_hdr.next_packet - sc->rec_page_start) +
    527 			    (sc->rec_page_stop - sc->next_packet));
    528 		--nlen;
    529 		if ((len & ED_PAGE_MASK) + sizeof(packet_hdr) > ED_PAGE_SIZE)
    530 			--nlen;
    531 		len = (len & ED_PAGE_MASK) | (nlen << ED_PAGE_SHIFT);
    532 #ifdef DIAGNOSTIC
    533 		if (len != packet_hdr.count) {
    534 			printf("%s: length does not match next packet pointer\n",
    535 			    sc->sc_dev.dv_xname);
    536 			printf("%s: len %04x nlen %04x start %02x first %02x curr %02x next %02x stop %02x\n",
    537 			    sc->sc_dev.dv_xname, packet_hdr.count, len,
    538 			    sc->rec_page_start, sc->next_packet, current,
    539 			    packet_hdr.next_packet, sc->rec_page_stop);
    540 		}
    541 #endif
    542 
    543 		/*
    544 		 * Be fairly liberal about what we allow as a "reasonable"
    545 		 * length so that a [crufty] packet will make it to BPF (and
    546 		 * can thus be analyzed).  Note that all that is really
    547 		 * important is that we have a length that will fit into one
    548 		 * mbuf cluster or less; the upper layer protocols can then
    549 		 * figure out the length from their own length field(s).
    550 		 */
    551 		if (len <= MCLBYTES &&
    552 		    packet_hdr.next_packet >= sc->rec_page_start &&
    553 		    packet_hdr.next_packet < sc->rec_page_stop) {
    554 			/* Go get packet. */
    555 			aeread(sc, packet_ptr + sizeof(struct ae_ring),
    556 			    len - sizeof(struct ae_ring));
    557 			++sc->sc_arpcom.ac_if.if_ipackets;
    558 		} else {
    559 			/* Really BAD.  The ring pointers are corrupted. */
    560 			log(LOG_ERR,
    561 			    "%s: NIC memory corrupt - invalid packet length %d\n",
    562 			    sc->sc_dev.dv_xname, len);
    563 			++sc->sc_arpcom.ac_if.if_ierrors;
    564 			aereset(sc);
    565 			return;
    566 		}
    567 
    568 		/* Update next packet pointer. */
    569 		sc->next_packet = packet_hdr.next_packet;
    570 
    571 		/*
    572 		 * Update NIC boundary pointer - being careful to keep it one
    573 		 * buffer behind (as recommended by NS databook).
    574 		 */
    575 		boundary = sc->next_packet - 1;
    576 		if (boundary < sc->rec_page_start)
    577 			boundary = sc->rec_page_stop - 1;
    578 		NIC_PUT(sc, ED_P0_BNRY, boundary);
    579 	} while (sc->next_packet != current);
    580 
    581 	goto loop;
    582 }
    583 
    584 /* Ethernet interface interrupt processor. */
    585 void
    586 aeintr(arg, slot)
    587 	void *arg;
    588 	int slot;
    589 {
    590 	struct ae_softc *sc = (struct ae_softc *)arg;
    591 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    592 	u_char isr;
    593 
    594 	/* Set NIC to page 0 registers. */
    595 	NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
    596 
    597 	isr = NIC_GET(sc, ED_P0_ISR);
    598 	if (!isr)
    599 		return;
    600 
    601 	/* Loop until there are no more new interrupts. */
    602 	for (;;) {
    603 		/*
    604 		 * Reset all the bits that we are 'acknowledging' by writing a
    605 		 * '1' to each bit position that was set.
    606 		 * (Writing a '1' *clears* the bit.)
    607 		 */
    608 		NIC_PUT(sc, ED_P0_ISR, isr);
    609 
    610 		/*
    611 		 * Handle transmitter interrupts.  Handle these first because
    612 		 * the receiver will reset the board under some conditions.
    613 		 */
    614 		if (isr & (ED_ISR_PTX | ED_ISR_TXE)) {
    615 			u_char  collisions = NIC_GET(sc, ED_P0_NCR) & 0x0f;
    616 
    617 			/*
    618 			 * Check for transmit error.  If a TX completed with an
    619 			 * error, we end up throwing the packet away.  Really
    620 			 * the only error that is possible is excessive
    621 			 * collisions, and in this case it is best to allow the
    622 			 * automatic mechanisms of TCP to backoff the flow.  Of
    623 			 * course, with UDP we're screwed, but this is expected
    624 			 * when a network is heavily loaded.
    625 			 */
    626 			(void) NIC_GET(sc, ED_P0_TSR);
    627 			if (isr & ED_ISR_TXE) {
    628 				/*
    629 				 * Excessive collisions (16).
    630 				 */
    631 				if ((NIC_GET(sc, ED_P0_TSR) & ED_TSR_ABT)
    632 				    && (collisions == 0)) {
    633 					/*
    634 					 * When collisions total 16, the P0_NCR
    635 					 * will indicate 0, and the TSR_ABT is
    636 					 * set.
    637 					 */
    638 					collisions = 16;
    639 				}
    640 
    641 				/* Update output errors counter. */
    642 				++ifp->if_oerrors;
    643 			} else {
    644 				/*
    645 				 * Update total number of successfully
    646 				 * transmitted packets.
    647 				 */
    648 				++ifp->if_opackets;
    649 			}
    650 
    651 			/* Done with the buffer. */
    652 			sc->txb_inuse--;
    653 
    654 			/* Clear watchdog timer. */
    655 			ifp->if_timer = 0;
    656 			ifp->if_flags &= ~IFF_OACTIVE;
    657 
    658 			/*
    659 			 * Add in total number of collisions on last
    660 			 * transmission.
    661 			 */
    662 			ifp->if_collisions += collisions;
    663 
    664 			/*
    665 			 * Decrement buffer in-use count if not zero (can only
    666 			 * be zero if a transmitter interrupt occured while not
    667 			 * actually transmitting).
    668 			 * If data is ready to transmit, start it transmitting,
    669 			 * otherwise defer until after handling receiver.
    670 			 */
    671 			if (sc->txb_inuse > 0)
    672 				ae_xmit(sc);
    673 		}
    674 
    675 		/* Handle receiver interrupts. */
    676 		if (isr & (ED_ISR_PRX | ED_ISR_RXE | ED_ISR_OVW)) {
    677 			/*
    678 			 * Overwrite warning.  In order to make sure that a
    679 			 * lockup of the local DMA hasn't occurred, we reset
    680 			 * and re-init the NIC.  The NSC manual suggests only a
    681 			 * partial reset/re-init is necessary - but some chips
    682 			 * seem to want more.  The DMA lockup has been seen
    683 			 * only with early rev chips - Methinks this bug was
    684 			 * fixed in later revs.  -DG
    685 			 */
    686 			if (isr & ED_ISR_OVW) {
    687 				++ifp->if_ierrors;
    688 #ifdef DIAGNOSTIC
    689 				log(LOG_WARNING,
    690 				    "%s: warning - receiver ring buffer overrun\n",
    691 				    sc->sc_dev.dv_xname);
    692 #endif
    693 				/* Stop/reset/re-init NIC. */
    694 				aereset(sc);
    695 			} else {
    696 				/*
    697 				 * Receiver Error.  One or more of: CRC error,
    698 				 * frame alignment error FIFO overrun, or
    699 				 * missed packet.
    700 				 */
    701 				if (isr & ED_ISR_RXE) {
    702 					++ifp->if_ierrors;
    703 #ifdef AE_DEBUG
    704 					printf("%s: receive error %x\n",
    705 					    sc->sc_dev.dv_xname,
    706 					    NIC_GET(sc, ED_P0_RSR));
    707 #endif
    708 				}
    709 
    710 				/*
    711 				 * Go get the packet(s)
    712 				 * XXX - Doing this on an error is dubious
    713 				 * because there shouldn't be any data to get
    714 				 * (we've configured the interface to not
    715 				 * accept packets with errors).
    716 				 */
    717 				ae_rint(sc);
    718 			}
    719 		}
    720 
    721 		/*
    722 		 * If it looks like the transmitter can take more data, attempt
    723 		 * to start output on the interface.  This is done after
    724 		 * handling the receiver to give the receiver priority.
    725 		 */
    726 		aestart(ifp);
    727 
    728 		/*
    729 		 * Return NIC CR to standard state: page 0, remote DMA
    730 		 * complete, start (toggling the TXP bit off, even if was just
    731 		 * set in the transmit routine, is *okay* - it is 'edge'
    732 		 * triggered from low to high).
    733 		 */
    734 		NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
    735 
    736 		/*
    737 		 * If the Network Talley Counters overflow, read them to reset
    738 		 * them.  It appears that old 8390's won't clear the ISR flag
    739 		 * otherwise - resulting in an infinite loop.
    740 		 */
    741 		if (isr & ED_ISR_CNT) {
    742 			(void)NIC_GET(sc, ED_P0_CNTR0);
    743 			(void)NIC_GET(sc, ED_P0_CNTR1);
    744 			(void)NIC_GET(sc, ED_P0_CNTR2);
    745 		}
    746 
    747 		isr = NIC_GET(sc, ED_P0_ISR);
    748 		if (!isr)
    749 			return;
    750 	}
    751 }
    752 
    753 /*
    754  * Process an ioctl request.  This code needs some work - it looks pretty ugly.
    755  */
    756 int
    757 aeioctl(ifp, cmd, data)
    758 	register struct ifnet *ifp;
    759 	u_long cmd;
    760 	caddr_t data;
    761 {
    762 	struct ae_softc *sc = ifp->if_softc;
    763 	register struct ifaddr *ifa = (struct ifaddr *) data;
    764 	struct ifreq *ifr = (struct ifreq *) data;
    765 	int     s, error = 0;
    766 
    767 	s = splnet();
    768 
    769 	switch (cmd) {
    770 
    771 	case SIOCSIFADDR:
    772 		ifp->if_flags |= IFF_UP;
    773 
    774 		switch (ifa->ifa_addr->sa_family) {
    775 #ifdef INET
    776 		case AF_INET:
    777 			aeinit(sc);
    778 			arp_ifinit(&sc->sc_arpcom, ifa);
    779 			break;
    780 #endif
    781 #ifdef NS
    782 			/* XXX - This code is probably wrong. */
    783 		case AF_NS:
    784 			{
    785 				register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    786 
    787 				if (ns_nullhost(*ina))
    788 					ina->x_host =
    789 					    *(union ns_host *) (sc->sc_arpcom.ac_enaddr);
    790 				else
    791 					bcopy(ina->x_host.c_host,
    792 					    sc->sc_arpcom.ac_enaddr,
    793 					    sizeof(sc->sc_arpcom.ac_enaddr));
    794 				/* Set new address. */
    795 				aeinit(sc);
    796 				break;
    797 			}
    798 #endif
    799 		default:
    800 			aeinit(sc);
    801 			break;
    802 		}
    803 		break;
    804 
    805 	case SIOCSIFFLAGS:
    806 		if ((ifp->if_flags & IFF_UP) == 0 &&
    807 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    808 			/*
    809 			 * If interface is marked down and it is running, then
    810 			 * stop it.
    811 			 */
    812 			aestop(sc);
    813 			ifp->if_flags &= ~IFF_RUNNING;
    814 		} else
    815 			if ((ifp->if_flags & IFF_UP) != 0 &&
    816 			    (ifp->if_flags & IFF_RUNNING) == 0) {
    817 				/*
    818 				 * If interface is marked up and it is stopped, then
    819 				 * start it.
    820 				 */
    821 				aeinit(sc);
    822 			} else {
    823 				/*
    824 				 * Reset the interface to pick up changes in any other
    825 				 * flags that affect hardware registers.
    826 				 */
    827 				aestop(sc);
    828 				aeinit(sc);
    829 			}
    830 		break;
    831 
    832 	case SIOCADDMULTI:
    833 	case SIOCDELMULTI:
    834 		/* Update our multicast list. */
    835 		error = (cmd == SIOCADDMULTI) ?
    836 		    ether_addmulti(ifr, &sc->sc_arpcom) :
    837 		    ether_delmulti(ifr, &sc->sc_arpcom);
    838 
    839 		if (error == ENETRESET) {
    840 			/*
    841 			 * Multicast list has changed; set the hardware filter
    842 			 * accordingly.
    843 			 */
    844 			aestop(sc);	/* XXX for ds_setmcaf? */
    845 			aeinit(sc);
    846 			error = 0;
    847 		}
    848 		break;
    849 
    850 	default:
    851 		error = EINVAL;
    852 		break;
    853 	}
    854 
    855 	splx(s);
    856 	return (error);
    857 }
    858 
    859 /*
    860  * Retreive packet from shared memory and send to the next level up via
    861  * ether_input().  If there is a BPF listener, give a copy to BPF, too.
    862  */
    863 void
    864 aeread(sc, buf, len)
    865 	struct ae_softc *sc;
    866 	int buf;
    867 	int len;
    868 {
    869 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    870 	struct mbuf *m;
    871 	struct ether_header *eh;
    872 
    873 	/* Pull packet off interface. */
    874 	m = aeget(sc, buf, len);
    875 	if (m == 0) {
    876 		ifp->if_ierrors++;
    877 		return;
    878 	}
    879 
    880 	ifp->if_ipackets++;
    881 
    882 	/* We assume that the header fits entirely in one mbuf. */
    883 	eh = mtod(m, struct ether_header *);
    884 
    885 #if NBPFILTER > 0
    886 	/*
    887 	 * Check if there's a BPF listener on this interface.
    888 	 * If so, hand off the raw packet to bpf.
    889 	 */
    890 	if (ifp->if_bpf) {
    891 		bpf_mtap(ifp->if_bpf, m);
    892 
    893 		/*
    894 		 * Note that the interface cannot be in promiscuous mode if
    895 		 * there are no BPF listeners.  And if we are in promiscuous
    896 		 * mode, we have to check if this packet is really ours.
    897 		 */
    898 		if ((ifp->if_flags & IFF_PROMISC) &&
    899 		    (eh->ether_dhost[0] & 1) == 0 &&	/* !mcast and !bcast */
    900 		    bcmp(eh->ether_dhost, sc->sc_arpcom.ac_enaddr,
    901 			sizeof(eh->ether_dhost)) != 0) {
    902 			m_freem(m);
    903 			return;
    904 		}
    905 	}
    906 #endif
    907 
    908 	/* Fix up data start offset in mbuf to point past ether header. */
    909 	m_adj(m, sizeof(struct ether_header));
    910 	ether_input(ifp, eh, m);
    911 }
    912 
    913 /*
    914  * Supporting routines.
    915  */
    916 /*
    917  * Given a source and destination address, copy 'amount' of a packet from the
    918  * ring buffer into a linear destination buffer.  Takes into account ring-wrap.
    919  */
    920 static inline int
    921 ae_ring_copy(sc, src, dst, amount)
    922 	struct ae_softc *sc;
    923 	int src;
    924 	caddr_t dst;
    925 	int amount;
    926 {
    927 	bus_space_tag_t bst = sc->sc_buft;
    928 	bus_space_handle_t bsh = sc->sc_bufh;
    929 	int tmp_amount;
    930 
    931 	/* Does copy wrap to lower addr in ring buffer? */
    932 	if (src + amount > sc->mem_size) {
    933 		tmp_amount = sc->mem_size - src;
    934 
    935 		/* Copy amount up to end of NIC memory. */
    936 		bus_space_read_region_1(bst, bsh, src, dst, tmp_amount);
    937 
    938 		amount -= tmp_amount;
    939 		src = sc->mem_ring;
    940 		dst += tmp_amount;
    941 	}
    942 	bus_space_read_region_1(bst, bsh, src, dst, amount);
    943 
    944 	return (src + amount);
    945 }
    946 
    947 /*
    948  * Copy data from receive buffer to end of mbuf chain allocate additional mbufs
    949  * as needed.  Return pointer to last mbuf in chain.
    950  * sc = ae info (softc)
    951  * src = pointer in ae ring buffer
    952  * dst = pointer to last mbuf in mbuf chain to copy to
    953  * amount = amount of data to copy
    954  */
    955 struct mbuf *
    956 aeget(sc, src, total_len)
    957 	struct ae_softc *sc;
    958 	int src;
    959 	u_short total_len;
    960 {
    961 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    962 	struct mbuf *top, **mp, *m;
    963 	int len;
    964 
    965 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    966 	if (m == 0)
    967 		return 0;
    968 	m->m_pkthdr.rcvif = ifp;
    969 	m->m_pkthdr.len = total_len;
    970 	len = MHLEN;
    971 	top = 0;
    972 	mp = &top;
    973 
    974 	while (total_len > 0) {
    975 		if (top) {
    976 			MGET(m, M_DONTWAIT, MT_DATA);
    977 			if (m == 0) {
    978 				m_freem(top);
    979 				return 0;
    980 			}
    981 			len = MLEN;
    982 		}
    983 		if (total_len >= MINCLSIZE) {
    984 			MCLGET(m, M_DONTWAIT);
    985 			if (m->m_flags & M_EXT)
    986 				len = MCLBYTES;
    987 		}
    988 		m->m_len = len = min(total_len, len);
    989 		src = ae_ring_copy(sc, src, mtod(m, caddr_t), len);
    990 		total_len -= len;
    991 		*mp = m;
    992 		mp = &m->m_next;
    993 	}
    994 
    995 	return top;
    996 }
    997 
    998 /*
    999  * Compute the multicast address filter from the list of multicast addresses we
   1000  * need to listen to.
   1001  */
   1002 void
   1003 ae_getmcaf(ac, af)
   1004 	struct arpcom *ac;
   1005 	u_char *af;
   1006 {
   1007 	struct ifnet *ifp = &ac->ac_if;
   1008 	struct ether_multi *enm;
   1009 	register u_char *cp, c;
   1010 	register u_long crc;
   1011 	register int i, len;
   1012 	struct ether_multistep step;
   1013 
   1014 	/*
   1015 	 * Set up multicast address filter by passing all multicast addresses
   1016 	 * through a crc generator, and then using the high order 6 bits as an
   1017 	 * index into the 64 bit logical address filter.  The high order bit
   1018 	 * selects the word, while the rest of the bits select the bit within
   1019 	 * the word.
   1020 	 */
   1021 
   1022 	if (ifp->if_flags & IFF_PROMISC) {
   1023 		ifp->if_flags |= IFF_ALLMULTI;
   1024 		for (i = 0; i < 8; i++)
   1025 			af[i] = 0xff;
   1026 		return;
   1027 	}
   1028 	for (i = 0; i < 8; i++)
   1029 		af[i] = 0;
   1030 	ETHER_FIRST_MULTI(step, ac, enm);
   1031 	while (enm != NULL) {
   1032 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
   1033 			sizeof(enm->enm_addrlo)) != 0) {
   1034 			/*
   1035 			 * We must listen to a range of multicast addresses.
   1036 			 * For now, just accept all multicasts, rather than
   1037 			 * trying to set only those filter bits needed to match
   1038 			 * the range.  (At this time, the only use of address
   1039 			 * ranges is for IP multicast routing, for which the
   1040 			 * range is big enough to require all bits set.)
   1041 			 */
   1042 			ifp->if_flags |= IFF_ALLMULTI;
   1043 			for (i = 0; i < 8; i++)
   1044 				af[i] = 0xff;
   1045 			return;
   1046 		}
   1047 		cp = enm->enm_addrlo;
   1048 		crc = 0xffffffff;
   1049 		for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
   1050 			c = *cp++;
   1051 			for (i = 8; --i >= 0;) {
   1052 				if (((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01)) {
   1053 					crc <<= 1;
   1054 					crc ^= 0x04c11db6 | 1;
   1055 				} else
   1056 					crc <<= 1;
   1057 				c >>= 1;
   1058 			}
   1059 		}
   1060 		/* Just want the 6 most significant bits. */
   1061 		crc >>= 26;
   1062 
   1063 		/* Turn on the corresponding bit in the filter. */
   1064 		af[crc >> 3] |= 1 << (crc & 0x7);
   1065 
   1066 		ETHER_NEXT_MULTI(step, enm);
   1067 	}
   1068 	ifp->if_flags &= ~IFF_ALLMULTI;
   1069 }
   1070 
   1071 /*
   1072  * Copy packet from mbuf to the board memory
   1073  *
   1074  * Currently uses an extra buffer/extra memory copy,
   1075  * unless the whole packet fits in one mbuf.
   1076  *
   1077  */
   1078 int
   1079 ae_put(sc, m, buf)
   1080 	struct ae_softc *sc;
   1081 	struct mbuf *m;
   1082 	int buf;
   1083 {
   1084 	u_char *data, savebyte[2];
   1085 	int len, wantbyte;
   1086 	u_short totlen = 0;
   1087 
   1088 	wantbyte = 0;
   1089 
   1090 	for (; m ; m = m->m_next) {
   1091 		data = mtod(m, u_char *);
   1092 		len = m->m_len;
   1093 		totlen += len;
   1094 		if (len > 0) {
   1095 			/* Finish the last word. */
   1096 			if (wantbyte) {
   1097 				savebyte[1] = *data;
   1098 				bus_space_write_region_2(sc->sc_buft,
   1099 				    sc->sc_bufh, buf, savebyte, 1);
   1100 				buf += 2;
   1101 				data++;
   1102 				len--;
   1103 				wantbyte = 0;
   1104 			}
   1105 			/* Output contiguous words. */
   1106 			if (len > 1) {
   1107 				bus_space_write_region_2(sc->sc_buft,
   1108 				    sc->sc_bufh, buf, data, len >> 1);
   1109 				buf += len & ~1;
   1110 				data += len & ~1;
   1111 				len &= 1;
   1112 			}
   1113 			/* Save last byte, if necessary. */
   1114 			if (len == 1) {
   1115 				savebyte[0] = *data;
   1116 				wantbyte = 1;
   1117 			}
   1118 		}
   1119 	}
   1120 
   1121 	if (wantbyte) {
   1122 		savebyte[1] = 0;
   1123 		bus_space_write_region_2(sc->sc_buft, sc->sc_bufh,
   1124 		    buf, savebyte, 1);
   1125 	}
   1126 	return (totlen);
   1127 }
   1128