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