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