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