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