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