xref: /titanic_50/usr/src/uts/common/io/mac/mac_client.c (revision 628680125482a37a45c692030029fd62a600f914)
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 = NULL;
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 	/*
2816 	 * If mac protection is enabled, only the permissible packets will be
2817 	 * returned by mac_protect_check().
2818 	 */
2819 	if ((mcip->mci_flent->
2820 	    fe_resource_props.mrp_mask & MRP_PROTECT) != 0 &&
2821 	    (mp_chain = mac_protect_check(mch, mp_chain)) == NULL)
2822 		goto done;
2823 
2824 	if (mcip->mci_subflow_tab != NULL &&
2825 	    mcip->mci_subflow_tab->ft_flow_count > 0 &&
2826 	    mac_flow_lookup(mcip->mci_subflow_tab, mp_chain,
2827 	    FLOW_OUTBOUND, &flent) == 0) {
2828 		/*
2829 		 * The main assumption here is that if in the event
2830 		 * we get a chain, all the packets will be classified
2831 		 * to the same Flow/SRS. If this changes for any
2832 		 * reason, the following logic should change as well.
2833 		 * I suppose the fanout_hint also assumes this .
2834 		 */
2835 		ASSERT(flent != NULL);
2836 		is_subflow = B_TRUE;
2837 	} else {
2838 		flent = mcip->mci_flent;
2839 	}
2840 
2841 	srs = flent->fe_tx_srs;
2842 	/*
2843 	 * This is to avoid panics with PF_PACKET that can call mac_tx()
2844 	 * against an interface that is not capable of sending. A rewrite
2845 	 * of the mac datapath is required to remove this limitation.
2846 	 */
2847 	if (srs == NULL) {
2848 		freemsgchain(mp_chain);
2849 		goto done;
2850 	}
2851 
2852 	srs_tx = &srs->srs_tx;
2853 	if (srs_tx->st_mode == SRS_TX_DEFAULT &&
2854 	    (srs->srs_state & SRS_ENQUEUED) == 0 &&
2855 	    mip->mi_nactiveclients == 1 && mip->mi_promisc_list == NULL &&
2856 	    mp_chain->b_next == NULL) {
2857 		uint64_t	obytes;
2858 
2859 		/*
2860 		 * Since dls always opens the underlying MAC, nclients equals
2861 		 * to 1 means that the only active client is dls itself acting
2862 		 * as a primary client of the MAC instance. Since dls will not
2863 		 * send tagged packets in that case, and dls is trusted to send
2864 		 * packets for its allowed VLAN(s), the VLAN tag insertion and
2865 		 * check is required only if nclients is greater than 1.
2866 		 */
2867 		if (mip->mi_nclients > 1) {
2868 			if (MAC_VID_CHECK_NEEDED(mcip)) {
2869 				int	err = 0;
2870 
2871 				MAC_VID_CHECK(mcip, mp_chain, err);
2872 				if (err != 0) {
2873 					freemsg(mp_chain);
2874 					mcip->mci_stat_oerrors++;
2875 					goto done;
2876 				}
2877 			}
2878 			if (MAC_TAG_NEEDED(mcip)) {
2879 				mp_chain = mac_add_vlan_tag(mp_chain, 0,
2880 				    mac_client_vid(mch));
2881 				if (mp_chain == NULL) {
2882 					mcip->mci_stat_oerrors++;
2883 					goto done;
2884 				}
2885 			}
2886 		}
2887 
2888 		obytes = (mp_chain->b_cont == NULL ? MBLKL(mp_chain) :
2889 		    msgdsize(mp_chain));
2890 
2891 		MAC_TX(mip, srs_tx->st_arg2, mp_chain,
2892 		    ((mcip->mci_state_flags & MCIS_SHARE_BOUND) != 0));
2893 
2894 		if (mp_chain == NULL) {
2895 			cookie = NULL;
2896 			mcip->mci_stat_obytes += obytes;
2897 			mcip->mci_stat_opackets += 1;
2898 			if ((srs->srs_type & SRST_FLOW) != 0) {
2899 				FLOW_STAT_UPDATE(flent, obytes, obytes);
2900 				FLOW_STAT_UPDATE(flent, opackets, 1);
2901 			}
2902 		} else {
2903 			mutex_enter(&srs->srs_lock);
2904 			cookie = mac_tx_srs_no_desc(srs, mp_chain,
2905 			    flag, ret_mp);
2906 			mutex_exit(&srs->srs_lock);
2907 		}
2908 	} else {
2909 		cookie = srs_tx->st_func(srs, mp_chain, hint, flag, ret_mp);
2910 	}
2911 
2912 done:
2913 	if (is_subflow)
2914 		FLOW_REFRELE(flent);
2915 
2916 	if (!(flag & MAC_TX_NO_HOLD))
2917 		MAC_TX_RELE(mcip, mytx);
2918 
2919 	return (cookie);
2920 }
2921 
2922 /*
2923  * mac_tx_is_blocked
2924  *
2925  * Given a cookie, it returns if the ring identified by the cookie is
2926  * flow-controlled or not. If NULL is passed in place of a cookie,
2927  * then it finds out if any of the underlying rings belonging to the
2928  * SRS is flow controlled or not and returns that status.
2929  */
2930 /* ARGSUSED */
2931 boolean_t
2932 mac_tx_is_flow_blocked(mac_client_handle_t mch, mac_tx_cookie_t cookie)
2933 {
2934 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
2935 	mac_soft_ring_set_t *mac_srs;
2936 	mac_soft_ring_t *sringp;
2937 	boolean_t blocked = B_FALSE;
2938 	mac_tx_percpu_t *mytx;
2939 	int err;
2940 	int i;
2941 
2942 	/*
2943 	 * Bump the reference count so that mac_srs won't be deleted.
2944 	 * If the client is currently quiesced and we failed to bump
2945 	 * the reference, return B_TRUE so that flow control stays
2946 	 * as enabled.
2947 	 *
2948 	 * Flow control will then be disabled once the client is no
2949 	 * longer quiesced.
2950 	 */
2951 	MAC_TX_TRY_HOLD(mcip, mytx, err);
2952 	if (err != 0)
2953 		return (B_TRUE);
2954 
2955 	if ((mac_srs = MCIP_TX_SRS(mcip)) == NULL) {
2956 		MAC_TX_RELE(mcip, mytx);
2957 		return (B_FALSE);
2958 	}
2959 
2960 	mutex_enter(&mac_srs->srs_lock);
2961 	if (mac_srs->srs_tx.st_mode == SRS_TX_FANOUT) {
2962 		if (cookie != NULL) {
2963 			sringp = (mac_soft_ring_t *)cookie;
2964 			mutex_enter(&sringp->s_ring_lock);
2965 			if (sringp->s_ring_state & S_RING_TX_HIWAT)
2966 				blocked = B_TRUE;
2967 			mutex_exit(&sringp->s_ring_lock);
2968 		} else {
2969 			for (i = 0; i < mac_srs->srs_oth_ring_count; i++) {
2970 				sringp = mac_srs->srs_oth_soft_rings[i];
2971 				mutex_enter(&sringp->s_ring_lock);
2972 				if (sringp->s_ring_state & S_RING_TX_HIWAT) {
2973 					blocked = B_TRUE;
2974 					mutex_exit(&sringp->s_ring_lock);
2975 					break;
2976 				}
2977 				mutex_exit(&sringp->s_ring_lock);
2978 			}
2979 		}
2980 	} else {
2981 		blocked = (mac_srs->srs_state & SRS_TX_HIWAT);
2982 	}
2983 	mutex_exit(&mac_srs->srs_lock);
2984 	MAC_TX_RELE(mcip, mytx);
2985 	return (blocked);
2986 }
2987 
2988 /*
2989  * Check if the MAC client is the primary MAC client.
2990  */
2991 boolean_t
2992 mac_is_primary_client(mac_client_impl_t *mcip)
2993 {
2994 	return (mcip->mci_flags & MAC_CLIENT_FLAGS_PRIMARY);
2995 }
2996 
2997 void
2998 mac_ioctl(mac_handle_t mh, queue_t *wq, mblk_t *bp)
2999 {
3000 	mac_impl_t	*mip = (mac_impl_t *)mh;
3001 	int cmd = ((struct iocblk *)bp->b_rptr)->ioc_cmd;
3002 
3003 	if ((cmd == ND_GET && (mip->mi_callbacks->mc_callbacks & MC_GETPROP)) ||
3004 	    (cmd == ND_SET && (mip->mi_callbacks->mc_callbacks & MC_SETPROP))) {
3005 		/*
3006 		 * If ndd props were registered, call them.
3007 		 * Note that ndd ioctls are Obsolete
3008 		 */
3009 		mac_ndd_ioctl(mip, wq, bp);
3010 		return;
3011 	}
3012 
3013 	/*
3014 	 * Call the driver to handle the ioctl.  The driver may not support
3015 	 * any ioctls, in which case we reply with a NAK on its behalf.
3016 	 */
3017 	if (mip->mi_callbacks->mc_callbacks & MC_IOCTL)
3018 		mip->mi_ioctl(mip->mi_driver, wq, bp);
3019 	else
3020 		miocnak(wq, bp, 0, EINVAL);
3021 }
3022 
3023 /*
3024  * Return the link state of the specified MAC instance.
3025  */
3026 link_state_t
3027 mac_link_get(mac_handle_t mh)
3028 {
3029 	return (((mac_impl_t *)mh)->mi_linkstate);
3030 }
3031 
3032 /*
3033  * Add a mac client specified notification callback. Please see the comments
3034  * above mac_callback_add() for general information about mac callback
3035  * addition/deletion in the presence of mac callback list walkers
3036  */
3037 mac_notify_handle_t
3038 mac_notify_add(mac_handle_t mh, mac_notify_t notify_fn, void *arg)
3039 {
3040 	mac_impl_t		*mip = (mac_impl_t *)mh;
3041 	mac_notify_cb_t		*mncb;
3042 	mac_cb_info_t		*mcbi;
3043 
3044 	/*
3045 	 * Allocate a notify callback structure, fill in the details and
3046 	 * use the mac callback list manipulation functions to chain into
3047 	 * the list of callbacks.
3048 	 */
3049 	mncb = kmem_zalloc(sizeof (mac_notify_cb_t), KM_SLEEP);
3050 	mncb->mncb_fn = notify_fn;
3051 	mncb->mncb_arg = arg;
3052 	mncb->mncb_mip = mip;
3053 	mncb->mncb_link.mcb_objp = mncb;
3054 	mncb->mncb_link.mcb_objsize = sizeof (mac_notify_cb_t);
3055 	mncb->mncb_link.mcb_flags = MCB_NOTIFY_CB_T;
3056 
3057 	mcbi = &mip->mi_notify_cb_info;
3058 
3059 	i_mac_perim_enter(mip);
3060 	mutex_enter(mcbi->mcbi_lockp);
3061 
3062 	mac_callback_add(&mip->mi_notify_cb_info, &mip->mi_notify_cb_list,
3063 	    &mncb->mncb_link);
3064 
3065 	mutex_exit(mcbi->mcbi_lockp);
3066 	i_mac_perim_exit(mip);
3067 	return ((mac_notify_handle_t)mncb);
3068 }
3069 
3070 void
3071 mac_notify_remove_wait(mac_handle_t mh)
3072 {
3073 	mac_impl_t	*mip = (mac_impl_t *)mh;
3074 	mac_cb_info_t	*mcbi = &mip->mi_notify_cb_info;
3075 
3076 	mutex_enter(mcbi->mcbi_lockp);
3077 	mac_callback_remove_wait(&mip->mi_notify_cb_info);
3078 	mutex_exit(mcbi->mcbi_lockp);
3079 }
3080 
3081 /*
3082  * Remove a mac client specified notification callback
3083  */
3084 int
3085 mac_notify_remove(mac_notify_handle_t mnh, boolean_t wait)
3086 {
3087 	mac_notify_cb_t	*mncb = (mac_notify_cb_t *)mnh;
3088 	mac_impl_t	*mip = mncb->mncb_mip;
3089 	mac_cb_info_t	*mcbi;
3090 	int		err = 0;
3091 
3092 	mcbi = &mip->mi_notify_cb_info;
3093 
3094 	i_mac_perim_enter(mip);
3095 	mutex_enter(mcbi->mcbi_lockp);
3096 
3097 	ASSERT(mncb->mncb_link.mcb_objp == mncb);
3098 	/*
3099 	 * If there aren't any list walkers, the remove would succeed
3100 	 * inline, else we wait for the deferred remove to complete
3101 	 */
3102 	if (mac_callback_remove(&mip->mi_notify_cb_info,
3103 	    &mip->mi_notify_cb_list, &mncb->mncb_link)) {
3104 		kmem_free(mncb, sizeof (mac_notify_cb_t));
3105 	} else {
3106 		err = EBUSY;
3107 	}
3108 
3109 	mutex_exit(mcbi->mcbi_lockp);
3110 	i_mac_perim_exit(mip);
3111 
3112 	/*
3113 	 * If we failed to remove the notification callback and "wait" is set
3114 	 * to be B_TRUE, wait for the callback to finish after we exit the
3115 	 * mac perimeter.
3116 	 */
3117 	if (err != 0 && wait) {
3118 		mac_notify_remove_wait((mac_handle_t)mip);
3119 		return (0);
3120 	}
3121 
3122 	return (err);
3123 }
3124 
3125 /*
3126  * Associate resource management callbacks with the specified MAC
3127  * clients.
3128  */
3129 
3130 void
3131 mac_resource_set_common(mac_client_handle_t mch, mac_resource_add_t add,
3132     mac_resource_remove_t remove, mac_resource_quiesce_t quiesce,
3133     mac_resource_restart_t restart, mac_resource_bind_t bind,
3134     void *arg)
3135 {
3136 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
3137 
3138 	mcip->mci_resource_add = add;
3139 	mcip->mci_resource_remove = remove;
3140 	mcip->mci_resource_quiesce = quiesce;
3141 	mcip->mci_resource_restart = restart;
3142 	mcip->mci_resource_bind = bind;
3143 	mcip->mci_resource_arg = arg;
3144 
3145 	if (arg == NULL)
3146 		mcip->mci_state_flags &= ~MCIS_CLIENT_POLL_CAPABLE;
3147 }
3148 
3149 void
3150 mac_resource_set(mac_client_handle_t mch, mac_resource_add_t add, void *arg)
3151 {
3152 	/* update the 'resource_add' callback */
3153 	mac_resource_set_common(mch, add, NULL, NULL, NULL, NULL, arg);
3154 }
3155 
3156 /*
3157  * Sets up the client resources and enable the polling interface over all the
3158  * SRS's and the soft rings of the client
3159  */
3160 void
3161 mac_client_poll_enable(mac_client_handle_t mch)
3162 {
3163 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
3164 	mac_soft_ring_set_t	*mac_srs;
3165 	flow_entry_t		*flent;
3166 	int			i;
3167 
3168 	flent = mcip->mci_flent;
3169 	ASSERT(flent != NULL);
3170 
3171 	for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
3172 		mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
3173 		ASSERT(mac_srs->srs_mcip == mcip);
3174 		mac_srs_client_poll_enable(mcip, mac_srs);
3175 	}
3176 }
3177 
3178 /*
3179  * Tears down the client resources and disable the polling interface over all
3180  * the SRS's and the soft rings of the client
3181  */
3182 void
3183 mac_client_poll_disable(mac_client_handle_t mch)
3184 {
3185 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
3186 	mac_soft_ring_set_t	*mac_srs;
3187 	flow_entry_t		*flent;
3188 	int			i;
3189 
3190 	flent = mcip->mci_flent;
3191 	ASSERT(flent != NULL);
3192 
3193 	for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
3194 		mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
3195 		ASSERT(mac_srs->srs_mcip == mcip);
3196 		mac_srs_client_poll_disable(mcip, mac_srs);
3197 	}
3198 }
3199 
3200 /*
3201  * Associate the CPUs specified by the given property with a MAC client.
3202  */
3203 int
3204 mac_cpu_set(mac_client_handle_t mch, mac_resource_props_t *mrp)
3205 {
3206 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
3207 	mac_impl_t *mip = mcip->mci_mip;
3208 	int err = 0;
3209 
3210 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
3211 
3212 	if ((err = mac_validate_props(mrp)) != 0)
3213 		return (err);
3214 
3215 	if (MCIP_DATAPATH_SETUP(mcip))
3216 		mac_flow_modify(mip->mi_flow_tab, mcip->mci_flent, mrp);
3217 
3218 	mac_update_resources(mrp, MCIP_RESOURCE_PROPS(mcip), B_FALSE);
3219 	return (0);
3220 }
3221 
3222 /*
3223  * Apply the specified properties to the specified MAC client.
3224  */
3225 int
3226 mac_client_set_resources(mac_client_handle_t mch, mac_resource_props_t *mrp)
3227 {
3228 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
3229 	mac_impl_t *mip = mcip->mci_mip;
3230 	int err = 0;
3231 
3232 	i_mac_perim_enter(mip);
3233 
3234 	if ((mrp->mrp_mask & MRP_MAXBW) || (mrp->mrp_mask & MRP_PRIORITY)) {
3235 		err = mac_resource_ctl_set(mch, mrp);
3236 		if (err != 0)
3237 			goto done;
3238 	}
3239 
3240 	if (mrp->mrp_mask & MRP_CPUS) {
3241 		err = mac_cpu_set(mch, mrp);
3242 		if (err != 0)
3243 			goto done;
3244 	}
3245 
3246 	if (mrp->mrp_mask & MRP_PROTECT)
3247 		err = mac_protect_set(mch, mrp);
3248 
3249 done:
3250 	i_mac_perim_exit(mip);
3251 	return (err);
3252 }
3253 
3254 /*
3255  * Return the properties currently associated with the specified MAC client.
3256  */
3257 void
3258 mac_client_get_resources(mac_client_handle_t mch, mac_resource_props_t *mrp)
3259 {
3260 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
3261 	mac_resource_props_t	*mcip_mrp = MCIP_RESOURCE_PROPS(mcip);
3262 
3263 	bcopy(mcip_mrp, mrp, sizeof (mac_resource_props_t));
3264 }
3265 
3266 /*
3267  * Pass a copy of the specified packet to the promiscuous callbacks
3268  * of the specified MAC.
3269  *
3270  * If sender is NULL, the function is being invoked for a packet chain
3271  * received from the wire. If sender is non-NULL, it points to
3272  * the MAC client from which the packet is being sent.
3273  *
3274  * The packets are distributed to the promiscuous callbacks as follows:
3275  *
3276  * - all packets are sent to the MAC_CLIENT_PROMISC_ALL callbacks
3277  * - all broadcast and multicast packets are sent to the
3278  *   MAC_CLIENT_PROMISC_FILTER and MAC_CLIENT_PROMISC_MULTI.
3279  *
3280  * The unicast packets of MAC_CLIENT_PROMISC_FILTER callbacks are dispatched
3281  * after classification by mac_rx_deliver().
3282  */
3283 
3284 static void
3285 mac_promisc_dispatch_one(mac_promisc_impl_t *mpip, mblk_t *mp,
3286     boolean_t loopback)
3287 {
3288 	mblk_t *mp_copy, *mp_next;
3289 
3290 	if (!mpip->mpi_no_copy || mpip->mpi_strip_vlan_tag) {
3291 		mp_copy = copymsg(mp);
3292 		if (mp_copy == NULL)
3293 			return;
3294 
3295 		if (mpip->mpi_strip_vlan_tag) {
3296 			mp_copy = mac_strip_vlan_tag_chain(mp_copy);
3297 			if (mp_copy == NULL)
3298 				return;
3299 		}
3300 		mp_next = NULL;
3301 	} else {
3302 		mp_copy = mp;
3303 		mp_next = mp->b_next;
3304 	}
3305 	mp_copy->b_next = NULL;
3306 
3307 	mpip->mpi_fn(mpip->mpi_arg, NULL, mp_copy, loopback);
3308 	if (mp_copy == mp)
3309 		mp->b_next = mp_next;
3310 }
3311 
3312 /*
3313  * Return the VID of a packet. Zero if the packet is not tagged.
3314  */
3315 static uint16_t
3316 mac_ether_vid(mblk_t *mp)
3317 {
3318 	struct ether_header *eth = (struct ether_header *)mp->b_rptr;
3319 
3320 	if (ntohs(eth->ether_type) == ETHERTYPE_VLAN) {
3321 		struct ether_vlan_header *t_evhp =
3322 		    (struct ether_vlan_header *)mp->b_rptr;
3323 		return (VLAN_ID(ntohs(t_evhp->ether_tci)));
3324 	}
3325 
3326 	return (0);
3327 }
3328 
3329 /*
3330  * Return whether the specified packet contains a multicast or broadcast
3331  * destination MAC address.
3332  */
3333 static boolean_t
3334 mac_is_mcast(mac_impl_t *mip, mblk_t *mp)
3335 {
3336 	mac_header_info_t hdr_info;
3337 
3338 	if (mac_header_info((mac_handle_t)mip, mp, &hdr_info) != 0)
3339 		return (B_FALSE);
3340 	return ((hdr_info.mhi_dsttype == MAC_ADDRTYPE_BROADCAST) ||
3341 	    (hdr_info.mhi_dsttype == MAC_ADDRTYPE_MULTICAST));
3342 }
3343 
3344 /*
3345  * Send a copy of an mblk chain to the MAC clients of the specified MAC.
3346  * "sender" points to the sender MAC client for outbound packets, and
3347  * is set to NULL for inbound packets.
3348  */
3349 void
3350 mac_promisc_dispatch(mac_impl_t *mip, mblk_t *mp_chain,
3351     mac_client_impl_t *sender)
3352 {
3353 	mac_promisc_impl_t *mpip;
3354 	mac_cb_t *mcb;
3355 	mblk_t *mp;
3356 	boolean_t is_mcast, is_sender;
3357 
3358 	MAC_PROMISC_WALKER_INC(mip);
3359 	for (mp = mp_chain; mp != NULL; mp = mp->b_next) {
3360 		is_mcast = mac_is_mcast(mip, mp);
3361 		/* send packet to interested callbacks */
3362 		for (mcb = mip->mi_promisc_list; mcb != NULL;
3363 		    mcb = mcb->mcb_nextp) {
3364 			mpip = (mac_promisc_impl_t *)mcb->mcb_objp;
3365 			is_sender = (mpip->mpi_mcip == sender);
3366 
3367 			if (is_sender && mpip->mpi_no_tx_loop)
3368 				/*
3369 				 * The sender doesn't want to receive
3370 				 * copies of the packets it sends.
3371 				 */
3372 				continue;
3373 
3374 			/* this client doesn't need any packets (bridge) */
3375 			if (mpip->mpi_fn == NULL)
3376 				continue;
3377 
3378 			/*
3379 			 * For an ethernet MAC, don't displatch a multicast
3380 			 * packet to a non-PROMISC_ALL callbacks unless the VID
3381 			 * of the packet matches the VID of the client.
3382 			 */
3383 			if (is_mcast &&
3384 			    mpip->mpi_type != MAC_CLIENT_PROMISC_ALL &&
3385 			    !mac_client_check_flow_vid(mpip->mpi_mcip,
3386 			    mac_ether_vid(mp)))
3387 				continue;
3388 
3389 			if (is_sender ||
3390 			    mpip->mpi_type == MAC_CLIENT_PROMISC_ALL ||
3391 			    is_mcast)
3392 				mac_promisc_dispatch_one(mpip, mp, is_sender);
3393 		}
3394 	}
3395 	MAC_PROMISC_WALKER_DCR(mip);
3396 }
3397 
3398 void
3399 mac_promisc_client_dispatch(mac_client_impl_t *mcip, mblk_t *mp_chain)
3400 {
3401 	mac_impl_t		*mip = mcip->mci_mip;
3402 	mac_promisc_impl_t	*mpip;
3403 	boolean_t		is_mcast;
3404 	mblk_t			*mp;
3405 	mac_cb_t		*mcb;
3406 
3407 	/*
3408 	 * The unicast packets for the MAC client still
3409 	 * need to be delivered to the MAC_CLIENT_PROMISC_FILTERED
3410 	 * promiscuous callbacks. The broadcast and multicast
3411 	 * packets were delivered from mac_rx().
3412 	 */
3413 	MAC_PROMISC_WALKER_INC(mip);
3414 	for (mp = mp_chain; mp != NULL; mp = mp->b_next) {
3415 		is_mcast = mac_is_mcast(mip, mp);
3416 		for (mcb = mcip->mci_promisc_list; mcb != NULL;
3417 		    mcb = mcb->mcb_nextp) {
3418 			mpip = (mac_promisc_impl_t *)mcb->mcb_objp;
3419 			if (mpip->mpi_type == MAC_CLIENT_PROMISC_FILTERED &&
3420 			    !is_mcast) {
3421 				mac_promisc_dispatch_one(mpip, mp, B_FALSE);
3422 			}
3423 		}
3424 	}
3425 	MAC_PROMISC_WALKER_DCR(mip);
3426 }
3427 
3428 /*
3429  * Return the margin value currently assigned to the specified MAC instance.
3430  */
3431 void
3432 mac_margin_get(mac_handle_t mh, uint32_t *marginp)
3433 {
3434 	mac_impl_t *mip = (mac_impl_t *)mh;
3435 
3436 	rw_enter(&(mip->mi_rw_lock), RW_READER);
3437 	*marginp = mip->mi_margin;
3438 	rw_exit(&(mip->mi_rw_lock));
3439 }
3440 
3441 /*
3442  * mac_info_get() is used for retrieving the mac_info when a DL_INFO_REQ is
3443  * issued before a DL_ATTACH_REQ. we walk the i_mac_impl_hash table and find
3444  * the first mac_impl_t with a matching driver name; then we copy its mac_info_t
3445  * to the caller. we do all this with i_mac_impl_lock held so the mac_impl_t
3446  * cannot disappear while we are accessing it.
3447  */
3448 typedef struct i_mac_info_state_s {
3449 	const char	*mi_name;
3450 	mac_info_t	*mi_infop;
3451 } i_mac_info_state_t;
3452 
3453 /*ARGSUSED*/
3454 static uint_t
3455 i_mac_info_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
3456 {
3457 	i_mac_info_state_t *statep = arg;
3458 	mac_impl_t *mip = (mac_impl_t *)val;
3459 
3460 	if (mip->mi_state_flags & MIS_DISABLED)
3461 		return (MH_WALK_CONTINUE);
3462 
3463 	if (strcmp(statep->mi_name,
3464 	    ddi_driver_name(mip->mi_dip)) != 0)
3465 		return (MH_WALK_CONTINUE);
3466 
3467 	statep->mi_infop = &mip->mi_info;
3468 	return (MH_WALK_TERMINATE);
3469 }
3470 
3471 boolean_t
3472 mac_info_get(const char *name, mac_info_t *minfop)
3473 {
3474 	i_mac_info_state_t state;
3475 
3476 	rw_enter(&i_mac_impl_lock, RW_READER);
3477 	state.mi_name = name;
3478 	state.mi_infop = NULL;
3479 	mod_hash_walk(i_mac_impl_hash, i_mac_info_walker, &state);
3480 	if (state.mi_infop == NULL) {
3481 		rw_exit(&i_mac_impl_lock);
3482 		return (B_FALSE);
3483 	}
3484 	*minfop = *state.mi_infop;
3485 	rw_exit(&i_mac_impl_lock);
3486 	return (B_TRUE);
3487 }
3488 
3489 /*
3490  * To get the capabilities that MAC layer cares about, such as rings, factory
3491  * mac address, vnic or not, it should directly invoke this function.  If the
3492  * link is part of a bridge, then the only "capability" it has is the inability
3493  * to do zero copy.
3494  */
3495 boolean_t
3496 i_mac_capab_get(mac_handle_t mh, mac_capab_t cap, void *cap_data)
3497 {
3498 	mac_impl_t *mip = (mac_impl_t *)mh;
3499 
3500 	if (mip->mi_bridge_link != NULL)
3501 		return (cap == MAC_CAPAB_NO_ZCOPY);
3502 	else if (mip->mi_callbacks->mc_callbacks & MC_GETCAPAB)
3503 		return (mip->mi_getcapab(mip->mi_driver, cap, cap_data));
3504 	else
3505 		return (B_FALSE);
3506 }
3507 
3508 /*
3509  * Capability query function. If number of active mac clients is greater than
3510  * 1, only limited capabilities can be advertised to the caller no matter the
3511  * driver has certain capability or not. Else, we query the driver to get the
3512  * capability.
3513  */
3514 boolean_t
3515 mac_capab_get(mac_handle_t mh, mac_capab_t cap, void *cap_data)
3516 {
3517 	mac_impl_t *mip = (mac_impl_t *)mh;
3518 
3519 	/*
3520 	 * if mi_nactiveclients > 1, only MAC_CAPAB_LEGACY, MAC_CAPAB_HCKSUM,
3521 	 * MAC_CAPAB_NO_NATIVEVLAN and MAC_CAPAB_NO_ZCOPY can be advertised.
3522 	 */
3523 	if (mip->mi_nactiveclients > 1) {
3524 		switch (cap) {
3525 		case MAC_CAPAB_NO_NATIVEVLAN:
3526 		case MAC_CAPAB_NO_ZCOPY:
3527 			return (B_TRUE);
3528 		case MAC_CAPAB_LEGACY:
3529 		case MAC_CAPAB_HCKSUM:
3530 			break;
3531 		default:
3532 			return (B_FALSE);
3533 		}
3534 	}
3535 
3536 	/* else get capab from driver */
3537 	return (i_mac_capab_get(mh, cap, cap_data));
3538 }
3539 
3540 boolean_t
3541 mac_sap_verify(mac_handle_t mh, uint32_t sap, uint32_t *bind_sap)
3542 {
3543 	mac_impl_t *mip = (mac_impl_t *)mh;
3544 
3545 	return (mip->mi_type->mt_ops.mtops_sap_verify(sap, bind_sap,
3546 	    mip->mi_pdata));
3547 }
3548 
3549 mblk_t *
3550 mac_header(mac_handle_t mh, const uint8_t *daddr, uint32_t sap, mblk_t *payload,
3551     size_t extra_len)
3552 {
3553 	mac_impl_t	*mip = (mac_impl_t *)mh;
3554 	const uint8_t	*hdr_daddr;
3555 
3556 	/*
3557 	 * If the MAC is point-to-point with a fixed destination address, then
3558 	 * we must always use that destination in the MAC header.
3559 	 */
3560 	hdr_daddr = (mip->mi_dstaddr_set ? mip->mi_dstaddr : daddr);
3561 	return (mip->mi_type->mt_ops.mtops_header(mip->mi_addr, hdr_daddr, sap,
3562 	    mip->mi_pdata, payload, extra_len));
3563 }
3564 
3565 int
3566 mac_header_info(mac_handle_t mh, mblk_t *mp, mac_header_info_t *mhip)
3567 {
3568 	mac_impl_t *mip = (mac_impl_t *)mh;
3569 
3570 	return (mip->mi_type->mt_ops.mtops_header_info(mp, mip->mi_pdata,
3571 	    mhip));
3572 }
3573 
3574 int
3575 mac_vlan_header_info(mac_handle_t mh, mblk_t *mp, mac_header_info_t *mhip)
3576 {
3577 	mac_impl_t	*mip = (mac_impl_t *)mh;
3578 	boolean_t	is_ethernet = (mip->mi_info.mi_media == DL_ETHER);
3579 	int		err = 0;
3580 
3581 	/*
3582 	 * Packets should always be at least 16 bit aligned.
3583 	 */
3584 	ASSERT(IS_P2ALIGNED(mp->b_rptr, sizeof (uint16_t)));
3585 
3586 	if ((err = mac_header_info(mh, mp, mhip)) != 0)
3587 		return (err);
3588 
3589 	/*
3590 	 * If this is a VLAN-tagged Ethernet packet, then the SAP in the
3591 	 * mac_header_info_t as returned by mac_header_info() is
3592 	 * ETHERTYPE_VLAN. We need to grab the ethertype from the VLAN header.
3593 	 */
3594 	if (is_ethernet && (mhip->mhi_bindsap == ETHERTYPE_VLAN)) {
3595 		struct ether_vlan_header *evhp;
3596 		uint16_t sap;
3597 		mblk_t *tmp = NULL;
3598 		size_t size;
3599 
3600 		size = sizeof (struct ether_vlan_header);
3601 		if (MBLKL(mp) < size) {
3602 			/*
3603 			 * Pullup the message in order to get the MAC header
3604 			 * infomation. Note that this is a read-only function,
3605 			 * we keep the input packet intact.
3606 			 */
3607 			if ((tmp = msgpullup(mp, size)) == NULL)
3608 				return (EINVAL);
3609 
3610 			mp = tmp;
3611 		}
3612 		evhp = (struct ether_vlan_header *)mp->b_rptr;
3613 		sap = ntohs(evhp->ether_type);
3614 		(void) mac_sap_verify(mh, sap, &mhip->mhi_bindsap);
3615 		mhip->mhi_hdrsize = sizeof (struct ether_vlan_header);
3616 		mhip->mhi_tci = ntohs(evhp->ether_tci);
3617 		mhip->mhi_istagged = B_TRUE;
3618 		freemsg(tmp);
3619 
3620 		if (VLAN_CFI(mhip->mhi_tci) != ETHER_CFI)
3621 			return (EINVAL);
3622 	} else {
3623 		mhip->mhi_istagged = B_FALSE;
3624 		mhip->mhi_tci = 0;
3625 	}
3626 
3627 	return (0);
3628 }
3629 
3630 mblk_t *
3631 mac_header_cook(mac_handle_t mh, mblk_t *mp)
3632 {
3633 	mac_impl_t *mip = (mac_impl_t *)mh;
3634 
3635 	if (mip->mi_type->mt_ops.mtops_ops & MTOPS_HEADER_COOK) {
3636 		if (DB_REF(mp) > 1) {
3637 			mblk_t *newmp = copymsg(mp);
3638 			if (newmp == NULL)
3639 				return (NULL);
3640 			freemsg(mp);
3641 			mp = newmp;
3642 		}
3643 		return (mip->mi_type->mt_ops.mtops_header_cook(mp,
3644 		    mip->mi_pdata));
3645 	}
3646 	return (mp);
3647 }
3648 
3649 mblk_t *
3650 mac_header_uncook(mac_handle_t mh, mblk_t *mp)
3651 {
3652 	mac_impl_t *mip = (mac_impl_t *)mh;
3653 
3654 	if (mip->mi_type->mt_ops.mtops_ops & MTOPS_HEADER_UNCOOK) {
3655 		if (DB_REF(mp) > 1) {
3656 			mblk_t *newmp = copymsg(mp);
3657 			if (newmp == NULL)
3658 				return (NULL);
3659 			freemsg(mp);
3660 			mp = newmp;
3661 		}
3662 		return (mip->mi_type->mt_ops.mtops_header_uncook(mp,
3663 		    mip->mi_pdata));
3664 	}
3665 	return (mp);
3666 }
3667 
3668 uint_t
3669 mac_addr_len(mac_handle_t mh)
3670 {
3671 	mac_impl_t *mip = (mac_impl_t *)mh;
3672 
3673 	return (mip->mi_type->mt_addr_length);
3674 }
3675 
3676 /* True if a MAC is a VNIC */
3677 boolean_t
3678 mac_is_vnic(mac_handle_t mh)
3679 {
3680 	return (((mac_impl_t *)mh)->mi_state_flags & MIS_IS_VNIC);
3681 }
3682 
3683 mac_handle_t
3684 mac_get_lower_mac_handle(mac_handle_t mh)
3685 {
3686 	mac_impl_t *mip = (mac_impl_t *)mh;
3687 
3688 	ASSERT(mac_is_vnic(mh));
3689 	return (((vnic_t *)mip->mi_driver)->vn_lower_mh);
3690 }
3691 
3692 void
3693 mac_update_resources(mac_resource_props_t *nmrp, mac_resource_props_t *cmrp,
3694     boolean_t is_user_flow)
3695 {
3696 	if (nmrp != NULL && cmrp != NULL) {
3697 		if (nmrp->mrp_mask & MRP_PRIORITY) {
3698 			if (nmrp->mrp_priority == MPL_RESET) {
3699 				cmrp->mrp_mask &= ~MRP_PRIORITY;
3700 				if (is_user_flow) {
3701 					cmrp->mrp_priority =
3702 					    MPL_SUBFLOW_DEFAULT;
3703 				} else {
3704 					cmrp->mrp_priority = MPL_LINK_DEFAULT;
3705 				}
3706 			} else {
3707 				cmrp->mrp_mask |= MRP_PRIORITY;
3708 				cmrp->mrp_priority = nmrp->mrp_priority;
3709 			}
3710 		}
3711 		if (nmrp->mrp_mask & MRP_MAXBW) {
3712 			cmrp->mrp_maxbw = nmrp->mrp_maxbw;
3713 			if (nmrp->mrp_maxbw == MRP_MAXBW_RESETVAL)
3714 				cmrp->mrp_mask &= ~MRP_MAXBW;
3715 			else
3716 				cmrp->mrp_mask |= MRP_MAXBW;
3717 		}
3718 		if (nmrp->mrp_mask & MRP_CPUS)
3719 			MAC_COPY_CPUS(nmrp, cmrp);
3720 
3721 		if (nmrp->mrp_mask & MRP_PROTECT)
3722 			mac_protect_update(nmrp, cmrp);
3723 	}
3724 }
3725 
3726 /*
3727  * i_mac_set_resources:
3728  *
3729  * This routine associates properties with the primary MAC client of
3730  * the specified MAC instance.
3731  * - Cache the properties in mac_impl_t
3732  * - Apply the properties to the primary MAC client if exists
3733  */
3734 int
3735 i_mac_set_resources(mac_handle_t mh, mac_resource_props_t *mrp)
3736 {
3737 	mac_impl_t		*mip = (mac_impl_t *)mh;
3738 	mac_client_impl_t	*mcip;
3739 	int			err = 0;
3740 	uint32_t		resmask, newresmask;
3741 	mac_resource_props_t	tmrp, umrp;
3742 
3743 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
3744 
3745 	err = mac_validate_props(mrp);
3746 	if (err != 0)
3747 		return (err);
3748 
3749 	bcopy(&mip->mi_resource_props, &umrp, sizeof (mac_resource_props_t));
3750 	resmask = umrp.mrp_mask;
3751 	mac_update_resources(mrp, &umrp, B_FALSE);
3752 	newresmask = umrp.mrp_mask;
3753 
3754 	if (resmask == 0 && newresmask != 0) {
3755 		/*
3756 		 * Bandwidth, priority or cpu link properties configured,
3757 		 * must disable fastpath.
3758 		 */
3759 		if ((err = mac_fastpath_disable((mac_handle_t)mip)) != 0)
3760 			return (err);
3761 	}
3762 
3763 	/*
3764 	 * Since bind_cpu may be modified by mac_client_set_resources()
3765 	 * we use a copy of bind_cpu and finally cache bind_cpu in mip.
3766 	 * This allows us to cache only user edits in mip.
3767 	 */
3768 	bcopy(mrp, &tmrp, sizeof (mac_resource_props_t));
3769 	mcip = mac_primary_client_handle(mip);
3770 	if (mcip != NULL && (mcip->mci_state_flags & MCIS_IS_AGGR_PORT) == 0) {
3771 		err =
3772 		    mac_client_set_resources((mac_client_handle_t)mcip, &tmrp);
3773 	}
3774 
3775 	/* Only update the values if mac_client_set_resources succeeded */
3776 	if (err == 0) {
3777 		bcopy(&umrp, &mip->mi_resource_props,
3778 		    sizeof (mac_resource_props_t));
3779 		/*
3780 		 * If bankwidth, priority or cpu link properties cleared,
3781 		 * renable fastpath.
3782 		 */
3783 		if (resmask != 0 && newresmask == 0)
3784 			mac_fastpath_enable((mac_handle_t)mip);
3785 	} else if (resmask == 0 && newresmask != 0) {
3786 		mac_fastpath_enable((mac_handle_t)mip);
3787 	}
3788 	return (err);
3789 }
3790 
3791 int
3792 mac_set_resources(mac_handle_t mh, mac_resource_props_t *mrp)
3793 {
3794 	int err;
3795 
3796 	i_mac_perim_enter((mac_impl_t *)mh);
3797 	err = i_mac_set_resources(mh, mrp);
3798 	i_mac_perim_exit((mac_impl_t *)mh);
3799 	return (err);
3800 }
3801 
3802 /*
3803  * Get the properties cached for the specified MAC instance.
3804  */
3805 void
3806 mac_get_resources(mac_handle_t mh, mac_resource_props_t *mrp)
3807 {
3808 	mac_impl_t 		*mip = (mac_impl_t *)mh;
3809 	mac_client_impl_t	*mcip;
3810 
3811 	if (mip->mi_state_flags & MIS_IS_VNIC) {
3812 		mcip = mac_primary_client_handle(mip);
3813 		if (mcip != NULL) {
3814 			mac_client_get_resources((mac_client_handle_t)mcip,
3815 			    mrp);
3816 			return;
3817 		}
3818 	}
3819 	bcopy(&mip->mi_resource_props, mrp, sizeof (mac_resource_props_t));
3820 }
3821 
3822 int
3823 mac_set_pvid(mac_handle_t mh, uint16_t pvid)
3824 {
3825 	mac_impl_t *mip = (mac_impl_t *)mh;
3826 	mac_client_impl_t *mcip;
3827 	mac_unicast_impl_t *muip;
3828 
3829 	i_mac_perim_enter(mip);
3830 	if (pvid != 0) {
3831 		for (mcip = mip->mi_clients_list; mcip != NULL;
3832 		    mcip = mcip->mci_client_next) {
3833 			for (muip = mcip->mci_unicast_list; muip != NULL;
3834 			    muip = muip->mui_next) {
3835 				if (muip->mui_vid == pvid) {
3836 					i_mac_perim_exit(mip);
3837 					return (EBUSY);
3838 				}
3839 			}
3840 		}
3841 	}
3842 	mip->mi_pvid = pvid;
3843 	i_mac_perim_exit(mip);
3844 	return (0);
3845 }
3846 
3847 uint16_t
3848 mac_get_pvid(mac_handle_t mh)
3849 {
3850 	mac_impl_t *mip = (mac_impl_t *)mh;
3851 
3852 	return (mip->mi_pvid);
3853 }
3854 
3855 uint32_t
3856 mac_get_llimit(mac_handle_t mh)
3857 {
3858 	mac_impl_t *mip = (mac_impl_t *)mh;
3859 
3860 	return (mip->mi_llimit);
3861 }
3862 
3863 uint32_t
3864 mac_get_ldecay(mac_handle_t mh)
3865 {
3866 	mac_impl_t *mip = (mac_impl_t *)mh;
3867 
3868 	return (mip->mi_ldecay);
3869 }
3870 
3871 /*
3872  * Rename a mac client, its flow, and the kstat.
3873  */
3874 int
3875 mac_rename_primary(mac_handle_t mh, const char *new_name)
3876 {
3877 	mac_impl_t		*mip = (mac_impl_t *)mh;
3878 	mac_client_impl_t	*cur_clnt = NULL;
3879 	flow_entry_t		*fep;
3880 
3881 	i_mac_perim_enter(mip);
3882 
3883 	/*
3884 	 * VNICs: we need to change the sys flow name and
3885 	 * the associated flow kstat.
3886 	 */
3887 	if (mip->mi_state_flags & MIS_IS_VNIC) {
3888 		ASSERT(new_name != NULL);
3889 		mac_rename_flow_names(mac_vnic_lower(mip), new_name);
3890 		goto done;
3891 	}
3892 	/*
3893 	 * This mac may itself be an aggr link, or it may have some client
3894 	 * which is an aggr port. For both cases, we need to change the
3895 	 * aggr port's mac client name, its flow name and the associated flow
3896 	 * kstat.
3897 	 */
3898 	if (mip->mi_state_flags & MIS_IS_AGGR) {
3899 		mac_capab_aggr_t aggr_cap;
3900 		mac_rename_fn_t rename_fn;
3901 		boolean_t ret;
3902 
3903 		ASSERT(new_name != NULL);
3904 		ret = i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_AGGR,
3905 		    (void *)(&aggr_cap));
3906 		ASSERT(ret == B_TRUE);
3907 		rename_fn = aggr_cap.mca_rename_fn;
3908 		rename_fn(new_name, mip->mi_driver);
3909 		/*
3910 		 * The aggr's client name and kstat flow name will be
3911 		 * updated below, i.e. via mac_rename_flow_names.
3912 		 */
3913 	}
3914 
3915 	for (cur_clnt = mip->mi_clients_list; cur_clnt != NULL;
3916 	    cur_clnt = cur_clnt->mci_client_next) {
3917 		if (cur_clnt->mci_state_flags & MCIS_IS_AGGR_PORT) {
3918 			if (new_name != NULL) {
3919 				char *str_st = cur_clnt->mci_name;
3920 				char *str_del = strchr(str_st, '-');
3921 
3922 				ASSERT(str_del != NULL);
3923 				bzero(str_del + 1, MAXNAMELEN -
3924 				    (str_del - str_st + 1));
3925 				bcopy(new_name, str_del + 1,
3926 				    strlen(new_name));
3927 			}
3928 			fep = cur_clnt->mci_flent;
3929 			mac_rename_flow(fep, cur_clnt->mci_name);
3930 			break;
3931 		} else if (new_name != NULL &&
3932 		    cur_clnt->mci_state_flags & MCIS_USE_DATALINK_NAME) {
3933 			mac_rename_flow_names(cur_clnt, new_name);
3934 			break;
3935 		}
3936 	}
3937 
3938 done:
3939 	i_mac_perim_exit(mip);
3940 	return (0);
3941 }
3942 
3943 /*
3944  * Rename the MAC client's flow names
3945  */
3946 static void
3947 mac_rename_flow_names(mac_client_impl_t *mcip, const char *new_name)
3948 {
3949 	flow_entry_t	*flent;
3950 	uint16_t	vid;
3951 	char		flowname[MAXFLOWNAMELEN];
3952 	mac_impl_t	*mip = mcip->mci_mip;
3953 
3954 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
3955 
3956 	/*
3957 	 * Use mi_rw_lock to ensure that threads not in the mac perimeter
3958 	 * see a self-consistent value for mci_name
3959 	 */
3960 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
3961 	(void) strlcpy(mcip->mci_name, new_name, sizeof (mcip->mci_name));
3962 	rw_exit(&mip->mi_rw_lock);
3963 
3964 	mac_rename_flow(mcip->mci_flent, new_name);
3965 
3966 	if (mcip->mci_nflents == 1)
3967 		return;
3968 
3969 	/*
3970 	 * We have to rename all the others too, no stats to destroy for
3971 	 * these.
3972 	 */
3973 	for (flent = mcip->mci_flent_list; flent != NULL;
3974 	    flent = flent->fe_client_next) {
3975 		if (flent != mcip->mci_flent) {
3976 			vid = i_mac_flow_vid(flent);
3977 			(void) sprintf(flowname, "%s%u", new_name, vid);
3978 			mac_flow_set_name(flent, flowname);
3979 		}
3980 	}
3981 }
3982 
3983 
3984 /*
3985  * Add a flow to the MAC client's flow list - i.e list of MAC/VID tuples
3986  * defined for the specified MAC client.
3987  */
3988 static void
3989 mac_client_add_to_flow_list(mac_client_impl_t *mcip, flow_entry_t *flent)
3990 {
3991 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
3992 	/*
3993 	 * The promisc Rx data path walks the mci_flent_list. Protect by
3994 	 * using mi_rw_lock
3995 	 */
3996 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
3997 
3998 	/* Add it to the head */
3999 	flent->fe_client_next = mcip->mci_flent_list;
4000 	mcip->mci_flent_list = flent;
4001 	mcip->mci_nflents++;
4002 
4003 	/*
4004 	 * Keep track of the number of non-zero VIDs addresses per MAC
4005 	 * client to avoid figuring it out in the data-path.
4006 	 */
4007 	if (i_mac_flow_vid(flent) != VLAN_ID_NONE)
4008 		mcip->mci_nvids++;
4009 
4010 	rw_exit(&mcip->mci_rw_lock);
4011 }
4012 
4013 /*
4014  * Remove a flow entry from the MAC client's list.
4015  */
4016 static void
4017 mac_client_remove_flow_from_list(mac_client_impl_t *mcip, flow_entry_t *flent)
4018 {
4019 	flow_entry_t	*fe = mcip->mci_flent_list;
4020 	flow_entry_t	*prev_fe = NULL;
4021 
4022 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
4023 	/*
4024 	 * The promisc Rx data path walks the mci_flent_list. Protect by
4025 	 * using mci_rw_lock
4026 	 */
4027 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
4028 	while ((fe != NULL) && (fe != flent)) {
4029 		prev_fe = fe;
4030 		fe = fe->fe_client_next;
4031 	}
4032 
4033 	ASSERT(fe != NULL);
4034 	if (prev_fe == NULL) {
4035 		/* Deleting the first node */
4036 		mcip->mci_flent_list = fe->fe_client_next;
4037 	} else {
4038 		prev_fe->fe_client_next = fe->fe_client_next;
4039 	}
4040 	mcip->mci_nflents--;
4041 
4042 	if (i_mac_flow_vid(flent) != VLAN_ID_NONE)
4043 		mcip->mci_nvids--;
4044 
4045 	rw_exit(&mcip->mci_rw_lock);
4046 }
4047 
4048 /*
4049  * Check if the given VID belongs to this MAC client.
4050  */
4051 boolean_t
4052 mac_client_check_flow_vid(mac_client_impl_t *mcip, uint16_t vid)
4053 {
4054 	flow_entry_t	*flent;
4055 	uint16_t	mci_vid;
4056 
4057 	/* The mci_flent_list is protected by mci_rw_lock */
4058 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
4059 	for (flent = mcip->mci_flent_list; flent != NULL;
4060 	    flent = flent->fe_client_next) {
4061 		mci_vid = i_mac_flow_vid(flent);
4062 		if (vid == mci_vid) {
4063 			rw_exit(&mcip->mci_rw_lock);
4064 			return (B_TRUE);
4065 		}
4066 	}
4067 	rw_exit(&mcip->mci_rw_lock);
4068 	return (B_FALSE);
4069 }
4070 
4071 /*
4072  * Get the flow entry for the specified <MAC addr, VID> tuple.
4073  */
4074 static flow_entry_t *
4075 mac_client_get_flow(mac_client_impl_t *mcip, mac_unicast_impl_t *muip)
4076 {
4077 	mac_address_t *map = mcip->mci_unicast;
4078 	flow_entry_t *flent;
4079 	uint16_t vid;
4080 	flow_desc_t flow_desc;
4081 
4082 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
4083 
4084 	mac_flow_get_desc(mcip->mci_flent, &flow_desc);
4085 	if (bcmp(flow_desc.fd_dst_mac, map->ma_addr, map->ma_len) != 0)
4086 		return (NULL);
4087 
4088 	for (flent = mcip->mci_flent_list; flent != NULL;
4089 	    flent = flent->fe_client_next) {
4090 		vid = i_mac_flow_vid(flent);
4091 		if (vid == muip->mui_vid) {
4092 			return (flent);
4093 		}
4094 	}
4095 
4096 	return (NULL);
4097 }
4098 
4099 /*
4100  * Since mci_flent has the SRSs, when we want to remove it, we replace
4101  * the flow_desc_t in mci_flent with that of an existing flent and then
4102  * remove that flent instead of mci_flent.
4103  */
4104 static flow_entry_t *
4105 mac_client_swap_mciflent(mac_client_impl_t *mcip)
4106 {
4107 	flow_entry_t	*flent = mcip->mci_flent;
4108 	flow_tab_t	*ft = flent->fe_flow_tab;
4109 	flow_entry_t	*flent1;
4110 	flow_desc_t	fl_desc;
4111 	char		fl_name[MAXFLOWNAMELEN];
4112 	int		err;
4113 
4114 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
4115 	ASSERT(mcip->mci_nflents > 1);
4116 
4117 	/* get the next flent following the primary flent  */
4118 	flent1 = mcip->mci_flent_list->fe_client_next;
4119 	ASSERT(flent1 != NULL && flent1->fe_flow_tab == ft);
4120 
4121 	/*
4122 	 * Remove the flent from the flow table before updating the
4123 	 * flow descriptor as the hash depends on the flow descriptor.
4124 	 * This also helps incoming packet classification avoid having
4125 	 * to grab fe_lock. Access to fe_flow_desc of a flent not in the
4126 	 * flow table is done under the fe_lock so that log or stat functions
4127 	 * see a self-consistent fe_flow_desc. The name and desc are specific
4128 	 * to a flow, the rest are shared by all the clients, including
4129 	 * resource control etc.
4130 	 */
4131 	mac_flow_remove(ft, flent, B_TRUE);
4132 	mac_flow_remove(ft, flent1, B_TRUE);
4133 
4134 	bcopy(&flent->fe_flow_desc, &fl_desc, sizeof (flow_desc_t));
4135 	bcopy(flent->fe_flow_name, fl_name, MAXFLOWNAMELEN);
4136 
4137 	/* update the primary flow entry */
4138 	mutex_enter(&flent->fe_lock);
4139 	bcopy(&flent1->fe_flow_desc, &flent->fe_flow_desc,
4140 	    sizeof (flow_desc_t));
4141 	bcopy(&flent1->fe_flow_name, &flent->fe_flow_name, MAXFLOWNAMELEN);
4142 	mutex_exit(&flent->fe_lock);
4143 
4144 	/* update the flow entry that is to be freed */
4145 	mutex_enter(&flent1->fe_lock);
4146 	bcopy(&fl_desc, &flent1->fe_flow_desc, sizeof (flow_desc_t));
4147 	bcopy(fl_name, &flent1->fe_flow_name, MAXFLOWNAMELEN);
4148 	mutex_exit(&flent1->fe_lock);
4149 
4150 	/* now reinsert the flow entries in the table */
4151 	err = mac_flow_add(ft, flent);
4152 	ASSERT(err == 0);
4153 
4154 	err = mac_flow_add(ft, flent1);
4155 	ASSERT(err == 0);
4156 
4157 	return (flent1);
4158 }
4159 
4160 /*
4161  * Return whether there is only one flow entry associated with this
4162  * MAC client.
4163  */
4164 static boolean_t
4165 mac_client_single_rcvr(mac_client_impl_t *mcip)
4166 {
4167 	return (mcip->mci_nflents == 1);
4168 }
4169 
4170 int
4171 mac_validate_props(mac_resource_props_t *mrp)
4172 {
4173 	if (mrp == NULL)
4174 		return (0);
4175 
4176 	if (mrp->mrp_mask & MRP_PRIORITY) {
4177 		mac_priority_level_t	pri = mrp->mrp_priority;
4178 
4179 		if (pri < MPL_LOW || pri > MPL_RESET)
4180 			return (EINVAL);
4181 	}
4182 
4183 	if (mrp->mrp_mask & MRP_MAXBW) {
4184 		uint64_t maxbw = mrp->mrp_maxbw;
4185 
4186 		if (maxbw < MRP_MAXBW_MINVAL && maxbw != 0)
4187 			return (EINVAL);
4188 	}
4189 	if (mrp->mrp_mask & MRP_CPUS) {
4190 		int i, j;
4191 		mac_cpu_mode_t	fanout;
4192 
4193 		if (mrp->mrp_ncpus > ncpus || mrp->mrp_ncpus > MAX_SR_FANOUT)
4194 			return (EINVAL);
4195 
4196 		for (i = 0; i < mrp->mrp_ncpus; i++) {
4197 			for (j = 0; j < mrp->mrp_ncpus; j++) {
4198 				if (i != j &&
4199 				    mrp->mrp_cpu[i] == mrp->mrp_cpu[j]) {
4200 					return (EINVAL);
4201 				}
4202 			}
4203 		}
4204 
4205 		for (i = 0; i < mrp->mrp_ncpus; i++) {
4206 			cpu_t *cp;
4207 			int rv;
4208 
4209 			mutex_enter(&cpu_lock);
4210 			cp = cpu_get(mrp->mrp_cpu[i]);
4211 			if (cp != NULL)
4212 				rv = cpu_is_online(cp);
4213 			else
4214 				rv = 0;
4215 			mutex_exit(&cpu_lock);
4216 			if (rv == 0)
4217 				return (EINVAL);
4218 		}
4219 
4220 		fanout = mrp->mrp_fanout_mode;
4221 		if (fanout < 0 || fanout > MCM_CPUS)
4222 			return (EINVAL);
4223 	}
4224 
4225 	if (mrp->mrp_mask & MRP_PROTECT) {
4226 		int err = mac_protect_validate(mrp);
4227 		if (err != 0)
4228 			return (err);
4229 	}
4230 	return (0);
4231 }
4232 
4233 /*
4234  * Send a MAC_NOTE_LINK notification to all the MAC clients whenever the
4235  * underlying physical link is down. This is to allow MAC clients to
4236  * communicate with other clients.
4237  */
4238 void
4239 mac_virtual_link_update(mac_impl_t *mip)
4240 {
4241 	if (mip->mi_linkstate != LINK_STATE_UP)
4242 		i_mac_notify(mip, MAC_NOTE_LINK);
4243 }
4244 
4245 /*
4246  * For clients that have a pass-thru MAC, e.g. VNIC, we set the VNIC's
4247  * mac handle in the client.
4248  */
4249 void
4250 mac_set_upper_mac(mac_client_handle_t mch, mac_handle_t mh)
4251 {
4252 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
4253 
4254 	mcip->mci_upper_mip = (mac_impl_t *)mh;
4255 }
4256 
4257 /*
4258  * Mark the mac as being used exclusively by the single mac client that is
4259  * doing some control operation on this mac. No further opens of this mac
4260  * will be allowed until this client calls mac_unmark_exclusive. The mac
4261  * client calling this function must already be in the mac perimeter
4262  */
4263 int
4264 mac_mark_exclusive(mac_handle_t mh)
4265 {
4266 	mac_impl_t	*mip = (mac_impl_t *)mh;
4267 
4268 	ASSERT(MAC_PERIM_HELD(mh));
4269 	/*
4270 	 * Look up its entry in the global hash table.
4271 	 */
4272 	rw_enter(&i_mac_impl_lock, RW_WRITER);
4273 	if (mip->mi_state_flags & MIS_DISABLED) {
4274 		rw_exit(&i_mac_impl_lock);
4275 		return (ENOENT);
4276 	}
4277 
4278 	/*
4279 	 * A reference to mac is held even if the link is not plumbed.
4280 	 * In i_dls_link_create() we open the MAC interface and hold the
4281 	 * reference. There is an additional reference for the mac_open
4282 	 * done in acquiring the mac perimeter
4283 	 */
4284 	if (mip->mi_ref != 2) {
4285 		rw_exit(&i_mac_impl_lock);
4286 		return (EBUSY);
4287 	}
4288 
4289 	ASSERT(!(mip->mi_state_flags & MIS_EXCLUSIVE_HELD));
4290 	mip->mi_state_flags |= MIS_EXCLUSIVE_HELD;
4291 	rw_exit(&i_mac_impl_lock);
4292 	return (0);
4293 }
4294 
4295 void
4296 mac_unmark_exclusive(mac_handle_t mh)
4297 {
4298 	mac_impl_t	*mip = (mac_impl_t *)mh;
4299 
4300 	ASSERT(MAC_PERIM_HELD(mh));
4301 
4302 	rw_enter(&i_mac_impl_lock, RW_WRITER);
4303 	/* 1 for the creation and another for the perimeter */
4304 	ASSERT(mip->mi_ref == 2 && (mip->mi_state_flags & MIS_EXCLUSIVE_HELD));
4305 	mip->mi_state_flags &= ~MIS_EXCLUSIVE_HELD;
4306 	rw_exit(&i_mac_impl_lock);
4307 }
4308 
4309 /*
4310  * Set the MTU for the specified MAC.  Note that this mechanism depends on
4311  * the driver calling mac_maxsdu_update() to update the link MTU if it was
4312  * successful in setting its MTU.
4313  *
4314  * Note that there is potential for improvement here.  A better model might be
4315  * to not require drivers to call mac_maxsdu_update(), but rather have this
4316  * function update mi_sdu_max and send notifications if the driver setprop
4317  * callback succeeds.  This would remove the burden and complexity from
4318  * drivers.
4319  */
4320 int
4321 mac_set_mtu(mac_handle_t mh, uint_t new_mtu, uint_t *old_mtu_arg)
4322 {
4323 	mac_impl_t *mip = (mac_impl_t *)mh;
4324 	uint_t old_mtu;
4325 	int rv = 0;
4326 
4327 	i_mac_perim_enter(mip);
4328 
4329 	if (!(mip->mi_callbacks->mc_callbacks & (MC_SETPROP|MC_GETPROP))) {
4330 		rv = ENOTSUP;
4331 		goto bail;
4332 	}
4333 
4334 	old_mtu = mip->mi_sdu_max;
4335 
4336 	if (old_mtu != new_mtu) {
4337 		rv = mip->mi_callbacks->mc_setprop(mip->mi_driver,
4338 		    "mtu", MAC_PROP_MTU, sizeof (uint_t), &new_mtu);
4339 	}
4340 
4341 bail:
4342 	i_mac_perim_exit(mip);
4343 
4344 	if (rv == 0 && old_mtu_arg != NULL)
4345 		*old_mtu_arg = old_mtu;
4346 	return (rv);
4347 }
4348 
4349 void
4350 mac_get_hwgrp_info(mac_handle_t mh, int grp_index, uint_t *grp_num,
4351     uint_t *n_rings, uint_t *type, uint_t *n_clnts, char *clnts_name)
4352 {
4353 	mac_impl_t *mip = (mac_impl_t *)mh;
4354 	mac_grp_client_t *mcip;
4355 	uint_t i = 0, index = 0;
4356 
4357 	/* Revisit when we implement fully dynamic group allocation */
4358 	ASSERT(grp_index >= 0 && grp_index < mip->mi_rx_group_count);
4359 
4360 	rw_enter(&mip->mi_rw_lock, RW_READER);
4361 	*grp_num = mip->mi_rx_groups[grp_index].mrg_index;
4362 	*type = mip->mi_rx_groups[grp_index].mrg_type;
4363 	*n_rings = mip->mi_rx_groups[grp_index].mrg_cur_count;
4364 	for (mcip = mip->mi_rx_groups[grp_index].mrg_clients; mcip != NULL;
4365 	    mcip = mcip->mgc_next) {
4366 		int name_len = strlen(mcip->mgc_client->mci_name);
4367 
4368 		/*
4369 		 * MAXCLIENTNAMELEN is the buffer size reserved for client
4370 		 * names.
4371 		 * XXXX Formating the client name string needs to be moved
4372 		 * to user land when fixing the size of dhi_clnts in
4373 		 * dld_hwgrpinfo_t. We should use n_clients * client_name for
4374 		 * dhi_clntsin instead of MAXCLIENTNAMELEN
4375 		 */
4376 		if (index + name_len >= MAXCLIENTNAMELEN) {
4377 			index = MAXCLIENTNAMELEN;
4378 			break;
4379 		}
4380 		bcopy(mcip->mgc_client->mci_name, &(clnts_name[index]),
4381 		    name_len);
4382 		index += name_len;
4383 		clnts_name[index++] = ',';
4384 		i++;
4385 	}
4386 
4387 	/* Get rid of the last , */
4388 	if (index > 0)
4389 		clnts_name[index - 1] = '\0';
4390 	*n_clnts = i;
4391 	rw_exit(&mip->mi_rw_lock);
4392 }
4393 
4394 uint_t
4395 mac_hwgrp_num(mac_handle_t mh)
4396 {
4397 	mac_impl_t *mip = (mac_impl_t *)mh;
4398 
4399 	return (mip->mi_rx_group_count);
4400 }
4401