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