Home | History | Annotate | Line # | Download | only in dev
if_ae.c revision 1.10
      1 /* $Id: if_ae.c,v 1.10 2008/01/19 22:10:15 dyoung Exp $ */
      2 /*-
      3  * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
      4  * Copyright (c) 2006 Garrett D'Amore.
      5  * All rights reserved.
      6  *
      7  * This code was written by Garrett D'Amore for the Champaign-Urbana
      8  * Community Wireless Network Project.
      9  *
     10  * Redistribution and use in source and binary forms, with or
     11  * without modification, are permitted provided that the following
     12  * conditions are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above
     16  *    copyright notice, this list of conditions and the following
     17  *    disclaimer in the documentation and/or other materials provided
     18  *    with the distribution.
     19  * 3. All advertising materials mentioning features or use of this
     20  *    software must display the following acknowledgements:
     21  *      This product includes software developed by the Urbana-Champaign
     22  *      Independent Media Center.
     23  *	This product includes software developed by Garrett D'Amore.
     24  * 4. Urbana-Champaign Independent Media Center's name and Garrett
     25  *    D'Amore's name may not be used to endorse or promote products
     26  *    derived from this software without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
     29  * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
     30  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     31  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
     33  * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
     34  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     35  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     36  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     37  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     38  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     39  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     40  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     41  */
     42 /*-
     43  * Copyright (c) 1998, 1999, 2000, 2002 The NetBSD Foundation, Inc.
     44  * All rights reserved.
     45  *
     46  * This code is derived from software contributed to The NetBSD Foundation
     47  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
     48  * NASA Ames Research Center; and by Charles M. Hannum.
     49  *
     50  * Redistribution and use in source and binary forms, with or without
     51  * modification, are permitted provided that the following conditions
     52  * are met:
     53  * 1. Redistributions of source code must retain the above copyright
     54  *    notice, this list of conditions and the following disclaimer.
     55  * 2. Redistributions in binary form must reproduce the above copyright
     56  *    notice, this list of conditions and the following disclaimer in the
     57  *    documentation and/or other materials provided with the distribution.
     58  * 3. All advertising materials mentioning features or use of this software
     59  *    must display the following acknowledgement:
     60  *	This product includes software developed by the NetBSD
     61  *	Foundation, Inc. and its contributors.
     62  * 4. Neither the name of The NetBSD Foundation nor the names of its
     63  *    contributors may be used to endorse or promote products derived
     64  *    from this software without specific prior written permission.
     65  *
     66  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     67  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     68  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     69  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     70  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     71  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     72  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     73  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     74  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     75  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     76  * POSSIBILITY OF SUCH DAMAGE.
     77  */
     78 
     79 /*
     80  * Device driver for the onboard ethernet MAC found on the AR5312
     81  * chip's AHB bus.
     82  *
     83  * This device is very simliar to the tulip in most regards, and
     84  * the code is directly derived from NetBSD's tulip.c.  However, it
     85  * is different enough that it did not seem to be a good idea to
     86  * add further complexity to the tulip driver, so we have our own.
     87  *
     88  * Also tulip has a lot of complexity in it for various parts/options
     89  * that we don't need, and on these little boxes with only ~8MB RAM, we
     90  * don't want any extra bloat.
     91  */
     92 
     93 /*
     94  * TODO:
     95  *
     96  * 1) Find out about BUS_MODE_ALIGN16B.  This chip can apparently align
     97  *    inbound packets on a half-word boundary, which would make life easier
     98  *    for TCP/IP.  (Aligning IP headers on a word.)
     99  *
    100  * 2) There is stuff in original tulip to shut down the device when reacting
    101  *    to a a change in link status.  Is that needed.
    102  *
    103  * 3) Test with variety of 10/100 HDX/FDX scenarios.
    104  *
    105  */
    106 
    107 #include <sys/cdefs.h>
    108 __KERNEL_RCSID(0, "$NetBSD: if_ae.c,v 1.10 2008/01/19 22:10:15 dyoung Exp $");
    109 
    110 #include "bpfilter.h"
    111 
    112 #include <sys/param.h>
    113 #include <sys/systm.h>
    114 #include <sys/callout.h>
    115 #include <sys/mbuf.h>
    116 #include <sys/malloc.h>
    117 #include <sys/kernel.h>
    118 #include <sys/socket.h>
    119 #include <sys/ioctl.h>
    120 #include <sys/errno.h>
    121 #include <sys/device.h>
    122 
    123 #include <machine/endian.h>
    124 
    125 #include <uvm/uvm_extern.h>
    126 
    127 #include <net/if.h>
    128 #include <net/if_dl.h>
    129 #include <net/if_media.h>
    130 #include <net/if_ether.h>
    131 
    132 #if NBPFILTER > 0
    133 #include <net/bpf.h>
    134 #endif
    135 
    136 #include <machine/bus.h>
    137 #include <machine/intr.h>
    138 
    139 #include <dev/mii/mii.h>
    140 #include <dev/mii/miivar.h>
    141 #include <dev/mii/mii_bitbang.h>
    142 
    143 #include <mips/atheros/include/arbusvar.h>
    144 #include <mips/atheros/dev/aereg.h>
    145 #include <mips/atheros/dev/aevar.h>
    146 
    147 static const struct {
    148 	u_int32_t txth_opmode;		/* OPMODE bits */
    149 	const char *txth_name;		/* name of mode */
    150 } ae_txthresh[] = {
    151 	{ OPMODE_TR_32,		"32 words" },
    152 	{ OPMODE_TR_64,		"64 words" },
    153 	{ OPMODE_TR_128,	"128 words" },
    154 	{ OPMODE_TR_256,	"256 words" },
    155 	{ OPMODE_SF,		"store and forward mode" },
    156 	{ 0,			NULL },
    157 };
    158 
    159 static int 	ae_match(struct device *, struct cfdata *, void *);
    160 static void	ae_attach(struct device *, struct device *, void *);
    161 static int	ae_detach(struct device *, int);
    162 static int	ae_activate(struct device *, enum devact);
    163 
    164 static void	ae_reset(struct ae_softc *);
    165 static void	ae_idle(struct ae_softc *, u_int32_t);
    166 
    167 static void	ae_start(struct ifnet *);
    168 static void	ae_watchdog(struct ifnet *);
    169 static int	ae_ioctl(struct ifnet *, u_long, void *);
    170 static int	ae_init(struct ifnet *);
    171 static void	ae_stop(struct ifnet *, int);
    172 
    173 static void	ae_shutdown(void *);
    174 
    175 static void	ae_rxdrain(struct ae_softc *);
    176 static int	ae_add_rxbuf(struct ae_softc *, int);
    177 
    178 static int	ae_enable(struct ae_softc *);
    179 static void	ae_disable(struct ae_softc *);
    180 static void	ae_power(int, void *);
    181 
    182 static void	ae_filter_setup(struct ae_softc *);
    183 
    184 static int	ae_intr(void *);
    185 static void	ae_rxintr(struct ae_softc *);
    186 static void	ae_txintr(struct ae_softc *);
    187 
    188 static void	ae_mii_tick(void *);
    189 static void	ae_mii_statchg(struct device *);
    190 
    191 static int	ae_mii_readreg(struct device *, int, int);
    192 static void	ae_mii_writereg(struct device *, int, int, int);
    193 
    194 #ifdef AE_DEBUG
    195 #define	DPRINTF(sc, x)	if ((sc)->sc_ethercom.ec_if.if_flags & IFF_DEBUG) \
    196 				printf x
    197 #else
    198 #define	DPRINTF(sc, x)	/* nothing */
    199 #endif
    200 
    201 #ifdef AE_STATS
    202 static void	ae_print_stats(struct ae_softc *);
    203 #endif
    204 
    205 CFATTACH_DECL(ae, sizeof(struct ae_softc),
    206     ae_match, ae_attach, ae_detach, ae_activate);
    207 
    208 /*
    209  * ae_match:
    210  *
    211  *	Check for a device match.
    212  */
    213 int
    214 ae_match(struct device *parent, struct cfdata *cf, void *aux)
    215 {
    216 	struct arbus_attach_args *aa = aux;
    217 
    218 	if (strcmp(aa->aa_name, cf->cf_name) == 0)
    219 		return 1;
    220 
    221 	return 0;
    222 
    223 }
    224 
    225 /*
    226  * ae_attach:
    227  *
    228  *	Attach an ae interface to the system.
    229  */
    230 void
    231 ae_attach(struct device *parent, struct device *self, void *aux)
    232 {
    233 	const uint8_t *enaddr;
    234 	prop_data_t ea;
    235 	struct ae_softc *sc = (void *)self;
    236 	struct arbus_attach_args *aa = aux;
    237 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    238 	int i, error;
    239 
    240 	callout_init(&sc->sc_tick_callout, 0);
    241 
    242 	printf(": Atheros AR531X 10/100 Ethernet\n");
    243 
    244 	/*
    245 	 * Try to get MAC address.
    246 	 */
    247 	ea = prop_dictionary_get(device_properties(&sc->sc_dev), "mac-addr");
    248 	if (ea == NULL) {
    249 		printf("%s: unable to get mac-addr property\n",
    250 		    sc->sc_dev.dv_xname);
    251 		return;
    252 	}
    253 	KASSERT(prop_object_type(ea) == PROP_TYPE_DATA);
    254 	KASSERT(prop_data_size(ea) == ETHER_ADDR_LEN);
    255 	enaddr = prop_data_data_nocopy(ea);
    256 
    257 	/* Announce ourselves. */
    258 	printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
    259 	    ether_sprintf(enaddr));
    260 
    261 	sc->sc_cirq = aa->aa_cirq;
    262 	sc->sc_mirq = aa->aa_mirq;
    263 	sc->sc_st = aa->aa_bst;
    264 	sc->sc_dmat = aa->aa_dmat;
    265 
    266 	SIMPLEQ_INIT(&sc->sc_txfreeq);
    267 	SIMPLEQ_INIT(&sc->sc_txdirtyq);
    268 
    269 	/*
    270 	 * Map registers.
    271 	 */
    272 	sc->sc_size = aa->aa_size;
    273 	if ((error = bus_space_map(sc->sc_st, aa->aa_addr, sc->sc_size, 0,
    274 	    &sc->sc_sh)) != 0) {
    275 		printf("%s: unable to map registers, error = %d\n",
    276 		    sc->sc_dev.dv_xname, error);
    277 		goto fail_0;
    278 	}
    279 
    280 	/*
    281 	 * Allocate the control data structures, and create and load the
    282 	 * DMA map for it.
    283 	 */
    284 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
    285 	    sizeof(struct ae_control_data), PAGE_SIZE, 0, &sc->sc_cdseg,
    286 	    1, &sc->sc_cdnseg, 0)) != 0) {
    287 		printf("%s: unable to allocate control data, error = %d\n",
    288 		    sc->sc_dev.dv_xname, error);
    289 		goto fail_1;
    290 	}
    291 
    292 	if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg,
    293 	    sizeof(struct ae_control_data), (void **)&sc->sc_control_data,
    294 	    BUS_DMA_COHERENT)) != 0) {
    295 		printf("%s: unable to map control data, error = %d\n",
    296 		    sc->sc_dev.dv_xname, error);
    297 		goto fail_2;
    298 	}
    299 
    300 	if ((error = bus_dmamap_create(sc->sc_dmat,
    301 	    sizeof(struct ae_control_data), 1,
    302 	    sizeof(struct ae_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
    303 		printf("%s: unable to create control data DMA map, "
    304 		    "error = %d\n", sc->sc_dev.dv_xname, error);
    305 		goto fail_3;
    306 	}
    307 
    308 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
    309 	    sc->sc_control_data, sizeof(struct ae_control_data), NULL,
    310 	    0)) != 0) {
    311 		printf("%s: unable to load control data DMA map, error = %d\n",
    312 		    sc->sc_dev.dv_xname, error);
    313 		goto fail_4;
    314 	}
    315 
    316 	/*
    317 	 * Create the transmit buffer DMA maps.
    318 	 */
    319 	for (i = 0; i < AE_TXQUEUELEN; i++) {
    320 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
    321 		    AE_NTXSEGS, MCLBYTES, 0, 0,
    322 		    &sc->sc_txsoft[i].txs_dmamap)) != 0) {
    323 			printf("%s: unable to create tx DMA map %d, "
    324 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
    325 			goto fail_5;
    326 		}
    327 	}
    328 
    329 	/*
    330 	 * Create the receive buffer DMA maps.
    331 	 */
    332 	for (i = 0; i < AE_NRXDESC; i++) {
    333 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
    334 		    MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
    335 			printf("%s: unable to create rx DMA map %d, "
    336 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
    337 			goto fail_6;
    338 		}
    339 		sc->sc_rxsoft[i].rxs_mbuf = NULL;
    340 	}
    341 
    342 	/*
    343 	 * Reset the chip to a known state.
    344 	 */
    345 	ae_reset(sc);
    346 
    347 	/*
    348 	 * From this point forward, the attachment cannot fail.  A failure
    349 	 * before this point releases all resources that may have been
    350 	 * allocated.
    351 	 */
    352 	sc->sc_flags |= AE_ATTACHED;
    353 
    354 	/*
    355 	 * Initialize our media structures.  This may probe the MII, if
    356 	 * present.
    357 	 */
    358 	sc->sc_mii.mii_ifp = ifp;
    359 	sc->sc_mii.mii_readreg = ae_mii_readreg;
    360 	sc->sc_mii.mii_writereg = ae_mii_writereg;
    361 	sc->sc_mii.mii_statchg = ae_mii_statchg;
    362 	sc->sc_ethercom.ec_mii = &sc->sc_mii;
    363 	ifmedia_init(&sc->sc_mii.mii_media, 0, ether_mediachange,
    364 	    ether_mediastatus);
    365 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
    366 	    MII_OFFSET_ANY, 0);
    367 
    368 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
    369 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
    370 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
    371 	} else
    372 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
    373 
    374 	sc->sc_tick = ae_mii_tick;
    375 
    376 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
    377 	ifp->if_softc = sc;
    378 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    379 	sc->sc_if_flags = ifp->if_flags;
    380 	ifp->if_ioctl = ae_ioctl;
    381 	ifp->if_start = ae_start;
    382 	ifp->if_watchdog = ae_watchdog;
    383 	ifp->if_init = ae_init;
    384 	ifp->if_stop = ae_stop;
    385 	IFQ_SET_READY(&ifp->if_snd);
    386 
    387 	/*
    388 	 * We can support 802.1Q VLAN-sized frames.
    389 	 */
    390 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
    391 
    392 	/*
    393 	 * Attach the interface.
    394 	 */
    395 	if_attach(ifp);
    396 	ether_ifattach(ifp, enaddr);
    397 
    398 #if NRND > 0
    399 	rnd_attach_source(&sc->sc_rnd_source, sc->sc_dev.dv_xname,
    400 	    RND_TYPE_NET, 0);
    401 #endif
    402 
    403 	/*
    404 	 * Make sure the interface is shutdown during reboot.
    405 	 */
    406 	sc->sc_sdhook = shutdownhook_establish(ae_shutdown, sc);
    407 	if (sc->sc_sdhook == NULL)
    408 		printf("%s: WARNING: unable to establish shutdown hook\n",
    409 		    sc->sc_dev.dv_xname);
    410 
    411 	/*
    412 	 * Add a suspend hook to make sure we come back up after a
    413 	 * resume.
    414 	 */
    415 	sc->sc_powerhook = powerhook_establish(sc->sc_dev.dv_xname,
    416 	    ae_power, sc);
    417 	if (sc->sc_powerhook == NULL)
    418 		printf("%s: WARNING: unable to establish power hook\n",
    419 		    sc->sc_dev.dv_xname);
    420 	return;
    421 
    422 	/*
    423 	 * Free any resources we've allocated during the failed attach
    424 	 * attempt.  Do this in reverse order and fall through.
    425 	 */
    426  fail_6:
    427 	for (i = 0; i < AE_NRXDESC; i++) {
    428 		if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
    429 			bus_dmamap_destroy(sc->sc_dmat,
    430 			    sc->sc_rxsoft[i].rxs_dmamap);
    431 	}
    432  fail_5:
    433 	for (i = 0; i < AE_TXQUEUELEN; i++) {
    434 		if (sc->sc_txsoft[i].txs_dmamap != NULL)
    435 			bus_dmamap_destroy(sc->sc_dmat,
    436 			    sc->sc_txsoft[i].txs_dmamap);
    437 	}
    438 	bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
    439  fail_4:
    440 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
    441  fail_3:
    442 	bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_control_data,
    443 	    sizeof(struct ae_control_data));
    444  fail_2:
    445 	bus_dmamem_free(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg);
    446  fail_1:
    447 	bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_size);
    448  fail_0:
    449 	return;
    450 }
    451 
    452 /*
    453  * ae_activate:
    454  *
    455  *	Handle device activation/deactivation requests.
    456  */
    457 int
    458 ae_activate(struct device *self, enum devact act)
    459 {
    460 	struct ae_softc *sc = (void *) self;
    461 	int s, error = 0;
    462 
    463 	s = splnet();
    464 	switch (act) {
    465 	case DVACT_ACTIVATE:
    466 		error = EOPNOTSUPP;
    467 		break;
    468 
    469 	case DVACT_DEACTIVATE:
    470 		mii_activate(&sc->sc_mii, act, MII_PHY_ANY, MII_OFFSET_ANY);
    471 		if_deactivate(&sc->sc_ethercom.ec_if);
    472 		break;
    473 	}
    474 	splx(s);
    475 
    476 	return (error);
    477 }
    478 
    479 /*
    480  * ae_detach:
    481  *
    482  *	Detach a device interface.
    483  */
    484 int
    485 ae_detach(struct device *self, int flags)
    486 {
    487 	struct ae_softc *sc = (void *)self;
    488 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    489 	struct ae_rxsoft *rxs;
    490 	struct ae_txsoft *txs;
    491 	int i;
    492 
    493 	/*
    494 	 * Succeed now if there isn't any work to do.
    495 	 */
    496 	if ((sc->sc_flags & AE_ATTACHED) == 0)
    497 		return (0);
    498 
    499 	/* Unhook our tick handler. */
    500 	if (sc->sc_tick)
    501 		callout_stop(&sc->sc_tick_callout);
    502 
    503 	/* Detach all PHYs */
    504 	mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
    505 
    506 	/* Delete all remaining media. */
    507 	ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
    508 
    509 #if NRND > 0
    510 	rnd_detach_source(&sc->sc_rnd_source);
    511 #endif
    512 	ether_ifdetach(ifp);
    513 	if_detach(ifp);
    514 
    515 	for (i = 0; i < AE_NRXDESC; i++) {
    516 		rxs = &sc->sc_rxsoft[i];
    517 		if (rxs->rxs_mbuf != NULL) {
    518 			bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
    519 			m_freem(rxs->rxs_mbuf);
    520 			rxs->rxs_mbuf = NULL;
    521 		}
    522 		bus_dmamap_destroy(sc->sc_dmat, rxs->rxs_dmamap);
    523 	}
    524 	for (i = 0; i < AE_TXQUEUELEN; i++) {
    525 		txs = &sc->sc_txsoft[i];
    526 		if (txs->txs_mbuf != NULL) {
    527 			bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
    528 			m_freem(txs->txs_mbuf);
    529 			txs->txs_mbuf = NULL;
    530 		}
    531 		bus_dmamap_destroy(sc->sc_dmat, txs->txs_dmamap);
    532 	}
    533 	bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
    534 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
    535 	bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_control_data,
    536 	    sizeof(struct ae_control_data));
    537 	bus_dmamem_free(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg);
    538 
    539 	shutdownhook_disestablish(sc->sc_sdhook);
    540 	powerhook_disestablish(sc->sc_powerhook);
    541 
    542 	bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_size);
    543 
    544 
    545 	return (0);
    546 }
    547 
    548 /*
    549  * ae_shutdown:
    550  *
    551  *	Make sure the interface is stopped at reboot time.
    552  */
    553 static void
    554 ae_shutdown(void *arg)
    555 {
    556 	struct ae_softc *sc = arg;
    557 
    558 	ae_stop(&sc->sc_ethercom.ec_if, 1);
    559 }
    560 
    561 /*
    562  * ae_start:		[ifnet interface function]
    563  *
    564  *	Start packet transmission on the interface.
    565  */
    566 static void
    567 ae_start(struct ifnet *ifp)
    568 {
    569 	struct ae_softc *sc = ifp->if_softc;
    570 	struct mbuf *m0, *m;
    571 	struct ae_txsoft *txs, *last_txs = NULL;
    572 	bus_dmamap_t dmamap;
    573 	int error, firsttx, nexttx, lasttx = 1, ofree, seg;
    574 
    575 	DPRINTF(sc, ("%s: ae_start: sc_flags 0x%08x, if_flags 0x%08x\n",
    576 	    sc->sc_dev.dv_xname, sc->sc_flags, ifp->if_flags));
    577 
    578 
    579 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
    580 		return;
    581 
    582 	/*
    583 	 * Remember the previous number of free descriptors and
    584 	 * the first descriptor we'll use.
    585 	 */
    586 	ofree = sc->sc_txfree;
    587 	firsttx = sc->sc_txnext;
    588 
    589 	DPRINTF(sc, ("%s: ae_start: txfree %d, txnext %d\n",
    590 	    sc->sc_dev.dv_xname, ofree, firsttx));
    591 
    592 	/*
    593 	 * Loop through the send queue, setting up transmit descriptors
    594 	 * until we drain the queue, or use up all available transmit
    595 	 * descriptors.
    596 	 */
    597 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txfreeq)) != NULL &&
    598 	       sc->sc_txfree != 0) {
    599 		/*
    600 		 * Grab a packet off the queue.
    601 		 */
    602 		IFQ_POLL(&ifp->if_snd, m0);
    603 		if (m0 == NULL)
    604 			break;
    605 		m = NULL;
    606 
    607 		dmamap = txs->txs_dmamap;
    608 
    609 		/*
    610 		 * Load the DMA map.  If this fails, the packet either
    611 		 * didn't fit in the alloted number of segments, or we were
    612 		 * short on resources.  In this case, we'll copy and try
    613 		 * again.
    614 		 */
    615 		if (((mtod(m0, uintptr_t) & 3) != 0) ||
    616 		    bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
    617 		      BUS_DMA_WRITE|BUS_DMA_NOWAIT) != 0) {
    618 			MGETHDR(m, M_DONTWAIT, MT_DATA);
    619 			if (m == NULL) {
    620 				printf("%s: unable to allocate Tx mbuf\n",
    621 				    sc->sc_dev.dv_xname);
    622 				break;
    623 			}
    624 			MCLAIM(m, &sc->sc_ethercom.ec_tx_mowner);
    625 			if (m0->m_pkthdr.len > MHLEN) {
    626 				MCLGET(m, M_DONTWAIT);
    627 				if ((m->m_flags & M_EXT) == 0) {
    628 					printf("%s: unable to allocate Tx "
    629 					    "cluster\n", sc->sc_dev.dv_xname);
    630 					m_freem(m);
    631 					break;
    632 				}
    633 			}
    634 			m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, void *));
    635 			m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
    636 			error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
    637 			    m, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
    638 			if (error) {
    639 				printf("%s: unable to load Tx buffer, "
    640 				    "error = %d\n", sc->sc_dev.dv_xname,
    641 				    error);
    642 				break;
    643 			}
    644 		}
    645 
    646 		/*
    647 		 * Ensure we have enough descriptors free to describe
    648 		 * the packet.
    649 		 */
    650 		if (dmamap->dm_nsegs > sc->sc_txfree) {
    651 			/*
    652 			 * Not enough free descriptors to transmit this
    653 			 * packet.  We haven't committed to anything yet,
    654 			 * so just unload the DMA map, put the packet
    655 			 * back on the queue, and punt.  Notify the upper
    656 			 * layer that there are no more slots left.
    657 			 *
    658 			 * XXX We could allocate an mbuf and copy, but
    659 			 * XXX it is worth it?
    660 			 */
    661 			ifp->if_flags |= IFF_OACTIVE;
    662 			bus_dmamap_unload(sc->sc_dmat, dmamap);
    663 			if (m != NULL)
    664 				m_freem(m);
    665 			break;
    666 		}
    667 
    668 		IFQ_DEQUEUE(&ifp->if_snd, m0);
    669 		if (m != NULL) {
    670 			m_freem(m0);
    671 			m0 = m;
    672 		}
    673 
    674 		/*
    675 		 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
    676 		 */
    677 
    678 		/* Sync the DMA map. */
    679 		bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
    680 		    BUS_DMASYNC_PREWRITE);
    681 
    682 		/*
    683 		 * Initialize the transmit descriptors.
    684 		 */
    685 		for (nexttx = sc->sc_txnext, seg = 0;
    686 		     seg < dmamap->dm_nsegs;
    687 		     seg++, nexttx = AE_NEXTTX(nexttx)) {
    688 			/*
    689 			 * If this is the first descriptor we're
    690 			 * enqueueing, don't set the OWN bit just
    691 			 * yet.  That could cause a race condition.
    692 			 * We'll do it below.
    693 			 */
    694 			sc->sc_txdescs[nexttx].ad_status =
    695 			    (nexttx == firsttx) ? 0 : ADSTAT_OWN;
    696 			sc->sc_txdescs[nexttx].ad_bufaddr1 =
    697 			    dmamap->dm_segs[seg].ds_addr;
    698 			sc->sc_txdescs[nexttx].ad_ctl =
    699 			    (dmamap->dm_segs[seg].ds_len <<
    700 				ADCTL_SIZE1_SHIFT) |
    701 				(nexttx == (AE_NTXDESC - 1) ?
    702 				    ADCTL_ER : 0);
    703 			lasttx = nexttx;
    704 		}
    705 
    706 		KASSERT(lasttx != -1);
    707 
    708 		/* Set `first segment' and `last segment' appropriately. */
    709 		sc->sc_txdescs[sc->sc_txnext].ad_ctl |= ADCTL_Tx_FS;
    710 		sc->sc_txdescs[lasttx].ad_ctl |= ADCTL_Tx_LS;
    711 
    712 #ifdef AE_DEBUG
    713 		if (ifp->if_flags & IFF_DEBUG) {
    714 			printf("     txsoft %p transmit chain:\n", txs);
    715 			for (seg = sc->sc_txnext;; seg = AE_NEXTTX(seg)) {
    716 				printf("     descriptor %d:\n", seg);
    717 				printf("       ad_status:   0x%08x\n",
    718 				    sc->sc_txdescs[seg].ad_status);
    719 				printf("       ad_ctl:      0x%08x\n",
    720 				    sc->sc_txdescs[seg].ad_ctl);
    721 				printf("       ad_bufaddr1: 0x%08x\n",
    722 				    sc->sc_txdescs[seg].ad_bufaddr1);
    723 				printf("       ad_bufaddr2: 0x%08x\n",
    724 				    sc->sc_txdescs[seg].ad_bufaddr2);
    725 				if (seg == lasttx)
    726 					break;
    727 			}
    728 		}
    729 #endif
    730 
    731 		/* Sync the descriptors we're using. */
    732 		AE_CDTXSYNC(sc, sc->sc_txnext, dmamap->dm_nsegs,
    733 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    734 
    735 		/*
    736 		 * Store a pointer to the packet so we can free it later,
    737 		 * and remember what txdirty will be once the packet is
    738 		 * done.
    739 		 */
    740 		txs->txs_mbuf = m0;
    741 		txs->txs_firstdesc = sc->sc_txnext;
    742 		txs->txs_lastdesc = lasttx;
    743 		txs->txs_ndescs = dmamap->dm_nsegs;
    744 
    745 		/* Advance the tx pointer. */
    746 		sc->sc_txfree -= dmamap->dm_nsegs;
    747 		sc->sc_txnext = nexttx;
    748 
    749 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txfreeq, txs_q);
    750 		SIMPLEQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
    751 
    752 		last_txs = txs;
    753 
    754 #if NBPFILTER > 0
    755 		/*
    756 		 * Pass the packet to any BPF listeners.
    757 		 */
    758 		if (ifp->if_bpf)
    759 			bpf_mtap(ifp->if_bpf, m0);
    760 #endif /* NBPFILTER > 0 */
    761 	}
    762 
    763 	if (txs == NULL || sc->sc_txfree == 0) {
    764 		/* No more slots left; notify upper layer. */
    765 		ifp->if_flags |= IFF_OACTIVE;
    766 	}
    767 
    768 	if (sc->sc_txfree != ofree) {
    769 		DPRINTF(sc, ("%s: packets enqueued, IC on %d, OWN on %d\n",
    770 		    sc->sc_dev.dv_xname, lasttx, firsttx));
    771 		/*
    772 		 * Cause a transmit interrupt to happen on the
    773 		 * last packet we enqueued.
    774 		 */
    775 		sc->sc_txdescs[lasttx].ad_ctl |= ADCTL_Tx_IC;
    776 		AE_CDTXSYNC(sc, lasttx, 1,
    777 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    778 
    779 		/*
    780 		 * The entire packet chain is set up.  Give the
    781 		 * first descriptor to the chip now.
    782 		 */
    783 		sc->sc_txdescs[firsttx].ad_status |= ADSTAT_OWN;
    784 		AE_CDTXSYNC(sc, firsttx, 1,
    785 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    786 
    787 		/* Wake up the transmitter. */
    788 		/* XXX USE AUTOPOLLING? */
    789 		AE_WRITE(sc, CSR_TXPOLL, TXPOLL_TPD);
    790 		AE_BARRIER(sc);
    791 
    792 		/* Set a watchdog timer in case the chip flakes out. */
    793 		ifp->if_timer = 5;
    794 	}
    795 }
    796 
    797 /*
    798  * ae_watchdog:	[ifnet interface function]
    799  *
    800  *	Watchdog timer handler.
    801  */
    802 static void
    803 ae_watchdog(struct ifnet *ifp)
    804 {
    805 	struct ae_softc *sc = ifp->if_softc;
    806 	int doing_transmit;
    807 
    808 	doing_transmit = (! SIMPLEQ_EMPTY(&sc->sc_txdirtyq));
    809 
    810 	if (doing_transmit) {
    811 		printf("%s: transmit timeout\n", sc->sc_dev.dv_xname);
    812 		ifp->if_oerrors++;
    813 	}
    814 	else
    815 		printf("%s: spurious watchdog timeout\n", sc->sc_dev.dv_xname);
    816 
    817 	(void) ae_init(ifp);
    818 
    819 	/* Try to get more packets going. */
    820 	ae_start(ifp);
    821 }
    822 
    823 /*
    824  * ae_ioctl:		[ifnet interface function]
    825  *
    826  *	Handle control requests from the operator.
    827  */
    828 static int
    829 ae_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    830 {
    831 	struct ae_softc *sc = ifp->if_softc;
    832 	struct ifreq *ifr = (struct ifreq *)data;
    833 	int s, error;
    834 
    835 	s = splnet();
    836 
    837 	switch (cmd) {
    838 	case SIOCSIFFLAGS:
    839 		/* If the interface is up and running, only modify the receive
    840 		 * filter when setting promiscuous or debug mode.  Otherwise
    841 		 * fall through to ether_ioctl, which will reset the chip.
    842 		 */
    843 #define RESETIGN (IFF_CANTCHANGE|IFF_DEBUG)
    844 		if (((ifp->if_flags & (IFF_UP|IFF_RUNNING))
    845 		    == (IFF_UP|IFF_RUNNING))
    846 		    && ((ifp->if_flags & (~RESETIGN))
    847 		    == (sc->sc_if_flags & (~RESETIGN)))) {
    848 			/* Set up the receive filter. */
    849 			ae_filter_setup(sc);
    850 			error = 0;
    851 			break;
    852 #undef RESETIGN
    853 		}
    854 		/* FALLTHROUGH */
    855 	default:
    856 		error = ether_ioctl(ifp, cmd, data);
    857 		if (error == ENETRESET) {
    858 			if (ifp->if_flags & IFF_RUNNING) {
    859 				/*
    860 				 * Multicast list has changed.  Set the
    861 				 * hardware filter accordingly.
    862 				 */
    863 				ae_filter_setup(sc);
    864 			}
    865 			error = 0;
    866 		}
    867 		break;
    868 	}
    869 
    870 	/* Try to get more packets going. */
    871 	if (AE_IS_ENABLED(sc))
    872 		ae_start(ifp);
    873 
    874 	sc->sc_if_flags = ifp->if_flags;
    875 	splx(s);
    876 	return (error);
    877 }
    878 
    879 /*
    880  * ae_intr:
    881  *
    882  *	Interrupt service routine.
    883  */
    884 int
    885 ae_intr(void *arg)
    886 {
    887 	struct ae_softc *sc = arg;
    888 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    889 	u_int32_t status, rxstatus, txstatus;
    890 	int handled = 0, txthresh;
    891 
    892 	DPRINTF(sc, ("%s: ae_intr\n", sc->sc_dev.dv_xname));
    893 
    894 #ifdef DEBUG
    895 	if (AE_IS_ENABLED(sc) == 0)
    896 		panic("%s: ae_intr: not enabled", sc->sc_dev.dv_xname);
    897 #endif
    898 
    899 	/*
    900 	 * If the interface isn't running, the interrupt couldn't
    901 	 * possibly have come from us.
    902 	 */
    903 	if ((ifp->if_flags & IFF_RUNNING) == 0 ||
    904 	    !device_is_active(&sc->sc_dev)) {
    905 		printf("spurious?!?\n");
    906 		return (0);
    907 	}
    908 
    909 	for (;;) {
    910 		status = AE_READ(sc, CSR_STATUS);
    911 		if (status) {
    912 			AE_WRITE(sc, CSR_STATUS, status);
    913 			AE_BARRIER(sc);
    914 		}
    915 
    916 		if ((status & sc->sc_inten) == 0)
    917 			break;
    918 
    919 		handled = 1;
    920 
    921 		rxstatus = status & sc->sc_rxint_mask;
    922 		txstatus = status & sc->sc_txint_mask;
    923 
    924 		if (rxstatus) {
    925 			/* Grab new any new packets. */
    926 			ae_rxintr(sc);
    927 
    928 			if (rxstatus & STATUS_RU) {
    929 				printf("%s: receive ring overrun\n",
    930 				    sc->sc_dev.dv_xname);
    931 				/* Get the receive process going again. */
    932 				AE_WRITE(sc, CSR_RXPOLL, RXPOLL_RPD);
    933 				AE_BARRIER(sc);
    934 				break;
    935 			}
    936 		}
    937 
    938 		if (txstatus) {
    939 			/* Sweep up transmit descriptors. */
    940 			ae_txintr(sc);
    941 
    942 			if (txstatus & STATUS_TJT)
    943 				printf("%s: transmit jabber timeout\n",
    944 				    sc->sc_dev.dv_xname);
    945 
    946 			if (txstatus & STATUS_UNF) {
    947 				/*
    948 				 * Increase our transmit threshold if
    949 				 * another is available.
    950 				 */
    951 				txthresh = sc->sc_txthresh + 1;
    952 				if (ae_txthresh[txthresh].txth_name != NULL) {
    953 					uint32_t opmode;
    954 					/* Idle the transmit process. */
    955 					opmode = AE_READ(sc, CSR_OPMODE);
    956 					ae_idle(sc, OPMODE_ST);
    957 
    958 					sc->sc_txthresh = txthresh;
    959 					opmode &=
    960 					    ~(OPMODE_TR|OPMODE_SF);
    961 					opmode |=
    962 					    ae_txthresh[txthresh].txth_opmode;
    963 					printf("%s: transmit underrun; new "
    964 					    "threshold: %s\n",
    965 					    sc->sc_dev.dv_xname,
    966 					    ae_txthresh[txthresh].txth_name);
    967 
    968 					/*
    969 					 * Set the new threshold and restart
    970 					 * the transmit process.
    971 					 */
    972 					AE_WRITE(sc, CSR_OPMODE, opmode);
    973 					AE_BARRIER(sc);
    974 				}
    975 					/*
    976 					 * XXX Log every Nth underrun from
    977 					 * XXX now on?
    978 					 */
    979 			}
    980 		}
    981 
    982 		if (status & (STATUS_TPS|STATUS_RPS)) {
    983 			if (status & STATUS_TPS)
    984 				printf("%s: transmit process stopped\n",
    985 				    sc->sc_dev.dv_xname);
    986 			if (status & STATUS_RPS)
    987 				printf("%s: receive process stopped\n",
    988 				    sc->sc_dev.dv_xname);
    989 			(void) ae_init(ifp);
    990 			break;
    991 		}
    992 
    993 		if (status & STATUS_SE) {
    994 			const char *str;
    995 
    996 			if (status & STATUS_TX_ABORT)
    997 				str = "tx abort";
    998 			else if (status & STATUS_RX_ABORT)
    999 				str = "rx abort";
   1000 			else
   1001 				str = "unknown error";
   1002 
   1003 			printf("%s: fatal system error: %s\n",
   1004 			    sc->sc_dev.dv_xname, str);
   1005 			(void) ae_init(ifp);
   1006 			break;
   1007 		}
   1008 
   1009 		/*
   1010 		 * Not handled:
   1011 		 *
   1012 		 *	Transmit buffer unavailable -- normal
   1013 		 *	condition, nothing to do, really.
   1014 		 *
   1015 		 *	General purpose timer experied -- we don't
   1016 		 *	use the general purpose timer.
   1017 		 *
   1018 		 *	Early receive interrupt -- not available on
   1019 		 *	all chips, we just use RI.  We also only
   1020 		 *	use single-segment receive DMA, so this
   1021 		 *	is mostly useless.
   1022 		 */
   1023 	}
   1024 
   1025 	/* Try to get more packets going. */
   1026 	ae_start(ifp);
   1027 
   1028 #if NRND > 0
   1029 	if (handled)
   1030 		rnd_add_uint32(&sc->sc_rnd_source, status);
   1031 #endif
   1032 	return (handled);
   1033 }
   1034 
   1035 /*
   1036  * ae_rxintr:
   1037  *
   1038  *	Helper; handle receive interrupts.
   1039  */
   1040 static void
   1041 ae_rxintr(struct ae_softc *sc)
   1042 {
   1043 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1044 	struct ether_header *eh;
   1045 	struct ae_rxsoft *rxs;
   1046 	struct mbuf *m;
   1047 	u_int32_t rxstat;
   1048 	int i, len;
   1049 
   1050 	for (i = sc->sc_rxptr;; i = AE_NEXTRX(i)) {
   1051 		rxs = &sc->sc_rxsoft[i];
   1052 
   1053 		AE_CDRXSYNC(sc, i,
   1054 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1055 
   1056 		rxstat = sc->sc_rxdescs[i].ad_status;
   1057 
   1058 		if (rxstat & ADSTAT_OWN) {
   1059 			/*
   1060 			 * We have processed all of the receive buffers.
   1061 			 */
   1062 			break;
   1063 		}
   1064 
   1065 		/*
   1066 		 * If any collisions were seen on the wire, count one.
   1067 		 */
   1068 		if (rxstat & ADSTAT_Rx_CS)
   1069 			ifp->if_collisions++;
   1070 
   1071 		/*
   1072 		 * If an error occurred, update stats, clear the status
   1073 		 * word, and leave the packet buffer in place.  It will
   1074 		 * simply be reused the next time the ring comes around.
   1075 	 	 * If 802.1Q VLAN MTU is enabled, ignore the Frame Too Long
   1076 		 * error.
   1077 		 */
   1078 		if (rxstat & ADSTAT_ES &&
   1079 		    ((sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) == 0 ||
   1080 		     (rxstat & (ADSTAT_Rx_DE | ADSTAT_Rx_RF |
   1081 				ADSTAT_Rx_DB | ADSTAT_Rx_CE)) != 0)) {
   1082 #define	PRINTERR(bit, str)						\
   1083 			if (rxstat & (bit))				\
   1084 				printf("%s: receive error: %s\n",	\
   1085 				    sc->sc_dev.dv_xname, str)
   1086 			ifp->if_ierrors++;
   1087 			PRINTERR(ADSTAT_Rx_DE, "descriptor error");
   1088 			PRINTERR(ADSTAT_Rx_RF, "runt frame");
   1089 			PRINTERR(ADSTAT_Rx_TL, "frame too long");
   1090 			PRINTERR(ADSTAT_Rx_RE, "MII error");
   1091 			PRINTERR(ADSTAT_Rx_DB, "dribbling bit");
   1092 			PRINTERR(ADSTAT_Rx_CE, "CRC error");
   1093 #undef PRINTERR
   1094 			AE_INIT_RXDESC(sc, i);
   1095 			continue;
   1096 		}
   1097 
   1098 		bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1099 		    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1100 
   1101 		/*
   1102 		 * No errors; receive the packet.  Note the chip
   1103 		 * includes the CRC with every packet.
   1104 		 */
   1105 		len = ADSTAT_Rx_LENGTH(rxstat) - ETHER_CRC_LEN;
   1106 
   1107 		/*
   1108 		 * XXX: the Atheros part can align on half words.  what
   1109 		 * is the performance implication of this?  Probably
   1110 		 * minimal, and we should use it...
   1111 		 */
   1112 #ifdef __NO_STRICT_ALIGNMENT
   1113 		/*
   1114 		 * Allocate a new mbuf cluster.  If that fails, we are
   1115 		 * out of memory, and must drop the packet and recycle
   1116 		 * the buffer that's already attached to this descriptor.
   1117 		 */
   1118 		m = rxs->rxs_mbuf;
   1119 		if (ae_add_rxbuf(sc, i) != 0) {
   1120 			ifp->if_ierrors++;
   1121 			AE_INIT_RXDESC(sc, i);
   1122 			bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1123 			    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1124 			continue;
   1125 		}
   1126 #else
   1127 		/*
   1128 		 * The chip's receive buffers must be 4-byte aligned.
   1129 		 * But this means that the data after the Ethernet header
   1130 		 * is misaligned.  We must allocate a new buffer and
   1131 		 * copy the data, shifted forward 2 bytes.
   1132 		 */
   1133 		MGETHDR(m, M_DONTWAIT, MT_DATA);
   1134 		if (m == NULL) {
   1135  dropit:
   1136 			ifp->if_ierrors++;
   1137 			AE_INIT_RXDESC(sc, i);
   1138 			bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1139 			    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1140 			continue;
   1141 		}
   1142 		MCLAIM(m, &sc->sc_ethercom.ec_rx_mowner);
   1143 		if (len > (MHLEN - 2)) {
   1144 			MCLGET(m, M_DONTWAIT);
   1145 			if ((m->m_flags & M_EXT) == 0) {
   1146 				m_freem(m);
   1147 				goto dropit;
   1148 			}
   1149 		}
   1150 		m->m_data += 2;
   1151 
   1152 		/*
   1153 		 * Note that we use clusters for incoming frames, so the
   1154 		 * buffer is virtually contiguous.
   1155 		 */
   1156 		memcpy(mtod(m, void *), mtod(rxs->rxs_mbuf, void *), len);
   1157 
   1158 		/* Allow the receive descriptor to continue using its mbuf. */
   1159 		AE_INIT_RXDESC(sc, i);
   1160 		bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1161 		    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1162 #endif /* __NO_STRICT_ALIGNMENT */
   1163 
   1164 		ifp->if_ipackets++;
   1165 		eh = mtod(m, struct ether_header *);
   1166 		m->m_pkthdr.rcvif = ifp;
   1167 		m->m_pkthdr.len = m->m_len = len;
   1168 
   1169 #if NBPFILTER > 0
   1170 		/*
   1171 		 * Pass this up to any BPF listeners, but only
   1172 		 * pass it up the stack if its for us.
   1173 		 */
   1174 		if (ifp->if_bpf)
   1175 			bpf_mtap(ifp->if_bpf, m);
   1176 #endif /* NBPFILTER > 0 */
   1177 
   1178 		/* Pass it on. */
   1179 		(*ifp->if_input)(ifp, m);
   1180 	}
   1181 
   1182 	/* Update the receive pointer. */
   1183 	sc->sc_rxptr = i;
   1184 }
   1185 
   1186 /*
   1187  * ae_txintr:
   1188  *
   1189  *	Helper; handle transmit interrupts.
   1190  */
   1191 static void
   1192 ae_txintr(struct ae_softc *sc)
   1193 {
   1194 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1195 	struct ae_txsoft *txs;
   1196 	u_int32_t txstat;
   1197 
   1198 	DPRINTF(sc, ("%s: ae_txintr: sc_flags 0x%08x\n",
   1199 	    sc->sc_dev.dv_xname, sc->sc_flags));
   1200 
   1201 	ifp->if_flags &= ~IFF_OACTIVE;
   1202 
   1203 	/*
   1204 	 * Go through our Tx list and free mbufs for those
   1205 	 * frames that have been transmitted.
   1206 	 */
   1207 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
   1208 		AE_CDTXSYNC(sc, txs->txs_lastdesc,
   1209 		    txs->txs_ndescs,
   1210 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1211 
   1212 #ifdef AE_DEBUG
   1213 		if (ifp->if_flags & IFF_DEBUG) {
   1214 			int i;
   1215 			printf("    txsoft %p transmit chain:\n", txs);
   1216 			for (i = txs->txs_firstdesc;; i = AE_NEXTTX(i)) {
   1217 				printf("     descriptor %d:\n", i);
   1218 				printf("       ad_status:   0x%08x\n",
   1219 				    sc->sc_txdescs[i].ad_status);
   1220 				printf("       ad_ctl:      0x%08x\n",
   1221 				    sc->sc_txdescs[i].ad_ctl);
   1222 				printf("       ad_bufaddr1: 0x%08x\n",
   1223 				    sc->sc_txdescs[i].ad_bufaddr1);
   1224 				printf("       ad_bufaddr2: 0x%08x\n",
   1225 				    sc->sc_txdescs[i].ad_bufaddr2);
   1226 				if (i == txs->txs_lastdesc)
   1227 					break;
   1228 			}
   1229 		}
   1230 #endif
   1231 
   1232 		txstat = sc->sc_txdescs[txs->txs_lastdesc].ad_status;
   1233 		if (txstat & ADSTAT_OWN)
   1234 			break;
   1235 
   1236 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
   1237 
   1238 		sc->sc_txfree += txs->txs_ndescs;
   1239 
   1240 		bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
   1241 		    0, txs->txs_dmamap->dm_mapsize,
   1242 		    BUS_DMASYNC_POSTWRITE);
   1243 		bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
   1244 		m_freem(txs->txs_mbuf);
   1245 		txs->txs_mbuf = NULL;
   1246 
   1247 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
   1248 
   1249 		/*
   1250 		 * Check for errors and collisions.
   1251 		 */
   1252 #ifdef AE_STATS
   1253 		if (txstat & ADSTAT_Tx_UF)
   1254 			sc->sc_stats.ts_tx_uf++;
   1255 		if (txstat & ADSTAT_Tx_TO)
   1256 			sc->sc_stats.ts_tx_to++;
   1257 		if (txstat & ADSTAT_Tx_EC)
   1258 			sc->sc_stats.ts_tx_ec++;
   1259 		if (txstat & ADSTAT_Tx_LC)
   1260 			sc->sc_stats.ts_tx_lc++;
   1261 #endif
   1262 
   1263 		if (txstat & (ADSTAT_Tx_UF|ADSTAT_Tx_TO))
   1264 			ifp->if_oerrors++;
   1265 
   1266 		if (txstat & ADSTAT_Tx_EC)
   1267 			ifp->if_collisions += 16;
   1268 		else
   1269 			ifp->if_collisions += ADSTAT_Tx_COLLISIONS(txstat);
   1270 		if (txstat & ADSTAT_Tx_LC)
   1271 			ifp->if_collisions++;
   1272 
   1273 		ifp->if_opackets++;
   1274 	}
   1275 
   1276 	/*
   1277 	 * If there are no more pending transmissions, cancel the watchdog
   1278 	 * timer.
   1279 	 */
   1280 	if (txs == NULL)
   1281 		ifp->if_timer = 0;
   1282 }
   1283 
   1284 #ifdef AE_STATS
   1285 void
   1286 ae_print_stats(struct ae_softc *sc)
   1287 {
   1288 
   1289 	printf("%s: tx_uf %lu, tx_to %lu, tx_ec %lu, tx_lc %lu\n",
   1290 	    sc->sc_dev.dv_xname,
   1291 	    sc->sc_stats.ts_tx_uf, sc->sc_stats.ts_tx_to,
   1292 	    sc->sc_stats.ts_tx_ec, sc->sc_stats.ts_tx_lc);
   1293 }
   1294 #endif
   1295 
   1296 /*
   1297  * ae_reset:
   1298  *
   1299  *	Perform a soft reset on the chip.
   1300  */
   1301 void
   1302 ae_reset(struct ae_softc *sc)
   1303 {
   1304 	int i;
   1305 
   1306 	AE_WRITE(sc, CSR_BUSMODE, BUSMODE_SWR);
   1307 	AE_BARRIER(sc);
   1308 
   1309 	/*
   1310 	 * The chip doesn't take itself out of reset automatically.
   1311 	 * We need to do so after 2us.
   1312 	 */
   1313 	delay(10);
   1314 	AE_WRITE(sc, CSR_BUSMODE, 0);
   1315 	AE_BARRIER(sc);
   1316 
   1317 	for (i = 0; i < 1000; i++) {
   1318 		/*
   1319 		 * Wait a bit for the reset to complete before peeking
   1320 		 * at the chip again.
   1321 		 */
   1322 		delay(10);
   1323 		if (AE_ISSET(sc, CSR_BUSMODE, BUSMODE_SWR) == 0)
   1324 			break;
   1325 	}
   1326 
   1327 	if (AE_ISSET(sc, CSR_BUSMODE, BUSMODE_SWR))
   1328 		printf("%s: reset failed to complete\n", sc->sc_dev.dv_xname);
   1329 
   1330 	delay(1000);
   1331 }
   1332 
   1333 /*
   1334  * ae_init:		[ ifnet interface function ]
   1335  *
   1336  *	Initialize the interface.  Must be called at splnet().
   1337  */
   1338 static int
   1339 ae_init(struct ifnet *ifp)
   1340 {
   1341 	struct ae_softc *sc = ifp->if_softc;
   1342 	struct ae_txsoft *txs;
   1343 	struct ae_rxsoft *rxs;
   1344 	const uint8_t *enaddr;
   1345 	int i, error = 0;
   1346 
   1347 	if ((error = ae_enable(sc)) != 0)
   1348 		goto out;
   1349 
   1350 	/*
   1351 	 * Cancel any pending I/O.
   1352 	 */
   1353 	ae_stop(ifp, 0);
   1354 
   1355 	/*
   1356 	 * Reset the chip to a known state.
   1357 	 */
   1358 	ae_reset(sc);
   1359 
   1360 	/*
   1361 	 * Initialize the BUSMODE register.
   1362 	 */
   1363 	AE_WRITE(sc, CSR_BUSMODE,
   1364 	    /* XXX: not sure if this is a good thing or not... */
   1365 	    //BUSMODE_ALIGN_16B |
   1366 	    BUSMODE_BAR | BUSMODE_BLE | BUSMODE_PBL_4LW);
   1367 	AE_BARRIER(sc);
   1368 
   1369 	/*
   1370 	 * Initialize the transmit descriptor ring.
   1371 	 */
   1372 	memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
   1373 	for (i = 0; i < AE_NTXDESC; i++) {
   1374 		sc->sc_txdescs[i].ad_ctl = 0;
   1375 		sc->sc_txdescs[i].ad_bufaddr2 =
   1376 		    AE_CDTXADDR(sc, AE_NEXTTX(i));
   1377 	}
   1378 	sc->sc_txdescs[AE_NTXDESC - 1].ad_ctl |= ADCTL_ER;
   1379 	AE_CDTXSYNC(sc, 0, AE_NTXDESC,
   1380 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1381 	sc->sc_txfree = AE_NTXDESC;
   1382 	sc->sc_txnext = 0;
   1383 
   1384 	/*
   1385 	 * Initialize the transmit job descriptors.
   1386 	 */
   1387 	SIMPLEQ_INIT(&sc->sc_txfreeq);
   1388 	SIMPLEQ_INIT(&sc->sc_txdirtyq);
   1389 	for (i = 0; i < AE_TXQUEUELEN; i++) {
   1390 		txs = &sc->sc_txsoft[i];
   1391 		txs->txs_mbuf = NULL;
   1392 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
   1393 	}
   1394 
   1395 	/*
   1396 	 * Initialize the receive descriptor and receive job
   1397 	 * descriptor rings.
   1398 	 */
   1399 	for (i = 0; i < AE_NRXDESC; i++) {
   1400 		rxs = &sc->sc_rxsoft[i];
   1401 		if (rxs->rxs_mbuf == NULL) {
   1402 			if ((error = ae_add_rxbuf(sc, i)) != 0) {
   1403 				printf("%s: unable to allocate or map rx "
   1404 				    "buffer %d, error = %d\n",
   1405 				    sc->sc_dev.dv_xname, i, error);
   1406 				/*
   1407 				 * XXX Should attempt to run with fewer receive
   1408 				 * XXX buffers instead of just failing.
   1409 				 */
   1410 				ae_rxdrain(sc);
   1411 				goto out;
   1412 			}
   1413 		} else
   1414 			AE_INIT_RXDESC(sc, i);
   1415 	}
   1416 	sc->sc_rxptr = 0;
   1417 
   1418 	/*
   1419 	 * Initialize the interrupt mask and enable interrupts.
   1420 	 */
   1421 	/* normal interrupts */
   1422 	sc->sc_inten =  STATUS_TI | STATUS_TU | STATUS_RI | STATUS_NIS;
   1423 
   1424 	/* abnormal interrupts */
   1425 	sc->sc_inten |= STATUS_TPS | STATUS_TJT | STATUS_UNF |
   1426 	    STATUS_RU | STATUS_RPS | STATUS_SE | STATUS_AIS;
   1427 
   1428 	sc->sc_rxint_mask = STATUS_RI|STATUS_RU;
   1429 	sc->sc_txint_mask = STATUS_TI|STATUS_UNF|STATUS_TJT;
   1430 
   1431 	sc->sc_rxint_mask &= sc->sc_inten;
   1432 	sc->sc_txint_mask &= sc->sc_inten;
   1433 
   1434 	AE_WRITE(sc, CSR_INTEN, sc->sc_inten);
   1435 	AE_WRITE(sc, CSR_STATUS, 0xffffffff);
   1436 
   1437 	/*
   1438 	 * Give the transmit and receive rings to the chip.
   1439 	 */
   1440 	AE_WRITE(sc, CSR_TXLIST, AE_CDTXADDR(sc, sc->sc_txnext));
   1441 	AE_WRITE(sc, CSR_RXLIST, AE_CDRXADDR(sc, sc->sc_rxptr));
   1442 	AE_BARRIER(sc);
   1443 
   1444 	/*
   1445 	 * Set the station address.
   1446 	 */
   1447 	enaddr = CLLADDR(ifp->if_sadl);
   1448 	AE_WRITE(sc, CSR_MACHI, enaddr[5] << 16 | enaddr[4]);
   1449 	AE_WRITE(sc, CSR_MACLO, enaddr[3] << 24 | enaddr[2] << 16 |
   1450 		enaddr[1] << 8 | enaddr[0]);
   1451 	AE_BARRIER(sc);
   1452 
   1453 	/*
   1454 	 * Set the receive filter.  This will start the transmit and
   1455 	 * receive processes.
   1456 	 */
   1457 	ae_filter_setup(sc);
   1458 
   1459 	/*
   1460 	 * Set the current media.
   1461 	 */
   1462 	if ((error = ether_mediachange(ifp)) != 0)
   1463 		goto out;
   1464 
   1465 	/*
   1466 	 * Start the mac.
   1467 	 */
   1468 	AE_SET(sc, CSR_MACCTL, MACCTL_RE | MACCTL_TE);
   1469 	AE_BARRIER(sc);
   1470 
   1471 	/*
   1472 	 * Write out the opmode.
   1473 	 */
   1474 	AE_WRITE(sc, CSR_OPMODE, OPMODE_SR | OPMODE_ST |
   1475 	    ae_txthresh[sc->sc_txthresh].txth_opmode);
   1476 	/*
   1477 	 * Start the receive process.
   1478 	 */
   1479 	AE_WRITE(sc, CSR_RXPOLL, RXPOLL_RPD);
   1480 	AE_BARRIER(sc);
   1481 
   1482 	if (sc->sc_tick != NULL) {
   1483 		/* Start the one second clock. */
   1484 		callout_reset(&sc->sc_tick_callout, hz >> 3, sc->sc_tick, sc);
   1485 	}
   1486 
   1487 	/*
   1488 	 * Note that the interface is now running.
   1489 	 */
   1490 	ifp->if_flags |= IFF_RUNNING;
   1491 	ifp->if_flags &= ~IFF_OACTIVE;
   1492 	sc->sc_if_flags = ifp->if_flags;
   1493 
   1494  out:
   1495 	if (error) {
   1496 		ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1497 		ifp->if_timer = 0;
   1498 		printf("%s: interface not running\n", sc->sc_dev.dv_xname);
   1499 	}
   1500 	return (error);
   1501 }
   1502 
   1503 /*
   1504  * ae_enable:
   1505  *
   1506  *	Enable the chip.
   1507  */
   1508 static int
   1509 ae_enable(struct ae_softc *sc)
   1510 {
   1511 
   1512 	if (AE_IS_ENABLED(sc) == 0) {
   1513 		sc->sc_ih = arbus_intr_establish(sc->sc_cirq, sc->sc_mirq,
   1514 		    ae_intr, sc);
   1515 		if (sc->sc_ih == NULL) {
   1516 			printf("%s: unable to establish interrupt\n",
   1517 			    sc->sc_dev.dv_xname);
   1518 			return (EIO);
   1519 		}
   1520 		sc->sc_flags |= AE_ENABLED;
   1521 	}
   1522 	return (0);
   1523 }
   1524 
   1525 /*
   1526  * ae_disable:
   1527  *
   1528  *	Disable the chip.
   1529  */
   1530 static void
   1531 ae_disable(struct ae_softc *sc)
   1532 {
   1533 
   1534 	if (AE_IS_ENABLED(sc)) {
   1535 		arbus_intr_disestablish(sc->sc_ih);
   1536 		sc->sc_flags &= ~AE_ENABLED;
   1537 	}
   1538 }
   1539 
   1540 /*
   1541  * ae_power:
   1542  *
   1543  *	Power management (suspend/resume) hook.
   1544  */
   1545 static void
   1546 ae_power(int why, void *arg)
   1547 {
   1548 	struct ae_softc *sc = arg;
   1549 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1550 	int s;
   1551 
   1552 	printf("power called: %d, %x\n", why, (uint32_t)arg);
   1553 	s = splnet();
   1554 	switch (why) {
   1555 	case PWR_STANDBY:
   1556 		/* do nothing! */
   1557 		break;
   1558 	case PWR_SUSPEND:
   1559 		ae_stop(ifp, 0);
   1560 		ae_disable(sc);
   1561 		break;
   1562 	case PWR_RESUME:
   1563 		if (ifp->if_flags & IFF_UP) {
   1564 			ae_enable(sc);
   1565 			ae_init(ifp);
   1566 		}
   1567 		break;
   1568 	case PWR_SOFTSUSPEND:
   1569 	case PWR_SOFTSTANDBY:
   1570 	case PWR_SOFTRESUME:
   1571 		break;
   1572 	}
   1573 	splx(s);
   1574 }
   1575 
   1576 /*
   1577  * ae_rxdrain:
   1578  *
   1579  *	Drain the receive queue.
   1580  */
   1581 static void
   1582 ae_rxdrain(struct ae_softc *sc)
   1583 {
   1584 	struct ae_rxsoft *rxs;
   1585 	int i;
   1586 
   1587 	for (i = 0; i < AE_NRXDESC; i++) {
   1588 		rxs = &sc->sc_rxsoft[i];
   1589 		if (rxs->rxs_mbuf != NULL) {
   1590 			bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
   1591 			m_freem(rxs->rxs_mbuf);
   1592 			rxs->rxs_mbuf = NULL;
   1593 		}
   1594 	}
   1595 }
   1596 
   1597 /*
   1598  * ae_stop:		[ ifnet interface function ]
   1599  *
   1600  *	Stop transmission on the interface.
   1601  */
   1602 static void
   1603 ae_stop(struct ifnet *ifp, int disable)
   1604 {
   1605 	struct ae_softc *sc = ifp->if_softc;
   1606 	struct ae_txsoft *txs;
   1607 
   1608 	if (sc->sc_tick != NULL) {
   1609 		/* Stop the one second clock. */
   1610 		callout_stop(&sc->sc_tick_callout);
   1611 	}
   1612 
   1613 	/* Down the MII. */
   1614 	mii_down(&sc->sc_mii);
   1615 
   1616 	/* Disable interrupts. */
   1617 	AE_WRITE(sc, CSR_INTEN, 0);
   1618 
   1619 	/* Stop the transmit and receive processes. */
   1620 	AE_WRITE(sc, CSR_OPMODE, 0);
   1621 	AE_WRITE(sc, CSR_RXLIST, 0);
   1622 	AE_WRITE(sc, CSR_TXLIST, 0);
   1623 	AE_CLR(sc, CSR_MACCTL, MACCTL_TE | MACCTL_RE);
   1624 	AE_BARRIER(sc);
   1625 
   1626 	/*
   1627 	 * Release any queued transmit buffers.
   1628 	 */
   1629 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
   1630 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
   1631 		if (txs->txs_mbuf != NULL) {
   1632 			bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
   1633 			m_freem(txs->txs_mbuf);
   1634 			txs->txs_mbuf = NULL;
   1635 		}
   1636 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
   1637 	}
   1638 
   1639 	if (disable) {
   1640 		ae_rxdrain(sc);
   1641 		ae_disable(sc);
   1642 	}
   1643 
   1644 	/*
   1645 	 * Mark the interface down and cancel the watchdog timer.
   1646 	 */
   1647 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1648 	sc->sc_if_flags = ifp->if_flags;
   1649 	ifp->if_timer = 0;
   1650 
   1651 	/*
   1652 	 * Reset the chip (needed on some flavors to actually disable it).
   1653 	 */
   1654 	ae_reset(sc);
   1655 }
   1656 
   1657 /*
   1658  * ae_add_rxbuf:
   1659  *
   1660  *	Add a receive buffer to the indicated descriptor.
   1661  */
   1662 static int
   1663 ae_add_rxbuf(struct ae_softc *sc, int idx)
   1664 {
   1665 	struct ae_rxsoft *rxs = &sc->sc_rxsoft[idx];
   1666 	struct mbuf *m;
   1667 	int error;
   1668 
   1669 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1670 	if (m == NULL)
   1671 		return (ENOBUFS);
   1672 
   1673 	MCLAIM(m, &sc->sc_ethercom.ec_rx_mowner);
   1674 	MCLGET(m, M_DONTWAIT);
   1675 	if ((m->m_flags & M_EXT) == 0) {
   1676 		m_freem(m);
   1677 		return (ENOBUFS);
   1678 	}
   1679 
   1680 	if (rxs->rxs_mbuf != NULL)
   1681 		bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
   1682 
   1683 	rxs->rxs_mbuf = m;
   1684 
   1685 	error = bus_dmamap_load(sc->sc_dmat, rxs->rxs_dmamap,
   1686 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL,
   1687 	    BUS_DMA_READ|BUS_DMA_NOWAIT);
   1688 	if (error) {
   1689 		printf("%s: can't load rx DMA map %d, error = %d\n",
   1690 		    sc->sc_dev.dv_xname, idx, error);
   1691 		panic("ae_add_rxbuf");	/* XXX */
   1692 	}
   1693 
   1694 	bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1695 	    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1696 
   1697 	AE_INIT_RXDESC(sc, idx);
   1698 
   1699 	return (0);
   1700 }
   1701 
   1702 /*
   1703  * ae_filter_setup:
   1704  *
   1705  *	Set the chip's receive filter.
   1706  */
   1707 static void
   1708 ae_filter_setup(struct ae_softc *sc)
   1709 {
   1710 	struct ethercom *ec = &sc->sc_ethercom;
   1711 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1712 	struct ether_multi *enm;
   1713 	struct ether_multistep step;
   1714 	uint32_t hash, mchash[2];
   1715 	uint32_t macctl = 0;
   1716 
   1717 	/*
   1718 	 * If the chip is running, we need to reset the interface,
   1719 	 * and will revisit here (with IFF_RUNNING) clear.  The
   1720 	 * chip seems to really not like to have its multicast
   1721 	 * filter programmed without a reset.
   1722 	 */
   1723 	if (ifp->if_flags & IFF_RUNNING) {
   1724 		(void) ae_init(ifp);
   1725 		return;
   1726 	}
   1727 
   1728 	DPRINTF(sc, ("%s: ae_filter_setup: sc_flags 0x%08x\n",
   1729 	    sc->sc_dev.dv_xname, sc->sc_flags));
   1730 
   1731 	macctl = AE_READ(sc, CSR_MACCTL);
   1732 	macctl &= ~(MACCTL_PR | MACCTL_PM);
   1733 	macctl |= MACCTL_HASH;
   1734 	macctl |= MACCTL_HBD;
   1735 	macctl |= MACCTL_PR;
   1736 
   1737 	if (ifp->if_flags & IFF_PROMISC) {
   1738 		macctl |= MACCTL_PR;
   1739 		goto allmulti;
   1740 	}
   1741 
   1742 	mchash[0] = mchash[1] = 0;
   1743 
   1744 	ETHER_FIRST_MULTI(step, ec, enm);
   1745 	while (enm != NULL) {
   1746 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   1747 			/*
   1748 			 * We must listen to a range of multicast addresses.
   1749 			 * For now, just accept all multicasts, rather than
   1750 			 * trying to set only those filter bits needed to match
   1751 			 * the range.  (At this time, the only use of address
   1752 			 * ranges is for IP multicast routing, for which the
   1753 			 * range is big enough to require all bits set.)
   1754 			 */
   1755 			goto allmulti;
   1756 		}
   1757 
   1758 		/* Verify whether we use big or little endian hashes */
   1759 		hash = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN) & 0x3f;
   1760 		mchash[hash >> 5] |= 1 << (hash & 0x1f);
   1761 		ETHER_NEXT_MULTI(step, enm);
   1762 	}
   1763 	ifp->if_flags &= ~IFF_ALLMULTI;
   1764 	goto setit;
   1765 
   1766  allmulti:
   1767 	ifp->if_flags |= IFF_ALLMULTI;
   1768 	mchash[0] = mchash[1] = 0xffffffff;
   1769 	macctl |= MACCTL_PM;
   1770 
   1771  setit:
   1772 	AE_WRITE(sc, CSR_HTHI, mchash[0]);
   1773 	AE_WRITE(sc, CSR_HTHI, mchash[1]);
   1774 
   1775 	AE_WRITE(sc, CSR_MACCTL, macctl);
   1776 	AE_BARRIER(sc);
   1777 
   1778 	DPRINTF(sc, ("%s: ae_filter_setup: returning %x\n",
   1779 		    sc->sc_dev.dv_xname, macctl));
   1780 }
   1781 
   1782 /*
   1783  * ae_idle:
   1784  *
   1785  *	Cause the transmit and/or receive processes to go idle.
   1786  */
   1787 void
   1788 ae_idle(struct ae_softc *sc, u_int32_t bits)
   1789 {
   1790 	static const char * const txstate_names[] = {
   1791 		"STOPPED",
   1792 		"RUNNING - FETCH",
   1793 		"RUNNING - WAIT",
   1794 		"RUNNING - READING",
   1795 		"-- RESERVED --",
   1796 		"RUNNING - SETUP",
   1797 		"SUSPENDED",
   1798 		"RUNNING - CLOSE",
   1799 	};
   1800 	static const char * const rxstate_names[] = {
   1801 		"STOPPED",
   1802 		"RUNNING - FETCH",
   1803 		"RUNNING - CHECK",
   1804 		"RUNNING - WAIT",
   1805 		"SUSPENDED",
   1806 		"RUNNING - CLOSE",
   1807 		"RUNNING - FLUSH",
   1808 		"RUNNING - QUEUE",
   1809 	};
   1810 
   1811 	u_int32_t csr, ackmask = 0;
   1812 	int i;
   1813 
   1814 	if (bits & OPMODE_ST)
   1815 		ackmask |= STATUS_TPS;
   1816 
   1817 	if (bits & OPMODE_SR)
   1818 		ackmask |= STATUS_RPS;
   1819 
   1820 	AE_CLR(sc, CSR_OPMODE, bits);
   1821 
   1822 	for (i = 0; i < 1000; i++) {
   1823 		if (AE_ISSET(sc, CSR_STATUS, ackmask) == ackmask)
   1824 			break;
   1825 		delay(10);
   1826 	}
   1827 
   1828 	csr = AE_READ(sc, CSR_STATUS);
   1829 	if ((csr & ackmask) != ackmask) {
   1830 		if ((bits & OPMODE_ST) != 0 && (csr & STATUS_TPS) == 0 &&
   1831 		    (csr & STATUS_TS) != STATUS_TS_STOPPED) {
   1832 			printf("%s: transmit process failed to idle: "
   1833 			    "state %s\n", sc->sc_dev.dv_xname,
   1834 			    txstate_names[(csr & STATUS_TS) >> 20]);
   1835 		}
   1836 		if ((bits & OPMODE_SR) != 0 && (csr & STATUS_RPS) == 0 &&
   1837 		    (csr & STATUS_RS) != STATUS_RS_STOPPED) {
   1838 			printf("%s: receive process failed to idle: "
   1839 			    "state %s\n", sc->sc_dev.dv_xname,
   1840 			    rxstate_names[(csr & STATUS_RS) >> 17]);
   1841 		}
   1842 	}
   1843 }
   1844 
   1845 /*****************************************************************************
   1846  * Support functions for MII-attached media.
   1847  *****************************************************************************/
   1848 
   1849 /*
   1850  * ae_mii_tick:
   1851  *
   1852  *	One second timer, used to tick the MII.
   1853  */
   1854 static void
   1855 ae_mii_tick(void *arg)
   1856 {
   1857 	struct ae_softc *sc = arg;
   1858 	int s;
   1859 
   1860 	if (!device_is_active(&sc->sc_dev))
   1861 		return;
   1862 
   1863 	s = splnet();
   1864 	mii_tick(&sc->sc_mii);
   1865 	splx(s);
   1866 
   1867 	callout_reset(&sc->sc_tick_callout, hz, sc->sc_tick, sc);
   1868 }
   1869 
   1870 /*
   1871  * ae_mii_statchg:	[mii interface function]
   1872  *
   1873  *	Callback from PHY when media changes.
   1874  */
   1875 static void
   1876 ae_mii_statchg(struct device *self)
   1877 {
   1878 	struct ae_softc *sc = (struct ae_softc *)self;
   1879 	uint32_t	macctl, flowc;
   1880 
   1881 	//opmode = AE_READ(sc, CSR_OPMODE);
   1882 	macctl = AE_READ(sc, CSR_MACCTL);
   1883 
   1884 	/* XXX: do we need to do this? */
   1885 	/* Idle the transmit and receive processes. */
   1886 	//ae_idle(sc, OPMODE_ST|OPMODE_SR);
   1887 
   1888 	if (sc->sc_mii.mii_media_active & IFM_FDX) {
   1889 		flowc = FLOWC_FCE;
   1890 		macctl &= ~MACCTL_DRO;
   1891 		macctl |= MACCTL_FDX;
   1892 	} else {
   1893 		flowc = 0;	/* cannot do flow control in HDX */
   1894 		macctl |= MACCTL_DRO;
   1895 		macctl &= ~MACCTL_FDX;
   1896 	}
   1897 
   1898 	AE_WRITE(sc, CSR_FLOWC, flowc);
   1899 	AE_WRITE(sc, CSR_MACCTL, macctl);
   1900 
   1901 	/* restore operational mode */
   1902 	//AE_WRITE(sc, CSR_OPMODE, opmode);
   1903 	AE_BARRIER(sc);
   1904 }
   1905 
   1906 /*
   1907  * ae_mii_readreg:
   1908  *
   1909  *	Read a PHY register.
   1910  */
   1911 static int
   1912 ae_mii_readreg(struct device *self, int phy, int reg)
   1913 {
   1914 	struct ae_softc	*sc = (struct ae_softc *)self;
   1915 	uint32_t	addr;
   1916 	int		i;
   1917 
   1918 	addr = (phy << MIIADDR_PHY_SHIFT) | (reg << MIIADDR_REG_SHIFT);
   1919 	AE_WRITE(sc, CSR_MIIADDR, addr);
   1920 	AE_BARRIER(sc);
   1921 	for (i = 0; i < 100000000; i++) {
   1922 		if ((AE_READ(sc, CSR_MIIADDR) & MIIADDR_BUSY) == 0)
   1923 			break;
   1924 	}
   1925 
   1926 	return (AE_READ(sc, CSR_MIIDATA) & 0xffff);
   1927 }
   1928 
   1929 /*
   1930  * ae_mii_writereg:
   1931  *
   1932  *	Write a PHY register.
   1933  */
   1934 static void
   1935 ae_mii_writereg(struct device *self, int phy, int reg, int val)
   1936 {
   1937 	struct ae_softc *sc = (struct ae_softc *)self;
   1938 	uint32_t	addr;
   1939 	int		i;
   1940 
   1941 	/* write the data register */
   1942 	AE_WRITE(sc, CSR_MIIDATA, val);
   1943 
   1944 	/* write the address to latch it in */
   1945 	addr = (phy << MIIADDR_PHY_SHIFT) | (reg << MIIADDR_REG_SHIFT) |
   1946 	    MIIADDR_WRITE;
   1947 	AE_WRITE(sc, CSR_MIIADDR, addr);
   1948 	AE_BARRIER(sc);
   1949 
   1950 	for (i = 0; i < 100000000; i++) {
   1951 		if ((AE_READ(sc, CSR_MIIADDR) & MIIADDR_BUSY) == 0)
   1952 			break;
   1953 	}
   1954 }
   1955