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