xref: /onnv-gate/usr/src/uts/common/io/mac/mac_client.c (revision 12325:8a132ae95aa2)
18275SEric Cheng /*
28275SEric Cheng  * CDDL HEADER START
38275SEric Cheng  *
48275SEric Cheng  * The contents of this file are subject to the terms of the
58275SEric Cheng  * Common Development and Distribution License (the "License").
68275SEric Cheng  * You may not use this file except in compliance with the License.
78275SEric Cheng  *
88275SEric Cheng  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
98275SEric Cheng  * or http://www.opensolaris.org/os/licensing.
108275SEric Cheng  * See the License for the specific language governing permissions
118275SEric Cheng  * and limitations under the License.
128275SEric Cheng  *
138275SEric Cheng  * When distributing Covered Code, include this CDDL HEADER in each
148275SEric Cheng  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
158275SEric Cheng  * If applicable, add the following below this CDDL HEADER, with the
168275SEric Cheng  * fields enclosed by brackets "[]" replaced with your own identifying
178275SEric Cheng  * information: Portions Copyright [yyyy] [name of copyright owner]
188275SEric Cheng  *
198275SEric Cheng  * CDDL HEADER END
208275SEric Cheng  */
218275SEric Cheng 
228275SEric Cheng /*
2312062SRajagopal.Kunhappan@Sun.COM  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
248275SEric Cheng  */
258275SEric Cheng 
268275SEric Cheng /*
278275SEric Cheng  * - General Introduction:
288275SEric Cheng  *
298275SEric Cheng  * This file contains the implementation of the MAC client kernel
308275SEric Cheng  * API and related code. The MAC client API allows a kernel module
318275SEric Cheng  * to gain access to a MAC instance (physical NIC, link aggregation, etc).
328275SEric Cheng  * It allows a MAC client to associate itself with a MAC address,
338275SEric Cheng  * VLANs, callback functions for data traffic and for promiscuous mode.
348275SEric Cheng  * The MAC client API is also used to specify the properties associated
358275SEric Cheng  * with a MAC client, such as bandwidth limits, priority, CPUS, etc.
368275SEric Cheng  * These properties are further used to determine the hardware resources
378275SEric Cheng  * to allocate to the various MAC clients.
388275SEric Cheng  *
398275SEric Cheng  * - Primary MAC clients:
408275SEric Cheng  *
418275SEric Cheng  * The MAC client API refers to "primary MAC clients". A primary MAC
428275SEric Cheng  * client is a client which "owns" the primary MAC address of
438275SEric Cheng  * the underlying MAC instance. The primary MAC address is called out
448275SEric Cheng  * since it is associated with specific semantics: the primary MAC
458275SEric Cheng  * address is the MAC address which is assigned to the IP interface
468275SEric Cheng  * when it is plumbed, and the primary MAC address is assigned
478275SEric Cheng  * to VLAN data-links. The primary address of a MAC instance can
488275SEric Cheng  * also change dynamically from under the MAC client, for example
498275SEric Cheng  * as a result of a change of state of a link aggregation. In that
508275SEric Cheng  * case the MAC layer automatically updates all data-structures which
518275SEric Cheng  * refer to the current value of the primary MAC address. Typical
528275SEric Cheng  * primary MAC clients are dls, aggr, and xnb. A typical non-primary
538275SEric Cheng  * MAC client is the vnic driver.
548275SEric Cheng  *
558275SEric Cheng  * - Virtual Switching:
568275SEric Cheng  *
578275SEric Cheng  * The MAC layer implements a virtual switch between the MAC clients
588275SEric Cheng  * (primary and non-primary) defined on top of the same underlying
598275SEric Cheng  * NIC (physical, link aggregation, etc). The virtual switch is
608275SEric Cheng  * VLAN-aware, i.e. it allows multiple MAC clients to be member
618275SEric Cheng  * of one or more VLANs, and the virtual switch will distribute
628275SEric Cheng  * multicast tagged packets only to the member of the corresponding
638275SEric Cheng  * VLANs.
648275SEric Cheng  *
658275SEric Cheng  * - Upper vs Lower MAC:
668275SEric Cheng  *
678275SEric Cheng  * Creating a VNIC on top of a MAC instance effectively causes
688275SEric Cheng  * two MAC instances to be layered on top of each other, one for
698275SEric Cheng  * the VNIC(s), one for the underlying MAC instance (physical NIC,
708275SEric Cheng  * link aggregation, etc). In the code below we refer to the
718275SEric Cheng  * underlying NIC as the "lower MAC", and we refer to VNICs as
728275SEric Cheng  * the "upper MAC".
738275SEric Cheng  *
748275SEric Cheng  * - Pass-through for VNICs:
758275SEric Cheng  *
768275SEric Cheng  * When VNICs are created on top of an underlying MAC, this causes
778275SEric Cheng  * a layering of two MAC instances. Since the lower MAC already
788275SEric Cheng  * does the switching and demultiplexing to its MAC clients, the
798275SEric Cheng  * upper MAC would simply have to pass packets to the layer below
808275SEric Cheng  * or above it, which would introduce overhead. In order to avoid
818275SEric Cheng  * this overhead, the MAC layer implements a pass-through mechanism
828275SEric Cheng  * for VNICs. When a VNIC opens the lower MAC instance, it saves
838275SEric Cheng  * the MAC client handle it optains from the MAC layer. When a MAC
848275SEric Cheng  * client opens a VNIC (upper MAC), the MAC layer detects that
858275SEric Cheng  * the MAC being opened is a VNIC, and gets the MAC client handle
868275SEric Cheng  * that the VNIC driver obtained from the lower MAC. This exchange
878275SEric Cheng  * is doing through a private capability between the MAC layer
888275SEric Cheng  * and the VNIC driver. The upper MAC then returns that handle
898275SEric Cheng  * directly to its MAC client. Any operation done by the upper
908275SEric Cheng  * MAC client is now done on the lower MAC client handle, which
918275SEric Cheng  * allows the VNIC driver to be completely bypassed for the
928275SEric Cheng  * performance sensitive data-path.
938275SEric Cheng  *
948275SEric Cheng  */
958275SEric Cheng 
968275SEric Cheng #include <sys/types.h>
978275SEric Cheng #include <sys/conf.h>
988275SEric Cheng #include <sys/id_space.h>
998275SEric Cheng #include <sys/esunddi.h>
1008275SEric Cheng #include <sys/stat.h>
1018275SEric Cheng #include <sys/mkdev.h>
1028275SEric Cheng #include <sys/stream.h>
1038275SEric Cheng #include <sys/strsun.h>
1048275SEric Cheng #include <sys/strsubr.h>
1058275SEric Cheng #include <sys/dlpi.h>
1068275SEric Cheng #include <sys/modhash.h>
1078275SEric Cheng #include <sys/mac_impl.h>
1088275SEric Cheng #include <sys/mac_client_impl.h>
1098275SEric Cheng #include <sys/mac_soft_ring.h>
11011878SVenu.Iyer@Sun.COM #include <sys/mac_stat.h>
1118275SEric Cheng #include <sys/dls.h>
1128275SEric Cheng #include <sys/dld.h>
1138275SEric Cheng #include <sys/modctl.h>
1148275SEric Cheng #include <sys/fs/dv_node.h>
1158275SEric Cheng #include <sys/thread.h>
1168275SEric Cheng #include <sys/proc.h>
1178275SEric Cheng #include <sys/callb.h>
1188275SEric Cheng #include <sys/cpuvar.h>
1198275SEric Cheng #include <sys/atomic.h>
1208275SEric Cheng #include <sys/sdt.h>
1218275SEric Cheng #include <sys/mac_flow.h>
1228275SEric Cheng #include <sys/ddi_intr_impl.h>
1238275SEric Cheng #include <sys/disp.h>
1248275SEric Cheng #include <sys/sdt.h>
1258275SEric Cheng #include <sys/vnic.h>
1268275SEric Cheng #include <sys/vnic_impl.h>
1278275SEric Cheng #include <sys/vlan.h>
1288275SEric Cheng #include <inet/ip.h>
1298275SEric Cheng #include <inet/ip6.h>
1308275SEric Cheng #include <sys/exacct.h>
1318275SEric Cheng #include <sys/exacct_impl.h>
1328275SEric Cheng #include <inet/nd.h>
1338275SEric Cheng #include <sys/ethernet.h>
1348275SEric Cheng 
1358275SEric Cheng kmem_cache_t	*mac_client_impl_cache;
1368275SEric Cheng kmem_cache_t	*mac_promisc_impl_cache;
1378275SEric Cheng 
1388275SEric Cheng static boolean_t mac_client_single_rcvr(mac_client_impl_t *);
1398275SEric Cheng static flow_entry_t *mac_client_swap_mciflent(mac_client_impl_t *);
1408275SEric Cheng static flow_entry_t *mac_client_get_flow(mac_client_impl_t *,
1418275SEric Cheng     mac_unicast_impl_t *);
1428275SEric Cheng static void mac_client_remove_flow_from_list(mac_client_impl_t *,
1438275SEric Cheng     flow_entry_t *);
1448275SEric Cheng static void mac_client_add_to_flow_list(mac_client_impl_t *, flow_entry_t *);
1458275SEric Cheng static void mac_rename_flow_names(mac_client_impl_t *, const char *);
1468275SEric Cheng static void mac_virtual_link_update(mac_impl_t *);
14711878SVenu.Iyer@Sun.COM static int mac_client_datapath_setup(mac_client_impl_t *, uint16_t,
14811878SVenu.Iyer@Sun.COM     uint8_t *, mac_resource_props_t *, boolean_t, mac_unicast_impl_t *);
14911878SVenu.Iyer@Sun.COM static void mac_client_datapath_teardown(mac_client_handle_t,
15011878SVenu.Iyer@Sun.COM     mac_unicast_impl_t *, flow_entry_t *);
1518275SEric Cheng 
1528275SEric Cheng /* ARGSUSED */
1538275SEric Cheng static int
1548275SEric Cheng i_mac_client_impl_ctor(void *buf, void *arg, int kmflag)
1558275SEric Cheng {
1568275SEric Cheng 	int	i;
1578275SEric Cheng 	mac_client_impl_t	*mcip = buf;
1588275SEric Cheng 
1598275SEric Cheng 	bzero(buf, MAC_CLIENT_IMPL_SIZE);
1608275SEric Cheng 	mutex_init(&mcip->mci_tx_cb_lock, NULL, MUTEX_DRIVER, NULL);
1618275SEric Cheng 	mcip->mci_tx_notify_cb_info.mcbi_lockp = &mcip->mci_tx_cb_lock;
1628275SEric Cheng 
1638275SEric Cheng 	ASSERT(mac_tx_percpu_cnt >= 0);
1648275SEric Cheng 	for (i = 0; i <= mac_tx_percpu_cnt; i++) {
1658275SEric Cheng 		mutex_init(&mcip->mci_tx_pcpu[i].pcpu_tx_lock, NULL,
1668275SEric Cheng 		    MUTEX_DRIVER, NULL);
1678275SEric Cheng 	}
1688275SEric Cheng 	cv_init(&mcip->mci_tx_cv, NULL, CV_DRIVER, NULL);
1698275SEric Cheng 
1708275SEric Cheng 	return (0);
1718275SEric Cheng }
1728275SEric Cheng 
1738275SEric Cheng /* ARGSUSED */
1748275SEric Cheng static void
1758275SEric Cheng i_mac_client_impl_dtor(void *buf, void *arg)
1768275SEric Cheng {
1778275SEric Cheng 	int	i;
1788275SEric Cheng 	mac_client_impl_t *mcip = buf;
1798275SEric Cheng 
1808275SEric Cheng 	ASSERT(mcip->mci_promisc_list == NULL);
1818275SEric Cheng 	ASSERT(mcip->mci_unicast_list == NULL);
1828275SEric Cheng 	ASSERT(mcip->mci_state_flags == 0);
1838275SEric Cheng 	ASSERT(mcip->mci_tx_flag == 0);
1848275SEric Cheng 
1858275SEric Cheng 	mutex_destroy(&mcip->mci_tx_cb_lock);
1868275SEric Cheng 
1878275SEric Cheng 	ASSERT(mac_tx_percpu_cnt >= 0);
1888275SEric Cheng 	for (i = 0; i <= mac_tx_percpu_cnt; i++) {
1898275SEric Cheng 		ASSERT(mcip->mci_tx_pcpu[i].pcpu_tx_refcnt == 0);
1908275SEric Cheng 		mutex_destroy(&mcip->mci_tx_pcpu[i].pcpu_tx_lock);
1918275SEric Cheng 	}
1928275SEric Cheng 	cv_destroy(&mcip->mci_tx_cv);
1938275SEric Cheng }
1948275SEric Cheng 
1958275SEric Cheng /* ARGSUSED */
1968275SEric Cheng static int
1978275SEric Cheng i_mac_promisc_impl_ctor(void *buf, void *arg, int kmflag)
1988275SEric Cheng {
1998275SEric Cheng 	mac_promisc_impl_t	*mpip = buf;
2008275SEric Cheng 
2018275SEric Cheng 	bzero(buf, sizeof (mac_promisc_impl_t));
2028275SEric Cheng 	mpip->mpi_mci_link.mcb_objp = buf;
2038275SEric Cheng 	mpip->mpi_mci_link.mcb_objsize = sizeof (mac_promisc_impl_t);
2048275SEric Cheng 	mpip->mpi_mi_link.mcb_objp = buf;
2058275SEric Cheng 	mpip->mpi_mi_link.mcb_objsize = sizeof (mac_promisc_impl_t);
2068275SEric Cheng 	return (0);
2078275SEric Cheng }
2088275SEric Cheng 
2098275SEric Cheng /* ARGSUSED */
2108275SEric Cheng static void
2118275SEric Cheng i_mac_promisc_impl_dtor(void *buf, void *arg)
2128275SEric Cheng {
2138275SEric Cheng 	mac_promisc_impl_t	*mpip = buf;
2148275SEric Cheng 
2158275SEric Cheng 	ASSERT(mpip->mpi_mci_link.mcb_objp != NULL);
2168275SEric Cheng 	ASSERT(mpip->mpi_mci_link.mcb_objsize == sizeof (mac_promisc_impl_t));
2178275SEric Cheng 	ASSERT(mpip->mpi_mi_link.mcb_objp == mpip->mpi_mci_link.mcb_objp);
2188275SEric Cheng 	ASSERT(mpip->mpi_mi_link.mcb_objsize == sizeof (mac_promisc_impl_t));
2198275SEric Cheng 
2208275SEric Cheng 	mpip->mpi_mci_link.mcb_objp = NULL;
2218275SEric Cheng 	mpip->mpi_mci_link.mcb_objsize = 0;
2228275SEric Cheng 	mpip->mpi_mi_link.mcb_objp = NULL;
2238275SEric Cheng 	mpip->mpi_mi_link.mcb_objsize = 0;
2248275SEric Cheng 
2258275SEric Cheng 	ASSERT(mpip->mpi_mci_link.mcb_flags == 0);
2268275SEric Cheng 	mpip->mpi_mci_link.mcb_objsize = 0;
2278275SEric Cheng }
2288275SEric Cheng 
2298275SEric Cheng void
2308275SEric Cheng mac_client_init(void)
2318275SEric Cheng {
2328275SEric Cheng 	ASSERT(mac_tx_percpu_cnt >= 0);
2338275SEric Cheng 
2348275SEric Cheng 	mac_client_impl_cache = kmem_cache_create("mac_client_impl_cache",
2358275SEric Cheng 	    MAC_CLIENT_IMPL_SIZE, 0, i_mac_client_impl_ctor,
2368275SEric Cheng 	    i_mac_client_impl_dtor, NULL, NULL, NULL, 0);
2378275SEric Cheng 	ASSERT(mac_client_impl_cache != NULL);
2388275SEric Cheng 
2398275SEric Cheng 	mac_promisc_impl_cache = kmem_cache_create("mac_promisc_impl_cache",
2408275SEric Cheng 	    sizeof (mac_promisc_impl_t), 0, i_mac_promisc_impl_ctor,
2418275SEric Cheng 	    i_mac_promisc_impl_dtor, NULL, NULL, NULL, 0);
2428275SEric Cheng 	ASSERT(mac_promisc_impl_cache != NULL);
2438275SEric Cheng }
2448275SEric Cheng 
2458275SEric Cheng void
2468275SEric Cheng mac_client_fini(void)
2478275SEric Cheng {
2488275SEric Cheng 	kmem_cache_destroy(mac_client_impl_cache);
2498275SEric Cheng 	kmem_cache_destroy(mac_promisc_impl_cache);
2508275SEric Cheng }
2518275SEric Cheng 
2528275SEric Cheng /*
2538275SEric Cheng  * Return the lower MAC client handle from the VNIC driver for the
2548275SEric Cheng  * specified VNIC MAC instance.
2558275SEric Cheng  */
2568275SEric Cheng mac_client_impl_t *
2578275SEric Cheng mac_vnic_lower(mac_impl_t *mip)
2588275SEric Cheng {
2598275SEric Cheng 	mac_capab_vnic_t cap;
2608275SEric Cheng 	mac_client_impl_t *mcip;
2618275SEric Cheng 
2628275SEric Cheng 	VERIFY(i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_VNIC, &cap));
2638275SEric Cheng 	mcip = cap.mcv_mac_client_handle(cap.mcv_arg);
2648275SEric Cheng 
2658275SEric Cheng 	return (mcip);
2668275SEric Cheng }
2678275SEric Cheng 
2688275SEric Cheng /*
2698275SEric Cheng  * Return the MAC client handle of the primary MAC client for the
2708275SEric Cheng  * specified MAC instance, or NULL otherwise.
2718275SEric Cheng  */
2728275SEric Cheng mac_client_impl_t *
2738275SEric Cheng mac_primary_client_handle(mac_impl_t *mip)
2748275SEric Cheng {
2758275SEric Cheng 	mac_client_impl_t *mcip;
2768275SEric Cheng 
2778275SEric Cheng 	if (mip->mi_state_flags & MIS_IS_VNIC)
2788275SEric Cheng 		return (mac_vnic_lower(mip));
2798275SEric Cheng 
2808275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
2818275SEric Cheng 
2828275SEric Cheng 	for (mcip = mip->mi_clients_list; mcip != NULL;
2838275SEric Cheng 	    mcip = mcip->mci_client_next) {
2848275SEric Cheng 		if (MCIP_DATAPATH_SETUP(mcip) && mac_is_primary_client(mcip))
2858275SEric Cheng 			return (mcip);
2868275SEric Cheng 	}
2878275SEric Cheng 	return (NULL);
2888275SEric Cheng }
2898275SEric Cheng 
2908275SEric Cheng /*
2918275SEric Cheng  * Open a MAC specified by its MAC name.
2928275SEric Cheng  */
2938275SEric Cheng int
2948275SEric Cheng mac_open(const char *macname, mac_handle_t *mhp)
2958275SEric Cheng {
2968275SEric Cheng 	mac_impl_t	*mip;
2978275SEric Cheng 	int		err;
2988275SEric Cheng 
2998275SEric Cheng 	/*
3008275SEric Cheng 	 * Look up its entry in the global hash table.
3018275SEric Cheng 	 */
3028275SEric Cheng 	if ((err = mac_hold(macname, &mip)) != 0)
3038275SEric Cheng 		return (err);
3048275SEric Cheng 
3058275SEric Cheng 	/*
3068275SEric Cheng 	 * Hold the dip associated to the MAC to prevent it from being
3078275SEric Cheng 	 * detached. For a softmac, its underlying dip is held by the
3088275SEric Cheng 	 * mi_open() callback.
3098275SEric Cheng 	 *
3108275SEric Cheng 	 * This is done to be more tolerant with some defective drivers,
3118275SEric Cheng 	 * which incorrectly handle mac_unregister() failure in their
3128275SEric Cheng 	 * xxx_detach() routine. For example, some drivers ignore the
3138275SEric Cheng 	 * failure of mac_unregister() and free all resources that
3148275SEric Cheng 	 * that are needed for data transmition.
3158275SEric Cheng 	 */
3168275SEric Cheng 	e_ddi_hold_devi(mip->mi_dip);
3178275SEric Cheng 
3188275SEric Cheng 	if (!(mip->mi_callbacks->mc_callbacks & MC_OPEN)) {
3198275SEric Cheng 		*mhp = (mac_handle_t)mip;
3208275SEric Cheng 		return (0);
3218275SEric Cheng 	}
3228275SEric Cheng 
3238275SEric Cheng 	/*
3248275SEric Cheng 	 * The mac perimeter is used in both mac_open and mac_close by the
3258275SEric Cheng 	 * framework to single thread the MC_OPEN/MC_CLOSE of drivers.
3268275SEric Cheng 	 */
3278275SEric Cheng 	i_mac_perim_enter(mip);
3288275SEric Cheng 	mip->mi_oref++;
3298275SEric Cheng 	if (mip->mi_oref != 1 || ((err = mip->mi_open(mip->mi_driver)) == 0)) {
3308275SEric Cheng 		*mhp = (mac_handle_t)mip;
3318275SEric Cheng 		i_mac_perim_exit(mip);
3328275SEric Cheng 		return (0);
3338275SEric Cheng 	}
3348275SEric Cheng 	mip->mi_oref--;
3358275SEric Cheng 	ddi_release_devi(mip->mi_dip);
3368275SEric Cheng 	mac_rele(mip);
3378275SEric Cheng 	i_mac_perim_exit(mip);
3388275SEric Cheng 	return (err);
3398275SEric Cheng }
3408275SEric Cheng 
3418275SEric Cheng /*
3428275SEric Cheng  * Open a MAC specified by its linkid.
3438275SEric Cheng  */
3448275SEric Cheng int
3458275SEric Cheng mac_open_by_linkid(datalink_id_t linkid, mac_handle_t *mhp)
3468275SEric Cheng {
3478275SEric Cheng 	dls_dl_handle_t	dlh;
3488275SEric Cheng 	int		err;
3498275SEric Cheng 
3508275SEric Cheng 	if ((err = dls_devnet_hold_tmp(linkid, &dlh)) != 0)
3518275SEric Cheng 		return (err);
3528275SEric Cheng 
3538275SEric Cheng 	dls_devnet_prop_task_wait(dlh);
3548275SEric Cheng 
3558275SEric Cheng 	err = mac_open(dls_devnet_mac(dlh), mhp);
3568275SEric Cheng 
3578275SEric Cheng 	dls_devnet_rele_tmp(dlh);
3588275SEric Cheng 	return (err);
3598275SEric Cheng }
3608275SEric Cheng 
3618275SEric Cheng /*
3628275SEric Cheng  * Open a MAC specified by its link name.
3638275SEric Cheng  */
3648275SEric Cheng int
3658275SEric Cheng mac_open_by_linkname(const char *link, mac_handle_t *mhp)
3668275SEric Cheng {
3678275SEric Cheng 	datalink_id_t	linkid;
3688275SEric Cheng 	int		err;
3698275SEric Cheng 
3708275SEric Cheng 	if ((err = dls_mgmt_get_linkid(link, &linkid)) != 0)
3718275SEric Cheng 		return (err);
3728275SEric Cheng 	return (mac_open_by_linkid(linkid, mhp));
3738275SEric Cheng }
3748275SEric Cheng 
3758275SEric Cheng /*
3768275SEric Cheng  * Close the specified MAC.
3778275SEric Cheng  */
3788275SEric Cheng void
3798275SEric Cheng mac_close(mac_handle_t mh)
3808275SEric Cheng {
3818275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
3828275SEric Cheng 
3838275SEric Cheng 	i_mac_perim_enter(mip);
3848275SEric Cheng 	/*
3858275SEric Cheng 	 * The mac perimeter is used in both mac_open and mac_close by the
3868275SEric Cheng 	 * framework to single thread the MC_OPEN/MC_CLOSE of drivers.
3878275SEric Cheng 	 */
3888275SEric Cheng 	if (mip->mi_callbacks->mc_callbacks & MC_OPEN) {
3898275SEric Cheng 		ASSERT(mip->mi_oref != 0);
3908275SEric Cheng 		if (--mip->mi_oref == 0) {
3918275SEric Cheng 			if ((mip->mi_callbacks->mc_callbacks & MC_CLOSE))
3928275SEric Cheng 				mip->mi_close(mip->mi_driver);
3938275SEric Cheng 		}
3948275SEric Cheng 	}
3958275SEric Cheng 	i_mac_perim_exit(mip);
3968275SEric Cheng 	ddi_release_devi(mip->mi_dip);
3978275SEric Cheng 	mac_rele(mip);
3988275SEric Cheng }
3998275SEric Cheng 
4008275SEric Cheng /*
4018275SEric Cheng  * Misc utility functions to retrieve various information about a MAC
4028275SEric Cheng  * instance or a MAC client.
4038275SEric Cheng  */
4048275SEric Cheng 
4058275SEric Cheng const mac_info_t *
4068275SEric Cheng mac_info(mac_handle_t mh)
4078275SEric Cheng {
4088275SEric Cheng 	return (&((mac_impl_t *)mh)->mi_info);
4098275SEric Cheng }
4108275SEric Cheng 
4118275SEric Cheng dev_info_t *
4128275SEric Cheng mac_devinfo_get(mac_handle_t mh)
4138275SEric Cheng {
4148275SEric Cheng 	return (((mac_impl_t *)mh)->mi_dip);
4158275SEric Cheng }
4168275SEric Cheng 
4179073SCathy.Zhou@Sun.COM void *
4189073SCathy.Zhou@Sun.COM mac_driver(mac_handle_t mh)
4199073SCathy.Zhou@Sun.COM {
4209073SCathy.Zhou@Sun.COM 	return (((mac_impl_t *)mh)->mi_driver);
4219073SCathy.Zhou@Sun.COM }
4229073SCathy.Zhou@Sun.COM 
4238275SEric Cheng const char *
4248275SEric Cheng mac_name(mac_handle_t mh)
4258275SEric Cheng {
4268275SEric Cheng 	return (((mac_impl_t *)mh)->mi_name);
4278275SEric Cheng }
4288275SEric Cheng 
42910639SDarren.Reed@Sun.COM int
43010639SDarren.Reed@Sun.COM mac_type(mac_handle_t mh)
43110639SDarren.Reed@Sun.COM {
43210639SDarren.Reed@Sun.COM 	return (((mac_impl_t *)mh)->mi_type->mt_type);
43310639SDarren.Reed@Sun.COM }
43410639SDarren.Reed@Sun.COM 
43511665SDarren.Reed@Sun.COM int
43611665SDarren.Reed@Sun.COM mac_nativetype(mac_handle_t mh)
43711665SDarren.Reed@Sun.COM {
43811665SDarren.Reed@Sun.COM 	return (((mac_impl_t *)mh)->mi_type->mt_nativetype);
43911665SDarren.Reed@Sun.COM }
44011665SDarren.Reed@Sun.COM 
4418275SEric Cheng char *
4428275SEric Cheng mac_client_name(mac_client_handle_t mch)
4438275SEric Cheng {
4448275SEric Cheng 	return (((mac_client_impl_t *)mch)->mci_name);
4458275SEric Cheng }
4468275SEric Cheng 
4478275SEric Cheng minor_t
4488275SEric Cheng mac_minor(mac_handle_t mh)
4498275SEric Cheng {
4508275SEric Cheng 	return (((mac_impl_t *)mh)->mi_minor);
4518275SEric Cheng }
4528275SEric Cheng 
4538275SEric Cheng /*
4548275SEric Cheng  * Return the VID associated with a MAC client. This function should
4558275SEric Cheng  * be called for clients which are associated with only one VID.
4568275SEric Cheng  */
4578275SEric Cheng uint16_t
4588275SEric Cheng mac_client_vid(mac_client_handle_t mch)
4598275SEric Cheng {
4608275SEric Cheng 	uint16_t		vid = VLAN_ID_NONE;
4618275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
4628275SEric Cheng 	flow_desc_t		flow_desc;
4638275SEric Cheng 
4648275SEric Cheng 	if (mcip->mci_nflents == 0)
4658275SEric Cheng 		return (vid);
4668275SEric Cheng 
4678275SEric Cheng 	ASSERT(MCIP_DATAPATH_SETUP(mcip) && mac_client_single_rcvr(mcip));
4688275SEric Cheng 
4698275SEric Cheng 	mac_flow_get_desc(mcip->mci_flent, &flow_desc);
4708275SEric Cheng 	if ((flow_desc.fd_mask & FLOW_LINK_VID) != 0)
4718275SEric Cheng 		vid = flow_desc.fd_vid;
4728275SEric Cheng 
4738275SEric Cheng 	return (vid);
4748275SEric Cheng }
4758275SEric Cheng 
4768275SEric Cheng /*
4779726SNicolas.Droux@Sun.COM  * Return whether the specified MAC client corresponds to a VLAN VNIC.
4789726SNicolas.Droux@Sun.COM  */
4799726SNicolas.Droux@Sun.COM boolean_t
4809726SNicolas.Droux@Sun.COM mac_client_is_vlan_vnic(mac_client_handle_t mch)
4819726SNicolas.Droux@Sun.COM {
4829726SNicolas.Droux@Sun.COM 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
4839726SNicolas.Droux@Sun.COM 
4849726SNicolas.Droux@Sun.COM 	return (((mcip->mci_state_flags & MCIS_IS_VNIC) != 0) &&
4859726SNicolas.Droux@Sun.COM 	    ((mcip->mci_flent->fe_type & FLOW_PRIMARY_MAC) != 0));
4869726SNicolas.Droux@Sun.COM }
4879726SNicolas.Droux@Sun.COM 
4889726SNicolas.Droux@Sun.COM /*
4898275SEric Cheng  * Return the link speed associated with the specified MAC client.
4908275SEric Cheng  *
4918275SEric Cheng  * The link speed of a MAC client is equal to the smallest value of
4928275SEric Cheng  * 1) the current link speed of the underlying NIC, or
4938275SEric Cheng  * 2) the bandwidth limit set for the MAC client.
4948275SEric Cheng  *
4958275SEric Cheng  * Note that the bandwidth limit can be higher than the speed
4968275SEric Cheng  * of the underlying NIC. This is allowed to avoid spurious
4978275SEric Cheng  * administration action failures or artifically lowering the
4988275SEric Cheng  * bandwidth limit of a link that may  have temporarily lowered
4998275SEric Cheng  * its link speed due to hardware problem or administrator action.
5008275SEric Cheng  */
5018275SEric Cheng static uint64_t
5028275SEric Cheng mac_client_ifspeed(mac_client_impl_t *mcip)
5038275SEric Cheng {
5048275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
5058275SEric Cheng 	uint64_t nic_speed;
5068275SEric Cheng 
5078275SEric Cheng 	nic_speed = mac_stat_get((mac_handle_t)mip, MAC_STAT_IFSPEED);
5088275SEric Cheng 
5098275SEric Cheng 	if (nic_speed == 0) {
5108275SEric Cheng 		return (0);
5118275SEric Cheng 	} else {
5128275SEric Cheng 		uint64_t policy_limit = (uint64_t)-1;
5138275SEric Cheng 
5148275SEric Cheng 		if (MCIP_RESOURCE_PROPS_MASK(mcip) & MRP_MAXBW)
5158275SEric Cheng 			policy_limit = MCIP_RESOURCE_PROPS_MAXBW(mcip);
5168275SEric Cheng 
5178275SEric Cheng 		return (MIN(policy_limit, nic_speed));
5188275SEric Cheng 	}
5198275SEric Cheng }
5208275SEric Cheng 
5218275SEric Cheng /*
5228275SEric Cheng  * Return the link state of the specified client. If here are more
5238275SEric Cheng  * than one clients of the underying mac_impl_t, the link state
5248275SEric Cheng  * will always be UP regardless of the link state of the underlying
5258275SEric Cheng  * mac_impl_t. This is needed to allow the MAC clients to continue
5268275SEric Cheng  * to communicate with each other even when the physical link of
5278275SEric Cheng  * their mac_impl_t is down.
5288275SEric Cheng  */
5298275SEric Cheng static uint64_t
5308275SEric Cheng mac_client_link_state(mac_client_impl_t *mcip)
5318275SEric Cheng {
5328275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
5338275SEric Cheng 	uint16_t vid;
5348275SEric Cheng 	mac_client_impl_t *mci_list;
5358275SEric Cheng 	mac_unicast_impl_t *mui_list, *oth_mui_list;
5368275SEric Cheng 
5378275SEric Cheng 	/*
5388275SEric Cheng 	 * Returns LINK_STATE_UP if there are other MAC clients defined on
5398275SEric Cheng 	 * mac_impl_t which share same VLAN ID as that of mcip. Note that
5408275SEric Cheng 	 * if 'mcip' has more than one VID's then we match ANY one of the
5418275SEric Cheng 	 * VID's with other MAC client's VID's and return LINK_STATE_UP.
5428275SEric Cheng 	 */
5438275SEric Cheng 	rw_enter(&mcip->mci_rw_lock, RW_READER);
5448275SEric Cheng 	for (mui_list = mcip->mci_unicast_list; mui_list != NULL;
5458275SEric Cheng 	    mui_list = mui_list->mui_next) {
5468275SEric Cheng 		vid = mui_list->mui_vid;
5478275SEric Cheng 		for (mci_list = mip->mi_clients_list; mci_list != NULL;
5488275SEric Cheng 		    mci_list = mci_list->mci_client_next) {
5498275SEric Cheng 			if (mci_list == mcip)
5508275SEric Cheng 				continue;
5518275SEric Cheng 			for (oth_mui_list = mci_list->mci_unicast_list;
5528275SEric Cheng 			    oth_mui_list != NULL; oth_mui_list = oth_mui_list->
5538275SEric Cheng 			    mui_next) {
5548275SEric Cheng 				if (vid == oth_mui_list->mui_vid) {
5558275SEric Cheng 					rw_exit(&mcip->mci_rw_lock);
5568275SEric Cheng 					return (LINK_STATE_UP);
5578275SEric Cheng 				}
5588275SEric Cheng 			}
5598275SEric Cheng 		}
5608275SEric Cheng 	}
5618275SEric Cheng 	rw_exit(&mcip->mci_rw_lock);
5628275SEric Cheng 
5638275SEric Cheng 	return (mac_stat_get((mac_handle_t)mip, MAC_STAT_LINK_STATE));
5648275SEric Cheng }
5658275SEric Cheng 
5668275SEric Cheng /*
56711878SVenu.Iyer@Sun.COM  * These statistics are consumed by dladm show-link -s <vnic>,
56811878SVenu.Iyer@Sun.COM  * dladm show-vnic -s and netstat. With the introduction of dlstat,
56911878SVenu.Iyer@Sun.COM  * dladm show-link -s and dladm show-vnic -s witll be EOL'ed while
57011878SVenu.Iyer@Sun.COM  * netstat will consume from kstats introduced for dlstat. This code
57111878SVenu.Iyer@Sun.COM  * will be removed at that time.
57211878SVenu.Iyer@Sun.COM  */
57311878SVenu.Iyer@Sun.COM 
57411878SVenu.Iyer@Sun.COM /*
5758275SEric Cheng  * Return the statistics of a MAC client. These statistics are different
5768275SEric Cheng  * then the statistics of the underlying MAC which are returned by
5778275SEric Cheng  * mac_stat_get().
5788275SEric Cheng  */
5798275SEric Cheng uint64_t
5808275SEric Cheng mac_client_stat_get(mac_client_handle_t mch, uint_t stat)
5818275SEric Cheng {
58211878SVenu.Iyer@Sun.COM 	mac_client_impl_t 	*mcip = (mac_client_impl_t *)mch;
58311878SVenu.Iyer@Sun.COM 	mac_impl_t 		*mip = mcip->mci_mip;
58411878SVenu.Iyer@Sun.COM 	flow_entry_t 		*flent = mcip->mci_flent;
58511878SVenu.Iyer@Sun.COM 	mac_soft_ring_set_t 	*mac_srs;
58611878SVenu.Iyer@Sun.COM 	mac_rx_stats_t		*mac_rx_stat;
58711878SVenu.Iyer@Sun.COM 	mac_tx_stats_t		*mac_tx_stat;
58811878SVenu.Iyer@Sun.COM 	int i;
58911878SVenu.Iyer@Sun.COM 	uint64_t val = 0;
59011878SVenu.Iyer@Sun.COM 
59111878SVenu.Iyer@Sun.COM 	mac_srs = (mac_soft_ring_set_t *)(flent->fe_tx_srs);
59211878SVenu.Iyer@Sun.COM 	mac_tx_stat = &mac_srs->srs_tx.st_stat;
5938275SEric Cheng 
5948275SEric Cheng 	switch (stat) {
5958275SEric Cheng 	case MAC_STAT_LINK_STATE:
5968275SEric Cheng 		val = mac_client_link_state(mcip);
5978275SEric Cheng 		break;
5988275SEric Cheng 	case MAC_STAT_LINK_UP:
5998275SEric Cheng 		val = (mac_client_link_state(mcip) == LINK_STATE_UP);
6008275SEric Cheng 		break;
6018275SEric Cheng 	case MAC_STAT_PROMISC:
6028275SEric Cheng 		val = mac_stat_get((mac_handle_t)mip, MAC_STAT_PROMISC);
6038275SEric Cheng 		break;
60410491SRishi.Srivatsavai@Sun.COM 	case MAC_STAT_LOWLINK_STATE:
60510491SRishi.Srivatsavai@Sun.COM 		val = mac_stat_get((mac_handle_t)mip, MAC_STAT_LOWLINK_STATE);
60610491SRishi.Srivatsavai@Sun.COM 		break;
6078275SEric Cheng 	case MAC_STAT_IFSPEED:
6088275SEric Cheng 		val = mac_client_ifspeed(mcip);
6098275SEric Cheng 		break;
6108275SEric Cheng 	case MAC_STAT_MULTIRCV:
61111878SVenu.Iyer@Sun.COM 		val = mcip->mci_misc_stat.mms_multircv;
6128275SEric Cheng 		break;
6138275SEric Cheng 	case MAC_STAT_BRDCSTRCV:
61411878SVenu.Iyer@Sun.COM 		val = mcip->mci_misc_stat.mms_brdcstrcv;
6158275SEric Cheng 		break;
6168275SEric Cheng 	case MAC_STAT_MULTIXMT:
61711878SVenu.Iyer@Sun.COM 		val = mcip->mci_misc_stat.mms_multixmt;
6188275SEric Cheng 		break;
6198275SEric Cheng 	case MAC_STAT_BRDCSTXMT:
62011878SVenu.Iyer@Sun.COM 		val = mcip->mci_misc_stat.mms_brdcstxmt;
6218275SEric Cheng 		break;
6228275SEric Cheng 	case MAC_STAT_OBYTES:
62311878SVenu.Iyer@Sun.COM 		val = mac_tx_stat->mts_obytes;
6248275SEric Cheng 		break;
6258275SEric Cheng 	case MAC_STAT_OPACKETS:
62611878SVenu.Iyer@Sun.COM 		val = mac_tx_stat->mts_opackets;
6278275SEric Cheng 		break;
6288275SEric Cheng 	case MAC_STAT_OERRORS:
62911878SVenu.Iyer@Sun.COM 		val = mac_tx_stat->mts_oerrors;
6308275SEric Cheng 		break;
6318275SEric Cheng 	case MAC_STAT_IPACKETS:
63211878SVenu.Iyer@Sun.COM 		for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
63311878SVenu.Iyer@Sun.COM 			mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
63411878SVenu.Iyer@Sun.COM 			mac_rx_stat = &mac_srs->srs_rx.sr_stat;
63511878SVenu.Iyer@Sun.COM 			val += mac_rx_stat->mrs_intrcnt +
63611878SVenu.Iyer@Sun.COM 			    mac_rx_stat->mrs_pollcnt + mac_rx_stat->mrs_lclcnt;
63711878SVenu.Iyer@Sun.COM 		}
6388275SEric Cheng 		break;
6398275SEric Cheng 	case MAC_STAT_RBYTES:
64011878SVenu.Iyer@Sun.COM 		for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
64111878SVenu.Iyer@Sun.COM 			mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
64211878SVenu.Iyer@Sun.COM 			mac_rx_stat = &mac_srs->srs_rx.sr_stat;
64311878SVenu.Iyer@Sun.COM 			val += mac_rx_stat->mrs_intrbytes +
64411878SVenu.Iyer@Sun.COM 			    mac_rx_stat->mrs_pollbytes +
64511878SVenu.Iyer@Sun.COM 			    mac_rx_stat->mrs_lclbytes;
64611878SVenu.Iyer@Sun.COM 		}
6478275SEric Cheng 		break;
6488275SEric Cheng 	case MAC_STAT_IERRORS:
64911878SVenu.Iyer@Sun.COM 		for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
65011878SVenu.Iyer@Sun.COM 			mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
65111878SVenu.Iyer@Sun.COM 			mac_rx_stat = &mac_srs->srs_rx.sr_stat;
65211878SVenu.Iyer@Sun.COM 			val += mac_rx_stat->mrs_ierrors;
65311878SVenu.Iyer@Sun.COM 		}
6548275SEric Cheng 		break;
6558275SEric Cheng 	default:
65611878SVenu.Iyer@Sun.COM 		val = mac_driver_stat_default(mip, stat);
6578275SEric Cheng 		break;
6588275SEric Cheng 	}
6598275SEric Cheng 
6608275SEric Cheng 	return (val);
6618275SEric Cheng }
6628275SEric Cheng 
6638275SEric Cheng /*
6648275SEric Cheng  * Return the statistics of the specified MAC instance.
6658275SEric Cheng  */
6668275SEric Cheng uint64_t
6678275SEric Cheng mac_stat_get(mac_handle_t mh, uint_t stat)
6688275SEric Cheng {
6698275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
6708275SEric Cheng 	uint64_t	val;
6718275SEric Cheng 	int		ret;
6728275SEric Cheng 
6738275SEric Cheng 	/*
6748275SEric Cheng 	 * The range of stat determines where it is maintained.  Stat
6758275SEric Cheng 	 * values from 0 up to (but not including) MAC_STAT_MIN are
6768275SEric Cheng 	 * mainteined by the mac module itself.  Everything else is
6778275SEric Cheng 	 * maintained by the driver.
6788275SEric Cheng 	 *
6798275SEric Cheng 	 * If the mac_impl_t being queried corresponds to a VNIC,
6808275SEric Cheng 	 * the stats need to be queried from the lower MAC client
6818275SEric Cheng 	 * corresponding to the VNIC. (The mac_link_update()
6828275SEric Cheng 	 * invoked by the driver to the lower MAC causes the *lower
6838275SEric Cheng 	 * MAC* to update its mi_linkstate, and send a notification
6848275SEric Cheng 	 * to its MAC clients. Due to the VNIC passthrough,
6858275SEric Cheng 	 * these notifications are sent to the upper MAC clients
6868275SEric Cheng 	 * of the VNIC directly, and the upper mac_impl_t of the VNIC
6878275SEric Cheng 	 * does not have a valid mi_linkstate.
6888275SEric Cheng 	 */
6898275SEric Cheng 	if (stat < MAC_STAT_MIN && !(mip->mi_state_flags & MIS_IS_VNIC)) {
6908275SEric Cheng 		/* these stats are maintained by the mac module itself */
6918275SEric Cheng 		switch (stat) {
6928275SEric Cheng 		case MAC_STAT_LINK_STATE:
6938275SEric Cheng 			return (mip->mi_linkstate);
6948275SEric Cheng 		case MAC_STAT_LINK_UP:
6958275SEric Cheng 			return (mip->mi_linkstate == LINK_STATE_UP);
6968275SEric Cheng 		case MAC_STAT_PROMISC:
6978275SEric Cheng 			return (mip->mi_devpromisc != 0);
69810491SRishi.Srivatsavai@Sun.COM 		case MAC_STAT_LOWLINK_STATE:
69910491SRishi.Srivatsavai@Sun.COM 			return (mip->mi_lowlinkstate);
7008275SEric Cheng 		default:
7018275SEric Cheng 			ASSERT(B_FALSE);
7028275SEric Cheng 		}
7038275SEric Cheng 	}
7048275SEric Cheng 
7058275SEric Cheng 	/*
7068275SEric Cheng 	 * Call the driver to get the given statistic.
7078275SEric Cheng 	 */
7088275SEric Cheng 	ret = mip->mi_getstat(mip->mi_driver, stat, &val);
7098275SEric Cheng 	if (ret != 0) {
7108275SEric Cheng 		/*
7118275SEric Cheng 		 * The driver doesn't support this statistic.  Get the
7128275SEric Cheng 		 * statistic's default value.
7138275SEric Cheng 		 */
71411878SVenu.Iyer@Sun.COM 		val = mac_driver_stat_default(mip, stat);
7158275SEric Cheng 	}
7168275SEric Cheng 	return (val);
7178275SEric Cheng }
7188275SEric Cheng 
7198275SEric Cheng /*
72011878SVenu.Iyer@Sun.COM  * Query hardware rx ring corresponding to the pseudo ring.
72111878SVenu.Iyer@Sun.COM  */
72211878SVenu.Iyer@Sun.COM uint64_t
72311878SVenu.Iyer@Sun.COM mac_pseudo_rx_ring_stat_get(mac_ring_handle_t handle, uint_t stat)
72411878SVenu.Iyer@Sun.COM {
72511878SVenu.Iyer@Sun.COM 	return (mac_rx_ring_stat_get(handle, stat));
72611878SVenu.Iyer@Sun.COM }
72711878SVenu.Iyer@Sun.COM 
72811878SVenu.Iyer@Sun.COM /*
72911878SVenu.Iyer@Sun.COM  * Query hardware tx ring corresponding to the pseudo ring.
73011878SVenu.Iyer@Sun.COM  */
73111878SVenu.Iyer@Sun.COM uint64_t
73211878SVenu.Iyer@Sun.COM mac_pseudo_tx_ring_stat_get(mac_ring_handle_t handle, uint_t stat)
73311878SVenu.Iyer@Sun.COM {
73411878SVenu.Iyer@Sun.COM 	return (mac_tx_ring_stat_get(handle, stat));
73511878SVenu.Iyer@Sun.COM }
73611878SVenu.Iyer@Sun.COM 
73711878SVenu.Iyer@Sun.COM /*
7388275SEric Cheng  * Utility function which returns the VID associated with a flow entry.
7398275SEric Cheng  */
7408275SEric Cheng uint16_t
7418275SEric Cheng i_mac_flow_vid(flow_entry_t *flent)
7428275SEric Cheng {
7438275SEric Cheng 	flow_desc_t	flow_desc;
7448275SEric Cheng 
7458275SEric Cheng 	mac_flow_get_desc(flent, &flow_desc);
7468275SEric Cheng 
7478275SEric Cheng 	if ((flow_desc.fd_mask & FLOW_LINK_VID) != 0)
7488275SEric Cheng 		return (flow_desc.fd_vid);
7498275SEric Cheng 	return (VLAN_ID_NONE);
7508275SEric Cheng }
7518275SEric Cheng 
7528275SEric Cheng /*
7538275SEric Cheng  * Verify the validity of the specified unicast MAC address. Returns B_TRUE
7548275SEric Cheng  * if the address is valid, B_FALSE otherwise (multicast address, or incorrect
7558275SEric Cheng  * length.
7568275SEric Cheng  */
7578275SEric Cheng boolean_t
7588275SEric Cheng mac_unicst_verify(mac_handle_t mh, const uint8_t *addr, uint_t len)
7598275SEric Cheng {
7608275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
7618275SEric Cheng 
7628275SEric Cheng 	/*
7638275SEric Cheng 	 * Verify the address. No lock is needed since mi_type and plugin
7648275SEric Cheng 	 * details don't change after mac_register().
7658275SEric Cheng 	 */
7668275SEric Cheng 	if ((len != mip->mi_type->mt_addr_length) ||
7678275SEric Cheng 	    (mip->mi_type->mt_ops.mtops_unicst_verify(addr,
7688275SEric Cheng 	    mip->mi_pdata)) != 0) {
7698275SEric Cheng 		return (B_FALSE);
7708275SEric Cheng 	} else {
7718275SEric Cheng 		return (B_TRUE);
7728275SEric Cheng 	}
7738275SEric Cheng }
7748275SEric Cheng 
7758275SEric Cheng void
7768275SEric Cheng mac_sdu_get(mac_handle_t mh, uint_t *min_sdu, uint_t *max_sdu)
7778275SEric Cheng {
7788275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
7798275SEric Cheng 
7808275SEric Cheng 	if (min_sdu != NULL)
7818275SEric Cheng 		*min_sdu = mip->mi_sdu_min;
7828275SEric Cheng 	if (max_sdu != NULL)
7838275SEric Cheng 		*max_sdu = mip->mi_sdu_max;
7848275SEric Cheng }
7858275SEric Cheng 
7868275SEric Cheng /*
7878275SEric Cheng  * Update the MAC unicast address of the specified client's flows. Currently
7888275SEric Cheng  * only one unicast MAC unicast address is allowed per client.
7898275SEric Cheng  */
7908275SEric Cheng static void
7918275SEric Cheng mac_unicast_update_client_flow(mac_client_impl_t *mcip)
7928275SEric Cheng {
7938275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
7948275SEric Cheng 	flow_entry_t *flent = mcip->mci_flent;
7958275SEric Cheng 	mac_address_t *map = mcip->mci_unicast;
7968275SEric Cheng 	flow_desc_t flow_desc;
7978275SEric Cheng 
7988275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
7998275SEric Cheng 	ASSERT(flent != NULL);
8008275SEric Cheng 
8018275SEric Cheng 	mac_flow_get_desc(flent, &flow_desc);
8028275SEric Cheng 	ASSERT(flow_desc.fd_mask & FLOW_LINK_DST);
8038275SEric Cheng 
8048275SEric Cheng 	bcopy(map->ma_addr, flow_desc.fd_dst_mac, map->ma_len);
8058275SEric Cheng 	mac_flow_set_desc(flent, &flow_desc);
8068275SEric Cheng 
8078275SEric Cheng 	/*
80811878SVenu.Iyer@Sun.COM 	 * The v6 local addr (used by mac protection) needs to be
80911878SVenu.Iyer@Sun.COM 	 * regenerated because our mac address has changed.
81011878SVenu.Iyer@Sun.COM 	 */
81111878SVenu.Iyer@Sun.COM 	mac_protect_update_v6_local_addr(mcip);
81211878SVenu.Iyer@Sun.COM 
81311878SVenu.Iyer@Sun.COM 	/*
8148275SEric Cheng 	 * A MAC client could have one MAC address but multiple
8158275SEric Cheng 	 * VLANs. In that case update the flow entries corresponding
8168275SEric Cheng 	 * to all VLANs of the MAC client.
8178275SEric Cheng 	 */
8188275SEric Cheng 	for (flent = mcip->mci_flent_list; flent != NULL;
8198275SEric Cheng 	    flent = flent->fe_client_next) {
8208275SEric Cheng 		mac_flow_get_desc(flent, &flow_desc);
8218275SEric Cheng 		if (!(flent->fe_type & FLOW_PRIMARY_MAC ||
8228275SEric Cheng 		    flent->fe_type & FLOW_VNIC_MAC))
8238275SEric Cheng 			continue;
8248275SEric Cheng 
8258275SEric Cheng 		bcopy(map->ma_addr, flow_desc.fd_dst_mac, map->ma_len);
8268275SEric Cheng 		mac_flow_set_desc(flent, &flow_desc);
8278275SEric Cheng 	}
8288275SEric Cheng }
8298275SEric Cheng 
8308275SEric Cheng /*
8318275SEric Cheng  * Update all clients that share the same unicast address.
8328275SEric Cheng  */
8338275SEric Cheng void
8348275SEric Cheng mac_unicast_update_clients(mac_impl_t *mip, mac_address_t *map)
8358275SEric Cheng {
8368275SEric Cheng 	mac_client_impl_t *mcip;
8378275SEric Cheng 
8388275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
8398275SEric Cheng 
8408275SEric Cheng 	/*
8418275SEric Cheng 	 * Find all clients that share the same unicast MAC address and update
8428275SEric Cheng 	 * them appropriately.
8438275SEric Cheng 	 */
8448275SEric Cheng 	for (mcip = mip->mi_clients_list; mcip != NULL;
8458275SEric Cheng 	    mcip = mcip->mci_client_next) {
8468275SEric Cheng 		/*
8478275SEric Cheng 		 * Ignore clients that don't share this MAC address.
8488275SEric Cheng 		 */
8498275SEric Cheng 		if (map != mcip->mci_unicast)
8508275SEric Cheng 			continue;
8518275SEric Cheng 
8528275SEric Cheng 		/*
8538275SEric Cheng 		 * Update those clients with same old unicast MAC address.
8548275SEric Cheng 		 */
8558275SEric Cheng 		mac_unicast_update_client_flow(mcip);
8568275SEric Cheng 	}
8578275SEric Cheng }
8588275SEric Cheng 
8598275SEric Cheng /*
8608275SEric Cheng  * Update the unicast MAC address of the specified VNIC MAC client.
8618275SEric Cheng  *
8628275SEric Cheng  * Check whether the operation is valid. Any of following cases should fail:
8638275SEric Cheng  *
8648275SEric Cheng  * 1. It's a VLAN type of VNIC.
8658275SEric Cheng  * 2. The new value is current "primary" MAC address.
8668275SEric Cheng  * 3. The current MAC address is shared with other clients.
8678275SEric Cheng  * 4. The new MAC address has been used. This case will be valid when
8688275SEric Cheng  *    client migration is fully supported.
8698275SEric Cheng  */
8708275SEric Cheng int
8718275SEric Cheng mac_vnic_unicast_set(mac_client_handle_t mch, const uint8_t *addr)
8728275SEric Cheng {
8738275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
8748275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
8758275SEric Cheng 	mac_address_t *map = mcip->mci_unicast;
8768275SEric Cheng 	int err;
8778275SEric Cheng 
8788275SEric Cheng 	ASSERT(!(mip->mi_state_flags & MIS_IS_VNIC));
8798275SEric Cheng 	ASSERT(mcip->mci_state_flags & MCIS_IS_VNIC);
8808275SEric Cheng 	ASSERT(mcip->mci_flags != MAC_CLIENT_FLAGS_PRIMARY);
8818275SEric Cheng 
8828275SEric Cheng 	i_mac_perim_enter(mip);
8838275SEric Cheng 
8848275SEric Cheng 	/*
8858275SEric Cheng 	 * If this is a VLAN type of VNIC, it's using "primary" MAC address
8868275SEric Cheng 	 * of the underlying interface. Must fail here. Refer to case 1 above.
8878275SEric Cheng 	 */
8888275SEric Cheng 	if (bcmp(map->ma_addr, mip->mi_addr, map->ma_len) == 0) {
8898275SEric Cheng 		i_mac_perim_exit(mip);
8908275SEric Cheng 		return (ENOTSUP);
8918275SEric Cheng 	}
8928275SEric Cheng 
8938275SEric Cheng 	/*
8948275SEric Cheng 	 * If the new address is the "primary" one, must fail. Refer to
8958275SEric Cheng 	 * case 2 above.
8968275SEric Cheng 	 */
8978275SEric Cheng 	if (bcmp(addr, mip->mi_addr, map->ma_len) == 0) {
8988275SEric Cheng 		i_mac_perim_exit(mip);
8998275SEric Cheng 		return (EACCES);
9008275SEric Cheng 	}
9018275SEric Cheng 
9028275SEric Cheng 	/*
9038275SEric Cheng 	 * If the address is shared by multiple clients, must fail. Refer
9048275SEric Cheng 	 * to case 3 above.
9058275SEric Cheng 	 */
9068275SEric Cheng 	if (mac_check_macaddr_shared(map)) {
9078275SEric Cheng 		i_mac_perim_exit(mip);
9088275SEric Cheng 		return (EBUSY);
9098275SEric Cheng 	}
9108275SEric Cheng 
9118275SEric Cheng 	/*
9128275SEric Cheng 	 * If the new address has been used, must fail for now. Refer to
9138275SEric Cheng 	 * case 4 above.
9148275SEric Cheng 	 */
9158275SEric Cheng 	if (mac_find_macaddr(mip, (uint8_t *)addr) != NULL) {
9168275SEric Cheng 		i_mac_perim_exit(mip);
9178275SEric Cheng 		return (ENOTSUP);
9188275SEric Cheng 	}
9198275SEric Cheng 
9208275SEric Cheng 	/*
9218275SEric Cheng 	 * Update the MAC address.
9228275SEric Cheng 	 */
9238275SEric Cheng 	err = mac_update_macaddr(map, (uint8_t *)addr);
9248275SEric Cheng 
9258275SEric Cheng 	if (err != 0) {
9268275SEric Cheng 		i_mac_perim_exit(mip);
9278275SEric Cheng 		return (err);
9288275SEric Cheng 	}
9298275SEric Cheng 
9308275SEric Cheng 	/*
9318275SEric Cheng 	 * Update all flows of this MAC client.
9328275SEric Cheng 	 */
9338275SEric Cheng 	mac_unicast_update_client_flow(mcip);
9348275SEric Cheng 
9358275SEric Cheng 	i_mac_perim_exit(mip);
9368275SEric Cheng 	return (0);
9378275SEric Cheng }
9388275SEric Cheng 
9398275SEric Cheng /*
9408275SEric Cheng  * Program the new primary unicast address of the specified MAC.
9418275SEric Cheng  *
9428275SEric Cheng  * Function mac_update_macaddr() takes care different types of underlying
9438275SEric Cheng  * MAC. If the underlying MAC is VNIC, the VNIC driver must have registerd
9448275SEric Cheng  * mi_unicst() entry point, that indirectly calls mac_vnic_unicast_set()
9458275SEric Cheng  * which will take care of updating the MAC address of the corresponding
9468275SEric Cheng  * MAC client.
9478275SEric Cheng  *
9488275SEric Cheng  * This is the only interface that allow the client to update the "primary"
9498275SEric Cheng  * MAC address of the underlying MAC. The new value must have not been
9508275SEric Cheng  * used by other clients.
9518275SEric Cheng  */
9528275SEric Cheng int
9538275SEric Cheng mac_unicast_primary_set(mac_handle_t mh, const uint8_t *addr)
9548275SEric Cheng {
9558275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
9568275SEric Cheng 	mac_address_t *map;
9578275SEric Cheng 	int err;
9588275SEric Cheng 
9598275SEric Cheng 	/* verify the address validity */
9608275SEric Cheng 	if (!mac_unicst_verify(mh, addr, mip->mi_type->mt_addr_length))
9618275SEric Cheng 		return (EINVAL);
9628275SEric Cheng 
9638275SEric Cheng 	i_mac_perim_enter(mip);
9648275SEric Cheng 
9658275SEric Cheng 	/*
9668275SEric Cheng 	 * If the new value is the same as the current primary address value,
9678275SEric Cheng 	 * there's nothing to do.
9688275SEric Cheng 	 */
9698275SEric Cheng 	if (bcmp(addr, mip->mi_addr, mip->mi_type->mt_addr_length) == 0) {
9708275SEric Cheng 		i_mac_perim_exit(mip);
9718275SEric Cheng 		return (0);
9728275SEric Cheng 	}
9738275SEric Cheng 
9748275SEric Cheng 	if (mac_find_macaddr(mip, (uint8_t *)addr) != 0) {
9758275SEric Cheng 		i_mac_perim_exit(mip);
9768275SEric Cheng 		return (EBUSY);
9778275SEric Cheng 	}
9788275SEric Cheng 
9798275SEric Cheng 	map = mac_find_macaddr(mip, mip->mi_addr);
9808275SEric Cheng 	ASSERT(map != NULL);
9818275SEric Cheng 
9828275SEric Cheng 	/*
9838275SEric Cheng 	 * Update the MAC address.
9848275SEric Cheng 	 */
9858275SEric Cheng 	if (mip->mi_state_flags & MIS_IS_AGGR) {
9868275SEric Cheng 		mac_capab_aggr_t aggr_cap;
9878275SEric Cheng 
9888275SEric Cheng 		/*
9898275SEric Cheng 		 * If the mac is an aggregation, other than the unicast
9908275SEric Cheng 		 * addresses programming, aggr must be informed about this
9918275SEric Cheng 		 * primary unicst address change to change its mac address
9928275SEric Cheng 		 * policy to be user-specified.
9938275SEric Cheng 		 */
9948275SEric Cheng 		ASSERT(map->ma_type == MAC_ADDRESS_TYPE_UNICAST_CLASSIFIED);
9958275SEric Cheng 		VERIFY(i_mac_capab_get(mh, MAC_CAPAB_AGGR, &aggr_cap));
9968275SEric Cheng 		err = aggr_cap.mca_unicst(mip->mi_driver, addr);
9978275SEric Cheng 		if (err == 0)
9988275SEric Cheng 			bcopy(addr, map->ma_addr, map->ma_len);
9998275SEric Cheng 	} else {
10008275SEric Cheng 		err = mac_update_macaddr(map, (uint8_t *)addr);
10018275SEric Cheng 	}
10028275SEric Cheng 
10038275SEric Cheng 	if (err != 0) {
10048275SEric Cheng 		i_mac_perim_exit(mip);
10058275SEric Cheng 		return (err);
10068275SEric Cheng 	}
10078275SEric Cheng 
10088275SEric Cheng 	mac_unicast_update_clients(mip, map);
10098275SEric Cheng 
10108275SEric Cheng 	/*
10118275SEric Cheng 	 * Save the new primary MAC address in mac_impl_t.
10128275SEric Cheng 	 */
10138275SEric Cheng 	bcopy(addr, mip->mi_addr, mip->mi_type->mt_addr_length);
10148275SEric Cheng 
10158275SEric Cheng 	i_mac_perim_exit(mip);
10168275SEric Cheng 
10178275SEric Cheng 	if (err == 0)
10188275SEric Cheng 		i_mac_notify(mip, MAC_NOTE_UNICST);
10198275SEric Cheng 
10208275SEric Cheng 	return (err);
10218275SEric Cheng }
10228275SEric Cheng 
10238275SEric Cheng /*
10248275SEric Cheng  * Return the current primary MAC address of the specified MAC.
10258275SEric Cheng  */
10268275SEric Cheng void
10278275SEric Cheng mac_unicast_primary_get(mac_handle_t mh, uint8_t *addr)
10288275SEric Cheng {
10298275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
10308275SEric Cheng 
10318275SEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_READER);
10328275SEric Cheng 	bcopy(mip->mi_addr, addr, mip->mi_type->mt_addr_length);
10338275SEric Cheng 	rw_exit(&mip->mi_rw_lock);
10348275SEric Cheng }
10358275SEric Cheng 
10368275SEric Cheng /*
10378275SEric Cheng  * Return information about the use of the primary MAC address of the
10388275SEric Cheng  * specified MAC instance:
10398275SEric Cheng  *
10408275SEric Cheng  * - if client_name is non-NULL, it must point to a string of at
10418275SEric Cheng  *   least MAXNAMELEN bytes, and will be set to the name of the MAC
10428275SEric Cheng  *   client which uses the primary MAC address.
10438275SEric Cheng  *
10448275SEric Cheng  * - if in_use is non-NULL, used to return whether the primary MAC
10458275SEric Cheng  *   address is currently in use.
10468275SEric Cheng  */
10478275SEric Cheng void
10488275SEric Cheng mac_unicast_primary_info(mac_handle_t mh, char *client_name, boolean_t *in_use)
10498275SEric Cheng {
10508275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
10518275SEric Cheng 	mac_client_impl_t *cur_client;
10528275SEric Cheng 
10538275SEric Cheng 	if (in_use != NULL)
10548275SEric Cheng 		*in_use = B_FALSE;
10558275SEric Cheng 	if (client_name != NULL)
10568275SEric Cheng 		bzero(client_name, MAXNAMELEN);
10578275SEric Cheng 
10588275SEric Cheng 	/*
10598275SEric Cheng 	 * The mi_rw_lock is used to protect threads that don't hold the
10608275SEric Cheng 	 * mac perimeter to get a consistent view of the mi_clients_list.
10618275SEric Cheng 	 * Threads that modify the list must hold both the mac perimeter and
10628275SEric Cheng 	 * mi_rw_lock(RW_WRITER)
10638275SEric Cheng 	 */
10648275SEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_READER);
10658275SEric Cheng 	for (cur_client = mip->mi_clients_list; cur_client != NULL;
10668275SEric Cheng 	    cur_client = cur_client->mci_client_next) {
10678275SEric Cheng 		if (mac_is_primary_client(cur_client) ||
10688275SEric Cheng 		    (mip->mi_state_flags & MIS_IS_VNIC)) {
10698275SEric Cheng 			rw_exit(&mip->mi_rw_lock);
10708275SEric Cheng 			if (in_use != NULL)
10718275SEric Cheng 				*in_use = B_TRUE;
10728275SEric Cheng 			if (client_name != NULL) {
10738275SEric Cheng 				bcopy(cur_client->mci_name, client_name,
10748275SEric Cheng 				    MAXNAMELEN);
10758275SEric Cheng 			}
10768275SEric Cheng 			return;
10778275SEric Cheng 		}
10788275SEric Cheng 	}
10798275SEric Cheng 	rw_exit(&mip->mi_rw_lock);
10808275SEric Cheng }
10818275SEric Cheng 
10828275SEric Cheng /*
108310616SSebastien.Roy@Sun.COM  * Return the current destination MAC address of the specified MAC.
108410616SSebastien.Roy@Sun.COM  */
108510616SSebastien.Roy@Sun.COM boolean_t
108610616SSebastien.Roy@Sun.COM mac_dst_get(mac_handle_t mh, uint8_t *addr)
108710616SSebastien.Roy@Sun.COM {
108810616SSebastien.Roy@Sun.COM 	mac_impl_t *mip = (mac_impl_t *)mh;
108910616SSebastien.Roy@Sun.COM 
109010616SSebastien.Roy@Sun.COM 	rw_enter(&mip->mi_rw_lock, RW_READER);
109110616SSebastien.Roy@Sun.COM 	if (mip->mi_dstaddr_set)
109210616SSebastien.Roy@Sun.COM 		bcopy(mip->mi_dstaddr, addr, mip->mi_type->mt_addr_length);
109310616SSebastien.Roy@Sun.COM 	rw_exit(&mip->mi_rw_lock);
109410616SSebastien.Roy@Sun.COM 	return (mip->mi_dstaddr_set);
109510616SSebastien.Roy@Sun.COM }
109610616SSebastien.Roy@Sun.COM 
109710616SSebastien.Roy@Sun.COM /*
10988275SEric Cheng  * Add the specified MAC client to the list of clients which opened
10998275SEric Cheng  * the specified MAC.
11008275SEric Cheng  */
11018275SEric Cheng static void
11028275SEric Cheng mac_client_add(mac_client_impl_t *mcip)
11038275SEric Cheng {
11048275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
11058275SEric Cheng 
11068275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
11078275SEric Cheng 
11088275SEric Cheng 	/* add VNIC to the front of the list */
11098275SEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
11108275SEric Cheng 	mcip->mci_client_next = mip->mi_clients_list;
11118275SEric Cheng 	mip->mi_clients_list = mcip;
11128275SEric Cheng 	mip->mi_nclients++;
11138275SEric Cheng 	rw_exit(&mip->mi_rw_lock);
11148275SEric Cheng }
11158275SEric Cheng 
11168275SEric Cheng /*
11178275SEric Cheng  * Remove the specified MAC client from the list of clients which opened
11188275SEric Cheng  * the specified MAC.
11198275SEric Cheng  */
11208275SEric Cheng static void
11218275SEric Cheng mac_client_remove(mac_client_impl_t *mcip)
11228275SEric Cheng {
11238275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
11248275SEric Cheng 	mac_client_impl_t **prev, *cclient;
11258275SEric Cheng 
11268275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
11278275SEric Cheng 
11288275SEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
11298275SEric Cheng 	prev = &mip->mi_clients_list;
11308275SEric Cheng 	cclient = *prev;
11318275SEric Cheng 	while (cclient != NULL && cclient != mcip) {
11328275SEric Cheng 		prev = &cclient->mci_client_next;
11338275SEric Cheng 		cclient = *prev;
11348275SEric Cheng 	}
11358275SEric Cheng 	ASSERT(cclient != NULL);
11368275SEric Cheng 	*prev = cclient->mci_client_next;
11378275SEric Cheng 	mip->mi_nclients--;
11388275SEric Cheng 	rw_exit(&mip->mi_rw_lock);
11398275SEric Cheng }
11408275SEric Cheng 
11418275SEric Cheng static mac_unicast_impl_t *
11428275SEric Cheng mac_client_find_vid(mac_client_impl_t *mcip, uint16_t vid)
11438275SEric Cheng {
11448275SEric Cheng 	mac_unicast_impl_t *muip = mcip->mci_unicast_list;
11458275SEric Cheng 
11468275SEric Cheng 	while ((muip != NULL) && (muip->mui_vid != vid))
11478275SEric Cheng 		muip = muip->mui_next;
11488275SEric Cheng 
11498275SEric Cheng 	return (muip);
11508275SEric Cheng }
11518275SEric Cheng 
11528275SEric Cheng /*
11538275SEric Cheng  * Return whether the specified (MAC address, VID) tuple is already used by
11548275SEric Cheng  * one of the MAC clients associated with the specified MAC.
11558275SEric Cheng  */
11568275SEric Cheng static boolean_t
11578275SEric Cheng mac_addr_in_use(mac_impl_t *mip, uint8_t *mac_addr, uint16_t vid)
11588275SEric Cheng {
11598275SEric Cheng 	mac_client_impl_t *client;
11608275SEric Cheng 	mac_address_t *map;
11618275SEric Cheng 
11628275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
11638275SEric Cheng 
11648275SEric Cheng 	for (client = mip->mi_clients_list; client != NULL;
11658275SEric Cheng 	    client = client->mci_client_next) {
11668275SEric Cheng 
11678275SEric Cheng 		/*
11688275SEric Cheng 		 * Ignore clients that don't have unicast address.
11698275SEric Cheng 		 */
11708275SEric Cheng 		if (client->mci_unicast_list == NULL)
11718275SEric Cheng 			continue;
11728275SEric Cheng 
11738275SEric Cheng 		map = client->mci_unicast;
11748275SEric Cheng 
11758275SEric Cheng 		if ((bcmp(mac_addr, map->ma_addr, map->ma_len) == 0) &&
11768275SEric Cheng 		    (mac_client_find_vid(client, vid) != NULL)) {
11778275SEric Cheng 			return (B_TRUE);
11788275SEric Cheng 		}
11798275SEric Cheng 	}
11808275SEric Cheng 
11818275SEric Cheng 	return (B_FALSE);
11828275SEric Cheng }
11838275SEric Cheng 
11848275SEric Cheng /*
11858275SEric Cheng  * Generate a random MAC address. The MAC address prefix is
11868275SEric Cheng  * stored in the array pointed to by mac_addr, and its length, in bytes,
11878275SEric Cheng  * is specified by prefix_len. The least significant bits
11888275SEric Cheng  * after prefix_len bytes are generated, and stored after the prefix
11898275SEric Cheng  * in the mac_addr array.
11908275SEric Cheng  */
11918275SEric Cheng int
11928275SEric Cheng mac_addr_random(mac_client_handle_t mch, uint_t prefix_len,
11938275SEric Cheng     uint8_t *mac_addr, mac_diag_t *diag)
11948275SEric Cheng {
11958275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
11968275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
11978275SEric Cheng 	size_t addr_len = mip->mi_type->mt_addr_length;
11988275SEric Cheng 
11998275SEric Cheng 	if (prefix_len >= addr_len) {
12008275SEric Cheng 		*diag = MAC_DIAG_MACPREFIXLEN_INVALID;
12018275SEric Cheng 		return (EINVAL);
12028275SEric Cheng 	}
12038275SEric Cheng 
12048275SEric Cheng 	/* check the prefix value */
12058275SEric Cheng 	if (prefix_len > 0) {
12068275SEric Cheng 		bzero(mac_addr + prefix_len, addr_len - prefix_len);
12078275SEric Cheng 		if (!mac_unicst_verify((mac_handle_t)mip, mac_addr,
12088275SEric Cheng 		    addr_len)) {
12098275SEric Cheng 			*diag = MAC_DIAG_MACPREFIX_INVALID;
12108275SEric Cheng 			return (EINVAL);
12118275SEric Cheng 		}
12128275SEric Cheng 	}
12138275SEric Cheng 
12148275SEric Cheng 	/* generate the MAC address */
12158275SEric Cheng 	if (prefix_len < addr_len) {
12168275SEric Cheng 		(void) random_get_pseudo_bytes(mac_addr +
12178275SEric Cheng 		    prefix_len, addr_len - prefix_len);
12188275SEric Cheng 	}
12198275SEric Cheng 
12208275SEric Cheng 	*diag = 0;
12218275SEric Cheng 	return (0);
12228275SEric Cheng }
12238275SEric Cheng 
12248275SEric Cheng /*
12258275SEric Cheng  * Set the priority range for this MAC client. This will be used to
12268275SEric Cheng  * determine the absolute priority for the threads created for this
12278275SEric Cheng  * MAC client using the specified "low", "medium" and "high" level.
12288275SEric Cheng  * This will also be used for any subflows on this MAC client.
12298275SEric Cheng  */
12308275SEric Cheng #define	MAC_CLIENT_SET_PRIORITY_RANGE(mcip, pri) {			\
12318275SEric Cheng 	(mcip)->mci_min_pri = FLOW_MIN_PRIORITY(MINCLSYSPRI,	\
12328275SEric Cheng 	    MAXCLSYSPRI, (pri));					\
12338275SEric Cheng 	(mcip)->mci_max_pri = FLOW_MAX_PRIORITY(MINCLSYSPRI,	\
12348275SEric Cheng 	    MAXCLSYSPRI, (mcip)->mci_min_pri);				\
12358275SEric Cheng 	}
12368275SEric Cheng 
12378275SEric Cheng /*
12388275SEric Cheng  * MAC client open entry point. Return a new MAC client handle. Each
12398275SEric Cheng  * MAC client is associated with a name, specified through the 'name'
12408275SEric Cheng  * argument.
12418275SEric Cheng  */
12428275SEric Cheng int
12438275SEric Cheng mac_client_open(mac_handle_t mh, mac_client_handle_t *mchp, char *name,
12448275SEric Cheng     uint16_t flags)
12458275SEric Cheng {
124611878SVenu.Iyer@Sun.COM 	mac_impl_t		*mip = (mac_impl_t *)mh;
124711878SVenu.Iyer@Sun.COM 	mac_client_impl_t	*mcip;
124811878SVenu.Iyer@Sun.COM 	int			err = 0;
124911878SVenu.Iyer@Sun.COM 	boolean_t		share_desired;
125011878SVenu.Iyer@Sun.COM 	flow_entry_t		*flent = NULL;
125111878SVenu.Iyer@Sun.COM 
125211878SVenu.Iyer@Sun.COM 	share_desired = (flags & MAC_OPEN_FLAGS_SHARES_DESIRED) != 0;
12538275SEric Cheng 	*mchp = NULL;
12548275SEric Cheng 
12558275SEric Cheng 	i_mac_perim_enter(mip);
12568275SEric Cheng 
12578275SEric Cheng 	if (mip->mi_state_flags & MIS_IS_VNIC) {
12588275SEric Cheng 		/*
12598275SEric Cheng 		 * The underlying MAC is a VNIC. Return the MAC client
12608275SEric Cheng 		 * handle of the lower MAC which was obtained by
12618275SEric Cheng 		 * the VNIC driver when it did its mac_client_open().
12628275SEric Cheng 		 */
12638275SEric Cheng 
12648275SEric Cheng 		mcip = mac_vnic_lower(mip);
12658275SEric Cheng 
12668275SEric Cheng 		/*
12678275SEric Cheng 		 * Note that multiple mac clients share the same mcip in
12688275SEric Cheng 		 * this case.
12698275SEric Cheng 		 */
12708275SEric Cheng 		if (flags & MAC_OPEN_FLAGS_EXCLUSIVE)
12718275SEric Cheng 			mcip->mci_state_flags |= MCIS_EXCLUSIVE;
12728275SEric Cheng 
12739473SVenu.Iyer@Sun.COM 		if (flags & MAC_OPEN_FLAGS_MULTI_PRIMARY)
12749473SVenu.Iyer@Sun.COM 			mcip->mci_flags |= MAC_CLIENT_FLAGS_MULTI_PRIMARY;
12759473SVenu.Iyer@Sun.COM 
12768275SEric Cheng 		mip->mi_clients_list = mcip;
12778275SEric Cheng 		i_mac_perim_exit(mip);
12788275SEric Cheng 		*mchp = (mac_client_handle_t)mcip;
12798275SEric Cheng 		return (err);
12808275SEric Cheng 	}
12818275SEric Cheng 
12828275SEric Cheng 	mcip = kmem_cache_alloc(mac_client_impl_cache, KM_SLEEP);
12838275SEric Cheng 
12848275SEric Cheng 	mcip->mci_mip = mip;
12858275SEric Cheng 	mcip->mci_upper_mip = NULL;
12868275SEric Cheng 	mcip->mci_rx_fn = mac_pkt_drop;
12878275SEric Cheng 	mcip->mci_rx_arg = NULL;
12889473SVenu.Iyer@Sun.COM 	mcip->mci_rx_p_fn = NULL;
12899473SVenu.Iyer@Sun.COM 	mcip->mci_rx_p_arg = NULL;
12909473SVenu.Iyer@Sun.COM 	mcip->mci_p_unicast_list = NULL;
12918275SEric Cheng 	mcip->mci_direct_rx_fn = NULL;
12928275SEric Cheng 	mcip->mci_direct_rx_arg = NULL;
12938275SEric Cheng 
12949473SVenu.Iyer@Sun.COM 	mcip->mci_unicast_list = NULL;
12959473SVenu.Iyer@Sun.COM 
12968275SEric Cheng 	if ((flags & MAC_OPEN_FLAGS_IS_VNIC) != 0)
12978275SEric Cheng 		mcip->mci_state_flags |= MCIS_IS_VNIC;
12988275SEric Cheng 
12998275SEric Cheng 	if ((flags & MAC_OPEN_FLAGS_EXCLUSIVE) != 0)
13008275SEric Cheng 		mcip->mci_state_flags |= MCIS_EXCLUSIVE;
13018275SEric Cheng 
13028275SEric Cheng 	if ((flags & MAC_OPEN_FLAGS_IS_AGGR_PORT) != 0)
13038275SEric Cheng 		mcip->mci_state_flags |= MCIS_IS_AGGR_PORT;
13048275SEric Cheng 
130511878SVenu.Iyer@Sun.COM 	if (mip->mi_state_flags & MIS_IS_AGGR)
130611878SVenu.Iyer@Sun.COM 		mcip->mci_state_flags |= MCIS_IS_AGGR;
130711878SVenu.Iyer@Sun.COM 
13088275SEric Cheng 	if ((flags & MAC_OPEN_FLAGS_USE_DATALINK_NAME) != 0) {
13098275SEric Cheng 		datalink_id_t	linkid;
13108275SEric Cheng 
13118275SEric Cheng 		ASSERT(name == NULL);
13128275SEric Cheng 		if ((err = dls_devnet_macname2linkid(mip->mi_name,
13138275SEric Cheng 		    &linkid)) != 0) {
13148275SEric Cheng 			goto done;
13158275SEric Cheng 		}
13168275SEric Cheng 		if ((err = dls_mgmt_get_linkinfo(linkid, mcip->mci_name, NULL,
13178275SEric Cheng 		    NULL, NULL)) != 0) {
13188275SEric Cheng 			/*
13198275SEric Cheng 			 * Use mac name if dlmgmtd is not available.
13208275SEric Cheng 			 */
13218275SEric Cheng 			if (err == EBADF) {
13228275SEric Cheng 				(void) strlcpy(mcip->mci_name, mip->mi_name,
13238275SEric Cheng 				    sizeof (mcip->mci_name));
13248275SEric Cheng 				err = 0;
13258275SEric Cheng 			} else {
13268275SEric Cheng 				goto done;
13278275SEric Cheng 			}
13288275SEric Cheng 		}
13298275SEric Cheng 		mcip->mci_state_flags |= MCIS_USE_DATALINK_NAME;
13308275SEric Cheng 	} else {
13318275SEric Cheng 		ASSERT(name != NULL);
13328275SEric Cheng 		if (strlen(name) > MAXNAMELEN) {
13338275SEric Cheng 			err = EINVAL;
13348275SEric Cheng 			goto done;
13358275SEric Cheng 		}
13368275SEric Cheng 		(void) strlcpy(mcip->mci_name, name, sizeof (mcip->mci_name));
13378275SEric Cheng 	}
13389473SVenu.Iyer@Sun.COM 
13399473SVenu.Iyer@Sun.COM 	if (flags & MAC_OPEN_FLAGS_MULTI_PRIMARY)
13409473SVenu.Iyer@Sun.COM 		mcip->mci_flags |= MAC_CLIENT_FLAGS_MULTI_PRIMARY;
13419473SVenu.Iyer@Sun.COM 
134211878SVenu.Iyer@Sun.COM 	if (flags & MAC_OPEN_FLAGS_NO_UNICAST_ADDR)
134311878SVenu.Iyer@Sun.COM 		mcip->mci_state_flags |= MCIS_NO_UNICAST_ADDR;
134411878SVenu.Iyer@Sun.COM 
134511878SVenu.Iyer@Sun.COM 	mac_protect_init(mcip);
134611878SVenu.Iyer@Sun.COM 
13478275SEric Cheng 	/* the subflow table will be created dynamically */
13488275SEric Cheng 	mcip->mci_subflow_tab = NULL;
134911878SVenu.Iyer@Sun.COM 
135011878SVenu.Iyer@Sun.COM 	mcip->mci_misc_stat.mms_multircv = 0;
135111878SVenu.Iyer@Sun.COM 	mcip->mci_misc_stat.mms_brdcstrcv = 0;
135211878SVenu.Iyer@Sun.COM 	mcip->mci_misc_stat.mms_multixmt = 0;
135311878SVenu.Iyer@Sun.COM 	mcip->mci_misc_stat.mms_brdcstxmt = 0;
13548275SEric Cheng 
13558275SEric Cheng 	/* Create an initial flow */
13568275SEric Cheng 
13578275SEric Cheng 	err = mac_flow_create(NULL, NULL, mcip->mci_name, NULL,
13588275SEric Cheng 	    mcip->mci_state_flags & MCIS_IS_VNIC ? FLOW_VNIC_MAC :
13598275SEric Cheng 	    FLOW_PRIMARY_MAC, &flent);
13608275SEric Cheng 	if (err != 0)
13618275SEric Cheng 		goto done;
13628275SEric Cheng 	mcip->mci_flent = flent;
13638275SEric Cheng 	FLOW_MARK(flent, FE_MC_NO_DATAPATH);
13648275SEric Cheng 	flent->fe_mcip = mcip;
13658275SEric Cheng 	/*
13668275SEric Cheng 	 * Place initial creation reference on the flow. This reference
13678275SEric Cheng 	 * is released in the corresponding delete action viz.
13688275SEric Cheng 	 * mac_unicast_remove after waiting for all transient refs to
13698275SEric Cheng 	 * to go away. The wait happens in mac_flow_wait.
13708275SEric Cheng 	 */
13718275SEric Cheng 	FLOW_REFHOLD(flent);
13728275SEric Cheng 
13738275SEric Cheng 	/*
13748275SEric Cheng 	 * Do this ahead of the mac_bcast_add() below so that the mi_nclients
13758275SEric Cheng 	 * will have the right value for mac_rx_srs_setup().
13768275SEric Cheng 	 */
13778275SEric Cheng 	mac_client_add(mcip);
13788275SEric Cheng 
13798275SEric Cheng 	mcip->mci_share = NULL;
138011878SVenu.Iyer@Sun.COM 	if (share_desired)
13818275SEric Cheng 		i_mac_share_alloc(mcip);
13828275SEric Cheng 
13838275SEric Cheng 	DTRACE_PROBE2(mac__client__open__allocated, mac_impl_t *,
13848275SEric Cheng 	    mcip->mci_mip, mac_client_impl_t *, mcip);
13858275SEric Cheng 	*mchp = (mac_client_handle_t)mcip;
13868275SEric Cheng 
138711878SVenu.Iyer@Sun.COM 	/*
138811878SVenu.Iyer@Sun.COM 	 * We will do mimimal datapath setup to allow a MAC client to
138911878SVenu.Iyer@Sun.COM 	 * transmit or receive non-unicast packets without waiting
139011878SVenu.Iyer@Sun.COM 	 * for mac_unicast_add.
139111878SVenu.Iyer@Sun.COM 	 */
139211878SVenu.Iyer@Sun.COM 	if (mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR) {
139311878SVenu.Iyer@Sun.COM 		if ((err = mac_client_datapath_setup(mcip, VLAN_ID_NONE,
139411878SVenu.Iyer@Sun.COM 		    NULL, NULL, B_TRUE, NULL)) != 0) {
139511878SVenu.Iyer@Sun.COM 			goto done;
139611878SVenu.Iyer@Sun.COM 		}
139711878SVenu.Iyer@Sun.COM 	}
13988275SEric Cheng 	i_mac_perim_exit(mip);
13998275SEric Cheng 	return (0);
14008275SEric Cheng 
14018275SEric Cheng done:
14028275SEric Cheng 	i_mac_perim_exit(mip);
14038275SEric Cheng 	mcip->mci_state_flags = 0;
14048275SEric Cheng 	mcip->mci_tx_flag = 0;
14058275SEric Cheng 	kmem_cache_free(mac_client_impl_cache, mcip);
14068275SEric Cheng 	return (err);
14078275SEric Cheng }
14088275SEric Cheng 
14098275SEric Cheng /*
14108275SEric Cheng  * Close the specified MAC client handle.
14118275SEric Cheng  */
14128275SEric Cheng void
14138275SEric Cheng mac_client_close(mac_client_handle_t mch, uint16_t flags)
14148275SEric Cheng {
14158275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
14168275SEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
14178275SEric Cheng 	flow_entry_t		*flent;
14188275SEric Cheng 
14198275SEric Cheng 	i_mac_perim_enter(mip);
14208275SEric Cheng 
14218275SEric Cheng 	if (flags & MAC_CLOSE_FLAGS_EXCLUSIVE)
14228275SEric Cheng 		mcip->mci_state_flags &= ~MCIS_EXCLUSIVE;
14238275SEric Cheng 
14248275SEric Cheng 	if ((mcip->mci_state_flags & MCIS_IS_VNIC) &&
14258275SEric Cheng 	    !(flags & MAC_CLOSE_FLAGS_IS_VNIC)) {
14268275SEric Cheng 		/*
14278275SEric Cheng 		 * This is an upper VNIC client initiated operation.
14288275SEric Cheng 		 * The lower MAC client will be closed by the VNIC driver
14298275SEric Cheng 		 * when the VNIC is deleted.
14308275SEric Cheng 		 */
14318275SEric Cheng 
14328275SEric Cheng 		i_mac_perim_exit(mip);
14338275SEric Cheng 		return;
14348275SEric Cheng 	}
14358275SEric Cheng 
143611878SVenu.Iyer@Sun.COM 	/* If we have only setup up minimal datapth setup, tear it down */
143711878SVenu.Iyer@Sun.COM 	if (mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR) {
143811878SVenu.Iyer@Sun.COM 		mac_client_datapath_teardown((mac_client_handle_t)mcip, NULL,
143911878SVenu.Iyer@Sun.COM 		    mcip->mci_flent);
144011878SVenu.Iyer@Sun.COM 		mcip->mci_state_flags &= ~MCIS_NO_UNICAST_ADDR;
144111878SVenu.Iyer@Sun.COM 	}
144211878SVenu.Iyer@Sun.COM 
14438275SEric Cheng 	/*
14448275SEric Cheng 	 * Remove the flent associated with the MAC client
14458275SEric Cheng 	 */
14468275SEric Cheng 	flent = mcip->mci_flent;
14478275SEric Cheng 	mcip->mci_flent = NULL;
14488275SEric Cheng 	FLOW_FINAL_REFRELE(flent);
14498275SEric Cheng 
14508275SEric Cheng 	/*
14518275SEric Cheng 	 * MAC clients must remove the unicast addresses and promisc callbacks
14528275SEric Cheng 	 * they added before issuing a mac_client_close().
14538275SEric Cheng 	 */
14548275SEric Cheng 	ASSERT(mcip->mci_unicast_list == NULL);
14558275SEric Cheng 	ASSERT(mcip->mci_promisc_list == NULL);
14568275SEric Cheng 	ASSERT(mcip->mci_tx_notify_cb_list == NULL);
14578275SEric Cheng 
14588275SEric Cheng 	i_mac_share_free(mcip);
145911878SVenu.Iyer@Sun.COM 	mac_protect_fini(mcip);
14608275SEric Cheng 	mac_client_remove(mcip);
14618275SEric Cheng 
14628275SEric Cheng 	i_mac_perim_exit(mip);
14638275SEric Cheng 	mcip->mci_subflow_tab = NULL;
14648275SEric Cheng 	mcip->mci_state_flags = 0;
14658275SEric Cheng 	mcip->mci_tx_flag = 0;
14668275SEric Cheng 	kmem_cache_free(mac_client_impl_cache, mch);
14678275SEric Cheng }
14688275SEric Cheng 
14698275SEric Cheng /*
147011021SEric.Cheng@Sun.COM  * Set the rx bypass receive callback.
14718275SEric Cheng  */
14728275SEric Cheng boolean_t
14738275SEric Cheng mac_rx_bypass_set(mac_client_handle_t mch, mac_direct_rx_t rx_fn, void *arg1)
14748275SEric Cheng {
14758275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
14768275SEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
14778275SEric Cheng 
14788275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
14798275SEric Cheng 
14808275SEric Cheng 	/*
14818833SVenu.Iyer@Sun.COM 	 * If the mac_client is a VLAN, we should not do DLS bypass and
14828833SVenu.Iyer@Sun.COM 	 * instead let the packets come up via mac_rx_deliver so the vlan
14838833SVenu.Iyer@Sun.COM 	 * header can be stripped.
14848275SEric Cheng 	 */
14858833SVenu.Iyer@Sun.COM 	if (mcip->mci_nvids > 0)
14868275SEric Cheng 		return (B_FALSE);
14878275SEric Cheng 
14888275SEric Cheng 	/*
14898275SEric Cheng 	 * These are not accessed directly in the data path, and hence
14908275SEric Cheng 	 * don't need any protection
14918275SEric Cheng 	 */
14928275SEric Cheng 	mcip->mci_direct_rx_fn = rx_fn;
14938275SEric Cheng 	mcip->mci_direct_rx_arg = arg1;
14948275SEric Cheng 	return (B_TRUE);
14958275SEric Cheng }
14968275SEric Cheng 
14978275SEric Cheng /*
149811021SEric.Cheng@Sun.COM  * Enable/Disable rx bypass. By default, bypass is assumed to be enabled.
149911021SEric.Cheng@Sun.COM  */
150011021SEric.Cheng@Sun.COM void
150111021SEric.Cheng@Sun.COM mac_rx_bypass_enable(mac_client_handle_t mch)
150211021SEric.Cheng@Sun.COM {
150311021SEric.Cheng@Sun.COM 	((mac_client_impl_t *)mch)->mci_state_flags &= ~MCIS_RX_BYPASS_DISABLE;
150411021SEric.Cheng@Sun.COM }
150511021SEric.Cheng@Sun.COM 
150611021SEric.Cheng@Sun.COM void
150711021SEric.Cheng@Sun.COM mac_rx_bypass_disable(mac_client_handle_t mch)
150811021SEric.Cheng@Sun.COM {
150911021SEric.Cheng@Sun.COM 	((mac_client_impl_t *)mch)->mci_state_flags |= MCIS_RX_BYPASS_DISABLE;
151011021SEric.Cheng@Sun.COM }
151111021SEric.Cheng@Sun.COM 
151211021SEric.Cheng@Sun.COM /*
15138275SEric Cheng  * Set the receive callback for the specified MAC client. There can be
15148275SEric Cheng  * at most one such callback per MAC client.
15158275SEric Cheng  */
15168275SEric Cheng void
15178275SEric Cheng mac_rx_set(mac_client_handle_t mch, mac_rx_t rx_fn, void *arg)
15188275SEric Cheng {
15198275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
15208275SEric Cheng 	mac_impl_t	*mip = mcip->mci_mip;
15218275SEric Cheng 
15228275SEric Cheng 	/*
15238275SEric Cheng 	 * Instead of adding an extra set of locks and refcnts in
15248275SEric Cheng 	 * the datapath at the mac client boundary, we temporarily quiesce
15258275SEric Cheng 	 * the SRS and related entities. We then change the receive function
15268275SEric Cheng 	 * without interference from any receive data thread and then reenable
15278275SEric Cheng 	 * the data flow subsequently.
15288275SEric Cheng 	 */
15298275SEric Cheng 	i_mac_perim_enter(mip);
15308275SEric Cheng 	mac_rx_client_quiesce(mch);
15318275SEric Cheng 
15328275SEric Cheng 	mcip->mci_rx_fn = rx_fn;
15338275SEric Cheng 	mcip->mci_rx_arg = arg;
15348275SEric Cheng 	mac_rx_client_restart(mch);
15358275SEric Cheng 	i_mac_perim_exit(mip);
15368275SEric Cheng }
15378275SEric Cheng 
15388275SEric Cheng /*
15398275SEric Cheng  * Reset the receive callback for the specified MAC client.
15408275SEric Cheng  */
15418275SEric Cheng void
15428275SEric Cheng mac_rx_clear(mac_client_handle_t mch)
15438275SEric Cheng {
15448275SEric Cheng 	mac_rx_set(mch, mac_pkt_drop, NULL);
15458275SEric Cheng }
15468275SEric Cheng 
15478275SEric Cheng /*
15488275SEric Cheng  * Walk the MAC client subflow table and updates their priority values.
15498275SEric Cheng  */
15508275SEric Cheng static int
15518275SEric Cheng mac_update_subflow_priority_cb(flow_entry_t *flent, void *arg)
15528275SEric Cheng {
15538275SEric Cheng 	mac_flow_update_priority(arg, flent);
15548275SEric Cheng 	return (0);
15558275SEric Cheng }
15568275SEric Cheng 
15578275SEric Cheng void
15588275SEric Cheng mac_update_subflow_priority(mac_client_impl_t *mcip)
15598275SEric Cheng {
15608275SEric Cheng 	(void) mac_flow_walk(mcip->mci_subflow_tab,
15618275SEric Cheng 	    mac_update_subflow_priority_cb, mcip);
15628275SEric Cheng }
15638275SEric Cheng 
15648275SEric Cheng /*
156511878SVenu.Iyer@Sun.COM  * Modify the TX or RX ring properties. We could either just move around
156611878SVenu.Iyer@Sun.COM  * rings, i.e add/remove rings given to a client. Or this might cause the
156711878SVenu.Iyer@Sun.COM  * client to move from hardware based to software or the other way around.
156811878SVenu.Iyer@Sun.COM  * If we want to reset this property, then we clear the mask, additionally
156911878SVenu.Iyer@Sun.COM  * if the client was given a non-default group we remove all rings except
157011878SVenu.Iyer@Sun.COM  * for 1 and give it back to the default group.
157111878SVenu.Iyer@Sun.COM  */
157211878SVenu.Iyer@Sun.COM int
157311878SVenu.Iyer@Sun.COM mac_client_set_rings_prop(mac_client_impl_t *mcip, mac_resource_props_t *mrp,
157411878SVenu.Iyer@Sun.COM     mac_resource_props_t *tmrp)
157511878SVenu.Iyer@Sun.COM {
157611878SVenu.Iyer@Sun.COM 	mac_impl_t		*mip = mcip->mci_mip;
157711878SVenu.Iyer@Sun.COM 	flow_entry_t		*flent = mcip->mci_flent;
157811878SVenu.Iyer@Sun.COM 	uint8_t			*mac_addr;
157911878SVenu.Iyer@Sun.COM 	int			err = 0;
158011878SVenu.Iyer@Sun.COM 	mac_group_t		*defgrp;
158111878SVenu.Iyer@Sun.COM 	mac_group_t		*group;
158211878SVenu.Iyer@Sun.COM 	mac_group_t		*ngrp;
158311878SVenu.Iyer@Sun.COM 	mac_resource_props_t	*cmrp = MCIP_RESOURCE_PROPS(mcip);
158411878SVenu.Iyer@Sun.COM 	uint_t			ringcnt;
158511878SVenu.Iyer@Sun.COM 	boolean_t		unspec;
158611878SVenu.Iyer@Sun.COM 
158711878SVenu.Iyer@Sun.COM 	if (mcip->mci_share != NULL)
158811878SVenu.Iyer@Sun.COM 		return (EINVAL);
158911878SVenu.Iyer@Sun.COM 
159011878SVenu.Iyer@Sun.COM 	if (mrp->mrp_mask & MRP_RX_RINGS) {
159111878SVenu.Iyer@Sun.COM 		unspec = mrp->mrp_mask & MRP_RXRINGS_UNSPEC;
159211878SVenu.Iyer@Sun.COM 		group = flent->fe_rx_ring_group;
159311878SVenu.Iyer@Sun.COM 		defgrp = MAC_DEFAULT_RX_GROUP(mip);
159411878SVenu.Iyer@Sun.COM 		mac_addr = flent->fe_flow_desc.fd_dst_mac;
159511878SVenu.Iyer@Sun.COM 
159611878SVenu.Iyer@Sun.COM 		/*
159711878SVenu.Iyer@Sun.COM 		 * No resulting change. If we are resetting on a client on
159811878SVenu.Iyer@Sun.COM 		 * which there was no rx rings property. For dynamic group
159911878SVenu.Iyer@Sun.COM 		 * if we are setting the same number of rings already set.
160011878SVenu.Iyer@Sun.COM 		 * For static group if we are requesting a group again.
160111878SVenu.Iyer@Sun.COM 		 */
160211878SVenu.Iyer@Sun.COM 		if (mrp->mrp_mask & MRP_RINGS_RESET) {
160311878SVenu.Iyer@Sun.COM 			if (!(tmrp->mrp_mask & MRP_RX_RINGS))
160411878SVenu.Iyer@Sun.COM 				return (0);
160511878SVenu.Iyer@Sun.COM 		} else {
160611878SVenu.Iyer@Sun.COM 			if (unspec) {
160711878SVenu.Iyer@Sun.COM 				if (tmrp->mrp_mask & MRP_RXRINGS_UNSPEC)
160811878SVenu.Iyer@Sun.COM 					return (0);
160911878SVenu.Iyer@Sun.COM 			} else if (mip->mi_rx_group_type ==
161011878SVenu.Iyer@Sun.COM 			    MAC_GROUP_TYPE_DYNAMIC) {
161111878SVenu.Iyer@Sun.COM 				if ((tmrp->mrp_mask & MRP_RX_RINGS) &&
161211878SVenu.Iyer@Sun.COM 				    !(tmrp->mrp_mask & MRP_RXRINGS_UNSPEC) &&
161311878SVenu.Iyer@Sun.COM 				    mrp->mrp_nrxrings == tmrp->mrp_nrxrings) {
161411878SVenu.Iyer@Sun.COM 					return (0);
161511878SVenu.Iyer@Sun.COM 				}
161611878SVenu.Iyer@Sun.COM 			}
161711878SVenu.Iyer@Sun.COM 		}
161811878SVenu.Iyer@Sun.COM 		/* Resetting the prop */
161911878SVenu.Iyer@Sun.COM 		if (mrp->mrp_mask & MRP_RINGS_RESET) {
162011878SVenu.Iyer@Sun.COM 			/*
162111878SVenu.Iyer@Sun.COM 			 * We will just keep one ring and give others back if
162211878SVenu.Iyer@Sun.COM 			 * we are not the primary. For the primary we give
162311878SVenu.Iyer@Sun.COM 			 * all the rings in the default group except the
162411878SVenu.Iyer@Sun.COM 			 * default ring. If it is a static group, then
162511878SVenu.Iyer@Sun.COM 			 * we don't do anything, but clear the MRP_RX_RINGS
162611878SVenu.Iyer@Sun.COM 			 * flag.
162711878SVenu.Iyer@Sun.COM 			 */
162811878SVenu.Iyer@Sun.COM 			if (group != defgrp) {
162911878SVenu.Iyer@Sun.COM 				if (mip->mi_rx_group_type ==
163011878SVenu.Iyer@Sun.COM 				    MAC_GROUP_TYPE_DYNAMIC) {
163111878SVenu.Iyer@Sun.COM 					/*
163211878SVenu.Iyer@Sun.COM 					 * This group has reserved rings
163311878SVenu.Iyer@Sun.COM 					 * that need to be released now,
163411878SVenu.Iyer@Sun.COM 					 * so does the group.
163511878SVenu.Iyer@Sun.COM 					 */
163611878SVenu.Iyer@Sun.COM 					MAC_RX_RING_RELEASED(mip,
163711878SVenu.Iyer@Sun.COM 					    group->mrg_cur_count);
163811878SVenu.Iyer@Sun.COM 					MAC_RX_GRP_RELEASED(mip);
163911878SVenu.Iyer@Sun.COM 					if ((flent->fe_type &
164011878SVenu.Iyer@Sun.COM 					    FLOW_PRIMARY_MAC) != 0) {
164111878SVenu.Iyer@Sun.COM 						if (mip->mi_nactiveclients ==
164211878SVenu.Iyer@Sun.COM 						    1) {
164311878SVenu.Iyer@Sun.COM 							(void)
164411878SVenu.Iyer@Sun.COM 							    mac_rx_switch_group(
164511878SVenu.Iyer@Sun.COM 							    mcip, group,
164611878SVenu.Iyer@Sun.COM 							    defgrp);
164711878SVenu.Iyer@Sun.COM 							return (0);
164811878SVenu.Iyer@Sun.COM 						} else {
164911878SVenu.Iyer@Sun.COM 							cmrp->mrp_nrxrings =
165011878SVenu.Iyer@Sun.COM 							    group->
165111878SVenu.Iyer@Sun.COM 							    mrg_cur_count +
165211878SVenu.Iyer@Sun.COM 							    defgrp->
165311878SVenu.Iyer@Sun.COM 							    mrg_cur_count - 1;
165411878SVenu.Iyer@Sun.COM 						}
165511878SVenu.Iyer@Sun.COM 					} else {
165611878SVenu.Iyer@Sun.COM 						cmrp->mrp_nrxrings = 1;
165711878SVenu.Iyer@Sun.COM 					}
165811878SVenu.Iyer@Sun.COM 					(void) mac_group_ring_modify(mcip,
165911878SVenu.Iyer@Sun.COM 					    group, defgrp);
166011878SVenu.Iyer@Sun.COM 				} else {
166111878SVenu.Iyer@Sun.COM 					/*
166211878SVenu.Iyer@Sun.COM 					 * If this is a static group, we
166311878SVenu.Iyer@Sun.COM 					 * need to release the group. The
166411878SVenu.Iyer@Sun.COM 					 * client will remain in the same
166511878SVenu.Iyer@Sun.COM 					 * group till some other client
166611878SVenu.Iyer@Sun.COM 					 * needs this group.
166711878SVenu.Iyer@Sun.COM 					 */
166811878SVenu.Iyer@Sun.COM 					MAC_RX_GRP_RELEASED(mip);
166911878SVenu.Iyer@Sun.COM 				}
167011878SVenu.Iyer@Sun.COM 			/* Let check if we can give this an excl group */
167111878SVenu.Iyer@Sun.COM 			} else if (group == defgrp) {
167211878SVenu.Iyer@Sun.COM 				ngrp = 	mac_reserve_rx_group(mcip, mac_addr,
167311878SVenu.Iyer@Sun.COM 				    B_TRUE);
167411878SVenu.Iyer@Sun.COM 				/* Couldn't give it a group, that's fine */
167511878SVenu.Iyer@Sun.COM 				if (ngrp == NULL)
167611878SVenu.Iyer@Sun.COM 					return (0);
167711878SVenu.Iyer@Sun.COM 				/* Switch to H/W */
167811878SVenu.Iyer@Sun.COM 				if (mac_rx_switch_group(mcip, defgrp, ngrp) !=
167911878SVenu.Iyer@Sun.COM 				    0) {
168011878SVenu.Iyer@Sun.COM 					mac_stop_group(ngrp);
168111878SVenu.Iyer@Sun.COM 					return (0);
168211878SVenu.Iyer@Sun.COM 				}
168311878SVenu.Iyer@Sun.COM 			}
168411878SVenu.Iyer@Sun.COM 			/*
168511878SVenu.Iyer@Sun.COM 			 * If the client is in the default group, we will
168611878SVenu.Iyer@Sun.COM 			 * just clear the MRP_RX_RINGS and leave it as
168711878SVenu.Iyer@Sun.COM 			 * it rather than look for an exclusive group
168811878SVenu.Iyer@Sun.COM 			 * for it.
168911878SVenu.Iyer@Sun.COM 			 */
169011878SVenu.Iyer@Sun.COM 			return (0);
169111878SVenu.Iyer@Sun.COM 		}
169211878SVenu.Iyer@Sun.COM 
169311878SVenu.Iyer@Sun.COM 		if (group == defgrp && ((mrp->mrp_nrxrings > 0) || unspec)) {
169411878SVenu.Iyer@Sun.COM 			ngrp = 	mac_reserve_rx_group(mcip, mac_addr, B_TRUE);
169511878SVenu.Iyer@Sun.COM 			if (ngrp == NULL)
169611878SVenu.Iyer@Sun.COM 				return (ENOSPC);
169711878SVenu.Iyer@Sun.COM 
169811878SVenu.Iyer@Sun.COM 			/* Switch to H/W */
169911878SVenu.Iyer@Sun.COM 			if (mac_rx_switch_group(mcip, defgrp, ngrp) != 0) {
170011878SVenu.Iyer@Sun.COM 				mac_release_rx_group(mcip, ngrp);
170111878SVenu.Iyer@Sun.COM 				return (ENOSPC);
170211878SVenu.Iyer@Sun.COM 			}
170311878SVenu.Iyer@Sun.COM 			MAC_RX_GRP_RESERVED(mip);
170411878SVenu.Iyer@Sun.COM 			if (mip->mi_rx_group_type == MAC_GROUP_TYPE_DYNAMIC)
170511878SVenu.Iyer@Sun.COM 				MAC_RX_RING_RESERVED(mip, ngrp->mrg_cur_count);
170611878SVenu.Iyer@Sun.COM 		} else if (group != defgrp && !unspec &&
170711878SVenu.Iyer@Sun.COM 		    mrp->mrp_nrxrings == 0) {
170811878SVenu.Iyer@Sun.COM 			/* Switch to S/W */
170911878SVenu.Iyer@Sun.COM 			ringcnt = group->mrg_cur_count;
171011878SVenu.Iyer@Sun.COM 			if (mac_rx_switch_group(mcip, group, defgrp) != 0)
171111878SVenu.Iyer@Sun.COM 				return (ENOSPC);
171211878SVenu.Iyer@Sun.COM 			if (tmrp->mrp_mask & MRP_RX_RINGS) {
171311878SVenu.Iyer@Sun.COM 				MAC_RX_GRP_RELEASED(mip);
171411878SVenu.Iyer@Sun.COM 				if (mip->mi_rx_group_type ==
171511878SVenu.Iyer@Sun.COM 				    MAC_GROUP_TYPE_DYNAMIC) {
171611878SVenu.Iyer@Sun.COM 					MAC_RX_RING_RELEASED(mip, ringcnt);
171711878SVenu.Iyer@Sun.COM 				}
171811878SVenu.Iyer@Sun.COM 			}
171911878SVenu.Iyer@Sun.COM 		} else if (group != defgrp && mip->mi_rx_group_type ==
172011878SVenu.Iyer@Sun.COM 		    MAC_GROUP_TYPE_DYNAMIC) {
172111878SVenu.Iyer@Sun.COM 			ringcnt = group->mrg_cur_count;
172211878SVenu.Iyer@Sun.COM 			err = mac_group_ring_modify(mcip, group, defgrp);
172311878SVenu.Iyer@Sun.COM 			if (err != 0)
172411878SVenu.Iyer@Sun.COM 				return (err);
172511878SVenu.Iyer@Sun.COM 			/*
172611878SVenu.Iyer@Sun.COM 			 * Update the accounting. If this group
172711878SVenu.Iyer@Sun.COM 			 * already had explicitly reserved rings,
172811878SVenu.Iyer@Sun.COM 			 * we need to update the rings based on
172911878SVenu.Iyer@Sun.COM 			 * the new ring count. If this group
173011878SVenu.Iyer@Sun.COM 			 * had not explicitly reserved rings,
173111878SVenu.Iyer@Sun.COM 			 * then we just reserve the rings asked for
173211878SVenu.Iyer@Sun.COM 			 * and reserve the group.
173311878SVenu.Iyer@Sun.COM 			 */
173411878SVenu.Iyer@Sun.COM 			if (tmrp->mrp_mask & MRP_RX_RINGS) {
173511878SVenu.Iyer@Sun.COM 				if (ringcnt > group->mrg_cur_count) {
173611878SVenu.Iyer@Sun.COM 					MAC_RX_RING_RELEASED(mip,
173711878SVenu.Iyer@Sun.COM 					    ringcnt - group->mrg_cur_count);
173811878SVenu.Iyer@Sun.COM 				} else {
173911878SVenu.Iyer@Sun.COM 					MAC_RX_RING_RESERVED(mip,
174011878SVenu.Iyer@Sun.COM 					    group->mrg_cur_count - ringcnt);
174111878SVenu.Iyer@Sun.COM 				}
174211878SVenu.Iyer@Sun.COM 			} else {
174311878SVenu.Iyer@Sun.COM 				MAC_RX_RING_RESERVED(mip, group->mrg_cur_count);
174411878SVenu.Iyer@Sun.COM 				MAC_RX_GRP_RESERVED(mip);
174511878SVenu.Iyer@Sun.COM 			}
174611878SVenu.Iyer@Sun.COM 		}
174711878SVenu.Iyer@Sun.COM 	}
174811878SVenu.Iyer@Sun.COM 	if (mrp->mrp_mask & MRP_TX_RINGS) {
174911878SVenu.Iyer@Sun.COM 		unspec = mrp->mrp_mask & MRP_TXRINGS_UNSPEC;
175011878SVenu.Iyer@Sun.COM 		group = flent->fe_tx_ring_group;
175111878SVenu.Iyer@Sun.COM 		defgrp = MAC_DEFAULT_TX_GROUP(mip);
175211878SVenu.Iyer@Sun.COM 
175311878SVenu.Iyer@Sun.COM 		/*
175411878SVenu.Iyer@Sun.COM 		 * For static groups we only allow rings=0 or resetting the
175511878SVenu.Iyer@Sun.COM 		 * rings property.
175611878SVenu.Iyer@Sun.COM 		 */
175711878SVenu.Iyer@Sun.COM 		if (mrp->mrp_ntxrings > 0 &&
175811878SVenu.Iyer@Sun.COM 		    mip->mi_tx_group_type != MAC_GROUP_TYPE_DYNAMIC) {
175911878SVenu.Iyer@Sun.COM 			return (ENOTSUP);
176011878SVenu.Iyer@Sun.COM 		}
176111878SVenu.Iyer@Sun.COM 		if (mrp->mrp_mask & MRP_RINGS_RESET) {
176211878SVenu.Iyer@Sun.COM 			if (!(tmrp->mrp_mask & MRP_TX_RINGS))
176311878SVenu.Iyer@Sun.COM 				return (0);
176411878SVenu.Iyer@Sun.COM 		} else {
176511878SVenu.Iyer@Sun.COM 			if (unspec) {
176611878SVenu.Iyer@Sun.COM 				if (tmrp->mrp_mask & MRP_TXRINGS_UNSPEC)
176711878SVenu.Iyer@Sun.COM 					return (0);
176811878SVenu.Iyer@Sun.COM 			} else if (mip->mi_tx_group_type ==
176911878SVenu.Iyer@Sun.COM 			    MAC_GROUP_TYPE_DYNAMIC) {
177011878SVenu.Iyer@Sun.COM 				if ((tmrp->mrp_mask & MRP_TX_RINGS) &&
177111878SVenu.Iyer@Sun.COM 				    !(tmrp->mrp_mask & MRP_TXRINGS_UNSPEC) &&
177211878SVenu.Iyer@Sun.COM 				    mrp->mrp_ntxrings == tmrp->mrp_ntxrings) {
177311878SVenu.Iyer@Sun.COM 					return (0);
177411878SVenu.Iyer@Sun.COM 				}
177511878SVenu.Iyer@Sun.COM 			}
177611878SVenu.Iyer@Sun.COM 		}
177711878SVenu.Iyer@Sun.COM 		/* Resetting the prop */
177811878SVenu.Iyer@Sun.COM 		if (mrp->mrp_mask & MRP_RINGS_RESET) {
177911878SVenu.Iyer@Sun.COM 			if (group != defgrp) {
178011878SVenu.Iyer@Sun.COM 				if (mip->mi_tx_group_type ==
178111878SVenu.Iyer@Sun.COM 				    MAC_GROUP_TYPE_DYNAMIC) {
178211878SVenu.Iyer@Sun.COM 					ringcnt = group->mrg_cur_count;
178311878SVenu.Iyer@Sun.COM 					if ((flent->fe_type &
178411878SVenu.Iyer@Sun.COM 					    FLOW_PRIMARY_MAC) != 0) {
178511878SVenu.Iyer@Sun.COM 						mac_tx_client_quiesce(
178611878SVenu.Iyer@Sun.COM 						    (mac_client_handle_t)
178711878SVenu.Iyer@Sun.COM 						    mcip);
178811878SVenu.Iyer@Sun.COM 						mac_tx_switch_group(mcip,
178911878SVenu.Iyer@Sun.COM 						    group, defgrp);
179011878SVenu.Iyer@Sun.COM 						mac_tx_client_restart(
179111878SVenu.Iyer@Sun.COM 						    (mac_client_handle_t)
179211878SVenu.Iyer@Sun.COM 						    mcip);
179311878SVenu.Iyer@Sun.COM 						MAC_TX_GRP_RELEASED(mip);
179411878SVenu.Iyer@Sun.COM 						MAC_TX_RING_RELEASED(mip,
179511878SVenu.Iyer@Sun.COM 						    ringcnt);
179611878SVenu.Iyer@Sun.COM 						return (0);
179711878SVenu.Iyer@Sun.COM 					}
179811878SVenu.Iyer@Sun.COM 					cmrp->mrp_ntxrings = 1;
179911878SVenu.Iyer@Sun.COM 					(void) mac_group_ring_modify(mcip,
180011878SVenu.Iyer@Sun.COM 					    group, defgrp);
180111878SVenu.Iyer@Sun.COM 					/*
180211878SVenu.Iyer@Sun.COM 					 * This group has reserved rings
180311878SVenu.Iyer@Sun.COM 					 * that need to be released now.
180411878SVenu.Iyer@Sun.COM 					 */
180511878SVenu.Iyer@Sun.COM 					MAC_TX_RING_RELEASED(mip, ringcnt);
180611878SVenu.Iyer@Sun.COM 				}
180711878SVenu.Iyer@Sun.COM 				/*
180811878SVenu.Iyer@Sun.COM 				 * If this is a static group, we
180911878SVenu.Iyer@Sun.COM 				 * need to release the group. The
181011878SVenu.Iyer@Sun.COM 				 * client will remain in the same
181111878SVenu.Iyer@Sun.COM 				 * group till some other client
181211878SVenu.Iyer@Sun.COM 				 * needs this group.
181311878SVenu.Iyer@Sun.COM 				 */
181411878SVenu.Iyer@Sun.COM 				MAC_TX_GRP_RELEASED(mip);
181511878SVenu.Iyer@Sun.COM 			} else if (group == defgrp &&
181611878SVenu.Iyer@Sun.COM 			    (flent->fe_type & FLOW_PRIMARY_MAC) == 0) {
181711878SVenu.Iyer@Sun.COM 				ngrp = mac_reserve_tx_group(mcip, B_TRUE);
181811878SVenu.Iyer@Sun.COM 				if (ngrp == NULL)
181911878SVenu.Iyer@Sun.COM 					return (0);
182011878SVenu.Iyer@Sun.COM 				mac_tx_client_quiesce(
182111878SVenu.Iyer@Sun.COM 				    (mac_client_handle_t)mcip);
182211878SVenu.Iyer@Sun.COM 				mac_tx_switch_group(mcip, defgrp, ngrp);
182311878SVenu.Iyer@Sun.COM 				mac_tx_client_restart(
182411878SVenu.Iyer@Sun.COM 				    (mac_client_handle_t)mcip);
182511878SVenu.Iyer@Sun.COM 			}
182611878SVenu.Iyer@Sun.COM 			/*
182711878SVenu.Iyer@Sun.COM 			 * If the client is in the default group, we will
182811878SVenu.Iyer@Sun.COM 			 * just clear the MRP_TX_RINGS and leave it as
182911878SVenu.Iyer@Sun.COM 			 * it rather than look for an exclusive group
183011878SVenu.Iyer@Sun.COM 			 * for it.
183111878SVenu.Iyer@Sun.COM 			 */
183211878SVenu.Iyer@Sun.COM 			return (0);
183311878SVenu.Iyer@Sun.COM 		}
183411878SVenu.Iyer@Sun.COM 
183511878SVenu.Iyer@Sun.COM 		/* Switch to H/W */
183611878SVenu.Iyer@Sun.COM 		if (group == defgrp && ((mrp->mrp_ntxrings > 0) || unspec)) {
183711878SVenu.Iyer@Sun.COM 			ngrp = 	mac_reserve_tx_group(mcip, B_TRUE);
183811878SVenu.Iyer@Sun.COM 			if (ngrp == NULL)
183911878SVenu.Iyer@Sun.COM 				return (ENOSPC);
184011878SVenu.Iyer@Sun.COM 			mac_tx_client_quiesce((mac_client_handle_t)mcip);
184111878SVenu.Iyer@Sun.COM 			mac_tx_switch_group(mcip, defgrp, ngrp);
184211878SVenu.Iyer@Sun.COM 			mac_tx_client_restart((mac_client_handle_t)mcip);
184311878SVenu.Iyer@Sun.COM 			MAC_TX_GRP_RESERVED(mip);
184411878SVenu.Iyer@Sun.COM 			if (mip->mi_tx_group_type == MAC_GROUP_TYPE_DYNAMIC)
184511878SVenu.Iyer@Sun.COM 				MAC_TX_RING_RESERVED(mip, ngrp->mrg_cur_count);
184611878SVenu.Iyer@Sun.COM 		/* Switch to S/W */
184711878SVenu.Iyer@Sun.COM 		} else if (group != defgrp && !unspec &&
184811878SVenu.Iyer@Sun.COM 		    mrp->mrp_ntxrings == 0) {
184911878SVenu.Iyer@Sun.COM 			/* Switch to S/W */
185011878SVenu.Iyer@Sun.COM 			ringcnt = group->mrg_cur_count;
185111878SVenu.Iyer@Sun.COM 			mac_tx_client_quiesce((mac_client_handle_t)mcip);
185211878SVenu.Iyer@Sun.COM 			mac_tx_switch_group(mcip, group, defgrp);
185311878SVenu.Iyer@Sun.COM 			mac_tx_client_restart((mac_client_handle_t)mcip);
185411878SVenu.Iyer@Sun.COM 			if (tmrp->mrp_mask & MRP_TX_RINGS) {
185511878SVenu.Iyer@Sun.COM 				MAC_TX_GRP_RELEASED(mip);
185611878SVenu.Iyer@Sun.COM 				if (mip->mi_tx_group_type ==
185711878SVenu.Iyer@Sun.COM 				    MAC_GROUP_TYPE_DYNAMIC) {
185811878SVenu.Iyer@Sun.COM 					MAC_TX_RING_RELEASED(mip, ringcnt);
185911878SVenu.Iyer@Sun.COM 				}
186011878SVenu.Iyer@Sun.COM 			}
186111878SVenu.Iyer@Sun.COM 		} else if (group != defgrp && mip->mi_tx_group_type ==
186211878SVenu.Iyer@Sun.COM 		    MAC_GROUP_TYPE_DYNAMIC) {
186311878SVenu.Iyer@Sun.COM 			ringcnt = group->mrg_cur_count;
186411878SVenu.Iyer@Sun.COM 			err = mac_group_ring_modify(mcip, group, defgrp);
186511878SVenu.Iyer@Sun.COM 			if (err != 0)
186611878SVenu.Iyer@Sun.COM 				return (err);
186711878SVenu.Iyer@Sun.COM 			/*
186811878SVenu.Iyer@Sun.COM 			 * Update the accounting. If this group
186911878SVenu.Iyer@Sun.COM 			 * already had explicitly reserved rings,
187011878SVenu.Iyer@Sun.COM 			 * we need to update the rings based on
187111878SVenu.Iyer@Sun.COM 			 * the new ring count. If this group
187211878SVenu.Iyer@Sun.COM 			 * had not explicitly reserved rings,
187311878SVenu.Iyer@Sun.COM 			 * then we just reserve the rings asked for
187411878SVenu.Iyer@Sun.COM 			 * and reserve the group.
187511878SVenu.Iyer@Sun.COM 			 */
187611878SVenu.Iyer@Sun.COM 			if (tmrp->mrp_mask & MRP_TX_RINGS) {
187711878SVenu.Iyer@Sun.COM 				if (ringcnt > group->mrg_cur_count) {
187811878SVenu.Iyer@Sun.COM 					MAC_TX_RING_RELEASED(mip,
187911878SVenu.Iyer@Sun.COM 					    ringcnt - group->mrg_cur_count);
188011878SVenu.Iyer@Sun.COM 				} else {
188111878SVenu.Iyer@Sun.COM 					MAC_TX_RING_RESERVED(mip,
188211878SVenu.Iyer@Sun.COM 					    group->mrg_cur_count - ringcnt);
188311878SVenu.Iyer@Sun.COM 				}
188411878SVenu.Iyer@Sun.COM 			} else {
188511878SVenu.Iyer@Sun.COM 				MAC_TX_RING_RESERVED(mip, group->mrg_cur_count);
188611878SVenu.Iyer@Sun.COM 				MAC_TX_GRP_RESERVED(mip);
188711878SVenu.Iyer@Sun.COM 			}
188811878SVenu.Iyer@Sun.COM 		}
188911878SVenu.Iyer@Sun.COM 	}
189011878SVenu.Iyer@Sun.COM 	return (0);
189111878SVenu.Iyer@Sun.COM }
189211878SVenu.Iyer@Sun.COM 
189311878SVenu.Iyer@Sun.COM /*
18948275SEric Cheng  * When the MAC client is being brought up (i.e. we do a unicast_add) we need
18958275SEric Cheng  * to initialize the cpu and resource control structure in the
18968275SEric Cheng  * mac_client_impl_t from the mac_impl_t (i.e if there are any cached
18978275SEric Cheng  * properties before the flow entry for the unicast address was created).
18988275SEric Cheng  */
18998275SEric Cheng int
19008275SEric Cheng mac_resource_ctl_set(mac_client_handle_t mch, mac_resource_props_t *mrp)
19018275SEric Cheng {
19028275SEric Cheng 	mac_client_impl_t 	*mcip = (mac_client_impl_t *)mch;
19038275SEric Cheng 	mac_impl_t		*mip = (mac_impl_t *)mcip->mci_mip;
19048275SEric Cheng 	int			err = 0;
190511878SVenu.Iyer@Sun.COM 	flow_entry_t		*flent = mcip->mci_flent;
190611878SVenu.Iyer@Sun.COM 	mac_resource_props_t	*omrp, *nmrp = MCIP_RESOURCE_PROPS(mcip);
19078275SEric Cheng 
19088275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
19098275SEric Cheng 
191011878SVenu.Iyer@Sun.COM 	err = mac_validate_props(mcip->mci_state_flags & MCIS_IS_VNIC ?
191111878SVenu.Iyer@Sun.COM 	    mcip->mci_upper_mip : mip, mrp);
19128275SEric Cheng 	if (err != 0)
19138275SEric Cheng 		return (err);
19148275SEric Cheng 
191511878SVenu.Iyer@Sun.COM 	/*
191611878SVenu.Iyer@Sun.COM 	 * Copy over the existing properties since mac_update_resources
191711878SVenu.Iyer@Sun.COM 	 * will modify the client's mrp. Currently, the saved property
191811878SVenu.Iyer@Sun.COM 	 * is used to determine the difference between existing and
191911878SVenu.Iyer@Sun.COM 	 * modified rings property.
192011878SVenu.Iyer@Sun.COM 	 */
192111878SVenu.Iyer@Sun.COM 	omrp = kmem_zalloc(sizeof (*omrp), KM_SLEEP);
192211878SVenu.Iyer@Sun.COM 	bcopy(nmrp, omrp, sizeof (*omrp));
19238275SEric Cheng 	mac_update_resources(mrp, MCIP_RESOURCE_PROPS(mcip), B_FALSE);
19248275SEric Cheng 	if (MCIP_DATAPATH_SETUP(mcip)) {
19258275SEric Cheng 		/*
192611878SVenu.Iyer@Sun.COM 		 * We support rings only for primary client when there are
192711878SVenu.Iyer@Sun.COM 		 * multiple clients sharing the same MAC address (e.g. VLAN).
192811878SVenu.Iyer@Sun.COM 		 */
192911878SVenu.Iyer@Sun.COM 		if (mrp->mrp_mask & MRP_RX_RINGS ||
193011878SVenu.Iyer@Sun.COM 		    mrp->mrp_mask & MRP_TX_RINGS) {
193111878SVenu.Iyer@Sun.COM 
193211878SVenu.Iyer@Sun.COM 			if ((err = mac_client_set_rings_prop(mcip, mrp,
193311878SVenu.Iyer@Sun.COM 			    omrp)) != 0) {
193411878SVenu.Iyer@Sun.COM 				if (omrp->mrp_mask & MRP_RX_RINGS) {
193511878SVenu.Iyer@Sun.COM 					nmrp->mrp_mask |= MRP_RX_RINGS;
193611878SVenu.Iyer@Sun.COM 					nmrp->mrp_nrxrings = omrp->mrp_nrxrings;
193711878SVenu.Iyer@Sun.COM 				} else {
193811878SVenu.Iyer@Sun.COM 					nmrp->mrp_mask &= ~MRP_RX_RINGS;
193911878SVenu.Iyer@Sun.COM 					nmrp->mrp_nrxrings = 0;
194011878SVenu.Iyer@Sun.COM 				}
194111878SVenu.Iyer@Sun.COM 				if (omrp->mrp_mask & MRP_TX_RINGS) {
194211878SVenu.Iyer@Sun.COM 					nmrp->mrp_mask |= MRP_TX_RINGS;
194311878SVenu.Iyer@Sun.COM 					nmrp->mrp_ntxrings = omrp->mrp_ntxrings;
194411878SVenu.Iyer@Sun.COM 				} else {
194511878SVenu.Iyer@Sun.COM 					nmrp->mrp_mask &= ~MRP_TX_RINGS;
194611878SVenu.Iyer@Sun.COM 					nmrp->mrp_ntxrings = 0;
194711878SVenu.Iyer@Sun.COM 				}
194811878SVenu.Iyer@Sun.COM 				if (omrp->mrp_mask & MRP_RXRINGS_UNSPEC)
194911878SVenu.Iyer@Sun.COM 					omrp->mrp_mask |= MRP_RXRINGS_UNSPEC;
195011878SVenu.Iyer@Sun.COM 				else
195111878SVenu.Iyer@Sun.COM 					omrp->mrp_mask &= ~MRP_RXRINGS_UNSPEC;
195211878SVenu.Iyer@Sun.COM 
195311878SVenu.Iyer@Sun.COM 				if (omrp->mrp_mask & MRP_TXRINGS_UNSPEC)
195411878SVenu.Iyer@Sun.COM 					omrp->mrp_mask |= MRP_TXRINGS_UNSPEC;
195511878SVenu.Iyer@Sun.COM 				else
195611878SVenu.Iyer@Sun.COM 					omrp->mrp_mask &= ~MRP_TXRINGS_UNSPEC;
195711878SVenu.Iyer@Sun.COM 				kmem_free(omrp, sizeof (*omrp));
195811878SVenu.Iyer@Sun.COM 				return (err);
195911878SVenu.Iyer@Sun.COM 			}
196011878SVenu.Iyer@Sun.COM 
196111878SVenu.Iyer@Sun.COM 			/*
196211878SVenu.Iyer@Sun.COM 			 * If we modified the rings property of the primary
196311878SVenu.Iyer@Sun.COM 			 * we need to update the property fields of its
196411878SVenu.Iyer@Sun.COM 			 * VLANs as they inherit the primary's properites.
196511878SVenu.Iyer@Sun.COM 			 */
196611878SVenu.Iyer@Sun.COM 			if (mac_is_primary_client(mcip)) {
196711878SVenu.Iyer@Sun.COM 				mac_set_prim_vlan_rings(mip,
196811878SVenu.Iyer@Sun.COM 				    MCIP_RESOURCE_PROPS(mcip));
196911878SVenu.Iyer@Sun.COM 			}
197011878SVenu.Iyer@Sun.COM 		}
197111878SVenu.Iyer@Sun.COM 		/*
19728275SEric Cheng 		 * We have to set this prior to calling mac_flow_modify.
19738275SEric Cheng 		 */
19748275SEric Cheng 		if (mrp->mrp_mask & MRP_PRIORITY) {
19758275SEric Cheng 			if (mrp->mrp_priority == MPL_RESET) {
19768275SEric Cheng 				MAC_CLIENT_SET_PRIORITY_RANGE(mcip,
19778275SEric Cheng 				    MPL_LINK_DEFAULT);
19788275SEric Cheng 			} else {
19798275SEric Cheng 				MAC_CLIENT_SET_PRIORITY_RANGE(mcip,
19808275SEric Cheng 				    mrp->mrp_priority);
19818275SEric Cheng 			}
19828275SEric Cheng 		}
19838275SEric Cheng 
198411878SVenu.Iyer@Sun.COM 		mac_flow_modify(mip->mi_flow_tab, flent, mrp);
19858275SEric Cheng 		if (mrp->mrp_mask & MRP_PRIORITY)
19868275SEric Cheng 			mac_update_subflow_priority(mcip);
19878275SEric Cheng 	}
198811878SVenu.Iyer@Sun.COM 	kmem_free(omrp, sizeof (*omrp));
19898275SEric Cheng 	return (0);
19908275SEric Cheng }
19918275SEric Cheng 
19928275SEric Cheng void
19938275SEric Cheng mac_resource_ctl_get(mac_client_handle_t mch, mac_resource_props_t *mrp)
19948275SEric Cheng {
19958275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
19968275SEric Cheng 	mac_resource_props_t	*mcip_mrp = MCIP_RESOURCE_PROPS(mcip);
19978275SEric Cheng 
19988275SEric Cheng 	bcopy(mcip_mrp, mrp, sizeof (mac_resource_props_t));
19998275SEric Cheng }
20008275SEric Cheng 
20018275SEric Cheng static int
20028275SEric Cheng mac_unicast_flow_create(mac_client_impl_t *mcip, uint8_t *mac_addr,
20038275SEric Cheng     uint16_t vid, boolean_t is_primary, boolean_t first_flow,
20048275SEric Cheng     flow_entry_t **flent, mac_resource_props_t *mrp)
20058275SEric Cheng {
20068275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mcip->mci_mip;
20078275SEric Cheng 	flow_desc_t	flow_desc;
20088558SGirish.Moodalbail@Sun.COM 	char		flowname[MAXFLOWNAMELEN];
20098275SEric Cheng 	int		err;
20108275SEric Cheng 	uint_t		flent_flags;
20118275SEric Cheng 
20128275SEric Cheng 	/*
20138275SEric Cheng 	 * First unicast address being added, create a new flow
20148275SEric Cheng 	 * for that MAC client.
20158275SEric Cheng 	 */
20168275SEric Cheng 	bzero(&flow_desc, sizeof (flow_desc));
20178275SEric Cheng 
201811878SVenu.Iyer@Sun.COM 	ASSERT(mac_addr != NULL ||
201911878SVenu.Iyer@Sun.COM 	    (mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR));
202011878SVenu.Iyer@Sun.COM 	if (mac_addr != NULL) {
202111878SVenu.Iyer@Sun.COM 		flow_desc.fd_mac_len = mip->mi_type->mt_addr_length;
202211878SVenu.Iyer@Sun.COM 		bcopy(mac_addr, flow_desc.fd_dst_mac, flow_desc.fd_mac_len);
202311878SVenu.Iyer@Sun.COM 	}
20248275SEric Cheng 	flow_desc.fd_mask = FLOW_LINK_DST;
20258275SEric Cheng 	if (vid != 0) {
20268275SEric Cheng 		flow_desc.fd_vid = vid;
20278275SEric Cheng 		flow_desc.fd_mask |= FLOW_LINK_VID;
20288275SEric Cheng 	}
20298275SEric Cheng 
20308275SEric Cheng 	/*
20318275SEric Cheng 	 * XXX-nicolas. For now I'm keeping the FLOW_PRIMARY_MAC
20328275SEric Cheng 	 * and FLOW_VNIC. Even though they're a hack inherited
20338275SEric Cheng 	 * from the SRS code, we'll keep them for now. They're currently
20348275SEric Cheng 	 * consumed by mac_datapath_setup() to create the SRS.
20358275SEric Cheng 	 * That code should be eventually moved out of
20368275SEric Cheng 	 * mac_datapath_setup() and moved to a mac_srs_create()
20378275SEric Cheng 	 * function of some sort to keep things clean.
20388275SEric Cheng 	 *
20398275SEric Cheng 	 * Also, there's no reason why the SRS for the primary MAC
20408275SEric Cheng 	 * client should be different than any other MAC client. Until
20418275SEric Cheng 	 * this is cleaned-up, we support only one MAC unicast address
20428275SEric Cheng 	 * per client.
20438275SEric Cheng 	 *
20448275SEric Cheng 	 * We set FLOW_PRIMARY_MAC for the primary MAC address,
20458275SEric Cheng 	 * FLOW_VNIC for everything else.
20468275SEric Cheng 	 */
20478275SEric Cheng 	if (is_primary)
20488275SEric Cheng 		flent_flags = FLOW_PRIMARY_MAC;
20498275SEric Cheng 	else
20508275SEric Cheng 		flent_flags = FLOW_VNIC_MAC;
20518275SEric Cheng 
20528275SEric Cheng 	/*
20538275SEric Cheng 	 * For the first flow we use the mac client's name - mci_name, for
20548275SEric Cheng 	 * subsequent ones we just create a name with the vid. This is
20558275SEric Cheng 	 * so that we can add these flows to the same flow table. This is
20568275SEric Cheng 	 * fine as the flow name (except for the one with the mac client's
20578275SEric Cheng 	 * name) is not visible. When the first flow is removed, we just replace
20588275SEric Cheng 	 * its fdesc with another from the list, so we will still retain the
20598275SEric Cheng 	 * flent with the MAC client's flow name.
20608275SEric Cheng 	 */
20618275SEric Cheng 	if (first_flow) {
20628558SGirish.Moodalbail@Sun.COM 		bcopy(mcip->mci_name, flowname, MAXFLOWNAMELEN);
20638275SEric Cheng 	} else {
20648275SEric Cheng 		(void) sprintf(flowname, "%s%u", mcip->mci_name, vid);
20658275SEric Cheng 		flent_flags = FLOW_NO_STATS;
20668275SEric Cheng 	}
20678275SEric Cheng 
20688275SEric Cheng 	if ((err = mac_flow_create(&flow_desc, mrp, flowname, NULL,
20698275SEric Cheng 	    flent_flags, flent)) != 0)
20708275SEric Cheng 		return (err);
20718275SEric Cheng 
207211878SVenu.Iyer@Sun.COM 	mac_misc_stat_create(*flent);
20738275SEric Cheng 	FLOW_MARK(*flent, FE_INCIPIENT);
20748275SEric Cheng 	(*flent)->fe_mcip = mcip;
20758275SEric Cheng 
20768275SEric Cheng 	/*
20778275SEric Cheng 	 * Place initial creation reference on the flow. This reference
20788275SEric Cheng 	 * is released in the corresponding delete action viz.
20798275SEric Cheng 	 * mac_unicast_remove after waiting for all transient refs to
20808275SEric Cheng 	 * to go away. The wait happens in mac_flow_wait.
20818275SEric Cheng 	 * We have already held the reference in mac_client_open().
20828275SEric Cheng 	 */
20838275SEric Cheng 	if (!first_flow)
20848275SEric Cheng 		FLOW_REFHOLD(*flent);
20858275SEric Cheng 	return (0);
20868275SEric Cheng }
20878275SEric Cheng 
20888275SEric Cheng /* Refresh the multicast grouping for this VID. */
20898275SEric Cheng int
20908275SEric Cheng mac_client_update_mcast(void *arg, boolean_t add, const uint8_t *addrp)
20918275SEric Cheng {
20928275SEric Cheng 	flow_entry_t		*flent = arg;
20938275SEric Cheng 	mac_client_impl_t	*mcip = flent->fe_mcip;
20948275SEric Cheng 	uint16_t		vid;
20958275SEric Cheng 	flow_desc_t		flow_desc;
20968275SEric Cheng 
20978275SEric Cheng 	mac_flow_get_desc(flent, &flow_desc);
20988275SEric Cheng 	vid = (flow_desc.fd_mask & FLOW_LINK_VID) != 0 ?
20998275SEric Cheng 	    flow_desc.fd_vid : VLAN_ID_NONE;
21008275SEric Cheng 
21018275SEric Cheng 	/*
21028275SEric Cheng 	 * We don't call mac_multicast_add()/mac_multicast_remove() as
21038275SEric Cheng 	 * we want to add/remove for this specific vid.
21048275SEric Cheng 	 */
21058275SEric Cheng 	if (add) {
21068275SEric Cheng 		return (mac_bcast_add(mcip, addrp, vid,
21078275SEric Cheng 		    MAC_ADDRTYPE_MULTICAST));
21088275SEric Cheng 	} else {
21098275SEric Cheng 		mac_bcast_delete(mcip, addrp, vid);
21108275SEric Cheng 		return (0);
21118275SEric Cheng 	}
21128275SEric Cheng }
21138275SEric Cheng 
21148833SVenu.Iyer@Sun.COM static void
21158833SVenu.Iyer@Sun.COM mac_update_single_active_client(mac_impl_t *mip)
21168833SVenu.Iyer@Sun.COM {
21178833SVenu.Iyer@Sun.COM 	mac_client_impl_t *client = NULL;
21188833SVenu.Iyer@Sun.COM 
21198833SVenu.Iyer@Sun.COM 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
21208833SVenu.Iyer@Sun.COM 
21218833SVenu.Iyer@Sun.COM 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
21228833SVenu.Iyer@Sun.COM 	if (mip->mi_nactiveclients == 1) {
21238833SVenu.Iyer@Sun.COM 		/*
21248833SVenu.Iyer@Sun.COM 		 * Find the one active MAC client from the list of MAC
21258833SVenu.Iyer@Sun.COM 		 * clients. The active MAC client has at least one
21268833SVenu.Iyer@Sun.COM 		 * unicast address.
21278833SVenu.Iyer@Sun.COM 		 */
21288833SVenu.Iyer@Sun.COM 		for (client = mip->mi_clients_list; client != NULL;
21298833SVenu.Iyer@Sun.COM 		    client = client->mci_client_next) {
21308833SVenu.Iyer@Sun.COM 			if (client->mci_unicast_list != NULL)
21318833SVenu.Iyer@Sun.COM 				break;
21328833SVenu.Iyer@Sun.COM 		}
21338833SVenu.Iyer@Sun.COM 		ASSERT(client != NULL);
21348833SVenu.Iyer@Sun.COM 	}
21358833SVenu.Iyer@Sun.COM 
21368833SVenu.Iyer@Sun.COM 	/*
21378833SVenu.Iyer@Sun.COM 	 * mi_single_active_client is protected by the MAC impl's read/writer
21388833SVenu.Iyer@Sun.COM 	 * lock, which allows mac_rx() to check the value of that pointer
21398833SVenu.Iyer@Sun.COM 	 * as a reader.
21408833SVenu.Iyer@Sun.COM 	 */
21418833SVenu.Iyer@Sun.COM 	mip->mi_single_active_client = client;
21428833SVenu.Iyer@Sun.COM 	rw_exit(&mip->mi_rw_lock);
21438833SVenu.Iyer@Sun.COM }
21448833SVenu.Iyer@Sun.COM 
21458275SEric Cheng /*
21469473SVenu.Iyer@Sun.COM  * Set up the data path. Called from i_mac_unicast_add after having
21479473SVenu.Iyer@Sun.COM  * done all the validations including making sure this is an active
21489473SVenu.Iyer@Sun.COM  * client (i.e that is ready to process packets.)
21499473SVenu.Iyer@Sun.COM  */
21509473SVenu.Iyer@Sun.COM static int
21519473SVenu.Iyer@Sun.COM mac_client_datapath_setup(mac_client_impl_t *mcip, uint16_t vid,
21529473SVenu.Iyer@Sun.COM     uint8_t *mac_addr, mac_resource_props_t *mrp, boolean_t isprimary,
21539473SVenu.Iyer@Sun.COM     mac_unicast_impl_t *muip)
21549473SVenu.Iyer@Sun.COM {
21559473SVenu.Iyer@Sun.COM 	mac_impl_t	*mip = mcip->mci_mip;
21569473SVenu.Iyer@Sun.COM 	boolean_t	mac_started = B_FALSE;
21579473SVenu.Iyer@Sun.COM 	boolean_t	bcast_added = B_FALSE;
21589473SVenu.Iyer@Sun.COM 	boolean_t	nactiveclients_added = B_FALSE;
21599473SVenu.Iyer@Sun.COM 	flow_entry_t	*flent;
21609473SVenu.Iyer@Sun.COM 	int		err = 0;
216111878SVenu.Iyer@Sun.COM 	boolean_t	no_unicast;
216211878SVenu.Iyer@Sun.COM 
216311878SVenu.Iyer@Sun.COM 	no_unicast = mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR;
21649473SVenu.Iyer@Sun.COM 
21659473SVenu.Iyer@Sun.COM 	if ((err = mac_start((mac_handle_t)mip)) != 0)
21669473SVenu.Iyer@Sun.COM 		goto bail;
21679473SVenu.Iyer@Sun.COM 
21689473SVenu.Iyer@Sun.COM 	mac_started = B_TRUE;
21699473SVenu.Iyer@Sun.COM 
21709473SVenu.Iyer@Sun.COM 	/* add the MAC client to the broadcast address group by default */
21719473SVenu.Iyer@Sun.COM 	if (mip->mi_type->mt_brdcst_addr != NULL) {
21729473SVenu.Iyer@Sun.COM 		err = mac_bcast_add(mcip, mip->mi_type->mt_brdcst_addr, vid,
21739473SVenu.Iyer@Sun.COM 		    MAC_ADDRTYPE_BROADCAST);
21749473SVenu.Iyer@Sun.COM 		if (err != 0)
21759473SVenu.Iyer@Sun.COM 			goto bail;
21769473SVenu.Iyer@Sun.COM 		bcast_added = B_TRUE;
21779473SVenu.Iyer@Sun.COM 	}
21789473SVenu.Iyer@Sun.COM 
21799473SVenu.Iyer@Sun.COM 	/*
21809473SVenu.Iyer@Sun.COM 	 * If this is the first unicast address addition for this
21819473SVenu.Iyer@Sun.COM 	 * client, reuse the pre-allocated larval flow entry associated with
21829473SVenu.Iyer@Sun.COM 	 * the MAC client.
21839473SVenu.Iyer@Sun.COM 	 */
21849473SVenu.Iyer@Sun.COM 	flent = (mcip->mci_nflents == 0) ? mcip->mci_flent : NULL;
21859473SVenu.Iyer@Sun.COM 
21869473SVenu.Iyer@Sun.COM 	/* We are configuring the unicast flow now */
21879473SVenu.Iyer@Sun.COM 	if (!MCIP_DATAPATH_SETUP(mcip)) {
21889473SVenu.Iyer@Sun.COM 
218911878SVenu.Iyer@Sun.COM 		if (mrp != NULL) {
219011878SVenu.Iyer@Sun.COM 			MAC_CLIENT_SET_PRIORITY_RANGE(mcip,
219111878SVenu.Iyer@Sun.COM 			    (mrp->mrp_mask & MRP_PRIORITY) ? mrp->mrp_priority :
219211878SVenu.Iyer@Sun.COM 			    MPL_LINK_DEFAULT);
219311878SVenu.Iyer@Sun.COM 		}
21949473SVenu.Iyer@Sun.COM 		if ((err = mac_unicast_flow_create(mcip, mac_addr, vid,
21959473SVenu.Iyer@Sun.COM 		    isprimary, B_TRUE, &flent, mrp)) != 0)
21969473SVenu.Iyer@Sun.COM 			goto bail;
21979473SVenu.Iyer@Sun.COM 
21989473SVenu.Iyer@Sun.COM 		mip->mi_nactiveclients++;
21999473SVenu.Iyer@Sun.COM 		nactiveclients_added = B_TRUE;
22009473SVenu.Iyer@Sun.COM 
22019473SVenu.Iyer@Sun.COM 		/*
22029473SVenu.Iyer@Sun.COM 		 * This will allocate the RX ring group if possible for the
22039473SVenu.Iyer@Sun.COM 		 * flow and program the software classifier as needed.
22049473SVenu.Iyer@Sun.COM 		 */
22059473SVenu.Iyer@Sun.COM 		if ((err = mac_datapath_setup(mcip, flent, SRST_LINK)) != 0)
22069473SVenu.Iyer@Sun.COM 			goto bail;
22079473SVenu.Iyer@Sun.COM 
220811878SVenu.Iyer@Sun.COM 		if (no_unicast)
220911878SVenu.Iyer@Sun.COM 			goto done_setup;
22109473SVenu.Iyer@Sun.COM 		/*
22119473SVenu.Iyer@Sun.COM 		 * The unicast MAC address must have been added successfully.
22129473SVenu.Iyer@Sun.COM 		 */
22139473SVenu.Iyer@Sun.COM 		ASSERT(mcip->mci_unicast != NULL);
22149473SVenu.Iyer@Sun.COM 		/*
22159473SVenu.Iyer@Sun.COM 		 * Push down the sub-flows that were defined on this link
22169473SVenu.Iyer@Sun.COM 		 * hitherto. The flows are added to the active flow table
22179473SVenu.Iyer@Sun.COM 		 * and SRS, softrings etc. are created as needed.
22189473SVenu.Iyer@Sun.COM 		 */
22199473SVenu.Iyer@Sun.COM 		mac_link_init_flows((mac_client_handle_t)mcip);
22209473SVenu.Iyer@Sun.COM 	} else {
22219473SVenu.Iyer@Sun.COM 		mac_address_t *map = mcip->mci_unicast;
22229473SVenu.Iyer@Sun.COM 
222311878SVenu.Iyer@Sun.COM 		ASSERT(!no_unicast);
22249473SVenu.Iyer@Sun.COM 		/*
22259473SVenu.Iyer@Sun.COM 		 * A unicast flow already exists for that MAC client,
22269473SVenu.Iyer@Sun.COM 		 * this flow must be the same mac address but with
22279473SVenu.Iyer@Sun.COM 		 * different VID. It has been checked by mac_addr_in_use().
22289473SVenu.Iyer@Sun.COM 		 *
22299473SVenu.Iyer@Sun.COM 		 * We will use the SRS etc. from the mci_flent. Note that
22309473SVenu.Iyer@Sun.COM 		 * We don't need to create kstat for this as except for
22319473SVenu.Iyer@Sun.COM 		 * the fdesc, everything will be used from in the 1st flent.
22329473SVenu.Iyer@Sun.COM 		 */
22339473SVenu.Iyer@Sun.COM 
22349473SVenu.Iyer@Sun.COM 		if (bcmp(mac_addr, map->ma_addr, map->ma_len) != 0) {
22359473SVenu.Iyer@Sun.COM 			err = EINVAL;
22369473SVenu.Iyer@Sun.COM 			goto bail;
22379473SVenu.Iyer@Sun.COM 		}
22389473SVenu.Iyer@Sun.COM 
22399473SVenu.Iyer@Sun.COM 		if ((err = mac_unicast_flow_create(mcip, mac_addr, vid,
22409473SVenu.Iyer@Sun.COM 		    isprimary, B_FALSE, &flent, NULL)) != 0) {
22419473SVenu.Iyer@Sun.COM 			goto bail;
22429473SVenu.Iyer@Sun.COM 		}
22439473SVenu.Iyer@Sun.COM 		if ((err = mac_flow_add(mip->mi_flow_tab, flent)) != 0) {
22449473SVenu.Iyer@Sun.COM 			FLOW_FINAL_REFRELE(flent);
22459473SVenu.Iyer@Sun.COM 			goto bail;
22469473SVenu.Iyer@Sun.COM 		}
22479473SVenu.Iyer@Sun.COM 
22489473SVenu.Iyer@Sun.COM 		/* update the multicast group for this vid */
22499473SVenu.Iyer@Sun.COM 		mac_client_bcast_refresh(mcip, mac_client_update_mcast,
22509473SVenu.Iyer@Sun.COM 		    (void *)flent, B_TRUE);
22519473SVenu.Iyer@Sun.COM 
22529473SVenu.Iyer@Sun.COM 	}
22539473SVenu.Iyer@Sun.COM 
22549473SVenu.Iyer@Sun.COM 	/* populate the shared MAC address */
22559473SVenu.Iyer@Sun.COM 	muip->mui_map = mcip->mci_unicast;
22569473SVenu.Iyer@Sun.COM 
22579473SVenu.Iyer@Sun.COM 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
22589473SVenu.Iyer@Sun.COM 	muip->mui_next = mcip->mci_unicast_list;
22599473SVenu.Iyer@Sun.COM 	mcip->mci_unicast_list = muip;
22609473SVenu.Iyer@Sun.COM 	rw_exit(&mcip->mci_rw_lock);
22619473SVenu.Iyer@Sun.COM 
226211878SVenu.Iyer@Sun.COM done_setup:
22639473SVenu.Iyer@Sun.COM 	/*
22649473SVenu.Iyer@Sun.COM 	 * First add the flent to the flow list of this mcip. Then set
22659473SVenu.Iyer@Sun.COM 	 * the mip's mi_single_active_client if needed. The Rx path assumes
22669473SVenu.Iyer@Sun.COM 	 * that mip->mi_single_active_client will always have an associated
22679473SVenu.Iyer@Sun.COM 	 * flent.
22689473SVenu.Iyer@Sun.COM 	 */
22699473SVenu.Iyer@Sun.COM 	mac_client_add_to_flow_list(mcip, flent);
22709473SVenu.Iyer@Sun.COM 	if (nactiveclients_added)
22719473SVenu.Iyer@Sun.COM 		mac_update_single_active_client(mip);
22729473SVenu.Iyer@Sun.COM 	/*
22739473SVenu.Iyer@Sun.COM 	 * Trigger a renegotiation of the capabilities when the number of
22749473SVenu.Iyer@Sun.COM 	 * active clients changes from 1 to 2, since some of the capabilities
22759473SVenu.Iyer@Sun.COM 	 * might have to be disabled. Also send a MAC_NOTE_LINK notification
22769473SVenu.Iyer@Sun.COM 	 * to all the MAC clients whenever physical link is DOWN.
22779473SVenu.Iyer@Sun.COM 	 */
22789473SVenu.Iyer@Sun.COM 	if (mip->mi_nactiveclients == 2) {
22799473SVenu.Iyer@Sun.COM 		mac_capab_update((mac_handle_t)mip);
22809473SVenu.Iyer@Sun.COM 		mac_virtual_link_update(mip);
22819473SVenu.Iyer@Sun.COM 	}
22829473SVenu.Iyer@Sun.COM 	/*
22839473SVenu.Iyer@Sun.COM 	 * Now that the setup is complete, clear the INCIPIENT flag.
22849473SVenu.Iyer@Sun.COM 	 * The flag was set to avoid incoming packets seeing inconsistent
22859473SVenu.Iyer@Sun.COM 	 * structures while the setup was in progress. Clear the mci_tx_flag
22869473SVenu.Iyer@Sun.COM 	 * by calling mac_tx_client_block. It is possible that
22879473SVenu.Iyer@Sun.COM 	 * mac_unicast_remove was called prior to this mac_unicast_add which
22889473SVenu.Iyer@Sun.COM 	 * could have set the MCI_TX_QUIESCE flag.
22899473SVenu.Iyer@Sun.COM 	 */
22909473SVenu.Iyer@Sun.COM 	if (flent->fe_rx_ring_group != NULL)
22919473SVenu.Iyer@Sun.COM 		mac_rx_group_unmark(flent->fe_rx_ring_group, MR_INCIPIENT);
22929473SVenu.Iyer@Sun.COM 	FLOW_UNMARK(flent, FE_INCIPIENT);
22939473SVenu.Iyer@Sun.COM 	FLOW_UNMARK(flent, FE_MC_NO_DATAPATH);
22949473SVenu.Iyer@Sun.COM 	mac_tx_client_unblock(mcip);
22959473SVenu.Iyer@Sun.COM 	return (0);
22969473SVenu.Iyer@Sun.COM bail:
22979473SVenu.Iyer@Sun.COM 	if (bcast_added)
22989473SVenu.Iyer@Sun.COM 		mac_bcast_delete(mcip, mip->mi_type->mt_brdcst_addr, vid);
22999473SVenu.Iyer@Sun.COM 
23009473SVenu.Iyer@Sun.COM 	if (nactiveclients_added)
23019473SVenu.Iyer@Sun.COM 		mip->mi_nactiveclients--;
23029473SVenu.Iyer@Sun.COM 
230310013SNitin.Hande@Sun.COM 	if (mac_started)
230410013SNitin.Hande@Sun.COM 		mac_stop((mac_handle_t)mip);
230510013SNitin.Hande@Sun.COM 
23069473SVenu.Iyer@Sun.COM 	return (err);
23079473SVenu.Iyer@Sun.COM }
23089473SVenu.Iyer@Sun.COM 
23099473SVenu.Iyer@Sun.COM /*
23109473SVenu.Iyer@Sun.COM  * Return the passive primary MAC client, if present. The passive client is
23119473SVenu.Iyer@Sun.COM  * a stand-by client that has the same unicast address as another that is
23129473SVenu.Iyer@Sun.COM  * currenly active. Once the active client goes away, the passive client
23139473SVenu.Iyer@Sun.COM  * becomes active.
23149473SVenu.Iyer@Sun.COM  */
23159473SVenu.Iyer@Sun.COM static mac_client_impl_t *
23169473SVenu.Iyer@Sun.COM mac_get_passive_primary_client(mac_impl_t *mip)
23179473SVenu.Iyer@Sun.COM {
23189473SVenu.Iyer@Sun.COM 	mac_client_impl_t	*mcip;
23199473SVenu.Iyer@Sun.COM 
23209473SVenu.Iyer@Sun.COM 	for (mcip = mip->mi_clients_list; mcip != NULL;
23219473SVenu.Iyer@Sun.COM 	    mcip = mcip->mci_client_next) {
23229473SVenu.Iyer@Sun.COM 		if (mac_is_primary_client(mcip) &&
23239473SVenu.Iyer@Sun.COM 		    (mcip->mci_flags & MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) != 0) {
23249473SVenu.Iyer@Sun.COM 			return (mcip);
23259473SVenu.Iyer@Sun.COM 		}
23269473SVenu.Iyer@Sun.COM 	}
23279473SVenu.Iyer@Sun.COM 	return (NULL);
23289473SVenu.Iyer@Sun.COM }
23299473SVenu.Iyer@Sun.COM 
23309473SVenu.Iyer@Sun.COM /*
23318275SEric Cheng  * Add a new unicast address to the MAC client.
23328275SEric Cheng  *
23338275SEric Cheng  * The MAC address can be specified either by value, or the MAC client
23348275SEric Cheng  * can specify that it wants to use the primary MAC address of the
23358275SEric Cheng  * underlying MAC. See the introductory comments at the beginning
23368275SEric Cheng  * of this file for more more information on primary MAC addresses.
23378275SEric Cheng  *
23388275SEric Cheng  * Note also the tuple (MAC address, VID) must be unique
23398275SEric Cheng  * for the MAC clients defined on top of the same underlying MAC
23408275SEric Cheng  * instance, unless the MAC_UNICAST_NODUPCHECK is specified.
234110491SRishi.Srivatsavai@Sun.COM  *
234210491SRishi.Srivatsavai@Sun.COM  * In no case can a client use the PVID for the MAC, if the MAC has one set.
23438275SEric Cheng  */
23448275SEric Cheng int
23458275SEric Cheng i_mac_unicast_add(mac_client_handle_t mch, uint8_t *mac_addr, uint16_t flags,
23468275SEric Cheng     mac_unicast_handle_t *mah, uint16_t vid, mac_diag_t *diag)
23478275SEric Cheng {
23489473SVenu.Iyer@Sun.COM 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
23499473SVenu.Iyer@Sun.COM 	mac_impl_t		*mip = mcip->mci_mip;
23509473SVenu.Iyer@Sun.COM 	int			err;
23519473SVenu.Iyer@Sun.COM 	uint_t			mac_len = mip->mi_type->mt_addr_length;
23529473SVenu.Iyer@Sun.COM 	boolean_t		check_dups = !(flags & MAC_UNICAST_NODUPCHECK);
23539473SVenu.Iyer@Sun.COM 	boolean_t		fastpath_disabled = B_FALSE;
23549473SVenu.Iyer@Sun.COM 	boolean_t		is_primary = (flags & MAC_UNICAST_PRIMARY);
23559473SVenu.Iyer@Sun.COM 	boolean_t		is_unicast_hw = (flags & MAC_UNICAST_HW);
235611878SVenu.Iyer@Sun.COM 	mac_resource_props_t	*mrp;
23579473SVenu.Iyer@Sun.COM 	boolean_t		passive_client = B_FALSE;
23589473SVenu.Iyer@Sun.COM 	mac_unicast_impl_t	*muip;
23599473SVenu.Iyer@Sun.COM 	boolean_t		is_vnic_primary =
23609473SVenu.Iyer@Sun.COM 	    (flags & MAC_UNICAST_VNIC_PRIMARY);
23618275SEric Cheng 
23628275SEric Cheng 	/* when VID is non-zero, the underlying MAC can not be VNIC */
23638275SEric Cheng 	ASSERT(!((mip->mi_state_flags & MIS_IS_VNIC) && (vid != 0)));
23648275SEric Cheng 
23658275SEric Cheng 	/*
236611878SVenu.Iyer@Sun.COM 	 * Can't unicast add if the client asked only for minimal datapath
236711878SVenu.Iyer@Sun.COM 	 * setup.
236811878SVenu.Iyer@Sun.COM 	 */
236911878SVenu.Iyer@Sun.COM 	if (mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR)
237011878SVenu.Iyer@Sun.COM 		return (ENOTSUP);
237111878SVenu.Iyer@Sun.COM 
237211878SVenu.Iyer@Sun.COM 	/*
237310491SRishi.Srivatsavai@Sun.COM 	 * Check for an attempted use of the current Port VLAN ID, if enabled.
237410491SRishi.Srivatsavai@Sun.COM 	 * No client may use it.
237510491SRishi.Srivatsavai@Sun.COM 	 */
237610491SRishi.Srivatsavai@Sun.COM 	if (mip->mi_pvid != 0 && vid == mip->mi_pvid)
237710491SRishi.Srivatsavai@Sun.COM 		return (EBUSY);
237810491SRishi.Srivatsavai@Sun.COM 
237910491SRishi.Srivatsavai@Sun.COM 	/*
23808275SEric Cheng 	 * Check whether it's the primary client and flag it.
23818275SEric Cheng 	 */
23828275SEric Cheng 	if (!(mcip->mci_state_flags & MCIS_IS_VNIC) && is_primary && vid == 0)
23838275SEric Cheng 		mcip->mci_flags |= MAC_CLIENT_FLAGS_PRIMARY;
23848275SEric Cheng 
23858275SEric Cheng 	/*
23868275SEric Cheng 	 * is_vnic_primary is true when we come here as a VLAN VNIC
23878275SEric Cheng 	 * which uses the primary mac client's address but with a non-zero
23888275SEric Cheng 	 * VID. In this case the MAC address is not specified by an upper
23898275SEric Cheng 	 * MAC client.
23908275SEric Cheng 	 */
23918275SEric Cheng 	if ((mcip->mci_state_flags & MCIS_IS_VNIC) && is_primary &&
23928275SEric Cheng 	    !is_vnic_primary) {
23938275SEric Cheng 		/*
23948275SEric Cheng 		 * The address is being set by the upper MAC client
23958275SEric Cheng 		 * of a VNIC. The MAC address was already set by the
23968275SEric Cheng 		 * VNIC driver during VNIC creation.
23978275SEric Cheng 		 *
23988275SEric Cheng 		 * Note: a VNIC has only one MAC address. We return
23998275SEric Cheng 		 * the MAC unicast address handle of the lower MAC client
24008275SEric Cheng 		 * corresponding to the VNIC. We allocate a new entry
24018275SEric Cheng 		 * which is flagged appropriately, so that mac_unicast_remove()
24028275SEric Cheng 		 * doesn't attempt to free the original entry that
24038275SEric Cheng 		 * was allocated by the VNIC driver.
24048275SEric Cheng 		 */
24058275SEric Cheng 		ASSERT(mcip->mci_unicast != NULL);
24068275SEric Cheng 
24079024SVenu.Iyer@Sun.COM 		/* Check for VLAN flags, if present */
24089024SVenu.Iyer@Sun.COM 		if ((flags & MAC_UNICAST_TAG_DISABLE) != 0)
24099024SVenu.Iyer@Sun.COM 			mcip->mci_state_flags |= MCIS_TAG_DISABLE;
24109024SVenu.Iyer@Sun.COM 
24119024SVenu.Iyer@Sun.COM 		if ((flags & MAC_UNICAST_STRIP_DISABLE) != 0)
24129024SVenu.Iyer@Sun.COM 			mcip->mci_state_flags |= MCIS_STRIP_DISABLE;
24139024SVenu.Iyer@Sun.COM 
24149024SVenu.Iyer@Sun.COM 		if ((flags & MAC_UNICAST_DISABLE_TX_VID_CHECK) != 0)
24159024SVenu.Iyer@Sun.COM 			mcip->mci_state_flags |= MCIS_DISABLE_TX_VID_CHECK;
24169024SVenu.Iyer@Sun.COM 
24178275SEric Cheng 		/*
24188275SEric Cheng 		 * Ensure that the primary unicast address of the VNIC
24199473SVenu.Iyer@Sun.COM 		 * is added only once unless we have the
24209473SVenu.Iyer@Sun.COM 		 * MAC_CLIENT_FLAGS_MULTI_PRIMARY set (and this is not
24219473SVenu.Iyer@Sun.COM 		 * a passive MAC client).
24228275SEric Cheng 		 */
24239473SVenu.Iyer@Sun.COM 		if ((mcip->mci_flags & MAC_CLIENT_FLAGS_VNIC_PRIMARY) != 0) {
24249473SVenu.Iyer@Sun.COM 			if ((mcip->mci_flags &
24259473SVenu.Iyer@Sun.COM 			    MAC_CLIENT_FLAGS_MULTI_PRIMARY) == 0 ||
24269473SVenu.Iyer@Sun.COM 			    (mcip->mci_flags &
24279473SVenu.Iyer@Sun.COM 			    MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) != 0) {
24289473SVenu.Iyer@Sun.COM 				return (EBUSY);
24299473SVenu.Iyer@Sun.COM 			}
24309473SVenu.Iyer@Sun.COM 			mcip->mci_flags |= MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
24319473SVenu.Iyer@Sun.COM 			passive_client = B_TRUE;
24329473SVenu.Iyer@Sun.COM 		}
24338275SEric Cheng 
24348275SEric Cheng 		mcip->mci_flags |= MAC_CLIENT_FLAGS_VNIC_PRIMARY;
24358275SEric Cheng 
24368275SEric Cheng 		/*
24378275SEric Cheng 		 * Create a handle for vid 0.
24388275SEric Cheng 		 */
24398275SEric Cheng 		ASSERT(vid == 0);
24408275SEric Cheng 		muip = kmem_zalloc(sizeof (mac_unicast_impl_t), KM_SLEEP);
24418275SEric Cheng 		muip->mui_vid = vid;
24428275SEric Cheng 		*mah = (mac_unicast_handle_t)muip;
24439473SVenu.Iyer@Sun.COM 		/*
24449473SVenu.Iyer@Sun.COM 		 * This will be used by the caller to defer setting the
24459473SVenu.Iyer@Sun.COM 		 * rx functions.
24469473SVenu.Iyer@Sun.COM 		 */
24479473SVenu.Iyer@Sun.COM 		if (passive_client)
24489473SVenu.Iyer@Sun.COM 			return (EAGAIN);
24498275SEric Cheng 		return (0);
24508275SEric Cheng 	}
24518275SEric Cheng 
24528275SEric Cheng 	/* primary MAC clients cannot be opened on top of anchor VNICs */
24538275SEric Cheng 	if ((is_vnic_primary || is_primary) &&
24548275SEric Cheng 	    i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_ANCHOR_VNIC, NULL)) {
24558275SEric Cheng 		return (ENXIO);
24568275SEric Cheng 	}
24578275SEric Cheng 
24588275SEric Cheng 	/*
24599073SCathy.Zhou@Sun.COM 	 * If this is a VNIC/VLAN, disable softmac fast-path.
24609073SCathy.Zhou@Sun.COM 	 */
24619073SCathy.Zhou@Sun.COM 	if (mcip->mci_state_flags & MCIS_IS_VNIC) {
24629073SCathy.Zhou@Sun.COM 		err = mac_fastpath_disable((mac_handle_t)mip);
24639073SCathy.Zhou@Sun.COM 		if (err != 0)
24649073SCathy.Zhou@Sun.COM 			return (err);
24659073SCathy.Zhou@Sun.COM 		fastpath_disabled = B_TRUE;
24669073SCathy.Zhou@Sun.COM 	}
24679073SCathy.Zhou@Sun.COM 
24689073SCathy.Zhou@Sun.COM 	/*
24698275SEric Cheng 	 * Return EBUSY if:
24709073SCathy.Zhou@Sun.COM 	 *  - there is an exclusively active mac client exists.
24719073SCathy.Zhou@Sun.COM 	 *  - this is an exclusive active mac client but
24729073SCathy.Zhou@Sun.COM 	 *	a. there is already active mac clients exist, or
24739073SCathy.Zhou@Sun.COM 	 *	b. fastpath streams are already plumbed on this legacy device
247410491SRishi.Srivatsavai@Sun.COM 	 *  - the mac creator has disallowed active mac clients.
24758275SEric Cheng 	 */
247610491SRishi.Srivatsavai@Sun.COM 	if (mip->mi_state_flags & (MIS_EXCLUSIVE|MIS_NO_ACTIVE)) {
24779073SCathy.Zhou@Sun.COM 		if (fastpath_disabled)
24789073SCathy.Zhou@Sun.COM 			mac_fastpath_enable((mac_handle_t)mip);
24798275SEric Cheng 		return (EBUSY);
24808275SEric Cheng 	}
24818275SEric Cheng 
24829073SCathy.Zhou@Sun.COM 	if (mcip->mci_state_flags & MCIS_EXCLUSIVE) {
24839073SCathy.Zhou@Sun.COM 		ASSERT(!fastpath_disabled);
24849073SCathy.Zhou@Sun.COM 		if (mip->mi_nactiveclients != 0)
24859073SCathy.Zhou@Sun.COM 			return (EBUSY);
24869073SCathy.Zhou@Sun.COM 
24879073SCathy.Zhou@Sun.COM 		if ((mip->mi_state_flags & MIS_LEGACY) &&
24889073SCathy.Zhou@Sun.COM 		    !(mip->mi_capab_legacy.ml_active_set(mip->mi_driver))) {
24899073SCathy.Zhou@Sun.COM 			return (EBUSY);
24909073SCathy.Zhou@Sun.COM 		}
24918275SEric Cheng 		mip->mi_state_flags |= MIS_EXCLUSIVE;
24929073SCathy.Zhou@Sun.COM 	}
24938275SEric Cheng 
249411878SVenu.Iyer@Sun.COM 	mrp = kmem_zalloc(sizeof (*mrp), KM_SLEEP);
24958833SVenu.Iyer@Sun.COM 	if (is_primary && !(mcip->mci_state_flags & (MCIS_IS_VNIC |
24968833SVenu.Iyer@Sun.COM 	    MCIS_IS_AGGR_PORT))) {
24978275SEric Cheng 		/*
24988275SEric Cheng 		 * Apply the property cached in the mac_impl_t to the primary
24998833SVenu.Iyer@Sun.COM 		 * mac client. If the mac client is a VNIC or an aggregation
25008833SVenu.Iyer@Sun.COM 		 * port, its property should be set in the mcip when the
25018833SVenu.Iyer@Sun.COM 		 * VNIC/aggr was created.
25028275SEric Cheng 		 */
250311878SVenu.Iyer@Sun.COM 		mac_get_resources((mac_handle_t)mip, mrp);
250411878SVenu.Iyer@Sun.COM 		(void) mac_client_set_resources(mch, mrp);
25058275SEric Cheng 	} else if (mcip->mci_state_flags & MCIS_IS_VNIC) {
250611878SVenu.Iyer@Sun.COM 		/*
250711878SVenu.Iyer@Sun.COM 		 * This is a primary VLAN client, we don't support
250811878SVenu.Iyer@Sun.COM 		 * specifying rings property for this as it inherits the
250911878SVenu.Iyer@Sun.COM 		 * rings property from its MAC.
251011878SVenu.Iyer@Sun.COM 		 */
251111878SVenu.Iyer@Sun.COM 		if (is_vnic_primary) {
251211878SVenu.Iyer@Sun.COM 			mac_resource_props_t	*vmrp;
251311878SVenu.Iyer@Sun.COM 
251411878SVenu.Iyer@Sun.COM 			vmrp = MCIP_RESOURCE_PROPS(mcip);
251511878SVenu.Iyer@Sun.COM 			if (vmrp->mrp_mask & MRP_RX_RINGS ||
251611878SVenu.Iyer@Sun.COM 			    vmrp->mrp_mask & MRP_TX_RINGS) {
251711878SVenu.Iyer@Sun.COM 				if (fastpath_disabled)
251811878SVenu.Iyer@Sun.COM 					mac_fastpath_enable((mac_handle_t)mip);
251911878SVenu.Iyer@Sun.COM 				kmem_free(mrp, sizeof (*mrp));
252011878SVenu.Iyer@Sun.COM 				return (ENOTSUP);
252111878SVenu.Iyer@Sun.COM 			}
252211878SVenu.Iyer@Sun.COM 			/*
252311878SVenu.Iyer@Sun.COM 			 * Additionally we also need to inherit any
252411878SVenu.Iyer@Sun.COM 			 * rings property from the MAC.
252511878SVenu.Iyer@Sun.COM 			 */
252611878SVenu.Iyer@Sun.COM 			mac_get_resources((mac_handle_t)mip, mrp);
252711878SVenu.Iyer@Sun.COM 			if (mrp->mrp_mask & MRP_RX_RINGS) {
252811878SVenu.Iyer@Sun.COM 				vmrp->mrp_mask |= MRP_RX_RINGS;
252911878SVenu.Iyer@Sun.COM 				vmrp->mrp_nrxrings = mrp->mrp_nrxrings;
253011878SVenu.Iyer@Sun.COM 			}
253111878SVenu.Iyer@Sun.COM 			if (mrp->mrp_mask & MRP_TX_RINGS) {
253211878SVenu.Iyer@Sun.COM 				vmrp->mrp_mask |= MRP_TX_RINGS;
253311878SVenu.Iyer@Sun.COM 				vmrp->mrp_ntxrings = mrp->mrp_ntxrings;
253411878SVenu.Iyer@Sun.COM 			}
253511878SVenu.Iyer@Sun.COM 		}
253611878SVenu.Iyer@Sun.COM 		bcopy(MCIP_RESOURCE_PROPS(mcip), mrp, sizeof (*mrp));
25378275SEric Cheng 	}
25388275SEric Cheng 
25398275SEric Cheng 	muip = kmem_zalloc(sizeof (mac_unicast_impl_t), KM_SLEEP);
25408275SEric Cheng 	muip->mui_vid = vid;
25418275SEric Cheng 
25428275SEric Cheng 	if (is_primary || is_vnic_primary) {
25438275SEric Cheng 		mac_addr = mip->mi_addr;
25448275SEric Cheng 	} else {
25458275SEric Cheng 
25468275SEric Cheng 		/*
25478275SEric Cheng 		 * Verify the validity of the specified MAC addresses value.
25488275SEric Cheng 		 */
25498275SEric Cheng 		if (!mac_unicst_verify((mac_handle_t)mip, mac_addr, mac_len)) {
25508275SEric Cheng 			*diag = MAC_DIAG_MACADDR_INVALID;
25518275SEric Cheng 			err = EINVAL;
25529473SVenu.Iyer@Sun.COM 			goto bail_out;
25538275SEric Cheng 		}
25548275SEric Cheng 
25558275SEric Cheng 		/*
25568275SEric Cheng 		 * Make sure that the specified MAC address is different
25578275SEric Cheng 		 * than the unicast MAC address of the underlying NIC.
25588275SEric Cheng 		 */
25598275SEric Cheng 		if (check_dups && bcmp(mip->mi_addr, mac_addr, mac_len) == 0) {
25608275SEric Cheng 			*diag = MAC_DIAG_MACADDR_NIC;
25618275SEric Cheng 			err = EINVAL;
25629473SVenu.Iyer@Sun.COM 			goto bail_out;
25638275SEric Cheng 		}
25648275SEric Cheng 	}
25658275SEric Cheng 
25668275SEric Cheng 	/*
25679473SVenu.Iyer@Sun.COM 	 * Set the flags here so that if this is a passive client, we
25689473SVenu.Iyer@Sun.COM 	 * can return  and set it when we call mac_client_datapath_setup
25699473SVenu.Iyer@Sun.COM 	 * when this becomes the active client. If we defer to using these
25709473SVenu.Iyer@Sun.COM 	 * flags to mac_client_datapath_setup, then for a passive client,
25719473SVenu.Iyer@Sun.COM 	 * we'd have to store the flags somewhere (probably fe_flags)
25729473SVenu.Iyer@Sun.COM 	 * and then use it.
25738275SEric Cheng 	 */
25748275SEric Cheng 	if (!MCIP_DATAPATH_SETUP(mcip)) {
25758400SNicolas.Droux@Sun.COM 		if (is_unicast_hw) {
25768400SNicolas.Droux@Sun.COM 			/*
25778400SNicolas.Droux@Sun.COM 			 * The client requires a hardware MAC address slot
25788400SNicolas.Droux@Sun.COM 			 * for that unicast address. Since we support only
25798400SNicolas.Droux@Sun.COM 			 * one unicast MAC address per client, flag the
25808400SNicolas.Droux@Sun.COM 			 * MAC client itself.
25818400SNicolas.Droux@Sun.COM 			 */
25828400SNicolas.Droux@Sun.COM 			mcip->mci_state_flags |= MCIS_UNICAST_HW;
25838400SNicolas.Droux@Sun.COM 		}
25848275SEric Cheng 
25859024SVenu.Iyer@Sun.COM 		/* Check for VLAN flags, if present */
25869024SVenu.Iyer@Sun.COM 		if ((flags & MAC_UNICAST_TAG_DISABLE) != 0)
25879024SVenu.Iyer@Sun.COM 			mcip->mci_state_flags |= MCIS_TAG_DISABLE;
25889024SVenu.Iyer@Sun.COM 
25899024SVenu.Iyer@Sun.COM 		if ((flags & MAC_UNICAST_STRIP_DISABLE) != 0)
25909024SVenu.Iyer@Sun.COM 			mcip->mci_state_flags |= MCIS_STRIP_DISABLE;
25919024SVenu.Iyer@Sun.COM 
25929024SVenu.Iyer@Sun.COM 		if ((flags & MAC_UNICAST_DISABLE_TX_VID_CHECK) != 0)
25939024SVenu.Iyer@Sun.COM 			mcip->mci_state_flags |= MCIS_DISABLE_TX_VID_CHECK;
25948275SEric Cheng 	} else {
25959024SVenu.Iyer@Sun.COM 		/*
25969024SVenu.Iyer@Sun.COM 		 * Assert that the specified flags are consistent with the
25979024SVenu.Iyer@Sun.COM 		 * flags specified by previous calls to mac_unicast_add().
25989024SVenu.Iyer@Sun.COM 		 */
25999024SVenu.Iyer@Sun.COM 		ASSERT(((flags & MAC_UNICAST_TAG_DISABLE) != 0 &&
26009024SVenu.Iyer@Sun.COM 		    (mcip->mci_state_flags & MCIS_TAG_DISABLE) != 0) ||
26019024SVenu.Iyer@Sun.COM 		    ((flags & MAC_UNICAST_TAG_DISABLE) == 0 &&
26029024SVenu.Iyer@Sun.COM 		    (mcip->mci_state_flags & MCIS_TAG_DISABLE) == 0));
26039024SVenu.Iyer@Sun.COM 
26049024SVenu.Iyer@Sun.COM 		ASSERT(((flags & MAC_UNICAST_STRIP_DISABLE) != 0 &&
26059024SVenu.Iyer@Sun.COM 		    (mcip->mci_state_flags & MCIS_STRIP_DISABLE) != 0) ||
26069024SVenu.Iyer@Sun.COM 		    ((flags & MAC_UNICAST_STRIP_DISABLE) == 0 &&
26079024SVenu.Iyer@Sun.COM 		    (mcip->mci_state_flags & MCIS_STRIP_DISABLE) == 0));
26089024SVenu.Iyer@Sun.COM 
26099024SVenu.Iyer@Sun.COM 		ASSERT(((flags & MAC_UNICAST_DISABLE_TX_VID_CHECK) != 0 &&
26109024SVenu.Iyer@Sun.COM 		    (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK) != 0) ||
26119024SVenu.Iyer@Sun.COM 		    ((flags & MAC_UNICAST_DISABLE_TX_VID_CHECK) == 0 &&
26129024SVenu.Iyer@Sun.COM 		    (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK) == 0));
26139024SVenu.Iyer@Sun.COM 
26148400SNicolas.Droux@Sun.COM 		/*
26158400SNicolas.Droux@Sun.COM 		 * Make sure the client is consistent about its requests
26168400SNicolas.Droux@Sun.COM 		 * for MAC addresses. I.e. all requests from the clients
26178400SNicolas.Droux@Sun.COM 		 * must have the MAC_UNICAST_HW flag set or clear.
26188400SNicolas.Droux@Sun.COM 		 */
26198400SNicolas.Droux@Sun.COM 		if ((mcip->mci_state_flags & MCIS_UNICAST_HW) != 0 &&
26208400SNicolas.Droux@Sun.COM 		    !is_unicast_hw ||
26218400SNicolas.Droux@Sun.COM 		    (mcip->mci_state_flags & MCIS_UNICAST_HW) == 0 &&
26228400SNicolas.Droux@Sun.COM 		    is_unicast_hw) {
26238400SNicolas.Droux@Sun.COM 			err = EINVAL;
26249473SVenu.Iyer@Sun.COM 			goto bail_out;
26258275SEric Cheng 		}
26268275SEric Cheng 	}
26278275SEric Cheng 	/*
26289473SVenu.Iyer@Sun.COM 	 * Make sure the MAC address is not already used by
26299473SVenu.Iyer@Sun.COM 	 * another MAC client defined on top of the same
26309473SVenu.Iyer@Sun.COM 	 * underlying NIC. Unless we have MAC_CLIENT_FLAGS_MULTI_PRIMARY
26319473SVenu.Iyer@Sun.COM 	 * set when we allow a passive client to be present which will
26329473SVenu.Iyer@Sun.COM 	 * be activated when the currently active client goes away - this
26339473SVenu.Iyer@Sun.COM 	 * works only with primary addresses.
26348275SEric Cheng 	 */
26359473SVenu.Iyer@Sun.COM 	if ((check_dups || is_primary || is_vnic_primary) &&
26369473SVenu.Iyer@Sun.COM 	    mac_addr_in_use(mip, mac_addr, vid)) {
26379473SVenu.Iyer@Sun.COM 		/*
26389473SVenu.Iyer@Sun.COM 		 * Must have set the multiple primary address flag when
26399473SVenu.Iyer@Sun.COM 		 * we did a mac_client_open AND this should be a primary
26409473SVenu.Iyer@Sun.COM 		 * MAC client AND there should not already be a passive
26419473SVenu.Iyer@Sun.COM 		 * primary. If all is true then we let this succeed
26429473SVenu.Iyer@Sun.COM 		 * even if the address is a dup.
26439473SVenu.Iyer@Sun.COM 		 */
26449473SVenu.Iyer@Sun.COM 		if ((mcip->mci_flags & MAC_CLIENT_FLAGS_MULTI_PRIMARY) == 0 ||
26459473SVenu.Iyer@Sun.COM 		    (mcip->mci_flags & MAC_CLIENT_FLAGS_PRIMARY) == 0 ||
26469473SVenu.Iyer@Sun.COM 		    mac_get_passive_primary_client(mip) != NULL) {
26479473SVenu.Iyer@Sun.COM 			*diag = MAC_DIAG_MACADDR_INUSE;
26489473SVenu.Iyer@Sun.COM 			err = EEXIST;
26499473SVenu.Iyer@Sun.COM 			goto bail_out;
26509473SVenu.Iyer@Sun.COM 		}
26519473SVenu.Iyer@Sun.COM 		ASSERT((mcip->mci_flags &
26529473SVenu.Iyer@Sun.COM 		    MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) == 0);
26539473SVenu.Iyer@Sun.COM 		mcip->mci_flags |= MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
265411878SVenu.Iyer@Sun.COM 		kmem_free(mrp, sizeof (*mrp));
26559473SVenu.Iyer@Sun.COM 
26569473SVenu.Iyer@Sun.COM 		/*
26579473SVenu.Iyer@Sun.COM 		 * Stash the unicast address handle, we will use it when
26589473SVenu.Iyer@Sun.COM 		 * we set up the passive client.
26599473SVenu.Iyer@Sun.COM 		 */
26609473SVenu.Iyer@Sun.COM 		mcip->mci_p_unicast_list = muip;
26619473SVenu.Iyer@Sun.COM 		*mah = (mac_unicast_handle_t)muip;
26629473SVenu.Iyer@Sun.COM 		return (0);
26639473SVenu.Iyer@Sun.COM 	}
26649473SVenu.Iyer@Sun.COM 
266511878SVenu.Iyer@Sun.COM 	err = mac_client_datapath_setup(mcip, vid, mac_addr, mrp,
26669473SVenu.Iyer@Sun.COM 	    is_primary || is_vnic_primary, muip);
26679473SVenu.Iyer@Sun.COM 	if (err != 0)
26689473SVenu.Iyer@Sun.COM 		goto bail_out;
266911878SVenu.Iyer@Sun.COM 
267011878SVenu.Iyer@Sun.COM 	kmem_free(mrp, sizeof (*mrp));
26719473SVenu.Iyer@Sun.COM 	*mah = (mac_unicast_handle_t)muip;
26728275SEric Cheng 	return (0);
26739473SVenu.Iyer@Sun.COM 
26749473SVenu.Iyer@Sun.COM bail_out:
26759473SVenu.Iyer@Sun.COM 	if (fastpath_disabled)
26769473SVenu.Iyer@Sun.COM 		mac_fastpath_enable((mac_handle_t)mip);
26779073SCathy.Zhou@Sun.COM 	if (mcip->mci_state_flags & MCIS_EXCLUSIVE) {
26788275SEric Cheng 		mip->mi_state_flags &= ~MIS_EXCLUSIVE;
26799473SVenu.Iyer@Sun.COM 		if (mip->mi_state_flags & MIS_LEGACY) {
26809473SVenu.Iyer@Sun.COM 			mip->mi_capab_legacy.ml_active_clear(
26819473SVenu.Iyer@Sun.COM 			    mip->mi_driver);
26829473SVenu.Iyer@Sun.COM 		}
26839073SCathy.Zhou@Sun.COM 	}
268411878SVenu.Iyer@Sun.COM 	kmem_free(mrp, sizeof (*mrp));
26858275SEric Cheng 	kmem_free(muip, sizeof (mac_unicast_impl_t));
26868275SEric Cheng 	return (err);
26878275SEric Cheng }
26888275SEric Cheng 
26899473SVenu.Iyer@Sun.COM /*
26909473SVenu.Iyer@Sun.COM  * Wrapper function to mac_unicast_add when we want to have the same mac
26919473SVenu.Iyer@Sun.COM  * client open for two instances, one that is currently active and another
26929473SVenu.Iyer@Sun.COM  * that will become active when the current one is removed. In this case
26939473SVenu.Iyer@Sun.COM  * mac_unicast_add will return EGAIN and we will save the rx function and
26949473SVenu.Iyer@Sun.COM  * arg which will be used when we activate the passive client in
26959473SVenu.Iyer@Sun.COM  * mac_unicast_remove.
26969473SVenu.Iyer@Sun.COM  */
26979473SVenu.Iyer@Sun.COM int
26989473SVenu.Iyer@Sun.COM mac_unicast_add_set_rx(mac_client_handle_t mch, uint8_t *mac_addr,
26999473SVenu.Iyer@Sun.COM     uint16_t flags, mac_unicast_handle_t *mah,  uint16_t vid, mac_diag_t *diag,
27009473SVenu.Iyer@Sun.COM     mac_rx_t rx_fn, void *arg)
27019473SVenu.Iyer@Sun.COM {
27029473SVenu.Iyer@Sun.COM 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
27039473SVenu.Iyer@Sun.COM 	uint_t			err;
27049473SVenu.Iyer@Sun.COM 
27059473SVenu.Iyer@Sun.COM 	err = mac_unicast_add(mch, mac_addr, flags, mah, vid, diag);
27069473SVenu.Iyer@Sun.COM 	if (err != 0 && err != EAGAIN)
27079473SVenu.Iyer@Sun.COM 		return (err);
27089473SVenu.Iyer@Sun.COM 	if (err == EAGAIN) {
27099473SVenu.Iyer@Sun.COM 		if (rx_fn != NULL) {
27109473SVenu.Iyer@Sun.COM 			mcip->mci_rx_p_fn = rx_fn;
27119473SVenu.Iyer@Sun.COM 			mcip->mci_rx_p_arg = arg;
27129473SVenu.Iyer@Sun.COM 		}
27139473SVenu.Iyer@Sun.COM 		return (0);
27149473SVenu.Iyer@Sun.COM 	}
27159473SVenu.Iyer@Sun.COM 	if (rx_fn != NULL)
27169473SVenu.Iyer@Sun.COM 		mac_rx_set(mch, rx_fn, arg);
27179473SVenu.Iyer@Sun.COM 	return (err);
27189473SVenu.Iyer@Sun.COM }
27199473SVenu.Iyer@Sun.COM 
27208275SEric Cheng int
27218275SEric Cheng mac_unicast_add(mac_client_handle_t mch, uint8_t *mac_addr, uint16_t flags,
27228275SEric Cheng     mac_unicast_handle_t *mah, uint16_t vid, mac_diag_t *diag)
27238275SEric Cheng {
27248275SEric Cheng 	mac_impl_t *mip = ((mac_client_impl_t *)mch)->mci_mip;
27258275SEric Cheng 	uint_t err;
27268275SEric Cheng 
27278275SEric Cheng 	i_mac_perim_enter(mip);
27288275SEric Cheng 	err = i_mac_unicast_add(mch, mac_addr, flags, mah, vid, diag);
27298275SEric Cheng 	i_mac_perim_exit(mip);
27308275SEric Cheng 
27318275SEric Cheng 	return (err);
27328275SEric Cheng }
27338275SEric Cheng 
273411878SVenu.Iyer@Sun.COM static void
27359473SVenu.Iyer@Sun.COM mac_client_datapath_teardown(mac_client_handle_t mch, mac_unicast_impl_t *muip,
27369473SVenu.Iyer@Sun.COM     flow_entry_t *flent)
27378275SEric Cheng {
27389473SVenu.Iyer@Sun.COM 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
27399473SVenu.Iyer@Sun.COM 	mac_impl_t		*mip = mcip->mci_mip;
274011878SVenu.Iyer@Sun.COM 	boolean_t		no_unicast;
27418275SEric Cheng 
27428833SVenu.Iyer@Sun.COM 	/*
274311878SVenu.Iyer@Sun.COM 	 * If we have not added a unicast address for this MAC client, just
274411878SVenu.Iyer@Sun.COM 	 * teardown the datapath.
27458833SVenu.Iyer@Sun.COM 	 */
274611878SVenu.Iyer@Sun.COM 	no_unicast = mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR;
274711878SVenu.Iyer@Sun.COM 
274811878SVenu.Iyer@Sun.COM 	if (!no_unicast) {
274911878SVenu.Iyer@Sun.COM 		/*
275011878SVenu.Iyer@Sun.COM 		 * We would have initialized subflows etc. only if we brought
275111878SVenu.Iyer@Sun.COM 		 * up the primary client and set the unicast unicast address
275211878SVenu.Iyer@Sun.COM 		 * etc. Deactivate the flows. The flow entry will be removed
275311878SVenu.Iyer@Sun.COM 		 * from the active flow tables, and the associated SRS,
275411878SVenu.Iyer@Sun.COM 		 * softrings etc will be deleted. But the flow entry itself
275511878SVenu.Iyer@Sun.COM 		 * won't be destroyed, instead it will continue to be archived
275611878SVenu.Iyer@Sun.COM 		 * off the  the global flow hash list, for a possible future
275711878SVenu.Iyer@Sun.COM 		 * activation when say IP is plumbed again.
275811878SVenu.Iyer@Sun.COM 		 */
275911878SVenu.Iyer@Sun.COM 		mac_link_release_flows(mch);
276011878SVenu.Iyer@Sun.COM 	}
27618275SEric Cheng 	mip->mi_nactiveclients--;
27628833SVenu.Iyer@Sun.COM 	mac_update_single_active_client(mip);
27638275SEric Cheng 
27649473SVenu.Iyer@Sun.COM 	/* Tear down the data path */
27658275SEric Cheng 	mac_datapath_teardown(mcip, mcip->mci_flent, SRST_LINK);
27668275SEric Cheng 
27678275SEric Cheng 	/*
27688275SEric Cheng 	 * Prevent any future access to the flow entry through the mci_flent
27698275SEric Cheng 	 * pointer by setting the mci_flent to NULL. Access to mci_flent in
27708275SEric Cheng 	 * mac_bcast_send is also under mi_rw_lock.
27718275SEric Cheng 	 */
27728275SEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
27738275SEric Cheng 	flent = mcip->mci_flent;
27748275SEric Cheng 	mac_client_remove_flow_from_list(mcip, flent);
27758275SEric Cheng 
27768275SEric Cheng 	if (mcip->mci_state_flags & MCIS_DESC_LOGGED)
27778275SEric Cheng 		mcip->mci_state_flags &= ~MCIS_DESC_LOGGED;
27788275SEric Cheng 
27798275SEric Cheng 	/*
27808275SEric Cheng 	 * This is the last unicast address being removed and there shouldn't
27818275SEric Cheng 	 * be any outbound data threads at this point coming down from mac
27828275SEric Cheng 	 * clients. We have waited for the data threads to finish before
27838275SEric Cheng 	 * starting dld_str_detach. Non-data threads must access TX SRS
27848275SEric Cheng 	 * under mi_rw_lock.
27858275SEric Cheng 	 */
27868275SEric Cheng 	rw_exit(&mip->mi_rw_lock);
27878275SEric Cheng 
27888275SEric Cheng 	/*
27898275SEric Cheng 	 * Don't use FLOW_MARK with FE_MC_NO_DATAPATH, as the flow might
27908275SEric Cheng 	 * contain other flags, such as FE_CONDEMNED, which we need to
27918275SEric Cheng 	 * cleared. We don't call mac_flow_cleanup() for this unicast
27928275SEric Cheng 	 * flow as we have a already cleaned up SRSs etc. (via the teadown
27938275SEric Cheng 	 * path). We just clear the stats and reset the initial callback
27948275SEric Cheng 	 * function, the rest will be set when we call mac_flow_create,
27958275SEric Cheng 	 * if at all.
27968275SEric Cheng 	 */
27978275SEric Cheng 	mutex_enter(&flent->fe_lock);
27988275SEric Cheng 	ASSERT(flent->fe_refcnt == 1 && flent->fe_mbg == NULL &&
27998275SEric Cheng 	    flent->fe_tx_srs == NULL && flent->fe_rx_srs_cnt == 0);
28008275SEric Cheng 	flent->fe_flags = FE_MC_NO_DATAPATH;
28018275SEric Cheng 	flow_stat_destroy(flent);
280211878SVenu.Iyer@Sun.COM 	mac_misc_stat_delete(flent);
28038275SEric Cheng 
28048275SEric Cheng 	/* Initialize the receiver function to a safe routine */
28058275SEric Cheng 	flent->fe_cb_fn = (flow_fn_t)mac_pkt_drop;
28068275SEric Cheng 	flent->fe_cb_arg1 = NULL;
28078275SEric Cheng 	flent->fe_cb_arg2 = NULL;
28088275SEric Cheng 
28098275SEric Cheng 	flent->fe_index = -1;
28108275SEric Cheng 	mutex_exit(&flent->fe_lock);
28118275SEric Cheng 
28128275SEric Cheng 	if (mip->mi_type->mt_brdcst_addr != NULL) {
281311878SVenu.Iyer@Sun.COM 		ASSERT(muip != NULL || no_unicast);
28148275SEric Cheng 		mac_bcast_delete(mcip, mip->mi_type->mt_brdcst_addr,
281511878SVenu.Iyer@Sun.COM 		    muip != NULL ? muip->mui_vid : VLAN_ID_NONE);
28168275SEric Cheng 	}
28178275SEric Cheng 
28188275SEric Cheng 	if (mip->mi_nactiveclients == 1) {
28198275SEric Cheng 		mac_capab_update((mac_handle_t)mip);
28208275SEric Cheng 		mac_virtual_link_update(mip);
28218275SEric Cheng 	}
28229073SCathy.Zhou@Sun.COM 
28239073SCathy.Zhou@Sun.COM 	if (mcip->mci_state_flags & MCIS_EXCLUSIVE) {
28248275SEric Cheng 		mip->mi_state_flags &= ~MIS_EXCLUSIVE;
28259073SCathy.Zhou@Sun.COM 
28269073SCathy.Zhou@Sun.COM 		if (mip->mi_state_flags & MIS_LEGACY)
28279073SCathy.Zhou@Sun.COM 			mip->mi_capab_legacy.ml_active_clear(mip->mi_driver);
28289073SCathy.Zhou@Sun.COM 	}
28299073SCathy.Zhou@Sun.COM 
28308400SNicolas.Droux@Sun.COM 	mcip->mci_state_flags &= ~MCIS_UNICAST_HW;
28318275SEric Cheng 
28329024SVenu.Iyer@Sun.COM 	if (mcip->mci_state_flags & MCIS_TAG_DISABLE)
28339024SVenu.Iyer@Sun.COM 		mcip->mci_state_flags &= ~MCIS_TAG_DISABLE;
28349024SVenu.Iyer@Sun.COM 
28359024SVenu.Iyer@Sun.COM 	if (mcip->mci_state_flags & MCIS_STRIP_DISABLE)
28369024SVenu.Iyer@Sun.COM 		mcip->mci_state_flags &= ~MCIS_STRIP_DISABLE;
28379024SVenu.Iyer@Sun.COM 
28389024SVenu.Iyer@Sun.COM 	if (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK)
28399024SVenu.Iyer@Sun.COM 		mcip->mci_state_flags &= ~MCIS_DISABLE_TX_VID_CHECK;
28409024SVenu.Iyer@Sun.COM 
284111878SVenu.Iyer@Sun.COM 	if (muip != NULL)
284211878SVenu.Iyer@Sun.COM 		kmem_free(muip, sizeof (mac_unicast_impl_t));
284311878SVenu.Iyer@Sun.COM 	mac_protect_cancel_timer(mcip);
284411878SVenu.Iyer@Sun.COM 	mac_protect_flush_dhcp(mcip);
284511878SVenu.Iyer@Sun.COM 
284611878SVenu.Iyer@Sun.COM 	bzero(&mcip->mci_misc_stat, sizeof (mcip->mci_misc_stat));
28479073SCathy.Zhou@Sun.COM 	/*
28489073SCathy.Zhou@Sun.COM 	 * Disable fastpath if this is a VNIC or a VLAN.
28499073SCathy.Zhou@Sun.COM 	 */
28509073SCathy.Zhou@Sun.COM 	if (mcip->mci_state_flags & MCIS_IS_VNIC)
28519073SCathy.Zhou@Sun.COM 		mac_fastpath_enable((mac_handle_t)mip);
28528893SMichael.Lim@Sun.COM 	mac_stop((mac_handle_t)mip);
28539473SVenu.Iyer@Sun.COM }
28549473SVenu.Iyer@Sun.COM 
28559473SVenu.Iyer@Sun.COM /*
28569473SVenu.Iyer@Sun.COM  * Remove a MAC address which was previously added by mac_unicast_add().
28579473SVenu.Iyer@Sun.COM  */
28589473SVenu.Iyer@Sun.COM int
28599473SVenu.Iyer@Sun.COM mac_unicast_remove(mac_client_handle_t mch, mac_unicast_handle_t mah)
28609473SVenu.Iyer@Sun.COM {
28619473SVenu.Iyer@Sun.COM 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
28629473SVenu.Iyer@Sun.COM 	mac_unicast_impl_t *muip = (mac_unicast_impl_t *)mah;
28639473SVenu.Iyer@Sun.COM 	mac_unicast_impl_t *pre;
28649473SVenu.Iyer@Sun.COM 	mac_impl_t *mip = mcip->mci_mip;
28659473SVenu.Iyer@Sun.COM 	flow_entry_t		*flent;
286611878SVenu.Iyer@Sun.COM 	uint16_t mui_vid;
28679473SVenu.Iyer@Sun.COM 
28689473SVenu.Iyer@Sun.COM 	i_mac_perim_enter(mip);
28699473SVenu.Iyer@Sun.COM 	if (mcip->mci_flags & MAC_CLIENT_FLAGS_VNIC_PRIMARY) {
28709473SVenu.Iyer@Sun.COM 		/*
28719473SVenu.Iyer@Sun.COM 		 * Called made by the upper MAC client of a VNIC.
28729473SVenu.Iyer@Sun.COM 		 * There's nothing much to do, the unicast address will
28739473SVenu.Iyer@Sun.COM 		 * be removed by the VNIC driver when the VNIC is deleted,
28749473SVenu.Iyer@Sun.COM 		 * but let's ensure that all our transmit is done before
28759473SVenu.Iyer@Sun.COM 		 * the client does a mac_client_stop lest it trigger an
28769473SVenu.Iyer@Sun.COM 		 * assert in the driver.
28779473SVenu.Iyer@Sun.COM 		 */
28789473SVenu.Iyer@Sun.COM 		ASSERT(muip->mui_vid == 0);
28799473SVenu.Iyer@Sun.COM 
28809473SVenu.Iyer@Sun.COM 		mac_tx_client_flush(mcip);
28819473SVenu.Iyer@Sun.COM 
28829473SVenu.Iyer@Sun.COM 		if ((mcip->mci_flags & MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) != 0) {
28839473SVenu.Iyer@Sun.COM 			mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
28849473SVenu.Iyer@Sun.COM 			if (mcip->mci_rx_p_fn != NULL) {
28859473SVenu.Iyer@Sun.COM 				mac_rx_set(mch, mcip->mci_rx_p_fn,
28869473SVenu.Iyer@Sun.COM 				    mcip->mci_rx_p_arg);
28879473SVenu.Iyer@Sun.COM 				mcip->mci_rx_p_fn = NULL;
28889473SVenu.Iyer@Sun.COM 				mcip->mci_rx_p_arg = NULL;
28899473SVenu.Iyer@Sun.COM 			}
28909473SVenu.Iyer@Sun.COM 			kmem_free(muip, sizeof (mac_unicast_impl_t));
28919473SVenu.Iyer@Sun.COM 			i_mac_perim_exit(mip);
28929473SVenu.Iyer@Sun.COM 			return (0);
28939473SVenu.Iyer@Sun.COM 		}
28949473SVenu.Iyer@Sun.COM 		mcip->mci_flags &= ~MAC_CLIENT_FLAGS_VNIC_PRIMARY;
28959473SVenu.Iyer@Sun.COM 
28969473SVenu.Iyer@Sun.COM 		if (mcip->mci_state_flags & MCIS_TAG_DISABLE)
28979473SVenu.Iyer@Sun.COM 			mcip->mci_state_flags &= ~MCIS_TAG_DISABLE;
28989473SVenu.Iyer@Sun.COM 
28999473SVenu.Iyer@Sun.COM 		if (mcip->mci_state_flags & MCIS_STRIP_DISABLE)
29009473SVenu.Iyer@Sun.COM 			mcip->mci_state_flags &= ~MCIS_STRIP_DISABLE;
29019473SVenu.Iyer@Sun.COM 
29029473SVenu.Iyer@Sun.COM 		if (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK)
29039473SVenu.Iyer@Sun.COM 			mcip->mci_state_flags &= ~MCIS_DISABLE_TX_VID_CHECK;
29049473SVenu.Iyer@Sun.COM 
29059473SVenu.Iyer@Sun.COM 		kmem_free(muip, sizeof (mac_unicast_impl_t));
29069473SVenu.Iyer@Sun.COM 		i_mac_perim_exit(mip);
29079473SVenu.Iyer@Sun.COM 		return (0);
29089473SVenu.Iyer@Sun.COM 	}
29099473SVenu.Iyer@Sun.COM 
29109473SVenu.Iyer@Sun.COM 	ASSERT(muip != NULL);
29119473SVenu.Iyer@Sun.COM 
29129473SVenu.Iyer@Sun.COM 	/*
29139473SVenu.Iyer@Sun.COM 	 * We are removing a passive client, we haven't setup the datapath
29149473SVenu.Iyer@Sun.COM 	 * for this yet, so nothing much to do.
29159473SVenu.Iyer@Sun.COM 	 */
29169614SVenu.Iyer@Sun.COM 	if ((mcip->mci_flags & MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) != 0) {
29179473SVenu.Iyer@Sun.COM 
29189473SVenu.Iyer@Sun.COM 		ASSERT((mcip->mci_flent->fe_flags & FE_MC_NO_DATAPATH) != 0);
29199473SVenu.Iyer@Sun.COM 		ASSERT(mcip->mci_p_unicast_list == muip);
29209473SVenu.Iyer@Sun.COM 
29219614SVenu.Iyer@Sun.COM 		mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
29229614SVenu.Iyer@Sun.COM 
29239473SVenu.Iyer@Sun.COM 		mcip->mci_p_unicast_list = NULL;
29249473SVenu.Iyer@Sun.COM 		mcip->mci_rx_p_fn = NULL;
29259473SVenu.Iyer@Sun.COM 		mcip->mci_rx_p_arg = NULL;
29269473SVenu.Iyer@Sun.COM 
29279473SVenu.Iyer@Sun.COM 		mcip->mci_state_flags &= ~MCIS_UNICAST_HW;
29289473SVenu.Iyer@Sun.COM 
29299473SVenu.Iyer@Sun.COM 		if (mcip->mci_state_flags & MCIS_TAG_DISABLE)
29309473SVenu.Iyer@Sun.COM 			mcip->mci_state_flags &= ~MCIS_TAG_DISABLE;
29319473SVenu.Iyer@Sun.COM 
29329473SVenu.Iyer@Sun.COM 		if (mcip->mci_state_flags & MCIS_STRIP_DISABLE)
29339473SVenu.Iyer@Sun.COM 			mcip->mci_state_flags &= ~MCIS_STRIP_DISABLE;
29349473SVenu.Iyer@Sun.COM 
29359473SVenu.Iyer@Sun.COM 		if (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK)
29369473SVenu.Iyer@Sun.COM 			mcip->mci_state_flags &= ~MCIS_DISABLE_TX_VID_CHECK;
29379473SVenu.Iyer@Sun.COM 
29389473SVenu.Iyer@Sun.COM 		kmem_free(muip, sizeof (mac_unicast_impl_t));
29399473SVenu.Iyer@Sun.COM 		i_mac_perim_exit(mip);
29409473SVenu.Iyer@Sun.COM 		return (0);
29419473SVenu.Iyer@Sun.COM 	}
29429473SVenu.Iyer@Sun.COM 	/*
29439473SVenu.Iyer@Sun.COM 	 * Remove the VID from the list of client's VIDs.
29449473SVenu.Iyer@Sun.COM 	 */
29459473SVenu.Iyer@Sun.COM 	pre = mcip->mci_unicast_list;
29469473SVenu.Iyer@Sun.COM 	if (muip == pre) {
29479473SVenu.Iyer@Sun.COM 		mcip->mci_unicast_list = muip->mui_next;
29489473SVenu.Iyer@Sun.COM 	} else {
29499473SVenu.Iyer@Sun.COM 		while ((pre->mui_next != NULL) && (pre->mui_next != muip))
29509473SVenu.Iyer@Sun.COM 			pre = pre->mui_next;
29519473SVenu.Iyer@Sun.COM 		ASSERT(pre->mui_next == muip);
29529473SVenu.Iyer@Sun.COM 		rw_enter(&mcip->mci_rw_lock, RW_WRITER);
29539473SVenu.Iyer@Sun.COM 		pre->mui_next = muip->mui_next;
29549473SVenu.Iyer@Sun.COM 		rw_exit(&mcip->mci_rw_lock);
29559473SVenu.Iyer@Sun.COM 	}
29569473SVenu.Iyer@Sun.COM 
29579473SVenu.Iyer@Sun.COM 	if (!mac_client_single_rcvr(mcip)) {
29589473SVenu.Iyer@Sun.COM 		/*
29599473SVenu.Iyer@Sun.COM 		 * This MAC client is shared by more than one unicast
29609473SVenu.Iyer@Sun.COM 		 * addresses, so we will just remove the flent
29619473SVenu.Iyer@Sun.COM 		 * corresponding to the address being removed. We don't invoke
29629473SVenu.Iyer@Sun.COM 		 * mac_rx_classify_flow_rem() since the additional flow is
29639473SVenu.Iyer@Sun.COM 		 * not associated with its own separate set of SRS and rings,
29649473SVenu.Iyer@Sun.COM 		 * and these constructs are still needed for the remaining
29659473SVenu.Iyer@Sun.COM 		 * flows.
29669473SVenu.Iyer@Sun.COM 		 */
29679473SVenu.Iyer@Sun.COM 		flent = mac_client_get_flow(mcip, muip);
29689473SVenu.Iyer@Sun.COM 		ASSERT(flent != NULL);
29699473SVenu.Iyer@Sun.COM 
29709473SVenu.Iyer@Sun.COM 		/*
29719473SVenu.Iyer@Sun.COM 		 * The first one is disappearing, need to make sure
29729473SVenu.Iyer@Sun.COM 		 * we replace it with another from the list of
29739473SVenu.Iyer@Sun.COM 		 * shared clients.
29749473SVenu.Iyer@Sun.COM 		 */
29759473SVenu.Iyer@Sun.COM 		if (flent == mcip->mci_flent)
29769473SVenu.Iyer@Sun.COM 			flent = mac_client_swap_mciflent(mcip);
29779473SVenu.Iyer@Sun.COM 		mac_client_remove_flow_from_list(mcip, flent);
29789473SVenu.Iyer@Sun.COM 		mac_flow_remove(mip->mi_flow_tab, flent, B_FALSE);
29799473SVenu.Iyer@Sun.COM 		mac_flow_wait(flent, FLOW_DRIVER_UPCALL);
29809473SVenu.Iyer@Sun.COM 
29819473SVenu.Iyer@Sun.COM 		/*
29829473SVenu.Iyer@Sun.COM 		 * The multicast groups that were added by the client so
29839473SVenu.Iyer@Sun.COM 		 * far must be removed from the brodcast domain corresponding
29849473SVenu.Iyer@Sun.COM 		 * to the VID being removed.
29859473SVenu.Iyer@Sun.COM 		 */
29869473SVenu.Iyer@Sun.COM 		mac_client_bcast_refresh(mcip, mac_client_update_mcast,
29879473SVenu.Iyer@Sun.COM 		    (void *)flent, B_FALSE);
29889473SVenu.Iyer@Sun.COM 
29899473SVenu.Iyer@Sun.COM 		if (mip->mi_type->mt_brdcst_addr != NULL) {
29909473SVenu.Iyer@Sun.COM 			mac_bcast_delete(mcip, mip->mi_type->mt_brdcst_addr,
29919473SVenu.Iyer@Sun.COM 			    muip->mui_vid);
29929473SVenu.Iyer@Sun.COM 		}
29939473SVenu.Iyer@Sun.COM 
29949473SVenu.Iyer@Sun.COM 		FLOW_FINAL_REFRELE(flent);
29959473SVenu.Iyer@Sun.COM 		ASSERT(!(mcip->mci_state_flags & MCIS_EXCLUSIVE));
29969473SVenu.Iyer@Sun.COM 		/*
29979473SVenu.Iyer@Sun.COM 		 * Enable fastpath if this is a VNIC or a VLAN.
29989473SVenu.Iyer@Sun.COM 		 */
29999473SVenu.Iyer@Sun.COM 		if (mcip->mci_state_flags & MCIS_IS_VNIC)
30009473SVenu.Iyer@Sun.COM 			mac_fastpath_enable((mac_handle_t)mip);
30019473SVenu.Iyer@Sun.COM 		mac_stop((mac_handle_t)mip);
30029473SVenu.Iyer@Sun.COM 		i_mac_perim_exit(mip);
30039473SVenu.Iyer@Sun.COM 		return (0);
30049473SVenu.Iyer@Sun.COM 	}
30059473SVenu.Iyer@Sun.COM 
300611878SVenu.Iyer@Sun.COM 	mui_vid = muip->mui_vid;
30079473SVenu.Iyer@Sun.COM 	mac_client_datapath_teardown(mch, muip, flent);
30089473SVenu.Iyer@Sun.COM 
300911878SVenu.Iyer@Sun.COM 	if ((mcip->mci_flags & MAC_CLIENT_FLAGS_PRIMARY) && mui_vid == 0) {
301011878SVenu.Iyer@Sun.COM 		mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PRIMARY;
301111878SVenu.Iyer@Sun.COM 	} else {
301211878SVenu.Iyer@Sun.COM 		i_mac_perim_exit(mip);
301311878SVenu.Iyer@Sun.COM 		return (0);
301411878SVenu.Iyer@Sun.COM 	}
301511878SVenu.Iyer@Sun.COM 
30169473SVenu.Iyer@Sun.COM 	/*
30179473SVenu.Iyer@Sun.COM 	 * If we are removing the primary, check if we have a passive primary
30189473SVenu.Iyer@Sun.COM 	 * client that we need to activate now.
30199473SVenu.Iyer@Sun.COM 	 */
30209473SVenu.Iyer@Sun.COM 	mcip = mac_get_passive_primary_client(mip);
30219473SVenu.Iyer@Sun.COM 	if (mcip != NULL) {
302211878SVenu.Iyer@Sun.COM 		mac_resource_props_t	*mrp;
30239473SVenu.Iyer@Sun.COM 		mac_unicast_impl_t	*muip;
30249473SVenu.Iyer@Sun.COM 
30259473SVenu.Iyer@Sun.COM 		mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
302611878SVenu.Iyer@Sun.COM 		mrp = kmem_zalloc(sizeof (*mrp), KM_SLEEP);
302711878SVenu.Iyer@Sun.COM 
30289473SVenu.Iyer@Sun.COM 		/*
30299473SVenu.Iyer@Sun.COM 		 * Apply the property cached in the mac_impl_t to the
30309473SVenu.Iyer@Sun.COM 		 * primary mac client.
30319473SVenu.Iyer@Sun.COM 		 */
303211878SVenu.Iyer@Sun.COM 		mac_get_resources((mac_handle_t)mip, mrp);
303311878SVenu.Iyer@Sun.COM 		(void) mac_client_set_resources(mch, mrp);
30349473SVenu.Iyer@Sun.COM 		ASSERT(mcip->mci_p_unicast_list != NULL);
30359473SVenu.Iyer@Sun.COM 		muip = mcip->mci_p_unicast_list;
30369473SVenu.Iyer@Sun.COM 		mcip->mci_p_unicast_list = NULL;
30379473SVenu.Iyer@Sun.COM 		if (mac_client_datapath_setup(mcip, VLAN_ID_NONE,
303811878SVenu.Iyer@Sun.COM 		    mip->mi_addr, mrp, B_TRUE, muip) == 0) {
30399473SVenu.Iyer@Sun.COM 			if (mcip->mci_rx_p_fn != NULL) {
30409473SVenu.Iyer@Sun.COM 				mac_rx_set(mch, mcip->mci_rx_p_fn,
30419473SVenu.Iyer@Sun.COM 				    mcip->mci_rx_p_arg);
30429473SVenu.Iyer@Sun.COM 				mcip->mci_rx_p_fn = NULL;
30439473SVenu.Iyer@Sun.COM 				mcip->mci_rx_p_arg = NULL;
30449473SVenu.Iyer@Sun.COM 			}
30459822SVenu.Iyer@Sun.COM 		} else {
30469822SVenu.Iyer@Sun.COM 			kmem_free(muip, sizeof (mac_unicast_impl_t));
30479473SVenu.Iyer@Sun.COM 		}
304811878SVenu.Iyer@Sun.COM 		kmem_free(mrp, sizeof (*mrp));
30499473SVenu.Iyer@Sun.COM 	}
30508275SEric Cheng 	i_mac_perim_exit(mip);
30518275SEric Cheng 	return (0);
30528275SEric Cheng }
30538275SEric Cheng 
30548275SEric Cheng /*
30558275SEric Cheng  * Multicast add function invoked by MAC clients.
30568275SEric Cheng  */
30578275SEric Cheng int
30588275SEric Cheng mac_multicast_add(mac_client_handle_t mch, const uint8_t *addr)
30598275SEric Cheng {
30608275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
30618275SEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
30628275SEric Cheng 	flow_entry_t		*flent = mcip->mci_flent_list;
30638275SEric Cheng 	flow_entry_t		*prev_fe = NULL;
30648275SEric Cheng 	uint16_t		vid;
30658275SEric Cheng 	int			err = 0;
30668275SEric Cheng 
30678275SEric Cheng 	/* Verify the address is a valid multicast address */
30688275SEric Cheng 	if ((err = mip->mi_type->mt_ops.mtops_multicst_verify(addr,
30698275SEric Cheng 	    mip->mi_pdata)) != 0)
30708275SEric Cheng 		return (err);
30718275SEric Cheng 
30728275SEric Cheng 	i_mac_perim_enter(mip);
30738275SEric Cheng 	while (flent != NULL) {
30748275SEric Cheng 		vid = i_mac_flow_vid(flent);
30758275SEric Cheng 
30768275SEric Cheng 		err = mac_bcast_add((mac_client_impl_t *)mch, addr, vid,
30778275SEric Cheng 		    MAC_ADDRTYPE_MULTICAST);
30788275SEric Cheng 		if (err != 0)
30798275SEric Cheng 			break;
30808275SEric Cheng 		prev_fe = flent;
30818275SEric Cheng 		flent = flent->fe_client_next;
30828275SEric Cheng 	}
30838275SEric Cheng 
30848275SEric Cheng 	/*
30858275SEric Cheng 	 * If we failed adding, then undo all, rather than partial
30868275SEric Cheng 	 * success.
30878275SEric Cheng 	 */
30888275SEric Cheng 	if (flent != NULL && prev_fe != NULL) {
30898275SEric Cheng 		flent = mcip->mci_flent_list;
30908275SEric Cheng 		while (flent != prev_fe->fe_client_next) {
30918275SEric Cheng 			vid = i_mac_flow_vid(flent);
30928275SEric Cheng 			mac_bcast_delete((mac_client_impl_t *)mch, addr, vid);
30938275SEric Cheng 			flent = flent->fe_client_next;
30948275SEric Cheng 		}
30958275SEric Cheng 	}
30968275SEric Cheng 	i_mac_perim_exit(mip);
30978275SEric Cheng 	return (err);
30988275SEric Cheng }
30998275SEric Cheng 
31008275SEric Cheng /*
31018275SEric Cheng  * Multicast delete function invoked by MAC clients.
31028275SEric Cheng  */
31038275SEric Cheng void
31048275SEric Cheng mac_multicast_remove(mac_client_handle_t mch, const uint8_t *addr)
31058275SEric Cheng {
31068275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
31078275SEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
31088275SEric Cheng 	flow_entry_t		*flent;
31098275SEric Cheng 	uint16_t		vid;
31108275SEric Cheng 
31118275SEric Cheng 	i_mac_perim_enter(mip);
31128275SEric Cheng 	for (flent = mcip->mci_flent_list; flent != NULL;
31138275SEric Cheng 	    flent = flent->fe_client_next) {
31148275SEric Cheng 		vid = i_mac_flow_vid(flent);
31158275SEric Cheng 		mac_bcast_delete((mac_client_impl_t *)mch, addr, vid);
31168275SEric Cheng 	}
31178275SEric Cheng 	i_mac_perim_exit(mip);
31188275SEric Cheng }
31198275SEric Cheng 
31208275SEric Cheng /*
31218275SEric Cheng  * When a MAC client desires to capture packets on an interface,
31228275SEric Cheng  * it registers a promiscuous call back with mac_promisc_add().
31238275SEric Cheng  * There are three types of promiscuous callbacks:
31248275SEric Cheng  *
31258275SEric Cheng  * * MAC_CLIENT_PROMISC_ALL
31268275SEric Cheng  *   Captures all packets sent and received by the MAC client,
31278275SEric Cheng  *   the physical interface, as well as all other MAC clients
31288275SEric Cheng  *   defined on top of the same MAC.
31298275SEric Cheng  *
31308275SEric Cheng  * * MAC_CLIENT_PROMISC_FILTERED
31318275SEric Cheng  *   Captures all packets sent and received by the MAC client,
31328275SEric Cheng  *   plus all multicast traffic sent and received by the phyisical
31338275SEric Cheng  *   interface and the other MAC clients.
31348275SEric Cheng  *
31358275SEric Cheng  * * MAC_CLIENT_PROMISC_MULTI
31368275SEric Cheng  *   Captures all broadcast and multicast packets sent and
31378275SEric Cheng  *   received by the MAC clients as well as the physical interface.
31388275SEric Cheng  *
31398275SEric Cheng  * In all cases, the underlying MAC is put in promiscuous mode.
31408275SEric Cheng  */
31418275SEric Cheng int
31428275SEric Cheng mac_promisc_add(mac_client_handle_t mch, mac_client_promisc_type_t type,
31438275SEric Cheng     mac_rx_t fn, void *arg, mac_promisc_handle_t *mphp, uint16_t flags)
31448275SEric Cheng {
31458275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
31468275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
31478275SEric Cheng 	mac_promisc_impl_t *mpip;
31488275SEric Cheng 	mac_cb_info_t	*mcbi;
31498275SEric Cheng 	int rc;
31508275SEric Cheng 
31518275SEric Cheng 	i_mac_perim_enter(mip);
31528275SEric Cheng 
31538893SMichael.Lim@Sun.COM 	if ((rc = mac_start((mac_handle_t)mip)) != 0) {
31548275SEric Cheng 		i_mac_perim_exit(mip);
31558275SEric Cheng 		return (rc);
31568275SEric Cheng 	}
31578275SEric Cheng 
31588275SEric Cheng 	if ((mcip->mci_state_flags & MCIS_IS_VNIC) &&
31598275SEric Cheng 	    type == MAC_CLIENT_PROMISC_ALL) {
31608275SEric Cheng 		/*
31618275SEric Cheng 		 * The function is being invoked by the upper MAC client
31628275SEric Cheng 		 * of a VNIC. The VNIC should only see the traffic
31638275SEric Cheng 		 * it is entitled to.
31648275SEric Cheng 		 */
31658275SEric Cheng 		type = MAC_CLIENT_PROMISC_FILTERED;
31668275SEric Cheng 	}
31678275SEric Cheng 
31688275SEric Cheng 
31698275SEric Cheng 	/*
31708275SEric Cheng 	 * Turn on promiscuous mode for the underlying NIC.
31718275SEric Cheng 	 * This is needed even for filtered callbacks which
31728275SEric Cheng 	 * expect to receive all multicast traffic on the wire.
31738275SEric Cheng 	 *
31748275SEric Cheng 	 * Physical promiscuous mode should not be turned on if
31758275SEric Cheng 	 * MAC_PROMISC_FLAGS_NO_PHYS is set.
31768275SEric Cheng 	 */
31778275SEric Cheng 	if ((flags & MAC_PROMISC_FLAGS_NO_PHYS) == 0) {
31789641SGirish.Moodalbail@Sun.COM 		if ((rc = i_mac_promisc_set(mip, B_TRUE)) != 0) {
31798893SMichael.Lim@Sun.COM 			mac_stop((mac_handle_t)mip);
31808275SEric Cheng 			i_mac_perim_exit(mip);
31818275SEric Cheng 			return (rc);
31828275SEric Cheng 		}
31838275SEric Cheng 	}
31848275SEric Cheng 
31858275SEric Cheng 	mpip = kmem_cache_alloc(mac_promisc_impl_cache, KM_SLEEP);
31868275SEric Cheng 
31878275SEric Cheng 	mpip->mpi_type = type;
31888275SEric Cheng 	mpip->mpi_fn = fn;
31898275SEric Cheng 	mpip->mpi_arg = arg;
31908275SEric Cheng 	mpip->mpi_mcip = mcip;
31918275SEric Cheng 	mpip->mpi_no_tx_loop = ((flags & MAC_PROMISC_FLAGS_NO_TX_LOOP) != 0);
31928275SEric Cheng 	mpip->mpi_no_phys = ((flags & MAC_PROMISC_FLAGS_NO_PHYS) != 0);
31938833SVenu.Iyer@Sun.COM 	mpip->mpi_strip_vlan_tag =
31948833SVenu.Iyer@Sun.COM 	    ((flags & MAC_PROMISC_FLAGS_VLAN_TAG_STRIP) != 0);
319510639SDarren.Reed@Sun.COM 	mpip->mpi_no_copy = ((flags & MAC_PROMISC_FLAGS_NO_COPY) != 0);
31968275SEric Cheng 
31978275SEric Cheng 	mcbi = &mip->mi_promisc_cb_info;
31988275SEric Cheng 	mutex_enter(mcbi->mcbi_lockp);
31998275SEric Cheng 
32008275SEric Cheng 	mac_callback_add(&mip->mi_promisc_cb_info, &mcip->mci_promisc_list,
32018275SEric Cheng 	    &mpip->mpi_mci_link);
32028275SEric Cheng 	mac_callback_add(&mip->mi_promisc_cb_info, &mip->mi_promisc_list,
32038275SEric Cheng 	    &mpip->mpi_mi_link);
32048275SEric Cheng 
32058275SEric Cheng 	mutex_exit(mcbi->mcbi_lockp);
32068275SEric Cheng 
32078275SEric Cheng 	*mphp = (mac_promisc_handle_t)mpip;
32088275SEric Cheng 	i_mac_perim_exit(mip);
32098275SEric Cheng 	return (0);
32108275SEric Cheng }
32118275SEric Cheng 
32128275SEric Cheng /*
32138275SEric Cheng  * Remove a multicast address previously aded through mac_promisc_add().
32148275SEric Cheng  */
32159044SGirish.Moodalbail@Sun.COM void
32168275SEric Cheng mac_promisc_remove(mac_promisc_handle_t mph)
32178275SEric Cheng {
32188275SEric Cheng 	mac_promisc_impl_t *mpip = (mac_promisc_impl_t *)mph;
32198275SEric Cheng 	mac_client_impl_t *mcip = mpip->mpi_mcip;
32208275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
32218275SEric Cheng 	mac_cb_info_t *mcbi;
32229641SGirish.Moodalbail@Sun.COM 	int rv;
32238275SEric Cheng 
32248275SEric Cheng 	i_mac_perim_enter(mip);
32258275SEric Cheng 
32268275SEric Cheng 	/*
32278275SEric Cheng 	 * Even if the device can't be reset into normal mode, we still
32288275SEric Cheng 	 * need to clear the client promisc callbacks. The client may want
32298275SEric Cheng 	 * to close the mac end point and we can't have stale callbacks.
32308275SEric Cheng 	 */
32318275SEric Cheng 	if (!(mpip->mpi_no_phys)) {
32329641SGirish.Moodalbail@Sun.COM 		if ((rv = i_mac_promisc_set(mip, B_FALSE)) != 0) {
32339641SGirish.Moodalbail@Sun.COM 			cmn_err(CE_WARN, "%s: failed to switch OFF promiscuous"
32349641SGirish.Moodalbail@Sun.COM 			    " mode because of error 0x%x", mip->mi_name, rv);
32359641SGirish.Moodalbail@Sun.COM 		}
32368275SEric Cheng 	}
32378275SEric Cheng 	mcbi = &mip->mi_promisc_cb_info;
32388275SEric Cheng 	mutex_enter(mcbi->mcbi_lockp);
32398275SEric Cheng 	if (mac_callback_remove(mcbi, &mip->mi_promisc_list,
32408275SEric Cheng 	    &mpip->mpi_mi_link)) {
32418275SEric Cheng 		VERIFY(mac_callback_remove(&mip->mi_promisc_cb_info,
32428275SEric Cheng 		    &mcip->mci_promisc_list, &mpip->mpi_mci_link));
32438275SEric Cheng 		kmem_cache_free(mac_promisc_impl_cache, mpip);
32448275SEric Cheng 	} else {
32458275SEric Cheng 		mac_callback_remove_wait(&mip->mi_promisc_cb_info);
32468275SEric Cheng 	}
32478275SEric Cheng 	mutex_exit(mcbi->mcbi_lockp);
32488893SMichael.Lim@Sun.COM 	mac_stop((mac_handle_t)mip);
32498275SEric Cheng 
32508275SEric Cheng 	i_mac_perim_exit(mip);
32518275SEric Cheng }
32528275SEric Cheng 
32538275SEric Cheng /*
32548275SEric Cheng  * Reference count the number of active Tx threads. MCI_TX_QUIESCE indicates
32558275SEric Cheng  * that a control operation wants to quiesce the Tx data flow in which case
32568275SEric Cheng  * we return an error. Holding any of the per cpu locks ensures that the
32578275SEric Cheng  * mci_tx_flag won't change.
32588275SEric Cheng  *
32598275SEric Cheng  * 'CPU' must be accessed just once and used to compute the index into the
32608275SEric Cheng  * percpu array, and that index must be used for the entire duration of the
32618275SEric Cheng  * packet send operation. Note that the thread may be preempted and run on
32628275SEric Cheng  * another cpu any time and so we can't use 'CPU' more than once for the
32638275SEric Cheng  * operation.
32648275SEric Cheng  */
32658275SEric Cheng #define	MAC_TX_TRY_HOLD(mcip, mytx, error)				\
32668275SEric Cheng {									\
32678275SEric Cheng 	(error) = 0;							\
32688275SEric Cheng 	(mytx) = &(mcip)->mci_tx_pcpu[CPU->cpu_seqid & mac_tx_percpu_cnt]; \
32698275SEric Cheng 	mutex_enter(&(mytx)->pcpu_tx_lock);				\
32708275SEric Cheng 	if (!((mcip)->mci_tx_flag & MCI_TX_QUIESCE)) {			\
32718275SEric Cheng 		(mytx)->pcpu_tx_refcnt++;				\
32728275SEric Cheng 	} else {							\
32738275SEric Cheng 		(error) = -1;						\
32748275SEric Cheng 	}								\
32758275SEric Cheng 	mutex_exit(&(mytx)->pcpu_tx_lock);				\
32768275SEric Cheng }
32778275SEric Cheng 
32788275SEric Cheng /*
32798275SEric Cheng  * Release the reference. If needed, signal any control operation waiting
32808275SEric Cheng  * for Tx quiescence. The wait and signal are always done using the
32818275SEric Cheng  * mci_tx_pcpu[0]'s lock
32828275SEric Cheng  */
32838275SEric Cheng #define	MAC_TX_RELE(mcip, mytx) {					\
32848275SEric Cheng 	mutex_enter(&(mytx)->pcpu_tx_lock);				\
32858275SEric Cheng 	if (--(mytx)->pcpu_tx_refcnt == 0 &&				\
32868275SEric Cheng 	    (mcip)->mci_tx_flag & MCI_TX_QUIESCE) {			\
32878275SEric Cheng 		mutex_exit(&(mytx)->pcpu_tx_lock);			\
32888275SEric Cheng 		mutex_enter(&(mcip)->mci_tx_pcpu[0].pcpu_tx_lock);	\
32898275SEric Cheng 		cv_signal(&(mcip)->mci_tx_cv);				\
32908275SEric Cheng 		mutex_exit(&(mcip)->mci_tx_pcpu[0].pcpu_tx_lock);	\
32918275SEric Cheng 	} else {							\
32928275SEric Cheng 		mutex_exit(&(mytx)->pcpu_tx_lock);			\
32938275SEric Cheng 	}								\
32948275SEric Cheng }
32958275SEric Cheng 
32968275SEric Cheng /*
32978275SEric Cheng  * Send function invoked by MAC clients.
32988275SEric Cheng  */
32998275SEric Cheng mac_tx_cookie_t
33008275SEric Cheng mac_tx(mac_client_handle_t mch, mblk_t *mp_chain, uintptr_t hint,
33018275SEric Cheng     uint16_t flag, mblk_t **ret_mp)
33028275SEric Cheng {
330310734SEric Cheng 	mac_tx_cookie_t		cookie = NULL;
33048275SEric Cheng 	int			error;
33058275SEric Cheng 	mac_tx_percpu_t		*mytx;
33068275SEric Cheng 	mac_soft_ring_set_t	*srs;
33078275SEric Cheng 	flow_entry_t		*flent;
33088275SEric Cheng 	boolean_t		is_subflow = B_FALSE;
33098275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
33108275SEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
33118275SEric Cheng 	mac_srs_tx_t		*srs_tx;
33128275SEric Cheng 
33138275SEric Cheng 	/*
33148275SEric Cheng 	 * Check whether the active Tx threads count is bumped already.
33158275SEric Cheng 	 */
33168275SEric Cheng 	if (!(flag & MAC_TX_NO_HOLD)) {
33178275SEric Cheng 		MAC_TX_TRY_HOLD(mcip, mytx, error);
33188275SEric Cheng 		if (error != 0) {
33198275SEric Cheng 			freemsgchain(mp_chain);
33208275SEric Cheng 			return (NULL);
33218275SEric Cheng 		}
33228275SEric Cheng 	}
33238275SEric Cheng 
332410734SEric Cheng 	/*
332510734SEric Cheng 	 * If mac protection is enabled, only the permissible packets will be
332610734SEric Cheng 	 * returned by mac_protect_check().
332710734SEric Cheng 	 */
332810734SEric Cheng 	if ((mcip->mci_flent->
332910734SEric Cheng 	    fe_resource_props.mrp_mask & MRP_PROTECT) != 0 &&
333010734SEric Cheng 	    (mp_chain = mac_protect_check(mch, mp_chain)) == NULL)
333110734SEric Cheng 		goto done;
333210734SEric Cheng 
33338275SEric Cheng 	if (mcip->mci_subflow_tab != NULL &&
33348275SEric Cheng 	    mcip->mci_subflow_tab->ft_flow_count > 0 &&
33358275SEric Cheng 	    mac_flow_lookup(mcip->mci_subflow_tab, mp_chain,
33368275SEric Cheng 	    FLOW_OUTBOUND, &flent) == 0) {
33378275SEric Cheng 		/*
33388275SEric Cheng 		 * The main assumption here is that if in the event
33398275SEric Cheng 		 * we get a chain, all the packets will be classified
33408275SEric Cheng 		 * to the same Flow/SRS. If this changes for any
33418275SEric Cheng 		 * reason, the following logic should change as well.
33428275SEric Cheng 		 * I suppose the fanout_hint also assumes this .
33438275SEric Cheng 		 */
33448275SEric Cheng 		ASSERT(flent != NULL);
33458275SEric Cheng 		is_subflow = B_TRUE;
33468275SEric Cheng 	} else {
33478275SEric Cheng 		flent = mcip->mci_flent;
33488275SEric Cheng 	}
33498275SEric Cheng 
33508275SEric Cheng 	srs = flent->fe_tx_srs;
335110639SDarren.Reed@Sun.COM 	/*
335210639SDarren.Reed@Sun.COM 	 * This is to avoid panics with PF_PACKET that can call mac_tx()
335310639SDarren.Reed@Sun.COM 	 * against an interface that is not capable of sending. A rewrite
335410639SDarren.Reed@Sun.COM 	 * of the mac datapath is required to remove this limitation.
335510639SDarren.Reed@Sun.COM 	 */
335610639SDarren.Reed@Sun.COM 	if (srs == NULL) {
335710639SDarren.Reed@Sun.COM 		freemsgchain(mp_chain);
335810734SEric Cheng 		goto done;
335910639SDarren.Reed@Sun.COM 	}
336010734SEric Cheng 
33618275SEric Cheng 	srs_tx = &srs->srs_tx;
33628275SEric Cheng 	if (srs_tx->st_mode == SRS_TX_DEFAULT &&
33638275SEric Cheng 	    (srs->srs_state & SRS_ENQUEUED) == 0 &&
336411878SVenu.Iyer@Sun.COM 	    mip->mi_nactiveclients == 1 && mp_chain->b_next == NULL) {
33658275SEric Cheng 		uint64_t	obytes;
33668275SEric Cheng 
33678275SEric Cheng 		/*
33688275SEric Cheng 		 * Since dls always opens the underlying MAC, nclients equals
33698275SEric Cheng 		 * to 1 means that the only active client is dls itself acting
33708275SEric Cheng 		 * as a primary client of the MAC instance. Since dls will not
33718275SEric Cheng 		 * send tagged packets in that case, and dls is trusted to send
33728275SEric Cheng 		 * packets for its allowed VLAN(s), the VLAN tag insertion and
33738275SEric Cheng 		 * check is required only if nclients is greater than 1.
33748275SEric Cheng 		 */
33758275SEric Cheng 		if (mip->mi_nclients > 1) {
33768275SEric Cheng 			if (MAC_VID_CHECK_NEEDED(mcip)) {
33778275SEric Cheng 				int	err = 0;
33788275SEric Cheng 
33798275SEric Cheng 				MAC_VID_CHECK(mcip, mp_chain, err);
33808275SEric Cheng 				if (err != 0) {
33818275SEric Cheng 					freemsg(mp_chain);
338211878SVenu.Iyer@Sun.COM 					mcip->mci_misc_stat.mms_txerrors++;
33838275SEric Cheng 					goto done;
33848275SEric Cheng 				}
33858275SEric Cheng 			}
33868275SEric Cheng 			if (MAC_TAG_NEEDED(mcip)) {
33878275SEric Cheng 				mp_chain = mac_add_vlan_tag(mp_chain, 0,
33888275SEric Cheng 				    mac_client_vid(mch));
33898275SEric Cheng 				if (mp_chain == NULL) {
339011878SVenu.Iyer@Sun.COM 					mcip->mci_misc_stat.mms_txerrors++;
33918275SEric Cheng 					goto done;
33928275SEric Cheng 				}
33938275SEric Cheng 			}
33948275SEric Cheng 		}
33958275SEric Cheng 
33968275SEric Cheng 		obytes = (mp_chain->b_cont == NULL ? MBLKL(mp_chain) :
33978275SEric Cheng 		    msgdsize(mp_chain));
33988275SEric Cheng 
339911878SVenu.Iyer@Sun.COM 		MAC_TX(mip, srs_tx->st_arg2, mp_chain, mcip);
34008275SEric Cheng 		if (mp_chain == NULL) {
34018275SEric Cheng 			cookie = NULL;
340211878SVenu.Iyer@Sun.COM 			SRS_TX_STAT_UPDATE(srs, opackets, 1);
340311878SVenu.Iyer@Sun.COM 			SRS_TX_STAT_UPDATE(srs, obytes, obytes);
34048275SEric Cheng 		} else {
34058275SEric Cheng 			mutex_enter(&srs->srs_lock);
34068275SEric Cheng 			cookie = mac_tx_srs_no_desc(srs, mp_chain,
34078275SEric Cheng 			    flag, ret_mp);
34088275SEric Cheng 			mutex_exit(&srs->srs_lock);
34098275SEric Cheng 		}
34108275SEric Cheng 	} else {
34118275SEric Cheng 		cookie = srs_tx->st_func(srs, mp_chain, hint, flag, ret_mp);
34128275SEric Cheng 	}
34138275SEric Cheng 
34148275SEric Cheng done:
34158275SEric Cheng 	if (is_subflow)
34168275SEric Cheng 		FLOW_REFRELE(flent);
34178275SEric Cheng 
34188275SEric Cheng 	if (!(flag & MAC_TX_NO_HOLD))
34198275SEric Cheng 		MAC_TX_RELE(mcip, mytx);
34208275SEric Cheng 
34218275SEric Cheng 	return (cookie);
34228275SEric Cheng }
34238275SEric Cheng 
34248275SEric Cheng /*
34258275SEric Cheng  * mac_tx_is_blocked
34268275SEric Cheng  *
34278275SEric Cheng  * Given a cookie, it returns if the ring identified by the cookie is
34288833SVenu.Iyer@Sun.COM  * flow-controlled or not. If NULL is passed in place of a cookie,
34298833SVenu.Iyer@Sun.COM  * then it finds out if any of the underlying rings belonging to the
34308833SVenu.Iyer@Sun.COM  * SRS is flow controlled or not and returns that status.
34318275SEric Cheng  */
34328275SEric Cheng /* ARGSUSED */
34338275SEric Cheng boolean_t
34348275SEric Cheng mac_tx_is_flow_blocked(mac_client_handle_t mch, mac_tx_cookie_t cookie)
34358275SEric Cheng {
34368275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
34378833SVenu.Iyer@Sun.COM 	mac_soft_ring_set_t *mac_srs;
34388275SEric Cheng 	mac_soft_ring_t *sringp;
34398275SEric Cheng 	boolean_t blocked = B_FALSE;
34408833SVenu.Iyer@Sun.COM 	mac_tx_percpu_t *mytx;
34418833SVenu.Iyer@Sun.COM 	int err;
34428275SEric Cheng 	int i;
34438275SEric Cheng 
34448275SEric Cheng 	/*
34458833SVenu.Iyer@Sun.COM 	 * Bump the reference count so that mac_srs won't be deleted.
34468833SVenu.Iyer@Sun.COM 	 * If the client is currently quiesced and we failed to bump
34478833SVenu.Iyer@Sun.COM 	 * the reference, return B_TRUE so that flow control stays
34488833SVenu.Iyer@Sun.COM 	 * as enabled.
34498833SVenu.Iyer@Sun.COM 	 *
34508833SVenu.Iyer@Sun.COM 	 * Flow control will then be disabled once the client is no
34518833SVenu.Iyer@Sun.COM 	 * longer quiesced.
34528275SEric Cheng 	 */
34538833SVenu.Iyer@Sun.COM 	MAC_TX_TRY_HOLD(mcip, mytx, err);
34548833SVenu.Iyer@Sun.COM 	if (err != 0)
34558833SVenu.Iyer@Sun.COM 		return (B_TRUE);
34568833SVenu.Iyer@Sun.COM 
34578833SVenu.Iyer@Sun.COM 	if ((mac_srs = MCIP_TX_SRS(mcip)) == NULL) {
34588833SVenu.Iyer@Sun.COM 		MAC_TX_RELE(mcip, mytx);
34598275SEric Cheng 		return (B_FALSE);
34608833SVenu.Iyer@Sun.COM 	}
34618275SEric Cheng 
34628275SEric Cheng 	mutex_enter(&mac_srs->srs_lock);
346311878SVenu.Iyer@Sun.COM 	/*
346411878SVenu.Iyer@Sun.COM 	 * Only in the case of TX_FANOUT and TX_AGGR, the underlying
346511878SVenu.Iyer@Sun.COM 	 * softring (s_ring_state) will have the HIWAT set. This is
346611878SVenu.Iyer@Sun.COM 	 * the multiple Tx ring flow control case. For all other
346711878SVenu.Iyer@Sun.COM 	 * case, SRS (srs_state) will store the condition.
346811878SVenu.Iyer@Sun.COM 	 */
346911878SVenu.Iyer@Sun.COM 	if (mac_srs->srs_tx.st_mode == SRS_TX_FANOUT ||
347011878SVenu.Iyer@Sun.COM 	    mac_srs->srs_tx.st_mode == SRS_TX_AGGR) {
34718833SVenu.Iyer@Sun.COM 		if (cookie != NULL) {
34728833SVenu.Iyer@Sun.COM 			sringp = (mac_soft_ring_t *)cookie;
34738275SEric Cheng 			mutex_enter(&sringp->s_ring_lock);
34748833SVenu.Iyer@Sun.COM 			if (sringp->s_ring_state & S_RING_TX_HIWAT)
34758275SEric Cheng 				blocked = B_TRUE;
34768833SVenu.Iyer@Sun.COM 			mutex_exit(&sringp->s_ring_lock);
34778833SVenu.Iyer@Sun.COM 		} else {
347811878SVenu.Iyer@Sun.COM 			for (i = 0; i < mac_srs->srs_tx_ring_count; i++) {
347911878SVenu.Iyer@Sun.COM 				sringp = mac_srs->srs_tx_soft_rings[i];
34808833SVenu.Iyer@Sun.COM 				mutex_enter(&sringp->s_ring_lock);
34818833SVenu.Iyer@Sun.COM 				if (sringp->s_ring_state & S_RING_TX_HIWAT) {
34828833SVenu.Iyer@Sun.COM 					blocked = B_TRUE;
34838833SVenu.Iyer@Sun.COM 					mutex_exit(&sringp->s_ring_lock);
34848833SVenu.Iyer@Sun.COM 					break;
34858833SVenu.Iyer@Sun.COM 				}
34868275SEric Cheng 				mutex_exit(&sringp->s_ring_lock);
34878275SEric Cheng 			}
34888275SEric Cheng 		}
34898275SEric Cheng 	} else {
34908275SEric Cheng 		blocked = (mac_srs->srs_state & SRS_TX_HIWAT);
34918275SEric Cheng 	}
34928275SEric Cheng 	mutex_exit(&mac_srs->srs_lock);
34938833SVenu.Iyer@Sun.COM 	MAC_TX_RELE(mcip, mytx);
34948275SEric Cheng 	return (blocked);
34958275SEric Cheng }
34968275SEric Cheng 
34978275SEric Cheng /*
34988275SEric Cheng  * Check if the MAC client is the primary MAC client.
34998275SEric Cheng  */
35008275SEric Cheng boolean_t
35018275SEric Cheng mac_is_primary_client(mac_client_impl_t *mcip)
35028275SEric Cheng {
35038275SEric Cheng 	return (mcip->mci_flags & MAC_CLIENT_FLAGS_PRIMARY);
35048275SEric Cheng }
35058275SEric Cheng 
35068275SEric Cheng void
35078275SEric Cheng mac_ioctl(mac_handle_t mh, queue_t *wq, mblk_t *bp)
35088275SEric Cheng {
35098275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
35108275SEric Cheng 	int cmd = ((struct iocblk *)bp->b_rptr)->ioc_cmd;
35118275SEric Cheng 
35128275SEric Cheng 	if ((cmd == ND_GET && (mip->mi_callbacks->mc_callbacks & MC_GETPROP)) ||
35138275SEric Cheng 	    (cmd == ND_SET && (mip->mi_callbacks->mc_callbacks & MC_SETPROP))) {
35148275SEric Cheng 		/*
35158275SEric Cheng 		 * If ndd props were registered, call them.
35168275SEric Cheng 		 * Note that ndd ioctls are Obsolete
35178275SEric Cheng 		 */
35188275SEric Cheng 		mac_ndd_ioctl(mip, wq, bp);
35198275SEric Cheng 		return;
35208275SEric Cheng 	}
35218275SEric Cheng 
35228275SEric Cheng 	/*
35238275SEric Cheng 	 * Call the driver to handle the ioctl.  The driver may not support
35248275SEric Cheng 	 * any ioctls, in which case we reply with a NAK on its behalf.
35258275SEric Cheng 	 */
35268275SEric Cheng 	if (mip->mi_callbacks->mc_callbacks & MC_IOCTL)
35278275SEric Cheng 		mip->mi_ioctl(mip->mi_driver, wq, bp);
35288275SEric Cheng 	else
35298275SEric Cheng 		miocnak(wq, bp, 0, EINVAL);
35308275SEric Cheng }
35318275SEric Cheng 
35328275SEric Cheng /*
35338275SEric Cheng  * Return the link state of the specified MAC instance.
35348275SEric Cheng  */
35358275SEric Cheng link_state_t
35368275SEric Cheng mac_link_get(mac_handle_t mh)
35378275SEric Cheng {
35388275SEric Cheng 	return (((mac_impl_t *)mh)->mi_linkstate);
35398275SEric Cheng }
35408275SEric Cheng 
35418275SEric Cheng /*
35428275SEric Cheng  * Add a mac client specified notification callback. Please see the comments
35438275SEric Cheng  * above mac_callback_add() for general information about mac callback
35448275SEric Cheng  * addition/deletion in the presence of mac callback list walkers
35458275SEric Cheng  */
35468275SEric Cheng mac_notify_handle_t
35478275SEric Cheng mac_notify_add(mac_handle_t mh, mac_notify_t notify_fn, void *arg)
35488275SEric Cheng {
35498275SEric Cheng 	mac_impl_t		*mip = (mac_impl_t *)mh;
35508275SEric Cheng 	mac_notify_cb_t		*mncb;
35518275SEric Cheng 	mac_cb_info_t		*mcbi;
35528275SEric Cheng 
35538275SEric Cheng 	/*
35548275SEric Cheng 	 * Allocate a notify callback structure, fill in the details and
35558275SEric Cheng 	 * use the mac callback list manipulation functions to chain into
35568275SEric Cheng 	 * the list of callbacks.
35578275SEric Cheng 	 */
35588275SEric Cheng 	mncb = kmem_zalloc(sizeof (mac_notify_cb_t), KM_SLEEP);
35598275SEric Cheng 	mncb->mncb_fn = notify_fn;
35608275SEric Cheng 	mncb->mncb_arg = arg;
35618275SEric Cheng 	mncb->mncb_mip = mip;
35628275SEric Cheng 	mncb->mncb_link.mcb_objp = mncb;
35638275SEric Cheng 	mncb->mncb_link.mcb_objsize = sizeof (mac_notify_cb_t);
35648275SEric Cheng 	mncb->mncb_link.mcb_flags = MCB_NOTIFY_CB_T;
35658275SEric Cheng 
35668275SEric Cheng 	mcbi = &mip->mi_notify_cb_info;
35678275SEric Cheng 
35688275SEric Cheng 	i_mac_perim_enter(mip);
35698275SEric Cheng 	mutex_enter(mcbi->mcbi_lockp);
35708275SEric Cheng 
35718275SEric Cheng 	mac_callback_add(&mip->mi_notify_cb_info, &mip->mi_notify_cb_list,
35728275SEric Cheng 	    &mncb->mncb_link);
35738275SEric Cheng 
35748275SEric Cheng 	mutex_exit(mcbi->mcbi_lockp);
35758275SEric Cheng 	i_mac_perim_exit(mip);
35768275SEric Cheng 	return ((mac_notify_handle_t)mncb);
35778275SEric Cheng }
35788275SEric Cheng 
35798275SEric Cheng void
35808275SEric Cheng mac_notify_remove_wait(mac_handle_t mh)
35818275SEric Cheng {
35828275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
35838275SEric Cheng 	mac_cb_info_t	*mcbi = &mip->mi_notify_cb_info;
35848275SEric Cheng 
35858275SEric Cheng 	mutex_enter(mcbi->mcbi_lockp);
35868275SEric Cheng 	mac_callback_remove_wait(&mip->mi_notify_cb_info);
35878275SEric Cheng 	mutex_exit(mcbi->mcbi_lockp);
35888275SEric Cheng }
35898275SEric Cheng 
35908275SEric Cheng /*
35918275SEric Cheng  * Remove a mac client specified notification callback
35928275SEric Cheng  */
35938275SEric Cheng int
35948275SEric Cheng mac_notify_remove(mac_notify_handle_t mnh, boolean_t wait)
35958275SEric Cheng {
35968275SEric Cheng 	mac_notify_cb_t	*mncb = (mac_notify_cb_t *)mnh;
35978275SEric Cheng 	mac_impl_t	*mip = mncb->mncb_mip;
35988275SEric Cheng 	mac_cb_info_t	*mcbi;
35998275SEric Cheng 	int		err = 0;
36008275SEric Cheng 
36018275SEric Cheng 	mcbi = &mip->mi_notify_cb_info;
36028275SEric Cheng 
36038275SEric Cheng 	i_mac_perim_enter(mip);
36048275SEric Cheng 	mutex_enter(mcbi->mcbi_lockp);
36058275SEric Cheng 
36068275SEric Cheng 	ASSERT(mncb->mncb_link.mcb_objp == mncb);
36078275SEric Cheng 	/*
36088275SEric Cheng 	 * If there aren't any list walkers, the remove would succeed
36098275SEric Cheng 	 * inline, else we wait for the deferred remove to complete
36108275SEric Cheng 	 */
36118275SEric Cheng 	if (mac_callback_remove(&mip->mi_notify_cb_info,
36128275SEric Cheng 	    &mip->mi_notify_cb_list, &mncb->mncb_link)) {
36138275SEric Cheng 		kmem_free(mncb, sizeof (mac_notify_cb_t));
36148275SEric Cheng 	} else {
36158275SEric Cheng 		err = EBUSY;
36168275SEric Cheng 	}
36178275SEric Cheng 
36188275SEric Cheng 	mutex_exit(mcbi->mcbi_lockp);
36198275SEric Cheng 	i_mac_perim_exit(mip);
36208275SEric Cheng 
36218275SEric Cheng 	/*
36228275SEric Cheng 	 * If we failed to remove the notification callback and "wait" is set
36238275SEric Cheng 	 * to be B_TRUE, wait for the callback to finish after we exit the
36248275SEric Cheng 	 * mac perimeter.
36258275SEric Cheng 	 */
36268275SEric Cheng 	if (err != 0 && wait) {
36278275SEric Cheng 		mac_notify_remove_wait((mac_handle_t)mip);
36288275SEric Cheng 		return (0);
36298275SEric Cheng 	}
36308275SEric Cheng 
36318275SEric Cheng 	return (err);
36328275SEric Cheng }
36338275SEric Cheng 
36348275SEric Cheng /*
36358275SEric Cheng  * Associate resource management callbacks with the specified MAC
36368275SEric Cheng  * clients.
36378275SEric Cheng  */
36388275SEric Cheng 
36398275SEric Cheng void
36408275SEric Cheng mac_resource_set_common(mac_client_handle_t mch, mac_resource_add_t add,
36418275SEric Cheng     mac_resource_remove_t remove, mac_resource_quiesce_t quiesce,
36428275SEric Cheng     mac_resource_restart_t restart, mac_resource_bind_t bind,
36438275SEric Cheng     void *arg)
36448275SEric Cheng {
36458275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
36468275SEric Cheng 
36478275SEric Cheng 	mcip->mci_resource_add = add;
36488275SEric Cheng 	mcip->mci_resource_remove = remove;
36498275SEric Cheng 	mcip->mci_resource_quiesce = quiesce;
36508275SEric Cheng 	mcip->mci_resource_restart = restart;
36518275SEric Cheng 	mcip->mci_resource_bind = bind;
36528275SEric Cheng 	mcip->mci_resource_arg = arg;
36538275SEric Cheng }
36548275SEric Cheng 
36558275SEric Cheng void
36568275SEric Cheng mac_resource_set(mac_client_handle_t mch, mac_resource_add_t add, void *arg)
36578275SEric Cheng {
36588275SEric Cheng 	/* update the 'resource_add' callback */
36598275SEric Cheng 	mac_resource_set_common(mch, add, NULL, NULL, NULL, NULL, arg);
36608275SEric Cheng }
36618275SEric Cheng 
36628275SEric Cheng /*
36638275SEric Cheng  * Sets up the client resources and enable the polling interface over all the
36648275SEric Cheng  * SRS's and the soft rings of the client
36658275SEric Cheng  */
36668275SEric Cheng void
36678275SEric Cheng mac_client_poll_enable(mac_client_handle_t mch)
36688275SEric Cheng {
36698275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
36708275SEric Cheng 	mac_soft_ring_set_t	*mac_srs;
36718275SEric Cheng 	flow_entry_t		*flent;
36728275SEric Cheng 	int			i;
36738275SEric Cheng 
36748275SEric Cheng 	flent = mcip->mci_flent;
36758275SEric Cheng 	ASSERT(flent != NULL);
36768275SEric Cheng 
367711021SEric.Cheng@Sun.COM 	mcip->mci_state_flags |= MCIS_CLIENT_POLL_CAPABLE;
36788275SEric Cheng 	for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
36798275SEric Cheng 		mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
36808275SEric Cheng 		ASSERT(mac_srs->srs_mcip == mcip);
36818275SEric Cheng 		mac_srs_client_poll_enable(mcip, mac_srs);
36828275SEric Cheng 	}
36838275SEric Cheng }
36848275SEric Cheng 
36858275SEric Cheng /*
36868275SEric Cheng  * Tears down the client resources and disable the polling interface over all
36878275SEric Cheng  * the SRS's and the soft rings of the client
36888275SEric Cheng  */
36898275SEric Cheng void
36908275SEric Cheng mac_client_poll_disable(mac_client_handle_t mch)
36918275SEric Cheng {
36928275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
36938275SEric Cheng 	mac_soft_ring_set_t	*mac_srs;
36948275SEric Cheng 	flow_entry_t		*flent;
36958275SEric Cheng 	int			i;
36968275SEric Cheng 
36978275SEric Cheng 	flent = mcip->mci_flent;
36988275SEric Cheng 	ASSERT(flent != NULL);
36998275SEric Cheng 
370011021SEric.Cheng@Sun.COM 	mcip->mci_state_flags &= ~MCIS_CLIENT_POLL_CAPABLE;
37018275SEric Cheng 	for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
37028275SEric Cheng 		mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
37038275SEric Cheng 		ASSERT(mac_srs->srs_mcip == mcip);
37048275SEric Cheng 		mac_srs_client_poll_disable(mcip, mac_srs);
37058275SEric Cheng 	}
37068275SEric Cheng }
37078275SEric Cheng 
37088275SEric Cheng /*
37098275SEric Cheng  * Associate the CPUs specified by the given property with a MAC client.
37108275SEric Cheng  */
37118275SEric Cheng int
37128275SEric Cheng mac_cpu_set(mac_client_handle_t mch, mac_resource_props_t *mrp)
37138275SEric Cheng {
37148275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
37158275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
37168275SEric Cheng 	int err = 0;
37178275SEric Cheng 
37188275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
37198275SEric Cheng 
372011878SVenu.Iyer@Sun.COM 	if ((err = mac_validate_props(mcip->mci_state_flags & MCIS_IS_VNIC ?
372111878SVenu.Iyer@Sun.COM 	    mcip->mci_upper_mip : mip, mrp)) != 0) {
37228275SEric Cheng 		return (err);
372311878SVenu.Iyer@Sun.COM 	}
37248275SEric Cheng 	if (MCIP_DATAPATH_SETUP(mcip))
37258275SEric Cheng 		mac_flow_modify(mip->mi_flow_tab, mcip->mci_flent, mrp);
37268275SEric Cheng 
37278275SEric Cheng 	mac_update_resources(mrp, MCIP_RESOURCE_PROPS(mcip), B_FALSE);
37288275SEric Cheng 	return (0);
37298275SEric Cheng }
37308275SEric Cheng 
37318275SEric Cheng /*
37328275SEric Cheng  * Apply the specified properties to the specified MAC client.
37338275SEric Cheng  */
37348275SEric Cheng int
37358275SEric Cheng mac_client_set_resources(mac_client_handle_t mch, mac_resource_props_t *mrp)
37368275SEric Cheng {
37378275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
37388275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
37398275SEric Cheng 	int err = 0;
37408275SEric Cheng 
37418275SEric Cheng 	i_mac_perim_enter(mip);
37428275SEric Cheng 
37438275SEric Cheng 	if ((mrp->mrp_mask & MRP_MAXBW) || (mrp->mrp_mask & MRP_PRIORITY)) {
37448275SEric Cheng 		err = mac_resource_ctl_set(mch, mrp);
374510734SEric Cheng 		if (err != 0)
374610734SEric Cheng 			goto done;
37478275SEric Cheng 	}
37488275SEric Cheng 
374911878SVenu.Iyer@Sun.COM 	if (mrp->mrp_mask & (MRP_CPUS|MRP_POOL)) {
37508275SEric Cheng 		err = mac_cpu_set(mch, mrp);
375110734SEric Cheng 		if (err != 0)
375210734SEric Cheng 			goto done;
375310734SEric Cheng 	}
375410734SEric Cheng 
375511878SVenu.Iyer@Sun.COM 	if (mrp->mrp_mask & MRP_PROTECT) {
375610734SEric Cheng 		err = mac_protect_set(mch, mrp);
375711878SVenu.Iyer@Sun.COM 		if (err != 0)
375811878SVenu.Iyer@Sun.COM 			goto done;
375911878SVenu.Iyer@Sun.COM 	}
376011878SVenu.Iyer@Sun.COM 
376111878SVenu.Iyer@Sun.COM 	if ((mrp->mrp_mask & MRP_RX_RINGS) || (mrp->mrp_mask & MRP_TX_RINGS))
376211878SVenu.Iyer@Sun.COM 		err = mac_resource_ctl_set(mch, mrp);
376310734SEric Cheng 
376410734SEric Cheng done:
37658275SEric Cheng 	i_mac_perim_exit(mip);
37668275SEric Cheng 	return (err);
37678275SEric Cheng }
37688275SEric Cheng 
37698275SEric Cheng /*
37708275SEric Cheng  * Return the properties currently associated with the specified MAC client.
37718275SEric Cheng  */
37728275SEric Cheng void
37738275SEric Cheng mac_client_get_resources(mac_client_handle_t mch, mac_resource_props_t *mrp)
37748275SEric Cheng {
37758275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
37768275SEric Cheng 	mac_resource_props_t	*mcip_mrp = MCIP_RESOURCE_PROPS(mcip);
37778275SEric Cheng 
37788275SEric Cheng 	bcopy(mcip_mrp, mrp, sizeof (mac_resource_props_t));
37798275SEric Cheng }
37808275SEric Cheng 
37818275SEric Cheng /*
378211878SVenu.Iyer@Sun.COM  * Return the effective properties currently associated with the specified
378311878SVenu.Iyer@Sun.COM  * MAC client.
378411878SVenu.Iyer@Sun.COM  */
378511878SVenu.Iyer@Sun.COM void
378611878SVenu.Iyer@Sun.COM mac_client_get_effective_resources(mac_client_handle_t mch,
378711878SVenu.Iyer@Sun.COM     mac_resource_props_t *mrp)
378811878SVenu.Iyer@Sun.COM {
378911878SVenu.Iyer@Sun.COM 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
379011878SVenu.Iyer@Sun.COM 	mac_resource_props_t	*mcip_mrp = MCIP_EFFECTIVE_PROPS(mcip);
379111878SVenu.Iyer@Sun.COM 
379211878SVenu.Iyer@Sun.COM 	bcopy(mcip_mrp, mrp, sizeof (mac_resource_props_t));
379311878SVenu.Iyer@Sun.COM }
379411878SVenu.Iyer@Sun.COM 
379511878SVenu.Iyer@Sun.COM /*
37968275SEric Cheng  * Pass a copy of the specified packet to the promiscuous callbacks
37978275SEric Cheng  * of the specified MAC.
37988275SEric Cheng  *
37998275SEric Cheng  * If sender is NULL, the function is being invoked for a packet chain
38008275SEric Cheng  * received from the wire. If sender is non-NULL, it points to
38018275SEric Cheng  * the MAC client from which the packet is being sent.
38028275SEric Cheng  *
38038275SEric Cheng  * The packets are distributed to the promiscuous callbacks as follows:
38048275SEric Cheng  *
38058275SEric Cheng  * - all packets are sent to the MAC_CLIENT_PROMISC_ALL callbacks
38068275SEric Cheng  * - all broadcast and multicast packets are sent to the
38078275SEric Cheng  *   MAC_CLIENT_PROMISC_FILTER and MAC_CLIENT_PROMISC_MULTI.
38088275SEric Cheng  *
38098275SEric Cheng  * The unicast packets of MAC_CLIENT_PROMISC_FILTER callbacks are dispatched
38108275SEric Cheng  * after classification by mac_rx_deliver().
38118275SEric Cheng  */
38128275SEric Cheng 
38138275SEric Cheng static void
38148275SEric Cheng mac_promisc_dispatch_one(mac_promisc_impl_t *mpip, mblk_t *mp,
38158275SEric Cheng     boolean_t loopback)
38168275SEric Cheng {
381710639SDarren.Reed@Sun.COM 	mblk_t *mp_copy, *mp_next;
381810639SDarren.Reed@Sun.COM 
381910639SDarren.Reed@Sun.COM 	if (!mpip->mpi_no_copy || mpip->mpi_strip_vlan_tag) {
382010639SDarren.Reed@Sun.COM 		mp_copy = copymsg(mp);
382110639SDarren.Reed@Sun.COM 		if (mp_copy == NULL)
382210639SDarren.Reed@Sun.COM 			return;
382310639SDarren.Reed@Sun.COM 
382410639SDarren.Reed@Sun.COM 		if (mpip->mpi_strip_vlan_tag) {
382510639SDarren.Reed@Sun.COM 			mp_copy = mac_strip_vlan_tag_chain(mp_copy);
382610639SDarren.Reed@Sun.COM 			if (mp_copy == NULL)
382710639SDarren.Reed@Sun.COM 				return;
382810639SDarren.Reed@Sun.COM 		}
382910639SDarren.Reed@Sun.COM 		mp_next = NULL;
383010639SDarren.Reed@Sun.COM 	} else {
383110639SDarren.Reed@Sun.COM 		mp_copy = mp;
383210639SDarren.Reed@Sun.COM 		mp_next = mp->b_next;
383310639SDarren.Reed@Sun.COM 	}
38348275SEric Cheng 	mp_copy->b_next = NULL;
38358275SEric Cheng 
38368275SEric Cheng 	mpip->mpi_fn(mpip->mpi_arg, NULL, mp_copy, loopback);
383710639SDarren.Reed@Sun.COM 	if (mp_copy == mp)
383810639SDarren.Reed@Sun.COM 		mp->b_next = mp_next;
38398275SEric Cheng }
38408275SEric Cheng 
38418275SEric Cheng /*
38428275SEric Cheng  * Return the VID of a packet. Zero if the packet is not tagged.
38438275SEric Cheng  */
38448275SEric Cheng static uint16_t
38458275SEric Cheng mac_ether_vid(mblk_t *mp)
38468275SEric Cheng {
38478275SEric Cheng 	struct ether_header *eth = (struct ether_header *)mp->b_rptr;
38488275SEric Cheng 
38498275SEric Cheng 	if (ntohs(eth->ether_type) == ETHERTYPE_VLAN) {
38508275SEric Cheng 		struct ether_vlan_header *t_evhp =
38518275SEric Cheng 		    (struct ether_vlan_header *)mp->b_rptr;
38528275SEric Cheng 		return (VLAN_ID(ntohs(t_evhp->ether_tci)));
38538275SEric Cheng 	}
38548275SEric Cheng 
38558275SEric Cheng 	return (0);
38568275SEric Cheng }
38578275SEric Cheng 
38588275SEric Cheng /*
38598275SEric Cheng  * Return whether the specified packet contains a multicast or broadcast
38608275SEric Cheng  * destination MAC address.
38618275SEric Cheng  */
38628275SEric Cheng static boolean_t
38638275SEric Cheng mac_is_mcast(mac_impl_t *mip, mblk_t *mp)
38648275SEric Cheng {
38658275SEric Cheng 	mac_header_info_t hdr_info;
38668275SEric Cheng 
38678275SEric Cheng 	if (mac_header_info((mac_handle_t)mip, mp, &hdr_info) != 0)
38688275SEric Cheng 		return (B_FALSE);
38698275SEric Cheng 	return ((hdr_info.mhi_dsttype == MAC_ADDRTYPE_BROADCAST) ||
38708275SEric Cheng 	    (hdr_info.mhi_dsttype == MAC_ADDRTYPE_MULTICAST));
38718275SEric Cheng }
38728275SEric Cheng 
38738275SEric Cheng /*
38748275SEric Cheng  * Send a copy of an mblk chain to the MAC clients of the specified MAC.
38758275SEric Cheng  * "sender" points to the sender MAC client for outbound packets, and
38768275SEric Cheng  * is set to NULL for inbound packets.
38778275SEric Cheng  */
38788275SEric Cheng void
38798275SEric Cheng mac_promisc_dispatch(mac_impl_t *mip, mblk_t *mp_chain,
38808275SEric Cheng     mac_client_impl_t *sender)
38818275SEric Cheng {
38828275SEric Cheng 	mac_promisc_impl_t *mpip;
38838275SEric Cheng 	mac_cb_t *mcb;
38848275SEric Cheng 	mblk_t *mp;
38858275SEric Cheng 	boolean_t is_mcast, is_sender;
38868275SEric Cheng 
38878275SEric Cheng 	MAC_PROMISC_WALKER_INC(mip);
38888275SEric Cheng 	for (mp = mp_chain; mp != NULL; mp = mp->b_next) {
38898275SEric Cheng 		is_mcast = mac_is_mcast(mip, mp);
38908275SEric Cheng 		/* send packet to interested callbacks */
38918275SEric Cheng 		for (mcb = mip->mi_promisc_list; mcb != NULL;
38928275SEric Cheng 		    mcb = mcb->mcb_nextp) {
38938275SEric Cheng 			mpip = (mac_promisc_impl_t *)mcb->mcb_objp;
38948275SEric Cheng 			is_sender = (mpip->mpi_mcip == sender);
38958275SEric Cheng 
38968275SEric Cheng 			if (is_sender && mpip->mpi_no_tx_loop)
38978275SEric Cheng 				/*
38988275SEric Cheng 				 * The sender doesn't want to receive
38998275SEric Cheng 				 * copies of the packets it sends.
39008275SEric Cheng 				 */
39018275SEric Cheng 				continue;
39028275SEric Cheng 
390310491SRishi.Srivatsavai@Sun.COM 			/* this client doesn't need any packets (bridge) */
390410491SRishi.Srivatsavai@Sun.COM 			if (mpip->mpi_fn == NULL)
390510491SRishi.Srivatsavai@Sun.COM 				continue;
390610491SRishi.Srivatsavai@Sun.COM 
39078275SEric Cheng 			/*
39088275SEric Cheng 			 * For an ethernet MAC, don't displatch a multicast
39098275SEric Cheng 			 * packet to a non-PROMISC_ALL callbacks unless the VID
39108275SEric Cheng 			 * of the packet matches the VID of the client.
39118275SEric Cheng 			 */
39128275SEric Cheng 			if (is_mcast &&
39138275SEric Cheng 			    mpip->mpi_type != MAC_CLIENT_PROMISC_ALL &&
39148275SEric Cheng 			    !mac_client_check_flow_vid(mpip->mpi_mcip,
39158275SEric Cheng 			    mac_ether_vid(mp)))
39168275SEric Cheng 				continue;
39178275SEric Cheng 
39188275SEric Cheng 			if (is_sender ||
39198275SEric Cheng 			    mpip->mpi_type == MAC_CLIENT_PROMISC_ALL ||
39208275SEric Cheng 			    is_mcast)
39218275SEric Cheng 				mac_promisc_dispatch_one(mpip, mp, is_sender);
39228275SEric Cheng 		}
39238275SEric Cheng 	}
39248275SEric Cheng 	MAC_PROMISC_WALKER_DCR(mip);
39258275SEric Cheng }
39268275SEric Cheng 
39278275SEric Cheng void
39288275SEric Cheng mac_promisc_client_dispatch(mac_client_impl_t *mcip, mblk_t *mp_chain)
39298275SEric Cheng {
39308275SEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
39318275SEric Cheng 	mac_promisc_impl_t	*mpip;
39328275SEric Cheng 	boolean_t		is_mcast;
39338275SEric Cheng 	mblk_t			*mp;
39348275SEric Cheng 	mac_cb_t		*mcb;
39358275SEric Cheng 
39368275SEric Cheng 	/*
39378275SEric Cheng 	 * The unicast packets for the MAC client still
39388275SEric Cheng 	 * need to be delivered to the MAC_CLIENT_PROMISC_FILTERED
39398275SEric Cheng 	 * promiscuous callbacks. The broadcast and multicast
39408275SEric Cheng 	 * packets were delivered from mac_rx().
39418275SEric Cheng 	 */
39428275SEric Cheng 	MAC_PROMISC_WALKER_INC(mip);
39438275SEric Cheng 	for (mp = mp_chain; mp != NULL; mp = mp->b_next) {
39448275SEric Cheng 		is_mcast = mac_is_mcast(mip, mp);
39458275SEric Cheng 		for (mcb = mcip->mci_promisc_list; mcb != NULL;
39468275SEric Cheng 		    mcb = mcb->mcb_nextp) {
39478275SEric Cheng 			mpip = (mac_promisc_impl_t *)mcb->mcb_objp;
39488275SEric Cheng 			if (mpip->mpi_type == MAC_CLIENT_PROMISC_FILTERED &&
39498275SEric Cheng 			    !is_mcast) {
39508275SEric Cheng 				mac_promisc_dispatch_one(mpip, mp, B_FALSE);
39518275SEric Cheng 			}
39528275SEric Cheng 		}
39538275SEric Cheng 	}
39548275SEric Cheng 	MAC_PROMISC_WALKER_DCR(mip);
39558275SEric Cheng }
39568275SEric Cheng 
39578275SEric Cheng /*
39588275SEric Cheng  * Return the margin value currently assigned to the specified MAC instance.
39598275SEric Cheng  */
39608275SEric Cheng void
39618275SEric Cheng mac_margin_get(mac_handle_t mh, uint32_t *marginp)
39628275SEric Cheng {
39638275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
39648275SEric Cheng 
39658275SEric Cheng 	rw_enter(&(mip->mi_rw_lock), RW_READER);
39668275SEric Cheng 	*marginp = mip->mi_margin;
39678275SEric Cheng 	rw_exit(&(mip->mi_rw_lock));
39688275SEric Cheng }
39698275SEric Cheng 
39708275SEric Cheng /*
39718275SEric Cheng  * mac_info_get() is used for retrieving the mac_info when a DL_INFO_REQ is
39728275SEric Cheng  * issued before a DL_ATTACH_REQ. we walk the i_mac_impl_hash table and find
39738275SEric Cheng  * the first mac_impl_t with a matching driver name; then we copy its mac_info_t
39748275SEric Cheng  * to the caller. we do all this with i_mac_impl_lock held so the mac_impl_t
39758275SEric Cheng  * cannot disappear while we are accessing it.
39768275SEric Cheng  */
39778275SEric Cheng typedef struct i_mac_info_state_s {
39788275SEric Cheng 	const char	*mi_name;
39798275SEric Cheng 	mac_info_t	*mi_infop;
39808275SEric Cheng } i_mac_info_state_t;
39818275SEric Cheng 
39828275SEric Cheng /*ARGSUSED*/
39838275SEric Cheng static uint_t
39848275SEric Cheng i_mac_info_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
39858275SEric Cheng {
39868275SEric Cheng 	i_mac_info_state_t *statep = arg;
39878275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)val;
39888275SEric Cheng 
39898275SEric Cheng 	if (mip->mi_state_flags & MIS_DISABLED)
39908275SEric Cheng 		return (MH_WALK_CONTINUE);
39918275SEric Cheng 
39928275SEric Cheng 	if (strcmp(statep->mi_name,
39938275SEric Cheng 	    ddi_driver_name(mip->mi_dip)) != 0)
39948275SEric Cheng 		return (MH_WALK_CONTINUE);
39958275SEric Cheng 
39968275SEric Cheng 	statep->mi_infop = &mip->mi_info;
39978275SEric Cheng 	return (MH_WALK_TERMINATE);
39988275SEric Cheng }
39998275SEric Cheng 
40008275SEric Cheng boolean_t
40018275SEric Cheng mac_info_get(const char *name, mac_info_t *minfop)
40028275SEric Cheng {
40038275SEric Cheng 	i_mac_info_state_t state;
40048275SEric Cheng 
40058275SEric Cheng 	rw_enter(&i_mac_impl_lock, RW_READER);
40068275SEric Cheng 	state.mi_name = name;
40078275SEric Cheng 	state.mi_infop = NULL;
40088275SEric Cheng 	mod_hash_walk(i_mac_impl_hash, i_mac_info_walker, &state);
40098275SEric Cheng 	if (state.mi_infop == NULL) {
40108275SEric Cheng 		rw_exit(&i_mac_impl_lock);
40118275SEric Cheng 		return (B_FALSE);
40128275SEric Cheng 	}
40138275SEric Cheng 	*minfop = *state.mi_infop;
40148275SEric Cheng 	rw_exit(&i_mac_impl_lock);
40158275SEric Cheng 	return (B_TRUE);
40168275SEric Cheng }
40178275SEric Cheng 
40188275SEric Cheng /*
40198275SEric Cheng  * To get the capabilities that MAC layer cares about, such as rings, factory
402010491SRishi.Srivatsavai@Sun.COM  * mac address, vnic or not, it should directly invoke this function.  If the
402110491SRishi.Srivatsavai@Sun.COM  * link is part of a bridge, then the only "capability" it has is the inability
402210491SRishi.Srivatsavai@Sun.COM  * to do zero copy.
40238275SEric Cheng  */
40248275SEric Cheng boolean_t
40258275SEric Cheng i_mac_capab_get(mac_handle_t mh, mac_capab_t cap, void *cap_data)
40268275SEric Cheng {
40278275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
40288275SEric Cheng 
402910491SRishi.Srivatsavai@Sun.COM 	if (mip->mi_bridge_link != NULL)
403010491SRishi.Srivatsavai@Sun.COM 		return (cap == MAC_CAPAB_NO_ZCOPY);
403110491SRishi.Srivatsavai@Sun.COM 	else if (mip->mi_callbacks->mc_callbacks & MC_GETCAPAB)
40328275SEric Cheng 		return (mip->mi_getcapab(mip->mi_driver, cap, cap_data));
40338275SEric Cheng 	else
40348275SEric Cheng 		return (B_FALSE);
40358275SEric Cheng }
40368275SEric Cheng 
40378275SEric Cheng /*
40388275SEric Cheng  * Capability query function. If number of active mac clients is greater than
40398275SEric Cheng  * 1, only limited capabilities can be advertised to the caller no matter the
40408275SEric Cheng  * driver has certain capability or not. Else, we query the driver to get the
40418275SEric Cheng  * capability.
40428275SEric Cheng  */
40438275SEric Cheng boolean_t
40448275SEric Cheng mac_capab_get(mac_handle_t mh, mac_capab_t cap, void *cap_data)
40458275SEric Cheng {
40468275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
40478275SEric Cheng 
40488275SEric Cheng 	/*
40499073SCathy.Zhou@Sun.COM 	 * if mi_nactiveclients > 1, only MAC_CAPAB_LEGACY, MAC_CAPAB_HCKSUM,
40509073SCathy.Zhou@Sun.COM 	 * MAC_CAPAB_NO_NATIVEVLAN and MAC_CAPAB_NO_ZCOPY can be advertised.
40518275SEric Cheng 	 */
40528275SEric Cheng 	if (mip->mi_nactiveclients > 1) {
40538275SEric Cheng 		switch (cap) {
40548275SEric Cheng 		case MAC_CAPAB_NO_ZCOPY:
40558275SEric Cheng 			return (B_TRUE);
40569073SCathy.Zhou@Sun.COM 		case MAC_CAPAB_LEGACY:
40579073SCathy.Zhou@Sun.COM 		case MAC_CAPAB_HCKSUM:
4058*12325SCathy.Zhou@Sun.COM 		case MAC_CAPAB_NO_NATIVEVLAN:
40599073SCathy.Zhou@Sun.COM 			break;
40608275SEric Cheng 		default:
40618275SEric Cheng 			return (B_FALSE);
40628275SEric Cheng 		}
40638275SEric Cheng 	}
40648275SEric Cheng 
40658275SEric Cheng 	/* else get capab from driver */
40668275SEric Cheng 	return (i_mac_capab_get(mh, cap, cap_data));
40678275SEric Cheng }
40688275SEric Cheng 
40698275SEric Cheng boolean_t
40708275SEric Cheng mac_sap_verify(mac_handle_t mh, uint32_t sap, uint32_t *bind_sap)
40718275SEric Cheng {
40728275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
40738275SEric Cheng 
40748275SEric Cheng 	return (mip->mi_type->mt_ops.mtops_sap_verify(sap, bind_sap,
40758275SEric Cheng 	    mip->mi_pdata));
40768275SEric Cheng }
40778275SEric Cheng 
40788275SEric Cheng mblk_t *
40798275SEric Cheng mac_header(mac_handle_t mh, const uint8_t *daddr, uint32_t sap, mblk_t *payload,
40808275SEric Cheng     size_t extra_len)
40818275SEric Cheng {
408210616SSebastien.Roy@Sun.COM 	mac_impl_t	*mip = (mac_impl_t *)mh;
408310616SSebastien.Roy@Sun.COM 	const uint8_t	*hdr_daddr;
408410616SSebastien.Roy@Sun.COM 
408510616SSebastien.Roy@Sun.COM 	/*
408610616SSebastien.Roy@Sun.COM 	 * If the MAC is point-to-point with a fixed destination address, then
408710616SSebastien.Roy@Sun.COM 	 * we must always use that destination in the MAC header.
408810616SSebastien.Roy@Sun.COM 	 */
408910616SSebastien.Roy@Sun.COM 	hdr_daddr = (mip->mi_dstaddr_set ? mip->mi_dstaddr : daddr);
409010616SSebastien.Roy@Sun.COM 	return (mip->mi_type->mt_ops.mtops_header(mip->mi_addr, hdr_daddr, sap,
40918275SEric Cheng 	    mip->mi_pdata, payload, extra_len));
40928275SEric Cheng }
40938275SEric Cheng 
40948275SEric Cheng int
40958275SEric Cheng mac_header_info(mac_handle_t mh, mblk_t *mp, mac_header_info_t *mhip)
40968275SEric Cheng {
40978275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
40988275SEric Cheng 
40998275SEric Cheng 	return (mip->mi_type->mt_ops.mtops_header_info(mp, mip->mi_pdata,
41008275SEric Cheng 	    mhip));
41018275SEric Cheng }
41028275SEric Cheng 
410310734SEric Cheng int
410410734SEric Cheng mac_vlan_header_info(mac_handle_t mh, mblk_t *mp, mac_header_info_t *mhip)
410510734SEric Cheng {
410610734SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
410710734SEric Cheng 	boolean_t	is_ethernet = (mip->mi_info.mi_media == DL_ETHER);
410810734SEric Cheng 	int		err = 0;
410910734SEric Cheng 
411010734SEric Cheng 	/*
411110734SEric Cheng 	 * Packets should always be at least 16 bit aligned.
411210734SEric Cheng 	 */
411310734SEric Cheng 	ASSERT(IS_P2ALIGNED(mp->b_rptr, sizeof (uint16_t)));
411410734SEric Cheng 
411510734SEric Cheng 	if ((err = mac_header_info(mh, mp, mhip)) != 0)
411610734SEric Cheng 		return (err);
411710734SEric Cheng 
411810734SEric Cheng 	/*
411910734SEric Cheng 	 * If this is a VLAN-tagged Ethernet packet, then the SAP in the
412010734SEric Cheng 	 * mac_header_info_t as returned by mac_header_info() is
412110734SEric Cheng 	 * ETHERTYPE_VLAN. We need to grab the ethertype from the VLAN header.
412210734SEric Cheng 	 */
412310734SEric Cheng 	if (is_ethernet && (mhip->mhi_bindsap == ETHERTYPE_VLAN)) {
412410734SEric Cheng 		struct ether_vlan_header *evhp;
412510734SEric Cheng 		uint16_t sap;
412610734SEric Cheng 		mblk_t *tmp = NULL;
412710734SEric Cheng 		size_t size;
412810734SEric Cheng 
412910734SEric Cheng 		size = sizeof (struct ether_vlan_header);
413010734SEric Cheng 		if (MBLKL(mp) < size) {
413110734SEric Cheng 			/*
413210734SEric Cheng 			 * Pullup the message in order to get the MAC header
413310734SEric Cheng 			 * infomation. Note that this is a read-only function,
413410734SEric Cheng 			 * we keep the input packet intact.
413510734SEric Cheng 			 */
413610734SEric Cheng 			if ((tmp = msgpullup(mp, size)) == NULL)
413710734SEric Cheng 				return (EINVAL);
413810734SEric Cheng 
413910734SEric Cheng 			mp = tmp;
414010734SEric Cheng 		}
414110734SEric Cheng 		evhp = (struct ether_vlan_header *)mp->b_rptr;
414210734SEric Cheng 		sap = ntohs(evhp->ether_type);
414310734SEric Cheng 		(void) mac_sap_verify(mh, sap, &mhip->mhi_bindsap);
414410734SEric Cheng 		mhip->mhi_hdrsize = sizeof (struct ether_vlan_header);
414510734SEric Cheng 		mhip->mhi_tci = ntohs(evhp->ether_tci);
414610734SEric Cheng 		mhip->mhi_istagged = B_TRUE;
414710734SEric Cheng 		freemsg(tmp);
414810734SEric Cheng 
414910734SEric Cheng 		if (VLAN_CFI(mhip->mhi_tci) != ETHER_CFI)
415010734SEric Cheng 			return (EINVAL);
415110734SEric Cheng 	} else {
415210734SEric Cheng 		mhip->mhi_istagged = B_FALSE;
415310734SEric Cheng 		mhip->mhi_tci = 0;
415410734SEric Cheng 	}
415510734SEric Cheng 
415610734SEric Cheng 	return (0);
415710734SEric Cheng }
415810734SEric Cheng 
41598275SEric Cheng mblk_t *
41608275SEric Cheng mac_header_cook(mac_handle_t mh, mblk_t *mp)
41618275SEric Cheng {
41628275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
41638275SEric Cheng 
41648275SEric Cheng 	if (mip->mi_type->mt_ops.mtops_ops & MTOPS_HEADER_COOK) {
41658275SEric Cheng 		if (DB_REF(mp) > 1) {
41668275SEric Cheng 			mblk_t *newmp = copymsg(mp);
41678275SEric Cheng 			if (newmp == NULL)
41688275SEric Cheng 				return (NULL);
41698275SEric Cheng 			freemsg(mp);
41708275SEric Cheng 			mp = newmp;
41718275SEric Cheng 		}
41728275SEric Cheng 		return (mip->mi_type->mt_ops.mtops_header_cook(mp,
41738275SEric Cheng 		    mip->mi_pdata));
41748275SEric Cheng 	}
41758275SEric Cheng 	return (mp);
41768275SEric Cheng }
41778275SEric Cheng 
41788275SEric Cheng mblk_t *
41798275SEric Cheng mac_header_uncook(mac_handle_t mh, mblk_t *mp)
41808275SEric Cheng {
41818275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
41828275SEric Cheng 
41838275SEric Cheng 	if (mip->mi_type->mt_ops.mtops_ops & MTOPS_HEADER_UNCOOK) {
41848275SEric Cheng 		if (DB_REF(mp) > 1) {
41858275SEric Cheng 			mblk_t *newmp = copymsg(mp);
41868275SEric Cheng 			if (newmp == NULL)
41878275SEric Cheng 				return (NULL);
41888275SEric Cheng 			freemsg(mp);
41898275SEric Cheng 			mp = newmp;
41908275SEric Cheng 		}
41918275SEric Cheng 		return (mip->mi_type->mt_ops.mtops_header_uncook(mp,
41928275SEric Cheng 		    mip->mi_pdata));
41938275SEric Cheng 	}
41948275SEric Cheng 	return (mp);
41958275SEric Cheng }
41968275SEric Cheng 
41978275SEric Cheng uint_t
41988275SEric Cheng mac_addr_len(mac_handle_t mh)
41998275SEric Cheng {
42008275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
42018275SEric Cheng 
42028275SEric Cheng 	return (mip->mi_type->mt_addr_length);
42038275SEric Cheng }
42048275SEric Cheng 
42058275SEric Cheng /* True if a MAC is a VNIC */
42068275SEric Cheng boolean_t
42078275SEric Cheng mac_is_vnic(mac_handle_t mh)
42088275SEric Cheng {
42098275SEric Cheng 	return (((mac_impl_t *)mh)->mi_state_flags & MIS_IS_VNIC);
42108275SEric Cheng }
42118275SEric Cheng 
42128275SEric Cheng mac_handle_t
42138275SEric Cheng mac_get_lower_mac_handle(mac_handle_t mh)
42148275SEric Cheng {
42158275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
42168275SEric Cheng 
42178275SEric Cheng 	ASSERT(mac_is_vnic(mh));
42188275SEric Cheng 	return (((vnic_t *)mip->mi_driver)->vn_lower_mh);
42198275SEric Cheng }
42208275SEric Cheng 
422111878SVenu.Iyer@Sun.COM boolean_t
422211878SVenu.Iyer@Sun.COM mac_is_vnic_primary(mac_handle_t mh)
422311878SVenu.Iyer@Sun.COM {
422411878SVenu.Iyer@Sun.COM 	mac_impl_t *mip = (mac_impl_t *)mh;
422511878SVenu.Iyer@Sun.COM 
422611878SVenu.Iyer@Sun.COM 	ASSERT(mac_is_vnic(mh));
422711878SVenu.Iyer@Sun.COM 	return (((vnic_t *)mip->mi_driver)->vn_addr_type ==
422811878SVenu.Iyer@Sun.COM 	    VNIC_MAC_ADDR_TYPE_PRIMARY);
422911878SVenu.Iyer@Sun.COM }
423011878SVenu.Iyer@Sun.COM 
42318275SEric Cheng void
42328275SEric Cheng mac_update_resources(mac_resource_props_t *nmrp, mac_resource_props_t *cmrp,
42338275SEric Cheng     boolean_t is_user_flow)
42348275SEric Cheng {
42358275SEric Cheng 	if (nmrp != NULL && cmrp != NULL) {
42368275SEric Cheng 		if (nmrp->mrp_mask & MRP_PRIORITY) {
42378275SEric Cheng 			if (nmrp->mrp_priority == MPL_RESET) {
42388275SEric Cheng 				cmrp->mrp_mask &= ~MRP_PRIORITY;
42398275SEric Cheng 				if (is_user_flow) {
42408275SEric Cheng 					cmrp->mrp_priority =
42418275SEric Cheng 					    MPL_SUBFLOW_DEFAULT;
42428275SEric Cheng 				} else {
42438275SEric Cheng 					cmrp->mrp_priority = MPL_LINK_DEFAULT;
42448275SEric Cheng 				}
42458275SEric Cheng 			} else {
42468275SEric Cheng 				cmrp->mrp_mask |= MRP_PRIORITY;
42478275SEric Cheng 				cmrp->mrp_priority = nmrp->mrp_priority;
42488275SEric Cheng 			}
42498275SEric Cheng 		}
42508275SEric Cheng 		if (nmrp->mrp_mask & MRP_MAXBW) {
425111878SVenu.Iyer@Sun.COM 			if (nmrp->mrp_maxbw == MRP_MAXBW_RESETVAL) {
42528275SEric Cheng 				cmrp->mrp_mask &= ~MRP_MAXBW;
425311878SVenu.Iyer@Sun.COM 				cmrp->mrp_maxbw = 0;
425411878SVenu.Iyer@Sun.COM 			} else {
42558275SEric Cheng 				cmrp->mrp_mask |= MRP_MAXBW;
425611878SVenu.Iyer@Sun.COM 				cmrp->mrp_maxbw = nmrp->mrp_maxbw;
425711878SVenu.Iyer@Sun.COM 			}
42588275SEric Cheng 		}
42598275SEric Cheng 		if (nmrp->mrp_mask & MRP_CPUS)
42608275SEric Cheng 			MAC_COPY_CPUS(nmrp, cmrp);
426110734SEric Cheng 
426211878SVenu.Iyer@Sun.COM 		if (nmrp->mrp_mask & MRP_POOL) {
426311878SVenu.Iyer@Sun.COM 			if (strlen(nmrp->mrp_pool) == 0) {
426411878SVenu.Iyer@Sun.COM 				cmrp->mrp_mask &= ~MRP_POOL;
426511878SVenu.Iyer@Sun.COM 				bzero(cmrp->mrp_pool, sizeof (cmrp->mrp_pool));
426611878SVenu.Iyer@Sun.COM 			} else {
426711878SVenu.Iyer@Sun.COM 				cmrp->mrp_mask |= MRP_POOL;
426811878SVenu.Iyer@Sun.COM 				(void) strncpy(cmrp->mrp_pool, nmrp->mrp_pool,
426911878SVenu.Iyer@Sun.COM 				    sizeof (cmrp->mrp_pool));
427011878SVenu.Iyer@Sun.COM 			}
427111878SVenu.Iyer@Sun.COM 
427211878SVenu.Iyer@Sun.COM 		}
427311878SVenu.Iyer@Sun.COM 
427410734SEric Cheng 		if (nmrp->mrp_mask & MRP_PROTECT)
427510734SEric Cheng 			mac_protect_update(nmrp, cmrp);
427611878SVenu.Iyer@Sun.COM 
427711878SVenu.Iyer@Sun.COM 		/*
427811878SVenu.Iyer@Sun.COM 		 * Update the rings specified.
427911878SVenu.Iyer@Sun.COM 		 */
428011878SVenu.Iyer@Sun.COM 		if (nmrp->mrp_mask & MRP_RX_RINGS) {
428111878SVenu.Iyer@Sun.COM 			if (nmrp->mrp_mask & MRP_RINGS_RESET) {
428211878SVenu.Iyer@Sun.COM 				cmrp->mrp_mask &= ~MRP_RX_RINGS;
428311878SVenu.Iyer@Sun.COM 				if (cmrp->mrp_mask & MRP_RXRINGS_UNSPEC)
428411878SVenu.Iyer@Sun.COM 					cmrp->mrp_mask &= ~MRP_RXRINGS_UNSPEC;
428511878SVenu.Iyer@Sun.COM 				cmrp->mrp_nrxrings = 0;
428611878SVenu.Iyer@Sun.COM 			} else {
428711878SVenu.Iyer@Sun.COM 				cmrp->mrp_mask |= MRP_RX_RINGS;
428811878SVenu.Iyer@Sun.COM 				cmrp->mrp_nrxrings = nmrp->mrp_nrxrings;
428911878SVenu.Iyer@Sun.COM 			}
429011878SVenu.Iyer@Sun.COM 		}
429111878SVenu.Iyer@Sun.COM 		if (nmrp->mrp_mask & MRP_TX_RINGS) {
429211878SVenu.Iyer@Sun.COM 			if (nmrp->mrp_mask & MRP_RINGS_RESET) {
429311878SVenu.Iyer@Sun.COM 				cmrp->mrp_mask &= ~MRP_TX_RINGS;
429411878SVenu.Iyer@Sun.COM 				if (cmrp->mrp_mask & MRP_TXRINGS_UNSPEC)
429511878SVenu.Iyer@Sun.COM 					cmrp->mrp_mask &= ~MRP_TXRINGS_UNSPEC;
429611878SVenu.Iyer@Sun.COM 				cmrp->mrp_ntxrings = 0;
429711878SVenu.Iyer@Sun.COM 			} else {
429811878SVenu.Iyer@Sun.COM 				cmrp->mrp_mask |= MRP_TX_RINGS;
429911878SVenu.Iyer@Sun.COM 				cmrp->mrp_ntxrings = nmrp->mrp_ntxrings;
430011878SVenu.Iyer@Sun.COM 			}
430111878SVenu.Iyer@Sun.COM 		}
430211878SVenu.Iyer@Sun.COM 		if (nmrp->mrp_mask & MRP_RXRINGS_UNSPEC)
430311878SVenu.Iyer@Sun.COM 			cmrp->mrp_mask |= MRP_RXRINGS_UNSPEC;
430411878SVenu.Iyer@Sun.COM 		else if (cmrp->mrp_mask & MRP_RXRINGS_UNSPEC)
430511878SVenu.Iyer@Sun.COM 			cmrp->mrp_mask &= ~MRP_RXRINGS_UNSPEC;
430611878SVenu.Iyer@Sun.COM 
430711878SVenu.Iyer@Sun.COM 		if (nmrp->mrp_mask & MRP_TXRINGS_UNSPEC)
430811878SVenu.Iyer@Sun.COM 			cmrp->mrp_mask |= MRP_TXRINGS_UNSPEC;
430911878SVenu.Iyer@Sun.COM 		else if (cmrp->mrp_mask & MRP_TXRINGS_UNSPEC)
431011878SVenu.Iyer@Sun.COM 			cmrp->mrp_mask &= ~MRP_TXRINGS_UNSPEC;
43118275SEric Cheng 	}
43128275SEric Cheng }
43138275SEric Cheng 
43148275SEric Cheng /*
43158275SEric Cheng  * i_mac_set_resources:
43168275SEric Cheng  *
43178275SEric Cheng  * This routine associates properties with the primary MAC client of
43188275SEric Cheng  * the specified MAC instance.
43198275SEric Cheng  * - Cache the properties in mac_impl_t
43208275SEric Cheng  * - Apply the properties to the primary MAC client if exists
43218275SEric Cheng  */
43228275SEric Cheng int
43238275SEric Cheng i_mac_set_resources(mac_handle_t mh, mac_resource_props_t *mrp)
43248275SEric Cheng {
43258275SEric Cheng 	mac_impl_t		*mip = (mac_impl_t *)mh;
43268275SEric Cheng 	mac_client_impl_t	*mcip;
43278275SEric Cheng 	int			err = 0;
43289073SCathy.Zhou@Sun.COM 	uint32_t		resmask, newresmask;
432911878SVenu.Iyer@Sun.COM 	mac_resource_props_t	*tmrp, *umrp;
43308275SEric Cheng 
43318275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
43328275SEric Cheng 
433311878SVenu.Iyer@Sun.COM 	err = mac_validate_props(mip, mrp);
43348275SEric Cheng 	if (err != 0)
43358275SEric Cheng 		return (err);
43368275SEric Cheng 
433711878SVenu.Iyer@Sun.COM 	umrp = kmem_zalloc(sizeof (*umrp), KM_SLEEP);
433811878SVenu.Iyer@Sun.COM 	bcopy(&mip->mi_resource_props, umrp, sizeof (*umrp));
433911878SVenu.Iyer@Sun.COM 	resmask = umrp->mrp_mask;
434011878SVenu.Iyer@Sun.COM 	mac_update_resources(mrp, umrp, B_FALSE);
434111878SVenu.Iyer@Sun.COM 	newresmask = umrp->mrp_mask;
43429073SCathy.Zhou@Sun.COM 
43439073SCathy.Zhou@Sun.COM 	if (resmask == 0 && newresmask != 0) {
43449073SCathy.Zhou@Sun.COM 		/*
434511878SVenu.Iyer@Sun.COM 		 * Bandwidth, priority, cpu or pool link properties configured,
43469073SCathy.Zhou@Sun.COM 		 * must disable fastpath.
43479073SCathy.Zhou@Sun.COM 		 */
434811878SVenu.Iyer@Sun.COM 		if ((err = mac_fastpath_disable((mac_handle_t)mip)) != 0) {
434911878SVenu.Iyer@Sun.COM 			kmem_free(umrp, sizeof (*umrp));
43509073SCathy.Zhou@Sun.COM 			return (err);
435111878SVenu.Iyer@Sun.COM 		}
43529073SCathy.Zhou@Sun.COM 	}
43539073SCathy.Zhou@Sun.COM 
43548275SEric Cheng 	/*
43558275SEric Cheng 	 * Since bind_cpu may be modified by mac_client_set_resources()
43568275SEric Cheng 	 * we use a copy of bind_cpu and finally cache bind_cpu in mip.
43578275SEric Cheng 	 * This allows us to cache only user edits in mip.
43588275SEric Cheng 	 */
435911878SVenu.Iyer@Sun.COM 	tmrp = kmem_zalloc(sizeof (*tmrp), KM_SLEEP);
436011878SVenu.Iyer@Sun.COM 	bcopy(mrp, tmrp, sizeof (*tmrp));
43618275SEric Cheng 	mcip = mac_primary_client_handle(mip);
43628833SVenu.Iyer@Sun.COM 	if (mcip != NULL && (mcip->mci_state_flags & MCIS_IS_AGGR_PORT) == 0) {
436311878SVenu.Iyer@Sun.COM 		err = mac_client_set_resources((mac_client_handle_t)mcip, tmrp);
436411878SVenu.Iyer@Sun.COM 	} else if ((mrp->mrp_mask & MRP_RX_RINGS ||
436511878SVenu.Iyer@Sun.COM 	    mrp->mrp_mask & MRP_TX_RINGS)) {
436611878SVenu.Iyer@Sun.COM 		mac_client_impl_t	*vmcip;
436711878SVenu.Iyer@Sun.COM 
436811878SVenu.Iyer@Sun.COM 		/*
436911878SVenu.Iyer@Sun.COM 		 * If the primary is not up, we need to check if there
437011878SVenu.Iyer@Sun.COM 		 * are any VLANs on this primary. If there are then
437111878SVenu.Iyer@Sun.COM 		 * we need to set this property on the VLANs since
437211878SVenu.Iyer@Sun.COM 		 * VLANs follow the primary they are based on. Just
437311878SVenu.Iyer@Sun.COM 		 * look for the first VLAN and change its properties,
437411878SVenu.Iyer@Sun.COM 		 * all the other VLANs should be in the same group.
437511878SVenu.Iyer@Sun.COM 		 */
437611878SVenu.Iyer@Sun.COM 		for (vmcip = mip->mi_clients_list; vmcip != NULL;
437711878SVenu.Iyer@Sun.COM 		    vmcip = vmcip->mci_client_next) {
437811878SVenu.Iyer@Sun.COM 			if ((vmcip->mci_flent->fe_type & FLOW_PRIMARY_MAC) &&
437911878SVenu.Iyer@Sun.COM 			    mac_client_vid((mac_client_handle_t)vmcip) !=
438011878SVenu.Iyer@Sun.COM 			    VLAN_ID_NONE) {
438111878SVenu.Iyer@Sun.COM 				break;
438211878SVenu.Iyer@Sun.COM 			}
438311878SVenu.Iyer@Sun.COM 		}
438411878SVenu.Iyer@Sun.COM 		if (vmcip != NULL) {
438511878SVenu.Iyer@Sun.COM 			mac_resource_props_t	*omrp;
438611878SVenu.Iyer@Sun.COM 			mac_resource_props_t	*vmrp;
438711878SVenu.Iyer@Sun.COM 
438811878SVenu.Iyer@Sun.COM 			omrp = kmem_zalloc(sizeof (*omrp), KM_SLEEP);
438911878SVenu.Iyer@Sun.COM 			bcopy(MCIP_RESOURCE_PROPS(vmcip), omrp, sizeof (*omrp));
439011878SVenu.Iyer@Sun.COM 			/*
439111878SVenu.Iyer@Sun.COM 			 * We dont' call mac_update_resources since we
439211878SVenu.Iyer@Sun.COM 			 * want to take only the ring properties and
439311878SVenu.Iyer@Sun.COM 			 * not all the properties that may have changed.
439411878SVenu.Iyer@Sun.COM 			 */
439511878SVenu.Iyer@Sun.COM 			vmrp = MCIP_RESOURCE_PROPS(vmcip);
439611878SVenu.Iyer@Sun.COM 			if (mrp->mrp_mask & MRP_RX_RINGS) {
439711878SVenu.Iyer@Sun.COM 				if (mrp->mrp_mask & MRP_RINGS_RESET) {
439811878SVenu.Iyer@Sun.COM 					vmrp->mrp_mask &= ~MRP_RX_RINGS;
439911878SVenu.Iyer@Sun.COM 					if (vmrp->mrp_mask &
440011878SVenu.Iyer@Sun.COM 					    MRP_RXRINGS_UNSPEC) {
440111878SVenu.Iyer@Sun.COM 						vmrp->mrp_mask &=
440211878SVenu.Iyer@Sun.COM 						    ~MRP_RXRINGS_UNSPEC;
440311878SVenu.Iyer@Sun.COM 					}
440411878SVenu.Iyer@Sun.COM 					vmrp->mrp_nrxrings = 0;
440511878SVenu.Iyer@Sun.COM 				} else {
440611878SVenu.Iyer@Sun.COM 					vmrp->mrp_mask |= MRP_RX_RINGS;
440711878SVenu.Iyer@Sun.COM 					vmrp->mrp_nrxrings = mrp->mrp_nrxrings;
440811878SVenu.Iyer@Sun.COM 				}
440911878SVenu.Iyer@Sun.COM 			}
441011878SVenu.Iyer@Sun.COM 			if (mrp->mrp_mask & MRP_TX_RINGS) {
441111878SVenu.Iyer@Sun.COM 				if (mrp->mrp_mask & MRP_RINGS_RESET) {
441211878SVenu.Iyer@Sun.COM 					vmrp->mrp_mask &= ~MRP_TX_RINGS;
441311878SVenu.Iyer@Sun.COM 					if (vmrp->mrp_mask &
441411878SVenu.Iyer@Sun.COM 					    MRP_TXRINGS_UNSPEC) {
441511878SVenu.Iyer@Sun.COM 						vmrp->mrp_mask &=
441611878SVenu.Iyer@Sun.COM 						    ~MRP_TXRINGS_UNSPEC;
441711878SVenu.Iyer@Sun.COM 					}
441811878SVenu.Iyer@Sun.COM 					vmrp->mrp_ntxrings = 0;
441911878SVenu.Iyer@Sun.COM 				} else {
442011878SVenu.Iyer@Sun.COM 					vmrp->mrp_mask |= MRP_TX_RINGS;
442111878SVenu.Iyer@Sun.COM 					vmrp->mrp_ntxrings = mrp->mrp_ntxrings;
442211878SVenu.Iyer@Sun.COM 				}
442311878SVenu.Iyer@Sun.COM 			}
442411878SVenu.Iyer@Sun.COM 			if (mrp->mrp_mask & MRP_RXRINGS_UNSPEC)
442511878SVenu.Iyer@Sun.COM 				vmrp->mrp_mask |= MRP_RXRINGS_UNSPEC;
442611878SVenu.Iyer@Sun.COM 
442711878SVenu.Iyer@Sun.COM 			if (mrp->mrp_mask & MRP_TXRINGS_UNSPEC)
442811878SVenu.Iyer@Sun.COM 				vmrp->mrp_mask |= MRP_TXRINGS_UNSPEC;
442911878SVenu.Iyer@Sun.COM 
443011878SVenu.Iyer@Sun.COM 			if ((err = mac_client_set_rings_prop(vmcip, mrp,
443111878SVenu.Iyer@Sun.COM 			    omrp)) != 0) {
443211878SVenu.Iyer@Sun.COM 				bcopy(omrp, MCIP_RESOURCE_PROPS(vmcip),
443311878SVenu.Iyer@Sun.COM 				    sizeof (*omrp));
443411878SVenu.Iyer@Sun.COM 			} else {
443511878SVenu.Iyer@Sun.COM 				mac_set_prim_vlan_rings(mip, vmrp);
443611878SVenu.Iyer@Sun.COM 			}
443711878SVenu.Iyer@Sun.COM 			kmem_free(omrp, sizeof (*omrp));
443811878SVenu.Iyer@Sun.COM 		}
44398275SEric Cheng 	}
44409073SCathy.Zhou@Sun.COM 
44419073SCathy.Zhou@Sun.COM 	/* Only update the values if mac_client_set_resources succeeded */
44429073SCathy.Zhou@Sun.COM 	if (err == 0) {
444311878SVenu.Iyer@Sun.COM 		bcopy(umrp, &mip->mi_resource_props, sizeof (*umrp));
44449073SCathy.Zhou@Sun.COM 		/*
444511878SVenu.Iyer@Sun.COM 		 * If bandwidth, priority or cpu link properties cleared,
44469073SCathy.Zhou@Sun.COM 		 * renable fastpath.
44479073SCathy.Zhou@Sun.COM 		 */
44489073SCathy.Zhou@Sun.COM 		if (resmask != 0 && newresmask == 0)
44499073SCathy.Zhou@Sun.COM 			mac_fastpath_enable((mac_handle_t)mip);
44509073SCathy.Zhou@Sun.COM 	} else if (resmask == 0 && newresmask != 0) {
44519073SCathy.Zhou@Sun.COM 		mac_fastpath_enable((mac_handle_t)mip);
44529073SCathy.Zhou@Sun.COM 	}
445311878SVenu.Iyer@Sun.COM 	kmem_free(tmrp, sizeof (*tmrp));
445411878SVenu.Iyer@Sun.COM 	kmem_free(umrp, sizeof (*umrp));
44558275SEric Cheng 	return (err);
44568275SEric Cheng }
44578275SEric Cheng 
44588275SEric Cheng int
44598275SEric Cheng mac_set_resources(mac_handle_t mh, mac_resource_props_t *mrp)
44608275SEric Cheng {
44618275SEric Cheng 	int err;
44628275SEric Cheng 
44638275SEric Cheng 	i_mac_perim_enter((mac_impl_t *)mh);
44648275SEric Cheng 	err = i_mac_set_resources(mh, mrp);
44658275SEric Cheng 	i_mac_perim_exit((mac_impl_t *)mh);
44668275SEric Cheng 	return (err);
44678275SEric Cheng }
44688275SEric Cheng 
44698275SEric Cheng /*
44708275SEric Cheng  * Get the properties cached for the specified MAC instance.
44718275SEric Cheng  */
44728275SEric Cheng void
44738275SEric Cheng mac_get_resources(mac_handle_t mh, mac_resource_props_t *mrp)
44748275SEric Cheng {
44758275SEric Cheng 	mac_impl_t 		*mip = (mac_impl_t *)mh;
44768275SEric Cheng 	mac_client_impl_t	*mcip;
44778275SEric Cheng 
447811878SVenu.Iyer@Sun.COM 	mcip = mac_primary_client_handle(mip);
447911878SVenu.Iyer@Sun.COM 	if (mcip != NULL) {
448011878SVenu.Iyer@Sun.COM 		mac_client_get_resources((mac_client_handle_t)mcip, mrp);
448111878SVenu.Iyer@Sun.COM 		return;
44828275SEric Cheng 	}
44838275SEric Cheng 	bcopy(&mip->mi_resource_props, mrp, sizeof (mac_resource_props_t));
44848275SEric Cheng }
44858275SEric Cheng 
448611878SVenu.Iyer@Sun.COM /*
448711878SVenu.Iyer@Sun.COM  * Get the effective properties from the primary client of the
448811878SVenu.Iyer@Sun.COM  * specified MAC instance.
448911878SVenu.Iyer@Sun.COM  */
449011878SVenu.Iyer@Sun.COM void
449111878SVenu.Iyer@Sun.COM mac_get_effective_resources(mac_handle_t mh, mac_resource_props_t *mrp)
449211878SVenu.Iyer@Sun.COM {
449311878SVenu.Iyer@Sun.COM 	mac_impl_t 		*mip = (mac_impl_t *)mh;
449411878SVenu.Iyer@Sun.COM 	mac_client_impl_t	*mcip;
449511878SVenu.Iyer@Sun.COM 
449611878SVenu.Iyer@Sun.COM 	mcip = mac_primary_client_handle(mip);
449711878SVenu.Iyer@Sun.COM 	if (mcip != NULL) {
449811878SVenu.Iyer@Sun.COM 		mac_client_get_effective_resources((mac_client_handle_t)mcip,
449911878SVenu.Iyer@Sun.COM 		    mrp);
450011878SVenu.Iyer@Sun.COM 		return;
450111878SVenu.Iyer@Sun.COM 	}
450211878SVenu.Iyer@Sun.COM 	bzero(mrp, sizeof (mac_resource_props_t));
450311878SVenu.Iyer@Sun.COM }
450411878SVenu.Iyer@Sun.COM 
450510491SRishi.Srivatsavai@Sun.COM int
450610491SRishi.Srivatsavai@Sun.COM mac_set_pvid(mac_handle_t mh, uint16_t pvid)
450710491SRishi.Srivatsavai@Sun.COM {
450810491SRishi.Srivatsavai@Sun.COM 	mac_impl_t *mip = (mac_impl_t *)mh;
450910491SRishi.Srivatsavai@Sun.COM 	mac_client_impl_t *mcip;
451010491SRishi.Srivatsavai@Sun.COM 	mac_unicast_impl_t *muip;
451110491SRishi.Srivatsavai@Sun.COM 
451210491SRishi.Srivatsavai@Sun.COM 	i_mac_perim_enter(mip);
451310491SRishi.Srivatsavai@Sun.COM 	if (pvid != 0) {
451410491SRishi.Srivatsavai@Sun.COM 		for (mcip = mip->mi_clients_list; mcip != NULL;
451510491SRishi.Srivatsavai@Sun.COM 		    mcip = mcip->mci_client_next) {
451610491SRishi.Srivatsavai@Sun.COM 			for (muip = mcip->mci_unicast_list; muip != NULL;
451710491SRishi.Srivatsavai@Sun.COM 			    muip = muip->mui_next) {
451810491SRishi.Srivatsavai@Sun.COM 				if (muip->mui_vid == pvid) {
451910491SRishi.Srivatsavai@Sun.COM 					i_mac_perim_exit(mip);
452010491SRishi.Srivatsavai@Sun.COM 					return (EBUSY);
452110491SRishi.Srivatsavai@Sun.COM 				}
452210491SRishi.Srivatsavai@Sun.COM 			}
452310491SRishi.Srivatsavai@Sun.COM 		}
452410491SRishi.Srivatsavai@Sun.COM 	}
452510491SRishi.Srivatsavai@Sun.COM 	mip->mi_pvid = pvid;
452610491SRishi.Srivatsavai@Sun.COM 	i_mac_perim_exit(mip);
452710491SRishi.Srivatsavai@Sun.COM 	return (0);
452810491SRishi.Srivatsavai@Sun.COM }
452910491SRishi.Srivatsavai@Sun.COM 
453010491SRishi.Srivatsavai@Sun.COM uint16_t
453110491SRishi.Srivatsavai@Sun.COM mac_get_pvid(mac_handle_t mh)
453210491SRishi.Srivatsavai@Sun.COM {
453310491SRishi.Srivatsavai@Sun.COM 	mac_impl_t *mip = (mac_impl_t *)mh;
453410491SRishi.Srivatsavai@Sun.COM 
453510491SRishi.Srivatsavai@Sun.COM 	return (mip->mi_pvid);
453610491SRishi.Srivatsavai@Sun.COM }
453710491SRishi.Srivatsavai@Sun.COM 
453810491SRishi.Srivatsavai@Sun.COM uint32_t
453910491SRishi.Srivatsavai@Sun.COM mac_get_llimit(mac_handle_t mh)
454010491SRishi.Srivatsavai@Sun.COM {
454110491SRishi.Srivatsavai@Sun.COM 	mac_impl_t *mip = (mac_impl_t *)mh;
454210491SRishi.Srivatsavai@Sun.COM 
454310491SRishi.Srivatsavai@Sun.COM 	return (mip->mi_llimit);
454410491SRishi.Srivatsavai@Sun.COM }
454510491SRishi.Srivatsavai@Sun.COM 
454610491SRishi.Srivatsavai@Sun.COM uint32_t
454710491SRishi.Srivatsavai@Sun.COM mac_get_ldecay(mac_handle_t mh)
454810491SRishi.Srivatsavai@Sun.COM {
454910491SRishi.Srivatsavai@Sun.COM 	mac_impl_t *mip = (mac_impl_t *)mh;
455010491SRishi.Srivatsavai@Sun.COM 
455110491SRishi.Srivatsavai@Sun.COM 	return (mip->mi_ldecay);
455210491SRishi.Srivatsavai@Sun.COM }
455310491SRishi.Srivatsavai@Sun.COM 
45548275SEric Cheng /*
45558275SEric Cheng  * Rename a mac client, its flow, and the kstat.
45568275SEric Cheng  */
45578275SEric Cheng int
45588275SEric Cheng mac_rename_primary(mac_handle_t mh, const char *new_name)
45598275SEric Cheng {
45608275SEric Cheng 	mac_impl_t		*mip = (mac_impl_t *)mh;
45618275SEric Cheng 	mac_client_impl_t	*cur_clnt = NULL;
45628275SEric Cheng 	flow_entry_t		*fep;
45638275SEric Cheng 
45648275SEric Cheng 	i_mac_perim_enter(mip);
45658275SEric Cheng 
45668275SEric Cheng 	/*
45678275SEric Cheng 	 * VNICs: we need to change the sys flow name and
45688275SEric Cheng 	 * the associated flow kstat.
45698275SEric Cheng 	 */
45708275SEric Cheng 	if (mip->mi_state_flags & MIS_IS_VNIC) {
457111878SVenu.Iyer@Sun.COM 		mac_client_impl_t *mcip = mac_vnic_lower(mip);
45728275SEric Cheng 		ASSERT(new_name != NULL);
457311878SVenu.Iyer@Sun.COM 		mac_rename_flow_names(mcip, new_name);
457411878SVenu.Iyer@Sun.COM 		mac_stat_rename(mcip);
45758275SEric Cheng 		goto done;
45768275SEric Cheng 	}
45778275SEric Cheng 	/*
45788275SEric Cheng 	 * This mac may itself be an aggr link, or it may have some client
45798275SEric Cheng 	 * which is an aggr port. For both cases, we need to change the
45808275SEric Cheng 	 * aggr port's mac client name, its flow name and the associated flow
45818275SEric Cheng 	 * kstat.
45828275SEric Cheng 	 */
45838275SEric Cheng 	if (mip->mi_state_flags & MIS_IS_AGGR) {
45848275SEric Cheng 		mac_capab_aggr_t aggr_cap;
45858275SEric Cheng 		mac_rename_fn_t rename_fn;
45868275SEric Cheng 		boolean_t ret;
45878275SEric Cheng 
45888275SEric Cheng 		ASSERT(new_name != NULL);
45898275SEric Cheng 		ret = i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_AGGR,
45908275SEric Cheng 		    (void *)(&aggr_cap));
45918275SEric Cheng 		ASSERT(ret == B_TRUE);
45928275SEric Cheng 		rename_fn = aggr_cap.mca_rename_fn;
45938275SEric Cheng 		rename_fn(new_name, mip->mi_driver);
45948275SEric Cheng 		/*
45958275SEric Cheng 		 * The aggr's client name and kstat flow name will be
45968275SEric Cheng 		 * updated below, i.e. via mac_rename_flow_names.
45978275SEric Cheng 		 */
45988275SEric Cheng 	}
45998275SEric Cheng 
46008275SEric Cheng 	for (cur_clnt = mip->mi_clients_list; cur_clnt != NULL;
46018275SEric Cheng 	    cur_clnt = cur_clnt->mci_client_next) {
46028275SEric Cheng 		if (cur_clnt->mci_state_flags & MCIS_IS_AGGR_PORT) {
46038275SEric Cheng 			if (new_name != NULL) {
46048275SEric Cheng 				char *str_st = cur_clnt->mci_name;
46058275SEric Cheng 				char *str_del = strchr(str_st, '-');
46068275SEric Cheng 
46078275SEric Cheng 				ASSERT(str_del != NULL);
46088275SEric Cheng 				bzero(str_del + 1, MAXNAMELEN -
46098275SEric Cheng 				    (str_del - str_st + 1));
46108275SEric Cheng 				bcopy(new_name, str_del + 1,
46118275SEric Cheng 				    strlen(new_name));
46128275SEric Cheng 			}
46138275SEric Cheng 			fep = cur_clnt->mci_flent;
46148275SEric Cheng 			mac_rename_flow(fep, cur_clnt->mci_name);
46158275SEric Cheng 			break;
46168275SEric Cheng 		} else if (new_name != NULL &&
46178275SEric Cheng 		    cur_clnt->mci_state_flags & MCIS_USE_DATALINK_NAME) {
46188275SEric Cheng 			mac_rename_flow_names(cur_clnt, new_name);
46198275SEric Cheng 			break;
46208275SEric Cheng 		}
46218275SEric Cheng 	}
46228275SEric Cheng 
462311878SVenu.Iyer@Sun.COM 	/* Recreate kstats associated with aggr pseudo rings */
462411878SVenu.Iyer@Sun.COM 	if (mip->mi_state_flags & MIS_IS_AGGR)
462511878SVenu.Iyer@Sun.COM 		mac_pseudo_ring_stat_rename(mip);
462611878SVenu.Iyer@Sun.COM 
46278275SEric Cheng done:
46288275SEric Cheng 	i_mac_perim_exit(mip);
46298275SEric Cheng 	return (0);
46308275SEric Cheng }
46318275SEric Cheng 
46328275SEric Cheng /*
46338275SEric Cheng  * Rename the MAC client's flow names
46348275SEric Cheng  */
46358275SEric Cheng static void
46368275SEric Cheng mac_rename_flow_names(mac_client_impl_t *mcip, const char *new_name)
46378275SEric Cheng {
46388275SEric Cheng 	flow_entry_t	*flent;
46398275SEric Cheng 	uint16_t	vid;
46408558SGirish.Moodalbail@Sun.COM 	char		flowname[MAXFLOWNAMELEN];
46418275SEric Cheng 	mac_impl_t	*mip = mcip->mci_mip;
46428275SEric Cheng 
46438275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
46448275SEric Cheng 
46458275SEric Cheng 	/*
46468275SEric Cheng 	 * Use mi_rw_lock to ensure that threads not in the mac perimeter
46478275SEric Cheng 	 * see a self-consistent value for mci_name
46488275SEric Cheng 	 */
46498275SEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
46508275SEric Cheng 	(void) strlcpy(mcip->mci_name, new_name, sizeof (mcip->mci_name));
46518275SEric Cheng 	rw_exit(&mip->mi_rw_lock);
46528275SEric Cheng 
46538275SEric Cheng 	mac_rename_flow(mcip->mci_flent, new_name);
46548275SEric Cheng 
46558275SEric Cheng 	if (mcip->mci_nflents == 1)
46568275SEric Cheng 		return;
46578275SEric Cheng 
46588275SEric Cheng 	/*
46598275SEric Cheng 	 * We have to rename all the others too, no stats to destroy for
46608275SEric Cheng 	 * these.
46618275SEric Cheng 	 */
46628275SEric Cheng 	for (flent = mcip->mci_flent_list; flent != NULL;
46638275SEric Cheng 	    flent = flent->fe_client_next) {
46648275SEric Cheng 		if (flent != mcip->mci_flent) {
46658275SEric Cheng 			vid = i_mac_flow_vid(flent);
46668275SEric Cheng 			(void) sprintf(flowname, "%s%u", new_name, vid);
46678275SEric Cheng 			mac_flow_set_name(flent, flowname);
46688275SEric Cheng 		}
46698275SEric Cheng 	}
46708275SEric Cheng }
46718275SEric Cheng 
46728275SEric Cheng 
46738275SEric Cheng /*
46748275SEric Cheng  * Add a flow to the MAC client's flow list - i.e list of MAC/VID tuples
46758275SEric Cheng  * defined for the specified MAC client.
46768275SEric Cheng  */
46778275SEric Cheng static void
46788275SEric Cheng mac_client_add_to_flow_list(mac_client_impl_t *mcip, flow_entry_t *flent)
46798275SEric Cheng {
46808275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
46818275SEric Cheng 	/*
46828275SEric Cheng 	 * The promisc Rx data path walks the mci_flent_list. Protect by
46838275SEric Cheng 	 * using mi_rw_lock
46848275SEric Cheng 	 */
46858275SEric Cheng 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
46868275SEric Cheng 
46878275SEric Cheng 	/* Add it to the head */
46888275SEric Cheng 	flent->fe_client_next = mcip->mci_flent_list;
46898275SEric Cheng 	mcip->mci_flent_list = flent;
46908275SEric Cheng 	mcip->mci_nflents++;
46918275SEric Cheng 
46928275SEric Cheng 	/*
46938275SEric Cheng 	 * Keep track of the number of non-zero VIDs addresses per MAC
46948275SEric Cheng 	 * client to avoid figuring it out in the data-path.
46958275SEric Cheng 	 */
46968275SEric Cheng 	if (i_mac_flow_vid(flent) != VLAN_ID_NONE)
46978275SEric Cheng 		mcip->mci_nvids++;
46988275SEric Cheng 
46998275SEric Cheng 	rw_exit(&mcip->mci_rw_lock);
47008275SEric Cheng }
47018275SEric Cheng 
47028275SEric Cheng /*
47038275SEric Cheng  * Remove a flow entry from the MAC client's list.
47048275SEric Cheng  */
47058275SEric Cheng static void
47068275SEric Cheng mac_client_remove_flow_from_list(mac_client_impl_t *mcip, flow_entry_t *flent)
47078275SEric Cheng {
47088275SEric Cheng 	flow_entry_t	*fe = mcip->mci_flent_list;
47098275SEric Cheng 	flow_entry_t	*prev_fe = NULL;
47108275SEric Cheng 
47118275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
47128275SEric Cheng 	/*
47138275SEric Cheng 	 * The promisc Rx data path walks the mci_flent_list. Protect by
47148275SEric Cheng 	 * using mci_rw_lock
47158275SEric Cheng 	 */
47168275SEric Cheng 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
47178275SEric Cheng 	while ((fe != NULL) && (fe != flent)) {
47188275SEric Cheng 		prev_fe = fe;
47198275SEric Cheng 		fe = fe->fe_client_next;
47208275SEric Cheng 	}
47218275SEric Cheng 
47228558SGirish.Moodalbail@Sun.COM 	ASSERT(fe != NULL);
47238558SGirish.Moodalbail@Sun.COM 	if (prev_fe == NULL) {
47248558SGirish.Moodalbail@Sun.COM 		/* Deleting the first node */
47258558SGirish.Moodalbail@Sun.COM 		mcip->mci_flent_list = fe->fe_client_next;
47268558SGirish.Moodalbail@Sun.COM 	} else {
47278558SGirish.Moodalbail@Sun.COM 		prev_fe->fe_client_next = fe->fe_client_next;
47288275SEric Cheng 	}
47298558SGirish.Moodalbail@Sun.COM 	mcip->mci_nflents--;
47308558SGirish.Moodalbail@Sun.COM 
47318558SGirish.Moodalbail@Sun.COM 	if (i_mac_flow_vid(flent) != VLAN_ID_NONE)
47328558SGirish.Moodalbail@Sun.COM 		mcip->mci_nvids--;
47338558SGirish.Moodalbail@Sun.COM 
47348275SEric Cheng 	rw_exit(&mcip->mci_rw_lock);
47358275SEric Cheng }
47368275SEric Cheng 
47378275SEric Cheng /*
47388275SEric Cheng  * Check if the given VID belongs to this MAC client.
47398275SEric Cheng  */
47408275SEric Cheng boolean_t
47418275SEric Cheng mac_client_check_flow_vid(mac_client_impl_t *mcip, uint16_t vid)
47428275SEric Cheng {
47438275SEric Cheng 	flow_entry_t	*flent;
47448275SEric Cheng 	uint16_t	mci_vid;
47458275SEric Cheng 
47468275SEric Cheng 	/* The mci_flent_list is protected by mci_rw_lock */
47478275SEric Cheng 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
47488275SEric Cheng 	for (flent = mcip->mci_flent_list; flent != NULL;
47498275SEric Cheng 	    flent = flent->fe_client_next) {
47508275SEric Cheng 		mci_vid = i_mac_flow_vid(flent);
47518275SEric Cheng 		if (vid == mci_vid) {
47528275SEric Cheng 			rw_exit(&mcip->mci_rw_lock);
47538275SEric Cheng 			return (B_TRUE);
47548275SEric Cheng 		}
47558275SEric Cheng 	}
47568275SEric Cheng 	rw_exit(&mcip->mci_rw_lock);
47578275SEric Cheng 	return (B_FALSE);
47588275SEric Cheng }
47598275SEric Cheng 
47608275SEric Cheng /*
47618275SEric Cheng  * Get the flow entry for the specified <MAC addr, VID> tuple.
47628275SEric Cheng  */
47638275SEric Cheng static flow_entry_t *
47648275SEric Cheng mac_client_get_flow(mac_client_impl_t *mcip, mac_unicast_impl_t *muip)
47658275SEric Cheng {
47668275SEric Cheng 	mac_address_t *map = mcip->mci_unicast;
47678275SEric Cheng 	flow_entry_t *flent;
47688275SEric Cheng 	uint16_t vid;
47698275SEric Cheng 	flow_desc_t flow_desc;
47708275SEric Cheng 
47718275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
47728275SEric Cheng 
47738275SEric Cheng 	mac_flow_get_desc(mcip->mci_flent, &flow_desc);
47748275SEric Cheng 	if (bcmp(flow_desc.fd_dst_mac, map->ma_addr, map->ma_len) != 0)
47758275SEric Cheng 		return (NULL);
47768275SEric Cheng 
47778275SEric Cheng 	for (flent = mcip->mci_flent_list; flent != NULL;
47788275SEric Cheng 	    flent = flent->fe_client_next) {
47798275SEric Cheng 		vid = i_mac_flow_vid(flent);
47808275SEric Cheng 		if (vid == muip->mui_vid) {
47818275SEric Cheng 			return (flent);
47828275SEric Cheng 		}
47838275SEric Cheng 	}
47848275SEric Cheng 
47858275SEric Cheng 	return (NULL);
47868275SEric Cheng }
47878275SEric Cheng 
47888275SEric Cheng /*
47898275SEric Cheng  * Since mci_flent has the SRSs, when we want to remove it, we replace
47908275SEric Cheng  * the flow_desc_t in mci_flent with that of an existing flent and then
47918275SEric Cheng  * remove that flent instead of mci_flent.
47928275SEric Cheng  */
47938275SEric Cheng static flow_entry_t *
47948275SEric Cheng mac_client_swap_mciflent(mac_client_impl_t *mcip)
47958275SEric Cheng {
47968275SEric Cheng 	flow_entry_t	*flent = mcip->mci_flent;
47978275SEric Cheng 	flow_tab_t	*ft = flent->fe_flow_tab;
47988275SEric Cheng 	flow_entry_t	*flent1;
47998275SEric Cheng 	flow_desc_t	fl_desc;
48008558SGirish.Moodalbail@Sun.COM 	char		fl_name[MAXFLOWNAMELEN];
48018275SEric Cheng 	int		err;
48028275SEric Cheng 
48038275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
48048275SEric Cheng 	ASSERT(mcip->mci_nflents > 1);
48058275SEric Cheng 
48068275SEric Cheng 	/* get the next flent following the primary flent  */
48078275SEric Cheng 	flent1 = mcip->mci_flent_list->fe_client_next;
48088275SEric Cheng 	ASSERT(flent1 != NULL && flent1->fe_flow_tab == ft);
48098275SEric Cheng 
48108275SEric Cheng 	/*
48118275SEric Cheng 	 * Remove the flent from the flow table before updating the
48128275SEric Cheng 	 * flow descriptor as the hash depends on the flow descriptor.
48138275SEric Cheng 	 * This also helps incoming packet classification avoid having
48148275SEric Cheng 	 * to grab fe_lock. Access to fe_flow_desc of a flent not in the
48158275SEric Cheng 	 * flow table is done under the fe_lock so that log or stat functions
48168275SEric Cheng 	 * see a self-consistent fe_flow_desc. The name and desc are specific
48178275SEric Cheng 	 * to a flow, the rest are shared by all the clients, including
48188275SEric Cheng 	 * resource control etc.
48198275SEric Cheng 	 */
48208275SEric Cheng 	mac_flow_remove(ft, flent, B_TRUE);
48218275SEric Cheng 	mac_flow_remove(ft, flent1, B_TRUE);
48228275SEric Cheng 
48238275SEric Cheng 	bcopy(&flent->fe_flow_desc, &fl_desc, sizeof (flow_desc_t));
48248558SGirish.Moodalbail@Sun.COM 	bcopy(flent->fe_flow_name, fl_name, MAXFLOWNAMELEN);
48258275SEric Cheng 
48268275SEric Cheng 	/* update the primary flow entry */
48278275SEric Cheng 	mutex_enter(&flent->fe_lock);
48288275SEric Cheng 	bcopy(&flent1->fe_flow_desc, &flent->fe_flow_desc,
48298275SEric Cheng 	    sizeof (flow_desc_t));
48308558SGirish.Moodalbail@Sun.COM 	bcopy(&flent1->fe_flow_name, &flent->fe_flow_name, MAXFLOWNAMELEN);
48318275SEric Cheng 	mutex_exit(&flent->fe_lock);
48328275SEric Cheng 
48338275SEric Cheng 	/* update the flow entry that is to be freed */
48348275SEric Cheng 	mutex_enter(&flent1->fe_lock);
48358275SEric Cheng 	bcopy(&fl_desc, &flent1->fe_flow_desc, sizeof (flow_desc_t));
48368558SGirish.Moodalbail@Sun.COM 	bcopy(fl_name, &flent1->fe_flow_name, MAXFLOWNAMELEN);
48378275SEric Cheng 	mutex_exit(&flent1->fe_lock);
48388275SEric Cheng 
48398275SEric Cheng 	/* now reinsert the flow entries in the table */
48408275SEric Cheng 	err = mac_flow_add(ft, flent);
48418275SEric Cheng 	ASSERT(err == 0);
48428275SEric Cheng 
48438275SEric Cheng 	err = mac_flow_add(ft, flent1);
48448275SEric Cheng 	ASSERT(err == 0);
48458275SEric Cheng 
48468275SEric Cheng 	return (flent1);
48478275SEric Cheng }
48488275SEric Cheng 
48498275SEric Cheng /*
48508275SEric Cheng  * Return whether there is only one flow entry associated with this
48518275SEric Cheng  * MAC client.
48528275SEric Cheng  */
48538275SEric Cheng static boolean_t
48548275SEric Cheng mac_client_single_rcvr(mac_client_impl_t *mcip)
48558275SEric Cheng {
48568275SEric Cheng 	return (mcip->mci_nflents == 1);
48578275SEric Cheng }
48588275SEric Cheng 
48598275SEric Cheng int
486011878SVenu.Iyer@Sun.COM mac_validate_props(mac_impl_t *mip, mac_resource_props_t *mrp)
48618275SEric Cheng {
486211878SVenu.Iyer@Sun.COM 	boolean_t		reset;
486311878SVenu.Iyer@Sun.COM 	uint32_t		rings_needed;
486411878SVenu.Iyer@Sun.COM 	uint32_t		rings_avail;
486511878SVenu.Iyer@Sun.COM 	mac_group_type_t	gtype;
486611878SVenu.Iyer@Sun.COM 	mac_resource_props_t	*mip_mrp;
486711878SVenu.Iyer@Sun.COM 
48688275SEric Cheng 	if (mrp == NULL)
48698275SEric Cheng 		return (0);
48708275SEric Cheng 
48718275SEric Cheng 	if (mrp->mrp_mask & MRP_PRIORITY) {
48728275SEric Cheng 		mac_priority_level_t	pri = mrp->mrp_priority;
48738275SEric Cheng 
48748275SEric Cheng 		if (pri < MPL_LOW || pri > MPL_RESET)
48758275SEric Cheng 			return (EINVAL);
48768275SEric Cheng 	}
48778275SEric Cheng 
48788275SEric Cheng 	if (mrp->mrp_mask & MRP_MAXBW) {
48798275SEric Cheng 		uint64_t maxbw = mrp->mrp_maxbw;
48808275SEric Cheng 
48818275SEric Cheng 		if (maxbw < MRP_MAXBW_MINVAL && maxbw != 0)
48828275SEric Cheng 			return (EINVAL);
48838275SEric Cheng 	}
48848275SEric Cheng 	if (mrp->mrp_mask & MRP_CPUS) {
48859060SNitin.Hande@Sun.COM 		int i, j;
48868275SEric Cheng 		mac_cpu_mode_t	fanout;
48878275SEric Cheng 
488812062SRajagopal.Kunhappan@Sun.COM 		if (mrp->mrp_ncpus > ncpus)
48898275SEric Cheng 			return (EINVAL);
48908275SEric Cheng 
48918275SEric Cheng 		for (i = 0; i < mrp->mrp_ncpus; i++) {
48929060SNitin.Hande@Sun.COM 			for (j = 0; j < mrp->mrp_ncpus; j++) {
48939060SNitin.Hande@Sun.COM 				if (i != j &&
48949060SNitin.Hande@Sun.COM 				    mrp->mrp_cpu[i] == mrp->mrp_cpu[j]) {
48959060SNitin.Hande@Sun.COM 					return (EINVAL);
48969060SNitin.Hande@Sun.COM 				}
48979060SNitin.Hande@Sun.COM 			}
48989060SNitin.Hande@Sun.COM 		}
48999060SNitin.Hande@Sun.COM 
49009060SNitin.Hande@Sun.COM 		for (i = 0; i < mrp->mrp_ncpus; i++) {
49018275SEric Cheng 			cpu_t *cp;
49028275SEric Cheng 			int rv;
49038275SEric Cheng 
49048275SEric Cheng 			mutex_enter(&cpu_lock);
49058275SEric Cheng 			cp = cpu_get(mrp->mrp_cpu[i]);
49068275SEric Cheng 			if (cp != NULL)
49078275SEric Cheng 				rv = cpu_is_online(cp);
49088275SEric Cheng 			else
49098275SEric Cheng 				rv = 0;
49108275SEric Cheng 			mutex_exit(&cpu_lock);
49118275SEric Cheng 			if (rv == 0)
49128275SEric Cheng 				return (EINVAL);
49138275SEric Cheng 		}
49148275SEric Cheng 
49158275SEric Cheng 		fanout = mrp->mrp_fanout_mode;
49168275SEric Cheng 		if (fanout < 0 || fanout > MCM_CPUS)
49178275SEric Cheng 			return (EINVAL);
49188275SEric Cheng 	}
491910734SEric Cheng 
492010734SEric Cheng 	if (mrp->mrp_mask & MRP_PROTECT) {
492110734SEric Cheng 		int err = mac_protect_validate(mrp);
492210734SEric Cheng 		if (err != 0)
492310734SEric Cheng 			return (err);
492410734SEric Cheng 	}
492511878SVenu.Iyer@Sun.COM 
492611878SVenu.Iyer@Sun.COM 	if (!(mrp->mrp_mask & MRP_RX_RINGS) &&
492711878SVenu.Iyer@Sun.COM 	    !(mrp->mrp_mask & MRP_TX_RINGS)) {
492811878SVenu.Iyer@Sun.COM 		return (0);
492911878SVenu.Iyer@Sun.COM 	}
493011878SVenu.Iyer@Sun.COM 
493111878SVenu.Iyer@Sun.COM 	/*
493211878SVenu.Iyer@Sun.COM 	 * mip will be null when we come from mac_flow_create or
493311878SVenu.Iyer@Sun.COM 	 * mac_link_flow_modify. In the latter case it is a user flow,
493411878SVenu.Iyer@Sun.COM 	 * for which we don't support rings. In the former we would
493511878SVenu.Iyer@Sun.COM 	 * have validated the props beforehand (i_mac_unicast_add ->
493611878SVenu.Iyer@Sun.COM 	 * mac_client_set_resources -> validate for the primary and
493711878SVenu.Iyer@Sun.COM 	 * vnic_dev_create -> mac_client_set_resources -> validate for
493811878SVenu.Iyer@Sun.COM 	 * a vnic.
493911878SVenu.Iyer@Sun.COM 	 */
494011878SVenu.Iyer@Sun.COM 	if (mip == NULL)
494111878SVenu.Iyer@Sun.COM 		return (0);
494211878SVenu.Iyer@Sun.COM 
494311878SVenu.Iyer@Sun.COM 	/*
494411878SVenu.Iyer@Sun.COM 	 * We don't support setting rings property for a VNIC that is using a
494511878SVenu.Iyer@Sun.COM 	 * primary address (VLAN)
494611878SVenu.Iyer@Sun.COM 	 */
494711878SVenu.Iyer@Sun.COM 	if ((mip->mi_state_flags & MIS_IS_VNIC) &&
494811878SVenu.Iyer@Sun.COM 	    mac_is_vnic_primary((mac_handle_t)mip)) {
494911878SVenu.Iyer@Sun.COM 		return (ENOTSUP);
495011878SVenu.Iyer@Sun.COM 	}
495111878SVenu.Iyer@Sun.COM 
495211878SVenu.Iyer@Sun.COM 	mip_mrp = &mip->mi_resource_props;
495311878SVenu.Iyer@Sun.COM 	/*
495411878SVenu.Iyer@Sun.COM 	 * The rings property should be validated against the NICs
495511878SVenu.Iyer@Sun.COM 	 * resources
495611878SVenu.Iyer@Sun.COM 	 */
495711878SVenu.Iyer@Sun.COM 	if (mip->mi_state_flags & MIS_IS_VNIC)
495811878SVenu.Iyer@Sun.COM 		mip = (mac_impl_t *)mac_get_lower_mac_handle((mac_handle_t)mip);
495911878SVenu.Iyer@Sun.COM 
496011878SVenu.Iyer@Sun.COM 	reset = mrp->mrp_mask & MRP_RINGS_RESET;
496111878SVenu.Iyer@Sun.COM 	/*
496211878SVenu.Iyer@Sun.COM 	 * If groups are not supported, return error.
496311878SVenu.Iyer@Sun.COM 	 */
496411878SVenu.Iyer@Sun.COM 	if (((mrp->mrp_mask & MRP_RX_RINGS) && mip->mi_rx_groups == NULL) ||
496511878SVenu.Iyer@Sun.COM 	    ((mrp->mrp_mask & MRP_TX_RINGS) && mip->mi_tx_groups == NULL)) {
496611878SVenu.Iyer@Sun.COM 		return (EINVAL);
496711878SVenu.Iyer@Sun.COM 	}
496811878SVenu.Iyer@Sun.COM 	/*
496911878SVenu.Iyer@Sun.COM 	 * If we are just resetting, there is no validation needed.
497011878SVenu.Iyer@Sun.COM 	 */
497111878SVenu.Iyer@Sun.COM 	if (reset)
497211878SVenu.Iyer@Sun.COM 		return (0);
497311878SVenu.Iyer@Sun.COM 
497411878SVenu.Iyer@Sun.COM 	if (mrp->mrp_mask & MRP_RX_RINGS) {
497511878SVenu.Iyer@Sun.COM 		rings_needed = mrp->mrp_nrxrings;
497611878SVenu.Iyer@Sun.COM 		/*
497711878SVenu.Iyer@Sun.COM 		 * We just want to check if the number of additional
497811878SVenu.Iyer@Sun.COM 		 * rings requested is available.
497911878SVenu.Iyer@Sun.COM 		 */
498011878SVenu.Iyer@Sun.COM 		if (mip_mrp->mrp_mask & MRP_RX_RINGS) {
498111878SVenu.Iyer@Sun.COM 			if (mrp->mrp_nrxrings > mip_mrp->mrp_nrxrings)
498211878SVenu.Iyer@Sun.COM 				/* Just check for the additional rings */
498311878SVenu.Iyer@Sun.COM 				rings_needed -= mip_mrp->mrp_nrxrings;
498411878SVenu.Iyer@Sun.COM 			else
498511878SVenu.Iyer@Sun.COM 				/* We are not asking for additional rings */
498611878SVenu.Iyer@Sun.COM 				rings_needed = 0;
498711878SVenu.Iyer@Sun.COM 		}
498811878SVenu.Iyer@Sun.COM 		rings_avail = mip->mi_rxrings_avail;
498911878SVenu.Iyer@Sun.COM 		gtype = mip->mi_rx_group_type;
499011878SVenu.Iyer@Sun.COM 	} else {
499111878SVenu.Iyer@Sun.COM 		rings_needed = mrp->mrp_ntxrings;
499211878SVenu.Iyer@Sun.COM 		/* Similarly for the TX rings */
499311878SVenu.Iyer@Sun.COM 		if (mip_mrp->mrp_mask & MRP_TX_RINGS) {
499411878SVenu.Iyer@Sun.COM 			if (mrp->mrp_ntxrings > mip_mrp->mrp_ntxrings)
499511878SVenu.Iyer@Sun.COM 				/* Just check for the additional rings */
499611878SVenu.Iyer@Sun.COM 				rings_needed -= mip_mrp->mrp_ntxrings;
499711878SVenu.Iyer@Sun.COM 			else
499811878SVenu.Iyer@Sun.COM 				/* We are not asking for additional rings */
499911878SVenu.Iyer@Sun.COM 				rings_needed = 0;
500011878SVenu.Iyer@Sun.COM 		}
500111878SVenu.Iyer@Sun.COM 		rings_avail = mip->mi_txrings_avail;
500211878SVenu.Iyer@Sun.COM 		gtype = mip->mi_tx_group_type;
500311878SVenu.Iyer@Sun.COM 	}
500411878SVenu.Iyer@Sun.COM 
500511878SVenu.Iyer@Sun.COM 	/* Error if the group is dynamic .. */
500611878SVenu.Iyer@Sun.COM 	if (gtype == MAC_GROUP_TYPE_DYNAMIC) {
500711878SVenu.Iyer@Sun.COM 		/*
500811878SVenu.Iyer@Sun.COM 		 * .. and rings specified are more than available.
500911878SVenu.Iyer@Sun.COM 		 */
501011878SVenu.Iyer@Sun.COM 		if (rings_needed > rings_avail)
501111878SVenu.Iyer@Sun.COM 			return (EINVAL);
501211878SVenu.Iyer@Sun.COM 	} else {
501311878SVenu.Iyer@Sun.COM 		/*
501411878SVenu.Iyer@Sun.COM 		 * OR group is static and we have specified some rings.
501511878SVenu.Iyer@Sun.COM 		 */
501611878SVenu.Iyer@Sun.COM 		if (rings_needed > 0)
501711878SVenu.Iyer@Sun.COM 			return (EINVAL);
501811878SVenu.Iyer@Sun.COM 	}
50198275SEric Cheng 	return (0);
50208275SEric Cheng }
50218275SEric Cheng 
50228275SEric Cheng /*
50238275SEric Cheng  * Send a MAC_NOTE_LINK notification to all the MAC clients whenever the
50248275SEric Cheng  * underlying physical link is down. This is to allow MAC clients to
50258275SEric Cheng  * communicate with other clients.
50268275SEric Cheng  */
50278275SEric Cheng void
50288275SEric Cheng mac_virtual_link_update(mac_impl_t *mip)
50298275SEric Cheng {
50308275SEric Cheng 	if (mip->mi_linkstate != LINK_STATE_UP)
50318275SEric Cheng 		i_mac_notify(mip, MAC_NOTE_LINK);
50328275SEric Cheng }
50338275SEric Cheng 
50348275SEric Cheng /*
50358275SEric Cheng  * For clients that have a pass-thru MAC, e.g. VNIC, we set the VNIC's
50368275SEric Cheng  * mac handle in the client.
50378275SEric Cheng  */
50388275SEric Cheng void
503911878SVenu.Iyer@Sun.COM mac_set_upper_mac(mac_client_handle_t mch, mac_handle_t mh,
504011878SVenu.Iyer@Sun.COM     mac_resource_props_t *mrp)
50418275SEric Cheng {
50428275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
504311878SVenu.Iyer@Sun.COM 	mac_impl_t		*mip = (mac_impl_t *)mh;
504411878SVenu.Iyer@Sun.COM 
504511878SVenu.Iyer@Sun.COM 	mcip->mci_upper_mip = mip;
504611878SVenu.Iyer@Sun.COM 	/* If there are any properties, copy it over too */
504711878SVenu.Iyer@Sun.COM 	if (mrp != NULL) {
504811878SVenu.Iyer@Sun.COM 		bcopy(mrp, &mip->mi_resource_props,
504911878SVenu.Iyer@Sun.COM 		    sizeof (mac_resource_props_t));
505011878SVenu.Iyer@Sun.COM 	}
50518275SEric Cheng }
50528275SEric Cheng 
50538275SEric Cheng /*
50548275SEric Cheng  * Mark the mac as being used exclusively by the single mac client that is
50558275SEric Cheng  * doing some control operation on this mac. No further opens of this mac
50568275SEric Cheng  * will be allowed until this client calls mac_unmark_exclusive. The mac
50578275SEric Cheng  * client calling this function must already be in the mac perimeter
50588275SEric Cheng  */
50598275SEric Cheng int
50608275SEric Cheng mac_mark_exclusive(mac_handle_t mh)
50618275SEric Cheng {
50628275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
50638275SEric Cheng 
50648275SEric Cheng 	ASSERT(MAC_PERIM_HELD(mh));
50658275SEric Cheng 	/*
50668275SEric Cheng 	 * Look up its entry in the global hash table.
50678275SEric Cheng 	 */
50688275SEric Cheng 	rw_enter(&i_mac_impl_lock, RW_WRITER);
50698275SEric Cheng 	if (mip->mi_state_flags & MIS_DISABLED) {
50708275SEric Cheng 		rw_exit(&i_mac_impl_lock);
50718275SEric Cheng 		return (ENOENT);
50728275SEric Cheng 	}
50738275SEric Cheng 
50748275SEric Cheng 	/*
50758275SEric Cheng 	 * A reference to mac is held even if the link is not plumbed.
50768275SEric Cheng 	 * In i_dls_link_create() we open the MAC interface and hold the
50778275SEric Cheng 	 * reference. There is an additional reference for the mac_open
50788275SEric Cheng 	 * done in acquiring the mac perimeter
50798275SEric Cheng 	 */
50808275SEric Cheng 	if (mip->mi_ref != 2) {
50818275SEric Cheng 		rw_exit(&i_mac_impl_lock);
50828275SEric Cheng 		return (EBUSY);
50838275SEric Cheng 	}
50848275SEric Cheng 
50858275SEric Cheng 	ASSERT(!(mip->mi_state_flags & MIS_EXCLUSIVE_HELD));
50868275SEric Cheng 	mip->mi_state_flags |= MIS_EXCLUSIVE_HELD;
50878275SEric Cheng 	rw_exit(&i_mac_impl_lock);
50888275SEric Cheng 	return (0);
50898275SEric Cheng }
50908275SEric Cheng 
50918275SEric Cheng void
50928275SEric Cheng mac_unmark_exclusive(mac_handle_t mh)
50938275SEric Cheng {
50948275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
50958275SEric Cheng 
50968275SEric Cheng 	ASSERT(MAC_PERIM_HELD(mh));
50978275SEric Cheng 
50988275SEric Cheng 	rw_enter(&i_mac_impl_lock, RW_WRITER);
50998275SEric Cheng 	/* 1 for the creation and another for the perimeter */
51008275SEric Cheng 	ASSERT(mip->mi_ref == 2 && (mip->mi_state_flags & MIS_EXCLUSIVE_HELD));
51018275SEric Cheng 	mip->mi_state_flags &= ~MIS_EXCLUSIVE_HELD;
51028275SEric Cheng 	rw_exit(&i_mac_impl_lock);
51038275SEric Cheng }
51048275SEric Cheng 
51058275SEric Cheng /*
510611878SVenu.Iyer@Sun.COM  * Set the MTU for the specified MAC.
51078275SEric Cheng  */
51088275SEric Cheng int
51098275SEric Cheng mac_set_mtu(mac_handle_t mh, uint_t new_mtu, uint_t *old_mtu_arg)
51108275SEric Cheng {
51118275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
51128275SEric Cheng 	uint_t old_mtu;
511310674SSebastien.Roy@Sun.COM 	int rv = 0;
51148275SEric Cheng 
51158275SEric Cheng 	i_mac_perim_enter(mip);
51168275SEric Cheng 
511710616SSebastien.Roy@Sun.COM 	if (!(mip->mi_callbacks->mc_callbacks & (MC_SETPROP|MC_GETPROP))) {
51188275SEric Cheng 		rv = ENOTSUP;
51198275SEric Cheng 		goto bail;
51208275SEric Cheng 	}
51218275SEric Cheng 
512210616SSebastien.Roy@Sun.COM 	old_mtu = mip->mi_sdu_max;
51238275SEric Cheng 
512411878SVenu.Iyer@Sun.COM 	if (new_mtu == 0 || new_mtu < mip->mi_sdu_min) {
512511878SVenu.Iyer@Sun.COM 		rv = EINVAL;
512611878SVenu.Iyer@Sun.COM 		goto bail;
512711878SVenu.Iyer@Sun.COM 	}
512811878SVenu.Iyer@Sun.COM 
51298275SEric Cheng 	if (old_mtu != new_mtu) {
51308275SEric Cheng 		rv = mip->mi_callbacks->mc_setprop(mip->mi_driver,
51318275SEric Cheng 		    "mtu", MAC_PROP_MTU, sizeof (uint_t), &new_mtu);
513211878SVenu.Iyer@Sun.COM 		if (rv != 0)
513311878SVenu.Iyer@Sun.COM 			goto bail;
513411878SVenu.Iyer@Sun.COM 		rv = mac_maxsdu_update(mh, new_mtu);
513511878SVenu.Iyer@Sun.COM 		ASSERT(rv == 0);
51368275SEric Cheng 	}
51378275SEric Cheng 
51388275SEric Cheng bail:
51398275SEric Cheng 	i_mac_perim_exit(mip);
51408275SEric Cheng 
51418275SEric Cheng 	if (rv == 0 && old_mtu_arg != NULL)
51428275SEric Cheng 		*old_mtu_arg = old_mtu;
51438275SEric Cheng 	return (rv);
51448275SEric Cheng }
51458275SEric Cheng 
514611878SVenu.Iyer@Sun.COM /*
514711878SVenu.Iyer@Sun.COM  * Return the RX h/w information for the group indexed by grp_num.
514811878SVenu.Iyer@Sun.COM  */
51498275SEric Cheng void
515011878SVenu.Iyer@Sun.COM mac_get_hwrxgrp_info(mac_handle_t mh, int grp_index, uint_t *grp_num,
515111878SVenu.Iyer@Sun.COM     uint_t *n_rings, uint_t *rings, uint_t *type, uint_t *n_clnts,
515211878SVenu.Iyer@Sun.COM     char *clnts_name)
51538275SEric Cheng {
51548275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
51558275SEric Cheng 	mac_grp_client_t *mcip;
51568275SEric Cheng 	uint_t i = 0, index = 0;
515711878SVenu.Iyer@Sun.COM 	mac_ring_t	*ring;
51588275SEric Cheng 
51598275SEric Cheng 	/* Revisit when we implement fully dynamic group allocation */
51608275SEric Cheng 	ASSERT(grp_index >= 0 && grp_index < mip->mi_rx_group_count);
51618275SEric Cheng 
51628275SEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_READER);
51638275SEric Cheng 	*grp_num = mip->mi_rx_groups[grp_index].mrg_index;
51648275SEric Cheng 	*type = mip->mi_rx_groups[grp_index].mrg_type;
51658275SEric Cheng 	*n_rings = mip->mi_rx_groups[grp_index].mrg_cur_count;
516611878SVenu.Iyer@Sun.COM 	ring = mip->mi_rx_groups[grp_index].mrg_rings;
516711878SVenu.Iyer@Sun.COM 	for (index = 0; index < mip->mi_rx_groups[grp_index].mrg_cur_count;
516811878SVenu.Iyer@Sun.COM 	    index++) {
516911878SVenu.Iyer@Sun.COM 		rings[index] = ring->mr_index;
517011878SVenu.Iyer@Sun.COM 		ring = ring->mr_next;
517111878SVenu.Iyer@Sun.COM 	}
517211878SVenu.Iyer@Sun.COM 	/* Assuming the 1st is the default group */
517311878SVenu.Iyer@Sun.COM 	index = 0;
517411878SVenu.Iyer@Sun.COM 	if (grp_index == 0) {
517511878SVenu.Iyer@Sun.COM 		(void) strlcpy(clnts_name, "<default,mcast>,",
517611878SVenu.Iyer@Sun.COM 		    MAXCLIENTNAMELEN);
517711878SVenu.Iyer@Sun.COM 		index += strlen("<default,mcast>,");
517811878SVenu.Iyer@Sun.COM 	}
51798275SEric Cheng 	for (mcip = mip->mi_rx_groups[grp_index].mrg_clients; mcip != NULL;
51808275SEric Cheng 	    mcip = mcip->mgc_next) {
51818275SEric Cheng 		int name_len = strlen(mcip->mgc_client->mci_name);
51828275SEric Cheng 
51838275SEric Cheng 		/*
51848275SEric Cheng 		 * MAXCLIENTNAMELEN is the buffer size reserved for client
51858275SEric Cheng 		 * names.
51868275SEric Cheng 		 * XXXX Formating the client name string needs to be moved
51878275SEric Cheng 		 * to user land when fixing the size of dhi_clnts in
51888275SEric Cheng 		 * dld_hwgrpinfo_t. We should use n_clients * client_name for
51898275SEric Cheng 		 * dhi_clntsin instead of MAXCLIENTNAMELEN
51908275SEric Cheng 		 */
51918275SEric Cheng 		if (index + name_len >= MAXCLIENTNAMELEN) {
51928275SEric Cheng 			index = MAXCLIENTNAMELEN;
51938275SEric Cheng 			break;
51948275SEric Cheng 		}
51958275SEric Cheng 		bcopy(mcip->mgc_client->mci_name, &(clnts_name[index]),
51968275SEric Cheng 		    name_len);
51978275SEric Cheng 		index += name_len;
51988275SEric Cheng 		clnts_name[index++] = ',';
51998275SEric Cheng 		i++;
52008275SEric Cheng 	}
52018275SEric Cheng 
52028275SEric Cheng 	/* Get rid of the last , */
52038275SEric Cheng 	if (index > 0)
52048275SEric Cheng 		clnts_name[index - 1] = '\0';
52058275SEric Cheng 	*n_clnts = i;
52068275SEric Cheng 	rw_exit(&mip->mi_rw_lock);
52078275SEric Cheng }
52088275SEric Cheng 
520911878SVenu.Iyer@Sun.COM /*
521011878SVenu.Iyer@Sun.COM  * Return the TX h/w information for the group indexed by grp_num.
521111878SVenu.Iyer@Sun.COM  */
521211878SVenu.Iyer@Sun.COM void
521311878SVenu.Iyer@Sun.COM mac_get_hwtxgrp_info(mac_handle_t mh, int grp_index, uint_t *grp_num,
521411878SVenu.Iyer@Sun.COM     uint_t *n_rings, uint_t *rings, uint_t *type, uint_t *n_clnts,
521511878SVenu.Iyer@Sun.COM     char *clnts_name)
521611878SVenu.Iyer@Sun.COM {
521711878SVenu.Iyer@Sun.COM 	mac_impl_t *mip = (mac_impl_t *)mh;
521811878SVenu.Iyer@Sun.COM 	mac_grp_client_t *mcip;
521911878SVenu.Iyer@Sun.COM 	uint_t i = 0, index = 0;
522011878SVenu.Iyer@Sun.COM 	mac_ring_t	*ring;
522111878SVenu.Iyer@Sun.COM 
522211878SVenu.Iyer@Sun.COM 	/* Revisit when we implement fully dynamic group allocation */
522311878SVenu.Iyer@Sun.COM 	ASSERT(grp_index >= 0 && grp_index <= mip->mi_tx_group_count);
522411878SVenu.Iyer@Sun.COM 
522511878SVenu.Iyer@Sun.COM 	rw_enter(&mip->mi_rw_lock, RW_READER);
522611878SVenu.Iyer@Sun.COM 	*grp_num = mip->mi_tx_groups[grp_index].mrg_index > 0 ?
522711878SVenu.Iyer@Sun.COM 	    mip->mi_tx_groups[grp_index].mrg_index : grp_index;
522811878SVenu.Iyer@Sun.COM 	*type = mip->mi_tx_groups[grp_index].mrg_type;
522911878SVenu.Iyer@Sun.COM 	*n_rings = mip->mi_tx_groups[grp_index].mrg_cur_count;
523011878SVenu.Iyer@Sun.COM 	ring = mip->mi_tx_groups[grp_index].mrg_rings;
523111878SVenu.Iyer@Sun.COM 	for (index = 0; index < mip->mi_tx_groups[grp_index].mrg_cur_count;
523211878SVenu.Iyer@Sun.COM 	    index++) {
523311878SVenu.Iyer@Sun.COM 		rings[index] = ring->mr_index;
523411878SVenu.Iyer@Sun.COM 		ring = ring->mr_next;
523511878SVenu.Iyer@Sun.COM 	}
523611878SVenu.Iyer@Sun.COM 	index = 0;
523711878SVenu.Iyer@Sun.COM 	/* Default group has an index of -1 */
523811878SVenu.Iyer@Sun.COM 	if (mip->mi_tx_groups[grp_index].mrg_index < 0) {
523911878SVenu.Iyer@Sun.COM 		(void) strlcpy(clnts_name, "<default>,",
524011878SVenu.Iyer@Sun.COM 		    MAXCLIENTNAMELEN);
524111878SVenu.Iyer@Sun.COM 		index += strlen("<default>,");
524211878SVenu.Iyer@Sun.COM 	}
524311878SVenu.Iyer@Sun.COM 	for (mcip = mip->mi_tx_groups[grp_index].mrg_clients; mcip != NULL;
524411878SVenu.Iyer@Sun.COM 	    mcip = mcip->mgc_next) {
524511878SVenu.Iyer@Sun.COM 		int name_len = strlen(mcip->mgc_client->mci_name);
524611878SVenu.Iyer@Sun.COM 
524711878SVenu.Iyer@Sun.COM 		/*
524811878SVenu.Iyer@Sun.COM 		 * MAXCLIENTNAMELEN is the buffer size reserved for client
524911878SVenu.Iyer@Sun.COM 		 * names.
525011878SVenu.Iyer@Sun.COM 		 * XXXX Formating the client name string needs to be moved
525111878SVenu.Iyer@Sun.COM 		 * to user land when fixing the size of dhi_clnts in
525211878SVenu.Iyer@Sun.COM 		 * dld_hwgrpinfo_t. We should use n_clients * client_name for
525311878SVenu.Iyer@Sun.COM 		 * dhi_clntsin instead of MAXCLIENTNAMELEN
525411878SVenu.Iyer@Sun.COM 		 */
525511878SVenu.Iyer@Sun.COM 		if (index + name_len >= MAXCLIENTNAMELEN) {
525611878SVenu.Iyer@Sun.COM 			index = MAXCLIENTNAMELEN;
525711878SVenu.Iyer@Sun.COM 			break;
525811878SVenu.Iyer@Sun.COM 		}
525911878SVenu.Iyer@Sun.COM 		bcopy(mcip->mgc_client->mci_name, &(clnts_name[index]),
526011878SVenu.Iyer@Sun.COM 		    name_len);
526111878SVenu.Iyer@Sun.COM 		index += name_len;
526211878SVenu.Iyer@Sun.COM 		clnts_name[index++] = ',';
526311878SVenu.Iyer@Sun.COM 		i++;
526411878SVenu.Iyer@Sun.COM 	}
526511878SVenu.Iyer@Sun.COM 
526611878SVenu.Iyer@Sun.COM 	/* Get rid of the last , */
526711878SVenu.Iyer@Sun.COM 	if (index > 0)
526811878SVenu.Iyer@Sun.COM 		clnts_name[index - 1] = '\0';
526911878SVenu.Iyer@Sun.COM 	*n_clnts = i;
527011878SVenu.Iyer@Sun.COM 	rw_exit(&mip->mi_rw_lock);
527111878SVenu.Iyer@Sun.COM }
527211878SVenu.Iyer@Sun.COM 
527311878SVenu.Iyer@Sun.COM /*
527411878SVenu.Iyer@Sun.COM  * Return the group count for RX or TX.
527511878SVenu.Iyer@Sun.COM  */
52768275SEric Cheng uint_t
527711878SVenu.Iyer@Sun.COM mac_hwgrp_num(mac_handle_t mh, int type)
52788275SEric Cheng {
52798275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
52808275SEric Cheng 
528111878SVenu.Iyer@Sun.COM 	/*
528211878SVenu.Iyer@Sun.COM 	 * Return the Rx and Tx group count; for the Tx we need to
528311878SVenu.Iyer@Sun.COM 	 * include the default too.
528411878SVenu.Iyer@Sun.COM 	 */
528511878SVenu.Iyer@Sun.COM 	return (type == MAC_RING_TYPE_RX ? mip->mi_rx_group_count :
528611878SVenu.Iyer@Sun.COM 	    mip->mi_tx_groups != NULL ? mip->mi_tx_group_count + 1 : 0);
528711878SVenu.Iyer@Sun.COM }
528811878SVenu.Iyer@Sun.COM 
528911878SVenu.Iyer@Sun.COM /*
529011878SVenu.Iyer@Sun.COM  * The total number of free TX rings for this MAC.
529111878SVenu.Iyer@Sun.COM  */
529211878SVenu.Iyer@Sun.COM uint_t
529311878SVenu.Iyer@Sun.COM mac_txavail_get(mac_handle_t mh)
529411878SVenu.Iyer@Sun.COM {
529511878SVenu.Iyer@Sun.COM 	mac_impl_t	*mip = (mac_impl_t *)mh;
529611878SVenu.Iyer@Sun.COM 
529711878SVenu.Iyer@Sun.COM 	return (mip->mi_txrings_avail);
529811878SVenu.Iyer@Sun.COM }
529911878SVenu.Iyer@Sun.COM 
530011878SVenu.Iyer@Sun.COM /*
530111878SVenu.Iyer@Sun.COM  * The total number of free RX rings for this MAC.
530211878SVenu.Iyer@Sun.COM  */
530311878SVenu.Iyer@Sun.COM uint_t
530411878SVenu.Iyer@Sun.COM mac_rxavail_get(mac_handle_t mh)
530511878SVenu.Iyer@Sun.COM {
530611878SVenu.Iyer@Sun.COM 	mac_impl_t	*mip = (mac_impl_t *)mh;
530711878SVenu.Iyer@Sun.COM 
530811878SVenu.Iyer@Sun.COM 	return (mip->mi_rxrings_avail);
530911878SVenu.Iyer@Sun.COM }
531011878SVenu.Iyer@Sun.COM 
531111878SVenu.Iyer@Sun.COM /*
531211878SVenu.Iyer@Sun.COM  * The total number of reserved RX rings on this MAC.
531311878SVenu.Iyer@Sun.COM  */
531411878SVenu.Iyer@Sun.COM uint_t
531511878SVenu.Iyer@Sun.COM mac_rxrsvd_get(mac_handle_t mh)
531611878SVenu.Iyer@Sun.COM {
531711878SVenu.Iyer@Sun.COM 	mac_impl_t	*mip = (mac_impl_t *)mh;
531811878SVenu.Iyer@Sun.COM 
531911878SVenu.Iyer@Sun.COM 	return (mip->mi_rxrings_rsvd);
532011878SVenu.Iyer@Sun.COM }
532111878SVenu.Iyer@Sun.COM 
532211878SVenu.Iyer@Sun.COM /*
532311878SVenu.Iyer@Sun.COM  * The total number of reserved TX rings on this MAC.
532411878SVenu.Iyer@Sun.COM  */
532511878SVenu.Iyer@Sun.COM uint_t
532611878SVenu.Iyer@Sun.COM mac_txrsvd_get(mac_handle_t mh)
532711878SVenu.Iyer@Sun.COM {
532811878SVenu.Iyer@Sun.COM 	mac_impl_t	*mip = (mac_impl_t *)mh;
532911878SVenu.Iyer@Sun.COM 
533011878SVenu.Iyer@Sun.COM 	return (mip->mi_txrings_rsvd);
53318275SEric Cheng }
533211878SVenu.Iyer@Sun.COM 
533311878SVenu.Iyer@Sun.COM /*
533411878SVenu.Iyer@Sun.COM  * Total number of free RX groups on this MAC.
533511878SVenu.Iyer@Sun.COM  */
533611878SVenu.Iyer@Sun.COM uint_t
533711878SVenu.Iyer@Sun.COM mac_rxhwlnksavail_get(mac_handle_t mh)
533811878SVenu.Iyer@Sun.COM {
533911878SVenu.Iyer@Sun.COM 	mac_impl_t	*mip = (mac_impl_t *)mh;
534011878SVenu.Iyer@Sun.COM 
534111878SVenu.Iyer@Sun.COM 	return (mip->mi_rxhwclnt_avail);
534211878SVenu.Iyer@Sun.COM }
534311878SVenu.Iyer@Sun.COM 
534411878SVenu.Iyer@Sun.COM /*
534511878SVenu.Iyer@Sun.COM  * Total number of RX groups reserved on this MAC.
534611878SVenu.Iyer@Sun.COM  */
534711878SVenu.Iyer@Sun.COM uint_t
534811878SVenu.Iyer@Sun.COM mac_rxhwlnksrsvd_get(mac_handle_t mh)
534911878SVenu.Iyer@Sun.COM {
535011878SVenu.Iyer@Sun.COM 	mac_impl_t	*mip = (mac_impl_t *)mh;
535111878SVenu.Iyer@Sun.COM 
535211878SVenu.Iyer@Sun.COM 	return (mip->mi_rxhwclnt_used);
535311878SVenu.Iyer@Sun.COM }
535411878SVenu.Iyer@Sun.COM 
535511878SVenu.Iyer@Sun.COM /*
535611878SVenu.Iyer@Sun.COM  * Total number of free TX groups on this MAC.
535711878SVenu.Iyer@Sun.COM  */
535811878SVenu.Iyer@Sun.COM uint_t
535911878SVenu.Iyer@Sun.COM mac_txhwlnksavail_get(mac_handle_t mh)
536011878SVenu.Iyer@Sun.COM {
536111878SVenu.Iyer@Sun.COM 	mac_impl_t	*mip = (mac_impl_t *)mh;
536211878SVenu.Iyer@Sun.COM 
536311878SVenu.Iyer@Sun.COM 	return (mip->mi_txhwclnt_avail);
536411878SVenu.Iyer@Sun.COM }
536511878SVenu.Iyer@Sun.COM 
536611878SVenu.Iyer@Sun.COM /*
536711878SVenu.Iyer@Sun.COM  * Total number of TX groups reserved on this MAC.
536811878SVenu.Iyer@Sun.COM  */
536911878SVenu.Iyer@Sun.COM uint_t
537011878SVenu.Iyer@Sun.COM mac_txhwlnksrsvd_get(mac_handle_t mh)
537111878SVenu.Iyer@Sun.COM {
537211878SVenu.Iyer@Sun.COM 	mac_impl_t	*mip = (mac_impl_t *)mh;
537311878SVenu.Iyer@Sun.COM 
537411878SVenu.Iyer@Sun.COM 	return (mip->mi_txhwclnt_used);
537511878SVenu.Iyer@Sun.COM }
537611878SVenu.Iyer@Sun.COM 
537711878SVenu.Iyer@Sun.COM /*
537811878SVenu.Iyer@Sun.COM  * Initialize the rings property for a mac client. A non-0 value for
537911878SVenu.Iyer@Sun.COM  * rxring or txring specifies the number of rings required, a value
538011878SVenu.Iyer@Sun.COM  * of MAC_RXRINGS_NONE/MAC_TXRINGS_NONE specifies that it doesn't need
538111878SVenu.Iyer@Sun.COM  * any RX/TX rings and a value of MAC_RXRINGS_DONTCARE/MAC_TXRINGS_DONTCARE
538211878SVenu.Iyer@Sun.COM  * means the system can decide whether it can give any rings or not.
538311878SVenu.Iyer@Sun.COM  */
538411878SVenu.Iyer@Sun.COM void
538511878SVenu.Iyer@Sun.COM mac_client_set_rings(mac_client_handle_t mch, int rxrings, int txrings)
538611878SVenu.Iyer@Sun.COM {
538711878SVenu.Iyer@Sun.COM 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
538811878SVenu.Iyer@Sun.COM 	mac_resource_props_t	*mrp = MCIP_RESOURCE_PROPS(mcip);
538911878SVenu.Iyer@Sun.COM 
539011878SVenu.Iyer@Sun.COM 	if (rxrings != MAC_RXRINGS_DONTCARE) {
539111878SVenu.Iyer@Sun.COM 		mrp->mrp_mask |= MRP_RX_RINGS;
539211878SVenu.Iyer@Sun.COM 		mrp->mrp_nrxrings = rxrings;
539311878SVenu.Iyer@Sun.COM 	}
539411878SVenu.Iyer@Sun.COM 
539511878SVenu.Iyer@Sun.COM 	if (txrings != MAC_TXRINGS_DONTCARE) {
539611878SVenu.Iyer@Sun.COM 		mrp->mrp_mask |= MRP_TX_RINGS;
539711878SVenu.Iyer@Sun.COM 		mrp->mrp_ntxrings = txrings;
539811878SVenu.Iyer@Sun.COM 	}
539911878SVenu.Iyer@Sun.COM }
5400