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