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