xref: /onnv-gate/usr/src/uts/common/io/mac/mac_provider.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 /*
23*8833SVenu.Iyer@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
248275SEric Cheng  * Use is subject to license terms.
258275SEric Cheng  */
268275SEric Cheng 
278275SEric Cheng #include <sys/types.h>
288275SEric Cheng #include <sys/conf.h>
298275SEric Cheng #include <sys/id_space.h>
308275SEric Cheng #include <sys/esunddi.h>
318275SEric Cheng #include <sys/stat.h>
328275SEric Cheng #include <sys/mkdev.h>
338275SEric Cheng #include <sys/stream.h>
348275SEric Cheng #include <sys/strsubr.h>
358275SEric Cheng #include <sys/dlpi.h>
368275SEric Cheng #include <sys/modhash.h>
378275SEric Cheng #include <sys/mac.h>
388275SEric Cheng #include <sys/mac_provider.h>
398275SEric Cheng #include <sys/mac_impl.h>
408275SEric Cheng #include <sys/mac_client_impl.h>
418275SEric Cheng #include <sys/mac_client_priv.h>
428275SEric Cheng #include <sys/mac_soft_ring.h>
438275SEric Cheng #include <sys/modctl.h>
448275SEric Cheng #include <sys/fs/dv_node.h>
458275SEric Cheng #include <sys/thread.h>
468275SEric Cheng #include <sys/proc.h>
478275SEric Cheng #include <sys/callb.h>
488275SEric Cheng #include <sys/cpuvar.h>
498275SEric Cheng #include <sys/atomic.h>
508275SEric Cheng #include <sys/sdt.h>
518275SEric Cheng #include <sys/mac_flow.h>
528275SEric Cheng #include <sys/ddi_intr_impl.h>
538275SEric Cheng #include <sys/disp.h>
548275SEric Cheng #include <sys/sdt.h>
558275SEric Cheng 
568275SEric Cheng /*
578275SEric Cheng  * MAC Provider Interface.
588275SEric Cheng  *
598275SEric Cheng  * Interface for GLDv3 compatible NIC drivers.
608275SEric Cheng  */
618275SEric Cheng 
628275SEric Cheng static void i_mac_notify_thread(void *);
638275SEric Cheng 
648275SEric Cheng typedef void (*mac_notify_default_cb_fn_t)(mac_impl_t *);
658275SEric Cheng 
668275SEric Cheng typedef struct mac_notify_default_cb_s {
678275SEric Cheng 	mac_notify_type_t		mac_notify_type;
688275SEric Cheng 	mac_notify_default_cb_fn_t	mac_notify_cb_fn;
698275SEric Cheng }mac_notify_default_cb_t;
708275SEric Cheng 
718275SEric Cheng mac_notify_default_cb_t mac_notify_cb_list[] = {
728275SEric Cheng 	{ MAC_NOTE_LINK,		mac_fanout_recompute},
738275SEric Cheng 	{ MAC_NOTE_PROMISC,		NULL},
748275SEric Cheng 	{ MAC_NOTE_UNICST,		NULL},
758275SEric Cheng 	{ MAC_NOTE_TX,			NULL},
768275SEric Cheng 	{ MAC_NOTE_RESOURCE,		NULL},
778275SEric Cheng 	{ MAC_NOTE_DEVPROMISC,		NULL},
788275SEric Cheng 	{ MAC_NOTE_FASTPATH_FLUSH,	NULL},
798275SEric Cheng 	{ MAC_NOTE_SDU_SIZE,		NULL},
808275SEric Cheng 	{ MAC_NOTE_MARGIN,		NULL},
818275SEric Cheng 	{ MAC_NOTE_CAPAB_CHG,		NULL},
828275SEric Cheng 	{ MAC_NNOTE,			NULL},
838275SEric Cheng };
848275SEric Cheng 
858275SEric Cheng /*
868275SEric Cheng  * Driver support functions.
878275SEric Cheng  */
888275SEric Cheng 
898275SEric Cheng /* REGISTRATION */
908275SEric Cheng 
918275SEric Cheng mac_register_t *
928275SEric Cheng mac_alloc(uint_t mac_version)
938275SEric Cheng {
948275SEric Cheng 	mac_register_t *mregp;
958275SEric Cheng 
968275SEric Cheng 	/*
978275SEric Cheng 	 * Make sure there isn't a version mismatch between the driver and
988275SEric Cheng 	 * the framework.  In the future, if multiple versions are
998275SEric Cheng 	 * supported, this check could become more sophisticated.
1008275SEric Cheng 	 */
1018275SEric Cheng 	if (mac_version != MAC_VERSION)
1028275SEric Cheng 		return (NULL);
1038275SEric Cheng 
1048275SEric Cheng 	mregp = kmem_zalloc(sizeof (mac_register_t), KM_SLEEP);
1058275SEric Cheng 	mregp->m_version = mac_version;
1068275SEric Cheng 	return (mregp);
1078275SEric Cheng }
1088275SEric Cheng 
1098275SEric Cheng void
1108275SEric Cheng mac_free(mac_register_t *mregp)
1118275SEric Cheng {
1128275SEric Cheng 	kmem_free(mregp, sizeof (mac_register_t));
1138275SEric Cheng }
1148275SEric Cheng 
1158275SEric Cheng /*
1168275SEric Cheng  * mac_register() is how drivers register new MACs with the GLDv3
1178275SEric Cheng  * framework.  The mregp argument is allocated by drivers using the
1188275SEric Cheng  * mac_alloc() function, and can be freed using mac_free() immediately upon
1198275SEric Cheng  * return from mac_register().  Upon success (0 return value), the mhp
1208275SEric Cheng  * opaque pointer becomes the driver's handle to its MAC interface, and is
1218275SEric Cheng  * the argument to all other mac module entry points.
1228275SEric Cheng  */
1238275SEric Cheng /* ARGSUSED */
1248275SEric Cheng int
1258275SEric Cheng mac_register(mac_register_t *mregp, mac_handle_t *mhp)
1268275SEric Cheng {
1278275SEric Cheng 	mac_impl_t		*mip;
1288275SEric Cheng 	mactype_t		*mtype;
1298275SEric Cheng 	int			err = EINVAL;
1308275SEric Cheng 	struct devnames		*dnp = NULL;
1318275SEric Cheng 	uint_t			instance;
1328275SEric Cheng 	boolean_t		style1_created = B_FALSE;
1338275SEric Cheng 	boolean_t		style2_created = B_FALSE;
1348275SEric Cheng 	mac_capab_legacy_t	legacy;
1358275SEric Cheng 	char			*driver;
1368275SEric Cheng 	minor_t			minor = 0;
1378275SEric Cheng 
1388275SEric Cheng 	/* Find the required MAC-Type plugin. */
1398275SEric Cheng 	if ((mtype = mactype_getplugin(mregp->m_type_ident)) == NULL)
1408275SEric Cheng 		return (EINVAL);
1418275SEric Cheng 
1428275SEric Cheng 	/* Create a mac_impl_t to represent this MAC. */
1438275SEric Cheng 	mip = kmem_cache_alloc(i_mac_impl_cachep, KM_SLEEP);
1448275SEric Cheng 
1458275SEric Cheng 	/*
1468275SEric Cheng 	 * The mac is not ready for open yet.
1478275SEric Cheng 	 */
1488275SEric Cheng 	mip->mi_state_flags |= MIS_DISABLED;
1498275SEric Cheng 
1508275SEric Cheng 	/*
1518275SEric Cheng 	 * When a mac is registered, the m_instance field can be set to:
1528275SEric Cheng 	 *
1538275SEric Cheng 	 *  0:	Get the mac's instance number from m_dip.
1548275SEric Cheng 	 *	This is usually used for physical device dips.
1558275SEric Cheng 	 *
1568275SEric Cheng 	 *  [1 .. MAC_MAX_MINOR-1]: Use the value as the mac's instance number.
1578275SEric Cheng 	 *	For example, when an aggregation is created with the key option,
1588275SEric Cheng 	 *	"key" will be used as the instance number.
1598275SEric Cheng 	 *
1608275SEric Cheng 	 *  -1: Assign an instance number from [MAC_MAX_MINOR .. MAXMIN-1].
1618275SEric Cheng 	 *	This is often used when a MAC of a virtual link is registered
1628275SEric Cheng 	 *	(e.g., aggregation when "key" is not specified, or vnic).
1638275SEric Cheng 	 *
1648275SEric Cheng 	 * Note that the instance number is used to derive the mi_minor field
1658275SEric Cheng 	 * of mac_impl_t, which will then be used to derive the name of kstats
1668275SEric Cheng 	 * and the devfs nodes.  The first 2 cases are needed to preserve
1678275SEric Cheng 	 * backward compatibility.
1688275SEric Cheng 	 */
1698275SEric Cheng 	switch (mregp->m_instance) {
1708275SEric Cheng 	case 0:
1718275SEric Cheng 		instance = ddi_get_instance(mregp->m_dip);
1728275SEric Cheng 		break;
1738275SEric Cheng 	case ((uint_t)-1):
1748275SEric Cheng 		minor = mac_minor_hold(B_TRUE);
1758275SEric Cheng 		if (minor == 0) {
1768275SEric Cheng 			err = ENOSPC;
1778275SEric Cheng 			goto fail;
1788275SEric Cheng 		}
1798275SEric Cheng 		instance = minor - 1;
1808275SEric Cheng 		break;
1818275SEric Cheng 	default:
1828275SEric Cheng 		instance = mregp->m_instance;
1838275SEric Cheng 		if (instance >= MAC_MAX_MINOR) {
1848275SEric Cheng 			err = EINVAL;
1858275SEric Cheng 			goto fail;
1868275SEric Cheng 		}
1878275SEric Cheng 		break;
1888275SEric Cheng 	}
1898275SEric Cheng 
1908275SEric Cheng 	mip->mi_minor = (minor_t)(instance + 1);
1918275SEric Cheng 	mip->mi_dip = mregp->m_dip;
1928275SEric Cheng 	mip->mi_clients_list = NULL;
1938275SEric Cheng 	mip->mi_nclients = 0;
1948275SEric Cheng 
1958275SEric Cheng 	driver = (char *)ddi_driver_name(mip->mi_dip);
1968275SEric Cheng 
1978275SEric Cheng 	/* Construct the MAC name as <drvname><instance> */
1988275SEric Cheng 	(void) snprintf(mip->mi_name, sizeof (mip->mi_name), "%s%d",
1998275SEric Cheng 	    driver, instance);
2008275SEric Cheng 
2018275SEric Cheng 	mip->mi_driver = mregp->m_driver;
2028275SEric Cheng 
2038275SEric Cheng 	mip->mi_type = mtype;
2048275SEric Cheng 	mip->mi_margin = mregp->m_margin;
2058275SEric Cheng 	mip->mi_info.mi_media = mtype->mt_type;
2068275SEric Cheng 	mip->mi_info.mi_nativemedia = mtype->mt_nativetype;
2078275SEric Cheng 	if (mregp->m_max_sdu <= mregp->m_min_sdu)
2088275SEric Cheng 		goto fail;
2098275SEric Cheng 	mip->mi_sdu_min = mregp->m_min_sdu;
2108275SEric Cheng 	mip->mi_sdu_max = mregp->m_max_sdu;
2118275SEric Cheng 	mip->mi_info.mi_addr_length = mip->mi_type->mt_addr_length;
2128275SEric Cheng 	/*
2138275SEric Cheng 	 * If the media supports a broadcast address, cache a pointer to it
2148275SEric Cheng 	 * in the mac_info_t so that upper layers can use it.
2158275SEric Cheng 	 */
2168275SEric Cheng 	mip->mi_info.mi_brdcst_addr = mip->mi_type->mt_brdcst_addr;
2178275SEric Cheng 
2188275SEric Cheng 	mip->mi_v12n_level = mregp->m_v12n;
2198275SEric Cheng 
2208275SEric Cheng 	/*
2218275SEric Cheng 	 * Copy the unicast source address into the mac_info_t, but only if
2228275SEric Cheng 	 * the MAC-Type defines a non-zero address length.  We need to
2238275SEric Cheng 	 * handle MAC-Types that have an address length of 0
2248275SEric Cheng 	 * (point-to-point protocol MACs for example).
2258275SEric Cheng 	 */
2268275SEric Cheng 	if (mip->mi_type->mt_addr_length > 0) {
2278275SEric Cheng 		if (mregp->m_src_addr == NULL)
2288275SEric Cheng 			goto fail;
2298275SEric Cheng 		mip->mi_info.mi_unicst_addr =
2308275SEric Cheng 		    kmem_alloc(mip->mi_type->mt_addr_length, KM_SLEEP);
2318275SEric Cheng 		bcopy(mregp->m_src_addr, mip->mi_info.mi_unicst_addr,
2328275SEric Cheng 		    mip->mi_type->mt_addr_length);
2338275SEric Cheng 
2348275SEric Cheng 		/*
2358275SEric Cheng 		 * Copy the fixed 'factory' MAC address from the immutable
2368275SEric Cheng 		 * info.  This is taken to be the MAC address currently in
2378275SEric Cheng 		 * use.
2388275SEric Cheng 		 */
2398275SEric Cheng 		bcopy(mip->mi_info.mi_unicst_addr, mip->mi_addr,
2408275SEric Cheng 		    mip->mi_type->mt_addr_length);
2418275SEric Cheng 
2428275SEric Cheng 		/*
2438275SEric Cheng 		 * At this point, we should set up the classification
2448275SEric Cheng 		 * rules etc but we delay it till mac_open() so that
2458275SEric Cheng 		 * the resource discovery has taken place and we
2468275SEric Cheng 		 * know someone wants to use the device. Otherwise
2478275SEric Cheng 		 * memory gets allocated for Rx ring structures even
2488275SEric Cheng 		 * during probe.
2498275SEric Cheng 		 */
2508275SEric Cheng 
2518275SEric Cheng 		/* Copy the destination address if one is provided. */
2528275SEric Cheng 		if (mregp->m_dst_addr != NULL) {
2538275SEric Cheng 			bcopy(mregp->m_dst_addr, mip->mi_dstaddr,
2548275SEric Cheng 			    mip->mi_type->mt_addr_length);
2558275SEric Cheng 		}
2568275SEric Cheng 	} else if (mregp->m_src_addr != NULL) {
2578275SEric Cheng 		goto fail;
2588275SEric Cheng 	}
2598275SEric Cheng 
2608275SEric Cheng 	/*
2618275SEric Cheng 	 * The format of the m_pdata is specific to the plugin.  It is
2628275SEric Cheng 	 * passed in as an argument to all of the plugin callbacks.  The
2638275SEric Cheng 	 * driver can update this information by calling
2648275SEric Cheng 	 * mac_pdata_update().
2658275SEric Cheng 	 */
2668275SEric Cheng 	if (mregp->m_pdata != NULL) {
2678275SEric Cheng 		/*
2688275SEric Cheng 		 * Verify that the plugin supports MAC plugin data and that
2698275SEric Cheng 		 * the supplied data is valid.
2708275SEric Cheng 		 */
2718275SEric Cheng 		if (!(mip->mi_type->mt_ops.mtops_ops & MTOPS_PDATA_VERIFY))
2728275SEric Cheng 			goto fail;
2738275SEric Cheng 		if (!mip->mi_type->mt_ops.mtops_pdata_verify(mregp->m_pdata,
2748275SEric Cheng 		    mregp->m_pdata_size)) {
2758275SEric Cheng 			goto fail;
2768275SEric Cheng 		}
2778275SEric Cheng 		mip->mi_pdata = kmem_alloc(mregp->m_pdata_size, KM_SLEEP);
2788275SEric Cheng 		bcopy(mregp->m_pdata, mip->mi_pdata, mregp->m_pdata_size);
2798275SEric Cheng 		mip->mi_pdata_size = mregp->m_pdata_size;
2808275SEric Cheng 	}
2818275SEric Cheng 
2828275SEric Cheng 	/*
2838275SEric Cheng 	 * Register the private properties.
2848275SEric Cheng 	 */
2858275SEric Cheng 	mac_register_priv_prop(mip, mregp->m_priv_props,
2868275SEric Cheng 	    mregp->m_priv_prop_count);
2878275SEric Cheng 
2888275SEric Cheng 	/*
2898275SEric Cheng 	 * Stash the driver callbacks into the mac_impl_t, but first sanity
2908275SEric Cheng 	 * check to make sure all mandatory callbacks are set.
2918275SEric Cheng 	 */
2928275SEric Cheng 	if (mregp->m_callbacks->mc_getstat == NULL ||
2938275SEric Cheng 	    mregp->m_callbacks->mc_start == NULL ||
2948275SEric Cheng 	    mregp->m_callbacks->mc_stop == NULL ||
2958275SEric Cheng 	    mregp->m_callbacks->mc_setpromisc == NULL ||
2968275SEric Cheng 	    mregp->m_callbacks->mc_multicst == NULL) {
2978275SEric Cheng 		goto fail;
2988275SEric Cheng 	}
2998275SEric Cheng 	mip->mi_callbacks = mregp->m_callbacks;
3008275SEric Cheng 
3018275SEric Cheng 	if (i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_LEGACY, &legacy))
3028275SEric Cheng 		mip->mi_state_flags |= MIS_LEGACY;
3038275SEric Cheng 
3048275SEric Cheng 	if (mip->mi_state_flags & MIS_LEGACY) {
3058275SEric Cheng 		mip->mi_unsup_note = legacy.ml_unsup_note;
3068275SEric Cheng 		mip->mi_phy_dev = legacy.ml_dev;
3078275SEric Cheng 	} else {
3088275SEric Cheng 		mip->mi_unsup_note = 0;
3098275SEric Cheng 		mip->mi_phy_dev = makedevice(ddi_driver_major(mip->mi_dip),
3108275SEric Cheng 		    ddi_get_instance(mip->mi_dip) + 1);
3118275SEric Cheng 	}
3128275SEric Cheng 
3138275SEric Cheng 	/*
3148275SEric Cheng 	 * Allocate a notification thread. thread_create blocks for memory
3158275SEric Cheng 	 * if needed, it never fails.
3168275SEric Cheng 	 */
3178275SEric Cheng 	mip->mi_notify_thread = thread_create(NULL, 0, i_mac_notify_thread,
3188275SEric Cheng 	    mip, 0, &p0, TS_RUN, minclsyspri);
3198275SEric Cheng 
3208275SEric Cheng 	/*
3218275SEric Cheng 	 * Initialize the capabilities
3228275SEric Cheng 	 */
3238275SEric Cheng 
3248275SEric Cheng 	if (i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_VNIC, NULL))
3258275SEric Cheng 		mip->mi_state_flags |= MIS_IS_VNIC;
3268275SEric Cheng 
3278275SEric Cheng 	if (i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_AGGR, NULL))
3288275SEric Cheng 		mip->mi_state_flags |= MIS_IS_AGGR;
3298275SEric Cheng 
3308275SEric Cheng 	mac_addr_factory_init(mip);
3318275SEric Cheng 
3328275SEric Cheng 	/*
3338275SEric Cheng 	 * Enforce the virtrualization level registered.
3348275SEric Cheng 	 */
3358275SEric Cheng 	if (mip->mi_v12n_level & MAC_VIRT_LEVEL1) {
3368275SEric Cheng 		if (mac_init_rings(mip, MAC_RING_TYPE_RX) != 0 ||
3378275SEric Cheng 		    mac_init_rings(mip, MAC_RING_TYPE_TX) != 0)
3388275SEric Cheng 			goto fail;
3398275SEric Cheng 
3408275SEric Cheng 		/*
3418275SEric Cheng 		 * The driver needs to register at least rx rings for this
3428275SEric Cheng 		 * virtualization level.
3438275SEric Cheng 		 */
3448275SEric Cheng 		if (mip->mi_rx_groups == NULL)
3458275SEric Cheng 			goto fail;
3468275SEric Cheng 	}
3478275SEric Cheng 
3488275SEric Cheng 	/*
3498275SEric Cheng 	 * The driver must set mc_unicst entry point to NULL when it advertises
3508275SEric Cheng 	 * CAP_RINGS for rx groups.
3518275SEric Cheng 	 */
3528275SEric Cheng 	if (mip->mi_rx_groups != NULL) {
3538275SEric Cheng 		if (mregp->m_callbacks->mc_unicst != NULL)
3548275SEric Cheng 			goto fail;
3558275SEric Cheng 	} else {
3568275SEric Cheng 		if (mregp->m_callbacks->mc_unicst == NULL)
3578275SEric Cheng 			goto fail;
3588275SEric Cheng 	}
3598275SEric Cheng 
3608275SEric Cheng 	/*
3618275SEric Cheng 	 * The driver must set mc_tx entry point to NULL when it advertises
3628275SEric Cheng 	 * CAP_RINGS for tx rings.
3638275SEric Cheng 	 */
3648275SEric Cheng 	if (mip->mi_tx_groups != NULL) {
3658275SEric Cheng 		if (mregp->m_callbacks->mc_tx != NULL)
3668275SEric Cheng 			goto fail;
3678275SEric Cheng 	} else {
3688275SEric Cheng 		if (mregp->m_callbacks->mc_tx == NULL)
3698275SEric Cheng 			goto fail;
3708275SEric Cheng 	}
3718275SEric Cheng 
3728275SEric Cheng 	/*
3738275SEric Cheng 	 * Initialize MAC addresses. Must be called after mac_init_rings().
3748275SEric Cheng 	 */
3758275SEric Cheng 	mac_init_macaddr(mip);
3768275SEric Cheng 
3778275SEric Cheng 	mip->mi_share_capab.ms_snum = 0;
3788275SEric Cheng 	if (mip->mi_v12n_level & MAC_VIRT_HIO) {
3798275SEric Cheng 		(void) mac_capab_get((mac_handle_t)mip, MAC_CAPAB_SHARES,
3808275SEric Cheng 		    &mip->mi_share_capab);
3818275SEric Cheng 	}
3828275SEric Cheng 
3838275SEric Cheng 	/*
3848275SEric Cheng 	 * Initialize the kstats for this device.
3858275SEric Cheng 	 */
3868275SEric Cheng 	mac_stat_create(mip);
3878275SEric Cheng 
3888275SEric Cheng 	/* Zero out any properties. */
3898275SEric Cheng 	bzero(&mip->mi_resource_props, sizeof (mac_resource_props_t));
3908275SEric Cheng 
3918275SEric Cheng 	/* set the gldv3 flag in dn_flags */
3928275SEric Cheng 	dnp = &devnamesp[ddi_driver_major(mip->mi_dip)];
3938275SEric Cheng 	LOCK_DEV_OPS(&dnp->dn_lock);
3948275SEric Cheng 	dnp->dn_flags |= (DN_GLDV3_DRIVER | DN_NETWORK_DRIVER);
3958275SEric Cheng 	UNLOCK_DEV_OPS(&dnp->dn_lock);
3968275SEric Cheng 
3978275SEric Cheng 	if (mip->mi_minor < MAC_MAX_MINOR + 1) {
3988275SEric Cheng 		/* Create a style-2 DLPI device */
3998275SEric Cheng 		if (ddi_create_minor_node(mip->mi_dip, driver, S_IFCHR, 0,
4008275SEric Cheng 		    DDI_NT_NET, CLONE_DEV) != DDI_SUCCESS)
4018275SEric Cheng 			goto fail;
4028275SEric Cheng 		style2_created = B_TRUE;
4038275SEric Cheng 
4048275SEric Cheng 		/* Create a style-1 DLPI device */
4058275SEric Cheng 		if (ddi_create_minor_node(mip->mi_dip, mip->mi_name, S_IFCHR,
4068275SEric Cheng 		    mip->mi_minor, DDI_NT_NET, 0) != DDI_SUCCESS)
4078275SEric Cheng 			goto fail;
4088275SEric Cheng 		style1_created = B_TRUE;
4098275SEric Cheng 	}
4108275SEric Cheng 
4118275SEric Cheng 	mac_flow_l2tab_create(mip, &mip->mi_flow_tab);
4128275SEric Cheng 
4138275SEric Cheng 	rw_enter(&i_mac_impl_lock, RW_WRITER);
4148275SEric Cheng 	if (mod_hash_insert(i_mac_impl_hash,
4158275SEric Cheng 	    (mod_hash_key_t)mip->mi_name, (mod_hash_val_t)mip) != 0) {
4168275SEric Cheng 		rw_exit(&i_mac_impl_lock);
4178275SEric Cheng 		err = EEXIST;
4188275SEric Cheng 		goto fail;
4198275SEric Cheng 	}
4208275SEric Cheng 
4218275SEric Cheng 	DTRACE_PROBE2(mac__register, struct devnames *, dnp,
4228275SEric Cheng 	    (mac_impl_t *), mip);
4238275SEric Cheng 
4248275SEric Cheng 	/*
4258275SEric Cheng 	 * Mark the MAC to be ready for open.
4268275SEric Cheng 	 */
4278275SEric Cheng 	mip->mi_state_flags &= ~MIS_DISABLED;
4288275SEric Cheng 	rw_exit(&i_mac_impl_lock);
4298275SEric Cheng 
4308275SEric Cheng 	atomic_inc_32(&i_mac_impl_count);
4318275SEric Cheng 
4328275SEric Cheng 	cmn_err(CE_NOTE, "!%s registered", mip->mi_name);
4338275SEric Cheng 	*mhp = (mac_handle_t)mip;
4348275SEric Cheng 	return (0);
4358275SEric Cheng 
4368275SEric Cheng fail:
4378275SEric Cheng 	if (style1_created)
4388275SEric Cheng 		ddi_remove_minor_node(mip->mi_dip, mip->mi_name);
4398275SEric Cheng 
4408275SEric Cheng 	if (style2_created)
4418275SEric Cheng 		ddi_remove_minor_node(mip->mi_dip, driver);
4428275SEric Cheng 
4438275SEric Cheng 	mac_addr_factory_fini(mip);
4448275SEric Cheng 
4458275SEric Cheng 	/* Clean up registered MAC addresses */
4468275SEric Cheng 	mac_fini_macaddr(mip);
4478275SEric Cheng 
4488275SEric Cheng 	/* Clean up registered rings */
4498275SEric Cheng 	mac_free_rings(mip, MAC_RING_TYPE_RX);
4508275SEric Cheng 	mac_free_rings(mip, MAC_RING_TYPE_TX);
4518275SEric Cheng 
4528275SEric Cheng 	/* Clean up notification thread */
4538275SEric Cheng 	if (mip->mi_notify_thread != NULL)
4548275SEric Cheng 		i_mac_notify_exit(mip);
4558275SEric Cheng 
4568275SEric Cheng 	if (mip->mi_info.mi_unicst_addr != NULL) {
4578275SEric Cheng 		kmem_free(mip->mi_info.mi_unicst_addr,
4588275SEric Cheng 		    mip->mi_type->mt_addr_length);
4598275SEric Cheng 		mip->mi_info.mi_unicst_addr = NULL;
4608275SEric Cheng 	}
4618275SEric Cheng 
4628275SEric Cheng 	mac_stat_destroy(mip);
4638275SEric Cheng 
4648275SEric Cheng 	if (mip->mi_type != NULL) {
4658275SEric Cheng 		atomic_dec_32(&mip->mi_type->mt_ref);
4668275SEric Cheng 		mip->mi_type = NULL;
4678275SEric Cheng 	}
4688275SEric Cheng 
4698275SEric Cheng 	if (mip->mi_pdata != NULL) {
4708275SEric Cheng 		kmem_free(mip->mi_pdata, mip->mi_pdata_size);
4718275SEric Cheng 		mip->mi_pdata = NULL;
4728275SEric Cheng 		mip->mi_pdata_size = 0;
4738275SEric Cheng 	}
4748275SEric Cheng 
4758275SEric Cheng 	if (minor != 0) {
4768275SEric Cheng 		ASSERT(minor > MAC_MAX_MINOR);
4778275SEric Cheng 		mac_minor_rele(minor);
4788275SEric Cheng 	}
4798275SEric Cheng 
4808275SEric Cheng 	mac_unregister_priv_prop(mip);
4818275SEric Cheng 
4828275SEric Cheng 	kmem_cache_free(i_mac_impl_cachep, mip);
4838275SEric Cheng 	return (err);
4848275SEric Cheng }
4858275SEric Cheng 
4868275SEric Cheng /*
4878275SEric Cheng  * Unregister from the GLDv3 framework
4888275SEric Cheng  */
4898275SEric Cheng int
4908275SEric Cheng mac_unregister(mac_handle_t mh)
4918275SEric Cheng {
4928275SEric Cheng 	int			err;
4938275SEric Cheng 	mac_impl_t		*mip = (mac_impl_t *)mh;
4948275SEric Cheng 	mod_hash_val_t		val;
4958275SEric Cheng 	mac_margin_req_t	*mmr, *nextmmr;
4968275SEric Cheng 
4978275SEric Cheng 	/* Fail the unregister if there are any open references to this mac. */
4988275SEric Cheng 	if ((err = mac_disable_nowait(mh)) != 0)
4998275SEric Cheng 		return (err);
5008275SEric Cheng 
5018275SEric Cheng 	/*
5028275SEric Cheng 	 * Clean up notification thread and wait for it to exit.
5038275SEric Cheng 	 */
5048275SEric Cheng 	i_mac_notify_exit(mip);
5058275SEric Cheng 
5068275SEric Cheng 	i_mac_perim_enter(mip);
5078275SEric Cheng 
5088275SEric Cheng 	if (mip->mi_minor < MAC_MAX_MINOR + 1) {
5098275SEric Cheng 		ddi_remove_minor_node(mip->mi_dip, mip->mi_name);
5108275SEric Cheng 		ddi_remove_minor_node(mip->mi_dip,
5118275SEric Cheng 		    (char *)ddi_driver_name(mip->mi_dip));
5128275SEric Cheng 	}
5138275SEric Cheng 
5148275SEric Cheng 	ASSERT(mip->mi_nactiveclients == 0 && !(mip->mi_state_flags &
5158275SEric Cheng 	    MIS_EXCLUSIVE));
5168275SEric Cheng 
5178275SEric Cheng 	mac_stat_destroy(mip);
5188275SEric Cheng 
5198275SEric Cheng 	(void) mod_hash_remove(i_mac_impl_hash,
5208275SEric Cheng 	    (mod_hash_key_t)mip->mi_name, &val);
5218275SEric Cheng 	ASSERT(mip == (mac_impl_t *)val);
5228275SEric Cheng 
5238275SEric Cheng 	ASSERT(i_mac_impl_count > 0);
5248275SEric Cheng 	atomic_dec_32(&i_mac_impl_count);
5258275SEric Cheng 
5268275SEric Cheng 	if (mip->mi_pdata != NULL)
5278275SEric Cheng 		kmem_free(mip->mi_pdata, mip->mi_pdata_size);
5288275SEric Cheng 	mip->mi_pdata = NULL;
5298275SEric Cheng 	mip->mi_pdata_size = 0;
5308275SEric Cheng 
5318275SEric Cheng 	/*
5328275SEric Cheng 	 * Free the list of margin request.
5338275SEric Cheng 	 */
5348275SEric Cheng 	for (mmr = mip->mi_mmrp; mmr != NULL; mmr = nextmmr) {
5358275SEric Cheng 		nextmmr = mmr->mmr_nextp;
5368275SEric Cheng 		kmem_free(mmr, sizeof (mac_margin_req_t));
5378275SEric Cheng 	}
5388275SEric Cheng 	mip->mi_mmrp = NULL;
5398275SEric Cheng 
5408275SEric Cheng 	mip->mi_linkstate = LINK_STATE_UNKNOWN;
5418275SEric Cheng 	kmem_free(mip->mi_info.mi_unicst_addr, mip->mi_type->mt_addr_length);
5428275SEric Cheng 	mip->mi_info.mi_unicst_addr = NULL;
5438275SEric Cheng 
5448275SEric Cheng 	atomic_dec_32(&mip->mi_type->mt_ref);
5458275SEric Cheng 	mip->mi_type = NULL;
5468275SEric Cheng 
5478275SEric Cheng 	/*
5488275SEric Cheng 	 * Free the primary MAC address.
5498275SEric Cheng 	 */
5508275SEric Cheng 	mac_fini_macaddr(mip);
5518275SEric Cheng 
5528275SEric Cheng 	/*
5538275SEric Cheng 	 * free all rings
5548275SEric Cheng 	 */
5558275SEric Cheng 	mac_free_rings(mip, MAC_RING_TYPE_RX);
5568275SEric Cheng 	mac_free_rings(mip, MAC_RING_TYPE_TX);
5578275SEric Cheng 
5588275SEric Cheng 	mac_addr_factory_fini(mip);
5598275SEric Cheng 
5608275SEric Cheng 	bzero(mip->mi_addr, MAXMACADDRLEN);
5618275SEric Cheng 	bzero(mip->mi_dstaddr, MAXMACADDRLEN);
5628275SEric Cheng 
5638275SEric Cheng 	/* and the flows */
5648275SEric Cheng 	mac_flow_tab_destroy(mip->mi_flow_tab);
5658275SEric Cheng 	mip->mi_flow_tab = NULL;
5668275SEric Cheng 
5678275SEric Cheng 	if (mip->mi_minor > MAC_MAX_MINOR)
5688275SEric Cheng 		mac_minor_rele(mip->mi_minor);
5698275SEric Cheng 
5708275SEric Cheng 	cmn_err(CE_NOTE, "!%s unregistered", mip->mi_name);
5718275SEric Cheng 
5728275SEric Cheng 	/*
5738275SEric Cheng 	 * Reset the perim related fields to default values before
5748275SEric Cheng 	 * kmem_cache_free
5758275SEric Cheng 	 */
5768275SEric Cheng 	i_mac_perim_exit(mip);
5778275SEric Cheng 	mip->mi_state_flags = 0;
5788275SEric Cheng 
5798275SEric Cheng 	mac_unregister_priv_prop(mip);
5808275SEric Cheng 	kmem_cache_free(i_mac_impl_cachep, mip);
5818275SEric Cheng 
5828275SEric Cheng 	return (0);
5838275SEric Cheng }
5848275SEric Cheng 
5858275SEric Cheng /* DATA RECEPTION */
5868275SEric Cheng 
5878275SEric Cheng /*
5888275SEric Cheng  * This function is invoked for packets received by the MAC driver in
5898275SEric Cheng  * interrupt context. The ring generation number provided by the driver
5908275SEric Cheng  * is matched with the ring generation number held in MAC. If they do not
5918275SEric Cheng  * match, received packets are considered stale packets coming from an older
5928275SEric Cheng  * assignment of the ring. Drop them.
5938275SEric Cheng  */
5948275SEric Cheng void
5958275SEric Cheng mac_rx_ring(mac_handle_t mh, mac_ring_handle_t mrh, mblk_t *mp_chain,
5968275SEric Cheng     uint64_t mr_gen_num)
5978275SEric Cheng {
5988275SEric Cheng 	mac_ring_t		*mr = (mac_ring_t *)mrh;
5998275SEric Cheng 
6008275SEric Cheng 	if ((mr != NULL) && (mr->mr_gen_num != mr_gen_num)) {
6018275SEric Cheng 		DTRACE_PROBE2(mac__rx__rings__stale__packet, uint64_t,
6028275SEric Cheng 		    mr->mr_gen_num, uint64_t, mr_gen_num);
6038275SEric Cheng 		freemsgchain(mp_chain);
6048275SEric Cheng 		return;
6058275SEric Cheng 	}
6068275SEric Cheng 	mac_rx(mh, (mac_resource_handle_t)mrh, mp_chain);
6078275SEric Cheng }
6088275SEric Cheng 
6098275SEric Cheng /*
6108275SEric Cheng  * This function is invoked for each packet received by the underlying
6118275SEric Cheng  * driver.
6128275SEric Cheng  */
6138275SEric Cheng void
6148275SEric Cheng mac_rx(mac_handle_t mh, mac_resource_handle_t mrh, mblk_t *mp_chain)
6158275SEric Cheng {
6168275SEric Cheng 	mac_impl_t		*mip = (mac_impl_t *)mh;
6178275SEric Cheng 	mac_ring_t		*mr = (mac_ring_t *)mrh;
6188275SEric Cheng 	mac_soft_ring_set_t 	*mac_srs;
6198275SEric Cheng 	mblk_t			*bp = mp_chain;
6208275SEric Cheng 	boolean_t		hw_classified = B_FALSE;
6218275SEric Cheng 
6228275SEric Cheng 	/*
6238275SEric Cheng 	 * If there are any promiscuous mode callbacks defined for
6248275SEric Cheng 	 * this MAC, pass them a copy if appropriate.
6258275SEric Cheng 	 */
6268275SEric Cheng 	if (mip->mi_promisc_list != NULL)
6278275SEric Cheng 		mac_promisc_dispatch(mip, mp_chain, NULL);
6288275SEric Cheng 
6298275SEric Cheng 	if (mr != NULL) {
6308275SEric Cheng 		/*
6318275SEric Cheng 		 * If the SRS teardown has started, just return. The 'mr'
6328275SEric Cheng 		 * continues to be valid until the driver unregisters the mac.
6338275SEric Cheng 		 * Hardware classified packets will not make their way up
6348275SEric Cheng 		 * beyond this point once the teardown has started. The driver
6358275SEric Cheng 		 * is never passed a pointer to a flow entry or SRS or any
6368275SEric Cheng 		 * structure that can be freed much before mac_unregister.
6378275SEric Cheng 		 */
6388275SEric Cheng 		mutex_enter(&mr->mr_lock);
6398275SEric Cheng 		if ((mr->mr_state != MR_INUSE) || (mr->mr_flag &
6408275SEric Cheng 		    (MR_INCIPIENT | MR_CONDEMNED | MR_QUIESCE))) {
6418275SEric Cheng 			mutex_exit(&mr->mr_lock);
6428275SEric Cheng 			freemsgchain(mp_chain);
6438275SEric Cheng 			return;
6448275SEric Cheng 		}
6458275SEric Cheng 		if (mr->mr_classify_type == MAC_HW_CLASSIFIER) {
6468275SEric Cheng 			hw_classified = B_TRUE;
6478275SEric Cheng 			MR_REFHOLD_LOCKED(mr);
6488275SEric Cheng 		}
6498275SEric Cheng 		mutex_exit(&mr->mr_lock);
6508275SEric Cheng 
6518275SEric Cheng 		/*
6528275SEric Cheng 		 * We check if an SRS is controlling this ring.
6538275SEric Cheng 		 * If so, we can directly call the srs_lower_proc
6548275SEric Cheng 		 * routine otherwise we need to go through mac_rx_classify
6558275SEric Cheng 		 * to reach the right place.
6568275SEric Cheng 		 */
6578275SEric Cheng 		if (hw_classified) {
6588275SEric Cheng 			mac_srs = mr->mr_srs;
6598275SEric Cheng 			/*
6608275SEric Cheng 			 * This is supposed to be the fast path.
6618275SEric Cheng 			 * All packets received though here were steered by
6628275SEric Cheng 			 * the hardware classifier, and share the same
6638275SEric Cheng 			 * MAC header info.
6648275SEric Cheng 			 */
6658275SEric Cheng 			mac_srs->srs_rx.sr_lower_proc(mh,
6668275SEric Cheng 			    (mac_resource_handle_t)mac_srs, mp_chain, B_FALSE);
6678275SEric Cheng 			MR_REFRELE(mr);
6688275SEric Cheng 			return;
6698275SEric Cheng 		}
6708275SEric Cheng 		/* We'll fall through to software classification */
671*8833SVenu.Iyer@Sun.COM 	} else {
672*8833SVenu.Iyer@Sun.COM 		flow_entry_t *flent;
673*8833SVenu.Iyer@Sun.COM 		int err;
674*8833SVenu.Iyer@Sun.COM 
675*8833SVenu.Iyer@Sun.COM 		rw_enter(&mip->mi_rw_lock, RW_READER);
676*8833SVenu.Iyer@Sun.COM 		if (mip->mi_single_active_client != NULL) {
677*8833SVenu.Iyer@Sun.COM 			flent = mip->mi_single_active_client->mci_flent_list;
678*8833SVenu.Iyer@Sun.COM 			FLOW_TRY_REFHOLD(flent, err);
679*8833SVenu.Iyer@Sun.COM 			rw_exit(&mip->mi_rw_lock);
680*8833SVenu.Iyer@Sun.COM 			if (err == 0) {
681*8833SVenu.Iyer@Sun.COM 				(flent->fe_cb_fn)(flent->fe_cb_arg1,
682*8833SVenu.Iyer@Sun.COM 				    flent->fe_cb_arg2, mp_chain, B_FALSE);
683*8833SVenu.Iyer@Sun.COM 				FLOW_REFRELE(flent);
684*8833SVenu.Iyer@Sun.COM 				return;
685*8833SVenu.Iyer@Sun.COM 			}
686*8833SVenu.Iyer@Sun.COM 		} else {
687*8833SVenu.Iyer@Sun.COM 			rw_exit(&mip->mi_rw_lock);
688*8833SVenu.Iyer@Sun.COM 		}
6898275SEric Cheng 	}
6908275SEric Cheng 
6918275SEric Cheng 	if (!FLOW_TAB_EMPTY(mip->mi_flow_tab)) {
6928275SEric Cheng 		if ((bp = mac_rx_flow(mh, mrh, bp)) == NULL)
6938275SEric Cheng 			return;
6948275SEric Cheng 	}
6958275SEric Cheng 
6968275SEric Cheng 	freemsgchain(bp);
6978275SEric Cheng }
6988275SEric Cheng 
6998275SEric Cheng /* DATA TRANSMISSION */
7008275SEric Cheng 
7018275SEric Cheng /*
7028275SEric Cheng  * A driver's notification to resume transmission, in case of a provider
7038275SEric Cheng  * without TX rings.
7048275SEric Cheng  */
7058275SEric Cheng void
7068275SEric Cheng mac_tx_update(mac_handle_t mh)
7078275SEric Cheng {
7088275SEric Cheng 	/*
7098275SEric Cheng 	 * Walk the list of MAC clients (mac_client_handle)
7108275SEric Cheng 	 * and update
7118275SEric Cheng 	 */
7128275SEric Cheng 	i_mac_tx_srs_notify((mac_impl_t *)mh, NULL);
7138275SEric Cheng }
7148275SEric Cheng 
7158275SEric Cheng /*
7168275SEric Cheng  * A driver's notification to resume transmission on the specified TX ring.
7178275SEric Cheng  */
7188275SEric Cheng void
7198275SEric Cheng mac_tx_ring_update(mac_handle_t mh, mac_ring_handle_t rh)
7208275SEric Cheng {
7218275SEric Cheng 	i_mac_tx_srs_notify((mac_impl_t *)mh, rh);
7228275SEric Cheng }
7238275SEric Cheng 
7248275SEric Cheng /* LINK STATE */
7258275SEric Cheng /*
7268275SEric Cheng  * Notify the MAC layer about a link state change
7278275SEric Cheng  */
7288275SEric Cheng void
7298275SEric Cheng mac_link_update(mac_handle_t mh, link_state_t link)
7308275SEric Cheng {
7318275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
7328275SEric Cheng 
7338275SEric Cheng 	/*
7348275SEric Cheng 	 * Save the link state.
7358275SEric Cheng 	 */
7368275SEric Cheng 	mip->mi_linkstate = link;
7378275SEric Cheng 
7388275SEric Cheng 	/*
7398275SEric Cheng 	 * Send a MAC_NOTE_LINK notification.
7408275SEric Cheng 	 */
7418275SEric Cheng 	i_mac_notify(mip, MAC_NOTE_LINK);
7428275SEric Cheng }
7438275SEric Cheng 
7448275SEric Cheng /* OTHER CONTROL INFORMATION */
7458275SEric Cheng 
7468275SEric Cheng /*
7478275SEric Cheng  * A driver notified us that its primary MAC address has changed.
7488275SEric Cheng  */
7498275SEric Cheng void
7508275SEric Cheng mac_unicst_update(mac_handle_t mh, const uint8_t *addr)
7518275SEric Cheng {
7528275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
7538275SEric Cheng 
7548275SEric Cheng 	if (mip->mi_type->mt_addr_length == 0)
7558275SEric Cheng 		return;
7568275SEric Cheng 
7578275SEric Cheng 	i_mac_perim_enter(mip);
7588275SEric Cheng 	/*
7598275SEric Cheng 	 * If address doesn't change, do nothing.
7608275SEric Cheng 	 */
7618275SEric Cheng 	if (bcmp(addr, mip->mi_addr, mip->mi_type->mt_addr_length) == 0) {
7628275SEric Cheng 		i_mac_perim_exit(mip);
7638275SEric Cheng 		return;
7648275SEric Cheng 	}
7658275SEric Cheng 
7668275SEric Cheng 	/*
7678275SEric Cheng 	 * Freshen the MAC address value and update all MAC clients that
7688275SEric Cheng 	 * share this MAC address.
7698275SEric Cheng 	 */
7708275SEric Cheng 	mac_freshen_macaddr(mac_find_macaddr(mip, mip->mi_addr),
7718275SEric Cheng 	    (uint8_t *)addr);
7728275SEric Cheng 
7738275SEric Cheng 	i_mac_perim_exit(mip);
7748275SEric Cheng 
7758275SEric Cheng 	/*
7768275SEric Cheng 	 * Send a MAC_NOTE_UNICST notification.
7778275SEric Cheng 	 */
7788275SEric Cheng 	i_mac_notify(mip, MAC_NOTE_UNICST);
7798275SEric Cheng }
7808275SEric Cheng 
7818275SEric Cheng /*
7828275SEric Cheng  * The provider's hw resources (e.g. rings grouping) has changed.
7838275SEric Cheng  * Notify the MAC framework to trigger a re-negotiation of the capabilities.
7848275SEric Cheng  */
7858275SEric Cheng void
7868275SEric Cheng mac_resource_update(mac_handle_t mh)
7878275SEric Cheng {
7888275SEric Cheng 	/*
7898275SEric Cheng 	 * Send a MAC_NOTE_RESOURCE notification.
7908275SEric Cheng 	 */
7918275SEric Cheng 	i_mac_notify((mac_impl_t *)mh, MAC_NOTE_RESOURCE);
7928275SEric Cheng }
7938275SEric Cheng 
7948275SEric Cheng /*
7958275SEric Cheng  * MAC plugin information changed.
7968275SEric Cheng  */
7978275SEric Cheng int
7988275SEric Cheng mac_pdata_update(mac_handle_t mh, void *mac_pdata, size_t dsize)
7998275SEric Cheng {
8008275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
8018275SEric Cheng 
8028275SEric Cheng 	/*
8038275SEric Cheng 	 * Verify that the plugin supports MAC plugin data and that the
8048275SEric Cheng 	 * supplied data is valid.
8058275SEric Cheng 	 */
8068275SEric Cheng 	if (!(mip->mi_type->mt_ops.mtops_ops & MTOPS_PDATA_VERIFY))
8078275SEric Cheng 		return (EINVAL);
8088275SEric Cheng 	if (!mip->mi_type->mt_ops.mtops_pdata_verify(mac_pdata, dsize))
8098275SEric Cheng 		return (EINVAL);
8108275SEric Cheng 
8118275SEric Cheng 	if (mip->mi_pdata != NULL)
8128275SEric Cheng 		kmem_free(mip->mi_pdata, mip->mi_pdata_size);
8138275SEric Cheng 
8148275SEric Cheng 	mip->mi_pdata = kmem_alloc(dsize, KM_SLEEP);
8158275SEric Cheng 	bcopy(mac_pdata, mip->mi_pdata, dsize);
8168275SEric Cheng 	mip->mi_pdata_size = dsize;
8178275SEric Cheng 
8188275SEric Cheng 	/*
8198275SEric Cheng 	 * Since the MAC plugin data is used to construct MAC headers that
8208275SEric Cheng 	 * were cached in fast-path headers, we need to flush fast-path
8218275SEric Cheng 	 * information for links associated with this mac.
8228275SEric Cheng 	 */
8238275SEric Cheng 	i_mac_notify(mip, MAC_NOTE_FASTPATH_FLUSH);
8248275SEric Cheng 	return (0);
8258275SEric Cheng }
8268275SEric Cheng 
8278275SEric Cheng /*
8288275SEric Cheng  * Invoked by driver as well as the framework to notify its capability change.
8298275SEric Cheng  */
8308275SEric Cheng void
8318275SEric Cheng mac_capab_update(mac_handle_t mh)
8328275SEric Cheng {
8338275SEric Cheng 	/* Send MAC_NOTE_CAPAB_CHG notification */
8348275SEric Cheng 	i_mac_notify((mac_impl_t *)mh, MAC_NOTE_CAPAB_CHG);
8358275SEric Cheng }
8368275SEric Cheng 
8378275SEric Cheng int
8388275SEric Cheng mac_maxsdu_update(mac_handle_t mh, uint_t sdu_max)
8398275SEric Cheng {
8408275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
8418275SEric Cheng 
8428275SEric Cheng 	if (sdu_max <= mip->mi_sdu_min)
8438275SEric Cheng 		return (EINVAL);
8448275SEric Cheng 	mip->mi_sdu_max = sdu_max;
8458275SEric Cheng 
8468275SEric Cheng 	/* Send a MAC_NOTE_SDU_SIZE notification. */
8478275SEric Cheng 	i_mac_notify(mip, MAC_NOTE_SDU_SIZE);
8488275SEric Cheng 	return (0);
8498275SEric Cheng }
8508275SEric Cheng 
8518275SEric Cheng /* PRIVATE FUNCTIONS, FOR INTERNAL USE ONLY */
8528275SEric Cheng 
8538275SEric Cheng /*
8548275SEric Cheng  * Updates the mac_impl structure with the current state of the link
8558275SEric Cheng  */
8568275SEric Cheng static void
8578275SEric Cheng i_mac_log_link_state(mac_impl_t *mip)
8588275SEric Cheng {
8598275SEric Cheng 	/*
8608275SEric Cheng 	 * If no change, then it is not interesting.
8618275SEric Cheng 	 */
8628275SEric Cheng 	if (mip->mi_lastlinkstate == mip->mi_linkstate)
8638275SEric Cheng 		return;
8648275SEric Cheng 
8658275SEric Cheng 	switch (mip->mi_linkstate) {
8668275SEric Cheng 	case LINK_STATE_UP:
8678275SEric Cheng 		if (mip->mi_type->mt_ops.mtops_ops & MTOPS_LINK_DETAILS) {
8688275SEric Cheng 			char det[200];
8698275SEric Cheng 
8708275SEric Cheng 			mip->mi_type->mt_ops.mtops_link_details(det,
8718275SEric Cheng 			    sizeof (det), (mac_handle_t)mip, mip->mi_pdata);
8728275SEric Cheng 
8738275SEric Cheng 			cmn_err(CE_NOTE, "!%s link up, %s", mip->mi_name, det);
8748275SEric Cheng 		} else {
8758275SEric Cheng 			cmn_err(CE_NOTE, "!%s link up", mip->mi_name);
8768275SEric Cheng 		}
8778275SEric Cheng 		break;
8788275SEric Cheng 
8798275SEric Cheng 	case LINK_STATE_DOWN:
8808275SEric Cheng 		/*
8818275SEric Cheng 		 * Only transitions from UP to DOWN are interesting
8828275SEric Cheng 		 */
8838275SEric Cheng 		if (mip->mi_lastlinkstate != LINK_STATE_UNKNOWN)
8848275SEric Cheng 			cmn_err(CE_NOTE, "!%s link down", mip->mi_name);
8858275SEric Cheng 		break;
8868275SEric Cheng 
8878275SEric Cheng 	case LINK_STATE_UNKNOWN:
8888275SEric Cheng 		/*
8898275SEric Cheng 		 * This case is normally not interesting.
8908275SEric Cheng 		 */
8918275SEric Cheng 		break;
8928275SEric Cheng 	}
8938275SEric Cheng 	mip->mi_lastlinkstate = mip->mi_linkstate;
8948275SEric Cheng }
8958275SEric Cheng 
8968275SEric Cheng /*
8978275SEric Cheng  * Main routine for the callbacks notifications thread
8988275SEric Cheng  */
8998275SEric Cheng static void
9008275SEric Cheng i_mac_notify_thread(void *arg)
9018275SEric Cheng {
9028275SEric Cheng 	mac_impl_t	*mip = arg;
9038275SEric Cheng 	callb_cpr_t	cprinfo;
9048275SEric Cheng 	mac_cb_t	*mcb;
9058275SEric Cheng 	mac_cb_info_t	*mcbi;
9068275SEric Cheng 	mac_notify_cb_t	*mncb;
9078275SEric Cheng 
9088275SEric Cheng 	mcbi = &mip->mi_notify_cb_info;
9098275SEric Cheng 	CALLB_CPR_INIT(&cprinfo, mcbi->mcbi_lockp, callb_generic_cpr,
9108275SEric Cheng 	    "i_mac_notify_thread");
9118275SEric Cheng 
9128275SEric Cheng 	mutex_enter(mcbi->mcbi_lockp);
9138275SEric Cheng 
9148275SEric Cheng 	for (;;) {
9158275SEric Cheng 		uint32_t	bits;
9168275SEric Cheng 		uint32_t	type;
9178275SEric Cheng 
9188275SEric Cheng 		bits = mip->mi_notify_bits;
9198275SEric Cheng 		if (bits == 0) {
9208275SEric Cheng 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
9218275SEric Cheng 			cv_wait(&mcbi->mcbi_cv, mcbi->mcbi_lockp);
9228275SEric Cheng 			CALLB_CPR_SAFE_END(&cprinfo, mcbi->mcbi_lockp);
9238275SEric Cheng 			continue;
9248275SEric Cheng 		}
9258275SEric Cheng 		mip->mi_notify_bits = 0;
9268275SEric Cheng 		if ((bits & (1 << MAC_NNOTE)) != 0) {
9278275SEric Cheng 			/* request to quit */
9288275SEric Cheng 			ASSERT(mip->mi_state_flags & MIS_DISABLED);
9298275SEric Cheng 			break;
9308275SEric Cheng 		}
9318275SEric Cheng 
9328275SEric Cheng 		mutex_exit(mcbi->mcbi_lockp);
9338275SEric Cheng 
9348275SEric Cheng 		/*
9358275SEric Cheng 		 * Log link changes.
9368275SEric Cheng 		 */
9378275SEric Cheng 		if ((bits & (1 << MAC_NOTE_LINK)) != 0)
9388275SEric Cheng 			i_mac_log_link_state(mip);
9398275SEric Cheng 
9408275SEric Cheng 		/*
9418275SEric Cheng 		 * Do notification callbacks for each notification type.
9428275SEric Cheng 		 */
9438275SEric Cheng 		for (type = 0; type < MAC_NNOTE; type++) {
9448275SEric Cheng 			if ((bits & (1 << type)) == 0) {
9458275SEric Cheng 				continue;
9468275SEric Cheng 			}
9478275SEric Cheng 
9488275SEric Cheng 			if (mac_notify_cb_list[type].mac_notify_cb_fn)
9498275SEric Cheng 				mac_notify_cb_list[type].mac_notify_cb_fn(mip);
9508275SEric Cheng 
9518275SEric Cheng 			/*
9528275SEric Cheng 			 * Walk the list of notifications.
9538275SEric Cheng 			 */
9548275SEric Cheng 			MAC_CALLBACK_WALKER_INC(&mip->mi_notify_cb_info);
9558275SEric Cheng 			for (mcb = mip->mi_notify_cb_list; mcb != NULL;
9568275SEric Cheng 			    mcb = mcb->mcb_nextp) {
9578275SEric Cheng 				mncb = (mac_notify_cb_t *)mcb->mcb_objp;
9588275SEric Cheng 				mncb->mncb_fn(mncb->mncb_arg, type);
9598275SEric Cheng 			}
9608275SEric Cheng 			MAC_CALLBACK_WALKER_DCR(&mip->mi_notify_cb_info,
9618275SEric Cheng 			    &mip->mi_notify_cb_list);
9628275SEric Cheng 		}
9638275SEric Cheng 
9648275SEric Cheng 		mutex_enter(mcbi->mcbi_lockp);
9658275SEric Cheng 	}
9668275SEric Cheng 
9678275SEric Cheng 	mip->mi_state_flags |= MIS_NOTIFY_DONE;
9688275SEric Cheng 	cv_broadcast(&mcbi->mcbi_cv);
9698275SEric Cheng 
9708275SEric Cheng 	/* CALLB_CPR_EXIT drops the lock */
9718275SEric Cheng 	CALLB_CPR_EXIT(&cprinfo);
9728275SEric Cheng 	thread_exit();
9738275SEric Cheng }
9748275SEric Cheng 
9758275SEric Cheng /*
9768275SEric Cheng  * Signal the i_mac_notify_thread asking it to quit.
9778275SEric Cheng  * Then wait till it is done.
9788275SEric Cheng  */
9798275SEric Cheng void
9808275SEric Cheng i_mac_notify_exit(mac_impl_t *mip)
9818275SEric Cheng {
9828275SEric Cheng 	mac_cb_info_t	*mcbi;
9838275SEric Cheng 
9848275SEric Cheng 	mcbi = &mip->mi_notify_cb_info;
9858275SEric Cheng 
9868275SEric Cheng 	mutex_enter(mcbi->mcbi_lockp);
9878275SEric Cheng 	mip->mi_notify_bits = (1 << MAC_NNOTE);
9888275SEric Cheng 	cv_broadcast(&mcbi->mcbi_cv);
9898275SEric Cheng 
9908275SEric Cheng 
9918275SEric Cheng 	while ((mip->mi_notify_thread != NULL) &&
9928275SEric Cheng 	    !(mip->mi_state_flags & MIS_NOTIFY_DONE)) {
9938275SEric Cheng 		cv_wait(&mcbi->mcbi_cv, mcbi->mcbi_lockp);
9948275SEric Cheng 	}
9958275SEric Cheng 
9968275SEric Cheng 	/* Necessary clean up before doing kmem_cache_free */
9978275SEric Cheng 	mip->mi_state_flags &= ~MIS_NOTIFY_DONE;
9988275SEric Cheng 	mip->mi_notify_bits = 0;
9998275SEric Cheng 	mip->mi_notify_thread = NULL;
10008275SEric Cheng 	mutex_exit(mcbi->mcbi_lockp);
10018275SEric Cheng }
10028275SEric Cheng 
10038275SEric Cheng /*
10048275SEric Cheng  * Entry point invoked by drivers to dynamically add a ring to an
10058275SEric Cheng  * existing group.
10068275SEric Cheng  */
10078275SEric Cheng int
10088275SEric Cheng mac_group_add_ring(mac_group_handle_t gh, int index)
10098275SEric Cheng {
10108275SEric Cheng 	mac_group_t *group = (mac_group_t *)gh;
10118275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)group->mrg_mh;
10128275SEric Cheng 	int ret;
10138275SEric Cheng 
10148275SEric Cheng 	i_mac_perim_enter(mip);
10158275SEric Cheng 
10168275SEric Cheng 	/*
10178275SEric Cheng 	 * Only RX rings can be added or removed by drivers currently.
10188275SEric Cheng 	 */
10198275SEric Cheng 	ASSERT(group->mrg_type == MAC_RING_TYPE_RX);
10208275SEric Cheng 
10218275SEric Cheng 	ret = i_mac_group_add_ring(group, NULL, index);
10228275SEric Cheng 
10238275SEric Cheng 	i_mac_perim_exit(mip);
10248275SEric Cheng 
10258275SEric Cheng 	return (ret);
10268275SEric Cheng }
10278275SEric Cheng 
10288275SEric Cheng /*
10298275SEric Cheng  * Entry point invoked by drivers to dynamically remove a ring
10308275SEric Cheng  * from an existing group. The specified ring handle must no longer
10318275SEric Cheng  * be used by the driver after a call to this function.
10328275SEric Cheng  */
10338275SEric Cheng void
10348275SEric Cheng mac_group_rem_ring(mac_group_handle_t gh, mac_ring_handle_t rh)
10358275SEric Cheng {
10368275SEric Cheng 	mac_group_t *group = (mac_group_t *)gh;
10378275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)group->mrg_mh;
10388275SEric Cheng 
10398275SEric Cheng 	i_mac_perim_enter(mip);
10408275SEric Cheng 
10418275SEric Cheng 	/*
10428275SEric Cheng 	 * Only RX rings can be added or removed by drivers currently.
10438275SEric Cheng 	 */
10448275SEric Cheng 	ASSERT(group->mrg_type == MAC_RING_TYPE_RX);
10458275SEric Cheng 
10468275SEric Cheng 	i_mac_group_rem_ring(group, (mac_ring_t *)rh, B_TRUE);
10478275SEric Cheng 
10488275SEric Cheng 	i_mac_perim_exit(mip);
10498275SEric Cheng }
1050