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