Home | History | Annotate | Line # | Download | only in dev
if_ae.c revision 1.50
      1 /*	$NetBSD: if_ae.c,v 1.50 1996/12/18 02:54:43 scottr Exp $	*/
      2 
      3 /*
      4  * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
      5  * adapters.
      6  *
      7  * Copyright (c) 1994, 1995 Charles M. Hannum.  All rights reserved.
      8  *
      9  * Copyright (C) 1993, David Greenman.  This software may be used, modified,
     10  * copied, distributed, and sold, in both source and binary form provided that
     11  * the above copyright and these terms are retained.  Under no circumstances is
     12  * the author responsible for the proper functioning of this software, nor does
     13  * the author assume any responsibility for damages incurred with its use.
     14  *
     15  * Adapted for MacBSD by Brad Parker <brad (at) fcr.com>.
     16  *
     17  * Currently supports:
     18  *	Apples NB Ethernet card
     19  *	Interlan A310 Nubus Ethernet card
     20  *	Cayman Systems GatorCard
     21  *	Asante MacCon II/E
     22  */
     23 
     24 #include "bpfilter.h"
     25 
     26 #include <sys/param.h>
     27 #include <sys/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 static int	ae_card_vendor __P((struct nubus_attach_args *na));
    107 static int	ae_size_card_memory __P((caddr_t addr));
    108 
    109 int aematch __P((struct device *, struct cfdata *, void *));
    110 void aeattach __P((struct device *, struct device *, void *));
    111 void aeintr __P((void *, int));
    112 int aeioctl __P((struct ifnet *, u_long, caddr_t));
    113 void aestart __P((struct ifnet *));
    114 void aewatchdog __P((struct ifnet *));
    115 void aereset __P((struct ae_softc *));
    116 void aeinit __P((struct ae_softc *));
    117 void aestop __P((struct ae_softc *));
    118 
    119 void aeread __P((struct ae_softc *, caddr_t, int));
    120 struct mbuf *aeget __P((struct ae_softc *, caddr_t, int));
    121 
    122 #define inline			/* XXX for debugging porpoises */
    123 
    124 u_short ae_put __P((struct ae_softc *, struct mbuf *, caddr_t));
    125 void ae_getmcaf __P((struct arpcom *, u_char *));
    126 
    127 static inline void ae_rint __P((struct ae_softc *));
    128 static inline void ae_xmit __P((struct ae_softc *));
    129 static inline caddr_t ae_ring_copy __P((
    130 		struct ae_softc *, caddr_t, caddr_t, int));
    131 
    132 struct cfattach ae_ca = {
    133 	sizeof(struct ae_softc), aematch, aeattach
    134 };
    135 
    136 struct cfdriver ae_cd = {
    137 	NULL, "ae", DV_IFNET
    138 };
    139 
    140 #define	ETHER_MIN_LEN	64
    141 #define ETHER_MAX_LEN	1518
    142 #define	ETHER_ADDR_LEN	6
    143 
    144 static char zero = 0;
    145 
    146 /*
    147  * XXX These two should be moved to locore, and maybe changed to use shorts
    148  * instead of bytes.  The reason for these is that bcopy and bzero use longs,
    149  * which the ethernet cards can't handle.
    150  */
    151 
    152 void			bszero __P((u_short *addr, int len));
    153 static inline void	word_copy __P((caddr_t a, caddr_t b, int len));
    154 static inline void	byte_copy __P((caddr_t a, caddr_t b, int len));
    155 
    156 void
    157 bszero(u_short * addr, int len)
    158 {
    159 	while (len--)
    160 		*addr++ = 0;
    161 }
    162 
    163 /*
    164  * Memory copy, copies word at time.
    165  */
    166 static inline void
    167 word_copy(a, b, len)
    168 	caddr_t a, b;
    169 	int     len;
    170 {
    171 	u_short *x = (u_short *) a, *y = (u_short *) b;
    172 
    173 	len >>= 1;
    174 	while (len--)
    175 		*y++ = *x++;
    176 }
    177 
    178 /*
    179  * Memory copy, copies bytes at time.
    180  */
    181 static inline void
    182 byte_copy(a, b, len)
    183 	caddr_t a, b;
    184 	int     len;
    185 {
    186 	while (len--)
    187 		*b++ = *a++;
    188 }
    189 
    190 static int
    191 ae_card_vendor(na)
    192 	struct nubus_attach_args *na;
    193 {
    194 	int vendor;
    195 
    196 	switch (na->drsw) {
    197 	case NUBUS_DRSW_3COM:
    198 	case NUBUS_DRSW_APPLE:
    199 	case NUBUS_DRSW_TECHWORKS:
    200 		vendor = AE_VENDOR_APPLE;
    201 		break;
    202 	case NUBUS_DRSW_ASANTE:
    203 		vendor = AE_VENDOR_ASANTE;
    204 		break;
    205 	case NUBUS_DRSW_FARALLON:
    206 		vendor = AE_VENDOR_FARALLON;
    207 		break;
    208 	case NUBUS_DRSW_FOCUS:
    209 		vendor = AE_VENDOR_FOCUS;
    210 		break;
    211 	case NUBUS_DRSW_GATOR:
    212 		switch (na->drhw) {
    213 		default:
    214 		case NUBUS_DRHW_INTERLAN:
    215 			vendor = AE_VENDOR_INTERLAN;
    216 			break;
    217 		case NUBUS_DRHW_KINETICS:
    218 			vendor = AE_VENDOR_DAYNA;
    219 			break;
    220 		}
    221 		break;
    222 	default:
    223 #ifdef AE_DEBUG
    224 		printf("Unknown ethernet drsw: %x\n", na->drsw);
    225 #endif
    226 		vendor = AE_VENDOR_UNKNOWN;
    227 	}
    228 	return vendor;
    229 }
    230 
    231 static int
    232 ae_size_card_memory(addr)
    233 	caddr_t addr;
    234 {
    235 	u_short *p;
    236 	u_short i1, i2, i3, i4;
    237 
    238 	p = (u_short *) addr;
    239 
    240 	/*
    241 	 * very simple size memory, assuming it's installed in 8k
    242 	 * banks; also assume it will generally mirror in upper banks
    243 	 * if not installed.
    244 	 */
    245 	i1 = (8192 * 0) / 2;
    246 	i2 = (8192 * 1) / 2;
    247 	i3 = (8192 * 2) / 2;
    248 	i4 = (8192 * 3) / 2;
    249 
    250 	p[i1] = 0x1111;
    251 	p[i2] = 0x2222;
    252 	p[i3] = 0x3333;
    253 	p[i4] = 0x4444;
    254 
    255 	if (p[i1] == 0x1111 && p[i2] == 0x2222 &&
    256 	    p[i3] == 0x3333 && p[i4] == 0x4444)
    257 		return 8192 * 4;
    258 
    259 	if ((p[i1] == 0x1111 && p[i2] == 0x2222) ||
    260 	    (p[i1] == 0x3333 && p[i2] == 0x4444))
    261 		return 8192 * 2;
    262 
    263 	if (p[i1] == 0x1111 || p[i1] == 0x4444)
    264 		return 8192;
    265 
    266 	return 0;
    267 }
    268 
    269 int
    270 aematch(parent, cf, aux)
    271 	struct device *parent;
    272 	struct cfdata *cf;
    273 	void *aux;
    274 {
    275 	struct nubus_attach_args *na = (struct nubus_attach_args *) aux;
    276 
    277 	if (na->category != NUBUS_CATEGORY_NETWORK)
    278 		return 0;
    279 
    280 	if (na->type != NUBUS_TYPE_ETHERNET)
    281 		return 0;
    282 
    283 	switch (ae_card_vendor(na)) {
    284 	case AE_VENDOR_APPLE:
    285 	case AE_VENDOR_ASANTE:
    286 	case AE_VENDOR_FARALLON:
    287 	case AE_VENDOR_INTERLAN:
    288 		break;
    289 
    290 	case AE_VENDOR_DAYNA:
    291 	case AE_VENDOR_FOCUS:
    292 	default:
    293 		return 0;
    294 	}
    295 	return 1;
    296 }
    297 
    298 /*
    299  * Install interface into kernel networking data structures
    300  */
    301 void
    302 aeattach(parent, self, aux)
    303 	struct device *parent, *self;
    304 	void   *aux;
    305 {
    306 	struct ae_softc *sc = (struct ae_softc *) self;
    307 	struct nubus_attach_args *na = (struct nubus_attach_args *) aux;
    308 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    309 	caddr_t addr;
    310 	int i, memsize;
    311 	int flags = 0;
    312 
    313 	sc->regs_rev = 0;
    314 	sc->vendor = ae_card_vendor(na);
    315 	strncpy(sc->type_str, nubus_get_card_name(na->fmt),
    316 	    INTERFACE_NAME_LEN);
    317 	sc->type_str[INTERFACE_NAME_LEN-1] = '\0';
    318 
    319 	addr = (caddr_t) na->fmt->virtual_base;
    320 	memsize = 0;
    321 
    322 	switch (sc->vendor) {
    323 	case AE_VENDOR_INTERLAN:
    324 		sc->nic_addr = addr + GC_NIC_OFFSET;
    325 		sc->rom_addr = addr + GC_ROM_OFFSET;
    326 		sc->mem_start = addr + GC_DATA_OFFSET;
    327 		if ((memsize = ae_size_card_memory(sc->mem_start)) == 0) {
    328 			printf(": failed to determine size of RAM.\n");
    329 			return;
    330 		}
    331 
    332 		/* reset the NIC chip */
    333 		*((caddr_t) addr + GC_RESET_OFFSET) = (char) zero;
    334 
    335 		/* Get station address from on-board ROM */
    336 		for (i = 0; i < ETHER_ADDR_LEN; ++i)
    337 			sc->sc_arpcom.ac_enaddr[i] = *(sc->rom_addr + i * 4);
    338 		break;
    339 
    340 		/* apple-compatible cards */
    341 	case AE_VENDOR_ASANTE:
    342 	case AE_VENDOR_APPLE:
    343 		sc->regs_rev = 1;
    344 		sc->nic_addr = addr + AE_NIC_OFFSET;
    345 		sc->rom_addr = addr + AE_ROM_OFFSET;
    346 		sc->mem_start = addr + AE_DATA_OFFSET;
    347 		if ((memsize = ae_size_card_memory(sc->mem_start)) == 0) {
    348 			printf(": failed to determine size of RAM.\n");
    349 			return;
    350 		}
    351 
    352 		/* Get station address from on-board ROM */
    353 		for (i = 0; i < ETHER_ADDR_LEN; ++i)
    354 			sc->sc_arpcom.ac_enaddr[i] = *(sc->rom_addr + i * 2);
    355 		break;
    356 
    357 	case AE_VENDOR_DAYNA:
    358 		sc->nic_addr = addr + DP_NIC_OFFSET;
    359 		sc->rom_addr = addr + DP_ROM_OFFSET;
    360 		sc->mem_start = addr + DP_DATA_OFFSET;
    361 		memsize = 8192;
    362 
    363 		/* Get station address from on-board ROM */
    364 		for (i = 0; i < ETHER_ADDR_LEN; ++i)
    365 			sc->sc_arpcom.ac_enaddr[i] = *(sc->rom_addr + i * 2);
    366 
    367 		printf(": unsupported Dayna hardware\n");
    368 		return;
    369 
    370 	case AE_VENDOR_FARALLON:
    371 		sc->regs_rev = 1;
    372 		sc->rom_addr = addr + FE_ROM_OFFSET;
    373 		sc->nic_addr = addr + AE_NIC_OFFSET;
    374 		sc->mem_start = addr + AE_DATA_OFFSET;
    375 		if ((memsize = ae_size_card_memory(sc->mem_start)) == 0) {
    376 			printf(": failed to determine size of RAM.\n");
    377 			return;
    378 		}
    379 
    380 		/* Get station address from on-board ROM */
    381 		for (i = 0; i < ETHER_ADDR_LEN; ++i)
    382 			sc->sc_arpcom.ac_enaddr[i] = *(sc->rom_addr + i);
    383 		break;
    384 	case AE_VENDOR_FOCUS:
    385 		printf(": unsupported Focus hardware\n");
    386 		return;
    387 	}
    388 
    389 	sc->cr_proto = ED_CR_RD2;
    390 
    391 	/* Allocate one xmit buffer if < 16k, two buffers otherwise. */
    392 	if ((memsize < 16384) || (flags & AE_FLAGS_NO_DOUBLE_BUFFERING))
    393 		sc->txb_cnt = 1;
    394 	else
    395 		sc->txb_cnt = 2;
    396 
    397 	sc->tx_page_start = 0;
    398 	sc->rec_page_start = sc->tx_page_start + sc->txb_cnt * ED_TXBUF_SIZE;
    399 	sc->rec_page_stop = sc->tx_page_start + (memsize >> ED_PAGE_SHIFT);
    400 	sc->mem_ring = sc->mem_start + (sc->rec_page_start << ED_PAGE_SHIFT);
    401 	sc->mem_size = memsize;
    402 	sc->mem_end = sc->mem_start + memsize;
    403 
    404 	/* Now zero memory and verify that it is clear. */
    405 	bszero((u_short *) sc->mem_start, memsize / 2);
    406 
    407 	for (i = 0; i < memsize; ++i)
    408 		if (sc->mem_start[i])
    409 printf("%s: failed to clear shared memory at %p - check configuration\n",
    410 			    sc->sc_dev.dv_xname,
    411 			    sc->mem_start + i);
    412 
    413 	bcopy(na->fmt, &sc->sc_slot, sizeof(nubus_slot));
    414 
    415 
    416 	/* Set interface to stopped condition (reset). */
    417 	aestop(sc);
    418 
    419 	/* Initialize ifnet structure. */
    420 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    421 	ifp->if_softc = sc;
    422 	ifp->if_start = aestart;
    423 	ifp->if_ioctl = aeioctl;
    424 	ifp->if_watchdog = aewatchdog;
    425 	ifp->if_flags =
    426 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    427 
    428 	/* Attach the interface. */
    429 	if_attach(ifp);
    430 	ether_ifattach(ifp);
    431 
    432 	/* Print additional info when attached. */
    433 	printf(": address %s, ", ether_sprintf(sc->sc_arpcom.ac_enaddr));
    434 
    435 	printf("type %s, %ldKB memory\n", sc->type_str, sc->mem_size / 1024);
    436 
    437 #if NBPFILTER > 0
    438 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    439 #endif
    440 
    441 	/* make sure interrupts are vectored to us */
    442 	add_nubus_intr(sc->sc_slot.slot, aeintr, sc);
    443 
    444 	/*
    445 	 * XXX -- enable nubus interrupts here.  Should be done elsewhere,
    446 	 *        but that currently breaks with some nubus video cards'
    447 	 *	  interrupts.  So we only enable nubus interrupts if we
    448 	 *	  have an ethernet card...  i.e., we do it here.
    449 	 */
    450 	enable_nubus_intr();
    451 }
    452 
    453 /*
    454  * Reset interface.
    455  */
    456 void
    457 aereset(sc)
    458 	struct ae_softc *sc;
    459 {
    460 	int     s;
    461 
    462 	s = splnet();
    463 	aestop(sc);
    464 	aeinit(sc);
    465 	splx(s);
    466 }
    467 
    468 /*
    469  * Take interface offline.
    470  */
    471 void
    472 aestop(sc)
    473 	struct ae_softc *sc;
    474 {
    475 	int     n = 5000;
    476 
    477 	/* Stop everything on the interface, and select page 0 registers. */
    478 	NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
    479 
    480 	/*
    481 	 * Wait for interface to enter stopped state, but limit # of checks to
    482 	 * 'n' (about 5ms).  It shouldn't even take 5us on modern DS8390's, but
    483 	 * just in case it's an old one.
    484 	 */
    485 	while (((NIC_GET(sc, ED_P0_ISR) & ED_ISR_RST) == 0) && --n);
    486 }
    487 
    488 /*
    489  * Device timeout/watchdog routine.  Entered if the device neglects to generate
    490  * an interrupt after a transmit has been started on it.
    491  */
    492 static int aeintr_ctr = 0;
    493 
    494 void
    495 aewatchdog(ifp)
    496 	struct ifnet *ifp;
    497 {
    498 	struct ae_softc *sc = ifp->if_softc;
    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]) ((void *) 1);
    511 
    512 	if (i != aeintr_ctr) {
    513 		log(LOG_ERR, "%s: device timeout, recovered\n",
    514 		    sc->sc_dev.dv_xname);
    515 		return;
    516 	}
    517 #endif
    518 
    519 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
    520 	++sc->sc_arpcom.ac_if.if_oerrors;
    521 
    522 	aereset(sc);
    523 }
    524 
    525 /*
    526  * Initialize device.
    527  */
    528 void
    529 aeinit(sc)
    530 	struct ae_softc *sc;
    531 {
    532 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    533 	int     i;
    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 = ifp->if_softc;
    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, slot)
    861 	void	*arg;
    862 	int	slot;
    863 {
    864 	struct ae_softc *sc = arg;
    865 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    866 	u_char  isr;
    867 
    868 	aeintr_ctr++;
    869 
    870 	/* Set NIC to page 0 registers. */
    871 	NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
    872 
    873 	isr = NIC_GET(sc, ED_P0_ISR);
    874 	if (!isr)
    875 		return;
    876 
    877 	/* Loop until there are no more new interrupts. */
    878 	for (;;) {
    879 		/*
    880 		 * Reset all the bits that we are 'acknowledging' by writing a
    881 		 * '1' to each bit position that was set.
    882 		 * (Writing a '1' *clears* the bit.)
    883 		 */
    884 		NIC_PUT(sc, ED_P0_ISR, isr);
    885 
    886 		/*
    887 		 * Handle transmitter interrupts.  Handle these first because
    888 		 * the receiver will reset the board under some conditions.
    889 		 */
    890 		if (isr & (ED_ISR_PTX | ED_ISR_TXE)) {
    891 			u_char  collisions = NIC_GET(sc, ED_P0_NCR) & 0x0f;
    892 
    893 			/*
    894 			 * Check for transmit error.  If a TX completed with an
    895 			 * error, we end up throwing the packet away.  Really
    896 			 * the only error that is possible is excessive
    897 			 * collisions, and in this case it is best to allow the
    898 			 * automatic mechanisms of TCP to backoff the flow.  Of
    899 			 * course, with UDP we're screwed, but this is expected
    900 			 * when a network is heavily loaded.
    901 			 */
    902 			(void) NIC_GET(sc, ED_P0_TSR);
    903 			if (isr & ED_ISR_TXE) {
    904 				/*
    905 				 * Excessive collisions (16).
    906 				 */
    907 				if ((NIC_GET(sc, ED_P0_TSR) & ED_TSR_ABT)
    908 				    && (collisions == 0)) {
    909 					/*
    910 					 * When collisions total 16, the P0_NCR
    911 					 * will indicate 0, and the TSR_ABT is
    912 					 * set.
    913 					 */
    914 					collisions = 16;
    915 				}
    916 				/* Update output errors counter. */
    917 				++ifp->if_oerrors;
    918 			} else {
    919 				/*
    920 				 * Update total number of successfully
    921 				 * transmitted packets.
    922 				 */
    923 				++ifp->if_opackets;
    924 			}
    925 
    926 			/* Done with the buffer. */
    927 			sc->txb_inuse--;
    928 
    929 			/* Clear watchdog timer. */
    930 			ifp->if_timer = 0;
    931 			ifp->if_flags &= ~IFF_OACTIVE;
    932 
    933 			/*
    934 			 * Add in total number of collisions on last
    935 			 * transmission.
    936 			 */
    937 			ifp->if_collisions += collisions;
    938 
    939 			/*
    940 			 * Decrement buffer in-use count if not zero (can only
    941 			 * be zero if a transmitter interrupt occured while not
    942 			 * actually transmitting).
    943 			 * If data is ready to transmit, start it transmitting,
    944 			 * otherwise defer until after handling receiver.
    945 			 */
    946 			if (sc->txb_inuse > 0)
    947 				ae_xmit(sc);
    948 		}
    949 		/* Handle receiver interrupts. */
    950 		if (isr & (ED_ISR_PRX | ED_ISR_RXE | ED_ISR_OVW)) {
    951 			/*
    952 			 * Overwrite warning.  In order to make sure that a
    953 			 * lockup of the local DMA hasn't occurred, we reset
    954 			 * and re-init the NIC.  The NSC manual suggests only a
    955 			 * partial reset/re-init is necessary - but some chips
    956 			 * seem to want more.  The DMA lockup has been seen
    957 			 * only with early rev chips - Methinks this bug was
    958 			 * fixed in later revs.  -DG
    959 			 */
    960 			if (isr & ED_ISR_OVW) {
    961 				++ifp->if_ierrors;
    962 #ifdef DIAGNOSTIC
    963 				log(LOG_WARNING,
    964 				    "%s: warning - receiver ring buffer overrun\n",
    965 				    sc->sc_dev.dv_xname);
    966 #endif
    967 				/* Stop/reset/re-init NIC. */
    968 				aereset(sc);
    969 			} else {
    970 				/*
    971 				 * Receiver Error.  One or more of: CRC error,
    972 				 * frame alignment error FIFO overrun, or
    973 				 * missed packet.
    974 				 */
    975 				if (isr & ED_ISR_RXE) {
    976 					++ifp->if_ierrors;
    977 #ifdef AE_DEBUG
    978 					printf("%s: receive error %x\n",
    979 					    sc->sc_dev.dv_xname,
    980 					    NIC_GET(sc, ED_P0_RSR));
    981 #endif
    982 				}
    983 				/*
    984 				 * Go get the packet(s)
    985 				 * XXX - Doing this on an error is dubious
    986 				 * because there shouldn't be any data to get
    987 				 * (we've configured the interface to not
    988 				 * accept packets with errors).
    989 				 */
    990 				ae_rint(sc);
    991 			}
    992 		}
    993 		/*
    994 		 * If it looks like the transmitter can take more data, attempt
    995 		 * to start output on the interface.  This is done after
    996 		 * handling the receiver to give the receiver priority.
    997 		 */
    998 		aestart(ifp);
    999 
   1000 		/*
   1001 		 * Return NIC CR to standard state: page 0, remote DMA
   1002 		 * complete, start (toggling the TXP bit off, even if was just
   1003 		 * set in the transmit routine, is *okay* - it is 'edge'
   1004 		 * triggered from low to high).
   1005 		 */
   1006 		NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
   1007 
   1008 		/*
   1009 		 * If the Network Talley Counters overflow, read them to reset
   1010 		 * them.  It appears that old 8390's won't clear the ISR flag
   1011 		 * otherwise - resulting in an infinite loop.
   1012 		 */
   1013 		if (isr & ED_ISR_CNT) {
   1014 			static u_char	dummy;
   1015 			dummy = NIC_GET(sc, ED_P0_CNTR0);
   1016 			dummy = NIC_GET(sc, ED_P0_CNTR1);
   1017 			dummy = NIC_GET(sc, ED_P0_CNTR2);
   1018 		}
   1019 		isr = NIC_GET(sc, ED_P0_ISR);
   1020 		if (!isr)
   1021 			return;
   1022 	}
   1023 }
   1024 
   1025 /*
   1026  * Process an ioctl request.  This code needs some work - it looks pretty ugly.
   1027  */
   1028 int
   1029 aeioctl(ifp, cmd, data)
   1030 	register struct ifnet *ifp;
   1031 	u_long  cmd;
   1032 	caddr_t data;
   1033 {
   1034 	struct ae_softc *sc = ifp->if_softc;
   1035 	register struct ifaddr *ifa = (struct ifaddr *) data;
   1036 	struct ifreq *ifr = (struct ifreq *) data;
   1037 	int     s, error = 0;
   1038 
   1039 	s = splnet();
   1040 
   1041 	switch (cmd) {
   1042 
   1043 	case SIOCSIFADDR:
   1044 		ifp->if_flags |= IFF_UP;
   1045 
   1046 		switch (ifa->ifa_addr->sa_family) {
   1047 #ifdef INET
   1048 		case AF_INET:
   1049 			aeinit(sc);
   1050 			arp_ifinit(&sc->sc_arpcom, ifa);
   1051 			break;
   1052 #endif
   1053 #ifdef NS
   1054 			/* XXX - This code is probably wrong. */
   1055 		case AF_NS:
   1056 			{
   1057 				register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1058 
   1059 				if (ns_nullhost(*ina))
   1060 					ina->x_host =
   1061 					    *(union ns_host *) (sc->sc_arpcom.ac_enaddr);
   1062 				else
   1063 					bcopy(ina->x_host.c_host,
   1064 					    sc->sc_arpcom.ac_enaddr,
   1065 					    sizeof(sc->sc_arpcom.ac_enaddr));
   1066 				/* Set new address. */
   1067 				aeinit(sc);
   1068 				break;
   1069 			}
   1070 #endif
   1071 		default:
   1072 			aeinit(sc);
   1073 			break;
   1074 		}
   1075 		break;
   1076 
   1077 	case SIOCSIFFLAGS:
   1078 		if ((ifp->if_flags & IFF_UP) == 0 &&
   1079 		    (ifp->if_flags & IFF_RUNNING) != 0) {
   1080 			/*
   1081 			 * If interface is marked down and it is running, then
   1082 			 * stop it.
   1083 			 */
   1084 			aestop(sc);
   1085 			ifp->if_flags &= ~IFF_RUNNING;
   1086 		} else
   1087 			if ((ifp->if_flags & IFF_UP) != 0 &&
   1088 			    (ifp->if_flags & IFF_RUNNING) == 0) {
   1089 				/*
   1090 				 * If interface is marked up and it is stopped, then
   1091 				 * start it.
   1092 				 */
   1093 				aeinit(sc);
   1094 			} else {
   1095 				/*
   1096 				 * Reset the interface to pick up changes in any other
   1097 				 * flags that affect hardware registers.
   1098 				 */
   1099 				aestop(sc);
   1100 				aeinit(sc);
   1101 			}
   1102 		break;
   1103 
   1104 	case SIOCADDMULTI:
   1105 	case SIOCDELMULTI:
   1106 		/* Update our multicast list. */
   1107 		error = (cmd == SIOCADDMULTI) ?
   1108 		    ether_addmulti(ifr, &sc->sc_arpcom) :
   1109 		    ether_delmulti(ifr, &sc->sc_arpcom);
   1110 
   1111 		if (error == ENETRESET) {
   1112 			/*
   1113 			 * Multicast list has changed; set the hardware filter
   1114 			 * accordingly.
   1115 			 */
   1116 			aestop(sc);	/* XXX for ds_setmcaf? */
   1117 			aeinit(sc);
   1118 			error = 0;
   1119 		}
   1120 		break;
   1121 
   1122 	default:
   1123 		error = EINVAL;
   1124 		break;
   1125 	}
   1126 
   1127 	splx(s);
   1128 	return (error);
   1129 }
   1130 
   1131 /*
   1132  * Retreive packet from shared memory and send to the next level up via
   1133  * ether_input().  If there is a BPF listener, give a copy to BPF, too.
   1134  */
   1135 void
   1136 aeread(sc, buf, len)
   1137 	struct ae_softc *sc;
   1138 	caddr_t buf;
   1139 	int len;
   1140 {
   1141 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
   1142 	struct mbuf *m;
   1143 	struct ether_header *eh;
   1144 
   1145 	/* Pull packet off interface. */
   1146 	m = aeget(sc, buf, len);
   1147 	if (m == 0) {
   1148 		ifp->if_ierrors++;
   1149 		return;
   1150 	}
   1151 
   1152 	ifp->if_ipackets++;
   1153 
   1154 	/* We assume that the header fits entirely in one mbuf. */
   1155 	eh = mtod(m, struct ether_header *);
   1156 
   1157 #if NBPFILTER > 0
   1158 	/*
   1159 	 * Check if there's a BPF listener on this interface.
   1160 	 * If so, hand off the raw packet to bpf.
   1161 	 */
   1162 	if (ifp->if_bpf) {
   1163 		bpf_mtap(ifp->if_bpf, m);
   1164 
   1165 		/*
   1166 		 * Note that the interface cannot be in promiscuous mode if
   1167 		 * there are no BPF listeners.  And if we are in promiscuous
   1168 		 * mode, we have to check if this packet is really ours.
   1169 		 */
   1170 		if ((ifp->if_flags & IFF_PROMISC) &&
   1171 		    (eh->ether_dhost[0] & 1) == 0 &&	/* !mcast and !bcast */
   1172 		    bcmp(eh->ether_dhost, sc->sc_arpcom.ac_enaddr,
   1173 			sizeof(eh->ether_dhost)) != 0) {
   1174 			m_freem(m);
   1175 			return;
   1176 		}
   1177 	}
   1178 #endif
   1179 
   1180 	/* Fix up data start offset in mbuf to point past ether header. */
   1181 	m_adj(m, sizeof(struct ether_header));
   1182 	ether_input(ifp, eh, m);
   1183 }
   1184 
   1185 /*
   1186  * Supporting routines.
   1187  */
   1188 /*
   1189  * Given a source and destination address, copy 'amount' of a packet from the
   1190  * ring buffer into a linear destination buffer.  Takes into account ring-wrap.
   1191  */
   1192 static inline caddr_t
   1193 ae_ring_copy(sc, src, dst, amount)
   1194 	struct ae_softc *sc;
   1195 	caddr_t src, dst;
   1196 	int amount;
   1197 {
   1198 	u_short tmp_amount;
   1199 
   1200 	/* Does copy wrap to lower addr in ring buffer? */
   1201 	if (src + amount > sc->mem_end) {
   1202 		tmp_amount = sc->mem_end - src;
   1203 
   1204 		/* Copy amount up to end of NIC memory. */
   1205 		byte_copy(src, dst, tmp_amount);
   1206 
   1207 		amount -= tmp_amount;
   1208 		src = sc->mem_ring;
   1209 		dst += tmp_amount;
   1210 	}
   1211 	byte_copy(src, dst, amount);
   1212 
   1213 	return (src + amount);
   1214 }
   1215 
   1216 /*
   1217  * Copy data from receive buffer to end of mbuf chain allocate additional mbufs
   1218  * as needed.  Return pointer to last mbuf in chain.
   1219  * sc = ae info (softc)
   1220  * src = pointer in ae ring buffer
   1221  * dst = pointer to last mbuf in mbuf chain to copy to
   1222  * amount = amount of data to copy
   1223  */
   1224 struct mbuf *
   1225 aeget(sc, src, total_len)
   1226 	struct ae_softc *sc;
   1227 	caddr_t src;
   1228 	u_short total_len;
   1229 {
   1230 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
   1231 	struct mbuf *top, **mp, *m;
   1232 	int len;
   1233 
   1234 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1235 	if (m == 0)
   1236 		return 0;
   1237 	m->m_pkthdr.rcvif = ifp;
   1238 	m->m_pkthdr.len = total_len;
   1239 	len = MHLEN;
   1240 	top = 0;
   1241 	mp = &top;
   1242 
   1243 	while (total_len > 0) {
   1244 		if (top) {
   1245 			MGET(m, M_DONTWAIT, MT_DATA);
   1246 			if (m == 0) {
   1247 				m_freem(top);
   1248 				return 0;
   1249 			}
   1250 			len = MLEN;
   1251 		}
   1252 		if (total_len >= MINCLSIZE) {
   1253 			MCLGET(m, M_DONTWAIT);
   1254 			if (m->m_flags & M_EXT)
   1255 				len = MCLBYTES;
   1256 		}
   1257 		m->m_len = len = min(total_len, len);
   1258 		src = ae_ring_copy(sc, src, mtod(m, caddr_t), (int) len);
   1259 		total_len -= len;
   1260 		*mp = m;
   1261 		mp = &m->m_next;
   1262 	}
   1263 
   1264 	return top;
   1265 }
   1266 /*
   1267  * Compute the multicast address filter from the list of multicast addresses we
   1268  * need to listen to.
   1269  */
   1270 void
   1271 ae_getmcaf(ac, af)
   1272 	struct arpcom *ac;
   1273 	u_char *af;
   1274 {
   1275 	struct ifnet *ifp = &ac->ac_if;
   1276 	struct ether_multi *enm;
   1277 	register u_char *cp, c;
   1278 	register u_long crc;
   1279 	register int i, len;
   1280 	struct ether_multistep step;
   1281 
   1282 	/*
   1283 	 * Set up multicast address filter by passing all multicast addresses
   1284 	 * through a crc generator, and then using the high order 6 bits as an
   1285 	 * index into the 64 bit logical address filter.  The high order bit
   1286 	 * selects the word, while the rest of the bits select the bit within
   1287 	 * the word.
   1288 	 */
   1289 
   1290 	if (ifp->if_flags & IFF_PROMISC) {
   1291 		ifp->if_flags |= IFF_ALLMULTI;
   1292 		for (i = 0; i < 8; i++)
   1293 			af[i] = 0xff;
   1294 		return;
   1295 	}
   1296 	for (i = 0; i < 8; i++)
   1297 		af[i] = 0;
   1298 	ETHER_FIRST_MULTI(step, ac, enm);
   1299 	while (enm != NULL) {
   1300 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
   1301 			sizeof(enm->enm_addrlo)) != 0) {
   1302 			/*
   1303 			 * We must listen to a range of multicast addresses.
   1304 			 * For now, just accept all multicasts, rather than
   1305 			 * trying to set only those filter bits needed to match
   1306 			 * the range.  (At this time, the only use of address
   1307 			 * ranges is for IP multicast routing, for which the
   1308 			 * range is big enough to require all bits set.)
   1309 			 */
   1310 			ifp->if_flags |= IFF_ALLMULTI;
   1311 			for (i = 0; i < 8; i++)
   1312 				af[i] = 0xff;
   1313 			return;
   1314 		}
   1315 		cp = enm->enm_addrlo;
   1316 		crc = 0xffffffff;
   1317 		for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
   1318 			c = *cp++;
   1319 			for (i = 8; --i >= 0;) {
   1320 				if (((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01)) {
   1321 					crc <<= 1;
   1322 					crc ^= 0x04c11db6 | 1;
   1323 				} else
   1324 					crc <<= 1;
   1325 				c >>= 1;
   1326 			}
   1327 		}
   1328 		/* Just want the 6 most significant bits. */
   1329 		crc >>= 26;
   1330 
   1331 		/* Turn on the corresponding bit in the filter. */
   1332 		af[crc >> 3] |= 1 << (crc & 0x7);
   1333 
   1334 		ETHER_NEXT_MULTI(step, enm);
   1335 	}
   1336 	ifp->if_flags &= ~IFF_ALLMULTI;
   1337 }
   1338 /*
   1339  * Copy packet from mbuf to the board memory
   1340  *
   1341  * Currently uses an extra buffer/extra memory copy,
   1342  * unless the whole packet fits in one mbuf.
   1343  *
   1344  */
   1345 u_short
   1346 ae_put(sc, m, buf)
   1347 	struct ae_softc *sc;
   1348 	struct mbuf *m;
   1349 	caddr_t buf;
   1350 {
   1351 	u_char *data, savebyte[2];
   1352 	int     len, wantbyte;
   1353 	u_short totlen = 0;
   1354 
   1355 	wantbyte = 0;
   1356 
   1357 	for (; m ; m = m->m_next) {
   1358 		data = mtod(m, u_char *);
   1359 		len = m->m_len;
   1360 		totlen += len;
   1361 		if (len > 0) {
   1362 			/* Finish the last word. */
   1363 			if (wantbyte) {
   1364 				savebyte[1] = *data;
   1365 				word_copy(savebyte, buf, 2);
   1366 				buf += 2;
   1367 				data++;
   1368 				len--;
   1369 				wantbyte = 0;
   1370 			}
   1371 			/* Output contiguous words. */
   1372 			if (len > 1) {
   1373 				word_copy(data, buf, len);
   1374 				buf += len & ~1;
   1375 				data += len & ~1;
   1376 				len &= 1;
   1377 			}
   1378 			/* Save last byte, if necessary. */
   1379 			if (len == 1) {
   1380 				savebyte[0] = *data;
   1381 				wantbyte = 1;
   1382 			}
   1383 		}
   1384 	}
   1385 
   1386 	if (wantbyte) {
   1387 		savebyte[1] = 0;
   1388 		word_copy(savebyte, buf, 2);
   1389 	}
   1390 	return (totlen);
   1391 }
   1392