xref: /freebsd/sys/dev/ale/if_ale.c (revision ddaf6524682b3ab9e50f7575db319814dbbd053a)
13c6e15bcSPyun YongHyeon /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
43c6e15bcSPyun YongHyeon  * Copyright (c) 2008, Pyun YongHyeon <yongari@FreeBSD.org>
53c6e15bcSPyun YongHyeon  * All rights reserved.
63c6e15bcSPyun YongHyeon  *
73c6e15bcSPyun YongHyeon  * Redistribution and use in source and binary forms, with or without
83c6e15bcSPyun YongHyeon  * modification, are permitted provided that the following conditions
93c6e15bcSPyun YongHyeon  * are met:
103c6e15bcSPyun YongHyeon  * 1. Redistributions of source code must retain the above copyright
113c6e15bcSPyun YongHyeon  *    notice unmodified, this list of conditions, and the following
123c6e15bcSPyun YongHyeon  *    disclaimer.
133c6e15bcSPyun YongHyeon  * 2. Redistributions in binary form must reproduce the above copyright
143c6e15bcSPyun YongHyeon  *    notice, this list of conditions and the following disclaimer in the
153c6e15bcSPyun YongHyeon  *    documentation and/or other materials provided with the distribution.
163c6e15bcSPyun YongHyeon  *
173c6e15bcSPyun YongHyeon  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
183c6e15bcSPyun YongHyeon  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
193c6e15bcSPyun YongHyeon  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
203c6e15bcSPyun YongHyeon  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
213c6e15bcSPyun YongHyeon  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
223c6e15bcSPyun YongHyeon  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
233c6e15bcSPyun YongHyeon  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
243c6e15bcSPyun YongHyeon  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
253c6e15bcSPyun YongHyeon  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
263c6e15bcSPyun YongHyeon  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
273c6e15bcSPyun YongHyeon  * SUCH DAMAGE.
283c6e15bcSPyun YongHyeon  */
293c6e15bcSPyun YongHyeon 
303c6e15bcSPyun YongHyeon /* Driver for Atheros AR8121/AR8113/AR8114 PCIe Ethernet. */
313c6e15bcSPyun YongHyeon 
323c6e15bcSPyun YongHyeon #include <sys/param.h>
333c6e15bcSPyun YongHyeon #include <sys/systm.h>
343c6e15bcSPyun YongHyeon #include <sys/bus.h>
353c6e15bcSPyun YongHyeon #include <sys/endian.h>
363c6e15bcSPyun YongHyeon #include <sys/kernel.h>
373c6e15bcSPyun YongHyeon #include <sys/malloc.h>
383c6e15bcSPyun YongHyeon #include <sys/mbuf.h>
393c6e15bcSPyun YongHyeon #include <sys/module.h>
403c6e15bcSPyun YongHyeon #include <sys/rman.h>
413c6e15bcSPyun YongHyeon #include <sys/queue.h>
423c6e15bcSPyun YongHyeon #include <sys/socket.h>
433c6e15bcSPyun YongHyeon #include <sys/sockio.h>
443c6e15bcSPyun YongHyeon #include <sys/sysctl.h>
453c6e15bcSPyun YongHyeon #include <sys/taskqueue.h>
463c6e15bcSPyun YongHyeon 
473c6e15bcSPyun YongHyeon #include <net/bpf.h>
483c6e15bcSPyun YongHyeon #include <net/if.h>
4976039bc8SGleb Smirnoff #include <net/if_var.h>
503c6e15bcSPyun YongHyeon #include <net/if_arp.h>
513c6e15bcSPyun YongHyeon #include <net/ethernet.h>
523c6e15bcSPyun YongHyeon #include <net/if_dl.h>
533c6e15bcSPyun YongHyeon #include <net/if_llc.h>
543c6e15bcSPyun YongHyeon #include <net/if_media.h>
553c6e15bcSPyun YongHyeon #include <net/if_types.h>
563c6e15bcSPyun YongHyeon #include <net/if_vlan_var.h>
573c6e15bcSPyun YongHyeon 
583c6e15bcSPyun YongHyeon #include <netinet/in.h>
593c6e15bcSPyun YongHyeon #include <netinet/in_systm.h>
603c6e15bcSPyun YongHyeon #include <netinet/ip.h>
613c6e15bcSPyun YongHyeon #include <netinet/tcp.h>
623c6e15bcSPyun YongHyeon 
633c6e15bcSPyun YongHyeon #include <dev/mii/mii.h>
643c6e15bcSPyun YongHyeon #include <dev/mii/miivar.h>
653c6e15bcSPyun YongHyeon 
663c6e15bcSPyun YongHyeon #include <dev/pci/pcireg.h>
673c6e15bcSPyun YongHyeon #include <dev/pci/pcivar.h>
683c6e15bcSPyun YongHyeon 
693c6e15bcSPyun YongHyeon #include <machine/bus.h>
703c6e15bcSPyun YongHyeon #include <machine/in_cksum.h>
713c6e15bcSPyun YongHyeon 
723c6e15bcSPyun YongHyeon #include <dev/ale/if_alereg.h>
733c6e15bcSPyun YongHyeon #include <dev/ale/if_alevar.h>
743c6e15bcSPyun YongHyeon 
753c6e15bcSPyun YongHyeon /* "device miibus" required.  See GENERIC if you get errors here. */
763c6e15bcSPyun YongHyeon #include "miibus_if.h"
773c6e15bcSPyun YongHyeon 
783c6e15bcSPyun YongHyeon /* For more information about Tx checksum offload issues see ale_encap(). */
793c6e15bcSPyun YongHyeon #define	ALE_CSUM_FEATURES	(CSUM_TCP | CSUM_UDP)
803c6e15bcSPyun YongHyeon 
813c6e15bcSPyun YongHyeon MODULE_DEPEND(ale, pci, 1, 1, 1);
823c6e15bcSPyun YongHyeon MODULE_DEPEND(ale, ether, 1, 1, 1);
833c6e15bcSPyun YongHyeon MODULE_DEPEND(ale, miibus, 1, 1, 1);
843c6e15bcSPyun YongHyeon 
853c6e15bcSPyun YongHyeon /* Tunables. */
863c6e15bcSPyun YongHyeon static int msi_disable = 0;
873c6e15bcSPyun YongHyeon static int msix_disable = 0;
883c6e15bcSPyun YongHyeon TUNABLE_INT("hw.ale.msi_disable", &msi_disable);
893c6e15bcSPyun YongHyeon TUNABLE_INT("hw.ale.msix_disable", &msix_disable);
903c6e15bcSPyun YongHyeon 
913c6e15bcSPyun YongHyeon /*
923c6e15bcSPyun YongHyeon  * Devices supported by this driver.
933c6e15bcSPyun YongHyeon  */
9426a93551SMarius Strobl static const struct ale_dev {
953c6e15bcSPyun YongHyeon 	uint16_t	ale_vendorid;
963c6e15bcSPyun YongHyeon 	uint16_t	ale_deviceid;
973c6e15bcSPyun YongHyeon 	const char	*ale_name;
9829658c96SDimitry Andric } ale_devs[] = {
993c6e15bcSPyun YongHyeon     { VENDORID_ATHEROS, DEVICEID_ATHEROS_AR81XX,
1003c6e15bcSPyun YongHyeon     "Atheros AR8121/AR8113/AR8114 PCIe Ethernet" },
1013c6e15bcSPyun YongHyeon };
1023c6e15bcSPyun YongHyeon 
1033c6e15bcSPyun YongHyeon static int	ale_attach(device_t);
1043c6e15bcSPyun YongHyeon static int	ale_check_boundary(struct ale_softc *);
1053c6e15bcSPyun YongHyeon static int	ale_detach(device_t);
1063c6e15bcSPyun YongHyeon static int	ale_dma_alloc(struct ale_softc *);
1073c6e15bcSPyun YongHyeon static void	ale_dma_free(struct ale_softc *);
1083c6e15bcSPyun YongHyeon static void	ale_dmamap_cb(void *, bus_dma_segment_t *, int, int);
1093c6e15bcSPyun YongHyeon static int	ale_encap(struct ale_softc *, struct mbuf **);
1103c6e15bcSPyun YongHyeon static void	ale_get_macaddr(struct ale_softc *);
1113c6e15bcSPyun YongHyeon static void	ale_init(void *);
1123c6e15bcSPyun YongHyeon static void	ale_init_locked(struct ale_softc *);
1133c6e15bcSPyun YongHyeon static void	ale_init_rx_pages(struct ale_softc *);
1143c6e15bcSPyun YongHyeon static void	ale_init_tx_ring(struct ale_softc *);
1153c6e15bcSPyun YongHyeon static void	ale_int_task(void *, int);
1163c6e15bcSPyun YongHyeon static int	ale_intr(void *);
11752436412SJustin Hibbits static int	ale_ioctl(if_t, u_long, caddr_t);
1183c6e15bcSPyun YongHyeon static void	ale_mac_config(struct ale_softc *);
1193c6e15bcSPyun YongHyeon static int	ale_miibus_readreg(device_t, int, int);
1203c6e15bcSPyun YongHyeon static void	ale_miibus_statchg(device_t);
1213c6e15bcSPyun YongHyeon static int	ale_miibus_writereg(device_t, int, int, int);
12252436412SJustin Hibbits static int	ale_mediachange(if_t);
12352436412SJustin Hibbits static void	ale_mediastatus(if_t, struct ifmediareq *);
1243c6e15bcSPyun YongHyeon static void	ale_phy_reset(struct ale_softc *);
1253c6e15bcSPyun YongHyeon static int	ale_probe(device_t);
1263c6e15bcSPyun YongHyeon static void	ale_reset(struct ale_softc *);
1273c6e15bcSPyun YongHyeon static int	ale_resume(device_t);
1283c6e15bcSPyun YongHyeon static void	ale_rx_update_page(struct ale_softc *, struct ale_rx_page **,
1293c6e15bcSPyun YongHyeon     uint32_t, uint32_t *);
1303c6e15bcSPyun YongHyeon static void	ale_rxcsum(struct ale_softc *, struct mbuf *, uint32_t);
1313c6e15bcSPyun YongHyeon static int	ale_rxeof(struct ale_softc *sc, int);
1323c6e15bcSPyun YongHyeon static void	ale_rxfilter(struct ale_softc *);
1333c6e15bcSPyun YongHyeon static void	ale_rxvlan(struct ale_softc *);
1343c6e15bcSPyun YongHyeon static void	ale_setlinkspeed(struct ale_softc *);
1353c6e15bcSPyun YongHyeon static void	ale_setwol(struct ale_softc *);
1363c6e15bcSPyun YongHyeon static int	ale_shutdown(device_t);
13752436412SJustin Hibbits static void	ale_start(if_t);
13852436412SJustin Hibbits static void	ale_start_locked(if_t);
1393c6e15bcSPyun YongHyeon static void	ale_stats_clear(struct ale_softc *);
1403c6e15bcSPyun YongHyeon static void	ale_stats_update(struct ale_softc *);
1413c6e15bcSPyun YongHyeon static void	ale_stop(struct ale_softc *);
1423c6e15bcSPyun YongHyeon static void	ale_stop_mac(struct ale_softc *);
1433c6e15bcSPyun YongHyeon static int	ale_suspend(device_t);
1443c6e15bcSPyun YongHyeon static void	ale_sysctl_node(struct ale_softc *);
1453c6e15bcSPyun YongHyeon static void	ale_tick(void *);
1463c6e15bcSPyun YongHyeon static void	ale_txeof(struct ale_softc *);
1473c6e15bcSPyun YongHyeon static void	ale_watchdog(struct ale_softc *);
1483c6e15bcSPyun YongHyeon static int	sysctl_int_range(SYSCTL_HANDLER_ARGS, int, int);
1493c6e15bcSPyun YongHyeon static int	sysctl_hw_ale_proc_limit(SYSCTL_HANDLER_ARGS);
1503c6e15bcSPyun YongHyeon static int	sysctl_hw_ale_int_mod(SYSCTL_HANDLER_ARGS);
1513c6e15bcSPyun YongHyeon 
1523c6e15bcSPyun YongHyeon static device_method_t ale_methods[] = {
1533c6e15bcSPyun YongHyeon 	/* Device interface. */
1543c6e15bcSPyun YongHyeon 	DEVMETHOD(device_probe,		ale_probe),
1553c6e15bcSPyun YongHyeon 	DEVMETHOD(device_attach,	ale_attach),
1563c6e15bcSPyun YongHyeon 	DEVMETHOD(device_detach,	ale_detach),
1573c6e15bcSPyun YongHyeon 	DEVMETHOD(device_shutdown,	ale_shutdown),
1583c6e15bcSPyun YongHyeon 	DEVMETHOD(device_suspend,	ale_suspend),
1593c6e15bcSPyun YongHyeon 	DEVMETHOD(device_resume,	ale_resume),
1603c6e15bcSPyun YongHyeon 
1613c6e15bcSPyun YongHyeon 	/* MII interface. */
1623c6e15bcSPyun YongHyeon 	DEVMETHOD(miibus_readreg,	ale_miibus_readreg),
1633c6e15bcSPyun YongHyeon 	DEVMETHOD(miibus_writereg,	ale_miibus_writereg),
1643c6e15bcSPyun YongHyeon 	DEVMETHOD(miibus_statchg,	ale_miibus_statchg),
1653c6e15bcSPyun YongHyeon 
16626a93551SMarius Strobl 	DEVMETHOD_END
1673c6e15bcSPyun YongHyeon };
1683c6e15bcSPyun YongHyeon 
1693c6e15bcSPyun YongHyeon static driver_t ale_driver = {
1703c6e15bcSPyun YongHyeon 	"ale",
1713c6e15bcSPyun YongHyeon 	ale_methods,
1723c6e15bcSPyun YongHyeon 	sizeof(struct ale_softc)
1733c6e15bcSPyun YongHyeon };
1743c6e15bcSPyun YongHyeon 
175cf80fdefSJohn Baldwin DRIVER_MODULE(ale, pci, ale_driver, NULL, NULL);
176ef50201aSWarner Losh MODULE_PNP_INFO("U16:vendor;U16:device;D:#", pci, ale, ale_devs,
177329e817fSWarner Losh     nitems(ale_devs));
1783e38757dSJohn Baldwin DRIVER_MODULE(miibus, ale, miibus_driver, NULL, NULL);
1793c6e15bcSPyun YongHyeon 
1803c6e15bcSPyun YongHyeon static struct resource_spec ale_res_spec_mem[] = {
1813c6e15bcSPyun YongHyeon 	{ SYS_RES_MEMORY,	PCIR_BAR(0),	RF_ACTIVE },
1823c6e15bcSPyun YongHyeon 	{ -1,			0,		0 }
1833c6e15bcSPyun YongHyeon };
1843c6e15bcSPyun YongHyeon 
1853c6e15bcSPyun YongHyeon static struct resource_spec ale_irq_spec_legacy[] = {
1863c6e15bcSPyun YongHyeon 	{ SYS_RES_IRQ,		0,		RF_ACTIVE | RF_SHAREABLE },
1873c6e15bcSPyun YongHyeon 	{ -1,			0,		0 }
1883c6e15bcSPyun YongHyeon };
1893c6e15bcSPyun YongHyeon 
1903c6e15bcSPyun YongHyeon static struct resource_spec ale_irq_spec_msi[] = {
1913c6e15bcSPyun YongHyeon 	{ SYS_RES_IRQ,		1,		RF_ACTIVE },
1923c6e15bcSPyun YongHyeon 	{ -1,			0,		0 }
1933c6e15bcSPyun YongHyeon };
1943c6e15bcSPyun YongHyeon 
1953c6e15bcSPyun YongHyeon static struct resource_spec ale_irq_spec_msix[] = {
1963c6e15bcSPyun YongHyeon 	{ SYS_RES_IRQ,		1,		RF_ACTIVE },
1973c6e15bcSPyun YongHyeon 	{ -1,			0,		0 }
1983c6e15bcSPyun YongHyeon };
1993c6e15bcSPyun YongHyeon 
2003c6e15bcSPyun YongHyeon static int
ale_miibus_readreg(device_t dev,int phy,int reg)2013c6e15bcSPyun YongHyeon ale_miibus_readreg(device_t dev, int phy, int reg)
2023c6e15bcSPyun YongHyeon {
2033c6e15bcSPyun YongHyeon 	struct ale_softc *sc;
2043c6e15bcSPyun YongHyeon 	uint32_t v;
2053c6e15bcSPyun YongHyeon 	int i;
2063c6e15bcSPyun YongHyeon 
2073c6e15bcSPyun YongHyeon 	sc = device_get_softc(dev);
2083c6e15bcSPyun YongHyeon 
2093c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_MDIO, MDIO_OP_EXECUTE | MDIO_OP_READ |
2103c6e15bcSPyun YongHyeon 	    MDIO_SUP_PREAMBLE | MDIO_CLK_25_4 | MDIO_REG_ADDR(reg));
2113c6e15bcSPyun YongHyeon 	for (i = ALE_PHY_TIMEOUT; i > 0; i--) {
2123c6e15bcSPyun YongHyeon 		DELAY(5);
2133c6e15bcSPyun YongHyeon 		v = CSR_READ_4(sc, ALE_MDIO);
2143c6e15bcSPyun YongHyeon 		if ((v & (MDIO_OP_EXECUTE | MDIO_OP_BUSY)) == 0)
2153c6e15bcSPyun YongHyeon 			break;
2163c6e15bcSPyun YongHyeon 	}
2173c6e15bcSPyun YongHyeon 
2183c6e15bcSPyun YongHyeon 	if (i == 0) {
2193c6e15bcSPyun YongHyeon 		device_printf(sc->ale_dev, "phy read timeout : %d\n", reg);
2203c6e15bcSPyun YongHyeon 		return (0);
2213c6e15bcSPyun YongHyeon 	}
2223c6e15bcSPyun YongHyeon 
2233c6e15bcSPyun YongHyeon 	return ((v & MDIO_DATA_MASK) >> MDIO_DATA_SHIFT);
2243c6e15bcSPyun YongHyeon }
2253c6e15bcSPyun YongHyeon 
2263c6e15bcSPyun YongHyeon static int
ale_miibus_writereg(device_t dev,int phy,int reg,int val)2273c6e15bcSPyun YongHyeon ale_miibus_writereg(device_t dev, int phy, int reg, int val)
2283c6e15bcSPyun YongHyeon {
2293c6e15bcSPyun YongHyeon 	struct ale_softc *sc;
2303c6e15bcSPyun YongHyeon 	uint32_t v;
2313c6e15bcSPyun YongHyeon 	int i;
2323c6e15bcSPyun YongHyeon 
2333c6e15bcSPyun YongHyeon 	sc = device_get_softc(dev);
2343c6e15bcSPyun YongHyeon 
2353c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_MDIO, MDIO_OP_EXECUTE | MDIO_OP_WRITE |
2363c6e15bcSPyun YongHyeon 	    (val & MDIO_DATA_MASK) << MDIO_DATA_SHIFT |
2373c6e15bcSPyun YongHyeon 	    MDIO_SUP_PREAMBLE | MDIO_CLK_25_4 | MDIO_REG_ADDR(reg));
2383c6e15bcSPyun YongHyeon 	for (i = ALE_PHY_TIMEOUT; i > 0; i--) {
2393c6e15bcSPyun YongHyeon 		DELAY(5);
2403c6e15bcSPyun YongHyeon 		v = CSR_READ_4(sc, ALE_MDIO);
2413c6e15bcSPyun YongHyeon 		if ((v & (MDIO_OP_EXECUTE | MDIO_OP_BUSY)) == 0)
2423c6e15bcSPyun YongHyeon 			break;
2433c6e15bcSPyun YongHyeon 	}
2443c6e15bcSPyun YongHyeon 
2453c6e15bcSPyun YongHyeon 	if (i == 0)
2463c6e15bcSPyun YongHyeon 		device_printf(sc->ale_dev, "phy write timeout : %d\n", reg);
2473c6e15bcSPyun YongHyeon 
2483c6e15bcSPyun YongHyeon 	return (0);
2493c6e15bcSPyun YongHyeon }
2503c6e15bcSPyun YongHyeon 
2513c6e15bcSPyun YongHyeon static void
ale_miibus_statchg(device_t dev)2523c6e15bcSPyun YongHyeon ale_miibus_statchg(device_t dev)
2533c6e15bcSPyun YongHyeon {
2543c6e15bcSPyun YongHyeon 	struct ale_softc *sc;
2552b6f7122SPyun YongHyeon 	struct mii_data *mii;
25652436412SJustin Hibbits 	if_t ifp;
2572b6f7122SPyun YongHyeon 	uint32_t reg;
2583c6e15bcSPyun YongHyeon 
2593c6e15bcSPyun YongHyeon 	sc = device_get_softc(dev);
2602b6f7122SPyun YongHyeon 	mii = device_get_softc(sc->ale_miibus);
2612b6f7122SPyun YongHyeon 	ifp = sc->ale_ifp;
2622b6f7122SPyun YongHyeon 	if (mii == NULL || ifp == NULL ||
26352436412SJustin Hibbits 	    (if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0)
2642b6f7122SPyun YongHyeon 		return;
2653c6e15bcSPyun YongHyeon 
2662b6f7122SPyun YongHyeon 	sc->ale_flags &= ~ALE_FLAG_LINK;
2672b6f7122SPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
2682b6f7122SPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
2692b6f7122SPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
2702b6f7122SPyun YongHyeon 		case IFM_10_T:
2712b6f7122SPyun YongHyeon 		case IFM_100_TX:
2722b6f7122SPyun YongHyeon 			sc->ale_flags |= ALE_FLAG_LINK;
2732b6f7122SPyun YongHyeon 			break;
2742b6f7122SPyun YongHyeon 		case IFM_1000_T:
2752b6f7122SPyun YongHyeon 			if ((sc->ale_flags & ALE_FLAG_FASTETHER) == 0)
2762b6f7122SPyun YongHyeon 				sc->ale_flags |= ALE_FLAG_LINK;
2772b6f7122SPyun YongHyeon 			break;
2782b6f7122SPyun YongHyeon 		default:
2792b6f7122SPyun YongHyeon 			break;
2802b6f7122SPyun YongHyeon 		}
2812b6f7122SPyun YongHyeon 	}
2822b6f7122SPyun YongHyeon 
2832b6f7122SPyun YongHyeon 	/* Stop Rx/Tx MACs. */
2842b6f7122SPyun YongHyeon 	ale_stop_mac(sc);
2852b6f7122SPyun YongHyeon 
2862b6f7122SPyun YongHyeon 	/* Program MACs with resolved speed/duplex/flow-control. */
2872b6f7122SPyun YongHyeon 	if ((sc->ale_flags & ALE_FLAG_LINK) != 0) {
2882b6f7122SPyun YongHyeon 		ale_mac_config(sc);
2892b6f7122SPyun YongHyeon 		/* Reenable Tx/Rx MACs. */
2902b6f7122SPyun YongHyeon 		reg = CSR_READ_4(sc, ALE_MAC_CFG);
2912b6f7122SPyun YongHyeon 		reg |= MAC_CFG_TX_ENB | MAC_CFG_RX_ENB;
2922b6f7122SPyun YongHyeon 		CSR_WRITE_4(sc, ALE_MAC_CFG, reg);
2932b6f7122SPyun YongHyeon 	}
2943c6e15bcSPyun YongHyeon }
2953c6e15bcSPyun YongHyeon 
2963c6e15bcSPyun YongHyeon static void
ale_mediastatus(if_t ifp,struct ifmediareq * ifmr)29752436412SJustin Hibbits ale_mediastatus(if_t ifp, struct ifmediareq *ifmr)
2983c6e15bcSPyun YongHyeon {
2993c6e15bcSPyun YongHyeon 	struct ale_softc *sc;
3003c6e15bcSPyun YongHyeon 	struct mii_data *mii;
3013c6e15bcSPyun YongHyeon 
30252436412SJustin Hibbits 	sc = if_getsoftc(ifp);
3033c6e15bcSPyun YongHyeon 	ALE_LOCK(sc);
30452436412SJustin Hibbits 	if ((if_getflags(ifp) & IFF_UP) == 0) {
305fd3d448fSPyun YongHyeon 		ALE_UNLOCK(sc);
306fd3d448fSPyun YongHyeon 		return;
307fd3d448fSPyun YongHyeon 	}
3083c6e15bcSPyun YongHyeon 	mii = device_get_softc(sc->ale_miibus);
3093c6e15bcSPyun YongHyeon 
3103c6e15bcSPyun YongHyeon 	mii_pollstat(mii);
3113c6e15bcSPyun YongHyeon 	ifmr->ifm_status = mii->mii_media_status;
3123c6e15bcSPyun YongHyeon 	ifmr->ifm_active = mii->mii_media_active;
31357c81d92SPyun YongHyeon 	ALE_UNLOCK(sc);
3143c6e15bcSPyun YongHyeon }
3153c6e15bcSPyun YongHyeon 
3163c6e15bcSPyun YongHyeon static int
ale_mediachange(if_t ifp)31752436412SJustin Hibbits ale_mediachange(if_t ifp)
3183c6e15bcSPyun YongHyeon {
3193c6e15bcSPyun YongHyeon 	struct ale_softc *sc;
3203c6e15bcSPyun YongHyeon 	struct mii_data *mii;
3213c6e15bcSPyun YongHyeon 	struct mii_softc *miisc;
3223c6e15bcSPyun YongHyeon 	int error;
3233c6e15bcSPyun YongHyeon 
32452436412SJustin Hibbits 	sc = if_getsoftc(ifp);
3253c6e15bcSPyun YongHyeon 	ALE_LOCK(sc);
3263c6e15bcSPyun YongHyeon 	mii = device_get_softc(sc->ale_miibus);
3273c6e15bcSPyun YongHyeon 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
3283fcb7a53SMarius Strobl 		PHY_RESET(miisc);
3293c6e15bcSPyun YongHyeon 	error = mii_mediachg(mii);
3303c6e15bcSPyun YongHyeon 	ALE_UNLOCK(sc);
3313c6e15bcSPyun YongHyeon 
3323c6e15bcSPyun YongHyeon 	return (error);
3333c6e15bcSPyun YongHyeon }
3343c6e15bcSPyun YongHyeon 
3353c6e15bcSPyun YongHyeon static int
ale_probe(device_t dev)3363c6e15bcSPyun YongHyeon ale_probe(device_t dev)
3373c6e15bcSPyun YongHyeon {
33826a93551SMarius Strobl 	const struct ale_dev *sp;
3393c6e15bcSPyun YongHyeon 	int i;
3403c6e15bcSPyun YongHyeon 	uint16_t vendor, devid;
3413c6e15bcSPyun YongHyeon 
3423c6e15bcSPyun YongHyeon 	vendor = pci_get_vendor(dev);
3433c6e15bcSPyun YongHyeon 	devid = pci_get_device(dev);
3443c6e15bcSPyun YongHyeon 	sp = ale_devs;
34573a1170aSPedro F. Giffuni 	for (i = 0; i < nitems(ale_devs); i++) {
3463c6e15bcSPyun YongHyeon 		if (vendor == sp->ale_vendorid &&
3473c6e15bcSPyun YongHyeon 		    devid == sp->ale_deviceid) {
3483c6e15bcSPyun YongHyeon 			device_set_desc(dev, sp->ale_name);
3493c6e15bcSPyun YongHyeon 			return (BUS_PROBE_DEFAULT);
3503c6e15bcSPyun YongHyeon 		}
3513c6e15bcSPyun YongHyeon 		sp++;
3523c6e15bcSPyun YongHyeon 	}
3533c6e15bcSPyun YongHyeon 
3543c6e15bcSPyun YongHyeon 	return (ENXIO);
3553c6e15bcSPyun YongHyeon }
3563c6e15bcSPyun YongHyeon 
3573c6e15bcSPyun YongHyeon static void
ale_get_macaddr(struct ale_softc * sc)3583c6e15bcSPyun YongHyeon ale_get_macaddr(struct ale_softc *sc)
3593c6e15bcSPyun YongHyeon {
3603c6e15bcSPyun YongHyeon 	uint32_t ea[2], reg;
3613c6e15bcSPyun YongHyeon 	int i, vpdc;
3623c6e15bcSPyun YongHyeon 
3633c6e15bcSPyun YongHyeon 	reg = CSR_READ_4(sc, ALE_SPI_CTRL);
3643c6e15bcSPyun YongHyeon 	if ((reg & SPI_VPD_ENB) != 0) {
3653c6e15bcSPyun YongHyeon 		reg &= ~SPI_VPD_ENB;
3663c6e15bcSPyun YongHyeon 		CSR_WRITE_4(sc, ALE_SPI_CTRL, reg);
3673c6e15bcSPyun YongHyeon 	}
3683c6e15bcSPyun YongHyeon 
3693b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->ale_dev, PCIY_VPD, &vpdc) == 0) {
3703c6e15bcSPyun YongHyeon 		/*
3713c6e15bcSPyun YongHyeon 		 * PCI VPD capability found, let TWSI reload EEPROM.
3723c6e15bcSPyun YongHyeon 		 * This will set ethernet address of controller.
3733c6e15bcSPyun YongHyeon 		 */
3743c6e15bcSPyun YongHyeon 		CSR_WRITE_4(sc, ALE_TWSI_CTRL, CSR_READ_4(sc, ALE_TWSI_CTRL) |
3753c6e15bcSPyun YongHyeon 		    TWSI_CTRL_SW_LD_START);
3763c6e15bcSPyun YongHyeon 		for (i = 100; i > 0; i--) {
3773c6e15bcSPyun YongHyeon 			DELAY(1000);
3783c6e15bcSPyun YongHyeon 			reg = CSR_READ_4(sc, ALE_TWSI_CTRL);
3793c6e15bcSPyun YongHyeon 			if ((reg & TWSI_CTRL_SW_LD_START) == 0)
3803c6e15bcSPyun YongHyeon 				break;
3813c6e15bcSPyun YongHyeon 		}
3823c6e15bcSPyun YongHyeon 		if (i == 0)
3833c6e15bcSPyun YongHyeon 			device_printf(sc->ale_dev,
3843c6e15bcSPyun YongHyeon 			    "reloading EEPROM timeout!\n");
3853c6e15bcSPyun YongHyeon 	} else {
3863c6e15bcSPyun YongHyeon 		if (bootverbose)
3873c6e15bcSPyun YongHyeon 			device_printf(sc->ale_dev,
3883c6e15bcSPyun YongHyeon 			    "PCI VPD capability not found!\n");
3893c6e15bcSPyun YongHyeon 	}
3903c6e15bcSPyun YongHyeon 
3913c6e15bcSPyun YongHyeon 	ea[0] = CSR_READ_4(sc, ALE_PAR0);
3923c6e15bcSPyun YongHyeon 	ea[1] = CSR_READ_4(sc, ALE_PAR1);
3933c6e15bcSPyun YongHyeon 	sc->ale_eaddr[0] = (ea[1] >> 8) & 0xFF;
3943c6e15bcSPyun YongHyeon 	sc->ale_eaddr[1] = (ea[1] >> 0) & 0xFF;
3953c6e15bcSPyun YongHyeon 	sc->ale_eaddr[2] = (ea[0] >> 24) & 0xFF;
3963c6e15bcSPyun YongHyeon 	sc->ale_eaddr[3] = (ea[0] >> 16) & 0xFF;
3973c6e15bcSPyun YongHyeon 	sc->ale_eaddr[4] = (ea[0] >> 8) & 0xFF;
3983c6e15bcSPyun YongHyeon 	sc->ale_eaddr[5] = (ea[0] >> 0) & 0xFF;
3993c6e15bcSPyun YongHyeon }
4003c6e15bcSPyun YongHyeon 
4013c6e15bcSPyun YongHyeon static void
ale_phy_reset(struct ale_softc * sc)4023c6e15bcSPyun YongHyeon ale_phy_reset(struct ale_softc *sc)
4033c6e15bcSPyun YongHyeon {
4043c6e15bcSPyun YongHyeon 
4053c6e15bcSPyun YongHyeon 	/* Reset magic from Linux. */
4063c6e15bcSPyun YongHyeon 	CSR_WRITE_2(sc, ALE_GPHY_CTRL,
4073c6e15bcSPyun YongHyeon 	    GPHY_CTRL_HIB_EN | GPHY_CTRL_HIB_PULSE | GPHY_CTRL_SEL_ANA_RESET |
4083c6e15bcSPyun YongHyeon 	    GPHY_CTRL_PHY_PLL_ON);
4093c6e15bcSPyun YongHyeon 	DELAY(1000);
4103c6e15bcSPyun YongHyeon 	CSR_WRITE_2(sc, ALE_GPHY_CTRL,
4113c6e15bcSPyun YongHyeon 	    GPHY_CTRL_EXT_RESET | GPHY_CTRL_HIB_EN | GPHY_CTRL_HIB_PULSE |
4123c6e15bcSPyun YongHyeon 	    GPHY_CTRL_SEL_ANA_RESET | GPHY_CTRL_PHY_PLL_ON);
4133c6e15bcSPyun YongHyeon 	DELAY(1000);
41419042fb8SPyun YongHyeon 
41519042fb8SPyun YongHyeon #define	ATPHY_DBG_ADDR		0x1D
41619042fb8SPyun YongHyeon #define	ATPHY_DBG_DATA		0x1E
41719042fb8SPyun YongHyeon 
41819042fb8SPyun YongHyeon 	/* Enable hibernation mode. */
41919042fb8SPyun YongHyeon 	ale_miibus_writereg(sc->ale_dev, sc->ale_phyaddr,
42019042fb8SPyun YongHyeon 	    ATPHY_DBG_ADDR, 0x0B);
42119042fb8SPyun YongHyeon 	ale_miibus_writereg(sc->ale_dev, sc->ale_phyaddr,
42219042fb8SPyun YongHyeon 	    ATPHY_DBG_DATA, 0xBC00);
42319042fb8SPyun YongHyeon 	/* Set Class A/B for all modes. */
42419042fb8SPyun YongHyeon 	ale_miibus_writereg(sc->ale_dev, sc->ale_phyaddr,
42519042fb8SPyun YongHyeon 	    ATPHY_DBG_ADDR, 0x00);
42619042fb8SPyun YongHyeon 	ale_miibus_writereg(sc->ale_dev, sc->ale_phyaddr,
42719042fb8SPyun YongHyeon 	    ATPHY_DBG_DATA, 0x02EF);
42819042fb8SPyun YongHyeon 	/* Enable 10BT power saving. */
42919042fb8SPyun YongHyeon 	ale_miibus_writereg(sc->ale_dev, sc->ale_phyaddr,
43019042fb8SPyun YongHyeon 	    ATPHY_DBG_ADDR, 0x12);
43119042fb8SPyun YongHyeon 	ale_miibus_writereg(sc->ale_dev, sc->ale_phyaddr,
43219042fb8SPyun YongHyeon 	    ATPHY_DBG_DATA, 0x4C04);
43319042fb8SPyun YongHyeon 	/* Adjust 1000T power. */
43419042fb8SPyun YongHyeon 	ale_miibus_writereg(sc->ale_dev, sc->ale_phyaddr,
43519042fb8SPyun YongHyeon 	    ATPHY_DBG_ADDR, 0x04);
43619042fb8SPyun YongHyeon 	ale_miibus_writereg(sc->ale_dev, sc->ale_phyaddr,
43719042fb8SPyun YongHyeon 	    ATPHY_DBG_ADDR, 0x8BBB);
43819042fb8SPyun YongHyeon 	/* 10BT center tap voltage. */
43919042fb8SPyun YongHyeon 	ale_miibus_writereg(sc->ale_dev, sc->ale_phyaddr,
44019042fb8SPyun YongHyeon 	    ATPHY_DBG_ADDR, 0x05);
44119042fb8SPyun YongHyeon 	ale_miibus_writereg(sc->ale_dev, sc->ale_phyaddr,
44219042fb8SPyun YongHyeon 	    ATPHY_DBG_ADDR, 0x2C46);
44319042fb8SPyun YongHyeon 
44419042fb8SPyun YongHyeon #undef	ATPHY_DBG_ADDR
44519042fb8SPyun YongHyeon #undef	ATPHY_DBG_DATA
44619042fb8SPyun YongHyeon 	DELAY(1000);
4473c6e15bcSPyun YongHyeon }
4483c6e15bcSPyun YongHyeon 
4493c6e15bcSPyun YongHyeon static int
ale_attach(device_t dev)4503c6e15bcSPyun YongHyeon ale_attach(device_t dev)
4513c6e15bcSPyun YongHyeon {
4523c6e15bcSPyun YongHyeon 	struct ale_softc *sc;
45352436412SJustin Hibbits 	if_t ifp;
4543c6e15bcSPyun YongHyeon 	uint16_t burst;
455*ddaf6524SJohn Baldwin 	int error, i, msic, msixc;
4563c6e15bcSPyun YongHyeon 	uint32_t rxf_len, txf_len;
4573c6e15bcSPyun YongHyeon 
4583c6e15bcSPyun YongHyeon 	error = 0;
4593c6e15bcSPyun YongHyeon 	sc = device_get_softc(dev);
4603c6e15bcSPyun YongHyeon 	sc->ale_dev = dev;
4613c6e15bcSPyun YongHyeon 
4623c6e15bcSPyun YongHyeon 	mtx_init(&sc->ale_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
4633c6e15bcSPyun YongHyeon 	    MTX_DEF);
4643c6e15bcSPyun YongHyeon 	callout_init_mtx(&sc->ale_tick_ch, &sc->ale_mtx, 0);
4656c3e93cbSGleb Smirnoff 	NET_TASK_INIT(&sc->ale_int_task, 0, ale_int_task, sc);
4663c6e15bcSPyun YongHyeon 
4673c6e15bcSPyun YongHyeon 	/* Map the device. */
4683c6e15bcSPyun YongHyeon 	pci_enable_busmaster(dev);
4693c6e15bcSPyun YongHyeon 	sc->ale_res_spec = ale_res_spec_mem;
4703c6e15bcSPyun YongHyeon 	sc->ale_irq_spec = ale_irq_spec_legacy;
4713c6e15bcSPyun YongHyeon 	error = bus_alloc_resources(dev, sc->ale_res_spec, sc->ale_res);
4723c6e15bcSPyun YongHyeon 	if (error != 0) {
4733c6e15bcSPyun YongHyeon 		device_printf(dev, "cannot allocate memory resources.\n");
4743c6e15bcSPyun YongHyeon 		goto fail;
4753c6e15bcSPyun YongHyeon 	}
4763c6e15bcSPyun YongHyeon 
4773c6e15bcSPyun YongHyeon 	/* Set PHY address. */
4783c6e15bcSPyun YongHyeon 	sc->ale_phyaddr = ALE_PHY_ADDR;
4793c6e15bcSPyun YongHyeon 
4803c6e15bcSPyun YongHyeon 	/* Reset PHY. */
4813c6e15bcSPyun YongHyeon 	ale_phy_reset(sc);
4823c6e15bcSPyun YongHyeon 
4833c6e15bcSPyun YongHyeon 	/* Reset the ethernet controller. */
4843c6e15bcSPyun YongHyeon 	ale_reset(sc);
4853c6e15bcSPyun YongHyeon 
4863c6e15bcSPyun YongHyeon 	/* Get PCI and chip id/revision. */
4873c6e15bcSPyun YongHyeon 	sc->ale_rev = pci_get_revid(dev);
4883c6e15bcSPyun YongHyeon 	if (sc->ale_rev >= 0xF0) {
4893c6e15bcSPyun YongHyeon 		/* L2E Rev. B. AR8114 */
4903c6e15bcSPyun YongHyeon 		sc->ale_flags |= ALE_FLAG_FASTETHER;
4913c6e15bcSPyun YongHyeon 	} else {
4923c6e15bcSPyun YongHyeon 		if ((CSR_READ_4(sc, ALE_PHY_STATUS) & PHY_STATUS_100M) != 0) {
4933c6e15bcSPyun YongHyeon 			/* L1E AR8121 */
4943c6e15bcSPyun YongHyeon 			sc->ale_flags |= ALE_FLAG_JUMBO;
4953c6e15bcSPyun YongHyeon 		} else {
4963c6e15bcSPyun YongHyeon 			/* L2E Rev. A. AR8113 */
4973c6e15bcSPyun YongHyeon 			sc->ale_flags |= ALE_FLAG_FASTETHER;
4983c6e15bcSPyun YongHyeon 		}
4993c6e15bcSPyun YongHyeon 	}
5003c6e15bcSPyun YongHyeon 	/*
5013c6e15bcSPyun YongHyeon 	 * All known controllers seems to require 4 bytes alignment
5023c6e15bcSPyun YongHyeon 	 * of Tx buffers to make Tx checksum offload with custom
5033c6e15bcSPyun YongHyeon 	 * checksum generation method work.
5043c6e15bcSPyun YongHyeon 	 */
5053c6e15bcSPyun YongHyeon 	sc->ale_flags |= ALE_FLAG_TXCSUM_BUG;
5063c6e15bcSPyun YongHyeon 	/*
5073c6e15bcSPyun YongHyeon 	 * All known controllers seems to have issues on Rx checksum
5083c6e15bcSPyun YongHyeon 	 * offload for fragmented IP datagrams.
5093c6e15bcSPyun YongHyeon 	 */
5103c6e15bcSPyun YongHyeon 	sc->ale_flags |= ALE_FLAG_RXCSUM_BUG;
5113c6e15bcSPyun YongHyeon 	/*
5123c6e15bcSPyun YongHyeon 	 * Don't use Tx CMB. It is known to cause RRS update failure
5133c6e15bcSPyun YongHyeon 	 * under certain circumstances. Typical phenomenon of the
5143c6e15bcSPyun YongHyeon 	 * issue would be unexpected sequence number encountered in
5153c6e15bcSPyun YongHyeon 	 * Rx handler.
5163c6e15bcSPyun YongHyeon 	 */
5173c6e15bcSPyun YongHyeon 	sc->ale_flags |= ALE_FLAG_TXCMB_BUG;
5183c6e15bcSPyun YongHyeon 	sc->ale_chip_rev = CSR_READ_4(sc, ALE_MASTER_CFG) >>
5193c6e15bcSPyun YongHyeon 	    MASTER_CHIP_REV_SHIFT;
5203c6e15bcSPyun YongHyeon 	if (bootverbose) {
5213c6e15bcSPyun YongHyeon 		device_printf(dev, "PCI device revision : 0x%04x\n",
5223c6e15bcSPyun YongHyeon 		    sc->ale_rev);
5233c6e15bcSPyun YongHyeon 		device_printf(dev, "Chip id/revision : 0x%04x\n",
5243c6e15bcSPyun YongHyeon 		    sc->ale_chip_rev);
5253c6e15bcSPyun YongHyeon 	}
5263c6e15bcSPyun YongHyeon 	txf_len = CSR_READ_4(sc, ALE_SRAM_TX_FIFO_LEN);
5273c6e15bcSPyun YongHyeon 	rxf_len = CSR_READ_4(sc, ALE_SRAM_RX_FIFO_LEN);
5283c6e15bcSPyun YongHyeon 	/*
5293c6e15bcSPyun YongHyeon 	 * Uninitialized hardware returns an invalid chip id/revision
5303c6e15bcSPyun YongHyeon 	 * as well as 0xFFFFFFFF for Tx/Rx fifo length.
5313c6e15bcSPyun YongHyeon 	 */
5323c6e15bcSPyun YongHyeon 	if (sc->ale_chip_rev == 0xFFFF || txf_len == 0xFFFFFFFF ||
5333c6e15bcSPyun YongHyeon 	    rxf_len == 0xFFFFFFF) {
5343c6e15bcSPyun YongHyeon 		device_printf(dev,"chip revision : 0x%04x, %u Tx FIFO "
5353c6e15bcSPyun YongHyeon 		    "%u Rx FIFO -- not initialized?\n", sc->ale_chip_rev,
5363c6e15bcSPyun YongHyeon 		    txf_len, rxf_len);
5373c6e15bcSPyun YongHyeon 		error = ENXIO;
5383c6e15bcSPyun YongHyeon 		goto fail;
5393c6e15bcSPyun YongHyeon 	}
5403c6e15bcSPyun YongHyeon 	device_printf(dev, "%u Tx FIFO, %u Rx FIFO\n", txf_len, rxf_len);
5413c6e15bcSPyun YongHyeon 
5423c6e15bcSPyun YongHyeon 	/* Allocate IRQ resources. */
5433c6e15bcSPyun YongHyeon 	msixc = pci_msix_count(dev);
5443c6e15bcSPyun YongHyeon 	msic = pci_msi_count(dev);
5453c6e15bcSPyun YongHyeon 	if (bootverbose) {
5463c6e15bcSPyun YongHyeon 		device_printf(dev, "MSIX count : %d\n", msixc);
5473c6e15bcSPyun YongHyeon 		device_printf(dev, "MSI count : %d\n", msic);
5483c6e15bcSPyun YongHyeon 	}
5493c6e15bcSPyun YongHyeon 
5503c6e15bcSPyun YongHyeon 	/* Prefer MSIX over MSI. */
5513c6e15bcSPyun YongHyeon 	if (msix_disable == 0 || msi_disable == 0) {
5523c6e15bcSPyun YongHyeon 		if (msix_disable == 0 && msixc == ALE_MSIX_MESSAGES &&
5533c6e15bcSPyun YongHyeon 		    pci_alloc_msix(dev, &msixc) == 0) {
5549dd3fa37SPyun YongHyeon 			if (msixc == ALE_MSIX_MESSAGES) {
5553c6e15bcSPyun YongHyeon 				device_printf(dev, "Using %d MSIX messages.\n",
5563c6e15bcSPyun YongHyeon 				    msixc);
5573c6e15bcSPyun YongHyeon 				sc->ale_flags |= ALE_FLAG_MSIX;
5583c6e15bcSPyun YongHyeon 				sc->ale_irq_spec = ale_irq_spec_msix;
5593c6e15bcSPyun YongHyeon 			} else
5603c6e15bcSPyun YongHyeon 				pci_release_msi(dev);
5613c6e15bcSPyun YongHyeon 		}
5623c6e15bcSPyun YongHyeon 		if (msi_disable == 0 && (sc->ale_flags & ALE_FLAG_MSIX) == 0 &&
5633c6e15bcSPyun YongHyeon 		    msic == ALE_MSI_MESSAGES &&
5643c6e15bcSPyun YongHyeon 		    pci_alloc_msi(dev, &msic) == 0) {
5653c6e15bcSPyun YongHyeon 			if (msic == ALE_MSI_MESSAGES) {
5663c6e15bcSPyun YongHyeon 				device_printf(dev, "Using %d MSI messages.\n",
5673c6e15bcSPyun YongHyeon 				    msic);
5683c6e15bcSPyun YongHyeon 				sc->ale_flags |= ALE_FLAG_MSI;
5693c6e15bcSPyun YongHyeon 				sc->ale_irq_spec = ale_irq_spec_msi;
5703c6e15bcSPyun YongHyeon 			} else
5713c6e15bcSPyun YongHyeon 				pci_release_msi(dev);
5723c6e15bcSPyun YongHyeon 		}
5733c6e15bcSPyun YongHyeon 	}
5743c6e15bcSPyun YongHyeon 
5753c6e15bcSPyun YongHyeon 	error = bus_alloc_resources(dev, sc->ale_irq_spec, sc->ale_irq);
5763c6e15bcSPyun YongHyeon 	if (error != 0) {
5773c6e15bcSPyun YongHyeon 		device_printf(dev, "cannot allocate IRQ resources.\n");
5783c6e15bcSPyun YongHyeon 		goto fail;
5793c6e15bcSPyun YongHyeon 	}
5803c6e15bcSPyun YongHyeon 
5813c6e15bcSPyun YongHyeon 	/* Get DMA parameters from PCIe device control register. */
5823b0a4aefSJohn Baldwin 	if (pci_find_cap(dev, PCIY_EXPRESS, &i) == 0) {
5833c6e15bcSPyun YongHyeon 		sc->ale_flags |= ALE_FLAG_PCIE;
5843c6e15bcSPyun YongHyeon 		burst = pci_read_config(dev, i + 0x08, 2);
5853c6e15bcSPyun YongHyeon 		/* Max read request size. */
5863c6e15bcSPyun YongHyeon 		sc->ale_dma_rd_burst = ((burst >> 12) & 0x07) <<
5873c6e15bcSPyun YongHyeon 		    DMA_CFG_RD_BURST_SHIFT;
5883c6e15bcSPyun YongHyeon 		/* Max payload size. */
5893c6e15bcSPyun YongHyeon 		sc->ale_dma_wr_burst = ((burst >> 5) & 0x07) <<
5903c6e15bcSPyun YongHyeon 		    DMA_CFG_WR_BURST_SHIFT;
5913c6e15bcSPyun YongHyeon 		if (bootverbose) {
5923c6e15bcSPyun YongHyeon 			device_printf(dev, "Read request size : %d bytes.\n",
5933c6e15bcSPyun YongHyeon 			    128 << ((burst >> 12) & 0x07));
5943c6e15bcSPyun YongHyeon 			device_printf(dev, "TLP payload size : %d bytes.\n",
5953c6e15bcSPyun YongHyeon 			    128 << ((burst >> 5) & 0x07));
5963c6e15bcSPyun YongHyeon 		}
5973c6e15bcSPyun YongHyeon 	} else {
5983c6e15bcSPyun YongHyeon 		sc->ale_dma_rd_burst = DMA_CFG_RD_BURST_128;
5993c6e15bcSPyun YongHyeon 		sc->ale_dma_wr_burst = DMA_CFG_WR_BURST_128;
6003c6e15bcSPyun YongHyeon 	}
6013c6e15bcSPyun YongHyeon 
6023c6e15bcSPyun YongHyeon 	/* Create device sysctl node. */
6033c6e15bcSPyun YongHyeon 	ale_sysctl_node(sc);
6043c6e15bcSPyun YongHyeon 
6059dda5c8fSPyun YongHyeon 	if ((error = ale_dma_alloc(sc)) != 0)
6063c6e15bcSPyun YongHyeon 		goto fail;
6073c6e15bcSPyun YongHyeon 
6083c6e15bcSPyun YongHyeon 	/* Load station address. */
6093c6e15bcSPyun YongHyeon 	ale_get_macaddr(sc);
6103c6e15bcSPyun YongHyeon 
6113c6e15bcSPyun YongHyeon 	ifp = sc->ale_ifp = if_alloc(IFT_ETHER);
61252436412SJustin Hibbits 	if_setsoftc(ifp, sc);
6133c6e15bcSPyun YongHyeon 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
61452436412SJustin Hibbits 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
61552436412SJustin Hibbits 	if_setioctlfn(ifp, ale_ioctl);
61652436412SJustin Hibbits 	if_setstartfn(ifp, ale_start);
61752436412SJustin Hibbits 	if_setinitfn(ifp, ale_init);
61852436412SJustin Hibbits 	if_setsendqlen(ifp, ALE_TX_RING_CNT - 1);
61952436412SJustin Hibbits 	if_setsendqready(ifp);
62052436412SJustin Hibbits 	if_setcapabilities(ifp, IFCAP_RXCSUM | IFCAP_TXCSUM | IFCAP_TSO4);
62152436412SJustin Hibbits 	if_sethwassist(ifp, ALE_CSUM_FEATURES | CSUM_TSO);
622*ddaf6524SJohn Baldwin 	if (pci_has_pm(dev)) {
62352436412SJustin Hibbits 		if_setcapabilitiesbit(ifp, IFCAP_WOL_MAGIC | IFCAP_WOL_MCAST, 0);
6243c6e15bcSPyun YongHyeon 	}
62552436412SJustin Hibbits 	if_setcapenable(ifp, if_getcapabilities(ifp));
6263c6e15bcSPyun YongHyeon 
6273c6e15bcSPyun YongHyeon 	/* Set up MII bus. */
6288e5d93dbSMarius Strobl 	error = mii_attach(dev, &sc->ale_miibus, ifp, ale_mediachange,
6298e5d93dbSMarius Strobl 	    ale_mediastatus, BMSR_DEFCAPMASK, sc->ale_phyaddr, MII_OFFSET_ANY,
63026a93551SMarius Strobl 	    MIIF_DOPAUSE);
6318e5d93dbSMarius Strobl 	if (error != 0) {
6328e5d93dbSMarius Strobl 		device_printf(dev, "attaching PHYs failed\n");
6333c6e15bcSPyun YongHyeon 		goto fail;
6343c6e15bcSPyun YongHyeon 	}
6353c6e15bcSPyun YongHyeon 
6363c6e15bcSPyun YongHyeon 	ether_ifattach(ifp, sc->ale_eaddr);
6373c6e15bcSPyun YongHyeon 
6383c6e15bcSPyun YongHyeon 	/* VLAN capability setup. */
63952436412SJustin Hibbits 	if_setcapabilitiesbit(ifp, IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING |
64052436412SJustin Hibbits 	    IFCAP_VLAN_HWCSUM | IFCAP_VLAN_HWTSO, 0);
64152436412SJustin Hibbits 	if_setcapenable(ifp, if_getcapabilities(ifp));
642ca406a44SPyun YongHyeon 	/*
643ca406a44SPyun YongHyeon 	 * Even though controllers supported by ale(3) have Rx checksum
644ca406a44SPyun YongHyeon 	 * offload bug the workaround for fragmented frames seemed to
645ca406a44SPyun YongHyeon 	 * work so far. However it seems Rx checksum offload does not
646ca406a44SPyun YongHyeon 	 * work under certain conditions. So disable Rx checksum offload
647ca406a44SPyun YongHyeon 	 * until I find more clue about it but allow users to override it.
648ca406a44SPyun YongHyeon 	 */
64952436412SJustin Hibbits 	if_setcapenablebit(ifp, 0, IFCAP_RXCSUM);
6503c6e15bcSPyun YongHyeon 
6513c6e15bcSPyun YongHyeon 	/* Tell the upper layer(s) we support long frames. */
65252436412SJustin Hibbits 	if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
6533c6e15bcSPyun YongHyeon 
6543c6e15bcSPyun YongHyeon 	/* Create local taskq. */
6553c6e15bcSPyun YongHyeon 	sc->ale_tq = taskqueue_create_fast("ale_taskq", M_WAITOK,
6563c6e15bcSPyun YongHyeon 	    taskqueue_thread_enqueue, &sc->ale_tq);
6573c6e15bcSPyun YongHyeon 	taskqueue_start_threads(&sc->ale_tq, 1, PI_NET, "%s taskq",
6583c6e15bcSPyun YongHyeon 	    device_get_nameunit(sc->ale_dev));
6593c6e15bcSPyun YongHyeon 
6603c6e15bcSPyun YongHyeon 	if ((sc->ale_flags & ALE_FLAG_MSIX) != 0)
6613c6e15bcSPyun YongHyeon 		msic = ALE_MSIX_MESSAGES;
6623c6e15bcSPyun YongHyeon 	else if ((sc->ale_flags & ALE_FLAG_MSI) != 0)
6633c6e15bcSPyun YongHyeon 		msic = ALE_MSI_MESSAGES;
6643c6e15bcSPyun YongHyeon 	else
6653c6e15bcSPyun YongHyeon 		msic = 1;
6663c6e15bcSPyun YongHyeon 	for (i = 0; i < msic; i++) {
6673c6e15bcSPyun YongHyeon 		error = bus_setup_intr(dev, sc->ale_irq[i],
6683c6e15bcSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, ale_intr, NULL, sc,
6693c6e15bcSPyun YongHyeon 		    &sc->ale_intrhand[i]);
6703c6e15bcSPyun YongHyeon 		if (error != 0)
6713c6e15bcSPyun YongHyeon 			break;
6723c6e15bcSPyun YongHyeon 	}
6733c6e15bcSPyun YongHyeon 	if (error != 0) {
6743c6e15bcSPyun YongHyeon 		device_printf(dev, "could not set up interrupt handler.\n");
6753c6e15bcSPyun YongHyeon 		taskqueue_free(sc->ale_tq);
6763c6e15bcSPyun YongHyeon 		sc->ale_tq = NULL;
6773c6e15bcSPyun YongHyeon 		ether_ifdetach(ifp);
6783c6e15bcSPyun YongHyeon 		goto fail;
6793c6e15bcSPyun YongHyeon 	}
6803c6e15bcSPyun YongHyeon 
6813c6e15bcSPyun YongHyeon fail:
6823c6e15bcSPyun YongHyeon 	if (error != 0)
6833c6e15bcSPyun YongHyeon 		ale_detach(dev);
6843c6e15bcSPyun YongHyeon 
6853c6e15bcSPyun YongHyeon 	return (error);
6863c6e15bcSPyun YongHyeon }
6873c6e15bcSPyun YongHyeon 
6883c6e15bcSPyun YongHyeon static int
ale_detach(device_t dev)6893c6e15bcSPyun YongHyeon ale_detach(device_t dev)
6903c6e15bcSPyun YongHyeon {
6913c6e15bcSPyun YongHyeon 	struct ale_softc *sc;
69252436412SJustin Hibbits 	if_t ifp;
6933c6e15bcSPyun YongHyeon 	int i, msic;
6943c6e15bcSPyun YongHyeon 
6953c6e15bcSPyun YongHyeon 	sc = device_get_softc(dev);
6963c6e15bcSPyun YongHyeon 
6973c6e15bcSPyun YongHyeon 	ifp = sc->ale_ifp;
6983c6e15bcSPyun YongHyeon 	if (device_is_attached(dev)) {
699287e0d97SJohn Baldwin 		ether_ifdetach(ifp);
7003c6e15bcSPyun YongHyeon 		ALE_LOCK(sc);
7013c6e15bcSPyun YongHyeon 		ale_stop(sc);
7023c6e15bcSPyun YongHyeon 		ALE_UNLOCK(sc);
7033c6e15bcSPyun YongHyeon 		callout_drain(&sc->ale_tick_ch);
7043c6e15bcSPyun YongHyeon 		taskqueue_drain(sc->ale_tq, &sc->ale_int_task);
7053c6e15bcSPyun YongHyeon 	}
7063c6e15bcSPyun YongHyeon 
7073c6e15bcSPyun YongHyeon 	if (sc->ale_tq != NULL) {
7083c6e15bcSPyun YongHyeon 		taskqueue_drain(sc->ale_tq, &sc->ale_int_task);
7093c6e15bcSPyun YongHyeon 		taskqueue_free(sc->ale_tq);
7103c6e15bcSPyun YongHyeon 		sc->ale_tq = NULL;
7113c6e15bcSPyun YongHyeon 	}
7123c6e15bcSPyun YongHyeon 
7133c6e15bcSPyun YongHyeon 	bus_generic_detach(dev);
7143c6e15bcSPyun YongHyeon 	ale_dma_free(sc);
7153c6e15bcSPyun YongHyeon 
7163c6e15bcSPyun YongHyeon 	if (ifp != NULL) {
7173c6e15bcSPyun YongHyeon 		if_free(ifp);
7183c6e15bcSPyun YongHyeon 		sc->ale_ifp = NULL;
7193c6e15bcSPyun YongHyeon 	}
7203c6e15bcSPyun YongHyeon 
7213c6e15bcSPyun YongHyeon 	if ((sc->ale_flags & ALE_FLAG_MSIX) != 0)
7223c6e15bcSPyun YongHyeon 		msic = ALE_MSIX_MESSAGES;
7233c6e15bcSPyun YongHyeon 	else if ((sc->ale_flags & ALE_FLAG_MSI) != 0)
7243c6e15bcSPyun YongHyeon 		msic = ALE_MSI_MESSAGES;
7253c6e15bcSPyun YongHyeon 	else
7263c6e15bcSPyun YongHyeon 		msic = 1;
7273c6e15bcSPyun YongHyeon 	for (i = 0; i < msic; i++) {
7283c6e15bcSPyun YongHyeon 		if (sc->ale_intrhand[i] != NULL) {
7293c6e15bcSPyun YongHyeon 			bus_teardown_intr(dev, sc->ale_irq[i],
7303c6e15bcSPyun YongHyeon 			    sc->ale_intrhand[i]);
7313c6e15bcSPyun YongHyeon 			sc->ale_intrhand[i] = NULL;
7323c6e15bcSPyun YongHyeon 		}
7333c6e15bcSPyun YongHyeon 	}
7343c6e15bcSPyun YongHyeon 
7353c6e15bcSPyun YongHyeon 	bus_release_resources(dev, sc->ale_irq_spec, sc->ale_irq);
7363c6e15bcSPyun YongHyeon 	if ((sc->ale_flags & (ALE_FLAG_MSI | ALE_FLAG_MSIX)) != 0)
7373c6e15bcSPyun YongHyeon 		pci_release_msi(dev);
7383c6e15bcSPyun YongHyeon 	bus_release_resources(dev, sc->ale_res_spec, sc->ale_res);
7393c6e15bcSPyun YongHyeon 	mtx_destroy(&sc->ale_mtx);
7403c6e15bcSPyun YongHyeon 
7413c6e15bcSPyun YongHyeon 	return (0);
7423c6e15bcSPyun YongHyeon }
7433c6e15bcSPyun YongHyeon 
7443c6e15bcSPyun YongHyeon #define	ALE_SYSCTL_STAT_ADD32(c, h, n, p, d)	\
7453c6e15bcSPyun YongHyeon 	    SYSCTL_ADD_UINT(c, h, OID_AUTO, n, CTLFLAG_RD, p, 0, d)
7463c6e15bcSPyun YongHyeon 
7476dc7dc9aSMatthew D Fleming #define	ALE_SYSCTL_STAT_ADD64(c, h, n, p, d)	\
7486dc7dc9aSMatthew D Fleming 	    SYSCTL_ADD_UQUAD(c, h, OID_AUTO, n, CTLFLAG_RD, p, d)
7493c6e15bcSPyun YongHyeon 
7503c6e15bcSPyun YongHyeon static void
ale_sysctl_node(struct ale_softc * sc)7513c6e15bcSPyun YongHyeon ale_sysctl_node(struct ale_softc *sc)
7523c6e15bcSPyun YongHyeon {
7533c6e15bcSPyun YongHyeon 	struct sysctl_ctx_list *ctx;
7543c6e15bcSPyun YongHyeon 	struct sysctl_oid_list *child, *parent;
7553c6e15bcSPyun YongHyeon 	struct sysctl_oid *tree;
7563c6e15bcSPyun YongHyeon 	struct ale_hw_stats *stats;
7573c6e15bcSPyun YongHyeon 	int error;
7583c6e15bcSPyun YongHyeon 
7593c6e15bcSPyun YongHyeon 	stats = &sc->ale_stats;
7603c6e15bcSPyun YongHyeon 	ctx = device_get_sysctl_ctx(sc->ale_dev);
7613c6e15bcSPyun YongHyeon 	child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ale_dev));
7623c6e15bcSPyun YongHyeon 
7633c6e15bcSPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "int_rx_mod",
7647029da5cSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, &sc->ale_int_rx_mod,
7657029da5cSPawel Biernacki 	    0, sysctl_hw_ale_int_mod, "I", "ale Rx interrupt moderation");
7663c6e15bcSPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "int_tx_mod",
7677029da5cSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, &sc->ale_int_tx_mod,
7687029da5cSPawel Biernacki 	    0, sysctl_hw_ale_int_mod, "I", "ale Tx interrupt moderation");
7693c6e15bcSPyun YongHyeon 	/* Pull in device tunables. */
7703c6e15bcSPyun YongHyeon 	sc->ale_int_rx_mod = ALE_IM_RX_TIMER_DEFAULT;
7713c6e15bcSPyun YongHyeon 	error = resource_int_value(device_get_name(sc->ale_dev),
7723c6e15bcSPyun YongHyeon 	    device_get_unit(sc->ale_dev), "int_rx_mod", &sc->ale_int_rx_mod);
7733c6e15bcSPyun YongHyeon 	if (error == 0) {
7743c6e15bcSPyun YongHyeon 		if (sc->ale_int_rx_mod < ALE_IM_TIMER_MIN ||
7753c6e15bcSPyun YongHyeon 		    sc->ale_int_rx_mod > ALE_IM_TIMER_MAX) {
7763c6e15bcSPyun YongHyeon 			device_printf(sc->ale_dev, "int_rx_mod value out of "
7773c6e15bcSPyun YongHyeon 			    "range; using default: %d\n",
7783c6e15bcSPyun YongHyeon 			    ALE_IM_RX_TIMER_DEFAULT);
7793c6e15bcSPyun YongHyeon 			sc->ale_int_rx_mod = ALE_IM_RX_TIMER_DEFAULT;
7803c6e15bcSPyun YongHyeon 		}
7813c6e15bcSPyun YongHyeon 	}
7823c6e15bcSPyun YongHyeon 	sc->ale_int_tx_mod = ALE_IM_TX_TIMER_DEFAULT;
7833c6e15bcSPyun YongHyeon 	error = resource_int_value(device_get_name(sc->ale_dev),
7843c6e15bcSPyun YongHyeon 	    device_get_unit(sc->ale_dev), "int_tx_mod", &sc->ale_int_tx_mod);
7853c6e15bcSPyun YongHyeon 	if (error == 0) {
7863c6e15bcSPyun YongHyeon 		if (sc->ale_int_tx_mod < ALE_IM_TIMER_MIN ||
7873c6e15bcSPyun YongHyeon 		    sc->ale_int_tx_mod > ALE_IM_TIMER_MAX) {
7883c6e15bcSPyun YongHyeon 			device_printf(sc->ale_dev, "int_tx_mod value out of "
7893c6e15bcSPyun YongHyeon 			    "range; using default: %d\n",
7903c6e15bcSPyun YongHyeon 			    ALE_IM_TX_TIMER_DEFAULT);
7913c6e15bcSPyun YongHyeon 			sc->ale_int_tx_mod = ALE_IM_TX_TIMER_DEFAULT;
7923c6e15bcSPyun YongHyeon 		}
7933c6e15bcSPyun YongHyeon 	}
7943c6e15bcSPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "process_limit",
7957029da5cSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
7967029da5cSPawel Biernacki 	    &sc->ale_process_limit, 0, sysctl_hw_ale_proc_limit, "I",
7973c6e15bcSPyun YongHyeon 	    "max number of Rx events to process");
7983c6e15bcSPyun YongHyeon 	/* Pull in device tunables. */
7993c6e15bcSPyun YongHyeon 	sc->ale_process_limit = ALE_PROC_DEFAULT;
8003c6e15bcSPyun YongHyeon 	error = resource_int_value(device_get_name(sc->ale_dev),
8013c6e15bcSPyun YongHyeon 	    device_get_unit(sc->ale_dev), "process_limit",
8023c6e15bcSPyun YongHyeon 	    &sc->ale_process_limit);
8033c6e15bcSPyun YongHyeon 	if (error == 0) {
8043c6e15bcSPyun YongHyeon 		if (sc->ale_process_limit < ALE_PROC_MIN ||
8053c6e15bcSPyun YongHyeon 		    sc->ale_process_limit > ALE_PROC_MAX) {
8063c6e15bcSPyun YongHyeon 			device_printf(sc->ale_dev,
8073c6e15bcSPyun YongHyeon 			    "process_limit value out of range; "
8083c6e15bcSPyun YongHyeon 			    "using default: %d\n", ALE_PROC_DEFAULT);
8093c6e15bcSPyun YongHyeon 			sc->ale_process_limit = ALE_PROC_DEFAULT;
8103c6e15bcSPyun YongHyeon 		}
8113c6e15bcSPyun YongHyeon 	}
8123c6e15bcSPyun YongHyeon 
8133c6e15bcSPyun YongHyeon 	/* Misc statistics. */
8143c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "reset_brk_seq",
8153c6e15bcSPyun YongHyeon 	    &stats->reset_brk_seq,
8163c6e15bcSPyun YongHyeon 	    "Controller resets due to broken Rx sequnce number");
8173c6e15bcSPyun YongHyeon 
8187029da5cSPawel Biernacki 	tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "stats",
8197029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "ATE statistics");
8203c6e15bcSPyun YongHyeon 	parent = SYSCTL_CHILDREN(tree);
8213c6e15bcSPyun YongHyeon 
8223c6e15bcSPyun YongHyeon 	/* Rx statistics. */
8237029da5cSPawel Biernacki 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "rx",
8247029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Rx MAC statistics");
8253c6e15bcSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
8263c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "good_frames",
8273c6e15bcSPyun YongHyeon 	    &stats->rx_frames, "Good frames");
8283c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "good_bcast_frames",
8293c6e15bcSPyun YongHyeon 	    &stats->rx_bcast_frames, "Good broadcast frames");
8303c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "good_mcast_frames",
8313c6e15bcSPyun YongHyeon 	    &stats->rx_mcast_frames, "Good multicast frames");
8323c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "pause_frames",
8333c6e15bcSPyun YongHyeon 	    &stats->rx_pause_frames, "Pause control frames");
8343c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "control_frames",
8353c6e15bcSPyun YongHyeon 	    &stats->rx_control_frames, "Control frames");
8363c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "crc_errs",
8373c6e15bcSPyun YongHyeon 	    &stats->rx_crcerrs, "CRC errors");
8383c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "len_errs",
8393c6e15bcSPyun YongHyeon 	    &stats->rx_lenerrs, "Frames with length mismatched");
8403c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD64(ctx, child, "good_octets",
8413c6e15bcSPyun YongHyeon 	    &stats->rx_bytes, "Good octets");
8423c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD64(ctx, child, "good_bcast_octets",
8433c6e15bcSPyun YongHyeon 	    &stats->rx_bcast_bytes, "Good broadcast octets");
8443c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD64(ctx, child, "good_mcast_octets",
8453c6e15bcSPyun YongHyeon 	    &stats->rx_mcast_bytes, "Good multicast octets");
8463c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "runts",
8473c6e15bcSPyun YongHyeon 	    &stats->rx_runts, "Too short frames");
8483c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "fragments",
8493c6e15bcSPyun YongHyeon 	    &stats->rx_fragments, "Fragmented frames");
8503c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "frames_64",
8513c6e15bcSPyun YongHyeon 	    &stats->rx_pkts_64, "64 bytes frames");
8523c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "frames_65_127",
8533c6e15bcSPyun YongHyeon 	    &stats->rx_pkts_65_127, "65 to 127 bytes frames");
8543c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "frames_128_255",
8553c6e15bcSPyun YongHyeon 	    &stats->rx_pkts_128_255, "128 to 255 bytes frames");
8563c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "frames_256_511",
8573c6e15bcSPyun YongHyeon 	    &stats->rx_pkts_256_511, "256 to 511 bytes frames");
8583c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "frames_512_1023",
8593c6e15bcSPyun YongHyeon 	    &stats->rx_pkts_512_1023, "512 to 1023 bytes frames");
8603c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "frames_1024_1518",
8613c6e15bcSPyun YongHyeon 	    &stats->rx_pkts_1024_1518, "1024 to 1518 bytes frames");
8623c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "frames_1519_max",
8633c6e15bcSPyun YongHyeon 	    &stats->rx_pkts_1519_max, "1519 to max frames");
8643c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "trunc_errs",
8653c6e15bcSPyun YongHyeon 	    &stats->rx_pkts_truncated, "Truncated frames due to MTU size");
8663c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "fifo_oflows",
8673c6e15bcSPyun YongHyeon 	    &stats->rx_fifo_oflows, "FIFO overflows");
8683c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "rrs_errs",
8693c6e15bcSPyun YongHyeon 	    &stats->rx_rrs_errs, "Return status write-back errors");
8703c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "align_errs",
8713c6e15bcSPyun YongHyeon 	    &stats->rx_alignerrs, "Alignment errors");
8723c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "filtered",
8733c6e15bcSPyun YongHyeon 	    &stats->rx_pkts_filtered,
8743c6e15bcSPyun YongHyeon 	    "Frames dropped due to address filtering");
8753c6e15bcSPyun YongHyeon 
8763c6e15bcSPyun YongHyeon 	/* Tx statistics. */
8777029da5cSPawel Biernacki 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "tx",
8787029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Tx MAC statistics");
8793c6e15bcSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
8803c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "good_frames",
8813c6e15bcSPyun YongHyeon 	    &stats->tx_frames, "Good frames");
8823c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "good_bcast_frames",
8833c6e15bcSPyun YongHyeon 	    &stats->tx_bcast_frames, "Good broadcast frames");
8843c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "good_mcast_frames",
8853c6e15bcSPyun YongHyeon 	    &stats->tx_mcast_frames, "Good multicast frames");
8863c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "pause_frames",
8873c6e15bcSPyun YongHyeon 	    &stats->tx_pause_frames, "Pause control frames");
8883c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "control_frames",
8893c6e15bcSPyun YongHyeon 	    &stats->tx_control_frames, "Control frames");
8903c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "excess_defers",
8913c6e15bcSPyun YongHyeon 	    &stats->tx_excess_defer, "Frames with excessive derferrals");
8923c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "defers",
8933c6e15bcSPyun YongHyeon 	    &stats->tx_excess_defer, "Frames with derferrals");
8943c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD64(ctx, child, "good_octets",
8953c6e15bcSPyun YongHyeon 	    &stats->tx_bytes, "Good octets");
8963c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD64(ctx, child, "good_bcast_octets",
8973c6e15bcSPyun YongHyeon 	    &stats->tx_bcast_bytes, "Good broadcast octets");
8983c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD64(ctx, child, "good_mcast_octets",
8993c6e15bcSPyun YongHyeon 	    &stats->tx_mcast_bytes, "Good multicast octets");
9003c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "frames_64",
9013c6e15bcSPyun YongHyeon 	    &stats->tx_pkts_64, "64 bytes frames");
9023c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "frames_65_127",
9033c6e15bcSPyun YongHyeon 	    &stats->tx_pkts_65_127, "65 to 127 bytes frames");
9043c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "frames_128_255",
9053c6e15bcSPyun YongHyeon 	    &stats->tx_pkts_128_255, "128 to 255 bytes frames");
9063c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "frames_256_511",
9073c6e15bcSPyun YongHyeon 	    &stats->tx_pkts_256_511, "256 to 511 bytes frames");
9083c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "frames_512_1023",
9093c6e15bcSPyun YongHyeon 	    &stats->tx_pkts_512_1023, "512 to 1023 bytes frames");
9103c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "frames_1024_1518",
9113c6e15bcSPyun YongHyeon 	    &stats->tx_pkts_1024_1518, "1024 to 1518 bytes frames");
9123c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "frames_1519_max",
9133c6e15bcSPyun YongHyeon 	    &stats->tx_pkts_1519_max, "1519 to max frames");
9143c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "single_colls",
9153c6e15bcSPyun YongHyeon 	    &stats->tx_single_colls, "Single collisions");
9163c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "multi_colls",
9173c6e15bcSPyun YongHyeon 	    &stats->tx_multi_colls, "Multiple collisions");
9183c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "late_colls",
9193c6e15bcSPyun YongHyeon 	    &stats->tx_late_colls, "Late collisions");
9203c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "excess_colls",
9213c6e15bcSPyun YongHyeon 	    &stats->tx_excess_colls, "Excessive collisions");
9223c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "underruns",
9233c6e15bcSPyun YongHyeon 	    &stats->tx_underrun, "FIFO underruns");
9243c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "desc_underruns",
9253c6e15bcSPyun YongHyeon 	    &stats->tx_desc_underrun, "Descriptor write-back errors");
9263c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "len_errs",
9273c6e15bcSPyun YongHyeon 	    &stats->tx_lenerrs, "Frames with length mismatched");
9283c6e15bcSPyun YongHyeon 	ALE_SYSCTL_STAT_ADD32(ctx, child, "trunc_errs",
9293c6e15bcSPyun YongHyeon 	    &stats->tx_pkts_truncated, "Truncated frames due to MTU size");
9303c6e15bcSPyun YongHyeon }
9313c6e15bcSPyun YongHyeon 
9323c6e15bcSPyun YongHyeon #undef ALE_SYSCTL_STAT_ADD32
9333c6e15bcSPyun YongHyeon #undef ALE_SYSCTL_STAT_ADD64
9343c6e15bcSPyun YongHyeon 
9353c6e15bcSPyun YongHyeon struct ale_dmamap_arg {
9363c6e15bcSPyun YongHyeon 	bus_addr_t	ale_busaddr;
9373c6e15bcSPyun YongHyeon };
9383c6e15bcSPyun YongHyeon 
9393c6e15bcSPyun YongHyeon static void
ale_dmamap_cb(void * arg,bus_dma_segment_t * segs,int nsegs,int error)9403c6e15bcSPyun YongHyeon ale_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
9413c6e15bcSPyun YongHyeon {
9423c6e15bcSPyun YongHyeon 	struct ale_dmamap_arg *ctx;
9433c6e15bcSPyun YongHyeon 
9443c6e15bcSPyun YongHyeon 	if (error != 0)
9453c6e15bcSPyun YongHyeon 		return;
9463c6e15bcSPyun YongHyeon 
9473c6e15bcSPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
9483c6e15bcSPyun YongHyeon 
9493c6e15bcSPyun YongHyeon 	ctx = (struct ale_dmamap_arg *)arg;
9503c6e15bcSPyun YongHyeon 	ctx->ale_busaddr = segs[0].ds_addr;
9513c6e15bcSPyun YongHyeon }
9523c6e15bcSPyun YongHyeon 
9533c6e15bcSPyun YongHyeon /*
9543c6e15bcSPyun YongHyeon  * Tx descriptors/RXF0/CMB DMA blocks share ALE_DESC_ADDR_HI register
9553c6e15bcSPyun YongHyeon  * which specifies high address region of DMA blocks. Therefore these
9563c6e15bcSPyun YongHyeon  * blocks should have the same high address of given 4GB address
9573c6e15bcSPyun YongHyeon  * space(i.e. crossing 4GB boundary is not allowed).
9583c6e15bcSPyun YongHyeon  */
9593c6e15bcSPyun YongHyeon static int
ale_check_boundary(struct ale_softc * sc)9603c6e15bcSPyun YongHyeon ale_check_boundary(struct ale_softc *sc)
9613c6e15bcSPyun YongHyeon {
9623c6e15bcSPyun YongHyeon 	bus_addr_t rx_cmb_end[ALE_RX_PAGES], tx_cmb_end;
9633c6e15bcSPyun YongHyeon 	bus_addr_t rx_page_end[ALE_RX_PAGES], tx_ring_end;
9643c6e15bcSPyun YongHyeon 
9653c6e15bcSPyun YongHyeon 	rx_page_end[0] = sc->ale_cdata.ale_rx_page[0].page_paddr +
9663c6e15bcSPyun YongHyeon 	    sc->ale_pagesize;
9673c6e15bcSPyun YongHyeon 	rx_page_end[1] = sc->ale_cdata.ale_rx_page[1].page_paddr +
9683c6e15bcSPyun YongHyeon 	    sc->ale_pagesize;
9693c6e15bcSPyun YongHyeon 	tx_ring_end = sc->ale_cdata.ale_tx_ring_paddr + ALE_TX_RING_SZ;
9703c6e15bcSPyun YongHyeon 	tx_cmb_end = sc->ale_cdata.ale_tx_cmb_paddr + ALE_TX_CMB_SZ;
9713c6e15bcSPyun YongHyeon 	rx_cmb_end[0] = sc->ale_cdata.ale_rx_page[0].cmb_paddr + ALE_RX_CMB_SZ;
9723c6e15bcSPyun YongHyeon 	rx_cmb_end[1] = sc->ale_cdata.ale_rx_page[1].cmb_paddr + ALE_RX_CMB_SZ;
9733c6e15bcSPyun YongHyeon 
9743c6e15bcSPyun YongHyeon 	if ((ALE_ADDR_HI(tx_ring_end) !=
9753c6e15bcSPyun YongHyeon 	    ALE_ADDR_HI(sc->ale_cdata.ale_tx_ring_paddr)) ||
9763c6e15bcSPyun YongHyeon 	    (ALE_ADDR_HI(rx_page_end[0]) !=
9773c6e15bcSPyun YongHyeon 	    ALE_ADDR_HI(sc->ale_cdata.ale_rx_page[0].page_paddr)) ||
9783c6e15bcSPyun YongHyeon 	    (ALE_ADDR_HI(rx_page_end[1]) !=
9793c6e15bcSPyun YongHyeon 	    ALE_ADDR_HI(sc->ale_cdata.ale_rx_page[1].page_paddr)) ||
9803c6e15bcSPyun YongHyeon 	    (ALE_ADDR_HI(tx_cmb_end) !=
9813c6e15bcSPyun YongHyeon 	    ALE_ADDR_HI(sc->ale_cdata.ale_tx_cmb_paddr)) ||
9823c6e15bcSPyun YongHyeon 	    (ALE_ADDR_HI(rx_cmb_end[0]) !=
9833c6e15bcSPyun YongHyeon 	    ALE_ADDR_HI(sc->ale_cdata.ale_rx_page[0].cmb_paddr)) ||
9843c6e15bcSPyun YongHyeon 	    (ALE_ADDR_HI(rx_cmb_end[1]) !=
9853c6e15bcSPyun YongHyeon 	    ALE_ADDR_HI(sc->ale_cdata.ale_rx_page[1].cmb_paddr)))
9863c6e15bcSPyun YongHyeon 		return (EFBIG);
9873c6e15bcSPyun YongHyeon 
9883c6e15bcSPyun YongHyeon 	if ((ALE_ADDR_HI(tx_ring_end) != ALE_ADDR_HI(rx_page_end[0])) ||
9893c6e15bcSPyun YongHyeon 	    (ALE_ADDR_HI(tx_ring_end) != ALE_ADDR_HI(rx_page_end[1])) ||
9903c6e15bcSPyun YongHyeon 	    (ALE_ADDR_HI(tx_ring_end) != ALE_ADDR_HI(rx_cmb_end[0])) ||
9913c6e15bcSPyun YongHyeon 	    (ALE_ADDR_HI(tx_ring_end) != ALE_ADDR_HI(rx_cmb_end[1])) ||
9923c6e15bcSPyun YongHyeon 	    (ALE_ADDR_HI(tx_ring_end) != ALE_ADDR_HI(tx_cmb_end)))
9933c6e15bcSPyun YongHyeon 		return (EFBIG);
9943c6e15bcSPyun YongHyeon 
9953c6e15bcSPyun YongHyeon 	return (0);
9963c6e15bcSPyun YongHyeon }
9973c6e15bcSPyun YongHyeon 
9983c6e15bcSPyun YongHyeon static int
ale_dma_alloc(struct ale_softc * sc)9993c6e15bcSPyun YongHyeon ale_dma_alloc(struct ale_softc *sc)
10003c6e15bcSPyun YongHyeon {
10013c6e15bcSPyun YongHyeon 	struct ale_txdesc *txd;
10023c6e15bcSPyun YongHyeon 	bus_addr_t lowaddr;
10033c6e15bcSPyun YongHyeon 	struct ale_dmamap_arg ctx;
10043c6e15bcSPyun YongHyeon 	int error, guard_size, i;
10053c6e15bcSPyun YongHyeon 
10063c6e15bcSPyun YongHyeon 	if ((sc->ale_flags & ALE_FLAG_JUMBO) != 0)
10073c6e15bcSPyun YongHyeon 		guard_size = ALE_JUMBO_FRAMELEN;
10083c6e15bcSPyun YongHyeon 	else
10093c6e15bcSPyun YongHyeon 		guard_size = ALE_MAX_FRAMELEN;
10103c6e15bcSPyun YongHyeon 	sc->ale_pagesize = roundup(guard_size + ALE_RX_PAGE_SZ,
10113c6e15bcSPyun YongHyeon 	    ALE_RX_PAGE_ALIGN);
10123c6e15bcSPyun YongHyeon 	lowaddr = BUS_SPACE_MAXADDR;
10133c6e15bcSPyun YongHyeon again:
10143c6e15bcSPyun YongHyeon 	/* Create parent DMA tag. */
10153c6e15bcSPyun YongHyeon 	error = bus_dma_tag_create(
10163c6e15bcSPyun YongHyeon 	    bus_get_dma_tag(sc->ale_dev), /* parent */
10173c6e15bcSPyun YongHyeon 	    1, 0,			/* alignment, boundary */
10183c6e15bcSPyun YongHyeon 	    lowaddr,			/* lowaddr */
10193c6e15bcSPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
10203c6e15bcSPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
10213c6e15bcSPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsize */
10223c6e15bcSPyun YongHyeon 	    0,				/* nsegments */
10233c6e15bcSPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
10243c6e15bcSPyun YongHyeon 	    0,				/* flags */
10253c6e15bcSPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
10263c6e15bcSPyun YongHyeon 	    &sc->ale_cdata.ale_parent_tag);
10273c6e15bcSPyun YongHyeon 	if (error != 0) {
10283c6e15bcSPyun YongHyeon 		device_printf(sc->ale_dev,
10293c6e15bcSPyun YongHyeon 		    "could not create parent DMA tag.\n");
10303c6e15bcSPyun YongHyeon 		goto fail;
10313c6e15bcSPyun YongHyeon 	}
10323c6e15bcSPyun YongHyeon 
10333c6e15bcSPyun YongHyeon 	/* Create DMA tag for Tx descriptor ring. */
10343c6e15bcSPyun YongHyeon 	error = bus_dma_tag_create(
10353c6e15bcSPyun YongHyeon 	    sc->ale_cdata.ale_parent_tag, /* parent */
10363c6e15bcSPyun YongHyeon 	    ALE_TX_RING_ALIGN, 0,	/* alignment, boundary */
10373c6e15bcSPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* lowaddr */
10383c6e15bcSPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
10393c6e15bcSPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
10403c6e15bcSPyun YongHyeon 	    ALE_TX_RING_SZ,		/* maxsize */
10413c6e15bcSPyun YongHyeon 	    1,				/* nsegments */
10423c6e15bcSPyun YongHyeon 	    ALE_TX_RING_SZ,		/* maxsegsize */
10433c6e15bcSPyun YongHyeon 	    0,				/* flags */
10443c6e15bcSPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
10453c6e15bcSPyun YongHyeon 	    &sc->ale_cdata.ale_tx_ring_tag);
10463c6e15bcSPyun YongHyeon 	if (error != 0) {
10473c6e15bcSPyun YongHyeon 		device_printf(sc->ale_dev,
10483c6e15bcSPyun YongHyeon 		    "could not create Tx ring DMA tag.\n");
10493c6e15bcSPyun YongHyeon 		goto fail;
10503c6e15bcSPyun YongHyeon 	}
10513c6e15bcSPyun YongHyeon 
10523c6e15bcSPyun YongHyeon 	/* Create DMA tag for Rx pages. */
10533c6e15bcSPyun YongHyeon 	for (i = 0; i < ALE_RX_PAGES; i++) {
10543c6e15bcSPyun YongHyeon 		error = bus_dma_tag_create(
10553c6e15bcSPyun YongHyeon 		    sc->ale_cdata.ale_parent_tag, /* parent */
10563c6e15bcSPyun YongHyeon 		    ALE_RX_PAGE_ALIGN, 0,	/* alignment, boundary */
10573c6e15bcSPyun YongHyeon 		    BUS_SPACE_MAXADDR,		/* lowaddr */
10583c6e15bcSPyun YongHyeon 		    BUS_SPACE_MAXADDR,		/* highaddr */
10593c6e15bcSPyun YongHyeon 		    NULL, NULL,			/* filter, filterarg */
10603c6e15bcSPyun YongHyeon 		    sc->ale_pagesize,		/* maxsize */
10613c6e15bcSPyun YongHyeon 		    1,				/* nsegments */
10623c6e15bcSPyun YongHyeon 		    sc->ale_pagesize,		/* maxsegsize */
10633c6e15bcSPyun YongHyeon 		    0,				/* flags */
10643c6e15bcSPyun YongHyeon 		    NULL, NULL,			/* lockfunc, lockarg */
10653c6e15bcSPyun YongHyeon 		    &sc->ale_cdata.ale_rx_page[i].page_tag);
10663c6e15bcSPyun YongHyeon 		if (error != 0) {
10673c6e15bcSPyun YongHyeon 			device_printf(sc->ale_dev,
10683c6e15bcSPyun YongHyeon 			    "could not create Rx page %d DMA tag.\n", i);
10693c6e15bcSPyun YongHyeon 			goto fail;
10703c6e15bcSPyun YongHyeon 		}
10713c6e15bcSPyun YongHyeon 	}
10723c6e15bcSPyun YongHyeon 
10733c6e15bcSPyun YongHyeon 	/* Create DMA tag for Tx coalescing message block. */
10743c6e15bcSPyun YongHyeon 	error = bus_dma_tag_create(
10753c6e15bcSPyun YongHyeon 	    sc->ale_cdata.ale_parent_tag, /* parent */
10763c6e15bcSPyun YongHyeon 	    ALE_CMB_ALIGN, 0,		/* alignment, boundary */
10773c6e15bcSPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* lowaddr */
10783c6e15bcSPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
10793c6e15bcSPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
10803c6e15bcSPyun YongHyeon 	    ALE_TX_CMB_SZ,		/* maxsize */
10813c6e15bcSPyun YongHyeon 	    1,				/* nsegments */
10823c6e15bcSPyun YongHyeon 	    ALE_TX_CMB_SZ,		/* maxsegsize */
10833c6e15bcSPyun YongHyeon 	    0,				/* flags */
10843c6e15bcSPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
10853c6e15bcSPyun YongHyeon 	    &sc->ale_cdata.ale_tx_cmb_tag);
10863c6e15bcSPyun YongHyeon 	if (error != 0) {
10873c6e15bcSPyun YongHyeon 		device_printf(sc->ale_dev,
10883c6e15bcSPyun YongHyeon 		    "could not create Tx CMB DMA tag.\n");
10893c6e15bcSPyun YongHyeon 		goto fail;
10903c6e15bcSPyun YongHyeon 	}
10913c6e15bcSPyun YongHyeon 
10923c6e15bcSPyun YongHyeon 	/* Create DMA tag for Rx coalescing message block. */
10933c6e15bcSPyun YongHyeon 	for (i = 0; i < ALE_RX_PAGES; i++) {
10943c6e15bcSPyun YongHyeon 		error = bus_dma_tag_create(
10953c6e15bcSPyun YongHyeon 		    sc->ale_cdata.ale_parent_tag, /* parent */
10963c6e15bcSPyun YongHyeon 		    ALE_CMB_ALIGN, 0,		/* alignment, boundary */
10973c6e15bcSPyun YongHyeon 		    BUS_SPACE_MAXADDR,		/* lowaddr */
10983c6e15bcSPyun YongHyeon 		    BUS_SPACE_MAXADDR,		/* highaddr */
10993c6e15bcSPyun YongHyeon 		    NULL, NULL,			/* filter, filterarg */
11003c6e15bcSPyun YongHyeon 		    ALE_RX_CMB_SZ,		/* maxsize */
11013c6e15bcSPyun YongHyeon 		    1,				/* nsegments */
11023c6e15bcSPyun YongHyeon 		    ALE_RX_CMB_SZ,		/* maxsegsize */
11033c6e15bcSPyun YongHyeon 		    0,				/* flags */
11043c6e15bcSPyun YongHyeon 		    NULL, NULL,			/* lockfunc, lockarg */
11053c6e15bcSPyun YongHyeon 		    &sc->ale_cdata.ale_rx_page[i].cmb_tag);
11063c6e15bcSPyun YongHyeon 		if (error != 0) {
11073c6e15bcSPyun YongHyeon 			device_printf(sc->ale_dev,
11083c6e15bcSPyun YongHyeon 			    "could not create Rx page %d CMB DMA tag.\n", i);
11093c6e15bcSPyun YongHyeon 			goto fail;
11103c6e15bcSPyun YongHyeon 		}
11113c6e15bcSPyun YongHyeon 	}
11123c6e15bcSPyun YongHyeon 
11133c6e15bcSPyun YongHyeon 	/* Allocate DMA'able memory and load the DMA map for Tx ring. */
11143c6e15bcSPyun YongHyeon 	error = bus_dmamem_alloc(sc->ale_cdata.ale_tx_ring_tag,
11153c6e15bcSPyun YongHyeon 	    (void **)&sc->ale_cdata.ale_tx_ring,
11163c6e15bcSPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
11173c6e15bcSPyun YongHyeon 	    &sc->ale_cdata.ale_tx_ring_map);
11183c6e15bcSPyun YongHyeon 	if (error != 0) {
11193c6e15bcSPyun YongHyeon 		device_printf(sc->ale_dev,
11203c6e15bcSPyun YongHyeon 		    "could not allocate DMA'able memory for Tx ring.\n");
11213c6e15bcSPyun YongHyeon 		goto fail;
11223c6e15bcSPyun YongHyeon 	}
11233c6e15bcSPyun YongHyeon 	ctx.ale_busaddr = 0;
11243c6e15bcSPyun YongHyeon 	error = bus_dmamap_load(sc->ale_cdata.ale_tx_ring_tag,
11253c6e15bcSPyun YongHyeon 	    sc->ale_cdata.ale_tx_ring_map, sc->ale_cdata.ale_tx_ring,
11263c6e15bcSPyun YongHyeon 	    ALE_TX_RING_SZ, ale_dmamap_cb, &ctx, 0);
11273c6e15bcSPyun YongHyeon 	if (error != 0 || ctx.ale_busaddr == 0) {
11283c6e15bcSPyun YongHyeon 		device_printf(sc->ale_dev,
11293c6e15bcSPyun YongHyeon 		    "could not load DMA'able memory for Tx ring.\n");
11303c6e15bcSPyun YongHyeon 		goto fail;
11313c6e15bcSPyun YongHyeon 	}
11323c6e15bcSPyun YongHyeon 	sc->ale_cdata.ale_tx_ring_paddr = ctx.ale_busaddr;
11333c6e15bcSPyun YongHyeon 
11343c6e15bcSPyun YongHyeon 	/* Rx pages. */
11353c6e15bcSPyun YongHyeon 	for (i = 0; i < ALE_RX_PAGES; i++) {
11363c6e15bcSPyun YongHyeon 		error = bus_dmamem_alloc(sc->ale_cdata.ale_rx_page[i].page_tag,
11373c6e15bcSPyun YongHyeon 		    (void **)&sc->ale_cdata.ale_rx_page[i].page_addr,
11383c6e15bcSPyun YongHyeon 		    BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
11393c6e15bcSPyun YongHyeon 		    &sc->ale_cdata.ale_rx_page[i].page_map);
11403c6e15bcSPyun YongHyeon 		if (error != 0) {
11413c6e15bcSPyun YongHyeon 			device_printf(sc->ale_dev,
11423c6e15bcSPyun YongHyeon 			    "could not allocate DMA'able memory for "
11433c6e15bcSPyun YongHyeon 			    "Rx page %d.\n", i);
11443c6e15bcSPyun YongHyeon 			goto fail;
11453c6e15bcSPyun YongHyeon 		}
11463c6e15bcSPyun YongHyeon 		ctx.ale_busaddr = 0;
11473c6e15bcSPyun YongHyeon 		error = bus_dmamap_load(sc->ale_cdata.ale_rx_page[i].page_tag,
11483c6e15bcSPyun YongHyeon 		    sc->ale_cdata.ale_rx_page[i].page_map,
11493c6e15bcSPyun YongHyeon 		    sc->ale_cdata.ale_rx_page[i].page_addr,
11503c6e15bcSPyun YongHyeon 		    sc->ale_pagesize, ale_dmamap_cb, &ctx, 0);
11513c6e15bcSPyun YongHyeon 		if (error != 0 || ctx.ale_busaddr == 0) {
11523c6e15bcSPyun YongHyeon 			device_printf(sc->ale_dev,
11533c6e15bcSPyun YongHyeon 			    "could not load DMA'able memory for "
11543c6e15bcSPyun YongHyeon 			    "Rx page %d.\n", i);
11553c6e15bcSPyun YongHyeon 			goto fail;
11563c6e15bcSPyun YongHyeon 		}
11573c6e15bcSPyun YongHyeon 		sc->ale_cdata.ale_rx_page[i].page_paddr = ctx.ale_busaddr;
11583c6e15bcSPyun YongHyeon 	}
11593c6e15bcSPyun YongHyeon 
11603c6e15bcSPyun YongHyeon 	/* Tx CMB. */
11613c6e15bcSPyun YongHyeon 	error = bus_dmamem_alloc(sc->ale_cdata.ale_tx_cmb_tag,
11623c6e15bcSPyun YongHyeon 	    (void **)&sc->ale_cdata.ale_tx_cmb,
11633c6e15bcSPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
11643c6e15bcSPyun YongHyeon 	    &sc->ale_cdata.ale_tx_cmb_map);
11653c6e15bcSPyun YongHyeon 	if (error != 0) {
11663c6e15bcSPyun YongHyeon 		device_printf(sc->ale_dev,
11673c6e15bcSPyun YongHyeon 		    "could not allocate DMA'able memory for Tx CMB.\n");
11683c6e15bcSPyun YongHyeon 		goto fail;
11693c6e15bcSPyun YongHyeon 	}
11703c6e15bcSPyun YongHyeon 	ctx.ale_busaddr = 0;
11713c6e15bcSPyun YongHyeon 	error = bus_dmamap_load(sc->ale_cdata.ale_tx_cmb_tag,
11723c6e15bcSPyun YongHyeon 	    sc->ale_cdata.ale_tx_cmb_map, sc->ale_cdata.ale_tx_cmb,
11733c6e15bcSPyun YongHyeon 	    ALE_TX_CMB_SZ, ale_dmamap_cb, &ctx, 0);
11743c6e15bcSPyun YongHyeon 	if (error != 0 || ctx.ale_busaddr == 0) {
11753c6e15bcSPyun YongHyeon 		device_printf(sc->ale_dev,
11763c6e15bcSPyun YongHyeon 		    "could not load DMA'able memory for Tx CMB.\n");
11773c6e15bcSPyun YongHyeon 		goto fail;
11783c6e15bcSPyun YongHyeon 	}
11793c6e15bcSPyun YongHyeon 	sc->ale_cdata.ale_tx_cmb_paddr = ctx.ale_busaddr;
11803c6e15bcSPyun YongHyeon 
11813c6e15bcSPyun YongHyeon 	/* Rx CMB. */
11823c6e15bcSPyun YongHyeon 	for (i = 0; i < ALE_RX_PAGES; i++) {
11833c6e15bcSPyun YongHyeon 		error = bus_dmamem_alloc(sc->ale_cdata.ale_rx_page[i].cmb_tag,
11843c6e15bcSPyun YongHyeon 		    (void **)&sc->ale_cdata.ale_rx_page[i].cmb_addr,
11853c6e15bcSPyun YongHyeon 		    BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
11863c6e15bcSPyun YongHyeon 		    &sc->ale_cdata.ale_rx_page[i].cmb_map);
11873c6e15bcSPyun YongHyeon 		if (error != 0) {
11883c6e15bcSPyun YongHyeon 			device_printf(sc->ale_dev, "could not allocate "
11893c6e15bcSPyun YongHyeon 			    "DMA'able memory for Rx page %d CMB.\n", i);
11903c6e15bcSPyun YongHyeon 			goto fail;
11913c6e15bcSPyun YongHyeon 		}
11923c6e15bcSPyun YongHyeon 		ctx.ale_busaddr = 0;
11933c6e15bcSPyun YongHyeon 		error = bus_dmamap_load(sc->ale_cdata.ale_rx_page[i].cmb_tag,
11943c6e15bcSPyun YongHyeon 		    sc->ale_cdata.ale_rx_page[i].cmb_map,
11953c6e15bcSPyun YongHyeon 		    sc->ale_cdata.ale_rx_page[i].cmb_addr,
11963c6e15bcSPyun YongHyeon 		    ALE_RX_CMB_SZ, ale_dmamap_cb, &ctx, 0);
11973c6e15bcSPyun YongHyeon 		if (error != 0 || ctx.ale_busaddr == 0) {
11983c6e15bcSPyun YongHyeon 			device_printf(sc->ale_dev, "could not load DMA'able "
11993c6e15bcSPyun YongHyeon 			    "memory for Rx page %d CMB.\n", i);
12003c6e15bcSPyun YongHyeon 			goto fail;
12013c6e15bcSPyun YongHyeon 		}
12023c6e15bcSPyun YongHyeon 		sc->ale_cdata.ale_rx_page[i].cmb_paddr = ctx.ale_busaddr;
12033c6e15bcSPyun YongHyeon 	}
12043c6e15bcSPyun YongHyeon 
12053c6e15bcSPyun YongHyeon 	/*
12063c6e15bcSPyun YongHyeon 	 * Tx descriptors/RXF0/CMB DMA blocks share the same
12073c6e15bcSPyun YongHyeon 	 * high address region of 64bit DMA address space.
12083c6e15bcSPyun YongHyeon 	 */
12093c6e15bcSPyun YongHyeon 	if (lowaddr != BUS_SPACE_MAXADDR_32BIT &&
12103c6e15bcSPyun YongHyeon 	    (error = ale_check_boundary(sc)) != 0) {
12113c6e15bcSPyun YongHyeon 		device_printf(sc->ale_dev, "4GB boundary crossed, "
12123c6e15bcSPyun YongHyeon 		    "switching to 32bit DMA addressing mode.\n");
12133c6e15bcSPyun YongHyeon 		ale_dma_free(sc);
12143c6e15bcSPyun YongHyeon 		/*
12153c6e15bcSPyun YongHyeon 		 * Limit max allowable DMA address space to 32bit
12163c6e15bcSPyun YongHyeon 		 * and try again.
12173c6e15bcSPyun YongHyeon 		 */
12183c6e15bcSPyun YongHyeon 		lowaddr = BUS_SPACE_MAXADDR_32BIT;
12193c6e15bcSPyun YongHyeon 		goto again;
12203c6e15bcSPyun YongHyeon 	}
12213c6e15bcSPyun YongHyeon 
12223c6e15bcSPyun YongHyeon 	/*
12233c6e15bcSPyun YongHyeon 	 * Create Tx buffer parent tag.
12243c6e15bcSPyun YongHyeon 	 * AR81xx allows 64bit DMA addressing of Tx buffers so it
12253c6e15bcSPyun YongHyeon 	 * needs separate parent DMA tag as parent DMA address space
12263c6e15bcSPyun YongHyeon 	 * could be restricted to be within 32bit address space by
12273c6e15bcSPyun YongHyeon 	 * 4GB boundary crossing.
12283c6e15bcSPyun YongHyeon 	 */
12293c6e15bcSPyun YongHyeon 	error = bus_dma_tag_create(
12303c6e15bcSPyun YongHyeon 	    bus_get_dma_tag(sc->ale_dev), /* parent */
12313c6e15bcSPyun YongHyeon 	    1, 0,			/* alignment, boundary */
12323c6e15bcSPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* lowaddr */
12333c6e15bcSPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
12343c6e15bcSPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
12353c6e15bcSPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsize */
12363c6e15bcSPyun YongHyeon 	    0,				/* nsegments */
12373c6e15bcSPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
12383c6e15bcSPyun YongHyeon 	    0,				/* flags */
12393c6e15bcSPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
12403c6e15bcSPyun YongHyeon 	    &sc->ale_cdata.ale_buffer_tag);
12413c6e15bcSPyun YongHyeon 	if (error != 0) {
12423c6e15bcSPyun YongHyeon 		device_printf(sc->ale_dev,
12433c6e15bcSPyun YongHyeon 		    "could not create parent buffer DMA tag.\n");
12443c6e15bcSPyun YongHyeon 		goto fail;
12453c6e15bcSPyun YongHyeon 	}
12463c6e15bcSPyun YongHyeon 
12473c6e15bcSPyun YongHyeon 	/* Create DMA tag for Tx buffers. */
12483c6e15bcSPyun YongHyeon 	error = bus_dma_tag_create(
12493c6e15bcSPyun YongHyeon 	    sc->ale_cdata.ale_buffer_tag, /* parent */
12503c6e15bcSPyun YongHyeon 	    1, 0,			/* alignment, boundary */
12513c6e15bcSPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* lowaddr */
12523c6e15bcSPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
12533c6e15bcSPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
12543c6e15bcSPyun YongHyeon 	    ALE_TSO_MAXSIZE,		/* maxsize */
12553c6e15bcSPyun YongHyeon 	    ALE_MAXTXSEGS,		/* nsegments */
12563c6e15bcSPyun YongHyeon 	    ALE_TSO_MAXSEGSIZE,		/* maxsegsize */
12573c6e15bcSPyun YongHyeon 	    0,				/* flags */
12583c6e15bcSPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
12593c6e15bcSPyun YongHyeon 	    &sc->ale_cdata.ale_tx_tag);
12603c6e15bcSPyun YongHyeon 	if (error != 0) {
12613c6e15bcSPyun YongHyeon 		device_printf(sc->ale_dev, "could not create Tx DMA tag.\n");
12623c6e15bcSPyun YongHyeon 		goto fail;
12633c6e15bcSPyun YongHyeon 	}
12643c6e15bcSPyun YongHyeon 
12653c6e15bcSPyun YongHyeon 	/* Create DMA maps for Tx buffers. */
12663c6e15bcSPyun YongHyeon 	for (i = 0; i < ALE_TX_RING_CNT; i++) {
12673c6e15bcSPyun YongHyeon 		txd = &sc->ale_cdata.ale_txdesc[i];
12683c6e15bcSPyun YongHyeon 		txd->tx_m = NULL;
12693c6e15bcSPyun YongHyeon 		txd->tx_dmamap = NULL;
12703c6e15bcSPyun YongHyeon 		error = bus_dmamap_create(sc->ale_cdata.ale_tx_tag, 0,
12713c6e15bcSPyun YongHyeon 		    &txd->tx_dmamap);
12723c6e15bcSPyun YongHyeon 		if (error != 0) {
12733c6e15bcSPyun YongHyeon 			device_printf(sc->ale_dev,
12743c6e15bcSPyun YongHyeon 			    "could not create Tx dmamap.\n");
12753c6e15bcSPyun YongHyeon 			goto fail;
12763c6e15bcSPyun YongHyeon 		}
12773c6e15bcSPyun YongHyeon 	}
12783c6e15bcSPyun YongHyeon 
12793c6e15bcSPyun YongHyeon fail:
12803c6e15bcSPyun YongHyeon 	return (error);
12813c6e15bcSPyun YongHyeon }
12823c6e15bcSPyun YongHyeon 
12833c6e15bcSPyun YongHyeon static void
ale_dma_free(struct ale_softc * sc)12843c6e15bcSPyun YongHyeon ale_dma_free(struct ale_softc *sc)
12853c6e15bcSPyun YongHyeon {
12863c6e15bcSPyun YongHyeon 	struct ale_txdesc *txd;
12873c6e15bcSPyun YongHyeon 	int i;
12883c6e15bcSPyun YongHyeon 
12893c6e15bcSPyun YongHyeon 	/* Tx buffers. */
12903c6e15bcSPyun YongHyeon 	if (sc->ale_cdata.ale_tx_tag != NULL) {
12913c6e15bcSPyun YongHyeon 		for (i = 0; i < ALE_TX_RING_CNT; i++) {
12923c6e15bcSPyun YongHyeon 			txd = &sc->ale_cdata.ale_txdesc[i];
12933c6e15bcSPyun YongHyeon 			if (txd->tx_dmamap != NULL) {
12943c6e15bcSPyun YongHyeon 				bus_dmamap_destroy(sc->ale_cdata.ale_tx_tag,
12953c6e15bcSPyun YongHyeon 				    txd->tx_dmamap);
12963c6e15bcSPyun YongHyeon 				txd->tx_dmamap = NULL;
12973c6e15bcSPyun YongHyeon 			}
12983c6e15bcSPyun YongHyeon 		}
12993c6e15bcSPyun YongHyeon 		bus_dma_tag_destroy(sc->ale_cdata.ale_tx_tag);
13003c6e15bcSPyun YongHyeon 		sc->ale_cdata.ale_tx_tag = NULL;
13013c6e15bcSPyun YongHyeon 	}
13023c6e15bcSPyun YongHyeon 	/* Tx descriptor ring. */
13033c6e15bcSPyun YongHyeon 	if (sc->ale_cdata.ale_tx_ring_tag != NULL) {
1304068d8643SJohn Baldwin 		if (sc->ale_cdata.ale_tx_ring_paddr != 0)
13053c6e15bcSPyun YongHyeon 			bus_dmamap_unload(sc->ale_cdata.ale_tx_ring_tag,
13063c6e15bcSPyun YongHyeon 			    sc->ale_cdata.ale_tx_ring_map);
1307068d8643SJohn Baldwin 		if (sc->ale_cdata.ale_tx_ring != NULL)
13083c6e15bcSPyun YongHyeon 			bus_dmamem_free(sc->ale_cdata.ale_tx_ring_tag,
13093c6e15bcSPyun YongHyeon 			    sc->ale_cdata.ale_tx_ring,
13103c6e15bcSPyun YongHyeon 			    sc->ale_cdata.ale_tx_ring_map);
1311068d8643SJohn Baldwin 		sc->ale_cdata.ale_tx_ring_paddr = 0;
13123c6e15bcSPyun YongHyeon 		sc->ale_cdata.ale_tx_ring = NULL;
13133c6e15bcSPyun YongHyeon 		bus_dma_tag_destroy(sc->ale_cdata.ale_tx_ring_tag);
13143c6e15bcSPyun YongHyeon 		sc->ale_cdata.ale_tx_ring_tag = NULL;
13153c6e15bcSPyun YongHyeon 	}
13163c6e15bcSPyun YongHyeon 	/* Rx page block. */
13173c6e15bcSPyun YongHyeon 	for (i = 0; i < ALE_RX_PAGES; i++) {
13183c6e15bcSPyun YongHyeon 		if (sc->ale_cdata.ale_rx_page[i].page_tag != NULL) {
1319068d8643SJohn Baldwin 			if (sc->ale_cdata.ale_rx_page[i].page_paddr != 0)
13203c6e15bcSPyun YongHyeon 				bus_dmamap_unload(
13213c6e15bcSPyun YongHyeon 				    sc->ale_cdata.ale_rx_page[i].page_tag,
13223c6e15bcSPyun YongHyeon 				    sc->ale_cdata.ale_rx_page[i].page_map);
1323068d8643SJohn Baldwin 			if (sc->ale_cdata.ale_rx_page[i].page_addr != NULL)
13243c6e15bcSPyun YongHyeon 				bus_dmamem_free(
13253c6e15bcSPyun YongHyeon 				    sc->ale_cdata.ale_rx_page[i].page_tag,
13263c6e15bcSPyun YongHyeon 				    sc->ale_cdata.ale_rx_page[i].page_addr,
13273c6e15bcSPyun YongHyeon 				    sc->ale_cdata.ale_rx_page[i].page_map);
1328068d8643SJohn Baldwin 			sc->ale_cdata.ale_rx_page[i].page_paddr = 0;
13293c6e15bcSPyun YongHyeon 			sc->ale_cdata.ale_rx_page[i].page_addr = NULL;
13303c6e15bcSPyun YongHyeon 			bus_dma_tag_destroy(
13313c6e15bcSPyun YongHyeon 			    sc->ale_cdata.ale_rx_page[i].page_tag);
13323c6e15bcSPyun YongHyeon 			sc->ale_cdata.ale_rx_page[i].page_tag = NULL;
13333c6e15bcSPyun YongHyeon 		}
13343c6e15bcSPyun YongHyeon 	}
13353c6e15bcSPyun YongHyeon 	/* Rx CMB. */
13363c6e15bcSPyun YongHyeon 	for (i = 0; i < ALE_RX_PAGES; i++) {
13373c6e15bcSPyun YongHyeon 		if (sc->ale_cdata.ale_rx_page[i].cmb_tag != NULL) {
1338068d8643SJohn Baldwin 			if (sc->ale_cdata.ale_rx_page[i].cmb_paddr != 0)
13393c6e15bcSPyun YongHyeon 				bus_dmamap_unload(
13403c6e15bcSPyun YongHyeon 				    sc->ale_cdata.ale_rx_page[i].cmb_tag,
13413c6e15bcSPyun YongHyeon 				    sc->ale_cdata.ale_rx_page[i].cmb_map);
1342068d8643SJohn Baldwin 			if (sc->ale_cdata.ale_rx_page[i].cmb_addr != NULL)
13433c6e15bcSPyun YongHyeon 				bus_dmamem_free(
13443c6e15bcSPyun YongHyeon 				    sc->ale_cdata.ale_rx_page[i].cmb_tag,
13453c6e15bcSPyun YongHyeon 				    sc->ale_cdata.ale_rx_page[i].cmb_addr,
13463c6e15bcSPyun YongHyeon 				    sc->ale_cdata.ale_rx_page[i].cmb_map);
1347068d8643SJohn Baldwin 			sc->ale_cdata.ale_rx_page[i].cmb_paddr = 0;
13483c6e15bcSPyun YongHyeon 			sc->ale_cdata.ale_rx_page[i].cmb_addr = NULL;
13493c6e15bcSPyun YongHyeon 			bus_dma_tag_destroy(
13503c6e15bcSPyun YongHyeon 			    sc->ale_cdata.ale_rx_page[i].cmb_tag);
13513c6e15bcSPyun YongHyeon 			sc->ale_cdata.ale_rx_page[i].cmb_tag = NULL;
13523c6e15bcSPyun YongHyeon 		}
13533c6e15bcSPyun YongHyeon 	}
13543c6e15bcSPyun YongHyeon 	/* Tx CMB. */
13553c6e15bcSPyun YongHyeon 	if (sc->ale_cdata.ale_tx_cmb_tag != NULL) {
1356068d8643SJohn Baldwin 		if (sc->ale_cdata.ale_tx_cmb_paddr != 0)
13573c6e15bcSPyun YongHyeon 			bus_dmamap_unload(sc->ale_cdata.ale_tx_cmb_tag,
13583c6e15bcSPyun YongHyeon 			    sc->ale_cdata.ale_tx_cmb_map);
1359068d8643SJohn Baldwin 		if (sc->ale_cdata.ale_tx_cmb != NULL)
13603c6e15bcSPyun YongHyeon 			bus_dmamem_free(sc->ale_cdata.ale_tx_cmb_tag,
13613c6e15bcSPyun YongHyeon 			    sc->ale_cdata.ale_tx_cmb,
13623c6e15bcSPyun YongHyeon 			    sc->ale_cdata.ale_tx_cmb_map);
1363068d8643SJohn Baldwin 		sc->ale_cdata.ale_tx_cmb_paddr = 0;
13643c6e15bcSPyun YongHyeon 		sc->ale_cdata.ale_tx_cmb = NULL;
13653c6e15bcSPyun YongHyeon 		bus_dma_tag_destroy(sc->ale_cdata.ale_tx_cmb_tag);
13663c6e15bcSPyun YongHyeon 		sc->ale_cdata.ale_tx_cmb_tag = NULL;
13673c6e15bcSPyun YongHyeon 	}
13683c6e15bcSPyun YongHyeon 	if (sc->ale_cdata.ale_buffer_tag != NULL) {
13693c6e15bcSPyun YongHyeon 		bus_dma_tag_destroy(sc->ale_cdata.ale_buffer_tag);
13703c6e15bcSPyun YongHyeon 		sc->ale_cdata.ale_buffer_tag = NULL;
13713c6e15bcSPyun YongHyeon 	}
13723c6e15bcSPyun YongHyeon 	if (sc->ale_cdata.ale_parent_tag != NULL) {
13733c6e15bcSPyun YongHyeon 		bus_dma_tag_destroy(sc->ale_cdata.ale_parent_tag);
13743c6e15bcSPyun YongHyeon 		sc->ale_cdata.ale_parent_tag = NULL;
13753c6e15bcSPyun YongHyeon 	}
13763c6e15bcSPyun YongHyeon }
13773c6e15bcSPyun YongHyeon 
13783c6e15bcSPyun YongHyeon static int
ale_shutdown(device_t dev)13793c6e15bcSPyun YongHyeon ale_shutdown(device_t dev)
13803c6e15bcSPyun YongHyeon {
13813c6e15bcSPyun YongHyeon 
13823c6e15bcSPyun YongHyeon 	return (ale_suspend(dev));
13833c6e15bcSPyun YongHyeon }
13843c6e15bcSPyun YongHyeon 
13853c6e15bcSPyun YongHyeon /*
13863c6e15bcSPyun YongHyeon  * Note, this driver resets the link speed to 10/100Mbps by
13873c6e15bcSPyun YongHyeon  * restarting auto-negotiation in suspend/shutdown phase but we
13883c6e15bcSPyun YongHyeon  * don't know whether that auto-negotiation would succeed or not
13893c6e15bcSPyun YongHyeon  * as driver has no control after powering off/suspend operation.
13903c6e15bcSPyun YongHyeon  * If the renegotiation fail WOL may not work. Running at 1Gbps
13913c6e15bcSPyun YongHyeon  * will draw more power than 375mA at 3.3V which is specified in
13923c6e15bcSPyun YongHyeon  * PCI specification and that would result in complete
13933c6e15bcSPyun YongHyeon  * shutdowning power to ethernet controller.
13943c6e15bcSPyun YongHyeon  *
13953c6e15bcSPyun YongHyeon  * TODO
13963c6e15bcSPyun YongHyeon  * Save current negotiated media speed/duplex/flow-control to
13973c6e15bcSPyun YongHyeon  * softc and restore the same link again after resuming. PHY
13983c6e15bcSPyun YongHyeon  * handling such as power down/resetting to 100Mbps may be better
13993c6e15bcSPyun YongHyeon  * handled in suspend method in phy driver.
14003c6e15bcSPyun YongHyeon  */
14013c6e15bcSPyun YongHyeon static void
ale_setlinkspeed(struct ale_softc * sc)14023c6e15bcSPyun YongHyeon ale_setlinkspeed(struct ale_softc *sc)
14033c6e15bcSPyun YongHyeon {
14043c6e15bcSPyun YongHyeon 	struct mii_data *mii;
14053c6e15bcSPyun YongHyeon 	int aneg, i;
14063c6e15bcSPyun YongHyeon 
14073c6e15bcSPyun YongHyeon 	mii = device_get_softc(sc->ale_miibus);
14083c6e15bcSPyun YongHyeon 	mii_pollstat(mii);
14093c6e15bcSPyun YongHyeon 	aneg = 0;
14103c6e15bcSPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
14113c6e15bcSPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
14123c6e15bcSPyun YongHyeon 		switch IFM_SUBTYPE(mii->mii_media_active) {
14133c6e15bcSPyun YongHyeon 		case IFM_10_T:
14143c6e15bcSPyun YongHyeon 		case IFM_100_TX:
14153c6e15bcSPyun YongHyeon 			return;
14163c6e15bcSPyun YongHyeon 		case IFM_1000_T:
14173c6e15bcSPyun YongHyeon 			aneg++;
14183c6e15bcSPyun YongHyeon 			break;
14193c6e15bcSPyun YongHyeon 		default:
14203c6e15bcSPyun YongHyeon 			break;
14213c6e15bcSPyun YongHyeon 		}
14223c6e15bcSPyun YongHyeon 	}
14233c6e15bcSPyun YongHyeon 	ale_miibus_writereg(sc->ale_dev, sc->ale_phyaddr, MII_100T2CR, 0);
14243c6e15bcSPyun YongHyeon 	ale_miibus_writereg(sc->ale_dev, sc->ale_phyaddr,
14253c6e15bcSPyun YongHyeon 	    MII_ANAR, ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10 | ANAR_CSMA);
14263c6e15bcSPyun YongHyeon 	ale_miibus_writereg(sc->ale_dev, sc->ale_phyaddr,
14273c6e15bcSPyun YongHyeon 	    MII_BMCR, BMCR_RESET | BMCR_AUTOEN | BMCR_STARTNEG);
14283c6e15bcSPyun YongHyeon 	DELAY(1000);
14293c6e15bcSPyun YongHyeon 	if (aneg != 0) {
14303c6e15bcSPyun YongHyeon 		/*
14313c6e15bcSPyun YongHyeon 		 * Poll link state until ale(4) get a 10/100Mbps link.
14323c6e15bcSPyun YongHyeon 		 */
14333c6e15bcSPyun YongHyeon 		for (i = 0; i < MII_ANEGTICKS_GIGE; i++) {
14343c6e15bcSPyun YongHyeon 			mii_pollstat(mii);
14353c6e15bcSPyun YongHyeon 			if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID))
14363c6e15bcSPyun YongHyeon 			    == (IFM_ACTIVE | IFM_AVALID)) {
14373c6e15bcSPyun YongHyeon 				switch (IFM_SUBTYPE(
14383c6e15bcSPyun YongHyeon 				    mii->mii_media_active)) {
14393c6e15bcSPyun YongHyeon 				case IFM_10_T:
14403c6e15bcSPyun YongHyeon 				case IFM_100_TX:
14413c6e15bcSPyun YongHyeon 					ale_mac_config(sc);
14423c6e15bcSPyun YongHyeon 					return;
14433c6e15bcSPyun YongHyeon 				default:
14443c6e15bcSPyun YongHyeon 					break;
14453c6e15bcSPyun YongHyeon 				}
14463c6e15bcSPyun YongHyeon 			}
14473c6e15bcSPyun YongHyeon 			ALE_UNLOCK(sc);
14483c6e15bcSPyun YongHyeon 			pause("alelnk", hz);
14493c6e15bcSPyun YongHyeon 			ALE_LOCK(sc);
14503c6e15bcSPyun YongHyeon 		}
14513c6e15bcSPyun YongHyeon 		if (i == MII_ANEGTICKS_GIGE)
14523c6e15bcSPyun YongHyeon 			device_printf(sc->ale_dev,
14533c6e15bcSPyun YongHyeon 			    "establishing a link failed, WOL may not work!");
14543c6e15bcSPyun YongHyeon 	}
14553c6e15bcSPyun YongHyeon 	/*
14563c6e15bcSPyun YongHyeon 	 * No link, force MAC to have 100Mbps, full-duplex link.
14573c6e15bcSPyun YongHyeon 	 * This is the last resort and may/may not work.
14583c6e15bcSPyun YongHyeon 	 */
14593c6e15bcSPyun YongHyeon 	mii->mii_media_status = IFM_AVALID | IFM_ACTIVE;
14603c6e15bcSPyun YongHyeon 	mii->mii_media_active = IFM_ETHER | IFM_100_TX | IFM_FDX;
14613c6e15bcSPyun YongHyeon 	ale_mac_config(sc);
14623c6e15bcSPyun YongHyeon }
14633c6e15bcSPyun YongHyeon 
14643c6e15bcSPyun YongHyeon static void
ale_setwol(struct ale_softc * sc)14653c6e15bcSPyun YongHyeon ale_setwol(struct ale_softc *sc)
14663c6e15bcSPyun YongHyeon {
146752436412SJustin Hibbits 	if_t ifp;
14683c6e15bcSPyun YongHyeon 	uint32_t reg, pmcs;
14693c6e15bcSPyun YongHyeon 
14703c6e15bcSPyun YongHyeon 	ALE_LOCK_ASSERT(sc);
14713c6e15bcSPyun YongHyeon 
1472*ddaf6524SJohn Baldwin 	if (!pci_has_pm(sc->ale_dev)) {
14733c6e15bcSPyun YongHyeon 		/* Disable WOL. */
14743c6e15bcSPyun YongHyeon 		CSR_WRITE_4(sc, ALE_WOL_CFG, 0);
14753c6e15bcSPyun YongHyeon 		reg = CSR_READ_4(sc, ALE_PCIE_PHYMISC);
14763c6e15bcSPyun YongHyeon 		reg |= PCIE_PHYMISC_FORCE_RCV_DET;
14773c6e15bcSPyun YongHyeon 		CSR_WRITE_4(sc, ALE_PCIE_PHYMISC, reg);
14783c6e15bcSPyun YongHyeon 		/* Force PHY power down. */
14793c6e15bcSPyun YongHyeon 		CSR_WRITE_2(sc, ALE_GPHY_CTRL,
14803c6e15bcSPyun YongHyeon 		    GPHY_CTRL_EXT_RESET | GPHY_CTRL_HIB_EN |
14813c6e15bcSPyun YongHyeon 		    GPHY_CTRL_HIB_PULSE | GPHY_CTRL_PHY_PLL_ON |
14823c6e15bcSPyun YongHyeon 		    GPHY_CTRL_SEL_ANA_RESET | GPHY_CTRL_PHY_IDDQ |
14833c6e15bcSPyun YongHyeon 		    GPHY_CTRL_PCLK_SEL_DIS | GPHY_CTRL_PWDOWN_HW);
14843c6e15bcSPyun YongHyeon 		return;
14853c6e15bcSPyun YongHyeon 	}
14863c6e15bcSPyun YongHyeon 
14873c6e15bcSPyun YongHyeon 	ifp = sc->ale_ifp;
148852436412SJustin Hibbits 	if ((if_getcapenable(ifp) & IFCAP_WOL) != 0) {
14893c6e15bcSPyun YongHyeon 		if ((sc->ale_flags & ALE_FLAG_FASTETHER) == 0)
14903c6e15bcSPyun YongHyeon 			ale_setlinkspeed(sc);
14913c6e15bcSPyun YongHyeon 	}
14923c6e15bcSPyun YongHyeon 
14933c6e15bcSPyun YongHyeon 	pmcs = 0;
149452436412SJustin Hibbits 	if ((if_getcapenable(ifp) & IFCAP_WOL_MAGIC) != 0)
14953c6e15bcSPyun YongHyeon 		pmcs |= WOL_CFG_MAGIC | WOL_CFG_MAGIC_ENB;
14963c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_WOL_CFG, pmcs);
14973c6e15bcSPyun YongHyeon 	reg = CSR_READ_4(sc, ALE_MAC_CFG);
14983c6e15bcSPyun YongHyeon 	reg &= ~(MAC_CFG_DBG | MAC_CFG_PROMISC | MAC_CFG_ALLMULTI |
14993c6e15bcSPyun YongHyeon 	    MAC_CFG_BCAST);
150052436412SJustin Hibbits 	if ((if_getcapenable(ifp) & IFCAP_WOL_MCAST) != 0)
15013c6e15bcSPyun YongHyeon 		reg |= MAC_CFG_ALLMULTI | MAC_CFG_BCAST;
150252436412SJustin Hibbits 	if ((if_getcapenable(ifp) & IFCAP_WOL) != 0)
15033c6e15bcSPyun YongHyeon 		reg |= MAC_CFG_RX_ENB;
15043c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_MAC_CFG, reg);
15053c6e15bcSPyun YongHyeon 
150652436412SJustin Hibbits 	if ((if_getcapenable(ifp) & IFCAP_WOL) == 0) {
15073c6e15bcSPyun YongHyeon 		/* WOL disabled, PHY power down. */
15083c6e15bcSPyun YongHyeon 		reg = CSR_READ_4(sc, ALE_PCIE_PHYMISC);
15093c6e15bcSPyun YongHyeon 		reg |= PCIE_PHYMISC_FORCE_RCV_DET;
15103c6e15bcSPyun YongHyeon 		CSR_WRITE_4(sc, ALE_PCIE_PHYMISC, reg);
15113c6e15bcSPyun YongHyeon 		CSR_WRITE_2(sc, ALE_GPHY_CTRL,
15123c6e15bcSPyun YongHyeon 		    GPHY_CTRL_EXT_RESET | GPHY_CTRL_HIB_EN |
15133c6e15bcSPyun YongHyeon 		    GPHY_CTRL_HIB_PULSE | GPHY_CTRL_SEL_ANA_RESET |
15143c6e15bcSPyun YongHyeon 		    GPHY_CTRL_PHY_IDDQ | GPHY_CTRL_PCLK_SEL_DIS |
15153c6e15bcSPyun YongHyeon 		    GPHY_CTRL_PWDOWN_HW);
15163c6e15bcSPyun YongHyeon 	}
15173c6e15bcSPyun YongHyeon 	/* Request PME. */
151852436412SJustin Hibbits 	if ((if_getcapenable(ifp) & IFCAP_WOL) != 0)
1519*ddaf6524SJohn Baldwin 		pci_enable_pme(sc->ale_dev);
15203c6e15bcSPyun YongHyeon }
15213c6e15bcSPyun YongHyeon 
15223c6e15bcSPyun YongHyeon static int
ale_suspend(device_t dev)15233c6e15bcSPyun YongHyeon ale_suspend(device_t dev)
15243c6e15bcSPyun YongHyeon {
15253c6e15bcSPyun YongHyeon 	struct ale_softc *sc;
15263c6e15bcSPyun YongHyeon 
15273c6e15bcSPyun YongHyeon 	sc = device_get_softc(dev);
15283c6e15bcSPyun YongHyeon 
15293c6e15bcSPyun YongHyeon 	ALE_LOCK(sc);
15303c6e15bcSPyun YongHyeon 	ale_stop(sc);
15313c6e15bcSPyun YongHyeon 	ale_setwol(sc);
15323c6e15bcSPyun YongHyeon 	ALE_UNLOCK(sc);
15333c6e15bcSPyun YongHyeon 
15343c6e15bcSPyun YongHyeon 	return (0);
15353c6e15bcSPyun YongHyeon }
15363c6e15bcSPyun YongHyeon 
15373c6e15bcSPyun YongHyeon static int
ale_resume(device_t dev)15383c6e15bcSPyun YongHyeon ale_resume(device_t dev)
15393c6e15bcSPyun YongHyeon {
15403c6e15bcSPyun YongHyeon 	struct ale_softc *sc;
154152436412SJustin Hibbits 	if_t ifp;
15423c6e15bcSPyun YongHyeon 
15433c6e15bcSPyun YongHyeon 	sc = device_get_softc(dev);
15443c6e15bcSPyun YongHyeon 
15453c6e15bcSPyun YongHyeon 	/* Reset PHY. */
1546*ddaf6524SJohn Baldwin 	ALE_LOCK(sc);
15473c6e15bcSPyun YongHyeon 	ale_phy_reset(sc);
15483c6e15bcSPyun YongHyeon 	ifp = sc->ale_ifp;
154952436412SJustin Hibbits 	if ((if_getflags(ifp) & IFF_UP) != 0) {
155052436412SJustin Hibbits 		if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
15513c6e15bcSPyun YongHyeon 		ale_init_locked(sc);
15523c6e15bcSPyun YongHyeon 	}
15533c6e15bcSPyun YongHyeon 	ALE_UNLOCK(sc);
15543c6e15bcSPyun YongHyeon 
15553c6e15bcSPyun YongHyeon 	return (0);
15563c6e15bcSPyun YongHyeon }
15573c6e15bcSPyun YongHyeon 
15583c6e15bcSPyun YongHyeon static int
ale_encap(struct ale_softc * sc,struct mbuf ** m_head)15593c6e15bcSPyun YongHyeon ale_encap(struct ale_softc *sc, struct mbuf **m_head)
15603c6e15bcSPyun YongHyeon {
15613c6e15bcSPyun YongHyeon 	struct ale_txdesc *txd, *txd_last;
15623c6e15bcSPyun YongHyeon 	struct tx_desc *desc;
15633c6e15bcSPyun YongHyeon 	struct mbuf *m;
15643c6e15bcSPyun YongHyeon 	struct ip *ip;
15653c6e15bcSPyun YongHyeon 	struct tcphdr *tcp;
15663c6e15bcSPyun YongHyeon 	bus_dma_segment_t txsegs[ALE_MAXTXSEGS];
15673c6e15bcSPyun YongHyeon 	bus_dmamap_t map;
15684c7421aaSPyun YongHyeon 	uint32_t cflags, hdrlen, ip_off, poff, vtag;
15693c6e15bcSPyun YongHyeon 	int error, i, nsegs, prod, si;
15703c6e15bcSPyun YongHyeon 
15713c6e15bcSPyun YongHyeon 	ALE_LOCK_ASSERT(sc);
15723c6e15bcSPyun YongHyeon 
15733c6e15bcSPyun YongHyeon 	M_ASSERTPKTHDR((*m_head));
15743c6e15bcSPyun YongHyeon 
15753c6e15bcSPyun YongHyeon 	m = *m_head;
15763c6e15bcSPyun YongHyeon 	ip = NULL;
15773c6e15bcSPyun YongHyeon 	tcp = NULL;
15783c6e15bcSPyun YongHyeon 	cflags = vtag = 0;
15793c6e15bcSPyun YongHyeon 	ip_off = poff = 0;
15803c6e15bcSPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & (ALE_CSUM_FEATURES | CSUM_TSO)) != 0) {
15813c6e15bcSPyun YongHyeon 		/*
15823c6e15bcSPyun YongHyeon 		 * AR81xx requires offset of TCP/UDP payload in its Tx
15833c6e15bcSPyun YongHyeon 		 * descriptor to perform hardware Tx checksum offload.
15843c6e15bcSPyun YongHyeon 		 * Additionally, TSO requires IP/TCP header size and
15853c6e15bcSPyun YongHyeon 		 * modification of IP/TCP header in order to make TSO
15863c6e15bcSPyun YongHyeon 		 * engine work. This kind of operation takes many CPU
15873c6e15bcSPyun YongHyeon 		 * cycles on FreeBSD so fast host CPU is required to
15883c6e15bcSPyun YongHyeon 		 * get smooth TSO performance.
15893c6e15bcSPyun YongHyeon 		 */
15903c6e15bcSPyun YongHyeon 		struct ether_header *eh;
15913c6e15bcSPyun YongHyeon 
15923c6e15bcSPyun YongHyeon 		if (M_WRITABLE(m) == 0) {
15933c6e15bcSPyun YongHyeon 			/* Get a writable copy. */
1594c6499eccSGleb Smirnoff 			m = m_dup(*m_head, M_NOWAIT);
15953c6e15bcSPyun YongHyeon 			/* Release original mbufs. */
15963c6e15bcSPyun YongHyeon 			m_freem(*m_head);
15973c6e15bcSPyun YongHyeon 			if (m == NULL) {
15983c6e15bcSPyun YongHyeon 				*m_head = NULL;
15993c6e15bcSPyun YongHyeon 				return (ENOBUFS);
16003c6e15bcSPyun YongHyeon 			}
16013c6e15bcSPyun YongHyeon 			*m_head = m;
16023c6e15bcSPyun YongHyeon 		}
16033c6e15bcSPyun YongHyeon 
16043c6e15bcSPyun YongHyeon 		/*
16053c6e15bcSPyun YongHyeon 		 * Buggy-controller requires 4 byte aligned Tx buffer
16063c6e15bcSPyun YongHyeon 		 * to make custom checksum offload work.
16073c6e15bcSPyun YongHyeon 		 */
16083c6e15bcSPyun YongHyeon 		if ((sc->ale_flags & ALE_FLAG_TXCSUM_BUG) != 0 &&
16093c6e15bcSPyun YongHyeon 		    (m->m_pkthdr.csum_flags & ALE_CSUM_FEATURES) != 0 &&
16103c6e15bcSPyun YongHyeon 		    (mtod(m, intptr_t) & 3) != 0) {
1611c6499eccSGleb Smirnoff 			m = m_defrag(*m_head, M_NOWAIT);
16123c6e15bcSPyun YongHyeon 			if (m == NULL) {
16132c6d18ebSPyun YongHyeon 				m_freem(*m_head);
16143c6e15bcSPyun YongHyeon 				*m_head = NULL;
16153c6e15bcSPyun YongHyeon 				return (ENOBUFS);
16163c6e15bcSPyun YongHyeon 			}
16173c6e15bcSPyun YongHyeon 			*m_head = m;
16183c6e15bcSPyun YongHyeon 		}
16193c6e15bcSPyun YongHyeon 
16203c6e15bcSPyun YongHyeon 		ip_off = sizeof(struct ether_header);
16213c6e15bcSPyun YongHyeon 		m = m_pullup(m, ip_off);
16223c6e15bcSPyun YongHyeon 		if (m == NULL) {
16233c6e15bcSPyun YongHyeon 			*m_head = NULL;
16243c6e15bcSPyun YongHyeon 			return (ENOBUFS);
16253c6e15bcSPyun YongHyeon 		}
16263c6e15bcSPyun YongHyeon 		eh = mtod(m, struct ether_header *);
16273c6e15bcSPyun YongHyeon 		/*
16283c6e15bcSPyun YongHyeon 		 * Check if hardware VLAN insertion is off.
16293c6e15bcSPyun YongHyeon 		 * Additional check for LLC/SNAP frame?
16303c6e15bcSPyun YongHyeon 		 */
16313c6e15bcSPyun YongHyeon 		if (eh->ether_type == htons(ETHERTYPE_VLAN)) {
16323c6e15bcSPyun YongHyeon 			ip_off = sizeof(struct ether_vlan_header);
16333c6e15bcSPyun YongHyeon 			m = m_pullup(m, ip_off);
16343c6e15bcSPyun YongHyeon 			if (m == NULL) {
16353c6e15bcSPyun YongHyeon 				*m_head = NULL;
16363c6e15bcSPyun YongHyeon 				return (ENOBUFS);
16373c6e15bcSPyun YongHyeon 			}
16383c6e15bcSPyun YongHyeon 		}
16393c6e15bcSPyun YongHyeon 		m = m_pullup(m, ip_off + sizeof(struct ip));
16403c6e15bcSPyun YongHyeon 		if (m == NULL) {
16413c6e15bcSPyun YongHyeon 			*m_head = NULL;
16423c6e15bcSPyun YongHyeon 			return (ENOBUFS);
16433c6e15bcSPyun YongHyeon 		}
16443c6e15bcSPyun YongHyeon 		ip = (struct ip *)(mtod(m, char *) + ip_off);
16453c6e15bcSPyun YongHyeon 		poff = ip_off + (ip->ip_hl << 2);
16463c6e15bcSPyun YongHyeon 		if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
16473c6e15bcSPyun YongHyeon 			/*
16483c6e15bcSPyun YongHyeon 			 * XXX
16493c6e15bcSPyun YongHyeon 			 * AR81xx requires the first descriptor should
16503c6e15bcSPyun YongHyeon 			 * not include any TCP playload for TSO case.
16513c6e15bcSPyun YongHyeon 			 * (i.e. ethernet header + IP + TCP header only)
16523c6e15bcSPyun YongHyeon 			 * m_pullup(9) above will ensure this too.
16533c6e15bcSPyun YongHyeon 			 * However it's not correct if the first mbuf
16543c6e15bcSPyun YongHyeon 			 * of the chain does not use cluster.
16553c6e15bcSPyun YongHyeon 			 */
16563c6e15bcSPyun YongHyeon 			m = m_pullup(m, poff + sizeof(struct tcphdr));
16573c6e15bcSPyun YongHyeon 			if (m == NULL) {
16583c6e15bcSPyun YongHyeon 				*m_head = NULL;
16593c6e15bcSPyun YongHyeon 				return (ENOBUFS);
16603c6e15bcSPyun YongHyeon 			}
166196486faaSPyun YongHyeon 			ip = (struct ip *)(mtod(m, char *) + ip_off);
16623c6e15bcSPyun YongHyeon 			tcp = (struct tcphdr *)(mtod(m, char *) + poff);
16634c7421aaSPyun YongHyeon 			m = m_pullup(m, poff + (tcp->th_off << 2));
16644c7421aaSPyun YongHyeon 			if (m == NULL) {
16654c7421aaSPyun YongHyeon 				*m_head = NULL;
16664c7421aaSPyun YongHyeon 				return (ENOBUFS);
16674c7421aaSPyun YongHyeon 			}
16683c6e15bcSPyun YongHyeon 			/*
16693c6e15bcSPyun YongHyeon 			 * AR81xx requires IP/TCP header size and offset as
16703c6e15bcSPyun YongHyeon 			 * well as TCP pseudo checksum which complicates
16713c6e15bcSPyun YongHyeon 			 * TSO configuration. I guess this comes from the
16723c6e15bcSPyun YongHyeon 			 * adherence to Microsoft NDIS Large Send
16733c6e15bcSPyun YongHyeon 			 * specification which requires insertion of
16743c6e15bcSPyun YongHyeon 			 * pseudo checksum by upper stack. The pseudo
16753c6e15bcSPyun YongHyeon 			 * checksum that NDIS refers to doesn't include
16763c6e15bcSPyun YongHyeon 			 * TCP payload length so ale(4) should recompute
16773c6e15bcSPyun YongHyeon 			 * the pseudo checksum here. Hopefully this wouldn't
16783c6e15bcSPyun YongHyeon 			 * be much burden on modern CPUs.
16793c6e15bcSPyun YongHyeon 			 * Reset IP checksum and recompute TCP pseudo
16803c6e15bcSPyun YongHyeon 			 * checksum as NDIS specification said.
16813c6e15bcSPyun YongHyeon 			 */
16823c6e15bcSPyun YongHyeon 			ip->ip_sum = 0;
16833c6e15bcSPyun YongHyeon 			tcp->th_sum = in_pseudo(ip->ip_src.s_addr,
16843c6e15bcSPyun YongHyeon 			    ip->ip_dst.s_addr, htons(IPPROTO_TCP));
16853c6e15bcSPyun YongHyeon 		}
16863c6e15bcSPyun YongHyeon 		*m_head = m;
16873c6e15bcSPyun YongHyeon 	}
16883c6e15bcSPyun YongHyeon 
16893c6e15bcSPyun YongHyeon 	si = prod = sc->ale_cdata.ale_tx_prod;
16903c6e15bcSPyun YongHyeon 	txd = &sc->ale_cdata.ale_txdesc[prod];
16913c6e15bcSPyun YongHyeon 	txd_last = txd;
16923c6e15bcSPyun YongHyeon 	map = txd->tx_dmamap;
16933c6e15bcSPyun YongHyeon 
16943c6e15bcSPyun YongHyeon 	error =  bus_dmamap_load_mbuf_sg(sc->ale_cdata.ale_tx_tag, map,
16953c6e15bcSPyun YongHyeon 	    *m_head, txsegs, &nsegs, 0);
16963c6e15bcSPyun YongHyeon 	if (error == EFBIG) {
1697c6499eccSGleb Smirnoff 		m = m_collapse(*m_head, M_NOWAIT, ALE_MAXTXSEGS);
16983c6e15bcSPyun YongHyeon 		if (m == NULL) {
16993c6e15bcSPyun YongHyeon 			m_freem(*m_head);
17003c6e15bcSPyun YongHyeon 			*m_head = NULL;
17013c6e15bcSPyun YongHyeon 			return (ENOMEM);
17023c6e15bcSPyun YongHyeon 		}
17033c6e15bcSPyun YongHyeon 		*m_head = m;
17043c6e15bcSPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->ale_cdata.ale_tx_tag, map,
17053c6e15bcSPyun YongHyeon 		    *m_head, txsegs, &nsegs, 0);
17063c6e15bcSPyun YongHyeon 		if (error != 0) {
17073c6e15bcSPyun YongHyeon 			m_freem(*m_head);
17083c6e15bcSPyun YongHyeon 			*m_head = NULL;
17093c6e15bcSPyun YongHyeon 			return (error);
17103c6e15bcSPyun YongHyeon 		}
17113c6e15bcSPyun YongHyeon 	} else if (error != 0)
17123c6e15bcSPyun YongHyeon 		return (error);
17133c6e15bcSPyun YongHyeon 	if (nsegs == 0) {
17143c6e15bcSPyun YongHyeon 		m_freem(*m_head);
17153c6e15bcSPyun YongHyeon 		*m_head = NULL;
17163c6e15bcSPyun YongHyeon 		return (EIO);
17173c6e15bcSPyun YongHyeon 	}
17183c6e15bcSPyun YongHyeon 
17193c6e15bcSPyun YongHyeon 	/* Check descriptor overrun. */
17204c7421aaSPyun YongHyeon 	if (sc->ale_cdata.ale_tx_cnt + nsegs >= ALE_TX_RING_CNT - 3) {
17213c6e15bcSPyun YongHyeon 		bus_dmamap_unload(sc->ale_cdata.ale_tx_tag, map);
17223c6e15bcSPyun YongHyeon 		return (ENOBUFS);
17233c6e15bcSPyun YongHyeon 	}
17243c6e15bcSPyun YongHyeon 	bus_dmamap_sync(sc->ale_cdata.ale_tx_tag, map, BUS_DMASYNC_PREWRITE);
17253c6e15bcSPyun YongHyeon 
17263c6e15bcSPyun YongHyeon 	m = *m_head;
17276da6d0a9SPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
17286da6d0a9SPyun YongHyeon 		/* Request TSO and set MSS. */
17296da6d0a9SPyun YongHyeon 		cflags |= ALE_TD_TSO;
17306da6d0a9SPyun YongHyeon 		cflags |= ((uint32_t)m->m_pkthdr.tso_segsz << ALE_TD_MSS_SHIFT);
17316da6d0a9SPyun YongHyeon 		/* Set IP/TCP header size. */
17326da6d0a9SPyun YongHyeon 		cflags |= ip->ip_hl << ALE_TD_IPHDR_LEN_SHIFT;
17336da6d0a9SPyun YongHyeon 		cflags |= tcp->th_off << ALE_TD_TCPHDR_LEN_SHIFT;
17346da6d0a9SPyun YongHyeon 	} else if ((m->m_pkthdr.csum_flags & ALE_CSUM_FEATURES) != 0) {
17353c6e15bcSPyun YongHyeon 		/*
17363c6e15bcSPyun YongHyeon 		 * AR81xx supports Tx custom checksum offload feature
17373c6e15bcSPyun YongHyeon 		 * that offloads single 16bit checksum computation.
17383c6e15bcSPyun YongHyeon 		 * So you can choose one among IP, TCP and UDP.
17393c6e15bcSPyun YongHyeon 		 * Normally driver sets checksum start/insertion
17403c6e15bcSPyun YongHyeon 		 * position from the information of TCP/UDP frame as
17413c6e15bcSPyun YongHyeon 		 * TCP/UDP checksum takes more time than that of IP.
17423c6e15bcSPyun YongHyeon 		 * However it seems that custom checksum offload
17433c6e15bcSPyun YongHyeon 		 * requires 4 bytes aligned Tx buffers due to hardware
17443c6e15bcSPyun YongHyeon 		 * bug.
17453c6e15bcSPyun YongHyeon 		 * AR81xx also supports explicit Tx checksum computation
17463c6e15bcSPyun YongHyeon 		 * if it is told that the size of IP header and TCP
17473c6e15bcSPyun YongHyeon 		 * header(for UDP, the header size does not matter
17483c6e15bcSPyun YongHyeon 		 * because it's fixed length). However with this scheme
17493c6e15bcSPyun YongHyeon 		 * TSO does not work so you have to choose one either
17503c6e15bcSPyun YongHyeon 		 * TSO or explicit Tx checksum offload. I chosen TSO
17513c6e15bcSPyun YongHyeon 		 * plus custom checksum offload with work-around which
17523c6e15bcSPyun YongHyeon 		 * will cover most common usage for this consumer
17533c6e15bcSPyun YongHyeon 		 * ethernet controller. The work-around takes a lot of
17543c6e15bcSPyun YongHyeon 		 * CPU cycles if Tx buffer is not aligned on 4 bytes
17553c6e15bcSPyun YongHyeon 		 * boundary, though.
17563c6e15bcSPyun YongHyeon 		 */
17573c6e15bcSPyun YongHyeon 		cflags |= ALE_TD_CXSUM;
17583c6e15bcSPyun YongHyeon 		/* Set checksum start offset. */
17593c6e15bcSPyun YongHyeon 		cflags |= (poff << ALE_TD_CSUM_PLOADOFFSET_SHIFT);
17603c6e15bcSPyun YongHyeon 		/* Set checksum insertion position of TCP/UDP. */
17613c6e15bcSPyun YongHyeon 		cflags |= ((poff + m->m_pkthdr.csum_data) <<
17623c6e15bcSPyun YongHyeon 		    ALE_TD_CSUM_XSUMOFFSET_SHIFT);
17633c6e15bcSPyun YongHyeon 	}
17643c6e15bcSPyun YongHyeon 
17653c6e15bcSPyun YongHyeon 	/* Configure VLAN hardware tag insertion. */
17663c6e15bcSPyun YongHyeon 	if ((m->m_flags & M_VLANTAG) != 0) {
17673c6e15bcSPyun YongHyeon 		vtag = ALE_TX_VLAN_TAG(m->m_pkthdr.ether_vtag);
17683c6e15bcSPyun YongHyeon 		vtag = ((vtag << ALE_TD_VLAN_SHIFT) & ALE_TD_VLAN_MASK);
17693c6e15bcSPyun YongHyeon 		cflags |= ALE_TD_INSERT_VLAN_TAG;
17703c6e15bcSPyun YongHyeon 	}
17713c6e15bcSPyun YongHyeon 
17724c7421aaSPyun YongHyeon 	i = 0;
17734c7421aaSPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
17744c7421aaSPyun YongHyeon 		/*
17754c7421aaSPyun YongHyeon 		 * Make sure the first fragment contains
17764c7421aaSPyun YongHyeon 		 * only ethernet and IP/TCP header with options.
17774c7421aaSPyun YongHyeon 		 */
17784c7421aaSPyun YongHyeon 		hdrlen =  poff + (tcp->th_off << 2);
17794c7421aaSPyun YongHyeon 		desc = &sc->ale_cdata.ale_tx_ring[prod];
17804c7421aaSPyun YongHyeon 		desc->addr = htole64(txsegs[i].ds_addr);
17814c7421aaSPyun YongHyeon 		desc->len = htole32(ALE_TX_BYTES(hdrlen) | vtag);
17824c7421aaSPyun YongHyeon 		desc->flags = htole32(cflags);
17834c7421aaSPyun YongHyeon 		sc->ale_cdata.ale_tx_cnt++;
17844c7421aaSPyun YongHyeon 		ALE_DESC_INC(prod, ALE_TX_RING_CNT);
17854c7421aaSPyun YongHyeon 		if (m->m_len - hdrlen > 0) {
17864c7421aaSPyun YongHyeon 			/* Handle remaining payload of the first fragment. */
17874c7421aaSPyun YongHyeon 			desc = &sc->ale_cdata.ale_tx_ring[prod];
17884c7421aaSPyun YongHyeon 			desc->addr = htole64(txsegs[i].ds_addr + hdrlen);
17894c7421aaSPyun YongHyeon 			desc->len = htole32(ALE_TX_BYTES(m->m_len - hdrlen) |
17904c7421aaSPyun YongHyeon 			    vtag);
17914c7421aaSPyun YongHyeon 			desc->flags = htole32(cflags);
17924c7421aaSPyun YongHyeon 			sc->ale_cdata.ale_tx_cnt++;
17934c7421aaSPyun YongHyeon 			ALE_DESC_INC(prod, ALE_TX_RING_CNT);
17944c7421aaSPyun YongHyeon 		}
17954c7421aaSPyun YongHyeon 		i = 1;
17964c7421aaSPyun YongHyeon 	}
17974c7421aaSPyun YongHyeon 	for (; i < nsegs; i++) {
17983c6e15bcSPyun YongHyeon 		desc = &sc->ale_cdata.ale_tx_ring[prod];
17993c6e15bcSPyun YongHyeon 		desc->addr = htole64(txsegs[i].ds_addr);
18003c6e15bcSPyun YongHyeon 		desc->len = htole32(ALE_TX_BYTES(txsegs[i].ds_len) | vtag);
18013c6e15bcSPyun YongHyeon 		desc->flags = htole32(cflags);
18023c6e15bcSPyun YongHyeon 		sc->ale_cdata.ale_tx_cnt++;
18033c6e15bcSPyun YongHyeon 		ALE_DESC_INC(prod, ALE_TX_RING_CNT);
18043c6e15bcSPyun YongHyeon 	}
18053c6e15bcSPyun YongHyeon 	/* Update producer index. */
18063c6e15bcSPyun YongHyeon 	sc->ale_cdata.ale_tx_prod = prod;
18073c6e15bcSPyun YongHyeon 	/* Set TSO header on the first descriptor. */
18083c6e15bcSPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
18093c6e15bcSPyun YongHyeon 		desc = &sc->ale_cdata.ale_tx_ring[si];
18103c6e15bcSPyun YongHyeon 		desc->flags |= htole32(ALE_TD_TSO_HDR);
18113c6e15bcSPyun YongHyeon 	}
18123c6e15bcSPyun YongHyeon 
18133c6e15bcSPyun YongHyeon 	/* Finally set EOP on the last descriptor. */
18143c6e15bcSPyun YongHyeon 	prod = (prod + ALE_TX_RING_CNT - 1) % ALE_TX_RING_CNT;
18153c6e15bcSPyun YongHyeon 	desc = &sc->ale_cdata.ale_tx_ring[prod];
18163c6e15bcSPyun YongHyeon 	desc->flags |= htole32(ALE_TD_EOP);
18173c6e15bcSPyun YongHyeon 
18183c6e15bcSPyun YongHyeon 	/* Swap dmamap of the first and the last. */
18193c6e15bcSPyun YongHyeon 	txd = &sc->ale_cdata.ale_txdesc[prod];
18203c6e15bcSPyun YongHyeon 	map = txd_last->tx_dmamap;
18213c6e15bcSPyun YongHyeon 	txd_last->tx_dmamap = txd->tx_dmamap;
18223c6e15bcSPyun YongHyeon 	txd->tx_dmamap = map;
18233c6e15bcSPyun YongHyeon 	txd->tx_m = m;
18243c6e15bcSPyun YongHyeon 
18253c6e15bcSPyun YongHyeon 	/* Sync descriptors. */
18263c6e15bcSPyun YongHyeon 	bus_dmamap_sync(sc->ale_cdata.ale_tx_ring_tag,
18273c6e15bcSPyun YongHyeon 	    sc->ale_cdata.ale_tx_ring_map,
18283c6e15bcSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
18293c6e15bcSPyun YongHyeon 
18303c6e15bcSPyun YongHyeon 	return (0);
18313c6e15bcSPyun YongHyeon }
18323c6e15bcSPyun YongHyeon 
18333c6e15bcSPyun YongHyeon static void
ale_start(if_t ifp)183452436412SJustin Hibbits ale_start(if_t ifp)
18353c6e15bcSPyun YongHyeon {
183632341ad6SJohn Baldwin         struct ale_softc *sc;
18373c6e15bcSPyun YongHyeon 
183852436412SJustin Hibbits 	sc = if_getsoftc(ifp);
183932341ad6SJohn Baldwin 	ALE_LOCK(sc);
184032341ad6SJohn Baldwin 	ale_start_locked(ifp);
184132341ad6SJohn Baldwin 	ALE_UNLOCK(sc);
18423c6e15bcSPyun YongHyeon }
18433c6e15bcSPyun YongHyeon 
18443c6e15bcSPyun YongHyeon static void
ale_start_locked(if_t ifp)184552436412SJustin Hibbits ale_start_locked(if_t ifp)
18463c6e15bcSPyun YongHyeon {
18473c6e15bcSPyun YongHyeon         struct ale_softc *sc;
18483c6e15bcSPyun YongHyeon         struct mbuf *m_head;
18493c6e15bcSPyun YongHyeon 	int enq;
18503c6e15bcSPyun YongHyeon 
185152436412SJustin Hibbits 	sc = if_getsoftc(ifp);
18523c6e15bcSPyun YongHyeon 
185332341ad6SJohn Baldwin 	ALE_LOCK_ASSERT(sc);
18543c6e15bcSPyun YongHyeon 
18553c6e15bcSPyun YongHyeon 	/* Reclaim transmitted frames. */
18563c6e15bcSPyun YongHyeon 	if (sc->ale_cdata.ale_tx_cnt >= ALE_TX_DESC_HIWAT)
18573c6e15bcSPyun YongHyeon 		ale_txeof(sc);
18583c6e15bcSPyun YongHyeon 
185952436412SJustin Hibbits 	if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
186032341ad6SJohn Baldwin 	    IFF_DRV_RUNNING || (sc->ale_flags & ALE_FLAG_LINK) == 0)
18613c6e15bcSPyun YongHyeon 		return;
18623c6e15bcSPyun YongHyeon 
186352436412SJustin Hibbits 	for (enq = 0; !if_sendq_empty(ifp); ) {
186452436412SJustin Hibbits 		m_head = if_dequeue(ifp);
18653c6e15bcSPyun YongHyeon 		if (m_head == NULL)
18663c6e15bcSPyun YongHyeon 			break;
18673c6e15bcSPyun YongHyeon 		/*
18683c6e15bcSPyun YongHyeon 		 * Pack the data into the transmit ring. If we
18693c6e15bcSPyun YongHyeon 		 * don't have room, set the OACTIVE flag and wait
18703c6e15bcSPyun YongHyeon 		 * for the NIC to drain the ring.
18713c6e15bcSPyun YongHyeon 		 */
18723c6e15bcSPyun YongHyeon 		if (ale_encap(sc, &m_head)) {
18733c6e15bcSPyun YongHyeon 			if (m_head == NULL)
18743c6e15bcSPyun YongHyeon 				break;
187552436412SJustin Hibbits 			if_sendq_prepend(ifp, m_head);
187652436412SJustin Hibbits 			if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
18773c6e15bcSPyun YongHyeon 			break;
18783c6e15bcSPyun YongHyeon 		}
18793c6e15bcSPyun YongHyeon 
18803c6e15bcSPyun YongHyeon 		enq++;
18813c6e15bcSPyun YongHyeon 		/*
18823c6e15bcSPyun YongHyeon 		 * If there's a BPF listener, bounce a copy of this frame
18833c6e15bcSPyun YongHyeon 		 * to him.
18843c6e15bcSPyun YongHyeon 		 */
18853c6e15bcSPyun YongHyeon 		ETHER_BPF_MTAP(ifp, m_head);
18863c6e15bcSPyun YongHyeon 	}
18873c6e15bcSPyun YongHyeon 
18883c6e15bcSPyun YongHyeon 	if (enq > 0) {
18893c6e15bcSPyun YongHyeon 		/* Kick. */
18903c6e15bcSPyun YongHyeon 		CSR_WRITE_4(sc, ALE_MBOX_TPD_PROD_IDX,
18913c6e15bcSPyun YongHyeon 		    sc->ale_cdata.ale_tx_prod);
18923c6e15bcSPyun YongHyeon 		/* Set a timeout in case the chip goes out to lunch. */
18933c6e15bcSPyun YongHyeon 		sc->ale_watchdog_timer = ALE_TX_TIMEOUT;
18943c6e15bcSPyun YongHyeon 	}
18953c6e15bcSPyun YongHyeon }
18963c6e15bcSPyun YongHyeon 
18973c6e15bcSPyun YongHyeon static void
ale_watchdog(struct ale_softc * sc)18983c6e15bcSPyun YongHyeon ale_watchdog(struct ale_softc *sc)
18993c6e15bcSPyun YongHyeon {
190052436412SJustin Hibbits 	if_t ifp;
19013c6e15bcSPyun YongHyeon 
19023c6e15bcSPyun YongHyeon 	ALE_LOCK_ASSERT(sc);
19033c6e15bcSPyun YongHyeon 
19043c6e15bcSPyun YongHyeon 	if (sc->ale_watchdog_timer == 0 || --sc->ale_watchdog_timer)
19053c6e15bcSPyun YongHyeon 		return;
19063c6e15bcSPyun YongHyeon 
19073c6e15bcSPyun YongHyeon 	ifp = sc->ale_ifp;
19083c6e15bcSPyun YongHyeon 	if ((sc->ale_flags & ALE_FLAG_LINK) == 0) {
19093c6e15bcSPyun YongHyeon 		if_printf(sc->ale_ifp, "watchdog timeout (lost link)\n");
191049998654SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
191152436412SJustin Hibbits 		if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
19123c6e15bcSPyun YongHyeon 		ale_init_locked(sc);
19133c6e15bcSPyun YongHyeon 		return;
19143c6e15bcSPyun YongHyeon 	}
19153c6e15bcSPyun YongHyeon 	if_printf(sc->ale_ifp, "watchdog timeout -- resetting\n");
191649998654SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
191752436412SJustin Hibbits 	if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
19183c6e15bcSPyun YongHyeon 	ale_init_locked(sc);
191952436412SJustin Hibbits 	if (!if_sendq_empty(ifp))
192032341ad6SJohn Baldwin 		ale_start_locked(ifp);
19213c6e15bcSPyun YongHyeon }
19223c6e15bcSPyun YongHyeon 
19233c6e15bcSPyun YongHyeon static int
ale_ioctl(if_t ifp,u_long cmd,caddr_t data)192452436412SJustin Hibbits ale_ioctl(if_t ifp, u_long cmd, caddr_t data)
19253c6e15bcSPyun YongHyeon {
19263c6e15bcSPyun YongHyeon 	struct ale_softc *sc;
19273c6e15bcSPyun YongHyeon 	struct ifreq *ifr;
19283c6e15bcSPyun YongHyeon 	struct mii_data *mii;
19293c6e15bcSPyun YongHyeon 	int error, mask;
19303c6e15bcSPyun YongHyeon 
193152436412SJustin Hibbits 	sc = if_getsoftc(ifp);
19323c6e15bcSPyun YongHyeon 	ifr = (struct ifreq *)data;
19333c6e15bcSPyun YongHyeon 	error = 0;
19343c6e15bcSPyun YongHyeon 	switch (cmd) {
19353c6e15bcSPyun YongHyeon 	case SIOCSIFMTU:
19363c6e15bcSPyun YongHyeon 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ALE_JUMBO_MTU ||
19373c6e15bcSPyun YongHyeon 		    ((sc->ale_flags & ALE_FLAG_JUMBO) == 0 &&
19383c6e15bcSPyun YongHyeon 		    ifr->ifr_mtu > ETHERMTU))
19393c6e15bcSPyun YongHyeon 			error = EINVAL;
194052436412SJustin Hibbits 		else if (if_getmtu(ifp) != ifr->ifr_mtu) {
19413c6e15bcSPyun YongHyeon 			ALE_LOCK(sc);
194252436412SJustin Hibbits 			if_setmtu(ifp, ifr->ifr_mtu);
194352436412SJustin Hibbits 			if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
194452436412SJustin Hibbits 				if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
19453c6e15bcSPyun YongHyeon 				ale_init_locked(sc);
19463c6e15bcSPyun YongHyeon 			}
19473c6e15bcSPyun YongHyeon 			ALE_UNLOCK(sc);
19483c6e15bcSPyun YongHyeon 		}
19493c6e15bcSPyun YongHyeon 		break;
19503c6e15bcSPyun YongHyeon 	case SIOCSIFFLAGS:
19513c6e15bcSPyun YongHyeon 		ALE_LOCK(sc);
195252436412SJustin Hibbits 		if ((if_getflags(ifp) & IFF_UP) != 0) {
195352436412SJustin Hibbits 			if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
195452436412SJustin Hibbits 				if (((if_getflags(ifp) ^ sc->ale_if_flags)
19553c6e15bcSPyun YongHyeon 				    & (IFF_PROMISC | IFF_ALLMULTI)) != 0)
19563c6e15bcSPyun YongHyeon 					ale_rxfilter(sc);
19573c6e15bcSPyun YongHyeon 			} else {
19583c6e15bcSPyun YongHyeon 				ale_init_locked(sc);
19593c6e15bcSPyun YongHyeon 			}
19603c6e15bcSPyun YongHyeon 		} else {
196152436412SJustin Hibbits 			if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)
19623c6e15bcSPyun YongHyeon 				ale_stop(sc);
19633c6e15bcSPyun YongHyeon 		}
196452436412SJustin Hibbits 		sc->ale_if_flags = if_getflags(ifp);
19653c6e15bcSPyun YongHyeon 		ALE_UNLOCK(sc);
19663c6e15bcSPyun YongHyeon 		break;
19673c6e15bcSPyun YongHyeon 	case SIOCADDMULTI:
19683c6e15bcSPyun YongHyeon 	case SIOCDELMULTI:
19693c6e15bcSPyun YongHyeon 		ALE_LOCK(sc);
197052436412SJustin Hibbits 		if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)
19713c6e15bcSPyun YongHyeon 			ale_rxfilter(sc);
19723c6e15bcSPyun YongHyeon 		ALE_UNLOCK(sc);
19733c6e15bcSPyun YongHyeon 		break;
19743c6e15bcSPyun YongHyeon 	case SIOCSIFMEDIA:
19753c6e15bcSPyun YongHyeon 	case SIOCGIFMEDIA:
19763c6e15bcSPyun YongHyeon 		mii = device_get_softc(sc->ale_miibus);
19773c6e15bcSPyun YongHyeon 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
19783c6e15bcSPyun YongHyeon 		break;
19793c6e15bcSPyun YongHyeon 	case SIOCSIFCAP:
19803c6e15bcSPyun YongHyeon 		ALE_LOCK(sc);
198152436412SJustin Hibbits 		mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
19823c6e15bcSPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
198352436412SJustin Hibbits 		    (if_getcapabilities(ifp) & IFCAP_TXCSUM) != 0) {
198452436412SJustin Hibbits 			if_togglecapenable(ifp, IFCAP_TXCSUM);
198552436412SJustin Hibbits 			if ((if_getcapenable(ifp) & IFCAP_TXCSUM) != 0)
198652436412SJustin Hibbits 				if_sethwassistbits(ifp, ALE_CSUM_FEATURES, 0);
19873c6e15bcSPyun YongHyeon 			else
198852436412SJustin Hibbits 				if_sethwassistbits(ifp, 0, ALE_CSUM_FEATURES);
19893c6e15bcSPyun YongHyeon 		}
19903c6e15bcSPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
199152436412SJustin Hibbits 		    (if_getcapabilities(ifp) & IFCAP_RXCSUM) != 0)
199252436412SJustin Hibbits 			if_togglecapenable(ifp, IFCAP_RXCSUM);
19933c6e15bcSPyun YongHyeon 		if ((mask & IFCAP_TSO4) != 0 &&
199452436412SJustin Hibbits 		    (if_getcapabilities(ifp) & IFCAP_TSO4) != 0) {
199552436412SJustin Hibbits 			if_togglecapenable(ifp, IFCAP_TSO4);
199652436412SJustin Hibbits 			if ((if_getcapenable(ifp) & IFCAP_TSO4) != 0)
199752436412SJustin Hibbits 				if_sethwassistbits(ifp, CSUM_TSO, 0);
19983c6e15bcSPyun YongHyeon 			else
199952436412SJustin Hibbits 				if_sethwassistbits(ifp, 0, CSUM_TSO);
20003c6e15bcSPyun YongHyeon 		}
20013c6e15bcSPyun YongHyeon 
20023c6e15bcSPyun YongHyeon 		if ((mask & IFCAP_WOL_MCAST) != 0 &&
200352436412SJustin Hibbits 		    (if_getcapabilities(ifp) & IFCAP_WOL_MCAST) != 0)
200452436412SJustin Hibbits 			if_togglecapenable(ifp, IFCAP_WOL_MCAST);
20053c6e15bcSPyun YongHyeon 		if ((mask & IFCAP_WOL_MAGIC) != 0 &&
200652436412SJustin Hibbits 		    (if_getcapabilities(ifp) & IFCAP_WOL_MAGIC) != 0)
200752436412SJustin Hibbits 			if_togglecapenable(ifp, IFCAP_WOL_MAGIC);
20083c6e15bcSPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWCSUM) != 0 &&
200952436412SJustin Hibbits 		    (if_getcapabilities(ifp) & IFCAP_VLAN_HWCSUM) != 0)
201052436412SJustin Hibbits 			if_togglecapenable(ifp, IFCAP_VLAN_HWCSUM);
20113c6e15bcSPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
201252436412SJustin Hibbits 		    (if_getcapabilities(ifp) & IFCAP_VLAN_HWTSO) != 0)
201352436412SJustin Hibbits 			if_togglecapenable(ifp, IFCAP_VLAN_HWTSO);
20145b8b73f6SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
201552436412SJustin Hibbits 		    (if_getcapabilities(ifp) & IFCAP_VLAN_HWTAGGING) != 0) {
201652436412SJustin Hibbits 			if_togglecapenable(ifp, IFCAP_VLAN_HWTAGGING);
201752436412SJustin Hibbits 			if ((if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) == 0)
201852436412SJustin Hibbits 				if_setcapenablebit(ifp, 0, IFCAP_VLAN_HWTSO);
20195b8b73f6SPyun YongHyeon 			ale_rxvlan(sc);
20205b8b73f6SPyun YongHyeon 		}
20213c6e15bcSPyun YongHyeon 		ALE_UNLOCK(sc);
20223c6e15bcSPyun YongHyeon 		VLAN_CAPABILITIES(ifp);
20233c6e15bcSPyun YongHyeon 		break;
20243c6e15bcSPyun YongHyeon 	default:
20253c6e15bcSPyun YongHyeon 		error = ether_ioctl(ifp, cmd, data);
20263c6e15bcSPyun YongHyeon 		break;
20273c6e15bcSPyun YongHyeon 	}
20283c6e15bcSPyun YongHyeon 
20293c6e15bcSPyun YongHyeon 	return (error);
20303c6e15bcSPyun YongHyeon }
20313c6e15bcSPyun YongHyeon 
20323c6e15bcSPyun YongHyeon static void
ale_mac_config(struct ale_softc * sc)20333c6e15bcSPyun YongHyeon ale_mac_config(struct ale_softc *sc)
20343c6e15bcSPyun YongHyeon {
20353c6e15bcSPyun YongHyeon 	struct mii_data *mii;
20363c6e15bcSPyun YongHyeon 	uint32_t reg;
20373c6e15bcSPyun YongHyeon 
20383c6e15bcSPyun YongHyeon 	ALE_LOCK_ASSERT(sc);
20393c6e15bcSPyun YongHyeon 
20403c6e15bcSPyun YongHyeon 	mii = device_get_softc(sc->ale_miibus);
20413c6e15bcSPyun YongHyeon 	reg = CSR_READ_4(sc, ALE_MAC_CFG);
20423c6e15bcSPyun YongHyeon 	reg &= ~(MAC_CFG_FULL_DUPLEX | MAC_CFG_TX_FC | MAC_CFG_RX_FC |
20433c6e15bcSPyun YongHyeon 	    MAC_CFG_SPEED_MASK);
20443c6e15bcSPyun YongHyeon 	/* Reprogram MAC with resolved speed/duplex. */
20453c6e15bcSPyun YongHyeon 	switch (IFM_SUBTYPE(mii->mii_media_active)) {
20463c6e15bcSPyun YongHyeon 	case IFM_10_T:
20473c6e15bcSPyun YongHyeon 	case IFM_100_TX:
20483c6e15bcSPyun YongHyeon 		reg |= MAC_CFG_SPEED_10_100;
20493c6e15bcSPyun YongHyeon 		break;
20503c6e15bcSPyun YongHyeon 	case IFM_1000_T:
20513c6e15bcSPyun YongHyeon 		reg |= MAC_CFG_SPEED_1000;
20523c6e15bcSPyun YongHyeon 		break;
20533c6e15bcSPyun YongHyeon 	}
20543c6e15bcSPyun YongHyeon 	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
20553c6e15bcSPyun YongHyeon 		reg |= MAC_CFG_FULL_DUPLEX;
20563c6e15bcSPyun YongHyeon 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
20573c6e15bcSPyun YongHyeon 			reg |= MAC_CFG_TX_FC;
20583c6e15bcSPyun YongHyeon 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
20593c6e15bcSPyun YongHyeon 			reg |= MAC_CFG_RX_FC;
20603c6e15bcSPyun YongHyeon 	}
20613c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_MAC_CFG, reg);
20623c6e15bcSPyun YongHyeon }
20633c6e15bcSPyun YongHyeon 
20643c6e15bcSPyun YongHyeon static void
ale_stats_clear(struct ale_softc * sc)20653c6e15bcSPyun YongHyeon ale_stats_clear(struct ale_softc *sc)
20663c6e15bcSPyun YongHyeon {
20673c6e15bcSPyun YongHyeon 	struct smb sb;
20683c6e15bcSPyun YongHyeon 	uint32_t *reg;
20693c6e15bcSPyun YongHyeon 	int i;
20703c6e15bcSPyun YongHyeon 
20713c6e15bcSPyun YongHyeon 	for (reg = &sb.rx_frames, i = 0; reg <= &sb.rx_pkts_filtered; reg++) {
20723c6e15bcSPyun YongHyeon 		CSR_READ_4(sc, ALE_RX_MIB_BASE + i);
20733c6e15bcSPyun YongHyeon 		i += sizeof(uint32_t);
20743c6e15bcSPyun YongHyeon 	}
20753c6e15bcSPyun YongHyeon 	/* Read Tx statistics. */
20763c6e15bcSPyun YongHyeon 	for (reg = &sb.tx_frames, i = 0; reg <= &sb.tx_mcast_bytes; reg++) {
20773c6e15bcSPyun YongHyeon 		CSR_READ_4(sc, ALE_TX_MIB_BASE + i);
20783c6e15bcSPyun YongHyeon 		i += sizeof(uint32_t);
20793c6e15bcSPyun YongHyeon 	}
20803c6e15bcSPyun YongHyeon }
20813c6e15bcSPyun YongHyeon 
20823c6e15bcSPyun YongHyeon static void
ale_stats_update(struct ale_softc * sc)20833c6e15bcSPyun YongHyeon ale_stats_update(struct ale_softc *sc)
20843c6e15bcSPyun YongHyeon {
20853c6e15bcSPyun YongHyeon 	struct ale_hw_stats *stat;
20863c6e15bcSPyun YongHyeon 	struct smb sb, *smb;
208752436412SJustin Hibbits 	if_t ifp;
20883c6e15bcSPyun YongHyeon 	uint32_t *reg;
20893c6e15bcSPyun YongHyeon 	int i;
20903c6e15bcSPyun YongHyeon 
20913c6e15bcSPyun YongHyeon 	ALE_LOCK_ASSERT(sc);
20923c6e15bcSPyun YongHyeon 
20933c6e15bcSPyun YongHyeon 	ifp = sc->ale_ifp;
20943c6e15bcSPyun YongHyeon 	stat = &sc->ale_stats;
20953c6e15bcSPyun YongHyeon 	smb = &sb;
20963c6e15bcSPyun YongHyeon 
20973c6e15bcSPyun YongHyeon 	/* Read Rx statistics. */
20983c6e15bcSPyun YongHyeon 	for (reg = &sb.rx_frames, i = 0; reg <= &sb.rx_pkts_filtered; reg++) {
20993c6e15bcSPyun YongHyeon 		*reg = CSR_READ_4(sc, ALE_RX_MIB_BASE + i);
21003c6e15bcSPyun YongHyeon 		i += sizeof(uint32_t);
21013c6e15bcSPyun YongHyeon 	}
21023c6e15bcSPyun YongHyeon 	/* Read Tx statistics. */
21033c6e15bcSPyun YongHyeon 	for (reg = &sb.tx_frames, i = 0; reg <= &sb.tx_mcast_bytes; reg++) {
21043c6e15bcSPyun YongHyeon 		*reg = CSR_READ_4(sc, ALE_TX_MIB_BASE + i);
21053c6e15bcSPyun YongHyeon 		i += sizeof(uint32_t);
21063c6e15bcSPyun YongHyeon 	}
21073c6e15bcSPyun YongHyeon 
21083c6e15bcSPyun YongHyeon 	/* Rx stats. */
21093c6e15bcSPyun YongHyeon 	stat->rx_frames += smb->rx_frames;
21103c6e15bcSPyun YongHyeon 	stat->rx_bcast_frames += smb->rx_bcast_frames;
21113c6e15bcSPyun YongHyeon 	stat->rx_mcast_frames += smb->rx_mcast_frames;
21123c6e15bcSPyun YongHyeon 	stat->rx_pause_frames += smb->rx_pause_frames;
21133c6e15bcSPyun YongHyeon 	stat->rx_control_frames += smb->rx_control_frames;
21143c6e15bcSPyun YongHyeon 	stat->rx_crcerrs += smb->rx_crcerrs;
21153c6e15bcSPyun YongHyeon 	stat->rx_lenerrs += smb->rx_lenerrs;
21163c6e15bcSPyun YongHyeon 	stat->rx_bytes += smb->rx_bytes;
21173c6e15bcSPyun YongHyeon 	stat->rx_runts += smb->rx_runts;
21183c6e15bcSPyun YongHyeon 	stat->rx_fragments += smb->rx_fragments;
21193c6e15bcSPyun YongHyeon 	stat->rx_pkts_64 += smb->rx_pkts_64;
21203c6e15bcSPyun YongHyeon 	stat->rx_pkts_65_127 += smb->rx_pkts_65_127;
21213c6e15bcSPyun YongHyeon 	stat->rx_pkts_128_255 += smb->rx_pkts_128_255;
21223c6e15bcSPyun YongHyeon 	stat->rx_pkts_256_511 += smb->rx_pkts_256_511;
21233c6e15bcSPyun YongHyeon 	stat->rx_pkts_512_1023 += smb->rx_pkts_512_1023;
21243c6e15bcSPyun YongHyeon 	stat->rx_pkts_1024_1518 += smb->rx_pkts_1024_1518;
21253c6e15bcSPyun YongHyeon 	stat->rx_pkts_1519_max += smb->rx_pkts_1519_max;
21263c6e15bcSPyun YongHyeon 	stat->rx_pkts_truncated += smb->rx_pkts_truncated;
21273c6e15bcSPyun YongHyeon 	stat->rx_fifo_oflows += smb->rx_fifo_oflows;
21283c6e15bcSPyun YongHyeon 	stat->rx_rrs_errs += smb->rx_rrs_errs;
21293c6e15bcSPyun YongHyeon 	stat->rx_alignerrs += smb->rx_alignerrs;
21303c6e15bcSPyun YongHyeon 	stat->rx_bcast_bytes += smb->rx_bcast_bytes;
21313c6e15bcSPyun YongHyeon 	stat->rx_mcast_bytes += smb->rx_mcast_bytes;
21323c6e15bcSPyun YongHyeon 	stat->rx_pkts_filtered += smb->rx_pkts_filtered;
21333c6e15bcSPyun YongHyeon 
21343c6e15bcSPyun YongHyeon 	/* Tx stats. */
21353c6e15bcSPyun YongHyeon 	stat->tx_frames += smb->tx_frames;
21363c6e15bcSPyun YongHyeon 	stat->tx_bcast_frames += smb->tx_bcast_frames;
21373c6e15bcSPyun YongHyeon 	stat->tx_mcast_frames += smb->tx_mcast_frames;
21383c6e15bcSPyun YongHyeon 	stat->tx_pause_frames += smb->tx_pause_frames;
21393c6e15bcSPyun YongHyeon 	stat->tx_excess_defer += smb->tx_excess_defer;
21403c6e15bcSPyun YongHyeon 	stat->tx_control_frames += smb->tx_control_frames;
21413c6e15bcSPyun YongHyeon 	stat->tx_deferred += smb->tx_deferred;
21423c6e15bcSPyun YongHyeon 	stat->tx_bytes += smb->tx_bytes;
21433c6e15bcSPyun YongHyeon 	stat->tx_pkts_64 += smb->tx_pkts_64;
21443c6e15bcSPyun YongHyeon 	stat->tx_pkts_65_127 += smb->tx_pkts_65_127;
21453c6e15bcSPyun YongHyeon 	stat->tx_pkts_128_255 += smb->tx_pkts_128_255;
21463c6e15bcSPyun YongHyeon 	stat->tx_pkts_256_511 += smb->tx_pkts_256_511;
21473c6e15bcSPyun YongHyeon 	stat->tx_pkts_512_1023 += smb->tx_pkts_512_1023;
21483c6e15bcSPyun YongHyeon 	stat->tx_pkts_1024_1518 += smb->tx_pkts_1024_1518;
21493c6e15bcSPyun YongHyeon 	stat->tx_pkts_1519_max += smb->tx_pkts_1519_max;
21503c6e15bcSPyun YongHyeon 	stat->tx_single_colls += smb->tx_single_colls;
21513c6e15bcSPyun YongHyeon 	stat->tx_multi_colls += smb->tx_multi_colls;
21523c6e15bcSPyun YongHyeon 	stat->tx_late_colls += smb->tx_late_colls;
21533c6e15bcSPyun YongHyeon 	stat->tx_excess_colls += smb->tx_excess_colls;
21543c6e15bcSPyun YongHyeon 	stat->tx_underrun += smb->tx_underrun;
21553c6e15bcSPyun YongHyeon 	stat->tx_desc_underrun += smb->tx_desc_underrun;
21563c6e15bcSPyun YongHyeon 	stat->tx_lenerrs += smb->tx_lenerrs;
21573c6e15bcSPyun YongHyeon 	stat->tx_pkts_truncated += smb->tx_pkts_truncated;
21583c6e15bcSPyun YongHyeon 	stat->tx_bcast_bytes += smb->tx_bcast_bytes;
21593c6e15bcSPyun YongHyeon 	stat->tx_mcast_bytes += smb->tx_mcast_bytes;
21603c6e15bcSPyun YongHyeon 
21613c6e15bcSPyun YongHyeon 	/* Update counters in ifnet. */
216249998654SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OPACKETS, smb->tx_frames);
21633c6e15bcSPyun YongHyeon 
216449998654SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_COLLISIONS, smb->tx_single_colls +
21653c6e15bcSPyun YongHyeon 	    smb->tx_multi_colls * 2 + smb->tx_late_colls +
21660999f75aSPyun YongHyeon 	    smb->tx_excess_colls * HDPX_CFG_RETRY_DEFAULT);
21673c6e15bcSPyun YongHyeon 
21680999f75aSPyun YongHyeon 	if_inc_counter(ifp, IFCOUNTER_OERRORS, smb->tx_late_colls +
21690999f75aSPyun YongHyeon 	    smb->tx_excess_colls + smb->tx_underrun + smb->tx_pkts_truncated);
21703c6e15bcSPyun YongHyeon 
217149998654SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, smb->rx_frames);
21723c6e15bcSPyun YongHyeon 
217349998654SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IERRORS,
217449998654SGleb Smirnoff 	    smb->rx_crcerrs + smb->rx_lenerrs +
21753c6e15bcSPyun YongHyeon 	    smb->rx_runts + smb->rx_pkts_truncated +
21763c6e15bcSPyun YongHyeon 	    smb->rx_fifo_oflows + smb->rx_rrs_errs +
217749998654SGleb Smirnoff 	    smb->rx_alignerrs);
21783c6e15bcSPyun YongHyeon }
21793c6e15bcSPyun YongHyeon 
21803c6e15bcSPyun YongHyeon static int
ale_intr(void * arg)21813c6e15bcSPyun YongHyeon ale_intr(void *arg)
21823c6e15bcSPyun YongHyeon {
21833c6e15bcSPyun YongHyeon 	struct ale_softc *sc;
21843c6e15bcSPyun YongHyeon 	uint32_t status;
21853c6e15bcSPyun YongHyeon 
21863c6e15bcSPyun YongHyeon 	sc = (struct ale_softc *)arg;
21873c6e15bcSPyun YongHyeon 
21883c6e15bcSPyun YongHyeon 	status = CSR_READ_4(sc, ALE_INTR_STATUS);
21893c6e15bcSPyun YongHyeon 	if ((status & ALE_INTRS) == 0)
21903c6e15bcSPyun YongHyeon 		return (FILTER_STRAY);
21913c6e15bcSPyun YongHyeon 	/* Disable interrupts. */
21923c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_INTR_STATUS, INTR_DIS_INT);
21933c6e15bcSPyun YongHyeon 	taskqueue_enqueue(sc->ale_tq, &sc->ale_int_task);
21943c6e15bcSPyun YongHyeon 
21953c6e15bcSPyun YongHyeon 	return (FILTER_HANDLED);
21963c6e15bcSPyun YongHyeon }
21973c6e15bcSPyun YongHyeon 
21983c6e15bcSPyun YongHyeon static void
ale_int_task(void * arg,int pending)21993c6e15bcSPyun YongHyeon ale_int_task(void *arg, int pending)
22003c6e15bcSPyun YongHyeon {
22013c6e15bcSPyun YongHyeon 	struct ale_softc *sc;
220252436412SJustin Hibbits 	if_t ifp;
22033c6e15bcSPyun YongHyeon 	uint32_t status;
22043c6e15bcSPyun YongHyeon 	int more;
22053c6e15bcSPyun YongHyeon 
22063c6e15bcSPyun YongHyeon 	sc = (struct ale_softc *)arg;
22073c6e15bcSPyun YongHyeon 
22083c6e15bcSPyun YongHyeon 	status = CSR_READ_4(sc, ALE_INTR_STATUS);
2209287e0d97SJohn Baldwin 	ALE_LOCK(sc);
2210e23559dbSPyun YongHyeon 	if (sc->ale_morework != 0)
22113c6e15bcSPyun YongHyeon 		status |= INTR_RX_PKT;
22123c6e15bcSPyun YongHyeon 	if ((status & ALE_INTRS) == 0)
22133c6e15bcSPyun YongHyeon 		goto done;
22143c6e15bcSPyun YongHyeon 
22153c6e15bcSPyun YongHyeon 	/* Acknowledge interrupts but still disable interrupts. */
22163c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_INTR_STATUS, status | INTR_DIS_INT);
22173c6e15bcSPyun YongHyeon 
22183c6e15bcSPyun YongHyeon 	ifp = sc->ale_ifp;
22193c6e15bcSPyun YongHyeon 	more = 0;
222052436412SJustin Hibbits 	if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
22213c6e15bcSPyun YongHyeon 		more = ale_rxeof(sc, sc->ale_process_limit);
22223c6e15bcSPyun YongHyeon 		if (more == EAGAIN)
2223e23559dbSPyun YongHyeon 			sc->ale_morework = 1;
22243c6e15bcSPyun YongHyeon 		else if (more == EIO) {
22253c6e15bcSPyun YongHyeon 			sc->ale_stats.reset_brk_seq++;
222652436412SJustin Hibbits 			if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
22273c6e15bcSPyun YongHyeon 			ale_init_locked(sc);
22283c6e15bcSPyun YongHyeon 			ALE_UNLOCK(sc);
22293c6e15bcSPyun YongHyeon 			return;
22303c6e15bcSPyun YongHyeon 		}
22313c6e15bcSPyun YongHyeon 
22323c6e15bcSPyun YongHyeon 		if ((status & (INTR_DMA_RD_TO_RST | INTR_DMA_WR_TO_RST)) != 0) {
22333c6e15bcSPyun YongHyeon 			if ((status & INTR_DMA_RD_TO_RST) != 0)
22343c6e15bcSPyun YongHyeon 				device_printf(sc->ale_dev,
22353c6e15bcSPyun YongHyeon 				    "DMA read error! -- resetting\n");
22363c6e15bcSPyun YongHyeon 			if ((status & INTR_DMA_WR_TO_RST) != 0)
22373c6e15bcSPyun YongHyeon 				device_printf(sc->ale_dev,
22383c6e15bcSPyun YongHyeon 				    "DMA write error! -- resetting\n");
223952436412SJustin Hibbits 			if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
22403c6e15bcSPyun YongHyeon 			ale_init_locked(sc);
22413c6e15bcSPyun YongHyeon 			ALE_UNLOCK(sc);
22423c6e15bcSPyun YongHyeon 			return;
22433c6e15bcSPyun YongHyeon 		}
224452436412SJustin Hibbits 		if (!if_sendq_empty(ifp))
224532341ad6SJohn Baldwin 			ale_start_locked(ifp);
22463c6e15bcSPyun YongHyeon 	}
22473c6e15bcSPyun YongHyeon 
22483c6e15bcSPyun YongHyeon 	if (more == EAGAIN ||
22493c6e15bcSPyun YongHyeon 	    (CSR_READ_4(sc, ALE_INTR_STATUS) & ALE_INTRS) != 0) {
2250287e0d97SJohn Baldwin 		ALE_UNLOCK(sc);
22513c6e15bcSPyun YongHyeon 		taskqueue_enqueue(sc->ale_tq, &sc->ale_int_task);
22523c6e15bcSPyun YongHyeon 		return;
22533c6e15bcSPyun YongHyeon 	}
22543c6e15bcSPyun YongHyeon 
22553c6e15bcSPyun YongHyeon done:
2256287e0d97SJohn Baldwin 	ALE_UNLOCK(sc);
2257287e0d97SJohn Baldwin 
22583c6e15bcSPyun YongHyeon 	/* Re-enable interrupts. */
22593c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_INTR_STATUS, 0x7FFFFFFF);
22603c6e15bcSPyun YongHyeon }
22613c6e15bcSPyun YongHyeon 
22623c6e15bcSPyun YongHyeon static void
ale_txeof(struct ale_softc * sc)22633c6e15bcSPyun YongHyeon ale_txeof(struct ale_softc *sc)
22643c6e15bcSPyun YongHyeon {
226552436412SJustin Hibbits 	if_t ifp;
22663c6e15bcSPyun YongHyeon 	struct ale_txdesc *txd;
22673c6e15bcSPyun YongHyeon 	uint32_t cons, prod;
22683c6e15bcSPyun YongHyeon 	int prog;
22693c6e15bcSPyun YongHyeon 
22703c6e15bcSPyun YongHyeon 	ALE_LOCK_ASSERT(sc);
22713c6e15bcSPyun YongHyeon 
22723c6e15bcSPyun YongHyeon 	ifp = sc->ale_ifp;
22733c6e15bcSPyun YongHyeon 
22743c6e15bcSPyun YongHyeon 	if (sc->ale_cdata.ale_tx_cnt == 0)
22753c6e15bcSPyun YongHyeon 		return;
22763c6e15bcSPyun YongHyeon 
22773c6e15bcSPyun YongHyeon 	bus_dmamap_sync(sc->ale_cdata.ale_tx_ring_tag,
22783c6e15bcSPyun YongHyeon 	    sc->ale_cdata.ale_tx_ring_map,
22793c6e15bcSPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
22803c6e15bcSPyun YongHyeon 	if ((sc->ale_flags & ALE_FLAG_TXCMB_BUG) == 0) {
22813c6e15bcSPyun YongHyeon 		bus_dmamap_sync(sc->ale_cdata.ale_tx_cmb_tag,
22823c6e15bcSPyun YongHyeon 		    sc->ale_cdata.ale_tx_cmb_map,
22833c6e15bcSPyun YongHyeon 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
22843c6e15bcSPyun YongHyeon 		prod = *sc->ale_cdata.ale_tx_cmb & TPD_CNT_MASK;
22853c6e15bcSPyun YongHyeon 	} else
22863c6e15bcSPyun YongHyeon 		prod = CSR_READ_2(sc, ALE_TPD_CONS_IDX);
22873c6e15bcSPyun YongHyeon 	cons = sc->ale_cdata.ale_tx_cons;
22883c6e15bcSPyun YongHyeon 	/*
22893c6e15bcSPyun YongHyeon 	 * Go through our Tx list and free mbufs for those
22903c6e15bcSPyun YongHyeon 	 * frames which have been transmitted.
22913c6e15bcSPyun YongHyeon 	 */
22923c6e15bcSPyun YongHyeon 	for (prog = 0; cons != prod; prog++,
22933c6e15bcSPyun YongHyeon 	    ALE_DESC_INC(cons, ALE_TX_RING_CNT)) {
22943c6e15bcSPyun YongHyeon 		if (sc->ale_cdata.ale_tx_cnt <= 0)
22953c6e15bcSPyun YongHyeon 			break;
22963c6e15bcSPyun YongHyeon 		prog++;
229752436412SJustin Hibbits 		if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
22983c6e15bcSPyun YongHyeon 		sc->ale_cdata.ale_tx_cnt--;
22993c6e15bcSPyun YongHyeon 		txd = &sc->ale_cdata.ale_txdesc[cons];
23003c6e15bcSPyun YongHyeon 		if (txd->tx_m != NULL) {
23013c6e15bcSPyun YongHyeon 			/* Reclaim transmitted mbufs. */
23023c6e15bcSPyun YongHyeon 			bus_dmamap_sync(sc->ale_cdata.ale_tx_tag,
23033c6e15bcSPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
23043c6e15bcSPyun YongHyeon 			bus_dmamap_unload(sc->ale_cdata.ale_tx_tag,
23053c6e15bcSPyun YongHyeon 			    txd->tx_dmamap);
23063c6e15bcSPyun YongHyeon 			m_freem(txd->tx_m);
23073c6e15bcSPyun YongHyeon 			txd->tx_m = NULL;
23083c6e15bcSPyun YongHyeon 		}
23093c6e15bcSPyun YongHyeon 	}
23103c6e15bcSPyun YongHyeon 
23113c6e15bcSPyun YongHyeon 	if (prog > 0) {
23123c6e15bcSPyun YongHyeon 		sc->ale_cdata.ale_tx_cons = cons;
23133c6e15bcSPyun YongHyeon 		/*
23143c6e15bcSPyun YongHyeon 		 * Unarm watchdog timer only when there is no pending
23153c6e15bcSPyun YongHyeon 		 * Tx descriptors in queue.
23163c6e15bcSPyun YongHyeon 		 */
23173c6e15bcSPyun YongHyeon 		if (sc->ale_cdata.ale_tx_cnt == 0)
23183c6e15bcSPyun YongHyeon 			sc->ale_watchdog_timer = 0;
23193c6e15bcSPyun YongHyeon 	}
23203c6e15bcSPyun YongHyeon }
23213c6e15bcSPyun YongHyeon 
23223c6e15bcSPyun YongHyeon static void
ale_rx_update_page(struct ale_softc * sc,struct ale_rx_page ** page,uint32_t length,uint32_t * prod)23233c6e15bcSPyun YongHyeon ale_rx_update_page(struct ale_softc *sc, struct ale_rx_page **page,
23243c6e15bcSPyun YongHyeon     uint32_t length, uint32_t *prod)
23253c6e15bcSPyun YongHyeon {
23263c6e15bcSPyun YongHyeon 	struct ale_rx_page *rx_page;
23273c6e15bcSPyun YongHyeon 
23283c6e15bcSPyun YongHyeon 	rx_page = *page;
23293c6e15bcSPyun YongHyeon 	/* Update consumer position. */
23303c6e15bcSPyun YongHyeon 	rx_page->cons += roundup(length + sizeof(struct rx_rs),
23313c6e15bcSPyun YongHyeon 	    ALE_RX_PAGE_ALIGN);
23323c6e15bcSPyun YongHyeon 	if (rx_page->cons >= ALE_RX_PAGE_SZ) {
23333c6e15bcSPyun YongHyeon 		/*
23343c6e15bcSPyun YongHyeon 		 * End of Rx page reached, let hardware reuse
23353c6e15bcSPyun YongHyeon 		 * this page.
23363c6e15bcSPyun YongHyeon 		 */
23373c6e15bcSPyun YongHyeon 		rx_page->cons = 0;
23383c6e15bcSPyun YongHyeon 		*rx_page->cmb_addr = 0;
23393c6e15bcSPyun YongHyeon 		bus_dmamap_sync(rx_page->cmb_tag, rx_page->cmb_map,
23403c6e15bcSPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
23413c6e15bcSPyun YongHyeon 		CSR_WRITE_1(sc, ALE_RXF0_PAGE0 + sc->ale_cdata.ale_rx_curp,
23423c6e15bcSPyun YongHyeon 		    RXF_VALID);
23433c6e15bcSPyun YongHyeon 		/* Switch to alternate Rx page. */
23443c6e15bcSPyun YongHyeon 		sc->ale_cdata.ale_rx_curp ^= 1;
23453c6e15bcSPyun YongHyeon 		rx_page = *page =
23463c6e15bcSPyun YongHyeon 		    &sc->ale_cdata.ale_rx_page[sc->ale_cdata.ale_rx_curp];
23473c6e15bcSPyun YongHyeon 		/* Page flipped, sync CMB and Rx page. */
23483c6e15bcSPyun YongHyeon 		bus_dmamap_sync(rx_page->page_tag, rx_page->page_map,
23493c6e15bcSPyun YongHyeon 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
23503c6e15bcSPyun YongHyeon 		bus_dmamap_sync(rx_page->cmb_tag, rx_page->cmb_map,
23513c6e15bcSPyun YongHyeon 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
23523c6e15bcSPyun YongHyeon 		/* Sync completed, cache updated producer index. */
23533c6e15bcSPyun YongHyeon 		*prod = *rx_page->cmb_addr;
23543c6e15bcSPyun YongHyeon 	}
23553c6e15bcSPyun YongHyeon }
23563c6e15bcSPyun YongHyeon 
23573c6e15bcSPyun YongHyeon /*
23583c6e15bcSPyun YongHyeon  * It seems that AR81xx controller can compute partial checksum.
23593c6e15bcSPyun YongHyeon  * The partial checksum value can be used to accelerate checksum
23603c6e15bcSPyun YongHyeon  * computation for fragmented TCP/UDP packets. Upper network stack
23613c6e15bcSPyun YongHyeon  * already takes advantage of the partial checksum value in IP
23623c6e15bcSPyun YongHyeon  * reassembly stage. But I'm not sure the correctness of the
23633c6e15bcSPyun YongHyeon  * partial hardware checksum assistance due to lack of data sheet.
23643c6e15bcSPyun YongHyeon  * In addition, the Rx feature of controller that requires copying
23653c6e15bcSPyun YongHyeon  * for every frames effectively nullifies one of most nice offload
23663c6e15bcSPyun YongHyeon  * capability of controller.
23673c6e15bcSPyun YongHyeon  */
23683c6e15bcSPyun YongHyeon static void
ale_rxcsum(struct ale_softc * sc,struct mbuf * m,uint32_t status)23693c6e15bcSPyun YongHyeon ale_rxcsum(struct ale_softc *sc, struct mbuf *m, uint32_t status)
23703c6e15bcSPyun YongHyeon {
237152436412SJustin Hibbits 	if_t ifp;
23723c6e15bcSPyun YongHyeon 	struct ip *ip;
23733c6e15bcSPyun YongHyeon 	char *p;
23743c6e15bcSPyun YongHyeon 
23753c6e15bcSPyun YongHyeon 	ifp = sc->ale_ifp;
23763c6e15bcSPyun YongHyeon 	m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
23773c6e15bcSPyun YongHyeon 	if ((status & ALE_RD_IPCSUM_NOK) == 0)
23783c6e15bcSPyun YongHyeon 		m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
23793c6e15bcSPyun YongHyeon 
23803c6e15bcSPyun YongHyeon 	if ((sc->ale_flags & ALE_FLAG_RXCSUM_BUG) == 0) {
23813c6e15bcSPyun YongHyeon 		if (((status & ALE_RD_IPV4_FRAG) == 0) &&
23823c6e15bcSPyun YongHyeon 		    ((status & (ALE_RD_TCP | ALE_RD_UDP)) != 0) &&
23833c6e15bcSPyun YongHyeon 		    ((status & ALE_RD_TCP_UDPCSUM_NOK) == 0)) {
23843c6e15bcSPyun YongHyeon 			m->m_pkthdr.csum_flags |=
23853c6e15bcSPyun YongHyeon 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
23863c6e15bcSPyun YongHyeon 			m->m_pkthdr.csum_data = 0xffff;
23873c6e15bcSPyun YongHyeon 		}
23883c6e15bcSPyun YongHyeon 	} else {
23893c6e15bcSPyun YongHyeon 		if ((status & (ALE_RD_TCP | ALE_RD_UDP)) != 0 &&
23903c6e15bcSPyun YongHyeon 		    (status & ALE_RD_TCP_UDPCSUM_NOK) == 0) {
23913c6e15bcSPyun YongHyeon 			p = mtod(m, char *);
23923c6e15bcSPyun YongHyeon 			p += ETHER_HDR_LEN;
23933c6e15bcSPyun YongHyeon 			if ((status & ALE_RD_802_3) != 0)
23943c6e15bcSPyun YongHyeon 				p += LLC_SNAPFRAMELEN;
239552436412SJustin Hibbits 			if ((if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) == 0 &&
23963c6e15bcSPyun YongHyeon 			    (status & ALE_RD_VLAN) != 0)
23973c6e15bcSPyun YongHyeon 				p += ETHER_VLAN_ENCAP_LEN;
23983c6e15bcSPyun YongHyeon 			ip = (struct ip *)p;
23993c6e15bcSPyun YongHyeon 			if (ip->ip_off != 0 && (status & ALE_RD_IPV4_DF) == 0)
24003c6e15bcSPyun YongHyeon 				return;
24013c6e15bcSPyun YongHyeon 			m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
24023c6e15bcSPyun YongHyeon 			    CSUM_PSEUDO_HDR;
24033c6e15bcSPyun YongHyeon 			m->m_pkthdr.csum_data = 0xffff;
24043c6e15bcSPyun YongHyeon 		}
24053c6e15bcSPyun YongHyeon 	}
24063c6e15bcSPyun YongHyeon 	/*
24073c6e15bcSPyun YongHyeon 	 * Don't mark bad checksum for TCP/UDP frames
24083c6e15bcSPyun YongHyeon 	 * as fragmented frames may always have set
24093c6e15bcSPyun YongHyeon 	 * bad checksummed bit of frame status.
24103c6e15bcSPyun YongHyeon 	 */
24113c6e15bcSPyun YongHyeon }
24123c6e15bcSPyun YongHyeon 
24133c6e15bcSPyun YongHyeon /* Process received frames. */
24143c6e15bcSPyun YongHyeon static int
ale_rxeof(struct ale_softc * sc,int count)24153c6e15bcSPyun YongHyeon ale_rxeof(struct ale_softc *sc, int count)
24163c6e15bcSPyun YongHyeon {
24173c6e15bcSPyun YongHyeon 	struct ale_rx_page *rx_page;
24183c6e15bcSPyun YongHyeon 	struct rx_rs *rs;
241952436412SJustin Hibbits 	if_t ifp;
24203c6e15bcSPyun YongHyeon 	struct mbuf *m;
24213c6e15bcSPyun YongHyeon 	uint32_t length, prod, seqno, status, vtags;
24223c6e15bcSPyun YongHyeon 	int prog;
24233c6e15bcSPyun YongHyeon 
24243c6e15bcSPyun YongHyeon 	ifp = sc->ale_ifp;
24253c6e15bcSPyun YongHyeon 	rx_page = &sc->ale_cdata.ale_rx_page[sc->ale_cdata.ale_rx_curp];
24263c6e15bcSPyun YongHyeon 	bus_dmamap_sync(rx_page->cmb_tag, rx_page->cmb_map,
24273c6e15bcSPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
24283c6e15bcSPyun YongHyeon 	bus_dmamap_sync(rx_page->page_tag, rx_page->page_map,
24293c6e15bcSPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
24303c6e15bcSPyun YongHyeon 	/*
24313c6e15bcSPyun YongHyeon 	 * Don't directly access producer index as hardware may
24323c6e15bcSPyun YongHyeon 	 * update it while Rx handler is in progress. It would
24333c6e15bcSPyun YongHyeon 	 * be even better if there is a way to let hardware
24343c6e15bcSPyun YongHyeon 	 * know how far driver processed its received frames.
24353c6e15bcSPyun YongHyeon 	 * Alternatively, hardware could provide a way to disable
24363c6e15bcSPyun YongHyeon 	 * CMB updates until driver acknowledges the end of CMB
24373c6e15bcSPyun YongHyeon 	 * access.
24383c6e15bcSPyun YongHyeon 	 */
24393c6e15bcSPyun YongHyeon 	prod = *rx_page->cmb_addr;
24403c6e15bcSPyun YongHyeon 	for (prog = 0; prog < count; prog++) {
24413c6e15bcSPyun YongHyeon 		if (rx_page->cons >= prod)
24423c6e15bcSPyun YongHyeon 			break;
24433c6e15bcSPyun YongHyeon 		rs = (struct rx_rs *)(rx_page->page_addr + rx_page->cons);
24443c6e15bcSPyun YongHyeon 		seqno = ALE_RX_SEQNO(le32toh(rs->seqno));
24453c6e15bcSPyun YongHyeon 		if (sc->ale_cdata.ale_rx_seqno != seqno) {
24463c6e15bcSPyun YongHyeon 			/*
24473c6e15bcSPyun YongHyeon 			 * Normally I believe this should not happen unless
24483c6e15bcSPyun YongHyeon 			 * severe driver bug or corrupted memory. However
24493c6e15bcSPyun YongHyeon 			 * it seems to happen under certain conditions which
24503c6e15bcSPyun YongHyeon 			 * is triggered by abrupt Rx events such as initiation
24513c6e15bcSPyun YongHyeon 			 * of bulk transfer of remote host. It's not easy to
24523c6e15bcSPyun YongHyeon 			 * reproduce this and I doubt it could be related
24533c6e15bcSPyun YongHyeon 			 * with FIFO overflow of hardware or activity of Tx
24543c6e15bcSPyun YongHyeon 			 * CMB updates. I also remember similar behaviour
24553c6e15bcSPyun YongHyeon 			 * seen on RealTek 8139 which uses resembling Rx
24563c6e15bcSPyun YongHyeon 			 * scheme.
24573c6e15bcSPyun YongHyeon 			 */
24583c6e15bcSPyun YongHyeon 			if (bootverbose)
24593c6e15bcSPyun YongHyeon 				device_printf(sc->ale_dev,
24603c6e15bcSPyun YongHyeon 				    "garbled seq: %u, expected: %u -- "
24613c6e15bcSPyun YongHyeon 				    "resetting!\n", seqno,
24623c6e15bcSPyun YongHyeon 				    sc->ale_cdata.ale_rx_seqno);
24633c6e15bcSPyun YongHyeon 			return (EIO);
24643c6e15bcSPyun YongHyeon 		}
24653c6e15bcSPyun YongHyeon 		/* Frame received. */
24663c6e15bcSPyun YongHyeon 		sc->ale_cdata.ale_rx_seqno++;
24673c6e15bcSPyun YongHyeon 		length = ALE_RX_BYTES(le32toh(rs->length));
24683c6e15bcSPyun YongHyeon 		status = le32toh(rs->flags);
24693c6e15bcSPyun YongHyeon 		if ((status & ALE_RD_ERROR) != 0) {
24703c6e15bcSPyun YongHyeon 			/*
24713c6e15bcSPyun YongHyeon 			 * We want to pass the following frames to upper
24723c6e15bcSPyun YongHyeon 			 * layer regardless of error status of Rx return
24733c6e15bcSPyun YongHyeon 			 * status.
24743c6e15bcSPyun YongHyeon 			 *
24753c6e15bcSPyun YongHyeon 			 *  o IP/TCP/UDP checksum is bad.
24763c6e15bcSPyun YongHyeon 			 *  o frame length and protocol specific length
24773c6e15bcSPyun YongHyeon 			 *     does not match.
24783c6e15bcSPyun YongHyeon 			 */
24793c6e15bcSPyun YongHyeon 			if ((status & (ALE_RD_CRC | ALE_RD_CODE |
24803c6e15bcSPyun YongHyeon 			    ALE_RD_DRIBBLE | ALE_RD_RUNT | ALE_RD_OFLOW |
24813c6e15bcSPyun YongHyeon 			    ALE_RD_TRUNC)) != 0) {
24823c6e15bcSPyun YongHyeon 				ale_rx_update_page(sc, &rx_page, length, &prod);
24833c6e15bcSPyun YongHyeon 				continue;
24843c6e15bcSPyun YongHyeon 			}
24853c6e15bcSPyun YongHyeon 		}
24863c6e15bcSPyun YongHyeon 		/*
24873c6e15bcSPyun YongHyeon 		 * m_devget(9) is major bottle-neck of ale(4)(It comes
24883c6e15bcSPyun YongHyeon 		 * from hardware limitation). For jumbo frames we could
24893c6e15bcSPyun YongHyeon 		 * get a slightly better performance if driver use
24903c6e15bcSPyun YongHyeon 		 * m_getjcl(9) with proper buffer size argument. However
24913c6e15bcSPyun YongHyeon 		 * that would make code more complicated and I don't
24923c6e15bcSPyun YongHyeon 		 * think users would expect good Rx performance numbers
24933c6e15bcSPyun YongHyeon 		 * on these low-end consumer ethernet controller.
24943c6e15bcSPyun YongHyeon 		 */
24953c6e15bcSPyun YongHyeon 		m = m_devget((char *)(rs + 1), length - ETHER_CRC_LEN,
24963c6e15bcSPyun YongHyeon 		    ETHER_ALIGN, ifp, NULL);
24973c6e15bcSPyun YongHyeon 		if (m == NULL) {
249849998654SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
24993c6e15bcSPyun YongHyeon 			ale_rx_update_page(sc, &rx_page, length, &prod);
25003c6e15bcSPyun YongHyeon 			continue;
25013c6e15bcSPyun YongHyeon 		}
250252436412SJustin Hibbits 		if ((if_getcapenable(ifp) & IFCAP_RXCSUM) != 0 &&
25033c6e15bcSPyun YongHyeon 		    (status & ALE_RD_IPV4) != 0)
25043c6e15bcSPyun YongHyeon 			ale_rxcsum(sc, m, status);
250552436412SJustin Hibbits 		if ((if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) != 0 &&
25063c6e15bcSPyun YongHyeon 		    (status & ALE_RD_VLAN) != 0) {
25073c6e15bcSPyun YongHyeon 			vtags = ALE_RX_VLAN(le32toh(rs->vtags));
25083c6e15bcSPyun YongHyeon 			m->m_pkthdr.ether_vtag = ALE_RX_VLAN_TAG(vtags);
25093c6e15bcSPyun YongHyeon 			m->m_flags |= M_VLANTAG;
25103c6e15bcSPyun YongHyeon 		}
25113c6e15bcSPyun YongHyeon 
25123c6e15bcSPyun YongHyeon 		/* Pass it to upper layer. */
2513287e0d97SJohn Baldwin 		ALE_UNLOCK(sc);
251452436412SJustin Hibbits 		if_input(ifp, m);
2515287e0d97SJohn Baldwin 		ALE_LOCK(sc);
25163c6e15bcSPyun YongHyeon 
25173c6e15bcSPyun YongHyeon 		ale_rx_update_page(sc, &rx_page, length, &prod);
25183c6e15bcSPyun YongHyeon 	}
25193c6e15bcSPyun YongHyeon 
25203c6e15bcSPyun YongHyeon 	return (count > 0 ? 0 : EAGAIN);
25213c6e15bcSPyun YongHyeon }
25223c6e15bcSPyun YongHyeon 
25233c6e15bcSPyun YongHyeon static void
ale_tick(void * arg)25243c6e15bcSPyun YongHyeon ale_tick(void *arg)
25253c6e15bcSPyun YongHyeon {
25263c6e15bcSPyun YongHyeon 	struct ale_softc *sc;
25273c6e15bcSPyun YongHyeon 	struct mii_data *mii;
25283c6e15bcSPyun YongHyeon 
25293c6e15bcSPyun YongHyeon 	sc = (struct ale_softc *)arg;
25303c6e15bcSPyun YongHyeon 
25313c6e15bcSPyun YongHyeon 	ALE_LOCK_ASSERT(sc);
25323c6e15bcSPyun YongHyeon 
25333c6e15bcSPyun YongHyeon 	mii = device_get_softc(sc->ale_miibus);
25343c6e15bcSPyun YongHyeon 	mii_tick(mii);
25353c6e15bcSPyun YongHyeon 	ale_stats_update(sc);
25363c6e15bcSPyun YongHyeon 	/*
25373c6e15bcSPyun YongHyeon 	 * Reclaim Tx buffers that have been transferred. It's not
25383c6e15bcSPyun YongHyeon 	 * needed here but it would release allocated mbuf chains
25393c6e15bcSPyun YongHyeon 	 * faster and limit the maximum delay to a hz.
25403c6e15bcSPyun YongHyeon 	 */
25413c6e15bcSPyun YongHyeon 	ale_txeof(sc);
25423c6e15bcSPyun YongHyeon 	ale_watchdog(sc);
25433c6e15bcSPyun YongHyeon 	callout_reset(&sc->ale_tick_ch, hz, ale_tick, sc);
25443c6e15bcSPyun YongHyeon }
25453c6e15bcSPyun YongHyeon 
25463c6e15bcSPyun YongHyeon static void
ale_reset(struct ale_softc * sc)25473c6e15bcSPyun YongHyeon ale_reset(struct ale_softc *sc)
25483c6e15bcSPyun YongHyeon {
25493c6e15bcSPyun YongHyeon 	uint32_t reg;
25503c6e15bcSPyun YongHyeon 	int i;
25513c6e15bcSPyun YongHyeon 
25523c6e15bcSPyun YongHyeon 	/* Initialize PCIe module. From Linux. */
25533c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, 0x1008, CSR_READ_4(sc, 0x1008) | 0x8000);
25543c6e15bcSPyun YongHyeon 
25553c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_MASTER_CFG, MASTER_RESET);
25563c6e15bcSPyun YongHyeon 	for (i = ALE_RESET_TIMEOUT; i > 0; i--) {
25573c6e15bcSPyun YongHyeon 		DELAY(10);
25583c6e15bcSPyun YongHyeon 		if ((CSR_READ_4(sc, ALE_MASTER_CFG) & MASTER_RESET) == 0)
25593c6e15bcSPyun YongHyeon 			break;
25603c6e15bcSPyun YongHyeon 	}
25613c6e15bcSPyun YongHyeon 	if (i == 0)
25623c6e15bcSPyun YongHyeon 		device_printf(sc->ale_dev, "master reset timeout!\n");
25633c6e15bcSPyun YongHyeon 
25643c6e15bcSPyun YongHyeon 	for (i = ALE_RESET_TIMEOUT; i > 0; i--) {
25653c6e15bcSPyun YongHyeon 		if ((reg = CSR_READ_4(sc, ALE_IDLE_STATUS)) == 0)
25663c6e15bcSPyun YongHyeon 			break;
25673c6e15bcSPyun YongHyeon 		DELAY(10);
25683c6e15bcSPyun YongHyeon 	}
25693c6e15bcSPyun YongHyeon 
25703c6e15bcSPyun YongHyeon 	if (i == 0)
25713c6e15bcSPyun YongHyeon 		device_printf(sc->ale_dev, "reset timeout(0x%08x)!\n", reg);
25723c6e15bcSPyun YongHyeon }
25733c6e15bcSPyun YongHyeon 
25743c6e15bcSPyun YongHyeon static void
ale_init(void * xsc)25753c6e15bcSPyun YongHyeon ale_init(void *xsc)
25763c6e15bcSPyun YongHyeon {
25773c6e15bcSPyun YongHyeon 	struct ale_softc *sc;
25783c6e15bcSPyun YongHyeon 
25793c6e15bcSPyun YongHyeon 	sc = (struct ale_softc *)xsc;
25803c6e15bcSPyun YongHyeon 	ALE_LOCK(sc);
25813c6e15bcSPyun YongHyeon 	ale_init_locked(sc);
25823c6e15bcSPyun YongHyeon 	ALE_UNLOCK(sc);
25833c6e15bcSPyun YongHyeon }
25843c6e15bcSPyun YongHyeon 
25853c6e15bcSPyun YongHyeon static void
ale_init_locked(struct ale_softc * sc)25863c6e15bcSPyun YongHyeon ale_init_locked(struct ale_softc *sc)
25873c6e15bcSPyun YongHyeon {
258852436412SJustin Hibbits 	if_t ifp;
25893c6e15bcSPyun YongHyeon 	struct mii_data *mii;
25903c6e15bcSPyun YongHyeon 	uint8_t eaddr[ETHER_ADDR_LEN];
25913c6e15bcSPyun YongHyeon 	bus_addr_t paddr;
25923c6e15bcSPyun YongHyeon 	uint32_t reg, rxf_hi, rxf_lo;
25933c6e15bcSPyun YongHyeon 
25943c6e15bcSPyun YongHyeon 	ALE_LOCK_ASSERT(sc);
25953c6e15bcSPyun YongHyeon 
25963c6e15bcSPyun YongHyeon 	ifp = sc->ale_ifp;
25973c6e15bcSPyun YongHyeon 	mii = device_get_softc(sc->ale_miibus);
25983c6e15bcSPyun YongHyeon 
259952436412SJustin Hibbits 	if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)
26003c6e15bcSPyun YongHyeon 		return;
26013c6e15bcSPyun YongHyeon 	/*
26023c6e15bcSPyun YongHyeon 	 * Cancel any pending I/O.
26033c6e15bcSPyun YongHyeon 	 */
26043c6e15bcSPyun YongHyeon 	ale_stop(sc);
26053c6e15bcSPyun YongHyeon 	/*
26063c6e15bcSPyun YongHyeon 	 * Reset the chip to a known state.
26073c6e15bcSPyun YongHyeon 	 */
26083c6e15bcSPyun YongHyeon 	ale_reset(sc);
26093c6e15bcSPyun YongHyeon 	/* Initialize Tx descriptors, DMA memory blocks. */
26103c6e15bcSPyun YongHyeon 	ale_init_rx_pages(sc);
26113c6e15bcSPyun YongHyeon 	ale_init_tx_ring(sc);
26123c6e15bcSPyun YongHyeon 
26133c6e15bcSPyun YongHyeon 	/* Reprogram the station address. */
261452436412SJustin Hibbits 	bcopy(if_getlladdr(ifp), eaddr, ETHER_ADDR_LEN);
26153c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_PAR0,
26163c6e15bcSPyun YongHyeon 	    eaddr[2] << 24 | eaddr[3] << 16 | eaddr[4] << 8 | eaddr[5]);
26173c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_PAR1, eaddr[0] << 8 | eaddr[1]);
26183c6e15bcSPyun YongHyeon 	/*
26193c6e15bcSPyun YongHyeon 	 * Clear WOL status and disable all WOL feature as WOL
26203c6e15bcSPyun YongHyeon 	 * would interfere Rx operation under normal environments.
26213c6e15bcSPyun YongHyeon 	 */
26223c6e15bcSPyun YongHyeon 	CSR_READ_4(sc, ALE_WOL_CFG);
26233c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_WOL_CFG, 0);
26243c6e15bcSPyun YongHyeon 	/*
26253c6e15bcSPyun YongHyeon 	 * Set Tx descriptor/RXF0/CMB base addresses. They share
26263c6e15bcSPyun YongHyeon 	 * the same high address part of DMAable region.
26273c6e15bcSPyun YongHyeon 	 */
26283c6e15bcSPyun YongHyeon 	paddr = sc->ale_cdata.ale_tx_ring_paddr;
26293c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_TPD_ADDR_HI, ALE_ADDR_HI(paddr));
26303c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_TPD_ADDR_LO, ALE_ADDR_LO(paddr));
26313c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_TPD_CNT,
26323c6e15bcSPyun YongHyeon 	    (ALE_TX_RING_CNT << TPD_CNT_SHIFT) & TPD_CNT_MASK);
26333c6e15bcSPyun YongHyeon 	/* Set Rx page base address, note we use single queue. */
26343c6e15bcSPyun YongHyeon 	paddr = sc->ale_cdata.ale_rx_page[0].page_paddr;
26353c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_RXF0_PAGE0_ADDR_LO, ALE_ADDR_LO(paddr));
26363c6e15bcSPyun YongHyeon 	paddr = sc->ale_cdata.ale_rx_page[1].page_paddr;
26373c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_RXF0_PAGE1_ADDR_LO, ALE_ADDR_LO(paddr));
26383c6e15bcSPyun YongHyeon 	/* Set Tx/Rx CMB addresses. */
26393c6e15bcSPyun YongHyeon 	paddr = sc->ale_cdata.ale_tx_cmb_paddr;
26403c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_TX_CMB_ADDR_LO, ALE_ADDR_LO(paddr));
26413c6e15bcSPyun YongHyeon 	paddr = sc->ale_cdata.ale_rx_page[0].cmb_paddr;
26423c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_RXF0_CMB0_ADDR_LO, ALE_ADDR_LO(paddr));
26433c6e15bcSPyun YongHyeon 	paddr = sc->ale_cdata.ale_rx_page[1].cmb_paddr;
26443c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_RXF0_CMB1_ADDR_LO, ALE_ADDR_LO(paddr));
26453c6e15bcSPyun YongHyeon 	/* Mark RXF0 is valid. */
26463c6e15bcSPyun YongHyeon 	CSR_WRITE_1(sc, ALE_RXF0_PAGE0, RXF_VALID);
26473c6e15bcSPyun YongHyeon 	CSR_WRITE_1(sc, ALE_RXF0_PAGE1, RXF_VALID);
26483c6e15bcSPyun YongHyeon 	/*
26493c6e15bcSPyun YongHyeon 	 * No need to initialize RFX1/RXF2/RXF3. We don't use
26503c6e15bcSPyun YongHyeon 	 * multi-queue yet.
26513c6e15bcSPyun YongHyeon 	 */
26523c6e15bcSPyun YongHyeon 
26533c6e15bcSPyun YongHyeon 	/* Set Rx page size, excluding guard frame size. */
26543c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_RXF_PAGE_SIZE, ALE_RX_PAGE_SZ);
26553c6e15bcSPyun YongHyeon 	/* Tell hardware that we're ready to load DMA blocks. */
26563c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_DMA_BLOCK, DMA_BLOCK_LOAD);
26573c6e15bcSPyun YongHyeon 
26583c6e15bcSPyun YongHyeon 	/* Set Rx/Tx interrupt trigger threshold. */
26593c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_INT_TRIG_THRESH, (1 << INT_TRIG_RX_THRESH_SHIFT) |
26603c6e15bcSPyun YongHyeon 	    (4 << INT_TRIG_TX_THRESH_SHIFT));
26613c6e15bcSPyun YongHyeon 	/*
26623c6e15bcSPyun YongHyeon 	 * XXX
26633c6e15bcSPyun YongHyeon 	 * Set interrupt trigger timer, its purpose and relation
26643c6e15bcSPyun YongHyeon 	 * with interrupt moderation mechanism is not clear yet.
26653c6e15bcSPyun YongHyeon 	 */
26663c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_INT_TRIG_TIMER,
26673c6e15bcSPyun YongHyeon 	    ((ALE_USECS(10) << INT_TRIG_RX_TIMER_SHIFT) |
26683c6e15bcSPyun YongHyeon 	    (ALE_USECS(1000) << INT_TRIG_TX_TIMER_SHIFT)));
26693c6e15bcSPyun YongHyeon 
26703c6e15bcSPyun YongHyeon 	/* Configure interrupt moderation timer. */
26713c6e15bcSPyun YongHyeon 	reg = ALE_USECS(sc->ale_int_rx_mod) << IM_TIMER_RX_SHIFT;
26723c6e15bcSPyun YongHyeon 	reg |= ALE_USECS(sc->ale_int_tx_mod) << IM_TIMER_TX_SHIFT;
26733c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_IM_TIMER, reg);
26743c6e15bcSPyun YongHyeon 	reg = CSR_READ_4(sc, ALE_MASTER_CFG);
26753c6e15bcSPyun YongHyeon 	reg &= ~(MASTER_CHIP_REV_MASK | MASTER_CHIP_ID_MASK);
26763c6e15bcSPyun YongHyeon 	reg &= ~(MASTER_IM_RX_TIMER_ENB | MASTER_IM_TX_TIMER_ENB);
26773c6e15bcSPyun YongHyeon 	if (ALE_USECS(sc->ale_int_rx_mod) != 0)
26783c6e15bcSPyun YongHyeon 		reg |= MASTER_IM_RX_TIMER_ENB;
26793c6e15bcSPyun YongHyeon 	if (ALE_USECS(sc->ale_int_tx_mod) != 0)
26803c6e15bcSPyun YongHyeon 		reg |= MASTER_IM_TX_TIMER_ENB;
26813c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_MASTER_CFG, reg);
26823c6e15bcSPyun YongHyeon 	CSR_WRITE_2(sc, ALE_INTR_CLR_TIMER, ALE_USECS(1000));
26833c6e15bcSPyun YongHyeon 
26843c6e15bcSPyun YongHyeon 	/* Set Maximum frame size of controller. */
268552436412SJustin Hibbits 	if (if_getmtu(ifp) < ETHERMTU)
26863c6e15bcSPyun YongHyeon 		sc->ale_max_frame_size = ETHERMTU;
26873c6e15bcSPyun YongHyeon 	else
268852436412SJustin Hibbits 		sc->ale_max_frame_size = if_getmtu(ifp);
26893c6e15bcSPyun YongHyeon 	sc->ale_max_frame_size += ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN +
26903c6e15bcSPyun YongHyeon 	    ETHER_CRC_LEN;
26913c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_FRAME_SIZE, sc->ale_max_frame_size);
26923c6e15bcSPyun YongHyeon 	/* Configure IPG/IFG parameters. */
26933c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_IPG_IFG_CFG,
26943c6e15bcSPyun YongHyeon 	    ((IPG_IFG_IPGT_DEFAULT << IPG_IFG_IPGT_SHIFT) & IPG_IFG_IPGT_MASK) |
26953c6e15bcSPyun YongHyeon 	    ((IPG_IFG_MIFG_DEFAULT << IPG_IFG_MIFG_SHIFT) & IPG_IFG_MIFG_MASK) |
26963c6e15bcSPyun YongHyeon 	    ((IPG_IFG_IPG1_DEFAULT << IPG_IFG_IPG1_SHIFT) & IPG_IFG_IPG1_MASK) |
26973c6e15bcSPyun YongHyeon 	    ((IPG_IFG_IPG2_DEFAULT << IPG_IFG_IPG2_SHIFT) & IPG_IFG_IPG2_MASK));
26983c6e15bcSPyun YongHyeon 	/* Set parameters for half-duplex media. */
26993c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_HDPX_CFG,
27003c6e15bcSPyun YongHyeon 	    ((HDPX_CFG_LCOL_DEFAULT << HDPX_CFG_LCOL_SHIFT) &
27013c6e15bcSPyun YongHyeon 	    HDPX_CFG_LCOL_MASK) |
27023c6e15bcSPyun YongHyeon 	    ((HDPX_CFG_RETRY_DEFAULT << HDPX_CFG_RETRY_SHIFT) &
27033c6e15bcSPyun YongHyeon 	    HDPX_CFG_RETRY_MASK) | HDPX_CFG_EXC_DEF_EN |
27043c6e15bcSPyun YongHyeon 	    ((HDPX_CFG_ABEBT_DEFAULT << HDPX_CFG_ABEBT_SHIFT) &
27053c6e15bcSPyun YongHyeon 	    HDPX_CFG_ABEBT_MASK) |
27063c6e15bcSPyun YongHyeon 	    ((HDPX_CFG_JAMIPG_DEFAULT << HDPX_CFG_JAMIPG_SHIFT) &
27073c6e15bcSPyun YongHyeon 	    HDPX_CFG_JAMIPG_MASK));
27083c6e15bcSPyun YongHyeon 
27093c6e15bcSPyun YongHyeon 	/* Configure Tx jumbo frame parameters. */
27103c6e15bcSPyun YongHyeon 	if ((sc->ale_flags & ALE_FLAG_JUMBO) != 0) {
271152436412SJustin Hibbits 		if (if_getmtu(ifp) < ETHERMTU)
27123c6e15bcSPyun YongHyeon 			reg = sc->ale_max_frame_size;
271352436412SJustin Hibbits 		else if (if_getmtu(ifp) < 6 * 1024)
27143c6e15bcSPyun YongHyeon 			reg = (sc->ale_max_frame_size * 2) / 3;
27153c6e15bcSPyun YongHyeon 		else
27163c6e15bcSPyun YongHyeon 			reg = sc->ale_max_frame_size / 2;
27173c6e15bcSPyun YongHyeon 		CSR_WRITE_4(sc, ALE_TX_JUMBO_THRESH,
27183c6e15bcSPyun YongHyeon 		    roundup(reg, TX_JUMBO_THRESH_UNIT) >>
27193c6e15bcSPyun YongHyeon 		    TX_JUMBO_THRESH_UNIT_SHIFT);
27203c6e15bcSPyun YongHyeon 	}
27213c6e15bcSPyun YongHyeon 	/* Configure TxQ. */
27223c6e15bcSPyun YongHyeon 	reg = (128 << (sc->ale_dma_rd_burst >> DMA_CFG_RD_BURST_SHIFT))
27233c6e15bcSPyun YongHyeon 	    << TXQ_CFG_TX_FIFO_BURST_SHIFT;
27243c6e15bcSPyun YongHyeon 	reg |= (TXQ_CFG_TPD_BURST_DEFAULT << TXQ_CFG_TPD_BURST_SHIFT) &
27253c6e15bcSPyun YongHyeon 	    TXQ_CFG_TPD_BURST_MASK;
27263c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_TXQ_CFG, reg | TXQ_CFG_ENHANCED_MODE | TXQ_CFG_ENB);
27273c6e15bcSPyun YongHyeon 
27283c6e15bcSPyun YongHyeon 	/* Configure Rx jumbo frame & flow control parameters. */
27293c6e15bcSPyun YongHyeon 	if ((sc->ale_flags & ALE_FLAG_JUMBO) != 0) {
27303c6e15bcSPyun YongHyeon 		reg = roundup(sc->ale_max_frame_size, RX_JUMBO_THRESH_UNIT);
27313c6e15bcSPyun YongHyeon 		CSR_WRITE_4(sc, ALE_RX_JUMBO_THRESH,
27323c6e15bcSPyun YongHyeon 		    (((reg >> RX_JUMBO_THRESH_UNIT_SHIFT) <<
27333c6e15bcSPyun YongHyeon 		    RX_JUMBO_THRESH_MASK_SHIFT) & RX_JUMBO_THRESH_MASK) |
27343c6e15bcSPyun YongHyeon 		    ((RX_JUMBO_LKAH_DEFAULT << RX_JUMBO_LKAH_SHIFT) &
27353c6e15bcSPyun YongHyeon 		    RX_JUMBO_LKAH_MASK));
27363c6e15bcSPyun YongHyeon 		reg = CSR_READ_4(sc, ALE_SRAM_RX_FIFO_LEN);
27373c6e15bcSPyun YongHyeon 		rxf_hi = (reg * 7) / 10;
27383c6e15bcSPyun YongHyeon 		rxf_lo = (reg * 3)/ 10;
27393c6e15bcSPyun YongHyeon 		CSR_WRITE_4(sc, ALE_RX_FIFO_PAUSE_THRESH,
27403c6e15bcSPyun YongHyeon 		    ((rxf_lo << RX_FIFO_PAUSE_THRESH_LO_SHIFT) &
27413c6e15bcSPyun YongHyeon 		    RX_FIFO_PAUSE_THRESH_LO_MASK) |
27423c6e15bcSPyun YongHyeon 		    ((rxf_hi << RX_FIFO_PAUSE_THRESH_HI_SHIFT) &
27433c6e15bcSPyun YongHyeon 		    RX_FIFO_PAUSE_THRESH_HI_MASK));
27443c6e15bcSPyun YongHyeon 	}
27453c6e15bcSPyun YongHyeon 
27463c6e15bcSPyun YongHyeon 	/* Disable RSS. */
27473c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_RSS_IDT_TABLE0, 0);
27483c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_RSS_CPU, 0);
27493c6e15bcSPyun YongHyeon 
27503c6e15bcSPyun YongHyeon 	/* Configure RxQ. */
27513c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_RXQ_CFG,
27523c6e15bcSPyun YongHyeon 	    RXQ_CFG_ALIGN_32 | RXQ_CFG_CUT_THROUGH_ENB | RXQ_CFG_ENB);
27533c6e15bcSPyun YongHyeon 
27543c6e15bcSPyun YongHyeon 	/* Configure DMA parameters. */
27553c6e15bcSPyun YongHyeon 	reg = 0;
27563c6e15bcSPyun YongHyeon 	if ((sc->ale_flags & ALE_FLAG_TXCMB_BUG) == 0)
27573c6e15bcSPyun YongHyeon 		reg |= DMA_CFG_TXCMB_ENB;
27583c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_DMA_CFG,
27593c6e15bcSPyun YongHyeon 	    DMA_CFG_OUT_ORDER | DMA_CFG_RD_REQ_PRI | DMA_CFG_RCB_64 |
27603c6e15bcSPyun YongHyeon 	    sc->ale_dma_rd_burst | reg |
27613c6e15bcSPyun YongHyeon 	    sc->ale_dma_wr_burst | DMA_CFG_RXCMB_ENB |
27623c6e15bcSPyun YongHyeon 	    ((DMA_CFG_RD_DELAY_CNT_DEFAULT << DMA_CFG_RD_DELAY_CNT_SHIFT) &
27633c6e15bcSPyun YongHyeon 	    DMA_CFG_RD_DELAY_CNT_MASK) |
27643c6e15bcSPyun YongHyeon 	    ((DMA_CFG_WR_DELAY_CNT_DEFAULT << DMA_CFG_WR_DELAY_CNT_SHIFT) &
27653c6e15bcSPyun YongHyeon 	    DMA_CFG_WR_DELAY_CNT_MASK));
27663c6e15bcSPyun YongHyeon 
27673c6e15bcSPyun YongHyeon 	/*
27683c6e15bcSPyun YongHyeon 	 * Hardware can be configured to issue SMB interrupt based
27693c6e15bcSPyun YongHyeon 	 * on programmed interval. Since there is a callout that is
27703c6e15bcSPyun YongHyeon 	 * invoked for every hz in driver we use that instead of
27713c6e15bcSPyun YongHyeon 	 * relying on periodic SMB interrupt.
27723c6e15bcSPyun YongHyeon 	 */
27733c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_SMB_STAT_TIMER, ALE_USECS(0));
27743c6e15bcSPyun YongHyeon 	/* Clear MAC statistics. */
27753c6e15bcSPyun YongHyeon 	ale_stats_clear(sc);
27763c6e15bcSPyun YongHyeon 
27773c6e15bcSPyun YongHyeon 	/*
27783c6e15bcSPyun YongHyeon 	 * Configure Tx/Rx MACs.
27793c6e15bcSPyun YongHyeon 	 *  - Auto-padding for short frames.
27803c6e15bcSPyun YongHyeon 	 *  - Enable CRC generation.
27813c6e15bcSPyun YongHyeon 	 *  Actual reconfiguration of MAC for resolved speed/duplex
27823c6e15bcSPyun YongHyeon 	 *  is followed after detection of link establishment.
27833c6e15bcSPyun YongHyeon 	 *  AR81xx always does checksum computation regardless of
27843c6e15bcSPyun YongHyeon 	 *  MAC_CFG_RXCSUM_ENB bit. In fact, setting the bit will
27853c6e15bcSPyun YongHyeon 	 *  cause Rx handling issue for fragmented IP datagrams due
27863c6e15bcSPyun YongHyeon 	 *  to silicon bug.
27873c6e15bcSPyun YongHyeon 	 */
27883c6e15bcSPyun YongHyeon 	reg = MAC_CFG_TX_CRC_ENB | MAC_CFG_TX_AUTO_PAD | MAC_CFG_FULL_DUPLEX |
27893c6e15bcSPyun YongHyeon 	    ((MAC_CFG_PREAMBLE_DEFAULT << MAC_CFG_PREAMBLE_SHIFT) &
27903c6e15bcSPyun YongHyeon 	    MAC_CFG_PREAMBLE_MASK);
27913c6e15bcSPyun YongHyeon 	if ((sc->ale_flags & ALE_FLAG_FASTETHER) != 0)
27923c6e15bcSPyun YongHyeon 		reg |= MAC_CFG_SPEED_10_100;
27933c6e15bcSPyun YongHyeon 	else
27943c6e15bcSPyun YongHyeon 		reg |= MAC_CFG_SPEED_1000;
27953c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_MAC_CFG, reg);
27963c6e15bcSPyun YongHyeon 
27973c6e15bcSPyun YongHyeon 	/* Set up the receive filter. */
27983c6e15bcSPyun YongHyeon 	ale_rxfilter(sc);
27993c6e15bcSPyun YongHyeon 	ale_rxvlan(sc);
28003c6e15bcSPyun YongHyeon 
28013c6e15bcSPyun YongHyeon 	/* Acknowledge all pending interrupts and clear it. */
28023c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_INTR_MASK, ALE_INTRS);
28033c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_INTR_STATUS, 0xFFFFFFFF);
28043c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_INTR_STATUS, 0);
28053c6e15bcSPyun YongHyeon 
280652436412SJustin Hibbits 	if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
280752436412SJustin Hibbits 	if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
28082b6f7122SPyun YongHyeon 
28093c6e15bcSPyun YongHyeon 	sc->ale_flags &= ~ALE_FLAG_LINK;
28103c6e15bcSPyun YongHyeon 	/* Switch to the current media. */
28113c6e15bcSPyun YongHyeon 	mii_mediachg(mii);
28123c6e15bcSPyun YongHyeon 
28133c6e15bcSPyun YongHyeon 	callout_reset(&sc->ale_tick_ch, hz, ale_tick, sc);
28143c6e15bcSPyun YongHyeon }
28153c6e15bcSPyun YongHyeon 
28163c6e15bcSPyun YongHyeon static void
ale_stop(struct ale_softc * sc)28173c6e15bcSPyun YongHyeon ale_stop(struct ale_softc *sc)
28183c6e15bcSPyun YongHyeon {
281952436412SJustin Hibbits 	if_t ifp;
28203c6e15bcSPyun YongHyeon 	struct ale_txdesc *txd;
28213c6e15bcSPyun YongHyeon 	uint32_t reg;
28223c6e15bcSPyun YongHyeon 	int i;
28233c6e15bcSPyun YongHyeon 
28243c6e15bcSPyun YongHyeon 	ALE_LOCK_ASSERT(sc);
28253c6e15bcSPyun YongHyeon 	/*
28263c6e15bcSPyun YongHyeon 	 * Mark the interface down and cancel the watchdog timer.
28273c6e15bcSPyun YongHyeon 	 */
28283c6e15bcSPyun YongHyeon 	ifp = sc->ale_ifp;
282952436412SJustin Hibbits 	if_setdrvflagbits(ifp, 0, (IFF_DRV_RUNNING | IFF_DRV_OACTIVE));
28303c6e15bcSPyun YongHyeon 	sc->ale_flags &= ~ALE_FLAG_LINK;
28313c6e15bcSPyun YongHyeon 	callout_stop(&sc->ale_tick_ch);
28323c6e15bcSPyun YongHyeon 	sc->ale_watchdog_timer = 0;
28333c6e15bcSPyun YongHyeon 	ale_stats_update(sc);
28343c6e15bcSPyun YongHyeon 	/* Disable interrupts. */
28353c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_INTR_MASK, 0);
28363c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_INTR_STATUS, 0xFFFFFFFF);
28373c6e15bcSPyun YongHyeon 	/* Disable queue processing and DMA. */
28383c6e15bcSPyun YongHyeon 	reg = CSR_READ_4(sc, ALE_TXQ_CFG);
28393c6e15bcSPyun YongHyeon 	reg &= ~TXQ_CFG_ENB;
28403c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_TXQ_CFG, reg);
28413c6e15bcSPyun YongHyeon 	reg = CSR_READ_4(sc, ALE_RXQ_CFG);
28423c6e15bcSPyun YongHyeon 	reg &= ~RXQ_CFG_ENB;
28433c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_RXQ_CFG, reg);
28443c6e15bcSPyun YongHyeon 	reg = CSR_READ_4(sc, ALE_DMA_CFG);
28453c6e15bcSPyun YongHyeon 	reg &= ~(DMA_CFG_TXCMB_ENB | DMA_CFG_RXCMB_ENB);
28463c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_DMA_CFG, reg);
28473c6e15bcSPyun YongHyeon 	DELAY(1000);
28483c6e15bcSPyun YongHyeon 	/* Stop Rx/Tx MACs. */
28493c6e15bcSPyun YongHyeon 	ale_stop_mac(sc);
28503c6e15bcSPyun YongHyeon 	/* Disable interrupts which might be touched in taskq handler. */
28513c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_INTR_STATUS, 0xFFFFFFFF);
28523c6e15bcSPyun YongHyeon 
28533c6e15bcSPyun YongHyeon 	/*
28543c6e15bcSPyun YongHyeon 	 * Free TX mbufs still in the queues.
28553c6e15bcSPyun YongHyeon 	 */
28563c6e15bcSPyun YongHyeon 	for (i = 0; i < ALE_TX_RING_CNT; i++) {
28573c6e15bcSPyun YongHyeon 		txd = &sc->ale_cdata.ale_txdesc[i];
28583c6e15bcSPyun YongHyeon 		if (txd->tx_m != NULL) {
28593c6e15bcSPyun YongHyeon 			bus_dmamap_sync(sc->ale_cdata.ale_tx_tag,
28603c6e15bcSPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
28613c6e15bcSPyun YongHyeon 			bus_dmamap_unload(sc->ale_cdata.ale_tx_tag,
28623c6e15bcSPyun YongHyeon 			    txd->tx_dmamap);
28633c6e15bcSPyun YongHyeon 			m_freem(txd->tx_m);
28643c6e15bcSPyun YongHyeon 			txd->tx_m = NULL;
28653c6e15bcSPyun YongHyeon 		}
28663c6e15bcSPyun YongHyeon         }
28673c6e15bcSPyun YongHyeon }
28683c6e15bcSPyun YongHyeon 
28693c6e15bcSPyun YongHyeon static void
ale_stop_mac(struct ale_softc * sc)28703c6e15bcSPyun YongHyeon ale_stop_mac(struct ale_softc *sc)
28713c6e15bcSPyun YongHyeon {
28723c6e15bcSPyun YongHyeon 	uint32_t reg;
28733c6e15bcSPyun YongHyeon 	int i;
28743c6e15bcSPyun YongHyeon 
28753c6e15bcSPyun YongHyeon 	ALE_LOCK_ASSERT(sc);
28763c6e15bcSPyun YongHyeon 
28773c6e15bcSPyun YongHyeon 	reg = CSR_READ_4(sc, ALE_MAC_CFG);
28783c6e15bcSPyun YongHyeon 	if ((reg & (MAC_CFG_TX_ENB | MAC_CFG_RX_ENB)) != 0) {
28799f988ef1SPyun YongHyeon 		reg &= ~(MAC_CFG_TX_ENB | MAC_CFG_RX_ENB);
28803c6e15bcSPyun YongHyeon 		CSR_WRITE_4(sc, ALE_MAC_CFG, reg);
28813c6e15bcSPyun YongHyeon 	}
28823c6e15bcSPyun YongHyeon 
28833c6e15bcSPyun YongHyeon 	for (i = ALE_TIMEOUT; i > 0; i--) {
28843c6e15bcSPyun YongHyeon 		reg = CSR_READ_4(sc, ALE_IDLE_STATUS);
28853c6e15bcSPyun YongHyeon 		if (reg == 0)
28863c6e15bcSPyun YongHyeon 			break;
28873c6e15bcSPyun YongHyeon 		DELAY(10);
28883c6e15bcSPyun YongHyeon 	}
28893c6e15bcSPyun YongHyeon 	if (i == 0)
28903c6e15bcSPyun YongHyeon 		device_printf(sc->ale_dev,
28913c6e15bcSPyun YongHyeon 		    "could not disable Tx/Rx MAC(0x%08x)!\n", reg);
28923c6e15bcSPyun YongHyeon }
28933c6e15bcSPyun YongHyeon 
28943c6e15bcSPyun YongHyeon static void
ale_init_tx_ring(struct ale_softc * sc)28953c6e15bcSPyun YongHyeon ale_init_tx_ring(struct ale_softc *sc)
28963c6e15bcSPyun YongHyeon {
28973c6e15bcSPyun YongHyeon 	struct ale_txdesc *txd;
28983c6e15bcSPyun YongHyeon 	int i;
28993c6e15bcSPyun YongHyeon 
29003c6e15bcSPyun YongHyeon 	ALE_LOCK_ASSERT(sc);
29013c6e15bcSPyun YongHyeon 
29023c6e15bcSPyun YongHyeon 	sc->ale_cdata.ale_tx_prod = 0;
29033c6e15bcSPyun YongHyeon 	sc->ale_cdata.ale_tx_cons = 0;
29043c6e15bcSPyun YongHyeon 	sc->ale_cdata.ale_tx_cnt = 0;
29053c6e15bcSPyun YongHyeon 
29063c6e15bcSPyun YongHyeon 	bzero(sc->ale_cdata.ale_tx_ring, ALE_TX_RING_SZ);
29073c6e15bcSPyun YongHyeon 	bzero(sc->ale_cdata.ale_tx_cmb, ALE_TX_CMB_SZ);
29083c6e15bcSPyun YongHyeon 	for (i = 0; i < ALE_TX_RING_CNT; i++) {
29093c6e15bcSPyun YongHyeon 		txd = &sc->ale_cdata.ale_txdesc[i];
29103c6e15bcSPyun YongHyeon 		txd->tx_m = NULL;
29113c6e15bcSPyun YongHyeon 	}
29123c6e15bcSPyun YongHyeon 	*sc->ale_cdata.ale_tx_cmb = 0;
29133c6e15bcSPyun YongHyeon 	bus_dmamap_sync(sc->ale_cdata.ale_tx_cmb_tag,
29143c6e15bcSPyun YongHyeon 	    sc->ale_cdata.ale_tx_cmb_map,
29153c6e15bcSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
29163c6e15bcSPyun YongHyeon 	bus_dmamap_sync(sc->ale_cdata.ale_tx_ring_tag,
29173c6e15bcSPyun YongHyeon 	    sc->ale_cdata.ale_tx_ring_map,
29183c6e15bcSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
29193c6e15bcSPyun YongHyeon }
29203c6e15bcSPyun YongHyeon 
29213c6e15bcSPyun YongHyeon static void
ale_init_rx_pages(struct ale_softc * sc)29223c6e15bcSPyun YongHyeon ale_init_rx_pages(struct ale_softc *sc)
29233c6e15bcSPyun YongHyeon {
29243c6e15bcSPyun YongHyeon 	struct ale_rx_page *rx_page;
29253c6e15bcSPyun YongHyeon 	int i;
29263c6e15bcSPyun YongHyeon 
29273c6e15bcSPyun YongHyeon 	ALE_LOCK_ASSERT(sc);
29283c6e15bcSPyun YongHyeon 
2929e23559dbSPyun YongHyeon 	sc->ale_morework = 0;
29303c6e15bcSPyun YongHyeon 	sc->ale_cdata.ale_rx_seqno = 0;
29313c6e15bcSPyun YongHyeon 	sc->ale_cdata.ale_rx_curp = 0;
29323c6e15bcSPyun YongHyeon 
29333c6e15bcSPyun YongHyeon 	for (i = 0; i < ALE_RX_PAGES; i++) {
29343c6e15bcSPyun YongHyeon 		rx_page = &sc->ale_cdata.ale_rx_page[i];
29353c6e15bcSPyun YongHyeon 		bzero(rx_page->page_addr, sc->ale_pagesize);
29363c6e15bcSPyun YongHyeon 		bzero(rx_page->cmb_addr, ALE_RX_CMB_SZ);
29373c6e15bcSPyun YongHyeon 		rx_page->cons = 0;
29383c6e15bcSPyun YongHyeon 		*rx_page->cmb_addr = 0;
29393c6e15bcSPyun YongHyeon 		bus_dmamap_sync(rx_page->page_tag, rx_page->page_map,
29403c6e15bcSPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
29413c6e15bcSPyun YongHyeon 		bus_dmamap_sync(rx_page->cmb_tag, rx_page->cmb_map,
29423c6e15bcSPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
29433c6e15bcSPyun YongHyeon 	}
29443c6e15bcSPyun YongHyeon }
29453c6e15bcSPyun YongHyeon 
29463c6e15bcSPyun YongHyeon static void
ale_rxvlan(struct ale_softc * sc)29473c6e15bcSPyun YongHyeon ale_rxvlan(struct ale_softc *sc)
29483c6e15bcSPyun YongHyeon {
294952436412SJustin Hibbits 	if_t ifp;
29503c6e15bcSPyun YongHyeon 	uint32_t reg;
29513c6e15bcSPyun YongHyeon 
29523c6e15bcSPyun YongHyeon 	ALE_LOCK_ASSERT(sc);
29533c6e15bcSPyun YongHyeon 
29543c6e15bcSPyun YongHyeon 	ifp = sc->ale_ifp;
29553c6e15bcSPyun YongHyeon 	reg = CSR_READ_4(sc, ALE_MAC_CFG);
29563c6e15bcSPyun YongHyeon 	reg &= ~MAC_CFG_VLAN_TAG_STRIP;
295752436412SJustin Hibbits 	if ((if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) != 0)
29583c6e15bcSPyun YongHyeon 		reg |= MAC_CFG_VLAN_TAG_STRIP;
29593c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_MAC_CFG, reg);
29603c6e15bcSPyun YongHyeon }
29613c6e15bcSPyun YongHyeon 
296216650a01SGleb Smirnoff static u_int
ale_hash_maddr(void * arg,struct sockaddr_dl * sdl,u_int cnt)296316650a01SGleb Smirnoff ale_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
296416650a01SGleb Smirnoff {
296516650a01SGleb Smirnoff 	uint32_t crc, *mchash = arg;
296616650a01SGleb Smirnoff 
296716650a01SGleb Smirnoff 	crc = ether_crc32_be(LLADDR(sdl), ETHER_ADDR_LEN);
296816650a01SGleb Smirnoff 	mchash[crc >> 31] |= 1 << ((crc >> 26) & 0x1f);
296916650a01SGleb Smirnoff 
297016650a01SGleb Smirnoff 	return (1);
297116650a01SGleb Smirnoff }
297216650a01SGleb Smirnoff 
29733c6e15bcSPyun YongHyeon static void
ale_rxfilter(struct ale_softc * sc)29743c6e15bcSPyun YongHyeon ale_rxfilter(struct ale_softc *sc)
29753c6e15bcSPyun YongHyeon {
297652436412SJustin Hibbits 	if_t ifp;
29773c6e15bcSPyun YongHyeon 	uint32_t mchash[2];
29783c6e15bcSPyun YongHyeon 	uint32_t rxcfg;
29793c6e15bcSPyun YongHyeon 
29803c6e15bcSPyun YongHyeon 	ALE_LOCK_ASSERT(sc);
29813c6e15bcSPyun YongHyeon 
29823c6e15bcSPyun YongHyeon 	ifp = sc->ale_ifp;
29833c6e15bcSPyun YongHyeon 
29843c6e15bcSPyun YongHyeon 	rxcfg = CSR_READ_4(sc, ALE_MAC_CFG);
29853c6e15bcSPyun YongHyeon 	rxcfg &= ~(MAC_CFG_ALLMULTI | MAC_CFG_BCAST | MAC_CFG_PROMISC);
298652436412SJustin Hibbits 	if ((if_getflags(ifp) & IFF_BROADCAST) != 0)
29873c6e15bcSPyun YongHyeon 		rxcfg |= MAC_CFG_BCAST;
298852436412SJustin Hibbits 	if ((if_getflags(ifp) & (IFF_PROMISC | IFF_ALLMULTI)) != 0) {
298952436412SJustin Hibbits 		if ((if_getflags(ifp) & IFF_PROMISC) != 0)
29903c6e15bcSPyun YongHyeon 			rxcfg |= MAC_CFG_PROMISC;
299152436412SJustin Hibbits 		if ((if_getflags(ifp) & IFF_ALLMULTI) != 0)
29923c6e15bcSPyun YongHyeon 			rxcfg |= MAC_CFG_ALLMULTI;
29933c6e15bcSPyun YongHyeon 		CSR_WRITE_4(sc, ALE_MAR0, 0xFFFFFFFF);
29943c6e15bcSPyun YongHyeon 		CSR_WRITE_4(sc, ALE_MAR1, 0xFFFFFFFF);
29953c6e15bcSPyun YongHyeon 		CSR_WRITE_4(sc, ALE_MAC_CFG, rxcfg);
29963c6e15bcSPyun YongHyeon 		return;
29973c6e15bcSPyun YongHyeon 	}
29983c6e15bcSPyun YongHyeon 
29993c6e15bcSPyun YongHyeon 	/* Program new filter. */
30003c6e15bcSPyun YongHyeon 	bzero(mchash, sizeof(mchash));
300116650a01SGleb Smirnoff 	if_foreach_llmaddr(ifp, ale_hash_maddr, &mchash);
30023c6e15bcSPyun YongHyeon 
30033c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_MAR0, mchash[0]);
30043c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_MAR1, mchash[1]);
30053c6e15bcSPyun YongHyeon 	CSR_WRITE_4(sc, ALE_MAC_CFG, rxcfg);
30063c6e15bcSPyun YongHyeon }
30073c6e15bcSPyun YongHyeon 
30083c6e15bcSPyun YongHyeon static int
sysctl_int_range(SYSCTL_HANDLER_ARGS,int low,int high)30093c6e15bcSPyun YongHyeon sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
30103c6e15bcSPyun YongHyeon {
30113c6e15bcSPyun YongHyeon 	int error, value;
30123c6e15bcSPyun YongHyeon 
30133c6e15bcSPyun YongHyeon 	if (arg1 == NULL)
30143c6e15bcSPyun YongHyeon 		return (EINVAL);
30153c6e15bcSPyun YongHyeon 	value = *(int *)arg1;
30163c6e15bcSPyun YongHyeon 	error = sysctl_handle_int(oidp, &value, 0, req);
30173c6e15bcSPyun YongHyeon 	if (error || req->newptr == NULL)
30183c6e15bcSPyun YongHyeon 		return (error);
30193c6e15bcSPyun YongHyeon 	if (value < low || value > high)
30203c6e15bcSPyun YongHyeon 		return (EINVAL);
30213c6e15bcSPyun YongHyeon         *(int *)arg1 = value;
30223c6e15bcSPyun YongHyeon 
30233c6e15bcSPyun YongHyeon         return (0);
30243c6e15bcSPyun YongHyeon }
30253c6e15bcSPyun YongHyeon 
30263c6e15bcSPyun YongHyeon static int
sysctl_hw_ale_proc_limit(SYSCTL_HANDLER_ARGS)30273c6e15bcSPyun YongHyeon sysctl_hw_ale_proc_limit(SYSCTL_HANDLER_ARGS)
30283c6e15bcSPyun YongHyeon {
30293c6e15bcSPyun YongHyeon 	return (sysctl_int_range(oidp, arg1, arg2, req,
30303c6e15bcSPyun YongHyeon 	    ALE_PROC_MIN, ALE_PROC_MAX));
30313c6e15bcSPyun YongHyeon }
30323c6e15bcSPyun YongHyeon 
30333c6e15bcSPyun YongHyeon static int
sysctl_hw_ale_int_mod(SYSCTL_HANDLER_ARGS)30343c6e15bcSPyun YongHyeon sysctl_hw_ale_int_mod(SYSCTL_HANDLER_ARGS)
30353c6e15bcSPyun YongHyeon {
30363c6e15bcSPyun YongHyeon 
30373c6e15bcSPyun YongHyeon 	return (sysctl_int_range(oidp, arg1, arg2, req,
30383c6e15bcSPyun YongHyeon 	    ALE_IM_TIMER_MIN, ALE_IM_TIMER_MAX));
30393c6e15bcSPyun YongHyeon }
3040