xref: /illumos-gate/usr/src/uts/common/io/mac/mac_client.c (revision 57f6b2a25cc23b5c46f9c6de51402167ec6754cc)
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) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2019 Joyent, Inc.
25  * Copyright 2017 RackTop Systems.
26  * Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
27  */
28 
29 /*
30  * - General Introduction:
31  *
32  * This file contains the implementation of the MAC client kernel
33  * API and related code. The MAC client API allows a kernel module
34  * to gain access to a MAC instance (physical NIC, link aggregation, etc).
35  * It allows a MAC client to associate itself with a MAC address,
36  * VLANs, callback functions for data traffic and for promiscuous mode.
37  * The MAC client API is also used to specify the properties associated
38  * with a MAC client, such as bandwidth limits, priority, CPUS, etc.
39  * These properties are further used to determine the hardware resources
40  * to allocate to the various MAC clients.
41  *
42  * - Primary MAC clients:
43  *
44  * The MAC client API refers to "primary MAC clients". A primary MAC
45  * client is a client which "owns" the primary MAC address of
46  * the underlying MAC instance. The primary MAC address is called out
47  * since it is associated with specific semantics: the primary MAC
48  * address is the MAC address which is assigned to the IP interface
49  * when it is plumbed, and the primary MAC address is assigned
50  * to VLAN data-links. The primary address of a MAC instance can
51  * also change dynamically from under the MAC client, for example
52  * as a result of a change of state of a link aggregation. In that
53  * case the MAC layer automatically updates all data-structures which
54  * refer to the current value of the primary MAC address. Typical
55  * primary MAC clients are dls, aggr, and xnb. A typical non-primary
56  * MAC client is the vnic driver.
57  *
58  * - Virtual Switching:
59  *
60  * The MAC layer implements a virtual switch between the MAC clients
61  * (primary and non-primary) defined on top of the same underlying
62  * NIC (physical, link aggregation, etc). The virtual switch is
63  * VLAN-aware, i.e. it allows multiple MAC clients to be member
64  * of one or more VLANs, and the virtual switch will distribute
65  * multicast tagged packets only to the member of the corresponding
66  * VLANs.
67  *
68  * - Upper vs Lower MAC:
69  *
70  * Creating a VNIC on top of a MAC instance effectively causes
71  * two MAC instances to be layered on top of each other, one for
72  * the VNIC(s), one for the underlying MAC instance (physical NIC,
73  * link aggregation, etc). In the code below we refer to the
74  * underlying NIC as the "lower MAC", and we refer to VNICs as
75  * the "upper MAC".
76  *
77  * - Pass-through for VNICs:
78  *
79  * When VNICs are created on top of an underlying MAC, this causes
80  * a layering of two MAC instances. Since the lower MAC already
81  * does the switching and demultiplexing to its MAC clients, the
82  * upper MAC would simply have to pass packets to the layer below
83  * or above it, which would introduce overhead. In order to avoid
84  * this overhead, the MAC layer implements a pass-through mechanism
85  * for VNICs. When a VNIC opens the lower MAC instance, it saves
86  * the MAC client handle it optains from the MAC layer. When a MAC
87  * client opens a VNIC (upper MAC), the MAC layer detects that
88  * the MAC being opened is a VNIC, and gets the MAC client handle
89  * that the VNIC driver obtained from the lower MAC. This exchange
90  * is done through a private capability between the MAC layer
91  * and the VNIC driver. The upper MAC then returns that handle
92  * directly to its MAC client. Any operation done by the upper
93  * MAC client is now done on the lower MAC client handle, which
94  * allows the VNIC driver to be completely bypassed for the
95  * performance sensitive data-path.
96  *
97  * - Secondary MACs for VNICs:
98  *
99  * VNICs support multiple upper mac clients to enable support for
100  * multiple MAC addresses on the VNIC. When the VNIC is created the
101  * initial mac client is the primary upper mac. Any additional mac
102  * clients are secondary macs. These are kept in sync with the primary
103  * (for things such as the rx function and resource control settings)
104  * using the same private capability interface between the MAC layer
105  * and the VNIC layer.
106  *
107  */
108 
109 #include <sys/types.h>
110 #include <sys/conf.h>
111 #include <sys/id_space.h>
112 #include <sys/esunddi.h>
113 #include <sys/stat.h>
114 #include <sys/mkdev.h>
115 #include <sys/stream.h>
116 #include <sys/strsun.h>
117 #include <sys/strsubr.h>
118 #include <sys/dlpi.h>
119 #include <sys/modhash.h>
120 #include <sys/mac_impl.h>
121 #include <sys/mac_client_impl.h>
122 #include <sys/mac_soft_ring.h>
123 #include <sys/mac_stat.h>
124 #include <sys/dls.h>
125 #include <sys/dld.h>
126 #include <sys/modctl.h>
127 #include <sys/fs/dv_node.h>
128 #include <sys/thread.h>
129 #include <sys/proc.h>
130 #include <sys/callb.h>
131 #include <sys/cpuvar.h>
132 #include <sys/atomic.h>
133 #include <sys/sdt.h>
134 #include <sys/mac_flow.h>
135 #include <sys/ddi_intr_impl.h>
136 #include <sys/disp.h>
137 #include <sys/sdt.h>
138 #include <sys/vnic.h>
139 #include <sys/vnic_impl.h>
140 #include <sys/vlan.h>
141 #include <inet/ip.h>
142 #include <inet/ip6.h>
143 #include <sys/exacct.h>
144 #include <sys/exacct_impl.h>
145 #include <inet/nd.h>
146 #include <sys/ethernet.h>
147 
148 kmem_cache_t	*mac_client_impl_cache;
149 kmem_cache_t	*mac_promisc_impl_cache;
150 
151 static boolean_t mac_client_single_rcvr(mac_client_impl_t *);
152 static flow_entry_t *mac_client_swap_mciflent(mac_client_impl_t *);
153 static flow_entry_t *mac_client_get_flow(mac_client_impl_t *,
154     mac_unicast_impl_t *);
155 static void mac_client_remove_flow_from_list(mac_client_impl_t *,
156     flow_entry_t *);
157 static void mac_client_add_to_flow_list(mac_client_impl_t *, flow_entry_t *);
158 static void mac_rename_flow_names(mac_client_impl_t *, const char *);
159 static void mac_virtual_link_update(mac_impl_t *);
160 static int mac_client_datapath_setup(mac_client_impl_t *, uint16_t,
161     uint8_t *, mac_resource_props_t *, boolean_t, mac_unicast_impl_t *);
162 static void mac_client_datapath_teardown(mac_client_handle_t,
163     mac_unicast_impl_t *, flow_entry_t *);
164 static int mac_resource_ctl_set(mac_client_handle_t, mac_resource_props_t *);
165 
166 /* ARGSUSED */
167 static int
168 i_mac_client_impl_ctor(void *buf, void *arg, int kmflag)
169 {
170 	int	i;
171 	mac_client_impl_t	*mcip = buf;
172 
173 	bzero(buf, MAC_CLIENT_IMPL_SIZE);
174 	mutex_init(&mcip->mci_tx_cb_lock, NULL, MUTEX_DRIVER, NULL);
175 	mcip->mci_tx_notify_cb_info.mcbi_lockp = &mcip->mci_tx_cb_lock;
176 
177 	ASSERT(mac_tx_percpu_cnt >= 0);
178 	for (i = 0; i <= mac_tx_percpu_cnt; i++) {
179 		mutex_init(&mcip->mci_tx_pcpu[i].pcpu_tx_lock, NULL,
180 		    MUTEX_DRIVER, NULL);
181 	}
182 	cv_init(&mcip->mci_tx_cv, NULL, CV_DRIVER, NULL);
183 
184 	return (0);
185 }
186 
187 /* ARGSUSED */
188 static void
189 i_mac_client_impl_dtor(void *buf, void *arg)
190 {
191 	int	i;
192 	mac_client_impl_t *mcip = buf;
193 
194 	ASSERT(mcip->mci_promisc_list == NULL);
195 	ASSERT(mcip->mci_unicast_list == NULL);
196 	ASSERT(mcip->mci_state_flags == 0);
197 	ASSERT(mcip->mci_tx_flag == 0);
198 
199 	mutex_destroy(&mcip->mci_tx_cb_lock);
200 
201 	ASSERT(mac_tx_percpu_cnt >= 0);
202 	for (i = 0; i <= mac_tx_percpu_cnt; i++) {
203 		ASSERT(mcip->mci_tx_pcpu[i].pcpu_tx_refcnt == 0);
204 		mutex_destroy(&mcip->mci_tx_pcpu[i].pcpu_tx_lock);
205 	}
206 	cv_destroy(&mcip->mci_tx_cv);
207 }
208 
209 /* ARGSUSED */
210 static int
211 i_mac_promisc_impl_ctor(void *buf, void *arg, int kmflag)
212 {
213 	mac_promisc_impl_t	*mpip = buf;
214 
215 	bzero(buf, sizeof (mac_promisc_impl_t));
216 	mpip->mpi_mci_link.mcb_objp = buf;
217 	mpip->mpi_mci_link.mcb_objsize = sizeof (mac_promisc_impl_t);
218 	mpip->mpi_mi_link.mcb_objp = buf;
219 	mpip->mpi_mi_link.mcb_objsize = sizeof (mac_promisc_impl_t);
220 	return (0);
221 }
222 
223 /* ARGSUSED */
224 static void
225 i_mac_promisc_impl_dtor(void *buf, void *arg)
226 {
227 	mac_promisc_impl_t	*mpip = buf;
228 
229 	ASSERT(mpip->mpi_mci_link.mcb_objp != NULL);
230 	ASSERT(mpip->mpi_mci_link.mcb_objsize == sizeof (mac_promisc_impl_t));
231 	ASSERT(mpip->mpi_mi_link.mcb_objp == mpip->mpi_mci_link.mcb_objp);
232 	ASSERT(mpip->mpi_mi_link.mcb_objsize == sizeof (mac_promisc_impl_t));
233 
234 	mpip->mpi_mci_link.mcb_objp = NULL;
235 	mpip->mpi_mci_link.mcb_objsize = 0;
236 	mpip->mpi_mi_link.mcb_objp = NULL;
237 	mpip->mpi_mi_link.mcb_objsize = 0;
238 
239 	ASSERT(mpip->mpi_mci_link.mcb_flags == 0);
240 	mpip->mpi_mci_link.mcb_objsize = 0;
241 }
242 
243 void
244 mac_client_init(void)
245 {
246 	ASSERT(mac_tx_percpu_cnt >= 0);
247 
248 	mac_client_impl_cache = kmem_cache_create("mac_client_impl_cache",
249 	    MAC_CLIENT_IMPL_SIZE, 0, i_mac_client_impl_ctor,
250 	    i_mac_client_impl_dtor, NULL, NULL, NULL, 0);
251 	ASSERT(mac_client_impl_cache != NULL);
252 
253 	mac_promisc_impl_cache = kmem_cache_create("mac_promisc_impl_cache",
254 	    sizeof (mac_promisc_impl_t), 0, i_mac_promisc_impl_ctor,
255 	    i_mac_promisc_impl_dtor, NULL, NULL, NULL, 0);
256 	ASSERT(mac_promisc_impl_cache != NULL);
257 }
258 
259 void
260 mac_client_fini(void)
261 {
262 	kmem_cache_destroy(mac_client_impl_cache);
263 	kmem_cache_destroy(mac_promisc_impl_cache);
264 }
265 
266 /*
267  * Return the lower MAC client handle from the VNIC driver for the
268  * specified VNIC MAC instance.
269  */
270 mac_client_impl_t *
271 mac_vnic_lower(mac_impl_t *mip)
272 {
273 	mac_capab_vnic_t cap;
274 	mac_client_impl_t *mcip;
275 
276 	VERIFY(i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_VNIC, &cap));
277 	mcip = cap.mcv_mac_client_handle(cap.mcv_arg);
278 
279 	return (mcip);
280 }
281 
282 /*
283  * Update the secondary macs
284  */
285 void
286 mac_vnic_secondary_update(mac_impl_t *mip)
287 {
288 	mac_capab_vnic_t cap;
289 
290 	VERIFY(i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_VNIC, &cap));
291 	cap.mcv_mac_secondary_update(cap.mcv_arg);
292 }
293 
294 /*
295  * Return the MAC client handle of the primary MAC client for the
296  * specified MAC instance, or NULL otherwise.
297  */
298 mac_client_impl_t *
299 mac_primary_client_handle(mac_impl_t *mip)
300 {
301 	mac_client_impl_t *mcip;
302 
303 	if (mip->mi_state_flags & MIS_IS_VNIC)
304 		return (mac_vnic_lower(mip));
305 
306 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
307 
308 	for (mcip = mip->mi_clients_list; mcip != NULL;
309 	    mcip = mcip->mci_client_next) {
310 		if (MCIP_DATAPATH_SETUP(mcip) && mac_is_primary_client(mcip))
311 			return (mcip);
312 	}
313 	return (NULL);
314 }
315 
316 /*
317  * Open a MAC specified by its MAC name.
318  */
319 int
320 mac_open(const char *macname, mac_handle_t *mhp)
321 {
322 	mac_impl_t	*mip;
323 	int		err;
324 
325 	/*
326 	 * Look up its entry in the global hash table.
327 	 */
328 	if ((err = mac_hold(macname, &mip)) != 0)
329 		return (err);
330 
331 	/*
332 	 * Hold the dip associated to the MAC to prevent it from being
333 	 * detached. For a softmac, its underlying dip is held by the
334 	 * mi_open() callback.
335 	 *
336 	 * This is done to be more tolerant with some defective drivers,
337 	 * which incorrectly handle mac_unregister() failure in their
338 	 * xxx_detach() routine. For example, some drivers ignore the
339 	 * failure of mac_unregister() and free all resources that
340 	 * that are needed for data transmition.
341 	 */
342 	e_ddi_hold_devi(mip->mi_dip);
343 
344 	if (!(mip->mi_callbacks->mc_callbacks & MC_OPEN)) {
345 		*mhp = (mac_handle_t)mip;
346 		return (0);
347 	}
348 
349 	/*
350 	 * The mac perimeter is used in both mac_open and mac_close by the
351 	 * framework to single thread the MC_OPEN/MC_CLOSE of drivers.
352 	 */
353 	i_mac_perim_enter(mip);
354 	mip->mi_oref++;
355 	if (mip->mi_oref != 1 || ((err = mip->mi_open(mip->mi_driver)) == 0)) {
356 		*mhp = (mac_handle_t)mip;
357 		i_mac_perim_exit(mip);
358 		return (0);
359 	}
360 	mip->mi_oref--;
361 	ddi_release_devi(mip->mi_dip);
362 	mac_rele(mip);
363 	i_mac_perim_exit(mip);
364 	return (err);
365 }
366 
367 /*
368  * Open a MAC specified by its linkid.
369  */
370 int
371 mac_open_by_linkid(datalink_id_t linkid, mac_handle_t *mhp)
372 {
373 	dls_dl_handle_t	dlh;
374 	int		err;
375 
376 	if ((err = dls_devnet_hold_tmp(linkid, &dlh)) != 0)
377 		return (err);
378 
379 	dls_devnet_prop_task_wait(dlh);
380 
381 	err = mac_open(dls_devnet_mac(dlh), mhp);
382 
383 	dls_devnet_rele_tmp(dlh);
384 	return (err);
385 }
386 
387 /*
388  * Open a MAC specified by its link name.
389  */
390 int
391 mac_open_by_linkname(const char *link, mac_handle_t *mhp)
392 {
393 	datalink_id_t	linkid;
394 	int		err;
395 
396 	if ((err = dls_mgmt_get_linkid(link, &linkid)) != 0)
397 		return (err);
398 	return (mac_open_by_linkid(linkid, mhp));
399 }
400 
401 /*
402  * Close the specified MAC.
403  */
404 void
405 mac_close(mac_handle_t mh)
406 {
407 	mac_impl_t	*mip = (mac_impl_t *)mh;
408 
409 	i_mac_perim_enter(mip);
410 	/*
411 	 * The mac perimeter is used in both mac_open and mac_close by the
412 	 * framework to single thread the MC_OPEN/MC_CLOSE of drivers.
413 	 */
414 	if (mip->mi_callbacks->mc_callbacks & MC_OPEN) {
415 		ASSERT(mip->mi_oref != 0);
416 		if (--mip->mi_oref == 0) {
417 			if ((mip->mi_callbacks->mc_callbacks & MC_CLOSE))
418 				mip->mi_close(mip->mi_driver);
419 		}
420 	}
421 	i_mac_perim_exit(mip);
422 	ddi_release_devi(mip->mi_dip);
423 	mac_rele(mip);
424 }
425 
426 /*
427  * Misc utility functions to retrieve various information about a MAC
428  * instance or a MAC client.
429  */
430 
431 const mac_info_t *
432 mac_info(mac_handle_t mh)
433 {
434 	return (&((mac_impl_t *)mh)->mi_info);
435 }
436 
437 dev_info_t *
438 mac_devinfo_get(mac_handle_t mh)
439 {
440 	return (((mac_impl_t *)mh)->mi_dip);
441 }
442 
443 void *
444 mac_driver(mac_handle_t mh)
445 {
446 	return (((mac_impl_t *)mh)->mi_driver);
447 }
448 
449 const char *
450 mac_name(mac_handle_t mh)
451 {
452 	return (((mac_impl_t *)mh)->mi_name);
453 }
454 
455 int
456 mac_type(mac_handle_t mh)
457 {
458 	return (((mac_impl_t *)mh)->mi_type->mt_type);
459 }
460 
461 int
462 mac_nativetype(mac_handle_t mh)
463 {
464 	return (((mac_impl_t *)mh)->mi_type->mt_nativetype);
465 }
466 
467 char *
468 mac_client_name(mac_client_handle_t mch)
469 {
470 	return (((mac_client_impl_t *)mch)->mci_name);
471 }
472 
473 minor_t
474 mac_minor(mac_handle_t mh)
475 {
476 	return (((mac_impl_t *)mh)->mi_minor);
477 }
478 
479 /*
480  * Return the VID associated with a MAC client. This function should
481  * be called for clients which are associated with only one VID.
482  */
483 uint16_t
484 mac_client_vid(mac_client_handle_t mch)
485 {
486 	uint16_t		vid = VLAN_ID_NONE;
487 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
488 	flow_desc_t		flow_desc;
489 
490 	if (mcip->mci_nflents == 0)
491 		return (vid);
492 
493 	ASSERT(MCIP_DATAPATH_SETUP(mcip) && mac_client_single_rcvr(mcip));
494 
495 	mac_flow_get_desc(mcip->mci_flent, &flow_desc);
496 	if ((flow_desc.fd_mask & FLOW_LINK_VID) != 0)
497 		vid = flow_desc.fd_vid;
498 
499 	return (vid);
500 }
501 
502 /*
503  * Return whether the specified MAC client corresponds to a VLAN VNIC.
504  */
505 boolean_t
506 mac_client_is_vlan_vnic(mac_client_handle_t mch)
507 {
508 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
509 
510 	return (((mcip->mci_state_flags & MCIS_IS_VNIC) != 0) &&
511 	    ((mcip->mci_flent->fe_type & FLOW_PRIMARY_MAC) != 0));
512 }
513 
514 /*
515  * Return the link speed associated with the specified MAC client.
516  *
517  * The link speed of a MAC client is equal to the smallest value of
518  * 1) the current link speed of the underlying NIC, or
519  * 2) the bandwidth limit set for the MAC client.
520  *
521  * Note that the bandwidth limit can be higher than the speed
522  * of the underlying NIC. This is allowed to avoid spurious
523  * administration action failures or artifically lowering the
524  * bandwidth limit of a link that may  have temporarily lowered
525  * its link speed due to hardware problem or administrator action.
526  */
527 static uint64_t
528 mac_client_ifspeed(mac_client_impl_t *mcip)
529 {
530 	mac_impl_t *mip = mcip->mci_mip;
531 	uint64_t nic_speed;
532 
533 	nic_speed = mac_stat_get((mac_handle_t)mip, MAC_STAT_IFSPEED);
534 
535 	if (nic_speed == 0) {
536 		return (0);
537 	} else {
538 		uint64_t policy_limit = (uint64_t)-1;
539 
540 		if (MCIP_RESOURCE_PROPS_MASK(mcip) & MRP_MAXBW)
541 			policy_limit = MCIP_RESOURCE_PROPS_MAXBW(mcip);
542 
543 		return (MIN(policy_limit, nic_speed));
544 	}
545 }
546 
547 /*
548  * Return the link state of the specified client. If here are more
549  * than one clients of the underying mac_impl_t, the link state
550  * will always be UP regardless of the link state of the underlying
551  * mac_impl_t. This is needed to allow the MAC clients to continue
552  * to communicate with each other even when the physical link of
553  * their mac_impl_t is down.
554  */
555 static uint64_t
556 mac_client_link_state(mac_client_impl_t *mcip)
557 {
558 	mac_impl_t *mip = mcip->mci_mip;
559 	uint16_t vid;
560 	mac_client_impl_t *mci_list;
561 	mac_unicast_impl_t *mui_list, *oth_mui_list;
562 
563 	/*
564 	 * Returns LINK_STATE_UP if there are other MAC clients defined on
565 	 * mac_impl_t which share same VLAN ID as that of mcip. Note that
566 	 * if 'mcip' has more than one VID's then we match ANY one of the
567 	 * VID's with other MAC client's VID's and return LINK_STATE_UP.
568 	 */
569 	rw_enter(&mcip->mci_rw_lock, RW_READER);
570 	for (mui_list = mcip->mci_unicast_list; mui_list != NULL;
571 	    mui_list = mui_list->mui_next) {
572 		vid = mui_list->mui_vid;
573 		for (mci_list = mip->mi_clients_list; mci_list != NULL;
574 		    mci_list = mci_list->mci_client_next) {
575 			if (mci_list == mcip)
576 				continue;
577 			for (oth_mui_list = mci_list->mci_unicast_list;
578 			    oth_mui_list != NULL; oth_mui_list = oth_mui_list->
579 			    mui_next) {
580 				if (vid == oth_mui_list->mui_vid) {
581 					rw_exit(&mcip->mci_rw_lock);
582 					return (LINK_STATE_UP);
583 				}
584 			}
585 		}
586 	}
587 	rw_exit(&mcip->mci_rw_lock);
588 
589 	return (mac_stat_get((mac_handle_t)mip, MAC_STAT_LINK_STATE));
590 }
591 
592 /*
593  * These statistics are consumed by dladm show-link -s <vnic>,
594  * dladm show-vnic -s and netstat. With the introduction of dlstat,
595  * dladm show-link -s and dladm show-vnic -s witll be EOL'ed while
596  * netstat will consume from kstats introduced for dlstat. This code
597  * will be removed at that time.
598  */
599 
600 /*
601  * Return the statistics of a MAC client. These statistics are different
602  * then the statistics of the underlying MAC which are returned by
603  * mac_stat_get().
604  *
605  * Note that for things based on the tx and rx stats, mac will end up clobbering
606  * those stats when the underlying set of rings in the srs changes. As such, we
607  * need to source not only the current set, but also the historical set when
608  * returning to the client, lest our counters appear to go backwards.
609  */
610 uint64_t
611 mac_client_stat_get(mac_client_handle_t mch, uint_t stat)
612 {
613 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
614 	mac_impl_t		*mip = mcip->mci_mip;
615 	flow_entry_t		*flent = mcip->mci_flent;
616 	mac_soft_ring_set_t	*mac_srs;
617 	mac_rx_stats_t		*mac_rx_stat, *old_rx_stat;
618 	mac_tx_stats_t		*mac_tx_stat, *old_tx_stat;
619 	int i;
620 	uint64_t val = 0;
621 
622 	mac_srs = (mac_soft_ring_set_t *)(flent->fe_tx_srs);
623 	mac_tx_stat = &mac_srs->srs_tx.st_stat;
624 	old_rx_stat = &mcip->mci_misc_stat.mms_defunctrxlanestats;
625 	old_tx_stat = &mcip->mci_misc_stat.mms_defuncttxlanestats;
626 
627 	switch (stat) {
628 	case MAC_STAT_LINK_STATE:
629 		val = mac_client_link_state(mcip);
630 		break;
631 	case MAC_STAT_LINK_UP:
632 		val = (mac_client_link_state(mcip) == LINK_STATE_UP);
633 		break;
634 	case MAC_STAT_PROMISC:
635 		val = mac_stat_get((mac_handle_t)mip, MAC_STAT_PROMISC);
636 		break;
637 	case MAC_STAT_LOWLINK_STATE:
638 		val = mac_stat_get((mac_handle_t)mip, MAC_STAT_LOWLINK_STATE);
639 		break;
640 	case MAC_STAT_IFSPEED:
641 		val = mac_client_ifspeed(mcip);
642 		break;
643 	case MAC_STAT_MULTIRCV:
644 		val = mcip->mci_misc_stat.mms_multircv;
645 		break;
646 	case MAC_STAT_BRDCSTRCV:
647 		val = mcip->mci_misc_stat.mms_brdcstrcv;
648 		break;
649 	case MAC_STAT_MULTIXMT:
650 		val = mcip->mci_misc_stat.mms_multixmt;
651 		break;
652 	case MAC_STAT_BRDCSTXMT:
653 		val = mcip->mci_misc_stat.mms_brdcstxmt;
654 		break;
655 	case MAC_STAT_OBYTES:
656 		val = mac_tx_stat->mts_obytes;
657 		val += old_tx_stat->mts_obytes;
658 		break;
659 	case MAC_STAT_OPACKETS:
660 		val = mac_tx_stat->mts_opackets;
661 		val += old_tx_stat->mts_opackets;
662 		break;
663 	case MAC_STAT_OERRORS:
664 		val = mac_tx_stat->mts_oerrors;
665 		val += old_tx_stat->mts_oerrors;
666 		break;
667 	case MAC_STAT_IPACKETS:
668 		for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
669 			mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
670 			mac_rx_stat = &mac_srs->srs_rx.sr_stat;
671 			val += mac_rx_stat->mrs_intrcnt +
672 			    mac_rx_stat->mrs_pollcnt + mac_rx_stat->mrs_lclcnt;
673 		}
674 		val += old_rx_stat->mrs_intrcnt + old_rx_stat->mrs_pollcnt +
675 		    old_rx_stat->mrs_lclcnt;
676 		break;
677 	case MAC_STAT_RBYTES:
678 		for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
679 			mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
680 			mac_rx_stat = &mac_srs->srs_rx.sr_stat;
681 			val += mac_rx_stat->mrs_intrbytes +
682 			    mac_rx_stat->mrs_pollbytes +
683 			    mac_rx_stat->mrs_lclbytes;
684 		}
685 		val += old_rx_stat->mrs_intrbytes + old_rx_stat->mrs_pollbytes +
686 		    old_rx_stat->mrs_lclbytes;
687 		break;
688 	case MAC_STAT_IERRORS:
689 		for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
690 			mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
691 			mac_rx_stat = &mac_srs->srs_rx.sr_stat;
692 			val += mac_rx_stat->mrs_ierrors;
693 		}
694 		val += old_rx_stat->mrs_ierrors;
695 		break;
696 	default:
697 		val = mac_driver_stat_default(mip, stat);
698 		break;
699 	}
700 
701 	return (val);
702 }
703 
704 /*
705  * Return the statistics of the specified MAC instance.
706  */
707 uint64_t
708 mac_stat_get(mac_handle_t mh, uint_t stat)
709 {
710 	mac_impl_t	*mip = (mac_impl_t *)mh;
711 	uint64_t	val;
712 	int		ret;
713 
714 	/*
715 	 * The range of stat determines where it is maintained.  Stat
716 	 * values from 0 up to (but not including) MAC_STAT_MIN are
717 	 * mainteined by the mac module itself.  Everything else is
718 	 * maintained by the driver.
719 	 *
720 	 * If the mac_impl_t being queried corresponds to a VNIC,
721 	 * the stats need to be queried from the lower MAC client
722 	 * corresponding to the VNIC. (The mac_link_update()
723 	 * invoked by the driver to the lower MAC causes the *lower
724 	 * MAC* to update its mi_linkstate, and send a notification
725 	 * to its MAC clients. Due to the VNIC passthrough,
726 	 * these notifications are sent to the upper MAC clients
727 	 * of the VNIC directly, and the upper mac_impl_t of the VNIC
728 	 * does not have a valid mi_linkstate.
729 	 */
730 	if (stat < MAC_STAT_MIN && !(mip->mi_state_flags & MIS_IS_VNIC)) {
731 		/* these stats are maintained by the mac module itself */
732 		switch (stat) {
733 		case MAC_STAT_LINK_STATE:
734 			return (mip->mi_linkstate);
735 		case MAC_STAT_LINK_UP:
736 			return (mip->mi_linkstate == LINK_STATE_UP);
737 		case MAC_STAT_PROMISC:
738 			return (mip->mi_devpromisc != 0);
739 		case MAC_STAT_LOWLINK_STATE:
740 			return (mip->mi_lowlinkstate);
741 		default:
742 			ASSERT(B_FALSE);
743 		}
744 	}
745 
746 	/*
747 	 * Call the driver to get the given statistic.
748 	 */
749 	ret = mip->mi_getstat(mip->mi_driver, stat, &val);
750 	if (ret != 0) {
751 		/*
752 		 * The driver doesn't support this statistic.  Get the
753 		 * statistic's default value.
754 		 */
755 		val = mac_driver_stat_default(mip, stat);
756 	}
757 	return (val);
758 }
759 
760 /*
761  * Query hardware rx ring corresponding to the pseudo ring.
762  */
763 uint64_t
764 mac_pseudo_rx_ring_stat_get(mac_ring_handle_t handle, uint_t stat)
765 {
766 	return (mac_rx_ring_stat_get(handle, stat));
767 }
768 
769 /*
770  * Query hardware tx ring corresponding to the pseudo ring.
771  */
772 uint64_t
773 mac_pseudo_tx_ring_stat_get(mac_ring_handle_t handle, uint_t stat)
774 {
775 	return (mac_tx_ring_stat_get(handle, stat));
776 }
777 
778 /*
779  * Utility function which returns the VID associated with a flow entry.
780  */
781 uint16_t
782 i_mac_flow_vid(flow_entry_t *flent)
783 {
784 	flow_desc_t	flow_desc;
785 
786 	mac_flow_get_desc(flent, &flow_desc);
787 
788 	if ((flow_desc.fd_mask & FLOW_LINK_VID) != 0)
789 		return (flow_desc.fd_vid);
790 	return (VLAN_ID_NONE);
791 }
792 
793 /*
794  * Verify the validity of the specified unicast MAC address. Returns B_TRUE
795  * if the address is valid, B_FALSE otherwise (multicast address, or incorrect
796  * length.
797  */
798 boolean_t
799 mac_unicst_verify(mac_handle_t mh, const uint8_t *addr, uint_t len)
800 {
801 	mac_impl_t	*mip = (mac_impl_t *)mh;
802 
803 	/*
804 	 * Verify the address. No lock is needed since mi_type and plugin
805 	 * details don't change after mac_register().
806 	 */
807 	if ((len != mip->mi_type->mt_addr_length) ||
808 	    (mip->mi_type->mt_ops.mtops_unicst_verify(addr,
809 	    mip->mi_pdata)) != 0) {
810 		return (B_FALSE);
811 	} else {
812 		return (B_TRUE);
813 	}
814 }
815 
816 void
817 mac_sdu_get(mac_handle_t mh, uint_t *min_sdu, uint_t *max_sdu)
818 {
819 	mac_impl_t	*mip = (mac_impl_t *)mh;
820 
821 	if (min_sdu != NULL)
822 		*min_sdu = mip->mi_sdu_min;
823 	if (max_sdu != NULL)
824 		*max_sdu = mip->mi_sdu_max;
825 }
826 
827 void
828 mac_sdu_get2(mac_handle_t mh, uint_t *min_sdu, uint_t *max_sdu,
829     uint_t *multicast_sdu)
830 {
831 	mac_impl_t	*mip = (mac_impl_t *)mh;
832 
833 	if (min_sdu != NULL)
834 		*min_sdu = mip->mi_sdu_min;
835 	if (max_sdu != NULL)
836 		*max_sdu = mip->mi_sdu_max;
837 	if (multicast_sdu != NULL)
838 		*multicast_sdu = mip->mi_sdu_multicast;
839 }
840 
841 /*
842  * Update the MAC unicast address of the specified client's flows. Currently
843  * only one unicast MAC unicast address is allowed per client.
844  */
845 static void
846 mac_unicast_update_client_flow(mac_client_impl_t *mcip)
847 {
848 	mac_impl_t *mip = mcip->mci_mip;
849 	flow_entry_t *flent = mcip->mci_flent;
850 	mac_address_t *map = mcip->mci_unicast;
851 	flow_desc_t flow_desc;
852 
853 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
854 	ASSERT(flent != NULL);
855 
856 	mac_flow_get_desc(flent, &flow_desc);
857 	ASSERT(flow_desc.fd_mask & FLOW_LINK_DST);
858 
859 	bcopy(map->ma_addr, flow_desc.fd_dst_mac, map->ma_len);
860 	mac_flow_set_desc(flent, &flow_desc);
861 
862 	/*
863 	 * The v6 local and SLAAC addrs (used by mac protection) need to be
864 	 * regenerated because our mac address has changed.
865 	 */
866 	mac_protect_update_mac_token(mcip);
867 
868 	/*
869 	 * When there are multiple VLANs sharing the same MAC address,
870 	 * each gets its own MAC client, except when running on sun4v
871 	 * vsw. In that case the mci_flent_list is used to place
872 	 * multiple VLAN flows on one MAC client. If we ever get rid
873 	 * of vsw then this code can go, but until then we need to
874 	 * update all flow entries.
875 	 */
876 	for (flent = mcip->mci_flent_list; flent != NULL;
877 	    flent = flent->fe_client_next) {
878 		mac_flow_get_desc(flent, &flow_desc);
879 		if (!(flent->fe_type & FLOW_PRIMARY_MAC ||
880 		    flent->fe_type & FLOW_VNIC_MAC))
881 			continue;
882 
883 		bcopy(map->ma_addr, flow_desc.fd_dst_mac, map->ma_len);
884 		mac_flow_set_desc(flent, &flow_desc);
885 	}
886 }
887 
888 /*
889  * Update all clients that share the same unicast address.
890  */
891 void
892 mac_unicast_update_clients(mac_impl_t *mip, mac_address_t *map)
893 {
894 	mac_client_impl_t *mcip;
895 
896 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
897 
898 	/*
899 	 * Find all clients that share the same unicast MAC address and update
900 	 * them appropriately.
901 	 */
902 	for (mcip = mip->mi_clients_list; mcip != NULL;
903 	    mcip = mcip->mci_client_next) {
904 		/*
905 		 * Ignore clients that don't share this MAC address.
906 		 */
907 		if (map != mcip->mci_unicast)
908 			continue;
909 
910 		/*
911 		 * Update those clients with same old unicast MAC address.
912 		 */
913 		mac_unicast_update_client_flow(mcip);
914 	}
915 }
916 
917 /*
918  * Update the unicast MAC address of the specified VNIC MAC client.
919  *
920  * Check whether the operation is valid. Any of following cases should fail:
921  *
922  * 1. It's a VLAN type of VNIC.
923  * 2. The new value is current "primary" MAC address.
924  * 3. The current MAC address is shared with other clients.
925  * 4. The new MAC address has been used. This case will be valid when
926  *    client migration is fully supported.
927  */
928 int
929 mac_vnic_unicast_set(mac_client_handle_t mch, const uint8_t *addr)
930 {
931 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
932 	mac_impl_t *mip = mcip->mci_mip;
933 	mac_address_t *map = mcip->mci_unicast;
934 	int err;
935 
936 	ASSERT(!(mip->mi_state_flags & MIS_IS_VNIC));
937 	ASSERT(mcip->mci_state_flags & MCIS_IS_VNIC);
938 	ASSERT(mcip->mci_flags != MAC_CLIENT_FLAGS_PRIMARY);
939 
940 	i_mac_perim_enter(mip);
941 
942 	/*
943 	 * If this is a VLAN type of VNIC, it's using "primary" MAC address
944 	 * of the underlying interface. Must fail here. Refer to case 1 above.
945 	 */
946 	if (bcmp(map->ma_addr, mip->mi_addr, map->ma_len) == 0) {
947 		i_mac_perim_exit(mip);
948 		return (ENOTSUP);
949 	}
950 
951 	/*
952 	 * If the new address is the "primary" one, must fail. Refer to
953 	 * case 2 above.
954 	 */
955 	if (bcmp(addr, mip->mi_addr, map->ma_len) == 0) {
956 		i_mac_perim_exit(mip);
957 		return (EACCES);
958 	}
959 
960 	/*
961 	 * If the address is shared by multiple clients, must fail. Refer
962 	 * to case 3 above.
963 	 */
964 	if (mac_check_macaddr_shared(map)) {
965 		i_mac_perim_exit(mip);
966 		return (EBUSY);
967 	}
968 
969 	/*
970 	 * If the new address has been used, must fail for now. Refer to
971 	 * case 4 above.
972 	 */
973 	if (mac_find_macaddr(mip, (uint8_t *)addr) != NULL) {
974 		i_mac_perim_exit(mip);
975 		return (ENOTSUP);
976 	}
977 
978 	/*
979 	 * Update the MAC address.
980 	 */
981 	err = mac_update_macaddr(map, (uint8_t *)addr);
982 
983 	if (err != 0) {
984 		i_mac_perim_exit(mip);
985 		return (err);
986 	}
987 
988 	/*
989 	 * Update all flows of this MAC client.
990 	 */
991 	mac_unicast_update_client_flow(mcip);
992 
993 	i_mac_perim_exit(mip);
994 	return (0);
995 }
996 
997 /*
998  * Program the new primary unicast address of the specified MAC.
999  *
1000  * Function mac_update_macaddr() takes care different types of underlying
1001  * MAC. If the underlying MAC is VNIC, the VNIC driver must have registerd
1002  * mi_unicst() entry point, that indirectly calls mac_vnic_unicast_set()
1003  * which will take care of updating the MAC address of the corresponding
1004  * MAC client.
1005  *
1006  * This is the only interface that allow the client to update the "primary"
1007  * MAC address of the underlying MAC. The new value must have not been
1008  * used by other clients.
1009  */
1010 int
1011 mac_unicast_primary_set(mac_handle_t mh, const uint8_t *addr)
1012 {
1013 	mac_impl_t *mip = (mac_impl_t *)mh;
1014 	mac_address_t *map;
1015 	int err;
1016 
1017 	/* verify the address validity */
1018 	if (!mac_unicst_verify(mh, addr, mip->mi_type->mt_addr_length))
1019 		return (EINVAL);
1020 
1021 	i_mac_perim_enter(mip);
1022 
1023 	/*
1024 	 * If the new value is the same as the current primary address value,
1025 	 * there's nothing to do.
1026 	 */
1027 	if (bcmp(addr, mip->mi_addr, mip->mi_type->mt_addr_length) == 0) {
1028 		i_mac_perim_exit(mip);
1029 		return (0);
1030 	}
1031 
1032 	if (mac_find_macaddr(mip, (uint8_t *)addr) != NULL) {
1033 		i_mac_perim_exit(mip);
1034 		return (EBUSY);
1035 	}
1036 
1037 	map = mac_find_macaddr(mip, mip->mi_addr);
1038 	ASSERT(map != NULL);
1039 
1040 	/*
1041 	 * Update the MAC address.
1042 	 */
1043 	if (mip->mi_state_flags & MIS_IS_AGGR) {
1044 		mac_capab_aggr_t aggr_cap;
1045 
1046 		/*
1047 		 * If the MAC is an aggregation, other than the unicast
1048 		 * addresses programming, aggr must be informed about this
1049 		 * primary unicst address change to change its MAC address
1050 		 * policy to be user-specified.
1051 		 */
1052 		ASSERT(map->ma_type == MAC_ADDRESS_TYPE_UNICAST_CLASSIFIED);
1053 		VERIFY(i_mac_capab_get(mh, MAC_CAPAB_AGGR, &aggr_cap));
1054 		err = aggr_cap.mca_unicst(mip->mi_driver, addr);
1055 		if (err == 0)
1056 			bcopy(addr, map->ma_addr, map->ma_len);
1057 	} else {
1058 		err = mac_update_macaddr(map, (uint8_t *)addr);
1059 	}
1060 
1061 	if (err != 0) {
1062 		i_mac_perim_exit(mip);
1063 		return (err);
1064 	}
1065 
1066 	mac_unicast_update_clients(mip, map);
1067 
1068 	/*
1069 	 * Save the new primary MAC address in mac_impl_t.
1070 	 */
1071 	bcopy(addr, mip->mi_addr, mip->mi_type->mt_addr_length);
1072 
1073 	i_mac_perim_exit(mip);
1074 
1075 	if (err == 0)
1076 		i_mac_notify(mip, MAC_NOTE_UNICST);
1077 
1078 	return (err);
1079 }
1080 
1081 /*
1082  * Return the current primary MAC address of the specified MAC.
1083  */
1084 void
1085 mac_unicast_primary_get(mac_handle_t mh, uint8_t *addr)
1086 {
1087 	mac_impl_t *mip = (mac_impl_t *)mh;
1088 
1089 	rw_enter(&mip->mi_rw_lock, RW_READER);
1090 	bcopy(mip->mi_addr, addr, mip->mi_type->mt_addr_length);
1091 	rw_exit(&mip->mi_rw_lock);
1092 }
1093 
1094 /*
1095  * Return the secondary MAC address for the specified handle
1096  */
1097 void
1098 mac_unicast_secondary_get(mac_client_handle_t mh, uint8_t *addr)
1099 {
1100 	mac_client_impl_t *mcip = (mac_client_impl_t *)mh;
1101 
1102 	ASSERT(mcip->mci_unicast != NULL);
1103 	bcopy(mcip->mci_unicast->ma_addr, addr, mcip->mci_unicast->ma_len);
1104 }
1105 
1106 /*
1107  * Return information about the use of the primary MAC address of the
1108  * specified MAC instance:
1109  *
1110  * - if client_name is non-NULL, it must point to a string of at
1111  *   least MAXNAMELEN bytes, and will be set to the name of the MAC
1112  *   client which uses the primary MAC address.
1113  *
1114  * - if in_use is non-NULL, used to return whether the primary MAC
1115  *   address is currently in use.
1116  */
1117 void
1118 mac_unicast_primary_info(mac_handle_t mh, char *client_name, boolean_t *in_use)
1119 {
1120 	mac_impl_t *mip = (mac_impl_t *)mh;
1121 	mac_client_impl_t *cur_client;
1122 
1123 	if (in_use != NULL)
1124 		*in_use = B_FALSE;
1125 	if (client_name != NULL)
1126 		bzero(client_name, MAXNAMELEN);
1127 
1128 	/*
1129 	 * The mi_rw_lock is used to protect threads that don't hold the
1130 	 * mac perimeter to get a consistent view of the mi_clients_list.
1131 	 * Threads that modify the list must hold both the mac perimeter and
1132 	 * mi_rw_lock(RW_WRITER)
1133 	 */
1134 	rw_enter(&mip->mi_rw_lock, RW_READER);
1135 	for (cur_client = mip->mi_clients_list; cur_client != NULL;
1136 	    cur_client = cur_client->mci_client_next) {
1137 		if (mac_is_primary_client(cur_client) ||
1138 		    (mip->mi_state_flags & MIS_IS_VNIC)) {
1139 			rw_exit(&mip->mi_rw_lock);
1140 			if (in_use != NULL)
1141 				*in_use = B_TRUE;
1142 			if (client_name != NULL) {
1143 				bcopy(cur_client->mci_name, client_name,
1144 				    MAXNAMELEN);
1145 			}
1146 			return;
1147 		}
1148 	}
1149 	rw_exit(&mip->mi_rw_lock);
1150 }
1151 
1152 /*
1153  * Return the current destination MAC address of the specified MAC.
1154  */
1155 boolean_t
1156 mac_dst_get(mac_handle_t mh, uint8_t *addr)
1157 {
1158 	mac_impl_t *mip = (mac_impl_t *)mh;
1159 
1160 	rw_enter(&mip->mi_rw_lock, RW_READER);
1161 	if (mip->mi_dstaddr_set)
1162 		bcopy(mip->mi_dstaddr, addr, mip->mi_type->mt_addr_length);
1163 	rw_exit(&mip->mi_rw_lock);
1164 	return (mip->mi_dstaddr_set);
1165 }
1166 
1167 /*
1168  * Add the specified MAC client to the list of clients which opened
1169  * the specified MAC.
1170  */
1171 static void
1172 mac_client_add(mac_client_impl_t *mcip)
1173 {
1174 	mac_impl_t *mip = mcip->mci_mip;
1175 
1176 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
1177 
1178 	/* add VNIC to the front of the list */
1179 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
1180 	mcip->mci_client_next = mip->mi_clients_list;
1181 	mip->mi_clients_list = mcip;
1182 	mip->mi_nclients++;
1183 	rw_exit(&mip->mi_rw_lock);
1184 }
1185 
1186 /*
1187  * Remove the specified MAC client from the list of clients which opened
1188  * the specified MAC.
1189  */
1190 static void
1191 mac_client_remove(mac_client_impl_t *mcip)
1192 {
1193 	mac_impl_t *mip = mcip->mci_mip;
1194 	mac_client_impl_t **prev, *cclient;
1195 
1196 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
1197 
1198 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
1199 	prev = &mip->mi_clients_list;
1200 	cclient = *prev;
1201 	while (cclient != NULL && cclient != mcip) {
1202 		prev = &cclient->mci_client_next;
1203 		cclient = *prev;
1204 	}
1205 	ASSERT(cclient != NULL);
1206 	*prev = cclient->mci_client_next;
1207 	mip->mi_nclients--;
1208 	rw_exit(&mip->mi_rw_lock);
1209 }
1210 
1211 static mac_unicast_impl_t *
1212 mac_client_find_vid(mac_client_impl_t *mcip, uint16_t vid)
1213 {
1214 	mac_unicast_impl_t *muip = mcip->mci_unicast_list;
1215 
1216 	while ((muip != NULL) && (muip->mui_vid != vid))
1217 		muip = muip->mui_next;
1218 
1219 	return (muip);
1220 }
1221 
1222 /*
1223  * Return whether the specified (MAC address, VID) tuple is already used by
1224  * one of the MAC clients associated with the specified MAC.
1225  */
1226 static boolean_t
1227 mac_addr_in_use(mac_impl_t *mip, uint8_t *mac_addr, uint16_t vid)
1228 {
1229 	mac_client_impl_t *client;
1230 	mac_address_t *map;
1231 
1232 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
1233 
1234 	for (client = mip->mi_clients_list; client != NULL;
1235 	    client = client->mci_client_next) {
1236 
1237 		/*
1238 		 * Ignore clients that don't have unicast address.
1239 		 */
1240 		if (client->mci_unicast_list == NULL)
1241 			continue;
1242 
1243 		map = client->mci_unicast;
1244 
1245 		if ((bcmp(mac_addr, map->ma_addr, map->ma_len) == 0) &&
1246 		    (mac_client_find_vid(client, vid) != NULL)) {
1247 			return (B_TRUE);
1248 		}
1249 	}
1250 
1251 	return (B_FALSE);
1252 }
1253 
1254 /*
1255  * Generate a random MAC address. The MAC address prefix is
1256  * stored in the array pointed to by mac_addr, and its length, in bytes,
1257  * is specified by prefix_len. The least significant bits
1258  * after prefix_len bytes are generated, and stored after the prefix
1259  * in the mac_addr array.
1260  */
1261 int
1262 mac_addr_random(mac_client_handle_t mch, uint_t prefix_len,
1263     uint8_t *mac_addr, mac_diag_t *diag)
1264 {
1265 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
1266 	mac_impl_t *mip = mcip->mci_mip;
1267 	size_t addr_len = mip->mi_type->mt_addr_length;
1268 
1269 	if (prefix_len >= addr_len) {
1270 		*diag = MAC_DIAG_MACPREFIXLEN_INVALID;
1271 		return (EINVAL);
1272 	}
1273 
1274 	/* check the prefix value */
1275 	if (prefix_len > 0) {
1276 		bzero(mac_addr + prefix_len, addr_len - prefix_len);
1277 		if (!mac_unicst_verify((mac_handle_t)mip, mac_addr,
1278 		    addr_len)) {
1279 			*diag = MAC_DIAG_MACPREFIX_INVALID;
1280 			return (EINVAL);
1281 		}
1282 	}
1283 
1284 	/* generate the MAC address */
1285 	if (prefix_len < addr_len) {
1286 		(void) random_get_pseudo_bytes(mac_addr +
1287 		    prefix_len, addr_len - prefix_len);
1288 	}
1289 
1290 	*diag = MAC_DIAG_NONE;
1291 	return (0);
1292 }
1293 
1294 /*
1295  * Set the priority range for this MAC client. This will be used to
1296  * determine the absolute priority for the threads created for this
1297  * MAC client using the specified "low", "medium" and "high" level.
1298  * This will also be used for any subflows on this MAC client.
1299  */
1300 #define	MAC_CLIENT_SET_PRIORITY_RANGE(mcip, pri) {			\
1301 	(mcip)->mci_min_pri = FLOW_MIN_PRIORITY(MINCLSYSPRI,	\
1302 	    MAXCLSYSPRI, (pri));					\
1303 	(mcip)->mci_max_pri = FLOW_MAX_PRIORITY(MINCLSYSPRI,	\
1304 	    MAXCLSYSPRI, (mcip)->mci_min_pri);				\
1305 	}
1306 
1307 /*
1308  * MAC client open entry point. Return a new MAC client handle. Each
1309  * MAC client is associated with a name, specified through the 'name'
1310  * argument.
1311  */
1312 int
1313 mac_client_open(mac_handle_t mh, mac_client_handle_t *mchp, char *name,
1314     uint16_t flags)
1315 {
1316 	mac_impl_t		*mip = (mac_impl_t *)mh;
1317 	mac_client_impl_t	*mcip;
1318 	int			err = 0;
1319 	boolean_t		share_desired;
1320 	flow_entry_t		*flent = NULL;
1321 
1322 	share_desired = (flags & MAC_OPEN_FLAGS_SHARES_DESIRED) != 0;
1323 	*mchp = NULL;
1324 
1325 	i_mac_perim_enter(mip);
1326 
1327 	if (mip->mi_state_flags & MIS_IS_VNIC) {
1328 		/*
1329 		 * The underlying MAC is a VNIC. Return the MAC client
1330 		 * handle of the lower MAC which was obtained by
1331 		 * the VNIC driver when it did its mac_client_open().
1332 		 */
1333 
1334 		mcip = mac_vnic_lower(mip);
1335 
1336 		/*
1337 		 * Note that multiple mac clients share the same mcip in
1338 		 * this case.
1339 		 */
1340 		if (flags & MAC_OPEN_FLAGS_EXCLUSIVE)
1341 			mcip->mci_state_flags |= MCIS_EXCLUSIVE;
1342 
1343 		if (flags & MAC_OPEN_FLAGS_MULTI_PRIMARY)
1344 			mcip->mci_flags |= MAC_CLIENT_FLAGS_MULTI_PRIMARY;
1345 
1346 		mip->mi_clients_list = mcip;
1347 		i_mac_perim_exit(mip);
1348 		*mchp = (mac_client_handle_t)mcip;
1349 
1350 		DTRACE_PROBE2(mac__client__open__nonallocated, mac_impl_t *,
1351 		    mcip->mci_mip, mac_client_impl_t *, mcip);
1352 
1353 		return (err);
1354 	}
1355 
1356 	mcip = kmem_cache_alloc(mac_client_impl_cache, KM_SLEEP);
1357 
1358 	mcip->mci_mip = mip;
1359 	mcip->mci_upper_mip = NULL;
1360 	mcip->mci_rx_fn = mac_pkt_drop;
1361 	mcip->mci_rx_arg = NULL;
1362 	mcip->mci_rx_p_fn = NULL;
1363 	mcip->mci_rx_p_arg = NULL;
1364 	mcip->mci_p_unicast_list = NULL;
1365 	mcip->mci_direct_rx_fn = NULL;
1366 	mcip->mci_direct_rx_arg = NULL;
1367 	mcip->mci_vidcache = MCIP_VIDCACHE_INVALID;
1368 
1369 	mcip->mci_unicast_list = NULL;
1370 
1371 	if ((flags & MAC_OPEN_FLAGS_IS_VNIC) != 0)
1372 		mcip->mci_state_flags |= MCIS_IS_VNIC;
1373 
1374 	if ((flags & MAC_OPEN_FLAGS_EXCLUSIVE) != 0)
1375 		mcip->mci_state_flags |= MCIS_EXCLUSIVE;
1376 
1377 	if ((flags & MAC_OPEN_FLAGS_IS_AGGR_PORT) != 0)
1378 		mcip->mci_state_flags |= MCIS_IS_AGGR_PORT;
1379 
1380 	if (mip->mi_state_flags & MIS_IS_AGGR)
1381 		mcip->mci_state_flags |= MCIS_IS_AGGR_CLIENT;
1382 
1383 	if ((flags & MAC_OPEN_FLAGS_USE_DATALINK_NAME) != 0) {
1384 		datalink_id_t	linkid;
1385 
1386 		ASSERT(name == NULL);
1387 		if ((err = dls_devnet_macname2linkid(mip->mi_name,
1388 		    &linkid)) != 0) {
1389 			goto done;
1390 		}
1391 		if ((err = dls_mgmt_get_linkinfo(linkid, mcip->mci_name, NULL,
1392 		    NULL, NULL)) != 0) {
1393 			/*
1394 			 * Use mac name if dlmgmtd is not available.
1395 			 */
1396 			if (err == EBADF) {
1397 				(void) strlcpy(mcip->mci_name, mip->mi_name,
1398 				    sizeof (mcip->mci_name));
1399 				err = 0;
1400 			} else {
1401 				goto done;
1402 			}
1403 		}
1404 		mcip->mci_state_flags |= MCIS_USE_DATALINK_NAME;
1405 	} else {
1406 		ASSERT(name != NULL);
1407 		if (strlen(name) > MAXNAMELEN) {
1408 			err = EINVAL;
1409 			goto done;
1410 		}
1411 		(void) strlcpy(mcip->mci_name, name, sizeof (mcip->mci_name));
1412 	}
1413 
1414 	if (flags & MAC_OPEN_FLAGS_MULTI_PRIMARY)
1415 		mcip->mci_flags |= MAC_CLIENT_FLAGS_MULTI_PRIMARY;
1416 
1417 	if (flags & MAC_OPEN_FLAGS_NO_UNICAST_ADDR)
1418 		mcip->mci_state_flags |= MCIS_NO_UNICAST_ADDR;
1419 
1420 	mac_protect_init(mcip);
1421 
1422 	/* the subflow table will be created dynamically */
1423 	mcip->mci_subflow_tab = NULL;
1424 
1425 	mcip->mci_misc_stat.mms_multircv = 0;
1426 	mcip->mci_misc_stat.mms_brdcstrcv = 0;
1427 	mcip->mci_misc_stat.mms_multixmt = 0;
1428 	mcip->mci_misc_stat.mms_brdcstxmt = 0;
1429 
1430 	/* Create an initial flow */
1431 
1432 	err = mac_flow_create(NULL, NULL, mcip->mci_name, NULL,
1433 	    mcip->mci_state_flags & MCIS_IS_VNIC ? FLOW_VNIC_MAC :
1434 	    FLOW_PRIMARY_MAC, &flent);
1435 	if (err != 0)
1436 		goto done;
1437 	mcip->mci_flent = flent;
1438 	FLOW_MARK(flent, FE_MC_NO_DATAPATH);
1439 	flent->fe_mcip = mcip;
1440 
1441 	/*
1442 	 * Place initial creation reference on the flow. This reference
1443 	 * is released in the corresponding delete action viz.
1444 	 * mac_unicast_remove after waiting for all transient refs to
1445 	 * to go away. The wait happens in mac_flow_wait.
1446 	 */
1447 	FLOW_REFHOLD(flent);
1448 
1449 	/*
1450 	 * Do this ahead of the mac_bcast_add() below so that the mi_nclients
1451 	 * will have the right value for mac_rx_srs_setup().
1452 	 */
1453 	mac_client_add(mcip);
1454 
1455 	mcip->mci_share = 0;
1456 	if (share_desired)
1457 		i_mac_share_alloc(mcip);
1458 
1459 	/*
1460 	 * We will do mimimal datapath setup to allow a MAC client to
1461 	 * transmit or receive non-unicast packets without waiting
1462 	 * for mac_unicast_add.
1463 	 */
1464 	if (mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR) {
1465 		if ((err = mac_client_datapath_setup(mcip, VLAN_ID_NONE,
1466 		    NULL, NULL, B_TRUE, NULL)) != 0) {
1467 			goto done;
1468 		}
1469 	}
1470 
1471 	DTRACE_PROBE2(mac__client__open__allocated, mac_impl_t *,
1472 	    mcip->mci_mip, mac_client_impl_t *, mcip);
1473 
1474 	*mchp = (mac_client_handle_t)mcip;
1475 	i_mac_perim_exit(mip);
1476 	return (0);
1477 
1478 done:
1479 	i_mac_perim_exit(mip);
1480 	mcip->mci_state_flags = 0;
1481 	mcip->mci_tx_flag = 0;
1482 	kmem_cache_free(mac_client_impl_cache, mcip);
1483 	return (err);
1484 }
1485 
1486 /*
1487  * Close the specified MAC client handle.
1488  */
1489 void
1490 mac_client_close(mac_client_handle_t mch, uint16_t flags)
1491 {
1492 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
1493 	mac_impl_t		*mip = mcip->mci_mip;
1494 	flow_entry_t		*flent;
1495 
1496 	i_mac_perim_enter(mip);
1497 
1498 	if (flags & MAC_CLOSE_FLAGS_EXCLUSIVE)
1499 		mcip->mci_state_flags &= ~MCIS_EXCLUSIVE;
1500 
1501 	if ((mcip->mci_state_flags & MCIS_IS_VNIC) &&
1502 	    !(flags & MAC_CLOSE_FLAGS_IS_VNIC)) {
1503 		/*
1504 		 * This is an upper VNIC client initiated operation.
1505 		 * The lower MAC client will be closed by the VNIC driver
1506 		 * when the VNIC is deleted.
1507 		 */
1508 
1509 		i_mac_perim_exit(mip);
1510 		return;
1511 	}
1512 
1513 	/* If we have only setup up minimal datapth setup, tear it down */
1514 	if (mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR) {
1515 		mac_client_datapath_teardown((mac_client_handle_t)mcip, NULL,
1516 		    mcip->mci_flent);
1517 		mcip->mci_state_flags &= ~MCIS_NO_UNICAST_ADDR;
1518 	}
1519 
1520 	/*
1521 	 * Remove the flent associated with the MAC client
1522 	 */
1523 	flent = mcip->mci_flent;
1524 	mcip->mci_flent = NULL;
1525 	FLOW_FINAL_REFRELE(flent);
1526 
1527 	/*
1528 	 * MAC clients must remove the unicast addresses and promisc callbacks
1529 	 * they added before issuing a mac_client_close().
1530 	 */
1531 	ASSERT(mcip->mci_unicast_list == NULL);
1532 	ASSERT(mcip->mci_promisc_list == NULL);
1533 	ASSERT(mcip->mci_tx_notify_cb_list == NULL);
1534 
1535 	i_mac_share_free(mcip);
1536 	mac_protect_fini(mcip);
1537 	mac_client_remove(mcip);
1538 
1539 	i_mac_perim_exit(mip);
1540 	mcip->mci_subflow_tab = NULL;
1541 	mcip->mci_state_flags = 0;
1542 	mcip->mci_tx_flag = 0;
1543 	kmem_cache_free(mac_client_impl_cache, mch);
1544 }
1545 
1546 /*
1547  * Set the Rx bypass receive callback and return B_TRUE. Return
1548  * B_FALSE if it's not possible to enable bypass.
1549  */
1550 boolean_t
1551 mac_rx_bypass_set(mac_client_handle_t mch, mac_direct_rx_t rx_fn, void *arg1)
1552 {
1553 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
1554 	mac_impl_t		*mip = mcip->mci_mip;
1555 
1556 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
1557 
1558 	/*
1559 	 * If the client has more than one VLAN then process packets
1560 	 * through DLS. This should happen only when sun4v vsw is on
1561 	 * the scene.
1562 	 */
1563 	if (mcip->mci_nvids > 1)
1564 		return (B_FALSE);
1565 
1566 	/*
1567 	 * These are not accessed directly in the data path, and hence
1568 	 * don't need any protection
1569 	 */
1570 	mcip->mci_direct_rx_fn = rx_fn;
1571 	mcip->mci_direct_rx_arg = arg1;
1572 	return (B_TRUE);
1573 }
1574 
1575 /*
1576  * Enable/Disable rx bypass. By default, bypass is assumed to be enabled.
1577  */
1578 void
1579 mac_rx_bypass_enable(mac_client_handle_t mch)
1580 {
1581 	((mac_client_impl_t *)mch)->mci_state_flags &= ~MCIS_RX_BYPASS_DISABLE;
1582 }
1583 
1584 void
1585 mac_rx_bypass_disable(mac_client_handle_t mch)
1586 {
1587 	((mac_client_impl_t *)mch)->mci_state_flags |= MCIS_RX_BYPASS_DISABLE;
1588 }
1589 
1590 /*
1591  * Set the receive callback for the specified MAC client. There can be
1592  * at most one such callback per MAC client.
1593  */
1594 void
1595 mac_rx_set(mac_client_handle_t mch, mac_rx_t rx_fn, void *arg)
1596 {
1597 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
1598 	mac_impl_t	*mip = mcip->mci_mip;
1599 	mac_impl_t	*umip = mcip->mci_upper_mip;
1600 
1601 	/*
1602 	 * Instead of adding an extra set of locks and refcnts in
1603 	 * the datapath at the mac client boundary, we temporarily quiesce
1604 	 * the SRS and related entities. We then change the receive function
1605 	 * without interference from any receive data thread and then reenable
1606 	 * the data flow subsequently.
1607 	 */
1608 	i_mac_perim_enter(mip);
1609 	mac_rx_client_quiesce(mch);
1610 
1611 	mcip->mci_rx_fn = rx_fn;
1612 	mcip->mci_rx_arg = arg;
1613 	mac_rx_client_restart(mch);
1614 	i_mac_perim_exit(mip);
1615 
1616 	/*
1617 	 * If we're changing the Rx function on the primary MAC of a VNIC,
1618 	 * make sure any secondary addresses on the VNIC are updated as well.
1619 	 */
1620 	if (umip != NULL) {
1621 		ASSERT((umip->mi_state_flags & MIS_IS_VNIC) != 0);
1622 		mac_vnic_secondary_update(umip);
1623 	}
1624 }
1625 
1626 /*
1627  * Reset the receive callback for the specified MAC client.
1628  */
1629 void
1630 mac_rx_clear(mac_client_handle_t mch)
1631 {
1632 	mac_rx_set(mch, mac_pkt_drop, NULL);
1633 }
1634 
1635 void
1636 mac_rx_barrier(mac_client_handle_t mch)
1637 {
1638 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
1639 	mac_impl_t *mip = mcip->mci_mip;
1640 
1641 	i_mac_perim_enter(mip);
1642 
1643 	/* If a RX callback is set, quiesce and restart that datapath */
1644 	if (mcip->mci_rx_fn != mac_pkt_drop) {
1645 		mac_rx_client_quiesce(mch);
1646 		mac_rx_client_restart(mch);
1647 	}
1648 
1649 	/* If any promisc callbacks are registered, perform a barrier there */
1650 	if (mcip->mci_promisc_list != NULL || mip->mi_promisc_list != NULL) {
1651 		mac_cb_info_t *mcbi =  &mip->mi_promisc_cb_info;
1652 
1653 		mutex_enter(mcbi->mcbi_lockp);
1654 		mac_callback_barrier(mcbi);
1655 		mutex_exit(mcbi->mcbi_lockp);
1656 	}
1657 
1658 	i_mac_perim_exit(mip);
1659 }
1660 
1661 void
1662 mac_secondary_dup(mac_client_handle_t smch, mac_client_handle_t dmch)
1663 {
1664 	mac_client_impl_t *smcip = (mac_client_impl_t *)smch;
1665 	mac_client_impl_t *dmcip = (mac_client_impl_t *)dmch;
1666 	flow_entry_t *flent = dmcip->mci_flent;
1667 
1668 	/* This should only be called to setup secondary macs */
1669 	ASSERT((flent->fe_type & FLOW_PRIMARY_MAC) == 0);
1670 
1671 	mac_rx_set(dmch, smcip->mci_rx_fn, smcip->mci_rx_arg);
1672 	dmcip->mci_promisc_list = smcip->mci_promisc_list;
1673 
1674 	/*
1675 	 * Duplicate the primary mac resources to the secondary.
1676 	 * Since we already validated the resource controls when setting
1677 	 * them on the primary, we can ignore errors here.
1678 	 */
1679 	(void) mac_resource_ctl_set(dmch, MCIP_RESOURCE_PROPS(smcip));
1680 }
1681 
1682 /*
1683  * Called when removing a secondary MAC. Currently only clears the promisc_list
1684  * since we share the primary mac's promisc_list.
1685  */
1686 void
1687 mac_secondary_cleanup(mac_client_handle_t mch)
1688 {
1689 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
1690 	flow_entry_t *flent = mcip->mci_flent;
1691 
1692 	/* This should only be called for secondary macs */
1693 	ASSERT((flent->fe_type & FLOW_PRIMARY_MAC) == 0);
1694 	mcip->mci_promisc_list = NULL;
1695 }
1696 
1697 /*
1698  * Walk the MAC client subflow table and updates their priority values.
1699  */
1700 static int
1701 mac_update_subflow_priority_cb(flow_entry_t *flent, void *arg)
1702 {
1703 	mac_flow_update_priority(arg, flent);
1704 	return (0);
1705 }
1706 
1707 void
1708 mac_update_subflow_priority(mac_client_impl_t *mcip)
1709 {
1710 	(void) mac_flow_walk(mcip->mci_subflow_tab,
1711 	    mac_update_subflow_priority_cb, mcip);
1712 }
1713 
1714 /*
1715  * Modify the TX or RX ring properties. We could either just move around
1716  * rings, i.e add/remove rings given to a client. Or this might cause the
1717  * client to move from hardware based to software or the other way around.
1718  * If we want to reset this property, then we clear the mask, additionally
1719  * if the client was given a non-default group we remove all rings except
1720  * for 1 and give it back to the default group.
1721  */
1722 int
1723 mac_client_set_rings_prop(mac_client_impl_t *mcip, mac_resource_props_t *mrp,
1724     mac_resource_props_t *tmrp)
1725 {
1726 	mac_impl_t		*mip = mcip->mci_mip;
1727 	flow_entry_t		*flent = mcip->mci_flent;
1728 	uint8_t			*mac_addr;
1729 	int			err = 0;
1730 	mac_group_t		*defgrp;
1731 	mac_group_t		*group;
1732 	mac_group_t		*ngrp;
1733 	mac_resource_props_t	*cmrp = MCIP_RESOURCE_PROPS(mcip);
1734 	uint_t			ringcnt;
1735 	boolean_t		unspec;
1736 
1737 	if (mcip->mci_share != 0)
1738 		return (EINVAL);
1739 
1740 	if (mrp->mrp_mask & MRP_RX_RINGS) {
1741 		unspec = mrp->mrp_mask & MRP_RXRINGS_UNSPEC;
1742 		group = flent->fe_rx_ring_group;
1743 		defgrp = MAC_DEFAULT_RX_GROUP(mip);
1744 		mac_addr = flent->fe_flow_desc.fd_dst_mac;
1745 
1746 		/*
1747 		 * No resulting change. If we are resetting on a client on
1748 		 * which there was no rx rings property. For dynamic group
1749 		 * if we are setting the same number of rings already set.
1750 		 * For static group if we are requesting a group again.
1751 		 */
1752 		if (mrp->mrp_mask & MRP_RINGS_RESET) {
1753 			if (!(tmrp->mrp_mask & MRP_RX_RINGS))
1754 				return (0);
1755 		} else {
1756 			if (unspec) {
1757 				if (tmrp->mrp_mask & MRP_RXRINGS_UNSPEC)
1758 					return (0);
1759 			} else if (mip->mi_rx_group_type ==
1760 			    MAC_GROUP_TYPE_DYNAMIC) {
1761 				if ((tmrp->mrp_mask & MRP_RX_RINGS) &&
1762 				    !(tmrp->mrp_mask & MRP_RXRINGS_UNSPEC) &&
1763 				    mrp->mrp_nrxrings == tmrp->mrp_nrxrings) {
1764 					return (0);
1765 				}
1766 			}
1767 		}
1768 		/* Resetting the prop */
1769 		if (mrp->mrp_mask & MRP_RINGS_RESET) {
1770 			/*
1771 			 * We will just keep one ring and give others back if
1772 			 * we are not the primary. For the primary we give
1773 			 * all the rings in the default group except the
1774 			 * default ring. If it is a static group, then
1775 			 * we don't do anything, but clear the MRP_RX_RINGS
1776 			 * flag.
1777 			 */
1778 			if (group != defgrp) {
1779 				if (mip->mi_rx_group_type ==
1780 				    MAC_GROUP_TYPE_DYNAMIC) {
1781 					/*
1782 					 * This group has reserved rings
1783 					 * that need to be released now,
1784 					 * so does the group.
1785 					 */
1786 					MAC_RX_RING_RELEASED(mip,
1787 					    group->mrg_cur_count);
1788 					MAC_RX_GRP_RELEASED(mip);
1789 					if ((flent->fe_type &
1790 					    FLOW_PRIMARY_MAC) != 0) {
1791 						if (mip->mi_nactiveclients ==
1792 						    1) {
1793 							(void)
1794 							    mac_rx_switch_group(
1795 							    mcip, group,
1796 							    defgrp);
1797 							return (0);
1798 						} else {
1799 							cmrp->mrp_nrxrings =
1800 							    group->
1801 							    mrg_cur_count +
1802 							    defgrp->
1803 							    mrg_cur_count - 1;
1804 						}
1805 					} else {
1806 						cmrp->mrp_nrxrings = 1;
1807 					}
1808 					(void) mac_group_ring_modify(mcip,
1809 					    group, defgrp);
1810 				} else {
1811 					/*
1812 					 * If this is a static group, we
1813 					 * need to release the group. The
1814 					 * client will remain in the same
1815 					 * group till some other client
1816 					 * needs this group.
1817 					 */
1818 					MAC_RX_GRP_RELEASED(mip);
1819 				}
1820 			/* Let check if we can give this an excl group */
1821 			} else if (group == defgrp) {
1822 				/*
1823 				 * If multiple clients share an
1824 				 * address then they must stay on the
1825 				 * default group.
1826 				 */
1827 				if (mac_check_macaddr_shared(mcip->mci_unicast))
1828 					return (0);
1829 
1830 				ngrp = mac_reserve_rx_group(mcip, mac_addr,
1831 				    B_TRUE);
1832 				/* Couldn't give it a group, that's fine */
1833 				if (ngrp == NULL)
1834 					return (0);
1835 				/* Switch to H/W */
1836 				if (mac_rx_switch_group(mcip, defgrp, ngrp) !=
1837 				    0) {
1838 					mac_stop_group(ngrp);
1839 					return (0);
1840 				}
1841 			}
1842 			/*
1843 			 * If the client is in the default group, we will
1844 			 * just clear the MRP_RX_RINGS and leave it as
1845 			 * it rather than look for an exclusive group
1846 			 * for it.
1847 			 */
1848 			return (0);
1849 		}
1850 
1851 		if (group == defgrp && ((mrp->mrp_nrxrings > 0) || unspec)) {
1852 			/*
1853 			 * We are requesting Rx rings. Try to reserve
1854 			 * a non-default group.
1855 			 *
1856 			 * If multiple clients share an address then
1857 			 * they must stay on the default group.
1858 			 */
1859 			if (mac_check_macaddr_shared(mcip->mci_unicast))
1860 				return (EINVAL);
1861 
1862 			ngrp = mac_reserve_rx_group(mcip, mac_addr, B_TRUE);
1863 			if (ngrp == NULL)
1864 				return (ENOSPC);
1865 
1866 			/* Switch to H/W */
1867 			if (mac_rx_switch_group(mcip, defgrp, ngrp) != 0) {
1868 				mac_release_rx_group(mcip, ngrp);
1869 				return (ENOSPC);
1870 			}
1871 			MAC_RX_GRP_RESERVED(mip);
1872 			if (mip->mi_rx_group_type == MAC_GROUP_TYPE_DYNAMIC)
1873 				MAC_RX_RING_RESERVED(mip, ngrp->mrg_cur_count);
1874 		} else if (group != defgrp && !unspec &&
1875 		    mrp->mrp_nrxrings == 0) {
1876 			/* Switch to S/W */
1877 			ringcnt = group->mrg_cur_count;
1878 			if (mac_rx_switch_group(mcip, group, defgrp) != 0)
1879 				return (ENOSPC);
1880 			if (tmrp->mrp_mask & MRP_RX_RINGS) {
1881 				MAC_RX_GRP_RELEASED(mip);
1882 				if (mip->mi_rx_group_type ==
1883 				    MAC_GROUP_TYPE_DYNAMIC) {
1884 					MAC_RX_RING_RELEASED(mip, ringcnt);
1885 				}
1886 			}
1887 		} else if (group != defgrp && mip->mi_rx_group_type ==
1888 		    MAC_GROUP_TYPE_DYNAMIC) {
1889 			ringcnt = group->mrg_cur_count;
1890 			err = mac_group_ring_modify(mcip, group, defgrp);
1891 			if (err != 0)
1892 				return (err);
1893 			/*
1894 			 * Update the accounting. If this group
1895 			 * already had explicitly reserved rings,
1896 			 * we need to update the rings based on
1897 			 * the new ring count. If this group
1898 			 * had not explicitly reserved rings,
1899 			 * then we just reserve the rings asked for
1900 			 * and reserve the group.
1901 			 */
1902 			if (tmrp->mrp_mask & MRP_RX_RINGS) {
1903 				if (ringcnt > group->mrg_cur_count) {
1904 					MAC_RX_RING_RELEASED(mip,
1905 					    ringcnt - group->mrg_cur_count);
1906 				} else {
1907 					MAC_RX_RING_RESERVED(mip,
1908 					    group->mrg_cur_count - ringcnt);
1909 				}
1910 			} else {
1911 				MAC_RX_RING_RESERVED(mip, group->mrg_cur_count);
1912 				MAC_RX_GRP_RESERVED(mip);
1913 			}
1914 		}
1915 	}
1916 	if (mrp->mrp_mask & MRP_TX_RINGS) {
1917 		unspec = mrp->mrp_mask & MRP_TXRINGS_UNSPEC;
1918 		group = flent->fe_tx_ring_group;
1919 		defgrp = MAC_DEFAULT_TX_GROUP(mip);
1920 
1921 		/*
1922 		 * For static groups we only allow rings=0 or resetting the
1923 		 * rings property.
1924 		 */
1925 		if (mrp->mrp_ntxrings > 0 &&
1926 		    mip->mi_tx_group_type != MAC_GROUP_TYPE_DYNAMIC) {
1927 			return (ENOTSUP);
1928 		}
1929 		if (mrp->mrp_mask & MRP_RINGS_RESET) {
1930 			if (!(tmrp->mrp_mask & MRP_TX_RINGS))
1931 				return (0);
1932 		} else {
1933 			if (unspec) {
1934 				if (tmrp->mrp_mask & MRP_TXRINGS_UNSPEC)
1935 					return (0);
1936 			} else if (mip->mi_tx_group_type ==
1937 			    MAC_GROUP_TYPE_DYNAMIC) {
1938 				if ((tmrp->mrp_mask & MRP_TX_RINGS) &&
1939 				    !(tmrp->mrp_mask & MRP_TXRINGS_UNSPEC) &&
1940 				    mrp->mrp_ntxrings == tmrp->mrp_ntxrings) {
1941 					return (0);
1942 				}
1943 			}
1944 		}
1945 		/* Resetting the prop */
1946 		if (mrp->mrp_mask & MRP_RINGS_RESET) {
1947 			if (group != defgrp) {
1948 				if (mip->mi_tx_group_type ==
1949 				    MAC_GROUP_TYPE_DYNAMIC) {
1950 					ringcnt = group->mrg_cur_count;
1951 					if ((flent->fe_type &
1952 					    FLOW_PRIMARY_MAC) != 0) {
1953 						mac_tx_client_quiesce(
1954 						    (mac_client_handle_t)
1955 						    mcip);
1956 						mac_tx_switch_group(mcip,
1957 						    group, defgrp);
1958 						mac_tx_client_restart(
1959 						    (mac_client_handle_t)
1960 						    mcip);
1961 						MAC_TX_GRP_RELEASED(mip);
1962 						MAC_TX_RING_RELEASED(mip,
1963 						    ringcnt);
1964 						return (0);
1965 					}
1966 					cmrp->mrp_ntxrings = 1;
1967 					(void) mac_group_ring_modify(mcip,
1968 					    group, defgrp);
1969 					/*
1970 					 * This group has reserved rings
1971 					 * that need to be released now.
1972 					 */
1973 					MAC_TX_RING_RELEASED(mip, ringcnt);
1974 				}
1975 				/*
1976 				 * If this is a static group, we
1977 				 * need to release the group. The
1978 				 * client will remain in the same
1979 				 * group till some other client
1980 				 * needs this group.
1981 				 */
1982 				MAC_TX_GRP_RELEASED(mip);
1983 			} else if (group == defgrp &&
1984 			    (flent->fe_type & FLOW_PRIMARY_MAC) == 0) {
1985 				ngrp = mac_reserve_tx_group(mcip, B_TRUE);
1986 				if (ngrp == NULL)
1987 					return (0);
1988 				mac_tx_client_quiesce(
1989 				    (mac_client_handle_t)mcip);
1990 				mac_tx_switch_group(mcip, defgrp, ngrp);
1991 				mac_tx_client_restart(
1992 				    (mac_client_handle_t)mcip);
1993 			}
1994 			/*
1995 			 * If the client is in the default group, we will
1996 			 * just clear the MRP_TX_RINGS and leave it as
1997 			 * it rather than look for an exclusive group
1998 			 * for it.
1999 			 */
2000 			return (0);
2001 		}
2002 
2003 		/* Switch to H/W */
2004 		if (group == defgrp && ((mrp->mrp_ntxrings > 0) || unspec)) {
2005 			ngrp = mac_reserve_tx_group(mcip, B_TRUE);
2006 			if (ngrp == NULL)
2007 				return (ENOSPC);
2008 			mac_tx_client_quiesce((mac_client_handle_t)mcip);
2009 			mac_tx_switch_group(mcip, defgrp, ngrp);
2010 			mac_tx_client_restart((mac_client_handle_t)mcip);
2011 			MAC_TX_GRP_RESERVED(mip);
2012 			if (mip->mi_tx_group_type == MAC_GROUP_TYPE_DYNAMIC)
2013 				MAC_TX_RING_RESERVED(mip, ngrp->mrg_cur_count);
2014 		/* Switch to S/W */
2015 		} else if (group != defgrp && !unspec &&
2016 		    mrp->mrp_ntxrings == 0) {
2017 			/* Switch to S/W */
2018 			ringcnt = group->mrg_cur_count;
2019 			mac_tx_client_quiesce((mac_client_handle_t)mcip);
2020 			mac_tx_switch_group(mcip, group, defgrp);
2021 			mac_tx_client_restart((mac_client_handle_t)mcip);
2022 			if (tmrp->mrp_mask & MRP_TX_RINGS) {
2023 				MAC_TX_GRP_RELEASED(mip);
2024 				if (mip->mi_tx_group_type ==
2025 				    MAC_GROUP_TYPE_DYNAMIC) {
2026 					MAC_TX_RING_RELEASED(mip, ringcnt);
2027 				}
2028 			}
2029 		} else if (group != defgrp && mip->mi_tx_group_type ==
2030 		    MAC_GROUP_TYPE_DYNAMIC) {
2031 			ringcnt = group->mrg_cur_count;
2032 			err = mac_group_ring_modify(mcip, group, defgrp);
2033 			if (err != 0)
2034 				return (err);
2035 			/*
2036 			 * Update the accounting. If this group
2037 			 * already had explicitly reserved rings,
2038 			 * we need to update the rings based on
2039 			 * the new ring count. If this group
2040 			 * had not explicitly reserved rings,
2041 			 * then we just reserve the rings asked for
2042 			 * and reserve the group.
2043 			 */
2044 			if (tmrp->mrp_mask & MRP_TX_RINGS) {
2045 				if (ringcnt > group->mrg_cur_count) {
2046 					MAC_TX_RING_RELEASED(mip,
2047 					    ringcnt - group->mrg_cur_count);
2048 				} else {
2049 					MAC_TX_RING_RESERVED(mip,
2050 					    group->mrg_cur_count - ringcnt);
2051 				}
2052 			} else {
2053 				MAC_TX_RING_RESERVED(mip, group->mrg_cur_count);
2054 				MAC_TX_GRP_RESERVED(mip);
2055 			}
2056 		}
2057 	}
2058 	return (0);
2059 }
2060 
2061 /*
2062  * When the MAC client is being brought up (i.e. we do a unicast_add) we need
2063  * to initialize the cpu and resource control structure in the
2064  * mac_client_impl_t from the mac_impl_t (i.e if there are any cached
2065  * properties before the flow entry for the unicast address was created).
2066  */
2067 static int
2068 mac_resource_ctl_set(mac_client_handle_t mch, mac_resource_props_t *mrp)
2069 {
2070 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
2071 	mac_impl_t		*mip = (mac_impl_t *)mcip->mci_mip;
2072 	mac_impl_t		*umip = mcip->mci_upper_mip;
2073 	int			err = 0;
2074 	flow_entry_t		*flent = mcip->mci_flent;
2075 	mac_resource_props_t	*omrp, *nmrp = MCIP_RESOURCE_PROPS(mcip);
2076 
2077 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
2078 
2079 	err = mac_validate_props(mcip->mci_state_flags & MCIS_IS_VNIC ?
2080 	    mcip->mci_upper_mip : mip, mrp);
2081 	if (err != 0)
2082 		return (err);
2083 
2084 	/*
2085 	 * Copy over the existing properties since mac_update_resources
2086 	 * will modify the client's mrp. Currently, the saved property
2087 	 * is used to determine the difference between existing and
2088 	 * modified rings property.
2089 	 */
2090 	omrp = kmem_zalloc(sizeof (*omrp), KM_SLEEP);
2091 	bcopy(nmrp, omrp, sizeof (*omrp));
2092 	mac_update_resources(mrp, MCIP_RESOURCE_PROPS(mcip), B_FALSE);
2093 	if (MCIP_DATAPATH_SETUP(mcip)) {
2094 		/*
2095 		 * We support rings only for primary client when there are
2096 		 * multiple clients sharing the same MAC address (e.g. VLAN).
2097 		 */
2098 		if (mrp->mrp_mask & MRP_RX_RINGS ||
2099 		    mrp->mrp_mask & MRP_TX_RINGS) {
2100 
2101 			if ((err = mac_client_set_rings_prop(mcip, mrp,
2102 			    omrp)) != 0) {
2103 				if (omrp->mrp_mask & MRP_RX_RINGS) {
2104 					nmrp->mrp_mask |= MRP_RX_RINGS;
2105 					nmrp->mrp_nrxrings = omrp->mrp_nrxrings;
2106 				} else {
2107 					nmrp->mrp_mask &= ~MRP_RX_RINGS;
2108 					nmrp->mrp_nrxrings = 0;
2109 				}
2110 				if (omrp->mrp_mask & MRP_TX_RINGS) {
2111 					nmrp->mrp_mask |= MRP_TX_RINGS;
2112 					nmrp->mrp_ntxrings = omrp->mrp_ntxrings;
2113 				} else {
2114 					nmrp->mrp_mask &= ~MRP_TX_RINGS;
2115 					nmrp->mrp_ntxrings = 0;
2116 				}
2117 				if (omrp->mrp_mask & MRP_RXRINGS_UNSPEC)
2118 					omrp->mrp_mask |= MRP_RXRINGS_UNSPEC;
2119 				else
2120 					omrp->mrp_mask &= ~MRP_RXRINGS_UNSPEC;
2121 
2122 				if (omrp->mrp_mask & MRP_TXRINGS_UNSPEC)
2123 					omrp->mrp_mask |= MRP_TXRINGS_UNSPEC;
2124 				else
2125 					omrp->mrp_mask &= ~MRP_TXRINGS_UNSPEC;
2126 				kmem_free(omrp, sizeof (*omrp));
2127 				return (err);
2128 			}
2129 
2130 			/*
2131 			 * If we modified the rings property of the primary
2132 			 * we need to update the property fields of its
2133 			 * VLANs as they inherit the primary's properites.
2134 			 */
2135 			if (mac_is_primary_client(mcip)) {
2136 				mac_set_prim_vlan_rings(mip,
2137 				    MCIP_RESOURCE_PROPS(mcip));
2138 			}
2139 		}
2140 		/*
2141 		 * We have to set this prior to calling mac_flow_modify.
2142 		 */
2143 		if (mrp->mrp_mask & MRP_PRIORITY) {
2144 			if (mrp->mrp_priority == MPL_RESET) {
2145 				MAC_CLIENT_SET_PRIORITY_RANGE(mcip,
2146 				    MPL_LINK_DEFAULT);
2147 			} else {
2148 				MAC_CLIENT_SET_PRIORITY_RANGE(mcip,
2149 				    mrp->mrp_priority);
2150 			}
2151 		}
2152 
2153 		mac_flow_modify(mip->mi_flow_tab, flent, mrp);
2154 		if (mrp->mrp_mask & MRP_PRIORITY)
2155 			mac_update_subflow_priority(mcip);
2156 
2157 		/* Apply these resource settings to any secondary macs */
2158 		if (umip != NULL) {
2159 			ASSERT((umip->mi_state_flags & MIS_IS_VNIC) != 0);
2160 			mac_vnic_secondary_update(umip);
2161 		}
2162 	}
2163 	kmem_free(omrp, sizeof (*omrp));
2164 	return (0);
2165 }
2166 
2167 static int
2168 mac_unicast_flow_create(mac_client_impl_t *mcip, uint8_t *mac_addr,
2169     uint16_t vid, boolean_t is_primary, boolean_t first_flow,
2170     flow_entry_t **flent, mac_resource_props_t *mrp)
2171 {
2172 	mac_impl_t	*mip = (mac_impl_t *)mcip->mci_mip;
2173 	flow_desc_t	flow_desc;
2174 	char		flowname[MAXFLOWNAMELEN];
2175 	int		err;
2176 	uint_t		flent_flags;
2177 
2178 	/*
2179 	 * First unicast address being added, create a new flow
2180 	 * for that MAC client.
2181 	 */
2182 	bzero(&flow_desc, sizeof (flow_desc));
2183 
2184 	ASSERT(mac_addr != NULL ||
2185 	    (mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR));
2186 	if (mac_addr != NULL) {
2187 		flow_desc.fd_mac_len = mip->mi_type->mt_addr_length;
2188 		bcopy(mac_addr, flow_desc.fd_dst_mac, flow_desc.fd_mac_len);
2189 	}
2190 	flow_desc.fd_mask = FLOW_LINK_DST;
2191 	if (vid != 0) {
2192 		flow_desc.fd_vid = vid;
2193 		flow_desc.fd_mask |= FLOW_LINK_VID;
2194 	}
2195 
2196 	/*
2197 	 * XXX-nicolas. For now I'm keeping the FLOW_PRIMARY_MAC
2198 	 * and FLOW_VNIC. Even though they're a hack inherited
2199 	 * from the SRS code, we'll keep them for now. They're currently
2200 	 * consumed by mac_datapath_setup() to create the SRS.
2201 	 * That code should be eventually moved out of
2202 	 * mac_datapath_setup() and moved to a mac_srs_create()
2203 	 * function of some sort to keep things clean.
2204 	 *
2205 	 * Also, there's no reason why the SRS for the primary MAC
2206 	 * client should be different than any other MAC client. Until
2207 	 * this is cleaned-up, we support only one MAC unicast address
2208 	 * per client.
2209 	 *
2210 	 * We set FLOW_PRIMARY_MAC for the primary MAC address,
2211 	 * FLOW_VNIC for everything else.
2212 	 */
2213 	if (is_primary)
2214 		flent_flags = FLOW_PRIMARY_MAC;
2215 	else
2216 		flent_flags = FLOW_VNIC_MAC;
2217 
2218 	/*
2219 	 * For the first flow we use the MAC client's name - mci_name, for
2220 	 * subsequent ones we just create a name with the VID. This is
2221 	 * so that we can add these flows to the same flow table. This is
2222 	 * fine as the flow name (except for the one with the MAC client's
2223 	 * name) is not visible. When the first flow is removed, we just replace
2224 	 * its fdesc with another from the list, so we will still retain the
2225 	 * flent with the MAC client's flow name.
2226 	 */
2227 	if (first_flow) {
2228 		bcopy(mcip->mci_name, flowname, MAXFLOWNAMELEN);
2229 	} else {
2230 		(void) sprintf(flowname, "%s%u", mcip->mci_name, vid);
2231 		flent_flags = FLOW_NO_STATS;
2232 	}
2233 
2234 	if ((err = mac_flow_create(&flow_desc, mrp, flowname, NULL,
2235 	    flent_flags, flent)) != 0)
2236 		return (err);
2237 
2238 	mac_misc_stat_create(*flent);
2239 	FLOW_MARK(*flent, FE_INCIPIENT);
2240 	(*flent)->fe_mcip = mcip;
2241 
2242 	/*
2243 	 * Place initial creation reference on the flow. This reference
2244 	 * is released in the corresponding delete action viz.
2245 	 * mac_unicast_remove after waiting for all transient refs to
2246 	 * to go away. The wait happens in mac_flow_wait.
2247 	 * We have already held the reference in mac_client_open().
2248 	 */
2249 	if (!first_flow)
2250 		FLOW_REFHOLD(*flent);
2251 	return (0);
2252 }
2253 
2254 /* Refresh the multicast grouping for this VID. */
2255 int
2256 mac_client_update_mcast(void *arg, boolean_t add, const uint8_t *addrp)
2257 {
2258 	flow_entry_t		*flent = arg;
2259 	mac_client_impl_t	*mcip = flent->fe_mcip;
2260 	uint16_t		vid;
2261 	flow_desc_t		flow_desc;
2262 
2263 	mac_flow_get_desc(flent, &flow_desc);
2264 	vid = (flow_desc.fd_mask & FLOW_LINK_VID) != 0 ?
2265 	    flow_desc.fd_vid : VLAN_ID_NONE;
2266 
2267 	/*
2268 	 * We don't call mac_multicast_add()/mac_multicast_remove() as
2269 	 * we want to add/remove for this specific vid.
2270 	 */
2271 	if (add) {
2272 		return (mac_bcast_add(mcip, addrp, vid,
2273 		    MAC_ADDRTYPE_MULTICAST));
2274 	} else {
2275 		mac_bcast_delete(mcip, addrp, vid);
2276 		return (0);
2277 	}
2278 }
2279 
2280 static void
2281 mac_update_single_active_client(mac_impl_t *mip)
2282 {
2283 	mac_client_impl_t *client = NULL;
2284 
2285 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
2286 
2287 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
2288 	if (mip->mi_nactiveclients == 1) {
2289 		/*
2290 		 * Find the one active MAC client from the list of MAC
2291 		 * clients. The active MAC client has at least one
2292 		 * unicast address.
2293 		 */
2294 		for (client = mip->mi_clients_list; client != NULL;
2295 		    client = client->mci_client_next) {
2296 			if (client->mci_unicast_list != NULL)
2297 				break;
2298 		}
2299 		ASSERT(client != NULL);
2300 	}
2301 
2302 	/*
2303 	 * mi_single_active_client is protected by the MAC impl's read/writer
2304 	 * lock, which allows mac_rx() to check the value of that pointer
2305 	 * as a reader.
2306 	 */
2307 	mip->mi_single_active_client = client;
2308 	rw_exit(&mip->mi_rw_lock);
2309 }
2310 
2311 /*
2312  * Set up the data path. Called from i_mac_unicast_add after having
2313  * done all the validations including making sure this is an active
2314  * client (i.e that is ready to process packets.)
2315  */
2316 static int
2317 mac_client_datapath_setup(mac_client_impl_t *mcip, uint16_t vid,
2318     uint8_t *mac_addr, mac_resource_props_t *mrp, boolean_t isprimary,
2319     mac_unicast_impl_t *muip)
2320 {
2321 	mac_impl_t	*mip = mcip->mci_mip;
2322 	boolean_t	mac_started = B_FALSE;
2323 	boolean_t	bcast_added = B_FALSE;
2324 	boolean_t	nactiveclients_added = B_FALSE;
2325 	flow_entry_t	*flent;
2326 	int		err = 0;
2327 	boolean_t	no_unicast;
2328 
2329 	no_unicast = mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR;
2330 
2331 	if ((err = mac_start((mac_handle_t)mip)) != 0)
2332 		goto bail;
2333 
2334 	mac_started = B_TRUE;
2335 
2336 	/* add the MAC client to the broadcast address group by default */
2337 	if (mip->mi_type->mt_brdcst_addr != NULL) {
2338 		err = mac_bcast_add(mcip, mip->mi_type->mt_brdcst_addr, vid,
2339 		    MAC_ADDRTYPE_BROADCAST);
2340 		if (err != 0)
2341 			goto bail;
2342 		bcast_added = B_TRUE;
2343 	}
2344 
2345 	/*
2346 	 * If this is the first unicast address addition for this
2347 	 * client, reuse the pre-allocated larval flow entry associated with
2348 	 * the MAC client.
2349 	 */
2350 	flent = (mcip->mci_nflents == 0) ? mcip->mci_flent : NULL;
2351 
2352 	/* We are configuring the unicast flow now */
2353 	if (!MCIP_DATAPATH_SETUP(mcip)) {
2354 
2355 		if (mrp != NULL) {
2356 			MAC_CLIENT_SET_PRIORITY_RANGE(mcip,
2357 			    (mrp->mrp_mask & MRP_PRIORITY) ? mrp->mrp_priority :
2358 			    MPL_LINK_DEFAULT);
2359 		}
2360 		if ((err = mac_unicast_flow_create(mcip, mac_addr, vid,
2361 		    isprimary, B_TRUE, &flent, mrp)) != 0)
2362 			goto bail;
2363 
2364 		mip->mi_nactiveclients++;
2365 		nactiveclients_added = B_TRUE;
2366 
2367 		/*
2368 		 * This will allocate the RX ring group if possible for the
2369 		 * flow and program the software classifier as needed.
2370 		 */
2371 		if ((err = mac_datapath_setup(mcip, flent, SRST_LINK)) != 0)
2372 			goto bail;
2373 
2374 		if (no_unicast)
2375 			goto done_setup;
2376 		/*
2377 		 * The unicast MAC address must have been added successfully.
2378 		 */
2379 		ASSERT(mcip->mci_unicast != NULL);
2380 
2381 		/*
2382 		 * Push down the sub-flows that were defined on this link
2383 		 * hitherto. The flows are added to the active flow table
2384 		 * and SRS, softrings etc. are created as needed.
2385 		 */
2386 		mac_link_init_flows((mac_client_handle_t)mcip);
2387 	} else {
2388 		mac_address_t *map = mcip->mci_unicast;
2389 
2390 		ASSERT(!no_unicast);
2391 		/*
2392 		 * A unicast flow already exists for that MAC client
2393 		 * so this flow must be the same MAC address but with
2394 		 * a different VID. It has been checked by
2395 		 * mac_addr_in_use().
2396 		 *
2397 		 * We will use the SRS etc. from the initial
2398 		 * mci_flent. We don't need to create a kstat for
2399 		 * this, as except for the fdesc, everything will be
2400 		 * used from the first flent.
2401 		 *
2402 		 * The only time we should see multiple flents on the
2403 		 * same MAC client is on the sun4v vsw. If we removed
2404 		 * that code we should be able to remove the entire
2405 		 * notion of multiple flents on a MAC client (this
2406 		 * doesn't affect sub/user flows because they have
2407 		 * their own list unrelated to mci_flent_list).
2408 		 */
2409 		if (bcmp(mac_addr, map->ma_addr, map->ma_len) != 0) {
2410 			err = EINVAL;
2411 			goto bail;
2412 		}
2413 
2414 		if ((err = mac_unicast_flow_create(mcip, mac_addr, vid,
2415 		    isprimary, B_FALSE, &flent, NULL)) != 0) {
2416 			goto bail;
2417 		}
2418 		if ((err = mac_flow_add(mip->mi_flow_tab, flent)) != 0) {
2419 			FLOW_FINAL_REFRELE(flent);
2420 			goto bail;
2421 		}
2422 
2423 		/* update the multicast group for this vid */
2424 		mac_client_bcast_refresh(mcip, mac_client_update_mcast,
2425 		    (void *)flent, B_TRUE);
2426 
2427 	}
2428 
2429 	/* populate the shared MAC address */
2430 	muip->mui_map = mcip->mci_unicast;
2431 
2432 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
2433 	muip->mui_next = mcip->mci_unicast_list;
2434 	mcip->mci_unicast_list = muip;
2435 	rw_exit(&mcip->mci_rw_lock);
2436 
2437 done_setup:
2438 	/*
2439 	 * First add the flent to the flow list of this mcip. Then set
2440 	 * the mip's mi_single_active_client if needed. The Rx path assumes
2441 	 * that mip->mi_single_active_client will always have an associated
2442 	 * flent.
2443 	 */
2444 	mac_client_add_to_flow_list(mcip, flent);
2445 	if (nactiveclients_added)
2446 		mac_update_single_active_client(mip);
2447 	/*
2448 	 * Trigger a renegotiation of the capabilities when the number of
2449 	 * active clients changes from 1 to 2, since some of the capabilities
2450 	 * might have to be disabled. Also send a MAC_NOTE_LINK notification
2451 	 * to all the MAC clients whenever physical link is DOWN.
2452 	 */
2453 	if (mip->mi_nactiveclients == 2) {
2454 		mac_capab_update((mac_handle_t)mip);
2455 		mac_virtual_link_update(mip);
2456 	}
2457 	/*
2458 	 * Now that the setup is complete, clear the INCIPIENT flag.
2459 	 * The flag was set to avoid incoming packets seeing inconsistent
2460 	 * structures while the setup was in progress. Clear the mci_tx_flag
2461 	 * by calling mac_tx_client_block. It is possible that
2462 	 * mac_unicast_remove was called prior to this mac_unicast_add which
2463 	 * could have set the MCI_TX_QUIESCE flag.
2464 	 */
2465 	if (flent->fe_rx_ring_group != NULL)
2466 		mac_rx_group_unmark(flent->fe_rx_ring_group, MR_INCIPIENT);
2467 	FLOW_UNMARK(flent, FE_INCIPIENT);
2468 
2469 	/*
2470 	 * If this is an aggr port client, don't enable the flow's
2471 	 * datapath at this stage. Otherwise, bcast traffic could
2472 	 * arrive while the aggr port is in the process of
2473 	 * initializing. Instead, the flow's datapath is started later
2474 	 * when mac_client_set_flow_cb() is called.
2475 	 */
2476 	if ((mcip->mci_state_flags & MCIS_IS_AGGR_PORT) == 0)
2477 		FLOW_UNMARK(flent, FE_MC_NO_DATAPATH);
2478 
2479 	mac_tx_client_unblock(mcip);
2480 	return (0);
2481 bail:
2482 	if (bcast_added)
2483 		mac_bcast_delete(mcip, mip->mi_type->mt_brdcst_addr, vid);
2484 
2485 	if (nactiveclients_added)
2486 		mip->mi_nactiveclients--;
2487 
2488 	if (mac_started)
2489 		mac_stop((mac_handle_t)mip);
2490 
2491 	return (err);
2492 }
2493 
2494 /*
2495  * Return the passive primary MAC client, if present. The passive client is
2496  * a stand-by client that has the same unicast address as another that is
2497  * currenly active. Once the active client goes away, the passive client
2498  * becomes active.
2499  */
2500 static mac_client_impl_t *
2501 mac_get_passive_primary_client(mac_impl_t *mip)
2502 {
2503 	mac_client_impl_t	*mcip;
2504 
2505 	for (mcip = mip->mi_clients_list; mcip != NULL;
2506 	    mcip = mcip->mci_client_next) {
2507 		if (mac_is_primary_client(mcip) &&
2508 		    (mcip->mci_flags & MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) != 0) {
2509 			return (mcip);
2510 		}
2511 	}
2512 	return (NULL);
2513 }
2514 
2515 /*
2516  * Add a new unicast address to the MAC client.
2517  *
2518  * The MAC address can be specified either by value, or the MAC client
2519  * can specify that it wants to use the primary MAC address of the
2520  * underlying MAC. See the introductory comments at the beginning
2521  * of this file for more more information on primary MAC addresses.
2522  *
2523  * Note also the tuple (MAC address, VID) must be unique
2524  * for the MAC clients defined on top of the same underlying MAC
2525  * instance, unless the MAC_UNICAST_NODUPCHECK is specified.
2526  *
2527  * In no case can a client use the PVID for the MAC, if the MAC has one set.
2528  */
2529 int
2530 i_mac_unicast_add(mac_client_handle_t mch, uint8_t *mac_addr, uint16_t flags,
2531     mac_unicast_handle_t *mah, uint16_t vid, mac_diag_t *diag)
2532 {
2533 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
2534 	mac_impl_t		*mip = mcip->mci_mip;
2535 	int			err;
2536 	uint_t			mac_len = mip->mi_type->mt_addr_length;
2537 	boolean_t		check_dups = !(flags & MAC_UNICAST_NODUPCHECK);
2538 	boolean_t		fastpath_disabled = B_FALSE;
2539 	boolean_t		is_primary = (flags & MAC_UNICAST_PRIMARY);
2540 	boolean_t		is_unicast_hw = (flags & MAC_UNICAST_HW);
2541 	mac_resource_props_t	*mrp;
2542 	boolean_t		passive_client = B_FALSE;
2543 	mac_unicast_impl_t	*muip;
2544 	boolean_t		is_vnic_primary =
2545 	    (flags & MAC_UNICAST_VNIC_PRIMARY);
2546 
2547 	/*
2548 	 * When the VID is non-zero the underlying MAC cannot be a
2549 	 * VNIC. I.e., dladm create-vlan cannot take a VNIC as
2550 	 * argument, only the primary MAC client.
2551 	 */
2552 	ASSERT(!((mip->mi_state_flags & MIS_IS_VNIC) && (vid != VLAN_ID_NONE)));
2553 
2554 	*diag = MAC_DIAG_NONE;
2555 
2556 	/*
2557 	 * Can't unicast add if the client asked only for minimal datapath
2558 	 * setup.
2559 	 */
2560 	if (mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR)
2561 		return (ENOTSUP);
2562 
2563 	/*
2564 	 * Check for an attempted use of the current Port VLAN ID, if enabled.
2565 	 * No client may use it.
2566 	 */
2567 	if (mip->mi_pvid != VLAN_ID_NONE && vid == mip->mi_pvid)
2568 		return (EBUSY);
2569 
2570 	/*
2571 	 * Check whether it's the primary client and flag it.
2572 	 */
2573 	if (!(mcip->mci_state_flags & MCIS_IS_VNIC) && is_primary &&
2574 	    vid == VLAN_ID_NONE)
2575 		mcip->mci_flags |= MAC_CLIENT_FLAGS_PRIMARY;
2576 
2577 	/*
2578 	 * is_vnic_primary is true when we come here as a VLAN VNIC
2579 	 * which uses the primary MAC client's address but with a non-zero
2580 	 * VID. In this case the MAC address is not specified by an upper
2581 	 * MAC client.
2582 	 */
2583 	if ((mcip->mci_state_flags & MCIS_IS_VNIC) && is_primary &&
2584 	    !is_vnic_primary) {
2585 		/*
2586 		 * The address is being set by the upper MAC client
2587 		 * of a VNIC. The MAC address was already set by the
2588 		 * VNIC driver during VNIC creation.
2589 		 *
2590 		 * Note: a VNIC has only one MAC address. We return
2591 		 * the MAC unicast address handle of the lower MAC client
2592 		 * corresponding to the VNIC. We allocate a new entry
2593 		 * which is flagged appropriately, so that mac_unicast_remove()
2594 		 * doesn't attempt to free the original entry that
2595 		 * was allocated by the VNIC driver.
2596 		 */
2597 		ASSERT(mcip->mci_unicast != NULL);
2598 
2599 		/* Check for VLAN flags, if present */
2600 		if ((flags & MAC_UNICAST_TAG_DISABLE) != 0)
2601 			mcip->mci_state_flags |= MCIS_TAG_DISABLE;
2602 
2603 		if ((flags & MAC_UNICAST_STRIP_DISABLE) != 0)
2604 			mcip->mci_state_flags |= MCIS_STRIP_DISABLE;
2605 
2606 		if ((flags & MAC_UNICAST_DISABLE_TX_VID_CHECK) != 0)
2607 			mcip->mci_state_flags |= MCIS_DISABLE_TX_VID_CHECK;
2608 
2609 		/*
2610 		 * Ensure that the primary unicast address of the VNIC
2611 		 * is added only once unless we have the
2612 		 * MAC_CLIENT_FLAGS_MULTI_PRIMARY set (and this is not
2613 		 * a passive MAC client).
2614 		 */
2615 		if ((mcip->mci_flags & MAC_CLIENT_FLAGS_VNIC_PRIMARY) != 0) {
2616 			if ((mcip->mci_flags &
2617 			    MAC_CLIENT_FLAGS_MULTI_PRIMARY) == 0 ||
2618 			    (mcip->mci_flags &
2619 			    MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) != 0) {
2620 				return (EBUSY);
2621 			}
2622 			mcip->mci_flags |= MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
2623 			passive_client = B_TRUE;
2624 		}
2625 
2626 		mcip->mci_flags |= MAC_CLIENT_FLAGS_VNIC_PRIMARY;
2627 
2628 		/*
2629 		 * Create a handle for vid 0.
2630 		 */
2631 		ASSERT(vid == VLAN_ID_NONE);
2632 		muip = kmem_zalloc(sizeof (mac_unicast_impl_t), KM_SLEEP);
2633 		muip->mui_vid = vid;
2634 		*mah = (mac_unicast_handle_t)muip;
2635 		/*
2636 		 * This will be used by the caller to defer setting the
2637 		 * rx functions.
2638 		 */
2639 		if (passive_client)
2640 			return (EAGAIN);
2641 		return (0);
2642 	}
2643 
2644 	/* primary MAC clients cannot be opened on top of anchor VNICs */
2645 	if ((is_vnic_primary || is_primary) &&
2646 	    i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_ANCHOR_VNIC, NULL)) {
2647 		return (ENXIO);
2648 	}
2649 
2650 	/*
2651 	 * If this is a VNIC/VLAN, disable softmac fast-path. This is
2652 	 * only relevant to legacy devices which use softmac to
2653 	 * interface with GLDv3.
2654 	 */
2655 	if (mcip->mci_state_flags & MCIS_IS_VNIC) {
2656 		err = mac_fastpath_disable((mac_handle_t)mip);
2657 		if (err != 0)
2658 			return (err);
2659 		fastpath_disabled = B_TRUE;
2660 	}
2661 
2662 	/*
2663 	 * Return EBUSY if:
2664 	 *  - there is an exclusively active mac client exists.
2665 	 *  - this is an exclusive active mac client but
2666 	 *	a. there is already active mac clients exist, or
2667 	 *	b. fastpath streams are already plumbed on this legacy device
2668 	 *  - the mac creator has disallowed active mac clients.
2669 	 */
2670 	if (mip->mi_state_flags & (MIS_EXCLUSIVE|MIS_NO_ACTIVE)) {
2671 		if (fastpath_disabled)
2672 			mac_fastpath_enable((mac_handle_t)mip);
2673 		return (EBUSY);
2674 	}
2675 
2676 	if (mcip->mci_state_flags & MCIS_EXCLUSIVE) {
2677 		ASSERT(!fastpath_disabled);
2678 		if (mip->mi_nactiveclients != 0)
2679 			return (EBUSY);
2680 
2681 		if ((mip->mi_state_flags & MIS_LEGACY) &&
2682 		    !(mip->mi_capab_legacy.ml_active_set(mip->mi_driver))) {
2683 			return (EBUSY);
2684 		}
2685 		mip->mi_state_flags |= MIS_EXCLUSIVE;
2686 	}
2687 
2688 	mrp = kmem_zalloc(sizeof (*mrp), KM_SLEEP);
2689 	if (is_primary && !(mcip->mci_state_flags & (MCIS_IS_VNIC |
2690 	    MCIS_IS_AGGR_PORT))) {
2691 		/*
2692 		 * Apply the property cached in the mac_impl_t to the primary
2693 		 * mac client. If the mac client is a VNIC or an aggregation
2694 		 * port, its property should be set in the mcip when the
2695 		 * VNIC/aggr was created.
2696 		 */
2697 		mac_get_resources((mac_handle_t)mip, mrp);
2698 		(void) mac_client_set_resources(mch, mrp);
2699 	} else if (mcip->mci_state_flags & MCIS_IS_VNIC) {
2700 		/*
2701 		 * This is a VLAN client sharing the address of the
2702 		 * primary MAC client; i.e., one created via dladm
2703 		 * create-vlan. We don't support specifying ring
2704 		 * properties for this type of client as it inherits
2705 		 * these from the primary MAC client.
2706 		 */
2707 		if (is_vnic_primary) {
2708 			mac_resource_props_t	*vmrp;
2709 
2710 			vmrp = MCIP_RESOURCE_PROPS(mcip);
2711 			if (vmrp->mrp_mask & MRP_RX_RINGS ||
2712 			    vmrp->mrp_mask & MRP_TX_RINGS) {
2713 				if (fastpath_disabled)
2714 					mac_fastpath_enable((mac_handle_t)mip);
2715 				kmem_free(mrp, sizeof (*mrp));
2716 				return (ENOTSUP);
2717 			}
2718 			/*
2719 			 * Additionally we also need to inherit any
2720 			 * rings property from the MAC.
2721 			 */
2722 			mac_get_resources((mac_handle_t)mip, mrp);
2723 			if (mrp->mrp_mask & MRP_RX_RINGS) {
2724 				vmrp->mrp_mask |= MRP_RX_RINGS;
2725 				vmrp->mrp_nrxrings = mrp->mrp_nrxrings;
2726 			}
2727 			if (mrp->mrp_mask & MRP_TX_RINGS) {
2728 				vmrp->mrp_mask |= MRP_TX_RINGS;
2729 				vmrp->mrp_ntxrings = mrp->mrp_ntxrings;
2730 			}
2731 		}
2732 		bcopy(MCIP_RESOURCE_PROPS(mcip), mrp, sizeof (*mrp));
2733 	}
2734 
2735 	muip = kmem_zalloc(sizeof (mac_unicast_impl_t), KM_SLEEP);
2736 	muip->mui_vid = vid;
2737 
2738 	if (is_primary || is_vnic_primary) {
2739 		mac_addr = mip->mi_addr;
2740 	} else {
2741 
2742 		/*
2743 		 * Verify the validity of the specified MAC addresses value.
2744 		 */
2745 		if (!mac_unicst_verify((mac_handle_t)mip, mac_addr, mac_len)) {
2746 			*diag = MAC_DIAG_MACADDR_INVALID;
2747 			err = EINVAL;
2748 			goto bail_out;
2749 		}
2750 
2751 		/*
2752 		 * Make sure that the specified MAC address is different
2753 		 * than the unicast MAC address of the underlying NIC.
2754 		 */
2755 		if (check_dups && bcmp(mip->mi_addr, mac_addr, mac_len) == 0) {
2756 			*diag = MAC_DIAG_MACADDR_NIC;
2757 			err = EINVAL;
2758 			goto bail_out;
2759 		}
2760 	}
2761 
2762 	/*
2763 	 * Set the flags here so that if this is a passive client, we
2764 	 * can return and set it when we call mac_client_datapath_setup
2765 	 * when this becomes the active client. If we defer to using these
2766 	 * flags to mac_client_datapath_setup, then for a passive client,
2767 	 * we'd have to store the flags somewhere (probably fe_flags)
2768 	 * and then use it.
2769 	 */
2770 	if (!MCIP_DATAPATH_SETUP(mcip)) {
2771 		if (is_unicast_hw) {
2772 			/*
2773 			 * The client requires a hardware MAC address slot
2774 			 * for that unicast address. Since we support only
2775 			 * one unicast MAC address per client, flag the
2776 			 * MAC client itself.
2777 			 */
2778 			mcip->mci_state_flags |= MCIS_UNICAST_HW;
2779 		}
2780 
2781 		/* Check for VLAN flags, if present */
2782 		if ((flags & MAC_UNICAST_TAG_DISABLE) != 0)
2783 			mcip->mci_state_flags |= MCIS_TAG_DISABLE;
2784 
2785 		if ((flags & MAC_UNICAST_STRIP_DISABLE) != 0)
2786 			mcip->mci_state_flags |= MCIS_STRIP_DISABLE;
2787 
2788 		if ((flags & MAC_UNICAST_DISABLE_TX_VID_CHECK) != 0)
2789 			mcip->mci_state_flags |= MCIS_DISABLE_TX_VID_CHECK;
2790 	} else {
2791 		/*
2792 		 * Assert that the specified flags are consistent with the
2793 		 * flags specified by previous calls to mac_unicast_add().
2794 		 */
2795 		ASSERT(((flags & MAC_UNICAST_TAG_DISABLE) != 0 &&
2796 		    (mcip->mci_state_flags & MCIS_TAG_DISABLE) != 0) ||
2797 		    ((flags & MAC_UNICAST_TAG_DISABLE) == 0 &&
2798 		    (mcip->mci_state_flags & MCIS_TAG_DISABLE) == 0));
2799 
2800 		ASSERT(((flags & MAC_UNICAST_STRIP_DISABLE) != 0 &&
2801 		    (mcip->mci_state_flags & MCIS_STRIP_DISABLE) != 0) ||
2802 		    ((flags & MAC_UNICAST_STRIP_DISABLE) == 0 &&
2803 		    (mcip->mci_state_flags & MCIS_STRIP_DISABLE) == 0));
2804 
2805 		ASSERT(((flags & MAC_UNICAST_DISABLE_TX_VID_CHECK) != 0 &&
2806 		    (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK) != 0) ||
2807 		    ((flags & MAC_UNICAST_DISABLE_TX_VID_CHECK) == 0 &&
2808 		    (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK) == 0));
2809 
2810 		/*
2811 		 * Make sure the client is consistent about its requests
2812 		 * for MAC addresses. I.e. all requests from the clients
2813 		 * must have the MAC_UNICAST_HW flag set or clear.
2814 		 */
2815 		if (((mcip->mci_state_flags & MCIS_UNICAST_HW) != 0 &&
2816 		    !is_unicast_hw) ||
2817 		    ((mcip->mci_state_flags & MCIS_UNICAST_HW) == 0 &&
2818 		    is_unicast_hw)) {
2819 			err = EINVAL;
2820 			goto bail_out;
2821 		}
2822 	}
2823 	/*
2824 	 * Make sure the MAC address is not already used by
2825 	 * another MAC client defined on top of the same
2826 	 * underlying NIC. Unless we have MAC_CLIENT_FLAGS_MULTI_PRIMARY
2827 	 * set when we allow a passive client to be present which will
2828 	 * be activated when the currently active client goes away - this
2829 	 * works only with primary addresses.
2830 	 */
2831 	if ((check_dups || is_primary || is_vnic_primary) &&
2832 	    mac_addr_in_use(mip, mac_addr, vid)) {
2833 		/*
2834 		 * Must have set the multiple primary address flag when
2835 		 * we did a mac_client_open AND this should be a primary
2836 		 * MAC client AND there should not already be a passive
2837 		 * primary. If all is true then we let this succeed
2838 		 * even if the address is a dup.
2839 		 */
2840 		if ((mcip->mci_flags & MAC_CLIENT_FLAGS_MULTI_PRIMARY) == 0 ||
2841 		    (mcip->mci_flags & MAC_CLIENT_FLAGS_PRIMARY) == 0 ||
2842 		    mac_get_passive_primary_client(mip) != NULL) {
2843 			*diag = MAC_DIAG_MACADDR_INUSE;
2844 			err = EEXIST;
2845 			goto bail_out;
2846 		}
2847 		ASSERT((mcip->mci_flags &
2848 		    MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) == 0);
2849 		mcip->mci_flags |= MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
2850 		kmem_free(mrp, sizeof (*mrp));
2851 
2852 		/*
2853 		 * Stash the unicast address handle, we will use it when
2854 		 * we set up the passive client.
2855 		 */
2856 		mcip->mci_p_unicast_list = muip;
2857 		*mah = (mac_unicast_handle_t)muip;
2858 		return (0);
2859 	}
2860 
2861 	err = mac_client_datapath_setup(mcip, vid, mac_addr, mrp,
2862 	    is_primary || is_vnic_primary, muip);
2863 	if (err != 0)
2864 		goto bail_out;
2865 
2866 	kmem_free(mrp, sizeof (*mrp));
2867 	*mah = (mac_unicast_handle_t)muip;
2868 	return (0);
2869 
2870 bail_out:
2871 	if (fastpath_disabled)
2872 		mac_fastpath_enable((mac_handle_t)mip);
2873 	if (mcip->mci_state_flags & MCIS_EXCLUSIVE) {
2874 		mip->mi_state_flags &= ~MIS_EXCLUSIVE;
2875 		if (mip->mi_state_flags & MIS_LEGACY) {
2876 			mip->mi_capab_legacy.ml_active_clear(
2877 			    mip->mi_driver);
2878 		}
2879 	}
2880 	kmem_free(mrp, sizeof (*mrp));
2881 	kmem_free(muip, sizeof (mac_unicast_impl_t));
2882 	return (err);
2883 }
2884 
2885 /*
2886  * Wrapper function to mac_unicast_add when we want to have the same mac
2887  * client open for two instances, one that is currently active and another
2888  * that will become active when the current one is removed. In this case
2889  * mac_unicast_add will return EGAIN and we will save the rx function and
2890  * arg which will be used when we activate the passive client in
2891  * mac_unicast_remove.
2892  */
2893 int
2894 mac_unicast_add_set_rx(mac_client_handle_t mch, uint8_t *mac_addr,
2895     uint16_t flags, mac_unicast_handle_t *mah,  uint16_t vid, mac_diag_t *diag,
2896     mac_rx_t rx_fn, void *arg)
2897 {
2898 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
2899 	uint_t			err;
2900 
2901 	err = mac_unicast_add(mch, mac_addr, flags, mah, vid, diag);
2902 	if (err != 0 && err != EAGAIN)
2903 		return (err);
2904 	if (err == EAGAIN) {
2905 		if (rx_fn != NULL) {
2906 			mcip->mci_rx_p_fn = rx_fn;
2907 			mcip->mci_rx_p_arg = arg;
2908 		}
2909 		return (0);
2910 	}
2911 	if (rx_fn != NULL)
2912 		mac_rx_set(mch, rx_fn, arg);
2913 	return (err);
2914 }
2915 
2916 int
2917 mac_unicast_add(mac_client_handle_t mch, uint8_t *mac_addr, uint16_t flags,
2918     mac_unicast_handle_t *mah, uint16_t vid, mac_diag_t *diag)
2919 {
2920 	mac_impl_t *mip = ((mac_client_impl_t *)mch)->mci_mip;
2921 	uint_t err;
2922 
2923 	i_mac_perim_enter(mip);
2924 	err = i_mac_unicast_add(mch, mac_addr, flags, mah, vid, diag);
2925 	i_mac_perim_exit(mip);
2926 
2927 	return (err);
2928 }
2929 
2930 static void
2931 mac_client_datapath_teardown(mac_client_handle_t mch, mac_unicast_impl_t *muip,
2932     flow_entry_t *flent)
2933 {
2934 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
2935 	mac_impl_t		*mip = mcip->mci_mip;
2936 	boolean_t		no_unicast;
2937 
2938 	/*
2939 	 * If we have not added a unicast address for this MAC client, just
2940 	 * teardown the datapath.
2941 	 */
2942 	no_unicast = mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR;
2943 
2944 	if (!no_unicast) {
2945 		/*
2946 		 * We would have initialized subflows etc. only if we brought
2947 		 * up the primary client and set the unicast unicast address
2948 		 * etc. Deactivate the flows. The flow entry will be removed
2949 		 * from the active flow tables, and the associated SRS,
2950 		 * softrings etc will be deleted. But the flow entry itself
2951 		 * won't be destroyed, instead it will continue to be archived
2952 		 * off the  the global flow hash list, for a possible future
2953 		 * activation when say IP is plumbed again.
2954 		 */
2955 		mac_link_release_flows(mch);
2956 	}
2957 	mip->mi_nactiveclients--;
2958 	mac_update_single_active_client(mip);
2959 
2960 	/* Tear down the data path */
2961 	mac_datapath_teardown(mcip, mcip->mci_flent, SRST_LINK);
2962 
2963 	/*
2964 	 * Prevent any future access to the flow entry through the mci_flent
2965 	 * pointer by setting the mci_flent to NULL. Access to mci_flent in
2966 	 * mac_bcast_send is also under mi_rw_lock.
2967 	 */
2968 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
2969 	flent = mcip->mci_flent;
2970 	mac_client_remove_flow_from_list(mcip, flent);
2971 
2972 	if (mcip->mci_state_flags & MCIS_DESC_LOGGED)
2973 		mcip->mci_state_flags &= ~MCIS_DESC_LOGGED;
2974 
2975 	/*
2976 	 * This is the last unicast address being removed and there shouldn't
2977 	 * be any outbound data threads at this point coming down from mac
2978 	 * clients. We have waited for the data threads to finish before
2979 	 * starting dld_str_detach. Non-data threads must access TX SRS
2980 	 * under mi_rw_lock.
2981 	 */
2982 	rw_exit(&mip->mi_rw_lock);
2983 
2984 	/*
2985 	 * Don't use FLOW_MARK with FE_MC_NO_DATAPATH, as the flow might
2986 	 * contain other flags, such as FE_CONDEMNED, which we need to
2987 	 * cleared. We don't call mac_flow_cleanup() for this unicast
2988 	 * flow as we have a already cleaned up SRSs etc. (via the teadown
2989 	 * path). We just clear the stats and reset the initial callback
2990 	 * function, the rest will be set when we call mac_flow_create,
2991 	 * if at all.
2992 	 */
2993 	mutex_enter(&flent->fe_lock);
2994 	ASSERT(flent->fe_refcnt == 1 && flent->fe_mbg == NULL &&
2995 	    flent->fe_tx_srs == NULL && flent->fe_rx_srs_cnt == 0);
2996 	flent->fe_flags = FE_MC_NO_DATAPATH;
2997 	flow_stat_destroy(flent);
2998 	mac_misc_stat_delete(flent);
2999 
3000 	/* Initialize the receiver function to a safe routine */
3001 	flent->fe_cb_fn = (flow_fn_t)mac_pkt_drop;
3002 	flent->fe_cb_arg1 = NULL;
3003 	flent->fe_cb_arg2 = NULL;
3004 
3005 	flent->fe_index = -1;
3006 	mutex_exit(&flent->fe_lock);
3007 
3008 	if (mip->mi_type->mt_brdcst_addr != NULL) {
3009 		ASSERT(muip != NULL || no_unicast);
3010 		mac_bcast_delete(mcip, mip->mi_type->mt_brdcst_addr,
3011 		    muip != NULL ? muip->mui_vid : VLAN_ID_NONE);
3012 	}
3013 
3014 	if (mip->mi_nactiveclients == 1) {
3015 		mac_capab_update((mac_handle_t)mip);
3016 		mac_virtual_link_update(mip);
3017 	}
3018 
3019 	if (mcip->mci_state_flags & MCIS_EXCLUSIVE) {
3020 		mip->mi_state_flags &= ~MIS_EXCLUSIVE;
3021 
3022 		if (mip->mi_state_flags & MIS_LEGACY)
3023 			mip->mi_capab_legacy.ml_active_clear(mip->mi_driver);
3024 	}
3025 
3026 	mcip->mci_state_flags &= ~MCIS_UNICAST_HW;
3027 
3028 	if (mcip->mci_state_flags & MCIS_TAG_DISABLE)
3029 		mcip->mci_state_flags &= ~MCIS_TAG_DISABLE;
3030 
3031 	if (mcip->mci_state_flags & MCIS_STRIP_DISABLE)
3032 		mcip->mci_state_flags &= ~MCIS_STRIP_DISABLE;
3033 
3034 	if (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK)
3035 		mcip->mci_state_flags &= ~MCIS_DISABLE_TX_VID_CHECK;
3036 
3037 	if (muip != NULL)
3038 		kmem_free(muip, sizeof (mac_unicast_impl_t));
3039 	mac_protect_cancel_timer(mcip);
3040 	mac_protect_flush_dynamic(mcip);
3041 
3042 	bzero(&mcip->mci_misc_stat, sizeof (mcip->mci_misc_stat));
3043 	/*
3044 	 * Disable fastpath if this is a VNIC or a VLAN.
3045 	 */
3046 	if (mcip->mci_state_flags & MCIS_IS_VNIC)
3047 		mac_fastpath_enable((mac_handle_t)mip);
3048 	mac_stop((mac_handle_t)mip);
3049 }
3050 
3051 /*
3052  * Remove a MAC address which was previously added by mac_unicast_add().
3053  */
3054 int
3055 mac_unicast_remove(mac_client_handle_t mch, mac_unicast_handle_t mah)
3056 {
3057 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
3058 	mac_unicast_impl_t *muip = (mac_unicast_impl_t *)mah;
3059 	mac_unicast_impl_t *pre;
3060 	mac_impl_t *mip = mcip->mci_mip;
3061 	flow_entry_t		*flent;
3062 	uint16_t mui_vid;
3063 
3064 	i_mac_perim_enter(mip);
3065 	if (mcip->mci_flags & MAC_CLIENT_FLAGS_VNIC_PRIMARY) {
3066 		/*
3067 		 * Call made by the upper MAC client of a VNIC.
3068 		 * There's nothing much to do, the unicast address will
3069 		 * be removed by the VNIC driver when the VNIC is deleted,
3070 		 * but let's ensure that all our transmit is done before
3071 		 * the client does a mac_client_stop lest it trigger an
3072 		 * assert in the driver.
3073 		 */
3074 		ASSERT(muip->mui_vid == VLAN_ID_NONE);
3075 
3076 		mac_tx_client_flush(mcip);
3077 
3078 		if ((mcip->mci_flags & MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) != 0) {
3079 			mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
3080 			if (mcip->mci_rx_p_fn != NULL) {
3081 				mac_rx_set(mch, mcip->mci_rx_p_fn,
3082 				    mcip->mci_rx_p_arg);
3083 				mcip->mci_rx_p_fn = NULL;
3084 				mcip->mci_rx_p_arg = NULL;
3085 			}
3086 			kmem_free(muip, sizeof (mac_unicast_impl_t));
3087 			i_mac_perim_exit(mip);
3088 			return (0);
3089 		}
3090 		mcip->mci_flags &= ~MAC_CLIENT_FLAGS_VNIC_PRIMARY;
3091 
3092 		if (mcip->mci_state_flags & MCIS_TAG_DISABLE)
3093 			mcip->mci_state_flags &= ~MCIS_TAG_DISABLE;
3094 
3095 		if (mcip->mci_state_flags & MCIS_STRIP_DISABLE)
3096 			mcip->mci_state_flags &= ~MCIS_STRIP_DISABLE;
3097 
3098 		if (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK)
3099 			mcip->mci_state_flags &= ~MCIS_DISABLE_TX_VID_CHECK;
3100 
3101 		kmem_free(muip, sizeof (mac_unicast_impl_t));
3102 		i_mac_perim_exit(mip);
3103 		return (0);
3104 	}
3105 
3106 	ASSERT(muip != NULL);
3107 
3108 	/*
3109 	 * We are removing a passive client, we haven't setup the datapath
3110 	 * for this yet, so nothing much to do.
3111 	 */
3112 	if ((mcip->mci_flags & MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) != 0) {
3113 
3114 		ASSERT((mcip->mci_flent->fe_flags & FE_MC_NO_DATAPATH) != 0);
3115 		ASSERT(mcip->mci_p_unicast_list == muip);
3116 
3117 		mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
3118 
3119 		mcip->mci_p_unicast_list = NULL;
3120 		mcip->mci_rx_p_fn = NULL;
3121 		mcip->mci_rx_p_arg = NULL;
3122 
3123 		mcip->mci_state_flags &= ~MCIS_UNICAST_HW;
3124 
3125 		if (mcip->mci_state_flags & MCIS_TAG_DISABLE)
3126 			mcip->mci_state_flags &= ~MCIS_TAG_DISABLE;
3127 
3128 		if (mcip->mci_state_flags & MCIS_STRIP_DISABLE)
3129 			mcip->mci_state_flags &= ~MCIS_STRIP_DISABLE;
3130 
3131 		if (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK)
3132 			mcip->mci_state_flags &= ~MCIS_DISABLE_TX_VID_CHECK;
3133 
3134 		kmem_free(muip, sizeof (mac_unicast_impl_t));
3135 		i_mac_perim_exit(mip);
3136 		return (0);
3137 	}
3138 
3139 	/*
3140 	 * Remove the VID from the list of client's VIDs.
3141 	 */
3142 	pre = mcip->mci_unicast_list;
3143 	if (muip == pre) {
3144 		mcip->mci_unicast_list = muip->mui_next;
3145 	} else {
3146 		while ((pre->mui_next != NULL) && (pre->mui_next != muip))
3147 			pre = pre->mui_next;
3148 		ASSERT(pre->mui_next == muip);
3149 		rw_enter(&mcip->mci_rw_lock, RW_WRITER);
3150 		pre->mui_next = muip->mui_next;
3151 		rw_exit(&mcip->mci_rw_lock);
3152 	}
3153 
3154 	if (!mac_client_single_rcvr(mcip)) {
3155 		/*
3156 		 * This MAC client is shared by more than one unicast
3157 		 * addresses, so we will just remove the flent
3158 		 * corresponding to the address being removed. We don't invoke
3159 		 * mac_rx_classify_flow_rem() since the additional flow is
3160 		 * not associated with its own separate set of SRS and rings,
3161 		 * and these constructs are still needed for the remaining
3162 		 * flows.
3163 		 */
3164 		flent = mac_client_get_flow(mcip, muip);
3165 		VERIFY3P(flent, !=, NULL);
3166 
3167 		/*
3168 		 * The first one is disappearing, need to make sure
3169 		 * we replace it with another from the list of
3170 		 * shared clients.
3171 		 */
3172 		if (flent == mcip->mci_flent)
3173 			flent = mac_client_swap_mciflent(mcip);
3174 		mac_client_remove_flow_from_list(mcip, flent);
3175 		mac_flow_remove(mip->mi_flow_tab, flent, B_FALSE);
3176 		mac_flow_wait(flent, FLOW_DRIVER_UPCALL);
3177 
3178 		/*
3179 		 * The multicast groups that were added by the client so
3180 		 * far must be removed from the brodcast domain corresponding
3181 		 * to the VID being removed.
3182 		 */
3183 		mac_client_bcast_refresh(mcip, mac_client_update_mcast,
3184 		    (void *)flent, B_FALSE);
3185 
3186 		if (mip->mi_type->mt_brdcst_addr != NULL) {
3187 			mac_bcast_delete(mcip, mip->mi_type->mt_brdcst_addr,
3188 			    muip->mui_vid);
3189 		}
3190 
3191 		FLOW_FINAL_REFRELE(flent);
3192 		ASSERT(!(mcip->mci_state_flags & MCIS_EXCLUSIVE));
3193 
3194 		/*
3195 		 * Enable fastpath if this is a VNIC or a VLAN.
3196 		 */
3197 		if (mcip->mci_state_flags & MCIS_IS_VNIC)
3198 			mac_fastpath_enable((mac_handle_t)mip);
3199 		mac_stop((mac_handle_t)mip);
3200 		i_mac_perim_exit(mip);
3201 		return (0);
3202 	}
3203 
3204 	mui_vid = muip->mui_vid;
3205 	mac_client_datapath_teardown(mch, muip, flent);
3206 
3207 	if ((mcip->mci_flags & MAC_CLIENT_FLAGS_PRIMARY) &&
3208 	    mui_vid == VLAN_ID_NONE) {
3209 		mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PRIMARY;
3210 	} else {
3211 		i_mac_perim_exit(mip);
3212 		return (0);
3213 	}
3214 
3215 	/*
3216 	 * If we are removing the primary, check if we have a passive primary
3217 	 * client that we need to activate now.
3218 	 */
3219 	mcip = mac_get_passive_primary_client(mip);
3220 	if (mcip != NULL) {
3221 		mac_resource_props_t	*mrp;
3222 		mac_unicast_impl_t	*muip;
3223 
3224 		mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
3225 		mrp = kmem_zalloc(sizeof (*mrp), KM_SLEEP);
3226 
3227 		/*
3228 		 * Apply the property cached in the mac_impl_t to the
3229 		 * primary mac client.
3230 		 */
3231 		mac_get_resources((mac_handle_t)mip, mrp);
3232 		(void) mac_client_set_resources(mch, mrp);
3233 		ASSERT(mcip->mci_p_unicast_list != NULL);
3234 		muip = mcip->mci_p_unicast_list;
3235 		mcip->mci_p_unicast_list = NULL;
3236 		if (mac_client_datapath_setup(mcip, VLAN_ID_NONE,
3237 		    mip->mi_addr, mrp, B_TRUE, muip) == 0) {
3238 			if (mcip->mci_rx_p_fn != NULL) {
3239 				mac_rx_set(mch, mcip->mci_rx_p_fn,
3240 				    mcip->mci_rx_p_arg);
3241 				mcip->mci_rx_p_fn = NULL;
3242 				mcip->mci_rx_p_arg = NULL;
3243 			}
3244 		} else {
3245 			kmem_free(muip, sizeof (mac_unicast_impl_t));
3246 		}
3247 		kmem_free(mrp, sizeof (*mrp));
3248 	}
3249 	i_mac_perim_exit(mip);
3250 	return (0);
3251 }
3252 
3253 /*
3254  * Multicast add function invoked by MAC clients.
3255  */
3256 int
3257 mac_multicast_add(mac_client_handle_t mch, const uint8_t *addr)
3258 {
3259 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
3260 	mac_impl_t		*mip = mcip->mci_mip;
3261 	flow_entry_t		*flent = mcip->mci_flent_list;
3262 	flow_entry_t		*prev_fe = NULL;
3263 	uint16_t		vid;
3264 	int			err = 0;
3265 
3266 	/* Verify the address is a valid multicast address */
3267 	if ((err = mip->mi_type->mt_ops.mtops_multicst_verify(addr,
3268 	    mip->mi_pdata)) != 0)
3269 		return (err);
3270 
3271 	i_mac_perim_enter(mip);
3272 	while (flent != NULL) {
3273 		vid = i_mac_flow_vid(flent);
3274 
3275 		err = mac_bcast_add((mac_client_impl_t *)mch, addr, vid,
3276 		    MAC_ADDRTYPE_MULTICAST);
3277 		if (err != 0)
3278 			break;
3279 		prev_fe = flent;
3280 		flent = flent->fe_client_next;
3281 	}
3282 
3283 	/*
3284 	 * If we failed adding, then undo all, rather than partial
3285 	 * success.
3286 	 */
3287 	if (flent != NULL && prev_fe != NULL) {
3288 		flent = mcip->mci_flent_list;
3289 		while (flent != prev_fe->fe_client_next) {
3290 			vid = i_mac_flow_vid(flent);
3291 			mac_bcast_delete((mac_client_impl_t *)mch, addr, vid);
3292 			flent = flent->fe_client_next;
3293 		}
3294 	}
3295 	i_mac_perim_exit(mip);
3296 	return (err);
3297 }
3298 
3299 /*
3300  * Multicast delete function invoked by MAC clients.
3301  */
3302 void
3303 mac_multicast_remove(mac_client_handle_t mch, const uint8_t *addr)
3304 {
3305 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
3306 	mac_impl_t		*mip = mcip->mci_mip;
3307 	flow_entry_t		*flent;
3308 	uint16_t		vid;
3309 
3310 	i_mac_perim_enter(mip);
3311 	for (flent = mcip->mci_flent_list; flent != NULL;
3312 	    flent = flent->fe_client_next) {
3313 		vid = i_mac_flow_vid(flent);
3314 		mac_bcast_delete((mac_client_impl_t *)mch, addr, vid);
3315 	}
3316 	i_mac_perim_exit(mip);
3317 }
3318 
3319 /*
3320  * When a MAC client desires to capture packets on an interface,
3321  * it registers a promiscuous call back with mac_promisc_add().
3322  * There are three types of promiscuous callbacks:
3323  *
3324  * * MAC_CLIENT_PROMISC_ALL
3325  *   Captures all packets sent and received by the MAC client,
3326  *   the physical interface, as well as all other MAC clients
3327  *   defined on top of the same MAC.
3328  *
3329  * * MAC_CLIENT_PROMISC_FILTERED
3330  *   Captures all packets sent and received by the MAC client,
3331  *   plus all multicast traffic sent and received by the phyisical
3332  *   interface and the other MAC clients.
3333  *
3334  * * MAC_CLIENT_PROMISC_MULTI
3335  *   Captures all broadcast and multicast packets sent and
3336  *   received by the MAC clients as well as the physical interface.
3337  *
3338  * In all cases, the underlying MAC is put in promiscuous mode.
3339  */
3340 int
3341 mac_promisc_add(mac_client_handle_t mch, mac_client_promisc_type_t type,
3342     mac_rx_t fn, void *arg, mac_promisc_handle_t *mphp, uint16_t flags)
3343 {
3344 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
3345 	mac_impl_t *mip = mcip->mci_mip;
3346 	mac_promisc_impl_t *mpip;
3347 	mac_cb_info_t	*mcbi;
3348 	int rc;
3349 
3350 	i_mac_perim_enter(mip);
3351 
3352 	if ((rc = mac_start((mac_handle_t)mip)) != 0) {
3353 		i_mac_perim_exit(mip);
3354 		return (rc);
3355 	}
3356 
3357 	if ((mcip->mci_state_flags & MCIS_IS_VNIC) &&
3358 	    type == MAC_CLIENT_PROMISC_ALL &&
3359 	    (mcip->mci_protect_flags & MPT_FLAG_PROMISC_FILTERED)) {
3360 		/*
3361 		 * The function is being invoked by the upper MAC client
3362 		 * of a VNIC. The VNIC should only see the traffic
3363 		 * it is entitled to.
3364 		 */
3365 		type = MAC_CLIENT_PROMISC_FILTERED;
3366 	}
3367 
3368 
3369 	/*
3370 	 * Turn on promiscuous mode for the underlying NIC.
3371 	 * This is needed even for filtered callbacks which
3372 	 * expect to receive all multicast traffic on the wire.
3373 	 *
3374 	 * Physical promiscuous mode should not be turned on if
3375 	 * MAC_PROMISC_FLAGS_NO_PHYS is set.
3376 	 */
3377 	if ((flags & MAC_PROMISC_FLAGS_NO_PHYS) == 0) {
3378 		if ((rc = i_mac_promisc_set(mip, B_TRUE)) != 0) {
3379 			mac_stop((mac_handle_t)mip);
3380 			i_mac_perim_exit(mip);
3381 			return (rc);
3382 		}
3383 	}
3384 
3385 	mpip = kmem_cache_alloc(mac_promisc_impl_cache, KM_SLEEP);
3386 
3387 	mpip->mpi_type = type;
3388 	mpip->mpi_fn = fn;
3389 	mpip->mpi_arg = arg;
3390 	mpip->mpi_mcip = mcip;
3391 	mpip->mpi_no_tx_loop = ((flags & MAC_PROMISC_FLAGS_NO_TX_LOOP) != 0);
3392 	mpip->mpi_no_phys = ((flags & MAC_PROMISC_FLAGS_NO_PHYS) != 0);
3393 	mpip->mpi_strip_vlan_tag =
3394 	    ((flags & MAC_PROMISC_FLAGS_VLAN_TAG_STRIP) != 0);
3395 	mpip->mpi_no_copy = ((flags & MAC_PROMISC_FLAGS_NO_COPY) != 0);
3396 
3397 	mcbi = &mip->mi_promisc_cb_info;
3398 	mutex_enter(mcbi->mcbi_lockp);
3399 
3400 	mac_callback_add(&mip->mi_promisc_cb_info, &mcip->mci_promisc_list,
3401 	    &mpip->mpi_mci_link);
3402 	mac_callback_add(&mip->mi_promisc_cb_info, &mip->mi_promisc_list,
3403 	    &mpip->mpi_mi_link);
3404 
3405 	mutex_exit(mcbi->mcbi_lockp);
3406 
3407 	*mphp = (mac_promisc_handle_t)mpip;
3408 
3409 	if (mcip->mci_state_flags & MCIS_IS_VNIC) {
3410 		mac_impl_t *umip = mcip->mci_upper_mip;
3411 
3412 		ASSERT(umip != NULL);
3413 		mac_vnic_secondary_update(umip);
3414 	}
3415 
3416 	i_mac_perim_exit(mip);
3417 
3418 	return (0);
3419 }
3420 
3421 /*
3422  * Remove a multicast address previously aded through mac_promisc_add().
3423  */
3424 void
3425 mac_promisc_remove(mac_promisc_handle_t mph)
3426 {
3427 	mac_promisc_impl_t *mpip = (mac_promisc_impl_t *)mph;
3428 	mac_client_impl_t *mcip = mpip->mpi_mcip;
3429 	mac_impl_t *mip = mcip->mci_mip;
3430 	mac_cb_info_t *mcbi;
3431 	int rv;
3432 
3433 	i_mac_perim_enter(mip);
3434 
3435 	/*
3436 	 * Even if the device can't be reset into normal mode, we still
3437 	 * need to clear the client promisc callbacks. The client may want
3438 	 * to close the mac end point and we can't have stale callbacks.
3439 	 */
3440 	if (!(mpip->mpi_no_phys)) {
3441 		if ((rv = i_mac_promisc_set(mip, B_FALSE)) != 0) {
3442 			cmn_err(CE_WARN, "%s: failed to switch OFF promiscuous"
3443 			    " mode because of error 0x%x", mip->mi_name, rv);
3444 		}
3445 	}
3446 	mcbi = &mip->mi_promisc_cb_info;
3447 	mutex_enter(mcbi->mcbi_lockp);
3448 	if (mac_callback_remove(mcbi, &mip->mi_promisc_list,
3449 	    &mpip->mpi_mi_link)) {
3450 		VERIFY(mac_callback_remove(&mip->mi_promisc_cb_info,
3451 		    &mcip->mci_promisc_list, &mpip->mpi_mci_link));
3452 		kmem_cache_free(mac_promisc_impl_cache, mpip);
3453 	} else {
3454 		mac_callback_remove_wait(&mip->mi_promisc_cb_info);
3455 	}
3456 
3457 	if (mcip->mci_state_flags & MCIS_IS_VNIC) {
3458 		mac_impl_t *umip = mcip->mci_upper_mip;
3459 
3460 		ASSERT(umip != NULL);
3461 		mac_vnic_secondary_update(umip);
3462 	}
3463 
3464 	mutex_exit(mcbi->mcbi_lockp);
3465 	mac_stop((mac_handle_t)mip);
3466 
3467 	i_mac_perim_exit(mip);
3468 }
3469 
3470 /*
3471  * Reference count the number of active Tx threads. MCI_TX_QUIESCE indicates
3472  * that a control operation wants to quiesce the Tx data flow in which case
3473  * we return an error. Holding any of the per cpu locks ensures that the
3474  * mci_tx_flag won't change.
3475  *
3476  * 'CPU' must be accessed just once and used to compute the index into the
3477  * percpu array, and that index must be used for the entire duration of the
3478  * packet send operation. Note that the thread may be preempted and run on
3479  * another cpu any time and so we can't use 'CPU' more than once for the
3480  * operation.
3481  */
3482 #define	MAC_TX_TRY_HOLD(mcip, mytx, error)				\
3483 {									\
3484 	(error) = 0;							\
3485 	(mytx) = &(mcip)->mci_tx_pcpu[CPU->cpu_seqid & mac_tx_percpu_cnt]; \
3486 	mutex_enter(&(mytx)->pcpu_tx_lock);				\
3487 	if (!((mcip)->mci_tx_flag & MCI_TX_QUIESCE)) {			\
3488 		(mytx)->pcpu_tx_refcnt++;				\
3489 	} else {							\
3490 		(error) = -1;						\
3491 	}								\
3492 	mutex_exit(&(mytx)->pcpu_tx_lock);				\
3493 }
3494 
3495 /*
3496  * Release the reference. If needed, signal any control operation waiting
3497  * for Tx quiescence. The wait and signal are always done using the
3498  * mci_tx_pcpu[0]'s lock
3499  */
3500 #define	MAC_TX_RELE(mcip, mytx) {					\
3501 	mutex_enter(&(mytx)->pcpu_tx_lock);				\
3502 	if (--(mytx)->pcpu_tx_refcnt == 0 &&				\
3503 	    (mcip)->mci_tx_flag & MCI_TX_QUIESCE) {			\
3504 		mutex_exit(&(mytx)->pcpu_tx_lock);			\
3505 		mutex_enter(&(mcip)->mci_tx_pcpu[0].pcpu_tx_lock);	\
3506 		cv_signal(&(mcip)->mci_tx_cv);				\
3507 		mutex_exit(&(mcip)->mci_tx_pcpu[0].pcpu_tx_lock);	\
3508 	} else {							\
3509 		mutex_exit(&(mytx)->pcpu_tx_lock);			\
3510 	}								\
3511 }
3512 
3513 /*
3514  * Send function invoked by MAC clients.
3515  */
3516 mac_tx_cookie_t
3517 mac_tx(mac_client_handle_t mch, mblk_t *mp_chain, uintptr_t hint,
3518     uint16_t flag, mblk_t **ret_mp)
3519 {
3520 	mac_tx_cookie_t		cookie = 0;
3521 	int			error;
3522 	mac_tx_percpu_t		*mytx;
3523 	mac_soft_ring_set_t	*srs;
3524 	flow_entry_t		*flent;
3525 	boolean_t		is_subflow = B_FALSE;
3526 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
3527 	mac_impl_t		*mip = mcip->mci_mip;
3528 	mac_srs_tx_t		*srs_tx;
3529 
3530 	/*
3531 	 * Check whether the active Tx threads count is bumped already.
3532 	 */
3533 	if (!(flag & MAC_TX_NO_HOLD)) {
3534 		MAC_TX_TRY_HOLD(mcip, mytx, error);
3535 		if (error != 0) {
3536 			freemsgchain(mp_chain);
3537 			return (0);
3538 		}
3539 	}
3540 
3541 	/*
3542 	 * If mac protection is enabled, only the permissible packets will be
3543 	 * returned by mac_protect_check().
3544 	 */
3545 	if ((mcip->mci_flent->
3546 	    fe_resource_props.mrp_mask & MRP_PROTECT) != 0 &&
3547 	    (mp_chain = mac_protect_check(mch, mp_chain)) == NULL)
3548 		goto done;
3549 
3550 	if (mcip->mci_subflow_tab != NULL &&
3551 	    mcip->mci_subflow_tab->ft_flow_count > 0 &&
3552 	    mac_flow_lookup(mcip->mci_subflow_tab, mp_chain,
3553 	    FLOW_OUTBOUND, &flent) == 0) {
3554 		/*
3555 		 * The main assumption here is that if in the event
3556 		 * we get a chain, all the packets will be classified
3557 		 * to the same Flow/SRS. If this changes for any
3558 		 * reason, the following logic should change as well.
3559 		 * I suppose the fanout_hint also assumes this .
3560 		 */
3561 		ASSERT(flent != NULL);
3562 		is_subflow = B_TRUE;
3563 	} else {
3564 		flent = mcip->mci_flent;
3565 	}
3566 
3567 	srs = flent->fe_tx_srs;
3568 	/*
3569 	 * This is to avoid panics with PF_PACKET that can call mac_tx()
3570 	 * against an interface that is not capable of sending. A rewrite
3571 	 * of the mac datapath is required to remove this limitation.
3572 	 */
3573 	if (srs == NULL) {
3574 		freemsgchain(mp_chain);
3575 		goto done;
3576 	}
3577 
3578 	srs_tx = &srs->srs_tx;
3579 	if (srs_tx->st_mode == SRS_TX_DEFAULT &&
3580 	    (srs->srs_state & SRS_ENQUEUED) == 0 &&
3581 	    mip->mi_nactiveclients == 1 && mp_chain->b_next == NULL) {
3582 		uint64_t	obytes;
3583 
3584 		/*
3585 		 * Since dls always opens the underlying MAC, nclients equals
3586 		 * to 1 means that the only active client is dls itself acting
3587 		 * as a primary client of the MAC instance. Since dls will not
3588 		 * send tagged packets in that case, and dls is trusted to send
3589 		 * packets for its allowed VLAN(s), the VLAN tag insertion and
3590 		 * check is required only if nclients is greater than 1.
3591 		 */
3592 		if (mip->mi_nclients > 1) {
3593 			if (MAC_VID_CHECK_NEEDED(mcip)) {
3594 				int	err = 0;
3595 
3596 				MAC_VID_CHECK(mcip, mp_chain, err);
3597 				if (err != 0) {
3598 					freemsg(mp_chain);
3599 					mcip->mci_misc_stat.mms_txerrors++;
3600 					goto done;
3601 				}
3602 			}
3603 			if (MAC_TAG_NEEDED(mcip)) {
3604 				mp_chain = mac_add_vlan_tag(mp_chain, 0,
3605 				    mac_client_vid(mch));
3606 				if (mp_chain == NULL) {
3607 					mcip->mci_misc_stat.mms_txerrors++;
3608 					goto done;
3609 				}
3610 			}
3611 		}
3612 
3613 		obytes = (mp_chain->b_cont == NULL ? MBLKL(mp_chain) :
3614 		    msgdsize(mp_chain));
3615 
3616 		MAC_TX(mip, srs_tx->st_arg2, mp_chain, mcip);
3617 		if (mp_chain == NULL) {
3618 			cookie = 0;
3619 			SRS_TX_STAT_UPDATE(srs, opackets, 1);
3620 			SRS_TX_STAT_UPDATE(srs, obytes, obytes);
3621 		} else {
3622 			mutex_enter(&srs->srs_lock);
3623 			cookie = mac_tx_srs_no_desc(srs, mp_chain,
3624 			    flag, ret_mp);
3625 			mutex_exit(&srs->srs_lock);
3626 		}
3627 	} else {
3628 		cookie = srs_tx->st_func(srs, mp_chain, hint, flag, ret_mp);
3629 	}
3630 
3631 done:
3632 	if (is_subflow)
3633 		FLOW_REFRELE(flent);
3634 
3635 	if (!(flag & MAC_TX_NO_HOLD))
3636 		MAC_TX_RELE(mcip, mytx);
3637 
3638 	return (cookie);
3639 }
3640 
3641 /*
3642  * mac_tx_is_blocked
3643  *
3644  * Given a cookie, it returns if the ring identified by the cookie is
3645  * flow-controlled or not. If NULL is passed in place of a cookie,
3646  * then it finds out if any of the underlying rings belonging to the
3647  * SRS is flow controlled or not and returns that status.
3648  */
3649 /* ARGSUSED */
3650 boolean_t
3651 mac_tx_is_flow_blocked(mac_client_handle_t mch, mac_tx_cookie_t cookie)
3652 {
3653 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
3654 	mac_soft_ring_set_t *mac_srs;
3655 	mac_soft_ring_t *sringp;
3656 	boolean_t blocked = B_FALSE;
3657 	mac_tx_percpu_t *mytx;
3658 	int err;
3659 	int i;
3660 
3661 	/*
3662 	 * Bump the reference count so that mac_srs won't be deleted.
3663 	 * If the client is currently quiesced and we failed to bump
3664 	 * the reference, return B_TRUE so that flow control stays
3665 	 * as enabled.
3666 	 *
3667 	 * Flow control will then be disabled once the client is no
3668 	 * longer quiesced.
3669 	 */
3670 	MAC_TX_TRY_HOLD(mcip, mytx, err);
3671 	if (err != 0)
3672 		return (B_TRUE);
3673 
3674 	if ((mac_srs = MCIP_TX_SRS(mcip)) == NULL) {
3675 		MAC_TX_RELE(mcip, mytx);
3676 		return (B_FALSE);
3677 	}
3678 
3679 	mutex_enter(&mac_srs->srs_lock);
3680 	/*
3681 	 * Only in the case of TX_FANOUT and TX_AGGR, the underlying
3682 	 * softring (s_ring_state) will have the HIWAT set. This is
3683 	 * the multiple Tx ring flow control case. For all other
3684 	 * case, SRS (srs_state) will store the condition.
3685 	 */
3686 	if (mac_srs->srs_tx.st_mode == SRS_TX_FANOUT ||
3687 	    mac_srs->srs_tx.st_mode == SRS_TX_AGGR) {
3688 		if (cookie != 0) {
3689 			sringp = (mac_soft_ring_t *)cookie;
3690 			mutex_enter(&sringp->s_ring_lock);
3691 			if (sringp->s_ring_state & S_RING_TX_HIWAT)
3692 				blocked = B_TRUE;
3693 			mutex_exit(&sringp->s_ring_lock);
3694 		} else {
3695 			for (i = 0; i < mac_srs->srs_tx_ring_count; i++) {
3696 				sringp = mac_srs->srs_tx_soft_rings[i];
3697 				mutex_enter(&sringp->s_ring_lock);
3698 				if (sringp->s_ring_state & S_RING_TX_HIWAT) {
3699 					blocked = B_TRUE;
3700 					mutex_exit(&sringp->s_ring_lock);
3701 					break;
3702 				}
3703 				mutex_exit(&sringp->s_ring_lock);
3704 			}
3705 		}
3706 	} else {
3707 		blocked = (mac_srs->srs_state & SRS_TX_HIWAT);
3708 	}
3709 	mutex_exit(&mac_srs->srs_lock);
3710 	MAC_TX_RELE(mcip, mytx);
3711 	return (blocked);
3712 }
3713 
3714 /*
3715  * Check if the MAC client is the primary MAC client.
3716  */
3717 boolean_t
3718 mac_is_primary_client(mac_client_impl_t *mcip)
3719 {
3720 	return (mcip->mci_flags & MAC_CLIENT_FLAGS_PRIMARY);
3721 }
3722 
3723 void
3724 mac_ioctl(mac_handle_t mh, queue_t *wq, mblk_t *bp)
3725 {
3726 	mac_impl_t	*mip = (mac_impl_t *)mh;
3727 	int cmd = ((struct iocblk *)bp->b_rptr)->ioc_cmd;
3728 
3729 	if ((cmd == ND_GET && (mip->mi_callbacks->mc_callbacks & MC_GETPROP)) ||
3730 	    (cmd == ND_SET && (mip->mi_callbacks->mc_callbacks & MC_SETPROP))) {
3731 		/*
3732 		 * If ndd props were registered, call them.
3733 		 * Note that ndd ioctls are Obsolete
3734 		 */
3735 		mac_ndd_ioctl(mip, wq, bp);
3736 		return;
3737 	}
3738 
3739 	/*
3740 	 * Call the driver to handle the ioctl.  The driver may not support
3741 	 * any ioctls, in which case we reply with a NAK on its behalf.
3742 	 */
3743 	if (mip->mi_callbacks->mc_callbacks & MC_IOCTL)
3744 		mip->mi_ioctl(mip->mi_driver, wq, bp);
3745 	else
3746 		miocnak(wq, bp, 0, EINVAL);
3747 }
3748 
3749 /*
3750  * Return the link state of the specified MAC instance.
3751  */
3752 link_state_t
3753 mac_link_get(mac_handle_t mh)
3754 {
3755 	return (((mac_impl_t *)mh)->mi_linkstate);
3756 }
3757 
3758 /*
3759  * Add a mac client specified notification callback. Please see the comments
3760  * above mac_callback_add() for general information about mac callback
3761  * addition/deletion in the presence of mac callback list walkers
3762  */
3763 mac_notify_handle_t
3764 mac_notify_add(mac_handle_t mh, mac_notify_t notify_fn, void *arg)
3765 {
3766 	mac_impl_t		*mip = (mac_impl_t *)mh;
3767 	mac_notify_cb_t		*mncb;
3768 	mac_cb_info_t		*mcbi;
3769 
3770 	/*
3771 	 * Allocate a notify callback structure, fill in the details and
3772 	 * use the mac callback list manipulation functions to chain into
3773 	 * the list of callbacks.
3774 	 */
3775 	mncb = kmem_zalloc(sizeof (mac_notify_cb_t), KM_SLEEP);
3776 	mncb->mncb_fn = notify_fn;
3777 	mncb->mncb_arg = arg;
3778 	mncb->mncb_mip = mip;
3779 	mncb->mncb_link.mcb_objp = mncb;
3780 	mncb->mncb_link.mcb_objsize = sizeof (mac_notify_cb_t);
3781 	mncb->mncb_link.mcb_flags = MCB_NOTIFY_CB_T;
3782 
3783 	mcbi = &mip->mi_notify_cb_info;
3784 
3785 	i_mac_perim_enter(mip);
3786 	mutex_enter(mcbi->mcbi_lockp);
3787 
3788 	mac_callback_add(&mip->mi_notify_cb_info, &mip->mi_notify_cb_list,
3789 	    &mncb->mncb_link);
3790 
3791 	mutex_exit(mcbi->mcbi_lockp);
3792 	i_mac_perim_exit(mip);
3793 	return ((mac_notify_handle_t)mncb);
3794 }
3795 
3796 void
3797 mac_notify_remove_wait(mac_handle_t mh)
3798 {
3799 	mac_impl_t	*mip = (mac_impl_t *)mh;
3800 	mac_cb_info_t	*mcbi = &mip->mi_notify_cb_info;
3801 
3802 	mutex_enter(mcbi->mcbi_lockp);
3803 	mac_callback_remove_wait(&mip->mi_notify_cb_info);
3804 	mutex_exit(mcbi->mcbi_lockp);
3805 }
3806 
3807 /*
3808  * Remove a mac client specified notification callback
3809  */
3810 int
3811 mac_notify_remove(mac_notify_handle_t mnh, boolean_t wait)
3812 {
3813 	mac_notify_cb_t	*mncb = (mac_notify_cb_t *)mnh;
3814 	mac_impl_t	*mip = mncb->mncb_mip;
3815 	mac_cb_info_t	*mcbi;
3816 	int		err = 0;
3817 
3818 	mcbi = &mip->mi_notify_cb_info;
3819 
3820 	i_mac_perim_enter(mip);
3821 	mutex_enter(mcbi->mcbi_lockp);
3822 
3823 	ASSERT(mncb->mncb_link.mcb_objp == mncb);
3824 	/*
3825 	 * If there aren't any list walkers, the remove would succeed
3826 	 * inline, else we wait for the deferred remove to complete
3827 	 */
3828 	if (mac_callback_remove(&mip->mi_notify_cb_info,
3829 	    &mip->mi_notify_cb_list, &mncb->mncb_link)) {
3830 		kmem_free(mncb, sizeof (mac_notify_cb_t));
3831 	} else {
3832 		err = EBUSY;
3833 	}
3834 
3835 	mutex_exit(mcbi->mcbi_lockp);
3836 	i_mac_perim_exit(mip);
3837 
3838 	/*
3839 	 * If we failed to remove the notification callback and "wait" is set
3840 	 * to be B_TRUE, wait for the callback to finish after we exit the
3841 	 * mac perimeter.
3842 	 */
3843 	if (err != 0 && wait) {
3844 		mac_notify_remove_wait((mac_handle_t)mip);
3845 		return (0);
3846 	}
3847 
3848 	return (err);
3849 }
3850 
3851 /*
3852  * Associate resource management callbacks with the specified MAC
3853  * clients.
3854  */
3855 
3856 void
3857 mac_resource_set_common(mac_client_handle_t mch, mac_resource_add_t add,
3858     mac_resource_remove_t remove, mac_resource_quiesce_t quiesce,
3859     mac_resource_restart_t restart, mac_resource_bind_t bind,
3860     void *arg)
3861 {
3862 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
3863 
3864 	mcip->mci_resource_add = add;
3865 	mcip->mci_resource_remove = remove;
3866 	mcip->mci_resource_quiesce = quiesce;
3867 	mcip->mci_resource_restart = restart;
3868 	mcip->mci_resource_bind = bind;
3869 	mcip->mci_resource_arg = arg;
3870 }
3871 
3872 void
3873 mac_resource_set(mac_client_handle_t mch, mac_resource_add_t add, void *arg)
3874 {
3875 	/* update the 'resource_add' callback */
3876 	mac_resource_set_common(mch, add, NULL, NULL, NULL, NULL, arg);
3877 }
3878 
3879 /*
3880  * Sets up the client resources and enable the polling interface over all the
3881  * SRS's and the soft rings of the client
3882  */
3883 void
3884 mac_client_poll_enable(mac_client_handle_t mch)
3885 {
3886 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
3887 	mac_soft_ring_set_t	*mac_srs;
3888 	flow_entry_t		*flent;
3889 	int			i;
3890 
3891 	flent = mcip->mci_flent;
3892 	ASSERT(flent != NULL);
3893 
3894 	mcip->mci_state_flags |= MCIS_CLIENT_POLL_CAPABLE;
3895 	for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
3896 		mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
3897 		ASSERT(mac_srs->srs_mcip == mcip);
3898 		mac_srs_client_poll_enable(mcip, mac_srs);
3899 	}
3900 }
3901 
3902 /*
3903  * Tears down the client resources and disable the polling interface over all
3904  * the SRS's and the soft rings of the client
3905  */
3906 void
3907 mac_client_poll_disable(mac_client_handle_t mch)
3908 {
3909 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
3910 	mac_soft_ring_set_t	*mac_srs;
3911 	flow_entry_t		*flent;
3912 	int			i;
3913 
3914 	flent = mcip->mci_flent;
3915 	ASSERT(flent != NULL);
3916 
3917 	mcip->mci_state_flags &= ~MCIS_CLIENT_POLL_CAPABLE;
3918 	for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
3919 		mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
3920 		ASSERT(mac_srs->srs_mcip == mcip);
3921 		mac_srs_client_poll_disable(mcip, mac_srs);
3922 	}
3923 }
3924 
3925 /*
3926  * Associate the CPUs specified by the given property with a MAC client.
3927  */
3928 int
3929 mac_cpu_set(mac_client_handle_t mch, mac_resource_props_t *mrp)
3930 {
3931 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
3932 	mac_impl_t *mip = mcip->mci_mip;
3933 	int err = 0;
3934 
3935 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
3936 
3937 	if ((err = mac_validate_props(mcip->mci_state_flags & MCIS_IS_VNIC ?
3938 	    mcip->mci_upper_mip : mip, mrp)) != 0) {
3939 		return (err);
3940 	}
3941 	if (MCIP_DATAPATH_SETUP(mcip))
3942 		mac_flow_modify(mip->mi_flow_tab, mcip->mci_flent, mrp);
3943 
3944 	mac_update_resources(mrp, MCIP_RESOURCE_PROPS(mcip), B_FALSE);
3945 	return (0);
3946 }
3947 
3948 /*
3949  * Apply the specified properties to the specified MAC client.
3950  */
3951 int
3952 mac_client_set_resources(mac_client_handle_t mch, mac_resource_props_t *mrp)
3953 {
3954 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
3955 	mac_impl_t *mip = mcip->mci_mip;
3956 	int err = 0;
3957 
3958 	i_mac_perim_enter(mip);
3959 
3960 	if ((mrp->mrp_mask & MRP_MAXBW) || (mrp->mrp_mask & MRP_PRIORITY)) {
3961 		err = mac_resource_ctl_set(mch, mrp);
3962 		if (err != 0)
3963 			goto done;
3964 	}
3965 
3966 	if (mrp->mrp_mask & (MRP_CPUS|MRP_POOL)) {
3967 		err = mac_cpu_set(mch, mrp);
3968 		if (err != 0)
3969 			goto done;
3970 	}
3971 
3972 	if (mrp->mrp_mask & MRP_PROTECT) {
3973 		err = mac_protect_set(mch, mrp);
3974 		if (err != 0)
3975 			goto done;
3976 	}
3977 
3978 	if ((mrp->mrp_mask & MRP_RX_RINGS) || (mrp->mrp_mask & MRP_TX_RINGS))
3979 		err = mac_resource_ctl_set(mch, mrp);
3980 
3981 done:
3982 	i_mac_perim_exit(mip);
3983 	return (err);
3984 }
3985 
3986 /*
3987  * Return the properties currently associated with the specified MAC client.
3988  */
3989 void
3990 mac_client_get_resources(mac_client_handle_t mch, mac_resource_props_t *mrp)
3991 {
3992 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
3993 	mac_resource_props_t	*mcip_mrp = MCIP_RESOURCE_PROPS(mcip);
3994 
3995 	bcopy(mcip_mrp, mrp, sizeof (mac_resource_props_t));
3996 }
3997 
3998 /*
3999  * Return the effective properties currently associated with the specified
4000  * MAC client.
4001  */
4002 void
4003 mac_client_get_effective_resources(mac_client_handle_t mch,
4004     mac_resource_props_t *mrp)
4005 {
4006 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
4007 	mac_resource_props_t	*mcip_mrp = MCIP_EFFECTIVE_PROPS(mcip);
4008 
4009 	bcopy(mcip_mrp, mrp, sizeof (mac_resource_props_t));
4010 }
4011 
4012 /*
4013  * Pass a copy of the specified packet to the promiscuous callbacks
4014  * of the specified MAC.
4015  *
4016  * If sender is NULL, the function is being invoked for a packet chain
4017  * received from the wire. If sender is non-NULL, it points to
4018  * the MAC client from which the packet is being sent.
4019  *
4020  * The packets are distributed to the promiscuous callbacks as follows:
4021  *
4022  * - all packets are sent to the MAC_CLIENT_PROMISC_ALL callbacks
4023  * - all broadcast and multicast packets are sent to the
4024  *   MAC_CLIENT_PROMISC_FILTER and MAC_CLIENT_PROMISC_MULTI.
4025  *
4026  * The unicast packets of MAC_CLIENT_PROMISC_FILTER callbacks are dispatched
4027  * after classification by mac_rx_deliver().
4028  */
4029 
4030 static void
4031 mac_promisc_dispatch_one(mac_promisc_impl_t *mpip, mblk_t *mp,
4032     boolean_t loopback)
4033 {
4034 	mblk_t *mp_copy, *mp_next;
4035 
4036 	if (!mpip->mpi_no_copy || mpip->mpi_strip_vlan_tag) {
4037 		mp_copy = copymsg(mp);
4038 		if (mp_copy == NULL)
4039 			return;
4040 
4041 		if (mpip->mpi_strip_vlan_tag) {
4042 			mp_copy = mac_strip_vlan_tag_chain(mp_copy);
4043 			if (mp_copy == NULL)
4044 				return;
4045 		}
4046 		mp_next = NULL;
4047 	} else {
4048 		mp_copy = mp;
4049 		mp_next = mp->b_next;
4050 	}
4051 	mp_copy->b_next = NULL;
4052 
4053 	mpip->mpi_fn(mpip->mpi_arg, NULL, mp_copy, loopback);
4054 	if (mp_copy == mp)
4055 		mp->b_next = mp_next;
4056 }
4057 
4058 /*
4059  * Return the VID of a packet. Zero if the packet is not tagged.
4060  */
4061 static uint16_t
4062 mac_ether_vid(mblk_t *mp)
4063 {
4064 	struct ether_header *eth = (struct ether_header *)mp->b_rptr;
4065 
4066 	if (ntohs(eth->ether_type) == ETHERTYPE_VLAN) {
4067 		struct ether_vlan_header *t_evhp =
4068 		    (struct ether_vlan_header *)mp->b_rptr;
4069 		return (VLAN_ID(ntohs(t_evhp->ether_tci)));
4070 	}
4071 
4072 	return (0);
4073 }
4074 
4075 /*
4076  * Return whether the specified packet contains a multicast or broadcast
4077  * destination MAC address.
4078  */
4079 static boolean_t
4080 mac_is_mcast(mac_impl_t *mip, mblk_t *mp)
4081 {
4082 	mac_header_info_t hdr_info;
4083 
4084 	if (mac_header_info((mac_handle_t)mip, mp, &hdr_info) != 0)
4085 		return (B_FALSE);
4086 	return ((hdr_info.mhi_dsttype == MAC_ADDRTYPE_BROADCAST) ||
4087 	    (hdr_info.mhi_dsttype == MAC_ADDRTYPE_MULTICAST));
4088 }
4089 
4090 /*
4091  * Send a copy of an mblk chain to the MAC clients of the specified MAC.
4092  * "sender" points to the sender MAC client for outbound packets, and
4093  * is set to NULL for inbound packets.
4094  */
4095 void
4096 mac_promisc_dispatch(mac_impl_t *mip, mblk_t *mp_chain,
4097     mac_client_impl_t *sender)
4098 {
4099 	mac_promisc_impl_t *mpip;
4100 	mac_cb_t *mcb;
4101 	mblk_t *mp;
4102 	boolean_t is_mcast, is_sender;
4103 
4104 	MAC_PROMISC_WALKER_INC(mip);
4105 	for (mp = mp_chain; mp != NULL; mp = mp->b_next) {
4106 		is_mcast = mac_is_mcast(mip, mp);
4107 		/* send packet to interested callbacks */
4108 		for (mcb = mip->mi_promisc_list; mcb != NULL;
4109 		    mcb = mcb->mcb_nextp) {
4110 			mpip = (mac_promisc_impl_t *)mcb->mcb_objp;
4111 			is_sender = (mpip->mpi_mcip == sender);
4112 
4113 			if (is_sender && mpip->mpi_no_tx_loop)
4114 				/*
4115 				 * The sender doesn't want to receive
4116 				 * copies of the packets it sends.
4117 				 */
4118 				continue;
4119 
4120 			/* this client doesn't need any packets (bridge) */
4121 			if (mpip->mpi_fn == NULL)
4122 				continue;
4123 
4124 			/*
4125 			 * For an ethernet MAC, don't displatch a multicast
4126 			 * packet to a non-PROMISC_ALL callbacks unless the VID
4127 			 * of the packet matches the VID of the client.
4128 			 */
4129 			if (is_mcast &&
4130 			    mpip->mpi_type != MAC_CLIENT_PROMISC_ALL &&
4131 			    !mac_client_check_flow_vid(mpip->mpi_mcip,
4132 			    mac_ether_vid(mp)))
4133 				continue;
4134 
4135 			if (is_sender ||
4136 			    mpip->mpi_type == MAC_CLIENT_PROMISC_ALL ||
4137 			    is_mcast)
4138 				mac_promisc_dispatch_one(mpip, mp, is_sender);
4139 		}
4140 	}
4141 	MAC_PROMISC_WALKER_DCR(mip);
4142 }
4143 
4144 void
4145 mac_promisc_client_dispatch(mac_client_impl_t *mcip, mblk_t *mp_chain)
4146 {
4147 	mac_impl_t		*mip = mcip->mci_mip;
4148 	mac_promisc_impl_t	*mpip;
4149 	boolean_t		is_mcast;
4150 	mblk_t			*mp;
4151 	mac_cb_t		*mcb;
4152 
4153 	/*
4154 	 * The unicast packets for the MAC client still
4155 	 * need to be delivered to the MAC_CLIENT_PROMISC_FILTERED
4156 	 * promiscuous callbacks. The broadcast and multicast
4157 	 * packets were delivered from mac_rx().
4158 	 */
4159 	MAC_PROMISC_WALKER_INC(mip);
4160 	for (mp = mp_chain; mp != NULL; mp = mp->b_next) {
4161 		is_mcast = mac_is_mcast(mip, mp);
4162 		for (mcb = mcip->mci_promisc_list; mcb != NULL;
4163 		    mcb = mcb->mcb_nextp) {
4164 			mpip = (mac_promisc_impl_t *)mcb->mcb_objp;
4165 			if (mpip->mpi_type == MAC_CLIENT_PROMISC_FILTERED &&
4166 			    !is_mcast) {
4167 				mac_promisc_dispatch_one(mpip, mp, B_FALSE);
4168 			}
4169 		}
4170 	}
4171 	MAC_PROMISC_WALKER_DCR(mip);
4172 }
4173 
4174 /*
4175  * Return the margin value currently assigned to the specified MAC instance.
4176  */
4177 void
4178 mac_margin_get(mac_handle_t mh, uint32_t *marginp)
4179 {
4180 	mac_impl_t *mip = (mac_impl_t *)mh;
4181 
4182 	rw_enter(&(mip->mi_rw_lock), RW_READER);
4183 	*marginp = mip->mi_margin;
4184 	rw_exit(&(mip->mi_rw_lock));
4185 }
4186 
4187 /*
4188  * mac_info_get() is used for retrieving the mac_info when a DL_INFO_REQ is
4189  * issued before a DL_ATTACH_REQ. we walk the i_mac_impl_hash table and find
4190  * the first mac_impl_t with a matching driver name; then we copy its mac_info_t
4191  * to the caller. we do all this with i_mac_impl_lock held so the mac_impl_t
4192  * cannot disappear while we are accessing it.
4193  */
4194 typedef struct i_mac_info_state_s {
4195 	const char	*mi_name;
4196 	mac_info_t	*mi_infop;
4197 } i_mac_info_state_t;
4198 
4199 /*ARGSUSED*/
4200 static uint_t
4201 i_mac_info_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
4202 {
4203 	i_mac_info_state_t *statep = arg;
4204 	mac_impl_t *mip = (mac_impl_t *)val;
4205 
4206 	if (mip->mi_state_flags & MIS_DISABLED)
4207 		return (MH_WALK_CONTINUE);
4208 
4209 	if (strcmp(statep->mi_name,
4210 	    ddi_driver_name(mip->mi_dip)) != 0)
4211 		return (MH_WALK_CONTINUE);
4212 
4213 	statep->mi_infop = &mip->mi_info;
4214 	return (MH_WALK_TERMINATE);
4215 }
4216 
4217 boolean_t
4218 mac_info_get(const char *name, mac_info_t *minfop)
4219 {
4220 	i_mac_info_state_t state;
4221 
4222 	rw_enter(&i_mac_impl_lock, RW_READER);
4223 	state.mi_name = name;
4224 	state.mi_infop = NULL;
4225 	mod_hash_walk(i_mac_impl_hash, i_mac_info_walker, &state);
4226 	if (state.mi_infop == NULL) {
4227 		rw_exit(&i_mac_impl_lock);
4228 		return (B_FALSE);
4229 	}
4230 	*minfop = *state.mi_infop;
4231 	rw_exit(&i_mac_impl_lock);
4232 	return (B_TRUE);
4233 }
4234 
4235 /*
4236  * To get the capabilities that MAC layer cares about, such as rings, factory
4237  * mac address, vnic or not, it should directly invoke this function.  If the
4238  * link is part of a bridge, then the only "capability" it has is the inability
4239  * to do zero copy.
4240  */
4241 boolean_t
4242 i_mac_capab_get(mac_handle_t mh, mac_capab_t cap, void *cap_data)
4243 {
4244 	mac_impl_t *mip = (mac_impl_t *)mh;
4245 
4246 	if (mip->mi_bridge_link != NULL)
4247 		return (cap == MAC_CAPAB_NO_ZCOPY);
4248 	else if (mip->mi_callbacks->mc_callbacks & MC_GETCAPAB)
4249 		return (mip->mi_getcapab(mip->mi_driver, cap, cap_data));
4250 	else
4251 		return (B_FALSE);
4252 }
4253 
4254 /*
4255  * Capability query function. If number of active mac clients is greater than
4256  * 1, only limited capabilities can be advertised to the caller no matter the
4257  * driver has certain capability or not. Else, we query the driver to get the
4258  * capability.
4259  */
4260 boolean_t
4261 mac_capab_get(mac_handle_t mh, mac_capab_t cap, void *cap_data)
4262 {
4263 	mac_impl_t *mip = (mac_impl_t *)mh;
4264 
4265 	/*
4266 	 * if mi_nactiveclients > 1, only MAC_CAPAB_LEGACY, MAC_CAPAB_HCKSUM,
4267 	 * MAC_CAPAB_NO_NATIVEVLAN and MAC_CAPAB_NO_ZCOPY can be advertised.
4268 	 */
4269 	if (mip->mi_nactiveclients > 1) {
4270 		switch (cap) {
4271 		case MAC_CAPAB_NO_ZCOPY:
4272 			return (B_TRUE);
4273 		case MAC_CAPAB_LEGACY:
4274 		case MAC_CAPAB_HCKSUM:
4275 		case MAC_CAPAB_NO_NATIVEVLAN:
4276 			break;
4277 		default:
4278 			return (B_FALSE);
4279 		}
4280 	}
4281 
4282 	/* else get capab from driver */
4283 	return (i_mac_capab_get(mh, cap, cap_data));
4284 }
4285 
4286 boolean_t
4287 mac_sap_verify(mac_handle_t mh, uint32_t sap, uint32_t *bind_sap)
4288 {
4289 	mac_impl_t *mip = (mac_impl_t *)mh;
4290 
4291 	return (mip->mi_type->mt_ops.mtops_sap_verify(sap, bind_sap,
4292 	    mip->mi_pdata));
4293 }
4294 
4295 mblk_t *
4296 mac_header(mac_handle_t mh, const uint8_t *daddr, uint32_t sap, mblk_t *payload,
4297     size_t extra_len)
4298 {
4299 	mac_impl_t	*mip = (mac_impl_t *)mh;
4300 	const uint8_t	*hdr_daddr;
4301 
4302 	/*
4303 	 * If the MAC is point-to-point with a fixed destination address, then
4304 	 * we must always use that destination in the MAC header.
4305 	 */
4306 	hdr_daddr = (mip->mi_dstaddr_set ? mip->mi_dstaddr : daddr);
4307 	return (mip->mi_type->mt_ops.mtops_header(mip->mi_addr, hdr_daddr, sap,
4308 	    mip->mi_pdata, payload, extra_len));
4309 }
4310 
4311 int
4312 mac_header_info(mac_handle_t mh, mblk_t *mp, mac_header_info_t *mhip)
4313 {
4314 	mac_impl_t *mip = (mac_impl_t *)mh;
4315 
4316 	return (mip->mi_type->mt_ops.mtops_header_info(mp, mip->mi_pdata,
4317 	    mhip));
4318 }
4319 
4320 int
4321 mac_vlan_header_info(mac_handle_t mh, mblk_t *mp, mac_header_info_t *mhip)
4322 {
4323 	mac_impl_t	*mip = (mac_impl_t *)mh;
4324 	boolean_t	is_ethernet = (mip->mi_info.mi_media == DL_ETHER);
4325 	int		err = 0;
4326 
4327 	/*
4328 	 * Packets should always be at least 16 bit aligned.
4329 	 */
4330 	ASSERT(IS_P2ALIGNED(mp->b_rptr, sizeof (uint16_t)));
4331 
4332 	if ((err = mac_header_info(mh, mp, mhip)) != 0)
4333 		return (err);
4334 
4335 	/*
4336 	 * If this is a VLAN-tagged Ethernet packet, then the SAP in the
4337 	 * mac_header_info_t as returned by mac_header_info() is
4338 	 * ETHERTYPE_VLAN. We need to grab the ethertype from the VLAN header.
4339 	 */
4340 	if (is_ethernet && (mhip->mhi_bindsap == ETHERTYPE_VLAN)) {
4341 		struct ether_vlan_header *evhp;
4342 		uint16_t sap;
4343 		mblk_t *tmp = NULL;
4344 		size_t size;
4345 
4346 		size = sizeof (struct ether_vlan_header);
4347 		if (MBLKL(mp) < size) {
4348 			/*
4349 			 * Pullup the message in order to get the MAC header
4350 			 * infomation. Note that this is a read-only function,
4351 			 * we keep the input packet intact.
4352 			 */
4353 			if ((tmp = msgpullup(mp, size)) == NULL)
4354 				return (EINVAL);
4355 
4356 			mp = tmp;
4357 		}
4358 		evhp = (struct ether_vlan_header *)mp->b_rptr;
4359 		sap = ntohs(evhp->ether_type);
4360 		(void) mac_sap_verify(mh, sap, &mhip->mhi_bindsap);
4361 		mhip->mhi_hdrsize = sizeof (struct ether_vlan_header);
4362 		mhip->mhi_tci = ntohs(evhp->ether_tci);
4363 		mhip->mhi_istagged = B_TRUE;
4364 		freemsg(tmp);
4365 
4366 		if (VLAN_CFI(mhip->mhi_tci) != ETHER_CFI)
4367 			return (EINVAL);
4368 	} else {
4369 		mhip->mhi_istagged = B_FALSE;
4370 		mhip->mhi_tci = 0;
4371 	}
4372 
4373 	return (0);
4374 }
4375 
4376 mblk_t *
4377 mac_header_cook(mac_handle_t mh, mblk_t *mp)
4378 {
4379 	mac_impl_t *mip = (mac_impl_t *)mh;
4380 
4381 	if (mip->mi_type->mt_ops.mtops_ops & MTOPS_HEADER_COOK) {
4382 		if (DB_REF(mp) > 1) {
4383 			mblk_t *newmp = copymsg(mp);
4384 			if (newmp == NULL)
4385 				return (NULL);
4386 			freemsg(mp);
4387 			mp = newmp;
4388 		}
4389 		return (mip->mi_type->mt_ops.mtops_header_cook(mp,
4390 		    mip->mi_pdata));
4391 	}
4392 	return (mp);
4393 }
4394 
4395 mblk_t *
4396 mac_header_uncook(mac_handle_t mh, mblk_t *mp)
4397 {
4398 	mac_impl_t *mip = (mac_impl_t *)mh;
4399 
4400 	if (mip->mi_type->mt_ops.mtops_ops & MTOPS_HEADER_UNCOOK) {
4401 		if (DB_REF(mp) > 1) {
4402 			mblk_t *newmp = copymsg(mp);
4403 			if (newmp == NULL)
4404 				return (NULL);
4405 			freemsg(mp);
4406 			mp = newmp;
4407 		}
4408 		return (mip->mi_type->mt_ops.mtops_header_uncook(mp,
4409 		    mip->mi_pdata));
4410 	}
4411 	return (mp);
4412 }
4413 
4414 uint_t
4415 mac_addr_len(mac_handle_t mh)
4416 {
4417 	mac_impl_t *mip = (mac_impl_t *)mh;
4418 
4419 	return (mip->mi_type->mt_addr_length);
4420 }
4421 
4422 /* True if a MAC is a VNIC */
4423 boolean_t
4424 mac_is_vnic(mac_handle_t mh)
4425 {
4426 	return (((mac_impl_t *)mh)->mi_state_flags & MIS_IS_VNIC);
4427 }
4428 
4429 mac_handle_t
4430 mac_get_lower_mac_handle(mac_handle_t mh)
4431 {
4432 	mac_impl_t *mip = (mac_impl_t *)mh;
4433 
4434 	ASSERT(mac_is_vnic(mh));
4435 	return (((vnic_t *)mip->mi_driver)->vn_lower_mh);
4436 }
4437 
4438 boolean_t
4439 mac_is_vnic_primary(mac_handle_t mh)
4440 {
4441 	mac_impl_t *mip = (mac_impl_t *)mh;
4442 
4443 	ASSERT(mac_is_vnic(mh));
4444 	return (((vnic_t *)mip->mi_driver)->vn_addr_type ==
4445 	    VNIC_MAC_ADDR_TYPE_PRIMARY);
4446 }
4447 
4448 void
4449 mac_update_resources(mac_resource_props_t *nmrp, mac_resource_props_t *cmrp,
4450     boolean_t is_user_flow)
4451 {
4452 	if (nmrp != NULL && cmrp != NULL) {
4453 		if (nmrp->mrp_mask & MRP_PRIORITY) {
4454 			if (nmrp->mrp_priority == MPL_RESET) {
4455 				cmrp->mrp_mask &= ~MRP_PRIORITY;
4456 				if (is_user_flow) {
4457 					cmrp->mrp_priority =
4458 					    MPL_SUBFLOW_DEFAULT;
4459 				} else {
4460 					cmrp->mrp_priority = MPL_LINK_DEFAULT;
4461 				}
4462 			} else {
4463 				cmrp->mrp_mask |= MRP_PRIORITY;
4464 				cmrp->mrp_priority = nmrp->mrp_priority;
4465 			}
4466 		}
4467 		if (nmrp->mrp_mask & MRP_MAXBW) {
4468 			if (nmrp->mrp_maxbw == MRP_MAXBW_RESETVAL) {
4469 				cmrp->mrp_mask &= ~MRP_MAXBW;
4470 				cmrp->mrp_maxbw = 0;
4471 			} else {
4472 				cmrp->mrp_mask |= MRP_MAXBW;
4473 				cmrp->mrp_maxbw = nmrp->mrp_maxbw;
4474 			}
4475 		}
4476 		if (nmrp->mrp_mask & MRP_CPUS)
4477 			MAC_COPY_CPUS(nmrp, cmrp);
4478 
4479 		if (nmrp->mrp_mask & MRP_POOL) {
4480 			if (strlen(nmrp->mrp_pool) == 0) {
4481 				cmrp->mrp_mask &= ~MRP_POOL;
4482 				bzero(cmrp->mrp_pool, sizeof (cmrp->mrp_pool));
4483 			} else {
4484 				cmrp->mrp_mask |= MRP_POOL;
4485 				(void) strncpy(cmrp->mrp_pool, nmrp->mrp_pool,
4486 				    sizeof (cmrp->mrp_pool));
4487 			}
4488 
4489 		}
4490 
4491 		if (nmrp->mrp_mask & MRP_PROTECT)
4492 			mac_protect_update(nmrp, cmrp);
4493 
4494 		/*
4495 		 * Update the rings specified.
4496 		 */
4497 		if (nmrp->mrp_mask & MRP_RX_RINGS) {
4498 			if (nmrp->mrp_mask & MRP_RINGS_RESET) {
4499 				cmrp->mrp_mask &= ~MRP_RX_RINGS;
4500 				if (cmrp->mrp_mask & MRP_RXRINGS_UNSPEC)
4501 					cmrp->mrp_mask &= ~MRP_RXRINGS_UNSPEC;
4502 				cmrp->mrp_nrxrings = 0;
4503 			} else {
4504 				cmrp->mrp_mask |= MRP_RX_RINGS;
4505 				cmrp->mrp_nrxrings = nmrp->mrp_nrxrings;
4506 			}
4507 		}
4508 		if (nmrp->mrp_mask & MRP_TX_RINGS) {
4509 			if (nmrp->mrp_mask & MRP_RINGS_RESET) {
4510 				cmrp->mrp_mask &= ~MRP_TX_RINGS;
4511 				if (cmrp->mrp_mask & MRP_TXRINGS_UNSPEC)
4512 					cmrp->mrp_mask &= ~MRP_TXRINGS_UNSPEC;
4513 				cmrp->mrp_ntxrings = 0;
4514 			} else {
4515 				cmrp->mrp_mask |= MRP_TX_RINGS;
4516 				cmrp->mrp_ntxrings = nmrp->mrp_ntxrings;
4517 			}
4518 		}
4519 		if (nmrp->mrp_mask & MRP_RXRINGS_UNSPEC)
4520 			cmrp->mrp_mask |= MRP_RXRINGS_UNSPEC;
4521 		else if (cmrp->mrp_mask & MRP_RXRINGS_UNSPEC)
4522 			cmrp->mrp_mask &= ~MRP_RXRINGS_UNSPEC;
4523 
4524 		if (nmrp->mrp_mask & MRP_TXRINGS_UNSPEC)
4525 			cmrp->mrp_mask |= MRP_TXRINGS_UNSPEC;
4526 		else if (cmrp->mrp_mask & MRP_TXRINGS_UNSPEC)
4527 			cmrp->mrp_mask &= ~MRP_TXRINGS_UNSPEC;
4528 	}
4529 }
4530 
4531 /*
4532  * i_mac_set_resources:
4533  *
4534  * This routine associates properties with the primary MAC client of
4535  * the specified MAC instance.
4536  * - Cache the properties in mac_impl_t
4537  * - Apply the properties to the primary MAC client if exists
4538  */
4539 int
4540 i_mac_set_resources(mac_handle_t mh, mac_resource_props_t *mrp)
4541 {
4542 	mac_impl_t		*mip = (mac_impl_t *)mh;
4543 	mac_client_impl_t	*mcip;
4544 	int			err = 0;
4545 	uint32_t		resmask, newresmask;
4546 	mac_resource_props_t	*tmrp, *umrp;
4547 
4548 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
4549 
4550 	err = mac_validate_props(mip, mrp);
4551 	if (err != 0)
4552 		return (err);
4553 
4554 	umrp = kmem_zalloc(sizeof (*umrp), KM_SLEEP);
4555 	bcopy(&mip->mi_resource_props, umrp, sizeof (*umrp));
4556 	resmask = umrp->mrp_mask;
4557 	mac_update_resources(mrp, umrp, B_FALSE);
4558 	newresmask = umrp->mrp_mask;
4559 
4560 	if (resmask == 0 && newresmask != 0) {
4561 		/*
4562 		 * Bandwidth, priority, cpu or pool link properties configured,
4563 		 * must disable fastpath.
4564 		 */
4565 		if ((err = mac_fastpath_disable((mac_handle_t)mip)) != 0) {
4566 			kmem_free(umrp, sizeof (*umrp));
4567 			return (err);
4568 		}
4569 	}
4570 
4571 	/*
4572 	 * Since bind_cpu may be modified by mac_client_set_resources()
4573 	 * we use a copy of bind_cpu and finally cache bind_cpu in mip.
4574 	 * This allows us to cache only user edits in mip.
4575 	 */
4576 	tmrp = kmem_zalloc(sizeof (*tmrp), KM_SLEEP);
4577 	bcopy(mrp, tmrp, sizeof (*tmrp));
4578 	mcip = mac_primary_client_handle(mip);
4579 	if (mcip != NULL && (mcip->mci_state_flags & MCIS_IS_AGGR_PORT) == 0) {
4580 		err = mac_client_set_resources((mac_client_handle_t)mcip, tmrp);
4581 	} else if ((mrp->mrp_mask & MRP_RX_RINGS ||
4582 	    mrp->mrp_mask & MRP_TX_RINGS)) {
4583 		mac_client_impl_t	*vmcip;
4584 
4585 		/*
4586 		 * If the primary is not up, we need to check if there
4587 		 * are any VLANs on this primary. If there are then
4588 		 * we need to set this property on the VLANs since
4589 		 * VLANs follow the primary they are based on. Just
4590 		 * look for the first VLAN and change its properties,
4591 		 * all the other VLANs should be in the same group.
4592 		 */
4593 		for (vmcip = mip->mi_clients_list; vmcip != NULL;
4594 		    vmcip = vmcip->mci_client_next) {
4595 			if ((vmcip->mci_flent->fe_type & FLOW_PRIMARY_MAC) &&
4596 			    mac_client_vid((mac_client_handle_t)vmcip) !=
4597 			    VLAN_ID_NONE) {
4598 				break;
4599 			}
4600 		}
4601 		if (vmcip != NULL) {
4602 			mac_resource_props_t	*omrp;
4603 			mac_resource_props_t	*vmrp;
4604 
4605 			omrp = kmem_zalloc(sizeof (*omrp), KM_SLEEP);
4606 			bcopy(MCIP_RESOURCE_PROPS(vmcip), omrp, sizeof (*omrp));
4607 			/*
4608 			 * We dont' call mac_update_resources since we
4609 			 * want to take only the ring properties and
4610 			 * not all the properties that may have changed.
4611 			 */
4612 			vmrp = MCIP_RESOURCE_PROPS(vmcip);
4613 			if (mrp->mrp_mask & MRP_RX_RINGS) {
4614 				if (mrp->mrp_mask & MRP_RINGS_RESET) {
4615 					vmrp->mrp_mask &= ~MRP_RX_RINGS;
4616 					if (vmrp->mrp_mask &
4617 					    MRP_RXRINGS_UNSPEC) {
4618 						vmrp->mrp_mask &=
4619 						    ~MRP_RXRINGS_UNSPEC;
4620 					}
4621 					vmrp->mrp_nrxrings = 0;
4622 				} else {
4623 					vmrp->mrp_mask |= MRP_RX_RINGS;
4624 					vmrp->mrp_nrxrings = mrp->mrp_nrxrings;
4625 				}
4626 			}
4627 			if (mrp->mrp_mask & MRP_TX_RINGS) {
4628 				if (mrp->mrp_mask & MRP_RINGS_RESET) {
4629 					vmrp->mrp_mask &= ~MRP_TX_RINGS;
4630 					if (vmrp->mrp_mask &
4631 					    MRP_TXRINGS_UNSPEC) {
4632 						vmrp->mrp_mask &=
4633 						    ~MRP_TXRINGS_UNSPEC;
4634 					}
4635 					vmrp->mrp_ntxrings = 0;
4636 				} else {
4637 					vmrp->mrp_mask |= MRP_TX_RINGS;
4638 					vmrp->mrp_ntxrings = mrp->mrp_ntxrings;
4639 				}
4640 			}
4641 			if (mrp->mrp_mask & MRP_RXRINGS_UNSPEC)
4642 				vmrp->mrp_mask |= MRP_RXRINGS_UNSPEC;
4643 
4644 			if (mrp->mrp_mask & MRP_TXRINGS_UNSPEC)
4645 				vmrp->mrp_mask |= MRP_TXRINGS_UNSPEC;
4646 
4647 			if ((err = mac_client_set_rings_prop(vmcip, mrp,
4648 			    omrp)) != 0) {
4649 				bcopy(omrp, MCIP_RESOURCE_PROPS(vmcip),
4650 				    sizeof (*omrp));
4651 			} else {
4652 				mac_set_prim_vlan_rings(mip, vmrp);
4653 			}
4654 			kmem_free(omrp, sizeof (*omrp));
4655 		}
4656 	}
4657 
4658 	/* Only update the values if mac_client_set_resources succeeded */
4659 	if (err == 0) {
4660 		bcopy(umrp, &mip->mi_resource_props, sizeof (*umrp));
4661 		/*
4662 		 * If bandwidth, priority or cpu link properties cleared,
4663 		 * renable fastpath.
4664 		 */
4665 		if (resmask != 0 && newresmask == 0)
4666 			mac_fastpath_enable((mac_handle_t)mip);
4667 	} else if (resmask == 0 && newresmask != 0) {
4668 		mac_fastpath_enable((mac_handle_t)mip);
4669 	}
4670 	kmem_free(tmrp, sizeof (*tmrp));
4671 	kmem_free(umrp, sizeof (*umrp));
4672 	return (err);
4673 }
4674 
4675 int
4676 mac_set_resources(mac_handle_t mh, mac_resource_props_t *mrp)
4677 {
4678 	int err;
4679 
4680 	i_mac_perim_enter((mac_impl_t *)mh);
4681 	err = i_mac_set_resources(mh, mrp);
4682 	i_mac_perim_exit((mac_impl_t *)mh);
4683 	return (err);
4684 }
4685 
4686 /*
4687  * Get the properties cached for the specified MAC instance.
4688  */
4689 void
4690 mac_get_resources(mac_handle_t mh, mac_resource_props_t *mrp)
4691 {
4692 	mac_impl_t		*mip = (mac_impl_t *)mh;
4693 	mac_client_impl_t	*mcip;
4694 
4695 	mcip = mac_primary_client_handle(mip);
4696 	if (mcip != NULL) {
4697 		mac_client_get_resources((mac_client_handle_t)mcip, mrp);
4698 		return;
4699 	}
4700 	bcopy(&mip->mi_resource_props, mrp, sizeof (mac_resource_props_t));
4701 }
4702 
4703 /*
4704  * Get the effective properties from the primary client of the
4705  * specified MAC instance.
4706  */
4707 void
4708 mac_get_effective_resources(mac_handle_t mh, mac_resource_props_t *mrp)
4709 {
4710 	mac_impl_t		*mip = (mac_impl_t *)mh;
4711 	mac_client_impl_t	*mcip;
4712 
4713 	mcip = mac_primary_client_handle(mip);
4714 	if (mcip != NULL) {
4715 		mac_client_get_effective_resources((mac_client_handle_t)mcip,
4716 		    mrp);
4717 		return;
4718 	}
4719 	bzero(mrp, sizeof (mac_resource_props_t));
4720 }
4721 
4722 int
4723 mac_set_pvid(mac_handle_t mh, uint16_t pvid)
4724 {
4725 	mac_impl_t *mip = (mac_impl_t *)mh;
4726 	mac_client_impl_t *mcip;
4727 	mac_unicast_impl_t *muip;
4728 
4729 	i_mac_perim_enter(mip);
4730 	if (pvid != 0) {
4731 		for (mcip = mip->mi_clients_list; mcip != NULL;
4732 		    mcip = mcip->mci_client_next) {
4733 			for (muip = mcip->mci_unicast_list; muip != NULL;
4734 			    muip = muip->mui_next) {
4735 				if (muip->mui_vid == pvid) {
4736 					i_mac_perim_exit(mip);
4737 					return (EBUSY);
4738 				}
4739 			}
4740 		}
4741 	}
4742 	mip->mi_pvid = pvid;
4743 	i_mac_perim_exit(mip);
4744 	return (0);
4745 }
4746 
4747 uint16_t
4748 mac_get_pvid(mac_handle_t mh)
4749 {
4750 	mac_impl_t *mip = (mac_impl_t *)mh;
4751 
4752 	return (mip->mi_pvid);
4753 }
4754 
4755 uint32_t
4756 mac_get_llimit(mac_handle_t mh)
4757 {
4758 	mac_impl_t *mip = (mac_impl_t *)mh;
4759 
4760 	return (mip->mi_llimit);
4761 }
4762 
4763 uint32_t
4764 mac_get_ldecay(mac_handle_t mh)
4765 {
4766 	mac_impl_t *mip = (mac_impl_t *)mh;
4767 
4768 	return (mip->mi_ldecay);
4769 }
4770 
4771 /*
4772  * Rename a mac client, its flow, and the kstat.
4773  */
4774 int
4775 mac_rename_primary(mac_handle_t mh, const char *new_name)
4776 {
4777 	mac_impl_t		*mip = (mac_impl_t *)mh;
4778 	mac_client_impl_t	*cur_clnt = NULL;
4779 	flow_entry_t		*fep;
4780 
4781 	i_mac_perim_enter(mip);
4782 
4783 	/*
4784 	 * VNICs: we need to change the sys flow name and
4785 	 * the associated flow kstat.
4786 	 */
4787 	if (mip->mi_state_flags & MIS_IS_VNIC) {
4788 		mac_client_impl_t *mcip = mac_vnic_lower(mip);
4789 		ASSERT(new_name != NULL);
4790 		mac_rename_flow_names(mcip, new_name);
4791 		mac_stat_rename(mcip);
4792 		goto done;
4793 	}
4794 	/*
4795 	 * This mac may itself be an aggr link, or it may have some client
4796 	 * which is an aggr port. For both cases, we need to change the
4797 	 * aggr port's mac client name, its flow name and the associated flow
4798 	 * kstat.
4799 	 */
4800 	if (mip->mi_state_flags & MIS_IS_AGGR) {
4801 		mac_capab_aggr_t aggr_cap;
4802 		mac_rename_fn_t rename_fn;
4803 		boolean_t ret;
4804 
4805 		ASSERT(new_name != NULL);
4806 		ret = i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_AGGR,
4807 		    (void *)(&aggr_cap));
4808 		ASSERT(ret == B_TRUE);
4809 		rename_fn = aggr_cap.mca_rename_fn;
4810 		rename_fn(new_name, mip->mi_driver);
4811 		/*
4812 		 * The aggr's client name and kstat flow name will be
4813 		 * updated below, i.e. via mac_rename_flow_names.
4814 		 */
4815 	}
4816 
4817 	for (cur_clnt = mip->mi_clients_list; cur_clnt != NULL;
4818 	    cur_clnt = cur_clnt->mci_client_next) {
4819 		if (cur_clnt->mci_state_flags & MCIS_IS_AGGR_PORT) {
4820 			if (new_name != NULL) {
4821 				char *str_st = cur_clnt->mci_name;
4822 				char *str_del = strchr(str_st, '-');
4823 
4824 				ASSERT(str_del != NULL);
4825 				bzero(str_del + 1, MAXNAMELEN -
4826 				    (str_del - str_st + 1));
4827 				bcopy(new_name, str_del + 1,
4828 				    strlen(new_name));
4829 			}
4830 			fep = cur_clnt->mci_flent;
4831 			mac_rename_flow(fep, cur_clnt->mci_name);
4832 			break;
4833 		} else if (new_name != NULL &&
4834 		    cur_clnt->mci_state_flags & MCIS_USE_DATALINK_NAME) {
4835 			mac_rename_flow_names(cur_clnt, new_name);
4836 			break;
4837 		}
4838 	}
4839 
4840 	/* Recreate kstats associated with aggr pseudo rings */
4841 	if (mip->mi_state_flags & MIS_IS_AGGR)
4842 		mac_pseudo_ring_stat_rename(mip);
4843 
4844 done:
4845 	i_mac_perim_exit(mip);
4846 	return (0);
4847 }
4848 
4849 /*
4850  * Rename the MAC client's flow names
4851  */
4852 static void
4853 mac_rename_flow_names(mac_client_impl_t *mcip, const char *new_name)
4854 {
4855 	flow_entry_t	*flent;
4856 	uint16_t	vid;
4857 	char		flowname[MAXFLOWNAMELEN];
4858 	mac_impl_t	*mip = mcip->mci_mip;
4859 
4860 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
4861 
4862 	/*
4863 	 * Use mi_rw_lock to ensure that threads not in the mac perimeter
4864 	 * see a self-consistent value for mci_name
4865 	 */
4866 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
4867 	(void) strlcpy(mcip->mci_name, new_name, sizeof (mcip->mci_name));
4868 	rw_exit(&mip->mi_rw_lock);
4869 
4870 	mac_rename_flow(mcip->mci_flent, new_name);
4871 
4872 	if (mcip->mci_nflents == 1)
4873 		return;
4874 
4875 	/*
4876 	 * We have to rename all the others too, no stats to destroy for
4877 	 * these.
4878 	 */
4879 	for (flent = mcip->mci_flent_list; flent != NULL;
4880 	    flent = flent->fe_client_next) {
4881 		if (flent != mcip->mci_flent) {
4882 			vid = i_mac_flow_vid(flent);
4883 			(void) sprintf(flowname, "%s%u", new_name, vid);
4884 			mac_flow_set_name(flent, flowname);
4885 		}
4886 	}
4887 }
4888 
4889 
4890 /*
4891  * Add a flow to the MAC client's flow list - i.e list of MAC/VID tuples
4892  * defined for the specified MAC client.
4893  */
4894 static void
4895 mac_client_add_to_flow_list(mac_client_impl_t *mcip, flow_entry_t *flent)
4896 {
4897 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
4898 	/*
4899 	 * The promisc Rx data path walks the mci_flent_list. Protect by
4900 	 * using mi_rw_lock
4901 	 */
4902 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
4903 
4904 	mcip->mci_vidcache = MCIP_VIDCACHE_INVALID;
4905 
4906 	/* Add it to the head */
4907 	flent->fe_client_next = mcip->mci_flent_list;
4908 	mcip->mci_flent_list = flent;
4909 	mcip->mci_nflents++;
4910 
4911 	/*
4912 	 * Keep track of the number of non-zero VIDs addresses per MAC
4913 	 * client to avoid figuring it out in the data-path.
4914 	 */
4915 	if (i_mac_flow_vid(flent) != VLAN_ID_NONE)
4916 		mcip->mci_nvids++;
4917 
4918 	rw_exit(&mcip->mci_rw_lock);
4919 }
4920 
4921 /*
4922  * Remove a flow entry from the MAC client's list.
4923  */
4924 static void
4925 mac_client_remove_flow_from_list(mac_client_impl_t *mcip, flow_entry_t *flent)
4926 {
4927 	flow_entry_t	*fe = mcip->mci_flent_list;
4928 	flow_entry_t	*prev_fe = NULL;
4929 
4930 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
4931 	/*
4932 	 * The promisc Rx data path walks the mci_flent_list. Protect by
4933 	 * using mci_rw_lock
4934 	 */
4935 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
4936 	mcip->mci_vidcache = MCIP_VIDCACHE_INVALID;
4937 
4938 	while ((fe != NULL) && (fe != flent)) {
4939 		prev_fe = fe;
4940 		fe = fe->fe_client_next;
4941 	}
4942 
4943 	ASSERT(fe != NULL);
4944 	if (prev_fe == NULL) {
4945 		/* Deleting the first node */
4946 		mcip->mci_flent_list = fe->fe_client_next;
4947 	} else {
4948 		prev_fe->fe_client_next = fe->fe_client_next;
4949 	}
4950 	mcip->mci_nflents--;
4951 
4952 	if (i_mac_flow_vid(flent) != VLAN_ID_NONE)
4953 		mcip->mci_nvids--;
4954 
4955 	rw_exit(&mcip->mci_rw_lock);
4956 }
4957 
4958 /*
4959  * Check if the given VID belongs to this MAC client.
4960  */
4961 boolean_t
4962 mac_client_check_flow_vid(mac_client_impl_t *mcip, uint16_t vid)
4963 {
4964 	flow_entry_t	*flent;
4965 	uint16_t	mci_vid;
4966 	uint32_t	cache = mcip->mci_vidcache;
4967 
4968 	/*
4969 	 * In hopes of not having to touch the mci_rw_lock, check to see if
4970 	 * this vid matches our cached result.
4971 	 */
4972 	if (MCIP_VIDCACHE_ISVALID(cache) && MCIP_VIDCACHE_VID(cache) == vid)
4973 		return (MCIP_VIDCACHE_BOOL(cache) ? B_TRUE : B_FALSE);
4974 
4975 	/* The mci_flent_list is protected by mci_rw_lock */
4976 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
4977 	for (flent = mcip->mci_flent_list; flent != NULL;
4978 	    flent = flent->fe_client_next) {
4979 		mci_vid = i_mac_flow_vid(flent);
4980 		if (vid == mci_vid) {
4981 			mcip->mci_vidcache = MCIP_VIDCACHE_CACHE(vid, B_TRUE);
4982 			rw_exit(&mcip->mci_rw_lock);
4983 			return (B_TRUE);
4984 		}
4985 	}
4986 
4987 	mcip->mci_vidcache = MCIP_VIDCACHE_CACHE(vid, B_FALSE);
4988 	rw_exit(&mcip->mci_rw_lock);
4989 	return (B_FALSE);
4990 }
4991 
4992 /*
4993  * Get the flow entry for the specified <MAC addr, VID> tuple.
4994  */
4995 static flow_entry_t *
4996 mac_client_get_flow(mac_client_impl_t *mcip, mac_unicast_impl_t *muip)
4997 {
4998 	mac_address_t *map = mcip->mci_unicast;
4999 	flow_entry_t *flent;
5000 	uint16_t vid;
5001 	flow_desc_t flow_desc;
5002 
5003 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
5004 
5005 	mac_flow_get_desc(mcip->mci_flent, &flow_desc);
5006 	if (bcmp(flow_desc.fd_dst_mac, map->ma_addr, map->ma_len) != 0)
5007 		return (NULL);
5008 
5009 	for (flent = mcip->mci_flent_list; flent != NULL;
5010 	    flent = flent->fe_client_next) {
5011 		vid = i_mac_flow_vid(flent);
5012 		if (vid == muip->mui_vid) {
5013 			return (flent);
5014 		}
5015 	}
5016 
5017 	return (NULL);
5018 }
5019 
5020 /*
5021  * Since mci_flent has the SRSs, when we want to remove it, we replace
5022  * the flow_desc_t in mci_flent with that of an existing flent and then
5023  * remove that flent instead of mci_flent.
5024  */
5025 static flow_entry_t *
5026 mac_client_swap_mciflent(mac_client_impl_t *mcip)
5027 {
5028 	flow_entry_t	*flent = mcip->mci_flent;
5029 	flow_tab_t	*ft = flent->fe_flow_tab;
5030 	flow_entry_t	*flent1;
5031 	flow_desc_t	fl_desc;
5032 	char		fl_name[MAXFLOWNAMELEN];
5033 	int		err;
5034 
5035 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
5036 	ASSERT(mcip->mci_nflents > 1);
5037 
5038 	/* get the next flent following the primary flent  */
5039 	flent1 = mcip->mci_flent_list->fe_client_next;
5040 	ASSERT(flent1 != NULL && flent1->fe_flow_tab == ft);
5041 
5042 	/*
5043 	 * Remove the flent from the flow table before updating the
5044 	 * flow descriptor as the hash depends on the flow descriptor.
5045 	 * This also helps incoming packet classification avoid having
5046 	 * to grab fe_lock. Access to fe_flow_desc of a flent not in the
5047 	 * flow table is done under the fe_lock so that log or stat functions
5048 	 * see a self-consistent fe_flow_desc. The name and desc are specific
5049 	 * to a flow, the rest are shared by all the clients, including
5050 	 * resource control etc.
5051 	 */
5052 	mac_flow_remove(ft, flent, B_TRUE);
5053 	mac_flow_remove(ft, flent1, B_TRUE);
5054 
5055 	bcopy(&flent->fe_flow_desc, &fl_desc, sizeof (flow_desc_t));
5056 	bcopy(flent->fe_flow_name, fl_name, MAXFLOWNAMELEN);
5057 
5058 	/* update the primary flow entry */
5059 	mutex_enter(&flent->fe_lock);
5060 	bcopy(&flent1->fe_flow_desc, &flent->fe_flow_desc,
5061 	    sizeof (flow_desc_t));
5062 	bcopy(&flent1->fe_flow_name, &flent->fe_flow_name, MAXFLOWNAMELEN);
5063 	mutex_exit(&flent->fe_lock);
5064 
5065 	/* update the flow entry that is to be freed */
5066 	mutex_enter(&flent1->fe_lock);
5067 	bcopy(&fl_desc, &flent1->fe_flow_desc, sizeof (flow_desc_t));
5068 	bcopy(fl_name, &flent1->fe_flow_name, MAXFLOWNAMELEN);
5069 	mutex_exit(&flent1->fe_lock);
5070 
5071 	/* now reinsert the flow entries in the table */
5072 	err = mac_flow_add(ft, flent);
5073 	ASSERT(err == 0);
5074 
5075 	err = mac_flow_add(ft, flent1);
5076 	ASSERT(err == 0);
5077 
5078 	return (flent1);
5079 }
5080 
5081 /*
5082  * Return whether there is only one flow entry associated with this
5083  * MAC client.
5084  */
5085 static boolean_t
5086 mac_client_single_rcvr(mac_client_impl_t *mcip)
5087 {
5088 	return (mcip->mci_nflents == 1);
5089 }
5090 
5091 int
5092 mac_validate_props(mac_impl_t *mip, mac_resource_props_t *mrp)
5093 {
5094 	boolean_t		reset;
5095 	uint32_t		rings_needed;
5096 	uint32_t		rings_avail;
5097 	mac_group_type_t	gtype;
5098 	mac_resource_props_t	*mip_mrp;
5099 
5100 	if (mrp == NULL)
5101 		return (0);
5102 
5103 	if (mrp->mrp_mask & MRP_PRIORITY) {
5104 		mac_priority_level_t	pri = mrp->mrp_priority;
5105 
5106 		if (pri < MPL_LOW || pri > MPL_RESET)
5107 			return (EINVAL);
5108 	}
5109 
5110 	if (mrp->mrp_mask & MRP_MAXBW) {
5111 		uint64_t maxbw = mrp->mrp_maxbw;
5112 
5113 		if (maxbw < MRP_MAXBW_MINVAL && maxbw != 0)
5114 			return (EINVAL);
5115 	}
5116 	if (mrp->mrp_mask & MRP_CPUS) {
5117 		int i, j;
5118 		mac_cpu_mode_t	fanout;
5119 
5120 		if (mrp->mrp_ncpus > ncpus)
5121 			return (EINVAL);
5122 
5123 		for (i = 0; i < mrp->mrp_ncpus; i++) {
5124 			for (j = 0; j < mrp->mrp_ncpus; j++) {
5125 				if (i != j &&
5126 				    mrp->mrp_cpu[i] == mrp->mrp_cpu[j]) {
5127 					return (EINVAL);
5128 				}
5129 			}
5130 		}
5131 
5132 		for (i = 0; i < mrp->mrp_ncpus; i++) {
5133 			cpu_t *cp;
5134 			int rv;
5135 
5136 			mutex_enter(&cpu_lock);
5137 			cp = cpu_get(mrp->mrp_cpu[i]);
5138 			if (cp != NULL)
5139 				rv = cpu_is_online(cp);
5140 			else
5141 				rv = 0;
5142 			mutex_exit(&cpu_lock);
5143 			if (rv == 0)
5144 				return (EINVAL);
5145 		}
5146 
5147 		fanout = mrp->mrp_fanout_mode;
5148 		if (fanout < 0 || fanout > MCM_CPUS)
5149 			return (EINVAL);
5150 	}
5151 
5152 	if (mrp->mrp_mask & MRP_PROTECT) {
5153 		int err = mac_protect_validate(mrp);
5154 		if (err != 0)
5155 			return (err);
5156 	}
5157 
5158 	if (!(mrp->mrp_mask & MRP_RX_RINGS) &&
5159 	    !(mrp->mrp_mask & MRP_TX_RINGS)) {
5160 		return (0);
5161 	}
5162 
5163 	/*
5164 	 * mip will be null when we come from mac_flow_create or
5165 	 * mac_link_flow_modify. In the latter case it is a user flow,
5166 	 * for which we don't support rings. In the former we would
5167 	 * have validated the props beforehand (i_mac_unicast_add ->
5168 	 * mac_client_set_resources -> validate for the primary and
5169 	 * vnic_dev_create -> mac_client_set_resources -> validate for
5170 	 * a vnic.
5171 	 */
5172 	if (mip == NULL)
5173 		return (0);
5174 
5175 	/*
5176 	 * We don't support setting rings property for a VNIC that is using a
5177 	 * primary address (VLAN)
5178 	 */
5179 	if ((mip->mi_state_flags & MIS_IS_VNIC) &&
5180 	    mac_is_vnic_primary((mac_handle_t)mip)) {
5181 		return (ENOTSUP);
5182 	}
5183 
5184 	mip_mrp = &mip->mi_resource_props;
5185 	/*
5186 	 * The rings property should be validated against the NICs
5187 	 * resources
5188 	 */
5189 	if (mip->mi_state_flags & MIS_IS_VNIC)
5190 		mip = (mac_impl_t *)mac_get_lower_mac_handle((mac_handle_t)mip);
5191 
5192 	reset = mrp->mrp_mask & MRP_RINGS_RESET;
5193 	/*
5194 	 * If groups are not supported, return error.
5195 	 */
5196 	if (((mrp->mrp_mask & MRP_RX_RINGS) && mip->mi_rx_groups == NULL) ||
5197 	    ((mrp->mrp_mask & MRP_TX_RINGS) && mip->mi_tx_groups == NULL)) {
5198 		return (EINVAL);
5199 	}
5200 	/*
5201 	 * If we are just resetting, there is no validation needed.
5202 	 */
5203 	if (reset)
5204 		return (0);
5205 
5206 	if (mrp->mrp_mask & MRP_RX_RINGS) {
5207 		rings_needed = mrp->mrp_nrxrings;
5208 		/*
5209 		 * We just want to check if the number of additional
5210 		 * rings requested is available.
5211 		 */
5212 		if (mip_mrp->mrp_mask & MRP_RX_RINGS) {
5213 			if (mrp->mrp_nrxrings > mip_mrp->mrp_nrxrings)
5214 				/* Just check for the additional rings */
5215 				rings_needed -= mip_mrp->mrp_nrxrings;
5216 			else
5217 				/* We are not asking for additional rings */
5218 				rings_needed = 0;
5219 		}
5220 		rings_avail = mip->mi_rxrings_avail;
5221 		gtype = mip->mi_rx_group_type;
5222 	} else {
5223 		rings_needed = mrp->mrp_ntxrings;
5224 		/* Similarly for the TX rings */
5225 		if (mip_mrp->mrp_mask & MRP_TX_RINGS) {
5226 			if (mrp->mrp_ntxrings > mip_mrp->mrp_ntxrings)
5227 				/* Just check for the additional rings */
5228 				rings_needed -= mip_mrp->mrp_ntxrings;
5229 			else
5230 				/* We are not asking for additional rings */
5231 				rings_needed = 0;
5232 		}
5233 		rings_avail = mip->mi_txrings_avail;
5234 		gtype = mip->mi_tx_group_type;
5235 	}
5236 
5237 	/* Error if the group is dynamic .. */
5238 	if (gtype == MAC_GROUP_TYPE_DYNAMIC) {
5239 		/*
5240 		 * .. and rings specified are more than available.
5241 		 */
5242 		if (rings_needed > rings_avail)
5243 			return (EINVAL);
5244 	} else {
5245 		/*
5246 		 * OR group is static and we have specified some rings.
5247 		 */
5248 		if (rings_needed > 0)
5249 			return (EINVAL);
5250 	}
5251 	return (0);
5252 }
5253 
5254 /*
5255  * Send a MAC_NOTE_LINK notification to all the MAC clients whenever the
5256  * underlying physical link is down. This is to allow MAC clients to
5257  * communicate with other clients.
5258  */
5259 void
5260 mac_virtual_link_update(mac_impl_t *mip)
5261 {
5262 	if (mip->mi_linkstate != LINK_STATE_UP)
5263 		i_mac_notify(mip, MAC_NOTE_LINK);
5264 }
5265 
5266 /*
5267  * For clients that have a pass-thru MAC, e.g. VNIC, we set the VNIC's
5268  * mac handle in the client.
5269  */
5270 void
5271 mac_set_upper_mac(mac_client_handle_t mch, mac_handle_t mh,
5272     mac_resource_props_t *mrp)
5273 {
5274 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
5275 	mac_impl_t		*mip = (mac_impl_t *)mh;
5276 
5277 	mcip->mci_upper_mip = mip;
5278 	/* If there are any properties, copy it over too */
5279 	if (mrp != NULL) {
5280 		bcopy(mrp, &mip->mi_resource_props,
5281 		    sizeof (mac_resource_props_t));
5282 	}
5283 }
5284 
5285 /*
5286  * Mark the mac as being used exclusively by the single mac client that is
5287  * doing some control operation on this mac. No further opens of this mac
5288  * will be allowed until this client calls mac_unmark_exclusive. The mac
5289  * client calling this function must already be in the mac perimeter
5290  */
5291 int
5292 mac_mark_exclusive(mac_handle_t mh)
5293 {
5294 	mac_impl_t	*mip = (mac_impl_t *)mh;
5295 
5296 	ASSERT(MAC_PERIM_HELD(mh));
5297 	/*
5298 	 * Look up its entry in the global hash table.
5299 	 */
5300 	rw_enter(&i_mac_impl_lock, RW_WRITER);
5301 	if (mip->mi_state_flags & MIS_DISABLED) {
5302 		rw_exit(&i_mac_impl_lock);
5303 		return (ENOENT);
5304 	}
5305 
5306 	/*
5307 	 * A reference to mac is held even if the link is not plumbed.
5308 	 * In i_dls_link_create() we open the MAC interface and hold the
5309 	 * reference. There is an additional reference for the mac_open
5310 	 * done in acquiring the mac perimeter
5311 	 */
5312 	if (mip->mi_ref != 2) {
5313 		rw_exit(&i_mac_impl_lock);
5314 		return (EBUSY);
5315 	}
5316 
5317 	ASSERT(!(mip->mi_state_flags & MIS_EXCLUSIVE_HELD));
5318 	mip->mi_state_flags |= MIS_EXCLUSIVE_HELD;
5319 	rw_exit(&i_mac_impl_lock);
5320 	return (0);
5321 }
5322 
5323 void
5324 mac_unmark_exclusive(mac_handle_t mh)
5325 {
5326 	mac_impl_t	*mip = (mac_impl_t *)mh;
5327 
5328 	ASSERT(MAC_PERIM_HELD(mh));
5329 
5330 	rw_enter(&i_mac_impl_lock, RW_WRITER);
5331 	/* 1 for the creation and another for the perimeter */
5332 	ASSERT(mip->mi_ref == 2 && (mip->mi_state_flags & MIS_EXCLUSIVE_HELD));
5333 	mip->mi_state_flags &= ~MIS_EXCLUSIVE_HELD;
5334 	rw_exit(&i_mac_impl_lock);
5335 }
5336 
5337 /*
5338  * Set the MTU for the specified MAC.
5339  */
5340 int
5341 mac_set_mtu(mac_handle_t mh, uint_t new_mtu, uint_t *old_mtu_arg)
5342 {
5343 	mac_impl_t *mip = (mac_impl_t *)mh;
5344 	uint_t old_mtu;
5345 	int rv = 0;
5346 
5347 	i_mac_perim_enter(mip);
5348 
5349 	if (!(mip->mi_callbacks->mc_callbacks & (MC_SETPROP|MC_GETPROP))) {
5350 		rv = ENOTSUP;
5351 		goto bail;
5352 	}
5353 
5354 	old_mtu = mip->mi_sdu_max;
5355 
5356 	if (new_mtu == 0 || new_mtu < mip->mi_sdu_min) {
5357 		rv = EINVAL;
5358 		goto bail;
5359 	}
5360 
5361 	rw_enter(&mip->mi_rw_lock, RW_READER);
5362 	if (mip->mi_mtrp != NULL && new_mtu < mip->mi_mtrp->mtr_mtu) {
5363 		rv = EBUSY;
5364 		rw_exit(&mip->mi_rw_lock);
5365 		goto bail;
5366 	}
5367 	rw_exit(&mip->mi_rw_lock);
5368 
5369 	if (old_mtu != new_mtu) {
5370 		rv = mip->mi_callbacks->mc_setprop(mip->mi_driver,
5371 		    "mtu", MAC_PROP_MTU, sizeof (uint_t), &new_mtu);
5372 		if (rv != 0)
5373 			goto bail;
5374 		rv = mac_maxsdu_update(mh, new_mtu);
5375 		ASSERT(rv == 0);
5376 	}
5377 
5378 bail:
5379 	i_mac_perim_exit(mip);
5380 
5381 	if (rv == 0 && old_mtu_arg != NULL)
5382 		*old_mtu_arg = old_mtu;
5383 	return (rv);
5384 }
5385 
5386 /*
5387  * Return the RX h/w information for the group indexed by grp_num.
5388  */
5389 void
5390 mac_get_hwrxgrp_info(mac_handle_t mh, int grp_index, uint_t *grp_num,
5391     uint_t *n_rings, uint_t *rings, uint_t *type, uint_t *n_clnts,
5392     char *clnts_name)
5393 {
5394 	mac_impl_t *mip = (mac_impl_t *)mh;
5395 	mac_grp_client_t *mcip;
5396 	uint_t i = 0, index = 0;
5397 	mac_ring_t	*ring;
5398 
5399 	/* Revisit when we implement fully dynamic group allocation */
5400 	ASSERT(grp_index >= 0 && grp_index < mip->mi_rx_group_count);
5401 
5402 	rw_enter(&mip->mi_rw_lock, RW_READER);
5403 	*grp_num = mip->mi_rx_groups[grp_index].mrg_index;
5404 	*type = mip->mi_rx_groups[grp_index].mrg_type;
5405 	*n_rings = mip->mi_rx_groups[grp_index].mrg_cur_count;
5406 	ring = mip->mi_rx_groups[grp_index].mrg_rings;
5407 	for (index = 0; index < mip->mi_rx_groups[grp_index].mrg_cur_count;
5408 	    index++) {
5409 		rings[index] = ring->mr_index;
5410 		ring = ring->mr_next;
5411 	}
5412 	/* Assuming the 1st is the default group */
5413 	index = 0;
5414 	if (grp_index == 0) {
5415 		(void) strlcpy(clnts_name, "<default,mcast>,",
5416 		    MAXCLIENTNAMELEN);
5417 		index += strlen("<default,mcast>,");
5418 	}
5419 	for (mcip = mip->mi_rx_groups[grp_index].mrg_clients; mcip != NULL;
5420 	    mcip = mcip->mgc_next) {
5421 		int name_len = strlen(mcip->mgc_client->mci_name);
5422 
5423 		/*
5424 		 * MAXCLIENTNAMELEN is the buffer size reserved for client
5425 		 * names.
5426 		 * XXXX Formating the client name string needs to be moved
5427 		 * to user land when fixing the size of dhi_clnts in
5428 		 * dld_hwgrpinfo_t. We should use n_clients * client_name for
5429 		 * dhi_clntsin instead of MAXCLIENTNAMELEN
5430 		 */
5431 		if (index + name_len >= MAXCLIENTNAMELEN) {
5432 			index = MAXCLIENTNAMELEN;
5433 			break;
5434 		}
5435 		bcopy(mcip->mgc_client->mci_name, &(clnts_name[index]),
5436 		    name_len);
5437 		index += name_len;
5438 		clnts_name[index++] = ',';
5439 		i++;
5440 	}
5441 
5442 	/* Get rid of the last , */
5443 	if (index > 0)
5444 		clnts_name[index - 1] = '\0';
5445 	*n_clnts = i;
5446 	rw_exit(&mip->mi_rw_lock);
5447 }
5448 
5449 /*
5450  * Return the TX h/w information for the group indexed by grp_num.
5451  */
5452 void
5453 mac_get_hwtxgrp_info(mac_handle_t mh, int grp_index, uint_t *grp_num,
5454     uint_t *n_rings, uint_t *rings, uint_t *type, uint_t *n_clnts,
5455     char *clnts_name)
5456 {
5457 	mac_impl_t *mip = (mac_impl_t *)mh;
5458 	mac_grp_client_t *mcip;
5459 	uint_t i = 0, index = 0;
5460 	mac_ring_t	*ring;
5461 
5462 	/* Revisit when we implement fully dynamic group allocation */
5463 	ASSERT(grp_index >= 0 && grp_index <= mip->mi_tx_group_count);
5464 
5465 	rw_enter(&mip->mi_rw_lock, RW_READER);
5466 	*grp_num = mip->mi_tx_groups[grp_index].mrg_index > 0 ?
5467 	    mip->mi_tx_groups[grp_index].mrg_index : grp_index;
5468 	*type = mip->mi_tx_groups[grp_index].mrg_type;
5469 	*n_rings = mip->mi_tx_groups[grp_index].mrg_cur_count;
5470 	ring = mip->mi_tx_groups[grp_index].mrg_rings;
5471 	for (index = 0; index < mip->mi_tx_groups[grp_index].mrg_cur_count;
5472 	    index++) {
5473 		rings[index] = ring->mr_index;
5474 		ring = ring->mr_next;
5475 	}
5476 	index = 0;
5477 	/* Default group has an index of -1 */
5478 	if (mip->mi_tx_groups[grp_index].mrg_index < 0) {
5479 		(void) strlcpy(clnts_name, "<default>,",
5480 		    MAXCLIENTNAMELEN);
5481 		index += strlen("<default>,");
5482 	}
5483 	for (mcip = mip->mi_tx_groups[grp_index].mrg_clients; mcip != NULL;
5484 	    mcip = mcip->mgc_next) {
5485 		int name_len = strlen(mcip->mgc_client->mci_name);
5486 
5487 		/*
5488 		 * MAXCLIENTNAMELEN is the buffer size reserved for client
5489 		 * names.
5490 		 * XXXX Formating the client name string needs to be moved
5491 		 * to user land when fixing the size of dhi_clnts in
5492 		 * dld_hwgrpinfo_t. We should use n_clients * client_name for
5493 		 * dhi_clntsin instead of MAXCLIENTNAMELEN
5494 		 */
5495 		if (index + name_len >= MAXCLIENTNAMELEN) {
5496 			index = MAXCLIENTNAMELEN;
5497 			break;
5498 		}
5499 		bcopy(mcip->mgc_client->mci_name, &(clnts_name[index]),
5500 		    name_len);
5501 		index += name_len;
5502 		clnts_name[index++] = ',';
5503 		i++;
5504 	}
5505 
5506 	/* Get rid of the last , */
5507 	if (index > 0)
5508 		clnts_name[index - 1] = '\0';
5509 	*n_clnts = i;
5510 	rw_exit(&mip->mi_rw_lock);
5511 }
5512 
5513 /*
5514  * Return the group count for RX or TX.
5515  */
5516 uint_t
5517 mac_hwgrp_num(mac_handle_t mh, int type)
5518 {
5519 	mac_impl_t *mip = (mac_impl_t *)mh;
5520 
5521 	/*
5522 	 * Return the Rx and Tx group count; for the Tx we need to
5523 	 * include the default too.
5524 	 */
5525 	return (type == MAC_RING_TYPE_RX ? mip->mi_rx_group_count :
5526 	    mip->mi_tx_groups != NULL ? mip->mi_tx_group_count + 1 : 0);
5527 }
5528 
5529 /*
5530  * The total number of free TX rings for this MAC.
5531  */
5532 uint_t
5533 mac_txavail_get(mac_handle_t mh)
5534 {
5535 	mac_impl_t	*mip = (mac_impl_t *)mh;
5536 
5537 	return (mip->mi_txrings_avail);
5538 }
5539 
5540 /*
5541  * The total number of free RX rings for this MAC.
5542  */
5543 uint_t
5544 mac_rxavail_get(mac_handle_t mh)
5545 {
5546 	mac_impl_t	*mip = (mac_impl_t *)mh;
5547 
5548 	return (mip->mi_rxrings_avail);
5549 }
5550 
5551 /*
5552  * The total number of reserved RX rings on this MAC.
5553  */
5554 uint_t
5555 mac_rxrsvd_get(mac_handle_t mh)
5556 {
5557 	mac_impl_t	*mip = (mac_impl_t *)mh;
5558 
5559 	return (mip->mi_rxrings_rsvd);
5560 }
5561 
5562 /*
5563  * The total number of reserved TX rings on this MAC.
5564  */
5565 uint_t
5566 mac_txrsvd_get(mac_handle_t mh)
5567 {
5568 	mac_impl_t	*mip = (mac_impl_t *)mh;
5569 
5570 	return (mip->mi_txrings_rsvd);
5571 }
5572 
5573 /*
5574  * Total number of free RX groups on this MAC.
5575  */
5576 uint_t
5577 mac_rxhwlnksavail_get(mac_handle_t mh)
5578 {
5579 	mac_impl_t	*mip = (mac_impl_t *)mh;
5580 
5581 	return (mip->mi_rxhwclnt_avail);
5582 }
5583 
5584 /*
5585  * Total number of RX groups reserved on this MAC.
5586  */
5587 uint_t
5588 mac_rxhwlnksrsvd_get(mac_handle_t mh)
5589 {
5590 	mac_impl_t	*mip = (mac_impl_t *)mh;
5591 
5592 	return (mip->mi_rxhwclnt_used);
5593 }
5594 
5595 /*
5596  * Total number of free TX groups on this MAC.
5597  */
5598 uint_t
5599 mac_txhwlnksavail_get(mac_handle_t mh)
5600 {
5601 	mac_impl_t	*mip = (mac_impl_t *)mh;
5602 
5603 	return (mip->mi_txhwclnt_avail);
5604 }
5605 
5606 /*
5607  * Total number of TX groups reserved on this MAC.
5608  */
5609 uint_t
5610 mac_txhwlnksrsvd_get(mac_handle_t mh)
5611 {
5612 	mac_impl_t	*mip = (mac_impl_t *)mh;
5613 
5614 	return (mip->mi_txhwclnt_used);
5615 }
5616 
5617 /*
5618  * Initialize the rings property for a mac client. A non-0 value for
5619  * rxring or txring specifies the number of rings required, a value
5620  * of MAC_RXRINGS_NONE/MAC_TXRINGS_NONE specifies that it doesn't need
5621  * any RX/TX rings and a value of MAC_RXRINGS_DONTCARE/MAC_TXRINGS_DONTCARE
5622  * means the system can decide whether it can give any rings or not.
5623  */
5624 void
5625 mac_client_set_rings(mac_client_handle_t mch, int rxrings, int txrings)
5626 {
5627 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
5628 	mac_resource_props_t	*mrp = MCIP_RESOURCE_PROPS(mcip);
5629 
5630 	if (rxrings != MAC_RXRINGS_DONTCARE) {
5631 		mrp->mrp_mask |= MRP_RX_RINGS;
5632 		mrp->mrp_nrxrings = rxrings;
5633 	}
5634 
5635 	if (txrings != MAC_TXRINGS_DONTCARE) {
5636 		mrp->mrp_mask |= MRP_TX_RINGS;
5637 		mrp->mrp_ntxrings = txrings;
5638 	}
5639 }
5640 
5641 boolean_t
5642 mac_get_promisc_filtered(mac_client_handle_t mch)
5643 {
5644 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
5645 
5646 	return (mcip->mci_protect_flags & MPT_FLAG_PROMISC_FILTERED);
5647 }
5648 
5649 void
5650 mac_set_promisc_filtered(mac_client_handle_t mch, boolean_t enable)
5651 {
5652 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
5653 
5654 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
5655 	if (enable)
5656 		mcip->mci_protect_flags |= MPT_FLAG_PROMISC_FILTERED;
5657 	else
5658 		mcip->mci_protect_flags &= ~MPT_FLAG_PROMISC_FILTERED;
5659 }
5660