Home | History | Annotate | Line # | Download | only in mii
mii_physubr.c revision 1.22
      1 /*	$NetBSD: mii_physubr.c,v 1.22 2001/05/31 16:02:29 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 1999, 2000 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  * XXX 802.3 doesn't specify ANAR or ANLPAR bits for 1000base.
     62  */
     63 const struct mii_media mii_media_table[] = {
     64 	{ BMCR_ISO,		ANAR_CSMA },		/* None */
     65 	{ BMCR_S10,		ANAR_CSMA|ANAR_10 },	/* 10baseT */
     66 	{ BMCR_S10|BMCR_FDX,	ANAR_CSMA|ANAR_10_FD },	/* 10baseT-FDX */
     67 	{ BMCR_S100,		ANAR_CSMA|ANAR_T4 },	/* 100baseT4 */
     68 	{ BMCR_S100,		ANAR_CSMA|ANAR_TX },	/* 100baseTX */
     69 	{ BMCR_S100|BMCR_FDX,	ANAR_CSMA|ANAR_TX_FD },	/* 100baseTX-FDX */
     70 	{ BMCR_S1000,		ANAR_CSMA },		/* 1000base */
     71 	{ BMCR_S1000|BMCR_FDX,	ANAR_CSMA },		/* 1000base-FDX */
     72 };
     73 
     74 void	mii_phy_auto_timeout __P((void *));
     75 
     76 void
     77 mii_phy_setmedia(sc)
     78 	struct mii_softc *sc;
     79 {
     80 	struct mii_data *mii = sc->mii_pdata;
     81 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
     82 	int bmcr, anar;
     83 
     84 	if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO) {
     85 		if ((PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) == 0)
     86 			(void) mii_phy_auto(sc, 1);
     87 		return;
     88 	}
     89 
     90 	/*
     91 	 * Table index is stored in the media entry.
     92 	 */
     93 
     94 #ifdef DIAGNOSTIC
     95 	if (ife->ifm_data < 0 || ife->ifm_data >= MII_NMEDIA)
     96 		panic("mii_phy_setmedia");
     97 #endif
     98 
     99 	anar = mii_media_table[ife->ifm_data].mm_anar;
    100 	bmcr = mii_media_table[ife->ifm_data].mm_bmcr;
    101 
    102 	if (ife->ifm_media & IFM_LOOP)
    103 		bmcr |= BMCR_LOOP;
    104 
    105 	PHY_WRITE(sc, MII_ANAR, anar);
    106 	PHY_WRITE(sc, MII_BMCR, bmcr);
    107 }
    108 
    109 int
    110 mii_phy_auto(sc, waitfor)
    111 	struct mii_softc *sc;
    112 	int waitfor;
    113 {
    114 	int bmsr, i;
    115 
    116 	if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
    117 		PHY_WRITE(sc, MII_ANAR,
    118 		    BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) | ANAR_CSMA);
    119 		PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
    120 	}
    121 
    122 	if (waitfor) {
    123 		/* Wait 500ms for it to complete. */
    124 		for (i = 0; i < 500; i++) {
    125 			if ((bmsr = PHY_READ(sc, MII_BMSR)) & BMSR_ACOMP)
    126 				return (0);
    127 			delay(1000);
    128 		}
    129 
    130 		/*
    131 		 * Don't need to worry about clearing MIIF_DOINGAUTO.
    132 		 * If that's set, a timeout is pending, and it will
    133 		 * clear the flag.
    134 		 */
    135 		return (EIO);
    136 	}
    137 
    138 	/*
    139 	 * Just let it finish asynchronously.  This is for the benefit of
    140 	 * the tick handler driving autonegotiation.  Don't want 500ms
    141 	 * delays all the time while the system is running!
    142 	 */
    143 	if (sc->mii_flags & MIIF_AUTOTSLEEP) {
    144 		sc->mii_flags |= MIIF_DOINGAUTO;
    145 		tsleep(&sc->mii_flags, PZERO, "miiaut", hz >> 1);
    146 		mii_phy_auto_timeout(sc);
    147 	} else if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
    148 		sc->mii_flags |= MIIF_DOINGAUTO;
    149 		callout_reset(&sc->mii_nway_ch, hz >> 1,
    150 		    mii_phy_auto_timeout, sc);
    151 	}
    152 	return (EJUSTRETURN);
    153 }
    154 
    155 void
    156 mii_phy_auto_timeout(arg)
    157 	void *arg;
    158 {
    159 	struct mii_softc *sc = arg;
    160 	int s, bmsr;
    161 
    162 	if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0)
    163 		return;
    164 
    165 	s = splnet();
    166 	sc->mii_flags &= ~MIIF_DOINGAUTO;
    167 	bmsr = PHY_READ(sc, MII_BMSR);
    168 
    169 	/* Update the media status. */
    170 	(void) PHY_SERVICE(sc, sc->mii_pdata, MII_POLLSTAT);
    171 	splx(s);
    172 }
    173 
    174 int
    175 mii_phy_tick(sc)
    176 	struct mii_softc *sc;
    177 {
    178 	struct mii_data *mii = sc->mii_pdata;
    179 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    180 	int reg;
    181 
    182 	/* Just bail now if the interface is down. */
    183 	if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    184 		return (EJUSTRETURN);
    185 
    186 	/*
    187 	 * If we're not doing autonegotiation, we don't need to do
    188 	 * any extra work here.  However, we need to check the link
    189 	 * status so we can generate an announcement if the status
    190 	 * changes.
    191 	 */
    192 	if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
    193 		return (0);
    194 
    195 	/* Read the status register twice; BMSR_LINK is latch-low. */
    196 	reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
    197 	if (reg & BMSR_LINK) {
    198 		/*
    199 		 * See above.
    200 		 */
    201 		return (0);
    202 	}
    203 
    204 	/*
    205 	 * Only retry autonegotiation every N seconds.
    206 	 */
    207 	KASSERT(sc->mii_anegticks != 0);
    208 	if (++sc->mii_ticks != sc->mii_anegticks)
    209 		return (EJUSTRETURN);
    210 
    211 	sc->mii_ticks = 0;
    212 	PHY_RESET(sc);
    213 
    214 	if (mii_phy_auto(sc, 0) == EJUSTRETURN)
    215 		return (EJUSTRETURN);
    216 
    217 	/*
    218 	 * Might need to generate a status message if autonegotiation
    219 	 * failed.
    220 	 */
    221 	return (0);
    222 }
    223 
    224 void
    225 mii_phy_reset(sc)
    226 	struct mii_softc *sc;
    227 {
    228 	int reg, i;
    229 
    230 	if (sc->mii_flags & MIIF_NOISOLATE)
    231 		reg = BMCR_RESET;
    232 	else
    233 		reg = BMCR_RESET | BMCR_ISO;
    234 	PHY_WRITE(sc, MII_BMCR, reg);
    235 
    236 	/* Wait 100ms for it to complete. */
    237 	for (i = 0; i < 100; i++) {
    238 		reg = PHY_READ(sc, MII_BMCR);
    239 		if ((reg & BMCR_RESET) == 0)
    240 			break;
    241 		delay(1000);
    242 	}
    243 
    244 	if (sc->mii_inst != 0 && ((sc->mii_flags & MIIF_NOISOLATE) == 0))
    245 		PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    246 }
    247 
    248 void
    249 mii_phy_down(sc)
    250 	struct mii_softc *sc;
    251 {
    252 
    253 	if (sc->mii_flags & MIIF_DOINGAUTO) {
    254 		sc->mii_flags &= ~MIIF_DOINGAUTO;
    255 		callout_stop(&sc->mii_nway_ch);
    256 	}
    257 }
    258 
    259 void
    260 mii_phy_status(sc)
    261 	struct mii_softc *sc;
    262 {
    263 
    264 	PHY_STATUS(sc);
    265 }
    266 
    267 void
    268 mii_phy_update(sc, cmd)
    269 	struct mii_softc *sc;
    270 	int cmd;
    271 {
    272 	struct mii_data *mii = sc->mii_pdata;
    273 
    274 	if (sc->mii_media_active != mii->mii_media_active ||
    275 	    sc->mii_media_status != mii->mii_media_status ||
    276 	    cmd == MII_MEDIACHG) {
    277 		(*mii->mii_statchg)(sc->mii_dev.dv_parent);
    278 		mii_phy_statusmsg(sc);
    279 		sc->mii_media_active = mii->mii_media_active;
    280 		sc->mii_media_status = mii->mii_media_status;
    281 	}
    282 }
    283 
    284 void
    285 mii_phy_statusmsg(sc)
    286 	struct mii_softc *sc;
    287 {
    288 	struct mii_data *mii = sc->mii_pdata;
    289 	struct ifnet *ifp = mii->mii_ifp;
    290 	int s, baudrate, link_state, announce = 0;
    291 
    292 	if (mii->mii_media_status & IFM_AVALID) {
    293 		if (mii->mii_media_status & IFM_ACTIVE)
    294 			link_state = LINK_STATE_UP;
    295 		else
    296 			link_state = LINK_STATE_DOWN;
    297 	} else
    298 		link_state = LINK_STATE_UNKNOWN;
    299 
    300 	baudrate = ifmedia_baudrate(mii->mii_media_active);
    301 
    302 	if (link_state != ifp->if_link_state) {
    303 		ifp->if_link_state = link_state;
    304 		/*
    305 		 * XXX Right here we'd like to notify protocols
    306 		 * XXX that the link status has changed, so that
    307 		 * XXX e.g. Duplicate Address Detection can restart.
    308 		 */
    309 		announce = 1;
    310 	}
    311 
    312 	if (baudrate != ifp->if_baudrate) {
    313 		ifp->if_baudrate = baudrate;
    314 		announce = 1;
    315 	}
    316 
    317 	if (announce) {
    318 		s = splnet();
    319 		rt_ifmsg(ifp);
    320 		splx(s);
    321 	}
    322 }
    323 
    324 /*
    325  * Initialize generic PHY media based on BMSR, called when a PHY is
    326  * attached.  We expect to be set up to print a comma-separated list
    327  * of media names.  Does not print a newline.
    328  */
    329 void
    330 mii_phy_add_media(sc)
    331 	struct mii_softc *sc;
    332 {
    333 	struct mii_data *mii = sc->mii_pdata;
    334 	const char *sep = "";
    335 
    336 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
    337 #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
    338 
    339 	if ((sc->mii_flags & MIIF_NOISOLATE) == 0)
    340 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
    341 		    MII_MEDIA_NONE);
    342 
    343 	if (sc->mii_capabilities & BMSR_10THDX) {
    344 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
    345 		    MII_MEDIA_10_T);
    346 		PRINT("10baseT");
    347 	}
    348 	if (sc->mii_capabilities & BMSR_10TFDX) {
    349 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
    350 		    MII_MEDIA_10_T_FDX);
    351 		PRINT("10baseT-FDX");
    352 	}
    353 	if (sc->mii_capabilities & BMSR_100TXHDX) {
    354 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
    355 		    MII_MEDIA_100_TX);
    356 		PRINT("100baseTX");
    357 	}
    358 	if (sc->mii_capabilities & BMSR_100TXFDX) {
    359 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
    360 		    MII_MEDIA_100_TX_FDX);
    361 		PRINT("100baseTX-FDX");
    362 	}
    363 	if (sc->mii_capabilities & BMSR_100T4) {
    364 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
    365 		    MII_MEDIA_100_T4);
    366 		PRINT("100baseT4");
    367 	}
    368 
    369 	if (sc->mii_extcapabilities & EXTSR_MEDIAMASK) {
    370 		/*
    371 		 * XXX Right now only handle 1000SX and 1000TX.  Need
    372 		 * XXX to handle 1000LX and 1000CX some how.
    373 		 *
    374 		 * Note since it can take 5 seconds to auto-negotiate
    375 		 * a gigabit link, we make anegticks 10 seconds for
    376 		 * all the gigabit media types.
    377 		 */
    378 		if (sc->mii_extcapabilities & EXTSR_1000XHDX) {
    379 			sc->mii_anegticks = 10;
    380 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0,
    381 			    sc->mii_inst), MII_MEDIA_1000);
    382 			PRINT("1000baseSX");
    383 		}
    384 		if (sc->mii_extcapabilities & EXTSR_1000XFDX) {
    385 			sc->mii_anegticks = 10;
    386 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX,
    387 			    sc->mii_inst), MII_MEDIA_1000_FDX);
    388 			PRINT("1000baseSX-FDX");
    389 		}
    390 		if (sc->mii_extcapabilities & EXTSR_1000THDX) {
    391 			sc->mii_anegticks = 10;
    392 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_TX, 0,
    393 			    sc->mii_inst), MII_MEDIA_1000);
    394 			PRINT("1000baseTX");
    395 		}
    396 		if (sc->mii_extcapabilities & EXTSR_1000TFDX) {
    397 			sc->mii_anegticks = 10;
    398 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_TX, IFM_FDX,
    399 			    sc->mii_inst), MII_MEDIA_1000_FDX);
    400 			PRINT("1000baseTX-FDX");
    401 		}
    402 	}
    403 
    404 	if (sc->mii_capabilities & BMSR_ANEG) {
    405 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
    406 		    MII_NMEDIA);	/* intentionally invalid index */
    407 		PRINT("auto");
    408 	}
    409 #undef ADD
    410 #undef PRINT
    411 }
    412 
    413 void
    414 mii_phy_delete_media(sc)
    415 	struct mii_softc *sc;
    416 {
    417 	struct mii_data *mii = sc->mii_pdata;
    418 
    419 	ifmedia_delete_instance(&mii->mii_media, sc->mii_inst);
    420 }
    421 
    422 int
    423 mii_phy_activate(self, act)
    424 	struct device *self;
    425 	enum devact act;
    426 {
    427 	int rv = 0;
    428 
    429 	switch (act) {
    430 	case DVACT_ACTIVATE:
    431 		rv = EOPNOTSUPP;
    432 		break;
    433 
    434 	case DVACT_DEACTIVATE:
    435 		/* Nothing special to do. */
    436 		break;
    437 	}
    438 
    439 	return (rv);
    440 }
    441 
    442 int
    443 mii_phy_detach(self, flags)
    444 	struct device *self;
    445 	int flags;
    446 {
    447 	struct mii_softc *sc = (void *) self;
    448 
    449 	if (sc->mii_flags & MIIF_DOINGAUTO)
    450 		callout_stop(&sc->mii_nway_ch);
    451 
    452 	mii_phy_delete_media(sc);
    453 
    454 	return (0);
    455 }
    456