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