Home | History | Annotate | Line # | Download | only in mii
mii.c revision 1.13
      1 /*	$NetBSD: mii.c,v 1.13 1999/09/25 00:10:13 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 	int bmsr, offset = 0;
     73 
     74 	LIST_INIT(&mii->mii_phys);
     75 
     76 	for (ma.mii_phyno = 0; ma.mii_phyno < MII_NPHY; ma.mii_phyno++) {
     77 #if 0 /* XXX not yet --thorpej */
     78 		/*
     79 		 * Make sure we haven't already configured a PHY at this
     80 		 * address.  This allows mii_phy_probe() to be called
     81 		 * multiple times.
     82 		 */
     83 		for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
     84 		     child = LIST_NEXT(child, mii_list)) {
     85 			if (child->mii_phy == ma.mii_phyno) {
     86 				/*
     87 				 * Yes, there is already something
     88 				 * configured at this address.
     89 				 */
     90 				continue;
     91 				offset++;
     92 			}
     93 		}
     94 #endif
     95 
     96 		/*
     97 		 * Check to see if there is a PHY at this address.  Note,
     98 		 * many braindead PHYs report 0/0 in their ID registers,
     99 		 * so we test for media in the BMSR.
    100 		 */
    101 		bmsr = (*mii->mii_readreg)(parent, ma.mii_phyno, MII_BMSR);
    102 		if (bmsr == 0 || bmsr == 0xffff ||
    103 		    (bmsr & BMSR_MEDIAMASK) == 0) {
    104 			/* Assume no PHY at this address. */
    105 			continue;
    106 		}
    107 
    108 		/*
    109 		 * Extract the IDs.  Braindead PHYs will be handled by
    110 		 * the `ukphy' driver, as we have no ID information to
    111 		 * match on.
    112 		 */
    113 		ma.mii_id1 = (*mii->mii_readreg)(parent, ma.mii_phyno,
    114 		    MII_PHYIDR1);
    115 		ma.mii_id2 = (*mii->mii_readreg)(parent, ma.mii_phyno,
    116 		    MII_PHYIDR2);
    117 
    118 		ma.mii_data = mii;
    119 		ma.mii_capmask = capmask;
    120 
    121 		if ((child = (struct mii_softc *)config_found_sm(parent, &ma,
    122 		    mii_print, mii_submatch)) != NULL) {
    123 			/*
    124 			 * Link it up in the parent's MII data.
    125 			 */
    126 			LIST_INSERT_HEAD(&mii->mii_phys, child, mii_list);
    127 			child->mii_offset = offset;
    128 			mii->mii_instance++;
    129 		}
    130 		offset++;
    131 	}
    132 }
    133 
    134 int
    135 mii_print(aux, pnp)
    136 	void *aux;
    137 	const char *pnp;
    138 {
    139 	struct mii_attach_args *ma = aux;
    140 
    141 	if (pnp != NULL)
    142 		printf("OUI 0x%06x model 0x%04x rev %d at %s",
    143 		    MII_OUI(ma->mii_id1, ma->mii_id2), MII_MODEL(ma->mii_id2),
    144 		    MII_REV(ma->mii_id2), pnp);
    145 
    146 	printf(" phy %d", ma->mii_phyno);
    147 	return (UNCONF);
    148 }
    149 
    150 int
    151 mii_submatch(parent, cf, aux)
    152 	struct device *parent;
    153 	struct cfdata *cf;
    154 	void *aux;
    155 {
    156 	struct mii_attach_args *ma = aux;
    157 
    158 	if (ma->mii_phyno != cf->cf_loc[MIICF_PHY] &&
    159 	    cf->cf_loc[MIICF_PHY] != MIICF_PHY_DEFAULT)
    160 		return (0);
    161 
    162 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    163 }
    164 
    165 /*
    166  * Media changed; notify all PHYs.
    167  */
    168 int
    169 mii_mediachg(mii)
    170 	struct mii_data *mii;
    171 {
    172 	struct mii_softc *child;
    173 	int rv;
    174 
    175 	mii->mii_media_status = 0;
    176 	mii->mii_media_active = IFM_NONE;
    177 
    178 	for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
    179 	     child = LIST_NEXT(child, mii_list)) {
    180 		rv = (*child->mii_service)(child, mii, MII_MEDIACHG);
    181 		if (rv)
    182 			return (rv);
    183 	}
    184 	return (0);
    185 }
    186 
    187 /*
    188  * Call the PHY tick routines, used during autonegotiation.
    189  */
    190 void
    191 mii_tick(mii)
    192 	struct mii_data *mii;
    193 {
    194 	struct mii_softc *child;
    195 
    196 	for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
    197 	     child = LIST_NEXT(child, mii_list))
    198 		(void) (*child->mii_service)(child, mii, MII_TICK);
    199 }
    200 
    201 /*
    202  * Get media status from PHYs.
    203  */
    204 void
    205 mii_pollstat(mii)
    206 	struct mii_data *mii;
    207 {
    208 	struct mii_softc *child;
    209 
    210 	mii->mii_media_status = 0;
    211 	mii->mii_media_active = IFM_NONE;
    212 
    213 	for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
    214 	     child = LIST_NEXT(child, mii_list))
    215 		(void) (*child->mii_service)(child, mii, MII_POLLSTAT);
    216 }
    217