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