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