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