Home | History | Annotate | Line # | Download | only in imx
if_enet.c revision 1.5
      1  1.5     ozaki /*	$NetBSD: if_enet.c,v 1.5 2016/02/09 08:32:08 ozaki-r Exp $	*/
      2  1.1       ryo 
      3  1.1       ryo /*
      4  1.1       ryo  * Copyright (c) 2014 Ryo Shimizu <ryo (at) nerv.org>
      5  1.1       ryo  * All rights reserved.
      6  1.1       ryo  *
      7  1.1       ryo  * Redistribution and use in source and binary forms, with or without
      8  1.1       ryo  * modification, are permitted provided that the following conditions
      9  1.1       ryo  * are met:
     10  1.1       ryo  * 1. Redistributions of source code must retain the above copyright
     11  1.1       ryo  *    notice, this list of conditions and the following disclaimer.
     12  1.1       ryo  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1       ryo  *    notice, this list of conditions and the following disclaimer in the
     14  1.1       ryo  *    documentation and/or other materials provided with the distribution.
     15  1.1       ryo  *
     16  1.1       ryo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1       ryo  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     18  1.1       ryo  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     19  1.1       ryo  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     20  1.1       ryo  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     21  1.1       ryo  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     22  1.1       ryo  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1       ryo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     24  1.1       ryo  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     25  1.1       ryo  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1       ryo  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1       ryo  */
     28  1.1       ryo 
     29  1.1       ryo /*
     30  1.1       ryo  * i.MX6 10/100/1000-Mbps ethernet MAC (ENET)
     31  1.1       ryo  */
     32  1.1       ryo 
     33  1.1       ryo #include <sys/cdefs.h>
     34  1.5     ozaki __KERNEL_RCSID(0, "$NetBSD: if_enet.c,v 1.5 2016/02/09 08:32:08 ozaki-r Exp $");
     35  1.1       ryo 
     36  1.1       ryo #include "imxocotp.h"
     37  1.1       ryo #include "imxccm.h"
     38  1.1       ryo #include "vlan.h"
     39  1.1       ryo 
     40  1.1       ryo #include <sys/param.h>
     41  1.1       ryo #include <sys/bus.h>
     42  1.1       ryo #include <sys/mbuf.h>
     43  1.1       ryo #include <sys/device.h>
     44  1.1       ryo #include <sys/sockio.h>
     45  1.1       ryo #include <sys/kernel.h>
     46  1.3  riastrad #include <sys/rndsource.h>
     47  1.1       ryo 
     48  1.1       ryo #include <lib/libkern/libkern.h>
     49  1.1       ryo 
     50  1.1       ryo #include <net/if.h>
     51  1.1       ryo #include <net/if_dl.h>
     52  1.1       ryo #include <net/if_media.h>
     53  1.1       ryo #include <net/if_ether.h>
     54  1.1       ryo #include <net/bpf.h>
     55  1.1       ryo #include <net/if_vlanvar.h>
     56  1.1       ryo 
     57  1.1       ryo #include <netinet/in.h>
     58  1.1       ryo #include <netinet/in_systm.h>
     59  1.1       ryo #include <netinet/ip.h>
     60  1.1       ryo 
     61  1.1       ryo #include <dev/mii/mii.h>
     62  1.1       ryo #include <dev/mii/miivar.h>
     63  1.1       ryo 
     64  1.1       ryo #include <arm/imx/imx6var.h>
     65  1.1       ryo #include <arm/imx/imx6_reg.h>
     66  1.1       ryo #include <arm/imx/imx6_ocotpreg.h>
     67  1.1       ryo #include <arm/imx/imx6_ocotpvar.h>
     68  1.1       ryo #include <arm/imx/imx6_ccmreg.h>
     69  1.1       ryo #include <arm/imx/imx6_ccmvar.h>
     70  1.1       ryo #include <arm/imx/if_enetreg.h>
     71  1.1       ryo #include "locators.h"
     72  1.1       ryo 
     73  1.1       ryo #undef DEBUG_ENET
     74  1.1       ryo #undef ENET_EVENT_COUNTER
     75  1.1       ryo 
     76  1.1       ryo #ifdef DEBUG_ENET
     77  1.1       ryo int enet_debug = 0;
     78  1.1       ryo # define DEVICE_DPRINTF(args...)	\
     79  1.1       ryo 	do { if (enet_debug) device_printf(sc->sc_dev, args); } while (0)
     80  1.1       ryo #else
     81  1.1       ryo # define DEVICE_DPRINTF(args...)
     82  1.1       ryo #endif
     83  1.1       ryo 
     84  1.1       ryo 
     85  1.1       ryo #define RXDESC_MAXBUFSIZE	0x07f0
     86  1.1       ryo 				/* iMX6 ENET not work greather than 0x0800... */
     87  1.1       ryo 
     88  1.1       ryo #undef ENET_SUPPORT_JUMBO	/* JUMBO FRAME SUPPORT is unstable */
     89  1.1       ryo #ifdef ENET_SUPPORT_JUMBO
     90  1.1       ryo # define ENET_MAX_PKT_LEN	4034	/* MAX FIFO LEN */
     91  1.1       ryo #else
     92  1.1       ryo # define ENET_MAX_PKT_LEN	1522
     93  1.1       ryo #endif
     94  1.1       ryo #define ENET_DEFAULT_PKT_LEN	1522	/* including VLAN tag */
     95  1.1       ryo #define MTU2FRAMESIZE(n)	\
     96  1.1       ryo 	((n) + ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN)
     97  1.1       ryo 
     98  1.1       ryo 
     99  1.1       ryo #define ENET_MAX_PKT_NSEGS	64
    100  1.1       ryo #define ENET_TX_RING_CNT	256	/* must be 2^n */
    101  1.1       ryo #define ENET_RX_RING_CNT	256	/* must be 2^n */
    102  1.1       ryo 
    103  1.1       ryo #define ENET_TX_NEXTIDX(idx)	(((idx) + 1) & (ENET_TX_RING_CNT - 1))
    104  1.1       ryo #define ENET_RX_NEXTIDX(idx)	(((idx) + 1) & (ENET_RX_RING_CNT - 1))
    105  1.1       ryo 
    106  1.1       ryo struct enet_txsoft {
    107  1.1       ryo 	struct mbuf *txs_mbuf;		/* head of our mbuf chain */
    108  1.1       ryo 	bus_dmamap_t txs_dmamap;	/* our DMA map */
    109  1.1       ryo };
    110  1.1       ryo 
    111  1.1       ryo struct enet_rxsoft {
    112  1.1       ryo 	struct mbuf *rxs_mbuf;		/* head of our mbuf chain */
    113  1.1       ryo 	bus_dmamap_t rxs_dmamap;	/* our DMA map */
    114  1.1       ryo };
    115  1.1       ryo 
    116  1.1       ryo struct enet_softc {
    117  1.1       ryo 	device_t sc_dev;
    118  1.1       ryo 
    119  1.1       ryo 	bus_addr_t sc_addr;
    120  1.1       ryo 	bus_space_tag_t sc_iot;
    121  1.1       ryo 	bus_space_handle_t sc_ioh;
    122  1.1       ryo 	bus_dma_tag_t sc_dmat;
    123  1.1       ryo 
    124  1.1       ryo 	/* interrupts */
    125  1.1       ryo 	void *sc_ih;
    126  1.1       ryo 	callout_t sc_tick_ch;
    127  1.1       ryo 	bool sc_stopping;
    128  1.1       ryo 
    129  1.1       ryo 	/* TX */
    130  1.1       ryo 	struct enet_txdesc *sc_txdesc_ring;	/* [ENET_TX_RING_CNT] */
    131  1.1       ryo 	bus_dmamap_t sc_txdesc_dmamap;
    132  1.1       ryo 	struct enet_rxdesc *sc_rxdesc_ring;	/* [ENET_RX_RING_CNT] */
    133  1.1       ryo 	bus_dmamap_t sc_rxdesc_dmamap;
    134  1.1       ryo 	struct enet_txsoft sc_txsoft[ENET_TX_RING_CNT];
    135  1.1       ryo 	int sc_tx_considx;
    136  1.1       ryo 	int sc_tx_prodidx;
    137  1.1       ryo 	int sc_tx_free;
    138  1.1       ryo 
    139  1.1       ryo 	/* RX */
    140  1.1       ryo 	struct enet_rxsoft sc_rxsoft[ENET_RX_RING_CNT];
    141  1.1       ryo 	int sc_rx_readidx;
    142  1.1       ryo 
    143  1.1       ryo 	/* misc */
    144  1.1       ryo 	int sc_if_flags;			/* local copy of if_flags */
    145  1.1       ryo 	int sc_flowflags;			/* 802.3x flow control flags */
    146  1.1       ryo 	struct ethercom sc_ethercom;		/* interface info */
    147  1.1       ryo 	struct mii_data sc_mii;
    148  1.1       ryo 	uint8_t sc_enaddr[ETHER_ADDR_LEN];
    149  1.1       ryo 	krndsource_t sc_rnd_source;
    150  1.1       ryo 
    151  1.1       ryo #ifdef ENET_EVENT_COUNTER
    152  1.1       ryo 	struct evcnt sc_ev_t_drop;
    153  1.1       ryo 	struct evcnt sc_ev_t_packets;
    154  1.1       ryo 	struct evcnt sc_ev_t_bc_pkt;
    155  1.1       ryo 	struct evcnt sc_ev_t_mc_pkt;
    156  1.1       ryo 	struct evcnt sc_ev_t_crc_align;
    157  1.1       ryo 	struct evcnt sc_ev_t_undersize;
    158  1.1       ryo 	struct evcnt sc_ev_t_oversize;
    159  1.1       ryo 	struct evcnt sc_ev_t_frag;
    160  1.1       ryo 	struct evcnt sc_ev_t_jab;
    161  1.1       ryo 	struct evcnt sc_ev_t_col;
    162  1.1       ryo 	struct evcnt sc_ev_t_p64;
    163  1.1       ryo 	struct evcnt sc_ev_t_p65to127n;
    164  1.1       ryo 	struct evcnt sc_ev_t_p128to255n;
    165  1.1       ryo 	struct evcnt sc_ev_t_p256to511;
    166  1.1       ryo 	struct evcnt sc_ev_t_p512to1023;
    167  1.1       ryo 	struct evcnt sc_ev_t_p1024to2047;
    168  1.1       ryo 	struct evcnt sc_ev_t_p_gte2048;
    169  1.1       ryo 	struct evcnt sc_ev_t_octets;
    170  1.1       ryo 	struct evcnt sc_ev_r_packets;
    171  1.1       ryo 	struct evcnt sc_ev_r_bc_pkt;
    172  1.1       ryo 	struct evcnt sc_ev_r_mc_pkt;
    173  1.1       ryo 	struct evcnt sc_ev_r_crc_align;
    174  1.1       ryo 	struct evcnt sc_ev_r_undersize;
    175  1.1       ryo 	struct evcnt sc_ev_r_oversize;
    176  1.1       ryo 	struct evcnt sc_ev_r_frag;
    177  1.1       ryo 	struct evcnt sc_ev_r_jab;
    178  1.1       ryo 	struct evcnt sc_ev_r_p64;
    179  1.1       ryo 	struct evcnt sc_ev_r_p65to127;
    180  1.1       ryo 	struct evcnt sc_ev_r_p128to255;
    181  1.1       ryo 	struct evcnt sc_ev_r_p256to511;
    182  1.1       ryo 	struct evcnt sc_ev_r_p512to1023;
    183  1.1       ryo 	struct evcnt sc_ev_r_p1024to2047;
    184  1.1       ryo 	struct evcnt sc_ev_r_p_gte2048;
    185  1.1       ryo 	struct evcnt sc_ev_r_octets;
    186  1.1       ryo #endif /* ENET_EVENT_COUNTER */
    187  1.1       ryo };
    188  1.1       ryo 
    189  1.1       ryo #define TXDESC_WRITEOUT(idx)					\
    190  1.1       ryo 	bus_dmamap_sync(sc->sc_dmat, sc->sc_txdesc_dmamap,	\
    191  1.1       ryo 	    sizeof(struct enet_txdesc) * (idx),			\
    192  1.1       ryo 	    sizeof(struct enet_txdesc),				\
    193  1.1       ryo 	    BUS_DMASYNC_PREWRITE)
    194  1.1       ryo 
    195  1.1       ryo #define TXDESC_READIN(idx)					\
    196  1.1       ryo 	bus_dmamap_sync(sc->sc_dmat, sc->sc_txdesc_dmamap,	\
    197  1.1       ryo 	    sizeof(struct enet_txdesc) * (idx),			\
    198  1.1       ryo 	    sizeof(struct enet_txdesc),				\
    199  1.1       ryo 	    BUS_DMASYNC_PREREAD)
    200  1.1       ryo 
    201  1.1       ryo #define RXDESC_WRITEOUT(idx)					\
    202  1.1       ryo 	bus_dmamap_sync(sc->sc_dmat, sc->sc_rxdesc_dmamap,	\
    203  1.1       ryo 	    sizeof(struct enet_rxdesc) * (idx),			\
    204  1.1       ryo 	    sizeof(struct enet_rxdesc),				\
    205  1.1       ryo 	    BUS_DMASYNC_PREWRITE)
    206  1.1       ryo 
    207  1.1       ryo #define RXDESC_READIN(idx)					\
    208  1.1       ryo 	bus_dmamap_sync(sc->sc_dmat, sc->sc_rxdesc_dmamap,	\
    209  1.1       ryo 	    sizeof(struct enet_rxdesc) * (idx),			\
    210  1.1       ryo 	    sizeof(struct enet_rxdesc),				\
    211  1.1       ryo 	    BUS_DMASYNC_PREREAD)
    212  1.1       ryo 
    213  1.1       ryo #define ENET_REG_READ(sc, reg)					\
    214  1.1       ryo 	bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, reg)
    215  1.1       ryo 
    216  1.1       ryo #define ENET_REG_WRITE(sc, reg, value)				\
    217  1.1       ryo 	bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, reg, value)
    218  1.1       ryo 
    219  1.1       ryo static int enet_match(device_t, struct cfdata *, void *);
    220  1.1       ryo static void enet_attach(device_t, device_t, void *);
    221  1.1       ryo #ifdef ENET_EVENT_COUNTER
    222  1.1       ryo static void enet_attach_evcnt(struct enet_softc *);
    223  1.1       ryo static void enet_update_evcnt(struct enet_softc *);
    224  1.1       ryo #endif
    225  1.1       ryo 
    226  1.1       ryo static int enet_intr(void *);
    227  1.1       ryo static void enet_tick(void *);
    228  1.1       ryo static int enet_tx_intr(void *);
    229  1.1       ryo static int enet_rx_intr(void *);
    230  1.1       ryo static void enet_rx_csum(struct enet_softc *, struct ifnet *, struct mbuf *,
    231  1.1       ryo                          int);
    232  1.1       ryo 
    233  1.1       ryo static void enet_start(struct ifnet *);
    234  1.1       ryo static int enet_ifflags_cb(struct ethercom *);
    235  1.1       ryo static int enet_ioctl(struct ifnet *, u_long, void *);
    236  1.1       ryo static int enet_init(struct ifnet *);
    237  1.1       ryo static void enet_stop(struct ifnet *, int);
    238  1.1       ryo static void enet_watchdog(struct ifnet *);
    239  1.1       ryo static void enet_mediastatus(struct ifnet *, struct ifmediareq *);
    240  1.1       ryo 
    241  1.1       ryo static int enet_miibus_readreg(device_t, int, int);
    242  1.1       ryo static void enet_miibus_writereg(device_t, int, int, int);
    243  1.1       ryo static void enet_miibus_statchg(struct ifnet *);
    244  1.1       ryo 
    245  1.1       ryo static void enet_ocotp_getmacaddr(uint8_t *);
    246  1.1       ryo static void enet_gethwaddr(struct enet_softc *, uint8_t *);
    247  1.1       ryo static void enet_sethwaddr(struct enet_softc *, uint8_t *);
    248  1.1       ryo static void enet_setmulti(struct enet_softc *);
    249  1.1       ryo static int enet_encap_mbufalign(struct mbuf **);
    250  1.1       ryo static int enet_encap_txring(struct enet_softc *, struct mbuf **);
    251  1.1       ryo static int enet_init_plls(struct enet_softc *);
    252  1.1       ryo static int enet_init_regs(struct enet_softc *, int);
    253  1.1       ryo static int enet_alloc_ring(struct enet_softc *);
    254  1.1       ryo static void enet_init_txring(struct enet_softc *);
    255  1.1       ryo static int enet_init_rxring(struct enet_softc *);
    256  1.1       ryo static void enet_reset_rxdesc(struct enet_softc *, int);
    257  1.1       ryo static int enet_alloc_rxbuf(struct enet_softc *, int);
    258  1.1       ryo static void enet_drain_txbuf(struct enet_softc *);
    259  1.1       ryo static void enet_drain_rxbuf(struct enet_softc *);
    260  1.1       ryo static int enet_alloc_dma(struct enet_softc *, size_t, void **,
    261  1.1       ryo                           bus_dmamap_t *);
    262  1.1       ryo 
    263  1.1       ryo CFATTACH_DECL_NEW(enet, sizeof(struct enet_softc),
    264  1.1       ryo     enet_match, enet_attach, NULL, NULL);
    265  1.1       ryo 
    266  1.1       ryo /* ARGSUSED */
    267  1.1       ryo static int
    268  1.1       ryo enet_match(device_t parent __unused, struct cfdata *match __unused, void *aux)
    269  1.1       ryo {
    270  1.1       ryo 	struct axi_attach_args *aa;
    271  1.1       ryo 
    272  1.1       ryo 	aa = aux;
    273  1.1       ryo 
    274  1.1       ryo 	switch (aa->aa_addr) {
    275  1.1       ryo 	case (IMX6_AIPS2_BASE + AIPS2_ENET_BASE):
    276  1.1       ryo 		return 1;
    277  1.1       ryo 	}
    278  1.1       ryo 
    279  1.1       ryo 	return 0;
    280  1.1       ryo }
    281  1.1       ryo 
    282  1.1       ryo /* ARGSUSED */
    283  1.1       ryo static void
    284  1.1       ryo enet_attach(device_t parent __unused, device_t self, void *aux)
    285  1.1       ryo {
    286  1.1       ryo 	struct enet_softc *sc;
    287  1.1       ryo 	struct axi_attach_args *aa;
    288  1.1       ryo 	struct ifnet *ifp;
    289  1.1       ryo 
    290  1.1       ryo 	aa = aux;
    291  1.1       ryo 	sc = device_private(self);
    292  1.1       ryo 	sc->sc_dev = self;
    293  1.1       ryo 	sc->sc_iot = aa->aa_iot;
    294  1.1       ryo 	sc->sc_addr = aa->aa_addr;
    295  1.1       ryo 	sc->sc_dmat = aa->aa_dmat;
    296  1.1       ryo 
    297  1.1       ryo 	if (aa->aa_size == AXICF_SIZE_DEFAULT)
    298  1.1       ryo 		aa->aa_size = AIPS2_ENET_SIZE;
    299  1.1       ryo 
    300  1.1       ryo 	aprint_naive("\n");
    301  1.1       ryo 	aprint_normal(": Gigabit Ethernet Controller\n");
    302  1.1       ryo 	if (bus_space_map(sc->sc_iot, sc->sc_addr, aa->aa_size, 0,
    303  1.1       ryo 	    &sc->sc_ioh)) {
    304  1.1       ryo 		aprint_error_dev(self, "cannot map registers\n");
    305  1.1       ryo 		return;
    306  1.1       ryo 	}
    307  1.1       ryo 
    308  1.1       ryo 	/* allocate dma buffer */
    309  1.1       ryo 	if (enet_alloc_ring(sc))
    310  1.1       ryo 		return;
    311  1.1       ryo 
    312  1.1       ryo #define IS_ENADDR_ZERO(enaddr)				\
    313  1.1       ryo 	((enaddr[0] | enaddr[1] | enaddr[2] |		\
    314  1.1       ryo 	 enaddr[3] | enaddr[4] | enaddr[5]) == 0)
    315  1.1       ryo 
    316  1.1       ryo 	/* get mac-address from SoC eFuse */
    317  1.1       ryo 	enet_ocotp_getmacaddr(sc->sc_enaddr);
    318  1.1       ryo 	if (IS_ENADDR_ZERO(sc->sc_enaddr)) {
    319  1.1       ryo 		/* by any chance, mac-address is already set by bootloader? */
    320  1.1       ryo 		enet_gethwaddr(sc, sc->sc_enaddr);
    321  1.1       ryo 		if (IS_ENADDR_ZERO(sc->sc_enaddr)) {
    322  1.1       ryo 			/* give up. set randomly */
    323  1.1       ryo 			uint32_t addr = random();
    324  1.1       ryo 			/* not multicast */
    325  1.1       ryo 			sc->sc_enaddr[0] = (addr >> 24) & 0xfc;
    326  1.1       ryo 			sc->sc_enaddr[1] = addr >> 16;
    327  1.1       ryo 			sc->sc_enaddr[2] = addr >> 8;
    328  1.1       ryo 			sc->sc_enaddr[3] = addr;
    329  1.1       ryo 			addr = random();
    330  1.1       ryo 			sc->sc_enaddr[4] = addr >> 8;
    331  1.1       ryo 			sc->sc_enaddr[5] = addr;
    332  1.1       ryo 
    333  1.1       ryo 			aprint_error_dev(self,
    334  1.1       ryo 			    "cannot get mac address. set randomly\n");
    335  1.1       ryo 		}
    336  1.1       ryo 	}
    337  1.1       ryo 	enet_sethwaddr(sc, sc->sc_enaddr);
    338  1.1       ryo 
    339  1.1       ryo 	aprint_normal_dev(self, "Ethernet address %s\n",
    340  1.1       ryo 	    ether_sprintf(sc->sc_enaddr));
    341  1.1       ryo 
    342  1.1       ryo 	/* power up and init */
    343  1.1       ryo 	if (enet_init_plls(sc) != 0)
    344  1.1       ryo 		goto failure;
    345  1.1       ryo 	enet_init_regs(sc, 1);
    346  1.1       ryo 
    347  1.1       ryo 	/* setup interrupt handlers */
    348  1.1       ryo 	if ((sc->sc_ih = intr_establish(aa->aa_irq, IPL_NET,
    349  1.2       ryo 	    IST_LEVEL, enet_intr, sc)) == NULL) {
    350  1.1       ryo 		aprint_error_dev(self, "unable to establish interrupt\n");
    351  1.1       ryo 		goto failure;
    352  1.1       ryo 	}
    353  1.1       ryo 
    354  1.2       ryo 	/* callout will be scheduled from enet_init() */
    355  1.2       ryo 	callout_init(&sc->sc_tick_ch, 0);
    356  1.2       ryo 	callout_setfunc(&sc->sc_tick_ch, enet_tick, sc);
    357  1.2       ryo 
    358  1.1       ryo 	/* setup ifp */
    359  1.1       ryo 	ifp = &sc->sc_ethercom.ec_if;
    360  1.1       ryo 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
    361  1.1       ryo 	ifp->if_softc = sc;
    362  1.1       ryo 	ifp->if_mtu = ETHERMTU;
    363  1.1       ryo 	ifp->if_baudrate = IF_Gbps(1);
    364  1.1       ryo 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    365  1.1       ryo 	ifp->if_ioctl = enet_ioctl;
    366  1.1       ryo 	ifp->if_start = enet_start;
    367  1.1       ryo 	ifp->if_init = enet_init;
    368  1.1       ryo 	ifp->if_stop = enet_stop;
    369  1.1       ryo 	ifp->if_watchdog = enet_watchdog;
    370  1.1       ryo 
    371  1.1       ryo 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
    372  1.1       ryo #ifdef ENET_SUPPORT_JUMBO
    373  1.1       ryo 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_JUMBO_MTU;
    374  1.1       ryo #endif
    375  1.1       ryo 
    376  1.1       ryo 	ifp->if_capabilities = IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx |
    377  1.1       ryo 	    IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_UDPv4_Tx |
    378  1.1       ryo 	    IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx |
    379  1.1       ryo 	    IFCAP_CSUM_TCPv6_Tx | IFCAP_CSUM_UDPv6_Tx |
    380  1.1       ryo 	    IFCAP_CSUM_TCPv6_Rx | IFCAP_CSUM_UDPv6_Rx;
    381  1.1       ryo 
    382  1.1       ryo 	IFQ_SET_MAXLEN(&ifp->if_snd, max(ENET_TX_RING_CNT, IFQ_MAXLEN));
    383  1.1       ryo 	IFQ_SET_READY(&ifp->if_snd);
    384  1.1       ryo 
    385  1.1       ryo 	/* setup MII */
    386  1.1       ryo 	sc->sc_ethercom.ec_mii = &sc->sc_mii;
    387  1.1       ryo 	sc->sc_mii.mii_ifp = ifp;
    388  1.1       ryo 	sc->sc_mii.mii_readreg = enet_miibus_readreg;
    389  1.1       ryo 	sc->sc_mii.mii_writereg = enet_miibus_writereg;
    390  1.1       ryo 	sc->sc_mii.mii_statchg = enet_miibus_statchg;
    391  1.1       ryo 	ifmedia_init(&sc->sc_mii.mii_media, 0, ether_mediachange,
    392  1.1       ryo 	    enet_mediastatus);
    393  1.1       ryo 
    394  1.1       ryo 	/* try to attach PHY */
    395  1.1       ryo 	mii_attach(self, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
    396  1.1       ryo 	    MII_OFFSET_ANY, 0);
    397  1.1       ryo 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
    398  1.1       ryo 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER | IFM_MANUAL,
    399  1.1       ryo 		    0, NULL);
    400  1.1       ryo 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_MANUAL);
    401  1.1       ryo 	} else {
    402  1.1       ryo 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_AUTO);
    403  1.1       ryo 	}
    404  1.1       ryo 
    405  1.1       ryo 	if_attach(ifp);
    406  1.1       ryo 	ether_ifattach(ifp, sc->sc_enaddr);
    407  1.1       ryo 	ether_set_ifflags_cb(&sc->sc_ethercom, enet_ifflags_cb);
    408  1.1       ryo 
    409  1.1       ryo 	rnd_attach_source(&sc->sc_rnd_source, device_xname(sc->sc_dev),
    410  1.1       ryo 	    RND_TYPE_NET, RND_FLAG_DEFAULT);
    411  1.1       ryo 
    412  1.1       ryo #ifdef ENET_EVENT_COUNTER
    413  1.1       ryo 	enet_attach_evcnt(sc);
    414  1.1       ryo #endif
    415  1.1       ryo 
    416  1.1       ryo 	sc->sc_stopping = false;
    417  1.1       ryo 
    418  1.1       ryo 	return;
    419  1.1       ryo 
    420  1.1       ryo  failure:
    421  1.1       ryo 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, aa->aa_size);
    422  1.1       ryo 	return;
    423  1.1       ryo }
    424  1.1       ryo 
    425  1.1       ryo #ifdef ENET_EVENT_COUNTER
    426  1.1       ryo static void
    427  1.1       ryo enet_attach_evcnt(struct enet_softc *sc)
    428  1.1       ryo {
    429  1.1       ryo 	const char *xname;
    430  1.1       ryo 
    431  1.1       ryo 	xname = device_xname(sc->sc_dev);
    432  1.1       ryo 
    433  1.1       ryo #define ENET_EVCNT_ATTACH(name)	\
    434  1.1       ryo 	evcnt_attach_dynamic(&sc->sc_ev_ ## name, EVCNT_TYPE_MISC,	\
    435  1.1       ryo 	    NULL, xname, #name);
    436  1.1       ryo 
    437  1.1       ryo 	ENET_EVCNT_ATTACH(t_drop);
    438  1.1       ryo 	ENET_EVCNT_ATTACH(t_packets);
    439  1.1       ryo 	ENET_EVCNT_ATTACH(t_bc_pkt);
    440  1.1       ryo 	ENET_EVCNT_ATTACH(t_mc_pkt);
    441  1.1       ryo 	ENET_EVCNT_ATTACH(t_crc_align);
    442  1.1       ryo 	ENET_EVCNT_ATTACH(t_undersize);
    443  1.1       ryo 	ENET_EVCNT_ATTACH(t_oversize);
    444  1.1       ryo 	ENET_EVCNT_ATTACH(t_frag);
    445  1.1       ryo 	ENET_EVCNT_ATTACH(t_jab);
    446  1.1       ryo 	ENET_EVCNT_ATTACH(t_col);
    447  1.1       ryo 	ENET_EVCNT_ATTACH(t_p64);
    448  1.1       ryo 	ENET_EVCNT_ATTACH(t_p65to127n);
    449  1.1       ryo 	ENET_EVCNT_ATTACH(t_p128to255n);
    450  1.1       ryo 	ENET_EVCNT_ATTACH(t_p256to511);
    451  1.1       ryo 	ENET_EVCNT_ATTACH(t_p512to1023);
    452  1.1       ryo 	ENET_EVCNT_ATTACH(t_p1024to2047);
    453  1.1       ryo 	ENET_EVCNT_ATTACH(t_p_gte2048);
    454  1.1       ryo 	ENET_EVCNT_ATTACH(t_octets);
    455  1.1       ryo 	ENET_EVCNT_ATTACH(r_packets);
    456  1.1       ryo 	ENET_EVCNT_ATTACH(r_bc_pkt);
    457  1.1       ryo 	ENET_EVCNT_ATTACH(r_mc_pkt);
    458  1.1       ryo 	ENET_EVCNT_ATTACH(r_crc_align);
    459  1.1       ryo 	ENET_EVCNT_ATTACH(r_undersize);
    460  1.1       ryo 	ENET_EVCNT_ATTACH(r_oversize);
    461  1.1       ryo 	ENET_EVCNT_ATTACH(r_frag);
    462  1.1       ryo 	ENET_EVCNT_ATTACH(r_jab);
    463  1.1       ryo 	ENET_EVCNT_ATTACH(r_p64);
    464  1.1       ryo 	ENET_EVCNT_ATTACH(r_p65to127);
    465  1.1       ryo 	ENET_EVCNT_ATTACH(r_p128to255);
    466  1.1       ryo 	ENET_EVCNT_ATTACH(r_p256to511);
    467  1.1       ryo 	ENET_EVCNT_ATTACH(r_p512to1023);
    468  1.1       ryo 	ENET_EVCNT_ATTACH(r_p1024to2047);
    469  1.1       ryo 	ENET_EVCNT_ATTACH(r_p_gte2048);
    470  1.1       ryo 	ENET_EVCNT_ATTACH(r_octets);
    471  1.1       ryo }
    472  1.1       ryo 
    473  1.1       ryo static void
    474  1.1       ryo enet_update_evcnt(struct enet_softc *sc)
    475  1.1       ryo {
    476  1.1       ryo 	sc->sc_ev_t_drop.ev_count += ENET_REG_READ(sc, ENET_RMON_T_DROP);
    477  1.1       ryo 	sc->sc_ev_t_packets.ev_count += ENET_REG_READ(sc, ENET_RMON_T_PACKETS);
    478  1.1       ryo 	sc->sc_ev_t_bc_pkt.ev_count += ENET_REG_READ(sc, ENET_RMON_T_BC_PKT);
    479  1.1       ryo 	sc->sc_ev_t_mc_pkt.ev_count += ENET_REG_READ(sc, ENET_RMON_T_MC_PKT);
    480  1.1       ryo 	sc->sc_ev_t_crc_align.ev_count += ENET_REG_READ(sc, ENET_RMON_T_CRC_ALIGN);
    481  1.1       ryo 	sc->sc_ev_t_undersize.ev_count += ENET_REG_READ(sc, ENET_RMON_T_UNDERSIZE);
    482  1.1       ryo 	sc->sc_ev_t_oversize.ev_count += ENET_REG_READ(sc, ENET_RMON_T_OVERSIZE);
    483  1.1       ryo 	sc->sc_ev_t_frag.ev_count += ENET_REG_READ(sc, ENET_RMON_T_FRAG);
    484  1.1       ryo 	sc->sc_ev_t_jab.ev_count += ENET_REG_READ(sc, ENET_RMON_T_JAB);
    485  1.1       ryo 	sc->sc_ev_t_col.ev_count += ENET_REG_READ(sc, ENET_RMON_T_COL);
    486  1.1       ryo 	sc->sc_ev_t_p64.ev_count += ENET_REG_READ(sc, ENET_RMON_T_P64);
    487  1.1       ryo 	sc->sc_ev_t_p65to127n.ev_count += ENET_REG_READ(sc, ENET_RMON_T_P65TO127N);
    488  1.1       ryo 	sc->sc_ev_t_p128to255n.ev_count += ENET_REG_READ(sc, ENET_RMON_T_P128TO255N);
    489  1.1       ryo 	sc->sc_ev_t_p256to511.ev_count += ENET_REG_READ(sc, ENET_RMON_T_P256TO511);
    490  1.1       ryo 	sc->sc_ev_t_p512to1023.ev_count += ENET_REG_READ(sc, ENET_RMON_T_P512TO1023);
    491  1.1       ryo 	sc->sc_ev_t_p1024to2047.ev_count += ENET_REG_READ(sc, ENET_RMON_T_P1024TO2047);
    492  1.1       ryo 	sc->sc_ev_t_p_gte2048.ev_count += ENET_REG_READ(sc, ENET_RMON_T_P_GTE2048);
    493  1.1       ryo 	sc->sc_ev_t_octets.ev_count += ENET_REG_READ(sc, ENET_RMON_T_OCTETS);
    494  1.1       ryo 	sc->sc_ev_r_packets.ev_count += ENET_REG_READ(sc, ENET_RMON_R_PACKETS);
    495  1.1       ryo 	sc->sc_ev_r_bc_pkt.ev_count += ENET_REG_READ(sc, ENET_RMON_R_BC_PKT);
    496  1.1       ryo 	sc->sc_ev_r_mc_pkt.ev_count += ENET_REG_READ(sc, ENET_RMON_R_MC_PKT);
    497  1.1       ryo 	sc->sc_ev_r_crc_align.ev_count += ENET_REG_READ(sc, ENET_RMON_R_CRC_ALIGN);
    498  1.1       ryo 	sc->sc_ev_r_undersize.ev_count += ENET_REG_READ(sc, ENET_RMON_R_UNDERSIZE);
    499  1.1       ryo 	sc->sc_ev_r_oversize.ev_count += ENET_REG_READ(sc, ENET_RMON_R_OVERSIZE);
    500  1.1       ryo 	sc->sc_ev_r_frag.ev_count += ENET_REG_READ(sc, ENET_RMON_R_FRAG);
    501  1.1       ryo 	sc->sc_ev_r_jab.ev_count += ENET_REG_READ(sc, ENET_RMON_R_JAB);
    502  1.1       ryo 	sc->sc_ev_r_p64.ev_count += ENET_REG_READ(sc, ENET_RMON_R_P64);
    503  1.1       ryo 	sc->sc_ev_r_p65to127.ev_count += ENET_REG_READ(sc, ENET_RMON_R_P65TO127);
    504  1.1       ryo 	sc->sc_ev_r_p128to255.ev_count += ENET_REG_READ(sc, ENET_RMON_R_P128TO255);
    505  1.1       ryo 	sc->sc_ev_r_p256to511.ev_count += ENET_REG_READ(sc, ENET_RMON_R_P256TO511);
    506  1.1       ryo 	sc->sc_ev_r_p512to1023.ev_count += ENET_REG_READ(sc, ENET_RMON_R_P512TO1023);
    507  1.1       ryo 	sc->sc_ev_r_p1024to2047.ev_count += ENET_REG_READ(sc, ENET_RMON_R_P1024TO2047);
    508  1.1       ryo 	sc->sc_ev_r_p_gte2048.ev_count += ENET_REG_READ(sc, ENET_RMON_R_P_GTE2048);
    509  1.1       ryo 	sc->sc_ev_r_octets.ev_count += ENET_REG_READ(sc, ENET_RMON_R_OCTETS);
    510  1.1       ryo }
    511  1.1       ryo #endif /* ENET_EVENT_COUNTER */
    512  1.1       ryo 
    513  1.1       ryo static void
    514  1.1       ryo enet_tick(void *arg)
    515  1.1       ryo {
    516  1.1       ryo 	struct enet_softc *sc;
    517  1.1       ryo 	struct mii_data *mii;
    518  1.1       ryo 	struct ifnet *ifp;
    519  1.1       ryo 	int s;
    520  1.1       ryo 
    521  1.1       ryo 	sc = arg;
    522  1.1       ryo 	mii = &sc->sc_mii;
    523  1.1       ryo 	ifp = &sc->sc_ethercom.ec_if;
    524  1.1       ryo 
    525  1.1       ryo 	s = splnet();
    526  1.1       ryo 
    527  1.1       ryo 	if (sc->sc_stopping)
    528  1.1       ryo 		goto out;
    529  1.1       ryo 
    530  1.1       ryo 
    531  1.1       ryo #ifdef ENET_EVENT_COUNTER
    532  1.1       ryo 	enet_update_evcnt(sc);
    533  1.1       ryo #endif
    534  1.1       ryo 
    535  1.1       ryo 	/* update counters */
    536  1.1       ryo 	ifp->if_ierrors += ENET_REG_READ(sc, ENET_RMON_R_UNDERSIZE);
    537  1.1       ryo 	ifp->if_ierrors += ENET_REG_READ(sc, ENET_RMON_R_FRAG);
    538  1.1       ryo 	ifp->if_ierrors += ENET_REG_READ(sc, ENET_RMON_R_JAB);
    539  1.1       ryo 
    540  1.1       ryo 	/* clear counters */
    541  1.1       ryo 	ENET_REG_WRITE(sc, ENET_MIBC, ENET_MIBC_MIB_CLEAR);
    542  1.1       ryo 	ENET_REG_WRITE(sc, ENET_MIBC, 0);
    543  1.1       ryo 
    544  1.1       ryo 	mii_tick(mii);
    545  1.1       ryo  out:
    546  1.1       ryo 
    547  1.1       ryo 	if (!sc->sc_stopping)
    548  1.1       ryo 		callout_schedule(&sc->sc_tick_ch, hz);
    549  1.1       ryo 
    550  1.1       ryo 	splx(s);
    551  1.1       ryo }
    552  1.1       ryo 
    553  1.1       ryo static int
    554  1.1       ryo enet_intr(void *arg)
    555  1.1       ryo {
    556  1.1       ryo 	struct enet_softc *sc;
    557  1.1       ryo 	struct ifnet *ifp;
    558  1.1       ryo 	uint32_t status;
    559  1.1       ryo 
    560  1.1       ryo 	sc = arg;
    561  1.1       ryo 	status = ENET_REG_READ(sc, ENET_EIR);
    562  1.1       ryo 
    563  1.1       ryo 	if (status & ENET_EIR_TXF)
    564  1.1       ryo 		enet_tx_intr(arg);
    565  1.1       ryo 
    566  1.1       ryo 	if (status & ENET_EIR_RXF)
    567  1.1       ryo 		enet_rx_intr(arg);
    568  1.1       ryo 
    569  1.1       ryo 	if (status & ENET_EIR_EBERR) {
    570  1.1       ryo 		device_printf(sc->sc_dev, "Ethernet Bus Error\n");
    571  1.1       ryo 		ifp = &sc->sc_ethercom.ec_if;
    572  1.1       ryo 		enet_stop(ifp, 1);
    573  1.1       ryo 		enet_init(ifp);
    574  1.1       ryo 	} else {
    575  1.1       ryo 		ENET_REG_WRITE(sc, ENET_EIR, status);
    576  1.1       ryo 	}
    577  1.1       ryo 
    578  1.1       ryo 	rnd_add_uint32(&sc->sc_rnd_source, status);
    579  1.1       ryo 
    580  1.1       ryo 	return 1;
    581  1.1       ryo }
    582  1.1       ryo 
    583  1.1       ryo static int
    584  1.1       ryo enet_tx_intr(void *arg)
    585  1.1       ryo {
    586  1.1       ryo 	struct enet_softc *sc;
    587  1.1       ryo 	struct ifnet *ifp;
    588  1.1       ryo 	struct enet_txsoft *txs;
    589  1.1       ryo 	int idx;
    590  1.1       ryo 
    591  1.1       ryo 	sc = (struct enet_softc *)arg;
    592  1.1       ryo 	ifp = &sc->sc_ethercom.ec_if;
    593  1.1       ryo 
    594  1.1       ryo 	for (idx = sc->sc_tx_considx; idx != sc->sc_tx_prodidx;
    595  1.1       ryo 	    idx = ENET_TX_NEXTIDX(idx)) {
    596  1.1       ryo 
    597  1.1       ryo 		txs = &sc->sc_txsoft[idx];
    598  1.1       ryo 
    599  1.1       ryo 		TXDESC_READIN(idx);
    600  1.1       ryo 		if (sc->sc_txdesc_ring[idx].tx_flags1_len & TXFLAGS1_R) {
    601  1.1       ryo 			/* This TX Descriptor has not been transmitted yet */
    602  1.1       ryo 			break;
    603  1.1       ryo 		}
    604  1.1       ryo 
    605  1.1       ryo 		/* txsoft is available on first segment (TXFLAGS1_T1) */
    606  1.1       ryo 		if (sc->sc_txdesc_ring[idx].tx_flags1_len & TXFLAGS1_T1) {
    607  1.1       ryo 			bus_dmamap_unload(sc->sc_dmat,
    608  1.1       ryo 			    txs->txs_dmamap);
    609  1.1       ryo 			m_freem(txs->txs_mbuf);
    610  1.1       ryo 			ifp->if_opackets++;
    611  1.1       ryo 		}
    612  1.1       ryo 
    613  1.1       ryo 		/* checking error */
    614  1.1       ryo 		if (sc->sc_txdesc_ring[idx].tx_flags1_len & TXFLAGS1_L) {
    615  1.1       ryo 			uint32_t flags2;
    616  1.1       ryo 
    617  1.1       ryo 			flags2 = sc->sc_txdesc_ring[idx].tx_flags2;
    618  1.1       ryo 
    619  1.1       ryo 			if (flags2 & (TXFLAGS2_TXE |
    620  1.1       ryo 			    TXFLAGS2_UE | TXFLAGS2_EE | TXFLAGS2_FE |
    621  1.1       ryo 			    TXFLAGS2_LCE | TXFLAGS2_OE | TXFLAGS2_TSE)) {
    622  1.1       ryo #ifdef DEBUG_ENET
    623  1.1       ryo 				if (enet_debug) {
    624  1.1       ryo 					char flagsbuf[128];
    625  1.1       ryo 
    626  1.1       ryo 					snprintb(flagsbuf, sizeof(flagsbuf),
    627  1.1       ryo 					    "\20" "\20TRANSMIT" "\16UNDERFLOW"
    628  1.1       ryo 					    "\15COLLISION" "\14FRAME"
    629  1.1       ryo 					    "\13LATECOLLISION" "\12OVERFLOW",
    630  1.1       ryo 					    flags2);
    631  1.1       ryo 
    632  1.1       ryo 					device_printf(sc->sc_dev,
    633  1.1       ryo 					    "txdesc[%d]: transmit error: "
    634  1.1       ryo 					    "flags2=%s\n", idx, flagsbuf);
    635  1.1       ryo 				}
    636  1.1       ryo #endif /* DEBUG_ENET */
    637  1.1       ryo 				ifp->if_oerrors++;
    638  1.1       ryo 			}
    639  1.1       ryo 		}
    640  1.1       ryo 
    641  1.1       ryo 		sc->sc_tx_free++;
    642  1.1       ryo 	}
    643  1.1       ryo 	sc->sc_tx_considx = idx;
    644  1.1       ryo 
    645  1.1       ryo 	if (sc->sc_tx_free > 0)
    646  1.1       ryo 		ifp->if_flags &= ~IFF_OACTIVE;
    647  1.1       ryo 
    648  1.1       ryo 	/*
    649  1.1       ryo 	 * No more pending TX descriptor,
    650  1.1       ryo 	 * cancel the watchdog timer.
    651  1.1       ryo 	 */
    652  1.1       ryo 	if (sc->sc_tx_free == ENET_TX_RING_CNT)
    653  1.1       ryo 		ifp->if_timer = 0;
    654  1.1       ryo 
    655  1.1       ryo 	return 1;
    656  1.1       ryo }
    657  1.1       ryo 
    658  1.1       ryo static int
    659  1.1       ryo enet_rx_intr(void *arg)
    660  1.1       ryo {
    661  1.1       ryo 	struct enet_softc *sc;
    662  1.1       ryo 	struct ifnet *ifp;
    663  1.1       ryo 	struct enet_rxsoft *rxs;
    664  1.1       ryo 	int idx, len, amount;
    665  1.1       ryo 	uint32_t flags1, flags2;
    666  1.1       ryo 	struct mbuf *m, *m0, *mprev;
    667  1.1       ryo 
    668  1.1       ryo 	sc = arg;
    669  1.1       ryo 	ifp = &sc->sc_ethercom.ec_if;
    670  1.1       ryo 
    671  1.1       ryo 	m0 = mprev = NULL;
    672  1.1       ryo 	amount = 0;
    673  1.1       ryo 	for (idx = sc->sc_rx_readidx; ; idx = ENET_RX_NEXTIDX(idx)) {
    674  1.1       ryo 
    675  1.1       ryo 		rxs = &sc->sc_rxsoft[idx];
    676  1.1       ryo 
    677  1.1       ryo 		RXDESC_READIN(idx);
    678  1.1       ryo 		if (sc->sc_rxdesc_ring[idx].rx_flags1_len & RXFLAGS1_E) {
    679  1.1       ryo 			/* This RX Descriptor has not been received yet */
    680  1.1       ryo 			break;
    681  1.1       ryo 		}
    682  1.1       ryo 
    683  1.1       ryo 		/*
    684  1.1       ryo 		 * build mbuf from RX Descriptor if needed
    685  1.1       ryo 		 */
    686  1.1       ryo 		m = rxs->rxs_mbuf;
    687  1.1       ryo 		rxs->rxs_mbuf = NULL;
    688  1.1       ryo 
    689  1.1       ryo 		flags1 = sc->sc_rxdesc_ring[idx].rx_flags1_len;
    690  1.1       ryo 		len = RXFLAGS1_LEN(flags1);
    691  1.1       ryo 
    692  1.1       ryo #define RACC_SHIFT16	2
    693  1.1       ryo 		if (m0 == NULL) {
    694  1.1       ryo 			m0 = m;
    695  1.1       ryo 			m_adj(m0, RACC_SHIFT16);
    696  1.1       ryo 			len -= RACC_SHIFT16;
    697  1.1       ryo 			m->m_len = len;
    698  1.1       ryo 			amount = len;
    699  1.1       ryo 		} else {
    700  1.1       ryo 			if (flags1 & RXFLAGS1_L)
    701  1.1       ryo 				len = len - amount - RACC_SHIFT16;
    702  1.1       ryo 
    703  1.1       ryo 			m->m_len = len;
    704  1.1       ryo 			amount += len;
    705  1.1       ryo 			m->m_flags &= ~M_PKTHDR;
    706  1.1       ryo 			mprev->m_next = m;
    707  1.1       ryo 		}
    708  1.1       ryo 		mprev = m;
    709  1.1       ryo 
    710  1.1       ryo 		flags2 = sc->sc_rxdesc_ring[idx].rx_flags2;
    711  1.1       ryo 
    712  1.1       ryo 		if (flags1 & RXFLAGS1_L) {
    713  1.1       ryo 			/* last buffer */
    714  1.1       ryo 			if ((amount < ETHER_HDR_LEN) ||
    715  1.1       ryo 			    ((flags1 & (RXFLAGS1_LG | RXFLAGS1_NO |
    716  1.1       ryo 			    RXFLAGS1_CR | RXFLAGS1_OV | RXFLAGS1_TR)) ||
    717  1.1       ryo 			    (flags2 & (RXFLAGS2_ME | RXFLAGS2_PE |
    718  1.1       ryo 			    RXFLAGS2_CE)))) {
    719  1.1       ryo 
    720  1.1       ryo #ifdef DEBUG_ENET
    721  1.1       ryo 				if (enet_debug) {
    722  1.1       ryo 					char flags1buf[128], flags2buf[128];
    723  1.1       ryo 					snprintb(flags1buf, sizeof(flags1buf),
    724  1.1       ryo 					    "\20" "\31MISS" "\26LENGTHVIOLATION"
    725  1.2       ryo 					    "\25NONOCTET" "\23CRC" "\22OVERRUN"
    726  1.1       ryo 					    "\21TRUNCATED", flags1);
    727  1.1       ryo 					snprintb(flags2buf, sizeof(flags2buf),
    728  1.1       ryo 					    "\20" "\40MAC" "\33PHY"
    729  1.1       ryo 					    "\32COLLISION", flags2);
    730  1.1       ryo 
    731  1.1       ryo 					DEVICE_DPRINTF(
    732  1.1       ryo 					    "rxdesc[%d]: receive error: "
    733  1.1       ryo 					    "flags1=%s,flags2=%s,len=%d\n",
    734  1.1       ryo 					    idx, flags1buf, flags2buf, amount);
    735  1.1       ryo 				}
    736  1.1       ryo #endif /* DEBUG_ENET */
    737  1.1       ryo 				ifp->if_ierrors++;
    738  1.1       ryo 				m_freem(m0);
    739  1.1       ryo 
    740  1.1       ryo 			} else {
    741  1.1       ryo 				/* packet receive ok */
    742  1.1       ryo 				ifp->if_ipackets++;
    743  1.1       ryo 				m0->m_pkthdr.rcvif = ifp;
    744  1.1       ryo 				m0->m_pkthdr.len = amount;
    745  1.1       ryo 
    746  1.1       ryo 				bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
    747  1.1       ryo 				    rxs->rxs_dmamap->dm_mapsize,
    748  1.1       ryo 				    BUS_DMASYNC_PREREAD);
    749  1.1       ryo 
    750  1.1       ryo 				if (ifp->if_csum_flags_rx & (M_CSUM_IPv4 |
    751  1.1       ryo 				    M_CSUM_TCPv4 | M_CSUM_UDPv4 |
    752  1.1       ryo 				    M_CSUM_TCPv6 | M_CSUM_UDPv6))
    753  1.1       ryo 					enet_rx_csum(sc, ifp, m0, idx);
    754  1.1       ryo 
    755  1.1       ryo 				/* Pass this up to any BPF listeners */
    756  1.1       ryo 				bpf_mtap(ifp, m0);
    757  1.1       ryo 
    758  1.5     ozaki 				if_percpuq_enqueue(ifp->if_percpuq, m0);
    759  1.1       ryo 			}
    760  1.1       ryo 
    761  1.1       ryo 			m0 = NULL;
    762  1.1       ryo 			mprev = NULL;
    763  1.1       ryo 			amount = 0;
    764  1.1       ryo 
    765  1.1       ryo 		} else {
    766  1.1       ryo 			/* continued from previous buffer */
    767  1.1       ryo 			bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
    768  1.1       ryo 			    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
    769  1.1       ryo 		}
    770  1.1       ryo 
    771  1.1       ryo 		bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
    772  1.1       ryo 		if (enet_alloc_rxbuf(sc, idx) != 0) {
    773  1.1       ryo 			panic("enet_alloc_rxbuf NULL\n");
    774  1.1       ryo 		}
    775  1.1       ryo 	}
    776  1.1       ryo 	sc->sc_rx_readidx = idx;
    777  1.1       ryo 
    778  1.1       ryo 	/* re-enable RX DMA to make sure */
    779  1.1       ryo 	ENET_REG_WRITE(sc, ENET_RDAR, ENET_RDAR_ACTIVE);
    780  1.1       ryo 
    781  1.1       ryo 	return 1;
    782  1.1       ryo }
    783  1.1       ryo 
    784  1.1       ryo static void
    785  1.1       ryo enet_rx_csum(struct enet_softc *sc, struct ifnet *ifp, struct mbuf *m, int idx)
    786  1.1       ryo {
    787  1.1       ryo 	uint32_t flags2;
    788  1.1       ryo 	uint8_t proto;
    789  1.1       ryo 
    790  1.1       ryo 	flags2 = sc->sc_rxdesc_ring[idx].rx_flags2;
    791  1.1       ryo 
    792  1.1       ryo 	if (flags2 & RXFLAGS2_IPV6) {
    793  1.1       ryo 		proto = sc->sc_rxdesc_ring[idx].rx_proto;
    794  1.1       ryo 
    795  1.1       ryo 		/* RXFLAGS2_PCR is valid when IPv6 and TCP/UDP */
    796  1.1       ryo 		if ((proto == IPPROTO_TCP) &&
    797  1.1       ryo 		    (ifp->if_csum_flags_rx & M_CSUM_TCPv6))
    798  1.1       ryo 			m->m_pkthdr.csum_flags |= M_CSUM_TCPv6;
    799  1.1       ryo 		else if ((proto == IPPROTO_UDP) &&
    800  1.1       ryo 		    (ifp->if_csum_flags_rx & M_CSUM_UDPv6))
    801  1.1       ryo 			m->m_pkthdr.csum_flags |= M_CSUM_UDPv6;
    802  1.1       ryo 		else
    803  1.1       ryo 			return;
    804  1.1       ryo 
    805  1.1       ryo 		/* IPv6 protocol checksum error */
    806  1.1       ryo 		if (flags2 & RXFLAGS2_PCR)
    807  1.1       ryo 			m->m_pkthdr.csum_flags |= M_CSUM_TCP_UDP_BAD;
    808  1.1       ryo 
    809  1.1       ryo 	} else {
    810  1.1       ryo 		struct ether_header *eh;
    811  1.1       ryo 		uint8_t *ip;
    812  1.1       ryo 
    813  1.1       ryo 		eh = mtod(m, struct ether_header *);
    814  1.1       ryo 
    815  1.1       ryo 		/* XXX: is an IPv4? */
    816  1.1       ryo 		if (ntohs(eh->ether_type) != ETHERTYPE_IP)
    817  1.1       ryo 			return;
    818  1.1       ryo 		ip = (uint8_t *)(eh + 1);
    819  1.1       ryo 		if ((ip[0] & 0xf0) == 0x40)
    820  1.1       ryo 			return;
    821  1.1       ryo 
    822  1.1       ryo 		proto = sc->sc_rxdesc_ring[idx].rx_proto;
    823  1.1       ryo 		if (flags2 & RXFLAGS2_ICE) {
    824  1.1       ryo 			if (ifp->if_csum_flags_rx & M_CSUM_IPv4) {
    825  1.1       ryo 				m->m_pkthdr.csum_flags |=
    826  1.1       ryo 				    M_CSUM_IPv4 | M_CSUM_IPv4_BAD;
    827  1.1       ryo 			}
    828  1.1       ryo 		} else {
    829  1.1       ryo 			if (ifp->if_csum_flags_rx & M_CSUM_IPv4) {
    830  1.1       ryo 				m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
    831  1.1       ryo 			}
    832  1.1       ryo 
    833  1.1       ryo 			/*
    834  1.1       ryo 			 * PCR is valid when
    835  1.1       ryo 			 * ICE == 0 and FRAG == 0
    836  1.1       ryo 			 */
    837  1.1       ryo 			if (flags2 & RXFLAGS2_FRAG)
    838  1.1       ryo 				return;
    839  1.1       ryo 
    840  1.1       ryo 			/*
    841  1.1       ryo 			 * PCR is valid when proto is TCP or UDP
    842  1.1       ryo 			 */
    843  1.1       ryo 			if ((proto == IPPROTO_TCP) &&
    844  1.1       ryo 			    (ifp->if_csum_flags_rx & M_CSUM_TCPv4))
    845  1.1       ryo 				m->m_pkthdr.csum_flags |= M_CSUM_TCPv4;
    846  1.1       ryo 			else if ((proto == IPPROTO_UDP) &&
    847  1.1       ryo 			    (ifp->if_csum_flags_rx & M_CSUM_UDPv4))
    848  1.1       ryo 				m->m_pkthdr.csum_flags |= M_CSUM_UDPv4;
    849  1.1       ryo 			else
    850  1.1       ryo 				return;
    851  1.1       ryo 
    852  1.1       ryo 			/* IPv4 protocol cksum error */
    853  1.1       ryo 			if (flags2 & RXFLAGS2_PCR)
    854  1.1       ryo 				m->m_pkthdr.csum_flags |= M_CSUM_TCP_UDP_BAD;
    855  1.1       ryo 		}
    856  1.1       ryo 	}
    857  1.1       ryo }
    858  1.1       ryo 
    859  1.1       ryo static void
    860  1.1       ryo enet_setmulti(struct enet_softc *sc)
    861  1.1       ryo {
    862  1.1       ryo 	struct ifnet *ifp;
    863  1.1       ryo 	struct ether_multi *enm;
    864  1.1       ryo 	struct ether_multistep step;
    865  1.1       ryo 	int promisc;
    866  1.1       ryo 	uint32_t crc;
    867  1.1       ryo 	uint32_t gaddr[2];
    868  1.1       ryo 
    869  1.1       ryo 	ifp = &sc->sc_ethercom.ec_if;
    870  1.1       ryo 
    871  1.1       ryo 	promisc = 0;
    872  1.1       ryo 	if ((ifp->if_flags & IFF_PROMISC) || sc->sc_ethercom.ec_multicnt > 0) {
    873  1.1       ryo 		ifp->if_flags |= IFF_ALLMULTI;
    874  1.1       ryo 		if (ifp->if_flags & IFF_PROMISC)
    875  1.1       ryo 			promisc = 1;
    876  1.1       ryo 		gaddr[0] = gaddr[1] = 0xffffffff;
    877  1.1       ryo 	} else {
    878  1.1       ryo 		gaddr[0] = gaddr[1] = 0;
    879  1.1       ryo 
    880  1.1       ryo 		ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
    881  1.1       ryo 		while (enm != NULL) {
    882  1.1       ryo 			crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
    883  1.1       ryo 			gaddr[crc >> 31] |= 1 << ((crc >> 26) & 0x1f);
    884  1.1       ryo 			ETHER_NEXT_MULTI(step, enm);
    885  1.1       ryo 		}
    886  1.1       ryo 	}
    887  1.1       ryo 
    888  1.1       ryo 	ENET_REG_WRITE(sc, ENET_GAUR, gaddr[0]);
    889  1.1       ryo 	ENET_REG_WRITE(sc, ENET_GALR, gaddr[1]);
    890  1.1       ryo 
    891  1.1       ryo 	if (promisc) {
    892  1.1       ryo 		/* match all packet */
    893  1.1       ryo 		ENET_REG_WRITE(sc, ENET_IAUR, 0xffffffff);
    894  1.1       ryo 		ENET_REG_WRITE(sc, ENET_IALR, 0xffffffff);
    895  1.1       ryo 	} else {
    896  1.1       ryo 		/* don't match any packet */
    897  1.1       ryo 		ENET_REG_WRITE(sc, ENET_IAUR, 0);
    898  1.1       ryo 		ENET_REG_WRITE(sc, ENET_IALR, 0);
    899  1.1       ryo 	}
    900  1.1       ryo }
    901  1.1       ryo 
    902  1.1       ryo static void
    903  1.1       ryo enet_ocotp_getmacaddr(uint8_t *macaddr)
    904  1.1       ryo {
    905  1.1       ryo #if NIMXOCOTP > 0
    906  1.1       ryo 	uint32_t addr;
    907  1.1       ryo 
    908  1.1       ryo 	addr = imxocotp_read(OCOTP_MAC1);
    909  1.1       ryo 	macaddr[0] = addr >> 8;
    910  1.1       ryo 	macaddr[1] = addr;
    911  1.1       ryo 
    912  1.1       ryo 	addr = imxocotp_read(OCOTP_MAC0);
    913  1.1       ryo 	macaddr[2] = addr >> 24;
    914  1.1       ryo 	macaddr[3] = addr >> 16;
    915  1.1       ryo 	macaddr[4] = addr >> 8;
    916  1.1       ryo 	macaddr[5] = addr;
    917  1.1       ryo #endif
    918  1.1       ryo }
    919  1.1       ryo 
    920  1.1       ryo static void
    921  1.1       ryo enet_gethwaddr(struct enet_softc *sc, uint8_t *hwaddr)
    922  1.1       ryo {
    923  1.1       ryo 	uint32_t paddr;
    924  1.1       ryo 
    925  1.1       ryo 	paddr = ENET_REG_READ(sc, ENET_PALR);
    926  1.1       ryo 	hwaddr[0] = paddr >> 24;
    927  1.1       ryo 	hwaddr[1] = paddr >> 16;
    928  1.1       ryo 	hwaddr[2] = paddr >> 8;
    929  1.1       ryo 	hwaddr[3] = paddr;
    930  1.1       ryo 
    931  1.1       ryo 	paddr = ENET_REG_READ(sc, ENET_PAUR);
    932  1.1       ryo 	hwaddr[4] = paddr >> 24;
    933  1.1       ryo 	hwaddr[5] = paddr >> 16;
    934  1.1       ryo }
    935  1.1       ryo 
    936  1.1       ryo static void
    937  1.1       ryo enet_sethwaddr(struct enet_softc *sc, uint8_t *hwaddr)
    938  1.1       ryo {
    939  1.1       ryo 	uint32_t paddr;
    940  1.1       ryo 
    941  1.1       ryo 	paddr = (hwaddr[0] << 24) | (hwaddr[1] << 16) | (hwaddr[2] << 8) |
    942  1.1       ryo 	    hwaddr[3];
    943  1.1       ryo 	ENET_REG_WRITE(sc, ENET_PALR, paddr);
    944  1.1       ryo 	paddr = (hwaddr[4] << 24) | (hwaddr[5] << 16);
    945  1.1       ryo 	ENET_REG_WRITE(sc, ENET_PAUR, paddr);
    946  1.1       ryo }
    947  1.1       ryo 
    948  1.1       ryo /*
    949  1.1       ryo  * ifnet interfaces
    950  1.1       ryo  */
    951  1.1       ryo static int
    952  1.1       ryo enet_init(struct ifnet *ifp)
    953  1.1       ryo {
    954  1.1       ryo 	struct enet_softc *sc;
    955  1.1       ryo 	int s, error;
    956  1.1       ryo 
    957  1.1       ryo 	sc = ifp->if_softc;
    958  1.1       ryo 
    959  1.1       ryo 	s = splnet();
    960  1.1       ryo 
    961  1.1       ryo 	enet_init_regs(sc, 0);
    962  1.1       ryo 	enet_init_txring(sc);
    963  1.1       ryo 	error = enet_init_rxring(sc);
    964  1.1       ryo 	if (error != 0) {
    965  1.1       ryo 		enet_drain_rxbuf(sc);
    966  1.1       ryo 		device_printf(sc->sc_dev, "Cannot allocate mbuf cluster\n");
    967  1.1       ryo 		goto init_failure;
    968  1.1       ryo 	}
    969  1.1       ryo 
    970  1.1       ryo 	/* reload mac address */
    971  1.1       ryo 	memcpy(sc->sc_enaddr, CLLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
    972  1.1       ryo 	enet_sethwaddr(sc, sc->sc_enaddr);
    973  1.1       ryo 
    974  1.1       ryo 	/* program multicast address */
    975  1.1       ryo 	enet_setmulti(sc);
    976  1.1       ryo 
    977  1.1       ryo 	/* update if_flags */
    978  1.1       ryo 	ifp->if_flags |= IFF_RUNNING;
    979  1.1       ryo 	ifp->if_flags &= ~IFF_OACTIVE;
    980  1.1       ryo 
    981  1.1       ryo 	/* update local copy of if_flags */
    982  1.1       ryo 	sc->sc_if_flags = ifp->if_flags;
    983  1.1       ryo 
    984  1.1       ryo 	/* mii */
    985  1.1       ryo 	mii_mediachg(&sc->sc_mii);
    986  1.1       ryo 
    987  1.1       ryo 	/* enable RX DMA */
    988  1.1       ryo 	ENET_REG_WRITE(sc, ENET_RDAR, ENET_RDAR_ACTIVE);
    989  1.1       ryo 
    990  1.1       ryo 	sc->sc_stopping = false;
    991  1.2       ryo 	callout_schedule(&sc->sc_tick_ch, hz);
    992  1.1       ryo 
    993  1.1       ryo  init_failure:
    994  1.1       ryo 	splx(s);
    995  1.1       ryo 
    996  1.1       ryo 	return error;
    997  1.1       ryo }
    998  1.1       ryo 
    999  1.1       ryo static void
   1000  1.1       ryo enet_start(struct ifnet *ifp)
   1001  1.1       ryo {
   1002  1.1       ryo 	struct enet_softc *sc;
   1003  1.1       ryo 	struct mbuf *m;
   1004  1.1       ryo 	int npkt;
   1005  1.1       ryo 
   1006  1.1       ryo 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
   1007  1.1       ryo 		return;
   1008  1.1       ryo 
   1009  1.1       ryo 	sc = ifp->if_softc;
   1010  1.1       ryo 	for (npkt = 0; ; npkt++) {
   1011  1.1       ryo 		IFQ_POLL(&ifp->if_snd, m);
   1012  1.1       ryo 		if (m == NULL)
   1013  1.1       ryo 			break;
   1014  1.1       ryo 
   1015  1.1       ryo 		if (sc->sc_tx_free <= 0) {
   1016  1.1       ryo 			/* no tx descriptor now... */
   1017  1.1       ryo 			ifp->if_flags |= IFF_OACTIVE;
   1018  1.1       ryo 			DEVICE_DPRINTF("TX descriptor is full\n");
   1019  1.1       ryo 			break;
   1020  1.1       ryo 		}
   1021  1.1       ryo 
   1022  1.1       ryo 		IFQ_DEQUEUE(&ifp->if_snd, m);
   1023  1.1       ryo 
   1024  1.1       ryo 		if (enet_encap_txring(sc, &m) != 0) {
   1025  1.1       ryo 			/* too many mbuf chains? */
   1026  1.1       ryo 			ifp->if_flags |= IFF_OACTIVE;
   1027  1.1       ryo 			DEVICE_DPRINTF(
   1028  1.1       ryo 			    "TX descriptor is full. dropping packet\n");
   1029  1.1       ryo 			m_freem(m);
   1030  1.1       ryo 			ifp->if_oerrors++;
   1031  1.1       ryo 			break;
   1032  1.1       ryo 		}
   1033  1.1       ryo 
   1034  1.1       ryo 		/* Pass the packet to any BPF listeners */
   1035  1.1       ryo 		bpf_mtap(ifp, m);
   1036  1.1       ryo 	}
   1037  1.1       ryo 
   1038  1.1       ryo 	if (npkt) {
   1039  1.1       ryo 		/* enable TX DMA */
   1040  1.1       ryo 		ENET_REG_WRITE(sc, ENET_TDAR, ENET_TDAR_ACTIVE);
   1041  1.1       ryo 
   1042  1.1       ryo 		ifp->if_timer = 5;
   1043  1.1       ryo 	}
   1044  1.1       ryo }
   1045  1.1       ryo 
   1046  1.1       ryo static void
   1047  1.1       ryo enet_stop(struct ifnet *ifp, int disable)
   1048  1.1       ryo {
   1049  1.1       ryo 	struct enet_softc *sc;
   1050  1.1       ryo 	int s;
   1051  1.1       ryo 	uint32_t v;
   1052  1.1       ryo 
   1053  1.1       ryo 	sc = ifp->if_softc;
   1054  1.1       ryo 
   1055  1.1       ryo 	s = splnet();
   1056  1.1       ryo 
   1057  1.1       ryo 	sc->sc_stopping = true;
   1058  1.1       ryo 	callout_stop(&sc->sc_tick_ch);
   1059  1.1       ryo 
   1060  1.1       ryo 	/* clear ENET_ECR[ETHEREN] to abort receive and transmit */
   1061  1.1       ryo 	v = ENET_REG_READ(sc, ENET_ECR);
   1062  1.1       ryo 	ENET_REG_WRITE(sc, ENET_ECR, v & ~ENET_ECR_ETHEREN);
   1063  1.1       ryo 
   1064  1.1       ryo 	/* Mark the interface as down and cancel the watchdog timer. */
   1065  1.1       ryo 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1066  1.1       ryo 	ifp->if_timer = 0;
   1067  1.1       ryo 
   1068  1.1       ryo 	if (disable) {
   1069  1.1       ryo 		enet_drain_txbuf(sc);
   1070  1.1       ryo 		enet_drain_rxbuf(sc);
   1071  1.1       ryo 	}
   1072  1.1       ryo 
   1073  1.1       ryo 	splx(s);
   1074  1.1       ryo }
   1075  1.1       ryo 
   1076  1.1       ryo static void
   1077  1.1       ryo enet_watchdog(struct ifnet *ifp)
   1078  1.1       ryo {
   1079  1.1       ryo 	struct enet_softc *sc;
   1080  1.1       ryo 	int s;
   1081  1.1       ryo 
   1082  1.1       ryo 	sc = ifp->if_softc;
   1083  1.1       ryo 	s = splnet();
   1084  1.1       ryo 
   1085  1.1       ryo 	device_printf(sc->sc_dev, "watchdog timeout\n");
   1086  1.1       ryo 	ifp->if_oerrors++;
   1087  1.1       ryo 
   1088  1.1       ryo 	/* salvage packets left in descriptors */
   1089  1.1       ryo 	enet_tx_intr(sc);
   1090  1.1       ryo 	enet_rx_intr(sc);
   1091  1.1       ryo 
   1092  1.1       ryo 	/* reset */
   1093  1.1       ryo 	enet_stop(ifp, 1);
   1094  1.1       ryo 	enet_init(ifp);
   1095  1.1       ryo 
   1096  1.1       ryo 	splx(s);
   1097  1.1       ryo }
   1098  1.1       ryo 
   1099  1.1       ryo static void
   1100  1.1       ryo enet_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
   1101  1.1       ryo {
   1102  1.1       ryo 	struct enet_softc *sc = ifp->if_softc;
   1103  1.1       ryo 
   1104  1.1       ryo 	ether_mediastatus(ifp, ifmr);
   1105  1.1       ryo 	ifmr->ifm_active = (ifmr->ifm_active & ~IFM_ETH_FMASK)
   1106  1.1       ryo 	    | sc->sc_flowflags;
   1107  1.1       ryo }
   1108  1.1       ryo 
   1109  1.1       ryo static int
   1110  1.1       ryo enet_ifflags_cb(struct ethercom *ec)
   1111  1.1       ryo {
   1112  1.1       ryo 	struct ifnet *ifp = &ec->ec_if;
   1113  1.1       ryo 	struct enet_softc *sc = ifp->if_softc;
   1114  1.1       ryo 	int change = ifp->if_flags ^ sc->sc_if_flags;
   1115  1.1       ryo 
   1116  1.1       ryo 	if ((change & ~(IFF_CANTCHANGE | IFF_DEBUG)) != 0)
   1117  1.1       ryo 		return ENETRESET;
   1118  1.1       ryo 	else if ((change & (IFF_PROMISC | IFF_ALLMULTI)) == 0)
   1119  1.1       ryo 		return 0;
   1120  1.1       ryo 
   1121  1.1       ryo 	enet_setmulti(sc);
   1122  1.1       ryo 
   1123  1.1       ryo 	sc->sc_if_flags = ifp->if_flags;
   1124  1.1       ryo 	return 0;
   1125  1.1       ryo }
   1126  1.1       ryo 
   1127  1.1       ryo static int
   1128  1.1       ryo enet_ioctl(struct ifnet *ifp, u_long command, void *data)
   1129  1.1       ryo {
   1130  1.1       ryo 	struct enet_softc *sc;
   1131  1.1       ryo 	struct ifreq *ifr;
   1132  1.1       ryo 	int s, error;
   1133  1.1       ryo 	uint32_t v;
   1134  1.1       ryo 
   1135  1.1       ryo 	sc = ifp->if_softc;
   1136  1.1       ryo 	ifr = data;
   1137  1.1       ryo 
   1138  1.1       ryo 	error = 0;
   1139  1.1       ryo 
   1140  1.1       ryo 	s = splnet();
   1141  1.1       ryo 
   1142  1.1       ryo 	switch (command) {
   1143  1.1       ryo 	case SIOCSIFMTU:
   1144  1.1       ryo 		if (MTU2FRAMESIZE(ifr->ifr_mtu) > ENET_MAX_PKT_LEN) {
   1145  1.1       ryo 			error = EINVAL;
   1146  1.1       ryo 		} else {
   1147  1.1       ryo 			ifp->if_mtu = ifr->ifr_mtu;
   1148  1.1       ryo 
   1149  1.1       ryo 			/* set maximum frame length */
   1150  1.1       ryo 			v = MTU2FRAMESIZE(ifr->ifr_mtu);
   1151  1.1       ryo 			ENET_REG_WRITE(sc, ENET_FTRL, v);
   1152  1.1       ryo 			v = ENET_REG_READ(sc, ENET_RCR);
   1153  1.1       ryo 			v &= ~ENET_RCR_MAX_FL(0x3fff);
   1154  1.1       ryo 			v |= ENET_RCR_MAX_FL(ifp->if_mtu +
   1155  1.1       ryo 			    ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN);
   1156  1.1       ryo 			ENET_REG_WRITE(sc, ENET_RCR, v);
   1157  1.1       ryo 		}
   1158  1.1       ryo 		break;
   1159  1.1       ryo 	case SIOCSIFMEDIA:
   1160  1.1       ryo 	case SIOCGIFMEDIA:
   1161  1.1       ryo 		/* Flow control requires full-duplex mode. */
   1162  1.1       ryo 		if (IFM_SUBTYPE(ifr->ifr_media) == IFM_AUTO ||
   1163  1.1       ryo 		    (ifr->ifr_media & IFM_FDX) == 0)
   1164  1.1       ryo 			ifr->ifr_media &= ~IFM_ETH_FMASK;
   1165  1.1       ryo 		if (IFM_SUBTYPE(ifr->ifr_media) != IFM_AUTO) {
   1166  1.1       ryo 			if ((ifr->ifr_media & IFM_ETH_FMASK) == IFM_FLOW) {
   1167  1.1       ryo 				/* We can do both TXPAUSE and RXPAUSE. */
   1168  1.1       ryo 				ifr->ifr_media |=
   1169  1.1       ryo 				    IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE;
   1170  1.1       ryo 			}
   1171  1.1       ryo 			sc->sc_flowflags = ifr->ifr_media & IFM_ETH_FMASK;
   1172  1.1       ryo 		}
   1173  1.1       ryo 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, command);
   1174  1.1       ryo 		break;
   1175  1.1       ryo 	default:
   1176  1.1       ryo 		error = ether_ioctl(ifp, command, data);
   1177  1.1       ryo 		if (error != ENETRESET)
   1178  1.1       ryo 			break;
   1179  1.1       ryo 
   1180  1.1       ryo 		/* post-process */
   1181  1.1       ryo 		error = 0;
   1182  1.1       ryo 		switch (command) {
   1183  1.1       ryo 		case SIOCSIFCAP:
   1184  1.1       ryo 			error = (*ifp->if_init)(ifp);
   1185  1.1       ryo 			break;
   1186  1.1       ryo 		case SIOCADDMULTI:
   1187  1.1       ryo 		case SIOCDELMULTI:
   1188  1.1       ryo 			if (ifp->if_flags & IFF_RUNNING)
   1189  1.1       ryo 				enet_setmulti(sc);
   1190  1.1       ryo 			break;
   1191  1.1       ryo 		}
   1192  1.1       ryo 		break;
   1193  1.1       ryo 	}
   1194  1.1       ryo 
   1195  1.1       ryo 	splx(s);
   1196  1.1       ryo 
   1197  1.1       ryo 	return error;
   1198  1.1       ryo }
   1199  1.1       ryo 
   1200  1.1       ryo /*
   1201  1.1       ryo  * for MII
   1202  1.1       ryo  */
   1203  1.1       ryo static int
   1204  1.1       ryo enet_miibus_readreg(device_t dev, int phy, int reg)
   1205  1.1       ryo {
   1206  1.1       ryo 	struct enet_softc *sc;
   1207  1.1       ryo 	int timeout;
   1208  1.1       ryo 	uint32_t val, status;
   1209  1.1       ryo 
   1210  1.1       ryo 	sc = device_private(dev);
   1211  1.1       ryo 
   1212  1.1       ryo 	/* clear MII update */
   1213  1.1       ryo 	ENET_REG_WRITE(sc, ENET_EIR, ENET_EIR_MII);
   1214  1.1       ryo 
   1215  1.1       ryo 	/* read command */
   1216  1.1       ryo 	ENET_REG_WRITE(sc, ENET_MMFR,
   1217  1.1       ryo 	    ENET_MMFR_ST | ENET_MMFR_OP_READ | ENET_MMFR_TA |
   1218  1.1       ryo 	    ENET_MMFR_PHY_REG(reg) | ENET_MMFR_PHY_ADDR(phy));
   1219  1.1       ryo 
   1220  1.1       ryo 	/* check MII update */
   1221  1.1       ryo 	for (timeout = 5000; timeout > 0; --timeout) {
   1222  1.1       ryo 		status = ENET_REG_READ(sc, ENET_EIR);
   1223  1.1       ryo 		if (status & ENET_EIR_MII)
   1224  1.1       ryo 			break;
   1225  1.1       ryo 	}
   1226  1.1       ryo 	if (timeout <= 0) {
   1227  1.1       ryo 		DEVICE_DPRINTF("MII read timeout: reg=0x%02x\n",
   1228  1.1       ryo 		    reg);
   1229  1.1       ryo 		val = -1;
   1230  1.1       ryo 	} else {
   1231  1.1       ryo 		val = ENET_REG_READ(sc, ENET_MMFR) & ENET_MMFR_DATAMASK;
   1232  1.1       ryo 	}
   1233  1.1       ryo 
   1234  1.1       ryo 	return val;
   1235  1.1       ryo }
   1236  1.1       ryo 
   1237  1.1       ryo static void
   1238  1.1       ryo enet_miibus_writereg(device_t dev, int phy, int reg, int val)
   1239  1.1       ryo {
   1240  1.1       ryo 	struct enet_softc *sc;
   1241  1.1       ryo 	int timeout;
   1242  1.1       ryo 
   1243  1.1       ryo 	sc = device_private(dev);
   1244  1.1       ryo 
   1245  1.1       ryo 	/* clear MII update */
   1246  1.1       ryo 	ENET_REG_WRITE(sc, ENET_EIR, ENET_EIR_MII);
   1247  1.1       ryo 
   1248  1.1       ryo 	/* write command */
   1249  1.1       ryo 	ENET_REG_WRITE(sc, ENET_MMFR,
   1250  1.1       ryo 	    ENET_MMFR_ST | ENET_MMFR_OP_WRITE | ENET_MMFR_TA |
   1251  1.1       ryo 	    ENET_MMFR_PHY_REG(reg) | ENET_MMFR_PHY_ADDR(phy) |
   1252  1.1       ryo 	    (ENET_MMFR_DATAMASK & val));
   1253  1.1       ryo 
   1254  1.1       ryo 	/* check MII update */
   1255  1.1       ryo 	for (timeout = 5000; timeout > 0; --timeout) {
   1256  1.1       ryo 		if (ENET_REG_READ(sc, ENET_EIR) & ENET_EIR_MII)
   1257  1.1       ryo 			break;
   1258  1.1       ryo 	}
   1259  1.1       ryo 	if (timeout <= 0) {
   1260  1.1       ryo 		DEVICE_DPRINTF("MII write timeout: reg=0x%02x\n",
   1261  1.1       ryo 		    reg);
   1262  1.1       ryo 	}
   1263  1.1       ryo }
   1264  1.1       ryo 
   1265  1.1       ryo static void
   1266  1.1       ryo enet_miibus_statchg(struct ifnet *ifp)
   1267  1.1       ryo {
   1268  1.1       ryo 	struct enet_softc *sc;
   1269  1.1       ryo 	struct mii_data *mii;
   1270  1.1       ryo 	struct ifmedia_entry *ife;
   1271  1.1       ryo 	uint32_t ecr, ecr0;
   1272  1.1       ryo 	uint32_t rcr, rcr0;
   1273  1.1       ryo 	uint32_t tcr, tcr0;
   1274  1.1       ryo 
   1275  1.1       ryo 	sc = ifp->if_softc;
   1276  1.1       ryo 	mii = &sc->sc_mii;
   1277  1.1       ryo 	ife = mii->mii_media.ifm_cur;
   1278  1.1       ryo 
   1279  1.1       ryo 	/* get current status */
   1280  1.1       ryo 	ecr0 = ecr = ENET_REG_READ(sc, ENET_ECR) & ~ENET_ECR_RESET;
   1281  1.1       ryo 	rcr0 = rcr = ENET_REG_READ(sc, ENET_RCR);
   1282  1.1       ryo 	tcr0 = tcr = ENET_REG_READ(sc, ENET_TCR);
   1283  1.1       ryo 
   1284  1.1       ryo 	if (IFM_SUBTYPE(mii->mii_media.ifm_cur->ifm_media) == IFM_AUTO &&
   1285  1.1       ryo 	    (mii->mii_media_active & IFM_ETH_FMASK) != sc->sc_flowflags) {
   1286  1.1       ryo 		sc->sc_flowflags = mii->mii_media_active & IFM_ETH_FMASK;
   1287  1.1       ryo 		mii->mii_media_active &= ~IFM_ETH_FMASK;
   1288  1.1       ryo 	}
   1289  1.1       ryo 
   1290  1.1       ryo 	if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) {
   1291  1.1       ryo 		tcr |= ENET_TCR_FDEN;	/* full duplex */
   1292  1.1       ryo 		rcr &= ~ENET_RCR_DRT;;	/* enable receive on transmit */
   1293  1.1       ryo 	} else {
   1294  1.1       ryo 		tcr &= ~ENET_TCR_FDEN;	/* half duplex */
   1295  1.1       ryo 		rcr |= ENET_RCR_DRT;	/* disable receive on transmit */
   1296  1.1       ryo 	}
   1297  1.1       ryo 
   1298  1.1       ryo 	if ((tcr ^ tcr0) & ENET_TCR_FDEN) {
   1299  1.1       ryo 		/*
   1300  1.1       ryo 		 * need to reset because
   1301  1.1       ryo 		 * FDEN can change when ECR[ETHEREN] is 0
   1302  1.1       ryo 		 */
   1303  1.1       ryo 		enet_init_regs(sc, 0);
   1304  1.1       ryo 		return;
   1305  1.1       ryo 	}
   1306  1.1       ryo 
   1307  1.1       ryo 	switch (IFM_SUBTYPE(ife->ifm_media)) {
   1308  1.1       ryo 	case IFM_AUTO:
   1309  1.1       ryo 	case IFM_1000_T:
   1310  1.1       ryo 		ecr |= ENET_ECR_SPEED;		/* 1000Mbps mode */
   1311  1.1       ryo 		break;
   1312  1.1       ryo 	case IFM_100_TX:
   1313  1.1       ryo 		ecr &= ~ENET_ECR_SPEED;		/* 100Mbps mode */
   1314  1.1       ryo 		rcr &= ~ENET_RCR_RMII_10T;	/* 100Mbps mode */
   1315  1.1       ryo 		break;
   1316  1.1       ryo 	case IFM_10_T:
   1317  1.1       ryo 		ecr &= ~ENET_ECR_SPEED;		/* 10Mbps mode */
   1318  1.1       ryo 		rcr |= ENET_RCR_RMII_10T;	/* 10Mbps mode */
   1319  1.1       ryo 		break;
   1320  1.1       ryo 	default:
   1321  1.1       ryo 		ecr = ecr0;
   1322  1.1       ryo 		rcr = rcr0;
   1323  1.1       ryo 		tcr = tcr0;
   1324  1.1       ryo 		break;
   1325  1.1       ryo 	}
   1326  1.1       ryo 
   1327  1.1       ryo 	if (sc->sc_flowflags & IFM_FLOW)
   1328  1.1       ryo 		rcr |= ENET_RCR_FCE;
   1329  1.1       ryo 	else
   1330  1.1       ryo 		rcr &= ~ENET_RCR_FCE;
   1331  1.1       ryo 
   1332  1.1       ryo 	/* update registers if need change */
   1333  1.1       ryo 	if (ecr != ecr0)
   1334  1.1       ryo 		ENET_REG_WRITE(sc, ENET_ECR, ecr);
   1335  1.1       ryo 	if (rcr != rcr0)
   1336  1.1       ryo 		ENET_REG_WRITE(sc, ENET_RCR, rcr);
   1337  1.1       ryo 	if (tcr != tcr0)
   1338  1.1       ryo 		ENET_REG_WRITE(sc, ENET_TCR, tcr);
   1339  1.1       ryo }
   1340  1.1       ryo 
   1341  1.1       ryo /*
   1342  1.1       ryo  * handling descriptors
   1343  1.1       ryo  */
   1344  1.1       ryo static void
   1345  1.1       ryo enet_init_txring(struct enet_softc *sc)
   1346  1.1       ryo {
   1347  1.1       ryo 	int i;
   1348  1.1       ryo 
   1349  1.1       ryo 	/* build TX ring */
   1350  1.1       ryo 	for (i = 0; i < ENET_TX_RING_CNT; i++) {
   1351  1.1       ryo 		sc->sc_txdesc_ring[i].tx_flags1_len =
   1352  1.1       ryo 		    ((i == (ENET_TX_RING_CNT - 1)) ? TXFLAGS1_W : 0);
   1353  1.1       ryo 		sc->sc_txdesc_ring[i].tx_databuf = 0;
   1354  1.1       ryo 		sc->sc_txdesc_ring[i].tx_flags2 = TXFLAGS2_INT;
   1355  1.1       ryo 		sc->sc_txdesc_ring[i].tx__reserved1 = 0;
   1356  1.1       ryo 		sc->sc_txdesc_ring[i].tx_flags3 = 0;
   1357  1.1       ryo 		sc->sc_txdesc_ring[i].tx_1588timestamp = 0;
   1358  1.1       ryo 		sc->sc_txdesc_ring[i].tx__reserved2 = 0;
   1359  1.1       ryo 		sc->sc_txdesc_ring[i].tx__reserved3 = 0;
   1360  1.1       ryo 
   1361  1.1       ryo 		TXDESC_WRITEOUT(i);
   1362  1.1       ryo 	}
   1363  1.1       ryo 
   1364  1.1       ryo 	sc->sc_tx_free = ENET_TX_RING_CNT;
   1365  1.1       ryo 	sc->sc_tx_considx = 0;
   1366  1.1       ryo 	sc->sc_tx_prodidx = 0;
   1367  1.1       ryo }
   1368  1.1       ryo 
   1369  1.1       ryo static int
   1370  1.1       ryo enet_init_rxring(struct enet_softc *sc)
   1371  1.1       ryo {
   1372  1.1       ryo 	int i, error;
   1373  1.1       ryo 
   1374  1.1       ryo 	/* build RX ring */
   1375  1.1       ryo 	for (i = 0; i < ENET_RX_RING_CNT; i++) {
   1376  1.1       ryo 		error = enet_alloc_rxbuf(sc, i);
   1377  1.1       ryo 		if (error != 0)
   1378  1.1       ryo 			return error;
   1379  1.1       ryo 	}
   1380  1.1       ryo 
   1381  1.1       ryo 	sc->sc_rx_readidx = 0;
   1382  1.1       ryo 
   1383  1.1       ryo 	return 0;
   1384  1.1       ryo }
   1385  1.1       ryo 
   1386  1.1       ryo static int
   1387  1.1       ryo enet_alloc_rxbuf(struct enet_softc *sc, int idx)
   1388  1.1       ryo {
   1389  1.1       ryo 	struct mbuf *m;
   1390  1.1       ryo 	int error;
   1391  1.1       ryo 
   1392  1.1       ryo 	KASSERT((idx >= 0) && (idx < ENET_RX_RING_CNT));
   1393  1.1       ryo 
   1394  1.1       ryo 	/* free mbuf if already allocated */
   1395  1.1       ryo 	if (sc->sc_rxsoft[idx].rxs_mbuf != NULL) {
   1396  1.1       ryo 		bus_dmamap_unload(sc->sc_dmat, sc->sc_rxsoft[idx].rxs_dmamap);
   1397  1.1       ryo 		m_freem(sc->sc_rxsoft[idx].rxs_mbuf);
   1398  1.1       ryo 		sc->sc_rxsoft[idx].rxs_mbuf = NULL;
   1399  1.1       ryo 	}
   1400  1.1       ryo 
   1401  1.1       ryo 	/* allocate new mbuf cluster */
   1402  1.1       ryo 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1403  1.1       ryo 	if (m == NULL)
   1404  1.1       ryo 		return ENOBUFS;
   1405  1.1       ryo 	MCLGET(m, M_DONTWAIT);
   1406  1.1       ryo 	if (!(m->m_flags & M_EXT)) {
   1407  1.1       ryo 		m_freem(m);
   1408  1.1       ryo 		return ENOBUFS;
   1409  1.1       ryo 	}
   1410  1.1       ryo 	m->m_len = MCLBYTES;
   1411  1.1       ryo 	m->m_next = NULL;
   1412  1.1       ryo 
   1413  1.1       ryo 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_rxsoft[idx].rxs_dmamap,
   1414  1.1       ryo 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL,
   1415  1.1       ryo 	    BUS_DMA_READ | BUS_DMA_NOWAIT);
   1416  1.4  christos 	if (error) {
   1417  1.4  christos 		m_freem(m);
   1418  1.1       ryo 		return error;
   1419  1.4  christos 	}
   1420  1.1       ryo 
   1421  1.1       ryo 	bus_dmamap_sync(sc->sc_dmat, sc->sc_rxsoft[idx].rxs_dmamap, 0,
   1422  1.1       ryo 	    sc->sc_rxsoft[idx].rxs_dmamap->dm_mapsize,
   1423  1.1       ryo 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1424  1.1       ryo 
   1425  1.1       ryo 	sc->sc_rxsoft[idx].rxs_mbuf = m;
   1426  1.1       ryo 	enet_reset_rxdesc(sc, idx);
   1427  1.1       ryo 	return 0;
   1428  1.1       ryo }
   1429  1.1       ryo 
   1430  1.1       ryo static void
   1431  1.1       ryo enet_reset_rxdesc(struct enet_softc *sc, int idx)
   1432  1.1       ryo {
   1433  1.1       ryo 	uint32_t paddr;
   1434  1.1       ryo 
   1435  1.1       ryo 	paddr = sc->sc_rxsoft[idx].rxs_dmamap->dm_segs[0].ds_addr;
   1436  1.1       ryo 
   1437  1.1       ryo 	sc->sc_rxdesc_ring[idx].rx_flags1_len =
   1438  1.1       ryo 	    RXFLAGS1_E |
   1439  1.1       ryo 	    ((idx == (ENET_RX_RING_CNT - 1)) ? RXFLAGS1_W : 0);
   1440  1.1       ryo 	sc->sc_rxdesc_ring[idx].rx_databuf = paddr;
   1441  1.1       ryo 	sc->sc_rxdesc_ring[idx].rx_flags2 =
   1442  1.1       ryo 	    RXFLAGS2_INT;
   1443  1.1       ryo 	sc->sc_rxdesc_ring[idx].rx_hl = 0;
   1444  1.1       ryo 	sc->sc_rxdesc_ring[idx].rx_proto = 0;
   1445  1.1       ryo 	sc->sc_rxdesc_ring[idx].rx_cksum = 0;
   1446  1.1       ryo 	sc->sc_rxdesc_ring[idx].rx_flags3 = 0;
   1447  1.1       ryo 	sc->sc_rxdesc_ring[idx].rx_1588timestamp = 0;
   1448  1.1       ryo 	sc->sc_rxdesc_ring[idx].rx__reserved2 = 0;
   1449  1.1       ryo 	sc->sc_rxdesc_ring[idx].rx__reserved3 = 0;
   1450  1.1       ryo 
   1451  1.1       ryo 	RXDESC_WRITEOUT(idx);
   1452  1.1       ryo }
   1453  1.1       ryo 
   1454  1.1       ryo static void
   1455  1.1       ryo enet_drain_txbuf(struct enet_softc *sc)
   1456  1.1       ryo {
   1457  1.1       ryo 	int idx;
   1458  1.1       ryo 	struct enet_txsoft *txs;
   1459  1.1       ryo 	struct ifnet *ifp;
   1460  1.1       ryo 
   1461  1.1       ryo 	ifp = &sc->sc_ethercom.ec_if;
   1462  1.1       ryo 
   1463  1.1       ryo 	for (idx = sc->sc_tx_considx; idx != sc->sc_tx_prodidx;
   1464  1.1       ryo 	    idx = ENET_TX_NEXTIDX(idx)) {
   1465  1.1       ryo 
   1466  1.1       ryo 		/* txsoft[] is used only first segment */
   1467  1.1       ryo 		txs = &sc->sc_txsoft[idx];
   1468  1.1       ryo 		TXDESC_READIN(idx);
   1469  1.1       ryo 		if (sc->sc_txdesc_ring[idx].tx_flags1_len & TXFLAGS1_T1) {
   1470  1.1       ryo 			sc->sc_txdesc_ring[idx].tx_flags1_len = 0;
   1471  1.1       ryo 			bus_dmamap_unload(sc->sc_dmat,
   1472  1.1       ryo 			    txs->txs_dmamap);
   1473  1.1       ryo 			m_freem(txs->txs_mbuf);
   1474  1.1       ryo 
   1475  1.1       ryo 			ifp->if_oerrors++;
   1476  1.1       ryo 		}
   1477  1.1       ryo 		sc->sc_tx_free++;
   1478  1.1       ryo 	}
   1479  1.1       ryo }
   1480  1.1       ryo 
   1481  1.1       ryo static void
   1482  1.1       ryo enet_drain_rxbuf(struct enet_softc *sc)
   1483  1.1       ryo {
   1484  1.1       ryo 	int i;
   1485  1.1       ryo 
   1486  1.1       ryo 	for (i = 0; i < ENET_RX_RING_CNT; i++) {
   1487  1.1       ryo 		if (sc->sc_rxsoft[i].rxs_mbuf != NULL) {
   1488  1.1       ryo 			sc->sc_rxdesc_ring[i].rx_flags1_len = 0;
   1489  1.1       ryo 			bus_dmamap_unload(sc->sc_dmat,
   1490  1.1       ryo 			    sc->sc_rxsoft[i].rxs_dmamap);
   1491  1.1       ryo 			m_freem(sc->sc_rxsoft[i].rxs_mbuf);
   1492  1.1       ryo 			sc->sc_rxsoft[i].rxs_mbuf = NULL;
   1493  1.1       ryo 		}
   1494  1.1       ryo 	}
   1495  1.1       ryo }
   1496  1.1       ryo 
   1497  1.1       ryo static int
   1498  1.1       ryo enet_alloc_ring(struct enet_softc *sc)
   1499  1.1       ryo {
   1500  1.1       ryo 	int i, error;
   1501  1.1       ryo 
   1502  1.1       ryo 	/*
   1503  1.1       ryo 	 * build DMA maps for TX.
   1504  1.1       ryo 	 * TX descriptor must be able to contain mbuf chains,
   1505  1.1       ryo 	 * so, make up ENET_MAX_PKT_NSEGS dmamap.
   1506  1.1       ryo 	 */
   1507  1.1       ryo 	for (i = 0; i < ENET_TX_RING_CNT; i++) {
   1508  1.1       ryo 		error = bus_dmamap_create(sc->sc_dmat, ENET_MAX_PKT_LEN,
   1509  1.1       ryo 		    ENET_MAX_PKT_NSEGS, ENET_MAX_PKT_LEN, 0, BUS_DMA_NOWAIT,
   1510  1.1       ryo 		    &sc->sc_txsoft[i].txs_dmamap);
   1511  1.1       ryo 
   1512  1.1       ryo 		if (error) {
   1513  1.1       ryo 			aprint_error_dev(sc->sc_dev,
   1514  1.1       ryo 			    "can't create DMA map for TX descs\n");
   1515  1.1       ryo 			goto fail_1;
   1516  1.1       ryo 		}
   1517  1.1       ryo 	}
   1518  1.1       ryo 
   1519  1.1       ryo 	/*
   1520  1.1       ryo 	 * build DMA maps for RX.
   1521  1.1       ryo 	 * RX descripter contains An mbuf cluster,
   1522  1.1       ryo 	 * and make up a dmamap.
   1523  1.1       ryo 	 */
   1524  1.1       ryo 	for (i = 0; i < ENET_RX_RING_CNT; i++) {
   1525  1.1       ryo 		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
   1526  1.1       ryo 		    1, MCLBYTES, 0, BUS_DMA_NOWAIT,
   1527  1.1       ryo 		    &sc->sc_rxsoft[i].rxs_dmamap);
   1528  1.1       ryo 		if (error) {
   1529  1.1       ryo 			aprint_error_dev(sc->sc_dev,
   1530  1.1       ryo 			    "can't create DMA map for RX descs\n");
   1531  1.1       ryo 			goto fail_2;
   1532  1.1       ryo 		}
   1533  1.1       ryo 	}
   1534  1.1       ryo 
   1535  1.1       ryo 	if (enet_alloc_dma(sc, sizeof(struct enet_txdesc) * ENET_TX_RING_CNT,
   1536  1.1       ryo 	    (void **)&(sc->sc_txdesc_ring), &(sc->sc_txdesc_dmamap)) != 0)
   1537  1.1       ryo 		return -1;
   1538  1.1       ryo 	memset(sc->sc_txdesc_ring, 0,
   1539  1.1       ryo 	    sizeof(struct enet_txdesc) * ENET_TX_RING_CNT);
   1540  1.1       ryo 
   1541  1.1       ryo 	if (enet_alloc_dma(sc, sizeof(struct enet_rxdesc) * ENET_RX_RING_CNT,
   1542  1.1       ryo 	    (void **)&(sc->sc_rxdesc_ring), &(sc->sc_rxdesc_dmamap)) != 0)
   1543  1.1       ryo 		return -1;
   1544  1.1       ryo 	memset(sc->sc_rxdesc_ring, 0,
   1545  1.1       ryo 	    sizeof(struct enet_rxdesc) * ENET_RX_RING_CNT);
   1546  1.1       ryo 
   1547  1.1       ryo 	return 0;
   1548  1.1       ryo 
   1549  1.1       ryo  fail_2:
   1550  1.1       ryo 	for (i = 0; i < ENET_RX_RING_CNT; i++) {
   1551  1.1       ryo 		if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
   1552  1.1       ryo 			bus_dmamap_destroy(sc->sc_dmat,
   1553  1.1       ryo 			    sc->sc_rxsoft[i].rxs_dmamap);
   1554  1.1       ryo 	}
   1555  1.1       ryo  fail_1:
   1556  1.1       ryo 	for (i = 0; i < ENET_TX_RING_CNT; i++) {
   1557  1.1       ryo 		if (sc->sc_txsoft[i].txs_dmamap != NULL)
   1558  1.1       ryo 			bus_dmamap_destroy(sc->sc_dmat,
   1559  1.1       ryo 			    sc->sc_txsoft[i].txs_dmamap);
   1560  1.1       ryo 	}
   1561  1.1       ryo 	return error;
   1562  1.1       ryo }
   1563  1.1       ryo 
   1564  1.1       ryo static int
   1565  1.1       ryo enet_encap_mbufalign(struct mbuf **mp)
   1566  1.1       ryo {
   1567  1.1       ryo 	struct mbuf *m, *m0, *mt, *p, *x;
   1568  1.1       ryo 	void *ap;
   1569  1.1       ryo 	uint32_t alignoff, chiplen;
   1570  1.1       ryo 
   1571  1.1       ryo 	/*
   1572  1.1       ryo 	 * iMX6 SoC ethernet controller requires
   1573  1.1       ryo 	 * address of buffer must aligned 8, and
   1574  1.1       ryo 	 * length of buffer must be greater than 10 (first fragment only?)
   1575  1.1       ryo 	 */
   1576  1.1       ryo #define ALIGNBYTE	8
   1577  1.1       ryo #define MINBUFSIZE	10
   1578  1.1       ryo #define ALIGN_PTR(p, align)	\
   1579  1.1       ryo 	(void *)(((uintptr_t)(p) + ((align) - 1)) & -(align))
   1580  1.1       ryo 
   1581  1.1       ryo 	m0 = *mp;
   1582  1.1       ryo 	mt = p = NULL;
   1583  1.1       ryo 	for (m = m0; m != NULL; m = m->m_next) {
   1584  1.1       ryo 		alignoff = (uintptr_t)m->m_data & (ALIGNBYTE - 1);
   1585  1.1       ryo 		if (m->m_len < (ALIGNBYTE * 2)) {
   1586  1.1       ryo 			/*
   1587  1.1       ryo 			 * rearrange mbuf data aligned
   1588  1.1       ryo 			 *
   1589  1.1       ryo 			 *        align 8 *       *       *       *       *
   1590  1.1       ryo 			 *               +0123456789abcdef0123456789abcdef0
   1591  1.1       ryo 			 * FROM m->m_data[___________abcdefghijklmn_______]
   1592  1.1       ryo 			 *
   1593  1.1       ryo 			 *               +0123456789abcdef0123456789abcdef0
   1594  1.1       ryo 			 * TO   m->m_data[________abcdefghijklm___________] or
   1595  1.1       ryo 			 *      m->m_data[________________abcdefghijklmn__]
   1596  1.1       ryo 			 */
   1597  1.1       ryo 			if ((alignoff != 0) && (m->m_len != 0)) {
   1598  1.1       ryo 				chiplen = ALIGNBYTE - alignoff;
   1599  1.1       ryo 				if (M_LEADINGSPACE(m) >= alignoff) {
   1600  1.1       ryo 					ap = m->m_data - alignoff;
   1601  1.1       ryo 					memmove(ap, m->m_data, m->m_len);
   1602  1.1       ryo 					m->m_data = ap;
   1603  1.1       ryo 				} else if (M_TRAILINGSPACE(m) >= chiplen) {
   1604  1.1       ryo 					ap = m->m_data + chiplen;
   1605  1.1       ryo 					memmove(ap, m->m_data, m->m_len);
   1606  1.1       ryo 					m->m_data = ap;
   1607  1.1       ryo 				} else {
   1608  1.1       ryo 					/*
   1609  1.1       ryo 					 * no space to align data. (M_READONLY?)
   1610  1.1       ryo 					 * allocate new mbuf aligned,
   1611  1.1       ryo 					 * and copy to it.
   1612  1.1       ryo 					 */
   1613  1.1       ryo 					MGET(x, M_DONTWAIT, m->m_type);
   1614  1.1       ryo 					if (x == NULL) {
   1615  1.1       ryo 						m_freem(m);
   1616  1.1       ryo 						return ENOBUFS;
   1617  1.1       ryo 					}
   1618  1.1       ryo 					MCLAIM(x, m->m_owner);
   1619  1.1       ryo 					if (m->m_flags & M_PKTHDR)
   1620  1.1       ryo 						M_MOVE_PKTHDR(x, m);
   1621  1.1       ryo 					x->m_len = m->m_len;
   1622  1.1       ryo 					x->m_data = ALIGN_PTR(x->m_data,
   1623  1.1       ryo 					    ALIGNBYTE);
   1624  1.1       ryo 					memcpy(mtod(x, void *), mtod(m, void *),
   1625  1.1       ryo 					    m->m_len);
   1626  1.1       ryo 					p->m_next = x;
   1627  1.1       ryo 					MFREE(m, x->m_next);
   1628  1.1       ryo 					m = x;
   1629  1.1       ryo 				}
   1630  1.1       ryo 			}
   1631  1.1       ryo 
   1632  1.1       ryo 			/*
   1633  1.1       ryo 			 * fill 1st mbuf at least 10byte
   1634  1.1       ryo 			 *
   1635  1.1       ryo 			 *        align 8 *       *       *       *       *
   1636  1.1       ryo 			 *               +0123456789abcdef0123456789abcdef0
   1637  1.1       ryo 			 * FROM m->m_data[________abcde___________________]
   1638  1.1       ryo 			 *      m->m_data[__fg____________________________]
   1639  1.1       ryo 			 *      m->m_data[_________________hi_____________]
   1640  1.1       ryo 			 *      m->m_data[__________jk____________________]
   1641  1.1       ryo 			 *      m->m_data[____l___________________________]
   1642  1.1       ryo 			 *
   1643  1.1       ryo 			 *               +0123456789abcdef0123456789abcdef0
   1644  1.1       ryo 			 * TO   m->m_data[________abcdefghij______________]
   1645  1.1       ryo 			 *      m->m_data[________________________________]
   1646  1.1       ryo 			 *      m->m_data[________________________________]
   1647  1.1       ryo 			 *      m->m_data[___________k____________________]
   1648  1.1       ryo 			 *      m->m_data[____l___________________________]
   1649  1.1       ryo 			 */
   1650  1.1       ryo 			if (mt == NULL) {
   1651  1.1       ryo 				mt = m;
   1652  1.1       ryo 				while (mt->m_len == 0) {
   1653  1.1       ryo 					mt = mt->m_next;
   1654  1.1       ryo 					if (mt == NULL) {
   1655  1.1       ryo 						m_freem(m);
   1656  1.1       ryo 						return ENOBUFS;
   1657  1.1       ryo 					}
   1658  1.1       ryo 				}
   1659  1.1       ryo 
   1660  1.1       ryo 				/* mt = 1st mbuf, x = 2nd mbuf */
   1661  1.1       ryo 				x = mt->m_next;
   1662  1.1       ryo 				while (mt->m_len < MINBUFSIZE) {
   1663  1.1       ryo 					if (x == NULL) {
   1664  1.1       ryo 						m_freem(m);
   1665  1.1       ryo 						return ENOBUFS;
   1666  1.1       ryo 					}
   1667  1.1       ryo 
   1668  1.1       ryo 					alignoff = (uintptr_t)x->m_data &
   1669  1.1       ryo 					    (ALIGNBYTE - 1);
   1670  1.1       ryo 					chiplen = ALIGNBYTE - alignoff;
   1671  1.1       ryo 					if (chiplen > x->m_len) {
   1672  1.1       ryo 						chiplen = x->m_len;
   1673  1.1       ryo 					} else if ((mt->m_len + chiplen) <
   1674  1.1       ryo 					    MINBUFSIZE) {
   1675  1.1       ryo 						/*
   1676  1.1       ryo 						 * next mbuf should be greater
   1677  1.1       ryo 						 * than ALIGNBYTE?
   1678  1.1       ryo 						 */
   1679  1.1       ryo 						if (x->m_len >= (chiplen +
   1680  1.1       ryo 						    ALIGNBYTE * 2))
   1681  1.1       ryo 							chiplen += ALIGNBYTE;
   1682  1.1       ryo 						else
   1683  1.1       ryo 							chiplen = x->m_len;
   1684  1.1       ryo 					}
   1685  1.1       ryo 
   1686  1.1       ryo 					if (chiplen &&
   1687  1.1       ryo 					    (M_TRAILINGSPACE(mt) < chiplen)) {
   1688  1.1       ryo 						/*
   1689  1.1       ryo 						 * move data to the begining of
   1690  1.1       ryo 						 * m_dat[] (aligned) to en-
   1691  1.1       ryo 						 * large trailingspace
   1692  1.1       ryo 						 */
   1693  1.1       ryo 						if (mt->m_flags & M_EXT) {
   1694  1.1       ryo 							ap = mt->m_ext.ext_buf;
   1695  1.1       ryo 						} else if (mt->m_flags &
   1696  1.1       ryo 						    M_PKTHDR) {
   1697  1.1       ryo 							ap = mt->m_pktdat;
   1698  1.1       ryo 						} else {
   1699  1.1       ryo 							ap = mt->m_dat;
   1700  1.1       ryo 						}
   1701  1.1       ryo 						ap = ALIGN_PTR(ap, ALIGNBYTE);
   1702  1.1       ryo 						memcpy(ap, mt->m_data, mt->m_len);
   1703  1.1       ryo 						mt->m_data = ap;
   1704  1.1       ryo 					}
   1705  1.1       ryo 
   1706  1.1       ryo 					if (chiplen &&
   1707  1.1       ryo 					    (M_TRAILINGSPACE(mt) >= chiplen)) {
   1708  1.1       ryo 						memcpy(mt->m_data + mt->m_len,
   1709  1.1       ryo 						    x->m_data, chiplen);
   1710  1.1       ryo 						mt->m_len += chiplen;
   1711  1.1       ryo 						m_adj(x, chiplen);
   1712  1.1       ryo 					}
   1713  1.1       ryo 
   1714  1.1       ryo 					x = x->m_next;
   1715  1.1       ryo 				}
   1716  1.1       ryo 			}
   1717  1.1       ryo 
   1718  1.1       ryo 		} else {
   1719  1.1       ryo 			mt = m;
   1720  1.1       ryo 
   1721  1.1       ryo 			/*
   1722  1.1       ryo 			 * allocate new mbuf x, and rearrange as below;
   1723  1.1       ryo 			 *
   1724  1.1       ryo 			 *        align 8 *       *       *       *       *
   1725  1.1       ryo 			 *               +0123456789abcdef0123456789abcdef0
   1726  1.1       ryo 			 * FROM m->m_data[____________abcdefghijklmnopq___]
   1727  1.1       ryo 			 *
   1728  1.1       ryo 			 *               +0123456789abcdef0123456789abcdef0
   1729  1.1       ryo 			 * TO   x->m_data[________abcdefghijkl____________]
   1730  1.1       ryo 			 *      m->m_data[________________________mnopq___]
   1731  1.1       ryo 			 *
   1732  1.1       ryo 			 */
   1733  1.1       ryo 			if (alignoff != 0) {
   1734  1.1       ryo 				/* at least ALIGNBYTE */
   1735  1.1       ryo 				chiplen = ALIGNBYTE - alignoff + ALIGNBYTE;
   1736  1.1       ryo 
   1737  1.1       ryo 				MGET(x, M_DONTWAIT, m->m_type);
   1738  1.1       ryo 				if (x == NULL) {
   1739  1.1       ryo 					m_freem(m);
   1740  1.1       ryo 					return ENOBUFS;
   1741  1.1       ryo 				}
   1742  1.1       ryo 				MCLAIM(x, m->m_owner);
   1743  1.1       ryo 				if (m->m_flags & M_PKTHDR)
   1744  1.1       ryo 					M_MOVE_PKTHDR(x, m);
   1745  1.1       ryo 				x->m_data = ALIGN_PTR(x->m_data, ALIGNBYTE);
   1746  1.1       ryo 				memcpy(mtod(x, void *), mtod(m, void *),
   1747  1.1       ryo 				    chiplen);
   1748  1.1       ryo 				x->m_len = chiplen;
   1749  1.1       ryo 				x->m_next = m;
   1750  1.1       ryo 				m_adj(m, chiplen);
   1751  1.1       ryo 
   1752  1.1       ryo 				if (p == NULL)
   1753  1.1       ryo 					m0 = x;
   1754  1.1       ryo 				else
   1755  1.1       ryo 					p->m_next = x;
   1756  1.1       ryo 			}
   1757  1.1       ryo 		}
   1758  1.1       ryo 		p = m;
   1759  1.1       ryo 	}
   1760  1.1       ryo 	*mp = m0;
   1761  1.1       ryo 
   1762  1.1       ryo 	return 0;
   1763  1.1       ryo }
   1764  1.1       ryo 
   1765  1.1       ryo static int
   1766  1.1       ryo enet_encap_txring(struct enet_softc *sc, struct mbuf **mp)
   1767  1.1       ryo {
   1768  1.1       ryo 	bus_dmamap_t map;
   1769  1.1       ryo 	struct mbuf *m;
   1770  1.1       ryo 	int csumflags, idx, i, error;
   1771  1.1       ryo 	uint32_t flags1, flags2;
   1772  1.1       ryo 
   1773  1.1       ryo 	idx = sc->sc_tx_prodidx;
   1774  1.1       ryo 	map = sc->sc_txsoft[idx].txs_dmamap;
   1775  1.1       ryo 
   1776  1.1       ryo 	/* align mbuf data for claim of ENET */
   1777  1.1       ryo 	error = enet_encap_mbufalign(mp);
   1778  1.1       ryo 	if (error != 0)
   1779  1.1       ryo 		return error;
   1780  1.1       ryo 
   1781  1.1       ryo 	m = *mp;
   1782  1.1       ryo 	csumflags = m->m_pkthdr.csum_flags;
   1783  1.1       ryo 
   1784  1.1       ryo 	error = bus_dmamap_load_mbuf(sc->sc_dmat, map, m,
   1785  1.1       ryo 	    BUS_DMA_NOWAIT);
   1786  1.1       ryo 	if (error != 0) {
   1787  1.1       ryo 		device_printf(sc->sc_dev,
   1788  1.1       ryo 		    "Error mapping mbuf into TX chain: error=%d\n", error);
   1789  1.1       ryo 		m_freem(m);
   1790  1.1       ryo 		return error;
   1791  1.1       ryo 	}
   1792  1.1       ryo 
   1793  1.1       ryo 	if (map->dm_nsegs > sc->sc_tx_free) {
   1794  1.1       ryo 		bus_dmamap_unload(sc->sc_dmat, map);
   1795  1.1       ryo 		device_printf(sc->sc_dev,
   1796  1.1       ryo 		    "too many mbuf chain %d\n", map->dm_nsegs);
   1797  1.1       ryo 		m_freem(m);
   1798  1.1       ryo 		return ENOBUFS;
   1799  1.1       ryo 	}
   1800  1.1       ryo 
   1801  1.1       ryo 	/* fill protocol cksum zero beforehand */
   1802  1.1       ryo 	if (csumflags & (M_CSUM_UDPv4 | M_CSUM_TCPv4 |
   1803  1.1       ryo 	    M_CSUM_UDPv6 | M_CSUM_TCPv6)) {
   1804  1.1       ryo 		struct mbuf *m1;
   1805  1.1       ryo 		int ehlen, moff;
   1806  1.1       ryo 		uint16_t etype;
   1807  1.1       ryo 
   1808  1.1       ryo 		m_copydata(m, ETHER_ADDR_LEN * 2, sizeof(etype), &etype);
   1809  1.1       ryo 		switch (ntohs(etype)) {
   1810  1.1       ryo 		case ETHERTYPE_IP:
   1811  1.1       ryo 		case ETHERTYPE_IPV6:
   1812  1.1       ryo 			ehlen = ETHER_HDR_LEN;
   1813  1.1       ryo 			break;
   1814  1.1       ryo 		case ETHERTYPE_VLAN:
   1815  1.1       ryo 			ehlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
   1816  1.1       ryo 			break;
   1817  1.1       ryo 		default:
   1818  1.1       ryo 			ehlen = 0;
   1819  1.1       ryo 			break;
   1820  1.1       ryo 		}
   1821  1.1       ryo 
   1822  1.1       ryo 		if (ehlen) {
   1823  1.1       ryo 			m1 = m_getptr(m, ehlen +
   1824  1.1       ryo 			    M_CSUM_DATA_IPv4_IPHL(m->m_pkthdr.csum_data) +
   1825  1.1       ryo 			    M_CSUM_DATA_IPv4_OFFSET(m->m_pkthdr.csum_data),
   1826  1.1       ryo 			    &moff);
   1827  1.1       ryo 			if (m1 != NULL)
   1828  1.1       ryo 				*(uint16_t *)(mtod(m1, char *) + moff) = 0;
   1829  1.1       ryo 		}
   1830  1.1       ryo 	}
   1831  1.1       ryo 
   1832  1.1       ryo 	bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
   1833  1.1       ryo 	    BUS_DMASYNC_PREWRITE);
   1834  1.1       ryo 
   1835  1.1       ryo 	for (i = 0; i < map->dm_nsegs; i++) {
   1836  1.1       ryo 		flags1 = TXFLAGS1_R;
   1837  1.1       ryo 		flags2 = 0;
   1838  1.1       ryo 
   1839  1.1       ryo 		if (i == 0) {
   1840  1.1       ryo 			flags1 |= TXFLAGS1_T1;	/* mark as first segment */
   1841  1.1       ryo 			sc->sc_txsoft[idx].txs_mbuf = m;
   1842  1.1       ryo 		}
   1843  1.1       ryo 
   1844  1.1       ryo 		/* checksum offloading */
   1845  1.1       ryo 		if (csumflags & (M_CSUM_UDPv4 | M_CSUM_TCPv4 |
   1846  1.1       ryo 		    M_CSUM_UDPv6 | M_CSUM_TCPv6))
   1847  1.1       ryo 			flags2 |= TXFLAGS2_PINS;
   1848  1.1       ryo 		if (csumflags & (M_CSUM_IPv4))
   1849  1.1       ryo 			flags2 |= TXFLAGS2_IINS;
   1850  1.1       ryo 
   1851  1.1       ryo 		if (i == map->dm_nsegs - 1) {
   1852  1.1       ryo 			/* mark last segment */
   1853  1.1       ryo 			flags1 |= TXFLAGS1_L | TXFLAGS1_TC;
   1854  1.1       ryo 			flags2 |= TXFLAGS2_INT;
   1855  1.1       ryo 		}
   1856  1.1       ryo 		if (idx == ENET_TX_RING_CNT - 1) {
   1857  1.1       ryo 			/* mark end of ring */
   1858  1.1       ryo 			flags1 |= TXFLAGS1_W;
   1859  1.1       ryo 		}
   1860  1.1       ryo 
   1861  1.1       ryo 		sc->sc_txdesc_ring[idx].tx_databuf = map->dm_segs[i].ds_addr;
   1862  1.1       ryo 		sc->sc_txdesc_ring[idx].tx_flags2 = flags2;
   1863  1.1       ryo 		sc->sc_txdesc_ring[idx].tx_flags3 = 0;
   1864  1.1       ryo 		sc->sc_txdesc_ring[idx].tx_flags1_len =
   1865  1.1       ryo 		    flags1 | TXFLAGS1_LEN(map->dm_segs[i].ds_len);
   1866  1.1       ryo 
   1867  1.1       ryo 		TXDESC_WRITEOUT(idx);
   1868  1.1       ryo 
   1869  1.1       ryo 		idx = ENET_TX_NEXTIDX(idx);
   1870  1.1       ryo 		sc->sc_tx_free--;
   1871  1.1       ryo 	}
   1872  1.1       ryo 
   1873  1.1       ryo 	sc->sc_tx_prodidx = idx;
   1874  1.1       ryo 
   1875  1.1       ryo 	return 0;
   1876  1.1       ryo }
   1877  1.1       ryo 
   1878  1.1       ryo /*
   1879  1.1       ryo  * device initialize
   1880  1.1       ryo  */
   1881  1.1       ryo static int
   1882  1.1       ryo enet_init_plls(struct enet_softc *sc)
   1883  1.1       ryo {
   1884  1.1       ryo #if NIMXCCM > 0
   1885  1.1       ryo 	/* PLL power up */
   1886  1.1       ryo 	if (imx6_pll_power(CCM_ANALOG_PLL_ENET, 1) != 0) {
   1887  1.1       ryo 		aprint_error_dev(sc->sc_dev,
   1888  1.1       ryo 		    "couldn't enable CCM_ANALOG_PLL_ENET\n");
   1889  1.1       ryo 		return -1;
   1890  1.1       ryo 	}
   1891  1.1       ryo #endif
   1892  1.1       ryo 
   1893  1.1       ryo 	return 0;
   1894  1.1       ryo }
   1895  1.1       ryo 
   1896  1.1       ryo static int
   1897  1.1       ryo enet_init_regs(struct enet_softc *sc, int init)
   1898  1.1       ryo {
   1899  1.1       ryo 	struct mii_data *mii;
   1900  1.1       ryo 	struct ifmedia_entry *ife;
   1901  1.1       ryo 	paddr_t paddr;
   1902  1.1       ryo 	uint32_t val;
   1903  1.1       ryo 	int fulldup, ecr_speed, rcr_speed, flowctrl;
   1904  1.1       ryo 
   1905  1.1       ryo 	if (init) {
   1906  1.1       ryo 		fulldup = 1;
   1907  1.1       ryo 		ecr_speed = ENET_ECR_SPEED;
   1908  1.1       ryo 		rcr_speed = 0;
   1909  1.1       ryo 		flowctrl = 0;
   1910  1.1       ryo 	} else {
   1911  1.1       ryo 		mii = &sc->sc_mii;
   1912  1.1       ryo 		ife = mii->mii_media.ifm_cur;
   1913  1.1       ryo 
   1914  1.1       ryo 		if ((ife->ifm_media & IFM_GMASK) == IFM_FDX)
   1915  1.1       ryo 			fulldup = 1;
   1916  1.1       ryo 		else
   1917  1.1       ryo 			fulldup = 0;
   1918  1.1       ryo 
   1919  1.1       ryo 		switch (IFM_SUBTYPE(ife->ifm_media)) {
   1920  1.1       ryo 		case IFM_10_T:
   1921  1.1       ryo 			ecr_speed = 0;
   1922  1.1       ryo 			rcr_speed = ENET_RCR_RMII_10T;
   1923  1.1       ryo 			break;
   1924  1.1       ryo 		case IFM_100_TX:
   1925  1.1       ryo 			ecr_speed = 0;
   1926  1.1       ryo 			rcr_speed = 0;
   1927  1.1       ryo 			break;
   1928  1.1       ryo 		default:
   1929  1.1       ryo 			ecr_speed = ENET_ECR_SPEED;
   1930  1.1       ryo 			rcr_speed = 0;
   1931  1.1       ryo 			break;
   1932  1.1       ryo 		}
   1933  1.1       ryo 
   1934  1.1       ryo 		flowctrl = sc->sc_flowflags & IFM_FLOW;
   1935  1.1       ryo 	}
   1936  1.1       ryo 
   1937  1.1       ryo 	/* reset */
   1938  1.1       ryo 	ENET_REG_WRITE(sc, ENET_ECR, ecr_speed | ENET_ECR_RESET);
   1939  1.1       ryo 
   1940  1.1       ryo 	/* mask and clear all interrupt */
   1941  1.1       ryo 	ENET_REG_WRITE(sc, ENET_EIMR, 0);
   1942  1.1       ryo 	ENET_REG_WRITE(sc, ENET_EIR, 0xffffffff);
   1943  1.1       ryo 
   1944  1.1       ryo 	/* full duplex */
   1945  1.1       ryo 	ENET_REG_WRITE(sc, ENET_TCR, fulldup ? ENET_TCR_FDEN : 0);
   1946  1.1       ryo 
   1947  1.1       ryo 	/* clear and enable MIB register */
   1948  1.1       ryo 	ENET_REG_WRITE(sc, ENET_MIBC, ENET_MIBC_MIB_CLEAR);
   1949  1.1       ryo 	ENET_REG_WRITE(sc, ENET_MIBC, 0);
   1950  1.1       ryo 
   1951  1.1       ryo 	/* MII speed setup. MDCclk(=2.5MHz) = PLL6clk/((val+1)*2) */
   1952  1.1       ryo 	val = (imx6_get_clock(IMX6CLK_PLL6) / 500000 - 1) / 10;
   1953  1.1       ryo 	ENET_REG_WRITE(sc, ENET_MSCR, val);
   1954  1.1       ryo 
   1955  1.1       ryo 	/* Opcode/Pause Duration */
   1956  1.1       ryo 	ENET_REG_WRITE(sc, ENET_OPD, 0x00010020);
   1957  1.1       ryo 
   1958  1.1       ryo 	/* Receive FIFO */
   1959  1.1       ryo 	ENET_REG_WRITE(sc, ENET_RSFL, 16);	/* RxFIFO Section Full */
   1960  1.1       ryo 	ENET_REG_WRITE(sc, ENET_RSEM, 0x84);	/* RxFIFO Section Empty */
   1961  1.1       ryo 	ENET_REG_WRITE(sc, ENET_RAEM, 8);	/* RxFIFO Almost Empty */
   1962  1.1       ryo 	ENET_REG_WRITE(sc, ENET_RAFL, 8);	/* RxFIFO Almost Full */
   1963  1.1       ryo 
   1964  1.1       ryo 	/* Transmit FIFO */
   1965  1.1       ryo 	ENET_REG_WRITE(sc, ENET_TFWR, ENET_TFWR_STRFWD |
   1966  1.1       ryo 	    ENET_TFWR_FIFO(128));		/* TxFIFO Watermark */
   1967  1.1       ryo 	ENET_REG_WRITE(sc, ENET_TSEM, 0);	/* TxFIFO Section Empty */
   1968  1.1       ryo 	ENET_REG_WRITE(sc, ENET_TAEM, 256);	/* TxFIFO Almost Empty */
   1969  1.1       ryo 	ENET_REG_WRITE(sc, ENET_TAFL, 8);	/* TxFIFO Almost Full */
   1970  1.1       ryo 	ENET_REG_WRITE(sc, ENET_TIPG, 12);	/* Tx Inter-Packet Gap */
   1971  1.1       ryo 
   1972  1.1       ryo 	/* hardware checksum is default off (override in TX descripter) */
   1973  1.1       ryo 	ENET_REG_WRITE(sc, ENET_TACC, 0);
   1974  1.1       ryo 
   1975  1.1       ryo 	/*
   1976  1.1       ryo 	 * align ethernet payload on 32bit, discard frames with MAC layer error,
   1977  1.1       ryo 	 * and don't discard checksum error
   1978  1.1       ryo 	 */
   1979  1.1       ryo 	ENET_REG_WRITE(sc, ENET_RACC, ENET_RACC_SHIFT16 | ENET_RACC_LINEDIS);
   1980  1.1       ryo 
   1981  1.1       ryo 	/* maximum frame size */
   1982  1.1       ryo 	val = ENET_DEFAULT_PKT_LEN;
   1983  1.1       ryo 	ENET_REG_WRITE(sc, ENET_FTRL, val);	/* Frame Truncation Length */
   1984  1.1       ryo 	ENET_REG_WRITE(sc, ENET_RCR,
   1985  1.1       ryo 	    ENET_RCR_PADEN |			/* RX frame padding remove */
   1986  1.1       ryo 	    ENET_RCR_RGMII_EN |			/* use RGMII */
   1987  1.1       ryo 	    (flowctrl ? ENET_RCR_FCE : 0) |	/* flow control enable */
   1988  1.1       ryo 	    rcr_speed |
   1989  1.1       ryo 	    (fulldup ? 0 : ENET_RCR_DRT) |
   1990  1.1       ryo 	    ENET_RCR_MAX_FL(val));
   1991  1.1       ryo 
   1992  1.1       ryo 	/* Maximum Receive BufSize per one descriptor */
   1993  1.1       ryo 	ENET_REG_WRITE(sc, ENET_MRBR, RXDESC_MAXBUFSIZE);
   1994  1.1       ryo 
   1995  1.1       ryo 
   1996  1.1       ryo 	/* TX/RX Descriptor Physical Address */
   1997  1.1       ryo 	paddr = sc->sc_txdesc_dmamap->dm_segs[0].ds_addr;
   1998  1.1       ryo 	ENET_REG_WRITE(sc, ENET_TDSR, paddr);
   1999  1.1       ryo 	paddr = sc->sc_rxdesc_dmamap->dm_segs[0].ds_addr;
   2000  1.1       ryo 	ENET_REG_WRITE(sc, ENET_RDSR, paddr);
   2001  1.1       ryo 	/* sync cache */
   2002  1.1       ryo 	bus_dmamap_sync(sc->sc_dmat, sc->sc_txdesc_dmamap, 0,
   2003  1.1       ryo 	    sc->sc_txdesc_dmamap->dm_mapsize, BUS_DMASYNC_PREWRITE);
   2004  1.1       ryo 	bus_dmamap_sync(sc->sc_dmat, sc->sc_rxdesc_dmamap, 0,
   2005  1.1       ryo 	    sc->sc_rxdesc_dmamap->dm_mapsize, BUS_DMASYNC_PREWRITE);
   2006  1.1       ryo 
   2007  1.1       ryo 	/* enable interrupts */
   2008  1.1       ryo 	ENET_REG_WRITE(sc, ENET_EIMR,
   2009  1.1       ryo 	    ENET_EIR_TXF |
   2010  1.1       ryo 	    ENET_EIR_RXF |
   2011  1.1       ryo 	    ENET_EIR_EBERR |
   2012  1.1       ryo 	    0);
   2013  1.1       ryo 
   2014  1.1       ryo 	/* enable ether */
   2015  1.1       ryo 	ENET_REG_WRITE(sc, ENET_ECR,
   2016  1.1       ryo #if _BYTE_ORDER == _LITTLE_ENDIAN
   2017  1.1       ryo 	    ENET_ECR_DBSWP |
   2018  1.1       ryo #endif
   2019  1.1       ryo 	    ENET_ECR_SPEED |	/* default 1000Mbps mode */
   2020  1.1       ryo 	    ENET_ECR_EN1588 |	/* use enhanced TX/RX descriptor */
   2021  1.1       ryo 	    ENET_ECR_ETHEREN);	/* Ethernet Enable */
   2022  1.1       ryo 
   2023  1.1       ryo 	return 0;
   2024  1.1       ryo }
   2025  1.1       ryo 
   2026  1.1       ryo static int
   2027  1.1       ryo enet_alloc_dma(struct enet_softc *sc, size_t size, void **addrp,
   2028  1.1       ryo               bus_dmamap_t *mapp)
   2029  1.1       ryo {
   2030  1.1       ryo 	bus_dma_segment_t seglist[1];
   2031  1.1       ryo 	int nsegs, error;
   2032  1.1       ryo 
   2033  1.1       ryo 	if ((error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, seglist,
   2034  1.2       ryo 	    1, &nsegs, M_NOWAIT)) != 0) {
   2035  1.1       ryo 		device_printf(sc->sc_dev,
   2036  1.1       ryo 		    "unable to allocate DMA buffer, error=%d\n", error);
   2037  1.1       ryo 		goto fail_alloc;
   2038  1.1       ryo 	}
   2039  1.1       ryo 
   2040  1.1       ryo 	if ((error = bus_dmamem_map(sc->sc_dmat, seglist, 1, size, addrp,
   2041  1.1       ryo 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
   2042  1.1       ryo 		device_printf(sc->sc_dev,
   2043  1.1       ryo 		    "unable to map DMA buffer, error=%d\n",
   2044  1.1       ryo 		    error);
   2045  1.1       ryo 		goto fail_map;
   2046  1.1       ryo 	}
   2047  1.1       ryo 
   2048  1.1       ryo 	if ((error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
   2049  1.1       ryo 	    BUS_DMA_NOWAIT, mapp)) != 0) {
   2050  1.1       ryo 		device_printf(sc->sc_dev,
   2051  1.1       ryo 		    "unable to create DMA map, error=%d\n", error);
   2052  1.1       ryo 		goto fail_create;
   2053  1.1       ryo 	}
   2054  1.1       ryo 
   2055  1.1       ryo 	if ((error = bus_dmamap_load(sc->sc_dmat, *mapp, *addrp, size, NULL,
   2056  1.1       ryo 	    BUS_DMA_NOWAIT)) != 0) {
   2057  1.1       ryo 		aprint_error_dev(sc->sc_dev,
   2058  1.1       ryo 		    "unable to load DMA map, error=%d\n", error);
   2059  1.1       ryo 		goto fail_load;
   2060  1.1       ryo 	}
   2061  1.1       ryo 
   2062  1.1       ryo 	return 0;
   2063  1.1       ryo 
   2064  1.1       ryo  fail_load:
   2065  1.1       ryo 	bus_dmamap_destroy(sc->sc_dmat, *mapp);
   2066  1.1       ryo  fail_create:
   2067  1.1       ryo 	bus_dmamem_unmap(sc->sc_dmat, *addrp, size);
   2068  1.1       ryo  fail_map:
   2069  1.1       ryo 	bus_dmamem_free(sc->sc_dmat, seglist, 1);
   2070  1.1       ryo  fail_alloc:
   2071  1.1       ryo 	return error;
   2072  1.1       ryo }
   2073