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