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