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