Home | History | Annotate | Line # | Download | only in mii
mii.c revision 1.41
      1 /*	$NetBSD: mii.c,v 1.41 2007/07/09 21:00:51 ad 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.41 2007/07/09 21:00:51 ad 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 
     63 /*
     64  * Helper function used by network interface drivers, attaches PHYs
     65  * to the network interface driver parent.
     66  */
     67 void
     68 mii_attach(struct device *parent, struct mii_data *mii, int capmask,
     69     int phyloc, int offloc, int flags)
     70 {
     71 	struct mii_attach_args ma;
     72 	struct mii_softc *child;
     73 	int bmsr, offset = 0;
     74 	int phymin, phymax;
     75 	int locs[MIICF_NLOCS];
     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 		locs[MIICF_PHY] = ma.mii_phyno;
    145 
    146 		child = (struct mii_softc *)config_found_sm_loc(parent, "mii",
    147 			locs, &ma, mii_print, config_stdsubmatch);
    148 		if (child) {
    149 			/*
    150 			 * Link it up in the parent's MII data.
    151 			 */
    152 			callout_init(&child->mii_nway_ch, 0);
    153 			LIST_INSERT_HEAD(&mii->mii_phys, child, mii_list);
    154 			child->mii_offset = offset;
    155 			mii->mii_instance++;
    156 		}
    157 		offset++;
    158 	}
    159 }
    160 
    161 void
    162 mii_activate(struct mii_data *mii, enum devact act, int phyloc, int offloc)
    163 {
    164 	struct mii_softc *child;
    165 
    166 	if (phyloc != MII_PHY_ANY && offloc != MII_OFFSET_ANY)
    167 		panic("mii_activate: phyloc and offloc specified");
    168 
    169 	if ((mii->mii_flags & MIIF_INITDONE) == 0)
    170 		return;
    171 
    172 	for (child = LIST_FIRST(&mii->mii_phys);
    173 	     child != NULL; child = LIST_NEXT(child, mii_list)) {
    174 		if (phyloc != MII_PHY_ANY || offloc != MII_OFFSET_ANY) {
    175 			if (phyloc != MII_PHY_ANY &&
    176 			    phyloc != child->mii_phy)
    177 				continue;
    178 			if (offloc != MII_OFFSET_ANY &&
    179 			    offloc != child->mii_offset)
    180 				continue;
    181 		}
    182 		switch (act) {
    183 		case DVACT_ACTIVATE:
    184 			panic("mii_activate: DVACT_ACTIVATE");
    185 			break;
    186 
    187 		case DVACT_DEACTIVATE:
    188 			if (config_deactivate(&child->mii_dev) != 0)
    189 				panic("%s: config_activate(%d) failed",
    190 				    child->mii_dev.dv_xname, act);
    191 		}
    192 	}
    193 }
    194 
    195 void
    196 mii_detach(struct mii_data *mii, int phyloc, int offloc)
    197 {
    198 	struct mii_softc *child, *nchild;
    199 
    200 	if (phyloc != MII_PHY_ANY && offloc != MII_PHY_ANY)
    201 		panic("mii_detach: phyloc and offloc specified");
    202 
    203 	if ((mii->mii_flags & MIIF_INITDONE) == 0)
    204 		return;
    205 
    206 	for (child = LIST_FIRST(&mii->mii_phys);
    207 	     child != NULL; child = nchild) {
    208 		nchild = LIST_NEXT(child, mii_list);
    209 		if (phyloc != MII_PHY_ANY || offloc != MII_OFFSET_ANY) {
    210 			if (phyloc != MII_PHY_ANY &&
    211 			    phyloc != child->mii_phy)
    212 				continue;
    213 			if (offloc != MII_OFFSET_ANY &&
    214 			    offloc != child->mii_offset)
    215 				continue;
    216 		}
    217 		LIST_REMOVE(child, mii_list);
    218 		(void) config_detach(&child->mii_dev, DETACH_FORCE);
    219 	}
    220 }
    221 
    222 static int
    223 mii_print(void *aux, const char *pnp)
    224 {
    225 	struct mii_attach_args *ma = aux;
    226 
    227 	if (pnp != NULL)
    228 		aprint_normal("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 	aprint_normal(" phy %d", ma->mii_phyno);
    233 	return (UNCONF);
    234 }
    235 
    236 /*
    237  * Media changed; notify all PHYs.
    238  */
    239 int
    240 mii_mediachg(struct mii_data *mii)
    241 {
    242 	struct mii_softc *child;
    243 	int rv;
    244 
    245 	mii->mii_media_status = 0;
    246 	mii->mii_media_active = IFM_NONE;
    247 
    248 	for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
    249 	     child = LIST_NEXT(child, mii_list)) {
    250 		rv = PHY_SERVICE(child, mii, MII_MEDIACHG);
    251 		if (rv)
    252 			return (rv);
    253 	}
    254 	return (0);
    255 }
    256 
    257 /*
    258  * Call the PHY tick routines, used during autonegotiation.
    259  */
    260 void
    261 mii_tick(struct mii_data *mii)
    262 {
    263 	struct mii_softc *child;
    264 
    265 	for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
    266 	     child = LIST_NEXT(child, mii_list))
    267 		(void) PHY_SERVICE(child, mii, MII_TICK);
    268 }
    269 
    270 /*
    271  * Get media status from PHYs.
    272  */
    273 void
    274 mii_pollstat(struct mii_data *mii)
    275 {
    276 	struct mii_softc *child;
    277 
    278 	mii->mii_media_status = 0;
    279 	mii->mii_media_active = IFM_NONE;
    280 
    281 	for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
    282 	     child = LIST_NEXT(child, mii_list))
    283 		(void) PHY_SERVICE(child, mii, MII_POLLSTAT);
    284 }
    285 
    286 /*
    287  * Inform the PHYs that the interface is down.
    288  */
    289 void
    290 mii_down(struct mii_data *mii)
    291 {
    292 	struct mii_softc *child;
    293 
    294 	for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
    295 	     child = LIST_NEXT(child, mii_list))
    296 		(void) PHY_SERVICE(child, mii, MII_DOWN);
    297 }
    298 
    299 static unsigned char
    300 bitreverse(unsigned char x)
    301 {
    302 	static unsigned char nibbletab[16] = {
    303 		0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15
    304 	};
    305 
    306 	return ((nibbletab[x & 15] << 4) | nibbletab[x >> 4]);
    307 }
    308 
    309 u_int
    310 mii_oui(u_int id1, u_int id2)
    311 {
    312 	u_int h;
    313 
    314 	h = (id1 << 6) | (id2 >> 10);
    315 
    316 	return ((bitreverse(h >> 16) << 16) |
    317 		(bitreverse((h >> 8) & 255) << 8) |
    318 		bitreverse(h & 255));
    319 }
    320