Home | History | Annotate | Line # | Download | only in mii
mii_physubr.c revision 1.68.2.1
      1  1.68.2.1     rmind /*	$NetBSD: mii_physubr.c,v 1.68.2.1 2010/07/03 01:19:36 rmind Exp $	*/
      2       1.1   thorpej 
      3       1.1   thorpej /*-
      4      1.23   thorpej  * Copyright (c) 1998, 1999, 2000, 2001 The NetBSD Foundation, Inc.
      5       1.1   thorpej  * All rights reserved.
      6       1.1   thorpej  *
      7       1.1   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1   thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9       1.1   thorpej  * NASA Ames Research Center.
     10       1.1   thorpej  *
     11       1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     12       1.1   thorpej  * modification, are permitted provided that the following conditions
     13       1.1   thorpej  * are met:
     14       1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     15       1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     16       1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17       1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     18       1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     19       1.1   thorpej  *
     20       1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21       1.1   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22       1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23       1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24       1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25       1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26       1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27       1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28       1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29       1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30       1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     31       1.1   thorpej  */
     32       1.1   thorpej 
     33       1.1   thorpej /*
     34       1.1   thorpej  * Subroutines common to all PHYs.
     35       1.1   thorpej  */
     36      1.30     lukem 
     37      1.30     lukem #include <sys/cdefs.h>
     38  1.68.2.1     rmind __KERNEL_RCSID(0, "$NetBSD: mii_physubr.c,v 1.68.2.1 2010/07/03 01:19:36 rmind Exp $");
     39       1.1   thorpej 
     40       1.1   thorpej #include <sys/param.h>
     41       1.1   thorpej #include <sys/device.h>
     42       1.1   thorpej #include <sys/systm.h>
     43       1.3   thorpej #include <sys/kernel.h>
     44       1.1   thorpej #include <sys/socket.h>
     45       1.3   thorpej #include <sys/errno.h>
     46  1.68.2.1     rmind #include <sys/module.h>
     47      1.19  augustss #include <sys/proc.h>
     48       1.1   thorpej 
     49       1.1   thorpej #include <net/if.h>
     50       1.1   thorpej #include <net/if_media.h>
     51      1.15   thorpej #include <net/route.h>
     52       1.1   thorpej 
     53       1.1   thorpej #include <dev/mii/mii.h>
     54       1.1   thorpej #include <dev/mii/miivar.h>
     55       1.1   thorpej 
     56  1.68.2.1     rmind const char *(*mii_get_descr)(int, int) = mii_get_descr_stub;
     57  1.68.2.1     rmind 
     58  1.68.2.1     rmind int mii_verbose_loaded = 0;
     59  1.68.2.1     rmind 
     60  1.68.2.1     rmind const char *mii_get_descr_stub(int oui, int model)
     61  1.68.2.1     rmind {
     62  1.68.2.1     rmind 	mii_load_verbose();
     63  1.68.2.1     rmind 	if (mii_verbose_loaded)
     64  1.68.2.1     rmind 		return mii_get_descr(oui, model);
     65  1.68.2.1     rmind 	else
     66  1.68.2.1     rmind 		return NULL;
     67  1.68.2.1     rmind }
     68  1.68.2.1     rmind 
     69  1.68.2.1     rmind /*
     70  1.68.2.1     rmind  * Routine to load the miiverbose kernel module as needed
     71  1.68.2.1     rmind  */
     72  1.68.2.1     rmind void mii_load_verbose(void)
     73  1.68.2.1     rmind {
     74  1.68.2.1     rmind 	if (mii_verbose_loaded)
     75  1.68.2.1     rmind 		return;
     76  1.68.2.1     rmind 
     77  1.68.2.1     rmind 	mutex_enter(&module_lock);
     78  1.68.2.1     rmind 	if (module_autoload("miiverbose", MODULE_CLASS_MISC) ==0)
     79  1.68.2.1     rmind 		mii_verbose_loaded++;
     80  1.68.2.1     rmind 	mutex_exit(&module_lock);
     81  1.68.2.1     rmind }
     82  1.68.2.1     rmind 
     83      1.46     joerg static void mii_phy_statusmsg(struct mii_softc *);
     84      1.46     joerg 
     85       1.6   thorpej /*
     86       1.6   thorpej  * Media to register setting conversion table.  Order matters.
     87       1.6   thorpej  */
     88      1.43   thorpej static const struct mii_media mii_media_table[MII_NMEDIA] = {
     89      1.23   thorpej 	/* None */
     90      1.23   thorpej 	{ BMCR_ISO,		ANAR_CSMA,
     91      1.23   thorpej 	  0, },
     92      1.23   thorpej 
     93      1.23   thorpej 	/* 10baseT */
     94      1.23   thorpej 	{ BMCR_S10,		ANAR_CSMA|ANAR_10,
     95      1.23   thorpej 	  0, },
     96      1.23   thorpej 
     97      1.23   thorpej 	/* 10baseT-FDX */
     98      1.23   thorpej 	{ BMCR_S10|BMCR_FDX,	ANAR_CSMA|ANAR_10_FD,
     99      1.23   thorpej 	  0, },
    100      1.23   thorpej 
    101      1.23   thorpej 	/* 100baseT4 */
    102      1.23   thorpej 	{ BMCR_S100,		ANAR_CSMA|ANAR_T4,
    103      1.23   thorpej 	  0, },
    104      1.23   thorpej 
    105      1.23   thorpej 	/* 100baseTX */
    106      1.23   thorpej 	{ BMCR_S100,		ANAR_CSMA|ANAR_TX,
    107      1.23   thorpej 	  0, },
    108      1.23   thorpej 
    109      1.23   thorpej 	/* 100baseTX-FDX */
    110      1.23   thorpej 	{ BMCR_S100|BMCR_FDX,	ANAR_CSMA|ANAR_TX_FD,
    111      1.23   thorpej 	  0, },
    112      1.23   thorpej 
    113      1.23   thorpej 	/* 1000baseX */
    114      1.23   thorpej 	{ BMCR_S1000,		ANAR_CSMA,
    115      1.23   thorpej 	  0, },
    116      1.23   thorpej 
    117      1.23   thorpej 	/* 1000baseX-FDX */
    118      1.23   thorpej 	{ BMCR_S1000|BMCR_FDX,	ANAR_CSMA,
    119      1.23   thorpej 	  0, },
    120      1.23   thorpej 
    121      1.23   thorpej 	/* 1000baseT */
    122      1.23   thorpej 	{ BMCR_S1000,		ANAR_CSMA,
    123      1.23   thorpej 	  GTCR_ADV_1000THDX },
    124      1.23   thorpej 
    125      1.23   thorpej 	/* 1000baseT-FDX */
    126      1.23   thorpej 	{ BMCR_S1000,		ANAR_CSMA,
    127      1.23   thorpej 	  GTCR_ADV_1000TFDX },
    128       1.6   thorpej };
    129       1.6   thorpej 
    130      1.43   thorpej static void	mii_phy_auto_timeout(void *);
    131       1.3   thorpej 
    132       1.6   thorpej void
    133      1.29   thorpej mii_phy_setmedia(struct mii_softc *sc)
    134       1.6   thorpej {
    135       1.6   thorpej 	struct mii_data *mii = sc->mii_pdata;
    136       1.6   thorpej 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    137      1.23   thorpej 	int bmcr, anar, gtcr;
    138      1.12   thorpej 
    139      1.13   thorpej 	if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO) {
    140      1.41   thorpej 		/*
    141      1.41   thorpej 		 * Force renegotiation if MIIF_DOPAUSE.
    142      1.41   thorpej 		 *
    143      1.41   thorpej 		 * XXX This is only necessary because many NICs don't
    144      1.41   thorpej 		 * XXX advertise PAUSE capabilities at boot time.  Maybe
    145      1.41   thorpej 		 * XXX we should force this only once?
    146      1.41   thorpej 		 */
    147      1.35      matt 		if ((PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN) == 0 ||
    148      1.38   thorpej 		    (sc->mii_flags & (MIIF_FORCEANEG|MIIF_DOPAUSE)))
    149      1.14   thorpej 			(void) mii_phy_auto(sc, 1);
    150      1.13   thorpej 		return;
    151      1.13   thorpej 	}
    152       1.6   thorpej 
    153       1.6   thorpej 	/*
    154       1.6   thorpej 	 * Table index is stored in the media entry.
    155       1.6   thorpej 	 */
    156       1.6   thorpej 
    157       1.6   thorpej #ifdef DIAGNOSTIC
    158      1.50  christos 	if (/* ife->ifm_data < 0 || */ ife->ifm_data >= MII_NMEDIA)
    159       1.6   thorpej 		panic("mii_phy_setmedia");
    160       1.6   thorpej #endif
    161       1.6   thorpej 
    162       1.6   thorpej 	anar = mii_media_table[ife->ifm_data].mm_anar;
    163       1.6   thorpej 	bmcr = mii_media_table[ife->ifm_data].mm_bmcr;
    164      1.23   thorpej 	gtcr = mii_media_table[ife->ifm_data].mm_gtcr;
    165      1.23   thorpej 
    166      1.23   thorpej 	if (mii->mii_media.ifm_media & IFM_ETH_MASTER) {
    167      1.23   thorpej 		switch (IFM_SUBTYPE(ife->ifm_media)) {
    168      1.25     bjh21 		case IFM_1000_T:
    169      1.23   thorpej 			gtcr |= GTCR_MAN_MS|GTCR_ADV_MS;
    170      1.23   thorpej 			break;
    171      1.23   thorpej 
    172      1.23   thorpej 		default:
    173      1.23   thorpej 			panic("mii_phy_setmedia: MASTER on wrong media");
    174      1.23   thorpej 		}
    175      1.23   thorpej 	}
    176       1.6   thorpej 
    177      1.38   thorpej 	if (mii->mii_media.ifm_media & IFM_FLOW) {
    178      1.38   thorpej 		if (sc->mii_flags & MIIF_IS_1000X)
    179      1.38   thorpej 			anar |= ANAR_X_PAUSE_SYM | ANAR_X_PAUSE_ASYM;
    180      1.38   thorpej 		else {
    181      1.38   thorpej 			anar |= ANAR_FC;
    182      1.38   thorpej 			/* XXX Only 1000BASE-T has PAUSE_ASYM? */
    183      1.38   thorpej 			if ((sc->mii_flags & MIIF_HAVE_GTCR) &&
    184      1.38   thorpej 			    (sc->mii_extcapabilities &
    185      1.38   thorpej 			     (EXTSR_1000THDX|EXTSR_1000TFDX)))
    186      1.38   thorpej 				anar |= ANAR_X_PAUSE_ASYM;
    187      1.38   thorpej 		}
    188      1.38   thorpej 	}
    189      1.38   thorpej 
    190       1.6   thorpej 	if (ife->ifm_media & IFM_LOOP)
    191       1.6   thorpej 		bmcr |= BMCR_LOOP;
    192       1.6   thorpej 
    193       1.6   thorpej 	PHY_WRITE(sc, MII_ANAR, anar);
    194       1.6   thorpej 	PHY_WRITE(sc, MII_BMCR, bmcr);
    195      1.23   thorpej 	if (sc->mii_flags & MIIF_HAVE_GTCR)
    196      1.23   thorpej 		PHY_WRITE(sc, MII_100T2CR, gtcr);
    197      1.23   thorpej }
    198      1.23   thorpej 
    199       1.1   thorpej int
    200      1.29   thorpej mii_phy_auto(struct mii_softc *sc, int waitfor)
    201       1.1   thorpej {
    202      1.31       uwe 	int i;
    203       1.1   thorpej 
    204       1.6   thorpej 	if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
    205      1.26   thorpej 		/*
    206      1.26   thorpej 		 * Check for 1000BASE-X.  Autonegotiation is a bit
    207      1.26   thorpej 		 * different on such devices.
    208      1.26   thorpej 		 */
    209      1.27   thorpej 		if (sc->mii_flags & MIIF_IS_1000X) {
    210      1.27   thorpej 			uint16_t anar = 0;
    211      1.27   thorpej 
    212      1.27   thorpej 			if (sc->mii_extcapabilities & EXTSR_1000XFDX)
    213      1.27   thorpej 				anar |= ANAR_X_FD;
    214      1.27   thorpej 			if (sc->mii_extcapabilities & EXTSR_1000XHDX)
    215      1.27   thorpej 				anar |= ANAR_X_HD;
    216      1.27   thorpej 
    217      1.27   thorpej 			if (sc->mii_flags & MIIF_DOPAUSE) {
    218      1.27   thorpej 				/* XXX Asymmetric vs. symmetric? */
    219      1.27   thorpej 				anar |= ANLPAR_X_PAUSE_TOWARDS;
    220      1.27   thorpej 			}
    221      1.27   thorpej 
    222      1.27   thorpej 			PHY_WRITE(sc, MII_ANAR, anar);
    223      1.27   thorpej 		} else {
    224      1.27   thorpej 			uint16_t anar;
    225      1.27   thorpej 
    226      1.27   thorpej 			anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) |
    227      1.27   thorpej 			    ANAR_CSMA;
    228      1.38   thorpej 			if (sc->mii_flags & MIIF_DOPAUSE) {
    229      1.27   thorpej 				anar |= ANAR_FC;
    230      1.38   thorpej 				/* XXX Only 1000BASE-T has PAUSE_ASYM? */
    231      1.38   thorpej 				if ((sc->mii_flags & MIIF_HAVE_GTCR) &&
    232      1.38   thorpej 				    (sc->mii_extcapabilities &
    233      1.38   thorpej 				     (EXTSR_1000THDX|EXTSR_1000TFDX)))
    234      1.38   thorpej 					anar |= ANAR_X_PAUSE_ASYM;
    235      1.38   thorpej 			}
    236      1.27   thorpej 			PHY_WRITE(sc, MII_ANAR, anar);
    237      1.27   thorpej 			if (sc->mii_flags & MIIF_HAVE_GTCR) {
    238      1.27   thorpej 				uint16_t gtcr = 0;
    239      1.27   thorpej 
    240      1.27   thorpej 				if (sc->mii_extcapabilities & EXTSR_1000TFDX)
    241      1.27   thorpej 					gtcr |= GTCR_ADV_1000TFDX;
    242      1.27   thorpej 				if (sc->mii_extcapabilities & EXTSR_1000THDX)
    243      1.27   thorpej 					gtcr |= GTCR_ADV_1000THDX;
    244      1.26   thorpej 
    245      1.27   thorpej 				PHY_WRITE(sc, MII_100T2CR, gtcr);
    246      1.27   thorpej 			}
    247      1.26   thorpej 		}
    248       1.6   thorpej 		PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
    249       1.3   thorpej 	}
    250       1.3   thorpej 
    251       1.3   thorpej 	if (waitfor) {
    252       1.3   thorpej 		/* Wait 500ms for it to complete. */
    253       1.3   thorpej 		for (i = 0; i < 500; i++) {
    254      1.31       uwe 			if (PHY_READ(sc, MII_BMSR) & BMSR_ACOMP)
    255       1.3   thorpej 				return (0);
    256       1.3   thorpej 			delay(1000);
    257       1.3   thorpej 		}
    258       1.3   thorpej 
    259       1.3   thorpej 		/*
    260       1.3   thorpej 		 * Don't need to worry about clearing MIIF_DOINGAUTO.
    261       1.3   thorpej 		 * If that's set, a timeout is pending, and it will
    262       1.3   thorpej 		 * clear the flag.
    263       1.3   thorpej 		 */
    264       1.3   thorpej 		return (EIO);
    265       1.1   thorpej 	}
    266       1.3   thorpej 
    267       1.3   thorpej 	/*
    268       1.3   thorpej 	 * Just let it finish asynchronously.  This is for the benefit of
    269       1.3   thorpej 	 * the tick handler driving autonegotiation.  Don't want 500ms
    270       1.3   thorpej 	 * delays all the time while the system is running!
    271       1.3   thorpej 	 */
    272      1.19  augustss 	if (sc->mii_flags & MIIF_AUTOTSLEEP) {
    273      1.19  augustss 		sc->mii_flags |= MIIF_DOINGAUTO;
    274      1.19  augustss 		tsleep(&sc->mii_flags, PZERO, "miiaut", hz >> 1);
    275      1.19  augustss 		mii_phy_auto_timeout(sc);
    276      1.19  augustss 	} else if ((sc->mii_flags & MIIF_DOINGAUTO) == 0) {
    277       1.6   thorpej 		sc->mii_flags |= MIIF_DOINGAUTO;
    278      1.17   thorpej 		callout_reset(&sc->mii_nway_ch, hz >> 1,
    279      1.17   thorpej 		    mii_phy_auto_timeout, sc);
    280       1.3   thorpej 	}
    281       1.3   thorpej 	return (EJUSTRETURN);
    282       1.3   thorpej }
    283       1.3   thorpej 
    284      1.43   thorpej static void
    285      1.29   thorpej mii_phy_auto_timeout(void *arg)
    286       1.3   thorpej {
    287       1.6   thorpej 	struct mii_softc *sc = arg;
    288      1.31       uwe 	int s;
    289       1.3   thorpej 
    290      1.60   xtraeme 	if (!device_is_active(sc->mii_dev))
    291       1.9   thorpej 		return;
    292       1.9   thorpej 
    293       1.3   thorpej 	s = splnet();
    294       1.6   thorpej 	sc->mii_flags &= ~MIIF_DOINGAUTO;
    295       1.3   thorpej 
    296       1.3   thorpej 	/* Update the media status. */
    297      1.18   thorpej 	(void) PHY_SERVICE(sc, sc->mii_pdata, MII_POLLSTAT);
    298       1.3   thorpej 	splx(s);
    299       1.2   thorpej }
    300       1.2   thorpej 
    301      1.15   thorpej int
    302      1.29   thorpej mii_phy_tick(struct mii_softc *sc)
    303      1.15   thorpej {
    304      1.15   thorpej 	struct mii_data *mii = sc->mii_pdata;
    305      1.15   thorpej 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    306      1.15   thorpej 	int reg;
    307      1.15   thorpej 
    308      1.15   thorpej 	/* Just bail now if the interface is down. */
    309      1.15   thorpej 	if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    310      1.15   thorpej 		return (EJUSTRETURN);
    311      1.15   thorpej 
    312      1.15   thorpej 	/*
    313      1.15   thorpej 	 * If we're not doing autonegotiation, we don't need to do
    314      1.15   thorpej 	 * any extra work here.  However, we need to check the link
    315      1.15   thorpej 	 * status so we can generate an announcement if the status
    316      1.15   thorpej 	 * changes.
    317      1.15   thorpej 	 */
    318      1.15   thorpej 	if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
    319      1.15   thorpej 		return (0);
    320      1.15   thorpej 
    321      1.15   thorpej 	/* Read the status register twice; BMSR_LINK is latch-low. */
    322      1.15   thorpej 	reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
    323      1.15   thorpej 	if (reg & BMSR_LINK) {
    324      1.15   thorpej 		/*
    325      1.15   thorpej 		 * See above.
    326      1.15   thorpej 		 */
    327      1.15   thorpej 		return (0);
    328      1.15   thorpej 	}
    329      1.15   thorpej 
    330      1.15   thorpej 	/*
    331      1.22   thorpej 	 * Only retry autonegotiation every N seconds.
    332      1.15   thorpej 	 */
    333      1.22   thorpej 	KASSERT(sc->mii_anegticks != 0);
    334      1.53  christos 	if (++sc->mii_ticks <= sc->mii_anegticks)
    335      1.15   thorpej 		return (EJUSTRETURN);
    336      1.15   thorpej 
    337      1.15   thorpej 	sc->mii_ticks = 0;
    338      1.18   thorpej 	PHY_RESET(sc);
    339      1.15   thorpej 
    340      1.15   thorpej 	if (mii_phy_auto(sc, 0) == EJUSTRETURN)
    341      1.15   thorpej 		return (EJUSTRETURN);
    342      1.15   thorpej 
    343      1.15   thorpej 	/*
    344      1.15   thorpej 	 * Might need to generate a status message if autonegotiation
    345      1.15   thorpej 	 * failed.
    346      1.15   thorpej 	 */
    347      1.15   thorpej 	return (0);
    348      1.15   thorpej }
    349      1.15   thorpej 
    350       1.2   thorpej void
    351      1.29   thorpej mii_phy_reset(struct mii_softc *sc)
    352       1.2   thorpej {
    353       1.2   thorpej 	int reg, i;
    354       1.2   thorpej 
    355       1.6   thorpej 	if (sc->mii_flags & MIIF_NOISOLATE)
    356       1.2   thorpej 		reg = BMCR_RESET;
    357       1.2   thorpej 	else
    358       1.2   thorpej 		reg = BMCR_RESET | BMCR_ISO;
    359       1.6   thorpej 	PHY_WRITE(sc, MII_BMCR, reg);
    360       1.2   thorpej 
    361      1.37    briggs 	/* Wait another 100ms for it to complete. */
    362       1.2   thorpej 	for (i = 0; i < 100; i++) {
    363      1.44     perry 		reg = PHY_READ(sc, MII_BMCR);
    364       1.2   thorpej 		if ((reg & BMCR_RESET) == 0)
    365       1.2   thorpej 			break;
    366       1.2   thorpej 		delay(1000);
    367       1.2   thorpej 	}
    368       1.2   thorpej 
    369       1.6   thorpej 	if (sc->mii_inst != 0 && ((sc->mii_flags & MIIF_NOISOLATE) == 0))
    370       1.6   thorpej 		PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    371       1.8   thorpej }
    372       1.8   thorpej 
    373       1.8   thorpej void
    374      1.29   thorpej mii_phy_down(struct mii_softc *sc)
    375       1.8   thorpej {
    376       1.8   thorpej 
    377       1.8   thorpej 	if (sc->mii_flags & MIIF_DOINGAUTO) {
    378       1.8   thorpej 		sc->mii_flags &= ~MIIF_DOINGAUTO;
    379      1.17   thorpej 		callout_stop(&sc->mii_nway_ch);
    380       1.8   thorpej 	}
    381      1.11   thorpej }
    382      1.11   thorpej 
    383      1.11   thorpej void
    384      1.29   thorpej mii_phy_status(struct mii_softc *sc)
    385      1.11   thorpej {
    386      1.11   thorpej 
    387      1.18   thorpej 	PHY_STATUS(sc);
    388      1.15   thorpej }
    389      1.15   thorpej 
    390      1.15   thorpej void
    391      1.29   thorpej mii_phy_update(struct mii_softc *sc, int cmd)
    392      1.15   thorpej {
    393      1.15   thorpej 	struct mii_data *mii = sc->mii_pdata;
    394      1.15   thorpej 
    395      1.15   thorpej 	if (sc->mii_media_active != mii->mii_media_active ||
    396      1.15   thorpej 	    sc->mii_media_status != mii->mii_media_status ||
    397      1.15   thorpej 	    cmd == MII_MEDIACHG) {
    398      1.46     joerg 		mii_phy_statusmsg(sc);
    399      1.60   xtraeme 		(*mii->mii_statchg)(device_parent(sc->mii_dev));
    400      1.15   thorpej 		sc->mii_media_active = mii->mii_media_active;
    401      1.15   thorpej 		sc->mii_media_status = mii->mii_media_status;
    402      1.15   thorpej 	}
    403      1.15   thorpej }
    404      1.15   thorpej 
    405      1.46     joerg static void
    406      1.29   thorpej mii_phy_statusmsg(struct mii_softc *sc)
    407      1.15   thorpej {
    408      1.15   thorpej 	struct mii_data *mii = sc->mii_pdata;
    409      1.15   thorpej 	struct ifnet *ifp = mii->mii_ifp;
    410      1.46     joerg 	int s;
    411      1.15   thorpej 
    412      1.46     joerg 	s = splnet();
    413      1.15   thorpej 	if (mii->mii_media_status & IFM_AVALID) {
    414      1.15   thorpej 		if (mii->mii_media_status & IFM_ACTIVE)
    415      1.46     joerg 			if_link_state_change(ifp, LINK_STATE_UP);
    416      1.15   thorpej 		else
    417      1.46     joerg 			if_link_state_change(ifp, LINK_STATE_DOWN);
    418      1.15   thorpej 	} else
    419      1.46     joerg 		if_link_state_change(ifp, LINK_STATE_UNKNOWN);
    420      1.46     joerg 	splx(s);
    421      1.15   thorpej 
    422      1.46     joerg 	ifp->if_baudrate = ifmedia_baudrate(mii->mii_media_active);
    423       1.5  drochner }
    424       1.5  drochner 
    425       1.5  drochner /*
    426       1.5  drochner  * Initialize generic PHY media based on BMSR, called when a PHY is
    427       1.5  drochner  * attached.  We expect to be set up to print a comma-separated list
    428       1.5  drochner  * of media names.  Does not print a newline.
    429       1.5  drochner  */
    430       1.5  drochner void
    431      1.29   thorpej mii_phy_add_media(struct mii_softc *sc)
    432       1.5  drochner {
    433       1.6   thorpej 	struct mii_data *mii = sc->mii_pdata;
    434      1.61    dyoung 	device_t self = sc->mii_dev;
    435       1.5  drochner 	const char *sep = "";
    436      1.38   thorpej 	int fdx = 0;
    437       1.5  drochner 
    438       1.5  drochner #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
    439      1.36   thorpej #define	PRINT(n)	aprint_normal("%s%s", sep, (n)); sep = ", "
    440       1.5  drochner 
    441       1.6   thorpej 	if ((sc->mii_flags & MIIF_NOISOLATE) == 0)
    442       1.6   thorpej 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
    443       1.6   thorpej 		    MII_MEDIA_NONE);
    444      1.28   thorpej 
    445      1.28   thorpej 	/*
    446      1.28   thorpej 	 * There are different interpretations for the bits in
    447      1.28   thorpej 	 * HomePNA PHYs.  And there is really only one media type
    448      1.28   thorpej 	 * that is supported.
    449      1.28   thorpej 	 */
    450      1.28   thorpej 	if (sc->mii_flags & MIIF_IS_HPNA) {
    451      1.28   thorpej 		if (sc->mii_capabilities & BMSR_10THDX) {
    452      1.28   thorpej 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_HPNA_1, 0,
    453      1.28   thorpej 					 sc->mii_inst),
    454      1.28   thorpej 			    MII_MEDIA_10_T);
    455      1.28   thorpej 			PRINT("HomePNA1");
    456      1.28   thorpej 		}
    457      1.61    dyoung 		goto out;
    458      1.28   thorpej 	}
    459       1.6   thorpej 
    460       1.6   thorpej 	if (sc->mii_capabilities & BMSR_10THDX) {
    461       1.6   thorpej 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, sc->mii_inst),
    462       1.6   thorpej 		    MII_MEDIA_10_T);
    463       1.5  drochner 		PRINT("10baseT");
    464       1.5  drochner 	}
    465       1.6   thorpej 	if (sc->mii_capabilities & BMSR_10TFDX) {
    466       1.6   thorpej 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, sc->mii_inst),
    467       1.6   thorpej 		    MII_MEDIA_10_T_FDX);
    468       1.5  drochner 		PRINT("10baseT-FDX");
    469      1.38   thorpej 		fdx = 1;
    470       1.5  drochner 	}
    471       1.6   thorpej 	if (sc->mii_capabilities & BMSR_100TXHDX) {
    472       1.6   thorpej 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, sc->mii_inst),
    473       1.6   thorpej 		    MII_MEDIA_100_TX);
    474       1.5  drochner 		PRINT("100baseTX");
    475       1.5  drochner 	}
    476       1.6   thorpej 	if (sc->mii_capabilities & BMSR_100TXFDX) {
    477       1.6   thorpej 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, sc->mii_inst),
    478       1.6   thorpej 		    MII_MEDIA_100_TX_FDX);
    479       1.5  drochner 		PRINT("100baseTX-FDX");
    480      1.38   thorpej 		fdx = 1;
    481       1.5  drochner 	}
    482       1.6   thorpej 	if (sc->mii_capabilities & BMSR_100T4) {
    483       1.6   thorpej 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, sc->mii_inst),
    484       1.6   thorpej 		    MII_MEDIA_100_T4);
    485       1.5  drochner 		PRINT("100baseT4");
    486       1.5  drochner 	}
    487      1.21   thorpej 
    488      1.21   thorpej 	if (sc->mii_extcapabilities & EXTSR_MEDIAMASK) {
    489      1.21   thorpej 		/*
    490      1.21   thorpej 		 * XXX Right now only handle 1000SX and 1000TX.  Need
    491      1.22   thorpej 		 * XXX to handle 1000LX and 1000CX some how.
    492      1.22   thorpej 		 *
    493      1.22   thorpej 		 * Note since it can take 5 seconds to auto-negotiate
    494      1.22   thorpej 		 * a gigabit link, we make anegticks 10 seconds for
    495      1.22   thorpej 		 * all the gigabit media types.
    496      1.21   thorpej 		 */
    497      1.21   thorpej 		if (sc->mii_extcapabilities & EXTSR_1000XHDX) {
    498      1.53  christos 			sc->mii_anegticks = MII_ANEGTICKS_GIGE;
    499      1.27   thorpej 			sc->mii_flags |= MIIF_IS_1000X;
    500      1.21   thorpej 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0,
    501      1.23   thorpej 			    sc->mii_inst), MII_MEDIA_1000_X);
    502      1.21   thorpej 			PRINT("1000baseSX");
    503      1.21   thorpej 		}
    504      1.21   thorpej 		if (sc->mii_extcapabilities & EXTSR_1000XFDX) {
    505      1.53  christos 			sc->mii_anegticks = MII_ANEGTICKS_GIGE;
    506      1.27   thorpej 			sc->mii_flags |= MIIF_IS_1000X;
    507      1.21   thorpej 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX,
    508      1.23   thorpej 			    sc->mii_inst), MII_MEDIA_1000_X_FDX);
    509      1.21   thorpej 			PRINT("1000baseSX-FDX");
    510      1.38   thorpej 			fdx = 1;
    511      1.21   thorpej 		}
    512      1.23   thorpej 
    513      1.23   thorpej 		/*
    514      1.23   thorpej 		 * 1000baseT media needs to be able to manipulate
    515      1.23   thorpej 		 * master/slave mode.  We set IFM_ETH_MASTER in
    516      1.23   thorpej 		 * the "don't care mask" and filter it out when
    517      1.23   thorpej 		 * the media is set.
    518      1.23   thorpej 		 *
    519      1.23   thorpej 		 * All 1000baseT PHYs have a 1000baseT control register.
    520      1.23   thorpej 		 */
    521      1.21   thorpej 		if (sc->mii_extcapabilities & EXTSR_1000THDX) {
    522      1.53  christos 			sc->mii_anegticks = MII_ANEGTICKS_GIGE;
    523      1.23   thorpej 			sc->mii_flags |= MIIF_HAVE_GTCR;
    524      1.23   thorpej 			mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
    525      1.25     bjh21 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 0,
    526      1.23   thorpej 			    sc->mii_inst), MII_MEDIA_1000_T);
    527      1.25     bjh21 			PRINT("1000baseT");
    528      1.21   thorpej 		}
    529      1.21   thorpej 		if (sc->mii_extcapabilities & EXTSR_1000TFDX) {
    530      1.53  christos 			sc->mii_anegticks = MII_ANEGTICKS_GIGE;
    531      1.23   thorpej 			sc->mii_flags |= MIIF_HAVE_GTCR;
    532      1.23   thorpej 			mii->mii_media.ifm_mask |= IFM_ETH_MASTER;
    533      1.25     bjh21 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, IFM_FDX,
    534      1.23   thorpej 			    sc->mii_inst), MII_MEDIA_1000_T_FDX);
    535      1.25     bjh21 			PRINT("1000baseT-FDX");
    536      1.38   thorpej 			fdx = 1;
    537      1.21   thorpej 		}
    538      1.21   thorpej 	}
    539      1.21   thorpej 
    540       1.6   thorpej 	if (sc->mii_capabilities & BMSR_ANEG) {
    541       1.6   thorpej 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst),
    542       1.6   thorpej 		    MII_NMEDIA);	/* intentionally invalid index */
    543       1.5  drochner 		PRINT("auto");
    544       1.5  drochner 	}
    545       1.5  drochner #undef ADD
    546       1.5  drochner #undef PRINT
    547      1.38   thorpej 	if (fdx != 0 && (sc->mii_flags & MIIF_DOPAUSE))
    548      1.38   thorpej 		mii->mii_media.ifm_mask |= IFM_ETH_FMASK;
    549      1.61    dyoung out:
    550      1.61    dyoung 	if (!pmf_device_register(self, NULL, mii_phy_resume)) {
    551      1.61    dyoung 		aprint_normal("\n");
    552      1.61    dyoung 		aprint_error_dev(self, "couldn't establish power handler");
    553      1.61    dyoung 	}
    554       1.9   thorpej }
    555       1.9   thorpej 
    556       1.9   thorpej void
    557      1.29   thorpej mii_phy_delete_media(struct mii_softc *sc)
    558       1.9   thorpej {
    559       1.9   thorpej 	struct mii_data *mii = sc->mii_pdata;
    560       1.9   thorpej 
    561       1.9   thorpej 	ifmedia_delete_instance(&mii->mii_media, sc->mii_inst);
    562       1.9   thorpej }
    563       1.9   thorpej 
    564       1.9   thorpej int
    565      1.64    cegger mii_phy_activate(device_t self, enum devact act)
    566       1.9   thorpej {
    567       1.9   thorpej 	switch (act) {
    568       1.9   thorpej 	case DVACT_DEACTIVATE:
    569      1.65    dyoung 		/* XXX Invalidate parent's media setting? */
    570      1.65    dyoung 		return 0;
    571      1.65    dyoung 	default:
    572      1.65    dyoung 		return EOPNOTSUPP;
    573       1.9   thorpej 	}
    574       1.9   thorpej }
    575       1.9   thorpej 
    576      1.31       uwe /* ARGSUSED1 */
    577       1.9   thorpej int
    578      1.64    cegger mii_phy_detach(device_t self, int flags)
    579       1.9   thorpej {
    580      1.49   thorpej 	struct mii_softc *sc = device_private(self);
    581       1.9   thorpej 
    582      1.65    dyoung 	/* XXX Invalidate parent's media setting? */
    583      1.65    dyoung 
    584       1.9   thorpej 	if (sc->mii_flags & MIIF_DOINGAUTO)
    585      1.68    martin 		callout_halt(&sc->mii_nway_ch, NULL);
    586       1.9   thorpej 
    587      1.63    dyoung 	callout_destroy(&sc->mii_nway_ch);
    588      1.63    dyoung 
    589      1.10   thorpej 	mii_phy_delete_media(sc);
    590      1.56    dyoung 	LIST_REMOVE(sc, mii_list);
    591       1.9   thorpej 
    592       1.9   thorpej 	return (0);
    593      1.24   thorpej }
    594      1.24   thorpej 
    595      1.24   thorpej const struct mii_phydesc *
    596      1.24   thorpej mii_phy_match(const struct mii_attach_args *ma, const struct mii_phydesc *mpd)
    597      1.24   thorpej {
    598      1.24   thorpej 
    599      1.24   thorpej 	for (; mpd->mpd_name != NULL; mpd++) {
    600      1.24   thorpej 		if (MII_OUI(ma->mii_id1, ma->mii_id2) == mpd->mpd_oui &&
    601      1.24   thorpej 		    MII_MODEL(ma->mii_id2) == mpd->mpd_model)
    602      1.24   thorpej 			return (mpd);
    603      1.24   thorpej 	}
    604      1.24   thorpej 	return (NULL);
    605       1.1   thorpej }
    606      1.38   thorpej 
    607      1.38   thorpej /*
    608      1.38   thorpej  * Return the flow control status flag from MII_ANAR & MII_ANLPAR.
    609      1.38   thorpej  */
    610      1.38   thorpej u_int
    611      1.40   thorpej mii_phy_flowstatus(struct mii_softc *sc)
    612      1.38   thorpej {
    613      1.38   thorpej 	u_int anar, anlpar;
    614      1.38   thorpej 
    615      1.39   thorpej 	if ((sc->mii_flags & MIIF_DOPAUSE) == 0)
    616      1.39   thorpej 		return (0);
    617      1.39   thorpej 
    618      1.38   thorpej 	anar = PHY_READ(sc, MII_ANAR);
    619      1.38   thorpej 	anlpar = PHY_READ(sc, MII_ANLPAR);
    620      1.38   thorpej 
    621      1.42   thorpej 	if ((anar & ANAR_X_PAUSE_SYM) & (anlpar & ANLPAR_X_PAUSE_SYM))
    622      1.42   thorpej 		return (IFM_FLOW|IFM_ETH_TXPAUSE|IFM_ETH_RXPAUSE);
    623      1.42   thorpej 
    624      1.38   thorpej 	if ((anar & ANAR_X_PAUSE_SYM) == 0) {
    625      1.38   thorpej 		if ((anar & ANAR_X_PAUSE_ASYM) &&
    626      1.38   thorpej 		    ((anlpar &
    627      1.38   thorpej 		      ANLPAR_X_PAUSE_TOWARDS) == ANLPAR_X_PAUSE_TOWARDS))
    628      1.38   thorpej 			return (IFM_FLOW|IFM_ETH_TXPAUSE);
    629      1.38   thorpej 		else
    630      1.38   thorpej 			return (0);
    631      1.38   thorpej 	}
    632      1.38   thorpej 
    633      1.38   thorpej 	if ((anar & ANAR_X_PAUSE_ASYM) == 0) {
    634      1.38   thorpej 		if (anlpar & ANLPAR_X_PAUSE_SYM)
    635      1.38   thorpej 			return (IFM_FLOW|IFM_ETH_TXPAUSE|IFM_ETH_RXPAUSE);
    636      1.38   thorpej 		else
    637      1.38   thorpej 			return (0);
    638      1.38   thorpej 	}
    639      1.38   thorpej 
    640      1.38   thorpej 	switch ((anlpar & ANLPAR_X_PAUSE_TOWARDS)) {
    641      1.38   thorpej 	case ANLPAR_X_PAUSE_NONE:
    642      1.38   thorpej 		return (0);
    643      1.44     perry 
    644      1.38   thorpej 	case ANLPAR_X_PAUSE_ASYM:
    645      1.38   thorpej 		return (IFM_FLOW|IFM_ETH_RXPAUSE);
    646      1.44     perry 
    647      1.38   thorpej 	default:
    648      1.38   thorpej 		return (IFM_FLOW|IFM_ETH_RXPAUSE|IFM_ETH_TXPAUSE);
    649      1.38   thorpej 	}
    650      1.38   thorpej 	/* NOTREACHED */
    651      1.38   thorpej }
    652      1.54  jmcneill 
    653      1.54  jmcneill bool
    654      1.67    dyoung mii_phy_resume(device_t dv, const pmf_qual_t *qual)
    655      1.54  jmcneill {
    656      1.54  jmcneill 	struct mii_softc *sc = device_private(dv);
    657      1.54  jmcneill 
    658      1.54  jmcneill 	PHY_RESET(sc);
    659      1.55    dyoung 	return PHY_SERVICE(sc, sc->mii_pdata, MII_MEDIACHG) == 0;
    660      1.54  jmcneill }
    661      1.62    cegger 
    662      1.62    cegger 
    663      1.62    cegger /*
    664      1.62    cegger  * Given an ifmedia word, return the corresponding ANAR value.
    665      1.62    cegger  */
    666      1.62    cegger int
    667      1.62    cegger mii_anar(int media)
    668      1.62    cegger {
    669      1.62    cegger 	int rv;
    670      1.62    cegger 
    671      1.62    cegger 	switch (media & (IFM_TMASK|IFM_NMASK|IFM_FDX)) {
    672      1.62    cegger 	case IFM_ETHER|IFM_10_T:
    673      1.62    cegger 		rv = ANAR_10|ANAR_CSMA;
    674      1.62    cegger 		break;
    675      1.62    cegger 	case IFM_ETHER|IFM_10_T|IFM_FDX:
    676      1.62    cegger 		rv = ANAR_10_FD|ANAR_CSMA;
    677      1.62    cegger 		break;
    678      1.62    cegger 	case IFM_ETHER|IFM_100_TX:
    679      1.62    cegger 		rv = ANAR_TX|ANAR_CSMA;
    680      1.62    cegger 		break;
    681      1.62    cegger 	case IFM_ETHER|IFM_100_TX|IFM_FDX:
    682      1.62    cegger 		rv = ANAR_TX_FD|ANAR_CSMA;
    683      1.62    cegger 		break;
    684      1.62    cegger 	case IFM_ETHER|IFM_100_T4:
    685      1.62    cegger 		rv = ANAR_T4|ANAR_CSMA;
    686      1.62    cegger 		break;
    687      1.62    cegger 	default:
    688      1.62    cegger 		rv = 0;
    689      1.62    cegger 		break;
    690      1.62    cegger 	}
    691      1.62    cegger 
    692      1.62    cegger 	return rv;
    693      1.62    cegger }
    694