Home | History | Annotate | Line # | Download | only in mii
mii_physubr.c revision 1.18.2.2
      1  1.18.2.2   nathanw /*	$NetBSD: mii_physubr.c,v 1.18.2.2 2001/08/24 00:09:59 nathanw Exp $	*/
      2       1.1   thorpej 
      3       1.1   thorpej /*-
      4  1.18.2.1   nathanw  * 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.1   thorpej 
     44       1.1   thorpej #include <sys/param.h>
     45       1.1   thorpej #include <sys/device.h>
     46       1.1   thorpej #include <sys/systm.h>
     47       1.3   thorpej #include <sys/kernel.h>
     48       1.1   thorpej #include <sys/socket.h>
     49       1.3   thorpej #include <sys/errno.h>
     50  1.18.2.1   nathanw #include <sys/proc.h>
     51       1.1   thorpej 
     52       1.1   thorpej #include <net/if.h>
     53       1.1   thorpej #include <net/if_media.h>
     54      1.15   thorpej #include <net/route.h>
     55       1.1   thorpej 
     56       1.1   thorpej #include <dev/mii/mii.h>
     57       1.1   thorpej #include <dev/mii/miivar.h>
     58       1.1   thorpej 
     59       1.6   thorpej /*
     60       1.6   thorpej  * Media to register setting conversion table.  Order matters.
     61       1.6   thorpej  */
     62  1.18.2.1   nathanw const struct mii_media mii_media_table[MII_NMEDIA] = {
     63  1.18.2.1   nathanw 	/* None */
     64  1.18.2.1   nathanw 	{ BMCR_ISO,		ANAR_CSMA,
     65  1.18.2.1   nathanw 	  0, },
     66  1.18.2.1   nathanw 
     67  1.18.2.1   nathanw 	/* 10baseT */
     68  1.18.2.1   nathanw 	{ BMCR_S10,		ANAR_CSMA|ANAR_10,
     69  1.18.2.1   nathanw 	  0, },
     70  1.18.2.1   nathanw 
     71  1.18.2.1   nathanw 	/* 10baseT-FDX */
     72  1.18.2.1   nathanw 	{ BMCR_S10|BMCR_FDX,	ANAR_CSMA|ANAR_10_FD,
     73  1.18.2.1   nathanw 	  0, },
     74  1.18.2.1   nathanw 
     75  1.18.2.1   nathanw 	/* 100baseT4 */
     76  1.18.2.1   nathanw 	{ BMCR_S100,		ANAR_CSMA|ANAR_T4,
     77  1.18.2.1   nathanw 	  0, },
     78  1.18.2.1   nathanw 
     79  1.18.2.1   nathanw 	/* 100baseTX */
     80  1.18.2.1   nathanw 	{ BMCR_S100,		ANAR_CSMA|ANAR_TX,
     81  1.18.2.1   nathanw 	  0, },
     82  1.18.2.1   nathanw 
     83  1.18.2.1   nathanw 	/* 100baseTX-FDX */
     84  1.18.2.1   nathanw 	{ BMCR_S100|BMCR_FDX,	ANAR_CSMA|ANAR_TX_FD,
     85  1.18.2.1   nathanw 	  0, },
     86  1.18.2.1   nathanw 
     87  1.18.2.1   nathanw 	/* 1000baseX */
     88  1.18.2.1   nathanw 	{ BMCR_S1000,		ANAR_CSMA,
     89  1.18.2.1   nathanw 	  0, },
     90  1.18.2.1   nathanw 
     91  1.18.2.1   nathanw 	/* 1000baseX-FDX */
     92  1.18.2.1   nathanw 	{ BMCR_S1000|BMCR_FDX,	ANAR_CSMA,
     93  1.18.2.1   nathanw 	  0, },
     94  1.18.2.1   nathanw 
     95  1.18.2.1   nathanw 	/* 1000baseT */
     96  1.18.2.1   nathanw 	{ BMCR_S1000,		ANAR_CSMA,
     97  1.18.2.1   nathanw 	  GTCR_ADV_1000THDX },
     98  1.18.2.1   nathanw 
     99  1.18.2.1   nathanw 	/* 1000baseT-FDX */
    100  1.18.2.1   nathanw 	{ BMCR_S1000,		ANAR_CSMA,
    101  1.18.2.1   nathanw 	  GTCR_ADV_1000TFDX },
    102       1.6   thorpej };
    103       1.6   thorpej 
    104       1.3   thorpej void	mii_phy_auto_timeout __P((void *));
    105       1.3   thorpej 
    106       1.6   thorpej void
    107       1.6   thorpej mii_phy_setmedia(sc)
    108       1.6   thorpej 	struct mii_softc *sc;
    109       1.6   thorpej {
    110       1.6   thorpej 	struct mii_data *mii = sc->mii_pdata;
    111       1.6   thorpej 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    112  1.18.2.1   nathanw 	int bmcr, anar, gtcr;
    113      1.12   thorpej 
    114      1.13   thorpej 	if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO) {
    115      1.14   thorpej 		if ((PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) == 0)
    116      1.14   thorpej 			(void) mii_phy_auto(sc, 1);
    117      1.13   thorpej 		return;
    118      1.13   thorpej 	}
    119       1.6   thorpej 
    120       1.6   thorpej 	/*
    121       1.6   thorpej 	 * Table index is stored in the media entry.
    122       1.6   thorpej 	 */
    123       1.6   thorpej 
    124       1.6   thorpej #ifdef DIAGNOSTIC
    125       1.6   thorpej 	if (ife->ifm_data < 0 || ife->ifm_data >= MII_NMEDIA)
    126       1.6   thorpej 		panic("mii_phy_setmedia");
    127       1.6   thorpej #endif
    128       1.6   thorpej 
    129       1.6   thorpej 	anar = mii_media_table[ife->ifm_data].mm_anar;
    130       1.6   thorpej 	bmcr = mii_media_table[ife->ifm_data].mm_bmcr;
    131  1.18.2.1   nathanw 	gtcr = mii_media_table[ife->ifm_data].mm_gtcr;
    132  1.18.2.1   nathanw 
    133  1.18.2.1   nathanw 	if (mii->mii_media.ifm_media & IFM_ETH_MASTER) {
    134  1.18.2.1   nathanw 		switch (IFM_SUBTYPE(ife->ifm_media)) {
    135  1.18.2.2   nathanw 		case IFM_1000_T:
    136  1.18.2.1   nathanw 			gtcr |= GTCR_MAN_MS|GTCR_ADV_MS;
    137  1.18.2.1   nathanw 			break;
    138  1.18.2.1   nathanw 
    139  1.18.2.1   nathanw 		default:
    140  1.18.2.1   nathanw 			panic("mii_phy_setmedia: MASTER on wrong media");
    141  1.18.2.1   nathanw 		}
    142  1.18.2.1   nathanw 	}
    143       1.6   thorpej 
    144       1.6   thorpej 	if (ife->ifm_media & IFM_LOOP)
    145       1.6   thorpej 		bmcr |= BMCR_LOOP;
    146       1.6   thorpej 
    147       1.6   thorpej 	PHY_WRITE(sc, MII_ANAR, anar);
    148       1.6   thorpej 	PHY_WRITE(sc, MII_BMCR, bmcr);
    149  1.18.2.1   nathanw 	if (sc->mii_flags & MIIF_HAVE_GTCR)
    150  1.18.2.1   nathanw 		PHY_WRITE(sc, MII_100T2CR, gtcr);
    151  1.18.2.1   nathanw }
    152  1.18.2.1   nathanw 
    153       1.1   thorpej int
    154       1.6   thorpej mii_phy_auto(sc, waitfor)
    155       1.6   thorpej 	struct mii_softc *sc;
    156       1.4    kleink 	int waitfor;
    157       1.1   thorpej {
    158       1.1   thorpej 	int bmsr, i;
    159       1.1   thorpej 
    160       1.6   thorpej 	if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
    161  1.18.2.2   nathanw 		/*
    162  1.18.2.2   nathanw 		 * Check for 1000BASE-X.  Autonegotiation is a bit
    163  1.18.2.2   nathanw 		 * different on such devices.
    164  1.18.2.2   nathanw 		 */
    165  1.18.2.2   nathanw 		if (sc->mii_flags & MIIF_IS_1000X) {
    166  1.18.2.2   nathanw 			uint16_t anar = 0;
    167  1.18.2.2   nathanw 
    168  1.18.2.2   nathanw 			if (sc->mii_extcapabilities & EXTSR_1000XFDX)
    169  1.18.2.2   nathanw 				anar |= ANAR_X_FD;
    170  1.18.2.2   nathanw 			if (sc->mii_extcapabilities & EXTSR_1000XHDX)
    171  1.18.2.2   nathanw 				anar |= ANAR_X_HD;
    172  1.18.2.2   nathanw 
    173  1.18.2.2   nathanw 			if (sc->mii_flags & MIIF_DOPAUSE) {
    174  1.18.2.2   nathanw 				/* XXX Asymmetric vs. symmetric? */
    175  1.18.2.2   nathanw 				anar |= ANLPAR_X_PAUSE_TOWARDS;
    176  1.18.2.2   nathanw 			}
    177  1.18.2.2   nathanw 
    178  1.18.2.2   nathanw 			PHY_WRITE(sc, MII_ANAR, anar);
    179  1.18.2.2   nathanw 		} else {
    180  1.18.2.2   nathanw 			uint16_t anar;
    181  1.18.2.2   nathanw 
    182  1.18.2.2   nathanw 			anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) |
    183  1.18.2.2   nathanw 			    ANAR_CSMA;
    184  1.18.2.2   nathanw 			if (sc->mii_flags & MIIF_DOPAUSE)
    185  1.18.2.2   nathanw 				anar |= ANAR_FC;
    186  1.18.2.2   nathanw 			PHY_WRITE(sc, MII_ANAR, anar);
    187  1.18.2.2   nathanw 			if (sc->mii_flags & MIIF_HAVE_GTCR) {
    188  1.18.2.2   nathanw 				uint16_t gtcr = 0;
    189  1.18.2.2   nathanw 
    190  1.18.2.2   nathanw 				if (sc->mii_extcapabilities & EXTSR_1000TFDX)
    191  1.18.2.2   nathanw 					gtcr |= GTCR_ADV_1000TFDX;
    192  1.18.2.2   nathanw 				if (sc->mii_extcapabilities & EXTSR_1000THDX)
    193  1.18.2.2   nathanw 					gtcr |= GTCR_ADV_1000THDX;
    194  1.18.2.2   nathanw 
    195  1.18.2.2   nathanw 				PHY_WRITE(sc, MII_100T2CR, gtcr);
    196  1.18.2.2   nathanw 			}
    197  1.18.2.2   nathanw 		}
    198       1.6   thorpej 		PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
    199       1.3   thorpej 	}
    200       1.3   thorpej 
    201       1.3   thorpej 	if (waitfor) {
    202       1.3   thorpej 		/* Wait 500ms for it to complete. */
    203       1.3   thorpej 		for (i = 0; i < 500; i++) {
    204       1.6   thorpej 			if ((bmsr = PHY_READ(sc, MII_BMSR)) & BMSR_ACOMP)
    205       1.3   thorpej 				return (0);
    206       1.3   thorpej 			delay(1000);
    207       1.3   thorpej 		}
    208       1.3   thorpej 
    209       1.3   thorpej 		/*
    210       1.3   thorpej 		 * Don't need to worry about clearing MIIF_DOINGAUTO.
    211       1.3   thorpej 		 * If that's set, a timeout is pending, and it will
    212       1.3   thorpej 		 * clear the flag.
    213       1.3   thorpej 		 */
    214       1.3   thorpej 		return (EIO);
    215       1.1   thorpej 	}
    216       1.3   thorpej 
    217       1.3   thorpej 	/*
    218       1.3   thorpej 	 * Just let it finish asynchronously.  This is for the benefit of
    219       1.3   thorpej 	 * the tick handler driving autonegotiation.  Don't want 500ms
    220       1.3   thorpej 	 * delays all the time while the system is running!
    221       1.3   thorpej 	 */
    222  1.18.2.1   nathanw 	if (sc->mii_flags & MIIF_AUTOTSLEEP) {
    223  1.18.2.1   nathanw 		sc->mii_flags |= MIIF_DOINGAUTO;
    224  1.18.2.1   nathanw 		tsleep(&sc->mii_flags, PZERO, "miiaut", hz >> 1);
    225  1.18.2.1   nathanw 		mii_phy_auto_timeout(sc);
    226  1.18.2.1   nathanw 	} else if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
    227       1.6   thorpej 		sc->mii_flags |= MIIF_DOINGAUTO;
    228      1.17   thorpej 		callout_reset(&sc->mii_nway_ch, hz >> 1,
    229      1.17   thorpej 		    mii_phy_auto_timeout, sc);
    230       1.3   thorpej 	}
    231       1.3   thorpej 	return (EJUSTRETURN);
    232       1.3   thorpej }
    233       1.3   thorpej 
    234       1.3   thorpej void
    235       1.3   thorpej mii_phy_auto_timeout(arg)
    236       1.3   thorpej 	void *arg;
    237       1.3   thorpej {
    238       1.6   thorpej 	struct mii_softc *sc = arg;
    239       1.3   thorpej 	int s, bmsr;
    240       1.3   thorpej 
    241       1.9   thorpej 	if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0)
    242       1.9   thorpej 		return;
    243       1.9   thorpej 
    244       1.3   thorpej 	s = splnet();
    245       1.6   thorpej 	sc->mii_flags &= ~MIIF_DOINGAUTO;
    246       1.6   thorpej 	bmsr = PHY_READ(sc, MII_BMSR);
    247       1.3   thorpej 
    248       1.3   thorpej 	/* Update the media status. */
    249      1.18   thorpej 	(void) PHY_SERVICE(sc, sc->mii_pdata, MII_POLLSTAT);
    250       1.3   thorpej 	splx(s);
    251       1.2   thorpej }
    252       1.2   thorpej 
    253      1.15   thorpej int
    254      1.15   thorpej mii_phy_tick(sc)
    255      1.15   thorpej 	struct mii_softc *sc;
    256      1.15   thorpej {
    257      1.15   thorpej 	struct mii_data *mii = sc->mii_pdata;
    258      1.15   thorpej 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    259      1.15   thorpej 	int reg;
    260      1.15   thorpej 
    261      1.15   thorpej 	/* Just bail now if the interface is down. */
    262      1.15   thorpej 	if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    263      1.15   thorpej 		return (EJUSTRETURN);
    264      1.15   thorpej 
    265      1.15   thorpej 	/*
    266      1.15   thorpej 	 * If we're not doing autonegotiation, we don't need to do
    267      1.15   thorpej 	 * any extra work here.  However, we need to check the link
    268      1.15   thorpej 	 * status so we can generate an announcement if the status
    269      1.15   thorpej 	 * changes.
    270      1.15   thorpej 	 */
    271      1.15   thorpej 	if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
    272      1.15   thorpej 		return (0);
    273      1.15   thorpej 
    274      1.15   thorpej 	/* Read the status register twice; BMSR_LINK is latch-low. */
    275      1.15   thorpej 	reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
    276      1.15   thorpej 	if (reg & BMSR_LINK) {
    277      1.15   thorpej 		/*
    278      1.15   thorpej 		 * See above.
    279      1.15   thorpej 		 */
    280      1.15   thorpej 		return (0);
    281      1.15   thorpej 	}
    282      1.15   thorpej 
    283      1.15   thorpej 	/*
    284  1.18.2.1   nathanw 	 * Only retry autonegotiation every N seconds.
    285      1.15   thorpej 	 */
    286  1.18.2.1   nathanw 	KASSERT(sc->mii_anegticks != 0);
    287  1.18.2.1   nathanw 	if (++sc->mii_ticks != sc->mii_anegticks)
    288      1.15   thorpej 		return (EJUSTRETURN);
    289      1.15   thorpej 
    290      1.15   thorpej 	sc->mii_ticks = 0;
    291      1.18   thorpej 	PHY_RESET(sc);
    292      1.15   thorpej 
    293      1.15   thorpej 	if (mii_phy_auto(sc, 0) == EJUSTRETURN)
    294      1.15   thorpej 		return (EJUSTRETURN);
    295      1.15   thorpej 
    296      1.15   thorpej 	/*
    297      1.15   thorpej 	 * Might need to generate a status message if autonegotiation
    298      1.15   thorpej 	 * failed.
    299      1.15   thorpej 	 */
    300      1.15   thorpej 	return (0);
    301      1.15   thorpej }
    302      1.15   thorpej 
    303       1.2   thorpej void
    304       1.6   thorpej mii_phy_reset(sc)
    305       1.6   thorpej 	struct mii_softc *sc;
    306       1.2   thorpej {
    307       1.2   thorpej 	int reg, i;
    308       1.2   thorpej 
    309       1.6   thorpej 	if (sc->mii_flags & MIIF_NOISOLATE)
    310       1.2   thorpej 		reg = BMCR_RESET;
    311       1.2   thorpej 	else
    312       1.2   thorpej 		reg = BMCR_RESET | BMCR_ISO;
    313       1.6   thorpej 	PHY_WRITE(sc, MII_BMCR, reg);
    314       1.2   thorpej 
    315       1.2   thorpej 	/* Wait 100ms for it to complete. */
    316       1.2   thorpej 	for (i = 0; i < 100; i++) {
    317       1.6   thorpej 		reg = PHY_READ(sc, MII_BMCR);
    318       1.2   thorpej 		if ((reg & BMCR_RESET) == 0)
    319       1.2   thorpej 			break;
    320       1.2   thorpej 		delay(1000);
    321       1.2   thorpej 	}
    322       1.2   thorpej 
    323       1.6   thorpej 	if (sc->mii_inst != 0 && ((sc->mii_flags & MIIF_NOISOLATE) == 0))
    324       1.6   thorpej 		PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    325       1.8   thorpej }
    326       1.8   thorpej 
    327       1.8   thorpej void
    328       1.8   thorpej mii_phy_down(sc)
    329       1.8   thorpej 	struct mii_softc *sc;
    330       1.8   thorpej {
    331       1.8   thorpej 
    332       1.8   thorpej 	if (sc->mii_flags & MIIF_DOINGAUTO) {
    333       1.8   thorpej 		sc->mii_flags &= ~MIIF_DOINGAUTO;
    334      1.17   thorpej 		callout_stop(&sc->mii_nway_ch);
    335       1.8   thorpej 	}
    336      1.11   thorpej }
    337      1.11   thorpej 
    338      1.11   thorpej void
    339      1.11   thorpej mii_phy_status(sc)
    340      1.11   thorpej 	struct mii_softc *sc;
    341      1.11   thorpej {
    342      1.11   thorpej 
    343      1.18   thorpej 	PHY_STATUS(sc);
    344      1.15   thorpej }
    345      1.15   thorpej 
    346      1.15   thorpej void
    347      1.15   thorpej mii_phy_update(sc, cmd)
    348      1.15   thorpej 	struct mii_softc *sc;
    349      1.15   thorpej 	int cmd;
    350      1.15   thorpej {
    351      1.15   thorpej 	struct mii_data *mii = sc->mii_pdata;
    352      1.15   thorpej 
    353      1.15   thorpej 	if (sc->mii_media_active != mii->mii_media_active ||
    354      1.15   thorpej 	    sc->mii_media_status != mii->mii_media_status ||
    355      1.15   thorpej 	    cmd == MII_MEDIACHG) {
    356      1.15   thorpej 		(*mii->mii_statchg)(sc->mii_dev.dv_parent);
    357      1.15   thorpej 		mii_phy_statusmsg(sc);
    358      1.15   thorpej 		sc->mii_media_active = mii->mii_media_active;
    359      1.15   thorpej 		sc->mii_media_status = mii->mii_media_status;
    360      1.15   thorpej 	}
    361      1.15   thorpej }
    362      1.15   thorpej 
    363      1.15   thorpej void
    364      1.15   thorpej mii_phy_statusmsg(sc)
    365      1.15   thorpej 	struct mii_softc *sc;
    366      1.15   thorpej {
    367      1.15   thorpej 	struct mii_data *mii = sc->mii_pdata;
    368      1.15   thorpej 	struct ifnet *ifp = mii->mii_ifp;
    369      1.16   thorpej 	int s, baudrate, link_state, announce = 0;
    370      1.15   thorpej 
    371      1.15   thorpej 	if (mii->mii_media_status & IFM_AVALID) {
    372      1.15   thorpej 		if (mii->mii_media_status & IFM_ACTIVE)
    373      1.15   thorpej 			link_state = LINK_STATE_UP;
    374      1.15   thorpej 		else
    375      1.15   thorpej 			link_state = LINK_STATE_DOWN;
    376      1.15   thorpej 	} else
    377      1.15   thorpej 		link_state = LINK_STATE_UNKNOWN;
    378      1.15   thorpej 
    379      1.15   thorpej 	baudrate = ifmedia_baudrate(mii->mii_media_active);
    380      1.15   thorpej 
    381      1.15   thorpej 	if (link_state != ifp->if_link_state) {
    382      1.15   thorpej 		ifp->if_link_state = link_state;
    383      1.16   thorpej 		/*
    384      1.16   thorpej 		 * XXX Right here we'd like to notify protocols
    385      1.16   thorpej 		 * XXX that the link status has changed, so that
    386      1.16   thorpej 		 * XXX e.g. Duplicate Address Detection can restart.
    387      1.16   thorpej 		 */
    388      1.15   thorpej 		announce = 1;
    389      1.15   thorpej 	}
    390      1.15   thorpej 
    391      1.15   thorpej 	if (baudrate != ifp->if_baudrate) {
    392      1.15   thorpej 		ifp->if_baudrate = baudrate;
    393      1.15   thorpej 		announce = 1;
    394      1.15   thorpej 	}
    395      1.15   thorpej 
    396      1.16   thorpej 	if (announce) {
    397  1.18.2.1   nathanw 		s = splnet();
    398      1.15   thorpej 		rt_ifmsg(ifp);
    399      1.16   thorpej 		splx(s);
    400      1.16   thorpej 	}
    401       1.5  drochner }
    402       1.5  drochner 
    403       1.5  drochner /*
    404       1.5  drochner  * Initialize generic PHY media based on BMSR, called when a PHY is
    405       1.5  drochner  * attached.  We expect to be set up to print a comma-separated list
    406       1.5  drochner  * of media names.  Does not print a newline.
    407       1.5  drochner  */
    408       1.5  drochner void
    409      1.10   thorpej mii_phy_add_media(sc)
    410       1.6   thorpej 	struct mii_softc *sc;
    411       1.5  drochner {
    412       1.6   thorpej 	struct mii_data *mii = sc->mii_pdata;
    413       1.5  drochner 	const char *sep = "";
    414       1.5  drochner 
    415       1.5  drochner #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
    416       1.5  drochner #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
    417       1.5  drochner 
    418       1.6   thorpej 	if ((sc->mii_flags & MIIF_NOISOLATE) == 0)
    419       1.6   thorpej 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
    420       1.6   thorpej 		    MII_MEDIA_NONE);
    421       1.6   thorpej 
    422       1.6   thorpej 	if (sc->mii_capabilities & BMSR_10THDX) {
    423       1.6   thorpej 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
    424       1.6   thorpej 		    MII_MEDIA_10_T);
    425       1.5  drochner 		PRINT("10baseT");
    426       1.5  drochner 	}
    427       1.6   thorpej 	if (sc->mii_capabilities & BMSR_10TFDX) {
    428       1.6   thorpej 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
    429       1.6   thorpej 		    MII_MEDIA_10_T_FDX);
    430       1.5  drochner 		PRINT("10baseT-FDX");
    431       1.5  drochner 	}
    432       1.6   thorpej 	if (sc->mii_capabilities & BMSR_100TXHDX) {
    433       1.6   thorpej 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
    434       1.6   thorpej 		    MII_MEDIA_100_TX);
    435       1.5  drochner 		PRINT("100baseTX");
    436       1.5  drochner 	}
    437       1.6   thorpej 	if (sc->mii_capabilities & BMSR_100TXFDX) {
    438       1.6   thorpej 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
    439       1.6   thorpej 		    MII_MEDIA_100_TX_FDX);
    440       1.5  drochner 		PRINT("100baseTX-FDX");
    441       1.5  drochner 	}
    442       1.6   thorpej 	if (sc->mii_capabilities & BMSR_100T4) {
    443       1.6   thorpej 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
    444       1.6   thorpej 		    MII_MEDIA_100_T4);
    445       1.5  drochner 		PRINT("100baseT4");
    446       1.5  drochner 	}
    447  1.18.2.1   nathanw 
    448  1.18.2.1   nathanw 	if (sc->mii_extcapabilities & EXTSR_MEDIAMASK) {
    449  1.18.2.1   nathanw 		/*
    450  1.18.2.1   nathanw 		 * XXX Right now only handle 1000SX and 1000TX.  Need
    451  1.18.2.1   nathanw 		 * XXX to handle 1000LX and 1000CX some how.
    452  1.18.2.1   nathanw 		 *
    453  1.18.2.1   nathanw 		 * Note since it can take 5 seconds to auto-negotiate
    454  1.18.2.1   nathanw 		 * a gigabit link, we make anegticks 10 seconds for
    455  1.18.2.1   nathanw 		 * all the gigabit media types.
    456  1.18.2.1   nathanw 		 */
    457  1.18.2.1   nathanw 		if (sc->mii_extcapabilities & EXTSR_1000XHDX) {
    458  1.18.2.1   nathanw 			sc->mii_anegticks = 10;
    459  1.18.2.2   nathanw 			sc->mii_flags |= MIIF_IS_1000X;
    460  1.18.2.1   nathanw 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0,
    461  1.18.2.1   nathanw 			    sc->mii_inst), MII_MEDIA_1000_X);
    462  1.18.2.1   nathanw 			PRINT("1000baseSX");
    463  1.18.2.1   nathanw 		}
    464  1.18.2.1   nathanw 		if (sc->mii_extcapabilities & EXTSR_1000XFDX) {
    465  1.18.2.1   nathanw 			sc->mii_anegticks = 10;
    466  1.18.2.2   nathanw 			sc->mii_flags |= MIIF_IS_1000X;
    467  1.18.2.1   nathanw 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX,
    468  1.18.2.1   nathanw 			    sc->mii_inst), MII_MEDIA_1000_X_FDX);
    469  1.18.2.1   nathanw 			PRINT("1000baseSX-FDX");
    470  1.18.2.1   nathanw 		}
    471  1.18.2.1   nathanw 
    472  1.18.2.1   nathanw 		/*
    473  1.18.2.1   nathanw 		 * 1000baseT media needs to be able to manipulate
    474  1.18.2.1   nathanw 		 * master/slave mode.  We set IFM_ETH_MASTER in
    475  1.18.2.1   nathanw 		 * the "don't care mask" and filter it out when
    476  1.18.2.1   nathanw 		 * the media is set.
    477  1.18.2.1   nathanw 		 *
    478  1.18.2.1   nathanw 		 * All 1000baseT PHYs have a 1000baseT control register.
    479  1.18.2.1   nathanw 		 */
    480  1.18.2.1   nathanw 		if (sc->mii_extcapabilities & EXTSR_1000THDX) {
    481  1.18.2.1   nathanw 			sc->mii_anegticks = 10;
    482  1.18.2.1   nathanw 			sc->mii_flags |= MIIF_HAVE_GTCR;
    483  1.18.2.1   nathanw 			mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
    484  1.18.2.2   nathanw 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 0,
    485  1.18.2.1   nathanw 			    sc->mii_inst), MII_MEDIA_1000_T);
    486  1.18.2.2   nathanw 			PRINT("1000baseT");
    487  1.18.2.1   nathanw 		}
    488  1.18.2.1   nathanw 		if (sc->mii_extcapabilities & EXTSR_1000TFDX) {
    489  1.18.2.1   nathanw 			sc->mii_anegticks = 10;
    490  1.18.2.1   nathanw 			sc->mii_flags |= MIIF_HAVE_GTCR;
    491  1.18.2.1   nathanw 			mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
    492  1.18.2.2   nathanw 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX,
    493  1.18.2.1   nathanw 			    sc->mii_inst), MII_MEDIA_1000_T_FDX);
    494  1.18.2.2   nathanw 			PRINT("1000baseT-FDX");
    495  1.18.2.1   nathanw 		}
    496  1.18.2.1   nathanw 	}
    497  1.18.2.1   nathanw 
    498       1.6   thorpej 	if (sc->mii_capabilities & BMSR_ANEG) {
    499       1.6   thorpej 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
    500       1.6   thorpej 		    MII_NMEDIA);	/* intentionally invalid index */
    501       1.5  drochner 		PRINT("auto");
    502       1.5  drochner 	}
    503       1.5  drochner #undef ADD
    504       1.5  drochner #undef PRINT
    505       1.9   thorpej }
    506       1.9   thorpej 
    507       1.9   thorpej void
    508      1.10   thorpej mii_phy_delete_media(sc)
    509       1.9   thorpej 	struct mii_softc *sc;
    510       1.9   thorpej {
    511       1.9   thorpej 	struct mii_data *mii = sc->mii_pdata;
    512       1.9   thorpej 
    513       1.9   thorpej 	ifmedia_delete_instance(&mii->mii_media, sc->mii_inst);
    514       1.9   thorpej }
    515       1.9   thorpej 
    516       1.9   thorpej int
    517      1.10   thorpej mii_phy_activate(self, act)
    518       1.9   thorpej 	struct device *self;
    519       1.9   thorpej 	enum devact act;
    520       1.9   thorpej {
    521       1.9   thorpej 	int rv = 0;
    522       1.9   thorpej 
    523       1.9   thorpej 	switch (act) {
    524       1.9   thorpej 	case DVACT_ACTIVATE:
    525       1.9   thorpej 		rv = EOPNOTSUPP;
    526       1.9   thorpej 		break;
    527       1.9   thorpej 
    528       1.9   thorpej 	case DVACT_DEACTIVATE:
    529       1.9   thorpej 		/* Nothing special to do. */
    530       1.9   thorpej 		break;
    531       1.9   thorpej 	}
    532       1.9   thorpej 
    533       1.9   thorpej 	return (rv);
    534       1.9   thorpej }
    535       1.9   thorpej 
    536       1.9   thorpej int
    537      1.10   thorpej mii_phy_detach(self, flags)
    538       1.9   thorpej 	struct device *self;
    539       1.9   thorpej 	int flags;
    540       1.9   thorpej {
    541       1.9   thorpej 	struct mii_softc *sc = (void *) self;
    542       1.9   thorpej 
    543       1.9   thorpej 	if (sc->mii_flags & MIIF_DOINGAUTO)
    544      1.17   thorpej 		callout_stop(&sc->mii_nway_ch);
    545       1.9   thorpej 
    546      1.10   thorpej 	mii_phy_delete_media(sc);
    547       1.9   thorpej 
    548       1.9   thorpej 	return (0);
    549  1.18.2.1   nathanw }
    550  1.18.2.1   nathanw 
    551  1.18.2.1   nathanw const struct mii_phydesc *
    552  1.18.2.1   nathanw mii_phy_match(const struct mii_attach_args *ma, const struct mii_phydesc *mpd)
    553  1.18.2.1   nathanw {
    554  1.18.2.1   nathanw 
    555  1.18.2.1   nathanw 	for (; mpd->mpd_name != NULL; mpd++) {
    556  1.18.2.1   nathanw 		if (MII_OUI(ma->mii_id1, ma->mii_id2) == mpd->mpd_oui &&
    557  1.18.2.1   nathanw 		    MII_MODEL(ma->mii_id2) == mpd->mpd_model)
    558  1.18.2.1   nathanw 			return (mpd);
    559  1.18.2.1   nathanw 	}
    560  1.18.2.1   nathanw 	return (NULL);
    561       1.1   thorpej }
    562