lib/libexecinfo: Fix static and profiled archives.
[dragonfly.git] / sys / net / if_media.c
blob4be61338818c8676de94d77991b36aef8ae70926
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 $ */
4 /*
5 * Copyright (c) 1997
6 * Jonathan Stone and Jason R. Thorpe. All rights reserved.
8 * This software is derived from information provided by Matt Thomas.
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.
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.
39 * BSD/OS-compatible network interface media selection.
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.
45 * Many thanks to Matt Thomas for providing the information necessary
46 * to implement this interface.
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>
55 #include <net/if.h>
56 #include <net/if_media.h>
59 * Compile-time options:
60 * IFMEDIA_DEBUG:
61 * turn on implementation-level debug kprintfs.
62 * Useful for debugging newly-ported drivers.
65 static struct ifmedia_entry *ifmedia_match(struct ifmedia *ifm,
66 int flags, int mask);
68 #ifdef IFMEDIA_DEBUG
69 int ifmedia_debug = 0;
70 static void ifmedia_printword (int);
71 #endif
74 * Initialize if_media struct for a specific interface instance.
76 void
77 ifmedia_init(struct ifmedia *ifm, int dontcare_mask,
78 ifm_change_cb_t change_callback, ifm_stat_cb_t status_callback)
81 LIST_INIT(&ifm->ifm_list);
82 ifm->ifm_cur = NULL;
83 ifm->ifm_media = IFM_NONE;
84 ifm->ifm_mask = dontcare_mask; /* IF don't-care bits */
85 ifm->ifm_change = change_callback;
86 ifm->ifm_status = status_callback;
89 void
90 ifmedia_removeall(struct ifmedia *ifm)
92 struct ifmedia_entry *entry;
94 while ((entry = LIST_FIRST(&ifm->ifm_list)) != NULL) {
95 LIST_REMOVE(entry, ifm_list);
96 kfree(entry, M_IFADDR);
98 ifm->ifm_cur = NULL;
99 ifm->ifm_media = IFM_NONE;
103 * Add a media configuration to the list of supported media
104 * for a specific interface instance.
106 void
107 ifmedia_add(struct ifmedia *ifm, int mword, int data, void *aux)
109 struct ifmedia_entry *entry;
111 #ifdef IFMEDIA_DEBUG
112 if (ifmedia_debug) {
113 if (ifm == NULL) {
114 kprintf("ifmedia_add: null ifm\n");
115 return;
117 kprintf("Adding entry for ");
118 ifmedia_printword(mword);
120 #endif
122 entry = kmalloc(sizeof(*entry), M_IFADDR, M_INTWAIT);
123 entry->ifm_media = mword;
124 entry->ifm_data = data;
125 entry->ifm_aux = aux;
127 LIST_INSERT_HEAD(&ifm->ifm_list, entry, ifm_list);
131 ifmedia_add_nodup(struct ifmedia *ifm, int mword, int data, void *aux)
133 struct ifmedia_entry *match;
135 match = ifmedia_match(ifm, mword, ifm->ifm_mask);
136 if (match != NULL)
137 return EEXIST;
138 ifmedia_add(ifm, mword, data, aux);
139 return 0;
143 * Add an array of media configurations to the list of
144 * supported media for a specific interface instance.
146 void
147 ifmedia_list_add(struct ifmedia *ifm, struct ifmedia_entry *lp,
148 int count)
150 int i;
152 for (i = 0; i < count; i++)
153 ifmedia_add(ifm, lp[i].ifm_media, lp[i].ifm_data,
154 lp[i].ifm_aux);
158 * Set the default active media.
160 * Called by device-specific code which is assumed to have already
161 * selected the default media in hardware. We do _not_ call the
162 * media-change callback.
164 void
165 ifmedia_set(struct ifmedia *ifm, int target)
167 struct ifmedia_entry *match;
169 match = ifmedia_match(ifm, target, ifm->ifm_mask);
170 if (match == NULL) {
171 kprintf("ifmedia_set: no match for 0x%x/0x%x\n",
172 target, ~ifm->ifm_mask);
173 panic("ifmedia_set");
175 ifm->ifm_cur = match;
176 ifm->ifm_media = target;
178 #ifdef IFMEDIA_DEBUG
179 if (ifmedia_debug) {
180 kprintf("ifmedia_set: target ");
181 ifmedia_printword(target);
182 kprintf("ifmedia_set: setting to ");
183 ifmedia_printword(ifm->ifm_cur->ifm_media);
185 #endif
189 ifmedia_tryset(struct ifmedia *ifm, int target)
191 struct ifmedia_entry *match;
193 match = ifmedia_match(ifm, target, ifm->ifm_mask);
194 if (match == NULL)
195 return ENOENT;
196 ifmedia_set(ifm, target);
197 return 0;
201 * Device-independent media ioctl support function, typically called
202 * from the device network ioctl procedure.
205 ifmedia_ioctl(struct ifnet *ifp, struct ifreq *ifr,
206 struct ifmedia *ifm, u_long cmd)
208 struct ifmedia_entry *match;
209 struct ifmediareq *ifmr = (struct ifmediareq *) ifr;
210 int error = 0, sticky;
212 if (ifp == NULL || ifr == NULL || ifm == NULL)
213 return (EINVAL);
215 ASSERT_IFNET_SERIALIZED_ALL(ifp);
217 switch (cmd) {
219 * Set the current media.
221 case SIOCSIFMEDIA:
223 struct ifmedia_entry *oldentry;
224 int oldmedia;
225 int newmedia = ifr->ifr_media;
227 match = ifmedia_match(ifm, newmedia, ifm->ifm_mask);
228 if (match == NULL) {
229 #ifdef IFMEDIA_DEBUG
230 if (ifmedia_debug) {
231 kprintf(
232 "ifmedia_ioctl: no media found for 0x%x\n",
233 newmedia);
235 #endif
236 return (ENXIO);
240 * If no change, we're done.
241 * XXX Automedia may invole software intervention.
242 * Keep going in case the the connected media changed.
243 * Similarly, if best match changed (kernel debugger?).
245 if ((IFM_SUBTYPE(newmedia) != IFM_AUTO) &&
246 (newmedia == ifm->ifm_media) &&
247 (match == ifm->ifm_cur))
248 return 0;
251 * We found a match, now make the driver switch to it.
252 * Make sure to preserve our old media type in case the
253 * driver can't switch.
255 #ifdef IFMEDIA_DEBUG
256 if (ifmedia_debug) {
257 kprintf("ifmedia_ioctl: switching %s to ",
258 ifp->if_xname);
259 ifmedia_printword(match->ifm_media);
261 #endif
262 oldentry = ifm->ifm_cur;
263 oldmedia = ifm->ifm_media;
264 ifm->ifm_cur = match;
265 ifm->ifm_media = newmedia;
266 error = (*ifm->ifm_change)(ifp);
267 if (error) {
268 ifm->ifm_cur = oldentry;
269 ifm->ifm_media = oldmedia;
271 break;
275 * Get list of available media and current media on interface.
277 case SIOCGIFMEDIA:
279 struct ifmedia_entry *ep;
280 int *kptr, count;
281 int usermax; /* user requested max */
283 kptr = NULL; /* XXX gcc */
286 * NOTE:
287 * ifm_cur may not contain certain media options, e.g.
288 * flow control options, so ifm_media should be used
289 * instead.
291 ifmr->ifm_active = ifmr->ifm_current = ifm->ifm_cur ?
292 ifm->ifm_media : IFM_NONE;
293 ifmr->ifm_mask = ifm->ifm_mask;
294 ifmr->ifm_status = 0;
295 (*ifm->ifm_status)(ifp, ifmr);
297 count = 0;
298 usermax = 0;
301 * If there are more interfaces on the list, count
302 * them. This allows the caller to set ifmr->ifm_count
303 * to 0 on the first call to know how much space to
304 * allocate.
306 LIST_FOREACH(ep, &ifm->ifm_list, ifm_list)
307 usermax++;
310 * Don't allow the user to ask for too many
311 * or a negative number.
313 if (ifmr->ifm_count > usermax)
314 ifmr->ifm_count = usermax;
315 else if (ifmr->ifm_count < 0)
316 return (EINVAL);
318 if (ifmr->ifm_count != 0) {
319 kptr = (int *)kmalloc(ifmr->ifm_count * sizeof(int),
320 M_TEMP, M_WAITOK);
323 * Get the media words from the interface's list.
325 ep = LIST_FIRST(&ifm->ifm_list);
326 for (; ep != NULL && count < ifmr->ifm_count;
327 ep = LIST_NEXT(ep, ifm_list), count++)
328 kptr[count] = ep->ifm_media;
330 if (ep != NULL)
331 error = E2BIG; /* oops! */
332 } else {
333 count = usermax;
337 * We do the copyout on E2BIG, because that's
338 * just our way of telling userland that there
339 * are more. This is the behavior I've observed
340 * under BSD/OS 3.0
342 sticky = error;
343 if ((error == 0 || error == E2BIG) && ifmr->ifm_count != 0) {
344 error = copyout((caddr_t)kptr,
345 (caddr_t)ifmr->ifm_ulist,
346 ifmr->ifm_count * sizeof(int));
349 if (error == 0)
350 error = sticky;
352 if (ifmr->ifm_count != 0)
353 kfree(kptr, M_TEMP);
355 ifmr->ifm_count = count;
356 break;
359 default:
360 return (EINVAL);
363 return (error);
367 * Find media entry matching a given ifm word.
370 static struct ifmedia_entry *
371 ifmedia_match(struct ifmedia *ifm, int target, int mask)
373 struct ifmedia_entry *match, *next;
375 match = NULL;
376 mask = ~mask;
378 LIST_FOREACH(next, &ifm->ifm_list, ifm_list) {
379 if ((next->ifm_media & mask) == (target & mask)) {
380 #if defined(IFMEDIA_DEBUG) || defined(DIAGNOSTIC)
381 if (match) {
382 kprintf("ifmedia_match: multiple match for "
383 "0x%x/0x%x\n", target, mask);
385 #endif
386 match = next;
390 return match;
393 struct ifmedia_baudrate ifmedia_baudrate_descriptions[] =
394 IFM_BAUDRATE_DESCRIPTIONS;
396 uint64_t
397 ifmedia_baudrate(int mword)
399 int i;
401 for (i = 0; ifmedia_baudrate_descriptions[i].ifmb_word != 0; i++) {
402 if ((mword & (IFM_NMASK|IFM_TMASK)) ==
403 ifmedia_baudrate_descriptions[i].ifmb_word)
404 return (ifmedia_baudrate_descriptions[i].ifmb_baudrate);
407 /* Not known. */
408 return (0);
412 ifmedia_str2ethfc(const char *str)
414 if (strcmp(str, IFM_ETH_FC_FULL) == 0)
415 return (IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE);
416 if (strcmp(str, IFM_ETH_FC_RXPAUSE) == 0)
417 return IFM_ETH_RXPAUSE;
418 if (strcmp(str, IFM_ETH_FC_TXPAUSE) == 0)
419 return IFM_ETH_TXPAUSE;
421 if (strcmp(str, IFM_ETH_FC_FORCE_FULL) == 0)
422 return (IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE | IFM_ETH_FORCEPAUSE);
423 if (strcmp(str, IFM_ETH_FC_FORCE_RXPAUSE) == 0)
424 return (IFM_ETH_RXPAUSE | IFM_ETH_FORCEPAUSE);
425 if (strcmp(str, IFM_ETH_FC_FORCE_TXPAUSE) == 0)
426 return (IFM_ETH_TXPAUSE | IFM_ETH_FORCEPAUSE);
427 if (strcmp(str, IFM_ETH_FC_FORCE_NONE) == 0)
428 return IFM_ETH_FORCEPAUSE;
430 return 0;
433 #ifdef IFMEDIA_DEBUG
434 struct ifmedia_description ifm_type_descriptions[] =
435 IFM_TYPE_DESCRIPTIONS;
437 struct ifmedia_description ifm_subtype_ethernet_descriptions[] =
438 IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
440 struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] =
441 IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
443 struct ifmedia_description ifm_subtype_ieee80211_descriptions[] =
444 IFM_SUBTYPE_IEEE80211_DESCRIPTIONS;
446 struct ifmedia_description ifm_subtype_ieee80211_option_descriptions[] =
447 IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS;
449 struct ifmedia_description ifm_subtype_shared_descriptions[] =
450 IFM_SUBTYPE_SHARED_DESCRIPTIONS;
452 struct ifmedia_description ifm_shared_option_descriptions[] =
453 IFM_SHARED_OPTION_DESCRIPTIONS;
455 struct ifmedia_type_to_subtype {
456 struct ifmedia_description *subtypes;
457 struct ifmedia_description *options;
460 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
461 struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = {
463 &ifm_subtype_ethernet_descriptions[0],
464 &ifm_subtype_ethernet_option_descriptions[0]
467 &ifm_subtype_ieee80211_descriptions[0],
468 &ifm_subtype_ieee80211_option_descriptions[0]
473 * print a media word.
475 static void
476 ifmedia_printword(int ifmw)
478 struct ifmedia_description *desc;
479 struct ifmedia_type_to_subtype *ttos;
480 int seen_option = 0;
482 /* Find the top-level interface type. */
483 for (desc = ifm_type_descriptions, ttos = ifmedia_types_to_subtypes;
484 desc->ifmt_string != NULL; desc++, ttos++)
485 if (IFM_TYPE(ifmw) == desc->ifmt_word)
486 break;
487 if (desc->ifmt_string == NULL) {
488 kprintf("<unknown type>\n");
489 return;
491 kprintf(desc->ifmt_string);
494 * Check for the shared subtype descriptions first, then the
495 * type-specific ones.
497 for (desc = ifm_subtype_shared_descriptions;
498 desc->ifmt_string != NULL; desc++)
499 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
500 goto got_subtype;
502 for (desc = ttos->subtypes; desc->ifmt_string != NULL; desc++)
503 if (IFM_SUBTYPE(ifmw) == desc->ifmt_word)
504 break;
505 if (desc->ifmt_string == NULL) {
506 kprintf(" <unknown subtype>\n");
507 return;
510 got_subtype:
511 kprintf(" %s", desc->ifmt_string);
514 * Look for shared options.
516 for (desc = ifm_shared_option_descriptions;
517 desc->ifmt_string != NULL; desc++) {
518 if (ifmw & desc->ifmt_word) {
519 if (seen_option == 0)
520 kprintf(" <");
521 kprintf("%s%s", seen_option++ ? "," : "",
522 desc->ifmt_string);
527 * Look for subtype-specific options.
529 for (desc = ttos->options; desc->ifmt_string != NULL; desc++) {
530 if (ifmw & desc->ifmt_word) {
531 if (seen_option == 0)
532 kprintf(" <");
533 kprintf("%s%s", seen_option++ ? "," : "",
534 desc->ifmt_string);
537 kprintf("%s\n", seen_option ? ">" : "");
539 #endif /* IFMEDIA_DEBUG */