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