Home | History | Annotate | Line # | Download | only in mii
mii.c revision 1.43
      1 /*	$NetBSD: mii.c,v 1.43 2007/12/29 19:34:55 dyoung 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.43 2007/12/29 19:34:55 dyoung 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 		LIST_FOREACH(child, &mii->mii_phys, mii_list) {
     98 			if (child->mii_phy == ma.mii_phyno) {
     99 				/*
    100 				 * Yes, there is already something
    101 				 * configured at this address.
    102 				 */
    103 				offset++;
    104 				continue;
    105 			}
    106 		}
    107 
    108 		/*
    109 		 * Check to see if there is a PHY at this address.  Note,
    110 		 * many braindead PHYs report 0/0 in their ID registers,
    111 		 * so we test for media in the BMSR.
    112 		 */
    113 		bmsr = (*mii->mii_readreg)(parent, ma.mii_phyno, MII_BMSR);
    114 		if (bmsr == 0 || bmsr == 0xffff ||
    115 		    (bmsr & (BMSR_EXTSTAT|BMSR_MEDIAMASK)) == 0) {
    116 			/* Assume no PHY at this address. */
    117 			continue;
    118 		}
    119 
    120 		/*
    121 		 * There is a PHY at this address.  If we were given an
    122 		 * `offset' locator, skip this PHY if it doesn't match.
    123 		 */
    124 		if (offloc != MII_OFFSET_ANY && offloc != offset) {
    125 			offset++;
    126 			continue;
    127 		}
    128 
    129 		/*
    130 		 * Extract the IDs.  Braindead PHYs will be handled by
    131 		 * the `ukphy' driver, as we have no ID information to
    132 		 * match on.
    133 		 */
    134 		ma.mii_id1 = (*mii->mii_readreg)(parent, ma.mii_phyno,
    135 		    MII_PHYIDR1);
    136 		ma.mii_id2 = (*mii->mii_readreg)(parent, ma.mii_phyno,
    137 		    MII_PHYIDR2);
    138 
    139 		ma.mii_data = mii;
    140 		ma.mii_capmask = capmask;
    141 		ma.mii_flags = flags | (mii->mii_flags & MIIF_INHERIT_MASK);
    142 
    143 		locs[MIICF_PHY] = ma.mii_phyno;
    144 
    145 		child = (struct mii_softc *)config_found_sm_loc(parent, "mii",
    146 			locs, &ma, mii_print, config_stdsubmatch);
    147 		if (child) {
    148 			/*
    149 			 * Link it up in the parent's MII data.
    150 			 */
    151 			callout_init(&child->mii_nway_ch, 0);
    152 			LIST_INSERT_HEAD(&mii->mii_phys, child, mii_list);
    153 			child->mii_offset = offset;
    154 			mii->mii_instance++;
    155 		}
    156 		offset++;
    157 	}
    158 }
    159 
    160 void
    161 mii_activate(struct mii_data *mii, enum devact act, int phyloc, int offloc)
    162 {
    163 	struct mii_softc *child;
    164 
    165 	if (phyloc != MII_PHY_ANY && offloc != MII_OFFSET_ANY)
    166 		panic("mii_activate: phyloc and offloc specified");
    167 
    168 	if ((mii->mii_flags & MIIF_INITDONE) == 0)
    169 		return;
    170 
    171 	LIST_FOREACH(child, &mii->mii_phys, mii_list) {
    172 		if (phyloc != MII_PHY_ANY || offloc != MII_OFFSET_ANY) {
    173 			if (phyloc != MII_PHY_ANY &&
    174 			    phyloc != child->mii_phy)
    175 				continue;
    176 			if (offloc != MII_OFFSET_ANY &&
    177 			    offloc != child->mii_offset)
    178 				continue;
    179 		}
    180 		switch (act) {
    181 		case DVACT_ACTIVATE:
    182 			panic("mii_activate: DVACT_ACTIVATE");
    183 			break;
    184 
    185 		case DVACT_DEACTIVATE:
    186 			if (config_deactivate(&child->mii_dev) != 0)
    187 				panic("%s: config_activate(%d) failed",
    188 				    child->mii_dev.dv_xname, act);
    189 		}
    190 	}
    191 }
    192 
    193 void
    194 mii_detach(struct mii_data *mii, int phyloc, int offloc)
    195 {
    196 	struct mii_softc *child, *nchild;
    197 
    198 	if (phyloc != MII_PHY_ANY && offloc != MII_PHY_ANY)
    199 		panic("mii_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 static int
    221 mii_print(void *aux, const char *pnp)
    222 {
    223 	struct mii_attach_args *ma = aux;
    224 
    225 	if (pnp != NULL)
    226 		aprint_normal("OUI 0x%06x model 0x%04x rev %d at %s",
    227 		    MII_OUI(ma->mii_id1, ma->mii_id2), MII_MODEL(ma->mii_id2),
    228 		    MII_REV(ma->mii_id2), pnp);
    229 
    230 	aprint_normal(" phy %d", ma->mii_phyno);
    231 	return (UNCONF);
    232 }
    233 
    234 static inline int
    235 phy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
    236 {
    237 	if (!device_is_active(&sc->mii_dev))
    238 		return ENXIO;
    239 	return PHY_SERVICE(sc, mii, cmd);
    240 }
    241 
    242 /*
    243  * Media changed; notify all PHYs.
    244  */
    245 int
    246 mii_mediachg(struct mii_data *mii)
    247 {
    248 	struct mii_softc *child;
    249 	int rv;
    250 
    251 	mii->mii_media_status = 0;
    252 	mii->mii_media_active = IFM_NONE;
    253 
    254 	LIST_FOREACH(child, &mii->mii_phys, mii_list) {
    255 		rv = phy_service(child, mii, MII_MEDIACHG);
    256 		if (rv)
    257 			return (rv);
    258 	}
    259 	return (0);
    260 }
    261 
    262 /*
    263  * Call the PHY tick routines, used during autonegotiation.
    264  */
    265 void
    266 mii_tick(struct mii_data *mii)
    267 {
    268 	struct mii_softc *child;
    269 
    270 	LIST_FOREACH(child, &mii->mii_phys, mii_list)
    271 		(void)phy_service(child, mii, MII_TICK);
    272 }
    273 
    274 /*
    275  * Get media status from PHYs.
    276  */
    277 void
    278 mii_pollstat(struct mii_data *mii)
    279 {
    280 	struct mii_softc *child;
    281 
    282 	mii->mii_media_status = 0;
    283 	mii->mii_media_active = IFM_NONE;
    284 
    285 	LIST_FOREACH(child, &mii->mii_phys, mii_list)
    286 		(void)phy_service(child, mii, MII_POLLSTAT);
    287 }
    288 
    289 /*
    290  * Inform the PHYs that the interface is down.
    291  */
    292 void
    293 mii_down(struct mii_data *mii)
    294 {
    295 	struct mii_softc *child;
    296 
    297 	LIST_FOREACH(child, &mii->mii_phys, mii_list)
    298 		(void)phy_service(child, mii, MII_DOWN);
    299 }
    300 
    301 static unsigned char
    302 bitreverse(unsigned char x)
    303 {
    304 	static unsigned char nibbletab[16] = {
    305 		0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15
    306 	};
    307 
    308 	return ((nibbletab[x & 15] << 4) | nibbletab[x >> 4]);
    309 }
    310 
    311 u_int
    312 mii_oui(u_int id1, u_int id2)
    313 {
    314 	u_int h;
    315 
    316 	h = (id1 << 6) | (id2 >> 10);
    317 
    318 	return ((bitreverse(h >> 16) << 16) |
    319 		(bitreverse((h >> 8) & 255) << 8) |
    320 		bitreverse(h & 255));
    321 }
    322