xref: /netbsd-src/sys/net/if_media.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: if_media.c,v 1.26 2007/05/29 21:32:30 christos 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.26 2007/05/29 21:32:30 christos 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(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(struct ifmedia *ifm, int dontcare_mask,
118     ifm_change_cb_t change_callback, ifm_stat_cb_t status_callback)
119 {
120 
121 	TAILQ_INIT(&ifm->ifm_list);
122 	ifm->ifm_cur = NULL;
123 	ifm->ifm_media = 0;
124 	ifm->ifm_mask = dontcare_mask;		/* IF don't-care bits */
125 	ifm->ifm_change = change_callback;
126 	ifm->ifm_status = status_callback;
127 }
128 
129 /*
130  * Add a media configuration to the list of supported media
131  * for a specific interface instance.
132  */
133 void
134 ifmedia_add(struct ifmedia *ifm, int mword, int data, void *aux)
135 {
136 	struct ifmedia_entry *entry;
137 
138 #ifdef IFMEDIA_DEBUG
139 	if (ifmedia_debug) {
140 		if (ifm == NULL) {
141 			printf("ifmedia_add: null ifm\n");
142 			return;
143 		}
144 		printf("Adding entry for ");
145 		ifmedia_printword(mword);
146 	}
147 #endif
148 
149 	entry = malloc(sizeof(*entry), M_IFMEDIA, M_NOWAIT);
150 	if (entry == NULL)
151 		panic("ifmedia_add: can't malloc entry");
152 
153 	entry->ifm_media = mword;
154 	entry->ifm_data = data;
155 	entry->ifm_aux = aux;
156 
157 	TAILQ_INSERT_TAIL(&ifm->ifm_list, entry, ifm_list);
158 }
159 
160 /*
161  * Add an array of media configurations to the list of
162  * supported media for a specific interface instance.
163  */
164 void
165 ifmedia_list_add(struct ifmedia *ifm, struct ifmedia_entry *lp, int count)
166 {
167 	int i;
168 
169 	for (i = 0; i < count; i++)
170 		ifmedia_add(ifm, lp[i].ifm_media, lp[i].ifm_data,
171 		    lp[i].ifm_aux);
172 }
173 
174 /*
175  * Set the default active media.
176  *
177  * Called by device-specific code which is assumed to have already
178  * selected the default media in hardware.  We do _not_ call the
179  * media-change callback.
180  */
181 void
182 ifmedia_set(struct ifmedia *ifm, int target)
183 {
184 	struct ifmedia_entry *match;
185 
186 	match = ifmedia_match(ifm, target, ifm->ifm_mask);
187 
188 	/*
189 	 * If we didn't find the requested media, then we try to fall
190 	 * back to target-type (IFM_ETHER, e.g.) | IFM_NONE.  If that's
191 	 * not on the list, then we add it and set the media to it.
192 	 *
193 	 * Since ifmedia_set is almost always called with IFM_AUTO or
194 	 * with a known-good media, this really should only occur if we:
195 	 *
196 	 * a) didn't find any PHYs, or
197 	 * b) didn't find an autoselect option on the PHY when the
198 	 *    parent ethernet driver expected to.
199 	 *
200 	 * In either case, it makes sense to select no media.
201 	 */
202 	if (match == NULL) {
203 		printf("ifmedia_set: no match for 0x%x/0x%x\n",
204 		    target, ~ifm->ifm_mask);
205 		target = (target & IFM_NMASK) | IFM_NONE;
206 		match = ifmedia_match(ifm, target, ifm->ifm_mask);
207 		if (match == NULL) {
208 			ifmedia_add(ifm, target, 0, NULL);
209 			match = ifmedia_match(ifm, target, ifm->ifm_mask);
210 			if (match == NULL) {
211 				panic("ifmedia_set failed");
212 			}
213 		}
214 	}
215 	ifm->ifm_cur = match;
216 
217 #ifdef IFMEDIA_DEBUG
218 	if (ifmedia_debug) {
219 		printf("ifmedia_set: target ");
220 		ifmedia_printword(target);
221 		printf("ifmedia_set: setting to ");
222 		ifmedia_printword(ifm->ifm_cur->ifm_media);
223 	}
224 #endif
225 }
226 
227 /*
228  * Device-independent media ioctl support function.
229  */
230 int
231 ifmedia_ioctl(struct ifnet *ifp, struct ifreq *ifr, struct ifmedia *ifm,
232     u_long cmd)
233 {
234 	struct ifmedia_entry *match;
235 	struct ifmediareq *ifmr = (struct ifmediareq *) ifr;
236 	int error = 0;
237 #ifdef OSIOCSIFMEDIA
238 	struct oifreq *oifr = (struct oifreq *)ifr;
239 #endif
240 
241 	if (ifp == NULL || ifr == NULL || ifm == NULL)
242 		return (EINVAL);
243 
244 	switch (cmd) {
245 
246 #ifdef OSIOCSIFMEDIA
247 	case OSIOCSIFMEDIA:
248 		ifr->ifr_media = oifr->ifr_media;
249 		/*FALLTHROUGH*/
250 #endif
251 	/*
252 	 * Set the current media.
253 	 */
254 	case SIOCSIFMEDIA:
255 	{
256 		struct ifmedia_entry *oldentry;
257 		u_int oldmedia;
258 		u_int newmedia = ifr->ifr_media;
259 
260 		match = ifmedia_match(ifm, newmedia, ifm->ifm_mask);
261 		if (match == NULL) {
262 #ifdef IFMEDIA_DEBUG
263 			if (ifmedia_debug) {
264 				printf(
265 				    "ifmedia_ioctl: no media found for 0x%x\n",
266 				    newmedia);
267 			}
268 #endif
269 			return (EINVAL);
270 		}
271 
272 		/*
273 		 * If no change, we're done.
274 		 * XXX Automedia may involve software intervention.
275 		 *     Keep going in case the connected media changed.
276 		 *     Similarly, if best match changed (kernel debugger?).
277 		 */
278 		if ((IFM_SUBTYPE(newmedia) != IFM_AUTO) &&
279 		    (newmedia == ifm->ifm_media) &&
280 		    (match == ifm->ifm_cur))
281 			return 0;
282 
283 		/*
284 		 * We found a match, now make the driver switch to it.
285 		 * Make sure to preserve our old media type in case the
286 		 * driver can't switch.
287 		 */
288 #ifdef IFMEDIA_DEBUG
289 		if (ifmedia_debug) {
290 			printf("ifmedia_ioctl: switching %s to ",
291 			    ifp->if_xname);
292 			ifmedia_printword(match->ifm_media);
293 		}
294 #endif
295 		oldentry = ifm->ifm_cur;
296 		oldmedia = ifm->ifm_media;
297 		ifm->ifm_cur = match;
298 		ifm->ifm_media = newmedia;
299 		error = (*ifm->ifm_change)(ifp);
300 		if (error) {
301 			ifm->ifm_cur = oldentry;
302 			ifm->ifm_media = oldmedia;
303 		}
304 		break;
305 	}
306 
307 	/*
308 	 * Get list of available media and current media on interface.
309 	 */
310 	case SIOCGIFMEDIA:
311 	{
312 		struct ifmedia_entry *ep;
313 		size_t nwords;
314 
315 		if (ifmr->ifm_count < 0)
316 			return EINVAL;
317 
318 		ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ?
319 		    ifm->ifm_cur->ifm_media : IFM_NONE;
320 		ifmr->ifm_mask = ifm->ifm_mask;
321 		ifmr->ifm_status = 0;
322 		(*ifm->ifm_status)(ifp, ifmr);
323 
324 		/*
325 		 * Count them so we know a-priori how much is the max we'll
326 		 * need.
327 		 */
328 		ep = TAILQ_FIRST(&ifm->ifm_list);
329 		for (nwords = 0; ep != NULL; ep = TAILQ_NEXT(ep, ifm_list))
330 			nwords++;
331 
332 		if (ifmr->ifm_count != 0) {
333 			size_t count;
334 			size_t minwords = nwords > (size_t)ifmr->ifm_count
335 			    ? (size_t)ifmr->ifm_count
336 			    : nwords;
337 			int *kptr = (int *)malloc(minwords * sizeof(int),
338 			    M_TEMP, M_WAITOK);
339 			/*
340 			 * Get the media words from the interface's list.
341 			 */
342 			ep = TAILQ_FIRST(&ifm->ifm_list);
343 			for (count = 0; ep != NULL && count < minwords;
344 			    ep = TAILQ_NEXT(ep, ifm_list), count++)
345 				kptr[count] = ep->ifm_media;
346 
347 			error = copyout(kptr, ifmr->ifm_ulist,
348 			    minwords * sizeof(int));
349 			if (error == 0 && ep != NULL)
350 				error = E2BIG;	/* oops! */
351 			free(kptr, M_TEMP);
352 		}
353 		ifmr->ifm_count = nwords;
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 struct ifmedia_entry *
368 ifmedia_match(struct ifmedia *ifm, u_int target, u_int mask)
369 {
370 	struct ifmedia_entry *match, *next;
371 
372 	match = NULL;
373 	mask = ~mask;
374 
375 	for (next = TAILQ_FIRST(&ifm->ifm_list); next != NULL;
376 	     next = TAILQ_NEXT(next, ifm_list)) {
377 		if ((next->ifm_media & mask) == (target & mask)) {
378 			if (match) {
379 #if defined(IFMEDIA_DEBUG) || defined(DIAGNOSTIC)
380 				printf("ifmedia_match: multiple match for "
381 				    "0x%x/0x%x, selected instance %d\n",
382 				    target, mask, IFM_INST(match->ifm_media));
383 #endif
384 				break;
385 			}
386 			match = next;
387 		}
388 	}
389 
390 	return match;
391 }
392 
393 /*
394  * Delete all media for a given instance.
395  */
396 void
397 ifmedia_delete_instance(struct ifmedia *ifm, u_int inst)
398 {
399 	struct ifmedia_entry *ife, *nife;
400 
401 	for (ife = TAILQ_FIRST(&ifm->ifm_list); ife != NULL;
402 	     ife = nife) {
403 		nife = TAILQ_NEXT(ife, ifm_list);
404 		if (inst == IFM_INST_ANY ||
405 		    inst == IFM_INST(ife->ifm_media)) {
406 			TAILQ_REMOVE(&ifm->ifm_list, ife, ifm_list);
407 			free(ife, M_IFMEDIA);
408 		}
409 	}
410 }
411 
412 /*
413  * Compute the interface `baudrate' from the media, for the interface
414  * metrics (used by routing daemons).
415  */
416 static const struct ifmedia_baudrate ifmedia_baudrate_descriptions[] =
417     IFM_BAUDRATE_DESCRIPTIONS;
418 
419 u_quad_t
420 ifmedia_baudrate(int mword)
421 {
422 	int i;
423 
424 	for (i = 0; ifmedia_baudrate_descriptions[i].ifmb_word != 0; i++) {
425 		if ((mword & (IFM_NMASK|IFM_TMASK)) ==
426 		    ifmedia_baudrate_descriptions[i].ifmb_word)
427 			return (ifmedia_baudrate_descriptions[i].ifmb_baudrate);
428 	}
429 
430 	/* Not known. */
431 	return (0);
432 }
433 
434 #ifdef IFMEDIA_DEBUG
435 
436 static const struct ifmedia_description ifm_type_descriptions[] =
437     IFM_TYPE_DESCRIPTIONS;
438 
439 static const struct ifmedia_description ifm_subtype_descriptions[] =
440     IFM_SUBTYPE_DESCRIPTIONS;
441 
442 static const struct ifmedia_description ifm_option_descriptions[] =
443     IFM_OPTION_DESCRIPTIONS;
444 
445 /*
446  * print a media word.
447  */
448 static void
449 ifmedia_printword(int ifmw)
450 {
451 	const struct ifmedia_description *desc;
452 	int seen_option = 0;
453 
454 	/* Print the top-level interface type. */
455 	for (desc = ifm_type_descriptions; desc->ifmt_string != NULL;
456 	     desc++) {
457 		if (IFM_TYPE(ifmw) == desc->ifmt_word)
458 			break;
459 	}
460 	if (desc->ifmt_string == NULL)
461 		printf("<unknown type> ");
462 	else
463 		printf("%s ", desc->ifmt_string);
464 
465 	/* Print the subtype. */
466 	for (desc = ifm_subtype_descriptions; desc->ifmt_string != NULL;
467 	     desc++) {
468 		if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) &&
469 		    IFM_SUBTYPE(desc->ifmt_word) == IFM_SUBTYPE(ifmw))
470 			break;
471 	}
472 	if (desc->ifmt_string == NULL)
473 		printf("<unknown subtype>");
474 	else
475 		printf("%s", desc->ifmt_string);
476 
477 	/* Print any options. */
478 	for (desc = ifm_option_descriptions; desc->ifmt_string != NULL;
479 	     desc++) {
480 		if (IFM_TYPE_MATCH(desc->ifmt_word, ifmw) &&
481 		    (ifmw & desc->ifmt_word) != 0 &&
482 		    (seen_option & IFM_OPTIONS(desc->ifmt_word)) == 0) {
483 			if (seen_option == 0)
484 				printf(" <");
485 			printf("%s%s", seen_option ? "," : "",
486 			    desc->ifmt_string);
487 			seen_option |= IFM_OPTIONS(desc->ifmt_word);
488 		}
489 	}
490 	printf("%s\n", seen_option ? ">" : "");
491 }
492 
493 #endif /* IFMEDIA_DEBUG */
494