xref: /freebsd/sys/dev/et/if_et.c (revision 7029da5c36f2d3cf6bb6c81bf551229f416399e8)
14d52a575SXin LI /*-
27282444bSPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
37282444bSPedro F. Giffuni  *
4e5fdd9deSXin LI  * Copyright (c) 2007 Sepherosa Ziehau.  All rights reserved.
54d52a575SXin LI  *
64d52a575SXin LI  * This code is derived from software contributed to The DragonFly Project
74d52a575SXin LI  * by Sepherosa Ziehau <sepherosa@gmail.com>
84d52a575SXin LI  *
94d52a575SXin LI  * Redistribution and use in source and binary forms, with or without
104d52a575SXin LI  * modification, are permitted provided that the following conditions
114d52a575SXin LI  * are met:
124d52a575SXin LI  *
134d52a575SXin LI  * 1. Redistributions of source code must retain the above copyright
144d52a575SXin LI  *    notice, this list of conditions and the following disclaimer.
154d52a575SXin LI  * 2. Redistributions in binary form must reproduce the above copyright
164d52a575SXin LI  *    notice, this list of conditions and the following disclaimer in
174d52a575SXin LI  *    the documentation and/or other materials provided with the
184d52a575SXin LI  *    distribution.
194d52a575SXin LI  * 3. Neither the name of The DragonFly Project nor the names of its
204d52a575SXin LI  *    contributors may be used to endorse or promote products derived
214d52a575SXin LI  *    from this software without specific, prior written permission.
224d52a575SXin LI  *
234d52a575SXin LI  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
244d52a575SXin LI  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
254d52a575SXin LI  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
264d52a575SXin LI  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
274d52a575SXin LI  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
284d52a575SXin LI  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
294d52a575SXin LI  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
304d52a575SXin LI  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
314d52a575SXin LI  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
324d52a575SXin LI  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
334d52a575SXin LI  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
344d52a575SXin LI  * SUCH DAMAGE.
354d52a575SXin LI  *
364d52a575SXin LI  * $DragonFly: src/sys/dev/netif/et/if_et.c,v 1.10 2008/05/18 07:47:14 sephe Exp $
374d52a575SXin LI  */
384d52a575SXin LI 
39fe42b04dSPyun YongHyeon #include <sys/cdefs.h>
40fe42b04dSPyun YongHyeon __FBSDID("$FreeBSD$");
41fe42b04dSPyun YongHyeon 
424d52a575SXin LI #include <sys/param.h>
434d52a575SXin LI #include <sys/systm.h>
444d52a575SXin LI #include <sys/endian.h>
454d52a575SXin LI #include <sys/kernel.h>
464d52a575SXin LI #include <sys/bus.h>
474d52a575SXin LI #include <sys/malloc.h>
484d52a575SXin LI #include <sys/mbuf.h>
494d52a575SXin LI #include <sys/proc.h>
504d52a575SXin LI #include <sys/rman.h>
514d52a575SXin LI #include <sys/module.h>
524d52a575SXin LI #include <sys/socket.h>
534d52a575SXin LI #include <sys/sockio.h>
544d52a575SXin LI #include <sys/sysctl.h>
554d52a575SXin LI 
564d52a575SXin LI #include <net/ethernet.h>
574d52a575SXin LI #include <net/if.h>
5876039bc8SGleb Smirnoff #include <net/if_var.h>
594d52a575SXin LI #include <net/if_dl.h>
604d52a575SXin LI #include <net/if_types.h>
614d52a575SXin LI #include <net/bpf.h>
624d52a575SXin LI #include <net/if_arp.h>
634d52a575SXin LI #include <net/if_media.h>
644d52a575SXin LI #include <net/if_vlan_var.h>
654d52a575SXin LI 
664d52a575SXin LI #include <machine/bus.h>
674d52a575SXin LI 
68d6c65d27SMarius Strobl #include <dev/mii/mii.h>
694d52a575SXin LI #include <dev/mii/miivar.h>
704d52a575SXin LI 
714d52a575SXin LI #include <dev/pci/pcireg.h>
724d52a575SXin LI #include <dev/pci/pcivar.h>
734d52a575SXin LI 
744d52a575SXin LI #include <dev/et/if_etreg.h>
754d52a575SXin LI #include <dev/et/if_etvar.h>
764d52a575SXin LI 
774d52a575SXin LI #include "miibus_if.h"
784d52a575SXin LI 
794d52a575SXin LI MODULE_DEPEND(et, pci, 1, 1, 1);
804d52a575SXin LI MODULE_DEPEND(et, ether, 1, 1, 1);
814d52a575SXin LI MODULE_DEPEND(et, miibus, 1, 1, 1);
824d52a575SXin LI 
83cc3c3b4eSPyun YongHyeon /* Tunables. */
84cc3c3b4eSPyun YongHyeon static int msi_disable = 0;
85accb4fcdSPyun YongHyeon TUNABLE_INT("hw.et.msi_disable", &msi_disable);
86cc3c3b4eSPyun YongHyeon 
879955274cSPyun YongHyeon #define	ET_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP)
889955274cSPyun YongHyeon 
894d52a575SXin LI static int	et_probe(device_t);
904d52a575SXin LI static int	et_attach(device_t);
914d52a575SXin LI static int	et_detach(device_t);
924d52a575SXin LI static int	et_shutdown(device_t);
930442028aSPyun YongHyeon static int	et_suspend(device_t);
940442028aSPyun YongHyeon static int	et_resume(device_t);
954d52a575SXin LI 
964d52a575SXin LI static int	et_miibus_readreg(device_t, int, int);
974d52a575SXin LI static int	et_miibus_writereg(device_t, int, int, int);
984d52a575SXin LI static void	et_miibus_statchg(device_t);
994d52a575SXin LI 
1004d52a575SXin LI static void	et_init_locked(struct et_softc *);
1014d52a575SXin LI static void	et_init(void *);
1024d52a575SXin LI static int	et_ioctl(struct ifnet *, u_long, caddr_t);
1034d52a575SXin LI static void	et_start_locked(struct ifnet *);
1044d52a575SXin LI static void	et_start(struct ifnet *);
10505884511SPyun YongHyeon static int	et_watchdog(struct et_softc *);
1064d52a575SXin LI static int	et_ifmedia_upd_locked(struct ifnet *);
1074d52a575SXin LI static int	et_ifmedia_upd(struct ifnet *);
1084d52a575SXin LI static void	et_ifmedia_sts(struct ifnet *, struct ifmediareq *);
109c13dc687SGleb Smirnoff static uint64_t	et_get_counter(struct ifnet *, ift_counter);
1104d52a575SXin LI 
1114d52a575SXin LI static void	et_add_sysctls(struct et_softc *);
1124d52a575SXin LI static int	et_sysctl_rx_intr_npkts(SYSCTL_HANDLER_ARGS);
1134d52a575SXin LI static int	et_sysctl_rx_intr_delay(SYSCTL_HANDLER_ARGS);
1144d52a575SXin LI 
1154d52a575SXin LI static void	et_intr(void *);
1164d52a575SXin LI static void	et_rxeof(struct et_softc *);
1174d52a575SXin LI static void	et_txeof(struct et_softc *);
1184d52a575SXin LI 
11905884511SPyun YongHyeon static int	et_dma_alloc(struct et_softc *);
12005884511SPyun YongHyeon static void	et_dma_free(struct et_softc *);
12105884511SPyun YongHyeon static void	et_dma_map_addr(void *, bus_dma_segment_t *, int, int);
12205884511SPyun YongHyeon static int	et_dma_ring_alloc(struct et_softc *, bus_size_t, bus_size_t,
12305884511SPyun YongHyeon 		    bus_dma_tag_t *, uint8_t **, bus_dmamap_t *, bus_addr_t *,
12405884511SPyun YongHyeon 		    const char *);
12505884511SPyun YongHyeon static void	et_dma_ring_free(struct et_softc *, bus_dma_tag_t *, uint8_t **,
126c34f1a08SJohn Baldwin 		    bus_dmamap_t, bus_addr_t *);
12705884511SPyun YongHyeon static void	et_init_tx_ring(struct et_softc *);
1284d52a575SXin LI static int	et_init_rx_ring(struct et_softc *);
1294d52a575SXin LI static void	et_free_tx_ring(struct et_softc *);
1304d52a575SXin LI static void	et_free_rx_ring(struct et_softc *);
1314d52a575SXin LI static int	et_encap(struct et_softc *, struct mbuf **);
13205884511SPyun YongHyeon static int	et_newbuf_cluster(struct et_rxbuf_data *, int);
13305884511SPyun YongHyeon static int	et_newbuf_hdr(struct et_rxbuf_data *, int);
13405884511SPyun YongHyeon static void	et_rxbuf_discard(struct et_rxbuf_data *, int);
1354d52a575SXin LI 
1364d52a575SXin LI static void	et_stop(struct et_softc *);
1374d52a575SXin LI static int	et_chip_init(struct et_softc *);
1384d52a575SXin LI static void	et_chip_attach(struct et_softc *);
1394d52a575SXin LI static void	et_init_mac(struct et_softc *);
1404d52a575SXin LI static void	et_init_rxmac(struct et_softc *);
1414d52a575SXin LI static void	et_init_txmac(struct et_softc *);
1424d52a575SXin LI static int	et_init_rxdma(struct et_softc *);
1434d52a575SXin LI static int	et_init_txdma(struct et_softc *);
1444d52a575SXin LI static int	et_start_rxdma(struct et_softc *);
1454d52a575SXin LI static int	et_start_txdma(struct et_softc *);
1464d52a575SXin LI static int	et_stop_rxdma(struct et_softc *);
1474d52a575SXin LI static int	et_stop_txdma(struct et_softc *);
1484d52a575SXin LI static void	et_reset(struct et_softc *);
1498b3c6496SPyun YongHyeon static int	et_bus_config(struct et_softc *);
1504d52a575SXin LI static void	et_get_eaddr(device_t, uint8_t[]);
1514d52a575SXin LI static void	et_setmulti(struct et_softc *);
1524d52a575SXin LI static void	et_tick(void *);
153e0b5ac02SPyun YongHyeon static void	et_stats_update(struct et_softc *);
1544d52a575SXin LI 
1554d52a575SXin LI static const struct et_dev {
1564d52a575SXin LI 	uint16_t	vid;
1574d52a575SXin LI 	uint16_t	did;
1584d52a575SXin LI 	const char	*desc;
1594d52a575SXin LI } et_devices[] = {
1604d52a575SXin LI 	{ PCI_VENDOR_LUCENT, PCI_PRODUCT_LUCENT_ET1310,
1614d52a575SXin LI 	  "Agere ET1310 Gigabit Ethernet" },
1624d52a575SXin LI 	{ PCI_VENDOR_LUCENT, PCI_PRODUCT_LUCENT_ET1310_FAST,
1634d52a575SXin LI 	  "Agere ET1310 Fast Ethernet" },
1644d52a575SXin LI 	{ 0, 0, NULL }
1654d52a575SXin LI };
1664d52a575SXin LI 
1674d52a575SXin LI static device_method_t et_methods[] = {
1684d52a575SXin LI 	DEVMETHOD(device_probe,		et_probe),
1694d52a575SXin LI 	DEVMETHOD(device_attach,	et_attach),
1704d52a575SXin LI 	DEVMETHOD(device_detach,	et_detach),
1714d52a575SXin LI 	DEVMETHOD(device_shutdown,	et_shutdown),
1720442028aSPyun YongHyeon 	DEVMETHOD(device_suspend,	et_suspend),
1730442028aSPyun YongHyeon 	DEVMETHOD(device_resume,	et_resume),
1744d52a575SXin LI 
1754d52a575SXin LI 	DEVMETHOD(miibus_readreg,	et_miibus_readreg),
1764d52a575SXin LI 	DEVMETHOD(miibus_writereg,	et_miibus_writereg),
1774d52a575SXin LI 	DEVMETHOD(miibus_statchg,	et_miibus_statchg),
1784d52a575SXin LI 
1794b7ec270SMarius Strobl 	DEVMETHOD_END
1804d52a575SXin LI };
1814d52a575SXin LI 
1824d52a575SXin LI static driver_t et_driver = {
1834d52a575SXin LI 	"et",
1844d52a575SXin LI 	et_methods,
1854d52a575SXin LI 	sizeof(struct et_softc)
1864d52a575SXin LI };
1874d52a575SXin LI 
1884d52a575SXin LI static devclass_t et_devclass;
1894d52a575SXin LI 
1904d52a575SXin LI DRIVER_MODULE(et, pci, et_driver, et_devclass, 0, 0);
1916ea57aa2SWarner Losh MODULE_PNP_INFO("U16:vendor;U16:device;D:#", pci, et, et_devices,
192329e817fSWarner Losh     nitems(et_devices) - 1);
1934d52a575SXin LI DRIVER_MODULE(miibus, et, miibus_driver, miibus_devclass, 0, 0);
1944d52a575SXin LI 
1954d52a575SXin LI static int	et_rx_intr_npkts = 32;
1964d52a575SXin LI static int	et_rx_intr_delay = 20;		/* x10 usec */
1974d52a575SXin LI static int	et_tx_intr_nsegs = 126;
1984d52a575SXin LI static uint32_t	et_timer = 1000 * 1000 * 1000;	/* nanosec */
1994d52a575SXin LI 
2004d52a575SXin LI TUNABLE_INT("hw.et.timer", &et_timer);
2014d52a575SXin LI TUNABLE_INT("hw.et.rx_intr_npkts", &et_rx_intr_npkts);
2024d52a575SXin LI TUNABLE_INT("hw.et.rx_intr_delay", &et_rx_intr_delay);
2034d52a575SXin LI TUNABLE_INT("hw.et.tx_intr_nsegs", &et_tx_intr_nsegs);
2044d52a575SXin LI 
2054d52a575SXin LI static int
2064d52a575SXin LI et_probe(device_t dev)
2074d52a575SXin LI {
2084d52a575SXin LI 	const struct et_dev *d;
2094d52a575SXin LI 	uint16_t did, vid;
2104d52a575SXin LI 
2114d52a575SXin LI 	vid = pci_get_vendor(dev);
2124d52a575SXin LI 	did = pci_get_device(dev);
2134d52a575SXin LI 
2144d52a575SXin LI 	for (d = et_devices; d->desc != NULL; ++d) {
2154d52a575SXin LI 		if (vid == d->vid && did == d->did) {
2164d52a575SXin LI 			device_set_desc(dev, d->desc);
217a64788d1SPyun YongHyeon 			return (BUS_PROBE_DEFAULT);
2184d52a575SXin LI 		}
2194d52a575SXin LI 	}
220398f1b65SPyun YongHyeon 	return (ENXIO);
2214d52a575SXin LI }
2224d52a575SXin LI 
2234d52a575SXin LI static int
2244d52a575SXin LI et_attach(device_t dev)
2254d52a575SXin LI {
2264d52a575SXin LI 	struct et_softc *sc;
2274d52a575SXin LI 	struct ifnet *ifp;
2284d52a575SXin LI 	uint8_t eaddr[ETHER_ADDR_LEN];
22938953bb0SPyun YongHyeon 	uint32_t pmcfg;
230cc3c3b4eSPyun YongHyeon 	int cap, error, msic;
2314d52a575SXin LI 
2324d52a575SXin LI 	sc = device_get_softc(dev);
2334d52a575SXin LI 	sc->dev = dev;
2344d52a575SXin LI 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
2354d52a575SXin LI 	    MTX_DEF);
236d2f7028cSPyun YongHyeon 	callout_init_mtx(&sc->sc_tick, &sc->sc_mtx, 0);
2374d52a575SXin LI 
2384d52a575SXin LI 	ifp = sc->ifp = if_alloc(IFT_ETHER);
2394d52a575SXin LI 	if (ifp == NULL) {
2404d52a575SXin LI 		device_printf(dev, "can not if_alloc()\n");
2414d52a575SXin LI 		error = ENOSPC;
2424d52a575SXin LI 		goto fail;
2434d52a575SXin LI 	}
2444d52a575SXin LI 
2454d52a575SXin LI 	/*
2464d52a575SXin LI 	 * Initialize tunables
2474d52a575SXin LI 	 */
2484d52a575SXin LI 	sc->sc_rx_intr_npkts = et_rx_intr_npkts;
2494d52a575SXin LI 	sc->sc_rx_intr_delay = et_rx_intr_delay;
2504d52a575SXin LI 	sc->sc_tx_intr_nsegs = et_tx_intr_nsegs;
2514d52a575SXin LI 	sc->sc_timer = et_timer;
2524d52a575SXin LI 
2534d52a575SXin LI 	/* Enable bus mastering */
2544d52a575SXin LI 	pci_enable_busmaster(dev);
2554d52a575SXin LI 
2564d52a575SXin LI 	/*
2574d52a575SXin LI 	 * Allocate IO memory
2584d52a575SXin LI 	 */
25939bea5ddSPyun YongHyeon 	sc->sc_mem_rid = PCIR_BAR(0);
2604d52a575SXin LI 	sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
2614d52a575SXin LI 	    &sc->sc_mem_rid, RF_ACTIVE);
2624d52a575SXin LI 	if (sc->sc_mem_res == NULL) {
2634d52a575SXin LI 		device_printf(dev, "can't allocate IO memory\n");
264398f1b65SPyun YongHyeon 		return (ENXIO);
2654d52a575SXin LI 	}
2664d52a575SXin LI 
267cc3c3b4eSPyun YongHyeon 	msic = 0;
2683b0a4aefSJohn Baldwin 	if (pci_find_cap(dev, PCIY_EXPRESS, &cap) == 0) {
269cc3c3b4eSPyun YongHyeon 		sc->sc_expcap = cap;
270cc3c3b4eSPyun YongHyeon 		sc->sc_flags |= ET_FLAG_PCIE;
271cc3c3b4eSPyun YongHyeon 		msic = pci_msi_count(dev);
272cc3c3b4eSPyun YongHyeon 		if (bootverbose)
273cc3c3b4eSPyun YongHyeon 			device_printf(dev, "MSI count: %d\n", msic);
274cc3c3b4eSPyun YongHyeon 	}
275cc3c3b4eSPyun YongHyeon 	if (msic > 0 && msi_disable == 0) {
276cc3c3b4eSPyun YongHyeon 		msic = 1;
277cc3c3b4eSPyun YongHyeon 		if (pci_alloc_msi(dev, &msic) == 0) {
278cc3c3b4eSPyun YongHyeon 			if (msic == 1) {
279cc3c3b4eSPyun YongHyeon 				device_printf(dev, "Using %d MSI message\n",
280cc3c3b4eSPyun YongHyeon 				    msic);
281cc3c3b4eSPyun YongHyeon 				sc->sc_flags |= ET_FLAG_MSI;
282cc3c3b4eSPyun YongHyeon 			} else
283cc3c3b4eSPyun YongHyeon 				pci_release_msi(dev);
284cc3c3b4eSPyun YongHyeon 		}
285cc3c3b4eSPyun YongHyeon 	}
286cc3c3b4eSPyun YongHyeon 
2874d52a575SXin LI 	/*
2884d52a575SXin LI 	 * Allocate IRQ
2894d52a575SXin LI 	 */
290cc3c3b4eSPyun YongHyeon 	if ((sc->sc_flags & ET_FLAG_MSI) == 0) {
2914d52a575SXin LI 		sc->sc_irq_rid = 0;
2924d52a575SXin LI 		sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
293cc3c3b4eSPyun YongHyeon 		    &sc->sc_irq_rid, RF_SHAREABLE | RF_ACTIVE);
294cc3c3b4eSPyun YongHyeon 	} else {
295cc3c3b4eSPyun YongHyeon 		sc->sc_irq_rid = 1;
296cc3c3b4eSPyun YongHyeon 		sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
297cc3c3b4eSPyun YongHyeon 		    &sc->sc_irq_rid, RF_ACTIVE);
298cc3c3b4eSPyun YongHyeon 	}
2994d52a575SXin LI 	if (sc->sc_irq_res == NULL) {
3004d52a575SXin LI 		device_printf(dev, "can't allocate irq\n");
3014d52a575SXin LI 		error = ENXIO;
3024d52a575SXin LI 		goto fail;
3034d52a575SXin LI 	}
3044d52a575SXin LI 
3051f009e2fSPyun YongHyeon 	if (pci_get_device(dev) == PCI_PRODUCT_LUCENT_ET1310_FAST)
3061f009e2fSPyun YongHyeon 		sc->sc_flags |= ET_FLAG_FASTETHER;
3071f009e2fSPyun YongHyeon 
3088b3c6496SPyun YongHyeon 	error = et_bus_config(sc);
3094d52a575SXin LI 	if (error)
3104d52a575SXin LI 		goto fail;
3114d52a575SXin LI 
3124d52a575SXin LI 	et_get_eaddr(dev, eaddr);
3134d52a575SXin LI 
31438953bb0SPyun YongHyeon 	/* Take PHY out of COMA and enable clocks. */
31538953bb0SPyun YongHyeon 	pmcfg = ET_PM_SYSCLK_GATE | ET_PM_TXCLK_GATE | ET_PM_RXCLK_GATE;
31638953bb0SPyun YongHyeon 	if ((sc->sc_flags & ET_FLAG_FASTETHER) == 0)
31738953bb0SPyun YongHyeon 		pmcfg |= EM_PM_GIGEPHY_ENB;
31838953bb0SPyun YongHyeon 	CSR_WRITE_4(sc, ET_PM, pmcfg);
3194d52a575SXin LI 
3204d52a575SXin LI 	et_reset(sc);
3214d52a575SXin LI 
32205884511SPyun YongHyeon 	error = et_dma_alloc(sc);
3234d52a575SXin LI 	if (error)
3244d52a575SXin LI 		goto fail;
3254d52a575SXin LI 
3264d52a575SXin LI 	ifp->if_softc = sc;
3274d52a575SXin LI 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
3284d52a575SXin LI 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
3294d52a575SXin LI 	ifp->if_init = et_init;
3304d52a575SXin LI 	ifp->if_ioctl = et_ioctl;
3314d52a575SXin LI 	ifp->if_start = et_start;
332c13dc687SGleb Smirnoff 	ifp->if_get_counter = et_get_counter;
333ed848e3aSPyun YongHyeon 	ifp->if_capabilities = IFCAP_TXCSUM | IFCAP_VLAN_MTU;
3344d52a575SXin LI 	ifp->if_capenable = ifp->if_capabilities;
335c8b727ceSPyun YongHyeon 	ifp->if_snd.ifq_drv_maxlen = ET_TX_NDESC - 1;
336c8b727ceSPyun YongHyeon 	IFQ_SET_MAXLEN(&ifp->if_snd, ET_TX_NDESC - 1);
3374d52a575SXin LI 	IFQ_SET_READY(&ifp->if_snd);
3384d52a575SXin LI 
3394d52a575SXin LI 	et_chip_attach(sc);
3404d52a575SXin LI 
341d6c65d27SMarius Strobl 	error = mii_attach(dev, &sc->sc_miibus, ifp, et_ifmedia_upd,
3425d384a0dSPyun YongHyeon 	    et_ifmedia_sts, BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY,
3435d384a0dSPyun YongHyeon 	    MIIF_DOPAUSE);
3444d52a575SXin LI 	if (error) {
345d6c65d27SMarius Strobl 		device_printf(dev, "attaching PHYs failed\n");
3464d52a575SXin LI 		goto fail;
3474d52a575SXin LI 	}
3484d52a575SXin LI 
3494d52a575SXin LI 	ether_ifattach(ifp, eaddr);
350d2f7028cSPyun YongHyeon 
351d2f7028cSPyun YongHyeon 	/* Tell the upper layer(s) we support long frames. */
352d2f7028cSPyun YongHyeon 	ifp->if_hdrlen = sizeof(struct ether_vlan_header);
3534d52a575SXin LI 
3544d52a575SXin LI 	error = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_NET | INTR_MPSAFE,
3554d52a575SXin LI 	    NULL, et_intr, sc, &sc->sc_irq_handle);
3564d52a575SXin LI 	if (error) {
3574d52a575SXin LI 		ether_ifdetach(ifp);
3584d52a575SXin LI 		device_printf(dev, "can't setup intr\n");
3594d52a575SXin LI 		goto fail;
3604d52a575SXin LI 	}
3614d52a575SXin LI 
3624d52a575SXin LI 	et_add_sysctls(sc);
3634d52a575SXin LI 
364398f1b65SPyun YongHyeon 	return (0);
3654d52a575SXin LI fail:
3664d52a575SXin LI 	et_detach(dev);
367398f1b65SPyun YongHyeon 	return (error);
3684d52a575SXin LI }
3694d52a575SXin LI 
3704d52a575SXin LI static int
3714d52a575SXin LI et_detach(device_t dev)
3724d52a575SXin LI {
3730b699044SPyun YongHyeon 	struct et_softc *sc;
3744d52a575SXin LI 
3750b699044SPyun YongHyeon 	sc = device_get_softc(dev);
3764d52a575SXin LI 	if (device_is_attached(dev)) {
377a64788d1SPyun YongHyeon 		ether_ifdetach(sc->ifp);
3784d52a575SXin LI 		ET_LOCK(sc);
3794d52a575SXin LI 		et_stop(sc);
3804d52a575SXin LI 		ET_UNLOCK(sc);
381a64788d1SPyun YongHyeon 		callout_drain(&sc->sc_tick);
3824d52a575SXin LI 	}
3834d52a575SXin LI 
3844d52a575SXin LI 	if (sc->sc_miibus != NULL)
3854d52a575SXin LI 		device_delete_child(dev, sc->sc_miibus);
3864d52a575SXin LI 	bus_generic_detach(dev);
3874d52a575SXin LI 
388a64788d1SPyun YongHyeon 	if (sc->sc_irq_handle != NULL)
389a64788d1SPyun YongHyeon 		bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_irq_handle);
390a64788d1SPyun YongHyeon 	if (sc->sc_irq_res != NULL)
391a64788d1SPyun YongHyeon 		bus_release_resource(dev, SYS_RES_IRQ,
392a64788d1SPyun YongHyeon 		    rman_get_rid(sc->sc_irq_res), sc->sc_irq_res);
393cc3c3b4eSPyun YongHyeon 	if ((sc->sc_flags & ET_FLAG_MSI) != 0)
394cc3c3b4eSPyun YongHyeon 		pci_release_msi(dev);
395a64788d1SPyun YongHyeon 	if (sc->sc_mem_res != NULL)
396a64788d1SPyun YongHyeon 		bus_release_resource(dev, SYS_RES_MEMORY,
397a64788d1SPyun YongHyeon 		    rman_get_rid(sc->sc_mem_res), sc->sc_mem_res);
3984d52a575SXin LI 
3994d52a575SXin LI 	if (sc->ifp != NULL)
4004d52a575SXin LI 		if_free(sc->ifp);
4014d52a575SXin LI 
40205884511SPyun YongHyeon 	et_dma_free(sc);
4035b8f4900SPyun YongHyeon 
4045b8f4900SPyun YongHyeon 	mtx_destroy(&sc->sc_mtx);
4054d52a575SXin LI 
406398f1b65SPyun YongHyeon 	return (0);
4074d52a575SXin LI }
4084d52a575SXin LI 
4094d52a575SXin LI static int
4104d52a575SXin LI et_shutdown(device_t dev)
4114d52a575SXin LI {
4120b699044SPyun YongHyeon 	struct et_softc *sc;
4134d52a575SXin LI 
4140b699044SPyun YongHyeon 	sc = device_get_softc(dev);
4154d52a575SXin LI 	ET_LOCK(sc);
4164d52a575SXin LI 	et_stop(sc);
4174d52a575SXin LI 	ET_UNLOCK(sc);
418398f1b65SPyun YongHyeon 	return (0);
4194d52a575SXin LI }
4204d52a575SXin LI 
4214d52a575SXin LI static int
4224d52a575SXin LI et_miibus_readreg(device_t dev, int phy, int reg)
4234d52a575SXin LI {
4240b699044SPyun YongHyeon 	struct et_softc *sc;
4254d52a575SXin LI 	uint32_t val;
4264d52a575SXin LI 	int i, ret;
4274d52a575SXin LI 
4280b699044SPyun YongHyeon 	sc = device_get_softc(dev);
4294d52a575SXin LI 	/* Stop any pending operations */
4304d52a575SXin LI 	CSR_WRITE_4(sc, ET_MII_CMD, 0);
4314d52a575SXin LI 
43223263665SPyun YongHyeon 	val = (phy << ET_MII_ADDR_PHY_SHIFT) & ET_MII_ADDR_PHY_MASK;
43323263665SPyun YongHyeon 	val |= (reg << ET_MII_ADDR_REG_SHIFT) & ET_MII_ADDR_REG_MASK;
4344d52a575SXin LI 	CSR_WRITE_4(sc, ET_MII_ADDR, val);
4354d52a575SXin LI 
4364d52a575SXin LI 	/* Start reading */
4374d52a575SXin LI 	CSR_WRITE_4(sc, ET_MII_CMD, ET_MII_CMD_READ);
4384d52a575SXin LI 
4394d52a575SXin LI #define NRETRY	50
4404d52a575SXin LI 
4414d52a575SXin LI 	for (i = 0; i < NRETRY; ++i) {
4424d52a575SXin LI 		val = CSR_READ_4(sc, ET_MII_IND);
4434d52a575SXin LI 		if ((val & (ET_MII_IND_BUSY | ET_MII_IND_INVALID)) == 0)
4444d52a575SXin LI 			break;
4454d52a575SXin LI 		DELAY(50);
4464d52a575SXin LI 	}
4474d52a575SXin LI 	if (i == NRETRY) {
4484d52a575SXin LI 		if_printf(sc->ifp,
4494d52a575SXin LI 			  "read phy %d, reg %d timed out\n", phy, reg);
4504d52a575SXin LI 		ret = 0;
4514d52a575SXin LI 		goto back;
4524d52a575SXin LI 	}
4534d52a575SXin LI 
4544d52a575SXin LI #undef NRETRY
4554d52a575SXin LI 
4564d52a575SXin LI 	val = CSR_READ_4(sc, ET_MII_STAT);
45723263665SPyun YongHyeon 	ret = val & ET_MII_STAT_VALUE_MASK;
4584d52a575SXin LI 
4594d52a575SXin LI back:
4604d52a575SXin LI 	/* Make sure that the current operation is stopped */
4614d52a575SXin LI 	CSR_WRITE_4(sc, ET_MII_CMD, 0);
462398f1b65SPyun YongHyeon 	return (ret);
4634d52a575SXin LI }
4644d52a575SXin LI 
4654d52a575SXin LI static int
4664d52a575SXin LI et_miibus_writereg(device_t dev, int phy, int reg, int val0)
4674d52a575SXin LI {
4680b699044SPyun YongHyeon 	struct et_softc *sc;
4694d52a575SXin LI 	uint32_t val;
4704d52a575SXin LI 	int i;
4714d52a575SXin LI 
4720b699044SPyun YongHyeon 	sc = device_get_softc(dev);
4734d52a575SXin LI 	/* Stop any pending operations */
4744d52a575SXin LI 	CSR_WRITE_4(sc, ET_MII_CMD, 0);
4754d52a575SXin LI 
47623263665SPyun YongHyeon 	val = (phy << ET_MII_ADDR_PHY_SHIFT) & ET_MII_ADDR_PHY_MASK;
47723263665SPyun YongHyeon 	val |= (reg << ET_MII_ADDR_REG_SHIFT) & ET_MII_ADDR_REG_MASK;
4784d52a575SXin LI 	CSR_WRITE_4(sc, ET_MII_ADDR, val);
4794d52a575SXin LI 
4804d52a575SXin LI 	/* Start writing */
48123263665SPyun YongHyeon 	CSR_WRITE_4(sc, ET_MII_CTRL,
48223263665SPyun YongHyeon 	    (val0 << ET_MII_CTRL_VALUE_SHIFT) & ET_MII_CTRL_VALUE_MASK);
4834d52a575SXin LI 
4844d52a575SXin LI #define NRETRY 100
4854d52a575SXin LI 
4864d52a575SXin LI 	for (i = 0; i < NRETRY; ++i) {
4874d52a575SXin LI 		val = CSR_READ_4(sc, ET_MII_IND);
4884d52a575SXin LI 		if ((val & ET_MII_IND_BUSY) == 0)
4894d52a575SXin LI 			break;
4904d52a575SXin LI 		DELAY(50);
4914d52a575SXin LI 	}
4924d52a575SXin LI 	if (i == NRETRY) {
4934d52a575SXin LI 		if_printf(sc->ifp,
4944d52a575SXin LI 			  "write phy %d, reg %d timed out\n", phy, reg);
4954d52a575SXin LI 		et_miibus_readreg(dev, phy, reg);
4964d52a575SXin LI 	}
4974d52a575SXin LI 
4984d52a575SXin LI #undef NRETRY
4994d52a575SXin LI 
5004d52a575SXin LI 	/* Make sure that the current operation is stopped */
5014d52a575SXin LI 	CSR_WRITE_4(sc, ET_MII_CMD, 0);
502398f1b65SPyun YongHyeon 	return (0);
5034d52a575SXin LI }
5044d52a575SXin LI 
5054d52a575SXin LI static void
5064d52a575SXin LI et_miibus_statchg(device_t dev)
5074d52a575SXin LI {
5081f009e2fSPyun YongHyeon 	struct et_softc *sc;
5091f009e2fSPyun YongHyeon 	struct mii_data *mii;
5101f009e2fSPyun YongHyeon 	struct ifnet *ifp;
5111f009e2fSPyun YongHyeon 	uint32_t cfg1, cfg2, ctrl;
5121f009e2fSPyun YongHyeon 	int i;
5131f009e2fSPyun YongHyeon 
5141f009e2fSPyun YongHyeon 	sc = device_get_softc(dev);
5151f009e2fSPyun YongHyeon 
5161f009e2fSPyun YongHyeon 	mii = device_get_softc(sc->sc_miibus);
5171f009e2fSPyun YongHyeon 	ifp = sc->ifp;
5181f009e2fSPyun YongHyeon 	if (mii == NULL || ifp == NULL ||
5191f009e2fSPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
5201f009e2fSPyun YongHyeon 		return;
5211f009e2fSPyun YongHyeon 
5221f009e2fSPyun YongHyeon 	sc->sc_flags &= ~ET_FLAG_LINK;
5231f009e2fSPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
5241f009e2fSPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
5251f009e2fSPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
5261f009e2fSPyun YongHyeon 		case IFM_10_T:
5271f009e2fSPyun YongHyeon 		case IFM_100_TX:
5281f009e2fSPyun YongHyeon 			sc->sc_flags |= ET_FLAG_LINK;
5291f009e2fSPyun YongHyeon 			break;
5301f009e2fSPyun YongHyeon 		case IFM_1000_T:
5311f009e2fSPyun YongHyeon 			if ((sc->sc_flags & ET_FLAG_FASTETHER) == 0)
5321f009e2fSPyun YongHyeon 				sc->sc_flags |= ET_FLAG_LINK;
5331f009e2fSPyun YongHyeon 			break;
5341f009e2fSPyun YongHyeon 		}
5351f009e2fSPyun YongHyeon 	}
5361f009e2fSPyun YongHyeon 
5371f009e2fSPyun YongHyeon 	/* XXX Stop TX/RX MAC? */
5381f009e2fSPyun YongHyeon 	if ((sc->sc_flags & ET_FLAG_LINK) == 0)
5391f009e2fSPyun YongHyeon 		return;
5401f009e2fSPyun YongHyeon 
5411f009e2fSPyun YongHyeon 	/* Program MACs with resolved speed/duplex/flow-control. */
5421f009e2fSPyun YongHyeon 	ctrl = CSR_READ_4(sc, ET_MAC_CTRL);
5431f009e2fSPyun YongHyeon 	ctrl &= ~(ET_MAC_CTRL_GHDX | ET_MAC_CTRL_MODE_MII);
5441f009e2fSPyun YongHyeon 	cfg1 = CSR_READ_4(sc, ET_MAC_CFG1);
5451f009e2fSPyun YongHyeon 	cfg1 &= ~(ET_MAC_CFG1_TXFLOW | ET_MAC_CFG1_RXFLOW |
5461f009e2fSPyun YongHyeon 	    ET_MAC_CFG1_LOOPBACK);
5471f009e2fSPyun YongHyeon 	cfg2 = CSR_READ_4(sc, ET_MAC_CFG2);
5481f009e2fSPyun YongHyeon 	cfg2 &= ~(ET_MAC_CFG2_MODE_MII | ET_MAC_CFG2_MODE_GMII |
5491f009e2fSPyun YongHyeon 	    ET_MAC_CFG2_FDX | ET_MAC_CFG2_BIGFRM);
5501f009e2fSPyun YongHyeon 	cfg2 |= ET_MAC_CFG2_LENCHK | ET_MAC_CFG2_CRC | ET_MAC_CFG2_PADCRC |
5511f009e2fSPyun YongHyeon 	    ((7 << ET_MAC_CFG2_PREAMBLE_LEN_SHIFT) &
5521f009e2fSPyun YongHyeon 	    ET_MAC_CFG2_PREAMBLE_LEN_MASK);
5531f009e2fSPyun YongHyeon 
5541f009e2fSPyun YongHyeon 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T)
5551f009e2fSPyun YongHyeon 		cfg2 |= ET_MAC_CFG2_MODE_GMII;
5561f009e2fSPyun YongHyeon 	else {
5571f009e2fSPyun YongHyeon 		cfg2 |= ET_MAC_CFG2_MODE_MII;
5581f009e2fSPyun YongHyeon 		ctrl |= ET_MAC_CTRL_MODE_MII;
5591f009e2fSPyun YongHyeon 	}
5601f009e2fSPyun YongHyeon 
5611f009e2fSPyun YongHyeon 	if (IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) {
5621f009e2fSPyun YongHyeon 		cfg2 |= ET_MAC_CFG2_FDX;
5635d384a0dSPyun YongHyeon 		/*
5645d384a0dSPyun YongHyeon 		 * Controller lacks automatic TX pause frame
5655d384a0dSPyun YongHyeon 		 * generation so it should be handled by driver.
5665d384a0dSPyun YongHyeon 		 * Even though driver can send pause frame with
5675d384a0dSPyun YongHyeon 		 * arbitrary pause time, controller does not
5685d384a0dSPyun YongHyeon 		 * provide a way that tells how many free RX
5695d384a0dSPyun YongHyeon 		 * buffers are available in controller.  This
5705d384a0dSPyun YongHyeon 		 * limitation makes it hard to generate XON frame
5715d384a0dSPyun YongHyeon 		 * in time on driver side so don't enable TX flow
5725d384a0dSPyun YongHyeon 		 * control.
5735d384a0dSPyun YongHyeon 		 */
5741f009e2fSPyun YongHyeon #ifdef notyet
5751f009e2fSPyun YongHyeon 		if (IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE)
5761f009e2fSPyun YongHyeon 			cfg1 |= ET_MAC_CFG1_TXFLOW;
5775d384a0dSPyun YongHyeon #endif
5781f009e2fSPyun YongHyeon 		if (IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE)
5791f009e2fSPyun YongHyeon 			cfg1 |= ET_MAC_CFG1_RXFLOW;
5801f009e2fSPyun YongHyeon 	} else
5811f009e2fSPyun YongHyeon 		ctrl |= ET_MAC_CTRL_GHDX;
5821f009e2fSPyun YongHyeon 
5831f009e2fSPyun YongHyeon 	CSR_WRITE_4(sc, ET_MAC_CTRL, ctrl);
5841f009e2fSPyun YongHyeon 	CSR_WRITE_4(sc, ET_MAC_CFG2, cfg2);
5851f009e2fSPyun YongHyeon 	cfg1 |= ET_MAC_CFG1_TXEN | ET_MAC_CFG1_RXEN;
5861f009e2fSPyun YongHyeon 	CSR_WRITE_4(sc, ET_MAC_CFG1, cfg1);
5871f009e2fSPyun YongHyeon 
5881f009e2fSPyun YongHyeon #define NRETRY	50
5891f009e2fSPyun YongHyeon 
5901f009e2fSPyun YongHyeon 	for (i = 0; i < NRETRY; ++i) {
5911f009e2fSPyun YongHyeon 		cfg1 = CSR_READ_4(sc, ET_MAC_CFG1);
5921f009e2fSPyun YongHyeon 		if ((cfg1 & (ET_MAC_CFG1_SYNC_TXEN | ET_MAC_CFG1_SYNC_RXEN)) ==
5931f009e2fSPyun YongHyeon 		    (ET_MAC_CFG1_SYNC_TXEN | ET_MAC_CFG1_SYNC_RXEN))
5941f009e2fSPyun YongHyeon 			break;
5951f009e2fSPyun YongHyeon 		DELAY(100);
5961f009e2fSPyun YongHyeon 	}
5971f009e2fSPyun YongHyeon 	if (i == NRETRY)
5981f009e2fSPyun YongHyeon 		if_printf(ifp, "can't enable RX/TX\n");
5991f009e2fSPyun YongHyeon 	sc->sc_flags |= ET_FLAG_TXRX_ENABLED;
6001f009e2fSPyun YongHyeon 
6011f009e2fSPyun YongHyeon #undef NRETRY
6024d52a575SXin LI }
6034d52a575SXin LI 
6044d52a575SXin LI static int
6054d52a575SXin LI et_ifmedia_upd_locked(struct ifnet *ifp)
6064d52a575SXin LI {
6070b699044SPyun YongHyeon 	struct et_softc *sc;
6080b699044SPyun YongHyeon 	struct mii_data *mii;
6094d52a575SXin LI 	struct mii_softc *miisc;
6104d52a575SXin LI 
6110b699044SPyun YongHyeon 	sc = ifp->if_softc;
6120b699044SPyun YongHyeon 	mii = device_get_softc(sc->sc_miibus);
6134d52a575SXin LI 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
6143fcb7a53SMarius Strobl 		PHY_RESET(miisc);
61596570638SPyun YongHyeon 	return (mii_mediachg(mii));
6164d52a575SXin LI }
6174d52a575SXin LI 
6184d52a575SXin LI static int
6194d52a575SXin LI et_ifmedia_upd(struct ifnet *ifp)
6204d52a575SXin LI {
6210b699044SPyun YongHyeon 	struct et_softc *sc;
6224d52a575SXin LI 	int res;
6234d52a575SXin LI 
6240b699044SPyun YongHyeon 	sc = ifp->if_softc;
6254d52a575SXin LI 	ET_LOCK(sc);
6264d52a575SXin LI 	res = et_ifmedia_upd_locked(ifp);
6274d52a575SXin LI 	ET_UNLOCK(sc);
6284d52a575SXin LI 
629398f1b65SPyun YongHyeon 	return (res);
6304d52a575SXin LI }
6314d52a575SXin LI 
6324d52a575SXin LI static void
6334d52a575SXin LI et_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
6344d52a575SXin LI {
6351f009e2fSPyun YongHyeon 	struct et_softc *sc;
6361f009e2fSPyun YongHyeon 	struct mii_data *mii;
6374d52a575SXin LI 
6381f009e2fSPyun YongHyeon 	sc = ifp->if_softc;
6390ae9f6a9SPyun YongHyeon 	ET_LOCK(sc);
6401f009e2fSPyun YongHyeon 	if ((ifp->if_flags & IFF_UP) == 0) {
6411f009e2fSPyun YongHyeon 		ET_UNLOCK(sc);
6421f009e2fSPyun YongHyeon 		return;
6431f009e2fSPyun YongHyeon 	}
6441f009e2fSPyun YongHyeon 
6451f009e2fSPyun YongHyeon 	mii = device_get_softc(sc->sc_miibus);
6464d52a575SXin LI 	mii_pollstat(mii);
6474d52a575SXin LI 	ifmr->ifm_active = mii->mii_media_active;
6484d52a575SXin LI 	ifmr->ifm_status = mii->mii_media_status;
6490ae9f6a9SPyun YongHyeon 	ET_UNLOCK(sc);
6504d52a575SXin LI }
6514d52a575SXin LI 
6524d52a575SXin LI static void
6534d52a575SXin LI et_stop(struct et_softc *sc)
6544d52a575SXin LI {
6550b699044SPyun YongHyeon 	struct ifnet *ifp;
6564d52a575SXin LI 
6574d52a575SXin LI 	ET_LOCK_ASSERT(sc);
6584d52a575SXin LI 
6590b699044SPyun YongHyeon 	ifp = sc->ifp;
6604d52a575SXin LI 	callout_stop(&sc->sc_tick);
6616537ffa6SPyun YongHyeon 	/* Disable interrupts. */
6626537ffa6SPyun YongHyeon 	CSR_WRITE_4(sc, ET_INTR_MASK, 0xffffffff);
6634d52a575SXin LI 
6641f009e2fSPyun YongHyeon 	CSR_WRITE_4(sc, ET_MAC_CFG1, CSR_READ_4(sc, ET_MAC_CFG1) & ~(
6651f009e2fSPyun YongHyeon 	    ET_MAC_CFG1_TXEN | ET_MAC_CFG1_RXEN));
6661f009e2fSPyun YongHyeon 	DELAY(100);
6671f009e2fSPyun YongHyeon 
6684d52a575SXin LI 	et_stop_rxdma(sc);
6694d52a575SXin LI 	et_stop_txdma(sc);
670e0b5ac02SPyun YongHyeon 	et_stats_update(sc);
6714d52a575SXin LI 
6724d52a575SXin LI 	et_free_tx_ring(sc);
6734d52a575SXin LI 	et_free_rx_ring(sc);
6744d52a575SXin LI 
6754d52a575SXin LI 	sc->sc_tx = 0;
6764d52a575SXin LI 	sc->sc_tx_intr = 0;
6774d52a575SXin LI 	sc->sc_flags &= ~ET_FLAG_TXRX_ENABLED;
6784d52a575SXin LI 
6794d52a575SXin LI 	sc->watchdog_timer = 0;
6804d52a575SXin LI 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
6814d52a575SXin LI }
6824d52a575SXin LI 
6834d52a575SXin LI static int
6848b3c6496SPyun YongHyeon et_bus_config(struct et_softc *sc)
6854d52a575SXin LI {
6864d52a575SXin LI 	uint32_t val, max_plsz;
6874d52a575SXin LI 	uint16_t ack_latency, replay_timer;
6884d52a575SXin LI 
6894d52a575SXin LI 	/*
6904d52a575SXin LI 	 * Test whether EEPROM is valid
6914d52a575SXin LI 	 * NOTE: Read twice to get the correct value
6924d52a575SXin LI 	 */
6938b3c6496SPyun YongHyeon 	pci_read_config(sc->dev, ET_PCIR_EEPROM_STATUS, 1);
6948b3c6496SPyun YongHyeon 	val = pci_read_config(sc->dev, ET_PCIR_EEPROM_STATUS, 1);
6954d52a575SXin LI 	if (val & ET_PCIM_EEPROM_STATUS_ERROR) {
6968b3c6496SPyun YongHyeon 		device_printf(sc->dev, "EEPROM status error 0x%02x\n", val);
697398f1b65SPyun YongHyeon 		return (ENXIO);
6984d52a575SXin LI 	}
6994d52a575SXin LI 
7004d52a575SXin LI 	/* TODO: LED */
7014d52a575SXin LI 
7028b3c6496SPyun YongHyeon 	if ((sc->sc_flags & ET_FLAG_PCIE) == 0)
7038b3c6496SPyun YongHyeon 		return (0);
7048b3c6496SPyun YongHyeon 
7054d52a575SXin LI 	/*
7064d52a575SXin LI 	 * Configure ACK latency and replay timer according to
7074d52a575SXin LI 	 * max playload size
7084d52a575SXin LI 	 */
7098b3c6496SPyun YongHyeon 	val = pci_read_config(sc->dev,
710389c8bd5SGavin Atkinson 	    sc->sc_expcap + PCIER_DEVICE_CAP, 4);
711389c8bd5SGavin Atkinson 	max_plsz = val & PCIEM_CAP_MAX_PAYLOAD;
7124d52a575SXin LI 
7134d52a575SXin LI 	switch (max_plsz) {
7144d52a575SXin LI 	case ET_PCIV_DEVICE_CAPS_PLSZ_128:
7154d52a575SXin LI 		ack_latency = ET_PCIV_ACK_LATENCY_128;
7164d52a575SXin LI 		replay_timer = ET_PCIV_REPLAY_TIMER_128;
7174d52a575SXin LI 		break;
7184d52a575SXin LI 
7194d52a575SXin LI 	case ET_PCIV_DEVICE_CAPS_PLSZ_256:
7204d52a575SXin LI 		ack_latency = ET_PCIV_ACK_LATENCY_256;
7214d52a575SXin LI 		replay_timer = ET_PCIV_REPLAY_TIMER_256;
7224d52a575SXin LI 		break;
7234d52a575SXin LI 
7244d52a575SXin LI 	default:
7258b3c6496SPyun YongHyeon 		ack_latency = pci_read_config(sc->dev, ET_PCIR_ACK_LATENCY, 2);
7268b3c6496SPyun YongHyeon 		replay_timer = pci_read_config(sc->dev,
7278b3c6496SPyun YongHyeon 		    ET_PCIR_REPLAY_TIMER, 2);
7288b3c6496SPyun YongHyeon 		device_printf(sc->dev, "ack latency %u, replay timer %u\n",
7294d52a575SXin LI 			      ack_latency, replay_timer);
7304d52a575SXin LI 		break;
7314d52a575SXin LI 	}
7324d52a575SXin LI 	if (ack_latency != 0) {
7338b3c6496SPyun YongHyeon 		pci_write_config(sc->dev, ET_PCIR_ACK_LATENCY, ack_latency, 2);
7348b3c6496SPyun YongHyeon 		pci_write_config(sc->dev, ET_PCIR_REPLAY_TIMER, replay_timer,
7358b3c6496SPyun YongHyeon 		    2);
7364d52a575SXin LI 	}
7374d52a575SXin LI 
7384d52a575SXin LI 	/*
7394d52a575SXin LI 	 * Set L0s and L1 latency timer to 2us
7404d52a575SXin LI 	 */
7418b3c6496SPyun YongHyeon 	val = pci_read_config(sc->dev, ET_PCIR_L0S_L1_LATENCY, 4);
742389c8bd5SGavin Atkinson 	val &= ~(PCIEM_LINK_CAP_L0S_EXIT | PCIEM_LINK_CAP_L1_EXIT);
74323263665SPyun YongHyeon 	/* L0s exit latency : 2us */
74423263665SPyun YongHyeon 	val |= 0x00005000;
74523263665SPyun YongHyeon 	/* L1 exit latency : 2us */
74623263665SPyun YongHyeon 	val |= 0x00028000;
7478b3c6496SPyun YongHyeon 	pci_write_config(sc->dev, ET_PCIR_L0S_L1_LATENCY, val, 4);
7484d52a575SXin LI 
7494d52a575SXin LI 	/*
7504d52a575SXin LI 	 * Set max read request size to 2048 bytes
7514d52a575SXin LI 	 */
75239bea5ddSPyun YongHyeon 	pci_set_max_read_req(sc->dev, 2048);
7534d52a575SXin LI 
754398f1b65SPyun YongHyeon 	return (0);
7554d52a575SXin LI }
7564d52a575SXin LI 
7574d52a575SXin LI static void
7584d52a575SXin LI et_get_eaddr(device_t dev, uint8_t eaddr[])
7594d52a575SXin LI {
7604d52a575SXin LI 	uint32_t val;
7614d52a575SXin LI 	int i;
7624d52a575SXin LI 
7634d52a575SXin LI 	val = pci_read_config(dev, ET_PCIR_MAC_ADDR0, 4);
7644d52a575SXin LI 	for (i = 0; i < 4; ++i)
7654d52a575SXin LI 		eaddr[i] = (val >> (8 * i)) & 0xff;
7664d52a575SXin LI 
7674d52a575SXin LI 	val = pci_read_config(dev, ET_PCIR_MAC_ADDR1, 2);
7684d52a575SXin LI 	for (; i < ETHER_ADDR_LEN; ++i)
7694d52a575SXin LI 		eaddr[i] = (val >> (8 * (i - 4))) & 0xff;
7704d52a575SXin LI }
7714d52a575SXin LI 
7724d52a575SXin LI static void
7734d52a575SXin LI et_reset(struct et_softc *sc)
7744d52a575SXin LI {
7750b699044SPyun YongHyeon 
7764d52a575SXin LI 	CSR_WRITE_4(sc, ET_MAC_CFG1,
7774d52a575SXin LI 		    ET_MAC_CFG1_RST_TXFUNC | ET_MAC_CFG1_RST_RXFUNC |
7784d52a575SXin LI 		    ET_MAC_CFG1_RST_TXMC | ET_MAC_CFG1_RST_RXMC |
7794d52a575SXin LI 		    ET_MAC_CFG1_SIM_RST | ET_MAC_CFG1_SOFT_RST);
7804d52a575SXin LI 
7814d52a575SXin LI 	CSR_WRITE_4(sc, ET_SWRST,
7824d52a575SXin LI 		    ET_SWRST_TXDMA | ET_SWRST_RXDMA |
7834d52a575SXin LI 		    ET_SWRST_TXMAC | ET_SWRST_RXMAC |
7844d52a575SXin LI 		    ET_SWRST_MAC | ET_SWRST_MAC_STAT | ET_SWRST_MMC);
7854d52a575SXin LI 
7864d52a575SXin LI 	CSR_WRITE_4(sc, ET_MAC_CFG1,
7874d52a575SXin LI 		    ET_MAC_CFG1_RST_TXFUNC | ET_MAC_CFG1_RST_RXFUNC |
7884d52a575SXin LI 		    ET_MAC_CFG1_RST_TXMC | ET_MAC_CFG1_RST_RXMC);
7894d52a575SXin LI 	CSR_WRITE_4(sc, ET_MAC_CFG1, 0);
7906537ffa6SPyun YongHyeon 	/* Disable interrupts. */
7914d52a575SXin LI 	CSR_WRITE_4(sc, ET_INTR_MASK, 0xffffffff);
7924d52a575SXin LI }
7934d52a575SXin LI 
79405884511SPyun YongHyeon struct et_dmamap_arg {
79505884511SPyun YongHyeon 	bus_addr_t	et_busaddr;
79605884511SPyun YongHyeon };
79705884511SPyun YongHyeon 
79805884511SPyun YongHyeon static void
79905884511SPyun YongHyeon et_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
8004d52a575SXin LI {
80105884511SPyun YongHyeon 	struct et_dmamap_arg *ctx;
80205884511SPyun YongHyeon 
80305884511SPyun YongHyeon 	if (error)
80405884511SPyun YongHyeon 		return;
80505884511SPyun YongHyeon 
80605884511SPyun YongHyeon 	KASSERT(nseg == 1, ("%s: %d segments returned!", __func__, nseg));
80705884511SPyun YongHyeon 
80805884511SPyun YongHyeon 	ctx = arg;
80905884511SPyun YongHyeon 	ctx->et_busaddr = segs->ds_addr;
81005884511SPyun YongHyeon }
81105884511SPyun YongHyeon 
81205884511SPyun YongHyeon static int
81305884511SPyun YongHyeon et_dma_ring_alloc(struct et_softc *sc, bus_size_t alignment, bus_size_t maxsize,
81405884511SPyun YongHyeon     bus_dma_tag_t *tag, uint8_t **ring, bus_dmamap_t *map, bus_addr_t *paddr,
81505884511SPyun YongHyeon     const char *msg)
81605884511SPyun YongHyeon {
81705884511SPyun YongHyeon 	struct et_dmamap_arg ctx;
81805884511SPyun YongHyeon 	int error;
81905884511SPyun YongHyeon 
82005884511SPyun YongHyeon 	error = bus_dma_tag_create(sc->sc_dtag, alignment, 0, BUS_SPACE_MAXADDR,
82105884511SPyun YongHyeon 	    BUS_SPACE_MAXADDR, NULL, NULL, maxsize, 1, maxsize, 0, NULL, NULL,
82205884511SPyun YongHyeon 	    tag);
82305884511SPyun YongHyeon 	if (error != 0) {
82405884511SPyun YongHyeon 		device_printf(sc->dev, "could not create %s dma tag\n", msg);
82505884511SPyun YongHyeon 		return (error);
82605884511SPyun YongHyeon 	}
82705884511SPyun YongHyeon 	/* Allocate DMA'able memory for ring. */
82805884511SPyun YongHyeon 	error = bus_dmamem_alloc(*tag, (void **)ring,
82905884511SPyun YongHyeon 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT, map);
83005884511SPyun YongHyeon 	if (error != 0) {
83105884511SPyun YongHyeon 		device_printf(sc->dev,
83205884511SPyun YongHyeon 		    "could not allocate DMA'able memory for %s\n", msg);
83305884511SPyun YongHyeon 		return (error);
83405884511SPyun YongHyeon 	}
83505884511SPyun YongHyeon 	/* Load the address of the ring. */
83605884511SPyun YongHyeon 	ctx.et_busaddr = 0;
83705884511SPyun YongHyeon 	error = bus_dmamap_load(*tag, *map, *ring, maxsize, et_dma_map_addr,
83805884511SPyun YongHyeon 	    &ctx, BUS_DMA_NOWAIT);
83905884511SPyun YongHyeon 	if (error != 0) {
84005884511SPyun YongHyeon 		device_printf(sc->dev,
84105884511SPyun YongHyeon 		    "could not load DMA'able memory for %s\n", msg);
84205884511SPyun YongHyeon 		return (error);
84305884511SPyun YongHyeon 	}
84405884511SPyun YongHyeon 	*paddr = ctx.et_busaddr;
84505884511SPyun YongHyeon 	return (0);
84605884511SPyun YongHyeon }
84705884511SPyun YongHyeon 
84805884511SPyun YongHyeon static void
84905884511SPyun YongHyeon et_dma_ring_free(struct et_softc *sc, bus_dma_tag_t *tag, uint8_t **ring,
850c34f1a08SJohn Baldwin     bus_dmamap_t map, bus_addr_t *paddr)
85105884511SPyun YongHyeon {
85205884511SPyun YongHyeon 
853c34f1a08SJohn Baldwin 	if (*paddr != 0) {
854c34f1a08SJohn Baldwin 		bus_dmamap_unload(*tag, map);
855c34f1a08SJohn Baldwin 		*paddr = 0;
856c34f1a08SJohn Baldwin 	}
857c34f1a08SJohn Baldwin 	if (*ring != NULL) {
858c34f1a08SJohn Baldwin 		bus_dmamem_free(*tag, *ring, map);
85905884511SPyun YongHyeon 		*ring = NULL;
86005884511SPyun YongHyeon 	}
86105884511SPyun YongHyeon 	if (*tag) {
86205884511SPyun YongHyeon 		bus_dma_tag_destroy(*tag);
86305884511SPyun YongHyeon 		*tag = NULL;
86405884511SPyun YongHyeon 	}
86505884511SPyun YongHyeon }
86605884511SPyun YongHyeon 
86705884511SPyun YongHyeon static int
86805884511SPyun YongHyeon et_dma_alloc(struct et_softc *sc)
86905884511SPyun YongHyeon {
87005884511SPyun YongHyeon 	struct et_txdesc_ring *tx_ring;
87105884511SPyun YongHyeon 	struct et_rxdesc_ring *rx_ring;
87205884511SPyun YongHyeon 	struct et_rxstat_ring *rxst_ring;
87305884511SPyun YongHyeon 	struct et_rxstatus_data *rxsd;
87405884511SPyun YongHyeon 	struct et_rxbuf_data *rbd;
87505884511SPyun YongHyeon         struct et_txbuf_data *tbd;
87605884511SPyun YongHyeon 	struct et_txstatus_data *txsd;
8774d52a575SXin LI 	int i, error;
8784d52a575SXin LI 
87905884511SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 1, 0,
88005884511SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
88105884511SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0, NULL, NULL,
88205884511SPyun YongHyeon 	    &sc->sc_dtag);
88305884511SPyun YongHyeon 	if (error != 0) {
88405884511SPyun YongHyeon 		device_printf(sc->dev, "could not allocate parent dma tag\n");
885398f1b65SPyun YongHyeon 		return (error);
8864d52a575SXin LI 	}
8874d52a575SXin LI 
88805884511SPyun YongHyeon 	/* TX ring. */
88905884511SPyun YongHyeon 	tx_ring = &sc->sc_tx_ring;
89005884511SPyun YongHyeon 	error = et_dma_ring_alloc(sc, ET_RING_ALIGN, ET_TX_RING_SIZE,
89105884511SPyun YongHyeon 	    &tx_ring->tr_dtag, (uint8_t **)&tx_ring->tr_desc, &tx_ring->tr_dmap,
89205884511SPyun YongHyeon 	    &tx_ring->tr_paddr, "TX ring");
8934d52a575SXin LI 	if (error)
894398f1b65SPyun YongHyeon 		return (error);
8954d52a575SXin LI 
89605884511SPyun YongHyeon 	/* TX status block. */
89705884511SPyun YongHyeon 	txsd = &sc->sc_tx_status;
89805884511SPyun YongHyeon 	error = et_dma_ring_alloc(sc, ET_STATUS_ALIGN, sizeof(uint32_t),
89905884511SPyun YongHyeon 	    &txsd->txsd_dtag, (uint8_t **)&txsd->txsd_status, &txsd->txsd_dmap,
90005884511SPyun YongHyeon 	    &txsd->txsd_paddr, "TX status block");
90105884511SPyun YongHyeon 	if (error)
90205884511SPyun YongHyeon 		return (error);
9034d52a575SXin LI 
90405884511SPyun YongHyeon 	/* RX ring 0, used as to recive small sized frames. */
90505884511SPyun YongHyeon 	rx_ring = &sc->sc_rx_ring[0];
90605884511SPyun YongHyeon 	error = et_dma_ring_alloc(sc, ET_RING_ALIGN, ET_RX_RING_SIZE,
90705884511SPyun YongHyeon 	    &rx_ring->rr_dtag, (uint8_t **)&rx_ring->rr_desc, &rx_ring->rr_dmap,
90805884511SPyun YongHyeon 	    &rx_ring->rr_paddr, "RX ring 0");
90905884511SPyun YongHyeon 	rx_ring->rr_posreg = ET_RX_RING0_POS;
91005884511SPyun YongHyeon 	if (error)
91105884511SPyun YongHyeon 		return (error);
9124d52a575SXin LI 
91305884511SPyun YongHyeon 	/* RX ring 1, used as to store normal sized frames. */
91405884511SPyun YongHyeon 	rx_ring = &sc->sc_rx_ring[1];
91505884511SPyun YongHyeon 	error = et_dma_ring_alloc(sc, ET_RING_ALIGN, ET_RX_RING_SIZE,
91605884511SPyun YongHyeon 	    &rx_ring->rr_dtag, (uint8_t **)&rx_ring->rr_desc, &rx_ring->rr_dmap,
91705884511SPyun YongHyeon 	    &rx_ring->rr_paddr, "RX ring 1");
91805884511SPyun YongHyeon 	rx_ring->rr_posreg = ET_RX_RING1_POS;
91905884511SPyun YongHyeon 	if (error)
92005884511SPyun YongHyeon 		return (error);
9214d52a575SXin LI 
92205884511SPyun YongHyeon 	/* RX stat ring. */
92305884511SPyun YongHyeon 	rxst_ring = &sc->sc_rxstat_ring;
92405884511SPyun YongHyeon 	error = et_dma_ring_alloc(sc, ET_RING_ALIGN, ET_RXSTAT_RING_SIZE,
92505884511SPyun YongHyeon 	    &rxst_ring->rsr_dtag, (uint8_t **)&rxst_ring->rsr_stat,
92605884511SPyun YongHyeon 	    &rxst_ring->rsr_dmap, &rxst_ring->rsr_paddr, "RX stat ring");
92705884511SPyun YongHyeon 	if (error)
92805884511SPyun YongHyeon 		return (error);
9294d52a575SXin LI 
93005884511SPyun YongHyeon 	/* RX status block. */
93105884511SPyun YongHyeon 	rxsd = &sc->sc_rx_status;
93205884511SPyun YongHyeon 	error = et_dma_ring_alloc(sc, ET_STATUS_ALIGN,
93305884511SPyun YongHyeon 	    sizeof(struct et_rxstatus), &rxsd->rxsd_dtag,
93405884511SPyun YongHyeon 	    (uint8_t **)&rxsd->rxsd_status, &rxsd->rxsd_dmap,
93505884511SPyun YongHyeon 	    &rxsd->rxsd_paddr, "RX status block");
93605884511SPyun YongHyeon 	if (error)
93705884511SPyun YongHyeon 		return (error);
9384d52a575SXin LI 
93905884511SPyun YongHyeon 	/* Create parent DMA tag for mbufs. */
94005884511SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 1, 0,
94105884511SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
94205884511SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0, NULL, NULL,
94305884511SPyun YongHyeon 	    &sc->sc_mbuf_dtag);
94405884511SPyun YongHyeon 	if (error != 0) {
94505884511SPyun YongHyeon 		device_printf(sc->dev,
94605884511SPyun YongHyeon 		    "could not allocate parent dma tag for mbuf\n");
947398f1b65SPyun YongHyeon 		return (error);
9484d52a575SXin LI 	}
9494d52a575SXin LI 
95005884511SPyun YongHyeon 	/* Create DMA tag for mini RX mbufs to use RX ring 0. */
95105884511SPyun YongHyeon 	error = bus_dma_tag_create(sc->sc_mbuf_dtag, 1, 0,
95205884511SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, MHLEN, 1,
95305884511SPyun YongHyeon 	    MHLEN, 0, NULL, NULL, &sc->sc_rx_mini_tag);
9544d52a575SXin LI 	if (error) {
95505884511SPyun YongHyeon 		device_printf(sc->dev, "could not create mini RX dma tag\n");
956398f1b65SPyun YongHyeon 		return (error);
9574d52a575SXin LI 	}
9584d52a575SXin LI 
95905884511SPyun YongHyeon 	/* Create DMA tag for standard RX mbufs to use RX ring 1. */
96005884511SPyun YongHyeon 	error = bus_dma_tag_create(sc->sc_mbuf_dtag, 1, 0,
96105884511SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1,
96205884511SPyun YongHyeon 	    MCLBYTES, 0, NULL, NULL, &sc->sc_rx_tag);
9634d52a575SXin LI 	if (error) {
96405884511SPyun YongHyeon 		device_printf(sc->dev, "could not create RX dma tag\n");
965398f1b65SPyun YongHyeon 		return (error);
9664d52a575SXin LI 	}
9674d52a575SXin LI 
96805884511SPyun YongHyeon 	/* Create DMA tag for TX mbufs. */
96905884511SPyun YongHyeon 	error = bus_dma_tag_create(sc->sc_mbuf_dtag, 1, 0,
97005884511SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
97105884511SPyun YongHyeon 	    MCLBYTES * ET_NSEG_MAX, ET_NSEG_MAX, MCLBYTES, 0, NULL, NULL,
97205884511SPyun YongHyeon 	    &sc->sc_tx_tag);
97305884511SPyun YongHyeon 	if (error) {
97405884511SPyun YongHyeon 		device_printf(sc->dev, "could not create TX dma tag\n");
97505884511SPyun YongHyeon 		return (error);
97605884511SPyun YongHyeon 	}
97705884511SPyun YongHyeon 
97805884511SPyun YongHyeon 	/* Initialize RX ring 0. */
97905884511SPyun YongHyeon 	rbd = &sc->sc_rx_data[0];
98005884511SPyun YongHyeon 	rbd->rbd_bufsize = ET_RXDMA_CTRL_RING0_128;
98105884511SPyun YongHyeon 	rbd->rbd_newbuf = et_newbuf_hdr;
98205884511SPyun YongHyeon 	rbd->rbd_discard = et_rxbuf_discard;
9834d52a575SXin LI 	rbd->rbd_softc = sc;
98405884511SPyun YongHyeon 	rbd->rbd_ring = &sc->sc_rx_ring[0];
98505884511SPyun YongHyeon 	/* Create DMA maps for mini RX buffers, ring 0. */
98605884511SPyun YongHyeon 	for (i = 0; i < ET_RX_NDESC; i++) {
98705884511SPyun YongHyeon 		error = bus_dmamap_create(sc->sc_rx_mini_tag, 0,
98805884511SPyun YongHyeon 		    &rbd->rbd_buf[i].rb_dmap);
98905884511SPyun YongHyeon 		if (error) {
99005884511SPyun YongHyeon 			device_printf(sc->dev,
99105884511SPyun YongHyeon 			    "could not create DMA map for mini RX mbufs\n");
99205884511SPyun YongHyeon 			return (error);
99305884511SPyun YongHyeon 		}
9944d52a575SXin LI 	}
9954d52a575SXin LI 
99605884511SPyun YongHyeon 	/* Create a spare DMA map for mini RX buffers, ring 0. */
99705884511SPyun YongHyeon 	error = bus_dmamap_create(sc->sc_rx_mini_tag, 0,
99805884511SPyun YongHyeon 	    &sc->sc_rx_mini_sparemap);
99905884511SPyun YongHyeon 	if (error) {
100005884511SPyun YongHyeon 		device_printf(sc->dev,
100105884511SPyun YongHyeon 		    "could not create spare DMA map for mini RX mbuf\n");
100205884511SPyun YongHyeon 		return (error);
100305884511SPyun YongHyeon 	}
100405884511SPyun YongHyeon 
100505884511SPyun YongHyeon 	/* Initialize RX ring 1. */
100605884511SPyun YongHyeon 	rbd = &sc->sc_rx_data[1];
100705884511SPyun YongHyeon 	rbd->rbd_bufsize = ET_RXDMA_CTRL_RING1_2048;
100805884511SPyun YongHyeon 	rbd->rbd_newbuf = et_newbuf_cluster;
100905884511SPyun YongHyeon 	rbd->rbd_discard = et_rxbuf_discard;
101005884511SPyun YongHyeon 	rbd->rbd_softc = sc;
101105884511SPyun YongHyeon 	rbd->rbd_ring = &sc->sc_rx_ring[1];
101205884511SPyun YongHyeon 	/* Create DMA maps for standard RX buffers, ring 1. */
101305884511SPyun YongHyeon 	for (i = 0; i < ET_RX_NDESC; i++) {
101405884511SPyun YongHyeon 		error = bus_dmamap_create(sc->sc_rx_tag, 0,
101505884511SPyun YongHyeon 		    &rbd->rbd_buf[i].rb_dmap);
101605884511SPyun YongHyeon 		if (error) {
101705884511SPyun YongHyeon 			device_printf(sc->dev,
101805884511SPyun YongHyeon 			    "could not create DMA map for mini RX mbufs\n");
101905884511SPyun YongHyeon 			return (error);
102005884511SPyun YongHyeon 		}
102105884511SPyun YongHyeon 	}
102205884511SPyun YongHyeon 
102305884511SPyun YongHyeon 	/* Create a spare DMA map for standard RX buffers, ring 1. */
102405884511SPyun YongHyeon 	error = bus_dmamap_create(sc->sc_rx_tag, 0, &sc->sc_rx_sparemap);
102505884511SPyun YongHyeon 	if (error) {
102605884511SPyun YongHyeon 		device_printf(sc->dev,
102705884511SPyun YongHyeon 		    "could not create spare DMA map for RX mbuf\n");
102805884511SPyun YongHyeon 		return (error);
102905884511SPyun YongHyeon 	}
103005884511SPyun YongHyeon 
103105884511SPyun YongHyeon 	/* Create DMA maps for TX buffers. */
103205884511SPyun YongHyeon 	tbd = &sc->sc_tx_data;
103305884511SPyun YongHyeon 	for (i = 0; i < ET_TX_NDESC; i++) {
103405884511SPyun YongHyeon 		error = bus_dmamap_create(sc->sc_tx_tag, 0,
10354d52a575SXin LI 		    &tbd->tbd_buf[i].tb_dmap);
10364d52a575SXin LI 		if (error) {
103705884511SPyun YongHyeon 			device_printf(sc->dev,
103805884511SPyun YongHyeon 			    "could not create DMA map for TX mbufs\n");
1039398f1b65SPyun YongHyeon 			return (error);
10404d52a575SXin LI 		}
10414d52a575SXin LI 	}
10424d52a575SXin LI 
1043398f1b65SPyun YongHyeon 	return (0);
10444d52a575SXin LI }
10454d52a575SXin LI 
10464d52a575SXin LI static void
104705884511SPyun YongHyeon et_dma_free(struct et_softc *sc)
10484d52a575SXin LI {
104905884511SPyun YongHyeon 	struct et_txdesc_ring *tx_ring;
105005884511SPyun YongHyeon 	struct et_rxdesc_ring *rx_ring;
105105884511SPyun YongHyeon 	struct et_txstatus_data *txsd;
105205884511SPyun YongHyeon 	struct et_rxstat_ring *rxst_ring;
105305884511SPyun YongHyeon 	struct et_rxstatus_data *rxsd;
105405884511SPyun YongHyeon 	struct et_rxbuf_data *rbd;
105505884511SPyun YongHyeon         struct et_txbuf_data *tbd;
10564d52a575SXin LI 	int i;
10574d52a575SXin LI 
105805884511SPyun YongHyeon 	/* Destroy DMA maps for mini RX buffers, ring 0. */
105905884511SPyun YongHyeon 	rbd = &sc->sc_rx_data[0];
106005884511SPyun YongHyeon 	for (i = 0; i < ET_RX_NDESC; i++) {
106105884511SPyun YongHyeon 		if (rbd->rbd_buf[i].rb_dmap) {
106205884511SPyun YongHyeon 			bus_dmamap_destroy(sc->sc_rx_mini_tag,
106305884511SPyun YongHyeon 			    rbd->rbd_buf[i].rb_dmap);
106405884511SPyun YongHyeon 			rbd->rbd_buf[i].rb_dmap = NULL;
10654d52a575SXin LI 		}
10664d52a575SXin LI 	}
106705884511SPyun YongHyeon 	if (sc->sc_rx_mini_sparemap) {
106805884511SPyun YongHyeon 		bus_dmamap_destroy(sc->sc_rx_mini_tag, sc->sc_rx_mini_sparemap);
106905884511SPyun YongHyeon 		sc->sc_rx_mini_sparemap = NULL;
107005884511SPyun YongHyeon 	}
107105884511SPyun YongHyeon 	if (sc->sc_rx_mini_tag) {
107205884511SPyun YongHyeon 		bus_dma_tag_destroy(sc->sc_rx_mini_tag);
107305884511SPyun YongHyeon 		sc->sc_rx_mini_tag = NULL;
10744d52a575SXin LI 	}
10754d52a575SXin LI 
107605884511SPyun YongHyeon 	/* Destroy DMA maps for standard RX buffers, ring 1. */
107705884511SPyun YongHyeon 	rbd = &sc->sc_rx_data[1];
107805884511SPyun YongHyeon 	for (i = 0; i < ET_RX_NDESC; i++) {
107905884511SPyun YongHyeon 		if (rbd->rbd_buf[i].rb_dmap) {
108005884511SPyun YongHyeon 			bus_dmamap_destroy(sc->sc_rx_tag,
108105884511SPyun YongHyeon 			    rbd->rbd_buf[i].rb_dmap);
108205884511SPyun YongHyeon 			rbd->rbd_buf[i].rb_dmap = NULL;
10834d52a575SXin LI 		}
10844d52a575SXin LI 	}
108505884511SPyun YongHyeon 	if (sc->sc_rx_sparemap) {
108605884511SPyun YongHyeon 		bus_dmamap_destroy(sc->sc_rx_tag, sc->sc_rx_sparemap);
108705884511SPyun YongHyeon 		sc->sc_rx_sparemap = NULL;
108805884511SPyun YongHyeon 	}
108905884511SPyun YongHyeon 	if (sc->sc_rx_tag) {
109005884511SPyun YongHyeon 		bus_dma_tag_destroy(sc->sc_rx_tag);
109105884511SPyun YongHyeon 		sc->sc_rx_tag = NULL;
109205884511SPyun YongHyeon 	}
10934d52a575SXin LI 
109405884511SPyun YongHyeon 	/* Destroy DMA maps for TX buffers. */
109505884511SPyun YongHyeon 	tbd = &sc->sc_tx_data;
109605884511SPyun YongHyeon 	for (i = 0; i < ET_TX_NDESC; i++) {
109705884511SPyun YongHyeon 		if (tbd->tbd_buf[i].tb_dmap) {
109805884511SPyun YongHyeon 			bus_dmamap_destroy(sc->sc_tx_tag,
109905884511SPyun YongHyeon 			    tbd->tbd_buf[i].tb_dmap);
110005884511SPyun YongHyeon 			tbd->tbd_buf[i].tb_dmap = NULL;
110105884511SPyun YongHyeon 		}
110205884511SPyun YongHyeon 	}
110305884511SPyun YongHyeon 	if (sc->sc_tx_tag) {
110405884511SPyun YongHyeon 		bus_dma_tag_destroy(sc->sc_tx_tag);
110505884511SPyun YongHyeon 		sc->sc_tx_tag = NULL;
110605884511SPyun YongHyeon 	}
110705884511SPyun YongHyeon 
110805884511SPyun YongHyeon 	/* Destroy mini RX ring, ring 0. */
110905884511SPyun YongHyeon 	rx_ring = &sc->sc_rx_ring[0];
111005884511SPyun YongHyeon 	et_dma_ring_free(sc, &rx_ring->rr_dtag, (void *)&rx_ring->rr_desc,
1111c34f1a08SJohn Baldwin 	    rx_ring->rr_dmap, &rx_ring->rr_paddr);
111205884511SPyun YongHyeon 	/* Destroy standard RX ring, ring 1. */
111305884511SPyun YongHyeon 	rx_ring = &sc->sc_rx_ring[1];
111405884511SPyun YongHyeon 	et_dma_ring_free(sc, &rx_ring->rr_dtag, (void *)&rx_ring->rr_desc,
1115c34f1a08SJohn Baldwin 	    rx_ring->rr_dmap, &rx_ring->rr_paddr);
111605884511SPyun YongHyeon 	/* Destroy RX stat ring. */
111705884511SPyun YongHyeon 	rxst_ring = &sc->sc_rxstat_ring;
111805884511SPyun YongHyeon 	et_dma_ring_free(sc, &rxst_ring->rsr_dtag, (void *)&rxst_ring->rsr_stat,
1119c34f1a08SJohn Baldwin 	    rxst_ring->rsr_dmap, &rxst_ring->rsr_paddr);
112005884511SPyun YongHyeon 	/* Destroy RX status block. */
112105884511SPyun YongHyeon 	rxsd = &sc->sc_rx_status;
112205884511SPyun YongHyeon 	et_dma_ring_free(sc, &rxst_ring->rsr_dtag, (void *)&rxst_ring->rsr_stat,
1123c34f1a08SJohn Baldwin 	    rxst_ring->rsr_dmap, &rxst_ring->rsr_paddr);
112405884511SPyun YongHyeon 	/* Destroy TX ring. */
112505884511SPyun YongHyeon 	tx_ring = &sc->sc_tx_ring;
112605884511SPyun YongHyeon 	et_dma_ring_free(sc, &tx_ring->tr_dtag, (void *)&tx_ring->tr_desc,
1127c34f1a08SJohn Baldwin 	    tx_ring->tr_dmap, &tx_ring->tr_paddr);
112805884511SPyun YongHyeon 	/* Destroy TX status block. */
112905884511SPyun YongHyeon 	txsd = &sc->sc_tx_status;
113005884511SPyun YongHyeon 	et_dma_ring_free(sc, &txsd->txsd_dtag, (void *)&txsd->txsd_status,
1131c34f1a08SJohn Baldwin 	    txsd->txsd_dmap, &txsd->txsd_paddr);
113205884511SPyun YongHyeon 
113305884511SPyun YongHyeon 	/* Destroy the parent tag. */
113405884511SPyun YongHyeon 	if (sc->sc_dtag) {
113505884511SPyun YongHyeon 		bus_dma_tag_destroy(sc->sc_dtag);
113605884511SPyun YongHyeon 		sc->sc_dtag = NULL;
113705884511SPyun YongHyeon 	}
11384d52a575SXin LI }
11394d52a575SXin LI 
11404d52a575SXin LI static void
11414d52a575SXin LI et_chip_attach(struct et_softc *sc)
11424d52a575SXin LI {
11434d52a575SXin LI 	uint32_t val;
11444d52a575SXin LI 
11454d52a575SXin LI 	/*
11464d52a575SXin LI 	 * Perform minimal initialization
11474d52a575SXin LI 	 */
11484d52a575SXin LI 
11494d52a575SXin LI 	/* Disable loopback */
11504d52a575SXin LI 	CSR_WRITE_4(sc, ET_LOOPBACK, 0);
11514d52a575SXin LI 
11524d52a575SXin LI 	/* Reset MAC */
11534d52a575SXin LI 	CSR_WRITE_4(sc, ET_MAC_CFG1,
11544d52a575SXin LI 		    ET_MAC_CFG1_RST_TXFUNC | ET_MAC_CFG1_RST_RXFUNC |
11554d52a575SXin LI 		    ET_MAC_CFG1_RST_TXMC | ET_MAC_CFG1_RST_RXMC |
11564d52a575SXin LI 		    ET_MAC_CFG1_SIM_RST | ET_MAC_CFG1_SOFT_RST);
11574d52a575SXin LI 
11584d52a575SXin LI 	/*
11594d52a575SXin LI 	 * Setup half duplex mode
11604d52a575SXin LI 	 */
116123263665SPyun YongHyeon 	val = (10 << ET_MAC_HDX_ALT_BEB_TRUNC_SHIFT) |
116223263665SPyun YongHyeon 	    (15 << ET_MAC_HDX_REXMIT_MAX_SHIFT) |
116323263665SPyun YongHyeon 	    (55 << ET_MAC_HDX_COLLWIN_SHIFT) |
11644d52a575SXin LI 	    ET_MAC_HDX_EXC_DEFER;
11654d52a575SXin LI 	CSR_WRITE_4(sc, ET_MAC_HDX, val);
11664d52a575SXin LI 
11674d52a575SXin LI 	/* Clear MAC control */
11684d52a575SXin LI 	CSR_WRITE_4(sc, ET_MAC_CTRL, 0);
11694d52a575SXin LI 
11704d52a575SXin LI 	/* Reset MII */
11714d52a575SXin LI 	CSR_WRITE_4(sc, ET_MII_CFG, ET_MII_CFG_CLKRST);
11724d52a575SXin LI 
11734d52a575SXin LI 	/* Bring MAC out of reset state */
11744d52a575SXin LI 	CSR_WRITE_4(sc, ET_MAC_CFG1, 0);
11754d52a575SXin LI 
11764d52a575SXin LI 	/* Enable memory controllers */
11774d52a575SXin LI 	CSR_WRITE_4(sc, ET_MMC_CTRL, ET_MMC_CTRL_ENABLE);
11784d52a575SXin LI }
11794d52a575SXin LI 
11804d52a575SXin LI static void
11814d52a575SXin LI et_intr(void *xsc)
11824d52a575SXin LI {
11830b699044SPyun YongHyeon 	struct et_softc *sc;
11844d52a575SXin LI 	struct ifnet *ifp;
1185fa1483ddSPyun YongHyeon 	uint32_t status;
11864d52a575SXin LI 
11870b699044SPyun YongHyeon 	sc = xsc;
11884d52a575SXin LI 	ET_LOCK(sc);
11894d52a575SXin LI 	ifp = sc->ifp;
1190fa1483ddSPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1191fa1483ddSPyun YongHyeon 		goto done;
1192fa1483ddSPyun YongHyeon 
1193fa1483ddSPyun YongHyeon 	status = CSR_READ_4(sc, ET_INTR_STATUS);
1194fa1483ddSPyun YongHyeon 	if ((status & ET_INTRS) == 0)
1195fa1483ddSPyun YongHyeon 		goto done;
11964d52a575SXin LI 
11976537ffa6SPyun YongHyeon 	/* Disable further interrupts. */
11986537ffa6SPyun YongHyeon 	CSR_WRITE_4(sc, ET_INTR_MASK, 0xffffffff);
11994d52a575SXin LI 
1200fa1483ddSPyun YongHyeon 	if (status & (ET_INTR_RXDMA_ERROR | ET_INTR_TXDMA_ERROR)) {
1201fa1483ddSPyun YongHyeon 		device_printf(sc->dev, "DMA error(0x%08x) -- resetting\n",
1202fa1483ddSPyun YongHyeon 		    status);
1203fa1483ddSPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1204fa1483ddSPyun YongHyeon 		et_init_locked(sc);
1205fa1483ddSPyun YongHyeon 		ET_UNLOCK(sc);
1206fa1483ddSPyun YongHyeon 		return;
1207fa1483ddSPyun YongHyeon 	}
1208fa1483ddSPyun YongHyeon 	if (status & ET_INTR_RXDMA)
12094d52a575SXin LI 		et_rxeof(sc);
1210fa1483ddSPyun YongHyeon 	if (status & (ET_INTR_TXDMA | ET_INTR_TIMER))
12114d52a575SXin LI 		et_txeof(sc);
1212fa1483ddSPyun YongHyeon 	if (status & ET_INTR_TIMER)
12134d52a575SXin LI 		CSR_WRITE_4(sc, ET_TIMER, sc->sc_timer);
1214244fd28bSPyun YongHyeon 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
12156537ffa6SPyun YongHyeon 		CSR_WRITE_4(sc, ET_INTR_MASK, ~ET_INTRS);
1216244fd28bSPyun YongHyeon 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1217244fd28bSPyun YongHyeon 			et_start_locked(ifp);
1218244fd28bSPyun YongHyeon 	}
1219fa1483ddSPyun YongHyeon done:
12204d52a575SXin LI 	ET_UNLOCK(sc);
12214d52a575SXin LI }
12224d52a575SXin LI 
12234d52a575SXin LI static void
12244d52a575SXin LI et_init_locked(struct et_softc *sc)
12254d52a575SXin LI {
122605884511SPyun YongHyeon 	struct ifnet *ifp;
122705884511SPyun YongHyeon 	int error;
12284d52a575SXin LI 
12294d52a575SXin LI 	ET_LOCK_ASSERT(sc);
12304d52a575SXin LI 
123105884511SPyun YongHyeon 	ifp = sc->ifp;
12324d52a575SXin LI 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
12334d52a575SXin LI 		return;
12344d52a575SXin LI 
12354d52a575SXin LI 	et_stop(sc);
12361f009e2fSPyun YongHyeon 	et_reset(sc);
12374d52a575SXin LI 
123805884511SPyun YongHyeon 	et_init_tx_ring(sc);
12394d52a575SXin LI 	error = et_init_rx_ring(sc);
12404d52a575SXin LI 	if (error)
124105884511SPyun YongHyeon 		return;
12424d52a575SXin LI 
12434d52a575SXin LI 	error = et_chip_init(sc);
12444d52a575SXin LI 	if (error)
12451f009e2fSPyun YongHyeon 		goto fail;
12464d52a575SXin LI 
12471f009e2fSPyun YongHyeon 	/*
12481f009e2fSPyun YongHyeon 	 * Start TX/RX DMA engine
12491f009e2fSPyun YongHyeon 	 */
12501f009e2fSPyun YongHyeon 	error = et_start_rxdma(sc);
12514d52a575SXin LI 	if (error)
12521f009e2fSPyun YongHyeon 		return;
12531f009e2fSPyun YongHyeon 
12541f009e2fSPyun YongHyeon 	error = et_start_txdma(sc);
12551f009e2fSPyun YongHyeon 	if (error)
12561f009e2fSPyun YongHyeon 		return;
12574d52a575SXin LI 
12586537ffa6SPyun YongHyeon 	/* Enable interrupts. */
12596537ffa6SPyun YongHyeon 	CSR_WRITE_4(sc, ET_INTR_MASK, ~ET_INTRS);
12604d52a575SXin LI 
12614d52a575SXin LI 	CSR_WRITE_4(sc, ET_TIMER, sc->sc_timer);
12624d52a575SXin LI 
12634d52a575SXin LI 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
12644d52a575SXin LI 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
12651f009e2fSPyun YongHyeon 
12661f009e2fSPyun YongHyeon 	sc->sc_flags &= ~ET_FLAG_LINK;
12671f009e2fSPyun YongHyeon 	et_ifmedia_upd_locked(ifp);
12681f009e2fSPyun YongHyeon 
12691f009e2fSPyun YongHyeon 	callout_reset(&sc->sc_tick, hz, et_tick, sc);
12701f009e2fSPyun YongHyeon 
12711f009e2fSPyun YongHyeon fail:
12724d52a575SXin LI 	if (error)
12734d52a575SXin LI 		et_stop(sc);
12744d52a575SXin LI }
12754d52a575SXin LI 
12764d52a575SXin LI static void
12774d52a575SXin LI et_init(void *xsc)
12784d52a575SXin LI {
12794d52a575SXin LI 	struct et_softc *sc = xsc;
12804d52a575SXin LI 
12814d52a575SXin LI 	ET_LOCK(sc);
12824d52a575SXin LI 	et_init_locked(sc);
12834d52a575SXin LI 	ET_UNLOCK(sc);
12844d52a575SXin LI }
12854d52a575SXin LI 
12864d52a575SXin LI static int
12874d52a575SXin LI et_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
12884d52a575SXin LI {
12890b699044SPyun YongHyeon 	struct et_softc *sc;
12900b699044SPyun YongHyeon 	struct mii_data *mii;
12910b699044SPyun YongHyeon 	struct ifreq *ifr;
12920b699044SPyun YongHyeon 	int error, mask, max_framelen;
12930b699044SPyun YongHyeon 
12940b699044SPyun YongHyeon 	sc = ifp->if_softc;
12950b699044SPyun YongHyeon 	ifr = (struct ifreq *)data;
12960b699044SPyun YongHyeon 	error = 0;
12974d52a575SXin LI 
12984d52a575SXin LI /* XXX LOCKSUSED */
12994d52a575SXin LI 	switch (cmd) {
13004d52a575SXin LI 	case SIOCSIFFLAGS:
13014d52a575SXin LI 		ET_LOCK(sc);
13024d52a575SXin LI 		if (ifp->if_flags & IFF_UP) {
13034d52a575SXin LI 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
13044d52a575SXin LI 				if ((ifp->if_flags ^ sc->sc_if_flags) &
13054d52a575SXin LI 				(IFF_ALLMULTI | IFF_PROMISC | IFF_BROADCAST))
13064d52a575SXin LI 					et_setmulti(sc);
13074d52a575SXin LI 			} else {
13084d52a575SXin LI 				et_init_locked(sc);
13094d52a575SXin LI 			}
13104d52a575SXin LI 		} else {
13114d52a575SXin LI 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
13124d52a575SXin LI 				et_stop(sc);
13134d52a575SXin LI 		}
13144d52a575SXin LI 		sc->sc_if_flags = ifp->if_flags;
13154d52a575SXin LI 		ET_UNLOCK(sc);
13164d52a575SXin LI 		break;
13174d52a575SXin LI 
13184d52a575SXin LI 	case SIOCSIFMEDIA:
13194d52a575SXin LI 	case SIOCGIFMEDIA:
13200b699044SPyun YongHyeon 		mii = device_get_softc(sc->sc_miibus);
13214d52a575SXin LI 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
13224d52a575SXin LI 		break;
13234d52a575SXin LI 
13244d52a575SXin LI 	case SIOCADDMULTI:
13254d52a575SXin LI 	case SIOCDELMULTI:
13264d52a575SXin LI 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
13274d52a575SXin LI 			ET_LOCK(sc);
13284d52a575SXin LI 			et_setmulti(sc);
13294d52a575SXin LI 			ET_UNLOCK(sc);
13304d52a575SXin LI 		}
13314d52a575SXin LI 		break;
13324d52a575SXin LI 
13334d52a575SXin LI 	case SIOCSIFMTU:
13348e5ad990SPyun YongHyeon 		ET_LOCK(sc);
13354d52a575SXin LI #if 0
13364d52a575SXin LI 		if (sc->sc_flags & ET_FLAG_JUMBO)
13374d52a575SXin LI 			max_framelen = ET_JUMBO_FRAMELEN;
13384d52a575SXin LI 		else
13394d52a575SXin LI #endif
13404d52a575SXin LI 			max_framelen = MCLBYTES - 1;
13414d52a575SXin LI 
13424d52a575SXin LI 		if (ET_FRAMELEN(ifr->ifr_mtu) > max_framelen) {
13434d52a575SXin LI 			error = EOPNOTSUPP;
13448e5ad990SPyun YongHyeon 			ET_UNLOCK(sc);
13454d52a575SXin LI 			break;
13464d52a575SXin LI 		}
13474d52a575SXin LI 
13484d52a575SXin LI 		if (ifp->if_mtu != ifr->ifr_mtu) {
13494d52a575SXin LI 			ifp->if_mtu = ifr->ifr_mtu;
13508e5ad990SPyun YongHyeon 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
13514d52a575SXin LI 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
13528e5ad990SPyun YongHyeon 				et_init_locked(sc);
13534d52a575SXin LI 			}
13548e5ad990SPyun YongHyeon 		}
13558e5ad990SPyun YongHyeon 		ET_UNLOCK(sc);
13564d52a575SXin LI 		break;
13574d52a575SXin LI 
13589955274cSPyun YongHyeon 	case SIOCSIFCAP:
13599955274cSPyun YongHyeon 		ET_LOCK(sc);
13609955274cSPyun YongHyeon 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
13619955274cSPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
13629955274cSPyun YongHyeon 		    (IFCAP_TXCSUM & ifp->if_capabilities) != 0) {
13639955274cSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TXCSUM;
13649955274cSPyun YongHyeon 			if ((IFCAP_TXCSUM & ifp->if_capenable) != 0)
13659955274cSPyun YongHyeon 				ifp->if_hwassist |= ET_CSUM_FEATURES;
13669955274cSPyun YongHyeon 			else
13679955274cSPyun YongHyeon 				ifp->if_hwassist &= ~ET_CSUM_FEATURES;
13689955274cSPyun YongHyeon 		}
13699955274cSPyun YongHyeon 		ET_UNLOCK(sc);
13709955274cSPyun YongHyeon 		break;
13719955274cSPyun YongHyeon 
13724d52a575SXin LI 	default:
13734d52a575SXin LI 		error = ether_ioctl(ifp, cmd, data);
13744d52a575SXin LI 		break;
13754d52a575SXin LI 	}
1376398f1b65SPyun YongHyeon 	return (error);
13774d52a575SXin LI }
13784d52a575SXin LI 
13794d52a575SXin LI static void
13804d52a575SXin LI et_start_locked(struct ifnet *ifp)
13814d52a575SXin LI {
1382c8b727ceSPyun YongHyeon 	struct et_softc *sc;
1383c8b727ceSPyun YongHyeon 	struct mbuf *m_head = NULL;
1384244fd28bSPyun YongHyeon 	struct et_txdesc_ring *tx_ring;
13854d52a575SXin LI 	struct et_txbuf_data *tbd;
1386244fd28bSPyun YongHyeon 	uint32_t tx_ready_pos;
1387c8b727ceSPyun YongHyeon 	int enq;
13884d52a575SXin LI 
1389c8b727ceSPyun YongHyeon 	sc = ifp->if_softc;
13904d52a575SXin LI 	ET_LOCK_ASSERT(sc);
13914d52a575SXin LI 
13921f009e2fSPyun YongHyeon 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
13931f009e2fSPyun YongHyeon 	    IFF_DRV_RUNNING ||
13941f009e2fSPyun YongHyeon 	    (sc->sc_flags & (ET_FLAG_LINK | ET_FLAG_TXRX_ENABLED)) !=
13951f009e2fSPyun YongHyeon 	    (ET_FLAG_LINK | ET_FLAG_TXRX_ENABLED))
13964d52a575SXin LI 		return;
13974d52a575SXin LI 
1398244fd28bSPyun YongHyeon 	/*
1399244fd28bSPyun YongHyeon 	 * Driver does not request TX completion interrupt for every
1400244fd28bSPyun YongHyeon 	 * queued frames to prevent generating excessive interrupts.
1401244fd28bSPyun YongHyeon 	 * This means driver may wait for TX completion interrupt even
1402453130d9SPedro F. Giffuni 	 * though some frames were successfully transmitted.  Reclaiming
1403244fd28bSPyun YongHyeon 	 * transmitted frames will ensure driver see all available
1404244fd28bSPyun YongHyeon 	 * descriptors.
1405244fd28bSPyun YongHyeon 	 */
1406c8b727ceSPyun YongHyeon 	tbd = &sc->sc_tx_data;
1407244fd28bSPyun YongHyeon 	if (tbd->tbd_used > (ET_TX_NDESC * 2) / 3)
1408244fd28bSPyun YongHyeon 		et_txeof(sc);
1409244fd28bSPyun YongHyeon 
1410c8b727ceSPyun YongHyeon 	for (enq = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd); ) {
1411c8b727ceSPyun YongHyeon 		if (tbd->tbd_used + ET_NSEG_SPARE >= ET_TX_NDESC) {
14124d52a575SXin LI 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
14134d52a575SXin LI 			break;
14144d52a575SXin LI 		}
14154d52a575SXin LI 
1416c8b727ceSPyun YongHyeon 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
1417c8b727ceSPyun YongHyeon 		if (m_head == NULL)
14184d52a575SXin LI 			break;
14194d52a575SXin LI 
1420c8b727ceSPyun YongHyeon 		if (et_encap(sc, &m_head)) {
1421c8b727ceSPyun YongHyeon 			if (m_head == NULL) {
1422c13dc687SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1423c8b727ceSPyun YongHyeon 				break;
1424c8b727ceSPyun YongHyeon 			}
1425c8b727ceSPyun YongHyeon 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
1426c8b727ceSPyun YongHyeon 			if (tbd->tbd_used > 0)
14274d52a575SXin LI 				ifp->if_drv_flags |= IFF_DRV_OACTIVE;
14284d52a575SXin LI 			break;
14294d52a575SXin LI 		}
1430c8b727ceSPyun YongHyeon 		enq++;
1431c8b727ceSPyun YongHyeon 		ETHER_BPF_MTAP(ifp, m_head);
14324d52a575SXin LI 	}
14334d52a575SXin LI 
1434244fd28bSPyun YongHyeon 	if (enq > 0) {
1435244fd28bSPyun YongHyeon 		tx_ring = &sc->sc_tx_ring;
1436244fd28bSPyun YongHyeon 		bus_dmamap_sync(tx_ring->tr_dtag, tx_ring->tr_dmap,
1437244fd28bSPyun YongHyeon 		    BUS_DMASYNC_PREWRITE);
1438244fd28bSPyun YongHyeon 		tx_ready_pos = tx_ring->tr_ready_index &
1439244fd28bSPyun YongHyeon 		    ET_TX_READY_POS_INDEX_MASK;
1440244fd28bSPyun YongHyeon 		if (tx_ring->tr_ready_wrap)
1441244fd28bSPyun YongHyeon 			tx_ready_pos |= ET_TX_READY_POS_WRAP;
1442244fd28bSPyun YongHyeon 		CSR_WRITE_4(sc, ET_TX_READY_POS, tx_ready_pos);
14434d52a575SXin LI 		sc->watchdog_timer = 5;
14444d52a575SXin LI 	}
1445244fd28bSPyun YongHyeon }
14464d52a575SXin LI 
14474d52a575SXin LI static void
14484d52a575SXin LI et_start(struct ifnet *ifp)
14494d52a575SXin LI {
14500b699044SPyun YongHyeon 	struct et_softc *sc;
14514d52a575SXin LI 
14520b699044SPyun YongHyeon 	sc = ifp->if_softc;
14534d52a575SXin LI 	ET_LOCK(sc);
14544d52a575SXin LI 	et_start_locked(ifp);
14554d52a575SXin LI 	ET_UNLOCK(sc);
14564d52a575SXin LI }
14574d52a575SXin LI 
145805884511SPyun YongHyeon static int
14594d52a575SXin LI et_watchdog(struct et_softc *sc)
14604d52a575SXin LI {
146105884511SPyun YongHyeon 	uint32_t status;
146205884511SPyun YongHyeon 
14634d52a575SXin LI 	ET_LOCK_ASSERT(sc);
14644d52a575SXin LI 
14654d52a575SXin LI 	if (sc->watchdog_timer == 0 || --sc->watchdog_timer)
146605884511SPyun YongHyeon 		return (0);
14674d52a575SXin LI 
146805884511SPyun YongHyeon 	bus_dmamap_sync(sc->sc_tx_status.txsd_dtag, sc->sc_tx_status.txsd_dmap,
146905884511SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD);
147005884511SPyun YongHyeon 	status = le32toh(*(sc->sc_tx_status.txsd_status));
147105884511SPyun YongHyeon 	if_printf(sc->ifp, "watchdog timed out (0x%08x) -- resetting\n",
147205884511SPyun YongHyeon 	    status);
14734d52a575SXin LI 
1474c13dc687SGleb Smirnoff 	if_inc_counter(sc->ifp, IFCOUNTER_OERRORS, 1);
1475744ec7f2SPyun YongHyeon 	sc->ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
14764d52a575SXin LI 	et_init_locked(sc);
147705884511SPyun YongHyeon 	return (EJUSTRETURN);
14784d52a575SXin LI }
14794d52a575SXin LI 
14804d52a575SXin LI static int
14814d52a575SXin LI et_stop_rxdma(struct et_softc *sc)
14824d52a575SXin LI {
14830b699044SPyun YongHyeon 
14844d52a575SXin LI 	CSR_WRITE_4(sc, ET_RXDMA_CTRL,
14854d52a575SXin LI 		    ET_RXDMA_CTRL_HALT | ET_RXDMA_CTRL_RING1_ENABLE);
14864d52a575SXin LI 
14874d52a575SXin LI 	DELAY(5);
14884d52a575SXin LI 	if ((CSR_READ_4(sc, ET_RXDMA_CTRL) & ET_RXDMA_CTRL_HALTED) == 0) {
14894d52a575SXin LI 		if_printf(sc->ifp, "can't stop RX DMA engine\n");
1490398f1b65SPyun YongHyeon 		return (ETIMEDOUT);
14914d52a575SXin LI 	}
1492398f1b65SPyun YongHyeon 	return (0);
14934d52a575SXin LI }
14944d52a575SXin LI 
14954d52a575SXin LI static int
14964d52a575SXin LI et_stop_txdma(struct et_softc *sc)
14974d52a575SXin LI {
14980b699044SPyun YongHyeon 
14994d52a575SXin LI 	CSR_WRITE_4(sc, ET_TXDMA_CTRL,
15004d52a575SXin LI 		    ET_TXDMA_CTRL_HALT | ET_TXDMA_CTRL_SINGLE_EPKT);
1501398f1b65SPyun YongHyeon 	return (0);
15024d52a575SXin LI }
15034d52a575SXin LI 
15044d52a575SXin LI static void
15054d52a575SXin LI et_free_tx_ring(struct et_softc *sc)
15064d52a575SXin LI {
150705884511SPyun YongHyeon 	struct et_txdesc_ring *tx_ring;
150805884511SPyun YongHyeon 	struct et_txbuf_data *tbd;
150905884511SPyun YongHyeon 	struct et_txbuf *tb;
15104d52a575SXin LI 	int i;
15114d52a575SXin LI 
151205884511SPyun YongHyeon 	tbd = &sc->sc_tx_data;
151305884511SPyun YongHyeon 	tx_ring = &sc->sc_tx_ring;
15144d52a575SXin LI 	for (i = 0; i < ET_TX_NDESC; ++i) {
151505884511SPyun YongHyeon 		tb = &tbd->tbd_buf[i];
15164d52a575SXin LI 		if (tb->tb_mbuf != NULL) {
151705884511SPyun YongHyeon 			bus_dmamap_sync(sc->sc_tx_tag, tb->tb_dmap,
151805884511SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
15194d52a575SXin LI 			bus_dmamap_unload(sc->sc_mbuf_dtag, tb->tb_dmap);
15204d52a575SXin LI 			m_freem(tb->tb_mbuf);
15214d52a575SXin LI 			tb->tb_mbuf = NULL;
15224d52a575SXin LI 		}
15234d52a575SXin LI 	}
15244d52a575SXin LI }
15254d52a575SXin LI 
15264d52a575SXin LI static void
15274d52a575SXin LI et_free_rx_ring(struct et_softc *sc)
15284d52a575SXin LI {
152905884511SPyun YongHyeon 	struct et_rxbuf_data *rbd;
153005884511SPyun YongHyeon 	struct et_rxdesc_ring *rx_ring;
153105884511SPyun YongHyeon 	struct et_rxbuf *rb;
15324d52a575SXin LI 	int i;
15334d52a575SXin LI 
153405884511SPyun YongHyeon 	/* Ring 0 */
153505884511SPyun YongHyeon 	rx_ring = &sc->sc_rx_ring[0];
153605884511SPyun YongHyeon 	rbd = &sc->sc_rx_data[0];
15374d52a575SXin LI 	for (i = 0; i < ET_RX_NDESC; ++i) {
153805884511SPyun YongHyeon 		rb = &rbd->rbd_buf[i];
15394d52a575SXin LI 		if (rb->rb_mbuf != NULL) {
154005884511SPyun YongHyeon 			bus_dmamap_sync(sc->sc_rx_mini_tag, rx_ring->rr_dmap,
154105884511SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
154205884511SPyun YongHyeon 			bus_dmamap_unload(sc->sc_rx_mini_tag, rb->rb_dmap);
15434d52a575SXin LI 			m_freem(rb->rb_mbuf);
15444d52a575SXin LI 			rb->rb_mbuf = NULL;
15454d52a575SXin LI 		}
15464d52a575SXin LI 	}
15474d52a575SXin LI 
154805884511SPyun YongHyeon 	/* Ring 1 */
154905884511SPyun YongHyeon 	rx_ring = &sc->sc_rx_ring[1];
155005884511SPyun YongHyeon 	rbd = &sc->sc_rx_data[1];
155105884511SPyun YongHyeon 	for (i = 0; i < ET_RX_NDESC; ++i) {
155205884511SPyun YongHyeon 		rb = &rbd->rbd_buf[i];
155305884511SPyun YongHyeon 		if (rb->rb_mbuf != NULL) {
155405884511SPyun YongHyeon 			bus_dmamap_sync(sc->sc_rx_tag, rx_ring->rr_dmap,
155505884511SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
155605884511SPyun YongHyeon 			bus_dmamap_unload(sc->sc_rx_tag, rb->rb_dmap);
155705884511SPyun YongHyeon 			m_freem(rb->rb_mbuf);
155805884511SPyun YongHyeon 			rb->rb_mbuf = NULL;
155905884511SPyun YongHyeon 		}
15604d52a575SXin LI 	}
15614d52a575SXin LI }
15624d52a575SXin LI 
156336581f82SGleb Smirnoff static u_int
156436581f82SGleb Smirnoff et_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
15654d52a575SXin LI {
156636581f82SGleb Smirnoff 	uint32_t h, *hp, *hash = arg;
15674d52a575SXin LI 
156836581f82SGleb Smirnoff 	h = ether_crc32_be(LLADDR(sdl), ETHER_ADDR_LEN);
15694d52a575SXin LI 	h = (h & 0x3f800000) >> 23;
15704d52a575SXin LI 
15714d52a575SXin LI 	hp = &hash[0];
15724d52a575SXin LI 	if (h >= 32 && h < 64) {
15734d52a575SXin LI 		h -= 32;
15744d52a575SXin LI 		hp = &hash[1];
15754d52a575SXin LI 	} else if (h >= 64 && h < 96) {
15764d52a575SXin LI 		h -= 64;
15774d52a575SXin LI 		hp = &hash[2];
15784d52a575SXin LI 	} else if (h >= 96) {
15794d52a575SXin LI 		h -= 96;
15804d52a575SXin LI 		hp = &hash[3];
15814d52a575SXin LI 	}
15824d52a575SXin LI 	*hp |= (1 << h);
15834d52a575SXin LI 
158436581f82SGleb Smirnoff 	return (1);
15854d52a575SXin LI }
158636581f82SGleb Smirnoff 
158736581f82SGleb Smirnoff static void
158836581f82SGleb Smirnoff et_setmulti(struct et_softc *sc)
158936581f82SGleb Smirnoff {
159036581f82SGleb Smirnoff 	struct ifnet *ifp;
159136581f82SGleb Smirnoff 	uint32_t hash[4] = { 0, 0, 0, 0 };
159236581f82SGleb Smirnoff 	uint32_t rxmac_ctrl, pktfilt;
159336581f82SGleb Smirnoff 	int i, count;
159436581f82SGleb Smirnoff 
159536581f82SGleb Smirnoff 	ET_LOCK_ASSERT(sc);
159636581f82SGleb Smirnoff 	ifp = sc->ifp;
159736581f82SGleb Smirnoff 
159836581f82SGleb Smirnoff 	pktfilt = CSR_READ_4(sc, ET_PKTFILT);
159936581f82SGleb Smirnoff 	rxmac_ctrl = CSR_READ_4(sc, ET_RXMAC_CTRL);
160036581f82SGleb Smirnoff 
160136581f82SGleb Smirnoff 	pktfilt &= ~(ET_PKTFILT_BCAST | ET_PKTFILT_MCAST | ET_PKTFILT_UCAST);
160236581f82SGleb Smirnoff 	if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI)) {
160336581f82SGleb Smirnoff 		rxmac_ctrl |= ET_RXMAC_CTRL_NO_PKTFILT;
160436581f82SGleb Smirnoff 		goto back;
160536581f82SGleb Smirnoff 	}
160636581f82SGleb Smirnoff 
160736581f82SGleb Smirnoff 	count = if_foreach_llmaddr(ifp, et_hash_maddr, &hash);
16084d52a575SXin LI 
16094d52a575SXin LI 	for (i = 0; i < 4; ++i)
16104d52a575SXin LI 		CSR_WRITE_4(sc, ET_MULTI_HASH + (i * 4), hash[i]);
16114d52a575SXin LI 
16124d52a575SXin LI 	if (count > 0)
16134d52a575SXin LI 		pktfilt |= ET_PKTFILT_MCAST;
16144d52a575SXin LI 	rxmac_ctrl &= ~ET_RXMAC_CTRL_NO_PKTFILT;
16154d52a575SXin LI back:
16164d52a575SXin LI 	CSR_WRITE_4(sc, ET_PKTFILT, pktfilt);
16174d52a575SXin LI 	CSR_WRITE_4(sc, ET_RXMAC_CTRL, rxmac_ctrl);
16184d52a575SXin LI }
16194d52a575SXin LI 
16204d52a575SXin LI static int
16214d52a575SXin LI et_chip_init(struct et_softc *sc)
16224d52a575SXin LI {
16230b699044SPyun YongHyeon 	struct ifnet *ifp;
16244d52a575SXin LI 	uint32_t rxq_end;
16254d52a575SXin LI 	int error, frame_len, rxmem_size;
16264d52a575SXin LI 
16270b699044SPyun YongHyeon 	ifp = sc->ifp;
16284d52a575SXin LI 	/*
16294d52a575SXin LI 	 * Split 16Kbytes internal memory between TX and RX
16304d52a575SXin LI 	 * according to frame length.
16314d52a575SXin LI 	 */
16324d52a575SXin LI 	frame_len = ET_FRAMELEN(ifp->if_mtu);
16334d52a575SXin LI 	if (frame_len < 2048) {
16344d52a575SXin LI 		rxmem_size = ET_MEM_RXSIZE_DEFAULT;
16354d52a575SXin LI 	} else if (frame_len <= ET_RXMAC_CUT_THRU_FRMLEN) {
16364d52a575SXin LI 		rxmem_size = ET_MEM_SIZE / 2;
16374d52a575SXin LI 	} else {
16384d52a575SXin LI 		rxmem_size = ET_MEM_SIZE -
16394d52a575SXin LI 		roundup(frame_len + ET_MEM_TXSIZE_EX, ET_MEM_UNIT);
16404d52a575SXin LI 	}
16414d52a575SXin LI 	rxq_end = ET_QUEUE_ADDR(rxmem_size);
16424d52a575SXin LI 
16434d52a575SXin LI 	CSR_WRITE_4(sc, ET_RXQUEUE_START, ET_QUEUE_ADDR_START);
16444d52a575SXin LI 	CSR_WRITE_4(sc, ET_RXQUEUE_END, rxq_end);
16454d52a575SXin LI 	CSR_WRITE_4(sc, ET_TXQUEUE_START, rxq_end + 1);
16464d52a575SXin LI 	CSR_WRITE_4(sc, ET_TXQUEUE_END, ET_QUEUE_ADDR_END);
16474d52a575SXin LI 
16484d52a575SXin LI 	/* No loopback */
16494d52a575SXin LI 	CSR_WRITE_4(sc, ET_LOOPBACK, 0);
16504d52a575SXin LI 
16514d52a575SXin LI 	/* Clear MSI configure */
1652cc3c3b4eSPyun YongHyeon 	if ((sc->sc_flags & ET_FLAG_MSI) == 0)
16534d52a575SXin LI 		CSR_WRITE_4(sc, ET_MSI_CFG, 0);
16544d52a575SXin LI 
16554d52a575SXin LI 	/* Disable timer */
16564d52a575SXin LI 	CSR_WRITE_4(sc, ET_TIMER, 0);
16574d52a575SXin LI 
16584d52a575SXin LI 	/* Initialize MAC */
16594d52a575SXin LI 	et_init_mac(sc);
16604d52a575SXin LI 
16614d52a575SXin LI 	/* Enable memory controllers */
16624d52a575SXin LI 	CSR_WRITE_4(sc, ET_MMC_CTRL, ET_MMC_CTRL_ENABLE);
16634d52a575SXin LI 
16644d52a575SXin LI 	/* Initialize RX MAC */
16654d52a575SXin LI 	et_init_rxmac(sc);
16664d52a575SXin LI 
16674d52a575SXin LI 	/* Initialize TX MAC */
16684d52a575SXin LI 	et_init_txmac(sc);
16694d52a575SXin LI 
16704d52a575SXin LI 	/* Initialize RX DMA engine */
16714d52a575SXin LI 	error = et_init_rxdma(sc);
16724d52a575SXin LI 	if (error)
1673398f1b65SPyun YongHyeon 		return (error);
16744d52a575SXin LI 
16754d52a575SXin LI 	/* Initialize TX DMA engine */
16764d52a575SXin LI 	error = et_init_txdma(sc);
16774d52a575SXin LI 	if (error)
1678398f1b65SPyun YongHyeon 		return (error);
16794d52a575SXin LI 
1680398f1b65SPyun YongHyeon 	return (0);
16814d52a575SXin LI }
16824d52a575SXin LI 
168305884511SPyun YongHyeon static void
16844d52a575SXin LI et_init_tx_ring(struct et_softc *sc)
16854d52a575SXin LI {
168605884511SPyun YongHyeon 	struct et_txdesc_ring *tx_ring;
168705884511SPyun YongHyeon 	struct et_txbuf_data *tbd;
168805884511SPyun YongHyeon 	struct et_txstatus_data *txsd;
16894d52a575SXin LI 
169005884511SPyun YongHyeon 	tx_ring = &sc->sc_tx_ring;
16914d52a575SXin LI 	bzero(tx_ring->tr_desc, ET_TX_RING_SIZE);
16924d52a575SXin LI 	bus_dmamap_sync(tx_ring->tr_dtag, tx_ring->tr_dmap,
16934d52a575SXin LI 	    BUS_DMASYNC_PREWRITE);
16944d52a575SXin LI 
169505884511SPyun YongHyeon 	tbd = &sc->sc_tx_data;
16964d52a575SXin LI 	tbd->tbd_start_index = 0;
16974d52a575SXin LI 	tbd->tbd_start_wrap = 0;
16984d52a575SXin LI 	tbd->tbd_used = 0;
16994d52a575SXin LI 
170005884511SPyun YongHyeon 	txsd = &sc->sc_tx_status;
17014d52a575SXin LI 	bzero(txsd->txsd_status, sizeof(uint32_t));
17024d52a575SXin LI 	bus_dmamap_sync(txsd->txsd_dtag, txsd->txsd_dmap,
170305884511SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
17044d52a575SXin LI }
17054d52a575SXin LI 
17064d52a575SXin LI static int
17074d52a575SXin LI et_init_rx_ring(struct et_softc *sc)
17084d52a575SXin LI {
170905884511SPyun YongHyeon 	struct et_rxstatus_data *rxsd;
171005884511SPyun YongHyeon 	struct et_rxstat_ring *rxst_ring;
171105884511SPyun YongHyeon 	struct et_rxbuf_data *rbd;
171205884511SPyun YongHyeon 	int i, error, n;
17134d52a575SXin LI 
17144d52a575SXin LI 	for (n = 0; n < ET_RX_NRING; ++n) {
171505884511SPyun YongHyeon 		rbd = &sc->sc_rx_data[n];
17164d52a575SXin LI 		for (i = 0; i < ET_RX_NDESC; ++i) {
171705884511SPyun YongHyeon 			error = rbd->rbd_newbuf(rbd, i);
17184d52a575SXin LI 			if (error) {
17194d52a575SXin LI 				if_printf(sc->ifp, "%d ring %d buf, "
17204d52a575SXin LI 					  "newbuf failed: %d\n", n, i, error);
1721398f1b65SPyun YongHyeon 				return (error);
17224d52a575SXin LI 			}
17234d52a575SXin LI 		}
17244d52a575SXin LI 	}
17254d52a575SXin LI 
172605884511SPyun YongHyeon 	rxsd = &sc->sc_rx_status;
17274d52a575SXin LI 	bzero(rxsd->rxsd_status, sizeof(struct et_rxstatus));
17284d52a575SXin LI 	bus_dmamap_sync(rxsd->rxsd_dtag, rxsd->rxsd_dmap,
172905884511SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
17304d52a575SXin LI 
173105884511SPyun YongHyeon 	rxst_ring = &sc->sc_rxstat_ring;
17324d52a575SXin LI 	bzero(rxst_ring->rsr_stat, ET_RXSTAT_RING_SIZE);
17334d52a575SXin LI 	bus_dmamap_sync(rxst_ring->rsr_dtag, rxst_ring->rsr_dmap,
173405884511SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
17354d52a575SXin LI 
1736398f1b65SPyun YongHyeon 	return (0);
17374d52a575SXin LI }
17384d52a575SXin LI 
17394d52a575SXin LI static int
17404d52a575SXin LI et_init_rxdma(struct et_softc *sc)
17414d52a575SXin LI {
17420b699044SPyun YongHyeon 	struct et_rxstatus_data *rxsd;
17430b699044SPyun YongHyeon 	struct et_rxstat_ring *rxst_ring;
17444d52a575SXin LI 	struct et_rxdesc_ring *rx_ring;
17454d52a575SXin LI 	int error;
17464d52a575SXin LI 
17474d52a575SXin LI 	error = et_stop_rxdma(sc);
17484d52a575SXin LI 	if (error) {
17494d52a575SXin LI 		if_printf(sc->ifp, "can't init RX DMA engine\n");
1750398f1b65SPyun YongHyeon 		return (error);
17514d52a575SXin LI 	}
17524d52a575SXin LI 
17534d52a575SXin LI 	/*
17544d52a575SXin LI 	 * Install RX status
17554d52a575SXin LI 	 */
17560b699044SPyun YongHyeon 	rxsd = &sc->sc_rx_status;
17574d52a575SXin LI 	CSR_WRITE_4(sc, ET_RX_STATUS_HI, ET_ADDR_HI(rxsd->rxsd_paddr));
17584d52a575SXin LI 	CSR_WRITE_4(sc, ET_RX_STATUS_LO, ET_ADDR_LO(rxsd->rxsd_paddr));
17594d52a575SXin LI 
17604d52a575SXin LI 	/*
17614d52a575SXin LI 	 * Install RX stat ring
17624d52a575SXin LI 	 */
17630b699044SPyun YongHyeon 	rxst_ring = &sc->sc_rxstat_ring;
17644d52a575SXin LI 	CSR_WRITE_4(sc, ET_RXSTAT_HI, ET_ADDR_HI(rxst_ring->rsr_paddr));
17654d52a575SXin LI 	CSR_WRITE_4(sc, ET_RXSTAT_LO, ET_ADDR_LO(rxst_ring->rsr_paddr));
17664d52a575SXin LI 	CSR_WRITE_4(sc, ET_RXSTAT_CNT, ET_RX_NSTAT - 1);
17674d52a575SXin LI 	CSR_WRITE_4(sc, ET_RXSTAT_POS, 0);
17684d52a575SXin LI 	CSR_WRITE_4(sc, ET_RXSTAT_MINCNT, ((ET_RX_NSTAT * 15) / 100) - 1);
17694d52a575SXin LI 
17704d52a575SXin LI 	/* Match ET_RXSTAT_POS */
17714d52a575SXin LI 	rxst_ring->rsr_index = 0;
17724d52a575SXin LI 	rxst_ring->rsr_wrap = 0;
17734d52a575SXin LI 
17744d52a575SXin LI 	/*
17754d52a575SXin LI 	 * Install the 2nd RX descriptor ring
17764d52a575SXin LI 	 */
17774d52a575SXin LI 	rx_ring = &sc->sc_rx_ring[1];
17784d52a575SXin LI 	CSR_WRITE_4(sc, ET_RX_RING1_HI, ET_ADDR_HI(rx_ring->rr_paddr));
17794d52a575SXin LI 	CSR_WRITE_4(sc, ET_RX_RING1_LO, ET_ADDR_LO(rx_ring->rr_paddr));
17804d52a575SXin LI 	CSR_WRITE_4(sc, ET_RX_RING1_CNT, ET_RX_NDESC - 1);
17814d52a575SXin LI 	CSR_WRITE_4(sc, ET_RX_RING1_POS, ET_RX_RING1_POS_WRAP);
17824d52a575SXin LI 	CSR_WRITE_4(sc, ET_RX_RING1_MINCNT, ((ET_RX_NDESC * 15) / 100) - 1);
17834d52a575SXin LI 
17844d52a575SXin LI 	/* Match ET_RX_RING1_POS */
17854d52a575SXin LI 	rx_ring->rr_index = 0;
17864d52a575SXin LI 	rx_ring->rr_wrap = 1;
17874d52a575SXin LI 
17884d52a575SXin LI 	/*
17894d52a575SXin LI 	 * Install the 1st RX descriptor ring
17904d52a575SXin LI 	 */
17914d52a575SXin LI 	rx_ring = &sc->sc_rx_ring[0];
17924d52a575SXin LI 	CSR_WRITE_4(sc, ET_RX_RING0_HI, ET_ADDR_HI(rx_ring->rr_paddr));
17934d52a575SXin LI 	CSR_WRITE_4(sc, ET_RX_RING0_LO, ET_ADDR_LO(rx_ring->rr_paddr));
17944d52a575SXin LI 	CSR_WRITE_4(sc, ET_RX_RING0_CNT, ET_RX_NDESC - 1);
17954d52a575SXin LI 	CSR_WRITE_4(sc, ET_RX_RING0_POS, ET_RX_RING0_POS_WRAP);
17964d52a575SXin LI 	CSR_WRITE_4(sc, ET_RX_RING0_MINCNT, ((ET_RX_NDESC * 15) / 100) - 1);
17974d52a575SXin LI 
17984d52a575SXin LI 	/* Match ET_RX_RING0_POS */
17994d52a575SXin LI 	rx_ring->rr_index = 0;
18004d52a575SXin LI 	rx_ring->rr_wrap = 1;
18014d52a575SXin LI 
18024d52a575SXin LI 	/*
18034d52a575SXin LI 	 * RX intr moderation
18044d52a575SXin LI 	 */
18054d52a575SXin LI 	CSR_WRITE_4(sc, ET_RX_INTR_NPKTS, sc->sc_rx_intr_npkts);
18064d52a575SXin LI 	CSR_WRITE_4(sc, ET_RX_INTR_DELAY, sc->sc_rx_intr_delay);
18074d52a575SXin LI 
1808398f1b65SPyun YongHyeon 	return (0);
18094d52a575SXin LI }
18104d52a575SXin LI 
18114d52a575SXin LI static int
18124d52a575SXin LI et_init_txdma(struct et_softc *sc)
18134d52a575SXin LI {
18140b699044SPyun YongHyeon 	struct et_txdesc_ring *tx_ring;
18150b699044SPyun YongHyeon 	struct et_txstatus_data *txsd;
18164d52a575SXin LI 	int error;
18174d52a575SXin LI 
18184d52a575SXin LI 	error = et_stop_txdma(sc);
18194d52a575SXin LI 	if (error) {
18204d52a575SXin LI 		if_printf(sc->ifp, "can't init TX DMA engine\n");
1821398f1b65SPyun YongHyeon 		return (error);
18224d52a575SXin LI 	}
18234d52a575SXin LI 
18244d52a575SXin LI 	/*
18254d52a575SXin LI 	 * Install TX descriptor ring
18264d52a575SXin LI 	 */
18270b699044SPyun YongHyeon 	tx_ring = &sc->sc_tx_ring;
18284d52a575SXin LI 	CSR_WRITE_4(sc, ET_TX_RING_HI, ET_ADDR_HI(tx_ring->tr_paddr));
18294d52a575SXin LI 	CSR_WRITE_4(sc, ET_TX_RING_LO, ET_ADDR_LO(tx_ring->tr_paddr));
18304d52a575SXin LI 	CSR_WRITE_4(sc, ET_TX_RING_CNT, ET_TX_NDESC - 1);
18314d52a575SXin LI 
18324d52a575SXin LI 	/*
18334d52a575SXin LI 	 * Install TX status
18344d52a575SXin LI 	 */
18350b699044SPyun YongHyeon 	txsd = &sc->sc_tx_status;
18364d52a575SXin LI 	CSR_WRITE_4(sc, ET_TX_STATUS_HI, ET_ADDR_HI(txsd->txsd_paddr));
18374d52a575SXin LI 	CSR_WRITE_4(sc, ET_TX_STATUS_LO, ET_ADDR_LO(txsd->txsd_paddr));
18384d52a575SXin LI 
18394d52a575SXin LI 	CSR_WRITE_4(sc, ET_TX_READY_POS, 0);
18404d52a575SXin LI 
18414d52a575SXin LI 	/* Match ET_TX_READY_POS */
18424d52a575SXin LI 	tx_ring->tr_ready_index = 0;
18434d52a575SXin LI 	tx_ring->tr_ready_wrap = 0;
18444d52a575SXin LI 
1845398f1b65SPyun YongHyeon 	return (0);
18464d52a575SXin LI }
18474d52a575SXin LI 
18484d52a575SXin LI static void
18494d52a575SXin LI et_init_mac(struct et_softc *sc)
18504d52a575SXin LI {
18510b699044SPyun YongHyeon 	struct ifnet *ifp;
18520b699044SPyun YongHyeon 	const uint8_t *eaddr;
18534d52a575SXin LI 	uint32_t val;
18544d52a575SXin LI 
18554d52a575SXin LI 	/* Reset MAC */
18564d52a575SXin LI 	CSR_WRITE_4(sc, ET_MAC_CFG1,
18574d52a575SXin LI 		    ET_MAC_CFG1_RST_TXFUNC | ET_MAC_CFG1_RST_RXFUNC |
18584d52a575SXin LI 		    ET_MAC_CFG1_RST_TXMC | ET_MAC_CFG1_RST_RXMC |
18594d52a575SXin LI 		    ET_MAC_CFG1_SIM_RST | ET_MAC_CFG1_SOFT_RST);
18604d52a575SXin LI 
18614d52a575SXin LI 	/*
18624d52a575SXin LI 	 * Setup inter packet gap
18634d52a575SXin LI 	 */
186423263665SPyun YongHyeon 	val = (56 << ET_IPG_NONB2B_1_SHIFT) |
186523263665SPyun YongHyeon 	    (88 << ET_IPG_NONB2B_2_SHIFT) |
186623263665SPyun YongHyeon 	    (80 << ET_IPG_MINIFG_SHIFT) |
186723263665SPyun YongHyeon 	    (96 << ET_IPG_B2B_SHIFT);
18684d52a575SXin LI 	CSR_WRITE_4(sc, ET_IPG, val);
18694d52a575SXin LI 
18704d52a575SXin LI 	/*
18714d52a575SXin LI 	 * Setup half duplex mode
18724d52a575SXin LI 	 */
187323263665SPyun YongHyeon 	val = (10 << ET_MAC_HDX_ALT_BEB_TRUNC_SHIFT) |
187423263665SPyun YongHyeon 	    (15 << ET_MAC_HDX_REXMIT_MAX_SHIFT) |
187523263665SPyun YongHyeon 	    (55 << ET_MAC_HDX_COLLWIN_SHIFT) |
18764d52a575SXin LI 	    ET_MAC_HDX_EXC_DEFER;
18774d52a575SXin LI 	CSR_WRITE_4(sc, ET_MAC_HDX, val);
18784d52a575SXin LI 
18794d52a575SXin LI 	/* Clear MAC control */
18804d52a575SXin LI 	CSR_WRITE_4(sc, ET_MAC_CTRL, 0);
18814d52a575SXin LI 
18824d52a575SXin LI 	/* Reset MII */
18834d52a575SXin LI 	CSR_WRITE_4(sc, ET_MII_CFG, ET_MII_CFG_CLKRST);
18844d52a575SXin LI 
18854d52a575SXin LI 	/*
18864d52a575SXin LI 	 * Set MAC address
18874d52a575SXin LI 	 */
18880b699044SPyun YongHyeon 	ifp = sc->ifp;
18890b699044SPyun YongHyeon 	eaddr = IF_LLADDR(ifp);
18904d52a575SXin LI 	val = eaddr[2] | (eaddr[3] << 8) | (eaddr[4] << 16) | (eaddr[5] << 24);
18914d52a575SXin LI 	CSR_WRITE_4(sc, ET_MAC_ADDR1, val);
18924d52a575SXin LI 	val = (eaddr[0] << 16) | (eaddr[1] << 24);
18934d52a575SXin LI 	CSR_WRITE_4(sc, ET_MAC_ADDR2, val);
18944d52a575SXin LI 
18954d52a575SXin LI 	/* Set max frame length */
18964d52a575SXin LI 	CSR_WRITE_4(sc, ET_MAX_FRMLEN, ET_FRAMELEN(ifp->if_mtu));
18974d52a575SXin LI 
18984d52a575SXin LI 	/* Bring MAC out of reset state */
18994d52a575SXin LI 	CSR_WRITE_4(sc, ET_MAC_CFG1, 0);
19004d52a575SXin LI }
19014d52a575SXin LI 
19024d52a575SXin LI static void
19034d52a575SXin LI et_init_rxmac(struct et_softc *sc)
19044d52a575SXin LI {
19050b699044SPyun YongHyeon 	struct ifnet *ifp;
19060b699044SPyun YongHyeon 	const uint8_t *eaddr;
19074d52a575SXin LI 	uint32_t val;
19084d52a575SXin LI 	int i;
19094d52a575SXin LI 
19104d52a575SXin LI 	/* Disable RX MAC and WOL */
19114d52a575SXin LI 	CSR_WRITE_4(sc, ET_RXMAC_CTRL, ET_RXMAC_CTRL_WOL_DISABLE);
19124d52a575SXin LI 
19134d52a575SXin LI 	/*
19144d52a575SXin LI 	 * Clear all WOL related registers
19154d52a575SXin LI 	 */
19164d52a575SXin LI 	for (i = 0; i < 3; ++i)
19174d52a575SXin LI 		CSR_WRITE_4(sc, ET_WOL_CRC + (i * 4), 0);
19184d52a575SXin LI 	for (i = 0; i < 20; ++i)
19194d52a575SXin LI 		CSR_WRITE_4(sc, ET_WOL_MASK + (i * 4), 0);
19204d52a575SXin LI 
19214d52a575SXin LI 	/*
19224d52a575SXin LI 	 * Set WOL source address.  XXX is this necessary?
19234d52a575SXin LI 	 */
19240b699044SPyun YongHyeon 	ifp = sc->ifp;
19250b699044SPyun YongHyeon 	eaddr = IF_LLADDR(ifp);
19264d52a575SXin LI 	val = (eaddr[2] << 24) | (eaddr[3] << 16) | (eaddr[4] << 8) | eaddr[5];
19274d52a575SXin LI 	CSR_WRITE_4(sc, ET_WOL_SA_LO, val);
19284d52a575SXin LI 	val = (eaddr[0] << 8) | eaddr[1];
19294d52a575SXin LI 	CSR_WRITE_4(sc, ET_WOL_SA_HI, val);
19304d52a575SXin LI 
19314d52a575SXin LI 	/* Clear packet filters */
19324d52a575SXin LI 	CSR_WRITE_4(sc, ET_PKTFILT, 0);
19334d52a575SXin LI 
19344d52a575SXin LI 	/* No ucast filtering */
19354d52a575SXin LI 	CSR_WRITE_4(sc, ET_UCAST_FILTADDR1, 0);
19364d52a575SXin LI 	CSR_WRITE_4(sc, ET_UCAST_FILTADDR2, 0);
19374d52a575SXin LI 	CSR_WRITE_4(sc, ET_UCAST_FILTADDR3, 0);
19384d52a575SXin LI 
19394d52a575SXin LI 	if (ET_FRAMELEN(ifp->if_mtu) > ET_RXMAC_CUT_THRU_FRMLEN) {
19404d52a575SXin LI 		/*
19414d52a575SXin LI 		 * In order to transmit jumbo packets greater than
19424d52a575SXin LI 		 * ET_RXMAC_CUT_THRU_FRMLEN bytes, the FIFO between
19434d52a575SXin LI 		 * RX MAC and RX DMA needs to be reduced in size to
19444d52a575SXin LI 		 * (ET_MEM_SIZE - ET_MEM_TXSIZE_EX - framelen).  In
19454d52a575SXin LI 		 * order to implement this, we must use "cut through"
19464d52a575SXin LI 		 * mode in the RX MAC, which chops packets down into
19474d52a575SXin LI 		 * segments.  In this case we selected 256 bytes,
19484d52a575SXin LI 		 * since this is the size of the PCI-Express TLP's
19494d52a575SXin LI 		 * that the ET1310 uses.
19504d52a575SXin LI 		 */
195123263665SPyun YongHyeon 		val = (ET_RXMAC_SEGSZ(256) & ET_RXMAC_MC_SEGSZ_MAX_MASK) |
19524d52a575SXin LI 		      ET_RXMAC_MC_SEGSZ_ENABLE;
19534d52a575SXin LI 	} else {
19544d52a575SXin LI 		val = 0;
19554d52a575SXin LI 	}
19564d52a575SXin LI 	CSR_WRITE_4(sc, ET_RXMAC_MC_SEGSZ, val);
19574d52a575SXin LI 
19584d52a575SXin LI 	CSR_WRITE_4(sc, ET_RXMAC_MC_WATERMARK, 0);
19594d52a575SXin LI 
19604d52a575SXin LI 	/* Initialize RX MAC management register */
19614d52a575SXin LI 	CSR_WRITE_4(sc, ET_RXMAC_MGT, 0);
19624d52a575SXin LI 
19634d52a575SXin LI 	CSR_WRITE_4(sc, ET_RXMAC_SPACE_AVL, 0);
19644d52a575SXin LI 
19654d52a575SXin LI 	CSR_WRITE_4(sc, ET_RXMAC_MGT,
19664d52a575SXin LI 		    ET_RXMAC_MGT_PASS_ECRC |
19674d52a575SXin LI 		    ET_RXMAC_MGT_PASS_ELEN |
19684d52a575SXin LI 		    ET_RXMAC_MGT_PASS_ETRUNC |
19694d52a575SXin LI 		    ET_RXMAC_MGT_CHECK_PKT);
19704d52a575SXin LI 
19714d52a575SXin LI 	/*
19724d52a575SXin LI 	 * Configure runt filtering (may not work on certain chip generation)
19734d52a575SXin LI 	 */
197423263665SPyun YongHyeon 	val = (ETHER_MIN_LEN << ET_PKTFILT_MINLEN_SHIFT) &
197523263665SPyun YongHyeon 	    ET_PKTFILT_MINLEN_MASK;
197623263665SPyun YongHyeon 	val |= ET_PKTFILT_FRAG;
19774d52a575SXin LI 	CSR_WRITE_4(sc, ET_PKTFILT, val);
19784d52a575SXin LI 
19794d52a575SXin LI 	/* Enable RX MAC but leave WOL disabled */
19804d52a575SXin LI 	CSR_WRITE_4(sc, ET_RXMAC_CTRL,
19814d52a575SXin LI 		    ET_RXMAC_CTRL_WOL_DISABLE | ET_RXMAC_CTRL_ENABLE);
19824d52a575SXin LI 
19834d52a575SXin LI 	/*
19844d52a575SXin LI 	 * Setup multicast hash and allmulti/promisc mode
19854d52a575SXin LI 	 */
19864d52a575SXin LI 	et_setmulti(sc);
19874d52a575SXin LI }
19884d52a575SXin LI 
19894d52a575SXin LI static void
19904d52a575SXin LI et_init_txmac(struct et_softc *sc)
19914d52a575SXin LI {
19920b699044SPyun YongHyeon 
19934d52a575SXin LI 	/* Disable TX MAC and FC(?) */
19944d52a575SXin LI 	CSR_WRITE_4(sc, ET_TXMAC_CTRL, ET_TXMAC_CTRL_FC_DISABLE);
19954d52a575SXin LI 
19965d384a0dSPyun YongHyeon 	/*
19975d384a0dSPyun YongHyeon 	 * Initialize pause time.
19985d384a0dSPyun YongHyeon 	 * This register should be set before XON/XOFF frame is
19995d384a0dSPyun YongHyeon 	 * sent by driver.
20005d384a0dSPyun YongHyeon 	 */
20015d384a0dSPyun YongHyeon 	CSR_WRITE_4(sc, ET_TXMAC_FLOWCTRL, 0 << ET_TXMAC_FLOWCTRL_CFPT_SHIFT);
20024d52a575SXin LI 
20034d52a575SXin LI 	/* Enable TX MAC but leave FC(?) diabled */
20044d52a575SXin LI 	CSR_WRITE_4(sc, ET_TXMAC_CTRL,
20054d52a575SXin LI 		    ET_TXMAC_CTRL_ENABLE | ET_TXMAC_CTRL_FC_DISABLE);
20064d52a575SXin LI }
20074d52a575SXin LI 
20084d52a575SXin LI static int
20094d52a575SXin LI et_start_rxdma(struct et_softc *sc)
20104d52a575SXin LI {
20110b699044SPyun YongHyeon 	uint32_t val;
20124d52a575SXin LI 
20130b699044SPyun YongHyeon 	val = (sc->sc_rx_data[0].rbd_bufsize & ET_RXDMA_CTRL_RING0_SIZE_MASK) |
20144d52a575SXin LI 	    ET_RXDMA_CTRL_RING0_ENABLE;
201523263665SPyun YongHyeon 	val |= (sc->sc_rx_data[1].rbd_bufsize & ET_RXDMA_CTRL_RING1_SIZE_MASK) |
20164d52a575SXin LI 	    ET_RXDMA_CTRL_RING1_ENABLE;
20174d52a575SXin LI 
20184d52a575SXin LI 	CSR_WRITE_4(sc, ET_RXDMA_CTRL, val);
20194d52a575SXin LI 
20204d52a575SXin LI 	DELAY(5);
20214d52a575SXin LI 
20224d52a575SXin LI 	if (CSR_READ_4(sc, ET_RXDMA_CTRL) & ET_RXDMA_CTRL_HALTED) {
20234d52a575SXin LI 		if_printf(sc->ifp, "can't start RX DMA engine\n");
2024398f1b65SPyun YongHyeon 		return (ETIMEDOUT);
20254d52a575SXin LI 	}
2026398f1b65SPyun YongHyeon 	return (0);
20274d52a575SXin LI }
20284d52a575SXin LI 
20294d52a575SXin LI static int
20304d52a575SXin LI et_start_txdma(struct et_softc *sc)
20314d52a575SXin LI {
20320b699044SPyun YongHyeon 
20334d52a575SXin LI 	CSR_WRITE_4(sc, ET_TXDMA_CTRL, ET_TXDMA_CTRL_SINGLE_EPKT);
2034398f1b65SPyun YongHyeon 	return (0);
20354d52a575SXin LI }
20364d52a575SXin LI 
20374d52a575SXin LI static void
20384d52a575SXin LI et_rxeof(struct et_softc *sc)
20394d52a575SXin LI {
20404d52a575SXin LI 	struct et_rxstatus_data *rxsd;
20414d52a575SXin LI 	struct et_rxstat_ring *rxst_ring;
204205884511SPyun YongHyeon 	struct et_rxbuf_data *rbd;
204305884511SPyun YongHyeon 	struct et_rxdesc_ring *rx_ring;
204405884511SPyun YongHyeon 	struct et_rxstat *st;
204505884511SPyun YongHyeon 	struct ifnet *ifp;
204605884511SPyun YongHyeon 	struct mbuf *m;
204705884511SPyun YongHyeon 	uint32_t rxstat_pos, rxring_pos;
204805884511SPyun YongHyeon 	uint32_t rxst_info1, rxst_info2, rxs_stat_ring;
204905884511SPyun YongHyeon 	int buflen, buf_idx, npost[2], ring_idx;
205005884511SPyun YongHyeon 	int rxst_index, rxst_wrap;
20514d52a575SXin LI 
20524d52a575SXin LI 	ET_LOCK_ASSERT(sc);
205305884511SPyun YongHyeon 
20544d52a575SXin LI 	ifp = sc->ifp;
20554d52a575SXin LI 	rxsd = &sc->sc_rx_status;
20564d52a575SXin LI 	rxst_ring = &sc->sc_rxstat_ring;
20574d52a575SXin LI 
20584d52a575SXin LI 	if ((sc->sc_flags & ET_FLAG_TXRX_ENABLED) == 0)
20594d52a575SXin LI 		return;
20604d52a575SXin LI 
20614d52a575SXin LI 	bus_dmamap_sync(rxsd->rxsd_dtag, rxsd->rxsd_dmap,
20624d52a575SXin LI 	    BUS_DMASYNC_POSTREAD);
20634d52a575SXin LI 	bus_dmamap_sync(rxst_ring->rsr_dtag, rxst_ring->rsr_dmap,
20644d52a575SXin LI 	    BUS_DMASYNC_POSTREAD);
20654d52a575SXin LI 
206605884511SPyun YongHyeon 	npost[0] = npost[1] = 0;
206726e07b50SPyun YongHyeon 	rxs_stat_ring = le32toh(rxsd->rxsd_status->rxs_stat_ring);
20684d52a575SXin LI 	rxst_wrap = (rxs_stat_ring & ET_RXS_STATRING_WRAP) ? 1 : 0;
206923263665SPyun YongHyeon 	rxst_index = (rxs_stat_ring & ET_RXS_STATRING_INDEX_MASK) >>
207023263665SPyun YongHyeon 	    ET_RXS_STATRING_INDEX_SHIFT;
20714d52a575SXin LI 
20724d52a575SXin LI 	while (rxst_index != rxst_ring->rsr_index ||
20734d52a575SXin LI 	    rxst_wrap != rxst_ring->rsr_wrap) {
207405884511SPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
207505884511SPyun YongHyeon 			break;
20764d52a575SXin LI 
20774d52a575SXin LI 		MPASS(rxst_ring->rsr_index < ET_RX_NSTAT);
20784d52a575SXin LI 		st = &rxst_ring->rsr_stat[rxst_ring->rsr_index];
207905884511SPyun YongHyeon 		rxst_info1 = le32toh(st->rxst_info1);
208026e07b50SPyun YongHyeon 		rxst_info2 = le32toh(st->rxst_info2);
208126e07b50SPyun YongHyeon 		buflen = (rxst_info2 & ET_RXST_INFO2_LEN_MASK) >>
208223263665SPyun YongHyeon 		    ET_RXST_INFO2_LEN_SHIFT;
208326e07b50SPyun YongHyeon 		buf_idx = (rxst_info2 & ET_RXST_INFO2_BUFIDX_MASK) >>
208423263665SPyun YongHyeon 		    ET_RXST_INFO2_BUFIDX_SHIFT;
208526e07b50SPyun YongHyeon 		ring_idx = (rxst_info2 & ET_RXST_INFO2_RINGIDX_MASK) >>
208623263665SPyun YongHyeon 		    ET_RXST_INFO2_RINGIDX_SHIFT;
20874d52a575SXin LI 
20884d52a575SXin LI 		if (++rxst_ring->rsr_index == ET_RX_NSTAT) {
20894d52a575SXin LI 			rxst_ring->rsr_index = 0;
20904d52a575SXin LI 			rxst_ring->rsr_wrap ^= 1;
20914d52a575SXin LI 		}
209223263665SPyun YongHyeon 		rxstat_pos = rxst_ring->rsr_index & ET_RXSTAT_POS_INDEX_MASK;
20934d52a575SXin LI 		if (rxst_ring->rsr_wrap)
20944d52a575SXin LI 			rxstat_pos |= ET_RXSTAT_POS_WRAP;
20954d52a575SXin LI 		CSR_WRITE_4(sc, ET_RXSTAT_POS, rxstat_pos);
20964d52a575SXin LI 
20974d52a575SXin LI 		if (ring_idx >= ET_RX_NRING) {
2098c13dc687SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
20994d52a575SXin LI 			if_printf(ifp, "invalid ring index %d\n", ring_idx);
21004d52a575SXin LI 			continue;
21014d52a575SXin LI 		}
21024d52a575SXin LI 		if (buf_idx >= ET_RX_NDESC) {
2103c13dc687SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
21044d52a575SXin LI 			if_printf(ifp, "invalid buf index %d\n", buf_idx);
21054d52a575SXin LI 			continue;
21064d52a575SXin LI 		}
21074d52a575SXin LI 
21084d52a575SXin LI 		rbd = &sc->sc_rx_data[ring_idx];
21094d52a575SXin LI 		m = rbd->rbd_buf[buf_idx].rb_mbuf;
211005884511SPyun YongHyeon 		if ((rxst_info1 & ET_RXST_INFO1_OK) == 0){
211105884511SPyun YongHyeon 			/* Discard errored frame. */
211205884511SPyun YongHyeon 			rbd->rbd_discard(rbd, buf_idx);
211305884511SPyun YongHyeon 		} else if (rbd->rbd_newbuf(rbd, buf_idx) != 0) {
211405884511SPyun YongHyeon 			/* No available mbufs, discard it. */
2115c13dc687SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
211605884511SPyun YongHyeon 			rbd->rbd_discard(rbd, buf_idx);
211705884511SPyun YongHyeon 		} else {
211805884511SPyun YongHyeon 			buflen -= ETHER_CRC_LEN;
211905884511SPyun YongHyeon 			if (buflen < ETHER_HDR_LEN) {
21204d52a575SXin LI 				m_freem(m);
2121c13dc687SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
21224d52a575SXin LI 			} else {
212305884511SPyun YongHyeon 				m->m_pkthdr.len = m->m_len = buflen;
21244d52a575SXin LI 				m->m_pkthdr.rcvif = ifp;
21254d52a575SXin LI 				ET_UNLOCK(sc);
21264d52a575SXin LI 				ifp->if_input(ifp, m);
21274d52a575SXin LI 				ET_LOCK(sc);
21284d52a575SXin LI 			}
21294d52a575SXin LI 		}
21304d52a575SXin LI 
21314d52a575SXin LI 		rx_ring = &sc->sc_rx_ring[ring_idx];
21324d52a575SXin LI 		if (buf_idx != rx_ring->rr_index) {
213305884511SPyun YongHyeon 			if_printf(ifp,
213405884511SPyun YongHyeon 			    "WARNING!! ring %d, buf_idx %d, rr_idx %d\n",
21354d52a575SXin LI 			    ring_idx, buf_idx, rx_ring->rr_index);
21364d52a575SXin LI 		}
21374d52a575SXin LI 
21384d52a575SXin LI 		MPASS(rx_ring->rr_index < ET_RX_NDESC);
21394d52a575SXin LI 		if (++rx_ring->rr_index == ET_RX_NDESC) {
21404d52a575SXin LI 			rx_ring->rr_index = 0;
21414d52a575SXin LI 			rx_ring->rr_wrap ^= 1;
21424d52a575SXin LI 		}
214323263665SPyun YongHyeon 		rxring_pos = rx_ring->rr_index & ET_RX_RING_POS_INDEX_MASK;
21444d52a575SXin LI 		if (rx_ring->rr_wrap)
21454d52a575SXin LI 			rxring_pos |= ET_RX_RING_POS_WRAP;
21464d52a575SXin LI 		CSR_WRITE_4(sc, rx_ring->rr_posreg, rxring_pos);
21474d52a575SXin LI 	}
214805884511SPyun YongHyeon 
214905884511SPyun YongHyeon 	bus_dmamap_sync(rxsd->rxsd_dtag, rxsd->rxsd_dmap,
215005884511SPyun YongHyeon 	    BUS_DMASYNC_PREREAD);
215105884511SPyun YongHyeon 	bus_dmamap_sync(rxst_ring->rsr_dtag, rxst_ring->rsr_dmap,
215205884511SPyun YongHyeon 	    BUS_DMASYNC_PREREAD);
21534d52a575SXin LI }
21544d52a575SXin LI 
21554d52a575SXin LI static int
21564d52a575SXin LI et_encap(struct et_softc *sc, struct mbuf **m0)
21574d52a575SXin LI {
215805884511SPyun YongHyeon 	struct et_txdesc_ring *tx_ring;
215905884511SPyun YongHyeon 	struct et_txbuf_data *tbd;
21604d52a575SXin LI 	struct et_txdesc *td;
216105884511SPyun YongHyeon 	struct mbuf *m;
216205884511SPyun YongHyeon 	bus_dma_segment_t segs[ET_NSEG_MAX];
21634d52a575SXin LI 	bus_dmamap_t map;
2164244fd28bSPyun YongHyeon 	uint32_t csum_flags, last_td_ctrl2;
216505884511SPyun YongHyeon 	int error, i, idx, first_idx, last_idx, nsegs;
21664d52a575SXin LI 
216705884511SPyun YongHyeon 	tx_ring = &sc->sc_tx_ring;
21684d52a575SXin LI 	MPASS(tx_ring->tr_ready_index < ET_TX_NDESC);
216905884511SPyun YongHyeon 	tbd = &sc->sc_tx_data;
21704d52a575SXin LI 	first_idx = tx_ring->tr_ready_index;
21714d52a575SXin LI 	map = tbd->tbd_buf[first_idx].tb_dmap;
21724d52a575SXin LI 
217305884511SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->sc_tx_tag, map, *m0, segs, &nsegs,
217405884511SPyun YongHyeon 	    0);
217505884511SPyun YongHyeon 	if (error == EFBIG) {
2176c6499eccSGleb Smirnoff 		m = m_collapse(*m0, M_NOWAIT, ET_NSEG_MAX);
217705884511SPyun YongHyeon 		if (m == NULL) {
217805884511SPyun YongHyeon 			m_freem(*m0);
217905884511SPyun YongHyeon 			*m0 = NULL;
218005884511SPyun YongHyeon 			return (ENOMEM);
21814d52a575SXin LI 		}
218205884511SPyun YongHyeon 		*m0 = m;
218305884511SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->sc_tx_tag, map, *m0, segs,
218405884511SPyun YongHyeon 		    &nsegs, 0);
218505884511SPyun YongHyeon 		if (error != 0) {
218605884511SPyun YongHyeon 			m_freem(*m0);
218705884511SPyun YongHyeon                         *m0 = NULL;
218805884511SPyun YongHyeon 			return (error);
21894d52a575SXin LI 		}
219005884511SPyun YongHyeon 	} else if (error != 0)
219105884511SPyun YongHyeon 		return (error);
21924d52a575SXin LI 
219305884511SPyun YongHyeon 	/* Check for descriptor overruns. */
219405884511SPyun YongHyeon 	if (tbd->tbd_used + nsegs > ET_TX_NDESC - 1) {
219505884511SPyun YongHyeon 		bus_dmamap_unload(sc->sc_tx_tag, map);
219605884511SPyun YongHyeon 		return (ENOBUFS);
21974d52a575SXin LI 	}
219805884511SPyun YongHyeon 	bus_dmamap_sync(sc->sc_tx_tag, map, BUS_DMASYNC_PREWRITE);
21994d52a575SXin LI 
22004d52a575SXin LI 	last_td_ctrl2 = ET_TDCTRL2_LAST_FRAG;
220105884511SPyun YongHyeon 	sc->sc_tx += nsegs;
22024d52a575SXin LI 	if (sc->sc_tx / sc->sc_tx_intr_nsegs != sc->sc_tx_intr) {
22034d52a575SXin LI 		sc->sc_tx_intr = sc->sc_tx / sc->sc_tx_intr_nsegs;
22044d52a575SXin LI 		last_td_ctrl2 |= ET_TDCTRL2_INTR;
22054d52a575SXin LI 	}
22064d52a575SXin LI 
220705884511SPyun YongHyeon 	m = *m0;
22089955274cSPyun YongHyeon 	csum_flags = 0;
22099955274cSPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & ET_CSUM_FEATURES) != 0) {
22109955274cSPyun YongHyeon 		if ((m->m_pkthdr.csum_flags & CSUM_IP) != 0)
22119955274cSPyun YongHyeon 			csum_flags |= ET_TDCTRL2_CSUM_IP;
22129955274cSPyun YongHyeon 		if ((m->m_pkthdr.csum_flags & CSUM_UDP) != 0)
22139955274cSPyun YongHyeon 			csum_flags |= ET_TDCTRL2_CSUM_UDP;
22149955274cSPyun YongHyeon 		else if ((m->m_pkthdr.csum_flags & CSUM_TCP) != 0)
22159955274cSPyun YongHyeon 			csum_flags |= ET_TDCTRL2_CSUM_TCP;
22169955274cSPyun YongHyeon 	}
22174d52a575SXin LI 	last_idx = -1;
221805884511SPyun YongHyeon 	for (i = 0; i < nsegs; ++i) {
22194d52a575SXin LI 		idx = (first_idx + i) % ET_TX_NDESC;
22204d52a575SXin LI 		td = &tx_ring->tr_desc[idx];
222126e07b50SPyun YongHyeon 		td->td_addr_hi = htole32(ET_ADDR_HI(segs[i].ds_addr));
222226e07b50SPyun YongHyeon 		td->td_addr_lo = htole32(ET_ADDR_LO(segs[i].ds_addr));
222326e07b50SPyun YongHyeon 		td->td_ctrl1 =  htole32(segs[i].ds_len & ET_TDCTRL1_LEN_MASK);
222405884511SPyun YongHyeon 		if (i == nsegs - 1) {
222505884511SPyun YongHyeon 			/* Last frag */
22269955274cSPyun YongHyeon 			td->td_ctrl2 = htole32(last_td_ctrl2 | csum_flags);
22274d52a575SXin LI 			last_idx = idx;
22289955274cSPyun YongHyeon 		} else
22299955274cSPyun YongHyeon 			td->td_ctrl2 = htole32(csum_flags);
22304d52a575SXin LI 
22314d52a575SXin LI 		MPASS(tx_ring->tr_ready_index < ET_TX_NDESC);
22324d52a575SXin LI 		if (++tx_ring->tr_ready_index == ET_TX_NDESC) {
22334d52a575SXin LI 			tx_ring->tr_ready_index = 0;
22344d52a575SXin LI 			tx_ring->tr_ready_wrap ^= 1;
22354d52a575SXin LI 		}
22364d52a575SXin LI 	}
22374d52a575SXin LI 	td = &tx_ring->tr_desc[first_idx];
223805884511SPyun YongHyeon 	/* First frag */
223905884511SPyun YongHyeon 	td->td_ctrl2 |= htole32(ET_TDCTRL2_FIRST_FRAG);
22404d52a575SXin LI 
22414d52a575SXin LI 	MPASS(last_idx >= 0);
22424d52a575SXin LI 	tbd->tbd_buf[first_idx].tb_dmap = tbd->tbd_buf[last_idx].tb_dmap;
22434d52a575SXin LI 	tbd->tbd_buf[last_idx].tb_dmap = map;
22444d52a575SXin LI 	tbd->tbd_buf[last_idx].tb_mbuf = m;
22454d52a575SXin LI 
224605884511SPyun YongHyeon 	tbd->tbd_used += nsegs;
22474d52a575SXin LI 	MPASS(tbd->tbd_used <= ET_TX_NDESC);
22484d52a575SXin LI 
224905884511SPyun YongHyeon 	return (0);
22504d52a575SXin LI }
22514d52a575SXin LI 
22524d52a575SXin LI static void
22534d52a575SXin LI et_txeof(struct et_softc *sc)
22544d52a575SXin LI {
22554d52a575SXin LI 	struct et_txdesc_ring *tx_ring;
22564d52a575SXin LI 	struct et_txbuf_data *tbd;
225705884511SPyun YongHyeon 	struct et_txbuf *tb;
225805884511SPyun YongHyeon 	struct ifnet *ifp;
22594d52a575SXin LI 	uint32_t tx_done;
22604d52a575SXin LI 	int end, wrap;
22614d52a575SXin LI 
22624d52a575SXin LI 	ET_LOCK_ASSERT(sc);
226305884511SPyun YongHyeon 
22644d52a575SXin LI 	ifp = sc->ifp;
22654d52a575SXin LI 	tx_ring = &sc->sc_tx_ring;
22664d52a575SXin LI 	tbd = &sc->sc_tx_data;
22674d52a575SXin LI 
22684d52a575SXin LI 	if ((sc->sc_flags & ET_FLAG_TXRX_ENABLED) == 0)
22694d52a575SXin LI 		return;
22704d52a575SXin LI 
22714d52a575SXin LI 	if (tbd->tbd_used == 0)
22724d52a575SXin LI 		return;
22734d52a575SXin LI 
227405884511SPyun YongHyeon 	bus_dmamap_sync(tx_ring->tr_dtag, tx_ring->tr_dmap,
227505884511SPyun YongHyeon 	    BUS_DMASYNC_POSTWRITE);
227605884511SPyun YongHyeon 
22774d52a575SXin LI 	tx_done = CSR_READ_4(sc, ET_TX_DONE_POS);
227823263665SPyun YongHyeon 	end = tx_done & ET_TX_DONE_POS_INDEX_MASK;
22794d52a575SXin LI 	wrap = (tx_done & ET_TX_DONE_POS_WRAP) ? 1 : 0;
22804d52a575SXin LI 
22814d52a575SXin LI 	while (tbd->tbd_start_index != end || tbd->tbd_start_wrap != wrap) {
22824d52a575SXin LI 		MPASS(tbd->tbd_start_index < ET_TX_NDESC);
22834d52a575SXin LI 		tb = &tbd->tbd_buf[tbd->tbd_start_index];
22844d52a575SXin LI 		if (tb->tb_mbuf != NULL) {
228505884511SPyun YongHyeon 			bus_dmamap_sync(sc->sc_tx_tag, tb->tb_dmap,
228605884511SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
228705884511SPyun YongHyeon 			bus_dmamap_unload(sc->sc_tx_tag, tb->tb_dmap);
22884d52a575SXin LI 			m_freem(tb->tb_mbuf);
22894d52a575SXin LI 			tb->tb_mbuf = NULL;
22904d52a575SXin LI 		}
22914d52a575SXin LI 
22924d52a575SXin LI 		if (++tbd->tbd_start_index == ET_TX_NDESC) {
22934d52a575SXin LI 			tbd->tbd_start_index = 0;
22944d52a575SXin LI 			tbd->tbd_start_wrap ^= 1;
22954d52a575SXin LI 		}
22964d52a575SXin LI 
22974d52a575SXin LI 		MPASS(tbd->tbd_used > 0);
22984d52a575SXin LI 		tbd->tbd_used--;
22994d52a575SXin LI 	}
23004d52a575SXin LI 
23014d52a575SXin LI 	if (tbd->tbd_used == 0)
23024d52a575SXin LI 		sc->watchdog_timer = 0;
230305884511SPyun YongHyeon 	if (tbd->tbd_used + ET_NSEG_SPARE < ET_TX_NDESC)
23044d52a575SXin LI 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
23054d52a575SXin LI }
23061f009e2fSPyun YongHyeon 
23074d52a575SXin LI static void
23084d52a575SXin LI et_tick(void *xsc)
23094d52a575SXin LI {
23100b699044SPyun YongHyeon 	struct et_softc *sc;
23114d52a575SXin LI 	struct ifnet *ifp;
23124d52a575SXin LI 	struct mii_data *mii;
23134d52a575SXin LI 
23140b699044SPyun YongHyeon 	sc = xsc;
23154d52a575SXin LI 	ET_LOCK_ASSERT(sc);
23164d52a575SXin LI 	ifp = sc->ifp;
23174d52a575SXin LI 	mii = device_get_softc(sc->sc_miibus);
23184d52a575SXin LI 
23194d52a575SXin LI 	mii_tick(mii);
2320e0b5ac02SPyun YongHyeon 	et_stats_update(sc);
232105884511SPyun YongHyeon 	if (et_watchdog(sc) == EJUSTRETURN)
232205884511SPyun YongHyeon 		return;
23234d52a575SXin LI 	callout_reset(&sc->sc_tick, hz, et_tick, sc);
23244d52a575SXin LI }
23254d52a575SXin LI 
23264d52a575SXin LI static int
232705884511SPyun YongHyeon et_newbuf_cluster(struct et_rxbuf_data *rbd, int buf_idx)
23284d52a575SXin LI {
232905884511SPyun YongHyeon 	struct et_softc *sc;
233005884511SPyun YongHyeon 	struct et_rxdesc *desc;
23314d52a575SXin LI 	struct et_rxbuf *rb;
23324d52a575SXin LI 	struct mbuf *m;
233305884511SPyun YongHyeon 	bus_dma_segment_t segs[1];
23344d52a575SXin LI 	bus_dmamap_t dmap;
233505884511SPyun YongHyeon 	int nsegs;
23364d52a575SXin LI 
23374d52a575SXin LI 	MPASS(buf_idx < ET_RX_NDESC);
2338c6499eccSGleb Smirnoff 	m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
233905884511SPyun YongHyeon 	if (m == NULL)
234005884511SPyun YongHyeon 		return (ENOBUFS);
234105884511SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MCLBYTES;
234205884511SPyun YongHyeon 	m_adj(m, ETHER_ALIGN);
234305884511SPyun YongHyeon 
234405884511SPyun YongHyeon 	sc = rbd->rbd_softc;
23454d52a575SXin LI 	rb = &rbd->rbd_buf[buf_idx];
23464d52a575SXin LI 
234705884511SPyun YongHyeon 	if (bus_dmamap_load_mbuf_sg(sc->sc_rx_tag, sc->sc_rx_sparemap, m,
234805884511SPyun YongHyeon 	    segs, &nsegs, 0) != 0) {
23494d52a575SXin LI 		m_freem(m);
235005884511SPyun YongHyeon 		return (ENOBUFS);
23514d52a575SXin LI 	}
235205884511SPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
23534d52a575SXin LI 
235405884511SPyun YongHyeon 	if (rb->rb_mbuf != NULL) {
235505884511SPyun YongHyeon 		bus_dmamap_sync(sc->sc_rx_tag, rb->rb_dmap,
23564d52a575SXin LI 		    BUS_DMASYNC_POSTREAD);
235705884511SPyun YongHyeon 		bus_dmamap_unload(sc->sc_rx_tag, rb->rb_dmap);
23584d52a575SXin LI 	}
23594d52a575SXin LI 	dmap = rb->rb_dmap;
236005884511SPyun YongHyeon 	rb->rb_dmap = sc->sc_rx_sparemap;
236105884511SPyun YongHyeon 	sc->sc_rx_sparemap = dmap;
236205884511SPyun YongHyeon 	bus_dmamap_sync(sc->sc_rx_tag, rb->rb_dmap, BUS_DMASYNC_PREREAD);
23634d52a575SXin LI 
236405884511SPyun YongHyeon 	rb->rb_mbuf = m;
236505884511SPyun YongHyeon 	desc = &rbd->rbd_ring->rr_desc[buf_idx];
236605884511SPyun YongHyeon 	desc->rd_addr_hi = htole32(ET_ADDR_HI(segs[0].ds_addr));
236705884511SPyun YongHyeon 	desc->rd_addr_lo = htole32(ET_ADDR_LO(segs[0].ds_addr));
236805884511SPyun YongHyeon 	desc->rd_ctrl = htole32(buf_idx & ET_RDCTRL_BUFIDX_MASK);
236905884511SPyun YongHyeon 	bus_dmamap_sync(rbd->rbd_ring->rr_dtag, rbd->rbd_ring->rr_dmap,
237005884511SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
237105884511SPyun YongHyeon 	return (0);
237205884511SPyun YongHyeon }
237305884511SPyun YongHyeon 
237405884511SPyun YongHyeon static void
237505884511SPyun YongHyeon et_rxbuf_discard(struct et_rxbuf_data *rbd, int buf_idx)
237605884511SPyun YongHyeon {
237705884511SPyun YongHyeon 	struct et_rxdesc *desc;
237805884511SPyun YongHyeon 
237905884511SPyun YongHyeon 	desc = &rbd->rbd_ring->rr_desc[buf_idx];
238005884511SPyun YongHyeon 	desc->rd_ctrl = htole32(buf_idx & ET_RDCTRL_BUFIDX_MASK);
238105884511SPyun YongHyeon 	bus_dmamap_sync(rbd->rbd_ring->rr_dtag, rbd->rbd_ring->rr_dmap,
238205884511SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
238305884511SPyun YongHyeon }
238405884511SPyun YongHyeon 
238505884511SPyun YongHyeon static int
238605884511SPyun YongHyeon et_newbuf_hdr(struct et_rxbuf_data *rbd, int buf_idx)
238705884511SPyun YongHyeon {
238805884511SPyun YongHyeon 	struct et_softc *sc;
238905884511SPyun YongHyeon 	struct et_rxdesc *desc;
239005884511SPyun YongHyeon 	struct et_rxbuf *rb;
239105884511SPyun YongHyeon 	struct mbuf *m;
239205884511SPyun YongHyeon 	bus_dma_segment_t segs[1];
239305884511SPyun YongHyeon 	bus_dmamap_t dmap;
239405884511SPyun YongHyeon 	int nsegs;
239505884511SPyun YongHyeon 
239605884511SPyun YongHyeon 	MPASS(buf_idx < ET_RX_NDESC);
2397c6499eccSGleb Smirnoff 	MGETHDR(m, M_NOWAIT, MT_DATA);
239805884511SPyun YongHyeon 	if (m == NULL)
239905884511SPyun YongHyeon 		return (ENOBUFS);
240005884511SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MHLEN;
240105884511SPyun YongHyeon 	m_adj(m, ETHER_ALIGN);
240205884511SPyun YongHyeon 
240305884511SPyun YongHyeon 	sc = rbd->rbd_softc;
240405884511SPyun YongHyeon 	rb = &rbd->rbd_buf[buf_idx];
240505884511SPyun YongHyeon 
240605884511SPyun YongHyeon 	if (bus_dmamap_load_mbuf_sg(sc->sc_rx_mini_tag, sc->sc_rx_mini_sparemap,
240705884511SPyun YongHyeon 	    m, segs, &nsegs, 0) != 0) {
240805884511SPyun YongHyeon 		m_freem(m);
240905884511SPyun YongHyeon 		return (ENOBUFS);
241005884511SPyun YongHyeon 	}
241105884511SPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
241205884511SPyun YongHyeon 
241305884511SPyun YongHyeon 	if (rb->rb_mbuf != NULL) {
241405884511SPyun YongHyeon 		bus_dmamap_sync(sc->sc_rx_mini_tag, rb->rb_dmap,
241505884511SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD);
241605884511SPyun YongHyeon 		bus_dmamap_unload(sc->sc_rx_mini_tag, rb->rb_dmap);
241705884511SPyun YongHyeon 	}
241805884511SPyun YongHyeon 	dmap = rb->rb_dmap;
241905884511SPyun YongHyeon 	rb->rb_dmap = sc->sc_rx_mini_sparemap;
242005884511SPyun YongHyeon 	sc->sc_rx_mini_sparemap = dmap;
242105884511SPyun YongHyeon 	bus_dmamap_sync(sc->sc_rx_mini_tag, rb->rb_dmap, BUS_DMASYNC_PREREAD);
242205884511SPyun YongHyeon 
242305884511SPyun YongHyeon 	rb->rb_mbuf = m;
242405884511SPyun YongHyeon 	desc = &rbd->rbd_ring->rr_desc[buf_idx];
242505884511SPyun YongHyeon 	desc->rd_addr_hi = htole32(ET_ADDR_HI(segs[0].ds_addr));
242605884511SPyun YongHyeon 	desc->rd_addr_lo = htole32(ET_ADDR_LO(segs[0].ds_addr));
242705884511SPyun YongHyeon 	desc->rd_ctrl = htole32(buf_idx & ET_RDCTRL_BUFIDX_MASK);
242805884511SPyun YongHyeon 	bus_dmamap_sync(rbd->rbd_ring->rr_dtag, rbd->rbd_ring->rr_dmap,
242905884511SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
243005884511SPyun YongHyeon 	return (0);
24314d52a575SXin LI }
24324d52a575SXin LI 
2433e0b5ac02SPyun YongHyeon #define	ET_SYSCTL_STAT_ADD32(c, h, n, p, d)	\
2434e0b5ac02SPyun YongHyeon 	    SYSCTL_ADD_UINT(c, h, OID_AUTO, n, CTLFLAG_RD, p, 0, d)
2435e0b5ac02SPyun YongHyeon #define	ET_SYSCTL_STAT_ADD64(c, h, n, p, d)	\
2436e0b5ac02SPyun YongHyeon 	    SYSCTL_ADD_UQUAD(c, h, OID_AUTO, n, CTLFLAG_RD, p, d)
2437e0b5ac02SPyun YongHyeon 
24384d52a575SXin LI /*
24394d52a575SXin LI  * Create sysctl tree
24404d52a575SXin LI  */
24414d52a575SXin LI static void
24424d52a575SXin LI et_add_sysctls(struct et_softc * sc)
24434d52a575SXin LI {
24444d52a575SXin LI 	struct sysctl_ctx_list *ctx;
2445e0b5ac02SPyun YongHyeon 	struct sysctl_oid_list *children, *parent;
2446e0b5ac02SPyun YongHyeon 	struct sysctl_oid *tree;
2447e0b5ac02SPyun YongHyeon 	struct et_hw_stats *stats;
24484d52a575SXin LI 
24494d52a575SXin LI 	ctx = device_get_sysctl_ctx(sc->dev);
24504d52a575SXin LI 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev));
24514d52a575SXin LI 
24524d52a575SXin LI 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rx_intr_npkts",
2453*7029da5cSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc, 0,
2454*7029da5cSPawel Biernacki 	    et_sysctl_rx_intr_npkts, "I", "RX IM, # packets per RX interrupt");
24554d52a575SXin LI 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rx_intr_delay",
2456*7029da5cSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc, 0,
2457*7029da5cSPawel Biernacki 	    et_sysctl_rx_intr_delay, "I",
24584d52a575SXin LI 	    "RX IM, RX interrupt delay (x10 usec)");
24594d52a575SXin LI 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_intr_nsegs",
24604d52a575SXin LI 	    CTLFLAG_RW, &sc->sc_tx_intr_nsegs, 0,
24614d52a575SXin LI 	    "TX IM, # segments per TX interrupt");
24624d52a575SXin LI 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "timer",
24634d52a575SXin LI 	    CTLFLAG_RW, &sc->sc_timer, 0, "TX timer");
2464e0b5ac02SPyun YongHyeon 
2465*7029da5cSPawel Biernacki 	tree = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "stats",
2466*7029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "ET statistics");
2467e0b5ac02SPyun YongHyeon         parent = SYSCTL_CHILDREN(tree);
2468e0b5ac02SPyun YongHyeon 
2469e0b5ac02SPyun YongHyeon 	/* TX/RX statistics. */
2470e0b5ac02SPyun YongHyeon 	stats = &sc->sc_stats;
2471e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD64(ctx, parent, "frames_64", &stats->pkts_64,
2472e0b5ac02SPyun YongHyeon 	    "0 to 64 bytes frames");
2473e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD64(ctx, parent, "frames_65_127", &stats->pkts_65,
2474e0b5ac02SPyun YongHyeon 	    "65 to 127 bytes frames");
2475e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD64(ctx, parent, "frames_128_255", &stats->pkts_128,
2476e0b5ac02SPyun YongHyeon 	    "128 to 255 bytes frames");
2477e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD64(ctx, parent, "frames_256_511", &stats->pkts_256,
2478e0b5ac02SPyun YongHyeon 	    "256 to 511 bytes frames");
2479e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD64(ctx, parent, "frames_512_1023", &stats->pkts_512,
2480e0b5ac02SPyun YongHyeon 	    "512 to 1023 bytes frames");
2481e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD64(ctx, parent, "frames_1024_1518", &stats->pkts_1024,
2482e0b5ac02SPyun YongHyeon 	    "1024 to 1518 bytes frames");
2483e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD64(ctx, parent, "frames_1519_1522", &stats->pkts_1519,
2484e0b5ac02SPyun YongHyeon 	    "1519 to 1522 bytes frames");
2485e0b5ac02SPyun YongHyeon 
2486e0b5ac02SPyun YongHyeon 	/* RX statistics. */
2487*7029da5cSPawel Biernacki 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "rx",
2488*7029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "RX MAC statistics");
2489e0b5ac02SPyun YongHyeon 	children = SYSCTL_CHILDREN(tree);
2490e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD64(ctx, children, "bytes",
2491e0b5ac02SPyun YongHyeon 	    &stats->rx_bytes, "Good bytes");
2492e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD64(ctx, children, "frames",
2493e0b5ac02SPyun YongHyeon 	    &stats->rx_frames, "Good frames");
2494e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "crc_errs",
2495e0b5ac02SPyun YongHyeon 	    &stats->rx_crcerrs, "CRC errors");
2496e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD64(ctx, children, "mcast_frames",
2497e0b5ac02SPyun YongHyeon 	    &stats->rx_mcast, "Multicast frames");
2498e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD64(ctx, children, "bcast_frames",
2499e0b5ac02SPyun YongHyeon 	    &stats->rx_bcast, "Broadcast frames");
2500e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "control",
2501e0b5ac02SPyun YongHyeon 	    &stats->rx_control, "Control frames");
2502e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "pause",
2503e0b5ac02SPyun YongHyeon 	    &stats->rx_pause, "Pause frames");
2504e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "unknown_control",
2505e0b5ac02SPyun YongHyeon 	    &stats->rx_unknown_control, "Unknown control frames");
2506e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "align_errs",
2507e0b5ac02SPyun YongHyeon 	    &stats->rx_alignerrs, "Alignment errors");
2508e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "len_errs",
2509e0b5ac02SPyun YongHyeon 	    &stats->rx_lenerrs, "Frames with length mismatched");
2510e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "code_errs",
2511e0b5ac02SPyun YongHyeon 	    &stats->rx_codeerrs, "Frames with code error");
2512e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "cs_errs",
2513e0b5ac02SPyun YongHyeon 	    &stats->rx_cserrs, "Frames with carrier sense error");
2514e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "runts",
2515e0b5ac02SPyun YongHyeon 	    &stats->rx_runts, "Too short frames");
2516e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD64(ctx, children, "oversize",
2517e0b5ac02SPyun YongHyeon 	    &stats->rx_oversize, "Oversized frames");
2518e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "fragments",
2519e0b5ac02SPyun YongHyeon 	    &stats->rx_fragments, "Fragmented frames");
2520e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "jabbers",
2521e0b5ac02SPyun YongHyeon 	    &stats->rx_jabbers, "Frames with jabber error");
2522e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "drop",
2523e0b5ac02SPyun YongHyeon 	    &stats->rx_drop, "Dropped frames");
2524e0b5ac02SPyun YongHyeon 
2525e0b5ac02SPyun YongHyeon 	/* TX statistics. */
2526*7029da5cSPawel Biernacki 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "tx",
2527*7029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "TX MAC statistics");
2528e0b5ac02SPyun YongHyeon 	children = SYSCTL_CHILDREN(tree);
2529e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD64(ctx, children, "bytes",
2530e0b5ac02SPyun YongHyeon 	    &stats->tx_bytes, "Good bytes");
2531e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD64(ctx, children, "frames",
2532e0b5ac02SPyun YongHyeon 	    &stats->tx_frames, "Good frames");
2533e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD64(ctx, children, "mcast_frames",
2534e0b5ac02SPyun YongHyeon 	    &stats->tx_mcast, "Multicast frames");
2535e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD64(ctx, children, "bcast_frames",
2536e0b5ac02SPyun YongHyeon 	    &stats->tx_bcast, "Broadcast frames");
2537e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "pause",
2538e0b5ac02SPyun YongHyeon 	    &stats->tx_pause, "Pause frames");
2539e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "deferred",
2540e0b5ac02SPyun YongHyeon 	    &stats->tx_deferred, "Deferred frames");
2541e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "excess_deferred",
2542e0b5ac02SPyun YongHyeon 	    &stats->tx_excess_deferred, "Excessively deferred frames");
2543e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "single_colls",
2544e0b5ac02SPyun YongHyeon 	    &stats->tx_single_colls, "Single collisions");
2545e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "multi_colls",
2546e0b5ac02SPyun YongHyeon 	    &stats->tx_multi_colls, "Multiple collisions");
2547e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "late_colls",
2548e0b5ac02SPyun YongHyeon 	    &stats->tx_late_colls, "Late collisions");
2549e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "excess_colls",
2550e0b5ac02SPyun YongHyeon 	    &stats->tx_excess_colls, "Excess collisions");
2551e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "total_colls",
2552e0b5ac02SPyun YongHyeon 	    &stats->tx_total_colls, "Total collisions");
2553e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "pause_honored",
2554e0b5ac02SPyun YongHyeon 	    &stats->tx_pause_honored, "Honored pause frames");
2555e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "drop",
2556e0b5ac02SPyun YongHyeon 	    &stats->tx_drop, "Dropped frames");
2557e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "jabbers",
2558e0b5ac02SPyun YongHyeon 	    &stats->tx_jabbers, "Frames with jabber errors");
2559e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "crc_errs",
2560e0b5ac02SPyun YongHyeon 	    &stats->tx_crcerrs, "Frames with CRC errors");
2561e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "control",
2562e0b5ac02SPyun YongHyeon 	    &stats->tx_control, "Control frames");
2563e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD64(ctx, children, "oversize",
2564e0b5ac02SPyun YongHyeon 	    &stats->tx_oversize, "Oversized frames");
2565e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "undersize",
2566e0b5ac02SPyun YongHyeon 	    &stats->tx_undersize, "Undersized frames");
2567e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "fragments",
2568e0b5ac02SPyun YongHyeon 	    &stats->tx_fragments, "Fragmented frames");
25694d52a575SXin LI }
25704d52a575SXin LI 
2571e0b5ac02SPyun YongHyeon #undef	ET_SYSCTL_STAT_ADD32
2572e0b5ac02SPyun YongHyeon #undef	ET_SYSCTL_STAT_ADD64
2573e0b5ac02SPyun YongHyeon 
25744d52a575SXin LI static int
25754d52a575SXin LI et_sysctl_rx_intr_npkts(SYSCTL_HANDLER_ARGS)
25764d52a575SXin LI {
25770b699044SPyun YongHyeon 	struct et_softc *sc;
25780b699044SPyun YongHyeon 	struct ifnet *ifp;
25790b699044SPyun YongHyeon 	int error, v;
25804d52a575SXin LI 
25810b699044SPyun YongHyeon 	sc = arg1;
25820b699044SPyun YongHyeon 	ifp = sc->ifp;
25834d52a575SXin LI 	v = sc->sc_rx_intr_npkts;
25844d52a575SXin LI 	error = sysctl_handle_int(oidp, &v, 0, req);
25854d52a575SXin LI 	if (error || req->newptr == NULL)
25864d52a575SXin LI 		goto back;
25874d52a575SXin LI 	if (v <= 0) {
25884d52a575SXin LI 		error = EINVAL;
25894d52a575SXin LI 		goto back;
25904d52a575SXin LI 	}
25914d52a575SXin LI 
25924d52a575SXin LI 	if (sc->sc_rx_intr_npkts != v) {
25934d52a575SXin LI 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
25944d52a575SXin LI 			CSR_WRITE_4(sc, ET_RX_INTR_NPKTS, v);
25954d52a575SXin LI 		sc->sc_rx_intr_npkts = v;
25964d52a575SXin LI 	}
25974d52a575SXin LI back:
2598398f1b65SPyun YongHyeon 	return (error);
25994d52a575SXin LI }
26004d52a575SXin LI 
26014d52a575SXin LI static int
26024d52a575SXin LI et_sysctl_rx_intr_delay(SYSCTL_HANDLER_ARGS)
26034d52a575SXin LI {
26040b699044SPyun YongHyeon 	struct et_softc *sc;
26050b699044SPyun YongHyeon 	struct ifnet *ifp;
26060b699044SPyun YongHyeon 	int error, v;
26074d52a575SXin LI 
26080b699044SPyun YongHyeon 	sc = arg1;
26090b699044SPyun YongHyeon 	ifp = sc->ifp;
26104d52a575SXin LI 	v = sc->sc_rx_intr_delay;
26114d52a575SXin LI 	error = sysctl_handle_int(oidp, &v, 0, req);
26124d52a575SXin LI 	if (error || req->newptr == NULL)
26134d52a575SXin LI 		goto back;
26144d52a575SXin LI 	if (v <= 0) {
26154d52a575SXin LI 		error = EINVAL;
26164d52a575SXin LI 		goto back;
26174d52a575SXin LI 	}
26184d52a575SXin LI 
26194d52a575SXin LI 	if (sc->sc_rx_intr_delay != v) {
26204d52a575SXin LI 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
26214d52a575SXin LI 			CSR_WRITE_4(sc, ET_RX_INTR_DELAY, v);
26224d52a575SXin LI 		sc->sc_rx_intr_delay = v;
26234d52a575SXin LI 	}
26244d52a575SXin LI back:
2625398f1b65SPyun YongHyeon 	return (error);
26264d52a575SXin LI }
26274d52a575SXin LI 
2628e0b5ac02SPyun YongHyeon static void
2629e0b5ac02SPyun YongHyeon et_stats_update(struct et_softc *sc)
2630e0b5ac02SPyun YongHyeon {
2631e0b5ac02SPyun YongHyeon 	struct et_hw_stats *stats;
2632e0b5ac02SPyun YongHyeon 
2633e0b5ac02SPyun YongHyeon 	stats = &sc->sc_stats;
2634e0b5ac02SPyun YongHyeon 	stats->pkts_64 += CSR_READ_4(sc, ET_STAT_PKTS_64);
2635e0b5ac02SPyun YongHyeon 	stats->pkts_65 += CSR_READ_4(sc, ET_STAT_PKTS_65_127);
2636e0b5ac02SPyun YongHyeon 	stats->pkts_128 += CSR_READ_4(sc, ET_STAT_PKTS_128_255);
2637e0b5ac02SPyun YongHyeon 	stats->pkts_256 += CSR_READ_4(sc, ET_STAT_PKTS_256_511);
2638e0b5ac02SPyun YongHyeon 	stats->pkts_512 += CSR_READ_4(sc, ET_STAT_PKTS_512_1023);
2639e0b5ac02SPyun YongHyeon 	stats->pkts_1024 += CSR_READ_4(sc, ET_STAT_PKTS_1024_1518);
2640e0b5ac02SPyun YongHyeon 	stats->pkts_1519 += CSR_READ_4(sc, ET_STAT_PKTS_1519_1522);
2641e0b5ac02SPyun YongHyeon 
2642e0b5ac02SPyun YongHyeon 	stats->rx_bytes += CSR_READ_4(sc, ET_STAT_RX_BYTES);
2643e0b5ac02SPyun YongHyeon 	stats->rx_frames += CSR_READ_4(sc, ET_STAT_RX_FRAMES);
2644e0b5ac02SPyun YongHyeon 	stats->rx_crcerrs += CSR_READ_4(sc, ET_STAT_RX_CRC_ERR);
2645e0b5ac02SPyun YongHyeon 	stats->rx_mcast += CSR_READ_4(sc, ET_STAT_RX_MCAST);
2646e0b5ac02SPyun YongHyeon 	stats->rx_bcast += CSR_READ_4(sc, ET_STAT_RX_BCAST);
2647e0b5ac02SPyun YongHyeon 	stats->rx_control += CSR_READ_4(sc, ET_STAT_RX_CTL);
2648e0b5ac02SPyun YongHyeon 	stats->rx_pause += CSR_READ_4(sc, ET_STAT_RX_PAUSE);
2649e0b5ac02SPyun YongHyeon 	stats->rx_unknown_control += CSR_READ_4(sc, ET_STAT_RX_UNKNOWN_CTL);
2650e0b5ac02SPyun YongHyeon 	stats->rx_alignerrs += CSR_READ_4(sc, ET_STAT_RX_ALIGN_ERR);
2651e0b5ac02SPyun YongHyeon 	stats->rx_lenerrs += CSR_READ_4(sc, ET_STAT_RX_LEN_ERR);
2652e0b5ac02SPyun YongHyeon 	stats->rx_codeerrs += CSR_READ_4(sc, ET_STAT_RX_CODE_ERR);
2653e0b5ac02SPyun YongHyeon 	stats->rx_cserrs += CSR_READ_4(sc, ET_STAT_RX_CS_ERR);
2654e0b5ac02SPyun YongHyeon 	stats->rx_runts += CSR_READ_4(sc, ET_STAT_RX_RUNT);
2655e0b5ac02SPyun YongHyeon 	stats->rx_oversize += CSR_READ_4(sc, ET_STAT_RX_OVERSIZE);
2656e0b5ac02SPyun YongHyeon 	stats->rx_fragments += CSR_READ_4(sc, ET_STAT_RX_FRAG);
2657e0b5ac02SPyun YongHyeon 	stats->rx_jabbers += CSR_READ_4(sc, ET_STAT_RX_JABBER);
2658e0b5ac02SPyun YongHyeon 	stats->rx_drop += CSR_READ_4(sc, ET_STAT_RX_DROP);
2659e0b5ac02SPyun YongHyeon 
2660e0b5ac02SPyun YongHyeon 	stats->tx_bytes += CSR_READ_4(sc, ET_STAT_TX_BYTES);
2661e0b5ac02SPyun YongHyeon 	stats->tx_frames += CSR_READ_4(sc, ET_STAT_TX_FRAMES);
2662e0b5ac02SPyun YongHyeon 	stats->tx_mcast += CSR_READ_4(sc, ET_STAT_TX_MCAST);
2663e0b5ac02SPyun YongHyeon 	stats->tx_bcast += CSR_READ_4(sc, ET_STAT_TX_BCAST);
2664e0b5ac02SPyun YongHyeon 	stats->tx_pause += CSR_READ_4(sc, ET_STAT_TX_PAUSE);
2665e0b5ac02SPyun YongHyeon 	stats->tx_deferred += CSR_READ_4(sc, ET_STAT_TX_DEFER);
2666e0b5ac02SPyun YongHyeon 	stats->tx_excess_deferred += CSR_READ_4(sc, ET_STAT_TX_EXCESS_DEFER);
2667e0b5ac02SPyun YongHyeon 	stats->tx_single_colls += CSR_READ_4(sc, ET_STAT_TX_SINGLE_COL);
2668e0b5ac02SPyun YongHyeon 	stats->tx_multi_colls += CSR_READ_4(sc, ET_STAT_TX_MULTI_COL);
2669e0b5ac02SPyun YongHyeon 	stats->tx_late_colls += CSR_READ_4(sc, ET_STAT_TX_LATE_COL);
2670e0b5ac02SPyun YongHyeon 	stats->tx_excess_colls += CSR_READ_4(sc, ET_STAT_TX_EXCESS_COL);
2671e0b5ac02SPyun YongHyeon 	stats->tx_total_colls += CSR_READ_4(sc, ET_STAT_TX_TOTAL_COL);
2672e0b5ac02SPyun YongHyeon 	stats->tx_pause_honored += CSR_READ_4(sc, ET_STAT_TX_PAUSE_HONOR);
2673e0b5ac02SPyun YongHyeon 	stats->tx_drop += CSR_READ_4(sc, ET_STAT_TX_DROP);
2674e0b5ac02SPyun YongHyeon 	stats->tx_jabbers += CSR_READ_4(sc, ET_STAT_TX_JABBER);
2675e0b5ac02SPyun YongHyeon 	stats->tx_crcerrs += CSR_READ_4(sc, ET_STAT_TX_CRC_ERR);
2676e0b5ac02SPyun YongHyeon 	stats->tx_control += CSR_READ_4(sc, ET_STAT_TX_CTL);
2677e0b5ac02SPyun YongHyeon 	stats->tx_oversize += CSR_READ_4(sc, ET_STAT_TX_OVERSIZE);
2678e0b5ac02SPyun YongHyeon 	stats->tx_undersize += CSR_READ_4(sc, ET_STAT_TX_UNDERSIZE);
2679e0b5ac02SPyun YongHyeon 	stats->tx_fragments += CSR_READ_4(sc, ET_STAT_TX_FRAG);
2680c13dc687SGleb Smirnoff }
2681e0b5ac02SPyun YongHyeon 
2682c13dc687SGleb Smirnoff static uint64_t
2683c13dc687SGleb Smirnoff et_get_counter(struct ifnet *ifp, ift_counter cnt)
2684c13dc687SGleb Smirnoff {
2685c13dc687SGleb Smirnoff 	struct et_softc *sc;
2686c13dc687SGleb Smirnoff 	struct et_hw_stats *stats;
2687c13dc687SGleb Smirnoff 
2688c13dc687SGleb Smirnoff 	sc = if_getsoftc(ifp);
2689c13dc687SGleb Smirnoff 	stats = &sc->sc_stats;
2690c13dc687SGleb Smirnoff 
2691c13dc687SGleb Smirnoff 	switch (cnt) {
2692c13dc687SGleb Smirnoff 	case IFCOUNTER_OPACKETS:
2693c13dc687SGleb Smirnoff 		return (stats->tx_frames);
2694c13dc687SGleb Smirnoff 	case IFCOUNTER_COLLISIONS:
2695c13dc687SGleb Smirnoff 		return (stats->tx_total_colls);
2696c13dc687SGleb Smirnoff 	case IFCOUNTER_OERRORS:
2697c13dc687SGleb Smirnoff 		return (stats->tx_drop + stats->tx_jabbers +
2698e0b5ac02SPyun YongHyeon 		    stats->tx_crcerrs + stats->tx_excess_deferred +
2699c13dc687SGleb Smirnoff 		    stats->tx_late_colls);
2700c13dc687SGleb Smirnoff 	case IFCOUNTER_IPACKETS:
2701c13dc687SGleb Smirnoff 		return (stats->rx_frames);
2702c13dc687SGleb Smirnoff 	case IFCOUNTER_IERRORS:
2703c13dc687SGleb Smirnoff 		return (stats->rx_crcerrs + stats->rx_alignerrs +
2704e0b5ac02SPyun YongHyeon 		    stats->rx_lenerrs + stats->rx_codeerrs + stats->rx_cserrs +
2705c13dc687SGleb Smirnoff 		    stats->rx_runts + stats->rx_jabbers + stats->rx_drop);
2706c13dc687SGleb Smirnoff 	default:
2707c13dc687SGleb Smirnoff 		return (if_get_counter_default(ifp, cnt));
2708c13dc687SGleb Smirnoff 	}
2709e0b5ac02SPyun YongHyeon }
2710e0b5ac02SPyun YongHyeon 
27110442028aSPyun YongHyeon static int
27120442028aSPyun YongHyeon et_suspend(device_t dev)
27130442028aSPyun YongHyeon {
27140442028aSPyun YongHyeon 	struct et_softc *sc;
271538953bb0SPyun YongHyeon 	uint32_t pmcfg;
27160442028aSPyun YongHyeon 
27170442028aSPyun YongHyeon 	sc = device_get_softc(dev);
27180442028aSPyun YongHyeon 	ET_LOCK(sc);
27190442028aSPyun YongHyeon 	if ((sc->ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
27200442028aSPyun YongHyeon 		et_stop(sc);
272138953bb0SPyun YongHyeon 	/* Diable all clocks and put PHY into COMA. */
272238953bb0SPyun YongHyeon 	pmcfg = CSR_READ_4(sc, ET_PM);
272338953bb0SPyun YongHyeon 	pmcfg &= ~(EM_PM_GIGEPHY_ENB | ET_PM_SYSCLK_GATE | ET_PM_TXCLK_GATE |
272438953bb0SPyun YongHyeon 	    ET_PM_RXCLK_GATE);
272538953bb0SPyun YongHyeon 	pmcfg |= ET_PM_PHY_SW_COMA;
272638953bb0SPyun YongHyeon 	CSR_WRITE_4(sc, ET_PM, pmcfg);
27270442028aSPyun YongHyeon 	ET_UNLOCK(sc);
27280442028aSPyun YongHyeon 	return (0);
27290442028aSPyun YongHyeon }
27300442028aSPyun YongHyeon 
27310442028aSPyun YongHyeon static int
27320442028aSPyun YongHyeon et_resume(device_t dev)
27330442028aSPyun YongHyeon {
27340442028aSPyun YongHyeon 	struct et_softc *sc;
273538953bb0SPyun YongHyeon 	uint32_t pmcfg;
27360442028aSPyun YongHyeon 
27370442028aSPyun YongHyeon 	sc = device_get_softc(dev);
27380442028aSPyun YongHyeon 	ET_LOCK(sc);
273938953bb0SPyun YongHyeon 	/* Take PHY out of COMA and enable clocks. */
274038953bb0SPyun YongHyeon 	pmcfg = ET_PM_SYSCLK_GATE | ET_PM_TXCLK_GATE | ET_PM_RXCLK_GATE;
274138953bb0SPyun YongHyeon 	if ((sc->sc_flags & ET_FLAG_FASTETHER) == 0)
274238953bb0SPyun YongHyeon 		pmcfg |= EM_PM_GIGEPHY_ENB;
274338953bb0SPyun YongHyeon 	CSR_WRITE_4(sc, ET_PM, pmcfg);
27440442028aSPyun YongHyeon 	if ((sc->ifp->if_flags & IFF_UP) != 0)
27450442028aSPyun YongHyeon 		et_init_locked(sc);
27460442028aSPyun YongHyeon 	ET_UNLOCK(sc);
27470442028aSPyun YongHyeon 	return (0);
27480442028aSPyun YongHyeon }
2749