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