The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/mii/mii.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: mii.c,v 1.12 1999/08/03 19:41:49 drochner Exp $        */
    2 
    3 /*-
    4  * Copyright (c) 1998 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  *
   20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   30  * POSSIBILITY OF SUCH DAMAGE.
   31  */
   32 
   33 #include <sys/cdefs.h>
   34 __FBSDID("$FreeBSD: releng/8.2/sys/dev/mii/mii.c 215886 2010-11-26 20:57:45Z marius $");
   35 
   36 /*
   37  * MII bus layer, glues MII-capable network interface drivers to sharable
   38  * PHY drivers.  This exports an interface compatible with BSD/OS 3.0's,
   39  * plus some NetBSD extensions.
   40  */
   41 
   42 #include <sys/param.h>
   43 #include <sys/systm.h>
   44 #include <sys/socket.h>
   45 #include <sys/malloc.h>
   46 #include <sys/module.h>
   47 #include <sys/bus.h> 
   48 
   49 #include <net/if.h>
   50 #include <net/if_media.h>
   51 #include <net/route.h>
   52 
   53 #include <dev/mii/mii.h>
   54 #include <dev/mii/miivar.h>
   55 
   56 MODULE_VERSION(miibus, 1);
   57 
   58 #include "miibus_if.h"
   59 
   60 static int miibus_print_child(device_t dev, device_t child);
   61 static int miibus_read_ivar(device_t dev, device_t child, int which,
   62     uintptr_t *result);
   63 static int miibus_child_location_str(device_t bus, device_t child, char *buf,
   64     size_t buflen);
   65 static int miibus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
   66     size_t buflen);
   67 static int miibus_readreg(device_t, int, int);
   68 static int miibus_writereg(device_t, int, int, int);
   69 static void miibus_statchg(device_t);
   70 static void miibus_linkchg(device_t);
   71 static void miibus_mediainit(device_t);
   72 
   73 static device_method_t miibus_methods[] = {
   74         /* device interface */
   75         DEVMETHOD(device_probe,         miibus_probe),
   76         DEVMETHOD(device_attach,        miibus_attach),
   77         DEVMETHOD(device_detach,        miibus_detach),
   78         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
   79 
   80         /* bus interface */
   81         DEVMETHOD(bus_print_child,      miibus_print_child),
   82         DEVMETHOD(bus_read_ivar,        miibus_read_ivar),
   83         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
   84         DEVMETHOD(bus_child_pnpinfo_str, miibus_child_pnpinfo_str),
   85         DEVMETHOD(bus_child_location_str, miibus_child_location_str),
   86 
   87         /* MII interface */
   88         DEVMETHOD(miibus_readreg,       miibus_readreg),
   89         DEVMETHOD(miibus_writereg,      miibus_writereg),
   90         DEVMETHOD(miibus_statchg,       miibus_statchg),    
   91         DEVMETHOD(miibus_linkchg,       miibus_linkchg),    
   92         DEVMETHOD(miibus_mediainit,     miibus_mediainit),    
   93 
   94         { 0, 0 }
   95 };
   96 
   97 devclass_t miibus_devclass;
   98 
   99 driver_t miibus_driver = {
  100         "miibus",
  101         miibus_methods,
  102         sizeof(struct mii_data)
  103 };
  104 
  105 struct miibus_ivars {
  106         struct ifnet    *ifp;
  107         ifm_change_cb_t ifmedia_upd;
  108         ifm_stat_cb_t   ifmedia_sts;
  109         int             mii_flags;
  110 };
  111 
  112 int
  113 miibus_probe(device_t dev)
  114 {
  115 
  116         device_set_desc(dev, "MII bus");
  117 
  118         return (BUS_PROBE_SPECIFIC);
  119 }
  120 
  121 int
  122 miibus_attach(device_t dev)
  123 {
  124         struct miibus_ivars     *ivars;
  125         struct mii_attach_args  *ma;
  126         struct mii_data         *mii;
  127         device_t                *children;
  128         int                     i, nchildren;
  129 
  130         mii = device_get_softc(dev);
  131         nchildren = 0;
  132         if (device_get_children(dev, &children, &nchildren) == 0) {
  133                 for (i = 0; i < nchildren; i++) {
  134                         ma = device_get_ivars(children[i]);
  135                         ma->mii_data = mii;
  136                 }
  137                 free(children, M_TEMP);
  138         }
  139         if (nchildren == 0) {
  140                 device_printf(dev, "cannot get children\n");
  141                 return (ENXIO);
  142         }
  143         ivars = device_get_ivars(dev);
  144         ifmedia_init(&mii->mii_media, IFM_IMASK, ivars->ifmedia_upd,
  145             ivars->ifmedia_sts);
  146         mii->mii_ifp = ivars->ifp;
  147         mii->mii_ifp->if_capabilities |= IFCAP_LINKSTATE;
  148         mii->mii_ifp->if_capenable |= IFCAP_LINKSTATE;
  149         LIST_INIT(&mii->mii_phys);
  150 
  151         return (bus_generic_attach(dev));
  152 }
  153 
  154 int
  155 miibus_detach(device_t dev)
  156 {
  157         struct mii_data         *mii;
  158 
  159         bus_generic_detach(dev);
  160         mii = device_get_softc(dev);
  161         ifmedia_removeall(&mii->mii_media);
  162         mii->mii_ifp = NULL;
  163 
  164         return (0);
  165 }
  166 
  167 static int
  168 miibus_print_child(device_t dev, device_t child)
  169 {
  170         struct mii_attach_args *ma;
  171         int retval;
  172 
  173         ma = device_get_ivars(child);
  174         retval = bus_print_child_header(dev, child);
  175         retval += printf(" PHY %d", ma->mii_phyno);
  176         retval += bus_print_child_footer(dev, child);
  177 
  178         return (retval);
  179 }
  180 
  181 static int
  182 miibus_read_ivar(device_t dev, device_t child __unused, int which,
  183     uintptr_t *result)
  184 {
  185         struct miibus_ivars *ivars;
  186 
  187         /*
  188          * NB: this uses the instance variables of the miibus rather than
  189          * its PHY children.
  190          */
  191         ivars = device_get_ivars(dev);
  192         switch (which) {
  193         case MIIBUS_IVAR_FLAGS:
  194                 *result = ivars->mii_flags;
  195                 break;
  196         default:
  197                 return (ENOENT);
  198         }
  199         return (0);
  200 }
  201 
  202 static int
  203 miibus_child_pnpinfo_str(device_t bus __unused, device_t child, char *buf,
  204     size_t buflen)
  205 {
  206         struct mii_attach_args *ma;
  207 
  208         ma = device_get_ivars(child);
  209         snprintf(buf, buflen, "oui=0x%x model=0x%x rev=0x%x",
  210             MII_OUI(ma->mii_id1, ma->mii_id2),
  211             MII_MODEL(ma->mii_id2), MII_REV(ma->mii_id2));
  212         return (0);
  213 }
  214 
  215 static int
  216 miibus_child_location_str(device_t bus __unused, device_t child, char *buf,
  217     size_t buflen)
  218 {
  219         struct mii_attach_args *ma;
  220 
  221         ma = device_get_ivars(child);
  222         snprintf(buf, buflen, "phyno=%d", ma->mii_phyno);
  223         return (0);
  224 }
  225 
  226 static int
  227 miibus_readreg(device_t dev, int phy, int reg)
  228 {
  229         device_t                parent;
  230 
  231         parent = device_get_parent(dev);
  232         return (MIIBUS_READREG(parent, phy, reg));
  233 }
  234 
  235 static int
  236 miibus_writereg(device_t dev, int phy, int reg, int data)
  237 {
  238         device_t                parent;
  239 
  240         parent = device_get_parent(dev);
  241         return (MIIBUS_WRITEREG(parent, phy, reg, data));
  242 }
  243 
  244 static void
  245 miibus_statchg(device_t dev)
  246 {
  247         device_t                parent;
  248         struct mii_data         *mii;
  249         struct ifnet            *ifp;
  250 
  251         parent = device_get_parent(dev);
  252         MIIBUS_STATCHG(parent);
  253 
  254         mii = device_get_softc(dev);
  255 
  256         /*
  257          * Note that each NIC's softc must start with an ifnet pointer.
  258          * XXX: EVIL HACK!
  259          */
  260         ifp = *(struct ifnet **)device_get_softc(parent);
  261         ifp->if_baudrate = ifmedia_baudrate(mii->mii_media_active);
  262 }
  263 
  264 static void
  265 miibus_linkchg(device_t dev)
  266 {
  267         struct mii_data         *mii;
  268         device_t                parent;
  269         int                     link_state;
  270 
  271         parent = device_get_parent(dev);
  272         MIIBUS_LINKCHG(parent);
  273 
  274         mii = device_get_softc(dev);
  275         
  276         if (mii->mii_media_status & IFM_AVALID) {
  277                 if (mii->mii_media_status & IFM_ACTIVE)
  278                         link_state = LINK_STATE_UP;
  279                 else
  280                         link_state = LINK_STATE_DOWN;
  281         } else
  282                 link_state = LINK_STATE_UNKNOWN;
  283         /*
  284          * Note that each NIC's softc must start with an ifnet pointer.
  285          * XXX: EVIL HACK!
  286          */
  287         if_link_state_change(*(struct ifnet**)device_get_softc(parent), link_state);
  288 }
  289 
  290 static void
  291 miibus_mediainit(device_t dev)
  292 {
  293         struct mii_data         *mii;
  294         struct ifmedia_entry    *m;
  295         int                     media = 0;
  296 
  297         /* Poke the parent in case it has any media of its own to add. */
  298         MIIBUS_MEDIAINIT(device_get_parent(dev));
  299 
  300         mii = device_get_softc(dev);
  301         LIST_FOREACH(m, &mii->mii_media.ifm_list, ifm_list) {
  302                 media = m->ifm_media;
  303                 if (media == (IFM_ETHER | IFM_AUTO))
  304                         break;
  305         }
  306 
  307         ifmedia_set(&mii->mii_media, media);
  308 }
  309 
  310 /*
  311  * Helper function used by network interface drivers, attaches the miibus and
  312  * the PHYs to the network interface driver parent.
  313  */
  314 int
  315 mii_attach(device_t dev, device_t *miibus, struct ifnet *ifp,
  316     ifm_change_cb_t ifmedia_upd, ifm_stat_cb_t ifmedia_sts, int capmask,
  317     int phyloc, int offloc, int flags)
  318 {
  319         struct miibus_ivars *ivars;
  320         struct mii_attach_args ma, *args;
  321         device_t *children, phy;
  322         int bmsr, first, i, nchildren, offset, phymax, phymin, rv;
  323 
  324         if (phyloc != MII_PHY_ANY && offloc != MII_OFFSET_ANY) {
  325                 printf("%s: phyloc and offloc specified\n", __func__);
  326                 return (EINVAL);
  327         }
  328 
  329         if (offloc != MII_OFFSET_ANY && (offloc < 0 || offloc >= MII_NPHY)) {
  330                 printf("%s: ivalid offloc %d\n", __func__, offloc);
  331                 return (EINVAL);
  332         }
  333 
  334         if (phyloc == MII_PHY_ANY) {
  335                 phymin = 0;
  336                 phymax = MII_NPHY - 1;
  337         } else {
  338                 if (phyloc < 0 || phyloc >= MII_NPHY) {
  339                         printf("%s: ivalid phyloc %d\n", __func__, phyloc);
  340                         return (EINVAL);
  341                 }
  342                 phymin = phymax = phyloc;
  343         }
  344 
  345         first = 0;
  346         if (*miibus == NULL) {
  347                 first = 1;
  348                 ivars = malloc(sizeof(*ivars), M_DEVBUF, M_NOWAIT);
  349                 if (ivars == NULL)
  350                         return (ENOMEM);
  351                 ivars->ifp = ifp;
  352                 ivars->ifmedia_upd = ifmedia_upd;
  353                 ivars->ifmedia_sts = ifmedia_sts;
  354                 ivars->mii_flags = flags;
  355                 *miibus = device_add_child(dev, "miibus", -1);
  356                 if (*miibus == NULL) {
  357                         rv = ENXIO;
  358                         goto fail;
  359                 }
  360                 device_set_ivars(*miibus, ivars);
  361         } else {
  362                 ivars = device_get_ivars(*miibus);
  363                 if (ivars->ifp != ifp || ivars->ifmedia_upd != ifmedia_upd ||
  364                     ivars->ifmedia_sts != ifmedia_sts ||
  365                     ivars->mii_flags != flags) {
  366                         printf("%s: non-matching invariant\n", __func__);
  367                         return (EINVAL);
  368                 }
  369                 /*
  370                  * Assignment of the attach arguments mii_data for the first
  371                  * pass is done in miibus_attach(), i.e. once the miibus softc
  372                  * has been allocated.
  373                  */
  374                 ma.mii_data = device_get_softc(*miibus);
  375         } 
  376 
  377         ma.mii_capmask = capmask;
  378 
  379         phy = NULL;
  380         offset = 0;
  381         for (ma.mii_phyno = phymin; ma.mii_phyno <= phymax; ma.mii_phyno++) {
  382                 /*
  383                  * Make sure we haven't already configured a PHY at this
  384                  * address.  This allows mii_attach() to be called
  385                  * multiple times.
  386                  */
  387                 if (device_get_children(*miibus, &children, &nchildren) == 0) {
  388                         for (i = 0; i < nchildren; i++) {
  389                                 args = device_get_ivars(children[i]);
  390                                 if (args->mii_phyno == ma.mii_phyno) {
  391                                         /*
  392                                          * Yes, there is already something
  393                                          * configured at this address.
  394                                          */
  395                                         free(children, M_TEMP);
  396                                         goto skip;
  397                                 }
  398                         }
  399                         free(children, M_TEMP);
  400                 }
  401 
  402                 /*
  403                  * Check to see if there is a PHY at this address.  Note,
  404                  * many braindead PHYs report 0/0 in their ID registers,
  405                  * so we test for media in the BMSR.
  406                  */
  407                 bmsr = MIIBUS_READREG(dev, ma.mii_phyno, MII_BMSR);
  408                 if (bmsr == 0 || bmsr == 0xffff ||
  409                     (bmsr & (BMSR_EXTSTAT | BMSR_MEDIAMASK)) == 0) {
  410                         /* Assume no PHY at this address. */
  411                         continue;
  412                 }
  413 
  414                 /*
  415                  * There is a PHY at this address.  If we were given an
  416                  * `offset' locator, skip this PHY if it doesn't match.
  417                  */
  418                 if (offloc != MII_OFFSET_ANY && offloc != offset)
  419                         goto skip;
  420 
  421                 /*
  422                  * Extract the IDs. Braindead PHYs will be handled by
  423                  * the `ukphy' driver, as we have no ID information to
  424                  * match on.
  425                  */
  426                 ma.mii_id1 = MIIBUS_READREG(dev, ma.mii_phyno, MII_PHYIDR1);
  427                 ma.mii_id2 = MIIBUS_READREG(dev, ma.mii_phyno, MII_PHYIDR2);
  428 
  429                 args = malloc(sizeof(struct mii_attach_args), M_DEVBUF,
  430                     M_NOWAIT);
  431                 if (args == NULL)
  432                         goto skip;
  433                 bcopy((char *)&ma, (char *)args, sizeof(ma));
  434                 phy = device_add_child(*miibus, NULL, -1);
  435                 if (phy == NULL) {
  436                         free(args, M_DEVBUF);
  437                         goto skip;
  438                 }
  439                 device_set_ivars(phy, args);
  440  skip:
  441                 offset++;
  442         }
  443 
  444         if (first != 0) {
  445                 if (phy == NULL) {
  446                         rv = ENXIO;
  447                         goto fail;
  448                 }
  449                 rv = bus_generic_attach(dev);
  450                 if (rv != 0)
  451                         goto fail;
  452 
  453                 /* Attaching of the PHY drivers is done in miibus_attach(). */
  454                 return (0);
  455         }
  456         rv = bus_generic_attach(*miibus);
  457         if (rv != 0)
  458                 goto fail;
  459 
  460         return (0);
  461 
  462  fail:
  463         if (*miibus != NULL)
  464                 device_delete_child(dev, *miibus);
  465         free(ivars, M_DEVBUF);
  466         if (first != 0)
  467                 *miibus = NULL;
  468         return (rv);
  469 }
  470 
  471 int
  472 mii_phy_probe(device_t dev, device_t *child, ifm_change_cb_t ifmedia_upd,
  473     ifm_stat_cb_t ifmedia_sts)
  474 {
  475         struct ifnet *ifp;
  476 
  477         /*
  478          * Note that each NIC's softc must start with an ifnet pointer.
  479          * XXX: EVIL HACK!
  480          */
  481         ifp = *(struct ifnet **)device_get_softc(dev);
  482         return (mii_attach(dev, child, ifp, ifmedia_upd, ifmedia_sts,
  483             BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0));
  484 }
  485 
  486 /*
  487  * Media changed; notify all PHYs.
  488  */
  489 int
  490 mii_mediachg(struct mii_data *mii)
  491 {
  492         struct mii_softc *child;
  493         struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
  494         int rv;
  495 
  496         mii->mii_media_status = 0;
  497         mii->mii_media_active = IFM_NONE;
  498 
  499         LIST_FOREACH(child, &mii->mii_phys, mii_list) {
  500                 /*
  501                  * If the media indicates a different PHY instance,
  502                  * isolate this one.
  503                  */
  504                 if (IFM_INST(ife->ifm_media) != child->mii_inst) {
  505                         if ((child->mii_flags & MIIF_NOISOLATE) != 0) {
  506                                 device_printf(child->mii_dev, "%s: "
  507                                     "can't handle non-zero PHY instance %d\n",
  508                                     __func__, child->mii_inst);
  509                                 continue;
  510                         }
  511                         PHY_WRITE(child, MII_BMCR, PHY_READ(child, MII_BMCR) |
  512                             BMCR_ISO);
  513                         continue;
  514                 }
  515                 rv = (*child->mii_service)(child, mii, MII_MEDIACHG);
  516                 if (rv)
  517                         return (rv);
  518         }
  519         return (0);
  520 }
  521 
  522 /*
  523  * Call the PHY tick routines, used during autonegotiation.
  524  */
  525 void
  526 mii_tick(struct mii_data *mii)
  527 {
  528         struct mii_softc *child;
  529         struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
  530 
  531         LIST_FOREACH(child, &mii->mii_phys, mii_list) {
  532                 /*
  533                  * If this PHY instance isn't currently selected, just skip
  534                  * it.
  535                  */
  536                 if (IFM_INST(ife->ifm_media) != child->mii_inst)
  537                         continue;
  538                 (void)(*child->mii_service)(child, mii, MII_TICK);
  539         }
  540 }
  541 
  542 /*
  543  * Get media status from PHYs.
  544  */
  545 void
  546 mii_pollstat(struct mii_data *mii)
  547 {
  548         struct mii_softc *child;
  549         struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
  550 
  551         mii->mii_media_status = 0;
  552         mii->mii_media_active = IFM_NONE;
  553 
  554         LIST_FOREACH(child, &mii->mii_phys, mii_list) {
  555                 /*
  556                  * If we're not polling this PHY instance, just skip it.
  557                  */
  558                 if (IFM_INST(ife->ifm_media) != child->mii_inst)
  559                         continue;
  560                 (void)(*child->mii_service)(child, mii, MII_POLLSTAT);
  561         }
  562 }
  563 
  564 /*
  565  * Inform the PHYs that the interface is down.
  566  */
  567 void
  568 mii_down(struct mii_data *mii)
  569 {
  570         struct mii_softc *child;
  571 
  572         LIST_FOREACH(child, &mii->mii_phys, mii_list)
  573                 mii_phy_down(child);
  574 }

Cache object: 3e174c2e47da629b392bfdb1b91d957a


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.