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