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