Home | History | Annotate | Line # | Download | only in mii
mii_physubr.c revision 1.32
      1 /*	$NetBSD: mii_physubr.c,v 1.32 2002/05/10 20:45:05 thorpej 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  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Subroutines common to all PHYs.
     42  */
     43 
     44 #include <sys/cdefs.h>
     45 __KERNEL_RCSID(0, "$NetBSD: mii_physubr.c,v 1.32 2002/05/10 20:45:05 thorpej Exp $");
     46 
     47 #include <sys/param.h>
     48 #include <sys/device.h>
     49 #include <sys/systm.h>
     50 #include <sys/kernel.h>
     51 #include <sys/socket.h>
     52 #include <sys/errno.h>
     53 #include <sys/proc.h>
     54 
     55 #include <net/if.h>
     56 #include <net/if_media.h>
     57 #include <net/route.h>
     58 
     59 #include <dev/mii/mii.h>
     60 #include <dev/mii/miivar.h>
     61 
     62 /*
     63  * Media to register setting conversion table.  Order matters.
     64  */
     65 const struct mii_media mii_media_table[MII_NMEDIA] = {
     66 	/* None */
     67 	{ BMCR_ISO,		ANAR_CSMA,
     68 	  0, },
     69 
     70 	/* 10baseT */
     71 	{ BMCR_S10,		ANAR_CSMA|ANAR_10,
     72 	  0, },
     73 
     74 	/* 10baseT-FDX */
     75 	{ BMCR_S10|BMCR_FDX,	ANAR_CSMA|ANAR_10_FD,
     76 	  0, },
     77 
     78 	/* 100baseT4 */
     79 	{ BMCR_S100,		ANAR_CSMA|ANAR_T4,
     80 	  0, },
     81 
     82 	/* 100baseTX */
     83 	{ BMCR_S100,		ANAR_CSMA|ANAR_TX,
     84 	  0, },
     85 
     86 	/* 100baseTX-FDX */
     87 	{ BMCR_S100|BMCR_FDX,	ANAR_CSMA|ANAR_TX_FD,
     88 	  0, },
     89 
     90 	/* 1000baseX */
     91 	{ BMCR_S1000,		ANAR_CSMA,
     92 	  0, },
     93 
     94 	/* 1000baseX-FDX */
     95 	{ BMCR_S1000|BMCR_FDX,	ANAR_CSMA,
     96 	  0, },
     97 
     98 	/* 1000baseT */
     99 	{ BMCR_S1000,		ANAR_CSMA,
    100 	  GTCR_ADV_1000THDX },
    101 
    102 	/* 1000baseT-FDX */
    103 	{ BMCR_S1000,		ANAR_CSMA,
    104 	  GTCR_ADV_1000TFDX },
    105 };
    106 
    107 void	mii_phy_auto_timeout(void *);
    108 
    109 void
    110 mii_phy_setmedia(struct mii_softc *sc)
    111 {
    112 	struct mii_data *mii = sc->mii_pdata;
    113 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    114 	int bmcr, anar, gtcr;
    115 
    116 	if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO) {
    117 //		if ((PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) == 0)
    118 			(void) mii_phy_auto(sc, 1);
    119 		return;
    120 	}
    121 
    122 	/*
    123 	 * Table index is stored in the media entry.
    124 	 */
    125 
    126 #ifdef DIAGNOSTIC
    127 	if (ife->ifm_data < 0 || ife->ifm_data >= MII_NMEDIA)
    128 		panic("mii_phy_setmedia");
    129 #endif
    130 
    131 	anar = mii_media_table[ife->ifm_data].mm_anar;
    132 	bmcr = mii_media_table[ife->ifm_data].mm_bmcr;
    133 	gtcr = mii_media_table[ife->ifm_data].mm_gtcr;
    134 
    135 	if (mii->mii_media.ifm_media & IFM_ETH_MASTER) {
    136 		switch (IFM_SUBTYPE(ife->ifm_media)) {
    137 		case IFM_1000_T:
    138 			gtcr |= GTCR_MAN_MS|GTCR_ADV_MS;
    139 			break;
    140 
    141 		default:
    142 			panic("mii_phy_setmedia: MASTER on wrong media");
    143 		}
    144 	}
    145 
    146 	if (ife->ifm_media & IFM_LOOP)
    147 		bmcr |= BMCR_LOOP;
    148 
    149 	PHY_WRITE(sc, MII_ANAR, anar);
    150 	PHY_WRITE(sc, MII_BMCR, bmcr);
    151 	if (sc->mii_flags & MIIF_HAVE_GTCR)
    152 		PHY_WRITE(sc, MII_100T2CR, gtcr);
    153 }
    154 
    155 int
    156 mii_phy_auto(struct mii_softc *sc, int waitfor)
    157 {
    158 	int i;
    159 
    160 	if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
    161 		/*
    162 		 * Check for 1000BASE-X.  Autonegotiation is a bit
    163 		 * different on such devices.
    164 		 */
    165 		if (sc->mii_flags & MIIF_IS_1000X) {
    166 			uint16_t anar = 0;
    167 
    168 			if (sc->mii_extcapabilities & EXTSR_1000XFDX)
    169 				anar |= ANAR_X_FD;
    170 			if (sc->mii_extcapabilities & EXTSR_1000XHDX)
    171 				anar |= ANAR_X_HD;
    172 
    173 			if (sc->mii_flags & MIIF_DOPAUSE) {
    174 				/* XXX Asymmetric vs. symmetric? */
    175 				anar |= ANLPAR_X_PAUSE_TOWARDS;
    176 			}
    177 
    178 			PHY_WRITE(sc, MII_ANAR, anar);
    179 		} else {
    180 			uint16_t anar;
    181 
    182 			anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) |
    183 			    ANAR_CSMA;
    184 			if (sc->mii_flags & MIIF_DOPAUSE)
    185 				anar |= ANAR_FC;
    186 			PHY_WRITE(sc, MII_ANAR, anar);
    187 			if (sc->mii_flags & MIIF_HAVE_GTCR) {
    188 				uint16_t gtcr = 0;
    189 
    190 				if (sc->mii_extcapabilities & EXTSR_1000TFDX)
    191 					gtcr |= GTCR_ADV_1000TFDX;
    192 				if (sc->mii_extcapabilities & EXTSR_1000THDX)
    193 					gtcr |= GTCR_ADV_1000THDX;
    194 
    195 				PHY_WRITE(sc, MII_100T2CR, gtcr);
    196 			}
    197 		}
    198 		PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
    199 	}
    200 
    201 	if (waitfor) {
    202 		/* Wait 500ms for it to complete. */
    203 		for (i = 0; i < 500; i++) {
    204 			if (PHY_READ(sc, MII_BMSR) & BMSR_ACOMP)
    205 				return (0);
    206 			delay(1000);
    207 		}
    208 
    209 		/*
    210 		 * Don't need to worry about clearing MIIF_DOINGAUTO.
    211 		 * If that's set, a timeout is pending, and it will
    212 		 * clear the flag.
    213 		 */
    214 		return (EIO);
    215 	}
    216 
    217 	/*
    218 	 * Just let it finish asynchronously.  This is for the benefit of
    219 	 * the tick handler driving autonegotiation.  Don't want 500ms
    220 	 * delays all the time while the system is running!
    221 	 */
    222 	if (sc->mii_flags & MIIF_AUTOTSLEEP) {
    223 		sc->mii_flags |= MIIF_DOINGAUTO;
    224 		tsleep(&sc->mii_flags, PZERO, "miiaut", hz >> 1);
    225 		mii_phy_auto_timeout(sc);
    226 	} else if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
    227 		sc->mii_flags |= MIIF_DOINGAUTO;
    228 		callout_reset(&sc->mii_nway_ch, hz >> 1,
    229 		    mii_phy_auto_timeout, sc);
    230 	}
    231 	return (EJUSTRETURN);
    232 }
    233 
    234 void
    235 mii_phy_auto_timeout(void *arg)
    236 {
    237 	struct mii_softc *sc = arg;
    238 	int s;
    239 
    240 	if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0)
    241 		return;
    242 
    243 	s = splnet();
    244 	sc->mii_flags &= ~MIIF_DOINGAUTO;
    245 
    246 	/* Update the media status. */
    247 	(void) PHY_SERVICE(sc, sc->mii_pdata, MII_POLLSTAT);
    248 	splx(s);
    249 }
    250 
    251 int
    252 mii_phy_tick(struct mii_softc *sc)
    253 {
    254 	struct mii_data *mii = sc->mii_pdata;
    255 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    256 	int reg;
    257 
    258 	/* Just bail now if the interface is down. */
    259 	if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    260 		return (EJUSTRETURN);
    261 
    262 	/*
    263 	 * If we're not doing autonegotiation, we don't need to do
    264 	 * any extra work here.  However, we need to check the link
    265 	 * status so we can generate an announcement if the status
    266 	 * changes.
    267 	 */
    268 	if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
    269 		return (0);
    270 
    271 	/* Read the status register twice; BMSR_LINK is latch-low. */
    272 	reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
    273 	if (reg & BMSR_LINK) {
    274 		/*
    275 		 * See above.
    276 		 */
    277 		return (0);
    278 	}
    279 
    280 	/*
    281 	 * Only retry autonegotiation every N seconds.
    282 	 */
    283 	KASSERT(sc->mii_anegticks != 0);
    284 	if (++sc->mii_ticks != sc->mii_anegticks)
    285 		return (EJUSTRETURN);
    286 
    287 	sc->mii_ticks = 0;
    288 	PHY_RESET(sc);
    289 
    290 	if (mii_phy_auto(sc, 0) == EJUSTRETURN)
    291 		return (EJUSTRETURN);
    292 
    293 	/*
    294 	 * Might need to generate a status message if autonegotiation
    295 	 * failed.
    296 	 */
    297 	return (0);
    298 }
    299 
    300 void
    301 mii_phy_reset(struct mii_softc *sc)
    302 {
    303 	int reg, i;
    304 
    305 	if (sc->mii_flags & MIIF_NOISOLATE)
    306 		reg = BMCR_RESET;
    307 	else
    308 		reg = BMCR_RESET | BMCR_ISO;
    309 	PHY_WRITE(sc, MII_BMCR, reg);
    310 
    311 	/* Wait 100ms for it to complete. */
    312 	for (i = 0; i < 100; i++) {
    313 		reg = PHY_READ(sc, MII_BMCR);
    314 		if ((reg & BMCR_RESET) == 0)
    315 			break;
    316 		delay(1000);
    317 	}
    318 
    319 	if (sc->mii_inst != 0 && ((sc->mii_flags & MIIF_NOISOLATE) == 0))
    320 		PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    321 }
    322 
    323 void
    324 mii_phy_down(struct mii_softc *sc)
    325 {
    326 
    327 	if (sc->mii_flags & MIIF_DOINGAUTO) {
    328 		sc->mii_flags &= ~MIIF_DOINGAUTO;
    329 		callout_stop(&sc->mii_nway_ch);
    330 	}
    331 }
    332 
    333 void
    334 mii_phy_status(struct mii_softc *sc)
    335 {
    336 
    337 	PHY_STATUS(sc);
    338 }
    339 
    340 void
    341 mii_phy_update(struct mii_softc *sc, int cmd)
    342 {
    343 	struct mii_data *mii = sc->mii_pdata;
    344 	int announce, s;
    345 
    346 	if (sc->mii_media_active != mii->mii_media_active ||
    347 	    sc->mii_media_status != mii->mii_media_status ||
    348 	    cmd == MII_MEDIACHG) {
    349 		announce = mii_phy_statusmsg(sc);
    350 		(*mii->mii_statchg)(sc->mii_dev.dv_parent);
    351 		sc->mii_media_active = mii->mii_media_active;
    352 		sc->mii_media_status = mii->mii_media_status;
    353 
    354 		if (announce) {
    355 			s = splnet();
    356 			rt_ifmsg(mii->mii_ifp);
    357 			splx(s);
    358 		}
    359 	}
    360 }
    361 
    362 int
    363 mii_phy_statusmsg(struct mii_softc *sc)
    364 {
    365 	struct mii_data *mii = sc->mii_pdata;
    366 	struct ifnet *ifp = mii->mii_ifp;
    367 	int baudrate, link_state, announce = 0;
    368 
    369 	if (mii->mii_media_status & IFM_AVALID) {
    370 		if (mii->mii_media_status & IFM_ACTIVE)
    371 			link_state = LINK_STATE_UP;
    372 		else
    373 			link_state = LINK_STATE_DOWN;
    374 	} else
    375 		link_state = LINK_STATE_UNKNOWN;
    376 
    377 	baudrate = ifmedia_baudrate(mii->mii_media_active);
    378 
    379 	if (link_state != ifp->if_link_state) {
    380 		ifp->if_link_state = link_state;
    381 		/*
    382 		 * XXX Right here we'd like to notify protocols
    383 		 * XXX that the link status has changed, so that
    384 		 * XXX e.g. Duplicate Address Detection can restart.
    385 		 */
    386 		announce = 1;
    387 	}
    388 
    389 	if (baudrate != ifp->if_baudrate) {
    390 		ifp->if_baudrate = baudrate;
    391 		announce = 1;
    392 	}
    393 
    394 	return (announce);
    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 	const char *sep = "";
    407 
    408 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
    409 #define	PRINT(n)	printf("%s%s", sep, (n)); sep = ", "
    410 
    411 	if ((sc->mii_flags & MIIF_NOISOLATE) == 0)
    412 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
    413 		    MII_MEDIA_NONE);
    414 
    415 	/*
    416 	 * There are different interpretations for the bits in
    417 	 * HomePNA PHYs.  And there is really only one media type
    418 	 * that is supported.
    419 	 */
    420 	if (sc->mii_flags & MIIF_IS_HPNA) {
    421 		if (sc->mii_capabilities & BMSR_10THDX) {
    422 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_HPNA_1, 0,
    423 					 sc->mii_inst),
    424 			    MII_MEDIA_10_T);
    425 			PRINT("HomePNA1");
    426 		}
    427 		return;
    428 	}
    429 
    430 	if (sc->mii_capabilities & BMSR_10THDX) {
    431 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
    432 		    MII_MEDIA_10_T);
    433 		PRINT("10baseT");
    434 	}
    435 	if (sc->mii_capabilities & BMSR_10TFDX) {
    436 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
    437 		    MII_MEDIA_10_T_FDX);
    438 		PRINT("10baseT-FDX");
    439 	}
    440 	if (sc->mii_capabilities & BMSR_100TXHDX) {
    441 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
    442 		    MII_MEDIA_100_TX);
    443 		PRINT("100baseTX");
    444 	}
    445 	if (sc->mii_capabilities & BMSR_100TXFDX) {
    446 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
    447 		    MII_MEDIA_100_TX_FDX);
    448 		PRINT("100baseTX-FDX");
    449 	}
    450 	if (sc->mii_capabilities & BMSR_100T4) {
    451 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
    452 		    MII_MEDIA_100_T4);
    453 		PRINT("100baseT4");
    454 	}
    455 
    456 	if (sc->mii_extcapabilities & EXTSR_MEDIAMASK) {
    457 		/*
    458 		 * XXX Right now only handle 1000SX and 1000TX.  Need
    459 		 * XXX to handle 1000LX and 1000CX some how.
    460 		 *
    461 		 * Note since it can take 5 seconds to auto-negotiate
    462 		 * a gigabit link, we make anegticks 10 seconds for
    463 		 * all the gigabit media types.
    464 		 */
    465 		if (sc->mii_extcapabilities & EXTSR_1000XHDX) {
    466 			sc->mii_anegticks = 10;
    467 			sc->mii_flags |= MIIF_IS_1000X;
    468 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0,
    469 			    sc->mii_inst), MII_MEDIA_1000_X);
    470 			PRINT("1000baseSX");
    471 		}
    472 		if (sc->mii_extcapabilities & EXTSR_1000XFDX) {
    473 			sc->mii_anegticks = 10;
    474 			sc->mii_flags |= MIIF_IS_1000X;
    475 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX,
    476 			    sc->mii_inst), MII_MEDIA_1000_X_FDX);
    477 			PRINT("1000baseSX-FDX");
    478 		}
    479 
    480 		/*
    481 		 * 1000baseT media needs to be able to manipulate
    482 		 * master/slave mode.  We set IFM_ETH_MASTER in
    483 		 * the "don't care mask" and filter it out when
    484 		 * the media is set.
    485 		 *
    486 		 * All 1000baseT PHYs have a 1000baseT control register.
    487 		 */
    488 		if (sc->mii_extcapabilities & EXTSR_1000THDX) {
    489 			sc->mii_anegticks = 10;
    490 			sc->mii_flags |= MIIF_HAVE_GTCR;
    491 			mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
    492 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 0,
    493 			    sc->mii_inst), MII_MEDIA_1000_T);
    494 			PRINT("1000baseT");
    495 		}
    496 		if (sc->mii_extcapabilities & EXTSR_1000TFDX) {
    497 			sc->mii_anegticks = 10;
    498 			sc->mii_flags |= MIIF_HAVE_GTCR;
    499 			mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
    500 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX,
    501 			    sc->mii_inst), MII_MEDIA_1000_T_FDX);
    502 			PRINT("1000baseT-FDX");
    503 		}
    504 	}
    505 
    506 	if (sc->mii_capabilities & BMSR_ANEG) {
    507 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
    508 		    MII_NMEDIA);	/* intentionally invalid index */
    509 		PRINT("auto");
    510 	}
    511 #undef ADD
    512 #undef PRINT
    513 }
    514 
    515 void
    516 mii_phy_delete_media(struct mii_softc *sc)
    517 {
    518 	struct mii_data *mii = sc->mii_pdata;
    519 
    520 	ifmedia_delete_instance(&mii->mii_media, sc->mii_inst);
    521 }
    522 
    523 int
    524 mii_phy_activate(struct device *self, enum devact act)
    525 {
    526 	int rv = 0;
    527 
    528 	switch (act) {
    529 	case DVACT_ACTIVATE:
    530 		rv = EOPNOTSUPP;
    531 		break;
    532 
    533 	case DVACT_DEACTIVATE:
    534 		/* Nothing special to do. */
    535 		break;
    536 	}
    537 
    538 	return (rv);
    539 }
    540 
    541 /* ARGSUSED1 */
    542 int
    543 mii_phy_detach(struct device *self, int flags)
    544 {
    545 	struct mii_softc *sc = (void *) self;
    546 
    547 	if (sc->mii_flags & MIIF_DOINGAUTO)
    548 		callout_stop(&sc->mii_nway_ch);
    549 
    550 	mii_phy_delete_media(sc);
    551 
    552 	return (0);
    553 }
    554 
    555 const struct mii_phydesc *
    556 mii_phy_match(const struct mii_attach_args *ma, const struct mii_phydesc *mpd)
    557 {
    558 
    559 	for (; mpd->mpd_name != NULL; mpd++) {
    560 		if (MII_OUI(ma->mii_id1, ma->mii_id2) == mpd->mpd_oui &&
    561 		    MII_MODEL(ma->mii_id2) == mpd->mpd_model)
    562 			return (mpd);
    563 	}
    564 	return (NULL);
    565 }
    566