Home | History | Annotate | Line # | Download | only in ic
dwc_gmac.c revision 1.55
      1  1.54   aymeric /* $NetBSD: dwc_gmac.c,v 1.55 2018/10/08 17:09:31 martin Exp $ */
      2  1.18  jmcneill 
      3   1.1    martin /*-
      4   1.1    martin  * Copyright (c) 2013, 2014 The NetBSD Foundation, Inc.
      5   1.1    martin  * All rights reserved.
      6   1.1    martin  *
      7   1.1    martin  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1    martin  * by Matt Thomas of 3am Software Foundry and Martin Husemann.
      9   1.1    martin  *
     10   1.1    martin  * Redistribution and use in source and binary forms, with or without
     11   1.1    martin  * modification, are permitted provided that the following conditions
     12   1.1    martin  * are met:
     13   1.1    martin  * 1. Redistributions of source code must retain the above copyright
     14   1.1    martin  *    notice, this list of conditions and the following disclaimer.
     15   1.1    martin  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1    martin  *    notice, this list of conditions and the following disclaimer in the
     17   1.1    martin  *    documentation and/or other materials provided with the distribution.
     18   1.1    martin  *
     19   1.1    martin  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1    martin  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1    martin  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1    martin  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1    martin  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1    martin  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1    martin  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1    martin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1    martin  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1    martin  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1    martin  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1    martin  */
     31   1.1    martin 
     32   1.1    martin /*
     33   1.1    martin  * This driver supports the Synopsis Designware GMAC core, as found
     34   1.1    martin  * on Allwinner A20 cores and others.
     35   1.1    martin  *
     36   1.1    martin  * Real documentation seems to not be available, the marketing product
     37   1.1    martin  * documents could be found here:
     38   1.1    martin  *
     39   1.1    martin  *  http://www.synopsys.com/dw/ipdir.php?ds=dwc_ether_mac10_100_1000_unive
     40   1.1    martin  */
     41   1.1    martin 
     42   1.1    martin #include <sys/cdefs.h>
     43   1.1    martin 
     44  1.54   aymeric __KERNEL_RCSID(1, "$NetBSD: dwc_gmac.c,v 1.55 2018/10/08 17:09:31 martin Exp $");
     45   1.7    martin 
     46   1.7    martin /* #define	DWC_GMAC_DEBUG	1 */
     47   1.1    martin 
     48  1.38     skrll #ifdef _KERNEL_OPT
     49   1.1    martin #include "opt_inet.h"
     50  1.38     skrll #include "opt_net_mpsafe.h"
     51  1.38     skrll #endif
     52   1.1    martin 
     53   1.1    martin #include <sys/param.h>
     54   1.1    martin #include <sys/bus.h>
     55   1.1    martin #include <sys/device.h>
     56   1.1    martin #include <sys/intr.h>
     57   1.1    martin #include <sys/systm.h>
     58   1.1    martin #include <sys/sockio.h>
     59  1.29  jmcneill #include <sys/cprng.h>
     60   1.1    martin 
     61   1.1    martin #include <net/if.h>
     62   1.1    martin #include <net/if_ether.h>
     63   1.1    martin #include <net/if_media.h>
     64   1.1    martin #include <net/bpf.h>
     65   1.1    martin #ifdef INET
     66   1.1    martin #include <netinet/if_inarp.h>
     67   1.1    martin #endif
     68   1.1    martin 
     69   1.1    martin #include <dev/mii/miivar.h>
     70   1.1    martin 
     71   1.1    martin #include <dev/ic/dwc_gmac_reg.h>
     72   1.1    martin #include <dev/ic/dwc_gmac_var.h>
     73   1.1    martin 
     74   1.1    martin static int dwc_gmac_miibus_read_reg(device_t, int, int);
     75   1.1    martin static void dwc_gmac_miibus_write_reg(device_t, int, int, int);
     76   1.1    martin static void dwc_gmac_miibus_statchg(struct ifnet *);
     77   1.1    martin 
     78   1.1    martin static int dwc_gmac_reset(struct dwc_gmac_softc *sc);
     79   1.1    martin static void dwc_gmac_write_hwaddr(struct dwc_gmac_softc *sc,
     80   1.1    martin 			 uint8_t enaddr[ETHER_ADDR_LEN]);
     81   1.1    martin static int dwc_gmac_alloc_dma_rings(struct dwc_gmac_softc *sc);
     82   1.1    martin static void dwc_gmac_free_dma_rings(struct dwc_gmac_softc *sc);
     83   1.1    martin static int dwc_gmac_alloc_rx_ring(struct dwc_gmac_softc *sc, struct dwc_gmac_rx_ring *);
     84   1.1    martin static void dwc_gmac_reset_rx_ring(struct dwc_gmac_softc *sc, struct dwc_gmac_rx_ring *);
     85   1.1    martin static void dwc_gmac_free_rx_ring(struct dwc_gmac_softc *sc, struct dwc_gmac_rx_ring *);
     86   1.1    martin static int dwc_gmac_alloc_tx_ring(struct dwc_gmac_softc *sc, struct dwc_gmac_tx_ring *);
     87   1.1    martin static void dwc_gmac_reset_tx_ring(struct dwc_gmac_softc *sc, struct dwc_gmac_tx_ring *);
     88   1.1    martin static void dwc_gmac_free_tx_ring(struct dwc_gmac_softc *sc, struct dwc_gmac_tx_ring *);
     89   1.1    martin static void dwc_gmac_txdesc_sync(struct dwc_gmac_softc *sc, int start, int end, int ops);
     90   1.1    martin static int dwc_gmac_init(struct ifnet *ifp);
     91  1.38     skrll static int dwc_gmac_init_locked(struct ifnet *ifp);
     92   1.1    martin static void dwc_gmac_stop(struct ifnet *ifp, int disable);
     93  1.38     skrll static void dwc_gmac_stop_locked(struct ifnet *ifp, int disable);
     94   1.1    martin static void dwc_gmac_start(struct ifnet *ifp);
     95  1.38     skrll static void dwc_gmac_start_locked(struct ifnet *ifp);
     96   1.1    martin static int dwc_gmac_queue(struct dwc_gmac_softc *sc, struct mbuf *m0);
     97   1.1    martin static int dwc_gmac_ioctl(struct ifnet *, u_long, void *);
     98   1.8    martin static void dwc_gmac_tx_intr(struct dwc_gmac_softc *sc);
     99   1.8    martin static void dwc_gmac_rx_intr(struct dwc_gmac_softc *sc);
    100  1.20  jmcneill static void dwc_gmac_setmulti(struct dwc_gmac_softc *sc);
    101  1.22    martin static int dwc_gmac_ifflags_cb(struct ethercom *);
    102  1.22    martin static uint32_t	bitrev32(uint32_t x);
    103  1.55    martin static void dwc_gmac_desc_set_owned_by_dev(struct dwc_gmac_dev_dmadesc *);
    104  1.55    martin static int  dwc_gmac_desc_is_owned_by_dev(struct dwc_gmac_dev_dmadesc *);
    105  1.55    martin static void dwc_gmac_desc_std_set_len(struct dwc_gmac_dev_dmadesc *, int);
    106  1.55    martin static uint32_t dwc_gmac_desc_std_get_len(struct dwc_gmac_dev_dmadesc *);
    107  1.55    martin static void dwc_gmac_desc_std_tx_init_flags(struct dwc_gmac_dev_dmadesc *);
    108  1.55    martin static void dwc_gmac_desc_std_tx_set_first_frag(struct dwc_gmac_dev_dmadesc *);
    109  1.55    martin static void dwc_gmac_desc_std_tx_set_last_frag(struct dwc_gmac_dev_dmadesc *);
    110  1.55    martin static void dwc_gmac_desc_std_rx_init_flags(struct dwc_gmac_dev_dmadesc *);
    111  1.55    martin static int  dwc_gmac_desc_std_rx_has_error(struct dwc_gmac_dev_dmadesc *);
    112  1.55    martin static void dwc_gmac_desc_enh_set_len(struct dwc_gmac_dev_dmadesc *, int);
    113  1.55    martin static uint32_t dwc_gmac_desc_enh_get_len(struct dwc_gmac_dev_dmadesc *);
    114  1.55    martin static void dwc_gmac_desc_enh_tx_init_flags(struct dwc_gmac_dev_dmadesc *);
    115  1.55    martin static void dwc_gmac_desc_enh_tx_set_first_frag(struct dwc_gmac_dev_dmadesc *);
    116  1.55    martin static void dwc_gmac_desc_enh_tx_set_last_frag(struct dwc_gmac_dev_dmadesc *);
    117  1.55    martin static void dwc_gmac_desc_enh_rx_init_flags(struct dwc_gmac_dev_dmadesc *);
    118  1.55    martin static int  dwc_gmac_desc_enh_rx_has_error(struct dwc_gmac_dev_dmadesc *);
    119  1.55    martin 
    120  1.55    martin static const struct dwc_gmac_desc_methods desc_methods_standard = {
    121  1.55    martin 	.tx_init_flags = dwc_gmac_desc_std_tx_init_flags,
    122  1.55    martin 	.tx_set_owned_by_dev = dwc_gmac_desc_set_owned_by_dev,
    123  1.55    martin 	.tx_is_owned_by_dev = dwc_gmac_desc_is_owned_by_dev,
    124  1.55    martin 	.tx_set_len = dwc_gmac_desc_std_set_len,
    125  1.55    martin 	.tx_set_first_frag = dwc_gmac_desc_std_tx_set_first_frag,
    126  1.55    martin 	.tx_set_last_frag = dwc_gmac_desc_std_tx_set_last_frag,
    127  1.55    martin 	.rx_init_flags = dwc_gmac_desc_std_rx_init_flags,
    128  1.55    martin 	.rx_set_owned_by_dev = dwc_gmac_desc_set_owned_by_dev,
    129  1.55    martin 	.rx_is_owned_by_dev = dwc_gmac_desc_is_owned_by_dev,
    130  1.55    martin 	.rx_set_len = dwc_gmac_desc_std_set_len,
    131  1.55    martin 	.rx_get_len = dwc_gmac_desc_std_get_len,
    132  1.55    martin 	.rx_has_error = dwc_gmac_desc_std_rx_has_error
    133  1.55    martin };
    134  1.55    martin 
    135  1.55    martin static const struct dwc_gmac_desc_methods desc_methods_enhanced = {
    136  1.55    martin 	.tx_init_flags = dwc_gmac_desc_enh_tx_init_flags,
    137  1.55    martin 	.tx_set_owned_by_dev = dwc_gmac_desc_set_owned_by_dev,
    138  1.55    martin 	.tx_is_owned_by_dev = dwc_gmac_desc_is_owned_by_dev,
    139  1.55    martin 	.tx_set_len = dwc_gmac_desc_enh_set_len,
    140  1.55    martin 	.tx_set_first_frag = dwc_gmac_desc_enh_tx_set_first_frag,
    141  1.55    martin 	.tx_set_last_frag = dwc_gmac_desc_enh_tx_set_last_frag,
    142  1.55    martin 	.rx_init_flags = dwc_gmac_desc_enh_rx_init_flags,
    143  1.55    martin 	.rx_set_owned_by_dev = dwc_gmac_desc_set_owned_by_dev,
    144  1.55    martin 	.rx_is_owned_by_dev = dwc_gmac_desc_is_owned_by_dev,
    145  1.55    martin 	.rx_set_len = dwc_gmac_desc_enh_set_len,
    146  1.55    martin 	.rx_get_len = dwc_gmac_desc_enh_get_len,
    147  1.55    martin 	.rx_has_error = dwc_gmac_desc_enh_rx_has_error
    148  1.55    martin };
    149  1.55    martin 
    150   1.1    martin 
    151   1.1    martin #define	TX_DESC_OFFSET(N)	((AWGE_RX_RING_COUNT+(N)) \
    152   1.1    martin 				    *sizeof(struct dwc_gmac_dev_dmadesc))
    153   1.8    martin #define	TX_NEXT(N)		(((N)+1) & (AWGE_TX_RING_COUNT-1))
    154   1.1    martin 
    155   1.1    martin #define RX_DESC_OFFSET(N)	((N)*sizeof(struct dwc_gmac_dev_dmadesc))
    156   1.8    martin #define	RX_NEXT(N)		(((N)+1) & (AWGE_RX_RING_COUNT-1))
    157   1.8    martin 
    158   1.8    martin 
    159   1.8    martin 
    160  1.11    martin #define	GMAC_DEF_DMA_INT_MASK	(GMAC_DMA_INT_TIE|GMAC_DMA_INT_RIE| \
    161   1.8    martin 				GMAC_DMA_INT_NIE|GMAC_DMA_INT_AIE| \
    162   1.8    martin 				GMAC_DMA_INT_FBE|GMAC_DMA_INT_UNE)
    163   1.8    martin 
    164   1.8    martin #define	GMAC_DMA_INT_ERRORS	(GMAC_DMA_INT_AIE|GMAC_DMA_INT_ERE| \
    165  1.10    martin 				GMAC_DMA_INT_FBE|	\
    166   1.8    martin 				GMAC_DMA_INT_RWE|GMAC_DMA_INT_RUE| \
    167   1.8    martin 				GMAC_DMA_INT_UNE|GMAC_DMA_INT_OVE| \
    168  1.10    martin 				GMAC_DMA_INT_TJE)
    169   1.8    martin 
    170   1.8    martin #define	AWIN_DEF_MAC_INTRMASK	\
    171   1.8    martin 	(AWIN_GMAC_MAC_INT_TSI | AWIN_GMAC_MAC_INT_ANEG |	\
    172  1.55    martin 	AWIN_GMAC_MAC_INT_LINKCHG)
    173   1.1    martin 
    174   1.7    martin #ifdef DWC_GMAC_DEBUG
    175   1.7    martin static void dwc_gmac_dump_dma(struct dwc_gmac_softc *sc);
    176   1.7    martin static void dwc_gmac_dump_tx_desc(struct dwc_gmac_softc *sc);
    177  1.11    martin static void dwc_gmac_dump_rx_desc(struct dwc_gmac_softc *sc);
    178   1.8    martin static void dwc_dump_and_abort(struct dwc_gmac_softc *sc, const char *msg);
    179  1.10    martin static void dwc_dump_status(struct dwc_gmac_softc *sc);
    180  1.22    martin static void dwc_gmac_dump_ffilt(struct dwc_gmac_softc *sc, uint32_t ffilt);
    181   1.7    martin #endif
    182   1.7    martin 
    183  1.38     skrll #ifdef NET_MPSAFE
    184  1.38     skrll #define DWCGMAC_MPSAFE	1
    185  1.38     skrll #endif
    186  1.38     skrll 
    187  1.51  jmcneill int
    188   1.5    martin dwc_gmac_attach(struct dwc_gmac_softc *sc, uint32_t mii_clk)
    189   1.1    martin {
    190   1.1    martin 	uint8_t enaddr[ETHER_ADDR_LEN];
    191  1.55    martin 	uint32_t maclo, machi, ver, hwft;
    192   1.1    martin 	struct mii_data * const mii = &sc->sc_mii;
    193   1.1    martin 	struct ifnet * const ifp = &sc->sc_ec.ec_if;
    194   1.5    martin 	prop_dictionary_t dict;
    195  1.41   msaitoh 	int rv;
    196   1.1    martin 
    197   1.1    martin 	mutex_init(&sc->sc_mdio_lock, MUTEX_DEFAULT, IPL_NET);
    198   1.3    martin 	sc->sc_mii_clk = mii_clk & 7;
    199   1.1    martin 
    200   1.5    martin 	dict = device_properties(sc->sc_dev);
    201   1.5    martin 	prop_data_t ea = dict ? prop_dictionary_get(dict, "mac-address") : NULL;
    202   1.5    martin 	if (ea != NULL) {
    203   1.5    martin 		/*
    204   1.5    martin 		 * If the MAC address is overriden by a device property,
    205   1.5    martin 		 * use that.
    206   1.5    martin 		 */
    207   1.5    martin 		KASSERT(prop_object_type(ea) == PROP_TYPE_DATA);
    208   1.5    martin 		KASSERT(prop_data_size(ea) == ETHER_ADDR_LEN);
    209   1.5    martin 		memcpy(enaddr, prop_data_data_nocopy(ea), ETHER_ADDR_LEN);
    210   1.5    martin 	} else {
    211   1.5    martin 		/*
    212   1.5    martin 		 * If we did not get an externaly configure address,
    213   1.5    martin 		 * try to read one from the current filter setup,
    214   1.5    martin 		 * before resetting the chip.
    215   1.5    martin 		 */
    216   1.8    martin 		maclo = bus_space_read_4(sc->sc_bst, sc->sc_bsh,
    217   1.8    martin 		    AWIN_GMAC_MAC_ADDR0LO);
    218   1.8    martin 		machi = bus_space_read_4(sc->sc_bst, sc->sc_bsh,
    219   1.8    martin 		    AWIN_GMAC_MAC_ADDR0HI);
    220  1.14  jmcneill 
    221  1.14  jmcneill 		if (maclo == 0xffffffff && (machi & 0xffff) == 0xffff) {
    222  1.29  jmcneill 			/* fake MAC address */
    223  1.29  jmcneill 			maclo = 0x00f2 | (cprng_strong32() << 16);
    224  1.29  jmcneill 			machi = cprng_strong32();
    225  1.14  jmcneill 		}
    226  1.14  jmcneill 
    227   1.1    martin 		enaddr[0] = maclo & 0x0ff;
    228   1.1    martin 		enaddr[1] = (maclo >> 8) & 0x0ff;
    229   1.1    martin 		enaddr[2] = (maclo >> 16) & 0x0ff;
    230   1.1    martin 		enaddr[3] = (maclo >> 24) & 0x0ff;
    231   1.1    martin 		enaddr[4] = machi & 0x0ff;
    232   1.1    martin 		enaddr[5] = (machi >> 8) & 0x0ff;
    233   1.1    martin 	}
    234   1.1    martin 
    235  1.55    martin 	ver = bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_VERSION);
    236  1.55    martin 	aprint_normal_dev(sc->sc_dev, "Core version: %08x\n", ver);
    237  1.55    martin 
    238   1.1    martin 	/*
    239  1.21     joerg 	 * Init chip and do initial setup
    240   1.1    martin 	 */
    241   1.1    martin 	if (dwc_gmac_reset(sc) != 0)
    242  1.51  jmcneill 		return ENXIO;	/* not much to cleanup, haven't attached yet */
    243   1.5    martin 	dwc_gmac_write_hwaddr(sc, enaddr);
    244  1.52     sevan 	aprint_normal_dev(sc->sc_dev, "Ethernet address %s\n",
    245   1.1    martin 	    ether_sprintf(enaddr));
    246   1.1    martin 
    247  1.55    martin 	hwft = 0;
    248  1.55    martin 	if (ver >= 0x35) {
    249  1.55    martin 		hwft = bus_space_read_4(sc->sc_bst, sc->sc_bsh,
    250  1.55    martin 		    AWIN_GMAC_DMA_HWFEATURES);
    251  1.55    martin 		aprint_normal_dev(sc->sc_dev,
    252  1.55    martin 		    "HW feature mask: %x\n", hwft);
    253  1.55    martin 	}
    254  1.55    martin 	if (hwft & GMAC_DMA_FEAT_ENHANCED_DESC) {
    255  1.55    martin 		aprint_normal_dev(sc->sc_dev,
    256  1.55    martin 		    "Using enhanced descriptor format\n");
    257  1.55    martin 		sc->sc_descm = &desc_methods_enhanced;
    258  1.55    martin 	} else {
    259  1.55    martin 		sc->sc_descm = &desc_methods_standard;
    260  1.55    martin 	}
    261  1.55    martin 
    262   1.1    martin 	/*
    263   1.1    martin 	 * Allocate Tx and Rx rings
    264   1.1    martin 	 */
    265   1.1    martin 	if (dwc_gmac_alloc_dma_rings(sc) != 0) {
    266   1.1    martin 		aprint_error_dev(sc->sc_dev, "could not allocate DMA rings\n");
    267   1.1    martin 		goto fail;
    268   1.1    martin 	}
    269  1.38     skrll 
    270   1.1    martin 	if (dwc_gmac_alloc_tx_ring(sc, &sc->sc_txq) != 0) {
    271   1.1    martin 		aprint_error_dev(sc->sc_dev, "could not allocate Tx ring\n");
    272   1.1    martin 		goto fail;
    273   1.1    martin 	}
    274   1.1    martin 
    275   1.1    martin 	if (dwc_gmac_alloc_rx_ring(sc, &sc->sc_rxq) != 0) {
    276   1.1    martin 		aprint_error_dev(sc->sc_dev, "could not allocate Rx ring\n");
    277   1.1    martin 		goto fail;
    278   1.1    martin 	}
    279   1.1    martin 
    280  1.38     skrll 	sc->sc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
    281  1.38     skrll 	mutex_init(&sc->sc_txq.t_mtx, MUTEX_DEFAULT, IPL_NET);
    282  1.38     skrll 	mutex_init(&sc->sc_rxq.r_mtx, MUTEX_DEFAULT, IPL_NET);
    283  1.38     skrll 
    284   1.1    martin 	/*
    285   1.1    martin 	 * Prepare interface data
    286   1.1    martin 	 */
    287   1.1    martin 	ifp->if_softc = sc;
    288   1.1    martin 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
    289   1.1    martin 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    290  1.44     ozaki #ifdef DWCGMAC_MPSAFE
    291  1.43     ozaki 	ifp->if_extflags = IFEF_MPSAFE;
    292  1.44     ozaki #endif
    293   1.1    martin 	ifp->if_ioctl = dwc_gmac_ioctl;
    294   1.1    martin 	ifp->if_start = dwc_gmac_start;
    295   1.1    martin 	ifp->if_init = dwc_gmac_init;
    296   1.1    martin 	ifp->if_stop = dwc_gmac_stop;
    297   1.1    martin 	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
    298   1.1    martin 	IFQ_SET_READY(&ifp->if_snd);
    299   1.1    martin 
    300   1.1    martin 	/*
    301   1.1    martin 	 * Attach MII subdevices
    302   1.1    martin 	 */
    303   1.2    martin 	sc->sc_ec.ec_mii = &sc->sc_mii;
    304   1.1    martin 	ifmedia_init(&mii->mii_media, 0, ether_mediachange, ether_mediastatus);
    305   1.1    martin         mii->mii_ifp = ifp;
    306   1.1    martin         mii->mii_readreg = dwc_gmac_miibus_read_reg;
    307   1.1    martin         mii->mii_writereg = dwc_gmac_miibus_write_reg;
    308   1.1    martin         mii->mii_statchg = dwc_gmac_miibus_statchg;
    309  1.25  jmcneill         mii_attach(sc->sc_dev, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY,
    310  1.25  jmcneill 	    MIIF_DOPAUSE);
    311   1.1    martin 
    312  1.38     skrll         if (LIST_EMPTY(&mii->mii_phys)) {
    313   1.1    martin                 aprint_error_dev(sc->sc_dev, "no PHY found!\n");
    314   1.1    martin                 ifmedia_add(&mii->mii_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
    315   1.1    martin                 ifmedia_set(&mii->mii_media, IFM_ETHER|IFM_MANUAL);
    316   1.1    martin         } else {
    317   1.1    martin                 ifmedia_set(&mii->mii_media, IFM_ETHER|IFM_AUTO);
    318   1.1    martin         }
    319   1.1    martin 
    320   1.1    martin 	/*
    321  1.33       tnn 	 * We can support 802.1Q VLAN-sized frames.
    322  1.33       tnn 	 */
    323  1.33       tnn 	sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_MTU;
    324  1.33       tnn 
    325  1.33       tnn 	/*
    326   1.1    martin 	 * Ready, attach interface
    327   1.1    martin 	 */
    328  1.38     skrll 	/* Attach the interface. */
    329  1.41   msaitoh 	rv = if_initialize(ifp);
    330  1.41   msaitoh 	if (rv != 0)
    331  1.41   msaitoh 		goto fail_2;
    332  1.38     skrll 	sc->sc_ipq = if_percpuq_create(&sc->sc_ec.ec_if);
    333  1.40     ozaki 	if_deferred_start_init(ifp, NULL);
    334   1.1    martin 	ether_ifattach(ifp, enaddr);
    335  1.22    martin 	ether_set_ifflags_cb(&sc->sc_ec, dwc_gmac_ifflags_cb);
    336  1.38     skrll 	if_register(ifp);
    337   1.1    martin 
    338   1.1    martin 	/*
    339   1.1    martin 	 * Enable interrupts
    340   1.1    martin 	 */
    341  1.38     skrll 	mutex_enter(sc->sc_lock);
    342  1.25  jmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_INTMASK,
    343   1.8    martin 	    AWIN_DEF_MAC_INTRMASK);
    344   1.8    martin 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_INTENABLE,
    345   1.8    martin 	    GMAC_DEF_DMA_INT_MASK);
    346  1.38     skrll 	mutex_exit(sc->sc_lock);
    347   1.1    martin 
    348  1.51  jmcneill 	return 0;
    349  1.51  jmcneill 
    350  1.41   msaitoh fail_2:
    351  1.41   msaitoh 	ifmedia_removeall(&mii->mii_media);
    352  1.42  jakllsch 	mii_detach(mii, MII_PHY_ANY, MII_OFFSET_ANY);
    353  1.41   msaitoh 	mutex_destroy(&sc->sc_txq.t_mtx);
    354  1.41   msaitoh 	mutex_destroy(&sc->sc_rxq.r_mtx);
    355  1.41   msaitoh 	mutex_obj_free(sc->sc_lock);
    356   1.1    martin fail:
    357   1.1    martin 	dwc_gmac_free_rx_ring(sc, &sc->sc_rxq);
    358   1.1    martin 	dwc_gmac_free_tx_ring(sc, &sc->sc_txq);
    359  1.41   msaitoh 	dwc_gmac_free_dma_rings(sc);
    360  1.41   msaitoh 	mutex_destroy(&sc->sc_mdio_lock);
    361  1.51  jmcneill 
    362  1.51  jmcneill 	return ENXIO;
    363   1.1    martin }
    364   1.1    martin 
    365   1.1    martin 
    366   1.1    martin 
    367   1.1    martin static int
    368   1.1    martin dwc_gmac_reset(struct dwc_gmac_softc *sc)
    369   1.1    martin {
    370   1.1    martin 	size_t cnt;
    371   1.1    martin 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_BUSMODE,
    372   1.1    martin 	    bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_BUSMODE) | GMAC_BUSMODE_RESET);
    373   1.1    martin 	for (cnt = 0; cnt < 3000; cnt++) {
    374   1.1    martin 		if ((bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_BUSMODE)
    375   1.1    martin 		    & GMAC_BUSMODE_RESET) == 0)
    376   1.1    martin 			return 0;
    377   1.1    martin 		delay(10);
    378   1.1    martin 	}
    379   1.1    martin 
    380   1.1    martin 	aprint_error_dev(sc->sc_dev, "reset timed out\n");
    381   1.1    martin 	return EIO;
    382   1.1    martin }
    383   1.1    martin 
    384   1.1    martin static void
    385   1.1    martin dwc_gmac_write_hwaddr(struct dwc_gmac_softc *sc,
    386   1.1    martin     uint8_t enaddr[ETHER_ADDR_LEN])
    387   1.1    martin {
    388  1.49  jmcneill 	uint32_t hi, lo;
    389   1.1    martin 
    390  1.49  jmcneill 	hi = enaddr[4] | (enaddr[5] << 8);
    391   1.1    martin 	lo = enaddr[0] | (enaddr[1] << 8) | (enaddr[2] << 16)
    392   1.1    martin 	    | (enaddr[3] << 24);
    393  1.49  jmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_ADDR0HI, hi);
    394   1.1    martin 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_ADDR0LO, lo);
    395   1.1    martin }
    396   1.1    martin 
    397   1.1    martin static int
    398   1.1    martin dwc_gmac_miibus_read_reg(device_t self, int phy, int reg)
    399   1.1    martin {
    400   1.1    martin 	struct dwc_gmac_softc * const sc = device_private(self);
    401   1.6    martin 	uint16_t mii;
    402   1.1    martin 	size_t cnt;
    403   1.1    martin 	int rv = 0;
    404   1.1    martin 
    405   1.6    martin 	mii = __SHIFTIN(phy,GMAC_MII_PHY_MASK)
    406   1.6    martin 	    | __SHIFTIN(reg,GMAC_MII_REG_MASK)
    407   1.6    martin 	    | __SHIFTIN(sc->sc_mii_clk,GMAC_MII_CLKMASK)
    408   1.6    martin 	    | GMAC_MII_BUSY;
    409   1.1    martin 
    410   1.1    martin 	mutex_enter(&sc->sc_mdio_lock);
    411   1.6    martin 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_MIIADDR, mii);
    412   1.1    martin 
    413   1.1    martin 	for (cnt = 0; cnt < 1000; cnt++) {
    414   1.3    martin 		if (!(bus_space_read_4(sc->sc_bst, sc->sc_bsh,
    415   1.3    martin 		    AWIN_GMAC_MAC_MIIADDR) & GMAC_MII_BUSY)) {
    416   1.3    martin 			rv = bus_space_read_4(sc->sc_bst, sc->sc_bsh,
    417   1.3    martin 			    AWIN_GMAC_MAC_MIIDATA);
    418   1.1    martin 			break;
    419   1.1    martin 		}
    420   1.1    martin 		delay(10);
    421   1.1    martin 	}
    422   1.1    martin 
    423   1.1    martin 	mutex_exit(&sc->sc_mdio_lock);
    424   1.1    martin 
    425   1.1    martin 	return rv;
    426   1.1    martin }
    427   1.1    martin 
    428   1.1    martin static void
    429   1.1    martin dwc_gmac_miibus_write_reg(device_t self, int phy, int reg, int val)
    430   1.1    martin {
    431   1.1    martin 	struct dwc_gmac_softc * const sc = device_private(self);
    432   1.6    martin 	uint16_t mii;
    433   1.1    martin 	size_t cnt;
    434   1.1    martin 
    435   1.6    martin 	mii = __SHIFTIN(phy,GMAC_MII_PHY_MASK)
    436   1.6    martin 	    | __SHIFTIN(reg,GMAC_MII_REG_MASK)
    437   1.6    martin 	    | __SHIFTIN(sc->sc_mii_clk,GMAC_MII_CLKMASK)
    438   1.6    martin 	    | GMAC_MII_BUSY | GMAC_MII_WRITE;
    439   1.1    martin 
    440   1.1    martin 	mutex_enter(&sc->sc_mdio_lock);
    441   1.1    martin 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_MIIDATA, val);
    442   1.6    martin 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_MIIADDR, mii);
    443   1.1    martin 
    444   1.1    martin 	for (cnt = 0; cnt < 1000; cnt++) {
    445   1.3    martin 		if (!(bus_space_read_4(sc->sc_bst, sc->sc_bsh,
    446   1.3    martin 		    AWIN_GMAC_MAC_MIIADDR) & GMAC_MII_BUSY))
    447   1.1    martin 			break;
    448   1.1    martin 		delay(10);
    449   1.1    martin 	}
    450  1.38     skrll 
    451   1.1    martin 	mutex_exit(&sc->sc_mdio_lock);
    452   1.1    martin }
    453   1.1    martin 
    454   1.1    martin static int
    455   1.1    martin dwc_gmac_alloc_rx_ring(struct dwc_gmac_softc *sc,
    456   1.1    martin 	struct dwc_gmac_rx_ring *ring)
    457   1.1    martin {
    458   1.1    martin 	struct dwc_gmac_rx_data *data;
    459   1.1    martin 	bus_addr_t physaddr;
    460   1.6    martin 	const size_t descsize = AWGE_RX_RING_COUNT * sizeof(*ring->r_desc);
    461   1.1    martin 	int error, i, next;
    462   1.1    martin 
    463   1.1    martin 	ring->r_cur = ring->r_next = 0;
    464   1.1    martin 	memset(ring->r_desc, 0, descsize);
    465   1.1    martin 
    466   1.1    martin 	/*
    467   1.1    martin 	 * Pre-allocate Rx buffers and populate Rx ring.
    468   1.1    martin 	 */
    469   1.1    martin 	for (i = 0; i < AWGE_RX_RING_COUNT; i++) {
    470   1.1    martin 		struct dwc_gmac_dev_dmadesc *desc;
    471   1.1    martin 
    472   1.1    martin 		data = &sc->sc_rxq.r_data[i];
    473   1.1    martin 
    474   1.1    martin 		MGETHDR(data->rd_m, M_DONTWAIT, MT_DATA);
    475   1.1    martin 		if (data->rd_m == NULL) {
    476   1.1    martin 			aprint_error_dev(sc->sc_dev,
    477   1.1    martin 			    "could not allocate rx mbuf #%d\n", i);
    478   1.1    martin 			error = ENOMEM;
    479   1.1    martin 			goto fail;
    480   1.1    martin 		}
    481   1.1    martin 		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
    482   1.1    martin 		    MCLBYTES, 0, BUS_DMA_NOWAIT, &data->rd_map);
    483   1.1    martin 		if (error != 0) {
    484   1.1    martin 			aprint_error_dev(sc->sc_dev,
    485   1.1    martin 			    "could not create DMA map\n");
    486   1.1    martin 			data->rd_map = NULL;
    487   1.1    martin 			goto fail;
    488   1.1    martin 		}
    489   1.1    martin 		MCLGET(data->rd_m, M_DONTWAIT);
    490   1.1    martin 		if (!(data->rd_m->m_flags & M_EXT)) {
    491   1.1    martin 			aprint_error_dev(sc->sc_dev,
    492   1.1    martin 			    "could not allocate mbuf cluster #%d\n", i);
    493   1.1    martin 			error = ENOMEM;
    494   1.1    martin 			goto fail;
    495   1.1    martin 		}
    496   1.1    martin 
    497   1.1    martin 		error = bus_dmamap_load(sc->sc_dmat, data->rd_map,
    498   1.1    martin 		    mtod(data->rd_m, void *), MCLBYTES, NULL,
    499   1.1    martin 		    BUS_DMA_READ | BUS_DMA_NOWAIT);
    500   1.1    martin 		if (error != 0) {
    501   1.1    martin 			aprint_error_dev(sc->sc_dev,
    502   1.1    martin 			    "could not load rx buf DMA map #%d", i);
    503   1.1    martin 			goto fail;
    504   1.1    martin 		}
    505   1.1    martin 		physaddr = data->rd_map->dm_segs[0].ds_addr;
    506   1.1    martin 
    507   1.1    martin 		desc = &sc->sc_rxq.r_desc[i];
    508   1.1    martin 		desc->ddesc_data = htole32(physaddr);
    509   1.8    martin 		next = RX_NEXT(i);
    510  1.38     skrll 		desc->ddesc_next = htole32(ring->r_physaddr
    511   1.1    martin 		    + next * sizeof(*desc));
    512  1.55    martin 		sc->sc_descm->rx_init_flags(desc);
    513  1.55    martin 		sc->sc_descm->rx_set_len(desc, AWGE_MAX_PACKET);
    514  1.55    martin 		sc->sc_descm->rx_set_owned_by_dev(desc);
    515   1.1    martin 	}
    516   1.1    martin 
    517   1.1    martin 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dma_ring_map, 0,
    518   1.1    martin 	    AWGE_RX_RING_COUNT*sizeof(struct dwc_gmac_dev_dmadesc),
    519  1.27      matt 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
    520   1.1    martin 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_RX_ADDR,
    521   1.6    martin 	    ring->r_physaddr);
    522   1.1    martin 
    523   1.1    martin 	return 0;
    524   1.1    martin 
    525   1.1    martin fail:
    526   1.1    martin 	dwc_gmac_free_rx_ring(sc, ring);
    527   1.1    martin 	return error;
    528   1.1    martin }
    529   1.1    martin 
    530   1.1    martin static void
    531   1.1    martin dwc_gmac_reset_rx_ring(struct dwc_gmac_softc *sc,
    532   1.1    martin 	struct dwc_gmac_rx_ring *ring)
    533   1.1    martin {
    534   1.1    martin 	struct dwc_gmac_dev_dmadesc *desc;
    535   1.1    martin 	int i;
    536   1.1    martin 
    537  1.38     skrll 	mutex_enter(&ring->r_mtx);
    538   1.1    martin 	for (i = 0; i < AWGE_RX_RING_COUNT; i++) {
    539   1.1    martin 		desc = &sc->sc_rxq.r_desc[i];
    540  1.55    martin 		sc->sc_descm->rx_init_flags(desc);
    541  1.55    martin 		sc->sc_descm->rx_set_len(desc, AWGE_MAX_PACKET);
    542  1.55    martin 		sc->sc_descm->rx_set_owned_by_dev(desc);
    543   1.1    martin 	}
    544   1.1    martin 
    545   1.1    martin 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dma_ring_map, 0,
    546   1.1    martin 	    AWGE_RX_RING_COUNT*sizeof(struct dwc_gmac_dev_dmadesc),
    547  1.27      matt 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    548   1.1    martin 
    549   1.1    martin 	ring->r_cur = ring->r_next = 0;
    550  1.11    martin 	/* reset DMA address to start of ring */
    551  1.11    martin 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_RX_ADDR,
    552  1.11    martin 	    sc->sc_rxq.r_physaddr);
    553  1.38     skrll 	mutex_exit(&ring->r_mtx);
    554   1.1    martin }
    555   1.1    martin 
    556   1.1    martin static int
    557   1.1    martin dwc_gmac_alloc_dma_rings(struct dwc_gmac_softc *sc)
    558   1.1    martin {
    559   1.1    martin 	const size_t descsize = AWGE_TOTAL_RING_COUNT *
    560   1.1    martin 		sizeof(struct dwc_gmac_dev_dmadesc);
    561   1.1    martin 	int error, nsegs;
    562   1.1    martin 	void *rings;
    563   1.1    martin 
    564   1.1    martin 	error = bus_dmamap_create(sc->sc_dmat, descsize, 1, descsize, 0,
    565   1.1    martin 	    BUS_DMA_NOWAIT, &sc->sc_dma_ring_map);
    566   1.1    martin 	if (error != 0) {
    567   1.1    martin 		aprint_error_dev(sc->sc_dev,
    568   1.1    martin 		    "could not create desc DMA map\n");
    569   1.1    martin 		sc->sc_dma_ring_map = NULL;
    570   1.1    martin 		goto fail;
    571   1.1    martin 	}
    572   1.1    martin 
    573   1.1    martin 	error = bus_dmamem_alloc(sc->sc_dmat, descsize, PAGE_SIZE, 0,
    574   1.1    martin 	    &sc->sc_dma_ring_seg, 1, &nsegs, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
    575   1.1    martin 	if (error != 0) {
    576   1.1    martin 		aprint_error_dev(sc->sc_dev,
    577   1.1    martin 		    "could not map DMA memory\n");
    578   1.1    martin 		goto fail;
    579   1.1    martin 	}
    580   1.1    martin 
    581   1.1    martin 	error = bus_dmamem_map(sc->sc_dmat, &sc->sc_dma_ring_seg, nsegs,
    582   1.1    martin 	    descsize, &rings, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
    583   1.1    martin 	if (error != 0) {
    584   1.1    martin 		aprint_error_dev(sc->sc_dev,
    585   1.1    martin 		    "could not allocate DMA memory\n");
    586   1.1    martin 		goto fail;
    587   1.1    martin 	}
    588   1.1    martin 
    589   1.1    martin 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_dma_ring_map, rings,
    590   1.1    martin 	    descsize, NULL, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
    591   1.1    martin 	if (error != 0) {
    592   1.1    martin 		aprint_error_dev(sc->sc_dev,
    593   1.1    martin 		    "could not load desc DMA map\n");
    594   1.1    martin 		goto fail;
    595   1.1    martin 	}
    596   1.1    martin 
    597   1.1    martin 	/* give first AWGE_RX_RING_COUNT to the RX side */
    598   1.1    martin 	sc->sc_rxq.r_desc = rings;
    599   1.1    martin 	sc->sc_rxq.r_physaddr = sc->sc_dma_ring_map->dm_segs[0].ds_addr;
    600   1.1    martin 
    601   1.1    martin 	/* and next rings to the TX side */
    602   1.1    martin 	sc->sc_txq.t_desc = sc->sc_rxq.r_desc + AWGE_RX_RING_COUNT;
    603  1.38     skrll 	sc->sc_txq.t_physaddr = sc->sc_rxq.r_physaddr +
    604   1.1    martin 	    AWGE_RX_RING_COUNT*sizeof(struct dwc_gmac_dev_dmadesc);
    605   1.1    martin 
    606   1.1    martin 	return 0;
    607   1.1    martin 
    608   1.1    martin fail:
    609   1.1    martin 	dwc_gmac_free_dma_rings(sc);
    610   1.1    martin 	return error;
    611   1.1    martin }
    612   1.1    martin 
    613   1.1    martin static void
    614   1.1    martin dwc_gmac_free_dma_rings(struct dwc_gmac_softc *sc)
    615   1.1    martin {
    616   1.1    martin 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dma_ring_map, 0,
    617   1.1    martin 	    sc->sc_dma_ring_map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
    618   1.1    martin 	bus_dmamap_unload(sc->sc_dmat, sc->sc_dma_ring_map);
    619   1.1    martin 	bus_dmamem_unmap(sc->sc_dmat, sc->sc_rxq.r_desc,
    620   1.1    martin 	    AWGE_TOTAL_RING_COUNT * sizeof(struct dwc_gmac_dev_dmadesc));
    621   1.1    martin 	bus_dmamem_free(sc->sc_dmat, &sc->sc_dma_ring_seg, 1);
    622   1.1    martin }
    623   1.1    martin 
    624   1.1    martin static void
    625   1.1    martin dwc_gmac_free_rx_ring(struct dwc_gmac_softc *sc, struct dwc_gmac_rx_ring *ring)
    626   1.1    martin {
    627   1.1    martin 	struct dwc_gmac_rx_data *data;
    628   1.1    martin 	int i;
    629   1.1    martin 
    630   1.1    martin 	if (ring->r_desc == NULL)
    631   1.1    martin 		return;
    632   1.1    martin 
    633   1.1    martin 
    634   1.1    martin 	for (i = 0; i < AWGE_RX_RING_COUNT; i++) {
    635   1.1    martin 		data = &ring->r_data[i];
    636   1.1    martin 
    637   1.1    martin 		if (data->rd_map != NULL) {
    638   1.1    martin 			bus_dmamap_sync(sc->sc_dmat, data->rd_map, 0,
    639   1.1    martin 			    AWGE_RX_RING_COUNT
    640   1.1    martin 				*sizeof(struct dwc_gmac_dev_dmadesc),
    641   1.1    martin 			    BUS_DMASYNC_POSTREAD);
    642   1.1    martin 			bus_dmamap_unload(sc->sc_dmat, data->rd_map);
    643   1.1    martin 			bus_dmamap_destroy(sc->sc_dmat, data->rd_map);
    644   1.1    martin 		}
    645   1.1    martin 		if (data->rd_m != NULL)
    646   1.1    martin 			m_freem(data->rd_m);
    647   1.1    martin 	}
    648   1.1    martin }
    649   1.1    martin 
    650   1.1    martin static int
    651   1.1    martin dwc_gmac_alloc_tx_ring(struct dwc_gmac_softc *sc,
    652   1.1    martin 	struct dwc_gmac_tx_ring *ring)
    653   1.1    martin {
    654   1.1    martin 	int i, error = 0;
    655   1.1    martin 
    656   1.1    martin 	ring->t_queued = 0;
    657   1.1    martin 	ring->t_cur = ring->t_next = 0;
    658   1.1    martin 
    659   1.1    martin 	memset(ring->t_desc, 0, AWGE_TX_RING_COUNT*sizeof(*ring->t_desc));
    660   1.1    martin 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dma_ring_map,
    661   1.1    martin 	    TX_DESC_OFFSET(0),
    662   1.1    martin 	    AWGE_TX_RING_COUNT*sizeof(struct dwc_gmac_dev_dmadesc),
    663   1.1    martin 	    BUS_DMASYNC_POSTWRITE);
    664   1.1    martin 
    665   1.1    martin 	for (i = 0; i < AWGE_TX_RING_COUNT; i++) {
    666   1.1    martin 		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
    667   1.1    martin 		    AWGE_TX_RING_COUNT, MCLBYTES, 0,
    668   1.1    martin 		    BUS_DMA_NOWAIT|BUS_DMA_COHERENT,
    669   1.1    martin 		    &ring->t_data[i].td_map);
    670   1.1    martin 		if (error != 0) {
    671   1.1    martin 			aprint_error_dev(sc->sc_dev,
    672   1.1    martin 			    "could not create TX DMA map #%d\n", i);
    673   1.1    martin 			ring->t_data[i].td_map = NULL;
    674   1.1    martin 			goto fail;
    675   1.1    martin 		}
    676   1.1    martin 		ring->t_desc[i].ddesc_next = htole32(
    677   1.1    martin 		    ring->t_physaddr + sizeof(struct dwc_gmac_dev_dmadesc)
    678   1.8    martin 		    *TX_NEXT(i));
    679   1.1    martin 	}
    680   1.1    martin 
    681   1.1    martin 	return 0;
    682   1.1    martin 
    683   1.1    martin fail:
    684   1.1    martin 	dwc_gmac_free_tx_ring(sc, ring);
    685   1.1    martin 	return error;
    686   1.1    martin }
    687   1.1    martin 
    688   1.1    martin static void
    689   1.1    martin dwc_gmac_txdesc_sync(struct dwc_gmac_softc *sc, int start, int end, int ops)
    690   1.1    martin {
    691   1.1    martin 	/* 'end' is pointing one descriptor beyound the last we want to sync */
    692   1.1    martin 	if (end > start) {
    693   1.1    martin 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dma_ring_map,
    694   1.1    martin 		    TX_DESC_OFFSET(start),
    695   1.1    martin 		    TX_DESC_OFFSET(end)-TX_DESC_OFFSET(start),
    696   1.1    martin 		    ops);
    697   1.1    martin 		return;
    698   1.1    martin 	}
    699   1.1    martin 	/* sync from 'start' to end of ring */
    700   1.1    martin 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dma_ring_map,
    701   1.1    martin 	    TX_DESC_OFFSET(start),
    702  1.31  jmcneill 	    TX_DESC_OFFSET(AWGE_TX_RING_COUNT)-TX_DESC_OFFSET(start),
    703   1.1    martin 	    ops);
    704  1.47  jmcneill 	if (TX_DESC_OFFSET(end) - TX_DESC_OFFSET(0) > 0) {
    705  1.47  jmcneill 		/* sync from start of ring to 'end' */
    706  1.47  jmcneill 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dma_ring_map,
    707  1.47  jmcneill 		    TX_DESC_OFFSET(0),
    708  1.47  jmcneill 		    TX_DESC_OFFSET(end)-TX_DESC_OFFSET(0),
    709  1.47  jmcneill 		    ops);
    710  1.47  jmcneill 	}
    711   1.1    martin }
    712   1.1    martin 
    713   1.1    martin static void
    714   1.1    martin dwc_gmac_reset_tx_ring(struct dwc_gmac_softc *sc,
    715   1.1    martin 	struct dwc_gmac_tx_ring *ring)
    716   1.1    martin {
    717   1.1    martin 	int i;
    718   1.1    martin 
    719  1.38     skrll 	mutex_enter(&ring->t_mtx);
    720   1.1    martin 	for (i = 0; i < AWGE_TX_RING_COUNT; i++) {
    721   1.1    martin 		struct dwc_gmac_tx_data *data = &ring->t_data[i];
    722   1.1    martin 
    723   1.1    martin 		if (data->td_m != NULL) {
    724   1.1    martin 			bus_dmamap_sync(sc->sc_dmat, data->td_active,
    725   1.1    martin 			    0, data->td_active->dm_mapsize,
    726   1.1    martin 			    BUS_DMASYNC_POSTWRITE);
    727   1.1    martin 			bus_dmamap_unload(sc->sc_dmat, data->td_active);
    728   1.1    martin 			m_freem(data->td_m);
    729   1.1    martin 			data->td_m = NULL;
    730   1.1    martin 		}
    731   1.1    martin 	}
    732   1.1    martin 
    733   1.1    martin 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dma_ring_map,
    734   1.1    martin 	    TX_DESC_OFFSET(0),
    735   1.1    martin 	    AWGE_TX_RING_COUNT*sizeof(struct dwc_gmac_dev_dmadesc),
    736  1.27      matt 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    737   1.6    martin 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_TX_ADDR,
    738   1.6    martin 	    sc->sc_txq.t_physaddr);
    739   1.1    martin 
    740   1.1    martin 	ring->t_queued = 0;
    741   1.1    martin 	ring->t_cur = ring->t_next = 0;
    742  1.38     skrll 	mutex_exit(&ring->t_mtx);
    743   1.1    martin }
    744   1.1    martin 
    745   1.1    martin static void
    746   1.1    martin dwc_gmac_free_tx_ring(struct dwc_gmac_softc *sc,
    747   1.1    martin 	struct dwc_gmac_tx_ring *ring)
    748   1.1    martin {
    749   1.1    martin 	int i;
    750   1.1    martin 
    751   1.1    martin 	/* unload the maps */
    752   1.1    martin 	for (i = 0; i < AWGE_TX_RING_COUNT; i++) {
    753   1.1    martin 		struct dwc_gmac_tx_data *data = &ring->t_data[i];
    754   1.1    martin 
    755   1.1    martin 		if (data->td_m != NULL) {
    756   1.1    martin 			bus_dmamap_sync(sc->sc_dmat, data->td_active,
    757   1.1    martin 			    0, data->td_map->dm_mapsize,
    758   1.1    martin 			    BUS_DMASYNC_POSTWRITE);
    759   1.1    martin 			bus_dmamap_unload(sc->sc_dmat, data->td_active);
    760   1.1    martin 			m_freem(data->td_m);
    761   1.1    martin 			data->td_m = NULL;
    762   1.1    martin 		}
    763   1.1    martin 	}
    764   1.1    martin 
    765   1.1    martin 	/* and actually free them */
    766   1.1    martin 	for (i = 0; i < AWGE_TX_RING_COUNT; i++) {
    767   1.1    martin 		struct dwc_gmac_tx_data *data = &ring->t_data[i];
    768   1.1    martin 
    769   1.1    martin 		bus_dmamap_destroy(sc->sc_dmat, data->td_map);
    770   1.1    martin 	}
    771   1.1    martin }
    772   1.1    martin 
    773   1.1    martin static void
    774   1.1    martin dwc_gmac_miibus_statchg(struct ifnet *ifp)
    775   1.1    martin {
    776   1.1    martin 	struct dwc_gmac_softc * const sc = ifp->if_softc;
    777   1.1    martin 	struct mii_data * const mii = &sc->sc_mii;
    778  1.25  jmcneill 	uint32_t conf, flow;
    779   1.1    martin 
    780   1.1    martin 	/*
    781   1.1    martin 	 * Set MII or GMII interface based on the speed
    782  1.38     skrll 	 * negotiated by the PHY.
    783   1.9    martin 	 */
    784   1.9    martin 	conf = bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_CONF);
    785   1.9    martin 	conf &= ~(AWIN_GMAC_MAC_CONF_FES100|AWIN_GMAC_MAC_CONF_MIISEL
    786   1.9    martin 	    |AWIN_GMAC_MAC_CONF_FULLDPLX);
    787  1.11    martin 	conf |= AWIN_GMAC_MAC_CONF_FRAMEBURST
    788  1.11    martin 	    | AWIN_GMAC_MAC_CONF_DISABLERXOWN
    789  1.25  jmcneill 	    | AWIN_GMAC_MAC_CONF_DISABLEJABBER
    790  1.25  jmcneill 	    | AWIN_GMAC_MAC_CONF_ACS
    791  1.11    martin 	    | AWIN_GMAC_MAC_CONF_RXENABLE
    792  1.11    martin 	    | AWIN_GMAC_MAC_CONF_TXENABLE;
    793   1.1    martin 	switch (IFM_SUBTYPE(mii->mii_media_active)) {
    794   1.1    martin 	case IFM_10_T:
    795  1.12  jmcneill 		conf |= AWIN_GMAC_MAC_CONF_MIISEL;
    796   1.9    martin 		break;
    797   1.1    martin 	case IFM_100_TX:
    798  1.12  jmcneill 		conf |= AWIN_GMAC_MAC_CONF_FES100 |
    799  1.12  jmcneill 			AWIN_GMAC_MAC_CONF_MIISEL;
    800   1.1    martin 		break;
    801   1.1    martin 	case IFM_1000_T:
    802   1.1    martin 		break;
    803   1.1    martin 	}
    804  1.46  jmcneill 	if (sc->sc_set_speed)
    805  1.46  jmcneill 		sc->sc_set_speed(sc, IFM_SUBTYPE(mii->mii_media_active));
    806  1.25  jmcneill 
    807  1.25  jmcneill 	flow = 0;
    808  1.25  jmcneill 	if (IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) {
    809   1.9    martin 		conf |= AWIN_GMAC_MAC_CONF_FULLDPLX;
    810  1.25  jmcneill 		flow |= __SHIFTIN(0x200, AWIN_GMAC_MAC_FLOWCTRL_PAUSE);
    811  1.25  jmcneill 	}
    812  1.25  jmcneill 	if (mii->mii_media_active & IFM_ETH_TXPAUSE) {
    813  1.25  jmcneill 		flow |= AWIN_GMAC_MAC_FLOWCTRL_TFE;
    814  1.25  jmcneill 	}
    815  1.25  jmcneill 	if (mii->mii_media_active & IFM_ETH_RXPAUSE) {
    816  1.25  jmcneill 		flow |= AWIN_GMAC_MAC_FLOWCTRL_RFE;
    817  1.25  jmcneill 	}
    818  1.25  jmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh,
    819  1.25  jmcneill 	    AWIN_GMAC_MAC_FLOWCTRL, flow);
    820   1.9    martin 
    821   1.9    martin #ifdef DWC_GMAC_DEBUG
    822   1.9    martin 	aprint_normal_dev(sc->sc_dev,
    823   1.9    martin 	    "setting MAC conf register: %08x\n", conf);
    824   1.9    martin #endif
    825   1.9    martin 
    826   1.9    martin 	bus_space_write_4(sc->sc_bst, sc->sc_bsh,
    827   1.9    martin 	    AWIN_GMAC_MAC_CONF, conf);
    828   1.1    martin }
    829   1.1    martin 
    830   1.1    martin static int
    831   1.1    martin dwc_gmac_init(struct ifnet *ifp)
    832   1.1    martin {
    833   1.1    martin 	struct dwc_gmac_softc *sc = ifp->if_softc;
    834  1.38     skrll 
    835  1.38     skrll 	mutex_enter(sc->sc_lock);
    836  1.38     skrll 	int ret = dwc_gmac_init_locked(ifp);
    837  1.38     skrll 	mutex_exit(sc->sc_lock);
    838  1.38     skrll 
    839  1.38     skrll 	return ret;
    840  1.38     skrll }
    841  1.38     skrll 
    842  1.38     skrll static int
    843  1.38     skrll dwc_gmac_init_locked(struct ifnet *ifp)
    844  1.38     skrll {
    845  1.38     skrll 	struct dwc_gmac_softc *sc = ifp->if_softc;
    846  1.13  jmcneill 	uint32_t ffilt;
    847   1.1    martin 
    848   1.1    martin 	if (ifp->if_flags & IFF_RUNNING)
    849   1.1    martin 		return 0;
    850   1.1    martin 
    851  1.38     skrll 	dwc_gmac_stop_locked(ifp, 0);
    852   1.1    martin 
    853   1.1    martin 	/*
    854  1.11    martin 	 * Configure DMA burst/transfer mode and RX/TX priorities.
    855  1.11    martin 	 * XXX - the GMAC_BUSMODE_PRIORXTX bits are undocumented.
    856  1.11    martin 	 */
    857  1.11    martin 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_BUSMODE,
    858  1.25  jmcneill 	    GMAC_BUSMODE_FIXEDBURST | GMAC_BUSMODE_4PBL |
    859  1.25  jmcneill 	    __SHIFTIN(2, GMAC_BUSMODE_RPBL) |
    860  1.25  jmcneill 	    __SHIFTIN(2, GMAC_BUSMODE_PBL));
    861  1.11    martin 
    862  1.11    martin 	/*
    863  1.13  jmcneill 	 * Set up address filter
    864  1.11    martin 	 */
    865  1.20  jmcneill 	ffilt = bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_FFILT);
    866  1.20  jmcneill 	if (ifp->if_flags & IFF_PROMISC) {
    867  1.13  jmcneill 		ffilt |= AWIN_GMAC_MAC_FFILT_PR;
    868  1.20  jmcneill 	} else {
    869  1.20  jmcneill 		ffilt &= ~AWIN_GMAC_MAC_FFILT_PR;
    870  1.20  jmcneill 	}
    871  1.20  jmcneill 	if (ifp->if_flags & IFF_BROADCAST) {
    872  1.20  jmcneill 		ffilt &= ~AWIN_GMAC_MAC_FFILT_DBF;
    873  1.20  jmcneill 	} else {
    874  1.20  jmcneill 		ffilt |= AWIN_GMAC_MAC_FFILT_DBF;
    875  1.20  jmcneill 	}
    876  1.13  jmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_FFILT, ffilt);
    877  1.11    martin 
    878  1.11    martin 	/*
    879  1.20  jmcneill 	 * Set up multicast filter
    880  1.20  jmcneill 	 */
    881  1.20  jmcneill 	dwc_gmac_setmulti(sc);
    882  1.20  jmcneill 
    883  1.20  jmcneill 	/*
    884   1.6    martin 	 * Set up dma pointer for RX and TX ring
    885   1.1    martin 	 */
    886   1.6    martin 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_RX_ADDR,
    887   1.6    martin 	    sc->sc_rxq.r_physaddr);
    888   1.6    martin 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_TX_ADDR,
    889   1.6    martin 	    sc->sc_txq.t_physaddr);
    890   1.6    martin 
    891   1.6    martin 	/*
    892  1.10    martin 	 * Start RX/TX part
    893   1.6    martin 	 */
    894  1.46  jmcneill 	uint32_t opmode = GMAC_DMA_OP_RXSTART | GMAC_DMA_OP_TXSTART;
    895  1.46  jmcneill 	if ((sc->sc_flags & DWC_GMAC_FORCE_THRESH_DMA_MODE) == 0) {
    896  1.46  jmcneill 		opmode |= GMAC_DMA_OP_RXSTOREFORWARD | GMAC_DMA_OP_TXSTOREFORWARD;
    897  1.46  jmcneill 	}
    898  1.46  jmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_OPMODE, opmode);
    899   1.1    martin 
    900  1.38     skrll 	sc->sc_stopping = false;
    901  1.38     skrll 
    902   1.1    martin 	ifp->if_flags |= IFF_RUNNING;
    903   1.1    martin 	ifp->if_flags &= ~IFF_OACTIVE;
    904   1.1    martin 
    905   1.1    martin 	return 0;
    906   1.1    martin }
    907   1.1    martin 
    908   1.1    martin static void
    909   1.1    martin dwc_gmac_start(struct ifnet *ifp)
    910   1.1    martin {
    911   1.1    martin 	struct dwc_gmac_softc *sc = ifp->if_softc;
    912  1.45    martin #ifdef DWCGMAC_MPSAFE
    913  1.43     ozaki 	KASSERT(if_is_mpsafe(ifp));
    914  1.45    martin #endif
    915  1.38     skrll 
    916  1.38     skrll 	mutex_enter(sc->sc_lock);
    917  1.38     skrll 	if (!sc->sc_stopping) {
    918  1.38     skrll 		mutex_enter(&sc->sc_txq.t_mtx);
    919  1.38     skrll 		dwc_gmac_start_locked(ifp);
    920  1.38     skrll 		mutex_exit(&sc->sc_txq.t_mtx);
    921  1.38     skrll 	}
    922  1.38     skrll 	mutex_exit(sc->sc_lock);
    923  1.38     skrll }
    924  1.38     skrll 
    925  1.38     skrll static void
    926  1.38     skrll dwc_gmac_start_locked(struct ifnet *ifp)
    927  1.38     skrll {
    928  1.38     skrll 	struct dwc_gmac_softc *sc = ifp->if_softc;
    929   1.1    martin 	int old = sc->sc_txq.t_queued;
    930  1.30    martin 	int start = sc->sc_txq.t_cur;
    931   1.1    martin 	struct mbuf *m0;
    932   1.1    martin 
    933   1.1    martin 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    934   1.1    martin 		return;
    935   1.1    martin 
    936   1.1    martin 	for (;;) {
    937   1.1    martin 		IFQ_POLL(&ifp->if_snd, m0);
    938   1.1    martin 		if (m0 == NULL)
    939   1.1    martin 			break;
    940   1.1    martin 		if (dwc_gmac_queue(sc, m0) != 0) {
    941   1.1    martin 			ifp->if_flags |= IFF_OACTIVE;
    942   1.1    martin 			break;
    943   1.1    martin 		}
    944   1.1    martin 		IFQ_DEQUEUE(&ifp->if_snd, m0);
    945  1.50   msaitoh 		bpf_mtap(ifp, m0, BPF_D_OUT);
    946  1.32    martin 		if (sc->sc_txq.t_queued == AWGE_TX_RING_COUNT) {
    947  1.32    martin 			ifp->if_flags |= IFF_OACTIVE;
    948  1.32    martin 			break;
    949  1.32    martin 		}
    950   1.1    martin 	}
    951   1.1    martin 
    952   1.1    martin 	if (sc->sc_txq.t_queued != old) {
    953   1.1    martin 		/* packets have been queued, kick it off */
    954  1.30    martin 		dwc_gmac_txdesc_sync(sc, start, sc->sc_txq.t_cur,
    955   1.1    martin 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    956  1.10    martin 
    957  1.10    martin #ifdef DWC_GMAC_DEBUG
    958  1.10    martin 		dwc_dump_status(sc);
    959  1.10    martin #endif
    960  1.55    martin 		bus_space_write_4(sc->sc_bst, sc->sc_bsh,
    961  1.55    martin 		    AWIN_GMAC_DMA_TXPOLL, ~0U);
    962   1.1    martin 	}
    963   1.1    martin }
    964   1.1    martin 
    965   1.1    martin static void
    966   1.1    martin dwc_gmac_stop(struct ifnet *ifp, int disable)
    967   1.1    martin {
    968   1.1    martin 	struct dwc_gmac_softc *sc = ifp->if_softc;
    969   1.1    martin 
    970  1.38     skrll 	mutex_enter(sc->sc_lock);
    971  1.38     skrll 	dwc_gmac_stop_locked(ifp, disable);
    972  1.38     skrll 	mutex_exit(sc->sc_lock);
    973  1.38     skrll }
    974  1.38     skrll 
    975  1.38     skrll static void
    976  1.38     skrll dwc_gmac_stop_locked(struct ifnet *ifp, int disable)
    977  1.38     skrll {
    978  1.38     skrll 	struct dwc_gmac_softc *sc = ifp->if_softc;
    979  1.38     skrll 
    980  1.38     skrll 	sc->sc_stopping = true;
    981  1.38     skrll 
    982   1.6    martin 	bus_space_write_4(sc->sc_bst, sc->sc_bsh,
    983   1.6    martin 	    AWIN_GMAC_DMA_OPMODE,
    984   1.6    martin 	    bus_space_read_4(sc->sc_bst, sc->sc_bsh,
    985   1.6    martin 	        AWIN_GMAC_DMA_OPMODE)
    986   1.6    martin 		& ~(GMAC_DMA_OP_TXSTART|GMAC_DMA_OP_RXSTART));
    987   1.6    martin 	bus_space_write_4(sc->sc_bst, sc->sc_bsh,
    988   1.6    martin 	    AWIN_GMAC_DMA_OPMODE,
    989   1.6    martin 	    bus_space_read_4(sc->sc_bst, sc->sc_bsh,
    990   1.6    martin 	        AWIN_GMAC_DMA_OPMODE) | GMAC_DMA_OP_FLUSHTX);
    991   1.6    martin 
    992   1.1    martin 	mii_down(&sc->sc_mii);
    993   1.1    martin 	dwc_gmac_reset_tx_ring(sc, &sc->sc_txq);
    994   1.1    martin 	dwc_gmac_reset_rx_ring(sc, &sc->sc_rxq);
    995  1.48  jmcneill 
    996  1.48  jmcneill 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    997   1.1    martin }
    998   1.1    martin 
    999   1.1    martin /*
   1000   1.1    martin  * Add m0 to the TX ring
   1001   1.1    martin  */
   1002   1.1    martin static int
   1003   1.1    martin dwc_gmac_queue(struct dwc_gmac_softc *sc, struct mbuf *m0)
   1004   1.1    martin {
   1005   1.1    martin 	struct dwc_gmac_dev_dmadesc *desc = NULL;
   1006   1.1    martin 	struct dwc_gmac_tx_data *data = NULL;
   1007   1.1    martin 	bus_dmamap_t map;
   1008   1.1    martin 	int error, i, first;
   1009   1.1    martin 
   1010   1.8    martin #ifdef DWC_GMAC_DEBUG
   1011   1.8    martin 	aprint_normal_dev(sc->sc_dev,
   1012   1.8    martin 	    "dwc_gmac_queue: adding mbuf chain %p\n", m0);
   1013   1.8    martin #endif
   1014   1.8    martin 
   1015   1.1    martin 	first = sc->sc_txq.t_cur;
   1016   1.1    martin 	map = sc->sc_txq.t_data[first].td_map;
   1017   1.1    martin 
   1018   1.1    martin 	error = bus_dmamap_load_mbuf(sc->sc_dmat, map, m0,
   1019   1.1    martin 	    BUS_DMA_WRITE|BUS_DMA_NOWAIT);
   1020   1.1    martin 	if (error != 0) {
   1021   1.1    martin 		aprint_error_dev(sc->sc_dev, "could not map mbuf "
   1022   1.1    martin 		    "(len: %d, error %d)\n", m0->m_pkthdr.len, error);
   1023   1.1    martin 		return error;
   1024   1.1    martin 	}
   1025   1.1    martin 
   1026  1.32    martin 	if (sc->sc_txq.t_queued + map->dm_nsegs > AWGE_TX_RING_COUNT) {
   1027   1.1    martin 		bus_dmamap_unload(sc->sc_dmat, map);
   1028   1.1    martin 		return ENOBUFS;
   1029   1.1    martin 	}
   1030   1.1    martin 
   1031   1.1    martin 	for (i = 0; i < map->dm_nsegs; i++) {
   1032   1.1    martin 		data = &sc->sc_txq.t_data[sc->sc_txq.t_cur];
   1033   1.8    martin 		desc = &sc->sc_txq.t_desc[sc->sc_txq.t_cur];
   1034   1.8    martin 
   1035   1.8    martin 		desc->ddesc_data = htole32(map->dm_segs[i].ds_addr);
   1036   1.7    martin 
   1037   1.7    martin #ifdef DWC_GMAC_DEBUG
   1038   1.7    martin 		aprint_normal_dev(sc->sc_dev, "enqueing desc #%d data %08lx "
   1039  1.55    martin 		    "len %lu\n", sc->sc_txq.t_cur,
   1040   1.7    martin 		    (unsigned long)map->dm_segs[i].ds_addr,
   1041  1.55    martin 		    (unsigned long)map->dm_segs[i].ds_len);
   1042   1.7    martin #endif
   1043   1.7    martin 
   1044  1.55    martin 		sc->sc_descm->tx_init_flags(desc);
   1045  1.55    martin 		sc->sc_descm->tx_set_len(desc, map->dm_segs[i].ds_len);
   1046  1.55    martin 
   1047  1.55    martin 		if (i == 0)
   1048  1.55    martin 			sc->sc_descm->tx_set_first_frag(desc);
   1049   1.1    martin 
   1050   1.1    martin 		/*
   1051   1.1    martin 		 * Defer passing ownership of the first descriptor
   1052  1.23     joerg 		 * until we are done.
   1053   1.1    martin 		 */
   1054  1.55    martin 		if (i != 0)
   1055  1.55    martin 			sc->sc_descm->tx_set_owned_by_dev(desc);
   1056   1.8    martin 
   1057   1.6    martin 		sc->sc_txq.t_queued++;
   1058   1.8    martin 		sc->sc_txq.t_cur = TX_NEXT(sc->sc_txq.t_cur);
   1059   1.1    martin 	}
   1060   1.1    martin 
   1061  1.55    martin 	sc->sc_descm->tx_set_last_frag(desc);
   1062   1.1    martin 
   1063   1.1    martin 	data->td_m = m0;
   1064   1.1    martin 	data->td_active = map;
   1065   1.1    martin 
   1066   1.1    martin 	bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
   1067  1.34  jmcneill 	    BUS_DMASYNC_PREWRITE);
   1068   1.1    martin 
   1069  1.32    martin 	/* Pass first to device */
   1070  1.55    martin 	sc->sc_descm->tx_set_owned_by_dev(&sc->sc_txq.t_desc[first]);
   1071  1.55    martin 
   1072  1.55    martin 	bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
   1073  1.55    martin 	    BUS_DMASYNC_PREWRITE);
   1074  1.32    martin 
   1075   1.1    martin 	return 0;
   1076   1.1    martin }
   1077   1.1    martin 
   1078  1.22    martin /*
   1079  1.22    martin  * If the interface is up and running, only modify the receive
   1080  1.22    martin  * filter when setting promiscuous or debug mode.  Otherwise fall
   1081  1.22    martin  * through to ether_ioctl, which will reset the chip.
   1082  1.22    martin  */
   1083  1.22    martin static int
   1084  1.22    martin dwc_gmac_ifflags_cb(struct ethercom *ec)
   1085  1.22    martin {
   1086  1.22    martin 	struct ifnet *ifp = &ec->ec_if;
   1087  1.22    martin 	struct dwc_gmac_softc *sc = ifp->if_softc;
   1088  1.38     skrll 	int ret = 0;
   1089  1.38     skrll 
   1090  1.38     skrll 	mutex_enter(sc->sc_lock);
   1091  1.22    martin 	int change = ifp->if_flags ^ sc->sc_if_flags;
   1092  1.38     skrll 	sc->sc_if_flags = ifp->if_flags;
   1093  1.22    martin 
   1094  1.38     skrll 	if ((change & ~(IFF_CANTCHANGE|IFF_DEBUG)) != 0) {
   1095  1.38     skrll 		ret = ENETRESET;
   1096  1.38     skrll 		goto out;
   1097  1.38     skrll 	}
   1098  1.38     skrll 	if ((change & IFF_PROMISC) != 0) {
   1099  1.22    martin 		dwc_gmac_setmulti(sc);
   1100  1.38     skrll 	}
   1101  1.38     skrll out:
   1102  1.38     skrll 	mutex_exit(sc->sc_lock);
   1103  1.38     skrll 
   1104  1.38     skrll 	return ret;
   1105  1.22    martin }
   1106  1.22    martin 
   1107   1.1    martin static int
   1108   1.1    martin dwc_gmac_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1109   1.1    martin {
   1110  1.20  jmcneill 	struct dwc_gmac_softc *sc = ifp->if_softc;
   1111  1.38     skrll 	int error = 0;
   1112  1.38     skrll 
   1113  1.38     skrll 	int s = splnet();
   1114  1.38     skrll 	error = ether_ioctl(ifp, cmd, data);
   1115   1.1    martin 
   1116  1.38     skrll #ifdef DWCGMAC_MPSAFE
   1117  1.38     skrll 	splx(s);
   1118  1.38     skrll #endif
   1119   1.1    martin 
   1120  1.38     skrll 	if (error == ENETRESET) {
   1121   1.1    martin 		error = 0;
   1122   1.1    martin 		if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
   1123   1.1    martin 			;
   1124  1.22    martin 		else if (ifp->if_flags & IFF_RUNNING) {
   1125  1.22    martin 			/*
   1126  1.22    martin 			 * Multicast list has changed; set the hardware filter
   1127  1.22    martin 			 * accordingly.
   1128  1.22    martin 			 */
   1129  1.38     skrll 			mutex_enter(sc->sc_lock);
   1130  1.20  jmcneill 			dwc_gmac_setmulti(sc);
   1131  1.38     skrll 			mutex_exit(sc->sc_lock);
   1132  1.22    martin 		}
   1133   1.1    martin 	}
   1134   1.1    martin 
   1135  1.22    martin 	/* Try to get things going again */
   1136  1.22    martin 	if (ifp->if_flags & IFF_UP)
   1137  1.22    martin 		dwc_gmac_start(ifp);
   1138  1.22    martin 	sc->sc_if_flags = sc->sc_ec.ec_if.if_flags;
   1139  1.38     skrll 
   1140  1.38     skrll #ifndef DWCGMAC_MPSAFE
   1141   1.1    martin 	splx(s);
   1142  1.38     skrll #endif
   1143  1.38     skrll 
   1144   1.1    martin 	return error;
   1145   1.1    martin }
   1146   1.1    martin 
   1147   1.8    martin static void
   1148   1.8    martin dwc_gmac_tx_intr(struct dwc_gmac_softc *sc)
   1149   1.8    martin {
   1150  1.32    martin 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1151   1.8    martin 	struct dwc_gmac_tx_data *data;
   1152   1.8    martin 	struct dwc_gmac_dev_dmadesc *desc;
   1153  1.32    martin 	int i, nsegs;
   1154   1.8    martin 
   1155  1.38     skrll 	mutex_enter(&sc->sc_txq.t_mtx);
   1156  1.38     skrll 
   1157  1.32    martin 	for (i = sc->sc_txq.t_next; sc->sc_txq.t_queued > 0; i = TX_NEXT(i)) {
   1158   1.8    martin #ifdef DWC_GMAC_DEBUG
   1159   1.8    martin 		aprint_normal_dev(sc->sc_dev,
   1160   1.8    martin 		    "dwc_gmac_tx_intr: checking desc #%d (t_queued: %d)\n",
   1161   1.8    martin 		    i, sc->sc_txq.t_queued);
   1162   1.8    martin #endif
   1163   1.8    martin 
   1164  1.26    martin 		/*
   1165  1.26    martin 		 * i+1 does not need to be a valid descriptor,
   1166  1.26    martin 		 * this is just a special notion to just sync
   1167  1.26    martin 		 * a single tx descriptor (i)
   1168  1.26    martin 		 */
   1169  1.26    martin 		dwc_gmac_txdesc_sync(sc, i, i+1,
   1170   1.8    martin 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1171  1.11    martin 
   1172  1.32    martin 		desc = &sc->sc_txq.t_desc[i];
   1173  1.55    martin 		if (sc->sc_descm->tx_is_owned_by_dev(desc))
   1174   1.8    martin 			break;
   1175  1.11    martin 
   1176   1.8    martin 		data = &sc->sc_txq.t_data[i];
   1177   1.8    martin 		if (data->td_m == NULL)
   1178   1.8    martin 			continue;
   1179  1.32    martin 
   1180  1.32    martin 		ifp->if_opackets++;
   1181  1.32    martin 		nsegs = data->td_active->dm_nsegs;
   1182   1.8    martin 		bus_dmamap_sync(sc->sc_dmat, data->td_active, 0,
   1183   1.8    martin 		    data->td_active->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   1184   1.8    martin 		bus_dmamap_unload(sc->sc_dmat, data->td_active);
   1185   1.8    martin 
   1186   1.8    martin #ifdef DWC_GMAC_DEBUG
   1187   1.8    martin 		aprint_normal_dev(sc->sc_dev,
   1188   1.8    martin 		    "dwc_gmac_tx_intr: done with packet at desc #%d, "
   1189   1.8    martin 		    "freeing mbuf %p\n", i, data->td_m);
   1190   1.8    martin #endif
   1191   1.8    martin 
   1192   1.8    martin 		m_freem(data->td_m);
   1193   1.8    martin 		data->td_m = NULL;
   1194  1.32    martin 
   1195  1.32    martin 		sc->sc_txq.t_queued -= nsegs;
   1196   1.8    martin 	}
   1197   1.8    martin 
   1198   1.8    martin 	sc->sc_txq.t_next = i;
   1199   1.8    martin 
   1200   1.8    martin 	if (sc->sc_txq.t_queued < AWGE_TX_RING_COUNT) {
   1201  1.32    martin 		ifp->if_flags &= ~IFF_OACTIVE;
   1202   1.8    martin 	}
   1203  1.38     skrll 	mutex_exit(&sc->sc_txq.t_mtx);
   1204   1.8    martin }
   1205   1.8    martin 
   1206   1.8    martin static void
   1207   1.8    martin dwc_gmac_rx_intr(struct dwc_gmac_softc *sc)
   1208   1.8    martin {
   1209  1.11    martin 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1210  1.11    martin 	struct dwc_gmac_dev_dmadesc *desc;
   1211  1.11    martin 	struct dwc_gmac_rx_data *data;
   1212  1.11    martin 	bus_addr_t physaddr;
   1213  1.11    martin 	struct mbuf *m, *mnew;
   1214  1.11    martin 	int i, len, error;
   1215  1.11    martin 
   1216  1.38     skrll 	mutex_enter(&sc->sc_rxq.r_mtx);
   1217  1.11    martin 	for (i = sc->sc_rxq.r_cur; ; i = RX_NEXT(i)) {
   1218  1.11    martin 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dma_ring_map,
   1219  1.11    martin 		    RX_DESC_OFFSET(i), sizeof(*desc),
   1220  1.11    martin 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1221  1.11    martin 		desc = &sc->sc_rxq.r_desc[i];
   1222  1.11    martin 		data = &sc->sc_rxq.r_data[i];
   1223  1.11    martin 
   1224  1.55    martin 		if (sc->sc_descm->rx_is_owned_by_dev(desc))
   1225  1.11    martin 			break;
   1226  1.11    martin 
   1227  1.55    martin 		if (sc->sc_descm->rx_has_error(desc)) {
   1228  1.11    martin #ifdef DWC_GMAC_DEBUG
   1229  1.15    martin 			aprint_normal_dev(sc->sc_dev,
   1230  1.15    martin 			    "RX error: descriptor status %08x, skipping\n",
   1231  1.55    martin 			    le32toh(desc->ddesc_status0));
   1232  1.11    martin #endif
   1233  1.11    martin 			ifp->if_ierrors++;
   1234  1.11    martin 			goto skip;
   1235  1.11    martin 		}
   1236  1.11    martin 
   1237  1.55    martin 		len = sc->sc_descm->rx_get_len(desc);
   1238  1.11    martin 
   1239  1.11    martin #ifdef DWC_GMAC_DEBUG
   1240  1.15    martin 		aprint_normal_dev(sc->sc_dev,
   1241  1.15    martin 		    "rx int: device is done with descriptor #%d, len: %d\n",
   1242  1.15    martin 		    i, len);
   1243  1.11    martin #endif
   1244  1.11    martin 
   1245  1.11    martin 		/*
   1246  1.11    martin 		 * Try to get a new mbuf before passing this one
   1247  1.11    martin 		 * up, if that fails, drop the packet and reuse
   1248  1.11    martin 		 * the existing one.
   1249  1.11    martin 		 */
   1250  1.11    martin 		MGETHDR(mnew, M_DONTWAIT, MT_DATA);
   1251  1.11    martin 		if (mnew == NULL) {
   1252  1.11    martin 			ifp->if_ierrors++;
   1253  1.11    martin 			goto skip;
   1254  1.11    martin 		}
   1255  1.11    martin 		MCLGET(mnew, M_DONTWAIT);
   1256  1.11    martin 		if ((mnew->m_flags & M_EXT) == 0) {
   1257  1.11    martin 			m_freem(mnew);
   1258  1.11    martin 			ifp->if_ierrors++;
   1259  1.11    martin 			goto skip;
   1260  1.11    martin 		}
   1261  1.11    martin 
   1262  1.11    martin 		/* unload old DMA map */
   1263  1.11    martin 		bus_dmamap_sync(sc->sc_dmat, data->rd_map, 0,
   1264  1.11    martin 		    data->rd_map->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1265  1.11    martin 		bus_dmamap_unload(sc->sc_dmat, data->rd_map);
   1266  1.11    martin 
   1267  1.11    martin 		/* and reload with new mbuf */
   1268  1.11    martin 		error = bus_dmamap_load(sc->sc_dmat, data->rd_map,
   1269  1.11    martin 		    mtod(mnew, void*), MCLBYTES, NULL,
   1270  1.11    martin 		    BUS_DMA_READ | BUS_DMA_NOWAIT);
   1271  1.11    martin 		if (error != 0) {
   1272  1.11    martin 			m_freem(mnew);
   1273  1.11    martin 			/* try to reload old mbuf */
   1274  1.11    martin 			error = bus_dmamap_load(sc->sc_dmat, data->rd_map,
   1275  1.11    martin 			    mtod(data->rd_m, void*), MCLBYTES, NULL,
   1276  1.11    martin 			    BUS_DMA_READ | BUS_DMA_NOWAIT);
   1277  1.11    martin 			if (error != 0) {
   1278  1.11    martin 				panic("%s: could not load old rx mbuf",
   1279  1.11    martin 				    device_xname(sc->sc_dev));
   1280  1.11    martin 			}
   1281  1.11    martin 			ifp->if_ierrors++;
   1282  1.11    martin 			goto skip;
   1283  1.11    martin 		}
   1284  1.11    martin 		physaddr = data->rd_map->dm_segs[0].ds_addr;
   1285  1.11    martin 
   1286  1.11    martin 		/*
   1287  1.11    martin 		 * New mbuf loaded, update RX ring and continue
   1288  1.11    martin 		 */
   1289  1.11    martin 		m = data->rd_m;
   1290  1.11    martin 		data->rd_m = mnew;
   1291  1.11    martin 		desc->ddesc_data = htole32(physaddr);
   1292  1.11    martin 
   1293  1.11    martin 		/* finalize mbuf */
   1294  1.11    martin 		m->m_pkthdr.len = m->m_len = len;
   1295  1.36     ozaki 		m_set_rcvif(m, ifp);
   1296  1.19      matt 		m->m_flags |= M_HASFCS;
   1297  1.11    martin 
   1298  1.39     skrll 		if_percpuq_enqueue(sc->sc_ipq, m);
   1299  1.11    martin 
   1300  1.11    martin skip:
   1301  1.27      matt 		bus_dmamap_sync(sc->sc_dmat, data->rd_map, 0,
   1302  1.27      matt 		    data->rd_map->dm_mapsize, BUS_DMASYNC_PREREAD);
   1303  1.55    martin 
   1304  1.55    martin 		sc->sc_descm->rx_init_flags(desc);
   1305  1.55    martin 		sc->sc_descm->rx_set_len(desc, AWGE_MAX_PACKET);
   1306  1.55    martin 		sc->sc_descm->rx_set_owned_by_dev(desc);
   1307  1.55    martin 
   1308  1.11    martin 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dma_ring_map,
   1309  1.11    martin 		    RX_DESC_OFFSET(i), sizeof(*desc),
   1310  1.11    martin 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1311  1.11    martin 	}
   1312  1.11    martin 
   1313  1.11    martin 	/* update RX pointer */
   1314  1.11    martin 	sc->sc_rxq.r_cur = i;
   1315  1.11    martin 
   1316  1.38     skrll 	mutex_exit(&sc->sc_rxq.r_mtx);
   1317   1.8    martin }
   1318   1.8    martin 
   1319  1.22    martin /*
   1320  1.24     skrll  * Reverse order of bits - http://aggregate.org/MAGIC/#Bit%20Reversal
   1321  1.22    martin  */
   1322  1.22    martin static uint32_t
   1323  1.22    martin bitrev32(uint32_t x)
   1324  1.22    martin {
   1325  1.22    martin 	x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
   1326  1.22    martin 	x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
   1327  1.22    martin 	x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
   1328  1.22    martin 	x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
   1329  1.22    martin 
   1330  1.22    martin 	return (x >> 16) | (x << 16);
   1331  1.22    martin }
   1332  1.22    martin 
   1333  1.20  jmcneill static void
   1334  1.20  jmcneill dwc_gmac_setmulti(struct dwc_gmac_softc *sc)
   1335  1.20  jmcneill {
   1336  1.20  jmcneill 	struct ifnet * const ifp = &sc->sc_ec.ec_if;
   1337  1.20  jmcneill 	struct ether_multi *enm;
   1338  1.20  jmcneill 	struct ether_multistep step;
   1339  1.20  jmcneill 	uint32_t hashes[2] = { 0, 0 };
   1340  1.22    martin 	uint32_t ffilt, h;
   1341  1.38     skrll 	int mcnt;
   1342  1.22    martin 
   1343  1.38     skrll 	KASSERT(mutex_owned(sc->sc_lock));
   1344  1.20  jmcneill 
   1345  1.20  jmcneill 	ffilt = bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_FFILT);
   1346  1.38     skrll 
   1347  1.20  jmcneill 	if (ifp->if_flags & IFF_PROMISC) {
   1348  1.22    martin 		ffilt |= AWIN_GMAC_MAC_FFILT_PR;
   1349  1.22    martin 		goto special_filter;
   1350  1.20  jmcneill 	}
   1351  1.20  jmcneill 
   1352  1.20  jmcneill 	ifp->if_flags &= ~IFF_ALLMULTI;
   1353  1.22    martin 	ffilt &= ~(AWIN_GMAC_MAC_FFILT_PM|AWIN_GMAC_MAC_FFILT_PR);
   1354  1.20  jmcneill 
   1355  1.20  jmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_HTLOW, 0);
   1356  1.20  jmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_HTHIGH, 0);
   1357  1.20  jmcneill 
   1358  1.20  jmcneill 	ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
   1359  1.20  jmcneill 	mcnt = 0;
   1360  1.20  jmcneill 	while (enm != NULL) {
   1361  1.20  jmcneill 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
   1362  1.22    martin 		    ETHER_ADDR_LEN) != 0) {
   1363  1.22    martin 			ffilt |= AWIN_GMAC_MAC_FFILT_PM;
   1364  1.22    martin 			ifp->if_flags |= IFF_ALLMULTI;
   1365  1.22    martin 			goto special_filter;
   1366  1.22    martin 		}
   1367  1.20  jmcneill 
   1368  1.22    martin 		h = bitrev32(
   1369  1.22    martin 			~ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN)
   1370  1.22    martin 		    ) >> 26;
   1371  1.20  jmcneill 		hashes[h >> 5] |= (1 << (h & 0x1f));
   1372  1.20  jmcneill 
   1373  1.20  jmcneill 		mcnt++;
   1374  1.20  jmcneill 		ETHER_NEXT_MULTI(step, enm);
   1375  1.20  jmcneill 	}
   1376  1.20  jmcneill 
   1377  1.20  jmcneill 	if (mcnt)
   1378  1.20  jmcneill 		ffilt |= AWIN_GMAC_MAC_FFILT_HMC;
   1379  1.20  jmcneill 	else
   1380  1.20  jmcneill 		ffilt &= ~AWIN_GMAC_MAC_FFILT_HMC;
   1381  1.20  jmcneill 
   1382  1.20  jmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_FFILT, ffilt);
   1383  1.20  jmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_HTLOW,
   1384  1.20  jmcneill 	    hashes[0]);
   1385  1.20  jmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_HTHIGH,
   1386  1.20  jmcneill 	    hashes[1]);
   1387  1.22    martin 	sc->sc_if_flags = sc->sc_ec.ec_if.if_flags;
   1388  1.22    martin 
   1389  1.22    martin #ifdef DWC_GMAC_DEBUG
   1390  1.22    martin 	dwc_gmac_dump_ffilt(sc, ffilt);
   1391  1.22    martin #endif
   1392  1.22    martin 	return;
   1393  1.22    martin 
   1394  1.22    martin special_filter:
   1395  1.22    martin #ifdef DWC_GMAC_DEBUG
   1396  1.22    martin 	dwc_gmac_dump_ffilt(sc, ffilt);
   1397  1.22    martin #endif
   1398  1.22    martin 	/* no MAC hashes, ALLMULTI or PROMISC */
   1399  1.22    martin 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_FFILT,
   1400  1.22    martin 	    ffilt);
   1401  1.22    martin 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_HTLOW,
   1402  1.22    martin 	    0xffffffff);
   1403  1.22    martin 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_HTHIGH,
   1404  1.22    martin 	    0xffffffff);
   1405  1.22    martin 	sc->sc_if_flags = sc->sc_ec.ec_if.if_flags;
   1406  1.20  jmcneill }
   1407  1.20  jmcneill 
   1408   1.1    martin int
   1409   1.1    martin dwc_gmac_intr(struct dwc_gmac_softc *sc)
   1410   1.1    martin {
   1411   1.1    martin 	uint32_t status, dma_status;
   1412   1.8    martin 	int rv = 0;
   1413   1.1    martin 
   1414  1.38     skrll 	if (sc->sc_stopping)
   1415  1.38     skrll 		return 0;
   1416  1.38     skrll 
   1417   1.1    martin 	status = bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_INTR);
   1418   1.2    martin 	if (status & AWIN_GMAC_MII_IRQ) {
   1419   1.1    martin 		(void)bus_space_read_4(sc->sc_bst, sc->sc_bsh,
   1420   1.1    martin 		    AWIN_GMAC_MII_STATUS);
   1421   1.8    martin 		rv = 1;
   1422   1.2    martin 		mii_pollstat(&sc->sc_mii);
   1423   1.2    martin 	}
   1424   1.1    martin 
   1425   1.1    martin 	dma_status = bus_space_read_4(sc->sc_bst, sc->sc_bsh,
   1426   1.1    martin 	    AWIN_GMAC_DMA_STATUS);
   1427   1.1    martin 
   1428   1.8    martin 	if (dma_status & (GMAC_DMA_INT_NIE|GMAC_DMA_INT_AIE))
   1429   1.8    martin 		rv = 1;
   1430   1.1    martin 
   1431   1.8    martin 	if (dma_status & GMAC_DMA_INT_TIE)
   1432   1.8    martin 		dwc_gmac_tx_intr(sc);
   1433   1.1    martin 
   1434   1.8    martin 	if (dma_status & GMAC_DMA_INT_RIE)
   1435   1.8    martin 		dwc_gmac_rx_intr(sc);
   1436   1.8    martin 
   1437   1.8    martin 	/*
   1438   1.8    martin 	 * Check error conditions
   1439   1.8    martin 	 */
   1440   1.8    martin 	if (dma_status & GMAC_DMA_INT_ERRORS) {
   1441   1.8    martin 		sc->sc_ec.ec_if.if_oerrors++;
   1442   1.8    martin #ifdef DWC_GMAC_DEBUG
   1443   1.8    martin 		dwc_dump_and_abort(sc, "interrupt error condition");
   1444   1.8    martin #endif
   1445   1.8    martin 	}
   1446   1.8    martin 
   1447   1.8    martin 	/* ack interrupt */
   1448   1.8    martin 	if (dma_status)
   1449   1.8    martin 		bus_space_write_4(sc->sc_bst, sc->sc_bsh,
   1450   1.8    martin 		    AWIN_GMAC_DMA_STATUS, dma_status & GMAC_DMA_INT_MASK);
   1451   1.8    martin 
   1452  1.28    martin 	/*
   1453  1.28    martin 	 * Get more packets
   1454  1.28    martin 	 */
   1455  1.28    martin 	if (rv)
   1456  1.40     ozaki 		if_schedule_deferred_start(&sc->sc_ec.ec_if);
   1457  1.28    martin 
   1458   1.8    martin 	return rv;
   1459   1.1    martin }
   1460   1.7    martin 
   1461  1.55    martin static void
   1462  1.55    martin dwc_gmac_desc_set_owned_by_dev(struct dwc_gmac_dev_dmadesc *desc)
   1463  1.55    martin {
   1464  1.55    martin 
   1465  1.55    martin 	desc->ddesc_status0 |= htole32(DDESC_STATUS_OWNEDBYDEV);
   1466  1.55    martin }
   1467  1.55    martin 
   1468  1.55    martin static int
   1469  1.55    martin dwc_gmac_desc_is_owned_by_dev(struct dwc_gmac_dev_dmadesc *desc)
   1470  1.55    martin {
   1471  1.55    martin 
   1472  1.55    martin 	return !!(le32toh(desc->ddesc_status0) & DDESC_STATUS_OWNEDBYDEV);
   1473  1.55    martin }
   1474  1.55    martin 
   1475  1.55    martin static void
   1476  1.55    martin dwc_gmac_desc_std_set_len(struct dwc_gmac_dev_dmadesc *desc, int len)
   1477  1.55    martin {
   1478  1.55    martin 	uint32_t cntl = le32toh(desc->ddesc_cntl1);
   1479  1.55    martin 
   1480  1.55    martin 	desc->ddesc_cntl1 = htole32((cntl & ~DDESC_CNTL_SIZE1MASK) |
   1481  1.55    martin 		__SHIFTIN(len, DDESC_CNTL_SIZE1MASK));
   1482  1.55    martin }
   1483  1.55    martin 
   1484  1.55    martin static uint32_t
   1485  1.55    martin dwc_gmac_desc_std_get_len(struct dwc_gmac_dev_dmadesc *desc)
   1486  1.55    martin {
   1487  1.55    martin 
   1488  1.55    martin 	return __SHIFTOUT(le32toh(desc->ddesc_status0), DDESC_STATUS_FRMLENMSK);
   1489  1.55    martin }
   1490  1.55    martin 
   1491  1.55    martin static void
   1492  1.55    martin dwc_gmac_desc_std_tx_init_flags(struct dwc_gmac_dev_dmadesc *desc)
   1493  1.55    martin {
   1494  1.55    martin 
   1495  1.55    martin 	desc->ddesc_status0 = 0;
   1496  1.55    martin 	desc->ddesc_cntl1 = htole32(DDESC_CNTL_TXCHAIN);
   1497  1.55    martin }
   1498  1.55    martin 
   1499  1.55    martin static void
   1500  1.55    martin dwc_gmac_desc_std_tx_set_first_frag(struct dwc_gmac_dev_dmadesc *desc)
   1501  1.55    martin {
   1502  1.55    martin 	uint32_t cntl = le32toh(desc->ddesc_cntl1);
   1503  1.55    martin 
   1504  1.55    martin 	desc->ddesc_cntl1 = htole32(cntl | DDESC_CNTL_TXFIRST);
   1505  1.55    martin }
   1506  1.55    martin 
   1507  1.55    martin static void
   1508  1.55    martin dwc_gmac_desc_std_tx_set_last_frag(struct dwc_gmac_dev_dmadesc *desc)
   1509  1.55    martin {
   1510  1.55    martin 	uint32_t cntl = le32toh(desc->ddesc_cntl1);
   1511  1.55    martin 
   1512  1.55    martin 	desc->ddesc_cntl1 = htole32(cntl |
   1513  1.55    martin 		DDESC_CNTL_TXLAST | DDESC_CNTL_TXINT);
   1514  1.55    martin }
   1515  1.55    martin 
   1516  1.55    martin static void
   1517  1.55    martin dwc_gmac_desc_std_rx_init_flags(struct dwc_gmac_dev_dmadesc *desc)
   1518  1.55    martin {
   1519  1.55    martin 
   1520  1.55    martin 	desc->ddesc_status0 = 0;
   1521  1.55    martin 	desc->ddesc_cntl1 = htole32(DDESC_CNTL_TXCHAIN);
   1522  1.55    martin }
   1523  1.55    martin 
   1524  1.55    martin static int
   1525  1.55    martin dwc_gmac_desc_std_rx_has_error(struct dwc_gmac_dev_dmadesc *desc) {
   1526  1.55    martin 	return !!(le32toh(desc->ddesc_status0) &
   1527  1.55    martin 		(DDESC_STATUS_RXERROR | DDESC_STATUS_RXTRUNCATED));
   1528  1.55    martin }
   1529  1.55    martin 
   1530  1.55    martin static void
   1531  1.55    martin dwc_gmac_desc_enh_set_len(struct dwc_gmac_dev_dmadesc *desc, int len)
   1532  1.55    martin {
   1533  1.55    martin 	uint32_t tdes1 = le32toh(desc->ddesc_cntl1);
   1534  1.55    martin 
   1535  1.55    martin 	desc->ddesc_cntl1 = htole32((tdes1 & ~DDESC_DES1_SIZE1MASK) |
   1536  1.55    martin 		__SHIFTIN(len, DDESC_DES1_SIZE1MASK));
   1537  1.55    martin }
   1538  1.55    martin 
   1539  1.55    martin static uint32_t
   1540  1.55    martin dwc_gmac_desc_enh_get_len(struct dwc_gmac_dev_dmadesc *desc)
   1541  1.55    martin {
   1542  1.55    martin 
   1543  1.55    martin 	return __SHIFTOUT(le32toh(desc->ddesc_status0), DDESC_RDES0_FL);
   1544  1.55    martin }
   1545  1.55    martin 
   1546  1.55    martin static void
   1547  1.55    martin dwc_gmac_desc_enh_tx_init_flags(struct dwc_gmac_dev_dmadesc *desc)
   1548  1.55    martin {
   1549  1.55    martin 
   1550  1.55    martin 	desc->ddesc_status0 = htole32(DDESC_TDES0_TCH);
   1551  1.55    martin 	desc->ddesc_cntl1 = 0;
   1552  1.55    martin }
   1553  1.55    martin 
   1554  1.55    martin static void
   1555  1.55    martin dwc_gmac_desc_enh_tx_set_first_frag(struct dwc_gmac_dev_dmadesc *desc)
   1556  1.55    martin {
   1557  1.55    martin 	uint32_t tdes0 = le32toh(desc->ddesc_status0);
   1558  1.55    martin 
   1559  1.55    martin 	desc->ddesc_status0 = htole32(tdes0 | DDESC_TDES0_FS);
   1560  1.55    martin }
   1561  1.55    martin 
   1562  1.55    martin static void
   1563  1.55    martin dwc_gmac_desc_enh_tx_set_last_frag(struct dwc_gmac_dev_dmadesc *desc)
   1564  1.55    martin {
   1565  1.55    martin 	uint32_t tdes0 = le32toh(desc->ddesc_status0);
   1566  1.55    martin 
   1567  1.55    martin 	desc->ddesc_status0 = htole32(tdes0 | DDESC_TDES0_LS | DDESC_TDES0_IC);
   1568  1.55    martin }
   1569  1.55    martin 
   1570  1.55    martin static void
   1571  1.55    martin dwc_gmac_desc_enh_rx_init_flags(struct dwc_gmac_dev_dmadesc *desc)
   1572  1.55    martin {
   1573  1.55    martin 
   1574  1.55    martin 	desc->ddesc_status0 = 0;
   1575  1.55    martin 	desc->ddesc_cntl1 = htole32(DDESC_RDES1_RCH);
   1576  1.55    martin }
   1577  1.55    martin 
   1578  1.55    martin static int
   1579  1.55    martin dwc_gmac_desc_enh_rx_has_error(struct dwc_gmac_dev_dmadesc *desc)
   1580  1.55    martin {
   1581  1.55    martin 
   1582  1.55    martin 	return !!(le32toh(desc->ddesc_status0) &
   1583  1.55    martin 		(DDESC_RDES0_ES | DDESC_RDES0_LE));
   1584  1.55    martin }
   1585  1.55    martin 
   1586   1.7    martin #ifdef DWC_GMAC_DEBUG
   1587   1.7    martin static void
   1588   1.7    martin dwc_gmac_dump_dma(struct dwc_gmac_softc *sc)
   1589   1.7    martin {
   1590   1.7    martin 	aprint_normal_dev(sc->sc_dev, "busmode: %08x\n",
   1591   1.7    martin 	    bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_BUSMODE));
   1592   1.7    martin 	aprint_normal_dev(sc->sc_dev, "tx poll: %08x\n",
   1593   1.7    martin 	    bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_TXPOLL));
   1594   1.7    martin 	aprint_normal_dev(sc->sc_dev, "rx poll: %08x\n",
   1595   1.7    martin 	    bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_RXPOLL));
   1596   1.7    martin 	aprint_normal_dev(sc->sc_dev, "rx descriptors: %08x\n",
   1597   1.7    martin 	    bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_RX_ADDR));
   1598   1.7    martin 	aprint_normal_dev(sc->sc_dev, "tx descriptors: %08x\n",
   1599   1.7    martin 	    bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_TX_ADDR));
   1600   1.7    martin 	aprint_normal_dev(sc->sc_dev, "status: %08x\n",
   1601   1.7    martin 	    bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_STATUS));
   1602   1.7    martin 	aprint_normal_dev(sc->sc_dev, "op mode: %08x\n",
   1603   1.7    martin 	    bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_OPMODE));
   1604   1.7    martin 	aprint_normal_dev(sc->sc_dev, "int enable: %08x\n",
   1605   1.7    martin 	    bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_INTENABLE));
   1606   1.7    martin 	aprint_normal_dev(sc->sc_dev, "cur tx: %08x\n",
   1607   1.7    martin 	    bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_CUR_TX_DESC));
   1608   1.7    martin 	aprint_normal_dev(sc->sc_dev, "cur rx: %08x\n",
   1609   1.7    martin 	    bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_CUR_RX_DESC));
   1610   1.7    martin 	aprint_normal_dev(sc->sc_dev, "cur tx buffer: %08x\n",
   1611   1.7    martin 	    bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_CUR_TX_BUFADDR));
   1612   1.7    martin 	aprint_normal_dev(sc->sc_dev, "cur rx buffer: %08x\n",
   1613   1.7    martin 	    bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_CUR_RX_BUFADDR));
   1614   1.7    martin }
   1615   1.7    martin 
   1616   1.7    martin static void
   1617   1.7    martin dwc_gmac_dump_tx_desc(struct dwc_gmac_softc *sc)
   1618   1.7    martin {
   1619   1.7    martin 	int i;
   1620   1.7    martin 
   1621   1.8    martin 	aprint_normal_dev(sc->sc_dev, "TX queue: cur=%d, next=%d, queued=%d\n",
   1622   1.8    martin 	    sc->sc_txq.t_cur, sc->sc_txq.t_next, sc->sc_txq.t_queued);
   1623   1.8    martin 	aprint_normal_dev(sc->sc_dev, "TX DMA descriptors:\n");
   1624   1.7    martin 	for (i = 0; i < AWGE_TX_RING_COUNT; i++) {
   1625   1.7    martin 		struct dwc_gmac_dev_dmadesc *desc = &sc->sc_txq.t_desc[i];
   1626  1.15    martin 		aprint_normal("#%d (%08lx): status: %08x cntl: %08x "
   1627  1.15    martin 		    "data: %08x next: %08x\n",
   1628  1.15    martin 		    i, sc->sc_txq.t_physaddr +
   1629  1.15    martin 			i*sizeof(struct dwc_gmac_dev_dmadesc),
   1630  1.55    martin 		    le32toh(desc->ddesc_status0), le32toh(desc->ddesc_cntl1),
   1631   1.7    martin 		    le32toh(desc->ddesc_data), le32toh(desc->ddesc_next));
   1632   1.7    martin 	}
   1633   1.7    martin }
   1634   1.8    martin 
   1635   1.8    martin static void
   1636  1.11    martin dwc_gmac_dump_rx_desc(struct dwc_gmac_softc *sc)
   1637  1.11    martin {
   1638  1.11    martin 	int i;
   1639  1.11    martin 
   1640  1.11    martin 	aprint_normal_dev(sc->sc_dev, "RX queue: cur=%d, next=%d\n",
   1641  1.11    martin 	    sc->sc_rxq.r_cur, sc->sc_rxq.r_next);
   1642  1.11    martin 	aprint_normal_dev(sc->sc_dev, "RX DMA descriptors:\n");
   1643  1.11    martin 	for (i = 0; i < AWGE_RX_RING_COUNT; i++) {
   1644  1.11    martin 		struct dwc_gmac_dev_dmadesc *desc = &sc->sc_rxq.r_desc[i];
   1645  1.15    martin 		aprint_normal("#%d (%08lx): status: %08x cntl: %08x "
   1646  1.15    martin 		    "data: %08x next: %08x\n",
   1647  1.15    martin 		    i, sc->sc_rxq.r_physaddr +
   1648  1.15    martin 			i*sizeof(struct dwc_gmac_dev_dmadesc),
   1649  1.55    martin 		    le32toh(desc->ddesc_status0), le32toh(desc->ddesc_cntl1),
   1650  1.11    martin 		    le32toh(desc->ddesc_data), le32toh(desc->ddesc_next));
   1651  1.11    martin 	}
   1652  1.11    martin }
   1653  1.11    martin 
   1654  1.11    martin static void
   1655  1.10    martin dwc_dump_status(struct dwc_gmac_softc *sc)
   1656   1.8    martin {
   1657   1.8    martin 	uint32_t status = bus_space_read_4(sc->sc_bst, sc->sc_bsh,
   1658   1.8    martin 	     AWIN_GMAC_MAC_INTR);
   1659   1.8    martin 	uint32_t dma_status = bus_space_read_4(sc->sc_bst, sc->sc_bsh,
   1660   1.8    martin 	     AWIN_GMAC_DMA_STATUS);
   1661   1.8    martin 	char buf[200];
   1662   1.8    martin 
   1663   1.8    martin 	/* print interrupt state */
   1664   1.8    martin 	snprintb(buf, sizeof(buf), "\177\20"
   1665  1.10    martin 	    "b\x10""NI\0"
   1666  1.10    martin 	    "b\x0f""AI\0"
   1667  1.10    martin 	    "b\x0e""ER\0"
   1668  1.10    martin 	    "b\x0d""FB\0"
   1669  1.10    martin 	    "b\x0a""ET\0"
   1670  1.10    martin 	    "b\x09""RW\0"
   1671  1.10    martin 	    "b\x08""RS\0"
   1672  1.10    martin 	    "b\x07""RU\0"
   1673  1.10    martin 	    "b\x06""RI\0"
   1674  1.10    martin 	    "b\x05""UN\0"
   1675  1.10    martin 	    "b\x04""OV\0"
   1676  1.10    martin 	    "b\x03""TJ\0"
   1677  1.10    martin 	    "b\x02""TU\0"
   1678  1.10    martin 	    "b\x01""TS\0"
   1679  1.10    martin 	    "b\x00""TI\0"
   1680   1.8    martin 	    "\0", dma_status);
   1681  1.10    martin 	aprint_normal_dev(sc->sc_dev, "INTR status: %08x, DMA status: %s\n",
   1682   1.8    martin 	    status, buf);
   1683  1.10    martin }
   1684   1.8    martin 
   1685  1.10    martin static void
   1686  1.10    martin dwc_dump_and_abort(struct dwc_gmac_softc *sc, const char *msg)
   1687  1.10    martin {
   1688  1.10    martin 	dwc_dump_status(sc);
   1689  1.22    martin 	dwc_gmac_dump_ffilt(sc,
   1690  1.22    martin 	    bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_FFILT));
   1691   1.8    martin 	dwc_gmac_dump_dma(sc);
   1692   1.8    martin 	dwc_gmac_dump_tx_desc(sc);
   1693  1.11    martin 	dwc_gmac_dump_rx_desc(sc);
   1694   1.8    martin 
   1695  1.21     joerg 	panic("%s", msg);
   1696   1.8    martin }
   1697  1.22    martin 
   1698  1.22    martin static void dwc_gmac_dump_ffilt(struct dwc_gmac_softc *sc, uint32_t ffilt)
   1699  1.22    martin {
   1700  1.22    martin 	char buf[200];
   1701  1.22    martin 
   1702  1.22    martin 	/* print filter setup */
   1703  1.22    martin 	snprintb(buf, sizeof(buf), "\177\20"
   1704  1.22    martin 	    "b\x1f""RA\0"
   1705  1.22    martin 	    "b\x0a""HPF\0"
   1706  1.22    martin 	    "b\x09""SAF\0"
   1707  1.22    martin 	    "b\x08""SAIF\0"
   1708  1.22    martin 	    "b\x05""DBF\0"
   1709  1.22    martin 	    "b\x04""PM\0"
   1710  1.22    martin 	    "b\x03""DAIF\0"
   1711  1.22    martin 	    "b\x02""HMC\0"
   1712  1.22    martin 	    "b\x01""HUC\0"
   1713  1.22    martin 	    "b\x00""PR\0"
   1714  1.22    martin 	    "\0", ffilt);
   1715  1.22    martin 	aprint_normal_dev(sc->sc_dev, "FFILT: %s\n", buf);
   1716  1.22    martin }
   1717   1.7    martin #endif
   1718