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