Home | History | Annotate | Line # | Download | only in mii
mii_physubr.c revision 1.24
      1 /*	$NetBSD: mii_physubr.c,v 1.24 2001/06/02 21:39:40 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/param.h>
     45 #include <sys/device.h>
     46 #include <sys/systm.h>
     47 #include <sys/kernel.h>
     48 #include <sys/socket.h>
     49 #include <sys/errno.h>
     50 #include <sys/proc.h>
     51 
     52 #include <net/if.h>
     53 #include <net/if_media.h>
     54 #include <net/route.h>
     55 
     56 #include <dev/mii/mii.h>
     57 #include <dev/mii/miivar.h>
     58 
     59 /*
     60  * Media to register setting conversion table.  Order matters.
     61  */
     62 const struct mii_media mii_media_table[MII_NMEDIA] = {
     63 	/* None */
     64 	{ BMCR_ISO,		ANAR_CSMA,
     65 	  0, },
     66 
     67 	/* 10baseT */
     68 	{ BMCR_S10,		ANAR_CSMA|ANAR_10,
     69 	  0, },
     70 
     71 	/* 10baseT-FDX */
     72 	{ BMCR_S10|BMCR_FDX,	ANAR_CSMA|ANAR_10_FD,
     73 	  0, },
     74 
     75 	/* 100baseT4 */
     76 	{ BMCR_S100,		ANAR_CSMA|ANAR_T4,
     77 	  0, },
     78 
     79 	/* 100baseTX */
     80 	{ BMCR_S100,		ANAR_CSMA|ANAR_TX,
     81 	  0, },
     82 
     83 	/* 100baseTX-FDX */
     84 	{ BMCR_S100|BMCR_FDX,	ANAR_CSMA|ANAR_TX_FD,
     85 	  0, },
     86 
     87 	/* 1000baseX */
     88 	{ BMCR_S1000,		ANAR_CSMA,
     89 	  0, },
     90 
     91 	/* 1000baseX-FDX */
     92 	{ BMCR_S1000|BMCR_FDX,	ANAR_CSMA,
     93 	  0, },
     94 
     95 	/* 1000baseT */
     96 	{ BMCR_S1000,		ANAR_CSMA,
     97 	  GTCR_ADV_1000THDX },
     98 
     99 	/* 1000baseT-FDX */
    100 	{ BMCR_S1000,		ANAR_CSMA,
    101 	  GTCR_ADV_1000TFDX },
    102 };
    103 
    104 void	mii_phy_auto_timeout __P((void *));
    105 
    106 void
    107 mii_phy_setmedia(sc)
    108 	struct mii_softc *sc;
    109 {
    110 	struct mii_data *mii = sc->mii_pdata;
    111 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    112 	int bmcr, anar, gtcr;
    113 
    114 	if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO) {
    115 		if ((PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) == 0)
    116 			(void) mii_phy_auto(sc, 1);
    117 		return;
    118 	}
    119 
    120 	/*
    121 	 * Table index is stored in the media entry.
    122 	 */
    123 
    124 #ifdef DIAGNOSTIC
    125 	if (ife->ifm_data < 0 || ife->ifm_data >= MII_NMEDIA)
    126 		panic("mii_phy_setmedia");
    127 #endif
    128 
    129 	anar = mii_media_table[ife->ifm_data].mm_anar;
    130 	bmcr = mii_media_table[ife->ifm_data].mm_bmcr;
    131 	gtcr = mii_media_table[ife->ifm_data].mm_gtcr;
    132 
    133 	if (mii->mii_media.ifm_media & IFM_ETH_MASTER) {
    134 		switch (IFM_SUBTYPE(ife->ifm_media)) {
    135 		case IFM_1000_TX:
    136 			gtcr |= GTCR_MAN_MS|GTCR_ADV_MS;
    137 			break;
    138 
    139 		default:
    140 			panic("mii_phy_setmedia: MASTER on wrong media");
    141 		}
    142 	}
    143 
    144 	if (ife->ifm_media & IFM_LOOP)
    145 		bmcr |= BMCR_LOOP;
    146 
    147 	PHY_WRITE(sc, MII_ANAR, anar);
    148 	PHY_WRITE(sc, MII_BMCR, bmcr);
    149 	if (sc->mii_flags & MIIF_HAVE_GTCR)
    150 		PHY_WRITE(sc, MII_100T2CR, gtcr);
    151 }
    152 
    153 static int
    154 mii_phy_extcap_to_gtcr(struct mii_softc *sc)
    155 {
    156 	int gtcr = 0;
    157 
    158 	if (sc->mii_extcapabilities & EXTSR_1000TFDX)
    159 		gtcr |= GTCR_ADV_1000TFDX;
    160 	if (sc->mii_extcapabilities & EXTSR_1000THDX)
    161 		gtcr |= GTCR_ADV_1000THDX;
    162 
    163 	return (gtcr);
    164 }
    165 
    166 int
    167 mii_phy_auto(sc, waitfor)
    168 	struct mii_softc *sc;
    169 	int waitfor;
    170 {
    171 	int bmsr, i;
    172 
    173 	if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
    174 		PHY_WRITE(sc, MII_ANAR,
    175 		    BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) | ANAR_CSMA);
    176 		if (sc->mii_flags & MIIF_HAVE_GTCR)
    177 			PHY_WRITE(sc, MII_100T2CR, mii_phy_extcap_to_gtcr(sc));
    178 		PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
    179 	}
    180 
    181 	if (waitfor) {
    182 		/* Wait 500ms for it to complete. */
    183 		for (i = 0; i < 500; i++) {
    184 			if ((bmsr = PHY_READ(sc, MII_BMSR)) & BMSR_ACOMP)
    185 				return (0);
    186 			delay(1000);
    187 		}
    188 
    189 		/*
    190 		 * Don't need to worry about clearing MIIF_DOINGAUTO.
    191 		 * If that's set, a timeout is pending, and it will
    192 		 * clear the flag.
    193 		 */
    194 		return (EIO);
    195 	}
    196 
    197 	/*
    198 	 * Just let it finish asynchronously.  This is for the benefit of
    199 	 * the tick handler driving autonegotiation.  Don't want 500ms
    200 	 * delays all the time while the system is running!
    201 	 */
    202 	if (sc->mii_flags & MIIF_AUTOTSLEEP) {
    203 		sc->mii_flags |= MIIF_DOINGAUTO;
    204 		tsleep(&sc->mii_flags, PZERO, "miiaut", hz >> 1);
    205 		mii_phy_auto_timeout(sc);
    206 	} else if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
    207 		sc->mii_flags |= MIIF_DOINGAUTO;
    208 		callout_reset(&sc->mii_nway_ch, hz >> 1,
    209 		    mii_phy_auto_timeout, sc);
    210 	}
    211 	return (EJUSTRETURN);
    212 }
    213 
    214 void
    215 mii_phy_auto_timeout(arg)
    216 	void *arg;
    217 {
    218 	struct mii_softc *sc = arg;
    219 	int s, bmsr;
    220 
    221 	if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0)
    222 		return;
    223 
    224 	s = splnet();
    225 	sc->mii_flags &= ~MIIF_DOINGAUTO;
    226 	bmsr = PHY_READ(sc, MII_BMSR);
    227 
    228 	/* Update the media status. */
    229 	(void) PHY_SERVICE(sc, sc->mii_pdata, MII_POLLSTAT);
    230 	splx(s);
    231 }
    232 
    233 int
    234 mii_phy_tick(sc)
    235 	struct mii_softc *sc;
    236 {
    237 	struct mii_data *mii = sc->mii_pdata;
    238 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    239 	int reg;
    240 
    241 	/* Just bail now if the interface is down. */
    242 	if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    243 		return (EJUSTRETURN);
    244 
    245 	/*
    246 	 * If we're not doing autonegotiation, we don't need to do
    247 	 * any extra work here.  However, we need to check the link
    248 	 * status so we can generate an announcement if the status
    249 	 * changes.
    250 	 */
    251 	if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
    252 		return (0);
    253 
    254 	/* Read the status register twice; BMSR_LINK is latch-low. */
    255 	reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
    256 	if (reg & BMSR_LINK) {
    257 		/*
    258 		 * See above.
    259 		 */
    260 		return (0);
    261 	}
    262 
    263 	/*
    264 	 * Only retry autonegotiation every N seconds.
    265 	 */
    266 	KASSERT(sc->mii_anegticks != 0);
    267 	if (++sc->mii_ticks != sc->mii_anegticks)
    268 		return (EJUSTRETURN);
    269 
    270 	sc->mii_ticks = 0;
    271 	PHY_RESET(sc);
    272 
    273 	if (mii_phy_auto(sc, 0) == EJUSTRETURN)
    274 		return (EJUSTRETURN);
    275 
    276 	/*
    277 	 * Might need to generate a status message if autonegotiation
    278 	 * failed.
    279 	 */
    280 	return (0);
    281 }
    282 
    283 void
    284 mii_phy_reset(sc)
    285 	struct mii_softc *sc;
    286 {
    287 	int reg, i;
    288 
    289 	if (sc->mii_flags & MIIF_NOISOLATE)
    290 		reg = BMCR_RESET;
    291 	else
    292 		reg = BMCR_RESET | BMCR_ISO;
    293 	PHY_WRITE(sc, MII_BMCR, reg);
    294 
    295 	/* Wait 100ms for it to complete. */
    296 	for (i = 0; i < 100; i++) {
    297 		reg = PHY_READ(sc, MII_BMCR);
    298 		if ((reg & BMCR_RESET) == 0)
    299 			break;
    300 		delay(1000);
    301 	}
    302 
    303 	if (sc->mii_inst != 0 && ((sc->mii_flags & MIIF_NOISOLATE) == 0))
    304 		PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    305 }
    306 
    307 void
    308 mii_phy_down(sc)
    309 	struct mii_softc *sc;
    310 {
    311 
    312 	if (sc->mii_flags & MIIF_DOINGAUTO) {
    313 		sc->mii_flags &= ~MIIF_DOINGAUTO;
    314 		callout_stop(&sc->mii_nway_ch);
    315 	}
    316 }
    317 
    318 void
    319 mii_phy_status(sc)
    320 	struct mii_softc *sc;
    321 {
    322 
    323 	PHY_STATUS(sc);
    324 }
    325 
    326 void
    327 mii_phy_update(sc, cmd)
    328 	struct mii_softc *sc;
    329 	int cmd;
    330 {
    331 	struct mii_data *mii = sc->mii_pdata;
    332 
    333 	if (sc->mii_media_active != mii->mii_media_active ||
    334 	    sc->mii_media_status != mii->mii_media_status ||
    335 	    cmd == MII_MEDIACHG) {
    336 		(*mii->mii_statchg)(sc->mii_dev.dv_parent);
    337 		mii_phy_statusmsg(sc);
    338 		sc->mii_media_active = mii->mii_media_active;
    339 		sc->mii_media_status = mii->mii_media_status;
    340 	}
    341 }
    342 
    343 void
    344 mii_phy_statusmsg(sc)
    345 	struct mii_softc *sc;
    346 {
    347 	struct mii_data *mii = sc->mii_pdata;
    348 	struct ifnet *ifp = mii->mii_ifp;
    349 	int s, baudrate, link_state, announce = 0;
    350 
    351 	if (mii->mii_media_status & IFM_AVALID) {
    352 		if (mii->mii_media_status & IFM_ACTIVE)
    353 			link_state = LINK_STATE_UP;
    354 		else
    355 			link_state = LINK_STATE_DOWN;
    356 	} else
    357 		link_state = LINK_STATE_UNKNOWN;
    358 
    359 	baudrate = ifmedia_baudrate(mii->mii_media_active);
    360 
    361 	if (link_state != ifp->if_link_state) {
    362 		ifp->if_link_state = link_state;
    363 		/*
    364 		 * XXX Right here we'd like to notify protocols
    365 		 * XXX that the link status has changed, so that
    366 		 * XXX e.g. Duplicate Address Detection can restart.
    367 		 */
    368 		announce = 1;
    369 	}
    370 
    371 	if (baudrate != ifp->if_baudrate) {
    372 		ifp->if_baudrate = baudrate;
    373 		announce = 1;
    374 	}
    375 
    376 	if (announce) {
    377 		s = splnet();
    378 		rt_ifmsg(ifp);
    379 		splx(s);
    380 	}
    381 }
    382 
    383 /*
    384  * Initialize generic PHY media based on BMSR, called when a PHY is
    385  * attached.  We expect to be set up to print a comma-separated list
    386  * of media names.  Does not print a newline.
    387  */
    388 void
    389 mii_phy_add_media(sc)
    390 	struct mii_softc *sc;
    391 {
    392 	struct mii_data *mii = sc->mii_pdata;
    393 	const char *sep = "";
    394 
    395 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
    396 #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
    397 
    398 	if ((sc->mii_flags & MIIF_NOISOLATE) == 0)
    399 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
    400 		    MII_MEDIA_NONE);
    401 
    402 	if (sc->mii_capabilities & BMSR_10THDX) {
    403 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
    404 		    MII_MEDIA_10_T);
    405 		PRINT("10baseT");
    406 	}
    407 	if (sc->mii_capabilities & BMSR_10TFDX) {
    408 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
    409 		    MII_MEDIA_10_T_FDX);
    410 		PRINT("10baseT-FDX");
    411 	}
    412 	if (sc->mii_capabilities & BMSR_100TXHDX) {
    413 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
    414 		    MII_MEDIA_100_TX);
    415 		PRINT("100baseTX");
    416 	}
    417 	if (sc->mii_capabilities & BMSR_100TXFDX) {
    418 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
    419 		    MII_MEDIA_100_TX_FDX);
    420 		PRINT("100baseTX-FDX");
    421 	}
    422 	if (sc->mii_capabilities & BMSR_100T4) {
    423 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
    424 		    MII_MEDIA_100_T4);
    425 		PRINT("100baseT4");
    426 	}
    427 
    428 	if (sc->mii_extcapabilities & EXTSR_MEDIAMASK) {
    429 		/*
    430 		 * XXX Right now only handle 1000SX and 1000TX.  Need
    431 		 * XXX to handle 1000LX and 1000CX some how.
    432 		 *
    433 		 * Note since it can take 5 seconds to auto-negotiate
    434 		 * a gigabit link, we make anegticks 10 seconds for
    435 		 * all the gigabit media types.
    436 		 */
    437 		if (sc->mii_extcapabilities & EXTSR_1000XHDX) {
    438 			sc->mii_anegticks = 10;
    439 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0,
    440 			    sc->mii_inst), MII_MEDIA_1000_X);
    441 			PRINT("1000baseSX");
    442 		}
    443 		if (sc->mii_extcapabilities & EXTSR_1000XFDX) {
    444 			sc->mii_anegticks = 10;
    445 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX,
    446 			    sc->mii_inst), MII_MEDIA_1000_X_FDX);
    447 			PRINT("1000baseSX-FDX");
    448 		}
    449 
    450 		/*
    451 		 * 1000baseT media needs to be able to manipulate
    452 		 * master/slave mode.  We set IFM_ETH_MASTER in
    453 		 * the "don't care mask" and filter it out when
    454 		 * the media is set.
    455 		 *
    456 		 * All 1000baseT PHYs have a 1000baseT control register.
    457 		 */
    458 		if (sc->mii_extcapabilities & EXTSR_1000THDX) {
    459 			sc->mii_anegticks = 10;
    460 			sc->mii_flags |= MIIF_HAVE_GTCR;
    461 			mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
    462 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_TX, 0,
    463 			    sc->mii_inst), MII_MEDIA_1000_T);
    464 			PRINT("1000baseTX");
    465 		}
    466 		if (sc->mii_extcapabilities & EXTSR_1000TFDX) {
    467 			sc->mii_anegticks = 10;
    468 			sc->mii_flags |= MIIF_HAVE_GTCR;
    469 			mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
    470 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_TX, IFM_FDX,
    471 			    sc->mii_inst), MII_MEDIA_1000_T_FDX);
    472 			PRINT("1000baseTX-FDX");
    473 		}
    474 	}
    475 
    476 	if (sc->mii_capabilities & BMSR_ANEG) {
    477 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
    478 		    MII_NMEDIA);	/* intentionally invalid index */
    479 		PRINT("auto");
    480 	}
    481 #undef ADD
    482 #undef PRINT
    483 }
    484 
    485 void
    486 mii_phy_delete_media(sc)
    487 	struct mii_softc *sc;
    488 {
    489 	struct mii_data *mii = sc->mii_pdata;
    490 
    491 	ifmedia_delete_instance(&mii->mii_media, sc->mii_inst);
    492 }
    493 
    494 int
    495 mii_phy_activate(self, act)
    496 	struct device *self;
    497 	enum devact act;
    498 {
    499 	int rv = 0;
    500 
    501 	switch (act) {
    502 	case DVACT_ACTIVATE:
    503 		rv = EOPNOTSUPP;
    504 		break;
    505 
    506 	case DVACT_DEACTIVATE:
    507 		/* Nothing special to do. */
    508 		break;
    509 	}
    510 
    511 	return (rv);
    512 }
    513 
    514 int
    515 mii_phy_detach(self, flags)
    516 	struct device *self;
    517 	int flags;
    518 {
    519 	struct mii_softc *sc = (void *) self;
    520 
    521 	if (sc->mii_flags & MIIF_DOINGAUTO)
    522 		callout_stop(&sc->mii_nway_ch);
    523 
    524 	mii_phy_delete_media(sc);
    525 
    526 	return (0);
    527 }
    528 
    529 const struct mii_phydesc *
    530 mii_phy_match(const struct mii_attach_args *ma, const struct mii_phydesc *mpd)
    531 {
    532 
    533 	for (; mpd->mpd_name != NULL; mpd++) {
    534 		if (MII_OUI(ma->mii_id1, ma->mii_id2) == mpd->mpd_oui &&
    535 		    MII_MODEL(ma->mii_id2) == mpd->mpd_model)
    536 			return (mpd);
    537 	}
    538 	return (NULL);
    539 }
    540