xref: /onnv-gate/usr/src/uts/common/io/mac/mac.c (revision 56:8b051e816bd8)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * MAC Services Module
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <sys/conf.h>
350Sstevel@tonic-gate #include <sys/stat.h>
360Sstevel@tonic-gate #include <sys/stream.h>
370Sstevel@tonic-gate #include <sys/strsun.h>
380Sstevel@tonic-gate #include <sys/strsubr.h>
390Sstevel@tonic-gate #include <sys/dlpi.h>
400Sstevel@tonic-gate #include <sys/mac.h>
410Sstevel@tonic-gate #include <sys/mac_impl.h>
420Sstevel@tonic-gate #include <sys/dld_impl.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #define	IMPL_HASHSZ	67	/* prime */
450Sstevel@tonic-gate 
460Sstevel@tonic-gate static kmem_cache_t	*i_mac_impl_cachep;
470Sstevel@tonic-gate static ght_t		i_mac_impl_hash;
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate  * Private functions.
510Sstevel@tonic-gate  */
520Sstevel@tonic-gate 
530Sstevel@tonic-gate /*ARGSUSED*/
540Sstevel@tonic-gate static boolean_t
550Sstevel@tonic-gate i_mac_ether_unicst_verify(mac_impl_t *mip, const uint8_t *addr)
560Sstevel@tonic-gate {
570Sstevel@tonic-gate 	/*
580Sstevel@tonic-gate 	 * Check the address is not a group address.
590Sstevel@tonic-gate 	 */
600Sstevel@tonic-gate 	if (addr[0] & 0x01)
610Sstevel@tonic-gate 		return (B_FALSE);
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 	return (B_TRUE);
640Sstevel@tonic-gate }
650Sstevel@tonic-gate 
660Sstevel@tonic-gate static boolean_t
670Sstevel@tonic-gate i_mac_ether_multicst_verify(mac_impl_t *mip, const uint8_t *addr)
680Sstevel@tonic-gate {
690Sstevel@tonic-gate 	mac_t		*mp = mip->mi_mp;
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 	/*
720Sstevel@tonic-gate 	 * Check the address is a group address.
730Sstevel@tonic-gate 	 */
740Sstevel@tonic-gate 	if (!(addr[0] & 0x01))
750Sstevel@tonic-gate 		return (B_FALSE);
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	/*
780Sstevel@tonic-gate 	 * Check the address is not the media broadcast address.
790Sstevel@tonic-gate 	 */
800Sstevel@tonic-gate 	if (bcmp(addr, mp->m_info.mi_brdcst_addr, mip->mi_addr_length) == 0)
810Sstevel@tonic-gate 		return (B_FALSE);
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	return (B_TRUE);
840Sstevel@tonic-gate }
850Sstevel@tonic-gate 
860Sstevel@tonic-gate /*ARGSUSED*/
870Sstevel@tonic-gate static int
880Sstevel@tonic-gate i_mac_constructor(void *buf, void *arg, int kmflag)
890Sstevel@tonic-gate {
900Sstevel@tonic-gate 	mac_impl_t	*mip = buf;
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	bzero(buf, sizeof (mac_impl_t));
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	mip->mi_link = LINK_STATE_UNKNOWN;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	rw_init(&mip->mi_state_lock, NULL, RW_DRIVER, NULL);
970Sstevel@tonic-gate 	rw_init(&mip->mi_data_lock, NULL, RW_DRIVER, NULL);
980Sstevel@tonic-gate 	rw_init(&mip->mi_notify_lock, NULL, RW_DRIVER, NULL);
990Sstevel@tonic-gate 	rw_init(&mip->mi_rx_lock, NULL, RW_DRIVER, NULL);
1000Sstevel@tonic-gate 	rw_init(&mip->mi_txloop_lock, NULL, RW_DRIVER, NULL);
1010Sstevel@tonic-gate 	rw_init(&mip->mi_resource_lock, NULL, RW_DRIVER, NULL);
1020Sstevel@tonic-gate 	mutex_init(&mip->mi_activelink_lock, NULL, MUTEX_DEFAULT, NULL);
1030Sstevel@tonic-gate 	return (0);
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate /*ARGSUSED*/
1070Sstevel@tonic-gate static void
1080Sstevel@tonic-gate i_mac_destructor(void *buf, void *arg)
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate 	mac_impl_t	*mip = buf;
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	ASSERT(mip->mi_mp == NULL);
1130Sstevel@tonic-gate 	ASSERT(mip->mi_hte == NULL);
1140Sstevel@tonic-gate 	ASSERT(mip->mi_ref == 0);
1150Sstevel@tonic-gate 	ASSERT(mip->mi_active == 0);
1160Sstevel@tonic-gate 	ASSERT(mip->mi_link == LINK_STATE_UNKNOWN);
1170Sstevel@tonic-gate 	ASSERT(mip->mi_devpromisc == 0);
1180Sstevel@tonic-gate 	ASSERT(mip->mi_promisc == 0);
1190Sstevel@tonic-gate 	ASSERT(mip->mi_mmap == NULL);
1200Sstevel@tonic-gate 	ASSERT(mip->mi_mnfp == NULL);
1210Sstevel@tonic-gate 	ASSERT(mip->mi_resource_add == NULL);
1220Sstevel@tonic-gate 	ASSERT(mip->mi_ksp == NULL);
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	rw_destroy(&mip->mi_state_lock);
1250Sstevel@tonic-gate 	rw_destroy(&mip->mi_data_lock);
1260Sstevel@tonic-gate 	rw_destroy(&mip->mi_notify_lock);
1270Sstevel@tonic-gate 	rw_destroy(&mip->mi_rx_lock);
1280Sstevel@tonic-gate 	rw_destroy(&mip->mi_txloop_lock);
1290Sstevel@tonic-gate 	rw_destroy(&mip->mi_resource_lock);
1300Sstevel@tonic-gate 	mutex_destroy(&mip->mi_activelink_lock);
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate int
1340Sstevel@tonic-gate i_mac_create(mac_t *mp)
1350Sstevel@tonic-gate {
1360Sstevel@tonic-gate 	dev_info_t	*dip;
1370Sstevel@tonic-gate 	mac_impl_t	*mip;
1380Sstevel@tonic-gate 	int		err;
1390Sstevel@tonic-gate 	ghte_t		hte;
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	dip = mp->m_dip;
1420Sstevel@tonic-gate 	ASSERT(dip != NULL);
1430Sstevel@tonic-gate 	ASSERT(ddi_get_instance(dip) >= 0);
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	/*
1460Sstevel@tonic-gate 	 * Allocate a new mac_impl_t.
1470Sstevel@tonic-gate 	 */
1480Sstevel@tonic-gate 	mip = kmem_cache_alloc(i_mac_impl_cachep, KM_SLEEP);
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	/*
1510Sstevel@tonic-gate 	 * Construct a name.
1520Sstevel@tonic-gate 	 */
1530Sstevel@tonic-gate 	(void) snprintf(mip->mi_dev, MAXNAMELEN - 1, "%s%d",
1540Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip));
1550Sstevel@tonic-gate 	mip->mi_port = mp->m_port;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	MAC_NAME(mip->mi_name, mip->mi_dev, mip->mi_port);
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	/*
1600Sstevel@tonic-gate 	 * Set the mac_t/mac_impl_t cross-references.
1610Sstevel@tonic-gate 	 */
1620Sstevel@tonic-gate 	mip->mi_mp = mp;
1630Sstevel@tonic-gate 	mp->m_impl = (void *)mip;
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	/*
1660Sstevel@tonic-gate 	 * Allocate a hash table entry.
1670Sstevel@tonic-gate 	 */
1680Sstevel@tonic-gate 	hte = ght_alloc(i_mac_impl_hash, KM_SLEEP);
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	GHT_KEY(hte) = GHT_PTR_TO_KEY(mip->mi_name);
1710Sstevel@tonic-gate 	GHT_VAL(hte) = GHT_PTR_TO_VAL(mip);
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	/*
1740Sstevel@tonic-gate 	 * Insert the hash table entry.
1750Sstevel@tonic-gate 	 */
1760Sstevel@tonic-gate 	ght_lock(i_mac_impl_hash, GHT_WRITE);
1770Sstevel@tonic-gate 	if ((err = ght_insert(hte)) != 0) {
1780Sstevel@tonic-gate 		ght_free(hte);
1790Sstevel@tonic-gate 		kmem_cache_free(i_mac_impl_cachep, mip);
1800Sstevel@tonic-gate 		goto done;
1810Sstevel@tonic-gate 	}
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	mip->mi_hte = hte;
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	/*
1860Sstevel@tonic-gate 	 * Copy the fixed 'factory' MAC address from the immutable info.
1870Sstevel@tonic-gate 	 * This is taken to be the MAC address currently in use.
1880Sstevel@tonic-gate 	 */
1890Sstevel@tonic-gate 	mip->mi_addr_length = mp->m_info.mi_addr_length;
1900Sstevel@tonic-gate 	bcopy(mp->m_info.mi_unicst_addr, mip->mi_addr, mip->mi_addr_length);
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	/*
1930Sstevel@tonic-gate 	 * Set up the address verification functions.
1940Sstevel@tonic-gate 	 */
1950Sstevel@tonic-gate 	ASSERT(mp->m_info.mi_media == DL_ETHER);
1960Sstevel@tonic-gate 	mip->mi_unicst_verify = i_mac_ether_unicst_verify;
1970Sstevel@tonic-gate 	mip->mi_multicst_verify = i_mac_ether_multicst_verify;
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	/*
200*56Smeem 	 * Set up the two possible transmit routines.
201*56Smeem 	 */
202*56Smeem 	mip->mi_txinfo.mt_fn = mp->m_tx;
203*56Smeem 	mip->mi_txinfo.mt_arg = mp->m_driver;
204*56Smeem 	mip->mi_txloopinfo.mt_fn = mac_txloop;
205*56Smeem 	mip->mi_txloopinfo.mt_arg = mip;
206*56Smeem 
207*56Smeem 	/*
2080Sstevel@tonic-gate 	 * Initialize the kstats for this device.
2090Sstevel@tonic-gate 	 */
2100Sstevel@tonic-gate 	mac_stat_create(mip);
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate done:
2130Sstevel@tonic-gate 	ght_unlock(i_mac_impl_hash);
2140Sstevel@tonic-gate 	return (err);
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate static void
2180Sstevel@tonic-gate i_mac_destroy(mac_t *mp)
2190Sstevel@tonic-gate {
2200Sstevel@tonic-gate 	mac_impl_t		*mip = mp->m_impl;
2210Sstevel@tonic-gate 	ghte_t			hte;
2220Sstevel@tonic-gate 	mac_multicst_addr_t	*p, *nextp;
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	ght_lock(i_mac_impl_hash, GHT_WRITE);
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	ASSERT(mip->mi_ref == 0);
2270Sstevel@tonic-gate 	ASSERT(!mip->mi_activelink);
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	/*
2300Sstevel@tonic-gate 	 * Destroy the kstats.
2310Sstevel@tonic-gate 	 */
2320Sstevel@tonic-gate 	mac_stat_destroy(mip);
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	/*
2350Sstevel@tonic-gate 	 * Remove and destroy the hash table entry.
2360Sstevel@tonic-gate 	 */
2370Sstevel@tonic-gate 	hte = mip->mi_hte;
2380Sstevel@tonic-gate 	ght_remove(hte);
2390Sstevel@tonic-gate 	ght_free(hte);
2400Sstevel@tonic-gate 	mip->mi_hte = NULL;
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	/*
2430Sstevel@tonic-gate 	 * Free the list of multicast addresses.
2440Sstevel@tonic-gate 	 */
2450Sstevel@tonic-gate 	for (p = mip->mi_mmap; p != NULL; p = nextp) {
2460Sstevel@tonic-gate 		nextp = p->mma_nextp;
2470Sstevel@tonic-gate 		kmem_free(p, sizeof (mac_multicst_addr_t));
2480Sstevel@tonic-gate 	}
2490Sstevel@tonic-gate 	mip->mi_mmap = NULL;
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	/*
2520Sstevel@tonic-gate 	 * Clean up the mac_impl_t ready to go back into the cache.
2530Sstevel@tonic-gate 	 */
2540Sstevel@tonic-gate 	mp->m_impl = NULL;
2550Sstevel@tonic-gate 	mip->mi_mp = NULL;
2560Sstevel@tonic-gate 	mip->mi_link = LINK_STATE_UNKNOWN;
2570Sstevel@tonic-gate 	mip->mi_destroying = B_FALSE;
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	/*
2600Sstevel@tonic-gate 	 * Free the structure back to the cache.
2610Sstevel@tonic-gate 	 */
2620Sstevel@tonic-gate 	kmem_cache_free(i_mac_impl_cachep, mip);
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	ght_unlock(i_mac_impl_hash);
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate static void
2680Sstevel@tonic-gate i_mac_notify(mac_impl_t *mip, mac_notify_type_t type)
2690Sstevel@tonic-gate {
2700Sstevel@tonic-gate 	mac_notify_fn_t		*mnfp;
2710Sstevel@tonic-gate 	mac_notify_t		notify;
2720Sstevel@tonic-gate 	void			*arg;
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	/*
2750Sstevel@tonic-gate 	 * Walk the list of notifications.
2760Sstevel@tonic-gate 	 */
2770Sstevel@tonic-gate 	rw_enter(&(mip->mi_notify_lock), RW_READER);
2780Sstevel@tonic-gate 	for (mnfp = mip->mi_mnfp; mnfp != NULL; mnfp = mnfp->mnf_nextp) {
2790Sstevel@tonic-gate 		notify = mnfp->mnf_fn;
2800Sstevel@tonic-gate 		arg = mnfp->mnf_arg;
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 		ASSERT(notify != NULL);
2830Sstevel@tonic-gate 		notify(arg, type);
2840Sstevel@tonic-gate 	}
2850Sstevel@tonic-gate 	rw_exit(&(mip->mi_notify_lock));
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate /*
2890Sstevel@tonic-gate  * Module initialization functions.
2900Sstevel@tonic-gate  */
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate void
2930Sstevel@tonic-gate mac_init(void)
2940Sstevel@tonic-gate {
2950Sstevel@tonic-gate 	int	err;
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	i_mac_impl_cachep = kmem_cache_create("mac_impl_cache",
2980Sstevel@tonic-gate 	    sizeof (mac_impl_t), 0, i_mac_constructor, i_mac_destructor, NULL,
2990Sstevel@tonic-gate 	    NULL, NULL, 0);
3000Sstevel@tonic-gate 	ASSERT(i_mac_impl_cachep != NULL);
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	err = ght_str_create("mac_impl_hash", IMPL_HASHSZ, &i_mac_impl_hash);
3030Sstevel@tonic-gate 	ASSERT(err == 0);
3040Sstevel@tonic-gate }
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate int
3070Sstevel@tonic-gate mac_fini(void)
3080Sstevel@tonic-gate {
3090Sstevel@tonic-gate 	int	err;
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	if ((err = ght_destroy(i_mac_impl_hash)) != 0)
3120Sstevel@tonic-gate 		return (err);
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	kmem_cache_destroy(i_mac_impl_cachep);
3150Sstevel@tonic-gate 	return (0);
3160Sstevel@tonic-gate }
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate /*
3190Sstevel@tonic-gate  * Client functions.
3200Sstevel@tonic-gate  */
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate int
3230Sstevel@tonic-gate mac_open(const char *dev, uint_t port, mac_handle_t *mhp)
3240Sstevel@tonic-gate {
3250Sstevel@tonic-gate 	char		name[MAXNAMELEN];
3260Sstevel@tonic-gate 	char		driver[MAXNAMELEN];
3270Sstevel@tonic-gate 	uint_t		instance;
3280Sstevel@tonic-gate 	major_t		major;
3290Sstevel@tonic-gate 	dev_info_t	*dip;
3300Sstevel@tonic-gate 	mac_impl_t	*mip;
3310Sstevel@tonic-gate 	ghte_t		hte;
3320Sstevel@tonic-gate 	int		err;
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 	/*
3350Sstevel@tonic-gate 	 * Check the device name length to make sure it won't overflow our
3360Sstevel@tonic-gate 	 * buffer.
3370Sstevel@tonic-gate 	 */
3380Sstevel@tonic-gate 	if (strlen(dev) >= MAXNAMELEN)
3390Sstevel@tonic-gate 		return (EINVAL);
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	/*
3420Sstevel@tonic-gate 	 * Split the device name into driver and instance components.
3430Sstevel@tonic-gate 	 */
3440Sstevel@tonic-gate 	if (ddi_parse(dev, driver, &instance) != DDI_SUCCESS)
3450Sstevel@tonic-gate 		return (EINVAL);
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	/*
3480Sstevel@tonic-gate 	 * Get the major number of the driver.
3490Sstevel@tonic-gate 	 */
3500Sstevel@tonic-gate 	if ((major = ddi_name_to_major(driver)) == (major_t)-1)
3510Sstevel@tonic-gate 		return (EINVAL);
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	/*
3540Sstevel@tonic-gate 	 * Hold the given instance to prevent it from being detached.
3550Sstevel@tonic-gate 	 * (This will also attach it if it is not currently attached).
3560Sstevel@tonic-gate 	 */
3570Sstevel@tonic-gate 	if ((dip = ddi_hold_devi_by_instance(major, instance, 0)) == NULL)
3580Sstevel@tonic-gate 		return (EINVAL);
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	/*
3610Sstevel@tonic-gate 	 * Construct the name of the MAC interface.
3620Sstevel@tonic-gate 	 */
3630Sstevel@tonic-gate 	MAC_NAME(name, dev, port);
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	/*
3660Sstevel@tonic-gate 	 * Look up its entry in the global hash table.
3670Sstevel@tonic-gate 	 */
3680Sstevel@tonic-gate again:
3690Sstevel@tonic-gate 	ght_lock(i_mac_impl_hash, GHT_WRITE);
3700Sstevel@tonic-gate 	if ((err = ght_find(i_mac_impl_hash, GHT_PTR_TO_KEY(name), &hte)) != 0)
3710Sstevel@tonic-gate 		goto failed;
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	mip = (mac_impl_t *)GHT_VAL(hte);
3740Sstevel@tonic-gate 	ASSERT(mip->mi_mp->m_dip == dip);
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	if (mip->mi_destroying) {
3770Sstevel@tonic-gate 		ght_unlock(i_mac_impl_hash);
3780Sstevel@tonic-gate 		goto again;
3790Sstevel@tonic-gate 	}
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	mip->mi_ref++;
3820Sstevel@tonic-gate 	ght_unlock(i_mac_impl_hash);
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	*mhp = (mac_handle_t)mip;
3850Sstevel@tonic-gate 	return (0);
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate failed:
3880Sstevel@tonic-gate 	ght_unlock(i_mac_impl_hash);
3890Sstevel@tonic-gate 	ddi_release_devi(dip);
3900Sstevel@tonic-gate 	return (err);
3910Sstevel@tonic-gate }
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate void
3940Sstevel@tonic-gate mac_close(mac_handle_t mh)
3950Sstevel@tonic-gate {
3960Sstevel@tonic-gate 	mac_impl_t	*mip = (mac_impl_t *)mh;
3970Sstevel@tonic-gate 	dev_info_t	*dip = mip->mi_mp->m_dip;
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 	ght_lock(i_mac_impl_hash, GHT_WRITE);
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 	ASSERT(mip->mi_ref != 0);
4020Sstevel@tonic-gate 	if (--mip->mi_ref == 0) {
4030Sstevel@tonic-gate 		ASSERT(!mip->mi_activelink);
4040Sstevel@tonic-gate 	}
4050Sstevel@tonic-gate 	ddi_release_devi(dip);
4060Sstevel@tonic-gate 	ght_unlock(i_mac_impl_hash);
4070Sstevel@tonic-gate }
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate const mac_info_t *
4100Sstevel@tonic-gate mac_info(mac_handle_t mh)
4110Sstevel@tonic-gate {
4120Sstevel@tonic-gate 	mac_impl_t	*mip = (mac_impl_t *)mh;
4130Sstevel@tonic-gate 	mac_t		*mp = mip->mi_mp;
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	/*
4160Sstevel@tonic-gate 	 * Return a pointer to the mac_info_t embedded in the mac_t.
4170Sstevel@tonic-gate 	 */
4180Sstevel@tonic-gate 	return (&(mp->m_info));
4190Sstevel@tonic-gate }
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate uint64_t
4220Sstevel@tonic-gate mac_stat_get(mac_handle_t mh, enum mac_stat stat)
4230Sstevel@tonic-gate {
4240Sstevel@tonic-gate 	mac_impl_t	*mip = (mac_impl_t *)mh;
4250Sstevel@tonic-gate 	mac_t		*mp = mip->mi_mp;
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	ASSERT(mp->m_info.mi_stat[stat]);
4280Sstevel@tonic-gate 	ASSERT(mp->m_stat != NULL);
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	/*
4310Sstevel@tonic-gate 	 * Call the driver to get the given statistic.
4320Sstevel@tonic-gate 	 */
4330Sstevel@tonic-gate 	return (mp->m_stat(mp->m_driver, stat));
4340Sstevel@tonic-gate }
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate int
4370Sstevel@tonic-gate mac_start(mac_handle_t mh)
4380Sstevel@tonic-gate {
4390Sstevel@tonic-gate 	mac_impl_t	*mip = (mac_impl_t *)mh;
4400Sstevel@tonic-gate 	mac_t		*mp = mip->mi_mp;
4410Sstevel@tonic-gate 	int		err;
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	ASSERT(mp->m_start != NULL);
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 	rw_enter(&(mip->mi_state_lock), RW_WRITER);
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 	/*
4480Sstevel@tonic-gate 	 * Check whether the device is already started.
4490Sstevel@tonic-gate 	 */
4500Sstevel@tonic-gate 	if (mip->mi_active++ != 0) {
4510Sstevel@tonic-gate 		/*
4520Sstevel@tonic-gate 		 * It's already started so there's nothing more to do.
4530Sstevel@tonic-gate 		 */
4540Sstevel@tonic-gate 		err = 0;
4550Sstevel@tonic-gate 		goto done;
4560Sstevel@tonic-gate 	}
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	/*
4590Sstevel@tonic-gate 	 * Start the device.
4600Sstevel@tonic-gate 	 */
4610Sstevel@tonic-gate 	if ((err = mp->m_start(mp->m_driver)) != 0)
4620Sstevel@tonic-gate 		--mip->mi_active;
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate done:
4650Sstevel@tonic-gate 	rw_exit(&(mip->mi_state_lock));
4660Sstevel@tonic-gate 	return (err);
4670Sstevel@tonic-gate }
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate void
4700Sstevel@tonic-gate mac_stop(mac_handle_t mh)
4710Sstevel@tonic-gate {
4720Sstevel@tonic-gate 	mac_impl_t	*mip = (mac_impl_t *)mh;
4730Sstevel@tonic-gate 	mac_t		*mp = mip->mi_mp;
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	ASSERT(mp->m_stop != NULL);
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 	rw_enter(&(mip->mi_state_lock), RW_WRITER);
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	/*
4800Sstevel@tonic-gate 	 * Check whether the device is still needed.
4810Sstevel@tonic-gate 	 */
4820Sstevel@tonic-gate 	ASSERT(mip->mi_active != 0);
4830Sstevel@tonic-gate 	if (--mip->mi_active != 0) {
4840Sstevel@tonic-gate 		/*
4850Sstevel@tonic-gate 		 * It's still needed so there's nothing more to do.
4860Sstevel@tonic-gate 		 */
4870Sstevel@tonic-gate 		goto done;
4880Sstevel@tonic-gate 	}
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	/*
4910Sstevel@tonic-gate 	 * Stop the device.
4920Sstevel@tonic-gate 	 */
4930Sstevel@tonic-gate 	mp->m_stop(mp->m_driver);
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate done:
4960Sstevel@tonic-gate 	rw_exit(&(mip->mi_state_lock));
4970Sstevel@tonic-gate }
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate int
5000Sstevel@tonic-gate mac_multicst_add(mac_handle_t mh, const uint8_t *addr)
5010Sstevel@tonic-gate {
5020Sstevel@tonic-gate 	mac_impl_t		*mip = (mac_impl_t *)mh;
5030Sstevel@tonic-gate 	mac_t			*mp = mip->mi_mp;
5040Sstevel@tonic-gate 	mac_multicst_addr_t	**pp;
5050Sstevel@tonic-gate 	mac_multicst_addr_t	*p;
5060Sstevel@tonic-gate 	int			err;
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	ASSERT(mp->m_multicst != NULL);
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 	/*
5110Sstevel@tonic-gate 	 * Verify the address.
5120Sstevel@tonic-gate 	 */
5130Sstevel@tonic-gate 	if (!(mip->mi_multicst_verify(mip, addr)))
5140Sstevel@tonic-gate 		return (EINVAL);
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	/*
5170Sstevel@tonic-gate 	 * Check whether the given address is already enabled.
5180Sstevel@tonic-gate 	 */
5190Sstevel@tonic-gate 	rw_enter(&(mip->mi_data_lock), RW_WRITER);
5200Sstevel@tonic-gate 	for (pp = &(mip->mi_mmap); (p = *pp) != NULL; pp = &(p->mma_nextp)) {
5210Sstevel@tonic-gate 		if (bcmp(p->mma_addr, addr, mip->mi_addr_length) == 0) {
5220Sstevel@tonic-gate 			/*
5230Sstevel@tonic-gate 			 * The address is already enabled so just bump the
5240Sstevel@tonic-gate 			 * reference count.
5250Sstevel@tonic-gate 			 */
5260Sstevel@tonic-gate 			p->mma_ref++;
5270Sstevel@tonic-gate 			err = 0;
5280Sstevel@tonic-gate 			goto done;
5290Sstevel@tonic-gate 		}
5300Sstevel@tonic-gate 	}
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	/*
5330Sstevel@tonic-gate 	 * Allocate a new list entry.
5340Sstevel@tonic-gate 	 */
5350Sstevel@tonic-gate 	if ((p = kmem_zalloc(sizeof (mac_multicst_addr_t),
5360Sstevel@tonic-gate 	    KM_NOSLEEP)) == NULL) {
5370Sstevel@tonic-gate 		err = ENOMEM;
5380Sstevel@tonic-gate 		goto done;
5390Sstevel@tonic-gate 	}
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	/*
5420Sstevel@tonic-gate 	 * Enable a new multicast address.
5430Sstevel@tonic-gate 	 */
5440Sstevel@tonic-gate 	if ((err = mp->m_multicst(mp->m_driver, B_TRUE, addr)) != 0) {
5450Sstevel@tonic-gate 		kmem_free(p, sizeof (mac_multicst_addr_t));
5460Sstevel@tonic-gate 		goto done;
5470Sstevel@tonic-gate 	}
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	/*
5500Sstevel@tonic-gate 	 * Add the address to the list of enabled addresses.
5510Sstevel@tonic-gate 	 */
5520Sstevel@tonic-gate 	bcopy(addr, p->mma_addr, mip->mi_addr_length);
5530Sstevel@tonic-gate 	p->mma_ref++;
5540Sstevel@tonic-gate 	*pp = p;
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate done:
5570Sstevel@tonic-gate 	rw_exit(&(mip->mi_data_lock));
5580Sstevel@tonic-gate 	return (err);
5590Sstevel@tonic-gate }
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate int
5620Sstevel@tonic-gate mac_multicst_remove(mac_handle_t mh, const uint8_t *addr)
5630Sstevel@tonic-gate {
5640Sstevel@tonic-gate 	mac_impl_t		*mip = (mac_impl_t *)mh;
5650Sstevel@tonic-gate 	mac_t			*mp = mip->mi_mp;
5660Sstevel@tonic-gate 	mac_multicst_addr_t	**pp;
5670Sstevel@tonic-gate 	mac_multicst_addr_t	*p;
5680Sstevel@tonic-gate 	int			err;
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 	ASSERT(mp->m_multicst != NULL);
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	/*
5730Sstevel@tonic-gate 	 * Find the entry in the list for the given address.
5740Sstevel@tonic-gate 	 */
5750Sstevel@tonic-gate 	rw_enter(&(mip->mi_data_lock), RW_WRITER);
5760Sstevel@tonic-gate 	for (pp = &(mip->mi_mmap); (p = *pp) != NULL; pp = &(p->mma_nextp)) {
5770Sstevel@tonic-gate 		if (bcmp(p->mma_addr, addr, mip->mi_addr_length) == 0) {
5780Sstevel@tonic-gate 			if (--p->mma_ref == 0)
5790Sstevel@tonic-gate 				break;
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 			/*
5820Sstevel@tonic-gate 			 * There is still a reference to this address so
5830Sstevel@tonic-gate 			 * there's nothing more to do.
5840Sstevel@tonic-gate 			 */
5850Sstevel@tonic-gate 			err = 0;
5860Sstevel@tonic-gate 			goto done;
5870Sstevel@tonic-gate 		}
5880Sstevel@tonic-gate 	}
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 	/*
5910Sstevel@tonic-gate 	 * We did not find an entry for the given address so it is not
5920Sstevel@tonic-gate 	 * currently enabled.
5930Sstevel@tonic-gate 	 */
5940Sstevel@tonic-gate 	if (p == NULL) {
5950Sstevel@tonic-gate 		err = ENOENT;
5960Sstevel@tonic-gate 		goto done;
5970Sstevel@tonic-gate 	}
5980Sstevel@tonic-gate 	ASSERT(p->mma_ref == 0);
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	/*
6010Sstevel@tonic-gate 	 * Disable the multicast address.
6020Sstevel@tonic-gate 	 */
6030Sstevel@tonic-gate 	if ((err = mp->m_multicst(mp->m_driver, B_FALSE, addr)) != 0) {
6040Sstevel@tonic-gate 		p->mma_ref++;
6050Sstevel@tonic-gate 		goto done;
6060Sstevel@tonic-gate 	}
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	/*
6090Sstevel@tonic-gate 	 * Remove it from the list.
6100Sstevel@tonic-gate 	 */
6110Sstevel@tonic-gate 	*pp = p->mma_nextp;
6120Sstevel@tonic-gate 	kmem_free(p, sizeof (mac_multicst_addr_t));
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate done:
6150Sstevel@tonic-gate 	rw_exit(&(mip->mi_data_lock));
6160Sstevel@tonic-gate 	return (err);
6170Sstevel@tonic-gate }
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate int
6200Sstevel@tonic-gate mac_unicst_set(mac_handle_t mh, const uint8_t *addr)
6210Sstevel@tonic-gate {
6220Sstevel@tonic-gate 	mac_impl_t	*mip = (mac_impl_t *)mh;
6230Sstevel@tonic-gate 	mac_t		*mp = mip->mi_mp;
6240Sstevel@tonic-gate 	int		err;
6250Sstevel@tonic-gate 	boolean_t	notify = B_FALSE;
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	ASSERT(mp->m_unicst != NULL);
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	/*
6300Sstevel@tonic-gate 	 * Verify the address.
6310Sstevel@tonic-gate 	 */
6320Sstevel@tonic-gate 	if (!(mip->mi_unicst_verify(mip, addr)))
6330Sstevel@tonic-gate 		return (EINVAL);
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	/*
6360Sstevel@tonic-gate 	 * Program the new unicast address.
6370Sstevel@tonic-gate 	 */
6380Sstevel@tonic-gate 	rw_enter(&(mip->mi_data_lock), RW_WRITER);
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 	/*
6410Sstevel@tonic-gate 	 * If address doesn't change, do nothing.
6420Sstevel@tonic-gate 	 * This check is necessary otherwise it may call into mac_unicst_set
6430Sstevel@tonic-gate 	 * recursively.
6440Sstevel@tonic-gate 	 */
6450Sstevel@tonic-gate 	if (bcmp(addr, mip->mi_addr, mip->mi_addr_length) == 0) {
6460Sstevel@tonic-gate 		err = 0;
6470Sstevel@tonic-gate 		goto done;
6480Sstevel@tonic-gate 	}
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 	if ((err = mp->m_unicst(mp->m_driver, addr)) != 0)
6510Sstevel@tonic-gate 		goto done;
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	/*
6540Sstevel@tonic-gate 	 * Save the address and flag that we need to send a notification.
6550Sstevel@tonic-gate 	 */
6560Sstevel@tonic-gate 	bcopy(addr, mip->mi_addr, mip->mi_addr_length);
6570Sstevel@tonic-gate 	notify = B_TRUE;
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate done:
6600Sstevel@tonic-gate 	rw_exit(&(mip->mi_data_lock));
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 	if (notify)
6630Sstevel@tonic-gate 		i_mac_notify(mip, MAC_NOTE_UNICST);
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 	return (err);
6660Sstevel@tonic-gate }
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate void
6690Sstevel@tonic-gate mac_unicst_get(mac_handle_t mh, uint8_t *addr)
6700Sstevel@tonic-gate {
6710Sstevel@tonic-gate 	mac_impl_t	*mip = (mac_impl_t *)mh;
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	/*
6740Sstevel@tonic-gate 	 * Copy out the current unicast address.
6750Sstevel@tonic-gate 	 */
6760Sstevel@tonic-gate 	rw_enter(&(mip->mi_data_lock), RW_READER);
6770Sstevel@tonic-gate 	bcopy(mip->mi_addr, addr, mip->mi_addr_length);
6780Sstevel@tonic-gate 	rw_exit(&(mip->mi_data_lock));
6790Sstevel@tonic-gate }
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate int
6820Sstevel@tonic-gate mac_promisc_set(mac_handle_t mh, boolean_t on, mac_promisc_type_t ptype)
6830Sstevel@tonic-gate {
6840Sstevel@tonic-gate 	mac_impl_t	*mip = (mac_impl_t *)mh;
6850Sstevel@tonic-gate 	mac_t		*mp = mip->mi_mp;
6860Sstevel@tonic-gate 	int		err = 0;
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	ASSERT(mp->m_promisc != NULL);
6890Sstevel@tonic-gate 	ASSERT(ptype == MAC_DEVPROMISC || ptype == MAC_PROMISC);
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 	/*
6920Sstevel@tonic-gate 	 * Determine whether we should enable or disable promiscuous mode.
6930Sstevel@tonic-gate 	 * For details on the distinction between "device promiscuous mode"
6940Sstevel@tonic-gate 	 * and "MAC promiscuous mode", see PSARC/2005/289.
6950Sstevel@tonic-gate 	 */
6960Sstevel@tonic-gate 	rw_enter(&(mip->mi_data_lock), RW_WRITER);
6970Sstevel@tonic-gate 	if (on) {
6980Sstevel@tonic-gate 		/*
6990Sstevel@tonic-gate 		 * Enable promiscuous mode on the device if not yet enabled.
7000Sstevel@tonic-gate 		 */
7010Sstevel@tonic-gate 		if (mip->mi_devpromisc++ == 0) {
7020Sstevel@tonic-gate 			if ((err = mp->m_promisc(mp->m_driver, B_TRUE)) != 0) {
7030Sstevel@tonic-gate 				mip->mi_devpromisc--;
7040Sstevel@tonic-gate 				goto done;
7050Sstevel@tonic-gate 			}
7060Sstevel@tonic-gate 			i_mac_notify(mip, MAC_NOTE_DEVPROMISC);
7070Sstevel@tonic-gate 		}
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 		/*
7100Sstevel@tonic-gate 		 * Enable promiscuous mode on the MAC if not yet enabled.
7110Sstevel@tonic-gate 		 */
7120Sstevel@tonic-gate 		if (ptype == MAC_PROMISC && mip->mi_promisc++ == 0)
7130Sstevel@tonic-gate 			i_mac_notify(mip, MAC_NOTE_PROMISC);
7140Sstevel@tonic-gate 	} else {
7150Sstevel@tonic-gate 		if (mip->mi_devpromisc == 0) {
7160Sstevel@tonic-gate 			err = EPROTO;
7170Sstevel@tonic-gate 			goto done;
7180Sstevel@tonic-gate 		}
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 		/*
7210Sstevel@tonic-gate 		 * Disable promiscuous mode on the device if this is the last
7220Sstevel@tonic-gate 		 * enabling.
7230Sstevel@tonic-gate 		 */
7240Sstevel@tonic-gate 		if (--mip->mi_devpromisc == 0) {
7250Sstevel@tonic-gate 			if ((err = mp->m_promisc(mp->m_driver, B_FALSE)) != 0) {
7260Sstevel@tonic-gate 				mip->mi_devpromisc++;
7270Sstevel@tonic-gate 				goto done;
7280Sstevel@tonic-gate 			}
7290Sstevel@tonic-gate 			i_mac_notify(mip, MAC_NOTE_DEVPROMISC);
7300Sstevel@tonic-gate 		}
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 		/*
7330Sstevel@tonic-gate 		 * Disable promiscuous mode on the MAC if this is the last
7340Sstevel@tonic-gate 		 * enabling.
7350Sstevel@tonic-gate 		 */
7360Sstevel@tonic-gate 		if (ptype == MAC_PROMISC && --mip->mi_promisc == 0)
7370Sstevel@tonic-gate 			i_mac_notify(mip, MAC_NOTE_PROMISC);
7380Sstevel@tonic-gate 	}
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate done:
7410Sstevel@tonic-gate 	rw_exit(&(mip->mi_data_lock));
7420Sstevel@tonic-gate 	return (err);
7430Sstevel@tonic-gate }
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate boolean_t
7460Sstevel@tonic-gate mac_promisc_get(mac_handle_t mh, mac_promisc_type_t ptype)
7470Sstevel@tonic-gate {
7480Sstevel@tonic-gate 	mac_impl_t		*mip = (mac_impl_t *)mh;
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 	ASSERT(ptype == MAC_DEVPROMISC || ptype == MAC_PROMISC);
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 	/*
7530Sstevel@tonic-gate 	 * Return the current promiscuity.
7540Sstevel@tonic-gate 	 */
7550Sstevel@tonic-gate 	if (ptype == MAC_DEVPROMISC)
7560Sstevel@tonic-gate 		return (mip->mi_devpromisc != 0);
7570Sstevel@tonic-gate 	else
7580Sstevel@tonic-gate 		return (mip->mi_promisc != 0);
7590Sstevel@tonic-gate }
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate void
7620Sstevel@tonic-gate mac_resources(mac_handle_t mh)
7630Sstevel@tonic-gate {
7640Sstevel@tonic-gate 	mac_impl_t	*mip = (mac_impl_t *)mh;
7650Sstevel@tonic-gate 	mac_t		*mp = mip->mi_mp;
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 	ASSERT(mp->m_resources != NULL);
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 	/*
7700Sstevel@tonic-gate 	 * Call the driver to register its resources.
7710Sstevel@tonic-gate 	 */
7720Sstevel@tonic-gate 	mp->m_resources(mp->m_driver);
7730Sstevel@tonic-gate }
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate void
7760Sstevel@tonic-gate mac_ioctl(mac_handle_t mh, queue_t *wq, mblk_t *bp)
7770Sstevel@tonic-gate {
7780Sstevel@tonic-gate 	mac_impl_t	*mip = (mac_impl_t *)mh;
7790Sstevel@tonic-gate 	mac_t		*mp = mip->mi_mp;
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	ASSERT(mp->m_ioctl != NULL);
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	/*
7840Sstevel@tonic-gate 	 * Call the driver to handle the ioctl.
7850Sstevel@tonic-gate 	 */
7860Sstevel@tonic-gate 	mp->m_ioctl(mp->m_driver, wq, bp);
7870Sstevel@tonic-gate }
7880Sstevel@tonic-gate 
789*56Smeem const mac_txinfo_t *
790*56Smeem mac_tx_get(mac_handle_t mh)
7910Sstevel@tonic-gate {
7920Sstevel@tonic-gate 	mac_impl_t	*mip = (mac_impl_t *)mh;
793*56Smeem 	mac_txinfo_t	*mtp;
794*56Smeem 
795*56Smeem 	/*
796*56Smeem 	 * Grab the lock to prevent us from racing with MAC_PROMISC being
797*56Smeem 	 * changed.  This is sufficient since MAC clients are careful to always
798*56Smeem 	 * call mac_txloop_add() prior to enabling MAC_PROMISC, and to disable
799*56Smeem 	 * MAC_PROMISC prior to calling mac_txloop_remove().
800*56Smeem 	 */
801*56Smeem 	rw_enter(&mip->mi_txloop_lock, RW_READER);
8020Sstevel@tonic-gate 
803*56Smeem 	if (mac_promisc_get(mh, MAC_PROMISC)) {
804*56Smeem 		ASSERT(mip->mi_mtfp != NULL);
805*56Smeem 		mtp = &mip->mi_txloopinfo;
806*56Smeem 	} else {
807*56Smeem 		/*
808*56Smeem 		 * Note that we cannot ASSERT() that mip->mi_mtfp is NULL,
809*56Smeem 		 * because to satisfy the above ASSERT(), we have to disable
810*56Smeem 		 * MAC_PROMISC prior to calling mac_txloop_remove().
811*56Smeem 		 */
812*56Smeem 		mtp = &mip->mi_txinfo;
8130Sstevel@tonic-gate 
814*56Smeem 	}
815*56Smeem 
816*56Smeem 	rw_exit(&mip->mi_txloop_lock);
817*56Smeem 	return (mtp);
8180Sstevel@tonic-gate }
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate link_state_t
8210Sstevel@tonic-gate mac_link_get(mac_handle_t mh)
8220Sstevel@tonic-gate {
8230Sstevel@tonic-gate 	mac_impl_t	*mip = (mac_impl_t *)mh;
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	/*
8260Sstevel@tonic-gate 	 * Return the current link state.
8270Sstevel@tonic-gate 	 */
8280Sstevel@tonic-gate 	return (mip->mi_link);
8290Sstevel@tonic-gate }
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate mac_notify_handle_t
8320Sstevel@tonic-gate mac_notify_add(mac_handle_t mh, mac_notify_t notify, void *arg)
8330Sstevel@tonic-gate {
8340Sstevel@tonic-gate 	mac_impl_t		*mip = (mac_impl_t *)mh;
8350Sstevel@tonic-gate 	mac_notify_fn_t		*mnfp;
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 	mnfp = kmem_zalloc(sizeof (mac_notify_fn_t), KM_SLEEP);
8380Sstevel@tonic-gate 	mnfp->mnf_fn = notify;
8390Sstevel@tonic-gate 	mnfp->mnf_arg = arg;
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 	/*
8420Sstevel@tonic-gate 	 * Add it to the head of the 'notify' callback list.
8430Sstevel@tonic-gate 	 */
8440Sstevel@tonic-gate 	rw_enter(&(mip->mi_notify_lock), RW_WRITER);
8450Sstevel@tonic-gate 	mnfp->mnf_nextp = mip->mi_mnfp;
8460Sstevel@tonic-gate 	mip->mi_mnfp = mnfp;
8470Sstevel@tonic-gate 	rw_exit(&(mip->mi_notify_lock));
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 	return ((mac_notify_handle_t)mnfp);
8500Sstevel@tonic-gate }
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate void
8530Sstevel@tonic-gate mac_notify_remove(mac_handle_t mh, mac_notify_handle_t mnh)
8540Sstevel@tonic-gate {
8550Sstevel@tonic-gate 	mac_impl_t		*mip = (mac_impl_t *)mh;
8560Sstevel@tonic-gate 	mac_notify_fn_t		*mnfp = (mac_notify_fn_t *)mnh;
8570Sstevel@tonic-gate 	mac_notify_fn_t		**pp;
8580Sstevel@tonic-gate 	mac_notify_fn_t		*p;
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 	/*
8610Sstevel@tonic-gate 	 * Search the 'notify' callback list for the function closure.
8620Sstevel@tonic-gate 	 */
8630Sstevel@tonic-gate 	rw_enter(&(mip->mi_notify_lock), RW_WRITER);
8640Sstevel@tonic-gate 	for (pp = &(mip->mi_mnfp); (p = *pp) != NULL;
8650Sstevel@tonic-gate 	    pp = &(p->mnf_nextp)) {
8660Sstevel@tonic-gate 		if (p == mnfp)
8670Sstevel@tonic-gate 			break;
8680Sstevel@tonic-gate 	}
8690Sstevel@tonic-gate 	ASSERT(p != NULL);
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 	/*
8720Sstevel@tonic-gate 	 * Remove it from the list.
8730Sstevel@tonic-gate 	 */
8740Sstevel@tonic-gate 	*pp = p->mnf_nextp;
8750Sstevel@tonic-gate 	rw_exit(&(mip->mi_notify_lock));
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 	/*
8780Sstevel@tonic-gate 	 * Free it.
8790Sstevel@tonic-gate 	 */
8800Sstevel@tonic-gate 	kmem_free(mnfp, sizeof (mac_notify_fn_t));
8810Sstevel@tonic-gate }
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate void
8840Sstevel@tonic-gate mac_notify(mac_handle_t mh)
8850Sstevel@tonic-gate {
8860Sstevel@tonic-gate 	mac_impl_t		*mip = (mac_impl_t *)mh;
8870Sstevel@tonic-gate 	mac_notify_type_t	type;
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 	for (type = 0; type < MAC_NNOTE; type++)
8900Sstevel@tonic-gate 		i_mac_notify(mip, type);
8910Sstevel@tonic-gate }
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate mac_rx_handle_t
8940Sstevel@tonic-gate mac_rx_add(mac_handle_t mh, mac_rx_t rx, void *arg)
8950Sstevel@tonic-gate {
8960Sstevel@tonic-gate 	mac_impl_t	*mip = (mac_impl_t *)mh;
8970Sstevel@tonic-gate 	mac_rx_fn_t	*mrfp;
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate 	mrfp = kmem_zalloc(sizeof (mac_rx_fn_t), KM_SLEEP);
9000Sstevel@tonic-gate 	mrfp->mrf_fn = rx;
9010Sstevel@tonic-gate 	mrfp->mrf_arg = arg;
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 	/*
9040Sstevel@tonic-gate 	 * Add it to the head of the 'rx' callback list.
9050Sstevel@tonic-gate 	 */
9060Sstevel@tonic-gate 	rw_enter(&(mip->mi_rx_lock), RW_WRITER);
9070Sstevel@tonic-gate 	mrfp->mrf_nextp = mip->mi_mrfp;
9080Sstevel@tonic-gate 	mip->mi_mrfp = mrfp;
9090Sstevel@tonic-gate 	rw_exit(&(mip->mi_rx_lock));
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 	return ((mac_rx_handle_t)mrfp);
9120Sstevel@tonic-gate }
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate /*
9150Sstevel@tonic-gate  * Unregister a receive function for this mac.  This removes the function
9160Sstevel@tonic-gate  * from the list of receive functions for this mac.
9170Sstevel@tonic-gate  */
9180Sstevel@tonic-gate void
9190Sstevel@tonic-gate mac_rx_remove(mac_handle_t mh, mac_rx_handle_t mrh)
9200Sstevel@tonic-gate {
9210Sstevel@tonic-gate 	mac_impl_t		*mip = (mac_impl_t *)mh;
9220Sstevel@tonic-gate 	mac_rx_fn_t		*mrfp = (mac_rx_fn_t *)mrh;
9230Sstevel@tonic-gate 	mac_rx_fn_t		**pp;
9240Sstevel@tonic-gate 	mac_rx_fn_t		*p;
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 	/*
9270Sstevel@tonic-gate 	 * Search the 'rx' callback list for the function closure.
9280Sstevel@tonic-gate 	 */
9290Sstevel@tonic-gate 	rw_enter(&(mip->mi_rx_lock), RW_WRITER);
9300Sstevel@tonic-gate 	for (pp = &(mip->mi_mrfp); (p = *pp) != NULL; pp = &(p->mrf_nextp)) {
9310Sstevel@tonic-gate 		if (p == mrfp)
9320Sstevel@tonic-gate 			break;
9330Sstevel@tonic-gate 	}
9340Sstevel@tonic-gate 	ASSERT(p != NULL);
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate 	/* Remove it from the list. */
9370Sstevel@tonic-gate 	*pp = p->mrf_nextp;
9380Sstevel@tonic-gate 	kmem_free(mrfp, sizeof (mac_rx_fn_t));
9390Sstevel@tonic-gate 	rw_exit(&(mip->mi_rx_lock));
9400Sstevel@tonic-gate }
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate mac_txloop_handle_t
9430Sstevel@tonic-gate mac_txloop_add(mac_handle_t mh, mac_txloop_t tx, void *arg)
9440Sstevel@tonic-gate {
9450Sstevel@tonic-gate 	mac_impl_t	*mip = (mac_impl_t *)mh;
9460Sstevel@tonic-gate 	mac_txloop_fn_t	*mtfp;
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 	mtfp = kmem_zalloc(sizeof (mac_txloop_fn_t), KM_SLEEP);
9490Sstevel@tonic-gate 	mtfp->mtf_fn = tx;
9500Sstevel@tonic-gate 	mtfp->mtf_arg = arg;
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 	/*
9530Sstevel@tonic-gate 	 * Add it to the head of the 'tx' callback list.
9540Sstevel@tonic-gate 	 */
9550Sstevel@tonic-gate 	rw_enter(&(mip->mi_txloop_lock), RW_WRITER);
9560Sstevel@tonic-gate 	mtfp->mtf_nextp = mip->mi_mtfp;
9570Sstevel@tonic-gate 	mip->mi_mtfp = mtfp;
9580Sstevel@tonic-gate 	rw_exit(&(mip->mi_txloop_lock));
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 	return ((mac_txloop_handle_t)mtfp);
9610Sstevel@tonic-gate }
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate /*
9640Sstevel@tonic-gate  * Unregister a transmit function for this mac.  This removes the function
9650Sstevel@tonic-gate  * from the list of transmit functions for this mac.
9660Sstevel@tonic-gate  */
9670Sstevel@tonic-gate void
9680Sstevel@tonic-gate mac_txloop_remove(mac_handle_t mh, mac_txloop_handle_t mth)
9690Sstevel@tonic-gate {
9700Sstevel@tonic-gate 	mac_impl_t		*mip = (mac_impl_t *)mh;
9710Sstevel@tonic-gate 	mac_txloop_fn_t		*mtfp = (mac_txloop_fn_t *)mth;
9720Sstevel@tonic-gate 	mac_txloop_fn_t		**pp;
9730Sstevel@tonic-gate 	mac_txloop_fn_t		*p;
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate 	/*
9760Sstevel@tonic-gate 	 * Search the 'tx' callback list for the function.
9770Sstevel@tonic-gate 	 */
9780Sstevel@tonic-gate 	rw_enter(&(mip->mi_txloop_lock), RW_WRITER);
9790Sstevel@tonic-gate 	for (pp = &(mip->mi_mtfp); (p = *pp) != NULL; pp = &(p->mtf_nextp)) {
9800Sstevel@tonic-gate 		if (p == mtfp)
9810Sstevel@tonic-gate 			break;
9820Sstevel@tonic-gate 	}
9830Sstevel@tonic-gate 	ASSERT(p != NULL);
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 	/* Remove it from the list. */
9860Sstevel@tonic-gate 	*pp = p->mtf_nextp;
9870Sstevel@tonic-gate 	kmem_free(mtfp, sizeof (mac_txloop_fn_t));
9880Sstevel@tonic-gate 	rw_exit(&(mip->mi_txloop_lock));
9890Sstevel@tonic-gate }
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate void
9920Sstevel@tonic-gate mac_resource_set(mac_handle_t mh, mac_resource_add_t add, void *arg)
9930Sstevel@tonic-gate {
9940Sstevel@tonic-gate 	mac_impl_t		*mip = (mac_impl_t *)mh;
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 	/*
9970Sstevel@tonic-gate 	 * Update the 'resource_add' callbacks.
9980Sstevel@tonic-gate 	 */
9990Sstevel@tonic-gate 	rw_enter(&(mip->mi_resource_lock), RW_WRITER);
10000Sstevel@tonic-gate 	mip->mi_resource_add = add;
10010Sstevel@tonic-gate 	mip->mi_resource_add_arg = arg;
10020Sstevel@tonic-gate 	rw_exit(&(mip->mi_resource_lock));
10030Sstevel@tonic-gate }
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate /*
10060Sstevel@tonic-gate  * Driver support functions.
10070Sstevel@tonic-gate  */
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate int
10100Sstevel@tonic-gate mac_register(mac_t *mp)
10110Sstevel@tonic-gate {
10120Sstevel@tonic-gate 	int	err;
10130Sstevel@tonic-gate 	char	name[MAXNAMELEN], aggr_name[MAXNAMELEN];
10140Sstevel@tonic-gate 	struct devnames *dnp;
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate #ifdef	DEBUG
10170Sstevel@tonic-gate 	if (strcmp(mp->m_ident, MAC_IDENT) != 0)
10180Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d/%d: possible mac interface mismatch",
10190Sstevel@tonic-gate 		    ddi_driver_name(mp->m_dip),
10200Sstevel@tonic-gate 		    ddi_get_instance(mp->m_dip),
10210Sstevel@tonic-gate 		    mp->m_port);
10220Sstevel@tonic-gate #endif	/* DEBUG */
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate 	ASSERT(!(mp->m_info.mi_addr_length & 1));
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 	/*
10270Sstevel@tonic-gate 	 * Create a new mac_impl_t to pair with the mac_t.
10280Sstevel@tonic-gate 	 */
10290Sstevel@tonic-gate 	if ((err = i_mac_create(mp)) != 0)
10300Sstevel@tonic-gate 		return (err);
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 	/*
10330Sstevel@tonic-gate 	 * Create a DDI_NT_MAC minor node such that libdevinfo(3lib) can be
10340Sstevel@tonic-gate 	 * used to search for mac interfaces.
10350Sstevel@tonic-gate 	 */
10360Sstevel@tonic-gate 	(void) sprintf(name, "%d", mp->m_port);
10370Sstevel@tonic-gate 	if (ddi_create_minor_node(mp->m_dip, name, S_IFCHR, mp->m_port,
10380Sstevel@tonic-gate 	    DDI_NT_MAC, 0) != DDI_SUCCESS) {
10390Sstevel@tonic-gate 		i_mac_destroy(mp);
10400Sstevel@tonic-gate 		return (EEXIST);
10410Sstevel@tonic-gate 	}
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 	/*
10440Sstevel@tonic-gate 	 * Right now only the "aggr" driver creates nodes at mac_register
10450Sstevel@tonic-gate 	 * time, but it is expected that in the future with some
10460Sstevel@tonic-gate 	 * enhancement of devfs, all the drivers can create nodes here.
10470Sstevel@tonic-gate 	 */
10480Sstevel@tonic-gate 	if (strcmp(ddi_driver_name(mp->m_dip), "aggr") == 0) {
10490Sstevel@tonic-gate 		(void) snprintf(aggr_name, MAXNAMELEN, "aggr%u", mp->m_port);
10500Sstevel@tonic-gate 		err = dld_ppa_create(aggr_name, AGGR_DEV, mp->m_port, 0);
10510Sstevel@tonic-gate 		if (err != 0) {
10520Sstevel@tonic-gate 			ASSERT(err != EEXIST);
10530Sstevel@tonic-gate 			ddi_remove_minor_node(mp->m_dip, name);
10540Sstevel@tonic-gate 			i_mac_destroy(mp);
10550Sstevel@tonic-gate 			return (err);
10560Sstevel@tonic-gate 		}
10570Sstevel@tonic-gate 	}
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 	/* set the gldv3 flag in dn_flags */
10600Sstevel@tonic-gate 	dnp = &devnamesp[ddi_driver_major(mp->m_dip)];
10610Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
10620Sstevel@tonic-gate 	dnp->dn_flags |= DN_GLDV3_DRIVER;
10630Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
10640Sstevel@tonic-gate 
10650Sstevel@tonic-gate 	cmn_err(CE_NOTE, "!%s%d/%d registered",
10660Sstevel@tonic-gate 	    ddi_driver_name(mp->m_dip),
10670Sstevel@tonic-gate 	    ddi_get_instance(mp->m_dip),
10680Sstevel@tonic-gate 	    mp->m_port);
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate 	return (0);
10710Sstevel@tonic-gate }
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate int
10740Sstevel@tonic-gate mac_unregister(mac_t *mp)
10750Sstevel@tonic-gate {
10760Sstevel@tonic-gate 	int		err;
10770Sstevel@tonic-gate 	char		name[MAXNAMELEN];
10780Sstevel@tonic-gate 	mac_impl_t	*mip = mp->m_impl;
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate 	/*
10810Sstevel@tonic-gate 	 * See if there are any other references to this mac_t (e.g., VLAN's).
10820Sstevel@tonic-gate 	 * If not, set mi_destroying to prevent any new VLAN's from being
10830Sstevel@tonic-gate 	 * created before we can perform the i_mac_destroy() below.
10840Sstevel@tonic-gate 	 */
10850Sstevel@tonic-gate 	ght_lock(i_mac_impl_hash, GHT_WRITE);
10860Sstevel@tonic-gate 	if (mip->mi_ref > 0) {
10870Sstevel@tonic-gate 		ght_unlock(i_mac_impl_hash);
10880Sstevel@tonic-gate 		return (EBUSY);
10890Sstevel@tonic-gate 	}
10900Sstevel@tonic-gate 	mip->mi_destroying = B_TRUE;
10910Sstevel@tonic-gate 	ght_unlock(i_mac_impl_hash);
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 	if (strcmp(ddi_driver_name(mp->m_dip), "aggr") == 0) {
10940Sstevel@tonic-gate 		(void) snprintf(name, MAXNAMELEN, "aggr%u", mp->m_port);
10950Sstevel@tonic-gate 		if ((err = dld_ppa_destroy(name)) != 0)
10960Sstevel@tonic-gate 			return (err);
10970Sstevel@tonic-gate 	}
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate 	/*
11000Sstevel@tonic-gate 	 * Destroy the mac_impl_t.
11010Sstevel@tonic-gate 	 */
11020Sstevel@tonic-gate 	i_mac_destroy(mp);
11030Sstevel@tonic-gate 
11040Sstevel@tonic-gate 	/*
11050Sstevel@tonic-gate 	 * Remove the minor node.
11060Sstevel@tonic-gate 	 */
11070Sstevel@tonic-gate 	(void) sprintf(name, "%d", mp->m_port);
11080Sstevel@tonic-gate 	ddi_remove_minor_node(mp->m_dip, name);
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate 	cmn_err(CE_NOTE, "!%s%d/%d unregistered",
11110Sstevel@tonic-gate 	    ddi_driver_name(mp->m_dip),
11120Sstevel@tonic-gate 	    ddi_get_instance(mp->m_dip),
11130Sstevel@tonic-gate 	    mp->m_port);
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate 	return (0);
11160Sstevel@tonic-gate }
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate void
11190Sstevel@tonic-gate mac_rx(mac_t *mp, mac_resource_handle_t mrh, mblk_t *bp)
11200Sstevel@tonic-gate {
11210Sstevel@tonic-gate 	mac_impl_t	*mip = mp->m_impl;
11220Sstevel@tonic-gate 	mac_rx_fn_t	*mrfp;
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 	/*
11250Sstevel@tonic-gate 	 * Call all registered receive functions.
11260Sstevel@tonic-gate 	 */
11270Sstevel@tonic-gate 	rw_enter(&mip->mi_rx_lock, RW_READER);
11280Sstevel@tonic-gate 	mrfp = mip->mi_mrfp;
11290Sstevel@tonic-gate 	if (mrfp == NULL) {
11300Sstevel@tonic-gate 		/* There are no registered receive functions. */
11310Sstevel@tonic-gate 		freemsgchain(bp);
11320Sstevel@tonic-gate 		rw_exit(&mip->mi_rx_lock);
11330Sstevel@tonic-gate 		return;
11340Sstevel@tonic-gate 	}
11350Sstevel@tonic-gate 	do {
11360Sstevel@tonic-gate 		mblk_t *recv_bp;
11370Sstevel@tonic-gate 
11380Sstevel@tonic-gate 		if (mrfp->mrf_nextp != NULL) {
11390Sstevel@tonic-gate 			/* XXX Do we bump a counter if copymsgchain() fails? */
11400Sstevel@tonic-gate 			recv_bp = copymsgchain(bp);
11410Sstevel@tonic-gate 		} else {
11420Sstevel@tonic-gate 			recv_bp = bp;
11430Sstevel@tonic-gate 		}
11440Sstevel@tonic-gate 		if (recv_bp != NULL)
11450Sstevel@tonic-gate 			mrfp->mrf_fn(mrfp->mrf_arg, mrh, recv_bp);
11460Sstevel@tonic-gate 		mrfp = mrfp->mrf_nextp;
11470Sstevel@tonic-gate 	} while (mrfp != NULL);
11480Sstevel@tonic-gate 	rw_exit(&mip->mi_rx_lock);
11490Sstevel@tonic-gate }
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate /*
11520Sstevel@tonic-gate  * Transmit function -- ONLY used when there are registered loopback listeners.
11530Sstevel@tonic-gate  */
11540Sstevel@tonic-gate mblk_t *
11550Sstevel@tonic-gate mac_txloop(void *arg, mblk_t *bp)
11560Sstevel@tonic-gate {
11570Sstevel@tonic-gate 	mac_impl_t	*mip = arg;
11580Sstevel@tonic-gate 	mac_t		*mp = mip->mi_mp;
11590Sstevel@tonic-gate 	mac_txloop_fn_t	*mtfp;
11600Sstevel@tonic-gate 	mblk_t		*loop_bp, *resid_bp, *next_bp;
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate 	while (bp != NULL) {
11630Sstevel@tonic-gate 		next_bp = bp->b_next;
11640Sstevel@tonic-gate 		bp->b_next = NULL;
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate 		if ((loop_bp = copymsg(bp)) == NULL)
11670Sstevel@tonic-gate 			goto noresources;
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 		if ((resid_bp = mp->m_tx(mp->m_driver, bp)) != NULL) {
11700Sstevel@tonic-gate 			ASSERT(resid_bp == bp);
11710Sstevel@tonic-gate 			freemsg(loop_bp);
11720Sstevel@tonic-gate 			goto noresources;
11730Sstevel@tonic-gate 		}
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate 		rw_enter(&mip->mi_txloop_lock, RW_READER);
11760Sstevel@tonic-gate 		mtfp = mip->mi_mtfp;
1177*56Smeem 		while (mtfp != NULL && loop_bp != NULL) {
11780Sstevel@tonic-gate 			bp = loop_bp;
1179*56Smeem 
1180*56Smeem 			/* XXX counter bump if copymsg() fails? */
1181*56Smeem 			if (mtfp->mtf_nextp != NULL)
11820Sstevel@tonic-gate 				loop_bp = copymsg(bp);
1183*56Smeem 			else
1184*56Smeem 				loop_bp = NULL;
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate 			mtfp->mtf_fn(mtfp->mtf_arg, bp);
1187*56Smeem 			mtfp = mtfp->mtf_nextp;
11880Sstevel@tonic-gate 		}
1189*56Smeem 		rw_exit(&mip->mi_txloop_lock);
11900Sstevel@tonic-gate 
1191*56Smeem 		/*
1192*56Smeem 		 * It's possible we've raced with the disabling of promiscuous
1193*56Smeem 		 * mode, in which case we can discard our copy.
1194*56Smeem 		 */
1195*56Smeem 		if (loop_bp != NULL)
1196*56Smeem 			freemsg(loop_bp);
1197*56Smeem 
11980Sstevel@tonic-gate 		bp = next_bp;
11990Sstevel@tonic-gate 	}
12000Sstevel@tonic-gate 
12010Sstevel@tonic-gate 	return (NULL);
12020Sstevel@tonic-gate 
12030Sstevel@tonic-gate noresources:
12040Sstevel@tonic-gate 	bp->b_next = next_bp;
12050Sstevel@tonic-gate 	return (bp);
12060Sstevel@tonic-gate }
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate void
12090Sstevel@tonic-gate mac_link_update(mac_t *mp, link_state_t link)
12100Sstevel@tonic-gate {
12110Sstevel@tonic-gate 	mac_impl_t	*mip = mp->m_impl;
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate 	ASSERT(mip->mi_mp == mp);
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate 	/*
12160Sstevel@tonic-gate 	 * Save the link state.
12170Sstevel@tonic-gate 	 */
12180Sstevel@tonic-gate 	mip->mi_link = link;
12190Sstevel@tonic-gate 
12200Sstevel@tonic-gate 	/*
12210Sstevel@tonic-gate 	 * Send a MAC_NOTE_LINK notification.
12220Sstevel@tonic-gate 	 */
12230Sstevel@tonic-gate 	i_mac_notify(mip, MAC_NOTE_LINK);
12240Sstevel@tonic-gate }
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate void
12270Sstevel@tonic-gate mac_unicst_update(mac_t *mp, const uint8_t *addr)
12280Sstevel@tonic-gate {
12290Sstevel@tonic-gate 	mac_impl_t	*mip = mp->m_impl;
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate 	ASSERT(mip->mi_mp == mp);
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 	/*
12340Sstevel@tonic-gate 	 * Save the address.
12350Sstevel@tonic-gate 	 */
12360Sstevel@tonic-gate 	bcopy(addr, mip->mi_addr, mip->mi_addr_length);
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 	/*
12390Sstevel@tonic-gate 	 * Send a MAC_NOTE_UNICST notification.
12400Sstevel@tonic-gate 	 */
12410Sstevel@tonic-gate 	i_mac_notify(mip, MAC_NOTE_UNICST);
12420Sstevel@tonic-gate }
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate void
12450Sstevel@tonic-gate mac_tx_update(mac_t *mp)
12460Sstevel@tonic-gate {
12470Sstevel@tonic-gate 	mac_impl_t	*mip = mp->m_impl;
12480Sstevel@tonic-gate 
12490Sstevel@tonic-gate 	ASSERT(mip->mi_mp == mp);
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate 	/*
12520Sstevel@tonic-gate 	 * Send a MAC_NOTE_TX notification.
12530Sstevel@tonic-gate 	 */
12540Sstevel@tonic-gate 	i_mac_notify(mip, MAC_NOTE_TX);
12550Sstevel@tonic-gate }
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate void
12580Sstevel@tonic-gate mac_resource_update(mac_t *mp)
12590Sstevel@tonic-gate {
12600Sstevel@tonic-gate 	mac_impl_t	*mip = mp->m_impl;
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate 	ASSERT(mip->mi_mp == mp);
12630Sstevel@tonic-gate 
12640Sstevel@tonic-gate 	/*
12650Sstevel@tonic-gate 	 * Send a MAC_NOTE_RESOURCE notification.
12660Sstevel@tonic-gate 	 */
12670Sstevel@tonic-gate 	i_mac_notify(mip, MAC_NOTE_RESOURCE);
12680Sstevel@tonic-gate }
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate mac_resource_handle_t
12710Sstevel@tonic-gate mac_resource_add(mac_t *mp, mac_resource_t *mrp)
12720Sstevel@tonic-gate {
12730Sstevel@tonic-gate 	mac_impl_t		*mip = mp->m_impl;
12740Sstevel@tonic-gate 	mac_resource_handle_t	mrh;
12750Sstevel@tonic-gate 	mac_resource_add_t	add;
12760Sstevel@tonic-gate 	void			*arg;
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 	rw_enter(&mip->mi_resource_lock, RW_READER);
12790Sstevel@tonic-gate 	add = mip->mi_resource_add;
12800Sstevel@tonic-gate 	arg = mip->mi_resource_add_arg;
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate 	mrh = add(arg, mrp);
12830Sstevel@tonic-gate 	rw_exit(&mip->mi_resource_lock);
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 	return (mrh);
12860Sstevel@tonic-gate }
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate void
12890Sstevel@tonic-gate mac_multicst_refresh(mac_t *mp, mac_multicst_t refresh, void *arg,
12900Sstevel@tonic-gate     boolean_t add)
12910Sstevel@tonic-gate {
12920Sstevel@tonic-gate 	mac_impl_t		*mip = mp->m_impl;
12930Sstevel@tonic-gate 	mac_multicst_addr_t	*p;
12940Sstevel@tonic-gate 
12950Sstevel@tonic-gate 	/*
12960Sstevel@tonic-gate 	 * If no specific refresh function was given then default to the
12970Sstevel@tonic-gate 	 * driver's m_multicst entry point.
12980Sstevel@tonic-gate 	 */
12990Sstevel@tonic-gate 	if (refresh == NULL) {
13000Sstevel@tonic-gate 		refresh = mp->m_multicst;
13010Sstevel@tonic-gate 		arg = mp->m_driver;
13020Sstevel@tonic-gate 	}
13030Sstevel@tonic-gate 	ASSERT(refresh != NULL);
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate 	/*
13060Sstevel@tonic-gate 	 * Walk the multicast address list and call the refresh function for
13070Sstevel@tonic-gate 	 * each address.
13080Sstevel@tonic-gate 	 */
13090Sstevel@tonic-gate 	rw_enter(&(mip->mi_data_lock), RW_READER);
13100Sstevel@tonic-gate 	for (p = mip->mi_mmap; p != NULL; p = p->mma_nextp)
13110Sstevel@tonic-gate 		refresh(arg, add, p->mma_addr);
13120Sstevel@tonic-gate 	rw_exit(&(mip->mi_data_lock));
13130Sstevel@tonic-gate }
13140Sstevel@tonic-gate 
13150Sstevel@tonic-gate void
13160Sstevel@tonic-gate mac_unicst_refresh(mac_t *mp, mac_unicst_t refresh, void *arg)
13170Sstevel@tonic-gate {
13180Sstevel@tonic-gate 	mac_impl_t	*mip = mp->m_impl;
13190Sstevel@tonic-gate 	/*
13200Sstevel@tonic-gate 	 * If no specific refresh function was given then default to the
13210Sstevel@tonic-gate 	 * driver's m_unicst entry point.
13220Sstevel@tonic-gate 	 */
13230Sstevel@tonic-gate 	if (refresh == NULL) {
13240Sstevel@tonic-gate 		refresh = mp->m_unicst;
13250Sstevel@tonic-gate 		arg = mp->m_driver;
13260Sstevel@tonic-gate 	}
13270Sstevel@tonic-gate 	ASSERT(refresh != NULL);
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate 	/*
13300Sstevel@tonic-gate 	 * Call the refresh function with the current unicast address.
13310Sstevel@tonic-gate 	 */
13320Sstevel@tonic-gate 	refresh(arg, mip->mi_addr);
13330Sstevel@tonic-gate }
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate void
13360Sstevel@tonic-gate mac_promisc_refresh(mac_t *mp, mac_promisc_t refresh, void *arg)
13370Sstevel@tonic-gate {
13380Sstevel@tonic-gate 	mac_impl_t	*mip = mp->m_impl;
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate 	/*
13410Sstevel@tonic-gate 	 * If no specific refresh function was given then default to the
13420Sstevel@tonic-gate 	 * driver's m_promisc entry point.
13430Sstevel@tonic-gate 	 */
13440Sstevel@tonic-gate 	if (refresh == NULL) {
13450Sstevel@tonic-gate 		refresh = mp->m_promisc;
13460Sstevel@tonic-gate 		arg = mp->m_driver;
13470Sstevel@tonic-gate 	}
13480Sstevel@tonic-gate 	ASSERT(refresh != NULL);
13490Sstevel@tonic-gate 
13500Sstevel@tonic-gate 	/*
13510Sstevel@tonic-gate 	 * Call the refresh function with the current promiscuity.
13520Sstevel@tonic-gate 	 */
13530Sstevel@tonic-gate 	refresh(arg, (mip->mi_devpromisc != 0));
13540Sstevel@tonic-gate }
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate boolean_t
13570Sstevel@tonic-gate mac_active_set(mac_handle_t mh)
13580Sstevel@tonic-gate {
13590Sstevel@tonic-gate 	mac_impl_t *mip = (mac_impl_t *)mh;
13600Sstevel@tonic-gate 
13610Sstevel@tonic-gate 	mutex_enter(&mip->mi_activelink_lock);
13620Sstevel@tonic-gate 	if (mip->mi_activelink) {
13630Sstevel@tonic-gate 		mutex_exit(&mip->mi_activelink_lock);
13640Sstevel@tonic-gate 		return (B_FALSE);
13650Sstevel@tonic-gate 	}
13660Sstevel@tonic-gate 	mip->mi_activelink = B_TRUE;
13670Sstevel@tonic-gate 	mutex_exit(&mip->mi_activelink_lock);
13680Sstevel@tonic-gate 	return (B_TRUE);
13690Sstevel@tonic-gate }
13700Sstevel@tonic-gate 
13710Sstevel@tonic-gate void
13720Sstevel@tonic-gate mac_active_clear(mac_handle_t mh)
13730Sstevel@tonic-gate {
13740Sstevel@tonic-gate 	mac_impl_t *mip = (mac_impl_t *)mh;
13750Sstevel@tonic-gate 
13760Sstevel@tonic-gate 	mutex_enter(&mip->mi_activelink_lock);
13770Sstevel@tonic-gate 	ASSERT(mip->mi_activelink);
13780Sstevel@tonic-gate 	mip->mi_activelink = B_FALSE;
13790Sstevel@tonic-gate 	mutex_exit(&mip->mi_activelink_lock);
13800Sstevel@tonic-gate }
1381