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