Home | History | Annotate | Line # | Download | only in mii
mii.c revision 1.13.2.1
      1  1.13.2.1   bouyer /*	$NetBSD: mii.c,v 1.13.2.1 2000/11/20 11:42:10 bouyer Exp $	*/
      2       1.6  thorpej 
      3       1.6  thorpej /*-
      4  1.13.2.1   bouyer  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
      5       1.6  thorpej  * All rights reserved.
      6       1.6  thorpej  *
      7       1.6  thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8       1.6  thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9       1.6  thorpej  * NASA Ames Research Center.
     10       1.1   bouyer  *
     11       1.1   bouyer  * Redistribution and use in source and binary forms, with or without
     12       1.1   bouyer  * modification, are permitted provided that the following conditions
     13       1.1   bouyer  * are met:
     14       1.1   bouyer  * 1. Redistributions of source code must retain the above copyright
     15       1.1   bouyer  *    notice, this list of conditions and the following disclaimer.
     16       1.1   bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     17       1.1   bouyer  *    notice, this list of conditions and the following disclaimer in the
     18       1.1   bouyer  *    documentation and/or other materials provided with the distribution.
     19       1.1   bouyer  * 3. All advertising materials mentioning features or use of this software
     20       1.1   bouyer  *    must display the following acknowledgement:
     21       1.6  thorpej  *	This product includes software developed by the NetBSD
     22       1.6  thorpej  *	Foundation, Inc. and its contributors.
     23       1.6  thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24       1.6  thorpej  *    contributors may be used to endorse or promote products derived
     25       1.6  thorpej  *    from this software without specific prior written permission.
     26       1.1   bouyer  *
     27       1.6  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28       1.6  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29       1.6  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30       1.6  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31       1.6  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32       1.6  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33       1.6  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34       1.6  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35       1.6  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36       1.6  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37       1.6  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38       1.6  thorpej  */
     39       1.6  thorpej 
     40       1.6  thorpej /*
     41       1.6  thorpej  * MII bus layer, glues MII-capable network interface drivers to sharable
     42       1.6  thorpej  * PHY drivers.  This exports an interface compatible with BSD/OS 3.0's,
     43       1.6  thorpej  * plus some NetBSD extensions.
     44       1.1   bouyer  */
     45       1.1   bouyer 
     46       1.1   bouyer #include <sys/param.h>
     47       1.6  thorpej #include <sys/device.h>
     48       1.1   bouyer #include <sys/systm.h>
     49       1.1   bouyer #include <sys/socket.h>
     50       1.1   bouyer 
     51       1.1   bouyer #include <net/if.h>
     52       1.1   bouyer #include <net/if_media.h>
     53       1.1   bouyer 
     54       1.6  thorpej #include <dev/mii/mii.h>
     55       1.6  thorpej #include <dev/mii/miivar.h>
     56       1.1   bouyer 
     57       1.6  thorpej int	mii_print __P((void *, const char *));
     58       1.6  thorpej int	mii_submatch __P((struct device *, struct cfdata *, void *));
     59       1.1   bouyer 
     60       1.6  thorpej /*
     61       1.6  thorpej  * Helper function used by network interface drivers, attaches PHYs
     62       1.6  thorpej  * to the network interface driver parent.
     63       1.6  thorpej  */
     64       1.6  thorpej void
     65  1.13.2.1   bouyer mii_attach(parent, mii, capmask, phyloc, offloc, flags)
     66       1.6  thorpej 	struct device *parent;
     67       1.6  thorpej 	struct mii_data *mii;
     68  1.13.2.1   bouyer 	int capmask, phyloc, offloc, flags;
     69       1.6  thorpej {
     70       1.6  thorpej 	struct mii_attach_args ma;
     71       1.6  thorpej 	struct mii_softc *child;
     72      1.13  thorpej 	int bmsr, offset = 0;
     73  1.13.2.1   bouyer 	int phymin, phymax;
     74       1.1   bouyer 
     75  1.13.2.1   bouyer 	if (phyloc != MII_PHY_ANY && offloc != MII_PHY_ANY)
     76  1.13.2.1   bouyer 		panic("mii_attach: phyloc and offloc specified");
     77       1.1   bouyer 
     78  1.13.2.1   bouyer 	if (phyloc == MII_PHY_ANY) {
     79  1.13.2.1   bouyer 		phymin = 0;
     80  1.13.2.1   bouyer 		phymax = MII_NPHY - 1;
     81  1.13.2.1   bouyer 	} else
     82  1.13.2.1   bouyer 		phymin = phymax = phyloc;
     83  1.13.2.1   bouyer 
     84  1.13.2.1   bouyer 	if ((mii->mii_flags & MIIF_INITDONE) == 0) {
     85  1.13.2.1   bouyer 		LIST_INIT(&mii->mii_phys);
     86  1.13.2.1   bouyer 		mii->mii_flags |= MIIF_INITDONE;
     87  1.13.2.1   bouyer 	}
     88  1.13.2.1   bouyer 
     89  1.13.2.1   bouyer 	for (ma.mii_phyno = phymin; ma.mii_phyno <= phymax; ma.mii_phyno++) {
     90      1.13  thorpej 		/*
     91      1.13  thorpej 		 * Make sure we haven't already configured a PHY at this
     92  1.13.2.1   bouyer 		 * address.  This allows mii_attach() to be called
     93      1.13  thorpej 		 * multiple times.
     94      1.13  thorpej 		 */
     95      1.13  thorpej 		for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
     96      1.13  thorpej 		     child = LIST_NEXT(child, mii_list)) {
     97      1.13  thorpej 			if (child->mii_phy == ma.mii_phyno) {
     98      1.13  thorpej 				/*
     99      1.13  thorpej 				 * Yes, there is already something
    100      1.13  thorpej 				 * configured at this address.
    101      1.13  thorpej 				 */
    102      1.13  thorpej 				offset++;
    103  1.13.2.1   bouyer 				continue;
    104      1.13  thorpej 			}
    105      1.13  thorpej 		}
    106      1.13  thorpej 
    107       1.6  thorpej 		/*
    108      1.11  thorpej 		 * Check to see if there is a PHY at this address.  Note,
    109      1.11  thorpej 		 * many braindead PHYs report 0/0 in their ID registers,
    110      1.11  thorpej 		 * so we test for media in the BMSR.
    111      1.11  thorpej 		 */
    112      1.11  thorpej 		bmsr = (*mii->mii_readreg)(parent, ma.mii_phyno, MII_BMSR);
    113      1.11  thorpej 		if (bmsr == 0 || bmsr == 0xffff ||
    114      1.11  thorpej 		    (bmsr & BMSR_MEDIAMASK) == 0) {
    115      1.11  thorpej 			/* Assume no PHY at this address. */
    116      1.11  thorpej 			continue;
    117      1.11  thorpej 		}
    118      1.11  thorpej 
    119      1.11  thorpej 		/*
    120  1.13.2.1   bouyer 		 * There is a PHY at this address.  If we were given an
    121  1.13.2.1   bouyer 		 * `offset' locator, skip this PHY if it doesn't match.
    122  1.13.2.1   bouyer 		 */
    123  1.13.2.1   bouyer 		if (offloc != MII_OFFSET_ANY && offloc != offset) {
    124  1.13.2.1   bouyer 			offset++;
    125  1.13.2.1   bouyer 			continue;
    126  1.13.2.1   bouyer 		}
    127  1.13.2.1   bouyer 
    128  1.13.2.1   bouyer 		/*
    129      1.11  thorpej 		 * Extract the IDs.  Braindead PHYs will be handled by
    130      1.11  thorpej 		 * the `ukphy' driver, as we have no ID information to
    131      1.11  thorpej 		 * match on.
    132       1.6  thorpej 		 */
    133       1.6  thorpej 		ma.mii_id1 = (*mii->mii_readreg)(parent, ma.mii_phyno,
    134       1.6  thorpej 		    MII_PHYIDR1);
    135       1.6  thorpej 		ma.mii_id2 = (*mii->mii_readreg)(parent, ma.mii_phyno,
    136       1.6  thorpej 		    MII_PHYIDR2);
    137       1.1   bouyer 
    138       1.6  thorpej 		ma.mii_data = mii;
    139       1.6  thorpej 		ma.mii_capmask = capmask;
    140  1.13.2.1   bouyer 		ma.mii_flags = flags;
    141       1.1   bouyer 
    142       1.6  thorpej 		if ((child = (struct mii_softc *)config_found_sm(parent, &ma,
    143       1.6  thorpej 		    mii_print, mii_submatch)) != NULL) {
    144       1.6  thorpej 			/*
    145       1.6  thorpej 			 * Link it up in the parent's MII data.
    146       1.6  thorpej 			 */
    147  1.13.2.1   bouyer 			callout_init(&child->mii_nway_ch);
    148       1.6  thorpej 			LIST_INSERT_HEAD(&mii->mii_phys, child, mii_list);
    149      1.13  thorpej 			child->mii_offset = offset;
    150       1.6  thorpej 			mii->mii_instance++;
    151       1.6  thorpej 		}
    152      1.13  thorpej 		offset++;
    153       1.6  thorpej 	}
    154       1.4   bouyer }
    155       1.1   bouyer 
    156  1.13.2.1   bouyer void
    157  1.13.2.1   bouyer mii_activate(mii, act, phyloc, offloc)
    158  1.13.2.1   bouyer 	struct mii_data *mii;
    159  1.13.2.1   bouyer 	enum devact act;
    160  1.13.2.1   bouyer 	int phyloc, offloc;
    161  1.13.2.1   bouyer {
    162  1.13.2.1   bouyer 	struct mii_softc *child;
    163  1.13.2.1   bouyer 
    164  1.13.2.1   bouyer 	if (phyloc != MII_PHY_ANY && offloc != MII_PHY_ANY)
    165  1.13.2.1   bouyer 		panic("mii_activate: phyloc and offloc specified");
    166  1.13.2.1   bouyer 
    167  1.13.2.1   bouyer 	if ((mii->mii_flags & MIIF_INITDONE) == 0)
    168  1.13.2.1   bouyer 		return;
    169  1.13.2.1   bouyer 
    170  1.13.2.1   bouyer 	for (child = LIST_FIRST(&mii->mii_phys);
    171  1.13.2.1   bouyer 	     child != NULL; child = LIST_NEXT(child, mii_list)) {
    172  1.13.2.1   bouyer 		if (phyloc != MII_PHY_ANY || offloc != MII_OFFSET_ANY) {
    173  1.13.2.1   bouyer 			if (phyloc != MII_PHY_ANY &&
    174  1.13.2.1   bouyer 			    phyloc != child->mii_phy)
    175  1.13.2.1   bouyer 				continue;
    176  1.13.2.1   bouyer 			if (offloc != MII_OFFSET_ANY &&
    177  1.13.2.1   bouyer 			    offloc != child->mii_offset)
    178  1.13.2.1   bouyer 				continue;
    179  1.13.2.1   bouyer 		}
    180  1.13.2.1   bouyer 		switch (act) {
    181  1.13.2.1   bouyer 		case DVACT_ACTIVATE:
    182  1.13.2.1   bouyer 			panic("mii_activate: DVACT_ACTIVATE");
    183  1.13.2.1   bouyer 			break;
    184  1.13.2.1   bouyer 
    185  1.13.2.1   bouyer 		case DVACT_DEACTIVATE:
    186  1.13.2.1   bouyer 			if (config_deactivate(&child->mii_dev) != 0)
    187  1.13.2.1   bouyer 				panic("%s: config_activate(%d) failed\n",
    188  1.13.2.1   bouyer 				    child->mii_dev.dv_xname, act);
    189  1.13.2.1   bouyer 		}
    190  1.13.2.1   bouyer 	}
    191  1.13.2.1   bouyer }
    192  1.13.2.1   bouyer 
    193  1.13.2.1   bouyer void
    194  1.13.2.1   bouyer mii_detach(mii, phyloc, offloc)
    195  1.13.2.1   bouyer 	struct mii_data *mii;
    196  1.13.2.1   bouyer 	int phyloc, offloc;
    197  1.13.2.1   bouyer {
    198  1.13.2.1   bouyer 	struct mii_softc *child, *nchild;
    199  1.13.2.1   bouyer 
    200  1.13.2.1   bouyer 	if (phyloc != MII_PHY_ANY && offloc != MII_PHY_ANY)
    201  1.13.2.1   bouyer 		panic("mii_detach: phyloc and offloc specified");
    202  1.13.2.1   bouyer 
    203  1.13.2.1   bouyer 	if ((mii->mii_flags & MIIF_INITDONE) == 0)
    204  1.13.2.1   bouyer 		return;
    205  1.13.2.1   bouyer 
    206  1.13.2.1   bouyer 	for (child = LIST_FIRST(&mii->mii_phys);
    207  1.13.2.1   bouyer 	     child != NULL; child = nchild) {
    208  1.13.2.1   bouyer 		nchild = LIST_NEXT(child, mii_list);
    209  1.13.2.1   bouyer 		if (phyloc != MII_PHY_ANY || offloc != MII_OFFSET_ANY) {
    210  1.13.2.1   bouyer 			if (phyloc != MII_PHY_ANY &&
    211  1.13.2.1   bouyer 			    phyloc != child->mii_phy)
    212  1.13.2.1   bouyer 				continue;
    213  1.13.2.1   bouyer 			if (offloc != MII_OFFSET_ANY &&
    214  1.13.2.1   bouyer 			    offloc != child->mii_offset)
    215  1.13.2.1   bouyer 				continue;
    216  1.13.2.1   bouyer 		}
    217  1.13.2.1   bouyer 		LIST_REMOVE(child, mii_list);
    218  1.13.2.1   bouyer 		(void) config_detach(&child->mii_dev, DETACH_FORCE);
    219  1.13.2.1   bouyer 	}
    220  1.13.2.1   bouyer }
    221  1.13.2.1   bouyer 
    222       1.2  thorpej int
    223       1.2  thorpej mii_print(aux, pnp)
    224       1.1   bouyer 	void *aux;
    225       1.1   bouyer 	const char *pnp;
    226       1.1   bouyer {
    227       1.6  thorpej 	struct mii_attach_args *ma = aux;
    228       1.2  thorpej 
    229       1.6  thorpej 	if (pnp != NULL)
    230       1.8  thorpej 		printf("OUI 0x%06x model 0x%04x rev %d at %s",
    231       1.6  thorpej 		    MII_OUI(ma->mii_id1, ma->mii_id2), MII_MODEL(ma->mii_id2),
    232       1.6  thorpej 		    MII_REV(ma->mii_id2), pnp);
    233       1.6  thorpej 
    234       1.6  thorpej 	printf(" phy %d", ma->mii_phyno);
    235       1.1   bouyer 	return (UNCONF);
    236       1.1   bouyer }
    237       1.1   bouyer 
    238       1.1   bouyer int
    239       1.6  thorpej mii_submatch(parent, cf, aux)
    240       1.1   bouyer 	struct device *parent;
    241       1.1   bouyer 	struct cfdata *cf;
    242       1.1   bouyer 	void *aux;
    243       1.1   bouyer {
    244       1.6  thorpej 	struct mii_attach_args *ma = aux;
    245       1.1   bouyer 
    246       1.7  thorpej 	if (ma->mii_phyno != cf->cf_loc[MIICF_PHY] &&
    247       1.7  thorpej 	    cf->cf_loc[MIICF_PHY] != MIICF_PHY_DEFAULT)
    248       1.6  thorpej 		return (0);
    249       1.1   bouyer 
    250       1.1   bouyer 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    251       1.1   bouyer }
    252       1.1   bouyer 
    253       1.6  thorpej /*
    254       1.6  thorpej  * Media changed; notify all PHYs.
    255       1.6  thorpej  */
    256       1.6  thorpej int
    257       1.6  thorpej mii_mediachg(mii)
    258       1.6  thorpej 	struct mii_data *mii;
    259       1.1   bouyer {
    260       1.6  thorpej 	struct mii_softc *child;
    261       1.6  thorpej 	int rv;
    262       1.1   bouyer 
    263       1.6  thorpej 	mii->mii_media_status = 0;
    264       1.6  thorpej 	mii->mii_media_active = IFM_NONE;
    265       1.6  thorpej 
    266       1.6  thorpej 	for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
    267       1.6  thorpej 	     child = LIST_NEXT(child, mii_list)) {
    268  1.13.2.1   bouyer 		rv = PHY_SERVICE(child, mii, MII_MEDIACHG);
    269       1.6  thorpej 		if (rv)
    270       1.6  thorpej 			return (rv);
    271       1.1   bouyer 	}
    272       1.6  thorpej 	return (0);
    273       1.1   bouyer }
    274       1.1   bouyer 
    275       1.6  thorpej /*
    276       1.6  thorpej  * Call the PHY tick routines, used during autonegotiation.
    277       1.6  thorpej  */
    278       1.6  thorpej void
    279       1.6  thorpej mii_tick(mii)
    280       1.6  thorpej 	struct mii_data *mii;
    281       1.1   bouyer {
    282       1.6  thorpej 	struct mii_softc *child;
    283       1.1   bouyer 
    284       1.6  thorpej 	for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
    285       1.6  thorpej 	     child = LIST_NEXT(child, mii_list))
    286  1.13.2.1   bouyer 		(void) PHY_SERVICE(child, mii, MII_TICK);
    287       1.1   bouyer }
    288       1.1   bouyer 
    289       1.6  thorpej /*
    290       1.6  thorpej  * Get media status from PHYs.
    291       1.6  thorpej  */
    292       1.2  thorpej void
    293       1.6  thorpej mii_pollstat(mii)
    294       1.6  thorpej 	struct mii_data *mii;
    295       1.1   bouyer {
    296       1.6  thorpej 	struct mii_softc *child;
    297       1.1   bouyer 
    298       1.6  thorpej 	mii->mii_media_status = 0;
    299       1.6  thorpej 	mii->mii_media_active = IFM_NONE;
    300       1.1   bouyer 
    301       1.6  thorpej 	for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
    302       1.6  thorpej 	     child = LIST_NEXT(child, mii_list))
    303  1.13.2.1   bouyer 		(void) PHY_SERVICE(child, mii, MII_POLLSTAT);
    304  1.13.2.1   bouyer }
    305  1.13.2.1   bouyer 
    306  1.13.2.1   bouyer /*
    307  1.13.2.1   bouyer  * Inform the PHYs that the interface is down.
    308  1.13.2.1   bouyer  */
    309  1.13.2.1   bouyer void
    310  1.13.2.1   bouyer mii_down(mii)
    311  1.13.2.1   bouyer 	struct mii_data *mii;
    312  1.13.2.1   bouyer {
    313  1.13.2.1   bouyer 	struct mii_softc *child;
    314  1.13.2.1   bouyer 
    315  1.13.2.1   bouyer 	for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
    316  1.13.2.1   bouyer 	     child = LIST_NEXT(child, mii_list))
    317  1.13.2.1   bouyer 		(void) PHY_SERVICE(child, mii, MII_DOWN);
    318       1.1   bouyer }
    319