Home | History | Annotate | Line # | Download | only in mii
mii.c revision 1.17
      1 /*	$NetBSD: mii.c,v 1.17 2000/01/27 16:44:30 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 2000 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, phyloc, offloc)
     66 	struct device *parent;
     67 	struct mii_data *mii;
     68 	int capmask, phyloc, offloc;
     69 {
     70 	struct mii_attach_args ma;
     71 	struct mii_softc *child;
     72 	int bmsr, offset = 0;
     73 	int phymin, phymax;
     74 
     75 	if (phyloc != MII_PHY_ANY && offloc != MII_PHY_ANY)
     76 		panic("mii_phy_probe: phyloc and offloc specified");
     77 
     78 	if (phyloc == MII_PHY_ANY) {
     79 		phymin = 0;
     80 		phymax = MII_NPHY - 1;
     81 	} else
     82 		phymin = phymax = phyloc;
     83 
     84 	if ((mii->mii_flags & MIIF_INITDONE) == 0) {
     85 		LIST_INIT(&mii->mii_phys);
     86 		mii->mii_flags |= MIIF_INITDONE;
     87 	}
     88 
     89 	for (ma.mii_phyno = phymin; ma.mii_phyno <= phymax; ma.mii_phyno++) {
     90 		/*
     91 		 * Make sure we haven't already configured a PHY at this
     92 		 * address.  This allows mii_phy_probe() to be called
     93 		 * multiple times.
     94 		 */
     95 		for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
     96 		     child = LIST_NEXT(child, mii_list)) {
     97 			if (child->mii_phy == ma.mii_phyno) {
     98 				/*
     99 				 * Yes, there is already something
    100 				 * configured at this address.
    101 				 */
    102 				offset++;
    103 				continue;
    104 			}
    105 		}
    106 
    107 		/*
    108 		 * Check to see if there is a PHY at this address.  Note,
    109 		 * many braindead PHYs report 0/0 in their ID registers,
    110 		 * so we test for media in the BMSR.
    111 		 */
    112 		bmsr = (*mii->mii_readreg)(parent, ma.mii_phyno, MII_BMSR);
    113 		if (bmsr == 0 || bmsr == 0xffff ||
    114 		    (bmsr & BMSR_MEDIAMASK) == 0) {
    115 			/* Assume no PHY at this address. */
    116 			continue;
    117 		}
    118 
    119 		/*
    120 		 * There is a PHY at this address.  If we were given an
    121 		 * `offset' locator, skip this PHY if it doesn't match.
    122 		 */
    123 		if (offloc != MII_OFFSET_ANY && offloc != offset) {
    124 			offset++;
    125 			continue;
    126 		}
    127 
    128 		/*
    129 		 * Extract the IDs.  Braindead PHYs will be handled by
    130 		 * the `ukphy' driver, as we have no ID information to
    131 		 * match on.
    132 		 */
    133 		ma.mii_id1 = (*mii->mii_readreg)(parent, ma.mii_phyno,
    134 		    MII_PHYIDR1);
    135 		ma.mii_id2 = (*mii->mii_readreg)(parent, ma.mii_phyno,
    136 		    MII_PHYIDR2);
    137 
    138 		ma.mii_data = mii;
    139 		ma.mii_capmask = capmask;
    140 
    141 		if ((child = (struct mii_softc *)config_found_sm(parent, &ma,
    142 		    mii_print, mii_submatch)) != NULL) {
    143 			/*
    144 			 * Link it up in the parent's MII data.
    145 			 */
    146 			LIST_INSERT_HEAD(&mii->mii_phys, child, mii_list);
    147 			child->mii_offset = offset;
    148 			mii->mii_instance++;
    149 		}
    150 		offset++;
    151 	}
    152 }
    153 
    154 void
    155 mii_phy_activate(mii, act, phyloc, offloc)
    156 	struct mii_data *mii;
    157 	enum devact act;
    158 	int phyloc, offloc;
    159 {
    160 	struct mii_softc *child;
    161 
    162 	if (phyloc != MII_PHY_ANY && offloc != MII_PHY_ANY)
    163 		panic("mii_phy_detach: phyloc and offloc specified");
    164 
    165 	if ((mii->mii_flags & MIIF_INITDONE) == 0)
    166 		return;
    167 
    168 	for (child = LIST_FIRST(&mii->mii_phys);
    169 	     child != NULL; child = LIST_NEXT(child, mii_list)) {
    170 		if (phyloc != MII_PHY_ANY || offloc != MII_OFFSET_ANY) {
    171 			if (phyloc != MII_PHY_ANY &&
    172 			    phyloc != child->mii_phy)
    173 				continue;
    174 			if (offloc != MII_OFFSET_ANY &&
    175 			    offloc != child->mii_offset)
    176 				continue;
    177 		}
    178 		switch (act) {
    179 		case DVACT_ACTIVATE:
    180 			panic("mii_phy_activate: DVACT_ACTIVATE");
    181 			break;
    182 
    183 		case DVACT_DEACTIVATE:
    184 			if (config_deactivate(&child->mii_dev) != 0)
    185 				panic("%s: config_activate(%d) failed\n",
    186 				    child->mii_dev.dv_xname, act);
    187 		}
    188 	}
    189 }
    190 
    191 void
    192 mii_phy_detach(mii, phyloc, offloc)
    193 	struct mii_data *mii;
    194 	int phyloc, offloc;
    195 {
    196 	struct mii_softc *child, *nchild;
    197 
    198 	if (phyloc != MII_PHY_ANY && offloc != MII_PHY_ANY)
    199 		panic("mii_phy_detach: phyloc and offloc specified");
    200 
    201 	if ((mii->mii_flags & MIIF_INITDONE) == 0)
    202 		return;
    203 
    204 	for (child = LIST_FIRST(&mii->mii_phys);
    205 	     child != NULL; child = nchild) {
    206 		nchild = LIST_NEXT(child, mii_list);
    207 		if (phyloc != MII_PHY_ANY || offloc != MII_OFFSET_ANY) {
    208 			if (phyloc != MII_PHY_ANY &&
    209 			    phyloc != child->mii_phy)
    210 				continue;
    211 			if (offloc != MII_OFFSET_ANY &&
    212 			    offloc != child->mii_offset)
    213 				continue;
    214 		}
    215 		LIST_REMOVE(child, mii_list);
    216 		(void) config_detach(&child->mii_dev, DETACH_FORCE);
    217 	}
    218 }
    219 
    220 int
    221 mii_print(aux, pnp)
    222 	void *aux;
    223 	const char *pnp;
    224 {
    225 	struct mii_attach_args *ma = aux;
    226 
    227 	if (pnp != NULL)
    228 		printf("OUI 0x%06x model 0x%04x rev %d at %s",
    229 		    MII_OUI(ma->mii_id1, ma->mii_id2), MII_MODEL(ma->mii_id2),
    230 		    MII_REV(ma->mii_id2), pnp);
    231 
    232 	printf(" phy %d", ma->mii_phyno);
    233 	return (UNCONF);
    234 }
    235 
    236 int
    237 mii_submatch(parent, cf, aux)
    238 	struct device *parent;
    239 	struct cfdata *cf;
    240 	void *aux;
    241 {
    242 	struct mii_attach_args *ma = aux;
    243 
    244 	if (ma->mii_phyno != cf->cf_loc[MIICF_PHY] &&
    245 	    cf->cf_loc[MIICF_PHY] != MIICF_PHY_DEFAULT)
    246 		return (0);
    247 
    248 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    249 }
    250 
    251 /*
    252  * Media changed; notify all PHYs.
    253  */
    254 int
    255 mii_mediachg(mii)
    256 	struct mii_data *mii;
    257 {
    258 	struct mii_softc *child;
    259 	int rv;
    260 
    261 	mii->mii_media_status = 0;
    262 	mii->mii_media_active = IFM_NONE;
    263 
    264 	for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
    265 	     child = LIST_NEXT(child, mii_list)) {
    266 		rv = (*child->mii_service)(child, mii, MII_MEDIACHG);
    267 		if (rv)
    268 			return (rv);
    269 	}
    270 	return (0);
    271 }
    272 
    273 /*
    274  * Call the PHY tick routines, used during autonegotiation.
    275  */
    276 void
    277 mii_tick(mii)
    278 	struct mii_data *mii;
    279 {
    280 	struct mii_softc *child;
    281 
    282 	for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
    283 	     child = LIST_NEXT(child, mii_list))
    284 		(void) (*child->mii_service)(child, mii, MII_TICK);
    285 }
    286 
    287 /*
    288  * Get media status from PHYs.
    289  */
    290 void
    291 mii_pollstat(mii)
    292 	struct mii_data *mii;
    293 {
    294 	struct mii_softc *child;
    295 
    296 	mii->mii_media_status = 0;
    297 	mii->mii_media_active = IFM_NONE;
    298 
    299 	for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
    300 	     child = LIST_NEXT(child, mii_list))
    301 		(void) (*child->mii_service)(child, mii, MII_POLLSTAT);
    302 }
    303 
    304 /*
    305  * Inform the PHYs that the interface is down.
    306  */
    307 void
    308 mii_down(mii)
    309 	struct mii_data *mii;
    310 {
    311 	struct mii_softc *child;
    312 
    313 	for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
    314 	     child = LIST_NEXT(child, mii_list))
    315 		(void) (*child->mii_service)(child, mii, MII_DOWN);
    316 }
    317