1 /*	$NetBSD: if_media.h,v 1.3 1997/03/26 01:19:27 thorpej Exp $	*/
2 /* $FreeBSD: src/sys/net/if_media.h,v 1.17 2002/05/07 18:16:39 imp 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 #ifndef _NET_IF_MEDIA_H_
39 #define _NET_IF_MEDIA_H_
40 
41 /*
42  * Prototypes and definitions for BSD/OS-compatible network interface
43  * media selection.
44  *
45  * Where it is safe to do so, this code strays slightly from the BSD/OS
46  * design.  Software which uses the API (device drivers, basically)
47  * shouldn't notice any difference.
48  *
49  * Many thanks to Matt Thomas for providing the information necessary
50  * to implement this interface.
51  */
52 
53 #ifdef _KERNEL
54 
55 #include <sys/queue.h>
56 
57 /*
58  * Driver callbacks for media status and change requests.
59  */
60 typedef	int (*ifm_change_cb_t)(struct ifnet *ifp);
61 typedef	void (*ifm_stat_cb_t)(struct ifnet *ifp, struct ifmediareq *req);
62 
63 /*
64  * In-kernel representation of a single supported media type.
65  */
66 struct ifmedia_entry {
67 	LIST_ENTRY(ifmedia_entry) ifm_list;
68 	int	ifm_media;	/* description of this media attachment */
69 	int	ifm_data;	/* for driver-specific use */
70 	void	*ifm_aux;	/* for driver-specific use */
71 };
72 
73 /*
74  * One of these goes into a network interface's softc structure.
75  * It is used to keep general media state.
76  */
77 struct ifmedia {
78 	int	ifm_mask;	/* mask of changes we don't care about */
79 	int	ifm_media;	/* current user-set media word */
80 	struct ifmedia_entry *ifm_cur;	/* currently selected media */
81 	LIST_HEAD(, ifmedia_entry) ifm_list; /* list of all supported media */
82 	ifm_change_cb_t	ifm_change;	/* media change driver callback */
83 	ifm_stat_cb_t	ifm_status;	/* media status driver callback */
84 };
85 
86 /* Initialize an interface's struct if_media field. */
87 void	ifmedia_init(struct ifmedia *ifm, int dontcare_mask,
88 	    ifm_change_cb_t change_callback, ifm_stat_cb_t status_callback);
89 
90 /* Remove all mediums from a struct ifmedia.  */
91 void	ifmedia_removeall( struct ifmedia *ifm);
92 
93 /* Add one supported medium to a struct ifmedia. */
94 void	ifmedia_add(struct ifmedia *ifm, int mword, int data, void *aux);
95 
96 /* Add an array (of ifmedia_entry) media to a struct ifmedia. */
97 void	ifmedia_list_add(struct ifmedia *mp, struct ifmedia_entry *lp,
98 	    int count);
99 
100 /* Set default media type on initialization. */
101 void	ifmedia_set(struct ifmedia *ifm, int mword);
102 
103 /* Common ioctl function for getting/setting media, called by driver. */
104 int	ifmedia_ioctl(struct ifnet *ifp, struct ifreq *ifr,
105 	    struct ifmedia *ifm, u_long cmd);
106 
107 #endif /*_KERNEL */
108 
109 /*
110  * if_media Options word:
111  *	Bits	Use
112  *	----	-------
113  *	0-4	Media variant
114  *	5-7	Media type
115  *	8-15	Type specific options
116  *	16-19	RFU
117  *	20-27	Shared (global) options
118  *	28-31	Instance
119  */
120 
121 /*
122  * Ethernet
123  */
124 #define	IFM_ETHER	0x00000020
125 #define	IFM_10_T	3		/* 10BaseT - RJ45 */
126 #define	IFM_10_2	4		/* 10Base2 - Thinnet */
127 #define	IFM_10_5	5		/* 10Base5 - AUI */
128 #define	IFM_100_TX	6		/* 100BaseTX - RJ45 */
129 #define	IFM_100_FX	7		/* 100BaseFX - Fiber */
130 #define	IFM_100_T4	8		/* 100BaseT4 - 4 pair cat 3 */
131 #define	IFM_100_VG	9		/* 100VG-AnyLAN */
132 #define	IFM_100_T2	10		/* 100BaseT2 */
133 #define	IFM_1000_SX	11		/* 1000BaseSX - multi-mode fiber */
134 #define	IFM_10_STP	12		/* 10BaseT over shielded TP */
135 #define	IFM_10_FL	13		/* 10BaseFL - Fiber */
136 #define	IFM_1000_LX	14		/* 1000baseLX - single-mode fiber */
137 #define	IFM_1000_CX	15		/* 1000baseCX - 150ohm STP */
138 #define	IFM_1000_T	16		/* 1000baseT - 4 pair cat 5 */
139 #define	IFM_HPNA_1	17		/* HomePNA 1.0 (1Mb/s) */
140 /* note 31 is the max! */
141 
142 #define	IFM_ETH_MASTER	0x00000100	/* master mode (1000baseT) */
143 
144 /*
145  * Token ring
146  */
147 #define	IFM_TOKEN	0x00000040
148 #define	IFM_TOK_STP4	3		/* Shielded twisted pair 4m - DB9 */
149 #define	IFM_TOK_STP16	4		/* Shielded twisted pair 16m - DB9 */
150 #define	IFM_TOK_UTP4	5		/* Unshielded twisted pair 4m - RJ45 */
151 #define	IFM_TOK_UTP16	6		/* Unshielded twisted pair 16m - RJ45 */
152 #define	IFM_TOK_STP100  7		/* Shielded twisted pair 100m - DB9 */
153 #define	IFM_TOK_UTP100  8		/* Unshielded twisted pair 100m - RJ45 */
154 #define	IFM_TOK_ETR	0x00000200	/* Early token release */
155 #define	IFM_TOK_SRCRT	0x00000400	/* Enable source routing features */
156 #define	IFM_TOK_ALLR	0x00000800	/* All routes / Single route bcast */
157 #define	IFM_TOK_DTR	0x00002000	/* Dedicated token ring */
158 #define	IFM_TOK_CLASSIC	0x00004000	/* Classic token ring */
159 #define	IFM_TOK_AUTO	0x00008000	/* Automatic Dedicate/Classic token ring */
160 
161 /*
162  * FDDI
163  */
164 #define	IFM_FDDI	0x00000060
165 #define	IFM_FDDI_SMF	3		/* Single-mode fiber */
166 #define	IFM_FDDI_MMF	4		/* Multi-mode fiber */
167 #define	IFM_FDDI_UTP	5		/* CDDI / UTP */
168 #define	IFM_FDDI_DA	0x00000100	/* Dual attach / single attach */
169 
170 /*
171  * IEEE 802.11 Wireless
172  */
173 #define	IFM_IEEE80211	0x00000080
174 #define	IFM_IEEE80211_FH1	3	/* Frequency Hopping 1Mbps */
175 #define	IFM_IEEE80211_FH2	4	/* Frequency Hopping 2Mbps */
176 #define	IFM_IEEE80211_DS1	5	/* Direct Sequence 1Mbps */
177 #define	IFM_IEEE80211_DS2	6	/* Direct Sequence 2Mbps */
178 #define	IFM_IEEE80211_DS5	7	/* Direct Sequence 5.5Mbps */
179 #define	IFM_IEEE80211_DS11	8	/* Direct Sequence 11Mbps */
180 #define	IFM_IEEE80211_DS22	9	/* Direct Sequence 22Mbps */
181 #define	IFM_IEEE80211_ADHOC	0x00000100	/* Operate in Adhoc mode */
182 #define	IFM_IEEE80211_HOSTAP	0x00000200	/* Operate in Host AP mode */
183 #define	IFM_IEEE80211_IBSS	0x00000400	/* Operate in IBSS mode */
184 #define	IFM_IEEE80211_IBSSMASTER 0x00000800	/* Operate as an IBSS master */
185 
186 /*
187  * Shared media sub-types
188  */
189 #define	IFM_AUTO	0		/* Autoselect best media */
190 #define	IFM_MANUAL	1		/* Jumper/dipswitch selects media */
191 #define	IFM_NONE	2		/* Deselect all media */
192 
193 /*
194  * Shared options
195  */
196 #define	IFM_FDX		0x00100000	/* Force full duplex */
197 #define	IFM_HDX		0x00200000	/* Force half duplex */
198 #define	IFM_FLAG0	0x01000000	/* Driver defined flag */
199 #define	IFM_FLAG1	0x02000000	/* Driver defined flag */
200 #define	IFM_FLAG2	0x04000000	/* Driver defined flag */
201 #define	IFM_LOOP	0x08000000	/* Put hardware in loopback */
202 
203 /*
204  * Masks
205  */
206 #define	IFM_NMASK	0x000000e0	/* Network type */
207 #define	IFM_TMASK	0x0000001f	/* Media sub-type */
208 #define	IFM_IMASK	0xf0000000	/* Instance */
209 #define	IFM_ISHIFT	28		/* Instance shift */
210 #define	IFM_OMASK	0x0000ff00	/* Type specific options */
211 #define	IFM_GMASK	0x0ff00000	/* Global options */
212 
213 /*
214  * Status bits
215  */
216 #define	IFM_AVALID	0x00000001	/* Active bit valid */
217 #define	IFM_ACTIVE	0x00000002	/* Interface attached to working net */
218 
219 /*
220  * Macros to extract various bits of information from the media word.
221  */
222 #define	IFM_TYPE(x)         ((x) & IFM_NMASK)
223 #define	IFM_SUBTYPE(x)      ((x) & IFM_TMASK)
224 #define	IFM_TYPE_OPTIONS(x) ((x) & IFM_OMASK)
225 #define	IFM_INST(x)         (((x) & IFM_IMASK) >> IFM_ISHIFT)
226 #define	IFM_OPTIONS(x)	((x) & (IFM_OMASK|IFM_GMASK))
227 
228 #define	IFM_INST_MAX	IFM_INST(IFM_IMASK)
229 
230 /*
231  * Macro to create a media word.
232  */
233 #define	IFM_MAKEWORD(type, subtype, options, instance)			\
234 	((type) | (subtype) | (options) | ((instance) << IFM_ISHIFT))
235 
236 /*
237  * NetBSD extension not defined in the BSDI API.  This is used in various
238  * places to get the canonical description for a given type/subtype.
239  *
240  * NOTE: all but the top-level type descriptions must contain NO whitespace!
241  * Otherwise, parsing these in ifconfig(8) would be a nightmare.
242  */
243 struct ifmedia_description {
244 	int	ifmt_word;		/* word value; may be masked */
245 	const char *ifmt_string;	/* description */
246 };
247 
248 #define	IFM_TYPE_DESCRIPTIONS {						\
249 	{ IFM_ETHER,		"Ethernet" },				\
250 	{ IFM_TOKEN,		"Token ring" },				\
251 	{ IFM_FDDI,		"FDDI" },				\
252 	{ IFM_IEEE80211,	"IEEE 802.11 Wireless Ethernet" },	\
253 	{ 0, NULL },							\
254 }
255 
256 #define	IFM_SUBTYPE_ETHERNET_DESCRIPTIONS {				\
257 	{ IFM_10_T,	"10baseT/UTP" },				\
258 	{ IFM_10_2,	"10base2/BNC" },				\
259 	{ IFM_10_5,	"10base5/AUI" },				\
260 	{ IFM_100_TX,	"100baseTX" },					\
261 	{ IFM_100_FX,	"100baseFX" },					\
262 	{ IFM_100_T4,	"100baseT4" },					\
263 	{ IFM_100_VG,	"100baseVG" },					\
264 	{ IFM_100_T2,	"100baseT2" },					\
265 	{ IFM_10_STP,	"10baseSTP" },					\
266 	{ IFM_10_FL,	"10baseFL" },					\
267 	{ IFM_1000_SX,	"1000baseSX" },					\
268 	{ IFM_1000_LX,	"1000baseLX" },					\
269 	{ IFM_1000_CX,	"1000baseCX" },					\
270 	{ IFM_1000_T,	"1000baseTX" },					\
271 	{ IFM_1000_T,	"1000baseT" },					\
272 	{ IFM_HPNA_1,	"homePNA" },					\
273 	{ 0, NULL },							\
274 }
275 
276 #define	IFM_SUBTYPE_ETHERNET_ALIASES {					\
277 	{ IFM_10_T,	"UTP" },					\
278 	{ IFM_10_T,	"10UTP" },					\
279 	{ IFM_10_2,	"BNC" },					\
280 	{ IFM_10_2,	"10BNC" },					\
281 	{ IFM_10_5,	"AUI" },					\
282 	{ IFM_10_5,	"10AUI" },					\
283 	{ IFM_100_TX,	"100TX" },					\
284 	{ IFM_100_T4,	"100T4" },					\
285 	{ IFM_100_VG,	"100VG" },					\
286 	{ IFM_100_T2,	"100T2" },					\
287 	{ IFM_10_STP,	"10STP" },					\
288 	{ IFM_10_FL,	"10FL" },					\
289 	{ IFM_1000_SX,	"1000SX" },					\
290 	{ IFM_1000_LX,	"1000LX" },					\
291 	{ IFM_1000_CX,	"1000CX" },					\
292 	{ IFM_1000_T,	"1000TX" },					\
293 	{ IFM_1000_T,	"1000T" },					\
294 	{ 0, NULL },							\
295 }
296 
297 #define	IFM_SUBTYPE_ETHERNET_OPTION_DESCRIPTIONS {			\
298 	{ 0, NULL },							\
299 }
300 
301 #define	IFM_SUBTYPE_TOKENRING_DESCRIPTIONS {				\
302 	{ IFM_TOK_STP4,	"DB9/4Mbit" },					\
303 	{ IFM_TOK_STP16, "DB9/16Mbit" },				\
304 	{ IFM_TOK_UTP4,	"UTP/4Mbit" },					\
305 	{ IFM_TOK_UTP16, "UTP/16Mbit" },				\
306 	{ IFM_TOK_STP100, "STP/100Mbit" },				\
307 	{ IFM_TOK_UTP100, "UTP/100Mbit" },				\
308 	{ 0, NULL },							\
309 }
310 
311 #define	IFM_SUBTYPE_TOKENRING_ALIASES {					\
312 	{ IFM_TOK_STP4,	"4STP" },					\
313 	{ IFM_TOK_STP16, "16STP" },					\
314 	{ IFM_TOK_UTP4,	"4UTP" },					\
315 	{ IFM_TOK_UTP16, "16UTP" },					\
316 	{ IFM_TOK_STP100, "100STP" },					\
317 	{ IFM_TOK_UTP100, "100UTP" },					\
318 	{ 0, NULL },							\
319 }
320 
321 #define	IFM_SUBTYPE_TOKENRING_OPTION_DESCRIPTIONS {			\
322 	{ IFM_TOK_ETR,	"EarlyTokenRelease" },				\
323 	{ IFM_TOK_SRCRT, "SourceRouting" },				\
324 	{ IFM_TOK_ALLR,	"AllRoutes" },					\
325 	{ IFM_TOK_DTR,	"Dedicated" },					\
326 	{ IFM_TOK_CLASSIC,"Classic" },					\
327 	{ IFM_TOK_AUTO,	" " },						\
328 	{ 0, NULL },							\
329 }
330 
331 #define	IFM_SUBTYPE_FDDI_DESCRIPTIONS {					\
332 	{ IFM_FDDI_SMF, "Single-mode" },				\
333 	{ IFM_FDDI_MMF, "Multi-mode" },					\
334 	{ IFM_FDDI_UTP, "UTP" },					\
335 	{ 0, NULL },							\
336 }
337 
338 #define	IFM_SUBTYPE_FDDI_ALIASES {					\
339 	{ IFM_FDDI_SMF,	"SMF" },					\
340 	{ IFM_FDDI_MMF,	"MMF" },					\
341 	{ IFM_FDDI_UTP,	"CDDI" },					\
342 	{ 0, NULL },							\
343 }
344 
345 #define	IFM_SUBTYPE_FDDI_OPTION_DESCRIPTIONS {				\
346 	{ IFM_FDDI_DA, "Dual-attach" },					\
347 	{ 0, NULL },							\
348 }
349 
350 #define	IFM_SUBTYPE_IEEE80211_DESCRIPTIONS {				\
351 	{ IFM_IEEE80211_FH1, "FH/1Mbps" },				\
352 	{ IFM_IEEE80211_FH2, "FH/2Mbps" },				\
353 	{ IFM_IEEE80211_DS1, "DS/1Mbps" },				\
354 	{ IFM_IEEE80211_DS2, "DS/2Mbps" },				\
355 	{ IFM_IEEE80211_DS5, "DS/5.5Mbps" },				\
356 	{ IFM_IEEE80211_DS11, "DS/11Mbps" },				\
357 	{ IFM_IEEE80211_DS22, "DS/22Mbps" },				\
358 	{ 0, NULL },							\
359 }
360 
361 #define	IFM_SUBTYPE_IEEE80211_ALIASES {					\
362 	{ IFM_IEEE80211_FH1, "FH1" },					\
363 	{ IFM_IEEE80211_FH2, "FH2" },					\
364 	{ IFM_IEEE80211_FH1, "FrequencyHopping/1Mbps" },		\
365 	{ IFM_IEEE80211_FH2, "FrequencyHopping/2Mbps" },		\
366 	{ IFM_IEEE80211_DS1, "DS1" },					\
367 	{ IFM_IEEE80211_DS2, "DS2" },					\
368 	{ IFM_IEEE80211_DS5, "DS5.5" },					\
369 	{ IFM_IEEE80211_DS11, "DS11" },					\
370 	{ IFM_IEEE80211_DS22, "DS22" },					\
371 	{ IFM_IEEE80211_DS1, "DirectSequence/1Mbps" },			\
372 	{ IFM_IEEE80211_DS2, "DirectSequence/2Mbps" },			\
373 	{ IFM_IEEE80211_DS5, "DirectSequence/5.5Mbps" },		\
374 	{ IFM_IEEE80211_DS11, "DirectSequence/11Mbps" },		\
375 	{ IFM_IEEE80211_DS22, "DirectSequence/22Mbps" },		\
376 	{ 0, NULL },							\
377 }
378 
379 #define	IFM_SUBTYPE_IEEE80211_OPTION_DESCRIPTIONS {			\
380 	{ IFM_IEEE80211_ADHOC, "adhoc" },				\
381 	{ IFM_IEEE80211_HOSTAP, "hostap" },				\
382 	{ IFM_IEEE80211_IBSS, "ibss" },					\
383 	{ IFM_IEEE80211_IBSSMASTER, "ibss-master" },			\
384 	{ 0, NULL },							\
385 }
386 
387 #define	IFM_SUBTYPE_SHARED_DESCRIPTIONS {				\
388 	{ IFM_AUTO,	"autoselect" },					\
389 	{ IFM_MANUAL,	"manual" },					\
390 	{ IFM_NONE,	"none" },					\
391 	{ 0, NULL },							\
392 }
393 
394 #define	IFM_SUBTYPE_SHARED_ALIASES {					\
395 	{ IFM_AUTO,	"auto" },					\
396 	{ 0, NULL },							\
397 }
398 
399 #define	IFM_SHARED_OPTION_DESCRIPTIONS {				\
400 	{ IFM_FDX,	"full-duplex" },				\
401 	{ IFM_HDX,	"half-duplex" },				\
402 	{ IFM_FLAG0,	"flag0" },					\
403 	{ IFM_FLAG1,	"flag1" },					\
404 	{ IFM_FLAG2,	"flag2" },					\
405 	{ IFM_LOOP,	"hw-loopback" },				\
406 	{ 0, NULL },							\
407 }
408 
409 #endif	/* _NET_IF_MEDIA_H_ */
410