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