xref: /freebsd/sys/dev/et/if_et.c (revision 5d384a0de9e590036647cbf0e7db4f4404135657)
14d52a575SXin LI /*-
2e5fdd9deSXin LI  * Copyright (c) 2007 Sepherosa Ziehau.  All rights reserved.
34d52a575SXin LI  *
44d52a575SXin LI  * This code is derived from software contributed to The DragonFly Project
54d52a575SXin LI  * by Sepherosa Ziehau <sepherosa@gmail.com>
64d52a575SXin LI  *
74d52a575SXin LI  * Redistribution and use in source and binary forms, with or without
84d52a575SXin LI  * modification, are permitted provided that the following conditions
94d52a575SXin LI  * are met:
104d52a575SXin LI  *
114d52a575SXin LI  * 1. Redistributions of source code must retain the above copyright
124d52a575SXin LI  *    notice, this list of conditions and the following disclaimer.
134d52a575SXin LI  * 2. Redistributions in binary form must reproduce the above copyright
144d52a575SXin LI  *    notice, this list of conditions and the following disclaimer in
154d52a575SXin LI  *    the documentation and/or other materials provided with the
164d52a575SXin LI  *    distribution.
174d52a575SXin LI  * 3. Neither the name of The DragonFly Project nor the names of its
184d52a575SXin LI  *    contributors may be used to endorse or promote products derived
194d52a575SXin LI  *    from this software without specific, prior written permission.
204d52a575SXin LI  *
214d52a575SXin LI  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
224d52a575SXin LI  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
234d52a575SXin LI  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
244d52a575SXin LI  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
254d52a575SXin LI  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
264d52a575SXin LI  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
274d52a575SXin LI  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
284d52a575SXin LI  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
294d52a575SXin LI  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
304d52a575SXin LI  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
314d52a575SXin LI  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
324d52a575SXin LI  * SUCH DAMAGE.
334d52a575SXin LI  *
344d52a575SXin LI  * $DragonFly: src/sys/dev/netif/et/if_et.c,v 1.10 2008/05/18 07:47:14 sephe Exp $
354d52a575SXin LI  */
364d52a575SXin LI 
37fe42b04dSPyun YongHyeon #include <sys/cdefs.h>
38fe42b04dSPyun YongHyeon __FBSDID("$FreeBSD$");
39fe42b04dSPyun YongHyeon 
404d52a575SXin LI #include <sys/param.h>
414d52a575SXin LI #include <sys/systm.h>
424d52a575SXin LI #include <sys/endian.h>
434d52a575SXin LI #include <sys/kernel.h>
444d52a575SXin LI #include <sys/bus.h>
454d52a575SXin LI #include <sys/malloc.h>
464d52a575SXin LI #include <sys/mbuf.h>
474d52a575SXin LI #include <sys/proc.h>
484d52a575SXin LI #include <sys/rman.h>
494d52a575SXin LI #include <sys/module.h>
504d52a575SXin LI #include <sys/socket.h>
514d52a575SXin LI #include <sys/sockio.h>
524d52a575SXin LI #include <sys/sysctl.h>
534d52a575SXin LI 
544d52a575SXin LI #include <net/ethernet.h>
554d52a575SXin LI #include <net/if.h>
564d52a575SXin LI #include <net/if_dl.h>
574d52a575SXin LI #include <net/if_types.h>
584d52a575SXin LI #include <net/bpf.h>
594d52a575SXin LI #include <net/if_arp.h>
604d52a575SXin LI #include <net/if_media.h>
614d52a575SXin LI #include <net/if_vlan_var.h>
624d52a575SXin LI 
634d52a575SXin LI #include <machine/bus.h>
644d52a575SXin LI 
65d6c65d27SMarius Strobl #include <dev/mii/mii.h>
664d52a575SXin LI #include <dev/mii/miivar.h>
674d52a575SXin LI 
684d52a575SXin LI #include <dev/pci/pcireg.h>
694d52a575SXin LI #include <dev/pci/pcivar.h>
704d52a575SXin LI 
714d52a575SXin LI #include <dev/et/if_etreg.h>
724d52a575SXin LI #include <dev/et/if_etvar.h>
734d52a575SXin LI 
744d52a575SXin LI #include "miibus_if.h"
754d52a575SXin LI 
764d52a575SXin LI MODULE_DEPEND(et, pci, 1, 1, 1);
774d52a575SXin LI MODULE_DEPEND(et, ether, 1, 1, 1);
784d52a575SXin LI MODULE_DEPEND(et, miibus, 1, 1, 1);
794d52a575SXin LI 
80cc3c3b4eSPyun YongHyeon /* Tunables. */
81cc3c3b4eSPyun YongHyeon static int msi_disable = 0;
82accb4fcdSPyun YongHyeon TUNABLE_INT("hw.et.msi_disable", &msi_disable);
83cc3c3b4eSPyun YongHyeon 
849955274cSPyun YongHyeon #define	ET_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP)
859955274cSPyun YongHyeon 
864d52a575SXin LI static int	et_probe(device_t);
874d52a575SXin LI static int	et_attach(device_t);
884d52a575SXin LI static int	et_detach(device_t);
894d52a575SXin LI static int	et_shutdown(device_t);
900442028aSPyun YongHyeon static int	et_suspend(device_t);
910442028aSPyun YongHyeon static int	et_resume(device_t);
924d52a575SXin LI 
934d52a575SXin LI static int	et_miibus_readreg(device_t, int, int);
944d52a575SXin LI static int	et_miibus_writereg(device_t, int, int, int);
954d52a575SXin LI static void	et_miibus_statchg(device_t);
964d52a575SXin LI 
974d52a575SXin LI static void	et_init_locked(struct et_softc *);
984d52a575SXin LI static void	et_init(void *);
994d52a575SXin LI static int	et_ioctl(struct ifnet *, u_long, caddr_t);
1004d52a575SXin LI static void	et_start_locked(struct ifnet *);
1014d52a575SXin LI static void	et_start(struct ifnet *);
10205884511SPyun YongHyeon static int	et_watchdog(struct et_softc *);
1034d52a575SXin LI static int	et_ifmedia_upd_locked(struct ifnet *);
1044d52a575SXin LI static int	et_ifmedia_upd(struct ifnet *);
1054d52a575SXin LI static void	et_ifmedia_sts(struct ifnet *, struct ifmediareq *);
1064d52a575SXin LI 
1074d52a575SXin LI static void	et_add_sysctls(struct et_softc *);
1084d52a575SXin LI static int	et_sysctl_rx_intr_npkts(SYSCTL_HANDLER_ARGS);
1094d52a575SXin LI static int	et_sysctl_rx_intr_delay(SYSCTL_HANDLER_ARGS);
1104d52a575SXin LI 
1114d52a575SXin LI static void	et_intr(void *);
1124d52a575SXin LI static void	et_rxeof(struct et_softc *);
1134d52a575SXin LI static void	et_txeof(struct et_softc *);
1144d52a575SXin LI 
11505884511SPyun YongHyeon static int	et_dma_alloc(struct et_softc *);
11605884511SPyun YongHyeon static void	et_dma_free(struct et_softc *);
11705884511SPyun YongHyeon static void	et_dma_map_addr(void *, bus_dma_segment_t *, int, int);
11805884511SPyun YongHyeon static int	et_dma_ring_alloc(struct et_softc *, bus_size_t, bus_size_t,
11905884511SPyun YongHyeon 		    bus_dma_tag_t *, uint8_t **, bus_dmamap_t *, bus_addr_t *,
12005884511SPyun YongHyeon 		    const char *);
12105884511SPyun YongHyeon static void	et_dma_ring_free(struct et_softc *, bus_dma_tag_t *, uint8_t **,
12205884511SPyun YongHyeon 		    bus_dmamap_t *);
12305884511SPyun YongHyeon static void	et_init_tx_ring(struct et_softc *);
1244d52a575SXin LI static int	et_init_rx_ring(struct et_softc *);
1254d52a575SXin LI static void	et_free_tx_ring(struct et_softc *);
1264d52a575SXin LI static void	et_free_rx_ring(struct et_softc *);
1274d52a575SXin LI static int	et_encap(struct et_softc *, struct mbuf **);
12805884511SPyun YongHyeon static int	et_newbuf_cluster(struct et_rxbuf_data *, int);
12905884511SPyun YongHyeon static int	et_newbuf_hdr(struct et_rxbuf_data *, int);
13005884511SPyun YongHyeon static void	et_rxbuf_discard(struct et_rxbuf_data *, int);
1314d52a575SXin LI 
1324d52a575SXin LI static void	et_stop(struct et_softc *);
1334d52a575SXin LI static int	et_chip_init(struct et_softc *);
1344d52a575SXin LI static void	et_chip_attach(struct et_softc *);
1354d52a575SXin LI static void	et_init_mac(struct et_softc *);
1364d52a575SXin LI static void	et_init_rxmac(struct et_softc *);
1374d52a575SXin LI static void	et_init_txmac(struct et_softc *);
1384d52a575SXin LI static int	et_init_rxdma(struct et_softc *);
1394d52a575SXin LI static int	et_init_txdma(struct et_softc *);
1404d52a575SXin LI static int	et_start_rxdma(struct et_softc *);
1414d52a575SXin LI static int	et_start_txdma(struct et_softc *);
1424d52a575SXin LI static int	et_stop_rxdma(struct et_softc *);
1434d52a575SXin LI static int	et_stop_txdma(struct et_softc *);
1444d52a575SXin LI static void	et_reset(struct et_softc *);
1458b3c6496SPyun YongHyeon static int	et_bus_config(struct et_softc *);
1464d52a575SXin LI static void	et_get_eaddr(device_t, uint8_t[]);
1474d52a575SXin LI static void	et_setmulti(struct et_softc *);
1484d52a575SXin LI static void	et_tick(void *);
149e0b5ac02SPyun YongHyeon static void	et_stats_update(struct et_softc *);
1504d52a575SXin LI 
1514d52a575SXin LI static const struct et_dev {
1524d52a575SXin LI 	uint16_t	vid;
1534d52a575SXin LI 	uint16_t	did;
1544d52a575SXin LI 	const char	*desc;
1554d52a575SXin LI } et_devices[] = {
1564d52a575SXin LI 	{ PCI_VENDOR_LUCENT, PCI_PRODUCT_LUCENT_ET1310,
1574d52a575SXin LI 	  "Agere ET1310 Gigabit Ethernet" },
1584d52a575SXin LI 	{ PCI_VENDOR_LUCENT, PCI_PRODUCT_LUCENT_ET1310_FAST,
1594d52a575SXin LI 	  "Agere ET1310 Fast Ethernet" },
1604d52a575SXin LI 	{ 0, 0, NULL }
1614d52a575SXin LI };
1624d52a575SXin LI 
1634d52a575SXin LI static device_method_t et_methods[] = {
1644d52a575SXin LI 	DEVMETHOD(device_probe,		et_probe),
1654d52a575SXin LI 	DEVMETHOD(device_attach,	et_attach),
1664d52a575SXin LI 	DEVMETHOD(device_detach,	et_detach),
1674d52a575SXin LI 	DEVMETHOD(device_shutdown,	et_shutdown),
1680442028aSPyun YongHyeon 	DEVMETHOD(device_suspend,	et_suspend),
1690442028aSPyun YongHyeon 	DEVMETHOD(device_resume,	et_resume),
1704d52a575SXin LI 
1714d52a575SXin LI 	DEVMETHOD(miibus_readreg,	et_miibus_readreg),
1724d52a575SXin LI 	DEVMETHOD(miibus_writereg,	et_miibus_writereg),
1734d52a575SXin LI 	DEVMETHOD(miibus_statchg,	et_miibus_statchg),
1744d52a575SXin LI 
1754b7ec270SMarius Strobl 	DEVMETHOD_END
1764d52a575SXin LI };
1774d52a575SXin LI 
1784d52a575SXin LI static driver_t et_driver = {
1794d52a575SXin LI 	"et",
1804d52a575SXin LI 	et_methods,
1814d52a575SXin LI 	sizeof(struct et_softc)
1824d52a575SXin LI };
1834d52a575SXin LI 
1844d52a575SXin LI static devclass_t et_devclass;
1854d52a575SXin LI 
1864d52a575SXin LI DRIVER_MODULE(et, pci, et_driver, et_devclass, 0, 0);
1874d52a575SXin LI DRIVER_MODULE(miibus, et, miibus_driver, miibus_devclass, 0, 0);
1884d52a575SXin LI 
1894d52a575SXin LI static int	et_rx_intr_npkts = 32;
1904d52a575SXin LI static int	et_rx_intr_delay = 20;		/* x10 usec */
1914d52a575SXin LI static int	et_tx_intr_nsegs = 126;
1924d52a575SXin LI static uint32_t	et_timer = 1000 * 1000 * 1000;	/* nanosec */
1934d52a575SXin LI 
1944d52a575SXin LI TUNABLE_INT("hw.et.timer", &et_timer);
1954d52a575SXin LI TUNABLE_INT("hw.et.rx_intr_npkts", &et_rx_intr_npkts);
1964d52a575SXin LI TUNABLE_INT("hw.et.rx_intr_delay", &et_rx_intr_delay);
1974d52a575SXin LI TUNABLE_INT("hw.et.tx_intr_nsegs", &et_tx_intr_nsegs);
1984d52a575SXin LI 
1994d52a575SXin LI static int
2004d52a575SXin LI et_probe(device_t dev)
2014d52a575SXin LI {
2024d52a575SXin LI 	const struct et_dev *d;
2034d52a575SXin LI 	uint16_t did, vid;
2044d52a575SXin LI 
2054d52a575SXin LI 	vid = pci_get_vendor(dev);
2064d52a575SXin LI 	did = pci_get_device(dev);
2074d52a575SXin LI 
2084d52a575SXin LI 	for (d = et_devices; d->desc != NULL; ++d) {
2094d52a575SXin LI 		if (vid == d->vid && did == d->did) {
2104d52a575SXin LI 			device_set_desc(dev, d->desc);
211a64788d1SPyun YongHyeon 			return (BUS_PROBE_DEFAULT);
2124d52a575SXin LI 		}
2134d52a575SXin LI 	}
214398f1b65SPyun YongHyeon 	return (ENXIO);
2154d52a575SXin LI }
2164d52a575SXin LI 
2174d52a575SXin LI static int
2184d52a575SXin LI et_attach(device_t dev)
2194d52a575SXin LI {
2204d52a575SXin LI 	struct et_softc *sc;
2214d52a575SXin LI 	struct ifnet *ifp;
2224d52a575SXin LI 	uint8_t eaddr[ETHER_ADDR_LEN];
22338953bb0SPyun YongHyeon 	uint32_t pmcfg;
224cc3c3b4eSPyun YongHyeon 	int cap, error, msic;
2254d52a575SXin LI 
2264d52a575SXin LI 	sc = device_get_softc(dev);
2274d52a575SXin LI 	sc->dev = dev;
2284d52a575SXin LI 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
2294d52a575SXin LI 	    MTX_DEF);
230d2f7028cSPyun YongHyeon 	callout_init_mtx(&sc->sc_tick, &sc->sc_mtx, 0);
2314d52a575SXin LI 
2324d52a575SXin LI 	ifp = sc->ifp = if_alloc(IFT_ETHER);
2334d52a575SXin LI 	if (ifp == NULL) {
2344d52a575SXin LI 		device_printf(dev, "can not if_alloc()\n");
2354d52a575SXin LI 		error = ENOSPC;
2364d52a575SXin LI 		goto fail;
2374d52a575SXin LI 	}
2384d52a575SXin LI 
2394d52a575SXin LI 	/*
2404d52a575SXin LI 	 * Initialize tunables
2414d52a575SXin LI 	 */
2424d52a575SXin LI 	sc->sc_rx_intr_npkts = et_rx_intr_npkts;
2434d52a575SXin LI 	sc->sc_rx_intr_delay = et_rx_intr_delay;
2444d52a575SXin LI 	sc->sc_tx_intr_nsegs = et_tx_intr_nsegs;
2454d52a575SXin LI 	sc->sc_timer = et_timer;
2464d52a575SXin LI 
2474d52a575SXin LI 	/* Enable bus mastering */
2484d52a575SXin LI 	pci_enable_busmaster(dev);
2494d52a575SXin LI 
2504d52a575SXin LI 	/*
2514d52a575SXin LI 	 * Allocate IO memory
2524d52a575SXin LI 	 */
25339bea5ddSPyun YongHyeon 	sc->sc_mem_rid = PCIR_BAR(0);
2544d52a575SXin LI 	sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
2554d52a575SXin LI 	    &sc->sc_mem_rid, RF_ACTIVE);
2564d52a575SXin LI 	if (sc->sc_mem_res == NULL) {
2574d52a575SXin LI 		device_printf(dev, "can't allocate IO memory\n");
258398f1b65SPyun YongHyeon 		return (ENXIO);
2594d52a575SXin LI 	}
2604d52a575SXin LI 
261cc3c3b4eSPyun YongHyeon 	msic = 0;
2623b0a4aefSJohn Baldwin 	if (pci_find_cap(dev, PCIY_EXPRESS, &cap) == 0) {
263cc3c3b4eSPyun YongHyeon 		sc->sc_expcap = cap;
264cc3c3b4eSPyun YongHyeon 		sc->sc_flags |= ET_FLAG_PCIE;
265cc3c3b4eSPyun YongHyeon 		msic = pci_msi_count(dev);
266cc3c3b4eSPyun YongHyeon 		if (bootverbose)
267cc3c3b4eSPyun YongHyeon 			device_printf(dev, "MSI count: %d\n", msic);
268cc3c3b4eSPyun YongHyeon 	}
269cc3c3b4eSPyun YongHyeon 	if (msic > 0 && msi_disable == 0) {
270cc3c3b4eSPyun YongHyeon 		msic = 1;
271cc3c3b4eSPyun YongHyeon 		if (pci_alloc_msi(dev, &msic) == 0) {
272cc3c3b4eSPyun YongHyeon 			if (msic == 1) {
273cc3c3b4eSPyun YongHyeon 				device_printf(dev, "Using %d MSI message\n",
274cc3c3b4eSPyun YongHyeon 				    msic);
275cc3c3b4eSPyun YongHyeon 				sc->sc_flags |= ET_FLAG_MSI;
276cc3c3b4eSPyun YongHyeon 			} else
277cc3c3b4eSPyun YongHyeon 				pci_release_msi(dev);
278cc3c3b4eSPyun YongHyeon 		}
279cc3c3b4eSPyun YongHyeon 	}
280cc3c3b4eSPyun YongHyeon 
2814d52a575SXin LI 	/*
2824d52a575SXin LI 	 * Allocate IRQ
2834d52a575SXin LI 	 */
284cc3c3b4eSPyun YongHyeon 	if ((sc->sc_flags & ET_FLAG_MSI) == 0) {
2854d52a575SXin LI 		sc->sc_irq_rid = 0;
2864d52a575SXin LI 		sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
287cc3c3b4eSPyun YongHyeon 		    &sc->sc_irq_rid, RF_SHAREABLE | RF_ACTIVE);
288cc3c3b4eSPyun YongHyeon 	} else {
289cc3c3b4eSPyun YongHyeon 		sc->sc_irq_rid = 1;
290cc3c3b4eSPyun YongHyeon 		sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
291cc3c3b4eSPyun YongHyeon 		    &sc->sc_irq_rid, RF_ACTIVE);
292cc3c3b4eSPyun YongHyeon 	}
2934d52a575SXin LI 	if (sc->sc_irq_res == NULL) {
2944d52a575SXin LI 		device_printf(dev, "can't allocate irq\n");
2954d52a575SXin LI 		error = ENXIO;
2964d52a575SXin LI 		goto fail;
2974d52a575SXin LI 	}
2984d52a575SXin LI 
2991f009e2fSPyun YongHyeon 	if (pci_get_device(dev) == PCI_PRODUCT_LUCENT_ET1310_FAST)
3001f009e2fSPyun YongHyeon 		sc->sc_flags |= ET_FLAG_FASTETHER;
3011f009e2fSPyun YongHyeon 
3028b3c6496SPyun YongHyeon 	error = et_bus_config(sc);
3034d52a575SXin LI 	if (error)
3044d52a575SXin LI 		goto fail;
3054d52a575SXin LI 
3064d52a575SXin LI 	et_get_eaddr(dev, eaddr);
3074d52a575SXin LI 
30838953bb0SPyun YongHyeon 	/* Take PHY out of COMA and enable clocks. */
30938953bb0SPyun YongHyeon 	pmcfg = ET_PM_SYSCLK_GATE | ET_PM_TXCLK_GATE | ET_PM_RXCLK_GATE;
31038953bb0SPyun YongHyeon 	if ((sc->sc_flags & ET_FLAG_FASTETHER) == 0)
31138953bb0SPyun YongHyeon 		pmcfg |= EM_PM_GIGEPHY_ENB;
31238953bb0SPyun YongHyeon 	CSR_WRITE_4(sc, ET_PM, pmcfg);
3134d52a575SXin LI 
3144d52a575SXin LI 	et_reset(sc);
3154d52a575SXin LI 
31605884511SPyun YongHyeon 	error = et_dma_alloc(sc);
3174d52a575SXin LI 	if (error)
3184d52a575SXin LI 		goto fail;
3194d52a575SXin LI 
3204d52a575SXin LI 	ifp->if_softc = sc;
3214d52a575SXin LI 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
3224d52a575SXin LI 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
3234d52a575SXin LI 	ifp->if_init = et_init;
3244d52a575SXin LI 	ifp->if_ioctl = et_ioctl;
3254d52a575SXin LI 	ifp->if_start = et_start;
326ed848e3aSPyun YongHyeon 	ifp->if_capabilities = IFCAP_TXCSUM | IFCAP_VLAN_MTU;
3274d52a575SXin LI 	ifp->if_capenable = ifp->if_capabilities;
328c8b727ceSPyun YongHyeon 	ifp->if_snd.ifq_drv_maxlen = ET_TX_NDESC - 1;
329c8b727ceSPyun YongHyeon 	IFQ_SET_MAXLEN(&ifp->if_snd, ET_TX_NDESC - 1);
3304d52a575SXin LI 	IFQ_SET_READY(&ifp->if_snd);
3314d52a575SXin LI 
3324d52a575SXin LI 	et_chip_attach(sc);
3334d52a575SXin LI 
334d6c65d27SMarius Strobl 	error = mii_attach(dev, &sc->sc_miibus, ifp, et_ifmedia_upd,
335*5d384a0dSPyun YongHyeon 	    et_ifmedia_sts, BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY,
336*5d384a0dSPyun YongHyeon 	    MIIF_DOPAUSE);
3374d52a575SXin LI 	if (error) {
338d6c65d27SMarius Strobl 		device_printf(dev, "attaching PHYs failed\n");
3394d52a575SXin LI 		goto fail;
3404d52a575SXin LI 	}
3414d52a575SXin LI 
3424d52a575SXin LI 	ether_ifattach(ifp, eaddr);
343d2f7028cSPyun YongHyeon 
344d2f7028cSPyun YongHyeon 	/* Tell the upper layer(s) we support long frames. */
345d2f7028cSPyun YongHyeon 	ifp->if_hdrlen = sizeof(struct ether_vlan_header);
3464d52a575SXin LI 
3474d52a575SXin LI 	error = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_NET | INTR_MPSAFE,
3484d52a575SXin LI 	    NULL, et_intr, sc, &sc->sc_irq_handle);
3494d52a575SXin LI 	if (error) {
3504d52a575SXin LI 		ether_ifdetach(ifp);
3514d52a575SXin LI 		device_printf(dev, "can't setup intr\n");
3524d52a575SXin LI 		goto fail;
3534d52a575SXin LI 	}
3544d52a575SXin LI 
3554d52a575SXin LI 	et_add_sysctls(sc);
3564d52a575SXin LI 
357398f1b65SPyun YongHyeon 	return (0);
3584d52a575SXin LI fail:
3594d52a575SXin LI 	et_detach(dev);
360398f1b65SPyun YongHyeon 	return (error);
3614d52a575SXin LI }
3624d52a575SXin LI 
3634d52a575SXin LI static int
3644d52a575SXin LI et_detach(device_t dev)
3654d52a575SXin LI {
3664d52a575SXin LI 	struct et_softc *sc = device_get_softc(dev);
3674d52a575SXin LI 
3684d52a575SXin LI 	if (device_is_attached(dev)) {
369a64788d1SPyun YongHyeon 		ether_ifdetach(sc->ifp);
3704d52a575SXin LI 		ET_LOCK(sc);
3714d52a575SXin LI 		et_stop(sc);
3724d52a575SXin LI 		ET_UNLOCK(sc);
373a64788d1SPyun YongHyeon 		callout_drain(&sc->sc_tick);
3744d52a575SXin LI 	}
3754d52a575SXin LI 
3764d52a575SXin LI 	if (sc->sc_miibus != NULL)
3774d52a575SXin LI 		device_delete_child(dev, sc->sc_miibus);
3784d52a575SXin LI 	bus_generic_detach(dev);
3794d52a575SXin LI 
380a64788d1SPyun YongHyeon 	if (sc->sc_irq_handle != NULL)
381a64788d1SPyun YongHyeon 		bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_irq_handle);
382a64788d1SPyun YongHyeon 	if (sc->sc_irq_res != NULL)
383a64788d1SPyun YongHyeon 		bus_release_resource(dev, SYS_RES_IRQ,
384a64788d1SPyun YongHyeon 		    rman_get_rid(sc->sc_irq_res), sc->sc_irq_res);
385cc3c3b4eSPyun YongHyeon 	if ((sc->sc_flags & ET_FLAG_MSI) != 0)
386cc3c3b4eSPyun YongHyeon 		pci_release_msi(dev);
387a64788d1SPyun YongHyeon 	if (sc->sc_mem_res != NULL)
388a64788d1SPyun YongHyeon 		bus_release_resource(dev, SYS_RES_MEMORY,
389a64788d1SPyun YongHyeon 		    rman_get_rid(sc->sc_mem_res), sc->sc_mem_res);
3904d52a575SXin LI 
3914d52a575SXin LI 	if (sc->ifp != NULL)
3924d52a575SXin LI 		if_free(sc->ifp);
3934d52a575SXin LI 
39405884511SPyun YongHyeon 	et_dma_free(sc);
3955b8f4900SPyun YongHyeon 
3965b8f4900SPyun YongHyeon 	mtx_destroy(&sc->sc_mtx);
3974d52a575SXin LI 
398398f1b65SPyun YongHyeon 	return (0);
3994d52a575SXin LI }
4004d52a575SXin LI 
4014d52a575SXin LI static int
4024d52a575SXin LI et_shutdown(device_t dev)
4034d52a575SXin LI {
4044d52a575SXin LI 	struct et_softc *sc = device_get_softc(dev);
4054d52a575SXin LI 
4064d52a575SXin LI 	ET_LOCK(sc);
4074d52a575SXin LI 	et_stop(sc);
4084d52a575SXin LI 	ET_UNLOCK(sc);
409398f1b65SPyun YongHyeon 	return (0);
4104d52a575SXin LI }
4114d52a575SXin LI 
4124d52a575SXin LI static int
4134d52a575SXin LI et_miibus_readreg(device_t dev, int phy, int reg)
4144d52a575SXin LI {
4154d52a575SXin LI 	struct et_softc *sc = device_get_softc(dev);
4164d52a575SXin LI 	uint32_t val;
4174d52a575SXin LI 	int i, ret;
4184d52a575SXin LI 
4194d52a575SXin LI 	/* Stop any pending operations */
4204d52a575SXin LI 	CSR_WRITE_4(sc, ET_MII_CMD, 0);
4214d52a575SXin LI 
42223263665SPyun YongHyeon 	val = (phy << ET_MII_ADDR_PHY_SHIFT) & ET_MII_ADDR_PHY_MASK;
42323263665SPyun YongHyeon 	val |= (reg << ET_MII_ADDR_REG_SHIFT) & ET_MII_ADDR_REG_MASK;
4244d52a575SXin LI 	CSR_WRITE_4(sc, ET_MII_ADDR, val);
4254d52a575SXin LI 
4264d52a575SXin LI 	/* Start reading */
4274d52a575SXin LI 	CSR_WRITE_4(sc, ET_MII_CMD, ET_MII_CMD_READ);
4284d52a575SXin LI 
4294d52a575SXin LI #define NRETRY	50
4304d52a575SXin LI 
4314d52a575SXin LI 	for (i = 0; i < NRETRY; ++i) {
4324d52a575SXin LI 		val = CSR_READ_4(sc, ET_MII_IND);
4334d52a575SXin LI 		if ((val & (ET_MII_IND_BUSY | ET_MII_IND_INVALID)) == 0)
4344d52a575SXin LI 			break;
4354d52a575SXin LI 		DELAY(50);
4364d52a575SXin LI 	}
4374d52a575SXin LI 	if (i == NRETRY) {
4384d52a575SXin LI 		if_printf(sc->ifp,
4394d52a575SXin LI 			  "read phy %d, reg %d timed out\n", phy, reg);
4404d52a575SXin LI 		ret = 0;
4414d52a575SXin LI 		goto back;
4424d52a575SXin LI 	}
4434d52a575SXin LI 
4444d52a575SXin LI #undef NRETRY
4454d52a575SXin LI 
4464d52a575SXin LI 	val = CSR_READ_4(sc, ET_MII_STAT);
44723263665SPyun YongHyeon 	ret = val & ET_MII_STAT_VALUE_MASK;
4484d52a575SXin LI 
4494d52a575SXin LI back:
4504d52a575SXin LI 	/* Make sure that the current operation is stopped */
4514d52a575SXin LI 	CSR_WRITE_4(sc, ET_MII_CMD, 0);
452398f1b65SPyun YongHyeon 	return (ret);
4534d52a575SXin LI }
4544d52a575SXin LI 
4554d52a575SXin LI static int
4564d52a575SXin LI et_miibus_writereg(device_t dev, int phy, int reg, int val0)
4574d52a575SXin LI {
4584d52a575SXin LI 	struct et_softc *sc = device_get_softc(dev);
4594d52a575SXin LI 	uint32_t val;
4604d52a575SXin LI 	int i;
4614d52a575SXin LI 
4624d52a575SXin LI 	/* Stop any pending operations */
4634d52a575SXin LI 	CSR_WRITE_4(sc, ET_MII_CMD, 0);
4644d52a575SXin LI 
46523263665SPyun YongHyeon 	val = (phy << ET_MII_ADDR_PHY_SHIFT) & ET_MII_ADDR_PHY_MASK;
46623263665SPyun YongHyeon 	val |= (reg << ET_MII_ADDR_REG_SHIFT) & ET_MII_ADDR_REG_MASK;
4674d52a575SXin LI 	CSR_WRITE_4(sc, ET_MII_ADDR, val);
4684d52a575SXin LI 
4694d52a575SXin LI 	/* Start writing */
47023263665SPyun YongHyeon 	CSR_WRITE_4(sc, ET_MII_CTRL,
47123263665SPyun YongHyeon 	    (val0 << ET_MII_CTRL_VALUE_SHIFT) & ET_MII_CTRL_VALUE_MASK);
4724d52a575SXin LI 
4734d52a575SXin LI #define NRETRY 100
4744d52a575SXin LI 
4754d52a575SXin LI 	for (i = 0; i < NRETRY; ++i) {
4764d52a575SXin LI 		val = CSR_READ_4(sc, ET_MII_IND);
4774d52a575SXin LI 		if ((val & ET_MII_IND_BUSY) == 0)
4784d52a575SXin LI 			break;
4794d52a575SXin LI 		DELAY(50);
4804d52a575SXin LI 	}
4814d52a575SXin LI 	if (i == NRETRY) {
4824d52a575SXin LI 		if_printf(sc->ifp,
4834d52a575SXin LI 			  "write phy %d, reg %d timed out\n", phy, reg);
4844d52a575SXin LI 		et_miibus_readreg(dev, phy, reg);
4854d52a575SXin LI 	}
4864d52a575SXin LI 
4874d52a575SXin LI #undef NRETRY
4884d52a575SXin LI 
4894d52a575SXin LI 	/* Make sure that the current operation is stopped */
4904d52a575SXin LI 	CSR_WRITE_4(sc, ET_MII_CMD, 0);
491398f1b65SPyun YongHyeon 	return (0);
4924d52a575SXin LI }
4934d52a575SXin LI 
4944d52a575SXin LI static void
4954d52a575SXin LI et_miibus_statchg(device_t dev)
4964d52a575SXin LI {
4971f009e2fSPyun YongHyeon 	struct et_softc *sc;
4981f009e2fSPyun YongHyeon 	struct mii_data *mii;
4991f009e2fSPyun YongHyeon 	struct ifnet *ifp;
5001f009e2fSPyun YongHyeon 	uint32_t cfg1, cfg2, ctrl;
5011f009e2fSPyun YongHyeon 	int i;
5021f009e2fSPyun YongHyeon 
5031f009e2fSPyun YongHyeon 	sc = device_get_softc(dev);
5041f009e2fSPyun YongHyeon 
5051f009e2fSPyun YongHyeon 	mii = device_get_softc(sc->sc_miibus);
5061f009e2fSPyun YongHyeon 	ifp = sc->ifp;
5071f009e2fSPyun YongHyeon 	if (mii == NULL || ifp == NULL ||
5081f009e2fSPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
5091f009e2fSPyun YongHyeon 		return;
5101f009e2fSPyun YongHyeon 
5111f009e2fSPyun YongHyeon 	sc->sc_flags &= ~ET_FLAG_LINK;
5121f009e2fSPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
5131f009e2fSPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
5141f009e2fSPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
5151f009e2fSPyun YongHyeon 		case IFM_10_T:
5161f009e2fSPyun YongHyeon 		case IFM_100_TX:
5171f009e2fSPyun YongHyeon 			sc->sc_flags |= ET_FLAG_LINK;
5181f009e2fSPyun YongHyeon 			break;
5191f009e2fSPyun YongHyeon 		case IFM_1000_T:
5201f009e2fSPyun YongHyeon 			if ((sc->sc_flags & ET_FLAG_FASTETHER) == 0)
5211f009e2fSPyun YongHyeon 				sc->sc_flags |= ET_FLAG_LINK;
5221f009e2fSPyun YongHyeon 			break;
5231f009e2fSPyun YongHyeon 		}
5241f009e2fSPyun YongHyeon 	}
5251f009e2fSPyun YongHyeon 
5261f009e2fSPyun YongHyeon 	/* XXX Stop TX/RX MAC? */
5271f009e2fSPyun YongHyeon 	if ((sc->sc_flags & ET_FLAG_LINK) == 0)
5281f009e2fSPyun YongHyeon 		return;
5291f009e2fSPyun YongHyeon 
5301f009e2fSPyun YongHyeon 	/* Program MACs with resolved speed/duplex/flow-control. */
5311f009e2fSPyun YongHyeon 	ctrl = CSR_READ_4(sc, ET_MAC_CTRL);
5321f009e2fSPyun YongHyeon 	ctrl &= ~(ET_MAC_CTRL_GHDX | ET_MAC_CTRL_MODE_MII);
5331f009e2fSPyun YongHyeon 	cfg1 = CSR_READ_4(sc, ET_MAC_CFG1);
5341f009e2fSPyun YongHyeon 	cfg1 &= ~(ET_MAC_CFG1_TXFLOW | ET_MAC_CFG1_RXFLOW |
5351f009e2fSPyun YongHyeon 	    ET_MAC_CFG1_LOOPBACK);
5361f009e2fSPyun YongHyeon 	cfg2 = CSR_READ_4(sc, ET_MAC_CFG2);
5371f009e2fSPyun YongHyeon 	cfg2 &= ~(ET_MAC_CFG2_MODE_MII | ET_MAC_CFG2_MODE_GMII |
5381f009e2fSPyun YongHyeon 	    ET_MAC_CFG2_FDX | ET_MAC_CFG2_BIGFRM);
5391f009e2fSPyun YongHyeon 	cfg2 |= ET_MAC_CFG2_LENCHK | ET_MAC_CFG2_CRC | ET_MAC_CFG2_PADCRC |
5401f009e2fSPyun YongHyeon 	    ((7 << ET_MAC_CFG2_PREAMBLE_LEN_SHIFT) &
5411f009e2fSPyun YongHyeon 	    ET_MAC_CFG2_PREAMBLE_LEN_MASK);
5421f009e2fSPyun YongHyeon 
5431f009e2fSPyun YongHyeon 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T)
5441f009e2fSPyun YongHyeon 		cfg2 |= ET_MAC_CFG2_MODE_GMII;
5451f009e2fSPyun YongHyeon 	else {
5461f009e2fSPyun YongHyeon 		cfg2 |= ET_MAC_CFG2_MODE_MII;
5471f009e2fSPyun YongHyeon 		ctrl |= ET_MAC_CTRL_MODE_MII;
5481f009e2fSPyun YongHyeon 	}
5491f009e2fSPyun YongHyeon 
5501f009e2fSPyun YongHyeon 	if (IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) {
5511f009e2fSPyun YongHyeon 		cfg2 |= ET_MAC_CFG2_FDX;
552*5d384a0dSPyun YongHyeon 		/*
553*5d384a0dSPyun YongHyeon 		 * Controller lacks automatic TX pause frame
554*5d384a0dSPyun YongHyeon 		 * generation so it should be handled by driver.
555*5d384a0dSPyun YongHyeon 		 * Even though driver can send pause frame with
556*5d384a0dSPyun YongHyeon 		 * arbitrary pause time, controller does not
557*5d384a0dSPyun YongHyeon 		 * provide a way that tells how many free RX
558*5d384a0dSPyun YongHyeon 		 * buffers are available in controller.  This
559*5d384a0dSPyun YongHyeon 		 * limitation makes it hard to generate XON frame
560*5d384a0dSPyun YongHyeon 		 * in time on driver side so don't enable TX flow
561*5d384a0dSPyun YongHyeon 		 * control.
562*5d384a0dSPyun YongHyeon 		 */
5631f009e2fSPyun YongHyeon #ifdef notyet
5641f009e2fSPyun YongHyeon 		if (IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE)
5651f009e2fSPyun YongHyeon 			cfg1 |= ET_MAC_CFG1_TXFLOW;
566*5d384a0dSPyun YongHyeon #endif
5671f009e2fSPyun YongHyeon 		if (IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE)
5681f009e2fSPyun YongHyeon 			cfg1 |= ET_MAC_CFG1_RXFLOW;
5691f009e2fSPyun YongHyeon 	} else
5701f009e2fSPyun YongHyeon 		ctrl |= ET_MAC_CTRL_GHDX;
5711f009e2fSPyun YongHyeon 
5721f009e2fSPyun YongHyeon 	CSR_WRITE_4(sc, ET_MAC_CTRL, ctrl);
5731f009e2fSPyun YongHyeon 	CSR_WRITE_4(sc, ET_MAC_CFG2, cfg2);
5741f009e2fSPyun YongHyeon 	cfg1 |= ET_MAC_CFG1_TXEN | ET_MAC_CFG1_RXEN;
5751f009e2fSPyun YongHyeon 	CSR_WRITE_4(sc, ET_MAC_CFG1, cfg1);
5761f009e2fSPyun YongHyeon 
5771f009e2fSPyun YongHyeon #define NRETRY	50
5781f009e2fSPyun YongHyeon 
5791f009e2fSPyun YongHyeon 	for (i = 0; i < NRETRY; ++i) {
5801f009e2fSPyun YongHyeon 		cfg1 = CSR_READ_4(sc, ET_MAC_CFG1);
5811f009e2fSPyun YongHyeon 		if ((cfg1 & (ET_MAC_CFG1_SYNC_TXEN | ET_MAC_CFG1_SYNC_RXEN)) ==
5821f009e2fSPyun YongHyeon 		    (ET_MAC_CFG1_SYNC_TXEN | ET_MAC_CFG1_SYNC_RXEN))
5831f009e2fSPyun YongHyeon 			break;
5841f009e2fSPyun YongHyeon 		DELAY(100);
5851f009e2fSPyun YongHyeon 	}
5861f009e2fSPyun YongHyeon 	if (i == NRETRY)
5871f009e2fSPyun YongHyeon 		if_printf(ifp, "can't enable RX/TX\n");
5881f009e2fSPyun YongHyeon 	sc->sc_flags |= ET_FLAG_TXRX_ENABLED;
5891f009e2fSPyun YongHyeon 
5901f009e2fSPyun YongHyeon #undef NRETRY
5914d52a575SXin LI }
5924d52a575SXin LI 
5934d52a575SXin LI static int
5944d52a575SXin LI et_ifmedia_upd_locked(struct ifnet *ifp)
5954d52a575SXin LI {
5964d52a575SXin LI 	struct et_softc *sc = ifp->if_softc;
5974d52a575SXin LI 	struct mii_data *mii = device_get_softc(sc->sc_miibus);
5984d52a575SXin LI 	struct mii_softc *miisc;
5994d52a575SXin LI 
6004d52a575SXin LI 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
6013fcb7a53SMarius Strobl 		PHY_RESET(miisc);
60296570638SPyun YongHyeon 	return (mii_mediachg(mii));
6034d52a575SXin LI }
6044d52a575SXin LI 
6054d52a575SXin LI static int
6064d52a575SXin LI et_ifmedia_upd(struct ifnet *ifp)
6074d52a575SXin LI {
6084d52a575SXin LI 	struct et_softc *sc = ifp->if_softc;
6094d52a575SXin LI 	int res;
6104d52a575SXin LI 
6114d52a575SXin LI 	ET_LOCK(sc);
6124d52a575SXin LI 	res = et_ifmedia_upd_locked(ifp);
6134d52a575SXin LI 	ET_UNLOCK(sc);
6144d52a575SXin LI 
615398f1b65SPyun YongHyeon 	return (res);
6164d52a575SXin LI }
6174d52a575SXin LI 
6184d52a575SXin LI static void
6194d52a575SXin LI et_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
6204d52a575SXin LI {
6211f009e2fSPyun YongHyeon 	struct et_softc *sc;
6221f009e2fSPyun YongHyeon 	struct mii_data *mii;
6234d52a575SXin LI 
6241f009e2fSPyun YongHyeon 	sc = ifp->if_softc;
6250ae9f6a9SPyun YongHyeon 	ET_LOCK(sc);
6261f009e2fSPyun YongHyeon 	if ((ifp->if_flags & IFF_UP) == 0) {
6271f009e2fSPyun YongHyeon 		ET_UNLOCK(sc);
6281f009e2fSPyun YongHyeon 		return;
6291f009e2fSPyun YongHyeon 	}
6301f009e2fSPyun YongHyeon 
6311f009e2fSPyun YongHyeon 	mii = device_get_softc(sc->sc_miibus);
6324d52a575SXin LI 	mii_pollstat(mii);
6334d52a575SXin LI 	ifmr->ifm_active = mii->mii_media_active;
6344d52a575SXin LI 	ifmr->ifm_status = mii->mii_media_status;
6350ae9f6a9SPyun YongHyeon 	ET_UNLOCK(sc);
6364d52a575SXin LI }
6374d52a575SXin LI 
6384d52a575SXin LI static void
6394d52a575SXin LI et_stop(struct et_softc *sc)
6404d52a575SXin LI {
6414d52a575SXin LI 	struct ifnet *ifp = sc->ifp;
6424d52a575SXin LI 
6434d52a575SXin LI 	ET_LOCK_ASSERT(sc);
6444d52a575SXin LI 
6454d52a575SXin LI 	callout_stop(&sc->sc_tick);
6466537ffa6SPyun YongHyeon 	/* Disable interrupts. */
6476537ffa6SPyun YongHyeon 	CSR_WRITE_4(sc, ET_INTR_MASK, 0xffffffff);
6484d52a575SXin LI 
6491f009e2fSPyun YongHyeon 	CSR_WRITE_4(sc, ET_MAC_CFG1, CSR_READ_4(sc, ET_MAC_CFG1) & ~(
6501f009e2fSPyun YongHyeon 	    ET_MAC_CFG1_TXEN | ET_MAC_CFG1_RXEN));
6511f009e2fSPyun YongHyeon 	DELAY(100);
6521f009e2fSPyun YongHyeon 
6534d52a575SXin LI 	et_stop_rxdma(sc);
6544d52a575SXin LI 	et_stop_txdma(sc);
655e0b5ac02SPyun YongHyeon 	et_stats_update(sc);
6564d52a575SXin LI 
6574d52a575SXin LI 	et_free_tx_ring(sc);
6584d52a575SXin LI 	et_free_rx_ring(sc);
6594d52a575SXin LI 
6604d52a575SXin LI 	sc->sc_tx = 0;
6614d52a575SXin LI 	sc->sc_tx_intr = 0;
6624d52a575SXin LI 	sc->sc_flags &= ~ET_FLAG_TXRX_ENABLED;
6634d52a575SXin LI 
6644d52a575SXin LI 	sc->watchdog_timer = 0;
6654d52a575SXin LI 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
6664d52a575SXin LI }
6674d52a575SXin LI 
6684d52a575SXin LI static int
6698b3c6496SPyun YongHyeon et_bus_config(struct et_softc *sc)
6704d52a575SXin LI {
6714d52a575SXin LI 	uint32_t val, max_plsz;
6724d52a575SXin LI 	uint16_t ack_latency, replay_timer;
6734d52a575SXin LI 
6744d52a575SXin LI 	/*
6754d52a575SXin LI 	 * Test whether EEPROM is valid
6764d52a575SXin LI 	 * NOTE: Read twice to get the correct value
6774d52a575SXin LI 	 */
6788b3c6496SPyun YongHyeon 	pci_read_config(sc->dev, ET_PCIR_EEPROM_STATUS, 1);
6798b3c6496SPyun YongHyeon 	val = pci_read_config(sc->dev, ET_PCIR_EEPROM_STATUS, 1);
6804d52a575SXin LI 	if (val & ET_PCIM_EEPROM_STATUS_ERROR) {
6818b3c6496SPyun YongHyeon 		device_printf(sc->dev, "EEPROM status error 0x%02x\n", val);
682398f1b65SPyun YongHyeon 		return (ENXIO);
6834d52a575SXin LI 	}
6844d52a575SXin LI 
6854d52a575SXin LI 	/* TODO: LED */
6864d52a575SXin LI 
6878b3c6496SPyun YongHyeon 	if ((sc->sc_flags & ET_FLAG_PCIE) == 0)
6888b3c6496SPyun YongHyeon 		return (0);
6898b3c6496SPyun YongHyeon 
6904d52a575SXin LI 	/*
6914d52a575SXin LI 	 * Configure ACK latency and replay timer according to
6924d52a575SXin LI 	 * max playload size
6934d52a575SXin LI 	 */
6948b3c6496SPyun YongHyeon 	val = pci_read_config(sc->dev,
6958b3c6496SPyun YongHyeon 	    sc->sc_expcap + PCIR_EXPRESS_DEVICE_CAP, 4);
6968b3c6496SPyun YongHyeon 	max_plsz = val & PCIM_EXP_CAP_MAX_PAYLOAD;
6974d52a575SXin LI 
6984d52a575SXin LI 	switch (max_plsz) {
6994d52a575SXin LI 	case ET_PCIV_DEVICE_CAPS_PLSZ_128:
7004d52a575SXin LI 		ack_latency = ET_PCIV_ACK_LATENCY_128;
7014d52a575SXin LI 		replay_timer = ET_PCIV_REPLAY_TIMER_128;
7024d52a575SXin LI 		break;
7034d52a575SXin LI 
7044d52a575SXin LI 	case ET_PCIV_DEVICE_CAPS_PLSZ_256:
7054d52a575SXin LI 		ack_latency = ET_PCIV_ACK_LATENCY_256;
7064d52a575SXin LI 		replay_timer = ET_PCIV_REPLAY_TIMER_256;
7074d52a575SXin LI 		break;
7084d52a575SXin LI 
7094d52a575SXin LI 	default:
7108b3c6496SPyun YongHyeon 		ack_latency = pci_read_config(sc->dev, ET_PCIR_ACK_LATENCY, 2);
7118b3c6496SPyun YongHyeon 		replay_timer = pci_read_config(sc->dev,
7128b3c6496SPyun YongHyeon 		    ET_PCIR_REPLAY_TIMER, 2);
7138b3c6496SPyun YongHyeon 		device_printf(sc->dev, "ack latency %u, replay timer %u\n",
7144d52a575SXin LI 			      ack_latency, replay_timer);
7154d52a575SXin LI 		break;
7164d52a575SXin LI 	}
7174d52a575SXin LI 	if (ack_latency != 0) {
7188b3c6496SPyun YongHyeon 		pci_write_config(sc->dev, ET_PCIR_ACK_LATENCY, ack_latency, 2);
7198b3c6496SPyun YongHyeon 		pci_write_config(sc->dev, ET_PCIR_REPLAY_TIMER, replay_timer,
7208b3c6496SPyun YongHyeon 		    2);
7214d52a575SXin LI 	}
7224d52a575SXin LI 
7234d52a575SXin LI 	/*
7244d52a575SXin LI 	 * Set L0s and L1 latency timer to 2us
7254d52a575SXin LI 	 */
7268b3c6496SPyun YongHyeon 	val = pci_read_config(sc->dev, ET_PCIR_L0S_L1_LATENCY, 4);
72723263665SPyun YongHyeon 	val &= ~(PCIM_LINK_CAP_L0S_EXIT | PCIM_LINK_CAP_L1_EXIT);
72823263665SPyun YongHyeon 	/* L0s exit latency : 2us */
72923263665SPyun YongHyeon 	val |= 0x00005000;
73023263665SPyun YongHyeon 	/* L1 exit latency : 2us */
73123263665SPyun YongHyeon 	val |= 0x00028000;
7328b3c6496SPyun YongHyeon 	pci_write_config(sc->dev, ET_PCIR_L0S_L1_LATENCY, val, 4);
7334d52a575SXin LI 
7344d52a575SXin LI 	/*
7354d52a575SXin LI 	 * Set max read request size to 2048 bytes
7364d52a575SXin LI 	 */
73739bea5ddSPyun YongHyeon 	pci_set_max_read_req(sc->dev, 2048);
7384d52a575SXin LI 
739398f1b65SPyun YongHyeon 	return (0);
7404d52a575SXin LI }
7414d52a575SXin LI 
7424d52a575SXin LI static void
7434d52a575SXin LI et_get_eaddr(device_t dev, uint8_t eaddr[])
7444d52a575SXin LI {
7454d52a575SXin LI 	uint32_t val;
7464d52a575SXin LI 	int i;
7474d52a575SXin LI 
7484d52a575SXin LI 	val = pci_read_config(dev, ET_PCIR_MAC_ADDR0, 4);
7494d52a575SXin LI 	for (i = 0; i < 4; ++i)
7504d52a575SXin LI 		eaddr[i] = (val >> (8 * i)) & 0xff;
7514d52a575SXin LI 
7524d52a575SXin LI 	val = pci_read_config(dev, ET_PCIR_MAC_ADDR1, 2);
7534d52a575SXin LI 	for (; i < ETHER_ADDR_LEN; ++i)
7544d52a575SXin LI 		eaddr[i] = (val >> (8 * (i - 4))) & 0xff;
7554d52a575SXin LI }
7564d52a575SXin LI 
7574d52a575SXin LI static void
7584d52a575SXin LI et_reset(struct et_softc *sc)
7594d52a575SXin LI {
7604d52a575SXin LI 	CSR_WRITE_4(sc, ET_MAC_CFG1,
7614d52a575SXin LI 		    ET_MAC_CFG1_RST_TXFUNC | ET_MAC_CFG1_RST_RXFUNC |
7624d52a575SXin LI 		    ET_MAC_CFG1_RST_TXMC | ET_MAC_CFG1_RST_RXMC |
7634d52a575SXin LI 		    ET_MAC_CFG1_SIM_RST | ET_MAC_CFG1_SOFT_RST);
7644d52a575SXin LI 
7654d52a575SXin LI 	CSR_WRITE_4(sc, ET_SWRST,
7664d52a575SXin LI 		    ET_SWRST_TXDMA | ET_SWRST_RXDMA |
7674d52a575SXin LI 		    ET_SWRST_TXMAC | ET_SWRST_RXMAC |
7684d52a575SXin LI 		    ET_SWRST_MAC | ET_SWRST_MAC_STAT | ET_SWRST_MMC);
7694d52a575SXin LI 
7704d52a575SXin LI 	CSR_WRITE_4(sc, ET_MAC_CFG1,
7714d52a575SXin LI 		    ET_MAC_CFG1_RST_TXFUNC | ET_MAC_CFG1_RST_RXFUNC |
7724d52a575SXin LI 		    ET_MAC_CFG1_RST_TXMC | ET_MAC_CFG1_RST_RXMC);
7734d52a575SXin LI 	CSR_WRITE_4(sc, ET_MAC_CFG1, 0);
7746537ffa6SPyun YongHyeon 	/* Disable interrupts. */
7754d52a575SXin LI 	CSR_WRITE_4(sc, ET_INTR_MASK, 0xffffffff);
7764d52a575SXin LI }
7774d52a575SXin LI 
77805884511SPyun YongHyeon struct et_dmamap_arg {
77905884511SPyun YongHyeon 	bus_addr_t	et_busaddr;
78005884511SPyun YongHyeon };
78105884511SPyun YongHyeon 
78205884511SPyun YongHyeon static void
78305884511SPyun YongHyeon et_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
7844d52a575SXin LI {
78505884511SPyun YongHyeon 	struct et_dmamap_arg *ctx;
78605884511SPyun YongHyeon 
78705884511SPyun YongHyeon 	if (error)
78805884511SPyun YongHyeon 		return;
78905884511SPyun YongHyeon 
79005884511SPyun YongHyeon 	KASSERT(nseg == 1, ("%s: %d segments returned!", __func__, nseg));
79105884511SPyun YongHyeon 
79205884511SPyun YongHyeon 	ctx = arg;
79305884511SPyun YongHyeon 	ctx->et_busaddr = segs->ds_addr;
79405884511SPyun YongHyeon }
79505884511SPyun YongHyeon 
79605884511SPyun YongHyeon static int
79705884511SPyun YongHyeon et_dma_ring_alloc(struct et_softc *sc, bus_size_t alignment, bus_size_t maxsize,
79805884511SPyun YongHyeon     bus_dma_tag_t *tag, uint8_t **ring, bus_dmamap_t *map, bus_addr_t *paddr,
79905884511SPyun YongHyeon     const char *msg)
80005884511SPyun YongHyeon {
80105884511SPyun YongHyeon 	struct et_dmamap_arg ctx;
80205884511SPyun YongHyeon 	int error;
80305884511SPyun YongHyeon 
80405884511SPyun YongHyeon 	error = bus_dma_tag_create(sc->sc_dtag, alignment, 0, BUS_SPACE_MAXADDR,
80505884511SPyun YongHyeon 	    BUS_SPACE_MAXADDR, NULL, NULL, maxsize, 1, maxsize, 0, NULL, NULL,
80605884511SPyun YongHyeon 	    tag);
80705884511SPyun YongHyeon 	if (error != 0) {
80805884511SPyun YongHyeon 		device_printf(sc->dev, "could not create %s dma tag\n", msg);
80905884511SPyun YongHyeon 		return (error);
81005884511SPyun YongHyeon 	}
81105884511SPyun YongHyeon 	/* Allocate DMA'able memory for ring. */
81205884511SPyun YongHyeon 	error = bus_dmamem_alloc(*tag, (void **)ring,
81305884511SPyun YongHyeon 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT, map);
81405884511SPyun YongHyeon 	if (error != 0) {
81505884511SPyun YongHyeon 		device_printf(sc->dev,
81605884511SPyun YongHyeon 		    "could not allocate DMA'able memory for %s\n", msg);
81705884511SPyun YongHyeon 		return (error);
81805884511SPyun YongHyeon 	}
81905884511SPyun YongHyeon 	/* Load the address of the ring. */
82005884511SPyun YongHyeon 	ctx.et_busaddr = 0;
82105884511SPyun YongHyeon 	error = bus_dmamap_load(*tag, *map, *ring, maxsize, et_dma_map_addr,
82205884511SPyun YongHyeon 	    &ctx, BUS_DMA_NOWAIT);
82305884511SPyun YongHyeon 	if (error != 0) {
82405884511SPyun YongHyeon 		device_printf(sc->dev,
82505884511SPyun YongHyeon 		    "could not load DMA'able memory for %s\n", msg);
82605884511SPyun YongHyeon 		return (error);
82705884511SPyun YongHyeon 	}
82805884511SPyun YongHyeon 	*paddr = ctx.et_busaddr;
82905884511SPyun YongHyeon 	return (0);
83005884511SPyun YongHyeon }
83105884511SPyun YongHyeon 
83205884511SPyun YongHyeon static void
83305884511SPyun YongHyeon et_dma_ring_free(struct et_softc *sc, bus_dma_tag_t *tag, uint8_t **ring,
83405884511SPyun YongHyeon     bus_dmamap_t *map)
83505884511SPyun YongHyeon {
83605884511SPyun YongHyeon 
83705884511SPyun YongHyeon 	if (*map != NULL)
83805884511SPyun YongHyeon 		bus_dmamap_unload(*tag, *map);
83905884511SPyun YongHyeon 	if (*map != NULL && *ring != NULL) {
84005884511SPyun YongHyeon 		bus_dmamem_free(*tag, *ring, *map);
84105884511SPyun YongHyeon 		*ring = NULL;
84205884511SPyun YongHyeon 		*map = NULL;
84305884511SPyun YongHyeon 	}
84405884511SPyun YongHyeon 	if (*tag) {
84505884511SPyun YongHyeon 		bus_dma_tag_destroy(*tag);
84605884511SPyun YongHyeon 		*tag = NULL;
84705884511SPyun YongHyeon 	}
84805884511SPyun YongHyeon }
84905884511SPyun YongHyeon 
85005884511SPyun YongHyeon static int
85105884511SPyun YongHyeon et_dma_alloc(struct et_softc *sc)
85205884511SPyun YongHyeon {
85305884511SPyun YongHyeon 	struct et_txdesc_ring *tx_ring;
85405884511SPyun YongHyeon 	struct et_rxdesc_ring *rx_ring;
85505884511SPyun YongHyeon 	struct et_rxstat_ring *rxst_ring;
85605884511SPyun YongHyeon 	struct et_rxstatus_data *rxsd;
85705884511SPyun YongHyeon 	struct et_rxbuf_data *rbd;
85805884511SPyun YongHyeon         struct et_txbuf_data *tbd;
85905884511SPyun YongHyeon 	struct et_txstatus_data *txsd;
8604d52a575SXin LI 	int i, error;
8614d52a575SXin LI 
86205884511SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 1, 0,
86305884511SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
86405884511SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0, NULL, NULL,
86505884511SPyun YongHyeon 	    &sc->sc_dtag);
86605884511SPyun YongHyeon 	if (error != 0) {
86705884511SPyun YongHyeon 		device_printf(sc->dev, "could not allocate parent dma tag\n");
868398f1b65SPyun YongHyeon 		return (error);
8694d52a575SXin LI 	}
8704d52a575SXin LI 
87105884511SPyun YongHyeon 	/* TX ring. */
87205884511SPyun YongHyeon 	tx_ring = &sc->sc_tx_ring;
87305884511SPyun YongHyeon 	error = et_dma_ring_alloc(sc, ET_RING_ALIGN, ET_TX_RING_SIZE,
87405884511SPyun YongHyeon 	    &tx_ring->tr_dtag, (uint8_t **)&tx_ring->tr_desc, &tx_ring->tr_dmap,
87505884511SPyun YongHyeon 	    &tx_ring->tr_paddr, "TX ring");
8764d52a575SXin LI 	if (error)
877398f1b65SPyun YongHyeon 		return (error);
8784d52a575SXin LI 
87905884511SPyun YongHyeon 	/* TX status block. */
88005884511SPyun YongHyeon 	txsd = &sc->sc_tx_status;
88105884511SPyun YongHyeon 	error = et_dma_ring_alloc(sc, ET_STATUS_ALIGN, sizeof(uint32_t),
88205884511SPyun YongHyeon 	    &txsd->txsd_dtag, (uint8_t **)&txsd->txsd_status, &txsd->txsd_dmap,
88305884511SPyun YongHyeon 	    &txsd->txsd_paddr, "TX status block");
88405884511SPyun YongHyeon 	if (error)
88505884511SPyun YongHyeon 		return (error);
8864d52a575SXin LI 
88705884511SPyun YongHyeon 	/* RX ring 0, used as to recive small sized frames. */
88805884511SPyun YongHyeon 	rx_ring = &sc->sc_rx_ring[0];
88905884511SPyun YongHyeon 	error = et_dma_ring_alloc(sc, ET_RING_ALIGN, ET_RX_RING_SIZE,
89005884511SPyun YongHyeon 	    &rx_ring->rr_dtag, (uint8_t **)&rx_ring->rr_desc, &rx_ring->rr_dmap,
89105884511SPyun YongHyeon 	    &rx_ring->rr_paddr, "RX ring 0");
89205884511SPyun YongHyeon 	rx_ring->rr_posreg = ET_RX_RING0_POS;
89305884511SPyun YongHyeon 	if (error)
89405884511SPyun YongHyeon 		return (error);
8954d52a575SXin LI 
89605884511SPyun YongHyeon 	/* RX ring 1, used as to store normal sized frames. */
89705884511SPyun YongHyeon 	rx_ring = &sc->sc_rx_ring[1];
89805884511SPyun YongHyeon 	error = et_dma_ring_alloc(sc, ET_RING_ALIGN, ET_RX_RING_SIZE,
89905884511SPyun YongHyeon 	    &rx_ring->rr_dtag, (uint8_t **)&rx_ring->rr_desc, &rx_ring->rr_dmap,
90005884511SPyun YongHyeon 	    &rx_ring->rr_paddr, "RX ring 1");
90105884511SPyun YongHyeon 	rx_ring->rr_posreg = ET_RX_RING1_POS;
90205884511SPyun YongHyeon 	if (error)
90305884511SPyun YongHyeon 		return (error);
9044d52a575SXin LI 
90505884511SPyun YongHyeon 	/* RX stat ring. */
90605884511SPyun YongHyeon 	rxst_ring = &sc->sc_rxstat_ring;
90705884511SPyun YongHyeon 	error = et_dma_ring_alloc(sc, ET_RING_ALIGN, ET_RXSTAT_RING_SIZE,
90805884511SPyun YongHyeon 	    &rxst_ring->rsr_dtag, (uint8_t **)&rxst_ring->rsr_stat,
90905884511SPyun YongHyeon 	    &rxst_ring->rsr_dmap, &rxst_ring->rsr_paddr, "RX stat ring");
91005884511SPyun YongHyeon 	if (error)
91105884511SPyun YongHyeon 		return (error);
9124d52a575SXin LI 
91305884511SPyun YongHyeon 	/* RX status block. */
91405884511SPyun YongHyeon 	rxsd = &sc->sc_rx_status;
91505884511SPyun YongHyeon 	error = et_dma_ring_alloc(sc, ET_STATUS_ALIGN,
91605884511SPyun YongHyeon 	    sizeof(struct et_rxstatus), &rxsd->rxsd_dtag,
91705884511SPyun YongHyeon 	    (uint8_t **)&rxsd->rxsd_status, &rxsd->rxsd_dmap,
91805884511SPyun YongHyeon 	    &rxsd->rxsd_paddr, "RX status block");
91905884511SPyun YongHyeon 	if (error)
92005884511SPyun YongHyeon 		return (error);
9214d52a575SXin LI 
92205884511SPyun YongHyeon 	/* Create parent DMA tag for mbufs. */
92305884511SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 1, 0,
92405884511SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
92505884511SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0, NULL, NULL,
92605884511SPyun YongHyeon 	    &sc->sc_mbuf_dtag);
92705884511SPyun YongHyeon 	if (error != 0) {
92805884511SPyun YongHyeon 		device_printf(sc->dev,
92905884511SPyun YongHyeon 		    "could not allocate parent dma tag for mbuf\n");
930398f1b65SPyun YongHyeon 		return (error);
9314d52a575SXin LI 	}
9324d52a575SXin LI 
93305884511SPyun YongHyeon 	/* Create DMA tag for mini RX mbufs to use RX ring 0. */
93405884511SPyun YongHyeon 	error = bus_dma_tag_create(sc->sc_mbuf_dtag, 1, 0,
93505884511SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, MHLEN, 1,
93605884511SPyun YongHyeon 	    MHLEN, 0, NULL, NULL, &sc->sc_rx_mini_tag);
9374d52a575SXin LI 	if (error) {
93805884511SPyun YongHyeon 		device_printf(sc->dev, "could not create mini RX dma tag\n");
939398f1b65SPyun YongHyeon 		return (error);
9404d52a575SXin LI 	}
9414d52a575SXin LI 
94205884511SPyun YongHyeon 	/* Create DMA tag for standard RX mbufs to use RX ring 1. */
94305884511SPyun YongHyeon 	error = bus_dma_tag_create(sc->sc_mbuf_dtag, 1, 0,
94405884511SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1,
94505884511SPyun YongHyeon 	    MCLBYTES, 0, NULL, NULL, &sc->sc_rx_tag);
9464d52a575SXin LI 	if (error) {
94705884511SPyun YongHyeon 		device_printf(sc->dev, "could not create RX dma tag\n");
948398f1b65SPyun YongHyeon 		return (error);
9494d52a575SXin LI 	}
9504d52a575SXin LI 
95105884511SPyun YongHyeon 	/* Create DMA tag for TX mbufs. */
95205884511SPyun YongHyeon 	error = bus_dma_tag_create(sc->sc_mbuf_dtag, 1, 0,
95305884511SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
95405884511SPyun YongHyeon 	    MCLBYTES * ET_NSEG_MAX, ET_NSEG_MAX, MCLBYTES, 0, NULL, NULL,
95505884511SPyun YongHyeon 	    &sc->sc_tx_tag);
95605884511SPyun YongHyeon 	if (error) {
95705884511SPyun YongHyeon 		device_printf(sc->dev, "could not create TX dma tag\n");
95805884511SPyun YongHyeon 		return (error);
95905884511SPyun YongHyeon 	}
96005884511SPyun YongHyeon 
96105884511SPyun YongHyeon 	/* Initialize RX ring 0. */
96205884511SPyun YongHyeon 	rbd = &sc->sc_rx_data[0];
96305884511SPyun YongHyeon 	rbd->rbd_bufsize = ET_RXDMA_CTRL_RING0_128;
96405884511SPyun YongHyeon 	rbd->rbd_newbuf = et_newbuf_hdr;
96505884511SPyun YongHyeon 	rbd->rbd_discard = et_rxbuf_discard;
9664d52a575SXin LI 	rbd->rbd_softc = sc;
96705884511SPyun YongHyeon 	rbd->rbd_ring = &sc->sc_rx_ring[0];
96805884511SPyun YongHyeon 	/* Create DMA maps for mini RX buffers, ring 0. */
96905884511SPyun YongHyeon 	for (i = 0; i < ET_RX_NDESC; i++) {
97005884511SPyun YongHyeon 		error = bus_dmamap_create(sc->sc_rx_mini_tag, 0,
97105884511SPyun YongHyeon 		    &rbd->rbd_buf[i].rb_dmap);
97205884511SPyun YongHyeon 		if (error) {
97305884511SPyun YongHyeon 			device_printf(sc->dev,
97405884511SPyun YongHyeon 			    "could not create DMA map for mini RX mbufs\n");
97505884511SPyun YongHyeon 			return (error);
97605884511SPyun YongHyeon 		}
9774d52a575SXin LI 	}
9784d52a575SXin LI 
97905884511SPyun YongHyeon 	/* Create a spare DMA map for mini RX buffers, ring 0. */
98005884511SPyun YongHyeon 	error = bus_dmamap_create(sc->sc_rx_mini_tag, 0,
98105884511SPyun YongHyeon 	    &sc->sc_rx_mini_sparemap);
98205884511SPyun YongHyeon 	if (error) {
98305884511SPyun YongHyeon 		device_printf(sc->dev,
98405884511SPyun YongHyeon 		    "could not create spare DMA map for mini RX mbuf\n");
98505884511SPyun YongHyeon 		return (error);
98605884511SPyun YongHyeon 	}
98705884511SPyun YongHyeon 
98805884511SPyun YongHyeon 	/* Initialize RX ring 1. */
98905884511SPyun YongHyeon 	rbd = &sc->sc_rx_data[1];
99005884511SPyun YongHyeon 	rbd->rbd_bufsize = ET_RXDMA_CTRL_RING1_2048;
99105884511SPyun YongHyeon 	rbd->rbd_newbuf = et_newbuf_cluster;
99205884511SPyun YongHyeon 	rbd->rbd_discard = et_rxbuf_discard;
99305884511SPyun YongHyeon 	rbd->rbd_softc = sc;
99405884511SPyun YongHyeon 	rbd->rbd_ring = &sc->sc_rx_ring[1];
99505884511SPyun YongHyeon 	/* Create DMA maps for standard RX buffers, ring 1. */
99605884511SPyun YongHyeon 	for (i = 0; i < ET_RX_NDESC; i++) {
99705884511SPyun YongHyeon 		error = bus_dmamap_create(sc->sc_rx_tag, 0,
99805884511SPyun YongHyeon 		    &rbd->rbd_buf[i].rb_dmap);
99905884511SPyun YongHyeon 		if (error) {
100005884511SPyun YongHyeon 			device_printf(sc->dev,
100105884511SPyun YongHyeon 			    "could not create DMA map for mini RX mbufs\n");
100205884511SPyun YongHyeon 			return (error);
100305884511SPyun YongHyeon 		}
100405884511SPyun YongHyeon 	}
100505884511SPyun YongHyeon 
100605884511SPyun YongHyeon 	/* Create a spare DMA map for standard RX buffers, ring 1. */
100705884511SPyun YongHyeon 	error = bus_dmamap_create(sc->sc_rx_tag, 0, &sc->sc_rx_sparemap);
100805884511SPyun YongHyeon 	if (error) {
100905884511SPyun YongHyeon 		device_printf(sc->dev,
101005884511SPyun YongHyeon 		    "could not create spare DMA map for RX mbuf\n");
101105884511SPyun YongHyeon 		return (error);
101205884511SPyun YongHyeon 	}
101305884511SPyun YongHyeon 
101405884511SPyun YongHyeon 	/* Create DMA maps for TX buffers. */
101505884511SPyun YongHyeon 	tbd = &sc->sc_tx_data;
101605884511SPyun YongHyeon 	for (i = 0; i < ET_TX_NDESC; i++) {
101705884511SPyun YongHyeon 		error = bus_dmamap_create(sc->sc_tx_tag, 0,
10184d52a575SXin LI 		    &tbd->tbd_buf[i].tb_dmap);
10194d52a575SXin LI 		if (error) {
102005884511SPyun YongHyeon 			device_printf(sc->dev,
102105884511SPyun YongHyeon 			    "could not create DMA map for TX mbufs\n");
1022398f1b65SPyun YongHyeon 			return (error);
10234d52a575SXin LI 		}
10244d52a575SXin LI 	}
10254d52a575SXin LI 
1026398f1b65SPyun YongHyeon 	return (0);
10274d52a575SXin LI }
10284d52a575SXin LI 
10294d52a575SXin LI static void
103005884511SPyun YongHyeon et_dma_free(struct et_softc *sc)
10314d52a575SXin LI {
103205884511SPyun YongHyeon 	struct et_txdesc_ring *tx_ring;
103305884511SPyun YongHyeon 	struct et_rxdesc_ring *rx_ring;
103405884511SPyun YongHyeon 	struct et_txstatus_data *txsd;
103505884511SPyun YongHyeon 	struct et_rxstat_ring *rxst_ring;
103605884511SPyun YongHyeon 	struct et_rxstatus_data *rxsd;
103705884511SPyun YongHyeon 	struct et_rxbuf_data *rbd;
103805884511SPyun YongHyeon         struct et_txbuf_data *tbd;
10394d52a575SXin LI 	int i;
10404d52a575SXin LI 
104105884511SPyun YongHyeon 	/* Destroy DMA maps for mini RX buffers, ring 0. */
104205884511SPyun YongHyeon 	rbd = &sc->sc_rx_data[0];
104305884511SPyun YongHyeon 	for (i = 0; i < ET_RX_NDESC; i++) {
104405884511SPyun YongHyeon 		if (rbd->rbd_buf[i].rb_dmap) {
104505884511SPyun YongHyeon 			bus_dmamap_destroy(sc->sc_rx_mini_tag,
104605884511SPyun YongHyeon 			    rbd->rbd_buf[i].rb_dmap);
104705884511SPyun YongHyeon 			rbd->rbd_buf[i].rb_dmap = NULL;
10484d52a575SXin LI 		}
10494d52a575SXin LI 	}
105005884511SPyun YongHyeon 	if (sc->sc_rx_mini_sparemap) {
105105884511SPyun YongHyeon 		bus_dmamap_destroy(sc->sc_rx_mini_tag, sc->sc_rx_mini_sparemap);
105205884511SPyun YongHyeon 		sc->sc_rx_mini_sparemap = NULL;
105305884511SPyun YongHyeon 	}
105405884511SPyun YongHyeon 	if (sc->sc_rx_mini_tag) {
105505884511SPyun YongHyeon 		bus_dma_tag_destroy(sc->sc_rx_mini_tag);
105605884511SPyun YongHyeon 		sc->sc_rx_mini_tag = NULL;
10574d52a575SXin LI 	}
10584d52a575SXin LI 
105905884511SPyun YongHyeon 	/* Destroy DMA maps for standard RX buffers, ring 1. */
106005884511SPyun YongHyeon 	rbd = &sc->sc_rx_data[1];
106105884511SPyun YongHyeon 	for (i = 0; i < ET_RX_NDESC; i++) {
106205884511SPyun YongHyeon 		if (rbd->rbd_buf[i].rb_dmap) {
106305884511SPyun YongHyeon 			bus_dmamap_destroy(sc->sc_rx_tag,
106405884511SPyun YongHyeon 			    rbd->rbd_buf[i].rb_dmap);
106505884511SPyun YongHyeon 			rbd->rbd_buf[i].rb_dmap = NULL;
10664d52a575SXin LI 		}
10674d52a575SXin LI 	}
106805884511SPyun YongHyeon 	if (sc->sc_rx_sparemap) {
106905884511SPyun YongHyeon 		bus_dmamap_destroy(sc->sc_rx_tag, sc->sc_rx_sparemap);
107005884511SPyun YongHyeon 		sc->sc_rx_sparemap = NULL;
107105884511SPyun YongHyeon 	}
107205884511SPyun YongHyeon 	if (sc->sc_rx_tag) {
107305884511SPyun YongHyeon 		bus_dma_tag_destroy(sc->sc_rx_tag);
107405884511SPyun YongHyeon 		sc->sc_rx_tag = NULL;
107505884511SPyun YongHyeon 	}
10764d52a575SXin LI 
107705884511SPyun YongHyeon 	/* Destroy DMA maps for TX buffers. */
107805884511SPyun YongHyeon 	tbd = &sc->sc_tx_data;
107905884511SPyun YongHyeon 	for (i = 0; i < ET_TX_NDESC; i++) {
108005884511SPyun YongHyeon 		if (tbd->tbd_buf[i].tb_dmap) {
108105884511SPyun YongHyeon 			bus_dmamap_destroy(sc->sc_tx_tag,
108205884511SPyun YongHyeon 			    tbd->tbd_buf[i].tb_dmap);
108305884511SPyun YongHyeon 			tbd->tbd_buf[i].tb_dmap = NULL;
108405884511SPyun YongHyeon 		}
108505884511SPyun YongHyeon 	}
108605884511SPyun YongHyeon 	if (sc->sc_tx_tag) {
108705884511SPyun YongHyeon 		bus_dma_tag_destroy(sc->sc_tx_tag);
108805884511SPyun YongHyeon 		sc->sc_tx_tag = NULL;
108905884511SPyun YongHyeon 	}
109005884511SPyun YongHyeon 
109105884511SPyun YongHyeon 	/* Destroy mini RX ring, ring 0. */
109205884511SPyun YongHyeon 	rx_ring = &sc->sc_rx_ring[0];
109305884511SPyun YongHyeon 	et_dma_ring_free(sc, &rx_ring->rr_dtag, (void *)&rx_ring->rr_desc,
109405884511SPyun YongHyeon 	    &rx_ring->rr_dmap);
109505884511SPyun YongHyeon 	/* Destroy standard RX ring, ring 1. */
109605884511SPyun YongHyeon 	rx_ring = &sc->sc_rx_ring[1];
109705884511SPyun YongHyeon 	et_dma_ring_free(sc, &rx_ring->rr_dtag, (void *)&rx_ring->rr_desc,
109805884511SPyun YongHyeon 	    &rx_ring->rr_dmap);
109905884511SPyun YongHyeon 	/* Destroy RX stat ring. */
110005884511SPyun YongHyeon 	rxst_ring = &sc->sc_rxstat_ring;
110105884511SPyun YongHyeon 	et_dma_ring_free(sc, &rxst_ring->rsr_dtag, (void *)&rxst_ring->rsr_stat,
110205884511SPyun YongHyeon 	    &rxst_ring->rsr_dmap);
110305884511SPyun YongHyeon 	/* Destroy RX status block. */
110405884511SPyun YongHyeon 	rxsd = &sc->sc_rx_status;
110505884511SPyun YongHyeon 	et_dma_ring_free(sc, &rxst_ring->rsr_dtag, (void *)&rxst_ring->rsr_stat,
110605884511SPyun YongHyeon 	    &rxst_ring->rsr_dmap);
110705884511SPyun YongHyeon 	/* Destroy TX ring. */
110805884511SPyun YongHyeon 	tx_ring = &sc->sc_tx_ring;
110905884511SPyun YongHyeon 	et_dma_ring_free(sc, &tx_ring->tr_dtag, (void *)&tx_ring->tr_desc,
111005884511SPyun YongHyeon 	    &tx_ring->tr_dmap);
111105884511SPyun YongHyeon 	/* Destroy TX status block. */
111205884511SPyun YongHyeon 	txsd = &sc->sc_tx_status;
111305884511SPyun YongHyeon 	et_dma_ring_free(sc, &txsd->txsd_dtag, (void *)&txsd->txsd_status,
111405884511SPyun YongHyeon 	    &txsd->txsd_dmap);
111505884511SPyun YongHyeon 
111605884511SPyun YongHyeon 	/* Destroy the parent tag. */
111705884511SPyun YongHyeon 	if (sc->sc_dtag) {
111805884511SPyun YongHyeon 		bus_dma_tag_destroy(sc->sc_dtag);
111905884511SPyun YongHyeon 		sc->sc_dtag = NULL;
112005884511SPyun YongHyeon 	}
11214d52a575SXin LI }
11224d52a575SXin LI 
11234d52a575SXin LI static void
11244d52a575SXin LI et_chip_attach(struct et_softc *sc)
11254d52a575SXin LI {
11264d52a575SXin LI 	uint32_t val;
11274d52a575SXin LI 
11284d52a575SXin LI 	/*
11294d52a575SXin LI 	 * Perform minimal initialization
11304d52a575SXin LI 	 */
11314d52a575SXin LI 
11324d52a575SXin LI 	/* Disable loopback */
11334d52a575SXin LI 	CSR_WRITE_4(sc, ET_LOOPBACK, 0);
11344d52a575SXin LI 
11354d52a575SXin LI 	/* Reset MAC */
11364d52a575SXin LI 	CSR_WRITE_4(sc, ET_MAC_CFG1,
11374d52a575SXin LI 		    ET_MAC_CFG1_RST_TXFUNC | ET_MAC_CFG1_RST_RXFUNC |
11384d52a575SXin LI 		    ET_MAC_CFG1_RST_TXMC | ET_MAC_CFG1_RST_RXMC |
11394d52a575SXin LI 		    ET_MAC_CFG1_SIM_RST | ET_MAC_CFG1_SOFT_RST);
11404d52a575SXin LI 
11414d52a575SXin LI 	/*
11424d52a575SXin LI 	 * Setup half duplex mode
11434d52a575SXin LI 	 */
114423263665SPyun YongHyeon 	val = (10 << ET_MAC_HDX_ALT_BEB_TRUNC_SHIFT) |
114523263665SPyun YongHyeon 	    (15 << ET_MAC_HDX_REXMIT_MAX_SHIFT) |
114623263665SPyun YongHyeon 	    (55 << ET_MAC_HDX_COLLWIN_SHIFT) |
11474d52a575SXin LI 	    ET_MAC_HDX_EXC_DEFER;
11484d52a575SXin LI 	CSR_WRITE_4(sc, ET_MAC_HDX, val);
11494d52a575SXin LI 
11504d52a575SXin LI 	/* Clear MAC control */
11514d52a575SXin LI 	CSR_WRITE_4(sc, ET_MAC_CTRL, 0);
11524d52a575SXin LI 
11534d52a575SXin LI 	/* Reset MII */
11544d52a575SXin LI 	CSR_WRITE_4(sc, ET_MII_CFG, ET_MII_CFG_CLKRST);
11554d52a575SXin LI 
11564d52a575SXin LI 	/* Bring MAC out of reset state */
11574d52a575SXin LI 	CSR_WRITE_4(sc, ET_MAC_CFG1, 0);
11584d52a575SXin LI 
11594d52a575SXin LI 	/* Enable memory controllers */
11604d52a575SXin LI 	CSR_WRITE_4(sc, ET_MMC_CTRL, ET_MMC_CTRL_ENABLE);
11614d52a575SXin LI }
11624d52a575SXin LI 
11634d52a575SXin LI static void
11644d52a575SXin LI et_intr(void *xsc)
11654d52a575SXin LI {
11664d52a575SXin LI 	struct et_softc *sc = xsc;
11674d52a575SXin LI 	struct ifnet *ifp;
1168fa1483ddSPyun YongHyeon 	uint32_t status;
11694d52a575SXin LI 
11704d52a575SXin LI 	ET_LOCK(sc);
11714d52a575SXin LI 	ifp = sc->ifp;
1172fa1483ddSPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1173fa1483ddSPyun YongHyeon 		goto done;
1174fa1483ddSPyun YongHyeon 
1175fa1483ddSPyun YongHyeon 	status = CSR_READ_4(sc, ET_INTR_STATUS);
1176fa1483ddSPyun YongHyeon 	if ((status & ET_INTRS) == 0)
1177fa1483ddSPyun YongHyeon 		goto done;
11784d52a575SXin LI 
11796537ffa6SPyun YongHyeon 	/* Disable further interrupts. */
11806537ffa6SPyun YongHyeon 	CSR_WRITE_4(sc, ET_INTR_MASK, 0xffffffff);
11814d52a575SXin LI 
1182fa1483ddSPyun YongHyeon 	if (status & (ET_INTR_RXDMA_ERROR | ET_INTR_TXDMA_ERROR)) {
1183fa1483ddSPyun YongHyeon 		device_printf(sc->dev, "DMA error(0x%08x) -- resetting\n",
1184fa1483ddSPyun YongHyeon 		    status);
1185fa1483ddSPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1186fa1483ddSPyun YongHyeon 		et_init_locked(sc);
1187fa1483ddSPyun YongHyeon 		ET_UNLOCK(sc);
1188fa1483ddSPyun YongHyeon 		return;
1189fa1483ddSPyun YongHyeon 	}
1190fa1483ddSPyun YongHyeon 	if (status & ET_INTR_RXDMA)
11914d52a575SXin LI 		et_rxeof(sc);
1192fa1483ddSPyun YongHyeon 	if (status & (ET_INTR_TXDMA | ET_INTR_TIMER))
11934d52a575SXin LI 		et_txeof(sc);
1194fa1483ddSPyun YongHyeon 	if (status & ET_INTR_TIMER)
11954d52a575SXin LI 		CSR_WRITE_4(sc, ET_TIMER, sc->sc_timer);
1196244fd28bSPyun YongHyeon 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
11976537ffa6SPyun YongHyeon 		CSR_WRITE_4(sc, ET_INTR_MASK, ~ET_INTRS);
1198244fd28bSPyun YongHyeon 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1199244fd28bSPyun YongHyeon 			et_start_locked(ifp);
1200244fd28bSPyun YongHyeon 	}
1201fa1483ddSPyun YongHyeon done:
12024d52a575SXin LI 	ET_UNLOCK(sc);
12034d52a575SXin LI }
12044d52a575SXin LI 
12054d52a575SXin LI static void
12064d52a575SXin LI et_init_locked(struct et_softc *sc)
12074d52a575SXin LI {
120805884511SPyun YongHyeon 	struct ifnet *ifp;
120905884511SPyun YongHyeon 	int error;
12104d52a575SXin LI 
12114d52a575SXin LI 	ET_LOCK_ASSERT(sc);
12124d52a575SXin LI 
121305884511SPyun YongHyeon 	ifp = sc->ifp;
12144d52a575SXin LI 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
12154d52a575SXin LI 		return;
12164d52a575SXin LI 
12174d52a575SXin LI 	et_stop(sc);
12181f009e2fSPyun YongHyeon 	et_reset(sc);
12194d52a575SXin LI 
122005884511SPyun YongHyeon 	et_init_tx_ring(sc);
12214d52a575SXin LI 	error = et_init_rx_ring(sc);
12224d52a575SXin LI 	if (error)
122305884511SPyun YongHyeon 		return;
12244d52a575SXin LI 
12254d52a575SXin LI 	error = et_chip_init(sc);
12264d52a575SXin LI 	if (error)
12271f009e2fSPyun YongHyeon 		goto fail;
12284d52a575SXin LI 
12291f009e2fSPyun YongHyeon 	/*
12301f009e2fSPyun YongHyeon 	 * Start TX/RX DMA engine
12311f009e2fSPyun YongHyeon 	 */
12321f009e2fSPyun YongHyeon 	error = et_start_rxdma(sc);
12334d52a575SXin LI 	if (error)
12341f009e2fSPyun YongHyeon 		return;
12351f009e2fSPyun YongHyeon 
12361f009e2fSPyun YongHyeon 	error = et_start_txdma(sc);
12371f009e2fSPyun YongHyeon 	if (error)
12381f009e2fSPyun YongHyeon 		return;
12394d52a575SXin LI 
12406537ffa6SPyun YongHyeon 	/* Enable interrupts. */
12416537ffa6SPyun YongHyeon 	CSR_WRITE_4(sc, ET_INTR_MASK, ~ET_INTRS);
12424d52a575SXin LI 
12434d52a575SXin LI 	CSR_WRITE_4(sc, ET_TIMER, sc->sc_timer);
12444d52a575SXin LI 
12454d52a575SXin LI 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
12464d52a575SXin LI 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
12471f009e2fSPyun YongHyeon 
12481f009e2fSPyun YongHyeon 	sc->sc_flags &= ~ET_FLAG_LINK;
12491f009e2fSPyun YongHyeon 	et_ifmedia_upd_locked(ifp);
12501f009e2fSPyun YongHyeon 
12511f009e2fSPyun YongHyeon 	callout_reset(&sc->sc_tick, hz, et_tick, sc);
12521f009e2fSPyun YongHyeon 
12531f009e2fSPyun YongHyeon fail:
12544d52a575SXin LI 	if (error)
12554d52a575SXin LI 		et_stop(sc);
12564d52a575SXin LI }
12574d52a575SXin LI 
12584d52a575SXin LI static void
12594d52a575SXin LI et_init(void *xsc)
12604d52a575SXin LI {
12614d52a575SXin LI 	struct et_softc *sc = xsc;
12624d52a575SXin LI 
12634d52a575SXin LI 	ET_LOCK(sc);
12644d52a575SXin LI 	et_init_locked(sc);
12654d52a575SXin LI 	ET_UNLOCK(sc);
12664d52a575SXin LI }
12674d52a575SXin LI 
12684d52a575SXin LI static int
12694d52a575SXin LI et_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
12704d52a575SXin LI {
12714d52a575SXin LI 	struct et_softc *sc = ifp->if_softc;
12724d52a575SXin LI 	struct mii_data *mii = device_get_softc(sc->sc_miibus);
12734d52a575SXin LI 	struct ifreq *ifr = (struct ifreq *)data;
12749955274cSPyun YongHyeon 	int error = 0, mask, max_framelen;
12754d52a575SXin LI 
12764d52a575SXin LI /* XXX LOCKSUSED */
12774d52a575SXin LI 	switch (cmd) {
12784d52a575SXin LI 	case SIOCSIFFLAGS:
12794d52a575SXin LI 		ET_LOCK(sc);
12804d52a575SXin LI 		if (ifp->if_flags & IFF_UP) {
12814d52a575SXin LI 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
12824d52a575SXin LI 				if ((ifp->if_flags ^ sc->sc_if_flags) &
12834d52a575SXin LI 				(IFF_ALLMULTI | IFF_PROMISC | IFF_BROADCAST))
12844d52a575SXin LI 					et_setmulti(sc);
12854d52a575SXin LI 			} else {
12864d52a575SXin LI 				et_init_locked(sc);
12874d52a575SXin LI 			}
12884d52a575SXin LI 		} else {
12894d52a575SXin LI 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
12904d52a575SXin LI 				et_stop(sc);
12914d52a575SXin LI 		}
12924d52a575SXin LI 		sc->sc_if_flags = ifp->if_flags;
12934d52a575SXin LI 		ET_UNLOCK(sc);
12944d52a575SXin LI 		break;
12954d52a575SXin LI 
12964d52a575SXin LI 	case SIOCSIFMEDIA:
12974d52a575SXin LI 	case SIOCGIFMEDIA:
12984d52a575SXin LI 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
12994d52a575SXin LI 		break;
13004d52a575SXin LI 
13014d52a575SXin LI 	case SIOCADDMULTI:
13024d52a575SXin LI 	case SIOCDELMULTI:
13034d52a575SXin LI 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
13044d52a575SXin LI 			ET_LOCK(sc);
13054d52a575SXin LI 			et_setmulti(sc);
13064d52a575SXin LI 			ET_UNLOCK(sc);
13074d52a575SXin LI 		}
13084d52a575SXin LI 		break;
13094d52a575SXin LI 
13104d52a575SXin LI 	case SIOCSIFMTU:
13118e5ad990SPyun YongHyeon 		ET_LOCK(sc);
13124d52a575SXin LI #if 0
13134d52a575SXin LI 		if (sc->sc_flags & ET_FLAG_JUMBO)
13144d52a575SXin LI 			max_framelen = ET_JUMBO_FRAMELEN;
13154d52a575SXin LI 		else
13164d52a575SXin LI #endif
13174d52a575SXin LI 			max_framelen = MCLBYTES - 1;
13184d52a575SXin LI 
13194d52a575SXin LI 		if (ET_FRAMELEN(ifr->ifr_mtu) > max_framelen) {
13204d52a575SXin LI 			error = EOPNOTSUPP;
13218e5ad990SPyun YongHyeon 			ET_UNLOCK(sc);
13224d52a575SXin LI 			break;
13234d52a575SXin LI 		}
13244d52a575SXin LI 
13254d52a575SXin LI 		if (ifp->if_mtu != ifr->ifr_mtu) {
13264d52a575SXin LI 			ifp->if_mtu = ifr->ifr_mtu;
13278e5ad990SPyun YongHyeon 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
13284d52a575SXin LI 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
13298e5ad990SPyun YongHyeon 				et_init_locked(sc);
13304d52a575SXin LI 			}
13318e5ad990SPyun YongHyeon 		}
13328e5ad990SPyun YongHyeon 		ET_UNLOCK(sc);
13334d52a575SXin LI 		break;
13344d52a575SXin LI 
13359955274cSPyun YongHyeon 	case SIOCSIFCAP:
13369955274cSPyun YongHyeon 		ET_LOCK(sc);
13379955274cSPyun YongHyeon 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
13389955274cSPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
13399955274cSPyun YongHyeon 		    (IFCAP_TXCSUM & ifp->if_capabilities) != 0) {
13409955274cSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TXCSUM;
13419955274cSPyun YongHyeon 			if ((IFCAP_TXCSUM & ifp->if_capenable) != 0)
13429955274cSPyun YongHyeon 				ifp->if_hwassist |= ET_CSUM_FEATURES;
13439955274cSPyun YongHyeon 			else
13449955274cSPyun YongHyeon 				ifp->if_hwassist &= ~ET_CSUM_FEATURES;
13459955274cSPyun YongHyeon 		}
13469955274cSPyun YongHyeon 		ET_UNLOCK(sc);
13479955274cSPyun YongHyeon 		break;
13489955274cSPyun YongHyeon 
13494d52a575SXin LI 	default:
13504d52a575SXin LI 		error = ether_ioctl(ifp, cmd, data);
13514d52a575SXin LI 		break;
13524d52a575SXin LI 	}
1353398f1b65SPyun YongHyeon 	return (error);
13544d52a575SXin LI }
13554d52a575SXin LI 
13564d52a575SXin LI static void
13574d52a575SXin LI et_start_locked(struct ifnet *ifp)
13584d52a575SXin LI {
1359c8b727ceSPyun YongHyeon 	struct et_softc *sc;
1360c8b727ceSPyun YongHyeon 	struct mbuf *m_head = NULL;
1361244fd28bSPyun YongHyeon 	struct et_txdesc_ring *tx_ring;
13624d52a575SXin LI 	struct et_txbuf_data *tbd;
1363244fd28bSPyun YongHyeon 	uint32_t tx_ready_pos;
1364c8b727ceSPyun YongHyeon 	int enq;
13654d52a575SXin LI 
1366c8b727ceSPyun YongHyeon 	sc = ifp->if_softc;
13674d52a575SXin LI 	ET_LOCK_ASSERT(sc);
13684d52a575SXin LI 
13691f009e2fSPyun YongHyeon 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
13701f009e2fSPyun YongHyeon 	    IFF_DRV_RUNNING ||
13711f009e2fSPyun YongHyeon 	    (sc->sc_flags & (ET_FLAG_LINK | ET_FLAG_TXRX_ENABLED)) !=
13721f009e2fSPyun YongHyeon 	    (ET_FLAG_LINK | ET_FLAG_TXRX_ENABLED))
13734d52a575SXin LI 		return;
13744d52a575SXin LI 
1375244fd28bSPyun YongHyeon 	/*
1376244fd28bSPyun YongHyeon 	 * Driver does not request TX completion interrupt for every
1377244fd28bSPyun YongHyeon 	 * queued frames to prevent generating excessive interrupts.
1378244fd28bSPyun YongHyeon 	 * This means driver may wait for TX completion interrupt even
1379244fd28bSPyun YongHyeon 	 * though some frames were sucessfully transmitted.  Reclaiming
1380244fd28bSPyun YongHyeon 	 * transmitted frames will ensure driver see all available
1381244fd28bSPyun YongHyeon 	 * descriptors.
1382244fd28bSPyun YongHyeon 	 */
1383c8b727ceSPyun YongHyeon 	tbd = &sc->sc_tx_data;
1384244fd28bSPyun YongHyeon 	if (tbd->tbd_used > (ET_TX_NDESC * 2) / 3)
1385244fd28bSPyun YongHyeon 		et_txeof(sc);
1386244fd28bSPyun YongHyeon 
1387c8b727ceSPyun YongHyeon 	for (enq = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd); ) {
1388c8b727ceSPyun YongHyeon 		if (tbd->tbd_used + ET_NSEG_SPARE >= ET_TX_NDESC) {
13894d52a575SXin LI 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
13904d52a575SXin LI 			break;
13914d52a575SXin LI 		}
13924d52a575SXin LI 
1393c8b727ceSPyun YongHyeon 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
1394c8b727ceSPyun YongHyeon 		if (m_head == NULL)
13954d52a575SXin LI 			break;
13964d52a575SXin LI 
1397c8b727ceSPyun YongHyeon 		if (et_encap(sc, &m_head)) {
1398c8b727ceSPyun YongHyeon 			if (m_head == NULL) {
13994d52a575SXin LI 				ifp->if_oerrors++;
1400c8b727ceSPyun YongHyeon 				break;
1401c8b727ceSPyun YongHyeon 			}
1402c8b727ceSPyun YongHyeon 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
1403c8b727ceSPyun YongHyeon 			if (tbd->tbd_used > 0)
14044d52a575SXin LI 				ifp->if_drv_flags |= IFF_DRV_OACTIVE;
14054d52a575SXin LI 			break;
14064d52a575SXin LI 		}
1407c8b727ceSPyun YongHyeon 		enq++;
1408c8b727ceSPyun YongHyeon 		ETHER_BPF_MTAP(ifp, m_head);
14094d52a575SXin LI 	}
14104d52a575SXin LI 
1411244fd28bSPyun YongHyeon 	if (enq > 0) {
1412244fd28bSPyun YongHyeon 		tx_ring = &sc->sc_tx_ring;
1413244fd28bSPyun YongHyeon 		bus_dmamap_sync(tx_ring->tr_dtag, tx_ring->tr_dmap,
1414244fd28bSPyun YongHyeon 		    BUS_DMASYNC_PREWRITE);
1415244fd28bSPyun YongHyeon 		tx_ready_pos = tx_ring->tr_ready_index &
1416244fd28bSPyun YongHyeon 		    ET_TX_READY_POS_INDEX_MASK;
1417244fd28bSPyun YongHyeon 		if (tx_ring->tr_ready_wrap)
1418244fd28bSPyun YongHyeon 			tx_ready_pos |= ET_TX_READY_POS_WRAP;
1419244fd28bSPyun YongHyeon 		CSR_WRITE_4(sc, ET_TX_READY_POS, tx_ready_pos);
14204d52a575SXin LI 		sc->watchdog_timer = 5;
14214d52a575SXin LI 	}
1422244fd28bSPyun YongHyeon }
14234d52a575SXin LI 
14244d52a575SXin LI static void
14254d52a575SXin LI et_start(struct ifnet *ifp)
14264d52a575SXin LI {
14274d52a575SXin LI 	struct et_softc *sc = ifp->if_softc;
14284d52a575SXin LI 
14294d52a575SXin LI 	ET_LOCK(sc);
14304d52a575SXin LI 	et_start_locked(ifp);
14314d52a575SXin LI 	ET_UNLOCK(sc);
14324d52a575SXin LI }
14334d52a575SXin LI 
143405884511SPyun YongHyeon static int
14354d52a575SXin LI et_watchdog(struct et_softc *sc)
14364d52a575SXin LI {
143705884511SPyun YongHyeon 	uint32_t status;
143805884511SPyun YongHyeon 
14394d52a575SXin LI 	ET_LOCK_ASSERT(sc);
14404d52a575SXin LI 
14414d52a575SXin LI 	if (sc->watchdog_timer == 0 || --sc->watchdog_timer)
144205884511SPyun YongHyeon 		return (0);
14434d52a575SXin LI 
144405884511SPyun YongHyeon 	bus_dmamap_sync(sc->sc_tx_status.txsd_dtag, sc->sc_tx_status.txsd_dmap,
144505884511SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD);
144605884511SPyun YongHyeon 	status = le32toh(*(sc->sc_tx_status.txsd_status));
144705884511SPyun YongHyeon 	if_printf(sc->ifp, "watchdog timed out (0x%08x) -- resetting\n",
144805884511SPyun YongHyeon 	    status);
14494d52a575SXin LI 
1450744ec7f2SPyun YongHyeon 	sc->ifp->if_oerrors++;
1451744ec7f2SPyun YongHyeon 	sc->ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
14524d52a575SXin LI 	et_init_locked(sc);
145305884511SPyun YongHyeon 	return (EJUSTRETURN);
14544d52a575SXin LI }
14554d52a575SXin LI 
14564d52a575SXin LI static int
14574d52a575SXin LI et_stop_rxdma(struct et_softc *sc)
14584d52a575SXin LI {
14594d52a575SXin LI 	CSR_WRITE_4(sc, ET_RXDMA_CTRL,
14604d52a575SXin LI 		    ET_RXDMA_CTRL_HALT | ET_RXDMA_CTRL_RING1_ENABLE);
14614d52a575SXin LI 
14624d52a575SXin LI 	DELAY(5);
14634d52a575SXin LI 	if ((CSR_READ_4(sc, ET_RXDMA_CTRL) & ET_RXDMA_CTRL_HALTED) == 0) {
14644d52a575SXin LI 		if_printf(sc->ifp, "can't stop RX DMA engine\n");
1465398f1b65SPyun YongHyeon 		return (ETIMEDOUT);
14664d52a575SXin LI 	}
1467398f1b65SPyun YongHyeon 	return (0);
14684d52a575SXin LI }
14694d52a575SXin LI 
14704d52a575SXin LI static int
14714d52a575SXin LI et_stop_txdma(struct et_softc *sc)
14724d52a575SXin LI {
14734d52a575SXin LI 	CSR_WRITE_4(sc, ET_TXDMA_CTRL,
14744d52a575SXin LI 		    ET_TXDMA_CTRL_HALT | ET_TXDMA_CTRL_SINGLE_EPKT);
1475398f1b65SPyun YongHyeon 	return (0);
14764d52a575SXin LI }
14774d52a575SXin LI 
14784d52a575SXin LI static void
14794d52a575SXin LI et_free_tx_ring(struct et_softc *sc)
14804d52a575SXin LI {
148105884511SPyun YongHyeon 	struct et_txdesc_ring *tx_ring;
148205884511SPyun YongHyeon 	struct et_txbuf_data *tbd;
148305884511SPyun YongHyeon 	struct et_txbuf *tb;
14844d52a575SXin LI 	int i;
14854d52a575SXin LI 
148605884511SPyun YongHyeon 	tbd = &sc->sc_tx_data;
148705884511SPyun YongHyeon 	tx_ring = &sc->sc_tx_ring;
14884d52a575SXin LI 	for (i = 0; i < ET_TX_NDESC; ++i) {
148905884511SPyun YongHyeon 		tb = &tbd->tbd_buf[i];
14904d52a575SXin LI 		if (tb->tb_mbuf != NULL) {
149105884511SPyun YongHyeon 			bus_dmamap_sync(sc->sc_tx_tag, tb->tb_dmap,
149205884511SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
14934d52a575SXin LI 			bus_dmamap_unload(sc->sc_mbuf_dtag, tb->tb_dmap);
14944d52a575SXin LI 			m_freem(tb->tb_mbuf);
14954d52a575SXin LI 			tb->tb_mbuf = NULL;
14964d52a575SXin LI 		}
14974d52a575SXin LI 	}
14984d52a575SXin LI }
14994d52a575SXin LI 
15004d52a575SXin LI static void
15014d52a575SXin LI et_free_rx_ring(struct et_softc *sc)
15024d52a575SXin LI {
150305884511SPyun YongHyeon 	struct et_rxbuf_data *rbd;
150405884511SPyun YongHyeon 	struct et_rxdesc_ring *rx_ring;
150505884511SPyun YongHyeon 	struct et_rxbuf *rb;
15064d52a575SXin LI 	int i;
15074d52a575SXin LI 
150805884511SPyun YongHyeon 	/* Ring 0 */
150905884511SPyun YongHyeon 	rx_ring = &sc->sc_rx_ring[0];
151005884511SPyun YongHyeon 	rbd = &sc->sc_rx_data[0];
15114d52a575SXin LI 	for (i = 0; i < ET_RX_NDESC; ++i) {
151205884511SPyun YongHyeon 		rb = &rbd->rbd_buf[i];
15134d52a575SXin LI 		if (rb->rb_mbuf != NULL) {
151405884511SPyun YongHyeon 			bus_dmamap_sync(sc->sc_rx_mini_tag, rx_ring->rr_dmap,
151505884511SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
151605884511SPyun YongHyeon 			bus_dmamap_unload(sc->sc_rx_mini_tag, rb->rb_dmap);
15174d52a575SXin LI 			m_freem(rb->rb_mbuf);
15184d52a575SXin LI 			rb->rb_mbuf = NULL;
15194d52a575SXin LI 		}
15204d52a575SXin LI 	}
15214d52a575SXin LI 
152205884511SPyun YongHyeon 	/* Ring 1 */
152305884511SPyun YongHyeon 	rx_ring = &sc->sc_rx_ring[1];
152405884511SPyun YongHyeon 	rbd = &sc->sc_rx_data[1];
152505884511SPyun YongHyeon 	for (i = 0; i < ET_RX_NDESC; ++i) {
152605884511SPyun YongHyeon 		rb = &rbd->rbd_buf[i];
152705884511SPyun YongHyeon 		if (rb->rb_mbuf != NULL) {
152805884511SPyun YongHyeon 			bus_dmamap_sync(sc->sc_rx_tag, rx_ring->rr_dmap,
152905884511SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
153005884511SPyun YongHyeon 			bus_dmamap_unload(sc->sc_rx_tag, rb->rb_dmap);
153105884511SPyun YongHyeon 			m_freem(rb->rb_mbuf);
153205884511SPyun YongHyeon 			rb->rb_mbuf = NULL;
153305884511SPyun YongHyeon 		}
15344d52a575SXin LI 	}
15354d52a575SXin LI }
15364d52a575SXin LI 
15374d52a575SXin LI static void
15384d52a575SXin LI et_setmulti(struct et_softc *sc)
15394d52a575SXin LI {
15404d52a575SXin LI 	struct ifnet *ifp;
15414d52a575SXin LI 	uint32_t hash[4] = { 0, 0, 0, 0 };
15424d52a575SXin LI 	uint32_t rxmac_ctrl, pktfilt;
15434d52a575SXin LI 	struct ifmultiaddr *ifma;
15444d52a575SXin LI 	int i, count;
15454d52a575SXin LI 
15464d52a575SXin LI 	ET_LOCK_ASSERT(sc);
15474d52a575SXin LI 	ifp = sc->ifp;
15484d52a575SXin LI 
15494d52a575SXin LI 	pktfilt = CSR_READ_4(sc, ET_PKTFILT);
15504d52a575SXin LI 	rxmac_ctrl = CSR_READ_4(sc, ET_RXMAC_CTRL);
15514d52a575SXin LI 
15524d52a575SXin LI 	pktfilt &= ~(ET_PKTFILT_BCAST | ET_PKTFILT_MCAST | ET_PKTFILT_UCAST);
15534d52a575SXin LI 	if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI)) {
15544d52a575SXin LI 		rxmac_ctrl |= ET_RXMAC_CTRL_NO_PKTFILT;
15554d52a575SXin LI 		goto back;
15564d52a575SXin LI 	}
15574d52a575SXin LI 
15584d52a575SXin LI 	count = 0;
1559eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
15604d52a575SXin LI 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
15614d52a575SXin LI 		uint32_t *hp, h;
15624d52a575SXin LI 
15634d52a575SXin LI 		if (ifma->ifma_addr->sa_family != AF_LINK)
15644d52a575SXin LI 			continue;
15654d52a575SXin LI 
15664d52a575SXin LI 		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
15674d52a575SXin LI 				   ifma->ifma_addr), ETHER_ADDR_LEN);
15684d52a575SXin LI 		h = (h & 0x3f800000) >> 23;
15694d52a575SXin LI 
15704d52a575SXin LI 		hp = &hash[0];
15714d52a575SXin LI 		if (h >= 32 && h < 64) {
15724d52a575SXin LI 			h -= 32;
15734d52a575SXin LI 			hp = &hash[1];
15744d52a575SXin LI 		} else if (h >= 64 && h < 96) {
15754d52a575SXin LI 			h -= 64;
15764d52a575SXin LI 			hp = &hash[2];
15774d52a575SXin LI 		} else if (h >= 96) {
15784d52a575SXin LI 			h -= 96;
15794d52a575SXin LI 			hp = &hash[3];
15804d52a575SXin LI 		}
15814d52a575SXin LI 		*hp |= (1 << h);
15824d52a575SXin LI 
15834d52a575SXin LI 		++count;
15844d52a575SXin LI 	}
1585eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
15864d52a575SXin LI 
15874d52a575SXin LI 	for (i = 0; i < 4; ++i)
15884d52a575SXin LI 		CSR_WRITE_4(sc, ET_MULTI_HASH + (i * 4), hash[i]);
15894d52a575SXin LI 
15904d52a575SXin LI 	if (count > 0)
15914d52a575SXin LI 		pktfilt |= ET_PKTFILT_MCAST;
15924d52a575SXin LI 	rxmac_ctrl &= ~ET_RXMAC_CTRL_NO_PKTFILT;
15934d52a575SXin LI back:
15944d52a575SXin LI 	CSR_WRITE_4(sc, ET_PKTFILT, pktfilt);
15954d52a575SXin LI 	CSR_WRITE_4(sc, ET_RXMAC_CTRL, rxmac_ctrl);
15964d52a575SXin LI }
15974d52a575SXin LI 
15984d52a575SXin LI static int
15994d52a575SXin LI et_chip_init(struct et_softc *sc)
16004d52a575SXin LI {
16014d52a575SXin LI 	struct ifnet *ifp = sc->ifp;
16024d52a575SXin LI 	uint32_t rxq_end;
16034d52a575SXin LI 	int error, frame_len, rxmem_size;
16044d52a575SXin LI 
16054d52a575SXin LI 	/*
16064d52a575SXin LI 	 * Split 16Kbytes internal memory between TX and RX
16074d52a575SXin LI 	 * according to frame length.
16084d52a575SXin LI 	 */
16094d52a575SXin LI 	frame_len = ET_FRAMELEN(ifp->if_mtu);
16104d52a575SXin LI 	if (frame_len < 2048) {
16114d52a575SXin LI 		rxmem_size = ET_MEM_RXSIZE_DEFAULT;
16124d52a575SXin LI 	} else if (frame_len <= ET_RXMAC_CUT_THRU_FRMLEN) {
16134d52a575SXin LI 		rxmem_size = ET_MEM_SIZE / 2;
16144d52a575SXin LI 	} else {
16154d52a575SXin LI 		rxmem_size = ET_MEM_SIZE -
16164d52a575SXin LI 		roundup(frame_len + ET_MEM_TXSIZE_EX, ET_MEM_UNIT);
16174d52a575SXin LI 	}
16184d52a575SXin LI 	rxq_end = ET_QUEUE_ADDR(rxmem_size);
16194d52a575SXin LI 
16204d52a575SXin LI 	CSR_WRITE_4(sc, ET_RXQUEUE_START, ET_QUEUE_ADDR_START);
16214d52a575SXin LI 	CSR_WRITE_4(sc, ET_RXQUEUE_END, rxq_end);
16224d52a575SXin LI 	CSR_WRITE_4(sc, ET_TXQUEUE_START, rxq_end + 1);
16234d52a575SXin LI 	CSR_WRITE_4(sc, ET_TXQUEUE_END, ET_QUEUE_ADDR_END);
16244d52a575SXin LI 
16254d52a575SXin LI 	/* No loopback */
16264d52a575SXin LI 	CSR_WRITE_4(sc, ET_LOOPBACK, 0);
16274d52a575SXin LI 
16284d52a575SXin LI 	/* Clear MSI configure */
1629cc3c3b4eSPyun YongHyeon 	if ((sc->sc_flags & ET_FLAG_MSI) == 0)
16304d52a575SXin LI 		CSR_WRITE_4(sc, ET_MSI_CFG, 0);
16314d52a575SXin LI 
16324d52a575SXin LI 	/* Disable timer */
16334d52a575SXin LI 	CSR_WRITE_4(sc, ET_TIMER, 0);
16344d52a575SXin LI 
16354d52a575SXin LI 	/* Initialize MAC */
16364d52a575SXin LI 	et_init_mac(sc);
16374d52a575SXin LI 
16384d52a575SXin LI 	/* Enable memory controllers */
16394d52a575SXin LI 	CSR_WRITE_4(sc, ET_MMC_CTRL, ET_MMC_CTRL_ENABLE);
16404d52a575SXin LI 
16414d52a575SXin LI 	/* Initialize RX MAC */
16424d52a575SXin LI 	et_init_rxmac(sc);
16434d52a575SXin LI 
16444d52a575SXin LI 	/* Initialize TX MAC */
16454d52a575SXin LI 	et_init_txmac(sc);
16464d52a575SXin LI 
16474d52a575SXin LI 	/* Initialize RX DMA engine */
16484d52a575SXin LI 	error = et_init_rxdma(sc);
16494d52a575SXin LI 	if (error)
1650398f1b65SPyun YongHyeon 		return (error);
16514d52a575SXin LI 
16524d52a575SXin LI 	/* Initialize TX DMA engine */
16534d52a575SXin LI 	error = et_init_txdma(sc);
16544d52a575SXin LI 	if (error)
1655398f1b65SPyun YongHyeon 		return (error);
16564d52a575SXin LI 
1657398f1b65SPyun YongHyeon 	return (0);
16584d52a575SXin LI }
16594d52a575SXin LI 
166005884511SPyun YongHyeon static void
16614d52a575SXin LI et_init_tx_ring(struct et_softc *sc)
16624d52a575SXin LI {
166305884511SPyun YongHyeon 	struct et_txdesc_ring *tx_ring;
166405884511SPyun YongHyeon 	struct et_txbuf_data *tbd;
166505884511SPyun YongHyeon 	struct et_txstatus_data *txsd;
16664d52a575SXin LI 
166705884511SPyun YongHyeon 	tx_ring = &sc->sc_tx_ring;
16684d52a575SXin LI 	bzero(tx_ring->tr_desc, ET_TX_RING_SIZE);
16694d52a575SXin LI 	bus_dmamap_sync(tx_ring->tr_dtag, tx_ring->tr_dmap,
16704d52a575SXin LI 	    BUS_DMASYNC_PREWRITE);
16714d52a575SXin LI 
167205884511SPyun YongHyeon 	tbd = &sc->sc_tx_data;
16734d52a575SXin LI 	tbd->tbd_start_index = 0;
16744d52a575SXin LI 	tbd->tbd_start_wrap = 0;
16754d52a575SXin LI 	tbd->tbd_used = 0;
16764d52a575SXin LI 
167705884511SPyun YongHyeon 	txsd = &sc->sc_tx_status;
16784d52a575SXin LI 	bzero(txsd->txsd_status, sizeof(uint32_t));
16794d52a575SXin LI 	bus_dmamap_sync(txsd->txsd_dtag, txsd->txsd_dmap,
168005884511SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
16814d52a575SXin LI }
16824d52a575SXin LI 
16834d52a575SXin LI static int
16844d52a575SXin LI et_init_rx_ring(struct et_softc *sc)
16854d52a575SXin LI {
168605884511SPyun YongHyeon 	struct et_rxstatus_data *rxsd;
168705884511SPyun YongHyeon 	struct et_rxstat_ring *rxst_ring;
168805884511SPyun YongHyeon 	struct et_rxbuf_data *rbd;
168905884511SPyun YongHyeon 	int i, error, n;
16904d52a575SXin LI 
16914d52a575SXin LI 	for (n = 0; n < ET_RX_NRING; ++n) {
169205884511SPyun YongHyeon 		rbd = &sc->sc_rx_data[n];
16934d52a575SXin LI 		for (i = 0; i < ET_RX_NDESC; ++i) {
169405884511SPyun YongHyeon 			error = rbd->rbd_newbuf(rbd, i);
16954d52a575SXin LI 			if (error) {
16964d52a575SXin LI 				if_printf(sc->ifp, "%d ring %d buf, "
16974d52a575SXin LI 					  "newbuf failed: %d\n", n, i, error);
1698398f1b65SPyun YongHyeon 				return (error);
16994d52a575SXin LI 			}
17004d52a575SXin LI 		}
17014d52a575SXin LI 	}
17024d52a575SXin LI 
170305884511SPyun YongHyeon 	rxsd = &sc->sc_rx_status;
17044d52a575SXin LI 	bzero(rxsd->rxsd_status, sizeof(struct et_rxstatus));
17054d52a575SXin LI 	bus_dmamap_sync(rxsd->rxsd_dtag, rxsd->rxsd_dmap,
170605884511SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
17074d52a575SXin LI 
170805884511SPyun YongHyeon 	rxst_ring = &sc->sc_rxstat_ring;
17094d52a575SXin LI 	bzero(rxst_ring->rsr_stat, ET_RXSTAT_RING_SIZE);
17104d52a575SXin LI 	bus_dmamap_sync(rxst_ring->rsr_dtag, rxst_ring->rsr_dmap,
171105884511SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
17124d52a575SXin LI 
1713398f1b65SPyun YongHyeon 	return (0);
17144d52a575SXin LI }
17154d52a575SXin LI 
17164d52a575SXin LI static int
17174d52a575SXin LI et_init_rxdma(struct et_softc *sc)
17184d52a575SXin LI {
17194d52a575SXin LI 	struct et_rxstatus_data *rxsd = &sc->sc_rx_status;
17204d52a575SXin LI 	struct et_rxstat_ring *rxst_ring = &sc->sc_rxstat_ring;
17214d52a575SXin LI 	struct et_rxdesc_ring *rx_ring;
17224d52a575SXin LI 	int error;
17234d52a575SXin LI 
17244d52a575SXin LI 	error = et_stop_rxdma(sc);
17254d52a575SXin LI 	if (error) {
17264d52a575SXin LI 		if_printf(sc->ifp, "can't init RX DMA engine\n");
1727398f1b65SPyun YongHyeon 		return (error);
17284d52a575SXin LI 	}
17294d52a575SXin LI 
17304d52a575SXin LI 	/*
17314d52a575SXin LI 	 * Install RX status
17324d52a575SXin LI 	 */
17334d52a575SXin LI 	CSR_WRITE_4(sc, ET_RX_STATUS_HI, ET_ADDR_HI(rxsd->rxsd_paddr));
17344d52a575SXin LI 	CSR_WRITE_4(sc, ET_RX_STATUS_LO, ET_ADDR_LO(rxsd->rxsd_paddr));
17354d52a575SXin LI 
17364d52a575SXin LI 	/*
17374d52a575SXin LI 	 * Install RX stat ring
17384d52a575SXin LI 	 */
17394d52a575SXin LI 	CSR_WRITE_4(sc, ET_RXSTAT_HI, ET_ADDR_HI(rxst_ring->rsr_paddr));
17404d52a575SXin LI 	CSR_WRITE_4(sc, ET_RXSTAT_LO, ET_ADDR_LO(rxst_ring->rsr_paddr));
17414d52a575SXin LI 	CSR_WRITE_4(sc, ET_RXSTAT_CNT, ET_RX_NSTAT - 1);
17424d52a575SXin LI 	CSR_WRITE_4(sc, ET_RXSTAT_POS, 0);
17434d52a575SXin LI 	CSR_WRITE_4(sc, ET_RXSTAT_MINCNT, ((ET_RX_NSTAT * 15) / 100) - 1);
17444d52a575SXin LI 
17454d52a575SXin LI 	/* Match ET_RXSTAT_POS */
17464d52a575SXin LI 	rxst_ring->rsr_index = 0;
17474d52a575SXin LI 	rxst_ring->rsr_wrap = 0;
17484d52a575SXin LI 
17494d52a575SXin LI 	/*
17504d52a575SXin LI 	 * Install the 2nd RX descriptor ring
17514d52a575SXin LI 	 */
17524d52a575SXin LI 	rx_ring = &sc->sc_rx_ring[1];
17534d52a575SXin LI 	CSR_WRITE_4(sc, ET_RX_RING1_HI, ET_ADDR_HI(rx_ring->rr_paddr));
17544d52a575SXin LI 	CSR_WRITE_4(sc, ET_RX_RING1_LO, ET_ADDR_LO(rx_ring->rr_paddr));
17554d52a575SXin LI 	CSR_WRITE_4(sc, ET_RX_RING1_CNT, ET_RX_NDESC - 1);
17564d52a575SXin LI 	CSR_WRITE_4(sc, ET_RX_RING1_POS, ET_RX_RING1_POS_WRAP);
17574d52a575SXin LI 	CSR_WRITE_4(sc, ET_RX_RING1_MINCNT, ((ET_RX_NDESC * 15) / 100) - 1);
17584d52a575SXin LI 
17594d52a575SXin LI 	/* Match ET_RX_RING1_POS */
17604d52a575SXin LI 	rx_ring->rr_index = 0;
17614d52a575SXin LI 	rx_ring->rr_wrap = 1;
17624d52a575SXin LI 
17634d52a575SXin LI 	/*
17644d52a575SXin LI 	 * Install the 1st RX descriptor ring
17654d52a575SXin LI 	 */
17664d52a575SXin LI 	rx_ring = &sc->sc_rx_ring[0];
17674d52a575SXin LI 	CSR_WRITE_4(sc, ET_RX_RING0_HI, ET_ADDR_HI(rx_ring->rr_paddr));
17684d52a575SXin LI 	CSR_WRITE_4(sc, ET_RX_RING0_LO, ET_ADDR_LO(rx_ring->rr_paddr));
17694d52a575SXin LI 	CSR_WRITE_4(sc, ET_RX_RING0_CNT, ET_RX_NDESC - 1);
17704d52a575SXin LI 	CSR_WRITE_4(sc, ET_RX_RING0_POS, ET_RX_RING0_POS_WRAP);
17714d52a575SXin LI 	CSR_WRITE_4(sc, ET_RX_RING0_MINCNT, ((ET_RX_NDESC * 15) / 100) - 1);
17724d52a575SXin LI 
17734d52a575SXin LI 	/* Match ET_RX_RING0_POS */
17744d52a575SXin LI 	rx_ring->rr_index = 0;
17754d52a575SXin LI 	rx_ring->rr_wrap = 1;
17764d52a575SXin LI 
17774d52a575SXin LI 	/*
17784d52a575SXin LI 	 * RX intr moderation
17794d52a575SXin LI 	 */
17804d52a575SXin LI 	CSR_WRITE_4(sc, ET_RX_INTR_NPKTS, sc->sc_rx_intr_npkts);
17814d52a575SXin LI 	CSR_WRITE_4(sc, ET_RX_INTR_DELAY, sc->sc_rx_intr_delay);
17824d52a575SXin LI 
1783398f1b65SPyun YongHyeon 	return (0);
17844d52a575SXin LI }
17854d52a575SXin LI 
17864d52a575SXin LI static int
17874d52a575SXin LI et_init_txdma(struct et_softc *sc)
17884d52a575SXin LI {
17894d52a575SXin LI 	struct et_txdesc_ring *tx_ring = &sc->sc_tx_ring;
17904d52a575SXin LI 	struct et_txstatus_data *txsd = &sc->sc_tx_status;
17914d52a575SXin LI 	int error;
17924d52a575SXin LI 
17934d52a575SXin LI 	error = et_stop_txdma(sc);
17944d52a575SXin LI 	if (error) {
17954d52a575SXin LI 		if_printf(sc->ifp, "can't init TX DMA engine\n");
1796398f1b65SPyun YongHyeon 		return (error);
17974d52a575SXin LI 	}
17984d52a575SXin LI 
17994d52a575SXin LI 	/*
18004d52a575SXin LI 	 * Install TX descriptor ring
18014d52a575SXin LI 	 */
18024d52a575SXin LI 	CSR_WRITE_4(sc, ET_TX_RING_HI, ET_ADDR_HI(tx_ring->tr_paddr));
18034d52a575SXin LI 	CSR_WRITE_4(sc, ET_TX_RING_LO, ET_ADDR_LO(tx_ring->tr_paddr));
18044d52a575SXin LI 	CSR_WRITE_4(sc, ET_TX_RING_CNT, ET_TX_NDESC - 1);
18054d52a575SXin LI 
18064d52a575SXin LI 	/*
18074d52a575SXin LI 	 * Install TX status
18084d52a575SXin LI 	 */
18094d52a575SXin LI 	CSR_WRITE_4(sc, ET_TX_STATUS_HI, ET_ADDR_HI(txsd->txsd_paddr));
18104d52a575SXin LI 	CSR_WRITE_4(sc, ET_TX_STATUS_LO, ET_ADDR_LO(txsd->txsd_paddr));
18114d52a575SXin LI 
18124d52a575SXin LI 	CSR_WRITE_4(sc, ET_TX_READY_POS, 0);
18134d52a575SXin LI 
18144d52a575SXin LI 	/* Match ET_TX_READY_POS */
18154d52a575SXin LI 	tx_ring->tr_ready_index = 0;
18164d52a575SXin LI 	tx_ring->tr_ready_wrap = 0;
18174d52a575SXin LI 
1818398f1b65SPyun YongHyeon 	return (0);
18194d52a575SXin LI }
18204d52a575SXin LI 
18214d52a575SXin LI static void
18224d52a575SXin LI et_init_mac(struct et_softc *sc)
18234d52a575SXin LI {
18244d52a575SXin LI 	struct ifnet *ifp = sc->ifp;
18254d52a575SXin LI 	const uint8_t *eaddr = IF_LLADDR(ifp);
18264d52a575SXin LI 	uint32_t val;
18274d52a575SXin LI 
18284d52a575SXin LI 	/* Reset MAC */
18294d52a575SXin LI 	CSR_WRITE_4(sc, ET_MAC_CFG1,
18304d52a575SXin LI 		    ET_MAC_CFG1_RST_TXFUNC | ET_MAC_CFG1_RST_RXFUNC |
18314d52a575SXin LI 		    ET_MAC_CFG1_RST_TXMC | ET_MAC_CFG1_RST_RXMC |
18324d52a575SXin LI 		    ET_MAC_CFG1_SIM_RST | ET_MAC_CFG1_SOFT_RST);
18334d52a575SXin LI 
18344d52a575SXin LI 	/*
18354d52a575SXin LI 	 * Setup inter packet gap
18364d52a575SXin LI 	 */
183723263665SPyun YongHyeon 	val = (56 << ET_IPG_NONB2B_1_SHIFT) |
183823263665SPyun YongHyeon 	    (88 << ET_IPG_NONB2B_2_SHIFT) |
183923263665SPyun YongHyeon 	    (80 << ET_IPG_MINIFG_SHIFT) |
184023263665SPyun YongHyeon 	    (96 << ET_IPG_B2B_SHIFT);
18414d52a575SXin LI 	CSR_WRITE_4(sc, ET_IPG, val);
18424d52a575SXin LI 
18434d52a575SXin LI 	/*
18444d52a575SXin LI 	 * Setup half duplex mode
18454d52a575SXin LI 	 */
184623263665SPyun YongHyeon 	val = (10 << ET_MAC_HDX_ALT_BEB_TRUNC_SHIFT) |
184723263665SPyun YongHyeon 	    (15 << ET_MAC_HDX_REXMIT_MAX_SHIFT) |
184823263665SPyun YongHyeon 	    (55 << ET_MAC_HDX_COLLWIN_SHIFT) |
18494d52a575SXin LI 	    ET_MAC_HDX_EXC_DEFER;
18504d52a575SXin LI 	CSR_WRITE_4(sc, ET_MAC_HDX, val);
18514d52a575SXin LI 
18524d52a575SXin LI 	/* Clear MAC control */
18534d52a575SXin LI 	CSR_WRITE_4(sc, ET_MAC_CTRL, 0);
18544d52a575SXin LI 
18554d52a575SXin LI 	/* Reset MII */
18564d52a575SXin LI 	CSR_WRITE_4(sc, ET_MII_CFG, ET_MII_CFG_CLKRST);
18574d52a575SXin LI 
18584d52a575SXin LI 	/*
18594d52a575SXin LI 	 * Set MAC address
18604d52a575SXin LI 	 */
18614d52a575SXin LI 	val = eaddr[2] | (eaddr[3] << 8) | (eaddr[4] << 16) | (eaddr[5] << 24);
18624d52a575SXin LI 	CSR_WRITE_4(sc, ET_MAC_ADDR1, val);
18634d52a575SXin LI 	val = (eaddr[0] << 16) | (eaddr[1] << 24);
18644d52a575SXin LI 	CSR_WRITE_4(sc, ET_MAC_ADDR2, val);
18654d52a575SXin LI 
18664d52a575SXin LI 	/* Set max frame length */
18674d52a575SXin LI 	CSR_WRITE_4(sc, ET_MAX_FRMLEN, ET_FRAMELEN(ifp->if_mtu));
18684d52a575SXin LI 
18694d52a575SXin LI 	/* Bring MAC out of reset state */
18704d52a575SXin LI 	CSR_WRITE_4(sc, ET_MAC_CFG1, 0);
18714d52a575SXin LI }
18724d52a575SXin LI 
18734d52a575SXin LI static void
18744d52a575SXin LI et_init_rxmac(struct et_softc *sc)
18754d52a575SXin LI {
18764d52a575SXin LI 	struct ifnet *ifp = sc->ifp;
18774d52a575SXin LI 	const uint8_t *eaddr = IF_LLADDR(ifp);
18784d52a575SXin LI 	uint32_t val;
18794d52a575SXin LI 	int i;
18804d52a575SXin LI 
18814d52a575SXin LI 	/* Disable RX MAC and WOL */
18824d52a575SXin LI 	CSR_WRITE_4(sc, ET_RXMAC_CTRL, ET_RXMAC_CTRL_WOL_DISABLE);
18834d52a575SXin LI 
18844d52a575SXin LI 	/*
18854d52a575SXin LI 	 * Clear all WOL related registers
18864d52a575SXin LI 	 */
18874d52a575SXin LI 	for (i = 0; i < 3; ++i)
18884d52a575SXin LI 		CSR_WRITE_4(sc, ET_WOL_CRC + (i * 4), 0);
18894d52a575SXin LI 	for (i = 0; i < 20; ++i)
18904d52a575SXin LI 		CSR_WRITE_4(sc, ET_WOL_MASK + (i * 4), 0);
18914d52a575SXin LI 
18924d52a575SXin LI 	/*
18934d52a575SXin LI 	 * Set WOL source address.  XXX is this necessary?
18944d52a575SXin LI 	 */
18954d52a575SXin LI 	val = (eaddr[2] << 24) | (eaddr[3] << 16) | (eaddr[4] << 8) | eaddr[5];
18964d52a575SXin LI 	CSR_WRITE_4(sc, ET_WOL_SA_LO, val);
18974d52a575SXin LI 	val = (eaddr[0] << 8) | eaddr[1];
18984d52a575SXin LI 	CSR_WRITE_4(sc, ET_WOL_SA_HI, val);
18994d52a575SXin LI 
19004d52a575SXin LI 	/* Clear packet filters */
19014d52a575SXin LI 	CSR_WRITE_4(sc, ET_PKTFILT, 0);
19024d52a575SXin LI 
19034d52a575SXin LI 	/* No ucast filtering */
19044d52a575SXin LI 	CSR_WRITE_4(sc, ET_UCAST_FILTADDR1, 0);
19054d52a575SXin LI 	CSR_WRITE_4(sc, ET_UCAST_FILTADDR2, 0);
19064d52a575SXin LI 	CSR_WRITE_4(sc, ET_UCAST_FILTADDR3, 0);
19074d52a575SXin LI 
19084d52a575SXin LI 	if (ET_FRAMELEN(ifp->if_mtu) > ET_RXMAC_CUT_THRU_FRMLEN) {
19094d52a575SXin LI 		/*
19104d52a575SXin LI 		 * In order to transmit jumbo packets greater than
19114d52a575SXin LI 		 * ET_RXMAC_CUT_THRU_FRMLEN bytes, the FIFO between
19124d52a575SXin LI 		 * RX MAC and RX DMA needs to be reduced in size to
19134d52a575SXin LI 		 * (ET_MEM_SIZE - ET_MEM_TXSIZE_EX - framelen).  In
19144d52a575SXin LI 		 * order to implement this, we must use "cut through"
19154d52a575SXin LI 		 * mode in the RX MAC, which chops packets down into
19164d52a575SXin LI 		 * segments.  In this case we selected 256 bytes,
19174d52a575SXin LI 		 * since this is the size of the PCI-Express TLP's
19184d52a575SXin LI 		 * that the ET1310 uses.
19194d52a575SXin LI 		 */
192023263665SPyun YongHyeon 		val = (ET_RXMAC_SEGSZ(256) & ET_RXMAC_MC_SEGSZ_MAX_MASK) |
19214d52a575SXin LI 		      ET_RXMAC_MC_SEGSZ_ENABLE;
19224d52a575SXin LI 	} else {
19234d52a575SXin LI 		val = 0;
19244d52a575SXin LI 	}
19254d52a575SXin LI 	CSR_WRITE_4(sc, ET_RXMAC_MC_SEGSZ, val);
19264d52a575SXin LI 
19274d52a575SXin LI 	CSR_WRITE_4(sc, ET_RXMAC_MC_WATERMARK, 0);
19284d52a575SXin LI 
19294d52a575SXin LI 	/* Initialize RX MAC management register */
19304d52a575SXin LI 	CSR_WRITE_4(sc, ET_RXMAC_MGT, 0);
19314d52a575SXin LI 
19324d52a575SXin LI 	CSR_WRITE_4(sc, ET_RXMAC_SPACE_AVL, 0);
19334d52a575SXin LI 
19344d52a575SXin LI 	CSR_WRITE_4(sc, ET_RXMAC_MGT,
19354d52a575SXin LI 		    ET_RXMAC_MGT_PASS_ECRC |
19364d52a575SXin LI 		    ET_RXMAC_MGT_PASS_ELEN |
19374d52a575SXin LI 		    ET_RXMAC_MGT_PASS_ETRUNC |
19384d52a575SXin LI 		    ET_RXMAC_MGT_CHECK_PKT);
19394d52a575SXin LI 
19404d52a575SXin LI 	/*
19414d52a575SXin LI 	 * Configure runt filtering (may not work on certain chip generation)
19424d52a575SXin LI 	 */
194323263665SPyun YongHyeon 	val = (ETHER_MIN_LEN << ET_PKTFILT_MINLEN_SHIFT) &
194423263665SPyun YongHyeon 	    ET_PKTFILT_MINLEN_MASK;
194523263665SPyun YongHyeon 	val |= ET_PKTFILT_FRAG;
19464d52a575SXin LI 	CSR_WRITE_4(sc, ET_PKTFILT, val);
19474d52a575SXin LI 
19484d52a575SXin LI 	/* Enable RX MAC but leave WOL disabled */
19494d52a575SXin LI 	CSR_WRITE_4(sc, ET_RXMAC_CTRL,
19504d52a575SXin LI 		    ET_RXMAC_CTRL_WOL_DISABLE | ET_RXMAC_CTRL_ENABLE);
19514d52a575SXin LI 
19524d52a575SXin LI 	/*
19534d52a575SXin LI 	 * Setup multicast hash and allmulti/promisc mode
19544d52a575SXin LI 	 */
19554d52a575SXin LI 	et_setmulti(sc);
19564d52a575SXin LI }
19574d52a575SXin LI 
19584d52a575SXin LI static void
19594d52a575SXin LI et_init_txmac(struct et_softc *sc)
19604d52a575SXin LI {
19614d52a575SXin LI 	/* Disable TX MAC and FC(?) */
19624d52a575SXin LI 	CSR_WRITE_4(sc, ET_TXMAC_CTRL, ET_TXMAC_CTRL_FC_DISABLE);
19634d52a575SXin LI 
1964*5d384a0dSPyun YongHyeon 	/*
1965*5d384a0dSPyun YongHyeon 	 * Initialize pause time.
1966*5d384a0dSPyun YongHyeon 	 * This register should be set before XON/XOFF frame is
1967*5d384a0dSPyun YongHyeon 	 * sent by driver.
1968*5d384a0dSPyun YongHyeon 	 */
1969*5d384a0dSPyun YongHyeon 	CSR_WRITE_4(sc, ET_TXMAC_FLOWCTRL, 0 << ET_TXMAC_FLOWCTRL_CFPT_SHIFT);
19704d52a575SXin LI 
19714d52a575SXin LI 	/* Enable TX MAC but leave FC(?) diabled */
19724d52a575SXin LI 	CSR_WRITE_4(sc, ET_TXMAC_CTRL,
19734d52a575SXin LI 		    ET_TXMAC_CTRL_ENABLE | ET_TXMAC_CTRL_FC_DISABLE);
19744d52a575SXin LI }
19754d52a575SXin LI 
19764d52a575SXin LI static int
19774d52a575SXin LI et_start_rxdma(struct et_softc *sc)
19784d52a575SXin LI {
19794d52a575SXin LI 	uint32_t val = 0;
19804d52a575SXin LI 
198123263665SPyun YongHyeon 	val |= (sc->sc_rx_data[0].rbd_bufsize & ET_RXDMA_CTRL_RING0_SIZE_MASK) |
19824d52a575SXin LI 	       ET_RXDMA_CTRL_RING0_ENABLE;
198323263665SPyun YongHyeon 	val |= (sc->sc_rx_data[1].rbd_bufsize & ET_RXDMA_CTRL_RING1_SIZE_MASK) |
19844d52a575SXin LI 	       ET_RXDMA_CTRL_RING1_ENABLE;
19854d52a575SXin LI 
19864d52a575SXin LI 	CSR_WRITE_4(sc, ET_RXDMA_CTRL, val);
19874d52a575SXin LI 
19884d52a575SXin LI 	DELAY(5);
19894d52a575SXin LI 
19904d52a575SXin LI 	if (CSR_READ_4(sc, ET_RXDMA_CTRL) & ET_RXDMA_CTRL_HALTED) {
19914d52a575SXin LI 		if_printf(sc->ifp, "can't start RX DMA engine\n");
1992398f1b65SPyun YongHyeon 		return (ETIMEDOUT);
19934d52a575SXin LI 	}
1994398f1b65SPyun YongHyeon 	return (0);
19954d52a575SXin LI }
19964d52a575SXin LI 
19974d52a575SXin LI static int
19984d52a575SXin LI et_start_txdma(struct et_softc *sc)
19994d52a575SXin LI {
20004d52a575SXin LI 	CSR_WRITE_4(sc, ET_TXDMA_CTRL, ET_TXDMA_CTRL_SINGLE_EPKT);
2001398f1b65SPyun YongHyeon 	return (0);
20024d52a575SXin LI }
20034d52a575SXin LI 
20044d52a575SXin LI static void
20054d52a575SXin LI et_rxeof(struct et_softc *sc)
20064d52a575SXin LI {
20074d52a575SXin LI 	struct et_rxstatus_data *rxsd;
20084d52a575SXin LI 	struct et_rxstat_ring *rxst_ring;
200905884511SPyun YongHyeon 	struct et_rxbuf_data *rbd;
201005884511SPyun YongHyeon 	struct et_rxdesc_ring *rx_ring;
201105884511SPyun YongHyeon 	struct et_rxstat *st;
201205884511SPyun YongHyeon 	struct ifnet *ifp;
201305884511SPyun YongHyeon 	struct mbuf *m;
201405884511SPyun YongHyeon 	uint32_t rxstat_pos, rxring_pos;
201505884511SPyun YongHyeon 	uint32_t rxst_info1, rxst_info2, rxs_stat_ring;
201605884511SPyun YongHyeon 	int buflen, buf_idx, npost[2], ring_idx;
201705884511SPyun YongHyeon 	int rxst_index, rxst_wrap;
20184d52a575SXin LI 
20194d52a575SXin LI 	ET_LOCK_ASSERT(sc);
202005884511SPyun YongHyeon 
20214d52a575SXin LI 	ifp = sc->ifp;
20224d52a575SXin LI 	rxsd = &sc->sc_rx_status;
20234d52a575SXin LI 	rxst_ring = &sc->sc_rxstat_ring;
20244d52a575SXin LI 
20254d52a575SXin LI 	if ((sc->sc_flags & ET_FLAG_TXRX_ENABLED) == 0)
20264d52a575SXin LI 		return;
20274d52a575SXin LI 
20284d52a575SXin LI 	bus_dmamap_sync(rxsd->rxsd_dtag, rxsd->rxsd_dmap,
20294d52a575SXin LI 	    BUS_DMASYNC_POSTREAD);
20304d52a575SXin LI 	bus_dmamap_sync(rxst_ring->rsr_dtag, rxst_ring->rsr_dmap,
20314d52a575SXin LI 	    BUS_DMASYNC_POSTREAD);
20324d52a575SXin LI 
203305884511SPyun YongHyeon 	npost[0] = npost[1] = 0;
203426e07b50SPyun YongHyeon 	rxs_stat_ring = le32toh(rxsd->rxsd_status->rxs_stat_ring);
20354d52a575SXin LI 	rxst_wrap = (rxs_stat_ring & ET_RXS_STATRING_WRAP) ? 1 : 0;
203623263665SPyun YongHyeon 	rxst_index = (rxs_stat_ring & ET_RXS_STATRING_INDEX_MASK) >>
203723263665SPyun YongHyeon 	    ET_RXS_STATRING_INDEX_SHIFT;
20384d52a575SXin LI 
20394d52a575SXin LI 	while (rxst_index != rxst_ring->rsr_index ||
20404d52a575SXin LI 	    rxst_wrap != rxst_ring->rsr_wrap) {
204105884511SPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
204205884511SPyun YongHyeon 			break;
20434d52a575SXin LI 
20444d52a575SXin LI 		MPASS(rxst_ring->rsr_index < ET_RX_NSTAT);
20454d52a575SXin LI 		st = &rxst_ring->rsr_stat[rxst_ring->rsr_index];
204605884511SPyun YongHyeon 		rxst_info1 = le32toh(st->rxst_info1);
204726e07b50SPyun YongHyeon 		rxst_info2 = le32toh(st->rxst_info2);
204826e07b50SPyun YongHyeon 		buflen = (rxst_info2 & ET_RXST_INFO2_LEN_MASK) >>
204923263665SPyun YongHyeon 		    ET_RXST_INFO2_LEN_SHIFT;
205026e07b50SPyun YongHyeon 		buf_idx = (rxst_info2 & ET_RXST_INFO2_BUFIDX_MASK) >>
205123263665SPyun YongHyeon 		    ET_RXST_INFO2_BUFIDX_SHIFT;
205226e07b50SPyun YongHyeon 		ring_idx = (rxst_info2 & ET_RXST_INFO2_RINGIDX_MASK) >>
205323263665SPyun YongHyeon 		    ET_RXST_INFO2_RINGIDX_SHIFT;
20544d52a575SXin LI 
20554d52a575SXin LI 		if (++rxst_ring->rsr_index == ET_RX_NSTAT) {
20564d52a575SXin LI 			rxst_ring->rsr_index = 0;
20574d52a575SXin LI 			rxst_ring->rsr_wrap ^= 1;
20584d52a575SXin LI 		}
205923263665SPyun YongHyeon 		rxstat_pos = rxst_ring->rsr_index & ET_RXSTAT_POS_INDEX_MASK;
20604d52a575SXin LI 		if (rxst_ring->rsr_wrap)
20614d52a575SXin LI 			rxstat_pos |= ET_RXSTAT_POS_WRAP;
20624d52a575SXin LI 		CSR_WRITE_4(sc, ET_RXSTAT_POS, rxstat_pos);
20634d52a575SXin LI 
20644d52a575SXin LI 		if (ring_idx >= ET_RX_NRING) {
20654d52a575SXin LI 			ifp->if_ierrors++;
20664d52a575SXin LI 			if_printf(ifp, "invalid ring index %d\n", ring_idx);
20674d52a575SXin LI 			continue;
20684d52a575SXin LI 		}
20694d52a575SXin LI 		if (buf_idx >= ET_RX_NDESC) {
20704d52a575SXin LI 			ifp->if_ierrors++;
20714d52a575SXin LI 			if_printf(ifp, "invalid buf index %d\n", buf_idx);
20724d52a575SXin LI 			continue;
20734d52a575SXin LI 		}
20744d52a575SXin LI 
20754d52a575SXin LI 		rbd = &sc->sc_rx_data[ring_idx];
20764d52a575SXin LI 		m = rbd->rbd_buf[buf_idx].rb_mbuf;
207705884511SPyun YongHyeon 		if ((rxst_info1 & ET_RXST_INFO1_OK) == 0){
207805884511SPyun YongHyeon 			/* Discard errored frame. */
207905884511SPyun YongHyeon 			rbd->rbd_discard(rbd, buf_idx);
208005884511SPyun YongHyeon 		} else if (rbd->rbd_newbuf(rbd, buf_idx) != 0) {
208105884511SPyun YongHyeon 			/* No available mbufs, discard it. */
208205884511SPyun YongHyeon 			ifp->if_iqdrops++;
208305884511SPyun YongHyeon 			rbd->rbd_discard(rbd, buf_idx);
208405884511SPyun YongHyeon 		} else {
208505884511SPyun YongHyeon 			buflen -= ETHER_CRC_LEN;
208605884511SPyun YongHyeon 			if (buflen < ETHER_HDR_LEN) {
20874d52a575SXin LI 				m_freem(m);
20884d52a575SXin LI 				ifp->if_ierrors++;
20894d52a575SXin LI 			} else {
209005884511SPyun YongHyeon 				m->m_pkthdr.len = m->m_len = buflen;
20914d52a575SXin LI 				m->m_pkthdr.rcvif = ifp;
20924d52a575SXin LI 				ET_UNLOCK(sc);
20934d52a575SXin LI 				ifp->if_input(ifp, m);
20944d52a575SXin LI 				ET_LOCK(sc);
20954d52a575SXin LI 			}
20964d52a575SXin LI 		}
20974d52a575SXin LI 
20984d52a575SXin LI 		rx_ring = &sc->sc_rx_ring[ring_idx];
20994d52a575SXin LI 		if (buf_idx != rx_ring->rr_index) {
210005884511SPyun YongHyeon 			if_printf(ifp,
210105884511SPyun YongHyeon 			    "WARNING!! ring %d, buf_idx %d, rr_idx %d\n",
21024d52a575SXin LI 			    ring_idx, buf_idx, rx_ring->rr_index);
21034d52a575SXin LI 		}
21044d52a575SXin LI 
21054d52a575SXin LI 		MPASS(rx_ring->rr_index < ET_RX_NDESC);
21064d52a575SXin LI 		if (++rx_ring->rr_index == ET_RX_NDESC) {
21074d52a575SXin LI 			rx_ring->rr_index = 0;
21084d52a575SXin LI 			rx_ring->rr_wrap ^= 1;
21094d52a575SXin LI 		}
211023263665SPyun YongHyeon 		rxring_pos = rx_ring->rr_index & ET_RX_RING_POS_INDEX_MASK;
21114d52a575SXin LI 		if (rx_ring->rr_wrap)
21124d52a575SXin LI 			rxring_pos |= ET_RX_RING_POS_WRAP;
21134d52a575SXin LI 		CSR_WRITE_4(sc, rx_ring->rr_posreg, rxring_pos);
21144d52a575SXin LI 	}
211505884511SPyun YongHyeon 
211605884511SPyun YongHyeon 	bus_dmamap_sync(rxsd->rxsd_dtag, rxsd->rxsd_dmap,
211705884511SPyun YongHyeon 	    BUS_DMASYNC_PREREAD);
211805884511SPyun YongHyeon 	bus_dmamap_sync(rxst_ring->rsr_dtag, rxst_ring->rsr_dmap,
211905884511SPyun YongHyeon 	    BUS_DMASYNC_PREREAD);
21204d52a575SXin LI }
21214d52a575SXin LI 
21224d52a575SXin LI static int
21234d52a575SXin LI et_encap(struct et_softc *sc, struct mbuf **m0)
21244d52a575SXin LI {
212505884511SPyun YongHyeon 	struct et_txdesc_ring *tx_ring;
212605884511SPyun YongHyeon 	struct et_txbuf_data *tbd;
21274d52a575SXin LI 	struct et_txdesc *td;
212805884511SPyun YongHyeon 	struct mbuf *m;
212905884511SPyun YongHyeon 	bus_dma_segment_t segs[ET_NSEG_MAX];
21304d52a575SXin LI 	bus_dmamap_t map;
2131244fd28bSPyun YongHyeon 	uint32_t csum_flags, last_td_ctrl2;
213205884511SPyun YongHyeon 	int error, i, idx, first_idx, last_idx, nsegs;
21334d52a575SXin LI 
213405884511SPyun YongHyeon 	tx_ring = &sc->sc_tx_ring;
21354d52a575SXin LI 	MPASS(tx_ring->tr_ready_index < ET_TX_NDESC);
213605884511SPyun YongHyeon 	tbd = &sc->sc_tx_data;
21374d52a575SXin LI 	first_idx = tx_ring->tr_ready_index;
21384d52a575SXin LI 	map = tbd->tbd_buf[first_idx].tb_dmap;
21394d52a575SXin LI 
214005884511SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->sc_tx_tag, map, *m0, segs, &nsegs,
214105884511SPyun YongHyeon 	    0);
214205884511SPyun YongHyeon 	if (error == EFBIG) {
214305884511SPyun YongHyeon 		m = m_collapse(*m0, M_DONTWAIT, ET_NSEG_MAX);
214405884511SPyun YongHyeon 		if (m == NULL) {
214505884511SPyun YongHyeon 			m_freem(*m0);
214605884511SPyun YongHyeon 			*m0 = NULL;
214705884511SPyun YongHyeon 			return (ENOMEM);
21484d52a575SXin LI 		}
214905884511SPyun YongHyeon 		*m0 = m;
215005884511SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->sc_tx_tag, map, *m0, segs,
215105884511SPyun YongHyeon 		    &nsegs, 0);
215205884511SPyun YongHyeon 		if (error != 0) {
215305884511SPyun YongHyeon 			m_freem(*m0);
215405884511SPyun YongHyeon                         *m0 = NULL;
215505884511SPyun YongHyeon 			return (error);
21564d52a575SXin LI 		}
215705884511SPyun YongHyeon 	} else if (error != 0)
215805884511SPyun YongHyeon 		return (error);
21594d52a575SXin LI 
216005884511SPyun YongHyeon 	/* Check for descriptor overruns. */
216105884511SPyun YongHyeon 	if (tbd->tbd_used + nsegs > ET_TX_NDESC - 1) {
216205884511SPyun YongHyeon 		bus_dmamap_unload(sc->sc_tx_tag, map);
216305884511SPyun YongHyeon 		return (ENOBUFS);
21644d52a575SXin LI 	}
216505884511SPyun YongHyeon 	bus_dmamap_sync(sc->sc_tx_tag, map, BUS_DMASYNC_PREWRITE);
21664d52a575SXin LI 
21674d52a575SXin LI 	last_td_ctrl2 = ET_TDCTRL2_LAST_FRAG;
216805884511SPyun YongHyeon 	sc->sc_tx += nsegs;
21694d52a575SXin LI 	if (sc->sc_tx / sc->sc_tx_intr_nsegs != sc->sc_tx_intr) {
21704d52a575SXin LI 		sc->sc_tx_intr = sc->sc_tx / sc->sc_tx_intr_nsegs;
21714d52a575SXin LI 		last_td_ctrl2 |= ET_TDCTRL2_INTR;
21724d52a575SXin LI 	}
21734d52a575SXin LI 
217405884511SPyun YongHyeon 	m = *m0;
21759955274cSPyun YongHyeon 	csum_flags = 0;
21769955274cSPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & ET_CSUM_FEATURES) != 0) {
21779955274cSPyun YongHyeon 		if ((m->m_pkthdr.csum_flags & CSUM_IP) != 0)
21789955274cSPyun YongHyeon 			csum_flags |= ET_TDCTRL2_CSUM_IP;
21799955274cSPyun YongHyeon 		if ((m->m_pkthdr.csum_flags & CSUM_UDP) != 0)
21809955274cSPyun YongHyeon 			csum_flags |= ET_TDCTRL2_CSUM_UDP;
21819955274cSPyun YongHyeon 		else if ((m->m_pkthdr.csum_flags & CSUM_TCP) != 0)
21829955274cSPyun YongHyeon 			csum_flags |= ET_TDCTRL2_CSUM_TCP;
21839955274cSPyun YongHyeon 	}
21844d52a575SXin LI 	last_idx = -1;
218505884511SPyun YongHyeon 	for (i = 0; i < nsegs; ++i) {
21864d52a575SXin LI 		idx = (first_idx + i) % ET_TX_NDESC;
21874d52a575SXin LI 		td = &tx_ring->tr_desc[idx];
218826e07b50SPyun YongHyeon 		td->td_addr_hi = htole32(ET_ADDR_HI(segs[i].ds_addr));
218926e07b50SPyun YongHyeon 		td->td_addr_lo = htole32(ET_ADDR_LO(segs[i].ds_addr));
219026e07b50SPyun YongHyeon 		td->td_ctrl1 =  htole32(segs[i].ds_len & ET_TDCTRL1_LEN_MASK);
219105884511SPyun YongHyeon 		if (i == nsegs - 1) {
219205884511SPyun YongHyeon 			/* Last frag */
21939955274cSPyun YongHyeon 			td->td_ctrl2 = htole32(last_td_ctrl2 | csum_flags);
21944d52a575SXin LI 			last_idx = idx;
21959955274cSPyun YongHyeon 		} else
21969955274cSPyun YongHyeon 			td->td_ctrl2 = htole32(csum_flags);
21974d52a575SXin LI 
21984d52a575SXin LI 		MPASS(tx_ring->tr_ready_index < ET_TX_NDESC);
21994d52a575SXin LI 		if (++tx_ring->tr_ready_index == ET_TX_NDESC) {
22004d52a575SXin LI 			tx_ring->tr_ready_index = 0;
22014d52a575SXin LI 			tx_ring->tr_ready_wrap ^= 1;
22024d52a575SXin LI 		}
22034d52a575SXin LI 	}
22044d52a575SXin LI 	td = &tx_ring->tr_desc[first_idx];
220505884511SPyun YongHyeon 	/* First frag */
220605884511SPyun YongHyeon 	td->td_ctrl2 |= htole32(ET_TDCTRL2_FIRST_FRAG);
22074d52a575SXin LI 
22084d52a575SXin LI 	MPASS(last_idx >= 0);
22094d52a575SXin LI 	tbd->tbd_buf[first_idx].tb_dmap = tbd->tbd_buf[last_idx].tb_dmap;
22104d52a575SXin LI 	tbd->tbd_buf[last_idx].tb_dmap = map;
22114d52a575SXin LI 	tbd->tbd_buf[last_idx].tb_mbuf = m;
22124d52a575SXin LI 
221305884511SPyun YongHyeon 	tbd->tbd_used += nsegs;
22144d52a575SXin LI 	MPASS(tbd->tbd_used <= ET_TX_NDESC);
22154d52a575SXin LI 
221605884511SPyun YongHyeon 	return (0);
22174d52a575SXin LI }
22184d52a575SXin LI 
22194d52a575SXin LI static void
22204d52a575SXin LI et_txeof(struct et_softc *sc)
22214d52a575SXin LI {
22224d52a575SXin LI 	struct et_txdesc_ring *tx_ring;
22234d52a575SXin LI 	struct et_txbuf_data *tbd;
222405884511SPyun YongHyeon 	struct et_txbuf *tb;
222505884511SPyun YongHyeon 	struct ifnet *ifp;
22264d52a575SXin LI 	uint32_t tx_done;
22274d52a575SXin LI 	int end, wrap;
22284d52a575SXin LI 
22294d52a575SXin LI 	ET_LOCK_ASSERT(sc);
223005884511SPyun YongHyeon 
22314d52a575SXin LI 	ifp = sc->ifp;
22324d52a575SXin LI 	tx_ring = &sc->sc_tx_ring;
22334d52a575SXin LI 	tbd = &sc->sc_tx_data;
22344d52a575SXin LI 
22354d52a575SXin LI 	if ((sc->sc_flags & ET_FLAG_TXRX_ENABLED) == 0)
22364d52a575SXin LI 		return;
22374d52a575SXin LI 
22384d52a575SXin LI 	if (tbd->tbd_used == 0)
22394d52a575SXin LI 		return;
22404d52a575SXin LI 
224105884511SPyun YongHyeon 	bus_dmamap_sync(tx_ring->tr_dtag, tx_ring->tr_dmap,
224205884511SPyun YongHyeon 	    BUS_DMASYNC_POSTWRITE);
224305884511SPyun YongHyeon 
22444d52a575SXin LI 	tx_done = CSR_READ_4(sc, ET_TX_DONE_POS);
224523263665SPyun YongHyeon 	end = tx_done & ET_TX_DONE_POS_INDEX_MASK;
22464d52a575SXin LI 	wrap = (tx_done & ET_TX_DONE_POS_WRAP) ? 1 : 0;
22474d52a575SXin LI 
22484d52a575SXin LI 	while (tbd->tbd_start_index != end || tbd->tbd_start_wrap != wrap) {
22494d52a575SXin LI 		MPASS(tbd->tbd_start_index < ET_TX_NDESC);
22504d52a575SXin LI 		tb = &tbd->tbd_buf[tbd->tbd_start_index];
22514d52a575SXin LI 		if (tb->tb_mbuf != NULL) {
225205884511SPyun YongHyeon 			bus_dmamap_sync(sc->sc_tx_tag, tb->tb_dmap,
225305884511SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
225405884511SPyun YongHyeon 			bus_dmamap_unload(sc->sc_tx_tag, tb->tb_dmap);
22554d52a575SXin LI 			m_freem(tb->tb_mbuf);
22564d52a575SXin LI 			tb->tb_mbuf = NULL;
22574d52a575SXin LI 		}
22584d52a575SXin LI 
22594d52a575SXin LI 		if (++tbd->tbd_start_index == ET_TX_NDESC) {
22604d52a575SXin LI 			tbd->tbd_start_index = 0;
22614d52a575SXin LI 			tbd->tbd_start_wrap ^= 1;
22624d52a575SXin LI 		}
22634d52a575SXin LI 
22644d52a575SXin LI 		MPASS(tbd->tbd_used > 0);
22654d52a575SXin LI 		tbd->tbd_used--;
22664d52a575SXin LI 	}
22674d52a575SXin LI 
22684d52a575SXin LI 	if (tbd->tbd_used == 0)
22694d52a575SXin LI 		sc->watchdog_timer = 0;
227005884511SPyun YongHyeon 	if (tbd->tbd_used + ET_NSEG_SPARE < ET_TX_NDESC)
22714d52a575SXin LI 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
22724d52a575SXin LI }
22731f009e2fSPyun YongHyeon 
22744d52a575SXin LI static void
22754d52a575SXin LI et_tick(void *xsc)
22764d52a575SXin LI {
22774d52a575SXin LI 	struct et_softc *sc = xsc;
22784d52a575SXin LI 	struct ifnet *ifp;
22794d52a575SXin LI 	struct mii_data *mii;
22804d52a575SXin LI 
22814d52a575SXin LI 	ET_LOCK_ASSERT(sc);
22824d52a575SXin LI 	ifp = sc->ifp;
22834d52a575SXin LI 	mii = device_get_softc(sc->sc_miibus);
22844d52a575SXin LI 
22854d52a575SXin LI 	mii_tick(mii);
2286e0b5ac02SPyun YongHyeon 	et_stats_update(sc);
228705884511SPyun YongHyeon 	if (et_watchdog(sc) == EJUSTRETURN)
228805884511SPyun YongHyeon 		return;
22894d52a575SXin LI 	callout_reset(&sc->sc_tick, hz, et_tick, sc);
22904d52a575SXin LI }
22914d52a575SXin LI 
22924d52a575SXin LI static int
229305884511SPyun YongHyeon et_newbuf_cluster(struct et_rxbuf_data *rbd, int buf_idx)
22944d52a575SXin LI {
229505884511SPyun YongHyeon 	struct et_softc *sc;
229605884511SPyun YongHyeon 	struct et_rxdesc *desc;
22974d52a575SXin LI 	struct et_rxbuf *rb;
22984d52a575SXin LI 	struct mbuf *m;
229905884511SPyun YongHyeon 	bus_dma_segment_t segs[1];
23004d52a575SXin LI 	bus_dmamap_t dmap;
230105884511SPyun YongHyeon 	int nsegs;
23024d52a575SXin LI 
23034d52a575SXin LI 	MPASS(buf_idx < ET_RX_NDESC);
230405884511SPyun YongHyeon 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
230505884511SPyun YongHyeon 	if (m == NULL)
230605884511SPyun YongHyeon 		return (ENOBUFS);
230705884511SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MCLBYTES;
230805884511SPyun YongHyeon 	m_adj(m, ETHER_ALIGN);
230905884511SPyun YongHyeon 
231005884511SPyun YongHyeon 	sc = rbd->rbd_softc;
23114d52a575SXin LI 	rb = &rbd->rbd_buf[buf_idx];
23124d52a575SXin LI 
231305884511SPyun YongHyeon 	if (bus_dmamap_load_mbuf_sg(sc->sc_rx_tag, sc->sc_rx_sparemap, m,
231405884511SPyun YongHyeon 	    segs, &nsegs, 0) != 0) {
23154d52a575SXin LI 		m_freem(m);
231605884511SPyun YongHyeon 		return (ENOBUFS);
23174d52a575SXin LI 	}
231805884511SPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
23194d52a575SXin LI 
232005884511SPyun YongHyeon 	if (rb->rb_mbuf != NULL) {
232105884511SPyun YongHyeon 		bus_dmamap_sync(sc->sc_rx_tag, rb->rb_dmap,
23224d52a575SXin LI 		    BUS_DMASYNC_POSTREAD);
232305884511SPyun YongHyeon 		bus_dmamap_unload(sc->sc_rx_tag, rb->rb_dmap);
23244d52a575SXin LI 	}
23254d52a575SXin LI 	dmap = rb->rb_dmap;
232605884511SPyun YongHyeon 	rb->rb_dmap = sc->sc_rx_sparemap;
232705884511SPyun YongHyeon 	sc->sc_rx_sparemap = dmap;
232805884511SPyun YongHyeon 	bus_dmamap_sync(sc->sc_rx_tag, rb->rb_dmap, BUS_DMASYNC_PREREAD);
23294d52a575SXin LI 
233005884511SPyun YongHyeon 	rb->rb_mbuf = m;
233105884511SPyun YongHyeon 	desc = &rbd->rbd_ring->rr_desc[buf_idx];
233205884511SPyun YongHyeon 	desc->rd_addr_hi = htole32(ET_ADDR_HI(segs[0].ds_addr));
233305884511SPyun YongHyeon 	desc->rd_addr_lo = htole32(ET_ADDR_LO(segs[0].ds_addr));
233405884511SPyun YongHyeon 	desc->rd_ctrl = htole32(buf_idx & ET_RDCTRL_BUFIDX_MASK);
233505884511SPyun YongHyeon 	bus_dmamap_sync(rbd->rbd_ring->rr_dtag, rbd->rbd_ring->rr_dmap,
233605884511SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
233705884511SPyun YongHyeon 	return (0);
233805884511SPyun YongHyeon }
233905884511SPyun YongHyeon 
234005884511SPyun YongHyeon static void
234105884511SPyun YongHyeon et_rxbuf_discard(struct et_rxbuf_data *rbd, int buf_idx)
234205884511SPyun YongHyeon {
234305884511SPyun YongHyeon 	struct et_rxdesc *desc;
234405884511SPyun YongHyeon 
234505884511SPyun YongHyeon 	desc = &rbd->rbd_ring->rr_desc[buf_idx];
234605884511SPyun YongHyeon 	desc->rd_ctrl = htole32(buf_idx & ET_RDCTRL_BUFIDX_MASK);
234705884511SPyun YongHyeon 	bus_dmamap_sync(rbd->rbd_ring->rr_dtag, rbd->rbd_ring->rr_dmap,
234805884511SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
234905884511SPyun YongHyeon }
235005884511SPyun YongHyeon 
235105884511SPyun YongHyeon static int
235205884511SPyun YongHyeon et_newbuf_hdr(struct et_rxbuf_data *rbd, int buf_idx)
235305884511SPyun YongHyeon {
235405884511SPyun YongHyeon 	struct et_softc *sc;
235505884511SPyun YongHyeon 	struct et_rxdesc *desc;
235605884511SPyun YongHyeon 	struct et_rxbuf *rb;
235705884511SPyun YongHyeon 	struct mbuf *m;
235805884511SPyun YongHyeon 	bus_dma_segment_t segs[1];
235905884511SPyun YongHyeon 	bus_dmamap_t dmap;
236005884511SPyun YongHyeon 	int nsegs;
236105884511SPyun YongHyeon 
236205884511SPyun YongHyeon 	MPASS(buf_idx < ET_RX_NDESC);
236305884511SPyun YongHyeon 	MGETHDR(m, M_DONTWAIT, MT_DATA);
236405884511SPyun YongHyeon 	if (m == NULL)
236505884511SPyun YongHyeon 		return (ENOBUFS);
236605884511SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MHLEN;
236705884511SPyun YongHyeon 	m_adj(m, ETHER_ALIGN);
236805884511SPyun YongHyeon 
236905884511SPyun YongHyeon 	sc = rbd->rbd_softc;
237005884511SPyun YongHyeon 	rb = &rbd->rbd_buf[buf_idx];
237105884511SPyun YongHyeon 
237205884511SPyun YongHyeon 	if (bus_dmamap_load_mbuf_sg(sc->sc_rx_mini_tag, sc->sc_rx_mini_sparemap,
237305884511SPyun YongHyeon 	    m, segs, &nsegs, 0) != 0) {
237405884511SPyun YongHyeon 		m_freem(m);
237505884511SPyun YongHyeon 		return (ENOBUFS);
237605884511SPyun YongHyeon 	}
237705884511SPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
237805884511SPyun YongHyeon 
237905884511SPyun YongHyeon 	if (rb->rb_mbuf != NULL) {
238005884511SPyun YongHyeon 		bus_dmamap_sync(sc->sc_rx_mini_tag, rb->rb_dmap,
238105884511SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD);
238205884511SPyun YongHyeon 		bus_dmamap_unload(sc->sc_rx_mini_tag, rb->rb_dmap);
238305884511SPyun YongHyeon 	}
238405884511SPyun YongHyeon 	dmap = rb->rb_dmap;
238505884511SPyun YongHyeon 	rb->rb_dmap = sc->sc_rx_mini_sparemap;
238605884511SPyun YongHyeon 	sc->sc_rx_mini_sparemap = dmap;
238705884511SPyun YongHyeon 	bus_dmamap_sync(sc->sc_rx_mini_tag, rb->rb_dmap, BUS_DMASYNC_PREREAD);
238805884511SPyun YongHyeon 
238905884511SPyun YongHyeon 	rb->rb_mbuf = m;
239005884511SPyun YongHyeon 	desc = &rbd->rbd_ring->rr_desc[buf_idx];
239105884511SPyun YongHyeon 	desc->rd_addr_hi = htole32(ET_ADDR_HI(segs[0].ds_addr));
239205884511SPyun YongHyeon 	desc->rd_addr_lo = htole32(ET_ADDR_LO(segs[0].ds_addr));
239305884511SPyun YongHyeon 	desc->rd_ctrl = htole32(buf_idx & ET_RDCTRL_BUFIDX_MASK);
239405884511SPyun YongHyeon 	bus_dmamap_sync(rbd->rbd_ring->rr_dtag, rbd->rbd_ring->rr_dmap,
239505884511SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
239605884511SPyun YongHyeon 	return (0);
23974d52a575SXin LI }
23984d52a575SXin LI 
2399e0b5ac02SPyun YongHyeon #define	ET_SYSCTL_STAT_ADD32(c, h, n, p, d)	\
2400e0b5ac02SPyun YongHyeon 	    SYSCTL_ADD_UINT(c, h, OID_AUTO, n, CTLFLAG_RD, p, 0, d)
2401e0b5ac02SPyun YongHyeon #define	ET_SYSCTL_STAT_ADD64(c, h, n, p, d)	\
2402e0b5ac02SPyun YongHyeon 	    SYSCTL_ADD_UQUAD(c, h, OID_AUTO, n, CTLFLAG_RD, p, d)
2403e0b5ac02SPyun YongHyeon 
24044d52a575SXin LI /*
24054d52a575SXin LI  * Create sysctl tree
24064d52a575SXin LI  */
24074d52a575SXin LI static void
24084d52a575SXin LI et_add_sysctls(struct et_softc * sc)
24094d52a575SXin LI {
24104d52a575SXin LI 	struct sysctl_ctx_list *ctx;
2411e0b5ac02SPyun YongHyeon 	struct sysctl_oid_list *children, *parent;
2412e0b5ac02SPyun YongHyeon 	struct sysctl_oid *tree;
2413e0b5ac02SPyun YongHyeon 	struct et_hw_stats *stats;
24144d52a575SXin LI 
24154d52a575SXin LI 	ctx = device_get_sysctl_ctx(sc->dev);
24164d52a575SXin LI 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev));
24174d52a575SXin LI 
24184d52a575SXin LI 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rx_intr_npkts",
24194d52a575SXin LI 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, et_sysctl_rx_intr_npkts, "I",
24204d52a575SXin LI 	    "RX IM, # packets per RX interrupt");
24214d52a575SXin LI 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rx_intr_delay",
24224d52a575SXin LI 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, et_sysctl_rx_intr_delay, "I",
24234d52a575SXin LI 	    "RX IM, RX interrupt delay (x10 usec)");
24244d52a575SXin LI 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_intr_nsegs",
24254d52a575SXin LI 	    CTLFLAG_RW, &sc->sc_tx_intr_nsegs, 0,
24264d52a575SXin LI 	    "TX IM, # segments per TX interrupt");
24274d52a575SXin LI 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO, "timer",
24284d52a575SXin LI 	    CTLFLAG_RW, &sc->sc_timer, 0, "TX timer");
2429e0b5ac02SPyun YongHyeon 
2430e0b5ac02SPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "stats", CTLFLAG_RD,
2431e0b5ac02SPyun YongHyeon 	    NULL, "ET statistics");
2432e0b5ac02SPyun YongHyeon         parent = SYSCTL_CHILDREN(tree);
2433e0b5ac02SPyun YongHyeon 
2434e0b5ac02SPyun YongHyeon 	/* TX/RX statistics. */
2435e0b5ac02SPyun YongHyeon 	stats = &sc->sc_stats;
2436e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD64(ctx, parent, "frames_64", &stats->pkts_64,
2437e0b5ac02SPyun YongHyeon 	    "0 to 64 bytes frames");
2438e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD64(ctx, parent, "frames_65_127", &stats->pkts_65,
2439e0b5ac02SPyun YongHyeon 	    "65 to 127 bytes frames");
2440e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD64(ctx, parent, "frames_128_255", &stats->pkts_128,
2441e0b5ac02SPyun YongHyeon 	    "128 to 255 bytes frames");
2442e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD64(ctx, parent, "frames_256_511", &stats->pkts_256,
2443e0b5ac02SPyun YongHyeon 	    "256 to 511 bytes frames");
2444e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD64(ctx, parent, "frames_512_1023", &stats->pkts_512,
2445e0b5ac02SPyun YongHyeon 	    "512 to 1023 bytes frames");
2446e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD64(ctx, parent, "frames_1024_1518", &stats->pkts_1024,
2447e0b5ac02SPyun YongHyeon 	    "1024 to 1518 bytes frames");
2448e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD64(ctx, parent, "frames_1519_1522", &stats->pkts_1519,
2449e0b5ac02SPyun YongHyeon 	    "1519 to 1522 bytes frames");
2450e0b5ac02SPyun YongHyeon 
2451e0b5ac02SPyun YongHyeon 	/* RX statistics. */
2452e0b5ac02SPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "rx", CTLFLAG_RD,
2453e0b5ac02SPyun YongHyeon 	    NULL, "RX MAC statistics");
2454e0b5ac02SPyun YongHyeon 	children = SYSCTL_CHILDREN(tree);
2455e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD64(ctx, children, "bytes",
2456e0b5ac02SPyun YongHyeon 	    &stats->rx_bytes, "Good bytes");
2457e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD64(ctx, children, "frames",
2458e0b5ac02SPyun YongHyeon 	    &stats->rx_frames, "Good frames");
2459e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "crc_errs",
2460e0b5ac02SPyun YongHyeon 	    &stats->rx_crcerrs, "CRC errors");
2461e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD64(ctx, children, "mcast_frames",
2462e0b5ac02SPyun YongHyeon 	    &stats->rx_mcast, "Multicast frames");
2463e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD64(ctx, children, "bcast_frames",
2464e0b5ac02SPyun YongHyeon 	    &stats->rx_bcast, "Broadcast frames");
2465e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "control",
2466e0b5ac02SPyun YongHyeon 	    &stats->rx_control, "Control frames");
2467e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "pause",
2468e0b5ac02SPyun YongHyeon 	    &stats->rx_pause, "Pause frames");
2469e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "unknown_control",
2470e0b5ac02SPyun YongHyeon 	    &stats->rx_unknown_control, "Unknown control frames");
2471e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "align_errs",
2472e0b5ac02SPyun YongHyeon 	    &stats->rx_alignerrs, "Alignment errors");
2473e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "len_errs",
2474e0b5ac02SPyun YongHyeon 	    &stats->rx_lenerrs, "Frames with length mismatched");
2475e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "code_errs",
2476e0b5ac02SPyun YongHyeon 	    &stats->rx_codeerrs, "Frames with code error");
2477e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "cs_errs",
2478e0b5ac02SPyun YongHyeon 	    &stats->rx_cserrs, "Frames with carrier sense error");
2479e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "runts",
2480e0b5ac02SPyun YongHyeon 	    &stats->rx_runts, "Too short frames");
2481e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD64(ctx, children, "oversize",
2482e0b5ac02SPyun YongHyeon 	    &stats->rx_oversize, "Oversized frames");
2483e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "fragments",
2484e0b5ac02SPyun YongHyeon 	    &stats->rx_fragments, "Fragmented frames");
2485e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "jabbers",
2486e0b5ac02SPyun YongHyeon 	    &stats->rx_jabbers, "Frames with jabber error");
2487e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "drop",
2488e0b5ac02SPyun YongHyeon 	    &stats->rx_drop, "Dropped frames");
2489e0b5ac02SPyun YongHyeon 
2490e0b5ac02SPyun YongHyeon 	/* TX statistics. */
2491e0b5ac02SPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "tx", CTLFLAG_RD,
2492e0b5ac02SPyun YongHyeon 	    NULL, "TX MAC statistics");
2493e0b5ac02SPyun YongHyeon 	children = SYSCTL_CHILDREN(tree);
2494e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD64(ctx, children, "bytes",
2495e0b5ac02SPyun YongHyeon 	    &stats->tx_bytes, "Good bytes");
2496e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD64(ctx, children, "frames",
2497e0b5ac02SPyun YongHyeon 	    &stats->tx_frames, "Good frames");
2498e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD64(ctx, children, "mcast_frames",
2499e0b5ac02SPyun YongHyeon 	    &stats->tx_mcast, "Multicast frames");
2500e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD64(ctx, children, "bcast_frames",
2501e0b5ac02SPyun YongHyeon 	    &stats->tx_bcast, "Broadcast frames");
2502e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "pause",
2503e0b5ac02SPyun YongHyeon 	    &stats->tx_pause, "Pause frames");
2504e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "deferred",
2505e0b5ac02SPyun YongHyeon 	    &stats->tx_deferred, "Deferred frames");
2506e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "excess_deferred",
2507e0b5ac02SPyun YongHyeon 	    &stats->tx_excess_deferred, "Excessively deferred frames");
2508e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "single_colls",
2509e0b5ac02SPyun YongHyeon 	    &stats->tx_single_colls, "Single collisions");
2510e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "multi_colls",
2511e0b5ac02SPyun YongHyeon 	    &stats->tx_multi_colls, "Multiple collisions");
2512e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "late_colls",
2513e0b5ac02SPyun YongHyeon 	    &stats->tx_late_colls, "Late collisions");
2514e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "excess_colls",
2515e0b5ac02SPyun YongHyeon 	    &stats->tx_excess_colls, "Excess collisions");
2516e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "total_colls",
2517e0b5ac02SPyun YongHyeon 	    &stats->tx_total_colls, "Total collisions");
2518e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "pause_honored",
2519e0b5ac02SPyun YongHyeon 	    &stats->tx_pause_honored, "Honored pause frames");
2520e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "drop",
2521e0b5ac02SPyun YongHyeon 	    &stats->tx_drop, "Dropped frames");
2522e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "jabbers",
2523e0b5ac02SPyun YongHyeon 	    &stats->tx_jabbers, "Frames with jabber errors");
2524e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "crc_errs",
2525e0b5ac02SPyun YongHyeon 	    &stats->tx_crcerrs, "Frames with CRC errors");
2526e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "control",
2527e0b5ac02SPyun YongHyeon 	    &stats->tx_control, "Control frames");
2528e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD64(ctx, children, "oversize",
2529e0b5ac02SPyun YongHyeon 	    &stats->tx_oversize, "Oversized frames");
2530e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "undersize",
2531e0b5ac02SPyun YongHyeon 	    &stats->tx_undersize, "Undersized frames");
2532e0b5ac02SPyun YongHyeon 	ET_SYSCTL_STAT_ADD32(ctx, children, "fragments",
2533e0b5ac02SPyun YongHyeon 	    &stats->tx_fragments, "Fragmented frames");
25344d52a575SXin LI }
25354d52a575SXin LI 
2536e0b5ac02SPyun YongHyeon #undef	ET_SYSCTL_STAT_ADD32
2537e0b5ac02SPyun YongHyeon #undef	ET_SYSCTL_STAT_ADD64
2538e0b5ac02SPyun YongHyeon 
25394d52a575SXin LI static int
25404d52a575SXin LI et_sysctl_rx_intr_npkts(SYSCTL_HANDLER_ARGS)
25414d52a575SXin LI {
25424d52a575SXin LI 	struct et_softc *sc = arg1;
25434d52a575SXin LI 	struct ifnet *ifp = sc->ifp;
25444d52a575SXin LI 	int error = 0, v;
25454d52a575SXin LI 
25464d52a575SXin LI 	v = sc->sc_rx_intr_npkts;
25474d52a575SXin LI 	error = sysctl_handle_int(oidp, &v, 0, req);
25484d52a575SXin LI 	if (error || req->newptr == NULL)
25494d52a575SXin LI 		goto back;
25504d52a575SXin LI 	if (v <= 0) {
25514d52a575SXin LI 		error = EINVAL;
25524d52a575SXin LI 		goto back;
25534d52a575SXin LI 	}
25544d52a575SXin LI 
25554d52a575SXin LI 	if (sc->sc_rx_intr_npkts != v) {
25564d52a575SXin LI 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
25574d52a575SXin LI 			CSR_WRITE_4(sc, ET_RX_INTR_NPKTS, v);
25584d52a575SXin LI 		sc->sc_rx_intr_npkts = v;
25594d52a575SXin LI 	}
25604d52a575SXin LI back:
2561398f1b65SPyun YongHyeon 	return (error);
25624d52a575SXin LI }
25634d52a575SXin LI 
25644d52a575SXin LI static int
25654d52a575SXin LI et_sysctl_rx_intr_delay(SYSCTL_HANDLER_ARGS)
25664d52a575SXin LI {
25674d52a575SXin LI 	struct et_softc *sc = arg1;
25684d52a575SXin LI 	struct ifnet *ifp = sc->ifp;
25694d52a575SXin LI 	int error = 0, v;
25704d52a575SXin LI 
25714d52a575SXin LI 	v = sc->sc_rx_intr_delay;
25724d52a575SXin LI 	error = sysctl_handle_int(oidp, &v, 0, req);
25734d52a575SXin LI 	if (error || req->newptr == NULL)
25744d52a575SXin LI 		goto back;
25754d52a575SXin LI 	if (v <= 0) {
25764d52a575SXin LI 		error = EINVAL;
25774d52a575SXin LI 		goto back;
25784d52a575SXin LI 	}
25794d52a575SXin LI 
25804d52a575SXin LI 	if (sc->sc_rx_intr_delay != v) {
25814d52a575SXin LI 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
25824d52a575SXin LI 			CSR_WRITE_4(sc, ET_RX_INTR_DELAY, v);
25834d52a575SXin LI 		sc->sc_rx_intr_delay = v;
25844d52a575SXin LI 	}
25854d52a575SXin LI back:
2586398f1b65SPyun YongHyeon 	return (error);
25874d52a575SXin LI }
25884d52a575SXin LI 
2589e0b5ac02SPyun YongHyeon static void
2590e0b5ac02SPyun YongHyeon et_stats_update(struct et_softc *sc)
2591e0b5ac02SPyun YongHyeon {
2592e0b5ac02SPyun YongHyeon 	struct ifnet *ifp;
2593e0b5ac02SPyun YongHyeon 	struct et_hw_stats *stats;
2594e0b5ac02SPyun YongHyeon 
2595e0b5ac02SPyun YongHyeon 	stats = &sc->sc_stats;
2596e0b5ac02SPyun YongHyeon 	stats->pkts_64 += CSR_READ_4(sc, ET_STAT_PKTS_64);
2597e0b5ac02SPyun YongHyeon 	stats->pkts_65 += CSR_READ_4(sc, ET_STAT_PKTS_65_127);
2598e0b5ac02SPyun YongHyeon 	stats->pkts_128 += CSR_READ_4(sc, ET_STAT_PKTS_128_255);
2599e0b5ac02SPyun YongHyeon 	stats->pkts_256 += CSR_READ_4(sc, ET_STAT_PKTS_256_511);
2600e0b5ac02SPyun YongHyeon 	stats->pkts_512 += CSR_READ_4(sc, ET_STAT_PKTS_512_1023);
2601e0b5ac02SPyun YongHyeon 	stats->pkts_1024 += CSR_READ_4(sc, ET_STAT_PKTS_1024_1518);
2602e0b5ac02SPyun YongHyeon 	stats->pkts_1519 += CSR_READ_4(sc, ET_STAT_PKTS_1519_1522);
2603e0b5ac02SPyun YongHyeon 
2604e0b5ac02SPyun YongHyeon 	stats->rx_bytes += CSR_READ_4(sc, ET_STAT_RX_BYTES);
2605e0b5ac02SPyun YongHyeon 	stats->rx_frames += CSR_READ_4(sc, ET_STAT_RX_FRAMES);
2606e0b5ac02SPyun YongHyeon 	stats->rx_crcerrs += CSR_READ_4(sc, ET_STAT_RX_CRC_ERR);
2607e0b5ac02SPyun YongHyeon 	stats->rx_mcast += CSR_READ_4(sc, ET_STAT_RX_MCAST);
2608e0b5ac02SPyun YongHyeon 	stats->rx_bcast += CSR_READ_4(sc, ET_STAT_RX_BCAST);
2609e0b5ac02SPyun YongHyeon 	stats->rx_control += CSR_READ_4(sc, ET_STAT_RX_CTL);
2610e0b5ac02SPyun YongHyeon 	stats->rx_pause += CSR_READ_4(sc, ET_STAT_RX_PAUSE);
2611e0b5ac02SPyun YongHyeon 	stats->rx_unknown_control += CSR_READ_4(sc, ET_STAT_RX_UNKNOWN_CTL);
2612e0b5ac02SPyun YongHyeon 	stats->rx_alignerrs += CSR_READ_4(sc, ET_STAT_RX_ALIGN_ERR);
2613e0b5ac02SPyun YongHyeon 	stats->rx_lenerrs += CSR_READ_4(sc, ET_STAT_RX_LEN_ERR);
2614e0b5ac02SPyun YongHyeon 	stats->rx_codeerrs += CSR_READ_4(sc, ET_STAT_RX_CODE_ERR);
2615e0b5ac02SPyun YongHyeon 	stats->rx_cserrs += CSR_READ_4(sc, ET_STAT_RX_CS_ERR);
2616e0b5ac02SPyun YongHyeon 	stats->rx_runts += CSR_READ_4(sc, ET_STAT_RX_RUNT);
2617e0b5ac02SPyun YongHyeon 	stats->rx_oversize += CSR_READ_4(sc, ET_STAT_RX_OVERSIZE);
2618e0b5ac02SPyun YongHyeon 	stats->rx_fragments += CSR_READ_4(sc, ET_STAT_RX_FRAG);
2619e0b5ac02SPyun YongHyeon 	stats->rx_jabbers += CSR_READ_4(sc, ET_STAT_RX_JABBER);
2620e0b5ac02SPyun YongHyeon 	stats->rx_drop += CSR_READ_4(sc, ET_STAT_RX_DROP);
2621e0b5ac02SPyun YongHyeon 
2622e0b5ac02SPyun YongHyeon 	stats->tx_bytes += CSR_READ_4(sc, ET_STAT_TX_BYTES);
2623e0b5ac02SPyun YongHyeon 	stats->tx_frames += CSR_READ_4(sc, ET_STAT_TX_FRAMES);
2624e0b5ac02SPyun YongHyeon 	stats->tx_mcast += CSR_READ_4(sc, ET_STAT_TX_MCAST);
2625e0b5ac02SPyun YongHyeon 	stats->tx_bcast += CSR_READ_4(sc, ET_STAT_TX_BCAST);
2626e0b5ac02SPyun YongHyeon 	stats->tx_pause += CSR_READ_4(sc, ET_STAT_TX_PAUSE);
2627e0b5ac02SPyun YongHyeon 	stats->tx_deferred += CSR_READ_4(sc, ET_STAT_TX_DEFER);
2628e0b5ac02SPyun YongHyeon 	stats->tx_excess_deferred += CSR_READ_4(sc, ET_STAT_TX_EXCESS_DEFER);
2629e0b5ac02SPyun YongHyeon 	stats->tx_single_colls += CSR_READ_4(sc, ET_STAT_TX_SINGLE_COL);
2630e0b5ac02SPyun YongHyeon 	stats->tx_multi_colls += CSR_READ_4(sc, ET_STAT_TX_MULTI_COL);
2631e0b5ac02SPyun YongHyeon 	stats->tx_late_colls += CSR_READ_4(sc, ET_STAT_TX_LATE_COL);
2632e0b5ac02SPyun YongHyeon 	stats->tx_excess_colls += CSR_READ_4(sc, ET_STAT_TX_EXCESS_COL);
2633e0b5ac02SPyun YongHyeon 	stats->tx_total_colls += CSR_READ_4(sc, ET_STAT_TX_TOTAL_COL);
2634e0b5ac02SPyun YongHyeon 	stats->tx_pause_honored += CSR_READ_4(sc, ET_STAT_TX_PAUSE_HONOR);
2635e0b5ac02SPyun YongHyeon 	stats->tx_drop += CSR_READ_4(sc, ET_STAT_TX_DROP);
2636e0b5ac02SPyun YongHyeon 	stats->tx_jabbers += CSR_READ_4(sc, ET_STAT_TX_JABBER);
2637e0b5ac02SPyun YongHyeon 	stats->tx_crcerrs += CSR_READ_4(sc, ET_STAT_TX_CRC_ERR);
2638e0b5ac02SPyun YongHyeon 	stats->tx_control += CSR_READ_4(sc, ET_STAT_TX_CTL);
2639e0b5ac02SPyun YongHyeon 	stats->tx_oversize += CSR_READ_4(sc, ET_STAT_TX_OVERSIZE);
2640e0b5ac02SPyun YongHyeon 	stats->tx_undersize += CSR_READ_4(sc, ET_STAT_TX_UNDERSIZE);
2641e0b5ac02SPyun YongHyeon 	stats->tx_fragments += CSR_READ_4(sc, ET_STAT_TX_FRAG);
2642e0b5ac02SPyun YongHyeon 
2643e0b5ac02SPyun YongHyeon 	/* Update ifnet counters. */
2644e0b5ac02SPyun YongHyeon 	ifp = sc->ifp;
2645e0b5ac02SPyun YongHyeon 	ifp->if_opackets = (u_long)stats->tx_frames;
2646e0b5ac02SPyun YongHyeon 	ifp->if_collisions = stats->tx_total_colls;
2647e0b5ac02SPyun YongHyeon 	ifp->if_oerrors = stats->tx_drop + stats->tx_jabbers +
2648e0b5ac02SPyun YongHyeon 	    stats->tx_crcerrs + stats->tx_excess_deferred +
2649e0b5ac02SPyun YongHyeon 	    stats->tx_late_colls;
2650e0b5ac02SPyun YongHyeon 	ifp->if_ipackets = (u_long)stats->rx_frames;
2651e0b5ac02SPyun YongHyeon 	ifp->if_ierrors = stats->rx_crcerrs + stats->rx_alignerrs +
2652e0b5ac02SPyun YongHyeon 	    stats->rx_lenerrs + stats->rx_codeerrs + stats->rx_cserrs +
2653e0b5ac02SPyun YongHyeon 	    stats->rx_runts + stats->rx_jabbers + stats->rx_drop;
2654e0b5ac02SPyun YongHyeon }
2655e0b5ac02SPyun YongHyeon 
26560442028aSPyun YongHyeon static int
26570442028aSPyun YongHyeon et_suspend(device_t dev)
26580442028aSPyun YongHyeon {
26590442028aSPyun YongHyeon 	struct et_softc *sc;
266038953bb0SPyun YongHyeon 	uint32_t pmcfg;
26610442028aSPyun YongHyeon 
26620442028aSPyun YongHyeon 	sc = device_get_softc(dev);
26630442028aSPyun YongHyeon 	ET_LOCK(sc);
26640442028aSPyun YongHyeon 	if ((sc->ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
26650442028aSPyun YongHyeon 		et_stop(sc);
266638953bb0SPyun YongHyeon 	/* Diable all clocks and put PHY into COMA. */
266738953bb0SPyun YongHyeon 	pmcfg = CSR_READ_4(sc, ET_PM);
266838953bb0SPyun YongHyeon 	pmcfg &= ~(EM_PM_GIGEPHY_ENB | ET_PM_SYSCLK_GATE | ET_PM_TXCLK_GATE |
266938953bb0SPyun YongHyeon 	    ET_PM_RXCLK_GATE);
267038953bb0SPyun YongHyeon 	pmcfg |= ET_PM_PHY_SW_COMA;
267138953bb0SPyun YongHyeon 	CSR_WRITE_4(sc, ET_PM, pmcfg);
26720442028aSPyun YongHyeon 	ET_UNLOCK(sc);
26730442028aSPyun YongHyeon 	return (0);
26740442028aSPyun YongHyeon }
26750442028aSPyun YongHyeon 
26760442028aSPyun YongHyeon static int
26770442028aSPyun YongHyeon et_resume(device_t dev)
26780442028aSPyun YongHyeon {
26790442028aSPyun YongHyeon 	struct et_softc *sc;
268038953bb0SPyun YongHyeon 	uint32_t pmcfg;
26810442028aSPyun YongHyeon 
26820442028aSPyun YongHyeon 	sc = device_get_softc(dev);
26830442028aSPyun YongHyeon 	ET_LOCK(sc);
268438953bb0SPyun YongHyeon 	/* Take PHY out of COMA and enable clocks. */
268538953bb0SPyun YongHyeon 	pmcfg = ET_PM_SYSCLK_GATE | ET_PM_TXCLK_GATE | ET_PM_RXCLK_GATE;
268638953bb0SPyun YongHyeon 	if ((sc->sc_flags & ET_FLAG_FASTETHER) == 0)
268738953bb0SPyun YongHyeon 		pmcfg |= EM_PM_GIGEPHY_ENB;
268838953bb0SPyun YongHyeon 	CSR_WRITE_4(sc, ET_PM, pmcfg);
26890442028aSPyun YongHyeon 	if ((sc->ifp->if_flags & IFF_UP) != 0)
26900442028aSPyun YongHyeon 		et_init_locked(sc);
26910442028aSPyun YongHyeon 	ET_UNLOCK(sc);
26920442028aSPyun YongHyeon 	return (0);
26930442028aSPyun YongHyeon }
2694