Home | History | Annotate | Line # | Download | only in ic
dwc_eqos.c revision 1.56
      1  1.56  gutterid /* $NetBSD: dwc_eqos.c,v 1.56 2026/09/03 02:12:14 gutteridge Exp $ */
      2   1.1  jmcneill 
      3   1.1  jmcneill /*-
      4  1.45  jmcneill  * Copyright (c) 2022-2026 Jared McNeill <jmcneill (at) invisible.ca>
      5   1.1  jmcneill  * All rights reserved.
      6   1.1  jmcneill  *
      7   1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8   1.1  jmcneill  * modification, are permitted provided that the following conditions
      9   1.1  jmcneill  * are met:
     10   1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12   1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15   1.1  jmcneill  *
     16   1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17   1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18   1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19   1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20   1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21   1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22   1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23   1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24   1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25   1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26   1.1  jmcneill  * SUCH DAMAGE.
     27   1.1  jmcneill  */
     28   1.1  jmcneill 
     29   1.1  jmcneill /*
     30   1.1  jmcneill  * DesignWare Ethernet Quality-of-Service controller
     31  1.20   msaitoh  *
     32  1.20   msaitoh  * TODO:
     33  1.20   msaitoh  *	Multiqueue support.
     34  1.20   msaitoh  *	Add watchdog timer.
     35  1.20   msaitoh  *	Add detach function.
     36   1.1  jmcneill  */
     37   1.1  jmcneill 
     38   1.1  jmcneill #include <sys/cdefs.h>
     39  1.56  gutterid __KERNEL_RCSID(0, "$NetBSD: dwc_eqos.c,v 1.56 2026/09/03 02:12:14 gutteridge Exp $");
     40   1.1  jmcneill 
     41   1.1  jmcneill #include <sys/param.h>
     42   1.1  jmcneill #include <sys/bus.h>
     43   1.1  jmcneill #include <sys/device.h>
     44   1.1  jmcneill #include <sys/intr.h>
     45   1.1  jmcneill #include <sys/systm.h>
     46   1.1  jmcneill #include <sys/kernel.h>
     47   1.1  jmcneill #include <sys/mutex.h>
     48   1.1  jmcneill #include <sys/callout.h>
     49   1.1  jmcneill #include <sys/cprng.h>
     50   1.2       mrg #include <sys/evcnt.h>
     51  1.25   msaitoh #include <sys/sysctl.h>
     52   1.1  jmcneill #include <sys/rndsource.h>
     53   1.1  jmcneill 
     54   1.1  jmcneill #include <net/if.h>
     55   1.1  jmcneill #include <net/if_dl.h>
     56   1.1  jmcneill #include <net/if_ether.h>
     57   1.1  jmcneill #include <net/if_media.h>
     58   1.1  jmcneill #include <net/bpf.h>
     59   1.1  jmcneill 
     60   1.1  jmcneill #include <dev/mii/miivar.h>
     61   1.1  jmcneill 
     62   1.1  jmcneill #include <dev/ic/dwc_eqos_reg.h>
     63   1.1  jmcneill #include <dev/ic/dwc_eqos_var.h>
     64   1.1  jmcneill 
     65  1.13       ryo #define	EQOS_MAX_MTU		9000	/* up to 16364? but not tested */
     66  1.13       ryo #define	EQOS_TXDMA_SIZE		(EQOS_MAX_MTU + ETHER_HDR_LEN + ETHER_CRC_LEN)
     67  1.13       ryo #define	EQOS_RXDMA_SIZE		2048	/* Fixed value by hardware */
     68  1.13       ryo CTASSERT(MCLBYTES >= EQOS_RXDMA_SIZE);
     69  1.53  jmcneill #define	EQOS_RXBUF_SIZE		(EQOS_RXDMA_SIZE * EQOS_DMA_RXBUF_COUNT)
     70  1.55  jmcneill #define	EQOS_TX_PACKET_PER_INTR	16
     71   1.8    martin 
     72   1.1  jmcneill #ifdef EQOS_DEBUG
     73  1.25   msaitoh #define	EDEB_NOTE		(1U << 0)
     74  1.25   msaitoh #define	EDEB_INTR		(1U << 1)
     75  1.25   msaitoh #define	EDEB_RXRING		(1U << 2)
     76  1.25   msaitoh #define	EDEB_TXRING		(1U << 3)
     77  1.29   msaitoh unsigned int eqos_debug;	/* Default value */
     78  1.25   msaitoh #define	DPRINTF(FLAG, FORMAT, ...)			 \
     79  1.25   msaitoh 	if (sc->sc_debug & FLAG)			 \
     80   1.8    martin 		device_printf(sc->sc_dev, "%s: " FORMAT, \
     81   1.8    martin 		    __func__, ##__VA_ARGS__)
     82   1.1  jmcneill #else
     83   1.8    martin #define	DPRINTF(FLAG, FORMAT, ...)	((void)0)
     84   1.1  jmcneill #endif
     85   1.1  jmcneill 
     86   1.1  jmcneill #define	CALLOUT_FLAGS		CALLOUT_MPSAFE
     87   1.1  jmcneill 
     88  1.21   msaitoh #define	DESC_BOUNDARY		((sizeof(bus_size_t) > 4) ? (1ULL << 32) : 0)
     89   1.1  jmcneill #define	DESC_ALIGN		sizeof(struct eqos_dma_desc)
     90   1.1  jmcneill #define	TX_DESC_COUNT		EQOS_DMA_DESC_COUNT
     91   1.1  jmcneill #define	TX_DESC_SIZE		(TX_DESC_COUNT * DESC_ALIGN)
     92   1.1  jmcneill #define	RX_DESC_COUNT		EQOS_DMA_DESC_COUNT
     93   1.1  jmcneill #define	RX_DESC_SIZE		(RX_DESC_COUNT * DESC_ALIGN)
     94   1.1  jmcneill #define	MII_BUSY_RETRY		1000
     95   1.1  jmcneill 
     96   1.1  jmcneill #define	DESC_OFF(n)		((n) * sizeof(struct eqos_dma_desc))
     97   1.1  jmcneill #define	TX_SKIP(n, o)		(((n) + (o)) % TX_DESC_COUNT)
     98   1.1  jmcneill #define	TX_NEXT(n)		TX_SKIP(n, 1)
     99   1.1  jmcneill #define	RX_NEXT(n)		(((n) + 1) % RX_DESC_COUNT)
    100   1.1  jmcneill 
    101   1.1  jmcneill #define	TX_MAX_SEGS		128
    102   1.1  jmcneill 
    103   1.1  jmcneill #define	EQOS_LOCK(sc)			mutex_enter(&(sc)->sc_lock)
    104   1.1  jmcneill #define	EQOS_UNLOCK(sc)			mutex_exit(&(sc)->sc_lock)
    105   1.1  jmcneill #define	EQOS_ASSERT_LOCKED(sc)		KASSERT(mutex_owned(&(sc)->sc_lock))
    106   1.1  jmcneill 
    107   1.1  jmcneill #define	EQOS_TXLOCK(sc)			mutex_enter(&(sc)->sc_txlock)
    108   1.1  jmcneill #define	EQOS_TXUNLOCK(sc)		mutex_exit(&(sc)->sc_txlock)
    109   1.1  jmcneill #define	EQOS_ASSERT_TXLOCKED(sc)	KASSERT(mutex_owned(&(sc)->sc_txlock))
    110   1.1  jmcneill 
    111   1.1  jmcneill #define	EQOS_HW_FEATURE_ADDR64_32BIT(sc)				\
    112   1.1  jmcneill 	(((sc)->sc_hw_feature[1] & GMAC_MAC_HW_FEATURE1_ADDR64_MASK) ==	\
    113   1.1  jmcneill 	    GMAC_MAC_HW_FEATURE1_ADDR64_32BIT)
    114   1.1  jmcneill 
    115   1.1  jmcneill 
    116   1.1  jmcneill #define	RD4(sc, reg)			\
    117   1.1  jmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
    118   1.1  jmcneill #define	WR4(sc, reg, val)		\
    119   1.1  jmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    120   1.1  jmcneill 
    121  1.45  jmcneill /*
    122  1.45  jmcneill  * Request interrupt on completion for every "rx_batch" # packets.
    123  1.45  jmcneill  */
    124  1.45  jmcneill int eqos_rx_batch = 16;
    125  1.45  jmcneill 
    126  1.45  jmcneill /*
    127  1.45  jmcneill  * RX interrupt watchdog is triggered after "rx_wdt_count" * 256 system
    128  1.45  jmcneill  * clock cycles.
    129  1.45  jmcneill  */
    130  1.45  jmcneill int eqos_rx_wdt_count = 160;
    131  1.45  jmcneill 
    132  1.25   msaitoh static void	eqos_init_sysctls(struct eqos_softc *);
    133  1.25   msaitoh static int	eqos_sysctl_tx_cur_handler(SYSCTLFN_PROTO);
    134  1.25   msaitoh static int	eqos_sysctl_tx_end_handler(SYSCTLFN_PROTO);
    135  1.25   msaitoh static int	eqos_sysctl_rx_cur_handler(SYSCTLFN_PROTO);
    136  1.25   msaitoh static int	eqos_sysctl_rx_end_handler(SYSCTLFN_PROTO);
    137  1.25   msaitoh #ifdef EQOS_DEBUG
    138  1.25   msaitoh static int	eqos_sysctl_debug_handler(SYSCTLFN_PROTO);
    139  1.25   msaitoh #endif
    140  1.25   msaitoh 
    141   1.1  jmcneill static int
    142   1.1  jmcneill eqos_mii_readreg(device_t dev, int phy, int reg, uint16_t *val)
    143   1.1  jmcneill {
    144  1.15     skrll 	struct eqos_softc * const sc = device_private(dev);
    145   1.1  jmcneill 	uint32_t addr;
    146   1.1  jmcneill 	int retry;
    147   1.1  jmcneill 
    148   1.1  jmcneill 	addr = sc->sc_clock_range |
    149   1.1  jmcneill 	    (phy << GMAC_MAC_MDIO_ADDRESS_PA_SHIFT) |
    150   1.1  jmcneill 	    (reg << GMAC_MAC_MDIO_ADDRESS_RDA_SHIFT) |
    151  1.23   msaitoh 	    GMAC_MAC_MDIO_ADDRESS_GOC_READ | GMAC_MAC_MDIO_ADDRESS_GB;
    152   1.1  jmcneill 	WR4(sc, GMAC_MAC_MDIO_ADDRESS, addr);
    153   1.1  jmcneill 
    154   1.1  jmcneill 	for (retry = MII_BUSY_RETRY; retry > 0; retry--) {
    155  1.46   mlelstv 		delay(10);
    156   1.1  jmcneill 		addr = RD4(sc, GMAC_MAC_MDIO_ADDRESS);
    157   1.1  jmcneill 		if ((addr & GMAC_MAC_MDIO_ADDRESS_GB) == 0) {
    158   1.1  jmcneill 			*val = RD4(sc, GMAC_MAC_MDIO_DATA) & 0xFFFF;
    159   1.1  jmcneill 			break;
    160   1.1  jmcneill 		}
    161   1.1  jmcneill 	}
    162   1.1  jmcneill 	if (retry == 0) {
    163   1.1  jmcneill 		device_printf(dev, "phy read timeout, phy=%d reg=%d\n",
    164   1.1  jmcneill 		    phy, reg);
    165   1.1  jmcneill 		return ETIMEDOUT;
    166   1.1  jmcneill 	}
    167   1.1  jmcneill 
    168   1.1  jmcneill 	return 0;
    169   1.1  jmcneill }
    170   1.1  jmcneill 
    171   1.1  jmcneill static int
    172   1.1  jmcneill eqos_mii_writereg(device_t dev, int phy, int reg, uint16_t val)
    173   1.1  jmcneill {
    174  1.15     skrll 	struct eqos_softc * const sc = device_private(dev);
    175   1.1  jmcneill 	uint32_t addr;
    176   1.1  jmcneill 	int retry;
    177   1.1  jmcneill 
    178   1.1  jmcneill 	WR4(sc, GMAC_MAC_MDIO_DATA, val);
    179   1.1  jmcneill 
    180   1.1  jmcneill 	addr = sc->sc_clock_range |
    181   1.1  jmcneill 	    (phy << GMAC_MAC_MDIO_ADDRESS_PA_SHIFT) |
    182   1.1  jmcneill 	    (reg << GMAC_MAC_MDIO_ADDRESS_RDA_SHIFT) |
    183  1.23   msaitoh 	    GMAC_MAC_MDIO_ADDRESS_GOC_WRITE | GMAC_MAC_MDIO_ADDRESS_GB;
    184   1.1  jmcneill 	WR4(sc, GMAC_MAC_MDIO_ADDRESS, addr);
    185   1.1  jmcneill 
    186   1.1  jmcneill 	for (retry = MII_BUSY_RETRY; retry > 0; retry--) {
    187  1.46   mlelstv 		delay(10);
    188   1.1  jmcneill 		addr = RD4(sc, GMAC_MAC_MDIO_ADDRESS);
    189   1.1  jmcneill 		if ((addr & GMAC_MAC_MDIO_ADDRESS_GB) == 0) {
    190   1.1  jmcneill 			break;
    191   1.1  jmcneill 		}
    192   1.1  jmcneill 	}
    193   1.1  jmcneill 	if (retry == 0) {
    194   1.1  jmcneill 		device_printf(dev, "phy write timeout, phy=%d reg=%d\n",
    195   1.1  jmcneill 		    phy, reg);
    196   1.1  jmcneill 		return ETIMEDOUT;
    197   1.1  jmcneill 	}
    198   1.1  jmcneill 
    199   1.1  jmcneill 	return 0;
    200   1.1  jmcneill }
    201   1.1  jmcneill 
    202   1.1  jmcneill static void
    203   1.1  jmcneill eqos_update_link(struct eqos_softc *sc)
    204   1.1  jmcneill {
    205  1.15     skrll 	struct mii_data * const mii = &sc->sc_mii;
    206   1.1  jmcneill 	uint64_t baudrate;
    207  1.28   msaitoh 	uint32_t conf, flow;
    208   1.1  jmcneill 
    209   1.1  jmcneill 	baudrate = ifmedia_baudrate(mii->mii_media_active);
    210   1.1  jmcneill 
    211   1.1  jmcneill 	conf = RD4(sc, GMAC_MAC_CONFIGURATION);
    212   1.1  jmcneill 	switch (baudrate) {
    213   1.1  jmcneill 	case IF_Mbps(10):
    214   1.1  jmcneill 		conf |= GMAC_MAC_CONFIGURATION_PS;
    215   1.1  jmcneill 		conf &= ~GMAC_MAC_CONFIGURATION_FES;
    216   1.1  jmcneill 		break;
    217   1.1  jmcneill 	case IF_Mbps(100):
    218   1.1  jmcneill 		conf |= GMAC_MAC_CONFIGURATION_PS;
    219   1.1  jmcneill 		conf |= GMAC_MAC_CONFIGURATION_FES;
    220   1.1  jmcneill 		break;
    221   1.1  jmcneill 	case IF_Gbps(1):
    222   1.1  jmcneill 		conf &= ~GMAC_MAC_CONFIGURATION_PS;
    223   1.1  jmcneill 		conf &= ~GMAC_MAC_CONFIGURATION_FES;
    224   1.1  jmcneill 		break;
    225   1.1  jmcneill 	case IF_Mbps(2500ULL):
    226   1.1  jmcneill 		conf &= ~GMAC_MAC_CONFIGURATION_PS;
    227   1.1  jmcneill 		conf |= GMAC_MAC_CONFIGURATION_FES;
    228   1.1  jmcneill 		break;
    229   1.1  jmcneill 	}
    230   1.1  jmcneill 
    231  1.28   msaitoh 	/* Set duplex. */
    232   1.1  jmcneill 	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
    233   1.1  jmcneill 		conf |= GMAC_MAC_CONFIGURATION_DM;
    234   1.1  jmcneill 	} else {
    235   1.1  jmcneill 		conf &= ~GMAC_MAC_CONFIGURATION_DM;
    236   1.1  jmcneill 	}
    237  1.28   msaitoh 	WR4(sc, GMAC_MAC_CONFIGURATION, conf);
    238   1.1  jmcneill 
    239  1.28   msaitoh 	/* Set TX flow control. */
    240  1.28   msaitoh 	if (mii->mii_media_active & IFM_ETH_TXPAUSE) {
    241  1.28   msaitoh 		flow = GMAC_MAC_Q0_TX_FLOW_CTRL_TFE;
    242  1.28   msaitoh 		flow |= 0xFFFFU << GMAC_MAC_Q0_TX_FLOW_CTRL_PT_SHIFT;
    243  1.28   msaitoh 	} else
    244  1.28   msaitoh 		flow = 0;
    245  1.28   msaitoh 	WR4(sc, GMAC_MAC_Q0_TX_FLOW_CTRL, flow);
    246  1.28   msaitoh 
    247  1.28   msaitoh 	/* Set RX flow control. */
    248  1.28   msaitoh 	if (mii->mii_media_active & IFM_ETH_RXPAUSE)
    249  1.28   msaitoh 		flow = GMAC_MAC_RX_FLOW_CTRL_RFE;
    250  1.28   msaitoh 	else
    251  1.28   msaitoh 		flow = 0;
    252  1.28   msaitoh 	WR4(sc, GMAC_MAC_RX_FLOW_CTRL, flow);
    253   1.1  jmcneill }
    254   1.1  jmcneill 
    255   1.1  jmcneill static void
    256   1.1  jmcneill eqos_mii_statchg(struct ifnet *ifp)
    257   1.1  jmcneill {
    258   1.1  jmcneill 	struct eqos_softc * const sc = ifp->if_softc;
    259   1.1  jmcneill 
    260   1.1  jmcneill 	eqos_update_link(sc);
    261   1.1  jmcneill }
    262   1.1  jmcneill 
    263   1.1  jmcneill static void
    264   1.1  jmcneill eqos_dma_sync(struct eqos_softc *sc, bus_dmamap_t map,
    265   1.1  jmcneill     u_int start, u_int end, u_int total, int flags)
    266   1.1  jmcneill {
    267   1.1  jmcneill 	if (end > start) {
    268   1.1  jmcneill 		bus_dmamap_sync(sc->sc_dmat, map, DESC_OFF(start),
    269   1.1  jmcneill 		    DESC_OFF(end) - DESC_OFF(start), flags);
    270   1.1  jmcneill 	} else {
    271   1.1  jmcneill 		bus_dmamap_sync(sc->sc_dmat, map, DESC_OFF(start),
    272   1.1  jmcneill 		    DESC_OFF(total) - DESC_OFF(start), flags);
    273   1.8    martin 		if (end > 0) {
    274   1.1  jmcneill 			bus_dmamap_sync(sc->sc_dmat, map, DESC_OFF(0),
    275   1.1  jmcneill 			    DESC_OFF(end) - DESC_OFF(0), flags);
    276   1.1  jmcneill 		}
    277   1.1  jmcneill 	}
    278   1.1  jmcneill }
    279   1.1  jmcneill 
    280  1.44  jmcneill static uint32_t
    281   1.1  jmcneill eqos_setup_txdesc(struct eqos_softc *sc, int index, int flags,
    282  1.55  jmcneill     bus_addr_t paddr, u_int len, u_int total_len, bool ioc)
    283   1.1  jmcneill {
    284  1.44  jmcneill 	struct eqos_dma_desc desc;
    285   1.1  jmcneill 	uint32_t tdes2, tdes3;
    286   1.1  jmcneill 
    287  1.24   msaitoh 	DPRINTF(EDEB_TXRING, "preparing desc %u\n", index);
    288  1.24   msaitoh 
    289  1.26   msaitoh 	EQOS_ASSERT_TXLOCKED(sc);
    290  1.26   msaitoh 
    291   1.1  jmcneill 	if (paddr == 0 || len == 0) {
    292   1.8    martin 		DPRINTF(EDEB_TXRING,
    293   1.8    martin 		    "tx for desc %u done!\n", index);
    294   1.1  jmcneill 		KASSERT(flags == 0);
    295   1.1  jmcneill 		tdes2 = 0;
    296   1.1  jmcneill 		tdes3 = 0;
    297   1.1  jmcneill 		--sc->sc_tx.queued;
    298   1.1  jmcneill 	} else {
    299  1.55  jmcneill 		tdes2 = (ioc && (flags & EQOS_TDES3_TX_LD) != 0) ?
    300  1.55  jmcneill 			EQOS_TDES2_TX_IOC : 0;
    301   1.1  jmcneill 		tdes3 = flags;
    302   1.1  jmcneill 		++sc->sc_tx.queued;
    303   1.1  jmcneill 	}
    304   1.1  jmcneill 
    305  1.21   msaitoh 	KASSERT(!EQOS_HW_FEATURE_ADDR64_32BIT(sc) ||
    306  1.21   msaitoh 	    ((uint64_t)paddr >> 32) == 0);
    307   1.1  jmcneill 
    308  1.44  jmcneill 	desc.tdes0 = htole32((uint32_t)paddr);
    309  1.44  jmcneill 	desc.tdes1 = htole32((uint32_t)((uint64_t)paddr >> 32));
    310  1.44  jmcneill 	desc.tdes2 = htole32(tdes2 | len);
    311  1.44  jmcneill 	desc.tdes3 = htole32(tdes3 | total_len);
    312  1.44  jmcneill 
    313  1.44  jmcneill 	sc->sc_tx.desc_ring[index] = desc;
    314  1.44  jmcneill 
    315  1.44  jmcneill 	return tdes3 | total_len;
    316   1.1  jmcneill }
    317   1.1  jmcneill 
    318   1.1  jmcneill static int
    319  1.45  jmcneill eqos_setup_txbuf(struct eqos_softc *sc, int index, struct mbuf *m,
    320  1.45  jmcneill     uint32_t *ptdes3)
    321   1.1  jmcneill {
    322   1.1  jmcneill 	bus_dma_segment_t *segs;
    323   1.1  jmcneill 	int error, nsegs, cur, i;
    324  1.44  jmcneill 	uint32_t flags, first_tdes3;
    325   1.1  jmcneill 	bool nospace;
    326   1.1  jmcneill 
    327  1.24   msaitoh 	DPRINTF(EDEB_TXRING, "preparing desc %u\n", index);
    328  1.24   msaitoh 
    329   1.1  jmcneill 	/* at least one descriptor free ? */
    330   1.1  jmcneill 	if (sc->sc_tx.queued >= TX_DESC_COUNT - 1)
    331   1.1  jmcneill 		return -1;
    332   1.1  jmcneill 
    333   1.1  jmcneill 	error = bus_dmamap_load_mbuf(sc->sc_dmat,
    334   1.1  jmcneill 	    sc->sc_tx.buf_map[index].map, m, BUS_DMA_WRITE | BUS_DMA_NOWAIT);
    335   1.1  jmcneill 	if (error == EFBIG) {
    336   1.1  jmcneill 		device_printf(sc->sc_dev,
    337   1.1  jmcneill 		    "TX packet needs too many DMA segments, dropping...\n");
    338   1.1  jmcneill 		return -2;
    339   1.1  jmcneill 	}
    340   1.1  jmcneill 	if (error != 0) {
    341   1.1  jmcneill 		device_printf(sc->sc_dev,
    342   1.1  jmcneill 		    "TX packet cannot be mapped, retried...\n");
    343   1.1  jmcneill 		return 0;
    344   1.1  jmcneill 	}
    345   1.1  jmcneill 
    346   1.1  jmcneill 	segs = sc->sc_tx.buf_map[index].map->dm_segs;
    347   1.1  jmcneill 	nsegs = sc->sc_tx.buf_map[index].map->dm_nsegs;
    348   1.1  jmcneill 
    349   1.1  jmcneill 	nospace = sc->sc_tx.queued >= TX_DESC_COUNT - nsegs;
    350   1.1  jmcneill 	if (nospace) {
    351   1.1  jmcneill 		bus_dmamap_unload(sc->sc_dmat,
    352   1.1  jmcneill 		    sc->sc_tx.buf_map[index].map);
    353   1.1  jmcneill 		/* XXX coalesce and retry ? */
    354   1.1  jmcneill 		return -1;
    355   1.1  jmcneill 	}
    356   1.1  jmcneill 
    357   1.2       mrg 	bus_dmamap_sync(sc->sc_dmat, sc->sc_tx.buf_map[index].map,
    358   1.5  riastrad 	    0, sc->sc_tx.buf_map[index].map->dm_mapsize, BUS_DMASYNC_PREWRITE);
    359   1.1  jmcneill 
    360   1.1  jmcneill 	/* stored in same index as loaded map */
    361   1.1  jmcneill 	sc->sc_tx.buf_map[index].mbuf = m;
    362   1.1  jmcneill 
    363  1.47  jmcneill 	flags = EQOS_TDES3_TX_FD;
    364  1.47  jmcneill 	if (ptdes3 == NULL) {
    365  1.47  jmcneill 		flags |= EQOS_TDES3_TX_OWN;
    366  1.47  jmcneill 	}
    367  1.47  jmcneill 	if ((m->m_pkthdr.csum_flags & (M_CSUM_TCPv4 | M_CSUM_UDPv4)) != 0) {
    368  1.47  jmcneill 		flags |= EQOS_TDES3_TX_CIC_FULL;
    369  1.47  jmcneill 	} else if ((m->m_pkthdr.csum_flags & M_CSUM_IPv4) != 0) {
    370  1.47  jmcneill 		flags |= EQOS_TDES3_TX_CIC_IPHDR;
    371  1.47  jmcneill 	}
    372  1.44  jmcneill 	first_tdes3 = 0;
    373   1.1  jmcneill 
    374  1.55  jmcneill 	const bool ioc = (sc->sc_tx.count++ % EQOS_TX_PACKET_PER_INTR) == 0;
    375   1.1  jmcneill 	for (cur = index, i = 0; i < nsegs; i++) {
    376  1.44  jmcneill 		uint32_t tdes3;
    377  1.44  jmcneill 
    378   1.1  jmcneill 		if (i == nsegs - 1)
    379  1.12       ryo 			flags |= EQOS_TDES3_TX_LD;
    380   1.1  jmcneill 
    381  1.44  jmcneill 		tdes3 = eqos_setup_txdesc(sc, cur, flags, segs[i].ds_addr,
    382  1.55  jmcneill 		    segs[i].ds_len, m->m_pkthdr.len, ioc);
    383   1.1  jmcneill 		cur = TX_NEXT(cur);
    384   1.1  jmcneill 
    385  1.44  jmcneill 		if (i == 0) {
    386  1.44  jmcneill 			first_tdes3 = tdes3;
    387  1.44  jmcneill 			flags &= ~EQOS_TDES3_TX_FD;
    388  1.44  jmcneill 		}
    389  1.12       ryo 		flags |= EQOS_TDES3_TX_OWN;
    390   1.1  jmcneill 	}
    391   1.1  jmcneill 
    392  1.45  jmcneill 	if (ptdes3 != NULL) {
    393  1.45  jmcneill 		*ptdes3 = first_tdes3;
    394  1.45  jmcneill 	}
    395  1.45  jmcneill 
    396   1.8    martin 	DPRINTF(EDEB_TXRING, "passing tx desc %u to hardware, cur: %u, "
    397  1.45  jmcneill 	    "next: %u, queued: %u, owned: %u\n",
    398  1.45  jmcneill 	    index, sc->sc_tx.cur, sc->sc_tx.next, sc->sc_tx.queued,
    399  1.45  jmcneill 	    ptdes3 == NULL);
    400   1.1  jmcneill 
    401   1.1  jmcneill 	return nsegs;
    402   1.1  jmcneill }
    403   1.1  jmcneill 
    404   1.1  jmcneill static void
    405   1.1  jmcneill eqos_setup_rxdesc(struct eqos_softc *sc, int index, bus_addr_t paddr)
    406   1.1  jmcneill {
    407  1.44  jmcneill 	struct eqos_dma_desc desc;
    408  1.45  jmcneill 	uint32_t tdes3 = EQOS_TDES3_RX_OWN | EQOS_TDES3_RX_BUF1V;
    409  1.45  jmcneill 
    410  1.45  jmcneill 	if ((index % eqos_rx_batch) == 0) {
    411  1.45  jmcneill 		tdes3 |= EQOS_TDES3_RX_IOC;
    412  1.45  jmcneill 	}
    413   1.4  riastrad 
    414  1.24   msaitoh 	DPRINTF(EDEB_RXRING, "preparing desc %u\n", index);
    415  1.24   msaitoh 
    416  1.44  jmcneill 	desc.tdes0 = htole32((uint32_t)paddr);
    417  1.44  jmcneill 	desc.tdes1 = htole32((uint32_t)((uint64_t)paddr >> 32));
    418  1.44  jmcneill 	desc.tdes2 = htole32(0);
    419  1.44  jmcneill 	desc.tdes3 = 0;
    420  1.44  jmcneill 
    421  1.44  jmcneill 	sc->sc_rx.desc_ring[index] = desc;
    422   1.4  riastrad 	bus_dmamap_sync(sc->sc_dmat, sc->sc_rx.desc_map,
    423   1.4  riastrad 	    DESC_OFF(index), offsetof(struct eqos_dma_desc, tdes3),
    424   1.4  riastrad 	    BUS_DMASYNC_PREWRITE);
    425  1.45  jmcneill 	sc->sc_rx.desc_ring[index].tdes3 = htole32(tdes3);
    426   1.1  jmcneill }
    427   1.1  jmcneill 
    428   1.1  jmcneill static int
    429   1.1  jmcneill eqos_setup_rxbuf(struct eqos_softc *sc, int index, struct mbuf *m)
    430   1.1  jmcneill {
    431   1.1  jmcneill 	int error;
    432   1.1  jmcneill 
    433  1.24   msaitoh 	DPRINTF(EDEB_RXRING, "preparing desc %u\n", index);
    434  1.24   msaitoh 
    435  1.13       ryo #if MCLBYTES >= (EQOS_RXDMA_SIZE + ETHER_ALIGN)
    436  1.13       ryo 	m_adj(m, ETHER_ALIGN);
    437  1.13       ryo #endif
    438  1.13       ryo 
    439   1.1  jmcneill 	error = bus_dmamap_load_mbuf(sc->sc_dmat,
    440   1.1  jmcneill 	    sc->sc_rx.buf_map[index].map, m, BUS_DMA_READ | BUS_DMA_NOWAIT);
    441   1.1  jmcneill 	if (error != 0)
    442   1.1  jmcneill 		return error;
    443   1.1  jmcneill 
    444   1.1  jmcneill 	bus_dmamap_sync(sc->sc_dmat, sc->sc_rx.buf_map[index].map,
    445   1.1  jmcneill 	    0, sc->sc_rx.buf_map[index].map->dm_mapsize,
    446   1.1  jmcneill 	    BUS_DMASYNC_PREREAD);
    447   1.1  jmcneill 
    448   1.1  jmcneill 	sc->sc_rx.buf_map[index].mbuf = m;
    449   1.1  jmcneill 
    450   1.1  jmcneill 	return 0;
    451   1.1  jmcneill }
    452   1.1  jmcneill 
    453  1.53  jmcneill static struct eqos_rxbuf *
    454  1.53  jmcneill eqos_alloc_rxbuf(struct eqos_softc *sc)
    455  1.53  jmcneill {
    456  1.53  jmcneill 	struct eqos_rxbuf *rxbuf;
    457  1.53  jmcneill 
    458  1.53  jmcneill 	mutex_enter(&sc->sc_rxdata.freelist_mtx);
    459  1.53  jmcneill 	rxbuf = SLIST_FIRST(&sc->sc_rxdata.freelist);
    460  1.53  jmcneill 	if (rxbuf != NULL) {
    461  1.53  jmcneill 		SLIST_REMOVE_HEAD(&sc->sc_rxdata.freelist, next);
    462  1.53  jmcneill 	}
    463  1.53  jmcneill 	mutex_exit(&sc->sc_rxdata.freelist_mtx);
    464  1.53  jmcneill 
    465  1.53  jmcneill 	return rxbuf;
    466  1.53  jmcneill }
    467  1.53  jmcneill 
    468  1.53  jmcneill static void
    469  1.53  jmcneill eqos_free_rxbuf(struct mbuf *m, void *buf, size_t size, void *arg)
    470  1.53  jmcneill {
    471  1.53  jmcneill 	struct eqos_rxbuf *rxbuf = arg;
    472  1.53  jmcneill 	struct eqos_softc *sc = rxbuf->sc;
    473  1.53  jmcneill 
    474  1.53  jmcneill 	mutex_enter(&sc->sc_rxdata.freelist_mtx);
    475  1.53  jmcneill 	SLIST_INSERT_HEAD(&sc->sc_rxdata.freelist, rxbuf, next);
    476  1.53  jmcneill 	mutex_exit(&sc->sc_rxdata.freelist_mtx);
    477  1.53  jmcneill 
    478  1.53  jmcneill 	if (__predict_true(m != NULL)) {
    479  1.53  jmcneill 		pool_cache_put(mb_cache, m);
    480  1.53  jmcneill 	}
    481  1.53  jmcneill }
    482  1.53  jmcneill 
    483   1.1  jmcneill static struct mbuf *
    484   1.1  jmcneill eqos_alloc_mbufcl(struct eqos_softc *sc)
    485   1.1  jmcneill {
    486  1.53  jmcneill 	struct eqos_rxbuf *rxbuf;
    487   1.1  jmcneill 	struct mbuf *m;
    488   1.1  jmcneill 
    489  1.53  jmcneill 	m = m_gethdr(M_NOWAIT, MT_DATA);
    490  1.53  jmcneill 	if (m == NULL) {
    491  1.53  jmcneill 		return NULL;
    492  1.53  jmcneill 	}
    493  1.53  jmcneill 	rxbuf = eqos_alloc_rxbuf(sc);
    494  1.53  jmcneill 	if (rxbuf == NULL) {
    495  1.53  jmcneill 		m_freem(m);
    496  1.53  jmcneill 		return NULL;
    497  1.53  jmcneill 	}
    498  1.53  jmcneill 	MEXTADD(m, rxbuf->vaddr, EQOS_RXDMA_SIZE, 0, eqos_free_rxbuf, rxbuf);
    499  1.53  jmcneill 	m->m_flags |= M_EXT_RW;
    500  1.53  jmcneill 	m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
    501   1.1  jmcneill 
    502   1.1  jmcneill 	return m;
    503   1.1  jmcneill }
    504   1.1  jmcneill 
    505   1.1  jmcneill static void
    506   1.1  jmcneill eqos_enable_intr(struct eqos_softc *sc)
    507   1.1  jmcneill {
    508  1.23   msaitoh 
    509   1.1  jmcneill 	WR4(sc, GMAC_DMA_CHAN0_INTR_ENABLE,
    510   1.1  jmcneill 	    GMAC_DMA_CHAN0_INTR_ENABLE_NIE |
    511   1.1  jmcneill 	    GMAC_DMA_CHAN0_INTR_ENABLE_AIE |
    512   1.1  jmcneill 	    GMAC_DMA_CHAN0_INTR_ENABLE_FBE |
    513  1.48  jmcneill 	    GMAC_DMA_CHAN0_INTR_ENABLE_RIE |
    514  1.48  jmcneill 	    GMAC_DMA_CHAN0_INTR_ENABLE_TIE);
    515   1.1  jmcneill }
    516   1.1  jmcneill 
    517   1.1  jmcneill static void
    518   1.1  jmcneill eqos_disable_intr(struct eqos_softc *sc)
    519   1.1  jmcneill {
    520  1.23   msaitoh 
    521   1.1  jmcneill 	WR4(sc, GMAC_DMA_CHAN0_INTR_ENABLE, 0);
    522   1.1  jmcneill }
    523   1.1  jmcneill 
    524   1.1  jmcneill static void
    525   1.1  jmcneill eqos_tick(void *softc)
    526   1.1  jmcneill {
    527  1.15     skrll 	struct eqos_softc * const sc = softc;
    528  1.15     skrll 	struct mii_data * const mii = &sc->sc_mii;
    529   1.1  jmcneill 
    530   1.1  jmcneill 	EQOS_LOCK(sc);
    531   1.1  jmcneill 	mii_tick(mii);
    532  1.40     skrll 	if ((sc->sc_if_flags & IFF_RUNNING) != 0)
    533  1.30  riastrad 		callout_schedule(&sc->sc_stat_ch, hz);
    534   1.1  jmcneill 	EQOS_UNLOCK(sc);
    535   1.1  jmcneill }
    536   1.1  jmcneill 
    537   1.1  jmcneill static uint32_t
    538   1.1  jmcneill eqos_bitrev32(uint32_t x)
    539   1.1  jmcneill {
    540  1.23   msaitoh 
    541   1.1  jmcneill 	x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
    542   1.1  jmcneill 	x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
    543   1.1  jmcneill 	x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
    544   1.1  jmcneill 	x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
    545   1.1  jmcneill 
    546   1.1  jmcneill 	return (x >> 16) | (x << 16);
    547   1.1  jmcneill }
    548   1.1  jmcneill 
    549   1.1  jmcneill static void
    550   1.1  jmcneill eqos_setup_rxfilter(struct eqos_softc *sc)
    551   1.1  jmcneill {
    552   1.1  jmcneill 	struct ethercom *ec = &sc->sc_ec;
    553  1.35     skrll 	struct ifnet * const ifp = &ec->ec_if;
    554   1.1  jmcneill 	uint32_t pfil, crc, hashreg, hashbit, hash[2];
    555   1.1  jmcneill 	struct ether_multi *enm;
    556   1.1  jmcneill 	struct ether_multistep step;
    557   1.1  jmcneill 	const uint8_t *eaddr;
    558   1.1  jmcneill 	uint32_t val;
    559   1.1  jmcneill 
    560   1.1  jmcneill 	EQOS_ASSERT_LOCKED(sc);
    561   1.1  jmcneill 
    562   1.1  jmcneill 	pfil = RD4(sc, GMAC_MAC_PACKET_FILTER);
    563  1.46   mlelstv 
    564  1.46   mlelstv 	/* turn off promiscous mode */
    565  1.46   mlelstv 	/* turn off the receive-all mode */
    566  1.46   mlelstv 	/* turn off the non-UDP/TCP filter */
    567  1.46   mlelstv 	/* turn off broadcast filter */
    568  1.46   mlelstv 	/* turn off pass all multicast mode */
    569  1.46   mlelstv 	/* turn on  hash or perfect mode */
    570  1.46   mlelstv 	/* turn off source address filter */
    571  1.46   mlelstv 	/* turn off inverse source address filter */
    572  1.46   mlelstv 	/* turn off inverse destination address filter */
    573  1.46   mlelstv 	/* turn off multicast hash table matching */
    574  1.46   mlelstv 	/* turn off unicast hash table matching */
    575  1.46   mlelstv 	/* XXX keep IP Layer3+4 filter */
    576  1.46   mlelstv 	/* XXX keep VLAN tag filter */
    577  1.46   mlelstv 	/* don't pass control packets */
    578   1.1  jmcneill 	pfil &= ~(GMAC_MAC_PACKET_FILTER_PR |
    579  1.46   mlelstv 		  GMAC_MAC_PACKET_FILTER_RA |
    580  1.46   mlelstv 		  GMAC_MAC_PACKET_FILTER_DNTU |
    581  1.46   mlelstv 		  GMAC_MAC_PACKET_FILTER_DBF |
    582   1.1  jmcneill 		  GMAC_MAC_PACKET_FILTER_PM |
    583  1.46   mlelstv 		  GMAC_MAC_PACKET_FILTER_SAF |
    584  1.46   mlelstv 		  GMAC_MAC_PACKET_FILTER_SAIF |
    585  1.46   mlelstv 		  GMAC_MAC_PACKET_FILTER_DAIF |
    586   1.1  jmcneill 		  GMAC_MAC_PACKET_FILTER_HMC |
    587  1.46   mlelstv 		  GMAC_MAC_PACKET_FILTER_HUC |
    588  1.46   mlelstv 	          GMAC_MAC_PACKET_FILTER_PCF_MASK);
    589  1.46   mlelstv 	pfil |= GMAC_MAC_PACKET_FILTER_HPF;
    590   1.1  jmcneill 
    591  1.33  riastrad 	ETHER_LOCK(ec);
    592  1.40     skrll 	if ((sc->sc_if_flags & IFF_PROMISC) != 0)  {
    593  1.33  riastrad 		ec->ec_flags |= ETHER_F_ALLMULTI;
    594  1.46   mlelstv 		/* enable promiscous mode */
    595  1.46   mlelstv 		pfil |= GMAC_MAC_PACKET_FILTER_PR;
    596  1.46   mlelstv 		/* also receive control packets */
    597  1.46   mlelstv 		pfil |= GMAC_MAC_PACKET_FILTER_PCF_ALL;
    598  1.46   mlelstv 		/* ignored */
    599  1.46   mlelstv 		hash[0] = hash[1] = ~0U;
    600   1.1  jmcneill 	} else {
    601  1.46   mlelstv 		hash[0] = hash[1] = 0U;
    602  1.46   mlelstv 		ec->ec_flags &= ~ETHER_F_ALLMULTI;
    603  1.46   mlelstv 		/* enable multicast hash table matching */
    604  1.33  riastrad 		pfil |= GMAC_MAC_PACKET_FILTER_HMC;
    605   1.1  jmcneill 		ETHER_FIRST_MULTI(step, ec, enm);
    606   1.1  jmcneill 		while (enm != NULL) {
    607  1.33  riastrad 			if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
    608  1.33  riastrad 				ETHER_ADDR_LEN) != 0) {
    609  1.46   mlelstv 				/* disable multicast hash table matching */
    610  1.33  riastrad 				pfil &= ~GMAC_MAC_PACKET_FILTER_HMC;
    611  1.46   mlelstv 				/* pass all multicast instead */
    612  1.33  riastrad 				pfil |= GMAC_MAC_PACKET_FILTER_PM;
    613  1.46   mlelstv 				/* ignored */
    614  1.46   mlelstv 				hash[0] = hash[1] = ~0U;
    615  1.33  riastrad 				break;
    616  1.33  riastrad 			}
    617   1.1  jmcneill 			crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
    618   1.1  jmcneill 			crc &= 0x7f;
    619   1.1  jmcneill 			crc = eqos_bitrev32(~crc) >> 26;
    620   1.1  jmcneill 			hashreg = (crc >> 5);
    621   1.1  jmcneill 			hashbit = (crc & 0x1f);
    622   1.1  jmcneill 			hash[hashreg] |= (1 << hashbit);
    623   1.1  jmcneill 			ETHER_NEXT_MULTI(step, enm);
    624   1.1  jmcneill 		}
    625   1.1  jmcneill 	}
    626  1.33  riastrad 	ETHER_UNLOCK(ec);
    627   1.1  jmcneill 
    628   1.1  jmcneill 	/* Write our unicast address */
    629   1.1  jmcneill 	eaddr = CLLADDR(ifp->if_sadl);
    630  1.46   mlelstv 	val = eaddr[4] | (eaddr[5] << 8);
    631  1.46   mlelstv 	val = __SHIFTIN(val, GMAC_MAC_ADDRESS_HIGH_ADDR)
    632  1.46   mlelstv 	    | GMAC_MAC_ADDRESS_HIGH_AE;
    633   1.1  jmcneill 	WR4(sc, GMAC_MAC_ADDRESS0_HIGH, val);
    634   1.1  jmcneill 	val = eaddr[0] | (eaddr[1] << 8) | (eaddr[2] << 16) |
    635   1.1  jmcneill 	    (eaddr[3] << 24);
    636   1.1  jmcneill 	WR4(sc, GMAC_MAC_ADDRESS0_LOW, val);
    637   1.1  jmcneill 
    638   1.1  jmcneill 	/* Multicast hash filters */
    639  1.46   mlelstv 	/* XXX assumes 64 bit hashtable size */
    640   1.9    martin 	WR4(sc, GMAC_MAC_HASH_TABLE_REG0, hash[0]);
    641   1.9    martin 	WR4(sc, GMAC_MAC_HASH_TABLE_REG1, hash[1]);
    642   1.1  jmcneill 
    643   1.8    martin 	DPRINTF(EDEB_NOTE, "writing new packet filter config "
    644   1.8    martin 	    "%08x, hash[1]=%08x, hash[0]=%08x\n", pfil, hash[1], hash[0]);
    645   1.1  jmcneill 	/* Packet filter config */
    646   1.1  jmcneill 	WR4(sc, GMAC_MAC_PACKET_FILTER, pfil);
    647   1.1  jmcneill }
    648   1.1  jmcneill 
    649  1.52  jmcneill static void
    650  1.52  jmcneill eqos_setup_coe(struct eqos_softc *sc)
    651  1.52  jmcneill {
    652  1.52  jmcneill 	struct ifnet * const ifp = &sc->sc_ec.ec_if;
    653  1.52  jmcneill 	const uint64_t if_capenable = ifp->if_capenable;
    654  1.52  jmcneill 	uint32_t val;
    655  1.52  jmcneill 
    656  1.52  jmcneill 	EQOS_ASSERT_LOCKED(sc);
    657  1.52  jmcneill 
    658  1.52  jmcneill 	val = RD4(sc, GMAC_MAC_CONFIGURATION);
    659  1.52  jmcneill 	if ((if_capenable & (IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx |
    660  1.52  jmcneill 			     IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
    661  1.52  jmcneill 			     IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx)) != 0) {
    662  1.52  jmcneill 		val |= GMAC_MAC_CONFIGURATION_IPC;
    663  1.52  jmcneill 	} else {
    664  1.52  jmcneill 		val &= ~GMAC_MAC_CONFIGURATION_IPC;
    665  1.52  jmcneill 	}
    666  1.52  jmcneill 	WR4(sc, GMAC_MAC_CONFIGURATION, val);
    667  1.52  jmcneill }
    668  1.52  jmcneill 
    669   1.1  jmcneill static int
    670   1.1  jmcneill eqos_reset(struct eqos_softc *sc)
    671   1.1  jmcneill {
    672   1.1  jmcneill 	uint32_t val;
    673   1.1  jmcneill 	int retry;
    674   1.1  jmcneill 
    675   1.1  jmcneill 	WR4(sc, GMAC_DMA_MODE, GMAC_DMA_MODE_SWR);
    676   1.1  jmcneill 	for (retry = 2000; retry > 0; retry--) {
    677   1.1  jmcneill 		delay(1000);
    678   1.1  jmcneill 		val = RD4(sc, GMAC_DMA_MODE);
    679   1.1  jmcneill 		if ((val & GMAC_DMA_MODE_SWR) == 0) {
    680   1.1  jmcneill 			return 0;
    681   1.1  jmcneill 		}
    682   1.1  jmcneill 	}
    683   1.1  jmcneill 
    684   1.1  jmcneill 	device_printf(sc->sc_dev, "reset timeout!\n");
    685   1.1  jmcneill 	return ETIMEDOUT;
    686   1.1  jmcneill }
    687   1.1  jmcneill 
    688   1.1  jmcneill static void
    689   1.1  jmcneill eqos_init_rings(struct eqos_softc *sc, int qid)
    690   1.1  jmcneill {
    691   1.7    martin 	sc->sc_tx.cur = sc->sc_tx.next = sc->sc_tx.queued = 0;
    692   1.1  jmcneill 
    693  1.13       ryo 	sc->sc_rx_discarding = false;
    694  1.37       rin 	m_freem(sc->sc_rx_receiving_m);
    695  1.13       ryo 	sc->sc_rx_receiving_m = NULL;
    696  1.13       ryo 	sc->sc_rx_receiving_m_last = NULL;
    697  1.13       ryo 
    698   1.1  jmcneill 	WR4(sc, GMAC_DMA_CHAN0_TX_BASE_ADDR_HI,
    699  1.21   msaitoh 	    (uint32_t)((uint64_t)sc->sc_tx.desc_ring_paddr >> 32));
    700   1.1  jmcneill 	WR4(sc, GMAC_DMA_CHAN0_TX_BASE_ADDR,
    701   1.1  jmcneill 	    (uint32_t)sc->sc_tx.desc_ring_paddr);
    702   1.1  jmcneill 	WR4(sc, GMAC_DMA_CHAN0_TX_RING_LEN, TX_DESC_COUNT - 1);
    703  1.17    andvar 	DPRINTF(EDEB_TXRING, "tx ring paddr %lx with %u descriptors\n",
    704   1.8    martin 	    sc->sc_tx.desc_ring_paddr, TX_DESC_COUNT);
    705   1.1  jmcneill 
    706   1.8    martin 	sc->sc_rx.cur = sc->sc_rx.next = sc->sc_rx.queued = 0;
    707   1.1  jmcneill 	WR4(sc, GMAC_DMA_CHAN0_RX_BASE_ADDR_HI,
    708  1.21   msaitoh 	    (uint32_t)((uint64_t)sc->sc_rx.desc_ring_paddr >> 32));
    709   1.1  jmcneill 	WR4(sc, GMAC_DMA_CHAN0_RX_BASE_ADDR,
    710   1.1  jmcneill 	    (uint32_t)sc->sc_rx.desc_ring_paddr);
    711   1.1  jmcneill 	WR4(sc, GMAC_DMA_CHAN0_RX_RING_LEN, RX_DESC_COUNT - 1);
    712   1.1  jmcneill 	WR4(sc, GMAC_DMA_CHAN0_RX_END_ADDR,
    713   1.1  jmcneill 	    (uint32_t)sc->sc_rx.desc_ring_paddr +
    714   1.1  jmcneill 	    DESC_OFF((sc->sc_rx.cur - 1) % RX_DESC_COUNT));
    715  1.17    andvar 	DPRINTF(EDEB_RXRING, "rx ring paddr %lx with %u descriptors\n",
    716   1.8    martin 	    sc->sc_rx.desc_ring_paddr, RX_DESC_COUNT);
    717   1.1  jmcneill }
    718   1.1  jmcneill 
    719   1.1  jmcneill static int
    720   1.1  jmcneill eqos_init_locked(struct eqos_softc *sc)
    721   1.1  jmcneill {
    722  1.15     skrll 	struct ifnet * const ifp = &sc->sc_ec.ec_if;
    723  1.15     skrll 	struct mii_data * const mii = &sc->sc_mii;
    724  1.10       ryo 	uint32_t val, tqs, rqs;
    725   1.1  jmcneill 
    726   1.1  jmcneill 	EQOS_ASSERT_LOCKED(sc);
    727   1.1  jmcneill 	EQOS_ASSERT_TXLOCKED(sc);
    728   1.1  jmcneill 
    729  1.46   mlelstv 	if ((ifp->if_flags & IFF_RUNNING) != 0) {
    730  1.52  jmcneill 		/* Only Setup RX filter and checksum offload */
    731  1.46   mlelstv 		eqos_setup_rxfilter(sc);
    732  1.52  jmcneill 		eqos_setup_coe(sc);
    733   1.1  jmcneill 		return 0;
    734  1.46   mlelstv 	}
    735   1.1  jmcneill 
    736   1.1  jmcneill 	/* Setup TX/RX rings */
    737   1.1  jmcneill 	eqos_init_rings(sc, 0);
    738   1.1  jmcneill 
    739   1.1  jmcneill 	/* Setup RX filter */
    740  1.40     skrll 	sc->sc_if_flags = ifp->if_flags;
    741   1.1  jmcneill 	eqos_setup_rxfilter(sc);
    742   1.1  jmcneill 
    743  1.52  jmcneill 	/* Setup checksum offload */
    744  1.52  jmcneill 	eqos_setup_coe(sc);
    745  1.52  jmcneill 
    746   1.1  jmcneill 	WR4(sc, GMAC_MAC_1US_TIC_COUNTER, (sc->sc_csr_clock / 1000000) - 1);
    747   1.1  jmcneill 
    748  1.45  jmcneill 	/* RX interrupt watchdog timer */
    749  1.45  jmcneill 	WR4(sc, GMAC_DMA_CHAN0_RX_WATCHDOG,
    750  1.45  jmcneill 	    GMAC_DMA_CHAN0_RX_WATCHDOG_RWTU_256 | eqos_rx_wdt_count);
    751  1.45  jmcneill 
    752   1.1  jmcneill 	/* Enable transmit and receive DMA */
    753   1.1  jmcneill 	val = RD4(sc, GMAC_DMA_CHAN0_CONTROL);
    754   1.1  jmcneill 	val &= ~GMAC_DMA_CHAN0_CONTROL_DSL_MASK;
    755   1.1  jmcneill 	val |= ((DESC_ALIGN - 16) / 8) << GMAC_DMA_CHAN0_CONTROL_DSL_SHIFT;
    756   1.1  jmcneill 	val |= GMAC_DMA_CHAN0_CONTROL_PBLX8;
    757   1.1  jmcneill 	WR4(sc, GMAC_DMA_CHAN0_CONTROL, val);
    758   1.1  jmcneill 	val = RD4(sc, GMAC_DMA_CHAN0_TX_CONTROL);
    759  1.27   msaitoh 	val &= ~GMAC_DMA_CHAN0_TX_CONTROL_TXPBL_MASK;
    760  1.27   msaitoh 	val |= (sc->sc_dma_txpbl << GMAC_DMA_CHAN0_TX_CONTROL_TXPBL_SHIFT);
    761   1.1  jmcneill 	val |= GMAC_DMA_CHAN0_TX_CONTROL_OSP;
    762   1.1  jmcneill 	val |= GMAC_DMA_CHAN0_TX_CONTROL_START;
    763   1.1  jmcneill 	WR4(sc, GMAC_DMA_CHAN0_TX_CONTROL, val);
    764   1.1  jmcneill 	val = RD4(sc, GMAC_DMA_CHAN0_RX_CONTROL);
    765  1.27   msaitoh 	val &= ~(GMAC_DMA_CHAN0_RX_CONTROL_RBSZ_MASK |
    766  1.27   msaitoh 	    GMAC_DMA_CHAN0_RX_CONTROL_RXPBL_MASK);
    767   1.1  jmcneill 	val |= (MCLBYTES << GMAC_DMA_CHAN0_RX_CONTROL_RBSZ_SHIFT);
    768  1.27   msaitoh 	val |= (sc->sc_dma_rxpbl << GMAC_DMA_CHAN0_RX_CONTROL_RXPBL_SHIFT);
    769   1.1  jmcneill 	val |= GMAC_DMA_CHAN0_RX_CONTROL_START;
    770   1.1  jmcneill 	WR4(sc, GMAC_DMA_CHAN0_RX_CONTROL, val);
    771   1.1  jmcneill 
    772   1.6  jmcneill 	/* Disable counters */
    773   1.6  jmcneill 	WR4(sc, GMAC_MMC_CONTROL,
    774   1.6  jmcneill 	    GMAC_MMC_CONTROL_CNTFREEZ |
    775   1.6  jmcneill 	    GMAC_MMC_CONTROL_CNTPRST |
    776   1.6  jmcneill 	    GMAC_MMC_CONTROL_CNTPRSTLVL);
    777   1.6  jmcneill 
    778   1.1  jmcneill 	/* Configure operation modes */
    779   1.1  jmcneill 	WR4(sc, GMAC_MTL_TXQ0_OPERATION_MODE,
    780   1.1  jmcneill 	    GMAC_MTL_TXQ0_OPERATION_MODE_TSF |
    781   1.1  jmcneill 	    GMAC_MTL_TXQ0_OPERATION_MODE_TXQEN_EN);
    782   1.1  jmcneill 	WR4(sc, GMAC_MTL_RXQ0_OPERATION_MODE,
    783   1.1  jmcneill 	    GMAC_MTL_RXQ0_OPERATION_MODE_RSF |
    784   1.1  jmcneill 	    GMAC_MTL_RXQ0_OPERATION_MODE_FEP |
    785   1.1  jmcneill 	    GMAC_MTL_RXQ0_OPERATION_MODE_FUP);
    786   1.1  jmcneill 
    787  1.10       ryo 	/*
    788  1.10       ryo 	 * TX/RX fifo size in hw_feature[1] are log2(n/128), and
    789  1.10       ryo 	 * TQS/RQS in TXQ0/RXQ0_OPERATION_MODE are n/256-1.
    790  1.10       ryo 	 */
    791  1.10       ryo 	tqs = (128 << __SHIFTOUT(sc->sc_hw_feature[1],
    792  1.10       ryo 	    GMAC_MAC_HW_FEATURE1_TXFIFOSIZE) / 256) - 1;
    793  1.10       ryo 	val = RD4(sc, GMAC_MTL_TXQ0_OPERATION_MODE);
    794  1.10       ryo 	val &= ~GMAC_MTL_TXQ0_OPERATION_MODE_TQS;
    795  1.10       ryo 	val |= __SHIFTIN(tqs, GMAC_MTL_TXQ0_OPERATION_MODE_TQS);
    796  1.10       ryo 	WR4(sc, GMAC_MTL_TXQ0_OPERATION_MODE, val);
    797  1.10       ryo 
    798  1.10       ryo 	rqs = (128 << __SHIFTOUT(sc->sc_hw_feature[1],
    799  1.10       ryo 	    GMAC_MAC_HW_FEATURE1_RXFIFOSIZE) / 256) - 1;
    800  1.10       ryo 	val = RD4(sc, GMAC_MTL_RXQ0_OPERATION_MODE);
    801  1.10       ryo 	val &= ~GMAC_MTL_RXQ0_OPERATION_MODE_RQS;
    802  1.10       ryo 	val |= __SHIFTIN(rqs, GMAC_MTL_RXQ0_OPERATION_MODE_RQS);
    803  1.10       ryo 	WR4(sc, GMAC_MTL_RXQ0_OPERATION_MODE, val);
    804  1.10       ryo 
    805  1.28   msaitoh 	/*
    806  1.28   msaitoh 	 * Disable flow control.
    807  1.28   msaitoh 	 * It'll be configured later from the negotiated result.
    808  1.28   msaitoh 	 */
    809  1.28   msaitoh 	WR4(sc, GMAC_MAC_Q0_TX_FLOW_CTRL, 0);
    810  1.28   msaitoh 	WR4(sc, GMAC_MAC_RX_FLOW_CTRL, 0);
    811   1.1  jmcneill 
    812  1.10       ryo 	/* set RX queue mode. must be in DCB mode. */
    813  1.10       ryo 	val = __SHIFTIN(GMAC_RXQ_CTRL0_EN_DCB, GMAC_RXQ_CTRL0_EN_MASK);
    814  1.10       ryo 	WR4(sc, GMAC_RXQ_CTRL0, val);
    815  1.10       ryo 
    816   1.1  jmcneill 	/* Enable transmitter and receiver */
    817   1.1  jmcneill 	val = RD4(sc, GMAC_MAC_CONFIGURATION);
    818   1.1  jmcneill 	val |= GMAC_MAC_CONFIGURATION_BE;
    819   1.1  jmcneill 	val |= GMAC_MAC_CONFIGURATION_JD;
    820   1.1  jmcneill 	val |= GMAC_MAC_CONFIGURATION_JE;
    821   1.1  jmcneill 	val |= GMAC_MAC_CONFIGURATION_DCRS;
    822   1.1  jmcneill 	val |= GMAC_MAC_CONFIGURATION_TE;
    823   1.1  jmcneill 	val |= GMAC_MAC_CONFIGURATION_RE;
    824   1.1  jmcneill 	WR4(sc, GMAC_MAC_CONFIGURATION, val);
    825   1.1  jmcneill 
    826   1.1  jmcneill 	/* Enable interrupts */
    827   1.1  jmcneill 	eqos_enable_intr(sc);
    828   1.1  jmcneill 
    829  1.31  riastrad 	EQOS_ASSERT_TXLOCKED(sc);
    830  1.31  riastrad 	sc->sc_txrunning = true;
    831  1.31  riastrad 
    832   1.1  jmcneill 	ifp->if_flags |= IFF_RUNNING;
    833  1.40     skrll 	sc->sc_if_flags |= IFF_RUNNING;
    834   1.1  jmcneill 
    835   1.1  jmcneill 	mii_mediachg(mii);
    836   1.1  jmcneill 	callout_schedule(&sc->sc_stat_ch, hz);
    837   1.1  jmcneill 
    838   1.1  jmcneill 	return 0;
    839   1.1  jmcneill }
    840   1.1  jmcneill 
    841   1.1  jmcneill static int
    842   1.1  jmcneill eqos_init(struct ifnet *ifp)
    843   1.1  jmcneill {
    844  1.15     skrll 	struct eqos_softc * const sc = ifp->if_softc;
    845   1.1  jmcneill 	int error;
    846   1.1  jmcneill 
    847   1.1  jmcneill 	EQOS_LOCK(sc);
    848   1.1  jmcneill 	EQOS_TXLOCK(sc);
    849   1.1  jmcneill 	error = eqos_init_locked(sc);
    850   1.1  jmcneill 	EQOS_TXUNLOCK(sc);
    851   1.1  jmcneill 	EQOS_UNLOCK(sc);
    852   1.1  jmcneill 
    853   1.1  jmcneill 	return error;
    854   1.1  jmcneill }
    855   1.1  jmcneill 
    856   1.1  jmcneill static void
    857   1.1  jmcneill eqos_stop_locked(struct eqos_softc *sc, int disable)
    858   1.1  jmcneill {
    859  1.15     skrll 	struct ifnet * const ifp = &sc->sc_ec.ec_if;
    860   1.1  jmcneill 	uint32_t val;
    861   1.1  jmcneill 	int retry;
    862   1.1  jmcneill 
    863   1.1  jmcneill 	EQOS_ASSERT_LOCKED(sc);
    864   1.1  jmcneill 
    865  1.31  riastrad 	EQOS_TXLOCK(sc);
    866  1.31  riastrad 	sc->sc_txrunning = false;
    867  1.31  riastrad 	EQOS_TXUNLOCK(sc);
    868  1.31  riastrad 
    869  1.30  riastrad 	callout_halt(&sc->sc_stat_ch, &sc->sc_lock);
    870   1.1  jmcneill 
    871   1.1  jmcneill 	mii_down(&sc->sc_mii);
    872   1.1  jmcneill 
    873   1.1  jmcneill 	/* Disable receiver */
    874   1.1  jmcneill 	val = RD4(sc, GMAC_MAC_CONFIGURATION);
    875   1.1  jmcneill 	val &= ~GMAC_MAC_CONFIGURATION_RE;
    876   1.1  jmcneill 	WR4(sc, GMAC_MAC_CONFIGURATION, val);
    877   1.1  jmcneill 
    878   1.1  jmcneill 	/* Stop receive DMA */
    879   1.1  jmcneill 	val = RD4(sc, GMAC_DMA_CHAN0_RX_CONTROL);
    880   1.1  jmcneill 	val &= ~GMAC_DMA_CHAN0_RX_CONTROL_START;
    881   1.1  jmcneill 	WR4(sc, GMAC_DMA_CHAN0_RX_CONTROL, val);
    882   1.1  jmcneill 
    883   1.1  jmcneill 	/* Stop transmit DMA */
    884   1.1  jmcneill 	val = RD4(sc, GMAC_DMA_CHAN0_TX_CONTROL);
    885   1.1  jmcneill 	val &= ~GMAC_DMA_CHAN0_TX_CONTROL_START;
    886   1.1  jmcneill 	WR4(sc, GMAC_DMA_CHAN0_TX_CONTROL, val);
    887   1.1  jmcneill 
    888   1.1  jmcneill 	if (disable) {
    889   1.1  jmcneill 		/* Flush data in the TX FIFO */
    890   1.1  jmcneill 		val = RD4(sc, GMAC_MTL_TXQ0_OPERATION_MODE);
    891   1.1  jmcneill 		val |= GMAC_MTL_TXQ0_OPERATION_MODE_FTQ;
    892   1.1  jmcneill 		WR4(sc, GMAC_MTL_TXQ0_OPERATION_MODE, val);
    893   1.1  jmcneill 		/* Wait for flush to complete */
    894   1.1  jmcneill 		for (retry = 10000; retry > 0; retry--) {
    895   1.1  jmcneill 			val = RD4(sc, GMAC_MTL_TXQ0_OPERATION_MODE);
    896   1.1  jmcneill 			if ((val & GMAC_MTL_TXQ0_OPERATION_MODE_FTQ) == 0) {
    897   1.1  jmcneill 				break;
    898   1.1  jmcneill 			}
    899   1.1  jmcneill 			delay(1);
    900   1.1  jmcneill 		}
    901   1.1  jmcneill 		if (retry == 0) {
    902   1.1  jmcneill 			device_printf(sc->sc_dev,
    903   1.1  jmcneill 			    "timeout flushing TX queue\n");
    904   1.1  jmcneill 		}
    905   1.1  jmcneill 	}
    906   1.1  jmcneill 
    907   1.1  jmcneill 	/* Disable transmitter */
    908   1.1  jmcneill 	val = RD4(sc, GMAC_MAC_CONFIGURATION);
    909   1.1  jmcneill 	val &= ~GMAC_MAC_CONFIGURATION_TE;
    910   1.1  jmcneill 	WR4(sc, GMAC_MAC_CONFIGURATION, val);
    911   1.1  jmcneill 
    912   1.1  jmcneill 	/* Disable interrupts */
    913   1.1  jmcneill 	eqos_disable_intr(sc);
    914   1.1  jmcneill 
    915  1.44  jmcneill 	workqueue_wait(sc->sc_wq, &sc->sc_rxwork);
    916  1.44  jmcneill 
    917  1.40     skrll 	sc->sc_if_flags &= ~IFF_RUNNING;
    918  1.16   thorpej 	ifp->if_flags &= ~IFF_RUNNING;
    919   1.1  jmcneill }
    920   1.1  jmcneill 
    921   1.1  jmcneill static void
    922   1.1  jmcneill eqos_stop(struct ifnet *ifp, int disable)
    923   1.1  jmcneill {
    924   1.1  jmcneill 	struct eqos_softc * const sc = ifp->if_softc;
    925   1.1  jmcneill 
    926   1.1  jmcneill 	EQOS_LOCK(sc);
    927   1.1  jmcneill 	eqos_stop_locked(sc, disable);
    928   1.1  jmcneill 	EQOS_UNLOCK(sc);
    929   1.1  jmcneill }
    930   1.1  jmcneill 
    931   1.1  jmcneill static void
    932   1.1  jmcneill eqos_rxintr(struct eqos_softc *sc, int qid)
    933   1.1  jmcneill {
    934  1.15     skrll 	struct ifnet * const ifp = &sc->sc_ec.ec_if;
    935  1.47  jmcneill 	const uint64_t if_capenable = ifp->if_capenable;
    936  1.13       ryo 	int error, index, pkts = 0;
    937  1.13       ryo 	struct mbuf *m, *m0, *new_m, *mprev;
    938   1.1  jmcneill 	uint32_t tdes3;
    939  1.13       ryo 	bool discarding;
    940  1.13       ryo 
    941  1.13       ryo 	/* restore jumboframe context */
    942  1.13       ryo 	discarding = sc->sc_rx_discarding;
    943  1.13       ryo 	m0 = sc->sc_rx_receiving_m;
    944  1.13       ryo 	mprev = sc->sc_rx_receiving_m_last;
    945   1.1  jmcneill 
    946  1.46   mlelstv 	DPRINTF(EDEB_INTR, "qid: %u, discarding %u, m0 %p\n",
    947  1.46   mlelstv 	    qid, discarding, m0);
    948  1.46   mlelstv 
    949   1.1  jmcneill 	for (index = sc->sc_rx.cur; ; index = RX_NEXT(index)) {
    950   1.1  jmcneill 		eqos_dma_sync(sc, sc->sc_rx.desc_map,
    951   1.1  jmcneill 		    index, index + 1, RX_DESC_COUNT,
    952   1.1  jmcneill 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
    953   1.1  jmcneill 
    954   1.1  jmcneill 		tdes3 = le32toh(sc->sc_rx.desc_ring[index].tdes3);
    955  1.12       ryo 		if ((tdes3 & EQOS_TDES3_RX_OWN) != 0) {
    956   1.1  jmcneill 			break;
    957   1.1  jmcneill 		}
    958   1.1  jmcneill 
    959  1.56  gutterid 		/* now discarding until the last packet */
    960  1.13       ryo 		if (discarding)
    961  1.13       ryo 			goto rx_next;
    962  1.13       ryo 
    963  1.13       ryo 		if ((tdes3 & EQOS_TDES3_RX_CTXT) != 0)
    964  1.13       ryo 			goto rx_next;	/* ignore receive context descriptor */
    965  1.13       ryo 
    966  1.13       ryo 		/* error packet? */
    967  1.13       ryo 		if ((tdes3 & (EQOS_TDES3_RX_CE | EQOS_TDES3_RX_RWT |
    968  1.13       ryo 		    EQOS_TDES3_RX_OE | EQOS_TDES3_RX_RE |
    969  1.13       ryo 		    EQOS_TDES3_RX_DE)) != 0) {
    970  1.13       ryo #ifdef EQOS_DEBUG
    971  1.13       ryo 			char buf[128];
    972  1.13       ryo 			snprintb(buf, sizeof(buf),
    973  1.13       ryo 			    "\177\020"
    974  1.13       ryo 			    "b\x1e" "CTXT\0"	/* 30 */
    975  1.13       ryo 			    "b\x18" "CE\0"	/* 24 */
    976  1.13       ryo 			    "b\x17" "GP\0"	/* 23 */
    977  1.13       ryo 			    "b\x16" "WDT\0"	/* 22 */
    978  1.13       ryo 			    "b\x15" "OE\0"	/* 21 */
    979  1.13       ryo 			    "b\x14" "RE\0"	/* 20 */
    980  1.13       ryo 			    "b\x13" "DE\0"	/* 19 */
    981  1.13       ryo 			    "b\x0f" "ES\0"	/* 15 */
    982  1.13       ryo 			    "\0", tdes3);
    983  1.23   msaitoh 			DPRINTF(EDEB_NOTE,
    984  1.23   msaitoh 			    "rxdesc[%d].tdes3=%s\n", index, buf);
    985  1.13       ryo #endif
    986  1.13       ryo 			if_statinc(ifp, if_ierrors);
    987  1.13       ryo 			if (m0 != NULL) {
    988  1.13       ryo 				m_freem(m0);
    989  1.13       ryo 				m0 = mprev = NULL;
    990  1.13       ryo 			}
    991  1.13       ryo 			discarding = true;
    992  1.13       ryo 			goto rx_next;
    993  1.13       ryo 		}
    994  1.13       ryo 
    995   1.1  jmcneill 		bus_dmamap_sync(sc->sc_dmat, sc->sc_rx.buf_map[index].map,
    996   1.1  jmcneill 		    0, sc->sc_rx.buf_map[index].map->dm_mapsize,
    997   1.1  jmcneill 		    BUS_DMASYNC_POSTREAD);
    998  1.13       ryo 		m = sc->sc_rx.buf_map[index].mbuf;
    999  1.13       ryo 		new_m = eqos_alloc_mbufcl(sc);
   1000  1.13       ryo 		if (new_m == NULL) {
   1001  1.13       ryo 			/*
   1002  1.13       ryo 			 * cannot allocate new mbuf. discard this received
   1003  1.13       ryo 			 * packet, and reuse the mbuf for next.
   1004  1.13       ryo 			 */
   1005  1.13       ryo 			if_statinc(ifp, if_ierrors);
   1006  1.13       ryo 			if (m0 != NULL) {
   1007  1.13       ryo 				/* also discard the halfway jumbo packet */
   1008  1.13       ryo 				m_freem(m0);
   1009  1.13       ryo 				m0 = mprev = NULL;
   1010  1.13       ryo 			}
   1011  1.13       ryo 			discarding = true;
   1012  1.13       ryo 			goto rx_next;
   1013  1.13       ryo 		}
   1014  1.14       ryo 		bus_dmamap_unload(sc->sc_dmat,
   1015  1.14       ryo 		    sc->sc_rx.buf_map[index].map);
   1016  1.13       ryo 		error = eqos_setup_rxbuf(sc, index, new_m);
   1017  1.13       ryo 		if (error)
   1018  1.13       ryo 			panic("%s: %s: unable to load RX mbuf. error=%d",
   1019  1.13       ryo 			    device_xname(sc->sc_dev), __func__, error);
   1020   1.1  jmcneill 
   1021  1.13       ryo 		if (m0 == NULL) {
   1022  1.13       ryo 			m0 = m;
   1023  1.13       ryo 		} else {
   1024  1.13       ryo 			if (m->m_flags & M_PKTHDR)
   1025  1.13       ryo 				m_remove_pkthdr(m);
   1026  1.13       ryo 			mprev->m_next = m;
   1027  1.13       ryo 		}
   1028  1.13       ryo 		mprev = m;
   1029  1.13       ryo 
   1030  1.13       ryo 		if ((tdes3 & EQOS_TDES3_RX_LD) == 0) {
   1031  1.13       ryo 			/* to be continued in the next segment */
   1032  1.13       ryo 			m->m_len = EQOS_RXDMA_SIZE;
   1033  1.13       ryo 		} else {
   1034  1.13       ryo 			/* last segment */
   1035  1.13       ryo 			uint32_t totallen = tdes3 & EQOS_TDES3_RX_LENGTH_MASK;
   1036  1.13       ryo 			uint32_t mlen = totallen % EQOS_RXDMA_SIZE;
   1037  1.13       ryo 			if (mlen == 0)
   1038  1.13       ryo 				mlen = EQOS_RXDMA_SIZE;
   1039  1.13       ryo 			m->m_len = mlen;
   1040  1.13       ryo 			m0->m_pkthdr.len = totallen;
   1041  1.13       ryo 			m_set_rcvif(m0, ifp);
   1042  1.13       ryo 			m0->m_flags |= M_HASFCS;
   1043  1.13       ryo 			m0->m_nextpkt = NULL;
   1044  1.47  jmcneill 
   1045  1.47  jmcneill 			if ((tdes3 & EQOS_TDES3_RX_RS1V) != 0 &&
   1046  1.47  jmcneill 			    (if_capenable & IFCAP_CSUM_IPv4_Rx) != 0) {
   1047  1.47  jmcneill 				uint32_t tdes1 =
   1048  1.47  jmcneill 				    le32toh(sc->sc_rx.desc_ring[index].tdes1);
   1049  1.47  jmcneill 
   1050  1.47  jmcneill 				if ((tdes1 & (EQOS_TDES1_RX_IPCE |
   1051  1.47  jmcneill 					      EQOS_TDES1_RX_IPCB)) == 0) {
   1052  1.47  jmcneill 					if ((tdes1 & EQOS_TDES1_RX_IPV4) != 0) {
   1053  1.47  jmcneill 						m0->m_pkthdr.csum_flags |=
   1054  1.47  jmcneill 						    M_CSUM_IPv4;
   1055  1.47  jmcneill 					}
   1056  1.47  jmcneill 					switch (tdes1 & EQOS_TDES1_RX_PT_MASK) {
   1057  1.47  jmcneill 					case EQOS_TDES1_RX_PT_UDP:
   1058  1.47  jmcneill 						m0->m_pkthdr.csum_flags |=
   1059  1.47  jmcneill 						    M_CSUM_UDPv4;
   1060  1.47  jmcneill 						break;
   1061  1.47  jmcneill 					case EQOS_TDES1_RX_PT_TCP:
   1062  1.47  jmcneill 						m0->m_pkthdr.csum_flags |=
   1063  1.47  jmcneill 						    M_CSUM_TCPv4;
   1064  1.47  jmcneill 						break;
   1065  1.47  jmcneill 					}
   1066  1.47  jmcneill 				}
   1067  1.47  jmcneill 			}
   1068  1.47  jmcneill 
   1069  1.13       ryo 			if_percpuq_enqueue(ifp->if_percpuq, m0);
   1070  1.13       ryo 			m0 = mprev = NULL;
   1071   1.1  jmcneill 
   1072   1.1  jmcneill 			++pkts;
   1073   1.1  jmcneill 		}
   1074   1.1  jmcneill 
   1075  1.13       ryo  rx_next:
   1076  1.13       ryo 		if (discarding && (tdes3 & EQOS_TDES3_RX_LD) != 0)
   1077  1.13       ryo 			discarding = false;
   1078  1.13       ryo 
   1079  1.13       ryo 		eqos_setup_rxdesc(sc, index,
   1080  1.13       ryo 		    sc->sc_rx.buf_map[index].map->dm_segs[0].ds_addr);
   1081   1.1  jmcneill 		eqos_dma_sync(sc, sc->sc_rx.desc_map,
   1082   1.1  jmcneill 		    index, index + 1, RX_DESC_COUNT,
   1083   1.1  jmcneill 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   1084  1.50  jmcneill 
   1085  1.50  jmcneill 		WR4(sc, GMAC_DMA_CHAN0_RX_END_ADDR,
   1086  1.50  jmcneill 		    (uint32_t)sc->sc_rx.desc_ring_paddr + DESC_OFF(index));
   1087   1.1  jmcneill 	}
   1088  1.13       ryo 	/* save jumboframe context */
   1089  1.13       ryo 	sc->sc_rx_discarding = discarding;
   1090  1.13       ryo 	sc->sc_rx_receiving_m = m0;
   1091  1.13       ryo 	sc->sc_rx_receiving_m_last = mprev;
   1092   1.1  jmcneill 
   1093  1.24   msaitoh 	DPRINTF(EDEB_RXRING, "sc_rx.cur %u -> %u\n",
   1094  1.24   msaitoh 	    sc->sc_rx.cur, index);
   1095   1.1  jmcneill 	sc->sc_rx.cur = index;
   1096   1.1  jmcneill 
   1097   1.1  jmcneill 	if (pkts != 0) {
   1098   1.1  jmcneill 		rnd_add_uint32(&sc->sc_rndsource, pkts);
   1099   1.1  jmcneill 	}
   1100   1.1  jmcneill }
   1101   1.1  jmcneill 
   1102   1.1  jmcneill static void
   1103   1.1  jmcneill eqos_txintr(struct eqos_softc *sc, int qid)
   1104   1.1  jmcneill {
   1105  1.15     skrll 	struct ifnet * const ifp = &sc->sc_ec.ec_if;
   1106   1.1  jmcneill 	struct eqos_bufmap *bmap;
   1107   1.1  jmcneill 	struct eqos_dma_desc *desc;
   1108   1.1  jmcneill 	uint32_t tdes3;
   1109   1.1  jmcneill 	int i, pkts = 0;
   1110   1.1  jmcneill 
   1111   1.8    martin 	DPRINTF(EDEB_INTR, "qid: %u\n", qid);
   1112   1.8    martin 
   1113  1.26   msaitoh 	EQOS_ASSERT_TXLOCKED(sc);
   1114   1.1  jmcneill 
   1115  1.45  jmcneill 	eqos_dma_sync(sc, sc->sc_tx.desc_map,
   1116  1.45  jmcneill 	    sc->sc_tx.next, sc->sc_tx.cur, TX_DESC_COUNT,
   1117  1.45  jmcneill 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1118  1.45  jmcneill 
   1119   1.1  jmcneill 	for (i = sc->sc_tx.next; sc->sc_tx.queued > 0; i = TX_NEXT(i)) {
   1120   1.1  jmcneill 		KASSERT(sc->sc_tx.queued > 0);
   1121   1.1  jmcneill 		KASSERT(sc->sc_tx.queued <= TX_DESC_COUNT);
   1122  1.45  jmcneill 
   1123   1.1  jmcneill 		desc = &sc->sc_tx.desc_ring[i];
   1124   1.1  jmcneill 		tdes3 = le32toh(desc->tdes3);
   1125  1.12       ryo 		if ((tdes3 & EQOS_TDES3_TX_OWN) != 0) {
   1126   1.1  jmcneill 			break;
   1127   1.1  jmcneill 		}
   1128   1.1  jmcneill 		bmap = &sc->sc_tx.buf_map[i];
   1129   1.1  jmcneill 		if (bmap->mbuf != NULL) {
   1130   1.1  jmcneill 			bus_dmamap_sync(sc->sc_dmat, bmap->map,
   1131   1.1  jmcneill 			    0, bmap->map->dm_mapsize,
   1132   1.1  jmcneill 			    BUS_DMASYNC_POSTWRITE);
   1133   1.1  jmcneill 			bus_dmamap_unload(sc->sc_dmat, bmap->map);
   1134   1.1  jmcneill 			m_freem(bmap->mbuf);
   1135   1.1  jmcneill 			bmap->mbuf = NULL;
   1136   1.1  jmcneill 			++pkts;
   1137   1.1  jmcneill 		}
   1138   1.1  jmcneill 
   1139  1.44  jmcneill 		--sc->sc_tx.queued;
   1140   1.1  jmcneill 
   1141   1.1  jmcneill 		/* Last descriptor in a packet contains DMA status */
   1142  1.12       ryo 		if ((tdes3 & EQOS_TDES3_TX_LD) != 0) {
   1143  1.12       ryo 			if ((tdes3 & EQOS_TDES3_TX_DE) != 0) {
   1144   1.1  jmcneill 				device_printf(sc->sc_dev,
   1145   1.1  jmcneill 				    "TX [%u] desc error: 0x%08x\n",
   1146   1.1  jmcneill 				    i, tdes3);
   1147   1.1  jmcneill 				if_statinc(ifp, if_oerrors);
   1148  1.12       ryo 			} else if ((tdes3 & EQOS_TDES3_TX_ES) != 0) {
   1149   1.1  jmcneill 				device_printf(sc->sc_dev,
   1150   1.1  jmcneill 				    "TX [%u] tx error: 0x%08x\n",
   1151   1.1  jmcneill 				    i, tdes3);
   1152   1.1  jmcneill 				if_statinc(ifp, if_oerrors);
   1153   1.1  jmcneill 			} else {
   1154   1.1  jmcneill 				if_statinc(ifp, if_opackets);
   1155   1.1  jmcneill 			}
   1156   1.1  jmcneill 		}
   1157   1.1  jmcneill 
   1158   1.1  jmcneill 	}
   1159   1.1  jmcneill 
   1160   1.1  jmcneill 	sc->sc_tx.next = i;
   1161   1.1  jmcneill 
   1162   1.1  jmcneill 	if (pkts != 0) {
   1163   1.1  jmcneill 		rnd_add_uint32(&sc->sc_rndsource, pkts);
   1164   1.1  jmcneill 	}
   1165   1.1  jmcneill }
   1166   1.1  jmcneill 
   1167   1.1  jmcneill static void
   1168   1.1  jmcneill eqos_start_locked(struct eqos_softc *sc)
   1169   1.1  jmcneill {
   1170  1.15     skrll 	struct ifnet * const ifp = &sc->sc_ec.ec_if;
   1171   1.1  jmcneill 	struct mbuf *m;
   1172   1.1  jmcneill 	int cnt, nsegs, start;
   1173  1.45  jmcneill 	uint32_t first_tdes3 = 0;
   1174  1.45  jmcneill 	uint32_t *ptdes3 = &first_tdes3;
   1175   1.1  jmcneill 
   1176   1.1  jmcneill 	EQOS_ASSERT_TXLOCKED(sc);
   1177   1.1  jmcneill 
   1178  1.31  riastrad 	if (!sc->sc_txrunning)
   1179   1.1  jmcneill 		return;
   1180   1.1  jmcneill 
   1181   1.1  jmcneill 	for (cnt = 0, start = sc->sc_tx.cur; ; cnt++) {
   1182   1.1  jmcneill 		if (sc->sc_tx.queued >= TX_DESC_COUNT - TX_MAX_SEGS) {
   1183   1.8    martin 			DPRINTF(EDEB_TXRING, "%u sc_tx.queued, ring full\n",
   1184   1.8    martin 			    sc->sc_tx.queued);
   1185   1.1  jmcneill 			break;
   1186   1.1  jmcneill 		}
   1187   1.1  jmcneill 
   1188   1.1  jmcneill 		IFQ_POLL(&ifp->if_snd, m);
   1189   1.8    martin 		if (m == NULL)
   1190   1.1  jmcneill 			break;
   1191   1.1  jmcneill 
   1192  1.45  jmcneill 		nsegs = eqos_setup_txbuf(sc, sc->sc_tx.cur, m, ptdes3);
   1193   1.1  jmcneill 		if (nsegs <= 0) {
   1194   1.8    martin 			DPRINTF(EDEB_TXRING, "eqos_setup_txbuf failed "
   1195   1.8    martin 			    "with %d\n", nsegs);
   1196  1.16   thorpej 			if (nsegs == -2) {
   1197   1.1  jmcneill 				IFQ_DEQUEUE(&ifp->if_snd, m);
   1198   1.1  jmcneill 				m_freem(m);
   1199  1.16   thorpej 				continue;
   1200   1.1  jmcneill 			}
   1201   1.1  jmcneill 			break;
   1202   1.1  jmcneill 		}
   1203   1.1  jmcneill 
   1204   1.1  jmcneill 		IFQ_DEQUEUE(&ifp->if_snd, m);
   1205   1.1  jmcneill 		bpf_mtap(ifp, m, BPF_D_OUT);
   1206   1.1  jmcneill 
   1207   1.1  jmcneill 		sc->sc_tx.cur = TX_SKIP(sc->sc_tx.cur, nsegs);
   1208  1.45  jmcneill 		ptdes3 = NULL;
   1209   1.1  jmcneill 	}
   1210   1.1  jmcneill 
   1211   1.8    martin 	DPRINTF(EDEB_TXRING, "tx loop -> cnt = %u, cur: %u, next: %u, "
   1212   1.8    martin 	    "queued: %u\n", cnt, sc->sc_tx.cur, sc->sc_tx.next,
   1213   1.8    martin 	    sc->sc_tx.queued);
   1214   1.8    martin 
   1215   1.1  jmcneill 	if (cnt != 0) {
   1216   1.1  jmcneill 		eqos_dma_sync(sc, sc->sc_tx.desc_map,
   1217   1.1  jmcneill 		    start, sc->sc_tx.cur, TX_DESC_COUNT,
   1218   1.1  jmcneill 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1219   1.1  jmcneill 
   1220  1.45  jmcneill 		/*
   1221  1.45  jmcneill 		 * Defer setting OWN bit on the first descriptor until all
   1222  1.45  jmcneill 		 * descriptors have been updated.  The hardware will not try to
   1223  1.45  jmcneill 		 * process any descriptors past the first one still owned by
   1224  1.45  jmcneill 		 * software (i.e., with the OWN bit clear).
   1225  1.45  jmcneill 		 */
   1226  1.45  jmcneill 		sc->sc_tx.desc_ring[start].tdes3 =
   1227  1.45  jmcneill 		    htole32(first_tdes3 | EQOS_TDES3_TX_OWN);
   1228  1.45  jmcneill 		bus_dmamap_sync(sc->sc_dmat, sc->sc_tx.desc_map,
   1229  1.45  jmcneill 		    DESC_OFF(start) + offsetof(struct eqos_dma_desc, tdes3),
   1230  1.45  jmcneill 		    sizeof(uint32_t),
   1231  1.45  jmcneill 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1232  1.45  jmcneill 
   1233   1.1  jmcneill 		/* Start and run TX DMA */
   1234   1.8    martin 		DPRINTF(EDEB_TXRING, "sending desc %u at %lx upto "
   1235   1.8    martin 		    "%u-1 at %lx cur tx desc: %x cur tx buf: %x\n", start,
   1236   1.8    martin 		    (uint32_t)sc->sc_tx.desc_ring_paddr + DESC_OFF(start),
   1237   1.8    martin 		    sc->sc_tx.cur,
   1238   1.8    martin 		    (uint32_t)sc->sc_tx.desc_ring_paddr +
   1239   1.8    martin 		    DESC_OFF(sc->sc_tx.cur),
   1240   1.8    martin 		    RD4(sc, GMAC_DMA_CHAN0_CUR_TX_DESC),
   1241   1.8    martin 		    RD4(sc, GMAC_DMA_CHAN0_CUR_TX_BUF_ADDR));
   1242   1.1  jmcneill 		WR4(sc, GMAC_DMA_CHAN0_TX_END_ADDR,
   1243   1.1  jmcneill 		    (uint32_t)sc->sc_tx.desc_ring_paddr +
   1244   1.1  jmcneill 		    DESC_OFF(sc->sc_tx.cur));
   1245   1.1  jmcneill 	}
   1246   1.1  jmcneill }
   1247   1.1  jmcneill 
   1248   1.1  jmcneill static void
   1249   1.1  jmcneill eqos_start(struct ifnet *ifp)
   1250   1.1  jmcneill {
   1251  1.15     skrll 	struct eqos_softc * const sc = ifp->if_softc;
   1252   1.1  jmcneill 
   1253  1.48  jmcneill 	EQOS_TXLOCK(sc);
   1254  1.48  jmcneill 	eqos_start_locked(sc);
   1255  1.48  jmcneill 	EQOS_TXUNLOCK(sc);
   1256  1.45  jmcneill }
   1257  1.45  jmcneill 
   1258  1.45  jmcneill static void
   1259  1.45  jmcneill eqos_receive(struct work *wk, void *arg)
   1260  1.45  jmcneill {
   1261  1.45  jmcneill 	struct eqos_softc *sc = arg;
   1262  1.45  jmcneill 	uint32_t dma_intr_enable;
   1263  1.45  jmcneill 
   1264  1.45  jmcneill 	EQOS_LOCK(sc);
   1265  1.45  jmcneill 
   1266  1.45  jmcneill 	eqos_rxintr(sc, 0);
   1267  1.45  jmcneill 
   1268  1.45  jmcneill 	dma_intr_enable = RD4(sc, GMAC_DMA_CHAN0_INTR_ENABLE);
   1269  1.48  jmcneill 	dma_intr_enable |= GMAC_DMA_CHAN0_INTR_ENABLE_RIE;
   1270  1.45  jmcneill 	WR4(sc, GMAC_DMA_CHAN0_INTR_ENABLE, dma_intr_enable);
   1271  1.45  jmcneill 
   1272  1.45  jmcneill 	EQOS_UNLOCK(sc);
   1273  1.45  jmcneill }
   1274  1.45  jmcneill 
   1275  1.45  jmcneill static void
   1276  1.48  jmcneill eqos_txsoftintr(void *arg)
   1277  1.45  jmcneill {
   1278  1.45  jmcneill 	struct eqos_softc *sc = arg;
   1279  1.49   mlelstv 	struct ifnet * const ifp = &sc->sc_ec.ec_if;
   1280  1.48  jmcneill 	uint32_t dma_intr_enable;
   1281  1.45  jmcneill 
   1282   1.1  jmcneill 	EQOS_TXLOCK(sc);
   1283  1.48  jmcneill 	eqos_txintr(sc, 0);
   1284   1.1  jmcneill 	EQOS_TXUNLOCK(sc);
   1285  1.45  jmcneill 
   1286  1.49   mlelstv 	if_schedule_deferred_start(ifp);
   1287  1.49   mlelstv 
   1288  1.48  jmcneill 	EQOS_LOCK(sc);
   1289  1.48  jmcneill 	dma_intr_enable = RD4(sc, GMAC_DMA_CHAN0_INTR_ENABLE);
   1290  1.48  jmcneill 	dma_intr_enable |= GMAC_DMA_CHAN0_INTR_ENABLE_TIE;
   1291  1.48  jmcneill 	WR4(sc, GMAC_DMA_CHAN0_INTR_ENABLE, dma_intr_enable);
   1292  1.48  jmcneill 	EQOS_UNLOCK(sc);
   1293   1.1  jmcneill }
   1294   1.1  jmcneill 
   1295   1.3       mrg static void
   1296   1.3       mrg eqos_intr_mtl(struct eqos_softc *sc, uint32_t mtl_status)
   1297   1.3       mrg {
   1298   1.3       mrg 	uint32_t debug_data __unused = 0, ictrl = 0;
   1299   1.3       mrg 
   1300   1.3       mrg 	if (mtl_status == 0)
   1301   1.3       mrg 		return;
   1302   1.3       mrg 
   1303   1.3       mrg 	/* Drain the errors reported by MTL_INTERRUPT_STATUS */
   1304   1.3       mrg 	sc->sc_ev_mtl.ev_count++;
   1305   1.3       mrg 
   1306   1.3       mrg 	if ((mtl_status & GMAC_MTL_INTERRUPT_STATUS_DBGIS) != 0) {
   1307   1.3       mrg 		debug_data = RD4(sc, GMAC_MTL_FIFO_DEBUG_DATA);
   1308   1.3       mrg 		sc->sc_ev_mtl_debugdata.ev_count++;
   1309   1.3       mrg 	}
   1310   1.3       mrg 	if ((mtl_status & GMAC_MTL_INTERRUPT_STATUS_Q0IS) != 0) {
   1311   1.3       mrg 		uint32_t new_status = 0;
   1312   1.3       mrg 
   1313   1.3       mrg 		ictrl = RD4(sc, GMAC_MTL_Q0_INTERRUPT_CTRL_STATUS);
   1314   1.3       mrg 		if ((ictrl & GMAC_MTL_Q0_INTERRUPT_CTRL_STATUS_RXOVFIS) != 0) {
   1315   1.3       mrg 			new_status |= GMAC_MTL_Q0_INTERRUPT_CTRL_STATUS_RXOVFIS;
   1316   1.3       mrg 			sc->sc_ev_mtl_rxovfis.ev_count++;
   1317   1.3       mrg 		}
   1318   1.3       mrg 		if ((ictrl & GMAC_MTL_Q0_INTERRUPT_CTRL_STATUS_TXUNFIS) != 0) {
   1319   1.3       mrg 			new_status |= GMAC_MTL_Q0_INTERRUPT_CTRL_STATUS_TXUNFIS;
   1320   1.3       mrg 			sc->sc_ev_mtl_txovfis.ev_count++;
   1321   1.3       mrg 		}
   1322   1.3       mrg 		if (new_status) {
   1323   1.3       mrg 			new_status |= (ictrl &
   1324  1.23   msaitoh 			    (GMAC_MTL_Q0_INTERRUPT_CTRL_STATUS_RXOIE |
   1325   1.3       mrg 			     GMAC_MTL_Q0_INTERRUPT_CTRL_STATUS_TXUIE));
   1326   1.3       mrg 			WR4(sc, GMAC_MTL_Q0_INTERRUPT_CTRL_STATUS, new_status);
   1327   1.3       mrg 		}
   1328   1.3       mrg 	}
   1329   1.8    martin 	DPRINTF(EDEB_INTR,
   1330   1.3       mrg 	    "GMAC_MTL_INTERRUPT_STATUS = 0x%08X, "
   1331   1.3       mrg 	    "GMAC_MTL_FIFO_DEBUG_DATA = 0x%08X, "
   1332   1.3       mrg 	    "GMAC_MTL_INTERRUPT_STATUS_Q0IS = 0x%08X\n",
   1333   1.3       mrg 	    mtl_status, debug_data, ictrl);
   1334   1.3       mrg }
   1335   1.3       mrg 
   1336   1.1  jmcneill int
   1337   1.1  jmcneill eqos_intr(void *arg)
   1338   1.1  jmcneill {
   1339  1.15     skrll 	struct eqos_softc * const sc = arg;
   1340  1.44  jmcneill 	uint32_t mac_status, mtl_status, dma_status, dma_intr_enable,
   1341  1.44  jmcneill 		 rx_tx_status;
   1342  1.48  jmcneill 	bool do_txintr = false;
   1343   1.1  jmcneill 
   1344  1.35     skrll 	EQOS_LOCK(sc);
   1345  1.35     skrll 
   1346   1.2       mrg 	sc->sc_ev_intr.ev_count++;
   1347   1.2       mrg 
   1348   1.1  jmcneill 	mac_status = RD4(sc, GMAC_MAC_INTERRUPT_STATUS);
   1349   1.1  jmcneill 	mac_status &= RD4(sc, GMAC_MAC_INTERRUPT_ENABLE);
   1350   1.1  jmcneill 
   1351   1.1  jmcneill 	if (mac_status) {
   1352   1.2       mrg 		sc->sc_ev_mac.ev_count++;
   1353   1.8    martin 		DPRINTF(EDEB_INTR,
   1354   1.1  jmcneill 		    "GMAC_MAC_INTERRUPT_STATUS = 0x%08X\n", mac_status);
   1355   1.1  jmcneill 	}
   1356   1.1  jmcneill 
   1357   1.1  jmcneill 	mtl_status = RD4(sc, GMAC_MTL_INTERRUPT_STATUS);
   1358   1.3       mrg 	eqos_intr_mtl(sc, mtl_status);
   1359   1.1  jmcneill 
   1360  1.44  jmcneill 	dma_intr_enable = RD4(sc, GMAC_DMA_CHAN0_INTR_ENABLE);
   1361   1.1  jmcneill 	dma_status = RD4(sc, GMAC_DMA_CHAN0_STATUS);
   1362  1.44  jmcneill 	dma_status &= dma_intr_enable;
   1363   1.1  jmcneill 	if (dma_status) {
   1364   1.1  jmcneill 		WR4(sc, GMAC_DMA_CHAN0_STATUS, dma_status);
   1365   1.1  jmcneill 	}
   1366   1.1  jmcneill 
   1367   1.1  jmcneill 	if ((dma_status & GMAC_DMA_CHAN0_STATUS_RI) != 0) {
   1368  1.48  jmcneill 		dma_intr_enable &= ~GMAC_DMA_CHAN0_INTR_ENABLE_RIE;
   1369  1.44  jmcneill 		WR4(sc, GMAC_DMA_CHAN0_INTR_ENABLE, dma_intr_enable);
   1370  1.44  jmcneill 		workqueue_enqueue(sc->sc_wq, &sc->sc_rxwork, NULL);
   1371   1.2       mrg 		sc->sc_ev_rxintr.ev_count++;
   1372   1.1  jmcneill 	}
   1373   1.1  jmcneill 
   1374  1.48  jmcneill 	if ((dma_status & GMAC_DMA_CHAN0_STATUS_TI) != 0) {
   1375  1.48  jmcneill 		do_txintr = true;
   1376  1.48  jmcneill 		dma_intr_enable &= ~GMAC_DMA_CHAN0_INTR_ENABLE_TIE;
   1377  1.48  jmcneill 		WR4(sc, GMAC_DMA_CHAN0_INTR_ENABLE, dma_intr_enable);
   1378  1.48  jmcneill 		sc->sc_ev_txintr.ev_count++;
   1379  1.48  jmcneill 	}
   1380  1.48  jmcneill 
   1381  1.35     skrll 	rx_tx_status = RD4(sc, GMAC_MAC_RX_TX_STATUS);
   1382  1.35     skrll 
   1383   1.1  jmcneill 	EQOS_UNLOCK(sc);
   1384   1.1  jmcneill 
   1385   1.1  jmcneill 	if ((mac_status | mtl_status | dma_status) == 0) {
   1386   1.8    martin 		DPRINTF(EDEB_NOTE, "spurious interrupt?!\n");
   1387   1.1  jmcneill 	}
   1388   1.1  jmcneill 
   1389   1.1  jmcneill 	if (rx_tx_status) {
   1390   1.2       mrg 		sc->sc_ev_status.ev_count++;
   1391   1.2       mrg 		if ((rx_tx_status & GMAC_MAC_RX_TX_STATUS_RWT) != 0)
   1392   1.2       mrg 			sc->sc_ev_rwt.ev_count++;
   1393   1.2       mrg 		if ((rx_tx_status & GMAC_MAC_RX_TX_STATUS_EXCOL) != 0)
   1394   1.2       mrg 			sc->sc_ev_excol.ev_count++;
   1395   1.2       mrg 		if ((rx_tx_status & GMAC_MAC_RX_TX_STATUS_LCOL) != 0)
   1396   1.2       mrg 			sc->sc_ev_lcol.ev_count++;
   1397   1.2       mrg 		if ((rx_tx_status & GMAC_MAC_RX_TX_STATUS_EXDEF) != 0)
   1398   1.2       mrg 			sc->sc_ev_exdef.ev_count++;
   1399   1.2       mrg 		if ((rx_tx_status & GMAC_MAC_RX_TX_STATUS_LCARR) != 0)
   1400   1.2       mrg 			sc->sc_ev_lcarr.ev_count++;
   1401   1.2       mrg 		if ((rx_tx_status & GMAC_MAC_RX_TX_STATUS_NCARR) != 0)
   1402   1.2       mrg 			sc->sc_ev_ncarr.ev_count++;
   1403   1.2       mrg 		if ((rx_tx_status & GMAC_MAC_RX_TX_STATUS_TJT) != 0)
   1404   1.2       mrg 			sc->sc_ev_tjt.ev_count++;
   1405   1.8    martin 
   1406  1.50  jmcneill 		DPRINTF(EDEB_INTR, "GMAC_MAC_RX_TX_STATUS = 0x%08x\n",
   1407   1.1  jmcneill 		    rx_tx_status);
   1408   1.1  jmcneill 	}
   1409   1.1  jmcneill 
   1410  1.48  jmcneill 	if (do_txintr) {
   1411  1.48  jmcneill 		softint_schedule(sc->sc_tx_si);
   1412  1.48  jmcneill 	}
   1413  1.48  jmcneill 
   1414   1.1  jmcneill 	return 1;
   1415   1.1  jmcneill }
   1416   1.1  jmcneill 
   1417   1.1  jmcneill static int
   1418   1.1  jmcneill eqos_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1419   1.1  jmcneill {
   1420  1.15     skrll 	struct eqos_softc * const sc = ifp->if_softc;
   1421  1.35     skrll 	int error;
   1422   1.1  jmcneill 
   1423  1.35     skrll 	switch (cmd) {
   1424  1.35     skrll 	case SIOCADDMULTI:
   1425  1.35     skrll 	case SIOCDELMULTI:
   1426  1.35     skrll 		break;
   1427  1.35     skrll 	default:
   1428  1.35     skrll 		KASSERT(IFNET_LOCKED(ifp));
   1429  1.35     skrll 	}
   1430   1.1  jmcneill 
   1431   1.1  jmcneill 	switch (cmd) {
   1432  1.35     skrll 	case SIOCSIFMTU: {
   1433  1.35     skrll 		struct ifreq * const ifr = (struct ifreq *)data;
   1434  1.13       ryo 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > EQOS_MAX_MTU) {
   1435  1.13       ryo 			error = EINVAL;
   1436  1.13       ryo 		} else {
   1437  1.13       ryo 			ifp->if_mtu = ifr->ifr_mtu;
   1438  1.13       ryo 			error = 0;	/* no need ENETRESET */
   1439  1.13       ryo 		}
   1440  1.13       ryo 		break;
   1441  1.35     skrll 	    }
   1442  1.35     skrll 	default: {
   1443  1.35     skrll 		const int s = splnet();
   1444   1.1  jmcneill 		error = ether_ioctl(ifp, cmd, data);
   1445   1.1  jmcneill 		splx(s);
   1446  1.35     skrll 
   1447   1.1  jmcneill 		if (error != ENETRESET)
   1448   1.1  jmcneill 			break;
   1449   1.1  jmcneill 
   1450   1.1  jmcneill 		error = 0;
   1451   1.1  jmcneill 
   1452   1.1  jmcneill 		if (cmd == SIOCSIFCAP)
   1453   1.1  jmcneill 			error = (*ifp->if_init)(ifp);
   1454  1.35     skrll 		else if (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI) {
   1455   1.1  jmcneill 			EQOS_LOCK(sc);
   1456  1.40     skrll 			if ((sc->sc_if_flags & IFF_RUNNING) != 0)
   1457  1.32  riastrad 				eqos_setup_rxfilter(sc);
   1458   1.1  jmcneill 			EQOS_UNLOCK(sc);
   1459   1.1  jmcneill 		}
   1460   1.1  jmcneill 		break;
   1461  1.35     skrll 	    }
   1462   1.1  jmcneill 	}
   1463   1.1  jmcneill 
   1464   1.1  jmcneill 	return error;
   1465   1.1  jmcneill }
   1466   1.1  jmcneill 
   1467  1.40     skrll static int
   1468  1.40     skrll eqos_ifflags_cb(struct ethercom *ec)
   1469  1.40     skrll {
   1470  1.40     skrll 	struct ifnet * const ifp = &ec->ec_if;
   1471  1.40     skrll 	struct eqos_softc * const sc = ifp->if_softc;
   1472  1.40     skrll 	int ret = 0;
   1473  1.40     skrll 
   1474  1.40     skrll 	KASSERT(IFNET_LOCKED(ifp));
   1475  1.40     skrll 	EQOS_LOCK(sc);
   1476  1.40     skrll 
   1477  1.40     skrll 	u_short change = ifp->if_flags ^ sc->sc_if_flags;
   1478  1.40     skrll 	sc->sc_if_flags = ifp->if_flags;
   1479  1.40     skrll 
   1480  1.40     skrll 	if ((change & ~(IFF_CANTCHANGE | IFF_DEBUG)) != 0) {
   1481  1.40     skrll 		ret = ENETRESET;
   1482  1.41     skrll 	} else if ((change & IFF_PROMISC) != 0) {
   1483  1.40     skrll 		if ((sc->sc_if_flags & IFF_RUNNING) != 0)
   1484  1.40     skrll 			eqos_setup_rxfilter(sc);
   1485  1.40     skrll 	}
   1486  1.40     skrll 	EQOS_UNLOCK(sc);
   1487  1.40     skrll 
   1488  1.40     skrll 	return ret;
   1489  1.40     skrll }
   1490  1.40     skrll 
   1491  1.40     skrll 
   1492   1.1  jmcneill static void
   1493   1.1  jmcneill eqos_get_eaddr(struct eqos_softc *sc, uint8_t *eaddr)
   1494   1.1  jmcneill {
   1495   1.1  jmcneill 	uint32_t maclo, machi;
   1496   1.1  jmcneill 
   1497  1.42   thorpej 	if (ether_getaddr(sc->sc_dev, eaddr)) {
   1498   1.1  jmcneill 		return;
   1499   1.1  jmcneill 	}
   1500   1.1  jmcneill 
   1501  1.18   msaitoh 	maclo = RD4(sc, GMAC_MAC_ADDRESS0_LOW);
   1502  1.46   mlelstv 	machi = __SHIFTOUT(RD4(sc, GMAC_MAC_ADDRESS0_HIGH),
   1503  1.46   mlelstv 	    GMAC_MAC_ADDRESS_HIGH_ADDR);
   1504  1.46   mlelstv 	if (maclo == 0xFFFFFFFF && machi == 0xFFFF) {
   1505  1.46   mlelstv 		/* Create one */
   1506  1.46   mlelstv 		maclo = 0x00f2 | (cprng_strong32() << 16);
   1507  1.46   mlelstv 		machi = cprng_strong32() & 0xffff;
   1508  1.46   mlelstv 	} else if (maclo & 0x00000001) {
   1509  1.18   msaitoh 		aprint_error_dev(sc->sc_dev,
   1510  1.38  bsiegert 		    "Wrong MAC address. Clearing the multicast bit.\n");
   1511  1.18   msaitoh 		maclo &= ~0x00000001;
   1512  1.18   msaitoh 	}
   1513   1.1  jmcneill 
   1514   1.1  jmcneill 	eaddr[0] = maclo & 0xff;
   1515   1.1  jmcneill 	eaddr[1] = (maclo >> 8) & 0xff;
   1516   1.1  jmcneill 	eaddr[2] = (maclo >> 16) & 0xff;
   1517   1.1  jmcneill 	eaddr[3] = (maclo >> 24) & 0xff;
   1518   1.1  jmcneill 	eaddr[4] = machi & 0xff;
   1519   1.1  jmcneill 	eaddr[5] = (machi >> 8) & 0xff;
   1520   1.1  jmcneill }
   1521   1.1  jmcneill 
   1522   1.1  jmcneill static void
   1523  1.27   msaitoh eqos_get_dma_pbl(struct eqos_softc *sc)
   1524  1.27   msaitoh {
   1525  1.27   msaitoh 	prop_dictionary_t prop = device_properties(sc->sc_dev);
   1526  1.27   msaitoh 	uint32_t pbl;
   1527  1.27   msaitoh 
   1528  1.27   msaitoh 	/* Set default values. */
   1529  1.27   msaitoh 	sc->sc_dma_txpbl = sc->sc_dma_rxpbl = EQOS_DMA_PBL_DEFAULT;
   1530  1.27   msaitoh 
   1531  1.27   msaitoh 	/* Get values from props. */
   1532  1.27   msaitoh 	if (prop_dictionary_get_uint32(prop, "snps,pbl", &pbl) && pbl)
   1533  1.27   msaitoh 		sc->sc_dma_txpbl = sc->sc_dma_rxpbl = pbl;
   1534  1.27   msaitoh 	if (prop_dictionary_get_uint32(prop, "snps,txpbl", &pbl) && pbl)
   1535  1.27   msaitoh 		sc->sc_dma_txpbl = pbl;
   1536  1.27   msaitoh 	if (prop_dictionary_get_uint32(prop, "snps,rxpbl", &pbl) && pbl)
   1537  1.27   msaitoh 		sc->sc_dma_rxpbl = pbl;
   1538  1.27   msaitoh }
   1539  1.27   msaitoh 
   1540  1.27   msaitoh static void
   1541   1.1  jmcneill eqos_axi_configure(struct eqos_softc *sc)
   1542   1.1  jmcneill {
   1543   1.1  jmcneill 	uint32_t val;
   1544   1.1  jmcneill 	u_int uival;
   1545   1.1  jmcneill 
   1546   1.1  jmcneill 	val = RD4(sc, GMAC_DMA_SYSBUS_MODE);
   1547  1.43   thorpej 
   1548  1.43   thorpej 	/* XXX are MB and FB mutually-exclusive? XXX */
   1549  1.43   thorpej 	if (device_getprop_bool(sc->sc_dev, "snps,mixed-burst")) {
   1550   1.1  jmcneill 		val |= GMAC_DMA_SYSBUS_MODE_MB;
   1551   1.1  jmcneill 	}
   1552  1.43   thorpej 	if (device_getprop_bool(sc->sc_dev, "snps,fixed-burst")) {
   1553   1.1  jmcneill 		val |= GMAC_DMA_SYSBUS_MODE_FB;
   1554   1.1  jmcneill 	}
   1555  1.43   thorpej 	if (device_getprop_uint(sc->sc_dev, "snps,wr_osr_lmt", &uival)) {
   1556   1.1  jmcneill 		val &= ~GMAC_DMA_SYSBUS_MODE_WR_OSR_LMT_MASK;
   1557  1.43   thorpej 		val |= (uival << GMAC_DMA_SYSBUS_MODE_WR_OSR_LMT_SHIFT) &
   1558  1.43   thorpej 		    GMAC_DMA_SYSBUS_MODE_WR_OSR_LMT_MASK;
   1559   1.1  jmcneill 	}
   1560  1.43   thorpej 	if (device_getprop_uint(sc->sc_dev, "snps,rd_osr_lmt", &uival)) {
   1561   1.1  jmcneill 		val &= ~GMAC_DMA_SYSBUS_MODE_RD_OSR_LMT_MASK;
   1562  1.43   thorpej 		val |= (uival << GMAC_DMA_SYSBUS_MODE_RD_OSR_LMT_SHIFT) &
   1563  1.43   thorpej 		    GMAC_DMA_SYSBUS_MODE_RD_OSR_LMT_MASK;
   1564   1.1  jmcneill 	}
   1565   1.1  jmcneill 
   1566   1.1  jmcneill 	if (!EQOS_HW_FEATURE_ADDR64_32BIT(sc)) {
   1567   1.1  jmcneill 		val |= GMAC_DMA_SYSBUS_MODE_EAME;
   1568   1.1  jmcneill 	}
   1569   1.1  jmcneill 
   1570  1.43   thorpej 	/* XXX snps,kbbe XXX */
   1571  1.43   thorpej 	/* XXX snps,blen XXX */
   1572  1.43   thorpej 
   1573   1.1  jmcneill 	/* XXX */
   1574   1.1  jmcneill 	val |= GMAC_DMA_SYSBUS_MODE_BLEN16;
   1575   1.1  jmcneill 	val |= GMAC_DMA_SYSBUS_MODE_BLEN8;
   1576   1.1  jmcneill 	val |= GMAC_DMA_SYSBUS_MODE_BLEN4;
   1577   1.1  jmcneill 
   1578   1.1  jmcneill 	WR4(sc, GMAC_DMA_SYSBUS_MODE, val);
   1579   1.1  jmcneill }
   1580   1.1  jmcneill 
   1581   1.1  jmcneill static int
   1582   1.1  jmcneill eqos_setup_dma(struct eqos_softc *sc, int qid)
   1583   1.1  jmcneill {
   1584   1.1  jmcneill 	struct mbuf *m;
   1585   1.1  jmcneill 	int error, nsegs, i;
   1586   1.1  jmcneill 
   1587  1.25   msaitoh 	/* Set back pointer */
   1588  1.25   msaitoh 	sc->sc_tx.sc = sc;
   1589  1.25   msaitoh 	sc->sc_rx.sc = sc;
   1590  1.25   msaitoh 
   1591   1.1  jmcneill 	/* Setup TX ring */
   1592   1.1  jmcneill 	error = bus_dmamap_create(sc->sc_dmat, TX_DESC_SIZE, 1, TX_DESC_SIZE,
   1593   1.1  jmcneill 	    DESC_BOUNDARY, BUS_DMA_WAITOK, &sc->sc_tx.desc_map);
   1594   1.1  jmcneill 	if (error) {
   1595   1.1  jmcneill 		return error;
   1596   1.1  jmcneill 	}
   1597   1.1  jmcneill 	error = bus_dmamem_alloc(sc->sc_dmat, TX_DESC_SIZE, DESC_ALIGN,
   1598   1.1  jmcneill 	    DESC_BOUNDARY, &sc->sc_tx.desc_dmaseg, 1, &nsegs, BUS_DMA_WAITOK);
   1599   1.1  jmcneill 	if (error) {
   1600   1.1  jmcneill 		return error;
   1601   1.1  jmcneill 	}
   1602   1.1  jmcneill 	error = bus_dmamem_map(sc->sc_dmat, &sc->sc_tx.desc_dmaseg, nsegs,
   1603  1.44  jmcneill 	    TX_DESC_SIZE, (void *)&sc->sc_tx.desc_ring,
   1604  1.44  jmcneill 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT);
   1605   1.1  jmcneill 	if (error) {
   1606   1.1  jmcneill 		return error;
   1607   1.1  jmcneill 	}
   1608   1.1  jmcneill 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_tx.desc_map,
   1609   1.1  jmcneill 	    sc->sc_tx.desc_ring, TX_DESC_SIZE, NULL, BUS_DMA_WAITOK);
   1610   1.1  jmcneill 	if (error) {
   1611   1.1  jmcneill 		return error;
   1612   1.1  jmcneill 	}
   1613   1.1  jmcneill 	sc->sc_tx.desc_ring_paddr = sc->sc_tx.desc_map->dm_segs[0].ds_addr;
   1614   1.1  jmcneill 
   1615   1.1  jmcneill 	memset(sc->sc_tx.desc_ring, 0, TX_DESC_SIZE);
   1616   1.1  jmcneill 	bus_dmamap_sync(sc->sc_dmat, sc->sc_tx.desc_map, 0, TX_DESC_SIZE,
   1617   1.1  jmcneill 	    BUS_DMASYNC_PREWRITE);
   1618   1.1  jmcneill 
   1619   1.1  jmcneill 	sc->sc_tx.queued = TX_DESC_COUNT;
   1620   1.1  jmcneill 	for (i = 0; i < TX_DESC_COUNT; i++) {
   1621  1.13       ryo 		error = bus_dmamap_create(sc->sc_dmat, EQOS_TXDMA_SIZE,
   1622  1.46   mlelstv 		    TX_MAX_SEGS, MCLBYTES, 0,
   1623  1.46   mlelstv 		    BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW,
   1624   1.1  jmcneill 		    &sc->sc_tx.buf_map[i].map);
   1625   1.1  jmcneill 		if (error != 0) {
   1626   1.1  jmcneill 			device_printf(sc->sc_dev,
   1627   1.1  jmcneill 			    "cannot create TX buffer map\n");
   1628   1.1  jmcneill 			return error;
   1629   1.1  jmcneill 		}
   1630  1.26   msaitoh 		EQOS_TXLOCK(sc);
   1631  1.55  jmcneill 		eqos_setup_txdesc(sc, i, 0, 0, 0, 0, false);
   1632  1.26   msaitoh 		EQOS_TXUNLOCK(sc);
   1633   1.1  jmcneill 	}
   1634   1.1  jmcneill 
   1635  1.53  jmcneill 	/* Setup RX buffers */
   1636  1.53  jmcneill 	error = bus_dmamap_create(sc->sc_dmat, EQOS_RXBUF_SIZE, 1,
   1637  1.53  jmcneill 	    EQOS_RXBUF_SIZE, 0, BUS_DMA_WAITOK, &sc->sc_rxdata.map);
   1638  1.53  jmcneill 	if (error) {
   1639  1.53  jmcneill 		return error;
   1640  1.53  jmcneill 	}
   1641  1.53  jmcneill 	error = bus_dmamem_alloc(sc->sc_dmat, EQOS_RXBUF_SIZE, PAGE_SIZE, 0,
   1642  1.53  jmcneill 	    &sc->sc_rxdata.seg, 1, &nsegs, BUS_DMA_WAITOK);
   1643  1.53  jmcneill 	if (error) {
   1644  1.53  jmcneill 		return error;
   1645  1.53  jmcneill 	}
   1646  1.53  jmcneill 	error = bus_dmamem_map(sc->sc_dmat, &sc->sc_rxdata.seg, nsegs,
   1647  1.53  jmcneill 	    EQOS_RXBUF_SIZE, &sc->sc_rxdata.vaddr, BUS_DMA_WAITOK);
   1648  1.53  jmcneill 	if (error) {
   1649  1.53  jmcneill 		return error;
   1650  1.53  jmcneill 	}
   1651  1.53  jmcneill 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_rxdata.map,
   1652  1.53  jmcneill 	    sc->sc_rxdata.vaddr, EQOS_RXBUF_SIZE, NULL, BUS_DMA_WAITOK);
   1653  1.53  jmcneill 	if (error) {
   1654  1.53  jmcneill 		return error;
   1655  1.53  jmcneill 	}
   1656  1.53  jmcneill 	sc->sc_rxdata.paddr = sc->sc_rxdata.map->dm_segs[0].ds_addr;
   1657  1.53  jmcneill 
   1658  1.53  jmcneill 	mutex_init(&sc->sc_rxdata.freelist_mtx, MUTEX_DEFAULT, IPL_NET);
   1659  1.53  jmcneill 	SLIST_INIT(&sc->sc_rxdata.freelist);
   1660  1.53  jmcneill 	for (i = 0; i < EQOS_DMA_RXBUF_COUNT; i++) {
   1661  1.53  jmcneill 		sc->sc_rxdata.rxbuf[i].sc = sc;
   1662  1.53  jmcneill 		sc->sc_rxdata.rxbuf[i].vaddr =
   1663  1.53  jmcneill 		    (char *)sc->sc_rxdata.vaddr + i * EQOS_RXDMA_SIZE;
   1664  1.53  jmcneill 		sc->sc_rxdata.rxbuf[i].paddr =
   1665  1.53  jmcneill 		    sc->sc_rxdata.paddr + i * EQOS_RXDMA_SIZE;
   1666  1.53  jmcneill 		SLIST_INSERT_HEAD(&sc->sc_rxdata.freelist,
   1667  1.53  jmcneill 		    &sc->sc_rxdata.rxbuf[i], next);
   1668  1.53  jmcneill 	}
   1669  1.53  jmcneill 
   1670   1.1  jmcneill 	/* Setup RX ring */
   1671   1.1  jmcneill 	error = bus_dmamap_create(sc->sc_dmat, RX_DESC_SIZE, 1, RX_DESC_SIZE,
   1672   1.1  jmcneill 	    DESC_BOUNDARY, BUS_DMA_WAITOK, &sc->sc_rx.desc_map);
   1673   1.1  jmcneill 	if (error) {
   1674   1.1  jmcneill 		return error;
   1675   1.1  jmcneill 	}
   1676   1.1  jmcneill 	error = bus_dmamem_alloc(sc->sc_dmat, RX_DESC_SIZE, DESC_ALIGN,
   1677   1.1  jmcneill 	    DESC_BOUNDARY, &sc->sc_rx.desc_dmaseg, 1, &nsegs, BUS_DMA_WAITOK);
   1678   1.1  jmcneill 	if (error) {
   1679   1.1  jmcneill 		return error;
   1680   1.1  jmcneill 	}
   1681   1.1  jmcneill 	error = bus_dmamem_map(sc->sc_dmat, &sc->sc_rx.desc_dmaseg, nsegs,
   1682  1.44  jmcneill 	    RX_DESC_SIZE, (void *)&sc->sc_rx.desc_ring,
   1683  1.44  jmcneill 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT);
   1684   1.1  jmcneill 	if (error) {
   1685   1.1  jmcneill 		return error;
   1686   1.1  jmcneill 	}
   1687   1.1  jmcneill 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_rx.desc_map,
   1688   1.1  jmcneill 	    sc->sc_rx.desc_ring, RX_DESC_SIZE, NULL, BUS_DMA_WAITOK);
   1689   1.1  jmcneill 	if (error) {
   1690   1.1  jmcneill 		return error;
   1691   1.1  jmcneill 	}
   1692   1.1  jmcneill 	sc->sc_rx.desc_ring_paddr = sc->sc_rx.desc_map->dm_segs[0].ds_addr;
   1693   1.1  jmcneill 
   1694   1.1  jmcneill 	memset(sc->sc_rx.desc_ring, 0, RX_DESC_SIZE);
   1695   1.1  jmcneill 
   1696   1.1  jmcneill 	for (i = 0; i < RX_DESC_COUNT; i++) {
   1697   1.1  jmcneill 		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
   1698  1.53  jmcneill 		    1, MCLBYTES, 0,
   1699  1.46   mlelstv 		    BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW,
   1700   1.1  jmcneill 		    &sc->sc_rx.buf_map[i].map);
   1701   1.1  jmcneill 		if (error != 0) {
   1702   1.1  jmcneill 			device_printf(sc->sc_dev,
   1703   1.1  jmcneill 			    "cannot create RX buffer map\n");
   1704   1.1  jmcneill 			return error;
   1705   1.1  jmcneill 		}
   1706   1.1  jmcneill 		if ((m = eqos_alloc_mbufcl(sc)) == NULL) {
   1707   1.1  jmcneill 			device_printf(sc->sc_dev, "cannot allocate RX mbuf\n");
   1708   1.1  jmcneill 			return ENOMEM;
   1709   1.1  jmcneill 		}
   1710   1.1  jmcneill 		error = eqos_setup_rxbuf(sc, i, m);
   1711   1.1  jmcneill 		if (error != 0) {
   1712   1.1  jmcneill 			device_printf(sc->sc_dev, "cannot create RX buffer\n");
   1713   1.1  jmcneill 			return error;
   1714   1.1  jmcneill 		}
   1715  1.13       ryo 		eqos_setup_rxdesc(sc, i,
   1716  1.13       ryo 		    sc->sc_rx.buf_map[i].map->dm_segs[0].ds_addr);
   1717   1.1  jmcneill 	}
   1718   1.1  jmcneill 	bus_dmamap_sync(sc->sc_dmat, sc->sc_rx.desc_map,
   1719   1.1  jmcneill 	    0, sc->sc_rx.desc_map->dm_mapsize,
   1720   1.1  jmcneill 	    BUS_DMASYNC_PREWRITE);
   1721   1.1  jmcneill 
   1722   1.1  jmcneill 	aprint_debug_dev(sc->sc_dev, "TX ring @ 0x%lX, RX ring @ 0x%lX\n",
   1723   1.1  jmcneill 	    sc->sc_tx.desc_ring_paddr, sc->sc_rx.desc_ring_paddr);
   1724   1.1  jmcneill 
   1725   1.1  jmcneill 	return 0;
   1726   1.1  jmcneill }
   1727   1.1  jmcneill 
   1728   1.1  jmcneill int
   1729   1.1  jmcneill eqos_attach(struct eqos_softc *sc)
   1730   1.1  jmcneill {
   1731  1.15     skrll 	struct mii_data * const mii = &sc->sc_mii;
   1732  1.15     skrll 	struct ifnet * const ifp = &sc->sc_ec.ec_if;
   1733  1.54  jmcneill 	struct ifcapreq ifcr;
   1734   1.1  jmcneill 	uint8_t eaddr[ETHER_ADDR_LEN];
   1735   1.1  jmcneill 	u_int userver, snpsver;
   1736   1.1  jmcneill 	int error;
   1737   1.1  jmcneill 	int n;
   1738   1.1  jmcneill 
   1739  1.25   msaitoh #ifdef EQOS_DEBUG
   1740  1.25   msaitoh 	/* Load the default debug flags. */
   1741  1.25   msaitoh 	sc->sc_debug = eqos_debug;
   1742  1.25   msaitoh #endif
   1743  1.25   msaitoh 
   1744   1.1  jmcneill 	const uint32_t ver = RD4(sc, GMAC_MAC_VERSION);
   1745   1.1  jmcneill 	userver = (ver & GMAC_MAC_VERSION_USERVER_MASK) >>
   1746   1.1  jmcneill 	    GMAC_MAC_VERSION_USERVER_SHIFT;
   1747   1.1  jmcneill 	snpsver = ver & GMAC_MAC_VERSION_SNPSVER_MASK;
   1748   1.1  jmcneill 
   1749  1.20   msaitoh 	if ((snpsver < 0x51) || (snpsver > 0x52)) {
   1750  1.36     skrll 		aprint_error(": EQOS version 0x%02x not supported\n",
   1751  1.23   msaitoh 		    snpsver);
   1752  1.23   msaitoh 		return ENXIO;
   1753  1.23   msaitoh 	}
   1754   1.1  jmcneill 
   1755   1.1  jmcneill 	if (sc->sc_csr_clock < 20000000) {
   1756   1.1  jmcneill 		aprint_error(": CSR clock too low\n");
   1757   1.1  jmcneill 		return EINVAL;
   1758   1.1  jmcneill 	} else if (sc->sc_csr_clock < 35000000) {
   1759   1.1  jmcneill 		sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_20_35;
   1760   1.1  jmcneill 	} else if (sc->sc_csr_clock < 60000000) {
   1761   1.1  jmcneill 		sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_35_60;
   1762   1.1  jmcneill 	} else if (sc->sc_csr_clock < 100000000) {
   1763   1.1  jmcneill 		sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_60_100;
   1764   1.1  jmcneill 	} else if (sc->sc_csr_clock < 150000000) {
   1765   1.1  jmcneill 		sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_100_150;
   1766   1.1  jmcneill 	} else if (sc->sc_csr_clock < 250000000) {
   1767   1.1  jmcneill 		sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_150_250;
   1768   1.1  jmcneill 	} else if (sc->sc_csr_clock < 300000000) {
   1769  1.19   msaitoh 		sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_250_300;
   1770  1.19   msaitoh 	} else if (sc->sc_csr_clock < 500000000) {
   1771   1.1  jmcneill 		sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_300_500;
   1772   1.1  jmcneill 	} else if (sc->sc_csr_clock < 800000000) {
   1773   1.1  jmcneill 		sc->sc_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_500_800;
   1774   1.1  jmcneill 	} else {
   1775   1.1  jmcneill 		aprint_error(": CSR clock too high\n");
   1776   1.1  jmcneill 		return EINVAL;
   1777   1.1  jmcneill 	}
   1778   1.1  jmcneill 
   1779   1.1  jmcneill 	for (n = 0; n < 4; n++) {
   1780   1.1  jmcneill 		sc->sc_hw_feature[n] = RD4(sc, GMAC_MAC_HW_FEATURE(n));
   1781   1.1  jmcneill 	}
   1782   1.1  jmcneill 
   1783   1.1  jmcneill 	aprint_naive("\n");
   1784   1.1  jmcneill 	aprint_normal(": DesignWare EQOS ver 0x%02x (0x%02x)\n",
   1785   1.1  jmcneill 	    snpsver, userver);
   1786   1.1  jmcneill 	aprint_verbose_dev(sc->sc_dev, "hw features %08x %08x %08x %08x\n",
   1787   1.1  jmcneill 	    sc->sc_hw_feature[0], sc->sc_hw_feature[1],
   1788   1.1  jmcneill 	    sc->sc_hw_feature[2], sc->sc_hw_feature[3]);
   1789   1.1  jmcneill 
   1790   1.1  jmcneill 	if (EQOS_HW_FEATURE_ADDR64_32BIT(sc)) {
   1791   1.1  jmcneill 		bus_dma_tag_t ntag;
   1792   1.1  jmcneill 
   1793   1.1  jmcneill 		error = bus_dmatag_subregion(sc->sc_dmat, 0, UINT32_MAX,
   1794   1.1  jmcneill 		    &ntag, 0);
   1795   1.1  jmcneill 		if (error) {
   1796   1.1  jmcneill 			aprint_error_dev(sc->sc_dev,
   1797   1.1  jmcneill 			    "failed to restrict DMA: %d\n", error);
   1798   1.1  jmcneill 			return error;
   1799   1.1  jmcneill 		}
   1800   1.1  jmcneill 		aprint_verbose_dev(sc->sc_dev, "using 32-bit DMA\n");
   1801   1.1  jmcneill 		sc->sc_dmat = ntag;
   1802   1.1  jmcneill 	}
   1803   1.1  jmcneill 
   1804   1.1  jmcneill 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NET);
   1805   1.1  jmcneill 	mutex_init(&sc->sc_txlock, MUTEX_DEFAULT, IPL_NET);
   1806  1.35     skrll 	callout_init(&sc->sc_stat_ch, CALLOUT_MPSAFE);
   1807   1.1  jmcneill 	callout_setfunc(&sc->sc_stat_ch, eqos_tick, sc);
   1808   1.1  jmcneill 
   1809   1.1  jmcneill 	eqos_get_eaddr(sc, eaddr);
   1810  1.23   msaitoh 	aprint_normal_dev(sc->sc_dev,
   1811  1.23   msaitoh 	    "Ethernet address %s\n", ether_sprintf(eaddr));
   1812   1.1  jmcneill 
   1813   1.1  jmcneill 	/* Soft reset EMAC core */
   1814   1.1  jmcneill 	error = eqos_reset(sc);
   1815   1.1  jmcneill 	if (error != 0) {
   1816   1.1  jmcneill 		return error;
   1817   1.1  jmcneill 	}
   1818   1.1  jmcneill 
   1819  1.27   msaitoh 	/* Get DMA burst length */
   1820  1.27   msaitoh 	eqos_get_dma_pbl(sc);
   1821  1.27   msaitoh 
   1822   1.1  jmcneill 	/* Configure AXI Bus mode parameters */
   1823   1.1  jmcneill 	eqos_axi_configure(sc);
   1824   1.1  jmcneill 
   1825   1.1  jmcneill 	/* Setup DMA descriptors */
   1826   1.1  jmcneill 	if (eqos_setup_dma(sc, 0) != 0) {
   1827  1.23   msaitoh 		aprint_error_dev(sc->sc_dev,
   1828  1.23   msaitoh 		    "failed to setup DMA descriptors\n");
   1829   1.1  jmcneill 		return EINVAL;
   1830   1.1  jmcneill 	}
   1831   1.1  jmcneill 
   1832  1.45  jmcneill 	/* Receive workers */
   1833  1.44  jmcneill 	error = workqueue_create(&sc->sc_wq, device_xname(sc->sc_dev),
   1834  1.45  jmcneill 	    eqos_receive, sc, PRI_SOFTNET, IPL_NET, WQ_MPSAFE | WQ_PERCPU);
   1835  1.45  jmcneill 	if (error != 0) {
   1836  1.45  jmcneill 		aprint_error_dev(sc->sc_dev,
   1837  1.45  jmcneill 		    "failed to create receive workqueue: %d\n", error);
   1838  1.45  jmcneill 		return error;
   1839  1.45  jmcneill 	}
   1840  1.45  jmcneill 
   1841  1.48  jmcneill 	/* Transmit softint */
   1842  1.48  jmcneill 	sc->sc_tx_si = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
   1843  1.48  jmcneill 	    eqos_txsoftintr, sc);
   1844  1.44  jmcneill 
   1845   1.1  jmcneill 	/* Setup ethernet interface */
   1846   1.1  jmcneill 	ifp->if_softc = sc;
   1847   1.1  jmcneill 	snprintf(ifp->if_xname, IFNAMSIZ, "%s", device_xname(sc->sc_dev));
   1848   1.1  jmcneill 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
   1849   1.1  jmcneill 	ifp->if_extflags = IFEF_MPSAFE;
   1850   1.1  jmcneill 	ifp->if_start = eqos_start;
   1851   1.1  jmcneill 	ifp->if_ioctl = eqos_ioctl;
   1852   1.1  jmcneill 	ifp->if_init = eqos_init;
   1853   1.1  jmcneill 	ifp->if_stop = eqos_stop;
   1854  1.47  jmcneill 	ifp->if_capabilities = IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx |
   1855  1.47  jmcneill             		       IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
   1856  1.47  jmcneill             		       IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx;
   1857  1.47  jmcneill 	ifp->if_capenable = 0;
   1858   1.1  jmcneill 	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
   1859   1.1  jmcneill 	IFQ_SET_READY(&ifp->if_snd);
   1860   1.1  jmcneill 
   1861  1.51  gutterid 	/* 802.1Q VLAN-sized frames and jumbo frames are supported */
   1862   1.1  jmcneill 	sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_MTU;
   1863  1.13       ryo 	sc->sc_ec.ec_capabilities |= ETHERCAP_JUMBO_MTU;
   1864   1.1  jmcneill 
   1865   1.1  jmcneill 	/* Attach MII driver */
   1866   1.1  jmcneill 	sc->sc_ec.ec_mii = mii;
   1867   1.1  jmcneill 	ifmedia_init(&mii->mii_media, 0, ether_mediachange, ether_mediastatus);
   1868   1.1  jmcneill 	mii->mii_ifp = ifp;
   1869   1.1  jmcneill 	mii->mii_readreg = eqos_mii_readreg;
   1870   1.1  jmcneill 	mii->mii_writereg = eqos_mii_writereg;
   1871   1.1  jmcneill 	mii->mii_statchg = eqos_mii_statchg;
   1872   1.1  jmcneill 	mii_attach(sc->sc_dev, mii, 0xffffffff, sc->sc_phy_id, MII_OFFSET_ANY,
   1873  1.28   msaitoh 	    MIIF_DOPAUSE);
   1874   1.1  jmcneill 
   1875   1.1  jmcneill 	if (LIST_EMPTY(&mii->mii_phys)) {
   1876   1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "no PHY found!\n");
   1877   1.1  jmcneill 		return ENOENT;
   1878   1.1  jmcneill 	}
   1879   1.1  jmcneill 	ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
   1880   1.1  jmcneill 
   1881   1.2       mrg 	/* Master interrupt evcnt */
   1882   1.2       mrg 	evcnt_attach_dynamic(&sc->sc_ev_intr, EVCNT_TYPE_INTR,
   1883   1.2       mrg 	    NULL, device_xname(sc->sc_dev), "interrupts");
   1884   1.2       mrg 
   1885   1.2       mrg 	/* Per-interrupt type, using main interrupt */
   1886   1.2       mrg 	evcnt_attach_dynamic(&sc->sc_ev_rxintr, EVCNT_TYPE_INTR,
   1887   1.2       mrg 	    &sc->sc_ev_intr, device_xname(sc->sc_dev), "rxintr");
   1888   1.2       mrg 	evcnt_attach_dynamic(&sc->sc_ev_txintr, EVCNT_TYPE_INTR,
   1889   1.2       mrg 	    &sc->sc_ev_intr, device_xname(sc->sc_dev), "txintr");
   1890   1.2       mrg 	evcnt_attach_dynamic(&sc->sc_ev_mac, EVCNT_TYPE_INTR,
   1891   1.2       mrg 	    &sc->sc_ev_intr, device_xname(sc->sc_dev), "macstatus");
   1892   1.2       mrg 	evcnt_attach_dynamic(&sc->sc_ev_mtl, EVCNT_TYPE_INTR,
   1893   1.2       mrg 	    &sc->sc_ev_intr, device_xname(sc->sc_dev), "intrstatus");
   1894   1.2       mrg 	evcnt_attach_dynamic(&sc->sc_ev_status, EVCNT_TYPE_INTR,
   1895   1.2       mrg 	    &sc->sc_ev_intr, device_xname(sc->sc_dev), "rxtxstatus");
   1896   1.2       mrg 
   1897   1.3       mrg 	/* MAC Status specific type, using macstatus interrupt */
   1898   1.3       mrg 	evcnt_attach_dynamic(&sc->sc_ev_mtl_debugdata, EVCNT_TYPE_INTR,
   1899   1.3       mrg 	    &sc->sc_ev_mtl, device_xname(sc->sc_dev), "debugdata");
   1900   1.3       mrg 	evcnt_attach_dynamic(&sc->sc_ev_mtl_rxovfis, EVCNT_TYPE_INTR,
   1901   1.3       mrg 	    &sc->sc_ev_mtl, device_xname(sc->sc_dev), "rxovfis");
   1902   1.3       mrg 	evcnt_attach_dynamic(&sc->sc_ev_mtl_txovfis, EVCNT_TYPE_INTR,
   1903   1.3       mrg 	    &sc->sc_ev_mtl, device_xname(sc->sc_dev), "txovfis");
   1904   1.3       mrg 
   1905   1.2       mrg 	/* RX/TX Status specific type, using rxtxstatus interrupt */
   1906   1.2       mrg 	evcnt_attach_dynamic(&sc->sc_ev_rwt, EVCNT_TYPE_INTR,
   1907   1.2       mrg 	    &sc->sc_ev_status, device_xname(sc->sc_dev), "rwt");
   1908   1.2       mrg 	evcnt_attach_dynamic(&sc->sc_ev_excol, EVCNT_TYPE_INTR,
   1909   1.2       mrg 	    &sc->sc_ev_status, device_xname(sc->sc_dev), "excol");
   1910   1.2       mrg 	evcnt_attach_dynamic(&sc->sc_ev_lcol, EVCNT_TYPE_INTR,
   1911   1.2       mrg 	    &sc->sc_ev_status, device_xname(sc->sc_dev), "lcol");
   1912   1.2       mrg 	evcnt_attach_dynamic(&sc->sc_ev_exdef, EVCNT_TYPE_INTR,
   1913   1.2       mrg 	    &sc->sc_ev_status, device_xname(sc->sc_dev), "exdef");
   1914   1.2       mrg 	evcnt_attach_dynamic(&sc->sc_ev_lcarr, EVCNT_TYPE_INTR,
   1915   1.2       mrg 	    &sc->sc_ev_status, device_xname(sc->sc_dev), "lcarr");
   1916   1.2       mrg 	evcnt_attach_dynamic(&sc->sc_ev_ncarr, EVCNT_TYPE_INTR,
   1917   1.2       mrg 	    &sc->sc_ev_status, device_xname(sc->sc_dev), "ncarr");
   1918   1.2       mrg 	evcnt_attach_dynamic(&sc->sc_ev_tjt, EVCNT_TYPE_INTR,
   1919   1.2       mrg 	    &sc->sc_ev_status, device_xname(sc->sc_dev), "tjt");
   1920   1.2       mrg 
   1921   1.1  jmcneill 	/* Attach interface */
   1922   1.1  jmcneill 	if_attach(ifp);
   1923   1.1  jmcneill 	if_deferred_start_init(ifp, NULL);
   1924   1.1  jmcneill 
   1925   1.1  jmcneill 	/* Attach ethernet interface */
   1926   1.1  jmcneill 	ether_ifattach(ifp, eaddr);
   1927  1.40     skrll 	ether_set_ifflags_cb(&sc->sc_ec, eqos_ifflags_cb);
   1928   1.1  jmcneill 
   1929  1.25   msaitoh 	eqos_init_sysctls(sc);
   1930  1.25   msaitoh 
   1931   1.1  jmcneill 	rnd_attach_source(&sc->sc_rndsource, ifp->if_xname, RND_TYPE_NET,
   1932   1.1  jmcneill 	    RND_FLAG_DEFAULT);
   1933   1.1  jmcneill 
   1934  1.54  jmcneill 	/* Try to enable COE */
   1935  1.54  jmcneill 	memset(&ifcr, 0, sizeof(ifcr));
   1936  1.54  jmcneill 	snprintf(ifcr.ifcr_name, sizeof(ifcr.ifcr_name), "%s", if_name(ifp));
   1937  1.54  jmcneill 	ifcr.ifcr_capenable = ifp->if_capabilities;
   1938  1.54  jmcneill 	error = ifioctl_common(ifp, SIOCSIFCAP, &ifcr);
   1939  1.54  jmcneill 	if (error != 0) {
   1940  1.54  jmcneill 		aprint_error_dev(sc->sc_dev, "failed to enable COE: %d\n",
   1941  1.54  jmcneill 		    error);
   1942  1.54  jmcneill 	}
   1943  1.54  jmcneill 
   1944   1.1  jmcneill 	return 0;
   1945   1.1  jmcneill }
   1946  1.25   msaitoh 
   1947  1.25   msaitoh static void
   1948  1.25   msaitoh eqos_init_sysctls(struct eqos_softc *sc)
   1949  1.25   msaitoh {
   1950  1.25   msaitoh 	struct sysctllog **log;
   1951  1.25   msaitoh 	const struct sysctlnode *rnode, *qnode, *cnode;
   1952  1.25   msaitoh 	const char *dvname;
   1953  1.25   msaitoh 	int i, rv;
   1954  1.25   msaitoh 
   1955  1.25   msaitoh 	log = &sc->sc_sysctllog;
   1956  1.25   msaitoh 	dvname = device_xname(sc->sc_dev);
   1957  1.25   msaitoh 
   1958  1.25   msaitoh 	rv = sysctl_createv(log, 0, NULL, &rnode,
   1959  1.25   msaitoh 	    0, CTLTYPE_NODE, dvname,
   1960  1.25   msaitoh 	    SYSCTL_DESCR("eqos information and settings"),
   1961  1.25   msaitoh 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
   1962  1.25   msaitoh 	if (rv != 0)
   1963  1.25   msaitoh 		goto err;
   1964  1.25   msaitoh 
   1965  1.25   msaitoh 	for (i = 0; i < 1; i++) {
   1966  1.25   msaitoh 		struct eqos_ring *txr = &sc->sc_tx;
   1967  1.25   msaitoh 		struct eqos_ring *rxr = &sc->sc_rx;
   1968  1.25   msaitoh 		const unsigned char *name = "q0";
   1969  1.25   msaitoh 
   1970  1.25   msaitoh 		if (sysctl_createv(log, 0, &rnode, &qnode,
   1971  1.25   msaitoh 		    0, CTLTYPE_NODE,
   1972  1.25   msaitoh 		    name, SYSCTL_DESCR("Queue Name"),
   1973  1.25   msaitoh 		    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL) != 0)
   1974  1.25   msaitoh 			break;
   1975  1.25   msaitoh 
   1976  1.25   msaitoh 		if (sysctl_createv(log, 0, &qnode, &cnode,
   1977  1.25   msaitoh 		    CTLFLAG_READONLY, CTLTYPE_INT,
   1978  1.25   msaitoh 		    "txs_cur", SYSCTL_DESCR("TX cur"),
   1979  1.25   msaitoh 		    NULL, 0, &txr->cur,
   1980  1.25   msaitoh 		    0, CTL_CREATE, CTL_EOL) != 0)
   1981  1.25   msaitoh 			break;
   1982  1.25   msaitoh 		if (sysctl_createv(log, 0, &qnode, &cnode,
   1983  1.25   msaitoh 		    CTLFLAG_READONLY, CTLTYPE_INT,
   1984  1.25   msaitoh 		    "txs_next", SYSCTL_DESCR("TX next"),
   1985  1.25   msaitoh 		    NULL, 0, &txr->next,
   1986  1.25   msaitoh 		    0, CTL_CREATE, CTL_EOL) != 0)
   1987  1.25   msaitoh 			break;
   1988  1.25   msaitoh 		if (sysctl_createv(log, 0, &qnode, &cnode,
   1989  1.25   msaitoh 		    CTLFLAG_READONLY, CTLTYPE_INT,
   1990  1.25   msaitoh 		    "txs_queued", SYSCTL_DESCR("TX queued"),
   1991  1.25   msaitoh 		    NULL, 0, &txr->queued,
   1992  1.25   msaitoh 		    0, CTL_CREATE, CTL_EOL) != 0)
   1993  1.25   msaitoh 			break;
   1994  1.25   msaitoh 		if (sysctl_createv(log, 0, &qnode, &cnode,
   1995  1.25   msaitoh 		    CTLFLAG_READONLY, CTLTYPE_INT,
   1996  1.25   msaitoh 		    "txr_cur", SYSCTL_DESCR("TX descriptor cur"),
   1997  1.25   msaitoh 		    eqos_sysctl_tx_cur_handler, 0, (void *)txr,
   1998  1.25   msaitoh 		    0, CTL_CREATE, CTL_EOL) != 0)
   1999  1.25   msaitoh 			break;
   2000  1.25   msaitoh 		if (sysctl_createv(log, 0, &qnode, &cnode,
   2001  1.25   msaitoh 		    CTLFLAG_READONLY, CTLTYPE_INT,
   2002  1.25   msaitoh 		    "txr_end", SYSCTL_DESCR("TX descriptor end"),
   2003  1.25   msaitoh 		    eqos_sysctl_tx_end_handler, 0, (void *)txr,
   2004  1.25   msaitoh 		    0, CTL_CREATE, CTL_EOL) != 0)
   2005  1.25   msaitoh 			break;
   2006  1.25   msaitoh 		if (sysctl_createv(log, 0, &qnode, &cnode,
   2007  1.25   msaitoh 		    CTLFLAG_READONLY, CTLTYPE_INT,
   2008  1.25   msaitoh 		    "rxs_cur", SYSCTL_DESCR("RX cur"),
   2009  1.25   msaitoh 		    NULL, 0, &rxr->cur,
   2010  1.25   msaitoh 		    0, CTL_CREATE, CTL_EOL) != 0)
   2011  1.25   msaitoh 			break;
   2012  1.25   msaitoh 		if (sysctl_createv(log, 0, &qnode, &cnode,
   2013  1.25   msaitoh 		    CTLFLAG_READONLY, CTLTYPE_INT,
   2014  1.25   msaitoh 		    "rxs_next", SYSCTL_DESCR("RX next"),
   2015  1.25   msaitoh 		    NULL, 0, &rxr->next,
   2016  1.25   msaitoh 		    0, CTL_CREATE, CTL_EOL) != 0)
   2017  1.25   msaitoh 			break;
   2018  1.25   msaitoh 		if (sysctl_createv(log, 0, &qnode, &cnode,
   2019  1.25   msaitoh 		    CTLFLAG_READONLY, CTLTYPE_INT,
   2020  1.25   msaitoh 		    "rxs_queued", SYSCTL_DESCR("RX queued"),
   2021  1.25   msaitoh 		    NULL, 0, &rxr->queued,
   2022  1.25   msaitoh 		    0, CTL_CREATE, CTL_EOL) != 0)
   2023  1.25   msaitoh 			break;
   2024  1.25   msaitoh 		if (sysctl_createv(log, 0, &qnode, &cnode,
   2025  1.25   msaitoh 		    CTLFLAG_READONLY, CTLTYPE_INT,
   2026  1.25   msaitoh 		    "rxr_cur", SYSCTL_DESCR("RX descriptor cur"),
   2027  1.25   msaitoh 		    eqos_sysctl_rx_cur_handler, 0, (void *)rxr,
   2028  1.25   msaitoh 		    0, CTL_CREATE, CTL_EOL) != 0)
   2029  1.25   msaitoh 			break;
   2030  1.25   msaitoh 		if (sysctl_createv(log, 0, &qnode, &cnode,
   2031  1.25   msaitoh 		    CTLFLAG_READONLY, CTLTYPE_INT,
   2032  1.25   msaitoh 		    "rxr_end", SYSCTL_DESCR("RX descriptor end"),
   2033  1.25   msaitoh 		    eqos_sysctl_rx_end_handler, 0, (void *)rxr,
   2034  1.25   msaitoh 		    0, CTL_CREATE, CTL_EOL) != 0)
   2035  1.25   msaitoh 			break;
   2036  1.25   msaitoh 	}
   2037  1.25   msaitoh 
   2038  1.25   msaitoh #ifdef EQOS_DEBUG
   2039  1.25   msaitoh 	rv = sysctl_createv(log, 0, &rnode, &cnode, CTLFLAG_READWRITE,
   2040  1.25   msaitoh 	    CTLTYPE_INT, "debug_flags",
   2041  1.25   msaitoh 	    SYSCTL_DESCR(
   2042  1.25   msaitoh 		    "Debug flags:\n"	\
   2043  1.25   msaitoh 		    "\t0x01 NOTE\n"	\
   2044  1.25   msaitoh 		    "\t0x02 INTR\n"	\
   2045  1.25   msaitoh 		    "\t0x04 RX RING\n"	\
   2046  1.25   msaitoh 		    "\t0x08 TX RING\n"),
   2047  1.25   msaitoh 	    eqos_sysctl_debug_handler, 0, (void *)sc, 0, CTL_CREATE, CTL_EOL);
   2048  1.25   msaitoh #endif
   2049  1.25   msaitoh 
   2050  1.25   msaitoh 	return;
   2051  1.25   msaitoh 
   2052  1.25   msaitoh err:
   2053  1.25   msaitoh 	sc->sc_sysctllog = NULL;
   2054  1.25   msaitoh 	device_printf(sc->sc_dev, "%s: sysctl_createv failed, rv = %d\n",
   2055  1.25   msaitoh 	    __func__, rv);
   2056  1.25   msaitoh }
   2057  1.25   msaitoh 
   2058  1.25   msaitoh static int
   2059  1.25   msaitoh eqos_sysctl_tx_cur_handler(SYSCTLFN_ARGS)
   2060  1.25   msaitoh {
   2061  1.25   msaitoh 	struct sysctlnode node = *rnode;
   2062  1.25   msaitoh 	struct eqos_ring *txq = (struct eqos_ring *)node.sysctl_data;
   2063  1.25   msaitoh 	struct eqos_softc *sc = txq->sc;
   2064  1.25   msaitoh 	uint32_t reg, index;
   2065  1.25   msaitoh 
   2066  1.25   msaitoh 	reg = RD4(sc, GMAC_DMA_CHAN0_CUR_TX_DESC);
   2067  1.25   msaitoh #if 0
   2068  1.25   msaitoh 	printf("head  = %08x\n", (uint32_t)sc->sc_tx.desc_ring_paddr);
   2069  1.25   msaitoh 	printf("cdesc = %08x\n", reg);
   2070  1.25   msaitoh 	printf("index = %zu\n",
   2071  1.25   msaitoh 	    (reg - (uint32_t)sc->sc_tx.desc_ring_paddr) /
   2072  1.25   msaitoh 	    sizeof(struct eqos_dma_desc));
   2073  1.25   msaitoh #endif
   2074  1.25   msaitoh 	if (reg == 0)
   2075  1.25   msaitoh 		index = 0;
   2076  1.25   msaitoh 	else {
   2077  1.25   msaitoh 		index = (reg - (uint32_t)sc->sc_tx.desc_ring_paddr) /
   2078  1.25   msaitoh 		    sizeof(struct eqos_dma_desc);
   2079  1.25   msaitoh 	}
   2080  1.25   msaitoh 	node.sysctl_data = &index;
   2081  1.25   msaitoh 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   2082  1.25   msaitoh }
   2083  1.25   msaitoh 
   2084  1.25   msaitoh static int
   2085  1.25   msaitoh eqos_sysctl_tx_end_handler(SYSCTLFN_ARGS)
   2086  1.25   msaitoh {
   2087  1.25   msaitoh 	struct sysctlnode node = *rnode;
   2088  1.25   msaitoh 	struct eqos_ring *txq = (struct eqos_ring *)node.sysctl_data;
   2089  1.25   msaitoh 	struct eqos_softc *sc = txq->sc;
   2090  1.25   msaitoh 	uint32_t reg, index;
   2091  1.25   msaitoh 
   2092  1.25   msaitoh 	reg = RD4(sc, GMAC_DMA_CHAN0_TX_END_ADDR);
   2093  1.25   msaitoh 	if (reg == 0)
   2094  1.25   msaitoh 		index = 0;
   2095  1.25   msaitoh 	else {
   2096  1.25   msaitoh 		index = (reg - (uint32_t)sc->sc_tx.desc_ring_paddr) /
   2097  1.25   msaitoh 		    sizeof(struct eqos_dma_desc);
   2098  1.25   msaitoh 	}
   2099  1.25   msaitoh 	node.sysctl_data = &index;
   2100  1.25   msaitoh 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   2101  1.25   msaitoh }
   2102  1.25   msaitoh 
   2103  1.25   msaitoh static int
   2104  1.25   msaitoh eqos_sysctl_rx_cur_handler(SYSCTLFN_ARGS)
   2105  1.25   msaitoh {
   2106  1.25   msaitoh 	struct sysctlnode node = *rnode;
   2107  1.25   msaitoh 	struct eqos_ring *rxq = (struct eqos_ring *)node.sysctl_data;
   2108  1.25   msaitoh 	struct eqos_softc *sc = rxq->sc;
   2109  1.25   msaitoh 	uint32_t reg, index;
   2110  1.25   msaitoh 
   2111  1.25   msaitoh 	reg = RD4(sc, GMAC_DMA_CHAN0_CUR_RX_DESC);
   2112  1.25   msaitoh 	if (reg == 0)
   2113  1.25   msaitoh 		index = 0;
   2114  1.25   msaitoh 	else {
   2115  1.25   msaitoh 		index = (reg - (uint32_t)sc->sc_rx.desc_ring_paddr) /
   2116  1.25   msaitoh 		    sizeof(struct eqos_dma_desc);
   2117  1.25   msaitoh 	}
   2118  1.25   msaitoh 	node.sysctl_data = &index;
   2119  1.25   msaitoh 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   2120  1.25   msaitoh }
   2121  1.25   msaitoh 
   2122  1.25   msaitoh static int
   2123  1.25   msaitoh eqos_sysctl_rx_end_handler(SYSCTLFN_ARGS)
   2124  1.25   msaitoh {
   2125  1.25   msaitoh 	struct sysctlnode node = *rnode;
   2126  1.25   msaitoh 	struct eqos_ring *rxq = (struct eqos_ring *)node.sysctl_data;
   2127  1.25   msaitoh 	struct eqos_softc *sc = rxq->sc;
   2128  1.25   msaitoh 	uint32_t reg, index;
   2129  1.25   msaitoh 
   2130  1.25   msaitoh 	reg = RD4(sc, GMAC_DMA_CHAN0_RX_END_ADDR);
   2131  1.25   msaitoh 	if (reg == 0)
   2132  1.25   msaitoh 		index = 0;
   2133  1.25   msaitoh 	else {
   2134  1.25   msaitoh 		index = (reg - (uint32_t)sc->sc_rx.desc_ring_paddr) /
   2135  1.25   msaitoh 		    sizeof(struct eqos_dma_desc);
   2136  1.25   msaitoh 	}
   2137  1.25   msaitoh 	node.sysctl_data = &index;
   2138  1.25   msaitoh 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   2139  1.25   msaitoh }
   2140  1.25   msaitoh 
   2141  1.25   msaitoh #ifdef EQOS_DEBUG
   2142  1.25   msaitoh static int
   2143  1.25   msaitoh eqos_sysctl_debug_handler(SYSCTLFN_ARGS)
   2144  1.25   msaitoh {
   2145  1.25   msaitoh 	struct sysctlnode node = *rnode;
   2146  1.25   msaitoh 	struct eqos_softc *sc = (struct eqos_softc *)node.sysctl_data;
   2147  1.25   msaitoh 	uint32_t dflags;
   2148  1.25   msaitoh 	int error;
   2149  1.25   msaitoh 
   2150  1.25   msaitoh 	dflags = sc->sc_debug;
   2151  1.25   msaitoh 	node.sysctl_data = &dflags;
   2152  1.25   msaitoh 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   2153  1.25   msaitoh 
   2154  1.25   msaitoh 	if (error || newp == NULL)
   2155  1.25   msaitoh 		return error;
   2156  1.25   msaitoh 
   2157  1.25   msaitoh 	sc->sc_debug = dflags;
   2158  1.25   msaitoh #if 0
   2159  1.25   msaitoh 	/* Addd debug code here if you want. */
   2160  1.25   msaitoh #endif
   2161  1.25   msaitoh 
   2162  1.25   msaitoh 	return 0;
   2163  1.25   msaitoh }
   2164  1.25   msaitoh #endif
   2165