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/net/if_media.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: if_media.c,v 1.21.2.1 2005/01/07 15:26:53 jdc 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  * 3. All advertising materials mentioning features or use of this software
   20  *    must display the following acknowledgement:
   21  *      This product includes software developed by the NetBSD
   22  *      Foundation, Inc. and its contributors.
   23  * 4. Neither the name of The NetBSD Foundation nor the names of its
   24  *    contributors may be used to endorse or promote products derived
   25  *    from this software without specific prior written permission.
   26  *
   27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   37  * POSSIBILITY OF SUCH DAMAGE.
   38  */
   39 
   40 /*
   41  * Copyright (c) 1997
   42  *      Jonathan Stone and Jason R. Thorpe.  All rights reserved.
   43  *
   44  * This software is derived from information provided by Matt Thomas.
   45  *
   46  * Redistribution and use in source and binary forms, with or without
   47  * modification, are permitted provided that the following conditions
   48  * are met:
   49  * 1. Redistributions of source code must retain the above copyright
   50  *    notice, this list of conditions and the following disclaimer.
   51  * 2. Redistributions in binary form must reproduce the above copyright
   52  *    notice, this list of conditions and the following disclaimer in the
   53  *    documentation and/or other materials provided with the distribution.
   54  * 3. All advertising materials mentioning features or use of this software
   55  *    must display the following acknowledgement:
   56  *      This product includes software developed by Jonathan Stone
   57  *      and Jason R. Thorpe for the NetBSD Project.
   58  * 4. The names of the authors may not be used to endorse or promote products
   59  *    derived from this software without specific prior written permission.
   60  *
   61  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
   62  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   63  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   64  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   65  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   66  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   67  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
   68  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   69  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   70  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   71  * SUCH DAMAGE.
   72  */
   73 
   74 /*
   75  * BSD/OS-compatible network interface media selection.
   76  *
   77  * Where it is safe to do so, this code strays slightly from the BSD/OS
   78  * design.  Software which uses the API (device drivers, basically)
   79  * shouldn't notice any difference.
   80  *
   81  * Many thanks to Matt Thomas for providing the information necessary
   82  * to implement this interface.
   83  */
   84 
   85 #include <sys/cdefs.h>
   86 __KERNEL_RCSID(0, "$NetBSD: if_media.c,v 1.21.2.1 2005/01/07 15:26:53 jdc Exp $");
   87 
   88 #include <sys/param.h>
   89 #include <sys/systm.h>
   90 #include <sys/errno.h>
   91 #include <sys/ioctl.h>
   92 #include <sys/socket.h>
   93 #include <sys/malloc.h>
   94 
   95 #include <net/if.h>
   96 #include <net/if_media.h>
   97 #include <net/netisr.h>
   98 
   99 /*
  100  * Compile-time options:
  101  * IFMEDIA_DEBUG:
  102  *      turn on implementation-level debug printfs.
  103  *      Useful for debugging newly-ported  drivers.
  104  */
  105 
  106 #ifdef IFMEDIA_DEBUG
  107 int     ifmedia_debug = 0;
  108 static  void ifmedia_printword __P((int));
  109 #endif
  110 
  111 MALLOC_DEFINE(M_IFMEDIA, "ifmedia", "interface media state");
  112 
  113 /*
  114  * Initialize if_media struct for a specific interface instance.
  115  */
  116 void
  117 ifmedia_init(ifm, dontcare_mask, change_callback, status_callback)
  118         struct ifmedia *ifm;
  119         int dontcare_mask;
  120         ifm_change_cb_t change_callback;
  121         ifm_stat_cb_t status_callback;
  122 {
  123 
  124         TAILQ_INIT(&ifm->ifm_list);
  125         ifm->ifm_cur = NULL;
  126         ifm->ifm_media = 0;
  127         ifm->ifm_mask = dontcare_mask;          /* IF don't-care bits */
  128         ifm->ifm_change = change_callback;
  129         ifm->ifm_status = status_callback;
  130 }
  131 
  132 /*
  133  * Add a media configuration to the list of supported media
  134  * for a specific interface instance.
  135  */
  136 void
  137 ifmedia_add(ifm, mword, data, aux)
  138         struct ifmedia *ifm;
  139         int mword;
  140         int data;
  141         void *aux;
  142 {
  143         struct ifmedia_entry *entry;
  144 
  145 #ifdef IFMEDIA_DEBUG
  146         if (ifmedia_debug) {
  147                 if (ifm == NULL) {
  148                         printf("ifmedia_add: null ifm\n");
  149                         return;
  150                 }
  151                 printf("Adding entry for ");
  152                 ifmedia_printword(mword);
  153         }
  154 #endif
  155 
  156         entry = malloc(sizeof(*entry), M_IFMEDIA, M_NOWAIT);
  157         if (entry == NULL)
  158                 panic("ifmedia_add: can't malloc entry");
  159 
  160         entry->ifm_media = mword;
  161         entry->ifm_data = data;
  162         entry->ifm_aux = aux;
  163 
  164         TAILQ_INSERT_TAIL(&ifm->ifm_list, entry, ifm_list);
  165 }
  166 
  167 /*
  168  * Add an array of media configurations to the list of
  169  * supported media for a specific interface instance.
  170  */
  171 void
  172 ifmedia_list_add(ifm, lp, count)
  173         struct ifmedia *ifm;
  174         struct ifmedia_entry *lp;
  175         int count;
  176 {
  177         int i;
  178 
  179         for (i = 0; i < count; i++)
  180                 ifmedia_add(ifm, lp[i].ifm_media, lp[i].ifm_data,
  181                     lp[i].ifm_aux);
  182 }
  183 
  184 /*
  185  * Set the default active media. 
  186  *
  187  * Called by device-specific code which is assumed to have already
  188  * selected the default media in hardware.  We do _not_ call the
  189  * media-change callback.
  190  */
  191 void
  192 ifmedia_set(ifm, target)
  193         struct ifmedia *ifm; 
  194         int target;
  195 {
  196         struct ifmedia_entry *match;
  197 
  198         match = ifmedia_match(ifm, target, ifm->ifm_mask);
  199 
  200         /*
  201          * If we didn't find the requested media, then we try to fall
  202          * back to target-type (IFM_ETHER, e.g.) | IFM_NONE.  If that's
  203          * not on the list, then we add it and set the media to it.
  204          *
  205          * Since ifmedia_set is almost always called with IFM_AUTO or
  206          * with a known-good media, this really should only occur if we:
  207          *
  208          * a) didn't find any PHYs, or
  209          * b) didn't find an autoselect option on the PHY when the
  210          *    parent ethernet driver expected to.
  211          *
  212          * In either case, it makes sense to select no media.
  213          */
  214         if (match == NULL) {
  215                 printf("ifmedia_set: no match for 0x%x/0x%x\n",
  216                     target, ~ifm->ifm_mask);
  217                 target = (target & IFM_NMASK) | IFM_NONE;
  218                 match = ifmedia_match(ifm, target, ifm->ifm_mask);
  219                 if (match == NULL) {
  220                         ifmedia_add(ifm, target, 0, NULL);
  221                         match = ifmedia_match(ifm, target, ifm->ifm_mask);
  222                         if (match == NULL) {
  223                                 panic("ifmedia_set failed");
  224                         }
  225                 }
  226         }
  227         ifm->ifm_cur = match;
  228 
  229 #ifdef IFMEDIA_DEBUG
  230         if (ifmedia_debug) {
  231                 printf("ifmedia_set: target ");
  232                 ifmedia_printword(target);
  233                 printf("ifmedia_set: setting to ");
  234                 ifmedia_printword(ifm->ifm_cur->ifm_media);
  235         }
  236 #endif
  237 }
  238 
  239 /*
  240  * Device-independent media ioctl support function.
  241  */
  242 int
  243 ifmedia_ioctl(ifp, ifr, ifm, cmd)
  244         struct ifnet *ifp;
  245         struct ifreq *ifr;
  246         struct ifmedia *ifm;
  247         u_long cmd;
  248 {
  249         struct ifmedia_entry *match;
  250         struct ifmediareq *ifmr = (struct ifmediareq *) ifr;
  251         int error = 0;
  252 
  253         if (ifp == NULL || ifr == NULL || ifm == NULL)
  254                 return (EINVAL);
  255 
  256         switch (cmd) {
  257 
  258         /*
  259          * Set the current media.
  260          */
  261         case  SIOCSIFMEDIA:
  262         {
  263                 struct ifmedia_entry *oldentry;
  264                 u_int oldmedia;
  265                 u_int newmedia = ifr->ifr_media;
  266 
  267                 match = ifmedia_match(ifm, newmedia, ifm->ifm_mask);
  268                 if (match == NULL) {
  269 #ifdef IFMEDIA_DEBUG
  270                         if (ifmedia_debug) {
  271                                 printf(
  272                                     "ifmedia_ioctl: no media found for 0x%x\n", 
  273                                     newmedia);
  274                         }
  275 #endif
  276                         return (EINVAL);
  277                 }
  278 
  279                 /*
  280                  * If no change, we're done.
  281                  * XXX Automedia may involve software intervention.
  282                  *     Keep going in case the connected media changed.
  283                  *     Similarly, if best match changed (kernel debugger?).
  284                  */
  285                 if ((IFM_SUBTYPE(newmedia) != IFM_AUTO) &&
  286                     (newmedia == ifm->ifm_media) &&
  287                     (match == ifm->ifm_cur))
  288                         return 0;
  289 
  290                 /*
  291                  * We found a match, now make the driver switch to it.
  292                  * Make sure to preserve our old media type in case the
  293                  * driver can't switch.
  294                  */
  295 #ifdef IFMEDIA_DEBUG
  296                 if (ifmedia_debug) {
  297                         printf("ifmedia_ioctl: switching %s to ",
  298                             ifp->if_xname);
  299                         ifmedia_printword(match->ifm_media);
  300                 }
  301 #endif
  302                 oldentry = ifm->ifm_cur;
  303                 oldmedia = ifm->ifm_media;
  304                 ifm->ifm_cur = match;
  305                 ifm->ifm_media = newmedia;
  306                 error = (*ifm->ifm_change)(ifp);
  307                 if (error) {
  308                         ifm->ifm_cur = oldentry;
  309                         ifm->ifm_media = oldmedia;
  310                 }
  311                 break;
  312         }
  313 
  314         /*
  315          * Get list of available media and current media on interface.
  316          */
  317         case  SIOCGIFMEDIA: 
  318         {
  319                 struct ifmedia_entry *ep;
  320                 size_t nwords;
  321 
  322                 if (ifmr->ifm_count < 0)
  323                         return EINVAL;
  324 
  325                 ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ?
  326                     ifm->ifm_cur->ifm_media : IFM_NONE;
  327                 ifmr->ifm_mask = ifm->ifm_mask;
  328                 ifmr->ifm_status = 0;
  329                 (*ifm->ifm_status)(ifp, ifmr);
  330 
  331                 /*
  332                  * Count them so we know a-priori how much is the max we'll
  333                  * need.
  334                  */
  335                 ep = TAILQ_FIRST(&ifm->ifm_list);
  336                 for (nwords = 0; ep != NULL; ep = TAILQ_NEXT(ep, ifm_list))
  337                         nwords++;
  338 
  339                 if (ifmr->ifm_count != 0) {
  340                         size_t count;
  341                         size_t minwords = nwords > (size_t)ifmr->ifm_count 
  342                             ? (size_t)ifmr->ifm_count
  343                             : nwords;
  344                         int *kptr = (int *)malloc(minwords * sizeof(int),
  345                             M_TEMP, M_WAITOK);
  346                         /*
  347                          * Get the media words from the interface's list.
  348                          */
  349                         ep = TAILQ_FIRST(&ifm->ifm_list);
  350                         for (count = 0; ep != NULL && count < minwords;
  351                             ep = TAILQ_NEXT(ep, ifm_list), count++)
  352                                 kptr[count] = ep->ifm_media;
  353 
  354                         error = copyout(kptr, ifmr->ifm_ulist,
  355                             minwords * sizeof(int));
  356                         if (error == 0 && ep != NULL)
  357                                 error = E2BIG;  /* oops! */
  358                         free(kptr, M_TEMP);
  359                 }
  360                 ifmr->ifm_count = nwords;
  361                 break;
  362         }
  363 
  364         default:
  365                 return (EINVAL);
  366         }
  367 
  368         return (error);
  369 }
  370 
  371 /*
  372  * Find media entry matching a given ifm word.
  373  */
  374 struct ifmedia_entry *
  375 ifmedia_match(ifm, target, mask)
  376         struct ifmedia *ifm; 
  377         u_int target;
  378         u_int mask;
  379 {
  380         struct ifmedia_entry *match, *next;
  381 
  382         match = NULL;
  383         mask = ~mask;
  384 
  385         for (next = TAILQ_FIRST(&ifm->ifm_list); next != NULL;
  386              next = TAILQ_NEXT(next, ifm_list)) {
  387                 if ((next->ifm_media & mask) == (target & mask)) {
  388                         if (match) {
  389 #if defined(IFMEDIA_DEBUG) || defined(DIAGNOSTIC)
  390                                 printf("ifmedia_match: multiple match for "
  391                                     "0x%x/0x%x, selected instance %d\n",
  392                                     target, mask, IFM_INST(match->ifm_media));
  393 #endif
  394                                 break;
  395                         }
  396                         match = next;
  397                 }
  398         }
  399 
  400         return match;
  401 }
  402 
  403 /*
  404  * Delete all media for a given instance.
  405  */
  406 void
  407 ifmedia_delete_instance(ifm, inst)
  408         struct ifmedia *ifm;
  409         u_int inst;
  410 {
  411         struct ifmedia_entry *ife, *nife;
  412 
  413         for (ife = TAILQ_FIRST(&ifm->ifm_list); ife != NULL;
  414              ife = nife) {
  415                 nife = TAILQ_NEXT(ife, ifm_list);
  416                 if (inst == IFM_INST_ANY ||
  417                     inst == IFM_INST(ife->ifm_media)) {
  418                         TAILQ_REMOVE(&ifm->ifm_list, ife, ifm_list);
  419                         free(ife, M_IFMEDIA);
  420                 }
  421         }
  422 }
  423 
  424 /*
  425  * Compute the interface `baudrate' from the media, for the interface
  426  * metrics (used by routing daemons).
  427  */
  428 static const struct ifmedia_baudrate ifmedia_baudrate_descriptions[] =
  429     IFM_BAUDRATE_DESCRIPTIONS;
  430 
  431 u_quad_t
  432 ifmedia_baudrate(int mword)
  433 {
  434         int i;
  435 
  436         for (i = 0; ifmedia_baudrate_descriptions[i].ifmb_word != 0; i++) {
  437                 if ((mword & (IFM_NMASK|IFM_TMASK)) ==
  438                     ifmedia_baudrate_descriptions[i].ifmb_word)
  439                         return (ifmedia_baudrate_descriptions[i].ifmb_baudrate);
  440         }
  441 
  442         /* Not known. */
  443         return (0);
  444 }
  445 
  446 #ifdef IFMEDIA_DEBUG
  447 
  448 static const struct ifmedia_description ifm_type_descriptions[] =
  449     IFM_TYPE_DESCRIPTIONS;
  450 
  451 static const struct ifmedia_description ifm_subtype_descriptions[] =
  452     IFM_SUBTYPE_DESCRIPTIONS;
  453 
  454 static const struct ifmedia_description ifm_option_descriptions[] =
  455     IFM_OPTION_DESCRIPTIONS;
  456 
  457 /*
  458  * print a media word.
  459  */
  460 static void
  461 ifmedia_printword(ifmw)
  462         int ifmw;
  463 {
  464         const struct ifmedia_description *desc;
  465         int seen_option = 0;
  466 
  467         /* Print the top-level interface type. */
  468         for (desc = ifm_type_descriptions; desc->ifmt_string != NULL;
  469              desc++) {
  470                 if (IFM_TYPE(ifmw) == desc->ifmt_word)
  471                         break;
  472         }
  473         if (desc->ifmt_string == NULL)
  474                 printf("<unknown type> ");
  475         else
  476                 printf("%s ", desc->ifmt_string);
  477 
  478         /* Print the subtype. */
  479         for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL;
  480              desc++) {
  481                 if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) &&
  482                     IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(ifmw))
  483                         break;
  484         }
  485         if (desc->ifmt_string == NULL)
  486                 printf("<unknown subtype>");
  487         else
  488                 printf("%s", desc->ifmt_string);
  489 
  490         /* Print any options. */
  491         for (desc = ifm_option_descriptions; desc->ifmt_string != NULL;
  492              desc++) {
  493                 if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) &&
  494                     (ifmw & desc->ifmt_word) != 0 &&
  495                     (seen_option & IFM_OPTIONS(desc->ifmt_word)) == 0) {
  496                         if (seen_option == 0)
  497                                 printf(" <");
  498                         printf("%s%s", seen_option ? "," : "",
  499                             desc->ifmt_string);
  500                         seen_option |= IFM_OPTIONS(desc->ifmt_word);
  501                 }
  502         }
  503         printf("%s\n", seen_option ? ">" : "");
  504 }
  505 
  506 #endif /* IFMEDIA_DEBUG */

Cache object: db2788a2da609de6676fc951472b4ef5


[ 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.