Home | History | Annotate | Line # | Download | only in ic
      1  1.22     skrll /* $NetBSD: bcmgenet.c,v 1.22 2024/10/06 19:34:06 skrll Exp $ */
      2   1.1  jmcneill 
      3   1.1  jmcneill /*-
      4   1.1  jmcneill  * Copyright (c) 2020 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  * Broadcom GENETv5
     31   1.1  jmcneill  */
     32   1.1  jmcneill 
     33   1.1  jmcneill #include "opt_ddb.h"
     34   1.1  jmcneill 
     35   1.1  jmcneill #include <sys/cdefs.h>
     36  1.22     skrll __KERNEL_RCSID(0, "$NetBSD: bcmgenet.c,v 1.22 2024/10/06 19:34:06 skrll Exp $");
     37   1.1  jmcneill 
     38   1.1  jmcneill #include <sys/param.h>
     39   1.1  jmcneill #include <sys/bus.h>
     40   1.1  jmcneill #include <sys/device.h>
     41   1.1  jmcneill #include <sys/intr.h>
     42   1.1  jmcneill #include <sys/systm.h>
     43   1.1  jmcneill #include <sys/kernel.h>
     44   1.1  jmcneill #include <sys/mutex.h>
     45   1.1  jmcneill #include <sys/callout.h>
     46   1.1  jmcneill #include <sys/cprng.h>
     47   1.1  jmcneill 
     48   1.9       rin #include <sys/rndsource.h>
     49   1.9       rin 
     50   1.1  jmcneill #include <net/if.h>
     51   1.1  jmcneill #include <net/if_dl.h>
     52   1.1  jmcneill #include <net/if_ether.h>
     53   1.1  jmcneill #include <net/if_media.h>
     54   1.1  jmcneill #include <net/bpf.h>
     55   1.1  jmcneill 
     56   1.1  jmcneill #include <dev/mii/miivar.h>
     57   1.1  jmcneill 
     58   1.1  jmcneill #include <dev/ic/bcmgenetreg.h>
     59   1.1  jmcneill #include <dev/ic/bcmgenetvar.h>
     60   1.1  jmcneill 
     61   1.1  jmcneill CTASSERT(MCLBYTES == 2048);
     62   1.1  jmcneill 
     63   1.1  jmcneill #ifdef GENET_DEBUG
     64   1.1  jmcneill #define	DPRINTF(...)	printf(##__VA_ARGS__)
     65   1.1  jmcneill #else
     66   1.1  jmcneill #define	DPRINTF(...)	((void)0)
     67   1.1  jmcneill #endif
     68   1.1  jmcneill 
     69   1.1  jmcneill #define	TX_MAX_SEGS		128
     70   1.8   mlelstv #define	TX_DESC_COUNT		256 /* GENET_DMA_DESC_COUNT */
     71   1.8   mlelstv #define	RX_DESC_COUNT		256 /* GENET_DMA_DESC_COUNT */
     72   1.1  jmcneill #define	MII_BUSY_RETRY		1000
     73   1.2  jmcneill #define	GENET_MAX_MDF_FILTER	17
     74   1.1  jmcneill 
     75   1.8   mlelstv #define	TX_SKIP(n, o)		(((n) + (o)) % TX_DESC_COUNT)
     76   1.8   mlelstv #define	TX_NEXT(n)		TX_SKIP(n, 1)
     77   1.8   mlelstv #define	RX_NEXT(n)		(((n) + 1) % RX_DESC_COUNT)
     78   1.8   mlelstv 
     79  1.20     skrll #define	GENET_LOCK(sc)			mutex_enter(&(sc)->sc_lock)
     80  1.20     skrll #define	GENET_UNLOCK(sc)		mutex_exit(&(sc)->sc_lock)
     81  1.20     skrll #define	GENET_ASSERT_LOCKED(sc)		KASSERT(mutex_owned(&(sc)->sc_lock))
     82   1.1  jmcneill 
     83   1.8   mlelstv #define	GENET_TXLOCK(sc)		mutex_enter(&(sc)->sc_txlock)
     84   1.8   mlelstv #define	GENET_TXUNLOCK(sc)		mutex_exit(&(sc)->sc_txlock)
     85   1.8   mlelstv #define	GENET_ASSERT_TXLOCKED(sc)	KASSERT(mutex_owned(&(sc)->sc_txlock))
     86   1.8   mlelstv 
     87   1.1  jmcneill #define	RD4(sc, reg)			\
     88   1.1  jmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
     89   1.1  jmcneill #define	WR4(sc, reg, val)		\
     90   1.1  jmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
     91   1.1  jmcneill 
     92   1.1  jmcneill static int
     93   1.1  jmcneill genet_mii_readreg(device_t dev, int phy, int reg, uint16_t *val)
     94   1.1  jmcneill {
     95   1.1  jmcneill 	struct genet_softc *sc = device_private(dev);
     96   1.1  jmcneill 	int retry;
     97   1.1  jmcneill 
     98   1.1  jmcneill 	WR4(sc, GENET_MDIO_CMD,
     99   1.1  jmcneill 	    GENET_MDIO_READ | GENET_MDIO_START_BUSY |
    100   1.1  jmcneill 	    __SHIFTIN(phy, GENET_MDIO_PMD) |
    101   1.1  jmcneill 	    __SHIFTIN(reg, GENET_MDIO_REG));
    102   1.1  jmcneill 	for (retry = MII_BUSY_RETRY; retry > 0; retry--) {
    103   1.1  jmcneill 		if ((RD4(sc, GENET_MDIO_CMD) & GENET_MDIO_START_BUSY) == 0) {
    104   1.1  jmcneill 			*val = RD4(sc, GENET_MDIO_CMD) & 0xffff;
    105   1.1  jmcneill 			break;
    106   1.1  jmcneill 		}
    107   1.1  jmcneill 		delay(10);
    108   1.1  jmcneill 	}
    109   1.1  jmcneill 
    110   1.1  jmcneill 	if (retry == 0) {
    111   1.1  jmcneill 		device_printf(dev, "phy read timeout, phy=%d reg=%d\n",
    112   1.1  jmcneill 		    phy, reg);
    113   1.1  jmcneill 		return ETIMEDOUT;
    114   1.1  jmcneill 	}
    115   1.1  jmcneill 
    116   1.1  jmcneill 	return 0;
    117   1.1  jmcneill }
    118   1.1  jmcneill 
    119   1.1  jmcneill static int
    120   1.1  jmcneill genet_mii_writereg(device_t dev, int phy, int reg, uint16_t val)
    121   1.1  jmcneill {
    122   1.1  jmcneill 	struct genet_softc *sc = device_private(dev);
    123   1.1  jmcneill 	int retry;
    124   1.1  jmcneill 
    125   1.1  jmcneill 	WR4(sc, GENET_MDIO_CMD,
    126   1.1  jmcneill 	    val | GENET_MDIO_WRITE | GENET_MDIO_START_BUSY |
    127   1.1  jmcneill 	    __SHIFTIN(phy, GENET_MDIO_PMD) |
    128   1.1  jmcneill 	    __SHIFTIN(reg, GENET_MDIO_REG));
    129   1.1  jmcneill 	for (retry = MII_BUSY_RETRY; retry > 0; retry--) {
    130   1.1  jmcneill 		if ((RD4(sc, GENET_MDIO_CMD) & GENET_MDIO_START_BUSY) == 0)
    131   1.1  jmcneill 			break;
    132   1.1  jmcneill 		delay(10);
    133   1.1  jmcneill 	}
    134   1.1  jmcneill 
    135   1.1  jmcneill 	if (retry == 0) {
    136   1.1  jmcneill 		device_printf(dev, "phy write timeout, phy=%d reg=%d\n",
    137   1.1  jmcneill 		    phy, reg);
    138   1.1  jmcneill 		return ETIMEDOUT;
    139   1.1  jmcneill 	}
    140   1.1  jmcneill 
    141   1.1  jmcneill 	return 0;
    142   1.1  jmcneill }
    143   1.1  jmcneill 
    144   1.1  jmcneill static void
    145   1.1  jmcneill genet_update_link(struct genet_softc *sc)
    146   1.1  jmcneill {
    147   1.1  jmcneill 	struct mii_data *mii = &sc->sc_mii;
    148   1.1  jmcneill 	uint32_t val;
    149   1.1  jmcneill 	u_int speed;
    150   1.1  jmcneill 
    151   1.1  jmcneill 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
    152   1.1  jmcneill 	    IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX)
    153   1.1  jmcneill 		speed = GENET_UMAC_CMD_SPEED_1000;
    154   1.1  jmcneill 	else if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX)
    155   1.1  jmcneill 		speed = GENET_UMAC_CMD_SPEED_100;
    156   1.1  jmcneill 	else
    157   1.1  jmcneill 		speed = GENET_UMAC_CMD_SPEED_10;
    158   1.1  jmcneill 
    159   1.1  jmcneill 	val = RD4(sc, GENET_EXT_RGMII_OOB_CTRL);
    160   1.1  jmcneill 	val &= ~GENET_EXT_RGMII_OOB_OOB_DISABLE;
    161   1.1  jmcneill 	val |= GENET_EXT_RGMII_OOB_RGMII_LINK;
    162   1.1  jmcneill 	val |= GENET_EXT_RGMII_OOB_RGMII_MODE_EN;
    163   1.1  jmcneill 	if (sc->sc_phy_mode == GENET_PHY_MODE_RGMII)
    164   1.1  jmcneill 		val |= GENET_EXT_RGMII_OOB_ID_MODE_DISABLE;
    165   1.6  jmcneill 	else
    166   1.6  jmcneill 		val &= ~GENET_EXT_RGMII_OOB_ID_MODE_DISABLE;
    167   1.1  jmcneill 	WR4(sc, GENET_EXT_RGMII_OOB_CTRL, val);
    168   1.1  jmcneill 
    169   1.1  jmcneill 	val = RD4(sc, GENET_UMAC_CMD);
    170   1.1  jmcneill 	val &= ~GENET_UMAC_CMD_SPEED;
    171   1.1  jmcneill 	val |= __SHIFTIN(speed, GENET_UMAC_CMD_SPEED);
    172   1.1  jmcneill 	WR4(sc, GENET_UMAC_CMD, val);
    173   1.1  jmcneill }
    174   1.1  jmcneill 
    175   1.1  jmcneill static void
    176   1.1  jmcneill genet_mii_statchg(struct ifnet *ifp)
    177   1.1  jmcneill {
    178   1.1  jmcneill 	struct genet_softc * const sc = ifp->if_softc;
    179   1.1  jmcneill 
    180   1.1  jmcneill 	genet_update_link(sc);
    181   1.1  jmcneill }
    182   1.1  jmcneill 
    183   1.1  jmcneill static void
    184   1.1  jmcneill genet_setup_txdesc(struct genet_softc *sc, int index, int flags,
    185   1.1  jmcneill     bus_addr_t paddr, u_int len)
    186   1.1  jmcneill {
    187   1.1  jmcneill 	uint32_t status;
    188   1.1  jmcneill 
    189   1.1  jmcneill 	status = flags | __SHIFTIN(len, GENET_TX_DESC_STATUS_BUFLEN);
    190   1.1  jmcneill 
    191   1.1  jmcneill 	WR4(sc, GENET_TX_DESC_ADDRESS_LO(index), (uint32_t)paddr);
    192   1.1  jmcneill 	WR4(sc, GENET_TX_DESC_ADDRESS_HI(index), (uint32_t)(paddr >> 32));
    193   1.1  jmcneill 	WR4(sc, GENET_TX_DESC_STATUS(index), status);
    194   1.1  jmcneill }
    195   1.1  jmcneill 
    196   1.1  jmcneill static int
    197   1.1  jmcneill genet_setup_txbuf(struct genet_softc *sc, int index, struct mbuf *m)
    198   1.1  jmcneill {
    199   1.1  jmcneill 	bus_dma_segment_t *segs;
    200   1.1  jmcneill 	int error, nsegs, cur, i;
    201   1.1  jmcneill 	uint32_t flags;
    202   1.8   mlelstv 	bool nospace;
    203   1.8   mlelstv 
    204   1.8   mlelstv 	/* at least one descriptor free ? */
    205   1.8   mlelstv 	if (sc->sc_tx.queued >= TX_DESC_COUNT - 1)
    206   1.8   mlelstv 		return -1;
    207   1.1  jmcneill 
    208   1.1  jmcneill 	error = bus_dmamap_load_mbuf(sc->sc_tx.buf_tag,
    209   1.1  jmcneill 	    sc->sc_tx.buf_map[index].map, m, BUS_DMA_WRITE | BUS_DMA_NOWAIT);
    210   1.1  jmcneill 	if (error == EFBIG) {
    211   1.1  jmcneill 		device_printf(sc->sc_dev,
    212   1.1  jmcneill 		    "TX packet needs too many DMA segments, dropping...\n");
    213   1.8   mlelstv 		return -2;
    214   1.8   mlelstv 	}
    215   1.8   mlelstv 	if (error != 0) {
    216   1.8   mlelstv 		device_printf(sc->sc_dev,
    217   1.8   mlelstv 		    "TX packet cannot be mapped, retried...\n");
    218   1.1  jmcneill 		return 0;
    219   1.1  jmcneill 	}
    220   1.1  jmcneill 
    221   1.1  jmcneill 	segs = sc->sc_tx.buf_map[index].map->dm_segs;
    222   1.1  jmcneill 	nsegs = sc->sc_tx.buf_map[index].map->dm_nsegs;
    223   1.1  jmcneill 
    224   1.8   mlelstv 	nospace = sc->sc_tx.queued >= TX_DESC_COUNT - nsegs;
    225   1.8   mlelstv 	if (nospace) {
    226   1.1  jmcneill 		bus_dmamap_unload(sc->sc_tx.buf_tag,
    227   1.1  jmcneill 		    sc->sc_tx.buf_map[index].map);
    228   1.8   mlelstv 		/* XXX coalesce and retry ? */
    229   1.1  jmcneill 		return -1;
    230   1.1  jmcneill 	}
    231   1.1  jmcneill 
    232   1.8   mlelstv 	bus_dmamap_sync(sc->sc_tx.buf_tag, sc->sc_tx.buf_map[index].map,
    233   1.8   mlelstv 	    0, sc->sc_tx.buf_map[index].map->dm_mapsize, BUS_DMASYNC_PREWRITE);
    234   1.8   mlelstv 
    235   1.8   mlelstv 	/* stored in same index as loaded map */
    236   1.8   mlelstv 	sc->sc_tx.buf_map[index].mbuf = m;
    237   1.8   mlelstv 
    238   1.1  jmcneill 	flags = GENET_TX_DESC_STATUS_SOP |
    239   1.1  jmcneill 		GENET_TX_DESC_STATUS_CRC |
    240   1.1  jmcneill 		GENET_TX_DESC_STATUS_QTAG;
    241   1.1  jmcneill 
    242   1.1  jmcneill 	for (cur = index, i = 0; i < nsegs; i++) {
    243   1.1  jmcneill 		if (i == nsegs - 1)
    244   1.1  jmcneill 			flags |= GENET_TX_DESC_STATUS_EOP;
    245   1.1  jmcneill 
    246   1.1  jmcneill 		genet_setup_txdesc(sc, cur, flags, segs[i].ds_addr,
    247   1.1  jmcneill 		    segs[i].ds_len);
    248   1.1  jmcneill 
    249   1.8   mlelstv 		if (i == 0)
    250   1.1  jmcneill 			flags &= ~GENET_TX_DESC_STATUS_SOP;
    251   1.1  jmcneill 		cur = TX_NEXT(cur);
    252   1.1  jmcneill 	}
    253   1.1  jmcneill 
    254   1.1  jmcneill 	return nsegs;
    255   1.1  jmcneill }
    256   1.1  jmcneill 
    257   1.1  jmcneill static void
    258   1.1  jmcneill genet_setup_rxdesc(struct genet_softc *sc, int index,
    259   1.1  jmcneill     bus_addr_t paddr, bus_size_t len)
    260   1.1  jmcneill {
    261   1.1  jmcneill 	WR4(sc, GENET_RX_DESC_ADDRESS_LO(index), (uint32_t)paddr);
    262   1.1  jmcneill 	WR4(sc, GENET_RX_DESC_ADDRESS_HI(index), (uint32_t)(paddr >> 32));
    263   1.1  jmcneill }
    264   1.1  jmcneill 
    265   1.1  jmcneill static int
    266   1.1  jmcneill genet_setup_rxbuf(struct genet_softc *sc, int index, struct mbuf *m)
    267   1.1  jmcneill {
    268   1.1  jmcneill 	int error;
    269   1.1  jmcneill 
    270   1.1  jmcneill 	error = bus_dmamap_load_mbuf(sc->sc_rx.buf_tag,
    271   1.1  jmcneill 	    sc->sc_rx.buf_map[index].map, m, BUS_DMA_READ | BUS_DMA_NOWAIT);
    272   1.1  jmcneill 	if (error != 0)
    273   1.1  jmcneill 		return error;
    274   1.1  jmcneill 
    275   1.1  jmcneill 	bus_dmamap_sync(sc->sc_rx.buf_tag, sc->sc_rx.buf_map[index].map,
    276   1.1  jmcneill 	    0, sc->sc_rx.buf_map[index].map->dm_mapsize,
    277   1.1  jmcneill 	    BUS_DMASYNC_PREREAD);
    278   1.1  jmcneill 
    279   1.1  jmcneill 	sc->sc_rx.buf_map[index].mbuf = m;
    280   1.1  jmcneill 	genet_setup_rxdesc(sc, index,
    281   1.1  jmcneill 	    sc->sc_rx.buf_map[index].map->dm_segs[0].ds_addr,
    282   1.1  jmcneill 	    sc->sc_rx.buf_map[index].map->dm_segs[0].ds_len);
    283   1.1  jmcneill 
    284   1.1  jmcneill 	return 0;
    285   1.1  jmcneill }
    286   1.1  jmcneill 
    287   1.1  jmcneill static struct mbuf *
    288   1.1  jmcneill genet_alloc_mbufcl(struct genet_softc *sc)
    289   1.1  jmcneill {
    290   1.1  jmcneill 	struct mbuf *m;
    291   1.1  jmcneill 
    292   1.1  jmcneill 	m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
    293   1.1  jmcneill 	if (m != NULL)
    294   1.1  jmcneill 		m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
    295   1.1  jmcneill 
    296   1.1  jmcneill 	return m;
    297   1.1  jmcneill }
    298   1.1  jmcneill 
    299   1.1  jmcneill static void
    300   1.1  jmcneill genet_enable_intr(struct genet_softc *sc)
    301   1.1  jmcneill {
    302   1.1  jmcneill 	WR4(sc, GENET_INTRL2_CPU_CLEAR_MASK,
    303   1.1  jmcneill 	    GENET_IRQ_TXDMA_DONE | GENET_IRQ_RXDMA_DONE);
    304   1.1  jmcneill }
    305   1.1  jmcneill 
    306   1.1  jmcneill static void
    307   1.1  jmcneill genet_disable_intr(struct genet_softc *sc)
    308   1.1  jmcneill {
    309   1.1  jmcneill 	/* Disable interrupts */
    310   1.1  jmcneill 	WR4(sc, GENET_INTRL2_CPU_SET_MASK, 0xffffffff);
    311   1.1  jmcneill 	WR4(sc, GENET_INTRL2_CPU_CLEAR, 0xffffffff);
    312   1.1  jmcneill }
    313   1.1  jmcneill 
    314   1.1  jmcneill static void
    315   1.1  jmcneill genet_tick(void *softc)
    316   1.1  jmcneill {
    317   1.1  jmcneill 	struct genet_softc *sc = softc;
    318   1.1  jmcneill 	struct mii_data *mii = &sc->sc_mii;
    319   1.1  jmcneill 
    320   1.1  jmcneill 	GENET_LOCK(sc);
    321   1.1  jmcneill 	mii_tick(mii);
    322  1.21     skrll 	if ((sc->sc_if_flags & IFF_RUNNING) != 0)
    323  1.20     skrll 		callout_schedule(&sc->sc_stat_ch, hz);
    324   1.1  jmcneill 	GENET_UNLOCK(sc);
    325   1.1  jmcneill }
    326   1.1  jmcneill 
    327   1.1  jmcneill static void
    328   1.2  jmcneill genet_setup_rxfilter_mdf(struct genet_softc *sc, u_int n, const uint8_t *ea)
    329   1.2  jmcneill {
    330   1.2  jmcneill 	uint32_t addr0 = (ea[0] << 8) | ea[1];
    331   1.2  jmcneill 	uint32_t addr1 = (ea[2] << 24) | (ea[3] << 16) | (ea[4] << 8) | ea[5];
    332   1.2  jmcneill 
    333   1.2  jmcneill 	WR4(sc, GENET_UMAC_MDF_ADDR0(n), addr0);
    334   1.2  jmcneill 	WR4(sc, GENET_UMAC_MDF_ADDR1(n), addr1);
    335   1.2  jmcneill }
    336   1.2  jmcneill 
    337   1.2  jmcneill static void
    338   1.1  jmcneill genet_setup_rxfilter(struct genet_softc *sc)
    339   1.1  jmcneill {
    340   1.2  jmcneill 	struct ethercom *ec = &sc->sc_ec;
    341   1.2  jmcneill 	struct ifnet *ifp = &ec->ec_if;
    342   1.2  jmcneill 	struct ether_multistep step;
    343   1.2  jmcneill 	struct ether_multi *enm;
    344   1.2  jmcneill 	uint32_t cmd, mdf_ctrl;
    345   1.2  jmcneill 	u_int n;
    346   1.1  jmcneill 
    347   1.1  jmcneill 	GENET_ASSERT_LOCKED(sc);
    348   1.1  jmcneill 
    349   1.2  jmcneill 	ETHER_LOCK(ec);
    350   1.2  jmcneill 
    351   1.2  jmcneill 	cmd = RD4(sc, GENET_UMAC_CMD);
    352   1.2  jmcneill 
    353   1.2  jmcneill 	/*
    354   1.2  jmcneill 	 * Count the required number of hardware filters. We need one
    355   1.2  jmcneill 	 * for each multicast address, plus one for our own address and
    356   1.2  jmcneill 	 * the broadcast address.
    357   1.2  jmcneill 	 */
    358   1.2  jmcneill 	ETHER_FIRST_MULTI(step, ec, enm);
    359   1.2  jmcneill 	for (n = 2; enm != NULL; n++)
    360   1.2  jmcneill 		ETHER_NEXT_MULTI(step, enm);
    361   1.2  jmcneill 
    362   1.2  jmcneill 	if (n > GENET_MAX_MDF_FILTER)
    363  1.22     skrll 		ec->ec_flags |= ETHER_F_ALLMULTI;
    364   1.2  jmcneill 	else
    365  1.22     skrll 		ec->ec_flags &= ~ETHER_F_ALLMULTI;
    366   1.2  jmcneill 
    367  1.22     skrll 	if ((sc->sc_if_flags & IFF_PROMISC) != 0) {
    368  1.22     skrll 		ec->ec_flags |= ETHER_F_ALLMULTI;
    369   1.2  jmcneill 		cmd |= GENET_UMAC_CMD_PROMISC;
    370   1.2  jmcneill 		mdf_ctrl = 0;
    371   1.2  jmcneill 	} else {
    372   1.2  jmcneill 		cmd &= ~GENET_UMAC_CMD_PROMISC;
    373   1.2  jmcneill 		genet_setup_rxfilter_mdf(sc, 0, ifp->if_broadcastaddr);
    374   1.2  jmcneill 		genet_setup_rxfilter_mdf(sc, 1, CLLADDR(ifp->if_sadl));
    375   1.2  jmcneill 		ETHER_FIRST_MULTI(step, ec, enm);
    376   1.2  jmcneill 		for (n = 2; enm != NULL; n++) {
    377   1.2  jmcneill 			genet_setup_rxfilter_mdf(sc, n, enm->enm_addrlo);
    378   1.2  jmcneill 			ETHER_NEXT_MULTI(step, enm);
    379   1.2  jmcneill 		}
    380   1.2  jmcneill 		mdf_ctrl = __BITS(GENET_MAX_MDF_FILTER - 1,
    381   1.2  jmcneill 				  GENET_MAX_MDF_FILTER - n);
    382   1.2  jmcneill 	}
    383   1.2  jmcneill 
    384   1.2  jmcneill 	WR4(sc, GENET_UMAC_CMD, cmd);
    385   1.2  jmcneill 	WR4(sc, GENET_UMAC_MDF_CTRL, mdf_ctrl);
    386   1.1  jmcneill 
    387   1.2  jmcneill 	ETHER_UNLOCK(ec);
    388   1.1  jmcneill }
    389   1.1  jmcneill 
    390   1.1  jmcneill static int
    391   1.1  jmcneill genet_reset(struct genet_softc *sc)
    392   1.1  jmcneill {
    393   1.1  jmcneill 	uint32_t val;
    394   1.1  jmcneill 
    395   1.1  jmcneill 	val = RD4(sc, GENET_SYS_RBUF_FLUSH_CTRL);
    396   1.1  jmcneill 	val |= GENET_SYS_RBUF_FLUSH_RESET;
    397   1.1  jmcneill 	WR4(sc, GENET_SYS_RBUF_FLUSH_CTRL, val);
    398   1.1  jmcneill 	delay(10);
    399   1.1  jmcneill 
    400   1.1  jmcneill 	val &= ~GENET_SYS_RBUF_FLUSH_RESET;
    401   1.1  jmcneill 	WR4(sc, GENET_SYS_RBUF_FLUSH_CTRL, val);
    402   1.1  jmcneill 	delay(10);
    403   1.1  jmcneill 
    404   1.1  jmcneill 	WR4(sc, GENET_SYS_RBUF_FLUSH_CTRL, 0);
    405   1.1  jmcneill 	delay(10);
    406   1.1  jmcneill 
    407   1.1  jmcneill 	WR4(sc, GENET_UMAC_CMD, 0);
    408   1.1  jmcneill 	WR4(sc, GENET_UMAC_CMD,
    409   1.1  jmcneill 	    GENET_UMAC_CMD_LCL_LOOP_EN | GENET_UMAC_CMD_SW_RESET);
    410   1.1  jmcneill 	delay(10);
    411   1.1  jmcneill 	WR4(sc, GENET_UMAC_CMD, 0);
    412   1.1  jmcneill 
    413   1.1  jmcneill 	WR4(sc, GENET_UMAC_MIB_CTRL, GENET_UMAC_MIB_RESET_RUNT |
    414   1.1  jmcneill 	    GENET_UMAC_MIB_RESET_RX | GENET_UMAC_MIB_RESET_TX);
    415   1.1  jmcneill 	WR4(sc, GENET_UMAC_MIB_CTRL, 0);
    416   1.1  jmcneill 
    417   1.1  jmcneill 	WR4(sc, GENET_UMAC_MAX_FRAME_LEN, 1536);
    418   1.1  jmcneill 
    419   1.1  jmcneill 	val = RD4(sc, GENET_RBUF_CTRL);
    420   1.1  jmcneill 	val |= GENET_RBUF_ALIGN_2B;
    421   1.1  jmcneill 	WR4(sc, GENET_RBUF_CTRL, val);
    422   1.1  jmcneill 
    423   1.1  jmcneill 	WR4(sc, GENET_RBUF_TBUF_SIZE_CTRL, 1);
    424   1.1  jmcneill 
    425   1.1  jmcneill 	return 0;
    426   1.1  jmcneill }
    427   1.1  jmcneill 
    428   1.1  jmcneill static void
    429   1.8   mlelstv genet_set_rxthresh(struct genet_softc *sc, int qid, int usecs, int count)
    430   1.8   mlelstv {
    431   1.8   mlelstv 	int ticks;
    432   1.8   mlelstv 	uint32_t val;
    433   1.8   mlelstv 
    434   1.8   mlelstv 	/* convert to 125MHz/1024 ticks */
    435   1.8   mlelstv 	ticks = howmany(usecs * 125, 1024);
    436   1.8   mlelstv 
    437   1.8   mlelstv 	if (count < 1)
    438   1.8   mlelstv 		count = 1;
    439   1.8   mlelstv 	if (count > GENET_INTR_THRESHOLD_MASK)
    440   1.8   mlelstv 		count = GENET_INTR_THRESHOLD_MASK;
    441   1.8   mlelstv 	if (ticks < 0)
    442   1.8   mlelstv 		ticks = 0;
    443   1.8   mlelstv 	if (ticks > GENET_DMA_RING_TIMEOUT_MASK)
    444   1.8   mlelstv 		ticks = GENET_DMA_RING_TIMEOUT_MASK;
    445   1.8   mlelstv 
    446   1.8   mlelstv 	WR4(sc, GENET_RX_DMA_MBUF_DONE_THRES(qid), count);
    447   1.8   mlelstv 
    448   1.8   mlelstv 	val = RD4(sc, GENET_RX_DMA_RING_TIMEOUT(qid));
    449   1.8   mlelstv 	val &= ~GENET_DMA_RING_TIMEOUT_MASK;
    450   1.8   mlelstv 	val |= ticks;
    451   1.8   mlelstv 	WR4(sc, GENET_RX_DMA_RING_TIMEOUT(qid), val);
    452   1.8   mlelstv }
    453   1.8   mlelstv 
    454   1.8   mlelstv static void
    455   1.8   mlelstv genet_set_txthresh(struct genet_softc *sc, int qid, int count)
    456   1.8   mlelstv {
    457   1.8   mlelstv 	if (count < 1)
    458   1.8   mlelstv 		count = 1;
    459   1.8   mlelstv 	if (count > GENET_INTR_THRESHOLD_MASK)
    460   1.8   mlelstv 		count = GENET_INTR_THRESHOLD_MASK;
    461   1.8   mlelstv 
    462   1.8   mlelstv 	WR4(sc, GENET_TX_DMA_MBUF_DONE_THRES(qid), count);
    463   1.8   mlelstv }
    464   1.8   mlelstv 
    465   1.8   mlelstv static void
    466   1.1  jmcneill genet_init_rings(struct genet_softc *sc, int qid)
    467   1.1  jmcneill {
    468   1.1  jmcneill 	uint32_t val;
    469   1.1  jmcneill 
    470   1.1  jmcneill 	/* TX ring */
    471   1.1  jmcneill 
    472   1.1  jmcneill 	sc->sc_tx.queued = 0;
    473   1.1  jmcneill 	sc->sc_tx.cidx = sc->sc_tx.pidx = 0;
    474   1.1  jmcneill 
    475   1.1  jmcneill 	WR4(sc, GENET_TX_SCB_BURST_SIZE, 0x08);
    476   1.1  jmcneill 
    477   1.1  jmcneill 	WR4(sc, GENET_TX_DMA_READ_PTR_LO(qid), 0);
    478   1.1  jmcneill 	WR4(sc, GENET_TX_DMA_READ_PTR_HI(qid), 0);
    479   1.1  jmcneill 	WR4(sc, GENET_TX_DMA_CONS_INDEX(qid), 0);
    480   1.1  jmcneill 	WR4(sc, GENET_TX_DMA_PROD_INDEX(qid), 0);
    481   1.1  jmcneill 	WR4(sc, GENET_TX_DMA_RING_BUF_SIZE(qid),
    482   1.1  jmcneill 	    __SHIFTIN(TX_DESC_COUNT, GENET_TX_DMA_RING_BUF_SIZE_DESC_COUNT) |
    483   1.1  jmcneill 	    __SHIFTIN(MCLBYTES, GENET_TX_DMA_RING_BUF_SIZE_BUF_LENGTH));
    484   1.1  jmcneill 	WR4(sc, GENET_TX_DMA_START_ADDR_LO(qid), 0);
    485   1.1  jmcneill 	WR4(sc, GENET_TX_DMA_START_ADDR_HI(qid), 0);
    486   1.1  jmcneill 	WR4(sc, GENET_TX_DMA_END_ADDR_LO(qid),
    487   1.1  jmcneill 	    TX_DESC_COUNT * GENET_DMA_DESC_SIZE / 4 - 1);
    488   1.1  jmcneill 	WR4(sc, GENET_TX_DMA_END_ADDR_HI(qid), 0);
    489   1.1  jmcneill 	WR4(sc, GENET_TX_DMA_FLOW_PERIOD(qid), 0);
    490   1.1  jmcneill 	WR4(sc, GENET_TX_DMA_WRITE_PTR_LO(qid), 0);
    491   1.1  jmcneill 	WR4(sc, GENET_TX_DMA_WRITE_PTR_HI(qid), 0);
    492   1.1  jmcneill 
    493   1.8   mlelstv 	/* interrupt after 10 packets or when ring empty */
    494   1.8   mlelstv 	genet_set_txthresh(sc, qid, 10);
    495   1.8   mlelstv 
    496   1.1  jmcneill 	WR4(sc, GENET_TX_DMA_RING_CFG, __BIT(qid));	/* enable */
    497   1.1  jmcneill 
    498   1.1  jmcneill 	/* Enable transmit DMA */
    499   1.1  jmcneill 	val = RD4(sc, GENET_TX_DMA_CTRL);
    500   1.1  jmcneill 	val |= GENET_TX_DMA_CTRL_EN;
    501   1.1  jmcneill 	val |= GENET_TX_DMA_CTRL_RBUF_EN(GENET_DMA_DEFAULT_QUEUE);
    502   1.1  jmcneill 	WR4(sc, GENET_TX_DMA_CTRL, val);
    503   1.1  jmcneill 
    504   1.1  jmcneill 	/* RX ring */
    505   1.1  jmcneill 
    506   1.1  jmcneill 	sc->sc_rx.cidx = sc->sc_rx.pidx = 0;
    507   1.1  jmcneill 
    508   1.1  jmcneill 	WR4(sc, GENET_RX_SCB_BURST_SIZE, 0x08);
    509   1.1  jmcneill 
    510   1.1  jmcneill 	WR4(sc, GENET_RX_DMA_WRITE_PTR_LO(qid), 0);
    511   1.1  jmcneill 	WR4(sc, GENET_RX_DMA_WRITE_PTR_HI(qid), 0);
    512   1.1  jmcneill 	WR4(sc, GENET_RX_DMA_PROD_INDEX(qid), 0);
    513   1.1  jmcneill 	WR4(sc, GENET_RX_DMA_CONS_INDEX(qid), 0);
    514   1.1  jmcneill 	WR4(sc, GENET_RX_DMA_RING_BUF_SIZE(qid),
    515   1.1  jmcneill 	    __SHIFTIN(RX_DESC_COUNT, GENET_RX_DMA_RING_BUF_SIZE_DESC_COUNT) |
    516   1.1  jmcneill 	    __SHIFTIN(MCLBYTES, GENET_RX_DMA_RING_BUF_SIZE_BUF_LENGTH));
    517   1.1  jmcneill 	WR4(sc, GENET_RX_DMA_START_ADDR_LO(qid), 0);
    518   1.1  jmcneill 	WR4(sc, GENET_RX_DMA_START_ADDR_HI(qid), 0);
    519   1.1  jmcneill 	WR4(sc, GENET_RX_DMA_END_ADDR_LO(qid),
    520   1.1  jmcneill 	    RX_DESC_COUNT * GENET_DMA_DESC_SIZE / 4 - 1);
    521   1.1  jmcneill 	WR4(sc, GENET_RX_DMA_END_ADDR_HI(qid), 0);
    522   1.1  jmcneill 	WR4(sc, GENET_RX_DMA_XON_XOFF_THRES(qid),
    523   1.1  jmcneill 	    __SHIFTIN(5, GENET_RX_DMA_XON_XOFF_THRES_LO) |
    524   1.1  jmcneill 	    __SHIFTIN(RX_DESC_COUNT >> 4, GENET_RX_DMA_XON_XOFF_THRES_HI));
    525   1.1  jmcneill 	WR4(sc, GENET_RX_DMA_READ_PTR_LO(qid), 0);
    526   1.1  jmcneill 	WR4(sc, GENET_RX_DMA_READ_PTR_HI(qid), 0);
    527   1.1  jmcneill 
    528   1.8   mlelstv 	/*
    529   1.8   mlelstv 	 * interrupt on first packet,
    530   1.8   mlelstv 	 * mitigation timeout timeout 57 us (~84 minimal packets at 1Gbit/s)
    531   1.8   mlelstv 	 */
    532   1.8   mlelstv 	genet_set_rxthresh(sc, qid, 57, 10);
    533   1.8   mlelstv 
    534   1.1  jmcneill 	WR4(sc, GENET_RX_DMA_RING_CFG, __BIT(qid));	/* enable */
    535   1.1  jmcneill 
    536   1.1  jmcneill 	/* Enable receive DMA */
    537   1.1  jmcneill 	val = RD4(sc, GENET_RX_DMA_CTRL);
    538   1.1  jmcneill 	val |= GENET_RX_DMA_CTRL_EN;
    539   1.1  jmcneill 	val |= GENET_RX_DMA_CTRL_RBUF_EN(GENET_DMA_DEFAULT_QUEUE);
    540   1.1  jmcneill 	WR4(sc, GENET_RX_DMA_CTRL, val);
    541   1.1  jmcneill }
    542   1.1  jmcneill 
    543   1.1  jmcneill static int
    544   1.1  jmcneill genet_init_locked(struct genet_softc *sc)
    545   1.1  jmcneill {
    546   1.1  jmcneill 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    547   1.1  jmcneill 	struct mii_data *mii = &sc->sc_mii;
    548   1.1  jmcneill 	uint32_t val;
    549   1.1  jmcneill 	const uint8_t *enaddr = CLLADDR(ifp->if_sadl);
    550   1.1  jmcneill 
    551   1.1  jmcneill 	GENET_ASSERT_LOCKED(sc);
    552   1.8   mlelstv 	GENET_ASSERT_TXLOCKED(sc);
    553   1.1  jmcneill 
    554   1.1  jmcneill 	if ((ifp->if_flags & IFF_RUNNING) != 0)
    555   1.1  jmcneill 		return 0;
    556   1.1  jmcneill 
    557   1.1  jmcneill 	if (sc->sc_phy_mode == GENET_PHY_MODE_RGMII ||
    558   1.6  jmcneill 	    sc->sc_phy_mode == GENET_PHY_MODE_RGMII_ID ||
    559   1.6  jmcneill 	    sc->sc_phy_mode == GENET_PHY_MODE_RGMII_RXID ||
    560   1.6  jmcneill 	    sc->sc_phy_mode == GENET_PHY_MODE_RGMII_TXID)
    561   1.1  jmcneill 		WR4(sc, GENET_SYS_PORT_CTRL,
    562   1.1  jmcneill 		    GENET_SYS_PORT_MODE_EXT_GPHY);
    563   1.6  jmcneill 	else
    564   1.6  jmcneill 		WR4(sc, GENET_SYS_PORT_CTRL, 0);
    565   1.1  jmcneill 
    566   1.1  jmcneill 	/* Write hardware address */
    567   1.2  jmcneill 	val = enaddr[3] | (enaddr[2] << 8) | (enaddr[1] << 16) |
    568   1.2  jmcneill 	    (enaddr[0] << 24);
    569   1.1  jmcneill 	WR4(sc, GENET_UMAC_MAC0, val);
    570   1.2  jmcneill 	val = enaddr[5] | (enaddr[4] << 8);
    571   1.1  jmcneill 	WR4(sc, GENET_UMAC_MAC1, val);
    572   1.1  jmcneill 
    573   1.1  jmcneill 	/* Setup RX filter */
    574  1.21     skrll 	sc->sc_if_flags = ifp->if_flags;
    575   1.1  jmcneill 	genet_setup_rxfilter(sc);
    576   1.1  jmcneill 
    577   1.1  jmcneill 	/* Setup TX/RX rings */
    578   1.1  jmcneill 	genet_init_rings(sc, GENET_DMA_DEFAULT_QUEUE);
    579   1.1  jmcneill 
    580   1.1  jmcneill 	/* Enable transmitter and receiver */
    581   1.1  jmcneill 	val = RD4(sc, GENET_UMAC_CMD);
    582   1.1  jmcneill 	val |= GENET_UMAC_CMD_TXEN;
    583   1.1  jmcneill 	val |= GENET_UMAC_CMD_RXEN;
    584   1.1  jmcneill 	WR4(sc, GENET_UMAC_CMD, val);
    585   1.1  jmcneill 
    586   1.1  jmcneill 	/* Enable interrupts */
    587   1.1  jmcneill 	genet_enable_intr(sc);
    588   1.1  jmcneill 
    589  1.20     skrll 	GENET_ASSERT_TXLOCKED(sc);
    590  1.20     skrll 	sc->sc_txrunning = true;
    591  1.20     skrll 
    592   1.1  jmcneill 	ifp->if_flags |= IFF_RUNNING;
    593  1.21     skrll 	sc->sc_if_flags |= IFF_RUNNING;
    594   1.1  jmcneill 
    595   1.1  jmcneill 	mii_mediachg(mii);
    596   1.1  jmcneill 	callout_schedule(&sc->sc_stat_ch, hz);
    597   1.1  jmcneill 
    598   1.1  jmcneill 	return 0;
    599   1.1  jmcneill }
    600   1.1  jmcneill 
    601   1.1  jmcneill static int
    602   1.1  jmcneill genet_init(struct ifnet *ifp)
    603   1.1  jmcneill {
    604   1.1  jmcneill 	struct genet_softc *sc = ifp->if_softc;
    605   1.1  jmcneill 	int error;
    606   1.1  jmcneill 
    607   1.1  jmcneill 	GENET_LOCK(sc);
    608   1.8   mlelstv 	GENET_TXLOCK(sc);
    609   1.1  jmcneill 	error = genet_init_locked(sc);
    610   1.8   mlelstv 	GENET_TXUNLOCK(sc);
    611   1.1  jmcneill 	GENET_UNLOCK(sc);
    612   1.1  jmcneill 
    613   1.1  jmcneill 	return error;
    614   1.1  jmcneill }
    615   1.1  jmcneill 
    616  1.18   mlelstv static int
    617  1.18   mlelstv genet_free_txbuf(struct genet_softc *sc, int index)
    618  1.18   mlelstv {
    619  1.18   mlelstv 	struct genet_bufmap *bmap;
    620  1.18   mlelstv 
    621  1.18   mlelstv 	bmap = &sc->sc_tx.buf_map[index];
    622  1.18   mlelstv 	if (bmap->mbuf == NULL)
    623  1.18   mlelstv 		return 0;
    624  1.18   mlelstv 
    625  1.18   mlelstv 	if (bmap->map->dm_mapsize > 0) {
    626  1.18   mlelstv 		bus_dmamap_sync(sc->sc_tx.buf_tag, bmap->map,
    627  1.18   mlelstv 		    0, bmap->map->dm_mapsize,
    628  1.18   mlelstv 		    BUS_DMASYNC_POSTWRITE);
    629  1.18   mlelstv 	}
    630  1.18   mlelstv 	bus_dmamap_unload(sc->sc_tx.buf_tag, bmap->map);
    631  1.18   mlelstv 	m_freem(bmap->mbuf);
    632  1.18   mlelstv 	bmap->mbuf = NULL;
    633  1.18   mlelstv 
    634  1.18   mlelstv 	return 1;
    635  1.18   mlelstv }
    636  1.18   mlelstv 
    637   1.1  jmcneill static void
    638   1.1  jmcneill genet_stop_locked(struct genet_softc *sc, int disable)
    639   1.1  jmcneill {
    640   1.1  jmcneill 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    641   1.1  jmcneill 	uint32_t val;
    642  1.18   mlelstv 	int i;
    643   1.1  jmcneill 
    644   1.1  jmcneill 	GENET_ASSERT_LOCKED(sc);
    645   1.1  jmcneill 
    646  1.20     skrll 	GENET_TXLOCK(sc);
    647  1.20     skrll 	sc->sc_txrunning = false;
    648  1.20     skrll 	GENET_TXUNLOCK(sc);
    649  1.20     skrll 
    650  1.20     skrll 	callout_halt(&sc->sc_stat_ch, &sc->sc_lock);
    651   1.1  jmcneill 
    652   1.1  jmcneill 	mii_down(&sc->sc_mii);
    653   1.1  jmcneill 
    654   1.1  jmcneill 	/* Disable receiver */
    655   1.1  jmcneill 	val = RD4(sc, GENET_UMAC_CMD);
    656   1.1  jmcneill 	val &= ~GENET_UMAC_CMD_RXEN;
    657   1.1  jmcneill 	WR4(sc, GENET_UMAC_CMD, val);
    658   1.1  jmcneill 
    659   1.1  jmcneill 	/* Stop receive DMA */
    660   1.1  jmcneill 	val = RD4(sc, GENET_RX_DMA_CTRL);
    661   1.1  jmcneill 	val &= ~GENET_RX_DMA_CTRL_EN;
    662   1.1  jmcneill 	WR4(sc, GENET_RX_DMA_CTRL, val);
    663   1.1  jmcneill 
    664   1.1  jmcneill 	/* Stop transmit DMA */
    665   1.1  jmcneill 	val = RD4(sc, GENET_TX_DMA_CTRL);
    666   1.1  jmcneill 	val &= ~GENET_TX_DMA_CTRL_EN;
    667   1.1  jmcneill 	WR4(sc, GENET_TX_DMA_CTRL, val);
    668   1.1  jmcneill 
    669   1.1  jmcneill 	/* Flush data in the TX FIFO */
    670   1.1  jmcneill 	WR4(sc, GENET_UMAC_TX_FLUSH, 1);
    671   1.1  jmcneill 	delay(10);
    672   1.1  jmcneill 	WR4(sc, GENET_UMAC_TX_FLUSH, 0);
    673   1.1  jmcneill 
    674   1.1  jmcneill 	/* Disable transmitter */
    675   1.1  jmcneill 	val = RD4(sc, GENET_UMAC_CMD);
    676   1.1  jmcneill 	val &= ~GENET_UMAC_CMD_TXEN;
    677   1.1  jmcneill 	WR4(sc, GENET_UMAC_CMD, val);
    678   1.1  jmcneill 
    679   1.1  jmcneill 	/* Disable interrupts */
    680   1.1  jmcneill 	genet_disable_intr(sc);
    681   1.1  jmcneill 
    682  1.18   mlelstv 	/* Free TX buffers */
    683  1.19   mlelstv 	for (i=0; i<TX_DESC_COUNT; ++i)
    684  1.18   mlelstv 		genet_free_txbuf(sc, i);
    685  1.18   mlelstv 
    686  1.21     skrll 	sc->sc_if_flags &= ~IFF_RUNNING;
    687  1.14   thorpej 	ifp->if_flags &= ~IFF_RUNNING;
    688   1.1  jmcneill }
    689   1.1  jmcneill 
    690   1.1  jmcneill static void
    691   1.1  jmcneill genet_stop(struct ifnet *ifp, int disable)
    692   1.1  jmcneill {
    693   1.1  jmcneill 	struct genet_softc * const sc = ifp->if_softc;
    694   1.1  jmcneill 
    695   1.1  jmcneill 	GENET_LOCK(sc);
    696   1.1  jmcneill 	genet_stop_locked(sc, disable);
    697   1.1  jmcneill 	GENET_UNLOCK(sc);
    698   1.1  jmcneill }
    699   1.1  jmcneill 
    700   1.1  jmcneill static void
    701   1.1  jmcneill genet_rxintr(struct genet_softc *sc, int qid)
    702   1.1  jmcneill {
    703   1.1  jmcneill 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    704   1.1  jmcneill 	int error, index, len, n;
    705   1.1  jmcneill 	struct mbuf *m, *m0;
    706   1.1  jmcneill 	uint32_t status, pidx, total;
    707   1.9       rin 	int pkts = 0;
    708   1.1  jmcneill 
    709   1.1  jmcneill 	pidx = RD4(sc, GENET_RX_DMA_PROD_INDEX(qid)) & 0xffff;
    710   1.1  jmcneill 	total = (pidx - sc->sc_rx.cidx) & 0xffff;
    711   1.1  jmcneill 
    712   1.1  jmcneill 	DPRINTF("RX pidx=%08x total=%d\n", pidx, total);
    713   1.1  jmcneill 
    714   1.8   mlelstv 	index = sc->sc_rx.cidx % RX_DESC_COUNT;
    715   1.1  jmcneill 	for (n = 0; n < total; n++) {
    716   1.1  jmcneill 		status = RD4(sc, GENET_RX_DESC_STATUS(index));
    717   1.8   mlelstv 
    718   1.8   mlelstv 		if (status & GENET_RX_DESC_STATUS_ALL_ERRS) {
    719   1.8   mlelstv 			if (status & GENET_RX_DESC_STATUS_OVRUN_ERR)
    720   1.8   mlelstv 				device_printf(sc->sc_dev, "overrun\n");
    721   1.8   mlelstv 			if (status & GENET_RX_DESC_STATUS_CRC_ERR)
    722   1.8   mlelstv 				device_printf(sc->sc_dev, "CRC error\n");
    723   1.8   mlelstv 			if (status & GENET_RX_DESC_STATUS_RX_ERR)
    724   1.8   mlelstv 				device_printf(sc->sc_dev, "receive error\n");
    725   1.8   mlelstv 			if (status & GENET_RX_DESC_STATUS_FRAME_ERR)
    726   1.8   mlelstv 				device_printf(sc->sc_dev, "frame error\n");
    727   1.8   mlelstv 			if (status & GENET_RX_DESC_STATUS_LEN_ERR)
    728   1.8   mlelstv 				device_printf(sc->sc_dev, "length error\n");
    729   1.8   mlelstv 			if_statinc(ifp, if_ierrors);
    730   1.8   mlelstv 			goto next;
    731   1.8   mlelstv 		}
    732   1.8   mlelstv 
    733   1.8   mlelstv 		if (status & GENET_RX_DESC_STATUS_OWN)
    734   1.8   mlelstv 			device_printf(sc->sc_dev, "OWN %d of %d\n",n,total);
    735   1.8   mlelstv 
    736   1.1  jmcneill 		len = __SHIFTOUT(status, GENET_RX_DESC_STATUS_BUFLEN);
    737   1.8   mlelstv 		if (len < ETHER_ALIGN) {
    738   1.8   mlelstv 			if_statinc(ifp, if_ierrors);
    739   1.8   mlelstv 			goto next;
    740   1.8   mlelstv 		}
    741   1.1  jmcneill 
    742   1.4  jmcneill 		m = sc->sc_rx.buf_map[index].mbuf;
    743   1.4  jmcneill 
    744   1.4  jmcneill 		if ((m0 = genet_alloc_mbufcl(sc)) == NULL) {
    745   1.4  jmcneill 			if_statinc(ifp, if_ierrors);
    746   1.4  jmcneill 			goto next;
    747   1.4  jmcneill 		}
    748  1.17   mlelstv 		MCLAIM(m0, &sc->sc_ec.ec_rx_mowner);
    749   1.8   mlelstv 
    750   1.8   mlelstv 		/* unload map before it gets loaded in setup_rxbuf */
    751  1.10  jmcneill 		if (sc->sc_rx.buf_map[index].map->dm_mapsize > 0) {
    752  1.17   mlelstv 			bus_dmamap_sync(sc->sc_rx.buf_tag,
    753  1.17   mlelstv 			    sc->sc_rx.buf_map[index].map,
    754  1.10  jmcneill 			    0, sc->sc_rx.buf_map[index].map->dm_mapsize,
    755  1.10  jmcneill 			    BUS_DMASYNC_POSTREAD);
    756  1.13   mlelstv 		}
    757   1.8   mlelstv 		bus_dmamap_unload(sc->sc_rx.buf_tag, sc->sc_rx.buf_map[index].map);
    758   1.8   mlelstv 		sc->sc_rx.buf_map[index].mbuf = NULL;
    759   1.8   mlelstv 
    760   1.4  jmcneill 		error = genet_setup_rxbuf(sc, index, m0);
    761   1.4  jmcneill 		if (error != 0) {
    762   1.8   mlelstv 			m_freem(m0);
    763   1.4  jmcneill 			if_statinc(ifp, if_ierrors);
    764   1.8   mlelstv 
    765   1.8   mlelstv 			/* XXX mbuf is unloaded but load failed */
    766   1.8   mlelstv 			m_freem(m);
    767   1.8   mlelstv 			device_printf(sc->sc_dev,
    768   1.8   mlelstv 			    "cannot load RX mbuf. panic?\n");
    769   1.4  jmcneill 			goto next;
    770   1.4  jmcneill 		}
    771   1.1  jmcneill 
    772   1.1  jmcneill 		DPRINTF("RX [#%d] index=%02x status=%08x len=%d adj_len=%d\n",
    773   1.1  jmcneill 		    n, index, status, len, len - ETHER_ALIGN);
    774   1.1  jmcneill 
    775   1.8   mlelstv 		m_set_rcvif(m, ifp);
    776   1.8   mlelstv 		m->m_len = m->m_pkthdr.len = len;
    777   1.8   mlelstv 		m_adj(m, ETHER_ALIGN);
    778   1.1  jmcneill 
    779   1.8   mlelstv 		if_percpuq_enqueue(ifp->if_percpuq, m);
    780   1.9       rin 		++pkts;
    781   1.1  jmcneill 
    782   1.4  jmcneill next:
    783   1.1  jmcneill 		index = RX_NEXT(index);
    784   1.1  jmcneill 
    785   1.1  jmcneill 		sc->sc_rx.cidx = (sc->sc_rx.cidx + 1) & 0xffff;
    786   1.1  jmcneill 		WR4(sc, GENET_RX_DMA_CONS_INDEX(qid), sc->sc_rx.cidx);
    787   1.1  jmcneill 	}
    788   1.9       rin 
    789   1.9       rin 	if (pkts != 0)
    790   1.9       rin 		rnd_add_uint32(&sc->sc_rndsource, pkts);
    791   1.1  jmcneill }
    792   1.1  jmcneill 
    793   1.1  jmcneill static void
    794   1.1  jmcneill genet_txintr(struct genet_softc *sc, int qid)
    795   1.1  jmcneill {
    796   1.1  jmcneill 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    797   1.8   mlelstv 	int cidx, i, pkts = 0;
    798   1.1  jmcneill 
    799   1.1  jmcneill 	cidx = RD4(sc, GENET_TX_DMA_CONS_INDEX(qid)) & 0xffff;
    800   1.8   mlelstv 	i = sc->sc_tx.cidx % TX_DESC_COUNT;
    801   1.8   mlelstv 	while (sc->sc_tx.cidx != cidx) {
    802  1.18   mlelstv 		pkts += genet_free_txbuf(sc, i);
    803   1.8   mlelstv 		i = TX_NEXT(i);
    804   1.8   mlelstv 		sc->sc_tx.cidx = (sc->sc_tx.cidx + 1) & 0xffff;
    805   1.1  jmcneill 	}
    806   1.1  jmcneill 
    807  1.18   mlelstv 	if (pkts != 0) {
    808  1.18   mlelstv 		if_statadd(ifp, if_opackets, pkts);
    809  1.18   mlelstv 		rnd_add_uint32(&sc->sc_rndsource, pkts);
    810  1.18   mlelstv 	}
    811   1.9       rin 
    812  1.18   mlelstv 	if_schedule_deferred_start(ifp);
    813   1.1  jmcneill }
    814   1.1  jmcneill 
    815   1.1  jmcneill static void
    816   1.1  jmcneill genet_start_locked(struct genet_softc *sc)
    817   1.1  jmcneill {
    818   1.1  jmcneill 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    819   1.1  jmcneill 	struct mbuf *m;
    820   1.1  jmcneill 	int nsegs, index, cnt;
    821   1.1  jmcneill 
    822   1.8   mlelstv 	GENET_ASSERT_TXLOCKED(sc);
    823   1.1  jmcneill 
    824  1.20     skrll 	if (!sc->sc_txrunning)
    825   1.1  jmcneill 		return;
    826   1.1  jmcneill 
    827   1.1  jmcneill 	const int qid = GENET_DMA_DEFAULT_QUEUE;
    828   1.1  jmcneill 
    829   1.8   mlelstv 	index = sc->sc_tx.pidx % TX_DESC_COUNT;
    830   1.1  jmcneill 	cnt = 0;
    831   1.1  jmcneill 
    832   1.8   mlelstv 	sc->sc_tx.queued = (RD4(sc, GENET_TX_DMA_PROD_INDEX(qid))
    833  1.18   mlelstv 	          - sc->sc_tx.cidx) & 0xffff;
    834  1.18   mlelstv 
    835  1.18   mlelstv 	/* At least one descriptor free ? */
    836  1.18   mlelstv 	if (sc->sc_tx.queued >= TX_DESC_COUNT - 1)
    837  1.18   mlelstv 		return;
    838   1.8   mlelstv 
    839   1.1  jmcneill 	for (;;) {
    840   1.1  jmcneill 		IFQ_POLL(&ifp->if_snd, m);
    841   1.1  jmcneill 		if (m == NULL)
    842   1.1  jmcneill 			break;
    843   1.1  jmcneill 
    844   1.1  jmcneill 		nsegs = genet_setup_txbuf(sc, index, m);
    845   1.1  jmcneill 		if (nsegs <= 0) {
    846  1.14   thorpej 			if (nsegs == -2) {
    847   1.8   mlelstv 				IFQ_DEQUEUE(&ifp->if_snd, m);
    848   1.8   mlelstv 				m_freem(m);
    849  1.14   thorpej 				continue;
    850   1.8   mlelstv 			}
    851   1.1  jmcneill 			break;
    852   1.1  jmcneill 		}
    853   1.8   mlelstv 
    854   1.1  jmcneill 		IFQ_DEQUEUE(&ifp->if_snd, m);
    855   1.1  jmcneill 		bpf_mtap(ifp, m, BPF_D_OUT);
    856   1.1  jmcneill 
    857   1.1  jmcneill 		index = TX_SKIP(index, nsegs);
    858   1.8   mlelstv 		sc->sc_tx.queued += nsegs;
    859   1.1  jmcneill 		sc->sc_tx.pidx = (sc->sc_tx.pidx + nsegs) & 0xffff;
    860   1.1  jmcneill 		cnt++;
    861   1.1  jmcneill 	}
    862   1.1  jmcneill 
    863   1.1  jmcneill 	if (cnt != 0)
    864   1.1  jmcneill 		WR4(sc, GENET_TX_DMA_PROD_INDEX(qid), sc->sc_tx.pidx);
    865   1.1  jmcneill }
    866   1.1  jmcneill 
    867   1.1  jmcneill static void
    868   1.1  jmcneill genet_start(struct ifnet *ifp)
    869   1.1  jmcneill {
    870   1.1  jmcneill 	struct genet_softc *sc = ifp->if_softc;
    871   1.1  jmcneill 
    872   1.8   mlelstv 	GENET_TXLOCK(sc);
    873   1.1  jmcneill 	genet_start_locked(sc);
    874   1.8   mlelstv 	GENET_TXUNLOCK(sc);
    875   1.1  jmcneill }
    876   1.1  jmcneill 
    877   1.1  jmcneill int
    878   1.1  jmcneill genet_intr(void *arg)
    879   1.1  jmcneill {
    880   1.1  jmcneill 	struct genet_softc *sc = arg;
    881   1.1  jmcneill 	uint32_t val;
    882   1.1  jmcneill 
    883   1.1  jmcneill 	val = RD4(sc, GENET_INTRL2_CPU_STAT);
    884   1.1  jmcneill 	val &= ~RD4(sc, GENET_INTRL2_CPU_STAT_MASK);
    885   1.1  jmcneill 	WR4(sc, GENET_INTRL2_CPU_CLEAR, val);
    886   1.1  jmcneill 
    887   1.8   mlelstv 	if (val & GENET_IRQ_RXDMA_DONE) {
    888   1.8   mlelstv 		GENET_LOCK(sc);
    889   1.1  jmcneill 		genet_rxintr(sc, GENET_DMA_DEFAULT_QUEUE);
    890   1.8   mlelstv 		GENET_UNLOCK(sc);
    891   1.8   mlelstv 	}
    892   1.1  jmcneill 
    893   1.1  jmcneill 	if (val & GENET_IRQ_TXDMA_DONE) {
    894   1.1  jmcneill 		genet_txintr(sc, GENET_DMA_DEFAULT_QUEUE);
    895   1.1  jmcneill 	}
    896   1.1  jmcneill 
    897   1.1  jmcneill 	return 1;
    898   1.1  jmcneill }
    899   1.1  jmcneill 
    900   1.1  jmcneill static int
    901   1.1  jmcneill genet_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    902   1.1  jmcneill {
    903   1.1  jmcneill 	struct genet_softc *sc = ifp->if_softc;
    904  1.20     skrll 	int error;
    905   1.1  jmcneill 
    906  1.21     skrll 	switch (cmd) {
    907  1.21     skrll 	case SIOCADDMULTI:
    908  1.21     skrll 	case SIOCDELMULTI:
    909  1.21     skrll 		break;
    910  1.21     skrll 	default:
    911  1.21     skrll 		KASSERT(IFNET_LOCKED(ifp));
    912  1.21     skrll 	}
    913  1.21     skrll 
    914  1.20     skrll 	const int s = splnet();
    915  1.20     skrll 	error = ether_ioctl(ifp, cmd, data);
    916  1.20     skrll 	splx(s);
    917   1.1  jmcneill 
    918  1.20     skrll 	if (error != ENETRESET)
    919  1.20     skrll 		return error;
    920   1.1  jmcneill 
    921  1.20     skrll 	error = 0;
    922   1.1  jmcneill 
    923  1.20     skrll 	if (cmd == SIOCSIFCAP)
    924  1.20     skrll 		error = if_init(ifp);
    925  1.20     skrll 	else if (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI) {
    926  1.20     skrll 		GENET_LOCK(sc);
    927  1.21     skrll 		if ((sc->sc_if_flags & IFF_RUNNING) != 0)
    928   1.1  jmcneill 			genet_setup_rxfilter(sc);
    929  1.20     skrll 		GENET_UNLOCK(sc);
    930   1.1  jmcneill 	}
    931   1.1  jmcneill 	return error;
    932   1.1  jmcneill }
    933   1.1  jmcneill 
    934  1.21     skrll static int
    935  1.21     skrll genet_ifflags_cb(struct ethercom *ec)
    936  1.21     skrll {
    937  1.21     skrll 	struct ifnet * const ifp = &ec->ec_if;
    938  1.21     skrll 	struct genet_softc * const sc = ifp->if_softc;
    939  1.21     skrll 	int ret = 0;
    940  1.21     skrll 
    941  1.21     skrll 	KASSERT(IFNET_LOCKED(ifp));
    942  1.21     skrll 	GENET_LOCK(sc);
    943  1.21     skrll 
    944  1.21     skrll 	u_short change = ifp->if_flags ^ sc->sc_if_flags;
    945  1.21     skrll 	sc->sc_if_flags = ifp->if_flags;
    946  1.21     skrll 
    947  1.21     skrll 	if ((change & ~(IFF_CANTCHANGE | IFF_DEBUG)) != 0) {
    948  1.21     skrll 		ret = ENETRESET;
    949  1.22     skrll 	} else if ((change & IFF_PROMISC) != 0) {
    950  1.21     skrll 		if ((sc->sc_if_flags & IFF_RUNNING) != 0)
    951  1.21     skrll 			genet_setup_rxfilter(sc);
    952  1.21     skrll 	}
    953  1.21     skrll 	GENET_UNLOCK(sc);
    954  1.21     skrll 
    955  1.21     skrll 	return ret;
    956  1.21     skrll }
    957  1.21     skrll 
    958   1.1  jmcneill static void
    959   1.1  jmcneill genet_get_eaddr(struct genet_softc *sc, uint8_t *eaddr)
    960   1.1  jmcneill {
    961   1.1  jmcneill 	prop_dictionary_t prop = device_properties(sc->sc_dev);
    962   1.5  jmcneill 	uint32_t maclo, machi, val;
    963   1.1  jmcneill 	prop_data_t eaprop;
    964   1.1  jmcneill 
    965   1.1  jmcneill 	eaprop = prop_dictionary_get(prop, "mac-address");
    966   1.5  jmcneill 	if (eaprop != NULL) {
    967   1.1  jmcneill 		KASSERT(prop_object_type(eaprop) == PROP_TYPE_DATA);
    968   1.1  jmcneill 		KASSERT(prop_data_size(eaprop) == ETHER_ADDR_LEN);
    969   1.7  jmcneill 		memcpy(eaddr, prop_data_value(eaprop),
    970   1.1  jmcneill 		    ETHER_ADDR_LEN);
    971   1.5  jmcneill 		return;
    972   1.5  jmcneill 	}
    973   1.5  jmcneill 
    974   1.5  jmcneill 	maclo = machi = 0;
    975   1.5  jmcneill 
    976   1.5  jmcneill 	val = RD4(sc, GENET_SYS_RBUF_FLUSH_CTRL);
    977   1.5  jmcneill 	if ((val & GENET_SYS_RBUF_FLUSH_RESET) == 0) {
    978  1.16   mlelstv 		maclo = RD4(sc, GENET_UMAC_MAC0);
    979  1.16   mlelstv 		machi = RD4(sc, GENET_UMAC_MAC1) & 0xffff;
    980   1.5  jmcneill 	}
    981   1.5  jmcneill 
    982   1.5  jmcneill 	if (maclo == 0 && machi == 0) {
    983   1.5  jmcneill 		/* Create one */
    984   1.5  jmcneill 		maclo = 0x00f2 | (cprng_strong32() & 0xffff0000);
    985   1.5  jmcneill 		machi = cprng_strong32() & 0xffff;
    986   1.1  jmcneill 	}
    987   1.1  jmcneill 
    988  1.16   mlelstv 	eaddr[0] = (maclo >> 24) & 0xff;
    989  1.16   mlelstv 	eaddr[1] = (maclo >> 16) & 0xff;
    990  1.16   mlelstv 	eaddr[2] = (maclo >>  8) & 0xff;
    991  1.16   mlelstv 	eaddr[3] = (maclo >>  0) & 0xff;
    992  1.16   mlelstv 	eaddr[4] = (machi >>  8) & 0xff;
    993  1.16   mlelstv 	eaddr[5] = (machi >>  0) & 0xff;
    994   1.1  jmcneill }
    995   1.1  jmcneill 
    996   1.1  jmcneill static int
    997   1.1  jmcneill genet_setup_dma(struct genet_softc *sc, int qid)
    998   1.1  jmcneill {
    999   1.1  jmcneill 	struct mbuf *m;
   1000   1.1  jmcneill 	int error, i;
   1001   1.1  jmcneill 
   1002   1.1  jmcneill 	/* Setup TX ring */
   1003   1.1  jmcneill 	sc->sc_tx.buf_tag = sc->sc_dmat;
   1004   1.1  jmcneill 	for (i = 0; i < TX_DESC_COUNT; i++) {
   1005   1.1  jmcneill 		error = bus_dmamap_create(sc->sc_tx.buf_tag, MCLBYTES,
   1006   1.1  jmcneill 		    TX_MAX_SEGS, MCLBYTES, 0, BUS_DMA_WAITOK,
   1007   1.1  jmcneill 		    &sc->sc_tx.buf_map[i].map);
   1008   1.1  jmcneill 		if (error != 0) {
   1009   1.1  jmcneill 			device_printf(sc->sc_dev,
   1010   1.1  jmcneill 			    "cannot create TX buffer map\n");
   1011   1.1  jmcneill 			return error;
   1012   1.1  jmcneill 		}
   1013   1.1  jmcneill 	}
   1014   1.1  jmcneill 
   1015   1.1  jmcneill 	/* Setup RX ring */
   1016   1.1  jmcneill 	sc->sc_rx.buf_tag = sc->sc_dmat;
   1017   1.1  jmcneill 	for (i = 0; i < RX_DESC_COUNT; i++) {
   1018   1.1  jmcneill 		error = bus_dmamap_create(sc->sc_rx.buf_tag, MCLBYTES,
   1019   1.1  jmcneill 		    1, MCLBYTES, 0, BUS_DMA_WAITOK,
   1020   1.1  jmcneill 		    &sc->sc_rx.buf_map[i].map);
   1021   1.1  jmcneill 		if (error != 0) {
   1022   1.1  jmcneill 			device_printf(sc->sc_dev,
   1023   1.1  jmcneill 			    "cannot create RX buffer map\n");
   1024   1.1  jmcneill 			return error;
   1025   1.1  jmcneill 		}
   1026   1.1  jmcneill 		if ((m = genet_alloc_mbufcl(sc)) == NULL) {
   1027   1.1  jmcneill 			device_printf(sc->sc_dev, "cannot allocate RX mbuf\n");
   1028   1.1  jmcneill 			return ENOMEM;
   1029   1.1  jmcneill 		}
   1030   1.1  jmcneill 		error = genet_setup_rxbuf(sc, i, m);
   1031   1.1  jmcneill 		if (error != 0) {
   1032   1.1  jmcneill 			device_printf(sc->sc_dev, "cannot create RX buffer\n");
   1033   1.1  jmcneill 			return error;
   1034   1.1  jmcneill 		}
   1035   1.1  jmcneill 	}
   1036   1.1  jmcneill 
   1037   1.1  jmcneill 	return 0;
   1038   1.1  jmcneill }
   1039   1.1  jmcneill 
   1040  1.17   mlelstv static void
   1041  1.17   mlelstv genet_claim_rxring(struct genet_softc *sc, int qid)
   1042  1.17   mlelstv {
   1043  1.17   mlelstv 	struct mbuf *m;
   1044  1.17   mlelstv 	int i;
   1045  1.17   mlelstv 
   1046  1.17   mlelstv 	/* Claim mbufs from RX ring */
   1047  1.17   mlelstv 	for (i = 0; i < RX_DESC_COUNT; i++) {
   1048  1.17   mlelstv 		m = sc->sc_rx.buf_map[i].mbuf;
   1049  1.17   mlelstv 		if (m != NULL) {
   1050  1.17   mlelstv 			MCLAIM(m, &sc->sc_ec.ec_rx_mowner);
   1051  1.17   mlelstv 		}
   1052  1.17   mlelstv 	}
   1053  1.17   mlelstv }
   1054  1.17   mlelstv 
   1055   1.1  jmcneill int
   1056   1.1  jmcneill genet_attach(struct genet_softc *sc)
   1057   1.1  jmcneill {
   1058   1.1  jmcneill 	struct mii_data *mii = &sc->sc_mii;
   1059   1.1  jmcneill 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1060   1.1  jmcneill 	uint8_t eaddr[ETHER_ADDR_LEN];
   1061   1.1  jmcneill 	u_int maj, min;
   1062   1.6  jmcneill 	int mii_flags = 0;
   1063   1.1  jmcneill 
   1064   1.1  jmcneill 	const uint32_t rev = RD4(sc, GENET_SYS_REV_CTRL);
   1065   1.1  jmcneill 	min = __SHIFTOUT(rev, SYS_REV_MINOR);
   1066   1.1  jmcneill 	maj = __SHIFTOUT(rev, SYS_REV_MAJOR);
   1067   1.1  jmcneill 	if (maj == 0)
   1068   1.1  jmcneill 		maj++;
   1069   1.1  jmcneill 	else if (maj == 5 || maj == 6)
   1070   1.1  jmcneill 		maj--;
   1071   1.1  jmcneill 
   1072   1.1  jmcneill 	if (maj != 5) {
   1073   1.1  jmcneill 		aprint_error(": GENETv%d.%d not supported\n", maj, min);
   1074   1.1  jmcneill 		return ENXIO;
   1075   1.1  jmcneill 	}
   1076   1.1  jmcneill 
   1077   1.6  jmcneill 	switch (sc->sc_phy_mode) {
   1078   1.6  jmcneill 	case GENET_PHY_MODE_RGMII_TXID:
   1079   1.6  jmcneill 		mii_flags |= MIIF_TXID;
   1080   1.6  jmcneill 		break;
   1081   1.6  jmcneill 	case GENET_PHY_MODE_RGMII_RXID:
   1082   1.6  jmcneill 		mii_flags |= MIIF_RXID;
   1083   1.6  jmcneill 		break;
   1084   1.6  jmcneill 	case GENET_PHY_MODE_RGMII_ID:
   1085   1.6  jmcneill 		mii_flags |= MIIF_RXID | MIIF_TXID;
   1086   1.6  jmcneill 		break;
   1087   1.6  jmcneill 	case GENET_PHY_MODE_RGMII:
   1088   1.6  jmcneill 	default:
   1089   1.6  jmcneill 		break;
   1090   1.6  jmcneill 	}
   1091   1.6  jmcneill 
   1092   1.1  jmcneill 	aprint_naive("\n");
   1093   1.1  jmcneill 	aprint_normal(": GENETv%d.%d\n", maj, min);
   1094   1.1  jmcneill 
   1095   1.1  jmcneill 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NET);
   1096   1.8   mlelstv 	mutex_init(&sc->sc_txlock, MUTEX_DEFAULT, IPL_NET);
   1097  1.20     skrll 	callout_init(&sc->sc_stat_ch, CALLOUT_MPSAFE);
   1098   1.1  jmcneill 	callout_setfunc(&sc->sc_stat_ch, genet_tick, sc);
   1099   1.1  jmcneill 
   1100   1.1  jmcneill 	genet_get_eaddr(sc, eaddr);
   1101   1.1  jmcneill 	aprint_normal_dev(sc->sc_dev, "Ethernet address %s\n", ether_sprintf(eaddr));
   1102   1.1  jmcneill 
   1103   1.1  jmcneill 	/* Soft reset EMAC core */
   1104   1.1  jmcneill 	genet_reset(sc);
   1105   1.1  jmcneill 
   1106   1.1  jmcneill 	/* Setup DMA descriptors */
   1107   1.1  jmcneill 	if (genet_setup_dma(sc, GENET_DMA_DEFAULT_QUEUE) != 0) {
   1108   1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "failed to setup DMA descriptors\n");
   1109   1.1  jmcneill 		return EINVAL;
   1110   1.1  jmcneill 	}
   1111   1.1  jmcneill 
   1112   1.1  jmcneill 	/* Setup ethernet interface */
   1113   1.1  jmcneill 	ifp->if_softc = sc;
   1114   1.3  jmcneill 	snprintf(ifp->if_xname, IFNAMSIZ, "%s", device_xname(sc->sc_dev));
   1115   1.1  jmcneill 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
   1116   1.1  jmcneill 	ifp->if_extflags = IFEF_MPSAFE;
   1117   1.1  jmcneill 	ifp->if_start = genet_start;
   1118   1.1  jmcneill 	ifp->if_ioctl = genet_ioctl;
   1119   1.1  jmcneill 	ifp->if_init = genet_init;
   1120   1.1  jmcneill 	ifp->if_stop = genet_stop;
   1121   1.1  jmcneill 	ifp->if_capabilities = 0;
   1122   1.1  jmcneill 	ifp->if_capenable = ifp->if_capabilities;
   1123   1.1  jmcneill 	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
   1124   1.1  jmcneill 	IFQ_SET_READY(&ifp->if_snd);
   1125   1.1  jmcneill 
   1126   1.1  jmcneill 	/* 802.1Q VLAN-sized frames are supported */
   1127   1.1  jmcneill 	sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_MTU;
   1128   1.1  jmcneill 
   1129   1.1  jmcneill 	/* Attach MII driver */
   1130   1.1  jmcneill 	sc->sc_ec.ec_mii = mii;
   1131   1.1  jmcneill 	ifmedia_init(&mii->mii_media, 0, ether_mediachange, ether_mediastatus);
   1132   1.1  jmcneill 	mii->mii_ifp = ifp;
   1133   1.1  jmcneill 	mii->mii_readreg = genet_mii_readreg;
   1134   1.1  jmcneill 	mii->mii_writereg = genet_mii_writereg;
   1135   1.1  jmcneill 	mii->mii_statchg = genet_mii_statchg;
   1136   1.1  jmcneill 	mii_attach(sc->sc_dev, mii, 0xffffffff, sc->sc_phy_id, MII_OFFSET_ANY,
   1137   1.6  jmcneill 	    mii_flags);
   1138   1.1  jmcneill 
   1139   1.1  jmcneill 	if (LIST_EMPTY(&mii->mii_phys)) {
   1140   1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "no PHY found!\n");
   1141   1.1  jmcneill 		return ENOENT;
   1142   1.1  jmcneill 	}
   1143   1.1  jmcneill 	ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
   1144   1.1  jmcneill 
   1145   1.1  jmcneill 	/* Attach interface */
   1146   1.1  jmcneill 	if_attach(ifp);
   1147   1.1  jmcneill 	if_deferred_start_init(ifp, NULL);
   1148   1.1  jmcneill 
   1149   1.1  jmcneill 	/* Attach ethernet interface */
   1150   1.1  jmcneill 	ether_ifattach(ifp, eaddr);
   1151  1.21     skrll 	ether_set_ifflags_cb(&sc->sc_ec, genet_ifflags_cb);
   1152  1.21     skrll 
   1153   1.1  jmcneill 
   1154  1.17   mlelstv 	/* MBUFTRACE */
   1155  1.17   mlelstv 	genet_claim_rxring(sc, GENET_DMA_DEFAULT_QUEUE);
   1156  1.17   mlelstv 
   1157   1.9       rin 	rnd_attach_source(&sc->sc_rndsource, ifp->if_xname, RND_TYPE_NET,
   1158   1.9       rin 	    RND_FLAG_DEFAULT);
   1159   1.9       rin 
   1160   1.1  jmcneill 	return 0;
   1161   1.1  jmcneill }
   1162   1.1  jmcneill 
   1163   1.1  jmcneill #ifdef DDB
   1164   1.1  jmcneill void	genet_debug(void);
   1165   1.1  jmcneill 
   1166   1.1  jmcneill void
   1167   1.1  jmcneill genet_debug(void)
   1168   1.1  jmcneill {
   1169   1.1  jmcneill 	device_t dev = device_find_by_xname("genet0");
   1170   1.1  jmcneill 	if (dev == NULL)
   1171   1.1  jmcneill 		return;
   1172   1.1  jmcneill 
   1173   1.1  jmcneill 	struct genet_softc * const sc = device_private(dev);
   1174   1.1  jmcneill 	const int qid = GENET_DMA_DEFAULT_QUEUE;
   1175   1.1  jmcneill 
   1176   1.1  jmcneill 	printf("TX CIDX = %08x (soft)\n", sc->sc_tx.cidx);
   1177   1.1  jmcneill 	printf("TX CIDX = %08x\n", RD4(sc, GENET_TX_DMA_CONS_INDEX(qid)));
   1178   1.1  jmcneill 	printf("TX PIDX = %08x (soft)\n", sc->sc_tx.pidx);
   1179   1.1  jmcneill 	printf("TX PIDX = %08x\n", RD4(sc, GENET_TX_DMA_PROD_INDEX(qid)));
   1180   1.1  jmcneill 
   1181   1.1  jmcneill 	printf("RX CIDX = %08x (soft)\n", sc->sc_rx.cidx);
   1182   1.1  jmcneill 	printf("RX CIDX = %08x\n", RD4(sc, GENET_RX_DMA_CONS_INDEX(qid)));
   1183   1.1  jmcneill 	printf("RX PIDX = %08x (soft)\n", sc->sc_rx.pidx);
   1184   1.1  jmcneill 	printf("RX PIDX = %08x\n", RD4(sc, GENET_RX_DMA_PROD_INDEX(qid)));
   1185   1.1  jmcneill }
   1186   1.1  jmcneill #endif
   1187