Home | History | Annotate | Line # | Download | only in pci
if_alc.c revision 1.48
      1  1.48   thorpej /*	$NetBSD: if_alc.c,v 1.48 2020/02/04 05:44:14 thorpej Exp $	*/
      2   1.1  jmcneill /*	$OpenBSD: if_alc.c,v 1.1 2009/08/08 09:31:13 kevlo Exp $	*/
      3   1.1  jmcneill /*-
      4   1.1  jmcneill  * Copyright (c) 2009, Pyun YongHyeon <yongari (at) FreeBSD.org>
      5   1.1  jmcneill  * All rights reserved.
      6   1.1  jmcneill  *
      7   1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8   1.1  jmcneill  * modification, are permitted provided that the following conditions
      9   1.1  jmcneill  * are met:
     10   1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11   1.1  jmcneill  *    notice unmodified, this list of conditions, and the following
     12   1.1  jmcneill  *    disclaimer.
     13   1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     15   1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     16   1.1  jmcneill  *
     17   1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     18   1.1  jmcneill  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19   1.1  jmcneill  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20   1.1  jmcneill  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21   1.1  jmcneill  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22   1.1  jmcneill  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23   1.1  jmcneill  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24   1.1  jmcneill  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25   1.1  jmcneill  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26   1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27   1.1  jmcneill  * SUCH DAMAGE.
     28   1.1  jmcneill  */
     29   1.1  jmcneill 
     30   1.2  jmcneill /* Driver for Atheros AR813x/AR815x PCIe Ethernet. */
     31   1.1  jmcneill 
     32   1.1  jmcneill #ifdef _KERNEL_OPT
     33   1.1  jmcneill #include "vlan.h"
     34   1.1  jmcneill #endif
     35   1.1  jmcneill 
     36   1.1  jmcneill #include <sys/param.h>
     37   1.1  jmcneill #include <sys/proc.h>
     38   1.1  jmcneill #include <sys/endian.h>
     39   1.1  jmcneill #include <sys/systm.h>
     40   1.1  jmcneill #include <sys/types.h>
     41   1.1  jmcneill #include <sys/sockio.h>
     42   1.1  jmcneill #include <sys/mbuf.h>
     43   1.1  jmcneill #include <sys/queue.h>
     44   1.1  jmcneill #include <sys/kernel.h>
     45   1.1  jmcneill #include <sys/device.h>
     46   1.1  jmcneill #include <sys/callout.h>
     47   1.1  jmcneill #include <sys/socket.h>
     48   1.1  jmcneill #include <sys/module.h>
     49   1.1  jmcneill 
     50   1.1  jmcneill #include <sys/bus.h>
     51   1.1  jmcneill 
     52  1.27   msaitoh #include <net/bpf.h>
     53   1.1  jmcneill #include <net/if.h>
     54   1.1  jmcneill #include <net/if_dl.h>
     55   1.1  jmcneill #include <net/if_llc.h>
     56   1.1  jmcneill #include <net/if_media.h>
     57   1.1  jmcneill #include <net/if_ether.h>
     58   1.1  jmcneill 
     59   1.1  jmcneill #ifdef INET
     60   1.1  jmcneill #include <netinet/in.h>
     61   1.1  jmcneill #include <netinet/in_systm.h>
     62   1.1  jmcneill #include <netinet/in_var.h>
     63   1.1  jmcneill #include <netinet/ip.h>
     64   1.1  jmcneill #endif
     65   1.1  jmcneill 
     66   1.1  jmcneill #include <net/if_types.h>
     67   1.1  jmcneill #include <net/if_vlanvar.h>
     68   1.1  jmcneill 
     69   1.1  jmcneill #include <dev/mii/mii.h>
     70   1.1  jmcneill #include <dev/mii/miivar.h>
     71   1.1  jmcneill 
     72   1.1  jmcneill #include <dev/pci/pcireg.h>
     73   1.1  jmcneill #include <dev/pci/pcivar.h>
     74   1.1  jmcneill #include <dev/pci/pcidevs.h>
     75   1.1  jmcneill 
     76   1.1  jmcneill #include <dev/pci/if_alcreg.h>
     77   1.1  jmcneill 
     78   1.2  jmcneill /*
     79   1.2  jmcneill  * Devices supported by this driver.
     80   1.2  jmcneill  */
     81   1.2  jmcneill static struct alc_ident alc_ident_table[] = {
     82   1.2  jmcneill 	{ PCI_VENDOR_ATTANSIC, PCI_PRODUCT_ATTANSIC_AR8131, 9 * 1024,
     83   1.2  jmcneill 		"Atheros AR8131 PCIe Gigabit Ethernet" },
     84   1.2  jmcneill 	{ PCI_VENDOR_ATTANSIC, PCI_PRODUCT_ATTANSIC_AR8132, 9 * 1024,
     85   1.2  jmcneill 		"Atheros AR8132 PCIe Fast Ethernet" },
     86   1.2  jmcneill 	{ PCI_VENDOR_ATTANSIC, PCI_PRODUCT_ATTANSIC_AR8151, 6 * 1024,
     87   1.2  jmcneill 		"Atheros AR8151 v1.0 PCIe Gigabit Ethernet" },
     88   1.2  jmcneill 	{ PCI_VENDOR_ATTANSIC, PCI_PRODUCT_ATTANSIC_AR8151_V2, 6 * 1024,
     89   1.2  jmcneill 		"Atheros AR8151 v2.0 PCIe Gigabit Ethernet" },
     90   1.2  jmcneill 	{ PCI_VENDOR_ATTANSIC, PCI_PRODUCT_ATTANSIC_AR8152_B, 6 * 1024,
     91   1.2  jmcneill 		"Atheros AR8152 v1.1 PCIe Fast Ethernet" },
     92   1.2  jmcneill 	{ PCI_VENDOR_ATTANSIC, PCI_PRODUCT_ATTANSIC_AR8152_B2, 6 * 1024,
     93   1.2  jmcneill 		"Atheros AR8152 v2.0 PCIe Fast Ethernet" },
     94  1.12  christos 	{ PCI_VENDOR_ATTANSIC, PCI_PRODUCT_ATTANSIC_AR8161, 9 * 1024,
     95  1.12  christos 		"Atheros AR8161 PCIe Gigabit Ethernet" },
     96  1.12  christos 	{ PCI_VENDOR_ATTANSIC, PCI_PRODUCT_ATTANSIC_AR8162, 9 * 1024,
     97  1.12  christos 		"Atheros AR8162 PCIe Fast Ethernet" },
     98  1.12  christos 	{ PCI_VENDOR_ATTANSIC, PCI_PRODUCT_ATTANSIC_AR8171, 9 * 1024,
     99  1.12  christos 		"Atheros AR8171 PCIe Gigabit Ethernet" },
    100  1.12  christos 	{ PCI_VENDOR_ATTANSIC, PCI_PRODUCT_ATTANSIC_AR8172, 9 * 1024,
    101  1.12  christos 		"Atheros AR8172 PCIe Fast Ethernet" },
    102  1.12  christos 	{ PCI_VENDOR_ATTANSIC, PCI_PRODUCT_ATTANSIC_E2200, 9 * 1024,
    103  1.12  christos 		"Killer E2200 Gigabit Ethernet" },
    104  1.42   msaitoh 	{ PCI_VENDOR_ATTANSIC, PCI_PRODUCT_ATTANSIC_E2400, 9 * 1024,
    105  1.42   msaitoh 		"Killer E2400 Gigabit Ethernet" },
    106  1.42   msaitoh 	{ PCI_VENDOR_ATTANSIC, PCI_PRODUCT_ATTANSIC_E2500, 9 * 1024,
    107  1.42   msaitoh 		"Killer E2500 Gigabit Ethernet" },
    108   1.2  jmcneill 	{ 0, 0, 0, NULL },
    109   1.2  jmcneill };
    110   1.2  jmcneill 
    111   1.1  jmcneill static int	alc_match(device_t, cfdata_t, void *);
    112   1.1  jmcneill static void	alc_attach(device_t, device_t, void *);
    113   1.1  jmcneill static int	alc_detach(device_t, int);
    114   1.1  jmcneill 
    115   1.1  jmcneill static int	alc_init(struct ifnet *);
    116   1.7       mrg static int	alc_init_backend(struct ifnet *, bool);
    117   1.1  jmcneill static void	alc_start(struct ifnet *);
    118   1.1  jmcneill static int	alc_ioctl(struct ifnet *, u_long, void *);
    119   1.1  jmcneill static void	alc_watchdog(struct ifnet *);
    120   1.1  jmcneill static int	alc_mediachange(struct ifnet *);
    121   1.1  jmcneill static void	alc_mediastatus(struct ifnet *, struct ifmediareq *);
    122   1.1  jmcneill 
    123  1.12  christos static void	alc_aspm(struct alc_softc *, int, int);
    124  1.12  christos static void	alc_aspm_813x(struct alc_softc *, int);
    125  1.12  christos static void	alc_aspm_816x(struct alc_softc *, int);
    126   1.1  jmcneill static void	alc_disable_l0s_l1(struct alc_softc *);
    127   1.1  jmcneill static int	alc_dma_alloc(struct alc_softc *);
    128   1.1  jmcneill static void	alc_dma_free(struct alc_softc *);
    129  1.12  christos static void	alc_dsp_fixup(struct alc_softc *, int);
    130   1.1  jmcneill static int	alc_encap(struct alc_softc *, struct mbuf **);
    131   1.2  jmcneill static struct alc_ident *
    132   1.2  jmcneill 		alc_find_ident(struct pci_attach_args *);
    133   1.1  jmcneill static void	alc_get_macaddr(struct alc_softc *);
    134  1.12  christos static void	alc_get_macaddr_813x(struct alc_softc *);
    135  1.12  christos static void	alc_get_macaddr_816x(struct alc_softc *);
    136  1.12  christos static void	alc_get_macaddr_par(struct alc_softc *);
    137   1.1  jmcneill static void	alc_init_cmb(struct alc_softc *);
    138   1.1  jmcneill static void	alc_init_rr_ring(struct alc_softc *);
    139   1.7       mrg static int	alc_init_rx_ring(struct alc_softc *, bool);
    140   1.1  jmcneill static void	alc_init_smb(struct alc_softc *);
    141   1.1  jmcneill static void	alc_init_tx_ring(struct alc_softc *);
    142   1.1  jmcneill static int	alc_intr(void *);
    143   1.1  jmcneill static void	alc_mac_config(struct alc_softc *);
    144  1.30   msaitoh static int	alc_mii_readreg_813x(struct alc_softc *, int, int, uint16_t *);
    145  1.30   msaitoh static int	alc_mii_readreg_816x(struct alc_softc *, int, int, uint16_t *);
    146  1.30   msaitoh static int	alc_mii_writereg_813x(struct alc_softc *, int, int, uint16_t);
    147  1.30   msaitoh static int	alc_mii_writereg_816x(struct alc_softc *, int, int, uint16_t);
    148  1.30   msaitoh static int	alc_miibus_readreg(device_t, int, int, uint16_t *);
    149   1.6      matt static void	alc_miibus_statchg(struct ifnet *);
    150  1.30   msaitoh static int	alc_miibus_writereg(device_t, int, int, uint16_t);
    151  1.30   msaitoh static int	alc_miidbg_readreg(struct alc_softc *, int, uint16_t *);
    152  1.30   msaitoh static int	alc_miidbg_writereg(struct alc_softc *, int, uint16_t);
    153  1.30   msaitoh static int	alc_miiext_readreg(struct alc_softc *, int, int, uint16_t *);
    154  1.30   msaitoh static int	alc_miiext_writereg(struct alc_softc *, int, int, uint16_t);
    155   1.7       mrg static int	alc_newbuf(struct alc_softc *, struct alc_rxdesc *, bool);
    156   1.1  jmcneill static void	alc_phy_down(struct alc_softc *);
    157   1.1  jmcneill static void	alc_phy_reset(struct alc_softc *);
    158  1.12  christos static void	alc_phy_reset_813x(struct alc_softc *);
    159  1.12  christos static void	alc_phy_reset_816x(struct alc_softc *);
    160   1.1  jmcneill static void	alc_reset(struct alc_softc *);
    161   1.1  jmcneill static void	alc_rxeof(struct alc_softc *, struct rx_rdesc *);
    162   1.1  jmcneill static int	alc_rxintr(struct alc_softc *);
    163   1.1  jmcneill static void	alc_iff(struct alc_softc *);
    164   1.1  jmcneill static void	alc_rxvlan(struct alc_softc *);
    165   1.1  jmcneill static void	alc_start_queue(struct alc_softc *);
    166   1.1  jmcneill static void	alc_stats_clear(struct alc_softc *);
    167   1.1  jmcneill static void	alc_stats_update(struct alc_softc *);
    168   1.1  jmcneill static void	alc_stop(struct ifnet *, int);
    169   1.1  jmcneill static void	alc_stop_mac(struct alc_softc *);
    170   1.1  jmcneill static void	alc_stop_queue(struct alc_softc *);
    171   1.1  jmcneill static void	alc_tick(void *);
    172   1.1  jmcneill static void	alc_txeof(struct alc_softc *);
    173  1.40   msaitoh static void	alc_init_pcie(struct alc_softc *);
    174   1.1  jmcneill 
    175  1.41   msaitoh static uint32_t alc_dma_burst[] = { 128, 256, 512, 1024, 2048, 4096, 0, 0 };
    176   1.1  jmcneill 
    177   1.1  jmcneill CFATTACH_DECL_NEW(alc, sizeof(struct alc_softc),
    178   1.1  jmcneill     alc_match, alc_attach, alc_detach, NULL);
    179   1.1  jmcneill 
    180   1.1  jmcneill int alcdebug = 0;
    181   1.1  jmcneill #define	DPRINTF(x)	do { if (alcdebug) printf x; } while (0)
    182   1.1  jmcneill 
    183   1.1  jmcneill #define ALC_CSUM_FEATURES	(M_CSUM_TCPv4 | M_CSUM_UDPv4)
    184   1.1  jmcneill 
    185   1.1  jmcneill static int
    186  1.30   msaitoh alc_miibus_readreg(device_t dev, int phy, int reg, uint16_t *val)
    187   1.1  jmcneill {
    188   1.1  jmcneill 	struct alc_softc *sc = device_private(dev);
    189  1.12  christos 	int v;
    190  1.12  christos 
    191  1.12  christos 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
    192  1.30   msaitoh 		v = alc_mii_readreg_816x(sc, phy, reg, val);
    193  1.12  christos 	else
    194  1.30   msaitoh 		v = alc_mii_readreg_813x(sc, phy, reg, val);
    195  1.12  christos 	return (v);
    196  1.12  christos }
    197  1.12  christos 
    198  1.30   msaitoh static int
    199  1.30   msaitoh alc_mii_readreg_813x(struct alc_softc *sc, int phy, int reg, uint16_t *val)
    200  1.12  christos {
    201   1.1  jmcneill 	uint32_t v;
    202   1.1  jmcneill 	int i;
    203   1.1  jmcneill 
    204   1.1  jmcneill 	if (phy != sc->alc_phyaddr)
    205  1.30   msaitoh 		return -1;
    206   1.1  jmcneill 
    207  1.12  christos 	/*
    208  1.12  christos 	 * For AR8132 fast ethernet controller, do not report 1000baseT
    209  1.12  christos 	 * capability to mii(4). Even though AR8132 uses the same
    210  1.12  christos 	 * model/revision number of F1 gigabit PHY, the PHY has no
    211  1.12  christos 	 * ability to establish 1000baseT link.
    212  1.12  christos 	 */
    213  1.30   msaitoh 	if ((sc->alc_flags & ALC_FLAG_FASTETHER) != 0 && reg == MII_EXTSR) {
    214  1.30   msaitoh 		*val = 0;
    215  1.12  christos 		return 0;
    216  1.30   msaitoh 	}
    217  1.12  christos 
    218   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_MDIO, MDIO_OP_EXECUTE | MDIO_OP_READ |
    219   1.1  jmcneill 	    MDIO_SUP_PREAMBLE | MDIO_CLK_25_4 | MDIO_REG_ADDR(reg));
    220   1.1  jmcneill 	for (i = ALC_PHY_TIMEOUT; i > 0; i--) {
    221   1.1  jmcneill 		DELAY(5);
    222   1.1  jmcneill 		v = CSR_READ_4(sc, ALC_MDIO);
    223   1.1  jmcneill 		if ((v & (MDIO_OP_EXECUTE | MDIO_OP_BUSY)) == 0)
    224   1.1  jmcneill 			break;
    225   1.1  jmcneill 	}
    226   1.1  jmcneill 
    227   1.1  jmcneill 	if (i == 0) {
    228   1.1  jmcneill 		printf("%s: phy read timeout: phy %d, reg %d\n",
    229   1.1  jmcneill 		    device_xname(sc->sc_dev), phy, reg);
    230  1.30   msaitoh 		return ETIMEDOUT;
    231   1.1  jmcneill 	}
    232   1.1  jmcneill 
    233  1.30   msaitoh 	*val = (v & MDIO_DATA_MASK) >> MDIO_DATA_SHIFT;
    234  1.30   msaitoh 	return 0;
    235   1.1  jmcneill }
    236   1.1  jmcneill 
    237  1.30   msaitoh static int
    238  1.30   msaitoh alc_mii_readreg_816x(struct alc_softc *sc, int phy, int reg, uint16_t *val)
    239  1.12  christos {
    240  1.12  christos 	uint32_t clk, v;
    241  1.12  christos 	int i;
    242  1.12  christos 
    243  1.12  christos 	if (phy != sc->alc_phyaddr)
    244  1.30   msaitoh 		return -1;
    245  1.12  christos 
    246  1.12  christos 	if ((sc->alc_flags & ALC_FLAG_LINK) != 0)
    247  1.12  christos 		clk = MDIO_CLK_25_128;
    248  1.12  christos 	else
    249  1.12  christos 		clk = MDIO_CLK_25_4;
    250  1.12  christos 	CSR_WRITE_4(sc, ALC_MDIO, MDIO_OP_EXECUTE | MDIO_OP_READ |
    251  1.12  christos 	    MDIO_SUP_PREAMBLE | clk | MDIO_REG_ADDR(reg));
    252  1.12  christos 	for (i = ALC_PHY_TIMEOUT; i > 0; i--) {
    253  1.12  christos 		DELAY(5);
    254  1.12  christos 		v = CSR_READ_4(sc, ALC_MDIO);
    255  1.12  christos 		if ((v & MDIO_OP_BUSY) == 0)
    256  1.12  christos 			break;
    257  1.12  christos 	}
    258  1.12  christos 
    259  1.12  christos 	if (i == 0) {
    260  1.12  christos 		printf("%s: phy read timeout: phy %d, reg %d\n",
    261  1.12  christos 		    device_xname(sc->sc_dev), phy, reg);
    262  1.30   msaitoh 		return ETIMEDOUT;
    263  1.12  christos 	}
    264  1.12  christos 
    265  1.30   msaitoh 	*val = (v & MDIO_DATA_MASK) >> MDIO_DATA_SHIFT;
    266  1.30   msaitoh 	return 0;
    267  1.12  christos }
    268  1.12  christos 
    269  1.30   msaitoh static int
    270  1.30   msaitoh alc_miibus_writereg(device_t dev, int phy, int reg, uint16_t val)
    271   1.1  jmcneill {
    272   1.1  jmcneill 	struct alc_softc *sc = device_private(dev);
    273  1.30   msaitoh 	int rv;
    274  1.12  christos 
    275  1.12  christos 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
    276  1.30   msaitoh 		rv = alc_mii_writereg_816x(sc, phy, reg, val);
    277  1.12  christos 	else
    278  1.30   msaitoh 		rv = alc_mii_writereg_813x(sc, phy, reg, val);
    279  1.12  christos 
    280  1.30   msaitoh 	return rv;
    281  1.12  christos }
    282  1.12  christos 
    283  1.30   msaitoh static int
    284  1.30   msaitoh alc_mii_writereg_813x(struct alc_softc *sc, int phy, int reg, uint16_t val)
    285  1.12  christos {
    286   1.1  jmcneill 	uint32_t v;
    287   1.1  jmcneill 	int i;
    288   1.1  jmcneill 
    289   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_MDIO, MDIO_OP_EXECUTE | MDIO_OP_WRITE |
    290   1.1  jmcneill 	    (val & MDIO_DATA_MASK) << MDIO_DATA_SHIFT |
    291   1.1  jmcneill 	    MDIO_SUP_PREAMBLE | MDIO_CLK_25_4 | MDIO_REG_ADDR(reg));
    292   1.1  jmcneill 	for (i = ALC_PHY_TIMEOUT; i > 0; i--) {
    293   1.1  jmcneill 		DELAY(5);
    294   1.1  jmcneill 		v = CSR_READ_4(sc, ALC_MDIO);
    295   1.1  jmcneill 		if ((v & (MDIO_OP_EXECUTE | MDIO_OP_BUSY)) == 0)
    296   1.1  jmcneill 			break;
    297   1.1  jmcneill 	}
    298   1.1  jmcneill 
    299  1.30   msaitoh 	if (i == 0) {
    300   1.1  jmcneill 		printf("%s: phy write timeout: phy %d, reg %d\n",
    301   1.1  jmcneill 		    device_xname(sc->sc_dev), phy, reg);
    302  1.30   msaitoh 		return ETIMEDOUT;
    303  1.30   msaitoh 	}
    304  1.12  christos 
    305  1.30   msaitoh 	return 0;
    306  1.12  christos }
    307  1.12  christos 
    308  1.30   msaitoh static int
    309  1.30   msaitoh alc_mii_writereg_816x(struct alc_softc *sc, int phy, int reg, uint16_t val)
    310  1.12  christos {
    311  1.12  christos 	uint32_t clk, v;
    312  1.12  christos 	int i;
    313  1.12  christos 
    314  1.12  christos 	if ((sc->alc_flags & ALC_FLAG_LINK) != 0)
    315  1.12  christos 		clk = MDIO_CLK_25_128;
    316  1.12  christos 	else
    317  1.12  christos 		clk = MDIO_CLK_25_4;
    318  1.12  christos 	CSR_WRITE_4(sc, ALC_MDIO, MDIO_OP_EXECUTE | MDIO_OP_WRITE |
    319  1.12  christos 	    ((val & MDIO_DATA_MASK) << MDIO_DATA_SHIFT) | MDIO_REG_ADDR(reg) |
    320  1.12  christos 	    MDIO_SUP_PREAMBLE | clk);
    321  1.12  christos 	for (i = ALC_PHY_TIMEOUT; i > 0; i--) {
    322  1.12  christos 		DELAY(5);
    323  1.12  christos 		v = CSR_READ_4(sc, ALC_MDIO);
    324  1.12  christos 		if ((v & MDIO_OP_BUSY) == 0)
    325  1.12  christos 			break;
    326  1.12  christos 	}
    327  1.12  christos 
    328  1.30   msaitoh 	if (i == 0) {
    329  1.12  christos 		printf("%s: phy write timeout: phy %d, reg %d\n",
    330  1.12  christos 		    device_xname(sc->sc_dev), phy, reg);
    331  1.30   msaitoh 		return ETIMEDOUT;
    332  1.30   msaitoh 	}
    333  1.12  christos 
    334  1.30   msaitoh 	return 0;
    335   1.1  jmcneill }
    336   1.1  jmcneill 
    337   1.1  jmcneill static void
    338   1.6      matt alc_miibus_statchg(struct ifnet *ifp)
    339   1.1  jmcneill {
    340   1.6      matt 	struct alc_softc *sc = ifp->if_softc;
    341   1.6      matt 	struct mii_data *mii = &sc->sc_miibus;
    342   1.1  jmcneill 	uint32_t reg;
    343   1.1  jmcneill 
    344   1.1  jmcneill 	if ((ifp->if_flags & IFF_RUNNING) == 0)
    345   1.1  jmcneill 		return;
    346   1.1  jmcneill 
    347   1.1  jmcneill 	sc->alc_flags &= ~ALC_FLAG_LINK;
    348   1.1  jmcneill 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
    349   1.1  jmcneill 	    (IFM_ACTIVE | IFM_AVALID)) {
    350   1.1  jmcneill 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
    351   1.1  jmcneill 		case IFM_10_T:
    352   1.1  jmcneill 		case IFM_100_TX:
    353   1.1  jmcneill 			sc->alc_flags |= ALC_FLAG_LINK;
    354   1.1  jmcneill 			break;
    355   1.1  jmcneill 		case IFM_1000_T:
    356   1.1  jmcneill 			if ((sc->alc_flags & ALC_FLAG_FASTETHER) == 0)
    357   1.1  jmcneill 				sc->alc_flags |= ALC_FLAG_LINK;
    358   1.1  jmcneill 			break;
    359   1.1  jmcneill 		default:
    360   1.1  jmcneill 			break;
    361   1.1  jmcneill 		}
    362   1.1  jmcneill 	}
    363   1.1  jmcneill 	/* Stop Rx/Tx MACs. */
    364   1.1  jmcneill 	alc_stop_mac(sc);
    365   1.1  jmcneill 
    366   1.1  jmcneill 	/* Program MACs with resolved speed/duplex/flow-control. */
    367   1.1  jmcneill 	if ((sc->alc_flags & ALC_FLAG_LINK) != 0) {
    368   1.1  jmcneill 		alc_start_queue(sc);
    369   1.1  jmcneill 		alc_mac_config(sc);
    370   1.1  jmcneill 		/* Re-enable Tx/Rx MACs. */
    371   1.1  jmcneill 		reg = CSR_READ_4(sc, ALC_MAC_CFG);
    372   1.1  jmcneill 		reg |= MAC_CFG_TX_ENB | MAC_CFG_RX_ENB;
    373   1.1  jmcneill 		CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
    374   1.1  jmcneill 	}
    375  1.12  christos 	alc_aspm(sc, 0, IFM_SUBTYPE(mii->mii_media_active));
    376  1.12  christos 	alc_dsp_fixup(sc, IFM_SUBTYPE(mii->mii_media_active));
    377  1.12  christos }
    378  1.12  christos 
    379  1.30   msaitoh static int
    380  1.30   msaitoh alc_miidbg_readreg(struct alc_softc *sc, int reg, uint16_t *val)
    381  1.12  christos {
    382  1.30   msaitoh 	int rv;
    383  1.12  christos 
    384  1.30   msaitoh 	rv = alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr, ALC_MII_DBG_ADDR,
    385  1.12  christos 	    reg);
    386  1.30   msaitoh 	if (rv != 0)
    387  1.30   msaitoh 		return rv;
    388  1.30   msaitoh 
    389  1.12  christos 	return (alc_miibus_readreg(sc->sc_dev, sc->alc_phyaddr,
    390  1.30   msaitoh 		ALC_MII_DBG_DATA, val));
    391   1.1  jmcneill }
    392   1.1  jmcneill 
    393  1.30   msaitoh static int
    394  1.30   msaitoh alc_miidbg_writereg(struct alc_softc *sc, int reg, uint16_t val)
    395  1.12  christos {
    396  1.30   msaitoh 	int rv;
    397  1.12  christos 
    398  1.30   msaitoh 	rv = alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr, ALC_MII_DBG_ADDR,
    399  1.12  christos 	    reg);
    400  1.30   msaitoh 	if (rv != 0)
    401  1.30   msaitoh 		return rv;
    402  1.30   msaitoh 
    403  1.30   msaitoh 	rv = alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr, ALC_MII_DBG_DATA,
    404  1.30   msaitoh 	    val);
    405  1.12  christos 
    406  1.30   msaitoh 	return rv;
    407  1.12  christos }
    408  1.12  christos 
    409  1.30   msaitoh static int
    410  1.30   msaitoh alc_miiext_readreg(struct alc_softc *sc, int devaddr, int reg, uint16_t *val)
    411  1.12  christos {
    412  1.12  christos 	uint32_t clk, v;
    413  1.12  christos 	int i;
    414  1.12  christos 
    415  1.12  christos 	CSR_WRITE_4(sc, ALC_EXT_MDIO, EXT_MDIO_REG(reg) |
    416  1.12  christos 	    EXT_MDIO_DEVADDR(devaddr));
    417  1.12  christos 	if ((sc->alc_flags & ALC_FLAG_LINK) != 0)
    418  1.12  christos 		clk = MDIO_CLK_25_128;
    419  1.12  christos 	else
    420  1.12  christos 		clk = MDIO_CLK_25_4;
    421  1.12  christos 	CSR_WRITE_4(sc, ALC_MDIO, MDIO_OP_EXECUTE | MDIO_OP_READ |
    422  1.12  christos 	    MDIO_SUP_PREAMBLE | clk | MDIO_MODE_EXT);
    423  1.12  christos 	for (i = ALC_PHY_TIMEOUT; i > 0; i--) {
    424  1.12  christos 		DELAY(5);
    425  1.12  christos 		v = CSR_READ_4(sc, ALC_MDIO);
    426  1.12  christos 		if ((v & MDIO_OP_BUSY) == 0)
    427  1.12  christos 			break;
    428  1.12  christos 	}
    429  1.12  christos 
    430  1.12  christos 	if (i == 0) {
    431  1.12  christos 		printf("%s: phy ext read timeout: %d\n",
    432  1.12  christos 		    device_xname(sc->sc_dev), reg);
    433  1.30   msaitoh 		return ETIMEDOUT;
    434  1.12  christos 	}
    435  1.12  christos 
    436  1.30   msaitoh 	*val = (v & MDIO_DATA_MASK) >> MDIO_DATA_SHIFT;
    437  1.30   msaitoh 	return 0;
    438  1.12  christos }
    439  1.12  christos 
    440  1.30   msaitoh static int
    441  1.30   msaitoh alc_miiext_writereg(struct alc_softc *sc, int devaddr, int reg, uint16_t val)
    442  1.12  christos {
    443  1.12  christos 	uint32_t clk, v;
    444  1.12  christos 	int i;
    445  1.12  christos 
    446  1.12  christos 	CSR_WRITE_4(sc, ALC_EXT_MDIO, EXT_MDIO_REG(reg) |
    447  1.12  christos 	    EXT_MDIO_DEVADDR(devaddr));
    448  1.12  christos 	if ((sc->alc_flags & ALC_FLAG_LINK) != 0)
    449  1.12  christos 		clk = MDIO_CLK_25_128;
    450  1.12  christos 	else
    451  1.12  christos 		clk = MDIO_CLK_25_4;
    452  1.12  christos 	CSR_WRITE_4(sc, ALC_MDIO, MDIO_OP_EXECUTE | MDIO_OP_WRITE |
    453  1.12  christos 	    ((val & MDIO_DATA_MASK) << MDIO_DATA_SHIFT) |
    454  1.12  christos 	    MDIO_SUP_PREAMBLE | clk | MDIO_MODE_EXT);
    455  1.12  christos 	for (i = ALC_PHY_TIMEOUT; i > 0; i--) {
    456  1.12  christos 		DELAY(5);
    457  1.12  christos 		v = CSR_READ_4(sc, ALC_MDIO);
    458  1.12  christos 		if ((v & MDIO_OP_BUSY) == 0)
    459  1.12  christos 			break;
    460  1.12  christos 	}
    461  1.12  christos 
    462  1.12  christos 	if (i == 0) {
    463  1.12  christos 		printf("%s: phy ext write timeout: reg %d\n",
    464  1.12  christos 		    device_xname(sc->sc_dev), reg);
    465  1.30   msaitoh 		return ETIMEDOUT;
    466  1.12  christos 	}
    467  1.12  christos 
    468  1.30   msaitoh 	return 0;
    469  1.12  christos }
    470  1.12  christos 
    471  1.12  christos static void
    472  1.12  christos alc_dsp_fixup(struct alc_softc *sc, int media)
    473  1.12  christos {
    474  1.12  christos 	uint16_t agc, len, val;
    475  1.12  christos 
    476  1.12  christos 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
    477  1.12  christos 		return;
    478  1.12  christos 	if (AR816X_REV(sc->alc_rev) >= AR816X_REV_C0)
    479  1.12  christos 		return;
    480  1.12  christos 
    481  1.12  christos 	/*
    482  1.12  christos 	 * Vendor PHY magic.
    483  1.12  christos 	 * 1000BT/AZ, wrong cable length
    484  1.12  christos 	 */
    485  1.12  christos 	if ((sc->alc_flags & ALC_FLAG_LINK) != 0) {
    486  1.30   msaitoh 		alc_miiext_readreg(sc, MII_EXT_PCS, MII_EXT_CLDCTL6, &len);
    487  1.12  christos 		len = (len >> EXT_CLDCTL6_CAB_LEN_SHIFT) &
    488  1.12  christos 		    EXT_CLDCTL6_CAB_LEN_MASK;
    489  1.13  christos 		/* XXX: used to be (alc >> shift) & mask which is 0 */
    490  1.30   msaitoh 		alc_miidbg_readreg(sc, MII_DBG_AGC, &agc);
    491  1.30   msaitoh 		agc &= DBG_AGC_2_VGA_MASK;
    492  1.13  christos 		agc >>= DBG_AGC_2_VGA_SHIFT;
    493  1.12  christos 		if ((media == IFM_1000_T && len > EXT_CLDCTL6_CAB_LEN_SHORT1G &&
    494  1.12  christos 		    agc > DBG_AGC_LONG1G_LIMT) ||
    495  1.12  christos 		    (media == IFM_100_TX && len > DBG_AGC_LONG100M_LIMT &&
    496  1.12  christos 		    agc > DBG_AGC_LONG1G_LIMT)) {
    497  1.12  christos 			alc_miidbg_writereg(sc, MII_DBG_AZ_ANADECT,
    498  1.12  christos 			    DBG_AZ_ANADECT_LONG);
    499  1.30   msaitoh 			alc_miiext_readreg(sc, MII_EXT_ANEG,
    500  1.30   msaitoh 			    MII_EXT_ANEG_AFE, &val);
    501  1.12  christos 			val |= ANEG_AFEE_10BT_100M_TH;
    502  1.12  christos 			alc_miiext_writereg(sc, MII_EXT_ANEG, MII_EXT_ANEG_AFE,
    503  1.12  christos 			    val);
    504  1.12  christos 		} else {
    505  1.12  christos 			alc_miidbg_writereg(sc, MII_DBG_AZ_ANADECT,
    506  1.12  christos 			    DBG_AZ_ANADECT_DEFAULT);
    507  1.30   msaitoh 			alc_miiext_readreg(sc, MII_EXT_ANEG,
    508  1.30   msaitoh 			    MII_EXT_ANEG_AFE, &val);
    509  1.12  christos 			val &= ~ANEG_AFEE_10BT_100M_TH;
    510  1.12  christos 			alc_miiext_writereg(sc, MII_EXT_ANEG, MII_EXT_ANEG_AFE,
    511  1.12  christos 			    val);
    512  1.12  christos 		}
    513  1.12  christos 		if ((sc->alc_flags & ALC_FLAG_LINK_WAR) != 0 &&
    514  1.12  christos 		    AR816X_REV(sc->alc_rev) == AR816X_REV_B0) {
    515  1.12  christos 			if (media == IFM_1000_T) {
    516  1.12  christos 				/*
    517  1.12  christos 				 * Giga link threshold, raise the tolerance of
    518  1.12  christos 				 * noise 50%.
    519  1.12  christos 				 */
    520  1.30   msaitoh 				alc_miidbg_readreg(sc, MII_DBG_MSE20DB, &val);
    521  1.12  christos 				val &= ~DBG_MSE20DB_TH_MASK;
    522  1.12  christos 				val |= (DBG_MSE20DB_TH_HI <<
    523  1.12  christos 				    DBG_MSE20DB_TH_SHIFT);
    524  1.12  christos 				alc_miidbg_writereg(sc, MII_DBG_MSE20DB, val);
    525  1.12  christos 			} else if (media == IFM_100_TX)
    526  1.12  christos 				alc_miidbg_writereg(sc, MII_DBG_MSE16DB,
    527  1.12  christos 				    DBG_MSE16DB_UP);
    528  1.12  christos 		}
    529  1.12  christos 	} else {
    530  1.30   msaitoh 		alc_miiext_readreg(sc, MII_EXT_ANEG, MII_EXT_ANEG_AFE, &val);
    531  1.12  christos 		val &= ~ANEG_AFEE_10BT_100M_TH;
    532  1.12  christos 		alc_miiext_writereg(sc, MII_EXT_ANEG, MII_EXT_ANEG_AFE, val);
    533  1.12  christos 		if ((sc->alc_flags & ALC_FLAG_LINK_WAR) != 0 &&
    534  1.12  christos 		    AR816X_REV(sc->alc_rev) == AR816X_REV_B0) {
    535  1.12  christos 			alc_miidbg_writereg(sc, MII_DBG_MSE16DB,
    536  1.12  christos 			    DBG_MSE16DB_DOWN);
    537  1.30   msaitoh 			alc_miidbg_readreg(sc, MII_DBG_MSE20DB, &val);
    538  1.12  christos 			val &= ~DBG_MSE20DB_TH_MASK;
    539  1.12  christos 			val |= (DBG_MSE20DB_TH_DEFAULT << DBG_MSE20DB_TH_SHIFT);
    540  1.12  christos 			alc_miidbg_writereg(sc, MII_DBG_MSE20DB, val);
    541  1.12  christos 		}
    542  1.36   msaitoh 	}
    543  1.12  christos }
    544  1.35   msaitoh 
    545  1.12  christos static void
    546   1.1  jmcneill alc_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
    547   1.1  jmcneill {
    548   1.1  jmcneill 	struct alc_softc *sc = ifp->if_softc;
    549   1.1  jmcneill 	struct mii_data *mii = &sc->sc_miibus;
    550   1.1  jmcneill 
    551  1.15      leot 	if ((ifp->if_flags & IFF_UP) == 0)
    552  1.15      leot 		return;
    553  1.15      leot 
    554   1.1  jmcneill 	mii_pollstat(mii);
    555   1.1  jmcneill 	ifmr->ifm_status = mii->mii_media_status;
    556   1.1  jmcneill 	ifmr->ifm_active = mii->mii_media_active;
    557   1.1  jmcneill }
    558   1.1  jmcneill 
    559   1.1  jmcneill static int
    560   1.1  jmcneill alc_mediachange(struct ifnet *ifp)
    561   1.1  jmcneill {
    562   1.1  jmcneill 	struct alc_softc *sc = ifp->if_softc;
    563   1.1  jmcneill 	struct mii_data *mii = &sc->sc_miibus;
    564   1.1  jmcneill 	int error;
    565   1.1  jmcneill 
    566   1.1  jmcneill 	if (mii->mii_instance != 0) {
    567   1.1  jmcneill 		struct mii_softc *miisc;
    568   1.1  jmcneill 
    569   1.1  jmcneill 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
    570   1.1  jmcneill 			mii_phy_reset(miisc);
    571   1.1  jmcneill 	}
    572   1.1  jmcneill 	error = mii_mediachg(mii);
    573   1.1  jmcneill 
    574   1.1  jmcneill 	return (error);
    575   1.1  jmcneill }
    576   1.1  jmcneill 
    577   1.2  jmcneill static struct alc_ident *
    578   1.2  jmcneill alc_find_ident(struct pci_attach_args *pa)
    579   1.2  jmcneill {
    580   1.2  jmcneill 	struct alc_ident *ident;
    581   1.2  jmcneill 	uint16_t vendor, devid;
    582   1.2  jmcneill 
    583   1.2  jmcneill 	vendor = PCI_VENDOR(pa->pa_id);
    584   1.2  jmcneill 	devid = PCI_PRODUCT(pa->pa_id);
    585   1.2  jmcneill 	for (ident = alc_ident_table; ident->name != NULL; ident++) {
    586   1.2  jmcneill 		if (vendor == ident->vendorid && devid == ident->deviceid)
    587   1.2  jmcneill 			return (ident);
    588   1.2  jmcneill 	}
    589   1.2  jmcneill 
    590   1.2  jmcneill 	return (NULL);
    591   1.2  jmcneill }
    592   1.2  jmcneill 
    593   1.1  jmcneill static int
    594   1.1  jmcneill alc_match(device_t dev, cfdata_t match, void *aux)
    595   1.1  jmcneill {
    596   1.1  jmcneill 	struct pci_attach_args *pa = aux;
    597   1.1  jmcneill 
    598   1.2  jmcneill 	return alc_find_ident(pa) != NULL;
    599   1.1  jmcneill }
    600   1.1  jmcneill 
    601   1.1  jmcneill static void
    602   1.1  jmcneill alc_get_macaddr(struct alc_softc *sc)
    603   1.1  jmcneill {
    604  1.12  christos 
    605  1.12  christos 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
    606  1.12  christos 		alc_get_macaddr_816x(sc);
    607  1.12  christos 	else
    608  1.12  christos 		alc_get_macaddr_813x(sc);
    609  1.12  christos }
    610  1.12  christos 
    611  1.12  christos static void
    612  1.12  christos alc_get_macaddr_813x(struct alc_softc *sc)
    613  1.12  christos {
    614  1.12  christos 	uint32_t opt;
    615   1.2  jmcneill 	uint16_t val;
    616   1.2  jmcneill 	int eeprom, i;
    617   1.1  jmcneill 
    618   1.2  jmcneill 	eeprom = 0;
    619   1.1  jmcneill 	opt = CSR_READ_4(sc, ALC_OPT_CFG);
    620   1.2  jmcneill 	if ((CSR_READ_4(sc, ALC_MASTER_CFG) & MASTER_OTP_SEL) != 0 &&
    621   1.2  jmcneill 	    (CSR_READ_4(sc, ALC_TWSI_DEBUG) & TWSI_DEBUG_DEV_EXIST) != 0) {
    622   1.1  jmcneill 		/*
    623   1.1  jmcneill 		 * EEPROM found, let TWSI reload EEPROM configuration.
    624   1.1  jmcneill 		 * This will set ethernet address of controller.
    625   1.1  jmcneill 		 */
    626   1.2  jmcneill 		eeprom++;
    627   1.2  jmcneill 		switch (sc->alc_ident->deviceid) {
    628   1.2  jmcneill 		case PCI_PRODUCT_ATTANSIC_AR8131:
    629   1.2  jmcneill 		case PCI_PRODUCT_ATTANSIC_AR8132:
    630   1.2  jmcneill 			if ((opt & OPT_CFG_CLK_ENB) == 0) {
    631   1.2  jmcneill 				opt |= OPT_CFG_CLK_ENB;
    632   1.2  jmcneill 				CSR_WRITE_4(sc, ALC_OPT_CFG, opt);
    633   1.2  jmcneill 				CSR_READ_4(sc, ALC_OPT_CFG);
    634   1.2  jmcneill 				DELAY(1000);
    635   1.2  jmcneill 			}
    636   1.2  jmcneill 			break;
    637   1.2  jmcneill 		case PCI_PRODUCT_ATTANSIC_AR8151:
    638   1.2  jmcneill 		case PCI_PRODUCT_ATTANSIC_AR8151_V2:
    639   1.2  jmcneill 		case PCI_PRODUCT_ATTANSIC_AR8152_B:
    640   1.2  jmcneill 		case PCI_PRODUCT_ATTANSIC_AR8152_B2:
    641   1.2  jmcneill 			alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
    642   1.2  jmcneill 			    ALC_MII_DBG_ADDR, 0x00);
    643  1.30   msaitoh 			alc_miibus_readreg(sc->sc_dev, sc->alc_phyaddr,
    644  1.30   msaitoh 			    ALC_MII_DBG_DATA, &val);
    645   1.2  jmcneill 			alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
    646   1.2  jmcneill 			    ALC_MII_DBG_DATA, val & 0xFF7F);
    647   1.2  jmcneill 			alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
    648   1.2  jmcneill 			    ALC_MII_DBG_ADDR, 0x3B);
    649  1.30   msaitoh 			alc_miibus_readreg(sc->sc_dev, sc->alc_phyaddr,
    650  1.30   msaitoh 			    ALC_MII_DBG_DATA, &val);
    651   1.2  jmcneill 			alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
    652   1.2  jmcneill 			    ALC_MII_DBG_DATA, val | 0x0008);
    653   1.2  jmcneill 			DELAY(20);
    654   1.2  jmcneill 			break;
    655   1.1  jmcneill 		}
    656   1.2  jmcneill 
    657   1.2  jmcneill 		CSR_WRITE_4(sc, ALC_LTSSM_ID_CFG,
    658   1.2  jmcneill 		    CSR_READ_4(sc, ALC_LTSSM_ID_CFG) & ~LTSSM_ID_WRO_ENB);
    659   1.2  jmcneill 		CSR_WRITE_4(sc, ALC_WOL_CFG, 0);
    660   1.2  jmcneill 		CSR_READ_4(sc, ALC_WOL_CFG);
    661   1.2  jmcneill 
    662   1.1  jmcneill 		CSR_WRITE_4(sc, ALC_TWSI_CFG, CSR_READ_4(sc, ALC_TWSI_CFG) |
    663   1.1  jmcneill 		    TWSI_CFG_SW_LD_START);
    664   1.1  jmcneill 		for (i = 100; i > 0; i--) {
    665   1.1  jmcneill 			DELAY(1000);
    666   1.1  jmcneill 			if ((CSR_READ_4(sc, ALC_TWSI_CFG) &
    667   1.1  jmcneill 			    TWSI_CFG_SW_LD_START) == 0)
    668   1.1  jmcneill 				break;
    669   1.1  jmcneill 		}
    670   1.1  jmcneill 		if (i == 0)
    671   1.8  christos 			printf("%s: reloading EEPROM timeout!\n",
    672   1.1  jmcneill 			    device_xname(sc->sc_dev));
    673   1.1  jmcneill 	} else {
    674   1.1  jmcneill 		if (alcdebug)
    675   1.1  jmcneill 			printf("%s: EEPROM not found!\n", device_xname(sc->sc_dev));
    676   1.1  jmcneill 	}
    677   1.2  jmcneill 	if (eeprom != 0) {
    678   1.2  jmcneill 		switch (sc->alc_ident->deviceid) {
    679   1.2  jmcneill 		case PCI_PRODUCT_ATTANSIC_AR8131:
    680   1.2  jmcneill 		case PCI_PRODUCT_ATTANSIC_AR8132:
    681   1.2  jmcneill 			if ((opt & OPT_CFG_CLK_ENB) != 0) {
    682   1.2  jmcneill 				opt &= ~OPT_CFG_CLK_ENB;
    683   1.2  jmcneill 				CSR_WRITE_4(sc, ALC_OPT_CFG, opt);
    684   1.2  jmcneill 				CSR_READ_4(sc, ALC_OPT_CFG);
    685   1.2  jmcneill 				DELAY(1000);
    686   1.2  jmcneill 			}
    687   1.2  jmcneill 			break;
    688   1.2  jmcneill 		case PCI_PRODUCT_ATTANSIC_AR8151:
    689   1.2  jmcneill 		case PCI_PRODUCT_ATTANSIC_AR8151_V2:
    690   1.2  jmcneill 		case PCI_PRODUCT_ATTANSIC_AR8152_B:
    691   1.2  jmcneill 		case PCI_PRODUCT_ATTANSIC_AR8152_B2:
    692   1.2  jmcneill 			alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
    693   1.2  jmcneill 			    ALC_MII_DBG_ADDR, 0x00);
    694  1.30   msaitoh 			alc_miibus_readreg(sc->sc_dev, sc->alc_phyaddr,
    695  1.30   msaitoh 			    ALC_MII_DBG_DATA, &val);
    696   1.2  jmcneill 			alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
    697   1.2  jmcneill 			    ALC_MII_DBG_DATA, val | 0x0080);
    698   1.2  jmcneill 			alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
    699   1.2  jmcneill 			    ALC_MII_DBG_ADDR, 0x3B);
    700  1.30   msaitoh 			alc_miibus_readreg(sc->sc_dev, sc->alc_phyaddr,
    701  1.30   msaitoh 			    ALC_MII_DBG_DATA, &val);
    702   1.2  jmcneill 			alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
    703   1.2  jmcneill 			    ALC_MII_DBG_DATA, val & 0xFFF7);
    704   1.2  jmcneill 			DELAY(20);
    705   1.2  jmcneill 			break;
    706   1.2  jmcneill 		}
    707   1.1  jmcneill 	}
    708   1.1  jmcneill 
    709  1.12  christos 	alc_get_macaddr_par(sc);
    710  1.12  christos }
    711  1.12  christos 
    712  1.12  christos static void
    713  1.12  christos alc_get_macaddr_816x(struct alc_softc *sc)
    714  1.12  christos {
    715  1.12  christos 	uint32_t reg;
    716  1.12  christos 	int i, reloaded;
    717  1.12  christos 
    718  1.12  christos 	reloaded = 0;
    719  1.12  christos 	/* Try to reload station address via TWSI. */
    720  1.12  christos 	for (i = 100; i > 0; i--) {
    721  1.12  christos 		reg = CSR_READ_4(sc, ALC_SLD);
    722  1.12  christos 		if ((reg & (SLD_PROGRESS | SLD_START)) == 0)
    723  1.12  christos 			break;
    724  1.12  christos 		DELAY(1000);
    725  1.12  christos 	}
    726  1.12  christos 	if (i != 0) {
    727  1.12  christos 		CSR_WRITE_4(sc, ALC_SLD, reg | SLD_START);
    728  1.12  christos 		for (i = 100; i > 0; i--) {
    729  1.12  christos 			DELAY(1000);
    730  1.12  christos 			reg = CSR_READ_4(sc, ALC_SLD);
    731  1.12  christos 			if ((reg & SLD_START) == 0)
    732  1.12  christos 				break;
    733  1.12  christos 		}
    734  1.12  christos 		if (i != 0)
    735  1.12  christos 			reloaded++;
    736  1.12  christos 		else if (alcdebug)
    737  1.12  christos 			printf("%s: reloading station address via TWSI timed out!\n",
    738  1.12  christos 			    device_xname(sc->sc_dev));
    739  1.12  christos 	}
    740  1.12  christos 
    741  1.12  christos 	/* Try to reload station address from EEPROM or FLASH. */
    742  1.12  christos 	if (reloaded == 0) {
    743  1.12  christos 		reg = CSR_READ_4(sc, ALC_EEPROM_LD);
    744  1.12  christos 		if ((reg & (EEPROM_LD_EEPROM_EXIST |
    745  1.12  christos 		    EEPROM_LD_FLASH_EXIST)) != 0) {
    746  1.12  christos 			for (i = 100; i > 0; i--) {
    747  1.12  christos 				reg = CSR_READ_4(sc, ALC_EEPROM_LD);
    748  1.12  christos 				if ((reg & (EEPROM_LD_PROGRESS |
    749  1.12  christos 				    EEPROM_LD_START)) == 0)
    750  1.12  christos 					break;
    751  1.12  christos 				DELAY(1000);
    752  1.12  christos 			}
    753  1.12  christos 			if (i != 0) {
    754  1.12  christos 				CSR_WRITE_4(sc, ALC_EEPROM_LD, reg |
    755  1.12  christos 				    EEPROM_LD_START);
    756  1.12  christos 				for (i = 100; i > 0; i--) {
    757  1.12  christos 					DELAY(1000);
    758  1.12  christos 					reg = CSR_READ_4(sc, ALC_EEPROM_LD);
    759  1.12  christos 					if ((reg & EEPROM_LD_START) == 0)
    760  1.12  christos 						break;
    761  1.12  christos 				}
    762  1.12  christos 			} else if (alcdebug)
    763  1.12  christos 				printf("%s: reloading EEPROM/FLASH timed out!\n",
    764  1.36   msaitoh 				  device_xname(sc->sc_dev));
    765  1.12  christos 		}
    766  1.12  christos 	}
    767  1.12  christos 
    768  1.12  christos 	alc_get_macaddr_par(sc);
    769  1.12  christos }
    770  1.12  christos 
    771  1.12  christos static void
    772  1.12  christos alc_get_macaddr_par(struct alc_softc *sc)
    773  1.12  christos {
    774  1.12  christos 	uint32_t ea[2];
    775  1.12  christos 
    776   1.1  jmcneill 	ea[0] = CSR_READ_4(sc, ALC_PAR0);
    777   1.1  jmcneill 	ea[1] = CSR_READ_4(sc, ALC_PAR1);
    778   1.1  jmcneill 	sc->alc_eaddr[0] = (ea[1] >> 8) & 0xFF;
    779   1.1  jmcneill 	sc->alc_eaddr[1] = (ea[1] >> 0) & 0xFF;
    780   1.1  jmcneill 	sc->alc_eaddr[2] = (ea[0] >> 24) & 0xFF;
    781   1.1  jmcneill 	sc->alc_eaddr[3] = (ea[0] >> 16) & 0xFF;
    782   1.1  jmcneill 	sc->alc_eaddr[4] = (ea[0] >> 8) & 0xFF;
    783   1.1  jmcneill 	sc->alc_eaddr[5] = (ea[0] >> 0) & 0xFF;
    784   1.1  jmcneill }
    785   1.1  jmcneill 
    786   1.1  jmcneill static void
    787   1.1  jmcneill alc_disable_l0s_l1(struct alc_softc *sc)
    788   1.1  jmcneill {
    789   1.1  jmcneill 	uint32_t pmcfg;
    790   1.1  jmcneill 
    791  1.12  christos 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
    792  1.12  christos 		/* Another magic from vendor. */
    793  1.12  christos 		pmcfg = CSR_READ_4(sc, ALC_PM_CFG);
    794  1.12  christos 		pmcfg &= ~(PM_CFG_L1_ENTRY_TIMER_MASK | PM_CFG_CLK_SWH_L1 |
    795  1.12  christos 		    PM_CFG_ASPM_L0S_ENB | PM_CFG_ASPM_L1_ENB |
    796  1.12  christos 		    PM_CFG_MAC_ASPM_CHK | PM_CFG_SERDES_PD_EX_L1);
    797  1.12  christos 		pmcfg |= PM_CFG_SERDES_BUDS_RX_L1_ENB |
    798  1.12  christos 		    PM_CFG_SERDES_PLL_L1_ENB | PM_CFG_SERDES_L1_ENB;
    799  1.12  christos 		CSR_WRITE_4(sc, ALC_PM_CFG, pmcfg);
    800  1.12  christos 	}
    801   1.1  jmcneill }
    802   1.1  jmcneill 
    803   1.1  jmcneill static void
    804   1.1  jmcneill alc_phy_reset(struct alc_softc *sc)
    805   1.1  jmcneill {
    806  1.12  christos 
    807  1.12  christos 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
    808  1.12  christos 		alc_phy_reset_816x(sc);
    809  1.12  christos 	else
    810  1.12  christos 		alc_phy_reset_813x(sc);
    811  1.12  christos }
    812  1.12  christos 
    813  1.12  christos static void
    814  1.12  christos alc_phy_reset_813x(struct alc_softc *sc)
    815  1.12  christos {
    816   1.1  jmcneill 	uint16_t data;
    817   1.1  jmcneill 
    818   1.1  jmcneill 	/* Reset magic from Linux. */
    819  1.12  christos 	CSR_WRITE_2(sc, ALC_GPHY_CFG, GPHY_CFG_SEL_ANA_RESET);
    820   1.1  jmcneill 	CSR_READ_2(sc, ALC_GPHY_CFG);
    821   1.1  jmcneill 	DELAY(10 * 1000);
    822   1.1  jmcneill 
    823  1.12  christos 	CSR_WRITE_2(sc, ALC_GPHY_CFG, GPHY_CFG_EXT_RESET |
    824   1.1  jmcneill 	    GPHY_CFG_SEL_ANA_RESET);
    825   1.1  jmcneill 	CSR_READ_2(sc, ALC_GPHY_CFG);
    826   1.1  jmcneill 	DELAY(10 * 1000);
    827   1.1  jmcneill 
    828   1.2  jmcneill 	/* DSP fixup, Vendor magic. */
    829   1.2  jmcneill 	if (sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8152_B) {
    830   1.2  jmcneill 		alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
    831   1.2  jmcneill 		    ALC_MII_DBG_ADDR, 0x000A);
    832  1.30   msaitoh 		alc_miibus_readreg(sc->sc_dev, sc->alc_phyaddr,
    833  1.30   msaitoh 		    ALC_MII_DBG_DATA, &data);
    834   1.2  jmcneill 		alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
    835   1.2  jmcneill 		    ALC_MII_DBG_DATA, data & 0xDFFF);
    836   1.2  jmcneill 	}
    837   1.2  jmcneill 	if (sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8151 ||
    838   1.2  jmcneill 	    sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8151_V2 ||
    839   1.2  jmcneill 	    sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8152_B ||
    840   1.2  jmcneill 	    sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8152_B2) {
    841   1.2  jmcneill 		alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
    842   1.2  jmcneill 		    ALC_MII_DBG_ADDR, 0x003B);
    843  1.30   msaitoh 		alc_miibus_readreg(sc->sc_dev, sc->alc_phyaddr,
    844  1.30   msaitoh 		    ALC_MII_DBG_DATA, &data);
    845   1.2  jmcneill 		alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
    846   1.2  jmcneill 		    ALC_MII_DBG_DATA, data & 0xFFF7);
    847   1.2  jmcneill 		DELAY(20 * 1000);
    848   1.2  jmcneill 	}
    849   1.2  jmcneill 	if (sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8151) {
    850   1.2  jmcneill 		alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
    851   1.2  jmcneill 		    ALC_MII_DBG_ADDR, 0x0029);
    852   1.2  jmcneill 		alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
    853   1.2  jmcneill 		    ALC_MII_DBG_DATA, 0x929D);
    854   1.2  jmcneill 	}
    855   1.2  jmcneill 	if (sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8131 ||
    856   1.2  jmcneill 	    sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8132 ||
    857   1.2  jmcneill 	    sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8151_V2 ||
    858   1.2  jmcneill 	    sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8152_B2) {
    859   1.2  jmcneill 		alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
    860   1.2  jmcneill 		    ALC_MII_DBG_ADDR, 0x0029);
    861   1.2  jmcneill 		alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
    862   1.2  jmcneill 		    ALC_MII_DBG_DATA, 0xB6DD);
    863   1.2  jmcneill 	}
    864   1.2  jmcneill 
    865   1.1  jmcneill 	/* Load DSP codes, vendor magic. */
    866   1.1  jmcneill 	data = ANA_LOOP_SEL_10BT | ANA_EN_MASK_TB | ANA_EN_10BT_IDLE |
    867   1.1  jmcneill 	    ((1 << ANA_INTERVAL_SEL_TIMER_SHIFT) & ANA_INTERVAL_SEL_TIMER_MASK);
    868   1.1  jmcneill 	alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
    869   1.1  jmcneill 	    ALC_MII_DBG_ADDR, MII_ANA_CFG18);
    870   1.1  jmcneill 	alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
    871   1.1  jmcneill 	    ALC_MII_DBG_DATA, data);
    872   1.1  jmcneill 
    873   1.1  jmcneill 	data = ((2 << ANA_SERDES_CDR_BW_SHIFT) & ANA_SERDES_CDR_BW_MASK) |
    874   1.1  jmcneill 	    ANA_SERDES_EN_DEEM | ANA_SERDES_SEL_HSP | ANA_SERDES_EN_PLL |
    875   1.1  jmcneill 	    ANA_SERDES_EN_LCKDT;
    876   1.1  jmcneill 	alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
    877   1.1  jmcneill 	    ALC_MII_DBG_ADDR, MII_ANA_CFG5);
    878   1.1  jmcneill 	alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
    879   1.1  jmcneill 	    ALC_MII_DBG_DATA, data);
    880   1.1  jmcneill 
    881   1.1  jmcneill 	data = ((44 << ANA_LONG_CABLE_TH_100_SHIFT) &
    882   1.1  jmcneill 	    ANA_LONG_CABLE_TH_100_MASK) |
    883   1.1  jmcneill 	    ((33 << ANA_SHORT_CABLE_TH_100_SHIFT) &
    884   1.1  jmcneill 	    ANA_SHORT_CABLE_TH_100_SHIFT) |
    885   1.1  jmcneill 	    ANA_BP_BAD_LINK_ACCUM | ANA_BP_SMALL_BW;
    886   1.1  jmcneill 	alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
    887   1.1  jmcneill 	    ALC_MII_DBG_ADDR, MII_ANA_CFG54);
    888   1.1  jmcneill 	alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
    889   1.1  jmcneill 	    ALC_MII_DBG_DATA, data);
    890   1.1  jmcneill 
    891   1.1  jmcneill 	data = ((11 << ANA_IECHO_ADJ_3_SHIFT) & ANA_IECHO_ADJ_3_MASK) |
    892   1.1  jmcneill 	    ((11 << ANA_IECHO_ADJ_2_SHIFT) & ANA_IECHO_ADJ_2_MASK) |
    893   1.1  jmcneill 	    ((8 << ANA_IECHO_ADJ_1_SHIFT) & ANA_IECHO_ADJ_1_MASK) |
    894   1.1  jmcneill 	    ((8 << ANA_IECHO_ADJ_0_SHIFT) & ANA_IECHO_ADJ_0_MASK);
    895   1.1  jmcneill 	alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
    896   1.1  jmcneill 	    ALC_MII_DBG_ADDR, MII_ANA_CFG4);
    897   1.1  jmcneill 	alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
    898   1.1  jmcneill 	    ALC_MII_DBG_DATA, data);
    899   1.1  jmcneill 
    900   1.1  jmcneill 	data = ((7 & ANA_MANUL_SWICH_ON_SHIFT) & ANA_MANUL_SWICH_ON_MASK) |
    901   1.1  jmcneill 	    ANA_RESTART_CAL | ANA_MAN_ENABLE | ANA_SEL_HSP | ANA_EN_HB |
    902   1.1  jmcneill 	    ANA_OEN_125M;
    903   1.1  jmcneill 	alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
    904   1.1  jmcneill 	    ALC_MII_DBG_ADDR, MII_ANA_CFG0);
    905   1.1  jmcneill 	alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
    906   1.1  jmcneill 	    ALC_MII_DBG_DATA, data);
    907   1.1  jmcneill 	DELAY(1000);
    908  1.12  christos 
    909  1.12  christos 	/* Disable hibernation. */
    910  1.12  christos 	alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr, ALC_MII_DBG_ADDR,
    911  1.12  christos 	    0x0029);
    912  1.30   msaitoh 	alc_miibus_readreg(sc->sc_dev, sc->alc_phyaddr,
    913  1.30   msaitoh 	    ALC_MII_DBG_DATA, &data);
    914  1.12  christos 	data &= ~0x8000;
    915  1.12  christos 	alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr, ALC_MII_DBG_DATA,
    916  1.12  christos 	    data);
    917  1.12  christos 
    918  1.12  christos 	alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr, ALC_MII_DBG_ADDR,
    919  1.12  christos 	    0x000B);
    920  1.30   msaitoh 	alc_miibus_readreg(sc->sc_dev, sc->alc_phyaddr,
    921  1.30   msaitoh 	    ALC_MII_DBG_DATA, &data);
    922  1.12  christos 	data &= ~0x8000;
    923  1.12  christos 	alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr, ALC_MII_DBG_DATA,
    924  1.12  christos 	    data);
    925  1.12  christos }
    926  1.12  christos 
    927  1.12  christos static void
    928  1.12  christos alc_phy_reset_816x(struct alc_softc *sc)
    929  1.12  christos {
    930  1.12  christos 	uint32_t val;
    931  1.30   msaitoh 	uint16_t phyval;
    932  1.12  christos 
    933  1.12  christos 	val = CSR_READ_4(sc, ALC_GPHY_CFG);
    934  1.12  christos 	val &= ~(GPHY_CFG_EXT_RESET | GPHY_CFG_LED_MODE |
    935  1.12  christos 	    GPHY_CFG_GATE_25M_ENB | GPHY_CFG_PHY_IDDQ | GPHY_CFG_PHY_PLL_ON |
    936  1.12  christos 	    GPHY_CFG_PWDOWN_HW | GPHY_CFG_100AB_ENB);
    937  1.12  christos 	val |= GPHY_CFG_SEL_ANA_RESET;
    938  1.12  christos #ifdef notyet
    939  1.12  christos 	val |= GPHY_CFG_HIB_PULSE | GPHY_CFG_HIB_EN | GPHY_CFG_SEL_ANA_RESET;
    940  1.12  christos #else
    941  1.12  christos 	/* Disable PHY hibernation. */
    942  1.12  christos 	val &= ~(GPHY_CFG_HIB_PULSE | GPHY_CFG_HIB_EN);
    943  1.12  christos #endif
    944  1.12  christos 	CSR_WRITE_4(sc, ALC_GPHY_CFG, val);
    945  1.12  christos 	DELAY(10);
    946  1.12  christos 	CSR_WRITE_4(sc, ALC_GPHY_CFG, val | GPHY_CFG_EXT_RESET);
    947  1.12  christos 	DELAY(800);
    948  1.12  christos 
    949  1.12  christos 	/* Vendor PHY magic. */
    950  1.12  christos #ifdef notyet
    951  1.12  christos 	alc_miidbg_writereg(sc, MII_DBG_LEGCYPS, DBG_LEGCYPS_DEFAULT);
    952  1.12  christos 	alc_miidbg_writereg(sc, MII_DBG_SYSMODCTL, DBG_SYSMODCTL_DEFAULT);
    953  1.12  christos 	alc_miiext_writereg(sc, MII_EXT_PCS, MII_EXT_VDRVBIAS,
    954  1.12  christos 	    EXT_VDRVBIAS_DEFAULT);
    955  1.12  christos #else
    956  1.12  christos 	/* Disable PHY hibernation. */
    957  1.12  christos 	alc_miidbg_writereg(sc, MII_DBG_LEGCYPS,
    958  1.12  christos 	    DBG_LEGCYPS_DEFAULT & ~DBG_LEGCYPS_ENB);
    959  1.12  christos 	alc_miidbg_writereg(sc, MII_DBG_HIBNEG,
    960  1.12  christos 	    DBG_HIBNEG_DEFAULT & ~(DBG_HIBNEG_PSHIB_EN | DBG_HIBNEG_HIB_PULSE));
    961  1.12  christos 	alc_miidbg_writereg(sc, MII_DBG_GREENCFG, DBG_GREENCFG_DEFAULT);
    962  1.12  christos #endif
    963  1.12  christos 
    964  1.12  christos 	/* XXX Disable EEE. */
    965  1.12  christos 	val = CSR_READ_4(sc, ALC_LPI_CTL);
    966  1.12  christos 	val &= ~LPI_CTL_ENB;
    967  1.12  christos 	CSR_WRITE_4(sc, ALC_LPI_CTL, val);
    968  1.12  christos 	alc_miiext_writereg(sc, MII_EXT_ANEG, MII_EXT_ANEG_LOCAL_EEEADV, 0);
    969  1.12  christos 
    970  1.12  christos 	/* PHY power saving. */
    971  1.12  christos 	alc_miidbg_writereg(sc, MII_DBG_TST10BTCFG, DBG_TST10BTCFG_DEFAULT);
    972  1.12  christos 	alc_miidbg_writereg(sc, MII_DBG_SRDSYSMOD, DBG_SRDSYSMOD_DEFAULT);
    973  1.12  christos 	alc_miidbg_writereg(sc, MII_DBG_TST100BTCFG, DBG_TST100BTCFG_DEFAULT);
    974  1.12  christos 	alc_miidbg_writereg(sc, MII_DBG_ANACTL, DBG_ANACTL_DEFAULT);
    975  1.30   msaitoh 	alc_miidbg_readreg(sc, MII_DBG_GREENCFG2, &phyval);
    976  1.30   msaitoh 	phyval &= ~DBG_GREENCFG2_GATE_DFSE_EN;
    977  1.30   msaitoh 	alc_miidbg_writereg(sc, MII_DBG_GREENCFG2, phyval);
    978  1.12  christos 
    979  1.12  christos 	/* RTL8139C, 120m issue. */
    980  1.12  christos 	alc_miiext_writereg(sc, MII_EXT_ANEG, MII_EXT_ANEG_NLP78,
    981  1.12  christos 	    ANEG_NLP78_120M_DEFAULT);
    982  1.12  christos 	alc_miiext_writereg(sc, MII_EXT_ANEG, MII_EXT_ANEG_S3DIG10,
    983  1.12  christos 	    ANEG_S3DIG10_DEFAULT);
    984  1.12  christos 
    985  1.12  christos 	if ((sc->alc_flags & ALC_FLAG_LINK_WAR) != 0) {
    986  1.12  christos 		/* Turn off half amplitude. */
    987  1.30   msaitoh 		alc_miiext_readreg(sc, MII_EXT_PCS, MII_EXT_CLDCTL3, &phyval);
    988  1.30   msaitoh 		phyval |= EXT_CLDCTL3_BP_CABLE1TH_DET_GT;
    989  1.30   msaitoh 		alc_miiext_writereg(sc, MII_EXT_PCS, MII_EXT_CLDCTL3, phyval);
    990  1.12  christos 		/* Turn off Green feature. */
    991  1.30   msaitoh 		alc_miidbg_readreg(sc, MII_DBG_GREENCFG2, &phyval);
    992  1.30   msaitoh 		phyval |= DBG_GREENCFG2_BP_GREEN;
    993  1.30   msaitoh 		alc_miidbg_writereg(sc, MII_DBG_GREENCFG2, phyval);
    994  1.12  christos 		/* Turn off half bias. */
    995  1.30   msaitoh 		alc_miiext_readreg(sc, MII_EXT_PCS, MII_EXT_CLDCTL5, &phyval);
    996  1.12  christos 		val |= EXT_CLDCTL5_BP_VD_HLFBIAS;
    997  1.30   msaitoh 		alc_miiext_writereg(sc, MII_EXT_PCS, MII_EXT_CLDCTL5, phyval);
    998  1.12  christos 	}
    999   1.1  jmcneill }
   1000   1.1  jmcneill 
   1001   1.1  jmcneill static void
   1002   1.1  jmcneill alc_phy_down(struct alc_softc *sc)
   1003   1.1  jmcneill {
   1004  1.12  christos 	uint32_t gphy;
   1005  1.12  christos 
   1006   1.2  jmcneill 	switch (sc->alc_ident->deviceid) {
   1007  1.12  christos 	case PCI_PRODUCT_ATTANSIC_AR8161:
   1008  1.12  christos 	case PCI_PRODUCT_ATTANSIC_E2200:
   1009  1.42   msaitoh 	case PCI_PRODUCT_ATTANSIC_E2400:
   1010  1.42   msaitoh 	case PCI_PRODUCT_ATTANSIC_E2500:
   1011  1.12  christos 	case PCI_PRODUCT_ATTANSIC_AR8162:
   1012  1.12  christos 	case PCI_PRODUCT_ATTANSIC_AR8171:
   1013  1.12  christos 	case PCI_PRODUCT_ATTANSIC_AR8172:
   1014  1.12  christos 		gphy = CSR_READ_4(sc, ALC_GPHY_CFG);
   1015  1.12  christos 		gphy &= ~(GPHY_CFG_EXT_RESET | GPHY_CFG_LED_MODE |
   1016  1.12  christos 		    GPHY_CFG_100AB_ENB | GPHY_CFG_PHY_PLL_ON);
   1017  1.12  christos 		gphy |= GPHY_CFG_HIB_EN | GPHY_CFG_HIB_PULSE |
   1018  1.12  christos 		    GPHY_CFG_SEL_ANA_RESET;
   1019  1.12  christos 		gphy |= GPHY_CFG_PHY_IDDQ | GPHY_CFG_PWDOWN_HW;
   1020  1.12  christos 		CSR_WRITE_4(sc, ALC_GPHY_CFG, gphy);
   1021  1.12  christos 		break;
   1022   1.2  jmcneill 	case PCI_PRODUCT_ATTANSIC_AR8151:
   1023   1.2  jmcneill 	case PCI_PRODUCT_ATTANSIC_AR8151_V2:
   1024  1.12  christos 	case PCI_PRODUCT_ATTANSIC_AR8152_B:
   1025  1.12  christos 	case PCI_PRODUCT_ATTANSIC_AR8152_B2:
   1026   1.2  jmcneill 		/*
   1027   1.2  jmcneill 		 * GPHY power down caused more problems on AR8151 v2.0.
   1028   1.2  jmcneill 		 * When driver is reloaded after GPHY power down,
   1029   1.2  jmcneill 		 * accesses to PHY/MAC registers hung the system. Only
   1030   1.2  jmcneill 		 * cold boot recovered from it.  I'm not sure whether
   1031   1.2  jmcneill 		 * AR8151 v1.0 also requires this one though.  I don't
   1032   1.2  jmcneill 		 * have AR8151 v1.0 controller in hand.
   1033   1.2  jmcneill 		 * The only option left is to isolate the PHY and
   1034   1.2  jmcneill 		 * initiates power down the PHY which in turn saves
   1035   1.2  jmcneill 		 * more power when driver is unloaded.
   1036   1.2  jmcneill 		 */
   1037   1.2  jmcneill 		alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
   1038   1.2  jmcneill 		    MII_BMCR, BMCR_ISO | BMCR_PDOWN);
   1039   1.2  jmcneill 		break;
   1040   1.2  jmcneill 	default:
   1041   1.2  jmcneill 		/* Force PHY down. */
   1042  1.12  christos 		CSR_WRITE_2(sc, ALC_GPHY_CFG, GPHY_CFG_EXT_RESET |
   1043   1.2  jmcneill 		    GPHY_CFG_SEL_ANA_RESET | GPHY_CFG_PHY_IDDQ |
   1044   1.2  jmcneill 		    GPHY_CFG_PWDOWN_HW);
   1045   1.2  jmcneill 		DELAY(1000);
   1046   1.2  jmcneill 		break;
   1047   1.2  jmcneill 	}
   1048   1.1  jmcneill }
   1049   1.1  jmcneill 
   1050   1.1  jmcneill static void
   1051  1.12  christos alc_aspm(struct alc_softc *sc, int init, int media)
   1052  1.12  christos {
   1053  1.12  christos 
   1054  1.12  christos 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
   1055  1.12  christos 		alc_aspm_816x(sc, init);
   1056  1.12  christos 	else
   1057  1.12  christos 		alc_aspm_813x(sc, media);
   1058  1.12  christos }
   1059  1.12  christos 
   1060  1.12  christos static void
   1061  1.12  christos alc_aspm_813x(struct alc_softc *sc, int media)
   1062   1.1  jmcneill {
   1063   1.1  jmcneill 	uint32_t pmcfg;
   1064   1.2  jmcneill 	uint16_t linkcfg;
   1065   1.8  christos 
   1066   1.1  jmcneill 	pmcfg = CSR_READ_4(sc, ALC_PM_CFG);
   1067   1.2  jmcneill 	if ((sc->alc_flags & (ALC_FLAG_APS | ALC_FLAG_PCIE)) ==
   1068   1.2  jmcneill 	    (ALC_FLAG_APS | ALC_FLAG_PCIE))
   1069   1.2  jmcneill 		linkcfg = CSR_READ_2(sc, sc->alc_expcap +
   1070   1.9   msaitoh 		    PCIE_LCSR);
   1071   1.2  jmcneill 	else
   1072   1.2  jmcneill 		linkcfg = 0;
   1073   1.1  jmcneill 	pmcfg &= ~PM_CFG_SERDES_PD_EX_L1;
   1074   1.2  jmcneill 	pmcfg &= ~(PM_CFG_L1_ENTRY_TIMER_MASK | PM_CFG_LCKDET_TIMER_MASK);
   1075   1.1  jmcneill 	pmcfg |= PM_CFG_MAC_ASPM_CHK;
   1076   1.2  jmcneill 	pmcfg |= (PM_CFG_LCKDET_TIMER_DEFAULT << PM_CFG_LCKDET_TIMER_SHIFT);
   1077   1.2  jmcneill 	pmcfg &= ~(PM_CFG_ASPM_L1_ENB | PM_CFG_ASPM_L0S_ENB);
   1078   1.2  jmcneill 
   1079   1.2  jmcneill 	if ((sc->alc_flags & ALC_FLAG_APS) != 0) {
   1080   1.2  jmcneill 		/* Disable extended sync except AR8152 B v1.0 */
   1081   1.2  jmcneill 		linkcfg &= ~0x80;
   1082   1.2  jmcneill 		if (sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8152_B &&
   1083   1.2  jmcneill 		    sc->alc_rev == ATHEROS_AR8152_B_V10)
   1084   1.2  jmcneill 			linkcfg |= 0x80;
   1085   1.9   msaitoh 		CSR_WRITE_2(sc, sc->alc_expcap + PCIE_LCSR,
   1086   1.2  jmcneill 		    linkcfg);
   1087   1.2  jmcneill 		pmcfg &= ~(PM_CFG_EN_BUFS_RX_L0S | PM_CFG_SA_DLY_ENB |
   1088   1.2  jmcneill 		    PM_CFG_HOTRST);
   1089   1.2  jmcneill 		pmcfg |= (PM_CFG_L1_ENTRY_TIMER_DEFAULT <<
   1090   1.2  jmcneill 		    PM_CFG_L1_ENTRY_TIMER_SHIFT);
   1091   1.2  jmcneill 		pmcfg &= ~PM_CFG_PM_REQ_TIMER_MASK;
   1092   1.2  jmcneill 		pmcfg |= (PM_CFG_PM_REQ_TIMER_DEFAULT <<
   1093   1.2  jmcneill 		    PM_CFG_PM_REQ_TIMER_SHIFT);
   1094   1.2  jmcneill 		pmcfg |= PM_CFG_SERDES_PD_EX_L1 | PM_CFG_PCIE_RECV;
   1095   1.2  jmcneill 	}
   1096   1.2  jmcneill 
   1097   1.1  jmcneill 	if ((sc->alc_flags & ALC_FLAG_LINK) != 0) {
   1098   1.2  jmcneill 		if ((sc->alc_flags & ALC_FLAG_L0S) != 0)
   1099   1.2  jmcneill 			pmcfg |= PM_CFG_ASPM_L0S_ENB;
   1100   1.2  jmcneill 		if ((sc->alc_flags & ALC_FLAG_L1S) != 0)
   1101   1.2  jmcneill 			pmcfg |= PM_CFG_ASPM_L1_ENB;
   1102   1.2  jmcneill 		if ((sc->alc_flags & ALC_FLAG_APS) != 0) {
   1103   1.2  jmcneill 			if (sc->alc_ident->deviceid ==
   1104   1.2  jmcneill 			    PCI_PRODUCT_ATTANSIC_AR8152_B)
   1105   1.2  jmcneill 				pmcfg &= ~PM_CFG_ASPM_L0S_ENB;
   1106   1.2  jmcneill 			pmcfg &= ~(PM_CFG_SERDES_L1_ENB |
   1107   1.2  jmcneill 			    PM_CFG_SERDES_PLL_L1_ENB |
   1108   1.2  jmcneill 			    PM_CFG_SERDES_BUDS_RX_L1_ENB);
   1109   1.2  jmcneill 			pmcfg |= PM_CFG_CLK_SWH_L1;
   1110   1.2  jmcneill 			if (media == IFM_100_TX || media == IFM_1000_T) {
   1111   1.2  jmcneill 				pmcfg &= ~PM_CFG_L1_ENTRY_TIMER_MASK;
   1112   1.2  jmcneill 				switch (sc->alc_ident->deviceid) {
   1113   1.2  jmcneill 				case PCI_PRODUCT_ATTANSIC_AR8152_B:
   1114   1.2  jmcneill 					pmcfg |= (7 <<
   1115   1.2  jmcneill 					    PM_CFG_L1_ENTRY_TIMER_SHIFT);
   1116   1.2  jmcneill 					break;
   1117   1.2  jmcneill 				case PCI_PRODUCT_ATTANSIC_AR8152_B2:
   1118   1.2  jmcneill 				case PCI_PRODUCT_ATTANSIC_AR8151_V2:
   1119   1.2  jmcneill 					pmcfg |= (4 <<
   1120   1.2  jmcneill 					    PM_CFG_L1_ENTRY_TIMER_SHIFT);
   1121   1.2  jmcneill 					break;
   1122   1.2  jmcneill 				default:
   1123   1.2  jmcneill 					pmcfg |= (15 <<
   1124   1.2  jmcneill 					    PM_CFG_L1_ENTRY_TIMER_SHIFT);
   1125   1.2  jmcneill 					break;
   1126   1.2  jmcneill 				}
   1127   1.2  jmcneill 			}
   1128   1.2  jmcneill 		} else {
   1129   1.2  jmcneill 			pmcfg |= PM_CFG_SERDES_L1_ENB |
   1130   1.2  jmcneill 			    PM_CFG_SERDES_PLL_L1_ENB |
   1131   1.2  jmcneill 			    PM_CFG_SERDES_BUDS_RX_L1_ENB;
   1132   1.2  jmcneill 			pmcfg &= ~(PM_CFG_CLK_SWH_L1 |
   1133   1.2  jmcneill 			    PM_CFG_ASPM_L1_ENB | PM_CFG_ASPM_L0S_ENB);
   1134   1.2  jmcneill 		}
   1135   1.1  jmcneill 	} else {
   1136   1.2  jmcneill 		pmcfg &= ~(PM_CFG_SERDES_BUDS_RX_L1_ENB | PM_CFG_SERDES_L1_ENB |
   1137   1.2  jmcneill 		    PM_CFG_SERDES_PLL_L1_ENB);
   1138   1.1  jmcneill 		pmcfg |= PM_CFG_CLK_SWH_L1;
   1139   1.2  jmcneill 		if ((sc->alc_flags & ALC_FLAG_L1S) != 0)
   1140   1.2  jmcneill 			pmcfg |= PM_CFG_ASPM_L1_ENB;
   1141   1.1  jmcneill 	}
   1142   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_PM_CFG, pmcfg);
   1143   1.1  jmcneill }
   1144   1.1  jmcneill 
   1145   1.1  jmcneill static void
   1146  1.12  christos alc_aspm_816x(struct alc_softc *sc, int init)
   1147  1.12  christos {
   1148  1.12  christos 	uint32_t pmcfg;
   1149  1.12  christos 
   1150  1.12  christos 	pmcfg = CSR_READ_4(sc, ALC_PM_CFG);
   1151  1.12  christos 	pmcfg &= ~PM_CFG_L1_ENTRY_TIMER_816X_MASK;
   1152  1.12  christos 	pmcfg |= PM_CFG_L1_ENTRY_TIMER_816X_DEFAULT;
   1153  1.12  christos 	pmcfg &= ~PM_CFG_PM_REQ_TIMER_MASK;
   1154  1.12  christos 	pmcfg |= PM_CFG_PM_REQ_TIMER_816X_DEFAULT;
   1155  1.12  christos 	pmcfg &= ~PM_CFG_LCKDET_TIMER_MASK;
   1156  1.12  christos 	pmcfg |= PM_CFG_LCKDET_TIMER_DEFAULT;
   1157  1.12  christos 	pmcfg |= PM_CFG_SERDES_PD_EX_L1 | PM_CFG_CLK_SWH_L1 | PM_CFG_PCIE_RECV;
   1158  1.12  christos 	pmcfg &= ~(PM_CFG_RX_L1_AFTER_L0S | PM_CFG_TX_L1_AFTER_L0S |
   1159  1.12  christos 	    PM_CFG_ASPM_L1_ENB | PM_CFG_ASPM_L0S_ENB |
   1160  1.12  christos 	    PM_CFG_SERDES_L1_ENB | PM_CFG_SERDES_PLL_L1_ENB |
   1161  1.12  christos 	    PM_CFG_SERDES_BUDS_RX_L1_ENB | PM_CFG_SA_DLY_ENB |
   1162  1.12  christos 	    PM_CFG_MAC_ASPM_CHK | PM_CFG_HOTRST);
   1163  1.12  christos 	if (AR816X_REV(sc->alc_rev) <= AR816X_REV_A1 &&
   1164  1.12  christos 	    (sc->alc_rev & 0x01) != 0)
   1165  1.12  christos 		pmcfg |= PM_CFG_SERDES_L1_ENB | PM_CFG_SERDES_PLL_L1_ENB;
   1166  1.12  christos 	if ((sc->alc_flags & ALC_FLAG_LINK) != 0) {
   1167  1.12  christos 		/* Link up, enable both L0s, L1s. */
   1168  1.12  christos 		pmcfg |= PM_CFG_ASPM_L0S_ENB | PM_CFG_ASPM_L1_ENB |
   1169  1.12  christos 		    PM_CFG_MAC_ASPM_CHK;
   1170  1.12  christos 	} else {
   1171  1.12  christos 		if (init != 0)
   1172  1.12  christos 			pmcfg |= PM_CFG_ASPM_L0S_ENB | PM_CFG_ASPM_L1_ENB |
   1173  1.12  christos 			    PM_CFG_MAC_ASPM_CHK;
   1174  1.12  christos 		else if ((sc->sc_ec.ec_if.if_flags & IFF_RUNNING) != 0)
   1175  1.12  christos 			pmcfg |= PM_CFG_ASPM_L1_ENB | PM_CFG_MAC_ASPM_CHK;
   1176  1.12  christos 	}
   1177  1.12  christos 	CSR_WRITE_4(sc, ALC_PM_CFG, pmcfg);
   1178  1.12  christos }
   1179  1.12  christos 
   1180  1.12  christos static void
   1181  1.40   msaitoh alc_init_pcie(struct alc_softc *sc)
   1182  1.40   msaitoh {
   1183  1.40   msaitoh 	const char *aspm_state[] = { "L0s/L1", "L0s", "L1", "L0s/L1" };
   1184  1.40   msaitoh 	uint32_t cap, ctl, val;
   1185  1.40   msaitoh 	int state;
   1186  1.40   msaitoh 
   1187  1.40   msaitoh 	/* Clear data link and flow-control protocol error. */
   1188  1.40   msaitoh 	val = CSR_READ_4(sc, ALC_PEX_UNC_ERR_SEV);
   1189  1.40   msaitoh 	val &= ~(PEX_UNC_ERR_SEV_DLP | PEX_UNC_ERR_SEV_FCP);
   1190  1.40   msaitoh 	CSR_WRITE_4(sc, ALC_PEX_UNC_ERR_SEV, val);
   1191  1.40   msaitoh 
   1192  1.40   msaitoh 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
   1193  1.40   msaitoh 		CSR_WRITE_4(sc, ALC_LTSSM_ID_CFG,
   1194  1.40   msaitoh 		    CSR_READ_4(sc, ALC_LTSSM_ID_CFG) & ~LTSSM_ID_WRO_ENB);
   1195  1.40   msaitoh 		CSR_WRITE_4(sc, ALC_PCIE_PHYMISC,
   1196  1.40   msaitoh 		    CSR_READ_4(sc, ALC_PCIE_PHYMISC) |
   1197  1.40   msaitoh 		    PCIE_PHYMISC_FORCE_RCV_DET);
   1198  1.40   msaitoh 		if (sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8152_B &&
   1199  1.40   msaitoh 		    sc->alc_rev == ATHEROS_AR8152_B_V10) {
   1200  1.40   msaitoh 			val = CSR_READ_4(sc, ALC_PCIE_PHYMISC2);
   1201  1.40   msaitoh 			val &= ~(PCIE_PHYMISC2_SERDES_CDR_MASK |
   1202  1.40   msaitoh 			    PCIE_PHYMISC2_SERDES_TH_MASK);
   1203  1.40   msaitoh 			val |= 3 << PCIE_PHYMISC2_SERDES_CDR_SHIFT;
   1204  1.40   msaitoh 			val |= 3 << PCIE_PHYMISC2_SERDES_TH_SHIFT;
   1205  1.40   msaitoh 			CSR_WRITE_4(sc, ALC_PCIE_PHYMISC2, val);
   1206  1.40   msaitoh 		}
   1207  1.40   msaitoh 		/* Disable ASPM L0S and L1. */
   1208  1.40   msaitoh 		cap = pci_conf_read(sc->sc_pct, sc->sc_pcitag,
   1209  1.40   msaitoh 		    sc->alc_expcap + PCIE_LCAP) >> 16;
   1210  1.40   msaitoh 		if ((cap & PCIE_LCAP_ASPM) != 0) {
   1211  1.40   msaitoh 			ctl = pci_conf_read(sc->sc_pct, sc->sc_pcitag,
   1212  1.40   msaitoh 			    sc->alc_expcap + PCIE_LCSR) >> 16;
   1213  1.40   msaitoh 			if ((ctl & 0x08) != 0)
   1214  1.40   msaitoh 				sc->alc_rcb = DMA_CFG_RCB_128;
   1215  1.40   msaitoh 			if (alcdebug)
   1216  1.40   msaitoh 				printf("%s: RCB %u bytes\n",
   1217  1.40   msaitoh 				    device_xname(sc->sc_dev),
   1218  1.40   msaitoh 				    sc->alc_rcb == DMA_CFG_RCB_64 ? 64 : 128);
   1219  1.40   msaitoh 			state = ctl & 0x03;
   1220  1.40   msaitoh 			if (state & 0x01)
   1221  1.40   msaitoh 				sc->alc_flags |= ALC_FLAG_L0S;
   1222  1.40   msaitoh 			if (state & 0x02)
   1223  1.40   msaitoh 				sc->alc_flags |= ALC_FLAG_L1S;
   1224  1.40   msaitoh 			if (alcdebug)
   1225  1.40   msaitoh 				printf("%s: ASPM %s %s\n",
   1226  1.40   msaitoh 				    device_xname(sc->sc_dev),
   1227  1.40   msaitoh 				    aspm_state[state],
   1228  1.40   msaitoh 				    state == 0 ? "disabled" : "enabled");
   1229  1.40   msaitoh 			alc_disable_l0s_l1(sc);
   1230  1.40   msaitoh 		} else {
   1231  1.40   msaitoh 			aprint_debug_dev(sc->sc_dev, "no ASPM support\n");
   1232  1.40   msaitoh 		}
   1233  1.40   msaitoh 	} else {
   1234  1.40   msaitoh 		val = CSR_READ_4(sc, ALC_PDLL_TRNS1);
   1235  1.40   msaitoh 		val &= ~PDLL_TRNS1_D3PLLOFF_ENB;
   1236  1.40   msaitoh 		CSR_WRITE_4(sc, ALC_PDLL_TRNS1, val);
   1237  1.40   msaitoh 		val = CSR_READ_4(sc, ALC_MASTER_CFG);
   1238  1.40   msaitoh 		if (AR816X_REV(sc->alc_rev) <= AR816X_REV_A1 &&
   1239  1.40   msaitoh 		    (sc->alc_rev & 0x01) != 0) {
   1240  1.40   msaitoh 			if ((val & MASTER_WAKEN_25M) == 0 ||
   1241  1.40   msaitoh 			    (val & MASTER_CLK_SEL_DIS) == 0) {
   1242  1.40   msaitoh 				val |= MASTER_WAKEN_25M | MASTER_CLK_SEL_DIS;
   1243  1.40   msaitoh 				CSR_WRITE_4(sc, ALC_MASTER_CFG, val);
   1244  1.40   msaitoh 			}
   1245  1.40   msaitoh 		} else {
   1246  1.40   msaitoh 			if ((val & MASTER_WAKEN_25M) == 0 ||
   1247  1.40   msaitoh 			    (val & MASTER_CLK_SEL_DIS) != 0) {
   1248  1.40   msaitoh 				val |= MASTER_WAKEN_25M;
   1249  1.40   msaitoh 				val &= ~MASTER_CLK_SEL_DIS;
   1250  1.40   msaitoh 				CSR_WRITE_4(sc, ALC_MASTER_CFG, val);
   1251  1.40   msaitoh 			}
   1252  1.40   msaitoh 		}
   1253  1.40   msaitoh 	}
   1254  1.40   msaitoh 	alc_aspm(sc, 1, IFM_UNKNOWN);
   1255  1.40   msaitoh }
   1256  1.40   msaitoh 
   1257  1.40   msaitoh static void
   1258   1.1  jmcneill alc_attach(device_t parent, device_t self, void *aux)
   1259   1.1  jmcneill {
   1260   1.1  jmcneill 
   1261   1.1  jmcneill 	struct alc_softc *sc = device_private(self);
   1262   1.1  jmcneill 	struct pci_attach_args *pa = aux;
   1263   1.1  jmcneill 	pci_chipset_tag_t pc = pa->pa_pc;
   1264   1.1  jmcneill 	pci_intr_handle_t ih;
   1265   1.1  jmcneill 	const char *intrstr;
   1266   1.1  jmcneill 	struct ifnet *ifp;
   1267  1.35   msaitoh 	struct mii_data * const mii = &sc->sc_miibus;
   1268   1.1  jmcneill 	pcireg_t memtype;
   1269   1.1  jmcneill 	uint16_t burst;
   1270  1.40   msaitoh 	int base, mii_flags, error = 0;
   1271  1.11  christos 	char intrbuf[PCI_INTRSTR_LEN];
   1272   1.1  jmcneill 
   1273   1.2  jmcneill 	sc->alc_ident = alc_find_ident(pa);
   1274  1.39   msaitoh 	sc->alc_rev = PCI_REVISION(pa->pa_class);
   1275   1.2  jmcneill 
   1276   1.1  jmcneill 	aprint_naive("\n");
   1277   1.2  jmcneill 	aprint_normal(": %s\n", sc->alc_ident->name);
   1278   1.1  jmcneill 
   1279   1.1  jmcneill 	sc->sc_dev = self;
   1280   1.1  jmcneill 	sc->sc_dmat = pa->pa_dmat;
   1281   1.1  jmcneill 	sc->sc_pct = pa->pa_pc;
   1282   1.1  jmcneill 	sc->sc_pcitag = pa->pa_tag;
   1283   1.1  jmcneill 
   1284   1.1  jmcneill 	/*
   1285   1.1  jmcneill 	 * Allocate IO memory
   1286   1.1  jmcneill 	 */
   1287   1.1  jmcneill 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, ALC_PCIR_BAR);
   1288   1.1  jmcneill 	switch (memtype) {
   1289   1.1  jmcneill 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
   1290   1.1  jmcneill 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT_1M:
   1291   1.1  jmcneill 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
   1292   1.1  jmcneill 		break;
   1293   1.1  jmcneill 	default:
   1294   1.1  jmcneill 		aprint_error_dev(self, "invalid base address register\n");
   1295   1.1  jmcneill 		break;
   1296   1.1  jmcneill 	}
   1297   1.1  jmcneill 
   1298   1.1  jmcneill 	if (pci_mapreg_map(pa, ALC_PCIR_BAR, memtype, 0, &sc->sc_mem_bt,
   1299   1.1  jmcneill 	    &sc->sc_mem_bh, NULL, &sc->sc_mem_size)) {
   1300   1.1  jmcneill 		aprint_error_dev(self, "could not map mem space\n");
   1301   1.1  jmcneill 		return;
   1302   1.1  jmcneill 	}
   1303   1.1  jmcneill 
   1304   1.1  jmcneill 	if (pci_intr_map(pa, &ih) != 0) {
   1305   1.1  jmcneill 		printf(": can't map interrupt\n");
   1306   1.1  jmcneill 		goto fail;
   1307   1.1  jmcneill 	}
   1308   1.1  jmcneill 
   1309   1.1  jmcneill 	/*
   1310   1.1  jmcneill 	 * Allocate IRQ
   1311   1.1  jmcneill 	 */
   1312  1.11  christos 	intrstr = pci_intr_string(sc->sc_pct, ih, intrbuf, sizeof(intrbuf));
   1313  1.29  jdolecek 	sc->sc_irq_handle = pci_intr_establish_xname(pc, ih, IPL_NET, alc_intr,
   1314  1.29  jdolecek 	    sc, device_xname(self));
   1315   1.1  jmcneill 	if (sc->sc_irq_handle == NULL) {
   1316   1.1  jmcneill 		printf(": could not establish interrupt");
   1317   1.1  jmcneill 		if (intrstr != NULL)
   1318   1.1  jmcneill 			printf(" at %s", intrstr);
   1319   1.1  jmcneill 		printf("\n");
   1320   1.1  jmcneill 		goto fail;
   1321   1.1  jmcneill 	}
   1322   1.4      matt 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
   1323   1.8  christos 
   1324   1.1  jmcneill 	/* Set PHY address. */
   1325   1.1  jmcneill 	sc->alc_phyaddr = ALC_PHY_ADDR;
   1326   1.1  jmcneill 
   1327   1.1  jmcneill 	/* Initialize DMA parameters. */
   1328   1.1  jmcneill 	sc->alc_dma_rd_burst = 0;
   1329   1.1  jmcneill 	sc->alc_dma_wr_burst = 0;
   1330   1.1  jmcneill 	sc->alc_rcb = DMA_CFG_RCB_64;
   1331   1.1  jmcneill 	if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PCIEXPRESS,
   1332   1.1  jmcneill 	    &base, NULL)) {
   1333   1.1  jmcneill 		sc->alc_flags |= ALC_FLAG_PCIE;
   1334   1.2  jmcneill 		sc->alc_expcap = base;
   1335   1.1  jmcneill 		burst = pci_conf_read(sc->sc_pct, sc->sc_pcitag,
   1336   1.9   msaitoh 		    base + PCIE_DCSR) >> 16;
   1337   1.1  jmcneill 		sc->alc_dma_rd_burst = (burst & 0x7000) >> 12;
   1338   1.1  jmcneill 		sc->alc_dma_wr_burst = (burst & 0x00e0) >> 5;
   1339   1.1  jmcneill 		if (alcdebug) {
   1340   1.1  jmcneill 			printf("%s: Read request size : %u bytes.\n",
   1341   1.8  christos 			    device_xname(sc->sc_dev),
   1342   1.1  jmcneill 			    alc_dma_burst[sc->alc_dma_rd_burst]);
   1343   1.1  jmcneill 			printf("%s: TLP payload size : %u bytes.\n",
   1344   1.1  jmcneill 			    device_xname(sc->sc_dev),
   1345   1.1  jmcneill 			    alc_dma_burst[sc->alc_dma_wr_burst]);
   1346   1.1  jmcneill 		}
   1347  1.12  christos 		if (alc_dma_burst[sc->alc_dma_rd_burst] > 1024)
   1348  1.12  christos 			sc->alc_dma_rd_burst = 3;
   1349  1.12  christos 		if (alc_dma_burst[sc->alc_dma_wr_burst] > 1024)
   1350  1.12  christos 			sc->alc_dma_wr_burst = 3;
   1351  1.42   msaitoh 		/*
   1352  1.42   msaitoh 		 * Force maximum payload size to 128 bytes for
   1353  1.42   msaitoh 		 * E2200/E2400/E2500.
   1354  1.42   msaitoh 		 * Otherwise it triggers DMA write error.
   1355  1.42   msaitoh 		 */
   1356  1.42   msaitoh 		if ((sc->alc_flags & ALC_FLAG_E2X00) != 0)
   1357  1.42   msaitoh 			sc->alc_dma_wr_burst = 0;
   1358  1.40   msaitoh 		alc_init_pcie(sc);
   1359   1.1  jmcneill 	}
   1360   1.1  jmcneill 
   1361   1.1  jmcneill 	/* Reset PHY. */
   1362   1.1  jmcneill 	alc_phy_reset(sc);
   1363   1.1  jmcneill 
   1364   1.1  jmcneill 	/* Reset the ethernet controller. */
   1365  1.12  christos 	alc_stop_mac(sc);
   1366   1.1  jmcneill 	alc_reset(sc);
   1367   1.1  jmcneill 
   1368   1.1  jmcneill 	/*
   1369   1.1  jmcneill 	 * One odd thing is AR8132 uses the same PHY hardware(F1
   1370   1.1  jmcneill 	 * gigabit PHY) of AR8131. So atphy(4) of AR8132 reports
   1371   1.1  jmcneill 	 * the PHY supports 1000Mbps but that's not true. The PHY
   1372   1.1  jmcneill 	 * used in AR8132 can't establish gigabit link even if it
   1373   1.1  jmcneill 	 * shows the same PHY model/revision number of AR8131.
   1374   1.1  jmcneill 	 */
   1375   1.2  jmcneill 	switch (sc->alc_ident->deviceid) {
   1376  1.42   msaitoh 	case PCI_PRODUCT_ATTANSIC_E2200:
   1377  1.42   msaitoh 	case PCI_PRODUCT_ATTANSIC_E2400:
   1378  1.42   msaitoh 	case PCI_PRODUCT_ATTANSIC_E2500:
   1379  1.42   msaitoh 		sc->alc_flags |= ALC_FLAG_E2X00;
   1380  1.42   msaitoh 		/* FALLTHROUGH */
   1381  1.12  christos 	case PCI_PRODUCT_ATTANSIC_AR8161:
   1382  1.12  christos 		if (PCI_SUBSYS_ID(pci_conf_read(
   1383  1.12  christos 		   sc->sc_pct, sc->sc_pcitag, PCI_SUBSYS_ID_REG)) == 0x0091 &&
   1384  1.12  christos 		   sc->alc_rev == 0)
   1385  1.12  christos 			sc->alc_flags |= ALC_FLAG_LINK_WAR;
   1386  1.12  christos 		/* FALLTHROUGH */
   1387  1.12  christos 	case PCI_PRODUCT_ATTANSIC_AR8171:
   1388  1.12  christos 		sc->alc_flags |= ALC_FLAG_AR816X_FAMILY;
   1389  1.12  christos 		break;
   1390  1.12  christos 	case PCI_PRODUCT_ATTANSIC_AR8162:
   1391  1.12  christos 	case PCI_PRODUCT_ATTANSIC_AR8172:
   1392  1.12  christos 		sc->alc_flags |= ALC_FLAG_FASTETHER | ALC_FLAG_AR816X_FAMILY;
   1393  1.12  christos 		break;
   1394   1.2  jmcneill 	case PCI_PRODUCT_ATTANSIC_AR8152_B:
   1395   1.2  jmcneill 	case PCI_PRODUCT_ATTANSIC_AR8152_B2:
   1396   1.2  jmcneill 		sc->alc_flags |= ALC_FLAG_APS;
   1397   1.2  jmcneill 		/* FALLTHROUGH */
   1398   1.1  jmcneill 	case PCI_PRODUCT_ATTANSIC_AR8132:
   1399   1.2  jmcneill 		sc->alc_flags |= ALC_FLAG_FASTETHER;
   1400   1.1  jmcneill 		break;
   1401   1.2  jmcneill 	case PCI_PRODUCT_ATTANSIC_AR8151:
   1402   1.2  jmcneill 	case PCI_PRODUCT_ATTANSIC_AR8151_V2:
   1403   1.2  jmcneill 		sc->alc_flags |= ALC_FLAG_APS;
   1404   1.2  jmcneill 		/* FALLTHROUGH */
   1405   1.1  jmcneill 	default:
   1406   1.1  jmcneill 		break;
   1407   1.1  jmcneill 	}
   1408  1.12  christos 	sc->alc_flags |= ALC_FLAG_JUMBO;
   1409   1.1  jmcneill 
   1410   1.1  jmcneill 	/*
   1411   1.2  jmcneill 	 * It seems that AR813x/AR815x has silicon bug for SMB. In
   1412   1.1  jmcneill 	 * addition, Atheros said that enabling SMB wouldn't improve
   1413   1.1  jmcneill 	 * performance. However I think it's bad to access lots of
   1414   1.1  jmcneill 	 * registers to extract MAC statistics.
   1415   1.1  jmcneill 	 */
   1416   1.1  jmcneill 	sc->alc_flags |= ALC_FLAG_SMB_BUG;
   1417   1.1  jmcneill 	/*
   1418   1.1  jmcneill 	 * Don't use Tx CMB. It is known to have silicon bug.
   1419   1.1  jmcneill 	 */
   1420   1.1  jmcneill 	sc->alc_flags |= ALC_FLAG_CMB_BUG;
   1421   1.1  jmcneill 	sc->alc_chip_rev = CSR_READ_4(sc, ALC_MASTER_CFG) >>
   1422   1.1  jmcneill 	    MASTER_CHIP_REV_SHIFT;
   1423   1.1  jmcneill 	if (alcdebug) {
   1424   1.1  jmcneill 		printf("%s: PCI device revision : 0x%04x\n",
   1425   1.1  jmcneill 		    device_xname(sc->sc_dev), sc->alc_rev);
   1426   1.1  jmcneill 		printf("%s: Chip id/revision : 0x%04x\n",
   1427   1.1  jmcneill 		    device_xname(sc->sc_dev), sc->alc_chip_rev);
   1428   1.1  jmcneill 		printf("%s: %u Tx FIFO, %u Rx FIFO\n", device_xname(sc->sc_dev),
   1429   1.1  jmcneill 		    CSR_READ_4(sc, ALC_SRAM_TX_FIFO_LEN) * 8,
   1430   1.1  jmcneill 		    CSR_READ_4(sc, ALC_SRAM_RX_FIFO_LEN) * 8);
   1431   1.1  jmcneill 	}
   1432   1.1  jmcneill 
   1433   1.1  jmcneill 	error = alc_dma_alloc(sc);
   1434   1.1  jmcneill 	if (error)
   1435   1.1  jmcneill 		goto fail;
   1436   1.1  jmcneill 
   1437   1.1  jmcneill 	callout_init(&sc->sc_tick_ch, 0);
   1438   1.1  jmcneill 	callout_setfunc(&sc->sc_tick_ch, alc_tick, sc);
   1439   1.1  jmcneill 
   1440   1.1  jmcneill 	/* Load station address. */
   1441   1.1  jmcneill 	alc_get_macaddr(sc);
   1442   1.1  jmcneill 
   1443   1.1  jmcneill 	aprint_normal_dev(self, "Ethernet address %s\n",
   1444   1.1  jmcneill 	    ether_sprintf(sc->alc_eaddr));
   1445   1.1  jmcneill 
   1446   1.1  jmcneill 	ifp = &sc->sc_ec.ec_if;
   1447   1.1  jmcneill 	ifp->if_softc = sc;
   1448   1.1  jmcneill 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
   1449   1.1  jmcneill 	ifp->if_init = alc_init;
   1450   1.1  jmcneill 	ifp->if_ioctl = alc_ioctl;
   1451   1.1  jmcneill 	ifp->if_start = alc_start;
   1452   1.1  jmcneill 	ifp->if_stop = alc_stop;
   1453   1.1  jmcneill 	ifp->if_watchdog = alc_watchdog;
   1454   1.1  jmcneill 	IFQ_SET_MAXLEN(&ifp->if_snd, ALC_TX_RING_CNT - 1);
   1455   1.1  jmcneill 	IFQ_SET_READY(&ifp->if_snd);
   1456   1.1  jmcneill 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
   1457   1.1  jmcneill 
   1458   1.1  jmcneill 	sc->sc_ec.ec_capabilities = ETHERCAP_VLAN_MTU;
   1459   1.1  jmcneill 
   1460   1.1  jmcneill #ifdef ALC_CHECKSUM
   1461   1.1  jmcneill 	ifp->if_capabilities |= IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx |
   1462   1.1  jmcneill 				IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
   1463  1.18  christos 				IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx;
   1464   1.1  jmcneill #endif
   1465   1.1  jmcneill 
   1466   1.1  jmcneill #if NVLAN > 0
   1467   1.1  jmcneill 	sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_HWTAGGING;
   1468  1.38   msaitoh 	sc->sc_ec.ec_capenable |= ETHERCAP_VLAN_HWTAGGING;
   1469   1.1  jmcneill #endif
   1470   1.1  jmcneill 
   1471  1.12  christos 	/*
   1472  1.12  christos 	 * XXX
   1473  1.12  christos 	 * It seems enabling Tx checksum offloading makes more trouble.
   1474  1.12  christos 	 * Sometimes the controller does not receive any frames when
   1475  1.12  christos 	 * Tx checksum offloading is enabled. I'm not sure whether this
   1476  1.12  christos 	 * is a bug in Tx checksum offloading logic or I got broken
   1477  1.12  christos 	 * sample boards. To safety, don't enable Tx checksum offloading
   1478  1.12  christos 	 * by default but give chance to users to toggle it if they know
   1479  1.12  christos 	 * their controllers work without problems.
   1480  1.12  christos 	 * Fortunately, Tx checksum offloading for AR816x family
   1481  1.12  christos 	 * seems to work.
   1482  1.12  christos 	 */
   1483  1.12  christos 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
   1484  1.12  christos 		ifp->if_capenable &= ~IFCAP_CSUM_IPv4_Tx;
   1485  1.12  christos 		ifp->if_capabilities &= ~ALC_CSUM_FEATURES;
   1486  1.12  christos 	}
   1487  1.12  christos 
   1488   1.1  jmcneill 	/* Set up MII bus. */
   1489  1.35   msaitoh 	mii->mii_ifp = ifp;
   1490  1.35   msaitoh 	mii->mii_readreg = alc_miibus_readreg;
   1491  1.35   msaitoh 	mii->mii_writereg = alc_miibus_writereg;
   1492  1.35   msaitoh 	mii->mii_statchg = alc_miibus_statchg;
   1493  1.35   msaitoh 
   1494  1.35   msaitoh 	sc->sc_ec.ec_mii = mii;
   1495  1.35   msaitoh 	ifmedia_init(&mii->mii_media, 0, alc_mediachange, alc_mediastatus);
   1496   1.1  jmcneill 	mii_flags = 0;
   1497   1.1  jmcneill 	if ((sc->alc_flags & ALC_FLAG_JUMBO) != 0)
   1498   1.1  jmcneill 		mii_flags |= MIIF_DOPAUSE;
   1499  1.35   msaitoh 	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY,
   1500   1.1  jmcneill 		MII_OFFSET_ANY, mii_flags);
   1501   1.1  jmcneill 
   1502  1.35   msaitoh 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
   1503   1.1  jmcneill 		printf("%s: no PHY found!\n", device_xname(sc->sc_dev));
   1504  1.35   msaitoh 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_MANUAL,
   1505   1.1  jmcneill 		    0, NULL);
   1506  1.35   msaitoh 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_MANUAL);
   1507   1.8  christos 	} else
   1508  1.35   msaitoh 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
   1509   1.1  jmcneill 
   1510   1.1  jmcneill 	if_attach(ifp);
   1511  1.22     ozaki 	if_deferred_start_init(ifp, NULL);
   1512   1.1  jmcneill 	ether_ifattach(ifp, sc->alc_eaddr);
   1513   1.1  jmcneill 
   1514   1.1  jmcneill 	if (!pmf_device_register(self, NULL, NULL))
   1515   1.1  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
   1516   1.1  jmcneill 	else
   1517   1.1  jmcneill 		pmf_class_network_register(self, ifp);
   1518   1.1  jmcneill 
   1519   1.1  jmcneill 	return;
   1520   1.1  jmcneill fail:
   1521   1.1  jmcneill 	alc_dma_free(sc);
   1522   1.1  jmcneill 	if (sc->sc_irq_handle != NULL) {
   1523   1.1  jmcneill 		pci_intr_disestablish(sc->sc_pct, sc->sc_irq_handle);
   1524   1.1  jmcneill 		sc->sc_irq_handle = NULL;
   1525   1.1  jmcneill 	}
   1526   1.1  jmcneill 	if (sc->sc_mem_size) {
   1527   1.1  jmcneill 		bus_space_unmap(sc->sc_mem_bt, sc->sc_mem_bh, sc->sc_mem_size);
   1528   1.1  jmcneill 		sc->sc_mem_size = 0;
   1529   1.1  jmcneill 	}
   1530   1.1  jmcneill }
   1531   1.1  jmcneill 
   1532   1.1  jmcneill static int
   1533   1.1  jmcneill alc_detach(device_t self, int flags)
   1534   1.1  jmcneill {
   1535   1.1  jmcneill 	struct alc_softc *sc = device_private(self);
   1536   1.1  jmcneill 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1537   1.1  jmcneill 	int s;
   1538   1.1  jmcneill 
   1539   1.1  jmcneill 	s = splnet();
   1540   1.1  jmcneill 	alc_stop(ifp, 0);
   1541   1.1  jmcneill 	splx(s);
   1542   1.1  jmcneill 
   1543   1.1  jmcneill 	mii_detach(&sc->sc_miibus, MII_PHY_ANY, MII_OFFSET_ANY);
   1544   1.1  jmcneill 
   1545   1.1  jmcneill 	ether_ifdetach(ifp);
   1546   1.1  jmcneill 	if_detach(ifp);
   1547   1.1  jmcneill 	alc_dma_free(sc);
   1548   1.1  jmcneill 
   1549  1.48   thorpej 	/* Delete all remaining media. */
   1550  1.48   thorpej 	ifmedia_fini(&sc->sc_miibus.mii_media);
   1551  1.48   thorpej 
   1552   1.1  jmcneill 	alc_phy_down(sc);
   1553   1.1  jmcneill 	if (sc->sc_irq_handle != NULL) {
   1554   1.1  jmcneill 		pci_intr_disestablish(sc->sc_pct, sc->sc_irq_handle);
   1555   1.1  jmcneill 		sc->sc_irq_handle = NULL;
   1556   1.1  jmcneill 	}
   1557   1.1  jmcneill 	if (sc->sc_mem_size) {
   1558   1.1  jmcneill 		bus_space_unmap(sc->sc_mem_bt, sc->sc_mem_bh, sc->sc_mem_size);
   1559   1.1  jmcneill 		sc->sc_mem_size = 0;
   1560   1.1  jmcneill 	}
   1561   1.1  jmcneill 
   1562   1.1  jmcneill 	return (0);
   1563   1.1  jmcneill }
   1564   1.1  jmcneill 
   1565   1.1  jmcneill static int
   1566   1.1  jmcneill alc_dma_alloc(struct alc_softc *sc)
   1567   1.1  jmcneill {
   1568   1.1  jmcneill 	struct alc_txdesc *txd;
   1569   1.1  jmcneill 	struct alc_rxdesc *rxd;
   1570   1.1  jmcneill 	int nsegs, error, i;
   1571   1.1  jmcneill 
   1572   1.1  jmcneill 	/*
   1573   1.1  jmcneill 	 * Create DMA stuffs for TX ring
   1574   1.1  jmcneill 	 */
   1575   1.1  jmcneill 	error = bus_dmamap_create(sc->sc_dmat, ALC_TX_RING_SZ, 1,
   1576   1.1  jmcneill 	    ALC_TX_RING_SZ, 0, BUS_DMA_NOWAIT, &sc->alc_cdata.alc_tx_ring_map);
   1577   1.1  jmcneill 	if (error) {
   1578   1.1  jmcneill 		sc->alc_cdata.alc_tx_ring_map = NULL;
   1579   1.1  jmcneill 		return (ENOBUFS);
   1580   1.1  jmcneill 	}
   1581   1.1  jmcneill 
   1582   1.1  jmcneill 	/* Allocate DMA'able memory for TX ring */
   1583   1.1  jmcneill 	error = bus_dmamem_alloc(sc->sc_dmat, ALC_TX_RING_SZ,
   1584   1.1  jmcneill 	    ETHER_ALIGN, 0, &sc->alc_rdata.alc_tx_ring_seg, 1,
   1585   1.1  jmcneill 	    &nsegs, BUS_DMA_NOWAIT);
   1586   1.1  jmcneill 	if (error) {
   1587   1.1  jmcneill 		printf("%s: could not allocate DMA'able memory for Tx ring.\n",
   1588   1.1  jmcneill 		    device_xname(sc->sc_dev));
   1589   1.1  jmcneill 		return error;
   1590   1.1  jmcneill 	}
   1591   1.1  jmcneill 
   1592   1.1  jmcneill 	error = bus_dmamem_map(sc->sc_dmat, &sc->alc_rdata.alc_tx_ring_seg,
   1593   1.1  jmcneill 	    nsegs, ALC_TX_RING_SZ, (void **)&sc->alc_rdata.alc_tx_ring,
   1594   1.1  jmcneill 	    BUS_DMA_NOWAIT);
   1595   1.1  jmcneill 	if (error)
   1596   1.1  jmcneill 		return (ENOBUFS);
   1597   1.1  jmcneill 
   1598   1.1  jmcneill 	/* Load the DMA map for Tx ring. */
   1599   1.1  jmcneill 	error = bus_dmamap_load(sc->sc_dmat, sc->alc_cdata.alc_tx_ring_map,
   1600   1.1  jmcneill 	    sc->alc_rdata.alc_tx_ring, ALC_TX_RING_SZ, NULL, BUS_DMA_WAITOK);
   1601   1.1  jmcneill 	if (error) {
   1602   1.1  jmcneill 		printf("%s: could not load DMA'able memory for Tx ring.\n",
   1603   1.1  jmcneill 		    device_xname(sc->sc_dev));
   1604   1.8  christos 		bus_dmamem_free(sc->sc_dmat,
   1605   1.1  jmcneill 		    &sc->alc_rdata.alc_tx_ring_seg, 1);
   1606   1.1  jmcneill 		return error;
   1607   1.1  jmcneill 	}
   1608   1.1  jmcneill 
   1609   1.8  christos 	sc->alc_rdata.alc_tx_ring_paddr =
   1610   1.1  jmcneill 	    sc->alc_cdata.alc_tx_ring_map->dm_segs[0].ds_addr;
   1611   1.1  jmcneill 
   1612   1.1  jmcneill 	/*
   1613   1.1  jmcneill 	 * Create DMA stuffs for RX ring
   1614   1.1  jmcneill 	 */
   1615   1.1  jmcneill 	error = bus_dmamap_create(sc->sc_dmat, ALC_RX_RING_SZ, 1,
   1616   1.1  jmcneill 	    ALC_RX_RING_SZ, 0, BUS_DMA_NOWAIT, &sc->alc_cdata.alc_rx_ring_map);
   1617   1.1  jmcneill 	if (error)
   1618   1.1  jmcneill 		return (ENOBUFS);
   1619   1.8  christos 
   1620   1.1  jmcneill 	/* Allocate DMA'able memory for RX ring */
   1621   1.1  jmcneill 	error = bus_dmamem_alloc(sc->sc_dmat, ALC_RX_RING_SZ,
   1622   1.1  jmcneill 	    ETHER_ALIGN, 0, &sc->alc_rdata.alc_rx_ring_seg, 1,
   1623   1.1  jmcneill 	    &nsegs, BUS_DMA_NOWAIT);
   1624   1.1  jmcneill 	if (error) {
   1625   1.1  jmcneill 		printf("%s: could not allocate DMA'able memory for Rx ring.\n",
   1626   1.1  jmcneill 		    device_xname(sc->sc_dev));
   1627   1.1  jmcneill 		return error;
   1628   1.1  jmcneill 	}
   1629   1.1  jmcneill 
   1630   1.1  jmcneill 	error = bus_dmamem_map(sc->sc_dmat, &sc->alc_rdata.alc_rx_ring_seg,
   1631   1.1  jmcneill 	    nsegs, ALC_RX_RING_SZ, (void **)&sc->alc_rdata.alc_rx_ring,
   1632   1.1  jmcneill 	    BUS_DMA_NOWAIT);
   1633   1.1  jmcneill 	if (error)
   1634   1.1  jmcneill 		return (ENOBUFS);
   1635   1.1  jmcneill 
   1636   1.1  jmcneill 	/* Load the DMA map for Rx ring. */
   1637   1.1  jmcneill 	error = bus_dmamap_load(sc->sc_dmat, sc->alc_cdata.alc_rx_ring_map,
   1638   1.1  jmcneill 	    sc->alc_rdata.alc_rx_ring, ALC_RX_RING_SZ, NULL, BUS_DMA_WAITOK);
   1639   1.1  jmcneill 	if (error) {
   1640   1.1  jmcneill 		printf("%s: could not load DMA'able memory for Rx ring.\n",
   1641   1.1  jmcneill 		    device_xname(sc->sc_dev));
   1642   1.1  jmcneill 		bus_dmamem_free(sc->sc_dmat,
   1643   1.1  jmcneill 		    &sc->alc_rdata.alc_rx_ring_seg, 1);
   1644   1.1  jmcneill 		return error;
   1645   1.1  jmcneill 	}
   1646   1.1  jmcneill 
   1647   1.1  jmcneill 	sc->alc_rdata.alc_rx_ring_paddr =
   1648   1.1  jmcneill 	    sc->alc_cdata.alc_rx_ring_map->dm_segs[0].ds_addr;
   1649   1.1  jmcneill 
   1650   1.1  jmcneill 	/*
   1651   1.1  jmcneill 	 * Create DMA stuffs for RX return ring
   1652   1.1  jmcneill 	 */
   1653   1.8  christos 	error = bus_dmamap_create(sc->sc_dmat, ALC_RR_RING_SZ, 1,
   1654   1.1  jmcneill 	    ALC_RR_RING_SZ, 0, BUS_DMA_NOWAIT, &sc->alc_cdata.alc_rr_ring_map);
   1655   1.1  jmcneill 	if (error)
   1656   1.1  jmcneill 		return (ENOBUFS);
   1657   1.1  jmcneill 
   1658   1.1  jmcneill 	/* Allocate DMA'able memory for RX return ring */
   1659   1.8  christos 	error = bus_dmamem_alloc(sc->sc_dmat, ALC_RR_RING_SZ,
   1660   1.8  christos 	    ETHER_ALIGN, 0, &sc->alc_rdata.alc_rr_ring_seg, 1,
   1661   1.1  jmcneill 	    &nsegs, BUS_DMA_NOWAIT);
   1662   1.1  jmcneill 	if (error) {
   1663   1.1  jmcneill 		printf("%s: could not allocate DMA'able memory for Rx "
   1664   1.1  jmcneill 		    "return ring.\n", device_xname(sc->sc_dev));
   1665   1.1  jmcneill 		return error;
   1666   1.1  jmcneill 	}
   1667   1.1  jmcneill 
   1668   1.1  jmcneill 	error = bus_dmamem_map(sc->sc_dmat, &sc->alc_rdata.alc_rr_ring_seg,
   1669   1.1  jmcneill 	    nsegs, ALC_RR_RING_SZ, (void **)&sc->alc_rdata.alc_rr_ring,
   1670   1.1  jmcneill 	    BUS_DMA_NOWAIT);
   1671   1.1  jmcneill 	if (error)
   1672   1.1  jmcneill 		return (ENOBUFS);
   1673   1.1  jmcneill 
   1674   1.1  jmcneill 	/*  Load the DMA map for Rx return ring. */
   1675   1.1  jmcneill 	error = bus_dmamap_load(sc->sc_dmat, sc->alc_cdata.alc_rr_ring_map,
   1676   1.1  jmcneill 	    sc->alc_rdata.alc_rr_ring, ALC_RR_RING_SZ, NULL, BUS_DMA_WAITOK);
   1677   1.1  jmcneill 	if (error) {
   1678   1.1  jmcneill 		printf("%s: could not load DMA'able memory for Rx return ring."
   1679   1.1  jmcneill 		    "\n", device_xname(sc->sc_dev));
   1680   1.1  jmcneill 		bus_dmamem_free(sc->sc_dmat,
   1681   1.1  jmcneill 		    &sc->alc_rdata.alc_rr_ring_seg, 1);
   1682   1.1  jmcneill 		return error;
   1683   1.1  jmcneill 	}
   1684   1.1  jmcneill 
   1685   1.8  christos 	sc->alc_rdata.alc_rr_ring_paddr =
   1686   1.1  jmcneill 	    sc->alc_cdata.alc_rr_ring_map->dm_segs[0].ds_addr;
   1687   1.1  jmcneill 
   1688   1.1  jmcneill 	/*
   1689   1.8  christos 	 * Create DMA stuffs for CMB block
   1690   1.1  jmcneill 	 */
   1691   1.8  christos 	error = bus_dmamap_create(sc->sc_dmat, ALC_CMB_SZ, 1,
   1692   1.8  christos 	    ALC_CMB_SZ, 0, BUS_DMA_NOWAIT,
   1693   1.1  jmcneill 	    &sc->alc_cdata.alc_cmb_map);
   1694   1.8  christos 	if (error)
   1695   1.1  jmcneill 		return (ENOBUFS);
   1696   1.1  jmcneill 
   1697   1.1  jmcneill 	/* Allocate DMA'able memory for CMB block */
   1698   1.8  christos 	error = bus_dmamem_alloc(sc->sc_dmat, ALC_CMB_SZ,
   1699   1.8  christos 	    ETHER_ALIGN, 0, &sc->alc_rdata.alc_cmb_seg, 1,
   1700   1.1  jmcneill 	    &nsegs, BUS_DMA_NOWAIT);
   1701   1.1  jmcneill 	if (error) {
   1702   1.1  jmcneill 		printf("%s: could not allocate DMA'able memory for "
   1703   1.1  jmcneill 		    "CMB block\n", device_xname(sc->sc_dev));
   1704   1.1  jmcneill 		return error;
   1705   1.1  jmcneill 	}
   1706   1.1  jmcneill 
   1707   1.1  jmcneill 	error = bus_dmamem_map(sc->sc_dmat, &sc->alc_rdata.alc_cmb_seg,
   1708   1.1  jmcneill 	    nsegs, ALC_CMB_SZ, (void **)&sc->alc_rdata.alc_cmb,
   1709   1.1  jmcneill 	    BUS_DMA_NOWAIT);
   1710   1.1  jmcneill 	if (error)
   1711   1.1  jmcneill 		return (ENOBUFS);
   1712   1.1  jmcneill 
   1713   1.1  jmcneill 	/*  Load the DMA map for CMB block. */
   1714   1.1  jmcneill 	error = bus_dmamap_load(sc->sc_dmat, sc->alc_cdata.alc_cmb_map,
   1715   1.8  christos 	    sc->alc_rdata.alc_cmb, ALC_CMB_SZ, NULL,
   1716   1.1  jmcneill 	    BUS_DMA_WAITOK);
   1717   1.1  jmcneill 	if (error) {
   1718   1.1  jmcneill 		printf("%s: could not load DMA'able memory for CMB block\n",
   1719   1.1  jmcneill 		    device_xname(sc->sc_dev));
   1720   1.1  jmcneill 		bus_dmamem_free(sc->sc_dmat,
   1721   1.1  jmcneill 		    &sc->alc_rdata.alc_cmb_seg, 1);
   1722   1.1  jmcneill 		return error;
   1723   1.1  jmcneill 	}
   1724   1.1  jmcneill 
   1725   1.8  christos 	sc->alc_rdata.alc_cmb_paddr =
   1726   1.1  jmcneill 	    sc->alc_cdata.alc_cmb_map->dm_segs[0].ds_addr;
   1727   1.1  jmcneill 
   1728   1.1  jmcneill 	/*
   1729   1.1  jmcneill 	 * Create DMA stuffs for SMB block
   1730   1.1  jmcneill 	 */
   1731   1.8  christos 	error = bus_dmamap_create(sc->sc_dmat, ALC_SMB_SZ, 1,
   1732   1.8  christos 	    ALC_SMB_SZ, 0, BUS_DMA_NOWAIT,
   1733   1.1  jmcneill 	    &sc->alc_cdata.alc_smb_map);
   1734   1.1  jmcneill 	if (error)
   1735   1.1  jmcneill 		return (ENOBUFS);
   1736   1.1  jmcneill 
   1737   1.1  jmcneill 	/* Allocate DMA'able memory for SMB block */
   1738   1.8  christos 	error = bus_dmamem_alloc(sc->sc_dmat, ALC_SMB_SZ,
   1739   1.8  christos 	    ETHER_ALIGN, 0, &sc->alc_rdata.alc_smb_seg, 1,
   1740   1.1  jmcneill 	    &nsegs, BUS_DMA_NOWAIT);
   1741   1.1  jmcneill 	if (error) {
   1742   1.1  jmcneill 		printf("%s: could not allocate DMA'able memory for "
   1743   1.1  jmcneill 		    "SMB block\n", device_xname(sc->sc_dev));
   1744   1.1  jmcneill 		return error;
   1745   1.1  jmcneill 	}
   1746   1.1  jmcneill 
   1747   1.1  jmcneill 	error = bus_dmamem_map(sc->sc_dmat, &sc->alc_rdata.alc_smb_seg,
   1748   1.1  jmcneill 	    nsegs, ALC_SMB_SZ, (void **)&sc->alc_rdata.alc_smb,
   1749   1.1  jmcneill 	    BUS_DMA_NOWAIT);
   1750   1.1  jmcneill 	if (error)
   1751   1.1  jmcneill 		return (ENOBUFS);
   1752   1.1  jmcneill 
   1753   1.1  jmcneill 	/*  Load the DMA map for SMB block */
   1754   1.1  jmcneill 	error = bus_dmamap_load(sc->sc_dmat, sc->alc_cdata.alc_smb_map,
   1755   1.8  christos 	    sc->alc_rdata.alc_smb, ALC_SMB_SZ, NULL,
   1756   1.1  jmcneill 	    BUS_DMA_WAITOK);
   1757   1.1  jmcneill 	if (error) {
   1758   1.1  jmcneill 		printf("%s: could not load DMA'able memory for SMB block\n",
   1759   1.1  jmcneill 		    device_xname(sc->sc_dev));
   1760   1.1  jmcneill 		bus_dmamem_free(sc->sc_dmat,
   1761   1.1  jmcneill 		    &sc->alc_rdata.alc_smb_seg, 1);
   1762   1.1  jmcneill 		return error;
   1763   1.1  jmcneill 	}
   1764   1.1  jmcneill 
   1765   1.8  christos 	sc->alc_rdata.alc_smb_paddr =
   1766   1.1  jmcneill 	    sc->alc_cdata.alc_smb_map->dm_segs[0].ds_addr;
   1767   1.1  jmcneill 
   1768   1.1  jmcneill 
   1769   1.1  jmcneill 	/* Create DMA maps for Tx buffers. */
   1770   1.1  jmcneill 	for (i = 0; i < ALC_TX_RING_CNT; i++) {
   1771   1.1  jmcneill 		txd = &sc->alc_cdata.alc_txdesc[i];
   1772   1.1  jmcneill 		txd->tx_m = NULL;
   1773   1.1  jmcneill 		txd->tx_dmamap = NULL;
   1774   1.1  jmcneill 		error = bus_dmamap_create(sc->sc_dmat, ALC_TSO_MAXSIZE,
   1775   1.1  jmcneill 		    ALC_MAXTXSEGS, ALC_TSO_MAXSEGSIZE, 0, BUS_DMA_NOWAIT,
   1776   1.1  jmcneill 		    &txd->tx_dmamap);
   1777   1.1  jmcneill 		if (error) {
   1778   1.1  jmcneill 			printf("%s: could not create Tx dmamap.\n",
   1779   1.1  jmcneill 			    device_xname(sc->sc_dev));
   1780   1.1  jmcneill 			return error;
   1781   1.1  jmcneill 		}
   1782   1.1  jmcneill 	}
   1783   1.1  jmcneill 
   1784   1.1  jmcneill 	/* Create DMA maps for Rx buffers. */
   1785   1.1  jmcneill 	error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES, 0,
   1786   1.1  jmcneill 	    BUS_DMA_NOWAIT, &sc->alc_cdata.alc_rx_sparemap);
   1787   1.1  jmcneill 	if (error) {
   1788   1.1  jmcneill 		printf("%s: could not create spare Rx dmamap.\n",
   1789   1.1  jmcneill 		    device_xname(sc->sc_dev));
   1790   1.1  jmcneill 		return error;
   1791   1.1  jmcneill 	}
   1792   1.1  jmcneill 
   1793   1.1  jmcneill 	for (i = 0; i < ALC_RX_RING_CNT; i++) {
   1794   1.1  jmcneill 		rxd = &sc->alc_cdata.alc_rxdesc[i];
   1795   1.1  jmcneill 		rxd->rx_m = NULL;
   1796   1.1  jmcneill 		rxd->rx_dmamap = NULL;
   1797   1.1  jmcneill 		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
   1798   1.1  jmcneill 		    MCLBYTES, 0, BUS_DMA_NOWAIT, &rxd->rx_dmamap);
   1799   1.1  jmcneill 		if (error) {
   1800   1.1  jmcneill 			printf("%s: could not create Rx dmamap.\n",
   1801   1.1  jmcneill 			    device_xname(sc->sc_dev));
   1802   1.1  jmcneill 			return error;
   1803   1.1  jmcneill 		}
   1804   1.1  jmcneill 	}
   1805   1.1  jmcneill 
   1806   1.1  jmcneill 	return (0);
   1807   1.1  jmcneill }
   1808   1.1  jmcneill 
   1809   1.1  jmcneill static void
   1810   1.1  jmcneill alc_dma_free(struct alc_softc *sc)
   1811   1.1  jmcneill {
   1812   1.1  jmcneill 	struct alc_txdesc *txd;
   1813   1.1  jmcneill 	struct alc_rxdesc *rxd;
   1814   1.1  jmcneill 	int i;
   1815   1.1  jmcneill 
   1816   1.1  jmcneill 	/* Tx buffers */
   1817   1.1  jmcneill 	for (i = 0; i < ALC_TX_RING_CNT; i++) {
   1818   1.1  jmcneill 		txd = &sc->alc_cdata.alc_txdesc[i];
   1819   1.1  jmcneill 		if (txd->tx_dmamap != NULL) {
   1820   1.1  jmcneill 			bus_dmamap_destroy(sc->sc_dmat, txd->tx_dmamap);
   1821   1.1  jmcneill 			txd->tx_dmamap = NULL;
   1822   1.1  jmcneill 		}
   1823   1.1  jmcneill 	}
   1824   1.1  jmcneill 	/* Rx buffers */
   1825   1.1  jmcneill 	for (i = 0; i < ALC_RX_RING_CNT; i++) {
   1826   1.1  jmcneill 		rxd = &sc->alc_cdata.alc_rxdesc[i];
   1827   1.1  jmcneill 		if (rxd->rx_dmamap != NULL) {
   1828   1.1  jmcneill 			bus_dmamap_destroy(sc->sc_dmat, rxd->rx_dmamap);
   1829   1.1  jmcneill 			rxd->rx_dmamap = NULL;
   1830   1.1  jmcneill 		}
   1831   1.1  jmcneill 	}
   1832   1.1  jmcneill 	if (sc->alc_cdata.alc_rx_sparemap != NULL) {
   1833   1.1  jmcneill 		bus_dmamap_destroy(sc->sc_dmat, sc->alc_cdata.alc_rx_sparemap);
   1834   1.1  jmcneill 		sc->alc_cdata.alc_rx_sparemap = NULL;
   1835   1.1  jmcneill 	}
   1836   1.1  jmcneill 
   1837   1.1  jmcneill 	/* Tx ring. */
   1838   1.1  jmcneill 	if (sc->alc_cdata.alc_tx_ring_map != NULL)
   1839   1.1  jmcneill 		bus_dmamap_unload(sc->sc_dmat, sc->alc_cdata.alc_tx_ring_map);
   1840   1.1  jmcneill 	if (sc->alc_cdata.alc_tx_ring_map != NULL &&
   1841   1.1  jmcneill 	    sc->alc_rdata.alc_tx_ring != NULL)
   1842   1.1  jmcneill 		bus_dmamem_free(sc->sc_dmat,
   1843   1.1  jmcneill 		    &sc->alc_rdata.alc_tx_ring_seg, 1);
   1844   1.1  jmcneill 	sc->alc_rdata.alc_tx_ring = NULL;
   1845   1.1  jmcneill 	sc->alc_cdata.alc_tx_ring_map = NULL;
   1846   1.1  jmcneill 
   1847   1.1  jmcneill 	/* Rx ring. */
   1848   1.8  christos 	if (sc->alc_cdata.alc_rx_ring_map != NULL)
   1849   1.1  jmcneill 		bus_dmamap_unload(sc->sc_dmat, sc->alc_cdata.alc_rx_ring_map);
   1850   1.1  jmcneill 	if (sc->alc_cdata.alc_rx_ring_map != NULL &&
   1851   1.1  jmcneill 	    sc->alc_rdata.alc_rx_ring != NULL)
   1852   1.8  christos 		bus_dmamem_free(sc->sc_dmat,
   1853   1.1  jmcneill 		    &sc->alc_rdata.alc_rx_ring_seg, 1);
   1854   1.1  jmcneill 	sc->alc_rdata.alc_rx_ring = NULL;
   1855   1.1  jmcneill 	sc->alc_cdata.alc_rx_ring_map = NULL;
   1856   1.1  jmcneill 
   1857   1.1  jmcneill 	/* Rx return ring. */
   1858   1.1  jmcneill 	if (sc->alc_cdata.alc_rr_ring_map != NULL)
   1859   1.1  jmcneill 		bus_dmamap_unload(sc->sc_dmat, sc->alc_cdata.alc_rr_ring_map);
   1860   1.1  jmcneill 	if (sc->alc_cdata.alc_rr_ring_map != NULL &&
   1861   1.1  jmcneill 	    sc->alc_rdata.alc_rr_ring != NULL)
   1862   1.8  christos 		bus_dmamem_free(sc->sc_dmat,
   1863   1.1  jmcneill 		    &sc->alc_rdata.alc_rr_ring_seg, 1);
   1864   1.1  jmcneill 	sc->alc_rdata.alc_rr_ring = NULL;
   1865   1.1  jmcneill 	sc->alc_cdata.alc_rr_ring_map = NULL;
   1866   1.1  jmcneill 
   1867   1.1  jmcneill 	/* CMB block */
   1868   1.1  jmcneill 	if (sc->alc_cdata.alc_cmb_map != NULL)
   1869   1.1  jmcneill 		bus_dmamap_unload(sc->sc_dmat, sc->alc_cdata.alc_cmb_map);
   1870   1.1  jmcneill 	if (sc->alc_cdata.alc_cmb_map != NULL &&
   1871   1.1  jmcneill 	    sc->alc_rdata.alc_cmb != NULL)
   1872   1.1  jmcneill 		bus_dmamem_free(sc->sc_dmat,
   1873   1.1  jmcneill 		    &sc->alc_rdata.alc_cmb_seg, 1);
   1874   1.1  jmcneill 	sc->alc_rdata.alc_cmb = NULL;
   1875   1.1  jmcneill 	sc->alc_cdata.alc_cmb_map = NULL;
   1876   1.1  jmcneill 
   1877   1.1  jmcneill 	/* SMB block */
   1878   1.1  jmcneill 	if (sc->alc_cdata.alc_smb_map != NULL)
   1879   1.1  jmcneill 		bus_dmamap_unload(sc->sc_dmat, sc->alc_cdata.alc_smb_map);
   1880   1.1  jmcneill 	if (sc->alc_cdata.alc_smb_map != NULL &&
   1881   1.1  jmcneill 	    sc->alc_rdata.alc_smb != NULL)
   1882   1.8  christos 		bus_dmamem_free(sc->sc_dmat,
   1883   1.1  jmcneill 		    &sc->alc_rdata.alc_smb_seg, 1);
   1884   1.1  jmcneill 	sc->alc_rdata.alc_smb = NULL;
   1885   1.1  jmcneill 	sc->alc_cdata.alc_smb_map = NULL;
   1886   1.1  jmcneill }
   1887   1.1  jmcneill 
   1888   1.1  jmcneill static int
   1889   1.1  jmcneill alc_encap(struct alc_softc *sc, struct mbuf **m_head)
   1890   1.1  jmcneill {
   1891   1.1  jmcneill 	struct alc_txdesc *txd, *txd_last;
   1892   1.1  jmcneill 	struct tx_desc *desc;
   1893   1.1  jmcneill 	struct mbuf *m;
   1894   1.1  jmcneill 	bus_dmamap_t map;
   1895   1.1  jmcneill 	uint32_t cflags, poff, vtag;
   1896   1.1  jmcneill 	int error, idx, nsegs, prod;
   1897   1.1  jmcneill 
   1898   1.1  jmcneill 	m = *m_head;
   1899   1.1  jmcneill 	cflags = vtag = 0;
   1900   1.1  jmcneill 	poff = 0;
   1901   1.1  jmcneill 
   1902   1.1  jmcneill 	prod = sc->alc_cdata.alc_tx_prod;
   1903   1.1  jmcneill 	txd = &sc->alc_cdata.alc_txdesc[prod];
   1904   1.1  jmcneill 	txd_last = txd;
   1905   1.1  jmcneill 	map = txd->tx_dmamap;
   1906   1.1  jmcneill 
   1907   1.1  jmcneill 	error = bus_dmamap_load_mbuf(sc->sc_dmat, map, *m_head, BUS_DMA_NOWAIT);
   1908   1.1  jmcneill 
   1909   1.1  jmcneill 	if (error == EFBIG) {
   1910   1.1  jmcneill 		error = 0;
   1911   1.1  jmcneill 
   1912   1.1  jmcneill 		*m_head = m_pullup(*m_head, MHLEN);
   1913   1.1  jmcneill 		if (*m_head == NULL) {
   1914   1.1  jmcneill 			printf("%s: can't defrag TX mbuf\n",
   1915   1.1  jmcneill 			    device_xname(sc->sc_dev));
   1916   1.1  jmcneill 			return ENOBUFS;
   1917   1.1  jmcneill 		}
   1918   1.1  jmcneill 
   1919   1.1  jmcneill 		error = bus_dmamap_load_mbuf(sc->sc_dmat, map, *m_head,
   1920   1.1  jmcneill 		    BUS_DMA_NOWAIT);
   1921   1.1  jmcneill 
   1922   1.1  jmcneill 		if (error != 0) {
   1923   1.1  jmcneill 			printf("%s: could not load defragged TX mbuf\n",
   1924   1.1  jmcneill 			    device_xname(sc->sc_dev));
   1925   1.1  jmcneill 			m_freem(*m_head);
   1926   1.1  jmcneill 			*m_head = NULL;
   1927   1.1  jmcneill 			return error;
   1928   1.1  jmcneill 		}
   1929   1.1  jmcneill 	} else if (error) {
   1930   1.1  jmcneill 		printf("%s: could not load TX mbuf\n", device_xname(sc->sc_dev));
   1931   1.1  jmcneill 		return (error);
   1932   1.1  jmcneill 	}
   1933   1.1  jmcneill 
   1934   1.1  jmcneill 	nsegs = map->dm_nsegs;
   1935   1.1  jmcneill 
   1936   1.1  jmcneill 	if (nsegs == 0) {
   1937   1.1  jmcneill 		m_freem(*m_head);
   1938   1.1  jmcneill 		*m_head = NULL;
   1939   1.1  jmcneill 		return (EIO);
   1940   1.1  jmcneill 	}
   1941   1.1  jmcneill 
   1942   1.1  jmcneill 	/* Check descriptor overrun. */
   1943   1.1  jmcneill 	if (sc->alc_cdata.alc_tx_cnt + nsegs >= ALC_TX_RING_CNT - 3) {
   1944   1.1  jmcneill 		bus_dmamap_unload(sc->sc_dmat, map);
   1945   1.1  jmcneill 		return (ENOBUFS);
   1946   1.1  jmcneill 	}
   1947   1.1  jmcneill 	bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
   1948   1.1  jmcneill 	    BUS_DMASYNC_PREWRITE);
   1949   1.1  jmcneill 
   1950   1.1  jmcneill 	m = *m_head;
   1951   1.1  jmcneill 	desc = NULL;
   1952   1.1  jmcneill 	idx = 0;
   1953   1.1  jmcneill #if NVLAN > 0
   1954   1.1  jmcneill 	/* Configure VLAN hardware tag insertion. */
   1955  1.25  knakahar 	if (vlan_has_tag(m)) {
   1956  1.25  knakahar 		vtag = htons(vlan_get_tag(m));
   1957   1.1  jmcneill 		vtag = (vtag << TD_VLAN_SHIFT) & TD_VLAN_MASK;
   1958   1.1  jmcneill 		cflags |= TD_INS_VLAN_TAG;
   1959   1.1  jmcneill 	}
   1960   1.1  jmcneill #endif
   1961   1.1  jmcneill 	/* Configure Tx checksum offload. */
   1962   1.1  jmcneill 	if ((m->m_pkthdr.csum_flags & ALC_CSUM_FEATURES) != 0) {
   1963   1.1  jmcneill 		cflags |= TD_CUSTOM_CSUM;
   1964   1.1  jmcneill 		/* Set checksum start offset. */
   1965   1.1  jmcneill 		cflags |= ((poff >> 1) << TD_PLOAD_OFFSET_SHIFT) &
   1966   1.1  jmcneill 		    TD_PLOAD_OFFSET_MASK;
   1967   1.8  christos 	}
   1968   1.1  jmcneill 	for (; idx < nsegs; idx++) {
   1969   1.1  jmcneill 		desc = &sc->alc_rdata.alc_tx_ring[prod];
   1970   1.1  jmcneill 		desc->len =
   1971   1.1  jmcneill 		    htole32(TX_BYTES(map->dm_segs[idx].ds_len) | vtag);
   1972   1.1  jmcneill 		desc->flags = htole32(cflags);
   1973   1.1  jmcneill 		desc->addr = htole64(map->dm_segs[idx].ds_addr);
   1974   1.1  jmcneill 		sc->alc_cdata.alc_tx_cnt++;
   1975   1.1  jmcneill 		ALC_DESC_INC(prod, ALC_TX_RING_CNT);
   1976   1.1  jmcneill 	}
   1977   1.1  jmcneill 	/* Update producer index. */
   1978   1.1  jmcneill 	sc->alc_cdata.alc_tx_prod = prod;
   1979   1.1  jmcneill 
   1980   1.1  jmcneill 	/* Finally set EOP on the last descriptor. */
   1981   1.1  jmcneill 	prod = (prod + ALC_TX_RING_CNT - 1) % ALC_TX_RING_CNT;
   1982   1.1  jmcneill 	desc = &sc->alc_rdata.alc_tx_ring[prod];
   1983   1.1  jmcneill 	desc->flags |= htole32(TD_EOP);
   1984   1.1  jmcneill 
   1985   1.1  jmcneill 	/* Swap dmamap of the first and the last. */
   1986   1.1  jmcneill 	txd = &sc->alc_cdata.alc_txdesc[prod];
   1987   1.1  jmcneill 	map = txd_last->tx_dmamap;
   1988   1.1  jmcneill 	txd_last->tx_dmamap = txd->tx_dmamap;
   1989   1.1  jmcneill 	txd->tx_dmamap = map;
   1990   1.1  jmcneill 	txd->tx_m = m;
   1991   1.1  jmcneill 
   1992   1.1  jmcneill 	return (0);
   1993   1.1  jmcneill }
   1994   1.1  jmcneill 
   1995   1.1  jmcneill static void
   1996   1.1  jmcneill alc_start(struct ifnet *ifp)
   1997   1.1  jmcneill {
   1998   1.1  jmcneill 	struct alc_softc *sc = ifp->if_softc;
   1999   1.1  jmcneill 	struct mbuf *m_head;
   2000   1.1  jmcneill 	int enq;
   2001   1.1  jmcneill 
   2002   1.1  jmcneill 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
   2003   1.1  jmcneill 		return;
   2004  1.15      leot 	if ((sc->alc_flags & ALC_FLAG_LINK) == 0)
   2005  1.15      leot 		return;
   2006  1.15      leot 	if (IFQ_IS_EMPTY(&ifp->if_snd))
   2007  1.15      leot 		return;
   2008   1.1  jmcneill 
   2009   1.1  jmcneill 	/* Reclaim transmitted frames. */
   2010   1.1  jmcneill 	if (sc->alc_cdata.alc_tx_cnt >= ALC_TX_DESC_HIWAT)
   2011   1.1  jmcneill 		alc_txeof(sc);
   2012   1.1  jmcneill 
   2013   1.1  jmcneill 	enq = 0;
   2014   1.1  jmcneill 	for (;;) {
   2015   1.1  jmcneill 		IFQ_DEQUEUE(&ifp->if_snd, m_head);
   2016   1.1  jmcneill 		if (m_head == NULL)
   2017   1.1  jmcneill 			break;
   2018   1.1  jmcneill 
   2019   1.1  jmcneill 		/*
   2020   1.1  jmcneill 		 * Pack the data into the transmit ring. If we
   2021   1.1  jmcneill 		 * don't have room, set the OACTIVE flag and wait
   2022   1.1  jmcneill 		 * for the NIC to drain the ring.
   2023   1.1  jmcneill 		 */
   2024   1.1  jmcneill 		if (alc_encap(sc, &m_head)) {
   2025   1.1  jmcneill 			if (m_head == NULL)
   2026   1.1  jmcneill 				break;
   2027   1.1  jmcneill 			ifp->if_flags |= IFF_OACTIVE;
   2028   1.1  jmcneill 			break;
   2029   1.1  jmcneill 		}
   2030   1.1  jmcneill 		enq = 1;
   2031  1.35   msaitoh 
   2032   1.1  jmcneill 		/*
   2033   1.1  jmcneill 		 * If there's a BPF listener, bounce a copy of this frame
   2034   1.1  jmcneill 		 * to him.
   2035   1.1  jmcneill 		 */
   2036  1.28   msaitoh 		bpf_mtap(ifp, m_head, BPF_D_OUT);
   2037   1.1  jmcneill 	}
   2038   1.1  jmcneill 
   2039   1.1  jmcneill 	if (enq) {
   2040   1.1  jmcneill 		/* Sync descriptors. */
   2041   1.1  jmcneill 		bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_tx_ring_map, 0,
   2042   1.8  christos 		    sc->alc_cdata.alc_tx_ring_map->dm_mapsize,
   2043   1.1  jmcneill 		    BUS_DMASYNC_PREWRITE);
   2044   1.1  jmcneill 		/* Kick. Assume we're using normal Tx priority queue. */
   2045   1.1  jmcneill 		CSR_WRITE_4(sc, ALC_MBOX_TD_PROD_IDX,
   2046   1.1  jmcneill 		    (sc->alc_cdata.alc_tx_prod <<
   2047   1.1  jmcneill 		    MBOX_TD_PROD_LO_IDX_SHIFT) &
   2048   1.1  jmcneill 		    MBOX_TD_PROD_LO_IDX_MASK);
   2049   1.1  jmcneill 		/* Set a timeout in case the chip goes out to lunch. */
   2050   1.1  jmcneill 		ifp->if_timer = ALC_TX_TIMEOUT;
   2051   1.1  jmcneill 	}
   2052   1.1  jmcneill }
   2053   1.1  jmcneill 
   2054   1.1  jmcneill static void
   2055   1.1  jmcneill alc_watchdog(struct ifnet *ifp)
   2056   1.1  jmcneill {
   2057   1.1  jmcneill 	struct alc_softc *sc = ifp->if_softc;
   2058   1.1  jmcneill 
   2059   1.1  jmcneill 	if ((sc->alc_flags & ALC_FLAG_LINK) == 0) {
   2060   1.1  jmcneill 		printf("%s: watchdog timeout (missed link)\n",
   2061   1.1  jmcneill 		    device_xname(sc->sc_dev));
   2062  1.47   thorpej 		if_statinc(ifp, if_oerrors);
   2063   1.7       mrg 		alc_init_backend(ifp, false);
   2064   1.1  jmcneill 		return;
   2065   1.1  jmcneill 	}
   2066   1.1  jmcneill 
   2067   1.1  jmcneill 	printf("%s: watchdog timeout\n", device_xname(sc->sc_dev));
   2068  1.47   thorpej 	if_statinc(ifp, if_oerrors);
   2069   1.7       mrg 	alc_init_backend(ifp, false);
   2070  1.15      leot 	alc_start(ifp);
   2071   1.1  jmcneill }
   2072   1.1  jmcneill 
   2073   1.1  jmcneill static int
   2074   1.1  jmcneill alc_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   2075   1.1  jmcneill {
   2076   1.1  jmcneill 	struct alc_softc *sc = ifp->if_softc;
   2077   1.1  jmcneill 	int s, error = 0;
   2078   1.1  jmcneill 
   2079   1.1  jmcneill 	s = splnet();
   2080   1.1  jmcneill 
   2081  1.16  christos 	switch (cmd) {
   2082  1.16  christos 	case SIOCSIFADDR:
   2083  1.34   msaitoh 		error = ether_ioctl(ifp, cmd, data);
   2084  1.16  christos 		ifp->if_flags |= IFF_UP;
   2085  1.16  christos 		if (!(ifp->if_flags & IFF_RUNNING))
   2086  1.16  christos 			alc_init(ifp);
   2087  1.16  christos 		break;
   2088  1.34   msaitoh 
   2089  1.16  christos 	case SIOCSIFFLAGS:
   2090  1.34   msaitoh 		error = ether_ioctl(ifp, cmd, data);
   2091  1.16  christos 		if (ifp->if_flags & IFF_UP) {
   2092  1.16  christos 			if (ifp->if_flags & IFF_RUNNING)
   2093  1.16  christos 				error = ENETRESET;
   2094  1.16  christos 			else
   2095  1.16  christos 				alc_init(ifp);
   2096  1.16  christos 		} else {
   2097  1.16  christos 			if (ifp->if_flags & IFF_RUNNING)
   2098  1.16  christos 				alc_stop(ifp, 0);
   2099  1.16  christos 		}
   2100  1.16  christos 		break;
   2101  1.34   msaitoh 
   2102  1.16  christos 	default:
   2103  1.16  christos 		error = ether_ioctl(ifp, cmd, data);
   2104  1.16  christos 		break;
   2105  1.16  christos 	}
   2106  1.35   msaitoh 
   2107   1.1  jmcneill 	if (error == ENETRESET) {
   2108   1.1  jmcneill 		if (ifp->if_flags & IFF_RUNNING)
   2109   1.1  jmcneill 			alc_iff(sc);
   2110   1.1  jmcneill 		error = 0;
   2111   1.1  jmcneill 	}
   2112   1.1  jmcneill 
   2113   1.1  jmcneill 	splx(s);
   2114   1.1  jmcneill 	return (error);
   2115   1.1  jmcneill }
   2116   1.1  jmcneill 
   2117   1.1  jmcneill static void
   2118   1.1  jmcneill alc_mac_config(struct alc_softc *sc)
   2119   1.1  jmcneill {
   2120   1.1  jmcneill 	struct mii_data *mii;
   2121   1.1  jmcneill 	uint32_t reg;
   2122   1.1  jmcneill 
   2123   1.1  jmcneill 	mii = &sc->sc_miibus;
   2124   1.1  jmcneill 	reg = CSR_READ_4(sc, ALC_MAC_CFG);
   2125   1.1  jmcneill 	reg &= ~(MAC_CFG_FULL_DUPLEX | MAC_CFG_TX_FC | MAC_CFG_RX_FC |
   2126   1.1  jmcneill 	    MAC_CFG_SPEED_MASK);
   2127   1.2  jmcneill 	if (sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8151 ||
   2128   1.2  jmcneill 	    sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8151_V2 ||
   2129   1.2  jmcneill 	    sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8152_B2)
   2130   1.2  jmcneill 		reg |= MAC_CFG_HASH_ALG_CRC32 | MAC_CFG_SPEED_MODE_SW;
   2131   1.1  jmcneill 	/* Reprogram MAC with resolved speed/duplex. */
   2132   1.1  jmcneill 	switch (IFM_SUBTYPE(mii->mii_media_active)) {
   2133   1.1  jmcneill 	case IFM_10_T:
   2134   1.1  jmcneill 	case IFM_100_TX:
   2135   1.1  jmcneill 		reg |= MAC_CFG_SPEED_10_100;
   2136   1.1  jmcneill 		break;
   2137   1.1  jmcneill 	case IFM_1000_T:
   2138   1.1  jmcneill 		reg |= MAC_CFG_SPEED_1000;
   2139   1.1  jmcneill 		break;
   2140   1.1  jmcneill 	}
   2141   1.1  jmcneill 	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
   2142   1.1  jmcneill 		reg |= MAC_CFG_FULL_DUPLEX;
   2143   1.1  jmcneill 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
   2144   1.1  jmcneill 			reg |= MAC_CFG_TX_FC;
   2145   1.1  jmcneill 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
   2146   1.1  jmcneill 			reg |= MAC_CFG_RX_FC;
   2147   1.1  jmcneill 	}
   2148   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
   2149   1.1  jmcneill }
   2150   1.1  jmcneill 
   2151   1.1  jmcneill static void
   2152   1.1  jmcneill alc_stats_clear(struct alc_softc *sc)
   2153   1.1  jmcneill {
   2154   1.1  jmcneill 	struct smb sb, *smb;
   2155   1.1  jmcneill 	uint32_t *reg;
   2156   1.1  jmcneill 	int i;
   2157   1.1  jmcneill 
   2158   1.1  jmcneill 	if ((sc->alc_flags & ALC_FLAG_SMB_BUG) == 0) {
   2159   1.1  jmcneill 		bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_smb_map, 0,
   2160   1.8  christos 		    sc->alc_cdata.alc_smb_map->dm_mapsize,
   2161  1.15      leot 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   2162   1.1  jmcneill 		smb = sc->alc_rdata.alc_smb;
   2163   1.1  jmcneill 		/* Update done, clear. */
   2164   1.1  jmcneill 		smb->updated = 0;
   2165   1.1  jmcneill 		bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_smb_map, 0,
   2166   1.8  christos 		    sc->alc_cdata.alc_smb_map->dm_mapsize,
   2167  1.15      leot 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   2168   1.1  jmcneill 	} else {
   2169   1.1  jmcneill 		for (reg = &sb.rx_frames, i = 0; reg <= &sb.rx_pkts_filtered;
   2170   1.1  jmcneill 		    reg++) {
   2171   1.1  jmcneill 			CSR_READ_4(sc, ALC_RX_MIB_BASE + i);
   2172   1.1  jmcneill 			i += sizeof(uint32_t);
   2173   1.1  jmcneill 		}
   2174   1.1  jmcneill 		/* Read Tx statistics. */
   2175   1.1  jmcneill 		for (reg = &sb.tx_frames, i = 0; reg <= &sb.tx_mcast_bytes;
   2176   1.1  jmcneill 		    reg++) {
   2177   1.1  jmcneill 			CSR_READ_4(sc, ALC_TX_MIB_BASE + i);
   2178   1.1  jmcneill 			i += sizeof(uint32_t);
   2179   1.1  jmcneill 		}
   2180   1.1  jmcneill 	}
   2181   1.1  jmcneill }
   2182   1.1  jmcneill 
   2183   1.1  jmcneill static void
   2184   1.1  jmcneill alc_stats_update(struct alc_softc *sc)
   2185   1.1  jmcneill {
   2186   1.1  jmcneill 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   2187   1.1  jmcneill 	struct alc_hw_stats *stat;
   2188   1.1  jmcneill 	struct smb sb, *smb;
   2189   1.1  jmcneill 	uint32_t *reg;
   2190   1.1  jmcneill 	int i;
   2191   1.1  jmcneill 
   2192   1.1  jmcneill 	stat = &sc->alc_stats;
   2193   1.1  jmcneill 	if ((sc->alc_flags & ALC_FLAG_SMB_BUG) == 0) {
   2194   1.1  jmcneill 		bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_smb_map, 0,
   2195   1.1  jmcneill 		    sc->alc_cdata.alc_smb_map->dm_mapsize,
   2196  1.15      leot 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   2197   1.1  jmcneill 		smb = sc->alc_rdata.alc_smb;
   2198   1.1  jmcneill 		if (smb->updated == 0)
   2199   1.1  jmcneill 			return;
   2200   1.1  jmcneill 	} else {
   2201   1.1  jmcneill 		smb = &sb;
   2202   1.1  jmcneill 		/* Read Rx statistics. */
   2203   1.1  jmcneill 		for (reg = &sb.rx_frames, i = 0; reg <= &sb.rx_pkts_filtered;
   2204   1.1  jmcneill 		    reg++) {
   2205   1.1  jmcneill 			*reg = CSR_READ_4(sc, ALC_RX_MIB_BASE + i);
   2206   1.1  jmcneill 			i += sizeof(uint32_t);
   2207   1.1  jmcneill 		}
   2208   1.1  jmcneill 		/* Read Tx statistics. */
   2209   1.1  jmcneill 		for (reg = &sb.tx_frames, i = 0; reg <= &sb.tx_mcast_bytes;
   2210   1.1  jmcneill 		    reg++) {
   2211   1.1  jmcneill 			*reg = CSR_READ_4(sc, ALC_TX_MIB_BASE + i);
   2212   1.1  jmcneill 			i += sizeof(uint32_t);
   2213   1.1  jmcneill 		}
   2214   1.1  jmcneill 	}
   2215   1.1  jmcneill 
   2216   1.1  jmcneill 	/* Rx stats. */
   2217   1.1  jmcneill 	stat->rx_frames += smb->rx_frames;
   2218   1.1  jmcneill 	stat->rx_bcast_frames += smb->rx_bcast_frames;
   2219   1.1  jmcneill 	stat->rx_mcast_frames += smb->rx_mcast_frames;
   2220   1.1  jmcneill 	stat->rx_pause_frames += smb->rx_pause_frames;
   2221   1.1  jmcneill 	stat->rx_control_frames += smb->rx_control_frames;
   2222   1.1  jmcneill 	stat->rx_crcerrs += smb->rx_crcerrs;
   2223   1.1  jmcneill 	stat->rx_lenerrs += smb->rx_lenerrs;
   2224   1.1  jmcneill 	stat->rx_bytes += smb->rx_bytes;
   2225   1.1  jmcneill 	stat->rx_runts += smb->rx_runts;
   2226   1.1  jmcneill 	stat->rx_fragments += smb->rx_fragments;
   2227   1.1  jmcneill 	stat->rx_pkts_64 += smb->rx_pkts_64;
   2228   1.1  jmcneill 	stat->rx_pkts_65_127 += smb->rx_pkts_65_127;
   2229   1.1  jmcneill 	stat->rx_pkts_128_255 += smb->rx_pkts_128_255;
   2230   1.1  jmcneill 	stat->rx_pkts_256_511 += smb->rx_pkts_256_511;
   2231   1.1  jmcneill 	stat->rx_pkts_512_1023 += smb->rx_pkts_512_1023;
   2232   1.1  jmcneill 	stat->rx_pkts_1024_1518 += smb->rx_pkts_1024_1518;
   2233   1.1  jmcneill 	stat->rx_pkts_1519_max += smb->rx_pkts_1519_max;
   2234   1.1  jmcneill 	stat->rx_pkts_truncated += smb->rx_pkts_truncated;
   2235   1.1  jmcneill 	stat->rx_fifo_oflows += smb->rx_fifo_oflows;
   2236   1.1  jmcneill 	stat->rx_rrs_errs += smb->rx_rrs_errs;
   2237   1.1  jmcneill 	stat->rx_alignerrs += smb->rx_alignerrs;
   2238   1.1  jmcneill 	stat->rx_bcast_bytes += smb->rx_bcast_bytes;
   2239   1.1  jmcneill 	stat->rx_mcast_bytes += smb->rx_mcast_bytes;
   2240   1.1  jmcneill 	stat->rx_pkts_filtered += smb->rx_pkts_filtered;
   2241   1.1  jmcneill 
   2242   1.1  jmcneill 	/* Tx stats. */
   2243   1.1  jmcneill 	stat->tx_frames += smb->tx_frames;
   2244   1.1  jmcneill 	stat->tx_bcast_frames += smb->tx_bcast_frames;
   2245   1.1  jmcneill 	stat->tx_mcast_frames += smb->tx_mcast_frames;
   2246   1.1  jmcneill 	stat->tx_pause_frames += smb->tx_pause_frames;
   2247   1.1  jmcneill 	stat->tx_excess_defer += smb->tx_excess_defer;
   2248   1.1  jmcneill 	stat->tx_control_frames += smb->tx_control_frames;
   2249   1.1  jmcneill 	stat->tx_deferred += smb->tx_deferred;
   2250   1.1  jmcneill 	stat->tx_bytes += smb->tx_bytes;
   2251   1.1  jmcneill 	stat->tx_pkts_64 += smb->tx_pkts_64;
   2252   1.1  jmcneill 	stat->tx_pkts_65_127 += smb->tx_pkts_65_127;
   2253   1.1  jmcneill 	stat->tx_pkts_128_255 += smb->tx_pkts_128_255;
   2254   1.1  jmcneill 	stat->tx_pkts_256_511 += smb->tx_pkts_256_511;
   2255   1.1  jmcneill 	stat->tx_pkts_512_1023 += smb->tx_pkts_512_1023;
   2256   1.1  jmcneill 	stat->tx_pkts_1024_1518 += smb->tx_pkts_1024_1518;
   2257   1.1  jmcneill 	stat->tx_pkts_1519_max += smb->tx_pkts_1519_max;
   2258   1.1  jmcneill 	stat->tx_single_colls += smb->tx_single_colls;
   2259   1.1  jmcneill 	stat->tx_multi_colls += smb->tx_multi_colls;
   2260   1.1  jmcneill 	stat->tx_late_colls += smb->tx_late_colls;
   2261   1.1  jmcneill 	stat->tx_excess_colls += smb->tx_excess_colls;
   2262   1.1  jmcneill 	stat->tx_underrun += smb->tx_underrun;
   2263   1.1  jmcneill 	stat->tx_desc_underrun += smb->tx_desc_underrun;
   2264   1.1  jmcneill 	stat->tx_lenerrs += smb->tx_lenerrs;
   2265   1.1  jmcneill 	stat->tx_pkts_truncated += smb->tx_pkts_truncated;
   2266   1.1  jmcneill 	stat->tx_bcast_bytes += smb->tx_bcast_bytes;
   2267   1.1  jmcneill 	stat->tx_mcast_bytes += smb->tx_mcast_bytes;
   2268   1.1  jmcneill 
   2269   1.1  jmcneill 	/* Update counters in ifnet. */
   2270  1.47   thorpej 	net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
   2271   1.1  jmcneill 
   2272  1.47   thorpej 	if_statadd_ref(nsr, if_opackets, smb->tx_frames);
   2273  1.47   thorpej 
   2274  1.47   thorpej 	if_statadd_ref(nsr, if_collisions,
   2275  1.47   thorpej 	    smb->tx_single_colls +
   2276   1.1  jmcneill 	    smb->tx_multi_colls * 2 + smb->tx_late_colls +
   2277  1.47   thorpej 	    smb->tx_excess_colls * HDPX_CFG_RETRY_DEFAULT);
   2278   1.1  jmcneill 
   2279  1.47   thorpej 	if_statadd_ref(nsr, if_oerrors,
   2280  1.47   thorpej 	    smb->tx_late_colls + smb->tx_excess_colls +
   2281  1.47   thorpej 	    smb->tx_underrun + smb->tx_pkts_truncated);
   2282   1.1  jmcneill 
   2283  1.47   thorpej 	if_statadd_ref(nsr, if_ierrors,
   2284  1.47   thorpej 	    smb->rx_crcerrs + smb->rx_lenerrs +
   2285   1.1  jmcneill 	    smb->rx_runts + smb->rx_pkts_truncated +
   2286   1.1  jmcneill 	    smb->rx_fifo_oflows + smb->rx_rrs_errs +
   2287  1.47   thorpej 	    smb->rx_alignerrs);
   2288  1.47   thorpej 
   2289  1.47   thorpej 	IF_STAT_PUTREF(ifp);
   2290   1.1  jmcneill 
   2291   1.1  jmcneill 	if ((sc->alc_flags & ALC_FLAG_SMB_BUG) == 0) {
   2292   1.1  jmcneill 		/* Update done, clear. */
   2293   1.1  jmcneill 		smb->updated = 0;
   2294   1.1  jmcneill 		bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_smb_map, 0,
   2295  1.15      leot 		sc->alc_cdata.alc_smb_map->dm_mapsize,
   2296  1.15      leot 		BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   2297   1.1  jmcneill 	}
   2298   1.1  jmcneill }
   2299   1.1  jmcneill 
   2300   1.1  jmcneill static int
   2301   1.1  jmcneill alc_intr(void *arg)
   2302   1.1  jmcneill {
   2303   1.1  jmcneill 	struct alc_softc *sc = arg;
   2304   1.1  jmcneill 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   2305   1.1  jmcneill 	uint32_t status;
   2306   1.1  jmcneill 
   2307   1.1  jmcneill 	status = CSR_READ_4(sc, ALC_INTR_STATUS);
   2308   1.1  jmcneill 	if ((status & ALC_INTRS) == 0)
   2309   1.1  jmcneill 		return (0);
   2310   1.1  jmcneill 
   2311   1.1  jmcneill 	/* Acknowledge and disable interrupts. */
   2312   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_INTR_STATUS, status | INTR_DIS_INT);
   2313   1.1  jmcneill 
   2314   1.1  jmcneill 	if (ifp->if_flags & IFF_RUNNING) {
   2315   1.1  jmcneill 		if (status & INTR_RX_PKT) {
   2316   1.1  jmcneill 			int error;
   2317   1.1  jmcneill 
   2318   1.1  jmcneill 			error = alc_rxintr(sc);
   2319   1.1  jmcneill 			if (error) {
   2320   1.7       mrg 				alc_init_backend(ifp, false);
   2321   1.1  jmcneill 				return (0);
   2322   1.1  jmcneill 			}
   2323   1.1  jmcneill 		}
   2324   1.1  jmcneill 
   2325   1.1  jmcneill 		if (status & (INTR_DMA_RD_TO_RST | INTR_DMA_WR_TO_RST |
   2326   1.1  jmcneill 		    INTR_TXQ_TO_RST)) {
   2327   1.1  jmcneill 			if (status & INTR_DMA_RD_TO_RST)
   2328   1.1  jmcneill 				printf("%s: DMA read error! -- resetting\n",
   2329   1.1  jmcneill 				    device_xname(sc->sc_dev));
   2330   1.1  jmcneill 			if (status & INTR_DMA_WR_TO_RST)
   2331   1.1  jmcneill 				printf("%s: DMA write error! -- resetting\n",
   2332   1.1  jmcneill 				    device_xname(sc->sc_dev));
   2333   1.1  jmcneill 			if (status & INTR_TXQ_TO_RST)
   2334   1.1  jmcneill 				printf("%s: TxQ reset! -- resetting\n",
   2335   1.1  jmcneill 				    device_xname(sc->sc_dev));
   2336   1.7       mrg 			alc_init_backend(ifp, false);
   2337   1.1  jmcneill 			return (0);
   2338   1.1  jmcneill 		}
   2339   1.1  jmcneill 
   2340   1.1  jmcneill 		alc_txeof(sc);
   2341  1.22     ozaki 		if_schedule_deferred_start(ifp);
   2342   1.1  jmcneill 	}
   2343   1.1  jmcneill 
   2344   1.1  jmcneill 	/* Re-enable interrupts. */
   2345   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_INTR_STATUS, 0x7FFFFFFF);
   2346   1.1  jmcneill 	return (1);
   2347   1.1  jmcneill }
   2348   1.1  jmcneill 
   2349   1.1  jmcneill static void
   2350   1.1  jmcneill alc_txeof(struct alc_softc *sc)
   2351   1.1  jmcneill {
   2352   1.1  jmcneill 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   2353   1.1  jmcneill 	struct alc_txdesc *txd;
   2354   1.1  jmcneill 	uint32_t cons, prod;
   2355   1.1  jmcneill 	int prog;
   2356   1.1  jmcneill 
   2357   1.1  jmcneill 	if (sc->alc_cdata.alc_tx_cnt == 0)
   2358   1.1  jmcneill 		return;
   2359   1.1  jmcneill 	bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_tx_ring_map, 0,
   2360   1.1  jmcneill 	    sc->alc_cdata.alc_tx_ring_map->dm_mapsize,
   2361   1.1  jmcneill 	    BUS_DMASYNC_POSTREAD);
   2362   1.1  jmcneill 	if ((sc->alc_flags & ALC_FLAG_CMB_BUG) == 0) {
   2363   1.1  jmcneill 		bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_cmb_map, 0,
   2364   1.8  christos 		    sc->alc_cdata.alc_cmb_map->dm_mapsize,
   2365   1.1  jmcneill 		    BUS_DMASYNC_POSTREAD);
   2366   1.1  jmcneill 		prod = sc->alc_rdata.alc_cmb->cons;
   2367   1.1  jmcneill 	} else
   2368   1.1  jmcneill 		prod = CSR_READ_4(sc, ALC_MBOX_TD_CONS_IDX);
   2369   1.1  jmcneill 	/* Assume we're using normal Tx priority queue. */
   2370   1.1  jmcneill 	prod = (prod & MBOX_TD_CONS_LO_IDX_MASK) >>
   2371   1.1  jmcneill 	    MBOX_TD_CONS_LO_IDX_SHIFT;
   2372   1.1  jmcneill 	cons = sc->alc_cdata.alc_tx_cons;
   2373   1.1  jmcneill 	/*
   2374   1.1  jmcneill 	 * Go through our Tx list and free mbufs for those
   2375   1.1  jmcneill 	 * frames which have been transmitted.
   2376   1.1  jmcneill 	 */
   2377   1.1  jmcneill 	for (prog = 0; cons != prod; prog++,
   2378   1.1  jmcneill 	    ALC_DESC_INC(cons, ALC_TX_RING_CNT)) {
   2379   1.1  jmcneill 		if (sc->alc_cdata.alc_tx_cnt <= 0)
   2380   1.1  jmcneill 			break;
   2381   1.1  jmcneill 		prog++;
   2382   1.1  jmcneill 		ifp->if_flags &= ~IFF_OACTIVE;
   2383   1.1  jmcneill 		sc->alc_cdata.alc_tx_cnt--;
   2384   1.1  jmcneill 		txd = &sc->alc_cdata.alc_txdesc[cons];
   2385   1.1  jmcneill 		if (txd->tx_m != NULL) {
   2386   1.1  jmcneill 			/* Reclaim transmitted mbufs. */
   2387  1.15      leot 			bus_dmamap_sync(sc->sc_dmat, txd->tx_dmamap, 0,
   2388  1.15      leot 			    txd->tx_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   2389   1.1  jmcneill 			bus_dmamap_unload(sc->sc_dmat, txd->tx_dmamap);
   2390   1.1  jmcneill 			m_freem(txd->tx_m);
   2391   1.1  jmcneill 			txd->tx_m = NULL;
   2392   1.1  jmcneill 		}
   2393   1.1  jmcneill 	}
   2394   1.1  jmcneill 
   2395   1.1  jmcneill 	if ((sc->alc_flags & ALC_FLAG_CMB_BUG) == 0)
   2396   1.1  jmcneill 	    bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_cmb_map, 0,
   2397  1.36   msaitoh 		sc->alc_cdata.alc_cmb_map->dm_mapsize, BUS_DMASYNC_PREREAD);
   2398   1.1  jmcneill 	sc->alc_cdata.alc_tx_cons = cons;
   2399   1.1  jmcneill 	/*
   2400   1.1  jmcneill 	 * Unarm watchdog timer only when there is no pending
   2401   1.1  jmcneill 	 * frames in Tx queue.
   2402   1.1  jmcneill 	 */
   2403   1.1  jmcneill 	if (sc->alc_cdata.alc_tx_cnt == 0)
   2404   1.1  jmcneill 		ifp->if_timer = 0;
   2405   1.1  jmcneill }
   2406   1.1  jmcneill 
   2407   1.1  jmcneill static int
   2408   1.7       mrg alc_newbuf(struct alc_softc *sc, struct alc_rxdesc *rxd, bool init)
   2409   1.1  jmcneill {
   2410   1.1  jmcneill 	struct mbuf *m;
   2411   1.1  jmcneill 	bus_dmamap_t map;
   2412   1.1  jmcneill 	int error;
   2413   1.1  jmcneill 
   2414   1.1  jmcneill 	MGETHDR(m, init ? M_WAITOK : M_DONTWAIT, MT_DATA);
   2415   1.1  jmcneill 	if (m == NULL)
   2416   1.1  jmcneill 		return (ENOBUFS);
   2417   1.1  jmcneill 	MCLGET(m, init ? M_WAITOK : M_DONTWAIT);
   2418   1.1  jmcneill 	if (!(m->m_flags & M_EXT)) {
   2419   1.1  jmcneill 		m_freem(m);
   2420   1.1  jmcneill 		return (ENOBUFS);
   2421   1.1  jmcneill 	}
   2422   1.1  jmcneill 
   2423   1.1  jmcneill 	m->m_len = m->m_pkthdr.len = RX_BUF_SIZE_MAX;
   2424   1.1  jmcneill 
   2425   1.1  jmcneill 	error = bus_dmamap_load_mbuf(sc->sc_dmat,
   2426   1.1  jmcneill 	    sc->alc_cdata.alc_rx_sparemap, m, BUS_DMA_NOWAIT);
   2427   1.1  jmcneill 
   2428   1.1  jmcneill 	if (error != 0) {
   2429   1.1  jmcneill 		m_freem(m);
   2430   1.1  jmcneill 
   2431   1.1  jmcneill 		if (init)
   2432   1.1  jmcneill 			printf("%s: can't load RX mbuf\n", device_xname(sc->sc_dev));
   2433   1.1  jmcneill 
   2434   1.1  jmcneill 		return (error);
   2435   1.1  jmcneill 	}
   2436   1.1  jmcneill 
   2437   1.1  jmcneill 	if (rxd->rx_m != NULL) {
   2438   1.1  jmcneill 		bus_dmamap_sync(sc->sc_dmat, rxd->rx_dmamap, 0,
   2439   1.1  jmcneill 		    rxd->rx_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
   2440   1.1  jmcneill 		bus_dmamap_unload(sc->sc_dmat, rxd->rx_dmamap);
   2441   1.1  jmcneill 	}
   2442   1.1  jmcneill 	map = rxd->rx_dmamap;
   2443   1.1  jmcneill 	rxd->rx_dmamap = sc->alc_cdata.alc_rx_sparemap;
   2444   1.1  jmcneill 	sc->alc_cdata.alc_rx_sparemap = map;
   2445  1.15      leot 	bus_dmamap_sync(sc->sc_dmat, rxd->rx_dmamap, 0, rxd->rx_dmamap->dm_mapsize,
   2446  1.15      leot 	    BUS_DMASYNC_PREREAD);
   2447   1.1  jmcneill 	rxd->rx_m = m;
   2448   1.1  jmcneill 	rxd->rx_desc->addr = htole64(rxd->rx_dmamap->dm_segs[0].ds_addr);
   2449   1.1  jmcneill 	return (0);
   2450   1.1  jmcneill }
   2451   1.1  jmcneill 
   2452   1.1  jmcneill static int
   2453   1.1  jmcneill alc_rxintr(struct alc_softc *sc)
   2454   1.1  jmcneill {
   2455   1.1  jmcneill 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   2456   1.1  jmcneill 	struct rx_rdesc *rrd;
   2457   1.1  jmcneill 	uint32_t nsegs, status;
   2458   1.1  jmcneill 	int rr_cons, prog;
   2459   1.1  jmcneill 
   2460   1.1  jmcneill 	bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_rr_ring_map, 0,
   2461  1.15      leot 	    sc->alc_cdata.alc_rr_ring_map->dm_mapsize,
   2462  1.15      leot 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   2463   1.1  jmcneill 	bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_rx_ring_map, 0,
   2464  1.15      leot 	    sc->alc_cdata.alc_rx_ring_map->dm_mapsize,
   2465  1.15      leot 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   2466   1.1  jmcneill 	rr_cons = sc->alc_cdata.alc_rr_cons;
   2467   1.1  jmcneill 	for (prog = 0; (ifp->if_flags & IFF_RUNNING) != 0;) {
   2468   1.1  jmcneill 		rrd = &sc->alc_rdata.alc_rr_ring[rr_cons];
   2469   1.1  jmcneill 		status = le32toh(rrd->status);
   2470   1.1  jmcneill 		if ((status & RRD_VALID) == 0)
   2471   1.1  jmcneill 			break;
   2472   1.1  jmcneill 		nsegs = RRD_RD_CNT(le32toh(rrd->rdinfo));
   2473   1.1  jmcneill 		if (nsegs == 0) {
   2474   1.1  jmcneill 			/* This should not happen! */
   2475   1.1  jmcneill 			if (alcdebug)
   2476   1.1  jmcneill 				printf("%s: unexpected segment count -- "
   2477   1.1  jmcneill 				    "resetting\n", device_xname(sc->sc_dev));
   2478   1.1  jmcneill 			return (EIO);
   2479   1.1  jmcneill 		}
   2480   1.1  jmcneill 		alc_rxeof(sc, rrd);
   2481   1.1  jmcneill 		/* Clear Rx return status. */
   2482   1.1  jmcneill 		rrd->status = 0;
   2483   1.1  jmcneill 		ALC_DESC_INC(rr_cons, ALC_RR_RING_CNT);
   2484   1.1  jmcneill 		sc->alc_cdata.alc_rx_cons += nsegs;
   2485   1.1  jmcneill 		sc->alc_cdata.alc_rx_cons %= ALC_RR_RING_CNT;
   2486   1.1  jmcneill 		prog += nsegs;
   2487   1.1  jmcneill 	}
   2488   1.1  jmcneill 
   2489   1.1  jmcneill 	if (prog > 0) {
   2490   1.1  jmcneill 		/* Update the consumer index. */
   2491   1.1  jmcneill 		sc->alc_cdata.alc_rr_cons = rr_cons;
   2492   1.1  jmcneill 		/* Sync Rx return descriptors. */
   2493   1.1  jmcneill 		bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_rr_ring_map, 0,
   2494   1.1  jmcneill 		    sc->alc_cdata.alc_rr_ring_map->dm_mapsize,
   2495  1.15      leot 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   2496   1.1  jmcneill 		/*
   2497   1.1  jmcneill 		 * Sync updated Rx descriptors such that controller see
   2498   1.1  jmcneill 		 * modified buffer addresses.
   2499   1.1  jmcneill 		 */
   2500   1.1  jmcneill 		bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_rx_ring_map, 0,
   2501   1.1  jmcneill 		    sc->alc_cdata.alc_rx_ring_map->dm_mapsize,
   2502   1.1  jmcneill 		    BUS_DMASYNC_PREWRITE);
   2503   1.1  jmcneill 		/*
   2504   1.1  jmcneill 		 * Let controller know availability of new Rx buffers.
   2505   1.1  jmcneill 		 * Since alc(4) use RXQ_CFG_RD_BURST_DEFAULT descriptors
   2506   1.1  jmcneill 		 * it may be possible to update ALC_MBOX_RD0_PROD_IDX
   2507   1.1  jmcneill 		 * only when Rx buffer pre-fetching is required. In
   2508   1.1  jmcneill 		 * addition we already set ALC_RX_RD_FREE_THRESH to
   2509   1.1  jmcneill 		 * RX_RD_FREE_THRESH_LO_DEFAULT descriptors. However
   2510   1.1  jmcneill 		 * it still seems that pre-fetching needs more
   2511   1.1  jmcneill 		 * experimentation.
   2512   1.1  jmcneill 		 */
   2513   1.1  jmcneill 		CSR_WRITE_4(sc, ALC_MBOX_RD0_PROD_IDX,
   2514   1.1  jmcneill 		    sc->alc_cdata.alc_rx_cons);
   2515   1.1  jmcneill 	}
   2516   1.1  jmcneill 
   2517   1.1  jmcneill 	return (0);
   2518   1.1  jmcneill }
   2519   1.1  jmcneill 
   2520   1.1  jmcneill /* Receive a frame. */
   2521   1.1  jmcneill static void
   2522   1.1  jmcneill alc_rxeof(struct alc_softc *sc, struct rx_rdesc *rrd)
   2523   1.1  jmcneill {
   2524   1.1  jmcneill 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   2525   1.1  jmcneill 	struct alc_rxdesc *rxd;
   2526   1.1  jmcneill 	struct mbuf *mp, *m;
   2527   1.1  jmcneill 	uint32_t rdinfo, status;
   2528   1.1  jmcneill 	int count, nsegs, rx_cons;
   2529   1.1  jmcneill 
   2530   1.1  jmcneill 	status = le32toh(rrd->status);
   2531   1.1  jmcneill 	rdinfo = le32toh(rrd->rdinfo);
   2532   1.1  jmcneill 	rx_cons = RRD_RD_IDX(rdinfo);
   2533   1.1  jmcneill 	nsegs = RRD_RD_CNT(rdinfo);
   2534   1.1  jmcneill 
   2535   1.1  jmcneill 	sc->alc_cdata.alc_rxlen = RRD_BYTES(status);
   2536   1.1  jmcneill 	if (status & (RRD_ERR_SUM | RRD_ERR_LENGTH)) {
   2537   1.1  jmcneill 		/*
   2538   1.1  jmcneill 		 * We want to pass the following frames to upper
   2539   1.1  jmcneill 		 * layer regardless of error status of Rx return
   2540   1.1  jmcneill 		 * ring.
   2541   1.1  jmcneill 		 *
   2542   1.1  jmcneill 		 *  o IP/TCP/UDP checksum is bad.
   2543   1.1  jmcneill 		 *  o frame length and protocol specific length
   2544   1.1  jmcneill 		 *     does not match.
   2545   1.1  jmcneill 		 *
   2546   1.1  jmcneill 		 *  Force network stack compute checksum for
   2547   1.1  jmcneill 		 *  errored frames.
   2548   1.1  jmcneill 		 */
   2549   1.1  jmcneill 		status |= RRD_TCP_UDPCSUM_NOK | RRD_IPCSUM_NOK;
   2550   1.2  jmcneill 		if ((status & (RRD_ERR_CRC | RRD_ERR_ALIGN |
   2551   1.2  jmcneill 		    RRD_ERR_TRUNC | RRD_ERR_RUNT)) != 0)
   2552   1.1  jmcneill 			return;
   2553   1.1  jmcneill 	}
   2554   1.1  jmcneill 
   2555   1.1  jmcneill 	for (count = 0; count < nsegs; count++,
   2556   1.1  jmcneill 	    ALC_DESC_INC(rx_cons, ALC_RX_RING_CNT)) {
   2557   1.1  jmcneill 		rxd = &sc->alc_cdata.alc_rxdesc[rx_cons];
   2558   1.1  jmcneill 		mp = rxd->rx_m;
   2559   1.1  jmcneill 		/* Add a new receive buffer to the ring. */
   2560   1.7       mrg 		if (alc_newbuf(sc, rxd, false) != 0) {
   2561  1.47   thorpej 			if_statinc(ifp, if_iqdrops);
   2562   1.1  jmcneill 			/* Reuse Rx buffers. */
   2563   1.1  jmcneill 			if (sc->alc_cdata.alc_rxhead != NULL)
   2564   1.1  jmcneill 				m_freem(sc->alc_cdata.alc_rxhead);
   2565   1.1  jmcneill 			break;
   2566   1.1  jmcneill 		}
   2567   1.1  jmcneill 
   2568   1.1  jmcneill 		/*
   2569   1.1  jmcneill 		 * Assume we've received a full sized frame.
   2570   1.1  jmcneill 		 * Actual size is fixed when we encounter the end of
   2571   1.1  jmcneill 		 * multi-segmented frame.
   2572   1.1  jmcneill 		 */
   2573   1.1  jmcneill 		mp->m_len = sc->alc_buf_size;
   2574   1.1  jmcneill 
   2575   1.1  jmcneill 		/* Chain received mbufs. */
   2576   1.1  jmcneill 		if (sc->alc_cdata.alc_rxhead == NULL) {
   2577   1.1  jmcneill 			sc->alc_cdata.alc_rxhead = mp;
   2578   1.1  jmcneill 			sc->alc_cdata.alc_rxtail = mp;
   2579   1.1  jmcneill 		} else {
   2580  1.26      maxv 			m_remove_pkthdr(mp);
   2581   1.1  jmcneill 			sc->alc_cdata.alc_rxprev_tail =
   2582   1.1  jmcneill 			    sc->alc_cdata.alc_rxtail;
   2583   1.1  jmcneill 			sc->alc_cdata.alc_rxtail->m_next = mp;
   2584   1.1  jmcneill 			sc->alc_cdata.alc_rxtail = mp;
   2585   1.1  jmcneill 		}
   2586   1.1  jmcneill 
   2587   1.1  jmcneill 		if (count == nsegs - 1) {
   2588   1.1  jmcneill 			/* Last desc. for this frame. */
   2589   1.1  jmcneill 			m = sc->alc_cdata.alc_rxhead;
   2590  1.26      maxv 			KASSERT(m->m_flags & M_PKTHDR);
   2591   1.1  jmcneill 			/*
   2592   1.1  jmcneill 			 * It seems that L1C/L2C controller has no way
   2593   1.1  jmcneill 			 * to tell hardware to strip CRC bytes.
   2594   1.1  jmcneill 			 */
   2595   1.1  jmcneill 			m->m_pkthdr.len =
   2596   1.1  jmcneill 			    sc->alc_cdata.alc_rxlen - ETHER_CRC_LEN;
   2597   1.1  jmcneill 			if (nsegs > 1) {
   2598   1.1  jmcneill 				/* Set last mbuf size. */
   2599   1.1  jmcneill 				mp->m_len = sc->alc_cdata.alc_rxlen -
   2600   1.1  jmcneill 				    (nsegs - 1) * sc->alc_buf_size;
   2601   1.1  jmcneill 				/* Remove the CRC bytes in chained mbufs. */
   2602   1.1  jmcneill 				if (mp->m_len <= ETHER_CRC_LEN) {
   2603   1.1  jmcneill 					sc->alc_cdata.alc_rxtail =
   2604   1.1  jmcneill 					    sc->alc_cdata.alc_rxprev_tail;
   2605   1.1  jmcneill 					sc->alc_cdata.alc_rxtail->m_len -=
   2606   1.1  jmcneill 					    (ETHER_CRC_LEN - mp->m_len);
   2607   1.1  jmcneill 					sc->alc_cdata.alc_rxtail->m_next = NULL;
   2608   1.1  jmcneill 					m_freem(mp);
   2609   1.1  jmcneill 				} else {
   2610   1.1  jmcneill 					mp->m_len -= ETHER_CRC_LEN;
   2611   1.1  jmcneill 				}
   2612   1.1  jmcneill 			} else
   2613   1.1  jmcneill 				m->m_len = m->m_pkthdr.len;
   2614  1.21     ozaki 			m_set_rcvif(m, ifp);
   2615   1.1  jmcneill #if NVLAN > 0
   2616   1.1  jmcneill 			/*
   2617   1.1  jmcneill 			 * Due to hardware bugs, Rx checksum offloading
   2618   1.1  jmcneill 			 * was intentionally disabled.
   2619   1.1  jmcneill 			 */
   2620   1.1  jmcneill 			if (status & RRD_VLAN_TAG) {
   2621  1.35   msaitoh 				uint32_t vtag = RRD_VLAN(le32toh(rrd->vtag));
   2622  1.25  knakahar 				vlan_set_tag(m, ntohs(vtag));
   2623   1.1  jmcneill 			}
   2624   1.1  jmcneill #endif
   2625   1.1  jmcneill 
   2626   1.1  jmcneill 			/* Pass it on. */
   2627  1.19     ozaki 			if_percpuq_enqueue(ifp->if_percpuq, m);
   2628   1.1  jmcneill 		}
   2629   1.1  jmcneill 	}
   2630   1.1  jmcneill 	/* Reset mbuf chains. */
   2631   1.1  jmcneill 	ALC_RXCHAIN_RESET(sc);
   2632   1.1  jmcneill }
   2633   1.1  jmcneill 
   2634   1.1  jmcneill static void
   2635   1.1  jmcneill alc_tick(void *xsc)
   2636   1.1  jmcneill {
   2637   1.1  jmcneill 	struct alc_softc *sc = xsc;
   2638   1.1  jmcneill 	struct mii_data *mii = &sc->sc_miibus;
   2639   1.1  jmcneill 	int s;
   2640   1.1  jmcneill 
   2641   1.1  jmcneill 	s = splnet();
   2642   1.1  jmcneill 	mii_tick(mii);
   2643   1.1  jmcneill 	alc_stats_update(sc);
   2644   1.1  jmcneill 	splx(s);
   2645   1.1  jmcneill 
   2646   1.1  jmcneill 	callout_schedule(&sc->sc_tick_ch, hz);
   2647   1.1  jmcneill }
   2648   1.1  jmcneill 
   2649   1.1  jmcneill static void
   2650  1.12  christos alc_osc_reset(struct alc_softc *sc)
   2651  1.12  christos {
   2652  1.12  christos 	uint32_t reg;
   2653  1.12  christos 
   2654  1.12  christos 	reg = CSR_READ_4(sc, ALC_MISC3);
   2655  1.12  christos 	reg &= ~MISC3_25M_BY_SW;
   2656  1.12  christos 	reg |= MISC3_25M_NOTO_INTNL;
   2657  1.12  christos 	CSR_WRITE_4(sc, ALC_MISC3, reg);
   2658  1.12  christos 
   2659  1.12  christos 	reg = CSR_READ_4(sc, ALC_MISC);
   2660  1.12  christos 	if (AR816X_REV(sc->alc_rev) >= AR816X_REV_B0) {
   2661  1.12  christos 		/*
   2662  1.12  christos 		 * Restore over-current protection default value.
   2663  1.12  christos 		 * This value could be reset by MAC reset.
   2664  1.12  christos 		 */
   2665  1.12  christos 		reg &= ~MISC_PSW_OCP_MASK;
   2666  1.12  christos 		reg |= (MISC_PSW_OCP_DEFAULT << MISC_PSW_OCP_SHIFT);
   2667  1.12  christos 		reg &= ~MISC_INTNLOSC_OPEN;
   2668  1.12  christos 		CSR_WRITE_4(sc, ALC_MISC, reg);
   2669  1.12  christos 		CSR_WRITE_4(sc, ALC_MISC, reg | MISC_INTNLOSC_OPEN);
   2670  1.12  christos 		reg = CSR_READ_4(sc, ALC_MISC2);
   2671  1.12  christos 		reg &= ~MISC2_CALB_START;
   2672  1.12  christos 		CSR_WRITE_4(sc, ALC_MISC2, reg);
   2673  1.12  christos 		CSR_WRITE_4(sc, ALC_MISC2, reg | MISC2_CALB_START);
   2674  1.12  christos 
   2675  1.12  christos 	} else {
   2676  1.12  christos 		reg &= ~MISC_INTNLOSC_OPEN;
   2677  1.12  christos 		/* Disable isolate for revision A devices. */
   2678  1.12  christos 		if (AR816X_REV(sc->alc_rev) <= AR816X_REV_A1)
   2679  1.12  christos 			reg &= ~MISC_ISO_ENB;
   2680  1.12  christos 		CSR_WRITE_4(sc, ALC_MISC, reg | MISC_INTNLOSC_OPEN);
   2681  1.12  christos 		CSR_WRITE_4(sc, ALC_MISC, reg);
   2682  1.12  christos 	}
   2683  1.12  christos 
   2684  1.12  christos 	DELAY(20);
   2685  1.12  christos }
   2686  1.12  christos 
   2687  1.12  christos static void
   2688   1.1  jmcneill alc_reset(struct alc_softc *sc)
   2689   1.1  jmcneill {
   2690  1.12  christos 	uint32_t pmcfg, reg;
   2691   1.1  jmcneill 	int i;
   2692   1.1  jmcneill 
   2693  1.12  christos 	pmcfg = 0;
   2694  1.12  christos 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
   2695  1.12  christos 		/* Reset workaround. */
   2696  1.12  christos 		CSR_WRITE_4(sc, ALC_MBOX_RD0_PROD_IDX, 1);
   2697  1.12  christos 		if (AR816X_REV(sc->alc_rev) <= AR816X_REV_A1 &&
   2698  1.12  christos 		    (sc->alc_rev & 0x01) != 0) {
   2699  1.12  christos 			/* Disable L0s/L1s before reset. */
   2700  1.12  christos 			pmcfg = CSR_READ_4(sc, ALC_PM_CFG);
   2701  1.12  christos 			if ((pmcfg & (PM_CFG_ASPM_L0S_ENB | PM_CFG_ASPM_L1_ENB))
   2702  1.12  christos 			    != 0) {
   2703  1.12  christos 				pmcfg &= ~(PM_CFG_ASPM_L0S_ENB |
   2704  1.12  christos 				    PM_CFG_ASPM_L1_ENB);
   2705  1.12  christos 				CSR_WRITE_4(sc, ALC_PM_CFG, pmcfg);
   2706  1.12  christos 			}
   2707  1.12  christos 		}
   2708  1.12  christos 	}
   2709  1.12  christos 	reg = CSR_READ_4(sc, ALC_MASTER_CFG);
   2710   1.2  jmcneill 	reg |= MASTER_OOB_DIS_OFF | MASTER_RESET;
   2711   1.2  jmcneill 	CSR_WRITE_4(sc, ALC_MASTER_CFG, reg);
   2712  1.12  christos 
   2713  1.12  christos 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
   2714  1.12  christos 		for (i = ALC_RESET_TIMEOUT; i > 0; i--) {
   2715  1.12  christos 			DELAY(10);
   2716  1.12  christos 			if (CSR_READ_4(sc, ALC_MBOX_RD0_PROD_IDX) == 0)
   2717  1.12  christos 				break;
   2718  1.12  christos 		}
   2719  1.12  christos 		if (i == 0)
   2720  1.12  christos 			printf("%s: MAC reset timeout!\n", device_xname(sc->sc_dev));
   2721  1.12  christos 	}
   2722   1.1  jmcneill 	for (i = ALC_RESET_TIMEOUT; i > 0; i--) {
   2723   1.1  jmcneill 		DELAY(10);
   2724   1.1  jmcneill 		if ((CSR_READ_4(sc, ALC_MASTER_CFG) & MASTER_RESET) == 0)
   2725   1.1  jmcneill 			break;
   2726   1.1  jmcneill 	}
   2727   1.1  jmcneill 	if (i == 0)
   2728   1.1  jmcneill 		printf("%s: master reset timeout!\n", device_xname(sc->sc_dev));
   2729   1.1  jmcneill 
   2730   1.1  jmcneill 	for (i = ALC_RESET_TIMEOUT; i > 0; i--) {
   2731  1.12  christos 		reg = CSR_READ_4(sc, ALC_IDLE_STATUS);
   2732  1.12  christos 		if ((reg & (IDLE_STATUS_RXMAC | IDLE_STATUS_TXMAC |
   2733  1.12  christos 		    IDLE_STATUS_RXQ | IDLE_STATUS_TXQ)) == 0)
   2734   1.1  jmcneill 			break;
   2735   1.1  jmcneill 		DELAY(10);
   2736   1.1  jmcneill 	}
   2737  1.12  christos 	if (i == 0)
   2738  1.12  christos 		printf("%s: reset timeout(0x%08x)!\n",
   2739  1.12  christos 		    device_xname(sc->sc_dev), reg);
   2740   1.1  jmcneill 
   2741  1.12  christos 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
   2742  1.12  christos 		if (AR816X_REV(sc->alc_rev) <= AR816X_REV_A1 &&
   2743  1.12  christos 		    (sc->alc_rev & 0x01) != 0) {
   2744  1.12  christos 			reg = CSR_READ_4(sc, ALC_MASTER_CFG);
   2745  1.12  christos 			reg |= MASTER_CLK_SEL_DIS;
   2746  1.12  christos 			CSR_WRITE_4(sc, ALC_MASTER_CFG, reg);
   2747  1.12  christos 			/* Restore L0s/L1s config. */
   2748  1.12  christos 			if ((pmcfg & (PM_CFG_ASPM_L0S_ENB | PM_CFG_ASPM_L1_ENB))
   2749  1.12  christos 			    != 0)
   2750  1.12  christos 				CSR_WRITE_4(sc, ALC_PM_CFG, pmcfg);
   2751  1.12  christos 		}
   2752  1.12  christos 
   2753  1.12  christos 		alc_osc_reset(sc);
   2754  1.12  christos 		reg = CSR_READ_4(sc, ALC_MISC3);
   2755  1.12  christos 		reg &= ~MISC3_25M_BY_SW;
   2756  1.12  christos 		reg |= MISC3_25M_NOTO_INTNL;
   2757  1.12  christos 		CSR_WRITE_4(sc, ALC_MISC3, reg);
   2758  1.12  christos 		reg = CSR_READ_4(sc, ALC_MISC);
   2759  1.12  christos 		reg &= ~MISC_INTNLOSC_OPEN;
   2760  1.12  christos 		if (AR816X_REV(sc->alc_rev) <= AR816X_REV_A1)
   2761  1.12  christos 			reg &= ~MISC_ISO_ENB;
   2762  1.12  christos 		CSR_WRITE_4(sc, ALC_MISC, reg);
   2763  1.12  christos 		DELAY(20);
   2764  1.12  christos 	}
   2765  1.12  christos 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0 ||
   2766  1.12  christos 	    sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8152_B ||
   2767  1.12  christos 	    sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8151_V2)
   2768  1.12  christos 		CSR_WRITE_4(sc, ALC_SERDES_LOCK,
   2769  1.12  christos 		    CSR_READ_4(sc, ALC_SERDES_LOCK) | SERDES_MAC_CLK_SLOWDOWN |
   2770  1.12  christos 		    SERDES_PHY_CLK_SLOWDOWN);
   2771   1.1  jmcneill }
   2772   1.1  jmcneill 
   2773   1.1  jmcneill static int
   2774   1.1  jmcneill alc_init(struct ifnet *ifp)
   2775   1.1  jmcneill {
   2776   1.8  christos 
   2777   1.7       mrg 	return alc_init_backend(ifp, true);
   2778   1.7       mrg }
   2779   1.7       mrg 
   2780   1.7       mrg static int
   2781   1.7       mrg alc_init_backend(struct ifnet *ifp, bool init)
   2782   1.7       mrg {
   2783   1.1  jmcneill 	struct alc_softc *sc = ifp->if_softc;
   2784   1.1  jmcneill 	struct mii_data *mii;
   2785   1.1  jmcneill 	uint8_t eaddr[ETHER_ADDR_LEN];
   2786   1.1  jmcneill 	bus_addr_t paddr;
   2787   1.1  jmcneill 	uint32_t reg, rxf_hi, rxf_lo;
   2788   1.1  jmcneill 	int error;
   2789   1.1  jmcneill 
   2790   1.1  jmcneill 	/*
   2791   1.1  jmcneill 	 * Cancel any pending I/O.
   2792   1.1  jmcneill 	 */
   2793   1.1  jmcneill 	alc_stop(ifp, 0);
   2794   1.1  jmcneill 	/*
   2795   1.1  jmcneill 	 * Reset the chip to a known state.
   2796   1.1  jmcneill 	 */
   2797   1.1  jmcneill 	alc_reset(sc);
   2798   1.1  jmcneill 
   2799   1.1  jmcneill 	/* Initialize Rx descriptors. */
   2800   1.7       mrg 	error = alc_init_rx_ring(sc, init);
   2801   1.1  jmcneill 	if (error != 0) {
   2802   1.1  jmcneill 		printf("%s: no memory for Rx buffers.\n", device_xname(sc->sc_dev));
   2803   1.1  jmcneill 		alc_stop(ifp, 0);
   2804   1.1  jmcneill 		return (error);
   2805   1.1  jmcneill 	}
   2806   1.1  jmcneill 	alc_init_rr_ring(sc);
   2807   1.1  jmcneill 	alc_init_tx_ring(sc);
   2808   1.1  jmcneill 	alc_init_cmb(sc);
   2809   1.1  jmcneill 	alc_init_smb(sc);
   2810   1.1  jmcneill 
   2811   1.2  jmcneill 	/* Enable all clocks. */
   2812  1.12  christos 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
   2813  1.12  christos 		CSR_WRITE_4(sc, ALC_CLK_GATING_CFG, CLK_GATING_DMAW_ENB |
   2814  1.12  christos 		    CLK_GATING_DMAR_ENB | CLK_GATING_TXQ_ENB |
   2815  1.12  christos 		    CLK_GATING_RXQ_ENB | CLK_GATING_TXMAC_ENB |
   2816  1.12  christos 		    CLK_GATING_RXMAC_ENB);
   2817  1.12  christos 		if (AR816X_REV(sc->alc_rev) >= AR816X_REV_B0)
   2818  1.12  christos 			CSR_WRITE_4(sc, ALC_IDLE_DECISN_TIMER,
   2819  1.12  christos 			    IDLE_DECISN_TIMER_DEFAULT_1MS);
   2820  1.12  christos 	} else
   2821  1.12  christos 		CSR_WRITE_4(sc, ALC_CLK_GATING_CFG, 0);
   2822   1.2  jmcneill 
   2823   1.1  jmcneill 	/* Reprogram the station address. */
   2824   1.1  jmcneill 	memcpy(eaddr, CLLADDR(ifp->if_sadl), sizeof(eaddr));
   2825  1.46   msaitoh 	CSR_WRITE_4(sc, ALC_PAR0, (uint32_t)eaddr[2] << 24
   2826  1.46   msaitoh 	    | eaddr[3] << 16 | eaddr[4] << 8 | eaddr[5]);
   2827   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_PAR1, eaddr[0] << 8 | eaddr[1]);
   2828   1.1  jmcneill 	/*
   2829   1.1  jmcneill 	 * Clear WOL status and disable all WOL feature as WOL
   2830   1.1  jmcneill 	 * would interfere Rx operation under normal environments.
   2831   1.1  jmcneill 	 */
   2832   1.1  jmcneill 	CSR_READ_4(sc, ALC_WOL_CFG);
   2833   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_WOL_CFG, 0);
   2834   1.1  jmcneill 	/* Set Tx descriptor base addresses. */
   2835   1.1  jmcneill 	paddr = sc->alc_rdata.alc_tx_ring_paddr;
   2836   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_TX_BASE_ADDR_HI, ALC_ADDR_HI(paddr));
   2837   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_TDL_HEAD_ADDR_LO, ALC_ADDR_LO(paddr));
   2838   1.1  jmcneill 	/* We don't use high priority ring. */
   2839   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_TDH_HEAD_ADDR_LO, 0);
   2840   1.1  jmcneill 	/* Set Tx descriptor counter. */
   2841   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_TD_RING_CNT,
   2842   1.1  jmcneill 	    (ALC_TX_RING_CNT << TD_RING_CNT_SHIFT) & TD_RING_CNT_MASK);
   2843   1.1  jmcneill 	/* Set Rx descriptor base addresses. */
   2844   1.1  jmcneill 	paddr = sc->alc_rdata.alc_rx_ring_paddr;
   2845   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_RX_BASE_ADDR_HI, ALC_ADDR_HI(paddr));
   2846   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_RD0_HEAD_ADDR_LO, ALC_ADDR_LO(paddr));
   2847  1.12  christos 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
   2848  1.12  christos 		/* We use one Rx ring. */
   2849  1.12  christos 		CSR_WRITE_4(sc, ALC_RD1_HEAD_ADDR_LO, 0);
   2850  1.12  christos 		CSR_WRITE_4(sc, ALC_RD2_HEAD_ADDR_LO, 0);
   2851  1.12  christos 		CSR_WRITE_4(sc, ALC_RD3_HEAD_ADDR_LO, 0);
   2852  1.12  christos 	}
   2853   1.1  jmcneill 	/* Set Rx descriptor counter. */
   2854   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_RD_RING_CNT,
   2855   1.1  jmcneill 	    (ALC_RX_RING_CNT << RD_RING_CNT_SHIFT) & RD_RING_CNT_MASK);
   2856   1.1  jmcneill 
   2857   1.1  jmcneill 	/*
   2858   1.1  jmcneill 	 * Let hardware split jumbo frames into alc_max_buf_sized chunks.
   2859   1.1  jmcneill 	 * if it do not fit the buffer size. Rx return descriptor holds
   2860   1.1  jmcneill 	 * a counter that indicates how many fragments were made by the
   2861   1.1  jmcneill 	 * hardware. The buffer size should be multiple of 8 bytes.
   2862   1.1  jmcneill 	 * Since hardware has limit on the size of buffer size, always
   2863   1.1  jmcneill 	 * use the maximum value.
   2864   1.1  jmcneill 	 * For strict-alignment architectures make sure to reduce buffer
   2865   1.1  jmcneill 	 * size by 8 bytes to make room for alignment fixup.
   2866   1.1  jmcneill 	 */
   2867   1.1  jmcneill 	sc->alc_buf_size = RX_BUF_SIZE_MAX;
   2868   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_RX_BUF_SIZE, sc->alc_buf_size);
   2869   1.1  jmcneill 
   2870   1.1  jmcneill 	paddr = sc->alc_rdata.alc_rr_ring_paddr;
   2871   1.1  jmcneill 	/* Set Rx return descriptor base addresses. */
   2872   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_RRD0_HEAD_ADDR_LO, ALC_ADDR_LO(paddr));
   2873  1.12  christos 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
   2874  1.12  christos 		/* We use one Rx return ring. */
   2875  1.12  christos 		CSR_WRITE_4(sc, ALC_RRD1_HEAD_ADDR_LO, 0);
   2876  1.12  christos 		CSR_WRITE_4(sc, ALC_RRD2_HEAD_ADDR_LO, 0);
   2877  1.12  christos 		CSR_WRITE_4(sc, ALC_RRD3_HEAD_ADDR_LO, 0);
   2878  1.41   msaitoh 	}
   2879   1.1  jmcneill 	/* Set Rx return descriptor counter. */
   2880   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_RRD_RING_CNT,
   2881   1.1  jmcneill 	    (ALC_RR_RING_CNT << RRD_RING_CNT_SHIFT) & RRD_RING_CNT_MASK);
   2882   1.1  jmcneill 	paddr = sc->alc_rdata.alc_cmb_paddr;
   2883   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_CMB_BASE_ADDR_LO, ALC_ADDR_LO(paddr));
   2884   1.1  jmcneill 	paddr = sc->alc_rdata.alc_smb_paddr;
   2885   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_SMB_BASE_ADDR_HI, ALC_ADDR_HI(paddr));
   2886   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_SMB_BASE_ADDR_LO, ALC_ADDR_LO(paddr));
   2887   1.1  jmcneill 
   2888   1.2  jmcneill 	if (sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8152_B) {
   2889   1.2  jmcneill 		/* Reconfigure SRAM - Vendor magic. */
   2890   1.2  jmcneill 		CSR_WRITE_4(sc, ALC_SRAM_RX_FIFO_LEN, 0x000002A0);
   2891   1.2  jmcneill 		CSR_WRITE_4(sc, ALC_SRAM_TX_FIFO_LEN, 0x00000100);
   2892   1.2  jmcneill 		CSR_WRITE_4(sc, ALC_SRAM_RX_FIFO_ADDR, 0x029F0000);
   2893   1.2  jmcneill 		CSR_WRITE_4(sc, ALC_SRAM_RD0_ADDR, 0x02BF02A0);
   2894   1.2  jmcneill 		CSR_WRITE_4(sc, ALC_SRAM_TX_FIFO_ADDR, 0x03BF02C0);
   2895   1.2  jmcneill 		CSR_WRITE_4(sc, ALC_SRAM_TD_ADDR, 0x03DF03C0);
   2896   1.2  jmcneill 		CSR_WRITE_4(sc, ALC_TXF_WATER_MARK, 0x00000000);
   2897   1.2  jmcneill 		CSR_WRITE_4(sc, ALC_RD_DMA_CFG, 0x00000000);
   2898   1.2  jmcneill 	}
   2899   1.2  jmcneill 
   2900   1.1  jmcneill 	/* Tell hardware that we're ready to load DMA blocks. */
   2901   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_DMA_BLOCK, DMA_BLOCK_LOAD);
   2902   1.1  jmcneill 
   2903   1.1  jmcneill 	/* Configure interrupt moderation timer. */
   2904   1.1  jmcneill 	sc->alc_int_rx_mod = ALC_IM_RX_TIMER_DEFAULT;
   2905   1.1  jmcneill 	sc->alc_int_tx_mod = ALC_IM_TX_TIMER_DEFAULT;
   2906   1.1  jmcneill 	reg = ALC_USECS(sc->alc_int_rx_mod) << IM_TIMER_RX_SHIFT;
   2907  1.12  christos 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0)
   2908  1.12  christos 		reg |= ALC_USECS(sc->alc_int_tx_mod) << IM_TIMER_TX_SHIFT;
   2909   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_IM_TIMER, reg);
   2910   1.1  jmcneill 	/*
   2911   1.1  jmcneill 	 * We don't want to automatic interrupt clear as task queue
   2912   1.1  jmcneill 	 * for the interrupt should know interrupt status.
   2913   1.1  jmcneill 	 */
   2914  1.12  christos 	reg = CSR_READ_4(sc, ALC_MASTER_CFG);
   2915  1.12  christos 	reg &= ~(MASTER_IM_RX_TIMER_ENB | MASTER_IM_TX_TIMER_ENB);
   2916  1.12  christos 	reg |= MASTER_SA_TIMER_ENB;
   2917   1.1  jmcneill 	if (ALC_USECS(sc->alc_int_rx_mod) != 0)
   2918   1.1  jmcneill 		reg |= MASTER_IM_RX_TIMER_ENB;
   2919  1.12  christos 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0 &&
   2920  1.12  christos 	    ALC_USECS(sc->alc_int_tx_mod) != 0)
   2921   1.1  jmcneill 		reg |= MASTER_IM_TX_TIMER_ENB;
   2922   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_MASTER_CFG, reg);
   2923   1.1  jmcneill 	/*
   2924   1.1  jmcneill 	 * Disable interrupt re-trigger timer. We don't want automatic
   2925   1.1  jmcneill 	 * re-triggering of un-ACKed interrupts.
   2926   1.1  jmcneill 	 */
   2927   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_INTR_RETRIG_TIMER, ALC_USECS(0));
   2928   1.1  jmcneill 	/* Configure CMB. */
   2929  1.12  christos 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
   2930  1.12  christos 		CSR_WRITE_4(sc, ALC_CMB_TD_THRESH, ALC_TX_RING_CNT / 3);
   2931  1.12  christos 		CSR_WRITE_4(sc, ALC_CMB_TX_TIMER,
   2932  1.12  christos 		    ALC_USECS(sc->alc_int_tx_mod));
   2933  1.12  christos 	} else {
   2934  1.12  christos 		if ((sc->alc_flags & ALC_FLAG_CMB_BUG) == 0) {
   2935  1.12  christos 			CSR_WRITE_4(sc, ALC_CMB_TD_THRESH, 4);
   2936  1.12  christos 			CSR_WRITE_4(sc, ALC_CMB_TX_TIMER, ALC_USECS(5000));
   2937  1.12  christos 		} else
   2938  1.12  christos 			CSR_WRITE_4(sc, ALC_CMB_TX_TIMER, ALC_USECS(0));
   2939  1.12  christos 	}
   2940   1.1  jmcneill 	/*
   2941   1.1  jmcneill 	 * Hardware can be configured to issue SMB interrupt based
   2942   1.1  jmcneill 	 * on programmed interval. Since there is a callout that is
   2943   1.1  jmcneill 	 * invoked for every hz in driver we use that instead of
   2944   1.1  jmcneill 	 * relying on periodic SMB interrupt.
   2945   1.1  jmcneill 	 */
   2946   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_SMB_STAT_TIMER, ALC_USECS(0));
   2947   1.1  jmcneill 	/* Clear MAC statistics. */
   2948   1.1  jmcneill 	alc_stats_clear(sc);
   2949   1.1  jmcneill 
   2950   1.1  jmcneill 	/*
   2951   1.1  jmcneill 	 * Always use maximum frame size that controller can support.
   2952   1.1  jmcneill 	 * Otherwise received frames that has larger frame length
   2953   1.1  jmcneill 	 * than alc(4) MTU would be silently dropped in hardware. This
   2954   1.1  jmcneill 	 * would make path-MTU discovery hard as sender wouldn't get
   2955   1.1  jmcneill 	 * any responses from receiver. alc(4) supports
   2956   1.1  jmcneill 	 * multi-fragmented frames on Rx path so it has no issue on
   2957   1.1  jmcneill 	 * assembling fragmented frames. Using maximum frame size also
   2958   1.1  jmcneill 	 * removes the need to reinitialize hardware when interface
   2959   1.1  jmcneill 	 * MTU configuration was changed.
   2960   1.1  jmcneill 	 *
   2961   1.1  jmcneill 	 * Be conservative in what you do, be liberal in what you
   2962   1.1  jmcneill 	 * accept from others - RFC 793.
   2963   1.1  jmcneill 	 */
   2964   1.2  jmcneill 	CSR_WRITE_4(sc, ALC_FRAME_SIZE, sc->alc_ident->max_framelen);
   2965   1.1  jmcneill 
   2966  1.12  christos 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
   2967  1.12  christos 		/* Disable header split(?) */
   2968  1.12  christos 		CSR_WRITE_4(sc, ALC_HDS_CFG, 0);
   2969  1.12  christos 
   2970  1.12  christos 		/* Configure IPG/IFG parameters. */
   2971  1.12  christos 		CSR_WRITE_4(sc, ALC_IPG_IFG_CFG,
   2972  1.12  christos 		    ((IPG_IFG_IPGT_DEFAULT << IPG_IFG_IPGT_SHIFT) &
   2973  1.12  christos 		    IPG_IFG_IPGT_MASK) |
   2974  1.12  christos 		    ((IPG_IFG_MIFG_DEFAULT << IPG_IFG_MIFG_SHIFT) &
   2975  1.12  christos 		    IPG_IFG_MIFG_MASK) |
   2976  1.12  christos 		    ((IPG_IFG_IPG1_DEFAULT << IPG_IFG_IPG1_SHIFT) &
   2977  1.12  christos 		    IPG_IFG_IPG1_MASK) |
   2978  1.12  christos 		    ((IPG_IFG_IPG2_DEFAULT << IPG_IFG_IPG2_SHIFT) &
   2979  1.12  christos 		    IPG_IFG_IPG2_MASK));
   2980  1.12  christos 		/* Set parameters for half-duplex media. */
   2981  1.12  christos 		CSR_WRITE_4(sc, ALC_HDPX_CFG,
   2982  1.12  christos 		    ((HDPX_CFG_LCOL_DEFAULT << HDPX_CFG_LCOL_SHIFT) &
   2983  1.12  christos 		    HDPX_CFG_LCOL_MASK) |
   2984  1.12  christos 		    ((HDPX_CFG_RETRY_DEFAULT << HDPX_CFG_RETRY_SHIFT) &
   2985  1.12  christos 		    HDPX_CFG_RETRY_MASK) | HDPX_CFG_EXC_DEF_EN |
   2986  1.12  christos 		    ((HDPX_CFG_ABEBT_DEFAULT << HDPX_CFG_ABEBT_SHIFT) &
   2987  1.12  christos 		    HDPX_CFG_ABEBT_MASK) |
   2988  1.12  christos 		    ((HDPX_CFG_JAMIPG_DEFAULT << HDPX_CFG_JAMIPG_SHIFT) &
   2989  1.12  christos 		    HDPX_CFG_JAMIPG_MASK));
   2990  1.12  christos 	}
   2991   1.1  jmcneill 
   2992   1.1  jmcneill 	/*
   2993   1.1  jmcneill 	 * Set TSO/checksum offload threshold. For frames that is
   2994   1.1  jmcneill 	 * larger than this threshold, hardware wouldn't do
   2995   1.1  jmcneill 	 * TSO/checksum offloading.
   2996   1.1  jmcneill 	 */
   2997  1.12  christos 	reg = (sc->alc_ident->max_framelen >> TSO_OFFLOAD_THRESH_UNIT_SHIFT) &
   2998  1.12  christos 	    TSO_OFFLOAD_THRESH_MASK;
   2999  1.12  christos 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
   3000  1.12  christos 		reg |= TSO_OFFLOAD_ERRLGPKT_DROP_ENB;
   3001  1.12  christos 	CSR_WRITE_4(sc, ALC_TSO_OFFLOAD_THRESH, reg);
   3002   1.1  jmcneill 	/* Configure TxQ. */
   3003   1.1  jmcneill 	reg = (alc_dma_burst[sc->alc_dma_rd_burst] <<
   3004   1.1  jmcneill 	    TXQ_CFG_TX_FIFO_BURST_SHIFT) & TXQ_CFG_TX_FIFO_BURST_MASK;
   3005   1.2  jmcneill 	if (sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8152_B ||
   3006   1.2  jmcneill 	    sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8152_B2)
   3007   1.2  jmcneill 		reg >>= 1;
   3008   1.1  jmcneill 	reg |= (TXQ_CFG_TD_BURST_DEFAULT << TXQ_CFG_TD_BURST_SHIFT) &
   3009   1.1  jmcneill 	    TXQ_CFG_TD_BURST_MASK;
   3010  1.12  christos 	reg |= TXQ_CFG_IP_OPTION_ENB | TXQ_CFG_8023_ENB;
   3011   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_TXQ_CFG, reg | TXQ_CFG_ENHANCED_MODE);
   3012  1.12  christos 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
   3013  1.12  christos 		reg = (TXQ_CFG_TD_BURST_DEFAULT << HQTD_CFG_Q1_BURST_SHIFT |
   3014  1.12  christos 		    TXQ_CFG_TD_BURST_DEFAULT << HQTD_CFG_Q2_BURST_SHIFT |
   3015  1.12  christos 		    TXQ_CFG_TD_BURST_DEFAULT << HQTD_CFG_Q3_BURST_SHIFT |
   3016  1.12  christos 		    HQTD_CFG_BURST_ENB);
   3017  1.12  christos 		CSR_WRITE_4(sc, ALC_HQTD_CFG, reg);
   3018  1.12  christos 		reg = WRR_PRI_RESTRICT_NONE;
   3019  1.12  christos 		reg |= (WRR_PRI_DEFAULT << WRR_PRI0_SHIFT |
   3020  1.12  christos 		    WRR_PRI_DEFAULT << WRR_PRI1_SHIFT |
   3021  1.12  christos 		    WRR_PRI_DEFAULT << WRR_PRI2_SHIFT |
   3022  1.12  christos 		    WRR_PRI_DEFAULT << WRR_PRI3_SHIFT);
   3023  1.12  christos 		CSR_WRITE_4(sc, ALC_WRR, reg);
   3024  1.12  christos 	} else {
   3025  1.12  christos 		/* Configure Rx free descriptor pre-fetching. */
   3026  1.12  christos 		CSR_WRITE_4(sc, ALC_RX_RD_FREE_THRESH,
   3027  1.12  christos 		    ((RX_RD_FREE_THRESH_HI_DEFAULT <<
   3028  1.12  christos 		    RX_RD_FREE_THRESH_HI_SHIFT) & RX_RD_FREE_THRESH_HI_MASK) |
   3029  1.12  christos 		    ((RX_RD_FREE_THRESH_LO_DEFAULT <<
   3030  1.12  christos 		    RX_RD_FREE_THRESH_LO_SHIFT) & RX_RD_FREE_THRESH_LO_MASK));
   3031  1.12  christos 	}
   3032   1.1  jmcneill 
   3033   1.1  jmcneill 	/*
   3034   1.1  jmcneill 	 * Configure flow control parameters.
   3035   1.1  jmcneill 	 * XON  : 80% of Rx FIFO
   3036   1.1  jmcneill 	 * XOFF : 30% of Rx FIFO
   3037   1.1  jmcneill 	 */
   3038  1.12  christos 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
   3039  1.12  christos 		reg = CSR_READ_4(sc, ALC_SRAM_RX_FIFO_LEN);
   3040  1.12  christos 		reg &= SRAM_RX_FIFO_LEN_MASK;
   3041  1.12  christos 		reg *= 8;
   3042  1.12  christos 		if (reg > 8 * 1024)
   3043  1.12  christos 			reg -= RX_FIFO_PAUSE_816X_RSVD;
   3044  1.12  christos 		else
   3045  1.12  christos 			reg -= RX_BUF_SIZE_MAX;
   3046  1.12  christos 		reg /= 8;
   3047  1.12  christos 		CSR_WRITE_4(sc, ALC_RX_FIFO_PAUSE_THRESH,
   3048  1.12  christos 		    ((reg << RX_FIFO_PAUSE_THRESH_LO_SHIFT) &
   3049  1.12  christos 		    RX_FIFO_PAUSE_THRESH_LO_MASK) |
   3050  1.12  christos 		    (((RX_FIFO_PAUSE_816X_RSVD / 8) <<
   3051  1.12  christos 		    RX_FIFO_PAUSE_THRESH_HI_SHIFT) &
   3052  1.12  christos 		    RX_FIFO_PAUSE_THRESH_HI_MASK));
   3053  1.12  christos 	} else if (sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8131 ||
   3054   1.2  jmcneill 	    sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8132) {
   3055   1.2  jmcneill 		reg = CSR_READ_4(sc, ALC_SRAM_RX_FIFO_LEN);
   3056   1.2  jmcneill 		rxf_hi = (reg * 8) / 10;
   3057   1.2  jmcneill 		rxf_lo = (reg * 3) / 10;
   3058   1.2  jmcneill 		CSR_WRITE_4(sc, ALC_RX_FIFO_PAUSE_THRESH,
   3059   1.2  jmcneill 		    ((rxf_lo << RX_FIFO_PAUSE_THRESH_LO_SHIFT) &
   3060   1.2  jmcneill 		     RX_FIFO_PAUSE_THRESH_LO_MASK) |
   3061   1.2  jmcneill 		    ((rxf_hi << RX_FIFO_PAUSE_THRESH_HI_SHIFT) &
   3062   1.2  jmcneill 		     RX_FIFO_PAUSE_THRESH_HI_MASK));
   3063   1.2  jmcneill 	}
   3064   1.2  jmcneill 
   3065  1.12  christos 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
   3066  1.12  christos 		/* Disable RSS until I understand L1C/L2C's RSS logic. */
   3067  1.12  christos 		CSR_WRITE_4(sc, ALC_RSS_IDT_TABLE0, 0);
   3068  1.12  christos 		CSR_WRITE_4(sc, ALC_RSS_CPU, 0);
   3069  1.12  christos 	}
   3070   1.1  jmcneill 
   3071   1.1  jmcneill 	/* Configure RxQ. */
   3072   1.1  jmcneill 	reg = (RXQ_CFG_RD_BURST_DEFAULT << RXQ_CFG_RD_BURST_SHIFT) &
   3073   1.1  jmcneill 	    RXQ_CFG_RD_BURST_MASK;
   3074   1.1  jmcneill 	reg |= RXQ_CFG_RSS_MODE_DIS;
   3075  1.41   msaitoh 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
   3076  1.12  christos 		reg |= (RXQ_CFG_816X_IDT_TBL_SIZE_DEFAULT <<
   3077  1.12  christos 		    RXQ_CFG_816X_IDT_TBL_SIZE_SHIFT) &
   3078  1.12  christos 		    RXQ_CFG_816X_IDT_TBL_SIZE_MASK;
   3079  1.41   msaitoh 		if ((sc->alc_flags & ALC_FLAG_FASTETHER) == 0)
   3080  1.41   msaitoh 			reg |= RXQ_CFG_ASPM_THROUGHPUT_LIMIT_100M;
   3081  1.41   msaitoh 	} else {
   3082  1.41   msaitoh 		if ((sc->alc_flags & ALC_FLAG_FASTETHER) == 0 &&
   3083  1.41   msaitoh 		    sc->alc_ident->deviceid != PCI_PRODUCT_ATTANSIC_AR8151_V2)
   3084  1.41   msaitoh 			reg |= RXQ_CFG_ASPM_THROUGHPUT_LIMIT_100M;
   3085  1.41   msaitoh 	}
   3086   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_RXQ_CFG, reg);
   3087   1.1  jmcneill 
   3088   1.1  jmcneill 	/* Configure DMA parameters. */
   3089   1.1  jmcneill 	reg = DMA_CFG_OUT_ORDER | DMA_CFG_RD_REQ_PRI;
   3090   1.1  jmcneill 	reg |= sc->alc_rcb;
   3091   1.1  jmcneill 	if ((sc->alc_flags & ALC_FLAG_CMB_BUG) == 0)
   3092   1.1  jmcneill 		reg |= DMA_CFG_CMB_ENB;
   3093   1.1  jmcneill 	if ((sc->alc_flags & ALC_FLAG_SMB_BUG) == 0)
   3094   1.1  jmcneill 		reg |= DMA_CFG_SMB_ENB;
   3095   1.1  jmcneill 	else
   3096   1.1  jmcneill 		reg |= DMA_CFG_SMB_DIS;
   3097   1.1  jmcneill 	reg |= (sc->alc_dma_rd_burst & DMA_CFG_RD_BURST_MASK) <<
   3098   1.1  jmcneill 	    DMA_CFG_RD_BURST_SHIFT;
   3099   1.1  jmcneill 	reg |= (sc->alc_dma_wr_burst & DMA_CFG_WR_BURST_MASK) <<
   3100   1.1  jmcneill 	    DMA_CFG_WR_BURST_SHIFT;
   3101   1.1  jmcneill 	reg |= (DMA_CFG_RD_DELAY_CNT_DEFAULT << DMA_CFG_RD_DELAY_CNT_SHIFT) &
   3102   1.1  jmcneill 	    DMA_CFG_RD_DELAY_CNT_MASK;
   3103   1.1  jmcneill 	reg |= (DMA_CFG_WR_DELAY_CNT_DEFAULT << DMA_CFG_WR_DELAY_CNT_SHIFT) &
   3104   1.1  jmcneill 	    DMA_CFG_WR_DELAY_CNT_MASK;
   3105  1.12  christos 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
   3106  1.12  christos 		switch (AR816X_REV(sc->alc_rev)) {
   3107  1.12  christos 		case AR816X_REV_A0:
   3108  1.12  christos 		case AR816X_REV_A1:
   3109  1.41   msaitoh 			reg |= DMA_CFG_RD_CHNL_SEL_2;
   3110  1.12  christos 			break;
   3111  1.12  christos 		case AR816X_REV_B0:
   3112  1.12  christos 			/* FALLTHROUGH */
   3113  1.12  christos 		default:
   3114  1.41   msaitoh 			reg |= DMA_CFG_RD_CHNL_SEL_4;
   3115  1.12  christos 			break;
   3116  1.12  christos 		}
   3117  1.12  christos 	}
   3118   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_DMA_CFG, reg);
   3119   1.1  jmcneill 
   3120   1.1  jmcneill 	/*
   3121   1.1  jmcneill 	 * Configure Tx/Rx MACs.
   3122   1.1  jmcneill 	 *  - Auto-padding for short frames.
   3123   1.1  jmcneill 	 *  - Enable CRC generation.
   3124   1.1  jmcneill 	 *  Actual reconfiguration of MAC for resolved speed/duplex
   3125   1.1  jmcneill 	 *  is followed after detection of link establishment.
   3126   1.2  jmcneill 	 *  AR813x/AR815x always does checksum computation regardless
   3127   1.1  jmcneill 	 *  of MAC_CFG_RXCSUM_ENB bit. Also the controller is known to
   3128   1.1  jmcneill 	 *  have bug in protocol field in Rx return structure so
   3129   1.1  jmcneill 	 *  these controllers can't handle fragmented frames. Disable
   3130   1.1  jmcneill 	 *  Rx checksum offloading until there is a newer controller
   3131   1.1  jmcneill 	 *  that has sane implementation.
   3132   1.1  jmcneill 	 */
   3133   1.1  jmcneill 	reg = MAC_CFG_TX_CRC_ENB | MAC_CFG_TX_AUTO_PAD | MAC_CFG_FULL_DUPLEX |
   3134   1.1  jmcneill 	    ((MAC_CFG_PREAMBLE_DEFAULT << MAC_CFG_PREAMBLE_SHIFT) &
   3135   1.1  jmcneill 	    MAC_CFG_PREAMBLE_MASK);
   3136  1.12  christos 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0 ||
   3137  1.12  christos 	    sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8151 ||
   3138   1.2  jmcneill 	    sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8151_V2 ||
   3139   1.2  jmcneill 	    sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8152_B2)
   3140   1.2  jmcneill 		reg |= MAC_CFG_HASH_ALG_CRC32 | MAC_CFG_SPEED_MODE_SW;
   3141   1.1  jmcneill 	if ((sc->alc_flags & ALC_FLAG_FASTETHER) != 0)
   3142   1.1  jmcneill 		reg |= MAC_CFG_SPEED_10_100;
   3143   1.1  jmcneill 	else
   3144   1.1  jmcneill 		reg |= MAC_CFG_SPEED_1000;
   3145   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
   3146   1.1  jmcneill 
   3147   1.1  jmcneill 	/* Set up the receive filter. */
   3148   1.1  jmcneill 	alc_iff(sc);
   3149   1.1  jmcneill 	alc_rxvlan(sc);
   3150   1.1  jmcneill 
   3151   1.1  jmcneill 	/* Acknowledge all pending interrupts and clear it. */
   3152   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_INTR_MASK, ALC_INTRS);
   3153   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_INTR_STATUS, 0xFFFFFFFF);
   3154   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_INTR_STATUS, 0);
   3155   1.1  jmcneill 
   3156   1.1  jmcneill 	sc->alc_flags &= ~ALC_FLAG_LINK;
   3157   1.1  jmcneill 	/* Switch to the current media. */
   3158   1.1  jmcneill 	mii = &sc->sc_miibus;
   3159   1.1  jmcneill 	mii_mediachg(mii);
   3160   1.1  jmcneill 
   3161   1.1  jmcneill 	callout_schedule(&sc->sc_tick_ch, hz);
   3162   1.1  jmcneill 
   3163   1.1  jmcneill 	ifp->if_flags |= IFF_RUNNING;
   3164   1.1  jmcneill 	ifp->if_flags &= ~IFF_OACTIVE;
   3165   1.1  jmcneill 
   3166   1.1  jmcneill 	return (0);
   3167   1.1  jmcneill }
   3168   1.1  jmcneill 
   3169   1.1  jmcneill static void
   3170   1.1  jmcneill alc_stop(struct ifnet *ifp, int disable)
   3171   1.1  jmcneill {
   3172   1.1  jmcneill 	struct alc_softc *sc = ifp->if_softc;
   3173   1.1  jmcneill 	struct alc_txdesc *txd;
   3174   1.1  jmcneill 	struct alc_rxdesc *rxd;
   3175   1.1  jmcneill 	uint32_t reg;
   3176   1.1  jmcneill 	int i;
   3177   1.1  jmcneill 
   3178   1.1  jmcneill 	callout_stop(&sc->sc_tick_ch);
   3179   1.1  jmcneill 
   3180   1.1  jmcneill 	/*
   3181   1.1  jmcneill 	 * Mark the interface down and cancel the watchdog timer.
   3182   1.1  jmcneill 	 */
   3183   1.1  jmcneill 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   3184   1.1  jmcneill 	ifp->if_timer = 0;
   3185   1.1  jmcneill 
   3186   1.1  jmcneill 	sc->alc_flags &= ~ALC_FLAG_LINK;
   3187   1.1  jmcneill 
   3188   1.1  jmcneill 	alc_stats_update(sc);
   3189   1.1  jmcneill 
   3190   1.1  jmcneill 	mii_down(&sc->sc_miibus);
   3191   1.1  jmcneill 
   3192   1.1  jmcneill 	/* Disable interrupts. */
   3193   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_INTR_MASK, 0);
   3194   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_INTR_STATUS, 0xFFFFFFFF);
   3195   1.1  jmcneill 
   3196   1.1  jmcneill 	/* Disable DMA. */
   3197   1.1  jmcneill 	reg = CSR_READ_4(sc, ALC_DMA_CFG);
   3198   1.1  jmcneill 	reg &= ~(DMA_CFG_CMB_ENB | DMA_CFG_SMB_ENB);
   3199   1.1  jmcneill 	reg |= DMA_CFG_SMB_DIS;
   3200   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_DMA_CFG, reg);
   3201   1.1  jmcneill 	DELAY(1000);
   3202   1.1  jmcneill 
   3203   1.1  jmcneill 	/* Stop Rx/Tx MACs. */
   3204   1.1  jmcneill 	alc_stop_mac(sc);
   3205   1.1  jmcneill 
   3206   1.1  jmcneill 	/* Disable interrupts which might be touched in taskq handler. */
   3207   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_INTR_STATUS, 0xFFFFFFFF);
   3208   1.1  jmcneill 
   3209  1.12  christos 	/* Disable L0s/L1s */
   3210  1.12  christos 	alc_aspm(sc, 0, IFM_UNKNOWN);
   3211  1.12  christos 
   3212   1.1  jmcneill 	/* Reclaim Rx buffers that have been processed. */
   3213   1.1  jmcneill 	if (sc->alc_cdata.alc_rxhead != NULL)
   3214   1.1  jmcneill 		m_freem(sc->alc_cdata.alc_rxhead);
   3215   1.1  jmcneill 	ALC_RXCHAIN_RESET(sc);
   3216   1.1  jmcneill 	/*
   3217   1.1  jmcneill 	 * Free Tx/Rx mbufs still in the queues.
   3218   1.1  jmcneill 	 */
   3219   1.1  jmcneill 	for (i = 0; i < ALC_RX_RING_CNT; i++) {
   3220   1.1  jmcneill 		rxd = &sc->alc_cdata.alc_rxdesc[i];
   3221   1.1  jmcneill 		if (rxd->rx_m != NULL) {
   3222  1.15      leot 			bus_dmamap_sync(sc->sc_dmat, rxd->rx_dmamap, 0,
   3223  1.15      leot 			    rxd->rx_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
   3224   1.1  jmcneill 			bus_dmamap_unload(sc->sc_dmat, rxd->rx_dmamap);
   3225   1.1  jmcneill 			m_freem(rxd->rx_m);
   3226   1.1  jmcneill 			rxd->rx_m = NULL;
   3227   1.1  jmcneill 		}
   3228   1.1  jmcneill 	}
   3229   1.1  jmcneill 	for (i = 0; i < ALC_TX_RING_CNT; i++) {
   3230   1.1  jmcneill 		txd = &sc->alc_cdata.alc_txdesc[i];
   3231   1.1  jmcneill 		if (txd->tx_m != NULL) {
   3232  1.15      leot 			bus_dmamap_sync(sc->sc_dmat, txd->tx_dmamap, 0,
   3233  1.15      leot 			    txd->tx_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   3234   1.1  jmcneill 			bus_dmamap_unload(sc->sc_dmat, txd->tx_dmamap);
   3235   1.1  jmcneill 			m_freem(txd->tx_m);
   3236   1.1  jmcneill 			txd->tx_m = NULL;
   3237   1.1  jmcneill 		}
   3238   1.1  jmcneill 	}
   3239   1.1  jmcneill }
   3240   1.1  jmcneill 
   3241   1.1  jmcneill static void
   3242   1.1  jmcneill alc_stop_mac(struct alc_softc *sc)
   3243   1.1  jmcneill {
   3244   1.1  jmcneill 	uint32_t reg;
   3245   1.1  jmcneill 	int i;
   3246   1.1  jmcneill 
   3247  1.12  christos 	alc_stop_queue(sc);
   3248   1.1  jmcneill 	/* Disable Rx/Tx MAC. */
   3249   1.1  jmcneill 	reg = CSR_READ_4(sc, ALC_MAC_CFG);
   3250   1.1  jmcneill 	if ((reg & (MAC_CFG_TX_ENB | MAC_CFG_RX_ENB)) != 0) {
   3251   1.2  jmcneill 		reg &= ~(MAC_CFG_TX_ENB | MAC_CFG_RX_ENB);
   3252   1.1  jmcneill 		CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
   3253   1.1  jmcneill 	}
   3254   1.1  jmcneill 	for (i = ALC_TIMEOUT; i > 0; i--) {
   3255   1.1  jmcneill 		reg = CSR_READ_4(sc, ALC_IDLE_STATUS);
   3256  1.12  christos 		if ((reg & (IDLE_STATUS_RXMAC | IDLE_STATUS_TXMAC)) == 0)
   3257   1.1  jmcneill 			break;
   3258   1.1  jmcneill 		DELAY(10);
   3259   1.1  jmcneill 	}
   3260   1.1  jmcneill 	if (i == 0)
   3261   1.1  jmcneill 		printf("%s: could not disable Rx/Tx MAC(0x%08x)!\n",
   3262   1.1  jmcneill 		    device_xname(sc->sc_dev), reg);
   3263   1.1  jmcneill }
   3264   1.1  jmcneill 
   3265   1.1  jmcneill static void
   3266   1.1  jmcneill alc_start_queue(struct alc_softc *sc)
   3267   1.1  jmcneill {
   3268   1.1  jmcneill 	uint32_t qcfg[] = {
   3269   1.1  jmcneill 		0,
   3270   1.1  jmcneill 		RXQ_CFG_QUEUE0_ENB,
   3271   1.1  jmcneill 		RXQ_CFG_QUEUE0_ENB | RXQ_CFG_QUEUE1_ENB,
   3272   1.1  jmcneill 		RXQ_CFG_QUEUE0_ENB | RXQ_CFG_QUEUE1_ENB | RXQ_CFG_QUEUE2_ENB,
   3273   1.1  jmcneill 		RXQ_CFG_ENB
   3274   1.1  jmcneill 	};
   3275   1.1  jmcneill 	uint32_t cfg;
   3276   1.1  jmcneill 
   3277   1.1  jmcneill 	/* Enable RxQ. */
   3278   1.1  jmcneill 	cfg = CSR_READ_4(sc, ALC_RXQ_CFG);
   3279  1.12  christos 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
   3280  1.12  christos 		cfg &= ~RXQ_CFG_ENB;
   3281  1.12  christos 		cfg |= qcfg[1];
   3282  1.12  christos 	} else
   3283  1.12  christos 		cfg |= RXQ_CFG_QUEUE0_ENB;
   3284   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_RXQ_CFG, cfg);
   3285   1.1  jmcneill 	/* Enable TxQ. */
   3286   1.1  jmcneill 	cfg = CSR_READ_4(sc, ALC_TXQ_CFG);
   3287   1.1  jmcneill 	cfg |= TXQ_CFG_ENB;
   3288   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_TXQ_CFG, cfg);
   3289   1.1  jmcneill }
   3290   1.1  jmcneill 
   3291   1.1  jmcneill static void
   3292   1.1  jmcneill alc_stop_queue(struct alc_softc *sc)
   3293   1.1  jmcneill {
   3294   1.1  jmcneill 	uint32_t reg;
   3295   1.1  jmcneill 	int i;
   3296   1.1  jmcneill 
   3297   1.1  jmcneill 	/* Disable RxQ. */
   3298   1.1  jmcneill 	reg = CSR_READ_4(sc, ALC_RXQ_CFG);
   3299  1.12  christos 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
   3300  1.12  christos 		if ((reg & RXQ_CFG_ENB) != 0) {
   3301  1.12  christos 			reg &= ~RXQ_CFG_ENB;
   3302  1.12  christos 			CSR_WRITE_4(sc, ALC_RXQ_CFG, reg);
   3303  1.12  christos 		}
   3304  1.12  christos 	} else {
   3305  1.12  christos 		if ((reg & RXQ_CFG_QUEUE0_ENB) != 0) {
   3306  1.12  christos 			reg &= ~RXQ_CFG_QUEUE0_ENB;
   3307  1.12  christos 			CSR_WRITE_4(sc, ALC_RXQ_CFG, reg);
   3308  1.12  christos 		}
   3309  1.36   msaitoh 	}
   3310   1.1  jmcneill 	/* Disable TxQ. */
   3311   1.1  jmcneill 	reg = CSR_READ_4(sc, ALC_TXQ_CFG);
   3312   1.2  jmcneill 	if ((reg & TXQ_CFG_ENB) != 0) {
   3313   1.1  jmcneill 		reg &= ~TXQ_CFG_ENB;
   3314   1.1  jmcneill 		CSR_WRITE_4(sc, ALC_TXQ_CFG, reg);
   3315   1.1  jmcneill 	}
   3316  1.12  christos 	DELAY(40);
   3317   1.1  jmcneill 	for (i = ALC_TIMEOUT; i > 0; i--) {
   3318   1.1  jmcneill 		reg = CSR_READ_4(sc, ALC_IDLE_STATUS);
   3319   1.1  jmcneill 		if ((reg & (IDLE_STATUS_RXQ | IDLE_STATUS_TXQ)) == 0)
   3320   1.1  jmcneill 			break;
   3321   1.1  jmcneill 		DELAY(10);
   3322   1.1  jmcneill 	}
   3323   1.1  jmcneill 	if (i == 0)
   3324   1.1  jmcneill 		printf("%s: could not disable RxQ/TxQ (0x%08x)!\n",
   3325   1.1  jmcneill 		    device_xname(sc->sc_dev), reg);
   3326   1.1  jmcneill }
   3327   1.1  jmcneill 
   3328   1.1  jmcneill static void
   3329   1.1  jmcneill alc_init_tx_ring(struct alc_softc *sc)
   3330   1.1  jmcneill {
   3331   1.1  jmcneill 	struct alc_ring_data *rd;
   3332   1.1  jmcneill 	struct alc_txdesc *txd;
   3333   1.1  jmcneill 	int i;
   3334   1.1  jmcneill 
   3335   1.1  jmcneill 	sc->alc_cdata.alc_tx_prod = 0;
   3336   1.1  jmcneill 	sc->alc_cdata.alc_tx_cons = 0;
   3337   1.1  jmcneill 	sc->alc_cdata.alc_tx_cnt = 0;
   3338   1.1  jmcneill 
   3339   1.1  jmcneill 	rd = &sc->alc_rdata;
   3340   1.1  jmcneill 	memset(rd->alc_tx_ring, 0, ALC_TX_RING_SZ);
   3341   1.1  jmcneill 	for (i = 0; i < ALC_TX_RING_CNT; i++) {
   3342   1.1  jmcneill 		txd = &sc->alc_cdata.alc_txdesc[i];
   3343   1.1  jmcneill 		txd->tx_m = NULL;
   3344   1.1  jmcneill 	}
   3345   1.1  jmcneill 
   3346   1.1  jmcneill 	bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_tx_ring_map, 0,
   3347   1.1  jmcneill 	    sc->alc_cdata.alc_tx_ring_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
   3348   1.1  jmcneill }
   3349   1.1  jmcneill 
   3350   1.1  jmcneill static int
   3351   1.7       mrg alc_init_rx_ring(struct alc_softc *sc, bool init)
   3352   1.1  jmcneill {
   3353   1.1  jmcneill 	struct alc_ring_data *rd;
   3354   1.1  jmcneill 	struct alc_rxdesc *rxd;
   3355   1.1  jmcneill 	int i;
   3356   1.1  jmcneill 
   3357   1.1  jmcneill 	sc->alc_cdata.alc_rx_cons = ALC_RX_RING_CNT - 1;
   3358   1.1  jmcneill 	rd = &sc->alc_rdata;
   3359   1.1  jmcneill 	memset(rd->alc_rx_ring, 0, ALC_RX_RING_SZ);
   3360   1.1  jmcneill 	for (i = 0; i < ALC_RX_RING_CNT; i++) {
   3361   1.1  jmcneill 		rxd = &sc->alc_cdata.alc_rxdesc[i];
   3362   1.1  jmcneill 		rxd->rx_m = NULL;
   3363   1.1  jmcneill 		rxd->rx_desc = &rd->alc_rx_ring[i];
   3364   1.7       mrg 		if (alc_newbuf(sc, rxd, init) != 0)
   3365   1.1  jmcneill 			return (ENOBUFS);
   3366   1.1  jmcneill 	}
   3367   1.1  jmcneill 
   3368   1.1  jmcneill 	/*
   3369   1.1  jmcneill 	 * Since controller does not update Rx descriptors, driver
   3370   1.1  jmcneill 	 * does have to read Rx descriptors back so BUS_DMASYNC_PREWRITE
   3371   1.1  jmcneill 	 * is enough to ensure coherence.
   3372   1.1  jmcneill 	 */
   3373   1.1  jmcneill 	bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_rx_ring_map, 0,
   3374   1.1  jmcneill 	    sc->alc_cdata.alc_rx_ring_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
   3375   1.1  jmcneill 	/* Let controller know availability of new Rx buffers. */
   3376   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_MBOX_RD0_PROD_IDX, sc->alc_cdata.alc_rx_cons);
   3377   1.1  jmcneill 
   3378   1.1  jmcneill 	return (0);
   3379   1.1  jmcneill }
   3380   1.1  jmcneill 
   3381   1.1  jmcneill static void
   3382   1.1  jmcneill alc_init_rr_ring(struct alc_softc *sc)
   3383   1.1  jmcneill {
   3384   1.1  jmcneill 	struct alc_ring_data *rd;
   3385   1.1  jmcneill 
   3386   1.1  jmcneill 	sc->alc_cdata.alc_rr_cons = 0;
   3387   1.1  jmcneill 	ALC_RXCHAIN_RESET(sc);
   3388   1.1  jmcneill 
   3389   1.1  jmcneill 	rd = &sc->alc_rdata;
   3390   1.1  jmcneill 	memset(rd->alc_rr_ring, 0, ALC_RR_RING_SZ);
   3391   1.1  jmcneill 	bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_rr_ring_map, 0,
   3392  1.15      leot 	    sc->alc_cdata.alc_rr_ring_map->dm_mapsize,
   3393  1.15      leot 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   3394   1.1  jmcneill }
   3395   1.1  jmcneill 
   3396   1.1  jmcneill static void
   3397   1.1  jmcneill alc_init_cmb(struct alc_softc *sc)
   3398   1.1  jmcneill {
   3399   1.1  jmcneill 	struct alc_ring_data *rd;
   3400   1.1  jmcneill 
   3401   1.1  jmcneill 	rd = &sc->alc_rdata;
   3402   1.1  jmcneill 	memset(rd->alc_cmb, 0, ALC_CMB_SZ);
   3403   1.1  jmcneill 	bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_cmb_map, 0,
   3404  1.15      leot 	    sc->alc_cdata.alc_cmb_map->dm_mapsize,
   3405  1.15      leot 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   3406   1.1  jmcneill }
   3407   1.1  jmcneill 
   3408   1.1  jmcneill static void
   3409   1.1  jmcneill alc_init_smb(struct alc_softc *sc)
   3410   1.1  jmcneill {
   3411   1.1  jmcneill 	struct alc_ring_data *rd;
   3412   1.1  jmcneill 
   3413   1.1  jmcneill 	rd = &sc->alc_rdata;
   3414   1.1  jmcneill 	memset(rd->alc_smb, 0, ALC_SMB_SZ);
   3415   1.1  jmcneill 	bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_smb_map, 0,
   3416  1.15      leot 	    sc->alc_cdata.alc_smb_map->dm_mapsize,
   3417  1.15      leot 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   3418   1.1  jmcneill }
   3419   1.1  jmcneill 
   3420   1.1  jmcneill static void
   3421   1.1  jmcneill alc_rxvlan(struct alc_softc *sc)
   3422   1.1  jmcneill {
   3423   1.1  jmcneill 	uint32_t reg;
   3424   1.1  jmcneill 
   3425   1.1  jmcneill 	reg = CSR_READ_4(sc, ALC_MAC_CFG);
   3426   1.3  sborrill 	if (sc->sc_ec.ec_capenable & ETHERCAP_VLAN_HWTAGGING)
   3427   1.1  jmcneill 		reg |= MAC_CFG_VLAN_TAG_STRIP;
   3428   1.1  jmcneill 	else
   3429   1.1  jmcneill 		reg &= ~MAC_CFG_VLAN_TAG_STRIP;
   3430   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
   3431   1.1  jmcneill }
   3432   1.1  jmcneill 
   3433   1.1  jmcneill static void
   3434   1.1  jmcneill alc_iff(struct alc_softc *sc)
   3435   1.1  jmcneill {
   3436   1.1  jmcneill 	struct ethercom *ec = &sc->sc_ec;
   3437   1.1  jmcneill 	struct ifnet *ifp = &ec->ec_if;
   3438   1.1  jmcneill 	struct ether_multi *enm;
   3439   1.1  jmcneill 	struct ether_multistep step;
   3440   1.1  jmcneill 	uint32_t crc;
   3441   1.1  jmcneill 	uint32_t mchash[2];
   3442   1.1  jmcneill 	uint32_t rxcfg;
   3443   1.1  jmcneill 
   3444   1.1  jmcneill 	rxcfg = CSR_READ_4(sc, ALC_MAC_CFG);
   3445   1.1  jmcneill 	rxcfg &= ~(MAC_CFG_ALLMULTI | MAC_CFG_BCAST | MAC_CFG_PROMISC);
   3446   1.1  jmcneill 	ifp->if_flags &= ~IFF_ALLMULTI;
   3447   1.1  jmcneill 
   3448   1.1  jmcneill 	/*
   3449   1.1  jmcneill 	 * Always accept broadcast frames.
   3450   1.1  jmcneill 	 */
   3451   1.1  jmcneill 	rxcfg |= MAC_CFG_BCAST;
   3452   1.1  jmcneill 
   3453  1.44   msaitoh 	/* Program new filter. */
   3454  1.44   msaitoh 	if ((ifp->if_flags & IFF_PROMISC) != 0)
   3455  1.44   msaitoh 		goto update;
   3456  1.44   msaitoh 
   3457  1.44   msaitoh 	memset(mchash, 0, sizeof(mchash));
   3458  1.44   msaitoh 
   3459  1.44   msaitoh 	ETHER_LOCK(ec);
   3460  1.44   msaitoh 	ETHER_FIRST_MULTI(step, ec, enm);
   3461  1.44   msaitoh 	while (enm != NULL) {
   3462  1.44   msaitoh 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   3463  1.44   msaitoh 			/* XXX Use ETHER_F_ALLMULTI in future. */
   3464  1.44   msaitoh 			ifp->if_flags |= IFF_ALLMULTI;
   3465  1.44   msaitoh 			ETHER_UNLOCK(ec);
   3466  1.44   msaitoh 			goto update;
   3467  1.44   msaitoh 		}
   3468  1.44   msaitoh 		crc = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN);
   3469  1.45   msaitoh 		mchash[crc >> 31] |= 1U << ((crc >> 26) & 0x1f);
   3470  1.44   msaitoh 		ETHER_NEXT_MULTI(step, enm);
   3471  1.44   msaitoh 	}
   3472  1.44   msaitoh 	ETHER_UNLOCK(ec);
   3473  1.44   msaitoh 
   3474  1.44   msaitoh update:
   3475  1.44   msaitoh 	if ((ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI)) != 0) {
   3476  1.44   msaitoh 		if (ifp->if_flags & IFF_PROMISC) {
   3477   1.1  jmcneill 			rxcfg |= MAC_CFG_PROMISC;
   3478  1.44   msaitoh 			/* XXX Use ETHER_F_ALLMULTI in future. */
   3479  1.44   msaitoh 			ifp->if_flags |= IFF_ALLMULTI;
   3480  1.44   msaitoh 		} else
   3481   1.1  jmcneill 			rxcfg |= MAC_CFG_ALLMULTI;
   3482   1.1  jmcneill 		mchash[0] = mchash[1] = 0xFFFFFFFF;
   3483   1.1  jmcneill 	}
   3484   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_MAR0, mchash[0]);
   3485   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_MAR1, mchash[1]);
   3486   1.1  jmcneill 	CSR_WRITE_4(sc, ALC_MAC_CFG, rxcfg);
   3487   1.1  jmcneill }
   3488   1.1  jmcneill 
   3489   1.5  jmcneill MODULE(MODULE_CLASS_DRIVER, if_alc, "pci");
   3490   1.1  jmcneill 
   3491   1.1  jmcneill #ifdef _MODULE
   3492   1.1  jmcneill #include "ioconf.c"
   3493   1.1  jmcneill #endif
   3494   1.1  jmcneill 
   3495   1.1  jmcneill static int
   3496   1.1  jmcneill if_alc_modcmd(modcmd_t cmd, void *opaque)
   3497   1.1  jmcneill {
   3498   1.1  jmcneill 	int error = 0;
   3499   1.1  jmcneill 
   3500   1.1  jmcneill 	switch (cmd) {
   3501   1.1  jmcneill 	case MODULE_CMD_INIT:
   3502   1.1  jmcneill #ifdef _MODULE
   3503   1.1  jmcneill 		error = config_init_component(cfdriver_ioconf_if_alc,
   3504   1.1  jmcneill 		    cfattach_ioconf_if_alc, cfdata_ioconf_if_alc);
   3505   1.1  jmcneill #endif
   3506   1.1  jmcneill 		return error;
   3507   1.1  jmcneill 	case MODULE_CMD_FINI:
   3508   1.1  jmcneill #ifdef _MODULE
   3509   1.1  jmcneill 		error = config_fini_component(cfdriver_ioconf_if_alc,
   3510   1.1  jmcneill 		    cfattach_ioconf_if_alc, cfdata_ioconf_if_alc);
   3511   1.1  jmcneill #endif
   3512   1.1  jmcneill 		return error;
   3513   1.1  jmcneill 	default:
   3514   1.1  jmcneill 		return ENOTTY;
   3515   1.1  jmcneill 	}
   3516   1.1  jmcneill }
   3517