xref: /onnv-gate/usr/src/uts/common/io/mac/mac_client.c (revision 8833:8adf20bc60e3)
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 /*
238558SGirish.Moodalbail@Sun.COM  * Copyright 2009 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 
4138275SEric Cheng const char *
4148275SEric Cheng mac_name(mac_handle_t mh)
4158275SEric Cheng {
4168275SEric Cheng 	return (((mac_impl_t *)mh)->mi_name);
4178275SEric Cheng }
4188275SEric Cheng 
4198275SEric Cheng char *
4208275SEric Cheng mac_client_name(mac_client_handle_t mch)
4218275SEric Cheng {
4228275SEric Cheng 	return (((mac_client_impl_t *)mch)->mci_name);
4238275SEric Cheng }
4248275SEric Cheng 
4258275SEric Cheng minor_t
4268275SEric Cheng mac_minor(mac_handle_t mh)
4278275SEric Cheng {
4288275SEric Cheng 	return (((mac_impl_t *)mh)->mi_minor);
4298275SEric Cheng }
4308275SEric Cheng 
4318275SEric Cheng /*
4328275SEric Cheng  * Return the VID associated with a MAC client. This function should
4338275SEric Cheng  * be called for clients which are associated with only one VID.
4348275SEric Cheng  */
4358275SEric Cheng uint16_t
4368275SEric Cheng mac_client_vid(mac_client_handle_t mch)
4378275SEric Cheng {
4388275SEric Cheng 	uint16_t		vid = VLAN_ID_NONE;
4398275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
4408275SEric Cheng 	flow_desc_t		flow_desc;
4418275SEric Cheng 
4428275SEric Cheng 	if (mcip->mci_nflents == 0)
4438275SEric Cheng 		return (vid);
4448275SEric Cheng 
4458275SEric Cheng 	ASSERT(MCIP_DATAPATH_SETUP(mcip) && mac_client_single_rcvr(mcip));
4468275SEric Cheng 
4478275SEric Cheng 	mac_flow_get_desc(mcip->mci_flent, &flow_desc);
4488275SEric Cheng 	if ((flow_desc.fd_mask & FLOW_LINK_VID) != 0)
4498275SEric Cheng 		vid = flow_desc.fd_vid;
4508275SEric Cheng 
4518275SEric Cheng 	return (vid);
4528275SEric Cheng }
4538275SEric Cheng 
4548275SEric Cheng /*
4558275SEric Cheng  * Return the link speed associated with the specified MAC client.
4568275SEric Cheng  *
4578275SEric Cheng  * The link speed of a MAC client is equal to the smallest value of
4588275SEric Cheng  * 1) the current link speed of the underlying NIC, or
4598275SEric Cheng  * 2) the bandwidth limit set for the MAC client.
4608275SEric Cheng  *
4618275SEric Cheng  * Note that the bandwidth limit can be higher than the speed
4628275SEric Cheng  * of the underlying NIC. This is allowed to avoid spurious
4638275SEric Cheng  * administration action failures or artifically lowering the
4648275SEric Cheng  * bandwidth limit of a link that may  have temporarily lowered
4658275SEric Cheng  * its link speed due to hardware problem or administrator action.
4668275SEric Cheng  */
4678275SEric Cheng static uint64_t
4688275SEric Cheng mac_client_ifspeed(mac_client_impl_t *mcip)
4698275SEric Cheng {
4708275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
4718275SEric Cheng 	uint64_t nic_speed;
4728275SEric Cheng 
4738275SEric Cheng 	nic_speed = mac_stat_get((mac_handle_t)mip, MAC_STAT_IFSPEED);
4748275SEric Cheng 
4758275SEric Cheng 	if (nic_speed == 0) {
4768275SEric Cheng 		return (0);
4778275SEric Cheng 	} else {
4788275SEric Cheng 		uint64_t policy_limit = (uint64_t)-1;
4798275SEric Cheng 
4808275SEric Cheng 		if (MCIP_RESOURCE_PROPS_MASK(mcip) & MRP_MAXBW)
4818275SEric Cheng 			policy_limit = MCIP_RESOURCE_PROPS_MAXBW(mcip);
4828275SEric Cheng 
4838275SEric Cheng 		return (MIN(policy_limit, nic_speed));
4848275SEric Cheng 	}
4858275SEric Cheng }
4868275SEric Cheng 
4878275SEric Cheng /*
4888275SEric Cheng  * Return the link state of the specified client. If here are more
4898275SEric Cheng  * than one clients of the underying mac_impl_t, the link state
4908275SEric Cheng  * will always be UP regardless of the link state of the underlying
4918275SEric Cheng  * mac_impl_t. This is needed to allow the MAC clients to continue
4928275SEric Cheng  * to communicate with each other even when the physical link of
4938275SEric Cheng  * their mac_impl_t is down.
4948275SEric Cheng  */
4958275SEric Cheng static uint64_t
4968275SEric Cheng mac_client_link_state(mac_client_impl_t *mcip)
4978275SEric Cheng {
4988275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
4998275SEric Cheng 	uint16_t vid;
5008275SEric Cheng 	mac_client_impl_t *mci_list;
5018275SEric Cheng 	mac_unicast_impl_t *mui_list, *oth_mui_list;
5028275SEric Cheng 
5038275SEric Cheng 	/*
5048275SEric Cheng 	 * Returns LINK_STATE_UP if there are other MAC clients defined on
5058275SEric Cheng 	 * mac_impl_t which share same VLAN ID as that of mcip. Note that
5068275SEric Cheng 	 * if 'mcip' has more than one VID's then we match ANY one of the
5078275SEric Cheng 	 * VID's with other MAC client's VID's and return LINK_STATE_UP.
5088275SEric Cheng 	 */
5098275SEric Cheng 	rw_enter(&mcip->mci_rw_lock, RW_READER);
5108275SEric Cheng 	for (mui_list = mcip->mci_unicast_list; mui_list != NULL;
5118275SEric Cheng 	    mui_list = mui_list->mui_next) {
5128275SEric Cheng 		vid = mui_list->mui_vid;
5138275SEric Cheng 		for (mci_list = mip->mi_clients_list; mci_list != NULL;
5148275SEric Cheng 		    mci_list = mci_list->mci_client_next) {
5158275SEric Cheng 			if (mci_list == mcip)
5168275SEric Cheng 				continue;
5178275SEric Cheng 			for (oth_mui_list = mci_list->mci_unicast_list;
5188275SEric Cheng 			    oth_mui_list != NULL; oth_mui_list = oth_mui_list->
5198275SEric Cheng 			    mui_next) {
5208275SEric Cheng 				if (vid == oth_mui_list->mui_vid) {
5218275SEric Cheng 					rw_exit(&mcip->mci_rw_lock);
5228275SEric Cheng 					return (LINK_STATE_UP);
5238275SEric Cheng 				}
5248275SEric Cheng 			}
5258275SEric Cheng 		}
5268275SEric Cheng 	}
5278275SEric Cheng 	rw_exit(&mcip->mci_rw_lock);
5288275SEric Cheng 
5298275SEric Cheng 	return (mac_stat_get((mac_handle_t)mip, MAC_STAT_LINK_STATE));
5308275SEric Cheng }
5318275SEric Cheng 
5328275SEric Cheng /*
5338275SEric Cheng  * Return the statistics of a MAC client. These statistics are different
5348275SEric Cheng  * then the statistics of the underlying MAC which are returned by
5358275SEric Cheng  * mac_stat_get().
5368275SEric Cheng  */
5378275SEric Cheng uint64_t
5388275SEric Cheng mac_client_stat_get(mac_client_handle_t mch, uint_t stat)
5398275SEric Cheng {
5408275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
5418275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
5428275SEric Cheng 	uint64_t val;
5438275SEric Cheng 
5448275SEric Cheng 	switch (stat) {
5458275SEric Cheng 	case MAC_STAT_LINK_STATE:
5468275SEric Cheng 		val = mac_client_link_state(mcip);
5478275SEric Cheng 		break;
5488275SEric Cheng 	case MAC_STAT_LINK_UP:
5498275SEric Cheng 		val = (mac_client_link_state(mcip) == LINK_STATE_UP);
5508275SEric Cheng 		break;
5518275SEric Cheng 	case MAC_STAT_PROMISC:
5528275SEric Cheng 		val = mac_stat_get((mac_handle_t)mip, MAC_STAT_PROMISC);
5538275SEric Cheng 		break;
5548275SEric Cheng 	case MAC_STAT_IFSPEED:
5558275SEric Cheng 		val = mac_client_ifspeed(mcip);
5568275SEric Cheng 		break;
5578275SEric Cheng 	case MAC_STAT_MULTIRCV:
5588275SEric Cheng 		val = mcip->mci_stat_multircv;
5598275SEric Cheng 		break;
5608275SEric Cheng 	case MAC_STAT_BRDCSTRCV:
5618275SEric Cheng 		val = mcip->mci_stat_brdcstrcv;
5628275SEric Cheng 		break;
5638275SEric Cheng 	case MAC_STAT_MULTIXMT:
5648275SEric Cheng 		val = mcip->mci_stat_multixmt;
5658275SEric Cheng 		break;
5668275SEric Cheng 	case MAC_STAT_BRDCSTXMT:
5678275SEric Cheng 		val = mcip->mci_stat_brdcstxmt;
5688275SEric Cheng 		break;
5698275SEric Cheng 	case MAC_STAT_OBYTES:
5708275SEric Cheng 		val = mcip->mci_stat_obytes;
5718275SEric Cheng 		break;
5728275SEric Cheng 	case MAC_STAT_OPACKETS:
5738275SEric Cheng 		val = mcip->mci_stat_opackets;
5748275SEric Cheng 		break;
5758275SEric Cheng 	case MAC_STAT_OERRORS:
5768275SEric Cheng 		val = mcip->mci_stat_oerrors;
5778275SEric Cheng 		break;
5788275SEric Cheng 	case MAC_STAT_IPACKETS:
5798275SEric Cheng 		val = mcip->mci_stat_ipackets;
5808275SEric Cheng 		break;
5818275SEric Cheng 	case MAC_STAT_RBYTES:
5828275SEric Cheng 		val = mcip->mci_stat_ibytes;
5838275SEric Cheng 		break;
5848275SEric Cheng 	case MAC_STAT_IERRORS:
5858275SEric Cheng 		val = mcip->mci_stat_ierrors;
5868275SEric Cheng 		break;
5878275SEric Cheng 	default:
5888275SEric Cheng 		val = mac_stat_default(mip, stat);
5898275SEric Cheng 		break;
5908275SEric Cheng 	}
5918275SEric Cheng 
5928275SEric Cheng 	return (val);
5938275SEric Cheng }
5948275SEric Cheng 
5958275SEric Cheng /*
5968275SEric Cheng  * Return the statistics of the specified MAC instance.
5978275SEric Cheng  */
5988275SEric Cheng uint64_t
5998275SEric Cheng mac_stat_get(mac_handle_t mh, uint_t stat)
6008275SEric Cheng {
6018275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
6028275SEric Cheng 	uint64_t	val;
6038275SEric Cheng 	int		ret;
6048275SEric Cheng 
6058275SEric Cheng 	/*
6068275SEric Cheng 	 * The range of stat determines where it is maintained.  Stat
6078275SEric Cheng 	 * values from 0 up to (but not including) MAC_STAT_MIN are
6088275SEric Cheng 	 * mainteined by the mac module itself.  Everything else is
6098275SEric Cheng 	 * maintained by the driver.
6108275SEric Cheng 	 *
6118275SEric Cheng 	 * If the mac_impl_t being queried corresponds to a VNIC,
6128275SEric Cheng 	 * the stats need to be queried from the lower MAC client
6138275SEric Cheng 	 * corresponding to the VNIC. (The mac_link_update()
6148275SEric Cheng 	 * invoked by the driver to the lower MAC causes the *lower
6158275SEric Cheng 	 * MAC* to update its mi_linkstate, and send a notification
6168275SEric Cheng 	 * to its MAC clients. Due to the VNIC passthrough,
6178275SEric Cheng 	 * these notifications are sent to the upper MAC clients
6188275SEric Cheng 	 * of the VNIC directly, and the upper mac_impl_t of the VNIC
6198275SEric Cheng 	 * does not have a valid mi_linkstate.
6208275SEric Cheng 	 */
6218275SEric Cheng 	if (stat < MAC_STAT_MIN && !(mip->mi_state_flags & MIS_IS_VNIC)) {
6228275SEric Cheng 		/* these stats are maintained by the mac module itself */
6238275SEric Cheng 		switch (stat) {
6248275SEric Cheng 		case MAC_STAT_LINK_STATE:
6258275SEric Cheng 			return (mip->mi_linkstate);
6268275SEric Cheng 		case MAC_STAT_LINK_UP:
6278275SEric Cheng 			return (mip->mi_linkstate == LINK_STATE_UP);
6288275SEric Cheng 		case MAC_STAT_PROMISC:
6298275SEric Cheng 			return (mip->mi_devpromisc != 0);
6308275SEric Cheng 		default:
6318275SEric Cheng 			ASSERT(B_FALSE);
6328275SEric Cheng 		}
6338275SEric Cheng 	}
6348275SEric Cheng 
6358275SEric Cheng 	/*
6368275SEric Cheng 	 * Call the driver to get the given statistic.
6378275SEric Cheng 	 */
6388275SEric Cheng 	ret = mip->mi_getstat(mip->mi_driver, stat, &val);
6398275SEric Cheng 	if (ret != 0) {
6408275SEric Cheng 		/*
6418275SEric Cheng 		 * The driver doesn't support this statistic.  Get the
6428275SEric Cheng 		 * statistic's default value.
6438275SEric Cheng 		 */
6448275SEric Cheng 		val = mac_stat_default(mip, stat);
6458275SEric Cheng 	}
6468275SEric Cheng 	return (val);
6478275SEric Cheng }
6488275SEric Cheng 
6498275SEric Cheng /*
6508275SEric Cheng  * Utility function which returns the VID associated with a flow entry.
6518275SEric Cheng  */
6528275SEric Cheng uint16_t
6538275SEric Cheng i_mac_flow_vid(flow_entry_t *flent)
6548275SEric Cheng {
6558275SEric Cheng 	flow_desc_t	flow_desc;
6568275SEric Cheng 
6578275SEric Cheng 	mac_flow_get_desc(flent, &flow_desc);
6588275SEric Cheng 
6598275SEric Cheng 	if ((flow_desc.fd_mask & FLOW_LINK_VID) != 0)
6608275SEric Cheng 		return (flow_desc.fd_vid);
6618275SEric Cheng 	return (VLAN_ID_NONE);
6628275SEric Cheng }
6638275SEric Cheng 
6648275SEric Cheng /*
6658275SEric Cheng  * Verify the validity of the specified unicast MAC address. Returns B_TRUE
6668275SEric Cheng  * if the address is valid, B_FALSE otherwise (multicast address, or incorrect
6678275SEric Cheng  * length.
6688275SEric Cheng  */
6698275SEric Cheng boolean_t
6708275SEric Cheng mac_unicst_verify(mac_handle_t mh, const uint8_t *addr, uint_t len)
6718275SEric Cheng {
6728275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
6738275SEric Cheng 
6748275SEric Cheng 	/*
6758275SEric Cheng 	 * Verify the address. No lock is needed since mi_type and plugin
6768275SEric Cheng 	 * details don't change after mac_register().
6778275SEric Cheng 	 */
6788275SEric Cheng 	if ((len != mip->mi_type->mt_addr_length) ||
6798275SEric Cheng 	    (mip->mi_type->mt_ops.mtops_unicst_verify(addr,
6808275SEric Cheng 	    mip->mi_pdata)) != 0) {
6818275SEric Cheng 		return (B_FALSE);
6828275SEric Cheng 	} else {
6838275SEric Cheng 		return (B_TRUE);
6848275SEric Cheng 	}
6858275SEric Cheng }
6868275SEric Cheng 
6878275SEric Cheng void
6888275SEric Cheng mac_sdu_get(mac_handle_t mh, uint_t *min_sdu, uint_t *max_sdu)
6898275SEric Cheng {
6908275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
6918275SEric Cheng 
6928275SEric Cheng 	if (min_sdu != NULL)
6938275SEric Cheng 		*min_sdu = mip->mi_sdu_min;
6948275SEric Cheng 	if (max_sdu != NULL)
6958275SEric Cheng 		*max_sdu = mip->mi_sdu_max;
6968275SEric Cheng }
6978275SEric Cheng 
6988275SEric Cheng /*
6998275SEric Cheng  * Update the MAC unicast address of the specified client's flows. Currently
7008275SEric Cheng  * only one unicast MAC unicast address is allowed per client.
7018275SEric Cheng  */
7028275SEric Cheng static void
7038275SEric Cheng mac_unicast_update_client_flow(mac_client_impl_t *mcip)
7048275SEric Cheng {
7058275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
7068275SEric Cheng 	flow_entry_t *flent = mcip->mci_flent;
7078275SEric Cheng 	mac_address_t *map = mcip->mci_unicast;
7088275SEric Cheng 	flow_desc_t flow_desc;
7098275SEric Cheng 
7108275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
7118275SEric Cheng 	ASSERT(flent != NULL);
7128275SEric Cheng 
7138275SEric Cheng 	mac_flow_get_desc(flent, &flow_desc);
7148275SEric Cheng 	ASSERT(flow_desc.fd_mask & FLOW_LINK_DST);
7158275SEric Cheng 
7168275SEric Cheng 	bcopy(map->ma_addr, flow_desc.fd_dst_mac, map->ma_len);
7178275SEric Cheng 	mac_flow_set_desc(flent, &flow_desc);
7188275SEric Cheng 
7198275SEric Cheng 	/*
7208275SEric Cheng 	 * A MAC client could have one MAC address but multiple
7218275SEric Cheng 	 * VLANs. In that case update the flow entries corresponding
7228275SEric Cheng 	 * to all VLANs of the MAC client.
7238275SEric Cheng 	 */
7248275SEric Cheng 	for (flent = mcip->mci_flent_list; flent != NULL;
7258275SEric Cheng 	    flent = flent->fe_client_next) {
7268275SEric Cheng 		mac_flow_get_desc(flent, &flow_desc);
7278275SEric Cheng 		if (!(flent->fe_type & FLOW_PRIMARY_MAC ||
7288275SEric Cheng 		    flent->fe_type & FLOW_VNIC_MAC))
7298275SEric Cheng 			continue;
7308275SEric Cheng 
7318275SEric Cheng 		bcopy(map->ma_addr, flow_desc.fd_dst_mac, map->ma_len);
7328275SEric Cheng 		mac_flow_set_desc(flent, &flow_desc);
7338275SEric Cheng 	}
7348275SEric Cheng }
7358275SEric Cheng 
7368275SEric Cheng /*
7378275SEric Cheng  * Update all clients that share the same unicast address.
7388275SEric Cheng  */
7398275SEric Cheng void
7408275SEric Cheng mac_unicast_update_clients(mac_impl_t *mip, mac_address_t *map)
7418275SEric Cheng {
7428275SEric Cheng 	mac_client_impl_t *mcip;
7438275SEric Cheng 
7448275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
7458275SEric Cheng 
7468275SEric Cheng 	/*
7478275SEric Cheng 	 * Find all clients that share the same unicast MAC address and update
7488275SEric Cheng 	 * them appropriately.
7498275SEric Cheng 	 */
7508275SEric Cheng 	for (mcip = mip->mi_clients_list; mcip != NULL;
7518275SEric Cheng 	    mcip = mcip->mci_client_next) {
7528275SEric Cheng 		/*
7538275SEric Cheng 		 * Ignore clients that don't share this MAC address.
7548275SEric Cheng 		 */
7558275SEric Cheng 		if (map != mcip->mci_unicast)
7568275SEric Cheng 			continue;
7578275SEric Cheng 
7588275SEric Cheng 		/*
7598275SEric Cheng 		 * Update those clients with same old unicast MAC address.
7608275SEric Cheng 		 */
7618275SEric Cheng 		mac_unicast_update_client_flow(mcip);
7628275SEric Cheng 	}
7638275SEric Cheng }
7648275SEric Cheng 
7658275SEric Cheng /*
7668275SEric Cheng  * Update the unicast MAC address of the specified VNIC MAC client.
7678275SEric Cheng  *
7688275SEric Cheng  * Check whether the operation is valid. Any of following cases should fail:
7698275SEric Cheng  *
7708275SEric Cheng  * 1. It's a VLAN type of VNIC.
7718275SEric Cheng  * 2. The new value is current "primary" MAC address.
7728275SEric Cheng  * 3. The current MAC address is shared with other clients.
7738275SEric Cheng  * 4. The new MAC address has been used. This case will be valid when
7748275SEric Cheng  *    client migration is fully supported.
7758275SEric Cheng  */
7768275SEric Cheng int
7778275SEric Cheng mac_vnic_unicast_set(mac_client_handle_t mch, const uint8_t *addr)
7788275SEric Cheng {
7798275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
7808275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
7818275SEric Cheng 	mac_address_t *map = mcip->mci_unicast;
7828275SEric Cheng 	int err;
7838275SEric Cheng 
7848275SEric Cheng 	ASSERT(!(mip->mi_state_flags & MIS_IS_VNIC));
7858275SEric Cheng 	ASSERT(mcip->mci_state_flags & MCIS_IS_VNIC);
7868275SEric Cheng 	ASSERT(mcip->mci_flags != MAC_CLIENT_FLAGS_PRIMARY);
7878275SEric Cheng 
7888275SEric Cheng 	i_mac_perim_enter(mip);
7898275SEric Cheng 
7908275SEric Cheng 	/*
7918275SEric Cheng 	 * If this is a VLAN type of VNIC, it's using "primary" MAC address
7928275SEric Cheng 	 * of the underlying interface. Must fail here. Refer to case 1 above.
7938275SEric Cheng 	 */
7948275SEric Cheng 	if (bcmp(map->ma_addr, mip->mi_addr, map->ma_len) == 0) {
7958275SEric Cheng 		i_mac_perim_exit(mip);
7968275SEric Cheng 		return (ENOTSUP);
7978275SEric Cheng 	}
7988275SEric Cheng 
7998275SEric Cheng 	/*
8008275SEric Cheng 	 * If the new address is the "primary" one, must fail. Refer to
8018275SEric Cheng 	 * case 2 above.
8028275SEric Cheng 	 */
8038275SEric Cheng 	if (bcmp(addr, mip->mi_addr, map->ma_len) == 0) {
8048275SEric Cheng 		i_mac_perim_exit(mip);
8058275SEric Cheng 		return (EACCES);
8068275SEric Cheng 	}
8078275SEric Cheng 
8088275SEric Cheng 	/*
8098275SEric Cheng 	 * If the address is shared by multiple clients, must fail. Refer
8108275SEric Cheng 	 * to case 3 above.
8118275SEric Cheng 	 */
8128275SEric Cheng 	if (mac_check_macaddr_shared(map)) {
8138275SEric Cheng 		i_mac_perim_exit(mip);
8148275SEric Cheng 		return (EBUSY);
8158275SEric Cheng 	}
8168275SEric Cheng 
8178275SEric Cheng 	/*
8188275SEric Cheng 	 * If the new address has been used, must fail for now. Refer to
8198275SEric Cheng 	 * case 4 above.
8208275SEric Cheng 	 */
8218275SEric Cheng 	if (mac_find_macaddr(mip, (uint8_t *)addr) != NULL) {
8228275SEric Cheng 		i_mac_perim_exit(mip);
8238275SEric Cheng 		return (ENOTSUP);
8248275SEric Cheng 	}
8258275SEric Cheng 
8268275SEric Cheng 	/*
8278275SEric Cheng 	 * Update the MAC address.
8288275SEric Cheng 	 */
8298275SEric Cheng 	err = mac_update_macaddr(map, (uint8_t *)addr);
8308275SEric Cheng 
8318275SEric Cheng 	if (err != 0) {
8328275SEric Cheng 		i_mac_perim_exit(mip);
8338275SEric Cheng 		return (err);
8348275SEric Cheng 	}
8358275SEric Cheng 
8368275SEric Cheng 	/*
8378275SEric Cheng 	 * Update all flows of this MAC client.
8388275SEric Cheng 	 */
8398275SEric Cheng 	mac_unicast_update_client_flow(mcip);
8408275SEric Cheng 
8418275SEric Cheng 	i_mac_perim_exit(mip);
8428275SEric Cheng 	return (0);
8438275SEric Cheng }
8448275SEric Cheng 
8458275SEric Cheng /*
8468275SEric Cheng  * Program the new primary unicast address of the specified MAC.
8478275SEric Cheng  *
8488275SEric Cheng  * Function mac_update_macaddr() takes care different types of underlying
8498275SEric Cheng  * MAC. If the underlying MAC is VNIC, the VNIC driver must have registerd
8508275SEric Cheng  * mi_unicst() entry point, that indirectly calls mac_vnic_unicast_set()
8518275SEric Cheng  * which will take care of updating the MAC address of the corresponding
8528275SEric Cheng  * MAC client.
8538275SEric Cheng  *
8548275SEric Cheng  * This is the only interface that allow the client to update the "primary"
8558275SEric Cheng  * MAC address of the underlying MAC. The new value must have not been
8568275SEric Cheng  * used by other clients.
8578275SEric Cheng  */
8588275SEric Cheng int
8598275SEric Cheng mac_unicast_primary_set(mac_handle_t mh, const uint8_t *addr)
8608275SEric Cheng {
8618275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
8628275SEric Cheng 	mac_address_t *map;
8638275SEric Cheng 	int err;
8648275SEric Cheng 
8658275SEric Cheng 	/* verify the address validity */
8668275SEric Cheng 	if (!mac_unicst_verify(mh, addr, mip->mi_type->mt_addr_length))
8678275SEric Cheng 		return (EINVAL);
8688275SEric Cheng 
8698275SEric Cheng 	i_mac_perim_enter(mip);
8708275SEric Cheng 
8718275SEric Cheng 	/*
8728275SEric Cheng 	 * If the new value is the same as the current primary address value,
8738275SEric Cheng 	 * there's nothing to do.
8748275SEric Cheng 	 */
8758275SEric Cheng 	if (bcmp(addr, mip->mi_addr, mip->mi_type->mt_addr_length) == 0) {
8768275SEric Cheng 		i_mac_perim_exit(mip);
8778275SEric Cheng 		return (0);
8788275SEric Cheng 	}
8798275SEric Cheng 
8808275SEric Cheng 	if (mac_find_macaddr(mip, (uint8_t *)addr) != 0) {
8818275SEric Cheng 		i_mac_perim_exit(mip);
8828275SEric Cheng 		return (EBUSY);
8838275SEric Cheng 	}
8848275SEric Cheng 
8858275SEric Cheng 	map = mac_find_macaddr(mip, mip->mi_addr);
8868275SEric Cheng 	ASSERT(map != NULL);
8878275SEric Cheng 
8888275SEric Cheng 	/*
8898275SEric Cheng 	 * Update the MAC address.
8908275SEric Cheng 	 */
8918275SEric Cheng 	if (mip->mi_state_flags & MIS_IS_AGGR) {
8928275SEric Cheng 		mac_capab_aggr_t aggr_cap;
8938275SEric Cheng 
8948275SEric Cheng 		/*
8958275SEric Cheng 		 * If the mac is an aggregation, other than the unicast
8968275SEric Cheng 		 * addresses programming, aggr must be informed about this
8978275SEric Cheng 		 * primary unicst address change to change its mac address
8988275SEric Cheng 		 * policy to be user-specified.
8998275SEric Cheng 		 */
9008275SEric Cheng 		ASSERT(map->ma_type == MAC_ADDRESS_TYPE_UNICAST_CLASSIFIED);
9018275SEric Cheng 		VERIFY(i_mac_capab_get(mh, MAC_CAPAB_AGGR, &aggr_cap));
9028275SEric Cheng 		err = aggr_cap.mca_unicst(mip->mi_driver, addr);
9038275SEric Cheng 		if (err == 0)
9048275SEric Cheng 			bcopy(addr, map->ma_addr, map->ma_len);
9058275SEric Cheng 	} else {
9068275SEric Cheng 		err = mac_update_macaddr(map, (uint8_t *)addr);
9078275SEric Cheng 	}
9088275SEric Cheng 
9098275SEric Cheng 	if (err != 0) {
9108275SEric Cheng 		i_mac_perim_exit(mip);
9118275SEric Cheng 		return (err);
9128275SEric Cheng 	}
9138275SEric Cheng 
9148275SEric Cheng 	mac_unicast_update_clients(mip, map);
9158275SEric Cheng 
9168275SEric Cheng 	/*
9178275SEric Cheng 	 * Save the new primary MAC address in mac_impl_t.
9188275SEric Cheng 	 */
9198275SEric Cheng 	bcopy(addr, mip->mi_addr, mip->mi_type->mt_addr_length);
9208275SEric Cheng 
9218275SEric Cheng 	i_mac_perim_exit(mip);
9228275SEric Cheng 
9238275SEric Cheng 	if (err == 0)
9248275SEric Cheng 		i_mac_notify(mip, MAC_NOTE_UNICST);
9258275SEric Cheng 
9268275SEric Cheng 	return (err);
9278275SEric Cheng }
9288275SEric Cheng 
9298275SEric Cheng /*
9308275SEric Cheng  * Return the current primary MAC address of the specified MAC.
9318275SEric Cheng  */
9328275SEric Cheng void
9338275SEric Cheng mac_unicast_primary_get(mac_handle_t mh, uint8_t *addr)
9348275SEric Cheng {
9358275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
9368275SEric Cheng 
9378275SEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_READER);
9388275SEric Cheng 	bcopy(mip->mi_addr, addr, mip->mi_type->mt_addr_length);
9398275SEric Cheng 	rw_exit(&mip->mi_rw_lock);
9408275SEric Cheng }
9418275SEric Cheng 
9428275SEric Cheng /*
9438275SEric Cheng  * Return information about the use of the primary MAC address of the
9448275SEric Cheng  * specified MAC instance:
9458275SEric Cheng  *
9468275SEric Cheng  * - if client_name is non-NULL, it must point to a string of at
9478275SEric Cheng  *   least MAXNAMELEN bytes, and will be set to the name of the MAC
9488275SEric Cheng  *   client which uses the primary MAC address.
9498275SEric Cheng  *
9508275SEric Cheng  * - if in_use is non-NULL, used to return whether the primary MAC
9518275SEric Cheng  *   address is currently in use.
9528275SEric Cheng  */
9538275SEric Cheng void
9548275SEric Cheng mac_unicast_primary_info(mac_handle_t mh, char *client_name, boolean_t *in_use)
9558275SEric Cheng {
9568275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
9578275SEric Cheng 	mac_client_impl_t *cur_client;
9588275SEric Cheng 
9598275SEric Cheng 	if (in_use != NULL)
9608275SEric Cheng 		*in_use = B_FALSE;
9618275SEric Cheng 	if (client_name != NULL)
9628275SEric Cheng 		bzero(client_name, MAXNAMELEN);
9638275SEric Cheng 
9648275SEric Cheng 	/*
9658275SEric Cheng 	 * The mi_rw_lock is used to protect threads that don't hold the
9668275SEric Cheng 	 * mac perimeter to get a consistent view of the mi_clients_list.
9678275SEric Cheng 	 * Threads that modify the list must hold both the mac perimeter and
9688275SEric Cheng 	 * mi_rw_lock(RW_WRITER)
9698275SEric Cheng 	 */
9708275SEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_READER);
9718275SEric Cheng 	for (cur_client = mip->mi_clients_list; cur_client != NULL;
9728275SEric Cheng 	    cur_client = cur_client->mci_client_next) {
9738275SEric Cheng 		if (mac_is_primary_client(cur_client) ||
9748275SEric Cheng 		    (mip->mi_state_flags & MIS_IS_VNIC)) {
9758275SEric Cheng 			rw_exit(&mip->mi_rw_lock);
9768275SEric Cheng 			if (in_use != NULL)
9778275SEric Cheng 				*in_use = B_TRUE;
9788275SEric Cheng 			if (client_name != NULL) {
9798275SEric Cheng 				bcopy(cur_client->mci_name, client_name,
9808275SEric Cheng 				    MAXNAMELEN);
9818275SEric Cheng 			}
9828275SEric Cheng 			return;
9838275SEric Cheng 		}
9848275SEric Cheng 	}
9858275SEric Cheng 	rw_exit(&mip->mi_rw_lock);
9868275SEric Cheng }
9878275SEric Cheng 
9888275SEric Cheng /*
9898275SEric Cheng  * Add the specified MAC client to the list of clients which opened
9908275SEric Cheng  * the specified MAC.
9918275SEric Cheng  */
9928275SEric Cheng static void
9938275SEric Cheng mac_client_add(mac_client_impl_t *mcip)
9948275SEric Cheng {
9958275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
9968275SEric Cheng 
9978275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
9988275SEric Cheng 
9998275SEric Cheng 	/* add VNIC to the front of the list */
10008275SEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
10018275SEric Cheng 	mcip->mci_client_next = mip->mi_clients_list;
10028275SEric Cheng 	mip->mi_clients_list = mcip;
10038275SEric Cheng 	mip->mi_nclients++;
10048275SEric Cheng 	rw_exit(&mip->mi_rw_lock);
10058275SEric Cheng }
10068275SEric Cheng 
10078275SEric Cheng /*
10088275SEric Cheng  * Remove the specified MAC client from the list of clients which opened
10098275SEric Cheng  * the specified MAC.
10108275SEric Cheng  */
10118275SEric Cheng static void
10128275SEric Cheng mac_client_remove(mac_client_impl_t *mcip)
10138275SEric Cheng {
10148275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
10158275SEric Cheng 	mac_client_impl_t **prev, *cclient;
10168275SEric Cheng 
10178275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
10188275SEric Cheng 
10198275SEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
10208275SEric Cheng 	prev = &mip->mi_clients_list;
10218275SEric Cheng 	cclient = *prev;
10228275SEric Cheng 	while (cclient != NULL && cclient != mcip) {
10238275SEric Cheng 		prev = &cclient->mci_client_next;
10248275SEric Cheng 		cclient = *prev;
10258275SEric Cheng 	}
10268275SEric Cheng 	ASSERT(cclient != NULL);
10278275SEric Cheng 	*prev = cclient->mci_client_next;
10288275SEric Cheng 	mip->mi_nclients--;
10298275SEric Cheng 	rw_exit(&mip->mi_rw_lock);
10308275SEric Cheng }
10318275SEric Cheng 
10328275SEric Cheng static mac_unicast_impl_t *
10338275SEric Cheng mac_client_find_vid(mac_client_impl_t *mcip, uint16_t vid)
10348275SEric Cheng {
10358275SEric Cheng 	mac_unicast_impl_t *muip = mcip->mci_unicast_list;
10368275SEric Cheng 
10378275SEric Cheng 	while ((muip != NULL) && (muip->mui_vid != vid))
10388275SEric Cheng 		muip = muip->mui_next;
10398275SEric Cheng 
10408275SEric Cheng 	return (muip);
10418275SEric Cheng }
10428275SEric Cheng 
10438275SEric Cheng /*
10448275SEric Cheng  * Return whether the specified (MAC address, VID) tuple is already used by
10458275SEric Cheng  * one of the MAC clients associated with the specified MAC.
10468275SEric Cheng  */
10478275SEric Cheng static boolean_t
10488275SEric Cheng mac_addr_in_use(mac_impl_t *mip, uint8_t *mac_addr, uint16_t vid)
10498275SEric Cheng {
10508275SEric Cheng 	mac_client_impl_t *client;
10518275SEric Cheng 	mac_address_t *map;
10528275SEric Cheng 
10538275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
10548275SEric Cheng 
10558275SEric Cheng 	for (client = mip->mi_clients_list; client != NULL;
10568275SEric Cheng 	    client = client->mci_client_next) {
10578275SEric Cheng 
10588275SEric Cheng 		/*
10598275SEric Cheng 		 * Ignore clients that don't have unicast address.
10608275SEric Cheng 		 */
10618275SEric Cheng 		if (client->mci_unicast_list == NULL)
10628275SEric Cheng 			continue;
10638275SEric Cheng 
10648275SEric Cheng 		map = client->mci_unicast;
10658275SEric Cheng 
10668275SEric Cheng 		if ((bcmp(mac_addr, map->ma_addr, map->ma_len) == 0) &&
10678275SEric Cheng 		    (mac_client_find_vid(client, vid) != NULL)) {
10688275SEric Cheng 			return (B_TRUE);
10698275SEric Cheng 		}
10708275SEric Cheng 	}
10718275SEric Cheng 
10728275SEric Cheng 	return (B_FALSE);
10738275SEric Cheng }
10748275SEric Cheng 
10758275SEric Cheng /*
10768275SEric Cheng  * Generate a random MAC address. The MAC address prefix is
10778275SEric Cheng  * stored in the array pointed to by mac_addr, and its length, in bytes,
10788275SEric Cheng  * is specified by prefix_len. The least significant bits
10798275SEric Cheng  * after prefix_len bytes are generated, and stored after the prefix
10808275SEric Cheng  * in the mac_addr array.
10818275SEric Cheng  */
10828275SEric Cheng int
10838275SEric Cheng mac_addr_random(mac_client_handle_t mch, uint_t prefix_len,
10848275SEric Cheng     uint8_t *mac_addr, mac_diag_t *diag)
10858275SEric Cheng {
10868275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
10878275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
10888275SEric Cheng 	size_t addr_len = mip->mi_type->mt_addr_length;
10898275SEric Cheng 
10908275SEric Cheng 	if (prefix_len >= addr_len) {
10918275SEric Cheng 		*diag = MAC_DIAG_MACPREFIXLEN_INVALID;
10928275SEric Cheng 		return (EINVAL);
10938275SEric Cheng 	}
10948275SEric Cheng 
10958275SEric Cheng 	/* check the prefix value */
10968275SEric Cheng 	if (prefix_len > 0) {
10978275SEric Cheng 		bzero(mac_addr + prefix_len, addr_len - prefix_len);
10988275SEric Cheng 		if (!mac_unicst_verify((mac_handle_t)mip, mac_addr,
10998275SEric Cheng 		    addr_len)) {
11008275SEric Cheng 			*diag = MAC_DIAG_MACPREFIX_INVALID;
11018275SEric Cheng 			return (EINVAL);
11028275SEric Cheng 		}
11038275SEric Cheng 	}
11048275SEric Cheng 
11058275SEric Cheng 	/* generate the MAC address */
11068275SEric Cheng 	if (prefix_len < addr_len) {
11078275SEric Cheng 		(void) random_get_pseudo_bytes(mac_addr +
11088275SEric Cheng 		    prefix_len, addr_len - prefix_len);
11098275SEric Cheng 	}
11108275SEric Cheng 
11118275SEric Cheng 	*diag = 0;
11128275SEric Cheng 	return (0);
11138275SEric Cheng }
11148275SEric Cheng 
11158275SEric Cheng /*
11168275SEric Cheng  * Set the priority range for this MAC client. This will be used to
11178275SEric Cheng  * determine the absolute priority for the threads created for this
11188275SEric Cheng  * MAC client using the specified "low", "medium" and "high" level.
11198275SEric Cheng  * This will also be used for any subflows on this MAC client.
11208275SEric Cheng  */
11218275SEric Cheng #define	MAC_CLIENT_SET_PRIORITY_RANGE(mcip, pri) {			\
11228275SEric Cheng 	(mcip)->mci_min_pri = FLOW_MIN_PRIORITY(MINCLSYSPRI,	\
11238275SEric Cheng 	    MAXCLSYSPRI, (pri));					\
11248275SEric Cheng 	(mcip)->mci_max_pri = FLOW_MAX_PRIORITY(MINCLSYSPRI,	\
11258275SEric Cheng 	    MAXCLSYSPRI, (mcip)->mci_min_pri);				\
11268275SEric Cheng 	}
11278275SEric Cheng 
11288275SEric Cheng /*
11298275SEric Cheng  * MAC client open entry point. Return a new MAC client handle. Each
11308275SEric Cheng  * MAC client is associated with a name, specified through the 'name'
11318275SEric Cheng  * argument.
11328275SEric Cheng  */
11338275SEric Cheng int
11348275SEric Cheng mac_client_open(mac_handle_t mh, mac_client_handle_t *mchp, char *name,
11358275SEric Cheng     uint16_t flags)
11368275SEric Cheng {
11378275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
11388275SEric Cheng 	mac_client_impl_t *mcip;
11398275SEric Cheng 	int err = 0;
11408275SEric Cheng 	boolean_t share_desired =
11418275SEric Cheng 	    ((flags & MAC_OPEN_FLAGS_SHARES_DESIRED) != 0);
11428275SEric Cheng 	boolean_t no_hwrings = ((flags & MAC_OPEN_FLAGS_NO_HWRINGS) != 0);
11438275SEric Cheng 	boolean_t req_hwrings = ((flags & MAC_OPEN_FLAGS_REQ_HWRINGS) != 0);
11448275SEric Cheng 	flow_entry_t	*flent = NULL;
11458275SEric Cheng 
11468275SEric Cheng 	*mchp = NULL;
11478275SEric Cheng 	if (share_desired && no_hwrings) {
11488275SEric Cheng 		/* can't have shares but no hardware rings */
11498275SEric Cheng 		return (EINVAL);
11508275SEric Cheng 	}
11518275SEric Cheng 
11528275SEric Cheng 	i_mac_perim_enter(mip);
11538275SEric Cheng 
11548275SEric Cheng 	if (mip->mi_state_flags & MIS_IS_VNIC) {
11558275SEric Cheng 		/*
11568275SEric Cheng 		 * The underlying MAC is a VNIC. Return the MAC client
11578275SEric Cheng 		 * handle of the lower MAC which was obtained by
11588275SEric Cheng 		 * the VNIC driver when it did its mac_client_open().
11598275SEric Cheng 		 */
11608275SEric Cheng 
11618275SEric Cheng 		mcip = mac_vnic_lower(mip);
11628275SEric Cheng 
11638275SEric Cheng 		/*
11648275SEric Cheng 		 * Note that multiple mac clients share the same mcip in
11658275SEric Cheng 		 * this case.
11668275SEric Cheng 		 */
11678275SEric Cheng 		if (flags & MAC_OPEN_FLAGS_EXCLUSIVE)
11688275SEric Cheng 			mcip->mci_state_flags |= MCIS_EXCLUSIVE;
11698275SEric Cheng 
11708275SEric Cheng 		mip->mi_clients_list = mcip;
11718275SEric Cheng 		i_mac_perim_exit(mip);
11728275SEric Cheng 		*mchp = (mac_client_handle_t)mcip;
11738275SEric Cheng 		return (err);
11748275SEric Cheng 	}
11758275SEric Cheng 
11768275SEric Cheng 	mcip = kmem_cache_alloc(mac_client_impl_cache, KM_SLEEP);
11778275SEric Cheng 
11788275SEric Cheng 	mcip->mci_mip = mip;
11798275SEric Cheng 	mcip->mci_upper_mip = NULL;
11808275SEric Cheng 	mcip->mci_rx_fn = mac_pkt_drop;
11818275SEric Cheng 	mcip->mci_rx_arg = NULL;
11828275SEric Cheng 	mcip->mci_direct_rx_fn = NULL;
11838275SEric Cheng 	mcip->mci_direct_rx_arg = NULL;
11848275SEric Cheng 
11858275SEric Cheng 	if ((flags & MAC_OPEN_FLAGS_IS_VNIC) != 0)
11868275SEric Cheng 		mcip->mci_state_flags |= MCIS_IS_VNIC;
11878275SEric Cheng 
11888275SEric Cheng 	if ((flags & MAC_OPEN_FLAGS_EXCLUSIVE) != 0)
11898275SEric Cheng 		mcip->mci_state_flags |= MCIS_EXCLUSIVE;
11908275SEric Cheng 
11918275SEric Cheng 	if ((flags & MAC_OPEN_FLAGS_IS_AGGR_PORT) != 0)
11928275SEric Cheng 		mcip->mci_state_flags |= MCIS_IS_AGGR_PORT;
11938275SEric Cheng 
11948275SEric Cheng 	if ((flags & MAC_OPEN_FLAGS_TAG_DISABLE) != 0)
11958275SEric Cheng 		mcip->mci_state_flags |= MCIS_TAG_DISABLE;
11968275SEric Cheng 
11978275SEric Cheng 	if ((flags & MAC_OPEN_FLAGS_STRIP_DISABLE) != 0)
11988275SEric Cheng 		mcip->mci_state_flags |= MCIS_STRIP_DISABLE;
11998275SEric Cheng 
12008275SEric Cheng 	if ((flags & MAC_OPEN_FLAGS_DISABLE_TX_VID_CHECK) != 0)
12018275SEric Cheng 		mcip->mci_state_flags |= MCIS_DISABLE_TX_VID_CHECK;
12028275SEric Cheng 
12038275SEric Cheng 	if ((flags & MAC_OPEN_FLAGS_USE_DATALINK_NAME) != 0) {
12048275SEric Cheng 		datalink_id_t	linkid;
12058275SEric Cheng 
12068275SEric Cheng 		ASSERT(name == NULL);
12078275SEric Cheng 		if ((err = dls_devnet_macname2linkid(mip->mi_name,
12088275SEric Cheng 		    &linkid)) != 0) {
12098275SEric Cheng 			goto done;
12108275SEric Cheng 		}
12118275SEric Cheng 		if ((err = dls_mgmt_get_linkinfo(linkid, mcip->mci_name, NULL,
12128275SEric Cheng 		    NULL, NULL)) != 0) {
12138275SEric Cheng 			/*
12148275SEric Cheng 			 * Use mac name if dlmgmtd is not available.
12158275SEric Cheng 			 */
12168275SEric Cheng 			if (err == EBADF) {
12178275SEric Cheng 				(void) strlcpy(mcip->mci_name, mip->mi_name,
12188275SEric Cheng 				    sizeof (mcip->mci_name));
12198275SEric Cheng 				err = 0;
12208275SEric Cheng 			} else {
12218275SEric Cheng 				goto done;
12228275SEric Cheng 			}
12238275SEric Cheng 		}
12248275SEric Cheng 		mcip->mci_state_flags |= MCIS_USE_DATALINK_NAME;
12258275SEric Cheng 	} else {
12268275SEric Cheng 		ASSERT(name != NULL);
12278275SEric Cheng 		if (strlen(name) > MAXNAMELEN) {
12288275SEric Cheng 			err = EINVAL;
12298275SEric Cheng 			goto done;
12308275SEric Cheng 		}
12318275SEric Cheng 		(void) strlcpy(mcip->mci_name, name, sizeof (mcip->mci_name));
12328275SEric Cheng 	}
12338275SEric Cheng 	/* the subflow table will be created dynamically */
12348275SEric Cheng 	mcip->mci_subflow_tab = NULL;
12358275SEric Cheng 	mcip->mci_stat_multircv = 0;
12368275SEric Cheng 	mcip->mci_stat_brdcstrcv = 0;
12378275SEric Cheng 	mcip->mci_stat_multixmt = 0;
12388275SEric Cheng 	mcip->mci_stat_brdcstxmt = 0;
12398275SEric Cheng 
12408275SEric Cheng 	mcip->mci_stat_obytes = 0;
12418275SEric Cheng 	mcip->mci_stat_opackets = 0;
12428275SEric Cheng 	mcip->mci_stat_oerrors = 0;
12438275SEric Cheng 	mcip->mci_stat_ibytes = 0;
12448275SEric Cheng 	mcip->mci_stat_ipackets = 0;
12458275SEric Cheng 	mcip->mci_stat_ierrors = 0;
12468275SEric Cheng 
12478275SEric Cheng 	/* Create an initial flow */
12488275SEric Cheng 
12498275SEric Cheng 	err = mac_flow_create(NULL, NULL, mcip->mci_name, NULL,
12508275SEric Cheng 	    mcip->mci_state_flags & MCIS_IS_VNIC ? FLOW_VNIC_MAC :
12518275SEric Cheng 	    FLOW_PRIMARY_MAC, &flent);
12528275SEric Cheng 	if (err != 0)
12538275SEric Cheng 		goto done;
12548275SEric Cheng 	mcip->mci_flent = flent;
12558275SEric Cheng 	FLOW_MARK(flent, FE_MC_NO_DATAPATH);
12568275SEric Cheng 	flent->fe_mcip = mcip;
12578275SEric Cheng 	/*
12588275SEric Cheng 	 * Place initial creation reference on the flow. This reference
12598275SEric Cheng 	 * is released in the corresponding delete action viz.
12608275SEric Cheng 	 * mac_unicast_remove after waiting for all transient refs to
12618275SEric Cheng 	 * to go away. The wait happens in mac_flow_wait.
12628275SEric Cheng 	 */
12638275SEric Cheng 	FLOW_REFHOLD(flent);
12648275SEric Cheng 
12658275SEric Cheng 	/*
12668275SEric Cheng 	 * Do this ahead of the mac_bcast_add() below so that the mi_nclients
12678275SEric Cheng 	 * will have the right value for mac_rx_srs_setup().
12688275SEric Cheng 	 */
12698275SEric Cheng 	mac_client_add(mcip);
12708275SEric Cheng 
12718400SNicolas.Droux@Sun.COM 	if (no_hwrings)
12728400SNicolas.Droux@Sun.COM 		mcip->mci_state_flags |= MCIS_NO_HWRINGS;
12738400SNicolas.Droux@Sun.COM 	if (req_hwrings)
12748400SNicolas.Droux@Sun.COM 		mcip->mci_state_flags |= MCIS_REQ_HWRINGS;
12758275SEric Cheng 	mcip->mci_share = NULL;
12768275SEric Cheng 	if (share_desired) {
12778275SEric Cheng 		ASSERT(!no_hwrings);
12788275SEric Cheng 		i_mac_share_alloc(mcip);
12798275SEric Cheng 	}
12808275SEric Cheng 
12818275SEric Cheng 	DTRACE_PROBE2(mac__client__open__allocated, mac_impl_t *,
12828275SEric Cheng 	    mcip->mci_mip, mac_client_impl_t *, mcip);
12838275SEric Cheng 	*mchp = (mac_client_handle_t)mcip;
12848275SEric Cheng 
12858275SEric Cheng 	i_mac_perim_exit(mip);
12868275SEric Cheng 	return (0);
12878275SEric Cheng 
12888275SEric Cheng done:
12898275SEric Cheng 	i_mac_perim_exit(mip);
12908275SEric Cheng 	mcip->mci_state_flags = 0;
12918275SEric Cheng 	mcip->mci_tx_flag = 0;
12928275SEric Cheng 	kmem_cache_free(mac_client_impl_cache, mcip);
12938275SEric Cheng 	return (err);
12948275SEric Cheng }
12958275SEric Cheng 
12968275SEric Cheng /*
12978275SEric Cheng  * Close the specified MAC client handle.
12988275SEric Cheng  */
12998275SEric Cheng void
13008275SEric Cheng mac_client_close(mac_client_handle_t mch, uint16_t flags)
13018275SEric Cheng {
13028275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
13038275SEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
13048275SEric Cheng 	flow_entry_t		*flent;
13058275SEric Cheng 
13068275SEric Cheng 	i_mac_perim_enter(mip);
13078275SEric Cheng 
13088275SEric Cheng 	if (flags & MAC_CLOSE_FLAGS_EXCLUSIVE)
13098275SEric Cheng 		mcip->mci_state_flags &= ~MCIS_EXCLUSIVE;
13108275SEric Cheng 
13118275SEric Cheng 	if ((mcip->mci_state_flags & MCIS_IS_VNIC) &&
13128275SEric Cheng 	    !(flags & MAC_CLOSE_FLAGS_IS_VNIC)) {
13138275SEric Cheng 		/*
13148275SEric Cheng 		 * This is an upper VNIC client initiated operation.
13158275SEric Cheng 		 * The lower MAC client will be closed by the VNIC driver
13168275SEric Cheng 		 * when the VNIC is deleted.
13178275SEric Cheng 		 */
13188275SEric Cheng 
13198275SEric Cheng 		i_mac_perim_exit(mip);
13208275SEric Cheng 		return;
13218275SEric Cheng 	}
13228275SEric Cheng 
13238275SEric Cheng 	/*
13248275SEric Cheng 	 * Remove the flent associated with the MAC client
13258275SEric Cheng 	 */
13268275SEric Cheng 	flent = mcip->mci_flent;
13278275SEric Cheng 	mcip->mci_flent = NULL;
13288275SEric Cheng 	FLOW_FINAL_REFRELE(flent);
13298275SEric Cheng 
13308275SEric Cheng 	/*
13318275SEric Cheng 	 * MAC clients must remove the unicast addresses and promisc callbacks
13328275SEric Cheng 	 * they added before issuing a mac_client_close().
13338275SEric Cheng 	 */
13348275SEric Cheng 	ASSERT(mcip->mci_unicast_list == NULL);
13358275SEric Cheng 	ASSERT(mcip->mci_promisc_list == NULL);
13368275SEric Cheng 	ASSERT(mcip->mci_tx_notify_cb_list == NULL);
13378275SEric Cheng 
13388275SEric Cheng 	i_mac_share_free(mcip);
13398275SEric Cheng 
13408275SEric Cheng 	mac_client_remove(mcip);
13418275SEric Cheng 
13428275SEric Cheng 	i_mac_perim_exit(mip);
13438275SEric Cheng 	mcip->mci_subflow_tab = NULL;
13448275SEric Cheng 	mcip->mci_state_flags = 0;
13458275SEric Cheng 	mcip->mci_tx_flag = 0;
13468275SEric Cheng 	kmem_cache_free(mac_client_impl_cache, mch);
13478275SEric Cheng }
13488275SEric Cheng 
13498275SEric Cheng /*
13508275SEric Cheng  * Enable bypass for the specified MAC client.
13518275SEric Cheng  */
13528275SEric Cheng boolean_t
13538275SEric Cheng mac_rx_bypass_set(mac_client_handle_t mch, mac_direct_rx_t rx_fn, void *arg1)
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 
13588275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
13598275SEric Cheng 
13608275SEric Cheng 	/*
1361*8833SVenu.Iyer@Sun.COM 	 * If the mac_client is a VLAN, we should not do DLS bypass and
1362*8833SVenu.Iyer@Sun.COM 	 * instead let the packets come up via mac_rx_deliver so the vlan
1363*8833SVenu.Iyer@Sun.COM 	 * header can be stripped.
13648275SEric Cheng 	 */
1365*8833SVenu.Iyer@Sun.COM 	if (mcip->mci_nvids > 0)
13668275SEric Cheng 		return (B_FALSE);
13678275SEric Cheng 
13688275SEric Cheng 	/*
13698275SEric Cheng 	 * These are not accessed directly in the data path, and hence
13708275SEric Cheng 	 * don't need any protection
13718275SEric Cheng 	 */
13728275SEric Cheng 	mcip->mci_direct_rx_fn = rx_fn;
13738275SEric Cheng 	mcip->mci_direct_rx_arg = arg1;
13748275SEric Cheng 	mcip->mci_state_flags |= MCIS_CLIENT_POLL_CAPABLE;
13758275SEric Cheng 	return (B_TRUE);
13768275SEric Cheng }
13778275SEric Cheng 
13788275SEric Cheng /*
13798275SEric Cheng  * Set the receive callback for the specified MAC client. There can be
13808275SEric Cheng  * at most one such callback per MAC client.
13818275SEric Cheng  */
13828275SEric Cheng void
13838275SEric Cheng mac_rx_set(mac_client_handle_t mch, mac_rx_t rx_fn, void *arg)
13848275SEric Cheng {
13858275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
13868275SEric Cheng 	mac_impl_t	*mip = mcip->mci_mip;
13878275SEric Cheng 
13888275SEric Cheng 	/*
13898275SEric Cheng 	 * Instead of adding an extra set of locks and refcnts in
13908275SEric Cheng 	 * the datapath at the mac client boundary, we temporarily quiesce
13918275SEric Cheng 	 * the SRS and related entities. We then change the receive function
13928275SEric Cheng 	 * without interference from any receive data thread and then reenable
13938275SEric Cheng 	 * the data flow subsequently.
13948275SEric Cheng 	 */
13958275SEric Cheng 	i_mac_perim_enter(mip);
13968275SEric Cheng 	mac_rx_client_quiesce(mch);
13978275SEric Cheng 
13988275SEric Cheng 	mcip->mci_rx_fn = rx_fn;
13998275SEric Cheng 	mcip->mci_rx_arg = arg;
14008275SEric Cheng 	mac_rx_client_restart(mch);
14018275SEric Cheng 	i_mac_perim_exit(mip);
14028275SEric Cheng }
14038275SEric Cheng 
14048275SEric Cheng /*
14058275SEric Cheng  * Reset the receive callback for the specified MAC client.
14068275SEric Cheng  */
14078275SEric Cheng void
14088275SEric Cheng mac_rx_clear(mac_client_handle_t mch)
14098275SEric Cheng {
14108275SEric Cheng 	mac_rx_set(mch, mac_pkt_drop, NULL);
14118275SEric Cheng }
14128275SEric Cheng 
14138275SEric Cheng /*
14148275SEric Cheng  * Walk the MAC client subflow table and updates their priority values.
14158275SEric Cheng  */
14168275SEric Cheng static int
14178275SEric Cheng mac_update_subflow_priority_cb(flow_entry_t *flent, void *arg)
14188275SEric Cheng {
14198275SEric Cheng 	mac_flow_update_priority(arg, flent);
14208275SEric Cheng 	return (0);
14218275SEric Cheng }
14228275SEric Cheng 
14238275SEric Cheng void
14248275SEric Cheng mac_update_subflow_priority(mac_client_impl_t *mcip)
14258275SEric Cheng {
14268275SEric Cheng 	(void) mac_flow_walk(mcip->mci_subflow_tab,
14278275SEric Cheng 	    mac_update_subflow_priority_cb, mcip);
14288275SEric Cheng }
14298275SEric Cheng 
14308275SEric Cheng /*
14318275SEric Cheng  * When the MAC client is being brought up (i.e. we do a unicast_add) we need
14328275SEric Cheng  * to initialize the cpu and resource control structure in the
14338275SEric Cheng  * mac_client_impl_t from the mac_impl_t (i.e if there are any cached
14348275SEric Cheng  * properties before the flow entry for the unicast address was created).
14358275SEric Cheng  */
14368275SEric Cheng int
14378275SEric Cheng mac_resource_ctl_set(mac_client_handle_t mch, mac_resource_props_t *mrp)
14388275SEric Cheng {
14398275SEric Cheng 	mac_client_impl_t 	*mcip = (mac_client_impl_t *)mch;
14408275SEric Cheng 	mac_impl_t		*mip = (mac_impl_t *)mcip->mci_mip;
14418275SEric Cheng 	int			err = 0;
14428275SEric Cheng 
14438275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
14448275SEric Cheng 
14458275SEric Cheng 	err = mac_validate_props(mrp);
14468275SEric Cheng 	if (err != 0)
14478275SEric Cheng 		return (err);
14488275SEric Cheng 
14498275SEric Cheng 	mac_update_resources(mrp, MCIP_RESOURCE_PROPS(mcip), B_FALSE);
14508275SEric Cheng 	if (MCIP_DATAPATH_SETUP(mcip)) {
14518275SEric Cheng 		/*
14528275SEric Cheng 		 * We have to set this prior to calling mac_flow_modify.
14538275SEric Cheng 		 */
14548275SEric Cheng 		if (mrp->mrp_mask & MRP_PRIORITY) {
14558275SEric Cheng 			if (mrp->mrp_priority == MPL_RESET) {
14568275SEric Cheng 				MAC_CLIENT_SET_PRIORITY_RANGE(mcip,
14578275SEric Cheng 				    MPL_LINK_DEFAULT);
14588275SEric Cheng 			} else {
14598275SEric Cheng 				MAC_CLIENT_SET_PRIORITY_RANGE(mcip,
14608275SEric Cheng 				    mrp->mrp_priority);
14618275SEric Cheng 			}
14628275SEric Cheng 		}
14638275SEric Cheng 
14648275SEric Cheng 		mac_flow_modify(mip->mi_flow_tab, mcip->mci_flent, mrp);
14658275SEric Cheng 		if (mrp->mrp_mask & MRP_PRIORITY)
14668275SEric Cheng 			mac_update_subflow_priority(mcip);
14678275SEric Cheng 		return (0);
14688275SEric Cheng 	}
14698275SEric Cheng 	return (0);
14708275SEric Cheng }
14718275SEric Cheng 
14728275SEric Cheng void
14738275SEric Cheng mac_resource_ctl_get(mac_client_handle_t mch, mac_resource_props_t *mrp)
14748275SEric Cheng {
14758275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
14768275SEric Cheng 	mac_resource_props_t	*mcip_mrp = MCIP_RESOURCE_PROPS(mcip);
14778275SEric Cheng 
14788275SEric Cheng 	bcopy(mcip_mrp, mrp, sizeof (mac_resource_props_t));
14798275SEric Cheng }
14808275SEric Cheng 
14818275SEric Cheng static int
14828275SEric Cheng mac_unicast_flow_create(mac_client_impl_t *mcip, uint8_t *mac_addr,
14838275SEric Cheng     uint16_t vid, boolean_t is_primary, boolean_t first_flow,
14848275SEric Cheng     flow_entry_t **flent, mac_resource_props_t *mrp)
14858275SEric Cheng {
14868275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mcip->mci_mip;
14878275SEric Cheng 	flow_desc_t	flow_desc;
14888558SGirish.Moodalbail@Sun.COM 	char		flowname[MAXFLOWNAMELEN];
14898275SEric Cheng 	int		err;
14908275SEric Cheng 	uint_t		flent_flags;
14918275SEric Cheng 
14928275SEric Cheng 	/*
14938275SEric Cheng 	 * First unicast address being added, create a new flow
14948275SEric Cheng 	 * for that MAC client.
14958275SEric Cheng 	 */
14968275SEric Cheng 	bzero(&flow_desc, sizeof (flow_desc));
14978275SEric Cheng 
14988275SEric Cheng 	flow_desc.fd_mac_len = mip->mi_type->mt_addr_length;
14998275SEric Cheng 	bcopy(mac_addr, flow_desc.fd_dst_mac, flow_desc.fd_mac_len);
15008275SEric Cheng 	flow_desc.fd_mask = FLOW_LINK_DST;
15018275SEric Cheng 	if (vid != 0) {
15028275SEric Cheng 		flow_desc.fd_vid = vid;
15038275SEric Cheng 		flow_desc.fd_mask |= FLOW_LINK_VID;
15048275SEric Cheng 	}
15058275SEric Cheng 
15068275SEric Cheng 	/*
15078275SEric Cheng 	 * XXX-nicolas. For now I'm keeping the FLOW_PRIMARY_MAC
15088275SEric Cheng 	 * and FLOW_VNIC. Even though they're a hack inherited
15098275SEric Cheng 	 * from the SRS code, we'll keep them for now. They're currently
15108275SEric Cheng 	 * consumed by mac_datapath_setup() to create the SRS.
15118275SEric Cheng 	 * That code should be eventually moved out of
15128275SEric Cheng 	 * mac_datapath_setup() and moved to a mac_srs_create()
15138275SEric Cheng 	 * function of some sort to keep things clean.
15148275SEric Cheng 	 *
15158275SEric Cheng 	 * Also, there's no reason why the SRS for the primary MAC
15168275SEric Cheng 	 * client should be different than any other MAC client. Until
15178275SEric Cheng 	 * this is cleaned-up, we support only one MAC unicast address
15188275SEric Cheng 	 * per client.
15198275SEric Cheng 	 *
15208275SEric Cheng 	 * We set FLOW_PRIMARY_MAC for the primary MAC address,
15218275SEric Cheng 	 * FLOW_VNIC for everything else.
15228275SEric Cheng 	 */
15238275SEric Cheng 	if (is_primary)
15248275SEric Cheng 		flent_flags = FLOW_PRIMARY_MAC;
15258275SEric Cheng 	else
15268275SEric Cheng 		flent_flags = FLOW_VNIC_MAC;
15278275SEric Cheng 
15288275SEric Cheng 	/*
15298275SEric Cheng 	 * For the first flow we use the mac client's name - mci_name, for
15308275SEric Cheng 	 * subsequent ones we just create a name with the vid. This is
15318275SEric Cheng 	 * so that we can add these flows to the same flow table. This is
15328275SEric Cheng 	 * fine as the flow name (except for the one with the mac client's
15338275SEric Cheng 	 * name) is not visible. When the first flow is removed, we just replace
15348275SEric Cheng 	 * its fdesc with another from the list, so we will still retain the
15358275SEric Cheng 	 * flent with the MAC client's flow name.
15368275SEric Cheng 	 */
15378275SEric Cheng 	if (first_flow) {
15388558SGirish.Moodalbail@Sun.COM 		bcopy(mcip->mci_name, flowname, MAXFLOWNAMELEN);
15398275SEric Cheng 	} else {
15408275SEric Cheng 		(void) sprintf(flowname, "%s%u", mcip->mci_name, vid);
15418275SEric Cheng 		flent_flags = FLOW_NO_STATS;
15428275SEric Cheng 	}
15438275SEric Cheng 
15448275SEric Cheng 	if ((err = mac_flow_create(&flow_desc, mrp, flowname, NULL,
15458275SEric Cheng 	    flent_flags, flent)) != 0)
15468275SEric Cheng 		return (err);
15478275SEric Cheng 
15488275SEric Cheng 	FLOW_MARK(*flent, FE_INCIPIENT);
15498275SEric Cheng 	(*flent)->fe_mcip = mcip;
15508275SEric Cheng 
15518275SEric Cheng 	/*
15528275SEric Cheng 	 * Place initial creation reference on the flow. This reference
15538275SEric Cheng 	 * is released in the corresponding delete action viz.
15548275SEric Cheng 	 * mac_unicast_remove after waiting for all transient refs to
15558275SEric Cheng 	 * to go away. The wait happens in mac_flow_wait.
15568275SEric Cheng 	 * We have already held the reference in mac_client_open().
15578275SEric Cheng 	 */
15588275SEric Cheng 	if (!first_flow)
15598275SEric Cheng 		FLOW_REFHOLD(*flent);
15608275SEric Cheng 	return (0);
15618275SEric Cheng }
15628275SEric Cheng 
15638275SEric Cheng /* Refresh the multicast grouping for this VID. */
15648275SEric Cheng int
15658275SEric Cheng mac_client_update_mcast(void *arg, boolean_t add, const uint8_t *addrp)
15668275SEric Cheng {
15678275SEric Cheng 	flow_entry_t		*flent = arg;
15688275SEric Cheng 	mac_client_impl_t	*mcip = flent->fe_mcip;
15698275SEric Cheng 	uint16_t		vid;
15708275SEric Cheng 	flow_desc_t		flow_desc;
15718275SEric Cheng 
15728275SEric Cheng 	mac_flow_get_desc(flent, &flow_desc);
15738275SEric Cheng 	vid = (flow_desc.fd_mask & FLOW_LINK_VID) != 0 ?
15748275SEric Cheng 	    flow_desc.fd_vid : VLAN_ID_NONE;
15758275SEric Cheng 
15768275SEric Cheng 	/*
15778275SEric Cheng 	 * We don't call mac_multicast_add()/mac_multicast_remove() as
15788275SEric Cheng 	 * we want to add/remove for this specific vid.
15798275SEric Cheng 	 */
15808275SEric Cheng 	if (add) {
15818275SEric Cheng 		return (mac_bcast_add(mcip, addrp, vid,
15828275SEric Cheng 		    MAC_ADDRTYPE_MULTICAST));
15838275SEric Cheng 	} else {
15848275SEric Cheng 		mac_bcast_delete(mcip, addrp, vid);
15858275SEric Cheng 		return (0);
15868275SEric Cheng 	}
15878275SEric Cheng }
15888275SEric Cheng 
1589*8833SVenu.Iyer@Sun.COM static void
1590*8833SVenu.Iyer@Sun.COM mac_update_single_active_client(mac_impl_t *mip)
1591*8833SVenu.Iyer@Sun.COM {
1592*8833SVenu.Iyer@Sun.COM 	mac_client_impl_t *client = NULL;
1593*8833SVenu.Iyer@Sun.COM 
1594*8833SVenu.Iyer@Sun.COM 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
1595*8833SVenu.Iyer@Sun.COM 
1596*8833SVenu.Iyer@Sun.COM 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
1597*8833SVenu.Iyer@Sun.COM 	if (mip->mi_nactiveclients == 1) {
1598*8833SVenu.Iyer@Sun.COM 		/*
1599*8833SVenu.Iyer@Sun.COM 		 * Find the one active MAC client from the list of MAC
1600*8833SVenu.Iyer@Sun.COM 		 * clients. The active MAC client has at least one
1601*8833SVenu.Iyer@Sun.COM 		 * unicast address.
1602*8833SVenu.Iyer@Sun.COM 		 */
1603*8833SVenu.Iyer@Sun.COM 		for (client = mip->mi_clients_list; client != NULL;
1604*8833SVenu.Iyer@Sun.COM 		    client = client->mci_client_next) {
1605*8833SVenu.Iyer@Sun.COM 			if (client->mci_unicast_list != NULL)
1606*8833SVenu.Iyer@Sun.COM 				break;
1607*8833SVenu.Iyer@Sun.COM 		}
1608*8833SVenu.Iyer@Sun.COM 		ASSERT(client != NULL);
1609*8833SVenu.Iyer@Sun.COM 	}
1610*8833SVenu.Iyer@Sun.COM 
1611*8833SVenu.Iyer@Sun.COM 	/*
1612*8833SVenu.Iyer@Sun.COM 	 * mi_single_active_client is protected by the MAC impl's read/writer
1613*8833SVenu.Iyer@Sun.COM 	 * lock, which allows mac_rx() to check the value of that pointer
1614*8833SVenu.Iyer@Sun.COM 	 * as a reader.
1615*8833SVenu.Iyer@Sun.COM 	 */
1616*8833SVenu.Iyer@Sun.COM 	mip->mi_single_active_client = client;
1617*8833SVenu.Iyer@Sun.COM 	rw_exit(&mip->mi_rw_lock);
1618*8833SVenu.Iyer@Sun.COM }
1619*8833SVenu.Iyer@Sun.COM 
16208275SEric Cheng /*
16218275SEric Cheng  * Add a new unicast address to the MAC client.
16228275SEric Cheng  *
16238275SEric Cheng  * The MAC address can be specified either by value, or the MAC client
16248275SEric Cheng  * can specify that it wants to use the primary MAC address of the
16258275SEric Cheng  * underlying MAC. See the introductory comments at the beginning
16268275SEric Cheng  * of this file for more more information on primary MAC addresses.
16278275SEric Cheng  *
16288275SEric Cheng  * Note also the tuple (MAC address, VID) must be unique
16298275SEric Cheng  * for the MAC clients defined on top of the same underlying MAC
16308275SEric Cheng  * instance, unless the MAC_UNICAST_NODUPCHECK is specified.
16318275SEric Cheng  */
16328275SEric Cheng 
16338275SEric Cheng int
16348275SEric Cheng i_mac_unicast_add(mac_client_handle_t mch, uint8_t *mac_addr, uint16_t flags,
16358275SEric Cheng     mac_unicast_handle_t *mah, uint16_t vid, mac_diag_t *diag)
16368275SEric Cheng {
16378275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
16388275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
16398275SEric Cheng 	mac_unicast_impl_t *muip;
16408275SEric Cheng 	flow_entry_t *flent;
16418275SEric Cheng 	int err;
16428275SEric Cheng 	uint_t mac_len = mip->mi_type->mt_addr_length;
16438275SEric Cheng 	boolean_t check_dups = !(flags & MAC_UNICAST_NODUPCHECK);
16448275SEric Cheng 	boolean_t is_primary = (flags & MAC_UNICAST_PRIMARY);
16458400SNicolas.Droux@Sun.COM 	boolean_t is_vnic_primary = (flags & MAC_UNICAST_VNIC_PRIMARY);
16468400SNicolas.Droux@Sun.COM 	boolean_t is_unicast_hw = (flags & MAC_UNICAST_HW);
16478275SEric Cheng 	boolean_t bcast_added = B_FALSE;
16488275SEric Cheng 	boolean_t nactiveclients_added = B_FALSE;
16498275SEric Cheng 	boolean_t mac_started = B_FALSE;
16508275SEric Cheng 	mac_resource_props_t mrp;
16518275SEric Cheng 
16528275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
16538275SEric Cheng 
16548275SEric Cheng 	/* when VID is non-zero, the underlying MAC can not be VNIC */
16558275SEric Cheng 	ASSERT(!((mip->mi_state_flags & MIS_IS_VNIC) && (vid != 0)));
16568275SEric Cheng 
16578275SEric Cheng 	/*
16588275SEric Cheng 	 * Check whether it's the primary client and flag it.
16598275SEric Cheng 	 */
16608275SEric Cheng 	if (!(mcip->mci_state_flags & MCIS_IS_VNIC) && is_primary && vid == 0)
16618275SEric Cheng 		mcip->mci_flags |= MAC_CLIENT_FLAGS_PRIMARY;
16628275SEric Cheng 
16638275SEric Cheng 	/*
16648275SEric Cheng 	 * is_vnic_primary is true when we come here as a VLAN VNIC
16658275SEric Cheng 	 * which uses the primary mac client's address but with a non-zero
16668275SEric Cheng 	 * VID. In this case the MAC address is not specified by an upper
16678275SEric Cheng 	 * MAC client.
16688275SEric Cheng 	 */
16698275SEric Cheng 	if ((mcip->mci_state_flags & MCIS_IS_VNIC) && is_primary &&
16708275SEric Cheng 	    !is_vnic_primary) {
16718275SEric Cheng 		/*
16728275SEric Cheng 		 * The address is being set by the upper MAC client
16738275SEric Cheng 		 * of a VNIC. The MAC address was already set by the
16748275SEric Cheng 		 * VNIC driver during VNIC creation.
16758275SEric Cheng 		 *
16768275SEric Cheng 		 * Note: a VNIC has only one MAC address. We return
16778275SEric Cheng 		 * the MAC unicast address handle of the lower MAC client
16788275SEric Cheng 		 * corresponding to the VNIC. We allocate a new entry
16798275SEric Cheng 		 * which is flagged appropriately, so that mac_unicast_remove()
16808275SEric Cheng 		 * doesn't attempt to free the original entry that
16818275SEric Cheng 		 * was allocated by the VNIC driver.
16828275SEric Cheng 		 */
16838275SEric Cheng 		ASSERT(mcip->mci_unicast != NULL);
16848275SEric Cheng 
16858275SEric Cheng 		/*
16868275SEric Cheng 		 * Ensure that the primary unicast address of the VNIC
16878275SEric Cheng 		 * is added only once.
16888275SEric Cheng 		 */
16898275SEric Cheng 		if (mcip->mci_flags & MAC_CLIENT_FLAGS_VNIC_PRIMARY)
16908275SEric Cheng 			return (EBUSY);
16918275SEric Cheng 
16928275SEric Cheng 		mcip->mci_flags |= MAC_CLIENT_FLAGS_VNIC_PRIMARY;
16938275SEric Cheng 
16948275SEric Cheng 		/*
16958275SEric Cheng 		 * Create a handle for vid 0.
16968275SEric Cheng 		 */
16978275SEric Cheng 		ASSERT(vid == 0);
16988275SEric Cheng 		muip = kmem_zalloc(sizeof (mac_unicast_impl_t), KM_SLEEP);
16998275SEric Cheng 		muip->mui_vid = vid;
17008275SEric Cheng 		*mah = (mac_unicast_handle_t)muip;
17018275SEric Cheng 		return (0);
17028275SEric Cheng 	}
17038275SEric Cheng 
17048275SEric Cheng 	/* primary MAC clients cannot be opened on top of anchor VNICs */
17058275SEric Cheng 	if ((is_vnic_primary || is_primary) &&
17068275SEric Cheng 	    i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_ANCHOR_VNIC, NULL)) {
17078275SEric Cheng 		return (ENXIO);
17088275SEric Cheng 	}
17098275SEric Cheng 
17108275SEric Cheng 	/*
17118275SEric Cheng 	 * Return EBUSY if:
17128275SEric Cheng 	 *  - this is an exclusive active mac client and there already exist
17138275SEric Cheng 	 *    active mac clients, or
17148275SEric Cheng 	 *  - there already exist an exclusively active mac client.
17158275SEric Cheng 	 */
17168275SEric Cheng 	if ((mcip->mci_state_flags & MCIS_EXCLUSIVE) &&
17178275SEric Cheng 	    (mip->mi_nactiveclients != 0) || (mip->mi_state_flags &
17188275SEric Cheng 	    MIS_EXCLUSIVE)) {
17198275SEric Cheng 		return (EBUSY);
17208275SEric Cheng 	}
17218275SEric Cheng 
17228275SEric Cheng 	if (mcip->mci_state_flags & MCIS_EXCLUSIVE)
17238275SEric Cheng 		mip->mi_state_flags |= MIS_EXCLUSIVE;
17248275SEric Cheng 
17258275SEric Cheng 	bzero(&mrp, sizeof (mac_resource_props_t));
1726*8833SVenu.Iyer@Sun.COM 	if (is_primary && !(mcip->mci_state_flags & (MCIS_IS_VNIC |
1727*8833SVenu.Iyer@Sun.COM 	    MCIS_IS_AGGR_PORT))) {
17288275SEric Cheng 		/*
17298275SEric Cheng 		 * Apply the property cached in the mac_impl_t to the primary
1730*8833SVenu.Iyer@Sun.COM 		 * mac client. If the mac client is a VNIC or an aggregation
1731*8833SVenu.Iyer@Sun.COM 		 * port, its property should be set in the mcip when the
1732*8833SVenu.Iyer@Sun.COM 		 * VNIC/aggr was created.
17338275SEric Cheng 		 */
17348275SEric Cheng 		mac_get_resources((mac_handle_t)mip, &mrp);
17358275SEric Cheng 		(void) mac_client_set_resources(mch, &mrp);
17368275SEric Cheng 	} else if (mcip->mci_state_flags & MCIS_IS_VNIC) {
17378275SEric Cheng 		bcopy(MCIP_RESOURCE_PROPS(mcip), &mrp,
17388275SEric Cheng 		    sizeof (mac_resource_props_t));
17398275SEric Cheng 	}
17408275SEric Cheng 
17418275SEric Cheng 	muip = kmem_zalloc(sizeof (mac_unicast_impl_t), KM_SLEEP);
17428275SEric Cheng 	muip->mui_vid = vid;
17438275SEric Cheng 
17448275SEric Cheng 	if (is_primary || is_vnic_primary) {
17458275SEric Cheng 		mac_addr = mip->mi_addr;
17468275SEric Cheng 		check_dups = B_TRUE;
17478275SEric Cheng 	} else {
17488275SEric Cheng 
17498275SEric Cheng 		/*
17508275SEric Cheng 		 * Verify the validity of the specified MAC addresses value.
17518275SEric Cheng 		 */
17528275SEric Cheng 		if (!mac_unicst_verify((mac_handle_t)mip, mac_addr, mac_len)) {
17538275SEric Cheng 			*diag = MAC_DIAG_MACADDR_INVALID;
17548275SEric Cheng 			err = EINVAL;
17558275SEric Cheng 			goto bail;
17568275SEric Cheng 		}
17578275SEric Cheng 
17588275SEric Cheng 		/*
17598275SEric Cheng 		 * Make sure that the specified MAC address is different
17608275SEric Cheng 		 * than the unicast MAC address of the underlying NIC.
17618275SEric Cheng 		 */
17628275SEric Cheng 		if (check_dups && bcmp(mip->mi_addr, mac_addr, mac_len) == 0) {
17638275SEric Cheng 			*diag = MAC_DIAG_MACADDR_NIC;
17648275SEric Cheng 			err = EINVAL;
17658275SEric Cheng 			goto bail;
17668275SEric Cheng 		}
17678275SEric Cheng 	}
17688275SEric Cheng 
17698275SEric Cheng 	/*
17708275SEric Cheng 	 * Make sure the MAC address is not already used by
17718275SEric Cheng 	 * another MAC client defined on top of the same
17728275SEric Cheng 	 * underlying NIC.
17738275SEric Cheng 	 * xxx-venu mac_unicast_add doesnt' seem to be called
17748275SEric Cheng 	 * with MAC_UNICAST_NODUPCHECK currently, if it does
17758275SEric Cheng 	 * get called we need to do mac_addr_in_use() just
17768275SEric Cheng 	 * to check for addr_in_use till 6697876 is fixed.
17778275SEric Cheng 	 */
17788275SEric Cheng 	if (check_dups && mac_addr_in_use(mip, mac_addr, vid)) {
17798275SEric Cheng 		*diag = MAC_DIAG_MACADDR_INUSE;
17808275SEric Cheng 		err = EEXIST;
17818275SEric Cheng 		goto bail;
17828275SEric Cheng 	}
17838275SEric Cheng 
17848275SEric Cheng 	if ((err = mac_start(mip)) != 0)
17858275SEric Cheng 		goto bail;
17868275SEric Cheng 
17878275SEric Cheng 	mac_started = B_TRUE;
17888275SEric Cheng 
17898275SEric Cheng 	/* add the MAC client to the broadcast address group by default */
17908275SEric Cheng 	if (mip->mi_type->mt_brdcst_addr != NULL) {
17918275SEric Cheng 		err = mac_bcast_add(mcip, mip->mi_type->mt_brdcst_addr, vid,
17928275SEric Cheng 		    MAC_ADDRTYPE_BROADCAST);
17938275SEric Cheng 		if (err != 0)
17948275SEric Cheng 			goto bail;
17958275SEric Cheng 		bcast_added = B_TRUE;
17968275SEric Cheng 	}
1797*8833SVenu.Iyer@Sun.COM 
1798*8833SVenu.Iyer@Sun.COM 	/*
1799*8833SVenu.Iyer@Sun.COM 	 * If this is the first unicast address addition for this
1800*8833SVenu.Iyer@Sun.COM 	 * client, reuse the pre-allocated larval flow entry associated with
1801*8833SVenu.Iyer@Sun.COM 	 * the MAC client.
1802*8833SVenu.Iyer@Sun.COM 	 */
1803*8833SVenu.Iyer@Sun.COM 	flent = (mcip->mci_nflents == 0) ? mcip->mci_flent : NULL;
18048400SNicolas.Droux@Sun.COM 
18058275SEric Cheng 	/* We are configuring the unicast flow now */
18068275SEric Cheng 	if (!MCIP_DATAPATH_SETUP(mcip)) {
18078400SNicolas.Droux@Sun.COM 		if (is_unicast_hw) {
18088400SNicolas.Droux@Sun.COM 			/*
18098400SNicolas.Droux@Sun.COM 			 * The client requires a hardware MAC address slot
18108400SNicolas.Droux@Sun.COM 			 * for that unicast address. Since we support only
18118400SNicolas.Droux@Sun.COM 			 * one unicast MAC address per client, flag the
18128400SNicolas.Droux@Sun.COM 			 * MAC client itself.
18138400SNicolas.Droux@Sun.COM 			 */
18148400SNicolas.Droux@Sun.COM 			mcip->mci_state_flags |= MCIS_UNICAST_HW;
18158400SNicolas.Droux@Sun.COM 		}
18168275SEric Cheng 
18178275SEric Cheng 		MAC_CLIENT_SET_PRIORITY_RANGE(mcip,
18188275SEric Cheng 		    (mrp.mrp_mask & MRP_PRIORITY) ? mrp.mrp_priority :
18198275SEric Cheng 		    MPL_LINK_DEFAULT);
18208275SEric Cheng 
18218275SEric Cheng 		if ((err = mac_unicast_flow_create(mcip, mac_addr, vid,
18228275SEric Cheng 		    is_primary || is_vnic_primary, B_TRUE, &flent, &mrp)) != 0)
18238275SEric Cheng 			goto bail;
18248275SEric Cheng 
18258275SEric Cheng 		mip->mi_nactiveclients++;
18268275SEric Cheng 		nactiveclients_added = B_TRUE;
1827*8833SVenu.Iyer@Sun.COM 
18288275SEric Cheng 		/*
18298275SEric Cheng 		 * This will allocate the RX ring group if possible for the
18308275SEric Cheng 		 * flow and program the software classifier as needed.
18318275SEric Cheng 		 */
18328275SEric Cheng 		if ((err = mac_datapath_setup(mcip, flent, SRST_LINK)) != 0)
18338275SEric Cheng 			goto bail;
18348275SEric Cheng 
18358275SEric Cheng 		/*
18368275SEric Cheng 		 * The unicast MAC address must have been added successfully.
18378275SEric Cheng 		 */
18388275SEric Cheng 		ASSERT(mcip->mci_unicast != NULL);
1839*8833SVenu.Iyer@Sun.COM 		/*
1840*8833SVenu.Iyer@Sun.COM 		 * Push down the sub-flows that were defined on this link
1841*8833SVenu.Iyer@Sun.COM 		 * hitherto. The flows are added to the active flow table
1842*8833SVenu.Iyer@Sun.COM 		 * and SRS, softrings etc. are created as needed.
1843*8833SVenu.Iyer@Sun.COM 		 */
1844*8833SVenu.Iyer@Sun.COM 		mac_link_init_flows(mch);
18458275SEric Cheng 	} else {
18468275SEric Cheng 		mac_address_t *map = mcip->mci_unicast;
18478275SEric Cheng 
18488275SEric Cheng 		/*
18498275SEric Cheng 		 * A unicast flow already exists for that MAC client,
18508275SEric Cheng 		 * this flow must be the same mac address but with
18518275SEric Cheng 		 * different VID. It has been checked by mac_addr_in_use().
18528275SEric Cheng 		 *
18538275SEric Cheng 		 * We will use the SRS etc. from the mci_flent. Note that
18548275SEric Cheng 		 * We don't need to create kstat for this as except for
18558275SEric Cheng 		 * the fdesc, everything will be used from in the 1st flent.
18568275SEric Cheng 		 */
18578275SEric Cheng 
18588275SEric Cheng 		if (bcmp(mac_addr, map->ma_addr, map->ma_len) != 0) {
18598275SEric Cheng 			err = EINVAL;
18608275SEric Cheng 			goto bail;
18618275SEric Cheng 		}
18628275SEric Cheng 
18638400SNicolas.Droux@Sun.COM 		/*
18648400SNicolas.Droux@Sun.COM 		 * Make sure the client is consistent about its requests
18658400SNicolas.Droux@Sun.COM 		 * for MAC addresses. I.e. all requests from the clients
18668400SNicolas.Droux@Sun.COM 		 * must have the MAC_UNICAST_HW flag set or clear.
18678400SNicolas.Droux@Sun.COM 		 */
18688400SNicolas.Droux@Sun.COM 		if ((mcip->mci_state_flags & MCIS_UNICAST_HW) != 0 &&
18698400SNicolas.Droux@Sun.COM 		    !is_unicast_hw ||
18708400SNicolas.Droux@Sun.COM 		    (mcip->mci_state_flags & MCIS_UNICAST_HW) == 0 &&
18718400SNicolas.Droux@Sun.COM 		    is_unicast_hw) {
18728400SNicolas.Droux@Sun.COM 			err = EINVAL;
18738400SNicolas.Droux@Sun.COM 			goto bail;
18748400SNicolas.Droux@Sun.COM 		}
18758400SNicolas.Droux@Sun.COM 
18768275SEric Cheng 		if ((err = mac_unicast_flow_create(mcip, mac_addr, vid,
18778275SEric Cheng 		    is_primary || is_vnic_primary, B_FALSE, &flent, NULL)) != 0)
18788275SEric Cheng 			goto bail;
18798275SEric Cheng 
18808275SEric Cheng 		if ((err = mac_flow_add(mip->mi_flow_tab, flent)) != 0) {
18818275SEric Cheng 			FLOW_FINAL_REFRELE(flent);
18828275SEric Cheng 			goto bail;
18838275SEric Cheng 		}
18848275SEric Cheng 
18858275SEric Cheng 		/* update the multicast group for this vid */
18868275SEric Cheng 		mac_client_bcast_refresh(mcip, mac_client_update_mcast,
18878275SEric Cheng 		    (void *)flent, B_TRUE);
18888275SEric Cheng 
18898275SEric Cheng 	}
18908275SEric Cheng 
18918275SEric Cheng 	/* populate the shared MAC address */
18928275SEric Cheng 	muip->mui_map = mcip->mci_unicast;
18938275SEric Cheng 
18948275SEric Cheng 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
18958275SEric Cheng 	muip->mui_next = mcip->mci_unicast_list;
18968275SEric Cheng 	mcip->mci_unicast_list = muip;
18978275SEric Cheng 	rw_exit(&mcip->mci_rw_lock);
18988275SEric Cheng 
1899*8833SVenu.Iyer@Sun.COM 	if (nactiveclients_added)
1900*8833SVenu.Iyer@Sun.COM 		mac_update_single_active_client(mip);
1901*8833SVenu.Iyer@Sun.COM 
19028275SEric Cheng 	*mah = (mac_unicast_handle_t)muip;
19038275SEric Cheng 
19048275SEric Cheng 	/* add it to the flow list of this mcip */
19058275SEric Cheng 	mac_client_add_to_flow_list(mcip, flent);
19068275SEric Cheng 
19078275SEric Cheng 	/*
19088275SEric Cheng 	 * Trigger a renegotiation of the capabilities when the number of
19098275SEric Cheng 	 * active clients changes from 1 to 2, since some of the capabilities
19108275SEric Cheng 	 * might have to be disabled. Also send a MAC_NOTE_LINK notification
19118275SEric Cheng 	 * to all the MAC clients whenever physical link is DOWN.
19128275SEric Cheng 	 */
19138275SEric Cheng 	if (mip->mi_nactiveclients == 2) {
19148275SEric Cheng 		mac_capab_update((mac_handle_t)mip);
19158275SEric Cheng 		mac_virtual_link_update(mip);
19168275SEric Cheng 	}
19178275SEric Cheng 	/*
19188275SEric Cheng 	 * Now that the setup is complete, clear the INCIPIENT flag.
19198275SEric Cheng 	 * The flag was set to avoid incoming packets seeing inconsistent
19208275SEric Cheng 	 * structures while the setup was in progress. Clear the mci_tx_flag
19218275SEric Cheng 	 * by calling mac_tx_client_block. It is possible that
19228275SEric Cheng 	 * mac_unicast_remove was called prior to this mac_unicast_add which
19238275SEric Cheng 	 * could have set the MCI_TX_QUIESCE flag.
19248275SEric Cheng 	 */
19258275SEric Cheng 	if (flent->fe_rx_ring_group != NULL)
19268275SEric Cheng 		mac_rx_group_unmark(flent->fe_rx_ring_group, MR_INCIPIENT);
19278275SEric Cheng 	FLOW_UNMARK(flent, FE_INCIPIENT);
19288275SEric Cheng 	FLOW_UNMARK(flent, FE_MC_NO_DATAPATH);
19298275SEric Cheng 	mac_tx_client_unblock(mcip);
19308275SEric Cheng 	return (0);
19318275SEric Cheng bail:
19328275SEric Cheng 	if (bcast_added)
19338275SEric Cheng 		mac_bcast_delete(mcip, mip->mi_type->mt_brdcst_addr, vid);
19348275SEric Cheng 	if (mac_started)
19358275SEric Cheng 		mac_stop(mip);
19368275SEric Cheng 
1937*8833SVenu.Iyer@Sun.COM 	if (nactiveclients_added) {
19388275SEric Cheng 		mip->mi_nactiveclients--;
1939*8833SVenu.Iyer@Sun.COM 		mac_update_single_active_client(mip);
1940*8833SVenu.Iyer@Sun.COM 	}
1941*8833SVenu.Iyer@Sun.COM 
19428275SEric Cheng 	if (mcip->mci_state_flags & MCIS_EXCLUSIVE)
19438275SEric Cheng 		mip->mi_state_flags &= ~MIS_EXCLUSIVE;
19448275SEric Cheng 	kmem_free(muip, sizeof (mac_unicast_impl_t));
19458275SEric Cheng 	return (err);
19468275SEric Cheng }
19478275SEric Cheng 
19488275SEric Cheng int
19498275SEric Cheng mac_unicast_add(mac_client_handle_t mch, uint8_t *mac_addr, uint16_t flags,
19508275SEric Cheng     mac_unicast_handle_t *mah, uint16_t vid, mac_diag_t *diag)
19518275SEric Cheng {
19528275SEric Cheng 	mac_impl_t *mip = ((mac_client_impl_t *)mch)->mci_mip;
19538275SEric Cheng 	uint_t err;
19548275SEric Cheng 
19558275SEric Cheng 	i_mac_perim_enter(mip);
19568275SEric Cheng 	err = i_mac_unicast_add(mch, mac_addr, flags, mah, vid, diag);
19578275SEric Cheng 	i_mac_perim_exit(mip);
19588275SEric Cheng 
19598275SEric Cheng 	return (err);
19608275SEric Cheng }
19618275SEric Cheng 
19628275SEric Cheng /*
19638275SEric Cheng  * Add the primary MAC address to the MAC client. This is a convenience
19648275SEric Cheng  * function which can be called by primary MAC clients which do not
19658275SEric Cheng  * need to specify any other additional flags.
19668275SEric Cheng  *
19678275SEric Cheng  * It's called in one of following situations:
19688275SEric Cheng  *   * dls as the primary MAC client
19698275SEric Cheng  *   * aggr as an exclusive client
19708275SEric Cheng  *   * by VNIC's client
19718275SEric Cheng  */
19728275SEric Cheng int
19738275SEric Cheng mac_unicast_primary_add(mac_client_handle_t mch, mac_unicast_handle_t *mah,
19748275SEric Cheng     mac_diag_t *diag)
19758275SEric Cheng {
19768275SEric Cheng 	return (mac_unicast_add(mch, NULL, MAC_UNICAST_PRIMARY, mah, 0, diag));
19778275SEric Cheng }
19788275SEric Cheng 
19798275SEric Cheng /*
19808275SEric Cheng  * Remove a MAC address which was previously added by mac_unicast_add().
19818275SEric Cheng  */
19828275SEric Cheng int
19838275SEric Cheng mac_unicast_remove(mac_client_handle_t mch, mac_unicast_handle_t mah)
19848275SEric Cheng {
19858275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
19868275SEric Cheng 	mac_unicast_impl_t *muip = (mac_unicast_impl_t *)mah;
19878275SEric Cheng 	mac_unicast_impl_t *pre;
19888275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
19898275SEric Cheng 	flow_entry_t *flent;
19908275SEric Cheng 
19918275SEric Cheng 	i_mac_perim_enter(mip);
19928275SEric Cheng 	if (mcip->mci_flags & MAC_CLIENT_FLAGS_VNIC_PRIMARY) {
19938275SEric Cheng 		/*
19948275SEric Cheng 		 * Called made by the upper MAC client of a VNIC.
19958275SEric Cheng 		 * There's nothing much to do, the unicast address will
19968275SEric Cheng 		 * be removed by the VNIC driver when the VNIC is deleted,
19978275SEric Cheng 		 * but let's ensure that all our transmit is done before
19988275SEric Cheng 		 * the client does a mac_client_stop lest it trigger an
19998275SEric Cheng 		 * assert in the driver.
20008275SEric Cheng 		 */
20018275SEric Cheng 		ASSERT(muip->mui_vid == 0);
20028275SEric Cheng 
20038275SEric Cheng 		mac_tx_client_flush(mcip);
20048275SEric Cheng 		mcip->mci_flags &= ~MAC_CLIENT_FLAGS_VNIC_PRIMARY;
20058275SEric Cheng 
20068275SEric Cheng 		kmem_free(muip, sizeof (mac_unicast_impl_t));
20078275SEric Cheng 		i_mac_perim_exit(mip);
20088275SEric Cheng 		return (0);
20098275SEric Cheng 	}
20108275SEric Cheng 
20118275SEric Cheng 	ASSERT(muip != NULL);
20128275SEric Cheng 
20138275SEric Cheng 	/*
20148275SEric Cheng 	 * Remove the VID from the list of client's VIDs.
20158275SEric Cheng 	 */
20168275SEric Cheng 	pre = mcip->mci_unicast_list;
2017*8833SVenu.Iyer@Sun.COM 	if (muip == pre) {
20188275SEric Cheng 		mcip->mci_unicast_list = muip->mui_next;
2019*8833SVenu.Iyer@Sun.COM 	} else {
20208275SEric Cheng 		while ((pre->mui_next != NULL) && (pre->mui_next != muip))
20218275SEric Cheng 			pre = pre->mui_next;
20228275SEric Cheng 		ASSERT(pre->mui_next == muip);
20238275SEric Cheng 		rw_enter(&mcip->mci_rw_lock, RW_WRITER);
20248275SEric Cheng 		pre->mui_next = muip->mui_next;
20258275SEric Cheng 		rw_exit(&mcip->mci_rw_lock);
20268275SEric Cheng 	}
20278275SEric Cheng 
20288275SEric Cheng 	if ((mcip->mci_flags & MAC_CLIENT_FLAGS_PRIMARY) && muip->mui_vid == 0)
20298275SEric Cheng 		mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PRIMARY;
20308275SEric Cheng 
20318275SEric Cheng 	if (!mac_client_single_rcvr(mcip)) {
2032*8833SVenu.Iyer@Sun.COM 		/*
2033*8833SVenu.Iyer@Sun.COM 		 * This MAC client is shared by more than one unicast
2034*8833SVenu.Iyer@Sun.COM 		 * addresses, so we will just remove the flent
2035*8833SVenu.Iyer@Sun.COM 		 * corresponding to the address being removed. We don't invoke
2036*8833SVenu.Iyer@Sun.COM 		 * mac_rx_classify_flow_rem() since the additional flow is
2037*8833SVenu.Iyer@Sun.COM 		 * not associated with its own separate set of SRS and rings,
2038*8833SVenu.Iyer@Sun.COM 		 * and these constructs are still needed for the remaining
2039*8833SVenu.Iyer@Sun.COM 		 * flows.
2040*8833SVenu.Iyer@Sun.COM 		 */
20418275SEric Cheng 		flent = mac_client_get_flow(mcip, muip);
20428275SEric Cheng 		ASSERT(flent != NULL);
20438275SEric Cheng 
20448275SEric Cheng 		/*
20458275SEric Cheng 		 * The first one is disappearing, need to make sure
20468275SEric Cheng 		 * we replace it with another from the list of
20478275SEric Cheng 		 * shared clients.
20488275SEric Cheng 		 */
20498275SEric Cheng 		if (flent == mcip->mci_flent)
20508275SEric Cheng 			flent = mac_client_swap_mciflent(mcip);
20518275SEric Cheng 		mac_client_remove_flow_from_list(mcip, flent);
20528275SEric Cheng 		mac_flow_remove(mip->mi_flow_tab, flent, B_FALSE);
20538275SEric Cheng 		mac_flow_wait(flent, FLOW_DRIVER_UPCALL);
20548275SEric Cheng 
20558275SEric Cheng 		/*
20568275SEric Cheng 		 * The multicast groups that were added by the client so
20578275SEric Cheng 		 * far must be removed from the brodcast domain corresponding
20588275SEric Cheng 		 * to the VID being removed.
20598275SEric Cheng 		 */
20608275SEric Cheng 		mac_client_bcast_refresh(mcip, mac_client_update_mcast,
20618275SEric Cheng 		    (void *)flent, B_FALSE);
20628275SEric Cheng 
20638275SEric Cheng 		if (mip->mi_type->mt_brdcst_addr != NULL) {
20648275SEric Cheng 			mac_bcast_delete(mcip, mip->mi_type->mt_brdcst_addr,
20658275SEric Cheng 			    muip->mui_vid);
20668275SEric Cheng 		}
20678275SEric Cheng 		mac_stop(mip);
20688275SEric Cheng 		FLOW_FINAL_REFRELE(flent);
20698275SEric Cheng 		i_mac_perim_exit(mip);
20708275SEric Cheng 		return (0);
20718275SEric Cheng 	}
20728275SEric Cheng 
2073*8833SVenu.Iyer@Sun.COM 	/*
2074*8833SVenu.Iyer@Sun.COM 	 * We would have initialized subflows etc. only if we brought up
2075*8833SVenu.Iyer@Sun.COM 	 * the primary client and set the unicast unicast address etc.
2076*8833SVenu.Iyer@Sun.COM 	 * Deactivate the flows. The flow entry will be removed from the
2077*8833SVenu.Iyer@Sun.COM 	 * active flow tables, and the associated SRS, softrings etc will
2078*8833SVenu.Iyer@Sun.COM 	 * be deleted. But the flow entry itself won't be destroyed, instead
2079*8833SVenu.Iyer@Sun.COM 	 * it will continue to be archived off the  the global flow hash
2080*8833SVenu.Iyer@Sun.COM 	 * list, for a possible future activation when say IP is plumbed
2081*8833SVenu.Iyer@Sun.COM 	 * again.
2082*8833SVenu.Iyer@Sun.COM 	 */
2083*8833SVenu.Iyer@Sun.COM 	mac_link_release_flows(mch);
2084*8833SVenu.Iyer@Sun.COM 
20858275SEric Cheng 	mip->mi_nactiveclients--;
2086*8833SVenu.Iyer@Sun.COM 	mac_update_single_active_client(mip);
20878275SEric Cheng 
20888275SEric Cheng 	/* Tear down the Data path */
20898275SEric Cheng 	mac_datapath_teardown(mcip, mcip->mci_flent, SRST_LINK);
20908275SEric Cheng 
20918275SEric Cheng 	/*
20928275SEric Cheng 	 * Prevent any future access to the flow entry through the mci_flent
20938275SEric Cheng 	 * pointer by setting the mci_flent to NULL. Access to mci_flent in
20948275SEric Cheng 	 * mac_bcast_send is also under mi_rw_lock.
20958275SEric Cheng 	 */
20968275SEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
20978275SEric Cheng 	flent = mcip->mci_flent;
20988275SEric Cheng 	mac_client_remove_flow_from_list(mcip, flent);
20998275SEric Cheng 
21008275SEric Cheng 	if (mcip->mci_state_flags & MCIS_DESC_LOGGED)
21018275SEric Cheng 		mcip->mci_state_flags &= ~MCIS_DESC_LOGGED;
21028275SEric Cheng 
21038275SEric Cheng 	/*
21048275SEric Cheng 	 * This is the last unicast address being removed and there shouldn't
21058275SEric Cheng 	 * be any outbound data threads at this point coming down from mac
21068275SEric Cheng 	 * clients. We have waited for the data threads to finish before
21078275SEric Cheng 	 * starting dld_str_detach. Non-data threads must access TX SRS
21088275SEric Cheng 	 * under mi_rw_lock.
21098275SEric Cheng 	 */
21108275SEric Cheng 	rw_exit(&mip->mi_rw_lock);
21118275SEric Cheng 
21128275SEric Cheng 	/*
21138275SEric Cheng 	 * Update the multicast group for this vid.
21148275SEric Cheng 	 */
21158275SEric Cheng 	mac_client_bcast_refresh(mcip, mac_client_update_mcast, (void *)flent,
21168275SEric Cheng 	    B_FALSE);
21178275SEric Cheng 
21188275SEric Cheng 	/*
21198275SEric Cheng 	 * Don't use FLOW_MARK with FE_MC_NO_DATAPATH, as the flow might
21208275SEric Cheng 	 * contain other flags, such as FE_CONDEMNED, which we need to
21218275SEric Cheng 	 * cleared. We don't call mac_flow_cleanup() for this unicast
21228275SEric Cheng 	 * flow as we have a already cleaned up SRSs etc. (via the teadown
21238275SEric Cheng 	 * path). We just clear the stats and reset the initial callback
21248275SEric Cheng 	 * function, the rest will be set when we call mac_flow_create,
21258275SEric Cheng 	 * if at all.
21268275SEric Cheng 	 */
21278275SEric Cheng 	mutex_enter(&flent->fe_lock);
21288275SEric Cheng 	ASSERT(flent->fe_refcnt == 1 && flent->fe_mbg == NULL &&
21298275SEric Cheng 	    flent->fe_tx_srs == NULL && flent->fe_rx_srs_cnt == 0);
21308275SEric Cheng 	flent->fe_flags = FE_MC_NO_DATAPATH;
21318275SEric Cheng 	flow_stat_destroy(flent);
21328275SEric Cheng 
21338275SEric Cheng 	/* Initialize the receiver function to a safe routine */
21348275SEric Cheng 	flent->fe_cb_fn = (flow_fn_t)mac_pkt_drop;
21358275SEric Cheng 	flent->fe_cb_arg1 = NULL;
21368275SEric Cheng 	flent->fe_cb_arg2 = NULL;
21378275SEric Cheng 
21388275SEric Cheng 	flent->fe_index = -1;
21398275SEric Cheng 	mutex_exit(&flent->fe_lock);
21408275SEric Cheng 
21418275SEric Cheng 	if (mip->mi_type->mt_brdcst_addr != NULL) {
21428275SEric Cheng 		mac_bcast_delete(mcip, mip->mi_type->mt_brdcst_addr,
21438275SEric Cheng 		    muip->mui_vid);
21448275SEric Cheng 	}
21458275SEric Cheng 
21468275SEric Cheng 	if (mip->mi_nactiveclients == 1) {
21478275SEric Cheng 		mac_capab_update((mac_handle_t)mip);
21488275SEric Cheng 		mac_virtual_link_update(mip);
21498275SEric Cheng 	}
21508275SEric Cheng 	if (mcip->mci_state_flags & MCIS_EXCLUSIVE)
21518275SEric Cheng 		mip->mi_state_flags &= ~MIS_EXCLUSIVE;
21528400SNicolas.Droux@Sun.COM 	mcip->mci_state_flags &= ~MCIS_UNICAST_HW;
21538275SEric Cheng 
21548275SEric Cheng 	mac_stop(mip);
21558275SEric Cheng 
21568275SEric Cheng 	i_mac_perim_exit(mip);
21578275SEric Cheng 	kmem_free(muip, sizeof (mac_unicast_impl_t));
21588275SEric Cheng 	return (0);
21598275SEric Cheng }
21608275SEric Cheng 
21618275SEric Cheng /*
21628275SEric Cheng  * Multicast add function invoked by MAC clients.
21638275SEric Cheng  */
21648275SEric Cheng int
21658275SEric Cheng mac_multicast_add(mac_client_handle_t mch, const uint8_t *addr)
21668275SEric Cheng {
21678275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
21688275SEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
21698275SEric Cheng 	flow_entry_t		*flent = mcip->mci_flent_list;
21708275SEric Cheng 	flow_entry_t		*prev_fe = NULL;
21718275SEric Cheng 	uint16_t		vid;
21728275SEric Cheng 	int			err = 0;
21738275SEric Cheng 
21748275SEric Cheng 	/* Verify the address is a valid multicast address */
21758275SEric Cheng 	if ((err = mip->mi_type->mt_ops.mtops_multicst_verify(addr,
21768275SEric Cheng 	    mip->mi_pdata)) != 0)
21778275SEric Cheng 		return (err);
21788275SEric Cheng 
21798275SEric Cheng 	i_mac_perim_enter(mip);
21808275SEric Cheng 	while (flent != NULL) {
21818275SEric Cheng 		vid = i_mac_flow_vid(flent);
21828275SEric Cheng 
21838275SEric Cheng 		err = mac_bcast_add((mac_client_impl_t *)mch, addr, vid,
21848275SEric Cheng 		    MAC_ADDRTYPE_MULTICAST);
21858275SEric Cheng 		if (err != 0)
21868275SEric Cheng 			break;
21878275SEric Cheng 		prev_fe = flent;
21888275SEric Cheng 		flent = flent->fe_client_next;
21898275SEric Cheng 	}
21908275SEric Cheng 
21918275SEric Cheng 	/*
21928275SEric Cheng 	 * If we failed adding, then undo all, rather than partial
21938275SEric Cheng 	 * success.
21948275SEric Cheng 	 */
21958275SEric Cheng 	if (flent != NULL && prev_fe != NULL) {
21968275SEric Cheng 		flent = mcip->mci_flent_list;
21978275SEric Cheng 		while (flent != prev_fe->fe_client_next) {
21988275SEric Cheng 			vid = i_mac_flow_vid(flent);
21998275SEric Cheng 			mac_bcast_delete((mac_client_impl_t *)mch, addr, vid);
22008275SEric Cheng 			flent = flent->fe_client_next;
22018275SEric Cheng 		}
22028275SEric Cheng 	}
22038275SEric Cheng 	i_mac_perim_exit(mip);
22048275SEric Cheng 	return (err);
22058275SEric Cheng }
22068275SEric Cheng 
22078275SEric Cheng /*
22088275SEric Cheng  * Multicast delete function invoked by MAC clients.
22098275SEric Cheng  */
22108275SEric Cheng void
22118275SEric Cheng mac_multicast_remove(mac_client_handle_t mch, const uint8_t *addr)
22128275SEric Cheng {
22138275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
22148275SEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
22158275SEric Cheng 	flow_entry_t		*flent;
22168275SEric Cheng 	uint16_t		vid;
22178275SEric Cheng 
22188275SEric Cheng 	i_mac_perim_enter(mip);
22198275SEric Cheng 	for (flent = mcip->mci_flent_list; flent != NULL;
22208275SEric Cheng 	    flent = flent->fe_client_next) {
22218275SEric Cheng 		vid = i_mac_flow_vid(flent);
22228275SEric Cheng 		mac_bcast_delete((mac_client_impl_t *)mch, addr, vid);
22238275SEric Cheng 	}
22248275SEric Cheng 	i_mac_perim_exit(mip);
22258275SEric Cheng }
22268275SEric Cheng 
22278275SEric Cheng /*
22288275SEric Cheng  * When a MAC client desires to capture packets on an interface,
22298275SEric Cheng  * it registers a promiscuous call back with mac_promisc_add().
22308275SEric Cheng  * There are three types of promiscuous callbacks:
22318275SEric Cheng  *
22328275SEric Cheng  * * MAC_CLIENT_PROMISC_ALL
22338275SEric Cheng  *   Captures all packets sent and received by the MAC client,
22348275SEric Cheng  *   the physical interface, as well as all other MAC clients
22358275SEric Cheng  *   defined on top of the same MAC.
22368275SEric Cheng  *
22378275SEric Cheng  * * MAC_CLIENT_PROMISC_FILTERED
22388275SEric Cheng  *   Captures all packets sent and received by the MAC client,
22398275SEric Cheng  *   plus all multicast traffic sent and received by the phyisical
22408275SEric Cheng  *   interface and the other MAC clients.
22418275SEric Cheng  *
22428275SEric Cheng  * * MAC_CLIENT_PROMISC_MULTI
22438275SEric Cheng  *   Captures all broadcast and multicast packets sent and
22448275SEric Cheng  *   received by the MAC clients as well as the physical interface.
22458275SEric Cheng  *
22468275SEric Cheng  * In all cases, the underlying MAC is put in promiscuous mode.
22478275SEric Cheng  */
22488275SEric Cheng int
22498275SEric Cheng mac_promisc_add(mac_client_handle_t mch, mac_client_promisc_type_t type,
22508275SEric Cheng     mac_rx_t fn, void *arg, mac_promisc_handle_t *mphp, uint16_t flags)
22518275SEric Cheng {
22528275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
22538275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
22548275SEric Cheng 	mac_promisc_impl_t *mpip;
22558275SEric Cheng 	mac_cb_info_t	*mcbi;
22568275SEric Cheng 	int rc;
22578275SEric Cheng 
22588275SEric Cheng 	i_mac_perim_enter(mip);
22598275SEric Cheng 
22608275SEric Cheng 	if ((rc = mac_start(mip)) != 0) {
22618275SEric Cheng 		i_mac_perim_exit(mip);
22628275SEric Cheng 		return (rc);
22638275SEric Cheng 	}
22648275SEric Cheng 
22658275SEric Cheng 	if ((mcip->mci_state_flags & MCIS_IS_VNIC) &&
22668275SEric Cheng 	    type == MAC_CLIENT_PROMISC_ALL) {
22678275SEric Cheng 		/*
22688275SEric Cheng 		 * The function is being invoked by the upper MAC client
22698275SEric Cheng 		 * of a VNIC. The VNIC should only see the traffic
22708275SEric Cheng 		 * it is entitled to.
22718275SEric Cheng 		 */
22728275SEric Cheng 		type = MAC_CLIENT_PROMISC_FILTERED;
22738275SEric Cheng 	}
22748275SEric Cheng 
22758275SEric Cheng 
22768275SEric Cheng 	/*
22778275SEric Cheng 	 * Turn on promiscuous mode for the underlying NIC.
22788275SEric Cheng 	 * This is needed even for filtered callbacks which
22798275SEric Cheng 	 * expect to receive all multicast traffic on the wire.
22808275SEric Cheng 	 *
22818275SEric Cheng 	 * Physical promiscuous mode should not be turned on if
22828275SEric Cheng 	 * MAC_PROMISC_FLAGS_NO_PHYS is set.
22838275SEric Cheng 	 */
22848275SEric Cheng 	if ((flags & MAC_PROMISC_FLAGS_NO_PHYS) == 0) {
22858275SEric Cheng 		if ((rc = i_mac_promisc_set(mip, B_TRUE, MAC_DEVPROMISC))
22868275SEric Cheng 		    != 0) {
22878275SEric Cheng 			mac_stop(mip);
22888275SEric Cheng 			i_mac_perim_exit(mip);
22898275SEric Cheng 			return (rc);
22908275SEric Cheng 		}
22918275SEric Cheng 	}
22928275SEric Cheng 
22938275SEric Cheng 	mpip = kmem_cache_alloc(mac_promisc_impl_cache, KM_SLEEP);
22948275SEric Cheng 
22958275SEric Cheng 	mpip->mpi_type = type;
22968275SEric Cheng 	mpip->mpi_fn = fn;
22978275SEric Cheng 	mpip->mpi_arg = arg;
22988275SEric Cheng 	mpip->mpi_mcip = mcip;
22998275SEric Cheng 	mpip->mpi_no_tx_loop = ((flags & MAC_PROMISC_FLAGS_NO_TX_LOOP) != 0);
23008275SEric Cheng 	mpip->mpi_no_phys = ((flags & MAC_PROMISC_FLAGS_NO_PHYS) != 0);
2301*8833SVenu.Iyer@Sun.COM 	mpip->mpi_strip_vlan_tag =
2302*8833SVenu.Iyer@Sun.COM 	    ((flags & MAC_PROMISC_FLAGS_VLAN_TAG_STRIP) != 0);
23038275SEric Cheng 
23048275SEric Cheng 	mcbi = &mip->mi_promisc_cb_info;
23058275SEric Cheng 	mutex_enter(mcbi->mcbi_lockp);
23068275SEric Cheng 
23078275SEric Cheng 	mac_callback_add(&mip->mi_promisc_cb_info, &mcip->mci_promisc_list,
23088275SEric Cheng 	    &mpip->mpi_mci_link);
23098275SEric Cheng 	mac_callback_add(&mip->mi_promisc_cb_info, &mip->mi_promisc_list,
23108275SEric Cheng 	    &mpip->mpi_mi_link);
23118275SEric Cheng 
23128275SEric Cheng 	mutex_exit(mcbi->mcbi_lockp);
23138275SEric Cheng 
23148275SEric Cheng 	*mphp = (mac_promisc_handle_t)mpip;
23158275SEric Cheng 	i_mac_perim_exit(mip);
23168275SEric Cheng 	return (0);
23178275SEric Cheng }
23188275SEric Cheng 
23198275SEric Cheng /*
23208275SEric Cheng  * Remove a multicast address previously aded through mac_promisc_add().
23218275SEric Cheng  */
23228275SEric Cheng int
23238275SEric Cheng mac_promisc_remove(mac_promisc_handle_t mph)
23248275SEric Cheng {
23258275SEric Cheng 	mac_promisc_impl_t *mpip = (mac_promisc_impl_t *)mph;
23268275SEric Cheng 	mac_client_impl_t *mcip = mpip->mpi_mcip;
23278275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
23288275SEric Cheng 	mac_cb_info_t *mcbi;
23298275SEric Cheng 	int rc = 0;
23308275SEric Cheng 
23318275SEric Cheng 	i_mac_perim_enter(mip);
23328275SEric Cheng 
23338275SEric Cheng 	/*
23348275SEric Cheng 	 * Even if the device can't be reset into normal mode, we still
23358275SEric Cheng 	 * need to clear the client promisc callbacks. The client may want
23368275SEric Cheng 	 * to close the mac end point and we can't have stale callbacks.
23378275SEric Cheng 	 */
23388275SEric Cheng 	if (!(mpip->mpi_no_phys)) {
23398275SEric Cheng 		rc = mac_promisc_set((mac_handle_t)mip, B_FALSE,
23408275SEric Cheng 		    MAC_DEVPROMISC);
23418275SEric Cheng 		if (rc != 0)
23428275SEric Cheng 			goto done;
23438275SEric Cheng 	}
23448275SEric Cheng 	mcbi = &mip->mi_promisc_cb_info;
23458275SEric Cheng 	mutex_enter(mcbi->mcbi_lockp);
23468275SEric Cheng 	if (mac_callback_remove(mcbi, &mip->mi_promisc_list,
23478275SEric Cheng 	    &mpip->mpi_mi_link)) {
23488275SEric Cheng 		VERIFY(mac_callback_remove(&mip->mi_promisc_cb_info,
23498275SEric Cheng 		    &mcip->mci_promisc_list, &mpip->mpi_mci_link));
23508275SEric Cheng 		kmem_cache_free(mac_promisc_impl_cache, mpip);
23518275SEric Cheng 	} else {
23528275SEric Cheng 		mac_callback_remove_wait(&mip->mi_promisc_cb_info);
23538275SEric Cheng 	}
23548275SEric Cheng 	mutex_exit(mcbi->mcbi_lockp);
23558275SEric Cheng 	mac_stop(mip);
23568275SEric Cheng 
23578275SEric Cheng done:
23588275SEric Cheng 	i_mac_perim_exit(mip);
23598275SEric Cheng 	return (rc);
23608275SEric Cheng }
23618275SEric Cheng 
23628275SEric Cheng /*
23638275SEric Cheng  * Reference count the number of active Tx threads. MCI_TX_QUIESCE indicates
23648275SEric Cheng  * that a control operation wants to quiesce the Tx data flow in which case
23658275SEric Cheng  * we return an error. Holding any of the per cpu locks ensures that the
23668275SEric Cheng  * mci_tx_flag won't change.
23678275SEric Cheng  *
23688275SEric Cheng  * 'CPU' must be accessed just once and used to compute the index into the
23698275SEric Cheng  * percpu array, and that index must be used for the entire duration of the
23708275SEric Cheng  * packet send operation. Note that the thread may be preempted and run on
23718275SEric Cheng  * another cpu any time and so we can't use 'CPU' more than once for the
23728275SEric Cheng  * operation.
23738275SEric Cheng  */
23748275SEric Cheng #define	MAC_TX_TRY_HOLD(mcip, mytx, error)				\
23758275SEric Cheng {									\
23768275SEric Cheng 	(error) = 0;							\
23778275SEric Cheng 	(mytx) = &(mcip)->mci_tx_pcpu[CPU->cpu_seqid & mac_tx_percpu_cnt]; \
23788275SEric Cheng 	mutex_enter(&(mytx)->pcpu_tx_lock);				\
23798275SEric Cheng 	if (!((mcip)->mci_tx_flag & MCI_TX_QUIESCE)) {			\
23808275SEric Cheng 		(mytx)->pcpu_tx_refcnt++;				\
23818275SEric Cheng 	} else {							\
23828275SEric Cheng 		(error) = -1;						\
23838275SEric Cheng 	}								\
23848275SEric Cheng 	mutex_exit(&(mytx)->pcpu_tx_lock);				\
23858275SEric Cheng }
23868275SEric Cheng 
23878275SEric Cheng /*
23888275SEric Cheng  * Release the reference. If needed, signal any control operation waiting
23898275SEric Cheng  * for Tx quiescence. The wait and signal are always done using the
23908275SEric Cheng  * mci_tx_pcpu[0]'s lock
23918275SEric Cheng  */
23928275SEric Cheng #define	MAC_TX_RELE(mcip, mytx) {					\
23938275SEric Cheng 	mutex_enter(&(mytx)->pcpu_tx_lock);				\
23948275SEric Cheng 	if (--(mytx)->pcpu_tx_refcnt == 0 &&				\
23958275SEric Cheng 	    (mcip)->mci_tx_flag & MCI_TX_QUIESCE) {			\
23968275SEric Cheng 		mutex_exit(&(mytx)->pcpu_tx_lock);			\
23978275SEric Cheng 		mutex_enter(&(mcip)->mci_tx_pcpu[0].pcpu_tx_lock);	\
23988275SEric Cheng 		cv_signal(&(mcip)->mci_tx_cv);				\
23998275SEric Cheng 		mutex_exit(&(mcip)->mci_tx_pcpu[0].pcpu_tx_lock);	\
24008275SEric Cheng 	} else {							\
24018275SEric Cheng 		mutex_exit(&(mytx)->pcpu_tx_lock);			\
24028275SEric Cheng 	}								\
24038275SEric Cheng }
24048275SEric Cheng 
24058275SEric Cheng /*
24068275SEric Cheng  * Bump the count of the number of active Tx threads. This is maintained as
24078275SEric Cheng  * a per CPU counter. On (CMT kind of) machines with large number of CPUs,
24088275SEric Cheng  * a single mci_tx_lock may become contended. However a count of the total
24098275SEric Cheng  * number of Tx threads per client is needed in order to quiesce the Tx side
24108275SEric Cheng  * prior to reassigning a Tx ring dynamically to another client. The thread
24118275SEric Cheng  * that needs to quiesce the Tx traffic grabs all the percpu locks and checks
24128275SEric Cheng  * the sum of the individual percpu refcnts. Each Tx data thread only grabs
24138275SEric Cheng  * its own percpu lock and increments its own refcnt.
24148275SEric Cheng  */
24158275SEric Cheng void *
24168275SEric Cheng mac_tx_hold(mac_client_handle_t mch)
24178275SEric Cheng {
24188275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
24198275SEric Cheng 	mac_tx_percpu_t	*mytx;
24208275SEric Cheng 	int error;
24218275SEric Cheng 
24228275SEric Cheng 	MAC_TX_TRY_HOLD(mcip, mytx, error);
24238275SEric Cheng 	return (error == 0 ? (void *)mytx : NULL);
24248275SEric Cheng }
24258275SEric Cheng 
24268275SEric Cheng void
24278275SEric Cheng mac_tx_rele(mac_client_handle_t mch, void *mytx_handle)
24288275SEric Cheng {
24298275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
24308275SEric Cheng 	mac_tx_percpu_t	*mytx = mytx_handle;
24318275SEric Cheng 
24328275SEric Cheng 	MAC_TX_RELE(mcip, mytx)
24338275SEric Cheng }
24348275SEric Cheng 
24358275SEric Cheng /*
24368275SEric Cheng  * Send function invoked by MAC clients.
24378275SEric Cheng  */
24388275SEric Cheng mac_tx_cookie_t
24398275SEric Cheng mac_tx(mac_client_handle_t mch, mblk_t *mp_chain, uintptr_t hint,
24408275SEric Cheng     uint16_t flag, mblk_t **ret_mp)
24418275SEric Cheng {
24428275SEric Cheng 	mac_tx_cookie_t		cookie;
24438275SEric Cheng 	int			error;
24448275SEric Cheng 	mac_tx_percpu_t		*mytx;
24458275SEric Cheng 	mac_soft_ring_set_t	*srs;
24468275SEric Cheng 	flow_entry_t		*flent;
24478275SEric Cheng 	boolean_t		is_subflow = B_FALSE;
24488275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
24498275SEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
24508275SEric Cheng 	mac_srs_tx_t		*srs_tx;
24518275SEric Cheng 
24528275SEric Cheng 	/*
24538275SEric Cheng 	 * Check whether the active Tx threads count is bumped already.
24548275SEric Cheng 	 */
24558275SEric Cheng 	if (!(flag & MAC_TX_NO_HOLD)) {
24568275SEric Cheng 		MAC_TX_TRY_HOLD(mcip, mytx, error);
24578275SEric Cheng 		if (error != 0) {
24588275SEric Cheng 			freemsgchain(mp_chain);
24598275SEric Cheng 			return (NULL);
24608275SEric Cheng 		}
24618275SEric Cheng 	}
24628275SEric Cheng 
24638275SEric Cheng 	if (mcip->mci_subflow_tab != NULL &&
24648275SEric Cheng 	    mcip->mci_subflow_tab->ft_flow_count > 0 &&
24658275SEric Cheng 	    mac_flow_lookup(mcip->mci_subflow_tab, mp_chain,
24668275SEric Cheng 	    FLOW_OUTBOUND, &flent) == 0) {
24678275SEric Cheng 		/*
24688275SEric Cheng 		 * The main assumption here is that if in the event
24698275SEric Cheng 		 * we get a chain, all the packets will be classified
24708275SEric Cheng 		 * to the same Flow/SRS. If this changes for any
24718275SEric Cheng 		 * reason, the following logic should change as well.
24728275SEric Cheng 		 * I suppose the fanout_hint also assumes this .
24738275SEric Cheng 		 */
24748275SEric Cheng 		ASSERT(flent != NULL);
24758275SEric Cheng 		is_subflow = B_TRUE;
24768275SEric Cheng 	} else {
24778275SEric Cheng 		flent = mcip->mci_flent;
24788275SEric Cheng 	}
24798275SEric Cheng 
24808275SEric Cheng 	srs = flent->fe_tx_srs;
24818275SEric Cheng 	srs_tx = &srs->srs_tx;
24828275SEric Cheng 	if (srs_tx->st_mode == SRS_TX_DEFAULT &&
24838275SEric Cheng 	    (srs->srs_state & SRS_ENQUEUED) == 0 &&
24848275SEric Cheng 	    mip->mi_nactiveclients == 1 && mip->mi_promisc_list == NULL &&
24858275SEric Cheng 	    mp_chain->b_next == NULL) {
24868275SEric Cheng 		uint64_t	obytes;
24878275SEric Cheng 
24888275SEric Cheng 		/*
24898275SEric Cheng 		 * Since dls always opens the underlying MAC, nclients equals
24908275SEric Cheng 		 * to 1 means that the only active client is dls itself acting
24918275SEric Cheng 		 * as a primary client of the MAC instance. Since dls will not
24928275SEric Cheng 		 * send tagged packets in that case, and dls is trusted to send
24938275SEric Cheng 		 * packets for its allowed VLAN(s), the VLAN tag insertion and
24948275SEric Cheng 		 * check is required only if nclients is greater than 1.
24958275SEric Cheng 		 */
24968275SEric Cheng 		if (mip->mi_nclients > 1) {
24978275SEric Cheng 			if (MAC_VID_CHECK_NEEDED(mcip)) {
24988275SEric Cheng 				int	err = 0;
24998275SEric Cheng 
25008275SEric Cheng 				MAC_VID_CHECK(mcip, mp_chain, err);
25018275SEric Cheng 				if (err != 0) {
25028275SEric Cheng 					freemsg(mp_chain);
25038275SEric Cheng 					mcip->mci_stat_oerrors++;
25048275SEric Cheng 					goto done;
25058275SEric Cheng 				}
25068275SEric Cheng 			}
25078275SEric Cheng 			if (MAC_TAG_NEEDED(mcip)) {
25088275SEric Cheng 				mp_chain = mac_add_vlan_tag(mp_chain, 0,
25098275SEric Cheng 				    mac_client_vid(mch));
25108275SEric Cheng 				if (mp_chain == NULL) {
25118275SEric Cheng 					mcip->mci_stat_oerrors++;
25128275SEric Cheng 					goto done;
25138275SEric Cheng 				}
25148275SEric Cheng 			}
25158275SEric Cheng 		}
25168275SEric Cheng 
25178275SEric Cheng 		obytes = (mp_chain->b_cont == NULL ? MBLKL(mp_chain) :
25188275SEric Cheng 		    msgdsize(mp_chain));
25198275SEric Cheng 
25208275SEric Cheng 		MAC_TX(mip, srs_tx->st_arg2, mp_chain, mcip);
25218275SEric Cheng 
25228275SEric Cheng 		if (mp_chain == NULL) {
25238275SEric Cheng 			cookie = NULL;
25248275SEric Cheng 			mcip->mci_stat_obytes += obytes;
25258275SEric Cheng 			mcip->mci_stat_opackets += 1;
25268275SEric Cheng 			if ((srs->srs_type & SRST_FLOW) != 0) {
25278275SEric Cheng 				FLOW_STAT_UPDATE(flent, obytes, obytes);
25288275SEric Cheng 				FLOW_STAT_UPDATE(flent, opackets, 1);
25298275SEric Cheng 			}
25308275SEric Cheng 		} else {
25318275SEric Cheng 			mutex_enter(&srs->srs_lock);
25328275SEric Cheng 			cookie = mac_tx_srs_no_desc(srs, mp_chain,
25338275SEric Cheng 			    flag, ret_mp);
25348275SEric Cheng 			mutex_exit(&srs->srs_lock);
25358275SEric Cheng 		}
25368275SEric Cheng 	} else {
25378275SEric Cheng 		cookie = srs_tx->st_func(srs, mp_chain, hint, flag, ret_mp);
25388275SEric Cheng 	}
25398275SEric Cheng 
25408275SEric Cheng done:
25418275SEric Cheng 	if (is_subflow)
25428275SEric Cheng 		FLOW_REFRELE(flent);
25438275SEric Cheng 
25448275SEric Cheng 	if (!(flag & MAC_TX_NO_HOLD))
25458275SEric Cheng 		MAC_TX_RELE(mcip, mytx);
25468275SEric Cheng 
25478275SEric Cheng 	return (cookie);
25488275SEric Cheng }
25498275SEric Cheng 
25508275SEric Cheng /*
25518275SEric Cheng  * mac_tx_is_blocked
25528275SEric Cheng  *
25538275SEric Cheng  * Given a cookie, it returns if the ring identified by the cookie is
2554*8833SVenu.Iyer@Sun.COM  * flow-controlled or not. If NULL is passed in place of a cookie,
2555*8833SVenu.Iyer@Sun.COM  * then it finds out if any of the underlying rings belonging to the
2556*8833SVenu.Iyer@Sun.COM  * SRS is flow controlled or not and returns that status.
25578275SEric Cheng  */
25588275SEric Cheng /* ARGSUSED */
25598275SEric Cheng boolean_t
25608275SEric Cheng mac_tx_is_flow_blocked(mac_client_handle_t mch, mac_tx_cookie_t cookie)
25618275SEric Cheng {
25628275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
2563*8833SVenu.Iyer@Sun.COM 	mac_soft_ring_set_t *mac_srs;
25648275SEric Cheng 	mac_soft_ring_t *sringp;
25658275SEric Cheng 	boolean_t blocked = B_FALSE;
2566*8833SVenu.Iyer@Sun.COM 	mac_tx_percpu_t *mytx;
2567*8833SVenu.Iyer@Sun.COM 	int err;
25688275SEric Cheng 	int i;
25698275SEric Cheng 
25708275SEric Cheng 	/*
2571*8833SVenu.Iyer@Sun.COM 	 * Bump the reference count so that mac_srs won't be deleted.
2572*8833SVenu.Iyer@Sun.COM 	 * If the client is currently quiesced and we failed to bump
2573*8833SVenu.Iyer@Sun.COM 	 * the reference, return B_TRUE so that flow control stays
2574*8833SVenu.Iyer@Sun.COM 	 * as enabled.
2575*8833SVenu.Iyer@Sun.COM 	 *
2576*8833SVenu.Iyer@Sun.COM 	 * Flow control will then be disabled once the client is no
2577*8833SVenu.Iyer@Sun.COM 	 * longer quiesced.
25788275SEric Cheng 	 */
2579*8833SVenu.Iyer@Sun.COM 	MAC_TX_TRY_HOLD(mcip, mytx, err);
2580*8833SVenu.Iyer@Sun.COM 	if (err != 0)
2581*8833SVenu.Iyer@Sun.COM 		return (B_TRUE);
2582*8833SVenu.Iyer@Sun.COM 
2583*8833SVenu.Iyer@Sun.COM 	if ((mac_srs = MCIP_TX_SRS(mcip)) == NULL) {
2584*8833SVenu.Iyer@Sun.COM 		MAC_TX_RELE(mcip, mytx);
25858275SEric Cheng 		return (B_FALSE);
2586*8833SVenu.Iyer@Sun.COM 	}
25878275SEric Cheng 
25888275SEric Cheng 	mutex_enter(&mac_srs->srs_lock);
25898275SEric Cheng 	if (mac_srs->srs_tx.st_mode == SRS_TX_FANOUT) {
2590*8833SVenu.Iyer@Sun.COM 		if (cookie != NULL) {
2591*8833SVenu.Iyer@Sun.COM 			sringp = (mac_soft_ring_t *)cookie;
25928275SEric Cheng 			mutex_enter(&sringp->s_ring_lock);
2593*8833SVenu.Iyer@Sun.COM 			if (sringp->s_ring_state & S_RING_TX_HIWAT)
25948275SEric Cheng 				blocked = B_TRUE;
2595*8833SVenu.Iyer@Sun.COM 			mutex_exit(&sringp->s_ring_lock);
2596*8833SVenu.Iyer@Sun.COM 		} else {
2597*8833SVenu.Iyer@Sun.COM 			for (i = 0; i < mac_srs->srs_oth_ring_count; i++) {
2598*8833SVenu.Iyer@Sun.COM 				sringp = mac_srs->srs_oth_soft_rings[i];
2599*8833SVenu.Iyer@Sun.COM 				mutex_enter(&sringp->s_ring_lock);
2600*8833SVenu.Iyer@Sun.COM 				if (sringp->s_ring_state & S_RING_TX_HIWAT) {
2601*8833SVenu.Iyer@Sun.COM 					blocked = B_TRUE;
2602*8833SVenu.Iyer@Sun.COM 					mutex_exit(&sringp->s_ring_lock);
2603*8833SVenu.Iyer@Sun.COM 					break;
2604*8833SVenu.Iyer@Sun.COM 				}
26058275SEric Cheng 				mutex_exit(&sringp->s_ring_lock);
26068275SEric Cheng 			}
26078275SEric Cheng 		}
26088275SEric Cheng 	} else {
26098275SEric Cheng 		blocked = (mac_srs->srs_state & SRS_TX_HIWAT);
26108275SEric Cheng 	}
26118275SEric Cheng 	mutex_exit(&mac_srs->srs_lock);
2612*8833SVenu.Iyer@Sun.COM 	MAC_TX_RELE(mcip, mytx);
26138275SEric Cheng 	return (blocked);
26148275SEric Cheng }
26158275SEric Cheng 
26168275SEric Cheng /*
26178275SEric Cheng  * Check if the MAC client is the primary MAC client.
26188275SEric Cheng  */
26198275SEric Cheng boolean_t
26208275SEric Cheng mac_is_primary_client(mac_client_impl_t *mcip)
26218275SEric Cheng {
26228275SEric Cheng 	return (mcip->mci_flags & MAC_CLIENT_FLAGS_PRIMARY);
26238275SEric Cheng }
26248275SEric Cheng 
26258275SEric Cheng void
26268275SEric Cheng mac_ioctl(mac_handle_t mh, queue_t *wq, mblk_t *bp)
26278275SEric Cheng {
26288275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
26298275SEric Cheng 	int cmd = ((struct iocblk *)bp->b_rptr)->ioc_cmd;
26308275SEric Cheng 
26318275SEric Cheng 	if ((cmd == ND_GET && (mip->mi_callbacks->mc_callbacks & MC_GETPROP)) ||
26328275SEric Cheng 	    (cmd == ND_SET && (mip->mi_callbacks->mc_callbacks & MC_SETPROP))) {
26338275SEric Cheng 		/*
26348275SEric Cheng 		 * If ndd props were registered, call them.
26358275SEric Cheng 		 * Note that ndd ioctls are Obsolete
26368275SEric Cheng 		 */
26378275SEric Cheng 		mac_ndd_ioctl(mip, wq, bp);
26388275SEric Cheng 		return;
26398275SEric Cheng 	}
26408275SEric Cheng 
26418275SEric Cheng 	/*
26428275SEric Cheng 	 * Call the driver to handle the ioctl.  The driver may not support
26438275SEric Cheng 	 * any ioctls, in which case we reply with a NAK on its behalf.
26448275SEric Cheng 	 */
26458275SEric Cheng 	if (mip->mi_callbacks->mc_callbacks & MC_IOCTL)
26468275SEric Cheng 		mip->mi_ioctl(mip->mi_driver, wq, bp);
26478275SEric Cheng 	else
26488275SEric Cheng 		miocnak(wq, bp, 0, EINVAL);
26498275SEric Cheng }
26508275SEric Cheng 
26518275SEric Cheng /*
26528275SEric Cheng  * Return the link state of the specified MAC instance.
26538275SEric Cheng  */
26548275SEric Cheng link_state_t
26558275SEric Cheng mac_link_get(mac_handle_t mh)
26568275SEric Cheng {
26578275SEric Cheng 	return (((mac_impl_t *)mh)->mi_linkstate);
26588275SEric Cheng }
26598275SEric Cheng 
26608275SEric Cheng /*
26618275SEric Cheng  * Add a mac client specified notification callback. Please see the comments
26628275SEric Cheng  * above mac_callback_add() for general information about mac callback
26638275SEric Cheng  * addition/deletion in the presence of mac callback list walkers
26648275SEric Cheng  */
26658275SEric Cheng mac_notify_handle_t
26668275SEric Cheng mac_notify_add(mac_handle_t mh, mac_notify_t notify_fn, void *arg)
26678275SEric Cheng {
26688275SEric Cheng 	mac_impl_t		*mip = (mac_impl_t *)mh;
26698275SEric Cheng 	mac_notify_cb_t		*mncb;
26708275SEric Cheng 	mac_cb_info_t		*mcbi;
26718275SEric Cheng 
26728275SEric Cheng 	/*
26738275SEric Cheng 	 * Allocate a notify callback structure, fill in the details and
26748275SEric Cheng 	 * use the mac callback list manipulation functions to chain into
26758275SEric Cheng 	 * the list of callbacks.
26768275SEric Cheng 	 */
26778275SEric Cheng 	mncb = kmem_zalloc(sizeof (mac_notify_cb_t), KM_SLEEP);
26788275SEric Cheng 	mncb->mncb_fn = notify_fn;
26798275SEric Cheng 	mncb->mncb_arg = arg;
26808275SEric Cheng 	mncb->mncb_mip = mip;
26818275SEric Cheng 	mncb->mncb_link.mcb_objp = mncb;
26828275SEric Cheng 	mncb->mncb_link.mcb_objsize = sizeof (mac_notify_cb_t);
26838275SEric Cheng 	mncb->mncb_link.mcb_flags = MCB_NOTIFY_CB_T;
26848275SEric Cheng 
26858275SEric Cheng 	mcbi = &mip->mi_notify_cb_info;
26868275SEric Cheng 
26878275SEric Cheng 	i_mac_perim_enter(mip);
26888275SEric Cheng 	mutex_enter(mcbi->mcbi_lockp);
26898275SEric Cheng 
26908275SEric Cheng 	mac_callback_add(&mip->mi_notify_cb_info, &mip->mi_notify_cb_list,
26918275SEric Cheng 	    &mncb->mncb_link);
26928275SEric Cheng 
26938275SEric Cheng 	mutex_exit(mcbi->mcbi_lockp);
26948275SEric Cheng 	i_mac_perim_exit(mip);
26958275SEric Cheng 	return ((mac_notify_handle_t)mncb);
26968275SEric Cheng }
26978275SEric Cheng 
26988275SEric Cheng void
26998275SEric Cheng mac_notify_remove_wait(mac_handle_t mh)
27008275SEric Cheng {
27018275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
27028275SEric Cheng 	mac_cb_info_t	*mcbi = &mip->mi_notify_cb_info;
27038275SEric Cheng 
27048275SEric Cheng 	mutex_enter(mcbi->mcbi_lockp);
27058275SEric Cheng 	mac_callback_remove_wait(&mip->mi_notify_cb_info);
27068275SEric Cheng 	mutex_exit(mcbi->mcbi_lockp);
27078275SEric Cheng }
27088275SEric Cheng 
27098275SEric Cheng /*
27108275SEric Cheng  * Remove a mac client specified notification callback
27118275SEric Cheng  */
27128275SEric Cheng int
27138275SEric Cheng mac_notify_remove(mac_notify_handle_t mnh, boolean_t wait)
27148275SEric Cheng {
27158275SEric Cheng 	mac_notify_cb_t	*mncb = (mac_notify_cb_t *)mnh;
27168275SEric Cheng 	mac_impl_t	*mip = mncb->mncb_mip;
27178275SEric Cheng 	mac_cb_info_t	*mcbi;
27188275SEric Cheng 	int		err = 0;
27198275SEric Cheng 
27208275SEric Cheng 	mcbi = &mip->mi_notify_cb_info;
27218275SEric Cheng 
27228275SEric Cheng 	i_mac_perim_enter(mip);
27238275SEric Cheng 	mutex_enter(mcbi->mcbi_lockp);
27248275SEric Cheng 
27258275SEric Cheng 	ASSERT(mncb->mncb_link.mcb_objp == mncb);
27268275SEric Cheng 	/*
27278275SEric Cheng 	 * If there aren't any list walkers, the remove would succeed
27288275SEric Cheng 	 * inline, else we wait for the deferred remove to complete
27298275SEric Cheng 	 */
27308275SEric Cheng 	if (mac_callback_remove(&mip->mi_notify_cb_info,
27318275SEric Cheng 	    &mip->mi_notify_cb_list, &mncb->mncb_link)) {
27328275SEric Cheng 		kmem_free(mncb, sizeof (mac_notify_cb_t));
27338275SEric Cheng 	} else {
27348275SEric Cheng 		err = EBUSY;
27358275SEric Cheng 	}
27368275SEric Cheng 
27378275SEric Cheng 	mutex_exit(mcbi->mcbi_lockp);
27388275SEric Cheng 	i_mac_perim_exit(mip);
27398275SEric Cheng 
27408275SEric Cheng 	/*
27418275SEric Cheng 	 * If we failed to remove the notification callback and "wait" is set
27428275SEric Cheng 	 * to be B_TRUE, wait for the callback to finish after we exit the
27438275SEric Cheng 	 * mac perimeter.
27448275SEric Cheng 	 */
27458275SEric Cheng 	if (err != 0 && wait) {
27468275SEric Cheng 		mac_notify_remove_wait((mac_handle_t)mip);
27478275SEric Cheng 		return (0);
27488275SEric Cheng 	}
27498275SEric Cheng 
27508275SEric Cheng 	return (err);
27518275SEric Cheng }
27528275SEric Cheng 
27538275SEric Cheng /*
27548275SEric Cheng  * Associate resource management callbacks with the specified MAC
27558275SEric Cheng  * clients.
27568275SEric Cheng  */
27578275SEric Cheng 
27588275SEric Cheng void
27598275SEric Cheng mac_resource_set_common(mac_client_handle_t mch, mac_resource_add_t add,
27608275SEric Cheng     mac_resource_remove_t remove, mac_resource_quiesce_t quiesce,
27618275SEric Cheng     mac_resource_restart_t restart, mac_resource_bind_t bind,
27628275SEric Cheng     void *arg)
27638275SEric Cheng {
27648275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
27658275SEric Cheng 
27668275SEric Cheng 	mcip->mci_resource_add = add;
27678275SEric Cheng 	mcip->mci_resource_remove = remove;
27688275SEric Cheng 	mcip->mci_resource_quiesce = quiesce;
27698275SEric Cheng 	mcip->mci_resource_restart = restart;
27708275SEric Cheng 	mcip->mci_resource_bind = bind;
27718275SEric Cheng 	mcip->mci_resource_arg = arg;
27728275SEric Cheng 
27738275SEric Cheng 	if (arg == NULL)
27748275SEric Cheng 		mcip->mci_state_flags &= ~MCIS_CLIENT_POLL_CAPABLE;
27758275SEric Cheng }
27768275SEric Cheng 
27778275SEric Cheng void
27788275SEric Cheng mac_resource_set(mac_client_handle_t mch, mac_resource_add_t add, void *arg)
27798275SEric Cheng {
27808275SEric Cheng 	/* update the 'resource_add' callback */
27818275SEric Cheng 	mac_resource_set_common(mch, add, NULL, NULL, NULL, NULL, arg);
27828275SEric Cheng }
27838275SEric Cheng 
27848275SEric Cheng /*
27858275SEric Cheng  * Sets up the client resources and enable the polling interface over all the
27868275SEric Cheng  * SRS's and the soft rings of the client
27878275SEric Cheng  */
27888275SEric Cheng void
27898275SEric Cheng mac_client_poll_enable(mac_client_handle_t mch)
27908275SEric Cheng {
27918275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
27928275SEric Cheng 	mac_soft_ring_set_t	*mac_srs;
27938275SEric Cheng 	flow_entry_t		*flent;
27948275SEric Cheng 	int			i;
27958275SEric Cheng 
27968275SEric Cheng 	flent = mcip->mci_flent;
27978275SEric Cheng 	ASSERT(flent != NULL);
27988275SEric Cheng 
27998275SEric Cheng 	for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
28008275SEric Cheng 		mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
28018275SEric Cheng 		ASSERT(mac_srs->srs_mcip == mcip);
28028275SEric Cheng 		mac_srs_client_poll_enable(mcip, mac_srs);
28038275SEric Cheng 	}
28048275SEric Cheng }
28058275SEric Cheng 
28068275SEric Cheng /*
28078275SEric Cheng  * Tears down the client resources and disable the polling interface over all
28088275SEric Cheng  * the SRS's and the soft rings of the client
28098275SEric Cheng  */
28108275SEric Cheng void
28118275SEric Cheng mac_client_poll_disable(mac_client_handle_t mch)
28128275SEric Cheng {
28138275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
28148275SEric Cheng 	mac_soft_ring_set_t	*mac_srs;
28158275SEric Cheng 	flow_entry_t		*flent;
28168275SEric Cheng 	int			i;
28178275SEric Cheng 
28188275SEric Cheng 	flent = mcip->mci_flent;
28198275SEric Cheng 	ASSERT(flent != NULL);
28208275SEric Cheng 
28218275SEric Cheng 	for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
28228275SEric Cheng 		mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
28238275SEric Cheng 		ASSERT(mac_srs->srs_mcip == mcip);
28248275SEric Cheng 		mac_srs_client_poll_disable(mcip, mac_srs);
28258275SEric Cheng 	}
28268275SEric Cheng }
28278275SEric Cheng 
28288275SEric Cheng /*
28298275SEric Cheng  * Associate the CPUs specified by the given property with a MAC client.
28308275SEric Cheng  */
28318275SEric Cheng int
28328275SEric Cheng mac_cpu_set(mac_client_handle_t mch, mac_resource_props_t *mrp)
28338275SEric Cheng {
28348275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
28358275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
28368275SEric Cheng 	int err = 0;
28378275SEric Cheng 
28388275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
28398275SEric Cheng 
28408275SEric Cheng 	if ((err = mac_validate_props(mrp)) != 0)
28418275SEric Cheng 		return (err);
28428275SEric Cheng 
28438275SEric Cheng 	if (MCIP_DATAPATH_SETUP(mcip))
28448275SEric Cheng 		mac_flow_modify(mip->mi_flow_tab, mcip->mci_flent, mrp);
28458275SEric Cheng 
28468275SEric Cheng 	mac_update_resources(mrp, MCIP_RESOURCE_PROPS(mcip), B_FALSE);
28478275SEric Cheng 	return (0);
28488275SEric Cheng }
28498275SEric Cheng 
28508275SEric Cheng /*
28518275SEric Cheng  * Apply the specified properties to the specified MAC client.
28528275SEric Cheng  */
28538275SEric Cheng int
28548275SEric Cheng mac_client_set_resources(mac_client_handle_t mch, mac_resource_props_t *mrp)
28558275SEric Cheng {
28568275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
28578275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
28588275SEric Cheng 	int err = 0;
28598275SEric Cheng 
28608275SEric Cheng 	i_mac_perim_enter(mip);
28618275SEric Cheng 
28628275SEric Cheng 	if ((mrp->mrp_mask & MRP_MAXBW) || (mrp->mrp_mask & MRP_PRIORITY)) {
28638275SEric Cheng 		err = mac_resource_ctl_set(mch, mrp);
28648275SEric Cheng 		if (err != 0) {
28658275SEric Cheng 			i_mac_perim_exit(mip);
28668275SEric Cheng 			return (err);
28678275SEric Cheng 		}
28688275SEric Cheng 	}
28698275SEric Cheng 
28708275SEric Cheng 	if (mrp->mrp_mask & MRP_CPUS)
28718275SEric Cheng 		err = mac_cpu_set(mch, mrp);
28728275SEric Cheng 
28738275SEric Cheng 	i_mac_perim_exit(mip);
28748275SEric Cheng 	return (err);
28758275SEric Cheng }
28768275SEric Cheng 
28778275SEric Cheng /*
28788275SEric Cheng  * Return the properties currently associated with the specified MAC client.
28798275SEric Cheng  */
28808275SEric Cheng void
28818275SEric Cheng mac_client_get_resources(mac_client_handle_t mch, mac_resource_props_t *mrp)
28828275SEric Cheng {
28838275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
28848275SEric Cheng 	mac_resource_props_t	*mcip_mrp = MCIP_RESOURCE_PROPS(mcip);
28858275SEric Cheng 
28868275SEric Cheng 	bcopy(mcip_mrp, mrp, sizeof (mac_resource_props_t));
28878275SEric Cheng }
28888275SEric Cheng 
28898275SEric Cheng /*
28908275SEric Cheng  * Pass a copy of the specified packet to the promiscuous callbacks
28918275SEric Cheng  * of the specified MAC.
28928275SEric Cheng  *
28938275SEric Cheng  * If sender is NULL, the function is being invoked for a packet chain
28948275SEric Cheng  * received from the wire. If sender is non-NULL, it points to
28958275SEric Cheng  * the MAC client from which the packet is being sent.
28968275SEric Cheng  *
28978275SEric Cheng  * The packets are distributed to the promiscuous callbacks as follows:
28988275SEric Cheng  *
28998275SEric Cheng  * - all packets are sent to the MAC_CLIENT_PROMISC_ALL callbacks
29008275SEric Cheng  * - all broadcast and multicast packets are sent to the
29018275SEric Cheng  *   MAC_CLIENT_PROMISC_FILTER and MAC_CLIENT_PROMISC_MULTI.
29028275SEric Cheng  *
29038275SEric Cheng  * The unicast packets of MAC_CLIENT_PROMISC_FILTER callbacks are dispatched
29048275SEric Cheng  * after classification by mac_rx_deliver().
29058275SEric Cheng  */
29068275SEric Cheng 
29078275SEric Cheng static void
29088275SEric Cheng mac_promisc_dispatch_one(mac_promisc_impl_t *mpip, mblk_t *mp,
29098275SEric Cheng     boolean_t loopback)
29108275SEric Cheng {
29118275SEric Cheng 	mblk_t *mp_copy;
29128275SEric Cheng 
29138275SEric Cheng 	mp_copy = copymsg(mp);
29148275SEric Cheng 	if (mp_copy == NULL)
29158275SEric Cheng 		return;
29168275SEric Cheng 	mp_copy->b_next = NULL;
29178275SEric Cheng 
2918*8833SVenu.Iyer@Sun.COM 	if (mpip->mpi_strip_vlan_tag) {
2919*8833SVenu.Iyer@Sun.COM 		if ((mp_copy = mac_strip_vlan_tag_chain(mp_copy)) == NULL)
2920*8833SVenu.Iyer@Sun.COM 			return;
2921*8833SVenu.Iyer@Sun.COM 	}
29228275SEric Cheng 	mpip->mpi_fn(mpip->mpi_arg, NULL, mp_copy, loopback);
29238275SEric Cheng }
29248275SEric Cheng 
29258275SEric Cheng /*
29268275SEric Cheng  * Return the VID of a packet. Zero if the packet is not tagged.
29278275SEric Cheng  */
29288275SEric Cheng static uint16_t
29298275SEric Cheng mac_ether_vid(mblk_t *mp)
29308275SEric Cheng {
29318275SEric Cheng 	struct ether_header *eth = (struct ether_header *)mp->b_rptr;
29328275SEric Cheng 
29338275SEric Cheng 	if (ntohs(eth->ether_type) == ETHERTYPE_VLAN) {
29348275SEric Cheng 		struct ether_vlan_header *t_evhp =
29358275SEric Cheng 		    (struct ether_vlan_header *)mp->b_rptr;
29368275SEric Cheng 		return (VLAN_ID(ntohs(t_evhp->ether_tci)));
29378275SEric Cheng 	}
29388275SEric Cheng 
29398275SEric Cheng 	return (0);
29408275SEric Cheng }
29418275SEric Cheng 
29428275SEric Cheng /*
29438275SEric Cheng  * Return whether the specified packet contains a multicast or broadcast
29448275SEric Cheng  * destination MAC address.
29458275SEric Cheng  */
29468275SEric Cheng static boolean_t
29478275SEric Cheng mac_is_mcast(mac_impl_t *mip, mblk_t *mp)
29488275SEric Cheng {
29498275SEric Cheng 	mac_header_info_t hdr_info;
29508275SEric Cheng 
29518275SEric Cheng 	if (mac_header_info((mac_handle_t)mip, mp, &hdr_info) != 0)
29528275SEric Cheng 		return (B_FALSE);
29538275SEric Cheng 	return ((hdr_info.mhi_dsttype == MAC_ADDRTYPE_BROADCAST) ||
29548275SEric Cheng 	    (hdr_info.mhi_dsttype == MAC_ADDRTYPE_MULTICAST));
29558275SEric Cheng }
29568275SEric Cheng 
29578275SEric Cheng /*
29588275SEric Cheng  * Send a copy of an mblk chain to the MAC clients of the specified MAC.
29598275SEric Cheng  * "sender" points to the sender MAC client for outbound packets, and
29608275SEric Cheng  * is set to NULL for inbound packets.
29618275SEric Cheng  */
29628275SEric Cheng void
29638275SEric Cheng mac_promisc_dispatch(mac_impl_t *mip, mblk_t *mp_chain,
29648275SEric Cheng     mac_client_impl_t *sender)
29658275SEric Cheng {
29668275SEric Cheng 	mac_promisc_impl_t *mpip;
29678275SEric Cheng 	mac_cb_t *mcb;
29688275SEric Cheng 	mblk_t *mp;
29698275SEric Cheng 	boolean_t is_mcast, is_sender;
29708275SEric Cheng 
29718275SEric Cheng 	MAC_PROMISC_WALKER_INC(mip);
29728275SEric Cheng 	for (mp = mp_chain; mp != NULL; mp = mp->b_next) {
29738275SEric Cheng 		is_mcast = mac_is_mcast(mip, mp);
29748275SEric Cheng 		/* send packet to interested callbacks */
29758275SEric Cheng 		for (mcb = mip->mi_promisc_list; mcb != NULL;
29768275SEric Cheng 		    mcb = mcb->mcb_nextp) {
29778275SEric Cheng 			mpip = (mac_promisc_impl_t *)mcb->mcb_objp;
29788275SEric Cheng 			is_sender = (mpip->mpi_mcip == sender);
29798275SEric Cheng 
29808275SEric Cheng 			if (is_sender && mpip->mpi_no_tx_loop)
29818275SEric Cheng 				/*
29828275SEric Cheng 				 * The sender doesn't want to receive
29838275SEric Cheng 				 * copies of the packets it sends.
29848275SEric Cheng 				 */
29858275SEric Cheng 				continue;
29868275SEric Cheng 
29878275SEric Cheng 			/*
29888275SEric Cheng 			 * For an ethernet MAC, don't displatch a multicast
29898275SEric Cheng 			 * packet to a non-PROMISC_ALL callbacks unless the VID
29908275SEric Cheng 			 * of the packet matches the VID of the client.
29918275SEric Cheng 			 */
29928275SEric Cheng 			if (is_mcast &&
29938275SEric Cheng 			    mpip->mpi_type != MAC_CLIENT_PROMISC_ALL &&
29948275SEric Cheng 			    !mac_client_check_flow_vid(mpip->mpi_mcip,
29958275SEric Cheng 			    mac_ether_vid(mp)))
29968275SEric Cheng 				continue;
29978275SEric Cheng 
29988275SEric Cheng 			if (is_sender ||
29998275SEric Cheng 			    mpip->mpi_type == MAC_CLIENT_PROMISC_ALL ||
30008275SEric Cheng 			    is_mcast)
30018275SEric Cheng 				mac_promisc_dispatch_one(mpip, mp, is_sender);
30028275SEric Cheng 		}
30038275SEric Cheng 	}
30048275SEric Cheng 	MAC_PROMISC_WALKER_DCR(mip);
30058275SEric Cheng }
30068275SEric Cheng 
30078275SEric Cheng void
30088275SEric Cheng mac_promisc_client_dispatch(mac_client_impl_t *mcip, mblk_t *mp_chain)
30098275SEric Cheng {
30108275SEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
30118275SEric Cheng 	mac_promisc_impl_t	*mpip;
30128275SEric Cheng 	boolean_t		is_mcast;
30138275SEric Cheng 	mblk_t			*mp;
30148275SEric Cheng 	mac_cb_t		*mcb;
30158275SEric Cheng 
30168275SEric Cheng 	/*
30178275SEric Cheng 	 * The unicast packets for the MAC client still
30188275SEric Cheng 	 * need to be delivered to the MAC_CLIENT_PROMISC_FILTERED
30198275SEric Cheng 	 * promiscuous callbacks. The broadcast and multicast
30208275SEric Cheng 	 * packets were delivered from mac_rx().
30218275SEric Cheng 	 */
30228275SEric Cheng 	MAC_PROMISC_WALKER_INC(mip);
30238275SEric Cheng 	for (mp = mp_chain; mp != NULL; mp = mp->b_next) {
30248275SEric Cheng 		is_mcast = mac_is_mcast(mip, mp);
30258275SEric Cheng 		for (mcb = mcip->mci_promisc_list; mcb != NULL;
30268275SEric Cheng 		    mcb = mcb->mcb_nextp) {
30278275SEric Cheng 			mpip = (mac_promisc_impl_t *)mcb->mcb_objp;
30288275SEric Cheng 			if (mpip->mpi_type == MAC_CLIENT_PROMISC_FILTERED &&
30298275SEric Cheng 			    !is_mcast) {
30308275SEric Cheng 				mac_promisc_dispatch_one(mpip, mp, B_FALSE);
30318275SEric Cheng 			}
30328275SEric Cheng 		}
30338275SEric Cheng 	}
30348275SEric Cheng 	MAC_PROMISC_WALKER_DCR(mip);
30358275SEric Cheng }
30368275SEric Cheng 
30378275SEric Cheng /*
30388275SEric Cheng  * Return the margin value currently assigned to the specified MAC instance.
30398275SEric Cheng  */
30408275SEric Cheng void
30418275SEric Cheng mac_margin_get(mac_handle_t mh, uint32_t *marginp)
30428275SEric Cheng {
30438275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
30448275SEric Cheng 
30458275SEric Cheng 	rw_enter(&(mip->mi_rw_lock), RW_READER);
30468275SEric Cheng 	*marginp = mip->mi_margin;
30478275SEric Cheng 	rw_exit(&(mip->mi_rw_lock));
30488275SEric Cheng }
30498275SEric Cheng 
30508275SEric Cheng /*
30518275SEric Cheng  * mac_info_get() is used for retrieving the mac_info when a DL_INFO_REQ is
30528275SEric Cheng  * issued before a DL_ATTACH_REQ. we walk the i_mac_impl_hash table and find
30538275SEric Cheng  * the first mac_impl_t with a matching driver name; then we copy its mac_info_t
30548275SEric Cheng  * to the caller. we do all this with i_mac_impl_lock held so the mac_impl_t
30558275SEric Cheng  * cannot disappear while we are accessing it.
30568275SEric Cheng  */
30578275SEric Cheng typedef struct i_mac_info_state_s {
30588275SEric Cheng 	const char	*mi_name;
30598275SEric Cheng 	mac_info_t	*mi_infop;
30608275SEric Cheng } i_mac_info_state_t;
30618275SEric Cheng 
30628275SEric Cheng /*ARGSUSED*/
30638275SEric Cheng static uint_t
30648275SEric Cheng i_mac_info_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
30658275SEric Cheng {
30668275SEric Cheng 	i_mac_info_state_t *statep = arg;
30678275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)val;
30688275SEric Cheng 
30698275SEric Cheng 	if (mip->mi_state_flags & MIS_DISABLED)
30708275SEric Cheng 		return (MH_WALK_CONTINUE);
30718275SEric Cheng 
30728275SEric Cheng 	if (strcmp(statep->mi_name,
30738275SEric Cheng 	    ddi_driver_name(mip->mi_dip)) != 0)
30748275SEric Cheng 		return (MH_WALK_CONTINUE);
30758275SEric Cheng 
30768275SEric Cheng 	statep->mi_infop = &mip->mi_info;
30778275SEric Cheng 	return (MH_WALK_TERMINATE);
30788275SEric Cheng }
30798275SEric Cheng 
30808275SEric Cheng boolean_t
30818275SEric Cheng mac_info_get(const char *name, mac_info_t *minfop)
30828275SEric Cheng {
30838275SEric Cheng 	i_mac_info_state_t state;
30848275SEric Cheng 
30858275SEric Cheng 	rw_enter(&i_mac_impl_lock, RW_READER);
30868275SEric Cheng 	state.mi_name = name;
30878275SEric Cheng 	state.mi_infop = NULL;
30888275SEric Cheng 	mod_hash_walk(i_mac_impl_hash, i_mac_info_walker, &state);
30898275SEric Cheng 	if (state.mi_infop == NULL) {
30908275SEric Cheng 		rw_exit(&i_mac_impl_lock);
30918275SEric Cheng 		return (B_FALSE);
30928275SEric Cheng 	}
30938275SEric Cheng 	*minfop = *state.mi_infop;
30948275SEric Cheng 	rw_exit(&i_mac_impl_lock);
30958275SEric Cheng 	return (B_TRUE);
30968275SEric Cheng }
30978275SEric Cheng 
30988275SEric Cheng /*
30998275SEric Cheng  * To get the capabilities that MAC layer cares about, such as rings, factory
31008275SEric Cheng  * mac address, vnic or not, it should directly invoke this function
31018275SEric Cheng  */
31028275SEric Cheng boolean_t
31038275SEric Cheng i_mac_capab_get(mac_handle_t mh, mac_capab_t cap, void *cap_data)
31048275SEric Cheng {
31058275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
31068275SEric Cheng 
31078275SEric Cheng 	if (mip->mi_callbacks->mc_callbacks & MC_GETCAPAB)
31088275SEric Cheng 		return (mip->mi_getcapab(mip->mi_driver, cap, cap_data));
31098275SEric Cheng 	else
31108275SEric Cheng 		return (B_FALSE);
31118275SEric Cheng }
31128275SEric Cheng 
31138275SEric Cheng /*
31148275SEric Cheng  * Capability query function. If number of active mac clients is greater than
31158275SEric Cheng  * 1, only limited capabilities can be advertised to the caller no matter the
31168275SEric Cheng  * driver has certain capability or not. Else, we query the driver to get the
31178275SEric Cheng  * capability.
31188275SEric Cheng  */
31198275SEric Cheng boolean_t
31208275SEric Cheng mac_capab_get(mac_handle_t mh, mac_capab_t cap, void *cap_data)
31218275SEric Cheng {
31228275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
31238275SEric Cheng 
31248275SEric Cheng 	/*
31258275SEric Cheng 	 * if mi_nactiveclients > 1, only MAC_CAPAB_HCKSUM,
31268275SEric Cheng 	 * MAC_CAPAB_NO_NATIVEVLAN, MAC_CAPAB_NO_ZCOPY can be advertised.
31278275SEric Cheng 	 */
31288275SEric Cheng 	if (mip->mi_nactiveclients > 1) {
31298275SEric Cheng 		switch (cap) {
31308275SEric Cheng 		case MAC_CAPAB_HCKSUM:
31318275SEric Cheng 			return (i_mac_capab_get(mh, cap, cap_data));
31328275SEric Cheng 		case MAC_CAPAB_NO_NATIVEVLAN:
31338275SEric Cheng 		case MAC_CAPAB_NO_ZCOPY:
31348275SEric Cheng 			return (B_TRUE);
31358275SEric Cheng 		default:
31368275SEric Cheng 			return (B_FALSE);
31378275SEric Cheng 		}
31388275SEric Cheng 	}
31398275SEric Cheng 
31408275SEric Cheng 	/* else get capab from driver */
31418275SEric Cheng 	return (i_mac_capab_get(mh, cap, cap_data));
31428275SEric Cheng }
31438275SEric Cheng 
31448275SEric Cheng boolean_t
31458275SEric Cheng mac_sap_verify(mac_handle_t mh, uint32_t sap, uint32_t *bind_sap)
31468275SEric Cheng {
31478275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
31488275SEric Cheng 
31498275SEric Cheng 	return (mip->mi_type->mt_ops.mtops_sap_verify(sap, bind_sap,
31508275SEric Cheng 	    mip->mi_pdata));
31518275SEric Cheng }
31528275SEric Cheng 
31538275SEric Cheng mblk_t *
31548275SEric Cheng mac_header(mac_handle_t mh, const uint8_t *daddr, uint32_t sap, mblk_t *payload,
31558275SEric Cheng     size_t extra_len)
31568275SEric Cheng {
31578275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
31588275SEric Cheng 
31598275SEric Cheng 	return (mip->mi_type->mt_ops.mtops_header(mip->mi_addr, daddr, sap,
31608275SEric Cheng 	    mip->mi_pdata, payload, extra_len));
31618275SEric Cheng }
31628275SEric Cheng 
31638275SEric Cheng int
31648275SEric Cheng mac_header_info(mac_handle_t mh, mblk_t *mp, mac_header_info_t *mhip)
31658275SEric Cheng {
31668275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
31678275SEric Cheng 
31688275SEric Cheng 	return (mip->mi_type->mt_ops.mtops_header_info(mp, mip->mi_pdata,
31698275SEric Cheng 	    mhip));
31708275SEric Cheng }
31718275SEric Cheng 
31728275SEric Cheng mblk_t *
31738275SEric Cheng mac_header_cook(mac_handle_t mh, mblk_t *mp)
31748275SEric Cheng {
31758275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
31768275SEric Cheng 
31778275SEric Cheng 	if (mip->mi_type->mt_ops.mtops_ops & MTOPS_HEADER_COOK) {
31788275SEric Cheng 		if (DB_REF(mp) > 1) {
31798275SEric Cheng 			mblk_t *newmp = copymsg(mp);
31808275SEric Cheng 			if (newmp == NULL)
31818275SEric Cheng 				return (NULL);
31828275SEric Cheng 			freemsg(mp);
31838275SEric Cheng 			mp = newmp;
31848275SEric Cheng 		}
31858275SEric Cheng 		return (mip->mi_type->mt_ops.mtops_header_cook(mp,
31868275SEric Cheng 		    mip->mi_pdata));
31878275SEric Cheng 	}
31888275SEric Cheng 	return (mp);
31898275SEric Cheng }
31908275SEric Cheng 
31918275SEric Cheng mblk_t *
31928275SEric Cheng mac_header_uncook(mac_handle_t mh, mblk_t *mp)
31938275SEric Cheng {
31948275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
31958275SEric Cheng 
31968275SEric Cheng 	if (mip->mi_type->mt_ops.mtops_ops & MTOPS_HEADER_UNCOOK) {
31978275SEric Cheng 		if (DB_REF(mp) > 1) {
31988275SEric Cheng 			mblk_t *newmp = copymsg(mp);
31998275SEric Cheng 			if (newmp == NULL)
32008275SEric Cheng 				return (NULL);
32018275SEric Cheng 			freemsg(mp);
32028275SEric Cheng 			mp = newmp;
32038275SEric Cheng 		}
32048275SEric Cheng 		return (mip->mi_type->mt_ops.mtops_header_uncook(mp,
32058275SEric Cheng 		    mip->mi_pdata));
32068275SEric Cheng 	}
32078275SEric Cheng 	return (mp);
32088275SEric Cheng }
32098275SEric Cheng 
32108275SEric Cheng uint_t
32118275SEric Cheng mac_addr_len(mac_handle_t mh)
32128275SEric Cheng {
32138275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
32148275SEric Cheng 
32158275SEric Cheng 	return (mip->mi_type->mt_addr_length);
32168275SEric Cheng }
32178275SEric Cheng 
32188275SEric Cheng /* True if a MAC is a VNIC */
32198275SEric Cheng boolean_t
32208275SEric Cheng mac_is_vnic(mac_handle_t mh)
32218275SEric Cheng {
32228275SEric Cheng 	return (((mac_impl_t *)mh)->mi_state_flags & MIS_IS_VNIC);
32238275SEric Cheng }
32248275SEric Cheng 
32258275SEric Cheng mac_handle_t
32268275SEric Cheng mac_get_lower_mac_handle(mac_handle_t mh)
32278275SEric Cheng {
32288275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
32298275SEric Cheng 
32308275SEric Cheng 	ASSERT(mac_is_vnic(mh));
32318275SEric Cheng 	return (((vnic_t *)mip->mi_driver)->vn_lower_mh);
32328275SEric Cheng }
32338275SEric Cheng 
32348275SEric Cheng void
32358275SEric Cheng mac_update_resources(mac_resource_props_t *nmrp, mac_resource_props_t *cmrp,
32368275SEric Cheng     boolean_t is_user_flow)
32378275SEric Cheng {
32388275SEric Cheng 	if (nmrp != NULL && cmrp != NULL) {
32398275SEric Cheng 		if (nmrp->mrp_mask & MRP_PRIORITY) {
32408275SEric Cheng 			if (nmrp->mrp_priority == MPL_RESET) {
32418275SEric Cheng 				cmrp->mrp_mask &= ~MRP_PRIORITY;
32428275SEric Cheng 				if (is_user_flow) {
32438275SEric Cheng 					cmrp->mrp_priority =
32448275SEric Cheng 					    MPL_SUBFLOW_DEFAULT;
32458275SEric Cheng 				} else {
32468275SEric Cheng 					cmrp->mrp_priority = MPL_LINK_DEFAULT;
32478275SEric Cheng 				}
32488275SEric Cheng 			} else {
32498275SEric Cheng 				cmrp->mrp_mask |= MRP_PRIORITY;
32508275SEric Cheng 				cmrp->mrp_priority = nmrp->mrp_priority;
32518275SEric Cheng 			}
32528275SEric Cheng 		}
32538275SEric Cheng 		if (nmrp->mrp_mask & MRP_MAXBW) {
32548275SEric Cheng 			cmrp->mrp_maxbw = nmrp->mrp_maxbw;
32558275SEric Cheng 			if (nmrp->mrp_maxbw == MRP_MAXBW_RESETVAL)
32568275SEric Cheng 				cmrp->mrp_mask &= ~MRP_MAXBW;
32578275SEric Cheng 			else
32588275SEric Cheng 				cmrp->mrp_mask |= MRP_MAXBW;
32598275SEric Cheng 		}
32608275SEric Cheng 		if (nmrp->mrp_mask & MRP_CPUS)
32618275SEric Cheng 			MAC_COPY_CPUS(nmrp, cmrp);
32628275SEric Cheng 	}
32638275SEric Cheng }
32648275SEric Cheng 
32658275SEric Cheng /*
32668275SEric Cheng  * i_mac_set_resources:
32678275SEric Cheng  *
32688275SEric Cheng  * This routine associates properties with the primary MAC client of
32698275SEric Cheng  * the specified MAC instance.
32708275SEric Cheng  * - Cache the properties in mac_impl_t
32718275SEric Cheng  * - Apply the properties to the primary MAC client if exists
32728275SEric Cheng  */
32738275SEric Cheng int
32748275SEric Cheng i_mac_set_resources(mac_handle_t mh, mac_resource_props_t *mrp)
32758275SEric Cheng {
32768275SEric Cheng 	mac_impl_t		*mip = (mac_impl_t *)mh;
32778275SEric Cheng 	mac_client_impl_t	*mcip;
32788275SEric Cheng 	int			err = 0;
32798275SEric Cheng 	mac_resource_props_t	tmrp;
32808275SEric Cheng 
32818275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
32828275SEric Cheng 
32838275SEric Cheng 	err = mac_validate_props(mrp);
32848275SEric Cheng 	if (err != 0)
32858275SEric Cheng 		return (err);
32868275SEric Cheng 
32878275SEric Cheng 	/*
32888275SEric Cheng 	 * Since bind_cpu may be modified by mac_client_set_resources()
32898275SEric Cheng 	 * we use a copy of bind_cpu and finally cache bind_cpu in mip.
32908275SEric Cheng 	 * This allows us to cache only user edits in mip.
32918275SEric Cheng 	 */
32928275SEric Cheng 	bcopy(mrp, &tmrp, sizeof (mac_resource_props_t));
32938275SEric Cheng 	mcip = mac_primary_client_handle(mip);
3294*8833SVenu.Iyer@Sun.COM 	if (mcip != NULL && (mcip->mci_state_flags & MCIS_IS_AGGR_PORT) == 0) {
32958275SEric Cheng 		err =
32968275SEric Cheng 		    mac_client_set_resources((mac_client_handle_t)mcip, &tmrp);
32978275SEric Cheng 	}
32988275SEric Cheng 	/* if mac_client_set_resources failed, do not update the values */
32998275SEric Cheng 	if (err == 0)
33008275SEric Cheng 		mac_update_resources(mrp, &mip->mi_resource_props, B_FALSE);
33018275SEric Cheng 	return (err);
33028275SEric Cheng }
33038275SEric Cheng 
33048275SEric Cheng int
33058275SEric Cheng mac_set_resources(mac_handle_t mh, mac_resource_props_t *mrp)
33068275SEric Cheng {
33078275SEric Cheng 	int err;
33088275SEric Cheng 
33098275SEric Cheng 	i_mac_perim_enter((mac_impl_t *)mh);
33108275SEric Cheng 	err = i_mac_set_resources(mh, mrp);
33118275SEric Cheng 	i_mac_perim_exit((mac_impl_t *)mh);
33128275SEric Cheng 	return (err);
33138275SEric Cheng }
33148275SEric Cheng 
33158275SEric Cheng /*
33168275SEric Cheng  * Get the properties cached for the specified MAC instance.
33178275SEric Cheng  */
33188275SEric Cheng void
33198275SEric Cheng mac_get_resources(mac_handle_t mh, mac_resource_props_t *mrp)
33208275SEric Cheng {
33218275SEric Cheng 	mac_impl_t 		*mip = (mac_impl_t *)mh;
33228275SEric Cheng 	mac_client_impl_t	*mcip;
33238275SEric Cheng 
33248275SEric Cheng 	if (mip->mi_state_flags & MIS_IS_VNIC) {
33258275SEric Cheng 		mcip = mac_primary_client_handle(mip);
33268275SEric Cheng 		if (mcip != NULL) {
33278275SEric Cheng 			mac_client_get_resources((mac_client_handle_t)mcip,
33288275SEric Cheng 			    mrp);
33298275SEric Cheng 			return;
33308275SEric Cheng 		}
33318275SEric Cheng 	}
33328275SEric Cheng 	bcopy(&mip->mi_resource_props, mrp, sizeof (mac_resource_props_t));
33338275SEric Cheng }
33348275SEric Cheng 
33358275SEric Cheng /*
33368275SEric Cheng  * Rename a mac client, its flow, and the kstat.
33378275SEric Cheng  */
33388275SEric Cheng int
33398275SEric Cheng mac_rename_primary(mac_handle_t mh, const char *new_name)
33408275SEric Cheng {
33418275SEric Cheng 	mac_impl_t		*mip = (mac_impl_t *)mh;
33428275SEric Cheng 	mac_client_impl_t	*cur_clnt = NULL;
33438275SEric Cheng 	flow_entry_t		*fep;
33448275SEric Cheng 
33458275SEric Cheng 	i_mac_perim_enter(mip);
33468275SEric Cheng 
33478275SEric Cheng 	/*
33488275SEric Cheng 	 * VNICs: we need to change the sys flow name and
33498275SEric Cheng 	 * the associated flow kstat.
33508275SEric Cheng 	 */
33518275SEric Cheng 	if (mip->mi_state_flags & MIS_IS_VNIC) {
33528275SEric Cheng 		ASSERT(new_name != NULL);
33538275SEric Cheng 		mac_rename_flow_names(mac_vnic_lower(mip), new_name);
33548275SEric Cheng 		goto done;
33558275SEric Cheng 	}
33568275SEric Cheng 	/*
33578275SEric Cheng 	 * This mac may itself be an aggr link, or it may have some client
33588275SEric Cheng 	 * which is an aggr port. For both cases, we need to change the
33598275SEric Cheng 	 * aggr port's mac client name, its flow name and the associated flow
33608275SEric Cheng 	 * kstat.
33618275SEric Cheng 	 */
33628275SEric Cheng 	if (mip->mi_state_flags & MIS_IS_AGGR) {
33638275SEric Cheng 		mac_capab_aggr_t aggr_cap;
33648275SEric Cheng 		mac_rename_fn_t rename_fn;
33658275SEric Cheng 		boolean_t ret;
33668275SEric Cheng 
33678275SEric Cheng 		ASSERT(new_name != NULL);
33688275SEric Cheng 		ret = i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_AGGR,
33698275SEric Cheng 		    (void *)(&aggr_cap));
33708275SEric Cheng 		ASSERT(ret == B_TRUE);
33718275SEric Cheng 		rename_fn = aggr_cap.mca_rename_fn;
33728275SEric Cheng 		rename_fn(new_name, mip->mi_driver);
33738275SEric Cheng 		/*
33748275SEric Cheng 		 * The aggr's client name and kstat flow name will be
33758275SEric Cheng 		 * updated below, i.e. via mac_rename_flow_names.
33768275SEric Cheng 		 */
33778275SEric Cheng 	}
33788275SEric Cheng 
33798275SEric Cheng 	for (cur_clnt = mip->mi_clients_list; cur_clnt != NULL;
33808275SEric Cheng 	    cur_clnt = cur_clnt->mci_client_next) {
33818275SEric Cheng 		if (cur_clnt->mci_state_flags & MCIS_IS_AGGR_PORT) {
33828275SEric Cheng 			if (new_name != NULL) {
33838275SEric Cheng 				char *str_st = cur_clnt->mci_name;
33848275SEric Cheng 				char *str_del = strchr(str_st, '-');
33858275SEric Cheng 
33868275SEric Cheng 				ASSERT(str_del != NULL);
33878275SEric Cheng 				bzero(str_del + 1, MAXNAMELEN -
33888275SEric Cheng 				    (str_del - str_st + 1));
33898275SEric Cheng 				bcopy(new_name, str_del + 1,
33908275SEric Cheng 				    strlen(new_name));
33918275SEric Cheng 			}
33928275SEric Cheng 			fep = cur_clnt->mci_flent;
33938275SEric Cheng 			mac_rename_flow(fep, cur_clnt->mci_name);
33948275SEric Cheng 			break;
33958275SEric Cheng 		} else if (new_name != NULL &&
33968275SEric Cheng 		    cur_clnt->mci_state_flags & MCIS_USE_DATALINK_NAME) {
33978275SEric Cheng 			mac_rename_flow_names(cur_clnt, new_name);
33988275SEric Cheng 			break;
33998275SEric Cheng 		}
34008275SEric Cheng 	}
34018275SEric Cheng 
34028275SEric Cheng done:
34038275SEric Cheng 	i_mac_perim_exit(mip);
34048275SEric Cheng 	return (0);
34058275SEric Cheng }
34068275SEric Cheng 
34078275SEric Cheng /*
34088275SEric Cheng  * Rename the MAC client's flow names
34098275SEric Cheng  */
34108275SEric Cheng static void
34118275SEric Cheng mac_rename_flow_names(mac_client_impl_t *mcip, const char *new_name)
34128275SEric Cheng {
34138275SEric Cheng 	flow_entry_t	*flent;
34148275SEric Cheng 	uint16_t	vid;
34158558SGirish.Moodalbail@Sun.COM 	char		flowname[MAXFLOWNAMELEN];
34168275SEric Cheng 	mac_impl_t	*mip = mcip->mci_mip;
34178275SEric Cheng 
34188275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
34198275SEric Cheng 
34208275SEric Cheng 	/*
34218275SEric Cheng 	 * Use mi_rw_lock to ensure that threads not in the mac perimeter
34228275SEric Cheng 	 * see a self-consistent value for mci_name
34238275SEric Cheng 	 */
34248275SEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
34258275SEric Cheng 	(void) strlcpy(mcip->mci_name, new_name, sizeof (mcip->mci_name));
34268275SEric Cheng 	rw_exit(&mip->mi_rw_lock);
34278275SEric Cheng 
34288275SEric Cheng 	mac_rename_flow(mcip->mci_flent, new_name);
34298275SEric Cheng 
34308275SEric Cheng 	if (mcip->mci_nflents == 1)
34318275SEric Cheng 		return;
34328275SEric Cheng 
34338275SEric Cheng 	/*
34348275SEric Cheng 	 * We have to rename all the others too, no stats to destroy for
34358275SEric Cheng 	 * these.
34368275SEric Cheng 	 */
34378275SEric Cheng 	for (flent = mcip->mci_flent_list; flent != NULL;
34388275SEric Cheng 	    flent = flent->fe_client_next) {
34398275SEric Cheng 		if (flent != mcip->mci_flent) {
34408275SEric Cheng 			vid = i_mac_flow_vid(flent);
34418275SEric Cheng 			(void) sprintf(flowname, "%s%u", new_name, vid);
34428275SEric Cheng 			mac_flow_set_name(flent, flowname);
34438275SEric Cheng 		}
34448275SEric Cheng 	}
34458275SEric Cheng }
34468275SEric Cheng 
34478275SEric Cheng 
34488275SEric Cheng /*
34498275SEric Cheng  * Add a flow to the MAC client's flow list - i.e list of MAC/VID tuples
34508275SEric Cheng  * defined for the specified MAC client.
34518275SEric Cheng  */
34528275SEric Cheng static void
34538275SEric Cheng mac_client_add_to_flow_list(mac_client_impl_t *mcip, flow_entry_t *flent)
34548275SEric Cheng {
34558275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
34568275SEric Cheng 	/*
34578275SEric Cheng 	 * The promisc Rx data path walks the mci_flent_list. Protect by
34588275SEric Cheng 	 * using mi_rw_lock
34598275SEric Cheng 	 */
34608275SEric Cheng 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
34618275SEric Cheng 
34628275SEric Cheng 	/* Add it to the head */
34638275SEric Cheng 	flent->fe_client_next = mcip->mci_flent_list;
34648275SEric Cheng 	mcip->mci_flent_list = flent;
34658275SEric Cheng 	mcip->mci_nflents++;
34668275SEric Cheng 
34678275SEric Cheng 	/*
34688275SEric Cheng 	 * Keep track of the number of non-zero VIDs addresses per MAC
34698275SEric Cheng 	 * client to avoid figuring it out in the data-path.
34708275SEric Cheng 	 */
34718275SEric Cheng 	if (i_mac_flow_vid(flent) != VLAN_ID_NONE)
34728275SEric Cheng 		mcip->mci_nvids++;
34738275SEric Cheng 
34748275SEric Cheng 	rw_exit(&mcip->mci_rw_lock);
34758275SEric Cheng }
34768275SEric Cheng 
34778275SEric Cheng /*
34788275SEric Cheng  * Remove a flow entry from the MAC client's list.
34798275SEric Cheng  */
34808275SEric Cheng static void
34818275SEric Cheng mac_client_remove_flow_from_list(mac_client_impl_t *mcip, flow_entry_t *flent)
34828275SEric Cheng {
34838275SEric Cheng 	flow_entry_t	*fe = mcip->mci_flent_list;
34848275SEric Cheng 	flow_entry_t	*prev_fe = NULL;
34858275SEric Cheng 
34868275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
34878275SEric Cheng 	/*
34888275SEric Cheng 	 * The promisc Rx data path walks the mci_flent_list. Protect by
34898275SEric Cheng 	 * using mci_rw_lock
34908275SEric Cheng 	 */
34918275SEric Cheng 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
34928275SEric Cheng 	while ((fe != NULL) && (fe != flent)) {
34938275SEric Cheng 		prev_fe = fe;
34948275SEric Cheng 		fe = fe->fe_client_next;
34958275SEric Cheng 	}
34968275SEric Cheng 
34978558SGirish.Moodalbail@Sun.COM 	ASSERT(fe != NULL);
34988558SGirish.Moodalbail@Sun.COM 	if (prev_fe == NULL) {
34998558SGirish.Moodalbail@Sun.COM 		/* Deleting the first node */
35008558SGirish.Moodalbail@Sun.COM 		mcip->mci_flent_list = fe->fe_client_next;
35018558SGirish.Moodalbail@Sun.COM 	} else {
35028558SGirish.Moodalbail@Sun.COM 		prev_fe->fe_client_next = fe->fe_client_next;
35038275SEric Cheng 	}
35048558SGirish.Moodalbail@Sun.COM 	mcip->mci_nflents--;
35058558SGirish.Moodalbail@Sun.COM 
35068558SGirish.Moodalbail@Sun.COM 	if (i_mac_flow_vid(flent) != VLAN_ID_NONE)
35078558SGirish.Moodalbail@Sun.COM 		mcip->mci_nvids--;
35088558SGirish.Moodalbail@Sun.COM 
35098275SEric Cheng 	rw_exit(&mcip->mci_rw_lock);
35108275SEric Cheng }
35118275SEric Cheng 
35128275SEric Cheng /*
35138275SEric Cheng  * Check if the given VID belongs to this MAC client.
35148275SEric Cheng  */
35158275SEric Cheng boolean_t
35168275SEric Cheng mac_client_check_flow_vid(mac_client_impl_t *mcip, uint16_t vid)
35178275SEric Cheng {
35188275SEric Cheng 	flow_entry_t	*flent;
35198275SEric Cheng 	uint16_t	mci_vid;
35208275SEric Cheng 
35218275SEric Cheng 	/* The mci_flent_list is protected by mci_rw_lock */
35228275SEric Cheng 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
35238275SEric Cheng 	for (flent = mcip->mci_flent_list; flent != NULL;
35248275SEric Cheng 	    flent = flent->fe_client_next) {
35258275SEric Cheng 		mci_vid = i_mac_flow_vid(flent);
35268275SEric Cheng 		if (vid == mci_vid) {
35278275SEric Cheng 			rw_exit(&mcip->mci_rw_lock);
35288275SEric Cheng 			return (B_TRUE);
35298275SEric Cheng 		}
35308275SEric Cheng 	}
35318275SEric Cheng 	rw_exit(&mcip->mci_rw_lock);
35328275SEric Cheng 	return (B_FALSE);
35338275SEric Cheng }
35348275SEric Cheng 
35358275SEric Cheng /*
35368275SEric Cheng  * Get the flow entry for the specified <MAC addr, VID> tuple.
35378275SEric Cheng  */
35388275SEric Cheng static flow_entry_t *
35398275SEric Cheng mac_client_get_flow(mac_client_impl_t *mcip, mac_unicast_impl_t *muip)
35408275SEric Cheng {
35418275SEric Cheng 	mac_address_t *map = mcip->mci_unicast;
35428275SEric Cheng 	flow_entry_t *flent;
35438275SEric Cheng 	uint16_t vid;
35448275SEric Cheng 	flow_desc_t flow_desc;
35458275SEric Cheng 
35468275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
35478275SEric Cheng 
35488275SEric Cheng 	mac_flow_get_desc(mcip->mci_flent, &flow_desc);
35498275SEric Cheng 	if (bcmp(flow_desc.fd_dst_mac, map->ma_addr, map->ma_len) != 0)
35508275SEric Cheng 		return (NULL);
35518275SEric Cheng 
35528275SEric Cheng 	for (flent = mcip->mci_flent_list; flent != NULL;
35538275SEric Cheng 	    flent = flent->fe_client_next) {
35548275SEric Cheng 		vid = i_mac_flow_vid(flent);
35558275SEric Cheng 		if (vid == muip->mui_vid) {
35568275SEric Cheng 			return (flent);
35578275SEric Cheng 		}
35588275SEric Cheng 	}
35598275SEric Cheng 
35608275SEric Cheng 	return (NULL);
35618275SEric Cheng }
35628275SEric Cheng 
35638275SEric Cheng /*
35648275SEric Cheng  * Since mci_flent has the SRSs, when we want to remove it, we replace
35658275SEric Cheng  * the flow_desc_t in mci_flent with that of an existing flent and then
35668275SEric Cheng  * remove that flent instead of mci_flent.
35678275SEric Cheng  */
35688275SEric Cheng static flow_entry_t *
35698275SEric Cheng mac_client_swap_mciflent(mac_client_impl_t *mcip)
35708275SEric Cheng {
35718275SEric Cheng 	flow_entry_t	*flent = mcip->mci_flent;
35728275SEric Cheng 	flow_tab_t	*ft = flent->fe_flow_tab;
35738275SEric Cheng 	flow_entry_t	*flent1;
35748275SEric Cheng 	flow_desc_t	fl_desc;
35758558SGirish.Moodalbail@Sun.COM 	char		fl_name[MAXFLOWNAMELEN];
35768275SEric Cheng 	int		err;
35778275SEric Cheng 
35788275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
35798275SEric Cheng 	ASSERT(mcip->mci_nflents > 1);
35808275SEric Cheng 
35818275SEric Cheng 	/* get the next flent following the primary flent  */
35828275SEric Cheng 	flent1 = mcip->mci_flent_list->fe_client_next;
35838275SEric Cheng 	ASSERT(flent1 != NULL && flent1->fe_flow_tab == ft);
35848275SEric Cheng 
35858275SEric Cheng 	/*
35868275SEric Cheng 	 * Remove the flent from the flow table before updating the
35878275SEric Cheng 	 * flow descriptor as the hash depends on the flow descriptor.
35888275SEric Cheng 	 * This also helps incoming packet classification avoid having
35898275SEric Cheng 	 * to grab fe_lock. Access to fe_flow_desc of a flent not in the
35908275SEric Cheng 	 * flow table is done under the fe_lock so that log or stat functions
35918275SEric Cheng 	 * see a self-consistent fe_flow_desc. The name and desc are specific
35928275SEric Cheng 	 * to a flow, the rest are shared by all the clients, including
35938275SEric Cheng 	 * resource control etc.
35948275SEric Cheng 	 */
35958275SEric Cheng 	mac_flow_remove(ft, flent, B_TRUE);
35968275SEric Cheng 	mac_flow_remove(ft, flent1, B_TRUE);
35978275SEric Cheng 
35988275SEric Cheng 	bcopy(&flent->fe_flow_desc, &fl_desc, sizeof (flow_desc_t));
35998558SGirish.Moodalbail@Sun.COM 	bcopy(flent->fe_flow_name, fl_name, MAXFLOWNAMELEN);
36008275SEric Cheng 
36018275SEric Cheng 	/* update the primary flow entry */
36028275SEric Cheng 	mutex_enter(&flent->fe_lock);
36038275SEric Cheng 	bcopy(&flent1->fe_flow_desc, &flent->fe_flow_desc,
36048275SEric Cheng 	    sizeof (flow_desc_t));
36058558SGirish.Moodalbail@Sun.COM 	bcopy(&flent1->fe_flow_name, &flent->fe_flow_name, MAXFLOWNAMELEN);
36068275SEric Cheng 	mutex_exit(&flent->fe_lock);
36078275SEric Cheng 
36088275SEric Cheng 	/* update the flow entry that is to be freed */
36098275SEric Cheng 	mutex_enter(&flent1->fe_lock);
36108275SEric Cheng 	bcopy(&fl_desc, &flent1->fe_flow_desc, sizeof (flow_desc_t));
36118558SGirish.Moodalbail@Sun.COM 	bcopy(fl_name, &flent1->fe_flow_name, MAXFLOWNAMELEN);
36128275SEric Cheng 	mutex_exit(&flent1->fe_lock);
36138275SEric Cheng 
36148275SEric Cheng 	/* now reinsert the flow entries in the table */
36158275SEric Cheng 	err = mac_flow_add(ft, flent);
36168275SEric Cheng 	ASSERT(err == 0);
36178275SEric Cheng 
36188275SEric Cheng 	err = mac_flow_add(ft, flent1);
36198275SEric Cheng 	ASSERT(err == 0);
36208275SEric Cheng 
36218275SEric Cheng 	return (flent1);
36228275SEric Cheng }
36238275SEric Cheng 
36248275SEric Cheng /*
36258275SEric Cheng  * Return whether there is only one flow entry associated with this
36268275SEric Cheng  * MAC client.
36278275SEric Cheng  */
36288275SEric Cheng static boolean_t
36298275SEric Cheng mac_client_single_rcvr(mac_client_impl_t *mcip)
36308275SEric Cheng {
36318275SEric Cheng 	return (mcip->mci_nflents == 1);
36328275SEric Cheng }
36338275SEric Cheng 
36348275SEric Cheng int
36358275SEric Cheng mac_validate_props(mac_resource_props_t *mrp)
36368275SEric Cheng {
36378275SEric Cheng 	if (mrp == NULL)
36388275SEric Cheng 		return (0);
36398275SEric Cheng 
36408275SEric Cheng 	if (mrp->mrp_mask & MRP_PRIORITY) {
36418275SEric Cheng 		mac_priority_level_t	pri = mrp->mrp_priority;
36428275SEric Cheng 
36438275SEric Cheng 		if (pri < MPL_LOW || pri > MPL_RESET)
36448275SEric Cheng 			return (EINVAL);
36458275SEric Cheng 	}
36468275SEric Cheng 
36478275SEric Cheng 	if (mrp->mrp_mask & MRP_MAXBW) {
36488275SEric Cheng 		uint64_t maxbw = mrp->mrp_maxbw;
36498275SEric Cheng 
36508275SEric Cheng 		if (maxbw < MRP_MAXBW_MINVAL && maxbw != 0)
36518275SEric Cheng 			return (EINVAL);
36528275SEric Cheng 	}
36538275SEric Cheng 	if (mrp->mrp_mask & MRP_CPUS) {
36548275SEric Cheng 		int i;
36558275SEric Cheng 		mac_cpu_mode_t	fanout;
36568275SEric Cheng 
36578275SEric Cheng 		if (mrp->mrp_ncpus > ncpus || mrp->mrp_ncpus > MAX_SR_FANOUT)
36588275SEric Cheng 			return (EINVAL);
36598275SEric Cheng 
36608275SEric Cheng 		for (i = 0; i < mrp->mrp_ncpus; i++) {
36618275SEric Cheng 			cpu_t *cp;
36628275SEric Cheng 			int rv;
36638275SEric Cheng 
36648275SEric Cheng 			mutex_enter(&cpu_lock);
36658275SEric Cheng 			cp = cpu_get(mrp->mrp_cpu[i]);
36668275SEric Cheng 			if (cp != NULL)
36678275SEric Cheng 				rv = cpu_is_online(cp);
36688275SEric Cheng 			else
36698275SEric Cheng 				rv = 0;
36708275SEric Cheng 			mutex_exit(&cpu_lock);
36718275SEric Cheng 			if (rv == 0)
36728275SEric Cheng 				return (EINVAL);
36738275SEric Cheng 		}
36748275SEric Cheng 
36758275SEric Cheng 		fanout = mrp->mrp_fanout_mode;
36768275SEric Cheng 		if (fanout < 0 || fanout > MCM_CPUS)
36778275SEric Cheng 			return (EINVAL);
36788275SEric Cheng 	}
36798275SEric Cheng 	return (0);
36808275SEric Cheng }
36818275SEric Cheng 
36828275SEric Cheng /*
36838275SEric Cheng  * Send a MAC_NOTE_LINK notification to all the MAC clients whenever the
36848275SEric Cheng  * underlying physical link is down. This is to allow MAC clients to
36858275SEric Cheng  * communicate with other clients.
36868275SEric Cheng  */
36878275SEric Cheng void
36888275SEric Cheng mac_virtual_link_update(mac_impl_t *mip)
36898275SEric Cheng {
36908275SEric Cheng 	if (mip->mi_linkstate != LINK_STATE_UP)
36918275SEric Cheng 		i_mac_notify(mip, MAC_NOTE_LINK);
36928275SEric Cheng }
36938275SEric Cheng 
36948275SEric Cheng /*
36958275SEric Cheng  * For clients that have a pass-thru MAC, e.g. VNIC, we set the VNIC's
36968275SEric Cheng  * mac handle in the client.
36978275SEric Cheng  */
36988275SEric Cheng void
36998275SEric Cheng mac_set_upper_mac(mac_client_handle_t mch, mac_handle_t mh)
37008275SEric Cheng {
37018275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
37028275SEric Cheng 
37038275SEric Cheng 	mcip->mci_upper_mip = (mac_impl_t *)mh;
37048275SEric Cheng }
37058275SEric Cheng 
37068275SEric Cheng /*
37078275SEric Cheng  * Mark the mac as being used exclusively by the single mac client that is
37088275SEric Cheng  * doing some control operation on this mac. No further opens of this mac
37098275SEric Cheng  * will be allowed until this client calls mac_unmark_exclusive. The mac
37108275SEric Cheng  * client calling this function must already be in the mac perimeter
37118275SEric Cheng  */
37128275SEric Cheng int
37138275SEric Cheng mac_mark_exclusive(mac_handle_t mh)
37148275SEric Cheng {
37158275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
37168275SEric Cheng 
37178275SEric Cheng 	ASSERT(MAC_PERIM_HELD(mh));
37188275SEric Cheng 	/*
37198275SEric Cheng 	 * Look up its entry in the global hash table.
37208275SEric Cheng 	 */
37218275SEric Cheng 	rw_enter(&i_mac_impl_lock, RW_WRITER);
37228275SEric Cheng 	if (mip->mi_state_flags & MIS_DISABLED) {
37238275SEric Cheng 		rw_exit(&i_mac_impl_lock);
37248275SEric Cheng 		return (ENOENT);
37258275SEric Cheng 	}
37268275SEric Cheng 
37278275SEric Cheng 	/*
37288275SEric Cheng 	 * A reference to mac is held even if the link is not plumbed.
37298275SEric Cheng 	 * In i_dls_link_create() we open the MAC interface and hold the
37308275SEric Cheng 	 * reference. There is an additional reference for the mac_open
37318275SEric Cheng 	 * done in acquiring the mac perimeter
37328275SEric Cheng 	 */
37338275SEric Cheng 	if (mip->mi_ref != 2) {
37348275SEric Cheng 		rw_exit(&i_mac_impl_lock);
37358275SEric Cheng 		return (EBUSY);
37368275SEric Cheng 	}
37378275SEric Cheng 
37388275SEric Cheng 	ASSERT(!(mip->mi_state_flags & MIS_EXCLUSIVE_HELD));
37398275SEric Cheng 	mip->mi_state_flags |= MIS_EXCLUSIVE_HELD;
37408275SEric Cheng 	rw_exit(&i_mac_impl_lock);
37418275SEric Cheng 	return (0);
37428275SEric Cheng }
37438275SEric Cheng 
37448275SEric Cheng void
37458275SEric Cheng mac_unmark_exclusive(mac_handle_t mh)
37468275SEric Cheng {
37478275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
37488275SEric Cheng 
37498275SEric Cheng 	ASSERT(MAC_PERIM_HELD(mh));
37508275SEric Cheng 
37518275SEric Cheng 	rw_enter(&i_mac_impl_lock, RW_WRITER);
37528275SEric Cheng 	/* 1 for the creation and another for the perimeter */
37538275SEric Cheng 	ASSERT(mip->mi_ref == 2 && (mip->mi_state_flags & MIS_EXCLUSIVE_HELD));
37548275SEric Cheng 	mip->mi_state_flags &= ~MIS_EXCLUSIVE_HELD;
37558275SEric Cheng 	rw_exit(&i_mac_impl_lock);
37568275SEric Cheng }
37578275SEric Cheng 
37588275SEric Cheng /*
37598275SEric Cheng  * Set the MTU for the specified device. The function returns EBUSY if
37608275SEric Cheng  * another MAC client prevents the caller to become the exclusive client.
37618275SEric Cheng  * Returns EAGAIN if the client is started.
37628275SEric Cheng  */
37638275SEric Cheng int
37648275SEric Cheng mac_set_mtu(mac_handle_t mh, uint_t new_mtu, uint_t *old_mtu_arg)
37658275SEric Cheng {
37668275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
37678275SEric Cheng 	uint_t old_mtu;
37688275SEric Cheng 	int rv;
37698275SEric Cheng 	boolean_t exclusive = B_FALSE;
37708275SEric Cheng 
37718275SEric Cheng 	i_mac_perim_enter(mip);
37728275SEric Cheng 
37738275SEric Cheng 	if ((mip->mi_callbacks->mc_callbacks & MC_SETPROP) == 0 ||
37748275SEric Cheng 	    (mip->mi_callbacks->mc_callbacks & MC_GETPROP) == 0) {
37758275SEric Cheng 		rv = ENOTSUP;
37768275SEric Cheng 		goto bail;
37778275SEric Cheng 	}
37788275SEric Cheng 
37798275SEric Cheng 	if ((rv = mac_mark_exclusive(mh)) != 0)
37808275SEric Cheng 		goto bail;
37818275SEric Cheng 	exclusive = B_TRUE;
37828275SEric Cheng 
37838275SEric Cheng 	if (mip->mi_active > 0) {
37848275SEric Cheng 		/*
37858275SEric Cheng 		 * The MAC instance is started, for example due to the
37868275SEric Cheng 		 * presence of a promiscuous clients. Fail the operation
37878275SEric Cheng 		 * since the MAC's MTU cannot be changed while the NIC
37888275SEric Cheng 		 * is started.
37898275SEric Cheng 		 */
37908275SEric Cheng 		rv = EAGAIN;
37918275SEric Cheng 		goto bail;
37928275SEric Cheng 	}
37938275SEric Cheng 
37948275SEric Cheng 	mac_sdu_get(mh, NULL, &old_mtu);
37958275SEric Cheng 
37968275SEric Cheng 	if (old_mtu != new_mtu) {
37978275SEric Cheng 		rv = mip->mi_callbacks->mc_setprop(mip->mi_driver,
37988275SEric Cheng 		    "mtu", MAC_PROP_MTU, sizeof (uint_t), &new_mtu);
37998275SEric Cheng 	}
38008275SEric Cheng 
38018275SEric Cheng bail:
38028275SEric Cheng 	if (exclusive)
38038275SEric Cheng 		mac_unmark_exclusive(mh);
38048275SEric Cheng 	i_mac_perim_exit(mip);
38058275SEric Cheng 
38068275SEric Cheng 	if (rv == 0 && old_mtu_arg != NULL)
38078275SEric Cheng 		*old_mtu_arg = old_mtu;
38088275SEric Cheng 	return (rv);
38098275SEric Cheng }
38108275SEric Cheng 
38118275SEric Cheng void
38128275SEric Cheng mac_get_hwgrp_info(mac_handle_t mh, int grp_index, uint_t *grp_num,
38138275SEric Cheng     uint_t *n_rings, uint_t *type, uint_t *n_clnts, char *clnts_name)
38148275SEric Cheng {
38158275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
38168275SEric Cheng 	mac_grp_client_t *mcip;
38178275SEric Cheng 	uint_t i = 0, index = 0;
38188275SEric Cheng 
38198275SEric Cheng 	/* Revisit when we implement fully dynamic group allocation */
38208275SEric Cheng 	ASSERT(grp_index >= 0 && grp_index < mip->mi_rx_group_count);
38218275SEric Cheng 
38228275SEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_READER);
38238275SEric Cheng 	*grp_num = mip->mi_rx_groups[grp_index].mrg_index;
38248275SEric Cheng 	*type = mip->mi_rx_groups[grp_index].mrg_type;
38258275SEric Cheng 	*n_rings = mip->mi_rx_groups[grp_index].mrg_cur_count;
38268275SEric Cheng 	for (mcip = mip->mi_rx_groups[grp_index].mrg_clients; mcip != NULL;
38278275SEric Cheng 	    mcip = mcip->mgc_next) {
38288275SEric Cheng 		int name_len = strlen(mcip->mgc_client->mci_name);
38298275SEric Cheng 
38308275SEric Cheng 		/*
38318275SEric Cheng 		 * MAXCLIENTNAMELEN is the buffer size reserved for client
38328275SEric Cheng 		 * names.
38338275SEric Cheng 		 * XXXX Formating the client name string needs to be moved
38348275SEric Cheng 		 * to user land when fixing the size of dhi_clnts in
38358275SEric Cheng 		 * dld_hwgrpinfo_t. We should use n_clients * client_name for
38368275SEric Cheng 		 * dhi_clntsin instead of MAXCLIENTNAMELEN
38378275SEric Cheng 		 */
38388275SEric Cheng 		if (index + name_len >= MAXCLIENTNAMELEN) {
38398275SEric Cheng 			index = MAXCLIENTNAMELEN;
38408275SEric Cheng 			break;
38418275SEric Cheng 		}
38428275SEric Cheng 		bcopy(mcip->mgc_client->mci_name, &(clnts_name[index]),
38438275SEric Cheng 		    name_len);
38448275SEric Cheng 		index += name_len;
38458275SEric Cheng 		clnts_name[index++] = ',';
38468275SEric Cheng 		i++;
38478275SEric Cheng 	}
38488275SEric Cheng 
38498275SEric Cheng 	/* Get rid of the last , */
38508275SEric Cheng 	if (index > 0)
38518275SEric Cheng 		clnts_name[index - 1] = '\0';
38528275SEric Cheng 	*n_clnts = i;
38538275SEric Cheng 	rw_exit(&mip->mi_rw_lock);
38548275SEric Cheng }
38558275SEric Cheng 
38568275SEric Cheng uint_t
38578275SEric Cheng mac_hwgrp_num(mac_handle_t mh)
38588275SEric Cheng {
38598275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
38608275SEric Cheng 
38618275SEric Cheng 	return (mip->mi_rx_group_count);
38628275SEric Cheng }
3863