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