Home | History | Annotate | Line # | Download | only in mii
mii_physubr.c revision 1.87
      1 /*	$NetBSD: mii_physubr.c,v 1.87 2019/04/09 11:28:45 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.87 2019/04/09 11:28:45 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 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
    469 #define	PRINT(n)	aprint_normal("%s%s", sep, (n)); sep = ", "
    470 
    471 	if ((sc->mii_flags & MIIF_NOISOLATE) == 0)
    472 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
    473 		    MII_MEDIA_NONE);
    474 
    475 	/*
    476 	 * There are different interpretations for the bits in
    477 	 * HomePNA PHYs.  And there is really only one media type
    478 	 * that is supported.
    479 	 */
    480 	if (sc->mii_flags & MIIF_IS_HPNA) {
    481 		if (sc->mii_capabilities & BMSR_10THDX) {
    482 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_HPNA_1, 0,
    483 					 sc->mii_inst),
    484 			    MII_MEDIA_10_T);
    485 			PRINT("HomePNA1");
    486 		}
    487 		goto out;
    488 	}
    489 
    490 	if (sc->mii_capabilities & BMSR_10THDX) {
    491 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
    492 		    MII_MEDIA_10_T);
    493 		PRINT("10baseT");
    494 	}
    495 	if (sc->mii_capabilities & BMSR_10TFDX) {
    496 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
    497 		    MII_MEDIA_10_T_FDX);
    498 		PRINT("10baseT-FDX");
    499 		fdx = 1;
    500 	}
    501 	if (sc->mii_capabilities & BMSR_100TXHDX) {
    502 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
    503 		    MII_MEDIA_100_TX);
    504 		PRINT("100baseTX");
    505 	}
    506 	if (sc->mii_capabilities & BMSR_100TXFDX) {
    507 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
    508 		    MII_MEDIA_100_TX_FDX);
    509 		PRINT("100baseTX-FDX");
    510 		fdx = 1;
    511 	}
    512 	if (sc->mii_capabilities & BMSR_100T4) {
    513 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
    514 		    MII_MEDIA_100_T4);
    515 		PRINT("100baseT4");
    516 	}
    517 
    518 	if (sc->mii_extcapabilities & EXTSR_MEDIAMASK) {
    519 		/*
    520 		 * XXX Right now only handle 1000SX and 1000TX.  Need
    521 		 * XXX to handle 1000LX and 1000CX some how.
    522 		 *
    523 		 * Note since it can take 5 seconds to auto-negotiate
    524 		 * a gigabit link, we make anegticks 10 seconds for
    525 		 * all the gigabit media types.
    526 		 */
    527 		if (sc->mii_extcapabilities & EXTSR_1000XHDX) {
    528 			sc->mii_anegticks = MII_ANEGTICKS_GIGE;
    529 			sc->mii_flags |= MIIF_IS_1000X;
    530 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0,
    531 			    sc->mii_inst), MII_MEDIA_1000_X);
    532 			PRINT("1000baseSX");
    533 		}
    534 		if (sc->mii_extcapabilities & EXTSR_1000XFDX) {
    535 			sc->mii_anegticks = MII_ANEGTICKS_GIGE;
    536 			sc->mii_flags |= MIIF_IS_1000X;
    537 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX,
    538 			    sc->mii_inst), MII_MEDIA_1000_X_FDX);
    539 			PRINT("1000baseSX-FDX");
    540 			fdx = 1;
    541 		}
    542 
    543 		/*
    544 		 * 1000baseT media needs to be able to manipulate
    545 		 * master/slave mode.  We set IFM_ETH_MASTER in
    546 		 * the "don't care mask" and filter it out when
    547 		 * the media is set.
    548 		 *
    549 		 * All 1000baseT PHYs have a 1000baseT control register.
    550 		 */
    551 		if (sc->mii_extcapabilities & EXTSR_1000THDX) {
    552 			sc->mii_anegticks = MII_ANEGTICKS_GIGE;
    553 			sc->mii_flags |= MIIF_HAVE_GTCR;
    554 			mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
    555 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 0,
    556 			    sc->mii_inst), MII_MEDIA_1000_T);
    557 			PRINT("1000baseT");
    558 		}
    559 		if (sc->mii_extcapabilities & EXTSR_1000TFDX) {
    560 			sc->mii_anegticks = MII_ANEGTICKS_GIGE;
    561 			sc->mii_flags |= MIIF_HAVE_GTCR;
    562 			mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
    563 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX,
    564 			    sc->mii_inst), MII_MEDIA_1000_T_FDX);
    565 			PRINT("1000baseT-FDX");
    566 			fdx = 1;
    567 		}
    568 	}
    569 
    570 	if (sc->mii_capabilities & BMSR_ANEG) {
    571 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
    572 		    MII_NMEDIA);	/* intentionally invalid index */
    573 		PRINT("auto");
    574 	}
    575 #undef ADD
    576 #undef PRINT
    577 	if (fdx != 0 && (sc->mii_flags & MIIF_DOPAUSE))
    578 		mii->mii_media.ifm_mask |= IFM_ETH_FMASK;
    579 out:
    580 	if (!pmf_device_register(self, NULL, mii_phy_resume)) {
    581 		aprint_normal("\n");
    582 		aprint_error_dev(self, "couldn't establish power handler");
    583 	}
    584 }
    585 
    586 void
    587 mii_phy_delete_media(struct mii_softc *sc)
    588 {
    589 	struct mii_data *mii = sc->mii_pdata;
    590 
    591 	ifmedia_delete_instance(&mii->mii_media, sc->mii_inst);
    592 }
    593 
    594 int
    595 mii_phy_activate(device_t self, enum devact act)
    596 {
    597 
    598 	switch (act) {
    599 	case DVACT_DEACTIVATE:
    600 		/* XXX Invalidate parent's media setting? */
    601 		return 0;
    602 	default:
    603 		return EOPNOTSUPP;
    604 	}
    605 }
    606 
    607 /* ARGSUSED1 */
    608 int
    609 mii_phy_detach(device_t self, int flags)
    610 {
    611 	struct mii_softc *sc = device_private(self);
    612 
    613 	/* XXX Invalidate parent's media setting? */
    614 
    615 	if (sc->mii_flags & MIIF_DOINGAUTO)
    616 		callout_halt(&sc->mii_nway_ch, NULL);
    617 
    618 	callout_destroy(&sc->mii_nway_ch);
    619 
    620 	mii_phy_delete_media(sc);
    621 	LIST_REMOVE(sc, mii_list);
    622 
    623 	return 0;
    624 }
    625 
    626 const struct mii_phydesc *
    627 mii_phy_match(const struct mii_attach_args *ma, const struct mii_phydesc *mpd)
    628 {
    629 
    630 	for (; mpd->mpd_name != NULL; mpd++) {
    631 		if (MII_OUI(ma->mii_id1, ma->mii_id2) == mpd->mpd_oui &&
    632 		    MII_MODEL(ma->mii_id2) == mpd->mpd_model)
    633 			return mpd;
    634 	}
    635 	return NULL;
    636 }
    637 
    638 /*
    639  * Return the flow control status flag from MII_ANAR & MII_ANLPAR.
    640  */
    641 u_int
    642 mii_phy_flowstatus(struct mii_softc *sc)
    643 {
    644 	uint16_t anar, anlpar;
    645 
    646 	if ((sc->mii_flags & MIIF_DOPAUSE) == 0)
    647 		return 0;
    648 
    649 	PHY_READ(sc, MII_ANAR, &anar);
    650 	PHY_READ(sc, MII_ANLPAR, &anlpar);
    651 
    652 	/* For 1000baseX, the bits are in a different location. */
    653 	if (sc->mii_flags & MIIF_IS_1000X) {
    654 		anar <<= 3;
    655 		anlpar <<= 3;
    656 	}
    657 
    658 	if ((anar & ANAR_PAUSE_SYM) & (anlpar & ANLPAR_PAUSE_SYM))
    659 		return (IFM_FLOW | IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE);
    660 
    661 	if ((anar & ANAR_PAUSE_SYM) == 0) {
    662 		if ((anar & ANAR_PAUSE_ASYM) &&
    663 		    ((anlpar & ANLPAR_PAUSE_TOWARDS) == ANLPAR_PAUSE_TOWARDS))
    664 			return (IFM_FLOW | IFM_ETH_TXPAUSE);
    665 		else
    666 			return 0;
    667 	}
    668 
    669 	if ((anar & ANAR_PAUSE_ASYM) == 0) {
    670 		if (anlpar & ANLPAR_PAUSE_SYM)
    671 			return (IFM_FLOW | IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE);
    672 		else
    673 			return 0;
    674 	}
    675 
    676 	switch ((anlpar & ANLPAR_PAUSE_TOWARDS)) {
    677 	case ANLPAR_PAUSE_NONE:
    678 		return 0;
    679 
    680 	case ANLPAR_PAUSE_ASYM:
    681 		return (IFM_FLOW | IFM_ETH_RXPAUSE);
    682 
    683 	default:
    684 		return (IFM_FLOW | IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE);
    685 	}
    686 	/* NOTREACHED */
    687 }
    688 
    689 bool
    690 mii_phy_resume(device_t dv, const pmf_qual_t *qual)
    691 {
    692 	struct mii_softc *sc = device_private(dv);
    693 
    694 	PHY_RESET(sc);
    695 	return PHY_SERVICE(sc, sc->mii_pdata, MII_MEDIACHG) == 0;
    696 }
    697 
    698 
    699 /*
    700  * Given an ifmedia word, return the corresponding ANAR value.
    701  */
    702 uint16_t
    703 mii_anar(int media)
    704 {
    705 	int rv;
    706 
    707 #ifdef DIAGNOSTIC
    708 	if (/* media < 0 || */ media >= MII_NMEDIA)
    709 		panic("mii_anar");
    710 #endif
    711 
    712 	rv = mii_media_table[media].mm_anar;
    713 
    714 	return rv;
    715 }
    716