Home | History | Annotate | Line # | Download | only in mii
mii_physubr.c revision 1.53.24.1
      1  1.53.24.1      matt /*	$NetBSD: mii_physubr.c,v 1.53.24.1 2008/01/09 01:53:22 matt Exp $	*/
      2        1.1   thorpej 
      3        1.1   thorpej /*-
      4       1.23   thorpej  * Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc.
      5        1.1   thorpej  * All rights reserved.
      6        1.1   thorpej  *
      7        1.1   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8        1.1   thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9        1.1   thorpej  * NASA Ames Research Center.
     10        1.1   thorpej  *
     11        1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     12        1.1   thorpej  * modification, are permitted provided that the following conditions
     13        1.1   thorpej  * are met:
     14        1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     15        1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     16        1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17        1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     18        1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     19        1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     20        1.1   thorpej  *    must display the following acknowledgement:
     21        1.1   thorpej  *	This product includes software developed by the NetBSD
     22        1.1   thorpej  *	Foundation, Inc. and its contributors.
     23        1.1   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24        1.1   thorpej  *    contributors may be used to endorse or promote products derived
     25        1.1   thorpej  *    from this software without specific prior written permission.
     26        1.1   thorpej  *
     27        1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28        1.1   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29        1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30        1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31        1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32        1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33        1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34        1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35        1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36        1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37        1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38        1.1   thorpej  */
     39        1.1   thorpej 
     40        1.1   thorpej /*
     41        1.1   thorpej  * Subroutines common to all PHYs.
     42        1.1   thorpej  */
     43       1.30     lukem 
     44       1.30     lukem #include <sys/cdefs.h>
     45  1.53.24.1      matt __KERNEL_RCSID(0, "$NetBSD: mii_physubr.c,v 1.53.24.1 2008/01/09 01:53:22 matt Exp $");
     46        1.1   thorpej 
     47        1.1   thorpej #include <sys/param.h>
     48        1.1   thorpej #include <sys/device.h>
     49        1.1   thorpej #include <sys/systm.h>
     50        1.3   thorpej #include <sys/kernel.h>
     51        1.1   thorpej #include <sys/socket.h>
     52        1.3   thorpej #include <sys/errno.h>
     53       1.19  augustss #include <sys/proc.h>
     54        1.1   thorpej 
     55        1.1   thorpej #include <net/if.h>
     56        1.1   thorpej #include <net/if_media.h>
     57       1.15   thorpej #include <net/route.h>
     58        1.1   thorpej 
     59        1.1   thorpej #include <dev/mii/mii.h>
     60        1.1   thorpej #include <dev/mii/miivar.h>
     61        1.1   thorpej 
     62       1.46     joerg static void mii_phy_statusmsg(struct mii_softc *);
     63       1.46     joerg 
     64        1.6   thorpej /*
     65        1.6   thorpej  * Media to register setting conversion table.  Order matters.
     66        1.6   thorpej  */
     67       1.43   thorpej static const struct mii_media mii_media_table[MII_NMEDIA] = {
     68       1.23   thorpej 	/* None */
     69       1.23   thorpej 	{ BMCR_ISO,		ANAR_CSMA,
     70       1.23   thorpej 	  0, },
     71       1.23   thorpej 
     72       1.23   thorpej 	/* 10baseT */
     73       1.23   thorpej 	{ BMCR_S10,		ANAR_CSMA|ANAR_10,
     74       1.23   thorpej 	  0, },
     75       1.23   thorpej 
     76       1.23   thorpej 	/* 10baseT-FDX */
     77       1.23   thorpej 	{ BMCR_S10|BMCR_FDX,	ANAR_CSMA|ANAR_10_FD,
     78       1.23   thorpej 	  0, },
     79       1.23   thorpej 
     80       1.23   thorpej 	/* 100baseT4 */
     81       1.23   thorpej 	{ BMCR_S100,		ANAR_CSMA|ANAR_T4,
     82       1.23   thorpej 	  0, },
     83       1.23   thorpej 
     84       1.23   thorpej 	/* 100baseTX */
     85       1.23   thorpej 	{ BMCR_S100,		ANAR_CSMA|ANAR_TX,
     86       1.23   thorpej 	  0, },
     87       1.23   thorpej 
     88       1.23   thorpej 	/* 100baseTX-FDX */
     89       1.23   thorpej 	{ BMCR_S100|BMCR_FDX,	ANAR_CSMA|ANAR_TX_FD,
     90       1.23   thorpej 	  0, },
     91       1.23   thorpej 
     92       1.23   thorpej 	/* 1000baseX */
     93       1.23   thorpej 	{ BMCR_S1000,		ANAR_CSMA,
     94       1.23   thorpej 	  0, },
     95       1.23   thorpej 
     96       1.23   thorpej 	/* 1000baseX-FDX */
     97       1.23   thorpej 	{ BMCR_S1000|BMCR_FDX,	ANAR_CSMA,
     98       1.23   thorpej 	  0, },
     99       1.23   thorpej 
    100       1.23   thorpej 	/* 1000baseT */
    101       1.23   thorpej 	{ BMCR_S1000,		ANAR_CSMA,
    102       1.23   thorpej 	  GTCR_ADV_1000THDX },
    103       1.23   thorpej 
    104       1.23   thorpej 	/* 1000baseT-FDX */
    105       1.23   thorpej 	{ BMCR_S1000,		ANAR_CSMA,
    106       1.23   thorpej 	  GTCR_ADV_1000TFDX },
    107        1.6   thorpej };
    108        1.6   thorpej 
    109       1.43   thorpej static void	mii_phy_auto_timeout(void *);
    110        1.3   thorpej 
    111        1.6   thorpej void
    112       1.29   thorpej mii_phy_setmedia(struct mii_softc *sc)
    113        1.6   thorpej {
    114        1.6   thorpej 	struct mii_data *mii = sc->mii_pdata;
    115        1.6   thorpej 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    116       1.23   thorpej 	int bmcr, anar, gtcr;
    117       1.12   thorpej 
    118       1.13   thorpej 	if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO) {
    119       1.41   thorpej 		/*
    120       1.41   thorpej 		 * Force renegotiation if MIIF_DOPAUSE.
    121       1.41   thorpej 		 *
    122       1.41   thorpej 		 * XXX This is only necessary because many NICs don't
    123       1.41   thorpej 		 * XXX advertise PAUSE capabilities at boot time.  Maybe
    124       1.41   thorpej 		 * XXX we should force this only once?
    125       1.41   thorpej 		 */
    126       1.35      matt 		if ((PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) == 0 ||
    127       1.38   thorpej 		    (sc->mii_flags & (MIIF_FORCEANEG|MIIF_DOPAUSE)))
    128       1.14   thorpej 			(void) mii_phy_auto(sc, 1);
    129       1.13   thorpej 		return;
    130       1.13   thorpej 	}
    131        1.6   thorpej 
    132        1.6   thorpej 	/*
    133        1.6   thorpej 	 * Table index is stored in the media entry.
    134        1.6   thorpej 	 */
    135        1.6   thorpej 
    136        1.6   thorpej #ifdef DIAGNOSTIC
    137       1.50  christos 	if (/* ife->ifm_data < 0 || */ ife->ifm_data >= MII_NMEDIA)
    138        1.6   thorpej 		panic("mii_phy_setmedia");
    139        1.6   thorpej #endif
    140        1.6   thorpej 
    141        1.6   thorpej 	anar = mii_media_table[ife->ifm_data].mm_anar;
    142        1.6   thorpej 	bmcr = mii_media_table[ife->ifm_data].mm_bmcr;
    143       1.23   thorpej 	gtcr = mii_media_table[ife->ifm_data].mm_gtcr;
    144       1.23   thorpej 
    145       1.23   thorpej 	if (mii->mii_media.ifm_media & IFM_ETH_MASTER) {
    146       1.23   thorpej 		switch (IFM_SUBTYPE(ife->ifm_media)) {
    147       1.25     bjh21 		case IFM_1000_T:
    148       1.23   thorpej 			gtcr |= GTCR_MAN_MS|GTCR_ADV_MS;
    149       1.23   thorpej 			break;
    150       1.23   thorpej 
    151       1.23   thorpej 		default:
    152       1.23   thorpej 			panic("mii_phy_setmedia: MASTER on wrong media");
    153       1.23   thorpej 		}
    154       1.23   thorpej 	}
    155        1.6   thorpej 
    156       1.38   thorpej 	if (mii->mii_media.ifm_media & IFM_FLOW) {
    157       1.38   thorpej 		if (sc->mii_flags & MIIF_IS_1000X)
    158       1.38   thorpej 			anar |= ANAR_X_PAUSE_SYM | ANAR_X_PAUSE_ASYM;
    159       1.38   thorpej 		else {
    160       1.38   thorpej 			anar |= ANAR_FC;
    161       1.38   thorpej 			/* XXX Only 1000BASE-T has PAUSE_ASYM? */
    162       1.38   thorpej 			if ((sc->mii_flags & MIIF_HAVE_GTCR) &&
    163       1.38   thorpej 			    (sc->mii_extcapabilities &
    164       1.38   thorpej 			     (EXTSR_1000THDX|EXTSR_1000TFDX)))
    165       1.38   thorpej 				anar |= ANAR_X_PAUSE_ASYM;
    166       1.38   thorpej 		}
    167       1.38   thorpej 	}
    168       1.38   thorpej 
    169        1.6   thorpej 	if (ife->ifm_media & IFM_LOOP)
    170        1.6   thorpej 		bmcr |= BMCR_LOOP;
    171        1.6   thorpej 
    172        1.6   thorpej 	PHY_WRITE(sc, MII_ANAR, anar);
    173        1.6   thorpej 	PHY_WRITE(sc, MII_BMCR, bmcr);
    174       1.23   thorpej 	if (sc->mii_flags & MIIF_HAVE_GTCR)
    175       1.23   thorpej 		PHY_WRITE(sc, MII_100T2CR, gtcr);
    176       1.23   thorpej }
    177       1.23   thorpej 
    178        1.1   thorpej int
    179       1.29   thorpej mii_phy_auto(struct mii_softc *sc, int waitfor)
    180        1.1   thorpej {
    181       1.31       uwe 	int i;
    182        1.1   thorpej 
    183        1.6   thorpej 	if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
    184       1.26   thorpej 		/*
    185       1.26   thorpej 		 * Check for 1000BASE-X.  Autonegotiation is a bit
    186       1.26   thorpej 		 * different on such devices.
    187       1.26   thorpej 		 */
    188       1.27   thorpej 		if (sc->mii_flags & MIIF_IS_1000X) {
    189       1.27   thorpej 			uint16_t anar = 0;
    190       1.27   thorpej 
    191       1.27   thorpej 			if (sc->mii_extcapabilities & EXTSR_1000XFDX)
    192       1.27   thorpej 				anar |= ANAR_X_FD;
    193       1.27   thorpej 			if (sc->mii_extcapabilities & EXTSR_1000XHDX)
    194       1.27   thorpej 				anar |= ANAR_X_HD;
    195       1.27   thorpej 
    196       1.27   thorpej 			if (sc->mii_flags & MIIF_DOPAUSE) {
    197       1.27   thorpej 				/* XXX Asymmetric vs. symmetric? */
    198       1.27   thorpej 				anar |= ANLPAR_X_PAUSE_TOWARDS;
    199       1.27   thorpej 			}
    200       1.27   thorpej 
    201       1.27   thorpej 			PHY_WRITE(sc, MII_ANAR, anar);
    202       1.27   thorpej 		} else {
    203       1.27   thorpej 			uint16_t anar;
    204       1.27   thorpej 
    205       1.27   thorpej 			anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) |
    206       1.27   thorpej 			    ANAR_CSMA;
    207       1.38   thorpej 			if (sc->mii_flags & MIIF_DOPAUSE) {
    208       1.27   thorpej 				anar |= ANAR_FC;
    209       1.38   thorpej 				/* XXX Only 1000BASE-T has PAUSE_ASYM? */
    210       1.38   thorpej 				if ((sc->mii_flags & MIIF_HAVE_GTCR) &&
    211       1.38   thorpej 				    (sc->mii_extcapabilities &
    212       1.38   thorpej 				     (EXTSR_1000THDX|EXTSR_1000TFDX)))
    213       1.38   thorpej 					anar |= ANAR_X_PAUSE_ASYM;
    214       1.38   thorpej 			}
    215       1.27   thorpej 			PHY_WRITE(sc, MII_ANAR, anar);
    216       1.27   thorpej 			if (sc->mii_flags & MIIF_HAVE_GTCR) {
    217       1.27   thorpej 				uint16_t gtcr = 0;
    218       1.27   thorpej 
    219       1.27   thorpej 				if (sc->mii_extcapabilities & EXTSR_1000TFDX)
    220       1.27   thorpej 					gtcr |= GTCR_ADV_1000TFDX;
    221       1.27   thorpej 				if (sc->mii_extcapabilities & EXTSR_1000THDX)
    222       1.27   thorpej 					gtcr |= GTCR_ADV_1000THDX;
    223       1.26   thorpej 
    224       1.27   thorpej 				PHY_WRITE(sc, MII_100T2CR, gtcr);
    225       1.27   thorpej 			}
    226       1.26   thorpej 		}
    227        1.6   thorpej 		PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
    228        1.3   thorpej 	}
    229        1.3   thorpej 
    230        1.3   thorpej 	if (waitfor) {
    231        1.3   thorpej 		/* Wait 500ms for it to complete. */
    232        1.3   thorpej 		for (i = 0; i < 500; i++) {
    233       1.31       uwe 			if (PHY_READ(sc, MII_BMSR) & BMSR_ACOMP)
    234        1.3   thorpej 				return (0);
    235        1.3   thorpej 			delay(1000);
    236        1.3   thorpej 		}
    237        1.3   thorpej 
    238        1.3   thorpej 		/*
    239        1.3   thorpej 		 * Don't need to worry about clearing MIIF_DOINGAUTO.
    240        1.3   thorpej 		 * If that's set, a timeout is pending, and it will
    241        1.3   thorpej 		 * clear the flag.
    242        1.3   thorpej 		 */
    243        1.3   thorpej 		return (EIO);
    244        1.1   thorpej 	}
    245        1.3   thorpej 
    246        1.3   thorpej 	/*
    247        1.3   thorpej 	 * Just let it finish asynchronously.  This is for the benefit of
    248        1.3   thorpej 	 * the tick handler driving autonegotiation.  Don't want 500ms
    249        1.3   thorpej 	 * delays all the time while the system is running!
    250        1.3   thorpej 	 */
    251       1.19  augustss 	if (sc->mii_flags & MIIF_AUTOTSLEEP) {
    252       1.19  augustss 		sc->mii_flags |= MIIF_DOINGAUTO;
    253       1.19  augustss 		tsleep(&sc->mii_flags, PZERO, "miiaut", hz >> 1);
    254       1.19  augustss 		mii_phy_auto_timeout(sc);
    255       1.19  augustss 	} else if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
    256        1.6   thorpej 		sc->mii_flags |= MIIF_DOINGAUTO;
    257       1.17   thorpej 		callout_reset(&sc->mii_nway_ch, hz >> 1,
    258       1.17   thorpej 		    mii_phy_auto_timeout, sc);
    259        1.3   thorpej 	}
    260        1.3   thorpej 	return (EJUSTRETURN);
    261        1.3   thorpej }
    262        1.3   thorpej 
    263       1.43   thorpej static void
    264       1.29   thorpej mii_phy_auto_timeout(void *arg)
    265        1.3   thorpej {
    266        1.6   thorpej 	struct mii_softc *sc = arg;
    267       1.31       uwe 	int s;
    268        1.3   thorpej 
    269       1.47   thorpej 	if (!device_is_active(&sc->mii_dev))
    270        1.9   thorpej 		return;
    271        1.9   thorpej 
    272        1.3   thorpej 	s = splnet();
    273        1.6   thorpej 	sc->mii_flags &= ~MIIF_DOINGAUTO;
    274        1.3   thorpej 
    275        1.3   thorpej 	/* Update the media status. */
    276       1.18   thorpej 	(void) PHY_SERVICE(sc, sc->mii_pdata, MII_POLLSTAT);
    277        1.3   thorpej 	splx(s);
    278        1.2   thorpej }
    279        1.2   thorpej 
    280       1.15   thorpej int
    281       1.29   thorpej mii_phy_tick(struct mii_softc *sc)
    282       1.15   thorpej {
    283       1.15   thorpej 	struct mii_data *mii = sc->mii_pdata;
    284       1.15   thorpej 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    285       1.15   thorpej 	int reg;
    286       1.15   thorpej 
    287       1.15   thorpej 	/* Just bail now if the interface is down. */
    288       1.15   thorpej 	if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    289       1.15   thorpej 		return (EJUSTRETURN);
    290       1.15   thorpej 
    291       1.15   thorpej 	/*
    292       1.15   thorpej 	 * If we're not doing autonegotiation, we don't need to do
    293       1.15   thorpej 	 * any extra work here.  However, we need to check the link
    294       1.15   thorpej 	 * status so we can generate an announcement if the status
    295       1.15   thorpej 	 * changes.
    296       1.15   thorpej 	 */
    297       1.15   thorpej 	if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
    298       1.15   thorpej 		return (0);
    299       1.15   thorpej 
    300       1.15   thorpej 	/* Read the status register twice; BMSR_LINK is latch-low. */
    301       1.15   thorpej 	reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
    302       1.15   thorpej 	if (reg & BMSR_LINK) {
    303       1.15   thorpej 		/*
    304       1.15   thorpej 		 * See above.
    305       1.15   thorpej 		 */
    306       1.15   thorpej 		return (0);
    307       1.15   thorpej 	}
    308       1.15   thorpej 
    309       1.15   thorpej 	/*
    310       1.22   thorpej 	 * Only retry autonegotiation every N seconds.
    311       1.15   thorpej 	 */
    312       1.22   thorpej 	KASSERT(sc->mii_anegticks != 0);
    313       1.53  christos 	if (++sc->mii_ticks <= sc->mii_anegticks)
    314       1.15   thorpej 		return (EJUSTRETURN);
    315       1.15   thorpej 
    316       1.15   thorpej 	sc->mii_ticks = 0;
    317       1.18   thorpej 	PHY_RESET(sc);
    318       1.15   thorpej 
    319       1.15   thorpej 	if (mii_phy_auto(sc, 0) == EJUSTRETURN)
    320       1.15   thorpej 		return (EJUSTRETURN);
    321       1.15   thorpej 
    322       1.15   thorpej 	/*
    323       1.15   thorpej 	 * Might need to generate a status message if autonegotiation
    324       1.15   thorpej 	 * failed.
    325       1.15   thorpej 	 */
    326       1.15   thorpej 	return (0);
    327       1.15   thorpej }
    328       1.15   thorpej 
    329        1.2   thorpej void
    330       1.29   thorpej mii_phy_reset(struct mii_softc *sc)
    331        1.2   thorpej {
    332        1.2   thorpej 	int reg, i;
    333        1.2   thorpej 
    334        1.6   thorpej 	if (sc->mii_flags & MIIF_NOISOLATE)
    335        1.2   thorpej 		reg = BMCR_RESET;
    336        1.2   thorpej 	else
    337        1.2   thorpej 		reg = BMCR_RESET | BMCR_ISO;
    338        1.6   thorpej 	PHY_WRITE(sc, MII_BMCR, reg);
    339        1.2   thorpej 
    340       1.37    briggs 	/*
    341       1.37    briggs 	 * It is best to allow a little time for the reset to settle
    342       1.37    briggs 	 * in before we start polling the BMCR again.  Notably, the
    343       1.37    briggs 	 * DP83840A manual states that there should be a 500us delay
    344       1.37    briggs 	 * between asserting software reset and attempting MII serial
    345       1.37    briggs 	 * operations.  Also, a DP83815 can get into a bad state on
    346       1.37    briggs 	 * cable removal and reinsertion if we do not delay here.
    347       1.37    briggs 	 */
    348       1.37    briggs 	delay(500);
    349       1.37    briggs 
    350       1.37    briggs 	/* Wait another 100ms for it to complete. */
    351        1.2   thorpej 	for (i = 0; i < 100; i++) {
    352       1.44     perry 		reg = PHY_READ(sc, MII_BMCR);
    353        1.2   thorpej 		if ((reg & BMCR_RESET) == 0)
    354        1.2   thorpej 			break;
    355        1.2   thorpej 		delay(1000);
    356        1.2   thorpej 	}
    357        1.2   thorpej 
    358        1.6   thorpej 	if (sc->mii_inst != 0 && ((sc->mii_flags & MIIF_NOISOLATE) == 0))
    359        1.6   thorpej 		PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    360        1.8   thorpej }
    361        1.8   thorpej 
    362        1.8   thorpej void
    363       1.29   thorpej mii_phy_down(struct mii_softc *sc)
    364        1.8   thorpej {
    365        1.8   thorpej 
    366        1.8   thorpej 	if (sc->mii_flags & MIIF_DOINGAUTO) {
    367        1.8   thorpej 		sc->mii_flags &= ~MIIF_DOINGAUTO;
    368       1.17   thorpej 		callout_stop(&sc->mii_nway_ch);
    369        1.8   thorpej 	}
    370       1.11   thorpej }
    371       1.11   thorpej 
    372       1.11   thorpej void
    373       1.29   thorpej mii_phy_status(struct mii_softc *sc)
    374       1.11   thorpej {
    375       1.11   thorpej 
    376       1.18   thorpej 	PHY_STATUS(sc);
    377       1.15   thorpej }
    378       1.15   thorpej 
    379       1.15   thorpej void
    380       1.29   thorpej mii_phy_update(struct mii_softc *sc, int cmd)
    381       1.15   thorpej {
    382       1.15   thorpej 	struct mii_data *mii = sc->mii_pdata;
    383       1.15   thorpej 
    384       1.15   thorpej 	if (sc->mii_media_active != mii->mii_media_active ||
    385       1.15   thorpej 	    sc->mii_media_status != mii->mii_media_status ||
    386       1.15   thorpej 	    cmd == MII_MEDIACHG) {
    387       1.46     joerg 		mii_phy_statusmsg(sc);
    388       1.48   thorpej 		(*mii->mii_statchg)(device_parent(&sc->mii_dev));
    389       1.15   thorpej 		sc->mii_media_active = mii->mii_media_active;
    390       1.15   thorpej 		sc->mii_media_status = mii->mii_media_status;
    391       1.15   thorpej 	}
    392       1.15   thorpej }
    393       1.15   thorpej 
    394       1.46     joerg static void
    395       1.29   thorpej mii_phy_statusmsg(struct mii_softc *sc)
    396       1.15   thorpej {
    397       1.15   thorpej 	struct mii_data *mii = sc->mii_pdata;
    398       1.15   thorpej 	struct ifnet *ifp = mii->mii_ifp;
    399       1.46     joerg 	int s;
    400       1.15   thorpej 
    401       1.46     joerg 	s = splnet();
    402       1.15   thorpej 	if (mii->mii_media_status & IFM_AVALID) {
    403       1.15   thorpej 		if (mii->mii_media_status & IFM_ACTIVE)
    404       1.46     joerg 			if_link_state_change(ifp, LINK_STATE_UP);
    405       1.15   thorpej 		else
    406       1.46     joerg 			if_link_state_change(ifp, LINK_STATE_DOWN);
    407       1.15   thorpej 	} else
    408       1.46     joerg 		if_link_state_change(ifp, LINK_STATE_UNKNOWN);
    409       1.46     joerg 	splx(s);
    410       1.15   thorpej 
    411       1.46     joerg 	ifp->if_baudrate = ifmedia_baudrate(mii->mii_media_active);
    412        1.5  drochner }
    413        1.5  drochner 
    414        1.5  drochner /*
    415        1.5  drochner  * Initialize generic PHY media based on BMSR, called when a PHY is
    416        1.5  drochner  * attached.  We expect to be set up to print a comma-separated list
    417        1.5  drochner  * of media names.  Does not print a newline.
    418        1.5  drochner  */
    419        1.5  drochner void
    420       1.29   thorpej mii_phy_add_media(struct mii_softc *sc)
    421        1.5  drochner {
    422        1.6   thorpej 	struct mii_data *mii = sc->mii_pdata;
    423        1.5  drochner 	const char *sep = "";
    424       1.38   thorpej 	int fdx = 0;
    425        1.5  drochner 
    426        1.5  drochner #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
    427       1.36   thorpej #define	PRINT(n)	aprint_normal("%s%s", sep, (n)); sep = ", "
    428        1.5  drochner 
    429        1.6   thorpej 	if ((sc->mii_flags & MIIF_NOISOLATE) == 0)
    430        1.6   thorpej 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
    431        1.6   thorpej 		    MII_MEDIA_NONE);
    432       1.28   thorpej 
    433       1.28   thorpej 	/*
    434       1.28   thorpej 	 * There are different interpretations for the bits in
    435       1.28   thorpej 	 * HomePNA PHYs.  And there is really only one media type
    436       1.28   thorpej 	 * that is supported.
    437       1.28   thorpej 	 */
    438       1.28   thorpej 	if (sc->mii_flags & MIIF_IS_HPNA) {
    439       1.28   thorpej 		if (sc->mii_capabilities & BMSR_10THDX) {
    440       1.28   thorpej 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_HPNA_1, 0,
    441       1.28   thorpej 					 sc->mii_inst),
    442       1.28   thorpej 			    MII_MEDIA_10_T);
    443       1.28   thorpej 			PRINT("HomePNA1");
    444       1.28   thorpej 		}
    445       1.28   thorpej 		return;
    446       1.28   thorpej 	}
    447        1.6   thorpej 
    448        1.6   thorpej 	if (sc->mii_capabilities & BMSR_10THDX) {
    449        1.6   thorpej 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
    450        1.6   thorpej 		    MII_MEDIA_10_T);
    451        1.5  drochner 		PRINT("10baseT");
    452        1.5  drochner 	}
    453        1.6   thorpej 	if (sc->mii_capabilities & BMSR_10TFDX) {
    454        1.6   thorpej 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
    455        1.6   thorpej 		    MII_MEDIA_10_T_FDX);
    456        1.5  drochner 		PRINT("10baseT-FDX");
    457       1.38   thorpej 		fdx = 1;
    458        1.5  drochner 	}
    459        1.6   thorpej 	if (sc->mii_capabilities & BMSR_100TXHDX) {
    460        1.6   thorpej 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
    461        1.6   thorpej 		    MII_MEDIA_100_TX);
    462        1.5  drochner 		PRINT("100baseTX");
    463        1.5  drochner 	}
    464        1.6   thorpej 	if (sc->mii_capabilities & BMSR_100TXFDX) {
    465        1.6   thorpej 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
    466        1.6   thorpej 		    MII_MEDIA_100_TX_FDX);
    467        1.5  drochner 		PRINT("100baseTX-FDX");
    468       1.38   thorpej 		fdx = 1;
    469        1.5  drochner 	}
    470        1.6   thorpej 	if (sc->mii_capabilities & BMSR_100T4) {
    471        1.6   thorpej 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
    472        1.6   thorpej 		    MII_MEDIA_100_T4);
    473        1.5  drochner 		PRINT("100baseT4");
    474        1.5  drochner 	}
    475       1.21   thorpej 
    476       1.21   thorpej 	if (sc->mii_extcapabilities & EXTSR_MEDIAMASK) {
    477       1.21   thorpej 		/*
    478       1.21   thorpej 		 * XXX Right now only handle 1000SX and 1000TX.  Need
    479       1.22   thorpej 		 * XXX to handle 1000LX and 1000CX some how.
    480       1.22   thorpej 		 *
    481       1.22   thorpej 		 * Note since it can take 5 seconds to auto-negotiate
    482       1.22   thorpej 		 * a gigabit link, we make anegticks 10 seconds for
    483       1.22   thorpej 		 * all the gigabit media types.
    484       1.21   thorpej 		 */
    485       1.21   thorpej 		if (sc->mii_extcapabilities & EXTSR_1000XHDX) {
    486       1.53  christos 			sc->mii_anegticks = MII_ANEGTICKS_GIGE;
    487       1.27   thorpej 			sc->mii_flags |= MIIF_IS_1000X;
    488       1.21   thorpej 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0,
    489       1.23   thorpej 			    sc->mii_inst), MII_MEDIA_1000_X);
    490       1.21   thorpej 			PRINT("1000baseSX");
    491       1.21   thorpej 		}
    492       1.21   thorpej 		if (sc->mii_extcapabilities & EXTSR_1000XFDX) {
    493       1.53  christos 			sc->mii_anegticks = MII_ANEGTICKS_GIGE;
    494       1.27   thorpej 			sc->mii_flags |= MIIF_IS_1000X;
    495       1.21   thorpej 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX,
    496       1.23   thorpej 			    sc->mii_inst), MII_MEDIA_1000_X_FDX);
    497       1.21   thorpej 			PRINT("1000baseSX-FDX");
    498       1.38   thorpej 			fdx = 1;
    499       1.21   thorpej 		}
    500       1.23   thorpej 
    501       1.23   thorpej 		/*
    502       1.23   thorpej 		 * 1000baseT media needs to be able to manipulate
    503       1.23   thorpej 		 * master/slave mode.  We set IFM_ETH_MASTER in
    504       1.23   thorpej 		 * the "don't care mask" and filter it out when
    505       1.23   thorpej 		 * the media is set.
    506       1.23   thorpej 		 *
    507       1.23   thorpej 		 * All 1000baseT PHYs have a 1000baseT control register.
    508       1.23   thorpej 		 */
    509       1.21   thorpej 		if (sc->mii_extcapabilities & EXTSR_1000THDX) {
    510       1.53  christos 			sc->mii_anegticks = MII_ANEGTICKS_GIGE;
    511       1.23   thorpej 			sc->mii_flags |= MIIF_HAVE_GTCR;
    512       1.23   thorpej 			mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
    513       1.25     bjh21 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 0,
    514       1.23   thorpej 			    sc->mii_inst), MII_MEDIA_1000_T);
    515       1.25     bjh21 			PRINT("1000baseT");
    516       1.21   thorpej 		}
    517       1.21   thorpej 		if (sc->mii_extcapabilities & EXTSR_1000TFDX) {
    518       1.53  christos 			sc->mii_anegticks = MII_ANEGTICKS_GIGE;
    519       1.23   thorpej 			sc->mii_flags |= MIIF_HAVE_GTCR;
    520       1.23   thorpej 			mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
    521       1.25     bjh21 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX,
    522       1.23   thorpej 			    sc->mii_inst), MII_MEDIA_1000_T_FDX);
    523       1.25     bjh21 			PRINT("1000baseT-FDX");
    524       1.38   thorpej 			fdx = 1;
    525       1.21   thorpej 		}
    526       1.21   thorpej 	}
    527       1.21   thorpej 
    528        1.6   thorpej 	if (sc->mii_capabilities & BMSR_ANEG) {
    529        1.6   thorpej 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
    530        1.6   thorpej 		    MII_NMEDIA);	/* intentionally invalid index */
    531        1.5  drochner 		PRINT("auto");
    532        1.5  drochner 	}
    533        1.5  drochner #undef ADD
    534        1.5  drochner #undef PRINT
    535       1.38   thorpej 	if (fdx != 0 && (sc->mii_flags & MIIF_DOPAUSE))
    536       1.38   thorpej 		mii->mii_media.ifm_mask |= IFM_ETH_FMASK;
    537        1.9   thorpej }
    538        1.9   thorpej 
    539        1.9   thorpej void
    540       1.29   thorpej mii_phy_delete_media(struct mii_softc *sc)
    541        1.9   thorpej {
    542        1.9   thorpej 	struct mii_data *mii = sc->mii_pdata;
    543        1.9   thorpej 
    544        1.9   thorpej 	ifmedia_delete_instance(&mii->mii_media, sc->mii_inst);
    545        1.9   thorpej }
    546        1.9   thorpej 
    547        1.9   thorpej int
    548       1.52  christos mii_phy_activate(struct device *self, enum devact act)
    549        1.9   thorpej {
    550        1.9   thorpej 	int rv = 0;
    551        1.9   thorpej 
    552        1.9   thorpej 	switch (act) {
    553        1.9   thorpej 	case DVACT_ACTIVATE:
    554        1.9   thorpej 		rv = EOPNOTSUPP;
    555        1.9   thorpej 		break;
    556        1.9   thorpej 
    557        1.9   thorpej 	case DVACT_DEACTIVATE:
    558        1.9   thorpej 		/* Nothing special to do. */
    559        1.9   thorpej 		break;
    560        1.9   thorpej 	}
    561        1.9   thorpej 
    562        1.9   thorpej 	return (rv);
    563        1.9   thorpej }
    564        1.9   thorpej 
    565       1.31       uwe /* ARGSUSED1 */
    566        1.9   thorpej int
    567       1.52  christos mii_phy_detach(struct device *self, int flags)
    568        1.9   thorpej {
    569       1.49   thorpej 	struct mii_softc *sc = device_private(self);
    570        1.9   thorpej 
    571        1.9   thorpej 	if (sc->mii_flags & MIIF_DOINGAUTO)
    572       1.17   thorpej 		callout_stop(&sc->mii_nway_ch);
    573        1.9   thorpej 
    574       1.10   thorpej 	mii_phy_delete_media(sc);
    575        1.9   thorpej 
    576        1.9   thorpej 	return (0);
    577       1.24   thorpej }
    578       1.24   thorpej 
    579       1.24   thorpej const struct mii_phydesc *
    580       1.24   thorpej mii_phy_match(const struct mii_attach_args *ma, const struct mii_phydesc *mpd)
    581       1.24   thorpej {
    582       1.24   thorpej 
    583       1.24   thorpej 	for (; mpd->mpd_name != NULL; mpd++) {
    584       1.24   thorpej 		if (MII_OUI(ma->mii_id1, ma->mii_id2) == mpd->mpd_oui &&
    585       1.24   thorpej 		    MII_MODEL(ma->mii_id2) == mpd->mpd_model)
    586       1.24   thorpej 			return (mpd);
    587       1.24   thorpej 	}
    588       1.24   thorpej 	return (NULL);
    589        1.1   thorpej }
    590       1.38   thorpej 
    591       1.38   thorpej /*
    592       1.38   thorpej  * Return the flow control status flag from MII_ANAR & MII_ANLPAR.
    593       1.38   thorpej  */
    594       1.38   thorpej u_int
    595       1.40   thorpej mii_phy_flowstatus(struct mii_softc *sc)
    596       1.38   thorpej {
    597       1.38   thorpej 	u_int anar, anlpar;
    598       1.38   thorpej 
    599       1.39   thorpej 	if ((sc->mii_flags & MIIF_DOPAUSE) == 0)
    600       1.39   thorpej 		return (0);
    601       1.39   thorpej 
    602       1.38   thorpej 	anar = PHY_READ(sc, MII_ANAR);
    603       1.38   thorpej 	anlpar = PHY_READ(sc, MII_ANLPAR);
    604       1.38   thorpej 
    605       1.42   thorpej 	if ((anar & ANAR_X_PAUSE_SYM) & (anlpar & ANLPAR_X_PAUSE_SYM))
    606       1.42   thorpej 		return (IFM_FLOW|IFM_ETH_TXPAUSE|IFM_ETH_RXPAUSE);
    607       1.42   thorpej 
    608       1.38   thorpej 	if ((anar & ANAR_X_PAUSE_SYM) == 0) {
    609       1.38   thorpej 		if ((anar & ANAR_X_PAUSE_ASYM) &&
    610       1.38   thorpej 		    ((anlpar &
    611       1.38   thorpej 		      ANLPAR_X_PAUSE_TOWARDS) == ANLPAR_X_PAUSE_TOWARDS))
    612       1.38   thorpej 			return (IFM_FLOW|IFM_ETH_TXPAUSE);
    613       1.38   thorpej 		else
    614       1.38   thorpej 			return (0);
    615       1.38   thorpej 	}
    616       1.38   thorpej 
    617       1.38   thorpej 	if ((anar & ANAR_X_PAUSE_ASYM) == 0) {
    618       1.38   thorpej 		if (anlpar & ANLPAR_X_PAUSE_SYM)
    619       1.38   thorpej 			return (IFM_FLOW|IFM_ETH_TXPAUSE|IFM_ETH_RXPAUSE);
    620       1.38   thorpej 		else
    621       1.38   thorpej 			return (0);
    622       1.38   thorpej 	}
    623       1.38   thorpej 
    624       1.38   thorpej 	switch ((anlpar & ANLPAR_X_PAUSE_TOWARDS)) {
    625       1.38   thorpej 	case ANLPAR_X_PAUSE_NONE:
    626       1.38   thorpej 		return (0);
    627       1.44     perry 
    628       1.38   thorpej 	case ANLPAR_X_PAUSE_ASYM:
    629       1.38   thorpej 		return (IFM_FLOW|IFM_ETH_RXPAUSE);
    630       1.44     perry 
    631       1.38   thorpej 	default:
    632       1.38   thorpej 		return (IFM_FLOW|IFM_ETH_RXPAUSE|IFM_ETH_TXPAUSE);
    633       1.38   thorpej 	}
    634       1.38   thorpej 	/* NOTREACHED */
    635       1.38   thorpej }
    636  1.53.24.1      matt 
    637  1.53.24.1      matt bool
    638  1.53.24.1      matt mii_phy_resume(device_t dv)
    639  1.53.24.1      matt {
    640  1.53.24.1      matt 	struct mii_softc *sc = device_private(dv);
    641  1.53.24.1      matt 
    642  1.53.24.1      matt 	PHY_RESET(sc);
    643  1.53.24.1      matt 	return PHY_SERVICE(sc, sc->mii_pdata, MII_MEDIACHG) == 0;
    644  1.53.24.1      matt }
    645