Home | History | Annotate | Line # | Download | only in net
if_media.c revision 1.2
      1 /*	$NetBSD: if_media.c,v 1.2 1998/08/06 02:19:34 thorpej 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/param.h>
     86 #include <sys/systm.h>
     87 #include <sys/errno.h>
     88 #include <sys/ioctl.h>
     89 #include <sys/socket.h>
     90 #include <sys/malloc.h>
     91 
     92 #include <net/if.h>
     93 #include <net/if_media.h>
     94 #include <net/netisr.h>
     95 
     96 /*
     97  * Compile-time options:
     98  * IFMEDIA_DEBUG:
     99  *	turn on implementation-level debug printfs.
    100  * 	Useful for debugging newly-ported  drivers.
    101  */
    102 
    103 struct ifmedia_entry *ifmedia_match __P((struct ifmedia *ifm,
    104     int flags, int mask));
    105 
    106 #ifdef IFMEDIA_DEBUG
    107 int	ifmedia_debug = 0;
    108 static	void ifmedia_printword __P((int));
    109 #endif
    110 
    111 /*
    112  * Initialize if_media struct for a specific interface instance.
    113  */
    114 void
    115 ifmedia_init(ifm, dontcare_mask, change_callback, status_callback)
    116 	struct ifmedia *ifm;
    117 	int dontcare_mask;
    118 	ifm_change_cb_t change_callback;
    119 	ifm_stat_cb_t status_callback;
    120 {
    121 
    122 	LIST_INIT(&ifm->ifm_list);
    123 	ifm->ifm_cur = NULL;
    124 	ifm->ifm_media = 0;
    125 	ifm->ifm_mask = dontcare_mask;		/* IF don't-care bits */
    126 	ifm->ifm_change = change_callback;
    127 	ifm->ifm_status = status_callback;
    128 }
    129 
    130 /*
    131  * Add a media configuration to the list of supported media
    132  * for a specific interface instance.
    133  */
    134 void
    135 ifmedia_add(ifm, mword, data, aux)
    136 	struct ifmedia *ifm;
    137 	int mword;
    138 	int data;
    139 	void *aux;
    140 {
    141 	register struct ifmedia_entry *entry;
    142 
    143 #ifdef IFMEDIA_DEBUG
    144 	if (ifmedia_debug) {
    145 		if (ifm == NULL) {
    146 			printf("ifmedia_add: null ifm\n");
    147 			return;
    148 		}
    149 		printf("Adding entry for ");
    150 		ifmedia_printword(mword);
    151 	}
    152 #endif
    153 
    154 	entry = malloc(sizeof(*entry), M_IFADDR, M_NOWAIT);
    155 	if (entry == NULL)
    156 		panic("ifmedia_add: can't malloc entry");
    157 
    158 	entry->ifm_media = mword;
    159 	entry->ifm_data = data;
    160 	entry->ifm_aux = aux;
    161 
    162 	LIST_INSERT_HEAD(&ifm->ifm_list, entry, ifm_list);
    163 }
    164 
    165 /*
    166  * Add an array of media configurations to the list of
    167  * supported media for a specific interface instance.
    168  */
    169 void
    170 ifmedia_list_add(ifm, lp, count)
    171 	struct ifmedia *ifm;
    172 	struct ifmedia_entry *lp;
    173 	int count;
    174 {
    175 	int i;
    176 
    177 	for (i = 0; i < count; i++)
    178 		ifmedia_add(ifm, lp[i].ifm_media, lp[i].ifm_data,
    179 		    lp[i].ifm_aux);
    180 }
    181 
    182 /*
    183  * Set the default active media.
    184  *
    185  * Called by device-specific code which is assumed to have already
    186  * selected the default media in hardware.  We do _not_ call the
    187  * media-change callback.
    188  */
    189 void
    190 ifmedia_set(ifm, target)
    191 	struct ifmedia *ifm;
    192 	int target;
    193 
    194 {
    195 	struct ifmedia_entry *match;
    196 
    197 	match = ifmedia_match(ifm, target, ifm->ifm_mask);
    198 
    199 	if (match == NULL) {
    200 		printf("ifmedia_set: no match for 0x%x/0x%x\n",
    201 		    target, ~ifm->ifm_mask);
    202 		panic("ifmedia_set");
    203 	}
    204 	ifm->ifm_cur = match;
    205 
    206 #ifdef IFMEDIA_DEBUG
    207 	if (ifmedia_debug) {
    208 		printf("ifmedia_set: target ");
    209 		ifmedia_printword(target);
    210 		printf("ifmedia_set: setting to ");
    211 		ifmedia_printword(ifm->ifm_cur->ifm_media);
    212 	}
    213 #endif
    214 }
    215 
    216 /*
    217  * Device-independent media ioctl support function.
    218  */
    219 int
    220 ifmedia_ioctl(ifp, ifr, ifm, cmd)
    221 	struct ifnet *ifp;
    222 	struct ifreq *ifr;
    223 	struct ifmedia *ifm;
    224 	u_long cmd;
    225 {
    226 	struct ifmedia_entry *match;
    227 	struct ifmediareq *ifmr = (struct ifmediareq *) ifr;
    228 	int error = 0, sticky;
    229 
    230 	if (ifp == NULL || ifr == NULL || ifm == NULL)
    231 		return(EINVAL);
    232 
    233 	switch (cmd) {
    234 
    235 	/*
    236 	 * Set the current media.
    237 	 */
    238 	case  SIOCSIFMEDIA:
    239 	{
    240 		struct ifmedia_entry *oldentry;
    241 		int oldmedia;
    242 		int newmedia = ifr->ifr_media;
    243 
    244 		match = ifmedia_match(ifm, newmedia, ifm->ifm_mask);
    245 		if (match == NULL) {
    246 #ifdef IFMEDIA_DEBUG
    247 			if (ifmedia_debug) {
    248 				printf(
    249 				    "ifmedia_ioctl: no media found for 0x%x\n",
    250 				    newmedia);
    251 			}
    252 #endif
    253 			return (ENXIO);
    254 		}
    255 
    256 		/*
    257 		 * If no change, we're done.
    258 		 * XXX Automedia may invole software intervention.
    259 		 *     Keep going in case the the connected media changed.
    260 		 *     Similarly, if best match changed (kernel debugger?).
    261 		 */
    262 		if ((IFM_SUBTYPE(newmedia) != IFM_AUTO) &&
    263 		    (newmedia == ifm->ifm_media) &&
    264 		    (match == ifm->ifm_cur))
    265 			return 0;
    266 
    267 		/*
    268 		 * We found a match, now make the driver switch to it.
    269 		 * Make sure to preserve our old media type in case the
    270 		 * driver can't switch.
    271 		 */
    272 #ifdef IFMEDIA_DEBUG
    273 		if (ifmedia_debug) {
    274 			printf("ifmedia_ioctl: switching %s to ",
    275 			    ifp->if_xname);
    276 			ifmedia_printword(match->ifm_media);
    277 		}
    278 #endif
    279 		oldentry = ifm->ifm_cur;
    280 		oldmedia = ifm->ifm_media;
    281 		ifm->ifm_cur = match;
    282 		ifm->ifm_media = newmedia;
    283 		error = (*ifm->ifm_change)(ifp);
    284 		if (error) {
    285 			ifm->ifm_cur = oldentry;
    286 			ifm->ifm_media = oldmedia;
    287 		}
    288 		break;
    289 	}
    290 
    291 	/*
    292 	 * Get list of available media and current media on interface.
    293 	 */
    294 	case  SIOCGIFMEDIA:
    295 	{
    296 		struct ifmedia_entry *ep;
    297 		int *kptr, count;
    298 
    299 		kptr = NULL;		/* XXX gcc */
    300 
    301 		ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ?
    302 		    ifm->ifm_cur->ifm_media : IFM_NONE;
    303 		ifmr->ifm_mask = ifm->ifm_mask;
    304 		ifmr->ifm_status = 0;
    305 		(*ifm->ifm_status)(ifp, ifmr);
    306 
    307 		count = 0;
    308 		ep = ifm->ifm_list.lh_first;
    309 
    310 		if (ifmr->ifm_count != 0) {
    311 			kptr = (int *)malloc(ifmr->ifm_count * sizeof(int),
    312 			    M_TEMP, M_WAITOK);
    313 
    314 			/*
    315 			 * Get the media words from the interface's list.
    316 			 */
    317 			for (; ep != NULL && count < ifmr->ifm_count;
    318 			    ep = ep->ifm_list.le_next, count++)
    319 				kptr[count] = ep->ifm_media;
    320 
    321 			if (ep != NULL)
    322 				error = E2BIG;	/* oops! */
    323 		}
    324 
    325 		/*
    326 		 * If there are more interfaces on the list, count
    327 		 * them.  This allows the caller to set ifmr->ifm_count
    328 		 * to 0 on the first call to know how much space to
    329 		 * callocate.
    330 		 */
    331 		for (; ep != NULL; ep = ep->ifm_list.le_next)
    332 			count++;
    333 
    334 		/*
    335 		 * We do the copyout on E2BIG, because that's
    336 		 * just our way of telling userland that there
    337 		 * are more.  This is the behavior I've observed
    338 		 * under BSD/OS 3.0
    339 		 */
    340 		sticky = error;
    341 		if ((error == 0 || error == E2BIG) && ifmr->ifm_count != 0) {
    342 			error = copyout((caddr_t)kptr,
    343 			    (caddr_t)ifmr->ifm_ulist,
    344 			    ifmr->ifm_count * sizeof(int));
    345 		}
    346 
    347 		if (error == 0)
    348 			error = sticky;
    349 
    350 		if (ifmr->ifm_count != 0)
    351 			free(kptr, M_TEMP);
    352 
    353 		ifmr->ifm_count = count;
    354 		break;
    355 	}
    356 
    357 	default:
    358 		return (EINVAL);
    359 	}
    360 
    361 	return (error);
    362 }
    363 
    364 /*
    365  * Find media entry matching a given ifm word.
    366  *
    367  */
    368 struct ifmedia_entry *
    369 ifmedia_match(ifm, target, mask)
    370 	struct ifmedia *ifm;
    371 	int target;
    372 	int mask;
    373 {
    374 	struct ifmedia_entry *match, *next;
    375 
    376 	match = NULL;
    377 	mask = ~mask;
    378 
    379 	for (next = ifm->ifm_list.lh_first; next != NULL;
    380 	    next = next->ifm_list.le_next) {
    381 		if ((next->ifm_media & mask) == (target & mask)) {
    382 #if defined(IFMEDIA_DEBUG) || defined(DIAGNOSTIC)
    383 			if (match) {
    384 				printf("ifmedia_match: multiple match for "
    385 				    "0x%x/0x%x\n", target, mask);
    386 			}
    387 #endif
    388 			match = next;
    389 		}
    390 	}
    391 
    392 	return match;
    393 }
    394 
    395 #ifdef IFMEDIA_DEBUG
    396 
    397 struct ifmedia_description ifm_type_descriptions[] =
    398     IFM_TYPE_DESCRIPTIONS;
    399 
    400 struct ifmedia_description ifm_subtype_descriptions[] =
    401     IFM_SUBTYPE_DESCRIPTIONS;
    402 
    403 struct ifmedia_description ifm_option_descriptions[] =
    404     IFM_OPTION_DESCRIPTIONS;
    405 
    406 /*
    407  * print a media word.
    408  */
    409 static void
    410 ifmedia_printword(ifmw)
    411 	int ifmw;
    412 {
    413 	struct ifmedia_description *desc;
    414 	int seen_option = 0;
    415 
    416 	/* Print the top-level interface type. */
    417 	for (desc = ifm_type_descriptions; desc->ifmt_string != NULL;
    418 	     desc++) {
    419 		if (IFM_TYPE(ifmw) == desc->ifmt_word)
    420 			break;
    421 	}
    422 	if (desc->ifmt_string == NULL)
    423 		printf("<unknown type> ");
    424 	else
    425 		printf("%s ", desc->ifmt_string);
    426 
    427 	/* Print the subtype. */
    428 	for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL;
    429 	     desc++) {
    430 		if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) &&
    431 		    IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(ifmw))
    432 			break;
    433 	}
    434 	if (desc->ifmt_string == NULL)
    435 		printf("<unknown subtype>");
    436 	else
    437 		printf("%s", desc->ifmt_string);
    438 
    439 	/* Print any options. */
    440 	for (desc = ifm_option_descriptions; desc->ifmt_string != NULL;
    441 	     desc++) {
    442 		if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) &&
    443 		    (ifmw & desc->ifmt_word) != 0 &&
    444 		    (seen_option & IFM_OPTIONS(desc->ifmt_word)) == 0) {
    445 			if (seen_option == 0)
    446 				printf(" <");
    447 			printf("%s%s", seen_option ? "," : ""
    448 			    desc->ifmt_string);
    449 			seen_option |= IFM_OPTIONS(desc->ifmt_word);
    450 		}
    451 	}
    452 	printf("%s\n", seen_option ? ">" : "");
    453 }
    454 
    455 #endif /* IFMEDIA_DEBUG */
    456