Home | History | Annotate | Line # | Download | only in dev
if_ae.c revision 1.21
      1 /*	$NetBSD: if_ae.c,v 1.21 1995/04/12 15:01:14 briggs 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 "../mac68k/via.h"
     59 #include "nubus.h"
     60 #include "if_aereg.h"
     61 
     62 /*
     63  * ae_softc: per line info and status
     64  */
     65 struct	ae_softc {
     66 	struct	device sc_dev;
     67 /*	struct	nubusdev sc_nu;
     68 	struct	intrhand sc_ih;	*/
     69 
     70 	struct	arpcom sc_arpcom;	/* ethernet common */
     71 
     72 	char	*type_str;	/* pointer to type string */
     73 	u_char	vendor;		/* interface vendor */
     74 	u_char	type;		/* interface type code */
     75 	u_char	regs_rev;	/* registers are reversed */
     76 
     77 #define	REG_MAP(sc, reg)	((sc)->regs_rev ? (0x0f-(reg))<<2 : (reg)<<2)
     78 #define NIC_GET(sc, reg)	((sc)->nic_addr[REG_MAP(sc, reg)])
     79 #define NIC_PUT(sc, reg, val)	((sc)->nic_addr[REG_MAP(sc, reg)] = (val))
     80 	volatile caddr_t nic_addr; /* NIC (DS8390) I/O bus address */
     81 	caddr_t	rom_addr;	/* on board prom address */
     82 
     83 	u_char	cr_proto;	/* values always set in CR */
     84 
     85 	caddr_t	mem_start;	/* shared memory start address */
     86 	caddr_t	mem_end;	/* shared memory end address */
     87 	u_long	mem_size;	/* total shared memory size */
     88 	caddr_t	mem_ring;	/* start of RX ring-buffer (in smem) */
     89 
     90 	u_char	mem_wr_short;	/* card memory requires int16 writes */
     91 
     92 	u_char	xmit_busy;	/* transmitter is busy */
     93 	u_char	txb_cnt;	/* Number of transmit buffers */
     94 	u_char	txb_inuse;	/* number of TX buffers currently in-use*/
     95 
     96 	u_char 	txb_new;	/* pointer to where new buffer will be added */
     97 	u_char	txb_next_tx;	/* pointer to next buffer ready to xmit */
     98 	u_short	txb_len[8];	/* buffered xmit buffer lengths */
     99 	u_char	tx_page_start;	/* first page of TX buffer area */
    100 	u_char	rec_page_start;	/* first page of RX ring-buffer */
    101 	u_char	rec_page_stop;	/* last page of RX ring-buffer */
    102 	u_char	next_packet;	/* pointer to next unread RX packet */
    103 };
    104 
    105 int ae_probe __P((struct device *, void *, void *));
    106 void ae_attach __P((struct device *, struct device *, void *));
    107 int ae_intr __P((int));
    108 int ae_ioctl __P((struct ifnet *, u_long, caddr_t));
    109 void ae_start __P((struct ifnet *));
    110 void ae_watchdog __P((/* short */));
    111 void ae_reset __P((struct ae_softc *));
    112 void ae_init __P((struct ae_softc *));
    113 void ae_stop __P((struct ae_softc *));
    114 void ae_getmcaf __P((struct arpcom *, u_long *));
    115 u_short ae_put __P((struct ae_softc *, struct mbuf *, caddr_t));
    116 
    117 #define inline	/* XXX for debugging porpoises */
    118 
    119 void ae_get_packet __P((struct ae_softc *, caddr_t, u_short));
    120 static inline void ae_rint __P((struct ae_softc *));
    121 static inline void ae_xmit __P((struct ae_softc *));
    122 static inline caddr_t ae_ring_copy __P((/* struct ae_softc *, caddr_t, caddr_t,
    123 					u_short */));
    124 
    125 struct cfdriver aecd = {
    126 	NULL, "ae", ae_probe, ae_attach, DV_IFNET, sizeof(struct ae_softc)
    127 };
    128 
    129 #define	ETHER_MIN_LEN	64
    130 #define ETHER_MAX_LEN	1518
    131 #define	ETHER_ADDR_LEN	6
    132 
    133 char ae_name[] = "8390 Nubus Ethernet card";
    134 static char zero = 0;
    135 static u_char ones = 0xff;
    136 
    137 struct vendor_S {
    138 	char	*manu;
    139 	int	len;
    140 	int	vendor;
    141 } vend[] = {
    142 	{ "Apple", 5, AE_VENDOR_APPLE },
    143 	{ "3Com",  4, AE_VENDOR_APPLE },
    144 	{ "Dayna", 5, AE_VENDOR_DAYNA },
    145 	{ "Inter", 5, AE_VENDOR_INTERLAN },
    146 	{ "Asant", 5, AE_VENDOR_ASANTE },
    147 };
    148 
    149 static int numvend = sizeof(vend)/sizeof(vend[0]);
    150 
    151 /*
    152  * XXX These two should be moved to locore, and maybe changed to use shorts
    153  * instead of bytes.  The reason for these is that bcopy and bzero use longs,
    154  * which the ethernet cards can't handle.
    155  */
    156 
    157 void
    158 bszero(u_short *addr, int len)
    159 {
    160 
    161 	while (len--)
    162 		*addr++ = 0;
    163 }
    164 
    165 /*
    166  * Memory copy, copies word at time.
    167  */
    168 #if 1
    169 static inline void
    170 word_copy(a, b, len)
    171 	caddr_t a, b;
    172 	int len;
    173 {
    174 	u_short *x = (u_short *)a,
    175 		*y = (u_short *)b;
    176 
    177 	len >>= 1;
    178 	while (len--)
    179 		*y++ = *x++;
    180 }
    181 #else
    182 static inline void
    183 word_copy(src, dest, len)
    184 	caddr_t src, dest;
    185 	int len;
    186 {
    187 	u_short *d = (u_short *)dest;
    188 	u_short *s = (u_short *)src;
    189 	char b1, b2;
    190 
    191 	/* odd case, src addr is unaligned */
    192 	if ( ((u_long)src) & 1 ) {
    193 		while (len > 0) {
    194 			b1 = *src++;
    195 			b2 = len > 1 ? *src++ : (*d & 0xff);
    196 			*d++ = (b1 << 8) | b2;
    197 			len -= 2;
    198 		}
    199 		return;
    200 	}
    201 
    202 	/* normal case, aligned src & dst */
    203 	while (len > 0) {
    204 		*d++ = *s++;
    205 		len -= 2;
    206 	}
    207 }
    208 #endif
    209 
    210 void
    211 ae_id_card(nu, sc)
    212 	struct nubus_hw	*nu;
    213 	struct ae_softc	*sc;
    214 {
    215 	int i;
    216 
    217 	/*
    218 	 * Try to determine what type of card this is...
    219 	 */
    220 	sc->vendor = AE_VENDOR_UNKNOWN;
    221 	for (i = 0; i < numvend; i++) {
    222 		if (!strncmp(nu->Slot.manufacturer, vend[i].manu, vend[i].len)) {
    223 			sc->vendor = vend[i].vendor;
    224 			break;
    225 		}
    226 	}
    227 	sc->type_str = (char *)(nu->Slot.manufacturer);
    228 
    229 }
    230 
    231 int
    232 ae_size_card_memory(sc)
    233 	struct ae_softc	*sc;
    234 {
    235 	u_short *p;
    236 	u_short i1, i2, i3, i4;
    237 	int size;
    238 
    239 	p = (u_short *)sc->mem_start;
    240 
    241 	/*
    242 	 * very simple size memory, assuming it's installed in 8k
    243 	 * banks; also assume it will generally mirror in upper banks
    244 	 * if not installed.
    245 	 */
    246 	i1 = (8192*0)/2;
    247 	i2 = (8192*1)/2;
    248 	i3 = (8192*2)/2;
    249 	i4 = (8192*3)/2;
    250 
    251 	p[i1] = 0x1111;
    252 	p[i2] = 0x2222;
    253 	p[i3] = 0x3333;
    254 	p[i4] = 0x4444;
    255 
    256 	if (p[i1] == 0x1111 && p[i2] == 0x2222 &&
    257 	    p[i3] == 0x3333 && p[i4] == 0x4444)
    258 		return 8192*4;
    259 
    260 	if ((p[i1] == 0x1111 && p[i2] == 0x2222) ||
    261 	    (p[i1] == 0x3333 && p[i2] == 0x4444))
    262 		return 8192*2;
    263 
    264 	if (p[i1] == 0x1111 || p[i1] == 0x4444)
    265 		return 8192;
    266 
    267 	return 0;
    268 }
    269 
    270 int
    271 ae_probe(parent, match, aux)
    272 	struct device *parent;
    273 	void *match, *aux;
    274 {
    275 	struct ae_softc *sc = match;
    276 	register struct nubus_hw *nu = aux;
    277 	int i, memsize;
    278 	int flags = 0;
    279 
    280 	if (nu->Slot.type != NUBUS_NETWORK)
    281 		return 0;
    282 
    283 	ae_id_card(nu, sc);
    284 
    285 	sc->regs_rev = 0;
    286 	sc->mem_wr_short = 0;
    287 
    288 	switch (sc->vendor) {
    289 	      case AE_VENDOR_INTERLAN:
    290 		sc->nic_addr = nu->addr + GC_NIC_OFFSET;
    291 		sc->rom_addr = nu->addr + GC_ROM_OFFSET;
    292 		sc->mem_start = nu->addr + GC_DATA_OFFSET;
    293 		if ((memsize = ae_size_card_memory(sc)) == 0)
    294 			return 0;
    295 
    296 		/* reset the NIC chip */
    297 		*((caddr_t)nu->addr + GC_RESET_OFFSET) = (char)zero;
    298 
    299 		/* Get station address from on-board ROM */
    300 		for (i = 0; i < ETHER_ADDR_LEN; ++i)
    301 			sc->sc_arpcom.ac_enaddr[i] = *(sc->rom_addr + i*4);
    302 		break;
    303 
    304 	      case AE_VENDOR_ASANTE:
    305 		/* memory writes require *(u_short *) */
    306 		sc->mem_wr_short = 1;
    307 		/* otherwise, pretend to be an apple card (fall through) */
    308 
    309 	      case AE_VENDOR_APPLE:
    310 		sc->regs_rev = 1;
    311 		sc->nic_addr = nu->addr + AE_NIC_OFFSET;
    312 		sc->rom_addr = nu->addr + AE_ROM_OFFSET;
    313 		sc->mem_start = nu->addr + AE_DATA_OFFSET;
    314 		if ((memsize = ae_size_card_memory(sc)) == 0)
    315 			return (0);
    316 
    317 		/* Get station address from on-board ROM */
    318 		for (i = 0; i < ETHER_ADDR_LEN; ++i)
    319 			sc->sc_arpcom.ac_enaddr[i] = *(sc->rom_addr + i*2);
    320 		break;
    321 
    322 	      case AE_VENDOR_DAYNA:
    323 		printf("We think we are a Dayna card, but ");
    324 		sc->nic_addr = nu->addr + DP_NIC_OFFSET;
    325 		sc->rom_addr = nu->addr + DP_ROM_OFFSET;
    326 		sc->mem_start = nu->addr + DP_DATA_OFFSET;
    327 		memsize = 8192;
    328 
    329 		/* Get station address from on-board ROM */
    330 		for (i = 0; i < ETHER_ADDR_LEN; ++i)
    331 			sc->sc_arpcom.ac_enaddr[i] = *(sc->rom_addr + i*2);
    332 		printf("it is dangerous to continue.\n");
    333 		return (0); /* Since we don't work yet... */
    334 		break;
    335 
    336 	      default:
    337 		return (0);
    338 		break;
    339 	}
    340 
    341 	sc->cr_proto = AE_CR_RD2;
    342 
    343 	/* Allocate one xmit buffer if < 16k, two buffers otherwise. */
    344 	if ((memsize < 16384) || (flags & AE_FLAGS_NO_DOUBLE_BUFFERING))
    345 		sc->txb_cnt = 1;
    346 	else
    347 		sc->txb_cnt = 2;
    348 
    349 	sc->tx_page_start = 0;
    350 	sc->rec_page_start = sc->tx_page_start + sc->txb_cnt * AE_TXBUF_SIZE;
    351 	sc->rec_page_stop = sc->tx_page_start + (memsize >> AE_PAGE_SHIFT);
    352 	sc->mem_ring = sc->mem_start + (sc->rec_page_start << AE_PAGE_SHIFT);
    353 	sc->mem_size = memsize;
    354 	sc->mem_end = sc->mem_start + memsize;
    355 
    356 	/* Now zero memory and verify that it is clear. */
    357 	bszero((u_short *)sc->mem_start, memsize / 2);
    358 
    359 	for (i = 0; i < memsize; ++i)
    360 		if (sc->mem_start[i]) {
    361 	        	printf("%s: failed to clear shared memory at %x - check configuration\n",
    362 			    sc->sc_dev.dv_xname,
    363 			    sc->mem_start + i);
    364 			return (0);
    365 		}
    366 
    367 	return (1);
    368 }
    369 
    370 /*
    371  * Install interface into kernel networking data structures
    372  */
    373 void
    374 ae_attach(parent, self, aux)
    375 	struct device	*parent, *self;
    376 	void *aux;
    377 {
    378 	struct ae_softc *sc = (void *)self;
    379 	struct nubus_hw	*nu = aux;
    380 	struct cfdata *cf = sc->sc_dev.dv_cfdata;
    381 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    382 
    383 	/* Set interface to stopped condition (reset). */
    384 	ae_stop(sc);
    385 
    386 	/* Initialize ifnet structure. */
    387 	ifp->if_unit = sc->sc_dev.dv_unit;
    388 	ifp->if_name = aecd.cd_name;
    389 	ifp->if_start = ae_start;
    390 	ifp->if_ioctl = ae_ioctl;
    391 	ifp->if_watchdog = ae_watchdog;
    392 	ifp->if_flags =
    393 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    394 
    395 	/* Attach the interface. */
    396 	if_attach(ifp);
    397 	ether_ifattach(ifp);
    398 
    399 	/* Print additional info when attached. */
    400 	printf(": address %s, ", ether_sprintf(sc->sc_arpcom.ac_enaddr));
    401 
    402 	if (sc->type_str && (*sc->type_str != 0))
    403 		printf("type %s", sc->type_str);
    404 	else
    405 		printf("type unknown (0x%x)", sc->type);
    406 
    407 	printf(", %dk mem.\n", sc->mem_size / 1024);
    408 
    409 #if NBPFILTER > 0
    410 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    411 #endif
    412 
    413 	/* make sure interrupts are vectored to us */
    414 	add_nubus_intr((int)sc->rom_addr & 0xFF000000, ae_intr, ifp->if_unit);
    415 }
    416 
    417 /*
    418  * Reset interface.
    419  */
    420 void
    421 ae_reset(sc)
    422 	struct ae_softc *sc;
    423 {
    424 	int s;
    425 
    426 	s = splimp();
    427 	ae_stop(sc);
    428 	ae_init(sc);
    429 	splx(s);
    430 }
    431 
    432 /*
    433  * Take interface offline.
    434  */
    435 void
    436 ae_stop(sc)
    437 	struct ae_softc *sc;
    438 {
    439 	int n = 5000;
    440 
    441 	/* Stop everything on the interface, and select page 0 registers. */
    442 	NIC_PUT(sc, AE_P0_CR, sc->cr_proto | AE_CR_PAGE_0 | AE_CR_STP);
    443 
    444 	/*
    445 	 * Wait for interface to enter stopped state, but limit # of checks to
    446 	 * 'n' (about 5ms).  It shouldn't even take 5us on modern DS8390's, but
    447 	 * just in case it's an old one.
    448 	 */
    449 	while (((NIC_GET(sc, AE_P0_ISR) & AE_ISR_RST) == 0) && --n);
    450 }
    451 
    452 /*
    453  * Device timeout/watchdog routine.  Entered if the device neglects to generate
    454  * an interrupt after a transmit has been started on it.
    455  */
    456 static	int aeintr_ctr = 0;
    457 void
    458 ae_watchdog(unit)
    459 	int unit;
    460 {
    461 	struct ae_softc *sc = aecd.cd_devs[unit];
    462 
    463 #if 1
    464 /*
    465  * This is a kludge!  The via code seems to miss slot interrupts
    466  * sometimes.  This kludges around that by calling the handler
    467  * by hand if the watchdog is activated. -- XXX (akb)
    468  */
    469 	int	i;
    470 
    471 	i = aeintr_ctr;
    472 
    473 	(*via2itab[1])(1);
    474 
    475 	if (i != aeintr_ctr) {
    476 		log(LOG_ERR, "ae%d: device timeout, recovered\n", unit);
    477 		return;
    478 	}
    479 #endif
    480 
    481 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
    482 	++sc->sc_arpcom.ac_if.if_oerrors;
    483 
    484 	ae_reset(sc);
    485 }
    486 
    487 /*
    488  * Initialize device.
    489  */
    490 void
    491 ae_init(sc)
    492 	struct ae_softc *sc;
    493 {
    494 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    495 	int i, s;
    496 	u_char command;
    497 	u_long mcaf[2];
    498 
    499 	/* Address not known. */
    500 	if (ifp->if_addrlist == 0)
    501 		return;
    502 
    503 	/*
    504 	 * Initialize the NIC in the exact order outlined in the NS manual.
    505 	 * This init procedure is "mandatory"...don't change what or when
    506 	 * things happen.
    507 	 */
    508 	s = splimp();
    509 
    510 	/* Reset transmitter flags. */
    511 	sc->xmit_busy = 0;
    512 	sc->sc_arpcom.ac_if.if_timer = 0;
    513 
    514 	sc->txb_inuse = 0;
    515 	sc->txb_new = 0;
    516 	sc->txb_next_tx = 0;
    517 
    518 	/* Set interface for page 0, remote DMA complete, stopped. */
    519 	NIC_PUT(sc, AE_P0_CR, sc->cr_proto | AE_CR_PAGE_0 | AE_CR_STP);
    520 
    521 	/*
    522 	 * Set FIFO threshold to 8, No auto-init Remote DMA, byte
    523 	 * order=80x86, word-wide DMA xfers,
    524 	 */
    525 	NIC_PUT(sc, AE_P0_DCR,
    526 	    AE_DCR_FT1 | AE_DCR_WTS | AE_DCR_LS);
    527 
    528 	/* Clear remote byte count registers. */
    529 	NIC_PUT(sc, AE_P0_RBCR0, 0);
    530 	NIC_PUT(sc, AE_P0_RBCR1, 0);
    531 
    532 	/* Tell RCR to do nothing for now. */
    533 	NIC_PUT(sc, AE_P0_RCR, AE_RCR_MON);
    534 
    535 	/* Place NIC in internal loopback mode. */
    536 	NIC_PUT(sc, AE_P0_TCR, AE_TCR_LB0);
    537 
    538 	/* Initialize receive buffer ring. */
    539 	NIC_PUT(sc, AE_P0_BNRY, sc->rec_page_start);
    540 	NIC_PUT(sc, AE_P0_PSTART, sc->rec_page_start);
    541 	NIC_PUT(sc, AE_P0_PSTOP, sc->rec_page_stop);
    542 
    543 	/*
    544 	 * Clear all interrupts.  A '1' in each bit position clears the
    545 	 * corresponding flag.
    546 	 */
    547 	NIC_PUT(sc, AE_P0_ISR, 0xff);
    548 
    549 	/*
    550 	 * Enable the following interrupts: receive/transmit complete,
    551 	 * receive/transmit error, and Receiver OverWrite.
    552 	 *
    553 	 * Counter overflow and Remote DMA complete are *not* enabled.
    554 	 */
    555 	NIC_PUT(sc, AE_P0_IMR,
    556 	    AE_IMR_PRXE | AE_IMR_PTXE | AE_IMR_RXEE | AE_IMR_TXEE |
    557 	    AE_IMR_OVWE);
    558 
    559 	/* Program command register for page 1. */
    560 	NIC_PUT(sc, AE_P0_CR, sc->cr_proto | AE_CR_PAGE_1 | AE_CR_STP);
    561 
    562 	/* Copy out our station address. */
    563 	for (i = 0; i < ETHER_ADDR_LEN; ++i)
    564 		NIC_PUT(sc, AE_P1_PAR0 + i, sc->sc_arpcom.ac_enaddr[i]);
    565 
    566 	/* Set multicast filter on chip. */
    567 	ae_getmcaf(&sc->sc_arpcom, mcaf);
    568 	for (i = 0; i < 8; i++)
    569 		NIC_PUT(sc, AE_P1_MAR0 + i, ((u_char *)mcaf)[i]);
    570 
    571 	/*
    572 	 * Set current page pointer to one page after the boundary pointer, as
    573 	 * recommended in the National manual.
    574 	 */
    575 	sc->next_packet = sc->rec_page_start + 1;
    576 	NIC_PUT(sc, AE_P1_CURR, sc->next_packet);
    577 
    578 	/* Program command register for page 0. */
    579 	NIC_PUT(sc, AE_P1_CR, sc->cr_proto | AE_CR_PAGE_0 | AE_CR_STP);
    580 
    581 	i = AE_RCR_AB | AE_RCR_AM;
    582 	if (ifp->if_flags & IFF_PROMISC) {
    583 		/*
    584 		 * Set promiscuous mode.  Multicast filter was set earlier so
    585 		 * that we should receive all multicast packets.
    586 		 */
    587 		i |= AE_RCR_PRO | AE_RCR_AR | AE_RCR_SEP;
    588 	}
    589 	NIC_PUT(sc, AE_P0_RCR, i);
    590 
    591 	/* Take interface out of loopback. */
    592 	NIC_PUT(sc, AE_P0_TCR, 0);
    593 
    594 	/* Fire up the interface. */
    595 	NIC_PUT(sc, AE_P0_CR, sc->cr_proto | AE_CR_PAGE_0 | AE_CR_STA);
    596 
    597 	/* Set 'running' flag, and clear output active flag. */
    598 	ifp->if_flags |= IFF_RUNNING;
    599 	ifp->if_flags &= ~IFF_OACTIVE;
    600 
    601 	/* ...and attempt to start output. */
    602 	ae_start(ifp);
    603 
    604 	splx(s);
    605 }
    606 
    607 /*
    608  * This routine actually starts the transmission on the interface.
    609  */
    610 static inline void
    611 ae_xmit(sc)
    612 	struct ae_softc *sc;
    613 {
    614 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    615 	u_short len;
    616 
    617 	len = sc->txb_len[sc->txb_next_tx];
    618 
    619 	/* Set NIC for page 0 register access. */
    620 	NIC_PUT(sc, AE_P0_CR, sc->cr_proto | AE_CR_PAGE_0 | AE_CR_STA);
    621 
    622 	/* Set TX buffer start page. */
    623 	NIC_PUT(sc, AE_P0_TPSR, sc->tx_page_start +
    624 	    sc->txb_next_tx * AE_TXBUF_SIZE);
    625 
    626 	/* Set TX length. */
    627 	NIC_PUT(sc, AE_P0_TBCR0, len);
    628 	NIC_PUT(sc, AE_P0_TBCR1, len >> 8);
    629 
    630 	/* Set page 0, remote DMA complete, transmit packet, and *start*. */
    631 	NIC_PUT(sc, AE_P0_CR, sc->cr_proto | AE_CR_PAGE_0 | AE_CR_TXP | AE_CR_STA);
    632 	sc->xmit_busy = 1;
    633 
    634 	/* Point to next transmit buffer slot and wrap if necessary. */
    635 	sc->txb_next_tx++;
    636 	if (sc->txb_next_tx == sc->txb_cnt)
    637 		sc->txb_next_tx = 0;
    638 
    639 	/* Set a timer just in case we never hear from the board again. */
    640 	ifp->if_timer = 2;
    641 }
    642 
    643 /*
    644  * Start output on interface.
    645  * We make two assumptions here:
    646  *  1) that the current priority is set to splimp _before_ this code
    647  *     is called *and* is returned to the appropriate priority after
    648  *     return
    649  *  2) that the IFF_OACTIVE flag is checked before this code is called
    650  *     (i.e. that the output part of the interface is idle)
    651  */
    652 void
    653 ae_start(ifp)
    654 	struct ifnet *ifp;
    655 {
    656 	struct ae_softc *sc = aecd.cd_devs[ifp->if_unit];
    657 	struct mbuf *m0, *m;
    658 	caddr_t buffer;
    659 	int len;
    660 
    661 outloop:
    662 	/*
    663 	 * First, see if there are buffered packets and an idle transmitter -
    664 	 * should never happen at this point.
    665 	 */
    666 	if (sc->txb_inuse && (sc->xmit_busy == 0)) {
    667 		printf("%s: packets buffered, but transmitter idle\n",
    668 		    sc->sc_dev.dv_xname);
    669 		ae_xmit(sc);
    670 	}
    671 
    672 	/* See if there is room to put another packet in the buffer. */
    673 	if (sc->txb_inuse == sc->txb_cnt) {
    674 		/* No room.  Indicate this to the outside world and exit. */
    675 		ifp->if_flags |= IFF_OACTIVE;
    676 		return;
    677 	}
    678 
    679 	IF_DEQUEUE(&sc->sc_arpcom.ac_if.if_snd, m);
    680 	if (m == 0) {
    681 		/*
    682 		 * We are using the !OACTIVE flag to indicate to the outside
    683 		 * world that we can accept an additional packet rather than
    684 		 * that the transmitter is _actually_ active.  Indeed, the
    685 		 * transmitter may be active, but if we haven't filled all the
    686 		 * buffers with data then we still want to accept more.
    687 		 */
    688 		ifp->if_flags &= ~IFF_OACTIVE;
    689 		return;
    690 	}
    691 
    692 	/* Copy the mbuf chain into the transmit buffer. */
    693 	m0 = m;
    694 
    695 	/* txb_new points to next open buffer slot. */
    696 	buffer = sc->mem_start + ((sc->txb_new * AE_TXBUF_SIZE) << AE_PAGE_SHIFT);
    697 
    698 	len = ae_put(sc, m, buffer);
    699 
    700 	sc->txb_len[sc->txb_new] = max(len, ETHER_MIN_LEN);
    701 	sc->txb_inuse++;
    702 
    703 	/* Point to next buffer slot and wrap if necessary. */
    704 	if (++sc->txb_new == sc->txb_cnt)
    705 		sc->txb_new = 0;
    706 
    707 	if (sc->xmit_busy == 0)
    708 		ae_xmit(sc);
    709 
    710 #if NBPFILTER > 0
    711 	/* Tap off here if there is a BPF listener. */
    712 	if (sc->sc_arpcom.ac_if.if_bpf)
    713 		bpf_mtap(sc->sc_arpcom.ac_if.if_bpf, m0);
    714 #endif
    715 
    716 	m_freem(m0);
    717 
    718 	/* Loop back to the top to possibly buffer more packets. */
    719 	goto outloop;
    720 }
    721 
    722 /*
    723  * Ethernet interface receiver interrupt.
    724  */
    725 static inline void
    726 ae_rint(sc)
    727 	struct ae_softc *sc;
    728 {
    729 	u_char boundary, current;
    730 	u_short len, len_save;
    731 	u_char nlen;
    732 	struct ae_ring packet_hdr;
    733 	caddr_t packet_ptr;
    734 
    735 loop:
    736 	/* Set NIC to page 1 registers to get 'current' pointer. */
    737 	NIC_PUT(sc, AE_P0_CR, sc->cr_proto | AE_CR_PAGE_1 | AE_CR_STA);
    738 
    739 	/*
    740 	 * 'sc->next_packet' is the logical beginning of the ring-buffer - i.e.
    741 	 * it points to where new data has been buffered.  The 'CURR' (current)
    742 	 * register points to the logical end of the ring-buffer - i.e. it
    743 	 * points to where additional new data will be added.  We loop here
    744 	 * until the logical beginning equals the logical end (or in other
    745 	 * words, until the ring-buffer is empty).
    746 	 */
    747 	current = NIC_GET(sc, AE_P1_CURR);
    748 	if (sc->next_packet == current)
    749 		return;
    750 
    751 	/* Set NIC to page 0 registers to update boundary register. */
    752 	NIC_PUT(sc, AE_P1_CR, sc->cr_proto | AE_CR_PAGE_0 | AE_CR_STA);
    753 
    754 	do {
    755 		/* Get pointer to this buffer's header structure. */
    756 		packet_ptr = sc->mem_ring +
    757 		    ((sc->next_packet - sc->rec_page_start) << AE_PAGE_SHIFT);
    758 
    759 		/*
    760 		 * The byte count includes a 4 byte header that was added by
    761 		 * the NIC.
    762 		 */
    763 		packet_hdr = *(struct ae_ring *)packet_ptr;
    764 		len = packet_hdr.count;
    765 		len_save = len = ((len >> 8) & 0xff) | ((len & 0xff) << 8);
    766 
    767 		/*
    768 		 * Try do deal with old, buggy chips that sometimes duplicate
    769 		 * the low byte of the length into the high byte.  We do this
    770 		 * by simply ignoring the high byte of the length and always
    771 		 * recalculating it.
    772 		 *
    773 		 * NOTE: sc->next_packet is pointing at the current packet.
    774 		 */
    775 		if (packet_hdr.next_packet >= sc->next_packet)
    776 			nlen = (packet_hdr.next_packet - sc->next_packet);
    777 		else
    778 			nlen = ((packet_hdr.next_packet - sc->rec_page_start) +
    779 				(sc->rec_page_stop - sc->next_packet));
    780 		--nlen;
    781 		if ((len & AE_PAGE_MASK) + sizeof(packet_hdr) > AE_PAGE_SIZE)
    782 			--nlen;
    783 		len = (len & AE_PAGE_MASK) | (nlen << AE_PAGE_SHIFT);
    784 #ifdef DIAGNOSTIC
    785 		if (len != len_save) {
    786 			printf("%s: length does not match next packet pointer\n",
    787 			    sc->sc_dev.dv_xname);
    788 			printf("%s: len %04x nlen %04x start %02x first %02x curr %02x next %02x stop %02x\n",
    789 			    sc->sc_dev.dv_xname, packet_hdr.count, len,
    790 			    sc->rec_page_start, sc->next_packet, current,
    791 			    packet_hdr.next_packet, sc->rec_page_stop);
    792 		}
    793 #endif
    794 
    795 		/*
    796 		 * Be fairly liberal about what we allow as a "reasonable"
    797 		 * length so that a [crufty] packet will make it to BPF (and
    798 		 * can thus be analyzed).  Note that all that is really
    799 		 * important is that we have a length that will fit into one
    800 		 * mbuf cluster or less; the upper layer protocols can then
    801 		 * figure out the length from their own length field(s).
    802 		 */
    803 		if (len <= MCLBYTES &&
    804 		    packet_hdr.next_packet >= sc->rec_page_start &&
    805 		    packet_hdr.next_packet < sc->rec_page_stop) {
    806 			/* Go get packet. */
    807 			ae_get_packet(sc, packet_ptr + sizeof(struct ae_ring),
    808 			    len - sizeof(struct ae_ring));
    809 			++sc->sc_arpcom.ac_if.if_ipackets;
    810 		} else {
    811 			/* Really BAD.  The ring pointers are corrupted. */
    812 			log(LOG_ERR,
    813 			    "%s: NIC memory corrupt - invalid packet length %d\n",
    814 			    sc->sc_dev.dv_xname, len);
    815 			++sc->sc_arpcom.ac_if.if_ierrors;
    816 			ae_reset(sc);
    817 			return;
    818 		}
    819 
    820 		/* Update next packet pointer. */
    821 		sc->next_packet = packet_hdr.next_packet;
    822 
    823 		/*
    824 		 * Update NIC boundary pointer - being careful to keep it one
    825 		 * buffer behind (as recommended by NS databook).
    826 		 */
    827 		boundary = sc->next_packet - 1;
    828 		if (boundary < sc->rec_page_start)
    829 			boundary = sc->rec_page_stop - 1;
    830 		NIC_PUT(sc, AE_P0_BNRY, boundary);
    831 	} while (sc->next_packet != current);
    832 
    833 	goto loop;
    834 }
    835 
    836 /* Ethernet interface interrupt processor. */
    837 int
    838 ae_intr(unit)
    839 	int unit;
    840 {
    841 	struct ae_softc *sc = aecd.cd_devs[unit];
    842 	u_char isr;
    843 
    844 	aeintr_ctr++;
    845 
    846 	/* Set NIC to page 0 registers. */
    847 	NIC_PUT(sc, AE_P0_CR, sc->cr_proto | AE_CR_PAGE_0 | AE_CR_STA);
    848 
    849 	isr = NIC_GET(sc, AE_P0_ISR);
    850 	if (!isr)
    851 		return (0);
    852 
    853 	/* Loop until there are no more new interrupts. */
    854 	for (;;) {
    855 		/*
    856 		 * Reset all the bits that we are 'acknowledging' by writing a
    857 		 * '1' to each bit position that was set.
    858 		 * (Writing a '1' *clears* the bit.)
    859 		 */
    860 		NIC_PUT(sc, AE_P0_ISR, isr);
    861 
    862 		/*
    863 		 * Handle transmitter interrupts.  Handle these first because
    864 		 * the receiver will reset the board under some conditions.
    865 		 */
    866 		if (isr & (AE_ISR_PTX | AE_ISR_TXE)) {
    867 			u_char collisions = NIC_GET(sc, AE_P0_NCR) & 0x0f;
    868 
    869 			/*
    870 			 * Check for transmit error.  If a TX completed with an
    871 			 * error, we end up throwing the packet away.  Really
    872 			 * the only error that is possible is excessive
    873 			 * collisions, and in this case it is best to allow the
    874 			 * automatic mechanisms of TCP to backoff the flow.  Of
    875 			 * course, with UDP we're screwed, but this is expected
    876 			 * when a network is heavily loaded.
    877 			 */
    878 			(void) NIC_GET(sc, AE_P0_TSR);
    879 			if (isr & AE_ISR_TXE) {
    880 				/*
    881 				 * Excessive collisions (16).
    882 				 */
    883 				if ((NIC_GET(sc, AE_P0_TSR) & AE_TSR_ABT)
    884 				    && (collisions == 0)) {
    885 					/*
    886 					 * When collisions total 16, the P0_NCR
    887 					 * will indicate 0, and the TSR_ABT is
    888 					 * set.
    889 					 */
    890 					collisions = 16;
    891 				}
    892 
    893 				/* Update output errors counter. */
    894 				++sc->sc_arpcom.ac_if.if_oerrors;
    895 			} else {
    896 				/*
    897 				 * Update total number of successfully
    898 				 * transmitted packets.
    899 				 */
    900 				++sc->sc_arpcom.ac_if.if_opackets;
    901 			}
    902 
    903 			/* Reset TX busy and output active flags. */
    904 			sc->xmit_busy = 0;
    905 			sc->sc_arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
    906 
    907 			/* Clear watchdog timer. */
    908 			sc->sc_arpcom.ac_if.if_timer = 0;
    909 
    910 			/*
    911 			 * Add in total number of collisions on last
    912 			 * transmission.
    913 			 */
    914 			sc->sc_arpcom.ac_if.if_collisions += collisions;
    915 
    916 			/*
    917 			 * Decrement buffer in-use count if not zero (can only
    918 			 * be zero if a transmitter interrupt occured while not
    919 			 * actually transmitting).
    920 			 * If data is ready to transmit, start it transmitting,
    921 			 * otherwise defer until after handling receiver.
    922 			 */
    923 			if (sc->txb_inuse && --sc->txb_inuse)
    924 				ae_xmit(sc);
    925 		}
    926 
    927 		/* Handle receiver interrupts. */
    928 		if (isr & (AE_ISR_PRX | AE_ISR_RXE | AE_ISR_OVW)) {
    929 			/*
    930 			 * Overwrite warning.  In order to make sure that a
    931 			 * lockup of the local DMA hasn't occurred, we reset
    932 			 * and re-init the NIC.  The NSC manual suggests only a
    933 			 * partial reset/re-init is necessary - but some chips
    934 			 * seem to want more.  The DMA lockup has been seen
    935 			 * only with early rev chips - Methinks this bug was
    936 			 * fixed in later revs.  -DG
    937 			 */
    938 			if (isr & AE_ISR_OVW) {
    939 				++sc->sc_arpcom.ac_if.if_ierrors;
    940 #ifdef DIAGNOSTIC
    941 				log(LOG_WARNING,
    942 				    "%s: warning - receiver ring buffer overrun\n",
    943 				    sc->sc_dev.dv_xname);
    944 #endif
    945 				/* Stop/reset/re-init NIC. */
    946 				ae_reset(sc);
    947 			} else {
    948 				/*
    949 				 * Receiver Error.  One or more of: CRC error,
    950 				 * frame alignment error FIFO overrun, or
    951 				 * missed packet.
    952 				 */
    953 				if (isr & AE_ISR_RXE) {
    954 					++sc->sc_arpcom.ac_if.if_ierrors;
    955 #ifdef AE_DEBUG
    956 					printf("%s: receive error %x\n",
    957 					    sc->sc_dev.dv_xname,
    958 					    NIC_GET(sc, AE_P0_RSR));
    959 #endif
    960 				}
    961 
    962 				/*
    963 				 * Go get the packet(s)
    964 				 * XXX - Doing this on an error is dubious
    965 				 * because there shouldn't be any data to get
    966 				 * (we've configured the interface to not
    967 				 * accept packets with errors).
    968 				 */
    969 				ae_rint(sc);
    970 			}
    971 		}
    972 
    973 		/*
    974 		 * If it looks like the transmitter can take more data, attempt
    975 		 * to start output on the interface.  This is done after
    976 		 * handling the receiver to give the receiver priority.
    977 		 */
    978 		if ((sc->sc_arpcom.ac_if.if_flags & IFF_OACTIVE) == 0)
    979 			ae_start(&sc->sc_arpcom.ac_if);
    980 
    981 		/*
    982 		 * Return NIC CR to standard state: page 0, remote DMA
    983 		 * complete, start (toggling the TXP bit off, even if was just
    984 		 * set in the transmit routine, is *okay* - it is 'edge'
    985 		 * triggered from low to high).
    986 		 */
    987 		NIC_PUT(sc, AE_P0_CR, sc->cr_proto | AE_CR_PAGE_0 | AE_CR_STA);
    988 
    989 		/*
    990 		 * If the Network Talley Counters overflow, read them to reset
    991 		 * them.  It appears that old 8390's won't clear the ISR flag
    992 		 * otherwise - resulting in an infinite loop.
    993 		 */
    994 		if (isr & AE_ISR_CNT) {
    995 			(void) NIC_GET(sc, AE_P0_CNTR0);
    996 			(void) NIC_GET(sc, AE_P0_CNTR1);
    997 			(void) NIC_GET(sc, AE_P0_CNTR2);
    998 		}
    999 
   1000 		isr = NIC_GET(sc, AE_P0_ISR);
   1001 		if (!isr)
   1002 			return (1);
   1003 	}
   1004 }
   1005 
   1006 /*
   1007  * Process an ioctl request.  This code needs some work - it looks pretty ugly.
   1008  */
   1009 int
   1010 ae_ioctl(ifp, command, data)
   1011 	register struct ifnet *ifp;
   1012 	u_long command;
   1013 	caddr_t data;
   1014 {
   1015 	struct ae_softc *sc = aecd.cd_devs[ifp->if_unit];
   1016 	register struct ifaddr *ifa = (struct ifaddr *)data;
   1017 	struct ifreq *ifr = (struct ifreq *)data;
   1018 	int s, error = 0;
   1019 
   1020 	s = splimp();
   1021 
   1022 	switch (command) {
   1023 
   1024 	case SIOCSIFADDR:
   1025 		ifp->if_flags |= IFF_UP;
   1026 
   1027 		switch (ifa->ifa_addr->sa_family) {
   1028 #ifdef INET
   1029 		case AF_INET:
   1030 			ae_init(sc);
   1031 			arp_ifinit(&sc->sc_arpcom, ifa);
   1032 			break;
   1033 #endif
   1034 #ifdef NS
   1035 		/* XXX - This code is probably wrong. */
   1036 		case AF_NS:
   1037 		    {
   1038 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1039 
   1040 			if (ns_nullhost(*ina))
   1041 				ina->x_host =
   1042 				    *(union ns_host *)(sc->sc_arpcom.ac_enaddr);
   1043 			else
   1044 				bcopy(ina->x_host.c_host,
   1045 				    sc->sc_arpcom.ac_enaddr,
   1046 				    sizeof(sc->sc_arpcom.ac_enaddr));
   1047 			/* Set new address. */
   1048 			ae_init(sc);
   1049 			break;
   1050 		    }
   1051 #endif
   1052 		default:
   1053 			ae_init(sc);
   1054 			break;
   1055 		}
   1056 		break;
   1057 
   1058 	case SIOCSIFFLAGS:
   1059 		if ((ifp->if_flags & IFF_UP) == 0 &&
   1060 		    (ifp->if_flags & IFF_RUNNING) != 0) {
   1061 			/*
   1062 			 * If interface is marked down and it is running, then
   1063 			 * stop it.
   1064 			 */
   1065 			ae_stop(sc);
   1066 			ifp->if_flags &= ~IFF_RUNNING;
   1067 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
   1068 			   (ifp->if_flags & IFF_RUNNING) == 0) {
   1069 			/*
   1070 			 * If interface is marked up and it is stopped, then
   1071 			 * start it.
   1072 			 */
   1073 			ae_init(sc);
   1074 		} else {
   1075 			/*
   1076 			 * Reset the interface to pick up changes in any other
   1077 			 * flags that affect hardware registers.
   1078 			 */
   1079 			ae_stop(sc);
   1080 			ae_init(sc);
   1081 		}
   1082 		break;
   1083 
   1084 	case SIOCADDMULTI:
   1085 	case SIOCDELMULTI:
   1086 		/* Update our multicast list. */
   1087 		error = (command == SIOCADDMULTI) ?
   1088 		    ether_addmulti(ifr, &sc->sc_arpcom) :
   1089 		    ether_delmulti(ifr, &sc->sc_arpcom);
   1090 
   1091 		if (error == ENETRESET) {
   1092 			/*
   1093 			 * Multicast list has changed; set the hardware filter
   1094 			 * accordingly.
   1095 			 */
   1096 			ae_stop(sc); /* XXX for ds_setmcaf? */
   1097 			ae_init(sc);
   1098 			error = 0;
   1099 		}
   1100 		break;
   1101 
   1102 	default:
   1103 		error = EINVAL;
   1104 	}
   1105 
   1106 	splx(s);
   1107 	return (error);
   1108 }
   1109 
   1110 /*
   1111  * Retreive packet from shared memory and send to the next level up via
   1112  * ether_input().  If there is a BPF listener, give a copy to BPF, too.
   1113  */
   1114 void
   1115 ae_get_packet(sc, buf, len)
   1116 	struct ae_softc *sc;
   1117 	caddr_t buf;
   1118 	u_short len;
   1119 {
   1120 	struct ether_header *eh;
   1121     	struct mbuf *m, *ae_ring_to_mbuf();
   1122 
   1123 	/* Allocate a header mbuf. */
   1124 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1125 	if (m == 0)
   1126 		return;
   1127 	m->m_pkthdr.rcvif = &sc->sc_arpcom.ac_if;
   1128 	m->m_pkthdr.len = len;
   1129 	m->m_len = 0;
   1130 
   1131 	/* The following silliness is to make NFS happy. */
   1132 #define EROUND	((sizeof(struct ether_header) + 3) & ~3)
   1133 #define EOFF	(EROUND - sizeof(struct ether_header))
   1134 
   1135 	/*
   1136 	 * The following assumes there is room for the ether header in the
   1137 	 * header mbuf.
   1138 	 */
   1139 	m->m_data += EOFF;
   1140 	eh = mtod(m, struct ether_header *);
   1141 
   1142 	word_copy(buf, mtod(m, caddr_t), sizeof(struct ether_header));
   1143 	buf += sizeof(struct ether_header);
   1144 	m->m_len += sizeof(struct ether_header);
   1145 	len -= sizeof(struct ether_header);
   1146 
   1147 	/* Pull packet off interface. */
   1148 	if (ae_ring_to_mbuf(sc, buf, m, len) == 0) {
   1149 		m_freem(m);
   1150 		return;
   1151 	}
   1152 
   1153 #if NBPFILTER > 0
   1154 	/*
   1155 	 * Check if there's a BPF listener on this interface.  If so, hand off
   1156 	 * the raw packet to bpf.
   1157 	 */
   1158 	if (sc->sc_arpcom.ac_if.if_bpf) {
   1159 		bpf_mtap(sc->sc_arpcom.ac_if.if_bpf, m);
   1160 
   1161 		/*
   1162 		 * Note that the interface cannot be in promiscuous mode if
   1163 		 * there are no BPF listeners.  And if we are in promiscuous
   1164 		 * mode, we have to check if this packet is really ours.
   1165 		 */
   1166 		if ((sc->sc_arpcom.ac_if.if_flags & IFF_PROMISC) &&
   1167 		    (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
   1168 		    bcmp(eh->ether_dhost, sc->sc_arpcom.ac_enaddr,
   1169 			    sizeof(eh->ether_dhost)) != 0) {
   1170 			m_freem(m);
   1171 			return;
   1172 		}
   1173 	}
   1174 #endif
   1175 
   1176 	/* Fix up data start offset in mbuf to point past ether header. */
   1177 	m_adj(m, sizeof(struct ether_header));
   1178 	ether_input(&sc->sc_arpcom.ac_if, eh, m);
   1179 }
   1180 
   1181 /*
   1182  * Supporting routines.
   1183  */
   1184 
   1185 /*
   1186  * Given a source and destination address, copy 'amount' of a packet from the
   1187  * ring buffer into a linear destination buffer.  Takes into account ring-wrap.
   1188  */
   1189 static inline caddr_t
   1190 ae_ring_copy(sc, src, dst, amount)
   1191 	struct ae_softc *sc;
   1192 	caddr_t src, dst;
   1193 	u_short	amount;
   1194 {
   1195 	u_short	tmp_amount;
   1196 
   1197 	/* Does copy wrap to lower addr in ring buffer? */
   1198 	if (src + amount > sc->mem_end) {
   1199 		tmp_amount = sc->mem_end - src;
   1200 
   1201 		/* Copy amount up to end of NIC memory. */
   1202 		word_copy(src, dst, tmp_amount);
   1203 
   1204 		amount -= tmp_amount;
   1205 		src = sc->mem_ring;
   1206 		dst += tmp_amount;
   1207 	}
   1208 
   1209 	word_copy(src, dst, amount);
   1210 
   1211 	return (src + amount);
   1212 }
   1213 
   1214 /*
   1215  * Copy data from receive buffer to end of mbuf chain allocate additional mbufs
   1216  * as needed.  Return pointer to last mbuf in chain.
   1217  * sc = ae info (softc)
   1218  * src = pointer in ae ring buffer
   1219  * dst = pointer to last mbuf in mbuf chain to copy to
   1220  * amount = amount of data to copy
   1221  */
   1222 struct mbuf *
   1223 ae_ring_to_mbuf(sc, src, dst, total_len)
   1224 	struct ae_softc *sc;
   1225 	caddr_t src;
   1226 	struct mbuf *dst;
   1227 	u_short total_len;
   1228 {
   1229 	register struct mbuf *m = dst;
   1230 
   1231 	/* Round the length to a word boundary. */
   1232 	total_len = (total_len + 1) & ~1;
   1233 
   1234 	while (total_len) {
   1235 		register u_short amount = min(total_len, M_TRAILINGSPACE(m));
   1236 
   1237 		if (amount == 0) {
   1238 			/*
   1239 			 * No more data in this mbuf; alloc another.
   1240 			 *
   1241 			 * If there is enough data for an mbuf cluster, attempt
   1242 			 * to allocate one of those, otherwise, a regular mbuf
   1243 			 * will do.
   1244 			 * Note that a regular mbuf is always required, even if
   1245 			 * we get a cluster - getting a cluster does not
   1246 			 * allocate any mbufs, and one is needed to assign the
   1247 			 * cluster to.  The mbuf that has a cluster extension
   1248 			 * can not be used to contain data - only the cluster
   1249 			 * can contain data.
   1250 			 */
   1251 			dst = m;
   1252 			MGET(m, M_DONTWAIT, MT_DATA);
   1253 			if (m == 0)
   1254 				return (0);
   1255 
   1256 			if (total_len >= MINCLSIZE)
   1257 				MCLGET(m, M_DONTWAIT);
   1258 
   1259 			m->m_len = 0;
   1260 			dst->m_next = m;
   1261 			amount = min(total_len, M_TRAILINGSPACE(m));
   1262 		}
   1263 
   1264 		src = ae_ring_copy(sc, src, mtod(m, caddr_t) + m->m_len,
   1265 		    amount);
   1266 
   1267 		m->m_len += amount;
   1268 		total_len -= amount;
   1269 	}
   1270 	return (m);
   1271 }
   1272 
   1273 /*
   1274  * Compute the multicast address filter from the list of multicast addresses we
   1275  * need to listen to.
   1276  */
   1277 void
   1278 ae_getmcaf(ac, af)
   1279 	struct arpcom *ac;
   1280 	u_long *af;
   1281 {
   1282 	struct ifnet *ifp = &ac->ac_if;
   1283 	struct ether_multi *enm;
   1284 	register u_char *cp, c;
   1285 	register u_long crc;
   1286 	register int i, len;
   1287 	struct ether_multistep step;
   1288 
   1289 	/*
   1290 	 * Set up multicast address filter by passing all multicast addresses
   1291 	 * through a crc generator, and then using the high order 6 bits as an
   1292 	 * index into the 64 bit logical address filter.  The high order bit
   1293 	 * selects the word, while the rest of the bits select the bit within
   1294 	 * the word.
   1295 	 */
   1296 
   1297 	if (ifp->if_flags & IFF_PROMISC) {
   1298 		ifp->if_flags |= IFF_ALLMULTI;
   1299 		af[0] = af[1] = 0xffffffff;
   1300 		return;
   1301 	}
   1302 
   1303 	af[0] = af[1] = 0;
   1304 	ETHER_FIRST_MULTI(step, ac, enm);
   1305 	while (enm != NULL) {
   1306 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
   1307 		    sizeof(enm->enm_addrlo)) != 0) {
   1308 			/*
   1309 			 * We must listen to a range of multicast addresses.
   1310 			 * For now, just accept all multicasts, rather than
   1311 			 * trying to set only those filter bits needed to match
   1312 			 * the range.  (At this time, the only use of address
   1313 			 * ranges is for IP multicast routing, for which the
   1314 			 * range is big enough to require all bits set.)
   1315 			 */
   1316 			ifp->if_flags |= IFF_ALLMULTI;
   1317 			af[0] = af[1] = 0xffffffff;
   1318 			return;
   1319 		}
   1320 
   1321 		cp = enm->enm_addrlo;
   1322 		crc = 0xffffffff;
   1323 		for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
   1324 			c = *cp++;
   1325 			for (i = 8; --i >= 0;) {
   1326 				if (((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01)) {
   1327 					crc <<= 1;
   1328 					crc ^= 0x04c11db6 | 1;
   1329 				} else
   1330 					crc <<= 1;
   1331 				c >>= 1;
   1332 			}
   1333 		}
   1334 		/* Just want the 6 most significant bits. */
   1335 		crc >>= 26;
   1336 
   1337 		/* Turn on the corresponding bit in the filter. */
   1338 		af[crc >> 5] |= 1 << ((crc & 0x1f) ^ 0);
   1339 
   1340 		ETHER_NEXT_MULTI(step, enm);
   1341 	}
   1342 	ifp->if_flags &= ~IFF_ALLMULTI;
   1343 }
   1344 
   1345 /*
   1346  * Copy packet from mbuf to the board memory
   1347  *
   1348  * Currently uses an extra buffer/extra memory copy,
   1349  * unless the whole packet fits in one mbuf.
   1350  *
   1351  */
   1352 u_short
   1353 ae_put(sc, m, buf)
   1354 	struct ae_softc *sc;
   1355 	struct mbuf *m;
   1356 	caddr_t buf;
   1357 {
   1358 	u_char *data, savebyte[2];
   1359 	int len, wantbyte;
   1360 	u_short totlen;
   1361 
   1362 	wantbyte = 0;
   1363 
   1364 	for (; m != 0; m = m->m_next) {
   1365 		data = mtod(m, u_char *);
   1366 		len = m->m_len;
   1367 		totlen += len;
   1368 		if (len > 0) {
   1369 			/* Finish the last word. */
   1370 			if (wantbyte) {
   1371 				savebyte[1] = *data;
   1372 				word_copy(savebyte, buf, 2);
   1373 				buf += 2;
   1374 				data++;
   1375 				len--;
   1376 				wantbyte = 0;
   1377 			}
   1378 			/* Output contiguous words. */
   1379 			if (len > 1) {
   1380 				word_copy(data, buf, len);
   1381 				buf += len & ~1;
   1382 				data += len & ~1;
   1383 				len &= 1;
   1384 			}
   1385 			/* Save last byte, if necessary. */
   1386 			if (len == 1) {
   1387 				savebyte[0] = *data;
   1388 				wantbyte = 1;
   1389 			}
   1390 		}
   1391 	}
   1392 
   1393 	if (wantbyte) {
   1394 		savebyte[1] = 0;
   1395 		word_copy(savebyte, buf, 2);
   1396 		buf += 2;
   1397 	}
   1398 
   1399 	return (totlen);
   1400 }
   1401