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