xref: /onnv-gate/usr/src/uts/common/io/mac/mac_client.c (revision 11878:ac93462db6d7)
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 /*
2311665SDarren.Reed@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
248275SEric Cheng  * Use is subject to license terms.
258275SEric Cheng  */
268275SEric Cheng 
278275SEric Cheng /*
288275SEric Cheng  * - General Introduction:
298275SEric Cheng  *
308275SEric Cheng  * This file contains the implementation of the MAC client kernel
318275SEric Cheng  * API and related code. The MAC client API allows a kernel module
328275SEric Cheng  * to gain access to a MAC instance (physical NIC, link aggregation, etc).
338275SEric Cheng  * It allows a MAC client to associate itself with a MAC address,
348275SEric Cheng  * VLANs, callback functions for data traffic and for promiscuous mode.
358275SEric Cheng  * The MAC client API is also used to specify the properties associated
368275SEric Cheng  * with a MAC client, such as bandwidth limits, priority, CPUS, etc.
378275SEric Cheng  * These properties are further used to determine the hardware resources
388275SEric Cheng  * to allocate to the various MAC clients.
398275SEric Cheng  *
408275SEric Cheng  * - Primary MAC clients:
418275SEric Cheng  *
428275SEric Cheng  * The MAC client API refers to "primary MAC clients". A primary MAC
438275SEric Cheng  * client is a client which "owns" the primary MAC address of
448275SEric Cheng  * the underlying MAC instance. The primary MAC address is called out
458275SEric Cheng  * since it is associated with specific semantics: the primary MAC
468275SEric Cheng  * address is the MAC address which is assigned to the IP interface
478275SEric Cheng  * when it is plumbed, and the primary MAC address is assigned
488275SEric Cheng  * to VLAN data-links. The primary address of a MAC instance can
498275SEric Cheng  * also change dynamically from under the MAC client, for example
508275SEric Cheng  * as a result of a change of state of a link aggregation. In that
518275SEric Cheng  * case the MAC layer automatically updates all data-structures which
528275SEric Cheng  * refer to the current value of the primary MAC address. Typical
538275SEric Cheng  * primary MAC clients are dls, aggr, and xnb. A typical non-primary
548275SEric Cheng  * MAC client is the vnic driver.
558275SEric Cheng  *
568275SEric Cheng  * - Virtual Switching:
578275SEric Cheng  *
588275SEric Cheng  * The MAC layer implements a virtual switch between the MAC clients
598275SEric Cheng  * (primary and non-primary) defined on top of the same underlying
608275SEric Cheng  * NIC (physical, link aggregation, etc). The virtual switch is
618275SEric Cheng  * VLAN-aware, i.e. it allows multiple MAC clients to be member
628275SEric Cheng  * of one or more VLANs, and the virtual switch will distribute
638275SEric Cheng  * multicast tagged packets only to the member of the corresponding
648275SEric Cheng  * VLANs.
658275SEric Cheng  *
668275SEric Cheng  * - Upper vs Lower MAC:
678275SEric Cheng  *
688275SEric Cheng  * Creating a VNIC on top of a MAC instance effectively causes
698275SEric Cheng  * two MAC instances to be layered on top of each other, one for
708275SEric Cheng  * the VNIC(s), one for the underlying MAC instance (physical NIC,
718275SEric Cheng  * link aggregation, etc). In the code below we refer to the
728275SEric Cheng  * underlying NIC as the "lower MAC", and we refer to VNICs as
738275SEric Cheng  * the "upper MAC".
748275SEric Cheng  *
758275SEric Cheng  * - Pass-through for VNICs:
768275SEric Cheng  *
778275SEric Cheng  * When VNICs are created on top of an underlying MAC, this causes
788275SEric Cheng  * a layering of two MAC instances. Since the lower MAC already
798275SEric Cheng  * does the switching and demultiplexing to its MAC clients, the
808275SEric Cheng  * upper MAC would simply have to pass packets to the layer below
818275SEric Cheng  * or above it, which would introduce overhead. In order to avoid
828275SEric Cheng  * this overhead, the MAC layer implements a pass-through mechanism
838275SEric Cheng  * for VNICs. When a VNIC opens the lower MAC instance, it saves
848275SEric Cheng  * the MAC client handle it optains from the MAC layer. When a MAC
858275SEric Cheng  * client opens a VNIC (upper MAC), the MAC layer detects that
868275SEric Cheng  * the MAC being opened is a VNIC, and gets the MAC client handle
878275SEric Cheng  * that the VNIC driver obtained from the lower MAC. This exchange
888275SEric Cheng  * is doing through a private capability between the MAC layer
898275SEric Cheng  * and the VNIC driver. The upper MAC then returns that handle
908275SEric Cheng  * directly to its MAC client. Any operation done by the upper
918275SEric Cheng  * MAC client is now done on the lower MAC client handle, which
928275SEric Cheng  * allows the VNIC driver to be completely bypassed for the
938275SEric Cheng  * performance sensitive data-path.
948275SEric Cheng  *
958275SEric Cheng  */
968275SEric Cheng 
978275SEric Cheng #include <sys/types.h>
988275SEric Cheng #include <sys/conf.h>
998275SEric Cheng #include <sys/id_space.h>
1008275SEric Cheng #include <sys/esunddi.h>
1018275SEric Cheng #include <sys/stat.h>
1028275SEric Cheng #include <sys/mkdev.h>
1038275SEric Cheng #include <sys/stream.h>
1048275SEric Cheng #include <sys/strsun.h>
1058275SEric Cheng #include <sys/strsubr.h>
1068275SEric Cheng #include <sys/dlpi.h>
1078275SEric Cheng #include <sys/modhash.h>
1088275SEric Cheng #include <sys/mac_impl.h>
1098275SEric Cheng #include <sys/mac_client_impl.h>
1108275SEric Cheng #include <sys/mac_soft_ring.h>
111*11878SVenu.Iyer@Sun.COM #include <sys/mac_stat.h>
1128275SEric Cheng #include <sys/dls.h>
1138275SEric Cheng #include <sys/dld.h>
1148275SEric Cheng #include <sys/modctl.h>
1158275SEric Cheng #include <sys/fs/dv_node.h>
1168275SEric Cheng #include <sys/thread.h>
1178275SEric Cheng #include <sys/proc.h>
1188275SEric Cheng #include <sys/callb.h>
1198275SEric Cheng #include <sys/cpuvar.h>
1208275SEric Cheng #include <sys/atomic.h>
1218275SEric Cheng #include <sys/sdt.h>
1228275SEric Cheng #include <sys/mac_flow.h>
1238275SEric Cheng #include <sys/ddi_intr_impl.h>
1248275SEric Cheng #include <sys/disp.h>
1258275SEric Cheng #include <sys/sdt.h>
1268275SEric Cheng #include <sys/vnic.h>
1278275SEric Cheng #include <sys/vnic_impl.h>
1288275SEric Cheng #include <sys/vlan.h>
1298275SEric Cheng #include <inet/ip.h>
1308275SEric Cheng #include <inet/ip6.h>
1318275SEric Cheng #include <sys/exacct.h>
1328275SEric Cheng #include <sys/exacct_impl.h>
1338275SEric Cheng #include <inet/nd.h>
1348275SEric Cheng #include <sys/ethernet.h>
1358275SEric Cheng 
1368275SEric Cheng kmem_cache_t	*mac_client_impl_cache;
1378275SEric Cheng kmem_cache_t	*mac_promisc_impl_cache;
1388275SEric Cheng 
1398275SEric Cheng static boolean_t mac_client_single_rcvr(mac_client_impl_t *);
1408275SEric Cheng static flow_entry_t *mac_client_swap_mciflent(mac_client_impl_t *);
1418275SEric Cheng static flow_entry_t *mac_client_get_flow(mac_client_impl_t *,
1428275SEric Cheng     mac_unicast_impl_t *);
1438275SEric Cheng static void mac_client_remove_flow_from_list(mac_client_impl_t *,
1448275SEric Cheng     flow_entry_t *);
1458275SEric Cheng static void mac_client_add_to_flow_list(mac_client_impl_t *, flow_entry_t *);
1468275SEric Cheng static void mac_rename_flow_names(mac_client_impl_t *, const char *);
1478275SEric Cheng static void mac_virtual_link_update(mac_impl_t *);
148*11878SVenu.Iyer@Sun.COM static int mac_client_datapath_setup(mac_client_impl_t *, uint16_t,
149*11878SVenu.Iyer@Sun.COM     uint8_t *, mac_resource_props_t *, boolean_t, mac_unicast_impl_t *);
150*11878SVenu.Iyer@Sun.COM static void mac_client_datapath_teardown(mac_client_handle_t,
151*11878SVenu.Iyer@Sun.COM     mac_unicast_impl_t *, flow_entry_t *);
1528275SEric Cheng 
1538275SEric Cheng /* ARGSUSED */
1548275SEric Cheng static int
1558275SEric Cheng i_mac_client_impl_ctor(void *buf, void *arg, int kmflag)
1568275SEric Cheng {
1578275SEric Cheng 	int	i;
1588275SEric Cheng 	mac_client_impl_t	*mcip = buf;
1598275SEric Cheng 
1608275SEric Cheng 	bzero(buf, MAC_CLIENT_IMPL_SIZE);
1618275SEric Cheng 	mutex_init(&mcip->mci_tx_cb_lock, NULL, MUTEX_DRIVER, NULL);
1628275SEric Cheng 	mcip->mci_tx_notify_cb_info.mcbi_lockp = &mcip->mci_tx_cb_lock;
1638275SEric Cheng 
1648275SEric Cheng 	ASSERT(mac_tx_percpu_cnt >= 0);
1658275SEric Cheng 	for (i = 0; i <= mac_tx_percpu_cnt; i++) {
1668275SEric Cheng 		mutex_init(&mcip->mci_tx_pcpu[i].pcpu_tx_lock, NULL,
1678275SEric Cheng 		    MUTEX_DRIVER, NULL);
1688275SEric Cheng 	}
1698275SEric Cheng 	cv_init(&mcip->mci_tx_cv, NULL, CV_DRIVER, NULL);
1708275SEric Cheng 
1718275SEric Cheng 	return (0);
1728275SEric Cheng }
1738275SEric Cheng 
1748275SEric Cheng /* ARGSUSED */
1758275SEric Cheng static void
1768275SEric Cheng i_mac_client_impl_dtor(void *buf, void *arg)
1778275SEric Cheng {
1788275SEric Cheng 	int	i;
1798275SEric Cheng 	mac_client_impl_t *mcip = buf;
1808275SEric Cheng 
1818275SEric Cheng 	ASSERT(mcip->mci_promisc_list == NULL);
1828275SEric Cheng 	ASSERT(mcip->mci_unicast_list == NULL);
1838275SEric Cheng 	ASSERT(mcip->mci_state_flags == 0);
1848275SEric Cheng 	ASSERT(mcip->mci_tx_flag == 0);
1858275SEric Cheng 
1868275SEric Cheng 	mutex_destroy(&mcip->mci_tx_cb_lock);
1878275SEric Cheng 
1888275SEric Cheng 	ASSERT(mac_tx_percpu_cnt >= 0);
1898275SEric Cheng 	for (i = 0; i <= mac_tx_percpu_cnt; i++) {
1908275SEric Cheng 		ASSERT(mcip->mci_tx_pcpu[i].pcpu_tx_refcnt == 0);
1918275SEric Cheng 		mutex_destroy(&mcip->mci_tx_pcpu[i].pcpu_tx_lock);
1928275SEric Cheng 	}
1938275SEric Cheng 	cv_destroy(&mcip->mci_tx_cv);
1948275SEric Cheng }
1958275SEric Cheng 
1968275SEric Cheng /* ARGSUSED */
1978275SEric Cheng static int
1988275SEric Cheng i_mac_promisc_impl_ctor(void *buf, void *arg, int kmflag)
1998275SEric Cheng {
2008275SEric Cheng 	mac_promisc_impl_t	*mpip = buf;
2018275SEric Cheng 
2028275SEric Cheng 	bzero(buf, sizeof (mac_promisc_impl_t));
2038275SEric Cheng 	mpip->mpi_mci_link.mcb_objp = buf;
2048275SEric Cheng 	mpip->mpi_mci_link.mcb_objsize = sizeof (mac_promisc_impl_t);
2058275SEric Cheng 	mpip->mpi_mi_link.mcb_objp = buf;
2068275SEric Cheng 	mpip->mpi_mi_link.mcb_objsize = sizeof (mac_promisc_impl_t);
2078275SEric Cheng 	return (0);
2088275SEric Cheng }
2098275SEric Cheng 
2108275SEric Cheng /* ARGSUSED */
2118275SEric Cheng static void
2128275SEric Cheng i_mac_promisc_impl_dtor(void *buf, void *arg)
2138275SEric Cheng {
2148275SEric Cheng 	mac_promisc_impl_t	*mpip = buf;
2158275SEric Cheng 
2168275SEric Cheng 	ASSERT(mpip->mpi_mci_link.mcb_objp != NULL);
2178275SEric Cheng 	ASSERT(mpip->mpi_mci_link.mcb_objsize == sizeof (mac_promisc_impl_t));
2188275SEric Cheng 	ASSERT(mpip->mpi_mi_link.mcb_objp == mpip->mpi_mci_link.mcb_objp);
2198275SEric Cheng 	ASSERT(mpip->mpi_mi_link.mcb_objsize == sizeof (mac_promisc_impl_t));
2208275SEric Cheng 
2218275SEric Cheng 	mpip->mpi_mci_link.mcb_objp = NULL;
2228275SEric Cheng 	mpip->mpi_mci_link.mcb_objsize = 0;
2238275SEric Cheng 	mpip->mpi_mi_link.mcb_objp = NULL;
2248275SEric Cheng 	mpip->mpi_mi_link.mcb_objsize = 0;
2258275SEric Cheng 
2268275SEric Cheng 	ASSERT(mpip->mpi_mci_link.mcb_flags == 0);
2278275SEric Cheng 	mpip->mpi_mci_link.mcb_objsize = 0;
2288275SEric Cheng }
2298275SEric Cheng 
2308275SEric Cheng void
2318275SEric Cheng mac_client_init(void)
2328275SEric Cheng {
2338275SEric Cheng 	ASSERT(mac_tx_percpu_cnt >= 0);
2348275SEric Cheng 
2358275SEric Cheng 	mac_client_impl_cache = kmem_cache_create("mac_client_impl_cache",
2368275SEric Cheng 	    MAC_CLIENT_IMPL_SIZE, 0, i_mac_client_impl_ctor,
2378275SEric Cheng 	    i_mac_client_impl_dtor, NULL, NULL, NULL, 0);
2388275SEric Cheng 	ASSERT(mac_client_impl_cache != NULL);
2398275SEric Cheng 
2408275SEric Cheng 	mac_promisc_impl_cache = kmem_cache_create("mac_promisc_impl_cache",
2418275SEric Cheng 	    sizeof (mac_promisc_impl_t), 0, i_mac_promisc_impl_ctor,
2428275SEric Cheng 	    i_mac_promisc_impl_dtor, NULL, NULL, NULL, 0);
2438275SEric Cheng 	ASSERT(mac_promisc_impl_cache != NULL);
2448275SEric Cheng }
2458275SEric Cheng 
2468275SEric Cheng void
2478275SEric Cheng mac_client_fini(void)
2488275SEric Cheng {
2498275SEric Cheng 	kmem_cache_destroy(mac_client_impl_cache);
2508275SEric Cheng 	kmem_cache_destroy(mac_promisc_impl_cache);
2518275SEric Cheng }
2528275SEric Cheng 
2538275SEric Cheng /*
2548275SEric Cheng  * Return the lower MAC client handle from the VNIC driver for the
2558275SEric Cheng  * specified VNIC MAC instance.
2568275SEric Cheng  */
2578275SEric Cheng mac_client_impl_t *
2588275SEric Cheng mac_vnic_lower(mac_impl_t *mip)
2598275SEric Cheng {
2608275SEric Cheng 	mac_capab_vnic_t cap;
2618275SEric Cheng 	mac_client_impl_t *mcip;
2628275SEric Cheng 
2638275SEric Cheng 	VERIFY(i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_VNIC, &cap));
2648275SEric Cheng 	mcip = cap.mcv_mac_client_handle(cap.mcv_arg);
2658275SEric Cheng 
2668275SEric Cheng 	return (mcip);
2678275SEric Cheng }
2688275SEric Cheng 
2698275SEric Cheng /*
2708275SEric Cheng  * Return the MAC client handle of the primary MAC client for the
2718275SEric Cheng  * specified MAC instance, or NULL otherwise.
2728275SEric Cheng  */
2738275SEric Cheng mac_client_impl_t *
2748275SEric Cheng mac_primary_client_handle(mac_impl_t *mip)
2758275SEric Cheng {
2768275SEric Cheng 	mac_client_impl_t *mcip;
2778275SEric Cheng 
2788275SEric Cheng 	if (mip->mi_state_flags & MIS_IS_VNIC)
2798275SEric Cheng 		return (mac_vnic_lower(mip));
2808275SEric Cheng 
2818275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
2828275SEric Cheng 
2838275SEric Cheng 	for (mcip = mip->mi_clients_list; mcip != NULL;
2848275SEric Cheng 	    mcip = mcip->mci_client_next) {
2858275SEric Cheng 		if (MCIP_DATAPATH_SETUP(mcip) && mac_is_primary_client(mcip))
2868275SEric Cheng 			return (mcip);
2878275SEric Cheng 	}
2888275SEric Cheng 	return (NULL);
2898275SEric Cheng }
2908275SEric Cheng 
2918275SEric Cheng /*
2928275SEric Cheng  * Open a MAC specified by its MAC name.
2938275SEric Cheng  */
2948275SEric Cheng int
2958275SEric Cheng mac_open(const char *macname, mac_handle_t *mhp)
2968275SEric Cheng {
2978275SEric Cheng 	mac_impl_t	*mip;
2988275SEric Cheng 	int		err;
2998275SEric Cheng 
3008275SEric Cheng 	/*
3018275SEric Cheng 	 * Look up its entry in the global hash table.
3028275SEric Cheng 	 */
3038275SEric Cheng 	if ((err = mac_hold(macname, &mip)) != 0)
3048275SEric Cheng 		return (err);
3058275SEric Cheng 
3068275SEric Cheng 	/*
3078275SEric Cheng 	 * Hold the dip associated to the MAC to prevent it from being
3088275SEric Cheng 	 * detached. For a softmac, its underlying dip is held by the
3098275SEric Cheng 	 * mi_open() callback.
3108275SEric Cheng 	 *
3118275SEric Cheng 	 * This is done to be more tolerant with some defective drivers,
3128275SEric Cheng 	 * which incorrectly handle mac_unregister() failure in their
3138275SEric Cheng 	 * xxx_detach() routine. For example, some drivers ignore the
3148275SEric Cheng 	 * failure of mac_unregister() and free all resources that
3158275SEric Cheng 	 * that are needed for data transmition.
3168275SEric Cheng 	 */
3178275SEric Cheng 	e_ddi_hold_devi(mip->mi_dip);
3188275SEric Cheng 
3198275SEric Cheng 	if (!(mip->mi_callbacks->mc_callbacks & MC_OPEN)) {
3208275SEric Cheng 		*mhp = (mac_handle_t)mip;
3218275SEric Cheng 		return (0);
3228275SEric Cheng 	}
3238275SEric Cheng 
3248275SEric Cheng 	/*
3258275SEric Cheng 	 * The mac perimeter is used in both mac_open and mac_close by the
3268275SEric Cheng 	 * framework to single thread the MC_OPEN/MC_CLOSE of drivers.
3278275SEric Cheng 	 */
3288275SEric Cheng 	i_mac_perim_enter(mip);
3298275SEric Cheng 	mip->mi_oref++;
3308275SEric Cheng 	if (mip->mi_oref != 1 || ((err = mip->mi_open(mip->mi_driver)) == 0)) {
3318275SEric Cheng 		*mhp = (mac_handle_t)mip;
3328275SEric Cheng 		i_mac_perim_exit(mip);
3338275SEric Cheng 		return (0);
3348275SEric Cheng 	}
3358275SEric Cheng 	mip->mi_oref--;
3368275SEric Cheng 	ddi_release_devi(mip->mi_dip);
3378275SEric Cheng 	mac_rele(mip);
3388275SEric Cheng 	i_mac_perim_exit(mip);
3398275SEric Cheng 	return (err);
3408275SEric Cheng }
3418275SEric Cheng 
3428275SEric Cheng /*
3438275SEric Cheng  * Open a MAC specified by its linkid.
3448275SEric Cheng  */
3458275SEric Cheng int
3468275SEric Cheng mac_open_by_linkid(datalink_id_t linkid, mac_handle_t *mhp)
3478275SEric Cheng {
3488275SEric Cheng 	dls_dl_handle_t	dlh;
3498275SEric Cheng 	int		err;
3508275SEric Cheng 
3518275SEric Cheng 	if ((err = dls_devnet_hold_tmp(linkid, &dlh)) != 0)
3528275SEric Cheng 		return (err);
3538275SEric Cheng 
3548275SEric Cheng 	dls_devnet_prop_task_wait(dlh);
3558275SEric Cheng 
3568275SEric Cheng 	err = mac_open(dls_devnet_mac(dlh), mhp);
3578275SEric Cheng 
3588275SEric Cheng 	dls_devnet_rele_tmp(dlh);
3598275SEric Cheng 	return (err);
3608275SEric Cheng }
3618275SEric Cheng 
3628275SEric Cheng /*
3638275SEric Cheng  * Open a MAC specified by its link name.
3648275SEric Cheng  */
3658275SEric Cheng int
3668275SEric Cheng mac_open_by_linkname(const char *link, mac_handle_t *mhp)
3678275SEric Cheng {
3688275SEric Cheng 	datalink_id_t	linkid;
3698275SEric Cheng 	int		err;
3708275SEric Cheng 
3718275SEric Cheng 	if ((err = dls_mgmt_get_linkid(link, &linkid)) != 0)
3728275SEric Cheng 		return (err);
3738275SEric Cheng 	return (mac_open_by_linkid(linkid, mhp));
3748275SEric Cheng }
3758275SEric Cheng 
3768275SEric Cheng /*
3778275SEric Cheng  * Close the specified MAC.
3788275SEric Cheng  */
3798275SEric Cheng void
3808275SEric Cheng mac_close(mac_handle_t mh)
3818275SEric Cheng {
3828275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
3838275SEric Cheng 
3848275SEric Cheng 	i_mac_perim_enter(mip);
3858275SEric Cheng 	/*
3868275SEric Cheng 	 * The mac perimeter is used in both mac_open and mac_close by the
3878275SEric Cheng 	 * framework to single thread the MC_OPEN/MC_CLOSE of drivers.
3888275SEric Cheng 	 */
3898275SEric Cheng 	if (mip->mi_callbacks->mc_callbacks & MC_OPEN) {
3908275SEric Cheng 		ASSERT(mip->mi_oref != 0);
3918275SEric Cheng 		if (--mip->mi_oref == 0) {
3928275SEric Cheng 			if ((mip->mi_callbacks->mc_callbacks & MC_CLOSE))
3938275SEric Cheng 				mip->mi_close(mip->mi_driver);
3948275SEric Cheng 		}
3958275SEric Cheng 	}
3968275SEric Cheng 	i_mac_perim_exit(mip);
3978275SEric Cheng 	ddi_release_devi(mip->mi_dip);
3988275SEric Cheng 	mac_rele(mip);
3998275SEric Cheng }
4008275SEric Cheng 
4018275SEric Cheng /*
4028275SEric Cheng  * Misc utility functions to retrieve various information about a MAC
4038275SEric Cheng  * instance or a MAC client.
4048275SEric Cheng  */
4058275SEric Cheng 
4068275SEric Cheng const mac_info_t *
4078275SEric Cheng mac_info(mac_handle_t mh)
4088275SEric Cheng {
4098275SEric Cheng 	return (&((mac_impl_t *)mh)->mi_info);
4108275SEric Cheng }
4118275SEric Cheng 
4128275SEric Cheng dev_info_t *
4138275SEric Cheng mac_devinfo_get(mac_handle_t mh)
4148275SEric Cheng {
4158275SEric Cheng 	return (((mac_impl_t *)mh)->mi_dip);
4168275SEric Cheng }
4178275SEric Cheng 
4189073SCathy.Zhou@Sun.COM void *
4199073SCathy.Zhou@Sun.COM mac_driver(mac_handle_t mh)
4209073SCathy.Zhou@Sun.COM {
4219073SCathy.Zhou@Sun.COM 	return (((mac_impl_t *)mh)->mi_driver);
4229073SCathy.Zhou@Sun.COM }
4239073SCathy.Zhou@Sun.COM 
4248275SEric Cheng const char *
4258275SEric Cheng mac_name(mac_handle_t mh)
4268275SEric Cheng {
4278275SEric Cheng 	return (((mac_impl_t *)mh)->mi_name);
4288275SEric Cheng }
4298275SEric Cheng 
43010639SDarren.Reed@Sun.COM int
43110639SDarren.Reed@Sun.COM mac_type(mac_handle_t mh)
43210639SDarren.Reed@Sun.COM {
43310639SDarren.Reed@Sun.COM 	return (((mac_impl_t *)mh)->mi_type->mt_type);
43410639SDarren.Reed@Sun.COM }
43510639SDarren.Reed@Sun.COM 
43611665SDarren.Reed@Sun.COM int
43711665SDarren.Reed@Sun.COM mac_nativetype(mac_handle_t mh)
43811665SDarren.Reed@Sun.COM {
43911665SDarren.Reed@Sun.COM 	return (((mac_impl_t *)mh)->mi_type->mt_nativetype);
44011665SDarren.Reed@Sun.COM }
44111665SDarren.Reed@Sun.COM 
4428275SEric Cheng char *
4438275SEric Cheng mac_client_name(mac_client_handle_t mch)
4448275SEric Cheng {
4458275SEric Cheng 	return (((mac_client_impl_t *)mch)->mci_name);
4468275SEric Cheng }
4478275SEric Cheng 
4488275SEric Cheng minor_t
4498275SEric Cheng mac_minor(mac_handle_t mh)
4508275SEric Cheng {
4518275SEric Cheng 	return (((mac_impl_t *)mh)->mi_minor);
4528275SEric Cheng }
4538275SEric Cheng 
4548275SEric Cheng /*
4558275SEric Cheng  * Return the VID associated with a MAC client. This function should
4568275SEric Cheng  * be called for clients which are associated with only one VID.
4578275SEric Cheng  */
4588275SEric Cheng uint16_t
4598275SEric Cheng mac_client_vid(mac_client_handle_t mch)
4608275SEric Cheng {
4618275SEric Cheng 	uint16_t		vid = VLAN_ID_NONE;
4628275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
4638275SEric Cheng 	flow_desc_t		flow_desc;
4648275SEric Cheng 
4658275SEric Cheng 	if (mcip->mci_nflents == 0)
4668275SEric Cheng 		return (vid);
4678275SEric Cheng 
4688275SEric Cheng 	ASSERT(MCIP_DATAPATH_SETUP(mcip) && mac_client_single_rcvr(mcip));
4698275SEric Cheng 
4708275SEric Cheng 	mac_flow_get_desc(mcip->mci_flent, &flow_desc);
4718275SEric Cheng 	if ((flow_desc.fd_mask & FLOW_LINK_VID) != 0)
4728275SEric Cheng 		vid = flow_desc.fd_vid;
4738275SEric Cheng 
4748275SEric Cheng 	return (vid);
4758275SEric Cheng }
4768275SEric Cheng 
4778275SEric Cheng /*
4789726SNicolas.Droux@Sun.COM  * Return whether the specified MAC client corresponds to a VLAN VNIC.
4799726SNicolas.Droux@Sun.COM  */
4809726SNicolas.Droux@Sun.COM boolean_t
4819726SNicolas.Droux@Sun.COM mac_client_is_vlan_vnic(mac_client_handle_t mch)
4829726SNicolas.Droux@Sun.COM {
4839726SNicolas.Droux@Sun.COM 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
4849726SNicolas.Droux@Sun.COM 
4859726SNicolas.Droux@Sun.COM 	return (((mcip->mci_state_flags & MCIS_IS_VNIC) != 0) &&
4869726SNicolas.Droux@Sun.COM 	    ((mcip->mci_flent->fe_type & FLOW_PRIMARY_MAC) != 0));
4879726SNicolas.Droux@Sun.COM }
4889726SNicolas.Droux@Sun.COM 
4899726SNicolas.Droux@Sun.COM /*
4908275SEric Cheng  * Return the link speed associated with the specified MAC client.
4918275SEric Cheng  *
4928275SEric Cheng  * The link speed of a MAC client is equal to the smallest value of
4938275SEric Cheng  * 1) the current link speed of the underlying NIC, or
4948275SEric Cheng  * 2) the bandwidth limit set for the MAC client.
4958275SEric Cheng  *
4968275SEric Cheng  * Note that the bandwidth limit can be higher than the speed
4978275SEric Cheng  * of the underlying NIC. This is allowed to avoid spurious
4988275SEric Cheng  * administration action failures or artifically lowering the
4998275SEric Cheng  * bandwidth limit of a link that may  have temporarily lowered
5008275SEric Cheng  * its link speed due to hardware problem or administrator action.
5018275SEric Cheng  */
5028275SEric Cheng static uint64_t
5038275SEric Cheng mac_client_ifspeed(mac_client_impl_t *mcip)
5048275SEric Cheng {
5058275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
5068275SEric Cheng 	uint64_t nic_speed;
5078275SEric Cheng 
5088275SEric Cheng 	nic_speed = mac_stat_get((mac_handle_t)mip, MAC_STAT_IFSPEED);
5098275SEric Cheng 
5108275SEric Cheng 	if (nic_speed == 0) {
5118275SEric Cheng 		return (0);
5128275SEric Cheng 	} else {
5138275SEric Cheng 		uint64_t policy_limit = (uint64_t)-1;
5148275SEric Cheng 
5158275SEric Cheng 		if (MCIP_RESOURCE_PROPS_MASK(mcip) & MRP_MAXBW)
5168275SEric Cheng 			policy_limit = MCIP_RESOURCE_PROPS_MAXBW(mcip);
5178275SEric Cheng 
5188275SEric Cheng 		return (MIN(policy_limit, nic_speed));
5198275SEric Cheng 	}
5208275SEric Cheng }
5218275SEric Cheng 
5228275SEric Cheng /*
5238275SEric Cheng  * Return the link state of the specified client. If here are more
5248275SEric Cheng  * than one clients of the underying mac_impl_t, the link state
5258275SEric Cheng  * will always be UP regardless of the link state of the underlying
5268275SEric Cheng  * mac_impl_t. This is needed to allow the MAC clients to continue
5278275SEric Cheng  * to communicate with each other even when the physical link of
5288275SEric Cheng  * their mac_impl_t is down.
5298275SEric Cheng  */
5308275SEric Cheng static uint64_t
5318275SEric Cheng mac_client_link_state(mac_client_impl_t *mcip)
5328275SEric Cheng {
5338275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
5348275SEric Cheng 	uint16_t vid;
5358275SEric Cheng 	mac_client_impl_t *mci_list;
5368275SEric Cheng 	mac_unicast_impl_t *mui_list, *oth_mui_list;
5378275SEric Cheng 
5388275SEric Cheng 	/*
5398275SEric Cheng 	 * Returns LINK_STATE_UP if there are other MAC clients defined on
5408275SEric Cheng 	 * mac_impl_t which share same VLAN ID as that of mcip. Note that
5418275SEric Cheng 	 * if 'mcip' has more than one VID's then we match ANY one of the
5428275SEric Cheng 	 * VID's with other MAC client's VID's and return LINK_STATE_UP.
5438275SEric Cheng 	 */
5448275SEric Cheng 	rw_enter(&mcip->mci_rw_lock, RW_READER);
5458275SEric Cheng 	for (mui_list = mcip->mci_unicast_list; mui_list != NULL;
5468275SEric Cheng 	    mui_list = mui_list->mui_next) {
5478275SEric Cheng 		vid = mui_list->mui_vid;
5488275SEric Cheng 		for (mci_list = mip->mi_clients_list; mci_list != NULL;
5498275SEric Cheng 		    mci_list = mci_list->mci_client_next) {
5508275SEric Cheng 			if (mci_list == mcip)
5518275SEric Cheng 				continue;
5528275SEric Cheng 			for (oth_mui_list = mci_list->mci_unicast_list;
5538275SEric Cheng 			    oth_mui_list != NULL; oth_mui_list = oth_mui_list->
5548275SEric Cheng 			    mui_next) {
5558275SEric Cheng 				if (vid == oth_mui_list->mui_vid) {
5568275SEric Cheng 					rw_exit(&mcip->mci_rw_lock);
5578275SEric Cheng 					return (LINK_STATE_UP);
5588275SEric Cheng 				}
5598275SEric Cheng 			}
5608275SEric Cheng 		}
5618275SEric Cheng 	}
5628275SEric Cheng 	rw_exit(&mcip->mci_rw_lock);
5638275SEric Cheng 
5648275SEric Cheng 	return (mac_stat_get((mac_handle_t)mip, MAC_STAT_LINK_STATE));
5658275SEric Cheng }
5668275SEric Cheng 
5678275SEric Cheng /*
568*11878SVenu.Iyer@Sun.COM  * These statistics are consumed by dladm show-link -s <vnic>,
569*11878SVenu.Iyer@Sun.COM  * dladm show-vnic -s and netstat. With the introduction of dlstat,
570*11878SVenu.Iyer@Sun.COM  * dladm show-link -s and dladm show-vnic -s witll be EOL'ed while
571*11878SVenu.Iyer@Sun.COM  * netstat will consume from kstats introduced for dlstat. This code
572*11878SVenu.Iyer@Sun.COM  * will be removed at that time.
573*11878SVenu.Iyer@Sun.COM  */
574*11878SVenu.Iyer@Sun.COM 
575*11878SVenu.Iyer@Sun.COM /*
5768275SEric Cheng  * Return the statistics of a MAC client. These statistics are different
5778275SEric Cheng  * then the statistics of the underlying MAC which are returned by
5788275SEric Cheng  * mac_stat_get().
5798275SEric Cheng  */
5808275SEric Cheng uint64_t
5818275SEric Cheng mac_client_stat_get(mac_client_handle_t mch, uint_t stat)
5828275SEric Cheng {
583*11878SVenu.Iyer@Sun.COM 	mac_client_impl_t 	*mcip = (mac_client_impl_t *)mch;
584*11878SVenu.Iyer@Sun.COM 	mac_impl_t 		*mip = mcip->mci_mip;
585*11878SVenu.Iyer@Sun.COM 	flow_entry_t 		*flent = mcip->mci_flent;
586*11878SVenu.Iyer@Sun.COM 	mac_soft_ring_set_t 	*mac_srs;
587*11878SVenu.Iyer@Sun.COM 	mac_rx_stats_t		*mac_rx_stat;
588*11878SVenu.Iyer@Sun.COM 	mac_tx_stats_t		*mac_tx_stat;
589*11878SVenu.Iyer@Sun.COM 	int i;
590*11878SVenu.Iyer@Sun.COM 	uint64_t val = 0;
591*11878SVenu.Iyer@Sun.COM 
592*11878SVenu.Iyer@Sun.COM 	mac_srs = (mac_soft_ring_set_t *)(flent->fe_tx_srs);
593*11878SVenu.Iyer@Sun.COM 	mac_tx_stat = &mac_srs->srs_tx.st_stat;
5948275SEric Cheng 
5958275SEric Cheng 	switch (stat) {
5968275SEric Cheng 	case MAC_STAT_LINK_STATE:
5978275SEric Cheng 		val = mac_client_link_state(mcip);
5988275SEric Cheng 		break;
5998275SEric Cheng 	case MAC_STAT_LINK_UP:
6008275SEric Cheng 		val = (mac_client_link_state(mcip) == LINK_STATE_UP);
6018275SEric Cheng 		break;
6028275SEric Cheng 	case MAC_STAT_PROMISC:
6038275SEric Cheng 		val = mac_stat_get((mac_handle_t)mip, MAC_STAT_PROMISC);
6048275SEric Cheng 		break;
60510491SRishi.Srivatsavai@Sun.COM 	case MAC_STAT_LOWLINK_STATE:
60610491SRishi.Srivatsavai@Sun.COM 		val = mac_stat_get((mac_handle_t)mip, MAC_STAT_LOWLINK_STATE);
60710491SRishi.Srivatsavai@Sun.COM 		break;
6088275SEric Cheng 	case MAC_STAT_IFSPEED:
6098275SEric Cheng 		val = mac_client_ifspeed(mcip);
6108275SEric Cheng 		break;
6118275SEric Cheng 	case MAC_STAT_MULTIRCV:
612*11878SVenu.Iyer@Sun.COM 		val = mcip->mci_misc_stat.mms_multircv;
6138275SEric Cheng 		break;
6148275SEric Cheng 	case MAC_STAT_BRDCSTRCV:
615*11878SVenu.Iyer@Sun.COM 		val = mcip->mci_misc_stat.mms_brdcstrcv;
6168275SEric Cheng 		break;
6178275SEric Cheng 	case MAC_STAT_MULTIXMT:
618*11878SVenu.Iyer@Sun.COM 		val = mcip->mci_misc_stat.mms_multixmt;
6198275SEric Cheng 		break;
6208275SEric Cheng 	case MAC_STAT_BRDCSTXMT:
621*11878SVenu.Iyer@Sun.COM 		val = mcip->mci_misc_stat.mms_brdcstxmt;
6228275SEric Cheng 		break;
6238275SEric Cheng 	case MAC_STAT_OBYTES:
624*11878SVenu.Iyer@Sun.COM 		val = mac_tx_stat->mts_obytes;
6258275SEric Cheng 		break;
6268275SEric Cheng 	case MAC_STAT_OPACKETS:
627*11878SVenu.Iyer@Sun.COM 		val = mac_tx_stat->mts_opackets;
6288275SEric Cheng 		break;
6298275SEric Cheng 	case MAC_STAT_OERRORS:
630*11878SVenu.Iyer@Sun.COM 		val = mac_tx_stat->mts_oerrors;
6318275SEric Cheng 		break;
6328275SEric Cheng 	case MAC_STAT_IPACKETS:
633*11878SVenu.Iyer@Sun.COM 		for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
634*11878SVenu.Iyer@Sun.COM 			mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
635*11878SVenu.Iyer@Sun.COM 			mac_rx_stat = &mac_srs->srs_rx.sr_stat;
636*11878SVenu.Iyer@Sun.COM 			val += mac_rx_stat->mrs_intrcnt +
637*11878SVenu.Iyer@Sun.COM 			    mac_rx_stat->mrs_pollcnt + mac_rx_stat->mrs_lclcnt;
638*11878SVenu.Iyer@Sun.COM 		}
6398275SEric Cheng 		break;
6408275SEric Cheng 	case MAC_STAT_RBYTES:
641*11878SVenu.Iyer@Sun.COM 		for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
642*11878SVenu.Iyer@Sun.COM 			mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
643*11878SVenu.Iyer@Sun.COM 			mac_rx_stat = &mac_srs->srs_rx.sr_stat;
644*11878SVenu.Iyer@Sun.COM 			val += mac_rx_stat->mrs_intrbytes +
645*11878SVenu.Iyer@Sun.COM 			    mac_rx_stat->mrs_pollbytes +
646*11878SVenu.Iyer@Sun.COM 			    mac_rx_stat->mrs_lclbytes;
647*11878SVenu.Iyer@Sun.COM 		}
6488275SEric Cheng 		break;
6498275SEric Cheng 	case MAC_STAT_IERRORS:
650*11878SVenu.Iyer@Sun.COM 		for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
651*11878SVenu.Iyer@Sun.COM 			mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
652*11878SVenu.Iyer@Sun.COM 			mac_rx_stat = &mac_srs->srs_rx.sr_stat;
653*11878SVenu.Iyer@Sun.COM 			val += mac_rx_stat->mrs_ierrors;
654*11878SVenu.Iyer@Sun.COM 		}
6558275SEric Cheng 		break;
6568275SEric Cheng 	default:
657*11878SVenu.Iyer@Sun.COM 		val = mac_driver_stat_default(mip, stat);
6588275SEric Cheng 		break;
6598275SEric Cheng 	}
6608275SEric Cheng 
6618275SEric Cheng 	return (val);
6628275SEric Cheng }
6638275SEric Cheng 
6648275SEric Cheng /*
6658275SEric Cheng  * Return the statistics of the specified MAC instance.
6668275SEric Cheng  */
6678275SEric Cheng uint64_t
6688275SEric Cheng mac_stat_get(mac_handle_t mh, uint_t stat)
6698275SEric Cheng {
6708275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
6718275SEric Cheng 	uint64_t	val;
6728275SEric Cheng 	int		ret;
6738275SEric Cheng 
6748275SEric Cheng 	/*
6758275SEric Cheng 	 * The range of stat determines where it is maintained.  Stat
6768275SEric Cheng 	 * values from 0 up to (but not including) MAC_STAT_MIN are
6778275SEric Cheng 	 * mainteined by the mac module itself.  Everything else is
6788275SEric Cheng 	 * maintained by the driver.
6798275SEric Cheng 	 *
6808275SEric Cheng 	 * If the mac_impl_t being queried corresponds to a VNIC,
6818275SEric Cheng 	 * the stats need to be queried from the lower MAC client
6828275SEric Cheng 	 * corresponding to the VNIC. (The mac_link_update()
6838275SEric Cheng 	 * invoked by the driver to the lower MAC causes the *lower
6848275SEric Cheng 	 * MAC* to update its mi_linkstate, and send a notification
6858275SEric Cheng 	 * to its MAC clients. Due to the VNIC passthrough,
6868275SEric Cheng 	 * these notifications are sent to the upper MAC clients
6878275SEric Cheng 	 * of the VNIC directly, and the upper mac_impl_t of the VNIC
6888275SEric Cheng 	 * does not have a valid mi_linkstate.
6898275SEric Cheng 	 */
6908275SEric Cheng 	if (stat < MAC_STAT_MIN && !(mip->mi_state_flags & MIS_IS_VNIC)) {
6918275SEric Cheng 		/* these stats are maintained by the mac module itself */
6928275SEric Cheng 		switch (stat) {
6938275SEric Cheng 		case MAC_STAT_LINK_STATE:
6948275SEric Cheng 			return (mip->mi_linkstate);
6958275SEric Cheng 		case MAC_STAT_LINK_UP:
6968275SEric Cheng 			return (mip->mi_linkstate == LINK_STATE_UP);
6978275SEric Cheng 		case MAC_STAT_PROMISC:
6988275SEric Cheng 			return (mip->mi_devpromisc != 0);
69910491SRishi.Srivatsavai@Sun.COM 		case MAC_STAT_LOWLINK_STATE:
70010491SRishi.Srivatsavai@Sun.COM 			return (mip->mi_lowlinkstate);
7018275SEric Cheng 		default:
7028275SEric Cheng 			ASSERT(B_FALSE);
7038275SEric Cheng 		}
7048275SEric Cheng 	}
7058275SEric Cheng 
7068275SEric Cheng 	/*
7078275SEric Cheng 	 * Call the driver to get the given statistic.
7088275SEric Cheng 	 */
7098275SEric Cheng 	ret = mip->mi_getstat(mip->mi_driver, stat, &val);
7108275SEric Cheng 	if (ret != 0) {
7118275SEric Cheng 		/*
7128275SEric Cheng 		 * The driver doesn't support this statistic.  Get the
7138275SEric Cheng 		 * statistic's default value.
7148275SEric Cheng 		 */
715*11878SVenu.Iyer@Sun.COM 		val = mac_driver_stat_default(mip, stat);
7168275SEric Cheng 	}
7178275SEric Cheng 	return (val);
7188275SEric Cheng }
7198275SEric Cheng 
7208275SEric Cheng /*
721*11878SVenu.Iyer@Sun.COM  * Query hardware rx ring corresponding to the pseudo ring.
722*11878SVenu.Iyer@Sun.COM  */
723*11878SVenu.Iyer@Sun.COM uint64_t
724*11878SVenu.Iyer@Sun.COM mac_pseudo_rx_ring_stat_get(mac_ring_handle_t handle, uint_t stat)
725*11878SVenu.Iyer@Sun.COM {
726*11878SVenu.Iyer@Sun.COM 	return (mac_rx_ring_stat_get(handle, stat));
727*11878SVenu.Iyer@Sun.COM }
728*11878SVenu.Iyer@Sun.COM 
729*11878SVenu.Iyer@Sun.COM /*
730*11878SVenu.Iyer@Sun.COM  * Query hardware tx ring corresponding to the pseudo ring.
731*11878SVenu.Iyer@Sun.COM  */
732*11878SVenu.Iyer@Sun.COM uint64_t
733*11878SVenu.Iyer@Sun.COM mac_pseudo_tx_ring_stat_get(mac_ring_handle_t handle, uint_t stat)
734*11878SVenu.Iyer@Sun.COM {
735*11878SVenu.Iyer@Sun.COM 	return (mac_tx_ring_stat_get(handle, stat));
736*11878SVenu.Iyer@Sun.COM }
737*11878SVenu.Iyer@Sun.COM 
738*11878SVenu.Iyer@Sun.COM /*
7398275SEric Cheng  * Utility function which returns the VID associated with a flow entry.
7408275SEric Cheng  */
7418275SEric Cheng uint16_t
7428275SEric Cheng i_mac_flow_vid(flow_entry_t *flent)
7438275SEric Cheng {
7448275SEric Cheng 	flow_desc_t	flow_desc;
7458275SEric Cheng 
7468275SEric Cheng 	mac_flow_get_desc(flent, &flow_desc);
7478275SEric Cheng 
7488275SEric Cheng 	if ((flow_desc.fd_mask & FLOW_LINK_VID) != 0)
7498275SEric Cheng 		return (flow_desc.fd_vid);
7508275SEric Cheng 	return (VLAN_ID_NONE);
7518275SEric Cheng }
7528275SEric Cheng 
7538275SEric Cheng /*
7548275SEric Cheng  * Verify the validity of the specified unicast MAC address. Returns B_TRUE
7558275SEric Cheng  * if the address is valid, B_FALSE otherwise (multicast address, or incorrect
7568275SEric Cheng  * length.
7578275SEric Cheng  */
7588275SEric Cheng boolean_t
7598275SEric Cheng mac_unicst_verify(mac_handle_t mh, const uint8_t *addr, uint_t len)
7608275SEric Cheng {
7618275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
7628275SEric Cheng 
7638275SEric Cheng 	/*
7648275SEric Cheng 	 * Verify the address. No lock is needed since mi_type and plugin
7658275SEric Cheng 	 * details don't change after mac_register().
7668275SEric Cheng 	 */
7678275SEric Cheng 	if ((len != mip->mi_type->mt_addr_length) ||
7688275SEric Cheng 	    (mip->mi_type->mt_ops.mtops_unicst_verify(addr,
7698275SEric Cheng 	    mip->mi_pdata)) != 0) {
7708275SEric Cheng 		return (B_FALSE);
7718275SEric Cheng 	} else {
7728275SEric Cheng 		return (B_TRUE);
7738275SEric Cheng 	}
7748275SEric Cheng }
7758275SEric Cheng 
7768275SEric Cheng void
7778275SEric Cheng mac_sdu_get(mac_handle_t mh, uint_t *min_sdu, uint_t *max_sdu)
7788275SEric Cheng {
7798275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
7808275SEric Cheng 
7818275SEric Cheng 	if (min_sdu != NULL)
7828275SEric Cheng 		*min_sdu = mip->mi_sdu_min;
7838275SEric Cheng 	if (max_sdu != NULL)
7848275SEric Cheng 		*max_sdu = mip->mi_sdu_max;
7858275SEric Cheng }
7868275SEric Cheng 
7878275SEric Cheng /*
7888275SEric Cheng  * Update the MAC unicast address of the specified client's flows. Currently
7898275SEric Cheng  * only one unicast MAC unicast address is allowed per client.
7908275SEric Cheng  */
7918275SEric Cheng static void
7928275SEric Cheng mac_unicast_update_client_flow(mac_client_impl_t *mcip)
7938275SEric Cheng {
7948275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
7958275SEric Cheng 	flow_entry_t *flent = mcip->mci_flent;
7968275SEric Cheng 	mac_address_t *map = mcip->mci_unicast;
7978275SEric Cheng 	flow_desc_t flow_desc;
7988275SEric Cheng 
7998275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
8008275SEric Cheng 	ASSERT(flent != NULL);
8018275SEric Cheng 
8028275SEric Cheng 	mac_flow_get_desc(flent, &flow_desc);
8038275SEric Cheng 	ASSERT(flow_desc.fd_mask & FLOW_LINK_DST);
8048275SEric Cheng 
8058275SEric Cheng 	bcopy(map->ma_addr, flow_desc.fd_dst_mac, map->ma_len);
8068275SEric Cheng 	mac_flow_set_desc(flent, &flow_desc);
8078275SEric Cheng 
8088275SEric Cheng 	/*
809*11878SVenu.Iyer@Sun.COM 	 * The v6 local addr (used by mac protection) needs to be
810*11878SVenu.Iyer@Sun.COM 	 * regenerated because our mac address has changed.
811*11878SVenu.Iyer@Sun.COM 	 */
812*11878SVenu.Iyer@Sun.COM 	mac_protect_update_v6_local_addr(mcip);
813*11878SVenu.Iyer@Sun.COM 
814*11878SVenu.Iyer@Sun.COM 	/*
8158275SEric Cheng 	 * A MAC client could have one MAC address but multiple
8168275SEric Cheng 	 * VLANs. In that case update the flow entries corresponding
8178275SEric Cheng 	 * to all VLANs of the MAC client.
8188275SEric Cheng 	 */
8198275SEric Cheng 	for (flent = mcip->mci_flent_list; flent != NULL;
8208275SEric Cheng 	    flent = flent->fe_client_next) {
8218275SEric Cheng 		mac_flow_get_desc(flent, &flow_desc);
8228275SEric Cheng 		if (!(flent->fe_type & FLOW_PRIMARY_MAC ||
8238275SEric Cheng 		    flent->fe_type & FLOW_VNIC_MAC))
8248275SEric Cheng 			continue;
8258275SEric Cheng 
8268275SEric Cheng 		bcopy(map->ma_addr, flow_desc.fd_dst_mac, map->ma_len);
8278275SEric Cheng 		mac_flow_set_desc(flent, &flow_desc);
8288275SEric Cheng 	}
8298275SEric Cheng }
8308275SEric Cheng 
8318275SEric Cheng /*
8328275SEric Cheng  * Update all clients that share the same unicast address.
8338275SEric Cheng  */
8348275SEric Cheng void
8358275SEric Cheng mac_unicast_update_clients(mac_impl_t *mip, mac_address_t *map)
8368275SEric Cheng {
8378275SEric Cheng 	mac_client_impl_t *mcip;
8388275SEric Cheng 
8398275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
8408275SEric Cheng 
8418275SEric Cheng 	/*
8428275SEric Cheng 	 * Find all clients that share the same unicast MAC address and update
8438275SEric Cheng 	 * them appropriately.
8448275SEric Cheng 	 */
8458275SEric Cheng 	for (mcip = mip->mi_clients_list; mcip != NULL;
8468275SEric Cheng 	    mcip = mcip->mci_client_next) {
8478275SEric Cheng 		/*
8488275SEric Cheng 		 * Ignore clients that don't share this MAC address.
8498275SEric Cheng 		 */
8508275SEric Cheng 		if (map != mcip->mci_unicast)
8518275SEric Cheng 			continue;
8528275SEric Cheng 
8538275SEric Cheng 		/*
8548275SEric Cheng 		 * Update those clients with same old unicast MAC address.
8558275SEric Cheng 		 */
8568275SEric Cheng 		mac_unicast_update_client_flow(mcip);
8578275SEric Cheng 	}
8588275SEric Cheng }
8598275SEric Cheng 
8608275SEric Cheng /*
8618275SEric Cheng  * Update the unicast MAC address of the specified VNIC MAC client.
8628275SEric Cheng  *
8638275SEric Cheng  * Check whether the operation is valid. Any of following cases should fail:
8648275SEric Cheng  *
8658275SEric Cheng  * 1. It's a VLAN type of VNIC.
8668275SEric Cheng  * 2. The new value is current "primary" MAC address.
8678275SEric Cheng  * 3. The current MAC address is shared with other clients.
8688275SEric Cheng  * 4. The new MAC address has been used. This case will be valid when
8698275SEric Cheng  *    client migration is fully supported.
8708275SEric Cheng  */
8718275SEric Cheng int
8728275SEric Cheng mac_vnic_unicast_set(mac_client_handle_t mch, const uint8_t *addr)
8738275SEric Cheng {
8748275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
8758275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
8768275SEric Cheng 	mac_address_t *map = mcip->mci_unicast;
8778275SEric Cheng 	int err;
8788275SEric Cheng 
8798275SEric Cheng 	ASSERT(!(mip->mi_state_flags & MIS_IS_VNIC));
8808275SEric Cheng 	ASSERT(mcip->mci_state_flags & MCIS_IS_VNIC);
8818275SEric Cheng 	ASSERT(mcip->mci_flags != MAC_CLIENT_FLAGS_PRIMARY);
8828275SEric Cheng 
8838275SEric Cheng 	i_mac_perim_enter(mip);
8848275SEric Cheng 
8858275SEric Cheng 	/*
8868275SEric Cheng 	 * If this is a VLAN type of VNIC, it's using "primary" MAC address
8878275SEric Cheng 	 * of the underlying interface. Must fail here. Refer to case 1 above.
8888275SEric Cheng 	 */
8898275SEric Cheng 	if (bcmp(map->ma_addr, mip->mi_addr, map->ma_len) == 0) {
8908275SEric Cheng 		i_mac_perim_exit(mip);
8918275SEric Cheng 		return (ENOTSUP);
8928275SEric Cheng 	}
8938275SEric Cheng 
8948275SEric Cheng 	/*
8958275SEric Cheng 	 * If the new address is the "primary" one, must fail. Refer to
8968275SEric Cheng 	 * case 2 above.
8978275SEric Cheng 	 */
8988275SEric Cheng 	if (bcmp(addr, mip->mi_addr, map->ma_len) == 0) {
8998275SEric Cheng 		i_mac_perim_exit(mip);
9008275SEric Cheng 		return (EACCES);
9018275SEric Cheng 	}
9028275SEric Cheng 
9038275SEric Cheng 	/*
9048275SEric Cheng 	 * If the address is shared by multiple clients, must fail. Refer
9058275SEric Cheng 	 * to case 3 above.
9068275SEric Cheng 	 */
9078275SEric Cheng 	if (mac_check_macaddr_shared(map)) {
9088275SEric Cheng 		i_mac_perim_exit(mip);
9098275SEric Cheng 		return (EBUSY);
9108275SEric Cheng 	}
9118275SEric Cheng 
9128275SEric Cheng 	/*
9138275SEric Cheng 	 * If the new address has been used, must fail for now. Refer to
9148275SEric Cheng 	 * case 4 above.
9158275SEric Cheng 	 */
9168275SEric Cheng 	if (mac_find_macaddr(mip, (uint8_t *)addr) != NULL) {
9178275SEric Cheng 		i_mac_perim_exit(mip);
9188275SEric Cheng 		return (ENOTSUP);
9198275SEric Cheng 	}
9208275SEric Cheng 
9218275SEric Cheng 	/*
9228275SEric Cheng 	 * Update the MAC address.
9238275SEric Cheng 	 */
9248275SEric Cheng 	err = mac_update_macaddr(map, (uint8_t *)addr);
9258275SEric Cheng 
9268275SEric Cheng 	if (err != 0) {
9278275SEric Cheng 		i_mac_perim_exit(mip);
9288275SEric Cheng 		return (err);
9298275SEric Cheng 	}
9308275SEric Cheng 
9318275SEric Cheng 	/*
9328275SEric Cheng 	 * Update all flows of this MAC client.
9338275SEric Cheng 	 */
9348275SEric Cheng 	mac_unicast_update_client_flow(mcip);
9358275SEric Cheng 
9368275SEric Cheng 	i_mac_perim_exit(mip);
9378275SEric Cheng 	return (0);
9388275SEric Cheng }
9398275SEric Cheng 
9408275SEric Cheng /*
9418275SEric Cheng  * Program the new primary unicast address of the specified MAC.
9428275SEric Cheng  *
9438275SEric Cheng  * Function mac_update_macaddr() takes care different types of underlying
9448275SEric Cheng  * MAC. If the underlying MAC is VNIC, the VNIC driver must have registerd
9458275SEric Cheng  * mi_unicst() entry point, that indirectly calls mac_vnic_unicast_set()
9468275SEric Cheng  * which will take care of updating the MAC address of the corresponding
9478275SEric Cheng  * MAC client.
9488275SEric Cheng  *
9498275SEric Cheng  * This is the only interface that allow the client to update the "primary"
9508275SEric Cheng  * MAC address of the underlying MAC. The new value must have not been
9518275SEric Cheng  * used by other clients.
9528275SEric Cheng  */
9538275SEric Cheng int
9548275SEric Cheng mac_unicast_primary_set(mac_handle_t mh, const uint8_t *addr)
9558275SEric Cheng {
9568275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
9578275SEric Cheng 	mac_address_t *map;
9588275SEric Cheng 	int err;
9598275SEric Cheng 
9608275SEric Cheng 	/* verify the address validity */
9618275SEric Cheng 	if (!mac_unicst_verify(mh, addr, mip->mi_type->mt_addr_length))
9628275SEric Cheng 		return (EINVAL);
9638275SEric Cheng 
9648275SEric Cheng 	i_mac_perim_enter(mip);
9658275SEric Cheng 
9668275SEric Cheng 	/*
9678275SEric Cheng 	 * If the new value is the same as the current primary address value,
9688275SEric Cheng 	 * there's nothing to do.
9698275SEric Cheng 	 */
9708275SEric Cheng 	if (bcmp(addr, mip->mi_addr, mip->mi_type->mt_addr_length) == 0) {
9718275SEric Cheng 		i_mac_perim_exit(mip);
9728275SEric Cheng 		return (0);
9738275SEric Cheng 	}
9748275SEric Cheng 
9758275SEric Cheng 	if (mac_find_macaddr(mip, (uint8_t *)addr) != 0) {
9768275SEric Cheng 		i_mac_perim_exit(mip);
9778275SEric Cheng 		return (EBUSY);
9788275SEric Cheng 	}
9798275SEric Cheng 
9808275SEric Cheng 	map = mac_find_macaddr(mip, mip->mi_addr);
9818275SEric Cheng 	ASSERT(map != NULL);
9828275SEric Cheng 
9838275SEric Cheng 	/*
9848275SEric Cheng 	 * Update the MAC address.
9858275SEric Cheng 	 */
9868275SEric Cheng 	if (mip->mi_state_flags & MIS_IS_AGGR) {
9878275SEric Cheng 		mac_capab_aggr_t aggr_cap;
9888275SEric Cheng 
9898275SEric Cheng 		/*
9908275SEric Cheng 		 * If the mac is an aggregation, other than the unicast
9918275SEric Cheng 		 * addresses programming, aggr must be informed about this
9928275SEric Cheng 		 * primary unicst address change to change its mac address
9938275SEric Cheng 		 * policy to be user-specified.
9948275SEric Cheng 		 */
9958275SEric Cheng 		ASSERT(map->ma_type == MAC_ADDRESS_TYPE_UNICAST_CLASSIFIED);
9968275SEric Cheng 		VERIFY(i_mac_capab_get(mh, MAC_CAPAB_AGGR, &aggr_cap));
9978275SEric Cheng 		err = aggr_cap.mca_unicst(mip->mi_driver, addr);
9988275SEric Cheng 		if (err == 0)
9998275SEric Cheng 			bcopy(addr, map->ma_addr, map->ma_len);
10008275SEric Cheng 	} else {
10018275SEric Cheng 		err = mac_update_macaddr(map, (uint8_t *)addr);
10028275SEric Cheng 	}
10038275SEric Cheng 
10048275SEric Cheng 	if (err != 0) {
10058275SEric Cheng 		i_mac_perim_exit(mip);
10068275SEric Cheng 		return (err);
10078275SEric Cheng 	}
10088275SEric Cheng 
10098275SEric Cheng 	mac_unicast_update_clients(mip, map);
10108275SEric Cheng 
10118275SEric Cheng 	/*
10128275SEric Cheng 	 * Save the new primary MAC address in mac_impl_t.
10138275SEric Cheng 	 */
10148275SEric Cheng 	bcopy(addr, mip->mi_addr, mip->mi_type->mt_addr_length);
10158275SEric Cheng 
10168275SEric Cheng 	i_mac_perim_exit(mip);
10178275SEric Cheng 
10188275SEric Cheng 	if (err == 0)
10198275SEric Cheng 		i_mac_notify(mip, MAC_NOTE_UNICST);
10208275SEric Cheng 
10218275SEric Cheng 	return (err);
10228275SEric Cheng }
10238275SEric Cheng 
10248275SEric Cheng /*
10258275SEric Cheng  * Return the current primary MAC address of the specified MAC.
10268275SEric Cheng  */
10278275SEric Cheng void
10288275SEric Cheng mac_unicast_primary_get(mac_handle_t mh, uint8_t *addr)
10298275SEric Cheng {
10308275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
10318275SEric Cheng 
10328275SEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_READER);
10338275SEric Cheng 	bcopy(mip->mi_addr, addr, mip->mi_type->mt_addr_length);
10348275SEric Cheng 	rw_exit(&mip->mi_rw_lock);
10358275SEric Cheng }
10368275SEric Cheng 
10378275SEric Cheng /*
10388275SEric Cheng  * Return information about the use of the primary MAC address of the
10398275SEric Cheng  * specified MAC instance:
10408275SEric Cheng  *
10418275SEric Cheng  * - if client_name is non-NULL, it must point to a string of at
10428275SEric Cheng  *   least MAXNAMELEN bytes, and will be set to the name of the MAC
10438275SEric Cheng  *   client which uses the primary MAC address.
10448275SEric Cheng  *
10458275SEric Cheng  * - if in_use is non-NULL, used to return whether the primary MAC
10468275SEric Cheng  *   address is currently in use.
10478275SEric Cheng  */
10488275SEric Cheng void
10498275SEric Cheng mac_unicast_primary_info(mac_handle_t mh, char *client_name, boolean_t *in_use)
10508275SEric Cheng {
10518275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
10528275SEric Cheng 	mac_client_impl_t *cur_client;
10538275SEric Cheng 
10548275SEric Cheng 	if (in_use != NULL)
10558275SEric Cheng 		*in_use = B_FALSE;
10568275SEric Cheng 	if (client_name != NULL)
10578275SEric Cheng 		bzero(client_name, MAXNAMELEN);
10588275SEric Cheng 
10598275SEric Cheng 	/*
10608275SEric Cheng 	 * The mi_rw_lock is used to protect threads that don't hold the
10618275SEric Cheng 	 * mac perimeter to get a consistent view of the mi_clients_list.
10628275SEric Cheng 	 * Threads that modify the list must hold both the mac perimeter and
10638275SEric Cheng 	 * mi_rw_lock(RW_WRITER)
10648275SEric Cheng 	 */
10658275SEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_READER);
10668275SEric Cheng 	for (cur_client = mip->mi_clients_list; cur_client != NULL;
10678275SEric Cheng 	    cur_client = cur_client->mci_client_next) {
10688275SEric Cheng 		if (mac_is_primary_client(cur_client) ||
10698275SEric Cheng 		    (mip->mi_state_flags & MIS_IS_VNIC)) {
10708275SEric Cheng 			rw_exit(&mip->mi_rw_lock);
10718275SEric Cheng 			if (in_use != NULL)
10728275SEric Cheng 				*in_use = B_TRUE;
10738275SEric Cheng 			if (client_name != NULL) {
10748275SEric Cheng 				bcopy(cur_client->mci_name, client_name,
10758275SEric Cheng 				    MAXNAMELEN);
10768275SEric Cheng 			}
10778275SEric Cheng 			return;
10788275SEric Cheng 		}
10798275SEric Cheng 	}
10808275SEric Cheng 	rw_exit(&mip->mi_rw_lock);
10818275SEric Cheng }
10828275SEric Cheng 
10838275SEric Cheng /*
108410616SSebastien.Roy@Sun.COM  * Return the current destination MAC address of the specified MAC.
108510616SSebastien.Roy@Sun.COM  */
108610616SSebastien.Roy@Sun.COM boolean_t
108710616SSebastien.Roy@Sun.COM mac_dst_get(mac_handle_t mh, uint8_t *addr)
108810616SSebastien.Roy@Sun.COM {
108910616SSebastien.Roy@Sun.COM 	mac_impl_t *mip = (mac_impl_t *)mh;
109010616SSebastien.Roy@Sun.COM 
109110616SSebastien.Roy@Sun.COM 	rw_enter(&mip->mi_rw_lock, RW_READER);
109210616SSebastien.Roy@Sun.COM 	if (mip->mi_dstaddr_set)
109310616SSebastien.Roy@Sun.COM 		bcopy(mip->mi_dstaddr, addr, mip->mi_type->mt_addr_length);
109410616SSebastien.Roy@Sun.COM 	rw_exit(&mip->mi_rw_lock);
109510616SSebastien.Roy@Sun.COM 	return (mip->mi_dstaddr_set);
109610616SSebastien.Roy@Sun.COM }
109710616SSebastien.Roy@Sun.COM 
109810616SSebastien.Roy@Sun.COM /*
10998275SEric Cheng  * Add the specified MAC client to the list of clients which opened
11008275SEric Cheng  * the specified MAC.
11018275SEric Cheng  */
11028275SEric Cheng static void
11038275SEric Cheng mac_client_add(mac_client_impl_t *mcip)
11048275SEric Cheng {
11058275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
11068275SEric Cheng 
11078275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
11088275SEric Cheng 
11098275SEric Cheng 	/* add VNIC to the front of the list */
11108275SEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
11118275SEric Cheng 	mcip->mci_client_next = mip->mi_clients_list;
11128275SEric Cheng 	mip->mi_clients_list = mcip;
11138275SEric Cheng 	mip->mi_nclients++;
11148275SEric Cheng 	rw_exit(&mip->mi_rw_lock);
11158275SEric Cheng }
11168275SEric Cheng 
11178275SEric Cheng /*
11188275SEric Cheng  * Remove the specified MAC client from the list of clients which opened
11198275SEric Cheng  * the specified MAC.
11208275SEric Cheng  */
11218275SEric Cheng static void
11228275SEric Cheng mac_client_remove(mac_client_impl_t *mcip)
11238275SEric Cheng {
11248275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
11258275SEric Cheng 	mac_client_impl_t **prev, *cclient;
11268275SEric Cheng 
11278275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
11288275SEric Cheng 
11298275SEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
11308275SEric Cheng 	prev = &mip->mi_clients_list;
11318275SEric Cheng 	cclient = *prev;
11328275SEric Cheng 	while (cclient != NULL && cclient != mcip) {
11338275SEric Cheng 		prev = &cclient->mci_client_next;
11348275SEric Cheng 		cclient = *prev;
11358275SEric Cheng 	}
11368275SEric Cheng 	ASSERT(cclient != NULL);
11378275SEric Cheng 	*prev = cclient->mci_client_next;
11388275SEric Cheng 	mip->mi_nclients--;
11398275SEric Cheng 	rw_exit(&mip->mi_rw_lock);
11408275SEric Cheng }
11418275SEric Cheng 
11428275SEric Cheng static mac_unicast_impl_t *
11438275SEric Cheng mac_client_find_vid(mac_client_impl_t *mcip, uint16_t vid)
11448275SEric Cheng {
11458275SEric Cheng 	mac_unicast_impl_t *muip = mcip->mci_unicast_list;
11468275SEric Cheng 
11478275SEric Cheng 	while ((muip != NULL) && (muip->mui_vid != vid))
11488275SEric Cheng 		muip = muip->mui_next;
11498275SEric Cheng 
11508275SEric Cheng 	return (muip);
11518275SEric Cheng }
11528275SEric Cheng 
11538275SEric Cheng /*
11548275SEric Cheng  * Return whether the specified (MAC address, VID) tuple is already used by
11558275SEric Cheng  * one of the MAC clients associated with the specified MAC.
11568275SEric Cheng  */
11578275SEric Cheng static boolean_t
11588275SEric Cheng mac_addr_in_use(mac_impl_t *mip, uint8_t *mac_addr, uint16_t vid)
11598275SEric Cheng {
11608275SEric Cheng 	mac_client_impl_t *client;
11618275SEric Cheng 	mac_address_t *map;
11628275SEric Cheng 
11638275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
11648275SEric Cheng 
11658275SEric Cheng 	for (client = mip->mi_clients_list; client != NULL;
11668275SEric Cheng 	    client = client->mci_client_next) {
11678275SEric Cheng 
11688275SEric Cheng 		/*
11698275SEric Cheng 		 * Ignore clients that don't have unicast address.
11708275SEric Cheng 		 */
11718275SEric Cheng 		if (client->mci_unicast_list == NULL)
11728275SEric Cheng 			continue;
11738275SEric Cheng 
11748275SEric Cheng 		map = client->mci_unicast;
11758275SEric Cheng 
11768275SEric Cheng 		if ((bcmp(mac_addr, map->ma_addr, map->ma_len) == 0) &&
11778275SEric Cheng 		    (mac_client_find_vid(client, vid) != NULL)) {
11788275SEric Cheng 			return (B_TRUE);
11798275SEric Cheng 		}
11808275SEric Cheng 	}
11818275SEric Cheng 
11828275SEric Cheng 	return (B_FALSE);
11838275SEric Cheng }
11848275SEric Cheng 
11858275SEric Cheng /*
11868275SEric Cheng  * Generate a random MAC address. The MAC address prefix is
11878275SEric Cheng  * stored in the array pointed to by mac_addr, and its length, in bytes,
11888275SEric Cheng  * is specified by prefix_len. The least significant bits
11898275SEric Cheng  * after prefix_len bytes are generated, and stored after the prefix
11908275SEric Cheng  * in the mac_addr array.
11918275SEric Cheng  */
11928275SEric Cheng int
11938275SEric Cheng mac_addr_random(mac_client_handle_t mch, uint_t prefix_len,
11948275SEric Cheng     uint8_t *mac_addr, mac_diag_t *diag)
11958275SEric Cheng {
11968275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
11978275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
11988275SEric Cheng 	size_t addr_len = mip->mi_type->mt_addr_length;
11998275SEric Cheng 
12008275SEric Cheng 	if (prefix_len >= addr_len) {
12018275SEric Cheng 		*diag = MAC_DIAG_MACPREFIXLEN_INVALID;
12028275SEric Cheng 		return (EINVAL);
12038275SEric Cheng 	}
12048275SEric Cheng 
12058275SEric Cheng 	/* check the prefix value */
12068275SEric Cheng 	if (prefix_len > 0) {
12078275SEric Cheng 		bzero(mac_addr + prefix_len, addr_len - prefix_len);
12088275SEric Cheng 		if (!mac_unicst_verify((mac_handle_t)mip, mac_addr,
12098275SEric Cheng 		    addr_len)) {
12108275SEric Cheng 			*diag = MAC_DIAG_MACPREFIX_INVALID;
12118275SEric Cheng 			return (EINVAL);
12128275SEric Cheng 		}
12138275SEric Cheng 	}
12148275SEric Cheng 
12158275SEric Cheng 	/* generate the MAC address */
12168275SEric Cheng 	if (prefix_len < addr_len) {
12178275SEric Cheng 		(void) random_get_pseudo_bytes(mac_addr +
12188275SEric Cheng 		    prefix_len, addr_len - prefix_len);
12198275SEric Cheng 	}
12208275SEric Cheng 
12218275SEric Cheng 	*diag = 0;
12228275SEric Cheng 	return (0);
12238275SEric Cheng }
12248275SEric Cheng 
12258275SEric Cheng /*
12268275SEric Cheng  * Set the priority range for this MAC client. This will be used to
12278275SEric Cheng  * determine the absolute priority for the threads created for this
12288275SEric Cheng  * MAC client using the specified "low", "medium" and "high" level.
12298275SEric Cheng  * This will also be used for any subflows on this MAC client.
12308275SEric Cheng  */
12318275SEric Cheng #define	MAC_CLIENT_SET_PRIORITY_RANGE(mcip, pri) {			\
12328275SEric Cheng 	(mcip)->mci_min_pri = FLOW_MIN_PRIORITY(MINCLSYSPRI,	\
12338275SEric Cheng 	    MAXCLSYSPRI, (pri));					\
12348275SEric Cheng 	(mcip)->mci_max_pri = FLOW_MAX_PRIORITY(MINCLSYSPRI,	\
12358275SEric Cheng 	    MAXCLSYSPRI, (mcip)->mci_min_pri);				\
12368275SEric Cheng 	}
12378275SEric Cheng 
12388275SEric Cheng /*
12398275SEric Cheng  * MAC client open entry point. Return a new MAC client handle. Each
12408275SEric Cheng  * MAC client is associated with a name, specified through the 'name'
12418275SEric Cheng  * argument.
12428275SEric Cheng  */
12438275SEric Cheng int
12448275SEric Cheng mac_client_open(mac_handle_t mh, mac_client_handle_t *mchp, char *name,
12458275SEric Cheng     uint16_t flags)
12468275SEric Cheng {
1247*11878SVenu.Iyer@Sun.COM 	mac_impl_t		*mip = (mac_impl_t *)mh;
1248*11878SVenu.Iyer@Sun.COM 	mac_client_impl_t	*mcip;
1249*11878SVenu.Iyer@Sun.COM 	int			err = 0;
1250*11878SVenu.Iyer@Sun.COM 	boolean_t		share_desired;
1251*11878SVenu.Iyer@Sun.COM 	flow_entry_t		*flent = NULL;
1252*11878SVenu.Iyer@Sun.COM 
1253*11878SVenu.Iyer@Sun.COM 	share_desired = (flags & MAC_OPEN_FLAGS_SHARES_DESIRED) != 0;
12548275SEric Cheng 	*mchp = NULL;
12558275SEric Cheng 
12568275SEric Cheng 	i_mac_perim_enter(mip);
12578275SEric Cheng 
12588275SEric Cheng 	if (mip->mi_state_flags & MIS_IS_VNIC) {
12598275SEric Cheng 		/*
12608275SEric Cheng 		 * The underlying MAC is a VNIC. Return the MAC client
12618275SEric Cheng 		 * handle of the lower MAC which was obtained by
12628275SEric Cheng 		 * the VNIC driver when it did its mac_client_open().
12638275SEric Cheng 		 */
12648275SEric Cheng 
12658275SEric Cheng 		mcip = mac_vnic_lower(mip);
12668275SEric Cheng 
12678275SEric Cheng 		/*
12688275SEric Cheng 		 * Note that multiple mac clients share the same mcip in
12698275SEric Cheng 		 * this case.
12708275SEric Cheng 		 */
12718275SEric Cheng 		if (flags & MAC_OPEN_FLAGS_EXCLUSIVE)
12728275SEric Cheng 			mcip->mci_state_flags |= MCIS_EXCLUSIVE;
12738275SEric Cheng 
12749473SVenu.Iyer@Sun.COM 		if (flags & MAC_OPEN_FLAGS_MULTI_PRIMARY)
12759473SVenu.Iyer@Sun.COM 			mcip->mci_flags |= MAC_CLIENT_FLAGS_MULTI_PRIMARY;
12769473SVenu.Iyer@Sun.COM 
12778275SEric Cheng 		mip->mi_clients_list = mcip;
12788275SEric Cheng 		i_mac_perim_exit(mip);
12798275SEric Cheng 		*mchp = (mac_client_handle_t)mcip;
12808275SEric Cheng 		return (err);
12818275SEric Cheng 	}
12828275SEric Cheng 
12838275SEric Cheng 	mcip = kmem_cache_alloc(mac_client_impl_cache, KM_SLEEP);
12848275SEric Cheng 
12858275SEric Cheng 	mcip->mci_mip = mip;
12868275SEric Cheng 	mcip->mci_upper_mip = NULL;
12878275SEric Cheng 	mcip->mci_rx_fn = mac_pkt_drop;
12888275SEric Cheng 	mcip->mci_rx_arg = NULL;
12899473SVenu.Iyer@Sun.COM 	mcip->mci_rx_p_fn = NULL;
12909473SVenu.Iyer@Sun.COM 	mcip->mci_rx_p_arg = NULL;
12919473SVenu.Iyer@Sun.COM 	mcip->mci_p_unicast_list = NULL;
12928275SEric Cheng 	mcip->mci_direct_rx_fn = NULL;
12938275SEric Cheng 	mcip->mci_direct_rx_arg = NULL;
12948275SEric Cheng 
12959473SVenu.Iyer@Sun.COM 	mcip->mci_unicast_list = NULL;
12969473SVenu.Iyer@Sun.COM 
12978275SEric Cheng 	if ((flags & MAC_OPEN_FLAGS_IS_VNIC) != 0)
12988275SEric Cheng 		mcip->mci_state_flags |= MCIS_IS_VNIC;
12998275SEric Cheng 
13008275SEric Cheng 	if ((flags & MAC_OPEN_FLAGS_EXCLUSIVE) != 0)
13018275SEric Cheng 		mcip->mci_state_flags |= MCIS_EXCLUSIVE;
13028275SEric Cheng 
13038275SEric Cheng 	if ((flags & MAC_OPEN_FLAGS_IS_AGGR_PORT) != 0)
13048275SEric Cheng 		mcip->mci_state_flags |= MCIS_IS_AGGR_PORT;
13058275SEric Cheng 
1306*11878SVenu.Iyer@Sun.COM 	if (mip->mi_state_flags & MIS_IS_AGGR)
1307*11878SVenu.Iyer@Sun.COM 		mcip->mci_state_flags |= MCIS_IS_AGGR;
1308*11878SVenu.Iyer@Sun.COM 
13098275SEric Cheng 	if ((flags & MAC_OPEN_FLAGS_USE_DATALINK_NAME) != 0) {
13108275SEric Cheng 		datalink_id_t	linkid;
13118275SEric Cheng 
13128275SEric Cheng 		ASSERT(name == NULL);
13138275SEric Cheng 		if ((err = dls_devnet_macname2linkid(mip->mi_name,
13148275SEric Cheng 		    &linkid)) != 0) {
13158275SEric Cheng 			goto done;
13168275SEric Cheng 		}
13178275SEric Cheng 		if ((err = dls_mgmt_get_linkinfo(linkid, mcip->mci_name, NULL,
13188275SEric Cheng 		    NULL, NULL)) != 0) {
13198275SEric Cheng 			/*
13208275SEric Cheng 			 * Use mac name if dlmgmtd is not available.
13218275SEric Cheng 			 */
13228275SEric Cheng 			if (err == EBADF) {
13238275SEric Cheng 				(void) strlcpy(mcip->mci_name, mip->mi_name,
13248275SEric Cheng 				    sizeof (mcip->mci_name));
13258275SEric Cheng 				err = 0;
13268275SEric Cheng 			} else {
13278275SEric Cheng 				goto done;
13288275SEric Cheng 			}
13298275SEric Cheng 		}
13308275SEric Cheng 		mcip->mci_state_flags |= MCIS_USE_DATALINK_NAME;
13318275SEric Cheng 	} else {
13328275SEric Cheng 		ASSERT(name != NULL);
13338275SEric Cheng 		if (strlen(name) > MAXNAMELEN) {
13348275SEric Cheng 			err = EINVAL;
13358275SEric Cheng 			goto done;
13368275SEric Cheng 		}
13378275SEric Cheng 		(void) strlcpy(mcip->mci_name, name, sizeof (mcip->mci_name));
13388275SEric Cheng 	}
13399473SVenu.Iyer@Sun.COM 
13409473SVenu.Iyer@Sun.COM 	if (flags & MAC_OPEN_FLAGS_MULTI_PRIMARY)
13419473SVenu.Iyer@Sun.COM 		mcip->mci_flags |= MAC_CLIENT_FLAGS_MULTI_PRIMARY;
13429473SVenu.Iyer@Sun.COM 
1343*11878SVenu.Iyer@Sun.COM 	if (flags & MAC_OPEN_FLAGS_NO_UNICAST_ADDR)
1344*11878SVenu.Iyer@Sun.COM 		mcip->mci_state_flags |= MCIS_NO_UNICAST_ADDR;
1345*11878SVenu.Iyer@Sun.COM 
1346*11878SVenu.Iyer@Sun.COM 	mac_protect_init(mcip);
1347*11878SVenu.Iyer@Sun.COM 
13488275SEric Cheng 	/* the subflow table will be created dynamically */
13498275SEric Cheng 	mcip->mci_subflow_tab = NULL;
1350*11878SVenu.Iyer@Sun.COM 
1351*11878SVenu.Iyer@Sun.COM 	mcip->mci_misc_stat.mms_multircv = 0;
1352*11878SVenu.Iyer@Sun.COM 	mcip->mci_misc_stat.mms_brdcstrcv = 0;
1353*11878SVenu.Iyer@Sun.COM 	mcip->mci_misc_stat.mms_multixmt = 0;
1354*11878SVenu.Iyer@Sun.COM 	mcip->mci_misc_stat.mms_brdcstxmt = 0;
13558275SEric Cheng 
13568275SEric Cheng 	/* Create an initial flow */
13578275SEric Cheng 
13588275SEric Cheng 	err = mac_flow_create(NULL, NULL, mcip->mci_name, NULL,
13598275SEric Cheng 	    mcip->mci_state_flags & MCIS_IS_VNIC ? FLOW_VNIC_MAC :
13608275SEric Cheng 	    FLOW_PRIMARY_MAC, &flent);
13618275SEric Cheng 	if (err != 0)
13628275SEric Cheng 		goto done;
13638275SEric Cheng 	mcip->mci_flent = flent;
13648275SEric Cheng 	FLOW_MARK(flent, FE_MC_NO_DATAPATH);
13658275SEric Cheng 	flent->fe_mcip = mcip;
13668275SEric Cheng 	/*
13678275SEric Cheng 	 * Place initial creation reference on the flow. This reference
13688275SEric Cheng 	 * is released in the corresponding delete action viz.
13698275SEric Cheng 	 * mac_unicast_remove after waiting for all transient refs to
13708275SEric Cheng 	 * to go away. The wait happens in mac_flow_wait.
13718275SEric Cheng 	 */
13728275SEric Cheng 	FLOW_REFHOLD(flent);
13738275SEric Cheng 
13748275SEric Cheng 	/*
13758275SEric Cheng 	 * Do this ahead of the mac_bcast_add() below so that the mi_nclients
13768275SEric Cheng 	 * will have the right value for mac_rx_srs_setup().
13778275SEric Cheng 	 */
13788275SEric Cheng 	mac_client_add(mcip);
13798275SEric Cheng 
13808275SEric Cheng 	mcip->mci_share = NULL;
1381*11878SVenu.Iyer@Sun.COM 	if (share_desired)
13828275SEric Cheng 		i_mac_share_alloc(mcip);
13838275SEric Cheng 
13848275SEric Cheng 	DTRACE_PROBE2(mac__client__open__allocated, mac_impl_t *,
13858275SEric Cheng 	    mcip->mci_mip, mac_client_impl_t *, mcip);
13868275SEric Cheng 	*mchp = (mac_client_handle_t)mcip;
13878275SEric Cheng 
1388*11878SVenu.Iyer@Sun.COM 	/*
1389*11878SVenu.Iyer@Sun.COM 	 * We will do mimimal datapath setup to allow a MAC client to
1390*11878SVenu.Iyer@Sun.COM 	 * transmit or receive non-unicast packets without waiting
1391*11878SVenu.Iyer@Sun.COM 	 * for mac_unicast_add.
1392*11878SVenu.Iyer@Sun.COM 	 */
1393*11878SVenu.Iyer@Sun.COM 	if (mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR) {
1394*11878SVenu.Iyer@Sun.COM 		if ((err = mac_client_datapath_setup(mcip, VLAN_ID_NONE,
1395*11878SVenu.Iyer@Sun.COM 		    NULL, NULL, B_TRUE, NULL)) != 0) {
1396*11878SVenu.Iyer@Sun.COM 			goto done;
1397*11878SVenu.Iyer@Sun.COM 		}
1398*11878SVenu.Iyer@Sun.COM 	}
13998275SEric Cheng 	i_mac_perim_exit(mip);
14008275SEric Cheng 	return (0);
14018275SEric Cheng 
14028275SEric Cheng done:
14038275SEric Cheng 	i_mac_perim_exit(mip);
14048275SEric Cheng 	mcip->mci_state_flags = 0;
14058275SEric Cheng 	mcip->mci_tx_flag = 0;
14068275SEric Cheng 	kmem_cache_free(mac_client_impl_cache, mcip);
14078275SEric Cheng 	return (err);
14088275SEric Cheng }
14098275SEric Cheng 
14108275SEric Cheng /*
14118275SEric Cheng  * Close the specified MAC client handle.
14128275SEric Cheng  */
14138275SEric Cheng void
14148275SEric Cheng mac_client_close(mac_client_handle_t mch, uint16_t flags)
14158275SEric Cheng {
14168275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
14178275SEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
14188275SEric Cheng 	flow_entry_t		*flent;
14198275SEric Cheng 
14208275SEric Cheng 	i_mac_perim_enter(mip);
14218275SEric Cheng 
14228275SEric Cheng 	if (flags & MAC_CLOSE_FLAGS_EXCLUSIVE)
14238275SEric Cheng 		mcip->mci_state_flags &= ~MCIS_EXCLUSIVE;
14248275SEric Cheng 
14258275SEric Cheng 	if ((mcip->mci_state_flags & MCIS_IS_VNIC) &&
14268275SEric Cheng 	    !(flags & MAC_CLOSE_FLAGS_IS_VNIC)) {
14278275SEric Cheng 		/*
14288275SEric Cheng 		 * This is an upper VNIC client initiated operation.
14298275SEric Cheng 		 * The lower MAC client will be closed by the VNIC driver
14308275SEric Cheng 		 * when the VNIC is deleted.
14318275SEric Cheng 		 */
14328275SEric Cheng 
14338275SEric Cheng 		i_mac_perim_exit(mip);
14348275SEric Cheng 		return;
14358275SEric Cheng 	}
14368275SEric Cheng 
1437*11878SVenu.Iyer@Sun.COM 	/* If we have only setup up minimal datapth setup, tear it down */
1438*11878SVenu.Iyer@Sun.COM 	if (mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR) {
1439*11878SVenu.Iyer@Sun.COM 		mac_client_datapath_teardown((mac_client_handle_t)mcip, NULL,
1440*11878SVenu.Iyer@Sun.COM 		    mcip->mci_flent);
1441*11878SVenu.Iyer@Sun.COM 		mcip->mci_state_flags &= ~MCIS_NO_UNICAST_ADDR;
1442*11878SVenu.Iyer@Sun.COM 	}
1443*11878SVenu.Iyer@Sun.COM 
14448275SEric Cheng 	/*
14458275SEric Cheng 	 * Remove the flent associated with the MAC client
14468275SEric Cheng 	 */
14478275SEric Cheng 	flent = mcip->mci_flent;
14488275SEric Cheng 	mcip->mci_flent = NULL;
14498275SEric Cheng 	FLOW_FINAL_REFRELE(flent);
14508275SEric Cheng 
14518275SEric Cheng 	/*
14528275SEric Cheng 	 * MAC clients must remove the unicast addresses and promisc callbacks
14538275SEric Cheng 	 * they added before issuing a mac_client_close().
14548275SEric Cheng 	 */
14558275SEric Cheng 	ASSERT(mcip->mci_unicast_list == NULL);
14568275SEric Cheng 	ASSERT(mcip->mci_promisc_list == NULL);
14578275SEric Cheng 	ASSERT(mcip->mci_tx_notify_cb_list == NULL);
14588275SEric Cheng 
14598275SEric Cheng 	i_mac_share_free(mcip);
1460*11878SVenu.Iyer@Sun.COM 	mac_protect_fini(mcip);
14618275SEric Cheng 	mac_client_remove(mcip);
14628275SEric Cheng 
14638275SEric Cheng 	i_mac_perim_exit(mip);
14648275SEric Cheng 	mcip->mci_subflow_tab = NULL;
14658275SEric Cheng 	mcip->mci_state_flags = 0;
14668275SEric Cheng 	mcip->mci_tx_flag = 0;
14678275SEric Cheng 	kmem_cache_free(mac_client_impl_cache, mch);
14688275SEric Cheng }
14698275SEric Cheng 
14708275SEric Cheng /*
147111021SEric.Cheng@Sun.COM  * Set the rx bypass receive callback.
14728275SEric Cheng  */
14738275SEric Cheng boolean_t
14748275SEric Cheng mac_rx_bypass_set(mac_client_handle_t mch, mac_direct_rx_t rx_fn, void *arg1)
14758275SEric Cheng {
14768275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
14778275SEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
14788275SEric Cheng 
14798275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
14808275SEric Cheng 
14818275SEric Cheng 	/*
14828833SVenu.Iyer@Sun.COM 	 * If the mac_client is a VLAN, we should not do DLS bypass and
14838833SVenu.Iyer@Sun.COM 	 * instead let the packets come up via mac_rx_deliver so the vlan
14848833SVenu.Iyer@Sun.COM 	 * header can be stripped.
14858275SEric Cheng 	 */
14868833SVenu.Iyer@Sun.COM 	if (mcip->mci_nvids > 0)
14878275SEric Cheng 		return (B_FALSE);
14888275SEric Cheng 
14898275SEric Cheng 	/*
14908275SEric Cheng 	 * These are not accessed directly in the data path, and hence
14918275SEric Cheng 	 * don't need any protection
14928275SEric Cheng 	 */
14938275SEric Cheng 	mcip->mci_direct_rx_fn = rx_fn;
14948275SEric Cheng 	mcip->mci_direct_rx_arg = arg1;
14958275SEric Cheng 	return (B_TRUE);
14968275SEric Cheng }
14978275SEric Cheng 
14988275SEric Cheng /*
149911021SEric.Cheng@Sun.COM  * Enable/Disable rx bypass. By default, bypass is assumed to be enabled.
150011021SEric.Cheng@Sun.COM  */
150111021SEric.Cheng@Sun.COM void
150211021SEric.Cheng@Sun.COM mac_rx_bypass_enable(mac_client_handle_t mch)
150311021SEric.Cheng@Sun.COM {
150411021SEric.Cheng@Sun.COM 	((mac_client_impl_t *)mch)->mci_state_flags &= ~MCIS_RX_BYPASS_DISABLE;
150511021SEric.Cheng@Sun.COM }
150611021SEric.Cheng@Sun.COM 
150711021SEric.Cheng@Sun.COM void
150811021SEric.Cheng@Sun.COM mac_rx_bypass_disable(mac_client_handle_t mch)
150911021SEric.Cheng@Sun.COM {
151011021SEric.Cheng@Sun.COM 	((mac_client_impl_t *)mch)->mci_state_flags |= MCIS_RX_BYPASS_DISABLE;
151111021SEric.Cheng@Sun.COM }
151211021SEric.Cheng@Sun.COM 
151311021SEric.Cheng@Sun.COM /*
15148275SEric Cheng  * Set the receive callback for the specified MAC client. There can be
15158275SEric Cheng  * at most one such callback per MAC client.
15168275SEric Cheng  */
15178275SEric Cheng void
15188275SEric Cheng mac_rx_set(mac_client_handle_t mch, mac_rx_t rx_fn, void *arg)
15198275SEric Cheng {
15208275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
15218275SEric Cheng 	mac_impl_t	*mip = mcip->mci_mip;
15228275SEric Cheng 
15238275SEric Cheng 	/*
15248275SEric Cheng 	 * Instead of adding an extra set of locks and refcnts in
15258275SEric Cheng 	 * the datapath at the mac client boundary, we temporarily quiesce
15268275SEric Cheng 	 * the SRS and related entities. We then change the receive function
15278275SEric Cheng 	 * without interference from any receive data thread and then reenable
15288275SEric Cheng 	 * the data flow subsequently.
15298275SEric Cheng 	 */
15308275SEric Cheng 	i_mac_perim_enter(mip);
15318275SEric Cheng 	mac_rx_client_quiesce(mch);
15328275SEric Cheng 
15338275SEric Cheng 	mcip->mci_rx_fn = rx_fn;
15348275SEric Cheng 	mcip->mci_rx_arg = arg;
15358275SEric Cheng 	mac_rx_client_restart(mch);
15368275SEric Cheng 	i_mac_perim_exit(mip);
15378275SEric Cheng }
15388275SEric Cheng 
15398275SEric Cheng /*
15408275SEric Cheng  * Reset the receive callback for the specified MAC client.
15418275SEric Cheng  */
15428275SEric Cheng void
15438275SEric Cheng mac_rx_clear(mac_client_handle_t mch)
15448275SEric Cheng {
15458275SEric Cheng 	mac_rx_set(mch, mac_pkt_drop, NULL);
15468275SEric Cheng }
15478275SEric Cheng 
15488275SEric Cheng /*
15498275SEric Cheng  * Walk the MAC client subflow table and updates their priority values.
15508275SEric Cheng  */
15518275SEric Cheng static int
15528275SEric Cheng mac_update_subflow_priority_cb(flow_entry_t *flent, void *arg)
15538275SEric Cheng {
15548275SEric Cheng 	mac_flow_update_priority(arg, flent);
15558275SEric Cheng 	return (0);
15568275SEric Cheng }
15578275SEric Cheng 
15588275SEric Cheng void
15598275SEric Cheng mac_update_subflow_priority(mac_client_impl_t *mcip)
15608275SEric Cheng {
15618275SEric Cheng 	(void) mac_flow_walk(mcip->mci_subflow_tab,
15628275SEric Cheng 	    mac_update_subflow_priority_cb, mcip);
15638275SEric Cheng }
15648275SEric Cheng 
15658275SEric Cheng /*
1566*11878SVenu.Iyer@Sun.COM  * Modify the TX or RX ring properties. We could either just move around
1567*11878SVenu.Iyer@Sun.COM  * rings, i.e add/remove rings given to a client. Or this might cause the
1568*11878SVenu.Iyer@Sun.COM  * client to move from hardware based to software or the other way around.
1569*11878SVenu.Iyer@Sun.COM  * If we want to reset this property, then we clear the mask, additionally
1570*11878SVenu.Iyer@Sun.COM  * if the client was given a non-default group we remove all rings except
1571*11878SVenu.Iyer@Sun.COM  * for 1 and give it back to the default group.
1572*11878SVenu.Iyer@Sun.COM  */
1573*11878SVenu.Iyer@Sun.COM int
1574*11878SVenu.Iyer@Sun.COM mac_client_set_rings_prop(mac_client_impl_t *mcip, mac_resource_props_t *mrp,
1575*11878SVenu.Iyer@Sun.COM     mac_resource_props_t *tmrp)
1576*11878SVenu.Iyer@Sun.COM {
1577*11878SVenu.Iyer@Sun.COM 	mac_impl_t		*mip = mcip->mci_mip;
1578*11878SVenu.Iyer@Sun.COM 	flow_entry_t		*flent = mcip->mci_flent;
1579*11878SVenu.Iyer@Sun.COM 	uint8_t			*mac_addr;
1580*11878SVenu.Iyer@Sun.COM 	int			err = 0;
1581*11878SVenu.Iyer@Sun.COM 	mac_group_t		*defgrp;
1582*11878SVenu.Iyer@Sun.COM 	mac_group_t		*group;
1583*11878SVenu.Iyer@Sun.COM 	mac_group_t		*ngrp;
1584*11878SVenu.Iyer@Sun.COM 	mac_resource_props_t	*cmrp = MCIP_RESOURCE_PROPS(mcip);
1585*11878SVenu.Iyer@Sun.COM 	uint_t			ringcnt;
1586*11878SVenu.Iyer@Sun.COM 	boolean_t		unspec;
1587*11878SVenu.Iyer@Sun.COM 
1588*11878SVenu.Iyer@Sun.COM 	if (mcip->mci_share != NULL)
1589*11878SVenu.Iyer@Sun.COM 		return (EINVAL);
1590*11878SVenu.Iyer@Sun.COM 
1591*11878SVenu.Iyer@Sun.COM 	if (mrp->mrp_mask & MRP_RX_RINGS) {
1592*11878SVenu.Iyer@Sun.COM 		unspec = mrp->mrp_mask & MRP_RXRINGS_UNSPEC;
1593*11878SVenu.Iyer@Sun.COM 		group = flent->fe_rx_ring_group;
1594*11878SVenu.Iyer@Sun.COM 		defgrp = MAC_DEFAULT_RX_GROUP(mip);
1595*11878SVenu.Iyer@Sun.COM 		mac_addr = flent->fe_flow_desc.fd_dst_mac;
1596*11878SVenu.Iyer@Sun.COM 
1597*11878SVenu.Iyer@Sun.COM 		/*
1598*11878SVenu.Iyer@Sun.COM 		 * No resulting change. If we are resetting on a client on
1599*11878SVenu.Iyer@Sun.COM 		 * which there was no rx rings property. For dynamic group
1600*11878SVenu.Iyer@Sun.COM 		 * if we are setting the same number of rings already set.
1601*11878SVenu.Iyer@Sun.COM 		 * For static group if we are requesting a group again.
1602*11878SVenu.Iyer@Sun.COM 		 */
1603*11878SVenu.Iyer@Sun.COM 		if (mrp->mrp_mask & MRP_RINGS_RESET) {
1604*11878SVenu.Iyer@Sun.COM 			if (!(tmrp->mrp_mask & MRP_RX_RINGS))
1605*11878SVenu.Iyer@Sun.COM 				return (0);
1606*11878SVenu.Iyer@Sun.COM 		} else {
1607*11878SVenu.Iyer@Sun.COM 			if (unspec) {
1608*11878SVenu.Iyer@Sun.COM 				if (tmrp->mrp_mask & MRP_RXRINGS_UNSPEC)
1609*11878SVenu.Iyer@Sun.COM 					return (0);
1610*11878SVenu.Iyer@Sun.COM 			} else if (mip->mi_rx_group_type ==
1611*11878SVenu.Iyer@Sun.COM 			    MAC_GROUP_TYPE_DYNAMIC) {
1612*11878SVenu.Iyer@Sun.COM 				if ((tmrp->mrp_mask & MRP_RX_RINGS) &&
1613*11878SVenu.Iyer@Sun.COM 				    !(tmrp->mrp_mask & MRP_RXRINGS_UNSPEC) &&
1614*11878SVenu.Iyer@Sun.COM 				    mrp->mrp_nrxrings == tmrp->mrp_nrxrings) {
1615*11878SVenu.Iyer@Sun.COM 					return (0);
1616*11878SVenu.Iyer@Sun.COM 				}
1617*11878SVenu.Iyer@Sun.COM 			}
1618*11878SVenu.Iyer@Sun.COM 		}
1619*11878SVenu.Iyer@Sun.COM 		/* Resetting the prop */
1620*11878SVenu.Iyer@Sun.COM 		if (mrp->mrp_mask & MRP_RINGS_RESET) {
1621*11878SVenu.Iyer@Sun.COM 			/*
1622*11878SVenu.Iyer@Sun.COM 			 * We will just keep one ring and give others back if
1623*11878SVenu.Iyer@Sun.COM 			 * we are not the primary. For the primary we give
1624*11878SVenu.Iyer@Sun.COM 			 * all the rings in the default group except the
1625*11878SVenu.Iyer@Sun.COM 			 * default ring. If it is a static group, then
1626*11878SVenu.Iyer@Sun.COM 			 * we don't do anything, but clear the MRP_RX_RINGS
1627*11878SVenu.Iyer@Sun.COM 			 * flag.
1628*11878SVenu.Iyer@Sun.COM 			 */
1629*11878SVenu.Iyer@Sun.COM 			if (group != defgrp) {
1630*11878SVenu.Iyer@Sun.COM 				if (mip->mi_rx_group_type ==
1631*11878SVenu.Iyer@Sun.COM 				    MAC_GROUP_TYPE_DYNAMIC) {
1632*11878SVenu.Iyer@Sun.COM 					/*
1633*11878SVenu.Iyer@Sun.COM 					 * This group has reserved rings
1634*11878SVenu.Iyer@Sun.COM 					 * that need to be released now,
1635*11878SVenu.Iyer@Sun.COM 					 * so does the group.
1636*11878SVenu.Iyer@Sun.COM 					 */
1637*11878SVenu.Iyer@Sun.COM 					MAC_RX_RING_RELEASED(mip,
1638*11878SVenu.Iyer@Sun.COM 					    group->mrg_cur_count);
1639*11878SVenu.Iyer@Sun.COM 					MAC_RX_GRP_RELEASED(mip);
1640*11878SVenu.Iyer@Sun.COM 					if ((flent->fe_type &
1641*11878SVenu.Iyer@Sun.COM 					    FLOW_PRIMARY_MAC) != 0) {
1642*11878SVenu.Iyer@Sun.COM 						if (mip->mi_nactiveclients ==
1643*11878SVenu.Iyer@Sun.COM 						    1) {
1644*11878SVenu.Iyer@Sun.COM 							(void)
1645*11878SVenu.Iyer@Sun.COM 							    mac_rx_switch_group(
1646*11878SVenu.Iyer@Sun.COM 							    mcip, group,
1647*11878SVenu.Iyer@Sun.COM 							    defgrp);
1648*11878SVenu.Iyer@Sun.COM 							return (0);
1649*11878SVenu.Iyer@Sun.COM 						} else {
1650*11878SVenu.Iyer@Sun.COM 							cmrp->mrp_nrxrings =
1651*11878SVenu.Iyer@Sun.COM 							    group->
1652*11878SVenu.Iyer@Sun.COM 							    mrg_cur_count +
1653*11878SVenu.Iyer@Sun.COM 							    defgrp->
1654*11878SVenu.Iyer@Sun.COM 							    mrg_cur_count - 1;
1655*11878SVenu.Iyer@Sun.COM 						}
1656*11878SVenu.Iyer@Sun.COM 					} else {
1657*11878SVenu.Iyer@Sun.COM 						cmrp->mrp_nrxrings = 1;
1658*11878SVenu.Iyer@Sun.COM 					}
1659*11878SVenu.Iyer@Sun.COM 					(void) mac_group_ring_modify(mcip,
1660*11878SVenu.Iyer@Sun.COM 					    group, defgrp);
1661*11878SVenu.Iyer@Sun.COM 				} else {
1662*11878SVenu.Iyer@Sun.COM 					/*
1663*11878SVenu.Iyer@Sun.COM 					 * If this is a static group, we
1664*11878SVenu.Iyer@Sun.COM 					 * need to release the group. The
1665*11878SVenu.Iyer@Sun.COM 					 * client will remain in the same
1666*11878SVenu.Iyer@Sun.COM 					 * group till some other client
1667*11878SVenu.Iyer@Sun.COM 					 * needs this group.
1668*11878SVenu.Iyer@Sun.COM 					 */
1669*11878SVenu.Iyer@Sun.COM 					MAC_RX_GRP_RELEASED(mip);
1670*11878SVenu.Iyer@Sun.COM 				}
1671*11878SVenu.Iyer@Sun.COM 			/* Let check if we can give this an excl group */
1672*11878SVenu.Iyer@Sun.COM 			} else if (group == defgrp) {
1673*11878SVenu.Iyer@Sun.COM 				ngrp = 	mac_reserve_rx_group(mcip, mac_addr,
1674*11878SVenu.Iyer@Sun.COM 				    B_TRUE);
1675*11878SVenu.Iyer@Sun.COM 				/* Couldn't give it a group, that's fine */
1676*11878SVenu.Iyer@Sun.COM 				if (ngrp == NULL)
1677*11878SVenu.Iyer@Sun.COM 					return (0);
1678*11878SVenu.Iyer@Sun.COM 				/* Switch to H/W */
1679*11878SVenu.Iyer@Sun.COM 				if (mac_rx_switch_group(mcip, defgrp, ngrp) !=
1680*11878SVenu.Iyer@Sun.COM 				    0) {
1681*11878SVenu.Iyer@Sun.COM 					mac_stop_group(ngrp);
1682*11878SVenu.Iyer@Sun.COM 					return (0);
1683*11878SVenu.Iyer@Sun.COM 				}
1684*11878SVenu.Iyer@Sun.COM 			}
1685*11878SVenu.Iyer@Sun.COM 			/*
1686*11878SVenu.Iyer@Sun.COM 			 * If the client is in the default group, we will
1687*11878SVenu.Iyer@Sun.COM 			 * just clear the MRP_RX_RINGS and leave it as
1688*11878SVenu.Iyer@Sun.COM 			 * it rather than look for an exclusive group
1689*11878SVenu.Iyer@Sun.COM 			 * for it.
1690*11878SVenu.Iyer@Sun.COM 			 */
1691*11878SVenu.Iyer@Sun.COM 			return (0);
1692*11878SVenu.Iyer@Sun.COM 		}
1693*11878SVenu.Iyer@Sun.COM 
1694*11878SVenu.Iyer@Sun.COM 		if (group == defgrp && ((mrp->mrp_nrxrings > 0) || unspec)) {
1695*11878SVenu.Iyer@Sun.COM 			ngrp = 	mac_reserve_rx_group(mcip, mac_addr, B_TRUE);
1696*11878SVenu.Iyer@Sun.COM 			if (ngrp == NULL)
1697*11878SVenu.Iyer@Sun.COM 				return (ENOSPC);
1698*11878SVenu.Iyer@Sun.COM 
1699*11878SVenu.Iyer@Sun.COM 			/* Switch to H/W */
1700*11878SVenu.Iyer@Sun.COM 			if (mac_rx_switch_group(mcip, defgrp, ngrp) != 0) {
1701*11878SVenu.Iyer@Sun.COM 				mac_release_rx_group(mcip, ngrp);
1702*11878SVenu.Iyer@Sun.COM 				return (ENOSPC);
1703*11878SVenu.Iyer@Sun.COM 			}
1704*11878SVenu.Iyer@Sun.COM 			MAC_RX_GRP_RESERVED(mip);
1705*11878SVenu.Iyer@Sun.COM 			if (mip->mi_rx_group_type == MAC_GROUP_TYPE_DYNAMIC)
1706*11878SVenu.Iyer@Sun.COM 				MAC_RX_RING_RESERVED(mip, ngrp->mrg_cur_count);
1707*11878SVenu.Iyer@Sun.COM 		} else if (group != defgrp && !unspec &&
1708*11878SVenu.Iyer@Sun.COM 		    mrp->mrp_nrxrings == 0) {
1709*11878SVenu.Iyer@Sun.COM 			/* Switch to S/W */
1710*11878SVenu.Iyer@Sun.COM 			ringcnt = group->mrg_cur_count;
1711*11878SVenu.Iyer@Sun.COM 			if (mac_rx_switch_group(mcip, group, defgrp) != 0)
1712*11878SVenu.Iyer@Sun.COM 				return (ENOSPC);
1713*11878SVenu.Iyer@Sun.COM 			if (tmrp->mrp_mask & MRP_RX_RINGS) {
1714*11878SVenu.Iyer@Sun.COM 				MAC_RX_GRP_RELEASED(mip);
1715*11878SVenu.Iyer@Sun.COM 				if (mip->mi_rx_group_type ==
1716*11878SVenu.Iyer@Sun.COM 				    MAC_GROUP_TYPE_DYNAMIC) {
1717*11878SVenu.Iyer@Sun.COM 					MAC_RX_RING_RELEASED(mip, ringcnt);
1718*11878SVenu.Iyer@Sun.COM 				}
1719*11878SVenu.Iyer@Sun.COM 			}
1720*11878SVenu.Iyer@Sun.COM 		} else if (group != defgrp && mip->mi_rx_group_type ==
1721*11878SVenu.Iyer@Sun.COM 		    MAC_GROUP_TYPE_DYNAMIC) {
1722*11878SVenu.Iyer@Sun.COM 			ringcnt = group->mrg_cur_count;
1723*11878SVenu.Iyer@Sun.COM 			err = mac_group_ring_modify(mcip, group, defgrp);
1724*11878SVenu.Iyer@Sun.COM 			if (err != 0)
1725*11878SVenu.Iyer@Sun.COM 				return (err);
1726*11878SVenu.Iyer@Sun.COM 			/*
1727*11878SVenu.Iyer@Sun.COM 			 * Update the accounting. If this group
1728*11878SVenu.Iyer@Sun.COM 			 * already had explicitly reserved rings,
1729*11878SVenu.Iyer@Sun.COM 			 * we need to update the rings based on
1730*11878SVenu.Iyer@Sun.COM 			 * the new ring count. If this group
1731*11878SVenu.Iyer@Sun.COM 			 * had not explicitly reserved rings,
1732*11878SVenu.Iyer@Sun.COM 			 * then we just reserve the rings asked for
1733*11878SVenu.Iyer@Sun.COM 			 * and reserve the group.
1734*11878SVenu.Iyer@Sun.COM 			 */
1735*11878SVenu.Iyer@Sun.COM 			if (tmrp->mrp_mask & MRP_RX_RINGS) {
1736*11878SVenu.Iyer@Sun.COM 				if (ringcnt > group->mrg_cur_count) {
1737*11878SVenu.Iyer@Sun.COM 					MAC_RX_RING_RELEASED(mip,
1738*11878SVenu.Iyer@Sun.COM 					    ringcnt - group->mrg_cur_count);
1739*11878SVenu.Iyer@Sun.COM 				} else {
1740*11878SVenu.Iyer@Sun.COM 					MAC_RX_RING_RESERVED(mip,
1741*11878SVenu.Iyer@Sun.COM 					    group->mrg_cur_count - ringcnt);
1742*11878SVenu.Iyer@Sun.COM 				}
1743*11878SVenu.Iyer@Sun.COM 			} else {
1744*11878SVenu.Iyer@Sun.COM 				MAC_RX_RING_RESERVED(mip, group->mrg_cur_count);
1745*11878SVenu.Iyer@Sun.COM 				MAC_RX_GRP_RESERVED(mip);
1746*11878SVenu.Iyer@Sun.COM 			}
1747*11878SVenu.Iyer@Sun.COM 		}
1748*11878SVenu.Iyer@Sun.COM 	}
1749*11878SVenu.Iyer@Sun.COM 	if (mrp->mrp_mask & MRP_TX_RINGS) {
1750*11878SVenu.Iyer@Sun.COM 		unspec = mrp->mrp_mask & MRP_TXRINGS_UNSPEC;
1751*11878SVenu.Iyer@Sun.COM 		group = flent->fe_tx_ring_group;
1752*11878SVenu.Iyer@Sun.COM 		defgrp = MAC_DEFAULT_TX_GROUP(mip);
1753*11878SVenu.Iyer@Sun.COM 
1754*11878SVenu.Iyer@Sun.COM 		/*
1755*11878SVenu.Iyer@Sun.COM 		 * For static groups we only allow rings=0 or resetting the
1756*11878SVenu.Iyer@Sun.COM 		 * rings property.
1757*11878SVenu.Iyer@Sun.COM 		 */
1758*11878SVenu.Iyer@Sun.COM 		if (mrp->mrp_ntxrings > 0 &&
1759*11878SVenu.Iyer@Sun.COM 		    mip->mi_tx_group_type != MAC_GROUP_TYPE_DYNAMIC) {
1760*11878SVenu.Iyer@Sun.COM 			return (ENOTSUP);
1761*11878SVenu.Iyer@Sun.COM 		}
1762*11878SVenu.Iyer@Sun.COM 		if (mrp->mrp_mask & MRP_RINGS_RESET) {
1763*11878SVenu.Iyer@Sun.COM 			if (!(tmrp->mrp_mask & MRP_TX_RINGS))
1764*11878SVenu.Iyer@Sun.COM 				return (0);
1765*11878SVenu.Iyer@Sun.COM 		} else {
1766*11878SVenu.Iyer@Sun.COM 			if (unspec) {
1767*11878SVenu.Iyer@Sun.COM 				if (tmrp->mrp_mask & MRP_TXRINGS_UNSPEC)
1768*11878SVenu.Iyer@Sun.COM 					return (0);
1769*11878SVenu.Iyer@Sun.COM 			} else if (mip->mi_tx_group_type ==
1770*11878SVenu.Iyer@Sun.COM 			    MAC_GROUP_TYPE_DYNAMIC) {
1771*11878SVenu.Iyer@Sun.COM 				if ((tmrp->mrp_mask & MRP_TX_RINGS) &&
1772*11878SVenu.Iyer@Sun.COM 				    !(tmrp->mrp_mask & MRP_TXRINGS_UNSPEC) &&
1773*11878SVenu.Iyer@Sun.COM 				    mrp->mrp_ntxrings == tmrp->mrp_ntxrings) {
1774*11878SVenu.Iyer@Sun.COM 					return (0);
1775*11878SVenu.Iyer@Sun.COM 				}
1776*11878SVenu.Iyer@Sun.COM 			}
1777*11878SVenu.Iyer@Sun.COM 		}
1778*11878SVenu.Iyer@Sun.COM 		/* Resetting the prop */
1779*11878SVenu.Iyer@Sun.COM 		if (mrp->mrp_mask & MRP_RINGS_RESET) {
1780*11878SVenu.Iyer@Sun.COM 			if (group != defgrp) {
1781*11878SVenu.Iyer@Sun.COM 				if (mip->mi_tx_group_type ==
1782*11878SVenu.Iyer@Sun.COM 				    MAC_GROUP_TYPE_DYNAMIC) {
1783*11878SVenu.Iyer@Sun.COM 					ringcnt = group->mrg_cur_count;
1784*11878SVenu.Iyer@Sun.COM 					if ((flent->fe_type &
1785*11878SVenu.Iyer@Sun.COM 					    FLOW_PRIMARY_MAC) != 0) {
1786*11878SVenu.Iyer@Sun.COM 						mac_tx_client_quiesce(
1787*11878SVenu.Iyer@Sun.COM 						    (mac_client_handle_t)
1788*11878SVenu.Iyer@Sun.COM 						    mcip);
1789*11878SVenu.Iyer@Sun.COM 						mac_tx_switch_group(mcip,
1790*11878SVenu.Iyer@Sun.COM 						    group, defgrp);
1791*11878SVenu.Iyer@Sun.COM 						mac_tx_client_restart(
1792*11878SVenu.Iyer@Sun.COM 						    (mac_client_handle_t)
1793*11878SVenu.Iyer@Sun.COM 						    mcip);
1794*11878SVenu.Iyer@Sun.COM 						MAC_TX_GRP_RELEASED(mip);
1795*11878SVenu.Iyer@Sun.COM 						MAC_TX_RING_RELEASED(mip,
1796*11878SVenu.Iyer@Sun.COM 						    ringcnt);
1797*11878SVenu.Iyer@Sun.COM 						return (0);
1798*11878SVenu.Iyer@Sun.COM 					}
1799*11878SVenu.Iyer@Sun.COM 					cmrp->mrp_ntxrings = 1;
1800*11878SVenu.Iyer@Sun.COM 					(void) mac_group_ring_modify(mcip,
1801*11878SVenu.Iyer@Sun.COM 					    group, defgrp);
1802*11878SVenu.Iyer@Sun.COM 					/*
1803*11878SVenu.Iyer@Sun.COM 					 * This group has reserved rings
1804*11878SVenu.Iyer@Sun.COM 					 * that need to be released now.
1805*11878SVenu.Iyer@Sun.COM 					 */
1806*11878SVenu.Iyer@Sun.COM 					MAC_TX_RING_RELEASED(mip, ringcnt);
1807*11878SVenu.Iyer@Sun.COM 				}
1808*11878SVenu.Iyer@Sun.COM 				/*
1809*11878SVenu.Iyer@Sun.COM 				 * If this is a static group, we
1810*11878SVenu.Iyer@Sun.COM 				 * need to release the group. The
1811*11878SVenu.Iyer@Sun.COM 				 * client will remain in the same
1812*11878SVenu.Iyer@Sun.COM 				 * group till some other client
1813*11878SVenu.Iyer@Sun.COM 				 * needs this group.
1814*11878SVenu.Iyer@Sun.COM 				 */
1815*11878SVenu.Iyer@Sun.COM 				MAC_TX_GRP_RELEASED(mip);
1816*11878SVenu.Iyer@Sun.COM 			} else if (group == defgrp &&
1817*11878SVenu.Iyer@Sun.COM 			    (flent->fe_type & FLOW_PRIMARY_MAC) == 0) {
1818*11878SVenu.Iyer@Sun.COM 				ngrp = mac_reserve_tx_group(mcip, B_TRUE);
1819*11878SVenu.Iyer@Sun.COM 				if (ngrp == NULL)
1820*11878SVenu.Iyer@Sun.COM 					return (0);
1821*11878SVenu.Iyer@Sun.COM 				mac_tx_client_quiesce(
1822*11878SVenu.Iyer@Sun.COM 				    (mac_client_handle_t)mcip);
1823*11878SVenu.Iyer@Sun.COM 				mac_tx_switch_group(mcip, defgrp, ngrp);
1824*11878SVenu.Iyer@Sun.COM 				mac_tx_client_restart(
1825*11878SVenu.Iyer@Sun.COM 				    (mac_client_handle_t)mcip);
1826*11878SVenu.Iyer@Sun.COM 			}
1827*11878SVenu.Iyer@Sun.COM 			/*
1828*11878SVenu.Iyer@Sun.COM 			 * If the client is in the default group, we will
1829*11878SVenu.Iyer@Sun.COM 			 * just clear the MRP_TX_RINGS and leave it as
1830*11878SVenu.Iyer@Sun.COM 			 * it rather than look for an exclusive group
1831*11878SVenu.Iyer@Sun.COM 			 * for it.
1832*11878SVenu.Iyer@Sun.COM 			 */
1833*11878SVenu.Iyer@Sun.COM 			return (0);
1834*11878SVenu.Iyer@Sun.COM 		}
1835*11878SVenu.Iyer@Sun.COM 
1836*11878SVenu.Iyer@Sun.COM 		/* Switch to H/W */
1837*11878SVenu.Iyer@Sun.COM 		if (group == defgrp && ((mrp->mrp_ntxrings > 0) || unspec)) {
1838*11878SVenu.Iyer@Sun.COM 			ngrp = 	mac_reserve_tx_group(mcip, B_TRUE);
1839*11878SVenu.Iyer@Sun.COM 			if (ngrp == NULL)
1840*11878SVenu.Iyer@Sun.COM 				return (ENOSPC);
1841*11878SVenu.Iyer@Sun.COM 			mac_tx_client_quiesce((mac_client_handle_t)mcip);
1842*11878SVenu.Iyer@Sun.COM 			mac_tx_switch_group(mcip, defgrp, ngrp);
1843*11878SVenu.Iyer@Sun.COM 			mac_tx_client_restart((mac_client_handle_t)mcip);
1844*11878SVenu.Iyer@Sun.COM 			MAC_TX_GRP_RESERVED(mip);
1845*11878SVenu.Iyer@Sun.COM 			if (mip->mi_tx_group_type == MAC_GROUP_TYPE_DYNAMIC)
1846*11878SVenu.Iyer@Sun.COM 				MAC_TX_RING_RESERVED(mip, ngrp->mrg_cur_count);
1847*11878SVenu.Iyer@Sun.COM 		/* Switch to S/W */
1848*11878SVenu.Iyer@Sun.COM 		} else if (group != defgrp && !unspec &&
1849*11878SVenu.Iyer@Sun.COM 		    mrp->mrp_ntxrings == 0) {
1850*11878SVenu.Iyer@Sun.COM 			/* Switch to S/W */
1851*11878SVenu.Iyer@Sun.COM 			ringcnt = group->mrg_cur_count;
1852*11878SVenu.Iyer@Sun.COM 			mac_tx_client_quiesce((mac_client_handle_t)mcip);
1853*11878SVenu.Iyer@Sun.COM 			mac_tx_switch_group(mcip, group, defgrp);
1854*11878SVenu.Iyer@Sun.COM 			mac_tx_client_restart((mac_client_handle_t)mcip);
1855*11878SVenu.Iyer@Sun.COM 			if (tmrp->mrp_mask & MRP_TX_RINGS) {
1856*11878SVenu.Iyer@Sun.COM 				MAC_TX_GRP_RELEASED(mip);
1857*11878SVenu.Iyer@Sun.COM 				if (mip->mi_tx_group_type ==
1858*11878SVenu.Iyer@Sun.COM 				    MAC_GROUP_TYPE_DYNAMIC) {
1859*11878SVenu.Iyer@Sun.COM 					MAC_TX_RING_RELEASED(mip, ringcnt);
1860*11878SVenu.Iyer@Sun.COM 				}
1861*11878SVenu.Iyer@Sun.COM 			}
1862*11878SVenu.Iyer@Sun.COM 		} else if (group != defgrp && mip->mi_tx_group_type ==
1863*11878SVenu.Iyer@Sun.COM 		    MAC_GROUP_TYPE_DYNAMIC) {
1864*11878SVenu.Iyer@Sun.COM 			ringcnt = group->mrg_cur_count;
1865*11878SVenu.Iyer@Sun.COM 			err = mac_group_ring_modify(mcip, group, defgrp);
1866*11878SVenu.Iyer@Sun.COM 			if (err != 0)
1867*11878SVenu.Iyer@Sun.COM 				return (err);
1868*11878SVenu.Iyer@Sun.COM 			/*
1869*11878SVenu.Iyer@Sun.COM 			 * Update the accounting. If this group
1870*11878SVenu.Iyer@Sun.COM 			 * already had explicitly reserved rings,
1871*11878SVenu.Iyer@Sun.COM 			 * we need to update the rings based on
1872*11878SVenu.Iyer@Sun.COM 			 * the new ring count. If this group
1873*11878SVenu.Iyer@Sun.COM 			 * had not explicitly reserved rings,
1874*11878SVenu.Iyer@Sun.COM 			 * then we just reserve the rings asked for
1875*11878SVenu.Iyer@Sun.COM 			 * and reserve the group.
1876*11878SVenu.Iyer@Sun.COM 			 */
1877*11878SVenu.Iyer@Sun.COM 			if (tmrp->mrp_mask & MRP_TX_RINGS) {
1878*11878SVenu.Iyer@Sun.COM 				if (ringcnt > group->mrg_cur_count) {
1879*11878SVenu.Iyer@Sun.COM 					MAC_TX_RING_RELEASED(mip,
1880*11878SVenu.Iyer@Sun.COM 					    ringcnt - group->mrg_cur_count);
1881*11878SVenu.Iyer@Sun.COM 				} else {
1882*11878SVenu.Iyer@Sun.COM 					MAC_TX_RING_RESERVED(mip,
1883*11878SVenu.Iyer@Sun.COM 					    group->mrg_cur_count - ringcnt);
1884*11878SVenu.Iyer@Sun.COM 				}
1885*11878SVenu.Iyer@Sun.COM 			} else {
1886*11878SVenu.Iyer@Sun.COM 				MAC_TX_RING_RESERVED(mip, group->mrg_cur_count);
1887*11878SVenu.Iyer@Sun.COM 				MAC_TX_GRP_RESERVED(mip);
1888*11878SVenu.Iyer@Sun.COM 			}
1889*11878SVenu.Iyer@Sun.COM 		}
1890*11878SVenu.Iyer@Sun.COM 	}
1891*11878SVenu.Iyer@Sun.COM 	return (0);
1892*11878SVenu.Iyer@Sun.COM }
1893*11878SVenu.Iyer@Sun.COM 
1894*11878SVenu.Iyer@Sun.COM /*
18958275SEric Cheng  * When the MAC client is being brought up (i.e. we do a unicast_add) we need
18968275SEric Cheng  * to initialize the cpu and resource control structure in the
18978275SEric Cheng  * mac_client_impl_t from the mac_impl_t (i.e if there are any cached
18988275SEric Cheng  * properties before the flow entry for the unicast address was created).
18998275SEric Cheng  */
19008275SEric Cheng int
19018275SEric Cheng mac_resource_ctl_set(mac_client_handle_t mch, mac_resource_props_t *mrp)
19028275SEric Cheng {
19038275SEric Cheng 	mac_client_impl_t 	*mcip = (mac_client_impl_t *)mch;
19048275SEric Cheng 	mac_impl_t		*mip = (mac_impl_t *)mcip->mci_mip;
19058275SEric Cheng 	int			err = 0;
1906*11878SVenu.Iyer@Sun.COM 	flow_entry_t		*flent = mcip->mci_flent;
1907*11878SVenu.Iyer@Sun.COM 	mac_resource_props_t	*omrp, *nmrp = MCIP_RESOURCE_PROPS(mcip);
19088275SEric Cheng 
19098275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
19108275SEric Cheng 
1911*11878SVenu.Iyer@Sun.COM 	err = mac_validate_props(mcip->mci_state_flags & MCIS_IS_VNIC ?
1912*11878SVenu.Iyer@Sun.COM 	    mcip->mci_upper_mip : mip, mrp);
19138275SEric Cheng 	if (err != 0)
19148275SEric Cheng 		return (err);
19158275SEric Cheng 
1916*11878SVenu.Iyer@Sun.COM 	/*
1917*11878SVenu.Iyer@Sun.COM 	 * Copy over the existing properties since mac_update_resources
1918*11878SVenu.Iyer@Sun.COM 	 * will modify the client's mrp. Currently, the saved property
1919*11878SVenu.Iyer@Sun.COM 	 * is used to determine the difference between existing and
1920*11878SVenu.Iyer@Sun.COM 	 * modified rings property.
1921*11878SVenu.Iyer@Sun.COM 	 */
1922*11878SVenu.Iyer@Sun.COM 	omrp = kmem_zalloc(sizeof (*omrp), KM_SLEEP);
1923*11878SVenu.Iyer@Sun.COM 	bcopy(nmrp, omrp, sizeof (*omrp));
19248275SEric Cheng 	mac_update_resources(mrp, MCIP_RESOURCE_PROPS(mcip), B_FALSE);
19258275SEric Cheng 	if (MCIP_DATAPATH_SETUP(mcip)) {
19268275SEric Cheng 		/*
1927*11878SVenu.Iyer@Sun.COM 		 * We support rings only for primary client when there are
1928*11878SVenu.Iyer@Sun.COM 		 * multiple clients sharing the same MAC address (e.g. VLAN).
1929*11878SVenu.Iyer@Sun.COM 		 */
1930*11878SVenu.Iyer@Sun.COM 		if (mrp->mrp_mask & MRP_RX_RINGS ||
1931*11878SVenu.Iyer@Sun.COM 		    mrp->mrp_mask & MRP_TX_RINGS) {
1932*11878SVenu.Iyer@Sun.COM 
1933*11878SVenu.Iyer@Sun.COM 			if ((err = mac_client_set_rings_prop(mcip, mrp,
1934*11878SVenu.Iyer@Sun.COM 			    omrp)) != 0) {
1935*11878SVenu.Iyer@Sun.COM 				if (omrp->mrp_mask & MRP_RX_RINGS) {
1936*11878SVenu.Iyer@Sun.COM 					nmrp->mrp_mask |= MRP_RX_RINGS;
1937*11878SVenu.Iyer@Sun.COM 					nmrp->mrp_nrxrings = omrp->mrp_nrxrings;
1938*11878SVenu.Iyer@Sun.COM 				} else {
1939*11878SVenu.Iyer@Sun.COM 					nmrp->mrp_mask &= ~MRP_RX_RINGS;
1940*11878SVenu.Iyer@Sun.COM 					nmrp->mrp_nrxrings = 0;
1941*11878SVenu.Iyer@Sun.COM 				}
1942*11878SVenu.Iyer@Sun.COM 				if (omrp->mrp_mask & MRP_TX_RINGS) {
1943*11878SVenu.Iyer@Sun.COM 					nmrp->mrp_mask |= MRP_TX_RINGS;
1944*11878SVenu.Iyer@Sun.COM 					nmrp->mrp_ntxrings = omrp->mrp_ntxrings;
1945*11878SVenu.Iyer@Sun.COM 				} else {
1946*11878SVenu.Iyer@Sun.COM 					nmrp->mrp_mask &= ~MRP_TX_RINGS;
1947*11878SVenu.Iyer@Sun.COM 					nmrp->mrp_ntxrings = 0;
1948*11878SVenu.Iyer@Sun.COM 				}
1949*11878SVenu.Iyer@Sun.COM 				if (omrp->mrp_mask & MRP_RXRINGS_UNSPEC)
1950*11878SVenu.Iyer@Sun.COM 					omrp->mrp_mask |= MRP_RXRINGS_UNSPEC;
1951*11878SVenu.Iyer@Sun.COM 				else
1952*11878SVenu.Iyer@Sun.COM 					omrp->mrp_mask &= ~MRP_RXRINGS_UNSPEC;
1953*11878SVenu.Iyer@Sun.COM 
1954*11878SVenu.Iyer@Sun.COM 				if (omrp->mrp_mask & MRP_TXRINGS_UNSPEC)
1955*11878SVenu.Iyer@Sun.COM 					omrp->mrp_mask |= MRP_TXRINGS_UNSPEC;
1956*11878SVenu.Iyer@Sun.COM 				else
1957*11878SVenu.Iyer@Sun.COM 					omrp->mrp_mask &= ~MRP_TXRINGS_UNSPEC;
1958*11878SVenu.Iyer@Sun.COM 				kmem_free(omrp, sizeof (*omrp));
1959*11878SVenu.Iyer@Sun.COM 				return (err);
1960*11878SVenu.Iyer@Sun.COM 			}
1961*11878SVenu.Iyer@Sun.COM 
1962*11878SVenu.Iyer@Sun.COM 			/*
1963*11878SVenu.Iyer@Sun.COM 			 * If we modified the rings property of the primary
1964*11878SVenu.Iyer@Sun.COM 			 * we need to update the property fields of its
1965*11878SVenu.Iyer@Sun.COM 			 * VLANs as they inherit the primary's properites.
1966*11878SVenu.Iyer@Sun.COM 			 */
1967*11878SVenu.Iyer@Sun.COM 			if (mac_is_primary_client(mcip)) {
1968*11878SVenu.Iyer@Sun.COM 				mac_set_prim_vlan_rings(mip,
1969*11878SVenu.Iyer@Sun.COM 				    MCIP_RESOURCE_PROPS(mcip));
1970*11878SVenu.Iyer@Sun.COM 			}
1971*11878SVenu.Iyer@Sun.COM 		}
1972*11878SVenu.Iyer@Sun.COM 		/*
19738275SEric Cheng 		 * We have to set this prior to calling mac_flow_modify.
19748275SEric Cheng 		 */
19758275SEric Cheng 		if (mrp->mrp_mask & MRP_PRIORITY) {
19768275SEric Cheng 			if (mrp->mrp_priority == MPL_RESET) {
19778275SEric Cheng 				MAC_CLIENT_SET_PRIORITY_RANGE(mcip,
19788275SEric Cheng 				    MPL_LINK_DEFAULT);
19798275SEric Cheng 			} else {
19808275SEric Cheng 				MAC_CLIENT_SET_PRIORITY_RANGE(mcip,
19818275SEric Cheng 				    mrp->mrp_priority);
19828275SEric Cheng 			}
19838275SEric Cheng 		}
19848275SEric Cheng 
1985*11878SVenu.Iyer@Sun.COM 		mac_flow_modify(mip->mi_flow_tab, flent, mrp);
19868275SEric Cheng 		if (mrp->mrp_mask & MRP_PRIORITY)
19878275SEric Cheng 			mac_update_subflow_priority(mcip);
19888275SEric Cheng 	}
1989*11878SVenu.Iyer@Sun.COM 	kmem_free(omrp, sizeof (*omrp));
19908275SEric Cheng 	return (0);
19918275SEric Cheng }
19928275SEric Cheng 
19938275SEric Cheng void
19948275SEric Cheng mac_resource_ctl_get(mac_client_handle_t mch, mac_resource_props_t *mrp)
19958275SEric Cheng {
19968275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
19978275SEric Cheng 	mac_resource_props_t	*mcip_mrp = MCIP_RESOURCE_PROPS(mcip);
19988275SEric Cheng 
19998275SEric Cheng 	bcopy(mcip_mrp, mrp, sizeof (mac_resource_props_t));
20008275SEric Cheng }
20018275SEric Cheng 
20028275SEric Cheng static int
20038275SEric Cheng mac_unicast_flow_create(mac_client_impl_t *mcip, uint8_t *mac_addr,
20048275SEric Cheng     uint16_t vid, boolean_t is_primary, boolean_t first_flow,
20058275SEric Cheng     flow_entry_t **flent, mac_resource_props_t *mrp)
20068275SEric Cheng {
20078275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mcip->mci_mip;
20088275SEric Cheng 	flow_desc_t	flow_desc;
20098558SGirish.Moodalbail@Sun.COM 	char		flowname[MAXFLOWNAMELEN];
20108275SEric Cheng 	int		err;
20118275SEric Cheng 	uint_t		flent_flags;
20128275SEric Cheng 
20138275SEric Cheng 	/*
20148275SEric Cheng 	 * First unicast address being added, create a new flow
20158275SEric Cheng 	 * for that MAC client.
20168275SEric Cheng 	 */
20178275SEric Cheng 	bzero(&flow_desc, sizeof (flow_desc));
20188275SEric Cheng 
2019*11878SVenu.Iyer@Sun.COM 	ASSERT(mac_addr != NULL ||
2020*11878SVenu.Iyer@Sun.COM 	    (mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR));
2021*11878SVenu.Iyer@Sun.COM 	if (mac_addr != NULL) {
2022*11878SVenu.Iyer@Sun.COM 		flow_desc.fd_mac_len = mip->mi_type->mt_addr_length;
2023*11878SVenu.Iyer@Sun.COM 		bcopy(mac_addr, flow_desc.fd_dst_mac, flow_desc.fd_mac_len);
2024*11878SVenu.Iyer@Sun.COM 	}
20258275SEric Cheng 	flow_desc.fd_mask = FLOW_LINK_DST;
20268275SEric Cheng 	if (vid != 0) {
20278275SEric Cheng 		flow_desc.fd_vid = vid;
20288275SEric Cheng 		flow_desc.fd_mask |= FLOW_LINK_VID;
20298275SEric Cheng 	}
20308275SEric Cheng 
20318275SEric Cheng 	/*
20328275SEric Cheng 	 * XXX-nicolas. For now I'm keeping the FLOW_PRIMARY_MAC
20338275SEric Cheng 	 * and FLOW_VNIC. Even though they're a hack inherited
20348275SEric Cheng 	 * from the SRS code, we'll keep them for now. They're currently
20358275SEric Cheng 	 * consumed by mac_datapath_setup() to create the SRS.
20368275SEric Cheng 	 * That code should be eventually moved out of
20378275SEric Cheng 	 * mac_datapath_setup() and moved to a mac_srs_create()
20388275SEric Cheng 	 * function of some sort to keep things clean.
20398275SEric Cheng 	 *
20408275SEric Cheng 	 * Also, there's no reason why the SRS for the primary MAC
20418275SEric Cheng 	 * client should be different than any other MAC client. Until
20428275SEric Cheng 	 * this is cleaned-up, we support only one MAC unicast address
20438275SEric Cheng 	 * per client.
20448275SEric Cheng 	 *
20458275SEric Cheng 	 * We set FLOW_PRIMARY_MAC for the primary MAC address,
20468275SEric Cheng 	 * FLOW_VNIC for everything else.
20478275SEric Cheng 	 */
20488275SEric Cheng 	if (is_primary)
20498275SEric Cheng 		flent_flags = FLOW_PRIMARY_MAC;
20508275SEric Cheng 	else
20518275SEric Cheng 		flent_flags = FLOW_VNIC_MAC;
20528275SEric Cheng 
20538275SEric Cheng 	/*
20548275SEric Cheng 	 * For the first flow we use the mac client's name - mci_name, for
20558275SEric Cheng 	 * subsequent ones we just create a name with the vid. This is
20568275SEric Cheng 	 * so that we can add these flows to the same flow table. This is
20578275SEric Cheng 	 * fine as the flow name (except for the one with the mac client's
20588275SEric Cheng 	 * name) is not visible. When the first flow is removed, we just replace
20598275SEric Cheng 	 * its fdesc with another from the list, so we will still retain the
20608275SEric Cheng 	 * flent with the MAC client's flow name.
20618275SEric Cheng 	 */
20628275SEric Cheng 	if (first_flow) {
20638558SGirish.Moodalbail@Sun.COM 		bcopy(mcip->mci_name, flowname, MAXFLOWNAMELEN);
20648275SEric Cheng 	} else {
20658275SEric Cheng 		(void) sprintf(flowname, "%s%u", mcip->mci_name, vid);
20668275SEric Cheng 		flent_flags = FLOW_NO_STATS;
20678275SEric Cheng 	}
20688275SEric Cheng 
20698275SEric Cheng 	if ((err = mac_flow_create(&flow_desc, mrp, flowname, NULL,
20708275SEric Cheng 	    flent_flags, flent)) != 0)
20718275SEric Cheng 		return (err);
20728275SEric Cheng 
2073*11878SVenu.Iyer@Sun.COM 	mac_misc_stat_create(*flent);
20748275SEric Cheng 	FLOW_MARK(*flent, FE_INCIPIENT);
20758275SEric Cheng 	(*flent)->fe_mcip = mcip;
20768275SEric Cheng 
20778275SEric Cheng 	/*
20788275SEric Cheng 	 * Place initial creation reference on the flow. This reference
20798275SEric Cheng 	 * is released in the corresponding delete action viz.
20808275SEric Cheng 	 * mac_unicast_remove after waiting for all transient refs to
20818275SEric Cheng 	 * to go away. The wait happens in mac_flow_wait.
20828275SEric Cheng 	 * We have already held the reference in mac_client_open().
20838275SEric Cheng 	 */
20848275SEric Cheng 	if (!first_flow)
20858275SEric Cheng 		FLOW_REFHOLD(*flent);
20868275SEric Cheng 	return (0);
20878275SEric Cheng }
20888275SEric Cheng 
20898275SEric Cheng /* Refresh the multicast grouping for this VID. */
20908275SEric Cheng int
20918275SEric Cheng mac_client_update_mcast(void *arg, boolean_t add, const uint8_t *addrp)
20928275SEric Cheng {
20938275SEric Cheng 	flow_entry_t		*flent = arg;
20948275SEric Cheng 	mac_client_impl_t	*mcip = flent->fe_mcip;
20958275SEric Cheng 	uint16_t		vid;
20968275SEric Cheng 	flow_desc_t		flow_desc;
20978275SEric Cheng 
20988275SEric Cheng 	mac_flow_get_desc(flent, &flow_desc);
20998275SEric Cheng 	vid = (flow_desc.fd_mask & FLOW_LINK_VID) != 0 ?
21008275SEric Cheng 	    flow_desc.fd_vid : VLAN_ID_NONE;
21018275SEric Cheng 
21028275SEric Cheng 	/*
21038275SEric Cheng 	 * We don't call mac_multicast_add()/mac_multicast_remove() as
21048275SEric Cheng 	 * we want to add/remove for this specific vid.
21058275SEric Cheng 	 */
21068275SEric Cheng 	if (add) {
21078275SEric Cheng 		return (mac_bcast_add(mcip, addrp, vid,
21088275SEric Cheng 		    MAC_ADDRTYPE_MULTICAST));
21098275SEric Cheng 	} else {
21108275SEric Cheng 		mac_bcast_delete(mcip, addrp, vid);
21118275SEric Cheng 		return (0);
21128275SEric Cheng 	}
21138275SEric Cheng }
21148275SEric Cheng 
21158833SVenu.Iyer@Sun.COM static void
21168833SVenu.Iyer@Sun.COM mac_update_single_active_client(mac_impl_t *mip)
21178833SVenu.Iyer@Sun.COM {
21188833SVenu.Iyer@Sun.COM 	mac_client_impl_t *client = NULL;
21198833SVenu.Iyer@Sun.COM 
21208833SVenu.Iyer@Sun.COM 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
21218833SVenu.Iyer@Sun.COM 
21228833SVenu.Iyer@Sun.COM 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
21238833SVenu.Iyer@Sun.COM 	if (mip->mi_nactiveclients == 1) {
21248833SVenu.Iyer@Sun.COM 		/*
21258833SVenu.Iyer@Sun.COM 		 * Find the one active MAC client from the list of MAC
21268833SVenu.Iyer@Sun.COM 		 * clients. The active MAC client has at least one
21278833SVenu.Iyer@Sun.COM 		 * unicast address.
21288833SVenu.Iyer@Sun.COM 		 */
21298833SVenu.Iyer@Sun.COM 		for (client = mip->mi_clients_list; client != NULL;
21308833SVenu.Iyer@Sun.COM 		    client = client->mci_client_next) {
21318833SVenu.Iyer@Sun.COM 			if (client->mci_unicast_list != NULL)
21328833SVenu.Iyer@Sun.COM 				break;
21338833SVenu.Iyer@Sun.COM 		}
21348833SVenu.Iyer@Sun.COM 		ASSERT(client != NULL);
21358833SVenu.Iyer@Sun.COM 	}
21368833SVenu.Iyer@Sun.COM 
21378833SVenu.Iyer@Sun.COM 	/*
21388833SVenu.Iyer@Sun.COM 	 * mi_single_active_client is protected by the MAC impl's read/writer
21398833SVenu.Iyer@Sun.COM 	 * lock, which allows mac_rx() to check the value of that pointer
21408833SVenu.Iyer@Sun.COM 	 * as a reader.
21418833SVenu.Iyer@Sun.COM 	 */
21428833SVenu.Iyer@Sun.COM 	mip->mi_single_active_client = client;
21438833SVenu.Iyer@Sun.COM 	rw_exit(&mip->mi_rw_lock);
21448833SVenu.Iyer@Sun.COM }
21458833SVenu.Iyer@Sun.COM 
21468275SEric Cheng /*
21479473SVenu.Iyer@Sun.COM  * Set up the data path. Called from i_mac_unicast_add after having
21489473SVenu.Iyer@Sun.COM  * done all the validations including making sure this is an active
21499473SVenu.Iyer@Sun.COM  * client (i.e that is ready to process packets.)
21509473SVenu.Iyer@Sun.COM  */
21519473SVenu.Iyer@Sun.COM static int
21529473SVenu.Iyer@Sun.COM mac_client_datapath_setup(mac_client_impl_t *mcip, uint16_t vid,
21539473SVenu.Iyer@Sun.COM     uint8_t *mac_addr, mac_resource_props_t *mrp, boolean_t isprimary,
21549473SVenu.Iyer@Sun.COM     mac_unicast_impl_t *muip)
21559473SVenu.Iyer@Sun.COM {
21569473SVenu.Iyer@Sun.COM 	mac_impl_t	*mip = mcip->mci_mip;
21579473SVenu.Iyer@Sun.COM 	boolean_t	mac_started = B_FALSE;
21589473SVenu.Iyer@Sun.COM 	boolean_t	bcast_added = B_FALSE;
21599473SVenu.Iyer@Sun.COM 	boolean_t	nactiveclients_added = B_FALSE;
21609473SVenu.Iyer@Sun.COM 	flow_entry_t	*flent;
21619473SVenu.Iyer@Sun.COM 	int		err = 0;
2162*11878SVenu.Iyer@Sun.COM 	boolean_t	no_unicast;
2163*11878SVenu.Iyer@Sun.COM 
2164*11878SVenu.Iyer@Sun.COM 	no_unicast = mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR;
21659473SVenu.Iyer@Sun.COM 
21669473SVenu.Iyer@Sun.COM 	if ((err = mac_start((mac_handle_t)mip)) != 0)
21679473SVenu.Iyer@Sun.COM 		goto bail;
21689473SVenu.Iyer@Sun.COM 
21699473SVenu.Iyer@Sun.COM 	mac_started = B_TRUE;
21709473SVenu.Iyer@Sun.COM 
21719473SVenu.Iyer@Sun.COM 	/* add the MAC client to the broadcast address group by default */
21729473SVenu.Iyer@Sun.COM 	if (mip->mi_type->mt_brdcst_addr != NULL) {
21739473SVenu.Iyer@Sun.COM 		err = mac_bcast_add(mcip, mip->mi_type->mt_brdcst_addr, vid,
21749473SVenu.Iyer@Sun.COM 		    MAC_ADDRTYPE_BROADCAST);
21759473SVenu.Iyer@Sun.COM 		if (err != 0)
21769473SVenu.Iyer@Sun.COM 			goto bail;
21779473SVenu.Iyer@Sun.COM 		bcast_added = B_TRUE;
21789473SVenu.Iyer@Sun.COM 	}
21799473SVenu.Iyer@Sun.COM 
21809473SVenu.Iyer@Sun.COM 	/*
21819473SVenu.Iyer@Sun.COM 	 * If this is the first unicast address addition for this
21829473SVenu.Iyer@Sun.COM 	 * client, reuse the pre-allocated larval flow entry associated with
21839473SVenu.Iyer@Sun.COM 	 * the MAC client.
21849473SVenu.Iyer@Sun.COM 	 */
21859473SVenu.Iyer@Sun.COM 	flent = (mcip->mci_nflents == 0) ? mcip->mci_flent : NULL;
21869473SVenu.Iyer@Sun.COM 
21879473SVenu.Iyer@Sun.COM 	/* We are configuring the unicast flow now */
21889473SVenu.Iyer@Sun.COM 	if (!MCIP_DATAPATH_SETUP(mcip)) {
21899473SVenu.Iyer@Sun.COM 
2190*11878SVenu.Iyer@Sun.COM 		if (mrp != NULL) {
2191*11878SVenu.Iyer@Sun.COM 			MAC_CLIENT_SET_PRIORITY_RANGE(mcip,
2192*11878SVenu.Iyer@Sun.COM 			    (mrp->mrp_mask & MRP_PRIORITY) ? mrp->mrp_priority :
2193*11878SVenu.Iyer@Sun.COM 			    MPL_LINK_DEFAULT);
2194*11878SVenu.Iyer@Sun.COM 		}
21959473SVenu.Iyer@Sun.COM 		if ((err = mac_unicast_flow_create(mcip, mac_addr, vid,
21969473SVenu.Iyer@Sun.COM 		    isprimary, B_TRUE, &flent, mrp)) != 0)
21979473SVenu.Iyer@Sun.COM 			goto bail;
21989473SVenu.Iyer@Sun.COM 
21999473SVenu.Iyer@Sun.COM 		mip->mi_nactiveclients++;
22009473SVenu.Iyer@Sun.COM 		nactiveclients_added = B_TRUE;
22019473SVenu.Iyer@Sun.COM 
22029473SVenu.Iyer@Sun.COM 		/*
22039473SVenu.Iyer@Sun.COM 		 * This will allocate the RX ring group if possible for the
22049473SVenu.Iyer@Sun.COM 		 * flow and program the software classifier as needed.
22059473SVenu.Iyer@Sun.COM 		 */
22069473SVenu.Iyer@Sun.COM 		if ((err = mac_datapath_setup(mcip, flent, SRST_LINK)) != 0)
22079473SVenu.Iyer@Sun.COM 			goto bail;
22089473SVenu.Iyer@Sun.COM 
2209*11878SVenu.Iyer@Sun.COM 		if (no_unicast)
2210*11878SVenu.Iyer@Sun.COM 			goto done_setup;
22119473SVenu.Iyer@Sun.COM 		/*
22129473SVenu.Iyer@Sun.COM 		 * The unicast MAC address must have been added successfully.
22139473SVenu.Iyer@Sun.COM 		 */
22149473SVenu.Iyer@Sun.COM 		ASSERT(mcip->mci_unicast != NULL);
22159473SVenu.Iyer@Sun.COM 		/*
22169473SVenu.Iyer@Sun.COM 		 * Push down the sub-flows that were defined on this link
22179473SVenu.Iyer@Sun.COM 		 * hitherto. The flows are added to the active flow table
22189473SVenu.Iyer@Sun.COM 		 * and SRS, softrings etc. are created as needed.
22199473SVenu.Iyer@Sun.COM 		 */
22209473SVenu.Iyer@Sun.COM 		mac_link_init_flows((mac_client_handle_t)mcip);
22219473SVenu.Iyer@Sun.COM 	} else {
22229473SVenu.Iyer@Sun.COM 		mac_address_t *map = mcip->mci_unicast;
22239473SVenu.Iyer@Sun.COM 
2224*11878SVenu.Iyer@Sun.COM 		ASSERT(!no_unicast);
22259473SVenu.Iyer@Sun.COM 		/*
22269473SVenu.Iyer@Sun.COM 		 * A unicast flow already exists for that MAC client,
22279473SVenu.Iyer@Sun.COM 		 * this flow must be the same mac address but with
22289473SVenu.Iyer@Sun.COM 		 * different VID. It has been checked by mac_addr_in_use().
22299473SVenu.Iyer@Sun.COM 		 *
22309473SVenu.Iyer@Sun.COM 		 * We will use the SRS etc. from the mci_flent. Note that
22319473SVenu.Iyer@Sun.COM 		 * We don't need to create kstat for this as except for
22329473SVenu.Iyer@Sun.COM 		 * the fdesc, everything will be used from in the 1st flent.
22339473SVenu.Iyer@Sun.COM 		 */
22349473SVenu.Iyer@Sun.COM 
22359473SVenu.Iyer@Sun.COM 		if (bcmp(mac_addr, map->ma_addr, map->ma_len) != 0) {
22369473SVenu.Iyer@Sun.COM 			err = EINVAL;
22379473SVenu.Iyer@Sun.COM 			goto bail;
22389473SVenu.Iyer@Sun.COM 		}
22399473SVenu.Iyer@Sun.COM 
22409473SVenu.Iyer@Sun.COM 		if ((err = mac_unicast_flow_create(mcip, mac_addr, vid,
22419473SVenu.Iyer@Sun.COM 		    isprimary, B_FALSE, &flent, NULL)) != 0) {
22429473SVenu.Iyer@Sun.COM 			goto bail;
22439473SVenu.Iyer@Sun.COM 		}
22449473SVenu.Iyer@Sun.COM 		if ((err = mac_flow_add(mip->mi_flow_tab, flent)) != 0) {
22459473SVenu.Iyer@Sun.COM 			FLOW_FINAL_REFRELE(flent);
22469473SVenu.Iyer@Sun.COM 			goto bail;
22479473SVenu.Iyer@Sun.COM 		}
22489473SVenu.Iyer@Sun.COM 
22499473SVenu.Iyer@Sun.COM 		/* update the multicast group for this vid */
22509473SVenu.Iyer@Sun.COM 		mac_client_bcast_refresh(mcip, mac_client_update_mcast,
22519473SVenu.Iyer@Sun.COM 		    (void *)flent, B_TRUE);
22529473SVenu.Iyer@Sun.COM 
22539473SVenu.Iyer@Sun.COM 	}
22549473SVenu.Iyer@Sun.COM 
22559473SVenu.Iyer@Sun.COM 	/* populate the shared MAC address */
22569473SVenu.Iyer@Sun.COM 	muip->mui_map = mcip->mci_unicast;
22579473SVenu.Iyer@Sun.COM 
22589473SVenu.Iyer@Sun.COM 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
22599473SVenu.Iyer@Sun.COM 	muip->mui_next = mcip->mci_unicast_list;
22609473SVenu.Iyer@Sun.COM 	mcip->mci_unicast_list = muip;
22619473SVenu.Iyer@Sun.COM 	rw_exit(&mcip->mci_rw_lock);
22629473SVenu.Iyer@Sun.COM 
2263*11878SVenu.Iyer@Sun.COM done_setup:
22649473SVenu.Iyer@Sun.COM 	/*
22659473SVenu.Iyer@Sun.COM 	 * First add the flent to the flow list of this mcip. Then set
22669473SVenu.Iyer@Sun.COM 	 * the mip's mi_single_active_client if needed. The Rx path assumes
22679473SVenu.Iyer@Sun.COM 	 * that mip->mi_single_active_client will always have an associated
22689473SVenu.Iyer@Sun.COM 	 * flent.
22699473SVenu.Iyer@Sun.COM 	 */
22709473SVenu.Iyer@Sun.COM 	mac_client_add_to_flow_list(mcip, flent);
22719473SVenu.Iyer@Sun.COM 	if (nactiveclients_added)
22729473SVenu.Iyer@Sun.COM 		mac_update_single_active_client(mip);
22739473SVenu.Iyer@Sun.COM 	/*
22749473SVenu.Iyer@Sun.COM 	 * Trigger a renegotiation of the capabilities when the number of
22759473SVenu.Iyer@Sun.COM 	 * active clients changes from 1 to 2, since some of the capabilities
22769473SVenu.Iyer@Sun.COM 	 * might have to be disabled. Also send a MAC_NOTE_LINK notification
22779473SVenu.Iyer@Sun.COM 	 * to all the MAC clients whenever physical link is DOWN.
22789473SVenu.Iyer@Sun.COM 	 */
22799473SVenu.Iyer@Sun.COM 	if (mip->mi_nactiveclients == 2) {
22809473SVenu.Iyer@Sun.COM 		mac_capab_update((mac_handle_t)mip);
22819473SVenu.Iyer@Sun.COM 		mac_virtual_link_update(mip);
22829473SVenu.Iyer@Sun.COM 	}
22839473SVenu.Iyer@Sun.COM 	/*
22849473SVenu.Iyer@Sun.COM 	 * Now that the setup is complete, clear the INCIPIENT flag.
22859473SVenu.Iyer@Sun.COM 	 * The flag was set to avoid incoming packets seeing inconsistent
22869473SVenu.Iyer@Sun.COM 	 * structures while the setup was in progress. Clear the mci_tx_flag
22879473SVenu.Iyer@Sun.COM 	 * by calling mac_tx_client_block. It is possible that
22889473SVenu.Iyer@Sun.COM 	 * mac_unicast_remove was called prior to this mac_unicast_add which
22899473SVenu.Iyer@Sun.COM 	 * could have set the MCI_TX_QUIESCE flag.
22909473SVenu.Iyer@Sun.COM 	 */
22919473SVenu.Iyer@Sun.COM 	if (flent->fe_rx_ring_group != NULL)
22929473SVenu.Iyer@Sun.COM 		mac_rx_group_unmark(flent->fe_rx_ring_group, MR_INCIPIENT);
22939473SVenu.Iyer@Sun.COM 	FLOW_UNMARK(flent, FE_INCIPIENT);
22949473SVenu.Iyer@Sun.COM 	FLOW_UNMARK(flent, FE_MC_NO_DATAPATH);
22959473SVenu.Iyer@Sun.COM 	mac_tx_client_unblock(mcip);
22969473SVenu.Iyer@Sun.COM 	return (0);
22979473SVenu.Iyer@Sun.COM bail:
22989473SVenu.Iyer@Sun.COM 	if (bcast_added)
22999473SVenu.Iyer@Sun.COM 		mac_bcast_delete(mcip, mip->mi_type->mt_brdcst_addr, vid);
23009473SVenu.Iyer@Sun.COM 
23019473SVenu.Iyer@Sun.COM 	if (nactiveclients_added)
23029473SVenu.Iyer@Sun.COM 		mip->mi_nactiveclients--;
23039473SVenu.Iyer@Sun.COM 
230410013SNitin.Hande@Sun.COM 	if (mac_started)
230510013SNitin.Hande@Sun.COM 		mac_stop((mac_handle_t)mip);
230610013SNitin.Hande@Sun.COM 
23079473SVenu.Iyer@Sun.COM 	return (err);
23089473SVenu.Iyer@Sun.COM }
23099473SVenu.Iyer@Sun.COM 
23109473SVenu.Iyer@Sun.COM /*
23119473SVenu.Iyer@Sun.COM  * Return the passive primary MAC client, if present. The passive client is
23129473SVenu.Iyer@Sun.COM  * a stand-by client that has the same unicast address as another that is
23139473SVenu.Iyer@Sun.COM  * currenly active. Once the active client goes away, the passive client
23149473SVenu.Iyer@Sun.COM  * becomes active.
23159473SVenu.Iyer@Sun.COM  */
23169473SVenu.Iyer@Sun.COM static mac_client_impl_t *
23179473SVenu.Iyer@Sun.COM mac_get_passive_primary_client(mac_impl_t *mip)
23189473SVenu.Iyer@Sun.COM {
23199473SVenu.Iyer@Sun.COM 	mac_client_impl_t	*mcip;
23209473SVenu.Iyer@Sun.COM 
23219473SVenu.Iyer@Sun.COM 	for (mcip = mip->mi_clients_list; mcip != NULL;
23229473SVenu.Iyer@Sun.COM 	    mcip = mcip->mci_client_next) {
23239473SVenu.Iyer@Sun.COM 		if (mac_is_primary_client(mcip) &&
23249473SVenu.Iyer@Sun.COM 		    (mcip->mci_flags & MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) != 0) {
23259473SVenu.Iyer@Sun.COM 			return (mcip);
23269473SVenu.Iyer@Sun.COM 		}
23279473SVenu.Iyer@Sun.COM 	}
23289473SVenu.Iyer@Sun.COM 	return (NULL);
23299473SVenu.Iyer@Sun.COM }
23309473SVenu.Iyer@Sun.COM 
23319473SVenu.Iyer@Sun.COM /*
23328275SEric Cheng  * Add a new unicast address to the MAC client.
23338275SEric Cheng  *
23348275SEric Cheng  * The MAC address can be specified either by value, or the MAC client
23358275SEric Cheng  * can specify that it wants to use the primary MAC address of the
23368275SEric Cheng  * underlying MAC. See the introductory comments at the beginning
23378275SEric Cheng  * of this file for more more information on primary MAC addresses.
23388275SEric Cheng  *
23398275SEric Cheng  * Note also the tuple (MAC address, VID) must be unique
23408275SEric Cheng  * for the MAC clients defined on top of the same underlying MAC
23418275SEric Cheng  * instance, unless the MAC_UNICAST_NODUPCHECK is specified.
234210491SRishi.Srivatsavai@Sun.COM  *
234310491SRishi.Srivatsavai@Sun.COM  * In no case can a client use the PVID for the MAC, if the MAC has one set.
23448275SEric Cheng  */
23458275SEric Cheng int
23468275SEric Cheng i_mac_unicast_add(mac_client_handle_t mch, uint8_t *mac_addr, uint16_t flags,
23478275SEric Cheng     mac_unicast_handle_t *mah, uint16_t vid, mac_diag_t *diag)
23488275SEric Cheng {
23499473SVenu.Iyer@Sun.COM 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
23509473SVenu.Iyer@Sun.COM 	mac_impl_t		*mip = mcip->mci_mip;
23519473SVenu.Iyer@Sun.COM 	int			err;
23529473SVenu.Iyer@Sun.COM 	uint_t			mac_len = mip->mi_type->mt_addr_length;
23539473SVenu.Iyer@Sun.COM 	boolean_t		check_dups = !(flags & MAC_UNICAST_NODUPCHECK);
23549473SVenu.Iyer@Sun.COM 	boolean_t		fastpath_disabled = B_FALSE;
23559473SVenu.Iyer@Sun.COM 	boolean_t		is_primary = (flags & MAC_UNICAST_PRIMARY);
23569473SVenu.Iyer@Sun.COM 	boolean_t		is_unicast_hw = (flags & MAC_UNICAST_HW);
2357*11878SVenu.Iyer@Sun.COM 	mac_resource_props_t	*mrp;
23589473SVenu.Iyer@Sun.COM 	boolean_t		passive_client = B_FALSE;
23599473SVenu.Iyer@Sun.COM 	mac_unicast_impl_t	*muip;
23609473SVenu.Iyer@Sun.COM 	boolean_t		is_vnic_primary =
23619473SVenu.Iyer@Sun.COM 	    (flags & MAC_UNICAST_VNIC_PRIMARY);
23628275SEric Cheng 
23638275SEric Cheng 	/* when VID is non-zero, the underlying MAC can not be VNIC */
23648275SEric Cheng 	ASSERT(!((mip->mi_state_flags & MIS_IS_VNIC) && (vid != 0)));
23658275SEric Cheng 
23668275SEric Cheng 	/*
2367*11878SVenu.Iyer@Sun.COM 	 * Can't unicast add if the client asked only for minimal datapath
2368*11878SVenu.Iyer@Sun.COM 	 * setup.
2369*11878SVenu.Iyer@Sun.COM 	 */
2370*11878SVenu.Iyer@Sun.COM 	if (mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR)
2371*11878SVenu.Iyer@Sun.COM 		return (ENOTSUP);
2372*11878SVenu.Iyer@Sun.COM 
2373*11878SVenu.Iyer@Sun.COM 	/*
237410491SRishi.Srivatsavai@Sun.COM 	 * Check for an attempted use of the current Port VLAN ID, if enabled.
237510491SRishi.Srivatsavai@Sun.COM 	 * No client may use it.
237610491SRishi.Srivatsavai@Sun.COM 	 */
237710491SRishi.Srivatsavai@Sun.COM 	if (mip->mi_pvid != 0 && vid == mip->mi_pvid)
237810491SRishi.Srivatsavai@Sun.COM 		return (EBUSY);
237910491SRishi.Srivatsavai@Sun.COM 
238010491SRishi.Srivatsavai@Sun.COM 	/*
23818275SEric Cheng 	 * Check whether it's the primary client and flag it.
23828275SEric Cheng 	 */
23838275SEric Cheng 	if (!(mcip->mci_state_flags & MCIS_IS_VNIC) && is_primary && vid == 0)
23848275SEric Cheng 		mcip->mci_flags |= MAC_CLIENT_FLAGS_PRIMARY;
23858275SEric Cheng 
23868275SEric Cheng 	/*
23878275SEric Cheng 	 * is_vnic_primary is true when we come here as a VLAN VNIC
23888275SEric Cheng 	 * which uses the primary mac client's address but with a non-zero
23898275SEric Cheng 	 * VID. In this case the MAC address is not specified by an upper
23908275SEric Cheng 	 * MAC client.
23918275SEric Cheng 	 */
23928275SEric Cheng 	if ((mcip->mci_state_flags & MCIS_IS_VNIC) && is_primary &&
23938275SEric Cheng 	    !is_vnic_primary) {
23948275SEric Cheng 		/*
23958275SEric Cheng 		 * The address is being set by the upper MAC client
23968275SEric Cheng 		 * of a VNIC. The MAC address was already set by the
23978275SEric Cheng 		 * VNIC driver during VNIC creation.
23988275SEric Cheng 		 *
23998275SEric Cheng 		 * Note: a VNIC has only one MAC address. We return
24008275SEric Cheng 		 * the MAC unicast address handle of the lower MAC client
24018275SEric Cheng 		 * corresponding to the VNIC. We allocate a new entry
24028275SEric Cheng 		 * which is flagged appropriately, so that mac_unicast_remove()
24038275SEric Cheng 		 * doesn't attempt to free the original entry that
24048275SEric Cheng 		 * was allocated by the VNIC driver.
24058275SEric Cheng 		 */
24068275SEric Cheng 		ASSERT(mcip->mci_unicast != NULL);
24078275SEric Cheng 
24089024SVenu.Iyer@Sun.COM 		/* Check for VLAN flags, if present */
24099024SVenu.Iyer@Sun.COM 		if ((flags & MAC_UNICAST_TAG_DISABLE) != 0)
24109024SVenu.Iyer@Sun.COM 			mcip->mci_state_flags |= MCIS_TAG_DISABLE;
24119024SVenu.Iyer@Sun.COM 
24129024SVenu.Iyer@Sun.COM 		if ((flags & MAC_UNICAST_STRIP_DISABLE) != 0)
24139024SVenu.Iyer@Sun.COM 			mcip->mci_state_flags |= MCIS_STRIP_DISABLE;
24149024SVenu.Iyer@Sun.COM 
24159024SVenu.Iyer@Sun.COM 		if ((flags & MAC_UNICAST_DISABLE_TX_VID_CHECK) != 0)
24169024SVenu.Iyer@Sun.COM 			mcip->mci_state_flags |= MCIS_DISABLE_TX_VID_CHECK;
24179024SVenu.Iyer@Sun.COM 
24188275SEric Cheng 		/*
24198275SEric Cheng 		 * Ensure that the primary unicast address of the VNIC
24209473SVenu.Iyer@Sun.COM 		 * is added only once unless we have the
24219473SVenu.Iyer@Sun.COM 		 * MAC_CLIENT_FLAGS_MULTI_PRIMARY set (and this is not
24229473SVenu.Iyer@Sun.COM 		 * a passive MAC client).
24238275SEric Cheng 		 */
24249473SVenu.Iyer@Sun.COM 		if ((mcip->mci_flags & MAC_CLIENT_FLAGS_VNIC_PRIMARY) != 0) {
24259473SVenu.Iyer@Sun.COM 			if ((mcip->mci_flags &
24269473SVenu.Iyer@Sun.COM 			    MAC_CLIENT_FLAGS_MULTI_PRIMARY) == 0 ||
24279473SVenu.Iyer@Sun.COM 			    (mcip->mci_flags &
24289473SVenu.Iyer@Sun.COM 			    MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) != 0) {
24299473SVenu.Iyer@Sun.COM 				return (EBUSY);
24309473SVenu.Iyer@Sun.COM 			}
24319473SVenu.Iyer@Sun.COM 			mcip->mci_flags |= MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
24329473SVenu.Iyer@Sun.COM 			passive_client = B_TRUE;
24339473SVenu.Iyer@Sun.COM 		}
24348275SEric Cheng 
24358275SEric Cheng 		mcip->mci_flags |= MAC_CLIENT_FLAGS_VNIC_PRIMARY;
24368275SEric Cheng 
24378275SEric Cheng 		/*
24388275SEric Cheng 		 * Create a handle for vid 0.
24398275SEric Cheng 		 */
24408275SEric Cheng 		ASSERT(vid == 0);
24418275SEric Cheng 		muip = kmem_zalloc(sizeof (mac_unicast_impl_t), KM_SLEEP);
24428275SEric Cheng 		muip->mui_vid = vid;
24438275SEric Cheng 		*mah = (mac_unicast_handle_t)muip;
24449473SVenu.Iyer@Sun.COM 		/*
24459473SVenu.Iyer@Sun.COM 		 * This will be used by the caller to defer setting the
24469473SVenu.Iyer@Sun.COM 		 * rx functions.
24479473SVenu.Iyer@Sun.COM 		 */
24489473SVenu.Iyer@Sun.COM 		if (passive_client)
24499473SVenu.Iyer@Sun.COM 			return (EAGAIN);
24508275SEric Cheng 		return (0);
24518275SEric Cheng 	}
24528275SEric Cheng 
24538275SEric Cheng 	/* primary MAC clients cannot be opened on top of anchor VNICs */
24548275SEric Cheng 	if ((is_vnic_primary || is_primary) &&
24558275SEric Cheng 	    i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_ANCHOR_VNIC, NULL)) {
24568275SEric Cheng 		return (ENXIO);
24578275SEric Cheng 	}
24588275SEric Cheng 
24598275SEric Cheng 	/*
24609073SCathy.Zhou@Sun.COM 	 * If this is a VNIC/VLAN, disable softmac fast-path.
24619073SCathy.Zhou@Sun.COM 	 */
24629073SCathy.Zhou@Sun.COM 	if (mcip->mci_state_flags & MCIS_IS_VNIC) {
24639073SCathy.Zhou@Sun.COM 		err = mac_fastpath_disable((mac_handle_t)mip);
24649073SCathy.Zhou@Sun.COM 		if (err != 0)
24659073SCathy.Zhou@Sun.COM 			return (err);
24669073SCathy.Zhou@Sun.COM 		fastpath_disabled = B_TRUE;
24679073SCathy.Zhou@Sun.COM 	}
24689073SCathy.Zhou@Sun.COM 
24699073SCathy.Zhou@Sun.COM 	/*
24708275SEric Cheng 	 * Return EBUSY if:
24719073SCathy.Zhou@Sun.COM 	 *  - there is an exclusively active mac client exists.
24729073SCathy.Zhou@Sun.COM 	 *  - this is an exclusive active mac client but
24739073SCathy.Zhou@Sun.COM 	 *	a. there is already active mac clients exist, or
24749073SCathy.Zhou@Sun.COM 	 *	b. fastpath streams are already plumbed on this legacy device
247510491SRishi.Srivatsavai@Sun.COM 	 *  - the mac creator has disallowed active mac clients.
24768275SEric Cheng 	 */
247710491SRishi.Srivatsavai@Sun.COM 	if (mip->mi_state_flags & (MIS_EXCLUSIVE|MIS_NO_ACTIVE)) {
24789073SCathy.Zhou@Sun.COM 		if (fastpath_disabled)
24799073SCathy.Zhou@Sun.COM 			mac_fastpath_enable((mac_handle_t)mip);
24808275SEric Cheng 		return (EBUSY);
24818275SEric Cheng 	}
24828275SEric Cheng 
24839073SCathy.Zhou@Sun.COM 	if (mcip->mci_state_flags & MCIS_EXCLUSIVE) {
24849073SCathy.Zhou@Sun.COM 		ASSERT(!fastpath_disabled);
24859073SCathy.Zhou@Sun.COM 		if (mip->mi_nactiveclients != 0)
24869073SCathy.Zhou@Sun.COM 			return (EBUSY);
24879073SCathy.Zhou@Sun.COM 
24889073SCathy.Zhou@Sun.COM 		if ((mip->mi_state_flags & MIS_LEGACY) &&
24899073SCathy.Zhou@Sun.COM 		    !(mip->mi_capab_legacy.ml_active_set(mip->mi_driver))) {
24909073SCathy.Zhou@Sun.COM 			return (EBUSY);
24919073SCathy.Zhou@Sun.COM 		}
24928275SEric Cheng 		mip->mi_state_flags |= MIS_EXCLUSIVE;
24939073SCathy.Zhou@Sun.COM 	}
24948275SEric Cheng 
2495*11878SVenu.Iyer@Sun.COM 	mrp = kmem_zalloc(sizeof (*mrp), KM_SLEEP);
24968833SVenu.Iyer@Sun.COM 	if (is_primary && !(mcip->mci_state_flags & (MCIS_IS_VNIC |
24978833SVenu.Iyer@Sun.COM 	    MCIS_IS_AGGR_PORT))) {
24988275SEric Cheng 		/*
24998275SEric Cheng 		 * Apply the property cached in the mac_impl_t to the primary
25008833SVenu.Iyer@Sun.COM 		 * mac client. If the mac client is a VNIC or an aggregation
25018833SVenu.Iyer@Sun.COM 		 * port, its property should be set in the mcip when the
25028833SVenu.Iyer@Sun.COM 		 * VNIC/aggr was created.
25038275SEric Cheng 		 */
2504*11878SVenu.Iyer@Sun.COM 		mac_get_resources((mac_handle_t)mip, mrp);
2505*11878SVenu.Iyer@Sun.COM 		(void) mac_client_set_resources(mch, mrp);
25068275SEric Cheng 	} else if (mcip->mci_state_flags & MCIS_IS_VNIC) {
2507*11878SVenu.Iyer@Sun.COM 		/*
2508*11878SVenu.Iyer@Sun.COM 		 * This is a primary VLAN client, we don't support
2509*11878SVenu.Iyer@Sun.COM 		 * specifying rings property for this as it inherits the
2510*11878SVenu.Iyer@Sun.COM 		 * rings property from its MAC.
2511*11878SVenu.Iyer@Sun.COM 		 */
2512*11878SVenu.Iyer@Sun.COM 		if (is_vnic_primary) {
2513*11878SVenu.Iyer@Sun.COM 			mac_resource_props_t	*vmrp;
2514*11878SVenu.Iyer@Sun.COM 
2515*11878SVenu.Iyer@Sun.COM 			vmrp = MCIP_RESOURCE_PROPS(mcip);
2516*11878SVenu.Iyer@Sun.COM 			if (vmrp->mrp_mask & MRP_RX_RINGS ||
2517*11878SVenu.Iyer@Sun.COM 			    vmrp->mrp_mask & MRP_TX_RINGS) {
2518*11878SVenu.Iyer@Sun.COM 				if (fastpath_disabled)
2519*11878SVenu.Iyer@Sun.COM 					mac_fastpath_enable((mac_handle_t)mip);
2520*11878SVenu.Iyer@Sun.COM 				kmem_free(mrp, sizeof (*mrp));
2521*11878SVenu.Iyer@Sun.COM 				return (ENOTSUP);
2522*11878SVenu.Iyer@Sun.COM 			}
2523*11878SVenu.Iyer@Sun.COM 			/*
2524*11878SVenu.Iyer@Sun.COM 			 * Additionally we also need to inherit any
2525*11878SVenu.Iyer@Sun.COM 			 * rings property from the MAC.
2526*11878SVenu.Iyer@Sun.COM 			 */
2527*11878SVenu.Iyer@Sun.COM 			mac_get_resources((mac_handle_t)mip, mrp);
2528*11878SVenu.Iyer@Sun.COM 			if (mrp->mrp_mask & MRP_RX_RINGS) {
2529*11878SVenu.Iyer@Sun.COM 				vmrp->mrp_mask |= MRP_RX_RINGS;
2530*11878SVenu.Iyer@Sun.COM 				vmrp->mrp_nrxrings = mrp->mrp_nrxrings;
2531*11878SVenu.Iyer@Sun.COM 			}
2532*11878SVenu.Iyer@Sun.COM 			if (mrp->mrp_mask & MRP_TX_RINGS) {
2533*11878SVenu.Iyer@Sun.COM 				vmrp->mrp_mask |= MRP_TX_RINGS;
2534*11878SVenu.Iyer@Sun.COM 				vmrp->mrp_ntxrings = mrp->mrp_ntxrings;
2535*11878SVenu.Iyer@Sun.COM 			}
2536*11878SVenu.Iyer@Sun.COM 		}
2537*11878SVenu.Iyer@Sun.COM 		bcopy(MCIP_RESOURCE_PROPS(mcip), mrp, sizeof (*mrp));
25388275SEric Cheng 	}
25398275SEric Cheng 
25408275SEric Cheng 	muip = kmem_zalloc(sizeof (mac_unicast_impl_t), KM_SLEEP);
25418275SEric Cheng 	muip->mui_vid = vid;
25428275SEric Cheng 
25438275SEric Cheng 	if (is_primary || is_vnic_primary) {
25448275SEric Cheng 		mac_addr = mip->mi_addr;
25458275SEric Cheng 	} else {
25468275SEric Cheng 
25478275SEric Cheng 		/*
25488275SEric Cheng 		 * Verify the validity of the specified MAC addresses value.
25498275SEric Cheng 		 */
25508275SEric Cheng 		if (!mac_unicst_verify((mac_handle_t)mip, mac_addr, mac_len)) {
25518275SEric Cheng 			*diag = MAC_DIAG_MACADDR_INVALID;
25528275SEric Cheng 			err = EINVAL;
25539473SVenu.Iyer@Sun.COM 			goto bail_out;
25548275SEric Cheng 		}
25558275SEric Cheng 
25568275SEric Cheng 		/*
25578275SEric Cheng 		 * Make sure that the specified MAC address is different
25588275SEric Cheng 		 * than the unicast MAC address of the underlying NIC.
25598275SEric Cheng 		 */
25608275SEric Cheng 		if (check_dups && bcmp(mip->mi_addr, mac_addr, mac_len) == 0) {
25618275SEric Cheng 			*diag = MAC_DIAG_MACADDR_NIC;
25628275SEric Cheng 			err = EINVAL;
25639473SVenu.Iyer@Sun.COM 			goto bail_out;
25648275SEric Cheng 		}
25658275SEric Cheng 	}
25668275SEric Cheng 
25678275SEric Cheng 	/*
25689473SVenu.Iyer@Sun.COM 	 * Set the flags here so that if this is a passive client, we
25699473SVenu.Iyer@Sun.COM 	 * can return  and set it when we call mac_client_datapath_setup
25709473SVenu.Iyer@Sun.COM 	 * when this becomes the active client. If we defer to using these
25719473SVenu.Iyer@Sun.COM 	 * flags to mac_client_datapath_setup, then for a passive client,
25729473SVenu.Iyer@Sun.COM 	 * we'd have to store the flags somewhere (probably fe_flags)
25739473SVenu.Iyer@Sun.COM 	 * and then use it.
25748275SEric Cheng 	 */
25758275SEric Cheng 	if (!MCIP_DATAPATH_SETUP(mcip)) {
25768400SNicolas.Droux@Sun.COM 		if (is_unicast_hw) {
25778400SNicolas.Droux@Sun.COM 			/*
25788400SNicolas.Droux@Sun.COM 			 * The client requires a hardware MAC address slot
25798400SNicolas.Droux@Sun.COM 			 * for that unicast address. Since we support only
25808400SNicolas.Droux@Sun.COM 			 * one unicast MAC address per client, flag the
25818400SNicolas.Droux@Sun.COM 			 * MAC client itself.
25828400SNicolas.Droux@Sun.COM 			 */
25838400SNicolas.Droux@Sun.COM 			mcip->mci_state_flags |= MCIS_UNICAST_HW;
25848400SNicolas.Droux@Sun.COM 		}
25858275SEric Cheng 
25869024SVenu.Iyer@Sun.COM 		/* Check for VLAN flags, if present */
25879024SVenu.Iyer@Sun.COM 		if ((flags & MAC_UNICAST_TAG_DISABLE) != 0)
25889024SVenu.Iyer@Sun.COM 			mcip->mci_state_flags |= MCIS_TAG_DISABLE;
25899024SVenu.Iyer@Sun.COM 
25909024SVenu.Iyer@Sun.COM 		if ((flags & MAC_UNICAST_STRIP_DISABLE) != 0)
25919024SVenu.Iyer@Sun.COM 			mcip->mci_state_flags |= MCIS_STRIP_DISABLE;
25929024SVenu.Iyer@Sun.COM 
25939024SVenu.Iyer@Sun.COM 		if ((flags & MAC_UNICAST_DISABLE_TX_VID_CHECK) != 0)
25949024SVenu.Iyer@Sun.COM 			mcip->mci_state_flags |= MCIS_DISABLE_TX_VID_CHECK;
25958275SEric Cheng 	} else {
25969024SVenu.Iyer@Sun.COM 		/*
25979024SVenu.Iyer@Sun.COM 		 * Assert that the specified flags are consistent with the
25989024SVenu.Iyer@Sun.COM 		 * flags specified by previous calls to mac_unicast_add().
25999024SVenu.Iyer@Sun.COM 		 */
26009024SVenu.Iyer@Sun.COM 		ASSERT(((flags & MAC_UNICAST_TAG_DISABLE) != 0 &&
26019024SVenu.Iyer@Sun.COM 		    (mcip->mci_state_flags & MCIS_TAG_DISABLE) != 0) ||
26029024SVenu.Iyer@Sun.COM 		    ((flags & MAC_UNICAST_TAG_DISABLE) == 0 &&
26039024SVenu.Iyer@Sun.COM 		    (mcip->mci_state_flags & MCIS_TAG_DISABLE) == 0));
26049024SVenu.Iyer@Sun.COM 
26059024SVenu.Iyer@Sun.COM 		ASSERT(((flags & MAC_UNICAST_STRIP_DISABLE) != 0 &&
26069024SVenu.Iyer@Sun.COM 		    (mcip->mci_state_flags & MCIS_STRIP_DISABLE) != 0) ||
26079024SVenu.Iyer@Sun.COM 		    ((flags & MAC_UNICAST_STRIP_DISABLE) == 0 &&
26089024SVenu.Iyer@Sun.COM 		    (mcip->mci_state_flags & MCIS_STRIP_DISABLE) == 0));
26099024SVenu.Iyer@Sun.COM 
26109024SVenu.Iyer@Sun.COM 		ASSERT(((flags & MAC_UNICAST_DISABLE_TX_VID_CHECK) != 0 &&
26119024SVenu.Iyer@Sun.COM 		    (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK) != 0) ||
26129024SVenu.Iyer@Sun.COM 		    ((flags & MAC_UNICAST_DISABLE_TX_VID_CHECK) == 0 &&
26139024SVenu.Iyer@Sun.COM 		    (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK) == 0));
26149024SVenu.Iyer@Sun.COM 
26158400SNicolas.Droux@Sun.COM 		/*
26168400SNicolas.Droux@Sun.COM 		 * Make sure the client is consistent about its requests
26178400SNicolas.Droux@Sun.COM 		 * for MAC addresses. I.e. all requests from the clients
26188400SNicolas.Droux@Sun.COM 		 * must have the MAC_UNICAST_HW flag set or clear.
26198400SNicolas.Droux@Sun.COM 		 */
26208400SNicolas.Droux@Sun.COM 		if ((mcip->mci_state_flags & MCIS_UNICAST_HW) != 0 &&
26218400SNicolas.Droux@Sun.COM 		    !is_unicast_hw ||
26228400SNicolas.Droux@Sun.COM 		    (mcip->mci_state_flags & MCIS_UNICAST_HW) == 0 &&
26238400SNicolas.Droux@Sun.COM 		    is_unicast_hw) {
26248400SNicolas.Droux@Sun.COM 			err = EINVAL;
26259473SVenu.Iyer@Sun.COM 			goto bail_out;
26268275SEric Cheng 		}
26278275SEric Cheng 	}
26288275SEric Cheng 	/*
26299473SVenu.Iyer@Sun.COM 	 * Make sure the MAC address is not already used by
26309473SVenu.Iyer@Sun.COM 	 * another MAC client defined on top of the same
26319473SVenu.Iyer@Sun.COM 	 * underlying NIC. Unless we have MAC_CLIENT_FLAGS_MULTI_PRIMARY
26329473SVenu.Iyer@Sun.COM 	 * set when we allow a passive client to be present which will
26339473SVenu.Iyer@Sun.COM 	 * be activated when the currently active client goes away - this
26349473SVenu.Iyer@Sun.COM 	 * works only with primary addresses.
26358275SEric Cheng 	 */
26369473SVenu.Iyer@Sun.COM 	if ((check_dups || is_primary || is_vnic_primary) &&
26379473SVenu.Iyer@Sun.COM 	    mac_addr_in_use(mip, mac_addr, vid)) {
26389473SVenu.Iyer@Sun.COM 		/*
26399473SVenu.Iyer@Sun.COM 		 * Must have set the multiple primary address flag when
26409473SVenu.Iyer@Sun.COM 		 * we did a mac_client_open AND this should be a primary
26419473SVenu.Iyer@Sun.COM 		 * MAC client AND there should not already be a passive
26429473SVenu.Iyer@Sun.COM 		 * primary. If all is true then we let this succeed
26439473SVenu.Iyer@Sun.COM 		 * even if the address is a dup.
26449473SVenu.Iyer@Sun.COM 		 */
26459473SVenu.Iyer@Sun.COM 		if ((mcip->mci_flags & MAC_CLIENT_FLAGS_MULTI_PRIMARY) == 0 ||
26469473SVenu.Iyer@Sun.COM 		    (mcip->mci_flags & MAC_CLIENT_FLAGS_PRIMARY) == 0 ||
26479473SVenu.Iyer@Sun.COM 		    mac_get_passive_primary_client(mip) != NULL) {
26489473SVenu.Iyer@Sun.COM 			*diag = MAC_DIAG_MACADDR_INUSE;
26499473SVenu.Iyer@Sun.COM 			err = EEXIST;
26509473SVenu.Iyer@Sun.COM 			goto bail_out;
26519473SVenu.Iyer@Sun.COM 		}
26529473SVenu.Iyer@Sun.COM 		ASSERT((mcip->mci_flags &
26539473SVenu.Iyer@Sun.COM 		    MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) == 0);
26549473SVenu.Iyer@Sun.COM 		mcip->mci_flags |= MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
2655*11878SVenu.Iyer@Sun.COM 		kmem_free(mrp, sizeof (*mrp));
26569473SVenu.Iyer@Sun.COM 
26579473SVenu.Iyer@Sun.COM 		/*
26589473SVenu.Iyer@Sun.COM 		 * Stash the unicast address handle, we will use it when
26599473SVenu.Iyer@Sun.COM 		 * we set up the passive client.
26609473SVenu.Iyer@Sun.COM 		 */
26619473SVenu.Iyer@Sun.COM 		mcip->mci_p_unicast_list = muip;
26629473SVenu.Iyer@Sun.COM 		*mah = (mac_unicast_handle_t)muip;
26639473SVenu.Iyer@Sun.COM 		return (0);
26649473SVenu.Iyer@Sun.COM 	}
26659473SVenu.Iyer@Sun.COM 
2666*11878SVenu.Iyer@Sun.COM 	err = mac_client_datapath_setup(mcip, vid, mac_addr, mrp,
26679473SVenu.Iyer@Sun.COM 	    is_primary || is_vnic_primary, muip);
26689473SVenu.Iyer@Sun.COM 	if (err != 0)
26699473SVenu.Iyer@Sun.COM 		goto bail_out;
2670*11878SVenu.Iyer@Sun.COM 
2671*11878SVenu.Iyer@Sun.COM 	kmem_free(mrp, sizeof (*mrp));
26729473SVenu.Iyer@Sun.COM 	*mah = (mac_unicast_handle_t)muip;
26738275SEric Cheng 	return (0);
26749473SVenu.Iyer@Sun.COM 
26759473SVenu.Iyer@Sun.COM bail_out:
26769473SVenu.Iyer@Sun.COM 	if (fastpath_disabled)
26779473SVenu.Iyer@Sun.COM 		mac_fastpath_enable((mac_handle_t)mip);
26789073SCathy.Zhou@Sun.COM 	if (mcip->mci_state_flags & MCIS_EXCLUSIVE) {
26798275SEric Cheng 		mip->mi_state_flags &= ~MIS_EXCLUSIVE;
26809473SVenu.Iyer@Sun.COM 		if (mip->mi_state_flags & MIS_LEGACY) {
26819473SVenu.Iyer@Sun.COM 			mip->mi_capab_legacy.ml_active_clear(
26829473SVenu.Iyer@Sun.COM 			    mip->mi_driver);
26839473SVenu.Iyer@Sun.COM 		}
26849073SCathy.Zhou@Sun.COM 	}
2685*11878SVenu.Iyer@Sun.COM 	kmem_free(mrp, sizeof (*mrp));
26868275SEric Cheng 	kmem_free(muip, sizeof (mac_unicast_impl_t));
26878275SEric Cheng 	return (err);
26888275SEric Cheng }
26898275SEric Cheng 
26909473SVenu.Iyer@Sun.COM /*
26919473SVenu.Iyer@Sun.COM  * Wrapper function to mac_unicast_add when we want to have the same mac
26929473SVenu.Iyer@Sun.COM  * client open for two instances, one that is currently active and another
26939473SVenu.Iyer@Sun.COM  * that will become active when the current one is removed. In this case
26949473SVenu.Iyer@Sun.COM  * mac_unicast_add will return EGAIN and we will save the rx function and
26959473SVenu.Iyer@Sun.COM  * arg which will be used when we activate the passive client in
26969473SVenu.Iyer@Sun.COM  * mac_unicast_remove.
26979473SVenu.Iyer@Sun.COM  */
26989473SVenu.Iyer@Sun.COM int
26999473SVenu.Iyer@Sun.COM mac_unicast_add_set_rx(mac_client_handle_t mch, uint8_t *mac_addr,
27009473SVenu.Iyer@Sun.COM     uint16_t flags, mac_unicast_handle_t *mah,  uint16_t vid, mac_diag_t *diag,
27019473SVenu.Iyer@Sun.COM     mac_rx_t rx_fn, void *arg)
27029473SVenu.Iyer@Sun.COM {
27039473SVenu.Iyer@Sun.COM 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
27049473SVenu.Iyer@Sun.COM 	uint_t			err;
27059473SVenu.Iyer@Sun.COM 
27069473SVenu.Iyer@Sun.COM 	err = mac_unicast_add(mch, mac_addr, flags, mah, vid, diag);
27079473SVenu.Iyer@Sun.COM 	if (err != 0 && err != EAGAIN)
27089473SVenu.Iyer@Sun.COM 		return (err);
27099473SVenu.Iyer@Sun.COM 	if (err == EAGAIN) {
27109473SVenu.Iyer@Sun.COM 		if (rx_fn != NULL) {
27119473SVenu.Iyer@Sun.COM 			mcip->mci_rx_p_fn = rx_fn;
27129473SVenu.Iyer@Sun.COM 			mcip->mci_rx_p_arg = arg;
27139473SVenu.Iyer@Sun.COM 		}
27149473SVenu.Iyer@Sun.COM 		return (0);
27159473SVenu.Iyer@Sun.COM 	}
27169473SVenu.Iyer@Sun.COM 	if (rx_fn != NULL)
27179473SVenu.Iyer@Sun.COM 		mac_rx_set(mch, rx_fn, arg);
27189473SVenu.Iyer@Sun.COM 	return (err);
27199473SVenu.Iyer@Sun.COM }
27209473SVenu.Iyer@Sun.COM 
27218275SEric Cheng int
27228275SEric Cheng mac_unicast_add(mac_client_handle_t mch, uint8_t *mac_addr, uint16_t flags,
27238275SEric Cheng     mac_unicast_handle_t *mah, uint16_t vid, mac_diag_t *diag)
27248275SEric Cheng {
27258275SEric Cheng 	mac_impl_t *mip = ((mac_client_impl_t *)mch)->mci_mip;
27268275SEric Cheng 	uint_t err;
27278275SEric Cheng 
27288275SEric Cheng 	i_mac_perim_enter(mip);
27298275SEric Cheng 	err = i_mac_unicast_add(mch, mac_addr, flags, mah, vid, diag);
27308275SEric Cheng 	i_mac_perim_exit(mip);
27318275SEric Cheng 
27328275SEric Cheng 	return (err);
27338275SEric Cheng }
27348275SEric Cheng 
2735*11878SVenu.Iyer@Sun.COM static void
27369473SVenu.Iyer@Sun.COM mac_client_datapath_teardown(mac_client_handle_t mch, mac_unicast_impl_t *muip,
27379473SVenu.Iyer@Sun.COM     flow_entry_t *flent)
27388275SEric Cheng {
27399473SVenu.Iyer@Sun.COM 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
27409473SVenu.Iyer@Sun.COM 	mac_impl_t		*mip = mcip->mci_mip;
2741*11878SVenu.Iyer@Sun.COM 	boolean_t		no_unicast;
27428275SEric Cheng 
27438833SVenu.Iyer@Sun.COM 	/*
2744*11878SVenu.Iyer@Sun.COM 	 * If we have not added a unicast address for this MAC client, just
2745*11878SVenu.Iyer@Sun.COM 	 * teardown the datapath.
27468833SVenu.Iyer@Sun.COM 	 */
2747*11878SVenu.Iyer@Sun.COM 	no_unicast = mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR;
2748*11878SVenu.Iyer@Sun.COM 
2749*11878SVenu.Iyer@Sun.COM 	if (!no_unicast) {
2750*11878SVenu.Iyer@Sun.COM 		/*
2751*11878SVenu.Iyer@Sun.COM 		 * We would have initialized subflows etc. only if we brought
2752*11878SVenu.Iyer@Sun.COM 		 * up the primary client and set the unicast unicast address
2753*11878SVenu.Iyer@Sun.COM 		 * etc. Deactivate the flows. The flow entry will be removed
2754*11878SVenu.Iyer@Sun.COM 		 * from the active flow tables, and the associated SRS,
2755*11878SVenu.Iyer@Sun.COM 		 * softrings etc will be deleted. But the flow entry itself
2756*11878SVenu.Iyer@Sun.COM 		 * won't be destroyed, instead it will continue to be archived
2757*11878SVenu.Iyer@Sun.COM 		 * off the  the global flow hash list, for a possible future
2758*11878SVenu.Iyer@Sun.COM 		 * activation when say IP is plumbed again.
2759*11878SVenu.Iyer@Sun.COM 		 */
2760*11878SVenu.Iyer@Sun.COM 		mac_link_release_flows(mch);
2761*11878SVenu.Iyer@Sun.COM 	}
27628275SEric Cheng 	mip->mi_nactiveclients--;
27638833SVenu.Iyer@Sun.COM 	mac_update_single_active_client(mip);
27648275SEric Cheng 
27659473SVenu.Iyer@Sun.COM 	/* Tear down the data path */
27668275SEric Cheng 	mac_datapath_teardown(mcip, mcip->mci_flent, SRST_LINK);
27678275SEric Cheng 
27688275SEric Cheng 	/*
27698275SEric Cheng 	 * Prevent any future access to the flow entry through the mci_flent
27708275SEric Cheng 	 * pointer by setting the mci_flent to NULL. Access to mci_flent in
27718275SEric Cheng 	 * mac_bcast_send is also under mi_rw_lock.
27728275SEric Cheng 	 */
27738275SEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
27748275SEric Cheng 	flent = mcip->mci_flent;
27758275SEric Cheng 	mac_client_remove_flow_from_list(mcip, flent);
27768275SEric Cheng 
27778275SEric Cheng 	if (mcip->mci_state_flags & MCIS_DESC_LOGGED)
27788275SEric Cheng 		mcip->mci_state_flags &= ~MCIS_DESC_LOGGED;
27798275SEric Cheng 
27808275SEric Cheng 	/*
27818275SEric Cheng 	 * This is the last unicast address being removed and there shouldn't
27828275SEric Cheng 	 * be any outbound data threads at this point coming down from mac
27838275SEric Cheng 	 * clients. We have waited for the data threads to finish before
27848275SEric Cheng 	 * starting dld_str_detach. Non-data threads must access TX SRS
27858275SEric Cheng 	 * under mi_rw_lock.
27868275SEric Cheng 	 */
27878275SEric Cheng 	rw_exit(&mip->mi_rw_lock);
27888275SEric Cheng 
27898275SEric Cheng 	/*
27908275SEric Cheng 	 * Don't use FLOW_MARK with FE_MC_NO_DATAPATH, as the flow might
27918275SEric Cheng 	 * contain other flags, such as FE_CONDEMNED, which we need to
27928275SEric Cheng 	 * cleared. We don't call mac_flow_cleanup() for this unicast
27938275SEric Cheng 	 * flow as we have a already cleaned up SRSs etc. (via the teadown
27948275SEric Cheng 	 * path). We just clear the stats and reset the initial callback
27958275SEric Cheng 	 * function, the rest will be set when we call mac_flow_create,
27968275SEric Cheng 	 * if at all.
27978275SEric Cheng 	 */
27988275SEric Cheng 	mutex_enter(&flent->fe_lock);
27998275SEric Cheng 	ASSERT(flent->fe_refcnt == 1 && flent->fe_mbg == NULL &&
28008275SEric Cheng 	    flent->fe_tx_srs == NULL && flent->fe_rx_srs_cnt == 0);
28018275SEric Cheng 	flent->fe_flags = FE_MC_NO_DATAPATH;
28028275SEric Cheng 	flow_stat_destroy(flent);
2803*11878SVenu.Iyer@Sun.COM 	mac_misc_stat_delete(flent);
28048275SEric Cheng 
28058275SEric Cheng 	/* Initialize the receiver function to a safe routine */
28068275SEric Cheng 	flent->fe_cb_fn = (flow_fn_t)mac_pkt_drop;
28078275SEric Cheng 	flent->fe_cb_arg1 = NULL;
28088275SEric Cheng 	flent->fe_cb_arg2 = NULL;
28098275SEric Cheng 
28108275SEric Cheng 	flent->fe_index = -1;
28118275SEric Cheng 	mutex_exit(&flent->fe_lock);
28128275SEric Cheng 
28138275SEric Cheng 	if (mip->mi_type->mt_brdcst_addr != NULL) {
2814*11878SVenu.Iyer@Sun.COM 		ASSERT(muip != NULL || no_unicast);
28158275SEric Cheng 		mac_bcast_delete(mcip, mip->mi_type->mt_brdcst_addr,
2816*11878SVenu.Iyer@Sun.COM 		    muip != NULL ? muip->mui_vid : VLAN_ID_NONE);
28178275SEric Cheng 	}
28188275SEric Cheng 
28198275SEric Cheng 	if (mip->mi_nactiveclients == 1) {
28208275SEric Cheng 		mac_capab_update((mac_handle_t)mip);
28218275SEric Cheng 		mac_virtual_link_update(mip);
28228275SEric Cheng 	}
28239073SCathy.Zhou@Sun.COM 
28249073SCathy.Zhou@Sun.COM 	if (mcip->mci_state_flags & MCIS_EXCLUSIVE) {
28258275SEric Cheng 		mip->mi_state_flags &= ~MIS_EXCLUSIVE;
28269073SCathy.Zhou@Sun.COM 
28279073SCathy.Zhou@Sun.COM 		if (mip->mi_state_flags & MIS_LEGACY)
28289073SCathy.Zhou@Sun.COM 			mip->mi_capab_legacy.ml_active_clear(mip->mi_driver);
28299073SCathy.Zhou@Sun.COM 	}
28309073SCathy.Zhou@Sun.COM 
28318400SNicolas.Droux@Sun.COM 	mcip->mci_state_flags &= ~MCIS_UNICAST_HW;
28328275SEric Cheng 
28339024SVenu.Iyer@Sun.COM 	if (mcip->mci_state_flags & MCIS_TAG_DISABLE)
28349024SVenu.Iyer@Sun.COM 		mcip->mci_state_flags &= ~MCIS_TAG_DISABLE;
28359024SVenu.Iyer@Sun.COM 
28369024SVenu.Iyer@Sun.COM 	if (mcip->mci_state_flags & MCIS_STRIP_DISABLE)
28379024SVenu.Iyer@Sun.COM 		mcip->mci_state_flags &= ~MCIS_STRIP_DISABLE;
28389024SVenu.Iyer@Sun.COM 
28399024SVenu.Iyer@Sun.COM 	if (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK)
28409024SVenu.Iyer@Sun.COM 		mcip->mci_state_flags &= ~MCIS_DISABLE_TX_VID_CHECK;
28419024SVenu.Iyer@Sun.COM 
2842*11878SVenu.Iyer@Sun.COM 	if (muip != NULL)
2843*11878SVenu.Iyer@Sun.COM 		kmem_free(muip, sizeof (mac_unicast_impl_t));
2844*11878SVenu.Iyer@Sun.COM 	mac_protect_cancel_timer(mcip);
2845*11878SVenu.Iyer@Sun.COM 	mac_protect_flush_dhcp(mcip);
2846*11878SVenu.Iyer@Sun.COM 
2847*11878SVenu.Iyer@Sun.COM 	bzero(&mcip->mci_misc_stat, sizeof (mcip->mci_misc_stat));
28489073SCathy.Zhou@Sun.COM 	/*
28499073SCathy.Zhou@Sun.COM 	 * Disable fastpath if this is a VNIC or a VLAN.
28509073SCathy.Zhou@Sun.COM 	 */
28519073SCathy.Zhou@Sun.COM 	if (mcip->mci_state_flags & MCIS_IS_VNIC)
28529073SCathy.Zhou@Sun.COM 		mac_fastpath_enable((mac_handle_t)mip);
28538893SMichael.Lim@Sun.COM 	mac_stop((mac_handle_t)mip);
28549473SVenu.Iyer@Sun.COM }
28559473SVenu.Iyer@Sun.COM 
28569473SVenu.Iyer@Sun.COM /*
28579473SVenu.Iyer@Sun.COM  * Remove a MAC address which was previously added by mac_unicast_add().
28589473SVenu.Iyer@Sun.COM  */
28599473SVenu.Iyer@Sun.COM int
28609473SVenu.Iyer@Sun.COM mac_unicast_remove(mac_client_handle_t mch, mac_unicast_handle_t mah)
28619473SVenu.Iyer@Sun.COM {
28629473SVenu.Iyer@Sun.COM 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
28639473SVenu.Iyer@Sun.COM 	mac_unicast_impl_t *muip = (mac_unicast_impl_t *)mah;
28649473SVenu.Iyer@Sun.COM 	mac_unicast_impl_t *pre;
28659473SVenu.Iyer@Sun.COM 	mac_impl_t *mip = mcip->mci_mip;
28669473SVenu.Iyer@Sun.COM 	flow_entry_t		*flent;
2867*11878SVenu.Iyer@Sun.COM 	uint16_t mui_vid;
28689473SVenu.Iyer@Sun.COM 
28699473SVenu.Iyer@Sun.COM 	i_mac_perim_enter(mip);
28709473SVenu.Iyer@Sun.COM 	if (mcip->mci_flags & MAC_CLIENT_FLAGS_VNIC_PRIMARY) {
28719473SVenu.Iyer@Sun.COM 		/*
28729473SVenu.Iyer@Sun.COM 		 * Called made by the upper MAC client of a VNIC.
28739473SVenu.Iyer@Sun.COM 		 * There's nothing much to do, the unicast address will
28749473SVenu.Iyer@Sun.COM 		 * be removed by the VNIC driver when the VNIC is deleted,
28759473SVenu.Iyer@Sun.COM 		 * but let's ensure that all our transmit is done before
28769473SVenu.Iyer@Sun.COM 		 * the client does a mac_client_stop lest it trigger an
28779473SVenu.Iyer@Sun.COM 		 * assert in the driver.
28789473SVenu.Iyer@Sun.COM 		 */
28799473SVenu.Iyer@Sun.COM 		ASSERT(muip->mui_vid == 0);
28809473SVenu.Iyer@Sun.COM 
28819473SVenu.Iyer@Sun.COM 		mac_tx_client_flush(mcip);
28829473SVenu.Iyer@Sun.COM 
28839473SVenu.Iyer@Sun.COM 		if ((mcip->mci_flags & MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) != 0) {
28849473SVenu.Iyer@Sun.COM 			mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
28859473SVenu.Iyer@Sun.COM 			if (mcip->mci_rx_p_fn != NULL) {
28869473SVenu.Iyer@Sun.COM 				mac_rx_set(mch, mcip->mci_rx_p_fn,
28879473SVenu.Iyer@Sun.COM 				    mcip->mci_rx_p_arg);
28889473SVenu.Iyer@Sun.COM 				mcip->mci_rx_p_fn = NULL;
28899473SVenu.Iyer@Sun.COM 				mcip->mci_rx_p_arg = NULL;
28909473SVenu.Iyer@Sun.COM 			}
28919473SVenu.Iyer@Sun.COM 			kmem_free(muip, sizeof (mac_unicast_impl_t));
28929473SVenu.Iyer@Sun.COM 			i_mac_perim_exit(mip);
28939473SVenu.Iyer@Sun.COM 			return (0);
28949473SVenu.Iyer@Sun.COM 		}
28959473SVenu.Iyer@Sun.COM 		mcip->mci_flags &= ~MAC_CLIENT_FLAGS_VNIC_PRIMARY;
28969473SVenu.Iyer@Sun.COM 
28979473SVenu.Iyer@Sun.COM 		if (mcip->mci_state_flags & MCIS_TAG_DISABLE)
28989473SVenu.Iyer@Sun.COM 			mcip->mci_state_flags &= ~MCIS_TAG_DISABLE;
28999473SVenu.Iyer@Sun.COM 
29009473SVenu.Iyer@Sun.COM 		if (mcip->mci_state_flags & MCIS_STRIP_DISABLE)
29019473SVenu.Iyer@Sun.COM 			mcip->mci_state_flags &= ~MCIS_STRIP_DISABLE;
29029473SVenu.Iyer@Sun.COM 
29039473SVenu.Iyer@Sun.COM 		if (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK)
29049473SVenu.Iyer@Sun.COM 			mcip->mci_state_flags &= ~MCIS_DISABLE_TX_VID_CHECK;
29059473SVenu.Iyer@Sun.COM 
29069473SVenu.Iyer@Sun.COM 		kmem_free(muip, sizeof (mac_unicast_impl_t));
29079473SVenu.Iyer@Sun.COM 		i_mac_perim_exit(mip);
29089473SVenu.Iyer@Sun.COM 		return (0);
29099473SVenu.Iyer@Sun.COM 	}
29109473SVenu.Iyer@Sun.COM 
29119473SVenu.Iyer@Sun.COM 	ASSERT(muip != NULL);
29129473SVenu.Iyer@Sun.COM 
29139473SVenu.Iyer@Sun.COM 	/*
29149473SVenu.Iyer@Sun.COM 	 * We are removing a passive client, we haven't setup the datapath
29159473SVenu.Iyer@Sun.COM 	 * for this yet, so nothing much to do.
29169473SVenu.Iyer@Sun.COM 	 */
29179614SVenu.Iyer@Sun.COM 	if ((mcip->mci_flags & MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) != 0) {
29189473SVenu.Iyer@Sun.COM 
29199473SVenu.Iyer@Sun.COM 		ASSERT((mcip->mci_flent->fe_flags & FE_MC_NO_DATAPATH) != 0);
29209473SVenu.Iyer@Sun.COM 		ASSERT(mcip->mci_p_unicast_list == muip);
29219473SVenu.Iyer@Sun.COM 
29229614SVenu.Iyer@Sun.COM 		mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
29239614SVenu.Iyer@Sun.COM 
29249473SVenu.Iyer@Sun.COM 		mcip->mci_p_unicast_list = NULL;
29259473SVenu.Iyer@Sun.COM 		mcip->mci_rx_p_fn = NULL;
29269473SVenu.Iyer@Sun.COM 		mcip->mci_rx_p_arg = NULL;
29279473SVenu.Iyer@Sun.COM 
29289473SVenu.Iyer@Sun.COM 		mcip->mci_state_flags &= ~MCIS_UNICAST_HW;
29299473SVenu.Iyer@Sun.COM 
29309473SVenu.Iyer@Sun.COM 		if (mcip->mci_state_flags & MCIS_TAG_DISABLE)
29319473SVenu.Iyer@Sun.COM 			mcip->mci_state_flags &= ~MCIS_TAG_DISABLE;
29329473SVenu.Iyer@Sun.COM 
29339473SVenu.Iyer@Sun.COM 		if (mcip->mci_state_flags & MCIS_STRIP_DISABLE)
29349473SVenu.Iyer@Sun.COM 			mcip->mci_state_flags &= ~MCIS_STRIP_DISABLE;
29359473SVenu.Iyer@Sun.COM 
29369473SVenu.Iyer@Sun.COM 		if (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK)
29379473SVenu.Iyer@Sun.COM 			mcip->mci_state_flags &= ~MCIS_DISABLE_TX_VID_CHECK;
29389473SVenu.Iyer@Sun.COM 
29399473SVenu.Iyer@Sun.COM 		kmem_free(muip, sizeof (mac_unicast_impl_t));
29409473SVenu.Iyer@Sun.COM 		i_mac_perim_exit(mip);
29419473SVenu.Iyer@Sun.COM 		return (0);
29429473SVenu.Iyer@Sun.COM 	}
29439473SVenu.Iyer@Sun.COM 	/*
29449473SVenu.Iyer@Sun.COM 	 * Remove the VID from the list of client's VIDs.
29459473SVenu.Iyer@Sun.COM 	 */
29469473SVenu.Iyer@Sun.COM 	pre = mcip->mci_unicast_list;
29479473SVenu.Iyer@Sun.COM 	if (muip == pre) {
29489473SVenu.Iyer@Sun.COM 		mcip->mci_unicast_list = muip->mui_next;
29499473SVenu.Iyer@Sun.COM 	} else {
29509473SVenu.Iyer@Sun.COM 		while ((pre->mui_next != NULL) && (pre->mui_next != muip))
29519473SVenu.Iyer@Sun.COM 			pre = pre->mui_next;
29529473SVenu.Iyer@Sun.COM 		ASSERT(pre->mui_next == muip);
29539473SVenu.Iyer@Sun.COM 		rw_enter(&mcip->mci_rw_lock, RW_WRITER);
29549473SVenu.Iyer@Sun.COM 		pre->mui_next = muip->mui_next;
29559473SVenu.Iyer@Sun.COM 		rw_exit(&mcip->mci_rw_lock);
29569473SVenu.Iyer@Sun.COM 	}
29579473SVenu.Iyer@Sun.COM 
29589473SVenu.Iyer@Sun.COM 	if (!mac_client_single_rcvr(mcip)) {
29599473SVenu.Iyer@Sun.COM 		/*
29609473SVenu.Iyer@Sun.COM 		 * This MAC client is shared by more than one unicast
29619473SVenu.Iyer@Sun.COM 		 * addresses, so we will just remove the flent
29629473SVenu.Iyer@Sun.COM 		 * corresponding to the address being removed. We don't invoke
29639473SVenu.Iyer@Sun.COM 		 * mac_rx_classify_flow_rem() since the additional flow is
29649473SVenu.Iyer@Sun.COM 		 * not associated with its own separate set of SRS and rings,
29659473SVenu.Iyer@Sun.COM 		 * and these constructs are still needed for the remaining
29669473SVenu.Iyer@Sun.COM 		 * flows.
29679473SVenu.Iyer@Sun.COM 		 */
29689473SVenu.Iyer@Sun.COM 		flent = mac_client_get_flow(mcip, muip);
29699473SVenu.Iyer@Sun.COM 		ASSERT(flent != NULL);
29709473SVenu.Iyer@Sun.COM 
29719473SVenu.Iyer@Sun.COM 		/*
29729473SVenu.Iyer@Sun.COM 		 * The first one is disappearing, need to make sure
29739473SVenu.Iyer@Sun.COM 		 * we replace it with another from the list of
29749473SVenu.Iyer@Sun.COM 		 * shared clients.
29759473SVenu.Iyer@Sun.COM 		 */
29769473SVenu.Iyer@Sun.COM 		if (flent == mcip->mci_flent)
29779473SVenu.Iyer@Sun.COM 			flent = mac_client_swap_mciflent(mcip);
29789473SVenu.Iyer@Sun.COM 		mac_client_remove_flow_from_list(mcip, flent);
29799473SVenu.Iyer@Sun.COM 		mac_flow_remove(mip->mi_flow_tab, flent, B_FALSE);
29809473SVenu.Iyer@Sun.COM 		mac_flow_wait(flent, FLOW_DRIVER_UPCALL);
29819473SVenu.Iyer@Sun.COM 
29829473SVenu.Iyer@Sun.COM 		/*
29839473SVenu.Iyer@Sun.COM 		 * The multicast groups that were added by the client so
29849473SVenu.Iyer@Sun.COM 		 * far must be removed from the brodcast domain corresponding
29859473SVenu.Iyer@Sun.COM 		 * to the VID being removed.
29869473SVenu.Iyer@Sun.COM 		 */
29879473SVenu.Iyer@Sun.COM 		mac_client_bcast_refresh(mcip, mac_client_update_mcast,
29889473SVenu.Iyer@Sun.COM 		    (void *)flent, B_FALSE);
29899473SVenu.Iyer@Sun.COM 
29909473SVenu.Iyer@Sun.COM 		if (mip->mi_type->mt_brdcst_addr != NULL) {
29919473SVenu.Iyer@Sun.COM 			mac_bcast_delete(mcip, mip->mi_type->mt_brdcst_addr,
29929473SVenu.Iyer@Sun.COM 			    muip->mui_vid);
29939473SVenu.Iyer@Sun.COM 		}
29949473SVenu.Iyer@Sun.COM 
29959473SVenu.Iyer@Sun.COM 		FLOW_FINAL_REFRELE(flent);
29969473SVenu.Iyer@Sun.COM 		ASSERT(!(mcip->mci_state_flags & MCIS_EXCLUSIVE));
29979473SVenu.Iyer@Sun.COM 		/*
29989473SVenu.Iyer@Sun.COM 		 * Enable fastpath if this is a VNIC or a VLAN.
29999473SVenu.Iyer@Sun.COM 		 */
30009473SVenu.Iyer@Sun.COM 		if (mcip->mci_state_flags & MCIS_IS_VNIC)
30019473SVenu.Iyer@Sun.COM 			mac_fastpath_enable((mac_handle_t)mip);
30029473SVenu.Iyer@Sun.COM 		mac_stop((mac_handle_t)mip);
30039473SVenu.Iyer@Sun.COM 		i_mac_perim_exit(mip);
30049473SVenu.Iyer@Sun.COM 		return (0);
30059473SVenu.Iyer@Sun.COM 	}
30069473SVenu.Iyer@Sun.COM 
3007*11878SVenu.Iyer@Sun.COM 	mui_vid = muip->mui_vid;
30089473SVenu.Iyer@Sun.COM 	mac_client_datapath_teardown(mch, muip, flent);
30099473SVenu.Iyer@Sun.COM 
3010*11878SVenu.Iyer@Sun.COM 	if ((mcip->mci_flags & MAC_CLIENT_FLAGS_PRIMARY) && mui_vid == 0) {
3011*11878SVenu.Iyer@Sun.COM 		mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PRIMARY;
3012*11878SVenu.Iyer@Sun.COM 	} else {
3013*11878SVenu.Iyer@Sun.COM 		i_mac_perim_exit(mip);
3014*11878SVenu.Iyer@Sun.COM 		return (0);
3015*11878SVenu.Iyer@Sun.COM 	}
3016*11878SVenu.Iyer@Sun.COM 
30179473SVenu.Iyer@Sun.COM 	/*
30189473SVenu.Iyer@Sun.COM 	 * If we are removing the primary, check if we have a passive primary
30199473SVenu.Iyer@Sun.COM 	 * client that we need to activate now.
30209473SVenu.Iyer@Sun.COM 	 */
30219473SVenu.Iyer@Sun.COM 	mcip = mac_get_passive_primary_client(mip);
30229473SVenu.Iyer@Sun.COM 	if (mcip != NULL) {
3023*11878SVenu.Iyer@Sun.COM 		mac_resource_props_t	*mrp;
30249473SVenu.Iyer@Sun.COM 		mac_unicast_impl_t	*muip;
30259473SVenu.Iyer@Sun.COM 
30269473SVenu.Iyer@Sun.COM 		mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
3027*11878SVenu.Iyer@Sun.COM 		mrp = kmem_zalloc(sizeof (*mrp), KM_SLEEP);
3028*11878SVenu.Iyer@Sun.COM 
30299473SVenu.Iyer@Sun.COM 		/*
30309473SVenu.Iyer@Sun.COM 		 * Apply the property cached in the mac_impl_t to the
30319473SVenu.Iyer@Sun.COM 		 * primary mac client.
30329473SVenu.Iyer@Sun.COM 		 */
3033*11878SVenu.Iyer@Sun.COM 		mac_get_resources((mac_handle_t)mip, mrp);
3034*11878SVenu.Iyer@Sun.COM 		(void) mac_client_set_resources(mch, mrp);
30359473SVenu.Iyer@Sun.COM 		ASSERT(mcip->mci_p_unicast_list != NULL);
30369473SVenu.Iyer@Sun.COM 		muip = mcip->mci_p_unicast_list;
30379473SVenu.Iyer@Sun.COM 		mcip->mci_p_unicast_list = NULL;
30389473SVenu.Iyer@Sun.COM 		if (mac_client_datapath_setup(mcip, VLAN_ID_NONE,
3039*11878SVenu.Iyer@Sun.COM 		    mip->mi_addr, mrp, B_TRUE, muip) == 0) {
30409473SVenu.Iyer@Sun.COM 			if (mcip->mci_rx_p_fn != NULL) {
30419473SVenu.Iyer@Sun.COM 				mac_rx_set(mch, mcip->mci_rx_p_fn,
30429473SVenu.Iyer@Sun.COM 				    mcip->mci_rx_p_arg);
30439473SVenu.Iyer@Sun.COM 				mcip->mci_rx_p_fn = NULL;
30449473SVenu.Iyer@Sun.COM 				mcip->mci_rx_p_arg = NULL;
30459473SVenu.Iyer@Sun.COM 			}
30469822SVenu.Iyer@Sun.COM 		} else {
30479822SVenu.Iyer@Sun.COM 			kmem_free(muip, sizeof (mac_unicast_impl_t));
30489473SVenu.Iyer@Sun.COM 		}
3049*11878SVenu.Iyer@Sun.COM 		kmem_free(mrp, sizeof (*mrp));
30509473SVenu.Iyer@Sun.COM 	}
30518275SEric Cheng 	i_mac_perim_exit(mip);
30528275SEric Cheng 	return (0);
30538275SEric Cheng }
30548275SEric Cheng 
30558275SEric Cheng /*
30568275SEric Cheng  * Multicast add function invoked by MAC clients.
30578275SEric Cheng  */
30588275SEric Cheng int
30598275SEric Cheng mac_multicast_add(mac_client_handle_t mch, const uint8_t *addr)
30608275SEric Cheng {
30618275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
30628275SEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
30638275SEric Cheng 	flow_entry_t		*flent = mcip->mci_flent_list;
30648275SEric Cheng 	flow_entry_t		*prev_fe = NULL;
30658275SEric Cheng 	uint16_t		vid;
30668275SEric Cheng 	int			err = 0;
30678275SEric Cheng 
30688275SEric Cheng 	/* Verify the address is a valid multicast address */
30698275SEric Cheng 	if ((err = mip->mi_type->mt_ops.mtops_multicst_verify(addr,
30708275SEric Cheng 	    mip->mi_pdata)) != 0)
30718275SEric Cheng 		return (err);
30728275SEric Cheng 
30738275SEric Cheng 	i_mac_perim_enter(mip);
30748275SEric Cheng 	while (flent != NULL) {
30758275SEric Cheng 		vid = i_mac_flow_vid(flent);
30768275SEric Cheng 
30778275SEric Cheng 		err = mac_bcast_add((mac_client_impl_t *)mch, addr, vid,
30788275SEric Cheng 		    MAC_ADDRTYPE_MULTICAST);
30798275SEric Cheng 		if (err != 0)
30808275SEric Cheng 			break;
30818275SEric Cheng 		prev_fe = flent;
30828275SEric Cheng 		flent = flent->fe_client_next;
30838275SEric Cheng 	}
30848275SEric Cheng 
30858275SEric Cheng 	/*
30868275SEric Cheng 	 * If we failed adding, then undo all, rather than partial
30878275SEric Cheng 	 * success.
30888275SEric Cheng 	 */
30898275SEric Cheng 	if (flent != NULL && prev_fe != NULL) {
30908275SEric Cheng 		flent = mcip->mci_flent_list;
30918275SEric Cheng 		while (flent != prev_fe->fe_client_next) {
30928275SEric Cheng 			vid = i_mac_flow_vid(flent);
30938275SEric Cheng 			mac_bcast_delete((mac_client_impl_t *)mch, addr, vid);
30948275SEric Cheng 			flent = flent->fe_client_next;
30958275SEric Cheng 		}
30968275SEric Cheng 	}
30978275SEric Cheng 	i_mac_perim_exit(mip);
30988275SEric Cheng 	return (err);
30998275SEric Cheng }
31008275SEric Cheng 
31018275SEric Cheng /*
31028275SEric Cheng  * Multicast delete function invoked by MAC clients.
31038275SEric Cheng  */
31048275SEric Cheng void
31058275SEric Cheng mac_multicast_remove(mac_client_handle_t mch, const uint8_t *addr)
31068275SEric Cheng {
31078275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
31088275SEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
31098275SEric Cheng 	flow_entry_t		*flent;
31108275SEric Cheng 	uint16_t		vid;
31118275SEric Cheng 
31128275SEric Cheng 	i_mac_perim_enter(mip);
31138275SEric Cheng 	for (flent = mcip->mci_flent_list; flent != NULL;
31148275SEric Cheng 	    flent = flent->fe_client_next) {
31158275SEric Cheng 		vid = i_mac_flow_vid(flent);
31168275SEric Cheng 		mac_bcast_delete((mac_client_impl_t *)mch, addr, vid);
31178275SEric Cheng 	}
31188275SEric Cheng 	i_mac_perim_exit(mip);
31198275SEric Cheng }
31208275SEric Cheng 
31218275SEric Cheng /*
31228275SEric Cheng  * When a MAC client desires to capture packets on an interface,
31238275SEric Cheng  * it registers a promiscuous call back with mac_promisc_add().
31248275SEric Cheng  * There are three types of promiscuous callbacks:
31258275SEric Cheng  *
31268275SEric Cheng  * * MAC_CLIENT_PROMISC_ALL
31278275SEric Cheng  *   Captures all packets sent and received by the MAC client,
31288275SEric Cheng  *   the physical interface, as well as all other MAC clients
31298275SEric Cheng  *   defined on top of the same MAC.
31308275SEric Cheng  *
31318275SEric Cheng  * * MAC_CLIENT_PROMISC_FILTERED
31328275SEric Cheng  *   Captures all packets sent and received by the MAC client,
31338275SEric Cheng  *   plus all multicast traffic sent and received by the phyisical
31348275SEric Cheng  *   interface and the other MAC clients.
31358275SEric Cheng  *
31368275SEric Cheng  * * MAC_CLIENT_PROMISC_MULTI
31378275SEric Cheng  *   Captures all broadcast and multicast packets sent and
31388275SEric Cheng  *   received by the MAC clients as well as the physical interface.
31398275SEric Cheng  *
31408275SEric Cheng  * In all cases, the underlying MAC is put in promiscuous mode.
31418275SEric Cheng  */
31428275SEric Cheng int
31438275SEric Cheng mac_promisc_add(mac_client_handle_t mch, mac_client_promisc_type_t type,
31448275SEric Cheng     mac_rx_t fn, void *arg, mac_promisc_handle_t *mphp, uint16_t flags)
31458275SEric Cheng {
31468275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
31478275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
31488275SEric Cheng 	mac_promisc_impl_t *mpip;
31498275SEric Cheng 	mac_cb_info_t	*mcbi;
31508275SEric Cheng 	int rc;
31518275SEric Cheng 
31528275SEric Cheng 	i_mac_perim_enter(mip);
31538275SEric Cheng 
31548893SMichael.Lim@Sun.COM 	if ((rc = mac_start((mac_handle_t)mip)) != 0) {
31558275SEric Cheng 		i_mac_perim_exit(mip);
31568275SEric Cheng 		return (rc);
31578275SEric Cheng 	}
31588275SEric Cheng 
31598275SEric Cheng 	if ((mcip->mci_state_flags & MCIS_IS_VNIC) &&
31608275SEric Cheng 	    type == MAC_CLIENT_PROMISC_ALL) {
31618275SEric Cheng 		/*
31628275SEric Cheng 		 * The function is being invoked by the upper MAC client
31638275SEric Cheng 		 * of a VNIC. The VNIC should only see the traffic
31648275SEric Cheng 		 * it is entitled to.
31658275SEric Cheng 		 */
31668275SEric Cheng 		type = MAC_CLIENT_PROMISC_FILTERED;
31678275SEric Cheng 	}
31688275SEric Cheng 
31698275SEric Cheng 
31708275SEric Cheng 	/*
31718275SEric Cheng 	 * Turn on promiscuous mode for the underlying NIC.
31728275SEric Cheng 	 * This is needed even for filtered callbacks which
31738275SEric Cheng 	 * expect to receive all multicast traffic on the wire.
31748275SEric Cheng 	 *
31758275SEric Cheng 	 * Physical promiscuous mode should not be turned on if
31768275SEric Cheng 	 * MAC_PROMISC_FLAGS_NO_PHYS is set.
31778275SEric Cheng 	 */
31788275SEric Cheng 	if ((flags & MAC_PROMISC_FLAGS_NO_PHYS) == 0) {
31799641SGirish.Moodalbail@Sun.COM 		if ((rc = i_mac_promisc_set(mip, B_TRUE)) != 0) {
31808893SMichael.Lim@Sun.COM 			mac_stop((mac_handle_t)mip);
31818275SEric Cheng 			i_mac_perim_exit(mip);
31828275SEric Cheng 			return (rc);
31838275SEric Cheng 		}
31848275SEric Cheng 	}
31858275SEric Cheng 
31868275SEric Cheng 	mpip = kmem_cache_alloc(mac_promisc_impl_cache, KM_SLEEP);
31878275SEric Cheng 
31888275SEric Cheng 	mpip->mpi_type = type;
31898275SEric Cheng 	mpip->mpi_fn = fn;
31908275SEric Cheng 	mpip->mpi_arg = arg;
31918275SEric Cheng 	mpip->mpi_mcip = mcip;
31928275SEric Cheng 	mpip->mpi_no_tx_loop = ((flags & MAC_PROMISC_FLAGS_NO_TX_LOOP) != 0);
31938275SEric Cheng 	mpip->mpi_no_phys = ((flags & MAC_PROMISC_FLAGS_NO_PHYS) != 0);
31948833SVenu.Iyer@Sun.COM 	mpip->mpi_strip_vlan_tag =
31958833SVenu.Iyer@Sun.COM 	    ((flags & MAC_PROMISC_FLAGS_VLAN_TAG_STRIP) != 0);
319610639SDarren.Reed@Sun.COM 	mpip->mpi_no_copy = ((flags & MAC_PROMISC_FLAGS_NO_COPY) != 0);
31978275SEric Cheng 
31988275SEric Cheng 	mcbi = &mip->mi_promisc_cb_info;
31998275SEric Cheng 	mutex_enter(mcbi->mcbi_lockp);
32008275SEric Cheng 
32018275SEric Cheng 	mac_callback_add(&mip->mi_promisc_cb_info, &mcip->mci_promisc_list,
32028275SEric Cheng 	    &mpip->mpi_mci_link);
32038275SEric Cheng 	mac_callback_add(&mip->mi_promisc_cb_info, &mip->mi_promisc_list,
32048275SEric Cheng 	    &mpip->mpi_mi_link);
32058275SEric Cheng 
32068275SEric Cheng 	mutex_exit(mcbi->mcbi_lockp);
32078275SEric Cheng 
32088275SEric Cheng 	*mphp = (mac_promisc_handle_t)mpip;
32098275SEric Cheng 	i_mac_perim_exit(mip);
32108275SEric Cheng 	return (0);
32118275SEric Cheng }
32128275SEric Cheng 
32138275SEric Cheng /*
32148275SEric Cheng  * Remove a multicast address previously aded through mac_promisc_add().
32158275SEric Cheng  */
32169044SGirish.Moodalbail@Sun.COM void
32178275SEric Cheng mac_promisc_remove(mac_promisc_handle_t mph)
32188275SEric Cheng {
32198275SEric Cheng 	mac_promisc_impl_t *mpip = (mac_promisc_impl_t *)mph;
32208275SEric Cheng 	mac_client_impl_t *mcip = mpip->mpi_mcip;
32218275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
32228275SEric Cheng 	mac_cb_info_t *mcbi;
32239641SGirish.Moodalbail@Sun.COM 	int rv;
32248275SEric Cheng 
32258275SEric Cheng 	i_mac_perim_enter(mip);
32268275SEric Cheng 
32278275SEric Cheng 	/*
32288275SEric Cheng 	 * Even if the device can't be reset into normal mode, we still
32298275SEric Cheng 	 * need to clear the client promisc callbacks. The client may want
32308275SEric Cheng 	 * to close the mac end point and we can't have stale callbacks.
32318275SEric Cheng 	 */
32328275SEric Cheng 	if (!(mpip->mpi_no_phys)) {
32339641SGirish.Moodalbail@Sun.COM 		if ((rv = i_mac_promisc_set(mip, B_FALSE)) != 0) {
32349641SGirish.Moodalbail@Sun.COM 			cmn_err(CE_WARN, "%s: failed to switch OFF promiscuous"
32359641SGirish.Moodalbail@Sun.COM 			    " mode because of error 0x%x", mip->mi_name, rv);
32369641SGirish.Moodalbail@Sun.COM 		}
32378275SEric Cheng 	}
32388275SEric Cheng 	mcbi = &mip->mi_promisc_cb_info;
32398275SEric Cheng 	mutex_enter(mcbi->mcbi_lockp);
32408275SEric Cheng 	if (mac_callback_remove(mcbi, &mip->mi_promisc_list,
32418275SEric Cheng 	    &mpip->mpi_mi_link)) {
32428275SEric Cheng 		VERIFY(mac_callback_remove(&mip->mi_promisc_cb_info,
32438275SEric Cheng 		    &mcip->mci_promisc_list, &mpip->mpi_mci_link));
32448275SEric Cheng 		kmem_cache_free(mac_promisc_impl_cache, mpip);
32458275SEric Cheng 	} else {
32468275SEric Cheng 		mac_callback_remove_wait(&mip->mi_promisc_cb_info);
32478275SEric Cheng 	}
32488275SEric Cheng 	mutex_exit(mcbi->mcbi_lockp);
32498893SMichael.Lim@Sun.COM 	mac_stop((mac_handle_t)mip);
32508275SEric Cheng 
32518275SEric Cheng 	i_mac_perim_exit(mip);
32528275SEric Cheng }
32538275SEric Cheng 
32548275SEric Cheng /*
32558275SEric Cheng  * Reference count the number of active Tx threads. MCI_TX_QUIESCE indicates
32568275SEric Cheng  * that a control operation wants to quiesce the Tx data flow in which case
32578275SEric Cheng  * we return an error. Holding any of the per cpu locks ensures that the
32588275SEric Cheng  * mci_tx_flag won't change.
32598275SEric Cheng  *
32608275SEric Cheng  * 'CPU' must be accessed just once and used to compute the index into the
32618275SEric Cheng  * percpu array, and that index must be used for the entire duration of the
32628275SEric Cheng  * packet send operation. Note that the thread may be preempted and run on
32638275SEric Cheng  * another cpu any time and so we can't use 'CPU' more than once for the
32648275SEric Cheng  * operation.
32658275SEric Cheng  */
32668275SEric Cheng #define	MAC_TX_TRY_HOLD(mcip, mytx, error)				\
32678275SEric Cheng {									\
32688275SEric Cheng 	(error) = 0;							\
32698275SEric Cheng 	(mytx) = &(mcip)->mci_tx_pcpu[CPU->cpu_seqid & mac_tx_percpu_cnt]; \
32708275SEric Cheng 	mutex_enter(&(mytx)->pcpu_tx_lock);				\
32718275SEric Cheng 	if (!((mcip)->mci_tx_flag & MCI_TX_QUIESCE)) {			\
32728275SEric Cheng 		(mytx)->pcpu_tx_refcnt++;				\
32738275SEric Cheng 	} else {							\
32748275SEric Cheng 		(error) = -1;						\
32758275SEric Cheng 	}								\
32768275SEric Cheng 	mutex_exit(&(mytx)->pcpu_tx_lock);				\
32778275SEric Cheng }
32788275SEric Cheng 
32798275SEric Cheng /*
32808275SEric Cheng  * Release the reference. If needed, signal any control operation waiting
32818275SEric Cheng  * for Tx quiescence. The wait and signal are always done using the
32828275SEric Cheng  * mci_tx_pcpu[0]'s lock
32838275SEric Cheng  */
32848275SEric Cheng #define	MAC_TX_RELE(mcip, mytx) {					\
32858275SEric Cheng 	mutex_enter(&(mytx)->pcpu_tx_lock);				\
32868275SEric Cheng 	if (--(mytx)->pcpu_tx_refcnt == 0 &&				\
32878275SEric Cheng 	    (mcip)->mci_tx_flag & MCI_TX_QUIESCE) {			\
32888275SEric Cheng 		mutex_exit(&(mytx)->pcpu_tx_lock);			\
32898275SEric Cheng 		mutex_enter(&(mcip)->mci_tx_pcpu[0].pcpu_tx_lock);	\
32908275SEric Cheng 		cv_signal(&(mcip)->mci_tx_cv);				\
32918275SEric Cheng 		mutex_exit(&(mcip)->mci_tx_pcpu[0].pcpu_tx_lock);	\
32928275SEric Cheng 	} else {							\
32938275SEric Cheng 		mutex_exit(&(mytx)->pcpu_tx_lock);			\
32948275SEric Cheng 	}								\
32958275SEric Cheng }
32968275SEric Cheng 
32978275SEric Cheng /*
32988275SEric Cheng  * Send function invoked by MAC clients.
32998275SEric Cheng  */
33008275SEric Cheng mac_tx_cookie_t
33018275SEric Cheng mac_tx(mac_client_handle_t mch, mblk_t *mp_chain, uintptr_t hint,
33028275SEric Cheng     uint16_t flag, mblk_t **ret_mp)
33038275SEric Cheng {
330410734SEric Cheng 	mac_tx_cookie_t		cookie = NULL;
33058275SEric Cheng 	int			error;
33068275SEric Cheng 	mac_tx_percpu_t		*mytx;
33078275SEric Cheng 	mac_soft_ring_set_t	*srs;
33088275SEric Cheng 	flow_entry_t		*flent;
33098275SEric Cheng 	boolean_t		is_subflow = B_FALSE;
33108275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
33118275SEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
33128275SEric Cheng 	mac_srs_tx_t		*srs_tx;
33138275SEric Cheng 
33148275SEric Cheng 	/*
33158275SEric Cheng 	 * Check whether the active Tx threads count is bumped already.
33168275SEric Cheng 	 */
33178275SEric Cheng 	if (!(flag & MAC_TX_NO_HOLD)) {
33188275SEric Cheng 		MAC_TX_TRY_HOLD(mcip, mytx, error);
33198275SEric Cheng 		if (error != 0) {
33208275SEric Cheng 			freemsgchain(mp_chain);
33218275SEric Cheng 			return (NULL);
33228275SEric Cheng 		}
33238275SEric Cheng 	}
33248275SEric Cheng 
332510734SEric Cheng 	/*
332610734SEric Cheng 	 * If mac protection is enabled, only the permissible packets will be
332710734SEric Cheng 	 * returned by mac_protect_check().
332810734SEric Cheng 	 */
332910734SEric Cheng 	if ((mcip->mci_flent->
333010734SEric Cheng 	    fe_resource_props.mrp_mask & MRP_PROTECT) != 0 &&
333110734SEric Cheng 	    (mp_chain = mac_protect_check(mch, mp_chain)) == NULL)
333210734SEric Cheng 		goto done;
333310734SEric Cheng 
33348275SEric Cheng 	if (mcip->mci_subflow_tab != NULL &&
33358275SEric Cheng 	    mcip->mci_subflow_tab->ft_flow_count > 0 &&
33368275SEric Cheng 	    mac_flow_lookup(mcip->mci_subflow_tab, mp_chain,
33378275SEric Cheng 	    FLOW_OUTBOUND, &flent) == 0) {
33388275SEric Cheng 		/*
33398275SEric Cheng 		 * The main assumption here is that if in the event
33408275SEric Cheng 		 * we get a chain, all the packets will be classified
33418275SEric Cheng 		 * to the same Flow/SRS. If this changes for any
33428275SEric Cheng 		 * reason, the following logic should change as well.
33438275SEric Cheng 		 * I suppose the fanout_hint also assumes this .
33448275SEric Cheng 		 */
33458275SEric Cheng 		ASSERT(flent != NULL);
33468275SEric Cheng 		is_subflow = B_TRUE;
33478275SEric Cheng 	} else {
33488275SEric Cheng 		flent = mcip->mci_flent;
33498275SEric Cheng 	}
33508275SEric Cheng 
33518275SEric Cheng 	srs = flent->fe_tx_srs;
335210639SDarren.Reed@Sun.COM 	/*
335310639SDarren.Reed@Sun.COM 	 * This is to avoid panics with PF_PACKET that can call mac_tx()
335410639SDarren.Reed@Sun.COM 	 * against an interface that is not capable of sending. A rewrite
335510639SDarren.Reed@Sun.COM 	 * of the mac datapath is required to remove this limitation.
335610639SDarren.Reed@Sun.COM 	 */
335710639SDarren.Reed@Sun.COM 	if (srs == NULL) {
335810639SDarren.Reed@Sun.COM 		freemsgchain(mp_chain);
335910734SEric Cheng 		goto done;
336010639SDarren.Reed@Sun.COM 	}
336110734SEric Cheng 
33628275SEric Cheng 	srs_tx = &srs->srs_tx;
33638275SEric Cheng 	if (srs_tx->st_mode == SRS_TX_DEFAULT &&
33648275SEric Cheng 	    (srs->srs_state & SRS_ENQUEUED) == 0 &&
3365*11878SVenu.Iyer@Sun.COM 	    mip->mi_nactiveclients == 1 && mp_chain->b_next == NULL) {
33668275SEric Cheng 		uint64_t	obytes;
33678275SEric Cheng 
33688275SEric Cheng 		/*
33698275SEric Cheng 		 * Since dls always opens the underlying MAC, nclients equals
33708275SEric Cheng 		 * to 1 means that the only active client is dls itself acting
33718275SEric Cheng 		 * as a primary client of the MAC instance. Since dls will not
33728275SEric Cheng 		 * send tagged packets in that case, and dls is trusted to send
33738275SEric Cheng 		 * packets for its allowed VLAN(s), the VLAN tag insertion and
33748275SEric Cheng 		 * check is required only if nclients is greater than 1.
33758275SEric Cheng 		 */
33768275SEric Cheng 		if (mip->mi_nclients > 1) {
33778275SEric Cheng 			if (MAC_VID_CHECK_NEEDED(mcip)) {
33788275SEric Cheng 				int	err = 0;
33798275SEric Cheng 
33808275SEric Cheng 				MAC_VID_CHECK(mcip, mp_chain, err);
33818275SEric Cheng 				if (err != 0) {
33828275SEric Cheng 					freemsg(mp_chain);
3383*11878SVenu.Iyer@Sun.COM 					mcip->mci_misc_stat.mms_txerrors++;
33848275SEric Cheng 					goto done;
33858275SEric Cheng 				}
33868275SEric Cheng 			}
33878275SEric Cheng 			if (MAC_TAG_NEEDED(mcip)) {
33888275SEric Cheng 				mp_chain = mac_add_vlan_tag(mp_chain, 0,
33898275SEric Cheng 				    mac_client_vid(mch));
33908275SEric Cheng 				if (mp_chain == NULL) {
3391*11878SVenu.Iyer@Sun.COM 					mcip->mci_misc_stat.mms_txerrors++;
33928275SEric Cheng 					goto done;
33938275SEric Cheng 				}
33948275SEric Cheng 			}
33958275SEric Cheng 		}
33968275SEric Cheng 
33978275SEric Cheng 		obytes = (mp_chain->b_cont == NULL ? MBLKL(mp_chain) :
33988275SEric Cheng 		    msgdsize(mp_chain));
33998275SEric Cheng 
3400*11878SVenu.Iyer@Sun.COM 		MAC_TX(mip, srs_tx->st_arg2, mp_chain, mcip);
34018275SEric Cheng 		if (mp_chain == NULL) {
34028275SEric Cheng 			cookie = NULL;
3403*11878SVenu.Iyer@Sun.COM 			SRS_TX_STAT_UPDATE(srs, opackets, 1);
3404*11878SVenu.Iyer@Sun.COM 			SRS_TX_STAT_UPDATE(srs, obytes, obytes);
34058275SEric Cheng 		} else {
34068275SEric Cheng 			mutex_enter(&srs->srs_lock);
34078275SEric Cheng 			cookie = mac_tx_srs_no_desc(srs, mp_chain,
34088275SEric Cheng 			    flag, ret_mp);
34098275SEric Cheng 			mutex_exit(&srs->srs_lock);
34108275SEric Cheng 		}
34118275SEric Cheng 	} else {
34128275SEric Cheng 		cookie = srs_tx->st_func(srs, mp_chain, hint, flag, ret_mp);
34138275SEric Cheng 	}
34148275SEric Cheng 
34158275SEric Cheng done:
34168275SEric Cheng 	if (is_subflow)
34178275SEric Cheng 		FLOW_REFRELE(flent);
34188275SEric Cheng 
34198275SEric Cheng 	if (!(flag & MAC_TX_NO_HOLD))
34208275SEric Cheng 		MAC_TX_RELE(mcip, mytx);
34218275SEric Cheng 
34228275SEric Cheng 	return (cookie);
34238275SEric Cheng }
34248275SEric Cheng 
34258275SEric Cheng /*
34268275SEric Cheng  * mac_tx_is_blocked
34278275SEric Cheng  *
34288275SEric Cheng  * Given a cookie, it returns if the ring identified by the cookie is
34298833SVenu.Iyer@Sun.COM  * flow-controlled or not. If NULL is passed in place of a cookie,
34308833SVenu.Iyer@Sun.COM  * then it finds out if any of the underlying rings belonging to the
34318833SVenu.Iyer@Sun.COM  * SRS is flow controlled or not and returns that status.
34328275SEric Cheng  */
34338275SEric Cheng /* ARGSUSED */
34348275SEric Cheng boolean_t
34358275SEric Cheng mac_tx_is_flow_blocked(mac_client_handle_t mch, mac_tx_cookie_t cookie)
34368275SEric Cheng {
34378275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
34388833SVenu.Iyer@Sun.COM 	mac_soft_ring_set_t *mac_srs;
34398275SEric Cheng 	mac_soft_ring_t *sringp;
34408275SEric Cheng 	boolean_t blocked = B_FALSE;
34418833SVenu.Iyer@Sun.COM 	mac_tx_percpu_t *mytx;
34428833SVenu.Iyer@Sun.COM 	int err;
34438275SEric Cheng 	int i;
34448275SEric Cheng 
34458275SEric Cheng 	/*
34468833SVenu.Iyer@Sun.COM 	 * Bump the reference count so that mac_srs won't be deleted.
34478833SVenu.Iyer@Sun.COM 	 * If the client is currently quiesced and we failed to bump
34488833SVenu.Iyer@Sun.COM 	 * the reference, return B_TRUE so that flow control stays
34498833SVenu.Iyer@Sun.COM 	 * as enabled.
34508833SVenu.Iyer@Sun.COM 	 *
34518833SVenu.Iyer@Sun.COM 	 * Flow control will then be disabled once the client is no
34528833SVenu.Iyer@Sun.COM 	 * longer quiesced.
34538275SEric Cheng 	 */
34548833SVenu.Iyer@Sun.COM 	MAC_TX_TRY_HOLD(mcip, mytx, err);
34558833SVenu.Iyer@Sun.COM 	if (err != 0)
34568833SVenu.Iyer@Sun.COM 		return (B_TRUE);
34578833SVenu.Iyer@Sun.COM 
34588833SVenu.Iyer@Sun.COM 	if ((mac_srs = MCIP_TX_SRS(mcip)) == NULL) {
34598833SVenu.Iyer@Sun.COM 		MAC_TX_RELE(mcip, mytx);
34608275SEric Cheng 		return (B_FALSE);
34618833SVenu.Iyer@Sun.COM 	}
34628275SEric Cheng 
34638275SEric Cheng 	mutex_enter(&mac_srs->srs_lock);
3464*11878SVenu.Iyer@Sun.COM 	/*
3465*11878SVenu.Iyer@Sun.COM 	 * Only in the case of TX_FANOUT and TX_AGGR, the underlying
3466*11878SVenu.Iyer@Sun.COM 	 * softring (s_ring_state) will have the HIWAT set. This is
3467*11878SVenu.Iyer@Sun.COM 	 * the multiple Tx ring flow control case. For all other
3468*11878SVenu.Iyer@Sun.COM 	 * case, SRS (srs_state) will store the condition.
3469*11878SVenu.Iyer@Sun.COM 	 */
3470*11878SVenu.Iyer@Sun.COM 	if (mac_srs->srs_tx.st_mode == SRS_TX_FANOUT ||
3471*11878SVenu.Iyer@Sun.COM 	    mac_srs->srs_tx.st_mode == SRS_TX_AGGR) {
34728833SVenu.Iyer@Sun.COM 		if (cookie != NULL) {
34738833SVenu.Iyer@Sun.COM 			sringp = (mac_soft_ring_t *)cookie;
34748275SEric Cheng 			mutex_enter(&sringp->s_ring_lock);
34758833SVenu.Iyer@Sun.COM 			if (sringp->s_ring_state & S_RING_TX_HIWAT)
34768275SEric Cheng 				blocked = B_TRUE;
34778833SVenu.Iyer@Sun.COM 			mutex_exit(&sringp->s_ring_lock);
34788833SVenu.Iyer@Sun.COM 		} else {
3479*11878SVenu.Iyer@Sun.COM 			for (i = 0; i < mac_srs->srs_tx_ring_count; i++) {
3480*11878SVenu.Iyer@Sun.COM 				sringp = mac_srs->srs_tx_soft_rings[i];
34818833SVenu.Iyer@Sun.COM 				mutex_enter(&sringp->s_ring_lock);
34828833SVenu.Iyer@Sun.COM 				if (sringp->s_ring_state & S_RING_TX_HIWAT) {
34838833SVenu.Iyer@Sun.COM 					blocked = B_TRUE;
34848833SVenu.Iyer@Sun.COM 					mutex_exit(&sringp->s_ring_lock);
34858833SVenu.Iyer@Sun.COM 					break;
34868833SVenu.Iyer@Sun.COM 				}
34878275SEric Cheng 				mutex_exit(&sringp->s_ring_lock);
34888275SEric Cheng 			}
34898275SEric Cheng 		}
34908275SEric Cheng 	} else {
34918275SEric Cheng 		blocked = (mac_srs->srs_state & SRS_TX_HIWAT);
34928275SEric Cheng 	}
34938275SEric Cheng 	mutex_exit(&mac_srs->srs_lock);
34948833SVenu.Iyer@Sun.COM 	MAC_TX_RELE(mcip, mytx);
34958275SEric Cheng 	return (blocked);
34968275SEric Cheng }
34978275SEric Cheng 
34988275SEric Cheng /*
34998275SEric Cheng  * Check if the MAC client is the primary MAC client.
35008275SEric Cheng  */
35018275SEric Cheng boolean_t
35028275SEric Cheng mac_is_primary_client(mac_client_impl_t *mcip)
35038275SEric Cheng {
35048275SEric Cheng 	return (mcip->mci_flags & MAC_CLIENT_FLAGS_PRIMARY);
35058275SEric Cheng }
35068275SEric Cheng 
35078275SEric Cheng void
35088275SEric Cheng mac_ioctl(mac_handle_t mh, queue_t *wq, mblk_t *bp)
35098275SEric Cheng {
35108275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
35118275SEric Cheng 	int cmd = ((struct iocblk *)bp->b_rptr)->ioc_cmd;
35128275SEric Cheng 
35138275SEric Cheng 	if ((cmd == ND_GET && (mip->mi_callbacks->mc_callbacks & MC_GETPROP)) ||
35148275SEric Cheng 	    (cmd == ND_SET && (mip->mi_callbacks->mc_callbacks & MC_SETPROP))) {
35158275SEric Cheng 		/*
35168275SEric Cheng 		 * If ndd props were registered, call them.
35178275SEric Cheng 		 * Note that ndd ioctls are Obsolete
35188275SEric Cheng 		 */
35198275SEric Cheng 		mac_ndd_ioctl(mip, wq, bp);
35208275SEric Cheng 		return;
35218275SEric Cheng 	}
35228275SEric Cheng 
35238275SEric Cheng 	/*
35248275SEric Cheng 	 * Call the driver to handle the ioctl.  The driver may not support
35258275SEric Cheng 	 * any ioctls, in which case we reply with a NAK on its behalf.
35268275SEric Cheng 	 */
35278275SEric Cheng 	if (mip->mi_callbacks->mc_callbacks & MC_IOCTL)
35288275SEric Cheng 		mip->mi_ioctl(mip->mi_driver, wq, bp);
35298275SEric Cheng 	else
35308275SEric Cheng 		miocnak(wq, bp, 0, EINVAL);
35318275SEric Cheng }
35328275SEric Cheng 
35338275SEric Cheng /*
35348275SEric Cheng  * Return the link state of the specified MAC instance.
35358275SEric Cheng  */
35368275SEric Cheng link_state_t
35378275SEric Cheng mac_link_get(mac_handle_t mh)
35388275SEric Cheng {
35398275SEric Cheng 	return (((mac_impl_t *)mh)->mi_linkstate);
35408275SEric Cheng }
35418275SEric Cheng 
35428275SEric Cheng /*
35438275SEric Cheng  * Add a mac client specified notification callback. Please see the comments
35448275SEric Cheng  * above mac_callback_add() for general information about mac callback
35458275SEric Cheng  * addition/deletion in the presence of mac callback list walkers
35468275SEric Cheng  */
35478275SEric Cheng mac_notify_handle_t
35488275SEric Cheng mac_notify_add(mac_handle_t mh, mac_notify_t notify_fn, void *arg)
35498275SEric Cheng {
35508275SEric Cheng 	mac_impl_t		*mip = (mac_impl_t *)mh;
35518275SEric Cheng 	mac_notify_cb_t		*mncb;
35528275SEric Cheng 	mac_cb_info_t		*mcbi;
35538275SEric Cheng 
35548275SEric Cheng 	/*
35558275SEric Cheng 	 * Allocate a notify callback structure, fill in the details and
35568275SEric Cheng 	 * use the mac callback list manipulation functions to chain into
35578275SEric Cheng 	 * the list of callbacks.
35588275SEric Cheng 	 */
35598275SEric Cheng 	mncb = kmem_zalloc(sizeof (mac_notify_cb_t), KM_SLEEP);
35608275SEric Cheng 	mncb->mncb_fn = notify_fn;
35618275SEric Cheng 	mncb->mncb_arg = arg;
35628275SEric Cheng 	mncb->mncb_mip = mip;
35638275SEric Cheng 	mncb->mncb_link.mcb_objp = mncb;
35648275SEric Cheng 	mncb->mncb_link.mcb_objsize = sizeof (mac_notify_cb_t);
35658275SEric Cheng 	mncb->mncb_link.mcb_flags = MCB_NOTIFY_CB_T;
35668275SEric Cheng 
35678275SEric Cheng 	mcbi = &mip->mi_notify_cb_info;
35688275SEric Cheng 
35698275SEric Cheng 	i_mac_perim_enter(mip);
35708275SEric Cheng 	mutex_enter(mcbi->mcbi_lockp);
35718275SEric Cheng 
35728275SEric Cheng 	mac_callback_add(&mip->mi_notify_cb_info, &mip->mi_notify_cb_list,
35738275SEric Cheng 	    &mncb->mncb_link);
35748275SEric Cheng 
35758275SEric Cheng 	mutex_exit(mcbi->mcbi_lockp);
35768275SEric Cheng 	i_mac_perim_exit(mip);
35778275SEric Cheng 	return ((mac_notify_handle_t)mncb);
35788275SEric Cheng }
35798275SEric Cheng 
35808275SEric Cheng void
35818275SEric Cheng mac_notify_remove_wait(mac_handle_t mh)
35828275SEric Cheng {
35838275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
35848275SEric Cheng 	mac_cb_info_t	*mcbi = &mip->mi_notify_cb_info;
35858275SEric Cheng 
35868275SEric Cheng 	mutex_enter(mcbi->mcbi_lockp);
35878275SEric Cheng 	mac_callback_remove_wait(&mip->mi_notify_cb_info);
35888275SEric Cheng 	mutex_exit(mcbi->mcbi_lockp);
35898275SEric Cheng }
35908275SEric Cheng 
35918275SEric Cheng /*
35928275SEric Cheng  * Remove a mac client specified notification callback
35938275SEric Cheng  */
35948275SEric Cheng int
35958275SEric Cheng mac_notify_remove(mac_notify_handle_t mnh, boolean_t wait)
35968275SEric Cheng {
35978275SEric Cheng 	mac_notify_cb_t	*mncb = (mac_notify_cb_t *)mnh;
35988275SEric Cheng 	mac_impl_t	*mip = mncb->mncb_mip;
35998275SEric Cheng 	mac_cb_info_t	*mcbi;
36008275SEric Cheng 	int		err = 0;
36018275SEric Cheng 
36028275SEric Cheng 	mcbi = &mip->mi_notify_cb_info;
36038275SEric Cheng 
36048275SEric Cheng 	i_mac_perim_enter(mip);
36058275SEric Cheng 	mutex_enter(mcbi->mcbi_lockp);
36068275SEric Cheng 
36078275SEric Cheng 	ASSERT(mncb->mncb_link.mcb_objp == mncb);
36088275SEric Cheng 	/*
36098275SEric Cheng 	 * If there aren't any list walkers, the remove would succeed
36108275SEric Cheng 	 * inline, else we wait for the deferred remove to complete
36118275SEric Cheng 	 */
36128275SEric Cheng 	if (mac_callback_remove(&mip->mi_notify_cb_info,
36138275SEric Cheng 	    &mip->mi_notify_cb_list, &mncb->mncb_link)) {
36148275SEric Cheng 		kmem_free(mncb, sizeof (mac_notify_cb_t));
36158275SEric Cheng 	} else {
36168275SEric Cheng 		err = EBUSY;
36178275SEric Cheng 	}
36188275SEric Cheng 
36198275SEric Cheng 	mutex_exit(mcbi->mcbi_lockp);
36208275SEric Cheng 	i_mac_perim_exit(mip);
36218275SEric Cheng 
36228275SEric Cheng 	/*
36238275SEric Cheng 	 * If we failed to remove the notification callback and "wait" is set
36248275SEric Cheng 	 * to be B_TRUE, wait for the callback to finish after we exit the
36258275SEric Cheng 	 * mac perimeter.
36268275SEric Cheng 	 */
36278275SEric Cheng 	if (err != 0 && wait) {
36288275SEric Cheng 		mac_notify_remove_wait((mac_handle_t)mip);
36298275SEric Cheng 		return (0);
36308275SEric Cheng 	}
36318275SEric Cheng 
36328275SEric Cheng 	return (err);
36338275SEric Cheng }
36348275SEric Cheng 
36358275SEric Cheng /*
36368275SEric Cheng  * Associate resource management callbacks with the specified MAC
36378275SEric Cheng  * clients.
36388275SEric Cheng  */
36398275SEric Cheng 
36408275SEric Cheng void
36418275SEric Cheng mac_resource_set_common(mac_client_handle_t mch, mac_resource_add_t add,
36428275SEric Cheng     mac_resource_remove_t remove, mac_resource_quiesce_t quiesce,
36438275SEric Cheng     mac_resource_restart_t restart, mac_resource_bind_t bind,
36448275SEric Cheng     void *arg)
36458275SEric Cheng {
36468275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
36478275SEric Cheng 
36488275SEric Cheng 	mcip->mci_resource_add = add;
36498275SEric Cheng 	mcip->mci_resource_remove = remove;
36508275SEric Cheng 	mcip->mci_resource_quiesce = quiesce;
36518275SEric Cheng 	mcip->mci_resource_restart = restart;
36528275SEric Cheng 	mcip->mci_resource_bind = bind;
36538275SEric Cheng 	mcip->mci_resource_arg = arg;
36548275SEric Cheng }
36558275SEric Cheng 
36568275SEric Cheng void
36578275SEric Cheng mac_resource_set(mac_client_handle_t mch, mac_resource_add_t add, void *arg)
36588275SEric Cheng {
36598275SEric Cheng 	/* update the 'resource_add' callback */
36608275SEric Cheng 	mac_resource_set_common(mch, add, NULL, NULL, NULL, NULL, arg);
36618275SEric Cheng }
36628275SEric Cheng 
36638275SEric Cheng /*
36648275SEric Cheng  * Sets up the client resources and enable the polling interface over all the
36658275SEric Cheng  * SRS's and the soft rings of the client
36668275SEric Cheng  */
36678275SEric Cheng void
36688275SEric Cheng mac_client_poll_enable(mac_client_handle_t mch)
36698275SEric Cheng {
36708275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
36718275SEric Cheng 	mac_soft_ring_set_t	*mac_srs;
36728275SEric Cheng 	flow_entry_t		*flent;
36738275SEric Cheng 	int			i;
36748275SEric Cheng 
36758275SEric Cheng 	flent = mcip->mci_flent;
36768275SEric Cheng 	ASSERT(flent != NULL);
36778275SEric Cheng 
367811021SEric.Cheng@Sun.COM 	mcip->mci_state_flags |= MCIS_CLIENT_POLL_CAPABLE;
36798275SEric Cheng 	for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
36808275SEric Cheng 		mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
36818275SEric Cheng 		ASSERT(mac_srs->srs_mcip == mcip);
36828275SEric Cheng 		mac_srs_client_poll_enable(mcip, mac_srs);
36838275SEric Cheng 	}
36848275SEric Cheng }
36858275SEric Cheng 
36868275SEric Cheng /*
36878275SEric Cheng  * Tears down the client resources and disable the polling interface over all
36888275SEric Cheng  * the SRS's and the soft rings of the client
36898275SEric Cheng  */
36908275SEric Cheng void
36918275SEric Cheng mac_client_poll_disable(mac_client_handle_t mch)
36928275SEric Cheng {
36938275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
36948275SEric Cheng 	mac_soft_ring_set_t	*mac_srs;
36958275SEric Cheng 	flow_entry_t		*flent;
36968275SEric Cheng 	int			i;
36978275SEric Cheng 
36988275SEric Cheng 	flent = mcip->mci_flent;
36998275SEric Cheng 	ASSERT(flent != NULL);
37008275SEric Cheng 
370111021SEric.Cheng@Sun.COM 	mcip->mci_state_flags &= ~MCIS_CLIENT_POLL_CAPABLE;
37028275SEric Cheng 	for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
37038275SEric Cheng 		mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
37048275SEric Cheng 		ASSERT(mac_srs->srs_mcip == mcip);
37058275SEric Cheng 		mac_srs_client_poll_disable(mcip, mac_srs);
37068275SEric Cheng 	}
37078275SEric Cheng }
37088275SEric Cheng 
37098275SEric Cheng /*
37108275SEric Cheng  * Associate the CPUs specified by the given property with a MAC client.
37118275SEric Cheng  */
37128275SEric Cheng int
37138275SEric Cheng mac_cpu_set(mac_client_handle_t mch, mac_resource_props_t *mrp)
37148275SEric Cheng {
37158275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
37168275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
37178275SEric Cheng 	int err = 0;
37188275SEric Cheng 
37198275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
37208275SEric Cheng 
3721*11878SVenu.Iyer@Sun.COM 	if ((err = mac_validate_props(mcip->mci_state_flags & MCIS_IS_VNIC ?
3722*11878SVenu.Iyer@Sun.COM 	    mcip->mci_upper_mip : mip, mrp)) != 0) {
37238275SEric Cheng 		return (err);
3724*11878SVenu.Iyer@Sun.COM 	}
37258275SEric Cheng 	if (MCIP_DATAPATH_SETUP(mcip))
37268275SEric Cheng 		mac_flow_modify(mip->mi_flow_tab, mcip->mci_flent, mrp);
37278275SEric Cheng 
37288275SEric Cheng 	mac_update_resources(mrp, MCIP_RESOURCE_PROPS(mcip), B_FALSE);
37298275SEric Cheng 	return (0);
37308275SEric Cheng }
37318275SEric Cheng 
37328275SEric Cheng /*
37338275SEric Cheng  * Apply the specified properties to the specified MAC client.
37348275SEric Cheng  */
37358275SEric Cheng int
37368275SEric Cheng mac_client_set_resources(mac_client_handle_t mch, mac_resource_props_t *mrp)
37378275SEric Cheng {
37388275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
37398275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
37408275SEric Cheng 	int err = 0;
37418275SEric Cheng 
37428275SEric Cheng 	i_mac_perim_enter(mip);
37438275SEric Cheng 
37448275SEric Cheng 	if ((mrp->mrp_mask & MRP_MAXBW) || (mrp->mrp_mask & MRP_PRIORITY)) {
37458275SEric Cheng 		err = mac_resource_ctl_set(mch, mrp);
374610734SEric Cheng 		if (err != 0)
374710734SEric Cheng 			goto done;
37488275SEric Cheng 	}
37498275SEric Cheng 
3750*11878SVenu.Iyer@Sun.COM 	if (mrp->mrp_mask & (MRP_CPUS|MRP_POOL)) {
37518275SEric Cheng 		err = mac_cpu_set(mch, mrp);
375210734SEric Cheng 		if (err != 0)
375310734SEric Cheng 			goto done;
375410734SEric Cheng 	}
375510734SEric Cheng 
3756*11878SVenu.Iyer@Sun.COM 	if (mrp->mrp_mask & MRP_PROTECT) {
375710734SEric Cheng 		err = mac_protect_set(mch, mrp);
3758*11878SVenu.Iyer@Sun.COM 		if (err != 0)
3759*11878SVenu.Iyer@Sun.COM 			goto done;
3760*11878SVenu.Iyer@Sun.COM 	}
3761*11878SVenu.Iyer@Sun.COM 
3762*11878SVenu.Iyer@Sun.COM 	if ((mrp->mrp_mask & MRP_RX_RINGS) || (mrp->mrp_mask & MRP_TX_RINGS))
3763*11878SVenu.Iyer@Sun.COM 		err = mac_resource_ctl_set(mch, mrp);
376410734SEric Cheng 
376510734SEric Cheng done:
37668275SEric Cheng 	i_mac_perim_exit(mip);
37678275SEric Cheng 	return (err);
37688275SEric Cheng }
37698275SEric Cheng 
37708275SEric Cheng /*
37718275SEric Cheng  * Return the properties currently associated with the specified MAC client.
37728275SEric Cheng  */
37738275SEric Cheng void
37748275SEric Cheng mac_client_get_resources(mac_client_handle_t mch, mac_resource_props_t *mrp)
37758275SEric Cheng {
37768275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
37778275SEric Cheng 	mac_resource_props_t	*mcip_mrp = MCIP_RESOURCE_PROPS(mcip);
37788275SEric Cheng 
37798275SEric Cheng 	bcopy(mcip_mrp, mrp, sizeof (mac_resource_props_t));
37808275SEric Cheng }
37818275SEric Cheng 
37828275SEric Cheng /*
3783*11878SVenu.Iyer@Sun.COM  * Return the effective properties currently associated with the specified
3784*11878SVenu.Iyer@Sun.COM  * MAC client.
3785*11878SVenu.Iyer@Sun.COM  */
3786*11878SVenu.Iyer@Sun.COM void
3787*11878SVenu.Iyer@Sun.COM mac_client_get_effective_resources(mac_client_handle_t mch,
3788*11878SVenu.Iyer@Sun.COM     mac_resource_props_t *mrp)
3789*11878SVenu.Iyer@Sun.COM {
3790*11878SVenu.Iyer@Sun.COM 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
3791*11878SVenu.Iyer@Sun.COM 	mac_resource_props_t	*mcip_mrp = MCIP_EFFECTIVE_PROPS(mcip);
3792*11878SVenu.Iyer@Sun.COM 
3793*11878SVenu.Iyer@Sun.COM 	bcopy(mcip_mrp, mrp, sizeof (mac_resource_props_t));
3794*11878SVenu.Iyer@Sun.COM }
3795*11878SVenu.Iyer@Sun.COM 
3796*11878SVenu.Iyer@Sun.COM /*
37978275SEric Cheng  * Pass a copy of the specified packet to the promiscuous callbacks
37988275SEric Cheng  * of the specified MAC.
37998275SEric Cheng  *
38008275SEric Cheng  * If sender is NULL, the function is being invoked for a packet chain
38018275SEric Cheng  * received from the wire. If sender is non-NULL, it points to
38028275SEric Cheng  * the MAC client from which the packet is being sent.
38038275SEric Cheng  *
38048275SEric Cheng  * The packets are distributed to the promiscuous callbacks as follows:
38058275SEric Cheng  *
38068275SEric Cheng  * - all packets are sent to the MAC_CLIENT_PROMISC_ALL callbacks
38078275SEric Cheng  * - all broadcast and multicast packets are sent to the
38088275SEric Cheng  *   MAC_CLIENT_PROMISC_FILTER and MAC_CLIENT_PROMISC_MULTI.
38098275SEric Cheng  *
38108275SEric Cheng  * The unicast packets of MAC_CLIENT_PROMISC_FILTER callbacks are dispatched
38118275SEric Cheng  * after classification by mac_rx_deliver().
38128275SEric Cheng  */
38138275SEric Cheng 
38148275SEric Cheng static void
38158275SEric Cheng mac_promisc_dispatch_one(mac_promisc_impl_t *mpip, mblk_t *mp,
38168275SEric Cheng     boolean_t loopback)
38178275SEric Cheng {
381810639SDarren.Reed@Sun.COM 	mblk_t *mp_copy, *mp_next;
381910639SDarren.Reed@Sun.COM 
382010639SDarren.Reed@Sun.COM 	if (!mpip->mpi_no_copy || mpip->mpi_strip_vlan_tag) {
382110639SDarren.Reed@Sun.COM 		mp_copy = copymsg(mp);
382210639SDarren.Reed@Sun.COM 		if (mp_copy == NULL)
382310639SDarren.Reed@Sun.COM 			return;
382410639SDarren.Reed@Sun.COM 
382510639SDarren.Reed@Sun.COM 		if (mpip->mpi_strip_vlan_tag) {
382610639SDarren.Reed@Sun.COM 			mp_copy = mac_strip_vlan_tag_chain(mp_copy);
382710639SDarren.Reed@Sun.COM 			if (mp_copy == NULL)
382810639SDarren.Reed@Sun.COM 				return;
382910639SDarren.Reed@Sun.COM 		}
383010639SDarren.Reed@Sun.COM 		mp_next = NULL;
383110639SDarren.Reed@Sun.COM 	} else {
383210639SDarren.Reed@Sun.COM 		mp_copy = mp;
383310639SDarren.Reed@Sun.COM 		mp_next = mp->b_next;
383410639SDarren.Reed@Sun.COM 	}
38358275SEric Cheng 	mp_copy->b_next = NULL;
38368275SEric Cheng 
38378275SEric Cheng 	mpip->mpi_fn(mpip->mpi_arg, NULL, mp_copy, loopback);
383810639SDarren.Reed@Sun.COM 	if (mp_copy == mp)
383910639SDarren.Reed@Sun.COM 		mp->b_next = mp_next;
38408275SEric Cheng }
38418275SEric Cheng 
38428275SEric Cheng /*
38438275SEric Cheng  * Return the VID of a packet. Zero if the packet is not tagged.
38448275SEric Cheng  */
38458275SEric Cheng static uint16_t
38468275SEric Cheng mac_ether_vid(mblk_t *mp)
38478275SEric Cheng {
38488275SEric Cheng 	struct ether_header *eth = (struct ether_header *)mp->b_rptr;
38498275SEric Cheng 
38508275SEric Cheng 	if (ntohs(eth->ether_type) == ETHERTYPE_VLAN) {
38518275SEric Cheng 		struct ether_vlan_header *t_evhp =
38528275SEric Cheng 		    (struct ether_vlan_header *)mp->b_rptr;
38538275SEric Cheng 		return (VLAN_ID(ntohs(t_evhp->ether_tci)));
38548275SEric Cheng 	}
38558275SEric Cheng 
38568275SEric Cheng 	return (0);
38578275SEric Cheng }
38588275SEric Cheng 
38598275SEric Cheng /*
38608275SEric Cheng  * Return whether the specified packet contains a multicast or broadcast
38618275SEric Cheng  * destination MAC address.
38628275SEric Cheng  */
38638275SEric Cheng static boolean_t
38648275SEric Cheng mac_is_mcast(mac_impl_t *mip, mblk_t *mp)
38658275SEric Cheng {
38668275SEric Cheng 	mac_header_info_t hdr_info;
38678275SEric Cheng 
38688275SEric Cheng 	if (mac_header_info((mac_handle_t)mip, mp, &hdr_info) != 0)
38698275SEric Cheng 		return (B_FALSE);
38708275SEric Cheng 	return ((hdr_info.mhi_dsttype == MAC_ADDRTYPE_BROADCAST) ||
38718275SEric Cheng 	    (hdr_info.mhi_dsttype == MAC_ADDRTYPE_MULTICAST));
38728275SEric Cheng }
38738275SEric Cheng 
38748275SEric Cheng /*
38758275SEric Cheng  * Send a copy of an mblk chain to the MAC clients of the specified MAC.
38768275SEric Cheng  * "sender" points to the sender MAC client for outbound packets, and
38778275SEric Cheng  * is set to NULL for inbound packets.
38788275SEric Cheng  */
38798275SEric Cheng void
38808275SEric Cheng mac_promisc_dispatch(mac_impl_t *mip, mblk_t *mp_chain,
38818275SEric Cheng     mac_client_impl_t *sender)
38828275SEric Cheng {
38838275SEric Cheng 	mac_promisc_impl_t *mpip;
38848275SEric Cheng 	mac_cb_t *mcb;
38858275SEric Cheng 	mblk_t *mp;
38868275SEric Cheng 	boolean_t is_mcast, is_sender;
38878275SEric Cheng 
38888275SEric Cheng 	MAC_PROMISC_WALKER_INC(mip);
38898275SEric Cheng 	for (mp = mp_chain; mp != NULL; mp = mp->b_next) {
38908275SEric Cheng 		is_mcast = mac_is_mcast(mip, mp);
38918275SEric Cheng 		/* send packet to interested callbacks */
38928275SEric Cheng 		for (mcb = mip->mi_promisc_list; mcb != NULL;
38938275SEric Cheng 		    mcb = mcb->mcb_nextp) {
38948275SEric Cheng 			mpip = (mac_promisc_impl_t *)mcb->mcb_objp;
38958275SEric Cheng 			is_sender = (mpip->mpi_mcip == sender);
38968275SEric Cheng 
38978275SEric Cheng 			if (is_sender && mpip->mpi_no_tx_loop)
38988275SEric Cheng 				/*
38998275SEric Cheng 				 * The sender doesn't want to receive
39008275SEric Cheng 				 * copies of the packets it sends.
39018275SEric Cheng 				 */
39028275SEric Cheng 				continue;
39038275SEric Cheng 
390410491SRishi.Srivatsavai@Sun.COM 			/* this client doesn't need any packets (bridge) */
390510491SRishi.Srivatsavai@Sun.COM 			if (mpip->mpi_fn == NULL)
390610491SRishi.Srivatsavai@Sun.COM 				continue;
390710491SRishi.Srivatsavai@Sun.COM 
39088275SEric Cheng 			/*
39098275SEric Cheng 			 * For an ethernet MAC, don't displatch a multicast
39108275SEric Cheng 			 * packet to a non-PROMISC_ALL callbacks unless the VID
39118275SEric Cheng 			 * of the packet matches the VID of the client.
39128275SEric Cheng 			 */
39138275SEric Cheng 			if (is_mcast &&
39148275SEric Cheng 			    mpip->mpi_type != MAC_CLIENT_PROMISC_ALL &&
39158275SEric Cheng 			    !mac_client_check_flow_vid(mpip->mpi_mcip,
39168275SEric Cheng 			    mac_ether_vid(mp)))
39178275SEric Cheng 				continue;
39188275SEric Cheng 
39198275SEric Cheng 			if (is_sender ||
39208275SEric Cheng 			    mpip->mpi_type == MAC_CLIENT_PROMISC_ALL ||
39218275SEric Cheng 			    is_mcast)
39228275SEric Cheng 				mac_promisc_dispatch_one(mpip, mp, is_sender);
39238275SEric Cheng 		}
39248275SEric Cheng 	}
39258275SEric Cheng 	MAC_PROMISC_WALKER_DCR(mip);
39268275SEric Cheng }
39278275SEric Cheng 
39288275SEric Cheng void
39298275SEric Cheng mac_promisc_client_dispatch(mac_client_impl_t *mcip, mblk_t *mp_chain)
39308275SEric Cheng {
39318275SEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
39328275SEric Cheng 	mac_promisc_impl_t	*mpip;
39338275SEric Cheng 	boolean_t		is_mcast;
39348275SEric Cheng 	mblk_t			*mp;
39358275SEric Cheng 	mac_cb_t		*mcb;
39368275SEric Cheng 
39378275SEric Cheng 	/*
39388275SEric Cheng 	 * The unicast packets for the MAC client still
39398275SEric Cheng 	 * need to be delivered to the MAC_CLIENT_PROMISC_FILTERED
39408275SEric Cheng 	 * promiscuous callbacks. The broadcast and multicast
39418275SEric Cheng 	 * packets were delivered from mac_rx().
39428275SEric Cheng 	 */
39438275SEric Cheng 	MAC_PROMISC_WALKER_INC(mip);
39448275SEric Cheng 	for (mp = mp_chain; mp != NULL; mp = mp->b_next) {
39458275SEric Cheng 		is_mcast = mac_is_mcast(mip, mp);
39468275SEric Cheng 		for (mcb = mcip->mci_promisc_list; mcb != NULL;
39478275SEric Cheng 		    mcb = mcb->mcb_nextp) {
39488275SEric Cheng 			mpip = (mac_promisc_impl_t *)mcb->mcb_objp;
39498275SEric Cheng 			if (mpip->mpi_type == MAC_CLIENT_PROMISC_FILTERED &&
39508275SEric Cheng 			    !is_mcast) {
39518275SEric Cheng 				mac_promisc_dispatch_one(mpip, mp, B_FALSE);
39528275SEric Cheng 			}
39538275SEric Cheng 		}
39548275SEric Cheng 	}
39558275SEric Cheng 	MAC_PROMISC_WALKER_DCR(mip);
39568275SEric Cheng }
39578275SEric Cheng 
39588275SEric Cheng /*
39598275SEric Cheng  * Return the margin value currently assigned to the specified MAC instance.
39608275SEric Cheng  */
39618275SEric Cheng void
39628275SEric Cheng mac_margin_get(mac_handle_t mh, uint32_t *marginp)
39638275SEric Cheng {
39648275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
39658275SEric Cheng 
39668275SEric Cheng 	rw_enter(&(mip->mi_rw_lock), RW_READER);
39678275SEric Cheng 	*marginp = mip->mi_margin;
39688275SEric Cheng 	rw_exit(&(mip->mi_rw_lock));
39698275SEric Cheng }
39708275SEric Cheng 
39718275SEric Cheng /*
39728275SEric Cheng  * mac_info_get() is used for retrieving the mac_info when a DL_INFO_REQ is
39738275SEric Cheng  * issued before a DL_ATTACH_REQ. we walk the i_mac_impl_hash table and find
39748275SEric Cheng  * the first mac_impl_t with a matching driver name; then we copy its mac_info_t
39758275SEric Cheng  * to the caller. we do all this with i_mac_impl_lock held so the mac_impl_t
39768275SEric Cheng  * cannot disappear while we are accessing it.
39778275SEric Cheng  */
39788275SEric Cheng typedef struct i_mac_info_state_s {
39798275SEric Cheng 	const char	*mi_name;
39808275SEric Cheng 	mac_info_t	*mi_infop;
39818275SEric Cheng } i_mac_info_state_t;
39828275SEric Cheng 
39838275SEric Cheng /*ARGSUSED*/
39848275SEric Cheng static uint_t
39858275SEric Cheng i_mac_info_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
39868275SEric Cheng {
39878275SEric Cheng 	i_mac_info_state_t *statep = arg;
39888275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)val;
39898275SEric Cheng 
39908275SEric Cheng 	if (mip->mi_state_flags & MIS_DISABLED)
39918275SEric Cheng 		return (MH_WALK_CONTINUE);
39928275SEric Cheng 
39938275SEric Cheng 	if (strcmp(statep->mi_name,
39948275SEric Cheng 	    ddi_driver_name(mip->mi_dip)) != 0)
39958275SEric Cheng 		return (MH_WALK_CONTINUE);
39968275SEric Cheng 
39978275SEric Cheng 	statep->mi_infop = &mip->mi_info;
39988275SEric Cheng 	return (MH_WALK_TERMINATE);
39998275SEric Cheng }
40008275SEric Cheng 
40018275SEric Cheng boolean_t
40028275SEric Cheng mac_info_get(const char *name, mac_info_t *minfop)
40038275SEric Cheng {
40048275SEric Cheng 	i_mac_info_state_t state;
40058275SEric Cheng 
40068275SEric Cheng 	rw_enter(&i_mac_impl_lock, RW_READER);
40078275SEric Cheng 	state.mi_name = name;
40088275SEric Cheng 	state.mi_infop = NULL;
40098275SEric Cheng 	mod_hash_walk(i_mac_impl_hash, i_mac_info_walker, &state);
40108275SEric Cheng 	if (state.mi_infop == NULL) {
40118275SEric Cheng 		rw_exit(&i_mac_impl_lock);
40128275SEric Cheng 		return (B_FALSE);
40138275SEric Cheng 	}
40148275SEric Cheng 	*minfop = *state.mi_infop;
40158275SEric Cheng 	rw_exit(&i_mac_impl_lock);
40168275SEric Cheng 	return (B_TRUE);
40178275SEric Cheng }
40188275SEric Cheng 
40198275SEric Cheng /*
40208275SEric Cheng  * To get the capabilities that MAC layer cares about, such as rings, factory
402110491SRishi.Srivatsavai@Sun.COM  * mac address, vnic or not, it should directly invoke this function.  If the
402210491SRishi.Srivatsavai@Sun.COM  * link is part of a bridge, then the only "capability" it has is the inability
402310491SRishi.Srivatsavai@Sun.COM  * to do zero copy.
40248275SEric Cheng  */
40258275SEric Cheng boolean_t
40268275SEric Cheng i_mac_capab_get(mac_handle_t mh, mac_capab_t cap, void *cap_data)
40278275SEric Cheng {
40288275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
40298275SEric Cheng 
403010491SRishi.Srivatsavai@Sun.COM 	if (mip->mi_bridge_link != NULL)
403110491SRishi.Srivatsavai@Sun.COM 		return (cap == MAC_CAPAB_NO_ZCOPY);
403210491SRishi.Srivatsavai@Sun.COM 	else if (mip->mi_callbacks->mc_callbacks & MC_GETCAPAB)
40338275SEric Cheng 		return (mip->mi_getcapab(mip->mi_driver, cap, cap_data));
40348275SEric Cheng 	else
40358275SEric Cheng 		return (B_FALSE);
40368275SEric Cheng }
40378275SEric Cheng 
40388275SEric Cheng /*
40398275SEric Cheng  * Capability query function. If number of active mac clients is greater than
40408275SEric Cheng  * 1, only limited capabilities can be advertised to the caller no matter the
40418275SEric Cheng  * driver has certain capability or not. Else, we query the driver to get the
40428275SEric Cheng  * capability.
40438275SEric Cheng  */
40448275SEric Cheng boolean_t
40458275SEric Cheng mac_capab_get(mac_handle_t mh, mac_capab_t cap, void *cap_data)
40468275SEric Cheng {
40478275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
40488275SEric Cheng 
40498275SEric Cheng 	/*
40509073SCathy.Zhou@Sun.COM 	 * if mi_nactiveclients > 1, only MAC_CAPAB_LEGACY, MAC_CAPAB_HCKSUM,
40519073SCathy.Zhou@Sun.COM 	 * MAC_CAPAB_NO_NATIVEVLAN and MAC_CAPAB_NO_ZCOPY can be advertised.
40528275SEric Cheng 	 */
40538275SEric Cheng 	if (mip->mi_nactiveclients > 1) {
40548275SEric Cheng 		switch (cap) {
40558275SEric Cheng 		case MAC_CAPAB_NO_NATIVEVLAN:
40568275SEric Cheng 		case MAC_CAPAB_NO_ZCOPY:
40578275SEric Cheng 			return (B_TRUE);
40589073SCathy.Zhou@Sun.COM 		case MAC_CAPAB_LEGACY:
40599073SCathy.Zhou@Sun.COM 		case MAC_CAPAB_HCKSUM:
40609073SCathy.Zhou@Sun.COM 			break;
40618275SEric Cheng 		default:
40628275SEric Cheng 			return (B_FALSE);
40638275SEric Cheng 		}
40648275SEric Cheng 	}
40658275SEric Cheng 
40668275SEric Cheng 	/* else get capab from driver */
40678275SEric Cheng 	return (i_mac_capab_get(mh, cap, cap_data));
40688275SEric Cheng }
40698275SEric Cheng 
40708275SEric Cheng boolean_t
40718275SEric Cheng mac_sap_verify(mac_handle_t mh, uint32_t sap, uint32_t *bind_sap)
40728275SEric Cheng {
40738275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
40748275SEric Cheng 
40758275SEric Cheng 	return (mip->mi_type->mt_ops.mtops_sap_verify(sap, bind_sap,
40768275SEric Cheng 	    mip->mi_pdata));
40778275SEric Cheng }
40788275SEric Cheng 
40798275SEric Cheng mblk_t *
40808275SEric Cheng mac_header(mac_handle_t mh, const uint8_t *daddr, uint32_t sap, mblk_t *payload,
40818275SEric Cheng     size_t extra_len)
40828275SEric Cheng {
408310616SSebastien.Roy@Sun.COM 	mac_impl_t	*mip = (mac_impl_t *)mh;
408410616SSebastien.Roy@Sun.COM 	const uint8_t	*hdr_daddr;
408510616SSebastien.Roy@Sun.COM 
408610616SSebastien.Roy@Sun.COM 	/*
408710616SSebastien.Roy@Sun.COM 	 * If the MAC is point-to-point with a fixed destination address, then
408810616SSebastien.Roy@Sun.COM 	 * we must always use that destination in the MAC header.
408910616SSebastien.Roy@Sun.COM 	 */
409010616SSebastien.Roy@Sun.COM 	hdr_daddr = (mip->mi_dstaddr_set ? mip->mi_dstaddr : daddr);
409110616SSebastien.Roy@Sun.COM 	return (mip->mi_type->mt_ops.mtops_header(mip->mi_addr, hdr_daddr, sap,
40928275SEric Cheng 	    mip->mi_pdata, payload, extra_len));
40938275SEric Cheng }
40948275SEric Cheng 
40958275SEric Cheng int
40968275SEric Cheng mac_header_info(mac_handle_t mh, mblk_t *mp, mac_header_info_t *mhip)
40978275SEric Cheng {
40988275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
40998275SEric Cheng 
41008275SEric Cheng 	return (mip->mi_type->mt_ops.mtops_header_info(mp, mip->mi_pdata,
41018275SEric Cheng 	    mhip));
41028275SEric Cheng }
41038275SEric Cheng 
410410734SEric Cheng int
410510734SEric Cheng mac_vlan_header_info(mac_handle_t mh, mblk_t *mp, mac_header_info_t *mhip)
410610734SEric Cheng {
410710734SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
410810734SEric Cheng 	boolean_t	is_ethernet = (mip->mi_info.mi_media == DL_ETHER);
410910734SEric Cheng 	int		err = 0;
411010734SEric Cheng 
411110734SEric Cheng 	/*
411210734SEric Cheng 	 * Packets should always be at least 16 bit aligned.
411310734SEric Cheng 	 */
411410734SEric Cheng 	ASSERT(IS_P2ALIGNED(mp->b_rptr, sizeof (uint16_t)));
411510734SEric Cheng 
411610734SEric Cheng 	if ((err = mac_header_info(mh, mp, mhip)) != 0)
411710734SEric Cheng 		return (err);
411810734SEric Cheng 
411910734SEric Cheng 	/*
412010734SEric Cheng 	 * If this is a VLAN-tagged Ethernet packet, then the SAP in the
412110734SEric Cheng 	 * mac_header_info_t as returned by mac_header_info() is
412210734SEric Cheng 	 * ETHERTYPE_VLAN. We need to grab the ethertype from the VLAN header.
412310734SEric Cheng 	 */
412410734SEric Cheng 	if (is_ethernet && (mhip->mhi_bindsap == ETHERTYPE_VLAN)) {
412510734SEric Cheng 		struct ether_vlan_header *evhp;
412610734SEric Cheng 		uint16_t sap;
412710734SEric Cheng 		mblk_t *tmp = NULL;
412810734SEric Cheng 		size_t size;
412910734SEric Cheng 
413010734SEric Cheng 		size = sizeof (struct ether_vlan_header);
413110734SEric Cheng 		if (MBLKL(mp) < size) {
413210734SEric Cheng 			/*
413310734SEric Cheng 			 * Pullup the message in order to get the MAC header
413410734SEric Cheng 			 * infomation. Note that this is a read-only function,
413510734SEric Cheng 			 * we keep the input packet intact.
413610734SEric Cheng 			 */
413710734SEric Cheng 			if ((tmp = msgpullup(mp, size)) == NULL)
413810734SEric Cheng 				return (EINVAL);
413910734SEric Cheng 
414010734SEric Cheng 			mp = tmp;
414110734SEric Cheng 		}
414210734SEric Cheng 		evhp = (struct ether_vlan_header *)mp->b_rptr;
414310734SEric Cheng 		sap = ntohs(evhp->ether_type);
414410734SEric Cheng 		(void) mac_sap_verify(mh, sap, &mhip->mhi_bindsap);
414510734SEric Cheng 		mhip->mhi_hdrsize = sizeof (struct ether_vlan_header);
414610734SEric Cheng 		mhip->mhi_tci = ntohs(evhp->ether_tci);
414710734SEric Cheng 		mhip->mhi_istagged = B_TRUE;
414810734SEric Cheng 		freemsg(tmp);
414910734SEric Cheng 
415010734SEric Cheng 		if (VLAN_CFI(mhip->mhi_tci) != ETHER_CFI)
415110734SEric Cheng 			return (EINVAL);
415210734SEric Cheng 	} else {
415310734SEric Cheng 		mhip->mhi_istagged = B_FALSE;
415410734SEric Cheng 		mhip->mhi_tci = 0;
415510734SEric Cheng 	}
415610734SEric Cheng 
415710734SEric Cheng 	return (0);
415810734SEric Cheng }
415910734SEric Cheng 
41608275SEric Cheng mblk_t *
41618275SEric Cheng mac_header_cook(mac_handle_t mh, mblk_t *mp)
41628275SEric Cheng {
41638275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
41648275SEric Cheng 
41658275SEric Cheng 	if (mip->mi_type->mt_ops.mtops_ops & MTOPS_HEADER_COOK) {
41668275SEric Cheng 		if (DB_REF(mp) > 1) {
41678275SEric Cheng 			mblk_t *newmp = copymsg(mp);
41688275SEric Cheng 			if (newmp == NULL)
41698275SEric Cheng 				return (NULL);
41708275SEric Cheng 			freemsg(mp);
41718275SEric Cheng 			mp = newmp;
41728275SEric Cheng 		}
41738275SEric Cheng 		return (mip->mi_type->mt_ops.mtops_header_cook(mp,
41748275SEric Cheng 		    mip->mi_pdata));
41758275SEric Cheng 	}
41768275SEric Cheng 	return (mp);
41778275SEric Cheng }
41788275SEric Cheng 
41798275SEric Cheng mblk_t *
41808275SEric Cheng mac_header_uncook(mac_handle_t mh, mblk_t *mp)
41818275SEric Cheng {
41828275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
41838275SEric Cheng 
41848275SEric Cheng 	if (mip->mi_type->mt_ops.mtops_ops & MTOPS_HEADER_UNCOOK) {
41858275SEric Cheng 		if (DB_REF(mp) > 1) {
41868275SEric Cheng 			mblk_t *newmp = copymsg(mp);
41878275SEric Cheng 			if (newmp == NULL)
41888275SEric Cheng 				return (NULL);
41898275SEric Cheng 			freemsg(mp);
41908275SEric Cheng 			mp = newmp;
41918275SEric Cheng 		}
41928275SEric Cheng 		return (mip->mi_type->mt_ops.mtops_header_uncook(mp,
41938275SEric Cheng 		    mip->mi_pdata));
41948275SEric Cheng 	}
41958275SEric Cheng 	return (mp);
41968275SEric Cheng }
41978275SEric Cheng 
41988275SEric Cheng uint_t
41998275SEric Cheng mac_addr_len(mac_handle_t mh)
42008275SEric Cheng {
42018275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
42028275SEric Cheng 
42038275SEric Cheng 	return (mip->mi_type->mt_addr_length);
42048275SEric Cheng }
42058275SEric Cheng 
42068275SEric Cheng /* True if a MAC is a VNIC */
42078275SEric Cheng boolean_t
42088275SEric Cheng mac_is_vnic(mac_handle_t mh)
42098275SEric Cheng {
42108275SEric Cheng 	return (((mac_impl_t *)mh)->mi_state_flags & MIS_IS_VNIC);
42118275SEric Cheng }
42128275SEric Cheng 
42138275SEric Cheng mac_handle_t
42148275SEric Cheng mac_get_lower_mac_handle(mac_handle_t mh)
42158275SEric Cheng {
42168275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
42178275SEric Cheng 
42188275SEric Cheng 	ASSERT(mac_is_vnic(mh));
42198275SEric Cheng 	return (((vnic_t *)mip->mi_driver)->vn_lower_mh);
42208275SEric Cheng }
42218275SEric Cheng 
4222*11878SVenu.Iyer@Sun.COM boolean_t
4223*11878SVenu.Iyer@Sun.COM mac_is_vnic_primary(mac_handle_t mh)
4224*11878SVenu.Iyer@Sun.COM {
4225*11878SVenu.Iyer@Sun.COM 	mac_impl_t *mip = (mac_impl_t *)mh;
4226*11878SVenu.Iyer@Sun.COM 
4227*11878SVenu.Iyer@Sun.COM 	ASSERT(mac_is_vnic(mh));
4228*11878SVenu.Iyer@Sun.COM 	return (((vnic_t *)mip->mi_driver)->vn_addr_type ==
4229*11878SVenu.Iyer@Sun.COM 	    VNIC_MAC_ADDR_TYPE_PRIMARY);
4230*11878SVenu.Iyer@Sun.COM }
4231*11878SVenu.Iyer@Sun.COM 
42328275SEric Cheng void
42338275SEric Cheng mac_update_resources(mac_resource_props_t *nmrp, mac_resource_props_t *cmrp,
42348275SEric Cheng     boolean_t is_user_flow)
42358275SEric Cheng {
42368275SEric Cheng 	if (nmrp != NULL && cmrp != NULL) {
42378275SEric Cheng 		if (nmrp->mrp_mask & MRP_PRIORITY) {
42388275SEric Cheng 			if (nmrp->mrp_priority == MPL_RESET) {
42398275SEric Cheng 				cmrp->mrp_mask &= ~MRP_PRIORITY;
42408275SEric Cheng 				if (is_user_flow) {
42418275SEric Cheng 					cmrp->mrp_priority =
42428275SEric Cheng 					    MPL_SUBFLOW_DEFAULT;
42438275SEric Cheng 				} else {
42448275SEric Cheng 					cmrp->mrp_priority = MPL_LINK_DEFAULT;
42458275SEric Cheng 				}
42468275SEric Cheng 			} else {
42478275SEric Cheng 				cmrp->mrp_mask |= MRP_PRIORITY;
42488275SEric Cheng 				cmrp->mrp_priority = nmrp->mrp_priority;
42498275SEric Cheng 			}
42508275SEric Cheng 		}
42518275SEric Cheng 		if (nmrp->mrp_mask & MRP_MAXBW) {
4252*11878SVenu.Iyer@Sun.COM 			if (nmrp->mrp_maxbw == MRP_MAXBW_RESETVAL) {
42538275SEric Cheng 				cmrp->mrp_mask &= ~MRP_MAXBW;
4254*11878SVenu.Iyer@Sun.COM 				cmrp->mrp_maxbw = 0;
4255*11878SVenu.Iyer@Sun.COM 			} else {
42568275SEric Cheng 				cmrp->mrp_mask |= MRP_MAXBW;
4257*11878SVenu.Iyer@Sun.COM 				cmrp->mrp_maxbw = nmrp->mrp_maxbw;
4258*11878SVenu.Iyer@Sun.COM 			}
42598275SEric Cheng 		}
42608275SEric Cheng 		if (nmrp->mrp_mask & MRP_CPUS)
42618275SEric Cheng 			MAC_COPY_CPUS(nmrp, cmrp);
426210734SEric Cheng 
4263*11878SVenu.Iyer@Sun.COM 		if (nmrp->mrp_mask & MRP_POOL) {
4264*11878SVenu.Iyer@Sun.COM 			if (strlen(nmrp->mrp_pool) == 0) {
4265*11878SVenu.Iyer@Sun.COM 				cmrp->mrp_mask &= ~MRP_POOL;
4266*11878SVenu.Iyer@Sun.COM 				bzero(cmrp->mrp_pool, sizeof (cmrp->mrp_pool));
4267*11878SVenu.Iyer@Sun.COM 			} else {
4268*11878SVenu.Iyer@Sun.COM 				cmrp->mrp_mask |= MRP_POOL;
4269*11878SVenu.Iyer@Sun.COM 				(void) strncpy(cmrp->mrp_pool, nmrp->mrp_pool,
4270*11878SVenu.Iyer@Sun.COM 				    sizeof (cmrp->mrp_pool));
4271*11878SVenu.Iyer@Sun.COM 			}
4272*11878SVenu.Iyer@Sun.COM 
4273*11878SVenu.Iyer@Sun.COM 		}
4274*11878SVenu.Iyer@Sun.COM 
427510734SEric Cheng 		if (nmrp->mrp_mask & MRP_PROTECT)
427610734SEric Cheng 			mac_protect_update(nmrp, cmrp);
4277*11878SVenu.Iyer@Sun.COM 
4278*11878SVenu.Iyer@Sun.COM 		/*
4279*11878SVenu.Iyer@Sun.COM 		 * Update the rings specified.
4280*11878SVenu.Iyer@Sun.COM 		 */
4281*11878SVenu.Iyer@Sun.COM 		if (nmrp->mrp_mask & MRP_RX_RINGS) {
4282*11878SVenu.Iyer@Sun.COM 			if (nmrp->mrp_mask & MRP_RINGS_RESET) {
4283*11878SVenu.Iyer@Sun.COM 				cmrp->mrp_mask &= ~MRP_RX_RINGS;
4284*11878SVenu.Iyer@Sun.COM 				if (cmrp->mrp_mask & MRP_RXRINGS_UNSPEC)
4285*11878SVenu.Iyer@Sun.COM 					cmrp->mrp_mask &= ~MRP_RXRINGS_UNSPEC;
4286*11878SVenu.Iyer@Sun.COM 				cmrp->mrp_nrxrings = 0;
4287*11878SVenu.Iyer@Sun.COM 			} else {
4288*11878SVenu.Iyer@Sun.COM 				cmrp->mrp_mask |= MRP_RX_RINGS;
4289*11878SVenu.Iyer@Sun.COM 				cmrp->mrp_nrxrings = nmrp->mrp_nrxrings;
4290*11878SVenu.Iyer@Sun.COM 			}
4291*11878SVenu.Iyer@Sun.COM 		}
4292*11878SVenu.Iyer@Sun.COM 		if (nmrp->mrp_mask & MRP_TX_RINGS) {
4293*11878SVenu.Iyer@Sun.COM 			if (nmrp->mrp_mask & MRP_RINGS_RESET) {
4294*11878SVenu.Iyer@Sun.COM 				cmrp->mrp_mask &= ~MRP_TX_RINGS;
4295*11878SVenu.Iyer@Sun.COM 				if (cmrp->mrp_mask & MRP_TXRINGS_UNSPEC)
4296*11878SVenu.Iyer@Sun.COM 					cmrp->mrp_mask &= ~MRP_TXRINGS_UNSPEC;
4297*11878SVenu.Iyer@Sun.COM 				cmrp->mrp_ntxrings = 0;
4298*11878SVenu.Iyer@Sun.COM 			} else {
4299*11878SVenu.Iyer@Sun.COM 				cmrp->mrp_mask |= MRP_TX_RINGS;
4300*11878SVenu.Iyer@Sun.COM 				cmrp->mrp_ntxrings = nmrp->mrp_ntxrings;
4301*11878SVenu.Iyer@Sun.COM 			}
4302*11878SVenu.Iyer@Sun.COM 		}
4303*11878SVenu.Iyer@Sun.COM 		if (nmrp->mrp_mask & MRP_RXRINGS_UNSPEC)
4304*11878SVenu.Iyer@Sun.COM 			cmrp->mrp_mask |= MRP_RXRINGS_UNSPEC;
4305*11878SVenu.Iyer@Sun.COM 		else if (cmrp->mrp_mask & MRP_RXRINGS_UNSPEC)
4306*11878SVenu.Iyer@Sun.COM 			cmrp->mrp_mask &= ~MRP_RXRINGS_UNSPEC;
4307*11878SVenu.Iyer@Sun.COM 
4308*11878SVenu.Iyer@Sun.COM 		if (nmrp->mrp_mask & MRP_TXRINGS_UNSPEC)
4309*11878SVenu.Iyer@Sun.COM 			cmrp->mrp_mask |= MRP_TXRINGS_UNSPEC;
4310*11878SVenu.Iyer@Sun.COM 		else if (cmrp->mrp_mask & MRP_TXRINGS_UNSPEC)
4311*11878SVenu.Iyer@Sun.COM 			cmrp->mrp_mask &= ~MRP_TXRINGS_UNSPEC;
43128275SEric Cheng 	}
43138275SEric Cheng }
43148275SEric Cheng 
43158275SEric Cheng /*
43168275SEric Cheng  * i_mac_set_resources:
43178275SEric Cheng  *
43188275SEric Cheng  * This routine associates properties with the primary MAC client of
43198275SEric Cheng  * the specified MAC instance.
43208275SEric Cheng  * - Cache the properties in mac_impl_t
43218275SEric Cheng  * - Apply the properties to the primary MAC client if exists
43228275SEric Cheng  */
43238275SEric Cheng int
43248275SEric Cheng i_mac_set_resources(mac_handle_t mh, mac_resource_props_t *mrp)
43258275SEric Cheng {
43268275SEric Cheng 	mac_impl_t		*mip = (mac_impl_t *)mh;
43278275SEric Cheng 	mac_client_impl_t	*mcip;
43288275SEric Cheng 	int			err = 0;
43299073SCathy.Zhou@Sun.COM 	uint32_t		resmask, newresmask;
4330*11878SVenu.Iyer@Sun.COM 	mac_resource_props_t	*tmrp, *umrp;
43318275SEric Cheng 
43328275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
43338275SEric Cheng 
4334*11878SVenu.Iyer@Sun.COM 	err = mac_validate_props(mip, mrp);
43358275SEric Cheng 	if (err != 0)
43368275SEric Cheng 		return (err);
43378275SEric Cheng 
4338*11878SVenu.Iyer@Sun.COM 	umrp = kmem_zalloc(sizeof (*umrp), KM_SLEEP);
4339*11878SVenu.Iyer@Sun.COM 	bcopy(&mip->mi_resource_props, umrp, sizeof (*umrp));
4340*11878SVenu.Iyer@Sun.COM 	resmask = umrp->mrp_mask;
4341*11878SVenu.Iyer@Sun.COM 	mac_update_resources(mrp, umrp, B_FALSE);
4342*11878SVenu.Iyer@Sun.COM 	newresmask = umrp->mrp_mask;
43439073SCathy.Zhou@Sun.COM 
43449073SCathy.Zhou@Sun.COM 	if (resmask == 0 && newresmask != 0) {
43459073SCathy.Zhou@Sun.COM 		/*
4346*11878SVenu.Iyer@Sun.COM 		 * Bandwidth, priority, cpu or pool link properties configured,
43479073SCathy.Zhou@Sun.COM 		 * must disable fastpath.
43489073SCathy.Zhou@Sun.COM 		 */
4349*11878SVenu.Iyer@Sun.COM 		if ((err = mac_fastpath_disable((mac_handle_t)mip)) != 0) {
4350*11878SVenu.Iyer@Sun.COM 			kmem_free(umrp, sizeof (*umrp));
43519073SCathy.Zhou@Sun.COM 			return (err);
4352*11878SVenu.Iyer@Sun.COM 		}
43539073SCathy.Zhou@Sun.COM 	}
43549073SCathy.Zhou@Sun.COM 
43558275SEric Cheng 	/*
43568275SEric Cheng 	 * Since bind_cpu may be modified by mac_client_set_resources()
43578275SEric Cheng 	 * we use a copy of bind_cpu and finally cache bind_cpu in mip.
43588275SEric Cheng 	 * This allows us to cache only user edits in mip.
43598275SEric Cheng 	 */
4360*11878SVenu.Iyer@Sun.COM 	tmrp = kmem_zalloc(sizeof (*tmrp), KM_SLEEP);
4361*11878SVenu.Iyer@Sun.COM 	bcopy(mrp, tmrp, sizeof (*tmrp));
43628275SEric Cheng 	mcip = mac_primary_client_handle(mip);
43638833SVenu.Iyer@Sun.COM 	if (mcip != NULL && (mcip->mci_state_flags & MCIS_IS_AGGR_PORT) == 0) {
4364*11878SVenu.Iyer@Sun.COM 		err = mac_client_set_resources((mac_client_handle_t)mcip, tmrp);
4365*11878SVenu.Iyer@Sun.COM 	} else if ((mrp->mrp_mask & MRP_RX_RINGS ||
4366*11878SVenu.Iyer@Sun.COM 	    mrp->mrp_mask & MRP_TX_RINGS)) {
4367*11878SVenu.Iyer@Sun.COM 		mac_client_impl_t	*vmcip;
4368*11878SVenu.Iyer@Sun.COM 
4369*11878SVenu.Iyer@Sun.COM 		/*
4370*11878SVenu.Iyer@Sun.COM 		 * If the primary is not up, we need to check if there
4371*11878SVenu.Iyer@Sun.COM 		 * are any VLANs on this primary. If there are then
4372*11878SVenu.Iyer@Sun.COM 		 * we need to set this property on the VLANs since
4373*11878SVenu.Iyer@Sun.COM 		 * VLANs follow the primary they are based on. Just
4374*11878SVenu.Iyer@Sun.COM 		 * look for the first VLAN and change its properties,
4375*11878SVenu.Iyer@Sun.COM 		 * all the other VLANs should be in the same group.
4376*11878SVenu.Iyer@Sun.COM 		 */
4377*11878SVenu.Iyer@Sun.COM 		for (vmcip = mip->mi_clients_list; vmcip != NULL;
4378*11878SVenu.Iyer@Sun.COM 		    vmcip = vmcip->mci_client_next) {
4379*11878SVenu.Iyer@Sun.COM 			if ((vmcip->mci_flent->fe_type & FLOW_PRIMARY_MAC) &&
4380*11878SVenu.Iyer@Sun.COM 			    mac_client_vid((mac_client_handle_t)vmcip) !=
4381*11878SVenu.Iyer@Sun.COM 			    VLAN_ID_NONE) {
4382*11878SVenu.Iyer@Sun.COM 				break;
4383*11878SVenu.Iyer@Sun.COM 			}
4384*11878SVenu.Iyer@Sun.COM 		}
4385*11878SVenu.Iyer@Sun.COM 		if (vmcip != NULL) {
4386*11878SVenu.Iyer@Sun.COM 			mac_resource_props_t	*omrp;
4387*11878SVenu.Iyer@Sun.COM 			mac_resource_props_t	*vmrp;
4388*11878SVenu.Iyer@Sun.COM 
4389*11878SVenu.Iyer@Sun.COM 			omrp = kmem_zalloc(sizeof (*omrp), KM_SLEEP);
4390*11878SVenu.Iyer@Sun.COM 			bcopy(MCIP_RESOURCE_PROPS(vmcip), omrp, sizeof (*omrp));
4391*11878SVenu.Iyer@Sun.COM 			/*
4392*11878SVenu.Iyer@Sun.COM 			 * We dont' call mac_update_resources since we
4393*11878SVenu.Iyer@Sun.COM 			 * want to take only the ring properties and
4394*11878SVenu.Iyer@Sun.COM 			 * not all the properties that may have changed.
4395*11878SVenu.Iyer@Sun.COM 			 */
4396*11878SVenu.Iyer@Sun.COM 			vmrp = MCIP_RESOURCE_PROPS(vmcip);
4397*11878SVenu.Iyer@Sun.COM 			if (mrp->mrp_mask & MRP_RX_RINGS) {
4398*11878SVenu.Iyer@Sun.COM 				if (mrp->mrp_mask & MRP_RINGS_RESET) {
4399*11878SVenu.Iyer@Sun.COM 					vmrp->mrp_mask &= ~MRP_RX_RINGS;
4400*11878SVenu.Iyer@Sun.COM 					if (vmrp->mrp_mask &
4401*11878SVenu.Iyer@Sun.COM 					    MRP_RXRINGS_UNSPEC) {
4402*11878SVenu.Iyer@Sun.COM 						vmrp->mrp_mask &=
4403*11878SVenu.Iyer@Sun.COM 						    ~MRP_RXRINGS_UNSPEC;
4404*11878SVenu.Iyer@Sun.COM 					}
4405*11878SVenu.Iyer@Sun.COM 					vmrp->mrp_nrxrings = 0;
4406*11878SVenu.Iyer@Sun.COM 				} else {
4407*11878SVenu.Iyer@Sun.COM 					vmrp->mrp_mask |= MRP_RX_RINGS;
4408*11878SVenu.Iyer@Sun.COM 					vmrp->mrp_nrxrings = mrp->mrp_nrxrings;
4409*11878SVenu.Iyer@Sun.COM 				}
4410*11878SVenu.Iyer@Sun.COM 			}
4411*11878SVenu.Iyer@Sun.COM 			if (mrp->mrp_mask & MRP_TX_RINGS) {
4412*11878SVenu.Iyer@Sun.COM 				if (mrp->mrp_mask & MRP_RINGS_RESET) {
4413*11878SVenu.Iyer@Sun.COM 					vmrp->mrp_mask &= ~MRP_TX_RINGS;
4414*11878SVenu.Iyer@Sun.COM 					if (vmrp->mrp_mask &
4415*11878SVenu.Iyer@Sun.COM 					    MRP_TXRINGS_UNSPEC) {
4416*11878SVenu.Iyer@Sun.COM 						vmrp->mrp_mask &=
4417*11878SVenu.Iyer@Sun.COM 						    ~MRP_TXRINGS_UNSPEC;
4418*11878SVenu.Iyer@Sun.COM 					}
4419*11878SVenu.Iyer@Sun.COM 					vmrp->mrp_ntxrings = 0;
4420*11878SVenu.Iyer@Sun.COM 				} else {
4421*11878SVenu.Iyer@Sun.COM 					vmrp->mrp_mask |= MRP_TX_RINGS;
4422*11878SVenu.Iyer@Sun.COM 					vmrp->mrp_ntxrings = mrp->mrp_ntxrings;
4423*11878SVenu.Iyer@Sun.COM 				}
4424*11878SVenu.Iyer@Sun.COM 			}
4425*11878SVenu.Iyer@Sun.COM 			if (mrp->mrp_mask & MRP_RXRINGS_UNSPEC)
4426*11878SVenu.Iyer@Sun.COM 				vmrp->mrp_mask |= MRP_RXRINGS_UNSPEC;
4427*11878SVenu.Iyer@Sun.COM 
4428*11878SVenu.Iyer@Sun.COM 			if (mrp->mrp_mask & MRP_TXRINGS_UNSPEC)
4429*11878SVenu.Iyer@Sun.COM 				vmrp->mrp_mask |= MRP_TXRINGS_UNSPEC;
4430*11878SVenu.Iyer@Sun.COM 
4431*11878SVenu.Iyer@Sun.COM 			if ((err = mac_client_set_rings_prop(vmcip, mrp,
4432*11878SVenu.Iyer@Sun.COM 			    omrp)) != 0) {
4433*11878SVenu.Iyer@Sun.COM 				bcopy(omrp, MCIP_RESOURCE_PROPS(vmcip),
4434*11878SVenu.Iyer@Sun.COM 				    sizeof (*omrp));
4435*11878SVenu.Iyer@Sun.COM 			} else {
4436*11878SVenu.Iyer@Sun.COM 				mac_set_prim_vlan_rings(mip, vmrp);
4437*11878SVenu.Iyer@Sun.COM 			}
4438*11878SVenu.Iyer@Sun.COM 			kmem_free(omrp, sizeof (*omrp));
4439*11878SVenu.Iyer@Sun.COM 		}
44408275SEric Cheng 	}
44419073SCathy.Zhou@Sun.COM 
44429073SCathy.Zhou@Sun.COM 	/* Only update the values if mac_client_set_resources succeeded */
44439073SCathy.Zhou@Sun.COM 	if (err == 0) {
4444*11878SVenu.Iyer@Sun.COM 		bcopy(umrp, &mip->mi_resource_props, sizeof (*umrp));
44459073SCathy.Zhou@Sun.COM 		/*
4446*11878SVenu.Iyer@Sun.COM 		 * If bandwidth, priority or cpu link properties cleared,
44479073SCathy.Zhou@Sun.COM 		 * renable fastpath.
44489073SCathy.Zhou@Sun.COM 		 */
44499073SCathy.Zhou@Sun.COM 		if (resmask != 0 && newresmask == 0)
44509073SCathy.Zhou@Sun.COM 			mac_fastpath_enable((mac_handle_t)mip);
44519073SCathy.Zhou@Sun.COM 	} else if (resmask == 0 && newresmask != 0) {
44529073SCathy.Zhou@Sun.COM 		mac_fastpath_enable((mac_handle_t)mip);
44539073SCathy.Zhou@Sun.COM 	}
4454*11878SVenu.Iyer@Sun.COM 	kmem_free(tmrp, sizeof (*tmrp));
4455*11878SVenu.Iyer@Sun.COM 	kmem_free(umrp, sizeof (*umrp));
44568275SEric Cheng 	return (err);
44578275SEric Cheng }
44588275SEric Cheng 
44598275SEric Cheng int
44608275SEric Cheng mac_set_resources(mac_handle_t mh, mac_resource_props_t *mrp)
44618275SEric Cheng {
44628275SEric Cheng 	int err;
44638275SEric Cheng 
44648275SEric Cheng 	i_mac_perim_enter((mac_impl_t *)mh);
44658275SEric Cheng 	err = i_mac_set_resources(mh, mrp);
44668275SEric Cheng 	i_mac_perim_exit((mac_impl_t *)mh);
44678275SEric Cheng 	return (err);
44688275SEric Cheng }
44698275SEric Cheng 
44708275SEric Cheng /*
44718275SEric Cheng  * Get the properties cached for the specified MAC instance.
44728275SEric Cheng  */
44738275SEric Cheng void
44748275SEric Cheng mac_get_resources(mac_handle_t mh, mac_resource_props_t *mrp)
44758275SEric Cheng {
44768275SEric Cheng 	mac_impl_t 		*mip = (mac_impl_t *)mh;
44778275SEric Cheng 	mac_client_impl_t	*mcip;
44788275SEric Cheng 
4479*11878SVenu.Iyer@Sun.COM 	mcip = mac_primary_client_handle(mip);
4480*11878SVenu.Iyer@Sun.COM 	if (mcip != NULL) {
4481*11878SVenu.Iyer@Sun.COM 		mac_client_get_resources((mac_client_handle_t)mcip, mrp);
4482*11878SVenu.Iyer@Sun.COM 		return;
44838275SEric Cheng 	}
44848275SEric Cheng 	bcopy(&mip->mi_resource_props, mrp, sizeof (mac_resource_props_t));
44858275SEric Cheng }
44868275SEric Cheng 
4487*11878SVenu.Iyer@Sun.COM /*
4488*11878SVenu.Iyer@Sun.COM  * Get the effective properties from the primary client of the
4489*11878SVenu.Iyer@Sun.COM  * specified MAC instance.
4490*11878SVenu.Iyer@Sun.COM  */
4491*11878SVenu.Iyer@Sun.COM void
4492*11878SVenu.Iyer@Sun.COM mac_get_effective_resources(mac_handle_t mh, mac_resource_props_t *mrp)
4493*11878SVenu.Iyer@Sun.COM {
4494*11878SVenu.Iyer@Sun.COM 	mac_impl_t 		*mip = (mac_impl_t *)mh;
4495*11878SVenu.Iyer@Sun.COM 	mac_client_impl_t	*mcip;
4496*11878SVenu.Iyer@Sun.COM 
4497*11878SVenu.Iyer@Sun.COM 	mcip = mac_primary_client_handle(mip);
4498*11878SVenu.Iyer@Sun.COM 	if (mcip != NULL) {
4499*11878SVenu.Iyer@Sun.COM 		mac_client_get_effective_resources((mac_client_handle_t)mcip,
4500*11878SVenu.Iyer@Sun.COM 		    mrp);
4501*11878SVenu.Iyer@Sun.COM 		return;
4502*11878SVenu.Iyer@Sun.COM 	}
4503*11878SVenu.Iyer@Sun.COM 	bzero(mrp, sizeof (mac_resource_props_t));
4504*11878SVenu.Iyer@Sun.COM }
4505*11878SVenu.Iyer@Sun.COM 
450610491SRishi.Srivatsavai@Sun.COM int
450710491SRishi.Srivatsavai@Sun.COM mac_set_pvid(mac_handle_t mh, uint16_t pvid)
450810491SRishi.Srivatsavai@Sun.COM {
450910491SRishi.Srivatsavai@Sun.COM 	mac_impl_t *mip = (mac_impl_t *)mh;
451010491SRishi.Srivatsavai@Sun.COM 	mac_client_impl_t *mcip;
451110491SRishi.Srivatsavai@Sun.COM 	mac_unicast_impl_t *muip;
451210491SRishi.Srivatsavai@Sun.COM 
451310491SRishi.Srivatsavai@Sun.COM 	i_mac_perim_enter(mip);
451410491SRishi.Srivatsavai@Sun.COM 	if (pvid != 0) {
451510491SRishi.Srivatsavai@Sun.COM 		for (mcip = mip->mi_clients_list; mcip != NULL;
451610491SRishi.Srivatsavai@Sun.COM 		    mcip = mcip->mci_client_next) {
451710491SRishi.Srivatsavai@Sun.COM 			for (muip = mcip->mci_unicast_list; muip != NULL;
451810491SRishi.Srivatsavai@Sun.COM 			    muip = muip->mui_next) {
451910491SRishi.Srivatsavai@Sun.COM 				if (muip->mui_vid == pvid) {
452010491SRishi.Srivatsavai@Sun.COM 					i_mac_perim_exit(mip);
452110491SRishi.Srivatsavai@Sun.COM 					return (EBUSY);
452210491SRishi.Srivatsavai@Sun.COM 				}
452310491SRishi.Srivatsavai@Sun.COM 			}
452410491SRishi.Srivatsavai@Sun.COM 		}
452510491SRishi.Srivatsavai@Sun.COM 	}
452610491SRishi.Srivatsavai@Sun.COM 	mip->mi_pvid = pvid;
452710491SRishi.Srivatsavai@Sun.COM 	i_mac_perim_exit(mip);
452810491SRishi.Srivatsavai@Sun.COM 	return (0);
452910491SRishi.Srivatsavai@Sun.COM }
453010491SRishi.Srivatsavai@Sun.COM 
453110491SRishi.Srivatsavai@Sun.COM uint16_t
453210491SRishi.Srivatsavai@Sun.COM mac_get_pvid(mac_handle_t mh)
453310491SRishi.Srivatsavai@Sun.COM {
453410491SRishi.Srivatsavai@Sun.COM 	mac_impl_t *mip = (mac_impl_t *)mh;
453510491SRishi.Srivatsavai@Sun.COM 
453610491SRishi.Srivatsavai@Sun.COM 	return (mip->mi_pvid);
453710491SRishi.Srivatsavai@Sun.COM }
453810491SRishi.Srivatsavai@Sun.COM 
453910491SRishi.Srivatsavai@Sun.COM uint32_t
454010491SRishi.Srivatsavai@Sun.COM mac_get_llimit(mac_handle_t mh)
454110491SRishi.Srivatsavai@Sun.COM {
454210491SRishi.Srivatsavai@Sun.COM 	mac_impl_t *mip = (mac_impl_t *)mh;
454310491SRishi.Srivatsavai@Sun.COM 
454410491SRishi.Srivatsavai@Sun.COM 	return (mip->mi_llimit);
454510491SRishi.Srivatsavai@Sun.COM }
454610491SRishi.Srivatsavai@Sun.COM 
454710491SRishi.Srivatsavai@Sun.COM uint32_t
454810491SRishi.Srivatsavai@Sun.COM mac_get_ldecay(mac_handle_t mh)
454910491SRishi.Srivatsavai@Sun.COM {
455010491SRishi.Srivatsavai@Sun.COM 	mac_impl_t *mip = (mac_impl_t *)mh;
455110491SRishi.Srivatsavai@Sun.COM 
455210491SRishi.Srivatsavai@Sun.COM 	return (mip->mi_ldecay);
455310491SRishi.Srivatsavai@Sun.COM }
455410491SRishi.Srivatsavai@Sun.COM 
45558275SEric Cheng /*
45568275SEric Cheng  * Rename a mac client, its flow, and the kstat.
45578275SEric Cheng  */
45588275SEric Cheng int
45598275SEric Cheng mac_rename_primary(mac_handle_t mh, const char *new_name)
45608275SEric Cheng {
45618275SEric Cheng 	mac_impl_t		*mip = (mac_impl_t *)mh;
45628275SEric Cheng 	mac_client_impl_t	*cur_clnt = NULL;
45638275SEric Cheng 	flow_entry_t		*fep;
45648275SEric Cheng 
45658275SEric Cheng 	i_mac_perim_enter(mip);
45668275SEric Cheng 
45678275SEric Cheng 	/*
45688275SEric Cheng 	 * VNICs: we need to change the sys flow name and
45698275SEric Cheng 	 * the associated flow kstat.
45708275SEric Cheng 	 */
45718275SEric Cheng 	if (mip->mi_state_flags & MIS_IS_VNIC) {
4572*11878SVenu.Iyer@Sun.COM 		mac_client_impl_t *mcip = mac_vnic_lower(mip);
45738275SEric Cheng 		ASSERT(new_name != NULL);
4574*11878SVenu.Iyer@Sun.COM 		mac_rename_flow_names(mcip, new_name);
4575*11878SVenu.Iyer@Sun.COM 		mac_stat_rename(mcip);
45768275SEric Cheng 		goto done;
45778275SEric Cheng 	}
45788275SEric Cheng 	/*
45798275SEric Cheng 	 * This mac may itself be an aggr link, or it may have some client
45808275SEric Cheng 	 * which is an aggr port. For both cases, we need to change the
45818275SEric Cheng 	 * aggr port's mac client name, its flow name and the associated flow
45828275SEric Cheng 	 * kstat.
45838275SEric Cheng 	 */
45848275SEric Cheng 	if (mip->mi_state_flags & MIS_IS_AGGR) {
45858275SEric Cheng 		mac_capab_aggr_t aggr_cap;
45868275SEric Cheng 		mac_rename_fn_t rename_fn;
45878275SEric Cheng 		boolean_t ret;
45888275SEric Cheng 
45898275SEric Cheng 		ASSERT(new_name != NULL);
45908275SEric Cheng 		ret = i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_AGGR,
45918275SEric Cheng 		    (void *)(&aggr_cap));
45928275SEric Cheng 		ASSERT(ret == B_TRUE);
45938275SEric Cheng 		rename_fn = aggr_cap.mca_rename_fn;
45948275SEric Cheng 		rename_fn(new_name, mip->mi_driver);
45958275SEric Cheng 		/*
45968275SEric Cheng 		 * The aggr's client name and kstat flow name will be
45978275SEric Cheng 		 * updated below, i.e. via mac_rename_flow_names.
45988275SEric Cheng 		 */
45998275SEric Cheng 	}
46008275SEric Cheng 
46018275SEric Cheng 	for (cur_clnt = mip->mi_clients_list; cur_clnt != NULL;
46028275SEric Cheng 	    cur_clnt = cur_clnt->mci_client_next) {
46038275SEric Cheng 		if (cur_clnt->mci_state_flags & MCIS_IS_AGGR_PORT) {
46048275SEric Cheng 			if (new_name != NULL) {
46058275SEric Cheng 				char *str_st = cur_clnt->mci_name;
46068275SEric Cheng 				char *str_del = strchr(str_st, '-');
46078275SEric Cheng 
46088275SEric Cheng 				ASSERT(str_del != NULL);
46098275SEric Cheng 				bzero(str_del + 1, MAXNAMELEN -
46108275SEric Cheng 				    (str_del - str_st + 1));
46118275SEric Cheng 				bcopy(new_name, str_del + 1,
46128275SEric Cheng 				    strlen(new_name));
46138275SEric Cheng 			}
46148275SEric Cheng 			fep = cur_clnt->mci_flent;
46158275SEric Cheng 			mac_rename_flow(fep, cur_clnt->mci_name);
46168275SEric Cheng 			break;
46178275SEric Cheng 		} else if (new_name != NULL &&
46188275SEric Cheng 		    cur_clnt->mci_state_flags & MCIS_USE_DATALINK_NAME) {
46198275SEric Cheng 			mac_rename_flow_names(cur_clnt, new_name);
46208275SEric Cheng 			break;
46218275SEric Cheng 		}
46228275SEric Cheng 	}
46238275SEric Cheng 
4624*11878SVenu.Iyer@Sun.COM 	/* Recreate kstats associated with aggr pseudo rings */
4625*11878SVenu.Iyer@Sun.COM 	if (mip->mi_state_flags & MIS_IS_AGGR)
4626*11878SVenu.Iyer@Sun.COM 		mac_pseudo_ring_stat_rename(mip);
4627*11878SVenu.Iyer@Sun.COM 
46288275SEric Cheng done:
46298275SEric Cheng 	i_mac_perim_exit(mip);
46308275SEric Cheng 	return (0);
46318275SEric Cheng }
46328275SEric Cheng 
46338275SEric Cheng /*
46348275SEric Cheng  * Rename the MAC client's flow names
46358275SEric Cheng  */
46368275SEric Cheng static void
46378275SEric Cheng mac_rename_flow_names(mac_client_impl_t *mcip, const char *new_name)
46388275SEric Cheng {
46398275SEric Cheng 	flow_entry_t	*flent;
46408275SEric Cheng 	uint16_t	vid;
46418558SGirish.Moodalbail@Sun.COM 	char		flowname[MAXFLOWNAMELEN];
46428275SEric Cheng 	mac_impl_t	*mip = mcip->mci_mip;
46438275SEric Cheng 
46448275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
46458275SEric Cheng 
46468275SEric Cheng 	/*
46478275SEric Cheng 	 * Use mi_rw_lock to ensure that threads not in the mac perimeter
46488275SEric Cheng 	 * see a self-consistent value for mci_name
46498275SEric Cheng 	 */
46508275SEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
46518275SEric Cheng 	(void) strlcpy(mcip->mci_name, new_name, sizeof (mcip->mci_name));
46528275SEric Cheng 	rw_exit(&mip->mi_rw_lock);
46538275SEric Cheng 
46548275SEric Cheng 	mac_rename_flow(mcip->mci_flent, new_name);
46558275SEric Cheng 
46568275SEric Cheng 	if (mcip->mci_nflents == 1)
46578275SEric Cheng 		return;
46588275SEric Cheng 
46598275SEric Cheng 	/*
46608275SEric Cheng 	 * We have to rename all the others too, no stats to destroy for
46618275SEric Cheng 	 * these.
46628275SEric Cheng 	 */
46638275SEric Cheng 	for (flent = mcip->mci_flent_list; flent != NULL;
46648275SEric Cheng 	    flent = flent->fe_client_next) {
46658275SEric Cheng 		if (flent != mcip->mci_flent) {
46668275SEric Cheng 			vid = i_mac_flow_vid(flent);
46678275SEric Cheng 			(void) sprintf(flowname, "%s%u", new_name, vid);
46688275SEric Cheng 			mac_flow_set_name(flent, flowname);
46698275SEric Cheng 		}
46708275SEric Cheng 	}
46718275SEric Cheng }
46728275SEric Cheng 
46738275SEric Cheng 
46748275SEric Cheng /*
46758275SEric Cheng  * Add a flow to the MAC client's flow list - i.e list of MAC/VID tuples
46768275SEric Cheng  * defined for the specified MAC client.
46778275SEric Cheng  */
46788275SEric Cheng static void
46798275SEric Cheng mac_client_add_to_flow_list(mac_client_impl_t *mcip, flow_entry_t *flent)
46808275SEric Cheng {
46818275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
46828275SEric Cheng 	/*
46838275SEric Cheng 	 * The promisc Rx data path walks the mci_flent_list. Protect by
46848275SEric Cheng 	 * using mi_rw_lock
46858275SEric Cheng 	 */
46868275SEric Cheng 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
46878275SEric Cheng 
46888275SEric Cheng 	/* Add it to the head */
46898275SEric Cheng 	flent->fe_client_next = mcip->mci_flent_list;
46908275SEric Cheng 	mcip->mci_flent_list = flent;
46918275SEric Cheng 	mcip->mci_nflents++;
46928275SEric Cheng 
46938275SEric Cheng 	/*
46948275SEric Cheng 	 * Keep track of the number of non-zero VIDs addresses per MAC
46958275SEric Cheng 	 * client to avoid figuring it out in the data-path.
46968275SEric Cheng 	 */
46978275SEric Cheng 	if (i_mac_flow_vid(flent) != VLAN_ID_NONE)
46988275SEric Cheng 		mcip->mci_nvids++;
46998275SEric Cheng 
47008275SEric Cheng 	rw_exit(&mcip->mci_rw_lock);
47018275SEric Cheng }
47028275SEric Cheng 
47038275SEric Cheng /*
47048275SEric Cheng  * Remove a flow entry from the MAC client's list.
47058275SEric Cheng  */
47068275SEric Cheng static void
47078275SEric Cheng mac_client_remove_flow_from_list(mac_client_impl_t *mcip, flow_entry_t *flent)
47088275SEric Cheng {
47098275SEric Cheng 	flow_entry_t	*fe = mcip->mci_flent_list;
47108275SEric Cheng 	flow_entry_t	*prev_fe = NULL;
47118275SEric Cheng 
47128275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
47138275SEric Cheng 	/*
47148275SEric Cheng 	 * The promisc Rx data path walks the mci_flent_list. Protect by
47158275SEric Cheng 	 * using mci_rw_lock
47168275SEric Cheng 	 */
47178275SEric Cheng 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
47188275SEric Cheng 	while ((fe != NULL) && (fe != flent)) {
47198275SEric Cheng 		prev_fe = fe;
47208275SEric Cheng 		fe = fe->fe_client_next;
47218275SEric Cheng 	}
47228275SEric Cheng 
47238558SGirish.Moodalbail@Sun.COM 	ASSERT(fe != NULL);
47248558SGirish.Moodalbail@Sun.COM 	if (prev_fe == NULL) {
47258558SGirish.Moodalbail@Sun.COM 		/* Deleting the first node */
47268558SGirish.Moodalbail@Sun.COM 		mcip->mci_flent_list = fe->fe_client_next;
47278558SGirish.Moodalbail@Sun.COM 	} else {
47288558SGirish.Moodalbail@Sun.COM 		prev_fe->fe_client_next = fe->fe_client_next;
47298275SEric Cheng 	}
47308558SGirish.Moodalbail@Sun.COM 	mcip->mci_nflents--;
47318558SGirish.Moodalbail@Sun.COM 
47328558SGirish.Moodalbail@Sun.COM 	if (i_mac_flow_vid(flent) != VLAN_ID_NONE)
47338558SGirish.Moodalbail@Sun.COM 		mcip->mci_nvids--;
47348558SGirish.Moodalbail@Sun.COM 
47358275SEric Cheng 	rw_exit(&mcip->mci_rw_lock);
47368275SEric Cheng }
47378275SEric Cheng 
47388275SEric Cheng /*
47398275SEric Cheng  * Check if the given VID belongs to this MAC client.
47408275SEric Cheng  */
47418275SEric Cheng boolean_t
47428275SEric Cheng mac_client_check_flow_vid(mac_client_impl_t *mcip, uint16_t vid)
47438275SEric Cheng {
47448275SEric Cheng 	flow_entry_t	*flent;
47458275SEric Cheng 	uint16_t	mci_vid;
47468275SEric Cheng 
47478275SEric Cheng 	/* The mci_flent_list is protected by mci_rw_lock */
47488275SEric Cheng 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
47498275SEric Cheng 	for (flent = mcip->mci_flent_list; flent != NULL;
47508275SEric Cheng 	    flent = flent->fe_client_next) {
47518275SEric Cheng 		mci_vid = i_mac_flow_vid(flent);
47528275SEric Cheng 		if (vid == mci_vid) {
47538275SEric Cheng 			rw_exit(&mcip->mci_rw_lock);
47548275SEric Cheng 			return (B_TRUE);
47558275SEric Cheng 		}
47568275SEric Cheng 	}
47578275SEric Cheng 	rw_exit(&mcip->mci_rw_lock);
47588275SEric Cheng 	return (B_FALSE);
47598275SEric Cheng }
47608275SEric Cheng 
47618275SEric Cheng /*
47628275SEric Cheng  * Get the flow entry for the specified <MAC addr, VID> tuple.
47638275SEric Cheng  */
47648275SEric Cheng static flow_entry_t *
47658275SEric Cheng mac_client_get_flow(mac_client_impl_t *mcip, mac_unicast_impl_t *muip)
47668275SEric Cheng {
47678275SEric Cheng 	mac_address_t *map = mcip->mci_unicast;
47688275SEric Cheng 	flow_entry_t *flent;
47698275SEric Cheng 	uint16_t vid;
47708275SEric Cheng 	flow_desc_t flow_desc;
47718275SEric Cheng 
47728275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
47738275SEric Cheng 
47748275SEric Cheng 	mac_flow_get_desc(mcip->mci_flent, &flow_desc);
47758275SEric Cheng 	if (bcmp(flow_desc.fd_dst_mac, map->ma_addr, map->ma_len) != 0)
47768275SEric Cheng 		return (NULL);
47778275SEric Cheng 
47788275SEric Cheng 	for (flent = mcip->mci_flent_list; flent != NULL;
47798275SEric Cheng 	    flent = flent->fe_client_next) {
47808275SEric Cheng 		vid = i_mac_flow_vid(flent);
47818275SEric Cheng 		if (vid == muip->mui_vid) {
47828275SEric Cheng 			return (flent);
47838275SEric Cheng 		}
47848275SEric Cheng 	}
47858275SEric Cheng 
47868275SEric Cheng 	return (NULL);
47878275SEric Cheng }
47888275SEric Cheng 
47898275SEric Cheng /*
47908275SEric Cheng  * Since mci_flent has the SRSs, when we want to remove it, we replace
47918275SEric Cheng  * the flow_desc_t in mci_flent with that of an existing flent and then
47928275SEric Cheng  * remove that flent instead of mci_flent.
47938275SEric Cheng  */
47948275SEric Cheng static flow_entry_t *
47958275SEric Cheng mac_client_swap_mciflent(mac_client_impl_t *mcip)
47968275SEric Cheng {
47978275SEric Cheng 	flow_entry_t	*flent = mcip->mci_flent;
47988275SEric Cheng 	flow_tab_t	*ft = flent->fe_flow_tab;
47998275SEric Cheng 	flow_entry_t	*flent1;
48008275SEric Cheng 	flow_desc_t	fl_desc;
48018558SGirish.Moodalbail@Sun.COM 	char		fl_name[MAXFLOWNAMELEN];
48028275SEric Cheng 	int		err;
48038275SEric Cheng 
48048275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
48058275SEric Cheng 	ASSERT(mcip->mci_nflents > 1);
48068275SEric Cheng 
48078275SEric Cheng 	/* get the next flent following the primary flent  */
48088275SEric Cheng 	flent1 = mcip->mci_flent_list->fe_client_next;
48098275SEric Cheng 	ASSERT(flent1 != NULL && flent1->fe_flow_tab == ft);
48108275SEric Cheng 
48118275SEric Cheng 	/*
48128275SEric Cheng 	 * Remove the flent from the flow table before updating the
48138275SEric Cheng 	 * flow descriptor as the hash depends on the flow descriptor.
48148275SEric Cheng 	 * This also helps incoming packet classification avoid having
48158275SEric Cheng 	 * to grab fe_lock. Access to fe_flow_desc of a flent not in the
48168275SEric Cheng 	 * flow table is done under the fe_lock so that log or stat functions
48178275SEric Cheng 	 * see a self-consistent fe_flow_desc. The name and desc are specific
48188275SEric Cheng 	 * to a flow, the rest are shared by all the clients, including
48198275SEric Cheng 	 * resource control etc.
48208275SEric Cheng 	 */
48218275SEric Cheng 	mac_flow_remove(ft, flent, B_TRUE);
48228275SEric Cheng 	mac_flow_remove(ft, flent1, B_TRUE);
48238275SEric Cheng 
48248275SEric Cheng 	bcopy(&flent->fe_flow_desc, &fl_desc, sizeof (flow_desc_t));
48258558SGirish.Moodalbail@Sun.COM 	bcopy(flent->fe_flow_name, fl_name, MAXFLOWNAMELEN);
48268275SEric Cheng 
48278275SEric Cheng 	/* update the primary flow entry */
48288275SEric Cheng 	mutex_enter(&flent->fe_lock);
48298275SEric Cheng 	bcopy(&flent1->fe_flow_desc, &flent->fe_flow_desc,
48308275SEric Cheng 	    sizeof (flow_desc_t));
48318558SGirish.Moodalbail@Sun.COM 	bcopy(&flent1->fe_flow_name, &flent->fe_flow_name, MAXFLOWNAMELEN);
48328275SEric Cheng 	mutex_exit(&flent->fe_lock);
48338275SEric Cheng 
48348275SEric Cheng 	/* update the flow entry that is to be freed */
48358275SEric Cheng 	mutex_enter(&flent1->fe_lock);
48368275SEric Cheng 	bcopy(&fl_desc, &flent1->fe_flow_desc, sizeof (flow_desc_t));
48378558SGirish.Moodalbail@Sun.COM 	bcopy(fl_name, &flent1->fe_flow_name, MAXFLOWNAMELEN);
48388275SEric Cheng 	mutex_exit(&flent1->fe_lock);
48398275SEric Cheng 
48408275SEric Cheng 	/* now reinsert the flow entries in the table */
48418275SEric Cheng 	err = mac_flow_add(ft, flent);
48428275SEric Cheng 	ASSERT(err == 0);
48438275SEric Cheng 
48448275SEric Cheng 	err = mac_flow_add(ft, flent1);
48458275SEric Cheng 	ASSERT(err == 0);
48468275SEric Cheng 
48478275SEric Cheng 	return (flent1);
48488275SEric Cheng }
48498275SEric Cheng 
48508275SEric Cheng /*
48518275SEric Cheng  * Return whether there is only one flow entry associated with this
48528275SEric Cheng  * MAC client.
48538275SEric Cheng  */
48548275SEric Cheng static boolean_t
48558275SEric Cheng mac_client_single_rcvr(mac_client_impl_t *mcip)
48568275SEric Cheng {
48578275SEric Cheng 	return (mcip->mci_nflents == 1);
48588275SEric Cheng }
48598275SEric Cheng 
48608275SEric Cheng int
4861*11878SVenu.Iyer@Sun.COM mac_validate_props(mac_impl_t *mip, mac_resource_props_t *mrp)
48628275SEric Cheng {
4863*11878SVenu.Iyer@Sun.COM 	boolean_t		reset;
4864*11878SVenu.Iyer@Sun.COM 	uint32_t		rings_needed;
4865*11878SVenu.Iyer@Sun.COM 	uint32_t		rings_avail;
4866*11878SVenu.Iyer@Sun.COM 	mac_group_type_t	gtype;
4867*11878SVenu.Iyer@Sun.COM 	mac_resource_props_t	*mip_mrp;
4868*11878SVenu.Iyer@Sun.COM 
48698275SEric Cheng 	if (mrp == NULL)
48708275SEric Cheng 		return (0);
48718275SEric Cheng 
48728275SEric Cheng 	if (mrp->mrp_mask & MRP_PRIORITY) {
48738275SEric Cheng 		mac_priority_level_t	pri = mrp->mrp_priority;
48748275SEric Cheng 
48758275SEric Cheng 		if (pri < MPL_LOW || pri > MPL_RESET)
48768275SEric Cheng 			return (EINVAL);
48778275SEric Cheng 	}
48788275SEric Cheng 
48798275SEric Cheng 	if (mrp->mrp_mask & MRP_MAXBW) {
48808275SEric Cheng 		uint64_t maxbw = mrp->mrp_maxbw;
48818275SEric Cheng 
48828275SEric Cheng 		if (maxbw < MRP_MAXBW_MINVAL && maxbw != 0)
48838275SEric Cheng 			return (EINVAL);
48848275SEric Cheng 	}
48858275SEric Cheng 	if (mrp->mrp_mask & MRP_CPUS) {
48869060SNitin.Hande@Sun.COM 		int i, j;
48878275SEric Cheng 		mac_cpu_mode_t	fanout;
48888275SEric Cheng 
48898275SEric Cheng 		if (mrp->mrp_ncpus > ncpus || mrp->mrp_ncpus > MAX_SR_FANOUT)
48908275SEric Cheng 			return (EINVAL);
48918275SEric Cheng 
48928275SEric Cheng 		for (i = 0; i < mrp->mrp_ncpus; i++) {
48939060SNitin.Hande@Sun.COM 			for (j = 0; j < mrp->mrp_ncpus; j++) {
48949060SNitin.Hande@Sun.COM 				if (i != j &&
48959060SNitin.Hande@Sun.COM 				    mrp->mrp_cpu[i] == mrp->mrp_cpu[j]) {
48969060SNitin.Hande@Sun.COM 					return (EINVAL);
48979060SNitin.Hande@Sun.COM 				}
48989060SNitin.Hande@Sun.COM 			}
48999060SNitin.Hande@Sun.COM 		}
49009060SNitin.Hande@Sun.COM 
49019060SNitin.Hande@Sun.COM 		for (i = 0; i < mrp->mrp_ncpus; i++) {
49028275SEric Cheng 			cpu_t *cp;
49038275SEric Cheng 			int rv;
49048275SEric Cheng 
49058275SEric Cheng 			mutex_enter(&cpu_lock);
49068275SEric Cheng 			cp = cpu_get(mrp->mrp_cpu[i]);
49078275SEric Cheng 			if (cp != NULL)
49088275SEric Cheng 				rv = cpu_is_online(cp);
49098275SEric Cheng 			else
49108275SEric Cheng 				rv = 0;
49118275SEric Cheng 			mutex_exit(&cpu_lock);
49128275SEric Cheng 			if (rv == 0)
49138275SEric Cheng 				return (EINVAL);
49148275SEric Cheng 		}
49158275SEric Cheng 
49168275SEric Cheng 		fanout = mrp->mrp_fanout_mode;
49178275SEric Cheng 		if (fanout < 0 || fanout > MCM_CPUS)
49188275SEric Cheng 			return (EINVAL);
49198275SEric Cheng 	}
492010734SEric Cheng 
492110734SEric Cheng 	if (mrp->mrp_mask & MRP_PROTECT) {
492210734SEric Cheng 		int err = mac_protect_validate(mrp);
492310734SEric Cheng 		if (err != 0)
492410734SEric Cheng 			return (err);
492510734SEric Cheng 	}
4926*11878SVenu.Iyer@Sun.COM 
4927*11878SVenu.Iyer@Sun.COM 	if (!(mrp->mrp_mask & MRP_RX_RINGS) &&
4928*11878SVenu.Iyer@Sun.COM 	    !(mrp->mrp_mask & MRP_TX_RINGS)) {
4929*11878SVenu.Iyer@Sun.COM 		return (0);
4930*11878SVenu.Iyer@Sun.COM 	}
4931*11878SVenu.Iyer@Sun.COM 
4932*11878SVenu.Iyer@Sun.COM 	/*
4933*11878SVenu.Iyer@Sun.COM 	 * mip will be null when we come from mac_flow_create or
4934*11878SVenu.Iyer@Sun.COM 	 * mac_link_flow_modify. In the latter case it is a user flow,
4935*11878SVenu.Iyer@Sun.COM 	 * for which we don't support rings. In the former we would
4936*11878SVenu.Iyer@Sun.COM 	 * have validated the props beforehand (i_mac_unicast_add ->
4937*11878SVenu.Iyer@Sun.COM 	 * mac_client_set_resources -> validate for the primary and
4938*11878SVenu.Iyer@Sun.COM 	 * vnic_dev_create -> mac_client_set_resources -> validate for
4939*11878SVenu.Iyer@Sun.COM 	 * a vnic.
4940*11878SVenu.Iyer@Sun.COM 	 */
4941*11878SVenu.Iyer@Sun.COM 	if (mip == NULL)
4942*11878SVenu.Iyer@Sun.COM 		return (0);
4943*11878SVenu.Iyer@Sun.COM 
4944*11878SVenu.Iyer@Sun.COM 	/*
4945*11878SVenu.Iyer@Sun.COM 	 * We don't support setting rings property for a VNIC that is using a
4946*11878SVenu.Iyer@Sun.COM 	 * primary address (VLAN)
4947*11878SVenu.Iyer@Sun.COM 	 */
4948*11878SVenu.Iyer@Sun.COM 	if ((mip->mi_state_flags & MIS_IS_VNIC) &&
4949*11878SVenu.Iyer@Sun.COM 	    mac_is_vnic_primary((mac_handle_t)mip)) {
4950*11878SVenu.Iyer@Sun.COM 		return (ENOTSUP);
4951*11878SVenu.Iyer@Sun.COM 	}
4952*11878SVenu.Iyer@Sun.COM 
4953*11878SVenu.Iyer@Sun.COM 	mip_mrp = &mip->mi_resource_props;
4954*11878SVenu.Iyer@Sun.COM 	/*
4955*11878SVenu.Iyer@Sun.COM 	 * The rings property should be validated against the NICs
4956*11878SVenu.Iyer@Sun.COM 	 * resources
4957*11878SVenu.Iyer@Sun.COM 	 */
4958*11878SVenu.Iyer@Sun.COM 	if (mip->mi_state_flags & MIS_IS_VNIC)
4959*11878SVenu.Iyer@Sun.COM 		mip = (mac_impl_t *)mac_get_lower_mac_handle((mac_handle_t)mip);
4960*11878SVenu.Iyer@Sun.COM 
4961*11878SVenu.Iyer@Sun.COM 	reset = mrp->mrp_mask & MRP_RINGS_RESET;
4962*11878SVenu.Iyer@Sun.COM 	/*
4963*11878SVenu.Iyer@Sun.COM 	 * If groups are not supported, return error.
4964*11878SVenu.Iyer@Sun.COM 	 */
4965*11878SVenu.Iyer@Sun.COM 	if (((mrp->mrp_mask & MRP_RX_RINGS) && mip->mi_rx_groups == NULL) ||
4966*11878SVenu.Iyer@Sun.COM 	    ((mrp->mrp_mask & MRP_TX_RINGS) && mip->mi_tx_groups == NULL)) {
4967*11878SVenu.Iyer@Sun.COM 		return (EINVAL);
4968*11878SVenu.Iyer@Sun.COM 	}
4969*11878SVenu.Iyer@Sun.COM 	/*
4970*11878SVenu.Iyer@Sun.COM 	 * If we are just resetting, there is no validation needed.
4971*11878SVenu.Iyer@Sun.COM 	 */
4972*11878SVenu.Iyer@Sun.COM 	if (reset)
4973*11878SVenu.Iyer@Sun.COM 		return (0);
4974*11878SVenu.Iyer@Sun.COM 
4975*11878SVenu.Iyer@Sun.COM 	if (mrp->mrp_mask & MRP_RX_RINGS) {
4976*11878SVenu.Iyer@Sun.COM 		rings_needed = mrp->mrp_nrxrings;
4977*11878SVenu.Iyer@Sun.COM 		/*
4978*11878SVenu.Iyer@Sun.COM 		 * We just want to check if the number of additional
4979*11878SVenu.Iyer@Sun.COM 		 * rings requested is available.
4980*11878SVenu.Iyer@Sun.COM 		 */
4981*11878SVenu.Iyer@Sun.COM 		if (mip_mrp->mrp_mask & MRP_RX_RINGS) {
4982*11878SVenu.Iyer@Sun.COM 			if (mrp->mrp_nrxrings > mip_mrp->mrp_nrxrings)
4983*11878SVenu.Iyer@Sun.COM 				/* Just check for the additional rings */
4984*11878SVenu.Iyer@Sun.COM 				rings_needed -= mip_mrp->mrp_nrxrings;
4985*11878SVenu.Iyer@Sun.COM 			else
4986*11878SVenu.Iyer@Sun.COM 				/* We are not asking for additional rings */
4987*11878SVenu.Iyer@Sun.COM 				rings_needed = 0;
4988*11878SVenu.Iyer@Sun.COM 		}
4989*11878SVenu.Iyer@Sun.COM 		rings_avail = mip->mi_rxrings_avail;
4990*11878SVenu.Iyer@Sun.COM 		gtype = mip->mi_rx_group_type;
4991*11878SVenu.Iyer@Sun.COM 	} else {
4992*11878SVenu.Iyer@Sun.COM 		rings_needed = mrp->mrp_ntxrings;
4993*11878SVenu.Iyer@Sun.COM 		/* Similarly for the TX rings */
4994*11878SVenu.Iyer@Sun.COM 		if (mip_mrp->mrp_mask & MRP_TX_RINGS) {
4995*11878SVenu.Iyer@Sun.COM 			if (mrp->mrp_ntxrings > mip_mrp->mrp_ntxrings)
4996*11878SVenu.Iyer@Sun.COM 				/* Just check for the additional rings */
4997*11878SVenu.Iyer@Sun.COM 				rings_needed -= mip_mrp->mrp_ntxrings;
4998*11878SVenu.Iyer@Sun.COM 			else
4999*11878SVenu.Iyer@Sun.COM 				/* We are not asking for additional rings */
5000*11878SVenu.Iyer@Sun.COM 				rings_needed = 0;
5001*11878SVenu.Iyer@Sun.COM 		}
5002*11878SVenu.Iyer@Sun.COM 		rings_avail = mip->mi_txrings_avail;
5003*11878SVenu.Iyer@Sun.COM 		gtype = mip->mi_tx_group_type;
5004*11878SVenu.Iyer@Sun.COM 	}
5005*11878SVenu.Iyer@Sun.COM 
5006*11878SVenu.Iyer@Sun.COM 	/* Error if the group is dynamic .. */
5007*11878SVenu.Iyer@Sun.COM 	if (gtype == MAC_GROUP_TYPE_DYNAMIC) {
5008*11878SVenu.Iyer@Sun.COM 		/*
5009*11878SVenu.Iyer@Sun.COM 		 * .. and rings specified are more than available.
5010*11878SVenu.Iyer@Sun.COM 		 */
5011*11878SVenu.Iyer@Sun.COM 		if (rings_needed > rings_avail)
5012*11878SVenu.Iyer@Sun.COM 			return (EINVAL);
5013*11878SVenu.Iyer@Sun.COM 	} else {
5014*11878SVenu.Iyer@Sun.COM 		/*
5015*11878SVenu.Iyer@Sun.COM 		 * OR group is static and we have specified some rings.
5016*11878SVenu.Iyer@Sun.COM 		 */
5017*11878SVenu.Iyer@Sun.COM 		if (rings_needed > 0)
5018*11878SVenu.Iyer@Sun.COM 			return (EINVAL);
5019*11878SVenu.Iyer@Sun.COM 	}
50208275SEric Cheng 	return (0);
50218275SEric Cheng }
50228275SEric Cheng 
50238275SEric Cheng /*
50248275SEric Cheng  * Send a MAC_NOTE_LINK notification to all the MAC clients whenever the
50258275SEric Cheng  * underlying physical link is down. This is to allow MAC clients to
50268275SEric Cheng  * communicate with other clients.
50278275SEric Cheng  */
50288275SEric Cheng void
50298275SEric Cheng mac_virtual_link_update(mac_impl_t *mip)
50308275SEric Cheng {
50318275SEric Cheng 	if (mip->mi_linkstate != LINK_STATE_UP)
50328275SEric Cheng 		i_mac_notify(mip, MAC_NOTE_LINK);
50338275SEric Cheng }
50348275SEric Cheng 
50358275SEric Cheng /*
50368275SEric Cheng  * For clients that have a pass-thru MAC, e.g. VNIC, we set the VNIC's
50378275SEric Cheng  * mac handle in the client.
50388275SEric Cheng  */
50398275SEric Cheng void
5040*11878SVenu.Iyer@Sun.COM mac_set_upper_mac(mac_client_handle_t mch, mac_handle_t mh,
5041*11878SVenu.Iyer@Sun.COM     mac_resource_props_t *mrp)
50428275SEric Cheng {
50438275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
5044*11878SVenu.Iyer@Sun.COM 	mac_impl_t		*mip = (mac_impl_t *)mh;
5045*11878SVenu.Iyer@Sun.COM 
5046*11878SVenu.Iyer@Sun.COM 	mcip->mci_upper_mip = mip;
5047*11878SVenu.Iyer@Sun.COM 	/* If there are any properties, copy it over too */
5048*11878SVenu.Iyer@Sun.COM 	if (mrp != NULL) {
5049*11878SVenu.Iyer@Sun.COM 		bcopy(mrp, &mip->mi_resource_props,
5050*11878SVenu.Iyer@Sun.COM 		    sizeof (mac_resource_props_t));
5051*11878SVenu.Iyer@Sun.COM 	}
50528275SEric Cheng }
50538275SEric Cheng 
50548275SEric Cheng /*
50558275SEric Cheng  * Mark the mac as being used exclusively by the single mac client that is
50568275SEric Cheng  * doing some control operation on this mac. No further opens of this mac
50578275SEric Cheng  * will be allowed until this client calls mac_unmark_exclusive. The mac
50588275SEric Cheng  * client calling this function must already be in the mac perimeter
50598275SEric Cheng  */
50608275SEric Cheng int
50618275SEric Cheng mac_mark_exclusive(mac_handle_t mh)
50628275SEric Cheng {
50638275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
50648275SEric Cheng 
50658275SEric Cheng 	ASSERT(MAC_PERIM_HELD(mh));
50668275SEric Cheng 	/*
50678275SEric Cheng 	 * Look up its entry in the global hash table.
50688275SEric Cheng 	 */
50698275SEric Cheng 	rw_enter(&i_mac_impl_lock, RW_WRITER);
50708275SEric Cheng 	if (mip->mi_state_flags & MIS_DISABLED) {
50718275SEric Cheng 		rw_exit(&i_mac_impl_lock);
50728275SEric Cheng 		return (ENOENT);
50738275SEric Cheng 	}
50748275SEric Cheng 
50758275SEric Cheng 	/*
50768275SEric Cheng 	 * A reference to mac is held even if the link is not plumbed.
50778275SEric Cheng 	 * In i_dls_link_create() we open the MAC interface and hold the
50788275SEric Cheng 	 * reference. There is an additional reference for the mac_open
50798275SEric Cheng 	 * done in acquiring the mac perimeter
50808275SEric Cheng 	 */
50818275SEric Cheng 	if (mip->mi_ref != 2) {
50828275SEric Cheng 		rw_exit(&i_mac_impl_lock);
50838275SEric Cheng 		return (EBUSY);
50848275SEric Cheng 	}
50858275SEric Cheng 
50868275SEric Cheng 	ASSERT(!(mip->mi_state_flags & MIS_EXCLUSIVE_HELD));
50878275SEric Cheng 	mip->mi_state_flags |= MIS_EXCLUSIVE_HELD;
50888275SEric Cheng 	rw_exit(&i_mac_impl_lock);
50898275SEric Cheng 	return (0);
50908275SEric Cheng }
50918275SEric Cheng 
50928275SEric Cheng void
50938275SEric Cheng mac_unmark_exclusive(mac_handle_t mh)
50948275SEric Cheng {
50958275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
50968275SEric Cheng 
50978275SEric Cheng 	ASSERT(MAC_PERIM_HELD(mh));
50988275SEric Cheng 
50998275SEric Cheng 	rw_enter(&i_mac_impl_lock, RW_WRITER);
51008275SEric Cheng 	/* 1 for the creation and another for the perimeter */
51018275SEric Cheng 	ASSERT(mip->mi_ref == 2 && (mip->mi_state_flags & MIS_EXCLUSIVE_HELD));
51028275SEric Cheng 	mip->mi_state_flags &= ~MIS_EXCLUSIVE_HELD;
51038275SEric Cheng 	rw_exit(&i_mac_impl_lock);
51048275SEric Cheng }
51058275SEric Cheng 
51068275SEric Cheng /*
5107*11878SVenu.Iyer@Sun.COM  * Set the MTU for the specified MAC.
51088275SEric Cheng  */
51098275SEric Cheng int
51108275SEric Cheng mac_set_mtu(mac_handle_t mh, uint_t new_mtu, uint_t *old_mtu_arg)
51118275SEric Cheng {
51128275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
51138275SEric Cheng 	uint_t old_mtu;
511410674SSebastien.Roy@Sun.COM 	int rv = 0;
51158275SEric Cheng 
51168275SEric Cheng 	i_mac_perim_enter(mip);
51178275SEric Cheng 
511810616SSebastien.Roy@Sun.COM 	if (!(mip->mi_callbacks->mc_callbacks & (MC_SETPROP|MC_GETPROP))) {
51198275SEric Cheng 		rv = ENOTSUP;
51208275SEric Cheng 		goto bail;
51218275SEric Cheng 	}
51228275SEric Cheng 
512310616SSebastien.Roy@Sun.COM 	old_mtu = mip->mi_sdu_max;
51248275SEric Cheng 
5125*11878SVenu.Iyer@Sun.COM 	if (new_mtu == 0 || new_mtu < mip->mi_sdu_min) {
5126*11878SVenu.Iyer@Sun.COM 		rv = EINVAL;
5127*11878SVenu.Iyer@Sun.COM 		goto bail;
5128*11878SVenu.Iyer@Sun.COM 	}
5129*11878SVenu.Iyer@Sun.COM 
51308275SEric Cheng 	if (old_mtu != new_mtu) {
51318275SEric Cheng 		rv = mip->mi_callbacks->mc_setprop(mip->mi_driver,
51328275SEric Cheng 		    "mtu", MAC_PROP_MTU, sizeof (uint_t), &new_mtu);
5133*11878SVenu.Iyer@Sun.COM 		if (rv != 0)
5134*11878SVenu.Iyer@Sun.COM 			goto bail;
5135*11878SVenu.Iyer@Sun.COM 		rv = mac_maxsdu_update(mh, new_mtu);
5136*11878SVenu.Iyer@Sun.COM 		ASSERT(rv == 0);
51378275SEric Cheng 	}
51388275SEric Cheng 
51398275SEric Cheng bail:
51408275SEric Cheng 	i_mac_perim_exit(mip);
51418275SEric Cheng 
51428275SEric Cheng 	if (rv == 0 && old_mtu_arg != NULL)
51438275SEric Cheng 		*old_mtu_arg = old_mtu;
51448275SEric Cheng 	return (rv);
51458275SEric Cheng }
51468275SEric Cheng 
5147*11878SVenu.Iyer@Sun.COM /*
5148*11878SVenu.Iyer@Sun.COM  * Return the RX h/w information for the group indexed by grp_num.
5149*11878SVenu.Iyer@Sun.COM  */
51508275SEric Cheng void
5151*11878SVenu.Iyer@Sun.COM mac_get_hwrxgrp_info(mac_handle_t mh, int grp_index, uint_t *grp_num,
5152*11878SVenu.Iyer@Sun.COM     uint_t *n_rings, uint_t *rings, uint_t *type, uint_t *n_clnts,
5153*11878SVenu.Iyer@Sun.COM     char *clnts_name)
51548275SEric Cheng {
51558275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
51568275SEric Cheng 	mac_grp_client_t *mcip;
51578275SEric Cheng 	uint_t i = 0, index = 0;
5158*11878SVenu.Iyer@Sun.COM 	mac_ring_t	*ring;
51598275SEric Cheng 
51608275SEric Cheng 	/* Revisit when we implement fully dynamic group allocation */
51618275SEric Cheng 	ASSERT(grp_index >= 0 && grp_index < mip->mi_rx_group_count);
51628275SEric Cheng 
51638275SEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_READER);
51648275SEric Cheng 	*grp_num = mip->mi_rx_groups[grp_index].mrg_index;
51658275SEric Cheng 	*type = mip->mi_rx_groups[grp_index].mrg_type;
51668275SEric Cheng 	*n_rings = mip->mi_rx_groups[grp_index].mrg_cur_count;
5167*11878SVenu.Iyer@Sun.COM 	ring = mip->mi_rx_groups[grp_index].mrg_rings;
5168*11878SVenu.Iyer@Sun.COM 	for (index = 0; index < mip->mi_rx_groups[grp_index].mrg_cur_count;
5169*11878SVenu.Iyer@Sun.COM 	    index++) {
5170*11878SVenu.Iyer@Sun.COM 		rings[index] = ring->mr_index;
5171*11878SVenu.Iyer@Sun.COM 		ring = ring->mr_next;
5172*11878SVenu.Iyer@Sun.COM 	}
5173*11878SVenu.Iyer@Sun.COM 	/* Assuming the 1st is the default group */
5174*11878SVenu.Iyer@Sun.COM 	index = 0;
5175*11878SVenu.Iyer@Sun.COM 	if (grp_index == 0) {
5176*11878SVenu.Iyer@Sun.COM 		(void) strlcpy(clnts_name, "<default,mcast>,",
5177*11878SVenu.Iyer@Sun.COM 		    MAXCLIENTNAMELEN);
5178*11878SVenu.Iyer@Sun.COM 		index += strlen("<default,mcast>,");
5179*11878SVenu.Iyer@Sun.COM 	}
51808275SEric Cheng 	for (mcip = mip->mi_rx_groups[grp_index].mrg_clients; mcip != NULL;
51818275SEric Cheng 	    mcip = mcip->mgc_next) {
51828275SEric Cheng 		int name_len = strlen(mcip->mgc_client->mci_name);
51838275SEric Cheng 
51848275SEric Cheng 		/*
51858275SEric Cheng 		 * MAXCLIENTNAMELEN is the buffer size reserved for client
51868275SEric Cheng 		 * names.
51878275SEric Cheng 		 * XXXX Formating the client name string needs to be moved
51888275SEric Cheng 		 * to user land when fixing the size of dhi_clnts in
51898275SEric Cheng 		 * dld_hwgrpinfo_t. We should use n_clients * client_name for
51908275SEric Cheng 		 * dhi_clntsin instead of MAXCLIENTNAMELEN
51918275SEric Cheng 		 */
51928275SEric Cheng 		if (index + name_len >= MAXCLIENTNAMELEN) {
51938275SEric Cheng 			index = MAXCLIENTNAMELEN;
51948275SEric Cheng 			break;
51958275SEric Cheng 		}
51968275SEric Cheng 		bcopy(mcip->mgc_client->mci_name, &(clnts_name[index]),
51978275SEric Cheng 		    name_len);
51988275SEric Cheng 		index += name_len;
51998275SEric Cheng 		clnts_name[index++] = ',';
52008275SEric Cheng 		i++;
52018275SEric Cheng 	}
52028275SEric Cheng 
52038275SEric Cheng 	/* Get rid of the last , */
52048275SEric Cheng 	if (index > 0)
52058275SEric Cheng 		clnts_name[index - 1] = '\0';
52068275SEric Cheng 	*n_clnts = i;
52078275SEric Cheng 	rw_exit(&mip->mi_rw_lock);
52088275SEric Cheng }
52098275SEric Cheng 
5210*11878SVenu.Iyer@Sun.COM /*
5211*11878SVenu.Iyer@Sun.COM  * Return the TX h/w information for the group indexed by grp_num.
5212*11878SVenu.Iyer@Sun.COM  */
5213*11878SVenu.Iyer@Sun.COM void
5214*11878SVenu.Iyer@Sun.COM mac_get_hwtxgrp_info(mac_handle_t mh, int grp_index, uint_t *grp_num,
5215*11878SVenu.Iyer@Sun.COM     uint_t *n_rings, uint_t *rings, uint_t *type, uint_t *n_clnts,
5216*11878SVenu.Iyer@Sun.COM     char *clnts_name)
5217*11878SVenu.Iyer@Sun.COM {
5218*11878SVenu.Iyer@Sun.COM 	mac_impl_t *mip = (mac_impl_t *)mh;
5219*11878SVenu.Iyer@Sun.COM 	mac_grp_client_t *mcip;
5220*11878SVenu.Iyer@Sun.COM 	uint_t i = 0, index = 0;
5221*11878SVenu.Iyer@Sun.COM 	mac_ring_t	*ring;
5222*11878SVenu.Iyer@Sun.COM 
5223*11878SVenu.Iyer@Sun.COM 	/* Revisit when we implement fully dynamic group allocation */
5224*11878SVenu.Iyer@Sun.COM 	ASSERT(grp_index >= 0 && grp_index <= mip->mi_tx_group_count);
5225*11878SVenu.Iyer@Sun.COM 
5226*11878SVenu.Iyer@Sun.COM 	rw_enter(&mip->mi_rw_lock, RW_READER);
5227*11878SVenu.Iyer@Sun.COM 	*grp_num = mip->mi_tx_groups[grp_index].mrg_index > 0 ?
5228*11878SVenu.Iyer@Sun.COM 	    mip->mi_tx_groups[grp_index].mrg_index : grp_index;
5229*11878SVenu.Iyer@Sun.COM 	*type = mip->mi_tx_groups[grp_index].mrg_type;
5230*11878SVenu.Iyer@Sun.COM 	*n_rings = mip->mi_tx_groups[grp_index].mrg_cur_count;
5231*11878SVenu.Iyer@Sun.COM 	ring = mip->mi_tx_groups[grp_index].mrg_rings;
5232*11878SVenu.Iyer@Sun.COM 	for (index = 0; index < mip->mi_tx_groups[grp_index].mrg_cur_count;
5233*11878SVenu.Iyer@Sun.COM 	    index++) {
5234*11878SVenu.Iyer@Sun.COM 		rings[index] = ring->mr_index;
5235*11878SVenu.Iyer@Sun.COM 		ring = ring->mr_next;
5236*11878SVenu.Iyer@Sun.COM 	}
5237*11878SVenu.Iyer@Sun.COM 	index = 0;
5238*11878SVenu.Iyer@Sun.COM 	/* Default group has an index of -1 */
5239*11878SVenu.Iyer@Sun.COM 	if (mip->mi_tx_groups[grp_index].mrg_index < 0) {
5240*11878SVenu.Iyer@Sun.COM 		(void) strlcpy(clnts_name, "<default>,",
5241*11878SVenu.Iyer@Sun.COM 		    MAXCLIENTNAMELEN);
5242*11878SVenu.Iyer@Sun.COM 		index += strlen("<default>,");
5243*11878SVenu.Iyer@Sun.COM 	}
5244*11878SVenu.Iyer@Sun.COM 	for (mcip = mip->mi_tx_groups[grp_index].mrg_clients; mcip != NULL;
5245*11878SVenu.Iyer@Sun.COM 	    mcip = mcip->mgc_next) {
5246*11878SVenu.Iyer@Sun.COM 		int name_len = strlen(mcip->mgc_client->mci_name);
5247*11878SVenu.Iyer@Sun.COM 
5248*11878SVenu.Iyer@Sun.COM 		/*
5249*11878SVenu.Iyer@Sun.COM 		 * MAXCLIENTNAMELEN is the buffer size reserved for client
5250*11878SVenu.Iyer@Sun.COM 		 * names.
5251*11878SVenu.Iyer@Sun.COM 		 * XXXX Formating the client name string needs to be moved
5252*11878SVenu.Iyer@Sun.COM 		 * to user land when fixing the size of dhi_clnts in
5253*11878SVenu.Iyer@Sun.COM 		 * dld_hwgrpinfo_t. We should use n_clients * client_name for
5254*11878SVenu.Iyer@Sun.COM 		 * dhi_clntsin instead of MAXCLIENTNAMELEN
5255*11878SVenu.Iyer@Sun.COM 		 */
5256*11878SVenu.Iyer@Sun.COM 		if (index + name_len >= MAXCLIENTNAMELEN) {
5257*11878SVenu.Iyer@Sun.COM 			index = MAXCLIENTNAMELEN;
5258*11878SVenu.Iyer@Sun.COM 			break;
5259*11878SVenu.Iyer@Sun.COM 		}
5260*11878SVenu.Iyer@Sun.COM 		bcopy(mcip->mgc_client->mci_name, &(clnts_name[index]),
5261*11878SVenu.Iyer@Sun.COM 		    name_len);
5262*11878SVenu.Iyer@Sun.COM 		index += name_len;
5263*11878SVenu.Iyer@Sun.COM 		clnts_name[index++] = ',';
5264*11878SVenu.Iyer@Sun.COM 		i++;
5265*11878SVenu.Iyer@Sun.COM 	}
5266*11878SVenu.Iyer@Sun.COM 
5267*11878SVenu.Iyer@Sun.COM 	/* Get rid of the last , */
5268*11878SVenu.Iyer@Sun.COM 	if (index > 0)
5269*11878SVenu.Iyer@Sun.COM 		clnts_name[index - 1] = '\0';
5270*11878SVenu.Iyer@Sun.COM 	*n_clnts = i;
5271*11878SVenu.Iyer@Sun.COM 	rw_exit(&mip->mi_rw_lock);
5272*11878SVenu.Iyer@Sun.COM }
5273*11878SVenu.Iyer@Sun.COM 
5274*11878SVenu.Iyer@Sun.COM /*
5275*11878SVenu.Iyer@Sun.COM  * Return the group count for RX or TX.
5276*11878SVenu.Iyer@Sun.COM  */
52778275SEric Cheng uint_t
5278*11878SVenu.Iyer@Sun.COM mac_hwgrp_num(mac_handle_t mh, int type)
52798275SEric Cheng {
52808275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
52818275SEric Cheng 
5282*11878SVenu.Iyer@Sun.COM 	/*
5283*11878SVenu.Iyer@Sun.COM 	 * Return the Rx and Tx group count; for the Tx we need to
5284*11878SVenu.Iyer@Sun.COM 	 * include the default too.
5285*11878SVenu.Iyer@Sun.COM 	 */
5286*11878SVenu.Iyer@Sun.COM 	return (type == MAC_RING_TYPE_RX ? mip->mi_rx_group_count :
5287*11878SVenu.Iyer@Sun.COM 	    mip->mi_tx_groups != NULL ? mip->mi_tx_group_count + 1 : 0);
5288*11878SVenu.Iyer@Sun.COM }
5289*11878SVenu.Iyer@Sun.COM 
5290*11878SVenu.Iyer@Sun.COM /*
5291*11878SVenu.Iyer@Sun.COM  * The total number of free TX rings for this MAC.
5292*11878SVenu.Iyer@Sun.COM  */
5293*11878SVenu.Iyer@Sun.COM uint_t
5294*11878SVenu.Iyer@Sun.COM mac_txavail_get(mac_handle_t mh)
5295*11878SVenu.Iyer@Sun.COM {
5296*11878SVenu.Iyer@Sun.COM 	mac_impl_t	*mip = (mac_impl_t *)mh;
5297*11878SVenu.Iyer@Sun.COM 
5298*11878SVenu.Iyer@Sun.COM 	return (mip->mi_txrings_avail);
5299*11878SVenu.Iyer@Sun.COM }
5300*11878SVenu.Iyer@Sun.COM 
5301*11878SVenu.Iyer@Sun.COM /*
5302*11878SVenu.Iyer@Sun.COM  * The total number of free RX rings for this MAC.
5303*11878SVenu.Iyer@Sun.COM  */
5304*11878SVenu.Iyer@Sun.COM uint_t
5305*11878SVenu.Iyer@Sun.COM mac_rxavail_get(mac_handle_t mh)
5306*11878SVenu.Iyer@Sun.COM {
5307*11878SVenu.Iyer@Sun.COM 	mac_impl_t	*mip = (mac_impl_t *)mh;
5308*11878SVenu.Iyer@Sun.COM 
5309*11878SVenu.Iyer@Sun.COM 	return (mip->mi_rxrings_avail);
5310*11878SVenu.Iyer@Sun.COM }
5311*11878SVenu.Iyer@Sun.COM 
5312*11878SVenu.Iyer@Sun.COM /*
5313*11878SVenu.Iyer@Sun.COM  * The total number of reserved RX rings on this MAC.
5314*11878SVenu.Iyer@Sun.COM  */
5315*11878SVenu.Iyer@Sun.COM uint_t
5316*11878SVenu.Iyer@Sun.COM mac_rxrsvd_get(mac_handle_t mh)
5317*11878SVenu.Iyer@Sun.COM {
5318*11878SVenu.Iyer@Sun.COM 	mac_impl_t	*mip = (mac_impl_t *)mh;
5319*11878SVenu.Iyer@Sun.COM 
5320*11878SVenu.Iyer@Sun.COM 	return (mip->mi_rxrings_rsvd);
5321*11878SVenu.Iyer@Sun.COM }
5322*11878SVenu.Iyer@Sun.COM 
5323*11878SVenu.Iyer@Sun.COM /*
5324*11878SVenu.Iyer@Sun.COM  * The total number of reserved TX rings on this MAC.
5325*11878SVenu.Iyer@Sun.COM  */
5326*11878SVenu.Iyer@Sun.COM uint_t
5327*11878SVenu.Iyer@Sun.COM mac_txrsvd_get(mac_handle_t mh)
5328*11878SVenu.Iyer@Sun.COM {
5329*11878SVenu.Iyer@Sun.COM 	mac_impl_t	*mip = (mac_impl_t *)mh;
5330*11878SVenu.Iyer@Sun.COM 
5331*11878SVenu.Iyer@Sun.COM 	return (mip->mi_txrings_rsvd);
53328275SEric Cheng }
5333*11878SVenu.Iyer@Sun.COM 
5334*11878SVenu.Iyer@Sun.COM /*
5335*11878SVenu.Iyer@Sun.COM  * Total number of free RX groups on this MAC.
5336*11878SVenu.Iyer@Sun.COM  */
5337*11878SVenu.Iyer@Sun.COM uint_t
5338*11878SVenu.Iyer@Sun.COM mac_rxhwlnksavail_get(mac_handle_t mh)
5339*11878SVenu.Iyer@Sun.COM {
5340*11878SVenu.Iyer@Sun.COM 	mac_impl_t	*mip = (mac_impl_t *)mh;
5341*11878SVenu.Iyer@Sun.COM 
5342*11878SVenu.Iyer@Sun.COM 	return (mip->mi_rxhwclnt_avail);
5343*11878SVenu.Iyer@Sun.COM }
5344*11878SVenu.Iyer@Sun.COM 
5345*11878SVenu.Iyer@Sun.COM /*
5346*11878SVenu.Iyer@Sun.COM  * Total number of RX groups reserved on this MAC.
5347*11878SVenu.Iyer@Sun.COM  */
5348*11878SVenu.Iyer@Sun.COM uint_t
5349*11878SVenu.Iyer@Sun.COM mac_rxhwlnksrsvd_get(mac_handle_t mh)
5350*11878SVenu.Iyer@Sun.COM {
5351*11878SVenu.Iyer@Sun.COM 	mac_impl_t	*mip = (mac_impl_t *)mh;
5352*11878SVenu.Iyer@Sun.COM 
5353*11878SVenu.Iyer@Sun.COM 	return (mip->mi_rxhwclnt_used);
5354*11878SVenu.Iyer@Sun.COM }
5355*11878SVenu.Iyer@Sun.COM 
5356*11878SVenu.Iyer@Sun.COM /*
5357*11878SVenu.Iyer@Sun.COM  * Total number of free TX groups on this MAC.
5358*11878SVenu.Iyer@Sun.COM  */
5359*11878SVenu.Iyer@Sun.COM uint_t
5360*11878SVenu.Iyer@Sun.COM mac_txhwlnksavail_get(mac_handle_t mh)
5361*11878SVenu.Iyer@Sun.COM {
5362*11878SVenu.Iyer@Sun.COM 	mac_impl_t	*mip = (mac_impl_t *)mh;
5363*11878SVenu.Iyer@Sun.COM 
5364*11878SVenu.Iyer@Sun.COM 	return (mip->mi_txhwclnt_avail);
5365*11878SVenu.Iyer@Sun.COM }
5366*11878SVenu.Iyer@Sun.COM 
5367*11878SVenu.Iyer@Sun.COM /*
5368*11878SVenu.Iyer@Sun.COM  * Total number of TX groups reserved on this MAC.
5369*11878SVenu.Iyer@Sun.COM  */
5370*11878SVenu.Iyer@Sun.COM uint_t
5371*11878SVenu.Iyer@Sun.COM mac_txhwlnksrsvd_get(mac_handle_t mh)
5372*11878SVenu.Iyer@Sun.COM {
5373*11878SVenu.Iyer@Sun.COM 	mac_impl_t	*mip = (mac_impl_t *)mh;
5374*11878SVenu.Iyer@Sun.COM 
5375*11878SVenu.Iyer@Sun.COM 	return (mip->mi_txhwclnt_used);
5376*11878SVenu.Iyer@Sun.COM }
5377*11878SVenu.Iyer@Sun.COM 
5378*11878SVenu.Iyer@Sun.COM /*
5379*11878SVenu.Iyer@Sun.COM  * Initialize the rings property for a mac client. A non-0 value for
5380*11878SVenu.Iyer@Sun.COM  * rxring or txring specifies the number of rings required, a value
5381*11878SVenu.Iyer@Sun.COM  * of MAC_RXRINGS_NONE/MAC_TXRINGS_NONE specifies that it doesn't need
5382*11878SVenu.Iyer@Sun.COM  * any RX/TX rings and a value of MAC_RXRINGS_DONTCARE/MAC_TXRINGS_DONTCARE
5383*11878SVenu.Iyer@Sun.COM  * means the system can decide whether it can give any rings or not.
5384*11878SVenu.Iyer@Sun.COM  */
5385*11878SVenu.Iyer@Sun.COM void
5386*11878SVenu.Iyer@Sun.COM mac_client_set_rings(mac_client_handle_t mch, int rxrings, int txrings)
5387*11878SVenu.Iyer@Sun.COM {
5388*11878SVenu.Iyer@Sun.COM 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
5389*11878SVenu.Iyer@Sun.COM 	mac_resource_props_t	*mrp = MCIP_RESOURCE_PROPS(mcip);
5390*11878SVenu.Iyer@Sun.COM 
5391*11878SVenu.Iyer@Sun.COM 	if (rxrings != MAC_RXRINGS_DONTCARE) {
5392*11878SVenu.Iyer@Sun.COM 		mrp->mrp_mask |= MRP_RX_RINGS;
5393*11878SVenu.Iyer@Sun.COM 		mrp->mrp_nrxrings = rxrings;
5394*11878SVenu.Iyer@Sun.COM 	}
5395*11878SVenu.Iyer@Sun.COM 
5396*11878SVenu.Iyer@Sun.COM 	if (txrings != MAC_TXRINGS_DONTCARE) {
5397*11878SVenu.Iyer@Sun.COM 		mrp->mrp_mask |= MRP_TX_RINGS;
5398*11878SVenu.Iyer@Sun.COM 		mrp->mrp_ntxrings = txrings;
5399*11878SVenu.Iyer@Sun.COM 	}
5400*11878SVenu.Iyer@Sun.COM }
5401