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