xref: /titanic_51/usr/src/uts/common/sys/mac.h (revision ba2e4443695ee6a6f420a35cd4fc3d3346d22932)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef	_SYS_MAC_H
27 #define	_SYS_MAC_H
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #include <sys/ddi.h>
32 #include <sys/sunddi.h>
33 #include <sys/stream.h>
34 
35 /*
36  * MAC Services Module
37  */
38 
39 #ifdef	__cplusplus
40 extern "C" {
41 #endif
42 
43 /*
44  * MAC Information (text emitted by modinfo(1m))
45  */
46 #define	MAC_INFO	"MAC Services v%I%"
47 
48 /*
49  * MAC version identifier.  This is used by mac_alloc() mac_register() to
50  * verify that incompatible drivers don't register.
51  */
52 #define	MAC_VERSION	0x1
53 
54 /*
55  * MAC-Type version identifier.  This is used by mactype_alloc() and
56  * mactype_register() to verify that incompatible MAC-Type plugins don't
57  * register.
58  */
59 #define	MACTYPE_VERSION	0x1
60 
61 /*
62  * Statistics
63  */
64 
65 #define	XCVR_UNDEFINED		0
66 #define	XCVR_NONE		1
67 #define	XCVR_10			2
68 #define	XCVR_100T4		3
69 #define	XCVR_100X		4
70 #define	XCVR_100T2		5
71 #define	XCVR_1000X		6
72 #define	XCVR_1000T		7
73 
74 typedef enum {
75 	LINK_STATE_UNKNOWN = -1,
76 	LINK_STATE_DOWN,
77 	LINK_STATE_UP
78 } link_state_t;
79 
80 typedef enum {
81 	LINK_DUPLEX_UNKNOWN = 0,
82 	LINK_DUPLEX_HALF,
83 	LINK_DUPLEX_FULL
84 } link_duplex_t;
85 
86 #ifdef	_KERNEL
87 
88 typedef struct mac_stat_info_s {
89 	uint_t		msi_stat;
90 	char		*msi_name;
91 	uint_t		msi_type;	/* as defined in kstat_named_init(9F) */
92 	uint64_t	msi_default;
93 } mac_stat_info_t;
94 
95 /*
96  * There are three ranges of statistics values.  0 to 1 - MAC_STAT_MIN are
97  * interface statistics maintained by the mac module.  MAC_STAT_MIN to 1 -
98  * MACTYPE_STAT_MIN are common MAC statistics defined by the mac module and
99  * maintained by each driver.  MACTYPE_STAT_MIN and above are statistics
100  * defined by MAC-Type plugins and maintained by each driver.
101  */
102 #define	MAC_STAT_MIN		1000
103 #define	MACTYPE_STAT_MIN	2000
104 
105 #define	IS_MAC_STAT(stat)	\
106 	(stat >= MAC_STAT_MIN && stat < MACTYPE_STAT_MIN)
107 #define	IS_MACTYPE_STAT(stat)	(stat >= MACTYPE_STAT_MIN)
108 
109 enum mac_driver_stat {
110 	/* MIB-II stats (RFC 1213 and RFC 1573) */
111 	MAC_STAT_IFSPEED = MAC_STAT_MIN,
112 	MAC_STAT_MULTIRCV,
113 	MAC_STAT_BRDCSTRCV,
114 	MAC_STAT_MULTIXMT,
115 	MAC_STAT_BRDCSTXMT,
116 	MAC_STAT_NORCVBUF,
117 	MAC_STAT_IERRORS,
118 	MAC_STAT_UNKNOWNS,
119 	MAC_STAT_NOXMTBUF,
120 	MAC_STAT_OERRORS,
121 	MAC_STAT_COLLISIONS,
122 	MAC_STAT_RBYTES,
123 	MAC_STAT_IPACKETS,
124 	MAC_STAT_OBYTES,
125 	MAC_STAT_OPACKETS
126 };
127 
128 #define	MAC_NSTAT	(MAC_STAT_OPACKETS - MAC_STAT_IFSPEED + 1)
129 
130 #define	MAC_STAT_ISACOUNTER(_stat) (		\
131 	    (_stat) == MAC_STAT_MULTIRCV ||	\
132 	    (_stat) == MAC_STAT_BRDCSTRCV ||	\
133 	    (_stat) == MAC_STAT_MULTIXMT ||	\
134 	    (_stat) == MAC_STAT_BRDCSTXMT ||	\
135 	    (_stat) == MAC_STAT_NORCVBUF ||	\
136 	    (_stat) == MAC_STAT_IERRORS ||	\
137 	    (_stat) == MAC_STAT_UNKNOWNS ||	\
138 	    (_stat) == MAC_STAT_NOXMTBUF ||	\
139 	    (_stat) == MAC_STAT_OERRORS ||	\
140 	    (_stat) == MAC_STAT_COLLISIONS ||	\
141 	    (_stat) == MAC_STAT_RBYTES ||	\
142 	    (_stat) == MAC_STAT_IPACKETS ||	\
143 	    (_stat) == MAC_STAT_OBYTES ||	\
144 	    (_stat) == MAC_STAT_OPACKETS)
145 
146 /*
147  * Maximum MAC address length
148  */
149 #define	MAXMACADDRLEN	20
150 
151 /*
152  * Immutable information. (This may not be modified after registration).
153  */
154 typedef struct mac_info_s {
155 	uint_t		mi_media;
156 	uint_t		mi_sdu_min;
157 	uint_t		mi_sdu_max;
158 	uint_t		mi_addr_length;
159 	uint8_t		*mi_unicst_addr;
160 	uint8_t		*mi_brdcst_addr;
161 } mac_info_t;
162 
163 /*
164  * MAC layer capabilities.  These capabilities are handled by the drivers'
165  * mc_capab_get() callbacks.  Some capabilities require the driver to fill
166  * in a given data structure, and others are simply boolean capabilities.
167  * Note that capability values must be powers of 2 so that consumers and
168  * providers of this interface can keep track of which capabilities they
169  * care about by keeping a bitfield of these things around somewhere.
170  */
171 typedef enum {
172 	MAC_CAPAB_HCKSUM	= 0x01,	/* data is a uint32_t for the txflags */
173 	MAC_CAPAB_POLL		= 0x02	/* boolean only, no data */
174 	/* add new capabilities here */
175 } mac_capab_t;
176 
177 /*
178  * MAC driver entry point types.
179  */
180 typedef int		(*mac_getstat_t)(void *, uint_t, uint64_t *);
181 typedef	int		(*mac_start_t)(void *);
182 typedef void		(*mac_stop_t)(void *);
183 typedef int		(*mac_setpromisc_t)(void *, boolean_t);
184 typedef int		(*mac_multicst_t)(void *, boolean_t, const uint8_t *);
185 typedef int		(*mac_unicst_t)(void *, const uint8_t *);
186 typedef void		(*mac_ioctl_t)(void *, queue_t *, mblk_t *);
187 typedef void		(*mac_resources_t)(void *);
188 typedef mblk_t		*(*mac_tx_t)(void *, mblk_t *);
189 typedef	boolean_t	(*mac_getcapab_t)(void *, mac_capab_t, void *);
190 
191 /*
192  * Drivers must set all of these callbacks except for mc_resources,
193  * mc_ioctl, and mc_getcapab, which are optional.  If any of these optional
194  * callbacks are set, their appropriate flags must be set in mc_callbacks.
195  * Any future additions to this list must also be accompanied by an
196  * associated mc_callbacks flag so that the framework can grow without
197  * affecting the binary compatibility of the interface.
198  */
199 typedef struct mac_callbacks_s {
200 	uint_t		mc_callbacks;	/* Denotes which callbacks are set */
201 	mac_getstat_t	mc_getstat;	/* Get the value of a statistic */
202 	mac_start_t	mc_start;	/* Start the device */
203 	mac_stop_t	mc_stop;	/* Stop the device */
204 	mac_setpromisc_t mc_setpromisc;	/* Enable or disable promiscuous mode */
205 	mac_multicst_t	mc_multicst;	/* Enable or disable a multicast addr */
206 	mac_unicst_t	mc_unicst;	/* Set the unicast MAC address */
207 	mac_tx_t	mc_tx;		/* Transmit a packet */
208 	mac_resources_t	mc_resources;	/* Get the device resources */
209 	mac_ioctl_t	mc_ioctl;	/* Process an unknown ioctl */
210 	mac_getcapab_t	mc_getcapab;	/* Get capability information */
211 } mac_callbacks_t;
212 
213 /*
214  * Flags for mc_callbacks.  Requiring drivers to set the flags associated
215  * with optional callbacks initialized in the structure allows the mac
216  * module to add optional callbacks in the future without requiring drivers
217  * to recompile.
218  */
219 #define	MC_RESOURCES	0x001
220 #define	MC_IOCTL	0x002
221 #define	MC_GETCAPAB	0x004
222 
223 typedef struct mac_register_s {
224 	uint_t		m_version;	/* set by mac_alloc() */
225 	const char	*m_type_ident;
226 	void		*m_driver;	/* Driver private data */
227 	dev_info_t	*m_dip;
228 	uint_t		m_instance;
229 	uint8_t		*m_src_addr;
230 	uint8_t		*m_dst_addr;
231 	mac_callbacks_t	*m_callbacks;
232 	uint_t		m_min_sdu;
233 	uint_t		m_max_sdu;
234 	void		*m_pdata;
235 	size_t		m_pdata_size;
236 } mac_register_t;
237 
238 /*
239  * Opaque handle types.
240  */
241 typedef	struct mac_t			*mac_handle_t;
242 typedef struct __mac_notify_handle	*mac_notify_handle_t;
243 typedef struct __mac_rx_handle		*mac_rx_handle_t;
244 typedef struct __mac_txloop_handle	*mac_txloop_handle_t;
245 typedef struct __mac_resource_handle	*mac_resource_handle_t;
246 
247 /*
248  * MAC interface callback types.
249  */
250 typedef enum {
251 	MAC_NOTE_LINK,
252 	MAC_NOTE_PROMISC,
253 	MAC_NOTE_UNICST,
254 	MAC_NOTE_TX,
255 	MAC_NOTE_RESOURCE,
256 	MAC_NOTE_DEVPROMISC,
257 	MAC_NOTE_FASTPATH_FLUSH,
258 	MAC_NNOTE	/* must be the last entry */
259 } mac_notify_type_t;
260 
261 typedef void		(*mac_notify_t)(void *, mac_notify_type_t);
262 typedef void		(*mac_rx_t)(void *, mac_resource_handle_t, mblk_t *);
263 typedef void		(*mac_txloop_t)(void *, mblk_t *);
264 typedef void		(*mac_blank_t)(void *, time_t, uint_t);
265 
266 /*
267  * MAC promiscuous types
268  */
269 typedef enum {
270 	MAC_PROMISC = 0x01,		/* MAC instance is promiscuous */
271 	MAC_DEVPROMISC = 0x02		/* Device is promiscuous */
272 } mac_promisc_type_t;
273 
274 /*
275  * MAC resource types
276  */
277 typedef enum {
278 	MAC_RX_FIFO = 1
279 } mac_resource_type_t;
280 
281 typedef struct mac_rx_fifo_s {
282 	mac_resource_type_t	mrf_type;	/* MAC_RX_FIFO */
283 	mac_blank_t		mrf_blank;
284 	void			*mrf_arg;
285 	time_t			mrf_normal_blank_time;
286 	uint_t			mrf_normal_pkt_count;
287 } mac_rx_fifo_t;
288 
289 typedef struct mac_txinfo_s {
290 	mac_tx_t		mt_fn;
291 	void			*mt_arg;
292 } mac_txinfo_t;
293 
294 typedef union mac_resource_u {
295 	mac_resource_type_t	mr_type;
296 	mac_rx_fifo_t		mr_fifo;
297 } mac_resource_t;
298 
299 typedef mac_resource_handle_t	(*mac_resource_add_t)(void *, mac_resource_t *);
300 
301 typedef enum {
302 	MAC_ADDRTYPE_UNICAST,
303 	MAC_ADDRTYPE_MULTICAST,
304 	MAC_ADDRTYPE_BROADCAST
305 } mac_addrtype_t;
306 
307 typedef struct mac_header_info_s {
308 	size_t		mhi_hdrsize;
309 	size_t		mhi_pktsize;
310 	const uint8_t	*mhi_daddr;
311 	const uint8_t	*mhi_saddr;
312 	uint32_t	mhi_origsap;
313 	uint32_t	mhi_bindsap;
314 	mac_addrtype_t	mhi_dsttype;
315 } mac_header_info_t;
316 
317 /*
318  * MAC-Type plugin interfaces
319  */
320 
321 typedef int		(*mtops_addr_verify_t)(const void *, void *);
322 typedef boolean_t	(*mtops_sap_verify_t)(uint32_t, uint32_t *, void *);
323 typedef mblk_t		*(*mtops_header_t)(const void *, const void *,
324     uint32_t, void *, mblk_t *, size_t);
325 typedef int		(*mtops_header_info_t)(mblk_t *, void *,
326     mac_header_info_t *);
327 typedef boolean_t	(*mtops_pdata_verify_t)(void *, size_t);
328 typedef	mblk_t		*(*mtops_header_modify_t)(mblk_t *, void *);
329 
330 typedef struct mactype_ops_s {
331 	uint_t			mtops_ops;
332 	/*
333 	 * mtops_unicst_verify() returns 0 if the given address is a valid
334 	 * unicast address, or a non-zero errno otherwise.
335 	 */
336 	mtops_addr_verify_t	mtops_unicst_verify;
337 	/*
338 	 * mtops_multicst_verify() returns 0 if the given address is a
339 	 * valid multicast address, or a non-zero errno otherwise.  If the
340 	 * media doesn't support multicast, ENOTSUP should be returned (for
341 	 * example).
342 	 */
343 	mtops_addr_verify_t	mtops_multicst_verify;
344 	/*
345 	 * mtops_sap_verify() returns B_TRUE if the given SAP is a valid
346 	 * SAP value, or B_FALSE otherwise.
347 	 */
348 	mtops_sap_verify_t	mtops_sap_verify;
349 	/*
350 	 * mtops_header() is used to allocate and construct a MAC header.
351 	 */
352 	mtops_header_t		mtops_header;
353 	/*
354 	 * mtops_header_info() is used to gather information on a given MAC
355 	 * header.
356 	 */
357 	mtops_header_info_t	mtops_header_info;
358 	/*
359 	 * mtops_pdata_verify() is used to verify the validity of MAC
360 	 * plugin data.  It is called by mac_register() if the driver has
361 	 * supplied MAC plugin data, and also by mac_pdata_update() when
362 	 * drivers update the data.
363 	 */
364 	mtops_pdata_verify_t	mtops_pdata_verify;
365 	/*
366 	 * mtops_header_cook() is an optional callback that converts (or
367 	 * "cooks") the given raw header (as sent by a raw DLPI consumer)
368 	 * into one that is appropriate to send down to the MAC driver.
369 	 * Following the example above, an Ethernet header sent down by a
370 	 * DLPI consumer would be converted to whatever header the MAC
371 	 * driver expects.
372 	 */
373 	mtops_header_modify_t	mtops_header_cook;
374 	/*
375 	 * mtops_header_uncook() is an optional callback that does the
376 	 * opposite of mtops_header_cook().  It "uncooks" a given MAC
377 	 * header (as received from the driver) for consumption by raw DLPI
378 	 * consumers.  For example, for a non-Ethernet plugin that wants
379 	 * raw DLPI consumers to be fooled into thinking that the device
380 	 * provides Ethernet access, this callback would modify the given
381 	 * mblk_t such that the MAC header is converted to an Ethernet
382 	 * header.
383 	 */
384 	mtops_header_modify_t	mtops_header_uncook;
385 } mactype_ops_t;
386 
387 /*
388  * mtops_ops exists for the plugin to enumerate the optional callback
389  * entrypoints it has defined.  This allows the mac module to define
390  * additional plugin entrypoints in mactype_ops_t without breaking backward
391  * compatibility with old plugins.
392  */
393 #define	MTOPS_PDATA_VERIFY	0x001
394 #define	MTOPS_HEADER_COOK	0x002
395 #define	MTOPS_HEADER_UNCOOK	0x004
396 
397 typedef struct mactype_register_s {
398 	uint_t		mtr_version;	/* set by mactype_alloc() */
399 	const char	*mtr_ident;
400 	mactype_ops_t	*mtr_ops;
401 	uint_t		mtr_mactype;
402 	uint_t		mtr_addrlen;
403 	uint8_t		*mtr_brdcst_addr;
404 	mac_stat_info_t	*mtr_stats;
405 	size_t		mtr_statcount;
406 } mactype_register_t;
407 
408 /*
409  * Client interface functions.
410  */
411 extern int			mac_open(const char *, uint_t, mac_handle_t *);
412 extern void			mac_close(mac_handle_t);
413 extern const mac_info_t		*mac_info(mac_handle_t);
414 extern boolean_t		mac_info_get(const char *, mac_info_t *);
415 extern uint64_t			mac_stat_get(mac_handle_t, uint_t);
416 extern int			mac_start(mac_handle_t);
417 extern void			mac_stop(mac_handle_t);
418 extern int			mac_promisc_set(mac_handle_t, boolean_t,
419     mac_promisc_type_t);
420 extern boolean_t		mac_promisc_get(mac_handle_t,
421     mac_promisc_type_t);
422 extern int 			mac_multicst_add(mac_handle_t, const uint8_t *);
423 extern int 			mac_multicst_remove(mac_handle_t,
424     const uint8_t *);
425 extern int			mac_unicst_set(mac_handle_t, const uint8_t *);
426 extern void			mac_unicst_get(mac_handle_t, uint8_t *);
427 extern void			mac_dest_get(mac_handle_t, uint8_t *);
428 extern void			mac_resources(mac_handle_t);
429 extern void			mac_ioctl(mac_handle_t, queue_t *, mblk_t *);
430 extern const mac_txinfo_t	*mac_tx_get(mac_handle_t);
431 extern link_state_t		mac_link_get(mac_handle_t);
432 extern mac_notify_handle_t	mac_notify_add(mac_handle_t, mac_notify_t,
433     void *);
434 extern void			mac_notify_remove(mac_handle_t,
435     mac_notify_handle_t);
436 extern void			mac_notify(mac_handle_t);
437 extern mac_rx_handle_t		mac_rx_add(mac_handle_t, mac_rx_t, void *);
438 extern void			mac_rx_remove(mac_handle_t, mac_rx_handle_t);
439 extern mblk_t			*mac_txloop(void *, mblk_t *);
440 extern mac_txloop_handle_t	mac_txloop_add(mac_handle_t, mac_txloop_t,
441     void *);
442 extern void			mac_txloop_remove(mac_handle_t,
443     mac_txloop_handle_t);
444 extern boolean_t		mac_active_set(mac_handle_t);
445 extern void			mac_active_clear(mac_handle_t);
446 extern void			mac_resource_set(mac_handle_t,
447     mac_resource_add_t, void *);
448 extern dev_info_t		*mac_devinfo_get(mac_handle_t);
449 extern boolean_t		mac_capab_get(mac_handle_t, mac_capab_t,
450     void *);
451 extern boolean_t		mac_sap_verify(mac_handle_t, uint32_t,
452     uint32_t *);
453 extern mblk_t			*mac_header(mac_handle_t, const uint8_t *,
454     uint32_t, mblk_t *, size_t);
455 extern int			mac_header_info(mac_handle_t, mblk_t *,
456     mac_header_info_t *);
457 extern mblk_t			*mac_header_cook(mac_handle_t, mblk_t *);
458 extern mblk_t			*mac_header_uncook(mac_handle_t, mblk_t *);
459 
460 /*
461  * Driver interface functions.
462  */
463 extern mac_register_t		*mac_alloc(uint_t);
464 extern void			mac_free(mac_register_t *);
465 extern int			mac_register(mac_register_t *, mac_handle_t *);
466 extern int  			mac_unregister(mac_handle_t);
467 extern void 			mac_rx(mac_handle_t, mac_resource_handle_t,
468     mblk_t *);
469 extern void 			mac_link_update(mac_handle_t, link_state_t);
470 extern void 			mac_unicst_update(mac_handle_t,
471     const uint8_t *);
472 extern void			mac_tx_update(mac_handle_t);
473 extern void			mac_resource_update(mac_handle_t);
474 extern mac_resource_handle_t	mac_resource_add(mac_handle_t,
475     mac_resource_t *);
476 extern int			mac_pdata_update(mac_handle_t, void *,
477     size_t);
478 extern void			mac_multicst_refresh(mac_handle_t,
479     mac_multicst_t, void *, boolean_t);
480 extern void			mac_unicst_refresh(mac_handle_t, mac_unicst_t,
481     void *);
482 extern void			mac_promisc_refresh(mac_handle_t,
483     mac_setpromisc_t, void *);
484 extern void			mac_init_ops(struct dev_ops *, const char *);
485 extern void			mac_fini_ops(struct dev_ops *);
486 extern mactype_register_t	*mactype_alloc(uint_t);
487 extern void			mactype_free(mactype_register_t *);
488 extern int			mactype_register(mactype_register_t *);
489 extern int			mactype_unregister(const char *);
490 
491 #endif	/* _KERNEL */
492 
493 #ifdef	__cplusplus
494 }
495 #endif
496 
497 #endif /* _SYS_MAC_H */
498