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