xref: /titanic_51/usr/src/uts/common/io/mac/mac_client.c (revision 7380b00ccf9fc5638da98d756925bc6d6c002a42)
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 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * - General Introduction:
29  *
30  * This file contains the implementation of the MAC client kernel
31  * API and related code. The MAC client API allows a kernel module
32  * to gain access to a MAC instance (physical NIC, link aggregation, etc).
33  * It allows a MAC client to associate itself with a MAC address,
34  * VLANs, callback functions for data traffic and for promiscuous mode.
35  * The MAC client API is also used to specify the properties associated
36  * with a MAC client, such as bandwidth limits, priority, CPUS, etc.
37  * These properties are further used to determine the hardware resources
38  * to allocate to the various MAC clients.
39  *
40  * - Primary MAC clients:
41  *
42  * The MAC client API refers to "primary MAC clients". A primary MAC
43  * client is a client which "owns" the primary MAC address of
44  * the underlying MAC instance. The primary MAC address is called out
45  * since it is associated with specific semantics: the primary MAC
46  * address is the MAC address which is assigned to the IP interface
47  * when it is plumbed, and the primary MAC address is assigned
48  * to VLAN data-links. The primary address of a MAC instance can
49  * also change dynamically from under the MAC client, for example
50  * as a result of a change of state of a link aggregation. In that
51  * case the MAC layer automatically updates all data-structures which
52  * refer to the current value of the primary MAC address. Typical
53  * primary MAC clients are dls, aggr, and xnb. A typical non-primary
54  * MAC client is the vnic driver.
55  *
56  * - Virtual Switching:
57  *
58  * The MAC layer implements a virtual switch between the MAC clients
59  * (primary and non-primary) defined on top of the same underlying
60  * NIC (physical, link aggregation, etc). The virtual switch is
61  * VLAN-aware, i.e. it allows multiple MAC clients to be member
62  * of one or more VLANs, and the virtual switch will distribute
63  * multicast tagged packets only to the member of the corresponding
64  * VLANs.
65  *
66  * - Upper vs Lower MAC:
67  *
68  * Creating a VNIC on top of a MAC instance effectively causes
69  * two MAC instances to be layered on top of each other, one for
70  * the VNIC(s), one for the underlying MAC instance (physical NIC,
71  * link aggregation, etc). In the code below we refer to the
72  * underlying NIC as the "lower MAC", and we refer to VNICs as
73  * the "upper MAC".
74  *
75  * - Pass-through for VNICs:
76  *
77  * When VNICs are created on top of an underlying MAC, this causes
78  * a layering of two MAC instances. Since the lower MAC already
79  * does the switching and demultiplexing to its MAC clients, the
80  * upper MAC would simply have to pass packets to the layer below
81  * or above it, which would introduce overhead. In order to avoid
82  * this overhead, the MAC layer implements a pass-through mechanism
83  * for VNICs. When a VNIC opens the lower MAC instance, it saves
84  * the MAC client handle it optains from the MAC layer. When a MAC
85  * client opens a VNIC (upper MAC), the MAC layer detects that
86  * the MAC being opened is a VNIC, and gets the MAC client handle
87  * that the VNIC driver obtained from the lower MAC. This exchange
88  * is doing through a private capability between the MAC layer
89  * and the VNIC driver. The upper MAC then returns that handle
90  * directly to its MAC client. Any operation done by the upper
91  * MAC client is now done on the lower MAC client handle, which
92  * allows the VNIC driver to be completely bypassed for the
93  * performance sensitive data-path.
94  *
95  */
96 
97 #include <sys/types.h>
98 #include <sys/conf.h>
99 #include <sys/id_space.h>
100 #include <sys/esunddi.h>
101 #include <sys/stat.h>
102 #include <sys/mkdev.h>
103 #include <sys/stream.h>
104 #include <sys/strsun.h>
105 #include <sys/strsubr.h>
106 #include <sys/dlpi.h>
107 #include <sys/modhash.h>
108 #include <sys/mac_impl.h>
109 #include <sys/mac_client_impl.h>
110 #include <sys/mac_soft_ring.h>
111 #include <sys/dls.h>
112 #include <sys/dld.h>
113 #include <sys/modctl.h>
114 #include <sys/fs/dv_node.h>
115 #include <sys/thread.h>
116 #include <sys/proc.h>
117 #include <sys/callb.h>
118 #include <sys/cpuvar.h>
119 #include <sys/atomic.h>
120 #include <sys/sdt.h>
121 #include <sys/mac_flow.h>
122 #include <sys/ddi_intr_impl.h>
123 #include <sys/disp.h>
124 #include <sys/sdt.h>
125 #include <sys/vnic.h>
126 #include <sys/vnic_impl.h>
127 #include <sys/vlan.h>
128 #include <inet/ip.h>
129 #include <inet/ip6.h>
130 #include <sys/exacct.h>
131 #include <sys/exacct_impl.h>
132 #include <inet/nd.h>
133 #include <sys/ethernet.h>
134 
135 kmem_cache_t	*mac_client_impl_cache;
136 kmem_cache_t	*mac_promisc_impl_cache;
137 
138 static boolean_t mac_client_single_rcvr(mac_client_impl_t *);
139 static flow_entry_t *mac_client_swap_mciflent(mac_client_impl_t *);
140 static flow_entry_t *mac_client_get_flow(mac_client_impl_t *,
141     mac_unicast_impl_t *);
142 static void mac_client_remove_flow_from_list(mac_client_impl_t *,
143     flow_entry_t *);
144 static void mac_client_add_to_flow_list(mac_client_impl_t *, flow_entry_t *);
145 static void mac_rename_flow_names(mac_client_impl_t *, const char *);
146 static void mac_virtual_link_update(mac_impl_t *);
147 
148 /* ARGSUSED */
149 static int
150 i_mac_client_impl_ctor(void *buf, void *arg, int kmflag)
151 {
152 	int	i;
153 	mac_client_impl_t	*mcip = buf;
154 
155 	bzero(buf, MAC_CLIENT_IMPL_SIZE);
156 	mutex_init(&mcip->mci_tx_cb_lock, NULL, MUTEX_DRIVER, NULL);
157 	mcip->mci_tx_notify_cb_info.mcbi_lockp = &mcip->mci_tx_cb_lock;
158 
159 	ASSERT(mac_tx_percpu_cnt >= 0);
160 	for (i = 0; i <= mac_tx_percpu_cnt; i++) {
161 		mutex_init(&mcip->mci_tx_pcpu[i].pcpu_tx_lock, NULL,
162 		    MUTEX_DRIVER, NULL);
163 	}
164 	cv_init(&mcip->mci_tx_cv, NULL, CV_DRIVER, NULL);
165 
166 	return (0);
167 }
168 
169 /* ARGSUSED */
170 static void
171 i_mac_client_impl_dtor(void *buf, void *arg)
172 {
173 	int	i;
174 	mac_client_impl_t *mcip = buf;
175 
176 	ASSERT(mcip->mci_promisc_list == NULL);
177 	ASSERT(mcip->mci_unicast_list == NULL);
178 	ASSERT(mcip->mci_state_flags == 0);
179 	ASSERT(mcip->mci_tx_flag == 0);
180 
181 	mutex_destroy(&mcip->mci_tx_cb_lock);
182 
183 	ASSERT(mac_tx_percpu_cnt >= 0);
184 	for (i = 0; i <= mac_tx_percpu_cnt; i++) {
185 		ASSERT(mcip->mci_tx_pcpu[i].pcpu_tx_refcnt == 0);
186 		mutex_destroy(&mcip->mci_tx_pcpu[i].pcpu_tx_lock);
187 	}
188 	cv_destroy(&mcip->mci_tx_cv);
189 }
190 
191 /* ARGSUSED */
192 static int
193 i_mac_promisc_impl_ctor(void *buf, void *arg, int kmflag)
194 {
195 	mac_promisc_impl_t	*mpip = buf;
196 
197 	bzero(buf, sizeof (mac_promisc_impl_t));
198 	mpip->mpi_mci_link.mcb_objp = buf;
199 	mpip->mpi_mci_link.mcb_objsize = sizeof (mac_promisc_impl_t);
200 	mpip->mpi_mi_link.mcb_objp = buf;
201 	mpip->mpi_mi_link.mcb_objsize = sizeof (mac_promisc_impl_t);
202 	return (0);
203 }
204 
205 /* ARGSUSED */
206 static void
207 i_mac_promisc_impl_dtor(void *buf, void *arg)
208 {
209 	mac_promisc_impl_t	*mpip = buf;
210 
211 	ASSERT(mpip->mpi_mci_link.mcb_objp != NULL);
212 	ASSERT(mpip->mpi_mci_link.mcb_objsize == sizeof (mac_promisc_impl_t));
213 	ASSERT(mpip->mpi_mi_link.mcb_objp == mpip->mpi_mci_link.mcb_objp);
214 	ASSERT(mpip->mpi_mi_link.mcb_objsize == sizeof (mac_promisc_impl_t));
215 
216 	mpip->mpi_mci_link.mcb_objp = NULL;
217 	mpip->mpi_mci_link.mcb_objsize = 0;
218 	mpip->mpi_mi_link.mcb_objp = NULL;
219 	mpip->mpi_mi_link.mcb_objsize = 0;
220 
221 	ASSERT(mpip->mpi_mci_link.mcb_flags == 0);
222 	mpip->mpi_mci_link.mcb_objsize = 0;
223 }
224 
225 void
226 mac_client_init(void)
227 {
228 	ASSERT(mac_tx_percpu_cnt >= 0);
229 
230 	mac_client_impl_cache = kmem_cache_create("mac_client_impl_cache",
231 	    MAC_CLIENT_IMPL_SIZE, 0, i_mac_client_impl_ctor,
232 	    i_mac_client_impl_dtor, NULL, NULL, NULL, 0);
233 	ASSERT(mac_client_impl_cache != NULL);
234 
235 	mac_promisc_impl_cache = kmem_cache_create("mac_promisc_impl_cache",
236 	    sizeof (mac_promisc_impl_t), 0, i_mac_promisc_impl_ctor,
237 	    i_mac_promisc_impl_dtor, NULL, NULL, NULL, 0);
238 	ASSERT(mac_promisc_impl_cache != NULL);
239 }
240 
241 void
242 mac_client_fini(void)
243 {
244 	kmem_cache_destroy(mac_client_impl_cache);
245 	kmem_cache_destroy(mac_promisc_impl_cache);
246 }
247 
248 /*
249  * Return the lower MAC client handle from the VNIC driver for the
250  * specified VNIC MAC instance.
251  */
252 mac_client_impl_t *
253 mac_vnic_lower(mac_impl_t *mip)
254 {
255 	mac_capab_vnic_t cap;
256 	mac_client_impl_t *mcip;
257 
258 	VERIFY(i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_VNIC, &cap));
259 	mcip = cap.mcv_mac_client_handle(cap.mcv_arg);
260 
261 	return (mcip);
262 }
263 
264 /*
265  * Return the MAC client handle of the primary MAC client for the
266  * specified MAC instance, or NULL otherwise.
267  */
268 mac_client_impl_t *
269 mac_primary_client_handle(mac_impl_t *mip)
270 {
271 	mac_client_impl_t *mcip;
272 
273 	if (mip->mi_state_flags & MIS_IS_VNIC)
274 		return (mac_vnic_lower(mip));
275 
276 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
277 
278 	for (mcip = mip->mi_clients_list; mcip != NULL;
279 	    mcip = mcip->mci_client_next) {
280 		if (MCIP_DATAPATH_SETUP(mcip) && mac_is_primary_client(mcip))
281 			return (mcip);
282 	}
283 	return (NULL);
284 }
285 
286 /*
287  * Open a MAC specified by its MAC name.
288  */
289 int
290 mac_open(const char *macname, mac_handle_t *mhp)
291 {
292 	mac_impl_t	*mip;
293 	int		err;
294 
295 	/*
296 	 * Look up its entry in the global hash table.
297 	 */
298 	if ((err = mac_hold(macname, &mip)) != 0)
299 		return (err);
300 
301 	/*
302 	 * Hold the dip associated to the MAC to prevent it from being
303 	 * detached. For a softmac, its underlying dip is held by the
304 	 * mi_open() callback.
305 	 *
306 	 * This is done to be more tolerant with some defective drivers,
307 	 * which incorrectly handle mac_unregister() failure in their
308 	 * xxx_detach() routine. For example, some drivers ignore the
309 	 * failure of mac_unregister() and free all resources that
310 	 * that are needed for data transmition.
311 	 */
312 	e_ddi_hold_devi(mip->mi_dip);
313 
314 	if (!(mip->mi_callbacks->mc_callbacks & MC_OPEN)) {
315 		*mhp = (mac_handle_t)mip;
316 		return (0);
317 	}
318 
319 	/*
320 	 * The mac perimeter is used in both mac_open and mac_close by the
321 	 * framework to single thread the MC_OPEN/MC_CLOSE of drivers.
322 	 */
323 	i_mac_perim_enter(mip);
324 	mip->mi_oref++;
325 	if (mip->mi_oref != 1 || ((err = mip->mi_open(mip->mi_driver)) == 0)) {
326 		*mhp = (mac_handle_t)mip;
327 		i_mac_perim_exit(mip);
328 		return (0);
329 	}
330 	mip->mi_oref--;
331 	ddi_release_devi(mip->mi_dip);
332 	mac_rele(mip);
333 	i_mac_perim_exit(mip);
334 	return (err);
335 }
336 
337 /*
338  * Open a MAC specified by its linkid.
339  */
340 int
341 mac_open_by_linkid(datalink_id_t linkid, mac_handle_t *mhp)
342 {
343 	dls_dl_handle_t	dlh;
344 	int		err;
345 
346 	if ((err = dls_devnet_hold_tmp(linkid, &dlh)) != 0)
347 		return (err);
348 
349 	dls_devnet_prop_task_wait(dlh);
350 
351 	err = mac_open(dls_devnet_mac(dlh), mhp);
352 
353 	dls_devnet_rele_tmp(dlh);
354 	return (err);
355 }
356 
357 /*
358  * Open a MAC specified by its link name.
359  */
360 int
361 mac_open_by_linkname(const char *link, mac_handle_t *mhp)
362 {
363 	datalink_id_t	linkid;
364 	int		err;
365 
366 	if ((err = dls_mgmt_get_linkid(link, &linkid)) != 0)
367 		return (err);
368 	return (mac_open_by_linkid(linkid, mhp));
369 }
370 
371 /*
372  * Close the specified MAC.
373  */
374 void
375 mac_close(mac_handle_t mh)
376 {
377 	mac_impl_t	*mip = (mac_impl_t *)mh;
378 
379 	i_mac_perim_enter(mip);
380 	/*
381 	 * The mac perimeter is used in both mac_open and mac_close by the
382 	 * framework to single thread the MC_OPEN/MC_CLOSE of drivers.
383 	 */
384 	if (mip->mi_callbacks->mc_callbacks & MC_OPEN) {
385 		ASSERT(mip->mi_oref != 0);
386 		if (--mip->mi_oref == 0) {
387 			if ((mip->mi_callbacks->mc_callbacks & MC_CLOSE))
388 				mip->mi_close(mip->mi_driver);
389 		}
390 	}
391 	i_mac_perim_exit(mip);
392 	ddi_release_devi(mip->mi_dip);
393 	mac_rele(mip);
394 }
395 
396 /*
397  * Misc utility functions to retrieve various information about a MAC
398  * instance or a MAC client.
399  */
400 
401 const mac_info_t *
402 mac_info(mac_handle_t mh)
403 {
404 	return (&((mac_impl_t *)mh)->mi_info);
405 }
406 
407 dev_info_t *
408 mac_devinfo_get(mac_handle_t mh)
409 {
410 	return (((mac_impl_t *)mh)->mi_dip);
411 }
412 
413 void *
414 mac_driver(mac_handle_t mh)
415 {
416 	return (((mac_impl_t *)mh)->mi_driver);
417 }
418 
419 const char *
420 mac_name(mac_handle_t mh)
421 {
422 	return (((mac_impl_t *)mh)->mi_name);
423 }
424 
425 char *
426 mac_client_name(mac_client_handle_t mch)
427 {
428 	return (((mac_client_impl_t *)mch)->mci_name);
429 }
430 
431 minor_t
432 mac_minor(mac_handle_t mh)
433 {
434 	return (((mac_impl_t *)mh)->mi_minor);
435 }
436 
437 /*
438  * Return the VID associated with a MAC client. This function should
439  * be called for clients which are associated with only one VID.
440  */
441 uint16_t
442 mac_client_vid(mac_client_handle_t mch)
443 {
444 	uint16_t		vid = VLAN_ID_NONE;
445 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
446 	flow_desc_t		flow_desc;
447 
448 	if (mcip->mci_nflents == 0)
449 		return (vid);
450 
451 	ASSERT(MCIP_DATAPATH_SETUP(mcip) && mac_client_single_rcvr(mcip));
452 
453 	mac_flow_get_desc(mcip->mci_flent, &flow_desc);
454 	if ((flow_desc.fd_mask & FLOW_LINK_VID) != 0)
455 		vid = flow_desc.fd_vid;
456 
457 	return (vid);
458 }
459 
460 /*
461  * Return whether the specified MAC client corresponds to a VLAN VNIC.
462  */
463 boolean_t
464 mac_client_is_vlan_vnic(mac_client_handle_t mch)
465 {
466 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
467 
468 	return (((mcip->mci_state_flags & MCIS_IS_VNIC) != 0) &&
469 	    ((mcip->mci_flent->fe_type & FLOW_PRIMARY_MAC) != 0));
470 }
471 
472 /*
473  * Return the link speed associated with the specified MAC client.
474  *
475  * The link speed of a MAC client is equal to the smallest value of
476  * 1) the current link speed of the underlying NIC, or
477  * 2) the bandwidth limit set for the MAC client.
478  *
479  * Note that the bandwidth limit can be higher than the speed
480  * of the underlying NIC. This is allowed to avoid spurious
481  * administration action failures or artifically lowering the
482  * bandwidth limit of a link that may  have temporarily lowered
483  * its link speed due to hardware problem or administrator action.
484  */
485 static uint64_t
486 mac_client_ifspeed(mac_client_impl_t *mcip)
487 {
488 	mac_impl_t *mip = mcip->mci_mip;
489 	uint64_t nic_speed;
490 
491 	nic_speed = mac_stat_get((mac_handle_t)mip, MAC_STAT_IFSPEED);
492 
493 	if (nic_speed == 0) {
494 		return (0);
495 	} else {
496 		uint64_t policy_limit = (uint64_t)-1;
497 
498 		if (MCIP_RESOURCE_PROPS_MASK(mcip) & MRP_MAXBW)
499 			policy_limit = MCIP_RESOURCE_PROPS_MAXBW(mcip);
500 
501 		return (MIN(policy_limit, nic_speed));
502 	}
503 }
504 
505 /*
506  * Return the link state of the specified client. If here are more
507  * than one clients of the underying mac_impl_t, the link state
508  * will always be UP regardless of the link state of the underlying
509  * mac_impl_t. This is needed to allow the MAC clients to continue
510  * to communicate with each other even when the physical link of
511  * their mac_impl_t is down.
512  */
513 static uint64_t
514 mac_client_link_state(mac_client_impl_t *mcip)
515 {
516 	mac_impl_t *mip = mcip->mci_mip;
517 	uint16_t vid;
518 	mac_client_impl_t *mci_list;
519 	mac_unicast_impl_t *mui_list, *oth_mui_list;
520 
521 	/*
522 	 * Returns LINK_STATE_UP if there are other MAC clients defined on
523 	 * mac_impl_t which share same VLAN ID as that of mcip. Note that
524 	 * if 'mcip' has more than one VID's then we match ANY one of the
525 	 * VID's with other MAC client's VID's and return LINK_STATE_UP.
526 	 */
527 	rw_enter(&mcip->mci_rw_lock, RW_READER);
528 	for (mui_list = mcip->mci_unicast_list; mui_list != NULL;
529 	    mui_list = mui_list->mui_next) {
530 		vid = mui_list->mui_vid;
531 		for (mci_list = mip->mi_clients_list; mci_list != NULL;
532 		    mci_list = mci_list->mci_client_next) {
533 			if (mci_list == mcip)
534 				continue;
535 			for (oth_mui_list = mci_list->mci_unicast_list;
536 			    oth_mui_list != NULL; oth_mui_list = oth_mui_list->
537 			    mui_next) {
538 				if (vid == oth_mui_list->mui_vid) {
539 					rw_exit(&mcip->mci_rw_lock);
540 					return (LINK_STATE_UP);
541 				}
542 			}
543 		}
544 	}
545 	rw_exit(&mcip->mci_rw_lock);
546 
547 	return (mac_stat_get((mac_handle_t)mip, MAC_STAT_LINK_STATE));
548 }
549 
550 /*
551  * Return the statistics of a MAC client. These statistics are different
552  * then the statistics of the underlying MAC which are returned by
553  * mac_stat_get().
554  */
555 uint64_t
556 mac_client_stat_get(mac_client_handle_t mch, uint_t stat)
557 {
558 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
559 	mac_impl_t *mip = mcip->mci_mip;
560 	uint64_t val;
561 
562 	switch (stat) {
563 	case MAC_STAT_LINK_STATE:
564 		val = mac_client_link_state(mcip);
565 		break;
566 	case MAC_STAT_LINK_UP:
567 		val = (mac_client_link_state(mcip) == LINK_STATE_UP);
568 		break;
569 	case MAC_STAT_PROMISC:
570 		val = mac_stat_get((mac_handle_t)mip, MAC_STAT_PROMISC);
571 		break;
572 	case MAC_STAT_LOWLINK_STATE:
573 		val = mac_stat_get((mac_handle_t)mip, MAC_STAT_LOWLINK_STATE);
574 		break;
575 	case MAC_STAT_IFSPEED:
576 		val = mac_client_ifspeed(mcip);
577 		break;
578 	case MAC_STAT_MULTIRCV:
579 		val = mcip->mci_stat_multircv;
580 		break;
581 	case MAC_STAT_BRDCSTRCV:
582 		val = mcip->mci_stat_brdcstrcv;
583 		break;
584 	case MAC_STAT_MULTIXMT:
585 		val = mcip->mci_stat_multixmt;
586 		break;
587 	case MAC_STAT_BRDCSTXMT:
588 		val = mcip->mci_stat_brdcstxmt;
589 		break;
590 	case MAC_STAT_OBYTES:
591 		val = mcip->mci_stat_obytes;
592 		break;
593 	case MAC_STAT_OPACKETS:
594 		val = mcip->mci_stat_opackets;
595 		break;
596 	case MAC_STAT_OERRORS:
597 		val = mcip->mci_stat_oerrors;
598 		break;
599 	case MAC_STAT_IPACKETS:
600 		val = mcip->mci_stat_ipackets;
601 		break;
602 	case MAC_STAT_RBYTES:
603 		val = mcip->mci_stat_ibytes;
604 		break;
605 	case MAC_STAT_IERRORS:
606 		val = mcip->mci_stat_ierrors;
607 		break;
608 	default:
609 		val = mac_stat_default(mip, stat);
610 		break;
611 	}
612 
613 	return (val);
614 }
615 
616 /*
617  * Return the statistics of the specified MAC instance.
618  */
619 uint64_t
620 mac_stat_get(mac_handle_t mh, uint_t stat)
621 {
622 	mac_impl_t	*mip = (mac_impl_t *)mh;
623 	uint64_t	val;
624 	int		ret;
625 
626 	/*
627 	 * The range of stat determines where it is maintained.  Stat
628 	 * values from 0 up to (but not including) MAC_STAT_MIN are
629 	 * mainteined by the mac module itself.  Everything else is
630 	 * maintained by the driver.
631 	 *
632 	 * If the mac_impl_t being queried corresponds to a VNIC,
633 	 * the stats need to be queried from the lower MAC client
634 	 * corresponding to the VNIC. (The mac_link_update()
635 	 * invoked by the driver to the lower MAC causes the *lower
636 	 * MAC* to update its mi_linkstate, and send a notification
637 	 * to its MAC clients. Due to the VNIC passthrough,
638 	 * these notifications are sent to the upper MAC clients
639 	 * of the VNIC directly, and the upper mac_impl_t of the VNIC
640 	 * does not have a valid mi_linkstate.
641 	 */
642 	if (stat < MAC_STAT_MIN && !(mip->mi_state_flags & MIS_IS_VNIC)) {
643 		/* these stats are maintained by the mac module itself */
644 		switch (stat) {
645 		case MAC_STAT_LINK_STATE:
646 			return (mip->mi_linkstate);
647 		case MAC_STAT_LINK_UP:
648 			return (mip->mi_linkstate == LINK_STATE_UP);
649 		case MAC_STAT_PROMISC:
650 			return (mip->mi_devpromisc != 0);
651 		case MAC_STAT_LOWLINK_STATE:
652 			return (mip->mi_lowlinkstate);
653 		default:
654 			ASSERT(B_FALSE);
655 		}
656 	}
657 
658 	/*
659 	 * Call the driver to get the given statistic.
660 	 */
661 	ret = mip->mi_getstat(mip->mi_driver, stat, &val);
662 	if (ret != 0) {
663 		/*
664 		 * The driver doesn't support this statistic.  Get the
665 		 * statistic's default value.
666 		 */
667 		val = mac_stat_default(mip, stat);
668 	}
669 	return (val);
670 }
671 
672 /*
673  * Utility function which returns the VID associated with a flow entry.
674  */
675 uint16_t
676 i_mac_flow_vid(flow_entry_t *flent)
677 {
678 	flow_desc_t	flow_desc;
679 
680 	mac_flow_get_desc(flent, &flow_desc);
681 
682 	if ((flow_desc.fd_mask & FLOW_LINK_VID) != 0)
683 		return (flow_desc.fd_vid);
684 	return (VLAN_ID_NONE);
685 }
686 
687 /*
688  * Verify the validity of the specified unicast MAC address. Returns B_TRUE
689  * if the address is valid, B_FALSE otherwise (multicast address, or incorrect
690  * length.
691  */
692 boolean_t
693 mac_unicst_verify(mac_handle_t mh, const uint8_t *addr, uint_t len)
694 {
695 	mac_impl_t	*mip = (mac_impl_t *)mh;
696 
697 	/*
698 	 * Verify the address. No lock is needed since mi_type and plugin
699 	 * details don't change after mac_register().
700 	 */
701 	if ((len != mip->mi_type->mt_addr_length) ||
702 	    (mip->mi_type->mt_ops.mtops_unicst_verify(addr,
703 	    mip->mi_pdata)) != 0) {
704 		return (B_FALSE);
705 	} else {
706 		return (B_TRUE);
707 	}
708 }
709 
710 void
711 mac_sdu_get(mac_handle_t mh, uint_t *min_sdu, uint_t *max_sdu)
712 {
713 	mac_impl_t	*mip = (mac_impl_t *)mh;
714 
715 	if (min_sdu != NULL)
716 		*min_sdu = mip->mi_sdu_min;
717 	if (max_sdu != NULL)
718 		*max_sdu = mip->mi_sdu_max;
719 }
720 
721 /*
722  * Update the MAC unicast address of the specified client's flows. Currently
723  * only one unicast MAC unicast address is allowed per client.
724  */
725 static void
726 mac_unicast_update_client_flow(mac_client_impl_t *mcip)
727 {
728 	mac_impl_t *mip = mcip->mci_mip;
729 	flow_entry_t *flent = mcip->mci_flent;
730 	mac_address_t *map = mcip->mci_unicast;
731 	flow_desc_t flow_desc;
732 
733 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
734 	ASSERT(flent != NULL);
735 
736 	mac_flow_get_desc(flent, &flow_desc);
737 	ASSERT(flow_desc.fd_mask & FLOW_LINK_DST);
738 
739 	bcopy(map->ma_addr, flow_desc.fd_dst_mac, map->ma_len);
740 	mac_flow_set_desc(flent, &flow_desc);
741 
742 	/*
743 	 * A MAC client could have one MAC address but multiple
744 	 * VLANs. In that case update the flow entries corresponding
745 	 * to all VLANs of the MAC client.
746 	 */
747 	for (flent = mcip->mci_flent_list; flent != NULL;
748 	    flent = flent->fe_client_next) {
749 		mac_flow_get_desc(flent, &flow_desc);
750 		if (!(flent->fe_type & FLOW_PRIMARY_MAC ||
751 		    flent->fe_type & FLOW_VNIC_MAC))
752 			continue;
753 
754 		bcopy(map->ma_addr, flow_desc.fd_dst_mac, map->ma_len);
755 		mac_flow_set_desc(flent, &flow_desc);
756 	}
757 }
758 
759 /*
760  * Update all clients that share the same unicast address.
761  */
762 void
763 mac_unicast_update_clients(mac_impl_t *mip, mac_address_t *map)
764 {
765 	mac_client_impl_t *mcip;
766 
767 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
768 
769 	/*
770 	 * Find all clients that share the same unicast MAC address and update
771 	 * them appropriately.
772 	 */
773 	for (mcip = mip->mi_clients_list; mcip != NULL;
774 	    mcip = mcip->mci_client_next) {
775 		/*
776 		 * Ignore clients that don't share this MAC address.
777 		 */
778 		if (map != mcip->mci_unicast)
779 			continue;
780 
781 		/*
782 		 * Update those clients with same old unicast MAC address.
783 		 */
784 		mac_unicast_update_client_flow(mcip);
785 	}
786 }
787 
788 /*
789  * Update the unicast MAC address of the specified VNIC MAC client.
790  *
791  * Check whether the operation is valid. Any of following cases should fail:
792  *
793  * 1. It's a VLAN type of VNIC.
794  * 2. The new value is current "primary" MAC address.
795  * 3. The current MAC address is shared with other clients.
796  * 4. The new MAC address has been used. This case will be valid when
797  *    client migration is fully supported.
798  */
799 int
800 mac_vnic_unicast_set(mac_client_handle_t mch, const uint8_t *addr)
801 {
802 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
803 	mac_impl_t *mip = mcip->mci_mip;
804 	mac_address_t *map = mcip->mci_unicast;
805 	int err;
806 
807 	ASSERT(!(mip->mi_state_flags & MIS_IS_VNIC));
808 	ASSERT(mcip->mci_state_flags & MCIS_IS_VNIC);
809 	ASSERT(mcip->mci_flags != MAC_CLIENT_FLAGS_PRIMARY);
810 
811 	i_mac_perim_enter(mip);
812 
813 	/*
814 	 * If this is a VLAN type of VNIC, it's using "primary" MAC address
815 	 * of the underlying interface. Must fail here. Refer to case 1 above.
816 	 */
817 	if (bcmp(map->ma_addr, mip->mi_addr, map->ma_len) == 0) {
818 		i_mac_perim_exit(mip);
819 		return (ENOTSUP);
820 	}
821 
822 	/*
823 	 * If the new address is the "primary" one, must fail. Refer to
824 	 * case 2 above.
825 	 */
826 	if (bcmp(addr, mip->mi_addr, map->ma_len) == 0) {
827 		i_mac_perim_exit(mip);
828 		return (EACCES);
829 	}
830 
831 	/*
832 	 * If the address is shared by multiple clients, must fail. Refer
833 	 * to case 3 above.
834 	 */
835 	if (mac_check_macaddr_shared(map)) {
836 		i_mac_perim_exit(mip);
837 		return (EBUSY);
838 	}
839 
840 	/*
841 	 * If the new address has been used, must fail for now. Refer to
842 	 * case 4 above.
843 	 */
844 	if (mac_find_macaddr(mip, (uint8_t *)addr) != NULL) {
845 		i_mac_perim_exit(mip);
846 		return (ENOTSUP);
847 	}
848 
849 	/*
850 	 * Update the MAC address.
851 	 */
852 	err = mac_update_macaddr(map, (uint8_t *)addr);
853 
854 	if (err != 0) {
855 		i_mac_perim_exit(mip);
856 		return (err);
857 	}
858 
859 	/*
860 	 * Update all flows of this MAC client.
861 	 */
862 	mac_unicast_update_client_flow(mcip);
863 
864 	i_mac_perim_exit(mip);
865 	return (0);
866 }
867 
868 /*
869  * Program the new primary unicast address of the specified MAC.
870  *
871  * Function mac_update_macaddr() takes care different types of underlying
872  * MAC. If the underlying MAC is VNIC, the VNIC driver must have registerd
873  * mi_unicst() entry point, that indirectly calls mac_vnic_unicast_set()
874  * which will take care of updating the MAC address of the corresponding
875  * MAC client.
876  *
877  * This is the only interface that allow the client to update the "primary"
878  * MAC address of the underlying MAC. The new value must have not been
879  * used by other clients.
880  */
881 int
882 mac_unicast_primary_set(mac_handle_t mh, const uint8_t *addr)
883 {
884 	mac_impl_t *mip = (mac_impl_t *)mh;
885 	mac_address_t *map;
886 	int err;
887 
888 	/* verify the address validity */
889 	if (!mac_unicst_verify(mh, addr, mip->mi_type->mt_addr_length))
890 		return (EINVAL);
891 
892 	i_mac_perim_enter(mip);
893 
894 	/*
895 	 * If the new value is the same as the current primary address value,
896 	 * there's nothing to do.
897 	 */
898 	if (bcmp(addr, mip->mi_addr, mip->mi_type->mt_addr_length) == 0) {
899 		i_mac_perim_exit(mip);
900 		return (0);
901 	}
902 
903 	if (mac_find_macaddr(mip, (uint8_t *)addr) != 0) {
904 		i_mac_perim_exit(mip);
905 		return (EBUSY);
906 	}
907 
908 	map = mac_find_macaddr(mip, mip->mi_addr);
909 	ASSERT(map != NULL);
910 
911 	/*
912 	 * Update the MAC address.
913 	 */
914 	if (mip->mi_state_flags & MIS_IS_AGGR) {
915 		mac_capab_aggr_t aggr_cap;
916 
917 		/*
918 		 * If the mac is an aggregation, other than the unicast
919 		 * addresses programming, aggr must be informed about this
920 		 * primary unicst address change to change its mac address
921 		 * policy to be user-specified.
922 		 */
923 		ASSERT(map->ma_type == MAC_ADDRESS_TYPE_UNICAST_CLASSIFIED);
924 		VERIFY(i_mac_capab_get(mh, MAC_CAPAB_AGGR, &aggr_cap));
925 		err = aggr_cap.mca_unicst(mip->mi_driver, addr);
926 		if (err == 0)
927 			bcopy(addr, map->ma_addr, map->ma_len);
928 	} else {
929 		err = mac_update_macaddr(map, (uint8_t *)addr);
930 	}
931 
932 	if (err != 0) {
933 		i_mac_perim_exit(mip);
934 		return (err);
935 	}
936 
937 	mac_unicast_update_clients(mip, map);
938 
939 	/*
940 	 * Save the new primary MAC address in mac_impl_t.
941 	 */
942 	bcopy(addr, mip->mi_addr, mip->mi_type->mt_addr_length);
943 
944 	i_mac_perim_exit(mip);
945 
946 	if (err == 0)
947 		i_mac_notify(mip, MAC_NOTE_UNICST);
948 
949 	return (err);
950 }
951 
952 /*
953  * Return the current primary MAC address of the specified MAC.
954  */
955 void
956 mac_unicast_primary_get(mac_handle_t mh, uint8_t *addr)
957 {
958 	mac_impl_t *mip = (mac_impl_t *)mh;
959 
960 	rw_enter(&mip->mi_rw_lock, RW_READER);
961 	bcopy(mip->mi_addr, addr, mip->mi_type->mt_addr_length);
962 	rw_exit(&mip->mi_rw_lock);
963 }
964 
965 /*
966  * Return information about the use of the primary MAC address of the
967  * specified MAC instance:
968  *
969  * - if client_name is non-NULL, it must point to a string of at
970  *   least MAXNAMELEN bytes, and will be set to the name of the MAC
971  *   client which uses the primary MAC address.
972  *
973  * - if in_use is non-NULL, used to return whether the primary MAC
974  *   address is currently in use.
975  */
976 void
977 mac_unicast_primary_info(mac_handle_t mh, char *client_name, boolean_t *in_use)
978 {
979 	mac_impl_t *mip = (mac_impl_t *)mh;
980 	mac_client_impl_t *cur_client;
981 
982 	if (in_use != NULL)
983 		*in_use = B_FALSE;
984 	if (client_name != NULL)
985 		bzero(client_name, MAXNAMELEN);
986 
987 	/*
988 	 * The mi_rw_lock is used to protect threads that don't hold the
989 	 * mac perimeter to get a consistent view of the mi_clients_list.
990 	 * Threads that modify the list must hold both the mac perimeter and
991 	 * mi_rw_lock(RW_WRITER)
992 	 */
993 	rw_enter(&mip->mi_rw_lock, RW_READER);
994 	for (cur_client = mip->mi_clients_list; cur_client != NULL;
995 	    cur_client = cur_client->mci_client_next) {
996 		if (mac_is_primary_client(cur_client) ||
997 		    (mip->mi_state_flags & MIS_IS_VNIC)) {
998 			rw_exit(&mip->mi_rw_lock);
999 			if (in_use != NULL)
1000 				*in_use = B_TRUE;
1001 			if (client_name != NULL) {
1002 				bcopy(cur_client->mci_name, client_name,
1003 				    MAXNAMELEN);
1004 			}
1005 			return;
1006 		}
1007 	}
1008 	rw_exit(&mip->mi_rw_lock);
1009 }
1010 
1011 /*
1012  * Return the current destination MAC address of the specified MAC.
1013  */
1014 boolean_t
1015 mac_dst_get(mac_handle_t mh, uint8_t *addr)
1016 {
1017 	mac_impl_t *mip = (mac_impl_t *)mh;
1018 
1019 	rw_enter(&mip->mi_rw_lock, RW_READER);
1020 	if (mip->mi_dstaddr_set)
1021 		bcopy(mip->mi_dstaddr, addr, mip->mi_type->mt_addr_length);
1022 	rw_exit(&mip->mi_rw_lock);
1023 	return (mip->mi_dstaddr_set);
1024 }
1025 
1026 /*
1027  * Add the specified MAC client to the list of clients which opened
1028  * the specified MAC.
1029  */
1030 static void
1031 mac_client_add(mac_client_impl_t *mcip)
1032 {
1033 	mac_impl_t *mip = mcip->mci_mip;
1034 
1035 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
1036 
1037 	/* add VNIC to the front of the list */
1038 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
1039 	mcip->mci_client_next = mip->mi_clients_list;
1040 	mip->mi_clients_list = mcip;
1041 	mip->mi_nclients++;
1042 	rw_exit(&mip->mi_rw_lock);
1043 }
1044 
1045 /*
1046  * Remove the specified MAC client from the list of clients which opened
1047  * the specified MAC.
1048  */
1049 static void
1050 mac_client_remove(mac_client_impl_t *mcip)
1051 {
1052 	mac_impl_t *mip = mcip->mci_mip;
1053 	mac_client_impl_t **prev, *cclient;
1054 
1055 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
1056 
1057 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
1058 	prev = &mip->mi_clients_list;
1059 	cclient = *prev;
1060 	while (cclient != NULL && cclient != mcip) {
1061 		prev = &cclient->mci_client_next;
1062 		cclient = *prev;
1063 	}
1064 	ASSERT(cclient != NULL);
1065 	*prev = cclient->mci_client_next;
1066 	mip->mi_nclients--;
1067 	rw_exit(&mip->mi_rw_lock);
1068 }
1069 
1070 static mac_unicast_impl_t *
1071 mac_client_find_vid(mac_client_impl_t *mcip, uint16_t vid)
1072 {
1073 	mac_unicast_impl_t *muip = mcip->mci_unicast_list;
1074 
1075 	while ((muip != NULL) && (muip->mui_vid != vid))
1076 		muip = muip->mui_next;
1077 
1078 	return (muip);
1079 }
1080 
1081 /*
1082  * Return whether the specified (MAC address, VID) tuple is already used by
1083  * one of the MAC clients associated with the specified MAC.
1084  */
1085 static boolean_t
1086 mac_addr_in_use(mac_impl_t *mip, uint8_t *mac_addr, uint16_t vid)
1087 {
1088 	mac_client_impl_t *client;
1089 	mac_address_t *map;
1090 
1091 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
1092 
1093 	for (client = mip->mi_clients_list; client != NULL;
1094 	    client = client->mci_client_next) {
1095 
1096 		/*
1097 		 * Ignore clients that don't have unicast address.
1098 		 */
1099 		if (client->mci_unicast_list == NULL)
1100 			continue;
1101 
1102 		map = client->mci_unicast;
1103 
1104 		if ((bcmp(mac_addr, map->ma_addr, map->ma_len) == 0) &&
1105 		    (mac_client_find_vid(client, vid) != NULL)) {
1106 			return (B_TRUE);
1107 		}
1108 	}
1109 
1110 	return (B_FALSE);
1111 }
1112 
1113 /*
1114  * Generate a random MAC address. The MAC address prefix is
1115  * stored in the array pointed to by mac_addr, and its length, in bytes,
1116  * is specified by prefix_len. The least significant bits
1117  * after prefix_len bytes are generated, and stored after the prefix
1118  * in the mac_addr array.
1119  */
1120 int
1121 mac_addr_random(mac_client_handle_t mch, uint_t prefix_len,
1122     uint8_t *mac_addr, mac_diag_t *diag)
1123 {
1124 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
1125 	mac_impl_t *mip = mcip->mci_mip;
1126 	size_t addr_len = mip->mi_type->mt_addr_length;
1127 
1128 	if (prefix_len >= addr_len) {
1129 		*diag = MAC_DIAG_MACPREFIXLEN_INVALID;
1130 		return (EINVAL);
1131 	}
1132 
1133 	/* check the prefix value */
1134 	if (prefix_len > 0) {
1135 		bzero(mac_addr + prefix_len, addr_len - prefix_len);
1136 		if (!mac_unicst_verify((mac_handle_t)mip, mac_addr,
1137 		    addr_len)) {
1138 			*diag = MAC_DIAG_MACPREFIX_INVALID;
1139 			return (EINVAL);
1140 		}
1141 	}
1142 
1143 	/* generate the MAC address */
1144 	if (prefix_len < addr_len) {
1145 		(void) random_get_pseudo_bytes(mac_addr +
1146 		    prefix_len, addr_len - prefix_len);
1147 	}
1148 
1149 	*diag = 0;
1150 	return (0);
1151 }
1152 
1153 /*
1154  * Set the priority range for this MAC client. This will be used to
1155  * determine the absolute priority for the threads created for this
1156  * MAC client using the specified "low", "medium" and "high" level.
1157  * This will also be used for any subflows on this MAC client.
1158  */
1159 #define	MAC_CLIENT_SET_PRIORITY_RANGE(mcip, pri) {			\
1160 	(mcip)->mci_min_pri = FLOW_MIN_PRIORITY(MINCLSYSPRI,	\
1161 	    MAXCLSYSPRI, (pri));					\
1162 	(mcip)->mci_max_pri = FLOW_MAX_PRIORITY(MINCLSYSPRI,	\
1163 	    MAXCLSYSPRI, (mcip)->mci_min_pri);				\
1164 	}
1165 
1166 /*
1167  * MAC client open entry point. Return a new MAC client handle. Each
1168  * MAC client is associated with a name, specified through the 'name'
1169  * argument.
1170  */
1171 int
1172 mac_client_open(mac_handle_t mh, mac_client_handle_t *mchp, char *name,
1173     uint16_t flags)
1174 {
1175 	mac_impl_t *mip = (mac_impl_t *)mh;
1176 	mac_client_impl_t *mcip;
1177 	int err = 0;
1178 	boolean_t share_desired =
1179 	    ((flags & MAC_OPEN_FLAGS_SHARES_DESIRED) != 0);
1180 	boolean_t no_hwrings = ((flags & MAC_OPEN_FLAGS_NO_HWRINGS) != 0);
1181 	boolean_t req_hwrings = ((flags & MAC_OPEN_FLAGS_REQ_HWRINGS) != 0);
1182 	flow_entry_t	*flent = NULL;
1183 
1184 	*mchp = NULL;
1185 	if (share_desired && no_hwrings) {
1186 		/* can't have shares but no hardware rings */
1187 		return (EINVAL);
1188 	}
1189 
1190 	i_mac_perim_enter(mip);
1191 
1192 	if (mip->mi_state_flags & MIS_IS_VNIC) {
1193 		/*
1194 		 * The underlying MAC is a VNIC. Return the MAC client
1195 		 * handle of the lower MAC which was obtained by
1196 		 * the VNIC driver when it did its mac_client_open().
1197 		 */
1198 
1199 		mcip = mac_vnic_lower(mip);
1200 
1201 		/*
1202 		 * Note that multiple mac clients share the same mcip in
1203 		 * this case.
1204 		 */
1205 		if (flags & MAC_OPEN_FLAGS_EXCLUSIVE)
1206 			mcip->mci_state_flags |= MCIS_EXCLUSIVE;
1207 
1208 		if (flags & MAC_OPEN_FLAGS_MULTI_PRIMARY)
1209 			mcip->mci_flags |= MAC_CLIENT_FLAGS_MULTI_PRIMARY;
1210 
1211 		mip->mi_clients_list = mcip;
1212 		i_mac_perim_exit(mip);
1213 		*mchp = (mac_client_handle_t)mcip;
1214 		return (err);
1215 	}
1216 
1217 	mcip = kmem_cache_alloc(mac_client_impl_cache, KM_SLEEP);
1218 
1219 	mcip->mci_mip = mip;
1220 	mcip->mci_upper_mip = NULL;
1221 	mcip->mci_rx_fn = mac_pkt_drop;
1222 	mcip->mci_rx_arg = NULL;
1223 	mcip->mci_rx_p_fn = NULL;
1224 	mcip->mci_rx_p_arg = NULL;
1225 	mcip->mci_p_unicast_list = NULL;
1226 	mcip->mci_direct_rx_fn = NULL;
1227 	mcip->mci_direct_rx_arg = NULL;
1228 
1229 	mcip->mci_unicast_list = NULL;
1230 
1231 	if ((flags & MAC_OPEN_FLAGS_IS_VNIC) != 0)
1232 		mcip->mci_state_flags |= MCIS_IS_VNIC;
1233 
1234 	if ((flags & MAC_OPEN_FLAGS_EXCLUSIVE) != 0)
1235 		mcip->mci_state_flags |= MCIS_EXCLUSIVE;
1236 
1237 	if ((flags & MAC_OPEN_FLAGS_IS_AGGR_PORT) != 0)
1238 		mcip->mci_state_flags |= MCIS_IS_AGGR_PORT;
1239 
1240 	if ((flags & MAC_OPEN_FLAGS_USE_DATALINK_NAME) != 0) {
1241 		datalink_id_t	linkid;
1242 
1243 		ASSERT(name == NULL);
1244 		if ((err = dls_devnet_macname2linkid(mip->mi_name,
1245 		    &linkid)) != 0) {
1246 			goto done;
1247 		}
1248 		if ((err = dls_mgmt_get_linkinfo(linkid, mcip->mci_name, NULL,
1249 		    NULL, NULL)) != 0) {
1250 			/*
1251 			 * Use mac name if dlmgmtd is not available.
1252 			 */
1253 			if (err == EBADF) {
1254 				(void) strlcpy(mcip->mci_name, mip->mi_name,
1255 				    sizeof (mcip->mci_name));
1256 				err = 0;
1257 			} else {
1258 				goto done;
1259 			}
1260 		}
1261 		mcip->mci_state_flags |= MCIS_USE_DATALINK_NAME;
1262 	} else {
1263 		ASSERT(name != NULL);
1264 		if (strlen(name) > MAXNAMELEN) {
1265 			err = EINVAL;
1266 			goto done;
1267 		}
1268 		(void) strlcpy(mcip->mci_name, name, sizeof (mcip->mci_name));
1269 	}
1270 
1271 	if (flags & MAC_OPEN_FLAGS_MULTI_PRIMARY)
1272 		mcip->mci_flags |= MAC_CLIENT_FLAGS_MULTI_PRIMARY;
1273 
1274 	/* the subflow table will be created dynamically */
1275 	mcip->mci_subflow_tab = NULL;
1276 	mcip->mci_stat_multircv = 0;
1277 	mcip->mci_stat_brdcstrcv = 0;
1278 	mcip->mci_stat_multixmt = 0;
1279 	mcip->mci_stat_brdcstxmt = 0;
1280 
1281 	mcip->mci_stat_obytes = 0;
1282 	mcip->mci_stat_opackets = 0;
1283 	mcip->mci_stat_oerrors = 0;
1284 	mcip->mci_stat_ibytes = 0;
1285 	mcip->mci_stat_ipackets = 0;
1286 	mcip->mci_stat_ierrors = 0;
1287 
1288 	/* Create an initial flow */
1289 
1290 	err = mac_flow_create(NULL, NULL, mcip->mci_name, NULL,
1291 	    mcip->mci_state_flags & MCIS_IS_VNIC ? FLOW_VNIC_MAC :
1292 	    FLOW_PRIMARY_MAC, &flent);
1293 	if (err != 0)
1294 		goto done;
1295 	mcip->mci_flent = flent;
1296 	FLOW_MARK(flent, FE_MC_NO_DATAPATH);
1297 	flent->fe_mcip = mcip;
1298 	/*
1299 	 * Place initial creation reference on the flow. This reference
1300 	 * is released in the corresponding delete action viz.
1301 	 * mac_unicast_remove after waiting for all transient refs to
1302 	 * to go away. The wait happens in mac_flow_wait.
1303 	 */
1304 	FLOW_REFHOLD(flent);
1305 
1306 	/*
1307 	 * Do this ahead of the mac_bcast_add() below so that the mi_nclients
1308 	 * will have the right value for mac_rx_srs_setup().
1309 	 */
1310 	mac_client_add(mcip);
1311 
1312 	if (no_hwrings)
1313 		mcip->mci_state_flags |= MCIS_NO_HWRINGS;
1314 	if (req_hwrings)
1315 		mcip->mci_state_flags |= MCIS_REQ_HWRINGS;
1316 	mcip->mci_share = NULL;
1317 	if (share_desired) {
1318 		ASSERT(!no_hwrings);
1319 		i_mac_share_alloc(mcip);
1320 	}
1321 
1322 	DTRACE_PROBE2(mac__client__open__allocated, mac_impl_t *,
1323 	    mcip->mci_mip, mac_client_impl_t *, mcip);
1324 	*mchp = (mac_client_handle_t)mcip;
1325 
1326 	i_mac_perim_exit(mip);
1327 	return (0);
1328 
1329 done:
1330 	i_mac_perim_exit(mip);
1331 	mcip->mci_state_flags = 0;
1332 	mcip->mci_tx_flag = 0;
1333 	kmem_cache_free(mac_client_impl_cache, mcip);
1334 	return (err);
1335 }
1336 
1337 /*
1338  * Close the specified MAC client handle.
1339  */
1340 void
1341 mac_client_close(mac_client_handle_t mch, uint16_t flags)
1342 {
1343 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
1344 	mac_impl_t		*mip = mcip->mci_mip;
1345 	flow_entry_t		*flent;
1346 
1347 	i_mac_perim_enter(mip);
1348 
1349 	if (flags & MAC_CLOSE_FLAGS_EXCLUSIVE)
1350 		mcip->mci_state_flags &= ~MCIS_EXCLUSIVE;
1351 
1352 	if ((mcip->mci_state_flags & MCIS_IS_VNIC) &&
1353 	    !(flags & MAC_CLOSE_FLAGS_IS_VNIC)) {
1354 		/*
1355 		 * This is an upper VNIC client initiated operation.
1356 		 * The lower MAC client will be closed by the VNIC driver
1357 		 * when the VNIC is deleted.
1358 		 */
1359 
1360 		i_mac_perim_exit(mip);
1361 		return;
1362 	}
1363 
1364 	/*
1365 	 * Remove the flent associated with the MAC client
1366 	 */
1367 	flent = mcip->mci_flent;
1368 	mcip->mci_flent = NULL;
1369 	FLOW_FINAL_REFRELE(flent);
1370 
1371 	/*
1372 	 * MAC clients must remove the unicast addresses and promisc callbacks
1373 	 * they added before issuing a mac_client_close().
1374 	 */
1375 	ASSERT(mcip->mci_unicast_list == NULL);
1376 	ASSERT(mcip->mci_promisc_list == NULL);
1377 	ASSERT(mcip->mci_tx_notify_cb_list == NULL);
1378 
1379 	i_mac_share_free(mcip);
1380 
1381 	mac_client_remove(mcip);
1382 
1383 	i_mac_perim_exit(mip);
1384 	mcip->mci_subflow_tab = NULL;
1385 	mcip->mci_state_flags = 0;
1386 	mcip->mci_tx_flag = 0;
1387 	kmem_cache_free(mac_client_impl_cache, mch);
1388 }
1389 
1390 /*
1391  * Enable bypass for the specified MAC client.
1392  */
1393 boolean_t
1394 mac_rx_bypass_set(mac_client_handle_t mch, mac_direct_rx_t rx_fn, void *arg1)
1395 {
1396 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
1397 	mac_impl_t		*mip = mcip->mci_mip;
1398 
1399 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
1400 
1401 	/*
1402 	 * If the mac_client is a VLAN, we should not do DLS bypass and
1403 	 * instead let the packets come up via mac_rx_deliver so the vlan
1404 	 * header can be stripped.
1405 	 */
1406 	if (mcip->mci_nvids > 0)
1407 		return (B_FALSE);
1408 
1409 	/*
1410 	 * These are not accessed directly in the data path, and hence
1411 	 * don't need any protection
1412 	 */
1413 	mcip->mci_direct_rx_fn = rx_fn;
1414 	mcip->mci_direct_rx_arg = arg1;
1415 	mcip->mci_state_flags |= MCIS_CLIENT_POLL_CAPABLE;
1416 	return (B_TRUE);
1417 }
1418 
1419 /*
1420  * Set the receive callback for the specified MAC client. There can be
1421  * at most one such callback per MAC client.
1422  */
1423 void
1424 mac_rx_set(mac_client_handle_t mch, mac_rx_t rx_fn, void *arg)
1425 {
1426 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
1427 	mac_impl_t	*mip = mcip->mci_mip;
1428 
1429 	/*
1430 	 * Instead of adding an extra set of locks and refcnts in
1431 	 * the datapath at the mac client boundary, we temporarily quiesce
1432 	 * the SRS and related entities. We then change the receive function
1433 	 * without interference from any receive data thread and then reenable
1434 	 * the data flow subsequently.
1435 	 */
1436 	i_mac_perim_enter(mip);
1437 	mac_rx_client_quiesce(mch);
1438 
1439 	mcip->mci_rx_fn = rx_fn;
1440 	mcip->mci_rx_arg = arg;
1441 	mac_rx_client_restart(mch);
1442 	i_mac_perim_exit(mip);
1443 }
1444 
1445 /*
1446  * Reset the receive callback for the specified MAC client.
1447  */
1448 void
1449 mac_rx_clear(mac_client_handle_t mch)
1450 {
1451 	mac_rx_set(mch, mac_pkt_drop, NULL);
1452 }
1453 
1454 /*
1455  * Walk the MAC client subflow table and updates their priority values.
1456  */
1457 static int
1458 mac_update_subflow_priority_cb(flow_entry_t *flent, void *arg)
1459 {
1460 	mac_flow_update_priority(arg, flent);
1461 	return (0);
1462 }
1463 
1464 void
1465 mac_update_subflow_priority(mac_client_impl_t *mcip)
1466 {
1467 	(void) mac_flow_walk(mcip->mci_subflow_tab,
1468 	    mac_update_subflow_priority_cb, mcip);
1469 }
1470 
1471 /*
1472  * When the MAC client is being brought up (i.e. we do a unicast_add) we need
1473  * to initialize the cpu and resource control structure in the
1474  * mac_client_impl_t from the mac_impl_t (i.e if there are any cached
1475  * properties before the flow entry for the unicast address was created).
1476  */
1477 int
1478 mac_resource_ctl_set(mac_client_handle_t mch, mac_resource_props_t *mrp)
1479 {
1480 	mac_client_impl_t 	*mcip = (mac_client_impl_t *)mch;
1481 	mac_impl_t		*mip = (mac_impl_t *)mcip->mci_mip;
1482 	int			err = 0;
1483 
1484 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
1485 
1486 	err = mac_validate_props(mrp);
1487 	if (err != 0)
1488 		return (err);
1489 
1490 	mac_update_resources(mrp, MCIP_RESOURCE_PROPS(mcip), B_FALSE);
1491 	if (MCIP_DATAPATH_SETUP(mcip)) {
1492 		/*
1493 		 * We have to set this prior to calling mac_flow_modify.
1494 		 */
1495 		if (mrp->mrp_mask & MRP_PRIORITY) {
1496 			if (mrp->mrp_priority == MPL_RESET) {
1497 				MAC_CLIENT_SET_PRIORITY_RANGE(mcip,
1498 				    MPL_LINK_DEFAULT);
1499 			} else {
1500 				MAC_CLIENT_SET_PRIORITY_RANGE(mcip,
1501 				    mrp->mrp_priority);
1502 			}
1503 		}
1504 
1505 		mac_flow_modify(mip->mi_flow_tab, mcip->mci_flent, mrp);
1506 		if (mrp->mrp_mask & MRP_PRIORITY)
1507 			mac_update_subflow_priority(mcip);
1508 		return (0);
1509 	}
1510 	return (0);
1511 }
1512 
1513 void
1514 mac_resource_ctl_get(mac_client_handle_t mch, mac_resource_props_t *mrp)
1515 {
1516 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
1517 	mac_resource_props_t	*mcip_mrp = MCIP_RESOURCE_PROPS(mcip);
1518 
1519 	bcopy(mcip_mrp, mrp, sizeof (mac_resource_props_t));
1520 }
1521 
1522 static int
1523 mac_unicast_flow_create(mac_client_impl_t *mcip, uint8_t *mac_addr,
1524     uint16_t vid, boolean_t is_primary, boolean_t first_flow,
1525     flow_entry_t **flent, mac_resource_props_t *mrp)
1526 {
1527 	mac_impl_t	*mip = (mac_impl_t *)mcip->mci_mip;
1528 	flow_desc_t	flow_desc;
1529 	char		flowname[MAXFLOWNAMELEN];
1530 	int		err;
1531 	uint_t		flent_flags;
1532 
1533 	/*
1534 	 * First unicast address being added, create a new flow
1535 	 * for that MAC client.
1536 	 */
1537 	bzero(&flow_desc, sizeof (flow_desc));
1538 
1539 	flow_desc.fd_mac_len = mip->mi_type->mt_addr_length;
1540 	bcopy(mac_addr, flow_desc.fd_dst_mac, flow_desc.fd_mac_len);
1541 	flow_desc.fd_mask = FLOW_LINK_DST;
1542 	if (vid != 0) {
1543 		flow_desc.fd_vid = vid;
1544 		flow_desc.fd_mask |= FLOW_LINK_VID;
1545 	}
1546 
1547 	/*
1548 	 * XXX-nicolas. For now I'm keeping the FLOW_PRIMARY_MAC
1549 	 * and FLOW_VNIC. Even though they're a hack inherited
1550 	 * from the SRS code, we'll keep them for now. They're currently
1551 	 * consumed by mac_datapath_setup() to create the SRS.
1552 	 * That code should be eventually moved out of
1553 	 * mac_datapath_setup() and moved to a mac_srs_create()
1554 	 * function of some sort to keep things clean.
1555 	 *
1556 	 * Also, there's no reason why the SRS for the primary MAC
1557 	 * client should be different than any other MAC client. Until
1558 	 * this is cleaned-up, we support only one MAC unicast address
1559 	 * per client.
1560 	 *
1561 	 * We set FLOW_PRIMARY_MAC for the primary MAC address,
1562 	 * FLOW_VNIC for everything else.
1563 	 */
1564 	if (is_primary)
1565 		flent_flags = FLOW_PRIMARY_MAC;
1566 	else
1567 		flent_flags = FLOW_VNIC_MAC;
1568 
1569 	/*
1570 	 * For the first flow we use the mac client's name - mci_name, for
1571 	 * subsequent ones we just create a name with the vid. This is
1572 	 * so that we can add these flows to the same flow table. This is
1573 	 * fine as the flow name (except for the one with the mac client's
1574 	 * name) is not visible. When the first flow is removed, we just replace
1575 	 * its fdesc with another from the list, so we will still retain the
1576 	 * flent with the MAC client's flow name.
1577 	 */
1578 	if (first_flow) {
1579 		bcopy(mcip->mci_name, flowname, MAXFLOWNAMELEN);
1580 	} else {
1581 		(void) sprintf(flowname, "%s%u", mcip->mci_name, vid);
1582 		flent_flags = FLOW_NO_STATS;
1583 	}
1584 
1585 	if ((err = mac_flow_create(&flow_desc, mrp, flowname, NULL,
1586 	    flent_flags, flent)) != 0)
1587 		return (err);
1588 
1589 	FLOW_MARK(*flent, FE_INCIPIENT);
1590 	(*flent)->fe_mcip = mcip;
1591 
1592 	/*
1593 	 * Place initial creation reference on the flow. This reference
1594 	 * is released in the corresponding delete action viz.
1595 	 * mac_unicast_remove after waiting for all transient refs to
1596 	 * to go away. The wait happens in mac_flow_wait.
1597 	 * We have already held the reference in mac_client_open().
1598 	 */
1599 	if (!first_flow)
1600 		FLOW_REFHOLD(*flent);
1601 	return (0);
1602 }
1603 
1604 /* Refresh the multicast grouping for this VID. */
1605 int
1606 mac_client_update_mcast(void *arg, boolean_t add, const uint8_t *addrp)
1607 {
1608 	flow_entry_t		*flent = arg;
1609 	mac_client_impl_t	*mcip = flent->fe_mcip;
1610 	uint16_t		vid;
1611 	flow_desc_t		flow_desc;
1612 
1613 	mac_flow_get_desc(flent, &flow_desc);
1614 	vid = (flow_desc.fd_mask & FLOW_LINK_VID) != 0 ?
1615 	    flow_desc.fd_vid : VLAN_ID_NONE;
1616 
1617 	/*
1618 	 * We don't call mac_multicast_add()/mac_multicast_remove() as
1619 	 * we want to add/remove for this specific vid.
1620 	 */
1621 	if (add) {
1622 		return (mac_bcast_add(mcip, addrp, vid,
1623 		    MAC_ADDRTYPE_MULTICAST));
1624 	} else {
1625 		mac_bcast_delete(mcip, addrp, vid);
1626 		return (0);
1627 	}
1628 }
1629 
1630 static void
1631 mac_update_single_active_client(mac_impl_t *mip)
1632 {
1633 	mac_client_impl_t *client = NULL;
1634 
1635 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
1636 
1637 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
1638 	if (mip->mi_nactiveclients == 1) {
1639 		/*
1640 		 * Find the one active MAC client from the list of MAC
1641 		 * clients. The active MAC client has at least one
1642 		 * unicast address.
1643 		 */
1644 		for (client = mip->mi_clients_list; client != NULL;
1645 		    client = client->mci_client_next) {
1646 			if (client->mci_unicast_list != NULL)
1647 				break;
1648 		}
1649 		ASSERT(client != NULL);
1650 	}
1651 
1652 	/*
1653 	 * mi_single_active_client is protected by the MAC impl's read/writer
1654 	 * lock, which allows mac_rx() to check the value of that pointer
1655 	 * as a reader.
1656 	 */
1657 	mip->mi_single_active_client = client;
1658 	rw_exit(&mip->mi_rw_lock);
1659 }
1660 
1661 /*
1662  * Set up the data path. Called from i_mac_unicast_add after having
1663  * done all the validations including making sure this is an active
1664  * client (i.e that is ready to process packets.)
1665  */
1666 static int
1667 mac_client_datapath_setup(mac_client_impl_t *mcip, uint16_t vid,
1668     uint8_t *mac_addr, mac_resource_props_t *mrp, boolean_t isprimary,
1669     mac_unicast_impl_t *muip)
1670 {
1671 	mac_impl_t	*mip = mcip->mci_mip;
1672 	boolean_t	mac_started = B_FALSE;
1673 	boolean_t	bcast_added = B_FALSE;
1674 	boolean_t	nactiveclients_added = B_FALSE;
1675 	flow_entry_t	*flent;
1676 	int		err = 0;
1677 
1678 	if ((err = mac_start((mac_handle_t)mip)) != 0)
1679 		goto bail;
1680 
1681 	mac_started = B_TRUE;
1682 
1683 	/* add the MAC client to the broadcast address group by default */
1684 	if (mip->mi_type->mt_brdcst_addr != NULL) {
1685 		err = mac_bcast_add(mcip, mip->mi_type->mt_brdcst_addr, vid,
1686 		    MAC_ADDRTYPE_BROADCAST);
1687 		if (err != 0)
1688 			goto bail;
1689 		bcast_added = B_TRUE;
1690 	}
1691 
1692 	/*
1693 	 * If this is the first unicast address addition for this
1694 	 * client, reuse the pre-allocated larval flow entry associated with
1695 	 * the MAC client.
1696 	 */
1697 	flent = (mcip->mci_nflents == 0) ? mcip->mci_flent : NULL;
1698 
1699 	/* We are configuring the unicast flow now */
1700 	if (!MCIP_DATAPATH_SETUP(mcip)) {
1701 
1702 		MAC_CLIENT_SET_PRIORITY_RANGE(mcip,
1703 		    (mrp->mrp_mask & MRP_PRIORITY) ? mrp->mrp_priority :
1704 		    MPL_LINK_DEFAULT);
1705 
1706 		if ((err = mac_unicast_flow_create(mcip, mac_addr, vid,
1707 		    isprimary, B_TRUE, &flent, mrp)) != 0)
1708 			goto bail;
1709 
1710 		mip->mi_nactiveclients++;
1711 		nactiveclients_added = B_TRUE;
1712 
1713 		/*
1714 		 * This will allocate the RX ring group if possible for the
1715 		 * flow and program the software classifier as needed.
1716 		 */
1717 		if ((err = mac_datapath_setup(mcip, flent, SRST_LINK)) != 0)
1718 			goto bail;
1719 
1720 		/*
1721 		 * The unicast MAC address must have been added successfully.
1722 		 */
1723 		ASSERT(mcip->mci_unicast != NULL);
1724 		/*
1725 		 * Push down the sub-flows that were defined on this link
1726 		 * hitherto. The flows are added to the active flow table
1727 		 * and SRS, softrings etc. are created as needed.
1728 		 */
1729 		mac_link_init_flows((mac_client_handle_t)mcip);
1730 	} else {
1731 		mac_address_t *map = mcip->mci_unicast;
1732 
1733 		/*
1734 		 * A unicast flow already exists for that MAC client,
1735 		 * this flow must be the same mac address but with
1736 		 * different VID. It has been checked by mac_addr_in_use().
1737 		 *
1738 		 * We will use the SRS etc. from the mci_flent. Note that
1739 		 * We don't need to create kstat for this as except for
1740 		 * the fdesc, everything will be used from in the 1st flent.
1741 		 */
1742 
1743 		if (bcmp(mac_addr, map->ma_addr, map->ma_len) != 0) {
1744 			err = EINVAL;
1745 			goto bail;
1746 		}
1747 
1748 		if ((err = mac_unicast_flow_create(mcip, mac_addr, vid,
1749 		    isprimary, B_FALSE, &flent, NULL)) != 0) {
1750 			goto bail;
1751 		}
1752 		if ((err = mac_flow_add(mip->mi_flow_tab, flent)) != 0) {
1753 			FLOW_FINAL_REFRELE(flent);
1754 			goto bail;
1755 		}
1756 
1757 		/* update the multicast group for this vid */
1758 		mac_client_bcast_refresh(mcip, mac_client_update_mcast,
1759 		    (void *)flent, B_TRUE);
1760 
1761 	}
1762 
1763 	/* populate the shared MAC address */
1764 	muip->mui_map = mcip->mci_unicast;
1765 
1766 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
1767 	muip->mui_next = mcip->mci_unicast_list;
1768 	mcip->mci_unicast_list = muip;
1769 	rw_exit(&mcip->mci_rw_lock);
1770 
1771 
1772 	/*
1773 	 * First add the flent to the flow list of this mcip. Then set
1774 	 * the mip's mi_single_active_client if needed. The Rx path assumes
1775 	 * that mip->mi_single_active_client will always have an associated
1776 	 * flent.
1777 	 */
1778 	mac_client_add_to_flow_list(mcip, flent);
1779 
1780 	if (nactiveclients_added)
1781 		mac_update_single_active_client(mip);
1782 	/*
1783 	 * Trigger a renegotiation of the capabilities when the number of
1784 	 * active clients changes from 1 to 2, since some of the capabilities
1785 	 * might have to be disabled. Also send a MAC_NOTE_LINK notification
1786 	 * to all the MAC clients whenever physical link is DOWN.
1787 	 */
1788 	if (mip->mi_nactiveclients == 2) {
1789 		mac_capab_update((mac_handle_t)mip);
1790 		mac_virtual_link_update(mip);
1791 	}
1792 	/*
1793 	 * Now that the setup is complete, clear the INCIPIENT flag.
1794 	 * The flag was set to avoid incoming packets seeing inconsistent
1795 	 * structures while the setup was in progress. Clear the mci_tx_flag
1796 	 * by calling mac_tx_client_block. It is possible that
1797 	 * mac_unicast_remove was called prior to this mac_unicast_add which
1798 	 * could have set the MCI_TX_QUIESCE flag.
1799 	 */
1800 	if (flent->fe_rx_ring_group != NULL)
1801 		mac_rx_group_unmark(flent->fe_rx_ring_group, MR_INCIPIENT);
1802 	FLOW_UNMARK(flent, FE_INCIPIENT);
1803 	FLOW_UNMARK(flent, FE_MC_NO_DATAPATH);
1804 	mac_tx_client_unblock(mcip);
1805 	return (0);
1806 bail:
1807 	if (bcast_added)
1808 		mac_bcast_delete(mcip, mip->mi_type->mt_brdcst_addr, vid);
1809 
1810 	if (nactiveclients_added)
1811 		mip->mi_nactiveclients--;
1812 
1813 	if (mac_started)
1814 		mac_stop((mac_handle_t)mip);
1815 
1816 	return (err);
1817 }
1818 
1819 /*
1820  * Return the passive primary MAC client, if present. The passive client is
1821  * a stand-by client that has the same unicast address as another that is
1822  * currenly active. Once the active client goes away, the passive client
1823  * becomes active.
1824  */
1825 static mac_client_impl_t *
1826 mac_get_passive_primary_client(mac_impl_t *mip)
1827 {
1828 	mac_client_impl_t	*mcip;
1829 
1830 	for (mcip = mip->mi_clients_list; mcip != NULL;
1831 	    mcip = mcip->mci_client_next) {
1832 		if (mac_is_primary_client(mcip) &&
1833 		    (mcip->mci_flags & MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) != 0) {
1834 			return (mcip);
1835 		}
1836 	}
1837 	return (NULL);
1838 }
1839 
1840 /*
1841  * Add a new unicast address to the MAC client.
1842  *
1843  * The MAC address can be specified either by value, or the MAC client
1844  * can specify that it wants to use the primary MAC address of the
1845  * underlying MAC. See the introductory comments at the beginning
1846  * of this file for more more information on primary MAC addresses.
1847  *
1848  * Note also the tuple (MAC address, VID) must be unique
1849  * for the MAC clients defined on top of the same underlying MAC
1850  * instance, unless the MAC_UNICAST_NODUPCHECK is specified.
1851  *
1852  * In no case can a client use the PVID for the MAC, if the MAC has one set.
1853  */
1854 int
1855 i_mac_unicast_add(mac_client_handle_t mch, uint8_t *mac_addr, uint16_t flags,
1856     mac_unicast_handle_t *mah, uint16_t vid, mac_diag_t *diag)
1857 {
1858 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
1859 	mac_impl_t		*mip = mcip->mci_mip;
1860 	int			err;
1861 	uint_t			mac_len = mip->mi_type->mt_addr_length;
1862 	boolean_t		check_dups = !(flags & MAC_UNICAST_NODUPCHECK);
1863 	boolean_t		fastpath_disabled = B_FALSE;
1864 	boolean_t		is_primary = (flags & MAC_UNICAST_PRIMARY);
1865 	boolean_t		is_unicast_hw = (flags & MAC_UNICAST_HW);
1866 	mac_resource_props_t	mrp;
1867 	boolean_t		passive_client = B_FALSE;
1868 	mac_unicast_impl_t	*muip;
1869 	boolean_t		is_vnic_primary =
1870 	    (flags & MAC_UNICAST_VNIC_PRIMARY);
1871 
1872 	/* when VID is non-zero, the underlying MAC can not be VNIC */
1873 	ASSERT(!((mip->mi_state_flags & MIS_IS_VNIC) && (vid != 0)));
1874 
1875 	/*
1876 	 * Check for an attempted use of the current Port VLAN ID, if enabled.
1877 	 * No client may use it.
1878 	 */
1879 	if (mip->mi_pvid != 0 && vid == mip->mi_pvid)
1880 		return (EBUSY);
1881 
1882 	/*
1883 	 * Check whether it's the primary client and flag it.
1884 	 */
1885 	if (!(mcip->mci_state_flags & MCIS_IS_VNIC) && is_primary && vid == 0)
1886 		mcip->mci_flags |= MAC_CLIENT_FLAGS_PRIMARY;
1887 
1888 	/*
1889 	 * is_vnic_primary is true when we come here as a VLAN VNIC
1890 	 * which uses the primary mac client's address but with a non-zero
1891 	 * VID. In this case the MAC address is not specified by an upper
1892 	 * MAC client.
1893 	 */
1894 	if ((mcip->mci_state_flags & MCIS_IS_VNIC) && is_primary &&
1895 	    !is_vnic_primary) {
1896 		/*
1897 		 * The address is being set by the upper MAC client
1898 		 * of a VNIC. The MAC address was already set by the
1899 		 * VNIC driver during VNIC creation.
1900 		 *
1901 		 * Note: a VNIC has only one MAC address. We return
1902 		 * the MAC unicast address handle of the lower MAC client
1903 		 * corresponding to the VNIC. We allocate a new entry
1904 		 * which is flagged appropriately, so that mac_unicast_remove()
1905 		 * doesn't attempt to free the original entry that
1906 		 * was allocated by the VNIC driver.
1907 		 */
1908 		ASSERT(mcip->mci_unicast != NULL);
1909 
1910 		/* Check for VLAN flags, if present */
1911 		if ((flags & MAC_UNICAST_TAG_DISABLE) != 0)
1912 			mcip->mci_state_flags |= MCIS_TAG_DISABLE;
1913 
1914 		if ((flags & MAC_UNICAST_STRIP_DISABLE) != 0)
1915 			mcip->mci_state_flags |= MCIS_STRIP_DISABLE;
1916 
1917 		if ((flags & MAC_UNICAST_DISABLE_TX_VID_CHECK) != 0)
1918 			mcip->mci_state_flags |= MCIS_DISABLE_TX_VID_CHECK;
1919 
1920 		/*
1921 		 * Ensure that the primary unicast address of the VNIC
1922 		 * is added only once unless we have the
1923 		 * MAC_CLIENT_FLAGS_MULTI_PRIMARY set (and this is not
1924 		 * a passive MAC client).
1925 		 */
1926 		if ((mcip->mci_flags & MAC_CLIENT_FLAGS_VNIC_PRIMARY) != 0) {
1927 			if ((mcip->mci_flags &
1928 			    MAC_CLIENT_FLAGS_MULTI_PRIMARY) == 0 ||
1929 			    (mcip->mci_flags &
1930 			    MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) != 0) {
1931 				return (EBUSY);
1932 			}
1933 			mcip->mci_flags |= MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
1934 			passive_client = B_TRUE;
1935 		}
1936 
1937 		mcip->mci_flags |= MAC_CLIENT_FLAGS_VNIC_PRIMARY;
1938 
1939 		/*
1940 		 * Create a handle for vid 0.
1941 		 */
1942 		ASSERT(vid == 0);
1943 		muip = kmem_zalloc(sizeof (mac_unicast_impl_t), KM_SLEEP);
1944 		muip->mui_vid = vid;
1945 		*mah = (mac_unicast_handle_t)muip;
1946 		/*
1947 		 * This will be used by the caller to defer setting the
1948 		 * rx functions.
1949 		 */
1950 		if (passive_client)
1951 			return (EAGAIN);
1952 		return (0);
1953 	}
1954 
1955 	/* primary MAC clients cannot be opened on top of anchor VNICs */
1956 	if ((is_vnic_primary || is_primary) &&
1957 	    i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_ANCHOR_VNIC, NULL)) {
1958 		return (ENXIO);
1959 	}
1960 
1961 	/*
1962 	 * If this is a VNIC/VLAN, disable softmac fast-path.
1963 	 */
1964 	if (mcip->mci_state_flags & MCIS_IS_VNIC) {
1965 		err = mac_fastpath_disable((mac_handle_t)mip);
1966 		if (err != 0)
1967 			return (err);
1968 		fastpath_disabled = B_TRUE;
1969 	}
1970 
1971 	/*
1972 	 * Return EBUSY if:
1973 	 *  - there is an exclusively active mac client exists.
1974 	 *  - this is an exclusive active mac client but
1975 	 *	a. there is already active mac clients exist, or
1976 	 *	b. fastpath streams are already plumbed on this legacy device
1977 	 *  - the mac creator has disallowed active mac clients.
1978 	 */
1979 	if (mip->mi_state_flags & (MIS_EXCLUSIVE|MIS_NO_ACTIVE)) {
1980 		if (fastpath_disabled)
1981 			mac_fastpath_enable((mac_handle_t)mip);
1982 		return (EBUSY);
1983 	}
1984 
1985 	if (mcip->mci_state_flags & MCIS_EXCLUSIVE) {
1986 		ASSERT(!fastpath_disabled);
1987 		if (mip->mi_nactiveclients != 0)
1988 			return (EBUSY);
1989 
1990 		if ((mip->mi_state_flags & MIS_LEGACY) &&
1991 		    !(mip->mi_capab_legacy.ml_active_set(mip->mi_driver))) {
1992 			return (EBUSY);
1993 		}
1994 		mip->mi_state_flags |= MIS_EXCLUSIVE;
1995 	}
1996 
1997 	bzero(&mrp, sizeof (mac_resource_props_t));
1998 	if (is_primary && !(mcip->mci_state_flags & (MCIS_IS_VNIC |
1999 	    MCIS_IS_AGGR_PORT))) {
2000 		/*
2001 		 * Apply the property cached in the mac_impl_t to the primary
2002 		 * mac client. If the mac client is a VNIC or an aggregation
2003 		 * port, its property should be set in the mcip when the
2004 		 * VNIC/aggr was created.
2005 		 */
2006 		mac_get_resources((mac_handle_t)mip, &mrp);
2007 		(void) mac_client_set_resources(mch, &mrp);
2008 	} else if (mcip->mci_state_flags & MCIS_IS_VNIC) {
2009 		bcopy(MCIP_RESOURCE_PROPS(mcip), &mrp,
2010 		    sizeof (mac_resource_props_t));
2011 	}
2012 
2013 	muip = kmem_zalloc(sizeof (mac_unicast_impl_t), KM_SLEEP);
2014 	muip->mui_vid = vid;
2015 
2016 	if (is_primary || is_vnic_primary) {
2017 		mac_addr = mip->mi_addr;
2018 	} else {
2019 
2020 		/*
2021 		 * Verify the validity of the specified MAC addresses value.
2022 		 */
2023 		if (!mac_unicst_verify((mac_handle_t)mip, mac_addr, mac_len)) {
2024 			*diag = MAC_DIAG_MACADDR_INVALID;
2025 			err = EINVAL;
2026 			goto bail_out;
2027 		}
2028 
2029 		/*
2030 		 * Make sure that the specified MAC address is different
2031 		 * than the unicast MAC address of the underlying NIC.
2032 		 */
2033 		if (check_dups && bcmp(mip->mi_addr, mac_addr, mac_len) == 0) {
2034 			*diag = MAC_DIAG_MACADDR_NIC;
2035 			err = EINVAL;
2036 			goto bail_out;
2037 		}
2038 	}
2039 
2040 	/*
2041 	 * Set the flags here so that if this is a passive client, we
2042 	 * can return  and set it when we call mac_client_datapath_setup
2043 	 * when this becomes the active client. If we defer to using these
2044 	 * flags to mac_client_datapath_setup, then for a passive client,
2045 	 * we'd have to store the flags somewhere (probably fe_flags)
2046 	 * and then use it.
2047 	 */
2048 	if (!MCIP_DATAPATH_SETUP(mcip)) {
2049 		if (is_unicast_hw) {
2050 			/*
2051 			 * The client requires a hardware MAC address slot
2052 			 * for that unicast address. Since we support only
2053 			 * one unicast MAC address per client, flag the
2054 			 * MAC client itself.
2055 			 */
2056 			mcip->mci_state_flags |= MCIS_UNICAST_HW;
2057 		}
2058 
2059 		/* Check for VLAN flags, if present */
2060 		if ((flags & MAC_UNICAST_TAG_DISABLE) != 0)
2061 			mcip->mci_state_flags |= MCIS_TAG_DISABLE;
2062 
2063 		if ((flags & MAC_UNICAST_STRIP_DISABLE) != 0)
2064 			mcip->mci_state_flags |= MCIS_STRIP_DISABLE;
2065 
2066 		if ((flags & MAC_UNICAST_DISABLE_TX_VID_CHECK) != 0)
2067 			mcip->mci_state_flags |= MCIS_DISABLE_TX_VID_CHECK;
2068 	} else {
2069 		/*
2070 		 * Assert that the specified flags are consistent with the
2071 		 * flags specified by previous calls to mac_unicast_add().
2072 		 */
2073 		ASSERT(((flags & MAC_UNICAST_TAG_DISABLE) != 0 &&
2074 		    (mcip->mci_state_flags & MCIS_TAG_DISABLE) != 0) ||
2075 		    ((flags & MAC_UNICAST_TAG_DISABLE) == 0 &&
2076 		    (mcip->mci_state_flags & MCIS_TAG_DISABLE) == 0));
2077 
2078 		ASSERT(((flags & MAC_UNICAST_STRIP_DISABLE) != 0 &&
2079 		    (mcip->mci_state_flags & MCIS_STRIP_DISABLE) != 0) ||
2080 		    ((flags & MAC_UNICAST_STRIP_DISABLE) == 0 &&
2081 		    (mcip->mci_state_flags & MCIS_STRIP_DISABLE) == 0));
2082 
2083 		ASSERT(((flags & MAC_UNICAST_DISABLE_TX_VID_CHECK) != 0 &&
2084 		    (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK) != 0) ||
2085 		    ((flags & MAC_UNICAST_DISABLE_TX_VID_CHECK) == 0 &&
2086 		    (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK) == 0));
2087 
2088 		/*
2089 		 * Make sure the client is consistent about its requests
2090 		 * for MAC addresses. I.e. all requests from the clients
2091 		 * must have the MAC_UNICAST_HW flag set or clear.
2092 		 */
2093 		if ((mcip->mci_state_flags & MCIS_UNICAST_HW) != 0 &&
2094 		    !is_unicast_hw ||
2095 		    (mcip->mci_state_flags & MCIS_UNICAST_HW) == 0 &&
2096 		    is_unicast_hw) {
2097 			err = EINVAL;
2098 			goto bail_out;
2099 		}
2100 	}
2101 	/*
2102 	 * Make sure the MAC address is not already used by
2103 	 * another MAC client defined on top of the same
2104 	 * underlying NIC. Unless we have MAC_CLIENT_FLAGS_MULTI_PRIMARY
2105 	 * set when we allow a passive client to be present which will
2106 	 * be activated when the currently active client goes away - this
2107 	 * works only with primary addresses.
2108 	 */
2109 	if ((check_dups || is_primary || is_vnic_primary) &&
2110 	    mac_addr_in_use(mip, mac_addr, vid)) {
2111 		/*
2112 		 * Must have set the multiple primary address flag when
2113 		 * we did a mac_client_open AND this should be a primary
2114 		 * MAC client AND there should not already be a passive
2115 		 * primary. If all is true then we let this succeed
2116 		 * even if the address is a dup.
2117 		 */
2118 		if ((mcip->mci_flags & MAC_CLIENT_FLAGS_MULTI_PRIMARY) == 0 ||
2119 		    (mcip->mci_flags & MAC_CLIENT_FLAGS_PRIMARY) == 0 ||
2120 		    mac_get_passive_primary_client(mip) != NULL) {
2121 			*diag = MAC_DIAG_MACADDR_INUSE;
2122 			err = EEXIST;
2123 			goto bail_out;
2124 		}
2125 		ASSERT((mcip->mci_flags &
2126 		    MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) == 0);
2127 		mcip->mci_flags |= MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
2128 
2129 		/*
2130 		 * Stash the unicast address handle, we will use it when
2131 		 * we set up the passive client.
2132 		 */
2133 		mcip->mci_p_unicast_list = muip;
2134 		*mah = (mac_unicast_handle_t)muip;
2135 		return (0);
2136 	}
2137 
2138 	err = mac_client_datapath_setup(mcip, vid, mac_addr, &mrp,
2139 	    is_primary || is_vnic_primary, muip);
2140 	if (err != 0)
2141 		goto bail_out;
2142 	*mah = (mac_unicast_handle_t)muip;
2143 	return (0);
2144 
2145 bail_out:
2146 	if (fastpath_disabled)
2147 		mac_fastpath_enable((mac_handle_t)mip);
2148 	if (mcip->mci_state_flags & MCIS_EXCLUSIVE) {
2149 		mip->mi_state_flags &= ~MIS_EXCLUSIVE;
2150 		if (mip->mi_state_flags & MIS_LEGACY) {
2151 			mip->mi_capab_legacy.ml_active_clear(
2152 			    mip->mi_driver);
2153 		}
2154 	}
2155 	kmem_free(muip, sizeof (mac_unicast_impl_t));
2156 	return (err);
2157 }
2158 
2159 /*
2160  * Wrapper function to mac_unicast_add when we want to have the same mac
2161  * client open for two instances, one that is currently active and another
2162  * that will become active when the current one is removed. In this case
2163  * mac_unicast_add will return EGAIN and we will save the rx function and
2164  * arg which will be used when we activate the passive client in
2165  * mac_unicast_remove.
2166  */
2167 int
2168 mac_unicast_add_set_rx(mac_client_handle_t mch, uint8_t *mac_addr,
2169     uint16_t flags, mac_unicast_handle_t *mah,  uint16_t vid, mac_diag_t *diag,
2170     mac_rx_t rx_fn, void *arg)
2171 {
2172 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
2173 	uint_t			err;
2174 
2175 	err = mac_unicast_add(mch, mac_addr, flags, mah, vid, diag);
2176 	if (err != 0 && err != EAGAIN)
2177 		return (err);
2178 	if (err == EAGAIN) {
2179 		if (rx_fn != NULL) {
2180 			mcip->mci_rx_p_fn = rx_fn;
2181 			mcip->mci_rx_p_arg = arg;
2182 		}
2183 		return (0);
2184 	}
2185 	if (rx_fn != NULL)
2186 		mac_rx_set(mch, rx_fn, arg);
2187 	return (err);
2188 }
2189 
2190 int
2191 mac_unicast_add(mac_client_handle_t mch, uint8_t *mac_addr, uint16_t flags,
2192     mac_unicast_handle_t *mah, uint16_t vid, mac_diag_t *diag)
2193 {
2194 	mac_impl_t *mip = ((mac_client_impl_t *)mch)->mci_mip;
2195 	uint_t err;
2196 
2197 	i_mac_perim_enter(mip);
2198 	err = i_mac_unicast_add(mch, mac_addr, flags, mah, vid, diag);
2199 	i_mac_perim_exit(mip);
2200 
2201 	return (err);
2202 }
2203 
2204 void
2205 mac_client_datapath_teardown(mac_client_handle_t mch, mac_unicast_impl_t *muip,
2206     flow_entry_t *flent)
2207 {
2208 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
2209 	mac_impl_t		*mip = mcip->mci_mip;
2210 
2211 	/*
2212 	 * We would have initialized subflows etc. only if we brought up
2213 	 * the primary client and set the unicast unicast address etc.
2214 	 * Deactivate the flows. The flow entry will be removed from the
2215 	 * active flow tables, and the associated SRS, softrings etc will
2216 	 * be deleted. But the flow entry itself won't be destroyed, instead
2217 	 * it will continue to be archived off the  the global flow hash
2218 	 * list, for a possible future activation when say IP is plumbed
2219 	 * again.
2220 	 */
2221 	mac_link_release_flows(mch);
2222 
2223 	mip->mi_nactiveclients--;
2224 	mac_update_single_active_client(mip);
2225 
2226 	/* Tear down the data path */
2227 	mac_datapath_teardown(mcip, mcip->mci_flent, SRST_LINK);
2228 
2229 	/*
2230 	 * Prevent any future access to the flow entry through the mci_flent
2231 	 * pointer by setting the mci_flent to NULL. Access to mci_flent in
2232 	 * mac_bcast_send is also under mi_rw_lock.
2233 	 */
2234 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
2235 	flent = mcip->mci_flent;
2236 	mac_client_remove_flow_from_list(mcip, flent);
2237 
2238 	if (mcip->mci_state_flags & MCIS_DESC_LOGGED)
2239 		mcip->mci_state_flags &= ~MCIS_DESC_LOGGED;
2240 
2241 	/*
2242 	 * This is the last unicast address being removed and there shouldn't
2243 	 * be any outbound data threads at this point coming down from mac
2244 	 * clients. We have waited for the data threads to finish before
2245 	 * starting dld_str_detach. Non-data threads must access TX SRS
2246 	 * under mi_rw_lock.
2247 	 */
2248 	rw_exit(&mip->mi_rw_lock);
2249 
2250 	/*
2251 	 * Don't use FLOW_MARK with FE_MC_NO_DATAPATH, as the flow might
2252 	 * contain other flags, such as FE_CONDEMNED, which we need to
2253 	 * cleared. We don't call mac_flow_cleanup() for this unicast
2254 	 * flow as we have a already cleaned up SRSs etc. (via the teadown
2255 	 * path). We just clear the stats and reset the initial callback
2256 	 * function, the rest will be set when we call mac_flow_create,
2257 	 * if at all.
2258 	 */
2259 	mutex_enter(&flent->fe_lock);
2260 	ASSERT(flent->fe_refcnt == 1 && flent->fe_mbg == NULL &&
2261 	    flent->fe_tx_srs == NULL && flent->fe_rx_srs_cnt == 0);
2262 	flent->fe_flags = FE_MC_NO_DATAPATH;
2263 	flow_stat_destroy(flent);
2264 
2265 	/* Initialize the receiver function to a safe routine */
2266 	flent->fe_cb_fn = (flow_fn_t)mac_pkt_drop;
2267 	flent->fe_cb_arg1 = NULL;
2268 	flent->fe_cb_arg2 = NULL;
2269 
2270 	flent->fe_index = -1;
2271 	mutex_exit(&flent->fe_lock);
2272 
2273 	if (mip->mi_type->mt_brdcst_addr != NULL) {
2274 		mac_bcast_delete(mcip, mip->mi_type->mt_brdcst_addr,
2275 		    muip->mui_vid);
2276 	}
2277 
2278 	if (mip->mi_nactiveclients == 1) {
2279 		mac_capab_update((mac_handle_t)mip);
2280 		mac_virtual_link_update(mip);
2281 	}
2282 
2283 	if (mcip->mci_state_flags & MCIS_EXCLUSIVE) {
2284 		mip->mi_state_flags &= ~MIS_EXCLUSIVE;
2285 
2286 		if (mip->mi_state_flags & MIS_LEGACY)
2287 			mip->mi_capab_legacy.ml_active_clear(mip->mi_driver);
2288 	}
2289 
2290 	mcip->mci_state_flags &= ~MCIS_UNICAST_HW;
2291 
2292 	if (mcip->mci_state_flags & MCIS_TAG_DISABLE)
2293 		mcip->mci_state_flags &= ~MCIS_TAG_DISABLE;
2294 
2295 	if (mcip->mci_state_flags & MCIS_STRIP_DISABLE)
2296 		mcip->mci_state_flags &= ~MCIS_STRIP_DISABLE;
2297 
2298 	if (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK)
2299 		mcip->mci_state_flags &= ~MCIS_DISABLE_TX_VID_CHECK;
2300 
2301 	kmem_free(muip, sizeof (mac_unicast_impl_t));
2302 
2303 	/*
2304 	 * Disable fastpath if this is a VNIC or a VLAN.
2305 	 */
2306 	if (mcip->mci_state_flags & MCIS_IS_VNIC)
2307 		mac_fastpath_enable((mac_handle_t)mip);
2308 	mac_stop((mac_handle_t)mip);
2309 }
2310 
2311 /*
2312  * Remove a MAC address which was previously added by mac_unicast_add().
2313  */
2314 int
2315 mac_unicast_remove(mac_client_handle_t mch, mac_unicast_handle_t mah)
2316 {
2317 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
2318 	mac_unicast_impl_t *muip = (mac_unicast_impl_t *)mah;
2319 	mac_unicast_impl_t *pre;
2320 	mac_impl_t *mip = mcip->mci_mip;
2321 	flow_entry_t		*flent;
2322 	boolean_t		isprimary = B_FALSE;
2323 
2324 	i_mac_perim_enter(mip);
2325 	if (mcip->mci_flags & MAC_CLIENT_FLAGS_VNIC_PRIMARY) {
2326 		/*
2327 		 * Called made by the upper MAC client of a VNIC.
2328 		 * There's nothing much to do, the unicast address will
2329 		 * be removed by the VNIC driver when the VNIC is deleted,
2330 		 * but let's ensure that all our transmit is done before
2331 		 * the client does a mac_client_stop lest it trigger an
2332 		 * assert in the driver.
2333 		 */
2334 		ASSERT(muip->mui_vid == 0);
2335 
2336 		mac_tx_client_flush(mcip);
2337 
2338 		if ((mcip->mci_flags & MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) != 0) {
2339 			mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
2340 			if (mcip->mci_rx_p_fn != NULL) {
2341 				mac_rx_set(mch, mcip->mci_rx_p_fn,
2342 				    mcip->mci_rx_p_arg);
2343 				mcip->mci_rx_p_fn = NULL;
2344 				mcip->mci_rx_p_arg = NULL;
2345 			}
2346 			kmem_free(muip, sizeof (mac_unicast_impl_t));
2347 			i_mac_perim_exit(mip);
2348 			return (0);
2349 		}
2350 		mcip->mci_flags &= ~MAC_CLIENT_FLAGS_VNIC_PRIMARY;
2351 
2352 		if (mcip->mci_state_flags & MCIS_TAG_DISABLE)
2353 			mcip->mci_state_flags &= ~MCIS_TAG_DISABLE;
2354 
2355 		if (mcip->mci_state_flags & MCIS_STRIP_DISABLE)
2356 			mcip->mci_state_flags &= ~MCIS_STRIP_DISABLE;
2357 
2358 		if (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK)
2359 			mcip->mci_state_flags &= ~MCIS_DISABLE_TX_VID_CHECK;
2360 
2361 		kmem_free(muip, sizeof (mac_unicast_impl_t));
2362 		i_mac_perim_exit(mip);
2363 		return (0);
2364 	}
2365 
2366 	ASSERT(muip != NULL);
2367 
2368 	/*
2369 	 * We are removing a passive client, we haven't setup the datapath
2370 	 * for this yet, so nothing much to do.
2371 	 */
2372 	if ((mcip->mci_flags & MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) != 0) {
2373 
2374 		ASSERT((mcip->mci_flent->fe_flags & FE_MC_NO_DATAPATH) != 0);
2375 		ASSERT(mcip->mci_p_unicast_list == muip);
2376 
2377 		mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
2378 
2379 		mcip->mci_p_unicast_list = NULL;
2380 		mcip->mci_rx_p_fn = NULL;
2381 		mcip->mci_rx_p_arg = NULL;
2382 
2383 		mcip->mci_state_flags &= ~MCIS_UNICAST_HW;
2384 
2385 		if (mcip->mci_state_flags & MCIS_TAG_DISABLE)
2386 			mcip->mci_state_flags &= ~MCIS_TAG_DISABLE;
2387 
2388 		if (mcip->mci_state_flags & MCIS_STRIP_DISABLE)
2389 			mcip->mci_state_flags &= ~MCIS_STRIP_DISABLE;
2390 
2391 		if (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK)
2392 			mcip->mci_state_flags &= ~MCIS_DISABLE_TX_VID_CHECK;
2393 
2394 		kmem_free(muip, sizeof (mac_unicast_impl_t));
2395 		i_mac_perim_exit(mip);
2396 		return (0);
2397 	}
2398 	/*
2399 	 * Remove the VID from the list of client's VIDs.
2400 	 */
2401 	pre = mcip->mci_unicast_list;
2402 	if (muip == pre) {
2403 		mcip->mci_unicast_list = muip->mui_next;
2404 	} else {
2405 		while ((pre->mui_next != NULL) && (pre->mui_next != muip))
2406 			pre = pre->mui_next;
2407 		ASSERT(pre->mui_next == muip);
2408 		rw_enter(&mcip->mci_rw_lock, RW_WRITER);
2409 		pre->mui_next = muip->mui_next;
2410 		rw_exit(&mcip->mci_rw_lock);
2411 	}
2412 
2413 	if ((mcip->mci_flags & MAC_CLIENT_FLAGS_PRIMARY) &&
2414 	    muip->mui_vid == 0) {
2415 		mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PRIMARY;
2416 		isprimary = B_TRUE;
2417 	}
2418 	if (!mac_client_single_rcvr(mcip)) {
2419 		/*
2420 		 * This MAC client is shared by more than one unicast
2421 		 * addresses, so we will just remove the flent
2422 		 * corresponding to the address being removed. We don't invoke
2423 		 * mac_rx_classify_flow_rem() since the additional flow is
2424 		 * not associated with its own separate set of SRS and rings,
2425 		 * and these constructs are still needed for the remaining
2426 		 * flows.
2427 		 */
2428 		flent = mac_client_get_flow(mcip, muip);
2429 		ASSERT(flent != NULL);
2430 
2431 		/*
2432 		 * The first one is disappearing, need to make sure
2433 		 * we replace it with another from the list of
2434 		 * shared clients.
2435 		 */
2436 		if (flent == mcip->mci_flent)
2437 			flent = mac_client_swap_mciflent(mcip);
2438 		mac_client_remove_flow_from_list(mcip, flent);
2439 		mac_flow_remove(mip->mi_flow_tab, flent, B_FALSE);
2440 		mac_flow_wait(flent, FLOW_DRIVER_UPCALL);
2441 
2442 		/*
2443 		 * The multicast groups that were added by the client so
2444 		 * far must be removed from the brodcast domain corresponding
2445 		 * to the VID being removed.
2446 		 */
2447 		mac_client_bcast_refresh(mcip, mac_client_update_mcast,
2448 		    (void *)flent, B_FALSE);
2449 
2450 		if (mip->mi_type->mt_brdcst_addr != NULL) {
2451 			mac_bcast_delete(mcip, mip->mi_type->mt_brdcst_addr,
2452 			    muip->mui_vid);
2453 		}
2454 
2455 		FLOW_FINAL_REFRELE(flent);
2456 		ASSERT(!(mcip->mci_state_flags & MCIS_EXCLUSIVE));
2457 		/*
2458 		 * Enable fastpath if this is a VNIC or a VLAN.
2459 		 */
2460 		if (mcip->mci_state_flags & MCIS_IS_VNIC)
2461 			mac_fastpath_enable((mac_handle_t)mip);
2462 		mac_stop((mac_handle_t)mip);
2463 		i_mac_perim_exit(mip);
2464 		return (0);
2465 	}
2466 
2467 	mac_client_datapath_teardown(mch, muip, flent);
2468 
2469 	/*
2470 	 * If we are removing the primary, check if we have a passive primary
2471 	 * client that we need to activate now.
2472 	 */
2473 	if (!isprimary) {
2474 		i_mac_perim_exit(mip);
2475 		return (0);
2476 	}
2477 	mcip = mac_get_passive_primary_client(mip);
2478 	if (mcip != NULL) {
2479 		mac_resource_props_t	mrp;
2480 		mac_unicast_impl_t	*muip;
2481 
2482 		mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
2483 		bzero(&mrp, sizeof (mac_resource_props_t));
2484 		/*
2485 		 * Apply the property cached in the mac_impl_t to the
2486 		 * primary mac client.
2487 		 */
2488 		mac_get_resources((mac_handle_t)mip, &mrp);
2489 		(void) mac_client_set_resources(mch, &mrp);
2490 		ASSERT(mcip->mci_p_unicast_list != NULL);
2491 		muip = mcip->mci_p_unicast_list;
2492 		mcip->mci_p_unicast_list = NULL;
2493 		if (mac_client_datapath_setup(mcip, VLAN_ID_NONE,
2494 		    mip->mi_addr, &mrp, B_TRUE, muip) == 0) {
2495 			if (mcip->mci_rx_p_fn != NULL) {
2496 				mac_rx_set(mch, mcip->mci_rx_p_fn,
2497 				    mcip->mci_rx_p_arg);
2498 				mcip->mci_rx_p_fn = NULL;
2499 				mcip->mci_rx_p_arg = NULL;
2500 			}
2501 		} else {
2502 			kmem_free(muip, sizeof (mac_unicast_impl_t));
2503 		}
2504 	}
2505 	i_mac_perim_exit(mip);
2506 	return (0);
2507 }
2508 
2509 /*
2510  * Multicast add function invoked by MAC clients.
2511  */
2512 int
2513 mac_multicast_add(mac_client_handle_t mch, const uint8_t *addr)
2514 {
2515 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
2516 	mac_impl_t		*mip = mcip->mci_mip;
2517 	flow_entry_t		*flent = mcip->mci_flent_list;
2518 	flow_entry_t		*prev_fe = NULL;
2519 	uint16_t		vid;
2520 	int			err = 0;
2521 
2522 	/* Verify the address is a valid multicast address */
2523 	if ((err = mip->mi_type->mt_ops.mtops_multicst_verify(addr,
2524 	    mip->mi_pdata)) != 0)
2525 		return (err);
2526 
2527 	i_mac_perim_enter(mip);
2528 	while (flent != NULL) {
2529 		vid = i_mac_flow_vid(flent);
2530 
2531 		err = mac_bcast_add((mac_client_impl_t *)mch, addr, vid,
2532 		    MAC_ADDRTYPE_MULTICAST);
2533 		if (err != 0)
2534 			break;
2535 		prev_fe = flent;
2536 		flent = flent->fe_client_next;
2537 	}
2538 
2539 	/*
2540 	 * If we failed adding, then undo all, rather than partial
2541 	 * success.
2542 	 */
2543 	if (flent != NULL && prev_fe != NULL) {
2544 		flent = mcip->mci_flent_list;
2545 		while (flent != prev_fe->fe_client_next) {
2546 			vid = i_mac_flow_vid(flent);
2547 			mac_bcast_delete((mac_client_impl_t *)mch, addr, vid);
2548 			flent = flent->fe_client_next;
2549 		}
2550 	}
2551 	i_mac_perim_exit(mip);
2552 	return (err);
2553 }
2554 
2555 /*
2556  * Multicast delete function invoked by MAC clients.
2557  */
2558 void
2559 mac_multicast_remove(mac_client_handle_t mch, const uint8_t *addr)
2560 {
2561 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
2562 	mac_impl_t		*mip = mcip->mci_mip;
2563 	flow_entry_t		*flent;
2564 	uint16_t		vid;
2565 
2566 	i_mac_perim_enter(mip);
2567 	for (flent = mcip->mci_flent_list; flent != NULL;
2568 	    flent = flent->fe_client_next) {
2569 		vid = i_mac_flow_vid(flent);
2570 		mac_bcast_delete((mac_client_impl_t *)mch, addr, vid);
2571 	}
2572 	i_mac_perim_exit(mip);
2573 }
2574 
2575 /*
2576  * When a MAC client desires to capture packets on an interface,
2577  * it registers a promiscuous call back with mac_promisc_add().
2578  * There are three types of promiscuous callbacks:
2579  *
2580  * * MAC_CLIENT_PROMISC_ALL
2581  *   Captures all packets sent and received by the MAC client,
2582  *   the physical interface, as well as all other MAC clients
2583  *   defined on top of the same MAC.
2584  *
2585  * * MAC_CLIENT_PROMISC_FILTERED
2586  *   Captures all packets sent and received by the MAC client,
2587  *   plus all multicast traffic sent and received by the phyisical
2588  *   interface and the other MAC clients.
2589  *
2590  * * MAC_CLIENT_PROMISC_MULTI
2591  *   Captures all broadcast and multicast packets sent and
2592  *   received by the MAC clients as well as the physical interface.
2593  *
2594  * In all cases, the underlying MAC is put in promiscuous mode.
2595  */
2596 int
2597 mac_promisc_add(mac_client_handle_t mch, mac_client_promisc_type_t type,
2598     mac_rx_t fn, void *arg, mac_promisc_handle_t *mphp, uint16_t flags)
2599 {
2600 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
2601 	mac_impl_t *mip = mcip->mci_mip;
2602 	mac_promisc_impl_t *mpip;
2603 	mac_cb_info_t	*mcbi;
2604 	int rc;
2605 
2606 	i_mac_perim_enter(mip);
2607 
2608 	if ((rc = mac_start((mac_handle_t)mip)) != 0) {
2609 		i_mac_perim_exit(mip);
2610 		return (rc);
2611 	}
2612 
2613 	if ((mcip->mci_state_flags & MCIS_IS_VNIC) &&
2614 	    type == MAC_CLIENT_PROMISC_ALL) {
2615 		/*
2616 		 * The function is being invoked by the upper MAC client
2617 		 * of a VNIC. The VNIC should only see the traffic
2618 		 * it is entitled to.
2619 		 */
2620 		type = MAC_CLIENT_PROMISC_FILTERED;
2621 	}
2622 
2623 
2624 	/*
2625 	 * Turn on promiscuous mode for the underlying NIC.
2626 	 * This is needed even for filtered callbacks which
2627 	 * expect to receive all multicast traffic on the wire.
2628 	 *
2629 	 * Physical promiscuous mode should not be turned on if
2630 	 * MAC_PROMISC_FLAGS_NO_PHYS is set.
2631 	 */
2632 	if ((flags & MAC_PROMISC_FLAGS_NO_PHYS) == 0) {
2633 		if ((rc = i_mac_promisc_set(mip, B_TRUE)) != 0) {
2634 			mac_stop((mac_handle_t)mip);
2635 			i_mac_perim_exit(mip);
2636 			return (rc);
2637 		}
2638 	}
2639 
2640 	mpip = kmem_cache_alloc(mac_promisc_impl_cache, KM_SLEEP);
2641 
2642 	mpip->mpi_type = type;
2643 	mpip->mpi_fn = fn;
2644 	mpip->mpi_arg = arg;
2645 	mpip->mpi_mcip = mcip;
2646 	mpip->mpi_no_tx_loop = ((flags & MAC_PROMISC_FLAGS_NO_TX_LOOP) != 0);
2647 	mpip->mpi_no_phys = ((flags & MAC_PROMISC_FLAGS_NO_PHYS) != 0);
2648 	mpip->mpi_strip_vlan_tag =
2649 	    ((flags & MAC_PROMISC_FLAGS_VLAN_TAG_STRIP) != 0);
2650 
2651 	mcbi = &mip->mi_promisc_cb_info;
2652 	mutex_enter(mcbi->mcbi_lockp);
2653 
2654 	mac_callback_add(&mip->mi_promisc_cb_info, &mcip->mci_promisc_list,
2655 	    &mpip->mpi_mci_link);
2656 	mac_callback_add(&mip->mi_promisc_cb_info, &mip->mi_promisc_list,
2657 	    &mpip->mpi_mi_link);
2658 
2659 	mutex_exit(mcbi->mcbi_lockp);
2660 
2661 	*mphp = (mac_promisc_handle_t)mpip;
2662 	i_mac_perim_exit(mip);
2663 	return (0);
2664 }
2665 
2666 /*
2667  * Remove a multicast address previously aded through mac_promisc_add().
2668  */
2669 void
2670 mac_promisc_remove(mac_promisc_handle_t mph)
2671 {
2672 	mac_promisc_impl_t *mpip = (mac_promisc_impl_t *)mph;
2673 	mac_client_impl_t *mcip = mpip->mpi_mcip;
2674 	mac_impl_t *mip = mcip->mci_mip;
2675 	mac_cb_info_t *mcbi;
2676 	int rv;
2677 
2678 	i_mac_perim_enter(mip);
2679 
2680 	/*
2681 	 * Even if the device can't be reset into normal mode, we still
2682 	 * need to clear the client promisc callbacks. The client may want
2683 	 * to close the mac end point and we can't have stale callbacks.
2684 	 */
2685 	if (!(mpip->mpi_no_phys)) {
2686 		if ((rv = i_mac_promisc_set(mip, B_FALSE)) != 0) {
2687 			cmn_err(CE_WARN, "%s: failed to switch OFF promiscuous"
2688 			    " mode because of error 0x%x", mip->mi_name, rv);
2689 		}
2690 	}
2691 	mcbi = &mip->mi_promisc_cb_info;
2692 	mutex_enter(mcbi->mcbi_lockp);
2693 	if (mac_callback_remove(mcbi, &mip->mi_promisc_list,
2694 	    &mpip->mpi_mi_link)) {
2695 		VERIFY(mac_callback_remove(&mip->mi_promisc_cb_info,
2696 		    &mcip->mci_promisc_list, &mpip->mpi_mci_link));
2697 		kmem_cache_free(mac_promisc_impl_cache, mpip);
2698 	} else {
2699 		mac_callback_remove_wait(&mip->mi_promisc_cb_info);
2700 	}
2701 	mutex_exit(mcbi->mcbi_lockp);
2702 	mac_stop((mac_handle_t)mip);
2703 
2704 	i_mac_perim_exit(mip);
2705 }
2706 
2707 /*
2708  * Reference count the number of active Tx threads. MCI_TX_QUIESCE indicates
2709  * that a control operation wants to quiesce the Tx data flow in which case
2710  * we return an error. Holding any of the per cpu locks ensures that the
2711  * mci_tx_flag won't change.
2712  *
2713  * 'CPU' must be accessed just once and used to compute the index into the
2714  * percpu array, and that index must be used for the entire duration of the
2715  * packet send operation. Note that the thread may be preempted and run on
2716  * another cpu any time and so we can't use 'CPU' more than once for the
2717  * operation.
2718  */
2719 #define	MAC_TX_TRY_HOLD(mcip, mytx, error)				\
2720 {									\
2721 	(error) = 0;							\
2722 	(mytx) = &(mcip)->mci_tx_pcpu[CPU->cpu_seqid & mac_tx_percpu_cnt]; \
2723 	mutex_enter(&(mytx)->pcpu_tx_lock);				\
2724 	if (!((mcip)->mci_tx_flag & MCI_TX_QUIESCE)) {			\
2725 		(mytx)->pcpu_tx_refcnt++;				\
2726 	} else {							\
2727 		(error) = -1;						\
2728 	}								\
2729 	mutex_exit(&(mytx)->pcpu_tx_lock);				\
2730 }
2731 
2732 /*
2733  * Release the reference. If needed, signal any control operation waiting
2734  * for Tx quiescence. The wait and signal are always done using the
2735  * mci_tx_pcpu[0]'s lock
2736  */
2737 #define	MAC_TX_RELE(mcip, mytx) {					\
2738 	mutex_enter(&(mytx)->pcpu_tx_lock);				\
2739 	if (--(mytx)->pcpu_tx_refcnt == 0 &&				\
2740 	    (mcip)->mci_tx_flag & MCI_TX_QUIESCE) {			\
2741 		mutex_exit(&(mytx)->pcpu_tx_lock);			\
2742 		mutex_enter(&(mcip)->mci_tx_pcpu[0].pcpu_tx_lock);	\
2743 		cv_signal(&(mcip)->mci_tx_cv);				\
2744 		mutex_exit(&(mcip)->mci_tx_pcpu[0].pcpu_tx_lock);	\
2745 	} else {							\
2746 		mutex_exit(&(mytx)->pcpu_tx_lock);			\
2747 	}								\
2748 }
2749 
2750 /*
2751  * Bump the count of the number of active Tx threads. This is maintained as
2752  * a per CPU counter. On (CMT kind of) machines with large number of CPUs,
2753  * a single mci_tx_lock may become contended. However a count of the total
2754  * number of Tx threads per client is needed in order to quiesce the Tx side
2755  * prior to reassigning a Tx ring dynamically to another client. The thread
2756  * that needs to quiesce the Tx traffic grabs all the percpu locks and checks
2757  * the sum of the individual percpu refcnts. Each Tx data thread only grabs
2758  * its own percpu lock and increments its own refcnt.
2759  */
2760 void *
2761 mac_tx_hold(mac_client_handle_t mch)
2762 {
2763 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
2764 	mac_tx_percpu_t	*mytx;
2765 	int error;
2766 
2767 	MAC_TX_TRY_HOLD(mcip, mytx, error);
2768 	return (error == 0 ? (void *)mytx : NULL);
2769 }
2770 
2771 void
2772 mac_tx_rele(mac_client_handle_t mch, void *mytx_handle)
2773 {
2774 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
2775 	mac_tx_percpu_t	*mytx = mytx_handle;
2776 
2777 	MAC_TX_RELE(mcip, mytx)
2778 }
2779 
2780 /*
2781  * Send function invoked by MAC clients.
2782  */
2783 mac_tx_cookie_t
2784 mac_tx(mac_client_handle_t mch, mblk_t *mp_chain, uintptr_t hint,
2785     uint16_t flag, mblk_t **ret_mp)
2786 {
2787 	mac_tx_cookie_t		cookie;
2788 	int			error;
2789 	mac_tx_percpu_t		*mytx;
2790 	mac_soft_ring_set_t	*srs;
2791 	flow_entry_t		*flent;
2792 	boolean_t		is_subflow = B_FALSE;
2793 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
2794 	mac_impl_t		*mip = mcip->mci_mip;
2795 	mac_srs_tx_t		*srs_tx;
2796 
2797 	/*
2798 	 * Check whether the active Tx threads count is bumped already.
2799 	 */
2800 	if (!(flag & MAC_TX_NO_HOLD)) {
2801 		MAC_TX_TRY_HOLD(mcip, mytx, error);
2802 		if (error != 0) {
2803 			freemsgchain(mp_chain);
2804 			return (NULL);
2805 		}
2806 	}
2807 
2808 	if (mcip->mci_subflow_tab != NULL &&
2809 	    mcip->mci_subflow_tab->ft_flow_count > 0 &&
2810 	    mac_flow_lookup(mcip->mci_subflow_tab, mp_chain,
2811 	    FLOW_OUTBOUND, &flent) == 0) {
2812 		/*
2813 		 * The main assumption here is that if in the event
2814 		 * we get a chain, all the packets will be classified
2815 		 * to the same Flow/SRS. If this changes for any
2816 		 * reason, the following logic should change as well.
2817 		 * I suppose the fanout_hint also assumes this .
2818 		 */
2819 		ASSERT(flent != NULL);
2820 		is_subflow = B_TRUE;
2821 	} else {
2822 		flent = mcip->mci_flent;
2823 	}
2824 
2825 	srs = flent->fe_tx_srs;
2826 	srs_tx = &srs->srs_tx;
2827 	if (srs_tx->st_mode == SRS_TX_DEFAULT &&
2828 	    (srs->srs_state & SRS_ENQUEUED) == 0 &&
2829 	    mip->mi_nactiveclients == 1 && mip->mi_promisc_list == NULL &&
2830 	    mp_chain->b_next == NULL) {
2831 		uint64_t	obytes;
2832 
2833 		/*
2834 		 * Since dls always opens the underlying MAC, nclients equals
2835 		 * to 1 means that the only active client is dls itself acting
2836 		 * as a primary client of the MAC instance. Since dls will not
2837 		 * send tagged packets in that case, and dls is trusted to send
2838 		 * packets for its allowed VLAN(s), the VLAN tag insertion and
2839 		 * check is required only if nclients is greater than 1.
2840 		 */
2841 		if (mip->mi_nclients > 1) {
2842 			if (MAC_VID_CHECK_NEEDED(mcip)) {
2843 				int	err = 0;
2844 
2845 				MAC_VID_CHECK(mcip, mp_chain, err);
2846 				if (err != 0) {
2847 					freemsg(mp_chain);
2848 					mcip->mci_stat_oerrors++;
2849 					goto done;
2850 				}
2851 			}
2852 			if (MAC_TAG_NEEDED(mcip)) {
2853 				mp_chain = mac_add_vlan_tag(mp_chain, 0,
2854 				    mac_client_vid(mch));
2855 				if (mp_chain == NULL) {
2856 					mcip->mci_stat_oerrors++;
2857 					goto done;
2858 				}
2859 			}
2860 		}
2861 
2862 		obytes = (mp_chain->b_cont == NULL ? MBLKL(mp_chain) :
2863 		    msgdsize(mp_chain));
2864 
2865 		MAC_TX(mip, srs_tx->st_arg2, mp_chain,
2866 		    ((mcip->mci_state_flags & MCIS_SHARE_BOUND) != 0));
2867 
2868 		if (mp_chain == NULL) {
2869 			cookie = NULL;
2870 			mcip->mci_stat_obytes += obytes;
2871 			mcip->mci_stat_opackets += 1;
2872 			if ((srs->srs_type & SRST_FLOW) != 0) {
2873 				FLOW_STAT_UPDATE(flent, obytes, obytes);
2874 				FLOW_STAT_UPDATE(flent, opackets, 1);
2875 			}
2876 		} else {
2877 			mutex_enter(&srs->srs_lock);
2878 			cookie = mac_tx_srs_no_desc(srs, mp_chain,
2879 			    flag, ret_mp);
2880 			mutex_exit(&srs->srs_lock);
2881 		}
2882 	} else {
2883 		cookie = srs_tx->st_func(srs, mp_chain, hint, flag, ret_mp);
2884 	}
2885 
2886 done:
2887 	if (is_subflow)
2888 		FLOW_REFRELE(flent);
2889 
2890 	if (!(flag & MAC_TX_NO_HOLD))
2891 		MAC_TX_RELE(mcip, mytx);
2892 
2893 	return (cookie);
2894 }
2895 
2896 /*
2897  * mac_tx_is_blocked
2898  *
2899  * Given a cookie, it returns if the ring identified by the cookie is
2900  * flow-controlled or not. If NULL is passed in place of a cookie,
2901  * then it finds out if any of the underlying rings belonging to the
2902  * SRS is flow controlled or not and returns that status.
2903  */
2904 /* ARGSUSED */
2905 boolean_t
2906 mac_tx_is_flow_blocked(mac_client_handle_t mch, mac_tx_cookie_t cookie)
2907 {
2908 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
2909 	mac_soft_ring_set_t *mac_srs;
2910 	mac_soft_ring_t *sringp;
2911 	boolean_t blocked = B_FALSE;
2912 	mac_tx_percpu_t *mytx;
2913 	int err;
2914 	int i;
2915 
2916 	/*
2917 	 * Bump the reference count so that mac_srs won't be deleted.
2918 	 * If the client is currently quiesced and we failed to bump
2919 	 * the reference, return B_TRUE so that flow control stays
2920 	 * as enabled.
2921 	 *
2922 	 * Flow control will then be disabled once the client is no
2923 	 * longer quiesced.
2924 	 */
2925 	MAC_TX_TRY_HOLD(mcip, mytx, err);
2926 	if (err != 0)
2927 		return (B_TRUE);
2928 
2929 	if ((mac_srs = MCIP_TX_SRS(mcip)) == NULL) {
2930 		MAC_TX_RELE(mcip, mytx);
2931 		return (B_FALSE);
2932 	}
2933 
2934 	mutex_enter(&mac_srs->srs_lock);
2935 	if (mac_srs->srs_tx.st_mode == SRS_TX_FANOUT) {
2936 		if (cookie != NULL) {
2937 			sringp = (mac_soft_ring_t *)cookie;
2938 			mutex_enter(&sringp->s_ring_lock);
2939 			if (sringp->s_ring_state & S_RING_TX_HIWAT)
2940 				blocked = B_TRUE;
2941 			mutex_exit(&sringp->s_ring_lock);
2942 		} else {
2943 			for (i = 0; i < mac_srs->srs_oth_ring_count; i++) {
2944 				sringp = mac_srs->srs_oth_soft_rings[i];
2945 				mutex_enter(&sringp->s_ring_lock);
2946 				if (sringp->s_ring_state & S_RING_TX_HIWAT) {
2947 					blocked = B_TRUE;
2948 					mutex_exit(&sringp->s_ring_lock);
2949 					break;
2950 				}
2951 				mutex_exit(&sringp->s_ring_lock);
2952 			}
2953 		}
2954 	} else {
2955 		blocked = (mac_srs->srs_state & SRS_TX_HIWAT);
2956 	}
2957 	mutex_exit(&mac_srs->srs_lock);
2958 	MAC_TX_RELE(mcip, mytx);
2959 	return (blocked);
2960 }
2961 
2962 /*
2963  * Check if the MAC client is the primary MAC client.
2964  */
2965 boolean_t
2966 mac_is_primary_client(mac_client_impl_t *mcip)
2967 {
2968 	return (mcip->mci_flags & MAC_CLIENT_FLAGS_PRIMARY);
2969 }
2970 
2971 void
2972 mac_ioctl(mac_handle_t mh, queue_t *wq, mblk_t *bp)
2973 {
2974 	mac_impl_t	*mip = (mac_impl_t *)mh;
2975 	int cmd = ((struct iocblk *)bp->b_rptr)->ioc_cmd;
2976 
2977 	if ((cmd == ND_GET && (mip->mi_callbacks->mc_callbacks & MC_GETPROP)) ||
2978 	    (cmd == ND_SET && (mip->mi_callbacks->mc_callbacks & MC_SETPROP))) {
2979 		/*
2980 		 * If ndd props were registered, call them.
2981 		 * Note that ndd ioctls are Obsolete
2982 		 */
2983 		mac_ndd_ioctl(mip, wq, bp);
2984 		return;
2985 	}
2986 
2987 	/*
2988 	 * Call the driver to handle the ioctl.  The driver may not support
2989 	 * any ioctls, in which case we reply with a NAK on its behalf.
2990 	 */
2991 	if (mip->mi_callbacks->mc_callbacks & MC_IOCTL)
2992 		mip->mi_ioctl(mip->mi_driver, wq, bp);
2993 	else
2994 		miocnak(wq, bp, 0, EINVAL);
2995 }
2996 
2997 /*
2998  * Return the link state of the specified MAC instance.
2999  */
3000 link_state_t
3001 mac_link_get(mac_handle_t mh)
3002 {
3003 	return (((mac_impl_t *)mh)->mi_linkstate);
3004 }
3005 
3006 /*
3007  * Add a mac client specified notification callback. Please see the comments
3008  * above mac_callback_add() for general information about mac callback
3009  * addition/deletion in the presence of mac callback list walkers
3010  */
3011 mac_notify_handle_t
3012 mac_notify_add(mac_handle_t mh, mac_notify_t notify_fn, void *arg)
3013 {
3014 	mac_impl_t		*mip = (mac_impl_t *)mh;
3015 	mac_notify_cb_t		*mncb;
3016 	mac_cb_info_t		*mcbi;
3017 
3018 	/*
3019 	 * Allocate a notify callback structure, fill in the details and
3020 	 * use the mac callback list manipulation functions to chain into
3021 	 * the list of callbacks.
3022 	 */
3023 	mncb = kmem_zalloc(sizeof (mac_notify_cb_t), KM_SLEEP);
3024 	mncb->mncb_fn = notify_fn;
3025 	mncb->mncb_arg = arg;
3026 	mncb->mncb_mip = mip;
3027 	mncb->mncb_link.mcb_objp = mncb;
3028 	mncb->mncb_link.mcb_objsize = sizeof (mac_notify_cb_t);
3029 	mncb->mncb_link.mcb_flags = MCB_NOTIFY_CB_T;
3030 
3031 	mcbi = &mip->mi_notify_cb_info;
3032 
3033 	i_mac_perim_enter(mip);
3034 	mutex_enter(mcbi->mcbi_lockp);
3035 
3036 	mac_callback_add(&mip->mi_notify_cb_info, &mip->mi_notify_cb_list,
3037 	    &mncb->mncb_link);
3038 
3039 	mutex_exit(mcbi->mcbi_lockp);
3040 	i_mac_perim_exit(mip);
3041 	return ((mac_notify_handle_t)mncb);
3042 }
3043 
3044 void
3045 mac_notify_remove_wait(mac_handle_t mh)
3046 {
3047 	mac_impl_t	*mip = (mac_impl_t *)mh;
3048 	mac_cb_info_t	*mcbi = &mip->mi_notify_cb_info;
3049 
3050 	mutex_enter(mcbi->mcbi_lockp);
3051 	mac_callback_remove_wait(&mip->mi_notify_cb_info);
3052 	mutex_exit(mcbi->mcbi_lockp);
3053 }
3054 
3055 /*
3056  * Remove a mac client specified notification callback
3057  */
3058 int
3059 mac_notify_remove(mac_notify_handle_t mnh, boolean_t wait)
3060 {
3061 	mac_notify_cb_t	*mncb = (mac_notify_cb_t *)mnh;
3062 	mac_impl_t	*mip = mncb->mncb_mip;
3063 	mac_cb_info_t	*mcbi;
3064 	int		err = 0;
3065 
3066 	mcbi = &mip->mi_notify_cb_info;
3067 
3068 	i_mac_perim_enter(mip);
3069 	mutex_enter(mcbi->mcbi_lockp);
3070 
3071 	ASSERT(mncb->mncb_link.mcb_objp == mncb);
3072 	/*
3073 	 * If there aren't any list walkers, the remove would succeed
3074 	 * inline, else we wait for the deferred remove to complete
3075 	 */
3076 	if (mac_callback_remove(&mip->mi_notify_cb_info,
3077 	    &mip->mi_notify_cb_list, &mncb->mncb_link)) {
3078 		kmem_free(mncb, sizeof (mac_notify_cb_t));
3079 	} else {
3080 		err = EBUSY;
3081 	}
3082 
3083 	mutex_exit(mcbi->mcbi_lockp);
3084 	i_mac_perim_exit(mip);
3085 
3086 	/*
3087 	 * If we failed to remove the notification callback and "wait" is set
3088 	 * to be B_TRUE, wait for the callback to finish after we exit the
3089 	 * mac perimeter.
3090 	 */
3091 	if (err != 0 && wait) {
3092 		mac_notify_remove_wait((mac_handle_t)mip);
3093 		return (0);
3094 	}
3095 
3096 	return (err);
3097 }
3098 
3099 /*
3100  * Associate resource management callbacks with the specified MAC
3101  * clients.
3102  */
3103 
3104 void
3105 mac_resource_set_common(mac_client_handle_t mch, mac_resource_add_t add,
3106     mac_resource_remove_t remove, mac_resource_quiesce_t quiesce,
3107     mac_resource_restart_t restart, mac_resource_bind_t bind,
3108     void *arg)
3109 {
3110 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
3111 
3112 	mcip->mci_resource_add = add;
3113 	mcip->mci_resource_remove = remove;
3114 	mcip->mci_resource_quiesce = quiesce;
3115 	mcip->mci_resource_restart = restart;
3116 	mcip->mci_resource_bind = bind;
3117 	mcip->mci_resource_arg = arg;
3118 
3119 	if (arg == NULL)
3120 		mcip->mci_state_flags &= ~MCIS_CLIENT_POLL_CAPABLE;
3121 }
3122 
3123 void
3124 mac_resource_set(mac_client_handle_t mch, mac_resource_add_t add, void *arg)
3125 {
3126 	/* update the 'resource_add' callback */
3127 	mac_resource_set_common(mch, add, NULL, NULL, NULL, NULL, arg);
3128 }
3129 
3130 /*
3131  * Sets up the client resources and enable the polling interface over all the
3132  * SRS's and the soft rings of the client
3133  */
3134 void
3135 mac_client_poll_enable(mac_client_handle_t mch)
3136 {
3137 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
3138 	mac_soft_ring_set_t	*mac_srs;
3139 	flow_entry_t		*flent;
3140 	int			i;
3141 
3142 	flent = mcip->mci_flent;
3143 	ASSERT(flent != NULL);
3144 
3145 	for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
3146 		mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
3147 		ASSERT(mac_srs->srs_mcip == mcip);
3148 		mac_srs_client_poll_enable(mcip, mac_srs);
3149 	}
3150 }
3151 
3152 /*
3153  * Tears down the client resources and disable the polling interface over all
3154  * the SRS's and the soft rings of the client
3155  */
3156 void
3157 mac_client_poll_disable(mac_client_handle_t mch)
3158 {
3159 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
3160 	mac_soft_ring_set_t	*mac_srs;
3161 	flow_entry_t		*flent;
3162 	int			i;
3163 
3164 	flent = mcip->mci_flent;
3165 	ASSERT(flent != NULL);
3166 
3167 	for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
3168 		mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
3169 		ASSERT(mac_srs->srs_mcip == mcip);
3170 		mac_srs_client_poll_disable(mcip, mac_srs);
3171 	}
3172 }
3173 
3174 /*
3175  * Associate the CPUs specified by the given property with a MAC client.
3176  */
3177 int
3178 mac_cpu_set(mac_client_handle_t mch, mac_resource_props_t *mrp)
3179 {
3180 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
3181 	mac_impl_t *mip = mcip->mci_mip;
3182 	int err = 0;
3183 
3184 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
3185 
3186 	if ((err = mac_validate_props(mrp)) != 0)
3187 		return (err);
3188 
3189 	if (MCIP_DATAPATH_SETUP(mcip))
3190 		mac_flow_modify(mip->mi_flow_tab, mcip->mci_flent, mrp);
3191 
3192 	mac_update_resources(mrp, MCIP_RESOURCE_PROPS(mcip), B_FALSE);
3193 	return (0);
3194 }
3195 
3196 /*
3197  * Apply the specified properties to the specified MAC client.
3198  */
3199 int
3200 mac_client_set_resources(mac_client_handle_t mch, mac_resource_props_t *mrp)
3201 {
3202 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
3203 	mac_impl_t *mip = mcip->mci_mip;
3204 	int err = 0;
3205 
3206 	i_mac_perim_enter(mip);
3207 
3208 	if ((mrp->mrp_mask & MRP_MAXBW) || (mrp->mrp_mask & MRP_PRIORITY)) {
3209 		err = mac_resource_ctl_set(mch, mrp);
3210 		if (err != 0) {
3211 			i_mac_perim_exit(mip);
3212 			return (err);
3213 		}
3214 	}
3215 
3216 	if (mrp->mrp_mask & MRP_CPUS)
3217 		err = mac_cpu_set(mch, mrp);
3218 
3219 	i_mac_perim_exit(mip);
3220 	return (err);
3221 }
3222 
3223 /*
3224  * Return the properties currently associated with the specified MAC client.
3225  */
3226 void
3227 mac_client_get_resources(mac_client_handle_t mch, mac_resource_props_t *mrp)
3228 {
3229 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
3230 	mac_resource_props_t	*mcip_mrp = MCIP_RESOURCE_PROPS(mcip);
3231 
3232 	bcopy(mcip_mrp, mrp, sizeof (mac_resource_props_t));
3233 }
3234 
3235 /*
3236  * Pass a copy of the specified packet to the promiscuous callbacks
3237  * of the specified MAC.
3238  *
3239  * If sender is NULL, the function is being invoked for a packet chain
3240  * received from the wire. If sender is non-NULL, it points to
3241  * the MAC client from which the packet is being sent.
3242  *
3243  * The packets are distributed to the promiscuous callbacks as follows:
3244  *
3245  * - all packets are sent to the MAC_CLIENT_PROMISC_ALL callbacks
3246  * - all broadcast and multicast packets are sent to the
3247  *   MAC_CLIENT_PROMISC_FILTER and MAC_CLIENT_PROMISC_MULTI.
3248  *
3249  * The unicast packets of MAC_CLIENT_PROMISC_FILTER callbacks are dispatched
3250  * after classification by mac_rx_deliver().
3251  */
3252 
3253 static void
3254 mac_promisc_dispatch_one(mac_promisc_impl_t *mpip, mblk_t *mp,
3255     boolean_t loopback)
3256 {
3257 	mblk_t *mp_copy;
3258 
3259 	mp_copy = copymsg(mp);
3260 	if (mp_copy == NULL)
3261 		return;
3262 	mp_copy->b_next = NULL;
3263 
3264 	if (mpip->mpi_strip_vlan_tag) {
3265 		if ((mp_copy = mac_strip_vlan_tag_chain(mp_copy)) == NULL)
3266 			return;
3267 	}
3268 	mpip->mpi_fn(mpip->mpi_arg, NULL, mp_copy, loopback);
3269 }
3270 
3271 /*
3272  * Return the VID of a packet. Zero if the packet is not tagged.
3273  */
3274 static uint16_t
3275 mac_ether_vid(mblk_t *mp)
3276 {
3277 	struct ether_header *eth = (struct ether_header *)mp->b_rptr;
3278 
3279 	if (ntohs(eth->ether_type) == ETHERTYPE_VLAN) {
3280 		struct ether_vlan_header *t_evhp =
3281 		    (struct ether_vlan_header *)mp->b_rptr;
3282 		return (VLAN_ID(ntohs(t_evhp->ether_tci)));
3283 	}
3284 
3285 	return (0);
3286 }
3287 
3288 /*
3289  * Return whether the specified packet contains a multicast or broadcast
3290  * destination MAC address.
3291  */
3292 static boolean_t
3293 mac_is_mcast(mac_impl_t *mip, mblk_t *mp)
3294 {
3295 	mac_header_info_t hdr_info;
3296 
3297 	if (mac_header_info((mac_handle_t)mip, mp, &hdr_info) != 0)
3298 		return (B_FALSE);
3299 	return ((hdr_info.mhi_dsttype == MAC_ADDRTYPE_BROADCAST) ||
3300 	    (hdr_info.mhi_dsttype == MAC_ADDRTYPE_MULTICAST));
3301 }
3302 
3303 /*
3304  * Send a copy of an mblk chain to the MAC clients of the specified MAC.
3305  * "sender" points to the sender MAC client for outbound packets, and
3306  * is set to NULL for inbound packets.
3307  */
3308 void
3309 mac_promisc_dispatch(mac_impl_t *mip, mblk_t *mp_chain,
3310     mac_client_impl_t *sender)
3311 {
3312 	mac_promisc_impl_t *mpip;
3313 	mac_cb_t *mcb;
3314 	mblk_t *mp;
3315 	boolean_t is_mcast, is_sender;
3316 
3317 	MAC_PROMISC_WALKER_INC(mip);
3318 	for (mp = mp_chain; mp != NULL; mp = mp->b_next) {
3319 		is_mcast = mac_is_mcast(mip, mp);
3320 		/* send packet to interested callbacks */
3321 		for (mcb = mip->mi_promisc_list; mcb != NULL;
3322 		    mcb = mcb->mcb_nextp) {
3323 			mpip = (mac_promisc_impl_t *)mcb->mcb_objp;
3324 			is_sender = (mpip->mpi_mcip == sender);
3325 
3326 			if (is_sender && mpip->mpi_no_tx_loop)
3327 				/*
3328 				 * The sender doesn't want to receive
3329 				 * copies of the packets it sends.
3330 				 */
3331 				continue;
3332 
3333 			/* this client doesn't need any packets (bridge) */
3334 			if (mpip->mpi_fn == NULL)
3335 				continue;
3336 
3337 			/*
3338 			 * For an ethernet MAC, don't displatch a multicast
3339 			 * packet to a non-PROMISC_ALL callbacks unless the VID
3340 			 * of the packet matches the VID of the client.
3341 			 */
3342 			if (is_mcast &&
3343 			    mpip->mpi_type != MAC_CLIENT_PROMISC_ALL &&
3344 			    !mac_client_check_flow_vid(mpip->mpi_mcip,
3345 			    mac_ether_vid(mp)))
3346 				continue;
3347 
3348 			if (is_sender ||
3349 			    mpip->mpi_type == MAC_CLIENT_PROMISC_ALL ||
3350 			    is_mcast)
3351 				mac_promisc_dispatch_one(mpip, mp, is_sender);
3352 		}
3353 	}
3354 	MAC_PROMISC_WALKER_DCR(mip);
3355 }
3356 
3357 void
3358 mac_promisc_client_dispatch(mac_client_impl_t *mcip, mblk_t *mp_chain)
3359 {
3360 	mac_impl_t		*mip = mcip->mci_mip;
3361 	mac_promisc_impl_t	*mpip;
3362 	boolean_t		is_mcast;
3363 	mblk_t			*mp;
3364 	mac_cb_t		*mcb;
3365 
3366 	/*
3367 	 * The unicast packets for the MAC client still
3368 	 * need to be delivered to the MAC_CLIENT_PROMISC_FILTERED
3369 	 * promiscuous callbacks. The broadcast and multicast
3370 	 * packets were delivered from mac_rx().
3371 	 */
3372 	MAC_PROMISC_WALKER_INC(mip);
3373 	for (mp = mp_chain; mp != NULL; mp = mp->b_next) {
3374 		is_mcast = mac_is_mcast(mip, mp);
3375 		for (mcb = mcip->mci_promisc_list; mcb != NULL;
3376 		    mcb = mcb->mcb_nextp) {
3377 			mpip = (mac_promisc_impl_t *)mcb->mcb_objp;
3378 			if (mpip->mpi_type == MAC_CLIENT_PROMISC_FILTERED &&
3379 			    !is_mcast) {
3380 				mac_promisc_dispatch_one(mpip, mp, B_FALSE);
3381 			}
3382 		}
3383 	}
3384 	MAC_PROMISC_WALKER_DCR(mip);
3385 }
3386 
3387 /*
3388  * Return the margin value currently assigned to the specified MAC instance.
3389  */
3390 void
3391 mac_margin_get(mac_handle_t mh, uint32_t *marginp)
3392 {
3393 	mac_impl_t *mip = (mac_impl_t *)mh;
3394 
3395 	rw_enter(&(mip->mi_rw_lock), RW_READER);
3396 	*marginp = mip->mi_margin;
3397 	rw_exit(&(mip->mi_rw_lock));
3398 }
3399 
3400 /*
3401  * mac_info_get() is used for retrieving the mac_info when a DL_INFO_REQ is
3402  * issued before a DL_ATTACH_REQ. we walk the i_mac_impl_hash table and find
3403  * the first mac_impl_t with a matching driver name; then we copy its mac_info_t
3404  * to the caller. we do all this with i_mac_impl_lock held so the mac_impl_t
3405  * cannot disappear while we are accessing it.
3406  */
3407 typedef struct i_mac_info_state_s {
3408 	const char	*mi_name;
3409 	mac_info_t	*mi_infop;
3410 } i_mac_info_state_t;
3411 
3412 /*ARGSUSED*/
3413 static uint_t
3414 i_mac_info_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
3415 {
3416 	i_mac_info_state_t *statep = arg;
3417 	mac_impl_t *mip = (mac_impl_t *)val;
3418 
3419 	if (mip->mi_state_flags & MIS_DISABLED)
3420 		return (MH_WALK_CONTINUE);
3421 
3422 	if (strcmp(statep->mi_name,
3423 	    ddi_driver_name(mip->mi_dip)) != 0)
3424 		return (MH_WALK_CONTINUE);
3425 
3426 	statep->mi_infop = &mip->mi_info;
3427 	return (MH_WALK_TERMINATE);
3428 }
3429 
3430 boolean_t
3431 mac_info_get(const char *name, mac_info_t *minfop)
3432 {
3433 	i_mac_info_state_t state;
3434 
3435 	rw_enter(&i_mac_impl_lock, RW_READER);
3436 	state.mi_name = name;
3437 	state.mi_infop = NULL;
3438 	mod_hash_walk(i_mac_impl_hash, i_mac_info_walker, &state);
3439 	if (state.mi_infop == NULL) {
3440 		rw_exit(&i_mac_impl_lock);
3441 		return (B_FALSE);
3442 	}
3443 	*minfop = *state.mi_infop;
3444 	rw_exit(&i_mac_impl_lock);
3445 	return (B_TRUE);
3446 }
3447 
3448 /*
3449  * To get the capabilities that MAC layer cares about, such as rings, factory
3450  * mac address, vnic or not, it should directly invoke this function.  If the
3451  * link is part of a bridge, then the only "capability" it has is the inability
3452  * to do zero copy.
3453  */
3454 boolean_t
3455 i_mac_capab_get(mac_handle_t mh, mac_capab_t cap, void *cap_data)
3456 {
3457 	mac_impl_t *mip = (mac_impl_t *)mh;
3458 
3459 	if (mip->mi_bridge_link != NULL)
3460 		return (cap == MAC_CAPAB_NO_ZCOPY);
3461 	else if (mip->mi_callbacks->mc_callbacks & MC_GETCAPAB)
3462 		return (mip->mi_getcapab(mip->mi_driver, cap, cap_data));
3463 	else
3464 		return (B_FALSE);
3465 }
3466 
3467 /*
3468  * Capability query function. If number of active mac clients is greater than
3469  * 1, only limited capabilities can be advertised to the caller no matter the
3470  * driver has certain capability or not. Else, we query the driver to get the
3471  * capability.
3472  */
3473 boolean_t
3474 mac_capab_get(mac_handle_t mh, mac_capab_t cap, void *cap_data)
3475 {
3476 	mac_impl_t *mip = (mac_impl_t *)mh;
3477 
3478 	/*
3479 	 * if mi_nactiveclients > 1, only MAC_CAPAB_LEGACY, MAC_CAPAB_HCKSUM,
3480 	 * MAC_CAPAB_NO_NATIVEVLAN and MAC_CAPAB_NO_ZCOPY can be advertised.
3481 	 */
3482 	if (mip->mi_nactiveclients > 1) {
3483 		switch (cap) {
3484 		case MAC_CAPAB_NO_NATIVEVLAN:
3485 		case MAC_CAPAB_NO_ZCOPY:
3486 			return (B_TRUE);
3487 		case MAC_CAPAB_LEGACY:
3488 		case MAC_CAPAB_HCKSUM:
3489 			break;
3490 		default:
3491 			return (B_FALSE);
3492 		}
3493 	}
3494 
3495 	/* else get capab from driver */
3496 	return (i_mac_capab_get(mh, cap, cap_data));
3497 }
3498 
3499 boolean_t
3500 mac_sap_verify(mac_handle_t mh, uint32_t sap, uint32_t *bind_sap)
3501 {
3502 	mac_impl_t *mip = (mac_impl_t *)mh;
3503 
3504 	return (mip->mi_type->mt_ops.mtops_sap_verify(sap, bind_sap,
3505 	    mip->mi_pdata));
3506 }
3507 
3508 mblk_t *
3509 mac_header(mac_handle_t mh, const uint8_t *daddr, uint32_t sap, mblk_t *payload,
3510     size_t extra_len)
3511 {
3512 	mac_impl_t	*mip = (mac_impl_t *)mh;
3513 	const uint8_t	*hdr_daddr;
3514 
3515 	/*
3516 	 * If the MAC is point-to-point with a fixed destination address, then
3517 	 * we must always use that destination in the MAC header.
3518 	 */
3519 	hdr_daddr = (mip->mi_dstaddr_set ? mip->mi_dstaddr : daddr);
3520 	return (mip->mi_type->mt_ops.mtops_header(mip->mi_addr, hdr_daddr, sap,
3521 	    mip->mi_pdata, payload, extra_len));
3522 }
3523 
3524 int
3525 mac_header_info(mac_handle_t mh, mblk_t *mp, mac_header_info_t *mhip)
3526 {
3527 	mac_impl_t *mip = (mac_impl_t *)mh;
3528 
3529 	return (mip->mi_type->mt_ops.mtops_header_info(mp, mip->mi_pdata,
3530 	    mhip));
3531 }
3532 
3533 mblk_t *
3534 mac_header_cook(mac_handle_t mh, mblk_t *mp)
3535 {
3536 	mac_impl_t *mip = (mac_impl_t *)mh;
3537 
3538 	if (mip->mi_type->mt_ops.mtops_ops & MTOPS_HEADER_COOK) {
3539 		if (DB_REF(mp) > 1) {
3540 			mblk_t *newmp = copymsg(mp);
3541 			if (newmp == NULL)
3542 				return (NULL);
3543 			freemsg(mp);
3544 			mp = newmp;
3545 		}
3546 		return (mip->mi_type->mt_ops.mtops_header_cook(mp,
3547 		    mip->mi_pdata));
3548 	}
3549 	return (mp);
3550 }
3551 
3552 mblk_t *
3553 mac_header_uncook(mac_handle_t mh, mblk_t *mp)
3554 {
3555 	mac_impl_t *mip = (mac_impl_t *)mh;
3556 
3557 	if (mip->mi_type->mt_ops.mtops_ops & MTOPS_HEADER_UNCOOK) {
3558 		if (DB_REF(mp) > 1) {
3559 			mblk_t *newmp = copymsg(mp);
3560 			if (newmp == NULL)
3561 				return (NULL);
3562 			freemsg(mp);
3563 			mp = newmp;
3564 		}
3565 		return (mip->mi_type->mt_ops.mtops_header_uncook(mp,
3566 		    mip->mi_pdata));
3567 	}
3568 	return (mp);
3569 }
3570 
3571 uint_t
3572 mac_addr_len(mac_handle_t mh)
3573 {
3574 	mac_impl_t *mip = (mac_impl_t *)mh;
3575 
3576 	return (mip->mi_type->mt_addr_length);
3577 }
3578 
3579 /* True if a MAC is a VNIC */
3580 boolean_t
3581 mac_is_vnic(mac_handle_t mh)
3582 {
3583 	return (((mac_impl_t *)mh)->mi_state_flags & MIS_IS_VNIC);
3584 }
3585 
3586 mac_handle_t
3587 mac_get_lower_mac_handle(mac_handle_t mh)
3588 {
3589 	mac_impl_t *mip = (mac_impl_t *)mh;
3590 
3591 	ASSERT(mac_is_vnic(mh));
3592 	return (((vnic_t *)mip->mi_driver)->vn_lower_mh);
3593 }
3594 
3595 void
3596 mac_update_resources(mac_resource_props_t *nmrp, mac_resource_props_t *cmrp,
3597     boolean_t is_user_flow)
3598 {
3599 	if (nmrp != NULL && cmrp != NULL) {
3600 		if (nmrp->mrp_mask & MRP_PRIORITY) {
3601 			if (nmrp->mrp_priority == MPL_RESET) {
3602 				cmrp->mrp_mask &= ~MRP_PRIORITY;
3603 				if (is_user_flow) {
3604 					cmrp->mrp_priority =
3605 					    MPL_SUBFLOW_DEFAULT;
3606 				} else {
3607 					cmrp->mrp_priority = MPL_LINK_DEFAULT;
3608 				}
3609 			} else {
3610 				cmrp->mrp_mask |= MRP_PRIORITY;
3611 				cmrp->mrp_priority = nmrp->mrp_priority;
3612 			}
3613 		}
3614 		if (nmrp->mrp_mask & MRP_MAXBW) {
3615 			cmrp->mrp_maxbw = nmrp->mrp_maxbw;
3616 			if (nmrp->mrp_maxbw == MRP_MAXBW_RESETVAL)
3617 				cmrp->mrp_mask &= ~MRP_MAXBW;
3618 			else
3619 				cmrp->mrp_mask |= MRP_MAXBW;
3620 		}
3621 		if (nmrp->mrp_mask & MRP_CPUS)
3622 			MAC_COPY_CPUS(nmrp, cmrp);
3623 	}
3624 }
3625 
3626 /*
3627  * i_mac_set_resources:
3628  *
3629  * This routine associates properties with the primary MAC client of
3630  * the specified MAC instance.
3631  * - Cache the properties in mac_impl_t
3632  * - Apply the properties to the primary MAC client if exists
3633  */
3634 int
3635 i_mac_set_resources(mac_handle_t mh, mac_resource_props_t *mrp)
3636 {
3637 	mac_impl_t		*mip = (mac_impl_t *)mh;
3638 	mac_client_impl_t	*mcip;
3639 	int			err = 0;
3640 	uint32_t		resmask, newresmask;
3641 	mac_resource_props_t	tmrp, umrp;
3642 
3643 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
3644 
3645 	err = mac_validate_props(mrp);
3646 	if (err != 0)
3647 		return (err);
3648 
3649 	bcopy(&mip->mi_resource_props, &umrp, sizeof (mac_resource_props_t));
3650 	resmask = umrp.mrp_mask;
3651 	mac_update_resources(mrp, &umrp, B_FALSE);
3652 	newresmask = umrp.mrp_mask;
3653 
3654 	if (resmask == 0 && newresmask != 0) {
3655 		/*
3656 		 * Bandwidth, priority or cpu link properties configured,
3657 		 * must disable fastpath.
3658 		 */
3659 		if ((err = mac_fastpath_disable((mac_handle_t)mip)) != 0)
3660 			return (err);
3661 	}
3662 
3663 	/*
3664 	 * Since bind_cpu may be modified by mac_client_set_resources()
3665 	 * we use a copy of bind_cpu and finally cache bind_cpu in mip.
3666 	 * This allows us to cache only user edits in mip.
3667 	 */
3668 	bcopy(mrp, &tmrp, sizeof (mac_resource_props_t));
3669 	mcip = mac_primary_client_handle(mip);
3670 	if (mcip != NULL && (mcip->mci_state_flags & MCIS_IS_AGGR_PORT) == 0) {
3671 		err =
3672 		    mac_client_set_resources((mac_client_handle_t)mcip, &tmrp);
3673 	}
3674 
3675 	/* Only update the values if mac_client_set_resources succeeded */
3676 	if (err == 0) {
3677 		bcopy(&umrp, &mip->mi_resource_props,
3678 		    sizeof (mac_resource_props_t));
3679 		/*
3680 		 * If bankwidth, priority or cpu link properties cleared,
3681 		 * renable fastpath.
3682 		 */
3683 		if (resmask != 0 && newresmask == 0)
3684 			mac_fastpath_enable((mac_handle_t)mip);
3685 	} else if (resmask == 0 && newresmask != 0) {
3686 		mac_fastpath_enable((mac_handle_t)mip);
3687 	}
3688 	return (err);
3689 }
3690 
3691 int
3692 mac_set_resources(mac_handle_t mh, mac_resource_props_t *mrp)
3693 {
3694 	int err;
3695 
3696 	i_mac_perim_enter((mac_impl_t *)mh);
3697 	err = i_mac_set_resources(mh, mrp);
3698 	i_mac_perim_exit((mac_impl_t *)mh);
3699 	return (err);
3700 }
3701 
3702 /*
3703  * Get the properties cached for the specified MAC instance.
3704  */
3705 void
3706 mac_get_resources(mac_handle_t mh, mac_resource_props_t *mrp)
3707 {
3708 	mac_impl_t 		*mip = (mac_impl_t *)mh;
3709 	mac_client_impl_t	*mcip;
3710 
3711 	if (mip->mi_state_flags & MIS_IS_VNIC) {
3712 		mcip = mac_primary_client_handle(mip);
3713 		if (mcip != NULL) {
3714 			mac_client_get_resources((mac_client_handle_t)mcip,
3715 			    mrp);
3716 			return;
3717 		}
3718 	}
3719 	bcopy(&mip->mi_resource_props, mrp, sizeof (mac_resource_props_t));
3720 }
3721 
3722 int
3723 mac_set_pvid(mac_handle_t mh, uint16_t pvid)
3724 {
3725 	mac_impl_t *mip = (mac_impl_t *)mh;
3726 	mac_client_impl_t *mcip;
3727 	mac_unicast_impl_t *muip;
3728 
3729 	i_mac_perim_enter(mip);
3730 	if (pvid != 0) {
3731 		for (mcip = mip->mi_clients_list; mcip != NULL;
3732 		    mcip = mcip->mci_client_next) {
3733 			for (muip = mcip->mci_unicast_list; muip != NULL;
3734 			    muip = muip->mui_next) {
3735 				if (muip->mui_vid == pvid) {
3736 					i_mac_perim_exit(mip);
3737 					return (EBUSY);
3738 				}
3739 			}
3740 		}
3741 	}
3742 	mip->mi_pvid = pvid;
3743 	i_mac_perim_exit(mip);
3744 	return (0);
3745 }
3746 
3747 uint16_t
3748 mac_get_pvid(mac_handle_t mh)
3749 {
3750 	mac_impl_t *mip = (mac_impl_t *)mh;
3751 
3752 	return (mip->mi_pvid);
3753 }
3754 
3755 uint32_t
3756 mac_get_llimit(mac_handle_t mh)
3757 {
3758 	mac_impl_t *mip = (mac_impl_t *)mh;
3759 
3760 	return (mip->mi_llimit);
3761 }
3762 
3763 uint32_t
3764 mac_get_ldecay(mac_handle_t mh)
3765 {
3766 	mac_impl_t *mip = (mac_impl_t *)mh;
3767 
3768 	return (mip->mi_ldecay);
3769 }
3770 
3771 /*
3772  * Rename a mac client, its flow, and the kstat.
3773  */
3774 int
3775 mac_rename_primary(mac_handle_t mh, const char *new_name)
3776 {
3777 	mac_impl_t		*mip = (mac_impl_t *)mh;
3778 	mac_client_impl_t	*cur_clnt = NULL;
3779 	flow_entry_t		*fep;
3780 
3781 	i_mac_perim_enter(mip);
3782 
3783 	/*
3784 	 * VNICs: we need to change the sys flow name and
3785 	 * the associated flow kstat.
3786 	 */
3787 	if (mip->mi_state_flags & MIS_IS_VNIC) {
3788 		ASSERT(new_name != NULL);
3789 		mac_rename_flow_names(mac_vnic_lower(mip), new_name);
3790 		goto done;
3791 	}
3792 	/*
3793 	 * This mac may itself be an aggr link, or it may have some client
3794 	 * which is an aggr port. For both cases, we need to change the
3795 	 * aggr port's mac client name, its flow name and the associated flow
3796 	 * kstat.
3797 	 */
3798 	if (mip->mi_state_flags & MIS_IS_AGGR) {
3799 		mac_capab_aggr_t aggr_cap;
3800 		mac_rename_fn_t rename_fn;
3801 		boolean_t ret;
3802 
3803 		ASSERT(new_name != NULL);
3804 		ret = i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_AGGR,
3805 		    (void *)(&aggr_cap));
3806 		ASSERT(ret == B_TRUE);
3807 		rename_fn = aggr_cap.mca_rename_fn;
3808 		rename_fn(new_name, mip->mi_driver);
3809 		/*
3810 		 * The aggr's client name and kstat flow name will be
3811 		 * updated below, i.e. via mac_rename_flow_names.
3812 		 */
3813 	}
3814 
3815 	for (cur_clnt = mip->mi_clients_list; cur_clnt != NULL;
3816 	    cur_clnt = cur_clnt->mci_client_next) {
3817 		if (cur_clnt->mci_state_flags & MCIS_IS_AGGR_PORT) {
3818 			if (new_name != NULL) {
3819 				char *str_st = cur_clnt->mci_name;
3820 				char *str_del = strchr(str_st, '-');
3821 
3822 				ASSERT(str_del != NULL);
3823 				bzero(str_del + 1, MAXNAMELEN -
3824 				    (str_del - str_st + 1));
3825 				bcopy(new_name, str_del + 1,
3826 				    strlen(new_name));
3827 			}
3828 			fep = cur_clnt->mci_flent;
3829 			mac_rename_flow(fep, cur_clnt->mci_name);
3830 			break;
3831 		} else if (new_name != NULL &&
3832 		    cur_clnt->mci_state_flags & MCIS_USE_DATALINK_NAME) {
3833 			mac_rename_flow_names(cur_clnt, new_name);
3834 			break;
3835 		}
3836 	}
3837 
3838 done:
3839 	i_mac_perim_exit(mip);
3840 	return (0);
3841 }
3842 
3843 /*
3844  * Rename the MAC client's flow names
3845  */
3846 static void
3847 mac_rename_flow_names(mac_client_impl_t *mcip, const char *new_name)
3848 {
3849 	flow_entry_t	*flent;
3850 	uint16_t	vid;
3851 	char		flowname[MAXFLOWNAMELEN];
3852 	mac_impl_t	*mip = mcip->mci_mip;
3853 
3854 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
3855 
3856 	/*
3857 	 * Use mi_rw_lock to ensure that threads not in the mac perimeter
3858 	 * see a self-consistent value for mci_name
3859 	 */
3860 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
3861 	(void) strlcpy(mcip->mci_name, new_name, sizeof (mcip->mci_name));
3862 	rw_exit(&mip->mi_rw_lock);
3863 
3864 	mac_rename_flow(mcip->mci_flent, new_name);
3865 
3866 	if (mcip->mci_nflents == 1)
3867 		return;
3868 
3869 	/*
3870 	 * We have to rename all the others too, no stats to destroy for
3871 	 * these.
3872 	 */
3873 	for (flent = mcip->mci_flent_list; flent != NULL;
3874 	    flent = flent->fe_client_next) {
3875 		if (flent != mcip->mci_flent) {
3876 			vid = i_mac_flow_vid(flent);
3877 			(void) sprintf(flowname, "%s%u", new_name, vid);
3878 			mac_flow_set_name(flent, flowname);
3879 		}
3880 	}
3881 }
3882 
3883 
3884 /*
3885  * Add a flow to the MAC client's flow list - i.e list of MAC/VID tuples
3886  * defined for the specified MAC client.
3887  */
3888 static void
3889 mac_client_add_to_flow_list(mac_client_impl_t *mcip, flow_entry_t *flent)
3890 {
3891 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
3892 	/*
3893 	 * The promisc Rx data path walks the mci_flent_list. Protect by
3894 	 * using mi_rw_lock
3895 	 */
3896 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
3897 
3898 	/* Add it to the head */
3899 	flent->fe_client_next = mcip->mci_flent_list;
3900 	mcip->mci_flent_list = flent;
3901 	mcip->mci_nflents++;
3902 
3903 	/*
3904 	 * Keep track of the number of non-zero VIDs addresses per MAC
3905 	 * client to avoid figuring it out in the data-path.
3906 	 */
3907 	if (i_mac_flow_vid(flent) != VLAN_ID_NONE)
3908 		mcip->mci_nvids++;
3909 
3910 	rw_exit(&mcip->mci_rw_lock);
3911 }
3912 
3913 /*
3914  * Remove a flow entry from the MAC client's list.
3915  */
3916 static void
3917 mac_client_remove_flow_from_list(mac_client_impl_t *mcip, flow_entry_t *flent)
3918 {
3919 	flow_entry_t	*fe = mcip->mci_flent_list;
3920 	flow_entry_t	*prev_fe = NULL;
3921 
3922 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
3923 	/*
3924 	 * The promisc Rx data path walks the mci_flent_list. Protect by
3925 	 * using mci_rw_lock
3926 	 */
3927 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
3928 	while ((fe != NULL) && (fe != flent)) {
3929 		prev_fe = fe;
3930 		fe = fe->fe_client_next;
3931 	}
3932 
3933 	ASSERT(fe != NULL);
3934 	if (prev_fe == NULL) {
3935 		/* Deleting the first node */
3936 		mcip->mci_flent_list = fe->fe_client_next;
3937 	} else {
3938 		prev_fe->fe_client_next = fe->fe_client_next;
3939 	}
3940 	mcip->mci_nflents--;
3941 
3942 	if (i_mac_flow_vid(flent) != VLAN_ID_NONE)
3943 		mcip->mci_nvids--;
3944 
3945 	rw_exit(&mcip->mci_rw_lock);
3946 }
3947 
3948 /*
3949  * Check if the given VID belongs to this MAC client.
3950  */
3951 boolean_t
3952 mac_client_check_flow_vid(mac_client_impl_t *mcip, uint16_t vid)
3953 {
3954 	flow_entry_t	*flent;
3955 	uint16_t	mci_vid;
3956 
3957 	/* The mci_flent_list is protected by mci_rw_lock */
3958 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
3959 	for (flent = mcip->mci_flent_list; flent != NULL;
3960 	    flent = flent->fe_client_next) {
3961 		mci_vid = i_mac_flow_vid(flent);
3962 		if (vid == mci_vid) {
3963 			rw_exit(&mcip->mci_rw_lock);
3964 			return (B_TRUE);
3965 		}
3966 	}
3967 	rw_exit(&mcip->mci_rw_lock);
3968 	return (B_FALSE);
3969 }
3970 
3971 /*
3972  * Get the flow entry for the specified <MAC addr, VID> tuple.
3973  */
3974 static flow_entry_t *
3975 mac_client_get_flow(mac_client_impl_t *mcip, mac_unicast_impl_t *muip)
3976 {
3977 	mac_address_t *map = mcip->mci_unicast;
3978 	flow_entry_t *flent;
3979 	uint16_t vid;
3980 	flow_desc_t flow_desc;
3981 
3982 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
3983 
3984 	mac_flow_get_desc(mcip->mci_flent, &flow_desc);
3985 	if (bcmp(flow_desc.fd_dst_mac, map->ma_addr, map->ma_len) != 0)
3986 		return (NULL);
3987 
3988 	for (flent = mcip->mci_flent_list; flent != NULL;
3989 	    flent = flent->fe_client_next) {
3990 		vid = i_mac_flow_vid(flent);
3991 		if (vid == muip->mui_vid) {
3992 			return (flent);
3993 		}
3994 	}
3995 
3996 	return (NULL);
3997 }
3998 
3999 /*
4000  * Since mci_flent has the SRSs, when we want to remove it, we replace
4001  * the flow_desc_t in mci_flent with that of an existing flent and then
4002  * remove that flent instead of mci_flent.
4003  */
4004 static flow_entry_t *
4005 mac_client_swap_mciflent(mac_client_impl_t *mcip)
4006 {
4007 	flow_entry_t	*flent = mcip->mci_flent;
4008 	flow_tab_t	*ft = flent->fe_flow_tab;
4009 	flow_entry_t	*flent1;
4010 	flow_desc_t	fl_desc;
4011 	char		fl_name[MAXFLOWNAMELEN];
4012 	int		err;
4013 
4014 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
4015 	ASSERT(mcip->mci_nflents > 1);
4016 
4017 	/* get the next flent following the primary flent  */
4018 	flent1 = mcip->mci_flent_list->fe_client_next;
4019 	ASSERT(flent1 != NULL && flent1->fe_flow_tab == ft);
4020 
4021 	/*
4022 	 * Remove the flent from the flow table before updating the
4023 	 * flow descriptor as the hash depends on the flow descriptor.
4024 	 * This also helps incoming packet classification avoid having
4025 	 * to grab fe_lock. Access to fe_flow_desc of a flent not in the
4026 	 * flow table is done under the fe_lock so that log or stat functions
4027 	 * see a self-consistent fe_flow_desc. The name and desc are specific
4028 	 * to a flow, the rest are shared by all the clients, including
4029 	 * resource control etc.
4030 	 */
4031 	mac_flow_remove(ft, flent, B_TRUE);
4032 	mac_flow_remove(ft, flent1, B_TRUE);
4033 
4034 	bcopy(&flent->fe_flow_desc, &fl_desc, sizeof (flow_desc_t));
4035 	bcopy(flent->fe_flow_name, fl_name, MAXFLOWNAMELEN);
4036 
4037 	/* update the primary flow entry */
4038 	mutex_enter(&flent->fe_lock);
4039 	bcopy(&flent1->fe_flow_desc, &flent->fe_flow_desc,
4040 	    sizeof (flow_desc_t));
4041 	bcopy(&flent1->fe_flow_name, &flent->fe_flow_name, MAXFLOWNAMELEN);
4042 	mutex_exit(&flent->fe_lock);
4043 
4044 	/* update the flow entry that is to be freed */
4045 	mutex_enter(&flent1->fe_lock);
4046 	bcopy(&fl_desc, &flent1->fe_flow_desc, sizeof (flow_desc_t));
4047 	bcopy(fl_name, &flent1->fe_flow_name, MAXFLOWNAMELEN);
4048 	mutex_exit(&flent1->fe_lock);
4049 
4050 	/* now reinsert the flow entries in the table */
4051 	err = mac_flow_add(ft, flent);
4052 	ASSERT(err == 0);
4053 
4054 	err = mac_flow_add(ft, flent1);
4055 	ASSERT(err == 0);
4056 
4057 	return (flent1);
4058 }
4059 
4060 /*
4061  * Return whether there is only one flow entry associated with this
4062  * MAC client.
4063  */
4064 static boolean_t
4065 mac_client_single_rcvr(mac_client_impl_t *mcip)
4066 {
4067 	return (mcip->mci_nflents == 1);
4068 }
4069 
4070 int
4071 mac_validate_props(mac_resource_props_t *mrp)
4072 {
4073 	if (mrp == NULL)
4074 		return (0);
4075 
4076 	if (mrp->mrp_mask & MRP_PRIORITY) {
4077 		mac_priority_level_t	pri = mrp->mrp_priority;
4078 
4079 		if (pri < MPL_LOW || pri > MPL_RESET)
4080 			return (EINVAL);
4081 	}
4082 
4083 	if (mrp->mrp_mask & MRP_MAXBW) {
4084 		uint64_t maxbw = mrp->mrp_maxbw;
4085 
4086 		if (maxbw < MRP_MAXBW_MINVAL && maxbw != 0)
4087 			return (EINVAL);
4088 	}
4089 	if (mrp->mrp_mask & MRP_CPUS) {
4090 		int i, j;
4091 		mac_cpu_mode_t	fanout;
4092 
4093 		if (mrp->mrp_ncpus > ncpus || mrp->mrp_ncpus > MAX_SR_FANOUT)
4094 			return (EINVAL);
4095 
4096 		for (i = 0; i < mrp->mrp_ncpus; i++) {
4097 			for (j = 0; j < mrp->mrp_ncpus; j++) {
4098 				if (i != j &&
4099 				    mrp->mrp_cpu[i] == mrp->mrp_cpu[j]) {
4100 					return (EINVAL);
4101 				}
4102 			}
4103 		}
4104 
4105 		for (i = 0; i < mrp->mrp_ncpus; i++) {
4106 			cpu_t *cp;
4107 			int rv;
4108 
4109 			mutex_enter(&cpu_lock);
4110 			cp = cpu_get(mrp->mrp_cpu[i]);
4111 			if (cp != NULL)
4112 				rv = cpu_is_online(cp);
4113 			else
4114 				rv = 0;
4115 			mutex_exit(&cpu_lock);
4116 			if (rv == 0)
4117 				return (EINVAL);
4118 		}
4119 
4120 		fanout = mrp->mrp_fanout_mode;
4121 		if (fanout < 0 || fanout > MCM_CPUS)
4122 			return (EINVAL);
4123 	}
4124 	return (0);
4125 }
4126 
4127 /*
4128  * Send a MAC_NOTE_LINK notification to all the MAC clients whenever the
4129  * underlying physical link is down. This is to allow MAC clients to
4130  * communicate with other clients.
4131  */
4132 void
4133 mac_virtual_link_update(mac_impl_t *mip)
4134 {
4135 	if (mip->mi_linkstate != LINK_STATE_UP)
4136 		i_mac_notify(mip, MAC_NOTE_LINK);
4137 }
4138 
4139 /*
4140  * For clients that have a pass-thru MAC, e.g. VNIC, we set the VNIC's
4141  * mac handle in the client.
4142  */
4143 void
4144 mac_set_upper_mac(mac_client_handle_t mch, mac_handle_t mh)
4145 {
4146 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
4147 
4148 	mcip->mci_upper_mip = (mac_impl_t *)mh;
4149 }
4150 
4151 /*
4152  * Mark the mac as being used exclusively by the single mac client that is
4153  * doing some control operation on this mac. No further opens of this mac
4154  * will be allowed until this client calls mac_unmark_exclusive. The mac
4155  * client calling this function must already be in the mac perimeter
4156  */
4157 int
4158 mac_mark_exclusive(mac_handle_t mh)
4159 {
4160 	mac_impl_t	*mip = (mac_impl_t *)mh;
4161 
4162 	ASSERT(MAC_PERIM_HELD(mh));
4163 	/*
4164 	 * Look up its entry in the global hash table.
4165 	 */
4166 	rw_enter(&i_mac_impl_lock, RW_WRITER);
4167 	if (mip->mi_state_flags & MIS_DISABLED) {
4168 		rw_exit(&i_mac_impl_lock);
4169 		return (ENOENT);
4170 	}
4171 
4172 	/*
4173 	 * A reference to mac is held even if the link is not plumbed.
4174 	 * In i_dls_link_create() we open the MAC interface and hold the
4175 	 * reference. There is an additional reference for the mac_open
4176 	 * done in acquiring the mac perimeter
4177 	 */
4178 	if (mip->mi_ref != 2) {
4179 		rw_exit(&i_mac_impl_lock);
4180 		return (EBUSY);
4181 	}
4182 
4183 	ASSERT(!(mip->mi_state_flags & MIS_EXCLUSIVE_HELD));
4184 	mip->mi_state_flags |= MIS_EXCLUSIVE_HELD;
4185 	rw_exit(&i_mac_impl_lock);
4186 	return (0);
4187 }
4188 
4189 void
4190 mac_unmark_exclusive(mac_handle_t mh)
4191 {
4192 	mac_impl_t	*mip = (mac_impl_t *)mh;
4193 
4194 	ASSERT(MAC_PERIM_HELD(mh));
4195 
4196 	rw_enter(&i_mac_impl_lock, RW_WRITER);
4197 	/* 1 for the creation and another for the perimeter */
4198 	ASSERT(mip->mi_ref == 2 && (mip->mi_state_flags & MIS_EXCLUSIVE_HELD));
4199 	mip->mi_state_flags &= ~MIS_EXCLUSIVE_HELD;
4200 	rw_exit(&i_mac_impl_lock);
4201 }
4202 
4203 /*
4204  * Set the MTU for the specified MAC.  Note that this mechanism depends on
4205  * the driver calling mac_maxsdu_update() to update the link MTU if it was
4206  * successful in setting its MTU.
4207  *
4208  * Note that there is potential for improvement here.  A better model might be
4209  * to not require drivers to call mac_maxsdu_update(), but rather have this
4210  * function update mi_sdu_max and send notifications if the driver setprop
4211  * callback succeeds.  This would remove the burden and complexity from
4212  * drivers.
4213  */
4214 int
4215 mac_set_mtu(mac_handle_t mh, uint_t new_mtu, uint_t *old_mtu_arg)
4216 {
4217 	mac_impl_t *mip = (mac_impl_t *)mh;
4218 	uint_t old_mtu;
4219 	int rv;
4220 
4221 	i_mac_perim_enter(mip);
4222 
4223 	if (!(mip->mi_callbacks->mc_callbacks & (MC_SETPROP|MC_GETPROP))) {
4224 		rv = ENOTSUP;
4225 		goto bail;
4226 	}
4227 
4228 	old_mtu = mip->mi_sdu_max;
4229 
4230 	if (old_mtu != new_mtu) {
4231 		rv = mip->mi_callbacks->mc_setprop(mip->mi_driver,
4232 		    "mtu", MAC_PROP_MTU, sizeof (uint_t), &new_mtu);
4233 	}
4234 
4235 bail:
4236 	i_mac_perim_exit(mip);
4237 
4238 	if (rv == 0 && old_mtu_arg != NULL)
4239 		*old_mtu_arg = old_mtu;
4240 	return (rv);
4241 }
4242 
4243 void
4244 mac_get_hwgrp_info(mac_handle_t mh, int grp_index, uint_t *grp_num,
4245     uint_t *n_rings, uint_t *type, uint_t *n_clnts, char *clnts_name)
4246 {
4247 	mac_impl_t *mip = (mac_impl_t *)mh;
4248 	mac_grp_client_t *mcip;
4249 	uint_t i = 0, index = 0;
4250 
4251 	/* Revisit when we implement fully dynamic group allocation */
4252 	ASSERT(grp_index >= 0 && grp_index < mip->mi_rx_group_count);
4253 
4254 	rw_enter(&mip->mi_rw_lock, RW_READER);
4255 	*grp_num = mip->mi_rx_groups[grp_index].mrg_index;
4256 	*type = mip->mi_rx_groups[grp_index].mrg_type;
4257 	*n_rings = mip->mi_rx_groups[grp_index].mrg_cur_count;
4258 	for (mcip = mip->mi_rx_groups[grp_index].mrg_clients; mcip != NULL;
4259 	    mcip = mcip->mgc_next) {
4260 		int name_len = strlen(mcip->mgc_client->mci_name);
4261 
4262 		/*
4263 		 * MAXCLIENTNAMELEN is the buffer size reserved for client
4264 		 * names.
4265 		 * XXXX Formating the client name string needs to be moved
4266 		 * to user land when fixing the size of dhi_clnts in
4267 		 * dld_hwgrpinfo_t. We should use n_clients * client_name for
4268 		 * dhi_clntsin instead of MAXCLIENTNAMELEN
4269 		 */
4270 		if (index + name_len >= MAXCLIENTNAMELEN) {
4271 			index = MAXCLIENTNAMELEN;
4272 			break;
4273 		}
4274 		bcopy(mcip->mgc_client->mci_name, &(clnts_name[index]),
4275 		    name_len);
4276 		index += name_len;
4277 		clnts_name[index++] = ',';
4278 		i++;
4279 	}
4280 
4281 	/* Get rid of the last , */
4282 	if (index > 0)
4283 		clnts_name[index - 1] = '\0';
4284 	*n_clnts = i;
4285 	rw_exit(&mip->mi_rw_lock);
4286 }
4287 
4288 uint_t
4289 mac_hwgrp_num(mac_handle_t mh)
4290 {
4291 	mac_impl_t *mip = (mac_impl_t *)mh;
4292 
4293 	return (mip->mi_rx_group_count);
4294 }
4295