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