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