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