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