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