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