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