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