Home | History | Annotate | Line # | Download | only in pci
if_ale.c revision 1.27
      1 /*	$NetBSD: if_ale.c,v 1.27 2019/01/22 03:42:27 msaitoh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2008, Pyun YongHyeon <yongari (at) FreeBSD.org>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice unmodified, this list of conditions, and the following
     12  *    disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  * SUCH DAMAGE.
     28  *
     29  * $FreeBSD: src/sys/dev/ale/if_ale.c,v 1.3 2008/12/03 09:01:12 yongari Exp $
     30  */
     31 
     32 /* Driver for Atheros AR8121/AR8113/AR8114 PCIe Ethernet. */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: if_ale.c,v 1.27 2019/01/22 03:42:27 msaitoh Exp $");
     36 
     37 #include "vlan.h"
     38 
     39 #include <sys/param.h>
     40 #include <sys/proc.h>
     41 #include <sys/endian.h>
     42 #include <sys/systm.h>
     43 #include <sys/types.h>
     44 #include <sys/sockio.h>
     45 #include <sys/mbuf.h>
     46 #include <sys/queue.h>
     47 #include <sys/kernel.h>
     48 #include <sys/device.h>
     49 #include <sys/callout.h>
     50 #include <sys/socket.h>
     51 
     52 #include <sys/bus.h>
     53 
     54 #include <net/if.h>
     55 #include <net/if_dl.h>
     56 #include <net/if_llc.h>
     57 #include <net/if_media.h>
     58 #include <net/if_ether.h>
     59 
     60 #ifdef INET
     61 #include <netinet/in.h>
     62 #include <netinet/in_systm.h>
     63 #include <netinet/in_var.h>
     64 #include <netinet/ip.h>
     65 #endif
     66 
     67 #include <net/if_types.h>
     68 #include <net/if_vlanvar.h>
     69 
     70 #include <net/bpf.h>
     71 
     72 #include <dev/mii/mii.h>
     73 #include <dev/mii/miivar.h>
     74 
     75 #include <dev/pci/pcireg.h>
     76 #include <dev/pci/pcivar.h>
     77 #include <dev/pci/pcidevs.h>
     78 
     79 #include <dev/pci/if_alereg.h>
     80 
     81 static int	ale_match(device_t, cfdata_t, void *);
     82 static void	ale_attach(device_t, device_t, void *);
     83 static int	ale_detach(device_t, int);
     84 
     85 static int	ale_miibus_readreg(device_t, int, int, uint16_t *);
     86 static int	ale_miibus_writereg(device_t, int, int, uint16_t);
     87 static void	ale_miibus_statchg(struct ifnet *);
     88 
     89 static int	ale_init(struct ifnet *);
     90 static void	ale_start(struct ifnet *);
     91 static int	ale_ioctl(struct ifnet *, u_long, void *);
     92 static void	ale_watchdog(struct ifnet *);
     93 static int	ale_mediachange(struct ifnet *);
     94 static void	ale_mediastatus(struct ifnet *, struct ifmediareq *);
     95 
     96 static int	ale_intr(void *);
     97 static int	ale_rxeof(struct ale_softc *sc);
     98 static void	ale_rx_update_page(struct ale_softc *, struct ale_rx_page **,
     99 		    uint32_t, uint32_t *);
    100 static void	ale_rxcsum(struct ale_softc *, struct mbuf *, uint32_t);
    101 static void	ale_txeof(struct ale_softc *);
    102 
    103 static int	ale_dma_alloc(struct ale_softc *);
    104 static void	ale_dma_free(struct ale_softc *);
    105 static int	ale_encap(struct ale_softc *, struct mbuf **);
    106 static void	ale_init_rx_pages(struct ale_softc *);
    107 static void	ale_init_tx_ring(struct ale_softc *);
    108 
    109 static void	ale_stop(struct ifnet *, int);
    110 static void	ale_tick(void *);
    111 static void	ale_get_macaddr(struct ale_softc *);
    112 static void	ale_mac_config(struct ale_softc *);
    113 static void	ale_phy_reset(struct ale_softc *);
    114 static void	ale_reset(struct ale_softc *);
    115 static void	ale_rxfilter(struct ale_softc *);
    116 static void	ale_rxvlan(struct ale_softc *);
    117 static void	ale_stats_clear(struct ale_softc *);
    118 static void	ale_stats_update(struct ale_softc *);
    119 static void	ale_stop_mac(struct ale_softc *);
    120 
    121 CFATTACH_DECL_NEW(ale, sizeof(struct ale_softc),
    122 	ale_match, ale_attach, ale_detach, NULL);
    123 
    124 int aledebug = 0;
    125 #define DPRINTF(x)	do { if (aledebug) printf x; } while (0)
    126 
    127 #define ETHER_ALIGN 2
    128 #define ALE_CSUM_FEATURES	(M_CSUM_TCPv4 | M_CSUM_UDPv4)
    129 
    130 static int
    131 ale_miibus_readreg(device_t dev, int phy, int reg, uint16_t *val)
    132 {
    133 	struct ale_softc *sc = device_private(dev);
    134 	uint32_t v;
    135 	int i;
    136 
    137 	if (phy != sc->ale_phyaddr)
    138 		return -1;
    139 
    140 	if (sc->ale_flags & ALE_FLAG_FASTETHER) {
    141 		switch (reg) {
    142 		case MII_100T2CR:
    143 		case MII_100T2SR:
    144 		case MII_EXTSR:
    145 			*val = 0;
    146 			return 0;
    147 		default:
    148 			break;
    149 		}
    150 	}
    151 
    152 	CSR_WRITE_4(sc, ALE_MDIO, MDIO_OP_EXECUTE | MDIO_OP_READ |
    153 	    MDIO_SUP_PREAMBLE | MDIO_CLK_25_4 | MDIO_REG_ADDR(reg));
    154 	for (i = ALE_PHY_TIMEOUT; i > 0; i--) {
    155 		DELAY(5);
    156 		v = CSR_READ_4(sc, ALE_MDIO);
    157 		if ((v & (MDIO_OP_EXECUTE | MDIO_OP_BUSY)) == 0)
    158 			break;
    159 	}
    160 
    161 	if (i == 0) {
    162 		printf("%s: phy read timeout: phy %d, reg %d\n",
    163 		    device_xname(sc->sc_dev), phy, reg);
    164 		return ETIMEDOUT;
    165 	}
    166 
    167 	*val = (v & MDIO_DATA_MASK) >> MDIO_DATA_SHIFT;
    168 	return 0;
    169 }
    170 
    171 static int
    172 ale_miibus_writereg(device_t dev, int phy, int reg, uint16_t val)
    173 {
    174 	struct ale_softc *sc = device_private(dev);
    175 	uint32_t v;
    176 	int i;
    177 
    178 	if (phy != sc->ale_phyaddr)
    179 		return -1;
    180 
    181 	if (sc->ale_flags & ALE_FLAG_FASTETHER) {
    182 		switch (reg) {
    183 		case MII_100T2CR:
    184 		case MII_100T2SR:
    185 		case MII_EXTSR:
    186 			return 0;
    187 		default:
    188 			break;
    189 		}
    190 	}
    191 
    192 	CSR_WRITE_4(sc, ALE_MDIO, MDIO_OP_EXECUTE | MDIO_OP_WRITE |
    193 	    (val & MDIO_DATA_MASK) << MDIO_DATA_SHIFT |
    194 	    MDIO_SUP_PREAMBLE | MDIO_CLK_25_4 | MDIO_REG_ADDR(reg));
    195 	for (i = ALE_PHY_TIMEOUT; i > 0; i--) {
    196 		DELAY(5);
    197 		v = CSR_READ_4(sc, ALE_MDIO);
    198 		if ((v & (MDIO_OP_EXECUTE | MDIO_OP_BUSY)) == 0)
    199 			break;
    200 	}
    201 
    202 	if (i == 0) {
    203 		printf("%s: phy write timeout: phy %d, reg %d\n",
    204 		    device_xname(sc->sc_dev), phy, reg);
    205 		return ETIMEDOUT;
    206 	}
    207 
    208 	return 0;
    209 }
    210 
    211 static void
    212 ale_miibus_statchg(struct ifnet *ifp)
    213 {
    214 	struct ale_softc *sc = ifp->if_softc;
    215 	struct mii_data *mii = &sc->sc_miibus;
    216 	uint32_t reg;
    217 
    218 	if ((ifp->if_flags & IFF_RUNNING) == 0)
    219 		return;
    220 
    221 	sc->ale_flags &= ~ALE_FLAG_LINK;
    222 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
    223 	    (IFM_ACTIVE | IFM_AVALID)) {
    224 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
    225 		case IFM_10_T:
    226 		case IFM_100_TX:
    227 			sc->ale_flags |= ALE_FLAG_LINK;
    228 			break;
    229 
    230 		case IFM_1000_T:
    231 			if ((sc->ale_flags & ALE_FLAG_FASTETHER) == 0)
    232 				sc->ale_flags |= ALE_FLAG_LINK;
    233 			break;
    234 
    235 		default:
    236 			break;
    237 		}
    238 	}
    239 
    240 	/* Stop Rx/Tx MACs. */
    241 	ale_stop_mac(sc);
    242 
    243 	/* Program MACs with resolved speed/duplex/flow-control. */
    244 	if ((sc->ale_flags & ALE_FLAG_LINK) != 0) {
    245 		ale_mac_config(sc);
    246 		/* Reenable Tx/Rx MACs. */
    247 		reg = CSR_READ_4(sc, ALE_MAC_CFG);
    248 		reg |= MAC_CFG_TX_ENB | MAC_CFG_RX_ENB;
    249 		CSR_WRITE_4(sc, ALE_MAC_CFG, reg);
    250 	}
    251 }
    252 
    253 void
    254 ale_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
    255 {
    256 	struct ale_softc *sc = ifp->if_softc;
    257 	struct mii_data *mii = &sc->sc_miibus;
    258 
    259 	mii_pollstat(mii);
    260 	ifmr->ifm_status = mii->mii_media_status;
    261 	ifmr->ifm_active = mii->mii_media_active;
    262 }
    263 
    264 int
    265 ale_mediachange(struct ifnet *ifp)
    266 {
    267 	struct ale_softc *sc = ifp->if_softc;
    268 	struct mii_data *mii = &sc->sc_miibus;
    269 	int error;
    270 
    271 	if (mii->mii_instance != 0) {
    272 		struct mii_softc *miisc;
    273 
    274 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
    275 			mii_phy_reset(miisc);
    276 	}
    277 	error = mii_mediachg(mii);
    278 
    279 	return error;
    280 }
    281 
    282 int
    283 ale_match(device_t dev, cfdata_t match, void *aux)
    284 {
    285 	struct pci_attach_args *pa = aux;
    286 
    287 	return (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ATTANSIC &&
    288 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATTANSIC_ETHERNET_L1E);
    289 }
    290 
    291 void
    292 ale_get_macaddr(struct ale_softc *sc)
    293 {
    294 	uint32_t ea[2], reg;
    295 	int i, vpdc;
    296 
    297 	reg = CSR_READ_4(sc, ALE_SPI_CTRL);
    298 	if ((reg & SPI_VPD_ENB) != 0) {
    299 		reg &= ~SPI_VPD_ENB;
    300 		CSR_WRITE_4(sc, ALE_SPI_CTRL, reg);
    301 	}
    302 
    303 	if (pci_get_capability(sc->sc_pct, sc->sc_pcitag, PCI_CAP_VPD,
    304 	    &vpdc, NULL)) {
    305 		/*
    306 		 * PCI VPD capability found, let TWSI reload EEPROM.
    307 		 * This will set ethernet address of controller.
    308 		 */
    309 		CSR_WRITE_4(sc, ALE_TWSI_CTRL, CSR_READ_4(sc, ALE_TWSI_CTRL) |
    310 		    TWSI_CTRL_SW_LD_START);
    311 		for (i = 100; i > 0; i--) {
    312 			DELAY(1000);
    313 			reg = CSR_READ_4(sc, ALE_TWSI_CTRL);
    314 			if ((reg & TWSI_CTRL_SW_LD_START) == 0)
    315 				break;
    316 		}
    317 		if (i == 0)
    318 			printf("%s: reloading EEPROM timeout!\n",
    319 			    device_xname(sc->sc_dev));
    320 	} else {
    321 		if (aledebug)
    322 			printf("%s: PCI VPD capability not found!\n",
    323 			    device_xname(sc->sc_dev));
    324 	}
    325 
    326 	ea[0] = CSR_READ_4(sc, ALE_PAR0);
    327 	ea[1] = CSR_READ_4(sc, ALE_PAR1);
    328 	sc->ale_eaddr[0] = (ea[1] >> 8) & 0xFF;
    329 	sc->ale_eaddr[1] = (ea[1] >> 0) & 0xFF;
    330 	sc->ale_eaddr[2] = (ea[0] >> 24) & 0xFF;
    331 	sc->ale_eaddr[3] = (ea[0] >> 16) & 0xFF;
    332 	sc->ale_eaddr[4] = (ea[0] >> 8) & 0xFF;
    333 	sc->ale_eaddr[5] = (ea[0] >> 0) & 0xFF;
    334 }
    335 
    336 void
    337 ale_phy_reset(struct ale_softc *sc)
    338 {
    339 	/* Reset magic from Linux. */
    340 	CSR_WRITE_2(sc, ALE_GPHY_CTRL,
    341 	    GPHY_CTRL_HIB_EN | GPHY_CTRL_HIB_PULSE | GPHY_CTRL_SEL_ANA_RESET |
    342 	    GPHY_CTRL_PHY_PLL_ON);
    343 	DELAY(1000);
    344 	CSR_WRITE_2(sc, ALE_GPHY_CTRL,
    345 	    GPHY_CTRL_EXT_RESET | GPHY_CTRL_HIB_EN | GPHY_CTRL_HIB_PULSE |
    346 	    GPHY_CTRL_SEL_ANA_RESET | GPHY_CTRL_PHY_PLL_ON);
    347 	DELAY(1000);
    348 
    349 #define	ATPHY_DBG_ADDR		0x1D
    350 #define	ATPHY_DBG_DATA		0x1E
    351 
    352 	/* Enable hibernation mode. */
    353 	ale_miibus_writereg(sc->sc_dev, sc->ale_phyaddr,
    354 	    ATPHY_DBG_ADDR, 0x0B);
    355 	ale_miibus_writereg(sc->sc_dev, sc->ale_phyaddr,
    356 	    ATPHY_DBG_DATA, 0xBC00);
    357 	/* Set Class A/B for all modes. */
    358 	ale_miibus_writereg(sc->sc_dev, sc->ale_phyaddr,
    359 	    ATPHY_DBG_ADDR, 0x00);
    360 	ale_miibus_writereg(sc->sc_dev, sc->ale_phyaddr,
    361 	    ATPHY_DBG_DATA, 0x02EF);
    362 	/* Enable 10BT power saving. */
    363 	ale_miibus_writereg(sc->sc_dev, sc->ale_phyaddr,
    364 	    ATPHY_DBG_ADDR, 0x12);
    365 	ale_miibus_writereg(sc->sc_dev, sc->ale_phyaddr,
    366 	    ATPHY_DBG_DATA, 0x4C04);
    367 	/* Adjust 1000T power. */
    368 	ale_miibus_writereg(sc->sc_dev, sc->ale_phyaddr,
    369 	    ATPHY_DBG_ADDR, 0x04);
    370 	ale_miibus_writereg(sc->sc_dev, sc->ale_phyaddr,
    371 	    ATPHY_DBG_DATA, 0x8BBB);
    372 	/* 10BT center tap voltage. */
    373 	ale_miibus_writereg(sc->sc_dev, sc->ale_phyaddr,
    374 	    ATPHY_DBG_ADDR, 0x05);
    375 	ale_miibus_writereg(sc->sc_dev, sc->ale_phyaddr,
    376 	    ATPHY_DBG_DATA, 0x2C46);
    377 
    378 #undef	ATPHY_DBG_ADDR
    379 #undef	ATPHY_DBG_DATA
    380 	DELAY(1000);
    381 }
    382 
    383 void
    384 ale_attach(device_t parent, device_t self, void *aux)
    385 {
    386 	struct ale_softc *sc = device_private(self);
    387 	struct pci_attach_args *pa = aux;
    388 	pci_chipset_tag_t pc = pa->pa_pc;
    389 	pci_intr_handle_t ih;
    390 	const char *intrstr;
    391 	struct ifnet *ifp;
    392 	pcireg_t memtype;
    393 	int mii_flags, error = 0;
    394 	uint32_t rxf_len, txf_len;
    395 	const char *chipname;
    396 	char intrbuf[PCI_INTRSTR_LEN];
    397 
    398 	aprint_naive("\n");
    399 	aprint_normal(": Attansic/Atheros L1E Ethernet\n");
    400 
    401 	sc->sc_dev = self;
    402 	sc->sc_dmat = pa->pa_dmat;
    403 	sc->sc_pct = pa->pa_pc;
    404 	sc->sc_pcitag = pa->pa_tag;
    405 
    406 	/*
    407 	 * Allocate IO memory
    408 	 */
    409 	memtype = pci_mapreg_type(sc->sc_pct, sc->sc_pcitag, ALE_PCIR_BAR);
    410 	switch (memtype) {
    411 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
    412 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT_1M:
    413 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
    414 		break;
    415 	default:
    416 		aprint_error_dev(self, "invalid base address register\n");
    417 		break;
    418 	}
    419 
    420 	if (pci_mapreg_map(pa, ALE_PCIR_BAR, memtype, 0, &sc->sc_mem_bt,
    421 	    &sc->sc_mem_bh, NULL, &sc->sc_mem_size)) {
    422 		aprint_error_dev(self, "could not map mem space\n");
    423 		return;
    424 	}
    425 
    426 	if (pci_intr_map(pa, &ih) != 0) {
    427 		aprint_error_dev(self, "could not map interrupt\n");
    428 		goto fail;
    429 	}
    430 
    431 	/*
    432 	 * Allocate IRQ
    433 	 */
    434 	intrstr = pci_intr_string(sc->sc_pct, ih, intrbuf, sizeof(intrbuf));
    435 	sc->sc_irq_handle = pci_intr_establish_xname(pc, ih, IPL_NET, ale_intr,
    436 	    sc, device_xname(self));
    437 	if (sc->sc_irq_handle == NULL) {
    438 		aprint_error_dev(self, "could not establish interrupt");
    439 		if (intrstr != NULL)
    440 			aprint_error(" at %s", intrstr);
    441 		aprint_error("\n");
    442 		goto fail;
    443 	}
    444 
    445 	/* Set PHY address. */
    446 	sc->ale_phyaddr = ALE_PHY_ADDR;
    447 
    448 	/* Reset PHY. */
    449 	ale_phy_reset(sc);
    450 
    451 	/* Reset the ethernet controller. */
    452 	ale_reset(sc);
    453 
    454 	/* Get PCI and chip id/revision. */
    455 	sc->ale_rev = PCI_REVISION(pa->pa_class);
    456 	if (sc->ale_rev >= 0xF0) {
    457 		/* L2E Rev. B. AR8114 */
    458 		sc->ale_flags |= ALE_FLAG_FASTETHER;
    459 		chipname = "AR8114 (L2E RevB)";
    460 	} else {
    461 		if ((CSR_READ_4(sc, ALE_PHY_STATUS) & PHY_STATUS_100M) != 0) {
    462 			/* L1E AR8121 */
    463 			sc->ale_flags |= ALE_FLAG_JUMBO;
    464 			chipname = "AR8121 (L1E)";
    465 		} else {
    466 			/* L2E Rev. A. AR8113 */
    467 			sc->ale_flags |= ALE_FLAG_FASTETHER;
    468 			chipname = "AR8113 (L2E RevA)";
    469 		}
    470 	}
    471 	aprint_normal_dev(self, "%s, %s\n", chipname, intrstr);
    472 
    473 	/*
    474 	 * All known controllers seems to require 4 bytes alignment
    475 	 * of Tx buffers to make Tx checksum offload with custom
    476 	 * checksum generation method work.
    477 	 */
    478 	sc->ale_flags |= ALE_FLAG_TXCSUM_BUG;
    479 
    480 	/*
    481 	 * All known controllers seems to have issues on Rx checksum
    482 	 * offload for fragmented IP datagrams.
    483 	 */
    484 	sc->ale_flags |= ALE_FLAG_RXCSUM_BUG;
    485 
    486 	/*
    487 	 * Don't use Tx CMB. It is known to cause RRS update failure
    488 	 * under certain circumstances. Typical phenomenon of the
    489 	 * issue would be unexpected sequence number encountered in
    490 	 * Rx handler.
    491 	 */
    492 	sc->ale_flags |= ALE_FLAG_TXCMB_BUG;
    493 	sc->ale_chip_rev = CSR_READ_4(sc, ALE_MASTER_CFG) >>
    494 	    MASTER_CHIP_REV_SHIFT;
    495 	aprint_debug_dev(self, "PCI device revision : 0x%04x\n", sc->ale_rev);
    496 	aprint_debug_dev(self, "Chip id/revision : 0x%04x\n", sc->ale_chip_rev);
    497 
    498 	/*
    499 	 * Uninitialized hardware returns an invalid chip id/revision
    500 	 * as well as 0xFFFFFFFF for Tx/Rx fifo length.
    501 	 */
    502 	txf_len = CSR_READ_4(sc, ALE_SRAM_TX_FIFO_LEN);
    503 	rxf_len = CSR_READ_4(sc, ALE_SRAM_RX_FIFO_LEN);
    504 	if (sc->ale_chip_rev == 0xFFFF || txf_len == 0xFFFFFFFF ||
    505 	    rxf_len == 0xFFFFFFF) {
    506 		aprint_error_dev(self, "chip revision : 0x%04x, %u Tx FIFO "
    507 		    "%u Rx FIFO -- not initialized?\n",
    508 		    sc->ale_chip_rev, txf_len, rxf_len);
    509 		goto fail;
    510 	}
    511 
    512 	if (aledebug) {
    513 		printf("%s: %u Tx FIFO, %u Rx FIFO\n", device_xname(sc->sc_dev),
    514 		    txf_len, rxf_len);
    515 	}
    516 
    517 	/* Set max allowable DMA size. */
    518 	sc->ale_dma_rd_burst = DMA_CFG_RD_BURST_128;
    519 	sc->ale_dma_wr_burst = DMA_CFG_WR_BURST_128;
    520 
    521 	callout_init(&sc->sc_tick_ch, 0);
    522 	callout_setfunc(&sc->sc_tick_ch, ale_tick, sc);
    523 
    524 	error = ale_dma_alloc(sc);
    525 	if (error)
    526 		goto fail;
    527 
    528 	/* Load station address. */
    529 	ale_get_macaddr(sc);
    530 
    531 	aprint_normal_dev(self, "Ethernet address %s\n",
    532 	    ether_sprintf(sc->ale_eaddr));
    533 
    534 	ifp = &sc->sc_ec.ec_if;
    535 	ifp->if_softc = sc;
    536 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    537 	ifp->if_init = ale_init;
    538 	ifp->if_ioctl = ale_ioctl;
    539 	ifp->if_start = ale_start;
    540 	ifp->if_stop = ale_stop;
    541 	ifp->if_watchdog = ale_watchdog;
    542 	IFQ_SET_MAXLEN(&ifp->if_snd, ALE_TX_RING_CNT - 1);
    543 	IFQ_SET_READY(&ifp->if_snd);
    544 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
    545 
    546 	sc->sc_ec.ec_capabilities = ETHERCAP_VLAN_MTU;
    547 
    548 #ifdef ALE_CHECKSUM
    549 	ifp->if_capabilities |= IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx |
    550 				IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
    551 				IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx;
    552 #endif
    553 
    554 #if NVLAN > 0
    555 	sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_HWTAGGING;
    556 #endif
    557 
    558 	/* Set up MII bus. */
    559 	sc->sc_miibus.mii_ifp = ifp;
    560 	sc->sc_miibus.mii_readreg = ale_miibus_readreg;
    561 	sc->sc_miibus.mii_writereg = ale_miibus_writereg;
    562 	sc->sc_miibus.mii_statchg = ale_miibus_statchg;
    563 
    564 	sc->sc_ec.ec_mii = &sc->sc_miibus;
    565 	ifmedia_init(&sc->sc_miibus.mii_media, 0, ale_mediachange,
    566 	    ale_mediastatus);
    567 	mii_flags = 0;
    568 	if ((sc->ale_flags & ALE_FLAG_JUMBO) != 0)
    569 		mii_flags |= MIIF_DOPAUSE;
    570 	mii_attach(self, &sc->sc_miibus, 0xffffffff, MII_PHY_ANY,
    571 	    MII_OFFSET_ANY, mii_flags);
    572 
    573 	if (LIST_FIRST(&sc->sc_miibus.mii_phys) == NULL) {
    574 		aprint_error_dev(self, "no PHY found!\n");
    575 		ifmedia_add(&sc->sc_miibus.mii_media, IFM_ETHER | IFM_MANUAL,
    576 		    0, NULL);
    577 		ifmedia_set(&sc->sc_miibus.mii_media, IFM_ETHER | IFM_MANUAL);
    578 	} else
    579 		ifmedia_set(&sc->sc_miibus.mii_media, IFM_ETHER | IFM_AUTO);
    580 
    581 	if_attach(ifp);
    582 	if_deferred_start_init(ifp, NULL);
    583 	ether_ifattach(ifp, sc->ale_eaddr);
    584 
    585 	if (pmf_device_register(self, NULL, NULL))
    586 		pmf_class_network_register(self, ifp);
    587 	else
    588 		aprint_error_dev(self, "couldn't establish power handler\n");
    589 
    590 	return;
    591 fail:
    592 	ale_dma_free(sc);
    593 	if (sc->sc_irq_handle != NULL) {
    594 		pci_intr_disestablish(pc, sc->sc_irq_handle);
    595 		sc->sc_irq_handle = NULL;
    596 	}
    597 	if (sc->sc_mem_size) {
    598 		bus_space_unmap(sc->sc_mem_bt, sc->sc_mem_bh, sc->sc_mem_size);
    599 		sc->sc_mem_size = 0;
    600 	}
    601 }
    602 
    603 static int
    604 ale_detach(device_t self, int flags)
    605 {
    606 	struct ale_softc *sc = device_private(self);
    607 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    608 	int s;
    609 
    610 	pmf_device_deregister(self);
    611 	s = splnet();
    612 	ale_stop(ifp, 0);
    613 	splx(s);
    614 
    615 	mii_detach(&sc->sc_miibus, MII_PHY_ANY, MII_OFFSET_ANY);
    616 
    617 	/* Delete all remaining media. */
    618 	ifmedia_delete_instance(&sc->sc_miibus.mii_media, IFM_INST_ANY);
    619 
    620 	ether_ifdetach(ifp);
    621 	if_detach(ifp);
    622 	ale_dma_free(sc);
    623 
    624 	if (sc->sc_irq_handle != NULL) {
    625 		pci_intr_disestablish(sc->sc_pct, sc->sc_irq_handle);
    626 		sc->sc_irq_handle = NULL;
    627 	}
    628 	if (sc->sc_mem_size) {
    629 		bus_space_unmap(sc->sc_mem_bt, sc->sc_mem_bh, sc->sc_mem_size);
    630 		sc->sc_mem_size = 0;
    631 	}
    632 
    633 	return 0;
    634 }
    635 
    636 
    637 static int
    638 ale_dma_alloc(struct ale_softc *sc)
    639 {
    640 	struct ale_txdesc *txd;
    641 	int nsegs, error, guard_size, i;
    642 
    643 	if ((sc->ale_flags & ALE_FLAG_JUMBO) != 0)
    644 		guard_size = ALE_JUMBO_FRAMELEN;
    645 	else
    646 		guard_size = ALE_MAX_FRAMELEN;
    647 	sc->ale_pagesize = roundup(guard_size + ALE_RX_PAGE_SZ,
    648 	    ALE_RX_PAGE_ALIGN);
    649 
    650 	/*
    651 	 * Create DMA stuffs for TX ring
    652 	 */
    653 	error = bus_dmamap_create(sc->sc_dmat, ALE_TX_RING_SZ, 1,
    654 	    ALE_TX_RING_SZ, 0, BUS_DMA_NOWAIT, &sc->ale_cdata.ale_tx_ring_map);
    655 	if (error) {
    656 		sc->ale_cdata.ale_tx_ring_map = NULL;
    657 		return ENOBUFS;
    658 	}
    659 
    660 	/* Allocate DMA'able memory for TX ring */
    661 	error = bus_dmamem_alloc(sc->sc_dmat, ALE_TX_RING_SZ,
    662 	    0, 0, &sc->ale_cdata.ale_tx_ring_seg, 1,
    663 	    &nsegs, BUS_DMA_WAITOK);
    664 	if (error) {
    665 		printf("%s: could not allocate DMA'able memory for Tx ring, "
    666 		    "error = %i\n", device_xname(sc->sc_dev), error);
    667 		return error;
    668 	}
    669 
    670 	error = bus_dmamem_map(sc->sc_dmat, &sc->ale_cdata.ale_tx_ring_seg,
    671 	    nsegs, ALE_TX_RING_SZ, (void **)&sc->ale_cdata.ale_tx_ring,
    672 	    BUS_DMA_NOWAIT);
    673 	if (error)
    674 		return ENOBUFS;
    675 
    676 	memset(sc->ale_cdata.ale_tx_ring, 0, ALE_TX_RING_SZ);
    677 
    678 	/* Load the DMA map for Tx ring. */
    679 	error = bus_dmamap_load(sc->sc_dmat, sc->ale_cdata.ale_tx_ring_map,
    680 	    sc->ale_cdata.ale_tx_ring, ALE_TX_RING_SZ, NULL, BUS_DMA_WAITOK);
    681 	if (error) {
    682 		printf("%s: could not load DMA'able memory for Tx ring.\n",
    683 		    device_xname(sc->sc_dev));
    684 		bus_dmamem_free(sc->sc_dmat,
    685 		    &sc->ale_cdata.ale_tx_ring_seg, 1);
    686 		return error;
    687 	}
    688 	sc->ale_cdata.ale_tx_ring_paddr =
    689 	    sc->ale_cdata.ale_tx_ring_map->dm_segs[0].ds_addr;
    690 
    691 	for (i = 0; i < ALE_RX_PAGES; i++) {
    692 		/*
    693 		 * Create DMA stuffs for RX pages
    694 		 */
    695 		error = bus_dmamap_create(sc->sc_dmat, sc->ale_pagesize, 1,
    696 		    sc->ale_pagesize, 0, BUS_DMA_NOWAIT,
    697 		    &sc->ale_cdata.ale_rx_page[i].page_map);
    698 		if (error) {
    699 			sc->ale_cdata.ale_rx_page[i].page_map = NULL;
    700 			return ENOBUFS;
    701 		}
    702 
    703 		/* Allocate DMA'able memory for RX pages */
    704 		error = bus_dmamem_alloc(sc->sc_dmat, sc->ale_pagesize,
    705 		    ETHER_ALIGN, 0, &sc->ale_cdata.ale_rx_page[i].page_seg,
    706 		    1, &nsegs, BUS_DMA_WAITOK);
    707 		if (error) {
    708 			printf("%s: could not allocate DMA'able memory for "
    709 			    "Rx ring.\n", device_xname(sc->sc_dev));
    710 			return error;
    711 		}
    712 		error = bus_dmamem_map(sc->sc_dmat,
    713 		    &sc->ale_cdata.ale_rx_page[i].page_seg, nsegs,
    714 		    sc->ale_pagesize,
    715 		    (void **)&sc->ale_cdata.ale_rx_page[i].page_addr,
    716 		    BUS_DMA_NOWAIT);
    717 		if (error)
    718 			return ENOBUFS;
    719 
    720 		memset(sc->ale_cdata.ale_rx_page[i].page_addr, 0,
    721 		    sc->ale_pagesize);
    722 
    723 		/* Load the DMA map for Rx pages. */
    724 		error = bus_dmamap_load(sc->sc_dmat,
    725 		    sc->ale_cdata.ale_rx_page[i].page_map,
    726 		    sc->ale_cdata.ale_rx_page[i].page_addr,
    727 		    sc->ale_pagesize, NULL, BUS_DMA_WAITOK);
    728 		if (error) {
    729 			printf("%s: could not load DMA'able memory for "
    730 			    "Rx pages.\n", device_xname(sc->sc_dev));
    731 			bus_dmamem_free(sc->sc_dmat,
    732 			    &sc->ale_cdata.ale_rx_page[i].page_seg, 1);
    733 			return error;
    734 		}
    735 		sc->ale_cdata.ale_rx_page[i].page_paddr =
    736 		    sc->ale_cdata.ale_rx_page[i].page_map->dm_segs[0].ds_addr;
    737 	}
    738 
    739 	/*
    740 	 * Create DMA stuffs for Tx CMB.
    741 	 */
    742 	error = bus_dmamap_create(sc->sc_dmat, ALE_TX_CMB_SZ, 1,
    743 	    ALE_TX_CMB_SZ, 0, BUS_DMA_NOWAIT, &sc->ale_cdata.ale_tx_cmb_map);
    744 	if (error) {
    745 		sc->ale_cdata.ale_tx_cmb_map = NULL;
    746 		return ENOBUFS;
    747 	}
    748 
    749 	/* Allocate DMA'able memory for Tx CMB. */
    750 	error = bus_dmamem_alloc(sc->sc_dmat, ALE_TX_CMB_SZ, ETHER_ALIGN, 0,
    751 	    &sc->ale_cdata.ale_tx_cmb_seg, 1, &nsegs, BUS_DMA_WAITOK);
    752 
    753 	if (error) {
    754 		printf("%s: could not allocate DMA'able memory for Tx CMB.\n",
    755 		    device_xname(sc->sc_dev));
    756 		return error;
    757 	}
    758 
    759 	error = bus_dmamem_map(sc->sc_dmat, &sc->ale_cdata.ale_tx_cmb_seg,
    760 	    nsegs, ALE_TX_CMB_SZ, (void **)&sc->ale_cdata.ale_tx_cmb,
    761 	    BUS_DMA_NOWAIT);
    762 	if (error)
    763 		return ENOBUFS;
    764 
    765 	memset(sc->ale_cdata.ale_tx_cmb, 0, ALE_TX_CMB_SZ);
    766 
    767 	/* Load the DMA map for Tx CMB. */
    768 	error = bus_dmamap_load(sc->sc_dmat, sc->ale_cdata.ale_tx_cmb_map,
    769 	    sc->ale_cdata.ale_tx_cmb, ALE_TX_CMB_SZ, NULL, BUS_DMA_WAITOK);
    770 	if (error) {
    771 		printf("%s: could not load DMA'able memory for Tx CMB.\n",
    772 		    device_xname(sc->sc_dev));
    773 		bus_dmamem_free(sc->sc_dmat,
    774 		    &sc->ale_cdata.ale_tx_cmb_seg, 1);
    775 		return error;
    776 	}
    777 
    778 	sc->ale_cdata.ale_tx_cmb_paddr =
    779 	    sc->ale_cdata.ale_tx_cmb_map->dm_segs[0].ds_addr;
    780 
    781 	for (i = 0; i < ALE_RX_PAGES; i++) {
    782 		/*
    783 		 * Create DMA stuffs for Rx CMB.
    784 		 */
    785 		error = bus_dmamap_create(sc->sc_dmat, ALE_RX_CMB_SZ, 1,
    786 		    ALE_RX_CMB_SZ, 0, BUS_DMA_NOWAIT,
    787 		    &sc->ale_cdata.ale_rx_page[i].cmb_map);
    788 		if (error) {
    789 			sc->ale_cdata.ale_rx_page[i].cmb_map = NULL;
    790 			return ENOBUFS;
    791 		}
    792 
    793 		/* Allocate DMA'able memory for Rx CMB */
    794 		error = bus_dmamem_alloc(sc->sc_dmat, ALE_RX_CMB_SZ,
    795 		    ETHER_ALIGN, 0, &sc->ale_cdata.ale_rx_page[i].cmb_seg, 1,
    796 		    &nsegs, BUS_DMA_WAITOK);
    797 		if (error) {
    798 			printf("%s: could not allocate DMA'able memory for "
    799 			    "Rx CMB\n", device_xname(sc->sc_dev));
    800 			return error;
    801 		}
    802 		error = bus_dmamem_map(sc->sc_dmat,
    803 		    &sc->ale_cdata.ale_rx_page[i].cmb_seg, nsegs,
    804 		    ALE_RX_CMB_SZ,
    805 		    (void **)&sc->ale_cdata.ale_rx_page[i].cmb_addr,
    806 		    BUS_DMA_NOWAIT);
    807 		if (error)
    808 			return ENOBUFS;
    809 
    810 		memset(sc->ale_cdata.ale_rx_page[i].cmb_addr, 0, ALE_RX_CMB_SZ);
    811 
    812 		/* Load the DMA map for Rx CMB */
    813 		error = bus_dmamap_load(sc->sc_dmat,
    814 		    sc->ale_cdata.ale_rx_page[i].cmb_map,
    815 		    sc->ale_cdata.ale_rx_page[i].cmb_addr,
    816 		    ALE_RX_CMB_SZ, NULL, BUS_DMA_WAITOK);
    817 		if (error) {
    818 			printf("%s: could not load DMA'able memory for Rx CMB"
    819 			    "\n", device_xname(sc->sc_dev));
    820 			bus_dmamem_free(sc->sc_dmat,
    821 			    &sc->ale_cdata.ale_rx_page[i].cmb_seg, 1);
    822 			return error;
    823 		}
    824 		sc->ale_cdata.ale_rx_page[i].cmb_paddr =
    825 		    sc->ale_cdata.ale_rx_page[i].cmb_map->dm_segs[0].ds_addr;
    826 	}
    827 
    828 
    829 	/* Create DMA maps for Tx buffers. */
    830 	for (i = 0; i < ALE_TX_RING_CNT; i++) {
    831 		txd = &sc->ale_cdata.ale_txdesc[i];
    832 		txd->tx_m = NULL;
    833 		txd->tx_dmamap = NULL;
    834 		error = bus_dmamap_create(sc->sc_dmat, ALE_TSO_MAXSIZE,
    835 		    ALE_MAXTXSEGS, ALE_TSO_MAXSEGSIZE, 0, BUS_DMA_NOWAIT,
    836 		    &txd->tx_dmamap);
    837 		if (error) {
    838 			txd->tx_dmamap = NULL;
    839 			printf("%s: could not create Tx dmamap.\n",
    840 			    device_xname(sc->sc_dev));
    841 			return error;
    842 		}
    843 	}
    844 
    845 	return 0;
    846 }
    847 
    848 static void
    849 ale_dma_free(struct ale_softc *sc)
    850 {
    851 	struct ale_txdesc *txd;
    852 	int i;
    853 
    854 	/* Tx buffers. */
    855 	for (i = 0; i < ALE_TX_RING_CNT; i++) {
    856 		txd = &sc->ale_cdata.ale_txdesc[i];
    857 		if (txd->tx_dmamap != NULL) {
    858 			bus_dmamap_destroy(sc->sc_dmat, txd->tx_dmamap);
    859 			txd->tx_dmamap = NULL;
    860 		}
    861 	}
    862 
    863 	/* Tx descriptor ring. */
    864 	if (sc->ale_cdata.ale_tx_ring_map != NULL)
    865 		bus_dmamap_unload(sc->sc_dmat, sc->ale_cdata.ale_tx_ring_map);
    866 	if (sc->ale_cdata.ale_tx_ring_map != NULL &&
    867 	    sc->ale_cdata.ale_tx_ring != NULL)
    868 		bus_dmamem_free(sc->sc_dmat,
    869 		    &sc->ale_cdata.ale_tx_ring_seg, 1);
    870 	sc->ale_cdata.ale_tx_ring = NULL;
    871 	sc->ale_cdata.ale_tx_ring_map = NULL;
    872 
    873 	/* Rx page block. */
    874 	for (i = 0; i < ALE_RX_PAGES; i++) {
    875 		if (sc->ale_cdata.ale_rx_page[i].page_map != NULL)
    876 			bus_dmamap_unload(sc->sc_dmat,
    877 			    sc->ale_cdata.ale_rx_page[i].page_map);
    878 		if (sc->ale_cdata.ale_rx_page[i].page_map != NULL &&
    879 		    sc->ale_cdata.ale_rx_page[i].page_addr != NULL)
    880 			bus_dmamem_free(sc->sc_dmat,
    881 			    &sc->ale_cdata.ale_rx_page[i].page_seg, 1);
    882 		sc->ale_cdata.ale_rx_page[i].page_addr = NULL;
    883 		sc->ale_cdata.ale_rx_page[i].page_map = NULL;
    884 	}
    885 
    886 	/* Rx CMB. */
    887 	for (i = 0; i < ALE_RX_PAGES; i++) {
    888 		if (sc->ale_cdata.ale_rx_page[i].cmb_map != NULL)
    889 			bus_dmamap_unload(sc->sc_dmat,
    890 			    sc->ale_cdata.ale_rx_page[i].cmb_map);
    891 		if (sc->ale_cdata.ale_rx_page[i].cmb_map != NULL &&
    892 		    sc->ale_cdata.ale_rx_page[i].cmb_addr != NULL)
    893 			bus_dmamem_free(sc->sc_dmat,
    894 			    &sc->ale_cdata.ale_rx_page[i].cmb_seg, 1);
    895 		sc->ale_cdata.ale_rx_page[i].cmb_addr = NULL;
    896 		sc->ale_cdata.ale_rx_page[i].cmb_map = NULL;
    897 	}
    898 
    899 	/* Tx CMB. */
    900 	if (sc->ale_cdata.ale_tx_cmb_map != NULL)
    901 		bus_dmamap_unload(sc->sc_dmat, sc->ale_cdata.ale_tx_cmb_map);
    902 	if (sc->ale_cdata.ale_tx_cmb_map != NULL &&
    903 	    sc->ale_cdata.ale_tx_cmb != NULL)
    904 		bus_dmamem_free(sc->sc_dmat,
    905 		    &sc->ale_cdata.ale_tx_cmb_seg, 1);
    906 	sc->ale_cdata.ale_tx_cmb = NULL;
    907 	sc->ale_cdata.ale_tx_cmb_map = NULL;
    908 
    909 }
    910 
    911 static int
    912 ale_encap(struct ale_softc *sc, struct mbuf **m_head)
    913 {
    914 	struct ale_txdesc *txd, *txd_last;
    915 	struct tx_desc *desc;
    916 	struct mbuf *m;
    917 	bus_dmamap_t map;
    918 	uint32_t cflags, poff, vtag;
    919 	int error, i, nsegs, prod;
    920 
    921 	m = *m_head;
    922 	cflags = vtag = 0;
    923 	poff = 0;
    924 
    925 	prod = sc->ale_cdata.ale_tx_prod;
    926 	txd = &sc->ale_cdata.ale_txdesc[prod];
    927 	txd_last = txd;
    928 	map = txd->tx_dmamap;
    929 
    930 	error = bus_dmamap_load_mbuf(sc->sc_dmat, map, *m_head, BUS_DMA_NOWAIT);
    931 	if (error == EFBIG) {
    932 		error = 0;
    933 
    934 		*m_head = m_pullup(*m_head, MHLEN);
    935 		if (*m_head == NULL) {
    936 			printf("%s: can't defrag TX mbuf\n",
    937 			    device_xname(sc->sc_dev));
    938 			return ENOBUFS;
    939 		}
    940 
    941 		error = bus_dmamap_load_mbuf(sc->sc_dmat, map, *m_head,
    942 		    BUS_DMA_NOWAIT);
    943 
    944 		if (error != 0) {
    945 			printf("%s: could not load defragged TX mbuf\n",
    946 			    device_xname(sc->sc_dev));
    947 			m_freem(*m_head);
    948 			*m_head = NULL;
    949 			return error;
    950 		}
    951 	} else if (error) {
    952 		printf("%s: could not load TX mbuf\n", device_xname(sc->sc_dev));
    953 		return error;
    954 	}
    955 
    956 	nsegs = map->dm_nsegs;
    957 
    958 	if (nsegs == 0) {
    959 		m_freem(*m_head);
    960 		*m_head = NULL;
    961 		return EIO;
    962 	}
    963 
    964 	/* Check descriptor overrun. */
    965 	if (sc->ale_cdata.ale_tx_cnt + nsegs >= ALE_TX_RING_CNT - 2) {
    966 		bus_dmamap_unload(sc->sc_dmat, map);
    967 		return ENOBUFS;
    968 	}
    969 	bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
    970 	    BUS_DMASYNC_PREWRITE);
    971 
    972 	m = *m_head;
    973 	/* Configure Tx checksum offload. */
    974 	if ((m->m_pkthdr.csum_flags & ALE_CSUM_FEATURES) != 0) {
    975 		/*
    976 		 * AR81xx supports Tx custom checksum offload feature
    977 		 * that offloads single 16bit checksum computation.
    978 		 * So you can choose one among IP, TCP and UDP.
    979 		 * Normally driver sets checksum start/insertion
    980 		 * position from the information of TCP/UDP frame as
    981 		 * TCP/UDP checksum takes more time than that of IP.
    982 		 * However it seems that custom checksum offload
    983 		 * requires 4 bytes aligned Tx buffers due to hardware
    984 		 * bug.
    985 		 * AR81xx also supports explicit Tx checksum computation
    986 		 * if it is told that the size of IP header and TCP
    987 		 * header(for UDP, the header size does not matter
    988 		 * because it's fixed length). However with this scheme
    989 		 * TSO does not work so you have to choose one either
    990 		 * TSO or explicit Tx checksum offload. I chosen TSO
    991 		 * plus custom checksum offload with work-around which
    992 		 * will cover most common usage for this consumer
    993 		 * ethernet controller. The work-around takes a lot of
    994 		 * CPU cycles if Tx buffer is not aligned on 4 bytes
    995 		 * boundary, though.
    996 		 */
    997 		cflags |= ALE_TD_CXSUM;
    998 		/* Set checksum start offset. */
    999 		cflags |= (poff << ALE_TD_CSUM_PLOADOFFSET_SHIFT);
   1000 	}
   1001 
   1002 #if NVLAN > 0
   1003 	/* Configure VLAN hardware tag insertion. */
   1004 	if (vlan_has_tag(m)) {
   1005 		vtag = ALE_TX_VLAN_TAG(htons(vlan_get_tag(m)));
   1006 		vtag = ((vtag << ALE_TD_VLAN_SHIFT) & ALE_TD_VLAN_MASK);
   1007 		cflags |= ALE_TD_INSERT_VLAN_TAG;
   1008 	}
   1009 #endif
   1010 
   1011 	desc = NULL;
   1012 	for (i = 0; i < nsegs; i++) {
   1013 		desc = &sc->ale_cdata.ale_tx_ring[prod];
   1014 		desc->addr = htole64(map->dm_segs[i].ds_addr);
   1015 		desc->len =
   1016 		    htole32(ALE_TX_BYTES(map->dm_segs[i].ds_len) | vtag);
   1017 		desc->flags = htole32(cflags);
   1018 		sc->ale_cdata.ale_tx_cnt++;
   1019 		ALE_DESC_INC(prod, ALE_TX_RING_CNT);
   1020 	}
   1021 	/* Update producer index. */
   1022 	sc->ale_cdata.ale_tx_prod = prod;
   1023 
   1024 	/* Finally set EOP on the last descriptor. */
   1025 	prod = (prod + ALE_TX_RING_CNT - 1) % ALE_TX_RING_CNT;
   1026 	desc = &sc->ale_cdata.ale_tx_ring[prod];
   1027 	desc->flags |= htole32(ALE_TD_EOP);
   1028 
   1029 	/* Swap dmamap of the first and the last. */
   1030 	txd = &sc->ale_cdata.ale_txdesc[prod];
   1031 	map = txd_last->tx_dmamap;
   1032 	txd_last->tx_dmamap = txd->tx_dmamap;
   1033 	txd->tx_dmamap = map;
   1034 	txd->tx_m = m;
   1035 
   1036 	/* Sync descriptors. */
   1037 	bus_dmamap_sync(sc->sc_dmat, sc->ale_cdata.ale_tx_ring_map, 0,
   1038 	    sc->ale_cdata.ale_tx_ring_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
   1039 
   1040 	return 0;
   1041 }
   1042 
   1043 static void
   1044 ale_start(struct ifnet *ifp)
   1045 {
   1046         struct ale_softc *sc = ifp->if_softc;
   1047 	struct mbuf *m_head;
   1048 	int enq;
   1049 
   1050 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
   1051 		return;
   1052 
   1053 	/* Reclaim transmitted frames. */
   1054 	if (sc->ale_cdata.ale_tx_cnt >= ALE_TX_DESC_HIWAT)
   1055 		ale_txeof(sc);
   1056 
   1057 	enq = 0;
   1058 	for (;;) {
   1059 		IFQ_DEQUEUE(&ifp->if_snd, m_head);
   1060 		if (m_head == NULL)
   1061 			break;
   1062 
   1063 		/*
   1064 		 * Pack the data into the transmit ring. If we
   1065 		 * don't have room, set the OACTIVE flag and wait
   1066 		 * for the NIC to drain the ring.
   1067 		 */
   1068 		if (ale_encap(sc, &m_head)) {
   1069 			if (m_head == NULL)
   1070 				break;
   1071 			IF_PREPEND(&ifp->if_snd, m_head);
   1072 			ifp->if_flags |= IFF_OACTIVE;
   1073 			break;
   1074 		}
   1075 		enq = 1;
   1076 
   1077 		/*
   1078 		 * If there's a BPF listener, bounce a copy of this frame
   1079 		 * to him.
   1080 		 */
   1081 		bpf_mtap(ifp, m_head, BPF_D_OUT);
   1082 	}
   1083 
   1084 	if (enq) {
   1085 		/* Kick. */
   1086 		CSR_WRITE_4(sc, ALE_MBOX_TPD_PROD_IDX,
   1087 		    sc->ale_cdata.ale_tx_prod);
   1088 
   1089 		/* Set a timeout in case the chip goes out to lunch. */
   1090 		ifp->if_timer = ALE_TX_TIMEOUT;
   1091 	}
   1092 }
   1093 
   1094 static void
   1095 ale_watchdog(struct ifnet *ifp)
   1096 {
   1097 	struct ale_softc *sc = ifp->if_softc;
   1098 
   1099 	if ((sc->ale_flags & ALE_FLAG_LINK) == 0) {
   1100 		printf("%s: watchdog timeout (missed link)\n",
   1101 		    device_xname(sc->sc_dev));
   1102 		ifp->if_oerrors++;
   1103 		ale_init(ifp);
   1104 		return;
   1105 	}
   1106 
   1107 	printf("%s: watchdog timeout\n", device_xname(sc->sc_dev));
   1108 	ifp->if_oerrors++;
   1109 	ale_init(ifp);
   1110 
   1111 	if (!IFQ_IS_EMPTY(&ifp->if_snd))
   1112 		ale_start(ifp);
   1113 }
   1114 
   1115 static int
   1116 ale_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1117 {
   1118 	struct ale_softc *sc = ifp->if_softc;
   1119 	int s, error;
   1120 
   1121 	s = splnet();
   1122 
   1123 	error = ether_ioctl(ifp, cmd, data);
   1124 	if (error == ENETRESET) {
   1125 		if (ifp->if_flags & IFF_RUNNING)
   1126 			ale_rxfilter(sc);
   1127 		error = 0;
   1128 	}
   1129 
   1130 	splx(s);
   1131 	return error;
   1132 }
   1133 
   1134 static void
   1135 ale_mac_config(struct ale_softc *sc)
   1136 {
   1137 	struct mii_data *mii;
   1138 	uint32_t reg;
   1139 
   1140 	mii = &sc->sc_miibus;
   1141 	reg = CSR_READ_4(sc, ALE_MAC_CFG);
   1142 	reg &= ~(MAC_CFG_FULL_DUPLEX | MAC_CFG_TX_FC | MAC_CFG_RX_FC |
   1143 	    MAC_CFG_SPEED_MASK);
   1144 
   1145 	/* Reprogram MAC with resolved speed/duplex. */
   1146 	switch (IFM_SUBTYPE(mii->mii_media_active)) {
   1147 	case IFM_10_T:
   1148 	case IFM_100_TX:
   1149 		reg |= MAC_CFG_SPEED_10_100;
   1150 		break;
   1151 	case IFM_1000_T:
   1152 		reg |= MAC_CFG_SPEED_1000;
   1153 		break;
   1154 	}
   1155 	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
   1156 		reg |= MAC_CFG_FULL_DUPLEX;
   1157 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
   1158 			reg |= MAC_CFG_TX_FC;
   1159 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
   1160 			reg |= MAC_CFG_RX_FC;
   1161 	}
   1162 	CSR_WRITE_4(sc, ALE_MAC_CFG, reg);
   1163 }
   1164 
   1165 static void
   1166 ale_stats_clear(struct ale_softc *sc)
   1167 {
   1168 	struct smb sb;
   1169 	uint32_t *reg;
   1170 	int i;
   1171 
   1172 	for (reg = &sb.rx_frames, i = 0; reg <= &sb.rx_pkts_filtered; reg++) {
   1173 		CSR_READ_4(sc, ALE_RX_MIB_BASE + i);
   1174 		i += sizeof(uint32_t);
   1175 	}
   1176 	/* Read Tx statistics. */
   1177 	for (reg = &sb.tx_frames, i = 0; reg <= &sb.tx_mcast_bytes; reg++) {
   1178 		CSR_READ_4(sc, ALE_TX_MIB_BASE + i);
   1179 		i += sizeof(uint32_t);
   1180 	}
   1181 }
   1182 
   1183 static void
   1184 ale_stats_update(struct ale_softc *sc)
   1185 {
   1186 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1187 	struct ale_hw_stats *stat;
   1188 	struct smb sb, *smb;
   1189 	uint32_t *reg;
   1190 	int i;
   1191 
   1192 	stat = &sc->ale_stats;
   1193 	smb = &sb;
   1194 
   1195 	/* Read Rx statistics. */
   1196 	for (reg = &sb.rx_frames, i = 0; reg <= &sb.rx_pkts_filtered; reg++) {
   1197 		*reg = CSR_READ_4(sc, ALE_RX_MIB_BASE + i);
   1198 		i += sizeof(uint32_t);
   1199 	}
   1200 	/* Read Tx statistics. */
   1201 	for (reg = &sb.tx_frames, i = 0; reg <= &sb.tx_mcast_bytes; reg++) {
   1202 		*reg = CSR_READ_4(sc, ALE_TX_MIB_BASE + i);
   1203 		i += sizeof(uint32_t);
   1204 	}
   1205 
   1206 	/* Rx stats. */
   1207 	stat->rx_frames += smb->rx_frames;
   1208 	stat->rx_bcast_frames += smb->rx_bcast_frames;
   1209 	stat->rx_mcast_frames += smb->rx_mcast_frames;
   1210 	stat->rx_pause_frames += smb->rx_pause_frames;
   1211 	stat->rx_control_frames += smb->rx_control_frames;
   1212 	stat->rx_crcerrs += smb->rx_crcerrs;
   1213 	stat->rx_lenerrs += smb->rx_lenerrs;
   1214 	stat->rx_bytes += smb->rx_bytes;
   1215 	stat->rx_runts += smb->rx_runts;
   1216 	stat->rx_fragments += smb->rx_fragments;
   1217 	stat->rx_pkts_64 += smb->rx_pkts_64;
   1218 	stat->rx_pkts_65_127 += smb->rx_pkts_65_127;
   1219 	stat->rx_pkts_128_255 += smb->rx_pkts_128_255;
   1220 	stat->rx_pkts_256_511 += smb->rx_pkts_256_511;
   1221 	stat->rx_pkts_512_1023 += smb->rx_pkts_512_1023;
   1222 	stat->rx_pkts_1024_1518 += smb->rx_pkts_1024_1518;
   1223 	stat->rx_pkts_1519_max += smb->rx_pkts_1519_max;
   1224 	stat->rx_pkts_truncated += smb->rx_pkts_truncated;
   1225 	stat->rx_fifo_oflows += smb->rx_fifo_oflows;
   1226 	stat->rx_rrs_errs += smb->rx_rrs_errs;
   1227 	stat->rx_alignerrs += smb->rx_alignerrs;
   1228 	stat->rx_bcast_bytes += smb->rx_bcast_bytes;
   1229 	stat->rx_mcast_bytes += smb->rx_mcast_bytes;
   1230 	stat->rx_pkts_filtered += smb->rx_pkts_filtered;
   1231 
   1232 	/* Tx stats. */
   1233 	stat->tx_frames += smb->tx_frames;
   1234 	stat->tx_bcast_frames += smb->tx_bcast_frames;
   1235 	stat->tx_mcast_frames += smb->tx_mcast_frames;
   1236 	stat->tx_pause_frames += smb->tx_pause_frames;
   1237 	stat->tx_excess_defer += smb->tx_excess_defer;
   1238 	stat->tx_control_frames += smb->tx_control_frames;
   1239 	stat->tx_deferred += smb->tx_deferred;
   1240 	stat->tx_bytes += smb->tx_bytes;
   1241 	stat->tx_pkts_64 += smb->tx_pkts_64;
   1242 	stat->tx_pkts_65_127 += smb->tx_pkts_65_127;
   1243 	stat->tx_pkts_128_255 += smb->tx_pkts_128_255;
   1244 	stat->tx_pkts_256_511 += smb->tx_pkts_256_511;
   1245 	stat->tx_pkts_512_1023 += smb->tx_pkts_512_1023;
   1246 	stat->tx_pkts_1024_1518 += smb->tx_pkts_1024_1518;
   1247 	stat->tx_pkts_1519_max += smb->tx_pkts_1519_max;
   1248 	stat->tx_single_colls += smb->tx_single_colls;
   1249 	stat->tx_multi_colls += smb->tx_multi_colls;
   1250 	stat->tx_late_colls += smb->tx_late_colls;
   1251 	stat->tx_excess_colls += smb->tx_excess_colls;
   1252 	stat->tx_abort += smb->tx_abort;
   1253 	stat->tx_underrun += smb->tx_underrun;
   1254 	stat->tx_desc_underrun += smb->tx_desc_underrun;
   1255 	stat->tx_lenerrs += smb->tx_lenerrs;
   1256 	stat->tx_pkts_truncated += smb->tx_pkts_truncated;
   1257 	stat->tx_bcast_bytes += smb->tx_bcast_bytes;
   1258 	stat->tx_mcast_bytes += smb->tx_mcast_bytes;
   1259 
   1260 	/* Update counters in ifnet. */
   1261 	ifp->if_opackets += smb->tx_frames;
   1262 
   1263 	ifp->if_collisions += smb->tx_single_colls +
   1264 	    smb->tx_multi_colls * 2 + smb->tx_late_colls +
   1265 	    smb->tx_abort * HDPX_CFG_RETRY_DEFAULT;
   1266 
   1267 	/*
   1268 	 * XXX
   1269 	 * tx_pkts_truncated counter looks suspicious. It constantly
   1270 	 * increments with no sign of Tx errors. This may indicate
   1271 	 * the counter name is not correct one so I've removed the
   1272 	 * counter in output errors.
   1273 	 */
   1274 	ifp->if_oerrors += smb->tx_abort + smb->tx_late_colls +
   1275 	    smb->tx_underrun;
   1276 
   1277 	ifp->if_ipackets += smb->rx_frames;
   1278 
   1279 	ifp->if_ierrors += smb->rx_crcerrs + smb->rx_lenerrs +
   1280 	    smb->rx_runts + smb->rx_pkts_truncated +
   1281 	    smb->rx_fifo_oflows + smb->rx_rrs_errs +
   1282 	    smb->rx_alignerrs;
   1283 }
   1284 
   1285 static int
   1286 ale_intr(void *xsc)
   1287 {
   1288 	struct ale_softc *sc = xsc;
   1289 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1290 	uint32_t status;
   1291 
   1292 	status = CSR_READ_4(sc, ALE_INTR_STATUS);
   1293 	if ((status & ALE_INTRS) == 0)
   1294 		return 0;
   1295 
   1296 	/* Acknowledge and disable interrupts. */
   1297 	CSR_WRITE_4(sc, ALE_INTR_STATUS, status | INTR_DIS_INT);
   1298 
   1299 	if (ifp->if_flags & IFF_RUNNING) {
   1300 		int error;
   1301 
   1302 		error = ale_rxeof(sc);
   1303 		if (error) {
   1304 			sc->ale_stats.reset_brk_seq++;
   1305 			ale_init(ifp);
   1306 			return 0;
   1307 		}
   1308 
   1309 		if (status & (INTR_DMA_RD_TO_RST | INTR_DMA_WR_TO_RST)) {
   1310 			if (status & INTR_DMA_RD_TO_RST)
   1311 				printf("%s: DMA read error! -- resetting\n",
   1312 				    device_xname(sc->sc_dev));
   1313 			if (status & INTR_DMA_WR_TO_RST)
   1314 				printf("%s: DMA write error! -- resetting\n",
   1315 				    device_xname(sc->sc_dev));
   1316 			ale_init(ifp);
   1317 			return 0;
   1318 		}
   1319 
   1320 		ale_txeof(sc);
   1321 		if_schedule_deferred_start(ifp);
   1322 	}
   1323 
   1324 	/* Re-enable interrupts. */
   1325 	CSR_WRITE_4(sc, ALE_INTR_STATUS, 0x7FFFFFFF);
   1326 	return 1;
   1327 }
   1328 
   1329 static void
   1330 ale_txeof(struct ale_softc *sc)
   1331 {
   1332 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1333 	struct ale_txdesc *txd;
   1334 	uint32_t cons, prod;
   1335 	int prog;
   1336 
   1337 	if (sc->ale_cdata.ale_tx_cnt == 0)
   1338 		return;
   1339 
   1340 	bus_dmamap_sync(sc->sc_dmat, sc->ale_cdata.ale_tx_ring_map, 0,
   1341 	    sc->ale_cdata.ale_tx_ring_map->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1342 	if ((sc->ale_flags & ALE_FLAG_TXCMB_BUG) == 0) {
   1343 		bus_dmamap_sync(sc->sc_dmat, sc->ale_cdata.ale_tx_cmb_map, 0,
   1344 		    sc->ale_cdata.ale_tx_cmb_map->dm_mapsize,
   1345 		    BUS_DMASYNC_POSTREAD);
   1346 		prod = *sc->ale_cdata.ale_tx_cmb & TPD_CNT_MASK;
   1347 	} else
   1348 		prod = CSR_READ_2(sc, ALE_TPD_CONS_IDX);
   1349 	cons = sc->ale_cdata.ale_tx_cons;
   1350 	/*
   1351 	 * Go through our Tx list and free mbufs for those
   1352 	 * frames which have been transmitted.
   1353 	 */
   1354 	for (prog = 0; cons != prod; prog++,
   1355 	     ALE_DESC_INC(cons, ALE_TX_RING_CNT)) {
   1356 		if (sc->ale_cdata.ale_tx_cnt <= 0)
   1357 			break;
   1358 		prog++;
   1359 		ifp->if_flags &= ~IFF_OACTIVE;
   1360 		sc->ale_cdata.ale_tx_cnt--;
   1361 		txd = &sc->ale_cdata.ale_txdesc[cons];
   1362 		if (txd->tx_m != NULL) {
   1363 			/* Reclaim transmitted mbufs. */
   1364 			bus_dmamap_unload(sc->sc_dmat, txd->tx_dmamap);
   1365 			m_freem(txd->tx_m);
   1366 			txd->tx_m = NULL;
   1367 		}
   1368 	}
   1369 
   1370 	if (prog > 0) {
   1371 		sc->ale_cdata.ale_tx_cons = cons;
   1372 		/*
   1373 		 * Unarm watchdog timer only when there is no pending
   1374 		 * Tx descriptors in queue.
   1375 		 */
   1376 		if (sc->ale_cdata.ale_tx_cnt == 0)
   1377 			ifp->if_timer = 0;
   1378 	}
   1379 }
   1380 
   1381 static void
   1382 ale_rx_update_page(struct ale_softc *sc, struct ale_rx_page **page,
   1383     uint32_t length, uint32_t *prod)
   1384 {
   1385 	struct ale_rx_page *rx_page;
   1386 
   1387 	rx_page = *page;
   1388 	/* Update consumer position. */
   1389 	rx_page->cons += roundup(length + sizeof(struct rx_rs),
   1390 	    ALE_RX_PAGE_ALIGN);
   1391 	if (rx_page->cons >= ALE_RX_PAGE_SZ) {
   1392 		/*
   1393 		 * End of Rx page reached, let hardware reuse
   1394 		 * this page.
   1395 		 */
   1396 		rx_page->cons = 0;
   1397 		*rx_page->cmb_addr = 0;
   1398 		bus_dmamap_sync(sc->sc_dmat, rx_page->cmb_map, 0,
   1399 		    rx_page->cmb_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
   1400 		CSR_WRITE_1(sc, ALE_RXF0_PAGE0 + sc->ale_cdata.ale_rx_curp,
   1401 		    RXF_VALID);
   1402 		/* Switch to alternate Rx page. */
   1403 		sc->ale_cdata.ale_rx_curp ^= 1;
   1404 		rx_page = *page =
   1405 		    &sc->ale_cdata.ale_rx_page[sc->ale_cdata.ale_rx_curp];
   1406 		/* Page flipped, sync CMB and Rx page. */
   1407 		bus_dmamap_sync(sc->sc_dmat, rx_page->page_map, 0,
   1408 		    rx_page->page_map->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1409 		bus_dmamap_sync(sc->sc_dmat, rx_page->cmb_map, 0,
   1410 		    rx_page->cmb_map->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1411 		/* Sync completed, cache updated producer index. */
   1412 		*prod = *rx_page->cmb_addr;
   1413 	}
   1414 }
   1415 
   1416 
   1417 /*
   1418  * It seems that AR81xx controller can compute partial checksum.
   1419  * The partial checksum value can be used to accelerate checksum
   1420  * computation for fragmented TCP/UDP packets. Upper network stack
   1421  * already takes advantage of the partial checksum value in IP
   1422  * reassembly stage. But I'm not sure the correctness of the
   1423  * partial hardware checksum assistance due to lack of data sheet.
   1424  * In addition, the Rx feature of controller that requires copying
   1425  * for every frames effectively nullifies one of most nice offload
   1426  * capability of controller.
   1427  */
   1428 static void
   1429 ale_rxcsum(struct ale_softc *sc, struct mbuf *m, uint32_t status)
   1430 {
   1431 	if (status & ALE_RD_IPCSUM_NOK)
   1432 		m->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD;
   1433 
   1434 	if ((sc->ale_flags & ALE_FLAG_RXCSUM_BUG) == 0) {
   1435 		if (((status & ALE_RD_IPV4_FRAG) == 0) &&
   1436 		    ((status & (ALE_RD_TCP | ALE_RD_UDP)) != 0) &&
   1437 		    (status & ALE_RD_TCP_UDPCSUM_NOK))
   1438 		{
   1439 			m->m_pkthdr.csum_flags |= M_CSUM_TCP_UDP_BAD;
   1440 		}
   1441 	} else {
   1442 		if ((status & (ALE_RD_TCP | ALE_RD_UDP)) != 0) {
   1443 			if (status & ALE_RD_TCP_UDPCSUM_NOK) {
   1444 				m->m_pkthdr.csum_flags |= M_CSUM_TCP_UDP_BAD;
   1445 			}
   1446 		}
   1447 	}
   1448 	/*
   1449 	 * Don't mark bad checksum for TCP/UDP frames
   1450 	 * as fragmented frames may always have set
   1451 	 * bad checksummed bit of frame status.
   1452 	 */
   1453 }
   1454 
   1455 /* Process received frames. */
   1456 static int
   1457 ale_rxeof(struct ale_softc *sc)
   1458 {
   1459 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1460 	struct ale_rx_page *rx_page;
   1461 	struct rx_rs *rs;
   1462 	struct mbuf *m;
   1463 	uint32_t length, prod, seqno, status;
   1464 	int prog;
   1465 
   1466 	rx_page = &sc->ale_cdata.ale_rx_page[sc->ale_cdata.ale_rx_curp];
   1467 	bus_dmamap_sync(sc->sc_dmat, rx_page->cmb_map, 0,
   1468 	    rx_page->cmb_map->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1469 	bus_dmamap_sync(sc->sc_dmat, rx_page->page_map, 0,
   1470 	    rx_page->page_map->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1471 	/*
   1472 	 * Don't directly access producer index as hardware may
   1473 	 * update it while Rx handler is in progress. It would
   1474 	 * be even better if there is a way to let hardware
   1475 	 * know how far driver processed its received frames.
   1476 	 * Alternatively, hardware could provide a way to disable
   1477 	 * CMB updates until driver acknowledges the end of CMB
   1478 	 * access.
   1479 	 */
   1480 	prod = *rx_page->cmb_addr;
   1481 	for (prog = 0; ; prog++) {
   1482 		if (rx_page->cons >= prod)
   1483 			break;
   1484 		rs = (struct rx_rs *)(rx_page->page_addr + rx_page->cons);
   1485 		seqno = ALE_RX_SEQNO(le32toh(rs->seqno));
   1486 		if (sc->ale_cdata.ale_rx_seqno != seqno) {
   1487 			/*
   1488 			 * Normally I believe this should not happen unless
   1489 			 * severe driver bug or corrupted memory. However
   1490 			 * it seems to happen under certain conditions which
   1491 			 * is triggered by abrupt Rx events such as initiation
   1492 			 * of bulk transfer of remote host. It's not easy to
   1493 			 * reproduce this and I doubt it could be related
   1494 			 * with FIFO overflow of hardware or activity of Tx
   1495 			 * CMB updates. I also remember similar behaviour
   1496 			 * seen on RealTek 8139 which uses resembling Rx
   1497 			 * scheme.
   1498 			 */
   1499 			if (aledebug)
   1500 				printf("%s: garbled seq: %u, expected: %u -- "
   1501 				    "resetting!\n", device_xname(sc->sc_dev),
   1502 				    seqno, sc->ale_cdata.ale_rx_seqno);
   1503 			return EIO;
   1504 		}
   1505 		/* Frame received. */
   1506 		sc->ale_cdata.ale_rx_seqno++;
   1507 		length = ALE_RX_BYTES(le32toh(rs->length));
   1508 		status = le32toh(rs->flags);
   1509 		if (status & ALE_RD_ERROR) {
   1510 			/*
   1511 			 * We want to pass the following frames to upper
   1512 			 * layer regardless of error status of Rx return
   1513 			 * status.
   1514 			 *
   1515 			 *  o IP/TCP/UDP checksum is bad.
   1516 			 *  o frame length and protocol specific length
   1517 			 *     does not match.
   1518 			 */
   1519 			if (status & (ALE_RD_CRC | ALE_RD_CODE |
   1520 			    ALE_RD_DRIBBLE | ALE_RD_RUNT | ALE_RD_OFLOW |
   1521 			    ALE_RD_TRUNC)) {
   1522 				ale_rx_update_page(sc, &rx_page, length, &prod);
   1523 				continue;
   1524 			}
   1525 		}
   1526 		/*
   1527 		 * m_devget(9) is major bottle-neck of ale(4)(It comes
   1528 		 * from hardware limitation). For jumbo frames we could
   1529 		 * get a slightly better performance if driver use
   1530 		 * m_getjcl(9) with proper buffer size argument. However
   1531 		 * that would make code more complicated and I don't
   1532 		 * think users would expect good Rx performance numbers
   1533 		 * on these low-end consumer ethernet controller.
   1534 		 */
   1535 		m = m_devget((char *)(rs + 1), length - ETHER_CRC_LEN,
   1536 		    0, ifp);
   1537 		if (m == NULL) {
   1538 			ifp->if_iqdrops++;
   1539 			ale_rx_update_page(sc, &rx_page, length, &prod);
   1540 			continue;
   1541 		}
   1542 		if (status & ALE_RD_IPV4)
   1543 			ale_rxcsum(sc, m, status);
   1544 #if NVLAN > 0
   1545 		if (status & ALE_RD_VLAN) {
   1546 			uint32_t vtags = ALE_RX_VLAN(le32toh(rs->vtags));
   1547 			vlan_set_tag(m, ALE_RX_VLAN_TAG(vtags));
   1548 		}
   1549 #endif
   1550 
   1551 		/* Pass it to upper layer. */
   1552 		if_percpuq_enqueue(ifp->if_percpuq, m);
   1553 
   1554 		ale_rx_update_page(sc, &rx_page, length, &prod);
   1555 	}
   1556 
   1557 	return 0;
   1558 }
   1559 
   1560 static void
   1561 ale_tick(void *xsc)
   1562 {
   1563 	struct ale_softc *sc = xsc;
   1564 	struct mii_data *mii = &sc->sc_miibus;
   1565 	int s;
   1566 
   1567 	s = splnet();
   1568 	mii_tick(mii);
   1569 	ale_stats_update(sc);
   1570 	splx(s);
   1571 
   1572 	callout_schedule(&sc->sc_tick_ch, hz);
   1573 }
   1574 
   1575 static void
   1576 ale_reset(struct ale_softc *sc)
   1577 {
   1578 	uint32_t reg;
   1579 	int i;
   1580 
   1581 	/* Initialize PCIe module. From Linux. */
   1582 	CSR_WRITE_4(sc, 0x1008, CSR_READ_4(sc, 0x1008) | 0x8000);
   1583 
   1584 	CSR_WRITE_4(sc, ALE_MASTER_CFG, MASTER_RESET);
   1585 	for (i = ALE_RESET_TIMEOUT; i > 0; i--) {
   1586 		DELAY(10);
   1587 		if ((CSR_READ_4(sc, ALE_MASTER_CFG) & MASTER_RESET) == 0)
   1588 			break;
   1589 	}
   1590 	if (i == 0)
   1591 		printf("%s: master reset timeout!\n", device_xname(sc->sc_dev));
   1592 
   1593 	for (i = ALE_RESET_TIMEOUT; i > 0; i--) {
   1594 		if ((reg = CSR_READ_4(sc, ALE_IDLE_STATUS)) == 0)
   1595 			break;
   1596 		DELAY(10);
   1597 	}
   1598 
   1599 	if (i == 0)
   1600 		printf("%s: reset timeout(0x%08x)!\n", device_xname(sc->sc_dev),
   1601 		    reg);
   1602 }
   1603 
   1604 static int
   1605 ale_init(struct ifnet *ifp)
   1606 {
   1607 	struct ale_softc *sc = ifp->if_softc;
   1608 	struct mii_data *mii;
   1609 	uint8_t eaddr[ETHER_ADDR_LEN];
   1610 	bus_addr_t paddr;
   1611 	uint32_t reg, rxf_hi, rxf_lo;
   1612 
   1613 	/*
   1614 	 * Cancel any pending I/O.
   1615 	 */
   1616 	ale_stop(ifp, 0);
   1617 
   1618 	/*
   1619 	 * Reset the chip to a known state.
   1620 	 */
   1621 	ale_reset(sc);
   1622 
   1623 	/* Initialize Tx descriptors, DMA memory blocks. */
   1624 	ale_init_rx_pages(sc);
   1625 	ale_init_tx_ring(sc);
   1626 
   1627 	/* Reprogram the station address. */
   1628 	memcpy(eaddr, CLLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
   1629 	CSR_WRITE_4(sc, ALE_PAR0,
   1630 	    eaddr[2] << 24 | eaddr[3] << 16 | eaddr[4] << 8 | eaddr[5]);
   1631 	CSR_WRITE_4(sc, ALE_PAR1, eaddr[0] << 8 | eaddr[1]);
   1632 
   1633 	/*
   1634 	 * Clear WOL status and disable all WOL feature as WOL
   1635 	 * would interfere Rx operation under normal environments.
   1636 	 */
   1637 	CSR_READ_4(sc, ALE_WOL_CFG);
   1638 	CSR_WRITE_4(sc, ALE_WOL_CFG, 0);
   1639 
   1640 	/*
   1641 	 * Set Tx descriptor/RXF0/CMB base addresses. They share
   1642 	 * the same high address part of DMAable region.
   1643 	 */
   1644 	paddr = sc->ale_cdata.ale_tx_ring_paddr;
   1645 	CSR_WRITE_4(sc, ALE_TPD_ADDR_HI, ALE_ADDR_HI(paddr));
   1646 	CSR_WRITE_4(sc, ALE_TPD_ADDR_LO, ALE_ADDR_LO(paddr));
   1647 	CSR_WRITE_4(sc, ALE_TPD_CNT,
   1648 	    (ALE_TX_RING_CNT << TPD_CNT_SHIFT) & TPD_CNT_MASK);
   1649 
   1650 	/* Set Rx page base address, note we use single queue. */
   1651 	paddr = sc->ale_cdata.ale_rx_page[0].page_paddr;
   1652 	CSR_WRITE_4(sc, ALE_RXF0_PAGE0_ADDR_LO, ALE_ADDR_LO(paddr));
   1653 	paddr = sc->ale_cdata.ale_rx_page[1].page_paddr;
   1654 	CSR_WRITE_4(sc, ALE_RXF0_PAGE1_ADDR_LO, ALE_ADDR_LO(paddr));
   1655 
   1656 	/* Set Tx/Rx CMB addresses. */
   1657 	paddr = sc->ale_cdata.ale_tx_cmb_paddr;
   1658 	CSR_WRITE_4(sc, ALE_TX_CMB_ADDR_LO, ALE_ADDR_LO(paddr));
   1659 	paddr = sc->ale_cdata.ale_rx_page[0].cmb_paddr;
   1660 	CSR_WRITE_4(sc, ALE_RXF0_CMB0_ADDR_LO, ALE_ADDR_LO(paddr));
   1661 	paddr = sc->ale_cdata.ale_rx_page[1].cmb_paddr;
   1662 	CSR_WRITE_4(sc, ALE_RXF0_CMB1_ADDR_LO, ALE_ADDR_LO(paddr));
   1663 
   1664 	/* Mark RXF0 is valid. */
   1665 	CSR_WRITE_1(sc, ALE_RXF0_PAGE0, RXF_VALID);
   1666 	CSR_WRITE_1(sc, ALE_RXF0_PAGE1, RXF_VALID);
   1667 	/*
   1668 	 * No need to initialize RFX1/RXF2/RXF3. We don't use
   1669 	 * multi-queue yet.
   1670 	 */
   1671 
   1672 	/* Set Rx page size, excluding guard frame size. */
   1673 	CSR_WRITE_4(sc, ALE_RXF_PAGE_SIZE, ALE_RX_PAGE_SZ);
   1674 
   1675 	/* Tell hardware that we're ready to load DMA blocks. */
   1676 	CSR_WRITE_4(sc, ALE_DMA_BLOCK, DMA_BLOCK_LOAD);
   1677 
   1678 	/* Set Rx/Tx interrupt trigger threshold. */
   1679 	CSR_WRITE_4(sc, ALE_INT_TRIG_THRESH, (1 << INT_TRIG_RX_THRESH_SHIFT) |
   1680 	    (4 << INT_TRIG_TX_THRESH_SHIFT));
   1681 	/*
   1682 	 * XXX
   1683 	 * Set interrupt trigger timer, its purpose and relation
   1684 	 * with interrupt moderation mechanism is not clear yet.
   1685 	 */
   1686 	CSR_WRITE_4(sc, ALE_INT_TRIG_TIMER,
   1687 	    ((ALE_USECS(10) << INT_TRIG_RX_TIMER_SHIFT) |
   1688 	    (ALE_USECS(1000) << INT_TRIG_TX_TIMER_SHIFT)));
   1689 
   1690 	/* Configure interrupt moderation timer. */
   1691 	sc->ale_int_rx_mod = ALE_IM_RX_TIMER_DEFAULT;
   1692 	sc->ale_int_tx_mod = ALE_IM_TX_TIMER_DEFAULT;
   1693 	reg = ALE_USECS(sc->ale_int_rx_mod) << IM_TIMER_RX_SHIFT;
   1694 	reg |= ALE_USECS(sc->ale_int_tx_mod) << IM_TIMER_TX_SHIFT;
   1695 	CSR_WRITE_4(sc, ALE_IM_TIMER, reg);
   1696 	reg = CSR_READ_4(sc, ALE_MASTER_CFG);
   1697 	reg &= ~(MASTER_CHIP_REV_MASK | MASTER_CHIP_ID_MASK);
   1698 	reg &= ~(MASTER_IM_RX_TIMER_ENB | MASTER_IM_TX_TIMER_ENB);
   1699 	if (ALE_USECS(sc->ale_int_rx_mod) != 0)
   1700 		reg |= MASTER_IM_RX_TIMER_ENB;
   1701 	if (ALE_USECS(sc->ale_int_tx_mod) != 0)
   1702 		reg |= MASTER_IM_TX_TIMER_ENB;
   1703 	CSR_WRITE_4(sc, ALE_MASTER_CFG, reg);
   1704 	CSR_WRITE_2(sc, ALE_INTR_CLR_TIMER, ALE_USECS(1000));
   1705 
   1706 	/* Set Maximum frame size of controller. */
   1707 	if (ifp->if_mtu < ETHERMTU)
   1708 		sc->ale_max_frame_size = ETHERMTU;
   1709 	else
   1710 		sc->ale_max_frame_size = ifp->if_mtu;
   1711 	sc->ale_max_frame_size += ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN + ETHER_CRC_LEN;
   1712 	CSR_WRITE_4(sc, ALE_FRAME_SIZE, sc->ale_max_frame_size);
   1713 
   1714 	/* Configure IPG/IFG parameters. */
   1715 	CSR_WRITE_4(sc, ALE_IPG_IFG_CFG,
   1716 	    ((IPG_IFG_IPGT_DEFAULT << IPG_IFG_IPGT_SHIFT) & IPG_IFG_IPGT_MASK) |
   1717 	    ((IPG_IFG_MIFG_DEFAULT << IPG_IFG_MIFG_SHIFT) & IPG_IFG_MIFG_MASK) |
   1718 	    ((IPG_IFG_IPG1_DEFAULT << IPG_IFG_IPG1_SHIFT) & IPG_IFG_IPG1_MASK) |
   1719 	    ((IPG_IFG_IPG2_DEFAULT << IPG_IFG_IPG2_SHIFT) & IPG_IFG_IPG2_MASK));
   1720 
   1721 	/* Set parameters for half-duplex media. */
   1722 	CSR_WRITE_4(sc, ALE_HDPX_CFG,
   1723 	    ((HDPX_CFG_LCOL_DEFAULT << HDPX_CFG_LCOL_SHIFT) &
   1724 	    HDPX_CFG_LCOL_MASK) |
   1725 	    ((HDPX_CFG_RETRY_DEFAULT << HDPX_CFG_RETRY_SHIFT) &
   1726 	    HDPX_CFG_RETRY_MASK) | HDPX_CFG_EXC_DEF_EN |
   1727 	    ((HDPX_CFG_ABEBT_DEFAULT << HDPX_CFG_ABEBT_SHIFT) &
   1728 	    HDPX_CFG_ABEBT_MASK) |
   1729 	    ((HDPX_CFG_JAMIPG_DEFAULT << HDPX_CFG_JAMIPG_SHIFT) &
   1730 	    HDPX_CFG_JAMIPG_MASK));
   1731 
   1732 	/* Configure Tx jumbo frame parameters. */
   1733 	if ((sc->ale_flags & ALE_FLAG_JUMBO) != 0) {
   1734 		if (ifp->if_mtu < ETHERMTU)
   1735 			reg = sc->ale_max_frame_size;
   1736 		else if (ifp->if_mtu < 6 * 1024)
   1737 			reg = (sc->ale_max_frame_size * 2) / 3;
   1738 		else
   1739 			reg = sc->ale_max_frame_size / 2;
   1740 		CSR_WRITE_4(sc, ALE_TX_JUMBO_THRESH,
   1741 		    roundup(reg, TX_JUMBO_THRESH_UNIT) >>
   1742 		    TX_JUMBO_THRESH_UNIT_SHIFT);
   1743 	}
   1744 
   1745 	/* Configure TxQ. */
   1746 	reg = (128 << (sc->ale_dma_rd_burst >> DMA_CFG_RD_BURST_SHIFT))
   1747 	    << TXQ_CFG_TX_FIFO_BURST_SHIFT;
   1748 	reg |= (TXQ_CFG_TPD_BURST_DEFAULT << TXQ_CFG_TPD_BURST_SHIFT) &
   1749 	    TXQ_CFG_TPD_BURST_MASK;
   1750 	CSR_WRITE_4(sc, ALE_TXQ_CFG, reg | TXQ_CFG_ENHANCED_MODE | TXQ_CFG_ENB);
   1751 
   1752 	/* Configure Rx jumbo frame & flow control parameters. */
   1753 	if ((sc->ale_flags & ALE_FLAG_JUMBO) != 0) {
   1754 		reg = roundup(sc->ale_max_frame_size, RX_JUMBO_THRESH_UNIT);
   1755 		CSR_WRITE_4(sc, ALE_RX_JUMBO_THRESH,
   1756 		    (((reg >> RX_JUMBO_THRESH_UNIT_SHIFT) <<
   1757 		    RX_JUMBO_THRESH_MASK_SHIFT) & RX_JUMBO_THRESH_MASK) |
   1758 		    ((RX_JUMBO_LKAH_DEFAULT << RX_JUMBO_LKAH_SHIFT) &
   1759 		    RX_JUMBO_LKAH_MASK));
   1760 		reg = CSR_READ_4(sc, ALE_SRAM_RX_FIFO_LEN);
   1761 		rxf_hi = (reg * 7) / 10;
   1762 		rxf_lo = (reg * 3)/ 10;
   1763 		CSR_WRITE_4(sc, ALE_RX_FIFO_PAUSE_THRESH,
   1764 		    ((rxf_lo << RX_FIFO_PAUSE_THRESH_LO_SHIFT) &
   1765 		    RX_FIFO_PAUSE_THRESH_LO_MASK) |
   1766 		    ((rxf_hi << RX_FIFO_PAUSE_THRESH_HI_SHIFT) &
   1767 		     RX_FIFO_PAUSE_THRESH_HI_MASK));
   1768 	}
   1769 
   1770 	/* Disable RSS. */
   1771 	CSR_WRITE_4(sc, ALE_RSS_IDT_TABLE0, 0);
   1772 	CSR_WRITE_4(sc, ALE_RSS_CPU, 0);
   1773 
   1774 	/* Configure RxQ. */
   1775 	CSR_WRITE_4(sc, ALE_RXQ_CFG,
   1776 	    RXQ_CFG_ALIGN_32 | RXQ_CFG_CUT_THROUGH_ENB | RXQ_CFG_ENB);
   1777 
   1778 	/* Configure DMA parameters. */
   1779 	reg = 0;
   1780 	if ((sc->ale_flags & ALE_FLAG_TXCMB_BUG) == 0)
   1781 		reg |= DMA_CFG_TXCMB_ENB;
   1782 	CSR_WRITE_4(sc, ALE_DMA_CFG,
   1783 	    DMA_CFG_OUT_ORDER | DMA_CFG_RD_REQ_PRI | DMA_CFG_RCB_64 |
   1784 	    sc->ale_dma_rd_burst | reg |
   1785 	    sc->ale_dma_wr_burst | DMA_CFG_RXCMB_ENB |
   1786 	    ((DMA_CFG_RD_DELAY_CNT_DEFAULT << DMA_CFG_RD_DELAY_CNT_SHIFT) &
   1787 	    DMA_CFG_RD_DELAY_CNT_MASK) |
   1788 	    ((DMA_CFG_WR_DELAY_CNT_DEFAULT << DMA_CFG_WR_DELAY_CNT_SHIFT) &
   1789 	    DMA_CFG_WR_DELAY_CNT_MASK));
   1790 
   1791 	/*
   1792 	 * Hardware can be configured to issue SMB interrupt based
   1793 	 * on programmed interval. Since there is a callout that is
   1794 	 * invoked for every hz in driver we use that instead of
   1795 	 * relying on periodic SMB interrupt.
   1796 	 */
   1797 	CSR_WRITE_4(sc, ALE_SMB_STAT_TIMER, ALE_USECS(0));
   1798 
   1799 	/* Clear MAC statistics. */
   1800 	ale_stats_clear(sc);
   1801 
   1802 	/*
   1803 	 * Configure Tx/Rx MACs.
   1804 	 *  - Auto-padding for short frames.
   1805 	 *  - Enable CRC generation.
   1806 	 *  Actual reconfiguration of MAC for resolved speed/duplex
   1807 	 *  is followed after detection of link establishment.
   1808 	 *  AR81xx always does checksum computation regardless of
   1809 	 *  MAC_CFG_RXCSUM_ENB bit. In fact, setting the bit will
   1810 	 *  cause Rx handling issue for fragmented IP datagrams due
   1811 	 *  to silicon bug.
   1812 	 */
   1813 	reg = MAC_CFG_TX_CRC_ENB | MAC_CFG_TX_AUTO_PAD | MAC_CFG_FULL_DUPLEX |
   1814 	    ((MAC_CFG_PREAMBLE_DEFAULT << MAC_CFG_PREAMBLE_SHIFT) &
   1815 	    MAC_CFG_PREAMBLE_MASK);
   1816 	if ((sc->ale_flags & ALE_FLAG_FASTETHER) != 0)
   1817 		reg |= MAC_CFG_SPEED_10_100;
   1818 	else
   1819 		reg |= MAC_CFG_SPEED_1000;
   1820 	CSR_WRITE_4(sc, ALE_MAC_CFG, reg);
   1821 
   1822 	/* Set up the receive filter. */
   1823 	ale_rxfilter(sc);
   1824 	ale_rxvlan(sc);
   1825 
   1826 	/* Acknowledge all pending interrupts and clear it. */
   1827 	CSR_WRITE_4(sc, ALE_INTR_MASK, ALE_INTRS);
   1828 	CSR_WRITE_4(sc, ALE_INTR_STATUS, 0xFFFFFFFF);
   1829 	CSR_WRITE_4(sc, ALE_INTR_STATUS, 0);
   1830 
   1831 	sc->ale_flags &= ~ALE_FLAG_LINK;
   1832 
   1833 	/* Switch to the current media. */
   1834 	mii = &sc->sc_miibus;
   1835 	mii_mediachg(mii);
   1836 
   1837 	callout_schedule(&sc->sc_tick_ch, hz);
   1838 
   1839 	ifp->if_flags |= IFF_RUNNING;
   1840 	ifp->if_flags &= ~IFF_OACTIVE;
   1841 
   1842 	return 0;
   1843 }
   1844 
   1845 static void
   1846 ale_stop(struct ifnet *ifp, int disable)
   1847 {
   1848 	struct ale_softc *sc = ifp->if_softc;
   1849 	struct ale_txdesc *txd;
   1850 	uint32_t reg;
   1851 	int i;
   1852 
   1853 	callout_stop(&sc->sc_tick_ch);
   1854 
   1855 	/*
   1856 	 * Mark the interface down and cancel the watchdog timer.
   1857 	 */
   1858 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1859 	ifp->if_timer = 0;
   1860 
   1861 	sc->ale_flags &= ~ALE_FLAG_LINK;
   1862 
   1863 	ale_stats_update(sc);
   1864 
   1865 	mii_down(&sc->sc_miibus);
   1866 
   1867 	/* Disable interrupts. */
   1868 	CSR_WRITE_4(sc, ALE_INTR_MASK, 0);
   1869 	CSR_WRITE_4(sc, ALE_INTR_STATUS, 0xFFFFFFFF);
   1870 
   1871 	/* Disable queue processing and DMA. */
   1872 	reg = CSR_READ_4(sc, ALE_TXQ_CFG);
   1873 	reg &= ~TXQ_CFG_ENB;
   1874 	CSR_WRITE_4(sc, ALE_TXQ_CFG, reg);
   1875 	reg = CSR_READ_4(sc, ALE_RXQ_CFG);
   1876 	reg &= ~RXQ_CFG_ENB;
   1877 	CSR_WRITE_4(sc, ALE_RXQ_CFG, reg);
   1878 	reg = CSR_READ_4(sc, ALE_DMA_CFG);
   1879 	reg &= ~(DMA_CFG_TXCMB_ENB | DMA_CFG_RXCMB_ENB);
   1880 	CSR_WRITE_4(sc, ALE_DMA_CFG, reg);
   1881 	DELAY(1000);
   1882 
   1883 	/* Stop Rx/Tx MACs. */
   1884 	ale_stop_mac(sc);
   1885 
   1886 	/* Disable interrupts again? XXX */
   1887 	CSR_WRITE_4(sc, ALE_INTR_STATUS, 0xFFFFFFFF);
   1888 
   1889 	/*
   1890 	 * Free TX mbufs still in the queues.
   1891 	 */
   1892 	for (i = 0; i < ALE_TX_RING_CNT; i++) {
   1893 		txd = &sc->ale_cdata.ale_txdesc[i];
   1894 		if (txd->tx_m != NULL) {
   1895 			bus_dmamap_unload(sc->sc_dmat, txd->tx_dmamap);
   1896 			m_freem(txd->tx_m);
   1897 			txd->tx_m = NULL;
   1898 		}
   1899         }
   1900 }
   1901 
   1902 static void
   1903 ale_stop_mac(struct ale_softc *sc)
   1904 {
   1905 	uint32_t reg;
   1906 	int i;
   1907 
   1908 	reg = CSR_READ_4(sc, ALE_MAC_CFG);
   1909 	if ((reg & (MAC_CFG_TX_ENB | MAC_CFG_RX_ENB)) != 0) {
   1910 		reg &= ~(MAC_CFG_TX_ENB | MAC_CFG_RX_ENB);
   1911 		CSR_WRITE_4(sc, ALE_MAC_CFG, reg);
   1912 	}
   1913 
   1914 	for (i = ALE_TIMEOUT; i > 0; i--) {
   1915 		reg = CSR_READ_4(sc, ALE_IDLE_STATUS);
   1916 		if (reg == 0)
   1917 			break;
   1918 		DELAY(10);
   1919 	}
   1920 	if (i == 0)
   1921 		printf("%s: could not disable Tx/Rx MAC(0x%08x)!\n",
   1922 		    device_xname(sc->sc_dev), reg);
   1923 }
   1924 
   1925 static void
   1926 ale_init_tx_ring(struct ale_softc *sc)
   1927 {
   1928 	struct ale_txdesc *txd;
   1929 	int i;
   1930 
   1931 	sc->ale_cdata.ale_tx_prod = 0;
   1932 	sc->ale_cdata.ale_tx_cons = 0;
   1933 	sc->ale_cdata.ale_tx_cnt = 0;
   1934 
   1935 	memset(sc->ale_cdata.ale_tx_ring, 0, ALE_TX_RING_SZ);
   1936 	memset(sc->ale_cdata.ale_tx_cmb, 0, ALE_TX_CMB_SZ);
   1937 	for (i = 0; i < ALE_TX_RING_CNT; i++) {
   1938 		txd = &sc->ale_cdata.ale_txdesc[i];
   1939 		txd->tx_m = NULL;
   1940 	}
   1941 	*sc->ale_cdata.ale_tx_cmb = 0;
   1942 	bus_dmamap_sync(sc->sc_dmat, sc->ale_cdata.ale_tx_cmb_map, 0,
   1943 	    sc->ale_cdata.ale_tx_cmb_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
   1944 	bus_dmamap_sync(sc->sc_dmat, sc->ale_cdata.ale_tx_ring_map, 0,
   1945 	    sc->ale_cdata.ale_tx_ring_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
   1946 }
   1947 
   1948 static void
   1949 ale_init_rx_pages(struct ale_softc *sc)
   1950 {
   1951 	struct ale_rx_page *rx_page;
   1952 	int i;
   1953 
   1954 	sc->ale_cdata.ale_rx_seqno = 0;
   1955 	sc->ale_cdata.ale_rx_curp = 0;
   1956 
   1957 	for (i = 0; i < ALE_RX_PAGES; i++) {
   1958 		rx_page = &sc->ale_cdata.ale_rx_page[i];
   1959 		memset(rx_page->page_addr, 0, sc->ale_pagesize);
   1960 		memset(rx_page->cmb_addr, 0, ALE_RX_CMB_SZ);
   1961 		rx_page->cons = 0;
   1962 		*rx_page->cmb_addr = 0;
   1963 		bus_dmamap_sync(sc->sc_dmat, rx_page->page_map, 0,
   1964 		    rx_page->page_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
   1965 		bus_dmamap_sync(sc->sc_dmat, rx_page->cmb_map, 0,
   1966 		    rx_page->cmb_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
   1967 	}
   1968 }
   1969 
   1970 static void
   1971 ale_rxvlan(struct ale_softc *sc)
   1972 {
   1973 	uint32_t reg;
   1974 
   1975 	reg = CSR_READ_4(sc, ALE_MAC_CFG);
   1976 	reg &= ~MAC_CFG_VLAN_TAG_STRIP;
   1977 	if (sc->sc_ec.ec_capenable & ETHERCAP_VLAN_HWTAGGING)
   1978 		reg |= MAC_CFG_VLAN_TAG_STRIP;
   1979 	CSR_WRITE_4(sc, ALE_MAC_CFG, reg);
   1980 }
   1981 
   1982 static void
   1983 ale_rxfilter(struct ale_softc *sc)
   1984 {
   1985 	struct ethercom *ec = &sc->sc_ec;
   1986 	struct ifnet *ifp = &ec->ec_if;
   1987 	struct ether_multi *enm;
   1988 	struct ether_multistep step;
   1989 	uint32_t crc;
   1990 	uint32_t mchash[2];
   1991 	uint32_t rxcfg;
   1992 
   1993 	rxcfg = CSR_READ_4(sc, ALE_MAC_CFG);
   1994 	rxcfg &= ~(MAC_CFG_ALLMULTI | MAC_CFG_BCAST | MAC_CFG_PROMISC);
   1995 	ifp->if_flags &= ~IFF_ALLMULTI;
   1996 
   1997 	/*
   1998 	 * Always accept broadcast frames.
   1999 	 */
   2000 	rxcfg |= MAC_CFG_BCAST;
   2001 
   2002 	if (ifp->if_flags & IFF_PROMISC || ec->ec_multicnt > 0) {
   2003 		ifp->if_flags |= IFF_ALLMULTI;
   2004 		if (ifp->if_flags & IFF_PROMISC)
   2005 			rxcfg |= MAC_CFG_PROMISC;
   2006 		else
   2007 			rxcfg |= MAC_CFG_ALLMULTI;
   2008 		mchash[0] = mchash[1] = 0xFFFFFFFF;
   2009 	} else {
   2010 		/* Program new filter. */
   2011 		memset(mchash, 0, sizeof(mchash));
   2012 
   2013 		ETHER_FIRST_MULTI(step, ec, enm);
   2014 		while (enm != NULL) {
   2015 			crc = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN);
   2016 			mchash[crc >> 31] |= 1 << ((crc >> 26) & 0x1f);
   2017 			ETHER_NEXT_MULTI(step, enm);
   2018 		}
   2019 	}
   2020 
   2021 	CSR_WRITE_4(sc, ALE_MAR0, mchash[0]);
   2022 	CSR_WRITE_4(sc, ALE_MAR1, mchash[1]);
   2023 	CSR_WRITE_4(sc, ALE_MAC_CFG, rxcfg);
   2024 }
   2025