xref: /illumos-gate/usr/src/uts/common/sys/mac_impl.h (revision bd025ac2b6fbfbc59dfc0bd50a0e56dd56de9229)
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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright 2019 Joyent, Inc.
24  */
25 
26 #ifndef	_SYS_MAC_IMPL_H
27 #define	_SYS_MAC_IMPL_H
28 
29 #include <sys/cpupart.h>
30 #include <sys/modhash.h>
31 #include <sys/mac_client.h>
32 #include <sys/mac_provider.h>
33 #include <sys/note.h>
34 #include <sys/avl.h>
35 #include <net/if.h>
36 #include <sys/mac_flow_impl.h>
37 #include <netinet/ip6.h>
38 
39 #ifdef	__cplusplus
40 extern "C" {
41 #endif
42 
43 /*
44  * This is the first minor number available for MAC provider private
45  * use.  This makes it possible to deliver a driver that is both a MAC
46  * provider and a regular character/block device.  See PSARC 2009/380
47  * for more detail about the construction of such devices.  The value
48  * chosen leaves half of the 32-bit minor numbers (which are really
49  * only 18 bits wide) available for driver private use.  Drivers can
50  * easily identify their private number by the presence of this value
51  * in the bits that make up the minor number, since its just the
52  * highest bit available for such minor numbers.
53  */
54 #define	MAC_PRIVATE_MINOR		((MAXMIN32 + 1) / 2)
55 
56 /*
57  * The maximum minor number that corresponds to a real instance.  This
58  * limits the number of physical ports that a mac provider can offer.
59  * Note that this macro must be synchronized with DLS_MAX_MINOR in
60  * <sys/dls.h>
61  */
62 #define	MAC_MAX_MINOR			1000
63 
64 typedef struct mac_margin_req_s	mac_margin_req_t;
65 
66 struct mac_margin_req_s {
67 	mac_margin_req_t	*mmr_nextp;
68 	uint_t			mmr_ref;
69 	uint32_t		mmr_margin;
70 };
71 
72 typedef struct mac_mtu_req_s mac_mtu_req_t;
73 struct mac_mtu_req_s {
74 	mac_mtu_req_t		*mtr_nextp;
75 	uint_t			mtr_ref;
76 	uint32_t		mtr_mtu;
77 };
78 
79 /* Generic linked chain type */
80 typedef	struct mac_chain_s {
81 	struct mac_chain_s	*next;
82 	void			*item;
83 } mac_chain_t;
84 
85 /*
86  * Generic mac callback list manipulation structures and macros. The mac_cb_t
87  * represents a general callback list element embedded in a particular
88  * data structure such as a mac_notify_cb_t or a mac_promisc_impl_t.
89  * The mac_cb_info_t represents general information about list walkers.
90  * Please see the comments above mac_callback_add for more information.
91  */
92 /* mcb_flags */
93 #define	MCB_CONDEMNED		0x1		/* Logically deleted */
94 #define	MCB_NOTIFY_CB_T		0x2
95 #define	MCB_TX_NOTIFY_CB_T	0x4
96 
97 extern boolean_t	mac_tx_serialize;
98 
99 typedef struct mac_cb_s {
100 	struct mac_cb_s		*mcb_nextp;	/* Linked list of callbacks */
101 	void			*mcb_objp;	/* Ptr to enclosing object  */
102 	size_t			mcb_objsize;	/* Sizeof the enclosing obj */
103 	uint_t			mcb_flags;
104 } mac_cb_t;
105 
106 typedef struct mac_cb_info_s {
107 	kmutex_t	*mcbi_lockp;
108 	kcondvar_t	mcbi_cv;
109 	uint_t		mcbi_del_cnt;		/* Deleted callback cnt */
110 	uint_t		mcbi_walker_cnt;	/* List walker count */
111 	uint_t		mcbi_barrier_cnt;	/* Barrier waiter count */
112 } mac_cb_info_t;
113 
114 typedef struct mac_notify_cb_s {
115 	mac_cb_t	mncb_link;		/* Linked list of callbacks */
116 	mac_notify_t	mncb_fn;		/* callback function */
117 	void		*mncb_arg;		/* callback argument */
118 	struct mac_impl_s *mncb_mip;
119 } mac_notify_cb_t;
120 
121 /*
122  * mac_callback_add(listinfo, listhead, listelement)
123  * mac_callback_remove(listinfo, listhead, listelement)
124  */
125 typedef boolean_t (*mcb_func_t)(mac_cb_info_t *, mac_cb_t **, mac_cb_t *);
126 
127 #define	MAC_CALLBACK_WALKER_INC(mcbi) \
128 	mac_callback_walker_enter(mcbi)
129 
130 #define	MAC_CALLBACK_WALKER_DCR(mcbi, headp) \
131 	mac_callback_walker_exit(mcbi, headp, B_FALSE)
132 
133 #define	MAC_PROMISC_WALKER_INC(mip) \
134 	mac_callback_walker_enter(&(mip)->mi_promisc_cb_info)
135 
136 #define	MAC_PROMISC_WALKER_DCR(mip) \
137 	mac_callback_walker_exit(&(mip)->mi_promisc_cb_info, \
138 	    &(mip)->mi_promisc_list, B_TRUE)
139 
140 typedef struct mactype_s {
141 	const char	*mt_ident;
142 	uint32_t	mt_ref;
143 	uint_t		mt_type;
144 	uint_t		mt_nativetype;
145 	size_t		mt_addr_length;
146 	uint8_t		*mt_brdcst_addr;
147 	mactype_ops_t	mt_ops;
148 	mac_stat_info_t	*mt_stats;	/* array of mac_stat_info_t elements */
149 	size_t		mt_statcount;	/* number of elements in mt_stats */
150 	mac_ndd_mapping_t *mt_mapping;
151 	size_t		mt_mappingcount;
152 } mactype_t;
153 
154 /*
155  * Multiple rings implementation.
156  */
157 typedef	enum {
158 	MAC_GROUP_STATE_UNINIT	= 0,	/* initial state of data structure */
159 	MAC_GROUP_STATE_REGISTERED,	/* hooked with h/w group */
160 	MAC_GROUP_STATE_RESERVED,	/* group is reserved and opened */
161 	MAC_GROUP_STATE_SHARED		/* default group shared among */
162 					/* multiple mac clients */
163 } mac_group_state_t;
164 
165 typedef	struct mac_ring_s mac_ring_t;
166 typedef	struct mac_group_s mac_group_t;
167 
168 /*
169  * Ring data structure for ring control and management.
170  */
171 typedef enum {
172 	MR_FREE,		/* Available for assignment to flows */
173 	MR_NEWLY_ADDED,		/* Just assigned to another group */
174 	MR_INUSE		/* Assigned to an SRS */
175 } mac_ring_state_t;
176 
177 /* mr_flag values */
178 #define	MR_INCIPIENT	0x1
179 #define	MR_CONDEMNED	0x2
180 #define	MR_QUIESCE	0x4
181 
182 typedef struct mac_impl_s mac_impl_t;
183 
184 struct mac_ring_s {
185 	int			mr_index;	/* index in the original list */
186 	mac_ring_type_t		mr_type;	/* ring type */
187 	mac_ring_t		*mr_next;	/* next ring in the chain */
188 	mac_group_handle_t	mr_gh;		/* reference to group */
189 
190 	mac_classify_type_t	mr_classify_type;
191 	struct mac_soft_ring_set_s *mr_srs;	/* associated SRS */
192 	mac_ring_handle_t	mr_prh;	/* associated pseudo ring hdl */
193 
194 	/*
195 	 * Ring passthru callback and arguments. See the
196 	 * MAC_PASSTHRU_CLASSIFIER comment in mac_provider.h.
197 	 */
198 	mac_rx_t		mr_pt_fn;
199 	void			*mr_pt_arg1;
200 	mac_resource_handle_t	mr_pt_arg2;
201 
202 	uint_t			mr_refcnt;	/* Ring references */
203 	/* ring generation no. to guard against drivers using stale rings */
204 	uint64_t		mr_gen_num;
205 
206 	kstat_t			*mr_ksp;	/* ring kstats */
207 	mac_impl_t		*mr_mip;	/* pointer to primary's mip */
208 
209 	kmutex_t		mr_lock;
210 	kcondvar_t		mr_cv;			/* mr_lock */
211 	mac_ring_state_t	mr_state;		/* mr_lock */
212 	uint_t			mr_flag;		/* mr_lock */
213 
214 	mac_ring_info_t		mr_info;	/* driver supplied info */
215 };
216 #define	mr_driver		mr_info.mri_driver
217 #define	mr_start		mr_info.mri_start
218 #define	mr_stop			mr_info.mri_stop
219 #define	mr_stat			mr_info.mri_stat
220 
221 #define	MAC_RING_MARK(mr, flag)		\
222 	(mr)->mr_flag |= flag;
223 
224 #define	MAC_RING_UNMARK(mr, flag)	\
225 	(mr)->mr_flag &= ~flag;
226 
227 /*
228  * Reference hold and release on mac_ring_t 'mr'
229  */
230 #define	MR_REFHOLD_LOCKED(mr)		{		\
231 	ASSERT(MUTEX_HELD(&mr->mr_lock));		\
232 	(mr)->mr_refcnt++;				\
233 }
234 
235 #define	MR_REFRELE(mr)		{			\
236 	mutex_enter(&(mr)->mr_lock);			\
237 	ASSERT((mr)->mr_refcnt != 0);			\
238 	(mr)->mr_refcnt--;				\
239 	if ((mr)->mr_refcnt == 0 &&			\
240 	    ((mr)->mr_flag & (MR_CONDEMNED | MR_QUIESCE))) \
241 		cv_signal(&(mr)->mr_cv);		\
242 	mutex_exit(&(mr)->mr_lock);			\
243 }
244 
245 /*
246  * Used to attach MAC clients to an Rx group. The members are SL
247  * protected.
248  */
249 typedef struct mac_grp_client {
250 	struct mac_grp_client		*mgc_next;
251 	struct mac_client_impl_s	*mgc_client;
252 } mac_grp_client_t;
253 
254 #define	MAC_GROUP_NO_CLIENT(g)	((g)->mrg_clients == NULL)
255 
256 #define	MAC_GROUP_ONLY_CLIENT(g)			\
257 	((((g)->mrg_clients != NULL) &&			\
258 	((g)->mrg_clients->mgc_next == NULL)) ?		\
259 	(g)->mrg_clients->mgc_client : NULL)
260 
261 #define	MAC_GROUP_HW_VLAN(g)				\
262 	(((g) != NULL) &&				\
263 	((g)->mrg_info.mgi_addvlan != NULL) &&		\
264 	((g)->mrg_info.mgi_remvlan != NULL))
265 
266 /*
267  * Common ring group data structure for ring control and management.
268  * The entire structure is SL protected.
269  */
270 struct mac_group_s {
271 	int			mrg_index;	/* index in the list */
272 	mac_ring_type_t		mrg_type;	/* ring type */
273 	mac_group_state_t	mrg_state;	/* state of the group */
274 	mac_group_t		*mrg_next;	/* next group in the chain */
275 	mac_handle_t		mrg_mh;		/* reference to MAC */
276 	mac_ring_t		*mrg_rings;	/* grouped rings */
277 	uint_t			mrg_cur_count;	/* actual size of group */
278 
279 	mac_grp_client_t	*mrg_clients;	/* clients list */
280 
281 	mac_group_info_t	mrg_info;	/* driver supplied info */
282 };
283 
284 #define	mrg_driver		mrg_info.mgi_driver
285 #define	mrg_start		mrg_info.mgi_start
286 #define	mrg_stop		mrg_info.mgi_stop
287 
288 #define	GROUP_INTR_HANDLE(g)		(g)->mrg_info.mgi_intr.mi_handle
289 #define	GROUP_INTR_ENABLE_FUNC(g)	(g)->mrg_info.mgi_intr.mi_enable
290 #define	GROUP_INTR_DISABLE_FUNC(g)	(g)->mrg_info.mgi_intr.mi_disable
291 
292 #define	MAC_RING_TX(mhp, rh, mp, rest) {				\
293 	mac_ring_handle_t mrh = rh;					\
294 	mac_impl_t *mimpl = (mac_impl_t *)mhp;				\
295 	/*								\
296 	 * Send packets through a selected tx ring, or through the	\
297 	 * default handler if there is no selected ring.		\
298 	 */								\
299 	if (mrh == NULL)						\
300 		mrh = mimpl->mi_default_tx_ring;			\
301 	if (mrh == NULL) {						\
302 		rest = mimpl->mi_tx(mimpl->mi_driver, mp);		\
303 	} else {							\
304 		rest = mac_hwring_tx(mrh, mp);				\
305 	}								\
306 }
307 
308 /*
309  * This is the final stop before reaching the underlying driver
310  * or aggregation, so this is where the bridging hook is implemented.
311  * Packets that are bridged will return through mac_bridge_tx(), with
312  * rh nulled out if the bridge chooses to send output on a different
313  * link due to forwarding.
314  */
315 #define	MAC_TX(mip, rh, mp, src_mcip) {					\
316 	mac_ring_handle_t	rhandle = (rh);				\
317 	/*								\
318 	 * If there is a bound Hybrid I/O share, send packets through	\
319 	 * the default tx ring. (When there's a bound Hybrid I/O share,	\
320 	 * the tx rings of this client are mapped in the guest domain	\
321 	 * and not accessible from here.)				\
322 	 */								\
323 	_NOTE(CONSTANTCONDITION)					\
324 	if ((src_mcip)->mci_state_flags & MCIS_SHARE_BOUND)		\
325 		rhandle = (mip)->mi_default_tx_ring;			\
326 	if (mip->mi_promisc_list != NULL)				\
327 		mac_promisc_dispatch(mip, mp, src_mcip);		\
328 	/*								\
329 	 * Grab the proper transmit pointer and handle. Special		\
330 	 * optimization: we can test mi_bridge_link itself atomically,	\
331 	 * and if that indicates no bridge send packets through tx ring.\
332 	 */								\
333 	if (mip->mi_bridge_link == NULL) {				\
334 		MAC_RING_TX(mip, rhandle, mp, mp);			\
335 	} else {							\
336 		mp = mac_bridge_tx(mip, rhandle, mp);			\
337 	}								\
338 }
339 
340 /* mci_tx_flag */
341 #define	MCI_TX_QUIESCE	0x1
342 
343 typedef struct mac_factory_addr_s {
344 	boolean_t		mfa_in_use;
345 	uint8_t			mfa_addr[MAXMACADDRLEN];
346 	struct mac_client_impl_s	*mfa_client;
347 } mac_factory_addr_t;
348 
349 typedef struct mac_mcast_addrs_s {
350 	struct mac_mcast_addrs_s	*mma_next;
351 	uint8_t				mma_addr[MAXMACADDRLEN];
352 	int				mma_ref;
353 } mac_mcast_addrs_t;
354 
355 typedef enum {
356 	MAC_ADDRESS_TYPE_UNICAST_CLASSIFIED = 1,	/* HW classification */
357 	MAC_ADDRESS_TYPE_UNICAST_PROMISC		/* promiscuous mode */
358 } mac_address_type_t;
359 
360 typedef struct mac_vlan_s {
361 	struct mac_vlan_s	*mv_next;
362 	uint16_t		mv_vid;
363 } mac_vlan_t;
364 
365 typedef struct mac_address_s {
366 	mac_address_type_t	ma_type;		/* address type */
367 	int			ma_nusers;		/* num users of addr */
368 	struct mac_address_s	*ma_next;		/* next address */
369 	uint8_t			ma_addr[MAXMACADDRLEN];	/* address value */
370 	size_t			ma_len;			/* address length */
371 	mac_vlan_t		*ma_vlans;		/* VLANs on this addr */
372 	boolean_t		ma_untagged;		/* accept untagged? */
373 	mac_group_t		*ma_group;		/* asscociated group */
374 	mac_impl_t		*ma_mip;		/* MAC handle */
375 } mac_address_t;
376 
377 extern krwlock_t i_mac_impl_lock;
378 extern mod_hash_t *i_mac_impl_hash;
379 extern kmem_cache_t *i_mac_impl_cachep;
380 extern uint_t i_mac_impl_count;
381 
382 /*
383  * Each registered MAC is associated with a mac_impl_t structure. The
384  * structure represents the undelying hardware, in terms of definition,
385  * resources (transmit, receive rings etc.), callback functions etc. It
386  * also holds the table of MAC clients that are configured on the device.
387  * The table is used for classifying incoming packets in software.
388  *
389  * The protection scheme uses 2 elements, a coarse serialization mechanism
390  * called perimeter and a finer traditional lock based scheme. More details
391  * can be found in the big block comment in mac.c.
392  *
393  * The protection scheme for each member of the mac_impl_t is described below.
394  *
395  * Write Once Only (WO): Typically these don't change for the lifetime of the
396  * data structure. For example something in mac_impl_t that stays the same
397  * from mac_register to mac_unregister, or something in a mac_client_impl_t
398  * that stays the same from mac_client_open to mac_client_close.
399  *
400  * Serializer (SL): Protected by the Serializer. All SLOP operations on a
401  * mac endpoint go through the serializer. MTOPs don't care about reading
402  * these fields atomically.
403  *
404  * Lock: Traditional mutex/rw lock. Modify operations still go through the
405  * mac serializer, the lock helps synchronize readers with writers.
406  */
407 struct mac_impl_s {
408 	krwlock_t		mi_rw_lock;
409 	list_node_t		mi_node;
410 	char			mi_name[LIFNAMSIZ];	/* WO */
411 	uint32_t		mi_state_flags;
412 	void			*mi_driver;		/* Driver private, WO */
413 	mac_info_t		mi_info;		/* WO */
414 	mactype_t		*mi_type;		/* WO */
415 	void			*mi_pdata;		/* WO */
416 	size_t			mi_pdata_size;		/* WO */
417 	mac_callbacks_t		*mi_callbacks;		/* WO */
418 	dev_info_t		*mi_dip;		/* WO */
419 	uint32_t		mi_ref;			/* i_mac_impl_lock */
420 	uint_t			mi_active;		/* SL */
421 	link_state_t		mi_linkstate;		/* none */
422 	link_state_t		mi_lowlinkstate;	/* none */
423 	link_state_t		mi_lastlowlinkstate;	/* none */
424 	uint_t			mi_devpromisc;		/* SL */
425 	uint8_t			mi_addr[MAXMACADDRLEN];	/* mi_rw_lock */
426 	uint8_t			mi_dstaddr[MAXMACADDRLEN]; /* mi_rw_lock */
427 	boolean_t		mi_dstaddr_set;
428 
429 	/*
430 	 * The mac perimeter. All client initiated create/modify operations
431 	 * on a mac end point go through this.
432 	 */
433 	kmutex_t		mi_perim_lock;
434 	kthread_t		*mi_perim_owner;	/* mi_perim_lock */
435 	uint_t			mi_perim_ocnt;		/* mi_perim_lock */
436 	kcondvar_t		mi_perim_cv;		/* mi_perim_lock */
437 
438 	/* mac notification callbacks */
439 	kmutex_t		mi_notify_lock;
440 	mac_cb_info_t		mi_notify_cb_info;	/* mi_notify_lock */
441 	mac_cb_t		*mi_notify_cb_list;	/* mi_notify_lock */
442 	kthread_t		*mi_notify_thread;	/* mi_notify_lock */
443 	uint_t			mi_notify_bits;		/* mi_notify_lock */
444 
445 	uint32_t		mi_v12n_level;		/* Virt'ion readiness */
446 
447 	/*
448 	 * RX groups, ring capability
449 	 * Fields of this block are SL protected.
450 	 */
451 	mac_group_type_t	mi_rx_group_type;	/* grouping type */
452 	uint_t			mi_rx_group_count;
453 	mac_group_t		*mi_rx_groups;
454 	mac_group_t		*mi_rx_donor_grp;
455 	uint_t			mi_rxrings_rsvd;
456 	uint_t			mi_rxrings_avail;
457 	uint_t			mi_rxhwclnt_avail;
458 	uint_t			mi_rxhwclnt_used;
459 
460 	mac_capab_rings_t	mi_rx_rings_cap;
461 
462 	/*
463 	 * TX groups and ring capability, SL Protected.
464 	 */
465 	mac_group_type_t	mi_tx_group_type;	/* grouping type */
466 	uint_t			mi_tx_group_count;
467 	uint_t			mi_tx_group_free;
468 	mac_group_t		*mi_tx_groups;
469 	mac_capab_rings_t	mi_tx_rings_cap;
470 	uint_t			mi_txrings_rsvd;
471 	uint_t			mi_txrings_avail;
472 	uint_t			mi_txhwclnt_avail;
473 	uint_t			mi_txhwclnt_used;
474 
475 	mac_ring_handle_t	mi_default_tx_ring;
476 
477 	/*
478 	 * Transceiver capabilities. SL protected.
479 	 */
480 	mac_capab_transceiver_t	mi_transceiver;
481 
482 	/*
483 	 * LED Capability information. SL protected.
484 	 */
485 	mac_led_mode_t		mi_led_modes;
486 	mac_capab_led_t		mi_led;
487 
488 	/*
489 	 * MAC address and VLAN lists. SL protected.
490 	 */
491 	mac_address_t		*mi_addresses;
492 
493 	/*
494 	 * This MAC's table of sub-flows
495 	 */
496 	flow_tab_t		*mi_flow_tab;		/* WO */
497 
498 	kstat_t			*mi_ksp;		/* WO */
499 	uint_t			mi_kstat_count;		/* WO */
500 	uint_t			mi_nactiveclients;	/* SL */
501 
502 	/* for broadcast and multicast support */
503 	struct mac_mcast_addrs_s *mi_mcast_addrs;	/* mi_rw_lock */
504 	struct mac_bcast_grp_s *mi_bcast_grp;		/* mi_rw_lock */
505 	uint_t			mi_bcast_ngrps;		/* mi_rw_lock */
506 
507 	/* list of MAC clients which opened this MAC */
508 	struct mac_client_impl_s *mi_clients_list;	/* mi_rw_lock */
509 	uint_t			mi_nclients;		/* mi_rw_lock */
510 	struct mac_client_impl_s *mi_single_active_client; /* mi_rw_lock */
511 
512 	uint32_t		mi_margin;		/* mi_rw_lock */
513 	uint_t			mi_sdu_min;		/* mi_rw_lock */
514 	uint_t			mi_sdu_max;		/* mi_rw_lock */
515 	uint_t			mi_sdu_multicast;	/* mi_rw_lock */
516 
517 	/*
518 	 * Cache of factory MAC addresses provided by the driver. If
519 	 * the driver doesn't provide multiple factory MAC addresses,
520 	 * the mi_factory_addr is set to NULL, and mi_factory_addr_num
521 	 * is set to zero.
522 	 */
523 	mac_factory_addr_t	*mi_factory_addr;	/* mi_rw_lock */
524 	uint_t			mi_factory_addr_num;	/* mi_rw_lock */
525 
526 	/* for promiscuous mode support */
527 	kmutex_t		mi_promisc_lock;
528 	mac_cb_t		*mi_promisc_list;	/* mi_promisc_lock */
529 	mac_cb_info_t		mi_promisc_cb_info;	/* mi_promisc_lock */
530 
531 	/* cache of rings over this mac_impl */
532 	kmutex_t		mi_ring_lock;
533 	mac_ring_t		*mi_ring_freelist;	/* mi_ring_lock */
534 
535 	/*
536 	 * These are used for caching the properties, if any, for the
537 	 * primary MAC client. If the MAC client is not yet in place
538 	 * when the properties are set then we cache them here to be
539 	 * applied to the MAC client when it is created.
540 	 */
541 	mac_resource_props_t	mi_resource_props;	/* SL */
542 	uint16_t		mi_pvid;		/* SL */
543 
544 	minor_t			mi_minor;		/* WO */
545 	uint32_t		mi_oref;		/* SL */
546 	mac_capab_legacy_t	mi_capab_legacy;	/* WO */
547 	dev_t			mi_phy_dev;		/* WO */
548 
549 	/*
550 	 * List of margin value requests added by mac clients. This list is
551 	 * sorted: the first one has the greatest value.
552 	 */
553 	mac_margin_req_t	*mi_mmrp;
554 	mac_mtu_req_t		*mi_mtrp;
555 	char			**mi_priv_prop;
556 	uint_t			mi_priv_prop_count;
557 
558 	/*
559 	 * Hybrid I/O related definitions.
560 	 */
561 	mac_capab_share_t	mi_share_capab;
562 
563 	/*
564 	 * Bridging hooks and limit values.  Uses mutex and reference counts
565 	 * (bridging only) for data path.  Limits need no synchronization.
566 	 */
567 	mac_handle_t		mi_bridge_link;
568 	kmutex_t		mi_bridge_lock;
569 	uint32_t		mi_llimit;
570 	uint32_t		mi_ldecay;
571 
572 /* This should be the last block in this structure */
573 #ifdef DEBUG
574 #define	MAC_PERIM_STACK_DEPTH	15
575 	int			mi_perim_stack_depth;
576 	pc_t			mi_perim_stack[MAC_PERIM_STACK_DEPTH];
577 #endif
578 };
579 
580 /*
581  * The default TX group is the last one in the list.
582  */
583 #define	MAC_DEFAULT_TX_GROUP(mip)	\
584 	(mip)->mi_tx_groups + (mip)->mi_tx_group_count
585 
586 /*
587  * The default RX group is the first one in the list
588  */
589 #define	MAC_DEFAULT_RX_GROUP(mip)	(mip)->mi_rx_groups
590 
591 /* Reserved RX rings */
592 #define	MAC_RX_RING_RESERVED(m, cnt)	{	\
593 	ASSERT((m)->mi_rxrings_avail >= (cnt));	\
594 	(m)->mi_rxrings_rsvd += (cnt);		\
595 	(m)->mi_rxrings_avail -= (cnt);		\
596 }
597 
598 /* Released RX rings */
599 #define	MAC_RX_RING_RELEASED(m, cnt)	{	\
600 	ASSERT((m)->mi_rxrings_rsvd >= (cnt));	\
601 	(m)->mi_rxrings_rsvd -= (cnt);		\
602 	(m)->mi_rxrings_avail += (cnt);		\
603 }
604 
605 /* Reserved a RX group */
606 #define	MAC_RX_GRP_RESERVED(m)	{		\
607 	ASSERT((m)->mi_rxhwclnt_avail > 0);	\
608 	(m)->mi_rxhwclnt_avail--;		\
609 	(m)->mi_rxhwclnt_used++;		\
610 }
611 
612 /* Released a RX group */
613 #define	MAC_RX_GRP_RELEASED(m)	{		\
614 	ASSERT((m)->mi_rxhwclnt_used > 0);	\
615 	(m)->mi_rxhwclnt_avail++;		\
616 	(m)->mi_rxhwclnt_used--;		\
617 }
618 
619 /* Reserved TX rings */
620 #define	MAC_TX_RING_RESERVED(m, cnt)	{	\
621 	ASSERT((m)->mi_txrings_avail >= (cnt));	\
622 	(m)->mi_txrings_rsvd += (cnt);		\
623 	(m)->mi_txrings_avail -= (cnt);		\
624 }
625 /* Released TX rings */
626 #define	MAC_TX_RING_RELEASED(m, cnt)	{	\
627 	ASSERT((m)->mi_txrings_rsvd >= (cnt));	\
628 	(m)->mi_txrings_rsvd -= (cnt);		\
629 	(m)->mi_txrings_avail += (cnt);		\
630 }
631 
632 /* Reserved a TX group */
633 #define	MAC_TX_GRP_RESERVED(m)	{		\
634 	ASSERT((m)->mi_txhwclnt_avail > 0);	\
635 	(m)->mi_txhwclnt_avail--;		\
636 	(m)->mi_txhwclnt_used++;		\
637 }
638 
639 /* Released a TX group */
640 #define	MAC_TX_GRP_RELEASED(m)	{		\
641 	ASSERT((m)->mi_txhwclnt_used > 0);	\
642 	(m)->mi_txhwclnt_avail++;		\
643 	(m)->mi_txhwclnt_used--;		\
644 }
645 
646 /* for mi_state_flags */
647 #define	MIS_DISABLED		0x0001
648 #define	MIS_IS_VNIC		0x0002
649 #define	MIS_IS_AGGR		0x0004
650 #define	MIS_NOTIFY_DONE		0x0008
651 #define	MIS_EXCLUSIVE		0x0010
652 #define	MIS_EXCLUSIVE_HELD	0x0020
653 #define	MIS_LEGACY		0x0040
654 #define	MIS_NO_ACTIVE		0x0080
655 #define	MIS_POLL_DISABLE	0x0100
656 
657 #define	mi_getstat	mi_callbacks->mc_getstat
658 #define	mi_start	mi_callbacks->mc_start
659 #define	mi_stop		mi_callbacks->mc_stop
660 #define	mi_open		mi_callbacks->mc_open
661 #define	mi_close	mi_callbacks->mc_close
662 #define	mi_setpromisc	mi_callbacks->mc_setpromisc
663 #define	mi_multicst	mi_callbacks->mc_multicst
664 #define	mi_unicst	mi_callbacks->mc_unicst
665 #define	mi_tx		mi_callbacks->mc_tx
666 #define	mi_ioctl	mi_callbacks->mc_ioctl
667 #define	mi_getcapab	mi_callbacks->mc_getcapab
668 
669 typedef struct mac_notify_task_arg {
670 	mac_impl_t		*mnt_mip;
671 	mac_notify_type_t	mnt_type;
672 	mac_ring_t		*mnt_ring;
673 } mac_notify_task_arg_t;
674 
675 /*
676  * The mac_perim_handle_t is an opaque type that encodes the 'mip' pointer
677  * and whether internally a mac_open was done when acquiring the perimeter.
678  */
679 #define	MAC_ENCODE_MPH(mph, mh, need_close)		\
680 	(mph) = (mac_perim_handle_t)((uintptr_t)(mh) | need_close)
681 
682 #define	MAC_DECODE_MPH(mph, mip, need_close) {		\
683 	mip = (mac_impl_t *)(((uintptr_t)mph) & ~0x1);	\
684 	(need_close) = ((uintptr_t)mph & 0x1);		\
685 }
686 
687 /*
688  * Type of property information that can be returned by a driver.
689  * Valid flags of the pr_flags of the mac_prop_info_t data structure.
690  */
691 #define	MAC_PROP_INFO_DEFAULT	0x0001
692 #define	MAC_PROP_INFO_RANGE	0x0002
693 #define	MAC_PROP_INFO_PERM	0x0004
694 
695 /*
696  * Property information. pr_flags is a combination of one of the
697  * MAC_PROP_INFO_* flags, it is reset by the framework before invoking
698  * the driver's prefix_propinfo() entry point.
699  *
700  * Drivers should use MAC_PROP_INFO_SET_*() macros to provide
701  * information about a property.
702  */
703 typedef struct mac_prop_info_state_s {
704 	uint8_t			pr_flags;
705 	uint8_t			pr_perm;
706 	uint8_t			pr_errno;
707 	void			*pr_default;
708 	size_t			pr_default_size;
709 	mac_propval_range_t	*pr_range;
710 	uint_t			pr_range_cur_count;
711 } mac_prop_info_state_t;
712 
713 #define	MAC_PROTECT_ENABLED(mcip, type) \
714 	(((mcip)->mci_flent-> \
715 	fe_resource_props.mrp_mask & MRP_PROTECT) != 0 && \
716 	((mcip)->mci_flent-> \
717 	fe_resource_props.mrp_protect.mp_types & (type)) != 0)
718 
719 typedef struct mac_client_impl_s mac_client_impl_t;
720 
721 extern void	mac_init(void);
722 extern int	mac_fini(void);
723 
724 extern void	mac_ndd_ioctl(mac_impl_t *, queue_t *, mblk_t *);
725 extern boolean_t mac_ip_hdr_length_v6(ip6_t *, uint8_t *, uint16_t *,
726     uint8_t *, ip6_frag_t **);
727 
728 extern mblk_t *mac_copymsgchain_cksum(mblk_t *);
729 extern mblk_t *mac_fix_cksum(mblk_t *);
730 extern void mac_packet_print(mac_handle_t, mblk_t *);
731 extern void mac_rx_deliver(void *, mac_resource_handle_t, mblk_t *,
732     mac_header_info_t *);
733 extern void mac_tx_notify(mac_impl_t *);
734 
735 extern void mac_callback_add(mac_cb_info_t *, mac_cb_t **, mac_cb_t *);
736 extern boolean_t mac_callback_remove(mac_cb_info_t *, mac_cb_t **, mac_cb_t *);
737 extern void mac_callback_remove_wait(mac_cb_info_t *);
738 extern void mac_callback_barrier(mac_cb_info_t *);
739 extern void mac_callback_free(mac_cb_t *);
740 extern void mac_callback_walker_enter(mac_cb_info_t *);
741 extern void mac_callback_walker_exit(mac_cb_info_t *, mac_cb_t **, boolean_t);
742 
743 /* in mac_bcast.c */
744 extern void mac_bcast_init(void);
745 extern void mac_bcast_fini(void);
746 extern mac_impl_t *mac_bcast_grp_mip(void *);
747 extern int mac_bcast_add(mac_client_impl_t *, const uint8_t *, uint16_t,
748     mac_addrtype_t);
749 extern void mac_bcast_delete(mac_client_impl_t *, const uint8_t *, uint16_t);
750 extern void mac_bcast_send(void *, void *, mblk_t *, boolean_t);
751 extern void mac_bcast_grp_free(void *);
752 extern void mac_bcast_refresh(mac_impl_t *, mac_multicst_t, void *,
753     boolean_t);
754 extern void mac_client_bcast_refresh(mac_client_impl_t *, mac_multicst_t,
755     void *, boolean_t);
756 
757 /*
758  * Grouping functions are used internally by MAC layer.
759  */
760 extern int mac_group_addmac(mac_group_t *, const uint8_t *);
761 extern int mac_group_remmac(mac_group_t *, const uint8_t *);
762 extern int mac_group_addvlan(mac_group_t *, uint16_t);
763 extern int mac_group_remvlan(mac_group_t *, uint16_t);
764 extern int mac_rx_group_add_flow(mac_client_impl_t *, flow_entry_t *,
765     mac_group_t *);
766 extern mblk_t *mac_hwring_tx(mac_ring_handle_t, mblk_t *);
767 extern mblk_t *mac_bridge_tx(mac_impl_t *, mac_ring_handle_t, mblk_t *);
768 extern mac_group_t *mac_reserve_rx_group(mac_client_impl_t *, uint8_t *,
769     boolean_t);
770 extern void mac_release_rx_group(mac_client_impl_t *, mac_group_t *);
771 extern int mac_rx_switch_group(mac_client_impl_t *, mac_group_t *,
772     mac_group_t *);
773 extern mac_ring_t *mac_reserve_tx_ring(mac_impl_t *, mac_ring_t *);
774 extern mac_group_t *mac_reserve_tx_group(mac_client_impl_t *, boolean_t);
775 extern void mac_release_tx_group(mac_client_impl_t *, mac_group_t *);
776 extern void mac_tx_switch_group(mac_client_impl_t *, mac_group_t *,
777     mac_group_t *);
778 extern void mac_rx_switch_grp_to_sw(mac_group_t *);
779 
780 /*
781  * MAC address functions are used internally by MAC layer.
782  */
783 extern mac_address_t *mac_find_macaddr(mac_impl_t *, uint8_t *);
784 extern mac_address_t *mac_find_macaddr_vlan(mac_impl_t *, uint8_t *, uint16_t);
785 extern boolean_t mac_check_macaddr_shared(mac_address_t *);
786 extern int mac_update_macaddr(mac_address_t *, uint8_t *);
787 extern void mac_freshen_macaddr(mac_address_t *, uint8_t *);
788 extern void mac_retrieve_macaddr(mac_address_t *, uint8_t *);
789 extern void mac_init_macaddr(mac_impl_t *);
790 extern void mac_fini_macaddr(mac_impl_t *);
791 
792 /*
793  * Flow construction/destruction routines.
794  * Not meant to be used by mac clients.
795  */
796 extern int mac_link_flow_init(mac_client_handle_t, flow_entry_t *);
797 extern void mac_link_flow_clean(mac_client_handle_t, flow_entry_t *);
798 
799 /*
800  * Fanout update routines called when the link speed of the NIC changes
801  * or when a MAC client's share is unbound.
802  */
803 extern void mac_fanout_recompute_client(mac_client_impl_t *, cpupart_t *);
804 extern void mac_fanout_recompute(mac_impl_t *);
805 
806 /*
807  * The following functions are used internally by the MAC layer to
808  * add/remove/update flows associated with a mac_impl_t. They should
809  * never be used directly by MAC clients.
810  */
811 extern int mac_datapath_setup(mac_client_impl_t *, flow_entry_t *, uint32_t);
812 extern void mac_datapath_teardown(mac_client_impl_t *, flow_entry_t *,
813     uint32_t);
814 extern void mac_rx_srs_group_setup(mac_client_impl_t *, flow_entry_t *,
815     uint32_t);
816 extern void mac_tx_srs_group_setup(mac_client_impl_t *, flow_entry_t *,
817     uint32_t);
818 extern void mac_rx_srs_group_teardown(flow_entry_t *, boolean_t);
819 extern void mac_tx_srs_group_teardown(mac_client_impl_t *, flow_entry_t *,
820 	    uint32_t);
821 extern int mac_rx_classify_flow_quiesce(flow_entry_t *, void *);
822 extern int mac_rx_classify_flow_restart(flow_entry_t *, void *);
823 extern void mac_client_quiesce(mac_client_impl_t *);
824 extern void mac_client_restart(mac_client_impl_t *);
825 
826 extern void mac_flow_update_priority(mac_client_impl_t *, flow_entry_t *);
827 
828 extern void mac_flow_rem_subflow(flow_entry_t *);
829 extern void mac_rename_flow(flow_entry_t *, const char *);
830 extern void mac_flow_set_name(flow_entry_t *, const char *);
831 
832 extern mblk_t *mac_add_vlan_tag(mblk_t *, uint_t, uint16_t);
833 extern mblk_t *mac_add_vlan_tag_chain(mblk_t *, uint_t, uint16_t);
834 extern mblk_t *mac_strip_vlan_tag_chain(mblk_t *);
835 extern void mac_pkt_drop(void *, mac_resource_handle_t, mblk_t *, boolean_t);
836 extern mblk_t *mac_rx_flow(mac_handle_t, mac_resource_handle_t, mblk_t *);
837 
838 extern void i_mac_share_alloc(mac_client_impl_t *);
839 extern void i_mac_share_free(mac_client_impl_t *);
840 extern void i_mac_perim_enter(mac_impl_t *);
841 extern void i_mac_perim_exit(mac_impl_t *);
842 extern int i_mac_perim_enter_nowait(mac_impl_t *);
843 extern void i_mac_tx_srs_notify(mac_impl_t *, mac_ring_handle_t);
844 extern int mac_hold(const char *, mac_impl_t **);
845 extern void mac_rele(mac_impl_t *);
846 extern int i_mac_disable(mac_impl_t *);
847 extern void i_mac_notify(mac_impl_t *, mac_notify_type_t);
848 extern void i_mac_notify_exit(mac_impl_t *);
849 extern void mac_rx_group_unmark(mac_group_t *, uint_t);
850 extern void mac_tx_client_flush(mac_client_impl_t *);
851 extern void mac_tx_client_block(mac_client_impl_t *);
852 extern void mac_tx_client_unblock(mac_client_impl_t *);
853 extern void mac_tx_invoke_callbacks(mac_client_impl_t *, mac_tx_cookie_t);
854 extern int i_mac_promisc_set(mac_impl_t *, boolean_t);
855 extern mactype_t *mactype_getplugin(const char *);
856 extern void mac_addr_factory_init(mac_impl_t *);
857 extern void mac_addr_factory_fini(mac_impl_t *);
858 extern void mac_register_priv_prop(mac_impl_t *, char **);
859 extern void mac_unregister_priv_prop(mac_impl_t *);
860 extern int mac_init_rings(mac_impl_t *, mac_ring_type_t);
861 extern void mac_free_rings(mac_impl_t *, mac_ring_type_t);
862 extern void mac_compare_ddi_handle(mac_group_t *, uint_t, mac_ring_t *);
863 
864 extern int mac_start_group(mac_group_t *);
865 extern void mac_stop_group(mac_group_t *);
866 extern int mac_start_ring(mac_ring_t *);
867 extern void mac_stop_ring(mac_ring_t *);
868 extern int mac_add_macaddr_vlan(mac_impl_t *, mac_group_t *, uint8_t *,
869     uint16_t, boolean_t);
870 extern int mac_remove_macaddr_vlan(mac_address_t *, uint16_t);
871 
872 extern void mac_set_group_state(mac_group_t *, mac_group_state_t);
873 extern void mac_group_add_client(mac_group_t *, mac_client_impl_t *);
874 extern void mac_group_remove_client(mac_group_t *, mac_client_impl_t *);
875 
876 extern int i_mac_group_add_ring(mac_group_t *, mac_ring_t *, int);
877 extern void i_mac_group_rem_ring(mac_group_t *, mac_ring_t *, boolean_t);
878 extern int mac_group_ring_modify(mac_client_impl_t *, mac_group_t *,
879     mac_group_t *);
880 extern void mac_poll_state_change(mac_handle_t, boolean_t);
881 
882 extern mac_group_state_t mac_group_next_state(mac_group_t *,
883     mac_client_impl_t **, mac_group_t *, boolean_t);
884 
885 extern mblk_t *mac_protect_check(mac_client_handle_t, mblk_t *);
886 extern int mac_protect_set(mac_client_handle_t, mac_resource_props_t *);
887 extern boolean_t mac_protect_enabled(mac_client_handle_t, uint32_t);
888 extern int mac_protect_validate(mac_resource_props_t *);
889 extern void mac_protect_update(mac_resource_props_t *, mac_resource_props_t *);
890 extern void mac_protect_update_mac_token(mac_client_impl_t *);
891 extern void mac_protect_intercept_dynamic(mac_client_impl_t *, mblk_t *);
892 extern void mac_protect_flush_dynamic(mac_client_impl_t *);
893 extern void mac_protect_cancel_timer(mac_client_impl_t *);
894 extern void mac_protect_init(mac_client_impl_t *);
895 extern void mac_protect_fini(mac_client_impl_t *);
896 
897 extern int mac_set_resources(mac_handle_t, mac_resource_props_t *);
898 extern void mac_get_resources(mac_handle_t, mac_resource_props_t *);
899 extern void mac_get_effective_resources(mac_handle_t, mac_resource_props_t *);
900 extern void mac_set_promisc_filtered(mac_client_handle_t, boolean_t);
901 extern boolean_t mac_get_promisc_filtered(mac_client_handle_t);
902 
903 extern cpupart_t *mac_pset_find(mac_resource_props_t *, boolean_t *);
904 extern void mac_set_pool_effective(boolean_t, cpupart_t *,
905     mac_resource_props_t *, mac_resource_props_t *);
906 extern void mac_set_rings_effective(mac_client_impl_t *);
907 extern mac_client_impl_t *mac_check_primary_relocation(mac_client_impl_t *,
908     boolean_t);
909 
910 /* Global callbacks into the bridging module (when loaded) */
911 extern mac_bridge_tx_t mac_bridge_tx_cb;
912 extern mac_bridge_rx_t mac_bridge_rx_cb;
913 extern mac_bridge_ref_t mac_bridge_ref_cb;
914 extern mac_bridge_ls_t mac_bridge_ls_cb;
915 
916 /*
917  * MAC Transceiver related functions
918  */
919 struct mac_transceiver_info {
920 	boolean_t		mti_present;
921 	boolean_t		mti_usable;
922 };
923 
924 extern void mac_transceiver_init(mac_impl_t *);
925 extern int mac_transceiver_count(mac_handle_t, uint_t *);
926 extern int mac_transceiver_info(mac_handle_t, uint_t, boolean_t *, boolean_t *);
927 extern int mac_transceiver_read(mac_handle_t, uint_t, uint_t, void *, size_t,
928     off_t, size_t *);
929 
930 /*
931  * MAC LED related functions
932  */
933 #define	MAC_LED_ALL	(MAC_LED_DEFAULT | MAC_LED_OFF | MAC_LED_IDENT | \
934 			    MAC_LED_ON)
935 extern void mac_led_init(mac_impl_t *);
936 extern int mac_led_get(mac_handle_t, mac_led_mode_t *, mac_led_mode_t *);
937 extern int mac_led_set(mac_handle_t, mac_led_mode_t);
938 
939 #ifdef	__cplusplus
940 }
941 #endif
942 
943 #endif	/* _SYS_MAC_IMPL_H */
944