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