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