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