xref: /dragonfly/sys/net/if_media.c (revision 335b9e93)
1 /*	$NetBSD: if_media.c,v 1.1 1997/03/17 02:55:15 thorpej Exp $	*/
2 /* $FreeBSD: src/sys/net/if_media.c,v 1.9.2.4 2001/07/04 00:12:38 brooks Exp $ */
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/socket.h>
52 #include <sys/sockio.h>
53 #include <sys/malloc.h>
54 
55 #include <net/if.h>
56 #include <net/if_var.h>
57 #include <net/if_media.h>
58 
59 /*
60  * Compile-time options:
61  * IFMEDIA_DEBUG:
62  *	turn on implementation-level debug kprintfs.
63  *	Useful for debugging newly-ported  drivers.
64  */
65 
66 static struct ifmedia_entry *ifmedia_match(struct ifmedia *ifm,
67     int flags, int mask);
68 
69 #ifdef IFMEDIA_DEBUG
70 int	ifmedia_debug = 0;
71 static	void ifmedia_printword (int);
72 #endif
73 
74 /*
75  * Initialize if_media struct for a specific interface instance.
76  */
77 void
78 ifmedia_init(struct ifmedia *ifm, int dontcare_mask,
79 	     ifm_change_cb_t change_callback, ifm_stat_cb_t status_callback)
80 {
81 
82 	LIST_INIT(&ifm->ifm_list);
83 	ifm->ifm_cur = NULL;
84 	ifm->ifm_media = IFM_NONE;
85 	ifm->ifm_mask = dontcare_mask;		/* IF don't-care bits */
86 	ifm->ifm_change = change_callback;
87 	ifm->ifm_status = status_callback;
88 }
89 
90 void
91 ifmedia_removeall(struct ifmedia *ifm)
92 {
93 	struct ifmedia_entry *entry;
94 
95 	while ((entry = LIST_FIRST(&ifm->ifm_list)) != NULL) {
96 		LIST_REMOVE(entry, ifm_list);
97 		kfree(entry, M_IFADDR);
98 	}
99 	ifm->ifm_cur = NULL;
100 	ifm->ifm_media = IFM_NONE;
101 }
102 
103 /*
104  * Add a media configuration to the list of supported media
105  * for a specific interface instance.
106  */
107 void
108 ifmedia_add(struct ifmedia *ifm, int mword, int data, void *aux)
109 {
110 	struct ifmedia_entry *entry;
111 
112 #ifdef IFMEDIA_DEBUG
113 	if (ifmedia_debug) {
114 		if (ifm == NULL) {
115 			kprintf("ifmedia_add: null ifm\n");
116 			return;
117 		}
118 		kprintf("Adding entry for ");
119 		ifmedia_printword(mword);
120 	}
121 #endif
122 
123 	entry = kmalloc(sizeof(*entry), M_IFADDR, M_INTWAIT);
124 	entry->ifm_media = mword;
125 	entry->ifm_data = data;
126 	entry->ifm_aux = aux;
127 
128 	LIST_INSERT_HEAD(&ifm->ifm_list, entry, ifm_list);
129 }
130 
131 int
132 ifmedia_add_nodup(struct ifmedia *ifm, int mword, int data, void *aux)
133 {
134 	struct ifmedia_entry *match;
135 
136 	match = ifmedia_match(ifm, mword, ifm->ifm_mask);
137 	if (match != NULL)
138 		return EEXIST;
139 	ifmedia_add(ifm, mword, data, aux);
140 	return 0;
141 }
142 
143 /*
144  * Add an array of media configurations to the list of
145  * supported media for a specific interface instance.
146  */
147 void
148 ifmedia_list_add(struct ifmedia *ifm, struct ifmedia_entry *lp,
149 		 int count)
150 {
151 	int i;
152 
153 	for (i = 0; i < count; i++)
154 		ifmedia_add(ifm, lp[i].ifm_media, lp[i].ifm_data,
155 		    lp[i].ifm_aux);
156 }
157 
158 /*
159  * Set the default active media.
160  *
161  * Called by device-specific code which is assumed to have already
162  * selected the default media in hardware.  We do _not_ call the
163  * media-change callback.
164  */
165 void
166 ifmedia_set(struct ifmedia *ifm, int target)
167 {
168 	struct ifmedia_entry *match;
169 
170 	match = ifmedia_match(ifm, target, ifm->ifm_mask);
171 	if (match == NULL) {
172 		kprintf("ifmedia_set: no match for 0x%x/0x%x\n",
173 		    target, ~ifm->ifm_mask);
174 		panic("ifmedia_set");
175 	}
176 	ifm->ifm_cur = match;
177 	ifm->ifm_media = target;
178 
179 #ifdef IFMEDIA_DEBUG
180 	if (ifmedia_debug) {
181 		kprintf("ifmedia_set: target ");
182 		ifmedia_printword(target);
183 		kprintf("ifmedia_set: setting to ");
184 		ifmedia_printword(ifm->ifm_cur->ifm_media);
185 	}
186 #endif
187 }
188 
189 int
190 ifmedia_tryset(struct ifmedia *ifm, int target)
191 {
192 	struct ifmedia_entry *match;
193 
194 	match = ifmedia_match(ifm, target, ifm->ifm_mask);
195 	if (match == NULL)
196 		return ENOENT;
197 	ifmedia_set(ifm, target);
198 	return 0;
199 }
200 
201 /*
202  * Device-independent media ioctl support function, typically called
203  * from the device network ioctl procedure.
204  */
205 int
206 ifmedia_ioctl(struct ifnet *ifp, struct ifreq *ifr,
207 	      struct ifmedia *ifm, u_long cmd)
208 {
209 	struct ifmedia_entry *match;
210 	struct ifmediareq *ifmr = (struct ifmediareq *) ifr;
211 	int error = 0, sticky;
212 
213 	if (ifp == NULL || ifr == NULL || ifm == NULL)
214 		return (EINVAL);
215 
216 	ASSERT_IFNET_SERIALIZED_ALL(ifp);
217 
218 	switch (cmd) {
219 	/*
220 	 * Set the current media.
221 	 */
222 	case  SIOCSIFMEDIA:
223 	{
224 		struct ifmedia_entry *oldentry;
225 		int oldmedia;
226 		int newmedia = ifr->ifr_media;
227 
228 		match = ifmedia_match(ifm, newmedia, ifm->ifm_mask);
229 		if (match == NULL) {
230 #ifdef IFMEDIA_DEBUG
231 			if (ifmedia_debug) {
232 				kprintf(
233 				    "ifmedia_ioctl: no media found for 0x%x\n",
234 				    newmedia);
235 			}
236 #endif
237 			return (ENXIO);
238 		}
239 
240 		/*
241 		 * If no change, we're done.
242 		 * XXX Automedia may invole software intervention.
243 		 *     Keep going in case the the connected media changed.
244 		 *     Similarly, if best match changed (kernel debugger?).
245 		 */
246 		if ((IFM_SUBTYPE(newmedia) != IFM_AUTO) &&
247 		    (newmedia == ifm->ifm_media) &&
248 		    (match == ifm->ifm_cur))
249 			return 0;
250 
251 		/*
252 		 * We found a match, now make the driver switch to it.
253 		 * Make sure to preserve our old media type in case the
254 		 * driver can't switch.
255 		 */
256 #ifdef IFMEDIA_DEBUG
257 		if (ifmedia_debug) {
258 			kprintf("ifmedia_ioctl: switching %s to ",
259 			    ifp->if_xname);
260 			ifmedia_printword(match->ifm_media);
261 		}
262 #endif
263 		oldentry = ifm->ifm_cur;
264 		oldmedia = ifm->ifm_media;
265 		ifm->ifm_cur = match;
266 		ifm->ifm_media = newmedia;
267 		error = (*ifm->ifm_change)(ifp);
268 		if (error) {
269 			ifm->ifm_cur = oldentry;
270 			ifm->ifm_media = oldmedia;
271 		}
272 		break;
273 	}
274 
275 	/*
276 	 * Get list of available media and current media on interface.
277 	 */
278 	case  SIOCGIFMEDIA:
279 	{
280 		struct ifmedia_entry *ep;
281 		int *kptr, count;
282 		int usermax;	/* user requested max */
283 
284 		kptr = NULL;		/* XXX gcc */
285 
286 		/*
287 		 * NOTE:
288 		 * ifm_cur may not contain certain media options, e.g.
289 		 * flow control options, so ifm_media should be used
290 		 * instead.
291 		 */
292 		ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ?
293 		    ifm->ifm_media : IFM_NONE;
294 		ifmr->ifm_mask = ifm->ifm_mask;
295 		ifmr->ifm_status = 0;
296 		(*ifm->ifm_status)(ifp, ifmr);
297 
298 		count = 0;
299 		usermax = 0;
300 
301 		/*
302 		 * If there are more interfaces on the list, count
303 		 * them.  This allows the caller to set ifmr->ifm_count
304 		 * to 0 on the first call to know how much space to
305 		 * allocate.
306 		 */
307 		LIST_FOREACH(ep, &ifm->ifm_list, ifm_list)
308 			usermax++;
309 
310 		/*
311 		 * Don't allow the user to ask for too many
312 		 * or a negative number.
313 		 */
314 		if (ifmr->ifm_count > usermax)
315 			ifmr->ifm_count = usermax;
316 		else if (ifmr->ifm_count < 0)
317 			return (EINVAL);
318 
319 		if (ifmr->ifm_count != 0) {
320 			kptr = (int *)kmalloc(ifmr->ifm_count * sizeof(int),
321 			    M_TEMP, M_WAITOK);
322 
323 			/*
324 			 * Get the media words from the interface's list.
325 			 */
326 			ep = LIST_FIRST(&ifm->ifm_list);
327 			for (; ep != NULL && count < ifmr->ifm_count;
328 			    ep = LIST_NEXT(ep, ifm_list), count++)
329 				kptr[count] = ep->ifm_media;
330 
331 			if (ep != NULL)
332 				error = E2BIG;	/* oops! */
333 		} else {
334 			count = usermax;
335 		}
336 
337 		/*
338 		 * We do the copyout on E2BIG, because that's
339 		 * just our way of telling userland that there
340 		 * are more.  This is the behavior I've observed
341 		 * under BSD/OS 3.0
342 		 */
343 		sticky = error;
344 		if ((error == 0 || error == E2BIG) && ifmr->ifm_count != 0) {
345 			error = copyout((caddr_t)kptr,
346 			    (caddr_t)ifmr->ifm_ulist,
347 			    ifmr->ifm_count * sizeof(int));
348 		}
349 
350 		if (error == 0)
351 			error = sticky;
352 
353 		if (ifmr->ifm_count != 0)
354 			kfree(kptr, M_TEMP);
355 
356 		ifmr->ifm_count = count;
357 		break;
358 	}
359 
360 	default:
361 		return (EINVAL);
362 	}
363 
364 	return (error);
365 }
366 
367 /*
368  * Find media entry matching a given ifm word.
369  *
370  */
371 static struct ifmedia_entry *
372 ifmedia_match(struct ifmedia *ifm, int target, int mask)
373 {
374 	struct ifmedia_entry *match, *next;
375 
376 	match = NULL;
377 	mask = ~mask;
378 
379 	LIST_FOREACH(next, &ifm->ifm_list, ifm_list) {
380 		if ((next->ifm_media & mask) == (target & mask)) {
381 #if defined(IFMEDIA_DEBUG) || defined(DIAGNOSTIC)
382 			if (match) {
383 				kprintf("ifmedia_match: multiple match for "
384 				    "0x%x/0x%x\n", target, mask);
385 			}
386 #endif
387 			match = next;
388 		}
389 	}
390 
391 	return match;
392 }
393 
394 struct ifmedia_baudrate ifmedia_baudrate_descriptions[] =
395     IFM_BAUDRATE_DESCRIPTIONS;
396 
397 uint64_t
398 ifmedia_baudrate(int mword)
399 {
400 	int i;
401 
402 	for (i = 0; ifmedia_baudrate_descriptions[i].ifmb_word != 0; i++) {
403 		if ((mword & (IFM_NMASK|IFM_TMASK)) ==
404 		    ifmedia_baudrate_descriptions[i].ifmb_word)
405 			return (ifmedia_baudrate_descriptions[i].ifmb_baudrate);
406 	}
407 
408 	/* Not known. */
409 	return (0);
410 }
411 
412 int
413 ifmedia_str2ethfc(const char *str)
414 {
415 	if (strcmp(str, IFM_ETH_FC_FULL) == 0)
416 		return (IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE);
417 	if (strcmp(str, IFM_ETH_FC_RXPAUSE) == 0)
418 		return IFM_ETH_RXPAUSE;
419 	if (strcmp(str, IFM_ETH_FC_TXPAUSE) == 0)
420 		return IFM_ETH_TXPAUSE;
421 
422 	if (strcmp(str, IFM_ETH_FC_FORCE_FULL) == 0)
423 		return (IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE | IFM_ETH_FORCEPAUSE);
424 	if (strcmp(str, IFM_ETH_FC_FORCE_RXPAUSE) == 0)
425 		return (IFM_ETH_RXPAUSE | IFM_ETH_FORCEPAUSE);
426 	if (strcmp(str, IFM_ETH_FC_FORCE_TXPAUSE) == 0)
427 		return (IFM_ETH_TXPAUSE | IFM_ETH_FORCEPAUSE);
428 	if (strcmp(str, IFM_ETH_FC_FORCE_NONE) == 0)
429 		return IFM_ETH_FORCEPAUSE;
430 
431 	return 0;
432 }
433 
434 #ifdef IFMEDIA_DEBUG
435 struct ifmedia_description ifm_type_descriptions[] =
436     IFM_TYPE_DESCRIPTIONS;
437 
438 struct ifmedia_description ifm_subtype_ethernet_descriptions[] =
439     IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
440 
441 struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] =
442     IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
443 
444 struct ifmedia_description ifm_subtype_ieee80211_descriptions[] =
445     IFM_SUBTYPE_IEEE80211_DESCRIPTIONS;
446 
447 struct ifmedia_description ifm_subtype_ieee80211_option_descriptions[] =
448     IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS;
449 
450 struct ifmedia_description ifm_subtype_shared_descriptions[] =
451     IFM_SUBTYPE_SHARED_DESCRIPTIONS;
452 
453 struct ifmedia_description ifm_shared_option_descriptions[] =
454     IFM_SHARED_OPTION_DESCRIPTIONS;
455 
456 struct ifmedia_type_to_subtype {
457 	struct ifmedia_description *subtypes;
458 	struct ifmedia_description *options;
459 };
460 
461 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
462 struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = {
463 	{
464 	  &ifm_subtype_ethernet_descriptions[0],
465 	  &ifm_subtype_ethernet_option_descriptions[0]
466 	},
467 	{
468 	  &ifm_subtype_ieee80211_descriptions[0],
469 	  &ifm_subtype_ieee80211_option_descriptions[0]
470 	},
471 };
472 
473 /*
474  * print a media word.
475  */
476 static void
477 ifmedia_printword(int ifmw)
478 {
479 	struct ifmedia_description *desc;
480 	struct ifmedia_type_to_subtype *ttos;
481 	int seen_option = 0;
482 
483 	/* Find the top-level interface type. */
484 	for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
485 	    desc->ifmt_string != NULL; desc++, ttos++)
486 		if (IFM_TYPE(ifmw) == desc->ifmt_word)
487 			break;
488 	if (desc->ifmt_string == NULL) {
489 		kprintf("<unknown type>\n");
490 		return;
491 	}
492 	kprintf(desc->ifmt_string);
493 
494 	/*
495 	 * Check for the shared subtype descriptions first, then the
496 	 * type-specific ones.
497 	 */
498 	for (desc = ifm_subtype_shared_descriptions;
499 	    desc->ifmt_string != NULL; desc++)
500 		if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
501 			goto got_subtype;
502 
503 	for (desc = ttos->subtypes; desc->ifmt_string != NULL; desc++)
504 		if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
505 			break;
506 	if (desc->ifmt_string == NULL) {
507 		kprintf(" <unknown subtype>\n");
508 		return;
509 	}
510 
511  got_subtype:
512 	kprintf(" %s", desc->ifmt_string);
513 
514 	/*
515 	 * Look for shared options.
516 	 */
517 	for (desc = ifm_shared_option_descriptions;
518 	    desc->ifmt_string != NULL; desc++) {
519 		if (ifmw & desc->ifmt_word) {
520 			if (seen_option == 0)
521 				kprintf(" <");
522 			kprintf("%s%s", seen_option++ ? "," : "",
523 			    desc->ifmt_string);
524 		}
525 	}
526 
527 	/*
528 	 * Look for subtype-specific options.
529 	 */
530 	for (desc = ttos->options; desc->ifmt_string != NULL; desc++) {
531 		if (ifmw & desc->ifmt_word) {
532 			if (seen_option == 0)
533 				kprintf(" <");
534 			kprintf("%s%s", seen_option++ ? "," : "",
535 			    desc->ifmt_string);
536 		}
537 	}
538 	kprintf("%s\n", seen_option ? ">" : "");
539 }
540 #endif /* IFMEDIA_DEBUG */
541