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