Home | History | Annotate | Line # | Download | only in mii
mii.c revision 1.7
      1  1.7  thorpej /*	$NetBSD: mii.c,v 1.7 1998/08/11 00:41:44 thorpej Exp $	*/
      2  1.6  thorpej 
      3  1.6  thorpej /*-
      4  1.6  thorpej  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  1.6  thorpej  * All rights reserved.
      6  1.6  thorpej  *
      7  1.6  thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.6  thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  1.6  thorpej  * NASA Ames Research Center.
     10  1.1   bouyer  *
     11  1.1   bouyer  * Redistribution and use in source and binary forms, with or without
     12  1.1   bouyer  * modification, are permitted provided that the following conditions
     13  1.1   bouyer  * are met:
     14  1.1   bouyer  * 1. Redistributions of source code must retain the above copyright
     15  1.1   bouyer  *    notice, this list of conditions and the following disclaimer.
     16  1.1   bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1   bouyer  *    notice, this list of conditions and the following disclaimer in the
     18  1.1   bouyer  *    documentation and/or other materials provided with the distribution.
     19  1.1   bouyer  * 3. All advertising materials mentioning features or use of this software
     20  1.1   bouyer  *    must display the following acknowledgement:
     21  1.6  thorpej  *	This product includes software developed by the NetBSD
     22  1.6  thorpej  *	Foundation, Inc. and its contributors.
     23  1.6  thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.6  thorpej  *    contributors may be used to endorse or promote products derived
     25  1.6  thorpej  *    from this software without specific prior written permission.
     26  1.1   bouyer  *
     27  1.6  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.6  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.6  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.6  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.6  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.6  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.6  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.6  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.6  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.6  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.6  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38  1.6  thorpej  */
     39  1.6  thorpej 
     40  1.6  thorpej /*
     41  1.6  thorpej  * MII bus layer, glues MII-capable network interface drivers to sharable
     42  1.6  thorpej  * PHY drivers.  This exports an interface compatible with BSD/OS 3.0's,
     43  1.6  thorpej  * plus some NetBSD extensions.
     44  1.1   bouyer  */
     45  1.1   bouyer 
     46  1.1   bouyer #include <sys/param.h>
     47  1.6  thorpej #include <sys/device.h>
     48  1.1   bouyer #include <sys/systm.h>
     49  1.1   bouyer #include <sys/socket.h>
     50  1.1   bouyer 
     51  1.1   bouyer #include <net/if.h>
     52  1.1   bouyer #include <net/if_media.h>
     53  1.1   bouyer 
     54  1.6  thorpej #include <dev/mii/mii.h>
     55  1.6  thorpej #include <dev/mii/miivar.h>
     56  1.1   bouyer 
     57  1.6  thorpej int	mii_print __P((void *, const char *));
     58  1.6  thorpej int	mii_submatch __P((struct device *, struct cfdata *, void *));
     59  1.1   bouyer 
     60  1.6  thorpej /*
     61  1.6  thorpej  * Helper function used by network interface drivers, attaches PHYs
     62  1.6  thorpej  * to the network interface driver parent.
     63  1.6  thorpej  */
     64  1.6  thorpej void
     65  1.6  thorpej mii_phy_probe(parent, mii, capmask)
     66  1.6  thorpej 	struct device *parent;
     67  1.6  thorpej 	struct mii_data *mii;
     68  1.6  thorpej 	int capmask;
     69  1.6  thorpej {
     70  1.6  thorpej 	struct mii_attach_args ma;
     71  1.6  thorpej 	struct mii_softc *child;
     72  1.1   bouyer 
     73  1.6  thorpej 	LIST_INIT(&mii->mii_phys);
     74  1.1   bouyer 
     75  1.6  thorpej 	for (ma.mii_phyno = 0; ma.mii_phyno < MII_NPHY; ma.mii_phyno++) {
     76  1.6  thorpej 		/*
     77  1.6  thorpej 		 * Check to see if there is a PHY at this address.  If
     78  1.6  thorpej 		 * the register contains garbage, assume no.
     79  1.6  thorpej 		 */
     80  1.6  thorpej 		ma.mii_id1 = (*mii->mii_readreg)(parent, ma.mii_phyno,
     81  1.6  thorpej 		    MII_PHYIDR1);
     82  1.6  thorpej 		ma.mii_id2 = (*mii->mii_readreg)(parent, ma.mii_phyno,
     83  1.6  thorpej 		    MII_PHYIDR2);
     84  1.6  thorpej 		if (ma.mii_id1 == 0 || ma.mii_id1 == 0xffff ||
     85  1.7  thorpej 		    ma.mii_id2 == 0 || ma.mii_id2 == 0xffff) {
     86  1.7  thorpej 			/*
     87  1.7  thorpej 			 * ARGH!!  3Com internal PHYs report 0/0 in their
     88  1.7  thorpej 			 * ID registers!  If we spot this, check to see
     89  1.7  thorpej 			 * if the BMSR has reasonable data in it.
     90  1.7  thorpej 			 */
     91  1.7  thorpej 			if (MII_OUI(ma.mii_id1, ma.mii_id2) == 0 &&
     92  1.7  thorpej 			    MII_MODEL(ma.mii_id2) == 0) {
     93  1.7  thorpej 				int bmsr = (*mii->mii_readreg)(parent,
     94  1.7  thorpej 				    ma.mii_phyno, MII_BMSR);
     95  1.7  thorpej 				if (bmsr == 0 || bmsr == 0xffff ||
     96  1.7  thorpej 				    (bmsr & BMSR_MEDIAMASK) == 0)
     97  1.7  thorpej 					continue;
     98  1.7  thorpej 			} else
     99  1.7  thorpej 				continue;
    100  1.7  thorpej 		}
    101  1.1   bouyer 
    102  1.6  thorpej 		ma.mii_data = mii;
    103  1.6  thorpej 		ma.mii_capmask = capmask;
    104  1.1   bouyer 
    105  1.6  thorpej 		if ((child = (struct mii_softc *)config_found_sm(parent, &ma,
    106  1.6  thorpej 		    mii_print, mii_submatch)) != NULL) {
    107  1.6  thorpej 			/*
    108  1.6  thorpej 			 * Link it up in the parent's MII data.
    109  1.6  thorpej 			 */
    110  1.6  thorpej 			LIST_INSERT_HEAD(&mii->mii_phys, child, mii_list);
    111  1.6  thorpej 			mii->mii_instance++;
    112  1.6  thorpej 		}
    113  1.6  thorpej 	}
    114  1.4   bouyer }
    115  1.1   bouyer 
    116  1.2  thorpej int
    117  1.2  thorpej mii_print(aux, pnp)
    118  1.1   bouyer 	void *aux;
    119  1.1   bouyer 	const char *pnp;
    120  1.1   bouyer {
    121  1.6  thorpej 	struct mii_attach_args *ma = aux;
    122  1.2  thorpej 
    123  1.6  thorpej 	if (pnp != NULL)
    124  1.6  thorpej 		printf("PHY oui 0x%x model 0x%x rev 0x%x at %s",
    125  1.6  thorpej 		    MII_OUI(ma->mii_id1, ma->mii_id2), MII_MODEL(ma->mii_id2),
    126  1.6  thorpej 		    MII_REV(ma->mii_id2), pnp);
    127  1.6  thorpej 
    128  1.6  thorpej 	printf(" phy %d", ma->mii_phyno);
    129  1.1   bouyer 	return (UNCONF);
    130  1.1   bouyer }
    131  1.1   bouyer 
    132  1.1   bouyer int
    133  1.6  thorpej mii_submatch(parent, cf, aux)
    134  1.1   bouyer 	struct device *parent;
    135  1.1   bouyer 	struct cfdata *cf;
    136  1.1   bouyer 	void *aux;
    137  1.1   bouyer {
    138  1.6  thorpej 	struct mii_attach_args *ma = aux;
    139  1.1   bouyer 
    140  1.7  thorpej 	if (ma->mii_phyno != cf->cf_loc[MIICF_PHY] &&
    141  1.7  thorpej 	    cf->cf_loc[MIICF_PHY] != MIICF_PHY_DEFAULT)
    142  1.6  thorpej 		return (0);
    143  1.1   bouyer 
    144  1.1   bouyer 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    145  1.1   bouyer }
    146  1.1   bouyer 
    147  1.6  thorpej /*
    148  1.6  thorpej  * Given an ifmedia word, return the corresponding ANAR value.
    149  1.6  thorpej  */
    150  1.6  thorpej int
    151  1.6  thorpej mii_anar(media)
    152  1.6  thorpej 	int media;
    153  1.1   bouyer {
    154  1.6  thorpej 	int rv;
    155  1.1   bouyer 
    156  1.6  thorpej 	switch (media & (IFM_TMASK|IFM_NMASK|IFM_FDX)) {
    157  1.6  thorpej 	case IFM_ETHER|IFM_10_T:
    158  1.6  thorpej 		rv = ANAR_10|ANAR_CSMA;
    159  1.6  thorpej 		break;
    160  1.6  thorpej 	case IFM_ETHER|IFM_10_T|IFM_FDX:
    161  1.6  thorpej 		rv = ANAR_10_FD|ANAR_CSMA;
    162  1.6  thorpej 		break;
    163  1.6  thorpej 	case IFM_ETHER|IFM_100_TX:
    164  1.6  thorpej 		rv = ANAR_TX|ANAR_CSMA;
    165  1.6  thorpej 		break;
    166  1.6  thorpej 	case IFM_ETHER|IFM_100_TX|IFM_FDX:
    167  1.6  thorpej 		rv = ANAR_TX_FD|ANAR_CSMA;
    168  1.6  thorpej 		break;
    169  1.6  thorpej 	case IFM_ETHER|IFM_100_T4:
    170  1.6  thorpej 		rv = ANAR_T4|ANAR_CSMA;
    171  1.6  thorpej 		break;
    172  1.6  thorpej 	default:
    173  1.6  thorpej 		rv = 0;
    174  1.6  thorpej 		break;
    175  1.1   bouyer 	}
    176  1.6  thorpej 
    177  1.6  thorpej 	return (rv);
    178  1.1   bouyer }
    179  1.1   bouyer 
    180  1.6  thorpej /*
    181  1.6  thorpej  * Media changed; notify all PHYs.
    182  1.6  thorpej  */
    183  1.6  thorpej int
    184  1.6  thorpej mii_mediachg(mii)
    185  1.6  thorpej 	struct mii_data *mii;
    186  1.1   bouyer {
    187  1.6  thorpej 	struct mii_softc *child;
    188  1.6  thorpej 	int rv;
    189  1.1   bouyer 
    190  1.6  thorpej 	mii->mii_media_status = 0;
    191  1.6  thorpej 	mii->mii_media_active = IFM_NONE;
    192  1.6  thorpej 
    193  1.6  thorpej 	for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
    194  1.6  thorpej 	     child = LIST_NEXT(child, mii_list)) {
    195  1.6  thorpej 		rv = (*child->mii_service)(child, mii, MII_MEDIACHG);
    196  1.6  thorpej 		if (rv)
    197  1.6  thorpej 			return (rv);
    198  1.1   bouyer 	}
    199  1.6  thorpej 	return (0);
    200  1.1   bouyer }
    201  1.1   bouyer 
    202  1.6  thorpej /*
    203  1.6  thorpej  * Call the PHY tick routines, used during autonegotiation.
    204  1.6  thorpej  */
    205  1.6  thorpej void
    206  1.6  thorpej mii_tick(mii)
    207  1.6  thorpej 	struct mii_data *mii;
    208  1.1   bouyer {
    209  1.6  thorpej 	struct mii_softc *child;
    210  1.1   bouyer 
    211  1.6  thorpej 	for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
    212  1.6  thorpej 	     child = LIST_NEXT(child, mii_list))
    213  1.6  thorpej 		(void) (*child->mii_service)(child, mii, MII_TICK);
    214  1.1   bouyer }
    215  1.1   bouyer 
    216  1.6  thorpej /*
    217  1.6  thorpej  * Get media status from PHYs.
    218  1.6  thorpej  */
    219  1.2  thorpej void
    220  1.6  thorpej mii_pollstat(mii)
    221  1.6  thorpej 	struct mii_data *mii;
    222  1.1   bouyer {
    223  1.6  thorpej 	struct mii_softc *child;
    224  1.1   bouyer 
    225  1.6  thorpej 	mii->mii_media_status = 0;
    226  1.6  thorpej 	mii->mii_media_active = IFM_NONE;
    227  1.1   bouyer 
    228  1.6  thorpej 	for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
    229  1.6  thorpej 	     child = LIST_NEXT(child, mii_list))
    230  1.6  thorpej 		(void) (*child->mii_service)(child, mii, MII_POLLSTAT);
    231  1.1   bouyer }
    232  1.1   bouyer 
    233  1.6  thorpej /*
    234  1.6  thorpej  * Initialize generic PHY media based on BMSR, called when a PHY is
    235  1.6  thorpej  * attached.  We expect to be set up to print a comma-separated list
    236  1.6  thorpej  * of media names.  Does not print a newline.
    237  1.6  thorpej  */
    238  1.2  thorpej void
    239  1.6  thorpej mii_add_media(mii, bmsr, instance)
    240  1.6  thorpej 	struct mii_data *mii;
    241  1.6  thorpej 	int bmsr, instance;
    242  1.6  thorpej {
    243  1.6  thorpej 	const char *sep = "";
    244  1.6  thorpej 
    245  1.6  thorpej #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
    246  1.6  thorpej #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
    247  1.6  thorpej 
    248  1.6  thorpej 	if (bmsr & BMSR_10THDX) {
    249  1.6  thorpej 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, instance), 0);
    250  1.6  thorpej 		PRINT("10baseT");
    251  1.6  thorpej 	}
    252  1.6  thorpej 	if (bmsr & BMSR_10TFDX) {
    253  1.6  thorpej 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, instance),
    254  1.6  thorpej 		    BMCR_FDX);
    255  1.6  thorpej 		PRINT("10baseT-FDX");
    256  1.6  thorpej 	}
    257  1.6  thorpej 	if (bmsr & BMSR_100TXHDX) {
    258  1.6  thorpej 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, instance),
    259  1.6  thorpej 		    BMCR_S100);
    260  1.6  thorpej 		PRINT("100baseTX");
    261  1.1   bouyer 	}
    262  1.6  thorpej 	if (bmsr & BMSR_100TXFDX) {
    263  1.6  thorpej 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, instance),
    264  1.6  thorpej 		    BMCR_S100|BMCR_FDX);
    265  1.6  thorpej 		PRINT("100baseTX-FDX");
    266  1.1   bouyer 	}
    267  1.6  thorpej 	if (bmsr & BMSR_100T4) {
    268  1.2  thorpej 		/*
    269  1.6  thorpej 		 * XXX How do you enable 100baseT4?  I assume we set
    270  1.6  thorpej 		 * XXX BMCR_S100 and then assume the PHYs will take
    271  1.6  thorpej 		 * XXX watever action is necessary to switch themselves
    272  1.6  thorpej 		 * XXX into T4 mode.
    273  1.2  thorpej 		 */
    274  1.6  thorpej 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_T4, 0, instance),
    275  1.6  thorpej 		    BMCR_S100);
    276  1.6  thorpej 		PRINT("100baseT4");
    277  1.1   bouyer 	}
    278  1.6  thorpej 	if (bmsr & BMSR_ANEG) {
    279  1.6  thorpej 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, instance),
    280  1.6  thorpej 		    BMCR_AUTOEN);
    281  1.6  thorpej 		PRINT("auto");
    282  1.1   bouyer 	}
    283  1.6  thorpej #undef ADD
    284  1.6  thorpej #undef PRINT
    285  1.1   bouyer }
    286