xref: /onnv-gate/usr/src/uts/common/io/mac/mac_client.c (revision 11665:abe81921ae9e)
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 /*
23*11665SDarren.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>
1118275SEric Cheng #include <sys/dls.h>
1128275SEric Cheng #include <sys/dld.h>
1138275SEric Cheng #include <sys/modctl.h>
1148275SEric Cheng #include <sys/fs/dv_node.h>
1158275SEric Cheng #include <sys/thread.h>
1168275SEric Cheng #include <sys/proc.h>
1178275SEric Cheng #include <sys/callb.h>
1188275SEric Cheng #include <sys/cpuvar.h>
1198275SEric Cheng #include <sys/atomic.h>
1208275SEric Cheng #include <sys/sdt.h>
1218275SEric Cheng #include <sys/mac_flow.h>
1228275SEric Cheng #include <sys/ddi_intr_impl.h>
1238275SEric Cheng #include <sys/disp.h>
1248275SEric Cheng #include <sys/sdt.h>
1258275SEric Cheng #include <sys/vnic.h>
1268275SEric Cheng #include <sys/vnic_impl.h>
1278275SEric Cheng #include <sys/vlan.h>
1288275SEric Cheng #include <inet/ip.h>
1298275SEric Cheng #include <inet/ip6.h>
1308275SEric Cheng #include <sys/exacct.h>
1318275SEric Cheng #include <sys/exacct_impl.h>
1328275SEric Cheng #include <inet/nd.h>
1338275SEric Cheng #include <sys/ethernet.h>
1348275SEric Cheng 
1358275SEric Cheng kmem_cache_t	*mac_client_impl_cache;
1368275SEric Cheng kmem_cache_t	*mac_promisc_impl_cache;
1378275SEric Cheng 
1388275SEric Cheng static boolean_t mac_client_single_rcvr(mac_client_impl_t *);
1398275SEric Cheng static flow_entry_t *mac_client_swap_mciflent(mac_client_impl_t *);
1408275SEric Cheng static flow_entry_t *mac_client_get_flow(mac_client_impl_t *,
1418275SEric Cheng     mac_unicast_impl_t *);
1428275SEric Cheng static void mac_client_remove_flow_from_list(mac_client_impl_t *,
1438275SEric Cheng     flow_entry_t *);
1448275SEric Cheng static void mac_client_add_to_flow_list(mac_client_impl_t *, flow_entry_t *);
1458275SEric Cheng static void mac_rename_flow_names(mac_client_impl_t *, const char *);
1468275SEric Cheng static void mac_virtual_link_update(mac_impl_t *);
1478275SEric Cheng 
1488275SEric Cheng /* ARGSUSED */
1498275SEric Cheng static int
1508275SEric Cheng i_mac_client_impl_ctor(void *buf, void *arg, int kmflag)
1518275SEric Cheng {
1528275SEric Cheng 	int	i;
1538275SEric Cheng 	mac_client_impl_t	*mcip = buf;
1548275SEric Cheng 
1558275SEric Cheng 	bzero(buf, MAC_CLIENT_IMPL_SIZE);
1568275SEric Cheng 	mutex_init(&mcip->mci_tx_cb_lock, NULL, MUTEX_DRIVER, NULL);
1578275SEric Cheng 	mcip->mci_tx_notify_cb_info.mcbi_lockp = &mcip->mci_tx_cb_lock;
1588275SEric Cheng 
1598275SEric Cheng 	ASSERT(mac_tx_percpu_cnt >= 0);
1608275SEric Cheng 	for (i = 0; i <= mac_tx_percpu_cnt; i++) {
1618275SEric Cheng 		mutex_init(&mcip->mci_tx_pcpu[i].pcpu_tx_lock, NULL,
1628275SEric Cheng 		    MUTEX_DRIVER, NULL);
1638275SEric Cheng 	}
1648275SEric Cheng 	cv_init(&mcip->mci_tx_cv, NULL, CV_DRIVER, NULL);
1658275SEric Cheng 
1668275SEric Cheng 	return (0);
1678275SEric Cheng }
1688275SEric Cheng 
1698275SEric Cheng /* ARGSUSED */
1708275SEric Cheng static void
1718275SEric Cheng i_mac_client_impl_dtor(void *buf, void *arg)
1728275SEric Cheng {
1738275SEric Cheng 	int	i;
1748275SEric Cheng 	mac_client_impl_t *mcip = buf;
1758275SEric Cheng 
1768275SEric Cheng 	ASSERT(mcip->mci_promisc_list == NULL);
1778275SEric Cheng 	ASSERT(mcip->mci_unicast_list == NULL);
1788275SEric Cheng 	ASSERT(mcip->mci_state_flags == 0);
1798275SEric Cheng 	ASSERT(mcip->mci_tx_flag == 0);
1808275SEric Cheng 
1818275SEric Cheng 	mutex_destroy(&mcip->mci_tx_cb_lock);
1828275SEric Cheng 
1838275SEric Cheng 	ASSERT(mac_tx_percpu_cnt >= 0);
1848275SEric Cheng 	for (i = 0; i <= mac_tx_percpu_cnt; i++) {
1858275SEric Cheng 		ASSERT(mcip->mci_tx_pcpu[i].pcpu_tx_refcnt == 0);
1868275SEric Cheng 		mutex_destroy(&mcip->mci_tx_pcpu[i].pcpu_tx_lock);
1878275SEric Cheng 	}
1888275SEric Cheng 	cv_destroy(&mcip->mci_tx_cv);
1898275SEric Cheng }
1908275SEric Cheng 
1918275SEric Cheng /* ARGSUSED */
1928275SEric Cheng static int
1938275SEric Cheng i_mac_promisc_impl_ctor(void *buf, void *arg, int kmflag)
1948275SEric Cheng {
1958275SEric Cheng 	mac_promisc_impl_t	*mpip = buf;
1968275SEric Cheng 
1978275SEric Cheng 	bzero(buf, sizeof (mac_promisc_impl_t));
1988275SEric Cheng 	mpip->mpi_mci_link.mcb_objp = buf;
1998275SEric Cheng 	mpip->mpi_mci_link.mcb_objsize = sizeof (mac_promisc_impl_t);
2008275SEric Cheng 	mpip->mpi_mi_link.mcb_objp = buf;
2018275SEric Cheng 	mpip->mpi_mi_link.mcb_objsize = sizeof (mac_promisc_impl_t);
2028275SEric Cheng 	return (0);
2038275SEric Cheng }
2048275SEric Cheng 
2058275SEric Cheng /* ARGSUSED */
2068275SEric Cheng static void
2078275SEric Cheng i_mac_promisc_impl_dtor(void *buf, void *arg)
2088275SEric Cheng {
2098275SEric Cheng 	mac_promisc_impl_t	*mpip = buf;
2108275SEric Cheng 
2118275SEric Cheng 	ASSERT(mpip->mpi_mci_link.mcb_objp != NULL);
2128275SEric Cheng 	ASSERT(mpip->mpi_mci_link.mcb_objsize == sizeof (mac_promisc_impl_t));
2138275SEric Cheng 	ASSERT(mpip->mpi_mi_link.mcb_objp == mpip->mpi_mci_link.mcb_objp);
2148275SEric Cheng 	ASSERT(mpip->mpi_mi_link.mcb_objsize == sizeof (mac_promisc_impl_t));
2158275SEric Cheng 
2168275SEric Cheng 	mpip->mpi_mci_link.mcb_objp = NULL;
2178275SEric Cheng 	mpip->mpi_mci_link.mcb_objsize = 0;
2188275SEric Cheng 	mpip->mpi_mi_link.mcb_objp = NULL;
2198275SEric Cheng 	mpip->mpi_mi_link.mcb_objsize = 0;
2208275SEric Cheng 
2218275SEric Cheng 	ASSERT(mpip->mpi_mci_link.mcb_flags == 0);
2228275SEric Cheng 	mpip->mpi_mci_link.mcb_objsize = 0;
2238275SEric Cheng }
2248275SEric Cheng 
2258275SEric Cheng void
2268275SEric Cheng mac_client_init(void)
2278275SEric Cheng {
2288275SEric Cheng 	ASSERT(mac_tx_percpu_cnt >= 0);
2298275SEric Cheng 
2308275SEric Cheng 	mac_client_impl_cache = kmem_cache_create("mac_client_impl_cache",
2318275SEric Cheng 	    MAC_CLIENT_IMPL_SIZE, 0, i_mac_client_impl_ctor,
2328275SEric Cheng 	    i_mac_client_impl_dtor, NULL, NULL, NULL, 0);
2338275SEric Cheng 	ASSERT(mac_client_impl_cache != NULL);
2348275SEric Cheng 
2358275SEric Cheng 	mac_promisc_impl_cache = kmem_cache_create("mac_promisc_impl_cache",
2368275SEric Cheng 	    sizeof (mac_promisc_impl_t), 0, i_mac_promisc_impl_ctor,
2378275SEric Cheng 	    i_mac_promisc_impl_dtor, NULL, NULL, NULL, 0);
2388275SEric Cheng 	ASSERT(mac_promisc_impl_cache != NULL);
2398275SEric Cheng }
2408275SEric Cheng 
2418275SEric Cheng void
2428275SEric Cheng mac_client_fini(void)
2438275SEric Cheng {
2448275SEric Cheng 	kmem_cache_destroy(mac_client_impl_cache);
2458275SEric Cheng 	kmem_cache_destroy(mac_promisc_impl_cache);
2468275SEric Cheng }
2478275SEric Cheng 
2488275SEric Cheng /*
2498275SEric Cheng  * Return the lower MAC client handle from the VNIC driver for the
2508275SEric Cheng  * specified VNIC MAC instance.
2518275SEric Cheng  */
2528275SEric Cheng mac_client_impl_t *
2538275SEric Cheng mac_vnic_lower(mac_impl_t *mip)
2548275SEric Cheng {
2558275SEric Cheng 	mac_capab_vnic_t cap;
2568275SEric Cheng 	mac_client_impl_t *mcip;
2578275SEric Cheng 
2588275SEric Cheng 	VERIFY(i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_VNIC, &cap));
2598275SEric Cheng 	mcip = cap.mcv_mac_client_handle(cap.mcv_arg);
2608275SEric Cheng 
2618275SEric Cheng 	return (mcip);
2628275SEric Cheng }
2638275SEric Cheng 
2648275SEric Cheng /*
2658275SEric Cheng  * Return the MAC client handle of the primary MAC client for the
2668275SEric Cheng  * specified MAC instance, or NULL otherwise.
2678275SEric Cheng  */
2688275SEric Cheng mac_client_impl_t *
2698275SEric Cheng mac_primary_client_handle(mac_impl_t *mip)
2708275SEric Cheng {
2718275SEric Cheng 	mac_client_impl_t *mcip;
2728275SEric Cheng 
2738275SEric Cheng 	if (mip->mi_state_flags & MIS_IS_VNIC)
2748275SEric Cheng 		return (mac_vnic_lower(mip));
2758275SEric Cheng 
2768275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
2778275SEric Cheng 
2788275SEric Cheng 	for (mcip = mip->mi_clients_list; mcip != NULL;
2798275SEric Cheng 	    mcip = mcip->mci_client_next) {
2808275SEric Cheng 		if (MCIP_DATAPATH_SETUP(mcip) && mac_is_primary_client(mcip))
2818275SEric Cheng 			return (mcip);
2828275SEric Cheng 	}
2838275SEric Cheng 	return (NULL);
2848275SEric Cheng }
2858275SEric Cheng 
2868275SEric Cheng /*
2878275SEric Cheng  * Open a MAC specified by its MAC name.
2888275SEric Cheng  */
2898275SEric Cheng int
2908275SEric Cheng mac_open(const char *macname, mac_handle_t *mhp)
2918275SEric Cheng {
2928275SEric Cheng 	mac_impl_t	*mip;
2938275SEric Cheng 	int		err;
2948275SEric Cheng 
2958275SEric Cheng 	/*
2968275SEric Cheng 	 * Look up its entry in the global hash table.
2978275SEric Cheng 	 */
2988275SEric Cheng 	if ((err = mac_hold(macname, &mip)) != 0)
2998275SEric Cheng 		return (err);
3008275SEric Cheng 
3018275SEric Cheng 	/*
3028275SEric Cheng 	 * Hold the dip associated to the MAC to prevent it from being
3038275SEric Cheng 	 * detached. For a softmac, its underlying dip is held by the
3048275SEric Cheng 	 * mi_open() callback.
3058275SEric Cheng 	 *
3068275SEric Cheng 	 * This is done to be more tolerant with some defective drivers,
3078275SEric Cheng 	 * which incorrectly handle mac_unregister() failure in their
3088275SEric Cheng 	 * xxx_detach() routine. For example, some drivers ignore the
3098275SEric Cheng 	 * failure of mac_unregister() and free all resources that
3108275SEric Cheng 	 * that are needed for data transmition.
3118275SEric Cheng 	 */
3128275SEric Cheng 	e_ddi_hold_devi(mip->mi_dip);
3138275SEric Cheng 
3148275SEric Cheng 	if (!(mip->mi_callbacks->mc_callbacks & MC_OPEN)) {
3158275SEric Cheng 		*mhp = (mac_handle_t)mip;
3168275SEric Cheng 		return (0);
3178275SEric Cheng 	}
3188275SEric Cheng 
3198275SEric Cheng 	/*
3208275SEric Cheng 	 * The mac perimeter is used in both mac_open and mac_close by the
3218275SEric Cheng 	 * framework to single thread the MC_OPEN/MC_CLOSE of drivers.
3228275SEric Cheng 	 */
3238275SEric Cheng 	i_mac_perim_enter(mip);
3248275SEric Cheng 	mip->mi_oref++;
3258275SEric Cheng 	if (mip->mi_oref != 1 || ((err = mip->mi_open(mip->mi_driver)) == 0)) {
3268275SEric Cheng 		*mhp = (mac_handle_t)mip;
3278275SEric Cheng 		i_mac_perim_exit(mip);
3288275SEric Cheng 		return (0);
3298275SEric Cheng 	}
3308275SEric Cheng 	mip->mi_oref--;
3318275SEric Cheng 	ddi_release_devi(mip->mi_dip);
3328275SEric Cheng 	mac_rele(mip);
3338275SEric Cheng 	i_mac_perim_exit(mip);
3348275SEric Cheng 	return (err);
3358275SEric Cheng }
3368275SEric Cheng 
3378275SEric Cheng /*
3388275SEric Cheng  * Open a MAC specified by its linkid.
3398275SEric Cheng  */
3408275SEric Cheng int
3418275SEric Cheng mac_open_by_linkid(datalink_id_t linkid, mac_handle_t *mhp)
3428275SEric Cheng {
3438275SEric Cheng 	dls_dl_handle_t	dlh;
3448275SEric Cheng 	int		err;
3458275SEric Cheng 
3468275SEric Cheng 	if ((err = dls_devnet_hold_tmp(linkid, &dlh)) != 0)
3478275SEric Cheng 		return (err);
3488275SEric Cheng 
3498275SEric Cheng 	dls_devnet_prop_task_wait(dlh);
3508275SEric Cheng 
3518275SEric Cheng 	err = mac_open(dls_devnet_mac(dlh), mhp);
3528275SEric Cheng 
3538275SEric Cheng 	dls_devnet_rele_tmp(dlh);
3548275SEric Cheng 	return (err);
3558275SEric Cheng }
3568275SEric Cheng 
3578275SEric Cheng /*
3588275SEric Cheng  * Open a MAC specified by its link name.
3598275SEric Cheng  */
3608275SEric Cheng int
3618275SEric Cheng mac_open_by_linkname(const char *link, mac_handle_t *mhp)
3628275SEric Cheng {
3638275SEric Cheng 	datalink_id_t	linkid;
3648275SEric Cheng 	int		err;
3658275SEric Cheng 
3668275SEric Cheng 	if ((err = dls_mgmt_get_linkid(link, &linkid)) != 0)
3678275SEric Cheng 		return (err);
3688275SEric Cheng 	return (mac_open_by_linkid(linkid, mhp));
3698275SEric Cheng }
3708275SEric Cheng 
3718275SEric Cheng /*
3728275SEric Cheng  * Close the specified MAC.
3738275SEric Cheng  */
3748275SEric Cheng void
3758275SEric Cheng mac_close(mac_handle_t mh)
3768275SEric Cheng {
3778275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
3788275SEric Cheng 
3798275SEric Cheng 	i_mac_perim_enter(mip);
3808275SEric Cheng 	/*
3818275SEric Cheng 	 * The mac perimeter is used in both mac_open and mac_close by the
3828275SEric Cheng 	 * framework to single thread the MC_OPEN/MC_CLOSE of drivers.
3838275SEric Cheng 	 */
3848275SEric Cheng 	if (mip->mi_callbacks->mc_callbacks & MC_OPEN) {
3858275SEric Cheng 		ASSERT(mip->mi_oref != 0);
3868275SEric Cheng 		if (--mip->mi_oref == 0) {
3878275SEric Cheng 			if ((mip->mi_callbacks->mc_callbacks & MC_CLOSE))
3888275SEric Cheng 				mip->mi_close(mip->mi_driver);
3898275SEric Cheng 		}
3908275SEric Cheng 	}
3918275SEric Cheng 	i_mac_perim_exit(mip);
3928275SEric Cheng 	ddi_release_devi(mip->mi_dip);
3938275SEric Cheng 	mac_rele(mip);
3948275SEric Cheng }
3958275SEric Cheng 
3968275SEric Cheng /*
3978275SEric Cheng  * Misc utility functions to retrieve various information about a MAC
3988275SEric Cheng  * instance or a MAC client.
3998275SEric Cheng  */
4008275SEric Cheng 
4018275SEric Cheng const mac_info_t *
4028275SEric Cheng mac_info(mac_handle_t mh)
4038275SEric Cheng {
4048275SEric Cheng 	return (&((mac_impl_t *)mh)->mi_info);
4058275SEric Cheng }
4068275SEric Cheng 
4078275SEric Cheng dev_info_t *
4088275SEric Cheng mac_devinfo_get(mac_handle_t mh)
4098275SEric Cheng {
4108275SEric Cheng 	return (((mac_impl_t *)mh)->mi_dip);
4118275SEric Cheng }
4128275SEric Cheng 
4139073SCathy.Zhou@Sun.COM void *
4149073SCathy.Zhou@Sun.COM mac_driver(mac_handle_t mh)
4159073SCathy.Zhou@Sun.COM {
4169073SCathy.Zhou@Sun.COM 	return (((mac_impl_t *)mh)->mi_driver);
4179073SCathy.Zhou@Sun.COM }
4189073SCathy.Zhou@Sun.COM 
4198275SEric Cheng const char *
4208275SEric Cheng mac_name(mac_handle_t mh)
4218275SEric Cheng {
4228275SEric Cheng 	return (((mac_impl_t *)mh)->mi_name);
4238275SEric Cheng }
4248275SEric Cheng 
42510639SDarren.Reed@Sun.COM int
42610639SDarren.Reed@Sun.COM mac_type(mac_handle_t mh)
42710639SDarren.Reed@Sun.COM {
42810639SDarren.Reed@Sun.COM 	return (((mac_impl_t *)mh)->mi_type->mt_type);
42910639SDarren.Reed@Sun.COM }
43010639SDarren.Reed@Sun.COM 
431*11665SDarren.Reed@Sun.COM int
432*11665SDarren.Reed@Sun.COM mac_nativetype(mac_handle_t mh)
433*11665SDarren.Reed@Sun.COM {
434*11665SDarren.Reed@Sun.COM 	return (((mac_impl_t *)mh)->mi_type->mt_nativetype);
435*11665SDarren.Reed@Sun.COM }
436*11665SDarren.Reed@Sun.COM 
4378275SEric Cheng char *
4388275SEric Cheng mac_client_name(mac_client_handle_t mch)
4398275SEric Cheng {
4408275SEric Cheng 	return (((mac_client_impl_t *)mch)->mci_name);
4418275SEric Cheng }
4428275SEric Cheng 
4438275SEric Cheng minor_t
4448275SEric Cheng mac_minor(mac_handle_t mh)
4458275SEric Cheng {
4468275SEric Cheng 	return (((mac_impl_t *)mh)->mi_minor);
4478275SEric Cheng }
4488275SEric Cheng 
4498275SEric Cheng /*
4508275SEric Cheng  * Return the VID associated with a MAC client. This function should
4518275SEric Cheng  * be called for clients which are associated with only one VID.
4528275SEric Cheng  */
4538275SEric Cheng uint16_t
4548275SEric Cheng mac_client_vid(mac_client_handle_t mch)
4558275SEric Cheng {
4568275SEric Cheng 	uint16_t		vid = VLAN_ID_NONE;
4578275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
4588275SEric Cheng 	flow_desc_t		flow_desc;
4598275SEric Cheng 
4608275SEric Cheng 	if (mcip->mci_nflents == 0)
4618275SEric Cheng 		return (vid);
4628275SEric Cheng 
4638275SEric Cheng 	ASSERT(MCIP_DATAPATH_SETUP(mcip) && mac_client_single_rcvr(mcip));
4648275SEric Cheng 
4658275SEric Cheng 	mac_flow_get_desc(mcip->mci_flent, &flow_desc);
4668275SEric Cheng 	if ((flow_desc.fd_mask & FLOW_LINK_VID) != 0)
4678275SEric Cheng 		vid = flow_desc.fd_vid;
4688275SEric Cheng 
4698275SEric Cheng 	return (vid);
4708275SEric Cheng }
4718275SEric Cheng 
4728275SEric Cheng /*
4739726SNicolas.Droux@Sun.COM  * Return whether the specified MAC client corresponds to a VLAN VNIC.
4749726SNicolas.Droux@Sun.COM  */
4759726SNicolas.Droux@Sun.COM boolean_t
4769726SNicolas.Droux@Sun.COM mac_client_is_vlan_vnic(mac_client_handle_t mch)
4779726SNicolas.Droux@Sun.COM {
4789726SNicolas.Droux@Sun.COM 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
4799726SNicolas.Droux@Sun.COM 
4809726SNicolas.Droux@Sun.COM 	return (((mcip->mci_state_flags & MCIS_IS_VNIC) != 0) &&
4819726SNicolas.Droux@Sun.COM 	    ((mcip->mci_flent->fe_type & FLOW_PRIMARY_MAC) != 0));
4829726SNicolas.Droux@Sun.COM }
4839726SNicolas.Droux@Sun.COM 
4849726SNicolas.Droux@Sun.COM /*
4858275SEric Cheng  * Return the link speed associated with the specified MAC client.
4868275SEric Cheng  *
4878275SEric Cheng  * The link speed of a MAC client is equal to the smallest value of
4888275SEric Cheng  * 1) the current link speed of the underlying NIC, or
4898275SEric Cheng  * 2) the bandwidth limit set for the MAC client.
4908275SEric Cheng  *
4918275SEric Cheng  * Note that the bandwidth limit can be higher than the speed
4928275SEric Cheng  * of the underlying NIC. This is allowed to avoid spurious
4938275SEric Cheng  * administration action failures or artifically lowering the
4948275SEric Cheng  * bandwidth limit of a link that may  have temporarily lowered
4958275SEric Cheng  * its link speed due to hardware problem or administrator action.
4968275SEric Cheng  */
4978275SEric Cheng static uint64_t
4988275SEric Cheng mac_client_ifspeed(mac_client_impl_t *mcip)
4998275SEric Cheng {
5008275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
5018275SEric Cheng 	uint64_t nic_speed;
5028275SEric Cheng 
5038275SEric Cheng 	nic_speed = mac_stat_get((mac_handle_t)mip, MAC_STAT_IFSPEED);
5048275SEric Cheng 
5058275SEric Cheng 	if (nic_speed == 0) {
5068275SEric Cheng 		return (0);
5078275SEric Cheng 	} else {
5088275SEric Cheng 		uint64_t policy_limit = (uint64_t)-1;
5098275SEric Cheng 
5108275SEric Cheng 		if (MCIP_RESOURCE_PROPS_MASK(mcip) & MRP_MAXBW)
5118275SEric Cheng 			policy_limit = MCIP_RESOURCE_PROPS_MAXBW(mcip);
5128275SEric Cheng 
5138275SEric Cheng 		return (MIN(policy_limit, nic_speed));
5148275SEric Cheng 	}
5158275SEric Cheng }
5168275SEric Cheng 
5178275SEric Cheng /*
5188275SEric Cheng  * Return the link state of the specified client. If here are more
5198275SEric Cheng  * than one clients of the underying mac_impl_t, the link state
5208275SEric Cheng  * will always be UP regardless of the link state of the underlying
5218275SEric Cheng  * mac_impl_t. This is needed to allow the MAC clients to continue
5228275SEric Cheng  * to communicate with each other even when the physical link of
5238275SEric Cheng  * their mac_impl_t is down.
5248275SEric Cheng  */
5258275SEric Cheng static uint64_t
5268275SEric Cheng mac_client_link_state(mac_client_impl_t *mcip)
5278275SEric Cheng {
5288275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
5298275SEric Cheng 	uint16_t vid;
5308275SEric Cheng 	mac_client_impl_t *mci_list;
5318275SEric Cheng 	mac_unicast_impl_t *mui_list, *oth_mui_list;
5328275SEric Cheng 
5338275SEric Cheng 	/*
5348275SEric Cheng 	 * Returns LINK_STATE_UP if there are other MAC clients defined on
5358275SEric Cheng 	 * mac_impl_t which share same VLAN ID as that of mcip. Note that
5368275SEric Cheng 	 * if 'mcip' has more than one VID's then we match ANY one of the
5378275SEric Cheng 	 * VID's with other MAC client's VID's and return LINK_STATE_UP.
5388275SEric Cheng 	 */
5398275SEric Cheng 	rw_enter(&mcip->mci_rw_lock, RW_READER);
5408275SEric Cheng 	for (mui_list = mcip->mci_unicast_list; mui_list != NULL;
5418275SEric Cheng 	    mui_list = mui_list->mui_next) {
5428275SEric Cheng 		vid = mui_list->mui_vid;
5438275SEric Cheng 		for (mci_list = mip->mi_clients_list; mci_list != NULL;
5448275SEric Cheng 		    mci_list = mci_list->mci_client_next) {
5458275SEric Cheng 			if (mci_list == mcip)
5468275SEric Cheng 				continue;
5478275SEric Cheng 			for (oth_mui_list = mci_list->mci_unicast_list;
5488275SEric Cheng 			    oth_mui_list != NULL; oth_mui_list = oth_mui_list->
5498275SEric Cheng 			    mui_next) {
5508275SEric Cheng 				if (vid == oth_mui_list->mui_vid) {
5518275SEric Cheng 					rw_exit(&mcip->mci_rw_lock);
5528275SEric Cheng 					return (LINK_STATE_UP);
5538275SEric Cheng 				}
5548275SEric Cheng 			}
5558275SEric Cheng 		}
5568275SEric Cheng 	}
5578275SEric Cheng 	rw_exit(&mcip->mci_rw_lock);
5588275SEric Cheng 
5598275SEric Cheng 	return (mac_stat_get((mac_handle_t)mip, MAC_STAT_LINK_STATE));
5608275SEric Cheng }
5618275SEric Cheng 
5628275SEric Cheng /*
5638275SEric Cheng  * Return the statistics of a MAC client. These statistics are different
5648275SEric Cheng  * then the statistics of the underlying MAC which are returned by
5658275SEric Cheng  * mac_stat_get().
5668275SEric Cheng  */
5678275SEric Cheng uint64_t
5688275SEric Cheng mac_client_stat_get(mac_client_handle_t mch, uint_t stat)
5698275SEric Cheng {
5708275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
5718275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
5728275SEric Cheng 	uint64_t val;
5738275SEric Cheng 
5748275SEric Cheng 	switch (stat) {
5758275SEric Cheng 	case MAC_STAT_LINK_STATE:
5768275SEric Cheng 		val = mac_client_link_state(mcip);
5778275SEric Cheng 		break;
5788275SEric Cheng 	case MAC_STAT_LINK_UP:
5798275SEric Cheng 		val = (mac_client_link_state(mcip) == LINK_STATE_UP);
5808275SEric Cheng 		break;
5818275SEric Cheng 	case MAC_STAT_PROMISC:
5828275SEric Cheng 		val = mac_stat_get((mac_handle_t)mip, MAC_STAT_PROMISC);
5838275SEric Cheng 		break;
58410491SRishi.Srivatsavai@Sun.COM 	case MAC_STAT_LOWLINK_STATE:
58510491SRishi.Srivatsavai@Sun.COM 		val = mac_stat_get((mac_handle_t)mip, MAC_STAT_LOWLINK_STATE);
58610491SRishi.Srivatsavai@Sun.COM 		break;
5878275SEric Cheng 	case MAC_STAT_IFSPEED:
5888275SEric Cheng 		val = mac_client_ifspeed(mcip);
5898275SEric Cheng 		break;
5908275SEric Cheng 	case MAC_STAT_MULTIRCV:
5918275SEric Cheng 		val = mcip->mci_stat_multircv;
5928275SEric Cheng 		break;
5938275SEric Cheng 	case MAC_STAT_BRDCSTRCV:
5948275SEric Cheng 		val = mcip->mci_stat_brdcstrcv;
5958275SEric Cheng 		break;
5968275SEric Cheng 	case MAC_STAT_MULTIXMT:
5978275SEric Cheng 		val = mcip->mci_stat_multixmt;
5988275SEric Cheng 		break;
5998275SEric Cheng 	case MAC_STAT_BRDCSTXMT:
6008275SEric Cheng 		val = mcip->mci_stat_brdcstxmt;
6018275SEric Cheng 		break;
6028275SEric Cheng 	case MAC_STAT_OBYTES:
6038275SEric Cheng 		val = mcip->mci_stat_obytes;
6048275SEric Cheng 		break;
6058275SEric Cheng 	case MAC_STAT_OPACKETS:
6068275SEric Cheng 		val = mcip->mci_stat_opackets;
6078275SEric Cheng 		break;
6088275SEric Cheng 	case MAC_STAT_OERRORS:
6098275SEric Cheng 		val = mcip->mci_stat_oerrors;
6108275SEric Cheng 		break;
6118275SEric Cheng 	case MAC_STAT_IPACKETS:
6128275SEric Cheng 		val = mcip->mci_stat_ipackets;
6138275SEric Cheng 		break;
6148275SEric Cheng 	case MAC_STAT_RBYTES:
6158275SEric Cheng 		val = mcip->mci_stat_ibytes;
6168275SEric Cheng 		break;
6178275SEric Cheng 	case MAC_STAT_IERRORS:
6188275SEric Cheng 		val = mcip->mci_stat_ierrors;
6198275SEric Cheng 		break;
6208275SEric Cheng 	default:
6218275SEric Cheng 		val = mac_stat_default(mip, stat);
6228275SEric Cheng 		break;
6238275SEric Cheng 	}
6248275SEric Cheng 
6258275SEric Cheng 	return (val);
6268275SEric Cheng }
6278275SEric Cheng 
6288275SEric Cheng /*
6298275SEric Cheng  * Return the statistics of the specified MAC instance.
6308275SEric Cheng  */
6318275SEric Cheng uint64_t
6328275SEric Cheng mac_stat_get(mac_handle_t mh, uint_t stat)
6338275SEric Cheng {
6348275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
6358275SEric Cheng 	uint64_t	val;
6368275SEric Cheng 	int		ret;
6378275SEric Cheng 
6388275SEric Cheng 	/*
6398275SEric Cheng 	 * The range of stat determines where it is maintained.  Stat
6408275SEric Cheng 	 * values from 0 up to (but not including) MAC_STAT_MIN are
6418275SEric Cheng 	 * mainteined by the mac module itself.  Everything else is
6428275SEric Cheng 	 * maintained by the driver.
6438275SEric Cheng 	 *
6448275SEric Cheng 	 * If the mac_impl_t being queried corresponds to a VNIC,
6458275SEric Cheng 	 * the stats need to be queried from the lower MAC client
6468275SEric Cheng 	 * corresponding to the VNIC. (The mac_link_update()
6478275SEric Cheng 	 * invoked by the driver to the lower MAC causes the *lower
6488275SEric Cheng 	 * MAC* to update its mi_linkstate, and send a notification
6498275SEric Cheng 	 * to its MAC clients. Due to the VNIC passthrough,
6508275SEric Cheng 	 * these notifications are sent to the upper MAC clients
6518275SEric Cheng 	 * of the VNIC directly, and the upper mac_impl_t of the VNIC
6528275SEric Cheng 	 * does not have a valid mi_linkstate.
6538275SEric Cheng 	 */
6548275SEric Cheng 	if (stat < MAC_STAT_MIN && !(mip->mi_state_flags & MIS_IS_VNIC)) {
6558275SEric Cheng 		/* these stats are maintained by the mac module itself */
6568275SEric Cheng 		switch (stat) {
6578275SEric Cheng 		case MAC_STAT_LINK_STATE:
6588275SEric Cheng 			return (mip->mi_linkstate);
6598275SEric Cheng 		case MAC_STAT_LINK_UP:
6608275SEric Cheng 			return (mip->mi_linkstate == LINK_STATE_UP);
6618275SEric Cheng 		case MAC_STAT_PROMISC:
6628275SEric Cheng 			return (mip->mi_devpromisc != 0);
66310491SRishi.Srivatsavai@Sun.COM 		case MAC_STAT_LOWLINK_STATE:
66410491SRishi.Srivatsavai@Sun.COM 			return (mip->mi_lowlinkstate);
6658275SEric Cheng 		default:
6668275SEric Cheng 			ASSERT(B_FALSE);
6678275SEric Cheng 		}
6688275SEric Cheng 	}
6698275SEric Cheng 
6708275SEric Cheng 	/*
6718275SEric Cheng 	 * Call the driver to get the given statistic.
6728275SEric Cheng 	 */
6738275SEric Cheng 	ret = mip->mi_getstat(mip->mi_driver, stat, &val);
6748275SEric Cheng 	if (ret != 0) {
6758275SEric Cheng 		/*
6768275SEric Cheng 		 * The driver doesn't support this statistic.  Get the
6778275SEric Cheng 		 * statistic's default value.
6788275SEric Cheng 		 */
6798275SEric Cheng 		val = mac_stat_default(mip, stat);
6808275SEric Cheng 	}
6818275SEric Cheng 	return (val);
6828275SEric Cheng }
6838275SEric Cheng 
6848275SEric Cheng /*
6858275SEric Cheng  * Utility function which returns the VID associated with a flow entry.
6868275SEric Cheng  */
6878275SEric Cheng uint16_t
6888275SEric Cheng i_mac_flow_vid(flow_entry_t *flent)
6898275SEric Cheng {
6908275SEric Cheng 	flow_desc_t	flow_desc;
6918275SEric Cheng 
6928275SEric Cheng 	mac_flow_get_desc(flent, &flow_desc);
6938275SEric Cheng 
6948275SEric Cheng 	if ((flow_desc.fd_mask & FLOW_LINK_VID) != 0)
6958275SEric Cheng 		return (flow_desc.fd_vid);
6968275SEric Cheng 	return (VLAN_ID_NONE);
6978275SEric Cheng }
6988275SEric Cheng 
6998275SEric Cheng /*
7008275SEric Cheng  * Verify the validity of the specified unicast MAC address. Returns B_TRUE
7018275SEric Cheng  * if the address is valid, B_FALSE otherwise (multicast address, or incorrect
7028275SEric Cheng  * length.
7038275SEric Cheng  */
7048275SEric Cheng boolean_t
7058275SEric Cheng mac_unicst_verify(mac_handle_t mh, const uint8_t *addr, uint_t len)
7068275SEric Cheng {
7078275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
7088275SEric Cheng 
7098275SEric Cheng 	/*
7108275SEric Cheng 	 * Verify the address. No lock is needed since mi_type and plugin
7118275SEric Cheng 	 * details don't change after mac_register().
7128275SEric Cheng 	 */
7138275SEric Cheng 	if ((len != mip->mi_type->mt_addr_length) ||
7148275SEric Cheng 	    (mip->mi_type->mt_ops.mtops_unicst_verify(addr,
7158275SEric Cheng 	    mip->mi_pdata)) != 0) {
7168275SEric Cheng 		return (B_FALSE);
7178275SEric Cheng 	} else {
7188275SEric Cheng 		return (B_TRUE);
7198275SEric Cheng 	}
7208275SEric Cheng }
7218275SEric Cheng 
7228275SEric Cheng void
7238275SEric Cheng mac_sdu_get(mac_handle_t mh, uint_t *min_sdu, uint_t *max_sdu)
7248275SEric Cheng {
7258275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
7268275SEric Cheng 
7278275SEric Cheng 	if (min_sdu != NULL)
7288275SEric Cheng 		*min_sdu = mip->mi_sdu_min;
7298275SEric Cheng 	if (max_sdu != NULL)
7308275SEric Cheng 		*max_sdu = mip->mi_sdu_max;
7318275SEric Cheng }
7328275SEric Cheng 
7338275SEric Cheng /*
7348275SEric Cheng  * Update the MAC unicast address of the specified client's flows. Currently
7358275SEric Cheng  * only one unicast MAC unicast address is allowed per client.
7368275SEric Cheng  */
7378275SEric Cheng static void
7388275SEric Cheng mac_unicast_update_client_flow(mac_client_impl_t *mcip)
7398275SEric Cheng {
7408275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
7418275SEric Cheng 	flow_entry_t *flent = mcip->mci_flent;
7428275SEric Cheng 	mac_address_t *map = mcip->mci_unicast;
7438275SEric Cheng 	flow_desc_t flow_desc;
7448275SEric Cheng 
7458275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
7468275SEric Cheng 	ASSERT(flent != NULL);
7478275SEric Cheng 
7488275SEric Cheng 	mac_flow_get_desc(flent, &flow_desc);
7498275SEric Cheng 	ASSERT(flow_desc.fd_mask & FLOW_LINK_DST);
7508275SEric Cheng 
7518275SEric Cheng 	bcopy(map->ma_addr, flow_desc.fd_dst_mac, map->ma_len);
7528275SEric Cheng 	mac_flow_set_desc(flent, &flow_desc);
7538275SEric Cheng 
7548275SEric Cheng 	/*
7558275SEric Cheng 	 * A MAC client could have one MAC address but multiple
7568275SEric Cheng 	 * VLANs. In that case update the flow entries corresponding
7578275SEric Cheng 	 * to all VLANs of the MAC client.
7588275SEric Cheng 	 */
7598275SEric Cheng 	for (flent = mcip->mci_flent_list; flent != NULL;
7608275SEric Cheng 	    flent = flent->fe_client_next) {
7618275SEric Cheng 		mac_flow_get_desc(flent, &flow_desc);
7628275SEric Cheng 		if (!(flent->fe_type & FLOW_PRIMARY_MAC ||
7638275SEric Cheng 		    flent->fe_type & FLOW_VNIC_MAC))
7648275SEric Cheng 			continue;
7658275SEric Cheng 
7668275SEric Cheng 		bcopy(map->ma_addr, flow_desc.fd_dst_mac, map->ma_len);
7678275SEric Cheng 		mac_flow_set_desc(flent, &flow_desc);
7688275SEric Cheng 	}
7698275SEric Cheng }
7708275SEric Cheng 
7718275SEric Cheng /*
7728275SEric Cheng  * Update all clients that share the same unicast address.
7738275SEric Cheng  */
7748275SEric Cheng void
7758275SEric Cheng mac_unicast_update_clients(mac_impl_t *mip, mac_address_t *map)
7768275SEric Cheng {
7778275SEric Cheng 	mac_client_impl_t *mcip;
7788275SEric Cheng 
7798275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
7808275SEric Cheng 
7818275SEric Cheng 	/*
7828275SEric Cheng 	 * Find all clients that share the same unicast MAC address and update
7838275SEric Cheng 	 * them appropriately.
7848275SEric Cheng 	 */
7858275SEric Cheng 	for (mcip = mip->mi_clients_list; mcip != NULL;
7868275SEric Cheng 	    mcip = mcip->mci_client_next) {
7878275SEric Cheng 		/*
7888275SEric Cheng 		 * Ignore clients that don't share this MAC address.
7898275SEric Cheng 		 */
7908275SEric Cheng 		if (map != mcip->mci_unicast)
7918275SEric Cheng 			continue;
7928275SEric Cheng 
7938275SEric Cheng 		/*
7948275SEric Cheng 		 * Update those clients with same old unicast MAC address.
7958275SEric Cheng 		 */
7968275SEric Cheng 		mac_unicast_update_client_flow(mcip);
7978275SEric Cheng 	}
7988275SEric Cheng }
7998275SEric Cheng 
8008275SEric Cheng /*
8018275SEric Cheng  * Update the unicast MAC address of the specified VNIC MAC client.
8028275SEric Cheng  *
8038275SEric Cheng  * Check whether the operation is valid. Any of following cases should fail:
8048275SEric Cheng  *
8058275SEric Cheng  * 1. It's a VLAN type of VNIC.
8068275SEric Cheng  * 2. The new value is current "primary" MAC address.
8078275SEric Cheng  * 3. The current MAC address is shared with other clients.
8088275SEric Cheng  * 4. The new MAC address has been used. This case will be valid when
8098275SEric Cheng  *    client migration is fully supported.
8108275SEric Cheng  */
8118275SEric Cheng int
8128275SEric Cheng mac_vnic_unicast_set(mac_client_handle_t mch, const uint8_t *addr)
8138275SEric Cheng {
8148275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
8158275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
8168275SEric Cheng 	mac_address_t *map = mcip->mci_unicast;
8178275SEric Cheng 	int err;
8188275SEric Cheng 
8198275SEric Cheng 	ASSERT(!(mip->mi_state_flags & MIS_IS_VNIC));
8208275SEric Cheng 	ASSERT(mcip->mci_state_flags & MCIS_IS_VNIC);
8218275SEric Cheng 	ASSERT(mcip->mci_flags != MAC_CLIENT_FLAGS_PRIMARY);
8228275SEric Cheng 
8238275SEric Cheng 	i_mac_perim_enter(mip);
8248275SEric Cheng 
8258275SEric Cheng 	/*
8268275SEric Cheng 	 * If this is a VLAN type of VNIC, it's using "primary" MAC address
8278275SEric Cheng 	 * of the underlying interface. Must fail here. Refer to case 1 above.
8288275SEric Cheng 	 */
8298275SEric Cheng 	if (bcmp(map->ma_addr, mip->mi_addr, map->ma_len) == 0) {
8308275SEric Cheng 		i_mac_perim_exit(mip);
8318275SEric Cheng 		return (ENOTSUP);
8328275SEric Cheng 	}
8338275SEric Cheng 
8348275SEric Cheng 	/*
8358275SEric Cheng 	 * If the new address is the "primary" one, must fail. Refer to
8368275SEric Cheng 	 * case 2 above.
8378275SEric Cheng 	 */
8388275SEric Cheng 	if (bcmp(addr, mip->mi_addr, map->ma_len) == 0) {
8398275SEric Cheng 		i_mac_perim_exit(mip);
8408275SEric Cheng 		return (EACCES);
8418275SEric Cheng 	}
8428275SEric Cheng 
8438275SEric Cheng 	/*
8448275SEric Cheng 	 * If the address is shared by multiple clients, must fail. Refer
8458275SEric Cheng 	 * to case 3 above.
8468275SEric Cheng 	 */
8478275SEric Cheng 	if (mac_check_macaddr_shared(map)) {
8488275SEric Cheng 		i_mac_perim_exit(mip);
8498275SEric Cheng 		return (EBUSY);
8508275SEric Cheng 	}
8518275SEric Cheng 
8528275SEric Cheng 	/*
8538275SEric Cheng 	 * If the new address has been used, must fail for now. Refer to
8548275SEric Cheng 	 * case 4 above.
8558275SEric Cheng 	 */
8568275SEric Cheng 	if (mac_find_macaddr(mip, (uint8_t *)addr) != NULL) {
8578275SEric Cheng 		i_mac_perim_exit(mip);
8588275SEric Cheng 		return (ENOTSUP);
8598275SEric Cheng 	}
8608275SEric Cheng 
8618275SEric Cheng 	/*
8628275SEric Cheng 	 * Update the MAC address.
8638275SEric Cheng 	 */
8648275SEric Cheng 	err = mac_update_macaddr(map, (uint8_t *)addr);
8658275SEric Cheng 
8668275SEric Cheng 	if (err != 0) {
8678275SEric Cheng 		i_mac_perim_exit(mip);
8688275SEric Cheng 		return (err);
8698275SEric Cheng 	}
8708275SEric Cheng 
8718275SEric Cheng 	/*
8728275SEric Cheng 	 * Update all flows of this MAC client.
8738275SEric Cheng 	 */
8748275SEric Cheng 	mac_unicast_update_client_flow(mcip);
8758275SEric Cheng 
8768275SEric Cheng 	i_mac_perim_exit(mip);
8778275SEric Cheng 	return (0);
8788275SEric Cheng }
8798275SEric Cheng 
8808275SEric Cheng /*
8818275SEric Cheng  * Program the new primary unicast address of the specified MAC.
8828275SEric Cheng  *
8838275SEric Cheng  * Function mac_update_macaddr() takes care different types of underlying
8848275SEric Cheng  * MAC. If the underlying MAC is VNIC, the VNIC driver must have registerd
8858275SEric Cheng  * mi_unicst() entry point, that indirectly calls mac_vnic_unicast_set()
8868275SEric Cheng  * which will take care of updating the MAC address of the corresponding
8878275SEric Cheng  * MAC client.
8888275SEric Cheng  *
8898275SEric Cheng  * This is the only interface that allow the client to update the "primary"
8908275SEric Cheng  * MAC address of the underlying MAC. The new value must have not been
8918275SEric Cheng  * used by other clients.
8928275SEric Cheng  */
8938275SEric Cheng int
8948275SEric Cheng mac_unicast_primary_set(mac_handle_t mh, const uint8_t *addr)
8958275SEric Cheng {
8968275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
8978275SEric Cheng 	mac_address_t *map;
8988275SEric Cheng 	int err;
8998275SEric Cheng 
9008275SEric Cheng 	/* verify the address validity */
9018275SEric Cheng 	if (!mac_unicst_verify(mh, addr, mip->mi_type->mt_addr_length))
9028275SEric Cheng 		return (EINVAL);
9038275SEric Cheng 
9048275SEric Cheng 	i_mac_perim_enter(mip);
9058275SEric Cheng 
9068275SEric Cheng 	/*
9078275SEric Cheng 	 * If the new value is the same as the current primary address value,
9088275SEric Cheng 	 * there's nothing to do.
9098275SEric Cheng 	 */
9108275SEric Cheng 	if (bcmp(addr, mip->mi_addr, mip->mi_type->mt_addr_length) == 0) {
9118275SEric Cheng 		i_mac_perim_exit(mip);
9128275SEric Cheng 		return (0);
9138275SEric Cheng 	}
9148275SEric Cheng 
9158275SEric Cheng 	if (mac_find_macaddr(mip, (uint8_t *)addr) != 0) {
9168275SEric Cheng 		i_mac_perim_exit(mip);
9178275SEric Cheng 		return (EBUSY);
9188275SEric Cheng 	}
9198275SEric Cheng 
9208275SEric Cheng 	map = mac_find_macaddr(mip, mip->mi_addr);
9218275SEric Cheng 	ASSERT(map != NULL);
9228275SEric Cheng 
9238275SEric Cheng 	/*
9248275SEric Cheng 	 * Update the MAC address.
9258275SEric Cheng 	 */
9268275SEric Cheng 	if (mip->mi_state_flags & MIS_IS_AGGR) {
9278275SEric Cheng 		mac_capab_aggr_t aggr_cap;
9288275SEric Cheng 
9298275SEric Cheng 		/*
9308275SEric Cheng 		 * If the mac is an aggregation, other than the unicast
9318275SEric Cheng 		 * addresses programming, aggr must be informed about this
9328275SEric Cheng 		 * primary unicst address change to change its mac address
9338275SEric Cheng 		 * policy to be user-specified.
9348275SEric Cheng 		 */
9358275SEric Cheng 		ASSERT(map->ma_type == MAC_ADDRESS_TYPE_UNICAST_CLASSIFIED);
9368275SEric Cheng 		VERIFY(i_mac_capab_get(mh, MAC_CAPAB_AGGR, &aggr_cap));
9378275SEric Cheng 		err = aggr_cap.mca_unicst(mip->mi_driver, addr);
9388275SEric Cheng 		if (err == 0)
9398275SEric Cheng 			bcopy(addr, map->ma_addr, map->ma_len);
9408275SEric Cheng 	} else {
9418275SEric Cheng 		err = mac_update_macaddr(map, (uint8_t *)addr);
9428275SEric Cheng 	}
9438275SEric Cheng 
9448275SEric Cheng 	if (err != 0) {
9458275SEric Cheng 		i_mac_perim_exit(mip);
9468275SEric Cheng 		return (err);
9478275SEric Cheng 	}
9488275SEric Cheng 
9498275SEric Cheng 	mac_unicast_update_clients(mip, map);
9508275SEric Cheng 
9518275SEric Cheng 	/*
9528275SEric Cheng 	 * Save the new primary MAC address in mac_impl_t.
9538275SEric Cheng 	 */
9548275SEric Cheng 	bcopy(addr, mip->mi_addr, mip->mi_type->mt_addr_length);
9558275SEric Cheng 
9568275SEric Cheng 	i_mac_perim_exit(mip);
9578275SEric Cheng 
9588275SEric Cheng 	if (err == 0)
9598275SEric Cheng 		i_mac_notify(mip, MAC_NOTE_UNICST);
9608275SEric Cheng 
9618275SEric Cheng 	return (err);
9628275SEric Cheng }
9638275SEric Cheng 
9648275SEric Cheng /*
9658275SEric Cheng  * Return the current primary MAC address of the specified MAC.
9668275SEric Cheng  */
9678275SEric Cheng void
9688275SEric Cheng mac_unicast_primary_get(mac_handle_t mh, uint8_t *addr)
9698275SEric Cheng {
9708275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
9718275SEric Cheng 
9728275SEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_READER);
9738275SEric Cheng 	bcopy(mip->mi_addr, addr, mip->mi_type->mt_addr_length);
9748275SEric Cheng 	rw_exit(&mip->mi_rw_lock);
9758275SEric Cheng }
9768275SEric Cheng 
9778275SEric Cheng /*
9788275SEric Cheng  * Return information about the use of the primary MAC address of the
9798275SEric Cheng  * specified MAC instance:
9808275SEric Cheng  *
9818275SEric Cheng  * - if client_name is non-NULL, it must point to a string of at
9828275SEric Cheng  *   least MAXNAMELEN bytes, and will be set to the name of the MAC
9838275SEric Cheng  *   client which uses the primary MAC address.
9848275SEric Cheng  *
9858275SEric Cheng  * - if in_use is non-NULL, used to return whether the primary MAC
9868275SEric Cheng  *   address is currently in use.
9878275SEric Cheng  */
9888275SEric Cheng void
9898275SEric Cheng mac_unicast_primary_info(mac_handle_t mh, char *client_name, boolean_t *in_use)
9908275SEric Cheng {
9918275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
9928275SEric Cheng 	mac_client_impl_t *cur_client;
9938275SEric Cheng 
9948275SEric Cheng 	if (in_use != NULL)
9958275SEric Cheng 		*in_use = B_FALSE;
9968275SEric Cheng 	if (client_name != NULL)
9978275SEric Cheng 		bzero(client_name, MAXNAMELEN);
9988275SEric Cheng 
9998275SEric Cheng 	/*
10008275SEric Cheng 	 * The mi_rw_lock is used to protect threads that don't hold the
10018275SEric Cheng 	 * mac perimeter to get a consistent view of the mi_clients_list.
10028275SEric Cheng 	 * Threads that modify the list must hold both the mac perimeter and
10038275SEric Cheng 	 * mi_rw_lock(RW_WRITER)
10048275SEric Cheng 	 */
10058275SEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_READER);
10068275SEric Cheng 	for (cur_client = mip->mi_clients_list; cur_client != NULL;
10078275SEric Cheng 	    cur_client = cur_client->mci_client_next) {
10088275SEric Cheng 		if (mac_is_primary_client(cur_client) ||
10098275SEric Cheng 		    (mip->mi_state_flags & MIS_IS_VNIC)) {
10108275SEric Cheng 			rw_exit(&mip->mi_rw_lock);
10118275SEric Cheng 			if (in_use != NULL)
10128275SEric Cheng 				*in_use = B_TRUE;
10138275SEric Cheng 			if (client_name != NULL) {
10148275SEric Cheng 				bcopy(cur_client->mci_name, client_name,
10158275SEric Cheng 				    MAXNAMELEN);
10168275SEric Cheng 			}
10178275SEric Cheng 			return;
10188275SEric Cheng 		}
10198275SEric Cheng 	}
10208275SEric Cheng 	rw_exit(&mip->mi_rw_lock);
10218275SEric Cheng }
10228275SEric Cheng 
10238275SEric Cheng /*
102410616SSebastien.Roy@Sun.COM  * Return the current destination MAC address of the specified MAC.
102510616SSebastien.Roy@Sun.COM  */
102610616SSebastien.Roy@Sun.COM boolean_t
102710616SSebastien.Roy@Sun.COM mac_dst_get(mac_handle_t mh, uint8_t *addr)
102810616SSebastien.Roy@Sun.COM {
102910616SSebastien.Roy@Sun.COM 	mac_impl_t *mip = (mac_impl_t *)mh;
103010616SSebastien.Roy@Sun.COM 
103110616SSebastien.Roy@Sun.COM 	rw_enter(&mip->mi_rw_lock, RW_READER);
103210616SSebastien.Roy@Sun.COM 	if (mip->mi_dstaddr_set)
103310616SSebastien.Roy@Sun.COM 		bcopy(mip->mi_dstaddr, addr, mip->mi_type->mt_addr_length);
103410616SSebastien.Roy@Sun.COM 	rw_exit(&mip->mi_rw_lock);
103510616SSebastien.Roy@Sun.COM 	return (mip->mi_dstaddr_set);
103610616SSebastien.Roy@Sun.COM }
103710616SSebastien.Roy@Sun.COM 
103810616SSebastien.Roy@Sun.COM /*
10398275SEric Cheng  * Add the specified MAC client to the list of clients which opened
10408275SEric Cheng  * the specified MAC.
10418275SEric Cheng  */
10428275SEric Cheng static void
10438275SEric Cheng mac_client_add(mac_client_impl_t *mcip)
10448275SEric Cheng {
10458275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
10468275SEric Cheng 
10478275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
10488275SEric Cheng 
10498275SEric Cheng 	/* add VNIC to the front of the list */
10508275SEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
10518275SEric Cheng 	mcip->mci_client_next = mip->mi_clients_list;
10528275SEric Cheng 	mip->mi_clients_list = mcip;
10538275SEric Cheng 	mip->mi_nclients++;
10548275SEric Cheng 	rw_exit(&mip->mi_rw_lock);
10558275SEric Cheng }
10568275SEric Cheng 
10578275SEric Cheng /*
10588275SEric Cheng  * Remove the specified MAC client from the list of clients which opened
10598275SEric Cheng  * the specified MAC.
10608275SEric Cheng  */
10618275SEric Cheng static void
10628275SEric Cheng mac_client_remove(mac_client_impl_t *mcip)
10638275SEric Cheng {
10648275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
10658275SEric Cheng 	mac_client_impl_t **prev, *cclient;
10668275SEric Cheng 
10678275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
10688275SEric Cheng 
10698275SEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
10708275SEric Cheng 	prev = &mip->mi_clients_list;
10718275SEric Cheng 	cclient = *prev;
10728275SEric Cheng 	while (cclient != NULL && cclient != mcip) {
10738275SEric Cheng 		prev = &cclient->mci_client_next;
10748275SEric Cheng 		cclient = *prev;
10758275SEric Cheng 	}
10768275SEric Cheng 	ASSERT(cclient != NULL);
10778275SEric Cheng 	*prev = cclient->mci_client_next;
10788275SEric Cheng 	mip->mi_nclients--;
10798275SEric Cheng 	rw_exit(&mip->mi_rw_lock);
10808275SEric Cheng }
10818275SEric Cheng 
10828275SEric Cheng static mac_unicast_impl_t *
10838275SEric Cheng mac_client_find_vid(mac_client_impl_t *mcip, uint16_t vid)
10848275SEric Cheng {
10858275SEric Cheng 	mac_unicast_impl_t *muip = mcip->mci_unicast_list;
10868275SEric Cheng 
10878275SEric Cheng 	while ((muip != NULL) && (muip->mui_vid != vid))
10888275SEric Cheng 		muip = muip->mui_next;
10898275SEric Cheng 
10908275SEric Cheng 	return (muip);
10918275SEric Cheng }
10928275SEric Cheng 
10938275SEric Cheng /*
10948275SEric Cheng  * Return whether the specified (MAC address, VID) tuple is already used by
10958275SEric Cheng  * one of the MAC clients associated with the specified MAC.
10968275SEric Cheng  */
10978275SEric Cheng static boolean_t
10988275SEric Cheng mac_addr_in_use(mac_impl_t *mip, uint8_t *mac_addr, uint16_t vid)
10998275SEric Cheng {
11008275SEric Cheng 	mac_client_impl_t *client;
11018275SEric Cheng 	mac_address_t *map;
11028275SEric Cheng 
11038275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
11048275SEric Cheng 
11058275SEric Cheng 	for (client = mip->mi_clients_list; client != NULL;
11068275SEric Cheng 	    client = client->mci_client_next) {
11078275SEric Cheng 
11088275SEric Cheng 		/*
11098275SEric Cheng 		 * Ignore clients that don't have unicast address.
11108275SEric Cheng 		 */
11118275SEric Cheng 		if (client->mci_unicast_list == NULL)
11128275SEric Cheng 			continue;
11138275SEric Cheng 
11148275SEric Cheng 		map = client->mci_unicast;
11158275SEric Cheng 
11168275SEric Cheng 		if ((bcmp(mac_addr, map->ma_addr, map->ma_len) == 0) &&
11178275SEric Cheng 		    (mac_client_find_vid(client, vid) != NULL)) {
11188275SEric Cheng 			return (B_TRUE);
11198275SEric Cheng 		}
11208275SEric Cheng 	}
11218275SEric Cheng 
11228275SEric Cheng 	return (B_FALSE);
11238275SEric Cheng }
11248275SEric Cheng 
11258275SEric Cheng /*
11268275SEric Cheng  * Generate a random MAC address. The MAC address prefix is
11278275SEric Cheng  * stored in the array pointed to by mac_addr, and its length, in bytes,
11288275SEric Cheng  * is specified by prefix_len. The least significant bits
11298275SEric Cheng  * after prefix_len bytes are generated, and stored after the prefix
11308275SEric Cheng  * in the mac_addr array.
11318275SEric Cheng  */
11328275SEric Cheng int
11338275SEric Cheng mac_addr_random(mac_client_handle_t mch, uint_t prefix_len,
11348275SEric Cheng     uint8_t *mac_addr, mac_diag_t *diag)
11358275SEric Cheng {
11368275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
11378275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
11388275SEric Cheng 	size_t addr_len = mip->mi_type->mt_addr_length;
11398275SEric Cheng 
11408275SEric Cheng 	if (prefix_len >= addr_len) {
11418275SEric Cheng 		*diag = MAC_DIAG_MACPREFIXLEN_INVALID;
11428275SEric Cheng 		return (EINVAL);
11438275SEric Cheng 	}
11448275SEric Cheng 
11458275SEric Cheng 	/* check the prefix value */
11468275SEric Cheng 	if (prefix_len > 0) {
11478275SEric Cheng 		bzero(mac_addr + prefix_len, addr_len - prefix_len);
11488275SEric Cheng 		if (!mac_unicst_verify((mac_handle_t)mip, mac_addr,
11498275SEric Cheng 		    addr_len)) {
11508275SEric Cheng 			*diag = MAC_DIAG_MACPREFIX_INVALID;
11518275SEric Cheng 			return (EINVAL);
11528275SEric Cheng 		}
11538275SEric Cheng 	}
11548275SEric Cheng 
11558275SEric Cheng 	/* generate the MAC address */
11568275SEric Cheng 	if (prefix_len < addr_len) {
11578275SEric Cheng 		(void) random_get_pseudo_bytes(mac_addr +
11588275SEric Cheng 		    prefix_len, addr_len - prefix_len);
11598275SEric Cheng 	}
11608275SEric Cheng 
11618275SEric Cheng 	*diag = 0;
11628275SEric Cheng 	return (0);
11638275SEric Cheng }
11648275SEric Cheng 
11658275SEric Cheng /*
11668275SEric Cheng  * Set the priority range for this MAC client. This will be used to
11678275SEric Cheng  * determine the absolute priority for the threads created for this
11688275SEric Cheng  * MAC client using the specified "low", "medium" and "high" level.
11698275SEric Cheng  * This will also be used for any subflows on this MAC client.
11708275SEric Cheng  */
11718275SEric Cheng #define	MAC_CLIENT_SET_PRIORITY_RANGE(mcip, pri) {			\
11728275SEric Cheng 	(mcip)->mci_min_pri = FLOW_MIN_PRIORITY(MINCLSYSPRI,	\
11738275SEric Cheng 	    MAXCLSYSPRI, (pri));					\
11748275SEric Cheng 	(mcip)->mci_max_pri = FLOW_MAX_PRIORITY(MINCLSYSPRI,	\
11758275SEric Cheng 	    MAXCLSYSPRI, (mcip)->mci_min_pri);				\
11768275SEric Cheng 	}
11778275SEric Cheng 
11788275SEric Cheng /*
11798275SEric Cheng  * MAC client open entry point. Return a new MAC client handle. Each
11808275SEric Cheng  * MAC client is associated with a name, specified through the 'name'
11818275SEric Cheng  * argument.
11828275SEric Cheng  */
11838275SEric Cheng int
11848275SEric Cheng mac_client_open(mac_handle_t mh, mac_client_handle_t *mchp, char *name,
11858275SEric Cheng     uint16_t flags)
11868275SEric Cheng {
11878275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
11888275SEric Cheng 	mac_client_impl_t *mcip;
11898275SEric Cheng 	int err = 0;
11908275SEric Cheng 	boolean_t share_desired =
11918275SEric Cheng 	    ((flags & MAC_OPEN_FLAGS_SHARES_DESIRED) != 0);
11928275SEric Cheng 	boolean_t no_hwrings = ((flags & MAC_OPEN_FLAGS_NO_HWRINGS) != 0);
11938275SEric Cheng 	boolean_t req_hwrings = ((flags & MAC_OPEN_FLAGS_REQ_HWRINGS) != 0);
11948275SEric Cheng 	flow_entry_t	*flent = NULL;
11958275SEric Cheng 
11968275SEric Cheng 	*mchp = NULL;
11978275SEric Cheng 	if (share_desired && no_hwrings) {
11988275SEric Cheng 		/* can't have shares but no hardware rings */
11998275SEric Cheng 		return (EINVAL);
12008275SEric Cheng 	}
12018275SEric Cheng 
12028275SEric Cheng 	i_mac_perim_enter(mip);
12038275SEric Cheng 
12048275SEric Cheng 	if (mip->mi_state_flags & MIS_IS_VNIC) {
12058275SEric Cheng 		/*
12068275SEric Cheng 		 * The underlying MAC is a VNIC. Return the MAC client
12078275SEric Cheng 		 * handle of the lower MAC which was obtained by
12088275SEric Cheng 		 * the VNIC driver when it did its mac_client_open().
12098275SEric Cheng 		 */
12108275SEric Cheng 
12118275SEric Cheng 		mcip = mac_vnic_lower(mip);
12128275SEric Cheng 
12138275SEric Cheng 		/*
12148275SEric Cheng 		 * Note that multiple mac clients share the same mcip in
12158275SEric Cheng 		 * this case.
12168275SEric Cheng 		 */
12178275SEric Cheng 		if (flags & MAC_OPEN_FLAGS_EXCLUSIVE)
12188275SEric Cheng 			mcip->mci_state_flags |= MCIS_EXCLUSIVE;
12198275SEric Cheng 
12209473SVenu.Iyer@Sun.COM 		if (flags & MAC_OPEN_FLAGS_MULTI_PRIMARY)
12219473SVenu.Iyer@Sun.COM 			mcip->mci_flags |= MAC_CLIENT_FLAGS_MULTI_PRIMARY;
12229473SVenu.Iyer@Sun.COM 
12238275SEric Cheng 		mip->mi_clients_list = mcip;
12248275SEric Cheng 		i_mac_perim_exit(mip);
12258275SEric Cheng 		*mchp = (mac_client_handle_t)mcip;
12268275SEric Cheng 		return (err);
12278275SEric Cheng 	}
12288275SEric Cheng 
12298275SEric Cheng 	mcip = kmem_cache_alloc(mac_client_impl_cache, KM_SLEEP);
12308275SEric Cheng 
12318275SEric Cheng 	mcip->mci_mip = mip;
12328275SEric Cheng 	mcip->mci_upper_mip = NULL;
12338275SEric Cheng 	mcip->mci_rx_fn = mac_pkt_drop;
12348275SEric Cheng 	mcip->mci_rx_arg = NULL;
12359473SVenu.Iyer@Sun.COM 	mcip->mci_rx_p_fn = NULL;
12369473SVenu.Iyer@Sun.COM 	mcip->mci_rx_p_arg = NULL;
12379473SVenu.Iyer@Sun.COM 	mcip->mci_p_unicast_list = NULL;
12388275SEric Cheng 	mcip->mci_direct_rx_fn = NULL;
12398275SEric Cheng 	mcip->mci_direct_rx_arg = NULL;
12408275SEric Cheng 
12419473SVenu.Iyer@Sun.COM 	mcip->mci_unicast_list = NULL;
12429473SVenu.Iyer@Sun.COM 
12438275SEric Cheng 	if ((flags & MAC_OPEN_FLAGS_IS_VNIC) != 0)
12448275SEric Cheng 		mcip->mci_state_flags |= MCIS_IS_VNIC;
12458275SEric Cheng 
12468275SEric Cheng 	if ((flags & MAC_OPEN_FLAGS_EXCLUSIVE) != 0)
12478275SEric Cheng 		mcip->mci_state_flags |= MCIS_EXCLUSIVE;
12488275SEric Cheng 
12498275SEric Cheng 	if ((flags & MAC_OPEN_FLAGS_IS_AGGR_PORT) != 0)
12508275SEric Cheng 		mcip->mci_state_flags |= MCIS_IS_AGGR_PORT;
12518275SEric Cheng 
12528275SEric Cheng 	if ((flags & MAC_OPEN_FLAGS_USE_DATALINK_NAME) != 0) {
12538275SEric Cheng 		datalink_id_t	linkid;
12548275SEric Cheng 
12558275SEric Cheng 		ASSERT(name == NULL);
12568275SEric Cheng 		if ((err = dls_devnet_macname2linkid(mip->mi_name,
12578275SEric Cheng 		    &linkid)) != 0) {
12588275SEric Cheng 			goto done;
12598275SEric Cheng 		}
12608275SEric Cheng 		if ((err = dls_mgmt_get_linkinfo(linkid, mcip->mci_name, NULL,
12618275SEric Cheng 		    NULL, NULL)) != 0) {
12628275SEric Cheng 			/*
12638275SEric Cheng 			 * Use mac name if dlmgmtd is not available.
12648275SEric Cheng 			 */
12658275SEric Cheng 			if (err == EBADF) {
12668275SEric Cheng 				(void) strlcpy(mcip->mci_name, mip->mi_name,
12678275SEric Cheng 				    sizeof (mcip->mci_name));
12688275SEric Cheng 				err = 0;
12698275SEric Cheng 			} else {
12708275SEric Cheng 				goto done;
12718275SEric Cheng 			}
12728275SEric Cheng 		}
12738275SEric Cheng 		mcip->mci_state_flags |= MCIS_USE_DATALINK_NAME;
12748275SEric Cheng 	} else {
12758275SEric Cheng 		ASSERT(name != NULL);
12768275SEric Cheng 		if (strlen(name) > MAXNAMELEN) {
12778275SEric Cheng 			err = EINVAL;
12788275SEric Cheng 			goto done;
12798275SEric Cheng 		}
12808275SEric Cheng 		(void) strlcpy(mcip->mci_name, name, sizeof (mcip->mci_name));
12818275SEric Cheng 	}
12829473SVenu.Iyer@Sun.COM 
12839473SVenu.Iyer@Sun.COM 	if (flags & MAC_OPEN_FLAGS_MULTI_PRIMARY)
12849473SVenu.Iyer@Sun.COM 		mcip->mci_flags |= MAC_CLIENT_FLAGS_MULTI_PRIMARY;
12859473SVenu.Iyer@Sun.COM 
12868275SEric Cheng 	/* the subflow table will be created dynamically */
12878275SEric Cheng 	mcip->mci_subflow_tab = NULL;
12888275SEric Cheng 	mcip->mci_stat_multircv = 0;
12898275SEric Cheng 	mcip->mci_stat_brdcstrcv = 0;
12908275SEric Cheng 	mcip->mci_stat_multixmt = 0;
12918275SEric Cheng 	mcip->mci_stat_brdcstxmt = 0;
12928275SEric Cheng 
12938275SEric Cheng 	mcip->mci_stat_obytes = 0;
12948275SEric Cheng 	mcip->mci_stat_opackets = 0;
12958275SEric Cheng 	mcip->mci_stat_oerrors = 0;
12968275SEric Cheng 	mcip->mci_stat_ibytes = 0;
12978275SEric Cheng 	mcip->mci_stat_ipackets = 0;
12988275SEric Cheng 	mcip->mci_stat_ierrors = 0;
12998275SEric Cheng 
13008275SEric Cheng 	/* Create an initial flow */
13018275SEric Cheng 
13028275SEric Cheng 	err = mac_flow_create(NULL, NULL, mcip->mci_name, NULL,
13038275SEric Cheng 	    mcip->mci_state_flags & MCIS_IS_VNIC ? FLOW_VNIC_MAC :
13048275SEric Cheng 	    FLOW_PRIMARY_MAC, &flent);
13058275SEric Cheng 	if (err != 0)
13068275SEric Cheng 		goto done;
13078275SEric Cheng 	mcip->mci_flent = flent;
13088275SEric Cheng 	FLOW_MARK(flent, FE_MC_NO_DATAPATH);
13098275SEric Cheng 	flent->fe_mcip = mcip;
13108275SEric Cheng 	/*
13118275SEric Cheng 	 * Place initial creation reference on the flow. This reference
13128275SEric Cheng 	 * is released in the corresponding delete action viz.
13138275SEric Cheng 	 * mac_unicast_remove after waiting for all transient refs to
13148275SEric Cheng 	 * to go away. The wait happens in mac_flow_wait.
13158275SEric Cheng 	 */
13168275SEric Cheng 	FLOW_REFHOLD(flent);
13178275SEric Cheng 
13188275SEric Cheng 	/*
13198275SEric Cheng 	 * Do this ahead of the mac_bcast_add() below so that the mi_nclients
13208275SEric Cheng 	 * will have the right value for mac_rx_srs_setup().
13218275SEric Cheng 	 */
13228275SEric Cheng 	mac_client_add(mcip);
13238275SEric Cheng 
13248400SNicolas.Droux@Sun.COM 	if (no_hwrings)
13258400SNicolas.Droux@Sun.COM 		mcip->mci_state_flags |= MCIS_NO_HWRINGS;
13268400SNicolas.Droux@Sun.COM 	if (req_hwrings)
13278400SNicolas.Droux@Sun.COM 		mcip->mci_state_flags |= MCIS_REQ_HWRINGS;
13288275SEric Cheng 	mcip->mci_share = NULL;
13298275SEric Cheng 	if (share_desired) {
13308275SEric Cheng 		ASSERT(!no_hwrings);
13318275SEric Cheng 		i_mac_share_alloc(mcip);
13328275SEric Cheng 	}
13338275SEric Cheng 
13348275SEric Cheng 	DTRACE_PROBE2(mac__client__open__allocated, mac_impl_t *,
13358275SEric Cheng 	    mcip->mci_mip, mac_client_impl_t *, mcip);
13368275SEric Cheng 	*mchp = (mac_client_handle_t)mcip;
13378275SEric Cheng 
13388275SEric Cheng 	i_mac_perim_exit(mip);
13398275SEric Cheng 	return (0);
13408275SEric Cheng 
13418275SEric Cheng done:
13428275SEric Cheng 	i_mac_perim_exit(mip);
13438275SEric Cheng 	mcip->mci_state_flags = 0;
13448275SEric Cheng 	mcip->mci_tx_flag = 0;
13458275SEric Cheng 	kmem_cache_free(mac_client_impl_cache, mcip);
13468275SEric Cheng 	return (err);
13478275SEric Cheng }
13488275SEric Cheng 
13498275SEric Cheng /*
13508275SEric Cheng  * Close the specified MAC client handle.
13518275SEric Cheng  */
13528275SEric Cheng void
13538275SEric Cheng mac_client_close(mac_client_handle_t mch, uint16_t flags)
13548275SEric Cheng {
13558275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
13568275SEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
13578275SEric Cheng 	flow_entry_t		*flent;
13588275SEric Cheng 
13598275SEric Cheng 	i_mac_perim_enter(mip);
13608275SEric Cheng 
13618275SEric Cheng 	if (flags & MAC_CLOSE_FLAGS_EXCLUSIVE)
13628275SEric Cheng 		mcip->mci_state_flags &= ~MCIS_EXCLUSIVE;
13638275SEric Cheng 
13648275SEric Cheng 	if ((mcip->mci_state_flags & MCIS_IS_VNIC) &&
13658275SEric Cheng 	    !(flags & MAC_CLOSE_FLAGS_IS_VNIC)) {
13668275SEric Cheng 		/*
13678275SEric Cheng 		 * This is an upper VNIC client initiated operation.
13688275SEric Cheng 		 * The lower MAC client will be closed by the VNIC driver
13698275SEric Cheng 		 * when the VNIC is deleted.
13708275SEric Cheng 		 */
13718275SEric Cheng 
13728275SEric Cheng 		i_mac_perim_exit(mip);
13738275SEric Cheng 		return;
13748275SEric Cheng 	}
13758275SEric Cheng 
13768275SEric Cheng 	/*
13778275SEric Cheng 	 * Remove the flent associated with the MAC client
13788275SEric Cheng 	 */
13798275SEric Cheng 	flent = mcip->mci_flent;
13808275SEric Cheng 	mcip->mci_flent = NULL;
13818275SEric Cheng 	FLOW_FINAL_REFRELE(flent);
13828275SEric Cheng 
13838275SEric Cheng 	/*
13848275SEric Cheng 	 * MAC clients must remove the unicast addresses and promisc callbacks
13858275SEric Cheng 	 * they added before issuing a mac_client_close().
13868275SEric Cheng 	 */
13878275SEric Cheng 	ASSERT(mcip->mci_unicast_list == NULL);
13888275SEric Cheng 	ASSERT(mcip->mci_promisc_list == NULL);
13898275SEric Cheng 	ASSERT(mcip->mci_tx_notify_cb_list == NULL);
13908275SEric Cheng 
13918275SEric Cheng 	i_mac_share_free(mcip);
13928275SEric Cheng 
13938275SEric Cheng 	mac_client_remove(mcip);
13948275SEric Cheng 
13958275SEric Cheng 	i_mac_perim_exit(mip);
13968275SEric Cheng 	mcip->mci_subflow_tab = NULL;
13978275SEric Cheng 	mcip->mci_state_flags = 0;
13988275SEric Cheng 	mcip->mci_tx_flag = 0;
13998275SEric Cheng 	kmem_cache_free(mac_client_impl_cache, mch);
14008275SEric Cheng }
14018275SEric Cheng 
14028275SEric Cheng /*
140311021SEric.Cheng@Sun.COM  * Set the rx bypass receive callback.
14048275SEric Cheng  */
14058275SEric Cheng boolean_t
14068275SEric Cheng mac_rx_bypass_set(mac_client_handle_t mch, mac_direct_rx_t rx_fn, void *arg1)
14078275SEric Cheng {
14088275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
14098275SEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
14108275SEric Cheng 
14118275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
14128275SEric Cheng 
14138275SEric Cheng 	/*
14148833SVenu.Iyer@Sun.COM 	 * If the mac_client is a VLAN, we should not do DLS bypass and
14158833SVenu.Iyer@Sun.COM 	 * instead let the packets come up via mac_rx_deliver so the vlan
14168833SVenu.Iyer@Sun.COM 	 * header can be stripped.
14178275SEric Cheng 	 */
14188833SVenu.Iyer@Sun.COM 	if (mcip->mci_nvids > 0)
14198275SEric Cheng 		return (B_FALSE);
14208275SEric Cheng 
14218275SEric Cheng 	/*
14228275SEric Cheng 	 * These are not accessed directly in the data path, and hence
14238275SEric Cheng 	 * don't need any protection
14248275SEric Cheng 	 */
14258275SEric Cheng 	mcip->mci_direct_rx_fn = rx_fn;
14268275SEric Cheng 	mcip->mci_direct_rx_arg = arg1;
14278275SEric Cheng 	return (B_TRUE);
14288275SEric Cheng }
14298275SEric Cheng 
14308275SEric Cheng /*
143111021SEric.Cheng@Sun.COM  * Enable/Disable rx bypass. By default, bypass is assumed to be enabled.
143211021SEric.Cheng@Sun.COM  */
143311021SEric.Cheng@Sun.COM void
143411021SEric.Cheng@Sun.COM mac_rx_bypass_enable(mac_client_handle_t mch)
143511021SEric.Cheng@Sun.COM {
143611021SEric.Cheng@Sun.COM 	((mac_client_impl_t *)mch)->mci_state_flags &= ~MCIS_RX_BYPASS_DISABLE;
143711021SEric.Cheng@Sun.COM }
143811021SEric.Cheng@Sun.COM 
143911021SEric.Cheng@Sun.COM void
144011021SEric.Cheng@Sun.COM mac_rx_bypass_disable(mac_client_handle_t mch)
144111021SEric.Cheng@Sun.COM {
144211021SEric.Cheng@Sun.COM 	((mac_client_impl_t *)mch)->mci_state_flags |= MCIS_RX_BYPASS_DISABLE;
144311021SEric.Cheng@Sun.COM }
144411021SEric.Cheng@Sun.COM 
144511021SEric.Cheng@Sun.COM /*
14468275SEric Cheng  * Set the receive callback for the specified MAC client. There can be
14478275SEric Cheng  * at most one such callback per MAC client.
14488275SEric Cheng  */
14498275SEric Cheng void
14508275SEric Cheng mac_rx_set(mac_client_handle_t mch, mac_rx_t rx_fn, void *arg)
14518275SEric Cheng {
14528275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
14538275SEric Cheng 	mac_impl_t	*mip = mcip->mci_mip;
14548275SEric Cheng 
14558275SEric Cheng 	/*
14568275SEric Cheng 	 * Instead of adding an extra set of locks and refcnts in
14578275SEric Cheng 	 * the datapath at the mac client boundary, we temporarily quiesce
14588275SEric Cheng 	 * the SRS and related entities. We then change the receive function
14598275SEric Cheng 	 * without interference from any receive data thread and then reenable
14608275SEric Cheng 	 * the data flow subsequently.
14618275SEric Cheng 	 */
14628275SEric Cheng 	i_mac_perim_enter(mip);
14638275SEric Cheng 	mac_rx_client_quiesce(mch);
14648275SEric Cheng 
14658275SEric Cheng 	mcip->mci_rx_fn = rx_fn;
14668275SEric Cheng 	mcip->mci_rx_arg = arg;
14678275SEric Cheng 	mac_rx_client_restart(mch);
14688275SEric Cheng 	i_mac_perim_exit(mip);
14698275SEric Cheng }
14708275SEric Cheng 
14718275SEric Cheng /*
14728275SEric Cheng  * Reset the receive callback for the specified MAC client.
14738275SEric Cheng  */
14748275SEric Cheng void
14758275SEric Cheng mac_rx_clear(mac_client_handle_t mch)
14768275SEric Cheng {
14778275SEric Cheng 	mac_rx_set(mch, mac_pkt_drop, NULL);
14788275SEric Cheng }
14798275SEric Cheng 
14808275SEric Cheng /*
14818275SEric Cheng  * Walk the MAC client subflow table and updates their priority values.
14828275SEric Cheng  */
14838275SEric Cheng static int
14848275SEric Cheng mac_update_subflow_priority_cb(flow_entry_t *flent, void *arg)
14858275SEric Cheng {
14868275SEric Cheng 	mac_flow_update_priority(arg, flent);
14878275SEric Cheng 	return (0);
14888275SEric Cheng }
14898275SEric Cheng 
14908275SEric Cheng void
14918275SEric Cheng mac_update_subflow_priority(mac_client_impl_t *mcip)
14928275SEric Cheng {
14938275SEric Cheng 	(void) mac_flow_walk(mcip->mci_subflow_tab,
14948275SEric Cheng 	    mac_update_subflow_priority_cb, mcip);
14958275SEric Cheng }
14968275SEric Cheng 
14978275SEric Cheng /*
14988275SEric Cheng  * When the MAC client is being brought up (i.e. we do a unicast_add) we need
14998275SEric Cheng  * to initialize the cpu and resource control structure in the
15008275SEric Cheng  * mac_client_impl_t from the mac_impl_t (i.e if there are any cached
15018275SEric Cheng  * properties before the flow entry for the unicast address was created).
15028275SEric Cheng  */
15038275SEric Cheng int
15048275SEric Cheng mac_resource_ctl_set(mac_client_handle_t mch, mac_resource_props_t *mrp)
15058275SEric Cheng {
15068275SEric Cheng 	mac_client_impl_t 	*mcip = (mac_client_impl_t *)mch;
15078275SEric Cheng 	mac_impl_t		*mip = (mac_impl_t *)mcip->mci_mip;
15088275SEric Cheng 	int			err = 0;
15098275SEric Cheng 
15108275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
15118275SEric Cheng 
15128275SEric Cheng 	err = mac_validate_props(mrp);
15138275SEric Cheng 	if (err != 0)
15148275SEric Cheng 		return (err);
15158275SEric Cheng 
15168275SEric Cheng 	mac_update_resources(mrp, MCIP_RESOURCE_PROPS(mcip), B_FALSE);
15178275SEric Cheng 	if (MCIP_DATAPATH_SETUP(mcip)) {
15188275SEric Cheng 		/*
15198275SEric Cheng 		 * We have to set this prior to calling mac_flow_modify.
15208275SEric Cheng 		 */
15218275SEric Cheng 		if (mrp->mrp_mask & MRP_PRIORITY) {
15228275SEric Cheng 			if (mrp->mrp_priority == MPL_RESET) {
15238275SEric Cheng 				MAC_CLIENT_SET_PRIORITY_RANGE(mcip,
15248275SEric Cheng 				    MPL_LINK_DEFAULT);
15258275SEric Cheng 			} else {
15268275SEric Cheng 				MAC_CLIENT_SET_PRIORITY_RANGE(mcip,
15278275SEric Cheng 				    mrp->mrp_priority);
15288275SEric Cheng 			}
15298275SEric Cheng 		}
15308275SEric Cheng 
15318275SEric Cheng 		mac_flow_modify(mip->mi_flow_tab, mcip->mci_flent, mrp);
15328275SEric Cheng 		if (mrp->mrp_mask & MRP_PRIORITY)
15338275SEric Cheng 			mac_update_subflow_priority(mcip);
15348275SEric Cheng 		return (0);
15358275SEric Cheng 	}
15368275SEric Cheng 	return (0);
15378275SEric Cheng }
15388275SEric Cheng 
15398275SEric Cheng void
15408275SEric Cheng mac_resource_ctl_get(mac_client_handle_t mch, mac_resource_props_t *mrp)
15418275SEric Cheng {
15428275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
15438275SEric Cheng 	mac_resource_props_t	*mcip_mrp = MCIP_RESOURCE_PROPS(mcip);
15448275SEric Cheng 
15458275SEric Cheng 	bcopy(mcip_mrp, mrp, sizeof (mac_resource_props_t));
15468275SEric Cheng }
15478275SEric Cheng 
15488275SEric Cheng static int
15498275SEric Cheng mac_unicast_flow_create(mac_client_impl_t *mcip, uint8_t *mac_addr,
15508275SEric Cheng     uint16_t vid, boolean_t is_primary, boolean_t first_flow,
15518275SEric Cheng     flow_entry_t **flent, mac_resource_props_t *mrp)
15528275SEric Cheng {
15538275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mcip->mci_mip;
15548275SEric Cheng 	flow_desc_t	flow_desc;
15558558SGirish.Moodalbail@Sun.COM 	char		flowname[MAXFLOWNAMELEN];
15568275SEric Cheng 	int		err;
15578275SEric Cheng 	uint_t		flent_flags;
15588275SEric Cheng 
15598275SEric Cheng 	/*
15608275SEric Cheng 	 * First unicast address being added, create a new flow
15618275SEric Cheng 	 * for that MAC client.
15628275SEric Cheng 	 */
15638275SEric Cheng 	bzero(&flow_desc, sizeof (flow_desc));
15648275SEric Cheng 
15658275SEric Cheng 	flow_desc.fd_mac_len = mip->mi_type->mt_addr_length;
15668275SEric Cheng 	bcopy(mac_addr, flow_desc.fd_dst_mac, flow_desc.fd_mac_len);
15678275SEric Cheng 	flow_desc.fd_mask = FLOW_LINK_DST;
15688275SEric Cheng 	if (vid != 0) {
15698275SEric Cheng 		flow_desc.fd_vid = vid;
15708275SEric Cheng 		flow_desc.fd_mask |= FLOW_LINK_VID;
15718275SEric Cheng 	}
15728275SEric Cheng 
15738275SEric Cheng 	/*
15748275SEric Cheng 	 * XXX-nicolas. For now I'm keeping the FLOW_PRIMARY_MAC
15758275SEric Cheng 	 * and FLOW_VNIC. Even though they're a hack inherited
15768275SEric Cheng 	 * from the SRS code, we'll keep them for now. They're currently
15778275SEric Cheng 	 * consumed by mac_datapath_setup() to create the SRS.
15788275SEric Cheng 	 * That code should be eventually moved out of
15798275SEric Cheng 	 * mac_datapath_setup() and moved to a mac_srs_create()
15808275SEric Cheng 	 * function of some sort to keep things clean.
15818275SEric Cheng 	 *
15828275SEric Cheng 	 * Also, there's no reason why the SRS for the primary MAC
15838275SEric Cheng 	 * client should be different than any other MAC client. Until
15848275SEric Cheng 	 * this is cleaned-up, we support only one MAC unicast address
15858275SEric Cheng 	 * per client.
15868275SEric Cheng 	 *
15878275SEric Cheng 	 * We set FLOW_PRIMARY_MAC for the primary MAC address,
15888275SEric Cheng 	 * FLOW_VNIC for everything else.
15898275SEric Cheng 	 */
15908275SEric Cheng 	if (is_primary)
15918275SEric Cheng 		flent_flags = FLOW_PRIMARY_MAC;
15928275SEric Cheng 	else
15938275SEric Cheng 		flent_flags = FLOW_VNIC_MAC;
15948275SEric Cheng 
15958275SEric Cheng 	/*
15968275SEric Cheng 	 * For the first flow we use the mac client's name - mci_name, for
15978275SEric Cheng 	 * subsequent ones we just create a name with the vid. This is
15988275SEric Cheng 	 * so that we can add these flows to the same flow table. This is
15998275SEric Cheng 	 * fine as the flow name (except for the one with the mac client's
16008275SEric Cheng 	 * name) is not visible. When the first flow is removed, we just replace
16018275SEric Cheng 	 * its fdesc with another from the list, so we will still retain the
16028275SEric Cheng 	 * flent with the MAC client's flow name.
16038275SEric Cheng 	 */
16048275SEric Cheng 	if (first_flow) {
16058558SGirish.Moodalbail@Sun.COM 		bcopy(mcip->mci_name, flowname, MAXFLOWNAMELEN);
16068275SEric Cheng 	} else {
16078275SEric Cheng 		(void) sprintf(flowname, "%s%u", mcip->mci_name, vid);
16088275SEric Cheng 		flent_flags = FLOW_NO_STATS;
16098275SEric Cheng 	}
16108275SEric Cheng 
16118275SEric Cheng 	if ((err = mac_flow_create(&flow_desc, mrp, flowname, NULL,
16128275SEric Cheng 	    flent_flags, flent)) != 0)
16138275SEric Cheng 		return (err);
16148275SEric Cheng 
16158275SEric Cheng 	FLOW_MARK(*flent, FE_INCIPIENT);
16168275SEric Cheng 	(*flent)->fe_mcip = mcip;
16178275SEric Cheng 
16188275SEric Cheng 	/*
16198275SEric Cheng 	 * Place initial creation reference on the flow. This reference
16208275SEric Cheng 	 * is released in the corresponding delete action viz.
16218275SEric Cheng 	 * mac_unicast_remove after waiting for all transient refs to
16228275SEric Cheng 	 * to go away. The wait happens in mac_flow_wait.
16238275SEric Cheng 	 * We have already held the reference in mac_client_open().
16248275SEric Cheng 	 */
16258275SEric Cheng 	if (!first_flow)
16268275SEric Cheng 		FLOW_REFHOLD(*flent);
16278275SEric Cheng 	return (0);
16288275SEric Cheng }
16298275SEric Cheng 
16308275SEric Cheng /* Refresh the multicast grouping for this VID. */
16318275SEric Cheng int
16328275SEric Cheng mac_client_update_mcast(void *arg, boolean_t add, const uint8_t *addrp)
16338275SEric Cheng {
16348275SEric Cheng 	flow_entry_t		*flent = arg;
16358275SEric Cheng 	mac_client_impl_t	*mcip = flent->fe_mcip;
16368275SEric Cheng 	uint16_t		vid;
16378275SEric Cheng 	flow_desc_t		flow_desc;
16388275SEric Cheng 
16398275SEric Cheng 	mac_flow_get_desc(flent, &flow_desc);
16408275SEric Cheng 	vid = (flow_desc.fd_mask & FLOW_LINK_VID) != 0 ?
16418275SEric Cheng 	    flow_desc.fd_vid : VLAN_ID_NONE;
16428275SEric Cheng 
16438275SEric Cheng 	/*
16448275SEric Cheng 	 * We don't call mac_multicast_add()/mac_multicast_remove() as
16458275SEric Cheng 	 * we want to add/remove for this specific vid.
16468275SEric Cheng 	 */
16478275SEric Cheng 	if (add) {
16488275SEric Cheng 		return (mac_bcast_add(mcip, addrp, vid,
16498275SEric Cheng 		    MAC_ADDRTYPE_MULTICAST));
16508275SEric Cheng 	} else {
16518275SEric Cheng 		mac_bcast_delete(mcip, addrp, vid);
16528275SEric Cheng 		return (0);
16538275SEric Cheng 	}
16548275SEric Cheng }
16558275SEric Cheng 
16568833SVenu.Iyer@Sun.COM static void
16578833SVenu.Iyer@Sun.COM mac_update_single_active_client(mac_impl_t *mip)
16588833SVenu.Iyer@Sun.COM {
16598833SVenu.Iyer@Sun.COM 	mac_client_impl_t *client = NULL;
16608833SVenu.Iyer@Sun.COM 
16618833SVenu.Iyer@Sun.COM 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
16628833SVenu.Iyer@Sun.COM 
16638833SVenu.Iyer@Sun.COM 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
16648833SVenu.Iyer@Sun.COM 	if (mip->mi_nactiveclients == 1) {
16658833SVenu.Iyer@Sun.COM 		/*
16668833SVenu.Iyer@Sun.COM 		 * Find the one active MAC client from the list of MAC
16678833SVenu.Iyer@Sun.COM 		 * clients. The active MAC client has at least one
16688833SVenu.Iyer@Sun.COM 		 * unicast address.
16698833SVenu.Iyer@Sun.COM 		 */
16708833SVenu.Iyer@Sun.COM 		for (client = mip->mi_clients_list; client != NULL;
16718833SVenu.Iyer@Sun.COM 		    client = client->mci_client_next) {
16728833SVenu.Iyer@Sun.COM 			if (client->mci_unicast_list != NULL)
16738833SVenu.Iyer@Sun.COM 				break;
16748833SVenu.Iyer@Sun.COM 		}
16758833SVenu.Iyer@Sun.COM 		ASSERT(client != NULL);
16768833SVenu.Iyer@Sun.COM 	}
16778833SVenu.Iyer@Sun.COM 
16788833SVenu.Iyer@Sun.COM 	/*
16798833SVenu.Iyer@Sun.COM 	 * mi_single_active_client is protected by the MAC impl's read/writer
16808833SVenu.Iyer@Sun.COM 	 * lock, which allows mac_rx() to check the value of that pointer
16818833SVenu.Iyer@Sun.COM 	 * as a reader.
16828833SVenu.Iyer@Sun.COM 	 */
16838833SVenu.Iyer@Sun.COM 	mip->mi_single_active_client = client;
16848833SVenu.Iyer@Sun.COM 	rw_exit(&mip->mi_rw_lock);
16858833SVenu.Iyer@Sun.COM }
16868833SVenu.Iyer@Sun.COM 
16878275SEric Cheng /*
16889473SVenu.Iyer@Sun.COM  * Set up the data path. Called from i_mac_unicast_add after having
16899473SVenu.Iyer@Sun.COM  * done all the validations including making sure this is an active
16909473SVenu.Iyer@Sun.COM  * client (i.e that is ready to process packets.)
16919473SVenu.Iyer@Sun.COM  */
16929473SVenu.Iyer@Sun.COM static int
16939473SVenu.Iyer@Sun.COM mac_client_datapath_setup(mac_client_impl_t *mcip, uint16_t vid,
16949473SVenu.Iyer@Sun.COM     uint8_t *mac_addr, mac_resource_props_t *mrp, boolean_t isprimary,
16959473SVenu.Iyer@Sun.COM     mac_unicast_impl_t *muip)
16969473SVenu.Iyer@Sun.COM {
16979473SVenu.Iyer@Sun.COM 	mac_impl_t	*mip = mcip->mci_mip;
16989473SVenu.Iyer@Sun.COM 	boolean_t	mac_started = B_FALSE;
16999473SVenu.Iyer@Sun.COM 	boolean_t	bcast_added = B_FALSE;
17009473SVenu.Iyer@Sun.COM 	boolean_t	nactiveclients_added = B_FALSE;
17019473SVenu.Iyer@Sun.COM 	flow_entry_t	*flent;
17029473SVenu.Iyer@Sun.COM 	int		err = 0;
17039473SVenu.Iyer@Sun.COM 
17049473SVenu.Iyer@Sun.COM 	if ((err = mac_start((mac_handle_t)mip)) != 0)
17059473SVenu.Iyer@Sun.COM 		goto bail;
17069473SVenu.Iyer@Sun.COM 
17079473SVenu.Iyer@Sun.COM 	mac_started = B_TRUE;
17089473SVenu.Iyer@Sun.COM 
17099473SVenu.Iyer@Sun.COM 	/* add the MAC client to the broadcast address group by default */
17109473SVenu.Iyer@Sun.COM 	if (mip->mi_type->mt_brdcst_addr != NULL) {
17119473SVenu.Iyer@Sun.COM 		err = mac_bcast_add(mcip, mip->mi_type->mt_brdcst_addr, vid,
17129473SVenu.Iyer@Sun.COM 		    MAC_ADDRTYPE_BROADCAST);
17139473SVenu.Iyer@Sun.COM 		if (err != 0)
17149473SVenu.Iyer@Sun.COM 			goto bail;
17159473SVenu.Iyer@Sun.COM 		bcast_added = B_TRUE;
17169473SVenu.Iyer@Sun.COM 	}
17179473SVenu.Iyer@Sun.COM 
17189473SVenu.Iyer@Sun.COM 	/*
17199473SVenu.Iyer@Sun.COM 	 * If this is the first unicast address addition for this
17209473SVenu.Iyer@Sun.COM 	 * client, reuse the pre-allocated larval flow entry associated with
17219473SVenu.Iyer@Sun.COM 	 * the MAC client.
17229473SVenu.Iyer@Sun.COM 	 */
17239473SVenu.Iyer@Sun.COM 	flent = (mcip->mci_nflents == 0) ? mcip->mci_flent : NULL;
17249473SVenu.Iyer@Sun.COM 
17259473SVenu.Iyer@Sun.COM 	/* We are configuring the unicast flow now */
17269473SVenu.Iyer@Sun.COM 	if (!MCIP_DATAPATH_SETUP(mcip)) {
17279473SVenu.Iyer@Sun.COM 
17289473SVenu.Iyer@Sun.COM 		MAC_CLIENT_SET_PRIORITY_RANGE(mcip,
17299473SVenu.Iyer@Sun.COM 		    (mrp->mrp_mask & MRP_PRIORITY) ? mrp->mrp_priority :
17309473SVenu.Iyer@Sun.COM 		    MPL_LINK_DEFAULT);
17319473SVenu.Iyer@Sun.COM 
17329473SVenu.Iyer@Sun.COM 		if ((err = mac_unicast_flow_create(mcip, mac_addr, vid,
17339473SVenu.Iyer@Sun.COM 		    isprimary, B_TRUE, &flent, mrp)) != 0)
17349473SVenu.Iyer@Sun.COM 			goto bail;
17359473SVenu.Iyer@Sun.COM 
17369473SVenu.Iyer@Sun.COM 		mip->mi_nactiveclients++;
17379473SVenu.Iyer@Sun.COM 		nactiveclients_added = B_TRUE;
17389473SVenu.Iyer@Sun.COM 
17399473SVenu.Iyer@Sun.COM 		/*
17409473SVenu.Iyer@Sun.COM 		 * This will allocate the RX ring group if possible for the
17419473SVenu.Iyer@Sun.COM 		 * flow and program the software classifier as needed.
17429473SVenu.Iyer@Sun.COM 		 */
17439473SVenu.Iyer@Sun.COM 		if ((err = mac_datapath_setup(mcip, flent, SRST_LINK)) != 0)
17449473SVenu.Iyer@Sun.COM 			goto bail;
17459473SVenu.Iyer@Sun.COM 
17469473SVenu.Iyer@Sun.COM 		/*
17479473SVenu.Iyer@Sun.COM 		 * The unicast MAC address must have been added successfully.
17489473SVenu.Iyer@Sun.COM 		 */
17499473SVenu.Iyer@Sun.COM 		ASSERT(mcip->mci_unicast != NULL);
17509473SVenu.Iyer@Sun.COM 		/*
17519473SVenu.Iyer@Sun.COM 		 * Push down the sub-flows that were defined on this link
17529473SVenu.Iyer@Sun.COM 		 * hitherto. The flows are added to the active flow table
17539473SVenu.Iyer@Sun.COM 		 * and SRS, softrings etc. are created as needed.
17549473SVenu.Iyer@Sun.COM 		 */
17559473SVenu.Iyer@Sun.COM 		mac_link_init_flows((mac_client_handle_t)mcip);
17569473SVenu.Iyer@Sun.COM 	} else {
17579473SVenu.Iyer@Sun.COM 		mac_address_t *map = mcip->mci_unicast;
17589473SVenu.Iyer@Sun.COM 
17599473SVenu.Iyer@Sun.COM 		/*
17609473SVenu.Iyer@Sun.COM 		 * A unicast flow already exists for that MAC client,
17619473SVenu.Iyer@Sun.COM 		 * this flow must be the same mac address but with
17629473SVenu.Iyer@Sun.COM 		 * different VID. It has been checked by mac_addr_in_use().
17639473SVenu.Iyer@Sun.COM 		 *
17649473SVenu.Iyer@Sun.COM 		 * We will use the SRS etc. from the mci_flent. Note that
17659473SVenu.Iyer@Sun.COM 		 * We don't need to create kstat for this as except for
17669473SVenu.Iyer@Sun.COM 		 * the fdesc, everything will be used from in the 1st flent.
17679473SVenu.Iyer@Sun.COM 		 */
17689473SVenu.Iyer@Sun.COM 
17699473SVenu.Iyer@Sun.COM 		if (bcmp(mac_addr, map->ma_addr, map->ma_len) != 0) {
17709473SVenu.Iyer@Sun.COM 			err = EINVAL;
17719473SVenu.Iyer@Sun.COM 			goto bail;
17729473SVenu.Iyer@Sun.COM 		}
17739473SVenu.Iyer@Sun.COM 
17749473SVenu.Iyer@Sun.COM 		if ((err = mac_unicast_flow_create(mcip, mac_addr, vid,
17759473SVenu.Iyer@Sun.COM 		    isprimary, B_FALSE, &flent, NULL)) != 0) {
17769473SVenu.Iyer@Sun.COM 			goto bail;
17779473SVenu.Iyer@Sun.COM 		}
17789473SVenu.Iyer@Sun.COM 		if ((err = mac_flow_add(mip->mi_flow_tab, flent)) != 0) {
17799473SVenu.Iyer@Sun.COM 			FLOW_FINAL_REFRELE(flent);
17809473SVenu.Iyer@Sun.COM 			goto bail;
17819473SVenu.Iyer@Sun.COM 		}
17829473SVenu.Iyer@Sun.COM 
17839473SVenu.Iyer@Sun.COM 		/* update the multicast group for this vid */
17849473SVenu.Iyer@Sun.COM 		mac_client_bcast_refresh(mcip, mac_client_update_mcast,
17859473SVenu.Iyer@Sun.COM 		    (void *)flent, B_TRUE);
17869473SVenu.Iyer@Sun.COM 
17879473SVenu.Iyer@Sun.COM 	}
17889473SVenu.Iyer@Sun.COM 
17899473SVenu.Iyer@Sun.COM 	/* populate the shared MAC address */
17909473SVenu.Iyer@Sun.COM 	muip->mui_map = mcip->mci_unicast;
17919473SVenu.Iyer@Sun.COM 
17929473SVenu.Iyer@Sun.COM 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
17939473SVenu.Iyer@Sun.COM 	muip->mui_next = mcip->mci_unicast_list;
17949473SVenu.Iyer@Sun.COM 	mcip->mci_unicast_list = muip;
17959473SVenu.Iyer@Sun.COM 	rw_exit(&mcip->mci_rw_lock);
17969473SVenu.Iyer@Sun.COM 
17979473SVenu.Iyer@Sun.COM 
17989473SVenu.Iyer@Sun.COM 	/*
17999473SVenu.Iyer@Sun.COM 	 * First add the flent to the flow list of this mcip. Then set
18009473SVenu.Iyer@Sun.COM 	 * the mip's mi_single_active_client if needed. The Rx path assumes
18019473SVenu.Iyer@Sun.COM 	 * that mip->mi_single_active_client will always have an associated
18029473SVenu.Iyer@Sun.COM 	 * flent.
18039473SVenu.Iyer@Sun.COM 	 */
18049473SVenu.Iyer@Sun.COM 	mac_client_add_to_flow_list(mcip, flent);
18059473SVenu.Iyer@Sun.COM 
18069473SVenu.Iyer@Sun.COM 	if (nactiveclients_added)
18079473SVenu.Iyer@Sun.COM 		mac_update_single_active_client(mip);
18089473SVenu.Iyer@Sun.COM 	/*
18099473SVenu.Iyer@Sun.COM 	 * Trigger a renegotiation of the capabilities when the number of
18109473SVenu.Iyer@Sun.COM 	 * active clients changes from 1 to 2, since some of the capabilities
18119473SVenu.Iyer@Sun.COM 	 * might have to be disabled. Also send a MAC_NOTE_LINK notification
18129473SVenu.Iyer@Sun.COM 	 * to all the MAC clients whenever physical link is DOWN.
18139473SVenu.Iyer@Sun.COM 	 */
18149473SVenu.Iyer@Sun.COM 	if (mip->mi_nactiveclients == 2) {
18159473SVenu.Iyer@Sun.COM 		mac_capab_update((mac_handle_t)mip);
18169473SVenu.Iyer@Sun.COM 		mac_virtual_link_update(mip);
18179473SVenu.Iyer@Sun.COM 	}
18189473SVenu.Iyer@Sun.COM 	/*
18199473SVenu.Iyer@Sun.COM 	 * Now that the setup is complete, clear the INCIPIENT flag.
18209473SVenu.Iyer@Sun.COM 	 * The flag was set to avoid incoming packets seeing inconsistent
18219473SVenu.Iyer@Sun.COM 	 * structures while the setup was in progress. Clear the mci_tx_flag
18229473SVenu.Iyer@Sun.COM 	 * by calling mac_tx_client_block. It is possible that
18239473SVenu.Iyer@Sun.COM 	 * mac_unicast_remove was called prior to this mac_unicast_add which
18249473SVenu.Iyer@Sun.COM 	 * could have set the MCI_TX_QUIESCE flag.
18259473SVenu.Iyer@Sun.COM 	 */
18269473SVenu.Iyer@Sun.COM 	if (flent->fe_rx_ring_group != NULL)
18279473SVenu.Iyer@Sun.COM 		mac_rx_group_unmark(flent->fe_rx_ring_group, MR_INCIPIENT);
18289473SVenu.Iyer@Sun.COM 	FLOW_UNMARK(flent, FE_INCIPIENT);
18299473SVenu.Iyer@Sun.COM 	FLOW_UNMARK(flent, FE_MC_NO_DATAPATH);
18309473SVenu.Iyer@Sun.COM 	mac_tx_client_unblock(mcip);
18319473SVenu.Iyer@Sun.COM 	return (0);
18329473SVenu.Iyer@Sun.COM bail:
18339473SVenu.Iyer@Sun.COM 	if (bcast_added)
18349473SVenu.Iyer@Sun.COM 		mac_bcast_delete(mcip, mip->mi_type->mt_brdcst_addr, vid);
18359473SVenu.Iyer@Sun.COM 
18369473SVenu.Iyer@Sun.COM 	if (nactiveclients_added)
18379473SVenu.Iyer@Sun.COM 		mip->mi_nactiveclients--;
18389473SVenu.Iyer@Sun.COM 
183910013SNitin.Hande@Sun.COM 	if (mac_started)
184010013SNitin.Hande@Sun.COM 		mac_stop((mac_handle_t)mip);
184110013SNitin.Hande@Sun.COM 
18429473SVenu.Iyer@Sun.COM 	return (err);
18439473SVenu.Iyer@Sun.COM }
18449473SVenu.Iyer@Sun.COM 
18459473SVenu.Iyer@Sun.COM /*
18469473SVenu.Iyer@Sun.COM  * Return the passive primary MAC client, if present. The passive client is
18479473SVenu.Iyer@Sun.COM  * a stand-by client that has the same unicast address as another that is
18489473SVenu.Iyer@Sun.COM  * currenly active. Once the active client goes away, the passive client
18499473SVenu.Iyer@Sun.COM  * becomes active.
18509473SVenu.Iyer@Sun.COM  */
18519473SVenu.Iyer@Sun.COM static mac_client_impl_t *
18529473SVenu.Iyer@Sun.COM mac_get_passive_primary_client(mac_impl_t *mip)
18539473SVenu.Iyer@Sun.COM {
18549473SVenu.Iyer@Sun.COM 	mac_client_impl_t	*mcip;
18559473SVenu.Iyer@Sun.COM 
18569473SVenu.Iyer@Sun.COM 	for (mcip = mip->mi_clients_list; mcip != NULL;
18579473SVenu.Iyer@Sun.COM 	    mcip = mcip->mci_client_next) {
18589473SVenu.Iyer@Sun.COM 		if (mac_is_primary_client(mcip) &&
18599473SVenu.Iyer@Sun.COM 		    (mcip->mci_flags & MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) != 0) {
18609473SVenu.Iyer@Sun.COM 			return (mcip);
18619473SVenu.Iyer@Sun.COM 		}
18629473SVenu.Iyer@Sun.COM 	}
18639473SVenu.Iyer@Sun.COM 	return (NULL);
18649473SVenu.Iyer@Sun.COM }
18659473SVenu.Iyer@Sun.COM 
18669473SVenu.Iyer@Sun.COM /*
18678275SEric Cheng  * Add a new unicast address to the MAC client.
18688275SEric Cheng  *
18698275SEric Cheng  * The MAC address can be specified either by value, or the MAC client
18708275SEric Cheng  * can specify that it wants to use the primary MAC address of the
18718275SEric Cheng  * underlying MAC. See the introductory comments at the beginning
18728275SEric Cheng  * of this file for more more information on primary MAC addresses.
18738275SEric Cheng  *
18748275SEric Cheng  * Note also the tuple (MAC address, VID) must be unique
18758275SEric Cheng  * for the MAC clients defined on top of the same underlying MAC
18768275SEric Cheng  * instance, unless the MAC_UNICAST_NODUPCHECK is specified.
187710491SRishi.Srivatsavai@Sun.COM  *
187810491SRishi.Srivatsavai@Sun.COM  * In no case can a client use the PVID for the MAC, if the MAC has one set.
18798275SEric Cheng  */
18808275SEric Cheng int
18818275SEric Cheng i_mac_unicast_add(mac_client_handle_t mch, uint8_t *mac_addr, uint16_t flags,
18828275SEric Cheng     mac_unicast_handle_t *mah, uint16_t vid, mac_diag_t *diag)
18838275SEric Cheng {
18849473SVenu.Iyer@Sun.COM 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
18859473SVenu.Iyer@Sun.COM 	mac_impl_t		*mip = mcip->mci_mip;
18869473SVenu.Iyer@Sun.COM 	int			err;
18879473SVenu.Iyer@Sun.COM 	uint_t			mac_len = mip->mi_type->mt_addr_length;
18889473SVenu.Iyer@Sun.COM 	boolean_t		check_dups = !(flags & MAC_UNICAST_NODUPCHECK);
18899473SVenu.Iyer@Sun.COM 	boolean_t		fastpath_disabled = B_FALSE;
18909473SVenu.Iyer@Sun.COM 	boolean_t		is_primary = (flags & MAC_UNICAST_PRIMARY);
18919473SVenu.Iyer@Sun.COM 	boolean_t		is_unicast_hw = (flags & MAC_UNICAST_HW);
18929473SVenu.Iyer@Sun.COM 	mac_resource_props_t	mrp;
18939473SVenu.Iyer@Sun.COM 	boolean_t		passive_client = B_FALSE;
18949473SVenu.Iyer@Sun.COM 	mac_unicast_impl_t	*muip;
18959473SVenu.Iyer@Sun.COM 	boolean_t		is_vnic_primary =
18969473SVenu.Iyer@Sun.COM 	    (flags & MAC_UNICAST_VNIC_PRIMARY);
18978275SEric Cheng 
18988275SEric Cheng 	/* when VID is non-zero, the underlying MAC can not be VNIC */
18998275SEric Cheng 	ASSERT(!((mip->mi_state_flags & MIS_IS_VNIC) && (vid != 0)));
19008275SEric Cheng 
19018275SEric Cheng 	/*
190210491SRishi.Srivatsavai@Sun.COM 	 * Check for an attempted use of the current Port VLAN ID, if enabled.
190310491SRishi.Srivatsavai@Sun.COM 	 * No client may use it.
190410491SRishi.Srivatsavai@Sun.COM 	 */
190510491SRishi.Srivatsavai@Sun.COM 	if (mip->mi_pvid != 0 && vid == mip->mi_pvid)
190610491SRishi.Srivatsavai@Sun.COM 		return (EBUSY);
190710491SRishi.Srivatsavai@Sun.COM 
190810491SRishi.Srivatsavai@Sun.COM 	/*
19098275SEric Cheng 	 * Check whether it's the primary client and flag it.
19108275SEric Cheng 	 */
19118275SEric Cheng 	if (!(mcip->mci_state_flags & MCIS_IS_VNIC) && is_primary && vid == 0)
19128275SEric Cheng 		mcip->mci_flags |= MAC_CLIENT_FLAGS_PRIMARY;
19138275SEric Cheng 
19148275SEric Cheng 	/*
19158275SEric Cheng 	 * is_vnic_primary is true when we come here as a VLAN VNIC
19168275SEric Cheng 	 * which uses the primary mac client's address but with a non-zero
19178275SEric Cheng 	 * VID. In this case the MAC address is not specified by an upper
19188275SEric Cheng 	 * MAC client.
19198275SEric Cheng 	 */
19208275SEric Cheng 	if ((mcip->mci_state_flags & MCIS_IS_VNIC) && is_primary &&
19218275SEric Cheng 	    !is_vnic_primary) {
19228275SEric Cheng 		/*
19238275SEric Cheng 		 * The address is being set by the upper MAC client
19248275SEric Cheng 		 * of a VNIC. The MAC address was already set by the
19258275SEric Cheng 		 * VNIC driver during VNIC creation.
19268275SEric Cheng 		 *
19278275SEric Cheng 		 * Note: a VNIC has only one MAC address. We return
19288275SEric Cheng 		 * the MAC unicast address handle of the lower MAC client
19298275SEric Cheng 		 * corresponding to the VNIC. We allocate a new entry
19308275SEric Cheng 		 * which is flagged appropriately, so that mac_unicast_remove()
19318275SEric Cheng 		 * doesn't attempt to free the original entry that
19328275SEric Cheng 		 * was allocated by the VNIC driver.
19338275SEric Cheng 		 */
19348275SEric Cheng 		ASSERT(mcip->mci_unicast != NULL);
19358275SEric Cheng 
19369024SVenu.Iyer@Sun.COM 		/* Check for VLAN flags, if present */
19379024SVenu.Iyer@Sun.COM 		if ((flags & MAC_UNICAST_TAG_DISABLE) != 0)
19389024SVenu.Iyer@Sun.COM 			mcip->mci_state_flags |= MCIS_TAG_DISABLE;
19399024SVenu.Iyer@Sun.COM 
19409024SVenu.Iyer@Sun.COM 		if ((flags & MAC_UNICAST_STRIP_DISABLE) != 0)
19419024SVenu.Iyer@Sun.COM 			mcip->mci_state_flags |= MCIS_STRIP_DISABLE;
19429024SVenu.Iyer@Sun.COM 
19439024SVenu.Iyer@Sun.COM 		if ((flags & MAC_UNICAST_DISABLE_TX_VID_CHECK) != 0)
19449024SVenu.Iyer@Sun.COM 			mcip->mci_state_flags |= MCIS_DISABLE_TX_VID_CHECK;
19459024SVenu.Iyer@Sun.COM 
19468275SEric Cheng 		/*
19478275SEric Cheng 		 * Ensure that the primary unicast address of the VNIC
19489473SVenu.Iyer@Sun.COM 		 * is added only once unless we have the
19499473SVenu.Iyer@Sun.COM 		 * MAC_CLIENT_FLAGS_MULTI_PRIMARY set (and this is not
19509473SVenu.Iyer@Sun.COM 		 * a passive MAC client).
19518275SEric Cheng 		 */
19529473SVenu.Iyer@Sun.COM 		if ((mcip->mci_flags & MAC_CLIENT_FLAGS_VNIC_PRIMARY) != 0) {
19539473SVenu.Iyer@Sun.COM 			if ((mcip->mci_flags &
19549473SVenu.Iyer@Sun.COM 			    MAC_CLIENT_FLAGS_MULTI_PRIMARY) == 0 ||
19559473SVenu.Iyer@Sun.COM 			    (mcip->mci_flags &
19569473SVenu.Iyer@Sun.COM 			    MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) != 0) {
19579473SVenu.Iyer@Sun.COM 				return (EBUSY);
19589473SVenu.Iyer@Sun.COM 			}
19599473SVenu.Iyer@Sun.COM 			mcip->mci_flags |= MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
19609473SVenu.Iyer@Sun.COM 			passive_client = B_TRUE;
19619473SVenu.Iyer@Sun.COM 		}
19628275SEric Cheng 
19638275SEric Cheng 		mcip->mci_flags |= MAC_CLIENT_FLAGS_VNIC_PRIMARY;
19648275SEric Cheng 
19658275SEric Cheng 		/*
19668275SEric Cheng 		 * Create a handle for vid 0.
19678275SEric Cheng 		 */
19688275SEric Cheng 		ASSERT(vid == 0);
19698275SEric Cheng 		muip = kmem_zalloc(sizeof (mac_unicast_impl_t), KM_SLEEP);
19708275SEric Cheng 		muip->mui_vid = vid;
19718275SEric Cheng 		*mah = (mac_unicast_handle_t)muip;
19729473SVenu.Iyer@Sun.COM 		/*
19739473SVenu.Iyer@Sun.COM 		 * This will be used by the caller to defer setting the
19749473SVenu.Iyer@Sun.COM 		 * rx functions.
19759473SVenu.Iyer@Sun.COM 		 */
19769473SVenu.Iyer@Sun.COM 		if (passive_client)
19779473SVenu.Iyer@Sun.COM 			return (EAGAIN);
19788275SEric Cheng 		return (0);
19798275SEric Cheng 	}
19808275SEric Cheng 
19818275SEric Cheng 	/* primary MAC clients cannot be opened on top of anchor VNICs */
19828275SEric Cheng 	if ((is_vnic_primary || is_primary) &&
19838275SEric Cheng 	    i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_ANCHOR_VNIC, NULL)) {
19848275SEric Cheng 		return (ENXIO);
19858275SEric Cheng 	}
19868275SEric Cheng 
19878275SEric Cheng 	/*
19889073SCathy.Zhou@Sun.COM 	 * If this is a VNIC/VLAN, disable softmac fast-path.
19899073SCathy.Zhou@Sun.COM 	 */
19909073SCathy.Zhou@Sun.COM 	if (mcip->mci_state_flags & MCIS_IS_VNIC) {
19919073SCathy.Zhou@Sun.COM 		err = mac_fastpath_disable((mac_handle_t)mip);
19929073SCathy.Zhou@Sun.COM 		if (err != 0)
19939073SCathy.Zhou@Sun.COM 			return (err);
19949073SCathy.Zhou@Sun.COM 		fastpath_disabled = B_TRUE;
19959073SCathy.Zhou@Sun.COM 	}
19969073SCathy.Zhou@Sun.COM 
19979073SCathy.Zhou@Sun.COM 	/*
19988275SEric Cheng 	 * Return EBUSY if:
19999073SCathy.Zhou@Sun.COM 	 *  - there is an exclusively active mac client exists.
20009073SCathy.Zhou@Sun.COM 	 *  - this is an exclusive active mac client but
20019073SCathy.Zhou@Sun.COM 	 *	a. there is already active mac clients exist, or
20029073SCathy.Zhou@Sun.COM 	 *	b. fastpath streams are already plumbed on this legacy device
200310491SRishi.Srivatsavai@Sun.COM 	 *  - the mac creator has disallowed active mac clients.
20048275SEric Cheng 	 */
200510491SRishi.Srivatsavai@Sun.COM 	if (mip->mi_state_flags & (MIS_EXCLUSIVE|MIS_NO_ACTIVE)) {
20069073SCathy.Zhou@Sun.COM 		if (fastpath_disabled)
20079073SCathy.Zhou@Sun.COM 			mac_fastpath_enable((mac_handle_t)mip);
20088275SEric Cheng 		return (EBUSY);
20098275SEric Cheng 	}
20108275SEric Cheng 
20119073SCathy.Zhou@Sun.COM 	if (mcip->mci_state_flags & MCIS_EXCLUSIVE) {
20129073SCathy.Zhou@Sun.COM 		ASSERT(!fastpath_disabled);
20139073SCathy.Zhou@Sun.COM 		if (mip->mi_nactiveclients != 0)
20149073SCathy.Zhou@Sun.COM 			return (EBUSY);
20159073SCathy.Zhou@Sun.COM 
20169073SCathy.Zhou@Sun.COM 		if ((mip->mi_state_flags & MIS_LEGACY) &&
20179073SCathy.Zhou@Sun.COM 		    !(mip->mi_capab_legacy.ml_active_set(mip->mi_driver))) {
20189073SCathy.Zhou@Sun.COM 			return (EBUSY);
20199073SCathy.Zhou@Sun.COM 		}
20208275SEric Cheng 		mip->mi_state_flags |= MIS_EXCLUSIVE;
20219073SCathy.Zhou@Sun.COM 	}
20228275SEric Cheng 
20238275SEric Cheng 	bzero(&mrp, sizeof (mac_resource_props_t));
20248833SVenu.Iyer@Sun.COM 	if (is_primary && !(mcip->mci_state_flags & (MCIS_IS_VNIC |
20258833SVenu.Iyer@Sun.COM 	    MCIS_IS_AGGR_PORT))) {
20268275SEric Cheng 		/*
20278275SEric Cheng 		 * Apply the property cached in the mac_impl_t to the primary
20288833SVenu.Iyer@Sun.COM 		 * mac client. If the mac client is a VNIC or an aggregation
20298833SVenu.Iyer@Sun.COM 		 * port, its property should be set in the mcip when the
20308833SVenu.Iyer@Sun.COM 		 * VNIC/aggr was created.
20318275SEric Cheng 		 */
20328275SEric Cheng 		mac_get_resources((mac_handle_t)mip, &mrp);
20338275SEric Cheng 		(void) mac_client_set_resources(mch, &mrp);
20348275SEric Cheng 	} else if (mcip->mci_state_flags & MCIS_IS_VNIC) {
20358275SEric Cheng 		bcopy(MCIP_RESOURCE_PROPS(mcip), &mrp,
20368275SEric Cheng 		    sizeof (mac_resource_props_t));
20378275SEric Cheng 	}
20388275SEric Cheng 
20398275SEric Cheng 	muip = kmem_zalloc(sizeof (mac_unicast_impl_t), KM_SLEEP);
20408275SEric Cheng 	muip->mui_vid = vid;
20418275SEric Cheng 
20428275SEric Cheng 	if (is_primary || is_vnic_primary) {
20438275SEric Cheng 		mac_addr = mip->mi_addr;
20448275SEric Cheng 	} else {
20458275SEric Cheng 
20468275SEric Cheng 		/*
20478275SEric Cheng 		 * Verify the validity of the specified MAC addresses value.
20488275SEric Cheng 		 */
20498275SEric Cheng 		if (!mac_unicst_verify((mac_handle_t)mip, mac_addr, mac_len)) {
20508275SEric Cheng 			*diag = MAC_DIAG_MACADDR_INVALID;
20518275SEric Cheng 			err = EINVAL;
20529473SVenu.Iyer@Sun.COM 			goto bail_out;
20538275SEric Cheng 		}
20548275SEric Cheng 
20558275SEric Cheng 		/*
20568275SEric Cheng 		 * Make sure that the specified MAC address is different
20578275SEric Cheng 		 * than the unicast MAC address of the underlying NIC.
20588275SEric Cheng 		 */
20598275SEric Cheng 		if (check_dups && bcmp(mip->mi_addr, mac_addr, mac_len) == 0) {
20608275SEric Cheng 			*diag = MAC_DIAG_MACADDR_NIC;
20618275SEric Cheng 			err = EINVAL;
20629473SVenu.Iyer@Sun.COM 			goto bail_out;
20638275SEric Cheng 		}
20648275SEric Cheng 	}
20658275SEric Cheng 
20668275SEric Cheng 	/*
20679473SVenu.Iyer@Sun.COM 	 * Set the flags here so that if this is a passive client, we
20689473SVenu.Iyer@Sun.COM 	 * can return  and set it when we call mac_client_datapath_setup
20699473SVenu.Iyer@Sun.COM 	 * when this becomes the active client. If we defer to using these
20709473SVenu.Iyer@Sun.COM 	 * flags to mac_client_datapath_setup, then for a passive client,
20719473SVenu.Iyer@Sun.COM 	 * we'd have to store the flags somewhere (probably fe_flags)
20729473SVenu.Iyer@Sun.COM 	 * and then use it.
20738275SEric Cheng 	 */
20748275SEric Cheng 	if (!MCIP_DATAPATH_SETUP(mcip)) {
20758400SNicolas.Droux@Sun.COM 		if (is_unicast_hw) {
20768400SNicolas.Droux@Sun.COM 			/*
20778400SNicolas.Droux@Sun.COM 			 * The client requires a hardware MAC address slot
20788400SNicolas.Droux@Sun.COM 			 * for that unicast address. Since we support only
20798400SNicolas.Droux@Sun.COM 			 * one unicast MAC address per client, flag the
20808400SNicolas.Droux@Sun.COM 			 * MAC client itself.
20818400SNicolas.Droux@Sun.COM 			 */
20828400SNicolas.Droux@Sun.COM 			mcip->mci_state_flags |= MCIS_UNICAST_HW;
20838400SNicolas.Droux@Sun.COM 		}
20848275SEric Cheng 
20859024SVenu.Iyer@Sun.COM 		/* Check for VLAN flags, if present */
20869024SVenu.Iyer@Sun.COM 		if ((flags & MAC_UNICAST_TAG_DISABLE) != 0)
20879024SVenu.Iyer@Sun.COM 			mcip->mci_state_flags |= MCIS_TAG_DISABLE;
20889024SVenu.Iyer@Sun.COM 
20899024SVenu.Iyer@Sun.COM 		if ((flags & MAC_UNICAST_STRIP_DISABLE) != 0)
20909024SVenu.Iyer@Sun.COM 			mcip->mci_state_flags |= MCIS_STRIP_DISABLE;
20919024SVenu.Iyer@Sun.COM 
20929024SVenu.Iyer@Sun.COM 		if ((flags & MAC_UNICAST_DISABLE_TX_VID_CHECK) != 0)
20939024SVenu.Iyer@Sun.COM 			mcip->mci_state_flags |= MCIS_DISABLE_TX_VID_CHECK;
20948275SEric Cheng 	} else {
20959024SVenu.Iyer@Sun.COM 		/*
20969024SVenu.Iyer@Sun.COM 		 * Assert that the specified flags are consistent with the
20979024SVenu.Iyer@Sun.COM 		 * flags specified by previous calls to mac_unicast_add().
20989024SVenu.Iyer@Sun.COM 		 */
20999024SVenu.Iyer@Sun.COM 		ASSERT(((flags & MAC_UNICAST_TAG_DISABLE) != 0 &&
21009024SVenu.Iyer@Sun.COM 		    (mcip->mci_state_flags & MCIS_TAG_DISABLE) != 0) ||
21019024SVenu.Iyer@Sun.COM 		    ((flags & MAC_UNICAST_TAG_DISABLE) == 0 &&
21029024SVenu.Iyer@Sun.COM 		    (mcip->mci_state_flags & MCIS_TAG_DISABLE) == 0));
21039024SVenu.Iyer@Sun.COM 
21049024SVenu.Iyer@Sun.COM 		ASSERT(((flags & MAC_UNICAST_STRIP_DISABLE) != 0 &&
21059024SVenu.Iyer@Sun.COM 		    (mcip->mci_state_flags & MCIS_STRIP_DISABLE) != 0) ||
21069024SVenu.Iyer@Sun.COM 		    ((flags & MAC_UNICAST_STRIP_DISABLE) == 0 &&
21079024SVenu.Iyer@Sun.COM 		    (mcip->mci_state_flags & MCIS_STRIP_DISABLE) == 0));
21089024SVenu.Iyer@Sun.COM 
21099024SVenu.Iyer@Sun.COM 		ASSERT(((flags & MAC_UNICAST_DISABLE_TX_VID_CHECK) != 0 &&
21109024SVenu.Iyer@Sun.COM 		    (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK) != 0) ||
21119024SVenu.Iyer@Sun.COM 		    ((flags & MAC_UNICAST_DISABLE_TX_VID_CHECK) == 0 &&
21129024SVenu.Iyer@Sun.COM 		    (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK) == 0));
21139024SVenu.Iyer@Sun.COM 
21148400SNicolas.Droux@Sun.COM 		/*
21158400SNicolas.Droux@Sun.COM 		 * Make sure the client is consistent about its requests
21168400SNicolas.Droux@Sun.COM 		 * for MAC addresses. I.e. all requests from the clients
21178400SNicolas.Droux@Sun.COM 		 * must have the MAC_UNICAST_HW flag set or clear.
21188400SNicolas.Droux@Sun.COM 		 */
21198400SNicolas.Droux@Sun.COM 		if ((mcip->mci_state_flags & MCIS_UNICAST_HW) != 0 &&
21208400SNicolas.Droux@Sun.COM 		    !is_unicast_hw ||
21218400SNicolas.Droux@Sun.COM 		    (mcip->mci_state_flags & MCIS_UNICAST_HW) == 0 &&
21228400SNicolas.Droux@Sun.COM 		    is_unicast_hw) {
21238400SNicolas.Droux@Sun.COM 			err = EINVAL;
21249473SVenu.Iyer@Sun.COM 			goto bail_out;
21258275SEric Cheng 		}
21268275SEric Cheng 	}
21278275SEric Cheng 	/*
21289473SVenu.Iyer@Sun.COM 	 * Make sure the MAC address is not already used by
21299473SVenu.Iyer@Sun.COM 	 * another MAC client defined on top of the same
21309473SVenu.Iyer@Sun.COM 	 * underlying NIC. Unless we have MAC_CLIENT_FLAGS_MULTI_PRIMARY
21319473SVenu.Iyer@Sun.COM 	 * set when we allow a passive client to be present which will
21329473SVenu.Iyer@Sun.COM 	 * be activated when the currently active client goes away - this
21339473SVenu.Iyer@Sun.COM 	 * works only with primary addresses.
21348275SEric Cheng 	 */
21359473SVenu.Iyer@Sun.COM 	if ((check_dups || is_primary || is_vnic_primary) &&
21369473SVenu.Iyer@Sun.COM 	    mac_addr_in_use(mip, mac_addr, vid)) {
21379473SVenu.Iyer@Sun.COM 		/*
21389473SVenu.Iyer@Sun.COM 		 * Must have set the multiple primary address flag when
21399473SVenu.Iyer@Sun.COM 		 * we did a mac_client_open AND this should be a primary
21409473SVenu.Iyer@Sun.COM 		 * MAC client AND there should not already be a passive
21419473SVenu.Iyer@Sun.COM 		 * primary. If all is true then we let this succeed
21429473SVenu.Iyer@Sun.COM 		 * even if the address is a dup.
21439473SVenu.Iyer@Sun.COM 		 */
21449473SVenu.Iyer@Sun.COM 		if ((mcip->mci_flags & MAC_CLIENT_FLAGS_MULTI_PRIMARY) == 0 ||
21459473SVenu.Iyer@Sun.COM 		    (mcip->mci_flags & MAC_CLIENT_FLAGS_PRIMARY) == 0 ||
21469473SVenu.Iyer@Sun.COM 		    mac_get_passive_primary_client(mip) != NULL) {
21479473SVenu.Iyer@Sun.COM 			*diag = MAC_DIAG_MACADDR_INUSE;
21489473SVenu.Iyer@Sun.COM 			err = EEXIST;
21499473SVenu.Iyer@Sun.COM 			goto bail_out;
21509473SVenu.Iyer@Sun.COM 		}
21519473SVenu.Iyer@Sun.COM 		ASSERT((mcip->mci_flags &
21529473SVenu.Iyer@Sun.COM 		    MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) == 0);
21539473SVenu.Iyer@Sun.COM 		mcip->mci_flags |= MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
21549473SVenu.Iyer@Sun.COM 
21559473SVenu.Iyer@Sun.COM 		/*
21569473SVenu.Iyer@Sun.COM 		 * Stash the unicast address handle, we will use it when
21579473SVenu.Iyer@Sun.COM 		 * we set up the passive client.
21589473SVenu.Iyer@Sun.COM 		 */
21599473SVenu.Iyer@Sun.COM 		mcip->mci_p_unicast_list = muip;
21609473SVenu.Iyer@Sun.COM 		*mah = (mac_unicast_handle_t)muip;
21619473SVenu.Iyer@Sun.COM 		return (0);
21629473SVenu.Iyer@Sun.COM 	}
21639473SVenu.Iyer@Sun.COM 
21649473SVenu.Iyer@Sun.COM 	err = mac_client_datapath_setup(mcip, vid, mac_addr, &mrp,
21659473SVenu.Iyer@Sun.COM 	    is_primary || is_vnic_primary, muip);
21669473SVenu.Iyer@Sun.COM 	if (err != 0)
21679473SVenu.Iyer@Sun.COM 		goto bail_out;
21689473SVenu.Iyer@Sun.COM 	*mah = (mac_unicast_handle_t)muip;
21698275SEric Cheng 	return (0);
21709473SVenu.Iyer@Sun.COM 
21719473SVenu.Iyer@Sun.COM bail_out:
21729473SVenu.Iyer@Sun.COM 	if (fastpath_disabled)
21739473SVenu.Iyer@Sun.COM 		mac_fastpath_enable((mac_handle_t)mip);
21749073SCathy.Zhou@Sun.COM 	if (mcip->mci_state_flags & MCIS_EXCLUSIVE) {
21758275SEric Cheng 		mip->mi_state_flags &= ~MIS_EXCLUSIVE;
21769473SVenu.Iyer@Sun.COM 		if (mip->mi_state_flags & MIS_LEGACY) {
21779473SVenu.Iyer@Sun.COM 			mip->mi_capab_legacy.ml_active_clear(
21789473SVenu.Iyer@Sun.COM 			    mip->mi_driver);
21799473SVenu.Iyer@Sun.COM 		}
21809073SCathy.Zhou@Sun.COM 	}
21818275SEric Cheng 	kmem_free(muip, sizeof (mac_unicast_impl_t));
21828275SEric Cheng 	return (err);
21838275SEric Cheng }
21848275SEric Cheng 
21859473SVenu.Iyer@Sun.COM /*
21869473SVenu.Iyer@Sun.COM  * Wrapper function to mac_unicast_add when we want to have the same mac
21879473SVenu.Iyer@Sun.COM  * client open for two instances, one that is currently active and another
21889473SVenu.Iyer@Sun.COM  * that will become active when the current one is removed. In this case
21899473SVenu.Iyer@Sun.COM  * mac_unicast_add will return EGAIN and we will save the rx function and
21909473SVenu.Iyer@Sun.COM  * arg which will be used when we activate the passive client in
21919473SVenu.Iyer@Sun.COM  * mac_unicast_remove.
21929473SVenu.Iyer@Sun.COM  */
21939473SVenu.Iyer@Sun.COM int
21949473SVenu.Iyer@Sun.COM mac_unicast_add_set_rx(mac_client_handle_t mch, uint8_t *mac_addr,
21959473SVenu.Iyer@Sun.COM     uint16_t flags, mac_unicast_handle_t *mah,  uint16_t vid, mac_diag_t *diag,
21969473SVenu.Iyer@Sun.COM     mac_rx_t rx_fn, void *arg)
21979473SVenu.Iyer@Sun.COM {
21989473SVenu.Iyer@Sun.COM 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
21999473SVenu.Iyer@Sun.COM 	uint_t			err;
22009473SVenu.Iyer@Sun.COM 
22019473SVenu.Iyer@Sun.COM 	err = mac_unicast_add(mch, mac_addr, flags, mah, vid, diag);
22029473SVenu.Iyer@Sun.COM 	if (err != 0 && err != EAGAIN)
22039473SVenu.Iyer@Sun.COM 		return (err);
22049473SVenu.Iyer@Sun.COM 	if (err == EAGAIN) {
22059473SVenu.Iyer@Sun.COM 		if (rx_fn != NULL) {
22069473SVenu.Iyer@Sun.COM 			mcip->mci_rx_p_fn = rx_fn;
22079473SVenu.Iyer@Sun.COM 			mcip->mci_rx_p_arg = arg;
22089473SVenu.Iyer@Sun.COM 		}
22099473SVenu.Iyer@Sun.COM 		return (0);
22109473SVenu.Iyer@Sun.COM 	}
22119473SVenu.Iyer@Sun.COM 	if (rx_fn != NULL)
22129473SVenu.Iyer@Sun.COM 		mac_rx_set(mch, rx_fn, arg);
22139473SVenu.Iyer@Sun.COM 	return (err);
22149473SVenu.Iyer@Sun.COM }
22159473SVenu.Iyer@Sun.COM 
22168275SEric Cheng int
22178275SEric Cheng mac_unicast_add(mac_client_handle_t mch, uint8_t *mac_addr, uint16_t flags,
22188275SEric Cheng     mac_unicast_handle_t *mah, uint16_t vid, mac_diag_t *diag)
22198275SEric Cheng {
22208275SEric Cheng 	mac_impl_t *mip = ((mac_client_impl_t *)mch)->mci_mip;
22218275SEric Cheng 	uint_t err;
22228275SEric Cheng 
22238275SEric Cheng 	i_mac_perim_enter(mip);
22248275SEric Cheng 	err = i_mac_unicast_add(mch, mac_addr, flags, mah, vid, diag);
22258275SEric Cheng 	i_mac_perim_exit(mip);
22268275SEric Cheng 
22278275SEric Cheng 	return (err);
22288275SEric Cheng }
22298275SEric Cheng 
22309473SVenu.Iyer@Sun.COM void
22319473SVenu.Iyer@Sun.COM mac_client_datapath_teardown(mac_client_handle_t mch, mac_unicast_impl_t *muip,
22329473SVenu.Iyer@Sun.COM     flow_entry_t *flent)
22338275SEric Cheng {
22349473SVenu.Iyer@Sun.COM 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
22359473SVenu.Iyer@Sun.COM 	mac_impl_t		*mip = mcip->mci_mip;
22368275SEric Cheng 
22378833SVenu.Iyer@Sun.COM 	/*
22388833SVenu.Iyer@Sun.COM 	 * We would have initialized subflows etc. only if we brought up
22398833SVenu.Iyer@Sun.COM 	 * the primary client and set the unicast unicast address etc.
22408833SVenu.Iyer@Sun.COM 	 * Deactivate the flows. The flow entry will be removed from the
22418833SVenu.Iyer@Sun.COM 	 * active flow tables, and the associated SRS, softrings etc will
22428833SVenu.Iyer@Sun.COM 	 * be deleted. But the flow entry itself won't be destroyed, instead
22438833SVenu.Iyer@Sun.COM 	 * it will continue to be archived off the  the global flow hash
22448833SVenu.Iyer@Sun.COM 	 * list, for a possible future activation when say IP is plumbed
22458833SVenu.Iyer@Sun.COM 	 * again.
22468833SVenu.Iyer@Sun.COM 	 */
22478833SVenu.Iyer@Sun.COM 	mac_link_release_flows(mch);
22488833SVenu.Iyer@Sun.COM 
22498275SEric Cheng 	mip->mi_nactiveclients--;
22508833SVenu.Iyer@Sun.COM 	mac_update_single_active_client(mip);
22518275SEric Cheng 
22529473SVenu.Iyer@Sun.COM 	/* Tear down the data path */
22538275SEric Cheng 	mac_datapath_teardown(mcip, mcip->mci_flent, SRST_LINK);
22548275SEric Cheng 
22558275SEric Cheng 	/*
22568275SEric Cheng 	 * Prevent any future access to the flow entry through the mci_flent
22578275SEric Cheng 	 * pointer by setting the mci_flent to NULL. Access to mci_flent in
22588275SEric Cheng 	 * mac_bcast_send is also under mi_rw_lock.
22598275SEric Cheng 	 */
22608275SEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
22618275SEric Cheng 	flent = mcip->mci_flent;
22628275SEric Cheng 	mac_client_remove_flow_from_list(mcip, flent);
22638275SEric Cheng 
22648275SEric Cheng 	if (mcip->mci_state_flags & MCIS_DESC_LOGGED)
22658275SEric Cheng 		mcip->mci_state_flags &= ~MCIS_DESC_LOGGED;
22668275SEric Cheng 
22678275SEric Cheng 	/*
22688275SEric Cheng 	 * This is the last unicast address being removed and there shouldn't
22698275SEric Cheng 	 * be any outbound data threads at this point coming down from mac
22708275SEric Cheng 	 * clients. We have waited for the data threads to finish before
22718275SEric Cheng 	 * starting dld_str_detach. Non-data threads must access TX SRS
22728275SEric Cheng 	 * under mi_rw_lock.
22738275SEric Cheng 	 */
22748275SEric Cheng 	rw_exit(&mip->mi_rw_lock);
22758275SEric Cheng 
22768275SEric Cheng 	/*
22778275SEric Cheng 	 * Don't use FLOW_MARK with FE_MC_NO_DATAPATH, as the flow might
22788275SEric Cheng 	 * contain other flags, such as FE_CONDEMNED, which we need to
22798275SEric Cheng 	 * cleared. We don't call mac_flow_cleanup() for this unicast
22808275SEric Cheng 	 * flow as we have a already cleaned up SRSs etc. (via the teadown
22818275SEric Cheng 	 * path). We just clear the stats and reset the initial callback
22828275SEric Cheng 	 * function, the rest will be set when we call mac_flow_create,
22838275SEric Cheng 	 * if at all.
22848275SEric Cheng 	 */
22858275SEric Cheng 	mutex_enter(&flent->fe_lock);
22868275SEric Cheng 	ASSERT(flent->fe_refcnt == 1 && flent->fe_mbg == NULL &&
22878275SEric Cheng 	    flent->fe_tx_srs == NULL && flent->fe_rx_srs_cnt == 0);
22888275SEric Cheng 	flent->fe_flags = FE_MC_NO_DATAPATH;
22898275SEric Cheng 	flow_stat_destroy(flent);
22908275SEric Cheng 
22918275SEric Cheng 	/* Initialize the receiver function to a safe routine */
22928275SEric Cheng 	flent->fe_cb_fn = (flow_fn_t)mac_pkt_drop;
22938275SEric Cheng 	flent->fe_cb_arg1 = NULL;
22948275SEric Cheng 	flent->fe_cb_arg2 = NULL;
22958275SEric Cheng 
22968275SEric Cheng 	flent->fe_index = -1;
22978275SEric Cheng 	mutex_exit(&flent->fe_lock);
22988275SEric Cheng 
22998275SEric Cheng 	if (mip->mi_type->mt_brdcst_addr != NULL) {
23008275SEric Cheng 		mac_bcast_delete(mcip, mip->mi_type->mt_brdcst_addr,
23018275SEric Cheng 		    muip->mui_vid);
23028275SEric Cheng 	}
23038275SEric Cheng 
23048275SEric Cheng 	if (mip->mi_nactiveclients == 1) {
23058275SEric Cheng 		mac_capab_update((mac_handle_t)mip);
23068275SEric Cheng 		mac_virtual_link_update(mip);
23078275SEric Cheng 	}
23089073SCathy.Zhou@Sun.COM 
23099073SCathy.Zhou@Sun.COM 	if (mcip->mci_state_flags & MCIS_EXCLUSIVE) {
23108275SEric Cheng 		mip->mi_state_flags &= ~MIS_EXCLUSIVE;
23119073SCathy.Zhou@Sun.COM 
23129073SCathy.Zhou@Sun.COM 		if (mip->mi_state_flags & MIS_LEGACY)
23139073SCathy.Zhou@Sun.COM 			mip->mi_capab_legacy.ml_active_clear(mip->mi_driver);
23149073SCathy.Zhou@Sun.COM 	}
23159073SCathy.Zhou@Sun.COM 
23168400SNicolas.Droux@Sun.COM 	mcip->mci_state_flags &= ~MCIS_UNICAST_HW;
23178275SEric Cheng 
23189024SVenu.Iyer@Sun.COM 	if (mcip->mci_state_flags & MCIS_TAG_DISABLE)
23199024SVenu.Iyer@Sun.COM 		mcip->mci_state_flags &= ~MCIS_TAG_DISABLE;
23209024SVenu.Iyer@Sun.COM 
23219024SVenu.Iyer@Sun.COM 	if (mcip->mci_state_flags & MCIS_STRIP_DISABLE)
23229024SVenu.Iyer@Sun.COM 		mcip->mci_state_flags &= ~MCIS_STRIP_DISABLE;
23239024SVenu.Iyer@Sun.COM 
23249024SVenu.Iyer@Sun.COM 	if (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK)
23259024SVenu.Iyer@Sun.COM 		mcip->mci_state_flags &= ~MCIS_DISABLE_TX_VID_CHECK;
23269024SVenu.Iyer@Sun.COM 
23279073SCathy.Zhou@Sun.COM 	kmem_free(muip, sizeof (mac_unicast_impl_t));
23289073SCathy.Zhou@Sun.COM 
23299073SCathy.Zhou@Sun.COM 	/*
23309073SCathy.Zhou@Sun.COM 	 * Disable fastpath if this is a VNIC or a VLAN.
23319073SCathy.Zhou@Sun.COM 	 */
23329073SCathy.Zhou@Sun.COM 	if (mcip->mci_state_flags & MCIS_IS_VNIC)
23339073SCathy.Zhou@Sun.COM 		mac_fastpath_enable((mac_handle_t)mip);
23348893SMichael.Lim@Sun.COM 	mac_stop((mac_handle_t)mip);
23359473SVenu.Iyer@Sun.COM }
23369473SVenu.Iyer@Sun.COM 
23379473SVenu.Iyer@Sun.COM /*
23389473SVenu.Iyer@Sun.COM  * Remove a MAC address which was previously added by mac_unicast_add().
23399473SVenu.Iyer@Sun.COM  */
23409473SVenu.Iyer@Sun.COM int
23419473SVenu.Iyer@Sun.COM mac_unicast_remove(mac_client_handle_t mch, mac_unicast_handle_t mah)
23429473SVenu.Iyer@Sun.COM {
23439473SVenu.Iyer@Sun.COM 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
23449473SVenu.Iyer@Sun.COM 	mac_unicast_impl_t *muip = (mac_unicast_impl_t *)mah;
23459473SVenu.Iyer@Sun.COM 	mac_unicast_impl_t *pre;
23469473SVenu.Iyer@Sun.COM 	mac_impl_t *mip = mcip->mci_mip;
23479473SVenu.Iyer@Sun.COM 	flow_entry_t		*flent;
23489473SVenu.Iyer@Sun.COM 	boolean_t		isprimary = B_FALSE;
23499473SVenu.Iyer@Sun.COM 
23509473SVenu.Iyer@Sun.COM 	i_mac_perim_enter(mip);
23519473SVenu.Iyer@Sun.COM 	if (mcip->mci_flags & MAC_CLIENT_FLAGS_VNIC_PRIMARY) {
23529473SVenu.Iyer@Sun.COM 		/*
23539473SVenu.Iyer@Sun.COM 		 * Called made by the upper MAC client of a VNIC.
23549473SVenu.Iyer@Sun.COM 		 * There's nothing much to do, the unicast address will
23559473SVenu.Iyer@Sun.COM 		 * be removed by the VNIC driver when the VNIC is deleted,
23569473SVenu.Iyer@Sun.COM 		 * but let's ensure that all our transmit is done before
23579473SVenu.Iyer@Sun.COM 		 * the client does a mac_client_stop lest it trigger an
23589473SVenu.Iyer@Sun.COM 		 * assert in the driver.
23599473SVenu.Iyer@Sun.COM 		 */
23609473SVenu.Iyer@Sun.COM 		ASSERT(muip->mui_vid == 0);
23619473SVenu.Iyer@Sun.COM 
23629473SVenu.Iyer@Sun.COM 		mac_tx_client_flush(mcip);
23639473SVenu.Iyer@Sun.COM 
23649473SVenu.Iyer@Sun.COM 		if ((mcip->mci_flags & MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) != 0) {
23659473SVenu.Iyer@Sun.COM 			mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
23669473SVenu.Iyer@Sun.COM 			if (mcip->mci_rx_p_fn != NULL) {
23679473SVenu.Iyer@Sun.COM 				mac_rx_set(mch, mcip->mci_rx_p_fn,
23689473SVenu.Iyer@Sun.COM 				    mcip->mci_rx_p_arg);
23699473SVenu.Iyer@Sun.COM 				mcip->mci_rx_p_fn = NULL;
23709473SVenu.Iyer@Sun.COM 				mcip->mci_rx_p_arg = NULL;
23719473SVenu.Iyer@Sun.COM 			}
23729473SVenu.Iyer@Sun.COM 			kmem_free(muip, sizeof (mac_unicast_impl_t));
23739473SVenu.Iyer@Sun.COM 			i_mac_perim_exit(mip);
23749473SVenu.Iyer@Sun.COM 			return (0);
23759473SVenu.Iyer@Sun.COM 		}
23769473SVenu.Iyer@Sun.COM 		mcip->mci_flags &= ~MAC_CLIENT_FLAGS_VNIC_PRIMARY;
23779473SVenu.Iyer@Sun.COM 
23789473SVenu.Iyer@Sun.COM 		if (mcip->mci_state_flags & MCIS_TAG_DISABLE)
23799473SVenu.Iyer@Sun.COM 			mcip->mci_state_flags &= ~MCIS_TAG_DISABLE;
23809473SVenu.Iyer@Sun.COM 
23819473SVenu.Iyer@Sun.COM 		if (mcip->mci_state_flags & MCIS_STRIP_DISABLE)
23829473SVenu.Iyer@Sun.COM 			mcip->mci_state_flags &= ~MCIS_STRIP_DISABLE;
23839473SVenu.Iyer@Sun.COM 
23849473SVenu.Iyer@Sun.COM 		if (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK)
23859473SVenu.Iyer@Sun.COM 			mcip->mci_state_flags &= ~MCIS_DISABLE_TX_VID_CHECK;
23869473SVenu.Iyer@Sun.COM 
23879473SVenu.Iyer@Sun.COM 		kmem_free(muip, sizeof (mac_unicast_impl_t));
23889473SVenu.Iyer@Sun.COM 		i_mac_perim_exit(mip);
23899473SVenu.Iyer@Sun.COM 		return (0);
23909473SVenu.Iyer@Sun.COM 	}
23919473SVenu.Iyer@Sun.COM 
23929473SVenu.Iyer@Sun.COM 	ASSERT(muip != NULL);
23939473SVenu.Iyer@Sun.COM 
23949473SVenu.Iyer@Sun.COM 	/*
23959473SVenu.Iyer@Sun.COM 	 * We are removing a passive client, we haven't setup the datapath
23969473SVenu.Iyer@Sun.COM 	 * for this yet, so nothing much to do.
23979473SVenu.Iyer@Sun.COM 	 */
23989614SVenu.Iyer@Sun.COM 	if ((mcip->mci_flags & MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) != 0) {
23999473SVenu.Iyer@Sun.COM 
24009473SVenu.Iyer@Sun.COM 		ASSERT((mcip->mci_flent->fe_flags & FE_MC_NO_DATAPATH) != 0);
24019473SVenu.Iyer@Sun.COM 		ASSERT(mcip->mci_p_unicast_list == muip);
24029473SVenu.Iyer@Sun.COM 
24039614SVenu.Iyer@Sun.COM 		mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
24049614SVenu.Iyer@Sun.COM 
24059473SVenu.Iyer@Sun.COM 		mcip->mci_p_unicast_list = NULL;
24069473SVenu.Iyer@Sun.COM 		mcip->mci_rx_p_fn = NULL;
24079473SVenu.Iyer@Sun.COM 		mcip->mci_rx_p_arg = NULL;
24089473SVenu.Iyer@Sun.COM 
24099473SVenu.Iyer@Sun.COM 		mcip->mci_state_flags &= ~MCIS_UNICAST_HW;
24109473SVenu.Iyer@Sun.COM 
24119473SVenu.Iyer@Sun.COM 		if (mcip->mci_state_flags & MCIS_TAG_DISABLE)
24129473SVenu.Iyer@Sun.COM 			mcip->mci_state_flags &= ~MCIS_TAG_DISABLE;
24139473SVenu.Iyer@Sun.COM 
24149473SVenu.Iyer@Sun.COM 		if (mcip->mci_state_flags & MCIS_STRIP_DISABLE)
24159473SVenu.Iyer@Sun.COM 			mcip->mci_state_flags &= ~MCIS_STRIP_DISABLE;
24169473SVenu.Iyer@Sun.COM 
24179473SVenu.Iyer@Sun.COM 		if (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK)
24189473SVenu.Iyer@Sun.COM 			mcip->mci_state_flags &= ~MCIS_DISABLE_TX_VID_CHECK;
24199473SVenu.Iyer@Sun.COM 
24209473SVenu.Iyer@Sun.COM 		kmem_free(muip, sizeof (mac_unicast_impl_t));
24219473SVenu.Iyer@Sun.COM 		i_mac_perim_exit(mip);
24229473SVenu.Iyer@Sun.COM 		return (0);
24239473SVenu.Iyer@Sun.COM 	}
24249473SVenu.Iyer@Sun.COM 	/*
24259473SVenu.Iyer@Sun.COM 	 * Remove the VID from the list of client's VIDs.
24269473SVenu.Iyer@Sun.COM 	 */
24279473SVenu.Iyer@Sun.COM 	pre = mcip->mci_unicast_list;
24289473SVenu.Iyer@Sun.COM 	if (muip == pre) {
24299473SVenu.Iyer@Sun.COM 		mcip->mci_unicast_list = muip->mui_next;
24309473SVenu.Iyer@Sun.COM 	} else {
24319473SVenu.Iyer@Sun.COM 		while ((pre->mui_next != NULL) && (pre->mui_next != muip))
24329473SVenu.Iyer@Sun.COM 			pre = pre->mui_next;
24339473SVenu.Iyer@Sun.COM 		ASSERT(pre->mui_next == muip);
24349473SVenu.Iyer@Sun.COM 		rw_enter(&mcip->mci_rw_lock, RW_WRITER);
24359473SVenu.Iyer@Sun.COM 		pre->mui_next = muip->mui_next;
24369473SVenu.Iyer@Sun.COM 		rw_exit(&mcip->mci_rw_lock);
24379473SVenu.Iyer@Sun.COM 	}
24389473SVenu.Iyer@Sun.COM 
24399473SVenu.Iyer@Sun.COM 	if ((mcip->mci_flags & MAC_CLIENT_FLAGS_PRIMARY) &&
24409473SVenu.Iyer@Sun.COM 	    muip->mui_vid == 0) {
24419473SVenu.Iyer@Sun.COM 		mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PRIMARY;
24429473SVenu.Iyer@Sun.COM 		isprimary = B_TRUE;
24439473SVenu.Iyer@Sun.COM 	}
24449473SVenu.Iyer@Sun.COM 	if (!mac_client_single_rcvr(mcip)) {
24459473SVenu.Iyer@Sun.COM 		/*
24469473SVenu.Iyer@Sun.COM 		 * This MAC client is shared by more than one unicast
24479473SVenu.Iyer@Sun.COM 		 * addresses, so we will just remove the flent
24489473SVenu.Iyer@Sun.COM 		 * corresponding to the address being removed. We don't invoke
24499473SVenu.Iyer@Sun.COM 		 * mac_rx_classify_flow_rem() since the additional flow is
24509473SVenu.Iyer@Sun.COM 		 * not associated with its own separate set of SRS and rings,
24519473SVenu.Iyer@Sun.COM 		 * and these constructs are still needed for the remaining
24529473SVenu.Iyer@Sun.COM 		 * flows.
24539473SVenu.Iyer@Sun.COM 		 */
24549473SVenu.Iyer@Sun.COM 		flent = mac_client_get_flow(mcip, muip);
24559473SVenu.Iyer@Sun.COM 		ASSERT(flent != NULL);
24569473SVenu.Iyer@Sun.COM 
24579473SVenu.Iyer@Sun.COM 		/*
24589473SVenu.Iyer@Sun.COM 		 * The first one is disappearing, need to make sure
24599473SVenu.Iyer@Sun.COM 		 * we replace it with another from the list of
24609473SVenu.Iyer@Sun.COM 		 * shared clients.
24619473SVenu.Iyer@Sun.COM 		 */
24629473SVenu.Iyer@Sun.COM 		if (flent == mcip->mci_flent)
24639473SVenu.Iyer@Sun.COM 			flent = mac_client_swap_mciflent(mcip);
24649473SVenu.Iyer@Sun.COM 		mac_client_remove_flow_from_list(mcip, flent);
24659473SVenu.Iyer@Sun.COM 		mac_flow_remove(mip->mi_flow_tab, flent, B_FALSE);
24669473SVenu.Iyer@Sun.COM 		mac_flow_wait(flent, FLOW_DRIVER_UPCALL);
24679473SVenu.Iyer@Sun.COM 
24689473SVenu.Iyer@Sun.COM 		/*
24699473SVenu.Iyer@Sun.COM 		 * The multicast groups that were added by the client so
24709473SVenu.Iyer@Sun.COM 		 * far must be removed from the brodcast domain corresponding
24719473SVenu.Iyer@Sun.COM 		 * to the VID being removed.
24729473SVenu.Iyer@Sun.COM 		 */
24739473SVenu.Iyer@Sun.COM 		mac_client_bcast_refresh(mcip, mac_client_update_mcast,
24749473SVenu.Iyer@Sun.COM 		    (void *)flent, B_FALSE);
24759473SVenu.Iyer@Sun.COM 
24769473SVenu.Iyer@Sun.COM 		if (mip->mi_type->mt_brdcst_addr != NULL) {
24779473SVenu.Iyer@Sun.COM 			mac_bcast_delete(mcip, mip->mi_type->mt_brdcst_addr,
24789473SVenu.Iyer@Sun.COM 			    muip->mui_vid);
24799473SVenu.Iyer@Sun.COM 		}
24809473SVenu.Iyer@Sun.COM 
24819473SVenu.Iyer@Sun.COM 		FLOW_FINAL_REFRELE(flent);
24829473SVenu.Iyer@Sun.COM 		ASSERT(!(mcip->mci_state_flags & MCIS_EXCLUSIVE));
24839473SVenu.Iyer@Sun.COM 		/*
24849473SVenu.Iyer@Sun.COM 		 * Enable fastpath if this is a VNIC or a VLAN.
24859473SVenu.Iyer@Sun.COM 		 */
24869473SVenu.Iyer@Sun.COM 		if (mcip->mci_state_flags & MCIS_IS_VNIC)
24879473SVenu.Iyer@Sun.COM 			mac_fastpath_enable((mac_handle_t)mip);
24889473SVenu.Iyer@Sun.COM 		mac_stop((mac_handle_t)mip);
24899473SVenu.Iyer@Sun.COM 		i_mac_perim_exit(mip);
24909473SVenu.Iyer@Sun.COM 		return (0);
24919473SVenu.Iyer@Sun.COM 	}
24929473SVenu.Iyer@Sun.COM 
24939473SVenu.Iyer@Sun.COM 	mac_client_datapath_teardown(mch, muip, flent);
24949473SVenu.Iyer@Sun.COM 
24959473SVenu.Iyer@Sun.COM 	/*
24969473SVenu.Iyer@Sun.COM 	 * If we are removing the primary, check if we have a passive primary
24979473SVenu.Iyer@Sun.COM 	 * client that we need to activate now.
24989473SVenu.Iyer@Sun.COM 	 */
24999473SVenu.Iyer@Sun.COM 	if (!isprimary) {
25009473SVenu.Iyer@Sun.COM 		i_mac_perim_exit(mip);
25019473SVenu.Iyer@Sun.COM 		return (0);
25029473SVenu.Iyer@Sun.COM 	}
25039473SVenu.Iyer@Sun.COM 	mcip = mac_get_passive_primary_client(mip);
25049473SVenu.Iyer@Sun.COM 	if (mcip != NULL) {
25059473SVenu.Iyer@Sun.COM 		mac_resource_props_t	mrp;
25069473SVenu.Iyer@Sun.COM 		mac_unicast_impl_t	*muip;
25079473SVenu.Iyer@Sun.COM 
25089473SVenu.Iyer@Sun.COM 		mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
25099473SVenu.Iyer@Sun.COM 		bzero(&mrp, sizeof (mac_resource_props_t));
25109473SVenu.Iyer@Sun.COM 		/*
25119473SVenu.Iyer@Sun.COM 		 * Apply the property cached in the mac_impl_t to the
25129473SVenu.Iyer@Sun.COM 		 * primary mac client.
25139473SVenu.Iyer@Sun.COM 		 */
25149473SVenu.Iyer@Sun.COM 		mac_get_resources((mac_handle_t)mip, &mrp);
25159473SVenu.Iyer@Sun.COM 		(void) mac_client_set_resources(mch, &mrp);
25169473SVenu.Iyer@Sun.COM 		ASSERT(mcip->mci_p_unicast_list != NULL);
25179473SVenu.Iyer@Sun.COM 		muip = mcip->mci_p_unicast_list;
25189473SVenu.Iyer@Sun.COM 		mcip->mci_p_unicast_list = NULL;
25199473SVenu.Iyer@Sun.COM 		if (mac_client_datapath_setup(mcip, VLAN_ID_NONE,
25209473SVenu.Iyer@Sun.COM 		    mip->mi_addr, &mrp, B_TRUE, muip) == 0) {
25219473SVenu.Iyer@Sun.COM 			if (mcip->mci_rx_p_fn != NULL) {
25229473SVenu.Iyer@Sun.COM 				mac_rx_set(mch, mcip->mci_rx_p_fn,
25239473SVenu.Iyer@Sun.COM 				    mcip->mci_rx_p_arg);
25249473SVenu.Iyer@Sun.COM 				mcip->mci_rx_p_fn = NULL;
25259473SVenu.Iyer@Sun.COM 				mcip->mci_rx_p_arg = NULL;
25269473SVenu.Iyer@Sun.COM 			}
25279822SVenu.Iyer@Sun.COM 		} else {
25289822SVenu.Iyer@Sun.COM 			kmem_free(muip, sizeof (mac_unicast_impl_t));
25299473SVenu.Iyer@Sun.COM 		}
25309473SVenu.Iyer@Sun.COM 	}
25318275SEric Cheng 	i_mac_perim_exit(mip);
25328275SEric Cheng 	return (0);
25338275SEric Cheng }
25348275SEric Cheng 
25358275SEric Cheng /*
25368275SEric Cheng  * Multicast add function invoked by MAC clients.
25378275SEric Cheng  */
25388275SEric Cheng int
25398275SEric Cheng mac_multicast_add(mac_client_handle_t mch, const uint8_t *addr)
25408275SEric Cheng {
25418275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
25428275SEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
25438275SEric Cheng 	flow_entry_t		*flent = mcip->mci_flent_list;
25448275SEric Cheng 	flow_entry_t		*prev_fe = NULL;
25458275SEric Cheng 	uint16_t		vid;
25468275SEric Cheng 	int			err = 0;
25478275SEric Cheng 
25488275SEric Cheng 	/* Verify the address is a valid multicast address */
25498275SEric Cheng 	if ((err = mip->mi_type->mt_ops.mtops_multicst_verify(addr,
25508275SEric Cheng 	    mip->mi_pdata)) != 0)
25518275SEric Cheng 		return (err);
25528275SEric Cheng 
25538275SEric Cheng 	i_mac_perim_enter(mip);
25548275SEric Cheng 	while (flent != NULL) {
25558275SEric Cheng 		vid = i_mac_flow_vid(flent);
25568275SEric Cheng 
25578275SEric Cheng 		err = mac_bcast_add((mac_client_impl_t *)mch, addr, vid,
25588275SEric Cheng 		    MAC_ADDRTYPE_MULTICAST);
25598275SEric Cheng 		if (err != 0)
25608275SEric Cheng 			break;
25618275SEric Cheng 		prev_fe = flent;
25628275SEric Cheng 		flent = flent->fe_client_next;
25638275SEric Cheng 	}
25648275SEric Cheng 
25658275SEric Cheng 	/*
25668275SEric Cheng 	 * If we failed adding, then undo all, rather than partial
25678275SEric Cheng 	 * success.
25688275SEric Cheng 	 */
25698275SEric Cheng 	if (flent != NULL && prev_fe != NULL) {
25708275SEric Cheng 		flent = mcip->mci_flent_list;
25718275SEric Cheng 		while (flent != prev_fe->fe_client_next) {
25728275SEric Cheng 			vid = i_mac_flow_vid(flent);
25738275SEric Cheng 			mac_bcast_delete((mac_client_impl_t *)mch, addr, vid);
25748275SEric Cheng 			flent = flent->fe_client_next;
25758275SEric Cheng 		}
25768275SEric Cheng 	}
25778275SEric Cheng 	i_mac_perim_exit(mip);
25788275SEric Cheng 	return (err);
25798275SEric Cheng }
25808275SEric Cheng 
25818275SEric Cheng /*
25828275SEric Cheng  * Multicast delete function invoked by MAC clients.
25838275SEric Cheng  */
25848275SEric Cheng void
25858275SEric Cheng mac_multicast_remove(mac_client_handle_t mch, const uint8_t *addr)
25868275SEric Cheng {
25878275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
25888275SEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
25898275SEric Cheng 	flow_entry_t		*flent;
25908275SEric Cheng 	uint16_t		vid;
25918275SEric Cheng 
25928275SEric Cheng 	i_mac_perim_enter(mip);
25938275SEric Cheng 	for (flent = mcip->mci_flent_list; flent != NULL;
25948275SEric Cheng 	    flent = flent->fe_client_next) {
25958275SEric Cheng 		vid = i_mac_flow_vid(flent);
25968275SEric Cheng 		mac_bcast_delete((mac_client_impl_t *)mch, addr, vid);
25978275SEric Cheng 	}
25988275SEric Cheng 	i_mac_perim_exit(mip);
25998275SEric Cheng }
26008275SEric Cheng 
26018275SEric Cheng /*
26028275SEric Cheng  * When a MAC client desires to capture packets on an interface,
26038275SEric Cheng  * it registers a promiscuous call back with mac_promisc_add().
26048275SEric Cheng  * There are three types of promiscuous callbacks:
26058275SEric Cheng  *
26068275SEric Cheng  * * MAC_CLIENT_PROMISC_ALL
26078275SEric Cheng  *   Captures all packets sent and received by the MAC client,
26088275SEric Cheng  *   the physical interface, as well as all other MAC clients
26098275SEric Cheng  *   defined on top of the same MAC.
26108275SEric Cheng  *
26118275SEric Cheng  * * MAC_CLIENT_PROMISC_FILTERED
26128275SEric Cheng  *   Captures all packets sent and received by the MAC client,
26138275SEric Cheng  *   plus all multicast traffic sent and received by the phyisical
26148275SEric Cheng  *   interface and the other MAC clients.
26158275SEric Cheng  *
26168275SEric Cheng  * * MAC_CLIENT_PROMISC_MULTI
26178275SEric Cheng  *   Captures all broadcast and multicast packets sent and
26188275SEric Cheng  *   received by the MAC clients as well as the physical interface.
26198275SEric Cheng  *
26208275SEric Cheng  * In all cases, the underlying MAC is put in promiscuous mode.
26218275SEric Cheng  */
26228275SEric Cheng int
26238275SEric Cheng mac_promisc_add(mac_client_handle_t mch, mac_client_promisc_type_t type,
26248275SEric Cheng     mac_rx_t fn, void *arg, mac_promisc_handle_t *mphp, uint16_t flags)
26258275SEric Cheng {
26268275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
26278275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
26288275SEric Cheng 	mac_promisc_impl_t *mpip;
26298275SEric Cheng 	mac_cb_info_t	*mcbi;
26308275SEric Cheng 	int rc;
26318275SEric Cheng 
26328275SEric Cheng 	i_mac_perim_enter(mip);
26338275SEric Cheng 
26348893SMichael.Lim@Sun.COM 	if ((rc = mac_start((mac_handle_t)mip)) != 0) {
26358275SEric Cheng 		i_mac_perim_exit(mip);
26368275SEric Cheng 		return (rc);
26378275SEric Cheng 	}
26388275SEric Cheng 
26398275SEric Cheng 	if ((mcip->mci_state_flags & MCIS_IS_VNIC) &&
26408275SEric Cheng 	    type == MAC_CLIENT_PROMISC_ALL) {
26418275SEric Cheng 		/*
26428275SEric Cheng 		 * The function is being invoked by the upper MAC client
26438275SEric Cheng 		 * of a VNIC. The VNIC should only see the traffic
26448275SEric Cheng 		 * it is entitled to.
26458275SEric Cheng 		 */
26468275SEric Cheng 		type = MAC_CLIENT_PROMISC_FILTERED;
26478275SEric Cheng 	}
26488275SEric Cheng 
26498275SEric Cheng 
26508275SEric Cheng 	/*
26518275SEric Cheng 	 * Turn on promiscuous mode for the underlying NIC.
26528275SEric Cheng 	 * This is needed even for filtered callbacks which
26538275SEric Cheng 	 * expect to receive all multicast traffic on the wire.
26548275SEric Cheng 	 *
26558275SEric Cheng 	 * Physical promiscuous mode should not be turned on if
26568275SEric Cheng 	 * MAC_PROMISC_FLAGS_NO_PHYS is set.
26578275SEric Cheng 	 */
26588275SEric Cheng 	if ((flags & MAC_PROMISC_FLAGS_NO_PHYS) == 0) {
26599641SGirish.Moodalbail@Sun.COM 		if ((rc = i_mac_promisc_set(mip, B_TRUE)) != 0) {
26608893SMichael.Lim@Sun.COM 			mac_stop((mac_handle_t)mip);
26618275SEric Cheng 			i_mac_perim_exit(mip);
26628275SEric Cheng 			return (rc);
26638275SEric Cheng 		}
26648275SEric Cheng 	}
26658275SEric Cheng 
26668275SEric Cheng 	mpip = kmem_cache_alloc(mac_promisc_impl_cache, KM_SLEEP);
26678275SEric Cheng 
26688275SEric Cheng 	mpip->mpi_type = type;
26698275SEric Cheng 	mpip->mpi_fn = fn;
26708275SEric Cheng 	mpip->mpi_arg = arg;
26718275SEric Cheng 	mpip->mpi_mcip = mcip;
26728275SEric Cheng 	mpip->mpi_no_tx_loop = ((flags & MAC_PROMISC_FLAGS_NO_TX_LOOP) != 0);
26738275SEric Cheng 	mpip->mpi_no_phys = ((flags & MAC_PROMISC_FLAGS_NO_PHYS) != 0);
26748833SVenu.Iyer@Sun.COM 	mpip->mpi_strip_vlan_tag =
26758833SVenu.Iyer@Sun.COM 	    ((flags & MAC_PROMISC_FLAGS_VLAN_TAG_STRIP) != 0);
267610639SDarren.Reed@Sun.COM 	mpip->mpi_no_copy = ((flags & MAC_PROMISC_FLAGS_NO_COPY) != 0);
26778275SEric Cheng 
26788275SEric Cheng 	mcbi = &mip->mi_promisc_cb_info;
26798275SEric Cheng 	mutex_enter(mcbi->mcbi_lockp);
26808275SEric Cheng 
26818275SEric Cheng 	mac_callback_add(&mip->mi_promisc_cb_info, &mcip->mci_promisc_list,
26828275SEric Cheng 	    &mpip->mpi_mci_link);
26838275SEric Cheng 	mac_callback_add(&mip->mi_promisc_cb_info, &mip->mi_promisc_list,
26848275SEric Cheng 	    &mpip->mpi_mi_link);
26858275SEric Cheng 
26868275SEric Cheng 	mutex_exit(mcbi->mcbi_lockp);
26878275SEric Cheng 
26888275SEric Cheng 	*mphp = (mac_promisc_handle_t)mpip;
26898275SEric Cheng 	i_mac_perim_exit(mip);
26908275SEric Cheng 	return (0);
26918275SEric Cheng }
26928275SEric Cheng 
26938275SEric Cheng /*
26948275SEric Cheng  * Remove a multicast address previously aded through mac_promisc_add().
26958275SEric Cheng  */
26969044SGirish.Moodalbail@Sun.COM void
26978275SEric Cheng mac_promisc_remove(mac_promisc_handle_t mph)
26988275SEric Cheng {
26998275SEric Cheng 	mac_promisc_impl_t *mpip = (mac_promisc_impl_t *)mph;
27008275SEric Cheng 	mac_client_impl_t *mcip = mpip->mpi_mcip;
27018275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
27028275SEric Cheng 	mac_cb_info_t *mcbi;
27039641SGirish.Moodalbail@Sun.COM 	int rv;
27048275SEric Cheng 
27058275SEric Cheng 	i_mac_perim_enter(mip);
27068275SEric Cheng 
27078275SEric Cheng 	/*
27088275SEric Cheng 	 * Even if the device can't be reset into normal mode, we still
27098275SEric Cheng 	 * need to clear the client promisc callbacks. The client may want
27108275SEric Cheng 	 * to close the mac end point and we can't have stale callbacks.
27118275SEric Cheng 	 */
27128275SEric Cheng 	if (!(mpip->mpi_no_phys)) {
27139641SGirish.Moodalbail@Sun.COM 		if ((rv = i_mac_promisc_set(mip, B_FALSE)) != 0) {
27149641SGirish.Moodalbail@Sun.COM 			cmn_err(CE_WARN, "%s: failed to switch OFF promiscuous"
27159641SGirish.Moodalbail@Sun.COM 			    " mode because of error 0x%x", mip->mi_name, rv);
27169641SGirish.Moodalbail@Sun.COM 		}
27178275SEric Cheng 	}
27188275SEric Cheng 	mcbi = &mip->mi_promisc_cb_info;
27198275SEric Cheng 	mutex_enter(mcbi->mcbi_lockp);
27208275SEric Cheng 	if (mac_callback_remove(mcbi, &mip->mi_promisc_list,
27218275SEric Cheng 	    &mpip->mpi_mi_link)) {
27228275SEric Cheng 		VERIFY(mac_callback_remove(&mip->mi_promisc_cb_info,
27238275SEric Cheng 		    &mcip->mci_promisc_list, &mpip->mpi_mci_link));
27248275SEric Cheng 		kmem_cache_free(mac_promisc_impl_cache, mpip);
27258275SEric Cheng 	} else {
27268275SEric Cheng 		mac_callback_remove_wait(&mip->mi_promisc_cb_info);
27278275SEric Cheng 	}
27288275SEric Cheng 	mutex_exit(mcbi->mcbi_lockp);
27298893SMichael.Lim@Sun.COM 	mac_stop((mac_handle_t)mip);
27308275SEric Cheng 
27318275SEric Cheng 	i_mac_perim_exit(mip);
27328275SEric Cheng }
27338275SEric Cheng 
27348275SEric Cheng /*
27358275SEric Cheng  * Reference count the number of active Tx threads. MCI_TX_QUIESCE indicates
27368275SEric Cheng  * that a control operation wants to quiesce the Tx data flow in which case
27378275SEric Cheng  * we return an error. Holding any of the per cpu locks ensures that the
27388275SEric Cheng  * mci_tx_flag won't change.
27398275SEric Cheng  *
27408275SEric Cheng  * 'CPU' must be accessed just once and used to compute the index into the
27418275SEric Cheng  * percpu array, and that index must be used for the entire duration of the
27428275SEric Cheng  * packet send operation. Note that the thread may be preempted and run on
27438275SEric Cheng  * another cpu any time and so we can't use 'CPU' more than once for the
27448275SEric Cheng  * operation.
27458275SEric Cheng  */
27468275SEric Cheng #define	MAC_TX_TRY_HOLD(mcip, mytx, error)				\
27478275SEric Cheng {									\
27488275SEric Cheng 	(error) = 0;							\
27498275SEric Cheng 	(mytx) = &(mcip)->mci_tx_pcpu[CPU->cpu_seqid & mac_tx_percpu_cnt]; \
27508275SEric Cheng 	mutex_enter(&(mytx)->pcpu_tx_lock);				\
27518275SEric Cheng 	if (!((mcip)->mci_tx_flag & MCI_TX_QUIESCE)) {			\
27528275SEric Cheng 		(mytx)->pcpu_tx_refcnt++;				\
27538275SEric Cheng 	} else {							\
27548275SEric Cheng 		(error) = -1;						\
27558275SEric Cheng 	}								\
27568275SEric Cheng 	mutex_exit(&(mytx)->pcpu_tx_lock);				\
27578275SEric Cheng }
27588275SEric Cheng 
27598275SEric Cheng /*
27608275SEric Cheng  * Release the reference. If needed, signal any control operation waiting
27618275SEric Cheng  * for Tx quiescence. The wait and signal are always done using the
27628275SEric Cheng  * mci_tx_pcpu[0]'s lock
27638275SEric Cheng  */
27648275SEric Cheng #define	MAC_TX_RELE(mcip, mytx) {					\
27658275SEric Cheng 	mutex_enter(&(mytx)->pcpu_tx_lock);				\
27668275SEric Cheng 	if (--(mytx)->pcpu_tx_refcnt == 0 &&				\
27678275SEric Cheng 	    (mcip)->mci_tx_flag & MCI_TX_QUIESCE) {			\
27688275SEric Cheng 		mutex_exit(&(mytx)->pcpu_tx_lock);			\
27698275SEric Cheng 		mutex_enter(&(mcip)->mci_tx_pcpu[0].pcpu_tx_lock);	\
27708275SEric Cheng 		cv_signal(&(mcip)->mci_tx_cv);				\
27718275SEric Cheng 		mutex_exit(&(mcip)->mci_tx_pcpu[0].pcpu_tx_lock);	\
27728275SEric Cheng 	} else {							\
27738275SEric Cheng 		mutex_exit(&(mytx)->pcpu_tx_lock);			\
27748275SEric Cheng 	}								\
27758275SEric Cheng }
27768275SEric Cheng 
27778275SEric Cheng /*
27788275SEric Cheng  * Bump the count of the number of active Tx threads. This is maintained as
27798275SEric Cheng  * a per CPU counter. On (CMT kind of) machines with large number of CPUs,
27808275SEric Cheng  * a single mci_tx_lock may become contended. However a count of the total
27818275SEric Cheng  * number of Tx threads per client is needed in order to quiesce the Tx side
27828275SEric Cheng  * prior to reassigning a Tx ring dynamically to another client. The thread
27838275SEric Cheng  * that needs to quiesce the Tx traffic grabs all the percpu locks and checks
27848275SEric Cheng  * the sum of the individual percpu refcnts. Each Tx data thread only grabs
27858275SEric Cheng  * its own percpu lock and increments its own refcnt.
27868275SEric Cheng  */
27878275SEric Cheng void *
27888275SEric Cheng mac_tx_hold(mac_client_handle_t mch)
27898275SEric Cheng {
27908275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
27918275SEric Cheng 	mac_tx_percpu_t	*mytx;
27928275SEric Cheng 	int error;
27938275SEric Cheng 
27948275SEric Cheng 	MAC_TX_TRY_HOLD(mcip, mytx, error);
27958275SEric Cheng 	return (error == 0 ? (void *)mytx : NULL);
27968275SEric Cheng }
27978275SEric Cheng 
27988275SEric Cheng void
27998275SEric Cheng mac_tx_rele(mac_client_handle_t mch, void *mytx_handle)
28008275SEric Cheng {
28018275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
28028275SEric Cheng 	mac_tx_percpu_t	*mytx = mytx_handle;
28038275SEric Cheng 
28048275SEric Cheng 	MAC_TX_RELE(mcip, mytx)
28058275SEric Cheng }
28068275SEric Cheng 
28078275SEric Cheng /*
28088275SEric Cheng  * Send function invoked by MAC clients.
28098275SEric Cheng  */
28108275SEric Cheng mac_tx_cookie_t
28118275SEric Cheng mac_tx(mac_client_handle_t mch, mblk_t *mp_chain, uintptr_t hint,
28128275SEric Cheng     uint16_t flag, mblk_t **ret_mp)
28138275SEric Cheng {
281410734SEric Cheng 	mac_tx_cookie_t		cookie = NULL;
28158275SEric Cheng 	int			error;
28168275SEric Cheng 	mac_tx_percpu_t		*mytx;
28178275SEric Cheng 	mac_soft_ring_set_t	*srs;
28188275SEric Cheng 	flow_entry_t		*flent;
28198275SEric Cheng 	boolean_t		is_subflow = B_FALSE;
28208275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
28218275SEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
28228275SEric Cheng 	mac_srs_tx_t		*srs_tx;
28238275SEric Cheng 
28248275SEric Cheng 	/*
28258275SEric Cheng 	 * Check whether the active Tx threads count is bumped already.
28268275SEric Cheng 	 */
28278275SEric Cheng 	if (!(flag & MAC_TX_NO_HOLD)) {
28288275SEric Cheng 		MAC_TX_TRY_HOLD(mcip, mytx, error);
28298275SEric Cheng 		if (error != 0) {
28308275SEric Cheng 			freemsgchain(mp_chain);
28318275SEric Cheng 			return (NULL);
28328275SEric Cheng 		}
28338275SEric Cheng 	}
28348275SEric Cheng 
283510734SEric Cheng 	/*
283610734SEric Cheng 	 * If mac protection is enabled, only the permissible packets will be
283710734SEric Cheng 	 * returned by mac_protect_check().
283810734SEric Cheng 	 */
283910734SEric Cheng 	if ((mcip->mci_flent->
284010734SEric Cheng 	    fe_resource_props.mrp_mask & MRP_PROTECT) != 0 &&
284110734SEric Cheng 	    (mp_chain = mac_protect_check(mch, mp_chain)) == NULL)
284210734SEric Cheng 		goto done;
284310734SEric Cheng 
28448275SEric Cheng 	if (mcip->mci_subflow_tab != NULL &&
28458275SEric Cheng 	    mcip->mci_subflow_tab->ft_flow_count > 0 &&
28468275SEric Cheng 	    mac_flow_lookup(mcip->mci_subflow_tab, mp_chain,
28478275SEric Cheng 	    FLOW_OUTBOUND, &flent) == 0) {
28488275SEric Cheng 		/*
28498275SEric Cheng 		 * The main assumption here is that if in the event
28508275SEric Cheng 		 * we get a chain, all the packets will be classified
28518275SEric Cheng 		 * to the same Flow/SRS. If this changes for any
28528275SEric Cheng 		 * reason, the following logic should change as well.
28538275SEric Cheng 		 * I suppose the fanout_hint also assumes this .
28548275SEric Cheng 		 */
28558275SEric Cheng 		ASSERT(flent != NULL);
28568275SEric Cheng 		is_subflow = B_TRUE;
28578275SEric Cheng 	} else {
28588275SEric Cheng 		flent = mcip->mci_flent;
28598275SEric Cheng 	}
28608275SEric Cheng 
28618275SEric Cheng 	srs = flent->fe_tx_srs;
286210639SDarren.Reed@Sun.COM 	/*
286310639SDarren.Reed@Sun.COM 	 * This is to avoid panics with PF_PACKET that can call mac_tx()
286410639SDarren.Reed@Sun.COM 	 * against an interface that is not capable of sending. A rewrite
286510639SDarren.Reed@Sun.COM 	 * of the mac datapath is required to remove this limitation.
286610639SDarren.Reed@Sun.COM 	 */
286710639SDarren.Reed@Sun.COM 	if (srs == NULL) {
286810639SDarren.Reed@Sun.COM 		freemsgchain(mp_chain);
286910734SEric Cheng 		goto done;
287010639SDarren.Reed@Sun.COM 	}
287110734SEric Cheng 
28728275SEric Cheng 	srs_tx = &srs->srs_tx;
28738275SEric Cheng 	if (srs_tx->st_mode == SRS_TX_DEFAULT &&
28748275SEric Cheng 	    (srs->srs_state & SRS_ENQUEUED) == 0 &&
28758275SEric Cheng 	    mip->mi_nactiveclients == 1 && mip->mi_promisc_list == NULL &&
28768275SEric Cheng 	    mp_chain->b_next == NULL) {
28778275SEric Cheng 		uint64_t	obytes;
28788275SEric Cheng 
28798275SEric Cheng 		/*
28808275SEric Cheng 		 * Since dls always opens the underlying MAC, nclients equals
28818275SEric Cheng 		 * to 1 means that the only active client is dls itself acting
28828275SEric Cheng 		 * as a primary client of the MAC instance. Since dls will not
28838275SEric Cheng 		 * send tagged packets in that case, and dls is trusted to send
28848275SEric Cheng 		 * packets for its allowed VLAN(s), the VLAN tag insertion and
28858275SEric Cheng 		 * check is required only if nclients is greater than 1.
28868275SEric Cheng 		 */
28878275SEric Cheng 		if (mip->mi_nclients > 1) {
28888275SEric Cheng 			if (MAC_VID_CHECK_NEEDED(mcip)) {
28898275SEric Cheng 				int	err = 0;
28908275SEric Cheng 
28918275SEric Cheng 				MAC_VID_CHECK(mcip, mp_chain, err);
28928275SEric Cheng 				if (err != 0) {
28938275SEric Cheng 					freemsg(mp_chain);
28948275SEric Cheng 					mcip->mci_stat_oerrors++;
28958275SEric Cheng 					goto done;
28968275SEric Cheng 				}
28978275SEric Cheng 			}
28988275SEric Cheng 			if (MAC_TAG_NEEDED(mcip)) {
28998275SEric Cheng 				mp_chain = mac_add_vlan_tag(mp_chain, 0,
29008275SEric Cheng 				    mac_client_vid(mch));
29018275SEric Cheng 				if (mp_chain == NULL) {
29028275SEric Cheng 					mcip->mci_stat_oerrors++;
29038275SEric Cheng 					goto done;
29048275SEric Cheng 				}
29058275SEric Cheng 			}
29068275SEric Cheng 		}
29078275SEric Cheng 
29088275SEric Cheng 		obytes = (mp_chain->b_cont == NULL ? MBLKL(mp_chain) :
29098275SEric Cheng 		    msgdsize(mp_chain));
29108275SEric Cheng 
291110491SRishi.Srivatsavai@Sun.COM 		MAC_TX(mip, srs_tx->st_arg2, mp_chain,
291210491SRishi.Srivatsavai@Sun.COM 		    ((mcip->mci_state_flags & MCIS_SHARE_BOUND) != 0));
29138275SEric Cheng 
29148275SEric Cheng 		if (mp_chain == NULL) {
29158275SEric Cheng 			cookie = NULL;
29168275SEric Cheng 			mcip->mci_stat_obytes += obytes;
29178275SEric Cheng 			mcip->mci_stat_opackets += 1;
29188275SEric Cheng 			if ((srs->srs_type & SRST_FLOW) != 0) {
29198275SEric Cheng 				FLOW_STAT_UPDATE(flent, obytes, obytes);
29208275SEric Cheng 				FLOW_STAT_UPDATE(flent, opackets, 1);
29218275SEric Cheng 			}
29228275SEric Cheng 		} else {
29238275SEric Cheng 			mutex_enter(&srs->srs_lock);
29248275SEric Cheng 			cookie = mac_tx_srs_no_desc(srs, mp_chain,
29258275SEric Cheng 			    flag, ret_mp);
29268275SEric Cheng 			mutex_exit(&srs->srs_lock);
29278275SEric Cheng 		}
29288275SEric Cheng 	} else {
29298275SEric Cheng 		cookie = srs_tx->st_func(srs, mp_chain, hint, flag, ret_mp);
29308275SEric Cheng 	}
29318275SEric Cheng 
29328275SEric Cheng done:
29338275SEric Cheng 	if (is_subflow)
29348275SEric Cheng 		FLOW_REFRELE(flent);
29358275SEric Cheng 
29368275SEric Cheng 	if (!(flag & MAC_TX_NO_HOLD))
29378275SEric Cheng 		MAC_TX_RELE(mcip, mytx);
29388275SEric Cheng 
29398275SEric Cheng 	return (cookie);
29408275SEric Cheng }
29418275SEric Cheng 
29428275SEric Cheng /*
29438275SEric Cheng  * mac_tx_is_blocked
29448275SEric Cheng  *
29458275SEric Cheng  * Given a cookie, it returns if the ring identified by the cookie is
29468833SVenu.Iyer@Sun.COM  * flow-controlled or not. If NULL is passed in place of a cookie,
29478833SVenu.Iyer@Sun.COM  * then it finds out if any of the underlying rings belonging to the
29488833SVenu.Iyer@Sun.COM  * SRS is flow controlled or not and returns that status.
29498275SEric Cheng  */
29508275SEric Cheng /* ARGSUSED */
29518275SEric Cheng boolean_t
29528275SEric Cheng mac_tx_is_flow_blocked(mac_client_handle_t mch, mac_tx_cookie_t cookie)
29538275SEric Cheng {
29548275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
29558833SVenu.Iyer@Sun.COM 	mac_soft_ring_set_t *mac_srs;
29568275SEric Cheng 	mac_soft_ring_t *sringp;
29578275SEric Cheng 	boolean_t blocked = B_FALSE;
29588833SVenu.Iyer@Sun.COM 	mac_tx_percpu_t *mytx;
29598833SVenu.Iyer@Sun.COM 	int err;
29608275SEric Cheng 	int i;
29618275SEric Cheng 
29628275SEric Cheng 	/*
29638833SVenu.Iyer@Sun.COM 	 * Bump the reference count so that mac_srs won't be deleted.
29648833SVenu.Iyer@Sun.COM 	 * If the client is currently quiesced and we failed to bump
29658833SVenu.Iyer@Sun.COM 	 * the reference, return B_TRUE so that flow control stays
29668833SVenu.Iyer@Sun.COM 	 * as enabled.
29678833SVenu.Iyer@Sun.COM 	 *
29688833SVenu.Iyer@Sun.COM 	 * Flow control will then be disabled once the client is no
29698833SVenu.Iyer@Sun.COM 	 * longer quiesced.
29708275SEric Cheng 	 */
29718833SVenu.Iyer@Sun.COM 	MAC_TX_TRY_HOLD(mcip, mytx, err);
29728833SVenu.Iyer@Sun.COM 	if (err != 0)
29738833SVenu.Iyer@Sun.COM 		return (B_TRUE);
29748833SVenu.Iyer@Sun.COM 
29758833SVenu.Iyer@Sun.COM 	if ((mac_srs = MCIP_TX_SRS(mcip)) == NULL) {
29768833SVenu.Iyer@Sun.COM 		MAC_TX_RELE(mcip, mytx);
29778275SEric Cheng 		return (B_FALSE);
29788833SVenu.Iyer@Sun.COM 	}
29798275SEric Cheng 
29808275SEric Cheng 	mutex_enter(&mac_srs->srs_lock);
29818275SEric Cheng 	if (mac_srs->srs_tx.st_mode == SRS_TX_FANOUT) {
29828833SVenu.Iyer@Sun.COM 		if (cookie != NULL) {
29838833SVenu.Iyer@Sun.COM 			sringp = (mac_soft_ring_t *)cookie;
29848275SEric Cheng 			mutex_enter(&sringp->s_ring_lock);
29858833SVenu.Iyer@Sun.COM 			if (sringp->s_ring_state & S_RING_TX_HIWAT)
29868275SEric Cheng 				blocked = B_TRUE;
29878833SVenu.Iyer@Sun.COM 			mutex_exit(&sringp->s_ring_lock);
29888833SVenu.Iyer@Sun.COM 		} else {
29898833SVenu.Iyer@Sun.COM 			for (i = 0; i < mac_srs->srs_oth_ring_count; i++) {
29908833SVenu.Iyer@Sun.COM 				sringp = mac_srs->srs_oth_soft_rings[i];
29918833SVenu.Iyer@Sun.COM 				mutex_enter(&sringp->s_ring_lock);
29928833SVenu.Iyer@Sun.COM 				if (sringp->s_ring_state & S_RING_TX_HIWAT) {
29938833SVenu.Iyer@Sun.COM 					blocked = B_TRUE;
29948833SVenu.Iyer@Sun.COM 					mutex_exit(&sringp->s_ring_lock);
29958833SVenu.Iyer@Sun.COM 					break;
29968833SVenu.Iyer@Sun.COM 				}
29978275SEric Cheng 				mutex_exit(&sringp->s_ring_lock);
29988275SEric Cheng 			}
29998275SEric Cheng 		}
30008275SEric Cheng 	} else {
30018275SEric Cheng 		blocked = (mac_srs->srs_state & SRS_TX_HIWAT);
30028275SEric Cheng 	}
30038275SEric Cheng 	mutex_exit(&mac_srs->srs_lock);
30048833SVenu.Iyer@Sun.COM 	MAC_TX_RELE(mcip, mytx);
30058275SEric Cheng 	return (blocked);
30068275SEric Cheng }
30078275SEric Cheng 
30088275SEric Cheng /*
30098275SEric Cheng  * Check if the MAC client is the primary MAC client.
30108275SEric Cheng  */
30118275SEric Cheng boolean_t
30128275SEric Cheng mac_is_primary_client(mac_client_impl_t *mcip)
30138275SEric Cheng {
30148275SEric Cheng 	return (mcip->mci_flags & MAC_CLIENT_FLAGS_PRIMARY);
30158275SEric Cheng }
30168275SEric Cheng 
30178275SEric Cheng void
30188275SEric Cheng mac_ioctl(mac_handle_t mh, queue_t *wq, mblk_t *bp)
30198275SEric Cheng {
30208275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
30218275SEric Cheng 	int cmd = ((struct iocblk *)bp->b_rptr)->ioc_cmd;
30228275SEric Cheng 
30238275SEric Cheng 	if ((cmd == ND_GET && (mip->mi_callbacks->mc_callbacks & MC_GETPROP)) ||
30248275SEric Cheng 	    (cmd == ND_SET && (mip->mi_callbacks->mc_callbacks & MC_SETPROP))) {
30258275SEric Cheng 		/*
30268275SEric Cheng 		 * If ndd props were registered, call them.
30278275SEric Cheng 		 * Note that ndd ioctls are Obsolete
30288275SEric Cheng 		 */
30298275SEric Cheng 		mac_ndd_ioctl(mip, wq, bp);
30308275SEric Cheng 		return;
30318275SEric Cheng 	}
30328275SEric Cheng 
30338275SEric Cheng 	/*
30348275SEric Cheng 	 * Call the driver to handle the ioctl.  The driver may not support
30358275SEric Cheng 	 * any ioctls, in which case we reply with a NAK on its behalf.
30368275SEric Cheng 	 */
30378275SEric Cheng 	if (mip->mi_callbacks->mc_callbacks & MC_IOCTL)
30388275SEric Cheng 		mip->mi_ioctl(mip->mi_driver, wq, bp);
30398275SEric Cheng 	else
30408275SEric Cheng 		miocnak(wq, bp, 0, EINVAL);
30418275SEric Cheng }
30428275SEric Cheng 
30438275SEric Cheng /*
30448275SEric Cheng  * Return the link state of the specified MAC instance.
30458275SEric Cheng  */
30468275SEric Cheng link_state_t
30478275SEric Cheng mac_link_get(mac_handle_t mh)
30488275SEric Cheng {
30498275SEric Cheng 	return (((mac_impl_t *)mh)->mi_linkstate);
30508275SEric Cheng }
30518275SEric Cheng 
30528275SEric Cheng /*
30538275SEric Cheng  * Add a mac client specified notification callback. Please see the comments
30548275SEric Cheng  * above mac_callback_add() for general information about mac callback
30558275SEric Cheng  * addition/deletion in the presence of mac callback list walkers
30568275SEric Cheng  */
30578275SEric Cheng mac_notify_handle_t
30588275SEric Cheng mac_notify_add(mac_handle_t mh, mac_notify_t notify_fn, void *arg)
30598275SEric Cheng {
30608275SEric Cheng 	mac_impl_t		*mip = (mac_impl_t *)mh;
30618275SEric Cheng 	mac_notify_cb_t		*mncb;
30628275SEric Cheng 	mac_cb_info_t		*mcbi;
30638275SEric Cheng 
30648275SEric Cheng 	/*
30658275SEric Cheng 	 * Allocate a notify callback structure, fill in the details and
30668275SEric Cheng 	 * use the mac callback list manipulation functions to chain into
30678275SEric Cheng 	 * the list of callbacks.
30688275SEric Cheng 	 */
30698275SEric Cheng 	mncb = kmem_zalloc(sizeof (mac_notify_cb_t), KM_SLEEP);
30708275SEric Cheng 	mncb->mncb_fn = notify_fn;
30718275SEric Cheng 	mncb->mncb_arg = arg;
30728275SEric Cheng 	mncb->mncb_mip = mip;
30738275SEric Cheng 	mncb->mncb_link.mcb_objp = mncb;
30748275SEric Cheng 	mncb->mncb_link.mcb_objsize = sizeof (mac_notify_cb_t);
30758275SEric Cheng 	mncb->mncb_link.mcb_flags = MCB_NOTIFY_CB_T;
30768275SEric Cheng 
30778275SEric Cheng 	mcbi = &mip->mi_notify_cb_info;
30788275SEric Cheng 
30798275SEric Cheng 	i_mac_perim_enter(mip);
30808275SEric Cheng 	mutex_enter(mcbi->mcbi_lockp);
30818275SEric Cheng 
30828275SEric Cheng 	mac_callback_add(&mip->mi_notify_cb_info, &mip->mi_notify_cb_list,
30838275SEric Cheng 	    &mncb->mncb_link);
30848275SEric Cheng 
30858275SEric Cheng 	mutex_exit(mcbi->mcbi_lockp);
30868275SEric Cheng 	i_mac_perim_exit(mip);
30878275SEric Cheng 	return ((mac_notify_handle_t)mncb);
30888275SEric Cheng }
30898275SEric Cheng 
30908275SEric Cheng void
30918275SEric Cheng mac_notify_remove_wait(mac_handle_t mh)
30928275SEric Cheng {
30938275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
30948275SEric Cheng 	mac_cb_info_t	*mcbi = &mip->mi_notify_cb_info;
30958275SEric Cheng 
30968275SEric Cheng 	mutex_enter(mcbi->mcbi_lockp);
30978275SEric Cheng 	mac_callback_remove_wait(&mip->mi_notify_cb_info);
30988275SEric Cheng 	mutex_exit(mcbi->mcbi_lockp);
30998275SEric Cheng }
31008275SEric Cheng 
31018275SEric Cheng /*
31028275SEric Cheng  * Remove a mac client specified notification callback
31038275SEric Cheng  */
31048275SEric Cheng int
31058275SEric Cheng mac_notify_remove(mac_notify_handle_t mnh, boolean_t wait)
31068275SEric Cheng {
31078275SEric Cheng 	mac_notify_cb_t	*mncb = (mac_notify_cb_t *)mnh;
31088275SEric Cheng 	mac_impl_t	*mip = mncb->mncb_mip;
31098275SEric Cheng 	mac_cb_info_t	*mcbi;
31108275SEric Cheng 	int		err = 0;
31118275SEric Cheng 
31128275SEric Cheng 	mcbi = &mip->mi_notify_cb_info;
31138275SEric Cheng 
31148275SEric Cheng 	i_mac_perim_enter(mip);
31158275SEric Cheng 	mutex_enter(mcbi->mcbi_lockp);
31168275SEric Cheng 
31178275SEric Cheng 	ASSERT(mncb->mncb_link.mcb_objp == mncb);
31188275SEric Cheng 	/*
31198275SEric Cheng 	 * If there aren't any list walkers, the remove would succeed
31208275SEric Cheng 	 * inline, else we wait for the deferred remove to complete
31218275SEric Cheng 	 */
31228275SEric Cheng 	if (mac_callback_remove(&mip->mi_notify_cb_info,
31238275SEric Cheng 	    &mip->mi_notify_cb_list, &mncb->mncb_link)) {
31248275SEric Cheng 		kmem_free(mncb, sizeof (mac_notify_cb_t));
31258275SEric Cheng 	} else {
31268275SEric Cheng 		err = EBUSY;
31278275SEric Cheng 	}
31288275SEric Cheng 
31298275SEric Cheng 	mutex_exit(mcbi->mcbi_lockp);
31308275SEric Cheng 	i_mac_perim_exit(mip);
31318275SEric Cheng 
31328275SEric Cheng 	/*
31338275SEric Cheng 	 * If we failed to remove the notification callback and "wait" is set
31348275SEric Cheng 	 * to be B_TRUE, wait for the callback to finish after we exit the
31358275SEric Cheng 	 * mac perimeter.
31368275SEric Cheng 	 */
31378275SEric Cheng 	if (err != 0 && wait) {
31388275SEric Cheng 		mac_notify_remove_wait((mac_handle_t)mip);
31398275SEric Cheng 		return (0);
31408275SEric Cheng 	}
31418275SEric Cheng 
31428275SEric Cheng 	return (err);
31438275SEric Cheng }
31448275SEric Cheng 
31458275SEric Cheng /*
31468275SEric Cheng  * Associate resource management callbacks with the specified MAC
31478275SEric Cheng  * clients.
31488275SEric Cheng  */
31498275SEric Cheng 
31508275SEric Cheng void
31518275SEric Cheng mac_resource_set_common(mac_client_handle_t mch, mac_resource_add_t add,
31528275SEric Cheng     mac_resource_remove_t remove, mac_resource_quiesce_t quiesce,
31538275SEric Cheng     mac_resource_restart_t restart, mac_resource_bind_t bind,
31548275SEric Cheng     void *arg)
31558275SEric Cheng {
31568275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
31578275SEric Cheng 
31588275SEric Cheng 	mcip->mci_resource_add = add;
31598275SEric Cheng 	mcip->mci_resource_remove = remove;
31608275SEric Cheng 	mcip->mci_resource_quiesce = quiesce;
31618275SEric Cheng 	mcip->mci_resource_restart = restart;
31628275SEric Cheng 	mcip->mci_resource_bind = bind;
31638275SEric Cheng 	mcip->mci_resource_arg = arg;
31648275SEric Cheng }
31658275SEric Cheng 
31668275SEric Cheng void
31678275SEric Cheng mac_resource_set(mac_client_handle_t mch, mac_resource_add_t add, void *arg)
31688275SEric Cheng {
31698275SEric Cheng 	/* update the 'resource_add' callback */
31708275SEric Cheng 	mac_resource_set_common(mch, add, NULL, NULL, NULL, NULL, arg);
31718275SEric Cheng }
31728275SEric Cheng 
31738275SEric Cheng /*
31748275SEric Cheng  * Sets up the client resources and enable the polling interface over all the
31758275SEric Cheng  * SRS's and the soft rings of the client
31768275SEric Cheng  */
31778275SEric Cheng void
31788275SEric Cheng mac_client_poll_enable(mac_client_handle_t mch)
31798275SEric Cheng {
31808275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
31818275SEric Cheng 	mac_soft_ring_set_t	*mac_srs;
31828275SEric Cheng 	flow_entry_t		*flent;
31838275SEric Cheng 	int			i;
31848275SEric Cheng 
31858275SEric Cheng 	flent = mcip->mci_flent;
31868275SEric Cheng 	ASSERT(flent != NULL);
31878275SEric Cheng 
318811021SEric.Cheng@Sun.COM 	mcip->mci_state_flags |= MCIS_CLIENT_POLL_CAPABLE;
31898275SEric Cheng 	for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
31908275SEric Cheng 		mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
31918275SEric Cheng 		ASSERT(mac_srs->srs_mcip == mcip);
31928275SEric Cheng 		mac_srs_client_poll_enable(mcip, mac_srs);
31938275SEric Cheng 	}
31948275SEric Cheng }
31958275SEric Cheng 
31968275SEric Cheng /*
31978275SEric Cheng  * Tears down the client resources and disable the polling interface over all
31988275SEric Cheng  * the SRS's and the soft rings of the client
31998275SEric Cheng  */
32008275SEric Cheng void
32018275SEric Cheng mac_client_poll_disable(mac_client_handle_t mch)
32028275SEric Cheng {
32038275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
32048275SEric Cheng 	mac_soft_ring_set_t	*mac_srs;
32058275SEric Cheng 	flow_entry_t		*flent;
32068275SEric Cheng 	int			i;
32078275SEric Cheng 
32088275SEric Cheng 	flent = mcip->mci_flent;
32098275SEric Cheng 	ASSERT(flent != NULL);
32108275SEric Cheng 
321111021SEric.Cheng@Sun.COM 	mcip->mci_state_flags &= ~MCIS_CLIENT_POLL_CAPABLE;
32128275SEric Cheng 	for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
32138275SEric Cheng 		mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
32148275SEric Cheng 		ASSERT(mac_srs->srs_mcip == mcip);
32158275SEric Cheng 		mac_srs_client_poll_disable(mcip, mac_srs);
32168275SEric Cheng 	}
32178275SEric Cheng }
32188275SEric Cheng 
32198275SEric Cheng /*
32208275SEric Cheng  * Associate the CPUs specified by the given property with a MAC client.
32218275SEric Cheng  */
32228275SEric Cheng int
32238275SEric Cheng mac_cpu_set(mac_client_handle_t mch, mac_resource_props_t *mrp)
32248275SEric Cheng {
32258275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
32268275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
32278275SEric Cheng 	int err = 0;
32288275SEric Cheng 
32298275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
32308275SEric Cheng 
32318275SEric Cheng 	if ((err = mac_validate_props(mrp)) != 0)
32328275SEric Cheng 		return (err);
32338275SEric Cheng 
32348275SEric Cheng 	if (MCIP_DATAPATH_SETUP(mcip))
32358275SEric Cheng 		mac_flow_modify(mip->mi_flow_tab, mcip->mci_flent, mrp);
32368275SEric Cheng 
32378275SEric Cheng 	mac_update_resources(mrp, MCIP_RESOURCE_PROPS(mcip), B_FALSE);
32388275SEric Cheng 	return (0);
32398275SEric Cheng }
32408275SEric Cheng 
32418275SEric Cheng /*
32428275SEric Cheng  * Apply the specified properties to the specified MAC client.
32438275SEric Cheng  */
32448275SEric Cheng int
32458275SEric Cheng mac_client_set_resources(mac_client_handle_t mch, mac_resource_props_t *mrp)
32468275SEric Cheng {
32478275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
32488275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
32498275SEric Cheng 	int err = 0;
32508275SEric Cheng 
32518275SEric Cheng 	i_mac_perim_enter(mip);
32528275SEric Cheng 
32538275SEric Cheng 	if ((mrp->mrp_mask & MRP_MAXBW) || (mrp->mrp_mask & MRP_PRIORITY)) {
32548275SEric Cheng 		err = mac_resource_ctl_set(mch, mrp);
325510734SEric Cheng 		if (err != 0)
325610734SEric Cheng 			goto done;
32578275SEric Cheng 	}
32588275SEric Cheng 
325910734SEric Cheng 	if (mrp->mrp_mask & MRP_CPUS) {
32608275SEric Cheng 		err = mac_cpu_set(mch, mrp);
326110734SEric Cheng 		if (err != 0)
326210734SEric Cheng 			goto done;
326310734SEric Cheng 	}
326410734SEric Cheng 
326510734SEric Cheng 	if (mrp->mrp_mask & MRP_PROTECT)
326610734SEric Cheng 		err = mac_protect_set(mch, mrp);
326710734SEric Cheng 
326810734SEric Cheng done:
32698275SEric Cheng 	i_mac_perim_exit(mip);
32708275SEric Cheng 	return (err);
32718275SEric Cheng }
32728275SEric Cheng 
32738275SEric Cheng /*
32748275SEric Cheng  * Return the properties currently associated with the specified MAC client.
32758275SEric Cheng  */
32768275SEric Cheng void
32778275SEric Cheng mac_client_get_resources(mac_client_handle_t mch, mac_resource_props_t *mrp)
32788275SEric Cheng {
32798275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
32808275SEric Cheng 	mac_resource_props_t	*mcip_mrp = MCIP_RESOURCE_PROPS(mcip);
32818275SEric Cheng 
32828275SEric Cheng 	bcopy(mcip_mrp, mrp, sizeof (mac_resource_props_t));
32838275SEric Cheng }
32848275SEric Cheng 
32858275SEric Cheng /*
32868275SEric Cheng  * Pass a copy of the specified packet to the promiscuous callbacks
32878275SEric Cheng  * of the specified MAC.
32888275SEric Cheng  *
32898275SEric Cheng  * If sender is NULL, the function is being invoked for a packet chain
32908275SEric Cheng  * received from the wire. If sender is non-NULL, it points to
32918275SEric Cheng  * the MAC client from which the packet is being sent.
32928275SEric Cheng  *
32938275SEric Cheng  * The packets are distributed to the promiscuous callbacks as follows:
32948275SEric Cheng  *
32958275SEric Cheng  * - all packets are sent to the MAC_CLIENT_PROMISC_ALL callbacks
32968275SEric Cheng  * - all broadcast and multicast packets are sent to the
32978275SEric Cheng  *   MAC_CLIENT_PROMISC_FILTER and MAC_CLIENT_PROMISC_MULTI.
32988275SEric Cheng  *
32998275SEric Cheng  * The unicast packets of MAC_CLIENT_PROMISC_FILTER callbacks are dispatched
33008275SEric Cheng  * after classification by mac_rx_deliver().
33018275SEric Cheng  */
33028275SEric Cheng 
33038275SEric Cheng static void
33048275SEric Cheng mac_promisc_dispatch_one(mac_promisc_impl_t *mpip, mblk_t *mp,
33058275SEric Cheng     boolean_t loopback)
33068275SEric Cheng {
330710639SDarren.Reed@Sun.COM 	mblk_t *mp_copy, *mp_next;
330810639SDarren.Reed@Sun.COM 
330910639SDarren.Reed@Sun.COM 	if (!mpip->mpi_no_copy || mpip->mpi_strip_vlan_tag) {
331010639SDarren.Reed@Sun.COM 		mp_copy = copymsg(mp);
331110639SDarren.Reed@Sun.COM 		if (mp_copy == NULL)
331210639SDarren.Reed@Sun.COM 			return;
331310639SDarren.Reed@Sun.COM 
331410639SDarren.Reed@Sun.COM 		if (mpip->mpi_strip_vlan_tag) {
331510639SDarren.Reed@Sun.COM 			mp_copy = mac_strip_vlan_tag_chain(mp_copy);
331610639SDarren.Reed@Sun.COM 			if (mp_copy == NULL)
331710639SDarren.Reed@Sun.COM 				return;
331810639SDarren.Reed@Sun.COM 		}
331910639SDarren.Reed@Sun.COM 		mp_next = NULL;
332010639SDarren.Reed@Sun.COM 	} else {
332110639SDarren.Reed@Sun.COM 		mp_copy = mp;
332210639SDarren.Reed@Sun.COM 		mp_next = mp->b_next;
332310639SDarren.Reed@Sun.COM 	}
33248275SEric Cheng 	mp_copy->b_next = NULL;
33258275SEric Cheng 
33268275SEric Cheng 	mpip->mpi_fn(mpip->mpi_arg, NULL, mp_copy, loopback);
332710639SDarren.Reed@Sun.COM 	if (mp_copy == mp)
332810639SDarren.Reed@Sun.COM 		mp->b_next = mp_next;
33298275SEric Cheng }
33308275SEric Cheng 
33318275SEric Cheng /*
33328275SEric Cheng  * Return the VID of a packet. Zero if the packet is not tagged.
33338275SEric Cheng  */
33348275SEric Cheng static uint16_t
33358275SEric Cheng mac_ether_vid(mblk_t *mp)
33368275SEric Cheng {
33378275SEric Cheng 	struct ether_header *eth = (struct ether_header *)mp->b_rptr;
33388275SEric Cheng 
33398275SEric Cheng 	if (ntohs(eth->ether_type) == ETHERTYPE_VLAN) {
33408275SEric Cheng 		struct ether_vlan_header *t_evhp =
33418275SEric Cheng 		    (struct ether_vlan_header *)mp->b_rptr;
33428275SEric Cheng 		return (VLAN_ID(ntohs(t_evhp->ether_tci)));
33438275SEric Cheng 	}
33448275SEric Cheng 
33458275SEric Cheng 	return (0);
33468275SEric Cheng }
33478275SEric Cheng 
33488275SEric Cheng /*
33498275SEric Cheng  * Return whether the specified packet contains a multicast or broadcast
33508275SEric Cheng  * destination MAC address.
33518275SEric Cheng  */
33528275SEric Cheng static boolean_t
33538275SEric Cheng mac_is_mcast(mac_impl_t *mip, mblk_t *mp)
33548275SEric Cheng {
33558275SEric Cheng 	mac_header_info_t hdr_info;
33568275SEric Cheng 
33578275SEric Cheng 	if (mac_header_info((mac_handle_t)mip, mp, &hdr_info) != 0)
33588275SEric Cheng 		return (B_FALSE);
33598275SEric Cheng 	return ((hdr_info.mhi_dsttype == MAC_ADDRTYPE_BROADCAST) ||
33608275SEric Cheng 	    (hdr_info.mhi_dsttype == MAC_ADDRTYPE_MULTICAST));
33618275SEric Cheng }
33628275SEric Cheng 
33638275SEric Cheng /*
33648275SEric Cheng  * Send a copy of an mblk chain to the MAC clients of the specified MAC.
33658275SEric Cheng  * "sender" points to the sender MAC client for outbound packets, and
33668275SEric Cheng  * is set to NULL for inbound packets.
33678275SEric Cheng  */
33688275SEric Cheng void
33698275SEric Cheng mac_promisc_dispatch(mac_impl_t *mip, mblk_t *mp_chain,
33708275SEric Cheng     mac_client_impl_t *sender)
33718275SEric Cheng {
33728275SEric Cheng 	mac_promisc_impl_t *mpip;
33738275SEric Cheng 	mac_cb_t *mcb;
33748275SEric Cheng 	mblk_t *mp;
33758275SEric Cheng 	boolean_t is_mcast, is_sender;
33768275SEric Cheng 
33778275SEric Cheng 	MAC_PROMISC_WALKER_INC(mip);
33788275SEric Cheng 	for (mp = mp_chain; mp != NULL; mp = mp->b_next) {
33798275SEric Cheng 		is_mcast = mac_is_mcast(mip, mp);
33808275SEric Cheng 		/* send packet to interested callbacks */
33818275SEric Cheng 		for (mcb = mip->mi_promisc_list; mcb != NULL;
33828275SEric Cheng 		    mcb = mcb->mcb_nextp) {
33838275SEric Cheng 			mpip = (mac_promisc_impl_t *)mcb->mcb_objp;
33848275SEric Cheng 			is_sender = (mpip->mpi_mcip == sender);
33858275SEric Cheng 
33868275SEric Cheng 			if (is_sender && mpip->mpi_no_tx_loop)
33878275SEric Cheng 				/*
33888275SEric Cheng 				 * The sender doesn't want to receive
33898275SEric Cheng 				 * copies of the packets it sends.
33908275SEric Cheng 				 */
33918275SEric Cheng 				continue;
33928275SEric Cheng 
339310491SRishi.Srivatsavai@Sun.COM 			/* this client doesn't need any packets (bridge) */
339410491SRishi.Srivatsavai@Sun.COM 			if (mpip->mpi_fn == NULL)
339510491SRishi.Srivatsavai@Sun.COM 				continue;
339610491SRishi.Srivatsavai@Sun.COM 
33978275SEric Cheng 			/*
33988275SEric Cheng 			 * For an ethernet MAC, don't displatch a multicast
33998275SEric Cheng 			 * packet to a non-PROMISC_ALL callbacks unless the VID
34008275SEric Cheng 			 * of the packet matches the VID of the client.
34018275SEric Cheng 			 */
34028275SEric Cheng 			if (is_mcast &&
34038275SEric Cheng 			    mpip->mpi_type != MAC_CLIENT_PROMISC_ALL &&
34048275SEric Cheng 			    !mac_client_check_flow_vid(mpip->mpi_mcip,
34058275SEric Cheng 			    mac_ether_vid(mp)))
34068275SEric Cheng 				continue;
34078275SEric Cheng 
34088275SEric Cheng 			if (is_sender ||
34098275SEric Cheng 			    mpip->mpi_type == MAC_CLIENT_PROMISC_ALL ||
34108275SEric Cheng 			    is_mcast)
34118275SEric Cheng 				mac_promisc_dispatch_one(mpip, mp, is_sender);
34128275SEric Cheng 		}
34138275SEric Cheng 	}
34148275SEric Cheng 	MAC_PROMISC_WALKER_DCR(mip);
34158275SEric Cheng }
34168275SEric Cheng 
34178275SEric Cheng void
34188275SEric Cheng mac_promisc_client_dispatch(mac_client_impl_t *mcip, mblk_t *mp_chain)
34198275SEric Cheng {
34208275SEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
34218275SEric Cheng 	mac_promisc_impl_t	*mpip;
34228275SEric Cheng 	boolean_t		is_mcast;
34238275SEric Cheng 	mblk_t			*mp;
34248275SEric Cheng 	mac_cb_t		*mcb;
34258275SEric Cheng 
34268275SEric Cheng 	/*
34278275SEric Cheng 	 * The unicast packets for the MAC client still
34288275SEric Cheng 	 * need to be delivered to the MAC_CLIENT_PROMISC_FILTERED
34298275SEric Cheng 	 * promiscuous callbacks. The broadcast and multicast
34308275SEric Cheng 	 * packets were delivered from mac_rx().
34318275SEric Cheng 	 */
34328275SEric Cheng 	MAC_PROMISC_WALKER_INC(mip);
34338275SEric Cheng 	for (mp = mp_chain; mp != NULL; mp = mp->b_next) {
34348275SEric Cheng 		is_mcast = mac_is_mcast(mip, mp);
34358275SEric Cheng 		for (mcb = mcip->mci_promisc_list; mcb != NULL;
34368275SEric Cheng 		    mcb = mcb->mcb_nextp) {
34378275SEric Cheng 			mpip = (mac_promisc_impl_t *)mcb->mcb_objp;
34388275SEric Cheng 			if (mpip->mpi_type == MAC_CLIENT_PROMISC_FILTERED &&
34398275SEric Cheng 			    !is_mcast) {
34408275SEric Cheng 				mac_promisc_dispatch_one(mpip, mp, B_FALSE);
34418275SEric Cheng 			}
34428275SEric Cheng 		}
34438275SEric Cheng 	}
34448275SEric Cheng 	MAC_PROMISC_WALKER_DCR(mip);
34458275SEric Cheng }
34468275SEric Cheng 
34478275SEric Cheng /*
34488275SEric Cheng  * Return the margin value currently assigned to the specified MAC instance.
34498275SEric Cheng  */
34508275SEric Cheng void
34518275SEric Cheng mac_margin_get(mac_handle_t mh, uint32_t *marginp)
34528275SEric Cheng {
34538275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
34548275SEric Cheng 
34558275SEric Cheng 	rw_enter(&(mip->mi_rw_lock), RW_READER);
34568275SEric Cheng 	*marginp = mip->mi_margin;
34578275SEric Cheng 	rw_exit(&(mip->mi_rw_lock));
34588275SEric Cheng }
34598275SEric Cheng 
34608275SEric Cheng /*
34618275SEric Cheng  * mac_info_get() is used for retrieving the mac_info when a DL_INFO_REQ is
34628275SEric Cheng  * issued before a DL_ATTACH_REQ. we walk the i_mac_impl_hash table and find
34638275SEric Cheng  * the first mac_impl_t with a matching driver name; then we copy its mac_info_t
34648275SEric Cheng  * to the caller. we do all this with i_mac_impl_lock held so the mac_impl_t
34658275SEric Cheng  * cannot disappear while we are accessing it.
34668275SEric Cheng  */
34678275SEric Cheng typedef struct i_mac_info_state_s {
34688275SEric Cheng 	const char	*mi_name;
34698275SEric Cheng 	mac_info_t	*mi_infop;
34708275SEric Cheng } i_mac_info_state_t;
34718275SEric Cheng 
34728275SEric Cheng /*ARGSUSED*/
34738275SEric Cheng static uint_t
34748275SEric Cheng i_mac_info_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
34758275SEric Cheng {
34768275SEric Cheng 	i_mac_info_state_t *statep = arg;
34778275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)val;
34788275SEric Cheng 
34798275SEric Cheng 	if (mip->mi_state_flags & MIS_DISABLED)
34808275SEric Cheng 		return (MH_WALK_CONTINUE);
34818275SEric Cheng 
34828275SEric Cheng 	if (strcmp(statep->mi_name,
34838275SEric Cheng 	    ddi_driver_name(mip->mi_dip)) != 0)
34848275SEric Cheng 		return (MH_WALK_CONTINUE);
34858275SEric Cheng 
34868275SEric Cheng 	statep->mi_infop = &mip->mi_info;
34878275SEric Cheng 	return (MH_WALK_TERMINATE);
34888275SEric Cheng }
34898275SEric Cheng 
34908275SEric Cheng boolean_t
34918275SEric Cheng mac_info_get(const char *name, mac_info_t *minfop)
34928275SEric Cheng {
34938275SEric Cheng 	i_mac_info_state_t state;
34948275SEric Cheng 
34958275SEric Cheng 	rw_enter(&i_mac_impl_lock, RW_READER);
34968275SEric Cheng 	state.mi_name = name;
34978275SEric Cheng 	state.mi_infop = NULL;
34988275SEric Cheng 	mod_hash_walk(i_mac_impl_hash, i_mac_info_walker, &state);
34998275SEric Cheng 	if (state.mi_infop == NULL) {
35008275SEric Cheng 		rw_exit(&i_mac_impl_lock);
35018275SEric Cheng 		return (B_FALSE);
35028275SEric Cheng 	}
35038275SEric Cheng 	*minfop = *state.mi_infop;
35048275SEric Cheng 	rw_exit(&i_mac_impl_lock);
35058275SEric Cheng 	return (B_TRUE);
35068275SEric Cheng }
35078275SEric Cheng 
35088275SEric Cheng /*
35098275SEric Cheng  * To get the capabilities that MAC layer cares about, such as rings, factory
351010491SRishi.Srivatsavai@Sun.COM  * mac address, vnic or not, it should directly invoke this function.  If the
351110491SRishi.Srivatsavai@Sun.COM  * link is part of a bridge, then the only "capability" it has is the inability
351210491SRishi.Srivatsavai@Sun.COM  * to do zero copy.
35138275SEric Cheng  */
35148275SEric Cheng boolean_t
35158275SEric Cheng i_mac_capab_get(mac_handle_t mh, mac_capab_t cap, void *cap_data)
35168275SEric Cheng {
35178275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
35188275SEric Cheng 
351910491SRishi.Srivatsavai@Sun.COM 	if (mip->mi_bridge_link != NULL)
352010491SRishi.Srivatsavai@Sun.COM 		return (cap == MAC_CAPAB_NO_ZCOPY);
352110491SRishi.Srivatsavai@Sun.COM 	else if (mip->mi_callbacks->mc_callbacks & MC_GETCAPAB)
35228275SEric Cheng 		return (mip->mi_getcapab(mip->mi_driver, cap, cap_data));
35238275SEric Cheng 	else
35248275SEric Cheng 		return (B_FALSE);
35258275SEric Cheng }
35268275SEric Cheng 
35278275SEric Cheng /*
35288275SEric Cheng  * Capability query function. If number of active mac clients is greater than
35298275SEric Cheng  * 1, only limited capabilities can be advertised to the caller no matter the
35308275SEric Cheng  * driver has certain capability or not. Else, we query the driver to get the
35318275SEric Cheng  * capability.
35328275SEric Cheng  */
35338275SEric Cheng boolean_t
35348275SEric Cheng mac_capab_get(mac_handle_t mh, mac_capab_t cap, void *cap_data)
35358275SEric Cheng {
35368275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
35378275SEric Cheng 
35388275SEric Cheng 	/*
35399073SCathy.Zhou@Sun.COM 	 * if mi_nactiveclients > 1, only MAC_CAPAB_LEGACY, MAC_CAPAB_HCKSUM,
35409073SCathy.Zhou@Sun.COM 	 * MAC_CAPAB_NO_NATIVEVLAN and MAC_CAPAB_NO_ZCOPY can be advertised.
35418275SEric Cheng 	 */
35428275SEric Cheng 	if (mip->mi_nactiveclients > 1) {
35438275SEric Cheng 		switch (cap) {
35448275SEric Cheng 		case MAC_CAPAB_NO_NATIVEVLAN:
35458275SEric Cheng 		case MAC_CAPAB_NO_ZCOPY:
35468275SEric Cheng 			return (B_TRUE);
35479073SCathy.Zhou@Sun.COM 		case MAC_CAPAB_LEGACY:
35489073SCathy.Zhou@Sun.COM 		case MAC_CAPAB_HCKSUM:
35499073SCathy.Zhou@Sun.COM 			break;
35508275SEric Cheng 		default:
35518275SEric Cheng 			return (B_FALSE);
35528275SEric Cheng 		}
35538275SEric Cheng 	}
35548275SEric Cheng 
35558275SEric Cheng 	/* else get capab from driver */
35568275SEric Cheng 	return (i_mac_capab_get(mh, cap, cap_data));
35578275SEric Cheng }
35588275SEric Cheng 
35598275SEric Cheng boolean_t
35608275SEric Cheng mac_sap_verify(mac_handle_t mh, uint32_t sap, uint32_t *bind_sap)
35618275SEric Cheng {
35628275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
35638275SEric Cheng 
35648275SEric Cheng 	return (mip->mi_type->mt_ops.mtops_sap_verify(sap, bind_sap,
35658275SEric Cheng 	    mip->mi_pdata));
35668275SEric Cheng }
35678275SEric Cheng 
35688275SEric Cheng mblk_t *
35698275SEric Cheng mac_header(mac_handle_t mh, const uint8_t *daddr, uint32_t sap, mblk_t *payload,
35708275SEric Cheng     size_t extra_len)
35718275SEric Cheng {
357210616SSebastien.Roy@Sun.COM 	mac_impl_t	*mip = (mac_impl_t *)mh;
357310616SSebastien.Roy@Sun.COM 	const uint8_t	*hdr_daddr;
357410616SSebastien.Roy@Sun.COM 
357510616SSebastien.Roy@Sun.COM 	/*
357610616SSebastien.Roy@Sun.COM 	 * If the MAC is point-to-point with a fixed destination address, then
357710616SSebastien.Roy@Sun.COM 	 * we must always use that destination in the MAC header.
357810616SSebastien.Roy@Sun.COM 	 */
357910616SSebastien.Roy@Sun.COM 	hdr_daddr = (mip->mi_dstaddr_set ? mip->mi_dstaddr : daddr);
358010616SSebastien.Roy@Sun.COM 	return (mip->mi_type->mt_ops.mtops_header(mip->mi_addr, hdr_daddr, sap,
35818275SEric Cheng 	    mip->mi_pdata, payload, extra_len));
35828275SEric Cheng }
35838275SEric Cheng 
35848275SEric Cheng int
35858275SEric Cheng mac_header_info(mac_handle_t mh, mblk_t *mp, mac_header_info_t *mhip)
35868275SEric Cheng {
35878275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
35888275SEric Cheng 
35898275SEric Cheng 	return (mip->mi_type->mt_ops.mtops_header_info(mp, mip->mi_pdata,
35908275SEric Cheng 	    mhip));
35918275SEric Cheng }
35928275SEric Cheng 
359310734SEric Cheng int
359410734SEric Cheng mac_vlan_header_info(mac_handle_t mh, mblk_t *mp, mac_header_info_t *mhip)
359510734SEric Cheng {
359610734SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
359710734SEric Cheng 	boolean_t	is_ethernet = (mip->mi_info.mi_media == DL_ETHER);
359810734SEric Cheng 	int		err = 0;
359910734SEric Cheng 
360010734SEric Cheng 	/*
360110734SEric Cheng 	 * Packets should always be at least 16 bit aligned.
360210734SEric Cheng 	 */
360310734SEric Cheng 	ASSERT(IS_P2ALIGNED(mp->b_rptr, sizeof (uint16_t)));
360410734SEric Cheng 
360510734SEric Cheng 	if ((err = mac_header_info(mh, mp, mhip)) != 0)
360610734SEric Cheng 		return (err);
360710734SEric Cheng 
360810734SEric Cheng 	/*
360910734SEric Cheng 	 * If this is a VLAN-tagged Ethernet packet, then the SAP in the
361010734SEric Cheng 	 * mac_header_info_t as returned by mac_header_info() is
361110734SEric Cheng 	 * ETHERTYPE_VLAN. We need to grab the ethertype from the VLAN header.
361210734SEric Cheng 	 */
361310734SEric Cheng 	if (is_ethernet && (mhip->mhi_bindsap == ETHERTYPE_VLAN)) {
361410734SEric Cheng 		struct ether_vlan_header *evhp;
361510734SEric Cheng 		uint16_t sap;
361610734SEric Cheng 		mblk_t *tmp = NULL;
361710734SEric Cheng 		size_t size;
361810734SEric Cheng 
361910734SEric Cheng 		size = sizeof (struct ether_vlan_header);
362010734SEric Cheng 		if (MBLKL(mp) < size) {
362110734SEric Cheng 			/*
362210734SEric Cheng 			 * Pullup the message in order to get the MAC header
362310734SEric Cheng 			 * infomation. Note that this is a read-only function,
362410734SEric Cheng 			 * we keep the input packet intact.
362510734SEric Cheng 			 */
362610734SEric Cheng 			if ((tmp = msgpullup(mp, size)) == NULL)
362710734SEric Cheng 				return (EINVAL);
362810734SEric Cheng 
362910734SEric Cheng 			mp = tmp;
363010734SEric Cheng 		}
363110734SEric Cheng 		evhp = (struct ether_vlan_header *)mp->b_rptr;
363210734SEric Cheng 		sap = ntohs(evhp->ether_type);
363310734SEric Cheng 		(void) mac_sap_verify(mh, sap, &mhip->mhi_bindsap);
363410734SEric Cheng 		mhip->mhi_hdrsize = sizeof (struct ether_vlan_header);
363510734SEric Cheng 		mhip->mhi_tci = ntohs(evhp->ether_tci);
363610734SEric Cheng 		mhip->mhi_istagged = B_TRUE;
363710734SEric Cheng 		freemsg(tmp);
363810734SEric Cheng 
363910734SEric Cheng 		if (VLAN_CFI(mhip->mhi_tci) != ETHER_CFI)
364010734SEric Cheng 			return (EINVAL);
364110734SEric Cheng 	} else {
364210734SEric Cheng 		mhip->mhi_istagged = B_FALSE;
364310734SEric Cheng 		mhip->mhi_tci = 0;
364410734SEric Cheng 	}
364510734SEric Cheng 
364610734SEric Cheng 	return (0);
364710734SEric Cheng }
364810734SEric Cheng 
36498275SEric Cheng mblk_t *
36508275SEric Cheng mac_header_cook(mac_handle_t mh, mblk_t *mp)
36518275SEric Cheng {
36528275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
36538275SEric Cheng 
36548275SEric Cheng 	if (mip->mi_type->mt_ops.mtops_ops & MTOPS_HEADER_COOK) {
36558275SEric Cheng 		if (DB_REF(mp) > 1) {
36568275SEric Cheng 			mblk_t *newmp = copymsg(mp);
36578275SEric Cheng 			if (newmp == NULL)
36588275SEric Cheng 				return (NULL);
36598275SEric Cheng 			freemsg(mp);
36608275SEric Cheng 			mp = newmp;
36618275SEric Cheng 		}
36628275SEric Cheng 		return (mip->mi_type->mt_ops.mtops_header_cook(mp,
36638275SEric Cheng 		    mip->mi_pdata));
36648275SEric Cheng 	}
36658275SEric Cheng 	return (mp);
36668275SEric Cheng }
36678275SEric Cheng 
36688275SEric Cheng mblk_t *
36698275SEric Cheng mac_header_uncook(mac_handle_t mh, mblk_t *mp)
36708275SEric Cheng {
36718275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
36728275SEric Cheng 
36738275SEric Cheng 	if (mip->mi_type->mt_ops.mtops_ops & MTOPS_HEADER_UNCOOK) {
36748275SEric Cheng 		if (DB_REF(mp) > 1) {
36758275SEric Cheng 			mblk_t *newmp = copymsg(mp);
36768275SEric Cheng 			if (newmp == NULL)
36778275SEric Cheng 				return (NULL);
36788275SEric Cheng 			freemsg(mp);
36798275SEric Cheng 			mp = newmp;
36808275SEric Cheng 		}
36818275SEric Cheng 		return (mip->mi_type->mt_ops.mtops_header_uncook(mp,
36828275SEric Cheng 		    mip->mi_pdata));
36838275SEric Cheng 	}
36848275SEric Cheng 	return (mp);
36858275SEric Cheng }
36868275SEric Cheng 
36878275SEric Cheng uint_t
36888275SEric Cheng mac_addr_len(mac_handle_t mh)
36898275SEric Cheng {
36908275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
36918275SEric Cheng 
36928275SEric Cheng 	return (mip->mi_type->mt_addr_length);
36938275SEric Cheng }
36948275SEric Cheng 
36958275SEric Cheng /* True if a MAC is a VNIC */
36968275SEric Cheng boolean_t
36978275SEric Cheng mac_is_vnic(mac_handle_t mh)
36988275SEric Cheng {
36998275SEric Cheng 	return (((mac_impl_t *)mh)->mi_state_flags & MIS_IS_VNIC);
37008275SEric Cheng }
37018275SEric Cheng 
37028275SEric Cheng mac_handle_t
37038275SEric Cheng mac_get_lower_mac_handle(mac_handle_t mh)
37048275SEric Cheng {
37058275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
37068275SEric Cheng 
37078275SEric Cheng 	ASSERT(mac_is_vnic(mh));
37088275SEric Cheng 	return (((vnic_t *)mip->mi_driver)->vn_lower_mh);
37098275SEric Cheng }
37108275SEric Cheng 
37118275SEric Cheng void
37128275SEric Cheng mac_update_resources(mac_resource_props_t *nmrp, mac_resource_props_t *cmrp,
37138275SEric Cheng     boolean_t is_user_flow)
37148275SEric Cheng {
37158275SEric Cheng 	if (nmrp != NULL && cmrp != NULL) {
37168275SEric Cheng 		if (nmrp->mrp_mask & MRP_PRIORITY) {
37178275SEric Cheng 			if (nmrp->mrp_priority == MPL_RESET) {
37188275SEric Cheng 				cmrp->mrp_mask &= ~MRP_PRIORITY;
37198275SEric Cheng 				if (is_user_flow) {
37208275SEric Cheng 					cmrp->mrp_priority =
37218275SEric Cheng 					    MPL_SUBFLOW_DEFAULT;
37228275SEric Cheng 				} else {
37238275SEric Cheng 					cmrp->mrp_priority = MPL_LINK_DEFAULT;
37248275SEric Cheng 				}
37258275SEric Cheng 			} else {
37268275SEric Cheng 				cmrp->mrp_mask |= MRP_PRIORITY;
37278275SEric Cheng 				cmrp->mrp_priority = nmrp->mrp_priority;
37288275SEric Cheng 			}
37298275SEric Cheng 		}
37308275SEric Cheng 		if (nmrp->mrp_mask & MRP_MAXBW) {
37318275SEric Cheng 			cmrp->mrp_maxbw = nmrp->mrp_maxbw;
37328275SEric Cheng 			if (nmrp->mrp_maxbw == MRP_MAXBW_RESETVAL)
37338275SEric Cheng 				cmrp->mrp_mask &= ~MRP_MAXBW;
37348275SEric Cheng 			else
37358275SEric Cheng 				cmrp->mrp_mask |= MRP_MAXBW;
37368275SEric Cheng 		}
37378275SEric Cheng 		if (nmrp->mrp_mask & MRP_CPUS)
37388275SEric Cheng 			MAC_COPY_CPUS(nmrp, cmrp);
373910734SEric Cheng 
374010734SEric Cheng 		if (nmrp->mrp_mask & MRP_PROTECT)
374110734SEric Cheng 			mac_protect_update(nmrp, cmrp);
37428275SEric Cheng 	}
37438275SEric Cheng }
37448275SEric Cheng 
37458275SEric Cheng /*
37468275SEric Cheng  * i_mac_set_resources:
37478275SEric Cheng  *
37488275SEric Cheng  * This routine associates properties with the primary MAC client of
37498275SEric Cheng  * the specified MAC instance.
37508275SEric Cheng  * - Cache the properties in mac_impl_t
37518275SEric Cheng  * - Apply the properties to the primary MAC client if exists
37528275SEric Cheng  */
37538275SEric Cheng int
37548275SEric Cheng i_mac_set_resources(mac_handle_t mh, mac_resource_props_t *mrp)
37558275SEric Cheng {
37568275SEric Cheng 	mac_impl_t		*mip = (mac_impl_t *)mh;
37578275SEric Cheng 	mac_client_impl_t	*mcip;
37588275SEric Cheng 	int			err = 0;
37599073SCathy.Zhou@Sun.COM 	uint32_t		resmask, newresmask;
37609073SCathy.Zhou@Sun.COM 	mac_resource_props_t	tmrp, umrp;
37618275SEric Cheng 
37628275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
37638275SEric Cheng 
37648275SEric Cheng 	err = mac_validate_props(mrp);
37658275SEric Cheng 	if (err != 0)
37668275SEric Cheng 		return (err);
37678275SEric Cheng 
37689073SCathy.Zhou@Sun.COM 	bcopy(&mip->mi_resource_props, &umrp, sizeof (mac_resource_props_t));
37699073SCathy.Zhou@Sun.COM 	resmask = umrp.mrp_mask;
37709073SCathy.Zhou@Sun.COM 	mac_update_resources(mrp, &umrp, B_FALSE);
37719073SCathy.Zhou@Sun.COM 	newresmask = umrp.mrp_mask;
37729073SCathy.Zhou@Sun.COM 
37739073SCathy.Zhou@Sun.COM 	if (resmask == 0 && newresmask != 0) {
37749073SCathy.Zhou@Sun.COM 		/*
37759073SCathy.Zhou@Sun.COM 		 * Bandwidth, priority or cpu link properties configured,
37769073SCathy.Zhou@Sun.COM 		 * must disable fastpath.
37779073SCathy.Zhou@Sun.COM 		 */
37789073SCathy.Zhou@Sun.COM 		if ((err = mac_fastpath_disable((mac_handle_t)mip)) != 0)
37799073SCathy.Zhou@Sun.COM 			return (err);
37809073SCathy.Zhou@Sun.COM 	}
37819073SCathy.Zhou@Sun.COM 
37828275SEric Cheng 	/*
37838275SEric Cheng 	 * Since bind_cpu may be modified by mac_client_set_resources()
37848275SEric Cheng 	 * we use a copy of bind_cpu and finally cache bind_cpu in mip.
37858275SEric Cheng 	 * This allows us to cache only user edits in mip.
37868275SEric Cheng 	 */
37878275SEric Cheng 	bcopy(mrp, &tmrp, sizeof (mac_resource_props_t));
37888275SEric Cheng 	mcip = mac_primary_client_handle(mip);
37898833SVenu.Iyer@Sun.COM 	if (mcip != NULL && (mcip->mci_state_flags & MCIS_IS_AGGR_PORT) == 0) {
37908275SEric Cheng 		err =
37918275SEric Cheng 		    mac_client_set_resources((mac_client_handle_t)mcip, &tmrp);
37928275SEric Cheng 	}
37939073SCathy.Zhou@Sun.COM 
37949073SCathy.Zhou@Sun.COM 	/* Only update the values if mac_client_set_resources succeeded */
37959073SCathy.Zhou@Sun.COM 	if (err == 0) {
37969073SCathy.Zhou@Sun.COM 		bcopy(&umrp, &mip->mi_resource_props,
37979073SCathy.Zhou@Sun.COM 		    sizeof (mac_resource_props_t));
37989073SCathy.Zhou@Sun.COM 		/*
37999073SCathy.Zhou@Sun.COM 		 * If bankwidth, priority or cpu link properties cleared,
38009073SCathy.Zhou@Sun.COM 		 * renable fastpath.
38019073SCathy.Zhou@Sun.COM 		 */
38029073SCathy.Zhou@Sun.COM 		if (resmask != 0 && newresmask == 0)
38039073SCathy.Zhou@Sun.COM 			mac_fastpath_enable((mac_handle_t)mip);
38049073SCathy.Zhou@Sun.COM 	} else if (resmask == 0 && newresmask != 0) {
38059073SCathy.Zhou@Sun.COM 		mac_fastpath_enable((mac_handle_t)mip);
38069073SCathy.Zhou@Sun.COM 	}
38078275SEric Cheng 	return (err);
38088275SEric Cheng }
38098275SEric Cheng 
38108275SEric Cheng int
38118275SEric Cheng mac_set_resources(mac_handle_t mh, mac_resource_props_t *mrp)
38128275SEric Cheng {
38138275SEric Cheng 	int err;
38148275SEric Cheng 
38158275SEric Cheng 	i_mac_perim_enter((mac_impl_t *)mh);
38168275SEric Cheng 	err = i_mac_set_resources(mh, mrp);
38178275SEric Cheng 	i_mac_perim_exit((mac_impl_t *)mh);
38188275SEric Cheng 	return (err);
38198275SEric Cheng }
38208275SEric Cheng 
38218275SEric Cheng /*
38228275SEric Cheng  * Get the properties cached for the specified MAC instance.
38238275SEric Cheng  */
38248275SEric Cheng void
38258275SEric Cheng mac_get_resources(mac_handle_t mh, mac_resource_props_t *mrp)
38268275SEric Cheng {
38278275SEric Cheng 	mac_impl_t 		*mip = (mac_impl_t *)mh;
38288275SEric Cheng 	mac_client_impl_t	*mcip;
38298275SEric Cheng 
38308275SEric Cheng 	if (mip->mi_state_flags & MIS_IS_VNIC) {
38318275SEric Cheng 		mcip = mac_primary_client_handle(mip);
38328275SEric Cheng 		if (mcip != NULL) {
38338275SEric Cheng 			mac_client_get_resources((mac_client_handle_t)mcip,
38348275SEric Cheng 			    mrp);
38358275SEric Cheng 			return;
38368275SEric Cheng 		}
38378275SEric Cheng 	}
38388275SEric Cheng 	bcopy(&mip->mi_resource_props, mrp, sizeof (mac_resource_props_t));
38398275SEric Cheng }
38408275SEric Cheng 
384110491SRishi.Srivatsavai@Sun.COM int
384210491SRishi.Srivatsavai@Sun.COM mac_set_pvid(mac_handle_t mh, uint16_t pvid)
384310491SRishi.Srivatsavai@Sun.COM {
384410491SRishi.Srivatsavai@Sun.COM 	mac_impl_t *mip = (mac_impl_t *)mh;
384510491SRishi.Srivatsavai@Sun.COM 	mac_client_impl_t *mcip;
384610491SRishi.Srivatsavai@Sun.COM 	mac_unicast_impl_t *muip;
384710491SRishi.Srivatsavai@Sun.COM 
384810491SRishi.Srivatsavai@Sun.COM 	i_mac_perim_enter(mip);
384910491SRishi.Srivatsavai@Sun.COM 	if (pvid != 0) {
385010491SRishi.Srivatsavai@Sun.COM 		for (mcip = mip->mi_clients_list; mcip != NULL;
385110491SRishi.Srivatsavai@Sun.COM 		    mcip = mcip->mci_client_next) {
385210491SRishi.Srivatsavai@Sun.COM 			for (muip = mcip->mci_unicast_list; muip != NULL;
385310491SRishi.Srivatsavai@Sun.COM 			    muip = muip->mui_next) {
385410491SRishi.Srivatsavai@Sun.COM 				if (muip->mui_vid == pvid) {
385510491SRishi.Srivatsavai@Sun.COM 					i_mac_perim_exit(mip);
385610491SRishi.Srivatsavai@Sun.COM 					return (EBUSY);
385710491SRishi.Srivatsavai@Sun.COM 				}
385810491SRishi.Srivatsavai@Sun.COM 			}
385910491SRishi.Srivatsavai@Sun.COM 		}
386010491SRishi.Srivatsavai@Sun.COM 	}
386110491SRishi.Srivatsavai@Sun.COM 	mip->mi_pvid = pvid;
386210491SRishi.Srivatsavai@Sun.COM 	i_mac_perim_exit(mip);
386310491SRishi.Srivatsavai@Sun.COM 	return (0);
386410491SRishi.Srivatsavai@Sun.COM }
386510491SRishi.Srivatsavai@Sun.COM 
386610491SRishi.Srivatsavai@Sun.COM uint16_t
386710491SRishi.Srivatsavai@Sun.COM mac_get_pvid(mac_handle_t mh)
386810491SRishi.Srivatsavai@Sun.COM {
386910491SRishi.Srivatsavai@Sun.COM 	mac_impl_t *mip = (mac_impl_t *)mh;
387010491SRishi.Srivatsavai@Sun.COM 
387110491SRishi.Srivatsavai@Sun.COM 	return (mip->mi_pvid);
387210491SRishi.Srivatsavai@Sun.COM }
387310491SRishi.Srivatsavai@Sun.COM 
387410491SRishi.Srivatsavai@Sun.COM uint32_t
387510491SRishi.Srivatsavai@Sun.COM mac_get_llimit(mac_handle_t mh)
387610491SRishi.Srivatsavai@Sun.COM {
387710491SRishi.Srivatsavai@Sun.COM 	mac_impl_t *mip = (mac_impl_t *)mh;
387810491SRishi.Srivatsavai@Sun.COM 
387910491SRishi.Srivatsavai@Sun.COM 	return (mip->mi_llimit);
388010491SRishi.Srivatsavai@Sun.COM }
388110491SRishi.Srivatsavai@Sun.COM 
388210491SRishi.Srivatsavai@Sun.COM uint32_t
388310491SRishi.Srivatsavai@Sun.COM mac_get_ldecay(mac_handle_t mh)
388410491SRishi.Srivatsavai@Sun.COM {
388510491SRishi.Srivatsavai@Sun.COM 	mac_impl_t *mip = (mac_impl_t *)mh;
388610491SRishi.Srivatsavai@Sun.COM 
388710491SRishi.Srivatsavai@Sun.COM 	return (mip->mi_ldecay);
388810491SRishi.Srivatsavai@Sun.COM }
388910491SRishi.Srivatsavai@Sun.COM 
38908275SEric Cheng /*
38918275SEric Cheng  * Rename a mac client, its flow, and the kstat.
38928275SEric Cheng  */
38938275SEric Cheng int
38948275SEric Cheng mac_rename_primary(mac_handle_t mh, const char *new_name)
38958275SEric Cheng {
38968275SEric Cheng 	mac_impl_t		*mip = (mac_impl_t *)mh;
38978275SEric Cheng 	mac_client_impl_t	*cur_clnt = NULL;
38988275SEric Cheng 	flow_entry_t		*fep;
38998275SEric Cheng 
39008275SEric Cheng 	i_mac_perim_enter(mip);
39018275SEric Cheng 
39028275SEric Cheng 	/*
39038275SEric Cheng 	 * VNICs: we need to change the sys flow name and
39048275SEric Cheng 	 * the associated flow kstat.
39058275SEric Cheng 	 */
39068275SEric Cheng 	if (mip->mi_state_flags & MIS_IS_VNIC) {
39078275SEric Cheng 		ASSERT(new_name != NULL);
39088275SEric Cheng 		mac_rename_flow_names(mac_vnic_lower(mip), new_name);
39098275SEric Cheng 		goto done;
39108275SEric Cheng 	}
39118275SEric Cheng 	/*
39128275SEric Cheng 	 * This mac may itself be an aggr link, or it may have some client
39138275SEric Cheng 	 * which is an aggr port. For both cases, we need to change the
39148275SEric Cheng 	 * aggr port's mac client name, its flow name and the associated flow
39158275SEric Cheng 	 * kstat.
39168275SEric Cheng 	 */
39178275SEric Cheng 	if (mip->mi_state_flags & MIS_IS_AGGR) {
39188275SEric Cheng 		mac_capab_aggr_t aggr_cap;
39198275SEric Cheng 		mac_rename_fn_t rename_fn;
39208275SEric Cheng 		boolean_t ret;
39218275SEric Cheng 
39228275SEric Cheng 		ASSERT(new_name != NULL);
39238275SEric Cheng 		ret = i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_AGGR,
39248275SEric Cheng 		    (void *)(&aggr_cap));
39258275SEric Cheng 		ASSERT(ret == B_TRUE);
39268275SEric Cheng 		rename_fn = aggr_cap.mca_rename_fn;
39278275SEric Cheng 		rename_fn(new_name, mip->mi_driver);
39288275SEric Cheng 		/*
39298275SEric Cheng 		 * The aggr's client name and kstat flow name will be
39308275SEric Cheng 		 * updated below, i.e. via mac_rename_flow_names.
39318275SEric Cheng 		 */
39328275SEric Cheng 	}
39338275SEric Cheng 
39348275SEric Cheng 	for (cur_clnt = mip->mi_clients_list; cur_clnt != NULL;
39358275SEric Cheng 	    cur_clnt = cur_clnt->mci_client_next) {
39368275SEric Cheng 		if (cur_clnt->mci_state_flags & MCIS_IS_AGGR_PORT) {
39378275SEric Cheng 			if (new_name != NULL) {
39388275SEric Cheng 				char *str_st = cur_clnt->mci_name;
39398275SEric Cheng 				char *str_del = strchr(str_st, '-');
39408275SEric Cheng 
39418275SEric Cheng 				ASSERT(str_del != NULL);
39428275SEric Cheng 				bzero(str_del + 1, MAXNAMELEN -
39438275SEric Cheng 				    (str_del - str_st + 1));
39448275SEric Cheng 				bcopy(new_name, str_del + 1,
39458275SEric Cheng 				    strlen(new_name));
39468275SEric Cheng 			}
39478275SEric Cheng 			fep = cur_clnt->mci_flent;
39488275SEric Cheng 			mac_rename_flow(fep, cur_clnt->mci_name);
39498275SEric Cheng 			break;
39508275SEric Cheng 		} else if (new_name != NULL &&
39518275SEric Cheng 		    cur_clnt->mci_state_flags & MCIS_USE_DATALINK_NAME) {
39528275SEric Cheng 			mac_rename_flow_names(cur_clnt, new_name);
39538275SEric Cheng 			break;
39548275SEric Cheng 		}
39558275SEric Cheng 	}
39568275SEric Cheng 
39578275SEric Cheng done:
39588275SEric Cheng 	i_mac_perim_exit(mip);
39598275SEric Cheng 	return (0);
39608275SEric Cheng }
39618275SEric Cheng 
39628275SEric Cheng /*
39638275SEric Cheng  * Rename the MAC client's flow names
39648275SEric Cheng  */
39658275SEric Cheng static void
39668275SEric Cheng mac_rename_flow_names(mac_client_impl_t *mcip, const char *new_name)
39678275SEric Cheng {
39688275SEric Cheng 	flow_entry_t	*flent;
39698275SEric Cheng 	uint16_t	vid;
39708558SGirish.Moodalbail@Sun.COM 	char		flowname[MAXFLOWNAMELEN];
39718275SEric Cheng 	mac_impl_t	*mip = mcip->mci_mip;
39728275SEric Cheng 
39738275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
39748275SEric Cheng 
39758275SEric Cheng 	/*
39768275SEric Cheng 	 * Use mi_rw_lock to ensure that threads not in the mac perimeter
39778275SEric Cheng 	 * see a self-consistent value for mci_name
39788275SEric Cheng 	 */
39798275SEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
39808275SEric Cheng 	(void) strlcpy(mcip->mci_name, new_name, sizeof (mcip->mci_name));
39818275SEric Cheng 	rw_exit(&mip->mi_rw_lock);
39828275SEric Cheng 
39838275SEric Cheng 	mac_rename_flow(mcip->mci_flent, new_name);
39848275SEric Cheng 
39858275SEric Cheng 	if (mcip->mci_nflents == 1)
39868275SEric Cheng 		return;
39878275SEric Cheng 
39888275SEric Cheng 	/*
39898275SEric Cheng 	 * We have to rename all the others too, no stats to destroy for
39908275SEric Cheng 	 * these.
39918275SEric Cheng 	 */
39928275SEric Cheng 	for (flent = mcip->mci_flent_list; flent != NULL;
39938275SEric Cheng 	    flent = flent->fe_client_next) {
39948275SEric Cheng 		if (flent != mcip->mci_flent) {
39958275SEric Cheng 			vid = i_mac_flow_vid(flent);
39968275SEric Cheng 			(void) sprintf(flowname, "%s%u", new_name, vid);
39978275SEric Cheng 			mac_flow_set_name(flent, flowname);
39988275SEric Cheng 		}
39998275SEric Cheng 	}
40008275SEric Cheng }
40018275SEric Cheng 
40028275SEric Cheng 
40038275SEric Cheng /*
40048275SEric Cheng  * Add a flow to the MAC client's flow list - i.e list of MAC/VID tuples
40058275SEric Cheng  * defined for the specified MAC client.
40068275SEric Cheng  */
40078275SEric Cheng static void
40088275SEric Cheng mac_client_add_to_flow_list(mac_client_impl_t *mcip, flow_entry_t *flent)
40098275SEric Cheng {
40108275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
40118275SEric Cheng 	/*
40128275SEric Cheng 	 * The promisc Rx data path walks the mci_flent_list. Protect by
40138275SEric Cheng 	 * using mi_rw_lock
40148275SEric Cheng 	 */
40158275SEric Cheng 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
40168275SEric Cheng 
40178275SEric Cheng 	/* Add it to the head */
40188275SEric Cheng 	flent->fe_client_next = mcip->mci_flent_list;
40198275SEric Cheng 	mcip->mci_flent_list = flent;
40208275SEric Cheng 	mcip->mci_nflents++;
40218275SEric Cheng 
40228275SEric Cheng 	/*
40238275SEric Cheng 	 * Keep track of the number of non-zero VIDs addresses per MAC
40248275SEric Cheng 	 * client to avoid figuring it out in the data-path.
40258275SEric Cheng 	 */
40268275SEric Cheng 	if (i_mac_flow_vid(flent) != VLAN_ID_NONE)
40278275SEric Cheng 		mcip->mci_nvids++;
40288275SEric Cheng 
40298275SEric Cheng 	rw_exit(&mcip->mci_rw_lock);
40308275SEric Cheng }
40318275SEric Cheng 
40328275SEric Cheng /*
40338275SEric Cheng  * Remove a flow entry from the MAC client's list.
40348275SEric Cheng  */
40358275SEric Cheng static void
40368275SEric Cheng mac_client_remove_flow_from_list(mac_client_impl_t *mcip, flow_entry_t *flent)
40378275SEric Cheng {
40388275SEric Cheng 	flow_entry_t	*fe = mcip->mci_flent_list;
40398275SEric Cheng 	flow_entry_t	*prev_fe = NULL;
40408275SEric Cheng 
40418275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
40428275SEric Cheng 	/*
40438275SEric Cheng 	 * The promisc Rx data path walks the mci_flent_list. Protect by
40448275SEric Cheng 	 * using mci_rw_lock
40458275SEric Cheng 	 */
40468275SEric Cheng 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
40478275SEric Cheng 	while ((fe != NULL) && (fe != flent)) {
40488275SEric Cheng 		prev_fe = fe;
40498275SEric Cheng 		fe = fe->fe_client_next;
40508275SEric Cheng 	}
40518275SEric Cheng 
40528558SGirish.Moodalbail@Sun.COM 	ASSERT(fe != NULL);
40538558SGirish.Moodalbail@Sun.COM 	if (prev_fe == NULL) {
40548558SGirish.Moodalbail@Sun.COM 		/* Deleting the first node */
40558558SGirish.Moodalbail@Sun.COM 		mcip->mci_flent_list = fe->fe_client_next;
40568558SGirish.Moodalbail@Sun.COM 	} else {
40578558SGirish.Moodalbail@Sun.COM 		prev_fe->fe_client_next = fe->fe_client_next;
40588275SEric Cheng 	}
40598558SGirish.Moodalbail@Sun.COM 	mcip->mci_nflents--;
40608558SGirish.Moodalbail@Sun.COM 
40618558SGirish.Moodalbail@Sun.COM 	if (i_mac_flow_vid(flent) != VLAN_ID_NONE)
40628558SGirish.Moodalbail@Sun.COM 		mcip->mci_nvids--;
40638558SGirish.Moodalbail@Sun.COM 
40648275SEric Cheng 	rw_exit(&mcip->mci_rw_lock);
40658275SEric Cheng }
40668275SEric Cheng 
40678275SEric Cheng /*
40688275SEric Cheng  * Check if the given VID belongs to this MAC client.
40698275SEric Cheng  */
40708275SEric Cheng boolean_t
40718275SEric Cheng mac_client_check_flow_vid(mac_client_impl_t *mcip, uint16_t vid)
40728275SEric Cheng {
40738275SEric Cheng 	flow_entry_t	*flent;
40748275SEric Cheng 	uint16_t	mci_vid;
40758275SEric Cheng 
40768275SEric Cheng 	/* The mci_flent_list is protected by mci_rw_lock */
40778275SEric Cheng 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
40788275SEric Cheng 	for (flent = mcip->mci_flent_list; flent != NULL;
40798275SEric Cheng 	    flent = flent->fe_client_next) {
40808275SEric Cheng 		mci_vid = i_mac_flow_vid(flent);
40818275SEric Cheng 		if (vid == mci_vid) {
40828275SEric Cheng 			rw_exit(&mcip->mci_rw_lock);
40838275SEric Cheng 			return (B_TRUE);
40848275SEric Cheng 		}
40858275SEric Cheng 	}
40868275SEric Cheng 	rw_exit(&mcip->mci_rw_lock);
40878275SEric Cheng 	return (B_FALSE);
40888275SEric Cheng }
40898275SEric Cheng 
40908275SEric Cheng /*
40918275SEric Cheng  * Get the flow entry for the specified <MAC addr, VID> tuple.
40928275SEric Cheng  */
40938275SEric Cheng static flow_entry_t *
40948275SEric Cheng mac_client_get_flow(mac_client_impl_t *mcip, mac_unicast_impl_t *muip)
40958275SEric Cheng {
40968275SEric Cheng 	mac_address_t *map = mcip->mci_unicast;
40978275SEric Cheng 	flow_entry_t *flent;
40988275SEric Cheng 	uint16_t vid;
40998275SEric Cheng 	flow_desc_t flow_desc;
41008275SEric Cheng 
41018275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
41028275SEric Cheng 
41038275SEric Cheng 	mac_flow_get_desc(mcip->mci_flent, &flow_desc);
41048275SEric Cheng 	if (bcmp(flow_desc.fd_dst_mac, map->ma_addr, map->ma_len) != 0)
41058275SEric Cheng 		return (NULL);
41068275SEric Cheng 
41078275SEric Cheng 	for (flent = mcip->mci_flent_list; flent != NULL;
41088275SEric Cheng 	    flent = flent->fe_client_next) {
41098275SEric Cheng 		vid = i_mac_flow_vid(flent);
41108275SEric Cheng 		if (vid == muip->mui_vid) {
41118275SEric Cheng 			return (flent);
41128275SEric Cheng 		}
41138275SEric Cheng 	}
41148275SEric Cheng 
41158275SEric Cheng 	return (NULL);
41168275SEric Cheng }
41178275SEric Cheng 
41188275SEric Cheng /*
41198275SEric Cheng  * Since mci_flent has the SRSs, when we want to remove it, we replace
41208275SEric Cheng  * the flow_desc_t in mci_flent with that of an existing flent and then
41218275SEric Cheng  * remove that flent instead of mci_flent.
41228275SEric Cheng  */
41238275SEric Cheng static flow_entry_t *
41248275SEric Cheng mac_client_swap_mciflent(mac_client_impl_t *mcip)
41258275SEric Cheng {
41268275SEric Cheng 	flow_entry_t	*flent = mcip->mci_flent;
41278275SEric Cheng 	flow_tab_t	*ft = flent->fe_flow_tab;
41288275SEric Cheng 	flow_entry_t	*flent1;
41298275SEric Cheng 	flow_desc_t	fl_desc;
41308558SGirish.Moodalbail@Sun.COM 	char		fl_name[MAXFLOWNAMELEN];
41318275SEric Cheng 	int		err;
41328275SEric Cheng 
41338275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
41348275SEric Cheng 	ASSERT(mcip->mci_nflents > 1);
41358275SEric Cheng 
41368275SEric Cheng 	/* get the next flent following the primary flent  */
41378275SEric Cheng 	flent1 = mcip->mci_flent_list->fe_client_next;
41388275SEric Cheng 	ASSERT(flent1 != NULL && flent1->fe_flow_tab == ft);
41398275SEric Cheng 
41408275SEric Cheng 	/*
41418275SEric Cheng 	 * Remove the flent from the flow table before updating the
41428275SEric Cheng 	 * flow descriptor as the hash depends on the flow descriptor.
41438275SEric Cheng 	 * This also helps incoming packet classification avoid having
41448275SEric Cheng 	 * to grab fe_lock. Access to fe_flow_desc of a flent not in the
41458275SEric Cheng 	 * flow table is done under the fe_lock so that log or stat functions
41468275SEric Cheng 	 * see a self-consistent fe_flow_desc. The name and desc are specific
41478275SEric Cheng 	 * to a flow, the rest are shared by all the clients, including
41488275SEric Cheng 	 * resource control etc.
41498275SEric Cheng 	 */
41508275SEric Cheng 	mac_flow_remove(ft, flent, B_TRUE);
41518275SEric Cheng 	mac_flow_remove(ft, flent1, B_TRUE);
41528275SEric Cheng 
41538275SEric Cheng 	bcopy(&flent->fe_flow_desc, &fl_desc, sizeof (flow_desc_t));
41548558SGirish.Moodalbail@Sun.COM 	bcopy(flent->fe_flow_name, fl_name, MAXFLOWNAMELEN);
41558275SEric Cheng 
41568275SEric Cheng 	/* update the primary flow entry */
41578275SEric Cheng 	mutex_enter(&flent->fe_lock);
41588275SEric Cheng 	bcopy(&flent1->fe_flow_desc, &flent->fe_flow_desc,
41598275SEric Cheng 	    sizeof (flow_desc_t));
41608558SGirish.Moodalbail@Sun.COM 	bcopy(&flent1->fe_flow_name, &flent->fe_flow_name, MAXFLOWNAMELEN);
41618275SEric Cheng 	mutex_exit(&flent->fe_lock);
41628275SEric Cheng 
41638275SEric Cheng 	/* update the flow entry that is to be freed */
41648275SEric Cheng 	mutex_enter(&flent1->fe_lock);
41658275SEric Cheng 	bcopy(&fl_desc, &flent1->fe_flow_desc, sizeof (flow_desc_t));
41668558SGirish.Moodalbail@Sun.COM 	bcopy(fl_name, &flent1->fe_flow_name, MAXFLOWNAMELEN);
41678275SEric Cheng 	mutex_exit(&flent1->fe_lock);
41688275SEric Cheng 
41698275SEric Cheng 	/* now reinsert the flow entries in the table */
41708275SEric Cheng 	err = mac_flow_add(ft, flent);
41718275SEric Cheng 	ASSERT(err == 0);
41728275SEric Cheng 
41738275SEric Cheng 	err = mac_flow_add(ft, flent1);
41748275SEric Cheng 	ASSERT(err == 0);
41758275SEric Cheng 
41768275SEric Cheng 	return (flent1);
41778275SEric Cheng }
41788275SEric Cheng 
41798275SEric Cheng /*
41808275SEric Cheng  * Return whether there is only one flow entry associated with this
41818275SEric Cheng  * MAC client.
41828275SEric Cheng  */
41838275SEric Cheng static boolean_t
41848275SEric Cheng mac_client_single_rcvr(mac_client_impl_t *mcip)
41858275SEric Cheng {
41868275SEric Cheng 	return (mcip->mci_nflents == 1);
41878275SEric Cheng }
41888275SEric Cheng 
41898275SEric Cheng int
41908275SEric Cheng mac_validate_props(mac_resource_props_t *mrp)
41918275SEric Cheng {
41928275SEric Cheng 	if (mrp == NULL)
41938275SEric Cheng 		return (0);
41948275SEric Cheng 
41958275SEric Cheng 	if (mrp->mrp_mask & MRP_PRIORITY) {
41968275SEric Cheng 		mac_priority_level_t	pri = mrp->mrp_priority;
41978275SEric Cheng 
41988275SEric Cheng 		if (pri < MPL_LOW || pri > MPL_RESET)
41998275SEric Cheng 			return (EINVAL);
42008275SEric Cheng 	}
42018275SEric Cheng 
42028275SEric Cheng 	if (mrp->mrp_mask & MRP_MAXBW) {
42038275SEric Cheng 		uint64_t maxbw = mrp->mrp_maxbw;
42048275SEric Cheng 
42058275SEric Cheng 		if (maxbw < MRP_MAXBW_MINVAL && maxbw != 0)
42068275SEric Cheng 			return (EINVAL);
42078275SEric Cheng 	}
42088275SEric Cheng 	if (mrp->mrp_mask & MRP_CPUS) {
42099060SNitin.Hande@Sun.COM 		int i, j;
42108275SEric Cheng 		mac_cpu_mode_t	fanout;
42118275SEric Cheng 
42128275SEric Cheng 		if (mrp->mrp_ncpus > ncpus || mrp->mrp_ncpus > MAX_SR_FANOUT)
42138275SEric Cheng 			return (EINVAL);
42148275SEric Cheng 
42158275SEric Cheng 		for (i = 0; i < mrp->mrp_ncpus; i++) {
42169060SNitin.Hande@Sun.COM 			for (j = 0; j < mrp->mrp_ncpus; j++) {
42179060SNitin.Hande@Sun.COM 				if (i != j &&
42189060SNitin.Hande@Sun.COM 				    mrp->mrp_cpu[i] == mrp->mrp_cpu[j]) {
42199060SNitin.Hande@Sun.COM 					return (EINVAL);
42209060SNitin.Hande@Sun.COM 				}
42219060SNitin.Hande@Sun.COM 			}
42229060SNitin.Hande@Sun.COM 		}
42239060SNitin.Hande@Sun.COM 
42249060SNitin.Hande@Sun.COM 		for (i = 0; i < mrp->mrp_ncpus; i++) {
42258275SEric Cheng 			cpu_t *cp;
42268275SEric Cheng 			int rv;
42278275SEric Cheng 
42288275SEric Cheng 			mutex_enter(&cpu_lock);
42298275SEric Cheng 			cp = cpu_get(mrp->mrp_cpu[i]);
42308275SEric Cheng 			if (cp != NULL)
42318275SEric Cheng 				rv = cpu_is_online(cp);
42328275SEric Cheng 			else
42338275SEric Cheng 				rv = 0;
42348275SEric Cheng 			mutex_exit(&cpu_lock);
42358275SEric Cheng 			if (rv == 0)
42368275SEric Cheng 				return (EINVAL);
42378275SEric Cheng 		}
42388275SEric Cheng 
42398275SEric Cheng 		fanout = mrp->mrp_fanout_mode;
42408275SEric Cheng 		if (fanout < 0 || fanout > MCM_CPUS)
42418275SEric Cheng 			return (EINVAL);
42428275SEric Cheng 	}
424310734SEric Cheng 
424410734SEric Cheng 	if (mrp->mrp_mask & MRP_PROTECT) {
424510734SEric Cheng 		int err = mac_protect_validate(mrp);
424610734SEric Cheng 		if (err != 0)
424710734SEric Cheng 			return (err);
424810734SEric Cheng 	}
42498275SEric Cheng 	return (0);
42508275SEric Cheng }
42518275SEric Cheng 
42528275SEric Cheng /*
42538275SEric Cheng  * Send a MAC_NOTE_LINK notification to all the MAC clients whenever the
42548275SEric Cheng  * underlying physical link is down. This is to allow MAC clients to
42558275SEric Cheng  * communicate with other clients.
42568275SEric Cheng  */
42578275SEric Cheng void
42588275SEric Cheng mac_virtual_link_update(mac_impl_t *mip)
42598275SEric Cheng {
42608275SEric Cheng 	if (mip->mi_linkstate != LINK_STATE_UP)
42618275SEric Cheng 		i_mac_notify(mip, MAC_NOTE_LINK);
42628275SEric Cheng }
42638275SEric Cheng 
42648275SEric Cheng /*
42658275SEric Cheng  * For clients that have a pass-thru MAC, e.g. VNIC, we set the VNIC's
42668275SEric Cheng  * mac handle in the client.
42678275SEric Cheng  */
42688275SEric Cheng void
42698275SEric Cheng mac_set_upper_mac(mac_client_handle_t mch, mac_handle_t mh)
42708275SEric Cheng {
42718275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
42728275SEric Cheng 
42738275SEric Cheng 	mcip->mci_upper_mip = (mac_impl_t *)mh;
42748275SEric Cheng }
42758275SEric Cheng 
42768275SEric Cheng /*
42778275SEric Cheng  * Mark the mac as being used exclusively by the single mac client that is
42788275SEric Cheng  * doing some control operation on this mac. No further opens of this mac
42798275SEric Cheng  * will be allowed until this client calls mac_unmark_exclusive. The mac
42808275SEric Cheng  * client calling this function must already be in the mac perimeter
42818275SEric Cheng  */
42828275SEric Cheng int
42838275SEric Cheng mac_mark_exclusive(mac_handle_t mh)
42848275SEric Cheng {
42858275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
42868275SEric Cheng 
42878275SEric Cheng 	ASSERT(MAC_PERIM_HELD(mh));
42888275SEric Cheng 	/*
42898275SEric Cheng 	 * Look up its entry in the global hash table.
42908275SEric Cheng 	 */
42918275SEric Cheng 	rw_enter(&i_mac_impl_lock, RW_WRITER);
42928275SEric Cheng 	if (mip->mi_state_flags & MIS_DISABLED) {
42938275SEric Cheng 		rw_exit(&i_mac_impl_lock);
42948275SEric Cheng 		return (ENOENT);
42958275SEric Cheng 	}
42968275SEric Cheng 
42978275SEric Cheng 	/*
42988275SEric Cheng 	 * A reference to mac is held even if the link is not plumbed.
42998275SEric Cheng 	 * In i_dls_link_create() we open the MAC interface and hold the
43008275SEric Cheng 	 * reference. There is an additional reference for the mac_open
43018275SEric Cheng 	 * done in acquiring the mac perimeter
43028275SEric Cheng 	 */
43038275SEric Cheng 	if (mip->mi_ref != 2) {
43048275SEric Cheng 		rw_exit(&i_mac_impl_lock);
43058275SEric Cheng 		return (EBUSY);
43068275SEric Cheng 	}
43078275SEric Cheng 
43088275SEric Cheng 	ASSERT(!(mip->mi_state_flags & MIS_EXCLUSIVE_HELD));
43098275SEric Cheng 	mip->mi_state_flags |= MIS_EXCLUSIVE_HELD;
43108275SEric Cheng 	rw_exit(&i_mac_impl_lock);
43118275SEric Cheng 	return (0);
43128275SEric Cheng }
43138275SEric Cheng 
43148275SEric Cheng void
43158275SEric Cheng mac_unmark_exclusive(mac_handle_t mh)
43168275SEric Cheng {
43178275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
43188275SEric Cheng 
43198275SEric Cheng 	ASSERT(MAC_PERIM_HELD(mh));
43208275SEric Cheng 
43218275SEric Cheng 	rw_enter(&i_mac_impl_lock, RW_WRITER);
43228275SEric Cheng 	/* 1 for the creation and another for the perimeter */
43238275SEric Cheng 	ASSERT(mip->mi_ref == 2 && (mip->mi_state_flags & MIS_EXCLUSIVE_HELD));
43248275SEric Cheng 	mip->mi_state_flags &= ~MIS_EXCLUSIVE_HELD;
43258275SEric Cheng 	rw_exit(&i_mac_impl_lock);
43268275SEric Cheng }
43278275SEric Cheng 
43288275SEric Cheng /*
432910616SSebastien.Roy@Sun.COM  * Set the MTU for the specified MAC.  Note that this mechanism depends on
433010616SSebastien.Roy@Sun.COM  * the driver calling mac_maxsdu_update() to update the link MTU if it was
433110616SSebastien.Roy@Sun.COM  * successful in setting its MTU.
433210616SSebastien.Roy@Sun.COM  *
433310616SSebastien.Roy@Sun.COM  * Note that there is potential for improvement here.  A better model might be
433410616SSebastien.Roy@Sun.COM  * to not require drivers to call mac_maxsdu_update(), but rather have this
433510616SSebastien.Roy@Sun.COM  * function update mi_sdu_max and send notifications if the driver setprop
433610616SSebastien.Roy@Sun.COM  * callback succeeds.  This would remove the burden and complexity from
433710616SSebastien.Roy@Sun.COM  * drivers.
43388275SEric Cheng  */
43398275SEric Cheng int
43408275SEric Cheng mac_set_mtu(mac_handle_t mh, uint_t new_mtu, uint_t *old_mtu_arg)
43418275SEric Cheng {
43428275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
43438275SEric Cheng 	uint_t old_mtu;
434410674SSebastien.Roy@Sun.COM 	int rv = 0;
43458275SEric Cheng 
43468275SEric Cheng 	i_mac_perim_enter(mip);
43478275SEric Cheng 
434810616SSebastien.Roy@Sun.COM 	if (!(mip->mi_callbacks->mc_callbacks & (MC_SETPROP|MC_GETPROP))) {
43498275SEric Cheng 		rv = ENOTSUP;
43508275SEric Cheng 		goto bail;
43518275SEric Cheng 	}
43528275SEric Cheng 
435310616SSebastien.Roy@Sun.COM 	old_mtu = mip->mi_sdu_max;
43548275SEric Cheng 
43558275SEric Cheng 	if (old_mtu != new_mtu) {
43568275SEric Cheng 		rv = mip->mi_callbacks->mc_setprop(mip->mi_driver,
43578275SEric Cheng 		    "mtu", MAC_PROP_MTU, sizeof (uint_t), &new_mtu);
43588275SEric Cheng 	}
43598275SEric Cheng 
43608275SEric Cheng bail:
43618275SEric Cheng 	i_mac_perim_exit(mip);
43628275SEric Cheng 
43638275SEric Cheng 	if (rv == 0 && old_mtu_arg != NULL)
43648275SEric Cheng 		*old_mtu_arg = old_mtu;
43658275SEric Cheng 	return (rv);
43668275SEric Cheng }
43678275SEric Cheng 
43688275SEric Cheng void
43698275SEric Cheng mac_get_hwgrp_info(mac_handle_t mh, int grp_index, uint_t *grp_num,
43708275SEric Cheng     uint_t *n_rings, uint_t *type, uint_t *n_clnts, char *clnts_name)
43718275SEric Cheng {
43728275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
43738275SEric Cheng 	mac_grp_client_t *mcip;
43748275SEric Cheng 	uint_t i = 0, index = 0;
43758275SEric Cheng 
43768275SEric Cheng 	/* Revisit when we implement fully dynamic group allocation */
43778275SEric Cheng 	ASSERT(grp_index >= 0 && grp_index < mip->mi_rx_group_count);
43788275SEric Cheng 
43798275SEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_READER);
43808275SEric Cheng 	*grp_num = mip->mi_rx_groups[grp_index].mrg_index;
43818275SEric Cheng 	*type = mip->mi_rx_groups[grp_index].mrg_type;
43828275SEric Cheng 	*n_rings = mip->mi_rx_groups[grp_index].mrg_cur_count;
43838275SEric Cheng 	for (mcip = mip->mi_rx_groups[grp_index].mrg_clients; mcip != NULL;
43848275SEric Cheng 	    mcip = mcip->mgc_next) {
43858275SEric Cheng 		int name_len = strlen(mcip->mgc_client->mci_name);
43868275SEric Cheng 
43878275SEric Cheng 		/*
43888275SEric Cheng 		 * MAXCLIENTNAMELEN is the buffer size reserved for client
43898275SEric Cheng 		 * names.
43908275SEric Cheng 		 * XXXX Formating the client name string needs to be moved
43918275SEric Cheng 		 * to user land when fixing the size of dhi_clnts in
43928275SEric Cheng 		 * dld_hwgrpinfo_t. We should use n_clients * client_name for
43938275SEric Cheng 		 * dhi_clntsin instead of MAXCLIENTNAMELEN
43948275SEric Cheng 		 */
43958275SEric Cheng 		if (index + name_len >= MAXCLIENTNAMELEN) {
43968275SEric Cheng 			index = MAXCLIENTNAMELEN;
43978275SEric Cheng 			break;
43988275SEric Cheng 		}
43998275SEric Cheng 		bcopy(mcip->mgc_client->mci_name, &(clnts_name[index]),
44008275SEric Cheng 		    name_len);
44018275SEric Cheng 		index += name_len;
44028275SEric Cheng 		clnts_name[index++] = ',';
44038275SEric Cheng 		i++;
44048275SEric Cheng 	}
44058275SEric Cheng 
44068275SEric Cheng 	/* Get rid of the last , */
44078275SEric Cheng 	if (index > 0)
44088275SEric Cheng 		clnts_name[index - 1] = '\0';
44098275SEric Cheng 	*n_clnts = i;
44108275SEric Cheng 	rw_exit(&mip->mi_rw_lock);
44118275SEric Cheng }
44128275SEric Cheng 
44138275SEric Cheng uint_t
44148275SEric Cheng mac_hwgrp_num(mac_handle_t mh)
44158275SEric Cheng {
44168275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
44178275SEric Cheng 
44188275SEric Cheng 	return (mip->mi_rx_group_count);
44198275SEric Cheng }
4420