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