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