Home | History | Annotate | Line # | Download | only in dev
if_ae.c revision 1.25
      1 /*	$NetBSD: if_ae.c,v 1.25 1995/04/21 02:47:53 briggs Exp $	*/
      2 
      3 /*
      4  * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
      5  * adapters.
      6  *
      7  * Copyright (c) 1994, 1995 Charles M. Hannum.  All rights reserved.
      8  *
      9  * Copyright (C) 1993, David Greenman.  This software may be used, modified,
     10  * copied, distributed, and sold, in both source and binary form provided that
     11  * the above copyright and these terms are retained.  Under no circumstances is
     12  * the author responsible for the proper functioning of this software, nor does
     13  * the author assume any responsibility 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 #include "bpfilter.h"
     25 
     26 #include <sys/param.h>
     27 #include <sys/types.h>
     28 #include <sys/systm.h>
     29 #include <sys/errno.h>
     30 #include <sys/ioctl.h>
     31 #include <sys/mbuf.h>
     32 #include <sys/socket.h>
     33 #include <sys/syslog.h>
     34 #include <sys/device.h>
     35 
     36 #include <net/if.h>
     37 #include <net/if_dl.h>
     38 #include <net/if_types.h>
     39 #include <net/netisr.h>
     40 
     41 #ifdef INET
     42 #include <netinet/in.h>
     43 #include <netinet/in_systm.h>
     44 #include <netinet/in_var.h>
     45 #include <netinet/ip.h>
     46 #include <netinet/if_ether.h>
     47 #endif
     48 
     49 #ifdef NS
     50 #include <netns/ns.h>
     51 #include <netns/ns_if.h>
     52 #endif
     53 
     54 #if NBPFILTER > 0
     55 #include <net/bpf.h>
     56 #include <net/bpfdesc.h>
     57 #endif
     58 
     59 #include "../mac68k/via.h"
     60 #include "nubus.h"
     61 #include <dev/ic/dp8390.h>
     62 #include "if_aereg.h"
     63 
     64 /*
     65  * ae_softc: per line info and status
     66  */
     67 struct ae_softc {
     68 	struct device sc_dev;
     69 /*	struct	nubusdev sc_nu;
     70 	struct	intrhand sc_ih;	*/
     71 
     72 	struct arpcom sc_arpcom;/* ethernet common */
     73 
     74 	char   *type_str;	/* pointer to type string */
     75 	u_char  vendor;		/* interface vendor */
     76 	u_char  type;		/* interface type code */
     77 	u_char  regs_rev;	/* registers are reversed */
     78 
     79 #define	REG_MAP(sc, reg)	((sc)->regs_rev ? (0x0f-(reg))<<2 : (reg)<<2)
     80 #define NIC_GET(sc, reg)	((sc)->nic_addr[REG_MAP(sc, reg)])
     81 #define NIC_PUT(sc, reg, val)	((sc)->nic_addr[REG_MAP(sc, reg)] = (val))
     82 	volatile caddr_t nic_addr;	/* NIC (DS8390) I/O bus address */
     83 	caddr_t rom_addr;	/* on board prom address */
     84 
     85 	u_char  cr_proto;	/* values always set in CR */
     86 
     87 	caddr_t mem_start;	/* shared memory start address */
     88 	caddr_t mem_end;	/* shared memory end address */
     89 	u_long  mem_size;	/* total shared memory size */
     90 	caddr_t mem_ring;	/* start of RX ring-buffer (in smem) */
     91 
     92 	u_char  mem_wr_short;	/* card memory requires int16 writes */
     93 
     94 	u_char  xmit_busy;	/* transmitter is busy */
     95 	u_char  txb_cnt;	/* Number of transmit buffers */
     96 	u_char  txb_inuse;	/* number of TX buffers currently in-use */
     97 
     98 	u_char  txb_new;	/* pointer to where new buffer will be added */
     99 	u_char  txb_next_tx;	/* pointer to next buffer ready to xmit */
    100 	u_short txb_len[8];	/* buffered xmit buffer lengths */
    101 	u_char  tx_page_start;	/* first page of TX buffer area */
    102 	u_char  rec_page_start;	/* first page of RX ring-buffer */
    103 	u_char  rec_page_stop;	/* last page of RX ring-buffer */
    104 	u_char  next_packet;	/* pointer to next unread RX packet */
    105 };
    106 
    107 int aeprobe __P((struct device *, void *, void *));
    108 void aeattach __P((struct device *, struct device *, void *));
    109 void aeintr __P((struct ae_softc *));
    110 int ae_ioctl __P((struct ifnet *, u_long, caddr_t));
    111 void ae_start __P((struct ifnet *));
    112 void ae_watchdog __P(( /* short */ ));
    113 void ae_reset __P((struct ae_softc *));
    114 void ae_init __P((struct ae_softc *));
    115 void ae_stop __P((struct ae_softc *));
    116 void ae_getmcaf __P((struct arpcom *, u_long *));
    117 u_short ae_put __P((struct ae_softc *, struct mbuf *, caddr_t));
    118 
    119 #define inline			/* XXX for debugging porpoises */
    120 
    121 void ae_get_packet __P(( /* struct ae_softc *, caddr_t, u_short */ ));
    122 static inline void ae_rint __P((struct ae_softc *));
    123 static inline void ae_xmit __P((struct ae_softc *));
    124 static inline caddr_t ae_ring_copy
    125 __P((				/* struct ae_softc *, caddr_t, caddr_t,
    126 	    u_short */ ));
    127 
    128 	struct cfdriver aecd = {
    129 		NULL, "ae", aeprobe, aeattach, DV_IFNET, sizeof(struct ae_softc)
    130 	};
    131 #define	ETHER_MIN_LEN	64
    132 #define ETHER_MAX_LEN	1518
    133 #define	ETHER_ADDR_LEN	6
    134 
    135 	char    ae_name[] = "8390 Nubus Ethernet card";
    136 	static char zero = 0;
    137 	static u_char ones = 0xff;
    138 
    139 	struct vendor_S {
    140 		char   *manu;
    141 		int     len;
    142 		int     vendor;
    143 	}       vend[] =
    144 {
    145 	{
    146 		"Apple", 5, AE_VENDOR_APPLE
    147 	},
    148 	{
    149 		"3Com", 4, AE_VENDOR_APPLE
    150 	},
    151 	{
    152 		"Dayna", 5, AE_VENDOR_DAYNA
    153 	},
    154 	{
    155 		"Inter", 5, AE_VENDOR_INTERLAN
    156 	},
    157 	{
    158 		"Asant", 5, AE_VENDOR_ASANTE
    159 	},
    160 };
    161 
    162 static int numvend = sizeof(vend) / sizeof(vend[0]);
    163 
    164 /*
    165  * XXX These two should be moved to locore, and maybe changed to use shorts
    166  * instead of bytes.  The reason for these is that bcopy and bzero use longs,
    167  * which the ethernet cards can't handle.
    168  */
    169 
    170 void
    171 bszero(u_short * addr, int len)
    172 {
    173 
    174 	while (len--)
    175 		*addr++ = 0;
    176 }
    177 /*
    178  * Memory copy, copies word at time.
    179  */
    180 static inline void
    181 word_copy(a, b, len)
    182 	caddr_t a, b;
    183 	int     len;
    184 {
    185 	u_short *x = (u_short *) a, *y = (u_short *) b;
    186 
    187 	len >>= 1;
    188 	while (len--)
    189 		*y++ = *x++;
    190 }
    191 /*
    192  * Memory copy, copies bytes at time.
    193  */
    194 static inline void
    195 byte_copy(a, b, len)
    196 	caddr_t a, b;
    197 	int     len;
    198 {
    199 	while (len--)
    200 		*b++ = *a++;
    201 }
    202 
    203 void
    204 ae_id_card(nu, sc)
    205 	struct nubus_hw *nu;
    206 	struct ae_softc *sc;
    207 {
    208 	int     i;
    209 
    210 	/*
    211 	 * Try to determine what type of card this is...
    212 	 */
    213 	sc->vendor = AE_VENDOR_UNKNOWN;
    214 	for (i = 0; i < numvend; i++) {
    215 		if (!strncmp(nu->slot.manufacturer, vend[i].manu, vend[i].len)) {
    216 			sc->vendor = vend[i].vendor;
    217 			break;
    218 		}
    219 	}
    220 	sc->type_str = (char *) (nu->slot.manufacturer);
    221 
    222 }
    223 
    224 int
    225 ae_size_card_memory(sc)
    226 	struct ae_softc *sc;
    227 {
    228 	u_short *p;
    229 	u_short i1, i2, i3, i4;
    230 	int     size;
    231 
    232 	p = (u_short *) sc->mem_start;
    233 
    234 	/*
    235 	 * very simple size memory, assuming it's installed in 8k
    236 	 * banks; also assume it will generally mirror in upper banks
    237 	 * if not installed.
    238 	 */
    239 	i1 = (8192 * 0) / 2;
    240 	i2 = (8192 * 1) / 2;
    241 	i3 = (8192 * 2) / 2;
    242 	i4 = (8192 * 3) / 2;
    243 
    244 	p[i1] = 0x1111;
    245 	p[i2] = 0x2222;
    246 	p[i3] = 0x3333;
    247 	p[i4] = 0x4444;
    248 
    249 	if (p[i1] == 0x1111 && p[i2] == 0x2222 &&
    250 	    p[i3] == 0x3333 && p[i4] == 0x4444)
    251 		return 8192 * 4;
    252 
    253 	if ((p[i1] == 0x1111 && p[i2] == 0x2222) ||
    254 	    (p[i1] == 0x3333 && p[i2] == 0x4444))
    255 		return 8192 * 2;
    256 
    257 	if (p[i1] == 0x1111 || p[i1] == 0x4444)
    258 		return 8192;
    259 
    260 	return 0;
    261 }
    262 
    263 int
    264 aeprobe(parent, match, aux)
    265 	struct device *parent;
    266 	void   *match, *aux;
    267 {
    268 	struct ae_softc *sc = match;
    269 	register struct nubus_hw *nu = aux;
    270 	int     i, memsize;
    271 	int     flags = 0;
    272 
    273 	if (nu->slot.type != NUBUS_NETWORK)
    274 		return 0;
    275 
    276 	ae_id_card(nu, sc);
    277 
    278 	sc->regs_rev = 0;
    279 	sc->mem_wr_short = 0;
    280 
    281 	switch (sc->vendor) {
    282 	case AE_VENDOR_INTERLAN:
    283 		sc->nic_addr = nu->addr + GC_NIC_OFFSET;
    284 		sc->rom_addr = nu->addr + GC_ROM_OFFSET;
    285 		sc->mem_start = nu->addr + GC_DATA_OFFSET;
    286 		if ((memsize = ae_size_card_memory(sc)) == 0)
    287 			return 0;
    288 
    289 		/* reset the NIC chip */
    290 		*((caddr_t) nu->addr + GC_RESET_OFFSET) = (char) zero;
    291 
    292 		/* Get station address from on-board ROM */
    293 		for (i = 0; i < ETHER_ADDR_LEN; ++i)
    294 			sc->sc_arpcom.ac_enaddr[i] = *(sc->rom_addr + i * 4);
    295 		break;
    296 
    297 	case AE_VENDOR_ASANTE:
    298 		/* memory writes require *(u_short *) */
    299 		sc->mem_wr_short = 1;
    300 		/* otherwise, pretend to be an apple card (fall through) */
    301 
    302 	case AE_VENDOR_APPLE:
    303 		sc->regs_rev = 1;
    304 		sc->nic_addr = nu->addr + AE_NIC_OFFSET;
    305 		sc->rom_addr = nu->addr + AE_ROM_OFFSET;
    306 		sc->mem_start = nu->addr + AE_DATA_OFFSET;
    307 		if ((memsize = ae_size_card_memory(sc)) == 0)
    308 			return (0);
    309 
    310 		/* Get station address from on-board ROM */
    311 		for (i = 0; i < ETHER_ADDR_LEN; ++i)
    312 			sc->sc_arpcom.ac_enaddr[i] = *(sc->rom_addr + i * 2);
    313 		break;
    314 
    315 	case AE_VENDOR_DAYNA:
    316 		printf("We think we are a Dayna card, but ");
    317 		sc->nic_addr = nu->addr + DP_NIC_OFFSET;
    318 		sc->rom_addr = nu->addr + DP_ROM_OFFSET;
    319 		sc->mem_start = nu->addr + DP_DATA_OFFSET;
    320 		memsize = 8192;
    321 
    322 		/* Get station address from on-board ROM */
    323 		for (i = 0; i < ETHER_ADDR_LEN; ++i)
    324 			sc->sc_arpcom.ac_enaddr[i] = *(sc->rom_addr + i * 2);
    325 		printf("it is dangerous to continue.\n");
    326 		return (0);	/* Since we don't work yet... */
    327 		break;
    328 
    329 	default:
    330 		return (0);
    331 		break;
    332 	}
    333 
    334 	sc->cr_proto = ED_CR_RD2;
    335 
    336 	/* Allocate one xmit buffer if < 16k, two buffers otherwise. */
    337 	if ((memsize < 16384) || (flags & AE_FLAGS_NO_DOUBLE_BUFFERING))
    338 		sc->txb_cnt = 1;
    339 	else
    340 		sc->txb_cnt = 2;
    341 
    342 	sc->tx_page_start = 0;
    343 	sc->rec_page_start = sc->tx_page_start + sc->txb_cnt * ED_TXBUF_SIZE;
    344 	sc->rec_page_stop = sc->tx_page_start + (memsize >> ED_PAGE_SHIFT);
    345 	sc->mem_ring = sc->mem_start + (sc->rec_page_start << ED_PAGE_SHIFT);
    346 	sc->mem_size = memsize;
    347 	sc->mem_end = sc->mem_start + memsize;
    348 
    349 	/* Now zero memory and verify that it is clear. */
    350 	bszero((u_short *) sc->mem_start, memsize / 2);
    351 
    352 	for (i = 0; i < memsize; ++i)
    353 		if (sc->mem_start[i]) {
    354 			printf("%s: failed to clear shared memory at %x - check configuration\n",
    355 			    sc->sc_dev.dv_xname,
    356 			    sc->mem_start + i);
    357 			return (0);
    358 		}
    359 	return (1);
    360 }
    361 /*
    362  * Install interface into kernel networking data structures
    363  */
    364 void
    365 aeattach(parent, self, aux)
    366 	struct device *parent, *self;
    367 	void   *aux;
    368 {
    369 	struct ae_softc *sc = (void *) self;
    370 	struct nubus_hw *nu = aux;
    371 	struct cfdata *cf = sc->sc_dev.dv_cfdata;
    372 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    373 
    374 	/* Set interface to stopped condition (reset). */
    375 	ae_stop(sc);
    376 
    377 	/* Initialize ifnet structure. */
    378 	ifp->if_unit = sc->sc_dev.dv_unit;
    379 	ifp->if_name = aecd.cd_name;
    380 	ifp->if_start = ae_start;
    381 	ifp->if_ioctl = ae_ioctl;
    382 	ifp->if_watchdog = ae_watchdog;
    383 	ifp->if_flags =
    384 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    385 
    386 	/* Attach the interface. */
    387 	if_attach(ifp);
    388 	ether_ifattach(ifp);
    389 
    390 	/* Print additional info when attached. */
    391 	printf(": address %s, ", ether_sprintf(sc->sc_arpcom.ac_enaddr));
    392 
    393 	if (sc->type_str && (*sc->type_str != 0))
    394 		printf("type %s", sc->type_str);
    395 	else
    396 		printf("type unknown (0x%x)", sc->type);
    397 
    398 	printf(", %dk mem.\n", sc->mem_size / 1024);
    399 
    400 #if NBPFILTER > 0
    401 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    402 #endif
    403 
    404 	/* make sure interrupts are vectored to us */
    405 	add_nubus_intr((int) sc->rom_addr & 0xFF000000, aeintr, sc);
    406 }
    407 /*
    408  * Reset interface.
    409  */
    410 void
    411 ae_reset(sc)
    412 	struct ae_softc *sc;
    413 {
    414 	int     s;
    415 
    416 	s = splimp();
    417 	ae_stop(sc);
    418 	ae_init(sc);
    419 	splx(s);
    420 }
    421 /*
    422  * Take interface offline.
    423  */
    424 void
    425 ae_stop(sc)
    426 	struct ae_softc *sc;
    427 {
    428 	int     n = 5000;
    429 
    430 	/* Stop everything on the interface, and select page 0 registers. */
    431 	NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
    432 
    433 	/*
    434 	 * Wait for interface to enter stopped state, but limit # of checks to
    435 	 * 'n' (about 5ms).  It shouldn't even take 5us on modern DS8390's, but
    436 	 * just in case it's an old one.
    437 	 */
    438 	while (((NIC_GET(sc, ED_P0_ISR) & ED_ISR_RST) == 0) && --n);
    439 }
    440 /*
    441  * Device timeout/watchdog routine.  Entered if the device neglects to generate
    442  * an interrupt after a transmit has been started on it.
    443  */
    444 static int aeintr_ctr = 0;
    445 void
    446 ae_watchdog(unit)
    447 	int     unit;
    448 {
    449 	struct ae_softc *sc = aecd.cd_devs[unit];
    450 
    451 #if 1
    452 /*
    453  * This is a kludge!  The via code seems to miss slot interrupts
    454  * sometimes.  This kludges around that by calling the handler
    455  * by hand if the watchdog is activated. -- XXX (akb)
    456  */
    457 	int     i;
    458 
    459 	i = aeintr_ctr;
    460 
    461 	(*via2itab[1]) (1);
    462 
    463 	if (i != aeintr_ctr) {
    464 		log(LOG_ERR, "ae%d: device timeout, recovered\n", unit);
    465 		return;
    466 	}
    467 #endif
    468 
    469 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
    470 	++sc->sc_arpcom.ac_if.if_oerrors;
    471 
    472 	ae_reset(sc);
    473 }
    474 /*
    475  * Initialize device.
    476  */
    477 void
    478 ae_init(sc)
    479 	struct ae_softc *sc;
    480 {
    481 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    482 	int     i, s;
    483 	u_char  command;
    484 	u_long  mcaf[2];
    485 
    486 	/* Address not known. */
    487 	if (ifp->if_addrlist == 0)
    488 		return;
    489 
    490 	/*
    491 	 * Initialize the NIC in the exact order outlined in the NS manual.
    492 	 * This init procedure is "mandatory"...don't change what or when
    493 	 * things happen.
    494 	 */
    495 	s = splimp();
    496 
    497 	/* Reset transmitter flags. */
    498 	sc->xmit_busy = 0;
    499 	sc->sc_arpcom.ac_if.if_timer = 0;
    500 
    501 	sc->txb_inuse = 0;
    502 	sc->txb_new = 0;
    503 	sc->txb_next_tx = 0;
    504 
    505 	/* Set interface for page 0, remote DMA complete, stopped. */
    506 	NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
    507 
    508 	/*
    509 	 * Set FIFO threshold to 8, No auto-init Remote DMA, byte
    510 	 * order=80x86, word-wide DMA xfers,
    511 	 */
    512 	NIC_PUT(sc, ED_P0_DCR,
    513 	    ED_DCR_FT1 | ED_DCR_WTS | ED_DCR_LS);
    514 
    515 	/* Clear remote byte count registers. */
    516 	NIC_PUT(sc, ED_P0_RBCR0, 0);
    517 	NIC_PUT(sc, ED_P0_RBCR1, 0);
    518 
    519 	/* Tell RCR to do nothing for now. */
    520 	NIC_PUT(sc, ED_P0_RCR, ED_RCR_MON);
    521 
    522 	/* Place NIC in internal loopback mode. */
    523 	NIC_PUT(sc, ED_P0_TCR, ED_TCR_LB0);
    524 
    525 	/* Initialize receive buffer ring. */
    526 	NIC_PUT(sc, ED_P0_TPSR, sc->rec_page_start);
    527 	NIC_PUT(sc, ED_P0_BNRY, sc->rec_page_start);
    528 	NIC_PUT(sc, ED_P0_PSTART, sc->rec_page_start);
    529 	NIC_PUT(sc, ED_P0_PSTOP, sc->rec_page_stop);
    530 
    531 	/*
    532 	 * Clear all interrupts.  A '1' in each bit position clears the
    533 	 * corresponding flag.
    534 	 */
    535 	NIC_PUT(sc, ED_P0_ISR, 0xff);
    536 
    537 	/*
    538 	 * Enable the following interrupts: receive/transmit complete,
    539 	 * receive/transmit error, and Receiver OverWrite.
    540 	 *
    541 	 * Counter overflow and Remote DMA complete are *not* enabled.
    542 	 */
    543 	NIC_PUT(sc, ED_P0_IMR,
    544 	    ED_IMR_PRXE | ED_IMR_PTXE | ED_IMR_RXEE | ED_IMR_TXEE |
    545 	    ED_IMR_OVWE);
    546 
    547 	/* Program command register for page 1. */
    548 	NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STP);
    549 
    550 	/* Copy out our station address. */
    551 	for (i = 0; i < ETHER_ADDR_LEN; ++i)
    552 		NIC_PUT(sc, ED_P1_PAR0 + i, sc->sc_arpcom.ac_enaddr[i]);
    553 
    554 	/* Set multicast filter on chip. */
    555 	ae_getmcaf(&sc->sc_arpcom, mcaf);
    556 	for (i = 0; i < 8; i++)
    557 		NIC_PUT(sc, ED_P1_MAR0 + i, ((u_char *) mcaf)[i]);
    558 
    559 	/*
    560 	 * Set current page pointer to one page after the boundary pointer, as
    561 	 * recommended in the National manual.
    562 	 */
    563 	sc->next_packet = sc->rec_page_start + 1;
    564 	NIC_PUT(sc, ED_P1_CURR, sc->next_packet);
    565 
    566 	/* Program command register for page 0. */
    567 	NIC_PUT(sc, ED_P1_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
    568 
    569 	i = ED_RCR_AB | ED_RCR_AM;
    570 	if (ifp->if_flags & IFF_PROMISC) {
    571 		/*
    572 		 * Set promiscuous mode.  Multicast filter was set earlier so
    573 		 * that we should receive all multicast packets.
    574 		 */
    575 		i |= ED_RCR_PRO | ED_RCR_AR | ED_RCR_SEP;
    576 	}
    577 	NIC_PUT(sc, ED_P0_RCR, i);
    578 
    579 	/* Take interface out of loopback. */
    580 	NIC_PUT(sc, ED_P0_TCR, 0);
    581 
    582 	/* Fire up the interface. */
    583 	NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
    584 
    585 	/* Set 'running' flag, and clear output active flag. */
    586 	ifp->if_flags |= IFF_RUNNING;
    587 	ifp->if_flags &= ~IFF_OACTIVE;
    588 
    589 	/* ...and attempt to start output. */
    590 	ae_start(ifp);
    591 
    592 	splx(s);
    593 }
    594 /*
    595  * This routine actually starts the transmission on the interface.
    596  */
    597 static inline void
    598 ae_xmit(sc)
    599 	struct ae_softc *sc;
    600 {
    601 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    602 	u_short len;
    603 
    604 	len = sc->txb_len[sc->txb_next_tx];
    605 
    606 	/* Set NIC for page 0 register access. */
    607 	NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
    608 
    609 	/* Set TX buffer start page. */
    610 	NIC_PUT(sc, ED_P0_TPSR, sc->tx_page_start +
    611 	    sc->txb_next_tx * ED_TXBUF_SIZE);
    612 
    613 	/* Set TX length. */
    614 	NIC_PUT(sc, ED_P0_TBCR0, len);
    615 	NIC_PUT(sc, ED_P0_TBCR1, len >> 8);
    616 
    617 	/* Set page 0, remote DMA complete, transmit packet, and *start*. */
    618 	NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_TXP | ED_CR_STA);
    619 	sc->xmit_busy = 1;
    620 
    621 	/* Point to next transmit buffer slot and wrap if necessary. */
    622 	sc->txb_next_tx++;
    623 	if (sc->txb_next_tx == sc->txb_cnt)
    624 		sc->txb_next_tx = 0;
    625 
    626 	/* Set a timer just in case we never hear from the board again. */
    627 	ifp->if_timer = 2;
    628 }
    629 /*
    630  * Start output on interface.
    631  * We make two assumptions here:
    632  *  1) that the current priority is set to splimp _before_ this code
    633  *     is called *and* is returned to the appropriate priority after
    634  *     return
    635  *  2) that the IFF_OACTIVE flag is checked before this code is called
    636  *     (i.e. that the output part of the interface is idle)
    637  */
    638 void
    639 ae_start(ifp)
    640 	struct ifnet *ifp;
    641 {
    642 	struct ae_softc *sc = aecd.cd_devs[ifp->if_unit];
    643 	struct mbuf *m0, *m;
    644 	caddr_t buffer;
    645 	int     len;
    646 
    647 outloop:
    648 	/*
    649 	 * First, see if there are buffered packets and an idle transmitter -
    650 	 * should never happen at this point.
    651 	 */
    652 	if (sc->txb_inuse && (sc->xmit_busy == 0)) {
    653 		printf("%s: packets buffered, but transmitter idle\n",
    654 		    sc->sc_dev.dv_xname);
    655 		ae_xmit(sc);
    656 	}
    657 	/* See if there is room to put another packet in the buffer. */
    658 	if (sc->txb_inuse == sc->txb_cnt) {
    659 		/* No room.  Indicate this to the outside world and exit. */
    660 		ifp->if_flags |= IFF_OACTIVE;
    661 		return;
    662 	}
    663 	IF_DEQUEUE(&sc->sc_arpcom.ac_if.if_snd, m);
    664 	if (m == 0) {
    665 		/*
    666 		 * We are using the !OACTIVE flag to indicate to the outside
    667 		 * world that we can accept an additional packet rather than
    668 		 * that the transmitter is _actually_ active.  Indeed, the
    669 		 * transmitter may be active, but if we haven't filled all the
    670 		 * buffers with data then we still want to accept more.
    671 		 */
    672 		ifp->if_flags &= ~IFF_OACTIVE;
    673 		return;
    674 	}
    675 	/* Copy the mbuf chain into the transmit buffer. */
    676 	m0 = m;
    677 
    678 	/* txb_new points to next open buffer slot. */
    679 	buffer = sc->mem_start + ((sc->txb_new * ED_TXBUF_SIZE) << ED_PAGE_SHIFT);
    680 
    681 	len = ae_put(sc, m, buffer);
    682 
    683 	sc->txb_len[sc->txb_new] = max(len, ETHER_MIN_LEN);
    684 	sc->txb_inuse++;
    685 
    686 	/* Point to next buffer slot and wrap if necessary. */
    687 	if (++sc->txb_new == sc->txb_cnt)
    688 		sc->txb_new = 0;
    689 
    690 	if (sc->xmit_busy == 0)
    691 		ae_xmit(sc);
    692 
    693 #if NBPFILTER > 0
    694 	/* Tap off here if there is a BPF listener. */
    695 	if (sc->sc_arpcom.ac_if.if_bpf)
    696 		bpf_mtap(sc->sc_arpcom.ac_if.if_bpf, m0);
    697 #endif
    698 
    699 	m_freem(m0);
    700 
    701 	/* Loop back to the top to possibly buffer more packets. */
    702 	goto outloop;
    703 }
    704 /*
    705  * Ethernet interface receiver interrupt.
    706  */
    707 static inline void
    708 ae_rint(sc)
    709 	struct ae_softc *sc;
    710 {
    711 	u_char  boundary, current;
    712 	u_short len;
    713 	u_char  nlen;
    714 	struct ae_ring packet_hdr;
    715 	caddr_t packet_ptr;
    716 
    717 loop:
    718 	/* Set NIC to page 1 registers to get 'current' pointer. */
    719 	NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STA);
    720 
    721 	/*
    722 	 * 'sc->next_packet' is the logical beginning of the ring-buffer - i.e.
    723 	 * it points to where new data has been buffered.  The 'CURR' (current)
    724 	 * register points to the logical end of the ring-buffer - i.e. it
    725 	 * points to where additional new data will be added.  We loop here
    726 	 * until the logical beginning equals the logical end (or in other
    727 	 * words, until the ring-buffer is empty).
    728 	 */
    729 	current = NIC_GET(sc, ED_P1_CURR);
    730 	if (sc->next_packet == current)
    731 		return;
    732 
    733 	/* Set NIC to page 0 registers to update boundary register. */
    734 	NIC_PUT(sc, ED_P1_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
    735 
    736 	do {
    737 		/* Get pointer to this buffer's header structure. */
    738 		packet_ptr = sc->mem_ring +
    739 		    ((sc->next_packet - sc->rec_page_start) << ED_PAGE_SHIFT);
    740 
    741 		/*
    742 		 * The byte count includes a 4 byte header that was added by
    743 		 * the NIC.
    744 		 */
    745 		packet_hdr = *(struct ae_ring *) packet_ptr;
    746 		packet_hdr.count =
    747 		    ((packet_hdr.count >> 8) & 0xff) |
    748 		    ((packet_hdr.count & 0xff) << 8);
    749 		len = packet_hdr.count;
    750 
    751 		/*
    752 		 * Try do deal with old, buggy chips that sometimes duplicate
    753 		 * the low byte of the length into the high byte.  We do this
    754 		 * by simply ignoring the high byte of the length and always
    755 		 * recalculating it.
    756 		 *
    757 		 * NOTE: sc->next_packet is pointing at the current packet.
    758 		 */
    759 		if (packet_hdr.next_packet >= sc->next_packet)
    760 			nlen = (packet_hdr.next_packet - sc->next_packet);
    761 		else
    762 			nlen = ((packet_hdr.next_packet - sc->rec_page_start) +
    763 			    (sc->rec_page_stop - sc->next_packet));
    764 		--nlen;
    765 		if ((len & ED_PAGE_MASK) + sizeof(packet_hdr) > ED_PAGE_SIZE)
    766 			--nlen;
    767 		len = (len & ED_PAGE_MASK) | (nlen << ED_PAGE_SHIFT);
    768 #ifdef DIAGNOSTIC
    769 		if (len != packet_hdr.count) {
    770 			printf("%s: length does not match next packet pointer\n",
    771 			    sc->sc_dev.dv_xname);
    772 			printf("%s: len %04x nlen %04x start %02x first %02x curr %02x next %02x stop %02x\n",
    773 			    sc->sc_dev.dv_xname, packet_hdr.count, len,
    774 			    sc->rec_page_start, sc->next_packet, current,
    775 			    packet_hdr.next_packet, sc->rec_page_stop);
    776 		}
    777 #endif
    778 
    779 		/*
    780 		 * Be fairly liberal about what we allow as a "reasonable"
    781 		 * length so that a [crufty] packet will make it to BPF (and
    782 		 * can thus be analyzed).  Note that all that is really
    783 		 * important is that we have a length that will fit into one
    784 		 * mbuf cluster or less; the upper layer protocols can then
    785 		 * figure out the length from their own length field(s).
    786 		 */
    787 		if (len <= MCLBYTES &&
    788 		    packet_hdr.next_packet >= sc->rec_page_start &&
    789 		    packet_hdr.next_packet < sc->rec_page_stop) {
    790 			/* Go get packet. */
    791 			ae_get_packet(sc, packet_ptr + sizeof(struct ae_ring),
    792 			    len - sizeof(struct ae_ring));
    793 			++sc->sc_arpcom.ac_if.if_ipackets;
    794 		} else {
    795 			/* Really BAD.  The ring pointers are corrupted. */
    796 			log(LOG_ERR,
    797 			    "%s: NIC memory corrupt - invalid packet length %d\n",
    798 			    sc->sc_dev.dv_xname, len);
    799 			++sc->sc_arpcom.ac_if.if_ierrors;
    800 			ae_reset(sc);
    801 			return;
    802 		}
    803 
    804 		/* Update next packet pointer. */
    805 		sc->next_packet = packet_hdr.next_packet;
    806 
    807 		/*
    808 		 * Update NIC boundary pointer - being careful to keep it one
    809 		 * buffer behind (as recommended by NS databook).
    810 		 */
    811 		boundary = sc->next_packet - 1;
    812 		if (boundary < sc->rec_page_start)
    813 			boundary = sc->rec_page_stop - 1;
    814 		NIC_PUT(sc, ED_P0_BNRY, boundary);
    815 	} while (sc->next_packet != current);
    816 
    817 	goto loop;
    818 }
    819 /* Ethernet interface interrupt processor. */
    820 void
    821 aeintr(sc)
    822 	struct ae_softc *sc;
    823 {
    824 	u_char  isr;
    825 
    826 	aeintr_ctr++;
    827 
    828 	/* Set NIC to page 0 registers. */
    829 	NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
    830 
    831 	isr = NIC_GET(sc, ED_P0_ISR);
    832 	if (!isr)
    833 		return;
    834 
    835 	/* Loop until there are no more new interrupts. */
    836 	for (;;) {
    837 		/*
    838 		 * Reset all the bits that we are 'acknowledging' by writing a
    839 		 * '1' to each bit position that was set.
    840 		 * (Writing a '1' *clears* the bit.)
    841 		 */
    842 		NIC_PUT(sc, ED_P0_ISR, isr);
    843 
    844 		/*
    845 		 * Handle transmitter interrupts.  Handle these first because
    846 		 * the receiver will reset the board under some conditions.
    847 		 */
    848 		if (isr & (ED_ISR_PTX | ED_ISR_TXE)) {
    849 			u_char  collisions = NIC_GET(sc, ED_P0_NCR) & 0x0f;
    850 
    851 			/*
    852 			 * Check for transmit error.  If a TX completed with an
    853 			 * error, we end up throwing the packet away.  Really
    854 			 * the only error that is possible is excessive
    855 			 * collisions, and in this case it is best to allow the
    856 			 * automatic mechanisms of TCP to backoff the flow.  Of
    857 			 * course, with UDP we're screwed, but this is expected
    858 			 * when a network is heavily loaded.
    859 			 */
    860 			(void) NIC_GET(sc, ED_P0_TSR);
    861 			if (isr & ED_ISR_TXE) {
    862 				/*
    863 				 * Excessive collisions (16).
    864 				 */
    865 				if ((NIC_GET(sc, ED_P0_TSR) & ED_TSR_ABT)
    866 				    && (collisions == 0)) {
    867 					/*
    868 					 * When collisions total 16, the P0_NCR
    869 					 * will indicate 0, and the TSR_ABT is
    870 					 * set.
    871 					 */
    872 					collisions = 16;
    873 				}
    874 				/* Update output errors counter. */
    875 				++sc->sc_arpcom.ac_if.if_oerrors;
    876 			} else {
    877 				/*
    878 				 * Update total number of successfully
    879 				 * transmitted packets.
    880 				 */
    881 				++sc->sc_arpcom.ac_if.if_opackets;
    882 			}
    883 
    884 			/* Reset TX busy and output active flags. */
    885 			sc->xmit_busy = 0;
    886 			sc->sc_arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
    887 
    888 			/* Clear watchdog timer. */
    889 			sc->sc_arpcom.ac_if.if_timer = 0;
    890 
    891 			/*
    892 			 * Add in total number of collisions on last
    893 			 * transmission.
    894 			 */
    895 			sc->sc_arpcom.ac_if.if_collisions += collisions;
    896 
    897 			/*
    898 			 * Decrement buffer in-use count if not zero (can only
    899 			 * be zero if a transmitter interrupt occured while not
    900 			 * actually transmitting).
    901 			 * If data is ready to transmit, start it transmitting,
    902 			 * otherwise defer until after handling receiver.
    903 			 */
    904 			if (sc->txb_inuse && --sc->txb_inuse)
    905 				ae_xmit(sc);
    906 		}
    907 		/* Handle receiver interrupts. */
    908 		if (isr & (ED_ISR_PRX | ED_ISR_RXE | ED_ISR_OVW)) {
    909 			/*
    910 			 * Overwrite warning.  In order to make sure that a
    911 			 * lockup of the local DMA hasn't occurred, we reset
    912 			 * and re-init the NIC.  The NSC manual suggests only a
    913 			 * partial reset/re-init is necessary - but some chips
    914 			 * seem to want more.  The DMA lockup has been seen
    915 			 * only with early rev chips - Methinks this bug was
    916 			 * fixed in later revs.  -DG
    917 			 */
    918 			if (isr & ED_ISR_OVW) {
    919 				++sc->sc_arpcom.ac_if.if_ierrors;
    920 #ifdef DIAGNOSTIC
    921 				log(LOG_WARNING,
    922 				    "%s: warning - receiver ring buffer overrun\n",
    923 				    sc->sc_dev.dv_xname);
    924 #endif
    925 				/* Stop/reset/re-init NIC. */
    926 				ae_reset(sc);
    927 			} else {
    928 				/*
    929 				 * Receiver Error.  One or more of: CRC error,
    930 				 * frame alignment error FIFO overrun, or
    931 				 * missed packet.
    932 				 */
    933 				if (isr & ED_ISR_RXE) {
    934 					++sc->sc_arpcom.ac_if.if_ierrors;
    935 #ifdef AE_DEBUG
    936 					printf("%s: receive error %x\n",
    937 					    sc->sc_dev.dv_xname,
    938 					    NIC_GET(sc, ED_P0_RSR));
    939 #endif
    940 				}
    941 				/*
    942 				 * Go get the packet(s)
    943 				 * XXX - Doing this on an error is dubious
    944 				 * because there shouldn't be any data to get
    945 				 * (we've configured the interface to not
    946 				 * accept packets with errors).
    947 				 */
    948 				ae_rint(sc);
    949 			}
    950 		}
    951 		/*
    952 		 * If it looks like the transmitter can take more data, attempt
    953 		 * to start output on the interface.  This is done after
    954 		 * handling the receiver to give the receiver priority.
    955 		 */
    956 		if ((sc->sc_arpcom.ac_if.if_flags & IFF_OACTIVE) == 0)
    957 			ae_start(&sc->sc_arpcom.ac_if);
    958 
    959 		/*
    960 		 * Return NIC CR to standard state: page 0, remote DMA
    961 		 * complete, start (toggling the TXP bit off, even if was just
    962 		 * set in the transmit routine, is *okay* - it is 'edge'
    963 		 * triggered from low to high).
    964 		 */
    965 		NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
    966 
    967 		/*
    968 		 * If the Network Talley Counters overflow, read them to reset
    969 		 * them.  It appears that old 8390's won't clear the ISR flag
    970 		 * otherwise - resulting in an infinite loop.
    971 		 */
    972 		if (isr & ED_ISR_CNT) {
    973 			(void) NIC_GET(sc, ED_P0_CNTR0);
    974 			(void) NIC_GET(sc, ED_P0_CNTR1);
    975 			(void) NIC_GET(sc, ED_P0_CNTR2);
    976 		}
    977 		isr = NIC_GET(sc, ED_P0_ISR);
    978 		if (!isr)
    979 			return;
    980 	}
    981 }
    982 /*
    983  * Process an ioctl request.  This code needs some work - it looks pretty ugly.
    984  */
    985 int
    986 ae_ioctl(ifp, command, data)
    987 	register struct ifnet *ifp;
    988 	u_long  command;
    989 	caddr_t data;
    990 {
    991 	struct ae_softc *sc = aecd.cd_devs[ifp->if_unit];
    992 	register struct ifaddr *ifa = (struct ifaddr *) data;
    993 	struct ifreq *ifr = (struct ifreq *) data;
    994 	int     s, error = 0;
    995 
    996 	s = splimp();
    997 
    998 	switch (command) {
    999 
   1000 	case SIOCSIFADDR:
   1001 		ifp->if_flags |= IFF_UP;
   1002 
   1003 		switch (ifa->ifa_addr->sa_family) {
   1004 #ifdef INET
   1005 		case AF_INET:
   1006 			ae_init(sc);
   1007 			arp_ifinit(&sc->sc_arpcom, ifa);
   1008 			break;
   1009 #endif
   1010 #ifdef NS
   1011 			/* XXX - This code is probably wrong. */
   1012 		case AF_NS:
   1013 			{
   1014 				register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1015 
   1016 				if (ns_nullhost(*ina))
   1017 					ina->x_host =
   1018 					    *(union ns_host *) (sc->sc_arpcom.ac_enaddr);
   1019 				else
   1020 					bcopy(ina->x_host.c_host,
   1021 					    sc->sc_arpcom.ac_enaddr,
   1022 					    sizeof(sc->sc_arpcom.ac_enaddr));
   1023 				/* Set new address. */
   1024 				ae_init(sc);
   1025 				break;
   1026 			}
   1027 #endif
   1028 		default:
   1029 			ae_init(sc);
   1030 			break;
   1031 		}
   1032 		break;
   1033 
   1034 	case SIOCSIFFLAGS:
   1035 		if ((ifp->if_flags & IFF_UP) == 0 &&
   1036 		    (ifp->if_flags & IFF_RUNNING) != 0) {
   1037 			/*
   1038 			 * If interface is marked down and it is running, then
   1039 			 * stop it.
   1040 			 */
   1041 			ae_stop(sc);
   1042 			ifp->if_flags &= ~IFF_RUNNING;
   1043 		} else
   1044 			if ((ifp->if_flags & IFF_UP) != 0 &&
   1045 			    (ifp->if_flags & IFF_RUNNING) == 0) {
   1046 				/*
   1047 				 * If interface is marked up and it is stopped, then
   1048 				 * start it.
   1049 				 */
   1050 				ae_init(sc);
   1051 			} else {
   1052 				/*
   1053 				 * Reset the interface to pick up changes in any other
   1054 				 * flags that affect hardware registers.
   1055 				 */
   1056 				ae_stop(sc);
   1057 				ae_init(sc);
   1058 			}
   1059 		break;
   1060 
   1061 	case SIOCADDMULTI:
   1062 	case SIOCDELMULTI:
   1063 		/* Update our multicast list. */
   1064 		error = (command == SIOCADDMULTI) ?
   1065 		    ether_addmulti(ifr, &sc->sc_arpcom) :
   1066 		    ether_delmulti(ifr, &sc->sc_arpcom);
   1067 
   1068 		if (error == ENETRESET) {
   1069 			/*
   1070 			 * Multicast list has changed; set the hardware filter
   1071 			 * accordingly.
   1072 			 */
   1073 			ae_stop(sc);	/* XXX for ds_setmcaf? */
   1074 			ae_init(sc);
   1075 			error = 0;
   1076 		}
   1077 		break;
   1078 
   1079 	default:
   1080 		error = EINVAL;
   1081 	}
   1082 
   1083 	splx(s);
   1084 	return (error);
   1085 }
   1086 /*
   1087  * Retreive packet from shared memory and send to the next level up via
   1088  * ether_input().  If there is a BPF listener, give a copy to BPF, too.
   1089  */
   1090 void
   1091 ae_get_packet(sc, buf, len)
   1092 	struct ae_softc *sc;
   1093 	caddr_t buf;
   1094 	u_short len;
   1095 {
   1096 	struct ether_header *eh;
   1097 	struct mbuf *m, *ae_ring_to_mbuf();
   1098 
   1099 	/* Allocate a header mbuf. */
   1100 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1101 	if (m == 0)
   1102 		return;
   1103 	m->m_pkthdr.rcvif = &sc->sc_arpcom.ac_if;
   1104 	m->m_pkthdr.len = len;
   1105 	m->m_len = 0;
   1106 
   1107 	/* The following silliness is to make NFS happy. */
   1108 #define EROUND	((sizeof(struct ether_header) + 3) & ~3)
   1109 #define EOFF	(EROUND - sizeof(struct ether_header))
   1110 
   1111 	/*
   1112 	 * The following assumes there is room for the ether header in the
   1113 	 * header mbuf.
   1114 	 */
   1115 	m->m_data += EOFF;
   1116 	eh = mtod(m, struct ether_header *);
   1117 
   1118 	word_copy(buf, mtod(m, caddr_t), sizeof(struct ether_header));
   1119 	buf += sizeof(struct ether_header);
   1120 	m->m_len += sizeof(struct ether_header);
   1121 	len -= sizeof(struct ether_header);
   1122 
   1123 	/* Pull packet off interface. */
   1124 	if (ae_ring_to_mbuf(sc, buf, m, len) == 0) {
   1125 		m_freem(m);
   1126 		return;
   1127 	}
   1128 #if NBPFILTER > 0
   1129 	/*
   1130 	 * Check if there's a BPF listener on this interface.  If so, hand off
   1131 	 * the raw packet to bpf.
   1132 	 */
   1133 	if (sc->sc_arpcom.ac_if.if_bpf) {
   1134 		bpf_mtap(sc->sc_arpcom.ac_if.if_bpf, m);
   1135 
   1136 		/*
   1137 		 * Note that the interface cannot be in promiscuous mode if
   1138 		 * there are no BPF listeners.  And if we are in promiscuous
   1139 		 * mode, we have to check if this packet is really ours.
   1140 		 */
   1141 		if ((sc->sc_arpcom.ac_if.if_flags & IFF_PROMISC) &&
   1142 		    (eh->ether_dhost[0] & 1) == 0 &&	/* !mcast and !bcast */
   1143 		    bcmp(eh->ether_dhost, sc->sc_arpcom.ac_enaddr,
   1144 			sizeof(eh->ether_dhost)) != 0) {
   1145 			m_freem(m);
   1146 			return;
   1147 		}
   1148 	}
   1149 #endif
   1150 
   1151 	/* Fix up data start offset in mbuf to point past ether header. */
   1152 	m_adj(m, sizeof(struct ether_header));
   1153 	ether_input(&sc->sc_arpcom.ac_if, eh, m);
   1154 }
   1155 /*
   1156  * Supporting routines.
   1157  */
   1158 
   1159 /*
   1160  * Given a source and destination address, copy 'amount' of a packet from the
   1161  * ring buffer into a linear destination buffer.  Takes into account ring-wrap.
   1162  */
   1163 static inline caddr_t
   1164 ae_ring_copy(sc, src, dst, amount)
   1165 	struct ae_softc *sc;
   1166 	caddr_t src, dst;
   1167 	u_short amount;
   1168 {
   1169 	u_short tmp_amount;
   1170 
   1171 	/* Does copy wrap to lower addr in ring buffer? */
   1172 	if (src + amount > sc->mem_end) {
   1173 		tmp_amount = sc->mem_end - src;
   1174 
   1175 		/* Copy amount up to end of NIC memory. */
   1176 		byte_copy(src, dst, tmp_amount);
   1177 
   1178 		amount -= tmp_amount;
   1179 		src = sc->mem_ring;
   1180 		dst += tmp_amount;
   1181 	}
   1182 	byte_copy(src, dst, amount);
   1183 
   1184 	return (src + amount);
   1185 }
   1186 /*
   1187  * Copy data from receive buffer to end of mbuf chain allocate additional mbufs
   1188  * as needed.  Return pointer to last mbuf in chain.
   1189  * sc = ae info (softc)
   1190  * src = pointer in ae ring buffer
   1191  * dst = pointer to last mbuf in mbuf chain to copy to
   1192  * amount = amount of data to copy
   1193  */
   1194 struct mbuf *
   1195 ae_ring_to_mbuf(sc, src, dst, total_len)
   1196 	struct ae_softc *sc;
   1197 	caddr_t src;
   1198 	struct mbuf *dst;
   1199 	u_short total_len;
   1200 {
   1201 	register struct mbuf *m = dst;
   1202 
   1203 	while (total_len) {
   1204 		register u_short amount = min(total_len, M_TRAILINGSPACE(m));
   1205 
   1206 		if (amount == 0) {
   1207 			/*
   1208 			 * No more data in this mbuf; alloc another.
   1209 			 *
   1210 			 * If there is enough data for an mbuf cluster, attempt
   1211 			 * to allocate one of those, otherwise, a regular mbuf
   1212 			 * will do.
   1213 			 * Note that a regular mbuf is always required, even if
   1214 			 * we get a cluster - getting a cluster does not
   1215 			 * allocate any mbufs, and one is needed to assign the
   1216 			 * cluster to.  The mbuf that has a cluster extension
   1217 			 * can not be used to contain data - only the cluster
   1218 			 * can contain data.
   1219 			 */
   1220 			dst = m;
   1221 			MGET(m, M_DONTWAIT, MT_DATA);
   1222 			if (m == 0)
   1223 				return (0);
   1224 
   1225 			if (total_len >= MINCLSIZE)
   1226 				MCLGET(m, M_DONTWAIT);
   1227 
   1228 			m->m_len = 0;
   1229 			dst->m_next = m;
   1230 			amount = min(total_len, M_TRAILINGSPACE(m));
   1231 		}
   1232 		src = ae_ring_copy(sc, src, mtod(m, caddr_t) + m->m_len,
   1233 		    amount);
   1234 
   1235 		m->m_len += amount;
   1236 		total_len -= amount;
   1237 	}
   1238 	return (m);
   1239 }
   1240 /*
   1241  * Compute the multicast address filter from the list of multicast addresses we
   1242  * need to listen to.
   1243  */
   1244 void
   1245 ae_getmcaf(ac, af)
   1246 	struct arpcom *ac;
   1247 	u_long *af;
   1248 {
   1249 	struct ifnet *ifp = &ac->ac_if;
   1250 	struct ether_multi *enm;
   1251 	register u_char *cp, c;
   1252 	register u_long crc;
   1253 	register int i, len;
   1254 	struct ether_multistep step;
   1255 
   1256 	/*
   1257 	 * Set up multicast address filter by passing all multicast addresses
   1258 	 * through a crc generator, and then using the high order 6 bits as an
   1259 	 * index into the 64 bit logical address filter.  The high order bit
   1260 	 * selects the word, while the rest of the bits select the bit within
   1261 	 * the word.
   1262 	 */
   1263 
   1264 	if (ifp->if_flags & IFF_PROMISC) {
   1265 		ifp->if_flags |= IFF_ALLMULTI;
   1266 		af[0] = af[1] = 0xffffffff;
   1267 		return;
   1268 	}
   1269 	af[0] = af[1] = 0;
   1270 	ETHER_FIRST_MULTI(step, ac, enm);
   1271 	while (enm != NULL) {
   1272 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
   1273 			sizeof(enm->enm_addrlo)) != 0) {
   1274 			/*
   1275 			 * We must listen to a range of multicast addresses.
   1276 			 * For now, just accept all multicasts, rather than
   1277 			 * trying to set only those filter bits needed to match
   1278 			 * the range.  (At this time, the only use of address
   1279 			 * ranges is for IP multicast routing, for which the
   1280 			 * range is big enough to require all bits set.)
   1281 			 */
   1282 			ifp->if_flags |= IFF_ALLMULTI;
   1283 			af[0] = af[1] = 0xffffffff;
   1284 			return;
   1285 		}
   1286 		cp = enm->enm_addrlo;
   1287 		crc = 0xffffffff;
   1288 		for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
   1289 			c = *cp++;
   1290 			for (i = 8; --i >= 0;) {
   1291 				if (((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01)) {
   1292 					crc <<= 1;
   1293 					crc ^= 0x04c11db6 | 1;
   1294 				} else
   1295 					crc <<= 1;
   1296 				c >>= 1;
   1297 			}
   1298 		}
   1299 		/* Just want the 6 most significant bits. */
   1300 		crc >>= 26;
   1301 
   1302 		/* Turn on the corresponding bit in the filter. */
   1303 		af[crc >> 5] |= 1 << ((crc & 0x1f) ^ 0);
   1304 
   1305 		ETHER_NEXT_MULTI(step, enm);
   1306 	}
   1307 	ifp->if_flags &= ~IFF_ALLMULTI;
   1308 }
   1309 /*
   1310  * Copy packet from mbuf to the board memory
   1311  *
   1312  * Currently uses an extra buffer/extra memory copy,
   1313  * unless the whole packet fits in one mbuf.
   1314  *
   1315  */
   1316 u_short
   1317 ae_put(sc, m, buf)
   1318 	struct ae_softc *sc;
   1319 	struct mbuf *m;
   1320 	caddr_t buf;
   1321 {
   1322 	u_char *data, savebyte[2];
   1323 	int     len, wantbyte;
   1324 	u_short totlen = 0;
   1325 
   1326 	wantbyte = 0;
   1327 
   1328 	for (; m != 0; m = m->m_next) {
   1329 		data = mtod(m, u_char *);
   1330 		len = m->m_len;
   1331 		totlen += len;
   1332 		if (len > 0) {
   1333 			/* Finish the last word. */
   1334 			if (wantbyte) {
   1335 				savebyte[1] = *data;
   1336 				word_copy(savebyte, buf, 2);
   1337 				buf += 2;
   1338 				data++;
   1339 				len--;
   1340 				wantbyte = 0;
   1341 			}
   1342 			/* Output contiguous words. */
   1343 			if (len > 1) {
   1344 				word_copy(data, buf, len);
   1345 				buf += len & ~1;
   1346 				data += len & ~1;
   1347 				len &= 1;
   1348 			}
   1349 			/* Save last byte, if necessary. */
   1350 			if (len == 1) {
   1351 				savebyte[0] = *data;
   1352 				wantbyte = 1;
   1353 			}
   1354 		}
   1355 	}
   1356 
   1357 	if (wantbyte) {
   1358 		savebyte[1] = 0;
   1359 		word_copy(savebyte, buf, 2);
   1360 	}
   1361 	return (totlen);
   1362 }
   1363