ipfw: Remove unnecessary complexity
[dragonfly.git] / sys / net / if_media.c
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:
61  *      turn on implementation-level debug kprintfs.
62  *      Useful for debugging newly-ported  drivers.
63  */
64
65 static struct ifmedia_entry *ifmedia_match(struct ifmedia *ifm,
66     int flags, int mask);
67
68 #ifdef IFMEDIA_DEBUG
69 int     ifmedia_debug = 0;
70 static  void ifmedia_printword (int);
71 #endif
72
73 /*
74  * Initialize if_media struct for a specific interface instance.
75  */
76 void
77 ifmedia_init(struct ifmedia *ifm, int dontcare_mask,
78              ifm_change_cb_t change_callback, ifm_stat_cb_t status_callback)
79 {
80
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;
87 }
88
89 void
90 ifmedia_removeall(struct ifmedia *ifm)
91 {
92         struct ifmedia_entry *entry;
93
94         while ((entry = LIST_FIRST(&ifm->ifm_list)) != NULL) {
95                 LIST_REMOVE(entry, ifm_list);
96                 kfree(entry, M_IFADDR);
97         }
98         ifm->ifm_cur = NULL;
99         ifm->ifm_media = IFM_NONE;
100 }
101
102 /*
103  * Add a media configuration to the list of supported media
104  * for a specific interface instance.
105  */
106 void
107 ifmedia_add(struct ifmedia *ifm, int mword, int data, void *aux)
108 {
109         struct ifmedia_entry *entry;
110
111 #ifdef IFMEDIA_DEBUG
112         if (ifmedia_debug) {
113                 if (ifm == NULL) {
114                         kprintf("ifmedia_add: null ifm\n");
115                         return;
116                 }
117                 kprintf("Adding entry for ");
118                 ifmedia_printword(mword);
119         }
120 #endif
121
122         entry = kmalloc(sizeof(*entry), M_IFADDR, M_INTWAIT);
123         entry->ifm_media = mword;
124         entry->ifm_data = data;
125         entry->ifm_aux = aux;
126
127         LIST_INSERT_HEAD(&ifm->ifm_list, entry, ifm_list);
128 }
129
130 int
131 ifmedia_add_nodup(struct ifmedia *ifm, int mword, int data, void *aux)
132 {
133         struct ifmedia_entry *match;
134
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;
140 }
141
142 /*
143  * Add an array of media configurations to the list of
144  * supported media for a specific interface instance.
145  */
146 void
147 ifmedia_list_add(struct ifmedia *ifm, struct ifmedia_entry *lp,
148                  int count)
149 {
150         int i;
151
152         for (i = 0; i < count; i++)
153                 ifmedia_add(ifm, lp[i].ifm_media, lp[i].ifm_data,
154                     lp[i].ifm_aux);
155 }
156
157 /*
158  * Set the default active media. 
159  *
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.
163  */
164 void
165 ifmedia_set(struct ifmedia *ifm, int target)
166 {
167         struct ifmedia_entry *match;
168
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");
174         }
175         ifm->ifm_cur = match;
176         ifm->ifm_media = target;
177
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);
184         }
185 #endif
186 }
187
188 int
189 ifmedia_tryset(struct ifmedia *ifm, int target)
190 {
191         struct ifmedia_entry *match;
192
193         match = ifmedia_match(ifm, target, ifm->ifm_mask);
194         if (match == NULL)
195                 return ENOENT;
196         ifmedia_set(ifm, target);
197         return 0;
198 }
199
200 /*
201  * Device-independent media ioctl support function, typically called
202  * from the device network ioctl procedure.
203  */
204 int
205 ifmedia_ioctl(struct ifnet *ifp, struct ifreq *ifr,
206               struct ifmedia *ifm, u_long cmd)
207 {
208         struct ifmedia_entry *match;
209         struct ifmediareq *ifmr = (struct ifmediareq *) ifr;
210         int error = 0, sticky;
211
212         if (ifp == NULL || ifr == NULL || ifm == NULL)
213                 return (EINVAL);
214
215         ASSERT_IFNET_SERIALIZED_ALL(ifp);
216
217         switch (cmd) {
218         /*
219          * Set the current media.
220          */
221         case  SIOCSIFMEDIA:
222         {
223                 struct ifmedia_entry *oldentry;
224                 int oldmedia;
225                 int newmedia = ifr->ifr_media;
226
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);
234                         }
235 #endif
236                         return (ENXIO);
237                 }
238
239                 /*
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?).
244                  */
245                 if ((IFM_SUBTYPE(newmedia) != IFM_AUTO) &&
246                     (newmedia == ifm->ifm_media) &&
247                     (match == ifm->ifm_cur))
248                         return 0;
249
250                 /*
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.
254                  */
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);
260                 }
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;
270                 }
271                 break;
272         }
273
274         /*
275          * Get list of available media and current media on interface.
276          */
277         case  SIOCGIFMEDIA: 
278         {
279                 struct ifmedia_entry *ep;
280                 int *kptr, count;
281                 int usermax;    /* user requested max */
282
283                 kptr = NULL;            /* XXX gcc */
284
285                 /*
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.
290                  */
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);
296
297                 count = 0;
298                 usermax = 0;
299
300                 /*
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.
305                  */
306                 LIST_FOREACH(ep, &ifm->ifm_list, ifm_list)
307                         usermax++;
308
309                 /*
310                  * Don't allow the user to ask for too many
311                  * or a negative number.
312                  */
313                 if (ifmr->ifm_count > usermax)
314                         ifmr->ifm_count = usermax;
315                 else if (ifmr->ifm_count < 0)
316                         return (EINVAL);
317
318                 if (ifmr->ifm_count != 0) {
319                         kptr = (int *)kmalloc(ifmr->ifm_count * sizeof(int),
320                             M_TEMP, M_WAITOK);
321
322                         /*
323                          * Get the media words from the interface's list.
324                          */
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;
329
330                         if (ep != NULL)
331                                 error = E2BIG;  /* oops! */
332                 } else {
333                         count = usermax;
334                 }
335
336                 /*
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
341                  */
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));
347                 }
348
349                 if (error == 0)
350                         error = sticky;
351
352                 if (ifmr->ifm_count != 0)
353                         kfree(kptr, M_TEMP);
354
355                 ifmr->ifm_count = count;
356                 break;
357         }
358
359         default:
360                 return (EINVAL);
361         }
362
363         return (error);
364 }
365
366 /*
367  * Find media entry matching a given ifm word.
368  *
369  */
370 static struct ifmedia_entry *
371 ifmedia_match(struct ifmedia *ifm, int target, int mask)
372 {
373         struct ifmedia_entry *match, *next;
374
375         match = NULL;
376         mask = ~mask;
377
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);
384                         }
385 #endif
386                         match = next;
387                 }
388         }
389
390         return match;
391 }
392
393 struct ifmedia_baudrate ifmedia_baudrate_descriptions[] =
394     IFM_BAUDRATE_DESCRIPTIONS;
395
396 uint64_t
397 ifmedia_baudrate(int mword)
398 {
399         int i;
400
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);
405         }
406
407         /* Not known. */
408         return (0);
409 }
410
411 int
412 ifmedia_str2ethfc(const char *str)
413 {
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;
420
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;
429
430         return 0;
431 }
432
433 #ifdef IFMEDIA_DEBUG
434 struct ifmedia_description ifm_type_descriptions[] =
435     IFM_TYPE_DESCRIPTIONS;
436
437 struct ifmedia_description ifm_subtype_ethernet_descriptions[] =
438     IFM_SUBTYPE_ETHERNET_DESCRIPTIONS;
439
440 struct ifmedia_description ifm_subtype_ethernet_option_descriptions[] =
441     IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS;
442
443 struct ifmedia_description ifm_subtype_ieee80211_descriptions[] =
444     IFM_SUBTYPE_IEEE80211_DESCRIPTIONS;
445
446 struct ifmedia_description ifm_subtype_ieee80211_option_descriptions[] =
447     IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS;
448
449 struct ifmedia_description ifm_subtype_shared_descriptions[] =
450     IFM_SUBTYPE_SHARED_DESCRIPTIONS;
451
452 struct ifmedia_description ifm_shared_option_descriptions[] =
453     IFM_SHARED_OPTION_DESCRIPTIONS;
454
455 struct ifmedia_type_to_subtype {
456         struct ifmedia_description *subtypes;
457         struct ifmedia_description *options;
458 };
459
460 /* must be in the same order as IFM_TYPE_DESCRIPTIONS */
461 struct ifmedia_type_to_subtype ifmedia_types_to_subtypes[] = {
462         {
463           &ifm_subtype_ethernet_descriptions[0],
464           &ifm_subtype_ethernet_option_descriptions[0]
465         },
466         {
467           &ifm_subtype_ieee80211_descriptions[0],
468           &ifm_subtype_ieee80211_option_descriptions[0]
469         },
470 };
471
472 /*
473  * print a media word.
474  */
475 static void
476 ifmedia_printword(int ifmw)
477 {
478         struct ifmedia_description *desc;
479         struct ifmedia_type_to_subtype *ttos;
480         int seen_option = 0;
481
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;
490         }
491         kprintf(desc->ifmt_string);
492
493         /*
494          * Check for the shared subtype descriptions first, then the
495          * type-specific ones.
496          */
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;
501
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;
508         }
509
510  got_subtype:
511         kprintf(" %s", desc->ifmt_string);
512
513         /*
514          * Look for shared options.
515          */
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);
523                 }
524         }
525
526         /*
527          * Look for subtype-specific options.
528          */
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); 
535                 }
536         }
537         kprintf("%s\n", seen_option ? ">" : "");
538 }
539 #endif /* IFMEDIA_DEBUG */