Home | History | Annotate | Line # | Download | only in dev
if_ae.c revision 1.12
      1 /*
      2  * Device driver for National Semiconductor DS8390 based ethernet adapters.
      3  *
      4  * Based on original ISA bus driver by David Greenman, 29-April-1993
      5  *
      6  * Copyright (C) 1993, David Greenman. This software may be used, modified,
      7  *   copied, distributed, and sold, in both source and binary form provided
      8  *   that the above copyright and these terms are retained. Under no
      9  *   circumstances is the author responsible for the proper functioning
     10  *   of this software, nor does the author assume any responsibility
     11  *   for damages incurred with its use.
     12  *
     13  * Adapted for MacBSD by Brad Parker <brad (at) fcr.com>
     14  *
     15  * Currently supports:
     16  *	Apples NB Ethernet card
     17  *	Interlan A310 Nubus Ethernet card
     18  *	Cayman Systems GatorCard
     19  */
     20 
     21 /*
     22  * $Id: if_ae.c,v 1.12 1994/03/20 03:03:26 lkestel Exp $
     23  */
     24 
     25 #include "ae.h"
     26 /* bpfilter included here in case it is needed in future net includes */
     27 #include "bpfilter.h"
     28 
     29 #include <sys/param.h>
     30 #include <sys/systm.h>
     31 #include <sys/errno.h>
     32 #include <sys/ioctl.h>
     33 #include <sys/mbuf.h>
     34 #include <sys/socket.h>
     35 #include <sys/syslog.h>
     36 
     37 #include <net/if.h>
     38 #include <net/if_dl.h>
     39 #include <net/if_types.h>
     40 #include <net/netisr.h>
     41 
     42 #ifdef INET
     43 #include <netinet/in.h>
     44 #include <netinet/in_systm.h>
     45 #include <netinet/in_var.h>
     46 #include <netinet/ip.h>
     47 #include <netinet/if_ether.h>
     48 #endif
     49 
     50 #ifdef NS
     51 #include <netns/ns.h>
     52 #include <netns/ns_if.h>
     53 #endif
     54 
     55 #if NBPFILTER > 0
     56 #include <net/bpf.h>
     57 #include <net/bpfdesc.h>
     58 #endif
     59 
     60 #include <sys/device.h>
     61 #include "nubus.h"
     62 #include "if_aereg.h"
     63 
     64 struct ae_device {
     65 	struct device	ae_dev;
     66 /*	struct nubusdev	ae_nu;
     67 	struct intrhand	ae_ih;	*/
     68 };
     69 
     70 /*
     71  * ae_softc: per line info and status
     72  */
     73 struct	ae_softc {
     74 	struct ae_device	*sc_ae;
     75 
     76 	struct	arpcom arpcom;	/* ethernet common */
     77 
     78 	char	*type_str;	/* pointer to type string */
     79 	u_char	vendor;		/* interface vendor */
     80 	u_char	type;		/* interface type code */
     81 #define	APPLE_CARD(sc)		((sc)->vendor == AE_VENDOR_APPLE)
     82 #define	REG_MAP(sc, reg)	(APPLE_CARD(sc) ? (0x0f-(reg))<<2 : (reg)<<2)
     83 #define NIC_GET(sc, reg)	((sc)->nic_addr[REG_MAP(sc, reg)])
     84 #define NIC_PUT(sc, reg, val)	((sc)->nic_addr[REG_MAP(sc, reg)] = (val))
     85 	volatile caddr_t nic_addr; /* NIC (DS8390) I/O bus address */
     86 	caddr_t	rom_addr;	/* on board prom address */
     87 	caddr_t	smem_start;	/* shared memory start address */
     88 	caddr_t	smem_end;	/* shared memory end address */
     89 	u_long	smem_size;	/* total shared memory size */
     90 	caddr_t	smem_ring;	/* start of RX ring-buffer (in smem) */
     91 
     92 	caddr_t	bpf;		/* BPF "magic cookie" */
     93 
     94 	u_char	xmit_busy;	/* transmitter is busy */
     95 	u_char	txb_cnt;	/* Number of transmit buffers */
     96 	u_char	txb_next;	/* Pointer to next buffer ready to xmit */
     97 	u_short	txb_next_len;	/* next xmit buffer length */
     98 	u_char	data_buffered;	/* data has been buffered in interface memory */
     99 	u_char	tx_page_start;	/* first page of TX buffer area */
    100 
    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 } ae_softc[NAE];
    105 
    106 void	ae_find(), ae_attach();
    107 int	ae_init(), aeintr(), ae_ioctl(), ae_probe(),
    108 	ae_start(), ae_reset(), ae_watchdog();
    109 
    110 struct cfdriver aecd =
    111 { NULL, "ae", ae_probe, ae_attach, DV_IFNET, sizeof(struct ae_device), NULL, 0 };
    112 
    113 static void ae_stop();
    114 static inline void ae_rint();
    115 static inline void ae_xmit();
    116 static inline char *ae_ring_copy();
    117 
    118 extern int ether_output();
    119 
    120 #define	ETHER_MIN_LEN	64
    121 #define ETHER_MAX_LEN	1518
    122 #define	ETHER_ADDR_LEN	6
    123 #define	ETHER_HDR_SIZE	14
    124 
    125 char ae_name[] = "8390 Nubus Ethernet card";
    126 static char zero = 0;
    127 static u_char ones = 0xff;
    128 
    129 struct vendor_S {
    130 	char	*manu;
    131 	int	len;
    132 	int	vendor;
    133 } vend[] = {
    134 	{ "Apple", 5, AE_VENDOR_APPLE },
    135 	{ "3Com",  4, AE_VENDOR_APPLE },
    136 	{ "Dayna", 5, AE_VENDOR_DAYNA },
    137 	{ "Inter", 5, AE_VENDOR_INTERLAN },
    138 };
    139 
    140 static int numvend = sizeof(vend)/sizeof(vend[0]);
    141 
    142 /*
    143  * XXX These two should be moved to locore, and maybe changed to use shorts
    144  * instead of bytes.  The reason for these is that bcopy and bzero use longs,
    145  * which the ethernet cards can't handle.
    146  */
    147 
    148 void
    149 bbzero (char *addr, int len)
    150 {
    151 	while (len--) {
    152 		*addr++ = 0;
    153 	}
    154 }
    155 
    156 void
    157 bbcopy (char *src, char *dest, int len)
    158 {
    159 	while (len--) {
    160 		*dest++ = *src++;
    161 	}
    162 }
    163 
    164 void
    165 ae_id_card(nu, sc)
    166 	struct nubus_hw	*nu;
    167 	struct ae_softc	*sc;
    168 {
    169 	int	i;
    170 
    171 	/*
    172 	 * Try to determine what type of card this is...
    173 	 */
    174 	sc->vendor = AE_VENDOR_UNKNOWN;
    175 	for (i=0 ; i<numvend ; i++) {
    176 		if (!strncmp(nu->Slot.manufacturer, vend[i].manu, vend[i].len)) {
    177 			sc->vendor = vend[i].vendor;
    178 			break;
    179 		}
    180 	}
    181 	sc->type_str = (char *) (nu->Slot.manufacturer);
    182 
    183 	/* see if it's an Interlan/GatorCard
    184 	sc->rom_addr = nu->addr + GC_ROM_OFFSET;
    185 	if (sc->rom_addr[0x18] == 0x0 &&
    186 	    sc->rom_addr[0x1c] == 0x55) {
    187 		sc->vendor = AE_VENDOR_INTERLAN;
    188 	} */
    189 }
    190 
    191 int
    192 ae_probe(parent, cf, aux)
    193 	struct cfdriver	*parent;
    194 	struct cfdata	*cf;
    195 	void		*aux;
    196 {
    197 	register struct nubus_hw *nu = (struct nubus_hw *) aux;
    198 	struct ae_softc *sc = &ae_softc[cf->cf_unit];
    199 	int i, memsize;
    200 	int flags = 0;
    201 
    202 	if (nu->Slot.type != NUBUS_NETWORK)
    203 		return 0;
    204 
    205 	ae_id_card(nu, sc);
    206 
    207 	switch (sc->vendor) {
    208 	      case AE_VENDOR_INTERLAN:
    209 		sc->nic_addr = nu->addr + GC_NIC_OFFSET;
    210 		sc->rom_addr = nu->addr + GC_ROM_OFFSET;
    211 		sc->smem_start = nu->addr + GC_DATA_OFFSET;
    212 		memsize = 8192;
    213 
    214 		/* reset the NIC chip */
    215 		*((caddr_t)nu->addr + GC_RESET_OFFSET) = (char)zero;
    216 
    217 		/* Get station address from on-board ROM */
    218 		for (i = 0; i < ETHER_ADDR_LEN; ++i)
    219 			sc->arpcom.ac_enaddr[i] = *(sc->rom_addr + i*4);
    220 		break;
    221 
    222 	      case AE_VENDOR_APPLE:
    223 		sc->nic_addr = nu->addr + AE_NIC_OFFSET;
    224 		sc->rom_addr = nu->addr + AE_ROM_OFFSET;
    225 		sc->smem_start = nu->addr + AE_DATA_OFFSET;
    226 		memsize = 8192;
    227 
    228 		/* Get station address from on-board ROM */
    229 		for (i = 0; i < ETHER_ADDR_LEN; ++i)
    230 			sc->arpcom.ac_enaddr[i] = *(sc->rom_addr + i*2);
    231 		break;
    232 
    233 	      case AE_VENDOR_DAYNA:
    234 		printf("We think we are a Dayna card, but ");
    235 		sc->nic_addr = nu->addr + DP_NIC_OFFSET;
    236 		sc->rom_addr = nu->addr + DP_ROM_OFFSET;
    237 		sc->smem_start = nu->addr + DP_DATA_OFFSET;
    238 		memsize = 8192;
    239 
    240 		/* Get station address from on-board ROM */
    241 		for (i = 0; i < ETHER_ADDR_LEN; ++i)
    242 			sc->arpcom.ac_enaddr[i] = *(sc->rom_addr + i*2);
    243 		printf("it is dangerous to continue.\n");
    244 		return 0; /* Since we don't work yet... */
    245 		break;
    246 
    247 	      default:
    248 		return 0;
    249 		break;
    250 	}
    251 
    252 	/*
    253 	 * allocate one xmit buffer if < 16k, two buffers otherwise
    254 	 */
    255 	if ((memsize < 16384) || (flags & AE_FLAGS_NO_DOUBLE_BUFFERING)) {
    256 		sc->smem_ring = sc->smem_start + (AE_PAGE_SIZE * AE_TXBUF_SIZE);
    257 		sc->txb_cnt = 1;
    258 		sc->rec_page_start = AE_TXBUF_SIZE;
    259 	} else {
    260 		sc->smem_ring = sc->smem_start + (AE_PAGE_SIZE * AE_TXBUF_SIZE * 2);
    261 		sc->txb_cnt = 2;
    262 		sc->rec_page_start = AE_TXBUF_SIZE * 2;
    263 	}
    264 
    265 	sc->smem_size = memsize;
    266 	sc->smem_end = sc->smem_start + memsize;
    267 	sc->rec_page_stop = memsize / AE_PAGE_SIZE;
    268 	sc->tx_page_start = 0;
    269 
    270 	/*
    271 	 * Now zero memory and verify that it is clear
    272 	 */
    273 	bbzero(sc->smem_start, memsize);
    274 
    275 	for (i = 0; i < memsize; ++i)
    276 		if (sc->smem_start[i]) {
    277 	        	printf(": failed to clear shared memory at %x\n",
    278 			       sc->smem_start + i);
    279 
    280 			return(0);
    281 		}
    282 
    283 #ifdef DEBUG_PRINT
    284 	printf("nic_addr %x, rom_addr %x\n",
    285 		sc->nic_addr, sc->rom_addr);
    286 	printf("smem_size %d\n", sc->smem_size);
    287 	printf("smem_start %x, smem_ring %x, smem_end %x\n",
    288 		sc->smem_start, sc->smem_ring, sc->smem_end);
    289 	printf("phys address %02x:%02x:%02x:%02x:%02x:%02x\n",
    290 		sc->arpcom.ac_enaddr[0],
    291 		sc->arpcom.ac_enaddr[1],
    292 		sc->arpcom.ac_enaddr[2],
    293 		sc->arpcom.ac_enaddr[3],
    294 		sc->arpcom.ac_enaddr[4],
    295 		sc->arpcom.ac_enaddr[5]);
    296 #endif
    297 
    298 	return(1);
    299 }
    300 
    301 /*
    302  * Install interface into kernel networking data structures
    303  */
    304 void
    305 ae_attach(parent, self, aux)
    306 	struct cfdriver	*parent, *self;
    307 	void		*aux;
    308 {
    309 	struct nubus_hw	*nu = aux;
    310 	struct ae_device *ae = (struct ae_device *) self;
    311 	struct ae_softc *sc = &ae_softc[ae->ae_dev.dv_unit];
    312 	struct cfdata *cf = ae->ae_dev.dv_cfdata;
    313 	struct ifnet *ifp = &sc->arpcom.ac_if;
    314 	struct ifaddr *ifa;
    315 	struct sockaddr_dl *sdl;
    316 
    317 	sc->sc_ae = ae;
    318 
    319 	/*
    320 	 * Set interface to stopped condition (reset)
    321 	 */
    322 	ae_stop(sc);
    323 
    324 	/*
    325 	 * Initialize ifnet structure
    326 	 */
    327 	ifp->if_unit = ae->ae_dev.dv_unit;
    328 	ifp->if_name = aecd.cd_name;
    329 	ifp->if_mtu = ETHERMTU;
    330 	ifp->if_output = ether_output;
    331 	ifp->if_start = ae_start;
    332 	ifp->if_ioctl = ae_ioctl;
    333 	ifp->if_reset = ae_reset;
    334 	ifp->if_watchdog = ae_watchdog;
    335 	ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS);
    336 
    337 #if 0
    338 	/*
    339 	 * Set default state for ALTPHYS flag (used to disable the transceiver
    340 	 * for AUI operation), based on compile-time config option.
    341 	 */
    342 	if (cf->cf_flags & AE_FLAGS_DISABLE_TRANSCEIVER)
    343 		ifp->if_flags |= IFF_ALTPHYS;
    344 #endif
    345 
    346 	/*
    347 	 * Attach the interface
    348 	 */
    349 	if_attach(ifp);
    350 
    351 	/*
    352 	 * Search down the ifa address list looking for the AF_LINK type entry
    353 	 */
    354  	ifa = ifp->if_addrlist;
    355 	while ((ifa != 0) && (ifa->ifa_addr != 0) &&
    356 	    (ifa->ifa_addr->sa_family != AF_LINK))
    357 		ifa = ifa->ifa_next;
    358 	/*
    359 	 * If we find an AF_LINK type entry we fill in the hardware address.
    360 	 *	This is useful for netstat(1) to keep track of which interface
    361 	 *	is which.
    362 	 */
    363 	if ((ifa != 0) && (ifa->ifa_addr != 0)) {
    364 		/*
    365 		 * Fill in the link-level address for this interface
    366 		 */
    367 		sdl = (struct sockaddr_dl *)ifa->ifa_addr;
    368 		sdl->sdl_type = IFT_ETHER;
    369 		sdl->sdl_alen = ETHER_ADDR_LEN;
    370 		sdl->sdl_slen = 0;
    371 		bbcopy(sc->arpcom.ac_enaddr, LLADDR(sdl), ETHER_ADDR_LEN);
    372 	}
    373 
    374 	/*
    375 	 * Print additional info when attached
    376 	 */
    377 	printf(": address %s, ", ether_sprintf(sc->arpcom.ac_enaddr));
    378 
    379 	if (sc->type_str && (*sc->type_str != 0))
    380 		printf("type %s ", sc->type_str);
    381 	else
    382 		printf("type unknown (0x%x) ", sc->type);
    383 
    384 	printf("\n");
    385 
    386 	/*
    387 	 * If BPF is in the kernel, call the attach for it
    388 	 */
    389 #if NBPFILTER > 0
    390 	bpfattach(&sc->bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    391 #endif
    392 }
    393 
    394 /*
    395  * Reset interface.
    396  */
    397 int
    398 ae_reset(sc)
    399 	struct ae_softc *sc;
    400 {
    401 	int s;
    402 
    403 	s = splnet();
    404 
    405 	/*
    406 	 * Stop interface and re-initialize.
    407 	 */
    408 	ae_stop(sc);
    409 	ae_init(sc);
    410 
    411 	(void) splx(s);
    412 }
    413 
    414 /*
    415  * Take interface offline.
    416  */
    417 void
    418 ae_stop(sc)
    419 	struct ae_softc *sc;
    420 {
    421 	int n = 5000;
    422 
    423 	/*
    424 	 * Stop everything on the interface, and select page 0 registers.
    425 	 */
    426 	NIC_PUT(sc, AE_P0_CR, AE_CR_RD2|AE_CR_STP);
    427 
    428 	/*
    429 	 * Wait for interface to enter stopped state, but limit # of checks
    430 	 *	to 'n' (about 5ms). It shouldn't even take 5us on modern
    431 	 *	DS8390's, but just in case it's an old one.
    432 	 */
    433 	while (((NIC_GET(sc, AE_P0_ISR) & AE_ISR_RST) == 0) && --n);
    434 }
    435 
    436 /*
    437  * Device timeout/watchdog routine. Entered if the device neglects to
    438  *	generate an interrupt after a transmit has been started on it.
    439  */
    440 int
    441 ae_watchdog(unit)
    442 	short unit;
    443 {
    444 	log(LOG_ERR, "ae%d: device timeout\n", unit);
    445 {
    446 struct ae_softc *sc = &ae_softc[unit];
    447 printf("cr %x, isr %x\n", NIC_GET(sc, AE_P0_CR), NIC_GET(sc, AE_P0_ISR));
    448 /* via_dump(); */
    449 if (NIC_GET(sc, AE_P0_ISR)) {
    450 	aeintr(0);
    451 	return;
    452 }
    453 }
    454 	ae_reset(unit);
    455 }
    456 
    457 /*
    458  * Initialize device.
    459  */
    460 ae_init(sc)
    461 	struct ae_softc *sc;
    462 {
    463 	struct ifnet *ifp = &sc->arpcom.ac_if;
    464 	int i, s;
    465 	u_char	command;
    466 
    467 
    468 	/* address not known */
    469 	if (ifp->if_addrlist == (struct ifaddr *)0) return;
    470 
    471 	/*
    472 	 * Initialize the NIC in the exact order outlined in the NS manual.
    473 	 *	This init procedure is "mandatory"...don't change what or when
    474 	 *	things happen.
    475 	 */
    476 	s = splnet();
    477 
    478 	/* reset transmitter flags */
    479 	sc->data_buffered = 0;
    480 	sc->xmit_busy = 0;
    481 	sc->arpcom.ac_if.if_timer = 0;
    482 
    483 	sc->txb_next = 0;
    484 
    485 	/* This variable is used below - don't move this assignment */
    486 	sc->next_packet = sc->rec_page_start + 1;
    487 
    488 #ifdef DEBUG_PRINT
    489 	printf("page_start %d, page_stop %d, next %d\n",
    490 		sc->rec_page_start, sc->rec_page_stop, sc->next_packet);
    491 #endif
    492 
    493 	/*
    494 	 * Set interface for page 0, Remote DMA complete, Stopped
    495 	 */
    496 	NIC_PUT(sc, AE_P0_CR, AE_CR_RD2|AE_CR_STP);
    497 
    498 	/*
    499 	 * Set FIFO threshold to 4, No auto-init Remote DMA, Burst mode,
    500 	 *	byte order=80x86, word-wide DMA xfers,
    501 	 */
    502 	NIC_PUT(sc, AE_P0_DCR, AE_DCR_FT1|AE_DCR_BMS|AE_DCR_WTS);
    503 
    504 	/*
    505 	 * Clear Remote Byte Count Registers
    506 	 */
    507 	NIC_PUT(sc, AE_P0_RBCR0, zero);
    508 	NIC_PUT(sc, AE_P0_RBCR1, zero);
    509 
    510 	/*
    511 	 * Enable reception of broadcast packets
    512 	 */
    513 	NIC_PUT(sc, AE_P0_RCR, AE_RCR_AB);
    514 
    515 	/*
    516 	 * Place NIC in internal loopback mode
    517 	 */
    518 	NIC_PUT(sc, AE_P0_TCR, AE_TCR_LB0);
    519 
    520 	/*
    521 	 * Initialize transmit/receive (ring-buffer) Page Start
    522 	 */
    523 	NIC_PUT(sc, AE_P0_TPSR, sc->tx_page_start);
    524 	NIC_PUT(sc, AE_P0_PSTART, sc->rec_page_start);
    525 
    526 	/*
    527 	 * Initialize Receiver (ring-buffer) Page Stop and Boundry
    528 	 */
    529 	NIC_PUT(sc, AE_P0_PSTOP, sc->rec_page_stop);
    530 	NIC_PUT(sc, AE_P0_BNRY, sc->rec_page_start);
    531 
    532 	/*
    533 	 * Clear all interrupts. A '1' in each bit position clears the
    534 	 *	corresponding flag.
    535 	 */
    536 	NIC_PUT(sc, AE_P0_ISR, ones);
    537 
    538 	/*
    539 	 * Enable the following interrupts: receive/transmit complete,
    540 	 *	receive/transmit error, and Receiver OverWrite.
    541 	 *
    542 	 * Counter overflow and Remote DMA complete are *not* enabled.
    543 	 */
    544 	NIC_PUT(sc, AE_P0_IMR,
    545 		AE_IMR_PRXE|AE_IMR_PTXE|AE_IMR_RXEE|AE_IMR_TXEE|AE_IMR_OVWE);
    546 
    547 	/*
    548 	 * Program Command Register for page 1
    549 	 */
    550 	NIC_PUT(sc, AE_P0_CR, AE_CR_PAGE_1|AE_CR_RD2|AE_CR_STP);
    551 
    552 	/*
    553 	 * Copy out our station address
    554 	 */
    555 	for (i = 0; i < ETHER_ADDR_LEN; ++i)
    556 		NIC_PUT(sc, AE_P1_PAR0 + i, sc->arpcom.ac_enaddr[i]);
    557 
    558 #if NBPFILTER > 0
    559 	/*
    560 	 * Initialize multicast address hashing registers to accept
    561 	 *	 all multicasts (only used when in promiscuous mode)
    562 	 */
    563 	for (i = 0; i < 8; ++i)
    564 		NIC_PUT(sc, AE_P1_MAR0 + i, 0xff);
    565 #endif
    566 
    567 	/*
    568 	 * Set Current Page pointer to next_packet (initialized above)
    569 	 */
    570 	NIC_PUT(sc, AE_P1_CURR, sc->next_packet);
    571 
    572 	/*
    573 	 * Set Command Register for page 0, Remote DMA complete,
    574 	 * 	and interface Start.
    575 	 */
    576 	NIC_PUT(sc, AE_P1_CR, AE_CR_RD2|AE_CR_STA);
    577 
    578 	/*
    579 	 * Take interface out of loopback
    580 	 */
    581 	NIC_PUT(sc, AE_P0_TCR, zero);
    582 
    583 	/*
    584 	 * Set 'running' flag, and clear output active flag.
    585 	 */
    586 	ifp->if_flags |= IFF_RUNNING;
    587 	ifp->if_flags &= ~IFF_OACTIVE;
    588 
    589 	/* XXXXXX */
    590 	add_nubus_intr((int)sc->rom_addr & 0xFF000000, aeintr, sc - ae_softc);
    591 
    592 	/*
    593 	 * ...and attempt to start output
    594 	 */
    595 	ae_start(ifp);
    596 
    597 	(void) splx(s);
    598 }
    599 
    600 /*
    601  * This routine actually starts the transmission on the interface
    602  */
    603 static inline void ae_xmit(ifp)
    604 	struct ifnet *ifp;
    605 {
    606 	struct ae_softc *sc = &ae_softc[ifp->if_unit];
    607 	u_short len = sc->txb_next_len;
    608 
    609 	/*
    610 	 * Set NIC for page 0 register access
    611 	 */
    612 	NIC_PUT(sc, AE_P0_CR, AE_CR_RD2|AE_CR_STA);
    613 
    614 	/*
    615 	 * Set TX buffer start page
    616 	 */
    617 	NIC_PUT(sc, AE_P0_TPSR, sc->tx_page_start +
    618 		sc->txb_next * AE_TXBUF_SIZE);
    619 
    620 	/*
    621 	 * Set TX length
    622 	 */
    623 	NIC_PUT(sc, AE_P0_TBCR0, len & 0xff);
    624 	NIC_PUT(sc, AE_P0_TBCR1, len >> 8);
    625 
    626 	/*
    627 	 * Set page 0, Remote DMA complete, Transmit Packet, and *Start*
    628 	 */
    629 	NIC_PUT(sc, AE_P0_CR, AE_CR_RD2|AE_CR_TXP|AE_CR_STA);
    630 
    631 	sc->xmit_busy = 1;
    632 	sc->data_buffered = 0;
    633 
    634 	/*
    635 	 * Switch buffers if we are doing double-buffered transmits
    636 	 */
    637 	if ((sc->txb_next == 0) && (sc->txb_cnt > 1))
    638 		sc->txb_next = 1;
    639 	else
    640 		sc->txb_next = 0;
    641 
    642 	/*
    643 	 * Set a timer just in case we never hear from the board again
    644 	 */
    645 	ifp->if_timer = 2;
    646 }
    647 
    648 /*
    649  * Start output on interface.
    650  * We make two assumptions here:
    651  *  1) that the current priority is set to splnet _before_ this code
    652  *     is called *and* is returned to the appropriate priority after
    653  *     return
    654  *  2) that the IFF_OACTIVE flag is checked before this code is called
    655  *     (i.e. that the output part of the interface is idle)
    656  */
    657 int
    658 ae_start(ifp)
    659 	struct ifnet *ifp;
    660 {
    661 	struct ae_softc *sc = &ae_softc[ifp->if_unit];
    662 	struct mbuf *m0, *m;
    663 	caddr_t buffer;
    664 	int len;
    665 
    666 outloop:
    667 	/*
    668 	 * See if there is room to send more data (i.e. one or both of the
    669 	 *	buffers is empty).
    670 	 */
    671 	if (sc->data_buffered)
    672 		if (sc->xmit_busy) {
    673 			/*
    674 			 * No room. Indicate this to the outside world
    675 			 *	and exit.
    676 			 */
    677 			ifp->if_flags |= IFF_OACTIVE;
    678 			return;
    679 		} else {
    680 			/*
    681 			 * Data is buffered, but we're not transmitting, so
    682 			 *	start the xmit on the buffered data.
    683 			 * Note that ae_xmit() resets the data_buffered flag
    684 			 *	before returning.
    685 			 */
    686 			ae_xmit(ifp);
    687 		}
    688 
    689 	IF_DEQUEUE(&sc->arpcom.ac_if.if_snd, m);
    690 	if (m == 0) {
    691 	/*
    692 	 * The following isn't pretty; we are using the !OACTIVE flag to
    693 	 * indicate to the outside world that we can accept an additional
    694 	 * packet rather than that the transmitter is _actually_
    695 	 * active. Indeed, the transmitter may be active, but if we haven't
    696 	 * filled the secondary buffer with data then we still want to
    697 	 * accept more.
    698 	 * Note that it isn't necessary to test the data_buffered flag -
    699 	 * we wouldn't have tried to de-queue the packet in the first place
    700 	 * if it was set.
    701 	 */
    702 		ifp->if_flags &= ~IFF_OACTIVE;
    703 		return;
    704 	}
    705 
    706 	/*
    707 	 * Copy the mbuf chain into the transmit buffer
    708 	 */
    709 	buffer = sc->smem_start + (sc->txb_next * AE_TXBUF_SIZE * AE_PAGE_SIZE);
    710 	len = 0;
    711 	for (m0 = m; m != 0; m = m->m_next) {
    712 		/*printf("ae: copy %d bytes @ %x\n", m->m_len, buffer);*/
    713 		bbcopy(mtod(m, caddr_t), buffer, m->m_len);
    714 		buffer += m->m_len;
    715        		len += m->m_len;
    716 	}
    717 if (len & 1) len++;
    718 
    719 	sc->txb_next_len = MAX(len, ETHER_MIN_LEN);
    720 
    721 	if (sc->txb_cnt > 1)
    722 		/*
    723 		 * only set 'buffered' flag if doing multiple buffers
    724 		 */
    725 		sc->data_buffered = 1;
    726 
    727 	if (sc->xmit_busy == 0)
    728 		ae_xmit(ifp);
    729 	/*
    730 	 * If there is BPF support in the configuration, tap off here.
    731 	 *   The following has support for converting trailer packets
    732 	 *   back to normal.
    733 	 */
    734 #if NBPFILTER > 0
    735 	if (sc->bpf) {
    736 		u_short etype;
    737 		int off, datasize, resid;
    738 		struct ether_header *eh;
    739 		struct trailer_header {
    740 			u_short ether_type;
    741 			u_short ether_residual;
    742 		} trailer_header;
    743 		char ether_packet[ETHER_MAX_LEN];
    744 		char *ep;
    745 
    746 		ep = ether_packet;
    747 
    748 		/*
    749 		 * We handle trailers below:
    750 		 * Copy ether header first, then residual data,
    751 		 * then data. Put all this in a temporary buffer
    752 		 * 'ether_packet' and send off to bpf. Since the
    753 		 * system has generated this packet, we assume
    754 		 * that all of the offsets in the packet are
    755 		 * correct; if they're not, the system will almost
    756 		 * certainly crash in m_copydata.
    757 		 * We make no assumptions about how the data is
    758 		 * arranged in the mbuf chain (i.e. how much
    759 		 * data is in each mbuf, if mbuf clusters are
    760 		 * used, etc.), which is why we use m_copydata
    761 		 * to get the ether header rather than assume
    762 		 * that this is located in the first mbuf.
    763 		 */
    764 		/* copy ether header */
    765 		m_copydata(m0, 0, sizeof(struct ether_header), ep);
    766 		eh = (struct ether_header *) ep;
    767 		ep += sizeof(struct ether_header);
    768 		etype = ntohs(eh->ether_type);
    769 		if (etype >= ETHERTYPE_TRAIL &&
    770 		    etype < ETHERTYPE_TRAIL+ETHERTYPE_NTRAILER) {
    771 			datasize = ((etype - ETHERTYPE_TRAIL) << 9);
    772 			off = datasize + sizeof(struct ether_header);
    773 
    774 			/* copy trailer_header into a data structure */
    775 			m_copydata(m0, off, sizeof(struct trailer_header),
    776 				&trailer_header.ether_type);
    777 
    778 			/* copy residual data */
    779 			m_copydata(m0, off+sizeof(struct trailer_header),
    780 				resid = ntohs(trailer_header.ether_residual) -
    781 				sizeof(struct trailer_header), ep);
    782 			ep += resid;
    783 
    784 			/* copy data */
    785 			m_copydata(m0, sizeof(struct ether_header),
    786 				datasize, ep);
    787 			ep += datasize;
    788 
    789 			/* restore original ether packet type */
    790 			eh->ether_type = trailer_header.ether_type;
    791 
    792 			bpf_tap(sc->bpf, ether_packet, ep - ether_packet);
    793 		} else
    794 			bpf_mtap(sc->bpf, m0);
    795 	}
    796 #endif
    797 
    798 	m_freem(m0);
    799 
    800 	/*
    801 	 * If we are doing double-buffering, a buffer might be free to
    802 	 *	fill with another packet, so loop back to the top.
    803 	 */
    804 	if (sc->txb_cnt > 1)
    805 		goto outloop;
    806 	else {
    807 		ifp->if_flags |= IFF_OACTIVE;
    808 		return;
    809 	}
    810 }
    811 
    812 /*
    813  * Ethernet interface receiver interrupt.
    814  */
    815 static inline void
    816 ae_rint(unit)
    817 	int unit;
    818 {
    819 	register struct ae_softc *sc = &ae_softc[unit];
    820 	u_char boundry, current;
    821 	u_short len;
    822 	struct ae_ring *packet_ptr;
    823 
    824 	/*
    825 	 * Set NIC to page 1 registers to get 'current' pointer
    826 	 */
    827 	NIC_PUT(sc, AE_P0_CR, AE_CR_PAGE_1|AE_CR_RD2|AE_CR_STA);
    828 
    829 	/*
    830 	 * 'sc->next_packet' is the logical beginning of the ring-buffer - i.e.
    831 	 *	it points to where new data has been buffered. The 'CURR'
    832 	 *	(current) register points to the logical end of the ring-buffer
    833 	 *	- i.e. it points to where additional new data will be added.
    834 	 *	We loop here until the logical beginning equals the logical
    835 	 *	end (or in other words, until the ring-buffer is empty).
    836 	 */
    837 	while (sc->next_packet != NIC_GET(sc, AE_P1_CURR)) {
    838 
    839 		/* get pointer to this buffer header structure */
    840 		packet_ptr = (struct ae_ring *)(sc->smem_ring +
    841 			 (sc->next_packet - sc->rec_page_start) * AE_PAGE_SIZE);
    842 
    843 		/*
    844 		 * The byte count includes the FCS - Frame Check Sequence (a
    845 		 *	32 bit CRC).
    846 		 */
    847 		len = packet_ptr->count[0] | (packet_ptr->count[1] << 8);
    848 		if ((len >= ETHER_MIN_LEN) && (len <= ETHER_MAX_LEN)) {
    849 			/*
    850 			 * Go get packet. len - 4 removes CRC from length.
    851 			 * (packet_ptr + 1) points to data just after the packet ring
    852 			 *	header (+4 bytes)
    853 			 */
    854 			ae_get_packet(sc, (caddr_t)(packet_ptr + 1), len - 4);
    855 			++sc->arpcom.ac_if.if_ipackets;
    856 		} else {
    857 			/*
    858 			 * Really BAD...probably indicates that the ring pointers
    859 			 *	are corrupted. Also seen on early rev chips under
    860 			 *	high load - the byte order of the length gets switched.
    861 			 */
    862 			log(LOG_ERR,
    863 				"ae%d: shared memory corrupt - invalid packet length %d\n",
    864 				unit, len);
    865 			ae_reset(unit);
    866 			return;
    867 		}
    868 
    869 		/*
    870 		 * Update next packet pointer
    871 		 */
    872 		sc->next_packet = packet_ptr->next_packet;
    873 
    874 		/*
    875 		 * Update NIC boundry pointer - being careful to keep it
    876 		 *	one buffer behind. (as recommended by NS databook)
    877 		 */
    878 		boundry = sc->next_packet - 1;
    879 		if (boundry < sc->rec_page_start)
    880 			boundry = sc->rec_page_stop - 1;
    881 
    882 		/*
    883 		 * Set NIC to page 0 registers to update boundry register
    884 		 */
    885 		NIC_PUT(sc, AE_P0_CR, AE_CR_RD2|AE_CR_STA);
    886 
    887 		NIC_PUT(sc, AE_P0_BNRY, boundry);
    888 
    889 		/*
    890 		 * Set NIC to page 1 registers before looping to top (prepare to
    891 		 *	get 'CURR' current pointer)
    892 		 */
    893 		NIC_PUT(sc, AE_P0_CR, AE_CR_PAGE_1|AE_CR_RD2|AE_CR_STA);
    894 	}
    895 }
    896 
    897 /*
    898  * Ethernet interface interrupt processor
    899  */
    900 int
    901 aeintr(unit)
    902 	int unit;
    903 {
    904 	struct ae_softc *sc = &ae_softc[unit];
    905 	u_char isr;
    906 
    907 	/*
    908 	 * Set NIC to page 0 registers
    909 	 */
    910 	NIC_PUT(sc, AE_P0_CR, AE_CR_RD2|AE_CR_STA);
    911 
    912 	/*
    913 	 * loop until there are no more new interrupts
    914 	 */
    915 	while (isr = NIC_GET(sc, AE_P0_ISR)) {
    916 
    917 		/*
    918 		 * reset all the bits that we are 'acknowledging'
    919 		 *	by writing a '1' to each bit position that was set
    920 		 * (writing a '1' *clears* the bit)
    921 		 */
    922 		NIC_PUT(sc, AE_P0_ISR, isr);
    923 
    924 		/*
    925 		 * Handle transmitter interrupts. Handle these first
    926 		 *	because the receiver will reset the board under
    927 		 *	some conditions.
    928 		 */
    929 		if (isr & (AE_ISR_PTX|AE_ISR_TXE)) {
    930 			u_char collisions = NIC_GET(sc, AE_P0_NCR);
    931 
    932 			/*
    933 			 * Check for transmit error. If a TX completed with an
    934 			 * error, we end up throwing the packet away. Really
    935 			 * the only error that is possible is excessive
    936 			 * collisions, and in this case it is best to allow the
    937 			 * automatic mechanisms of TCP to backoff the flow. Of
    938 			 * course, with UDP we're screwed, but this is expected
    939 			 * when a network is heavily loaded.
    940 			 */
    941 			if (isr & AE_ISR_TXE) {
    942 
    943 				/*
    944 				 * Excessive collisions (16)
    945 				 */
    946 				if ((NIC_GET(sc, AE_P0_TSR) & AE_TSR_ABT)
    947 					&& (collisions == 0)) {
    948 					/*
    949 					 *    When collisions total 16, the
    950 					 * P0_NCR will indicate 0, and the
    951 					 * TSR_ABT is set.
    952 					 */
    953 					collisions = 16;
    954 				}
    955 
    956 				/*
    957 				 * update output errors counter
    958 				 */
    959 				++sc->arpcom.ac_if.if_oerrors;
    960 			} else {
    961 				/*
    962 				 * Update total number of successfully
    963 				 * 	transmitted packets.
    964 				 */
    965 				++sc->arpcom.ac_if.if_opackets;
    966 			}
    967 
    968 			/*
    969 			 * reset tx busy and output active flags
    970 			 */
    971 			sc->xmit_busy = 0;
    972 			sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
    973 
    974 			/*
    975 			 * clear watchdog timer
    976 			 */
    977 			sc->arpcom.ac_if.if_timer = 0;
    978 
    979 			/*
    980 			 * Add in total number of collisions on last
    981 			 *	transmission.
    982 			 */
    983 			sc->arpcom.ac_if.if_collisions += collisions;
    984 
    985 			/*
    986 			 * If data is ready to transmit, start it transmitting,
    987 			 *	otherwise defer until after handling receiver
    988 			 */
    989 			if (sc->data_buffered)
    990 				ae_xmit(&sc->arpcom.ac_if);
    991 		}
    992 
    993 		/*
    994 		 * Handle receiver interrupts
    995 		 */
    996 		if (isr & (AE_ISR_PRX|AE_ISR_RXE|AE_ISR_OVW)) {
    997 		    /*
    998 		     * Overwrite warning. In order to make sure that a lockup
    999 		     *	of the local DMA hasn't occurred, we reset and
   1000 		     *	re-init the NIC. The NSC manual suggests only a
   1001 		     *	partial reset/re-init is necessary - but some
   1002 		     *	chips seem to want more. The DMA lockup has been
   1003 		     *	seen only with early rev chips - Methinks this
   1004 		     *	bug was fixed in later revs. -DG
   1005 		     */
   1006 			if (isr & AE_ISR_OVW) {
   1007 				++sc->arpcom.ac_if.if_ierrors;
   1008 				log(LOG_WARNING,
   1009 					"ae%d: warning - receiver ring buffer overrun\n",
   1010 					unit);
   1011 				/*
   1012 				 * Stop/reset/re-init NIC
   1013 				 */
   1014 				ae_reset(unit);
   1015 			} else {
   1016 
   1017 			    /*
   1018 			     * Receiver Error. One or more of: CRC error, frame
   1019 			     *	alignment error FIFO overrun, or missed packet.
   1020 			     */
   1021 				if (isr & AE_ISR_RXE) {
   1022 					++sc->arpcom.ac_if.if_ierrors;
   1023 #ifdef AE_DEBUG
   1024 					printf("ae%d: receive error %x\n", unit,
   1025 						NIC_GET(sc, AE_P0_RSR));
   1026 #endif
   1027 				}
   1028 
   1029 				/*
   1030 				 * Go get the packet(s)
   1031 				 * XXX - Doing this on an error is dubious
   1032 				 *    because there shouldn't be any data to
   1033 				 *    get (we've configured the interface to
   1034 				 *    not accept packets with errors).
   1035 				 */
   1036 				ae_rint (unit);
   1037 			}
   1038 		}
   1039 
   1040 		/*
   1041 		 * If it looks like the transmitter can take more data,
   1042 		 * 	attempt to start output on the interface.
   1043 		 *	This is done after handling the receiver to
   1044 		 *	give the receiver priority.
   1045 		 */
   1046 		if ((sc->arpcom.ac_if.if_flags & IFF_OACTIVE) == 0)
   1047 			ae_start(&sc->arpcom.ac_if);
   1048 
   1049 		/*
   1050 		 * return NIC CR to standard state: page 0, remote DMA complete,
   1051 		 * 	start (toggling the TXP bit off, even if was just set
   1052 		 *	in the transmit routine, is *okay* - it is 'edge'
   1053 		 *	triggered from low to high)
   1054 		 */
   1055 		NIC_PUT(sc, AE_P0_CR, AE_CR_RD2|AE_CR_STA);
   1056 
   1057 		/*
   1058 		 * If the Network Talley Counters overflow, read them to
   1059 		 *	reset them. It appears that old 8390's won't
   1060 		 *	clear the ISR flag otherwise - resulting in an
   1061 		 *	infinite loop.
   1062 		 */
   1063 		if (isr & AE_ISR_CNT) {
   1064 			(void) NIC_GET(sc, AE_P0_CNTR0);
   1065 			(void) NIC_GET(sc, AE_P0_CNTR1);
   1066 			(void) NIC_GET(sc, AE_P0_CNTR2);
   1067 		}
   1068 	}
   1069 }
   1070 
   1071 /*
   1072  * Process an ioctl request. This code needs some work - it looks
   1073  *	pretty ugly.
   1074  */
   1075 int
   1076 ae_ioctl(ifp, command, data)
   1077 	register struct ifnet *ifp;
   1078 	int command;
   1079 	caddr_t data;
   1080 {
   1081 	register struct ifaddr *ifa = (struct ifaddr *)data;
   1082 	struct ae_softc *sc = &ae_softc[ifp->if_unit];
   1083 	struct ifreq *ifr = (struct ifreq *)data;
   1084 	int s, error = 0;
   1085 
   1086 	s = splnet();
   1087 
   1088 	switch (command) {
   1089 
   1090 	case SIOCSIFADDR:
   1091 		ifp->if_flags |= IFF_UP;
   1092 
   1093 		switch (ifa->ifa_addr->sa_family) {
   1094 #ifdef INET
   1095 		case AF_INET:
   1096 			ae_init(sc);	/* before arpwhohas */
   1097 			/*
   1098 			 * See if another station has *our* IP address.
   1099 			 * i.e.: There is an address conflict! If a
   1100 			 * conflict exists, a message is sent to the
   1101 			 * console.
   1102 			 */
   1103 			((struct arpcom *)ifp)->ac_ipaddr =
   1104 				IA_SIN(ifa)->sin_addr;
   1105 			arpwhohas((struct arpcom *)ifp, &IA_SIN(ifa)->sin_addr);
   1106 			break;
   1107 #endif
   1108 #ifdef NS
   1109 		/*
   1110 		 * XXX - This code is probably wrong
   1111 		 */
   1112 		case AF_NS:
   1113 		    {
   1114 			register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
   1115 
   1116 			if (ns_nullhost(*ina))
   1117 				ina->x_host =
   1118 					*(union ns_host *)(sc->arpcom.ac_enaddr);
   1119 			else {
   1120 				/*
   1121 				 *
   1122 				 */
   1123 				bbcopy((caddr_t)ina->x_host.c_host,
   1124 				    (caddr_t)sc->arpcom.ac_enaddr,
   1125 					sizeof(sc->arpcom.ac_enaddr));
   1126 			}
   1127 			/*
   1128 			 * Set new address
   1129 			 */
   1130 			ae_init(sc);
   1131 			break;
   1132 		    }
   1133 #endif
   1134 		default:
   1135 			ae_init(sc);
   1136 			break;
   1137 		}
   1138 		break;
   1139 
   1140 	case SIOCSIFFLAGS:
   1141 		/*
   1142 		 * If interface is marked down and it is running, then stop it
   1143 		 */
   1144 		if (((ifp->if_flags & IFF_UP) == 0) &&
   1145 		    (ifp->if_flags & IFF_RUNNING)) {
   1146 			ae_stop(ifp->if_unit);
   1147 			ifp->if_flags &= ~IFF_RUNNING;
   1148 		} else {
   1149 		/*
   1150 		 * If interface is marked up and it is stopped, then start it
   1151 		 */
   1152 			if ((ifp->if_flags & IFF_UP) &&
   1153 		    	    ((ifp->if_flags & IFF_RUNNING) == 0))
   1154 				ae_init(sc);
   1155 		}
   1156 #if NBPFILTER > 0
   1157 		if (ifp->if_flags & IFF_PROMISC) {
   1158 			/*
   1159 			 * Set promiscuous mode on interface.
   1160 			 *	XXX - for multicasts to work, we would need to
   1161 			 *		write 1's in all bits of multicast
   1162 			 *		hashing array. For now we assume that
   1163 			 *		this was done in ae_init().
   1164 			 */
   1165 			NIC_PUT(sc, AE_P0_RCR,
   1166 				AE_RCR_PRO|AE_RCR_AM|AE_RCR_AB);
   1167 		} else {
   1168 			/*
   1169 			 * XXX - for multicasts to work, we would need to
   1170 			 *	rewrite the multicast hashing array with the
   1171 			 *	proper hash (would have been destroyed above).
   1172 			 */
   1173 			NIC_PUT(sc, AE_P0_RCR, AE_RCR_AB);
   1174 		}
   1175 #endif
   1176 		break;
   1177 
   1178 	default:
   1179 		error = EINVAL;
   1180 	}
   1181 	(void) splx(s);
   1182 	return (error);
   1183 }
   1184 
   1185 /*
   1186  * Macro to calculate a new address within shared memory when given an offset
   1187  *	from an address, taking into account ring-wrap.
   1188  */
   1189 #define	ringoffset(sc, start, off, type) \
   1190 	((type)( ((caddr_t)(start)+(off) >= (sc)->smem_end) ? \
   1191 		(((caddr_t)(start)+(off))) - (sc)->smem_end \
   1192 		+ (sc)->smem_ring: \
   1193 		((caddr_t)(start)+(off)) ))
   1194 
   1195 /*
   1196  * Retreive packet from shared memory and send to the next level up via
   1197  *	ether_input(). If there is a BPF listener, give a copy to BPF, too.
   1198  */
   1199 ae_get_packet(sc, buf, len)
   1200 	struct ae_softc *sc;
   1201 	char *buf;
   1202 	u_short len;
   1203 {
   1204 	struct ether_header *eh;
   1205     	struct mbuf *m, *head, *ae_ring_to_mbuf();
   1206 	u_short off;
   1207 	int resid;
   1208 	u_short etype;
   1209 	struct trailer_header {
   1210 		u_short	trail_type;
   1211 		u_short trail_residual;
   1212 	} trailer_header;
   1213 
   1214 	/* Allocate a header mbuf */
   1215 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1216 	if (m == 0)
   1217 		goto bad;
   1218 	m->m_pkthdr.rcvif = &sc->arpcom.ac_if;
   1219 	m->m_pkthdr.len = len;
   1220 	m->m_len = 0;
   1221 	head = m;
   1222 
   1223 	eh = (struct ether_header *)buf;
   1224 
   1225 	/* The following sillines is to make NFS happy */
   1226 #define EROUND	((sizeof(struct ether_header) + 3) & ~3)
   1227 #define EOFF	(EROUND - sizeof(struct ether_header))
   1228 
   1229 	/*
   1230 	 * The following assumes there is room for
   1231 	 * the ether header in the header mbuf
   1232 	 */
   1233 	head->m_data += EOFF;
   1234 	bbcopy(buf, mtod(head, caddr_t), sizeof(struct ether_header));
   1235 	buf += sizeof(struct ether_header);
   1236 	head->m_len += sizeof(struct ether_header);
   1237 	len -= sizeof(struct ether_header);
   1238 
   1239 	etype = ntohs((u_short)eh->ether_type);
   1240 
   1241 	/*
   1242 	 * Deal with trailer protocol:
   1243 	 * If trailer protocol, calculate the datasize as 'off',
   1244 	 * which is also the offset to the trailer header.
   1245 	 * Set resid to the amount of packet data following the
   1246 	 * trailer header.
   1247 	 * Finally, copy residual data into mbuf chain.
   1248 	 */
   1249 	if (etype >= ETHERTYPE_TRAIL &&
   1250 	    etype < ETHERTYPE_TRAIL+ETHERTYPE_NTRAILER) {
   1251 
   1252 		off = (etype - ETHERTYPE_TRAIL) << 9;
   1253 		if ((off + sizeof(struct trailer_header)) > len)
   1254 			goto bad;	/* insanity */
   1255 
   1256 		eh->ether_type = *ringoffset(sc, buf, off, u_short *);
   1257 		resid = ntohs(*ringoffset(sc, buf, off+2, u_short *));
   1258 
   1259 		if ((off + resid) > len) goto bad;	/* insanity */
   1260 
   1261 		resid -= sizeof(struct trailer_header);
   1262 		if (resid < 0) goto bad;	/* insanity */
   1263 
   1264 		m = ae_ring_to_mbuf(sc, ringoffset(sc, buf, off+4, char *), head, resid);
   1265 		if (m == 0) goto bad;
   1266 
   1267 		len = off;
   1268 		head->m_pkthdr.len -= 4; /* subtract trailer header */
   1269 	}
   1270 
   1271 	/*
   1272 	 * Pull packet off interface. Or if this was a trailer packet,
   1273 	 * the data portion is appended.
   1274 	 */
   1275 	m = ae_ring_to_mbuf(sc, buf, m, len);
   1276 	if (m == 0) goto bad;
   1277 
   1278 #if NBPFILTER > 0
   1279 	/*
   1280 	 * Check if there's a BPF listener on this interface.
   1281 	 * If so, hand off the raw packet to bpf.
   1282 	 */
   1283 	if (sc->bpf) {
   1284 		bpf_mtap(sc->bpf, head);
   1285 
   1286 		/*
   1287 		 * Note that the interface cannot be in promiscuous mode if
   1288 		 * there are no BPF listeners.  And if we are in promiscuous
   1289 		 * mode, we have to check if this packet is really ours.
   1290 		 *
   1291 		 * XXX This test does not support multicasts.
   1292 		 */
   1293 		if ((sc->arpcom.ac_if.if_flags & IFF_PROMISC) &&
   1294 			bcmp(eh->ether_dhost, sc->arpcom.ac_enaddr,
   1295 				sizeof(eh->ether_dhost)) != 0 &&
   1296 			bcmp(eh->ether_dhost, etherbroadcastaddr,
   1297 				sizeof(eh->ether_dhost)) != 0) {
   1298 
   1299 			m_freem(head);
   1300 			return;
   1301 		}
   1302 	}
   1303 #endif
   1304 
   1305 	/*
   1306 	 * Fix up data start offset in mbuf to point past ether header
   1307 	 */
   1308 	m_adj(head, sizeof(struct ether_header));
   1309 
   1310 	ether_input(&sc->arpcom.ac_if, eh, head);
   1311 	return;
   1312 
   1313 bad:	if (head)
   1314 		m_freem(head);
   1315 	return;
   1316 }
   1317 
   1318 /*
   1319  * Supporting routines
   1320  */
   1321 
   1322 /*
   1323  * Given a source and destination address, copy 'amount' of a packet from
   1324  *	the ring buffer into a linear destination buffer. Takes into account
   1325  *	ring-wrap.
   1326  */
   1327 static inline char *
   1328 ae_ring_copy(sc,src,dst,amount)
   1329 	struct ae_softc *sc;
   1330 	char	*src;
   1331 	char	*dst;
   1332 	u_short	amount;
   1333 {
   1334 	u_short	tmp_amount;
   1335 
   1336 	/* does copy wrap to lower addr in ring buffer? */
   1337 	if (src + amount > sc->smem_end) {
   1338 		tmp_amount = sc->smem_end - src;
   1339 		bbcopy(src, dst, tmp_amount);/* copy amount up to end of smem */
   1340 		amount -= tmp_amount;
   1341 		src = sc->smem_ring;
   1342 		dst += tmp_amount;
   1343 	}
   1344 
   1345 	bbcopy(src, dst, amount);
   1346 
   1347 	return(src + amount);
   1348 }
   1349 
   1350 /*
   1351  * Copy data from receive buffer to end of mbuf chain
   1352  * allocate additional mbufs as needed. return pointer
   1353  * to last mbuf in chain.
   1354  * sc = ed info (softc)
   1355  * src = pointer in ed ring buffer
   1356  * dst = pointer to last mbuf in mbuf chain to copy to
   1357  * amount = amount of data to copy
   1358  */
   1359 struct mbuf *
   1360 ae_ring_to_mbuf(sc,src,dst,total_len)
   1361 	struct ae_softc *sc;
   1362 	char *src;
   1363 	struct mbuf *dst;
   1364 	u_short total_len;
   1365 {
   1366 	register struct mbuf *m = dst;
   1367 
   1368 	while (total_len) {
   1369 		register u_short amount = min(total_len, M_TRAILINGSPACE(m));
   1370 
   1371 		if (amount == 0) { /* no more data in this mbuf, alloc another */
   1372 			/*
   1373 			 * If there is enough data for an mbuf cluster, attempt
   1374 			 * 	to allocate one of those, otherwise, a regular
   1375 			 *	mbuf will do.
   1376 			 * Note that a regular mbuf is always required, even if
   1377 			 *	we get a cluster - getting a cluster does not
   1378 			 *	allocate any mbufs, and one is needed to assign
   1379 			 *	the cluster to. The mbuf that has a cluster
   1380 			 *	extension can not be used to contain data - only
   1381 			 *	the cluster can contain data.
   1382 			 */
   1383 			dst = m;
   1384 			MGET(m, M_DONTWAIT, MT_DATA);
   1385 			if (m == 0)
   1386 				return (0);
   1387 
   1388 			if (total_len >= MINCLSIZE)
   1389 				MCLGET(m, M_DONTWAIT);
   1390 
   1391 			m->m_len = 0;
   1392 			dst->m_next = m;
   1393 			amount = min(total_len, M_TRAILINGSPACE(m));
   1394 		}
   1395 
   1396 		src = ae_ring_copy(sc, src, mtod(m, caddr_t) + m->m_len, amount);
   1397 
   1398 		m->m_len += amount;
   1399 		total_len -= amount;
   1400 
   1401 	}
   1402 	return (m);
   1403 }
   1404