Home | History | Annotate | Line # | Download | only in mii
mii.c revision 1.48
      1 /*	$NetBSD: mii.c,v 1.48 2008/05/05 01:37:56 tsutsui 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  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * MII bus layer, glues MII-capable network interface drivers to sharable
     35  * PHY drivers.
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: mii.c,v 1.48 2008/05/05 01:37:56 tsutsui Exp $");
     40 
     41 #include <sys/param.h>
     42 #include <sys/device.h>
     43 #include <sys/systm.h>
     44 #include <sys/socket.h>
     45 
     46 #include <net/if.h>
     47 #include <net/if_media.h>
     48 
     49 #include <dev/mii/mii.h>
     50 #include <dev/mii/miivar.h>
     51 
     52 #include "locators.h"
     53 
     54 static int	mii_print(void *, const char *);
     55 
     56 /*
     57  * Helper function used by network interface drivers, attaches PHYs
     58  * to the network interface driver parent.
     59  */
     60 void
     61 mii_attach(device_t parent, struct mii_data *mii, int capmask,
     62     int phyloc, int offloc, int flags)
     63 {
     64 	struct mii_attach_args ma;
     65 	struct mii_softc *child;
     66 	int bmsr, offset = 0;
     67 	int phymin, phymax;
     68 	int locs[MIICF_NLOCS];
     69 
     70 	if (phyloc != MII_PHY_ANY && offloc != MII_OFFSET_ANY)
     71 		panic("mii_attach: phyloc and offloc specified");
     72 
     73 	if (phyloc == MII_PHY_ANY) {
     74 		phymin = 0;
     75 		phymax = MII_NPHY - 1;
     76 	} else
     77 		phymin = phymax = phyloc;
     78 
     79 	if ((mii->mii_flags & MIIF_INITDONE) == 0) {
     80 		LIST_INIT(&mii->mii_phys);
     81 		mii->mii_flags |= MIIF_INITDONE;
     82 	}
     83 
     84 	for (ma.mii_phyno = phymin; ma.mii_phyno <= phymax; ma.mii_phyno++) {
     85 		/*
     86 		 * Make sure we haven't already configured a PHY at this
     87 		 * address.  This allows mii_attach() to be called
     88 		 * multiple times.
     89 		 */
     90 		LIST_FOREACH(child, &mii->mii_phys, mii_list) {
     91 			if (child->mii_phy == ma.mii_phyno) {
     92 				/*
     93 				 * Yes, there is already something
     94 				 * configured at this address.
     95 				 */
     96 				offset++;
     97 				continue;
     98 			}
     99 		}
    100 
    101 		/*
    102 		 * Check to see if there is a PHY at this address.  Note,
    103 		 * many braindead PHYs report 0/0 in their ID registers,
    104 		 * so we test for media in the BMSR.
    105 		 */
    106 		bmsr = (*mii->mii_readreg)(parent, ma.mii_phyno, MII_BMSR);
    107 		if (bmsr == 0 || bmsr == 0xffff ||
    108 		    (bmsr & (BMSR_EXTSTAT|BMSR_MEDIAMASK)) == 0) {
    109 			/* Assume no PHY at this address. */
    110 			continue;
    111 		}
    112 
    113 		/*
    114 		 * There is a PHY at this address.  If we were given an
    115 		 * `offset' locator, skip this PHY if it doesn't match.
    116 		 */
    117 		if (offloc != MII_OFFSET_ANY && offloc != offset) {
    118 			offset++;
    119 			continue;
    120 		}
    121 
    122 		/*
    123 		 * Extract the IDs.  Braindead PHYs will be handled by
    124 		 * the `ukphy' driver, as we have no ID information to
    125 		 * match on.
    126 		 */
    127 		ma.mii_id1 = (*mii->mii_readreg)(parent, ma.mii_phyno,
    128 		    MII_PHYIDR1);
    129 		ma.mii_id2 = (*mii->mii_readreg)(parent, ma.mii_phyno,
    130 		    MII_PHYIDR2);
    131 
    132 		ma.mii_data = mii;
    133 		ma.mii_capmask = capmask;
    134 		ma.mii_flags = flags | (mii->mii_flags & MIIF_INHERIT_MASK);
    135 
    136 		locs[MIICF_PHY] = ma.mii_phyno;
    137 
    138 		child = device_private(config_found_sm_loc(parent, "mii",
    139 			locs, &ma, mii_print, config_stdsubmatch));
    140 		if (child) {
    141 			/*
    142 			 * Link it up in the parent's MII data.
    143 			 */
    144 			callout_init(&child->mii_nway_ch, 0);
    145 			LIST_INSERT_HEAD(&mii->mii_phys, child, mii_list);
    146 			child->mii_offset = offset;
    147 			mii->mii_instance++;
    148 		}
    149 		offset++;
    150 	}
    151 }
    152 
    153 void
    154 mii_activate(struct mii_data *mii, enum devact act, int phyloc, int offloc)
    155 {
    156 	struct mii_softc *child;
    157 
    158 	if (phyloc != MII_PHY_ANY && offloc != MII_OFFSET_ANY)
    159 		panic("mii_activate: phyloc and offloc specified");
    160 
    161 	if ((mii->mii_flags & MIIF_INITDONE) == 0)
    162 		return;
    163 
    164 	LIST_FOREACH(child, &mii->mii_phys, mii_list) {
    165 		if (phyloc != MII_PHY_ANY || offloc != MII_OFFSET_ANY) {
    166 			if (phyloc != MII_PHY_ANY &&
    167 			    phyloc != child->mii_phy)
    168 				continue;
    169 			if (offloc != MII_OFFSET_ANY &&
    170 			    offloc != child->mii_offset)
    171 				continue;
    172 		}
    173 		switch (act) {
    174 		case DVACT_ACTIVATE:
    175 			panic("mii_activate: DVACT_ACTIVATE");
    176 			break;
    177 
    178 		case DVACT_DEACTIVATE:
    179 			if (config_deactivate(child->mii_dev) != 0)
    180 				panic("%s: config_activate(%d) failed",
    181 				    device_xname(child->mii_dev), act);
    182 		}
    183 	}
    184 }
    185 
    186 void
    187 mii_detach(struct mii_data *mii, int phyloc, int offloc)
    188 {
    189 	struct mii_softc *child, *nchild;
    190 
    191 	if (phyloc != MII_PHY_ANY && offloc != MII_PHY_ANY)
    192 		panic("mii_detach: phyloc and offloc specified");
    193 
    194 	if ((mii->mii_flags & MIIF_INITDONE) == 0)
    195 		return;
    196 
    197 	for (child = LIST_FIRST(&mii->mii_phys);
    198 	     child != NULL; child = nchild) {
    199 		nchild = LIST_NEXT(child, mii_list);
    200 		if (phyloc != MII_PHY_ANY || offloc != MII_OFFSET_ANY) {
    201 			if (phyloc != MII_PHY_ANY &&
    202 			    phyloc != child->mii_phy)
    203 				continue;
    204 			if (offloc != MII_OFFSET_ANY &&
    205 			    offloc != child->mii_offset)
    206 				continue;
    207 		}
    208 		(void)config_detach(child->mii_dev, DETACH_FORCE);
    209 	}
    210 }
    211 
    212 static int
    213 mii_print(void *aux, const char *pnp)
    214 {
    215 	struct mii_attach_args *ma = aux;
    216 
    217 	if (pnp != NULL)
    218 		aprint_normal("OUI 0x%06x model 0x%04x rev %d at %s",
    219 		    MII_OUI(ma->mii_id1, ma->mii_id2), MII_MODEL(ma->mii_id2),
    220 		    MII_REV(ma->mii_id2), pnp);
    221 
    222 	aprint_normal(" phy %d", ma->mii_phyno);
    223 	return (UNCONF);
    224 }
    225 
    226 static inline int
    227 phy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
    228 {
    229 	if (!device_is_active(sc->mii_dev))
    230 		return ENXIO;
    231 	return PHY_SERVICE(sc, mii, cmd);
    232 }
    233 
    234 int
    235 mii_ifmedia_change(struct mii_data *mii)
    236 {
    237 	return ifmedia_change(&mii->mii_media, mii->mii_ifp);
    238 }
    239 
    240 /*
    241  * Media changed; notify all PHYs.
    242  */
    243 int
    244 mii_mediachg(struct mii_data *mii)
    245 {
    246 	struct mii_softc *child;
    247 	int rv;
    248 
    249 	mii->mii_media_status = 0;
    250 	mii->mii_media_active = IFM_NONE;
    251 
    252 	LIST_FOREACH(child, &mii->mii_phys, mii_list) {
    253 		rv = phy_service(child, mii, MII_MEDIACHG);
    254 		if (rv)
    255 			return (rv);
    256 	}
    257 	return (0);
    258 }
    259 
    260 /*
    261  * Call the PHY tick routines, used during autonegotiation.
    262  */
    263 void
    264 mii_tick(struct mii_data *mii)
    265 {
    266 	struct mii_softc *child;
    267 
    268 	LIST_FOREACH(child, &mii->mii_phys, mii_list)
    269 		(void)phy_service(child, mii, MII_TICK);
    270 }
    271 
    272 /*
    273  * Get media status from PHYs.
    274  */
    275 void
    276 mii_pollstat(struct mii_data *mii)
    277 {
    278 	struct mii_softc *child;
    279 
    280 	mii->mii_media_status = 0;
    281 	mii->mii_media_active = IFM_NONE;
    282 
    283 	LIST_FOREACH(child, &mii->mii_phys, mii_list)
    284 		(void)phy_service(child, mii, MII_POLLSTAT);
    285 }
    286 
    287 /*
    288  * Inform the PHYs that the interface is down.
    289  */
    290 void
    291 mii_down(struct mii_data *mii)
    292 {
    293 	struct mii_softc *child;
    294 
    295 	LIST_FOREACH(child, &mii->mii_phys, 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