xref: /onnv-gate/usr/src/uts/common/io/mac/mac.c (revision 269:7ed63f24aa15)
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>
40*269Sericheng #include <sys/modhash.h>
410Sstevel@tonic-gate #include <sys/mac.h>
420Sstevel@tonic-gate #include <sys/mac_impl.h>
43*269Sericheng #include <sys/dls.h>
44*269Sericheng #include <sys/dld.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #define	IMPL_HASHSZ	67	/* prime */
470Sstevel@tonic-gate 
480Sstevel@tonic-gate static kmem_cache_t	*i_mac_impl_cachep;
49*269Sericheng static mod_hash_t	*i_mac_impl_hash;
50*269Sericheng krwlock_t		i_mac_impl_lock;
51*269Sericheng uint_t			i_mac_impl_count;
520Sstevel@tonic-gate 
530Sstevel@tonic-gate /*
540Sstevel@tonic-gate  * Private functions.
550Sstevel@tonic-gate  */
560Sstevel@tonic-gate 
570Sstevel@tonic-gate /*ARGSUSED*/
580Sstevel@tonic-gate static boolean_t
590Sstevel@tonic-gate i_mac_ether_unicst_verify(mac_impl_t *mip, const uint8_t *addr)
600Sstevel@tonic-gate {
610Sstevel@tonic-gate 	/*
620Sstevel@tonic-gate 	 * Check the address is not a group address.
630Sstevel@tonic-gate 	 */
640Sstevel@tonic-gate 	if (addr[0] & 0x01)
650Sstevel@tonic-gate 		return (B_FALSE);
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	return (B_TRUE);
680Sstevel@tonic-gate }
690Sstevel@tonic-gate 
700Sstevel@tonic-gate static boolean_t
710Sstevel@tonic-gate i_mac_ether_multicst_verify(mac_impl_t *mip, const uint8_t *addr)
720Sstevel@tonic-gate {
730Sstevel@tonic-gate 	mac_t		*mp = mip->mi_mp;
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	/*
760Sstevel@tonic-gate 	 * Check the address is a group address.
770Sstevel@tonic-gate 	 */
780Sstevel@tonic-gate 	if (!(addr[0] & 0x01))
790Sstevel@tonic-gate 		return (B_FALSE);
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	/*
820Sstevel@tonic-gate 	 * Check the address is not the media broadcast address.
830Sstevel@tonic-gate 	 */
840Sstevel@tonic-gate 	if (bcmp(addr, mp->m_info.mi_brdcst_addr, mip->mi_addr_length) == 0)
850Sstevel@tonic-gate 		return (B_FALSE);
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	return (B_TRUE);
880Sstevel@tonic-gate }
890Sstevel@tonic-gate 
900Sstevel@tonic-gate /*ARGSUSED*/
910Sstevel@tonic-gate static int
920Sstevel@tonic-gate i_mac_constructor(void *buf, void *arg, int kmflag)
930Sstevel@tonic-gate {
940Sstevel@tonic-gate 	mac_impl_t	*mip = buf;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	bzero(buf, sizeof (mac_impl_t));
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	mip->mi_link = LINK_STATE_UNKNOWN;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	rw_init(&mip->mi_state_lock, NULL, RW_DRIVER, NULL);
1010Sstevel@tonic-gate 	rw_init(&mip->mi_data_lock, NULL, RW_DRIVER, NULL);
1020Sstevel@tonic-gate 	rw_init(&mip->mi_notify_lock, NULL, RW_DRIVER, NULL);
1030Sstevel@tonic-gate 	rw_init(&mip->mi_rx_lock, NULL, RW_DRIVER, NULL);
1040Sstevel@tonic-gate 	rw_init(&mip->mi_txloop_lock, NULL, RW_DRIVER, NULL);
1050Sstevel@tonic-gate 	rw_init(&mip->mi_resource_lock, NULL, RW_DRIVER, NULL);
1060Sstevel@tonic-gate 	mutex_init(&mip->mi_activelink_lock, NULL, MUTEX_DEFAULT, NULL);
1070Sstevel@tonic-gate 	return (0);
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate /*ARGSUSED*/
1110Sstevel@tonic-gate static void
1120Sstevel@tonic-gate i_mac_destructor(void *buf, void *arg)
1130Sstevel@tonic-gate {
1140Sstevel@tonic-gate 	mac_impl_t	*mip = buf;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	ASSERT(mip->mi_mp == NULL);
1170Sstevel@tonic-gate 	ASSERT(mip->mi_ref == 0);
1180Sstevel@tonic-gate 	ASSERT(mip->mi_active == 0);
1190Sstevel@tonic-gate 	ASSERT(mip->mi_link == LINK_STATE_UNKNOWN);
1200Sstevel@tonic-gate 	ASSERT(mip->mi_devpromisc == 0);
1210Sstevel@tonic-gate 	ASSERT(mip->mi_promisc == 0);
1220Sstevel@tonic-gate 	ASSERT(mip->mi_mmap == NULL);
1230Sstevel@tonic-gate 	ASSERT(mip->mi_mnfp == NULL);
1240Sstevel@tonic-gate 	ASSERT(mip->mi_resource_add == NULL);
1250Sstevel@tonic-gate 	ASSERT(mip->mi_ksp == NULL);
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	rw_destroy(&mip->mi_state_lock);
1280Sstevel@tonic-gate 	rw_destroy(&mip->mi_data_lock);
1290Sstevel@tonic-gate 	rw_destroy(&mip->mi_notify_lock);
1300Sstevel@tonic-gate 	rw_destroy(&mip->mi_rx_lock);
1310Sstevel@tonic-gate 	rw_destroy(&mip->mi_txloop_lock);
1320Sstevel@tonic-gate 	rw_destroy(&mip->mi_resource_lock);
1330Sstevel@tonic-gate 	mutex_destroy(&mip->mi_activelink_lock);
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate 
136*269Sericheng static int
1370Sstevel@tonic-gate i_mac_create(mac_t *mp)
1380Sstevel@tonic-gate {
1390Sstevel@tonic-gate 	dev_info_t	*dip;
1400Sstevel@tonic-gate 	mac_impl_t	*mip;
141*269Sericheng 	int		err = 0;
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	dip = mp->m_dip;
1440Sstevel@tonic-gate 	ASSERT(dip != NULL);
1450Sstevel@tonic-gate 	ASSERT(ddi_get_instance(dip) >= 0);
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	/*
1480Sstevel@tonic-gate 	 * Allocate a new mac_impl_t.
1490Sstevel@tonic-gate 	 */
1500Sstevel@tonic-gate 	mip = kmem_cache_alloc(i_mac_impl_cachep, KM_SLEEP);
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	/*
1530Sstevel@tonic-gate 	 * Construct a name.
1540Sstevel@tonic-gate 	 */
1550Sstevel@tonic-gate 	(void) snprintf(mip->mi_dev, MAXNAMELEN - 1, "%s%d",
1560Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip));
1570Sstevel@tonic-gate 	mip->mi_port = mp->m_port;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	MAC_NAME(mip->mi_name, mip->mi_dev, mip->mi_port);
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	/*
1620Sstevel@tonic-gate 	 * Set the mac_t/mac_impl_t cross-references.
1630Sstevel@tonic-gate 	 */
1640Sstevel@tonic-gate 	mip->mi_mp = mp;
1650Sstevel@tonic-gate 	mp->m_impl = (void *)mip;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	/*
1680Sstevel@tonic-gate 	 * Insert the hash table entry.
1690Sstevel@tonic-gate 	 */
170*269Sericheng 	rw_enter(&i_mac_impl_lock, RW_WRITER);
171*269Sericheng 	if (mod_hash_insert(i_mac_impl_hash,
172*269Sericheng 	    (mod_hash_key_t)mip->mi_name, (mod_hash_val_t)mip) != 0) {
1730Sstevel@tonic-gate 		kmem_cache_free(i_mac_impl_cachep, mip);
174*269Sericheng 		err = EEXIST;
1750Sstevel@tonic-gate 		goto done;
1760Sstevel@tonic-gate 	}
177*269Sericheng 	i_mac_impl_count++;
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	/*
1800Sstevel@tonic-gate 	 * Copy the fixed 'factory' MAC address from the immutable info.
1810Sstevel@tonic-gate 	 * This is taken to be the MAC address currently in use.
1820Sstevel@tonic-gate 	 */
1830Sstevel@tonic-gate 	mip->mi_addr_length = mp->m_info.mi_addr_length;
1840Sstevel@tonic-gate 	bcopy(mp->m_info.mi_unicst_addr, mip->mi_addr, mip->mi_addr_length);
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	/*
1870Sstevel@tonic-gate 	 * Set up the address verification functions.
1880Sstevel@tonic-gate 	 */
1890Sstevel@tonic-gate 	ASSERT(mp->m_info.mi_media == DL_ETHER);
1900Sstevel@tonic-gate 	mip->mi_unicst_verify = i_mac_ether_unicst_verify;
1910Sstevel@tonic-gate 	mip->mi_multicst_verify = i_mac_ether_multicst_verify;
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	/*
19456Smeem 	 * Set up the two possible transmit routines.
19556Smeem 	 */
19656Smeem 	mip->mi_txinfo.mt_fn = mp->m_tx;
19756Smeem 	mip->mi_txinfo.mt_arg = mp->m_driver;
19856Smeem 	mip->mi_txloopinfo.mt_fn = mac_txloop;
19956Smeem 	mip->mi_txloopinfo.mt_arg = mip;
20056Smeem 
20156Smeem 	/*
2020Sstevel@tonic-gate 	 * Initialize the kstats for this device.
2030Sstevel@tonic-gate 	 */
2040Sstevel@tonic-gate 	mac_stat_create(mip);
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate done:
207*269Sericheng 	rw_exit(&i_mac_impl_lock);
2080Sstevel@tonic-gate 	return (err);
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate static void
2120Sstevel@tonic-gate i_mac_destroy(mac_t *mp)
2130Sstevel@tonic-gate {
2140Sstevel@tonic-gate 	mac_impl_t		*mip = mp->m_impl;
2150Sstevel@tonic-gate 	mac_multicst_addr_t	*p, *nextp;
216*269Sericheng 	mod_hash_val_t		val;
2170Sstevel@tonic-gate 
218*269Sericheng 	rw_enter(&i_mac_impl_lock, RW_WRITER);
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	ASSERT(mip->mi_ref == 0);
2210Sstevel@tonic-gate 	ASSERT(!mip->mi_activelink);
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	/*
2240Sstevel@tonic-gate 	 * Destroy the kstats.
2250Sstevel@tonic-gate 	 */
2260Sstevel@tonic-gate 	mac_stat_destroy(mip);
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	/*
2290Sstevel@tonic-gate 	 * Remove and destroy the hash table entry.
2300Sstevel@tonic-gate 	 */
231*269Sericheng 	(void) mod_hash_remove(i_mac_impl_hash,
232*269Sericheng 	    (mod_hash_key_t)mip->mi_name, &val);
233*269Sericheng 	ASSERT(mip == (mac_impl_t *)val);
234*269Sericheng 
235*269Sericheng 	ASSERT(i_mac_impl_count > 0);
236*269Sericheng 	i_mac_impl_count--;
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	/*
2390Sstevel@tonic-gate 	 * Free the list of multicast addresses.
2400Sstevel@tonic-gate 	 */
2410Sstevel@tonic-gate 	for (p = mip->mi_mmap; p != NULL; p = nextp) {
2420Sstevel@tonic-gate 		nextp = p->mma_nextp;
2430Sstevel@tonic-gate 		kmem_free(p, sizeof (mac_multicst_addr_t));
2440Sstevel@tonic-gate 	}
2450Sstevel@tonic-gate 	mip->mi_mmap = NULL;
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	/*
2480Sstevel@tonic-gate 	 * Clean up the mac_impl_t ready to go back into the cache.
2490Sstevel@tonic-gate 	 */
2500Sstevel@tonic-gate 	mp->m_impl = NULL;
2510Sstevel@tonic-gate 	mip->mi_mp = NULL;
2520Sstevel@tonic-gate 	mip->mi_link = LINK_STATE_UNKNOWN;
2530Sstevel@tonic-gate 	mip->mi_destroying = B_FALSE;
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	/*
2560Sstevel@tonic-gate 	 * Free the structure back to the cache.
2570Sstevel@tonic-gate 	 */
2580Sstevel@tonic-gate 	kmem_cache_free(i_mac_impl_cachep, mip);
2590Sstevel@tonic-gate 
260*269Sericheng 	rw_exit(&i_mac_impl_lock);
2610Sstevel@tonic-gate }
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate static void
2640Sstevel@tonic-gate i_mac_notify(mac_impl_t *mip, mac_notify_type_t type)
2650Sstevel@tonic-gate {
2660Sstevel@tonic-gate 	mac_notify_fn_t		*mnfp;
2670Sstevel@tonic-gate 	mac_notify_t		notify;
2680Sstevel@tonic-gate 	void			*arg;
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	/*
2710Sstevel@tonic-gate 	 * Walk the list of notifications.
2720Sstevel@tonic-gate 	 */
2730Sstevel@tonic-gate 	rw_enter(&(mip->mi_notify_lock), RW_READER);
2740Sstevel@tonic-gate 	for (mnfp = mip->mi_mnfp; mnfp != NULL; mnfp = mnfp->mnf_nextp) {
2750Sstevel@tonic-gate 		notify = mnfp->mnf_fn;
2760Sstevel@tonic-gate 		arg = mnfp->mnf_arg;
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 		ASSERT(notify != NULL);
2790Sstevel@tonic-gate 		notify(arg, type);
2800Sstevel@tonic-gate 	}
2810Sstevel@tonic-gate 	rw_exit(&(mip->mi_notify_lock));
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate /*
2850Sstevel@tonic-gate  * Module initialization functions.
2860Sstevel@tonic-gate  */
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate void
2890Sstevel@tonic-gate mac_init(void)
2900Sstevel@tonic-gate {
2910Sstevel@tonic-gate 	i_mac_impl_cachep = kmem_cache_create("mac_impl_cache",
2920Sstevel@tonic-gate 	    sizeof (mac_impl_t), 0, i_mac_constructor, i_mac_destructor, NULL,
2930Sstevel@tonic-gate 	    NULL, NULL, 0);
2940Sstevel@tonic-gate 	ASSERT(i_mac_impl_cachep != NULL);
2950Sstevel@tonic-gate 
296*269Sericheng 	i_mac_impl_hash = mod_hash_create_extended("mac_impl_hash",
297*269Sericheng 	    IMPL_HASHSZ, mod_hash_null_keydtor, mod_hash_null_valdtor,
298*269Sericheng 	    mod_hash_bystr, NULL, mod_hash_strkey_cmp, KM_SLEEP);
299*269Sericheng 	rw_init(&i_mac_impl_lock, NULL, RW_DEFAULT, NULL);
300*269Sericheng 	i_mac_impl_count = 0;
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate int
3040Sstevel@tonic-gate mac_fini(void)
3050Sstevel@tonic-gate {
306*269Sericheng 	if (i_mac_impl_count > 0)
307*269Sericheng 		return (EBUSY);
3080Sstevel@tonic-gate 
309*269Sericheng 	mod_hash_destroy_hash(i_mac_impl_hash);
310*269Sericheng 	rw_destroy(&i_mac_impl_lock);
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	kmem_cache_destroy(i_mac_impl_cachep);
3130Sstevel@tonic-gate 	return (0);
3140Sstevel@tonic-gate }
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate /*
3170Sstevel@tonic-gate  * Client functions.
3180Sstevel@tonic-gate  */
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate int
3210Sstevel@tonic-gate mac_open(const char *dev, uint_t port, mac_handle_t *mhp)
3220Sstevel@tonic-gate {
3230Sstevel@tonic-gate 	char		name[MAXNAMELEN];
3240Sstevel@tonic-gate 	char		driver[MAXNAMELEN];
3250Sstevel@tonic-gate 	uint_t		instance;
3260Sstevel@tonic-gate 	major_t		major;
3270Sstevel@tonic-gate 	dev_info_t	*dip;
3280Sstevel@tonic-gate 	mac_impl_t	*mip;
3290Sstevel@tonic-gate 	int		err;
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	/*
3320Sstevel@tonic-gate 	 * Check the device name length to make sure it won't overflow our
3330Sstevel@tonic-gate 	 * buffer.
3340Sstevel@tonic-gate 	 */
3350Sstevel@tonic-gate 	if (strlen(dev) >= MAXNAMELEN)
3360Sstevel@tonic-gate 		return (EINVAL);
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	/*
3390Sstevel@tonic-gate 	 * Split the device name into driver and instance components.
3400Sstevel@tonic-gate 	 */
3410Sstevel@tonic-gate 	if (ddi_parse(dev, driver, &instance) != DDI_SUCCESS)
3420Sstevel@tonic-gate 		return (EINVAL);
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	/*
3450Sstevel@tonic-gate 	 * Get the major number of the driver.
3460Sstevel@tonic-gate 	 */
3470Sstevel@tonic-gate 	if ((major = ddi_name_to_major(driver)) == (major_t)-1)
3480Sstevel@tonic-gate 		return (EINVAL);
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	/*
3510Sstevel@tonic-gate 	 * Hold the given instance to prevent it from being detached.
352*269Sericheng 	 * This will also attach the instance if it is not currently attached.
353*269Sericheng 	 * Currently we ensure that mac_register() (called by the driver's
354*269Sericheng 	 * attach entry point) and all code paths under it cannot possibly
355*269Sericheng 	 * call mac_open() because this would lead to a recursive attach
356*269Sericheng 	 * panic.
3570Sstevel@tonic-gate 	 */
3580Sstevel@tonic-gate 	if ((dip = ddi_hold_devi_by_instance(major, instance, 0)) == NULL)
3590Sstevel@tonic-gate 		return (EINVAL);
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	/*
3620Sstevel@tonic-gate 	 * Construct the name of the MAC interface.
3630Sstevel@tonic-gate 	 */
3640Sstevel@tonic-gate 	MAC_NAME(name, dev, port);
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	/*
3670Sstevel@tonic-gate 	 * Look up its entry in the global hash table.
3680Sstevel@tonic-gate 	 */
3690Sstevel@tonic-gate again:
370*269Sericheng 	rw_enter(&i_mac_impl_lock, RW_WRITER);
371*269Sericheng 	err = mod_hash_find(i_mac_impl_hash, (mod_hash_key_t)name,
372*269Sericheng 	    (mod_hash_val_t *)&mip);
373*269Sericheng 	if (err != 0) {
374*269Sericheng 		err = ENOENT;
3750Sstevel@tonic-gate 		goto failed;
376*269Sericheng 	}
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	if (mip->mi_destroying) {
379*269Sericheng 		rw_exit(&i_mac_impl_lock);
3800Sstevel@tonic-gate 		goto again;
3810Sstevel@tonic-gate 	}
3820Sstevel@tonic-gate 
383*269Sericheng 	/*
384*269Sericheng 	 * We currently only support the DL_ETHER media type.
385*269Sericheng 	 */
386*269Sericheng 	ASSERT(mip->mi_mp->m_info.mi_media == DL_ETHER);
3870Sstevel@tonic-gate 	mip->mi_ref++;
388*269Sericheng 	rw_exit(&i_mac_impl_lock);
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	*mhp = (mac_handle_t)mip;
3910Sstevel@tonic-gate 	return (0);
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate failed:
394*269Sericheng 	rw_exit(&i_mac_impl_lock);
3950Sstevel@tonic-gate 	ddi_release_devi(dip);
3960Sstevel@tonic-gate 	return (err);
3970Sstevel@tonic-gate }
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate void
4000Sstevel@tonic-gate mac_close(mac_handle_t mh)
4010Sstevel@tonic-gate {
4020Sstevel@tonic-gate 	mac_impl_t	*mip = (mac_impl_t *)mh;
4030Sstevel@tonic-gate 	dev_info_t	*dip = mip->mi_mp->m_dip;
4040Sstevel@tonic-gate 
405*269Sericheng 	rw_enter(&i_mac_impl_lock, RW_WRITER);
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	ASSERT(mip->mi_ref != 0);
4080Sstevel@tonic-gate 	if (--mip->mi_ref == 0) {
4090Sstevel@tonic-gate 		ASSERT(!mip->mi_activelink);
4100Sstevel@tonic-gate 	}
4110Sstevel@tonic-gate 	ddi_release_devi(dip);
412*269Sericheng 	rw_exit(&i_mac_impl_lock);
4130Sstevel@tonic-gate }
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate const mac_info_t *
4160Sstevel@tonic-gate mac_info(mac_handle_t mh)
4170Sstevel@tonic-gate {
4180Sstevel@tonic-gate 	mac_impl_t	*mip = (mac_impl_t *)mh;
4190Sstevel@tonic-gate 	mac_t		*mp = mip->mi_mp;
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	/*
4220Sstevel@tonic-gate 	 * Return a pointer to the mac_info_t embedded in the mac_t.
4230Sstevel@tonic-gate 	 */
4240Sstevel@tonic-gate 	return (&(mp->m_info));
4250Sstevel@tonic-gate }
4260Sstevel@tonic-gate 
427*269Sericheng dev_info_t *
428*269Sericheng mac_devinfo_get(mac_handle_t mh)
429*269Sericheng {
430*269Sericheng 	return (((mac_impl_t *)mh)->mi_mp->m_dip);
431*269Sericheng }
432*269Sericheng 
4330Sstevel@tonic-gate uint64_t
4340Sstevel@tonic-gate mac_stat_get(mac_handle_t mh, enum mac_stat stat)
4350Sstevel@tonic-gate {
4360Sstevel@tonic-gate 	mac_impl_t	*mip = (mac_impl_t *)mh;
4370Sstevel@tonic-gate 	mac_t		*mp = mip->mi_mp;
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	ASSERT(mp->m_info.mi_stat[stat]);
4400Sstevel@tonic-gate 	ASSERT(mp->m_stat != NULL);
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	/*
4430Sstevel@tonic-gate 	 * Call the driver to get the given statistic.
4440Sstevel@tonic-gate 	 */
4450Sstevel@tonic-gate 	return (mp->m_stat(mp->m_driver, stat));
4460Sstevel@tonic-gate }
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate int
4490Sstevel@tonic-gate mac_start(mac_handle_t mh)
4500Sstevel@tonic-gate {
4510Sstevel@tonic-gate 	mac_impl_t	*mip = (mac_impl_t *)mh;
4520Sstevel@tonic-gate 	mac_t		*mp = mip->mi_mp;
4530Sstevel@tonic-gate 	int		err;
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	ASSERT(mp->m_start != NULL);
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	rw_enter(&(mip->mi_state_lock), RW_WRITER);
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	/*
4600Sstevel@tonic-gate 	 * Check whether the device is already started.
4610Sstevel@tonic-gate 	 */
4620Sstevel@tonic-gate 	if (mip->mi_active++ != 0) {
4630Sstevel@tonic-gate 		/*
4640Sstevel@tonic-gate 		 * It's already started so there's nothing more to do.
4650Sstevel@tonic-gate 		 */
4660Sstevel@tonic-gate 		err = 0;
4670Sstevel@tonic-gate 		goto done;
4680Sstevel@tonic-gate 	}
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	/*
4710Sstevel@tonic-gate 	 * Start the device.
4720Sstevel@tonic-gate 	 */
4730Sstevel@tonic-gate 	if ((err = mp->m_start(mp->m_driver)) != 0)
4740Sstevel@tonic-gate 		--mip->mi_active;
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate done:
4770Sstevel@tonic-gate 	rw_exit(&(mip->mi_state_lock));
4780Sstevel@tonic-gate 	return (err);
4790Sstevel@tonic-gate }
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate void
4820Sstevel@tonic-gate mac_stop(mac_handle_t mh)
4830Sstevel@tonic-gate {
4840Sstevel@tonic-gate 	mac_impl_t	*mip = (mac_impl_t *)mh;
4850Sstevel@tonic-gate 	mac_t		*mp = mip->mi_mp;
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	ASSERT(mp->m_stop != NULL);
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	rw_enter(&(mip->mi_state_lock), RW_WRITER);
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	/*
4920Sstevel@tonic-gate 	 * Check whether the device is still needed.
4930Sstevel@tonic-gate 	 */
4940Sstevel@tonic-gate 	ASSERT(mip->mi_active != 0);
4950Sstevel@tonic-gate 	if (--mip->mi_active != 0) {
4960Sstevel@tonic-gate 		/*
4970Sstevel@tonic-gate 		 * It's still needed so there's nothing more to do.
4980Sstevel@tonic-gate 		 */
4990Sstevel@tonic-gate 		goto done;
5000Sstevel@tonic-gate 	}
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	/*
5030Sstevel@tonic-gate 	 * Stop the device.
5040Sstevel@tonic-gate 	 */
5050Sstevel@tonic-gate 	mp->m_stop(mp->m_driver);
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate done:
5080Sstevel@tonic-gate 	rw_exit(&(mip->mi_state_lock));
5090Sstevel@tonic-gate }
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate int
5120Sstevel@tonic-gate mac_multicst_add(mac_handle_t mh, const uint8_t *addr)
5130Sstevel@tonic-gate {
5140Sstevel@tonic-gate 	mac_impl_t		*mip = (mac_impl_t *)mh;
5150Sstevel@tonic-gate 	mac_t			*mp = mip->mi_mp;
5160Sstevel@tonic-gate 	mac_multicst_addr_t	**pp;
5170Sstevel@tonic-gate 	mac_multicst_addr_t	*p;
5180Sstevel@tonic-gate 	int			err;
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	ASSERT(mp->m_multicst != NULL);
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	/*
5230Sstevel@tonic-gate 	 * Verify the address.
5240Sstevel@tonic-gate 	 */
5250Sstevel@tonic-gate 	if (!(mip->mi_multicst_verify(mip, addr)))
5260Sstevel@tonic-gate 		return (EINVAL);
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	/*
5290Sstevel@tonic-gate 	 * Check whether the given address is already enabled.
5300Sstevel@tonic-gate 	 */
5310Sstevel@tonic-gate 	rw_enter(&(mip->mi_data_lock), RW_WRITER);
5320Sstevel@tonic-gate 	for (pp = &(mip->mi_mmap); (p = *pp) != NULL; pp = &(p->mma_nextp)) {
5330Sstevel@tonic-gate 		if (bcmp(p->mma_addr, addr, mip->mi_addr_length) == 0) {
5340Sstevel@tonic-gate 			/*
5350Sstevel@tonic-gate 			 * The address is already enabled so just bump the
5360Sstevel@tonic-gate 			 * reference count.
5370Sstevel@tonic-gate 			 */
5380Sstevel@tonic-gate 			p->mma_ref++;
5390Sstevel@tonic-gate 			err = 0;
5400Sstevel@tonic-gate 			goto done;
5410Sstevel@tonic-gate 		}
5420Sstevel@tonic-gate 	}
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 	/*
5450Sstevel@tonic-gate 	 * Allocate a new list entry.
5460Sstevel@tonic-gate 	 */
5470Sstevel@tonic-gate 	if ((p = kmem_zalloc(sizeof (mac_multicst_addr_t),
5480Sstevel@tonic-gate 	    KM_NOSLEEP)) == NULL) {
5490Sstevel@tonic-gate 		err = ENOMEM;
5500Sstevel@tonic-gate 		goto done;
5510Sstevel@tonic-gate 	}
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 	/*
5540Sstevel@tonic-gate 	 * Enable a new multicast address.
5550Sstevel@tonic-gate 	 */
5560Sstevel@tonic-gate 	if ((err = mp->m_multicst(mp->m_driver, B_TRUE, addr)) != 0) {
5570Sstevel@tonic-gate 		kmem_free(p, sizeof (mac_multicst_addr_t));
5580Sstevel@tonic-gate 		goto done;
5590Sstevel@tonic-gate 	}
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 	/*
5620Sstevel@tonic-gate 	 * Add the address to the list of enabled addresses.
5630Sstevel@tonic-gate 	 */
5640Sstevel@tonic-gate 	bcopy(addr, p->mma_addr, mip->mi_addr_length);
5650Sstevel@tonic-gate 	p->mma_ref++;
5660Sstevel@tonic-gate 	*pp = p;
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate done:
5690Sstevel@tonic-gate 	rw_exit(&(mip->mi_data_lock));
5700Sstevel@tonic-gate 	return (err);
5710Sstevel@tonic-gate }
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate int
5740Sstevel@tonic-gate mac_multicst_remove(mac_handle_t mh, const uint8_t *addr)
5750Sstevel@tonic-gate {
5760Sstevel@tonic-gate 	mac_impl_t		*mip = (mac_impl_t *)mh;
5770Sstevel@tonic-gate 	mac_t			*mp = mip->mi_mp;
5780Sstevel@tonic-gate 	mac_multicst_addr_t	**pp;
5790Sstevel@tonic-gate 	mac_multicst_addr_t	*p;
5800Sstevel@tonic-gate 	int			err;
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 	ASSERT(mp->m_multicst != NULL);
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 	/*
5850Sstevel@tonic-gate 	 * Find the entry in the list for the given address.
5860Sstevel@tonic-gate 	 */
5870Sstevel@tonic-gate 	rw_enter(&(mip->mi_data_lock), RW_WRITER);
5880Sstevel@tonic-gate 	for (pp = &(mip->mi_mmap); (p = *pp) != NULL; pp = &(p->mma_nextp)) {
5890Sstevel@tonic-gate 		if (bcmp(p->mma_addr, addr, mip->mi_addr_length) == 0) {
5900Sstevel@tonic-gate 			if (--p->mma_ref == 0)
5910Sstevel@tonic-gate 				break;
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 			/*
5940Sstevel@tonic-gate 			 * There is still a reference to this address so
5950Sstevel@tonic-gate 			 * there's nothing more to do.
5960Sstevel@tonic-gate 			 */
5970Sstevel@tonic-gate 			err = 0;
5980Sstevel@tonic-gate 			goto done;
5990Sstevel@tonic-gate 		}
6000Sstevel@tonic-gate 	}
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	/*
6030Sstevel@tonic-gate 	 * We did not find an entry for the given address so it is not
6040Sstevel@tonic-gate 	 * currently enabled.
6050Sstevel@tonic-gate 	 */
6060Sstevel@tonic-gate 	if (p == NULL) {
6070Sstevel@tonic-gate 		err = ENOENT;
6080Sstevel@tonic-gate 		goto done;
6090Sstevel@tonic-gate 	}
6100Sstevel@tonic-gate 	ASSERT(p->mma_ref == 0);
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 	/*
6130Sstevel@tonic-gate 	 * Disable the multicast address.
6140Sstevel@tonic-gate 	 */
6150Sstevel@tonic-gate 	if ((err = mp->m_multicst(mp->m_driver, B_FALSE, addr)) != 0) {
6160Sstevel@tonic-gate 		p->mma_ref++;
6170Sstevel@tonic-gate 		goto done;
6180Sstevel@tonic-gate 	}
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	/*
6210Sstevel@tonic-gate 	 * Remove it from the list.
6220Sstevel@tonic-gate 	 */
6230Sstevel@tonic-gate 	*pp = p->mma_nextp;
6240Sstevel@tonic-gate 	kmem_free(p, sizeof (mac_multicst_addr_t));
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate done:
6270Sstevel@tonic-gate 	rw_exit(&(mip->mi_data_lock));
6280Sstevel@tonic-gate 	return (err);
6290Sstevel@tonic-gate }
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate int
6320Sstevel@tonic-gate mac_unicst_set(mac_handle_t mh, const uint8_t *addr)
6330Sstevel@tonic-gate {
6340Sstevel@tonic-gate 	mac_impl_t	*mip = (mac_impl_t *)mh;
6350Sstevel@tonic-gate 	mac_t		*mp = mip->mi_mp;
6360Sstevel@tonic-gate 	int		err;
6370Sstevel@tonic-gate 	boolean_t	notify = B_FALSE;
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 	ASSERT(mp->m_unicst != NULL);
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	/*
6420Sstevel@tonic-gate 	 * Verify the address.
6430Sstevel@tonic-gate 	 */
6440Sstevel@tonic-gate 	if (!(mip->mi_unicst_verify(mip, addr)))
6450Sstevel@tonic-gate 		return (EINVAL);
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	/*
6480Sstevel@tonic-gate 	 * Program the new unicast address.
6490Sstevel@tonic-gate 	 */
6500Sstevel@tonic-gate 	rw_enter(&(mip->mi_data_lock), RW_WRITER);
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 	/*
6530Sstevel@tonic-gate 	 * If address doesn't change, do nothing.
6540Sstevel@tonic-gate 	 * This check is necessary otherwise it may call into mac_unicst_set
6550Sstevel@tonic-gate 	 * recursively.
6560Sstevel@tonic-gate 	 */
6570Sstevel@tonic-gate 	if (bcmp(addr, mip->mi_addr, mip->mi_addr_length) == 0) {
6580Sstevel@tonic-gate 		err = 0;
6590Sstevel@tonic-gate 		goto done;
6600Sstevel@tonic-gate 	}
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 	if ((err = mp->m_unicst(mp->m_driver, addr)) != 0)
6630Sstevel@tonic-gate 		goto done;
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 	/*
6660Sstevel@tonic-gate 	 * Save the address and flag that we need to send a notification.
6670Sstevel@tonic-gate 	 */
6680Sstevel@tonic-gate 	bcopy(addr, mip->mi_addr, mip->mi_addr_length);
6690Sstevel@tonic-gate 	notify = B_TRUE;
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate done:
6720Sstevel@tonic-gate 	rw_exit(&(mip->mi_data_lock));
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 	if (notify)
6750Sstevel@tonic-gate 		i_mac_notify(mip, MAC_NOTE_UNICST);
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 	return (err);
6780Sstevel@tonic-gate }
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate void
6810Sstevel@tonic-gate mac_unicst_get(mac_handle_t mh, uint8_t *addr)
6820Sstevel@tonic-gate {
6830Sstevel@tonic-gate 	mac_impl_t	*mip = (mac_impl_t *)mh;
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 	/*
6860Sstevel@tonic-gate 	 * Copy out the current unicast address.
6870Sstevel@tonic-gate 	 */
6880Sstevel@tonic-gate 	rw_enter(&(mip->mi_data_lock), RW_READER);
6890Sstevel@tonic-gate 	bcopy(mip->mi_addr, addr, mip->mi_addr_length);
6900Sstevel@tonic-gate 	rw_exit(&(mip->mi_data_lock));
6910Sstevel@tonic-gate }
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate int
6940Sstevel@tonic-gate mac_promisc_set(mac_handle_t mh, boolean_t on, mac_promisc_type_t ptype)
6950Sstevel@tonic-gate {
6960Sstevel@tonic-gate 	mac_impl_t	*mip = (mac_impl_t *)mh;
6970Sstevel@tonic-gate 	mac_t		*mp = mip->mi_mp;
6980Sstevel@tonic-gate 	int		err = 0;
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	ASSERT(mp->m_promisc != NULL);
7010Sstevel@tonic-gate 	ASSERT(ptype == MAC_DEVPROMISC || ptype == MAC_PROMISC);
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	/*
7040Sstevel@tonic-gate 	 * Determine whether we should enable or disable promiscuous mode.
7050Sstevel@tonic-gate 	 * For details on the distinction between "device promiscuous mode"
7060Sstevel@tonic-gate 	 * and "MAC promiscuous mode", see PSARC/2005/289.
7070Sstevel@tonic-gate 	 */
7080Sstevel@tonic-gate 	rw_enter(&(mip->mi_data_lock), RW_WRITER);
7090Sstevel@tonic-gate 	if (on) {
7100Sstevel@tonic-gate 		/*
7110Sstevel@tonic-gate 		 * Enable promiscuous mode on the device if not yet enabled.
7120Sstevel@tonic-gate 		 */
7130Sstevel@tonic-gate 		if (mip->mi_devpromisc++ == 0) {
7140Sstevel@tonic-gate 			if ((err = mp->m_promisc(mp->m_driver, B_TRUE)) != 0) {
7150Sstevel@tonic-gate 				mip->mi_devpromisc--;
7160Sstevel@tonic-gate 				goto done;
7170Sstevel@tonic-gate 			}
7180Sstevel@tonic-gate 			i_mac_notify(mip, MAC_NOTE_DEVPROMISC);
7190Sstevel@tonic-gate 		}
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 		/*
7220Sstevel@tonic-gate 		 * Enable promiscuous mode on the MAC if not yet enabled.
7230Sstevel@tonic-gate 		 */
7240Sstevel@tonic-gate 		if (ptype == MAC_PROMISC && mip->mi_promisc++ == 0)
7250Sstevel@tonic-gate 			i_mac_notify(mip, MAC_NOTE_PROMISC);
7260Sstevel@tonic-gate 	} else {
7270Sstevel@tonic-gate 		if (mip->mi_devpromisc == 0) {
7280Sstevel@tonic-gate 			err = EPROTO;
7290Sstevel@tonic-gate 			goto done;
7300Sstevel@tonic-gate 		}
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 		/*
7330Sstevel@tonic-gate 		 * Disable promiscuous mode on the device if this is the last
7340Sstevel@tonic-gate 		 * enabling.
7350Sstevel@tonic-gate 		 */
7360Sstevel@tonic-gate 		if (--mip->mi_devpromisc == 0) {
7370Sstevel@tonic-gate 			if ((err = mp->m_promisc(mp->m_driver, B_FALSE)) != 0) {
7380Sstevel@tonic-gate 				mip->mi_devpromisc++;
7390Sstevel@tonic-gate 				goto done;
7400Sstevel@tonic-gate 			}
7410Sstevel@tonic-gate 			i_mac_notify(mip, MAC_NOTE_DEVPROMISC);
7420Sstevel@tonic-gate 		}
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 		/*
7450Sstevel@tonic-gate 		 * Disable promiscuous mode on the MAC if this is the last
7460Sstevel@tonic-gate 		 * enabling.
7470Sstevel@tonic-gate 		 */
7480Sstevel@tonic-gate 		if (ptype == MAC_PROMISC && --mip->mi_promisc == 0)
7490Sstevel@tonic-gate 			i_mac_notify(mip, MAC_NOTE_PROMISC);
7500Sstevel@tonic-gate 	}
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate done:
7530Sstevel@tonic-gate 	rw_exit(&(mip->mi_data_lock));
7540Sstevel@tonic-gate 	return (err);
7550Sstevel@tonic-gate }
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate boolean_t
7580Sstevel@tonic-gate mac_promisc_get(mac_handle_t mh, mac_promisc_type_t ptype)
7590Sstevel@tonic-gate {
7600Sstevel@tonic-gate 	mac_impl_t		*mip = (mac_impl_t *)mh;
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 	ASSERT(ptype == MAC_DEVPROMISC || ptype == MAC_PROMISC);
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	/*
7650Sstevel@tonic-gate 	 * Return the current promiscuity.
7660Sstevel@tonic-gate 	 */
7670Sstevel@tonic-gate 	if (ptype == MAC_DEVPROMISC)
7680Sstevel@tonic-gate 		return (mip->mi_devpromisc != 0);
7690Sstevel@tonic-gate 	else
7700Sstevel@tonic-gate 		return (mip->mi_promisc != 0);
7710Sstevel@tonic-gate }
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate void
7740Sstevel@tonic-gate mac_resources(mac_handle_t mh)
7750Sstevel@tonic-gate {
7760Sstevel@tonic-gate 	mac_impl_t	*mip = (mac_impl_t *)mh;
7770Sstevel@tonic-gate 	mac_t		*mp = mip->mi_mp;
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 	ASSERT(mp->m_resources != NULL);
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	/*
7820Sstevel@tonic-gate 	 * Call the driver to register its resources.
7830Sstevel@tonic-gate 	 */
7840Sstevel@tonic-gate 	mp->m_resources(mp->m_driver);
7850Sstevel@tonic-gate }
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate void
7880Sstevel@tonic-gate mac_ioctl(mac_handle_t mh, queue_t *wq, mblk_t *bp)
7890Sstevel@tonic-gate {
7900Sstevel@tonic-gate 	mac_impl_t	*mip = (mac_impl_t *)mh;
7910Sstevel@tonic-gate 	mac_t		*mp = mip->mi_mp;
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	ASSERT(mp->m_ioctl != NULL);
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	/*
7960Sstevel@tonic-gate 	 * Call the driver to handle the ioctl.
7970Sstevel@tonic-gate 	 */
7980Sstevel@tonic-gate 	mp->m_ioctl(mp->m_driver, wq, bp);
7990Sstevel@tonic-gate }
8000Sstevel@tonic-gate 
80156Smeem const mac_txinfo_t *
80256Smeem mac_tx_get(mac_handle_t mh)
8030Sstevel@tonic-gate {
8040Sstevel@tonic-gate 	mac_impl_t	*mip = (mac_impl_t *)mh;
80556Smeem 	mac_txinfo_t	*mtp;
80656Smeem 
80756Smeem 	/*
80856Smeem 	 * Grab the lock to prevent us from racing with MAC_PROMISC being
80956Smeem 	 * changed.  This is sufficient since MAC clients are careful to always
81056Smeem 	 * call mac_txloop_add() prior to enabling MAC_PROMISC, and to disable
81156Smeem 	 * MAC_PROMISC prior to calling mac_txloop_remove().
81256Smeem 	 */
81356Smeem 	rw_enter(&mip->mi_txloop_lock, RW_READER);
8140Sstevel@tonic-gate 
81556Smeem 	if (mac_promisc_get(mh, MAC_PROMISC)) {
81656Smeem 		ASSERT(mip->mi_mtfp != NULL);
81756Smeem 		mtp = &mip->mi_txloopinfo;
81856Smeem 	} else {
81956Smeem 		/*
82056Smeem 		 * Note that we cannot ASSERT() that mip->mi_mtfp is NULL,
82156Smeem 		 * because to satisfy the above ASSERT(), we have to disable
82256Smeem 		 * MAC_PROMISC prior to calling mac_txloop_remove().
82356Smeem 		 */
82456Smeem 		mtp = &mip->mi_txinfo;
8250Sstevel@tonic-gate 
82656Smeem 	}
82756Smeem 
82856Smeem 	rw_exit(&mip->mi_txloop_lock);
82956Smeem 	return (mtp);
8300Sstevel@tonic-gate }
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate link_state_t
8330Sstevel@tonic-gate mac_link_get(mac_handle_t mh)
8340Sstevel@tonic-gate {
8350Sstevel@tonic-gate 	mac_impl_t	*mip = (mac_impl_t *)mh;
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 	/*
8380Sstevel@tonic-gate 	 * Return the current link state.
8390Sstevel@tonic-gate 	 */
8400Sstevel@tonic-gate 	return (mip->mi_link);
8410Sstevel@tonic-gate }
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate mac_notify_handle_t
8440Sstevel@tonic-gate mac_notify_add(mac_handle_t mh, mac_notify_t notify, void *arg)
8450Sstevel@tonic-gate {
8460Sstevel@tonic-gate 	mac_impl_t		*mip = (mac_impl_t *)mh;
8470Sstevel@tonic-gate 	mac_notify_fn_t		*mnfp;
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 	mnfp = kmem_zalloc(sizeof (mac_notify_fn_t), KM_SLEEP);
8500Sstevel@tonic-gate 	mnfp->mnf_fn = notify;
8510Sstevel@tonic-gate 	mnfp->mnf_arg = arg;
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 	/*
8540Sstevel@tonic-gate 	 * Add it to the head of the 'notify' callback list.
8550Sstevel@tonic-gate 	 */
8560Sstevel@tonic-gate 	rw_enter(&(mip->mi_notify_lock), RW_WRITER);
8570Sstevel@tonic-gate 	mnfp->mnf_nextp = mip->mi_mnfp;
8580Sstevel@tonic-gate 	mip->mi_mnfp = mnfp;
8590Sstevel@tonic-gate 	rw_exit(&(mip->mi_notify_lock));
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 	return ((mac_notify_handle_t)mnfp);
8620Sstevel@tonic-gate }
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate void
8650Sstevel@tonic-gate mac_notify_remove(mac_handle_t mh, mac_notify_handle_t mnh)
8660Sstevel@tonic-gate {
8670Sstevel@tonic-gate 	mac_impl_t		*mip = (mac_impl_t *)mh;
8680Sstevel@tonic-gate 	mac_notify_fn_t		*mnfp = (mac_notify_fn_t *)mnh;
8690Sstevel@tonic-gate 	mac_notify_fn_t		**pp;
8700Sstevel@tonic-gate 	mac_notify_fn_t		*p;
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 	/*
8730Sstevel@tonic-gate 	 * Search the 'notify' callback list for the function closure.
8740Sstevel@tonic-gate 	 */
8750Sstevel@tonic-gate 	rw_enter(&(mip->mi_notify_lock), RW_WRITER);
8760Sstevel@tonic-gate 	for (pp = &(mip->mi_mnfp); (p = *pp) != NULL;
8770Sstevel@tonic-gate 	    pp = &(p->mnf_nextp)) {
8780Sstevel@tonic-gate 		if (p == mnfp)
8790Sstevel@tonic-gate 			break;
8800Sstevel@tonic-gate 	}
8810Sstevel@tonic-gate 	ASSERT(p != NULL);
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	/*
8840Sstevel@tonic-gate 	 * Remove it from the list.
8850Sstevel@tonic-gate 	 */
8860Sstevel@tonic-gate 	*pp = p->mnf_nextp;
8870Sstevel@tonic-gate 	rw_exit(&(mip->mi_notify_lock));
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 	/*
8900Sstevel@tonic-gate 	 * Free it.
8910Sstevel@tonic-gate 	 */
8920Sstevel@tonic-gate 	kmem_free(mnfp, sizeof (mac_notify_fn_t));
8930Sstevel@tonic-gate }
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate void
8960Sstevel@tonic-gate mac_notify(mac_handle_t mh)
8970Sstevel@tonic-gate {
8980Sstevel@tonic-gate 	mac_impl_t		*mip = (mac_impl_t *)mh;
8990Sstevel@tonic-gate 	mac_notify_type_t	type;
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 	for (type = 0; type < MAC_NNOTE; type++)
9020Sstevel@tonic-gate 		i_mac_notify(mip, type);
9030Sstevel@tonic-gate }
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate mac_rx_handle_t
9060Sstevel@tonic-gate mac_rx_add(mac_handle_t mh, mac_rx_t rx, void *arg)
9070Sstevel@tonic-gate {
9080Sstevel@tonic-gate 	mac_impl_t	*mip = (mac_impl_t *)mh;
9090Sstevel@tonic-gate 	mac_rx_fn_t	*mrfp;
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 	mrfp = kmem_zalloc(sizeof (mac_rx_fn_t), KM_SLEEP);
9120Sstevel@tonic-gate 	mrfp->mrf_fn = rx;
9130Sstevel@tonic-gate 	mrfp->mrf_arg = arg;
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 	/*
9160Sstevel@tonic-gate 	 * Add it to the head of the 'rx' callback list.
9170Sstevel@tonic-gate 	 */
9180Sstevel@tonic-gate 	rw_enter(&(mip->mi_rx_lock), RW_WRITER);
9190Sstevel@tonic-gate 	mrfp->mrf_nextp = mip->mi_mrfp;
9200Sstevel@tonic-gate 	mip->mi_mrfp = mrfp;
9210Sstevel@tonic-gate 	rw_exit(&(mip->mi_rx_lock));
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate 	return ((mac_rx_handle_t)mrfp);
9240Sstevel@tonic-gate }
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate /*
9270Sstevel@tonic-gate  * Unregister a receive function for this mac.  This removes the function
9280Sstevel@tonic-gate  * from the list of receive functions for this mac.
9290Sstevel@tonic-gate  */
9300Sstevel@tonic-gate void
9310Sstevel@tonic-gate mac_rx_remove(mac_handle_t mh, mac_rx_handle_t mrh)
9320Sstevel@tonic-gate {
9330Sstevel@tonic-gate 	mac_impl_t		*mip = (mac_impl_t *)mh;
9340Sstevel@tonic-gate 	mac_rx_fn_t		*mrfp = (mac_rx_fn_t *)mrh;
9350Sstevel@tonic-gate 	mac_rx_fn_t		**pp;
9360Sstevel@tonic-gate 	mac_rx_fn_t		*p;
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 	/*
9390Sstevel@tonic-gate 	 * Search the 'rx' callback list for the function closure.
9400Sstevel@tonic-gate 	 */
9410Sstevel@tonic-gate 	rw_enter(&(mip->mi_rx_lock), RW_WRITER);
9420Sstevel@tonic-gate 	for (pp = &(mip->mi_mrfp); (p = *pp) != NULL; pp = &(p->mrf_nextp)) {
9430Sstevel@tonic-gate 		if (p == mrfp)
9440Sstevel@tonic-gate 			break;
9450Sstevel@tonic-gate 	}
9460Sstevel@tonic-gate 	ASSERT(p != NULL);
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 	/* Remove it from the list. */
9490Sstevel@tonic-gate 	*pp = p->mrf_nextp;
9500Sstevel@tonic-gate 	kmem_free(mrfp, sizeof (mac_rx_fn_t));
9510Sstevel@tonic-gate 	rw_exit(&(mip->mi_rx_lock));
9520Sstevel@tonic-gate }
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate mac_txloop_handle_t
9550Sstevel@tonic-gate mac_txloop_add(mac_handle_t mh, mac_txloop_t tx, void *arg)
9560Sstevel@tonic-gate {
9570Sstevel@tonic-gate 	mac_impl_t	*mip = (mac_impl_t *)mh;
9580Sstevel@tonic-gate 	mac_txloop_fn_t	*mtfp;
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 	mtfp = kmem_zalloc(sizeof (mac_txloop_fn_t), KM_SLEEP);
9610Sstevel@tonic-gate 	mtfp->mtf_fn = tx;
9620Sstevel@tonic-gate 	mtfp->mtf_arg = arg;
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 	/*
9650Sstevel@tonic-gate 	 * Add it to the head of the 'tx' callback list.
9660Sstevel@tonic-gate 	 */
9670Sstevel@tonic-gate 	rw_enter(&(mip->mi_txloop_lock), RW_WRITER);
9680Sstevel@tonic-gate 	mtfp->mtf_nextp = mip->mi_mtfp;
9690Sstevel@tonic-gate 	mip->mi_mtfp = mtfp;
9700Sstevel@tonic-gate 	rw_exit(&(mip->mi_txloop_lock));
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 	return ((mac_txloop_handle_t)mtfp);
9730Sstevel@tonic-gate }
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate /*
9760Sstevel@tonic-gate  * Unregister a transmit function for this mac.  This removes the function
9770Sstevel@tonic-gate  * from the list of transmit functions for this mac.
9780Sstevel@tonic-gate  */
9790Sstevel@tonic-gate void
9800Sstevel@tonic-gate mac_txloop_remove(mac_handle_t mh, mac_txloop_handle_t mth)
9810Sstevel@tonic-gate {
9820Sstevel@tonic-gate 	mac_impl_t		*mip = (mac_impl_t *)mh;
9830Sstevel@tonic-gate 	mac_txloop_fn_t		*mtfp = (mac_txloop_fn_t *)mth;
9840Sstevel@tonic-gate 	mac_txloop_fn_t		**pp;
9850Sstevel@tonic-gate 	mac_txloop_fn_t		*p;
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 	/*
9880Sstevel@tonic-gate 	 * Search the 'tx' callback list for the function.
9890Sstevel@tonic-gate 	 */
9900Sstevel@tonic-gate 	rw_enter(&(mip->mi_txloop_lock), RW_WRITER);
9910Sstevel@tonic-gate 	for (pp = &(mip->mi_mtfp); (p = *pp) != NULL; pp = &(p->mtf_nextp)) {
9920Sstevel@tonic-gate 		if (p == mtfp)
9930Sstevel@tonic-gate 			break;
9940Sstevel@tonic-gate 	}
9950Sstevel@tonic-gate 	ASSERT(p != NULL);
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 	/* Remove it from the list. */
9980Sstevel@tonic-gate 	*pp = p->mtf_nextp;
9990Sstevel@tonic-gate 	kmem_free(mtfp, sizeof (mac_txloop_fn_t));
10000Sstevel@tonic-gate 	rw_exit(&(mip->mi_txloop_lock));
10010Sstevel@tonic-gate }
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate void
10040Sstevel@tonic-gate mac_resource_set(mac_handle_t mh, mac_resource_add_t add, void *arg)
10050Sstevel@tonic-gate {
10060Sstevel@tonic-gate 	mac_impl_t		*mip = (mac_impl_t *)mh;
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 	/*
10090Sstevel@tonic-gate 	 * Update the 'resource_add' callbacks.
10100Sstevel@tonic-gate 	 */
10110Sstevel@tonic-gate 	rw_enter(&(mip->mi_resource_lock), RW_WRITER);
10120Sstevel@tonic-gate 	mip->mi_resource_add = add;
10130Sstevel@tonic-gate 	mip->mi_resource_add_arg = arg;
10140Sstevel@tonic-gate 	rw_exit(&(mip->mi_resource_lock));
10150Sstevel@tonic-gate }
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate /*
10180Sstevel@tonic-gate  * Driver support functions.
10190Sstevel@tonic-gate  */
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate int
10220Sstevel@tonic-gate mac_register(mac_t *mp)
10230Sstevel@tonic-gate {
1024*269Sericheng 	int	err, instance;
1025*269Sericheng 	char	name[MAXNAMELEN], devname[MAXNAMELEN];
1026*269Sericheng 	const char *drvname;
10270Sstevel@tonic-gate 	struct devnames *dnp;
1028*269Sericheng 	minor_t	minor;
10290Sstevel@tonic-gate 
1030*269Sericheng 	drvname = ddi_driver_name(mp->m_dip);
1031*269Sericheng 	instance = ddi_get_instance(mp->m_dip);
1032*269Sericheng 
1033*269Sericheng 	if (strcmp(mp->m_ident, MAC_IDENT) != 0) {
10340Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d/%d: possible mac interface mismatch",
1035*269Sericheng 		    drvname, instance, mp->m_port);
1036*269Sericheng 	}
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate 	/*
10390Sstevel@tonic-gate 	 * Create a new mac_impl_t to pair with the mac_t.
10400Sstevel@tonic-gate 	 */
10410Sstevel@tonic-gate 	if ((err = i_mac_create(mp)) != 0)
10420Sstevel@tonic-gate 		return (err);
10430Sstevel@tonic-gate 
1044*269Sericheng 	err = EEXIST;
1045*269Sericheng 	if (ddi_create_minor_node(mp->m_dip, (char *)drvname, S_IFCHR, 0,
1046*269Sericheng 	    DDI_NT_NET, CLONE_DEV) != DDI_SUCCESS)
1047*269Sericheng 		goto fail1;
1048*269Sericheng 
1049*269Sericheng 	(void) snprintf(devname, MAXNAMELEN, "%s%d", drvname, instance);
1050*269Sericheng 
1051*269Sericheng 	if (strcmp(drvname, "aggr") == 0) {
1052*269Sericheng 		(void) snprintf(name, MAXNAMELEN, "aggr%u", mp->m_port);
1053*269Sericheng 		minor = (minor_t)mp->m_port + 1;
1054*269Sericheng 	} else {
1055*269Sericheng 		(void) strlcpy(name, devname, MAXNAMELEN);
1056*269Sericheng 		minor = (minor_t)instance + 1;
10570Sstevel@tonic-gate 	}
10580Sstevel@tonic-gate 
1059*269Sericheng 	if (ddi_create_minor_node(mp->m_dip, name, S_IFCHR, minor,
1060*269Sericheng 	    DDI_NT_NET, 0) != DDI_SUCCESS)
1061*269Sericheng 		goto fail2;
1062*269Sericheng 
1063*269Sericheng 	if ((err = dls_create(name, devname, mp->m_port)) != 0)
1064*269Sericheng 		goto fail3;
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate 	/* set the gldv3 flag in dn_flags */
10670Sstevel@tonic-gate 	dnp = &devnamesp[ddi_driver_major(mp->m_dip)];
10680Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
10690Sstevel@tonic-gate 	dnp->dn_flags |= DN_GLDV3_DRIVER;
10700Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
10710Sstevel@tonic-gate 
1072*269Sericheng 	cmn_err(CE_NOTE, "!%s%d/%d registered", drvname, instance, mp->m_port);
1073*269Sericheng 	return (0);
10740Sstevel@tonic-gate 
1075*269Sericheng fail3:
1076*269Sericheng 	ddi_remove_minor_node(mp->m_dip, name);
1077*269Sericheng fail2:
1078*269Sericheng 	ddi_remove_minor_node(mp->m_dip, (char *)drvname);
1079*269Sericheng fail1:
1080*269Sericheng 	i_mac_destroy(mp);
1081*269Sericheng 	return (err);
10820Sstevel@tonic-gate }
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate int
10850Sstevel@tonic-gate mac_unregister(mac_t *mp)
10860Sstevel@tonic-gate {
1087*269Sericheng 	int		err, instance;
10880Sstevel@tonic-gate 	char		name[MAXNAMELEN];
1089*269Sericheng 	const char	*drvname;
10900Sstevel@tonic-gate 	mac_impl_t	*mip = mp->m_impl;
10910Sstevel@tonic-gate 
1092*269Sericheng 	drvname = ddi_driver_name(mp->m_dip);
1093*269Sericheng 	instance = ddi_get_instance(mp->m_dip);
1094*269Sericheng 
10950Sstevel@tonic-gate 	/*
10960Sstevel@tonic-gate 	 * See if there are any other references to this mac_t (e.g., VLAN's).
10970Sstevel@tonic-gate 	 * If not, set mi_destroying to prevent any new VLAN's from being
10980Sstevel@tonic-gate 	 * created before we can perform the i_mac_destroy() below.
10990Sstevel@tonic-gate 	 */
1100*269Sericheng 	rw_enter(&i_mac_impl_lock, RW_WRITER);
11010Sstevel@tonic-gate 	if (mip->mi_ref > 0) {
1102*269Sericheng 		rw_exit(&i_mac_impl_lock);
11030Sstevel@tonic-gate 		return (EBUSY);
11040Sstevel@tonic-gate 	}
11050Sstevel@tonic-gate 	mip->mi_destroying = B_TRUE;
1106*269Sericheng 	rw_exit(&i_mac_impl_lock);
11070Sstevel@tonic-gate 
1108*269Sericheng 	if (strcmp(drvname, "aggr") == 0)
11090Sstevel@tonic-gate 		(void) snprintf(name, MAXNAMELEN, "aggr%u", mp->m_port);
1110*269Sericheng 	else
1111*269Sericheng 		(void) snprintf(name, MAXNAMELEN, "%s%d", drvname, instance);
1112*269Sericheng 
1113*269Sericheng 	if ((err = dls_destroy(name)) != 0) {
1114*269Sericheng 		rw_enter(&i_mac_impl_lock, RW_WRITER);
1115*269Sericheng 		mip->mi_destroying = B_FALSE;
1116*269Sericheng 		rw_exit(&i_mac_impl_lock);
1117*269Sericheng 		return (err);
11180Sstevel@tonic-gate 	}
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate 	/*
11210Sstevel@tonic-gate 	 * Destroy the mac_impl_t.
11220Sstevel@tonic-gate 	 */
11230Sstevel@tonic-gate 	i_mac_destroy(mp);
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate 	/*
1126*269Sericheng 	 * Remove both style 1 and style 2 minor nodes
11270Sstevel@tonic-gate 	 */
1128*269Sericheng 	ddi_remove_minor_node(mp->m_dip, (char *)drvname);
11290Sstevel@tonic-gate 	ddi_remove_minor_node(mp->m_dip, name);
11300Sstevel@tonic-gate 
1131*269Sericheng 	cmn_err(CE_NOTE, "!%s%d/%d unregistered", drvname, instance,
11320Sstevel@tonic-gate 	    mp->m_port);
11330Sstevel@tonic-gate 	return (0);
11340Sstevel@tonic-gate }
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate void
11370Sstevel@tonic-gate mac_rx(mac_t *mp, mac_resource_handle_t mrh, mblk_t *bp)
11380Sstevel@tonic-gate {
11390Sstevel@tonic-gate 	mac_impl_t	*mip = mp->m_impl;
11400Sstevel@tonic-gate 	mac_rx_fn_t	*mrfp;
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate 	/*
11430Sstevel@tonic-gate 	 * Call all registered receive functions.
11440Sstevel@tonic-gate 	 */
11450Sstevel@tonic-gate 	rw_enter(&mip->mi_rx_lock, RW_READER);
11460Sstevel@tonic-gate 	mrfp = mip->mi_mrfp;
11470Sstevel@tonic-gate 	if (mrfp == NULL) {
11480Sstevel@tonic-gate 		/* There are no registered receive functions. */
11490Sstevel@tonic-gate 		freemsgchain(bp);
11500Sstevel@tonic-gate 		rw_exit(&mip->mi_rx_lock);
11510Sstevel@tonic-gate 		return;
11520Sstevel@tonic-gate 	}
11530Sstevel@tonic-gate 	do {
11540Sstevel@tonic-gate 		mblk_t *recv_bp;
11550Sstevel@tonic-gate 
11560Sstevel@tonic-gate 		if (mrfp->mrf_nextp != NULL) {
11570Sstevel@tonic-gate 			/* XXX Do we bump a counter if copymsgchain() fails? */
11580Sstevel@tonic-gate 			recv_bp = copymsgchain(bp);
11590Sstevel@tonic-gate 		} else {
11600Sstevel@tonic-gate 			recv_bp = bp;
11610Sstevel@tonic-gate 		}
11620Sstevel@tonic-gate 		if (recv_bp != NULL)
11630Sstevel@tonic-gate 			mrfp->mrf_fn(mrfp->mrf_arg, mrh, recv_bp);
11640Sstevel@tonic-gate 		mrfp = mrfp->mrf_nextp;
11650Sstevel@tonic-gate 	} while (mrfp != NULL);
11660Sstevel@tonic-gate 	rw_exit(&mip->mi_rx_lock);
11670Sstevel@tonic-gate }
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate /*
11700Sstevel@tonic-gate  * Transmit function -- ONLY used when there are registered loopback listeners.
11710Sstevel@tonic-gate  */
11720Sstevel@tonic-gate mblk_t *
11730Sstevel@tonic-gate mac_txloop(void *arg, mblk_t *bp)
11740Sstevel@tonic-gate {
11750Sstevel@tonic-gate 	mac_impl_t	*mip = arg;
11760Sstevel@tonic-gate 	mac_t		*mp = mip->mi_mp;
11770Sstevel@tonic-gate 	mac_txloop_fn_t	*mtfp;
11780Sstevel@tonic-gate 	mblk_t		*loop_bp, *resid_bp, *next_bp;
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate 	while (bp != NULL) {
11810Sstevel@tonic-gate 		next_bp = bp->b_next;
11820Sstevel@tonic-gate 		bp->b_next = NULL;
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 		if ((loop_bp = copymsg(bp)) == NULL)
11850Sstevel@tonic-gate 			goto noresources;
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 		if ((resid_bp = mp->m_tx(mp->m_driver, bp)) != NULL) {
11880Sstevel@tonic-gate 			ASSERT(resid_bp == bp);
11890Sstevel@tonic-gate 			freemsg(loop_bp);
11900Sstevel@tonic-gate 			goto noresources;
11910Sstevel@tonic-gate 		}
11920Sstevel@tonic-gate 
11930Sstevel@tonic-gate 		rw_enter(&mip->mi_txloop_lock, RW_READER);
11940Sstevel@tonic-gate 		mtfp = mip->mi_mtfp;
119556Smeem 		while (mtfp != NULL && loop_bp != NULL) {
11960Sstevel@tonic-gate 			bp = loop_bp;
119756Smeem 
119856Smeem 			/* XXX counter bump if copymsg() fails? */
119956Smeem 			if (mtfp->mtf_nextp != NULL)
12000Sstevel@tonic-gate 				loop_bp = copymsg(bp);
120156Smeem 			else
120256Smeem 				loop_bp = NULL;
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 			mtfp->mtf_fn(mtfp->mtf_arg, bp);
120556Smeem 			mtfp = mtfp->mtf_nextp;
12060Sstevel@tonic-gate 		}
120756Smeem 		rw_exit(&mip->mi_txloop_lock);
12080Sstevel@tonic-gate 
120956Smeem 		/*
121056Smeem 		 * It's possible we've raced with the disabling of promiscuous
121156Smeem 		 * mode, in which case we can discard our copy.
121256Smeem 		 */
121356Smeem 		if (loop_bp != NULL)
121456Smeem 			freemsg(loop_bp);
121556Smeem 
12160Sstevel@tonic-gate 		bp = next_bp;
12170Sstevel@tonic-gate 	}
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate 	return (NULL);
12200Sstevel@tonic-gate 
12210Sstevel@tonic-gate noresources:
12220Sstevel@tonic-gate 	bp->b_next = next_bp;
12230Sstevel@tonic-gate 	return (bp);
12240Sstevel@tonic-gate }
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate void
12270Sstevel@tonic-gate mac_link_update(mac_t *mp, link_state_t link)
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 link state.
12350Sstevel@tonic-gate 	 */
12360Sstevel@tonic-gate 	mip->mi_link = link;
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 	/*
12390Sstevel@tonic-gate 	 * Send a MAC_NOTE_LINK notification.
12400Sstevel@tonic-gate 	 */
12410Sstevel@tonic-gate 	i_mac_notify(mip, MAC_NOTE_LINK);
12420Sstevel@tonic-gate }
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate void
12450Sstevel@tonic-gate mac_unicst_update(mac_t *mp, const uint8_t *addr)
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 	 * Save the address.
12530Sstevel@tonic-gate 	 */
12540Sstevel@tonic-gate 	bcopy(addr, mip->mi_addr, mip->mi_addr_length);
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate 	/*
12570Sstevel@tonic-gate 	 * Send a MAC_NOTE_UNICST notification.
12580Sstevel@tonic-gate 	 */
12590Sstevel@tonic-gate 	i_mac_notify(mip, MAC_NOTE_UNICST);
12600Sstevel@tonic-gate }
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate void
12630Sstevel@tonic-gate mac_tx_update(mac_t *mp)
12640Sstevel@tonic-gate {
12650Sstevel@tonic-gate 	mac_impl_t	*mip = mp->m_impl;
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 	ASSERT(mip->mi_mp == mp);
12680Sstevel@tonic-gate 
12690Sstevel@tonic-gate 	/*
12700Sstevel@tonic-gate 	 * Send a MAC_NOTE_TX notification.
12710Sstevel@tonic-gate 	 */
12720Sstevel@tonic-gate 	i_mac_notify(mip, MAC_NOTE_TX);
12730Sstevel@tonic-gate }
12740Sstevel@tonic-gate 
12750Sstevel@tonic-gate void
12760Sstevel@tonic-gate mac_resource_update(mac_t *mp)
12770Sstevel@tonic-gate {
12780Sstevel@tonic-gate 	mac_impl_t	*mip = mp->m_impl;
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate 	ASSERT(mip->mi_mp == mp);
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate 	/*
12830Sstevel@tonic-gate 	 * Send a MAC_NOTE_RESOURCE notification.
12840Sstevel@tonic-gate 	 */
12850Sstevel@tonic-gate 	i_mac_notify(mip, MAC_NOTE_RESOURCE);
12860Sstevel@tonic-gate }
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate mac_resource_handle_t
12890Sstevel@tonic-gate mac_resource_add(mac_t *mp, mac_resource_t *mrp)
12900Sstevel@tonic-gate {
12910Sstevel@tonic-gate 	mac_impl_t		*mip = mp->m_impl;
12920Sstevel@tonic-gate 	mac_resource_handle_t	mrh;
12930Sstevel@tonic-gate 	mac_resource_add_t	add;
12940Sstevel@tonic-gate 	void			*arg;
12950Sstevel@tonic-gate 
12960Sstevel@tonic-gate 	rw_enter(&mip->mi_resource_lock, RW_READER);
12970Sstevel@tonic-gate 	add = mip->mi_resource_add;
12980Sstevel@tonic-gate 	arg = mip->mi_resource_add_arg;
12990Sstevel@tonic-gate 
13000Sstevel@tonic-gate 	mrh = add(arg, mrp);
13010Sstevel@tonic-gate 	rw_exit(&mip->mi_resource_lock);
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate 	return (mrh);
13040Sstevel@tonic-gate }
13050Sstevel@tonic-gate 
13060Sstevel@tonic-gate void
13070Sstevel@tonic-gate mac_multicst_refresh(mac_t *mp, mac_multicst_t refresh, void *arg,
13080Sstevel@tonic-gate     boolean_t add)
13090Sstevel@tonic-gate {
13100Sstevel@tonic-gate 	mac_impl_t		*mip = mp->m_impl;
13110Sstevel@tonic-gate 	mac_multicst_addr_t	*p;
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate 	/*
13140Sstevel@tonic-gate 	 * If no specific refresh function was given then default to the
13150Sstevel@tonic-gate 	 * driver's m_multicst entry point.
13160Sstevel@tonic-gate 	 */
13170Sstevel@tonic-gate 	if (refresh == NULL) {
13180Sstevel@tonic-gate 		refresh = mp->m_multicst;
13190Sstevel@tonic-gate 		arg = mp->m_driver;
13200Sstevel@tonic-gate 	}
13210Sstevel@tonic-gate 	ASSERT(refresh != NULL);
13220Sstevel@tonic-gate 
13230Sstevel@tonic-gate 	/*
13240Sstevel@tonic-gate 	 * Walk the multicast address list and call the refresh function for
13250Sstevel@tonic-gate 	 * each address.
13260Sstevel@tonic-gate 	 */
13270Sstevel@tonic-gate 	rw_enter(&(mip->mi_data_lock), RW_READER);
13280Sstevel@tonic-gate 	for (p = mip->mi_mmap; p != NULL; p = p->mma_nextp)
13290Sstevel@tonic-gate 		refresh(arg, add, p->mma_addr);
13300Sstevel@tonic-gate 	rw_exit(&(mip->mi_data_lock));
13310Sstevel@tonic-gate }
13320Sstevel@tonic-gate 
13330Sstevel@tonic-gate void
13340Sstevel@tonic-gate mac_unicst_refresh(mac_t *mp, mac_unicst_t refresh, void *arg)
13350Sstevel@tonic-gate {
13360Sstevel@tonic-gate 	mac_impl_t	*mip = mp->m_impl;
13370Sstevel@tonic-gate 	/*
13380Sstevel@tonic-gate 	 * If no specific refresh function was given then default to the
13390Sstevel@tonic-gate 	 * driver's m_unicst entry point.
13400Sstevel@tonic-gate 	 */
13410Sstevel@tonic-gate 	if (refresh == NULL) {
13420Sstevel@tonic-gate 		refresh = mp->m_unicst;
13430Sstevel@tonic-gate 		arg = mp->m_driver;
13440Sstevel@tonic-gate 	}
13450Sstevel@tonic-gate 	ASSERT(refresh != NULL);
13460Sstevel@tonic-gate 
13470Sstevel@tonic-gate 	/*
13480Sstevel@tonic-gate 	 * Call the refresh function with the current unicast address.
13490Sstevel@tonic-gate 	 */
13500Sstevel@tonic-gate 	refresh(arg, mip->mi_addr);
13510Sstevel@tonic-gate }
13520Sstevel@tonic-gate 
13530Sstevel@tonic-gate void
13540Sstevel@tonic-gate mac_promisc_refresh(mac_t *mp, mac_promisc_t refresh, void *arg)
13550Sstevel@tonic-gate {
13560Sstevel@tonic-gate 	mac_impl_t	*mip = mp->m_impl;
13570Sstevel@tonic-gate 
13580Sstevel@tonic-gate 	/*
13590Sstevel@tonic-gate 	 * If no specific refresh function was given then default to the
13600Sstevel@tonic-gate 	 * driver's m_promisc entry point.
13610Sstevel@tonic-gate 	 */
13620Sstevel@tonic-gate 	if (refresh == NULL) {
13630Sstevel@tonic-gate 		refresh = mp->m_promisc;
13640Sstevel@tonic-gate 		arg = mp->m_driver;
13650Sstevel@tonic-gate 	}
13660Sstevel@tonic-gate 	ASSERT(refresh != NULL);
13670Sstevel@tonic-gate 
13680Sstevel@tonic-gate 	/*
13690Sstevel@tonic-gate 	 * Call the refresh function with the current promiscuity.
13700Sstevel@tonic-gate 	 */
13710Sstevel@tonic-gate 	refresh(arg, (mip->mi_devpromisc != 0));
13720Sstevel@tonic-gate }
13730Sstevel@tonic-gate 
13740Sstevel@tonic-gate boolean_t
13750Sstevel@tonic-gate mac_active_set(mac_handle_t mh)
13760Sstevel@tonic-gate {
13770Sstevel@tonic-gate 	mac_impl_t *mip = (mac_impl_t *)mh;
13780Sstevel@tonic-gate 
13790Sstevel@tonic-gate 	mutex_enter(&mip->mi_activelink_lock);
13800Sstevel@tonic-gate 	if (mip->mi_activelink) {
13810Sstevel@tonic-gate 		mutex_exit(&mip->mi_activelink_lock);
13820Sstevel@tonic-gate 		return (B_FALSE);
13830Sstevel@tonic-gate 	}
13840Sstevel@tonic-gate 	mip->mi_activelink = B_TRUE;
13850Sstevel@tonic-gate 	mutex_exit(&mip->mi_activelink_lock);
13860Sstevel@tonic-gate 	return (B_TRUE);
13870Sstevel@tonic-gate }
13880Sstevel@tonic-gate 
13890Sstevel@tonic-gate void
13900Sstevel@tonic-gate mac_active_clear(mac_handle_t mh)
13910Sstevel@tonic-gate {
13920Sstevel@tonic-gate 	mac_impl_t *mip = (mac_impl_t *)mh;
13930Sstevel@tonic-gate 
13940Sstevel@tonic-gate 	mutex_enter(&mip->mi_activelink_lock);
13950Sstevel@tonic-gate 	ASSERT(mip->mi_activelink);
13960Sstevel@tonic-gate 	mip->mi_activelink = B_FALSE;
13970Sstevel@tonic-gate 	mutex_exit(&mip->mi_activelink_lock);
13980Sstevel@tonic-gate }
1399*269Sericheng 
1400*269Sericheng /*
1401*269Sericheng  * mac_info_get() is used for retrieving the mac_info when a DL_INFO_REQ is
1402*269Sericheng  * issued before a DL_ATTACH_REQ. we walk the i_mac_impl_hash table and find
1403*269Sericheng  * the first mac_impl_t with a matching driver name; then we copy its mac_info_t
1404*269Sericheng  * to the caller. we do all this with i_mac_impl_lock held so the mac_impl_t
1405*269Sericheng  * cannot disappear while we are accessing it.
1406*269Sericheng  */
1407*269Sericheng typedef struct i_mac_info_state_s {
1408*269Sericheng 	const char	*mi_name;
1409*269Sericheng 	mac_info_t	*mi_infop;
1410*269Sericheng } i_mac_info_state_t;
1411*269Sericheng 
1412*269Sericheng /*ARGSUSED*/
1413*269Sericheng static uint_t
1414*269Sericheng i_mac_info_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
1415*269Sericheng {
1416*269Sericheng 	i_mac_info_state_t	*statep = arg;
1417*269Sericheng 	mac_impl_t		*mip = (mac_impl_t *)val;
1418*269Sericheng 
1419*269Sericheng 	if (mip->mi_destroying)
1420*269Sericheng 		return (MH_WALK_CONTINUE);
1421*269Sericheng 
1422*269Sericheng 	if (strcmp(statep->mi_name,
1423*269Sericheng 	    ddi_driver_name(mip->mi_mp->m_dip)) != 0)
1424*269Sericheng 		return (MH_WALK_CONTINUE);
1425*269Sericheng 
1426*269Sericheng 	statep->mi_infop = &mip->mi_mp->m_info;
1427*269Sericheng 	return (MH_WALK_TERMINATE);
1428*269Sericheng }
1429*269Sericheng 
1430*269Sericheng boolean_t
1431*269Sericheng mac_info_get(const char *name, mac_info_t *minfop)
1432*269Sericheng {
1433*269Sericheng 	i_mac_info_state_t	state;
1434*269Sericheng 
1435*269Sericheng 	rw_enter(&i_mac_impl_lock, RW_READER);
1436*269Sericheng 	state.mi_name = name;
1437*269Sericheng 	state.mi_infop = NULL;
1438*269Sericheng 	mod_hash_walk(i_mac_impl_hash, i_mac_info_walker, &state);
1439*269Sericheng 	if (state.mi_infop == NULL) {
1440*269Sericheng 		rw_exit(&i_mac_impl_lock);
1441*269Sericheng 		return (B_FALSE);
1442*269Sericheng 	}
1443*269Sericheng 	*minfop = *state.mi_infop;
1444*269Sericheng 	rw_exit(&i_mac_impl_lock);
1445*269Sericheng 	return (B_TRUE);
1446*269Sericheng }
1447*269Sericheng 
1448*269Sericheng void
1449*269Sericheng mac_init_ops(struct dev_ops *ops, const char *name)
1450*269Sericheng {
1451*269Sericheng 	dld_init_ops(ops, name);
1452*269Sericheng }
1453*269Sericheng 
1454*269Sericheng void
1455*269Sericheng mac_fini_ops(struct dev_ops *ops)
1456*269Sericheng {
1457*269Sericheng 	dld_fini_ops(ops);
1458*269Sericheng }
1459