xref: /illumos-gate/usr/src/uts/common/io/mac/mac.c (revision 84de666edc7f7d835057ae4807a387447c086bcf)
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 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2020 Joyent, Inc.
25  * Copyright 2015 Garrett D'Amore <garrett@damore.org>
26  */
27 
28 /*
29  * MAC Services Module
30  *
31  * The GLDv3 framework locking -  The MAC layer
32  * --------------------------------------------
33  *
34  * The MAC layer is central to the GLD framework and can provide the locking
35  * framework needed for itself and for the use of MAC clients. MAC end points
36  * are fairly disjoint and don't share a lot of state. So a coarse grained
37  * multi-threading scheme is to single thread all create/modify/delete or set
38  * type of control operations on a per mac end point while allowing data threads
39  * concurrently.
40  *
41  * Control operations (set) that modify a mac end point are always serialized on
42  * a per mac end point basis, We have at most 1 such thread per mac end point
43  * at a time.
44  *
45  * All other operations that are not serialized are essentially multi-threaded.
46  * For example a control operation (get) like getting statistics which may not
47  * care about reading values atomically or data threads sending or receiving
48  * data. Mostly these type of operations don't modify the control state. Any
49  * state these operations care about are protected using traditional locks.
50  *
51  * The perimeter only serializes serial operations. It does not imply there
52  * aren't any other concurrent operations. However a serialized operation may
53  * sometimes need to make sure it is the only thread. In this case it needs
54  * to use reference counting mechanisms to cv_wait until any current data
55  * threads are done.
56  *
57  * The mac layer itself does not hold any locks across a call to another layer.
58  * The perimeter is however held across a down call to the driver to make the
59  * whole control operation atomic with respect to other control operations.
60  * Also the data path and get type control operations may proceed concurrently.
61  * These operations synchronize with the single serial operation on a given mac
62  * end point using regular locks. The perimeter ensures that conflicting
63  * operations like say a mac_multicast_add and a mac_multicast_remove on the
64  * same mac end point don't interfere with each other and also ensures that the
65  * changes in the mac layer and the call to the underlying driver to say add a
66  * multicast address are done atomically without interference from a thread
67  * trying to delete the same address.
68  *
69  * For example, consider
70  * mac_multicst_add()
71  * {
72  *	mac_perimeter_enter();	serialize all control operations
73  *
74  *	grab list lock		protect against access by data threads
75  *	add to list
76  *	drop list lock
77  *
78  *	call driver's mi_multicst
79  *
80  *	mac_perimeter_exit();
81  * }
82  *
83  * To lessen the number of serialization locks and simplify the lock hierarchy,
84  * we serialize all the control operations on a per mac end point by using a
85  * single serialization lock called the perimeter. We allow recursive entry into
86  * the perimeter to facilitate use of this mechanism by both the mac client and
87  * the MAC layer itself.
88  *
89  * MAC client means an entity that does an operation on a mac handle
90  * obtained from a mac_open/mac_client_open. Similarly MAC driver means
91  * an entity that does an operation on a mac handle obtained from a
92  * mac_register. An entity could be both client and driver but on different
93  * handles eg. aggr. and should only make the corresponding mac interface calls
94  * i.e. mac driver interface or mac client interface as appropriate for that
95  * mac handle.
96  *
97  * General rules.
98  * -------------
99  *
100  * R1. The lock order of upcall threads is natually opposite to downcall
101  * threads. Hence upcalls must not hold any locks across layers for fear of
102  * recursive lock enter and lock order violation. This applies to all layers.
103  *
104  * R2. The perimeter is just another lock. Since it is held in the down
105  * direction, acquiring the perimeter in an upcall is prohibited as it would
106  * cause a deadlock. This applies to all layers.
107  *
108  * Note that upcalls that need to grab the mac perimeter (for example
109  * mac_notify upcalls) can still achieve that by posting the request to a
110  * thread, which can then grab all the required perimeters and locks in the
111  * right global order. Note that in the above example the mac layer iself
112  * won't grab the mac perimeter in the mac_notify upcall, instead the upcall
113  * to the client must do that. Please see the aggr code for an example.
114  *
115  * MAC client rules
116  * ----------------
117  *
118  * R3. A MAC client may use the MAC provided perimeter facility to serialize
119  * control operations on a per mac end point. It does this by by acquring
120  * and holding the perimeter across a sequence of calls to the mac layer.
121  * This ensures atomicity across the entire block of mac calls. In this
122  * model the MAC client must not hold any client locks across the calls to
123  * the mac layer. This model is the preferred solution.
124  *
125  * R4. However if a MAC client has a lot of global state across all mac end
126  * points the per mac end point serialization may not be sufficient. In this
127  * case the client may choose to use global locks or use its own serialization.
128  * To avoid deadlocks, these client layer locks held across the mac calls
129  * in the control path must never be acquired by the data path for the reason
130  * mentioned below.
131  *
132  * (Assume that a control operation that holds a client lock blocks in the
133  * mac layer waiting for upcall reference counts to drop to zero. If an upcall
134  * data thread that holds this reference count, tries to acquire the same
135  * client lock subsequently it will deadlock).
136  *
137  * A MAC client may follow either the R3 model or the R4 model, but can't
138  * mix both. In the former, the hierarchy is Perim -> client locks, but in
139  * the latter it is client locks -> Perim.
140  *
141  * R5. MAC clients must make MAC calls (excluding data calls) in a cv_wait'able
142  * context since they may block while trying to acquire the perimeter.
143  * In addition some calls may block waiting for upcall refcnts to come down to
144  * zero.
145  *
146  * R6. MAC clients must make sure that they are single threaded and all threads
147  * from the top (in particular data threads) have finished before calling
148  * mac_client_close. The MAC framework does not track the number of client
149  * threads using the mac client handle. Also mac clients must make sure
150  * they have undone all the control operations before calling mac_client_close.
151  * For example mac_unicast_remove/mac_multicast_remove to undo the corresponding
152  * mac_unicast_add/mac_multicast_add.
153  *
154  * MAC framework rules
155  * -------------------
156  *
157  * R7. The mac layer itself must not hold any mac layer locks (except the mac
158  * perimeter) across a call to any other layer from the mac layer. The call to
159  * any other layer could be via mi_* entry points, classifier entry points into
160  * the driver or via upcall pointers into layers above. The mac perimeter may
161  * be acquired or held only in the down direction, for e.g. when calling into
162  * a mi_* driver enty point to provide atomicity of the operation.
163  *
164  * R8. Since it is not guaranteed (see R14) that drivers won't hold locks across
165  * mac driver interfaces, the MAC layer must provide a cut out for control
166  * interfaces like upcall notifications and start them in a separate thread.
167  *
168  * R9. Note that locking order also implies a plumbing order. For example
169  * VNICs are allowed to be created over aggrs, but not vice-versa. An attempt
170  * to plumb in any other order must be failed at mac_open time, otherwise it
171  * could lead to deadlocks due to inverse locking order.
172  *
173  * R10. MAC driver interfaces must not block since the driver could call them
174  * in interrupt context.
175  *
176  * R11. Walkers must preferably not hold any locks while calling walker
177  * callbacks. Instead these can operate on reference counts. In simple
178  * callbacks it may be ok to hold a lock and call the callbacks, but this is
179  * harder to maintain in the general case of arbitrary callbacks.
180  *
181  * R12. The MAC layer must protect upcall notification callbacks using reference
182  * counts rather than holding locks across the callbacks.
183  *
184  * R13. Given the variety of drivers, it is preferable if the MAC layer can make
185  * sure that any pointers (such as mac ring pointers) it passes to the driver
186  * remain valid until mac unregister time. Currently the mac layer achieves
187  * this by using generation numbers for rings and freeing the mac rings only
188  * at unregister time.  The MAC layer must provide a layer of indirection and
189  * must not expose underlying driver rings or driver data structures/pointers
190  * directly to MAC clients.
191  *
192  * MAC driver rules
193  * ----------------
194  *
195  * R14. It would be preferable if MAC drivers don't hold any locks across any
196  * mac call. However at a minimum they must not hold any locks across data
197  * upcalls. They must also make sure that all references to mac data structures
198  * are cleaned up and that it is single threaded at mac_unregister time.
199  *
200  * R15. MAC driver interfaces don't block and so the action may be done
201  * asynchronously in a separate thread as for example handling notifications.
202  * The driver must not assume that the action is complete when the call
203  * returns.
204  *
205  * R16. Drivers must maintain a generation number per Rx ring, and pass it
206  * back to mac_rx_ring(); They are expected to increment the generation
207  * number whenever the ring's stop routine is invoked.
208  * See comments in mac_rx_ring();
209  *
210  * R17 Similarly mi_stop is another synchronization point and the driver must
211  * ensure that all upcalls are done and there won't be any future upcall
212  * before returning from mi_stop.
213  *
214  * R18. The driver may assume that all set/modify control operations via
215  * the mi_* entry points are single threaded on a per mac end point.
216  *
217  * Lock and Perimeter hierarchy scenarios
218  * ---------------------------------------
219  *
220  * i_mac_impl_lock -> mi_rw_lock -> srs_lock -> s_ring_lock[i_mac_tx_srs_notify]
221  *
222  * ft_lock -> fe_lock [mac_flow_lookup]
223  *
224  * mi_rw_lock -> fe_lock [mac_bcast_send]
225  *
226  * srs_lock -> mac_bw_lock [mac_rx_srs_drain_bw]
227  *
228  * cpu_lock -> mac_srs_g_lock -> srs_lock -> s_ring_lock [mac_walk_srs_and_bind]
229  *
230  * i_dls_devnet_lock -> mac layer locks [dls_devnet_rename]
231  *
232  * Perimeters are ordered P1 -> P2 -> P3 from top to bottom in order of mac
233  * client to driver. In the case of clients that explictly use the mac provided
234  * perimeter mechanism for its serialization, the hierarchy is
235  * Perimeter -> mac layer locks, since the client never holds any locks across
236  * the mac calls. In the case of clients that use its own locks the hierarchy
237  * is Client locks -> Mac Perim -> Mac layer locks. The client never explicitly
238  * calls mac_perim_enter/exit in this case.
239  *
240  * Subflow creation rules
241  * ---------------------------
242  * o In case of a user specified cpulist present on underlying link and flows,
243  * the flows cpulist must be a subset of the underlying link.
244  * o In case of a user specified fanout mode present on link and flow, the
245  * subflow fanout count has to be less than or equal to that of the
246  * underlying link. The cpu-bindings for the subflows will be a subset of
247  * the underlying link.
248  * o In case if no cpulist specified on both underlying link and flow, the
249  * underlying link relies on a  MAC tunable to provide out of box fanout.
250  * The subflow will have no cpulist (the subflow will be unbound)
251  * o In case if no cpulist is specified on the underlying link, a subflow can
252  * carry  either a user-specified cpulist or fanout count. The cpu-bindings
253  * for the subflow will not adhere to restriction that they need to be subset
254  * of the underlying link.
255  * o In case where the underlying link is carrying either a user specified
256  * cpulist or fanout mode and for a unspecified subflow, the subflow will be
257  * created unbound.
258  * o While creating unbound subflows, bandwidth mode changes attempt to
259  * figure a right fanout count. In such cases the fanout count will override
260  * the unbound cpu-binding behavior.
261  * o In addition to this, while cycling between flow and link properties, we
262  * impose a restriction that if a link property has a subflow with
263  * user-specified attributes, we will not allow changing the link property.
264  * The administrator needs to reset all the user specified properties for the
265  * subflows before attempting a link property change.
266  * Some of the above rules can be overridden by specifying additional command
267  * line options while creating or modifying link or subflow properties.
268  *
269  * Datapath
270  * --------
271  *
272  * For information on the datapath, the world of soft rings, hardware rings, how
273  * it is structured, and the path of an mblk_t between a driver and a mac
274  * client, see mac_sched.c.
275  */
276 
277 #include <sys/types.h>
278 #include <sys/conf.h>
279 #include <sys/id_space.h>
280 #include <sys/esunddi.h>
281 #include <sys/stat.h>
282 #include <sys/mkdev.h>
283 #include <sys/stream.h>
284 #include <sys/strsun.h>
285 #include <sys/strsubr.h>
286 #include <sys/dlpi.h>
287 #include <sys/list.h>
288 #include <sys/modhash.h>
289 #include <sys/mac_provider.h>
290 #include <sys/mac_client_impl.h>
291 #include <sys/mac_soft_ring.h>
292 #include <sys/mac_stat.h>
293 #include <sys/mac_impl.h>
294 #include <sys/mac.h>
295 #include <sys/dls.h>
296 #include <sys/dld.h>
297 #include <sys/modctl.h>
298 #include <sys/fs/dv_node.h>
299 #include <sys/thread.h>
300 #include <sys/proc.h>
301 #include <sys/callb.h>
302 #include <sys/cpuvar.h>
303 #include <sys/atomic.h>
304 #include <sys/bitmap.h>
305 #include <sys/sdt.h>
306 #include <sys/mac_flow.h>
307 #include <sys/ddi_intr_impl.h>
308 #include <sys/disp.h>
309 #include <sys/sdt.h>
310 #include <sys/vnic.h>
311 #include <sys/vnic_impl.h>
312 #include <sys/vlan.h>
313 #include <inet/ip.h>
314 #include <inet/ip6.h>
315 #include <sys/exacct.h>
316 #include <sys/exacct_impl.h>
317 #include <inet/nd.h>
318 #include <sys/ethernet.h>
319 #include <sys/pool.h>
320 #include <sys/pool_pset.h>
321 #include <sys/cpupart.h>
322 #include <inet/wifi_ioctl.h>
323 #include <net/wpa.h>
324 
325 #define	IMPL_HASHSZ	67	/* prime */
326 
327 kmem_cache_t		*i_mac_impl_cachep;
328 mod_hash_t		*i_mac_impl_hash;
329 krwlock_t		i_mac_impl_lock;
330 uint_t			i_mac_impl_count;
331 static kmem_cache_t	*mac_ring_cache;
332 static id_space_t	*minor_ids;
333 static uint32_t		minor_count;
334 static pool_event_cb_t	mac_pool_event_reg;
335 
336 /*
337  * Logging stuff. Perhaps mac_logging_interval could be broken into
338  * mac_flow_log_interval and mac_link_log_interval if we want to be
339  * able to schedule them differently.
340  */
341 uint_t			mac_logging_interval;
342 boolean_t		mac_flow_log_enable;
343 boolean_t		mac_link_log_enable;
344 timeout_id_t		mac_logging_timer;
345 
346 #define	MACTYPE_KMODDIR	"mac"
347 #define	MACTYPE_HASHSZ	67
348 static mod_hash_t	*i_mactype_hash;
349 /*
350  * i_mactype_lock synchronizes threads that obtain references to mactype_t
351  * structures through i_mactype_getplugin().
352  */
353 static kmutex_t		i_mactype_lock;
354 
355 /*
356  * mac_tx_percpu_cnt
357  *
358  * Number of per cpu locks per mac_client_impl_t. Used by the transmit side
359  * in mac_tx to reduce lock contention. This is sized at boot time in mac_init.
360  * mac_tx_percpu_cnt_max is settable in /etc/system and must be a power of 2.
361  * Per cpu locks may be disabled by setting mac_tx_percpu_cnt_max to 1.
362  */
363 int mac_tx_percpu_cnt;
364 int mac_tx_percpu_cnt_max = 128;
365 
366 /*
367  * Call back functions for the bridge module.  These are guaranteed to be valid
368  * when holding a reference on a link or when holding mip->mi_bridge_lock and
369  * mi_bridge_link is non-NULL.
370  */
371 mac_bridge_tx_t mac_bridge_tx_cb;
372 mac_bridge_rx_t mac_bridge_rx_cb;
373 mac_bridge_ref_t mac_bridge_ref_cb;
374 mac_bridge_ls_t mac_bridge_ls_cb;
375 
376 static int i_mac_constructor(void *, void *, int);
377 static void i_mac_destructor(void *, void *);
378 static int i_mac_ring_ctor(void *, void *, int);
379 static void i_mac_ring_dtor(void *, void *);
380 static mblk_t *mac_rx_classify(mac_impl_t *, mac_resource_handle_t, mblk_t *);
381 void mac_tx_client_flush(mac_client_impl_t *);
382 void mac_tx_client_block(mac_client_impl_t *);
383 static void mac_rx_ring_quiesce(mac_ring_t *, uint_t);
384 static int mac_start_group_and_rings(mac_group_t *);
385 static void mac_stop_group_and_rings(mac_group_t *);
386 static void mac_pool_event_cb(pool_event_t, int, void *);
387 
388 typedef struct netinfo_s {
389 	list_node_t	ni_link;
390 	void		*ni_record;
391 	int		ni_size;
392 	int		ni_type;
393 } netinfo_t;
394 
395 /*
396  * Module initialization functions.
397  */
398 
399 void
400 mac_init(void)
401 {
402 	mac_tx_percpu_cnt = ((boot_max_ncpus == -1) ? max_ncpus :
403 	    boot_max_ncpus);
404 
405 	/* Upper bound is mac_tx_percpu_cnt_max */
406 	if (mac_tx_percpu_cnt > mac_tx_percpu_cnt_max)
407 		mac_tx_percpu_cnt = mac_tx_percpu_cnt_max;
408 
409 	if (mac_tx_percpu_cnt < 1) {
410 		/* Someone set max_tx_percpu_cnt_max to 0 or less */
411 		mac_tx_percpu_cnt = 1;
412 	}
413 
414 	ASSERT(mac_tx_percpu_cnt >= 1);
415 	mac_tx_percpu_cnt = (1 << highbit(mac_tx_percpu_cnt - 1));
416 	/*
417 	 * Make it of the form 2**N - 1 in the range
418 	 * [0 .. mac_tx_percpu_cnt_max - 1]
419 	 */
420 	mac_tx_percpu_cnt--;
421 
422 	i_mac_impl_cachep = kmem_cache_create("mac_impl_cache",
423 	    sizeof (mac_impl_t), 0, i_mac_constructor, i_mac_destructor,
424 	    NULL, NULL, NULL, 0);
425 	ASSERT(i_mac_impl_cachep != NULL);
426 
427 	mac_ring_cache = kmem_cache_create("mac_ring_cache",
428 	    sizeof (mac_ring_t), 0, i_mac_ring_ctor, i_mac_ring_dtor, NULL,
429 	    NULL, NULL, 0);
430 	ASSERT(mac_ring_cache != NULL);
431 
432 	i_mac_impl_hash = mod_hash_create_extended("mac_impl_hash",
433 	    IMPL_HASHSZ, mod_hash_null_keydtor, mod_hash_null_valdtor,
434 	    mod_hash_bystr, NULL, mod_hash_strkey_cmp, KM_SLEEP);
435 	rw_init(&i_mac_impl_lock, NULL, RW_DEFAULT, NULL);
436 
437 	mac_flow_init();
438 	mac_soft_ring_init();
439 	mac_bcast_init();
440 	mac_client_init();
441 
442 	i_mac_impl_count = 0;
443 
444 	i_mactype_hash = mod_hash_create_extended("mactype_hash",
445 	    MACTYPE_HASHSZ,
446 	    mod_hash_null_keydtor, mod_hash_null_valdtor,
447 	    mod_hash_bystr, NULL, mod_hash_strkey_cmp, KM_SLEEP);
448 
449 	/*
450 	 * Allocate an id space to manage minor numbers. The range of the
451 	 * space will be from MAC_MAX_MINOR+1 to MAC_PRIVATE_MINOR-1.  This
452 	 * leaves half of the 32-bit minors available for driver private use.
453 	 */
454 	minor_ids = id_space_create("mac_minor_ids", MAC_MAX_MINOR+1,
455 	    MAC_PRIVATE_MINOR-1);
456 	ASSERT(minor_ids != NULL);
457 	minor_count = 0;
458 
459 	/* Let's default to 20 seconds */
460 	mac_logging_interval = 20;
461 	mac_flow_log_enable = B_FALSE;
462 	mac_link_log_enable = B_FALSE;
463 	mac_logging_timer = NULL;
464 
465 	/* Register to be notified of noteworthy pools events */
466 	mac_pool_event_reg.pec_func =  mac_pool_event_cb;
467 	mac_pool_event_reg.pec_arg = NULL;
468 	pool_event_cb_register(&mac_pool_event_reg);
469 }
470 
471 int
472 mac_fini(void)
473 {
474 
475 	if (i_mac_impl_count > 0 || minor_count > 0)
476 		return (EBUSY);
477 
478 	pool_event_cb_unregister(&mac_pool_event_reg);
479 
480 	id_space_destroy(minor_ids);
481 	mac_flow_fini();
482 
483 	mod_hash_destroy_hash(i_mac_impl_hash);
484 	rw_destroy(&i_mac_impl_lock);
485 
486 	mac_client_fini();
487 	kmem_cache_destroy(mac_ring_cache);
488 
489 	mod_hash_destroy_hash(i_mactype_hash);
490 	mac_soft_ring_finish();
491 
492 
493 	return (0);
494 }
495 
496 /*
497  * Initialize a GLDv3 driver's device ops.  A driver that manages its own ops
498  * (e.g. softmac) may pass in a NULL ops argument.
499  */
500 void
501 mac_init_ops(struct dev_ops *ops, const char *name)
502 {
503 	major_t major = ddi_name_to_major((char *)name);
504 
505 	/*
506 	 * By returning on error below, we are not letting the driver continue
507 	 * in an undefined context.  The mac_register() function will faill if
508 	 * DN_GLDV3_DRIVER isn't set.
509 	 */
510 	if (major == DDI_MAJOR_T_NONE)
511 		return;
512 	LOCK_DEV_OPS(&devnamesp[major].dn_lock);
513 	devnamesp[major].dn_flags |= (DN_GLDV3_DRIVER | DN_NETWORK_DRIVER);
514 	UNLOCK_DEV_OPS(&devnamesp[major].dn_lock);
515 	if (ops != NULL)
516 		dld_init_ops(ops, name);
517 }
518 
519 void
520 mac_fini_ops(struct dev_ops *ops)
521 {
522 	dld_fini_ops(ops);
523 }
524 
525 /*ARGSUSED*/
526 static int
527 i_mac_constructor(void *buf, void *arg, int kmflag)
528 {
529 	mac_impl_t	*mip = buf;
530 
531 	bzero(buf, sizeof (mac_impl_t));
532 
533 	mip->mi_linkstate = LINK_STATE_UNKNOWN;
534 
535 	rw_init(&mip->mi_rw_lock, NULL, RW_DRIVER, NULL);
536 	mutex_init(&mip->mi_notify_lock, NULL, MUTEX_DRIVER, NULL);
537 	mutex_init(&mip->mi_promisc_lock, NULL, MUTEX_DRIVER, NULL);
538 	mutex_init(&mip->mi_ring_lock, NULL, MUTEX_DEFAULT, NULL);
539 
540 	mip->mi_notify_cb_info.mcbi_lockp = &mip->mi_notify_lock;
541 	cv_init(&mip->mi_notify_cb_info.mcbi_cv, NULL, CV_DRIVER, NULL);
542 	mip->mi_promisc_cb_info.mcbi_lockp = &mip->mi_promisc_lock;
543 	cv_init(&mip->mi_promisc_cb_info.mcbi_cv, NULL, CV_DRIVER, NULL);
544 
545 	mutex_init(&mip->mi_bridge_lock, NULL, MUTEX_DEFAULT, NULL);
546 
547 	return (0);
548 }
549 
550 /*ARGSUSED*/
551 static void
552 i_mac_destructor(void *buf, void *arg)
553 {
554 	mac_impl_t	*mip = buf;
555 	mac_cb_info_t	*mcbi;
556 
557 	ASSERT(mip->mi_ref == 0);
558 	ASSERT(mip->mi_active == 0);
559 	ASSERT(mip->mi_linkstate == LINK_STATE_UNKNOWN);
560 	ASSERT(mip->mi_devpromisc == 0);
561 	ASSERT(mip->mi_ksp == NULL);
562 	ASSERT(mip->mi_kstat_count == 0);
563 	ASSERT(mip->mi_nclients == 0);
564 	ASSERT(mip->mi_nactiveclients == 0);
565 	ASSERT(mip->mi_single_active_client == NULL);
566 	ASSERT(mip->mi_state_flags == 0);
567 	ASSERT(mip->mi_factory_addr == NULL);
568 	ASSERT(mip->mi_factory_addr_num == 0);
569 	ASSERT(mip->mi_default_tx_ring == NULL);
570 
571 	mcbi = &mip->mi_notify_cb_info;
572 	ASSERT(mcbi->mcbi_del_cnt == 0 && mcbi->mcbi_walker_cnt == 0);
573 	ASSERT(mip->mi_notify_bits == 0);
574 	ASSERT(mip->mi_notify_thread == NULL);
575 	ASSERT(mcbi->mcbi_lockp == &mip->mi_notify_lock);
576 	mcbi->mcbi_lockp = NULL;
577 
578 	mcbi = &mip->mi_promisc_cb_info;
579 	ASSERT(mcbi->mcbi_del_cnt == 0 && mip->mi_promisc_list == NULL);
580 	ASSERT(mip->mi_promisc_list == NULL);
581 	ASSERT(mcbi->mcbi_lockp == &mip->mi_promisc_lock);
582 	mcbi->mcbi_lockp = NULL;
583 
584 	ASSERT(mip->mi_bcast_ngrps == 0 && mip->mi_bcast_grp == NULL);
585 	ASSERT(mip->mi_perim_owner == NULL && mip->mi_perim_ocnt == 0);
586 
587 	rw_destroy(&mip->mi_rw_lock);
588 
589 	mutex_destroy(&mip->mi_promisc_lock);
590 	cv_destroy(&mip->mi_promisc_cb_info.mcbi_cv);
591 	mutex_destroy(&mip->mi_notify_lock);
592 	cv_destroy(&mip->mi_notify_cb_info.mcbi_cv);
593 	mutex_destroy(&mip->mi_ring_lock);
594 
595 	ASSERT(mip->mi_bridge_link == NULL);
596 }
597 
598 /* ARGSUSED */
599 static int
600 i_mac_ring_ctor(void *buf, void *arg, int kmflag)
601 {
602 	mac_ring_t *ring = (mac_ring_t *)buf;
603 
604 	bzero(ring, sizeof (mac_ring_t));
605 	cv_init(&ring->mr_cv, NULL, CV_DEFAULT, NULL);
606 	mutex_init(&ring->mr_lock, NULL, MUTEX_DEFAULT, NULL);
607 	ring->mr_state = MR_FREE;
608 	return (0);
609 }
610 
611 /* ARGSUSED */
612 static void
613 i_mac_ring_dtor(void *buf, void *arg)
614 {
615 	mac_ring_t *ring = (mac_ring_t *)buf;
616 
617 	cv_destroy(&ring->mr_cv);
618 	mutex_destroy(&ring->mr_lock);
619 }
620 
621 /*
622  * Common functions to do mac callback addition and deletion. Currently this is
623  * used by promisc callbacks and notify callbacks. List addition and deletion
624  * need to take care of list walkers. List walkers in general, can't hold list
625  * locks and make upcall callbacks due to potential lock order and recursive
626  * reentry issues. Instead list walkers increment the list walker count to mark
627  * the presence of a walker thread. Addition can be carefully done to ensure
628  * that the list walker always sees either the old list or the new list.
629  * However the deletion can't be done while the walker is active, instead the
630  * deleting thread simply marks the entry as logically deleted. The last walker
631  * physically deletes and frees up the logically deleted entries when the walk
632  * is complete.
633  */
634 void
635 mac_callback_add(mac_cb_info_t *mcbi, mac_cb_t **mcb_head,
636     mac_cb_t *mcb_elem)
637 {
638 	mac_cb_t	*p;
639 	mac_cb_t	**pp;
640 
641 	/* Verify it is not already in the list */
642 	for (pp = mcb_head; (p = *pp) != NULL; pp = &p->mcb_nextp) {
643 		if (p == mcb_elem)
644 			break;
645 	}
646 	VERIFY(p == NULL);
647 
648 	/*
649 	 * Add it to the head of the callback list. The membar ensures that
650 	 * the following list pointer manipulations reach global visibility
651 	 * in exactly the program order below.
652 	 */
653 	ASSERT(MUTEX_HELD(mcbi->mcbi_lockp));
654 
655 	mcb_elem->mcb_nextp = *mcb_head;
656 	membar_producer();
657 	*mcb_head = mcb_elem;
658 }
659 
660 /*
661  * Mark the entry as logically deleted. If there aren't any walkers unlink
662  * from the list. In either case return the corresponding status.
663  */
664 boolean_t
665 mac_callback_remove(mac_cb_info_t *mcbi, mac_cb_t **mcb_head,
666     mac_cb_t *mcb_elem)
667 {
668 	mac_cb_t	*p;
669 	mac_cb_t	**pp;
670 
671 	ASSERT(MUTEX_HELD(mcbi->mcbi_lockp));
672 	/*
673 	 * Search the callback list for the entry to be removed
674 	 */
675 	for (pp = mcb_head; (p = *pp) != NULL; pp = &p->mcb_nextp) {
676 		if (p == mcb_elem)
677 			break;
678 	}
679 	VERIFY(p != NULL);
680 
681 	/*
682 	 * If there are walkers just mark it as deleted and the last walker
683 	 * will remove from the list and free it.
684 	 */
685 	if (mcbi->mcbi_walker_cnt != 0) {
686 		p->mcb_flags |= MCB_CONDEMNED;
687 		mcbi->mcbi_del_cnt++;
688 		return (B_FALSE);
689 	}
690 
691 	ASSERT(mcbi->mcbi_del_cnt == 0);
692 	*pp = p->mcb_nextp;
693 	p->mcb_nextp = NULL;
694 	return (B_TRUE);
695 }
696 
697 /*
698  * Wait for all pending callback removals to be completed
699  */
700 void
701 mac_callback_remove_wait(mac_cb_info_t *mcbi)
702 {
703 	ASSERT(MUTEX_HELD(mcbi->mcbi_lockp));
704 	while (mcbi->mcbi_del_cnt != 0) {
705 		DTRACE_PROBE1(need_wait, mac_cb_info_t *, mcbi);
706 		cv_wait(&mcbi->mcbi_cv, mcbi->mcbi_lockp);
707 	}
708 }
709 
710 /*
711  * The last mac callback walker does the cleanup. Walk the list and unlik
712  * all the logically deleted entries and construct a temporary list of
713  * removed entries. Return the list of removed entries to the caller.
714  */
715 mac_cb_t *
716 mac_callback_walker_cleanup(mac_cb_info_t *mcbi, mac_cb_t **mcb_head)
717 {
718 	mac_cb_t	*p;
719 	mac_cb_t	**pp;
720 	mac_cb_t	*rmlist = NULL;		/* List of removed elements */
721 	int	cnt = 0;
722 
723 	ASSERT(MUTEX_HELD(mcbi->mcbi_lockp));
724 	ASSERT(mcbi->mcbi_del_cnt != 0 && mcbi->mcbi_walker_cnt == 0);
725 
726 	pp = mcb_head;
727 	while (*pp != NULL) {
728 		if ((*pp)->mcb_flags & MCB_CONDEMNED) {
729 			p = *pp;
730 			*pp = p->mcb_nextp;
731 			p->mcb_nextp = rmlist;
732 			rmlist = p;
733 			cnt++;
734 			continue;
735 		}
736 		pp = &(*pp)->mcb_nextp;
737 	}
738 
739 	ASSERT(mcbi->mcbi_del_cnt == cnt);
740 	mcbi->mcbi_del_cnt = 0;
741 	return (rmlist);
742 }
743 
744 boolean_t
745 mac_callback_lookup(mac_cb_t **mcb_headp, mac_cb_t *mcb_elem)
746 {
747 	mac_cb_t	*mcb;
748 
749 	/* Verify it is not already in the list */
750 	for (mcb = *mcb_headp; mcb != NULL; mcb = mcb->mcb_nextp) {
751 		if (mcb == mcb_elem)
752 			return (B_TRUE);
753 	}
754 
755 	return (B_FALSE);
756 }
757 
758 boolean_t
759 mac_callback_find(mac_cb_info_t *mcbi, mac_cb_t **mcb_headp, mac_cb_t *mcb_elem)
760 {
761 	boolean_t	found;
762 
763 	mutex_enter(mcbi->mcbi_lockp);
764 	found = mac_callback_lookup(mcb_headp, mcb_elem);
765 	mutex_exit(mcbi->mcbi_lockp);
766 
767 	return (found);
768 }
769 
770 /* Free the list of removed callbacks */
771 void
772 mac_callback_free(mac_cb_t *rmlist)
773 {
774 	mac_cb_t	*mcb;
775 	mac_cb_t	*mcb_next;
776 
777 	for (mcb = rmlist; mcb != NULL; mcb = mcb_next) {
778 		mcb_next = mcb->mcb_nextp;
779 		kmem_free(mcb->mcb_objp, mcb->mcb_objsize);
780 	}
781 }
782 
783 /*
784  * The promisc callbacks are in 2 lists, one off the 'mip' and another off the
785  * 'mcip' threaded by mpi_mi_link and mpi_mci_link respectively. However there
786  * is only a single shared total walker count, and an entry can't be physically
787  * unlinked if a walker is active on either list. The last walker does this
788  * cleanup of logically deleted entries.
789  */
790 void
791 i_mac_promisc_walker_cleanup(mac_impl_t *mip)
792 {
793 	mac_cb_t	*rmlist;
794 	mac_cb_t	*mcb;
795 	mac_cb_t	*mcb_next;
796 	mac_promisc_impl_t	*mpip;
797 
798 	/*
799 	 * Construct a temporary list of deleted callbacks by walking the
800 	 * the mi_promisc_list. Then for each entry in the temporary list,
801 	 * remove it from the mci_promisc_list and free the entry.
802 	 */
803 	rmlist = mac_callback_walker_cleanup(&mip->mi_promisc_cb_info,
804 	    &mip->mi_promisc_list);
805 
806 	for (mcb = rmlist; mcb != NULL; mcb = mcb_next) {
807 		mcb_next = mcb->mcb_nextp;
808 		mpip = (mac_promisc_impl_t *)mcb->mcb_objp;
809 		VERIFY(mac_callback_remove(&mip->mi_promisc_cb_info,
810 		    &mpip->mpi_mcip->mci_promisc_list, &mpip->mpi_mci_link));
811 		mcb->mcb_flags = 0;
812 		mcb->mcb_nextp = NULL;
813 		kmem_cache_free(mac_promisc_impl_cache, mpip);
814 	}
815 }
816 
817 void
818 i_mac_notify(mac_impl_t *mip, mac_notify_type_t type)
819 {
820 	mac_cb_info_t	*mcbi;
821 
822 	/*
823 	 * Signal the notify thread even after mi_ref has become zero and
824 	 * mi_disabled is set. The synchronization with the notify thread
825 	 * happens in mac_unregister and that implies the driver must make
826 	 * sure it is single-threaded (with respect to mac calls) and that
827 	 * all pending mac calls have returned before it calls mac_unregister
828 	 */
829 	rw_enter(&i_mac_impl_lock, RW_READER);
830 	if (mip->mi_state_flags & MIS_DISABLED)
831 		goto exit;
832 
833 	/*
834 	 * Guard against incorrect notifications.  (Running a newer
835 	 * mac client against an older implementation?)
836 	 */
837 	if (type >= MAC_NNOTE)
838 		goto exit;
839 
840 	mcbi = &mip->mi_notify_cb_info;
841 	mutex_enter(mcbi->mcbi_lockp);
842 	mip->mi_notify_bits |= (1 << type);
843 	cv_broadcast(&mcbi->mcbi_cv);
844 	mutex_exit(mcbi->mcbi_lockp);
845 
846 exit:
847 	rw_exit(&i_mac_impl_lock);
848 }
849 
850 /*
851  * Mac serialization primitives. Please see the block comment at the
852  * top of the file.
853  */
854 void
855 i_mac_perim_enter(mac_impl_t *mip)
856 {
857 	mac_client_impl_t	*mcip;
858 
859 	if (mip->mi_state_flags & MIS_IS_VNIC) {
860 		/*
861 		 * This is a VNIC. Return the lower mac since that is what
862 		 * we want to serialize on.
863 		 */
864 		mcip = mac_vnic_lower(mip);
865 		mip = mcip->mci_mip;
866 	}
867 
868 	mutex_enter(&mip->mi_perim_lock);
869 	if (mip->mi_perim_owner == curthread) {
870 		mip->mi_perim_ocnt++;
871 		mutex_exit(&mip->mi_perim_lock);
872 		return;
873 	}
874 
875 	while (mip->mi_perim_owner != NULL)
876 		cv_wait(&mip->mi_perim_cv, &mip->mi_perim_lock);
877 
878 	mip->mi_perim_owner = curthread;
879 	ASSERT(mip->mi_perim_ocnt == 0);
880 	mip->mi_perim_ocnt++;
881 #ifdef DEBUG
882 	mip->mi_perim_stack_depth = getpcstack(mip->mi_perim_stack,
883 	    MAC_PERIM_STACK_DEPTH);
884 #endif
885 	mutex_exit(&mip->mi_perim_lock);
886 }
887 
888 int
889 i_mac_perim_enter_nowait(mac_impl_t *mip)
890 {
891 	/*
892 	 * The vnic is a special case, since the serialization is done based
893 	 * on the lower mac. If the lower mac is busy, it does not imply the
894 	 * vnic can't be unregistered. But in the case of other drivers,
895 	 * a busy perimeter or open mac handles implies that the mac is busy
896 	 * and can't be unregistered.
897 	 */
898 	if (mip->mi_state_flags & MIS_IS_VNIC) {
899 		i_mac_perim_enter(mip);
900 		return (0);
901 	}
902 
903 	mutex_enter(&mip->mi_perim_lock);
904 	if (mip->mi_perim_owner != NULL) {
905 		mutex_exit(&mip->mi_perim_lock);
906 		return (EBUSY);
907 	}
908 	ASSERT(mip->mi_perim_ocnt == 0);
909 	mip->mi_perim_owner = curthread;
910 	mip->mi_perim_ocnt++;
911 	mutex_exit(&mip->mi_perim_lock);
912 
913 	return (0);
914 }
915 
916 void
917 i_mac_perim_exit(mac_impl_t *mip)
918 {
919 	mac_client_impl_t *mcip;
920 
921 	if (mip->mi_state_flags & MIS_IS_VNIC) {
922 		/*
923 		 * This is a VNIC. Return the lower mac since that is what
924 		 * we want to serialize on.
925 		 */
926 		mcip = mac_vnic_lower(mip);
927 		mip = mcip->mci_mip;
928 	}
929 
930 	ASSERT(mip->mi_perim_owner == curthread && mip->mi_perim_ocnt != 0);
931 
932 	mutex_enter(&mip->mi_perim_lock);
933 	if (--mip->mi_perim_ocnt == 0) {
934 		mip->mi_perim_owner = NULL;
935 		cv_signal(&mip->mi_perim_cv);
936 	}
937 	mutex_exit(&mip->mi_perim_lock);
938 }
939 
940 /*
941  * Returns whether the current thread holds the mac perimeter. Used in making
942  * assertions.
943  */
944 boolean_t
945 mac_perim_held(mac_handle_t mh)
946 {
947 	mac_impl_t	*mip = (mac_impl_t *)mh;
948 	mac_client_impl_t *mcip;
949 
950 	if (mip->mi_state_flags & MIS_IS_VNIC) {
951 		/*
952 		 * This is a VNIC. Return the lower mac since that is what
953 		 * we want to serialize on.
954 		 */
955 		mcip = mac_vnic_lower(mip);
956 		mip = mcip->mci_mip;
957 	}
958 	return (mip->mi_perim_owner == curthread);
959 }
960 
961 /*
962  * mac client interfaces to enter the mac perimeter of a mac end point, given
963  * its mac handle, or macname or linkid.
964  */
965 void
966 mac_perim_enter_by_mh(mac_handle_t mh, mac_perim_handle_t *mphp)
967 {
968 	mac_impl_t	*mip = (mac_impl_t *)mh;
969 
970 	i_mac_perim_enter(mip);
971 	/*
972 	 * The mac_perim_handle_t returned encodes the 'mip' and whether a
973 	 * mac_open has been done internally while entering the perimeter.
974 	 * This information is used in mac_perim_exit
975 	 */
976 	MAC_ENCODE_MPH(*mphp, mip, 0);
977 }
978 
979 int
980 mac_perim_enter_by_macname(const char *name, mac_perim_handle_t *mphp)
981 {
982 	int	err;
983 	mac_handle_t	mh;
984 
985 	if ((err = mac_open(name, &mh)) != 0)
986 		return (err);
987 
988 	mac_perim_enter_by_mh(mh, mphp);
989 	MAC_ENCODE_MPH(*mphp, mh, 1);
990 	return (0);
991 }
992 
993 int
994 mac_perim_enter_by_linkid(datalink_id_t linkid, mac_perim_handle_t *mphp)
995 {
996 	int	err;
997 	mac_handle_t	mh;
998 
999 	if ((err = mac_open_by_linkid(linkid, &mh)) != 0)
1000 		return (err);
1001 
1002 	mac_perim_enter_by_mh(mh, mphp);
1003 	MAC_ENCODE_MPH(*mphp, mh, 1);
1004 	return (0);
1005 }
1006 
1007 void
1008 mac_perim_exit(mac_perim_handle_t mph)
1009 {
1010 	mac_impl_t	*mip;
1011 	boolean_t	need_close;
1012 
1013 	MAC_DECODE_MPH(mph, mip, need_close);
1014 	i_mac_perim_exit(mip);
1015 	if (need_close)
1016 		mac_close((mac_handle_t)mip);
1017 }
1018 
1019 int
1020 mac_hold(const char *macname, mac_impl_t **pmip)
1021 {
1022 	mac_impl_t	*mip;
1023 	int		err;
1024 
1025 	/*
1026 	 * Check the device name length to make sure it won't overflow our
1027 	 * buffer.
1028 	 */
1029 	if (strlen(macname) >= MAXNAMELEN)
1030 		return (EINVAL);
1031 
1032 	/*
1033 	 * Look up its entry in the global hash table.
1034 	 */
1035 	rw_enter(&i_mac_impl_lock, RW_WRITER);
1036 	err = mod_hash_find(i_mac_impl_hash, (mod_hash_key_t)macname,
1037 	    (mod_hash_val_t *)&mip);
1038 
1039 	if (err != 0) {
1040 		rw_exit(&i_mac_impl_lock);
1041 		return (ENOENT);
1042 	}
1043 
1044 	if (mip->mi_state_flags & MIS_DISABLED) {
1045 		rw_exit(&i_mac_impl_lock);
1046 		return (ENOENT);
1047 	}
1048 
1049 	if (mip->mi_state_flags & MIS_EXCLUSIVE_HELD) {
1050 		rw_exit(&i_mac_impl_lock);
1051 		return (EBUSY);
1052 	}
1053 
1054 	mip->mi_ref++;
1055 	rw_exit(&i_mac_impl_lock);
1056 
1057 	*pmip = mip;
1058 	return (0);
1059 }
1060 
1061 void
1062 mac_rele(mac_impl_t *mip)
1063 {
1064 	rw_enter(&i_mac_impl_lock, RW_WRITER);
1065 	ASSERT(mip->mi_ref != 0);
1066 	if (--mip->mi_ref == 0) {
1067 		ASSERT(mip->mi_nactiveclients == 0 &&
1068 		    !(mip->mi_state_flags & MIS_EXCLUSIVE));
1069 	}
1070 	rw_exit(&i_mac_impl_lock);
1071 }
1072 
1073 /*
1074  * Private GLDv3 function to start a MAC instance.
1075  */
1076 int
1077 mac_start(mac_handle_t mh)
1078 {
1079 	mac_impl_t	*mip = (mac_impl_t *)mh;
1080 	int		err = 0;
1081 	mac_group_t	*defgrp;
1082 
1083 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
1084 	ASSERT(mip->mi_start != NULL);
1085 
1086 	/*
1087 	 * Check whether the device is already started.
1088 	 */
1089 	if (mip->mi_active++ == 0) {
1090 		mac_ring_t *ring = NULL;
1091 
1092 		/*
1093 		 * Start the device.
1094 		 */
1095 		err = mip->mi_start(mip->mi_driver);
1096 		if (err != 0) {
1097 			mip->mi_active--;
1098 			return (err);
1099 		}
1100 
1101 		/*
1102 		 * Start the default tx ring.
1103 		 */
1104 		if (mip->mi_default_tx_ring != NULL) {
1105 
1106 			ring = (mac_ring_t *)mip->mi_default_tx_ring;
1107 			if (ring->mr_state != MR_INUSE) {
1108 				err = mac_start_ring(ring);
1109 				if (err != 0) {
1110 					mip->mi_active--;
1111 					return (err);
1112 				}
1113 			}
1114 		}
1115 
1116 		if ((defgrp = MAC_DEFAULT_RX_GROUP(mip)) != NULL) {
1117 			/*
1118 			 * Start the default group which is responsible
1119 			 * for receiving broadcast and multicast
1120 			 * traffic for both primary and non-primary
1121 			 * MAC clients.
1122 			 */
1123 			ASSERT(defgrp->mrg_state == MAC_GROUP_STATE_REGISTERED);
1124 			err = mac_start_group_and_rings(defgrp);
1125 			if (err != 0) {
1126 				mip->mi_active--;
1127 				if ((ring != NULL) &&
1128 				    (ring->mr_state == MR_INUSE))
1129 					mac_stop_ring(ring);
1130 				return (err);
1131 			}
1132 			mac_set_group_state(defgrp, MAC_GROUP_STATE_SHARED);
1133 		}
1134 	}
1135 
1136 	return (err);
1137 }
1138 
1139 /*
1140  * Private GLDv3 function to stop a MAC instance.
1141  */
1142 void
1143 mac_stop(mac_handle_t mh)
1144 {
1145 	mac_impl_t	*mip = (mac_impl_t *)mh;
1146 	mac_group_t	*grp;
1147 
1148 	ASSERT(mip->mi_stop != NULL);
1149 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
1150 
1151 	/*
1152 	 * Check whether the device is still needed.
1153 	 */
1154 	ASSERT(mip->mi_active != 0);
1155 	if (--mip->mi_active == 0) {
1156 		if ((grp = MAC_DEFAULT_RX_GROUP(mip)) != NULL) {
1157 			/*
1158 			 * There should be no more active clients since the
1159 			 * MAC is being stopped. Stop the default RX group
1160 			 * and transition it back to registered state.
1161 			 *
1162 			 * When clients are torn down, the groups
1163 			 * are release via mac_release_rx_group which
1164 			 * knows the the default group is always in
1165 			 * started mode since broadcast uses it. So
1166 			 * we can assert that their are no clients
1167 			 * (since mac_bcast_add doesn't register itself
1168 			 * as a client) and group is in SHARED state.
1169 			 */
1170 			ASSERT(grp->mrg_state == MAC_GROUP_STATE_SHARED);
1171 			ASSERT(MAC_GROUP_NO_CLIENT(grp) &&
1172 			    mip->mi_nactiveclients == 0);
1173 			mac_stop_group_and_rings(grp);
1174 			mac_set_group_state(grp, MAC_GROUP_STATE_REGISTERED);
1175 		}
1176 
1177 		if (mip->mi_default_tx_ring != NULL) {
1178 			mac_ring_t *ring;
1179 
1180 			ring = (mac_ring_t *)mip->mi_default_tx_ring;
1181 			if (ring->mr_state == MR_INUSE) {
1182 				mac_stop_ring(ring);
1183 				ring->mr_flag = 0;
1184 			}
1185 		}
1186 
1187 		/*
1188 		 * Stop the device.
1189 		 */
1190 		mip->mi_stop(mip->mi_driver);
1191 	}
1192 }
1193 
1194 int
1195 i_mac_promisc_set(mac_impl_t *mip, boolean_t on)
1196 {
1197 	int		err = 0;
1198 
1199 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
1200 	ASSERT(mip->mi_setpromisc != NULL);
1201 
1202 	if (on) {
1203 		/*
1204 		 * Enable promiscuous mode on the device if not yet enabled.
1205 		 */
1206 		if (mip->mi_devpromisc++ == 0) {
1207 			err = mip->mi_setpromisc(mip->mi_driver, B_TRUE);
1208 			if (err != 0) {
1209 				mip->mi_devpromisc--;
1210 				return (err);
1211 			}
1212 			i_mac_notify(mip, MAC_NOTE_DEVPROMISC);
1213 		}
1214 	} else {
1215 		if (mip->mi_devpromisc == 0)
1216 			return (EPROTO);
1217 
1218 		/*
1219 		 * Disable promiscuous mode on the device if this is the last
1220 		 * enabling.
1221 		 */
1222 		if (--mip->mi_devpromisc == 0) {
1223 			err = mip->mi_setpromisc(mip->mi_driver, B_FALSE);
1224 			if (err != 0) {
1225 				mip->mi_devpromisc++;
1226 				return (err);
1227 			}
1228 			i_mac_notify(mip, MAC_NOTE_DEVPROMISC);
1229 		}
1230 	}
1231 
1232 	return (0);
1233 }
1234 
1235 /*
1236  * The promiscuity state can change any time. If the caller needs to take
1237  * actions that are atomic with the promiscuity state, then the caller needs
1238  * to bracket the entire sequence with mac_perim_enter/exit
1239  */
1240 boolean_t
1241 mac_promisc_get(mac_handle_t mh)
1242 {
1243 	mac_impl_t		*mip = (mac_impl_t *)mh;
1244 
1245 	/*
1246 	 * Return the current promiscuity.
1247 	 */
1248 	return (mip->mi_devpromisc != 0);
1249 }
1250 
1251 /*
1252  * Invoked at MAC instance attach time to initialize the list
1253  * of factory MAC addresses supported by a MAC instance. This function
1254  * builds a local cache in the mac_impl_t for the MAC addresses
1255  * supported by the underlying hardware. The MAC clients themselves
1256  * use the mac_addr_factory*() functions to query and reserve
1257  * factory MAC addresses.
1258  */
1259 void
1260 mac_addr_factory_init(mac_impl_t *mip)
1261 {
1262 	mac_capab_multifactaddr_t capab;
1263 	uint8_t *addr;
1264 	int i;
1265 
1266 	/*
1267 	 * First round to see how many factory MAC addresses are available.
1268 	 */
1269 	bzero(&capab, sizeof (capab));
1270 	if (!i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_MULTIFACTADDR,
1271 	    &capab) || (capab.mcm_naddr == 0)) {
1272 		/*
1273 		 * The MAC instance doesn't support multiple factory
1274 		 * MAC addresses, we're done here.
1275 		 */
1276 		return;
1277 	}
1278 
1279 	/*
1280 	 * Allocate the space and get all the factory addresses.
1281 	 */
1282 	addr = kmem_alloc(capab.mcm_naddr * MAXMACADDRLEN, KM_SLEEP);
1283 	capab.mcm_getaddr(mip->mi_driver, capab.mcm_naddr, addr);
1284 
1285 	mip->mi_factory_addr_num = capab.mcm_naddr;
1286 	mip->mi_factory_addr = kmem_zalloc(mip->mi_factory_addr_num *
1287 	    sizeof (mac_factory_addr_t), KM_SLEEP);
1288 
1289 	for (i = 0; i < capab.mcm_naddr; i++) {
1290 		bcopy(addr + i * MAXMACADDRLEN,
1291 		    mip->mi_factory_addr[i].mfa_addr,
1292 		    mip->mi_type->mt_addr_length);
1293 		mip->mi_factory_addr[i].mfa_in_use = B_FALSE;
1294 	}
1295 
1296 	kmem_free(addr, capab.mcm_naddr * MAXMACADDRLEN);
1297 }
1298 
1299 void
1300 mac_addr_factory_fini(mac_impl_t *mip)
1301 {
1302 	if (mip->mi_factory_addr == NULL) {
1303 		ASSERT(mip->mi_factory_addr_num == 0);
1304 		return;
1305 	}
1306 
1307 	kmem_free(mip->mi_factory_addr, mip->mi_factory_addr_num *
1308 	    sizeof (mac_factory_addr_t));
1309 
1310 	mip->mi_factory_addr = NULL;
1311 	mip->mi_factory_addr_num = 0;
1312 }
1313 
1314 /*
1315  * Reserve a factory MAC address. If *slot is set to -1, the function
1316  * attempts to reserve any of the available factory MAC addresses and
1317  * returns the reserved slot id. If no slots are available, the function
1318  * returns ENOSPC. If *slot is not set to -1, the function reserves
1319  * the specified slot if it is available, or returns EBUSY is the slot
1320  * is already used. Returns ENOTSUP if the underlying MAC does not
1321  * support multiple factory addresses. If the slot number is not -1 but
1322  * is invalid, returns EINVAL.
1323  */
1324 int
1325 mac_addr_factory_reserve(mac_client_handle_t mch, int *slot)
1326 {
1327 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
1328 	mac_impl_t *mip = mcip->mci_mip;
1329 	int i, ret = 0;
1330 
1331 	i_mac_perim_enter(mip);
1332 	/*
1333 	 * Protect against concurrent readers that may need a self-consistent
1334 	 * view of the factory addresses
1335 	 */
1336 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
1337 
1338 	if (mip->mi_factory_addr_num == 0) {
1339 		ret = ENOTSUP;
1340 		goto bail;
1341 	}
1342 
1343 	if (*slot != -1) {
1344 		/* check the specified slot */
1345 		if (*slot < 1 || *slot > mip->mi_factory_addr_num) {
1346 			ret = EINVAL;
1347 			goto bail;
1348 		}
1349 		if (mip->mi_factory_addr[*slot-1].mfa_in_use) {
1350 			ret = EBUSY;
1351 			goto bail;
1352 		}
1353 	} else {
1354 		/* pick the next available slot */
1355 		for (i = 0; i < mip->mi_factory_addr_num; i++) {
1356 			if (!mip->mi_factory_addr[i].mfa_in_use)
1357 				break;
1358 		}
1359 
1360 		if (i == mip->mi_factory_addr_num) {
1361 			ret = ENOSPC;
1362 			goto bail;
1363 		}
1364 		*slot = i+1;
1365 	}
1366 
1367 	mip->mi_factory_addr[*slot-1].mfa_in_use = B_TRUE;
1368 	mip->mi_factory_addr[*slot-1].mfa_client = mcip;
1369 
1370 bail:
1371 	rw_exit(&mip->mi_rw_lock);
1372 	i_mac_perim_exit(mip);
1373 	return (ret);
1374 }
1375 
1376 /*
1377  * Release the specified factory MAC address slot.
1378  */
1379 void
1380 mac_addr_factory_release(mac_client_handle_t mch, uint_t slot)
1381 {
1382 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
1383 	mac_impl_t *mip = mcip->mci_mip;
1384 
1385 	i_mac_perim_enter(mip);
1386 	/*
1387 	 * Protect against concurrent readers that may need a self-consistent
1388 	 * view of the factory addresses
1389 	 */
1390 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
1391 
1392 	ASSERT(slot > 0 && slot <= mip->mi_factory_addr_num);
1393 	ASSERT(mip->mi_factory_addr[slot-1].mfa_in_use);
1394 
1395 	mip->mi_factory_addr[slot-1].mfa_in_use = B_FALSE;
1396 
1397 	rw_exit(&mip->mi_rw_lock);
1398 	i_mac_perim_exit(mip);
1399 }
1400 
1401 /*
1402  * Stores in mac_addr the value of the specified MAC address. Returns
1403  * 0 on success, or EINVAL if the slot number is not valid for the MAC.
1404  * The caller must provide a string of at least MAXNAMELEN bytes.
1405  */
1406 void
1407 mac_addr_factory_value(mac_handle_t mh, int slot, uchar_t *mac_addr,
1408     uint_t *addr_len, char *client_name, boolean_t *in_use_arg)
1409 {
1410 	mac_impl_t *mip = (mac_impl_t *)mh;
1411 	boolean_t in_use;
1412 
1413 	ASSERT(slot > 0 && slot <= mip->mi_factory_addr_num);
1414 
1415 	/*
1416 	 * Readers need to hold mi_rw_lock. Writers need to hold mac perimeter
1417 	 * and mi_rw_lock
1418 	 */
1419 	rw_enter(&mip->mi_rw_lock, RW_READER);
1420 	bcopy(mip->mi_factory_addr[slot-1].mfa_addr, mac_addr, MAXMACADDRLEN);
1421 	*addr_len = mip->mi_type->mt_addr_length;
1422 	in_use = mip->mi_factory_addr[slot-1].mfa_in_use;
1423 	if (in_use && client_name != NULL) {
1424 		bcopy(mip->mi_factory_addr[slot-1].mfa_client->mci_name,
1425 		    client_name, MAXNAMELEN);
1426 	}
1427 	if (in_use_arg != NULL)
1428 		*in_use_arg = in_use;
1429 	rw_exit(&mip->mi_rw_lock);
1430 }
1431 
1432 /*
1433  * Returns the number of factory MAC addresses (in addition to the
1434  * primary MAC address), 0 if the underlying MAC doesn't support
1435  * that feature.
1436  */
1437 uint_t
1438 mac_addr_factory_num(mac_handle_t mh)
1439 {
1440 	mac_impl_t *mip = (mac_impl_t *)mh;
1441 
1442 	return (mip->mi_factory_addr_num);
1443 }
1444 
1445 
1446 void
1447 mac_rx_group_unmark(mac_group_t *grp, uint_t flag)
1448 {
1449 	mac_ring_t	*ring;
1450 
1451 	for (ring = grp->mrg_rings; ring != NULL; ring = ring->mr_next)
1452 		ring->mr_flag &= ~flag;
1453 }
1454 
1455 /*
1456  * The following mac_hwrings_xxx() functions are private mac client functions
1457  * used by the aggr driver to access and control the underlying HW Rx group
1458  * and rings. In this case, the aggr driver has exclusive control of the
1459  * underlying HW Rx group/rings, it calls the following functions to
1460  * start/stop the HW Rx rings, disable/enable polling, add/remove mac'
1461  * addresses, or set up the Rx callback.
1462  */
1463 /* ARGSUSED */
1464 static void
1465 mac_hwrings_rx_process(void *arg, mac_resource_handle_t srs,
1466     mblk_t *mp_chain, boolean_t loopback)
1467 {
1468 	mac_soft_ring_set_t	*mac_srs = (mac_soft_ring_set_t *)srs;
1469 	mac_srs_rx_t		*srs_rx = &mac_srs->srs_rx;
1470 	mac_direct_rx_t		proc;
1471 	void			*arg1;
1472 	mac_resource_handle_t	arg2;
1473 
1474 	proc = srs_rx->sr_func;
1475 	arg1 = srs_rx->sr_arg1;
1476 	arg2 = mac_srs->srs_mrh;
1477 
1478 	proc(arg1, arg2, mp_chain, NULL);
1479 }
1480 
1481 /*
1482  * This function is called to get the list of HW rings that are reserved by
1483  * an exclusive mac client.
1484  *
1485  * Return value: the number of HW rings.
1486  */
1487 int
1488 mac_hwrings_get(mac_client_handle_t mch, mac_group_handle_t *hwgh,
1489     mac_ring_handle_t *hwrh, mac_ring_type_t rtype)
1490 {
1491 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
1492 	flow_entry_t		*flent = mcip->mci_flent;
1493 	mac_group_t		*grp;
1494 	mac_ring_t		*ring;
1495 	int			cnt = 0;
1496 
1497 	if (rtype == MAC_RING_TYPE_RX) {
1498 		grp = flent->fe_rx_ring_group;
1499 	} else if (rtype == MAC_RING_TYPE_TX) {
1500 		grp = flent->fe_tx_ring_group;
1501 	} else {
1502 		ASSERT(B_FALSE);
1503 		return (-1);
1504 	}
1505 	/*
1506 	 * The mac client did not reserve any RX group, return directly.
1507 	 * This is probably because the underlying MAC does not support
1508 	 * any groups.
1509 	 */
1510 	if (hwgh != NULL)
1511 		*hwgh = NULL;
1512 	if (grp == NULL)
1513 		return (0);
1514 	/*
1515 	 * This group must be reserved by this mac client.
1516 	 */
1517 	ASSERT((grp->mrg_state == MAC_GROUP_STATE_RESERVED) &&
1518 	    (mcip == MAC_GROUP_ONLY_CLIENT(grp)));
1519 
1520 	for (ring = grp->mrg_rings; ring != NULL; ring = ring->mr_next, cnt++) {
1521 		ASSERT(cnt < MAX_RINGS_PER_GROUP);
1522 		hwrh[cnt] = (mac_ring_handle_t)ring;
1523 	}
1524 	if (hwgh != NULL)
1525 		*hwgh = (mac_group_handle_t)grp;
1526 
1527 	return (cnt);
1528 }
1529 
1530 /*
1531  * This function is called to get info about Tx/Rx rings.
1532  *
1533  * Return value: returns uint_t which will have various bits set
1534  * that indicates different properties of the ring.
1535  */
1536 uint_t
1537 mac_hwring_getinfo(mac_ring_handle_t rh)
1538 {
1539 	mac_ring_t *ring = (mac_ring_t *)rh;
1540 	mac_ring_info_t *info = &ring->mr_info;
1541 
1542 	return (info->mri_flags);
1543 }
1544 
1545 /*
1546  * Export ddi interrupt handles from the HW ring to the pseudo ring and
1547  * setup the RX callback of the mac client which exclusively controls
1548  * HW ring.
1549  */
1550 void
1551 mac_hwring_setup(mac_ring_handle_t hwrh, mac_resource_handle_t prh,
1552     mac_ring_handle_t pseudo_rh)
1553 {
1554 	mac_ring_t		*hw_ring = (mac_ring_t *)hwrh;
1555 	mac_ring_t		*pseudo_ring;
1556 	mac_soft_ring_set_t	*mac_srs = hw_ring->mr_srs;
1557 
1558 	if (pseudo_rh != NULL) {
1559 		pseudo_ring = (mac_ring_t *)pseudo_rh;
1560 		/* Export the ddi handles to pseudo ring */
1561 		pseudo_ring->mr_info.mri_intr.mi_ddi_handle =
1562 		    hw_ring->mr_info.mri_intr.mi_ddi_handle;
1563 		pseudo_ring->mr_info.mri_intr.mi_ddi_shared =
1564 		    hw_ring->mr_info.mri_intr.mi_ddi_shared;
1565 		/*
1566 		 * Save a pointer to pseudo ring in the hw ring. If
1567 		 * interrupt handle changes, the hw ring will be
1568 		 * notified of the change (see mac_ring_intr_set())
1569 		 * and the appropriate change has to be made to
1570 		 * the pseudo ring that has exported the ddi handle.
1571 		 */
1572 		hw_ring->mr_prh = pseudo_rh;
1573 	}
1574 
1575 	if (hw_ring->mr_type == MAC_RING_TYPE_RX) {
1576 		ASSERT(!(mac_srs->srs_type & SRST_TX));
1577 		mac_srs->srs_mrh = prh;
1578 		mac_srs->srs_rx.sr_lower_proc = mac_hwrings_rx_process;
1579 	}
1580 }
1581 
1582 void
1583 mac_hwring_teardown(mac_ring_handle_t hwrh)
1584 {
1585 	mac_ring_t		*hw_ring = (mac_ring_t *)hwrh;
1586 	mac_soft_ring_set_t	*mac_srs;
1587 
1588 	if (hw_ring == NULL)
1589 		return;
1590 	hw_ring->mr_prh = NULL;
1591 	if (hw_ring->mr_type == MAC_RING_TYPE_RX) {
1592 		mac_srs = hw_ring->mr_srs;
1593 		ASSERT(!(mac_srs->srs_type & SRST_TX));
1594 		mac_srs->srs_rx.sr_lower_proc = mac_rx_srs_process;
1595 		mac_srs->srs_mrh = NULL;
1596 	}
1597 }
1598 
1599 int
1600 mac_hwring_disable_intr(mac_ring_handle_t rh)
1601 {
1602 	mac_ring_t *rr_ring = (mac_ring_t *)rh;
1603 	mac_intr_t *intr = &rr_ring->mr_info.mri_intr;
1604 
1605 	return (intr->mi_disable(intr->mi_handle));
1606 }
1607 
1608 int
1609 mac_hwring_enable_intr(mac_ring_handle_t rh)
1610 {
1611 	mac_ring_t *rr_ring = (mac_ring_t *)rh;
1612 	mac_intr_t *intr = &rr_ring->mr_info.mri_intr;
1613 
1614 	return (intr->mi_enable(intr->mi_handle));
1615 }
1616 
1617 int
1618 mac_hwring_start(mac_ring_handle_t rh)
1619 {
1620 	mac_ring_t *rr_ring = (mac_ring_t *)rh;
1621 
1622 	MAC_RING_UNMARK(rr_ring, MR_QUIESCE);
1623 	return (0);
1624 }
1625 
1626 void
1627 mac_hwring_stop(mac_ring_handle_t rh)
1628 {
1629 	mac_ring_t *rr_ring = (mac_ring_t *)rh;
1630 
1631 	mac_rx_ring_quiesce(rr_ring, MR_QUIESCE);
1632 }
1633 
1634 mblk_t *
1635 mac_hwring_poll(mac_ring_handle_t rh, int bytes_to_pickup)
1636 {
1637 	mac_ring_t *rr_ring = (mac_ring_t *)rh;
1638 	mac_ring_info_t *info = &rr_ring->mr_info;
1639 
1640 	return (info->mri_poll(info->mri_driver, bytes_to_pickup));
1641 }
1642 
1643 /*
1644  * Send packets through a selected tx ring.
1645  */
1646 mblk_t *
1647 mac_hwring_tx(mac_ring_handle_t rh, mblk_t *mp)
1648 {
1649 	mac_ring_t *ring = (mac_ring_t *)rh;
1650 	mac_ring_info_t *info = &ring->mr_info;
1651 
1652 	ASSERT(ring->mr_type == MAC_RING_TYPE_TX &&
1653 	    ring->mr_state >= MR_INUSE);
1654 	return (info->mri_tx(info->mri_driver, mp));
1655 }
1656 
1657 /*
1658  * Query stats for a particular rx/tx ring
1659  */
1660 int
1661 mac_hwring_getstat(mac_ring_handle_t rh, uint_t stat, uint64_t *val)
1662 {
1663 	mac_ring_t	*ring = (mac_ring_t *)rh;
1664 	mac_ring_info_t *info = &ring->mr_info;
1665 
1666 	return (info->mri_stat(info->mri_driver, stat, val));
1667 }
1668 
1669 /*
1670  * Private function that is only used by aggr to send packets through
1671  * a port/Tx ring. Since aggr exposes a pseudo Tx ring even for ports
1672  * that does not expose Tx rings, aggr_ring_tx() entry point needs
1673  * access to mac_impl_t to send packets through m_tx() entry point.
1674  * It accomplishes this by calling mac_hwring_send_priv() function.
1675  */
1676 mblk_t *
1677 mac_hwring_send_priv(mac_client_handle_t mch, mac_ring_handle_t rh, mblk_t *mp)
1678 {
1679 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
1680 	mac_impl_t *mip = mcip->mci_mip;
1681 
1682 	MAC_TX(mip, rh, mp, mcip);
1683 	return (mp);
1684 }
1685 
1686 /*
1687  * Private function that is only used by aggr to update the default transmission
1688  * ring. Because aggr exposes a pseudo Tx ring even for ports that may
1689  * temporarily be down, it may need to update the default ring that is used by
1690  * MAC such that it refers to a link that can actively be used to send traffic.
1691  * Note that this is different from the case where the port has been removed
1692  * from the group. In those cases, all of the rings will be torn down because
1693  * the ring will no longer exist. It's important to give aggr a case where the
1694  * rings can still exist such that it may be able to continue to send LACP PDUs
1695  * to potentially restore the link.
1696  *
1697  * Finally, we explicitly don't do anything if the ring hasn't been enabled yet.
1698  * This is to help out aggr which doesn't really know the internal state that
1699  * MAC does about the rings and can't know that it's not quite ready for use
1700  * yet.
1701  */
1702 void
1703 mac_hwring_set_default(mac_handle_t mh, mac_ring_handle_t rh)
1704 {
1705 	mac_impl_t *mip = (mac_impl_t *)mh;
1706 	mac_ring_t *ring = (mac_ring_t *)rh;
1707 
1708 	ASSERT(MAC_PERIM_HELD(mh));
1709 	VERIFY(mip->mi_state_flags & MIS_IS_AGGR);
1710 
1711 	if (ring->mr_state != MR_INUSE)
1712 		return;
1713 
1714 	mip->mi_default_tx_ring = rh;
1715 }
1716 
1717 int
1718 mac_hwgroup_addmac(mac_group_handle_t gh, const uint8_t *addr)
1719 {
1720 	mac_group_t *group = (mac_group_t *)gh;
1721 
1722 	return (mac_group_addmac(group, addr));
1723 }
1724 
1725 int
1726 mac_hwgroup_remmac(mac_group_handle_t gh, const uint8_t *addr)
1727 {
1728 	mac_group_t *group = (mac_group_t *)gh;
1729 
1730 	return (mac_group_remmac(group, addr));
1731 }
1732 
1733 /*
1734  * Program the group's HW VLAN filter if it has such support.
1735  * Otherwise, the group will implicitly accept tagged traffic and
1736  * there is nothing to do.
1737  */
1738 int
1739 mac_hwgroup_addvlan(mac_group_handle_t gh, uint16_t vid)
1740 {
1741 	mac_group_t *group = (mac_group_t *)gh;
1742 
1743 	if (!MAC_GROUP_HW_VLAN(group))
1744 		return (0);
1745 
1746 	return (mac_group_addvlan(group, vid));
1747 }
1748 
1749 int
1750 mac_hwgroup_remvlan(mac_group_handle_t gh, uint16_t vid)
1751 {
1752 	mac_group_t *group = (mac_group_t *)gh;
1753 
1754 	if (!MAC_GROUP_HW_VLAN(group))
1755 		return (0);
1756 
1757 	return (mac_group_remvlan(group, vid));
1758 }
1759 
1760 /*
1761  * Determine if a MAC has HW VLAN support. This is a private API
1762  * consumed by aggr. In the future it might be nice to have a bitfield
1763  * in mac_capab_rings_t to track which forms of HW filtering are
1764  * supported by the MAC.
1765  */
1766 boolean_t
1767 mac_has_hw_vlan(mac_handle_t mh)
1768 {
1769 	mac_impl_t *mip = (mac_impl_t *)mh;
1770 
1771 	return (MAC_GROUP_HW_VLAN(mip->mi_rx_groups));
1772 }
1773 
1774 /*
1775  * Set the RX group to be shared/reserved. Note that the group must be
1776  * started/stopped outside of this function.
1777  */
1778 void
1779 mac_set_group_state(mac_group_t *grp, mac_group_state_t state)
1780 {
1781 	/*
1782 	 * If there is no change in the group state, just return.
1783 	 */
1784 	if (grp->mrg_state == state)
1785 		return;
1786 
1787 	switch (state) {
1788 	case MAC_GROUP_STATE_RESERVED:
1789 		/*
1790 		 * Successfully reserved the group.
1791 		 *
1792 		 * Given that there is an exclusive client controlling this
1793 		 * group, we enable the group level polling when available,
1794 		 * so that SRSs get to turn on/off individual rings they's
1795 		 * assigned to.
1796 		 */
1797 		ASSERT(MAC_PERIM_HELD(grp->mrg_mh));
1798 
1799 		if (grp->mrg_type == MAC_RING_TYPE_RX &&
1800 		    GROUP_INTR_DISABLE_FUNC(grp) != NULL) {
1801 			GROUP_INTR_DISABLE_FUNC(grp)(GROUP_INTR_HANDLE(grp));
1802 		}
1803 		break;
1804 
1805 	case MAC_GROUP_STATE_SHARED:
1806 		/*
1807 		 * Set all rings of this group to software classified.
1808 		 * If the group has an overriding interrupt, then re-enable it.
1809 		 */
1810 		ASSERT(MAC_PERIM_HELD(grp->mrg_mh));
1811 
1812 		if (grp->mrg_type == MAC_RING_TYPE_RX &&
1813 		    GROUP_INTR_ENABLE_FUNC(grp) != NULL) {
1814 			GROUP_INTR_ENABLE_FUNC(grp)(GROUP_INTR_HANDLE(grp));
1815 		}
1816 		/* The ring is not available for reservations any more */
1817 		break;
1818 
1819 	case MAC_GROUP_STATE_REGISTERED:
1820 		/* Also callable from mac_register, perim is not held */
1821 		break;
1822 
1823 	default:
1824 		ASSERT(B_FALSE);
1825 		break;
1826 	}
1827 
1828 	grp->mrg_state = state;
1829 }
1830 
1831 /*
1832  * Quiesce future hardware classified packets for the specified Rx ring
1833  */
1834 static void
1835 mac_rx_ring_quiesce(mac_ring_t *rx_ring, uint_t ring_flag)
1836 {
1837 	ASSERT(rx_ring->mr_classify_type == MAC_HW_CLASSIFIER);
1838 	ASSERT(ring_flag == MR_CONDEMNED || ring_flag  == MR_QUIESCE);
1839 
1840 	mutex_enter(&rx_ring->mr_lock);
1841 	rx_ring->mr_flag |= ring_flag;
1842 	while (rx_ring->mr_refcnt != 0)
1843 		cv_wait(&rx_ring->mr_cv, &rx_ring->mr_lock);
1844 	mutex_exit(&rx_ring->mr_lock);
1845 }
1846 
1847 /*
1848  * Please see mac_tx for details about the per cpu locking scheme
1849  */
1850 static void
1851 mac_tx_lock_all(mac_client_impl_t *mcip)
1852 {
1853 	int	i;
1854 
1855 	for (i = 0; i <= mac_tx_percpu_cnt; i++)
1856 		mutex_enter(&mcip->mci_tx_pcpu[i].pcpu_tx_lock);
1857 }
1858 
1859 static void
1860 mac_tx_unlock_all(mac_client_impl_t *mcip)
1861 {
1862 	int	i;
1863 
1864 	for (i = mac_tx_percpu_cnt; i >= 0; i--)
1865 		mutex_exit(&mcip->mci_tx_pcpu[i].pcpu_tx_lock);
1866 }
1867 
1868 static void
1869 mac_tx_unlock_allbutzero(mac_client_impl_t *mcip)
1870 {
1871 	int	i;
1872 
1873 	for (i = mac_tx_percpu_cnt; i > 0; i--)
1874 		mutex_exit(&mcip->mci_tx_pcpu[i].pcpu_tx_lock);
1875 }
1876 
1877 static int
1878 mac_tx_sum_refcnt(mac_client_impl_t *mcip)
1879 {
1880 	int	i;
1881 	int	refcnt = 0;
1882 
1883 	for (i = 0; i <= mac_tx_percpu_cnt; i++)
1884 		refcnt += mcip->mci_tx_pcpu[i].pcpu_tx_refcnt;
1885 
1886 	return (refcnt);
1887 }
1888 
1889 /*
1890  * Stop future Tx packets coming down from the client in preparation for
1891  * quiescing the Tx side. This is needed for dynamic reclaim and reassignment
1892  * of rings between clients
1893  */
1894 void
1895 mac_tx_client_block(mac_client_impl_t *mcip)
1896 {
1897 	mac_tx_lock_all(mcip);
1898 	mcip->mci_tx_flag |= MCI_TX_QUIESCE;
1899 	while (mac_tx_sum_refcnt(mcip) != 0) {
1900 		mac_tx_unlock_allbutzero(mcip);
1901 		cv_wait(&mcip->mci_tx_cv, &mcip->mci_tx_pcpu[0].pcpu_tx_lock);
1902 		mutex_exit(&mcip->mci_tx_pcpu[0].pcpu_tx_lock);
1903 		mac_tx_lock_all(mcip);
1904 	}
1905 	mac_tx_unlock_all(mcip);
1906 }
1907 
1908 void
1909 mac_tx_client_unblock(mac_client_impl_t *mcip)
1910 {
1911 	mac_tx_lock_all(mcip);
1912 	mcip->mci_tx_flag &= ~MCI_TX_QUIESCE;
1913 	mac_tx_unlock_all(mcip);
1914 	/*
1915 	 * We may fail to disable flow control for the last MAC_NOTE_TX
1916 	 * notification because the MAC client is quiesced. Send the
1917 	 * notification again.
1918 	 */
1919 	i_mac_notify(mcip->mci_mip, MAC_NOTE_TX);
1920 }
1921 
1922 /*
1923  * Wait for an SRS to quiesce. The SRS worker will signal us when the
1924  * quiesce is done.
1925  */
1926 static void
1927 mac_srs_quiesce_wait(mac_soft_ring_set_t *srs, uint_t srs_flag)
1928 {
1929 	mutex_enter(&srs->srs_lock);
1930 	while (!(srs->srs_state & srs_flag))
1931 		cv_wait(&srs->srs_quiesce_done_cv, &srs->srs_lock);
1932 	mutex_exit(&srs->srs_lock);
1933 }
1934 
1935 /*
1936  * Quiescing an Rx SRS is achieved by the following sequence. The protocol
1937  * works bottom up by cutting off packet flow from the bottommost point in the
1938  * mac, then the SRS, and then the soft rings. There are 2 use cases of this
1939  * mechanism. One is a temporary quiesce of the SRS, such as say while changing
1940  * the Rx callbacks. Another use case is Rx SRS teardown. In the former case
1941  * the QUIESCE prefix/suffix is used and in the latter the CONDEMNED is used
1942  * for the SRS and MR flags. In the former case the threads pause waiting for
1943  * a restart, while in the latter case the threads exit. The Tx SRS teardown
1944  * is also mostly similar to the above.
1945  *
1946  * 1. Stop future hardware classified packets at the lowest level in the mac.
1947  *    Remove any hardware classification rule (CONDEMNED case) and mark the
1948  *    rings as CONDEMNED or QUIESCE as appropriate. This prevents the mr_refcnt
1949  *    from increasing. Upcalls from the driver that come through hardware
1950  *    classification will be dropped in mac_rx from now on. Then we wait for
1951  *    the mr_refcnt to drop to zero. When the mr_refcnt reaches zero we are
1952  *    sure there aren't any upcall threads from the driver through hardware
1953  *    classification. In the case of SRS teardown we also remove the
1954  *    classification rule in the driver.
1955  *
1956  * 2. Stop future software classified packets by marking the flow entry with
1957  *    FE_QUIESCE or FE_CONDEMNED as appropriate which prevents the refcnt from
1958  *    increasing. We also remove the flow entry from the table in the latter
1959  *    case. Then wait for the fe_refcnt to reach an appropriate quiescent value
1960  *    that indicates there aren't any active threads using that flow entry.
1961  *
1962  * 3. Quiesce the SRS and softrings by signaling the SRS. The SRS poll thread,
1963  *    SRS worker thread, and the soft ring threads are quiesced in sequence
1964  *    with the SRS worker thread serving as a master controller. This
1965  *    mechansim is explained in mac_srs_worker_quiesce().
1966  *
1967  * The restart mechanism to reactivate the SRS and softrings is explained
1968  * in mac_srs_worker_restart(). Here we just signal the SRS worker to start the
1969  * restart sequence.
1970  */
1971 void
1972 mac_rx_srs_quiesce(mac_soft_ring_set_t *srs, uint_t srs_quiesce_flag)
1973 {
1974 	flow_entry_t	*flent = srs->srs_flent;
1975 	uint_t	mr_flag, srs_done_flag;
1976 
1977 	ASSERT(MAC_PERIM_HELD((mac_handle_t)FLENT_TO_MIP(flent)));
1978 	ASSERT(!(srs->srs_type & SRST_TX));
1979 
1980 	if (srs_quiesce_flag == SRS_CONDEMNED) {
1981 		mr_flag = MR_CONDEMNED;
1982 		srs_done_flag = SRS_CONDEMNED_DONE;
1983 		if (srs->srs_type & SRST_CLIENT_POLL_ENABLED)
1984 			mac_srs_client_poll_disable(srs->srs_mcip, srs);
1985 	} else {
1986 		ASSERT(srs_quiesce_flag == SRS_QUIESCE);
1987 		mr_flag = MR_QUIESCE;
1988 		srs_done_flag = SRS_QUIESCE_DONE;
1989 		if (srs->srs_type & SRST_CLIENT_POLL_ENABLED)
1990 			mac_srs_client_poll_quiesce(srs->srs_mcip, srs);
1991 	}
1992 
1993 	if (srs->srs_ring != NULL) {
1994 		mac_rx_ring_quiesce(srs->srs_ring, mr_flag);
1995 	} else {
1996 		/*
1997 		 * SRS is driven by software classification. In case
1998 		 * of CONDEMNED, the top level teardown functions will
1999 		 * deal with flow removal.
2000 		 */
2001 		if (srs_quiesce_flag != SRS_CONDEMNED) {
2002 			FLOW_MARK(flent, FE_QUIESCE);
2003 			mac_flow_wait(flent, FLOW_DRIVER_UPCALL);
2004 		}
2005 	}
2006 
2007 	/*
2008 	 * Signal the SRS to quiesce itself, and then cv_wait for the
2009 	 * SRS quiesce to complete. The SRS worker thread will wake us
2010 	 * up when the quiesce is complete
2011 	 */
2012 	mac_srs_signal(srs, srs_quiesce_flag);
2013 	mac_srs_quiesce_wait(srs, srs_done_flag);
2014 }
2015 
2016 /*
2017  * Remove an SRS.
2018  */
2019 void
2020 mac_rx_srs_remove(mac_soft_ring_set_t *srs)
2021 {
2022 	flow_entry_t *flent = srs->srs_flent;
2023 	int i;
2024 
2025 	mac_rx_srs_quiesce(srs, SRS_CONDEMNED);
2026 	/*
2027 	 * Locate and remove our entry in the fe_rx_srs[] array, and
2028 	 * adjust the fe_rx_srs array entries and array count by
2029 	 * moving the last entry into the vacated spot.
2030 	 */
2031 	mutex_enter(&flent->fe_lock);
2032 	for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
2033 		if (flent->fe_rx_srs[i] == srs)
2034 			break;
2035 	}
2036 
2037 	ASSERT(i != 0 && i < flent->fe_rx_srs_cnt);
2038 	if (i != flent->fe_rx_srs_cnt - 1) {
2039 		flent->fe_rx_srs[i] =
2040 		    flent->fe_rx_srs[flent->fe_rx_srs_cnt - 1];
2041 		i = flent->fe_rx_srs_cnt - 1;
2042 	}
2043 
2044 	flent->fe_rx_srs[i] = NULL;
2045 	flent->fe_rx_srs_cnt--;
2046 	mutex_exit(&flent->fe_lock);
2047 
2048 	mac_srs_free(srs);
2049 }
2050 
2051 static void
2052 mac_srs_clear_flag(mac_soft_ring_set_t *srs, uint_t flag)
2053 {
2054 	mutex_enter(&srs->srs_lock);
2055 	srs->srs_state &= ~flag;
2056 	mutex_exit(&srs->srs_lock);
2057 }
2058 
2059 void
2060 mac_rx_srs_restart(mac_soft_ring_set_t *srs)
2061 {
2062 	flow_entry_t	*flent = srs->srs_flent;
2063 	mac_ring_t	*mr;
2064 
2065 	ASSERT(MAC_PERIM_HELD((mac_handle_t)FLENT_TO_MIP(flent)));
2066 	ASSERT((srs->srs_type & SRST_TX) == 0);
2067 
2068 	/*
2069 	 * This handles a change in the number of SRSs between the quiesce and
2070 	 * and restart operation of a flow.
2071 	 */
2072 	if (!SRS_QUIESCED(srs))
2073 		return;
2074 
2075 	/*
2076 	 * Signal the SRS to restart itself. Wait for the restart to complete
2077 	 * Note that we only restart the SRS if it is not marked as
2078 	 * permanently quiesced.
2079 	 */
2080 	if (!SRS_QUIESCED_PERMANENT(srs)) {
2081 		mac_srs_signal(srs, SRS_RESTART);
2082 		mac_srs_quiesce_wait(srs, SRS_RESTART_DONE);
2083 		mac_srs_clear_flag(srs, SRS_RESTART_DONE);
2084 
2085 		mac_srs_client_poll_restart(srs->srs_mcip, srs);
2086 	}
2087 
2088 	/* Finally clear the flags to let the packets in */
2089 	mr = srs->srs_ring;
2090 	if (mr != NULL) {
2091 		MAC_RING_UNMARK(mr, MR_QUIESCE);
2092 		/* In case the ring was stopped, safely restart it */
2093 		if (mr->mr_state != MR_INUSE)
2094 			(void) mac_start_ring(mr);
2095 	} else {
2096 		FLOW_UNMARK(flent, FE_QUIESCE);
2097 	}
2098 }
2099 
2100 /*
2101  * Temporary quiesce of a flow and associated Rx SRS.
2102  * Please see block comment above mac_rx_classify_flow_rem.
2103  */
2104 /* ARGSUSED */
2105 int
2106 mac_rx_classify_flow_quiesce(flow_entry_t *flent, void *arg)
2107 {
2108 	int		i;
2109 
2110 	for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
2111 		mac_rx_srs_quiesce((mac_soft_ring_set_t *)flent->fe_rx_srs[i],
2112 		    SRS_QUIESCE);
2113 	}
2114 	return (0);
2115 }
2116 
2117 /*
2118  * Restart a flow and associated Rx SRS that has been quiesced temporarily
2119  * Please see block comment above mac_rx_classify_flow_rem
2120  */
2121 /* ARGSUSED */
2122 int
2123 mac_rx_classify_flow_restart(flow_entry_t *flent, void *arg)
2124 {
2125 	int		i;
2126 
2127 	for (i = 0; i < flent->fe_rx_srs_cnt; i++)
2128 		mac_rx_srs_restart((mac_soft_ring_set_t *)flent->fe_rx_srs[i]);
2129 
2130 	return (0);
2131 }
2132 
2133 void
2134 mac_srs_perm_quiesce(mac_client_handle_t mch, boolean_t on)
2135 {
2136 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
2137 	flow_entry_t		*flent = mcip->mci_flent;
2138 	mac_impl_t		*mip = mcip->mci_mip;
2139 	mac_soft_ring_set_t	*mac_srs;
2140 	int			i;
2141 
2142 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
2143 
2144 	if (flent == NULL)
2145 		return;
2146 
2147 	for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
2148 		mac_srs = flent->fe_rx_srs[i];
2149 		mutex_enter(&mac_srs->srs_lock);
2150 		if (on)
2151 			mac_srs->srs_state |= SRS_QUIESCE_PERM;
2152 		else
2153 			mac_srs->srs_state &= ~SRS_QUIESCE_PERM;
2154 		mutex_exit(&mac_srs->srs_lock);
2155 	}
2156 }
2157 
2158 void
2159 mac_rx_client_quiesce(mac_client_handle_t mch)
2160 {
2161 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
2162 	mac_impl_t		*mip = mcip->mci_mip;
2163 
2164 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
2165 
2166 	if (MCIP_DATAPATH_SETUP(mcip)) {
2167 		(void) mac_rx_classify_flow_quiesce(mcip->mci_flent,
2168 		    NULL);
2169 		(void) mac_flow_walk_nolock(mcip->mci_subflow_tab,
2170 		    mac_rx_classify_flow_quiesce, NULL);
2171 	}
2172 }
2173 
2174 void
2175 mac_rx_client_restart(mac_client_handle_t mch)
2176 {
2177 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
2178 	mac_impl_t		*mip = mcip->mci_mip;
2179 
2180 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
2181 
2182 	if (MCIP_DATAPATH_SETUP(mcip)) {
2183 		(void) mac_rx_classify_flow_restart(mcip->mci_flent, NULL);
2184 		(void) mac_flow_walk_nolock(mcip->mci_subflow_tab,
2185 		    mac_rx_classify_flow_restart, NULL);
2186 	}
2187 }
2188 
2189 /*
2190  * This function only quiesces the Tx SRS and softring worker threads. Callers
2191  * need to make sure that there aren't any mac client threads doing current or
2192  * future transmits in the mac before calling this function.
2193  */
2194 void
2195 mac_tx_srs_quiesce(mac_soft_ring_set_t *srs, uint_t srs_quiesce_flag)
2196 {
2197 	mac_client_impl_t	*mcip = srs->srs_mcip;
2198 
2199 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
2200 
2201 	ASSERT(srs->srs_type & SRST_TX);
2202 	ASSERT(srs_quiesce_flag == SRS_CONDEMNED ||
2203 	    srs_quiesce_flag == SRS_QUIESCE);
2204 
2205 	/*
2206 	 * Signal the SRS to quiesce itself, and then cv_wait for the
2207 	 * SRS quiesce to complete. The SRS worker thread will wake us
2208 	 * up when the quiesce is complete
2209 	 */
2210 	mac_srs_signal(srs, srs_quiesce_flag);
2211 	mac_srs_quiesce_wait(srs, srs_quiesce_flag == SRS_QUIESCE ?
2212 	    SRS_QUIESCE_DONE : SRS_CONDEMNED_DONE);
2213 }
2214 
2215 void
2216 mac_tx_srs_restart(mac_soft_ring_set_t *srs)
2217 {
2218 	/*
2219 	 * Resizing the fanout could result in creation of new SRSs.
2220 	 * They may not necessarily be in the quiesced state in which
2221 	 * case it need be restarted
2222 	 */
2223 	if (!SRS_QUIESCED(srs))
2224 		return;
2225 
2226 	mac_srs_signal(srs, SRS_RESTART);
2227 	mac_srs_quiesce_wait(srs, SRS_RESTART_DONE);
2228 	mac_srs_clear_flag(srs, SRS_RESTART_DONE);
2229 }
2230 
2231 /*
2232  * Temporary quiesce of a flow and associated Rx SRS.
2233  * Please see block comment above mac_rx_srs_quiesce
2234  */
2235 /* ARGSUSED */
2236 int
2237 mac_tx_flow_quiesce(flow_entry_t *flent, void *arg)
2238 {
2239 	/*
2240 	 * The fe_tx_srs is null for a subflow on an interface that is
2241 	 * not plumbed
2242 	 */
2243 	if (flent->fe_tx_srs != NULL)
2244 		mac_tx_srs_quiesce(flent->fe_tx_srs, SRS_QUIESCE);
2245 	return (0);
2246 }
2247 
2248 /* ARGSUSED */
2249 int
2250 mac_tx_flow_restart(flow_entry_t *flent, void *arg)
2251 {
2252 	/*
2253 	 * The fe_tx_srs is null for a subflow on an interface that is
2254 	 * not plumbed
2255 	 */
2256 	if (flent->fe_tx_srs != NULL)
2257 		mac_tx_srs_restart(flent->fe_tx_srs);
2258 	return (0);
2259 }
2260 
2261 static void
2262 i_mac_tx_client_quiesce(mac_client_handle_t mch, uint_t srs_quiesce_flag)
2263 {
2264 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
2265 
2266 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
2267 
2268 	mac_tx_client_block(mcip);
2269 	if (MCIP_TX_SRS(mcip) != NULL) {
2270 		mac_tx_srs_quiesce(MCIP_TX_SRS(mcip), srs_quiesce_flag);
2271 		(void) mac_flow_walk_nolock(mcip->mci_subflow_tab,
2272 		    mac_tx_flow_quiesce, NULL);
2273 	}
2274 }
2275 
2276 void
2277 mac_tx_client_quiesce(mac_client_handle_t mch)
2278 {
2279 	i_mac_tx_client_quiesce(mch, SRS_QUIESCE);
2280 }
2281 
2282 void
2283 mac_tx_client_condemn(mac_client_handle_t mch)
2284 {
2285 	i_mac_tx_client_quiesce(mch, SRS_CONDEMNED);
2286 }
2287 
2288 void
2289 mac_tx_client_restart(mac_client_handle_t mch)
2290 {
2291 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
2292 
2293 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
2294 
2295 	mac_tx_client_unblock(mcip);
2296 	if (MCIP_TX_SRS(mcip) != NULL) {
2297 		mac_tx_srs_restart(MCIP_TX_SRS(mcip));
2298 		(void) mac_flow_walk_nolock(mcip->mci_subflow_tab,
2299 		    mac_tx_flow_restart, NULL);
2300 	}
2301 }
2302 
2303 void
2304 mac_tx_client_flush(mac_client_impl_t *mcip)
2305 {
2306 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
2307 
2308 	mac_tx_client_quiesce((mac_client_handle_t)mcip);
2309 	mac_tx_client_restart((mac_client_handle_t)mcip);
2310 }
2311 
2312 void
2313 mac_client_quiesce(mac_client_impl_t *mcip)
2314 {
2315 	mac_rx_client_quiesce((mac_client_handle_t)mcip);
2316 	mac_tx_client_quiesce((mac_client_handle_t)mcip);
2317 }
2318 
2319 void
2320 mac_client_restart(mac_client_impl_t *mcip)
2321 {
2322 	mac_rx_client_restart((mac_client_handle_t)mcip);
2323 	mac_tx_client_restart((mac_client_handle_t)mcip);
2324 }
2325 
2326 /*
2327  * Allocate a minor number.
2328  */
2329 minor_t
2330 mac_minor_hold(boolean_t sleep)
2331 {
2332 	id_t id;
2333 
2334 	/*
2335 	 * Grab a value from the arena.
2336 	 */
2337 	atomic_inc_32(&minor_count);
2338 
2339 	if (sleep)
2340 		return ((uint_t)id_alloc(minor_ids));
2341 
2342 	if ((id = id_alloc_nosleep(minor_ids)) == -1) {
2343 		atomic_dec_32(&minor_count);
2344 		return (0);
2345 	}
2346 
2347 	return ((uint_t)id);
2348 }
2349 
2350 /*
2351  * Release a previously allocated minor number.
2352  */
2353 void
2354 mac_minor_rele(minor_t minor)
2355 {
2356 	/*
2357 	 * Return the value to the arena.
2358 	 */
2359 	id_free(minor_ids, minor);
2360 	atomic_dec_32(&minor_count);
2361 }
2362 
2363 uint32_t
2364 mac_no_notification(mac_handle_t mh)
2365 {
2366 	mac_impl_t *mip = (mac_impl_t *)mh;
2367 
2368 	return (((mip->mi_state_flags & MIS_LEGACY) != 0) ?
2369 	    mip->mi_capab_legacy.ml_unsup_note : 0);
2370 }
2371 
2372 /*
2373  * Prevent any new opens of this mac in preparation for unregister
2374  */
2375 int
2376 i_mac_disable(mac_impl_t *mip)
2377 {
2378 	mac_client_impl_t	*mcip;
2379 
2380 	rw_enter(&i_mac_impl_lock, RW_WRITER);
2381 	if (mip->mi_state_flags & MIS_DISABLED) {
2382 		/* Already disabled, return success */
2383 		rw_exit(&i_mac_impl_lock);
2384 		return (0);
2385 	}
2386 	/*
2387 	 * See if there are any other references to this mac_t (e.g., VLAN's).
2388 	 * If so return failure. If all the other checks below pass, then
2389 	 * set mi_disabled atomically under the i_mac_impl_lock to prevent
2390 	 * any new VLAN's from being created or new mac client opens of this
2391 	 * mac end point.
2392 	 */
2393 	if (mip->mi_ref > 0) {
2394 		rw_exit(&i_mac_impl_lock);
2395 		return (EBUSY);
2396 	}
2397 
2398 	/*
2399 	 * mac clients must delete all multicast groups they join before
2400 	 * closing. bcast groups are reference counted, the last client
2401 	 * to delete the group will wait till the group is physically
2402 	 * deleted. Since all clients have closed this mac end point
2403 	 * mi_bcast_ngrps must be zero at this point
2404 	 */
2405 	ASSERT(mip->mi_bcast_ngrps == 0);
2406 
2407 	/*
2408 	 * Don't let go of this if it has some flows.
2409 	 * All other code guarantees no flows are added to a disabled
2410 	 * mac, therefore it is sufficient to check for the flow table
2411 	 * only here.
2412 	 */
2413 	mcip = mac_primary_client_handle(mip);
2414 	if ((mcip != NULL) && mac_link_has_flows((mac_client_handle_t)mcip)) {
2415 		rw_exit(&i_mac_impl_lock);
2416 		return (ENOTEMPTY);
2417 	}
2418 
2419 	mip->mi_state_flags |= MIS_DISABLED;
2420 	rw_exit(&i_mac_impl_lock);
2421 	return (0);
2422 }
2423 
2424 int
2425 mac_disable_nowait(mac_handle_t mh)
2426 {
2427 	mac_impl_t	*mip = (mac_impl_t *)mh;
2428 	int err;
2429 
2430 	if ((err = i_mac_perim_enter_nowait(mip)) != 0)
2431 		return (err);
2432 	err = i_mac_disable(mip);
2433 	i_mac_perim_exit(mip);
2434 	return (err);
2435 }
2436 
2437 int
2438 mac_disable(mac_handle_t mh)
2439 {
2440 	mac_impl_t	*mip = (mac_impl_t *)mh;
2441 	int err;
2442 
2443 	i_mac_perim_enter(mip);
2444 	err = i_mac_disable(mip);
2445 	i_mac_perim_exit(mip);
2446 
2447 	/*
2448 	 * Clean up notification thread and wait for it to exit.
2449 	 */
2450 	if (err == 0)
2451 		i_mac_notify_exit(mip);
2452 
2453 	return (err);
2454 }
2455 
2456 /*
2457  * Called when the MAC instance has a non empty flow table, to de-multiplex
2458  * incoming packets to the right flow.
2459  */
2460 /* ARGSUSED */
2461 static mblk_t *
2462 mac_rx_classify(mac_impl_t *mip, mac_resource_handle_t mrh, mblk_t *mp)
2463 {
2464 	flow_entry_t	*flent = NULL;
2465 	uint_t		flags = FLOW_INBOUND;
2466 	int		err;
2467 
2468 	/*
2469 	 * If the MAC is a port of an aggregation, pass FLOW_IGNORE_VLAN
2470 	 * to mac_flow_lookup() so that the VLAN packets can be successfully
2471 	 * passed to the non-VLAN aggregation flows.
2472 	 *
2473 	 * Note that there is possibly a race between this and
2474 	 * mac_unicast_remove/add() and VLAN packets could be incorrectly
2475 	 * classified to non-VLAN flows of non-aggregation MAC clients. These
2476 	 * VLAN packets will be then filtered out by the MAC module.
2477 	 */
2478 	if ((mip->mi_state_flags & MIS_EXCLUSIVE) != 0)
2479 		flags |= FLOW_IGNORE_VLAN;
2480 
2481 	err = mac_flow_lookup(mip->mi_flow_tab, mp, flags, &flent);
2482 	if (err != 0) {
2483 		/* no registered receive function */
2484 		return (mp);
2485 	} else {
2486 		mac_client_impl_t	*mcip;
2487 
2488 		/*
2489 		 * This flent might just be an additional one on the MAC client,
2490 		 * i.e. for classification purposes (different fdesc), however
2491 		 * the resources, SRS et. al., are in the mci_flent, so if
2492 		 * this isn't the mci_flent, we need to get it.
2493 		 */
2494 		if ((mcip = flent->fe_mcip) != NULL &&
2495 		    mcip->mci_flent != flent) {
2496 			FLOW_REFRELE(flent);
2497 			flent = mcip->mci_flent;
2498 			FLOW_TRY_REFHOLD(flent, err);
2499 			if (err != 0)
2500 				return (mp);
2501 		}
2502 		(flent->fe_cb_fn)(flent->fe_cb_arg1, flent->fe_cb_arg2, mp,
2503 		    B_FALSE);
2504 		FLOW_REFRELE(flent);
2505 	}
2506 	return (NULL);
2507 }
2508 
2509 mblk_t *
2510 mac_rx_flow(mac_handle_t mh, mac_resource_handle_t mrh, mblk_t *mp_chain)
2511 {
2512 	mac_impl_t	*mip = (mac_impl_t *)mh;
2513 	mblk_t		*bp, *bp1, **bpp, *list = NULL;
2514 
2515 	/*
2516 	 * We walk the chain and attempt to classify each packet.
2517 	 * The packets that couldn't be classified will be returned
2518 	 * back to the caller.
2519 	 */
2520 	bp = mp_chain;
2521 	bpp = &list;
2522 	while (bp != NULL) {
2523 		bp1 = bp;
2524 		bp = bp->b_next;
2525 		bp1->b_next = NULL;
2526 
2527 		if (mac_rx_classify(mip, mrh, bp1) != NULL) {
2528 			*bpp = bp1;
2529 			bpp = &bp1->b_next;
2530 		}
2531 	}
2532 	return (list);
2533 }
2534 
2535 static int
2536 mac_tx_flow_srs_wakeup(flow_entry_t *flent, void *arg)
2537 {
2538 	mac_ring_handle_t ring = arg;
2539 
2540 	if (flent->fe_tx_srs)
2541 		mac_tx_srs_wakeup(flent->fe_tx_srs, ring);
2542 	return (0);
2543 }
2544 
2545 void
2546 i_mac_tx_srs_notify(mac_impl_t *mip, mac_ring_handle_t ring)
2547 {
2548 	mac_client_impl_t	*cclient;
2549 	mac_soft_ring_set_t	*mac_srs;
2550 
2551 	/*
2552 	 * After grabbing the mi_rw_lock, the list of clients can't change.
2553 	 * If there are any clients mi_disabled must be B_FALSE and can't
2554 	 * get set since there are clients. If there aren't any clients we
2555 	 * don't do anything. In any case the mip has to be valid. The driver
2556 	 * must make sure that it goes single threaded (with respect to mac
2557 	 * calls) and wait for all pending mac calls to finish before calling
2558 	 * mac_unregister.
2559 	 */
2560 	rw_enter(&i_mac_impl_lock, RW_READER);
2561 	if (mip->mi_state_flags & MIS_DISABLED) {
2562 		rw_exit(&i_mac_impl_lock);
2563 		return;
2564 	}
2565 
2566 	/*
2567 	 * Get MAC tx srs from walking mac_client_handle list.
2568 	 */
2569 	rw_enter(&mip->mi_rw_lock, RW_READER);
2570 	for (cclient = mip->mi_clients_list; cclient != NULL;
2571 	    cclient = cclient->mci_client_next) {
2572 		if ((mac_srs = MCIP_TX_SRS(cclient)) != NULL) {
2573 			mac_tx_srs_wakeup(mac_srs, ring);
2574 		} else {
2575 			/*
2576 			 * Aggr opens underlying ports in exclusive mode
2577 			 * and registers flow control callbacks using
2578 			 * mac_tx_client_notify(). When opened in
2579 			 * exclusive mode, Tx SRS won't be created
2580 			 * during mac_unicast_add().
2581 			 */
2582 			if (cclient->mci_state_flags & MCIS_EXCLUSIVE) {
2583 				mac_tx_invoke_callbacks(cclient,
2584 				    (mac_tx_cookie_t)ring);
2585 			}
2586 		}
2587 		(void) mac_flow_walk(cclient->mci_subflow_tab,
2588 		    mac_tx_flow_srs_wakeup, ring);
2589 	}
2590 	rw_exit(&mip->mi_rw_lock);
2591 	rw_exit(&i_mac_impl_lock);
2592 }
2593 
2594 /* ARGSUSED */
2595 void
2596 mac_multicast_refresh(mac_handle_t mh, mac_multicst_t refresh, void *arg,
2597     boolean_t add)
2598 {
2599 	mac_impl_t *mip = (mac_impl_t *)mh;
2600 
2601 	i_mac_perim_enter((mac_impl_t *)mh);
2602 	/*
2603 	 * If no specific refresh function was given then default to the
2604 	 * driver's m_multicst entry point.
2605 	 */
2606 	if (refresh == NULL) {
2607 		refresh = mip->mi_multicst;
2608 		arg = mip->mi_driver;
2609 	}
2610 
2611 	mac_bcast_refresh(mip, refresh, arg, add);
2612 	i_mac_perim_exit((mac_impl_t *)mh);
2613 }
2614 
2615 void
2616 mac_promisc_refresh(mac_handle_t mh, mac_setpromisc_t refresh, void *arg)
2617 {
2618 	mac_impl_t	*mip = (mac_impl_t *)mh;
2619 
2620 	/*
2621 	 * If no specific refresh function was given then default to the
2622 	 * driver's m_promisc entry point.
2623 	 */
2624 	if (refresh == NULL) {
2625 		refresh = mip->mi_setpromisc;
2626 		arg = mip->mi_driver;
2627 	}
2628 	ASSERT(refresh != NULL);
2629 
2630 	/*
2631 	 * Call the refresh function with the current promiscuity.
2632 	 */
2633 	refresh(arg, (mip->mi_devpromisc != 0));
2634 }
2635 
2636 /*
2637  * The mac client requests that the mac not to change its margin size to
2638  * be less than the specified value.  If "current" is B_TRUE, then the client
2639  * requests the mac not to change its margin size to be smaller than the
2640  * current size. Further, return the current margin size value in this case.
2641  *
2642  * We keep every requested size in an ordered list from largest to smallest.
2643  */
2644 int
2645 mac_margin_add(mac_handle_t mh, uint32_t *marginp, boolean_t current)
2646 {
2647 	mac_impl_t		*mip = (mac_impl_t *)mh;
2648 	mac_margin_req_t	**pp, *p;
2649 	int			err = 0;
2650 
2651 	rw_enter(&(mip->mi_rw_lock), RW_WRITER);
2652 	if (current)
2653 		*marginp = mip->mi_margin;
2654 
2655 	/*
2656 	 * If the current margin value cannot satisfy the margin requested,
2657 	 * return ENOTSUP directly.
2658 	 */
2659 	if (*marginp > mip->mi_margin) {
2660 		err = ENOTSUP;
2661 		goto done;
2662 	}
2663 
2664 	/*
2665 	 * Check whether the given margin is already in the list. If so,
2666 	 * bump the reference count.
2667 	 */
2668 	for (pp = &mip->mi_mmrp; (p = *pp) != NULL; pp = &p->mmr_nextp) {
2669 		if (p->mmr_margin == *marginp) {
2670 			/*
2671 			 * The margin requested is already in the list,
2672 			 * so just bump the reference count.
2673 			 */
2674 			p->mmr_ref++;
2675 			goto done;
2676 		}
2677 		if (p->mmr_margin < *marginp)
2678 			break;
2679 	}
2680 
2681 
2682 	p = kmem_zalloc(sizeof (mac_margin_req_t), KM_SLEEP);
2683 	p->mmr_margin = *marginp;
2684 	p->mmr_ref++;
2685 	p->mmr_nextp = *pp;
2686 	*pp = p;
2687 
2688 done:
2689 	rw_exit(&(mip->mi_rw_lock));
2690 	return (err);
2691 }
2692 
2693 /*
2694  * The mac client requests to cancel its previous mac_margin_add() request.
2695  * We remove the requested margin size from the list.
2696  */
2697 int
2698 mac_margin_remove(mac_handle_t mh, uint32_t margin)
2699 {
2700 	mac_impl_t		*mip = (mac_impl_t *)mh;
2701 	mac_margin_req_t	**pp, *p;
2702 	int			err = 0;
2703 
2704 	rw_enter(&(mip->mi_rw_lock), RW_WRITER);
2705 	/*
2706 	 * Find the entry in the list for the given margin.
2707 	 */
2708 	for (pp = &(mip->mi_mmrp); (p = *pp) != NULL; pp = &(p->mmr_nextp)) {
2709 		if (p->mmr_margin == margin) {
2710 			if (--p->mmr_ref == 0)
2711 				break;
2712 
2713 			/*
2714 			 * There is still a reference to this address so
2715 			 * there's nothing more to do.
2716 			 */
2717 			goto done;
2718 		}
2719 	}
2720 
2721 	/*
2722 	 * We did not find an entry for the given margin.
2723 	 */
2724 	if (p == NULL) {
2725 		err = ENOENT;
2726 		goto done;
2727 	}
2728 
2729 	ASSERT(p->mmr_ref == 0);
2730 
2731 	/*
2732 	 * Remove it from the list.
2733 	 */
2734 	*pp = p->mmr_nextp;
2735 	kmem_free(p, sizeof (mac_margin_req_t));
2736 done:
2737 	rw_exit(&(mip->mi_rw_lock));
2738 	return (err);
2739 }
2740 
2741 boolean_t
2742 mac_margin_update(mac_handle_t mh, uint32_t margin)
2743 {
2744 	mac_impl_t	*mip = (mac_impl_t *)mh;
2745 	uint32_t	margin_needed = 0;
2746 
2747 	rw_enter(&(mip->mi_rw_lock), RW_WRITER);
2748 
2749 	if (mip->mi_mmrp != NULL)
2750 		margin_needed = mip->mi_mmrp->mmr_margin;
2751 
2752 	if (margin_needed <= margin)
2753 		mip->mi_margin = margin;
2754 
2755 	rw_exit(&(mip->mi_rw_lock));
2756 
2757 	if (margin_needed <= margin)
2758 		i_mac_notify(mip, MAC_NOTE_MARGIN);
2759 
2760 	return (margin_needed <= margin);
2761 }
2762 
2763 /*
2764  * MAC clients use this interface to request that a MAC device not change its
2765  * MTU below the specified amount. At this time, that amount must be within the
2766  * range of the device's current minimum and the device's current maximum. eg. a
2767  * client cannot request a 3000 byte MTU when the device's MTU is currently
2768  * 2000.
2769  *
2770  * If "current" is set to B_TRUE, then the request is to simply to reserve the
2771  * current underlying mac's maximum for this mac client and return it in mtup.
2772  */
2773 int
2774 mac_mtu_add(mac_handle_t mh, uint32_t *mtup, boolean_t current)
2775 {
2776 	mac_impl_t		*mip = (mac_impl_t *)mh;
2777 	mac_mtu_req_t		*prev, *cur;
2778 	mac_propval_range_t	mpr;
2779 	int			err;
2780 
2781 	i_mac_perim_enter(mip);
2782 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
2783 
2784 	if (current == B_TRUE)
2785 		*mtup = mip->mi_sdu_max;
2786 	mpr.mpr_count = 1;
2787 	err = mac_prop_info(mh, MAC_PROP_MTU, "mtu", NULL, 0, &mpr, NULL);
2788 	if (err != 0) {
2789 		rw_exit(&mip->mi_rw_lock);
2790 		i_mac_perim_exit(mip);
2791 		return (err);
2792 	}
2793 
2794 	if (*mtup > mip->mi_sdu_max ||
2795 	    *mtup < mpr.mpr_range_uint32[0].mpur_min) {
2796 		rw_exit(&mip->mi_rw_lock);
2797 		i_mac_perim_exit(mip);
2798 		return (ENOTSUP);
2799 	}
2800 
2801 	prev = NULL;
2802 	for (cur = mip->mi_mtrp; cur != NULL; cur = cur->mtr_nextp) {
2803 		if (*mtup == cur->mtr_mtu) {
2804 			cur->mtr_ref++;
2805 			rw_exit(&mip->mi_rw_lock);
2806 			i_mac_perim_exit(mip);
2807 			return (0);
2808 		}
2809 
2810 		if (*mtup > cur->mtr_mtu)
2811 			break;
2812 
2813 		prev = cur;
2814 	}
2815 
2816 	cur = kmem_alloc(sizeof (mac_mtu_req_t), KM_SLEEP);
2817 	cur->mtr_mtu = *mtup;
2818 	cur->mtr_ref = 1;
2819 	if (prev != NULL) {
2820 		cur->mtr_nextp = prev->mtr_nextp;
2821 		prev->mtr_nextp = cur;
2822 	} else {
2823 		cur->mtr_nextp = mip->mi_mtrp;
2824 		mip->mi_mtrp = cur;
2825 	}
2826 
2827 	rw_exit(&mip->mi_rw_lock);
2828 	i_mac_perim_exit(mip);
2829 	return (0);
2830 }
2831 
2832 int
2833 mac_mtu_remove(mac_handle_t mh, uint32_t mtu)
2834 {
2835 	mac_impl_t *mip = (mac_impl_t *)mh;
2836 	mac_mtu_req_t *cur, *prev;
2837 
2838 	i_mac_perim_enter(mip);
2839 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
2840 
2841 	prev = NULL;
2842 	for (cur = mip->mi_mtrp; cur != NULL; cur = cur->mtr_nextp) {
2843 		if (cur->mtr_mtu == mtu) {
2844 			ASSERT(cur->mtr_ref > 0);
2845 			cur->mtr_ref--;
2846 			if (cur->mtr_ref == 0) {
2847 				if (prev == NULL) {
2848 					mip->mi_mtrp = cur->mtr_nextp;
2849 				} else {
2850 					prev->mtr_nextp = cur->mtr_nextp;
2851 				}
2852 				kmem_free(cur, sizeof (mac_mtu_req_t));
2853 			}
2854 			rw_exit(&mip->mi_rw_lock);
2855 			i_mac_perim_exit(mip);
2856 			return (0);
2857 		}
2858 
2859 		prev = cur;
2860 	}
2861 
2862 	rw_exit(&mip->mi_rw_lock);
2863 	i_mac_perim_exit(mip);
2864 	return (ENOENT);
2865 }
2866 
2867 /*
2868  * MAC Type Plugin functions.
2869  */
2870 
2871 mactype_t *
2872 mactype_getplugin(const char *pname)
2873 {
2874 	mactype_t	*mtype = NULL;
2875 	boolean_t	tried_modload = B_FALSE;
2876 
2877 	mutex_enter(&i_mactype_lock);
2878 
2879 find_registered_mactype:
2880 	if (mod_hash_find(i_mactype_hash, (mod_hash_key_t)pname,
2881 	    (mod_hash_val_t *)&mtype) != 0) {
2882 		if (!tried_modload) {
2883 			/*
2884 			 * If the plugin has not yet been loaded, then
2885 			 * attempt to load it now.  If modload() succeeds,
2886 			 * the plugin should have registered using
2887 			 * mactype_register(), in which case we can go back
2888 			 * and attempt to find it again.
2889 			 */
2890 			if (modload(MACTYPE_KMODDIR, (char *)pname) != -1) {
2891 				tried_modload = B_TRUE;
2892 				goto find_registered_mactype;
2893 			}
2894 		}
2895 	} else {
2896 		/*
2897 		 * Note that there's no danger that the plugin we've loaded
2898 		 * could be unloaded between the modload() step and the
2899 		 * reference count bump here, as we're holding
2900 		 * i_mactype_lock, which mactype_unregister() also holds.
2901 		 */
2902 		atomic_inc_32(&mtype->mt_ref);
2903 	}
2904 
2905 	mutex_exit(&i_mactype_lock);
2906 	return (mtype);
2907 }
2908 
2909 mactype_register_t *
2910 mactype_alloc(uint_t mactype_version)
2911 {
2912 	mactype_register_t *mtrp;
2913 
2914 	/*
2915 	 * Make sure there isn't a version mismatch between the plugin and
2916 	 * the framework.  In the future, if multiple versions are
2917 	 * supported, this check could become more sophisticated.
2918 	 */
2919 	if (mactype_version != MACTYPE_VERSION)
2920 		return (NULL);
2921 
2922 	mtrp = kmem_zalloc(sizeof (mactype_register_t), KM_SLEEP);
2923 	mtrp->mtr_version = mactype_version;
2924 	return (mtrp);
2925 }
2926 
2927 void
2928 mactype_free(mactype_register_t *mtrp)
2929 {
2930 	kmem_free(mtrp, sizeof (mactype_register_t));
2931 }
2932 
2933 int
2934 mactype_register(mactype_register_t *mtrp)
2935 {
2936 	mactype_t	*mtp;
2937 	mactype_ops_t	*ops = mtrp->mtr_ops;
2938 
2939 	/* Do some sanity checking before we register this MAC type. */
2940 	if (mtrp->mtr_ident == NULL || ops == NULL)
2941 		return (EINVAL);
2942 
2943 	/*
2944 	 * Verify that all mandatory callbacks are set in the ops
2945 	 * vector.
2946 	 */
2947 	if (ops->mtops_unicst_verify == NULL ||
2948 	    ops->mtops_multicst_verify == NULL ||
2949 	    ops->mtops_sap_verify == NULL ||
2950 	    ops->mtops_header == NULL ||
2951 	    ops->mtops_header_info == NULL) {
2952 		return (EINVAL);
2953 	}
2954 
2955 	mtp = kmem_zalloc(sizeof (*mtp), KM_SLEEP);
2956 	mtp->mt_ident = mtrp->mtr_ident;
2957 	mtp->mt_ops = *ops;
2958 	mtp->mt_type = mtrp->mtr_mactype;
2959 	mtp->mt_nativetype = mtrp->mtr_nativetype;
2960 	mtp->mt_addr_length = mtrp->mtr_addrlen;
2961 	if (mtrp->mtr_brdcst_addr != NULL) {
2962 		mtp->mt_brdcst_addr = kmem_alloc(mtrp->mtr_addrlen, KM_SLEEP);
2963 		bcopy(mtrp->mtr_brdcst_addr, mtp->mt_brdcst_addr,
2964 		    mtrp->mtr_addrlen);
2965 	}
2966 
2967 	mtp->mt_stats = mtrp->mtr_stats;
2968 	mtp->mt_statcount = mtrp->mtr_statcount;
2969 
2970 	mtp->mt_mapping = mtrp->mtr_mapping;
2971 	mtp->mt_mappingcount = mtrp->mtr_mappingcount;
2972 
2973 	if (mod_hash_insert(i_mactype_hash,
2974 	    (mod_hash_key_t)mtp->mt_ident, (mod_hash_val_t)mtp) != 0) {
2975 		kmem_free(mtp->mt_brdcst_addr, mtp->mt_addr_length);
2976 		kmem_free(mtp, sizeof (*mtp));
2977 		return (EEXIST);
2978 	}
2979 	return (0);
2980 }
2981 
2982 int
2983 mactype_unregister(const char *ident)
2984 {
2985 	mactype_t	*mtp;
2986 	mod_hash_val_t	val;
2987 	int		err;
2988 
2989 	/*
2990 	 * Let's not allow MAC drivers to use this plugin while we're
2991 	 * trying to unregister it.  Holding i_mactype_lock also prevents a
2992 	 * plugin from unregistering while a MAC driver is attempting to
2993 	 * hold a reference to it in i_mactype_getplugin().
2994 	 */
2995 	mutex_enter(&i_mactype_lock);
2996 
2997 	if ((err = mod_hash_find(i_mactype_hash, (mod_hash_key_t)ident,
2998 	    (mod_hash_val_t *)&mtp)) != 0) {
2999 		/* A plugin is trying to unregister, but it never registered. */
3000 		err = ENXIO;
3001 		goto done;
3002 	}
3003 
3004 	if (mtp->mt_ref != 0) {
3005 		err = EBUSY;
3006 		goto done;
3007 	}
3008 
3009 	err = mod_hash_remove(i_mactype_hash, (mod_hash_key_t)ident, &val);
3010 	ASSERT(err == 0);
3011 	if (err != 0) {
3012 		/* This should never happen, thus the ASSERT() above. */
3013 		err = EINVAL;
3014 		goto done;
3015 	}
3016 	ASSERT(mtp == (mactype_t *)val);
3017 
3018 	if (mtp->mt_brdcst_addr != NULL)
3019 		kmem_free(mtp->mt_brdcst_addr, mtp->mt_addr_length);
3020 	kmem_free(mtp, sizeof (mactype_t));
3021 done:
3022 	mutex_exit(&i_mactype_lock);
3023 	return (err);
3024 }
3025 
3026 /*
3027  * Checks the size of the value size specified for a property as
3028  * part of a property operation. Returns B_TRUE if the size is
3029  * correct, B_FALSE otherwise.
3030  */
3031 boolean_t
3032 mac_prop_check_size(mac_prop_id_t id, uint_t valsize, boolean_t is_range)
3033 {
3034 	uint_t minsize = 0;
3035 
3036 	if (is_range)
3037 		return (valsize >= sizeof (mac_propval_range_t));
3038 
3039 	switch (id) {
3040 	case MAC_PROP_ZONE:
3041 		minsize = sizeof (dld_ioc_zid_t);
3042 		break;
3043 	case MAC_PROP_AUTOPUSH:
3044 		if (valsize != 0)
3045 			minsize = sizeof (struct dlautopush);
3046 		break;
3047 	case MAC_PROP_TAGMODE:
3048 		minsize = sizeof (link_tagmode_t);
3049 		break;
3050 	case MAC_PROP_RESOURCE:
3051 	case MAC_PROP_RESOURCE_EFF:
3052 		minsize = sizeof (mac_resource_props_t);
3053 		break;
3054 	case MAC_PROP_DUPLEX:
3055 		minsize = sizeof (link_duplex_t);
3056 		break;
3057 	case MAC_PROP_SPEED:
3058 		minsize = sizeof (uint64_t);
3059 		break;
3060 	case MAC_PROP_STATUS:
3061 		minsize = sizeof (link_state_t);
3062 		break;
3063 	case MAC_PROP_AUTONEG:
3064 	case MAC_PROP_EN_AUTONEG:
3065 		minsize = sizeof (uint8_t);
3066 		break;
3067 	case MAC_PROP_MTU:
3068 	case MAC_PROP_LLIMIT:
3069 	case MAC_PROP_LDECAY:
3070 		minsize = sizeof (uint32_t);
3071 		break;
3072 	case MAC_PROP_FLOWCTRL:
3073 		minsize = sizeof (link_flowctrl_t);
3074 		break;
3075 	case MAC_PROP_ADV_5000FDX_CAP:
3076 	case MAC_PROP_EN_5000FDX_CAP:
3077 	case MAC_PROP_ADV_2500FDX_CAP:
3078 	case MAC_PROP_EN_2500FDX_CAP:
3079 	case MAC_PROP_ADV_100GFDX_CAP:
3080 	case MAC_PROP_EN_100GFDX_CAP:
3081 	case MAC_PROP_ADV_50GFDX_CAP:
3082 	case MAC_PROP_EN_50GFDX_CAP:
3083 	case MAC_PROP_ADV_40GFDX_CAP:
3084 	case MAC_PROP_EN_40GFDX_CAP:
3085 	case MAC_PROP_ADV_25GFDX_CAP:
3086 	case MAC_PROP_EN_25GFDX_CAP:
3087 	case MAC_PROP_ADV_10GFDX_CAP:
3088 	case MAC_PROP_EN_10GFDX_CAP:
3089 	case MAC_PROP_ADV_1000HDX_CAP:
3090 	case MAC_PROP_EN_1000HDX_CAP:
3091 	case MAC_PROP_ADV_100FDX_CAP:
3092 	case MAC_PROP_EN_100FDX_CAP:
3093 	case MAC_PROP_ADV_100HDX_CAP:
3094 	case MAC_PROP_EN_100HDX_CAP:
3095 	case MAC_PROP_ADV_10FDX_CAP:
3096 	case MAC_PROP_EN_10FDX_CAP:
3097 	case MAC_PROP_ADV_10HDX_CAP:
3098 	case MAC_PROP_EN_10HDX_CAP:
3099 	case MAC_PROP_ADV_100T4_CAP:
3100 	case MAC_PROP_EN_100T4_CAP:
3101 		minsize = sizeof (uint8_t);
3102 		break;
3103 	case MAC_PROP_PVID:
3104 		minsize = sizeof (uint16_t);
3105 		break;
3106 	case MAC_PROP_IPTUN_HOPLIMIT:
3107 		minsize = sizeof (uint32_t);
3108 		break;
3109 	case MAC_PROP_IPTUN_ENCAPLIMIT:
3110 		minsize = sizeof (uint32_t);
3111 		break;
3112 	case MAC_PROP_MAX_TX_RINGS_AVAIL:
3113 	case MAC_PROP_MAX_RX_RINGS_AVAIL:
3114 	case MAC_PROP_MAX_RXHWCLNT_AVAIL:
3115 	case MAC_PROP_MAX_TXHWCLNT_AVAIL:
3116 		minsize = sizeof (uint_t);
3117 		break;
3118 	case MAC_PROP_WL_ESSID:
3119 		minsize = sizeof (wl_linkstatus_t);
3120 		break;
3121 	case MAC_PROP_WL_BSSID:
3122 		minsize = sizeof (wl_bssid_t);
3123 		break;
3124 	case MAC_PROP_WL_BSSTYPE:
3125 		minsize = sizeof (wl_bss_type_t);
3126 		break;
3127 	case MAC_PROP_WL_LINKSTATUS:
3128 		minsize = sizeof (wl_linkstatus_t);
3129 		break;
3130 	case MAC_PROP_WL_DESIRED_RATES:
3131 		minsize = sizeof (wl_rates_t);
3132 		break;
3133 	case MAC_PROP_WL_SUPPORTED_RATES:
3134 		minsize = sizeof (wl_rates_t);
3135 		break;
3136 	case MAC_PROP_WL_AUTH_MODE:
3137 		minsize = sizeof (wl_authmode_t);
3138 		break;
3139 	case MAC_PROP_WL_ENCRYPTION:
3140 		minsize = sizeof (wl_encryption_t);
3141 		break;
3142 	case MAC_PROP_WL_RSSI:
3143 		minsize = sizeof (wl_rssi_t);
3144 		break;
3145 	case MAC_PROP_WL_PHY_CONFIG:
3146 		minsize = sizeof (wl_phy_conf_t);
3147 		break;
3148 	case MAC_PROP_WL_CAPABILITY:
3149 		minsize = sizeof (wl_capability_t);
3150 		break;
3151 	case MAC_PROP_WL_WPA:
3152 		minsize = sizeof (wl_wpa_t);
3153 		break;
3154 	case MAC_PROP_WL_SCANRESULTS:
3155 		minsize = sizeof (wl_wpa_ess_t);
3156 		break;
3157 	case MAC_PROP_WL_POWER_MODE:
3158 		minsize = sizeof (wl_ps_mode_t);
3159 		break;
3160 	case MAC_PROP_WL_RADIO:
3161 		minsize = sizeof (wl_radio_t);
3162 		break;
3163 	case MAC_PROP_WL_ESS_LIST:
3164 		minsize = sizeof (wl_ess_list_t);
3165 		break;
3166 	case MAC_PROP_WL_KEY_TAB:
3167 		minsize = sizeof (wl_wep_key_tab_t);
3168 		break;
3169 	case MAC_PROP_WL_CREATE_IBSS:
3170 		minsize = sizeof (wl_create_ibss_t);
3171 		break;
3172 	case MAC_PROP_WL_SETOPTIE:
3173 		minsize = sizeof (wl_wpa_ie_t);
3174 		break;
3175 	case MAC_PROP_WL_DELKEY:
3176 		minsize = sizeof (wl_del_key_t);
3177 		break;
3178 	case MAC_PROP_WL_KEY:
3179 		minsize = sizeof (wl_key_t);
3180 		break;
3181 	case MAC_PROP_WL_MLME:
3182 		minsize = sizeof (wl_mlme_t);
3183 		break;
3184 	case MAC_PROP_VN_PROMISC_FILTERED:
3185 		minsize = sizeof (boolean_t);
3186 		break;
3187 	}
3188 
3189 	return (valsize >= minsize);
3190 }
3191 
3192 /*
3193  * mac_set_prop() sets MAC or hardware driver properties:
3194  *
3195  * - MAC-managed properties such as resource properties include maxbw,
3196  *   priority, and cpu binding list, as well as the default port VID
3197  *   used by bridging. These properties are consumed by the MAC layer
3198  *   itself and not passed down to the driver. For resource control
3199  *   properties, this function invokes mac_set_resources() which will
3200  *   cache the property value in mac_impl_t and may call
3201  *   mac_client_set_resource() to update property value of the primary
3202  *   mac client, if it exists.
3203  *
3204  * - Properties which act on the hardware and must be passed to the
3205  *   driver, such as MTU, through the driver's mc_setprop() entry point.
3206  */
3207 int
3208 mac_set_prop(mac_handle_t mh, mac_prop_id_t id, char *name, void *val,
3209     uint_t valsize)
3210 {
3211 	int err = ENOTSUP;
3212 	mac_impl_t *mip = (mac_impl_t *)mh;
3213 
3214 	ASSERT(MAC_PERIM_HELD(mh));
3215 
3216 	switch (id) {
3217 	case MAC_PROP_RESOURCE: {
3218 		mac_resource_props_t *mrp;
3219 
3220 		/* call mac_set_resources() for MAC properties */
3221 		ASSERT(valsize >= sizeof (mac_resource_props_t));
3222 		mrp = kmem_zalloc(sizeof (*mrp), KM_SLEEP);
3223 		bcopy(val, mrp, sizeof (*mrp));
3224 		err = mac_set_resources(mh, mrp);
3225 		kmem_free(mrp, sizeof (*mrp));
3226 		break;
3227 	}
3228 
3229 	case MAC_PROP_PVID:
3230 		ASSERT(valsize >= sizeof (uint16_t));
3231 		if (mip->mi_state_flags & MIS_IS_VNIC)
3232 			return (EINVAL);
3233 		err = mac_set_pvid(mh, *(uint16_t *)val);
3234 		break;
3235 
3236 	case MAC_PROP_MTU: {
3237 		uint32_t mtu;
3238 
3239 		ASSERT(valsize >= sizeof (uint32_t));
3240 		bcopy(val, &mtu, sizeof (mtu));
3241 		err = mac_set_mtu(mh, mtu, NULL);
3242 		break;
3243 	}
3244 
3245 	case MAC_PROP_LLIMIT:
3246 	case MAC_PROP_LDECAY: {
3247 		uint32_t learnval;
3248 
3249 		if (valsize < sizeof (learnval) ||
3250 		    (mip->mi_state_flags & MIS_IS_VNIC))
3251 			return (EINVAL);
3252 		bcopy(val, &learnval, sizeof (learnval));
3253 		if (learnval == 0 && id == MAC_PROP_LDECAY)
3254 			return (EINVAL);
3255 		if (id == MAC_PROP_LLIMIT)
3256 			mip->mi_llimit = learnval;
3257 		else
3258 			mip->mi_ldecay = learnval;
3259 		err = 0;
3260 		break;
3261 	}
3262 
3263 	default:
3264 		/* For other driver properties, call driver's callback */
3265 		if (mip->mi_callbacks->mc_callbacks & MC_SETPROP) {
3266 			err = mip->mi_callbacks->mc_setprop(mip->mi_driver,
3267 			    name, id, valsize, val);
3268 		}
3269 	}
3270 	return (err);
3271 }
3272 
3273 /*
3274  * mac_get_prop() gets MAC or device driver properties.
3275  *
3276  * If the property is a driver property, mac_get_prop() calls driver's callback
3277  * entry point to get it.
3278  * If the property is a MAC property, mac_get_prop() invokes mac_get_resources()
3279  * which returns the cached value in mac_impl_t.
3280  */
3281 int
3282 mac_get_prop(mac_handle_t mh, mac_prop_id_t id, char *name, void *val,
3283     uint_t valsize)
3284 {
3285 	int err = ENOTSUP;
3286 	mac_impl_t *mip = (mac_impl_t *)mh;
3287 	uint_t	rings;
3288 	uint_t	vlinks;
3289 
3290 	bzero(val, valsize);
3291 
3292 	switch (id) {
3293 	case MAC_PROP_RESOURCE: {
3294 		mac_resource_props_t *mrp;
3295 
3296 		/* If mac property, read from cache */
3297 		ASSERT(valsize >= sizeof (mac_resource_props_t));
3298 		mrp = kmem_zalloc(sizeof (*mrp), KM_SLEEP);
3299 		mac_get_resources(mh, mrp);
3300 		bcopy(mrp, val, sizeof (*mrp));
3301 		kmem_free(mrp, sizeof (*mrp));
3302 		return (0);
3303 	}
3304 	case MAC_PROP_RESOURCE_EFF: {
3305 		mac_resource_props_t *mrp;
3306 
3307 		/* If mac effective property, read from client */
3308 		ASSERT(valsize >= sizeof (mac_resource_props_t));
3309 		mrp = kmem_zalloc(sizeof (*mrp), KM_SLEEP);
3310 		mac_get_effective_resources(mh, mrp);
3311 		bcopy(mrp, val, sizeof (*mrp));
3312 		kmem_free(mrp, sizeof (*mrp));
3313 		return (0);
3314 	}
3315 
3316 	case MAC_PROP_PVID:
3317 		ASSERT(valsize >= sizeof (uint16_t));
3318 		if (mip->mi_state_flags & MIS_IS_VNIC)
3319 			return (EINVAL);
3320 		*(uint16_t *)val = mac_get_pvid(mh);
3321 		return (0);
3322 
3323 	case MAC_PROP_LLIMIT:
3324 	case MAC_PROP_LDECAY:
3325 		ASSERT(valsize >= sizeof (uint32_t));
3326 		if (mip->mi_state_flags & MIS_IS_VNIC)
3327 			return (EINVAL);
3328 		if (id == MAC_PROP_LLIMIT)
3329 			bcopy(&mip->mi_llimit, val, sizeof (mip->mi_llimit));
3330 		else
3331 			bcopy(&mip->mi_ldecay, val, sizeof (mip->mi_ldecay));
3332 		return (0);
3333 
3334 	case MAC_PROP_MTU: {
3335 		uint32_t sdu;
3336 
3337 		ASSERT(valsize >= sizeof (uint32_t));
3338 		mac_sdu_get2(mh, NULL, &sdu, NULL);
3339 		bcopy(&sdu, val, sizeof (sdu));
3340 
3341 		return (0);
3342 	}
3343 	case MAC_PROP_STATUS: {
3344 		link_state_t link_state;
3345 
3346 		if (valsize < sizeof (link_state))
3347 			return (EINVAL);
3348 		link_state = mac_link_get(mh);
3349 		bcopy(&link_state, val, sizeof (link_state));
3350 
3351 		return (0);
3352 	}
3353 
3354 	case MAC_PROP_MAX_RX_RINGS_AVAIL:
3355 	case MAC_PROP_MAX_TX_RINGS_AVAIL:
3356 		ASSERT(valsize >= sizeof (uint_t));
3357 		rings = id == MAC_PROP_MAX_RX_RINGS_AVAIL ?
3358 		    mac_rxavail_get(mh) : mac_txavail_get(mh);
3359 		bcopy(&rings, val, sizeof (uint_t));
3360 		return (0);
3361 
3362 	case MAC_PROP_MAX_RXHWCLNT_AVAIL:
3363 	case MAC_PROP_MAX_TXHWCLNT_AVAIL:
3364 		ASSERT(valsize >= sizeof (uint_t));
3365 		vlinks = id == MAC_PROP_MAX_RXHWCLNT_AVAIL ?
3366 		    mac_rxhwlnksavail_get(mh) : mac_txhwlnksavail_get(mh);
3367 		bcopy(&vlinks, val, sizeof (uint_t));
3368 		return (0);
3369 
3370 	case MAC_PROP_RXRINGSRANGE:
3371 	case MAC_PROP_TXRINGSRANGE:
3372 		/*
3373 		 * The value for these properties are returned through
3374 		 * the MAC_PROP_RESOURCE property.
3375 		 */
3376 		return (0);
3377 
3378 	default:
3379 		break;
3380 
3381 	}
3382 
3383 	/* If driver property, request from driver */
3384 	if (mip->mi_callbacks->mc_callbacks & MC_GETPROP) {
3385 		err = mip->mi_callbacks->mc_getprop(mip->mi_driver, name, id,
3386 		    valsize, val);
3387 	}
3388 
3389 	return (err);
3390 }
3391 
3392 /*
3393  * Helper function to initialize the range structure for use in
3394  * mac_get_prop. If the type can be other than uint32, we can
3395  * pass that as an arg.
3396  */
3397 static void
3398 _mac_set_range(mac_propval_range_t *range, uint32_t min, uint32_t max)
3399 {
3400 	range->mpr_count = 1;
3401 	range->mpr_type = MAC_PROPVAL_UINT32;
3402 	range->mpr_range_uint32[0].mpur_min = min;
3403 	range->mpr_range_uint32[0].mpur_max = max;
3404 }
3405 
3406 /*
3407  * Returns information about the specified property, such as default
3408  * values or permissions.
3409  */
3410 int
3411 mac_prop_info(mac_handle_t mh, mac_prop_id_t id, char *name,
3412     void *default_val, uint_t default_size, mac_propval_range_t *range,
3413     uint_t *perm)
3414 {
3415 	mac_prop_info_state_t state;
3416 	mac_impl_t *mip = (mac_impl_t *)mh;
3417 	uint_t	max;
3418 
3419 	/*
3420 	 * A property is read/write by default unless the driver says
3421 	 * otherwise.
3422 	 */
3423 	if (perm != NULL)
3424 		*perm = MAC_PROP_PERM_RW;
3425 
3426 	if (default_val != NULL)
3427 		bzero(default_val, default_size);
3428 
3429 	/*
3430 	 * First, handle framework properties for which we don't need to
3431 	 * involve the driver.
3432 	 */
3433 	switch (id) {
3434 	case MAC_PROP_RESOURCE:
3435 	case MAC_PROP_PVID:
3436 	case MAC_PROP_LLIMIT:
3437 	case MAC_PROP_LDECAY:
3438 		return (0);
3439 
3440 	case MAC_PROP_MAX_RX_RINGS_AVAIL:
3441 	case MAC_PROP_MAX_TX_RINGS_AVAIL:
3442 	case MAC_PROP_MAX_RXHWCLNT_AVAIL:
3443 	case MAC_PROP_MAX_TXHWCLNT_AVAIL:
3444 		if (perm != NULL)
3445 			*perm = MAC_PROP_PERM_READ;
3446 		return (0);
3447 
3448 	case MAC_PROP_RXRINGSRANGE:
3449 	case MAC_PROP_TXRINGSRANGE:
3450 		/*
3451 		 * Currently, we support range for RX and TX rings properties.
3452 		 * When we extend this support to maxbw, cpus and priority,
3453 		 * we should move this to mac_get_resources.
3454 		 * There is no default value for RX or TX rings.
3455 		 */
3456 		if ((mip->mi_state_flags & MIS_IS_VNIC) &&
3457 		    mac_is_vnic_primary(mh)) {
3458 			/*
3459 			 * We don't support setting rings for a VLAN
3460 			 * data link because it shares its ring with the
3461 			 * primary MAC client.
3462 			 */
3463 			if (perm != NULL)
3464 				*perm = MAC_PROP_PERM_READ;
3465 			if (range != NULL)
3466 				range->mpr_count = 0;
3467 		} else if (range != NULL) {
3468 			if (mip->mi_state_flags & MIS_IS_VNIC)
3469 				mh = mac_get_lower_mac_handle(mh);
3470 			mip = (mac_impl_t *)mh;
3471 			if ((id == MAC_PROP_RXRINGSRANGE &&
3472 			    mip->mi_rx_group_type == MAC_GROUP_TYPE_STATIC) ||
3473 			    (id == MAC_PROP_TXRINGSRANGE &&
3474 			    mip->mi_tx_group_type == MAC_GROUP_TYPE_STATIC)) {
3475 				if (id == MAC_PROP_RXRINGSRANGE) {
3476 					if ((mac_rxhwlnksavail_get(mh) +
3477 					    mac_rxhwlnksrsvd_get(mh)) <= 1) {
3478 						/*
3479 						 * doesn't support groups or
3480 						 * rings
3481 						 */
3482 						range->mpr_count = 0;
3483 					} else {
3484 						/*
3485 						 * supports specifying groups,
3486 						 * but not rings
3487 						 */
3488 						_mac_set_range(range, 0, 0);
3489 					}
3490 				} else {
3491 					if ((mac_txhwlnksavail_get(mh) +
3492 					    mac_txhwlnksrsvd_get(mh)) <= 1) {
3493 						/*
3494 						 * doesn't support groups or
3495 						 * rings
3496 						 */
3497 						range->mpr_count = 0;
3498 					} else {
3499 						/*
3500 						 * supports specifying groups,
3501 						 * but not rings
3502 						 */
3503 						_mac_set_range(range, 0, 0);
3504 					}
3505 				}
3506 			} else {
3507 				max = id == MAC_PROP_RXRINGSRANGE ?
3508 				    mac_rxavail_get(mh) + mac_rxrsvd_get(mh) :
3509 				    mac_txavail_get(mh) + mac_txrsvd_get(mh);
3510 				if (max <= 1) {
3511 					/*
3512 					 * doesn't support groups or
3513 					 * rings
3514 					 */
3515 					range->mpr_count = 0;
3516 				} else  {
3517 					/*
3518 					 * -1 because we have to leave out the
3519 					 * default ring.
3520 					 */
3521 					_mac_set_range(range, 1, max - 1);
3522 				}
3523 			}
3524 		}
3525 		return (0);
3526 
3527 	case MAC_PROP_STATUS:
3528 		if (perm != NULL)
3529 			*perm = MAC_PROP_PERM_READ;
3530 		return (0);
3531 	}
3532 
3533 	/*
3534 	 * Get the property info from the driver if it implements the
3535 	 * property info entry point.
3536 	 */
3537 	bzero(&state, sizeof (state));
3538 
3539 	if (mip->mi_callbacks->mc_callbacks & MC_PROPINFO) {
3540 		state.pr_default = default_val;
3541 		state.pr_default_size = default_size;
3542 
3543 		/*
3544 		 * The caller specifies the maximum number of ranges
3545 		 * it can accomodate using mpr_count. We don't touch
3546 		 * this value until the driver returns from its
3547 		 * mc_propinfo() callback, and ensure we don't exceed
3548 		 * this number of range as the driver defines
3549 		 * supported range from its mc_propinfo().
3550 		 *
3551 		 * pr_range_cur_count keeps track of how many ranges
3552 		 * were defined by the driver from its mc_propinfo()
3553 		 * entry point.
3554 		 *
3555 		 * On exit, the user-specified range mpr_count returns
3556 		 * the number of ranges specified by the driver on
3557 		 * success, or the number of ranges it wanted to
3558 		 * define if that number of ranges could not be
3559 		 * accomodated by the specified range structure.  In
3560 		 * the latter case, the caller will be able to
3561 		 * allocate a larger range structure, and query the
3562 		 * property again.
3563 		 */
3564 		state.pr_range_cur_count = 0;
3565 		state.pr_range = range;
3566 
3567 		mip->mi_callbacks->mc_propinfo(mip->mi_driver, name, id,
3568 		    (mac_prop_info_handle_t)&state);
3569 
3570 		if (state.pr_flags & MAC_PROP_INFO_RANGE)
3571 			range->mpr_count = state.pr_range_cur_count;
3572 
3573 		/*
3574 		 * The operation could fail if the buffer supplied by
3575 		 * the user was too small for the range or default
3576 		 * value of the property.
3577 		 */
3578 		if (state.pr_errno != 0)
3579 			return (state.pr_errno);
3580 
3581 		if (perm != NULL && state.pr_flags & MAC_PROP_INFO_PERM)
3582 			*perm = state.pr_perm;
3583 	}
3584 
3585 	/*
3586 	 * The MAC layer may want to provide default values or allowed
3587 	 * ranges for properties if the driver does not provide a
3588 	 * property info entry point, or that entry point exists, but
3589 	 * it did not provide a default value or allowed ranges for
3590 	 * that property.
3591 	 */
3592 	switch (id) {
3593 	case MAC_PROP_MTU: {
3594 		uint32_t sdu;
3595 
3596 		mac_sdu_get2(mh, NULL, &sdu, NULL);
3597 
3598 		if (range != NULL && !(state.pr_flags &
3599 		    MAC_PROP_INFO_RANGE)) {
3600 			/* MTU range */
3601 			_mac_set_range(range, sdu, sdu);
3602 		}
3603 
3604 		if (default_val != NULL && !(state.pr_flags &
3605 		    MAC_PROP_INFO_DEFAULT)) {
3606 			if (mip->mi_info.mi_media == DL_ETHER)
3607 				sdu = ETHERMTU;
3608 			/* default MTU value */
3609 			bcopy(&sdu, default_val, sizeof (sdu));
3610 		}
3611 	}
3612 	}
3613 
3614 	return (0);
3615 }
3616 
3617 int
3618 mac_fastpath_disable(mac_handle_t mh)
3619 {
3620 	mac_impl_t	*mip = (mac_impl_t *)mh;
3621 
3622 	if ((mip->mi_state_flags & MIS_LEGACY) == 0)
3623 		return (0);
3624 
3625 	return (mip->mi_capab_legacy.ml_fastpath_disable(mip->mi_driver));
3626 }
3627 
3628 void
3629 mac_fastpath_enable(mac_handle_t mh)
3630 {
3631 	mac_impl_t	*mip = (mac_impl_t *)mh;
3632 
3633 	if ((mip->mi_state_flags & MIS_LEGACY) == 0)
3634 		return;
3635 
3636 	mip->mi_capab_legacy.ml_fastpath_enable(mip->mi_driver);
3637 }
3638 
3639 void
3640 mac_register_priv_prop(mac_impl_t *mip, char **priv_props)
3641 {
3642 	uint_t nprops, i;
3643 
3644 	if (priv_props == NULL)
3645 		return;
3646 
3647 	nprops = 0;
3648 	while (priv_props[nprops] != NULL)
3649 		nprops++;
3650 	if (nprops == 0)
3651 		return;
3652 
3653 
3654 	mip->mi_priv_prop = kmem_zalloc(nprops * sizeof (char *), KM_SLEEP);
3655 
3656 	for (i = 0; i < nprops; i++) {
3657 		mip->mi_priv_prop[i] = kmem_zalloc(MAXLINKPROPNAME, KM_SLEEP);
3658 		(void) strlcpy(mip->mi_priv_prop[i], priv_props[i],
3659 		    MAXLINKPROPNAME);
3660 	}
3661 
3662 	mip->mi_priv_prop_count = nprops;
3663 }
3664 
3665 void
3666 mac_unregister_priv_prop(mac_impl_t *mip)
3667 {
3668 	uint_t i;
3669 
3670 	if (mip->mi_priv_prop_count == 0) {
3671 		ASSERT(mip->mi_priv_prop == NULL);
3672 		return;
3673 	}
3674 
3675 	for (i = 0; i < mip->mi_priv_prop_count; i++)
3676 		kmem_free(mip->mi_priv_prop[i], MAXLINKPROPNAME);
3677 	kmem_free(mip->mi_priv_prop, mip->mi_priv_prop_count *
3678 	    sizeof (char *));
3679 
3680 	mip->mi_priv_prop = NULL;
3681 	mip->mi_priv_prop_count = 0;
3682 }
3683 
3684 /*
3685  * mac_ring_t 'mr' macros. Some rogue drivers may access ring structure
3686  * (by invoking mac_rx()) even after processing mac_stop_ring(). In such
3687  * cases if MAC free's the ring structure after mac_stop_ring(), any
3688  * illegal access to the ring structure coming from the driver will panic
3689  * the system. In order to protect the system from such inadverent access,
3690  * we maintain a cache of rings in the mac_impl_t after they get free'd up.
3691  * When packets are received on free'd up rings, MAC (through the generation
3692  * count mechanism) will drop such packets.
3693  */
3694 static mac_ring_t *
3695 mac_ring_alloc(mac_impl_t *mip)
3696 {
3697 	mac_ring_t *ring;
3698 
3699 	mutex_enter(&mip->mi_ring_lock);
3700 	if (mip->mi_ring_freelist != NULL) {
3701 		ring = mip->mi_ring_freelist;
3702 		mip->mi_ring_freelist = ring->mr_next;
3703 		bzero(ring, sizeof (mac_ring_t));
3704 		mutex_exit(&mip->mi_ring_lock);
3705 	} else {
3706 		mutex_exit(&mip->mi_ring_lock);
3707 		ring = kmem_cache_alloc(mac_ring_cache, KM_SLEEP);
3708 	}
3709 	ASSERT((ring != NULL) && (ring->mr_state == MR_FREE));
3710 	return (ring);
3711 }
3712 
3713 static void
3714 mac_ring_free(mac_impl_t *mip, mac_ring_t *ring)
3715 {
3716 	ASSERT(ring->mr_state == MR_FREE);
3717 
3718 	mutex_enter(&mip->mi_ring_lock);
3719 	ring->mr_state = MR_FREE;
3720 	ring->mr_flag = 0;
3721 	ring->mr_next = mip->mi_ring_freelist;
3722 	ring->mr_mip = NULL;
3723 	mip->mi_ring_freelist = ring;
3724 	mac_ring_stat_delete(ring);
3725 	mutex_exit(&mip->mi_ring_lock);
3726 }
3727 
3728 static void
3729 mac_ring_freeall(mac_impl_t *mip)
3730 {
3731 	mac_ring_t *ring_next;
3732 	mutex_enter(&mip->mi_ring_lock);
3733 	mac_ring_t *ring = mip->mi_ring_freelist;
3734 	while (ring != NULL) {
3735 		ring_next = ring->mr_next;
3736 		kmem_cache_free(mac_ring_cache, ring);
3737 		ring = ring_next;
3738 	}
3739 	mip->mi_ring_freelist = NULL;
3740 	mutex_exit(&mip->mi_ring_lock);
3741 }
3742 
3743 int
3744 mac_start_ring(mac_ring_t *ring)
3745 {
3746 	int rv = 0;
3747 
3748 	ASSERT(ring->mr_state == MR_FREE);
3749 
3750 	if (ring->mr_start != NULL) {
3751 		rv = ring->mr_start(ring->mr_driver, ring->mr_gen_num);
3752 		if (rv != 0)
3753 			return (rv);
3754 	}
3755 
3756 	ring->mr_state = MR_INUSE;
3757 	return (rv);
3758 }
3759 
3760 void
3761 mac_stop_ring(mac_ring_t *ring)
3762 {
3763 	ASSERT(ring->mr_state == MR_INUSE);
3764 
3765 	if (ring->mr_stop != NULL)
3766 		ring->mr_stop(ring->mr_driver);
3767 
3768 	ring->mr_state = MR_FREE;
3769 
3770 	/*
3771 	 * Increment the ring generation number for this ring.
3772 	 */
3773 	ring->mr_gen_num++;
3774 }
3775 
3776 int
3777 mac_start_group(mac_group_t *group)
3778 {
3779 	int rv = 0;
3780 
3781 	if (group->mrg_start != NULL)
3782 		rv = group->mrg_start(group->mrg_driver);
3783 
3784 	return (rv);
3785 }
3786 
3787 void
3788 mac_stop_group(mac_group_t *group)
3789 {
3790 	if (group->mrg_stop != NULL)
3791 		group->mrg_stop(group->mrg_driver);
3792 }
3793 
3794 /*
3795  * Called from mac_start() on the default Rx group. Broadcast and multicast
3796  * packets are received only on the default group. Hence the default group
3797  * needs to be up even if the primary client is not up, for the other groups
3798  * to be functional. We do this by calling this function at mac_start time
3799  * itself. However the broadcast packets that are received can't make their
3800  * way beyond mac_rx until a mac client creates a broadcast flow.
3801  */
3802 static int
3803 mac_start_group_and_rings(mac_group_t *group)
3804 {
3805 	mac_ring_t	*ring;
3806 	int		rv = 0;
3807 
3808 	ASSERT(group->mrg_state == MAC_GROUP_STATE_REGISTERED);
3809 	if ((rv = mac_start_group(group)) != 0)
3810 		return (rv);
3811 
3812 	for (ring = group->mrg_rings; ring != NULL; ring = ring->mr_next) {
3813 		ASSERT(ring->mr_state == MR_FREE);
3814 		if ((rv = mac_start_ring(ring)) != 0)
3815 			goto error;
3816 		ring->mr_classify_type = MAC_SW_CLASSIFIER;
3817 	}
3818 	return (0);
3819 
3820 error:
3821 	mac_stop_group_and_rings(group);
3822 	return (rv);
3823 }
3824 
3825 /* Called from mac_stop on the default Rx group */
3826 static void
3827 mac_stop_group_and_rings(mac_group_t *group)
3828 {
3829 	mac_ring_t	*ring;
3830 
3831 	for (ring = group->mrg_rings; ring != NULL; ring = ring->mr_next) {
3832 		if (ring->mr_state != MR_FREE) {
3833 			mac_stop_ring(ring);
3834 			ring->mr_flag = 0;
3835 			ring->mr_classify_type = MAC_NO_CLASSIFIER;
3836 		}
3837 	}
3838 	mac_stop_group(group);
3839 }
3840 
3841 
3842 static mac_ring_t *
3843 mac_init_ring(mac_impl_t *mip, mac_group_t *group, int index,
3844     mac_capab_rings_t *cap_rings)
3845 {
3846 	mac_ring_t *ring, *rnext;
3847 	mac_ring_info_t ring_info;
3848 	ddi_intr_handle_t ddi_handle;
3849 
3850 	ring = mac_ring_alloc(mip);
3851 
3852 	/* Prepare basic information of ring */
3853 
3854 	/*
3855 	 * Ring index is numbered to be unique across a particular device.
3856 	 * Ring index computation makes following assumptions:
3857 	 *	- For drivers with static grouping (e.g. ixgbe, bge),
3858 	 *	ring index exchanged with the driver (e.g. during mr_rget)
3859 	 *	is unique only across the group the ring belongs to.
3860 	 *	- Drivers with dynamic grouping (e.g. nxge), start
3861 	 *	with single group (mrg_index = 0).
3862 	 */
3863 	ring->mr_index = group->mrg_index * group->mrg_info.mgi_count + index;
3864 	ring->mr_type = group->mrg_type;
3865 	ring->mr_gh = (mac_group_handle_t)group;
3866 
3867 	/* Insert the new ring to the list. */
3868 	ring->mr_next = group->mrg_rings;
3869 	group->mrg_rings = ring;
3870 
3871 	/* Zero to reuse the info data structure */
3872 	bzero(&ring_info, sizeof (ring_info));
3873 
3874 	/* Query ring information from driver */
3875 	cap_rings->mr_rget(mip->mi_driver, group->mrg_type, group->mrg_index,
3876 	    index, &ring_info, (mac_ring_handle_t)ring);
3877 
3878 	ring->mr_info = ring_info;
3879 
3880 	/*
3881 	 * The interrupt handle could be shared among multiple rings.
3882 	 * Thus if there is a bunch of rings that are sharing an
3883 	 * interrupt, then only one ring among the bunch will be made
3884 	 * available for interrupt re-targeting; the rest will have
3885 	 * ddi_shared flag set to TRUE and would not be available for
3886 	 * be interrupt re-targeting.
3887 	 */
3888 	if ((ddi_handle = ring_info.mri_intr.mi_ddi_handle) != NULL) {
3889 		rnext = ring->mr_next;
3890 		while (rnext != NULL) {
3891 			if (rnext->mr_info.mri_intr.mi_ddi_handle ==
3892 			    ddi_handle) {
3893 				/*
3894 				 * If default ring (mr_index == 0) is part
3895 				 * of a group of rings sharing an
3896 				 * interrupt, then set ddi_shared flag for
3897 				 * the default ring and give another ring
3898 				 * the chance to be re-targeted.
3899 				 */
3900 				if (rnext->mr_index == 0 &&
3901 				    !rnext->mr_info.mri_intr.mi_ddi_shared) {
3902 					rnext->mr_info.mri_intr.mi_ddi_shared =
3903 					    B_TRUE;
3904 				} else {
3905 					ring->mr_info.mri_intr.mi_ddi_shared =
3906 					    B_TRUE;
3907 				}
3908 				break;
3909 			}
3910 			rnext = rnext->mr_next;
3911 		}
3912 		/*
3913 		 * If rnext is NULL, then no matching ddi_handle was found.
3914 		 * Rx rings get registered first. So if this is a Tx ring,
3915 		 * then go through all the Rx rings and see if there is a
3916 		 * matching ddi handle.
3917 		 */
3918 		if (rnext == NULL && ring->mr_type == MAC_RING_TYPE_TX) {
3919 			mac_compare_ddi_handle(mip->mi_rx_groups,
3920 			    mip->mi_rx_group_count, ring);
3921 		}
3922 	}
3923 
3924 	/* Update ring's status */
3925 	ring->mr_state = MR_FREE;
3926 	ring->mr_flag = 0;
3927 
3928 	/* Update the ring count of the group */
3929 	group->mrg_cur_count++;
3930 
3931 	/* Create per ring kstats */
3932 	if (ring->mr_stat != NULL) {
3933 		ring->mr_mip = mip;
3934 		mac_ring_stat_create(ring);
3935 	}
3936 
3937 	return (ring);
3938 }
3939 
3940 /*
3941  * Rings are chained together for easy regrouping.
3942  */
3943 static void
3944 mac_init_group(mac_impl_t *mip, mac_group_t *group, int size,
3945     mac_capab_rings_t *cap_rings)
3946 {
3947 	int index;
3948 
3949 	/*
3950 	 * Initialize all ring members of this group. Size of zero will not
3951 	 * enter the loop, so it's safe for initializing an empty group.
3952 	 */
3953 	for (index = size - 1; index >= 0; index--)
3954 		(void) mac_init_ring(mip, group, index, cap_rings);
3955 }
3956 
3957 int
3958 mac_init_rings(mac_impl_t *mip, mac_ring_type_t rtype)
3959 {
3960 	mac_capab_rings_t	*cap_rings;
3961 	mac_group_t		*group;
3962 	mac_group_t		*groups;
3963 	mac_group_info_t	group_info;
3964 	uint_t			group_free = 0;
3965 	uint_t			ring_left;
3966 	mac_ring_t		*ring;
3967 	int			g;
3968 	int			err = 0;
3969 	uint_t			grpcnt;
3970 	boolean_t		pseudo_txgrp = B_FALSE;
3971 
3972 	switch (rtype) {
3973 	case MAC_RING_TYPE_RX:
3974 		ASSERT(mip->mi_rx_groups == NULL);
3975 
3976 		cap_rings = &mip->mi_rx_rings_cap;
3977 		cap_rings->mr_type = MAC_RING_TYPE_RX;
3978 		break;
3979 	case MAC_RING_TYPE_TX:
3980 		ASSERT(mip->mi_tx_groups == NULL);
3981 
3982 		cap_rings = &mip->mi_tx_rings_cap;
3983 		cap_rings->mr_type = MAC_RING_TYPE_TX;
3984 		break;
3985 	default:
3986 		ASSERT(B_FALSE);
3987 	}
3988 
3989 	if (!i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_RINGS, cap_rings))
3990 		return (0);
3991 	grpcnt = cap_rings->mr_gnum;
3992 
3993 	/*
3994 	 * If we have multiple TX rings, but only one TX group, we can
3995 	 * create pseudo TX groups (one per TX ring) in the MAC layer,
3996 	 * except for an aggr. For an aggr currently we maintain only
3997 	 * one group with all the rings (for all its ports), going
3998 	 * forwards we might change this.
3999 	 */
4000 	if (rtype == MAC_RING_TYPE_TX &&
4001 	    cap_rings->mr_gnum == 0 && cap_rings->mr_rnum >  0 &&
4002 	    (mip->mi_state_flags & MIS_IS_AGGR) == 0) {
4003 		/*
4004 		 * The -1 here is because we create a default TX group
4005 		 * with all the rings in it.
4006 		 */
4007 		grpcnt = cap_rings->mr_rnum - 1;
4008 		pseudo_txgrp = B_TRUE;
4009 	}
4010 
4011 	/*
4012 	 * Allocate a contiguous buffer for all groups.
4013 	 */
4014 	groups = kmem_zalloc(sizeof (mac_group_t) * (grpcnt+ 1), KM_SLEEP);
4015 
4016 	ring_left = cap_rings->mr_rnum;
4017 
4018 	/*
4019 	 * Get all ring groups if any, and get their ring members
4020 	 * if any.
4021 	 */
4022 	for (g = 0; g < grpcnt; g++) {
4023 		group = groups + g;
4024 
4025 		/* Prepare basic information of the group */
4026 		group->mrg_index = g;
4027 		group->mrg_type = rtype;
4028 		group->mrg_state = MAC_GROUP_STATE_UNINIT;
4029 		group->mrg_mh = (mac_handle_t)mip;
4030 		group->mrg_next = group + 1;
4031 
4032 		/* Zero to reuse the info data structure */
4033 		bzero(&group_info, sizeof (group_info));
4034 
4035 		if (pseudo_txgrp) {
4036 			/*
4037 			 * This is a pseudo group that we created, apart
4038 			 * from setting the state there is nothing to be
4039 			 * done.
4040 			 */
4041 			group->mrg_state = MAC_GROUP_STATE_REGISTERED;
4042 			group_free++;
4043 			continue;
4044 		}
4045 		/* Query group information from driver */
4046 		cap_rings->mr_gget(mip->mi_driver, rtype, g, &group_info,
4047 		    (mac_group_handle_t)group);
4048 
4049 		switch (cap_rings->mr_group_type) {
4050 		case MAC_GROUP_TYPE_DYNAMIC:
4051 			if (cap_rings->mr_gaddring == NULL ||
4052 			    cap_rings->mr_gremring == NULL) {
4053 				DTRACE_PROBE3(
4054 				    mac__init__rings_no_addremring,
4055 				    char *, mip->mi_name,
4056 				    mac_group_add_ring_t,
4057 				    cap_rings->mr_gaddring,
4058 				    mac_group_add_ring_t,
4059 				    cap_rings->mr_gremring);
4060 				err = EINVAL;
4061 				goto bail;
4062 			}
4063 
4064 			switch (rtype) {
4065 			case MAC_RING_TYPE_RX:
4066 				/*
4067 				 * The first RX group must have non-zero
4068 				 * rings, and the following groups must
4069 				 * have zero rings.
4070 				 */
4071 				if (g == 0 && group_info.mgi_count == 0) {
4072 					DTRACE_PROBE1(
4073 					    mac__init__rings__rx__def__zero,
4074 					    char *, mip->mi_name);
4075 					err = EINVAL;
4076 					goto bail;
4077 				}
4078 				if (g > 0 && group_info.mgi_count != 0) {
4079 					DTRACE_PROBE3(
4080 					    mac__init__rings__rx__nonzero,
4081 					    char *, mip->mi_name,
4082 					    int, g, int, group_info.mgi_count);
4083 					err = EINVAL;
4084 					goto bail;
4085 				}
4086 				break;
4087 			case MAC_RING_TYPE_TX:
4088 				/*
4089 				 * All TX ring groups must have zero rings.
4090 				 */
4091 				if (group_info.mgi_count != 0) {
4092 					DTRACE_PROBE3(
4093 					    mac__init__rings__tx__nonzero,
4094 					    char *, mip->mi_name,
4095 					    int, g, int, group_info.mgi_count);
4096 					err = EINVAL;
4097 					goto bail;
4098 				}
4099 				break;
4100 			}
4101 			break;
4102 		case MAC_GROUP_TYPE_STATIC:
4103 			/*
4104 			 * Note that an empty group is allowed, e.g., an aggr
4105 			 * would start with an empty group.
4106 			 */
4107 			break;
4108 		default:
4109 			/* unknown group type */
4110 			DTRACE_PROBE2(mac__init__rings__unknown__type,
4111 			    char *, mip->mi_name,
4112 			    int, cap_rings->mr_group_type);
4113 			err = EINVAL;
4114 			goto bail;
4115 		}
4116 
4117 
4118 		/*
4119 		 * The driver must register some form of hardware MAC
4120 		 * filter in order for Rx groups to support multiple
4121 		 * MAC addresses.
4122 		 */
4123 		if (rtype == MAC_RING_TYPE_RX &&
4124 		    (group_info.mgi_addmac == NULL ||
4125 		    group_info.mgi_remmac == NULL)) {
4126 			DTRACE_PROBE1(mac__init__rings__no__mac__filter,
4127 			    char *, mip->mi_name);
4128 			err = EINVAL;
4129 			goto bail;
4130 		}
4131 
4132 		/* Cache driver-supplied information */
4133 		group->mrg_info = group_info;
4134 
4135 		/* Update the group's status and group count. */
4136 		mac_set_group_state(group, MAC_GROUP_STATE_REGISTERED);
4137 		group_free++;
4138 
4139 		group->mrg_rings = NULL;
4140 		group->mrg_cur_count = 0;
4141 		mac_init_group(mip, group, group_info.mgi_count, cap_rings);
4142 		ring_left -= group_info.mgi_count;
4143 
4144 		/* The current group size should be equal to default value */
4145 		ASSERT(group->mrg_cur_count == group_info.mgi_count);
4146 	}
4147 
4148 	/* Build up a dummy group for free resources as a pool */
4149 	group = groups + grpcnt;
4150 
4151 	/* Prepare basic information of the group */
4152 	group->mrg_index = -1;
4153 	group->mrg_type = rtype;
4154 	group->mrg_state = MAC_GROUP_STATE_UNINIT;
4155 	group->mrg_mh = (mac_handle_t)mip;
4156 	group->mrg_next = NULL;
4157 
4158 	/*
4159 	 * If there are ungrouped rings, allocate a continuous buffer for
4160 	 * remaining resources.
4161 	 */
4162 	if (ring_left != 0) {
4163 		group->mrg_rings = NULL;
4164 		group->mrg_cur_count = 0;
4165 		mac_init_group(mip, group, ring_left, cap_rings);
4166 
4167 		/* The current group size should be equal to ring_left */
4168 		ASSERT(group->mrg_cur_count == ring_left);
4169 
4170 		ring_left = 0;
4171 
4172 		/* Update this group's status */
4173 		mac_set_group_state(group, MAC_GROUP_STATE_REGISTERED);
4174 	} else {
4175 		group->mrg_rings = NULL;
4176 	}
4177 
4178 	ASSERT(ring_left == 0);
4179 
4180 bail:
4181 
4182 	/* Cache other important information to finalize the initialization */
4183 	switch (rtype) {
4184 	case MAC_RING_TYPE_RX:
4185 		mip->mi_rx_group_type = cap_rings->mr_group_type;
4186 		mip->mi_rx_group_count = cap_rings->mr_gnum;
4187 		mip->mi_rx_groups = groups;
4188 		mip->mi_rx_donor_grp = groups;
4189 		if (mip->mi_rx_group_type == MAC_GROUP_TYPE_DYNAMIC) {
4190 			/*
4191 			 * The default ring is reserved since it is
4192 			 * used for sending the broadcast etc. packets.
4193 			 */
4194 			mip->mi_rxrings_avail =
4195 			    mip->mi_rx_groups->mrg_cur_count - 1;
4196 			mip->mi_rxrings_rsvd = 1;
4197 		}
4198 		/*
4199 		 * The default group cannot be reserved. It is used by
4200 		 * all the clients that do not have an exclusive group.
4201 		 */
4202 		mip->mi_rxhwclnt_avail = mip->mi_rx_group_count - 1;
4203 		mip->mi_rxhwclnt_used = 1;
4204 		break;
4205 	case MAC_RING_TYPE_TX:
4206 		mip->mi_tx_group_type = pseudo_txgrp ? MAC_GROUP_TYPE_DYNAMIC :
4207 		    cap_rings->mr_group_type;
4208 		mip->mi_tx_group_count = grpcnt;
4209 		mip->mi_tx_group_free = group_free;
4210 		mip->mi_tx_groups = groups;
4211 
4212 		group = groups + grpcnt;
4213 		ring = group->mrg_rings;
4214 		/*
4215 		 * The ring can be NULL in the case of aggr. Aggr will
4216 		 * have an empty Tx group which will get populated
4217 		 * later when pseudo Tx rings are added after
4218 		 * mac_register() is done.
4219 		 */
4220 		if (ring == NULL) {
4221 			ASSERT(mip->mi_state_flags & MIS_IS_AGGR);
4222 			/*
4223 			 * pass the group to aggr so it can add Tx
4224 			 * rings to the group later.
4225 			 */
4226 			cap_rings->mr_gget(mip->mi_driver, rtype, 0, NULL,
4227 			    (mac_group_handle_t)group);
4228 			/*
4229 			 * Even though there are no rings at this time
4230 			 * (rings will come later), set the group
4231 			 * state to registered.
4232 			 */
4233 			group->mrg_state = MAC_GROUP_STATE_REGISTERED;
4234 		} else {
4235 			/*
4236 			 * Ring 0 is used as the default one and it could be
4237 			 * assigned to a client as well.
4238 			 */
4239 			while ((ring->mr_index != 0) && (ring->mr_next != NULL))
4240 				ring = ring->mr_next;
4241 			ASSERT(ring->mr_index == 0);
4242 			mip->mi_default_tx_ring = (mac_ring_handle_t)ring;
4243 		}
4244 		if (mip->mi_tx_group_type == MAC_GROUP_TYPE_DYNAMIC) {
4245 			mip->mi_txrings_avail = group->mrg_cur_count - 1;
4246 			/*
4247 			 * The default ring cannot be reserved.
4248 			 */
4249 			mip->mi_txrings_rsvd = 1;
4250 		}
4251 		/*
4252 		 * The default group cannot be reserved. It will be shared
4253 		 * by clients that do not have an exclusive group.
4254 		 */
4255 		mip->mi_txhwclnt_avail = mip->mi_tx_group_count;
4256 		mip->mi_txhwclnt_used = 1;
4257 		break;
4258 	default:
4259 		ASSERT(B_FALSE);
4260 	}
4261 
4262 	if (err != 0)
4263 		mac_free_rings(mip, rtype);
4264 
4265 	return (err);
4266 }
4267 
4268 /*
4269  * The ddi interrupt handle could be shared amoung rings. If so, compare
4270  * the new ring's ddi handle with the existing ones and set ddi_shared
4271  * flag.
4272  */
4273 void
4274 mac_compare_ddi_handle(mac_group_t *groups, uint_t grpcnt, mac_ring_t *cring)
4275 {
4276 	mac_group_t *group;
4277 	mac_ring_t *ring;
4278 	ddi_intr_handle_t ddi_handle;
4279 	int g;
4280 
4281 	ddi_handle = cring->mr_info.mri_intr.mi_ddi_handle;
4282 	for (g = 0; g < grpcnt; g++) {
4283 		group = groups + g;
4284 		for (ring = group->mrg_rings; ring != NULL;
4285 		    ring = ring->mr_next) {
4286 			if (ring == cring)
4287 				continue;
4288 			if (ring->mr_info.mri_intr.mi_ddi_handle ==
4289 			    ddi_handle) {
4290 				if (cring->mr_type == MAC_RING_TYPE_RX &&
4291 				    ring->mr_index == 0 &&
4292 				    !ring->mr_info.mri_intr.mi_ddi_shared) {
4293 					ring->mr_info.mri_intr.mi_ddi_shared =
4294 					    B_TRUE;
4295 				} else {
4296 					cring->mr_info.mri_intr.mi_ddi_shared =
4297 					    B_TRUE;
4298 				}
4299 				return;
4300 			}
4301 		}
4302 	}
4303 }
4304 
4305 /*
4306  * Called to free all groups of particular type (RX or TX). It's assumed that
4307  * no clients are using these groups.
4308  */
4309 void
4310 mac_free_rings(mac_impl_t *mip, mac_ring_type_t rtype)
4311 {
4312 	mac_group_t *group, *groups;
4313 	uint_t group_count;
4314 
4315 	switch (rtype) {
4316 	case MAC_RING_TYPE_RX:
4317 		if (mip->mi_rx_groups == NULL)
4318 			return;
4319 
4320 		groups = mip->mi_rx_groups;
4321 		group_count = mip->mi_rx_group_count;
4322 
4323 		mip->mi_rx_groups = NULL;
4324 		mip->mi_rx_donor_grp = NULL;
4325 		mip->mi_rx_group_count = 0;
4326 		break;
4327 	case MAC_RING_TYPE_TX:
4328 		ASSERT(mip->mi_tx_group_count == mip->mi_tx_group_free);
4329 
4330 		if (mip->mi_tx_groups == NULL)
4331 			return;
4332 
4333 		groups = mip->mi_tx_groups;
4334 		group_count = mip->mi_tx_group_count;
4335 
4336 		mip->mi_tx_groups = NULL;
4337 		mip->mi_tx_group_count = 0;
4338 		mip->mi_tx_group_free = 0;
4339 		mip->mi_default_tx_ring = NULL;
4340 		break;
4341 	default:
4342 		ASSERT(B_FALSE);
4343 	}
4344 
4345 	for (group = groups; group != NULL; group = group->mrg_next) {
4346 		mac_ring_t *ring;
4347 
4348 		if (group->mrg_cur_count == 0)
4349 			continue;
4350 
4351 		ASSERT(group->mrg_rings != NULL);
4352 
4353 		while ((ring = group->mrg_rings) != NULL) {
4354 			group->mrg_rings = ring->mr_next;
4355 			mac_ring_free(mip, ring);
4356 		}
4357 	}
4358 
4359 	/* Free all the cached rings */
4360 	mac_ring_freeall(mip);
4361 	/* Free the block of group data strutures */
4362 	kmem_free(groups, sizeof (mac_group_t) * (group_count + 1));
4363 }
4364 
4365 /*
4366  * Associate the VLAN filter to the receive group.
4367  */
4368 int
4369 mac_group_addvlan(mac_group_t *group, uint16_t vlan)
4370 {
4371 	VERIFY3S(group->mrg_type, ==, MAC_RING_TYPE_RX);
4372 	VERIFY3P(group->mrg_info.mgi_addvlan, !=, NULL);
4373 
4374 	if (vlan > VLAN_ID_MAX)
4375 		return (EINVAL);
4376 
4377 	vlan = MAC_VLAN_UNTAGGED_VID(vlan);
4378 	return (group->mrg_info.mgi_addvlan(group->mrg_info.mgi_driver, vlan));
4379 }
4380 
4381 /*
4382  * Dissociate the VLAN from the receive group.
4383  */
4384 int
4385 mac_group_remvlan(mac_group_t *group, uint16_t vlan)
4386 {
4387 	VERIFY3S(group->mrg_type, ==, MAC_RING_TYPE_RX);
4388 	VERIFY3P(group->mrg_info.mgi_remvlan, !=, NULL);
4389 
4390 	if (vlan > VLAN_ID_MAX)
4391 		return (EINVAL);
4392 
4393 	vlan = MAC_VLAN_UNTAGGED_VID(vlan);
4394 	return (group->mrg_info.mgi_remvlan(group->mrg_info.mgi_driver, vlan));
4395 }
4396 
4397 /*
4398  * Associate a MAC address with a receive group.
4399  *
4400  * The return value of this function should always be checked properly, because
4401  * any type of failure could cause unexpected results. A group can be added
4402  * or removed with a MAC address only after it has been reserved. Ideally,
4403  * a successful reservation always leads to calling mac_group_addmac() to
4404  * steer desired traffic. Failure of adding an unicast MAC address doesn't
4405  * always imply that the group is functioning abnormally.
4406  *
4407  * Currently this function is called everywhere, and it reflects assumptions
4408  * about MAC addresses in the implementation. CR 6735196.
4409  */
4410 int
4411 mac_group_addmac(mac_group_t *group, const uint8_t *addr)
4412 {
4413 	VERIFY3S(group->mrg_type, ==, MAC_RING_TYPE_RX);
4414 	VERIFY3P(group->mrg_info.mgi_addmac, !=, NULL);
4415 
4416 	return (group->mrg_info.mgi_addmac(group->mrg_info.mgi_driver, addr));
4417 }
4418 
4419 /*
4420  * Remove the association between MAC address and receive group.
4421  */
4422 int
4423 mac_group_remmac(mac_group_t *group, const uint8_t *addr)
4424 {
4425 	VERIFY3S(group->mrg_type, ==, MAC_RING_TYPE_RX);
4426 	VERIFY3P(group->mrg_info.mgi_remmac, !=, NULL);
4427 
4428 	return (group->mrg_info.mgi_remmac(group->mrg_info.mgi_driver, addr));
4429 }
4430 
4431 /*
4432  * This is the entry point for packets transmitted through the bridging code.
4433  * If no bridge is in place, MAC_RING_TX transmits using tx ring. The 'rh'
4434  * pointer may be NULL to select the default ring.
4435  */
4436 mblk_t *
4437 mac_bridge_tx(mac_impl_t *mip, mac_ring_handle_t rh, mblk_t *mp)
4438 {
4439 	mac_handle_t mh;
4440 
4441 	/*
4442 	 * Once we take a reference on the bridge link, the bridge
4443 	 * module itself can't unload, so the callback pointers are
4444 	 * stable.
4445 	 */
4446 	mutex_enter(&mip->mi_bridge_lock);
4447 	if ((mh = mip->mi_bridge_link) != NULL)
4448 		mac_bridge_ref_cb(mh, B_TRUE);
4449 	mutex_exit(&mip->mi_bridge_lock);
4450 	if (mh == NULL) {
4451 		MAC_RING_TX(mip, rh, mp, mp);
4452 	} else {
4453 		mp = mac_bridge_tx_cb(mh, rh, mp);
4454 		mac_bridge_ref_cb(mh, B_FALSE);
4455 	}
4456 
4457 	return (mp);
4458 }
4459 
4460 /*
4461  * Find a ring from its index.
4462  */
4463 mac_ring_handle_t
4464 mac_find_ring(mac_group_handle_t gh, int index)
4465 {
4466 	mac_group_t *group = (mac_group_t *)gh;
4467 	mac_ring_t *ring = group->mrg_rings;
4468 
4469 	for (ring = group->mrg_rings; ring != NULL; ring = ring->mr_next)
4470 		if (ring->mr_index == index)
4471 			break;
4472 
4473 	return ((mac_ring_handle_t)ring);
4474 }
4475 /*
4476  * Add a ring to an existing group.
4477  *
4478  * The ring must be either passed directly (for example if the ring
4479  * movement is initiated by the framework), or specified through a driver
4480  * index (for example when the ring is added by the driver.
4481  *
4482  * The caller needs to call mac_perim_enter() before calling this function.
4483  */
4484 int
4485 i_mac_group_add_ring(mac_group_t *group, mac_ring_t *ring, int index)
4486 {
4487 	mac_impl_t *mip = (mac_impl_t *)group->mrg_mh;
4488 	mac_capab_rings_t *cap_rings;
4489 	boolean_t driver_call = (ring == NULL);
4490 	mac_group_type_t group_type;
4491 	int ret = 0;
4492 	flow_entry_t *flent;
4493 
4494 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
4495 
4496 	switch (group->mrg_type) {
4497 	case MAC_RING_TYPE_RX:
4498 		cap_rings = &mip->mi_rx_rings_cap;
4499 		group_type = mip->mi_rx_group_type;
4500 		break;
4501 	case MAC_RING_TYPE_TX:
4502 		cap_rings = &mip->mi_tx_rings_cap;
4503 		group_type = mip->mi_tx_group_type;
4504 		break;
4505 	default:
4506 		ASSERT(B_FALSE);
4507 	}
4508 
4509 	/*
4510 	 * There should be no ring with the same ring index in the target
4511 	 * group.
4512 	 */
4513 	ASSERT(mac_find_ring((mac_group_handle_t)group,
4514 	    driver_call ? index : ring->mr_index) == NULL);
4515 
4516 	if (driver_call) {
4517 		/*
4518 		 * The function is called as a result of a request from
4519 		 * a driver to add a ring to an existing group, for example
4520 		 * from the aggregation driver. Allocate a new mac_ring_t
4521 		 * for that ring.
4522 		 */
4523 		ring = mac_init_ring(mip, group, index, cap_rings);
4524 		ASSERT(group->mrg_state > MAC_GROUP_STATE_UNINIT);
4525 	} else {
4526 		/*
4527 		 * The function is called as a result of a MAC layer request
4528 		 * to add a ring to an existing group. In this case the
4529 		 * ring is being moved between groups, which requires
4530 		 * the underlying driver to support dynamic grouping,
4531 		 * and the mac_ring_t already exists.
4532 		 */
4533 		ASSERT(group_type == MAC_GROUP_TYPE_DYNAMIC);
4534 		ASSERT(group->mrg_driver == NULL ||
4535 		    cap_rings->mr_gaddring != NULL);
4536 		ASSERT(ring->mr_gh == NULL);
4537 	}
4538 
4539 	/*
4540 	 * At this point the ring should not be in use, and it should be
4541 	 * of the right for the target group.
4542 	 */
4543 	ASSERT(ring->mr_state < MR_INUSE);
4544 	ASSERT(ring->mr_srs == NULL);
4545 	ASSERT(ring->mr_type == group->mrg_type);
4546 
4547 	if (!driver_call) {
4548 		/*
4549 		 * Add the driver level hardware ring if the process was not
4550 		 * initiated by the driver, and the target group is not the
4551 		 * group.
4552 		 */
4553 		if (group->mrg_driver != NULL) {
4554 			cap_rings->mr_gaddring(group->mrg_driver,
4555 			    ring->mr_driver, ring->mr_type);
4556 		}
4557 
4558 		/*
4559 		 * Insert the ring ahead existing rings.
4560 		 */
4561 		ring->mr_next = group->mrg_rings;
4562 		group->mrg_rings = ring;
4563 		ring->mr_gh = (mac_group_handle_t)group;
4564 		group->mrg_cur_count++;
4565 	}
4566 
4567 	/*
4568 	 * If the group has not been actively used, we're done.
4569 	 */
4570 	if (group->mrg_index != -1 &&
4571 	    group->mrg_state < MAC_GROUP_STATE_RESERVED)
4572 		return (0);
4573 
4574 	/*
4575 	 * Start the ring if needed. Failure causes to undo the grouping action.
4576 	 */
4577 	if (ring->mr_state != MR_INUSE) {
4578 		if ((ret = mac_start_ring(ring)) != 0) {
4579 			if (!driver_call) {
4580 				cap_rings->mr_gremring(group->mrg_driver,
4581 				    ring->mr_driver, ring->mr_type);
4582 			}
4583 			group->mrg_cur_count--;
4584 			group->mrg_rings = ring->mr_next;
4585 
4586 			ring->mr_gh = NULL;
4587 
4588 			if (driver_call)
4589 				mac_ring_free(mip, ring);
4590 
4591 			return (ret);
4592 		}
4593 	}
4594 
4595 	/*
4596 	 * Set up SRS/SR according to the ring type.
4597 	 */
4598 	switch (ring->mr_type) {
4599 	case MAC_RING_TYPE_RX:
4600 		/*
4601 		 * Setup an SRS on top of the new ring if the group is
4602 		 * reserved for someone's exclusive use.
4603 		 */
4604 		if (group->mrg_state == MAC_GROUP_STATE_RESERVED) {
4605 			mac_client_impl_t *mcip =  MAC_GROUP_ONLY_CLIENT(group);
4606 
4607 			VERIFY3P(mcip, !=, NULL);
4608 			flent = mcip->mci_flent;
4609 			VERIFY3S(flent->fe_rx_srs_cnt, >, 0);
4610 			mac_rx_srs_group_setup(mcip, flent, SRST_LINK);
4611 			mac_fanout_setup(mcip, flent, MCIP_RESOURCE_PROPS(mcip),
4612 			    mac_rx_deliver, mcip, NULL, NULL);
4613 		} else {
4614 			ring->mr_classify_type = MAC_SW_CLASSIFIER;
4615 		}
4616 		break;
4617 	case MAC_RING_TYPE_TX:
4618 	{
4619 		mac_grp_client_t	*mgcp = group->mrg_clients;
4620 		mac_client_impl_t	*mcip;
4621 		mac_soft_ring_set_t	*mac_srs;
4622 		mac_srs_tx_t		*tx;
4623 
4624 		if (MAC_GROUP_NO_CLIENT(group)) {
4625 			if (ring->mr_state == MR_INUSE)
4626 				mac_stop_ring(ring);
4627 			ring->mr_flag = 0;
4628 			break;
4629 		}
4630 		/*
4631 		 * If the rings are being moved to a group that has
4632 		 * clients using it, then add the new rings to the
4633 		 * clients SRS.
4634 		 */
4635 		while (mgcp != NULL) {
4636 			boolean_t	is_aggr;
4637 
4638 			mcip = mgcp->mgc_client;
4639 			flent = mcip->mci_flent;
4640 			is_aggr = (mcip->mci_state_flags & MCIS_IS_AGGR_CLIENT);
4641 			mac_srs = MCIP_TX_SRS(mcip);
4642 			tx = &mac_srs->srs_tx;
4643 			mac_tx_client_quiesce((mac_client_handle_t)mcip);
4644 			/*
4645 			 * If we are  growing from 1 to multiple rings.
4646 			 */
4647 			if (tx->st_mode == SRS_TX_BW ||
4648 			    tx->st_mode == SRS_TX_SERIALIZE ||
4649 			    tx->st_mode == SRS_TX_DEFAULT) {
4650 				mac_ring_t	*tx_ring = tx->st_arg2;
4651 
4652 				tx->st_arg2 = NULL;
4653 				mac_tx_srs_stat_recreate(mac_srs, B_TRUE);
4654 				mac_tx_srs_add_ring(mac_srs, tx_ring);
4655 				if (mac_srs->srs_type & SRST_BW_CONTROL) {
4656 					tx->st_mode = is_aggr ? SRS_TX_BW_AGGR :
4657 					    SRS_TX_BW_FANOUT;
4658 				} else {
4659 					tx->st_mode = is_aggr ? SRS_TX_AGGR :
4660 					    SRS_TX_FANOUT;
4661 				}
4662 				tx->st_func = mac_tx_get_func(tx->st_mode);
4663 			}
4664 			mac_tx_srs_add_ring(mac_srs, ring);
4665 			mac_fanout_setup(mcip, flent, MCIP_RESOURCE_PROPS(mcip),
4666 			    mac_rx_deliver, mcip, NULL, NULL);
4667 			mac_tx_client_restart((mac_client_handle_t)mcip);
4668 			mgcp = mgcp->mgc_next;
4669 		}
4670 		break;
4671 	}
4672 	default:
4673 		ASSERT(B_FALSE);
4674 	}
4675 	/*
4676 	 * For aggr, the default ring will be NULL to begin with. If it
4677 	 * is NULL, then pick the first ring that gets added as the
4678 	 * default ring. Any ring in an aggregation can be removed at
4679 	 * any time (by the user action of removing a link) and if the
4680 	 * current default ring gets removed, then a new one gets
4681 	 * picked (see i_mac_group_rem_ring()).
4682 	 */
4683 	if (mip->mi_state_flags & MIS_IS_AGGR &&
4684 	    mip->mi_default_tx_ring == NULL &&
4685 	    ring->mr_type == MAC_RING_TYPE_TX) {
4686 		mip->mi_default_tx_ring = (mac_ring_handle_t)ring;
4687 	}
4688 
4689 	MAC_RING_UNMARK(ring, MR_INCIPIENT);
4690 	return (0);
4691 }
4692 
4693 /*
4694  * Remove a ring from it's current group. MAC internal function for dynamic
4695  * grouping.
4696  *
4697  * The caller needs to call mac_perim_enter() before calling this function.
4698  */
4699 void
4700 i_mac_group_rem_ring(mac_group_t *group, mac_ring_t *ring,
4701     boolean_t driver_call)
4702 {
4703 	mac_impl_t *mip = (mac_impl_t *)group->mrg_mh;
4704 	mac_capab_rings_t *cap_rings = NULL;
4705 	mac_group_type_t group_type;
4706 
4707 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
4708 
4709 	ASSERT(mac_find_ring((mac_group_handle_t)group,
4710 	    ring->mr_index) == (mac_ring_handle_t)ring);
4711 	ASSERT((mac_group_t *)ring->mr_gh == group);
4712 	ASSERT(ring->mr_type == group->mrg_type);
4713 
4714 	if (ring->mr_state == MR_INUSE)
4715 		mac_stop_ring(ring);
4716 	switch (ring->mr_type) {
4717 	case MAC_RING_TYPE_RX:
4718 		group_type = mip->mi_rx_group_type;
4719 		cap_rings = &mip->mi_rx_rings_cap;
4720 
4721 		/*
4722 		 * Only hardware classified packets hold a reference to the
4723 		 * ring all the way up the Rx path. mac_rx_srs_remove()
4724 		 * will take care of quiescing the Rx path and removing the
4725 		 * SRS. The software classified path neither holds a reference
4726 		 * nor any association with the ring in mac_rx.
4727 		 */
4728 		if (ring->mr_srs != NULL) {
4729 			mac_rx_srs_remove(ring->mr_srs);
4730 			ring->mr_srs = NULL;
4731 		}
4732 
4733 		break;
4734 	case MAC_RING_TYPE_TX:
4735 	{
4736 		mac_grp_client_t	*mgcp;
4737 		mac_client_impl_t	*mcip;
4738 		mac_soft_ring_set_t	*mac_srs;
4739 		mac_srs_tx_t		*tx;
4740 		mac_ring_t		*rem_ring;
4741 		mac_group_t		*defgrp;
4742 		uint_t			ring_info = 0;
4743 
4744 		/*
4745 		 * For TX this function is invoked in three
4746 		 * cases:
4747 		 *
4748 		 * 1) In the case of a failure during the
4749 		 * initial creation of a group when a share is
4750 		 * associated with a MAC client. So the SRS is not
4751 		 * yet setup, and will be setup later after the
4752 		 * group has been reserved and populated.
4753 		 *
4754 		 * 2) From mac_release_tx_group() when freeing
4755 		 * a TX SRS.
4756 		 *
4757 		 * 3) In the case of aggr, when a port gets removed,
4758 		 * the pseudo Tx rings that it exposed gets removed.
4759 		 *
4760 		 * In the first two cases the SRS and its soft
4761 		 * rings are already quiesced.
4762 		 */
4763 		if (driver_call) {
4764 			mac_client_impl_t *mcip;
4765 			mac_soft_ring_set_t *mac_srs;
4766 			mac_soft_ring_t *sringp;
4767 			mac_srs_tx_t *srs_tx;
4768 
4769 			if (mip->mi_state_flags & MIS_IS_AGGR &&
4770 			    mip->mi_default_tx_ring ==
4771 			    (mac_ring_handle_t)ring) {
4772 				/* pick a new default Tx ring */
4773 				mip->mi_default_tx_ring =
4774 				    (group->mrg_rings != ring) ?
4775 				    (mac_ring_handle_t)group->mrg_rings :
4776 				    (mac_ring_handle_t)(ring->mr_next);
4777 			}
4778 			/* Presently only aggr case comes here */
4779 			if (group->mrg_state != MAC_GROUP_STATE_RESERVED)
4780 				break;
4781 
4782 			mcip = MAC_GROUP_ONLY_CLIENT(group);
4783 			ASSERT(mcip != NULL);
4784 			ASSERT(mcip->mci_state_flags & MCIS_IS_AGGR_CLIENT);
4785 			mac_srs = MCIP_TX_SRS(mcip);
4786 			ASSERT(mac_srs->srs_tx.st_mode == SRS_TX_AGGR ||
4787 			    mac_srs->srs_tx.st_mode == SRS_TX_BW_AGGR);
4788 			srs_tx = &mac_srs->srs_tx;
4789 			/*
4790 			 * Wakeup any callers blocked on this
4791 			 * Tx ring due to flow control.
4792 			 */
4793 			sringp = srs_tx->st_soft_rings[ring->mr_index];
4794 			ASSERT(sringp != NULL);
4795 			mac_tx_invoke_callbacks(mcip, (mac_tx_cookie_t)sringp);
4796 			mac_tx_client_quiesce((mac_client_handle_t)mcip);
4797 			mac_tx_srs_del_ring(mac_srs, ring);
4798 			mac_tx_client_restart((mac_client_handle_t)mcip);
4799 			break;
4800 		}
4801 		ASSERT(ring != (mac_ring_t *)mip->mi_default_tx_ring);
4802 		group_type = mip->mi_tx_group_type;
4803 		cap_rings = &mip->mi_tx_rings_cap;
4804 		/*
4805 		 * See if we need to take it out of the MAC clients using
4806 		 * this group
4807 		 */
4808 		if (MAC_GROUP_NO_CLIENT(group))
4809 			break;
4810 		mgcp = group->mrg_clients;
4811 		defgrp = MAC_DEFAULT_TX_GROUP(mip);
4812 		while (mgcp != NULL) {
4813 			mcip = mgcp->mgc_client;
4814 			mac_srs = MCIP_TX_SRS(mcip);
4815 			tx = &mac_srs->srs_tx;
4816 			mac_tx_client_quiesce((mac_client_handle_t)mcip);
4817 			/*
4818 			 * If we are here when removing rings from the
4819 			 * defgroup, mac_reserve_tx_ring would have
4820 			 * already deleted the ring from the MAC
4821 			 * clients in the group.
4822 			 */
4823 			if (group != defgrp) {
4824 				mac_tx_invoke_callbacks(mcip,
4825 				    (mac_tx_cookie_t)
4826 				    mac_tx_srs_get_soft_ring(mac_srs, ring));
4827 				mac_tx_srs_del_ring(mac_srs, ring);
4828 			}
4829 			/*
4830 			 * Additionally, if  we are left with only
4831 			 * one ring in the group after this, we need
4832 			 * to modify the mode etc. to. (We haven't
4833 			 * yet taken the ring out, so we check with 2).
4834 			 */
4835 			if (group->mrg_cur_count == 2) {
4836 				if (ring->mr_next == NULL)
4837 					rem_ring = group->mrg_rings;
4838 				else
4839 					rem_ring = ring->mr_next;
4840 				mac_tx_invoke_callbacks(mcip,
4841 				    (mac_tx_cookie_t)
4842 				    mac_tx_srs_get_soft_ring(mac_srs,
4843 				    rem_ring));
4844 				mac_tx_srs_del_ring(mac_srs, rem_ring);
4845 				if (rem_ring->mr_state != MR_INUSE) {
4846 					(void) mac_start_ring(rem_ring);
4847 				}
4848 				tx->st_arg2 = (void *)rem_ring;
4849 				mac_tx_srs_stat_recreate(mac_srs, B_FALSE);
4850 				ring_info = mac_hwring_getinfo(
4851 				    (mac_ring_handle_t)rem_ring);
4852 				/*
4853 				 * We are  shrinking from multiple
4854 				 * to 1 ring.
4855 				 */
4856 				if (mac_srs->srs_type & SRST_BW_CONTROL) {
4857 					tx->st_mode = SRS_TX_BW;
4858 				} else if (mac_tx_serialize ||
4859 				    (ring_info & MAC_RING_TX_SERIALIZE)) {
4860 					tx->st_mode = SRS_TX_SERIALIZE;
4861 				} else {
4862 					tx->st_mode = SRS_TX_DEFAULT;
4863 				}
4864 				tx->st_func = mac_tx_get_func(tx->st_mode);
4865 			}
4866 			mac_tx_client_restart((mac_client_handle_t)mcip);
4867 			mgcp = mgcp->mgc_next;
4868 		}
4869 		break;
4870 	}
4871 	default:
4872 		ASSERT(B_FALSE);
4873 	}
4874 
4875 	/*
4876 	 * Remove the ring from the group.
4877 	 */
4878 	if (ring == group->mrg_rings)
4879 		group->mrg_rings = ring->mr_next;
4880 	else {
4881 		mac_ring_t *pre;
4882 
4883 		pre = group->mrg_rings;
4884 		while (pre->mr_next != ring)
4885 			pre = pre->mr_next;
4886 		pre->mr_next = ring->mr_next;
4887 	}
4888 	group->mrg_cur_count--;
4889 
4890 	if (!driver_call) {
4891 		ASSERT(group_type == MAC_GROUP_TYPE_DYNAMIC);
4892 		ASSERT(group->mrg_driver == NULL ||
4893 		    cap_rings->mr_gremring != NULL);
4894 
4895 		/*
4896 		 * Remove the driver level hardware ring.
4897 		 */
4898 		if (group->mrg_driver != NULL) {
4899 			cap_rings->mr_gremring(group->mrg_driver,
4900 			    ring->mr_driver, ring->mr_type);
4901 		}
4902 	}
4903 
4904 	ring->mr_gh = NULL;
4905 	if (driver_call)
4906 		mac_ring_free(mip, ring);
4907 	else
4908 		ring->mr_flag = 0;
4909 }
4910 
4911 /*
4912  * Move a ring to the target group. If needed, remove the ring from the group
4913  * that it currently belongs to.
4914  *
4915  * The caller need to enter MAC's perimeter by calling mac_perim_enter().
4916  */
4917 static int
4918 mac_group_mov_ring(mac_impl_t *mip, mac_group_t *d_group, mac_ring_t *ring)
4919 {
4920 	mac_group_t *s_group = (mac_group_t *)ring->mr_gh;
4921 	int rv;
4922 
4923 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
4924 	ASSERT(d_group != NULL);
4925 	ASSERT(s_group == NULL || s_group->mrg_mh == d_group->mrg_mh);
4926 
4927 	if (s_group == d_group)
4928 		return (0);
4929 
4930 	/*
4931 	 * Remove it from current group first.
4932 	 */
4933 	if (s_group != NULL)
4934 		i_mac_group_rem_ring(s_group, ring, B_FALSE);
4935 
4936 	/*
4937 	 * Add it to the new group.
4938 	 */
4939 	rv = i_mac_group_add_ring(d_group, ring, 0);
4940 	if (rv != 0) {
4941 		/*
4942 		 * Failed to add ring back to source group. If
4943 		 * that fails, the ring is stuck in limbo, log message.
4944 		 */
4945 		if (i_mac_group_add_ring(s_group, ring, 0)) {
4946 			cmn_err(CE_WARN, "%s: failed to move ring %p\n",
4947 			    mip->mi_name, (void *)ring);
4948 		}
4949 	}
4950 
4951 	return (rv);
4952 }
4953 
4954 /*
4955  * Find a MAC address according to its value.
4956  */
4957 mac_address_t *
4958 mac_find_macaddr(mac_impl_t *mip, uint8_t *mac_addr)
4959 {
4960 	mac_address_t *map;
4961 
4962 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
4963 
4964 	for (map = mip->mi_addresses; map != NULL; map = map->ma_next) {
4965 		if (bcmp(mac_addr, map->ma_addr, map->ma_len) == 0)
4966 			break;
4967 	}
4968 
4969 	return (map);
4970 }
4971 
4972 /*
4973  * Check whether the MAC address is shared by multiple clients.
4974  */
4975 boolean_t
4976 mac_check_macaddr_shared(mac_address_t *map)
4977 {
4978 	ASSERT(MAC_PERIM_HELD((mac_handle_t)map->ma_mip));
4979 
4980 	return (map->ma_nusers > 1);
4981 }
4982 
4983 /*
4984  * Remove the specified MAC address from the MAC address list and free it.
4985  */
4986 static void
4987 mac_free_macaddr(mac_address_t *map)
4988 {
4989 	mac_impl_t *mip = map->ma_mip;
4990 
4991 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
4992 	VERIFY3P(mip->mi_addresses, !=, NULL);
4993 
4994 	VERIFY3P(map, ==, mac_find_macaddr(mip, map->ma_addr));
4995 	VERIFY3P(map, !=, NULL);
4996 	VERIFY3S(map->ma_nusers, ==, 0);
4997 	VERIFY3P(map->ma_vlans, ==, NULL);
4998 
4999 	if (map == mip->mi_addresses) {
5000 		mip->mi_addresses = map->ma_next;
5001 	} else {
5002 		mac_address_t *pre;
5003 
5004 		pre = mip->mi_addresses;
5005 		while (pre->ma_next != map)
5006 			pre = pre->ma_next;
5007 		pre->ma_next = map->ma_next;
5008 	}
5009 
5010 	kmem_free(map, sizeof (mac_address_t));
5011 }
5012 
5013 static mac_vlan_t *
5014 mac_find_vlan(mac_address_t *map, uint16_t vid)
5015 {
5016 	mac_vlan_t *mvp;
5017 
5018 	for (mvp = map->ma_vlans; mvp != NULL; mvp = mvp->mv_next) {
5019 		if (mvp->mv_vid == vid)
5020 			return (mvp);
5021 	}
5022 
5023 	return (NULL);
5024 }
5025 
5026 static mac_vlan_t *
5027 mac_add_vlan(mac_address_t *map, uint16_t vid)
5028 {
5029 	mac_vlan_t *mvp;
5030 
5031 	/*
5032 	 * We should never add the same {addr, VID} tuple more
5033 	 * than once, but let's be sure.
5034 	 */
5035 	for (mvp = map->ma_vlans; mvp != NULL; mvp = mvp->mv_next)
5036 		VERIFY3U(mvp->mv_vid, !=, vid);
5037 
5038 	/* Add the VLAN to the head of the VLAN list. */
5039 	mvp = kmem_zalloc(sizeof (mac_vlan_t), KM_SLEEP);
5040 	mvp->mv_vid = vid;
5041 	mvp->mv_next = map->ma_vlans;
5042 	map->ma_vlans = mvp;
5043 
5044 	return (mvp);
5045 }
5046 
5047 static void
5048 mac_rem_vlan(mac_address_t *map, mac_vlan_t *mvp)
5049 {
5050 	mac_vlan_t *pre;
5051 
5052 	if (map->ma_vlans == mvp) {
5053 		map->ma_vlans = mvp->mv_next;
5054 	} else {
5055 		pre = map->ma_vlans;
5056 		while (pre->mv_next != mvp) {
5057 			pre = pre->mv_next;
5058 
5059 			/*
5060 			 * We've reached the end of the list without
5061 			 * finding mvp.
5062 			 */
5063 			VERIFY3P(pre, !=, NULL);
5064 		}
5065 		pre->mv_next = mvp->mv_next;
5066 	}
5067 
5068 	kmem_free(mvp, sizeof (mac_vlan_t));
5069 }
5070 
5071 /*
5072  * Create a new mac_address_t if this is the first use of the address
5073  * or add a VID to an existing address. In either case, the
5074  * mac_address_t acts as a list of {addr, VID} tuples where each tuple
5075  * shares the same addr. If group is non-NULL then attempt to program
5076  * the MAC's HW filters for this group. Otherwise, if group is NULL,
5077  * then the MAC has no rings and there is nothing to program.
5078  */
5079 int
5080 mac_add_macaddr_vlan(mac_impl_t *mip, mac_group_t *group, uint8_t *addr,
5081     uint16_t vid, boolean_t use_hw)
5082 {
5083 	mac_address_t	*map;
5084 	mac_vlan_t	*mvp;
5085 	int		err = 0;
5086 	boolean_t	allocated_map = B_FALSE;
5087 	boolean_t	hw_mac = B_FALSE;
5088 	boolean_t	hw_vlan = B_FALSE;
5089 
5090 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
5091 
5092 	map = mac_find_macaddr(mip, addr);
5093 
5094 	/*
5095 	 * If this is the first use of this MAC address then allocate
5096 	 * and initialize a new structure.
5097 	 */
5098 	if (map == NULL) {
5099 		map = kmem_zalloc(sizeof (mac_address_t), KM_SLEEP);
5100 		map->ma_len = mip->mi_type->mt_addr_length;
5101 		bcopy(addr, map->ma_addr, map->ma_len);
5102 		map->ma_nusers = 0;
5103 		map->ma_group = group;
5104 		map->ma_mip = mip;
5105 		map->ma_untagged = B_FALSE;
5106 
5107 		/* Add the new MAC address to the head of the address list. */
5108 		map->ma_next = mip->mi_addresses;
5109 		mip->mi_addresses = map;
5110 
5111 		allocated_map = B_TRUE;
5112 	}
5113 
5114 	VERIFY(map->ma_group == NULL || map->ma_group == group);
5115 	if (map->ma_group == NULL)
5116 		map->ma_group = group;
5117 
5118 	if (vid == VLAN_ID_NONE) {
5119 		map->ma_untagged = B_TRUE;
5120 		mvp = NULL;
5121 	} else {
5122 		mvp = mac_add_vlan(map, vid);
5123 	}
5124 
5125 	/*
5126 	 * Set the VLAN HW filter if:
5127 	 *
5128 	 * o the MAC's VLAN HW filtering is enabled, and
5129 	 * o the address does not currently rely on promisc mode.
5130 	 *
5131 	 * This is called even when the client specifies an untagged
5132 	 * address (VLAN_ID_NONE) because some MAC providers require
5133 	 * setting additional bits to accept untagged traffic when
5134 	 * VLAN HW filtering is enabled.
5135 	 */
5136 	if (MAC_GROUP_HW_VLAN(group) &&
5137 	    map->ma_type != MAC_ADDRESS_TYPE_UNICAST_PROMISC) {
5138 		if ((err = mac_group_addvlan(group, vid)) != 0)
5139 			goto bail;
5140 
5141 		hw_vlan = B_TRUE;
5142 	}
5143 
5144 	VERIFY3S(map->ma_nusers, >=, 0);
5145 	map->ma_nusers++;
5146 
5147 	/*
5148 	 * If this MAC address already has a HW filter then simply
5149 	 * increment the counter.
5150 	 */
5151 	if (map->ma_nusers > 1)
5152 		return (0);
5153 
5154 	/*
5155 	 * All logic from here on out is executed during initial
5156 	 * creation only.
5157 	 */
5158 	VERIFY3S(map->ma_nusers, ==, 1);
5159 
5160 	/*
5161 	 * Activate this MAC address by adding it to the reserved group.
5162 	 */
5163 	if (group != NULL) {
5164 		err = mac_group_addmac(group, (const uint8_t *)addr);
5165 
5166 		/*
5167 		 * If the driver is out of filters then we can
5168 		 * continue and use promisc mode. For any other error,
5169 		 * assume the driver is in a state where we can't
5170 		 * program the filters or use promisc mode; so we must
5171 		 * bail.
5172 		 */
5173 		if (err != 0 && err != ENOSPC) {
5174 			map->ma_nusers--;
5175 			goto bail;
5176 		}
5177 
5178 		hw_mac = (err == 0);
5179 	}
5180 
5181 	if (hw_mac) {
5182 		map->ma_type = MAC_ADDRESS_TYPE_UNICAST_CLASSIFIED;
5183 		return (0);
5184 	}
5185 
5186 	/*
5187 	 * The MAC address addition failed. If the client requires a
5188 	 * hardware classified MAC address, fail the operation. This
5189 	 * feature is only used by sun4v vsw.
5190 	 */
5191 	if (use_hw && !hw_mac) {
5192 		err = ENOSPC;
5193 		map->ma_nusers--;
5194 		goto bail;
5195 	}
5196 
5197 	/*
5198 	 * If we reach this point then either the MAC doesn't have
5199 	 * RINGS capability or we are out of MAC address HW filters.
5200 	 * In any case we must put the MAC into promiscuous mode.
5201 	 */
5202 	VERIFY(group == NULL || !hw_mac);
5203 
5204 	/*
5205 	 * The one exception is the primary address. A non-RINGS
5206 	 * driver filters the primary address by default; promisc mode
5207 	 * is not needed.
5208 	 */
5209 	if ((group == NULL) &&
5210 	    (bcmp(map->ma_addr, mip->mi_addr, map->ma_len) == 0)) {
5211 		map->ma_type = MAC_ADDRESS_TYPE_UNICAST_CLASSIFIED;
5212 		return (0);
5213 	}
5214 
5215 	/*
5216 	 * Enable promiscuous mode in order to receive traffic to the
5217 	 * new MAC address. All existing HW filters still send their
5218 	 * traffic to their respective group/SRSes. But with promisc
5219 	 * enabled all unknown traffic is delivered to the default
5220 	 * group where it is SW classified via mac_rx_classify().
5221 	 */
5222 	if ((err = i_mac_promisc_set(mip, B_TRUE)) == 0) {
5223 		map->ma_type = MAC_ADDRESS_TYPE_UNICAST_PROMISC;
5224 		return (0);
5225 	}
5226 
5227 	/*
5228 	 * We failed to set promisc mode and we are about to free 'map'.
5229 	 */
5230 	map->ma_nusers = 0;
5231 
5232 bail:
5233 	if (hw_vlan) {
5234 		int err2 = mac_group_remvlan(group, vid);
5235 
5236 		if (err2 != 0) {
5237 			cmn_err(CE_WARN, "Failed to remove VLAN %u from group"
5238 			    " %d on MAC %s: %d.", vid, group->mrg_index,
5239 			    mip->mi_name, err2);
5240 		}
5241 	}
5242 
5243 	if (mvp != NULL)
5244 		mac_rem_vlan(map, mvp);
5245 
5246 	if (allocated_map)
5247 		mac_free_macaddr(map);
5248 
5249 	return (err);
5250 }
5251 
5252 int
5253 mac_remove_macaddr_vlan(mac_address_t *map, uint16_t vid)
5254 {
5255 	mac_vlan_t	*mvp;
5256 	mac_impl_t	*mip = map->ma_mip;
5257 	mac_group_t	*group = map->ma_group;
5258 	int		err = 0;
5259 
5260 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
5261 	VERIFY3P(map, ==, mac_find_macaddr(mip, map->ma_addr));
5262 
5263 	if (vid == VLAN_ID_NONE) {
5264 		map->ma_untagged = B_FALSE;
5265 		mvp = NULL;
5266 	} else {
5267 		mvp = mac_find_vlan(map, vid);
5268 		VERIFY3P(mvp, !=, NULL);
5269 	}
5270 
5271 	if (MAC_GROUP_HW_VLAN(group) &&
5272 	    map->ma_type == MAC_ADDRESS_TYPE_UNICAST_CLASSIFIED &&
5273 	    ((err = mac_group_remvlan(group, vid)) != 0))
5274 		return (err);
5275 
5276 	if (mvp != NULL)
5277 		mac_rem_vlan(map, mvp);
5278 
5279 	/*
5280 	 * If it's not the last client using this MAC address, only update
5281 	 * the MAC clients count.
5282 	 */
5283 	map->ma_nusers--;
5284 	if (map->ma_nusers > 0)
5285 		return (0);
5286 
5287 	VERIFY3S(map->ma_nusers, ==, 0);
5288 
5289 	/*
5290 	 * The MAC address is no longer used by any MAC client, so
5291 	 * remove it from its associated group. Turn off promiscuous
5292 	 * mode if this is the last address relying on it.
5293 	 */
5294 	switch (map->ma_type) {
5295 	case MAC_ADDRESS_TYPE_UNICAST_CLASSIFIED:
5296 		/*
5297 		 * Don't free the preset primary address for drivers that
5298 		 * don't advertise RINGS capability.
5299 		 */
5300 		if (group == NULL)
5301 			return (0);
5302 
5303 		if ((err = mac_group_remmac(group, map->ma_addr)) != 0) {
5304 			if (vid == VLAN_ID_NONE)
5305 				map->ma_untagged = B_TRUE;
5306 			else
5307 				(void) mac_add_vlan(map, vid);
5308 
5309 			/*
5310 			 * If we fail to remove the MAC address HW
5311 			 * filter but then also fail to re-add the
5312 			 * VLAN HW filter then we are in a busted
5313 			 * state. We do our best by logging a warning
5314 			 * and returning the original 'err' that got
5315 			 * us here. At this point, traffic for this
5316 			 * address + VLAN combination will be dropped
5317 			 * until the user reboots the system. In the
5318 			 * future, it would be nice to have a system
5319 			 * that can compare the state of expected
5320 			 * classification according to mac to the
5321 			 * actual state of the provider, and report
5322 			 * and fix any inconsistencies.
5323 			 */
5324 			if (MAC_GROUP_HW_VLAN(group)) {
5325 				int err2;
5326 
5327 				err2 = mac_group_addvlan(group, vid);
5328 				if (err2 != 0) {
5329 					cmn_err(CE_WARN, "Failed to readd VLAN"
5330 					    " %u to group %d on MAC %s: %d.",
5331 					    vid, group->mrg_index, mip->mi_name,
5332 					    err2);
5333 				}
5334 			}
5335 
5336 			map->ma_nusers = 1;
5337 			return (err);
5338 		}
5339 
5340 		map->ma_group = NULL;
5341 		break;
5342 	case MAC_ADDRESS_TYPE_UNICAST_PROMISC:
5343 		err = i_mac_promisc_set(mip, B_FALSE);
5344 		break;
5345 	default:
5346 		panic("Unexpected ma_type 0x%x, file: %s, line %d",
5347 		    map->ma_type, __FILE__, __LINE__);
5348 	}
5349 
5350 	if (err != 0) {
5351 		map->ma_nusers = 1;
5352 		return (err);
5353 	}
5354 
5355 	/*
5356 	 * We created MAC address for the primary one at registration, so we
5357 	 * won't free it here. mac_fini_macaddr() will take care of it.
5358 	 */
5359 	if (bcmp(map->ma_addr, mip->mi_addr, map->ma_len) != 0)
5360 		mac_free_macaddr(map);
5361 
5362 	return (0);
5363 }
5364 
5365 /*
5366  * Update an existing MAC address. The caller need to make sure that the new
5367  * value has not been used.
5368  */
5369 int
5370 mac_update_macaddr(mac_address_t *map, uint8_t *mac_addr)
5371 {
5372 	mac_impl_t *mip = map->ma_mip;
5373 	int err = 0;
5374 
5375 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
5376 	ASSERT(mac_find_macaddr(mip, mac_addr) == NULL);
5377 
5378 	switch (map->ma_type) {
5379 	case MAC_ADDRESS_TYPE_UNICAST_CLASSIFIED:
5380 		/*
5381 		 * Update the primary address for drivers that are not
5382 		 * RINGS capable.
5383 		 */
5384 		if (mip->mi_rx_groups == NULL) {
5385 			err = mip->mi_unicst(mip->mi_driver, (const uint8_t *)
5386 			    mac_addr);
5387 			if (err != 0)
5388 				return (err);
5389 			break;
5390 		}
5391 
5392 		/*
5393 		 * If this MAC address is not currently in use,
5394 		 * simply break out and update the value.
5395 		 */
5396 		if (map->ma_nusers == 0)
5397 			break;
5398 
5399 		/*
5400 		 * Need to replace the MAC address associated with a group.
5401 		 */
5402 		err = mac_group_remmac(map->ma_group, map->ma_addr);
5403 		if (err != 0)
5404 			return (err);
5405 
5406 		err = mac_group_addmac(map->ma_group, mac_addr);
5407 
5408 		/*
5409 		 * Failure hints hardware error. The MAC layer needs to
5410 		 * have error notification facility to handle this.
5411 		 * Now, simply try to restore the value.
5412 		 */
5413 		if (err != 0)
5414 			(void) mac_group_addmac(map->ma_group, map->ma_addr);
5415 
5416 		break;
5417 	case MAC_ADDRESS_TYPE_UNICAST_PROMISC:
5418 		/*
5419 		 * Need to do nothing more if in promiscuous mode.
5420 		 */
5421 		break;
5422 	default:
5423 		ASSERT(B_FALSE);
5424 	}
5425 
5426 	/*
5427 	 * Successfully replaced the MAC address.
5428 	 */
5429 	if (err == 0)
5430 		bcopy(mac_addr, map->ma_addr, map->ma_len);
5431 
5432 	return (err);
5433 }
5434 
5435 /*
5436  * Freshen the MAC address with new value. Its caller must have updated the
5437  * hardware MAC address before calling this function.
5438  * This funcitons is supposed to be used to handle the MAC address change
5439  * notification from underlying drivers.
5440  */
5441 void
5442 mac_freshen_macaddr(mac_address_t *map, uint8_t *mac_addr)
5443 {
5444 	mac_impl_t *mip = map->ma_mip;
5445 
5446 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
5447 	ASSERT(mac_find_macaddr(mip, mac_addr) == NULL);
5448 
5449 	/*
5450 	 * Freshen the MAC address with new value.
5451 	 */
5452 	bcopy(mac_addr, map->ma_addr, map->ma_len);
5453 	bcopy(mac_addr, mip->mi_addr, map->ma_len);
5454 
5455 	/*
5456 	 * Update all MAC clients that share this MAC address.
5457 	 */
5458 	mac_unicast_update_clients(mip, map);
5459 }
5460 
5461 /*
5462  * Set up the primary MAC address.
5463  */
5464 void
5465 mac_init_macaddr(mac_impl_t *mip)
5466 {
5467 	mac_address_t *map;
5468 
5469 	/*
5470 	 * The reference count is initialized to zero, until it's really
5471 	 * activated.
5472 	 */
5473 	map = kmem_zalloc(sizeof (mac_address_t), KM_SLEEP);
5474 	map->ma_len = mip->mi_type->mt_addr_length;
5475 	bcopy(mip->mi_addr, map->ma_addr, map->ma_len);
5476 
5477 	/*
5478 	 * If driver advertises RINGS capability, it shouldn't have initialized
5479 	 * its primary MAC address. For other drivers, including VNIC, the
5480 	 * primary address must work after registration.
5481 	 */
5482 	if (mip->mi_rx_groups == NULL)
5483 		map->ma_type = MAC_ADDRESS_TYPE_UNICAST_CLASSIFIED;
5484 
5485 	map->ma_mip = mip;
5486 
5487 	mip->mi_addresses = map;
5488 }
5489 
5490 /*
5491  * Clean up the primary MAC address. Note, only one primary MAC address
5492  * is allowed. All other MAC addresses must have been freed appropriately.
5493  */
5494 void
5495 mac_fini_macaddr(mac_impl_t *mip)
5496 {
5497 	mac_address_t *map = mip->mi_addresses;
5498 
5499 	if (map == NULL)
5500 		return;
5501 
5502 	/*
5503 	 * If mi_addresses is initialized, there should be exactly one
5504 	 * entry left on the list with no users.
5505 	 */
5506 	VERIFY3S(map->ma_nusers, ==, 0);
5507 	VERIFY3P(map->ma_next, ==, NULL);
5508 	VERIFY3P(map->ma_vlans, ==, NULL);
5509 
5510 	kmem_free(map, sizeof (mac_address_t));
5511 	mip->mi_addresses = NULL;
5512 }
5513 
5514 /*
5515  * Logging related functions.
5516  *
5517  * Note that Kernel statistics have been extended to maintain fine
5518  * granularity of statistics viz. hardware lane, software lane, fanout
5519  * stats etc. However, extended accounting continues to support only
5520  * aggregate statistics like before.
5521  */
5522 
5523 /* Write the flow description to a netinfo_t record */
5524 static netinfo_t *
5525 mac_write_flow_desc(flow_entry_t *flent, mac_client_impl_t *mcip)
5526 {
5527 	netinfo_t		*ninfo;
5528 	net_desc_t		*ndesc;
5529 	flow_desc_t		*fdesc;
5530 	mac_resource_props_t	*mrp;
5531 
5532 	ninfo = kmem_zalloc(sizeof (netinfo_t), KM_NOSLEEP);
5533 	if (ninfo == NULL)
5534 		return (NULL);
5535 	ndesc = kmem_zalloc(sizeof (net_desc_t), KM_NOSLEEP);
5536 	if (ndesc == NULL) {
5537 		kmem_free(ninfo, sizeof (netinfo_t));
5538 		return (NULL);
5539 	}
5540 
5541 	/*
5542 	 * Grab the fe_lock to see a self-consistent fe_flow_desc.
5543 	 * Updates to the fe_flow_desc are done under the fe_lock
5544 	 */
5545 	mutex_enter(&flent->fe_lock);
5546 	fdesc = &flent->fe_flow_desc;
5547 	mrp = &flent->fe_resource_props;
5548 
5549 	ndesc->nd_name = flent->fe_flow_name;
5550 	ndesc->nd_devname = mcip->mci_name;
5551 	bcopy(fdesc->fd_src_mac, ndesc->nd_ehost, ETHERADDRL);
5552 	bcopy(fdesc->fd_dst_mac, ndesc->nd_edest, ETHERADDRL);
5553 	ndesc->nd_sap = htonl(fdesc->fd_sap);
5554 	ndesc->nd_isv4 = (uint8_t)fdesc->fd_ipversion == IPV4_VERSION;
5555 	ndesc->nd_bw_limit = mrp->mrp_maxbw;
5556 	if (ndesc->nd_isv4) {
5557 		ndesc->nd_saddr[3] = htonl(fdesc->fd_local_addr.s6_addr32[3]);
5558 		ndesc->nd_daddr[3] = htonl(fdesc->fd_remote_addr.s6_addr32[3]);
5559 	} else {
5560 		bcopy(&fdesc->fd_local_addr, ndesc->nd_saddr, IPV6_ADDR_LEN);
5561 		bcopy(&fdesc->fd_remote_addr, ndesc->nd_daddr, IPV6_ADDR_LEN);
5562 	}
5563 	ndesc->nd_sport = htons(fdesc->fd_local_port);
5564 	ndesc->nd_dport = htons(fdesc->fd_remote_port);
5565 	ndesc->nd_protocol = (uint8_t)fdesc->fd_protocol;
5566 	mutex_exit(&flent->fe_lock);
5567 
5568 	ninfo->ni_record = ndesc;
5569 	ninfo->ni_size = sizeof (net_desc_t);
5570 	ninfo->ni_type = EX_NET_FLDESC_REC;
5571 
5572 	return (ninfo);
5573 }
5574 
5575 /* Write the flow statistics to a netinfo_t record */
5576 static netinfo_t *
5577 mac_write_flow_stats(flow_entry_t *flent)
5578 {
5579 	netinfo_t		*ninfo;
5580 	net_stat_t		*nstat;
5581 	mac_soft_ring_set_t	*mac_srs;
5582 	mac_rx_stats_t		*mac_rx_stat;
5583 	mac_tx_stats_t		*mac_tx_stat;
5584 	int			i;
5585 
5586 	ninfo = kmem_zalloc(sizeof (netinfo_t), KM_NOSLEEP);
5587 	if (ninfo == NULL)
5588 		return (NULL);
5589 	nstat = kmem_zalloc(sizeof (net_stat_t), KM_NOSLEEP);
5590 	if (nstat == NULL) {
5591 		kmem_free(ninfo, sizeof (netinfo_t));
5592 		return (NULL);
5593 	}
5594 
5595 	nstat->ns_name = flent->fe_flow_name;
5596 	for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
5597 		mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
5598 		mac_rx_stat = &mac_srs->srs_rx.sr_stat;
5599 
5600 		nstat->ns_ibytes += mac_rx_stat->mrs_intrbytes +
5601 		    mac_rx_stat->mrs_pollbytes + mac_rx_stat->mrs_lclbytes;
5602 		nstat->ns_ipackets += mac_rx_stat->mrs_intrcnt +
5603 		    mac_rx_stat->mrs_pollcnt + mac_rx_stat->mrs_lclcnt;
5604 		nstat->ns_oerrors += mac_rx_stat->mrs_ierrors;
5605 	}
5606 
5607 	mac_srs = (mac_soft_ring_set_t *)(flent->fe_tx_srs);
5608 	if (mac_srs != NULL) {
5609 		mac_tx_stat = &mac_srs->srs_tx.st_stat;
5610 
5611 		nstat->ns_obytes = mac_tx_stat->mts_obytes;
5612 		nstat->ns_opackets = mac_tx_stat->mts_opackets;
5613 		nstat->ns_oerrors = mac_tx_stat->mts_oerrors;
5614 	}
5615 
5616 	ninfo->ni_record = nstat;
5617 	ninfo->ni_size = sizeof (net_stat_t);
5618 	ninfo->ni_type = EX_NET_FLSTAT_REC;
5619 
5620 	return (ninfo);
5621 }
5622 
5623 /* Write the link description to a netinfo_t record */
5624 static netinfo_t *
5625 mac_write_link_desc(mac_client_impl_t *mcip)
5626 {
5627 	netinfo_t		*ninfo;
5628 	net_desc_t		*ndesc;
5629 	flow_entry_t		*flent = mcip->mci_flent;
5630 
5631 	ninfo = kmem_zalloc(sizeof (netinfo_t), KM_NOSLEEP);
5632 	if (ninfo == NULL)
5633 		return (NULL);
5634 	ndesc = kmem_zalloc(sizeof (net_desc_t), KM_NOSLEEP);
5635 	if (ndesc == NULL) {
5636 		kmem_free(ninfo, sizeof (netinfo_t));
5637 		return (NULL);
5638 	}
5639 
5640 	ndesc->nd_name = mcip->mci_name;
5641 	ndesc->nd_devname = mcip->mci_name;
5642 	ndesc->nd_isv4 = B_TRUE;
5643 	/*
5644 	 * Grab the fe_lock to see a self-consistent fe_flow_desc.
5645 	 * Updates to the fe_flow_desc are done under the fe_lock
5646 	 * after removing the flent from the flow table.
5647 	 */
5648 	mutex_enter(&flent->fe_lock);
5649 	bcopy(flent->fe_flow_desc.fd_src_mac, ndesc->nd_ehost, ETHERADDRL);
5650 	mutex_exit(&flent->fe_lock);
5651 
5652 	ninfo->ni_record = ndesc;
5653 	ninfo->ni_size = sizeof (net_desc_t);
5654 	ninfo->ni_type = EX_NET_LNDESC_REC;
5655 
5656 	return (ninfo);
5657 }
5658 
5659 /* Write the link statistics to a netinfo_t record */
5660 static netinfo_t *
5661 mac_write_link_stats(mac_client_impl_t *mcip)
5662 {
5663 	netinfo_t		*ninfo;
5664 	net_stat_t		*nstat;
5665 	flow_entry_t		*flent;
5666 	mac_soft_ring_set_t	*mac_srs;
5667 	mac_rx_stats_t		*mac_rx_stat;
5668 	mac_tx_stats_t		*mac_tx_stat;
5669 	int			i;
5670 
5671 	ninfo = kmem_zalloc(sizeof (netinfo_t), KM_NOSLEEP);
5672 	if (ninfo == NULL)
5673 		return (NULL);
5674 	nstat = kmem_zalloc(sizeof (net_stat_t), KM_NOSLEEP);
5675 	if (nstat == NULL) {
5676 		kmem_free(ninfo, sizeof (netinfo_t));
5677 		return (NULL);
5678 	}
5679 
5680 	nstat->ns_name = mcip->mci_name;
5681 	flent = mcip->mci_flent;
5682 	if (flent != NULL)  {
5683 		for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
5684 			mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
5685 			mac_rx_stat = &mac_srs->srs_rx.sr_stat;
5686 
5687 			nstat->ns_ibytes += mac_rx_stat->mrs_intrbytes +
5688 			    mac_rx_stat->mrs_pollbytes +
5689 			    mac_rx_stat->mrs_lclbytes;
5690 			nstat->ns_ipackets += mac_rx_stat->mrs_intrcnt +
5691 			    mac_rx_stat->mrs_pollcnt + mac_rx_stat->mrs_lclcnt;
5692 			nstat->ns_oerrors += mac_rx_stat->mrs_ierrors;
5693 		}
5694 	}
5695 
5696 	mac_srs = (mac_soft_ring_set_t *)(mcip->mci_flent->fe_tx_srs);
5697 	if (mac_srs != NULL) {
5698 		mac_tx_stat = &mac_srs->srs_tx.st_stat;
5699 
5700 		nstat->ns_obytes = mac_tx_stat->mts_obytes;
5701 		nstat->ns_opackets = mac_tx_stat->mts_opackets;
5702 		nstat->ns_oerrors = mac_tx_stat->mts_oerrors;
5703 	}
5704 
5705 	ninfo->ni_record = nstat;
5706 	ninfo->ni_size = sizeof (net_stat_t);
5707 	ninfo->ni_type = EX_NET_LNSTAT_REC;
5708 
5709 	return (ninfo);
5710 }
5711 
5712 typedef struct i_mac_log_state_s {
5713 	boolean_t	mi_last;
5714 	int		mi_fenable;
5715 	int		mi_lenable;
5716 	list_t		*mi_list;
5717 } i_mac_log_state_t;
5718 
5719 /*
5720  * For a given flow, if the description has not been logged before, do it now.
5721  * If it is a VNIC, then we have collected information about it from the MAC
5722  * table, so skip it.
5723  *
5724  * Called through mac_flow_walk_nolock()
5725  *
5726  * Return 0 if successful.
5727  */
5728 static int
5729 mac_log_flowinfo(flow_entry_t *flent, void *arg)
5730 {
5731 	mac_client_impl_t	*mcip = flent->fe_mcip;
5732 	i_mac_log_state_t	*lstate = arg;
5733 	netinfo_t		*ninfo;
5734 
5735 	if (mcip == NULL)
5736 		return (0);
5737 
5738 	/*
5739 	 * If the name starts with "vnic", and fe_user_generated is true (to
5740 	 * exclude the mcast and active flow entries created implicitly for
5741 	 * a vnic, it is a VNIC flow.  i.e. vnic1 is a vnic flow,
5742 	 * vnic/bge1/mcast1 is not and neither is vnic/bge1/active.
5743 	 */
5744 	if (strncasecmp(flent->fe_flow_name, "vnic", 4) == 0 &&
5745 	    (flent->fe_type & FLOW_USER) != 0) {
5746 		return (0);
5747 	}
5748 
5749 	if (!flent->fe_desc_logged) {
5750 		/*
5751 		 * We don't return error because we want to continue the
5752 		 * walk in case this is the last walk which means we
5753 		 * need to reset fe_desc_logged in all the flows.
5754 		 */
5755 		if ((ninfo = mac_write_flow_desc(flent, mcip)) == NULL)
5756 			return (0);
5757 		list_insert_tail(lstate->mi_list, ninfo);
5758 		flent->fe_desc_logged = B_TRUE;
5759 	}
5760 
5761 	/*
5762 	 * Regardless of the error, we want to proceed in case we have to
5763 	 * reset fe_desc_logged.
5764 	 */
5765 	ninfo = mac_write_flow_stats(flent);
5766 	if (ninfo == NULL)
5767 		return (-1);
5768 
5769 	list_insert_tail(lstate->mi_list, ninfo);
5770 
5771 	if (mcip != NULL && !(mcip->mci_state_flags & MCIS_DESC_LOGGED))
5772 		flent->fe_desc_logged = B_FALSE;
5773 
5774 	return (0);
5775 }
5776 
5777 /*
5778  * Log the description for each mac client of this mac_impl_t, if it
5779  * hasn't already been done. Additionally, log statistics for the link as
5780  * well. Walk the flow table and log information for each flow as well.
5781  * If it is the last walk (mci_last), then we turn off mci_desc_logged (and
5782  * also fe_desc_logged, if flow logging is on) since we want to log the
5783  * description if and when logging is restarted.
5784  *
5785  * Return 0 upon success or -1 upon failure
5786  */
5787 static int
5788 i_mac_impl_log(mac_impl_t *mip, i_mac_log_state_t *lstate)
5789 {
5790 	mac_client_impl_t	*mcip;
5791 	netinfo_t		*ninfo;
5792 
5793 	i_mac_perim_enter(mip);
5794 	/*
5795 	 * Only walk the client list for NIC and etherstub
5796 	 */
5797 	if ((mip->mi_state_flags & MIS_DISABLED) ||
5798 	    ((mip->mi_state_flags & MIS_IS_VNIC) &&
5799 	    (mac_get_lower_mac_handle((mac_handle_t)mip) != NULL))) {
5800 		i_mac_perim_exit(mip);
5801 		return (0);
5802 	}
5803 
5804 	for (mcip = mip->mi_clients_list; mcip != NULL;
5805 	    mcip = mcip->mci_client_next) {
5806 		if (!MCIP_DATAPATH_SETUP(mcip))
5807 			continue;
5808 		if (lstate->mi_lenable) {
5809 			if (!(mcip->mci_state_flags & MCIS_DESC_LOGGED)) {
5810 				ninfo = mac_write_link_desc(mcip);
5811 				if (ninfo == NULL) {
5812 				/*
5813 				 * We can't terminate it if this is the last
5814 				 * walk, else there might be some links with
5815 				 * mi_desc_logged set to true, which means
5816 				 * their description won't be logged the next
5817 				 * time logging is started (similarly for the
5818 				 * flows within such links). We can continue
5819 				 * without walking the flow table (i.e. to
5820 				 * set fe_desc_logged to false) because we
5821 				 * won't have written any flow stuff for this
5822 				 * link as we haven't logged the link itself.
5823 				 */
5824 					i_mac_perim_exit(mip);
5825 					if (lstate->mi_last)
5826 						return (0);
5827 					else
5828 						return (-1);
5829 				}
5830 				mcip->mci_state_flags |= MCIS_DESC_LOGGED;
5831 				list_insert_tail(lstate->mi_list, ninfo);
5832 			}
5833 		}
5834 
5835 		ninfo = mac_write_link_stats(mcip);
5836 		if (ninfo == NULL && !lstate->mi_last) {
5837 			i_mac_perim_exit(mip);
5838 			return (-1);
5839 		}
5840 		list_insert_tail(lstate->mi_list, ninfo);
5841 
5842 		if (lstate->mi_last)
5843 			mcip->mci_state_flags &= ~MCIS_DESC_LOGGED;
5844 
5845 		if (lstate->mi_fenable) {
5846 			if (mcip->mci_subflow_tab != NULL) {
5847 				(void) mac_flow_walk_nolock(
5848 				    mcip->mci_subflow_tab, mac_log_flowinfo,
5849 				    lstate);
5850 			}
5851 		}
5852 	}
5853 	i_mac_perim_exit(mip);
5854 	return (0);
5855 }
5856 
5857 /*
5858  * modhash walker function to add a mac_impl_t to a list
5859  */
5860 /*ARGSUSED*/
5861 static uint_t
5862 i_mac_impl_list_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
5863 {
5864 	list_t			*list = (list_t *)arg;
5865 	mac_impl_t		*mip = (mac_impl_t *)val;
5866 
5867 	if ((mip->mi_state_flags & MIS_DISABLED) == 0) {
5868 		list_insert_tail(list, mip);
5869 		mip->mi_ref++;
5870 	}
5871 
5872 	return (MH_WALK_CONTINUE);
5873 }
5874 
5875 void
5876 i_mac_log_info(list_t *net_log_list, i_mac_log_state_t *lstate)
5877 {
5878 	list_t			mac_impl_list;
5879 	mac_impl_t		*mip;
5880 	netinfo_t		*ninfo;
5881 
5882 	/* Create list of mac_impls */
5883 	ASSERT(RW_LOCK_HELD(&i_mac_impl_lock));
5884 	list_create(&mac_impl_list, sizeof (mac_impl_t), offsetof(mac_impl_t,
5885 	    mi_node));
5886 	mod_hash_walk(i_mac_impl_hash, i_mac_impl_list_walker, &mac_impl_list);
5887 	rw_exit(&i_mac_impl_lock);
5888 
5889 	/* Create log entries for each mac_impl */
5890 	for (mip = list_head(&mac_impl_list); mip != NULL;
5891 	    mip = list_next(&mac_impl_list, mip)) {
5892 		if (i_mac_impl_log(mip, lstate) != 0)
5893 			continue;
5894 	}
5895 
5896 	/* Remove elements and destroy list of mac_impls */
5897 	rw_enter(&i_mac_impl_lock, RW_WRITER);
5898 	while ((mip = list_remove_tail(&mac_impl_list)) != NULL) {
5899 		mip->mi_ref--;
5900 	}
5901 	rw_exit(&i_mac_impl_lock);
5902 	list_destroy(&mac_impl_list);
5903 
5904 	/*
5905 	 * Write log entries to files outside of locks, free associated
5906 	 * structures, and remove entries from the list.
5907 	 */
5908 	while ((ninfo = list_head(net_log_list)) != NULL) {
5909 		(void) exacct_commit_netinfo(ninfo->ni_record, ninfo->ni_type);
5910 		list_remove(net_log_list, ninfo);
5911 		kmem_free(ninfo->ni_record, ninfo->ni_size);
5912 		kmem_free(ninfo, sizeof (*ninfo));
5913 	}
5914 	list_destroy(net_log_list);
5915 }
5916 
5917 /*
5918  * The timer thread that runs every mac_logging_interval seconds and logs
5919  * link and/or flow information.
5920  */
5921 /* ARGSUSED */
5922 void
5923 mac_log_linkinfo(void *arg)
5924 {
5925 	i_mac_log_state_t	lstate;
5926 	list_t			net_log_list;
5927 
5928 	list_create(&net_log_list, sizeof (netinfo_t),
5929 	    offsetof(netinfo_t, ni_link));
5930 
5931 	rw_enter(&i_mac_impl_lock, RW_READER);
5932 	if (!mac_flow_log_enable && !mac_link_log_enable) {
5933 		rw_exit(&i_mac_impl_lock);
5934 		return;
5935 	}
5936 	lstate.mi_fenable = mac_flow_log_enable;
5937 	lstate.mi_lenable = mac_link_log_enable;
5938 	lstate.mi_last = B_FALSE;
5939 	lstate.mi_list = &net_log_list;
5940 
5941 	/* Write log entries for each mac_impl in the list */
5942 	i_mac_log_info(&net_log_list, &lstate);
5943 
5944 	if (mac_flow_log_enable || mac_link_log_enable) {
5945 		mac_logging_timer = timeout(mac_log_linkinfo, NULL,
5946 		    SEC_TO_TICK(mac_logging_interval));
5947 	}
5948 }
5949 
5950 typedef struct i_mac_fastpath_state_s {
5951 	boolean_t	mf_disable;
5952 	int		mf_err;
5953 } i_mac_fastpath_state_t;
5954 
5955 /* modhash walker function to enable or disable fastpath */
5956 /*ARGSUSED*/
5957 static uint_t
5958 i_mac_fastpath_walker(mod_hash_key_t key, mod_hash_val_t *val,
5959     void *arg)
5960 {
5961 	i_mac_fastpath_state_t	*state = arg;
5962 	mac_handle_t		mh = (mac_handle_t)val;
5963 
5964 	if (state->mf_disable)
5965 		state->mf_err = mac_fastpath_disable(mh);
5966 	else
5967 		mac_fastpath_enable(mh);
5968 
5969 	return (state->mf_err == 0 ? MH_WALK_CONTINUE : MH_WALK_TERMINATE);
5970 }
5971 
5972 /*
5973  * Start the logging timer.
5974  */
5975 int
5976 mac_start_logusage(mac_logtype_t type, uint_t interval)
5977 {
5978 	i_mac_fastpath_state_t	dstate = {B_TRUE, 0};
5979 	i_mac_fastpath_state_t	estate = {B_FALSE, 0};
5980 	int			err;
5981 
5982 	rw_enter(&i_mac_impl_lock, RW_WRITER);
5983 	switch (type) {
5984 	case MAC_LOGTYPE_FLOW:
5985 		if (mac_flow_log_enable) {
5986 			rw_exit(&i_mac_impl_lock);
5987 			return (0);
5988 		}
5989 		/* FALLTHRU */
5990 	case MAC_LOGTYPE_LINK:
5991 		if (mac_link_log_enable) {
5992 			rw_exit(&i_mac_impl_lock);
5993 			return (0);
5994 		}
5995 		break;
5996 	default:
5997 		ASSERT(0);
5998 	}
5999 
6000 	/* Disable fastpath */
6001 	mod_hash_walk(i_mac_impl_hash, i_mac_fastpath_walker, &dstate);
6002 	if ((err = dstate.mf_err) != 0) {
6003 		/* Reenable fastpath  */
6004 		mod_hash_walk(i_mac_impl_hash, i_mac_fastpath_walker, &estate);
6005 		rw_exit(&i_mac_impl_lock);
6006 		return (err);
6007 	}
6008 
6009 	switch (type) {
6010 	case MAC_LOGTYPE_FLOW:
6011 		mac_flow_log_enable = B_TRUE;
6012 		/* FALLTHRU */
6013 	case MAC_LOGTYPE_LINK:
6014 		mac_link_log_enable = B_TRUE;
6015 		break;
6016 	}
6017 
6018 	mac_logging_interval = interval;
6019 	rw_exit(&i_mac_impl_lock);
6020 	mac_log_linkinfo(NULL);
6021 	return (0);
6022 }
6023 
6024 /*
6025  * Stop the logging timer if both link and flow logging are turned off.
6026  */
6027 void
6028 mac_stop_logusage(mac_logtype_t type)
6029 {
6030 	i_mac_log_state_t	lstate;
6031 	i_mac_fastpath_state_t	estate = {B_FALSE, 0};
6032 	list_t			net_log_list;
6033 
6034 	list_create(&net_log_list, sizeof (netinfo_t),
6035 	    offsetof(netinfo_t, ni_link));
6036 
6037 	rw_enter(&i_mac_impl_lock, RW_WRITER);
6038 
6039 	lstate.mi_fenable = mac_flow_log_enable;
6040 	lstate.mi_lenable = mac_link_log_enable;
6041 	lstate.mi_list = &net_log_list;
6042 
6043 	/* Last walk */
6044 	lstate.mi_last = B_TRUE;
6045 
6046 	switch (type) {
6047 	case MAC_LOGTYPE_FLOW:
6048 		if (lstate.mi_fenable) {
6049 			ASSERT(mac_link_log_enable);
6050 			mac_flow_log_enable = B_FALSE;
6051 			mac_link_log_enable = B_FALSE;
6052 			break;
6053 		}
6054 		/* FALLTHRU */
6055 	case MAC_LOGTYPE_LINK:
6056 		if (!lstate.mi_lenable || mac_flow_log_enable) {
6057 			rw_exit(&i_mac_impl_lock);
6058 			return;
6059 		}
6060 		mac_link_log_enable = B_FALSE;
6061 		break;
6062 	default:
6063 		ASSERT(0);
6064 	}
6065 
6066 	/* Reenable fastpath */
6067 	mod_hash_walk(i_mac_impl_hash, i_mac_fastpath_walker, &estate);
6068 
6069 	(void) untimeout(mac_logging_timer);
6070 	mac_logging_timer = NULL;
6071 
6072 	/* Write log entries for each mac_impl in the list */
6073 	i_mac_log_info(&net_log_list, &lstate);
6074 }
6075 
6076 /*
6077  * Walk the rx and tx SRS/SRs for a flow and update the priority value.
6078  */
6079 void
6080 mac_flow_update_priority(mac_client_impl_t *mcip, flow_entry_t *flent)
6081 {
6082 	pri_t			pri;
6083 	int			count;
6084 	mac_soft_ring_set_t	*mac_srs;
6085 
6086 	if (flent->fe_rx_srs_cnt <= 0)
6087 		return;
6088 
6089 	if (((mac_soft_ring_set_t *)flent->fe_rx_srs[0])->srs_type ==
6090 	    SRST_FLOW) {
6091 		pri = FLOW_PRIORITY(mcip->mci_min_pri,
6092 		    mcip->mci_max_pri,
6093 		    flent->fe_resource_props.mrp_priority);
6094 	} else {
6095 		pri = mcip->mci_max_pri;
6096 	}
6097 
6098 	for (count = 0; count < flent->fe_rx_srs_cnt; count++) {
6099 		mac_srs = flent->fe_rx_srs[count];
6100 		mac_update_srs_priority(mac_srs, pri);
6101 	}
6102 	/*
6103 	 * If we have a Tx SRS, we need to modify all the threads associated
6104 	 * with it.
6105 	 */
6106 	if (flent->fe_tx_srs != NULL)
6107 		mac_update_srs_priority(flent->fe_tx_srs, pri);
6108 }
6109 
6110 /*
6111  * RX and TX rings are reserved according to different semantics depending
6112  * on the requests from the MAC clients and type of rings:
6113  *
6114  * On the Tx side, by default we reserve individual rings, independently from
6115  * the groups.
6116  *
6117  * On the Rx side, the reservation is at the granularity of the group
6118  * of rings, and used for v12n level 1 only. It has a special case for the
6119  * primary client.
6120  *
6121  * If a share is allocated to a MAC client, we allocate a TX group and an
6122  * RX group to the client, and assign TX rings and RX rings to these
6123  * groups according to information gathered from the driver through
6124  * the share capability.
6125  *
6126  * The foreseable evolution of Rx rings will handle v12n level 2 and higher
6127  * to allocate individual rings out of a group and program the hw classifier
6128  * based on IP address or higher level criteria.
6129  */
6130 
6131 /*
6132  * mac_reserve_tx_ring()
6133  * Reserve a unused ring by marking it with MR_INUSE state.
6134  * As reserved, the ring is ready to function.
6135  *
6136  * Notes for Hybrid I/O:
6137  *
6138  * If a specific ring is needed, it is specified through the desired_ring
6139  * argument. Otherwise that argument is set to NULL.
6140  * If the desired ring was previous allocated to another client, this
6141  * function swaps it with a new ring from the group of unassigned rings.
6142  */
6143 mac_ring_t *
6144 mac_reserve_tx_ring(mac_impl_t *mip, mac_ring_t *desired_ring)
6145 {
6146 	mac_group_t		*group;
6147 	mac_grp_client_t	*mgcp;
6148 	mac_client_impl_t	*mcip;
6149 	mac_soft_ring_set_t	*srs;
6150 
6151 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
6152 
6153 	/*
6154 	 * Find an available ring and start it before changing its status.
6155 	 * The unassigned rings are at the end of the mi_tx_groups
6156 	 * array.
6157 	 */
6158 	group = MAC_DEFAULT_TX_GROUP(mip);
6159 
6160 	/* Can't take the default ring out of the default group */
6161 	ASSERT(desired_ring != (mac_ring_t *)mip->mi_default_tx_ring);
6162 
6163 	if (desired_ring->mr_state == MR_FREE) {
6164 		ASSERT(MAC_GROUP_NO_CLIENT(group));
6165 		if (mac_start_ring(desired_ring) != 0)
6166 			return (NULL);
6167 		return (desired_ring);
6168 	}
6169 	/*
6170 	 * There are clients using this ring, so let's move the clients
6171 	 * away from using this ring.
6172 	 */
6173 	for (mgcp = group->mrg_clients; mgcp != NULL; mgcp = mgcp->mgc_next) {
6174 		mcip = mgcp->mgc_client;
6175 		mac_tx_client_quiesce((mac_client_handle_t)mcip);
6176 		srs = MCIP_TX_SRS(mcip);
6177 		ASSERT(mac_tx_srs_ring_present(srs, desired_ring));
6178 		mac_tx_invoke_callbacks(mcip,
6179 		    (mac_tx_cookie_t)mac_tx_srs_get_soft_ring(srs,
6180 		    desired_ring));
6181 		mac_tx_srs_del_ring(srs, desired_ring);
6182 		mac_tx_client_restart((mac_client_handle_t)mcip);
6183 	}
6184 	return (desired_ring);
6185 }
6186 
6187 /*
6188  * For a non-default group with multiple clients, return the primary client.
6189  */
6190 static mac_client_impl_t *
6191 mac_get_grp_primary(mac_group_t *grp)
6192 {
6193 	mac_grp_client_t	*mgcp = grp->mrg_clients;
6194 	mac_client_impl_t	*mcip;
6195 
6196 	while (mgcp != NULL) {
6197 		mcip = mgcp->mgc_client;
6198 		if (mcip->mci_flent->fe_type & FLOW_PRIMARY_MAC)
6199 			return (mcip);
6200 		mgcp = mgcp->mgc_next;
6201 	}
6202 	return (NULL);
6203 }
6204 
6205 /*
6206  * Hybrid I/O specifies the ring that should be given to a share.
6207  * If the ring is already used by clients, then we need to release
6208  * the ring back to the default group so that we can give it to
6209  * the share. This means the clients using this ring now get a
6210  * replacement ring. If there aren't any replacement rings, this
6211  * function returns a failure.
6212  */
6213 static int
6214 mac_reclaim_ring_from_grp(mac_impl_t *mip, mac_ring_type_t ring_type,
6215     mac_ring_t *ring, mac_ring_t **rings, int nrings)
6216 {
6217 	mac_group_t		*group = (mac_group_t *)ring->mr_gh;
6218 	mac_resource_props_t	*mrp;
6219 	mac_client_impl_t	*mcip;
6220 	mac_group_t		*defgrp;
6221 	mac_ring_t		*tring;
6222 	mac_group_t		*tgrp;
6223 	int			i;
6224 	int			j;
6225 
6226 	mcip = MAC_GROUP_ONLY_CLIENT(group);
6227 	if (mcip == NULL)
6228 		mcip = mac_get_grp_primary(group);
6229 	ASSERT(mcip != NULL);
6230 	ASSERT(mcip->mci_share == 0);
6231 
6232 	mrp = MCIP_RESOURCE_PROPS(mcip);
6233 	if (ring_type == MAC_RING_TYPE_RX) {
6234 		defgrp = mip->mi_rx_donor_grp;
6235 		if ((mrp->mrp_mask & MRP_RX_RINGS) == 0) {
6236 			/* Need to put this mac client in the default group */
6237 			if (mac_rx_switch_group(mcip, group, defgrp) != 0)
6238 				return (ENOSPC);
6239 		} else {
6240 			/*
6241 			 * Switch this ring with some other ring from
6242 			 * the default group.
6243 			 */
6244 			for (tring = defgrp->mrg_rings; tring != NULL;
6245 			    tring = tring->mr_next) {
6246 				if (tring->mr_index == 0)
6247 					continue;
6248 				for (j = 0; j < nrings; j++) {
6249 					if (rings[j] == tring)
6250 						break;
6251 				}
6252 				if (j >= nrings)
6253 					break;
6254 			}
6255 			if (tring == NULL)
6256 				return (ENOSPC);
6257 			if (mac_group_mov_ring(mip, group, tring) != 0)
6258 				return (ENOSPC);
6259 			if (mac_group_mov_ring(mip, defgrp, ring) != 0) {
6260 				(void) mac_group_mov_ring(mip, defgrp, tring);
6261 				return (ENOSPC);
6262 			}
6263 		}
6264 		ASSERT(ring->mr_gh == (mac_group_handle_t)defgrp);
6265 		return (0);
6266 	}
6267 
6268 	defgrp = MAC_DEFAULT_TX_GROUP(mip);
6269 	if (ring == (mac_ring_t *)mip->mi_default_tx_ring) {
6270 		/*
6271 		 * See if we can get a spare ring to replace the default
6272 		 * ring.
6273 		 */
6274 		if (defgrp->mrg_cur_count == 1) {
6275 			/*
6276 			 * Need to get a ring from another client, see if
6277 			 * there are any clients that can be moved to
6278 			 * the default group, thereby freeing some rings.
6279 			 */
6280 			for (i = 0; i < mip->mi_tx_group_count; i++) {
6281 				tgrp = &mip->mi_tx_groups[i];
6282 				if (tgrp->mrg_state ==
6283 				    MAC_GROUP_STATE_REGISTERED) {
6284 					continue;
6285 				}
6286 				mcip = MAC_GROUP_ONLY_CLIENT(tgrp);
6287 				if (mcip == NULL)
6288 					mcip = mac_get_grp_primary(tgrp);
6289 				ASSERT(mcip != NULL);
6290 				mrp = MCIP_RESOURCE_PROPS(mcip);
6291 				if ((mrp->mrp_mask & MRP_TX_RINGS) == 0) {
6292 					ASSERT(tgrp->mrg_cur_count == 1);
6293 					/*
6294 					 * If this ring is part of the
6295 					 * rings asked by the share we cannot
6296 					 * use it as the default ring.
6297 					 */
6298 					for (j = 0; j < nrings; j++) {
6299 						if (rings[j] == tgrp->mrg_rings)
6300 							break;
6301 					}
6302 					if (j < nrings)
6303 						continue;
6304 					mac_tx_client_quiesce(
6305 					    (mac_client_handle_t)mcip);
6306 					mac_tx_switch_group(mcip, tgrp,
6307 					    defgrp);
6308 					mac_tx_client_restart(
6309 					    (mac_client_handle_t)mcip);
6310 					break;
6311 				}
6312 			}
6313 			/*
6314 			 * All the rings are reserved, can't give up the
6315 			 * default ring.
6316 			 */
6317 			if (defgrp->mrg_cur_count <= 1)
6318 				return (ENOSPC);
6319 		}
6320 		/*
6321 		 * Swap the default ring with another.
6322 		 */
6323 		for (tring = defgrp->mrg_rings; tring != NULL;
6324 		    tring = tring->mr_next) {
6325 			/*
6326 			 * If this ring is part of the rings asked by the
6327 			 * share we cannot use it as the default ring.
6328 			 */
6329 			for (j = 0; j < nrings; j++) {
6330 				if (rings[j] == tring)
6331 					break;
6332 			}
6333 			if (j >= nrings)
6334 				break;
6335 		}
6336 		ASSERT(tring != NULL);
6337 		mip->mi_default_tx_ring = (mac_ring_handle_t)tring;
6338 		return (0);
6339 	}
6340 	/*
6341 	 * The Tx ring is with a group reserved by a MAC client. See if
6342 	 * we can swap it.
6343 	 */
6344 	ASSERT(group->mrg_state == MAC_GROUP_STATE_RESERVED);
6345 	mcip = MAC_GROUP_ONLY_CLIENT(group);
6346 	if (mcip == NULL)
6347 		mcip = mac_get_grp_primary(group);
6348 	ASSERT(mcip !=  NULL);
6349 	mrp = MCIP_RESOURCE_PROPS(mcip);
6350 	mac_tx_client_quiesce((mac_client_handle_t)mcip);
6351 	if ((mrp->mrp_mask & MRP_TX_RINGS) == 0) {
6352 		ASSERT(group->mrg_cur_count == 1);
6353 		/* Put this mac client in the default group */
6354 		mac_tx_switch_group(mcip, group, defgrp);
6355 	} else {
6356 		/*
6357 		 * Switch this ring with some other ring from
6358 		 * the default group.
6359 		 */
6360 		for (tring = defgrp->mrg_rings; tring != NULL;
6361 		    tring = tring->mr_next) {
6362 			if (tring == (mac_ring_t *)mip->mi_default_tx_ring)
6363 				continue;
6364 			/*
6365 			 * If this ring is part of the rings asked by the
6366 			 * share we cannot use it for swapping.
6367 			 */
6368 			for (j = 0; j < nrings; j++) {
6369 				if (rings[j] == tring)
6370 					break;
6371 			}
6372 			if (j >= nrings)
6373 				break;
6374 		}
6375 		if (tring == NULL) {
6376 			mac_tx_client_restart((mac_client_handle_t)mcip);
6377 			return (ENOSPC);
6378 		}
6379 		if (mac_group_mov_ring(mip, group, tring) != 0) {
6380 			mac_tx_client_restart((mac_client_handle_t)mcip);
6381 			return (ENOSPC);
6382 		}
6383 		if (mac_group_mov_ring(mip, defgrp, ring) != 0) {
6384 			(void) mac_group_mov_ring(mip, defgrp, tring);
6385 			mac_tx_client_restart((mac_client_handle_t)mcip);
6386 			return (ENOSPC);
6387 		}
6388 	}
6389 	mac_tx_client_restart((mac_client_handle_t)mcip);
6390 	ASSERT(ring->mr_gh == (mac_group_handle_t)defgrp);
6391 	return (0);
6392 }
6393 
6394 /*
6395  * Populate a zero-ring group with rings. If the share is non-NULL,
6396  * the rings are chosen according to that share.
6397  * Invoked after allocating a new RX or TX group through
6398  * mac_reserve_rx_group() or mac_reserve_tx_group(), respectively.
6399  * Returns zero on success, an errno otherwise.
6400  */
6401 int
6402 i_mac_group_allocate_rings(mac_impl_t *mip, mac_ring_type_t ring_type,
6403     mac_group_t *src_group, mac_group_t *new_group, mac_share_handle_t share,
6404     uint32_t ringcnt)
6405 {
6406 	mac_ring_t **rings, *ring;
6407 	uint_t nrings;
6408 	int rv = 0, i = 0, j;
6409 
6410 	ASSERT((ring_type == MAC_RING_TYPE_RX &&
6411 	    mip->mi_rx_group_type == MAC_GROUP_TYPE_DYNAMIC) ||
6412 	    (ring_type == MAC_RING_TYPE_TX &&
6413 	    mip->mi_tx_group_type == MAC_GROUP_TYPE_DYNAMIC));
6414 
6415 	/*
6416 	 * First find the rings to allocate to the group.
6417 	 */
6418 	if (share != 0) {
6419 		/* get rings through ms_squery() */
6420 		mip->mi_share_capab.ms_squery(share, ring_type, NULL, &nrings);
6421 		ASSERT(nrings != 0);
6422 		rings = kmem_alloc(nrings * sizeof (mac_ring_handle_t),
6423 		    KM_SLEEP);
6424 		mip->mi_share_capab.ms_squery(share, ring_type,
6425 		    (mac_ring_handle_t *)rings, &nrings);
6426 		for (i = 0; i < nrings; i++) {
6427 			/*
6428 			 * If we have given this ring to a non-default
6429 			 * group, we need to check if we can get this
6430 			 * ring.
6431 			 */
6432 			ring = rings[i];
6433 			if (ring->mr_gh != (mac_group_handle_t)src_group ||
6434 			    ring == (mac_ring_t *)mip->mi_default_tx_ring) {
6435 				if (mac_reclaim_ring_from_grp(mip, ring_type,
6436 				    ring, rings, nrings) != 0) {
6437 					rv = ENOSPC;
6438 					goto bail;
6439 				}
6440 			}
6441 		}
6442 	} else {
6443 		/*
6444 		 * Pick one ring from default group.
6445 		 *
6446 		 * for now pick the second ring which requires the first ring
6447 		 * at index 0 to stay in the default group, since it is the
6448 		 * ring which carries the multicast traffic.
6449 		 * We need a better way for a driver to indicate this,
6450 		 * for example a per-ring flag.
6451 		 */
6452 		rings = kmem_alloc(ringcnt * sizeof (mac_ring_handle_t),
6453 		    KM_SLEEP);
6454 		for (ring = src_group->mrg_rings; ring != NULL;
6455 		    ring = ring->mr_next) {
6456 			if (ring_type == MAC_RING_TYPE_RX &&
6457 			    ring->mr_index == 0) {
6458 				continue;
6459 			}
6460 			if (ring_type == MAC_RING_TYPE_TX &&
6461 			    ring == (mac_ring_t *)mip->mi_default_tx_ring) {
6462 				continue;
6463 			}
6464 			rings[i++] = ring;
6465 			if (i == ringcnt)
6466 				break;
6467 		}
6468 		ASSERT(ring != NULL);
6469 		nrings = i;
6470 		/* Not enough rings as required */
6471 		if (nrings != ringcnt) {
6472 			rv = ENOSPC;
6473 			goto bail;
6474 		}
6475 	}
6476 
6477 	switch (ring_type) {
6478 	case MAC_RING_TYPE_RX:
6479 		if (src_group->mrg_cur_count - nrings < 1) {
6480 			/* we ran out of rings */
6481 			rv = ENOSPC;
6482 			goto bail;
6483 		}
6484 
6485 		/* move receive rings to new group */
6486 		for (i = 0; i < nrings; i++) {
6487 			rv = mac_group_mov_ring(mip, new_group, rings[i]);
6488 			if (rv != 0) {
6489 				/* move rings back on failure */
6490 				for (j = 0; j < i; j++) {
6491 					(void) mac_group_mov_ring(mip,
6492 					    src_group, rings[j]);
6493 				}
6494 				goto bail;
6495 			}
6496 		}
6497 		break;
6498 
6499 	case MAC_RING_TYPE_TX: {
6500 		mac_ring_t *tmp_ring;
6501 
6502 		/* move the TX rings to the new group */
6503 		for (i = 0; i < nrings; i++) {
6504 			/* get the desired ring */
6505 			tmp_ring = mac_reserve_tx_ring(mip, rings[i]);
6506 			if (tmp_ring == NULL) {
6507 				rv = ENOSPC;
6508 				goto bail;
6509 			}
6510 			ASSERT(tmp_ring == rings[i]);
6511 			rv = mac_group_mov_ring(mip, new_group, rings[i]);
6512 			if (rv != 0) {
6513 				/* cleanup on failure */
6514 				for (j = 0; j < i; j++) {
6515 					(void) mac_group_mov_ring(mip,
6516 					    MAC_DEFAULT_TX_GROUP(mip),
6517 					    rings[j]);
6518 				}
6519 				goto bail;
6520 			}
6521 		}
6522 		break;
6523 	}
6524 	}
6525 
6526 	/* add group to share */
6527 	if (share != 0)
6528 		mip->mi_share_capab.ms_sadd(share, new_group->mrg_driver);
6529 
6530 bail:
6531 	/* free temporary array of rings */
6532 	kmem_free(rings, nrings * sizeof (mac_ring_handle_t));
6533 
6534 	return (rv);
6535 }
6536 
6537 void
6538 mac_group_add_client(mac_group_t *grp, mac_client_impl_t *mcip)
6539 {
6540 	mac_grp_client_t *mgcp;
6541 
6542 	for (mgcp = grp->mrg_clients; mgcp != NULL; mgcp = mgcp->mgc_next) {
6543 		if (mgcp->mgc_client == mcip)
6544 			break;
6545 	}
6546 
6547 	ASSERT(mgcp == NULL);
6548 
6549 	mgcp = kmem_zalloc(sizeof (mac_grp_client_t), KM_SLEEP);
6550 	mgcp->mgc_client = mcip;
6551 	mgcp->mgc_next = grp->mrg_clients;
6552 	grp->mrg_clients = mgcp;
6553 }
6554 
6555 void
6556 mac_group_remove_client(mac_group_t *grp, mac_client_impl_t *mcip)
6557 {
6558 	mac_grp_client_t *mgcp, **pprev;
6559 
6560 	for (pprev = &grp->mrg_clients, mgcp = *pprev; mgcp != NULL;
6561 	    pprev = &mgcp->mgc_next, mgcp = *pprev) {
6562 		if (mgcp->mgc_client == mcip)
6563 			break;
6564 	}
6565 
6566 	ASSERT(mgcp != NULL);
6567 
6568 	*pprev = mgcp->mgc_next;
6569 	kmem_free(mgcp, sizeof (mac_grp_client_t));
6570 }
6571 
6572 /*
6573  * Return true if any client on this group explicitly asked for HW
6574  * rings (of type mask) or have a bound share.
6575  */
6576 static boolean_t
6577 i_mac_clients_hw(mac_group_t *grp, uint32_t mask)
6578 {
6579 	mac_grp_client_t	*mgcip;
6580 	mac_client_impl_t	*mcip;
6581 	mac_resource_props_t	*mrp;
6582 
6583 	for (mgcip = grp->mrg_clients; mgcip != NULL; mgcip = mgcip->mgc_next) {
6584 		mcip = mgcip->mgc_client;
6585 		mrp = MCIP_RESOURCE_PROPS(mcip);
6586 		if (mcip->mci_share != 0 || (mrp->mrp_mask & mask) != 0)
6587 			return (B_TRUE);
6588 	}
6589 
6590 	return (B_FALSE);
6591 }
6592 
6593 /*
6594  * Finds an available group and exclusively reserves it for a client.
6595  * The group is chosen to suit the flow's resource controls (bandwidth and
6596  * fanout requirements) and the address type.
6597  * If the requestor is the pimary MAC then return the group with the
6598  * largest number of rings, otherwise the default ring when available.
6599  */
6600 mac_group_t *
6601 mac_reserve_rx_group(mac_client_impl_t *mcip, uint8_t *mac_addr, boolean_t move)
6602 {
6603 	mac_share_handle_t	share = mcip->mci_share;
6604 	mac_impl_t		*mip = mcip->mci_mip;
6605 	mac_group_t		*grp = NULL;
6606 	int			i;
6607 	int			err = 0;
6608 	mac_address_t		*map;
6609 	mac_resource_props_t	*mrp = MCIP_RESOURCE_PROPS(mcip);
6610 	int			nrings;
6611 	int			donor_grp_rcnt;
6612 	boolean_t		need_exclgrp = B_FALSE;
6613 	int			need_rings = 0;
6614 	mac_group_t		*candidate_grp = NULL;
6615 	mac_client_impl_t	*gclient;
6616 	mac_group_t		*donorgrp = NULL;
6617 	boolean_t		rxhw = mrp->mrp_mask & MRP_RX_RINGS;
6618 	boolean_t		unspec = mrp->mrp_mask & MRP_RXRINGS_UNSPEC;
6619 	boolean_t		isprimary;
6620 
6621 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
6622 
6623 	isprimary = mcip->mci_flent->fe_type & FLOW_PRIMARY_MAC;
6624 
6625 	/*
6626 	 * Check if a group already has this MAC address (case of VLANs)
6627 	 * unless we are moving this MAC client from one group to another.
6628 	 */
6629 	if (!move && (map = mac_find_macaddr(mip, mac_addr)) != NULL) {
6630 		if (map->ma_group != NULL)
6631 			return (map->ma_group);
6632 	}
6633 
6634 	if (mip->mi_rx_groups == NULL || mip->mi_rx_group_count == 0)
6635 		return (NULL);
6636 
6637 	/*
6638 	 * If this client is requesting exclusive MAC access then
6639 	 * return NULL to ensure the client uses the default group.
6640 	 */
6641 	if (mcip->mci_state_flags & MCIS_EXCLUSIVE)
6642 		return (NULL);
6643 
6644 	/* For dynamic groups default unspecified to 1 */
6645 	if (rxhw && unspec &&
6646 	    mip->mi_rx_group_type == MAC_GROUP_TYPE_DYNAMIC) {
6647 		mrp->mrp_nrxrings = 1;
6648 	}
6649 
6650 	/*
6651 	 * For static grouping we allow only specifying rings=0 and
6652 	 * unspecified
6653 	 */
6654 	if (rxhw && mrp->mrp_nrxrings > 0 &&
6655 	    mip->mi_rx_group_type == MAC_GROUP_TYPE_STATIC) {
6656 		return (NULL);
6657 	}
6658 
6659 	if (rxhw) {
6660 		/*
6661 		 * We have explicitly asked for a group (with nrxrings,
6662 		 * if unspec).
6663 		 */
6664 		if (unspec || mrp->mrp_nrxrings > 0) {
6665 			need_exclgrp = B_TRUE;
6666 			need_rings = mrp->mrp_nrxrings;
6667 		} else if (mrp->mrp_nrxrings == 0) {
6668 			/*
6669 			 * We have asked for a software group.
6670 			 */
6671 			return (NULL);
6672 		}
6673 	} else if (isprimary && mip->mi_nactiveclients == 1 &&
6674 	    mip->mi_rx_group_type == MAC_GROUP_TYPE_DYNAMIC) {
6675 		/*
6676 		 * If the primary is the only active client on this
6677 		 * mip and we have not asked for any rings, we give
6678 		 * it the default group so that the primary gets to
6679 		 * use all the rings.
6680 		 */
6681 		return (NULL);
6682 	}
6683 
6684 	/* The group that can donate rings */
6685 	donorgrp = mip->mi_rx_donor_grp;
6686 
6687 	/*
6688 	 * The number of rings that the default group can donate.
6689 	 * We need to leave at least one ring.
6690 	 */
6691 	donor_grp_rcnt = donorgrp->mrg_cur_count - 1;
6692 
6693 	/*
6694 	 * Try to exclusively reserve a RX group.
6695 	 *
6696 	 * For flows requiring HW_DEFAULT_RING (unicast flow of the primary
6697 	 * client), try to reserve the a non-default RX group and give
6698 	 * it all the rings from the donor group, except the default ring
6699 	 *
6700 	 * For flows requiring HW_RING (unicast flow of other clients), try
6701 	 * to reserve non-default RX group with the specified number of
6702 	 * rings, if available.
6703 	 *
6704 	 * For flows that have not asked for software or hardware ring,
6705 	 * try to reserve a non-default group with 1 ring, if available.
6706 	 */
6707 	for (i = 1; i < mip->mi_rx_group_count; i++) {
6708 		grp = &mip->mi_rx_groups[i];
6709 
6710 		DTRACE_PROBE3(rx__group__trying, char *, mip->mi_name,
6711 		    int, grp->mrg_index, mac_group_state_t, grp->mrg_state);
6712 
6713 		/*
6714 		 * Check if this group could be a candidate group for
6715 		 * eviction if we need a group for this MAC client,
6716 		 * but there aren't any. A candidate group is one
6717 		 * that didn't ask for an exclusive group, but got
6718 		 * one and it has enough rings (combined with what
6719 		 * the donor group can donate) for the new MAC
6720 		 * client.
6721 		 */
6722 		if (grp->mrg_state >= MAC_GROUP_STATE_RESERVED) {
6723 			/*
6724 			 * If the donor group is not the default
6725 			 * group, don't bother looking for a candidate
6726 			 * group. If we don't have enough rings we
6727 			 * will check if the primary group can be
6728 			 * vacated.
6729 			 */
6730 			if (candidate_grp == NULL &&
6731 			    donorgrp == MAC_DEFAULT_RX_GROUP(mip)) {
6732 				if (!i_mac_clients_hw(grp, MRP_RX_RINGS) &&
6733 				    (unspec ||
6734 				    (grp->mrg_cur_count + donor_grp_rcnt >=
6735 				    need_rings))) {
6736 					candidate_grp = grp;
6737 				}
6738 			}
6739 			continue;
6740 		}
6741 		/*
6742 		 * This group could already be SHARED by other multicast
6743 		 * flows on this client. In that case, the group would
6744 		 * be shared and has already been started.
6745 		 */
6746 		ASSERT(grp->mrg_state != MAC_GROUP_STATE_UNINIT);
6747 
6748 		if ((grp->mrg_state == MAC_GROUP_STATE_REGISTERED) &&
6749 		    (mac_start_group(grp) != 0)) {
6750 			continue;
6751 		}
6752 
6753 		if (mip->mi_rx_group_type != MAC_GROUP_TYPE_DYNAMIC)
6754 			break;
6755 		ASSERT(grp->mrg_cur_count == 0);
6756 
6757 		/*
6758 		 * Populate the group. Rings should be taken
6759 		 * from the donor group.
6760 		 */
6761 		nrings = rxhw ? need_rings : isprimary ? donor_grp_rcnt: 1;
6762 
6763 		/*
6764 		 * If the donor group can't donate, let's just walk and
6765 		 * see if someone can vacate a group, so that we have
6766 		 * enough rings for this, unless we already have
6767 		 * identified a candiate group..
6768 		 */
6769 		if (nrings <= donor_grp_rcnt) {
6770 			err = i_mac_group_allocate_rings(mip, MAC_RING_TYPE_RX,
6771 			    donorgrp, grp, share, nrings);
6772 			if (err == 0) {
6773 				/*
6774 				 * For a share i_mac_group_allocate_rings gets
6775 				 * the rings from the driver, let's populate
6776 				 * the property for the client now.
6777 				 */
6778 				if (share != 0) {
6779 					mac_client_set_rings(
6780 					    (mac_client_handle_t)mcip,
6781 					    grp->mrg_cur_count, -1);
6782 				}
6783 				if (mac_is_primary_client(mcip) && !rxhw)
6784 					mip->mi_rx_donor_grp = grp;
6785 				break;
6786 			}
6787 		}
6788 
6789 		DTRACE_PROBE3(rx__group__reserve__alloc__rings, char *,
6790 		    mip->mi_name, int, grp->mrg_index, int, err);
6791 
6792 		/*
6793 		 * It's a dynamic group but the grouping operation
6794 		 * failed.
6795 		 */
6796 		mac_stop_group(grp);
6797 	}
6798 
6799 	/* We didn't find an exclusive group for this MAC client */
6800 	if (i >= mip->mi_rx_group_count) {
6801 
6802 		if (!need_exclgrp)
6803 			return (NULL);
6804 
6805 		/*
6806 		 * If we found a candidate group then move the
6807 		 * existing MAC client from the candidate_group to the
6808 		 * default group and give the candidate_group to the
6809 		 * new MAC client. If we didn't find a candidate
6810 		 * group, then check if the primary is in its own
6811 		 * group and if it can make way for this MAC client.
6812 		 */
6813 		if (candidate_grp == NULL &&
6814 		    donorgrp != MAC_DEFAULT_RX_GROUP(mip) &&
6815 		    donorgrp->mrg_cur_count >= need_rings) {
6816 			candidate_grp = donorgrp;
6817 		}
6818 		if (candidate_grp != NULL) {
6819 			boolean_t	prim_grp = B_FALSE;
6820 
6821 			/*
6822 			 * Switch the existing MAC client from the
6823 			 * candidate group to the default group. If
6824 			 * the candidate group is the donor group,
6825 			 * then after the switch we need to update the
6826 			 * donor group too.
6827 			 */
6828 			grp = candidate_grp;
6829 			gclient = grp->mrg_clients->mgc_client;
6830 			VERIFY3P(gclient, !=, NULL);
6831 			if (grp == mip->mi_rx_donor_grp)
6832 				prim_grp = B_TRUE;
6833 			if (mac_rx_switch_group(gclient, grp,
6834 			    MAC_DEFAULT_RX_GROUP(mip)) != 0) {
6835 				return (NULL);
6836 			}
6837 			if (prim_grp) {
6838 				mip->mi_rx_donor_grp =
6839 				    MAC_DEFAULT_RX_GROUP(mip);
6840 				donorgrp = MAC_DEFAULT_RX_GROUP(mip);
6841 			}
6842 
6843 			/*
6844 			 * Now give this group with the required rings
6845 			 * to this MAC client.
6846 			 */
6847 			ASSERT(grp->mrg_state == MAC_GROUP_STATE_REGISTERED);
6848 			if (mac_start_group(grp) != 0)
6849 				return (NULL);
6850 
6851 			if (mip->mi_rx_group_type != MAC_GROUP_TYPE_DYNAMIC)
6852 				return (grp);
6853 
6854 			donor_grp_rcnt = donorgrp->mrg_cur_count - 1;
6855 			ASSERT(grp->mrg_cur_count == 0);
6856 			ASSERT(donor_grp_rcnt >= need_rings);
6857 			err = i_mac_group_allocate_rings(mip, MAC_RING_TYPE_RX,
6858 			    donorgrp, grp, share, need_rings);
6859 			if (err == 0) {
6860 				/*
6861 				 * For a share i_mac_group_allocate_rings gets
6862 				 * the rings from the driver, let's populate
6863 				 * the property for the client now.
6864 				 */
6865 				if (share != 0) {
6866 					mac_client_set_rings(
6867 					    (mac_client_handle_t)mcip,
6868 					    grp->mrg_cur_count, -1);
6869 				}
6870 				DTRACE_PROBE2(rx__group__reserved,
6871 				    char *, mip->mi_name, int, grp->mrg_index);
6872 				return (grp);
6873 			}
6874 			DTRACE_PROBE3(rx__group__reserve__alloc__rings, char *,
6875 			    mip->mi_name, int, grp->mrg_index, int, err);
6876 			mac_stop_group(grp);
6877 		}
6878 		return (NULL);
6879 	}
6880 	ASSERT(grp != NULL);
6881 
6882 	DTRACE_PROBE2(rx__group__reserved,
6883 	    char *, mip->mi_name, int, grp->mrg_index);
6884 	return (grp);
6885 }
6886 
6887 /*
6888  * mac_rx_release_group()
6889  *
6890  * Release the group when it has no remaining clients. The group is
6891  * stopped and its shares are removed and all rings are assigned back
6892  * to default group. This should never be called against the default
6893  * group.
6894  */
6895 void
6896 mac_release_rx_group(mac_client_impl_t *mcip, mac_group_t *group)
6897 {
6898 	mac_impl_t		*mip = mcip->mci_mip;
6899 	mac_ring_t		*ring;
6900 
6901 	ASSERT(group != MAC_DEFAULT_RX_GROUP(mip));
6902 	ASSERT(MAC_GROUP_NO_CLIENT(group) == B_TRUE);
6903 
6904 	if (mip->mi_rx_donor_grp == group)
6905 		mip->mi_rx_donor_grp = MAC_DEFAULT_RX_GROUP(mip);
6906 
6907 	/*
6908 	 * This is the case where there are no clients left. Any
6909 	 * SRS etc on this group have also be quiesced.
6910 	 */
6911 	for (ring = group->mrg_rings; ring != NULL; ring = ring->mr_next) {
6912 		if (ring->mr_classify_type == MAC_HW_CLASSIFIER) {
6913 			ASSERT(group->mrg_state == MAC_GROUP_STATE_RESERVED);
6914 			/*
6915 			 * Remove the SRS associated with the HW ring.
6916 			 * As a result, polling will be disabled.
6917 			 */
6918 			ring->mr_srs = NULL;
6919 		}
6920 		ASSERT(group->mrg_state < MAC_GROUP_STATE_RESERVED ||
6921 		    ring->mr_state == MR_INUSE);
6922 		if (ring->mr_state == MR_INUSE) {
6923 			mac_stop_ring(ring);
6924 			ring->mr_flag = 0;
6925 		}
6926 	}
6927 
6928 	/* remove group from share */
6929 	if (mcip->mci_share != 0) {
6930 		mip->mi_share_capab.ms_sremove(mcip->mci_share,
6931 		    group->mrg_driver);
6932 	}
6933 
6934 	if (mip->mi_rx_group_type == MAC_GROUP_TYPE_DYNAMIC) {
6935 		mac_ring_t *ring;
6936 
6937 		/*
6938 		 * Rings were dynamically allocated to group.
6939 		 * Move rings back to default group.
6940 		 */
6941 		while ((ring = group->mrg_rings) != NULL) {
6942 			(void) mac_group_mov_ring(mip, mip->mi_rx_donor_grp,
6943 			    ring);
6944 		}
6945 	}
6946 	mac_stop_group(group);
6947 	/*
6948 	 * Possible improvement: See if we can assign the group just released
6949 	 * to a another client of the mip
6950 	 */
6951 }
6952 
6953 /*
6954  * Move the MAC address from fgrp to tgrp.
6955  */
6956 static int
6957 mac_rx_move_macaddr(mac_client_impl_t *mcip, mac_group_t *fgrp,
6958     mac_group_t *tgrp)
6959 {
6960 	mac_impl_t		*mip = mcip->mci_mip;
6961 	uint8_t			maddr[MAXMACADDRLEN];
6962 	int			err = 0;
6963 	uint16_t		vid;
6964 	mac_unicast_impl_t	*muip;
6965 	boolean_t		use_hw;
6966 
6967 	mac_rx_client_quiesce((mac_client_handle_t)mcip);
6968 	VERIFY3P(mcip->mci_unicast, !=, NULL);
6969 	bcopy(mcip->mci_unicast->ma_addr, maddr, mcip->mci_unicast->ma_len);
6970 
6971 	/*
6972 	 * Does the client require MAC address hardware classifiction?
6973 	 */
6974 	use_hw = (mcip->mci_state_flags & MCIS_UNICAST_HW) != 0;
6975 	vid = i_mac_flow_vid(mcip->mci_flent);
6976 
6977 	/*
6978 	 * You can never move an address that is shared by multiple
6979 	 * clients. mac_datapath_setup() ensures that clients sharing
6980 	 * an address are placed on the default group. This guarantees
6981 	 * that a non-default group will only ever have one client and
6982 	 * thus make full use of HW filters.
6983 	 */
6984 	if (mac_check_macaddr_shared(mcip->mci_unicast))
6985 		return (EINVAL);
6986 
6987 	err = mac_remove_macaddr_vlan(mcip->mci_unicast, vid);
6988 
6989 	if (err != 0) {
6990 		mac_rx_client_restart((mac_client_handle_t)mcip);
6991 		return (err);
6992 	}
6993 
6994 	/*
6995 	 * If this isn't the primary MAC address then the
6996 	 * mac_address_t has been freed by the last call to
6997 	 * mac_remove_macaddr_vlan(). In any case, NULL the reference
6998 	 * to avoid a dangling pointer.
6999 	 */
7000 	mcip->mci_unicast = NULL;
7001 
7002 	/*
7003 	 * We also have to NULL all the mui_map references -- sun4v
7004 	 * strikes again!
7005 	 */
7006 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
7007 	for (muip = mcip->mci_unicast_list; muip != NULL; muip = muip->mui_next)
7008 		muip->mui_map = NULL;
7009 	rw_exit(&mcip->mci_rw_lock);
7010 
7011 	/*
7012 	 * Program the H/W Classifier first, if this fails we need not
7013 	 * proceed with the other stuff.
7014 	 */
7015 	if ((err = mac_add_macaddr_vlan(mip, tgrp, maddr, vid, use_hw)) != 0) {
7016 		int err2;
7017 
7018 		/* Revert back the H/W Classifier */
7019 		err2 = mac_add_macaddr_vlan(mip, fgrp, maddr, vid, use_hw);
7020 
7021 		if (err2 != 0) {
7022 			cmn_err(CE_WARN, "Failed to revert HW classification"
7023 			    " on MAC %s, for client %s: %d.", mip->mi_name,
7024 			    mcip->mci_name, err2);
7025 		}
7026 
7027 		mac_rx_client_restart((mac_client_handle_t)mcip);
7028 		return (err);
7029 	}
7030 
7031 	/*
7032 	 * Get a reference to the new mac_address_t and update the
7033 	 * client's reference. Then restart the client and add the
7034 	 * other clients of this MAC addr (if they exsit).
7035 	 */
7036 	mcip->mci_unicast = mac_find_macaddr(mip, maddr);
7037 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
7038 	for (muip = mcip->mci_unicast_list; muip != NULL; muip = muip->mui_next)
7039 		muip->mui_map = mcip->mci_unicast;
7040 	rw_exit(&mcip->mci_rw_lock);
7041 	mac_rx_client_restart((mac_client_handle_t)mcip);
7042 	return (0);
7043 }
7044 
7045 /*
7046  * Switch the MAC client from one group to another. This means we need
7047  * to remove the MAC address from the group, remove the MAC client,
7048  * teardown the SRSs and revert the group state. Then, we add the client
7049  * to the destination group, set the SRSs, and add the MAC address to the
7050  * group.
7051  */
7052 int
7053 mac_rx_switch_group(mac_client_impl_t *mcip, mac_group_t *fgrp,
7054     mac_group_t *tgrp)
7055 {
7056 	int			err;
7057 	mac_group_state_t	next_state;
7058 	mac_client_impl_t	*group_only_mcip;
7059 	mac_client_impl_t	*gmcip;
7060 	mac_impl_t		*mip = mcip->mci_mip;
7061 	mac_grp_client_t	*mgcp;
7062 
7063 	VERIFY3P(fgrp, ==, mcip->mci_flent->fe_rx_ring_group);
7064 
7065 	if ((err = mac_rx_move_macaddr(mcip, fgrp, tgrp)) != 0)
7066 		return (err);
7067 
7068 	/*
7069 	 * If the group is marked as reserved and in use by a single
7070 	 * client, then there is an SRS to teardown.
7071 	 */
7072 	if (fgrp->mrg_state == MAC_GROUP_STATE_RESERVED &&
7073 	    MAC_GROUP_ONLY_CLIENT(fgrp) != NULL) {
7074 		mac_rx_srs_group_teardown(mcip->mci_flent, B_TRUE);
7075 	}
7076 
7077 	/*
7078 	 * If we are moving the client from a non-default group, then
7079 	 * we know that any additional clients on this group share the
7080 	 * same MAC address. Since we moved the MAC address filter, we
7081 	 * need to move these clients too.
7082 	 *
7083 	 * If we are moving the client from the default group and its
7084 	 * MAC address has VLAN clients, then we must move those
7085 	 * clients as well.
7086 	 *
7087 	 * In both cases the idea is the same: we moved the MAC
7088 	 * address filter to the tgrp, so we must move all clients
7089 	 * using that MAC address to tgrp as well.
7090 	 */
7091 	if (fgrp != MAC_DEFAULT_RX_GROUP(mip)) {
7092 		mgcp = fgrp->mrg_clients;
7093 		while (mgcp != NULL) {
7094 			gmcip = mgcp->mgc_client;
7095 			mgcp = mgcp->mgc_next;
7096 			mac_group_remove_client(fgrp, gmcip);
7097 			mac_group_add_client(tgrp, gmcip);
7098 			gmcip->mci_flent->fe_rx_ring_group = tgrp;
7099 		}
7100 		mac_release_rx_group(mcip, fgrp);
7101 		VERIFY3B(MAC_GROUP_NO_CLIENT(fgrp), ==, B_TRUE);
7102 		mac_set_group_state(fgrp, MAC_GROUP_STATE_REGISTERED);
7103 	} else {
7104 		mac_group_remove_client(fgrp, mcip);
7105 		mac_group_add_client(tgrp, mcip);
7106 		mcip->mci_flent->fe_rx_ring_group = tgrp;
7107 
7108 		/*
7109 		 * If there are other clients (VLANs) sharing this address
7110 		 * then move them too.
7111 		 */
7112 		if (mac_check_macaddr_shared(mcip->mci_unicast)) {
7113 			/*
7114 			 * We need to move all the clients that are using
7115 			 * this MAC address.
7116 			 */
7117 			mgcp = fgrp->mrg_clients;
7118 			while (mgcp != NULL) {
7119 				gmcip = mgcp->mgc_client;
7120 				mgcp = mgcp->mgc_next;
7121 				if (mcip->mci_unicast == gmcip->mci_unicast) {
7122 					mac_group_remove_client(fgrp, gmcip);
7123 					mac_group_add_client(tgrp, gmcip);
7124 					gmcip->mci_flent->fe_rx_ring_group =
7125 					    tgrp;
7126 				}
7127 			}
7128 		}
7129 
7130 		/*
7131 		 * The default group still handles multicast and
7132 		 * broadcast traffic; it won't transition to
7133 		 * MAC_GROUP_STATE_REGISTERED.
7134 		 */
7135 		if (fgrp->mrg_state == MAC_GROUP_STATE_RESERVED)
7136 			mac_rx_group_unmark(fgrp, MR_CONDEMNED);
7137 		mac_set_group_state(fgrp, MAC_GROUP_STATE_SHARED);
7138 	}
7139 
7140 	next_state = mac_group_next_state(tgrp, &group_only_mcip,
7141 	    MAC_DEFAULT_RX_GROUP(mip), B_TRUE);
7142 	mac_set_group_state(tgrp, next_state);
7143 
7144 	/*
7145 	 * If the destination group is reserved, then setup the SRSes.
7146 	 * Otherwise make sure to use SW classification.
7147 	 */
7148 	if (tgrp->mrg_state == MAC_GROUP_STATE_RESERVED) {
7149 		mac_rx_srs_group_setup(mcip, mcip->mci_flent, SRST_LINK);
7150 		mac_fanout_setup(mcip, mcip->mci_flent,
7151 		    MCIP_RESOURCE_PROPS(mcip), mac_rx_deliver, mcip, NULL,
7152 		    NULL);
7153 		mac_rx_group_unmark(tgrp, MR_INCIPIENT);
7154 	} else {
7155 		mac_rx_switch_grp_to_sw(tgrp);
7156 	}
7157 
7158 	return (0);
7159 }
7160 
7161 /*
7162  * Reserves a TX group for the specified share. Invoked by mac_tx_srs_setup()
7163  * when a share was allocated to the client.
7164  */
7165 mac_group_t *
7166 mac_reserve_tx_group(mac_client_impl_t *mcip, boolean_t move)
7167 {
7168 	mac_impl_t		*mip = mcip->mci_mip;
7169 	mac_group_t		*grp = NULL;
7170 	int			rv;
7171 	int			i;
7172 	int			err;
7173 	mac_group_t		*defgrp;
7174 	mac_share_handle_t	share = mcip->mci_share;
7175 	mac_resource_props_t	*mrp = MCIP_RESOURCE_PROPS(mcip);
7176 	int			nrings;
7177 	int			defnrings;
7178 	boolean_t		need_exclgrp = B_FALSE;
7179 	int			need_rings = 0;
7180 	mac_group_t		*candidate_grp = NULL;
7181 	mac_client_impl_t	*gclient;
7182 	mac_resource_props_t	*gmrp;
7183 	boolean_t		txhw = mrp->mrp_mask & MRP_TX_RINGS;
7184 	boolean_t		unspec = mrp->mrp_mask & MRP_TXRINGS_UNSPEC;
7185 	boolean_t		isprimary;
7186 
7187 	isprimary = mcip->mci_flent->fe_type & FLOW_PRIMARY_MAC;
7188 
7189 	/*
7190 	 * When we come here for a VLAN on the primary (dladm create-vlan),
7191 	 * we need to pair it along with the primary (to keep it consistent
7192 	 * with the RX side). So, we check if the primary is already assigned
7193 	 * to a group and return the group if so. The other way is also
7194 	 * true, i.e. the VLAN is already created and now we are plumbing
7195 	 * the primary.
7196 	 */
7197 	if (!move && isprimary) {
7198 		for (gclient = mip->mi_clients_list; gclient != NULL;
7199 		    gclient = gclient->mci_client_next) {
7200 			if (gclient->mci_flent->fe_type & FLOW_PRIMARY_MAC &&
7201 			    gclient->mci_flent->fe_tx_ring_group != NULL) {
7202 				return (gclient->mci_flent->fe_tx_ring_group);
7203 			}
7204 		}
7205 	}
7206 
7207 	if (mip->mi_tx_groups == NULL || mip->mi_tx_group_count == 0)
7208 		return (NULL);
7209 
7210 	/* For dynamic groups, default unspec to 1 */
7211 	if (txhw && unspec &&
7212 	    mip->mi_tx_group_type == MAC_GROUP_TYPE_DYNAMIC) {
7213 		mrp->mrp_ntxrings = 1;
7214 	}
7215 	/*
7216 	 * For static grouping we allow only specifying rings=0 and
7217 	 * unspecified
7218 	 */
7219 	if (txhw && mrp->mrp_ntxrings > 0 &&
7220 	    mip->mi_tx_group_type == MAC_GROUP_TYPE_STATIC) {
7221 		return (NULL);
7222 	}
7223 
7224 	if (txhw) {
7225 		/*
7226 		 * We have explicitly asked for a group (with ntxrings,
7227 		 * if unspec).
7228 		 */
7229 		if (unspec || mrp->mrp_ntxrings > 0) {
7230 			need_exclgrp = B_TRUE;
7231 			need_rings = mrp->mrp_ntxrings;
7232 		} else if (mrp->mrp_ntxrings == 0) {
7233 			/*
7234 			 * We have asked for a software group.
7235 			 */
7236 			return (NULL);
7237 		}
7238 	}
7239 	defgrp = MAC_DEFAULT_TX_GROUP(mip);
7240 	/*
7241 	 * The number of rings that the default group can donate.
7242 	 * We need to leave at least one ring - the default ring - in
7243 	 * this group.
7244 	 */
7245 	defnrings = defgrp->mrg_cur_count - 1;
7246 
7247 	/*
7248 	 * Primary gets default group unless explicitly told not
7249 	 * to  (i.e. rings > 0).
7250 	 */
7251 	if (isprimary && !need_exclgrp)
7252 		return (NULL);
7253 
7254 	nrings = (mrp->mrp_mask & MRP_TX_RINGS) != 0 ? mrp->mrp_ntxrings : 1;
7255 	for (i = 0; i <  mip->mi_tx_group_count; i++) {
7256 		grp = &mip->mi_tx_groups[i];
7257 		if ((grp->mrg_state == MAC_GROUP_STATE_RESERVED) ||
7258 		    (grp->mrg_state == MAC_GROUP_STATE_UNINIT)) {
7259 			/*
7260 			 * Select a candidate for replacement if we don't
7261 			 * get an exclusive group. A candidate group is one
7262 			 * that didn't ask for an exclusive group, but got
7263 			 * one and it has enough rings (combined with what
7264 			 * the default group can donate) for the new MAC
7265 			 * client.
7266 			 */
7267 			if (grp->mrg_state == MAC_GROUP_STATE_RESERVED &&
7268 			    candidate_grp == NULL) {
7269 				gclient = MAC_GROUP_ONLY_CLIENT(grp);
7270 				VERIFY3P(gclient, !=, NULL);
7271 				gmrp = MCIP_RESOURCE_PROPS(gclient);
7272 				if (gclient->mci_share == 0 &&
7273 				    (gmrp->mrp_mask & MRP_TX_RINGS) == 0 &&
7274 				    (unspec ||
7275 				    (grp->mrg_cur_count + defnrings) >=
7276 				    need_rings)) {
7277 					candidate_grp = grp;
7278 				}
7279 			}
7280 			continue;
7281 		}
7282 		/*
7283 		 * If the default can't donate let's just walk and
7284 		 * see if someone can vacate a group, so that we have
7285 		 * enough rings for this.
7286 		 */
7287 		if (mip->mi_tx_group_type != MAC_GROUP_TYPE_DYNAMIC ||
7288 		    nrings <= defnrings) {
7289 			if (grp->mrg_state == MAC_GROUP_STATE_REGISTERED) {
7290 				rv = mac_start_group(grp);
7291 				ASSERT(rv == 0);
7292 			}
7293 			break;
7294 		}
7295 	}
7296 
7297 	/* The default group */
7298 	if (i >= mip->mi_tx_group_count) {
7299 		/*
7300 		 * If we need an exclusive group and have identified a
7301 		 * candidate group we switch the MAC client from the
7302 		 * candidate group to the default group and give the
7303 		 * candidate group to this client.
7304 		 */
7305 		if (need_exclgrp && candidate_grp != NULL) {
7306 			/*
7307 			 * Switch the MAC client from the candidate
7308 			 * group to the default group. We know the
7309 			 * candidate_grp came from a reserved group
7310 			 * and thus only has one client.
7311 			 */
7312 			grp = candidate_grp;
7313 			gclient = MAC_GROUP_ONLY_CLIENT(grp);
7314 			VERIFY3P(gclient, !=, NULL);
7315 			mac_tx_client_quiesce((mac_client_handle_t)gclient);
7316 			mac_tx_switch_group(gclient, grp, defgrp);
7317 			mac_tx_client_restart((mac_client_handle_t)gclient);
7318 
7319 			/*
7320 			 * Give the candidate group with the specified number
7321 			 * of rings to this MAC client.
7322 			 */
7323 			ASSERT(grp->mrg_state == MAC_GROUP_STATE_REGISTERED);
7324 			rv = mac_start_group(grp);
7325 			ASSERT(rv == 0);
7326 
7327 			if (mip->mi_tx_group_type != MAC_GROUP_TYPE_DYNAMIC)
7328 				return (grp);
7329 
7330 			ASSERT(grp->mrg_cur_count == 0);
7331 			ASSERT(defgrp->mrg_cur_count > need_rings);
7332 
7333 			err = i_mac_group_allocate_rings(mip, MAC_RING_TYPE_TX,
7334 			    defgrp, grp, share, need_rings);
7335 			if (err == 0) {
7336 				/*
7337 				 * For a share i_mac_group_allocate_rings gets
7338 				 * the rings from the driver, let's populate
7339 				 * the property for the client now.
7340 				 */
7341 				if (share != 0) {
7342 					mac_client_set_rings(
7343 					    (mac_client_handle_t)mcip, -1,
7344 					    grp->mrg_cur_count);
7345 				}
7346 				mip->mi_tx_group_free--;
7347 				return (grp);
7348 			}
7349 			DTRACE_PROBE3(tx__group__reserve__alloc__rings, char *,
7350 			    mip->mi_name, int, grp->mrg_index, int, err);
7351 			mac_stop_group(grp);
7352 		}
7353 		return (NULL);
7354 	}
7355 	/*
7356 	 * We got an exclusive group, but it is not dynamic.
7357 	 */
7358 	if (mip->mi_tx_group_type != MAC_GROUP_TYPE_DYNAMIC) {
7359 		mip->mi_tx_group_free--;
7360 		return (grp);
7361 	}
7362 
7363 	rv = i_mac_group_allocate_rings(mip, MAC_RING_TYPE_TX, defgrp, grp,
7364 	    share, nrings);
7365 	if (rv != 0) {
7366 		DTRACE_PROBE3(tx__group__reserve__alloc__rings,
7367 		    char *, mip->mi_name, int, grp->mrg_index, int, rv);
7368 		mac_stop_group(grp);
7369 		return (NULL);
7370 	}
7371 	/*
7372 	 * For a share i_mac_group_allocate_rings gets the rings from the
7373 	 * driver, let's populate the property for the client now.
7374 	 */
7375 	if (share != 0) {
7376 		mac_client_set_rings((mac_client_handle_t)mcip, -1,
7377 		    grp->mrg_cur_count);
7378 	}
7379 	mip->mi_tx_group_free--;
7380 	return (grp);
7381 }
7382 
7383 void
7384 mac_release_tx_group(mac_client_impl_t *mcip, mac_group_t *grp)
7385 {
7386 	mac_impl_t		*mip = mcip->mci_mip;
7387 	mac_share_handle_t	share = mcip->mci_share;
7388 	mac_ring_t		*ring;
7389 	mac_soft_ring_set_t	*srs = MCIP_TX_SRS(mcip);
7390 	mac_group_t		*defgrp;
7391 
7392 	defgrp = MAC_DEFAULT_TX_GROUP(mip);
7393 	if (srs != NULL) {
7394 		if (srs->srs_soft_ring_count > 0) {
7395 			for (ring = grp->mrg_rings; ring != NULL;
7396 			    ring = ring->mr_next) {
7397 				ASSERT(mac_tx_srs_ring_present(srs, ring));
7398 				mac_tx_invoke_callbacks(mcip,
7399 				    (mac_tx_cookie_t)
7400 				    mac_tx_srs_get_soft_ring(srs, ring));
7401 				mac_tx_srs_del_ring(srs, ring);
7402 			}
7403 		} else {
7404 			ASSERT(srs->srs_tx.st_arg2 != NULL);
7405 			srs->srs_tx.st_arg2 = NULL;
7406 			mac_srs_stat_delete(srs);
7407 		}
7408 	}
7409 	if (share != 0)
7410 		mip->mi_share_capab.ms_sremove(share, grp->mrg_driver);
7411 
7412 	/* move the ring back to the pool */
7413 	if (mip->mi_tx_group_type == MAC_GROUP_TYPE_DYNAMIC) {
7414 		while ((ring = grp->mrg_rings) != NULL)
7415 			(void) mac_group_mov_ring(mip, defgrp, ring);
7416 	}
7417 	mac_stop_group(grp);
7418 	mip->mi_tx_group_free++;
7419 }
7420 
7421 /*
7422  * Disassociate a MAC client from a group, i.e go through the rings in the
7423  * group and delete all the soft rings tied to them.
7424  */
7425 static void
7426 mac_tx_dismantle_soft_rings(mac_group_t *fgrp, flow_entry_t *flent)
7427 {
7428 	mac_client_impl_t	*mcip = flent->fe_mcip;
7429 	mac_soft_ring_set_t	*tx_srs;
7430 	mac_srs_tx_t		*tx;
7431 	mac_ring_t		*ring;
7432 
7433 	tx_srs = flent->fe_tx_srs;
7434 	tx = &tx_srs->srs_tx;
7435 
7436 	/* Single ring case we haven't created any soft rings */
7437 	if (tx->st_mode == SRS_TX_BW || tx->st_mode == SRS_TX_SERIALIZE ||
7438 	    tx->st_mode == SRS_TX_DEFAULT) {
7439 		tx->st_arg2 = NULL;
7440 		mac_srs_stat_delete(tx_srs);
7441 	/* Fanout case, where we have to dismantle the soft rings */
7442 	} else {
7443 		for (ring = fgrp->mrg_rings; ring != NULL;
7444 		    ring = ring->mr_next) {
7445 			ASSERT(mac_tx_srs_ring_present(tx_srs, ring));
7446 			mac_tx_invoke_callbacks(mcip,
7447 			    (mac_tx_cookie_t)mac_tx_srs_get_soft_ring(tx_srs,
7448 			    ring));
7449 			mac_tx_srs_del_ring(tx_srs, ring);
7450 		}
7451 		ASSERT(tx->st_arg2 == NULL);
7452 	}
7453 }
7454 
7455 /*
7456  * Switch the MAC client from one group to another. This means we need
7457  * to remove the MAC client, teardown the SRSs and revert the group state.
7458  * Then, we add the client to the destination roup, set the SRSs etc.
7459  */
7460 void
7461 mac_tx_switch_group(mac_client_impl_t *mcip, mac_group_t *fgrp,
7462     mac_group_t *tgrp)
7463 {
7464 	mac_client_impl_t	*group_only_mcip;
7465 	mac_impl_t		*mip = mcip->mci_mip;
7466 	flow_entry_t		*flent = mcip->mci_flent;
7467 	mac_group_t		*defgrp;
7468 	mac_grp_client_t	*mgcp;
7469 	mac_client_impl_t	*gmcip;
7470 	flow_entry_t		*gflent;
7471 
7472 	defgrp = MAC_DEFAULT_TX_GROUP(mip);
7473 	ASSERT(fgrp == flent->fe_tx_ring_group);
7474 
7475 	if (fgrp == defgrp) {
7476 		/*
7477 		 * If this is the primary we need to find any VLANs on
7478 		 * the primary and move them too.
7479 		 */
7480 		mac_group_remove_client(fgrp, mcip);
7481 		mac_tx_dismantle_soft_rings(fgrp, flent);
7482 		if (mac_check_macaddr_shared(mcip->mci_unicast)) {
7483 			mgcp = fgrp->mrg_clients;
7484 			while (mgcp != NULL) {
7485 				gmcip = mgcp->mgc_client;
7486 				mgcp = mgcp->mgc_next;
7487 				if (mcip->mci_unicast != gmcip->mci_unicast)
7488 					continue;
7489 				mac_tx_client_quiesce(
7490 				    (mac_client_handle_t)gmcip);
7491 
7492 				gflent = gmcip->mci_flent;
7493 				mac_group_remove_client(fgrp, gmcip);
7494 				mac_tx_dismantle_soft_rings(fgrp, gflent);
7495 
7496 				mac_group_add_client(tgrp, gmcip);
7497 				gflent->fe_tx_ring_group = tgrp;
7498 				/* We could directly set this to SHARED */
7499 				tgrp->mrg_state = mac_group_next_state(tgrp,
7500 				    &group_only_mcip, defgrp, B_FALSE);
7501 
7502 				mac_tx_srs_group_setup(gmcip, gflent,
7503 				    SRST_LINK);
7504 				mac_fanout_setup(gmcip, gflent,
7505 				    MCIP_RESOURCE_PROPS(gmcip), mac_rx_deliver,
7506 				    gmcip, NULL, NULL);
7507 
7508 				mac_tx_client_restart(
7509 				    (mac_client_handle_t)gmcip);
7510 			}
7511 		}
7512 		if (MAC_GROUP_NO_CLIENT(fgrp)) {
7513 			mac_ring_t	*ring;
7514 			int		cnt;
7515 			int		ringcnt;
7516 
7517 			fgrp->mrg_state = MAC_GROUP_STATE_REGISTERED;
7518 			/*
7519 			 * Additionally, we also need to stop all
7520 			 * the rings in the default group, except
7521 			 * the default ring. The reason being
7522 			 * this group won't be released since it is
7523 			 * the default group, so the rings won't
7524 			 * be stopped otherwise.
7525 			 */
7526 			ringcnt = fgrp->mrg_cur_count;
7527 			ring = fgrp->mrg_rings;
7528 			for (cnt = 0; cnt < ringcnt; cnt++) {
7529 				if (ring->mr_state == MR_INUSE &&
7530 				    ring !=
7531 				    (mac_ring_t *)mip->mi_default_tx_ring) {
7532 					mac_stop_ring(ring);
7533 					ring->mr_flag = 0;
7534 				}
7535 				ring = ring->mr_next;
7536 			}
7537 		} else if (MAC_GROUP_ONLY_CLIENT(fgrp) != NULL) {
7538 			fgrp->mrg_state = MAC_GROUP_STATE_RESERVED;
7539 		} else {
7540 			ASSERT(fgrp->mrg_state == MAC_GROUP_STATE_SHARED);
7541 		}
7542 	} else {
7543 		/*
7544 		 * We could have VLANs sharing the non-default group with
7545 		 * the primary.
7546 		 */
7547 		mgcp = fgrp->mrg_clients;
7548 		while (mgcp != NULL) {
7549 			gmcip = mgcp->mgc_client;
7550 			mgcp = mgcp->mgc_next;
7551 			if (gmcip == mcip)
7552 				continue;
7553 			mac_tx_client_quiesce((mac_client_handle_t)gmcip);
7554 			gflent = gmcip->mci_flent;
7555 
7556 			mac_group_remove_client(fgrp, gmcip);
7557 			mac_tx_dismantle_soft_rings(fgrp, gflent);
7558 
7559 			mac_group_add_client(tgrp, gmcip);
7560 			gflent->fe_tx_ring_group = tgrp;
7561 			/* We could directly set this to SHARED */
7562 			tgrp->mrg_state = mac_group_next_state(tgrp,
7563 			    &group_only_mcip, defgrp, B_FALSE);
7564 			mac_tx_srs_group_setup(gmcip, gflent, SRST_LINK);
7565 			mac_fanout_setup(gmcip, gflent,
7566 			    MCIP_RESOURCE_PROPS(gmcip), mac_rx_deliver,
7567 			    gmcip, NULL, NULL);
7568 
7569 			mac_tx_client_restart((mac_client_handle_t)gmcip);
7570 		}
7571 		mac_group_remove_client(fgrp, mcip);
7572 		mac_release_tx_group(mcip, fgrp);
7573 		fgrp->mrg_state = MAC_GROUP_STATE_REGISTERED;
7574 	}
7575 
7576 	/* Add it to the tgroup */
7577 	mac_group_add_client(tgrp, mcip);
7578 	flent->fe_tx_ring_group = tgrp;
7579 	tgrp->mrg_state = mac_group_next_state(tgrp, &group_only_mcip,
7580 	    defgrp, B_FALSE);
7581 
7582 	mac_tx_srs_group_setup(mcip, flent, SRST_LINK);
7583 	mac_fanout_setup(mcip, flent, MCIP_RESOURCE_PROPS(mcip),
7584 	    mac_rx_deliver, mcip, NULL, NULL);
7585 }
7586 
7587 /*
7588  * This is a 1-time control path activity initiated by the client (IP).
7589  * The mac perimeter protects against other simultaneous control activities,
7590  * for example an ioctl that attempts to change the degree of fanout and
7591  * increase or decrease the number of softrings associated with this Tx SRS.
7592  */
7593 static mac_tx_notify_cb_t *
7594 mac_client_tx_notify_add(mac_client_impl_t *mcip,
7595     mac_tx_notify_t notify, void *arg)
7596 {
7597 	mac_cb_info_t *mcbi;
7598 	mac_tx_notify_cb_t *mtnfp;
7599 
7600 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
7601 
7602 	mtnfp = kmem_zalloc(sizeof (mac_tx_notify_cb_t), KM_SLEEP);
7603 	mtnfp->mtnf_fn = notify;
7604 	mtnfp->mtnf_arg = arg;
7605 	mtnfp->mtnf_link.mcb_objp = mtnfp;
7606 	mtnfp->mtnf_link.mcb_objsize = sizeof (mac_tx_notify_cb_t);
7607 	mtnfp->mtnf_link.mcb_flags = MCB_TX_NOTIFY_CB_T;
7608 
7609 	mcbi = &mcip->mci_tx_notify_cb_info;
7610 	mutex_enter(mcbi->mcbi_lockp);
7611 	mac_callback_add(mcbi, &mcip->mci_tx_notify_cb_list, &mtnfp->mtnf_link);
7612 	mutex_exit(mcbi->mcbi_lockp);
7613 	return (mtnfp);
7614 }
7615 
7616 static void
7617 mac_client_tx_notify_remove(mac_client_impl_t *mcip, mac_tx_notify_cb_t *mtnfp)
7618 {
7619 	mac_cb_info_t	*mcbi;
7620 	mac_cb_t	**cblist;
7621 
7622 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
7623 
7624 	if (!mac_callback_find(&mcip->mci_tx_notify_cb_info,
7625 	    &mcip->mci_tx_notify_cb_list, &mtnfp->mtnf_link)) {
7626 		cmn_err(CE_WARN,
7627 		    "mac_client_tx_notify_remove: callback not "
7628 		    "found, mcip 0x%p mtnfp 0x%p", (void *)mcip, (void *)mtnfp);
7629 		return;
7630 	}
7631 
7632 	mcbi = &mcip->mci_tx_notify_cb_info;
7633 	cblist = &mcip->mci_tx_notify_cb_list;
7634 	mutex_enter(mcbi->mcbi_lockp);
7635 	if (mac_callback_remove(mcbi, cblist, &mtnfp->mtnf_link))
7636 		kmem_free(mtnfp, sizeof (mac_tx_notify_cb_t));
7637 	else
7638 		mac_callback_remove_wait(&mcip->mci_tx_notify_cb_info);
7639 	mutex_exit(mcbi->mcbi_lockp);
7640 }
7641 
7642 /*
7643  * mac_client_tx_notify():
7644  * call to add and remove flow control callback routine.
7645  */
7646 mac_tx_notify_handle_t
7647 mac_client_tx_notify(mac_client_handle_t mch, mac_tx_notify_t callb_func,
7648     void *ptr)
7649 {
7650 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
7651 	mac_tx_notify_cb_t	*mtnfp = NULL;
7652 
7653 	i_mac_perim_enter(mcip->mci_mip);
7654 
7655 	if (callb_func != NULL) {
7656 		/* Add a notify callback */
7657 		mtnfp = mac_client_tx_notify_add(mcip, callb_func, ptr);
7658 	} else {
7659 		mac_client_tx_notify_remove(mcip, (mac_tx_notify_cb_t *)ptr);
7660 	}
7661 	i_mac_perim_exit(mcip->mci_mip);
7662 
7663 	return ((mac_tx_notify_handle_t)mtnfp);
7664 }
7665 
7666 void
7667 mac_bridge_vectors(mac_bridge_tx_t txf, mac_bridge_rx_t rxf,
7668     mac_bridge_ref_t reff, mac_bridge_ls_t lsf)
7669 {
7670 	mac_bridge_tx_cb = txf;
7671 	mac_bridge_rx_cb = rxf;
7672 	mac_bridge_ref_cb = reff;
7673 	mac_bridge_ls_cb = lsf;
7674 }
7675 
7676 int
7677 mac_bridge_set(mac_handle_t mh, mac_handle_t link)
7678 {
7679 	mac_impl_t *mip = (mac_impl_t *)mh;
7680 	int retv;
7681 
7682 	mutex_enter(&mip->mi_bridge_lock);
7683 	if (mip->mi_bridge_link == NULL) {
7684 		mip->mi_bridge_link = link;
7685 		retv = 0;
7686 	} else {
7687 		retv = EBUSY;
7688 	}
7689 	mutex_exit(&mip->mi_bridge_lock);
7690 	if (retv == 0) {
7691 		mac_poll_state_change(mh, B_FALSE);
7692 		mac_capab_update(mh);
7693 	}
7694 	return (retv);
7695 }
7696 
7697 /*
7698  * Disable bridging on the indicated link.
7699  */
7700 void
7701 mac_bridge_clear(mac_handle_t mh, mac_handle_t link)
7702 {
7703 	mac_impl_t *mip = (mac_impl_t *)mh;
7704 
7705 	mutex_enter(&mip->mi_bridge_lock);
7706 	ASSERT(mip->mi_bridge_link == link);
7707 	mip->mi_bridge_link = NULL;
7708 	mutex_exit(&mip->mi_bridge_lock);
7709 	mac_poll_state_change(mh, B_TRUE);
7710 	mac_capab_update(mh);
7711 }
7712 
7713 void
7714 mac_no_active(mac_handle_t mh)
7715 {
7716 	mac_impl_t *mip = (mac_impl_t *)mh;
7717 
7718 	i_mac_perim_enter(mip);
7719 	mip->mi_state_flags |= MIS_NO_ACTIVE;
7720 	i_mac_perim_exit(mip);
7721 }
7722 
7723 /*
7724  * Walk the primary VLAN clients whenever the primary's rings property
7725  * changes and update the mac_resource_props_t for the VLAN's client.
7726  * We need to do this since we don't support setting these properties
7727  * on the primary's VLAN clients, but the VLAN clients have to
7728  * follow the primary w.r.t the rings property.
7729  */
7730 void
7731 mac_set_prim_vlan_rings(mac_impl_t  *mip, mac_resource_props_t *mrp)
7732 {
7733 	mac_client_impl_t	*vmcip;
7734 	mac_resource_props_t	*vmrp;
7735 
7736 	for (vmcip = mip->mi_clients_list; vmcip != NULL;
7737 	    vmcip = vmcip->mci_client_next) {
7738 		if (!(vmcip->mci_flent->fe_type & FLOW_PRIMARY_MAC) ||
7739 		    mac_client_vid((mac_client_handle_t)vmcip) ==
7740 		    VLAN_ID_NONE) {
7741 			continue;
7742 		}
7743 		vmrp = MCIP_RESOURCE_PROPS(vmcip);
7744 
7745 		vmrp->mrp_nrxrings =  mrp->mrp_nrxrings;
7746 		if (mrp->mrp_mask & MRP_RX_RINGS)
7747 			vmrp->mrp_mask |= MRP_RX_RINGS;
7748 		else if (vmrp->mrp_mask & MRP_RX_RINGS)
7749 			vmrp->mrp_mask &= ~MRP_RX_RINGS;
7750 
7751 		vmrp->mrp_ntxrings =  mrp->mrp_ntxrings;
7752 		if (mrp->mrp_mask & MRP_TX_RINGS)
7753 			vmrp->mrp_mask |= MRP_TX_RINGS;
7754 		else if (vmrp->mrp_mask & MRP_TX_RINGS)
7755 			vmrp->mrp_mask &= ~MRP_TX_RINGS;
7756 
7757 		if (mrp->mrp_mask & MRP_RXRINGS_UNSPEC)
7758 			vmrp->mrp_mask |= MRP_RXRINGS_UNSPEC;
7759 		else
7760 			vmrp->mrp_mask &= ~MRP_RXRINGS_UNSPEC;
7761 
7762 		if (mrp->mrp_mask & MRP_TXRINGS_UNSPEC)
7763 			vmrp->mrp_mask |= MRP_TXRINGS_UNSPEC;
7764 		else
7765 			vmrp->mrp_mask &= ~MRP_TXRINGS_UNSPEC;
7766 	}
7767 }
7768 
7769 /*
7770  * We are adding or removing ring(s) from a group. The source for taking
7771  * rings is the default group. The destination for giving rings back is
7772  * the default group.
7773  */
7774 int
7775 mac_group_ring_modify(mac_client_impl_t *mcip, mac_group_t *group,
7776     mac_group_t *defgrp)
7777 {
7778 	mac_resource_props_t	*mrp = MCIP_RESOURCE_PROPS(mcip);
7779 	uint_t			modify;
7780 	int			count;
7781 	mac_ring_t		*ring;
7782 	mac_ring_t		*next;
7783 	mac_impl_t		*mip = mcip->mci_mip;
7784 	mac_ring_t		**rings;
7785 	uint_t			ringcnt;
7786 	int			i = 0;
7787 	boolean_t		rx_group = group->mrg_type == MAC_RING_TYPE_RX;
7788 	int			start;
7789 	int			end;
7790 	mac_group_t		*tgrp;
7791 	int			j;
7792 	int			rv = 0;
7793 
7794 	/*
7795 	 * If we are asked for just a group, we give 1 ring, else
7796 	 * the specified number of rings.
7797 	 */
7798 	if (rx_group) {
7799 		ringcnt = (mrp->mrp_mask & MRP_RXRINGS_UNSPEC) ? 1:
7800 		    mrp->mrp_nrxrings;
7801 	} else {
7802 		ringcnt = (mrp->mrp_mask & MRP_TXRINGS_UNSPEC) ? 1:
7803 		    mrp->mrp_ntxrings;
7804 	}
7805 
7806 	/* don't allow modifying rings for a share for now. */
7807 	ASSERT(mcip->mci_share == 0);
7808 
7809 	if (ringcnt == group->mrg_cur_count)
7810 		return (0);
7811 
7812 	if (group->mrg_cur_count > ringcnt) {
7813 		modify = group->mrg_cur_count - ringcnt;
7814 		if (rx_group) {
7815 			if (mip->mi_rx_donor_grp == group) {
7816 				ASSERT(mac_is_primary_client(mcip));
7817 				mip->mi_rx_donor_grp = defgrp;
7818 			} else {
7819 				defgrp = mip->mi_rx_donor_grp;
7820 			}
7821 		}
7822 		ring = group->mrg_rings;
7823 		rings = kmem_alloc(modify * sizeof (mac_ring_handle_t),
7824 		    KM_SLEEP);
7825 		j = 0;
7826 		for (count = 0; count < modify; count++) {
7827 			next = ring->mr_next;
7828 			rv = mac_group_mov_ring(mip, defgrp, ring);
7829 			if (rv != 0) {
7830 				/* cleanup on failure */
7831 				for (j = 0; j < count; j++) {
7832 					(void) mac_group_mov_ring(mip, group,
7833 					    rings[j]);
7834 				}
7835 				break;
7836 			}
7837 			rings[j++] = ring;
7838 			ring = next;
7839 		}
7840 		kmem_free(rings, modify * sizeof (mac_ring_handle_t));
7841 		return (rv);
7842 	}
7843 	if (ringcnt >= MAX_RINGS_PER_GROUP)
7844 		return (EINVAL);
7845 
7846 	modify = ringcnt - group->mrg_cur_count;
7847 
7848 	if (rx_group) {
7849 		if (group != mip->mi_rx_donor_grp)
7850 			defgrp = mip->mi_rx_donor_grp;
7851 		else
7852 			/*
7853 			 * This is the donor group with all the remaining
7854 			 * rings. Default group now gets to be the donor
7855 			 */
7856 			mip->mi_rx_donor_grp = defgrp;
7857 		start = 1;
7858 		end = mip->mi_rx_group_count;
7859 	} else {
7860 		start = 0;
7861 		end = mip->mi_tx_group_count - 1;
7862 	}
7863 	/*
7864 	 * If the default doesn't have any rings, lets see if we can
7865 	 * take rings given to an h/w client that doesn't need it.
7866 	 * For now, we just see if there is  any one client that can donate
7867 	 * all the required rings.
7868 	 */
7869 	if (defgrp->mrg_cur_count < (modify + 1)) {
7870 		for (i = start; i < end; i++) {
7871 			if (rx_group) {
7872 				tgrp = &mip->mi_rx_groups[i];
7873 				if (tgrp == group || tgrp->mrg_state <
7874 				    MAC_GROUP_STATE_RESERVED) {
7875 					continue;
7876 				}
7877 				if (i_mac_clients_hw(tgrp, MRP_RX_RINGS))
7878 					continue;
7879 				mcip = tgrp->mrg_clients->mgc_client;
7880 				VERIFY3P(mcip, !=, NULL);
7881 				if ((tgrp->mrg_cur_count +
7882 				    defgrp->mrg_cur_count) < (modify + 1)) {
7883 					continue;
7884 				}
7885 				if (mac_rx_switch_group(mcip, tgrp,
7886 				    defgrp) != 0) {
7887 					return (ENOSPC);
7888 				}
7889 			} else {
7890 				tgrp = &mip->mi_tx_groups[i];
7891 				if (tgrp == group || tgrp->mrg_state <
7892 				    MAC_GROUP_STATE_RESERVED) {
7893 					continue;
7894 				}
7895 				if (i_mac_clients_hw(tgrp, MRP_TX_RINGS))
7896 					continue;
7897 				mcip = tgrp->mrg_clients->mgc_client;
7898 				VERIFY3P(mcip, !=, NULL);
7899 				if ((tgrp->mrg_cur_count +
7900 				    defgrp->mrg_cur_count) < (modify + 1)) {
7901 					continue;
7902 				}
7903 				/* OK, we can switch this to s/w */
7904 				mac_tx_client_quiesce(
7905 				    (mac_client_handle_t)mcip);
7906 				mac_tx_switch_group(mcip, tgrp, defgrp);
7907 				mac_tx_client_restart(
7908 				    (mac_client_handle_t)mcip);
7909 			}
7910 		}
7911 		if (defgrp->mrg_cur_count < (modify + 1))
7912 			return (ENOSPC);
7913 	}
7914 	if ((rv = i_mac_group_allocate_rings(mip, group->mrg_type, defgrp,
7915 	    group, mcip->mci_share, modify)) != 0) {
7916 		return (rv);
7917 	}
7918 	return (0);
7919 }
7920 
7921 /*
7922  * Given the poolname in mac_resource_props, find the cpupart
7923  * that is associated with this pool.  The cpupart will be used
7924  * later for finding the cpus to be bound to the networking threads.
7925  *
7926  * use_default is set B_TRUE if pools are enabled and pool_default
7927  * is returned.  This avoids a 2nd lookup to set the poolname
7928  * for pool-effective.
7929  *
7930  * returns:
7931  *
7932  *    NULL -   pools are disabled or if the 'cpus' property is set.
7933  *    cpupart of pool_default  - pools are enabled and the pool
7934  *             is not available or poolname is blank
7935  *    cpupart of named pool    - pools are enabled and the pool
7936  *             is available.
7937  */
7938 cpupart_t *
7939 mac_pset_find(mac_resource_props_t *mrp, boolean_t *use_default)
7940 {
7941 	pool_t		*pool;
7942 	cpupart_t	*cpupart;
7943 
7944 	*use_default = B_FALSE;
7945 
7946 	/* CPUs property is set */
7947 	if (mrp->mrp_mask & MRP_CPUS)
7948 		return (NULL);
7949 
7950 	ASSERT(pool_lock_held());
7951 
7952 	/* Pools are disabled, no pset */
7953 	if (pool_state == POOL_DISABLED)
7954 		return (NULL);
7955 
7956 	/* Pools property is set */
7957 	if (mrp->mrp_mask & MRP_POOL) {
7958 		if ((pool = pool_lookup_pool_by_name(mrp->mrp_pool)) == NULL) {
7959 			/* Pool not found */
7960 			DTRACE_PROBE1(mac_pset_find_no_pool, char *,
7961 			    mrp->mrp_pool);
7962 			*use_default = B_TRUE;
7963 			pool = pool_default;
7964 		}
7965 	/* Pools property is not set */
7966 	} else {
7967 		*use_default = B_TRUE;
7968 		pool = pool_default;
7969 	}
7970 
7971 	/* Find the CPU pset that corresponds to the pool */
7972 	mutex_enter(&cpu_lock);
7973 	if ((cpupart = cpupart_find(pool->pool_pset->pset_id)) == NULL) {
7974 		DTRACE_PROBE1(mac_find_pset_no_pset, psetid_t,
7975 		    pool->pool_pset->pset_id);
7976 	}
7977 	mutex_exit(&cpu_lock);
7978 
7979 	return (cpupart);
7980 }
7981 
7982 void
7983 mac_set_pool_effective(boolean_t use_default, cpupart_t *cpupart,
7984     mac_resource_props_t *mrp, mac_resource_props_t *emrp)
7985 {
7986 	ASSERT(pool_lock_held());
7987 
7988 	if (cpupart != NULL) {
7989 		emrp->mrp_mask |= MRP_POOL;
7990 		if (use_default) {
7991 			(void) strcpy(emrp->mrp_pool,
7992 			    "pool_default");
7993 		} else {
7994 			ASSERT(strlen(mrp->mrp_pool) != 0);
7995 			(void) strcpy(emrp->mrp_pool,
7996 			    mrp->mrp_pool);
7997 		}
7998 	} else {
7999 		emrp->mrp_mask &= ~MRP_POOL;
8000 		bzero(emrp->mrp_pool, MAXPATHLEN);
8001 	}
8002 }
8003 
8004 struct mac_pool_arg {
8005 	char		mpa_poolname[MAXPATHLEN];
8006 	pool_event_t	mpa_what;
8007 };
8008 
8009 /*ARGSUSED*/
8010 static uint_t
8011 mac_pool_link_update(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
8012 {
8013 	struct mac_pool_arg	*mpa = arg;
8014 	mac_impl_t		*mip = (mac_impl_t *)val;
8015 	mac_client_impl_t	*mcip;
8016 	mac_resource_props_t	*mrp, *emrp;
8017 	boolean_t		pool_update = B_FALSE;
8018 	boolean_t		pool_clear = B_FALSE;
8019 	boolean_t		use_default = B_FALSE;
8020 	cpupart_t		*cpupart = NULL;
8021 
8022 	mrp = kmem_zalloc(sizeof (*mrp), KM_SLEEP);
8023 	i_mac_perim_enter(mip);
8024 	for (mcip = mip->mi_clients_list; mcip != NULL;
8025 	    mcip = mcip->mci_client_next) {
8026 		pool_update = B_FALSE;
8027 		pool_clear = B_FALSE;
8028 		use_default = B_FALSE;
8029 		mac_client_get_resources((mac_client_handle_t)mcip, mrp);
8030 		emrp = MCIP_EFFECTIVE_PROPS(mcip);
8031 
8032 		/*
8033 		 * When pools are enabled
8034 		 */
8035 		if ((mpa->mpa_what == POOL_E_ENABLE) &&
8036 		    ((mrp->mrp_mask & MRP_CPUS) == 0)) {
8037 			mrp->mrp_mask |= MRP_POOL;
8038 			pool_update = B_TRUE;
8039 		}
8040 
8041 		/*
8042 		 * When pools are disabled
8043 		 */
8044 		if ((mpa->mpa_what == POOL_E_DISABLE) &&
8045 		    ((mrp->mrp_mask & MRP_CPUS) == 0)) {
8046 			mrp->mrp_mask |= MRP_POOL;
8047 			pool_clear = B_TRUE;
8048 		}
8049 
8050 		/*
8051 		 * Look for links with the pool property set and the poolname
8052 		 * matching the one which is changing.
8053 		 */
8054 		if (strcmp(mrp->mrp_pool, mpa->mpa_poolname) == 0) {
8055 			/*
8056 			 * The pool associated with the link has changed.
8057 			 */
8058 			if (mpa->mpa_what == POOL_E_CHANGE) {
8059 				mrp->mrp_mask |= MRP_POOL;
8060 				pool_update = B_TRUE;
8061 			}
8062 		}
8063 
8064 		/*
8065 		 * This link is associated with pool_default and
8066 		 * pool_default has changed.
8067 		 */
8068 		if ((mpa->mpa_what == POOL_E_CHANGE) &&
8069 		    (strcmp(emrp->mrp_pool, "pool_default") == 0) &&
8070 		    (strcmp(mpa->mpa_poolname, "pool_default") == 0)) {
8071 			mrp->mrp_mask |= MRP_POOL;
8072 			pool_update = B_TRUE;
8073 		}
8074 
8075 		/*
8076 		 * Get new list of cpus for the pool, bind network
8077 		 * threads to new list of cpus and update resources.
8078 		 */
8079 		if (pool_update) {
8080 			if (MCIP_DATAPATH_SETUP(mcip)) {
8081 				pool_lock();
8082 				cpupart = mac_pset_find(mrp, &use_default);
8083 				mac_fanout_setup(mcip, mcip->mci_flent, mrp,
8084 				    mac_rx_deliver, mcip, NULL, cpupart);
8085 				mac_set_pool_effective(use_default, cpupart,
8086 				    mrp, emrp);
8087 				pool_unlock();
8088 			}
8089 			mac_update_resources(mrp, MCIP_RESOURCE_PROPS(mcip),
8090 			    B_FALSE);
8091 		}
8092 
8093 		/*
8094 		 * Clear the effective pool and bind network threads
8095 		 * to any available CPU.
8096 		 */
8097 		if (pool_clear) {
8098 			if (MCIP_DATAPATH_SETUP(mcip)) {
8099 				emrp->mrp_mask &= ~MRP_POOL;
8100 				bzero(emrp->mrp_pool, MAXPATHLEN);
8101 				mac_fanout_setup(mcip, mcip->mci_flent, mrp,
8102 				    mac_rx_deliver, mcip, NULL, NULL);
8103 			}
8104 			mac_update_resources(mrp, MCIP_RESOURCE_PROPS(mcip),
8105 			    B_FALSE);
8106 		}
8107 	}
8108 	i_mac_perim_exit(mip);
8109 	kmem_free(mrp, sizeof (*mrp));
8110 	return (MH_WALK_CONTINUE);
8111 }
8112 
8113 static void
8114 mac_pool_update(void *arg)
8115 {
8116 	mod_hash_walk(i_mac_impl_hash, mac_pool_link_update, arg);
8117 	kmem_free(arg, sizeof (struct mac_pool_arg));
8118 }
8119 
8120 /*
8121  * Callback function to be executed when a noteworthy pool event
8122  * takes place.
8123  */
8124 /* ARGSUSED */
8125 static void
8126 mac_pool_event_cb(pool_event_t what, poolid_t id, void *arg)
8127 {
8128 	pool_t			*pool;
8129 	char			*poolname = NULL;
8130 	struct mac_pool_arg	*mpa;
8131 
8132 	pool_lock();
8133 	mpa = kmem_zalloc(sizeof (struct mac_pool_arg), KM_SLEEP);
8134 
8135 	switch (what) {
8136 	case POOL_E_ENABLE:
8137 	case POOL_E_DISABLE:
8138 		break;
8139 
8140 	case POOL_E_CHANGE:
8141 		pool = pool_lookup_pool_by_id(id);
8142 		if (pool == NULL) {
8143 			kmem_free(mpa, sizeof (struct mac_pool_arg));
8144 			pool_unlock();
8145 			return;
8146 		}
8147 		pool_get_name(pool, &poolname);
8148 		(void) strlcpy(mpa->mpa_poolname, poolname,
8149 		    sizeof (mpa->mpa_poolname));
8150 		break;
8151 
8152 	default:
8153 		kmem_free(mpa, sizeof (struct mac_pool_arg));
8154 		pool_unlock();
8155 		return;
8156 	}
8157 	pool_unlock();
8158 
8159 	mpa->mpa_what = what;
8160 
8161 	mac_pool_update(mpa);
8162 }
8163 
8164 /*
8165  * Set effective rings property. This could be called from datapath_setup/
8166  * datapath_teardown or set-linkprop.
8167  * If the group is reserved we just go ahead and set the effective rings.
8168  * Additionally, for TX this could mean the default group has lost/gained
8169  * some rings, so if the default group is reserved, we need to adjust the
8170  * effective rings for the default group clients. For RX, if we are working
8171  * with the non-default group, we just need to reset the effective props
8172  * for the default group clients.
8173  */
8174 void
8175 mac_set_rings_effective(mac_client_impl_t *mcip)
8176 {
8177 	mac_impl_t		*mip = mcip->mci_mip;
8178 	mac_group_t		*grp;
8179 	mac_group_t		*defgrp;
8180 	flow_entry_t		*flent = mcip->mci_flent;
8181 	mac_resource_props_t	*emrp = MCIP_EFFECTIVE_PROPS(mcip);
8182 	mac_grp_client_t	*mgcp;
8183 	mac_client_impl_t	*gmcip;
8184 
8185 	grp = flent->fe_rx_ring_group;
8186 	if (grp != NULL) {
8187 		defgrp = MAC_DEFAULT_RX_GROUP(mip);
8188 		/*
8189 		 * If we have reserved a group, set the effective rings
8190 		 * to the ring count in the group.
8191 		 */
8192 		if (grp->mrg_state == MAC_GROUP_STATE_RESERVED) {
8193 			emrp->mrp_mask |= MRP_RX_RINGS;
8194 			emrp->mrp_nrxrings = grp->mrg_cur_count;
8195 		}
8196 
8197 		/*
8198 		 * We go through the clients in the shared group and
8199 		 * reset the effective properties. It is possible this
8200 		 * might have already been done for some client (i.e.
8201 		 * if some client is being moved to a group that is
8202 		 * already shared). The case where the default group is
8203 		 * RESERVED is taken care of above (note in the RX side if
8204 		 * there is a non-default group, the default group is always
8205 		 * SHARED).
8206 		 */
8207 		if (grp != defgrp || grp->mrg_state == MAC_GROUP_STATE_SHARED) {
8208 			if (grp->mrg_state == MAC_GROUP_STATE_SHARED)
8209 				mgcp = grp->mrg_clients;
8210 			else
8211 				mgcp = defgrp->mrg_clients;
8212 			while (mgcp != NULL) {
8213 				gmcip = mgcp->mgc_client;
8214 				emrp = MCIP_EFFECTIVE_PROPS(gmcip);
8215 				if (emrp->mrp_mask & MRP_RX_RINGS) {
8216 					emrp->mrp_mask &= ~MRP_RX_RINGS;
8217 					emrp->mrp_nrxrings = 0;
8218 				}
8219 				mgcp = mgcp->mgc_next;
8220 			}
8221 		}
8222 	}
8223 
8224 	/* Now the TX side */
8225 	grp = flent->fe_tx_ring_group;
8226 	if (grp != NULL) {
8227 		defgrp = MAC_DEFAULT_TX_GROUP(mip);
8228 
8229 		if (grp->mrg_state == MAC_GROUP_STATE_RESERVED) {
8230 			emrp->mrp_mask |= MRP_TX_RINGS;
8231 			emrp->mrp_ntxrings = grp->mrg_cur_count;
8232 		} else if (grp->mrg_state == MAC_GROUP_STATE_SHARED) {
8233 			mgcp = grp->mrg_clients;
8234 			while (mgcp != NULL) {
8235 				gmcip = mgcp->mgc_client;
8236 				emrp = MCIP_EFFECTIVE_PROPS(gmcip);
8237 				if (emrp->mrp_mask & MRP_TX_RINGS) {
8238 					emrp->mrp_mask &= ~MRP_TX_RINGS;
8239 					emrp->mrp_ntxrings = 0;
8240 				}
8241 				mgcp = mgcp->mgc_next;
8242 			}
8243 		}
8244 
8245 		/*
8246 		 * If the group is not the default group and the default
8247 		 * group is reserved, the ring count in the default group
8248 		 * might have changed, update it.
8249 		 */
8250 		if (grp != defgrp &&
8251 		    defgrp->mrg_state == MAC_GROUP_STATE_RESERVED) {
8252 			gmcip = MAC_GROUP_ONLY_CLIENT(defgrp);
8253 			emrp = MCIP_EFFECTIVE_PROPS(gmcip);
8254 			emrp->mrp_ntxrings = defgrp->mrg_cur_count;
8255 		}
8256 	}
8257 	emrp = MCIP_EFFECTIVE_PROPS(mcip);
8258 }
8259 
8260 /*
8261  * Check if the primary is in the default group. If so, see if we
8262  * can give it a an exclusive group now that another client is
8263  * being configured. We take the primary out of the default group
8264  * because the multicast/broadcast packets for the all the clients
8265  * will land in the default ring in the default group which means
8266  * any client in the default group, even if it is the only on in
8267  * the group, will lose exclusive access to the rings, hence
8268  * polling.
8269  */
8270 mac_client_impl_t *
8271 mac_check_primary_relocation(mac_client_impl_t *mcip, boolean_t rxhw)
8272 {
8273 	mac_impl_t		*mip = mcip->mci_mip;
8274 	mac_group_t		*defgrp = MAC_DEFAULT_RX_GROUP(mip);
8275 	flow_entry_t		*flent = mcip->mci_flent;
8276 	mac_resource_props_t	*mrp = MCIP_RESOURCE_PROPS(mcip);
8277 	uint8_t			*mac_addr;
8278 	mac_group_t		*ngrp;
8279 
8280 	/*
8281 	 * Check if the primary is in the default group, if not
8282 	 * or if it is explicitly configured to be in the default
8283 	 * group OR set the RX rings property, return.
8284 	 */
8285 	if (flent->fe_rx_ring_group != defgrp || mrp->mrp_mask & MRP_RX_RINGS)
8286 		return (NULL);
8287 
8288 	/*
8289 	 * If the new client needs an exclusive group and we
8290 	 * don't have another for the primary, return.
8291 	 */
8292 	if (rxhw && mip->mi_rxhwclnt_avail < 2)
8293 		return (NULL);
8294 
8295 	mac_addr = flent->fe_flow_desc.fd_dst_mac;
8296 	/*
8297 	 * We call this when we are setting up the datapath for
8298 	 * the first non-primary.
8299 	 */
8300 	ASSERT(mip->mi_nactiveclients == 2);
8301 
8302 	/*
8303 	 * OK, now we have the primary that needs to be relocated.
8304 	 */
8305 	ngrp =  mac_reserve_rx_group(mcip, mac_addr, B_TRUE);
8306 	if (ngrp == NULL)
8307 		return (NULL);
8308 	if (mac_rx_switch_group(mcip, defgrp, ngrp) != 0) {
8309 		mac_stop_group(ngrp);
8310 		return (NULL);
8311 	}
8312 	return (mcip);
8313 }
8314 
8315 void
8316 mac_transceiver_init(mac_impl_t *mip)
8317 {
8318 	if (mac_capab_get((mac_handle_t)mip, MAC_CAPAB_TRANSCEIVER,
8319 	    &mip->mi_transceiver)) {
8320 		/*
8321 		 * The driver set a flag that we don't know about. In this case,
8322 		 * we need to warn about that case and ignore this capability.
8323 		 */
8324 		if (mip->mi_transceiver.mct_flags != 0) {
8325 			dev_err(mip->mi_dip, CE_WARN, "driver set transceiver "
8326 			    "flags to invalid value: 0x%x, ignoring "
8327 			    "capability", mip->mi_transceiver.mct_flags);
8328 			bzero(&mip->mi_transceiver,
8329 			    sizeof (mac_capab_transceiver_t));
8330 		}
8331 	} else {
8332 			bzero(&mip->mi_transceiver,
8333 			    sizeof (mac_capab_transceiver_t));
8334 	}
8335 }
8336 
8337 int
8338 mac_transceiver_count(mac_handle_t mh, uint_t *countp)
8339 {
8340 	mac_impl_t *mip = (mac_impl_t *)mh;
8341 
8342 	ASSERT(MAC_PERIM_HELD(mh));
8343 
8344 	if (mip->mi_transceiver.mct_ntransceivers == 0)
8345 		return (ENOTSUP);
8346 
8347 	*countp = mip->mi_transceiver.mct_ntransceivers;
8348 	return (0);
8349 }
8350 
8351 int
8352 mac_transceiver_info(mac_handle_t mh, uint_t tranid, boolean_t *present,
8353     boolean_t *usable)
8354 {
8355 	int ret;
8356 	mac_transceiver_info_t info;
8357 
8358 	mac_impl_t *mip = (mac_impl_t *)mh;
8359 
8360 	ASSERT(MAC_PERIM_HELD(mh));
8361 
8362 	if (mip->mi_transceiver.mct_info == NULL ||
8363 	    mip->mi_transceiver.mct_ntransceivers == 0)
8364 		return (ENOTSUP);
8365 
8366 	if (tranid >= mip->mi_transceiver.mct_ntransceivers)
8367 		return (EINVAL);
8368 
8369 	bzero(&info, sizeof (mac_transceiver_info_t));
8370 	if ((ret = mip->mi_transceiver.mct_info(mip->mi_driver, tranid,
8371 	    &info)) != 0) {
8372 		return (ret);
8373 	}
8374 
8375 	*present = info.mti_present;
8376 	*usable = info.mti_usable;
8377 	return (0);
8378 }
8379 
8380 int
8381 mac_transceiver_read(mac_handle_t mh, uint_t tranid, uint_t page, void *buf,
8382     size_t nbytes, off_t offset, size_t *nread)
8383 {
8384 	int ret;
8385 	size_t nr;
8386 	mac_impl_t *mip = (mac_impl_t *)mh;
8387 
8388 	ASSERT(MAC_PERIM_HELD(mh));
8389 
8390 	if (mip->mi_transceiver.mct_read == NULL)
8391 		return (ENOTSUP);
8392 
8393 	if (tranid >= mip->mi_transceiver.mct_ntransceivers)
8394 		return (EINVAL);
8395 
8396 	/*
8397 	 * All supported pages today are 256 bytes wide. Make sure offset +
8398 	 * nbytes never exceeds that.
8399 	 */
8400 	if (offset < 0 || offset >= 256 || nbytes > 256 ||
8401 	    offset + nbytes > 256)
8402 		return (EINVAL);
8403 
8404 	if (nread == NULL)
8405 		nread = &nr;
8406 	ret = mip->mi_transceiver.mct_read(mip->mi_driver, tranid, page, buf,
8407 	    nbytes, offset, nread);
8408 	if (ret == 0 && *nread > nbytes) {
8409 		dev_err(mip->mi_dip, CE_PANIC, "driver wrote %lu bytes into "
8410 		    "%lu byte sized buffer, possible memory corruption",
8411 		    *nread, nbytes);
8412 	}
8413 
8414 	return (ret);
8415 }
8416 
8417 void
8418 mac_led_init(mac_impl_t *mip)
8419 {
8420 	mip->mi_led_modes = MAC_LED_DEFAULT;
8421 
8422 	if (!mac_capab_get((mac_handle_t)mip, MAC_CAPAB_LED, &mip->mi_led)) {
8423 		bzero(&mip->mi_led, sizeof (mac_capab_led_t));
8424 		return;
8425 	}
8426 
8427 	if (mip->mi_led.mcl_flags != 0) {
8428 		dev_err(mip->mi_dip, CE_WARN, "driver set led capability "
8429 		    "flags to invalid value: 0x%x, ignoring "
8430 		    "capability", mip->mi_transceiver.mct_flags);
8431 		bzero(&mip->mi_led, sizeof (mac_capab_led_t));
8432 		return;
8433 	}
8434 
8435 	if ((mip->mi_led.mcl_modes & ~MAC_LED_ALL) != 0) {
8436 		dev_err(mip->mi_dip, CE_WARN, "driver set led capability "
8437 		    "supported modes to invalid value: 0x%x, ignoring "
8438 		    "capability", mip->mi_transceiver.mct_flags);
8439 		bzero(&mip->mi_led, sizeof (mac_capab_led_t));
8440 		return;
8441 	}
8442 }
8443 
8444 int
8445 mac_led_get(mac_handle_t mh, mac_led_mode_t *supported, mac_led_mode_t *active)
8446 {
8447 	mac_impl_t *mip = (mac_impl_t *)mh;
8448 
8449 	ASSERT(MAC_PERIM_HELD(mh));
8450 
8451 	if (mip->mi_led.mcl_set == NULL)
8452 		return (ENOTSUP);
8453 
8454 	*supported = mip->mi_led.mcl_modes;
8455 	*active = mip->mi_led_modes;
8456 
8457 	return (0);
8458 }
8459 
8460 /*
8461  * Update and multiplex the various LED requests. We only ever send one LED to
8462  * the underlying driver at a time. As such, we end up multiplexing all
8463  * requested states and picking one to send down to the driver.
8464  */
8465 int
8466 mac_led_set(mac_handle_t mh, mac_led_mode_t desired)
8467 {
8468 	int ret;
8469 	mac_led_mode_t driver;
8470 
8471 	mac_impl_t *mip = (mac_impl_t *)mh;
8472 
8473 	ASSERT(MAC_PERIM_HELD(mh));
8474 
8475 	/*
8476 	 * If we've been passed a desired value of zero, that indicates that
8477 	 * we're basically resetting to the value of zero, which is our default
8478 	 * value.
8479 	 */
8480 	if (desired == 0)
8481 		desired = MAC_LED_DEFAULT;
8482 
8483 	if (mip->mi_led.mcl_set == NULL)
8484 		return (ENOTSUP);
8485 
8486 	/*
8487 	 * Catch both values that we don't know about and those that the driver
8488 	 * doesn't support.
8489 	 */
8490 	if ((desired & ~MAC_LED_ALL) != 0)
8491 		return (EINVAL);
8492 
8493 	if ((desired & ~mip->mi_led.mcl_modes) != 0)
8494 		return (ENOTSUP);
8495 
8496 	/*
8497 	 * If we have the same value, then there is nothing to do.
8498 	 */
8499 	if (desired == mip->mi_led_modes)
8500 		return (0);
8501 
8502 	/*
8503 	 * Based on the desired value, determine what to send to the driver. We
8504 	 * only will send a single bit to the driver at any given time. IDENT
8505 	 * takes priority over OFF or ON. We also let OFF take priority over the
8506 	 * rest.
8507 	 */
8508 	if (desired & MAC_LED_IDENT) {
8509 		driver = MAC_LED_IDENT;
8510 	} else if (desired & MAC_LED_OFF) {
8511 		driver = MAC_LED_OFF;
8512 	} else if (desired & MAC_LED_ON) {
8513 		driver = MAC_LED_ON;
8514 	} else {
8515 		driver = MAC_LED_DEFAULT;
8516 	}
8517 
8518 	if ((ret = mip->mi_led.mcl_set(mip->mi_driver, driver, 0)) == 0) {
8519 		mip->mi_led_modes = desired;
8520 	}
8521 
8522 	return (ret);
8523 }
8524