gentbi.c revision 1.27.28.2       1  1.27.28.2    martin /*	$NetBSD: gentbi.c,v 1.27.28.2 2020/04/08 14:08:08 martin Exp $	*/
      2        1.1   thorpej 
      3        1.1   thorpej /*-
      4        1.1   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  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
     35        1.1   thorpej  *
     36        1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     37        1.1   thorpej  * modification, are permitted provided that the following conditions
     38        1.1   thorpej  * are met:
     39        1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     40        1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     41        1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     42        1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     43        1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     44        1.1   thorpej  *
     45        1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     46        1.1   thorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     47        1.1   thorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     48        1.1   thorpej  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     49        1.1   thorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     50        1.1   thorpej  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     51        1.1   thorpej  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     52        1.1   thorpej  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     53        1.1   thorpej  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     54        1.1   thorpej  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     55        1.1   thorpej  */
     56        1.1   thorpej 
     57        1.1   thorpej /*
     58       1.26   msaitoh  * Driver for generic ten-bit (1000BASE-SX) interfaces, built into
     59        1.1   thorpej  * many Gigabit Ethernet chips.
     60        1.1   thorpej  *
     61        1.1   thorpej  * All we have to do here is correctly report speed and duplex.
     62        1.1   thorpej  */
     63        1.4     lukem 
     64        1.4     lukem #include <sys/cdefs.h>
     65  1.27.28.2    martin __KERNEL_RCSID(0, "$NetBSD: gentbi.c,v 1.27.28.2 2020/04/08 14:08:08 martin Exp $");
     66        1.1   thorpej 
     67        1.1   thorpej #include <sys/param.h>
     68        1.1   thorpej #include <sys/systm.h>
     69        1.1   thorpej #include <sys/kernel.h>
     70        1.1   thorpej #include <sys/device.h>
     71        1.1   thorpej #include <sys/socket.h>
     72        1.1   thorpej #include <sys/errno.h>
     73        1.1   thorpej 
     74        1.1   thorpej #include <net/if.h>
     75        1.1   thorpej #include <net/if_media.h>
     76        1.1   thorpej 
     77        1.1   thorpej #include <dev/mii/mii.h>
     78        1.1   thorpej #include <dev/mii/miivar.h>
     79        1.1   thorpej #include <dev/mii/miidevs.h>
     80        1.1   thorpej 
     81       1.22   xtraeme static int	gentbimatch(device_t, cfdata_t, void *);
     82       1.22   xtraeme static void	gentbiattach(device_t, device_t, void *);
     83        1.1   thorpej 
     84       1.22   xtraeme CFATTACH_DECL_NEW(gentbi, sizeof(struct mii_softc),
     85        1.9   thorpej     gentbimatch, gentbiattach, mii_phy_detach, mii_phy_activate);
     86        1.1   thorpej 
     87       1.13   thorpej static int	gentbi_service(struct mii_softc *, struct mii_data *, int);
     88       1.13   thorpej static void	gentbi_status(struct mii_softc *);
     89        1.1   thorpej 
     90       1.13   thorpej static const struct mii_phy_funcs gentbi_funcs = {
     91        1.1   thorpej 	gentbi_service, gentbi_status, mii_phy_reset,
     92        1.1   thorpej };
     93        1.1   thorpej 
     94       1.13   thorpej static int
     95       1.22   xtraeme gentbimatch(device_t parent, cfdata_t match, void *aux)
     96        1.1   thorpej {
     97        1.1   thorpej 	struct mii_attach_args *ma = aux;
     98        1.1   thorpej 	struct mii_data *mii = ma->mii_data;
     99  1.27.28.1  christos 	int rv;
    100  1.27.28.1  christos 	uint16_t bmsr, extsr;
    101        1.1   thorpej 
    102        1.1   thorpej 	/*
    103        1.1   thorpej 	 * We match as a generic TBI if:
    104        1.1   thorpej 	 *
    105        1.1   thorpej 	 *	- There is no media in the BMSR.
    106        1.1   thorpej 	 *	- EXTSR has only 1000X.
    107        1.1   thorpej 	 */
    108  1.27.28.2    martin 	mii_lock(mii);
    109  1.27.28.1  christos 	rv = (*mii->mii_readreg)(parent, ma->mii_phyno, MII_BMSR, &bmsr);
    110  1.27.28.1  christos 	if ((rv != 0)
    111  1.27.28.2    martin 	    || (bmsr & BMSR_EXTSTAT) == 0 || (bmsr & BMSR_MEDIAMASK) != 0) {
    112  1.27.28.2    martin 		mii_unlock(mii);
    113  1.27.28.1  christos 		return 0;
    114  1.27.28.2    martin 	}
    115  1.27.28.1  christos 
    116  1.27.28.1  christos 	rv = (*mii->mii_readreg)(parent, ma->mii_phyno, MII_EXTSR, &extsr);
    117  1.27.28.2    martin 	if ((rv != 0) || ((extsr & (EXTSR_1000TFDX | EXTSR_1000THDX)) != 0)) {
    118  1.27.28.2    martin 		mii_unlock(mii);
    119  1.27.28.1  christos 		return 0;
    120  1.27.28.2    martin 	}
    121  1.27.28.2    martin 	mii_unlock(mii);
    122        1.1   thorpej 
    123  1.27.28.1  christos 	if (extsr & (EXTSR_1000XFDX | EXTSR_1000XHDX)) {
    124        1.1   thorpej 		/*
    125        1.1   thorpej 		 * We think this is a generic TBI.  Return a match
    126        1.1   thorpej 		 * priority higher than ukphy, but lower than what
    127        1.1   thorpej 		 * specific drivers will return.
    128        1.1   thorpej 		 */
    129  1.27.28.1  christos 		return 2;
    130        1.1   thorpej 	}
    131        1.1   thorpej 
    132  1.27.28.1  christos 	return 0;
    133        1.1   thorpej }
    134        1.1   thorpej 
    135       1.13   thorpej static void
    136       1.22   xtraeme gentbiattach(device_t parent, device_t self, void *aux)
    137        1.1   thorpej {
    138       1.15   thorpej 	struct mii_softc *sc = device_private(self);
    139        1.1   thorpej 	struct mii_attach_args *ma = aux;
    140        1.1   thorpej 	struct mii_data *mii = ma->mii_data;
    141       1.25   msaitoh 	int oui = MII_OUI(ma->mii_id1, ma->mii_id2);
    142       1.25   msaitoh 	int model = MII_MODEL(ma->mii_id2);
    143       1.25   msaitoh 	int rev = MII_REV(ma->mii_id2);
    144       1.25   msaitoh 	const char *descr;
    145       1.25   msaitoh 
    146       1.25   msaitoh 	if ((descr = mii_get_descr(oui, model)) != NULL)
    147       1.25   msaitoh 		aprint_normal(": %s (OUI 0x%06x, model 0x%04x), rev. %d\n",
    148       1.25   msaitoh 		    descr, oui, model, rev);
    149       1.25   msaitoh 	else
    150       1.25   msaitoh 		aprint_normal(": OUI 0x%06x, model 0x%04x, rev. %d\n",
    151       1.25   msaitoh 		    oui, model, rev);
    152        1.1   thorpej 
    153       1.25   msaitoh 	aprint_naive(": Generic ten-bit interface\n");
    154        1.1   thorpej 
    155       1.22   xtraeme 	sc->mii_dev = self;
    156        1.1   thorpej 	sc->mii_inst = mii->mii_instance;
    157        1.1   thorpej 	sc->mii_phy = ma->mii_phyno;
    158       1.25   msaitoh 	sc->mii_mpd_oui = oui;
    159       1.25   msaitoh 	sc->mii_mpd_model = model;
    160       1.25   msaitoh 	sc->mii_mpd_rev = rev;
    161        1.1   thorpej 	sc->mii_funcs = &gentbi_funcs;
    162        1.1   thorpej 	sc->mii_pdata = mii;
    163        1.5   thorpej 	sc->mii_flags = ma->mii_flags;
    164  1.27.28.2    martin 
    165  1.27.28.2    martin 	mii_lock(mii);
    166        1.1   thorpej 
    167        1.1   thorpej 	PHY_RESET(sc);
    168        1.1   thorpej 
    169        1.1   thorpej 	/*
    170        1.1   thorpej 	 * Mask out all media in the BMSR.  We only are really interested
    171        1.1   thorpej 	 * in "auto".
    172        1.1   thorpej 	 */
    173  1.27.28.1  christos 	PHY_READ(sc, MII_BMSR, &sc->mii_capabilities);
    174        1.1   thorpej 	sc->mii_capabilities =
    175  1.27.28.1  christos 	    sc->mii_capabilities & ma->mii_capmask & ~BMSR_MEDIAMASK;
    176        1.1   thorpej 	if (sc->mii_capabilities & BMSR_EXTSTAT)
    177  1.27.28.1  christos 		PHY_READ(sc, MII_EXTSR, &sc->mii_extcapabilities);
    178        1.1   thorpej 
    179  1.27.28.2    martin 	mii_unlock(mii);
    180  1.27.28.2    martin 
    181  1.27.28.2    martin 	mii_phy_add_media(sc);
    182        1.1   thorpej }
    183        1.1   thorpej 
    184       1.13   thorpej static int
    185        1.3   thorpej gentbi_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
    186        1.1   thorpej {
    187        1.1   thorpej 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    188  1.27.28.1  christos 	uint16_t reg;
    189        1.1   thorpej 
    190  1.27.28.2    martin 	KASSERT(mii_locked(mii));
    191  1.27.28.2    martin 
    192        1.1   thorpej 	switch (cmd) {
    193        1.1   thorpej 	case MII_POLLSTAT:
    194  1.27.28.1  christos 		/* If we're not polling our PHY instance, just return. */
    195        1.1   thorpej 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    196  1.27.28.1  christos 			return 0;
    197        1.1   thorpej 		break;
    198        1.1   thorpej 
    199        1.1   thorpej 	case MII_MEDIACHG:
    200        1.1   thorpej 		/*
    201        1.1   thorpej 		 * If the media indicates a different PHY instance,
    202        1.1   thorpej 		 * isolate ourselves.
    203        1.1   thorpej 		 */
    204        1.1   thorpej 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
    205  1.27.28.1  christos 			PHY_READ(sc, MII_BMCR, ®);
    206        1.1   thorpej 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
    207  1.27.28.1  christos 			return 0;
    208        1.1   thorpej 		}
    209        1.1   thorpej 
    210  1.27.28.1  christos 		/* If the interface is not up, don't do anything. */
    211        1.1   thorpej 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
    212        1.1   thorpej 			break;
    213        1.1   thorpej 
    214        1.1   thorpej 		mii_phy_setmedia(sc);
    215        1.1   thorpej 		break;
    216        1.1   thorpej 
    217        1.1   thorpej 	case MII_TICK:
    218  1.27.28.1  christos 		/* If we're not currently selected, just return. */
    219        1.1   thorpej 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
    220  1.27.28.1  christos 			return 0;
    221        1.1   thorpej 
    222        1.1   thorpej 		if (mii_phy_tick(sc) == EJUSTRETURN)
    223  1.27.28.1  christos 			return 0;
    224        1.1   thorpej 		break;
    225        1.1   thorpej 
    226        1.1   thorpej 	case MII_DOWN:
    227        1.1   thorpej 		mii_phy_down(sc);
    228  1.27.28.1  christos 		return 0;
    229        1.1   thorpej 	}
    230        1.1   thorpej 
    231        1.1   thorpej 	/* Update the media status. */
    232        1.1   thorpej 	mii_phy_status(sc);
    233        1.1   thorpej 
    234        1.1   thorpej 	/* Callback if something changed. */
    235        1.1   thorpej 	mii_phy_update(sc, cmd);
    236  1.27.28.1  christos 	return 0;
    237        1.1   thorpej }
    238        1.1   thorpej 
    239       1.13   thorpej static void
    240        1.3   thorpej gentbi_status(struct mii_softc *sc)
    241        1.1   thorpej {
    242        1.1   thorpej 	struct mii_data *mii = sc->mii_pdata;
    243        1.1   thorpej 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
    244  1.27.28.1  christos 	uint16_t bmsr, bmcr, anlpar;
    245        1.1   thorpej 
    246  1.27.28.2    martin 	KASSERT(mii_locked(mii));
    247  1.27.28.2    martin 
    248        1.1   thorpej 	mii->mii_media_status = IFM_AVALID;
    249        1.1   thorpej 	mii->mii_media_active = IFM_ETHER;
    250        1.1   thorpej 
    251  1.27.28.1  christos 	PHY_READ(sc, MII_BMSR, &bmsr);
    252  1.27.28.1  christos 	PHY_READ(sc, MII_BMSR, &bmsr);
    253        1.1   thorpej 
    254        1.1   thorpej 	if (bmsr & BMSR_LINK)
    255        1.1   thorpej 		mii->mii_media_status |= IFM_ACTIVE;
    256        1.1   thorpej 
    257  1.27.28.1  christos 	PHY_READ(sc, MII_BMCR, &bmcr);
    258        1.1   thorpej 	if (bmcr & BMCR_ISO) {
    259        1.1   thorpej 		mii->mii_media_active |= IFM_NONE;
    260        1.1   thorpej 		mii->mii_media_status = 0;
    261        1.1   thorpej 		return;
    262        1.1   thorpej 	}
    263        1.1   thorpej 
    264        1.1   thorpej 	if (bmcr & BMCR_LOOP)
    265        1.1   thorpej 		mii->mii_media_active |= IFM_LOOP;
    266        1.1   thorpej 
    267        1.1   thorpej 	if (bmcr & BMCR_AUTOEN) {
    268        1.1   thorpej 		/*
    269       1.26   msaitoh 		 * The media status bits are only valid if autonegotiation
    270        1.1   thorpej 		 * has completed (or it's disabled).
    271        1.1   thorpej 		 */
    272        1.1   thorpej 		if ((bmsr & BMSR_ACOMP) == 0) {
    273        1.1   thorpej 			/* Erg, still trying, I guess... */
    274        1.1   thorpej 			mii->mii_media_active |= IFM_NONE;
    275        1.1   thorpej 			return;
    276        1.1   thorpej 		}
    277        1.1   thorpej 
    278        1.1   thorpej 		/*
    279        1.2   thorpej 		 * The media is always 1000baseSX.  Check the ANLPAR to
    280        1.2   thorpej 		 * see if we're doing full-duplex.
    281        1.1   thorpej 		 */
    282        1.1   thorpej 		mii->mii_media_active |= IFM_1000_SX;
    283        1.1   thorpej 
    284  1.27.28.1  christos 		PHY_READ(sc, MII_ANLPAR, &anlpar);
    285        1.2   thorpej 		if ((sc->mii_extcapabilities & EXTSR_1000XFDX) != 0 &&
    286        1.2   thorpej 		    (anlpar & ANLPAR_X_FD) != 0)
    287       1.11   thorpej 			mii->mii_media_active |=
    288       1.12   thorpej 			    IFM_FDX | mii_phy_flowstatus(sc);
    289       1.27   msaitoh 		else
    290       1.27   msaitoh 			mii->mii_media_active |= IFM_HDX;
    291        1.1   thorpej 	} else
    292        1.1   thorpej 		mii->mii_media_active = ife->ifm_media;
    293        1.1   thorpej }
    294