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