Home | History | Annotate | Line # | Download | only in mii
mii.c revision 1.13
      1  1.13  thorpej /*	$NetBSD: mii.c,v 1.13 1999/09/25 00:10:13 thorpej Exp $	*/
      2   1.6  thorpej 
      3   1.6  thorpej /*-
      4   1.6  thorpej  * Copyright (c) 1998 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.6  thorpej mii_phy_probe(parent, mii, capmask)
     66   1.6  thorpej 	struct device *parent;
     67   1.6  thorpej 	struct mii_data *mii;
     68   1.6  thorpej 	int capmask;
     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.1   bouyer 
     74   1.6  thorpej 	LIST_INIT(&mii->mii_phys);
     75   1.1   bouyer 
     76   1.6  thorpej 	for (ma.mii_phyno = 0; ma.mii_phyno < MII_NPHY; ma.mii_phyno++) {
     77  1.13  thorpej #if 0 /* XXX not yet --thorpej */
     78  1.13  thorpej 		/*
     79  1.13  thorpej 		 * Make sure we haven't already configured a PHY at this
     80  1.13  thorpej 		 * address.  This allows mii_phy_probe() to be called
     81  1.13  thorpej 		 * multiple times.
     82  1.13  thorpej 		 */
     83  1.13  thorpej 		for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
     84  1.13  thorpej 		     child = LIST_NEXT(child, mii_list)) {
     85  1.13  thorpej 			if (child->mii_phy == ma.mii_phyno) {
     86  1.13  thorpej 				/*
     87  1.13  thorpej 				 * Yes, there is already something
     88  1.13  thorpej 				 * configured at this address.
     89  1.13  thorpej 				 */
     90  1.13  thorpej 				continue;
     91  1.13  thorpej 				offset++;
     92  1.13  thorpej 			}
     93  1.13  thorpej 		}
     94  1.13  thorpej #endif
     95  1.13  thorpej 
     96   1.6  thorpej 		/*
     97  1.11  thorpej 		 * Check to see if there is a PHY at this address.  Note,
     98  1.11  thorpej 		 * many braindead PHYs report 0/0 in their ID registers,
     99  1.11  thorpej 		 * so we test for media in the BMSR.
    100  1.11  thorpej 		 */
    101  1.11  thorpej 		bmsr = (*mii->mii_readreg)(parent, ma.mii_phyno, MII_BMSR);
    102  1.11  thorpej 		if (bmsr == 0 || bmsr == 0xffff ||
    103  1.11  thorpej 		    (bmsr & BMSR_MEDIAMASK) == 0) {
    104  1.11  thorpej 			/* Assume no PHY at this address. */
    105  1.11  thorpej 			continue;
    106  1.11  thorpej 		}
    107  1.11  thorpej 
    108  1.11  thorpej 		/*
    109  1.11  thorpej 		 * Extract the IDs.  Braindead PHYs will be handled by
    110  1.11  thorpej 		 * the `ukphy' driver, as we have no ID information to
    111  1.11  thorpej 		 * match on.
    112   1.6  thorpej 		 */
    113   1.6  thorpej 		ma.mii_id1 = (*mii->mii_readreg)(parent, ma.mii_phyno,
    114   1.6  thorpej 		    MII_PHYIDR1);
    115   1.6  thorpej 		ma.mii_id2 = (*mii->mii_readreg)(parent, ma.mii_phyno,
    116   1.6  thorpej 		    MII_PHYIDR2);
    117   1.1   bouyer 
    118   1.6  thorpej 		ma.mii_data = mii;
    119   1.6  thorpej 		ma.mii_capmask = capmask;
    120   1.1   bouyer 
    121   1.6  thorpej 		if ((child = (struct mii_softc *)config_found_sm(parent, &ma,
    122   1.6  thorpej 		    mii_print, mii_submatch)) != NULL) {
    123   1.6  thorpej 			/*
    124   1.6  thorpej 			 * Link it up in the parent's MII data.
    125   1.6  thorpej 			 */
    126   1.6  thorpej 			LIST_INSERT_HEAD(&mii->mii_phys, child, mii_list);
    127  1.13  thorpej 			child->mii_offset = offset;
    128   1.6  thorpej 			mii->mii_instance++;
    129   1.6  thorpej 		}
    130  1.13  thorpej 		offset++;
    131   1.6  thorpej 	}
    132   1.4   bouyer }
    133   1.1   bouyer 
    134   1.2  thorpej int
    135   1.2  thorpej mii_print(aux, pnp)
    136   1.1   bouyer 	void *aux;
    137   1.1   bouyer 	const char *pnp;
    138   1.1   bouyer {
    139   1.6  thorpej 	struct mii_attach_args *ma = aux;
    140   1.2  thorpej 
    141   1.6  thorpej 	if (pnp != NULL)
    142   1.8  thorpej 		printf("OUI 0x%06x model 0x%04x rev %d at %s",
    143   1.6  thorpej 		    MII_OUI(ma->mii_id1, ma->mii_id2), MII_MODEL(ma->mii_id2),
    144   1.6  thorpej 		    MII_REV(ma->mii_id2), pnp);
    145   1.6  thorpej 
    146   1.6  thorpej 	printf(" phy %d", ma->mii_phyno);
    147   1.1   bouyer 	return (UNCONF);
    148   1.1   bouyer }
    149   1.1   bouyer 
    150   1.1   bouyer int
    151   1.6  thorpej mii_submatch(parent, cf, aux)
    152   1.1   bouyer 	struct device *parent;
    153   1.1   bouyer 	struct cfdata *cf;
    154   1.1   bouyer 	void *aux;
    155   1.1   bouyer {
    156   1.6  thorpej 	struct mii_attach_args *ma = aux;
    157   1.1   bouyer 
    158   1.7  thorpej 	if (ma->mii_phyno != cf->cf_loc[MIICF_PHY] &&
    159   1.7  thorpej 	    cf->cf_loc[MIICF_PHY] != MIICF_PHY_DEFAULT)
    160   1.6  thorpej 		return (0);
    161   1.1   bouyer 
    162   1.1   bouyer 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    163   1.1   bouyer }
    164   1.1   bouyer 
    165   1.6  thorpej /*
    166   1.6  thorpej  * Media changed; notify all PHYs.
    167   1.6  thorpej  */
    168   1.6  thorpej int
    169   1.6  thorpej mii_mediachg(mii)
    170   1.6  thorpej 	struct mii_data *mii;
    171   1.1   bouyer {
    172   1.6  thorpej 	struct mii_softc *child;
    173   1.6  thorpej 	int rv;
    174   1.1   bouyer 
    175   1.6  thorpej 	mii->mii_media_status = 0;
    176   1.6  thorpej 	mii->mii_media_active = IFM_NONE;
    177   1.6  thorpej 
    178   1.6  thorpej 	for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
    179   1.6  thorpej 	     child = LIST_NEXT(child, mii_list)) {
    180   1.6  thorpej 		rv = (*child->mii_service)(child, mii, MII_MEDIACHG);
    181   1.6  thorpej 		if (rv)
    182   1.6  thorpej 			return (rv);
    183   1.1   bouyer 	}
    184   1.6  thorpej 	return (0);
    185   1.1   bouyer }
    186   1.1   bouyer 
    187   1.6  thorpej /*
    188   1.6  thorpej  * Call the PHY tick routines, used during autonegotiation.
    189   1.6  thorpej  */
    190   1.6  thorpej void
    191   1.6  thorpej mii_tick(mii)
    192   1.6  thorpej 	struct mii_data *mii;
    193   1.1   bouyer {
    194   1.6  thorpej 	struct mii_softc *child;
    195   1.1   bouyer 
    196   1.6  thorpej 	for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
    197   1.6  thorpej 	     child = LIST_NEXT(child, mii_list))
    198   1.6  thorpej 		(void) (*child->mii_service)(child, mii, MII_TICK);
    199   1.1   bouyer }
    200   1.1   bouyer 
    201   1.6  thorpej /*
    202   1.6  thorpej  * Get media status from PHYs.
    203   1.6  thorpej  */
    204   1.2  thorpej void
    205   1.6  thorpej mii_pollstat(mii)
    206   1.6  thorpej 	struct mii_data *mii;
    207   1.1   bouyer {
    208   1.6  thorpej 	struct mii_softc *child;
    209   1.1   bouyer 
    210   1.6  thorpej 	mii->mii_media_status = 0;
    211   1.6  thorpej 	mii->mii_media_active = IFM_NONE;
    212   1.1   bouyer 
    213   1.6  thorpej 	for (child = LIST_FIRST(&mii->mii_phys); child != NULL;
    214   1.6  thorpej 	     child = LIST_NEXT(child, mii_list))
    215   1.6  thorpej 		(void) (*child->mii_service)(child, mii, MII_POLLSTAT);
    216   1.1   bouyer }
    217