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