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