xref: /onnv-gate/usr/src/uts/common/io/softmac/softmac_main.c (revision 9073:a5a4bb23741e)
15895Syz147064 /*
25895Syz147064  * CDDL HEADER START
35895Syz147064  *
45895Syz147064  * The contents of this file are subject to the terms of the
55895Syz147064  * Common Development and Distribution License (the "License").
65895Syz147064  * You may not use this file except in compliance with the License.
75895Syz147064  *
85895Syz147064  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95895Syz147064  * or http://www.opensolaris.org/os/licensing.
105895Syz147064  * See the License for the specific language governing permissions
115895Syz147064  * and limitations under the License.
125895Syz147064  *
135895Syz147064  * When distributing Covered Code, include this CDDL HEADER in each
145895Syz147064  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155895Syz147064  * If applicable, add the following below this CDDL HEADER, with the
165895Syz147064  * fields enclosed by brackets "[]" replaced with your own identifying
175895Syz147064  * information: Portions Copyright [yyyy] [name of copyright owner]
185895Syz147064  *
195895Syz147064  * CDDL HEADER END
205895Syz147064  */
215895Syz147064 /*
228527SCathy.Zhou@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
235895Syz147064  * Use is subject to license terms.
245895Syz147064  */
255895Syz147064 
265895Syz147064 /*
275895Syz147064  * The softmac driver is used to "unify" non-GLDv3 drivers to the GLDv3
285895Syz147064  * framework.  It also creates the kernel datalink structure for each
295895Syz147064  * physical network device.
305895Syz147064  *
315895Syz147064  * Specifically, a softmac will be created for each physical network device
325895Syz147064  * (dip) during the device's post-attach process.  When this softmac is
335895Syz147064  * created, the following will also be done:
345895Syz147064  *   - create the device's <link name, linkid> mapping;
355895Syz147064  *   - register the mac if this is a non-GLDv3 device and the media type is
365895Syz147064  *     supported by the GLDv3 framework;
375895Syz147064  *   - create the kernel data-link structure for this physical device;
385895Syz147064  *
395895Syz147064  * This softmac will be destroyed during the device's pre-detach process,
405895Syz147064  * and all the above will be undone.
415895Syz147064  */
425895Syz147064 
435895Syz147064 #include <sys/types.h>
445895Syz147064 #include <sys/file.h>
455895Syz147064 #include <sys/cred.h>
465895Syz147064 #include <sys/dlpi.h>
478275SEric Cheng #include <sys/mac_provider.h>
488275SEric Cheng #include <sys/disp.h>
495895Syz147064 #include <sys/sunndi.h>
505895Syz147064 #include <sys/modhash.h>
515895Syz147064 #include <sys/stropts.h>
525895Syz147064 #include <sys/sysmacros.h>
535895Syz147064 #include <sys/vlan.h>
545895Syz147064 #include <sys/softmac_impl.h>
555895Syz147064 #include <sys/softmac.h>
565895Syz147064 #include <sys/dls.h>
575895Syz147064 
588275SEric Cheng /* Used as a parameter to the mod hash walk of softmac structures */
598275SEric Cheng typedef struct {
608275SEric Cheng 	softmac_t	*smw_softmac;
618275SEric Cheng 	boolean_t	smw_retry;
628275SEric Cheng } softmac_walk_t;
638275SEric Cheng 
645895Syz147064 /*
655895Syz147064  * Softmac hash table including softmacs for both style-2 and style-1 devices.
665895Syz147064  */
675895Syz147064 static krwlock_t	softmac_hash_lock;
685895Syz147064 static mod_hash_t	*softmac_hash;
698275SEric Cheng static kmutex_t		smac_global_lock;
708275SEric Cheng static kcondvar_t	smac_global_cv;
715895Syz147064 
72*9073SCathy.Zhou@Sun.COM static kmem_cache_t	*softmac_cachep;
73*9073SCathy.Zhou@Sun.COM 
745895Syz147064 #define	SOFTMAC_HASHSZ		64
755895Syz147064 
767323SCathy.Zhou@Sun.COM static void softmac_create_task(void *);
777323SCathy.Zhou@Sun.COM static void softmac_mac_register(softmac_t *);
785895Syz147064 static int softmac_create_datalink(softmac_t *);
795895Syz147064 static int softmac_m_start(void *);
805895Syz147064 static void softmac_m_stop(void *);
815895Syz147064 static int softmac_m_open(void *);
825895Syz147064 static void softmac_m_close(void *);
835895Syz147064 static boolean_t softmac_m_getcapab(void *, mac_capab_t, void *);
84*9073SCathy.Zhou@Sun.COM static int softmac_m_setprop(void *, const char *, mac_prop_id_t,
85*9073SCathy.Zhou@Sun.COM     uint_t, const void *);
86*9073SCathy.Zhou@Sun.COM static int softmac_m_getprop(void *, const char *, mac_prop_id_t,
87*9073SCathy.Zhou@Sun.COM     uint_t, uint_t, void *, uint_t *);
88*9073SCathy.Zhou@Sun.COM 
895895Syz147064 
905895Syz147064 #define	SOFTMAC_M_CALLBACK_FLAGS	\
91*9073SCathy.Zhou@Sun.COM 	(MC_IOCTL | MC_GETCAPAB | MC_OPEN | MC_CLOSE | MC_SETPROP | MC_GETPROP)
925895Syz147064 
935895Syz147064 static mac_callbacks_t softmac_m_callbacks = {
945895Syz147064 	SOFTMAC_M_CALLBACK_FLAGS,
955895Syz147064 	softmac_m_stat,
965895Syz147064 	softmac_m_start,
975895Syz147064 	softmac_m_stop,
985895Syz147064 	softmac_m_promisc,
995895Syz147064 	softmac_m_multicst,
1005895Syz147064 	softmac_m_unicst,
1015895Syz147064 	softmac_m_tx,
1025895Syz147064 	softmac_m_ioctl,
1035895Syz147064 	softmac_m_getcapab,
1045895Syz147064 	softmac_m_open,
105*9073SCathy.Zhou@Sun.COM 	softmac_m_close,
106*9073SCathy.Zhou@Sun.COM 	softmac_m_setprop,
107*9073SCathy.Zhou@Sun.COM 	softmac_m_getprop
1085895Syz147064 };
1095895Syz147064 
110*9073SCathy.Zhou@Sun.COM /*ARGSUSED*/
111*9073SCathy.Zhou@Sun.COM static int
112*9073SCathy.Zhou@Sun.COM softmac_constructor(void *buf, void *arg, int kmflag)
113*9073SCathy.Zhou@Sun.COM {
114*9073SCathy.Zhou@Sun.COM 	softmac_t	*softmac = buf;
115*9073SCathy.Zhou@Sun.COM 
116*9073SCathy.Zhou@Sun.COM 	bzero(buf, sizeof (softmac_t));
117*9073SCathy.Zhou@Sun.COM 	mutex_init(&softmac->smac_mutex, NULL, MUTEX_DEFAULT, NULL);
118*9073SCathy.Zhou@Sun.COM 	mutex_init(&softmac->smac_active_mutex, NULL, MUTEX_DEFAULT, NULL);
119*9073SCathy.Zhou@Sun.COM 	mutex_init(&softmac->smac_fp_mutex, NULL, MUTEX_DEFAULT, NULL);
120*9073SCathy.Zhou@Sun.COM 	cv_init(&softmac->smac_cv, NULL, CV_DEFAULT, NULL);
121*9073SCathy.Zhou@Sun.COM 	cv_init(&softmac->smac_fp_cv, NULL, CV_DEFAULT, NULL);
122*9073SCathy.Zhou@Sun.COM 	list_create(&softmac->smac_sup_list, sizeof (softmac_upper_t),
123*9073SCathy.Zhou@Sun.COM 	    offsetof(softmac_upper_t, su_list_node));
124*9073SCathy.Zhou@Sun.COM 	return (0);
125*9073SCathy.Zhou@Sun.COM }
126*9073SCathy.Zhou@Sun.COM 
127*9073SCathy.Zhou@Sun.COM /*ARGSUSED*/
128*9073SCathy.Zhou@Sun.COM static void
129*9073SCathy.Zhou@Sun.COM softmac_destructor(void *buf, void *arg)
130*9073SCathy.Zhou@Sun.COM {
131*9073SCathy.Zhou@Sun.COM 	softmac_t	*softmac = buf;
132*9073SCathy.Zhou@Sun.COM 
133*9073SCathy.Zhou@Sun.COM 	ASSERT(softmac->smac_fp_disable_clients == 0);
134*9073SCathy.Zhou@Sun.COM 	ASSERT(!softmac->smac_fastpath_admin_disabled);
135*9073SCathy.Zhou@Sun.COM 
136*9073SCathy.Zhou@Sun.COM 	ASSERT(!(softmac->smac_flags & SOFTMAC_ATTACH_DONE));
137*9073SCathy.Zhou@Sun.COM 	ASSERT(softmac->smac_hold_cnt == 0);
138*9073SCathy.Zhou@Sun.COM 	ASSERT(softmac->smac_attachok_cnt == 0);
139*9073SCathy.Zhou@Sun.COM 	ASSERT(softmac->smac_mh == NULL);
140*9073SCathy.Zhou@Sun.COM 	ASSERT(softmac->smac_softmac[0] == NULL &&
141*9073SCathy.Zhou@Sun.COM 	    softmac->smac_softmac[1] == NULL);
142*9073SCathy.Zhou@Sun.COM 	ASSERT(softmac->smac_state == SOFTMAC_INITIALIZED);
143*9073SCathy.Zhou@Sun.COM 	ASSERT(softmac->smac_lower == NULL);
144*9073SCathy.Zhou@Sun.COM 	ASSERT(softmac->smac_active == B_FALSE);
145*9073SCathy.Zhou@Sun.COM 	ASSERT(softmac->smac_nactive == 0);
146*9073SCathy.Zhou@Sun.COM 	ASSERT(list_is_empty(&softmac->smac_sup_list));
147*9073SCathy.Zhou@Sun.COM 
148*9073SCathy.Zhou@Sun.COM 	list_destroy(&softmac->smac_sup_list);
149*9073SCathy.Zhou@Sun.COM 	mutex_destroy(&softmac->smac_mutex);
150*9073SCathy.Zhou@Sun.COM 	mutex_destroy(&softmac->smac_active_mutex);
151*9073SCathy.Zhou@Sun.COM 	mutex_destroy(&softmac->smac_fp_mutex);
152*9073SCathy.Zhou@Sun.COM 	cv_destroy(&softmac->smac_cv);
153*9073SCathy.Zhou@Sun.COM 	cv_destroy(&softmac->smac_fp_cv);
154*9073SCathy.Zhou@Sun.COM }
155*9073SCathy.Zhou@Sun.COM 
1565895Syz147064 void
1575895Syz147064 softmac_init()
1585895Syz147064 {
1595895Syz147064 	softmac_hash = mod_hash_create_extended("softmac_hash",
1605895Syz147064 	    SOFTMAC_HASHSZ, mod_hash_null_keydtor, mod_hash_null_valdtor,
1615895Syz147064 	    mod_hash_bystr, NULL, mod_hash_strkey_cmp, KM_SLEEP);
1625895Syz147064 
1635895Syz147064 	rw_init(&softmac_hash_lock, NULL, RW_DEFAULT, NULL);
1648275SEric Cheng 	mutex_init(&smac_global_lock, NULL, MUTEX_DRIVER, NULL);
1658275SEric Cheng 	cv_init(&smac_global_cv, NULL, CV_DRIVER, NULL);
166*9073SCathy.Zhou@Sun.COM 
167*9073SCathy.Zhou@Sun.COM 	softmac_cachep = kmem_cache_create("softmac_cache",
168*9073SCathy.Zhou@Sun.COM 	    sizeof (softmac_t), 0, softmac_constructor,
169*9073SCathy.Zhou@Sun.COM 	    softmac_destructor, NULL, NULL, NULL, 0);
170*9073SCathy.Zhou@Sun.COM 	ASSERT(softmac_cachep != NULL);
171*9073SCathy.Zhou@Sun.COM 	softmac_fp_init();
1725895Syz147064 }
1735895Syz147064 
1745895Syz147064 void
1755895Syz147064 softmac_fini()
1765895Syz147064 {
177*9073SCathy.Zhou@Sun.COM 	softmac_fp_fini();
178*9073SCathy.Zhou@Sun.COM 	kmem_cache_destroy(softmac_cachep);
1795895Syz147064 	rw_destroy(&softmac_hash_lock);
1805895Syz147064 	mod_hash_destroy_hash(softmac_hash);
1818275SEric Cheng 	mutex_destroy(&smac_global_lock);
1828275SEric Cheng 	cv_destroy(&smac_global_cv);
1835895Syz147064 }
1845895Syz147064 
1855895Syz147064 /* ARGSUSED */
1865895Syz147064 static uint_t
1875895Syz147064 softmac_exist(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
1885895Syz147064 {
1895895Syz147064 	boolean_t *pexist = arg;
1905895Syz147064 
1915895Syz147064 	*pexist = B_TRUE;
1925895Syz147064 	return (MH_WALK_TERMINATE);
1935895Syz147064 }
1945895Syz147064 
1955895Syz147064 boolean_t
1965895Syz147064 softmac_busy()
1975895Syz147064 {
1985895Syz147064 	boolean_t exist = B_FALSE;
1995895Syz147064 
2005895Syz147064 	rw_enter(&softmac_hash_lock, RW_READER);
2015895Syz147064 	mod_hash_walk(softmac_hash, softmac_exist, &exist);
2025895Syz147064 	rw_exit(&softmac_hash_lock);
2035895Syz147064 	return (exist);
2045895Syz147064 }
2055895Syz147064 
2065895Syz147064 /*
2078275SEric Cheng  *
2088275SEric Cheng  * softmac_create() is called for each minor node during the post-attach of
2095895Syz147064  * each DDI_NT_NET device instance.  Note that it is possible that a device
2105895Syz147064  * instance has two minor nodes (DLPI style-1 and style-2), so that for that
2115895Syz147064  * specific device, softmac_create() could be called twice.
2125895Syz147064  *
2135895Syz147064  * A softmac_t is used to track each DDI_NT_NET device, and a softmac_dev_t
2145895Syz147064  * is created to track each minor node.
2155895Syz147064  *
2165895Syz147064  * For each minor node of a legacy device, a taskq is started to finish
2175895Syz147064  * softmac_mac_register(), which will finish the rest of work (see comments
2185895Syz147064  * above softmac_mac_register()).
2198275SEric Cheng  *
2208275SEric Cheng  *			softmac state machine
2218275SEric Cheng  * --------------------------------------------------------------------------
2228275SEric Cheng  * OLD STATE		EVENT					NEW STATE
2238275SEric Cheng  * --------------------------------------------------------------------------
2248275SEric Cheng  * UNINIT		attach of 1st minor node 		ATTACH_INPROG
2258275SEric Cheng  * okcnt = 0		net_postattach -> softmac_create	okcnt = 1
2268275SEric Cheng  *
2278275SEric Cheng  * ATTACH_INPROG	attach of 2nd minor node (GLDv3)	ATTACH_DONE
2288275SEric Cheng  * okcnt = 1		net_postattach -> softmac_create	okcnt = 2
2298275SEric Cheng  *
2308275SEric Cheng  * ATTACH_INPROG	attach of 2nd minor node (legacy)	ATTACH_INPROG
2318275SEric Cheng  * okcnt = 1		net_postattach -> softmac_create	okcnt = 2
2328275SEric Cheng  *			schedule softmac_mac_register
2338275SEric Cheng  *
2348275SEric Cheng  * ATTACH_INPROG	legacy device node			ATTACH_DONE
2358275SEric Cheng  * okcnt = 2		softmac_mac_register			okcnt = 2
2368275SEric Cheng  *
2378275SEric Cheng  * ATTACH_DONE		detach of 1st minor node		DETACH_INPROG
2388275SEric Cheng  * okcnt = 2		(success)				okcnt = 1
2398275SEric Cheng  *
2408275SEric Cheng  * DETACH_INPROG	detach of 2nd minor node		UNINIT (or free)
2418275SEric Cheng  * okcnt = 1		(success)				okcnt = 0
2428275SEric Cheng  *
2438275SEric Cheng  * ATTACH_DONE		detach failure				state unchanged
2448275SEric Cheng  * DETACH_INPROG						left = okcnt
2458275SEric Cheng  *
2468275SEric Cheng  * DETACH_INPROG	reattach				ATTACH_INPROG
2478275SEric Cheng  * okcnt = 0,1		net_postattach -> softmac_create
2488275SEric Cheng  *
2498275SEric Cheng  * ATTACH_DONE		reattach				ATTACH_DONE
2508275SEric Cheng  * left != 0		net_postattach -> softmac_create	left = 0
2518275SEric Cheng  *
2528275SEric Cheng  * Abbreviation notes:
2538275SEric Cheng  * states have SOFTMAC_ prefix,
2548275SEric Cheng  * okcnt - softmac_attach_okcnt,
2558275SEric Cheng  * left - softmac_attached_left
2565895Syz147064  */
2578275SEric Cheng 
2588275SEric Cheng #ifdef DEBUG
2598275SEric Cheng void
2608275SEric Cheng softmac_state_verify(softmac_t *softmac)
2618275SEric Cheng {
2628275SEric Cheng 	ASSERT(MUTEX_HELD(&softmac->smac_mutex));
2638275SEric Cheng 
2648275SEric Cheng 	/*
2658275SEric Cheng 	 * There are at most 2 minor nodes, one per DLPI style
2668275SEric Cheng 	 */
2678275SEric Cheng 	ASSERT(softmac->smac_cnt <= 2 && softmac->smac_attachok_cnt <= 2);
2688275SEric Cheng 
2698275SEric Cheng 	/*
2708275SEric Cheng 	 * The smac_attachok_cnt represents the number of attaches i.e. the
2718275SEric Cheng 	 * number of times net_postattach -> softmac_create() has been called
2728275SEric Cheng 	 * for a device instance.
2738275SEric Cheng 	 */
2748275SEric Cheng 	ASSERT(softmac->smac_attachok_cnt == SMAC_NONZERO_NODECNT(softmac));
2758275SEric Cheng 
2768275SEric Cheng 	/*
2778275SEric Cheng 	 * softmac_create (or softmac_mac_register) ->  softmac_create_datalink
2788275SEric Cheng 	 * happens only after all minor nodes have been attached
2798275SEric Cheng 	 */
2808275SEric Cheng 	ASSERT(softmac->smac_state != SOFTMAC_ATTACH_DONE ||
2818275SEric Cheng 	    softmac->smac_attachok_cnt == softmac->smac_cnt);
2828275SEric Cheng 
2838275SEric Cheng 	if (softmac->smac_attachok_cnt == 0) {
2848275SEric Cheng 		ASSERT(softmac->smac_state == SOFTMAC_UNINIT);
2858275SEric Cheng 		ASSERT(softmac->smac_mh == NULL);
2868275SEric Cheng 	} else if (softmac->smac_attachok_cnt < softmac->smac_cnt) {
2878275SEric Cheng 		ASSERT(softmac->smac_state == SOFTMAC_ATTACH_INPROG ||
2888275SEric Cheng 		    softmac->smac_state == SOFTMAC_DETACH_INPROG);
2898275SEric Cheng 		ASSERT(softmac->smac_mh == NULL);
2908275SEric Cheng 	} else {
2918275SEric Cheng 		/*
2928275SEric Cheng 		 * In the stable condition the state whould be
2938275SEric Cheng 		 * SOFTMAC_ATTACH_DONE. But there is a small transient window
2948275SEric Cheng 		 * in softmac_destroy where we change the state to
2958275SEric Cheng 		 * SOFTMAC_DETACH_INPROG and drop the lock before doing
2968275SEric Cheng 		 * the link destroy
2978275SEric Cheng 		 */
2988275SEric Cheng 		ASSERT(softmac->smac_attachok_cnt == softmac->smac_cnt);
2998275SEric Cheng 		ASSERT(softmac->smac_state != SOFTMAC_UNINIT);
3008275SEric Cheng 	}
3018275SEric Cheng 	if (softmac->smac_mh != NULL)
3028275SEric Cheng 		ASSERT(softmac->smac_attachok_cnt == softmac->smac_cnt);
3038275SEric Cheng }
3048275SEric Cheng #endif
3058275SEric Cheng 
3068275SEric Cheng #ifdef DEBUG
3078275SEric Cheng #define	SOFTMAC_STATE_VERIFY(softmac)	softmac_state_verify(softmac)
3088275SEric Cheng #else
3098275SEric Cheng #define	SOFTMAC_STATE_VERIFY(softmac)
3108275SEric Cheng #endif
3118275SEric Cheng 
3125895Syz147064 int
3135895Syz147064 softmac_create(dev_info_t *dip, dev_t dev)
3145895Syz147064 {
3155895Syz147064 	char		devname[MAXNAMELEN];
3165895Syz147064 	softmac_t	*softmac;
3175895Syz147064 	softmac_dev_t	*softmac_dev = NULL;
3185895Syz147064 	int		index;
3195895Syz147064 	int		ppa, err = 0;
3205895Syz147064 
3215895Syz147064 	/*
3225895Syz147064 	 * Force the softmac driver to be attached.
3235895Syz147064 	 */
3245895Syz147064 	if (i_ddi_attach_pseudo_node(SOFTMAC_DEV_NAME) == NULL) {
3255895Syz147064 		cmn_err(CE_WARN, "softmac_create:softmac attach fails");
3265895Syz147064 		return (ENXIO);
3275895Syz147064 	}
3285895Syz147064 
3295895Syz147064 	ppa = ddi_get_instance(dip);
3305895Syz147064 	(void) snprintf(devname, MAXNAMELEN, "%s%d", ddi_driver_name(dip), ppa);
3315895Syz147064 
3325895Syz147064 	/*
3335895Syz147064 	 * We expect legacy devices have at most two minor nodes - one style-1
3345895Syz147064 	 * and one style-2.
3355895Syz147064 	 */
3365895Syz147064 	if (!GLDV3_DRV(ddi_driver_major(dip)) &&
3375895Syz147064 	    i_ddi_minor_node_count(dip, DDI_NT_NET) > 2) {
3385895Syz147064 		cmn_err(CE_WARN, "%s has more than 2 minor nodes; unsupported",
3395895Syz147064 		    devname);
3405895Syz147064 		return (ENOTSUP);
3415895Syz147064 	}
3425895Syz147064 
3435895Syz147064 	/*
3445895Syz147064 	 * Check whether the softmac for the specified device already exists
3455895Syz147064 	 */
3465895Syz147064 	rw_enter(&softmac_hash_lock, RW_WRITER);
347*9073SCathy.Zhou@Sun.COM 	if ((mod_hash_find(softmac_hash, (mod_hash_key_t)devname,
3485895Syz147064 	    (mod_hash_val_t *)&softmac)) != 0) {
3495895Syz147064 
350*9073SCathy.Zhou@Sun.COM 		softmac = kmem_cache_alloc(softmac_cachep, KM_SLEEP);
3515895Syz147064 		(void) strlcpy(softmac->smac_devname, devname, MAXNAMELEN);
352*9073SCathy.Zhou@Sun.COM 
3535895Syz147064 		err = mod_hash_insert(softmac_hash,
3545895Syz147064 		    (mod_hash_key_t)softmac->smac_devname,
3555895Syz147064 		    (mod_hash_val_t)softmac);
3565895Syz147064 		ASSERT(err == 0);
3578275SEric Cheng 		mutex_enter(&smac_global_lock);
3588275SEric Cheng 		cv_broadcast(&smac_global_cv);
3598275SEric Cheng 		mutex_exit(&smac_global_lock);
3605895Syz147064 	}
3615895Syz147064 
3625895Syz147064 	mutex_enter(&softmac->smac_mutex);
3638275SEric Cheng 	SOFTMAC_STATE_VERIFY(softmac);
3648275SEric Cheng 	if (softmac->smac_state != SOFTMAC_ATTACH_DONE)
3658275SEric Cheng 		softmac->smac_state = SOFTMAC_ATTACH_INPROG;
3665895Syz147064 	if (softmac->smac_attachok_cnt == 0) {
3675895Syz147064 		/*
3685895Syz147064 		 * Initialize the softmac if this is the post-attach of the
3695895Syz147064 		 * first minor node.
3705895Syz147064 		 */
3715895Syz147064 		softmac->smac_flags = 0;
3725895Syz147064 		softmac->smac_umajor = ddi_driver_major(dip);
3735895Syz147064 		softmac->smac_uppa = ppa;
3745895Syz147064 
3755895Syz147064 		/*
3765895Syz147064 		 * Note that for GLDv3 devices, we create devfs minor nodes
3775895Syz147064 		 * for VLANs as well. Assume a GLDv3 driver on which only
3785895Syz147064 		 * a VLAN is created. During the detachment of this device
3795895Syz147064 		 * instance, the following would happen:
3805895Syz147064 		 * a. the pre-detach callback softmac_destroy() succeeds.
3815895Syz147064 		 *    Because the physical link itself is not in use,
3825895Syz147064 		 *    softmac_destroy() succeeds and destroys softmac_t;
3835895Syz147064 		 * b. the device detach fails in mac_unregister() because
3845895Syz147064 		 *    this MAC is still used by a VLAN.
3855895Syz147064 		 * c. the post-attach callback is then called which leads
3865895Syz147064 		 *    us here. Note that ddi_minor_node_count() returns 3
3875895Syz147064 		 *    (including the minior node of the VLAN). In that case,
3885895Syz147064 		 *    we must correct the minor node count to 2 as that is
3895895Syz147064 		 *    the count of minor nodes that go through post-attach.
3905895Syz147064 		 */
3915895Syz147064 		if (GLDV3_DRV(ddi_driver_major(dip))) {
3925895Syz147064 			softmac->smac_flags |= SOFTMAC_GLDV3;
3935895Syz147064 			softmac->smac_cnt = 2;
3945895Syz147064 		} else {
3955895Syz147064 			softmac->smac_cnt =
3965895Syz147064 			    i_ddi_minor_node_count(dip, DDI_NT_NET);
3975895Syz147064 		}
3985895Syz147064 	}
3995895Syz147064 
4005895Syz147064 	index = (getmajor(dev) == ddi_name_to_major("clone"));
4015895Syz147064 	if (softmac->smac_softmac[index] != NULL) {
4025895Syz147064 		/*
4038275SEric Cheng 		 * This is possible if the post_attach() is called after
4048275SEric Cheng 		 * pre_detach() fails. This seems to be a defect of the DACF
4058275SEric Cheng 		 * framework. We work around it by using a smac_attached_left
4068275SEric Cheng 		 * field that tracks this
4075895Syz147064 		 */
4088275SEric Cheng 		ASSERT(softmac->smac_attached_left != 0);
4098275SEric Cheng 		softmac->smac_attached_left--;
4105895Syz147064 		mutex_exit(&softmac->smac_mutex);
4115895Syz147064 		rw_exit(&softmac_hash_lock);
4125895Syz147064 		return (0);
4138275SEric Cheng 
4145895Syz147064 	}
4155895Syz147064 	mutex_exit(&softmac->smac_mutex);
4165895Syz147064 	rw_exit(&softmac_hash_lock);
4175895Syz147064 
4185895Syz147064 	softmac_dev = kmem_zalloc(sizeof (softmac_dev_t), KM_SLEEP);
4195895Syz147064 	softmac_dev->sd_dev = dev;
4208275SEric Cheng 
4218275SEric Cheng 	mutex_enter(&softmac->smac_mutex);
4225895Syz147064 	softmac->smac_softmac[index] = softmac_dev;
4235895Syz147064 	/*
4245895Syz147064 	 * Continue to register the mac and create the datalink only when all
4255895Syz147064 	 * the minor nodes are attached.
4265895Syz147064 	 */
4275895Syz147064 	if (++softmac->smac_attachok_cnt != softmac->smac_cnt) {
4285895Syz147064 		mutex_exit(&softmac->smac_mutex);
4295895Syz147064 		return (0);
4305895Syz147064 	}
4315895Syz147064 
4325895Syz147064 	/*
4337323SCathy.Zhou@Sun.COM 	 * All of the minor nodes have been attached; start a taskq
4348275SEric Cheng 	 * to do the rest of the work.  We use a taskq instead of
4357323SCathy.Zhou@Sun.COM 	 * doing the work here because:
4367323SCathy.Zhou@Sun.COM 	 *
4378275SEric Cheng 	 * We could be called as a result of a open() system call
4388275SEric Cheng 	 * where spec_open() already SLOCKED the snode. Using a taskq
4398275SEric Cheng 	 * sidesteps the risk that our ldi_open_by_dev() call would
4408275SEric Cheng 	 * deadlock trying to set SLOCKED on the snode again.
4417323SCathy.Zhou@Sun.COM 	 *
4428275SEric Cheng 	 * The devfs design requires that the downcalls don't use any
4438275SEric Cheng 	 * interruptible cv_wait which happens when we do door upcalls.
4448275SEric Cheng 	 * Otherwise the downcalls which may be holding devfs resources
4458275SEric Cheng 	 * may cause a deadlock if the thread is stopped. Also we need to make
4468275SEric Cheng 	 * sure these downcalls into softmac_create or softmac_destroy
4478275SEric Cheng 	 * don't cv_wait on any devfs related condition. Thus softmac_destroy
4488275SEric Cheng 	 * returns EBUSY if the asynchronous threads started in softmac_create
4498275SEric Cheng 	 * haven't finished.
4505895Syz147064 	 */
4518527SCathy.Zhou@Sun.COM 	(void) taskq_dispatch(system_taskq, softmac_create_task,
4528527SCathy.Zhou@Sun.COM 	    softmac, TQ_SLEEP);
4535895Syz147064 	mutex_exit(&softmac->smac_mutex);
4547323SCathy.Zhou@Sun.COM 	return (0);
4555895Syz147064 }
4565895Syz147064 
4575895Syz147064 static boolean_t
4585895Syz147064 softmac_m_getcapab(void *arg, mac_capab_t cap, void *cap_data)
4595895Syz147064 {
4605895Syz147064 	softmac_t *softmac = arg;
4615895Syz147064 
4625895Syz147064 	if (!(softmac->smac_capab_flags & cap))
4635895Syz147064 		return (B_FALSE);
4645895Syz147064 
4655895Syz147064 	switch (cap) {
4665895Syz147064 	case MAC_CAPAB_HCKSUM: {
4675895Syz147064 		uint32_t *txflags = cap_data;
4685895Syz147064 
4695895Syz147064 		*txflags = softmac->smac_hcksum_txflags;
4705895Syz147064 		break;
4715895Syz147064 	}
4725895Syz147064 	case MAC_CAPAB_LEGACY: {
4735895Syz147064 		mac_capab_legacy_t *legacy = cap_data;
4745895Syz147064 
475*9073SCathy.Zhou@Sun.COM 		/*
476*9073SCathy.Zhou@Sun.COM 		 * The caller is not interested in the details.
477*9073SCathy.Zhou@Sun.COM 		 */
478*9073SCathy.Zhou@Sun.COM 		if (legacy == NULL)
479*9073SCathy.Zhou@Sun.COM 			break;
480*9073SCathy.Zhou@Sun.COM 
4815895Syz147064 		legacy->ml_unsup_note = ~softmac->smac_notifications &
4825895Syz147064 		    (DL_NOTE_LINK_UP | DL_NOTE_LINK_DOWN | DL_NOTE_SPEED);
483*9073SCathy.Zhou@Sun.COM 		legacy->ml_active_set = softmac_active_set;
484*9073SCathy.Zhou@Sun.COM 		legacy->ml_active_clear = softmac_active_clear;
485*9073SCathy.Zhou@Sun.COM 		legacy->ml_fastpath_disable = softmac_fastpath_disable;
486*9073SCathy.Zhou@Sun.COM 		legacy->ml_fastpath_enable = softmac_fastpath_enable;
4875895Syz147064 		legacy->ml_dev = makedevice(softmac->smac_umajor,
4885895Syz147064 		    softmac->smac_uppa + 1);
4895895Syz147064 		break;
4905895Syz147064 	}
4915895Syz147064 
4925895Syz147064 	/*
4935895Syz147064 	 * For the capabilities below, there's nothing for us to fill in;
4945895Syz147064 	 * simply return B_TRUE if we support it.
4955895Syz147064 	 */
4965895Syz147064 	case MAC_CAPAB_NO_ZCOPY:
4975895Syz147064 	case MAC_CAPAB_NO_NATIVEVLAN:
4985895Syz147064 	default:
4995895Syz147064 		break;
5005895Syz147064 	}
5015895Syz147064 	return (B_TRUE);
5025895Syz147064 }
5035895Syz147064 
5045895Syz147064 static int
5055895Syz147064 softmac_update_info(softmac_t *softmac, datalink_id_t *linkidp)
5065895Syz147064 {
5075895Syz147064 	datalink_id_t	linkid = DATALINK_INVALID_LINKID;
5085895Syz147064 	uint32_t	media;
5095895Syz147064 	int		err;
5105895Syz147064 
5115895Syz147064 	if ((err = dls_mgmt_update(softmac->smac_devname, softmac->smac_media,
5125895Syz147064 	    softmac->smac_flags & SOFTMAC_NOSUPP, &media, &linkid)) == 0) {
5135895Syz147064 		*linkidp = linkid;
5145895Syz147064 	}
5155895Syz147064 
5165895Syz147064 	if (err == EEXIST) {
5175895Syz147064 		/*
5185895Syz147064 		 * There is a link name conflict.  Either:
5195895Syz147064 		 *
5205895Syz147064 		 * - An existing link with the same device name with a
5215895Syz147064 		 *   different media type from of the given type.
5225895Syz147064 		 *   Mark this link back to persistent only; or
5235895Syz147064 		 *
5245895Syz147064 		 * - We cannot assign the "suggested" name because
5255895Syz147064 		 *   GLDv3 and therefore vanity naming is not supported
5265895Syz147064 		 *   for this link type. Delete this link's <link name,
5275895Syz147064 		 *   linkid> mapping.
5285895Syz147064 		 */
5295895Syz147064 		if (media != softmac->smac_media) {
5305895Syz147064 			cmn_err(CE_WARN, "%s device %s conflicts with "
5315895Syz147064 			    "existing %s device %s.",
5325895Syz147064 			    dl_mactypestr(softmac->smac_media),
5335895Syz147064 			    softmac->smac_devname, dl_mactypestr(media),
5345895Syz147064 			    softmac->smac_devname);
5355895Syz147064 			(void) dls_mgmt_destroy(linkid, B_FALSE);
5365895Syz147064 		} else {
5375895Syz147064 			cmn_err(CE_WARN, "link name %s is already in-use.",
5385895Syz147064 			    softmac->smac_devname);
5395895Syz147064 			(void) dls_mgmt_destroy(linkid, B_TRUE);
5405895Syz147064 		}
5415895Syz147064 
5425895Syz147064 		cmn_err(CE_WARN, "%s device might not be available "
5435895Syz147064 		    "for use.", softmac->smac_devname);
5445895Syz147064 		cmn_err(CE_WARN, "See dladm(1M) for more information.");
5455895Syz147064 	}
5465895Syz147064 
5475895Syz147064 	return (err);
5485895Syz147064 }
5495895Syz147064 
5505895Syz147064 /*
5515895Syz147064  * This function:
5525895Syz147064  * 1. provides the link's media type to dlmgmtd.
5535895Syz147064  * 2. creates the GLDv3 datalink if the media type is supported by GLDv3.
5545895Syz147064  */
5555895Syz147064 static int
5565895Syz147064 softmac_create_datalink(softmac_t *softmac)
5575895Syz147064 {
5585895Syz147064 	datalink_id_t	linkid = DATALINK_INVALID_LINKID;
5595895Syz147064 	int		err;
5605895Syz147064 
5615895Syz147064 	/*
5627323SCathy.Zhou@Sun.COM 	 * Inform dlmgmtd of this link so that softmac_hold_device() is able
5637323SCathy.Zhou@Sun.COM 	 * to know the existence of this link. If this failed with EBADF,
5647323SCathy.Zhou@Sun.COM 	 * it might be because dlmgmtd was not started in time (e.g.,
5657323SCathy.Zhou@Sun.COM 	 * diskless boot); ignore the failure and continue to create
5667323SCathy.Zhou@Sun.COM 	 * the GLDv3 datalink if needed.
5675895Syz147064 	 */
5687323SCathy.Zhou@Sun.COM 	err = dls_mgmt_create(softmac->smac_devname,
5697323SCathy.Zhou@Sun.COM 	    makedevice(softmac->smac_umajor, softmac->smac_uppa + 1),
5707323SCathy.Zhou@Sun.COM 	    DATALINK_CLASS_PHYS, DL_OTHER, B_TRUE, &linkid);
5717323SCathy.Zhou@Sun.COM 	if (err != 0 && err != EBADF)
5727323SCathy.Zhou@Sun.COM 		return (err);
5737323SCathy.Zhou@Sun.COM 
5747323SCathy.Zhou@Sun.COM 	/*
5757323SCathy.Zhou@Sun.COM 	 * Provide the media type of the physical link to dlmgmtd.
5767323SCathy.Zhou@Sun.COM 	 */
5777323SCathy.Zhou@Sun.COM 	if ((err != EBADF) &&
5787323SCathy.Zhou@Sun.COM 	    ((err = softmac_update_info(softmac, &linkid)) != 0)) {
5795895Syz147064 		return (err);
5805895Syz147064 	}
5815895Syz147064 
5825895Syz147064 	/*
5835895Syz147064 	 * Create the GLDv3 datalink.
5845895Syz147064 	 */
5855895Syz147064 	if ((!(softmac->smac_flags & SOFTMAC_NOSUPP)) &&
5865895Syz147064 	    ((err = dls_devnet_create(softmac->smac_mh, linkid)) != 0)) {
5875895Syz147064 		cmn_err(CE_WARN, "dls_devnet_create failed for %s",
5885895Syz147064 		    softmac->smac_devname);
5895895Syz147064 		return (err);
5905895Syz147064 	}
5915895Syz147064 
5928275SEric Cheng 	if (linkid == DATALINK_INVALID_LINKID) {
5938275SEric Cheng 		mutex_enter(&softmac->smac_mutex);
5945895Syz147064 		softmac->smac_flags |= SOFTMAC_NEED_RECREATE;
5958275SEric Cheng 		mutex_exit(&softmac->smac_mutex);
5968275SEric Cheng 	}
5975895Syz147064 
5985895Syz147064 	return (0);
5995895Syz147064 }
6005895Syz147064 
6017323SCathy.Zhou@Sun.COM static void
6027323SCathy.Zhou@Sun.COM softmac_create_task(void *arg)
6037323SCathy.Zhou@Sun.COM {
6047323SCathy.Zhou@Sun.COM 	softmac_t	*softmac = arg;
6057323SCathy.Zhou@Sun.COM 	mac_handle_t	mh;
6067323SCathy.Zhou@Sun.COM 	int		err;
6077323SCathy.Zhou@Sun.COM 
6087323SCathy.Zhou@Sun.COM 	if (!GLDV3_DRV(softmac->smac_umajor)) {
6097323SCathy.Zhou@Sun.COM 		softmac_mac_register(softmac);
6107323SCathy.Zhou@Sun.COM 		return;
6117323SCathy.Zhou@Sun.COM 	}
6127323SCathy.Zhou@Sun.COM 
6137323SCathy.Zhou@Sun.COM 	if ((err = mac_open(softmac->smac_devname, &mh)) != 0)
6147323SCathy.Zhou@Sun.COM 		goto done;
6157323SCathy.Zhou@Sun.COM 
6167323SCathy.Zhou@Sun.COM 	mutex_enter(&softmac->smac_mutex);
6177323SCathy.Zhou@Sun.COM 	softmac->smac_media = (mac_info(mh))->mi_nativemedia;
6187323SCathy.Zhou@Sun.COM 	softmac->smac_mh = mh;
6198275SEric Cheng 	mutex_exit(&softmac->smac_mutex);
6207323SCathy.Zhou@Sun.COM 
6217323SCathy.Zhou@Sun.COM 	/*
6227323SCathy.Zhou@Sun.COM 	 * We can safely release the reference on the mac because
6237323SCathy.Zhou@Sun.COM 	 * this mac will only be unregistered and destroyed when
6247323SCathy.Zhou@Sun.COM 	 * the device detaches, and the softmac will be destroyed
6257323SCathy.Zhou@Sun.COM 	 * before then (in the pre-detach routine of the device).
6267323SCathy.Zhou@Sun.COM 	 */
6277323SCathy.Zhou@Sun.COM 	mac_close(mh);
6287323SCathy.Zhou@Sun.COM 
6297323SCathy.Zhou@Sun.COM 	/*
6307323SCathy.Zhou@Sun.COM 	 * Create the GLDv3 datalink for this mac.
6317323SCathy.Zhou@Sun.COM 	 */
6327323SCathy.Zhou@Sun.COM 	err = softmac_create_datalink(softmac);
6337323SCathy.Zhou@Sun.COM 
6348527SCathy.Zhou@Sun.COM done:
6358275SEric Cheng 	mutex_enter(&softmac->smac_mutex);
6368527SCathy.Zhou@Sun.COM 	if (err != 0)
6378275SEric Cheng 		softmac->smac_mh = NULL;
6388527SCathy.Zhou@Sun.COM 	softmac->smac_attacherr = err;
6398275SEric Cheng 	softmac->smac_state = SOFTMAC_ATTACH_DONE;
6407323SCathy.Zhou@Sun.COM 	cv_broadcast(&softmac->smac_cv);
6417323SCathy.Zhou@Sun.COM 	mutex_exit(&softmac->smac_mutex);
6427323SCathy.Zhou@Sun.COM }
6437323SCathy.Zhou@Sun.COM 
6445895Syz147064 /*
6455895Syz147064  * This function is only called for legacy devices. It:
6465895Syz147064  * 1. registers the MAC for the legacy devices whose media type is supported
6475895Syz147064  *    by the GLDv3 framework.
6485895Syz147064  * 2. creates the GLDv3 datalink if the media type is supported by GLDv3.
6495895Syz147064  */
6505895Syz147064 static void
6517323SCathy.Zhou@Sun.COM softmac_mac_register(softmac_t *softmac)
6525895Syz147064 {
6535895Syz147064 	softmac_dev_t	*softmac_dev;
6545895Syz147064 	dev_t		dev;
6555895Syz147064 	ldi_handle_t	lh = NULL;
6565895Syz147064 	ldi_ident_t	li = NULL;
6575895Syz147064 	int		index;
6585895Syz147064 	boolean_t	native_vlan = B_FALSE;
6595895Syz147064 	int		err;
6605895Syz147064 
6615895Syz147064 	/*
6625895Syz147064 	 * Note that we do not need any locks to access this softmac pointer,
6635895Syz147064 	 * as softmac_destroy() will wait until this function is called.
6645895Syz147064 	 */
6655895Syz147064 	ASSERT(softmac != NULL);
6668275SEric Cheng 	ASSERT(softmac->smac_state == SOFTMAC_ATTACH_INPROG &&
6678275SEric Cheng 	    softmac->smac_attachok_cnt == softmac->smac_cnt);
6685895Syz147064 
6695895Syz147064 	if ((err = ldi_ident_from_dip(softmac_dip, &li)) != 0) {
6705895Syz147064 		mutex_enter(&softmac->smac_mutex);
6715895Syz147064 		goto done;
6725895Syz147064 	}
6735895Syz147064 
6745895Syz147064 	/*
6755895Syz147064 	 * Determine whether this legacy device support VLANs by opening
6765895Syz147064 	 * the style-2 device node (if it exists) and attaching to a VLAN
6775895Syz147064 	 * PPA (1000 + ppa).
6785895Syz147064 	 */
6795895Syz147064 	dev = makedevice(ddi_name_to_major("clone"), softmac->smac_umajor);
6805895Syz147064 	err = ldi_open_by_dev(&dev, OTYP_CHR, FREAD|FWRITE, kcred, &lh, li);
6815895Syz147064 	if (err == 0) {
6825895Syz147064 		if (dl_attach(lh, softmac->smac_uppa + 1 * 1000, NULL) == 0)
6835895Syz147064 			native_vlan = B_TRUE;
6845895Syz147064 		(void) ldi_close(lh, FREAD|FWRITE, kcred);
6855895Syz147064 	}
6865895Syz147064 
6875895Syz147064 	err = EINVAL;
6885895Syz147064 	for (index = 0; index < 2; index++) {
6895895Syz147064 		dl_info_ack_t	dlia;
6905895Syz147064 		dl_error_ack_t	dlea;
6915895Syz147064 		uint32_t	notes;
6925895Syz147064 		struct strioctl	iocb;
6935895Syz147064 		uint32_t	margin;
6945895Syz147064 		int		rval;
6955895Syz147064 
6965895Syz147064 		if ((softmac_dev = softmac->smac_softmac[index]) == NULL)
6975895Syz147064 			continue;
6985895Syz147064 
6995895Syz147064 		softmac->smac_dev = dev = softmac_dev->sd_dev;
7005895Syz147064 		if (ldi_open_by_dev(&dev, OTYP_CHR, FREAD|FWRITE, kcred, &lh,
7015895Syz147064 		    li) != 0) {
7025895Syz147064 			continue;
7035895Syz147064 		}
7045895Syz147064 
7055895Syz147064 		/*
7065895Syz147064 		 * Pop all the intermediate modules in order to negotiate
7075895Syz147064 		 * capabilities correctly.
7085895Syz147064 		 */
7095895Syz147064 		while (ldi_ioctl(lh, I_POP, 0, FKIOCTL, kcred, &rval) == 0)
7105895Syz147064 			;
7115895Syz147064 
7125895Syz147064 		/* DLPI style-1 or DLPI style-2? */
7135895Syz147064 		if ((rval = dl_info(lh, &dlia, NULL, NULL, &dlea)) != 0) {
7145895Syz147064 			if (rval == ENOTSUP) {
7155895Syz147064 				cmn_err(CE_NOTE, "softmac: received "
7165895Syz147064 				    "DL_ERROR_ACK to DL_INFO_ACK; "
7175895Syz147064 				    "DLPI errno 0x%x, UNIX errno %d",
7185895Syz147064 				    dlea.dl_errno, dlea.dl_unix_errno);
7195895Syz147064 			}
7205895Syz147064 			(void) ldi_close(lh, FREAD|FWRITE, kcred);
7215895Syz147064 			continue;
7225895Syz147064 		}
7235895Syz147064 
7245895Syz147064 		/*
7255895Syz147064 		 * Currently only DL_ETHER has GLDv3 mac plugin support.
7265895Syz147064 		 * For media types that GLDv3 does not support, create a
7275895Syz147064 		 * link id for it.
7285895Syz147064 		 */
7295895Syz147064 		if ((softmac->smac_media = dlia.dl_mac_type) != DL_ETHER) {
7305895Syz147064 			(void) ldi_close(lh, FREAD|FWRITE, kcred);
7315895Syz147064 			err = 0;
7325895Syz147064 			break;
7335895Syz147064 		}
7345895Syz147064 
7355895Syz147064 		if ((dlia.dl_provider_style == DL_STYLE2) &&
7365895Syz147064 		    (dl_attach(lh, softmac->smac_uppa, NULL) != 0)) {
7375895Syz147064 			(void) ldi_close(lh, FREAD|FWRITE, kcred);
7385895Syz147064 			continue;
7395895Syz147064 		}
7405895Syz147064 
7415895Syz147064 		if ((rval = dl_bind(lh, 0, NULL)) != 0) {
7425895Syz147064 			if (rval == ENOTSUP) {
7435895Syz147064 				cmn_err(CE_NOTE, "softmac: received "
7445895Syz147064 				    "DL_ERROR_ACK to DL_BIND_ACK; "
7455895Syz147064 				    "DLPI errno 0x%x, UNIX errno %d",
7465895Syz147064 				    dlea.dl_errno, dlea.dl_unix_errno);
7475895Syz147064 			}
7485895Syz147064 			(void) ldi_close(lh, FREAD|FWRITE, kcred);
7495895Syz147064 			continue;
7505895Syz147064 		}
7515895Syz147064 
7525895Syz147064 		/*
7535895Syz147064 		 * Call dl_info() after dl_bind() because some drivers only
7545895Syz147064 		 * provide correct information (e.g. MAC address) once bound.
7555895Syz147064 		 */
7565895Syz147064 		softmac->smac_addrlen = sizeof (softmac->smac_unicst_addr);
7575895Syz147064 		if ((rval = dl_info(lh, &dlia, softmac->smac_unicst_addr,
7585895Syz147064 		    &softmac->smac_addrlen, &dlea)) != 0) {
7595895Syz147064 			if (rval == ENOTSUP) {
7605895Syz147064 				cmn_err(CE_NOTE, "softmac: received "
7615895Syz147064 				    "DL_ERROR_ACK to DL_INFO_ACK; "
7625895Syz147064 				    "DLPI errno 0x%x, UNIX errno %d",
7635895Syz147064 				    dlea.dl_errno, dlea.dl_unix_errno);
7645895Syz147064 			}
7655895Syz147064 			(void) ldi_close(lh, FREAD|FWRITE, kcred);
7665895Syz147064 			continue;
7675895Syz147064 		}
7685895Syz147064 
7695895Syz147064 		softmac->smac_style = dlia.dl_provider_style;
7705895Syz147064 		softmac->smac_saplen = ABS(dlia.dl_sap_length);
7715895Syz147064 		softmac->smac_min_sdu = dlia.dl_min_sdu;
7725895Syz147064 		softmac->smac_max_sdu = dlia.dl_max_sdu;
7735895Syz147064 
7745895Syz147064 		if ((softmac->smac_saplen != sizeof (uint16_t)) ||
7755895Syz147064 		    (softmac->smac_addrlen != ETHERADDRL) ||
7765895Syz147064 		    (dlia.dl_brdcst_addr_length != ETHERADDRL) ||
7775895Syz147064 		    (dlia.dl_brdcst_addr_offset == 0)) {
7785895Syz147064 			(void) ldi_close(lh, FREAD|FWRITE, kcred);
7795895Syz147064 			continue;
7805895Syz147064 		}
7815895Syz147064 
7825895Syz147064 		/*
7835895Syz147064 		 * Check other DLPI capabilities. Note that this must be after
7845895Syz147064 		 * dl_bind() because some drivers return DL_ERROR_ACK if the
7855895Syz147064 		 * stream is not bound. It is also before mac_register(), so
7865895Syz147064 		 * we don't need any lock protection here.
7875895Syz147064 		 */
7885895Syz147064 		softmac->smac_capab_flags =
7898275SEric Cheng 		    (MAC_CAPAB_NO_ZCOPY | MAC_CAPAB_LEGACY);
7905895Syz147064 
7915895Syz147064 		softmac->smac_no_capability_req = B_FALSE;
7925895Syz147064 		if (softmac_fill_capab(lh, softmac) != 0)
7935895Syz147064 			softmac->smac_no_capability_req = B_TRUE;
7945895Syz147064 
7955895Syz147064 		/*
7965895Syz147064 		 * Check the margin of the underlying driver.
7975895Syz147064 		 */
7985895Syz147064 		margin = 0;
7995895Syz147064 		iocb.ic_cmd = DLIOCMARGININFO;
8005895Syz147064 		iocb.ic_timout = INFTIM;
8015895Syz147064 		iocb.ic_len = sizeof (margin);
8025895Syz147064 		iocb.ic_dp = (char *)&margin;
8035895Syz147064 		softmac->smac_margin = 0;
8045895Syz147064 
8055895Syz147064 		if (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, kcred,
8065895Syz147064 		    &rval) == 0) {
8075895Syz147064 			softmac->smac_margin = margin;
8085895Syz147064 		}
8095895Syz147064 
8105895Syz147064 		/*
8115895Syz147064 		 * If the legacy driver doesn't support DLIOCMARGININFO, but
8125895Syz147064 		 * it can support native VLAN, correct its margin value to 4.
8135895Syz147064 		 */
8145895Syz147064 		if (native_vlan) {
8155895Syz147064 			if (softmac->smac_margin == 0)
8165895Syz147064 				softmac->smac_margin = VLAN_TAGSZ;
8175895Syz147064 		} else {
8185895Syz147064 			softmac->smac_capab_flags |= MAC_CAPAB_NO_NATIVEVLAN;
8195895Syz147064 		}
8205895Syz147064 
8215895Syz147064 		/*
8225895Syz147064 		 * Not all drivers support DL_NOTIFY_REQ, so ignore ENOTSUP.
8235895Syz147064 		 */
8245895Syz147064 		softmac->smac_notifications = 0;
8255895Syz147064 		notes = DL_NOTE_PHYS_ADDR | DL_NOTE_LINK_UP | DL_NOTE_LINK_DOWN;
8265895Syz147064 		switch (dl_notify(lh, &notes, NULL)) {
8275895Syz147064 		case 0:
8285895Syz147064 			softmac->smac_notifications = notes;
8295895Syz147064 			break;
8305895Syz147064 		case ENOTSUP:
8315895Syz147064 			break;
8325895Syz147064 		default:
8335895Syz147064 			(void) ldi_close(lh, FREAD|FWRITE, kcred);
8345895Syz147064 			continue;
8355895Syz147064 		}
8365895Syz147064 
8375895Syz147064 		(void) ldi_close(lh, FREAD|FWRITE, kcred);
8385895Syz147064 		err = 0;
8395895Syz147064 		break;
8405895Syz147064 	}
8415895Syz147064 	ldi_ident_release(li);
8425895Syz147064 
8435895Syz147064 	mutex_enter(&softmac->smac_mutex);
8445895Syz147064 
8455895Syz147064 	if (err != 0)
8465895Syz147064 		goto done;
8475895Syz147064 
8485895Syz147064 	if (softmac->smac_media != DL_ETHER)
8495895Syz147064 		softmac->smac_flags |= SOFTMAC_NOSUPP;
8505895Syz147064 
8515895Syz147064 	/*
8525895Syz147064 	 * Finally, we're ready to register ourselves with the MAC layer
8535895Syz147064 	 * interface; if this succeeds, we're all ready to start()
8545895Syz147064 	 */
8555895Syz147064 	if (!(softmac->smac_flags & SOFTMAC_NOSUPP)) {
8565895Syz147064 		mac_register_t	*macp;
8575895Syz147064 
8585895Syz147064 		if ((macp = mac_alloc(MAC_VERSION)) == NULL) {
8595895Syz147064 			err = ENOMEM;
8605895Syz147064 			goto done;
8615895Syz147064 		}
8625895Syz147064 
8635895Syz147064 		macp->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
8645895Syz147064 		macp->m_driver = softmac;
8655895Syz147064 		macp->m_dip = softmac_dip;
8665895Syz147064 
8675895Syz147064 		macp->m_margin = softmac->smac_margin;
8685895Syz147064 		macp->m_src_addr = softmac->smac_unicst_addr;
8695895Syz147064 		macp->m_min_sdu = softmac->smac_min_sdu;
8705895Syz147064 		macp->m_max_sdu = softmac->smac_max_sdu;
8715895Syz147064 		macp->m_callbacks = &softmac_m_callbacks;
8725895Syz147064 		macp->m_instance = (uint_t)-1;
8735895Syz147064 
8745895Syz147064 		err = mac_register(macp, &softmac->smac_mh);
8755895Syz147064 		mac_free(macp);
8765895Syz147064 		if (err != 0) {
8775895Syz147064 			cmn_err(CE_WARN, "mac_register failed for %s",
8785895Syz147064 			    softmac->smac_devname);
8795895Syz147064 			goto done;
8805895Syz147064 		}
8815895Syz147064 	}
8828275SEric Cheng 	mutex_exit(&softmac->smac_mutex);
8835895Syz147064 
8845895Syz147064 	/*
8855895Syz147064 	 * Try to create the datalink for this softmac.
8865895Syz147064 	 */
8875895Syz147064 	if ((err = softmac_create_datalink(softmac)) != 0) {
888*9073SCathy.Zhou@Sun.COM 		if (!(softmac->smac_flags & SOFTMAC_NOSUPP))
8895895Syz147064 			(void) mac_unregister(softmac->smac_mh);
890*9073SCathy.Zhou@Sun.COM 		mutex_enter(&softmac->smac_mutex);
891*9073SCathy.Zhou@Sun.COM 		softmac->smac_mh = NULL;
892*9073SCathy.Zhou@Sun.COM 		goto done;
8935895Syz147064 	}
8948275SEric Cheng 	/*
8958275SEric Cheng 	 * If succeed, create the thread which handles the DL_NOTIFY_IND from
8968275SEric Cheng 	 * the lower stream.
8978275SEric Cheng 	 */
898*9073SCathy.Zhou@Sun.COM 	mutex_enter(&softmac->smac_mutex);
8998275SEric Cheng 	if (softmac->smac_mh != NULL) {
9008275SEric Cheng 		softmac->smac_notify_thread = thread_create(NULL, 0,
9018275SEric Cheng 		    softmac_notify_thread, softmac, 0, &p0,
9028275SEric Cheng 		    TS_RUN, minclsyspri);
9038275SEric Cheng 	}
9045895Syz147064 
9055895Syz147064 done:
9068275SEric Cheng 	ASSERT(softmac->smac_state == SOFTMAC_ATTACH_INPROG &&
9078275SEric Cheng 	    softmac->smac_attachok_cnt == softmac->smac_cnt);
9088275SEric Cheng 	softmac->smac_state = SOFTMAC_ATTACH_DONE;
9095895Syz147064 	softmac->smac_attacherr = err;
9105895Syz147064 	cv_broadcast(&softmac->smac_cv);
9115895Syz147064 	mutex_exit(&softmac->smac_mutex);
9125895Syz147064 }
9135895Syz147064 
9145895Syz147064 int
9155895Syz147064 softmac_destroy(dev_info_t *dip, dev_t dev)
9165895Syz147064 {
9175895Syz147064 	char			devname[MAXNAMELEN];
9185895Syz147064 	softmac_t		*softmac;
9195895Syz147064 	softmac_dev_t		*softmac_dev;
9205895Syz147064 	int			index;
9215895Syz147064 	int			ppa, err;
9225895Syz147064 	datalink_id_t		linkid;
9238275SEric Cheng 	mac_handle_t		smac_mh;
9248275SEric Cheng 	uint32_t		smac_flags;
9255895Syz147064 
9265895Syz147064 	ppa = ddi_get_instance(dip);
9275895Syz147064 	(void) snprintf(devname, MAXNAMELEN, "%s%d", ddi_driver_name(dip), ppa);
9285895Syz147064 
9298275SEric Cheng 	/*
9308275SEric Cheng 	 * We are called only from the predetach entry point. The DACF
9318275SEric Cheng 	 * framework ensures there can't be a concurrent postattach call
9328275SEric Cheng 	 * for the same softmac. The softmac found out from the modhash
9338275SEric Cheng 	 * below can't vanish beneath us since this is the only place where
9348275SEric Cheng 	 * it is deleted.
9358275SEric Cheng 	 */
9365895Syz147064 	err = mod_hash_find(softmac_hash, (mod_hash_key_t)devname,
9375895Syz147064 	    (mod_hash_val_t *)&softmac);
9385895Syz147064 	ASSERT(err == 0);
9395895Syz147064 
9405895Syz147064 	mutex_enter(&softmac->smac_mutex);
9418275SEric Cheng 	SOFTMAC_STATE_VERIFY(softmac);
9425895Syz147064 
9435895Syz147064 	/*
9445895Syz147064 	 * Fail the predetach routine if this softmac is in-use.
9458275SEric Cheng 	 * Make sure these downcalls into softmac_create or softmac_destroy
9468275SEric Cheng 	 * don't cv_wait on any devfs related condition. Thus softmac_destroy
9478275SEric Cheng 	 * returns EBUSY if the asynchronous thread started in softmac_create
9488275SEric Cheng 	 * hasn't finished
9495895Syz147064 	 */
9508275SEric Cheng 	if ((softmac->smac_hold_cnt != 0) ||
9518275SEric Cheng 	    (softmac->smac_state == SOFTMAC_ATTACH_INPROG)) {
9525895Syz147064 		softmac->smac_attached_left = softmac->smac_attachok_cnt;
9535895Syz147064 		mutex_exit(&softmac->smac_mutex);
9545895Syz147064 		return (EBUSY);
9555895Syz147064 	}
9565895Syz147064 
9575895Syz147064 	/*
9585895Syz147064 	 * Even if the predetach of one minor node has already failed
9595895Syz147064 	 * (smac_attached_left is not 0), the DACF framework will continue
9605895Syz147064 	 * to call the predetach routines of the other minor nodes,
9615895Syz147064 	 * so we fail these calls here.
9625895Syz147064 	 */
9635895Syz147064 	if (softmac->smac_attached_left != 0) {
9645895Syz147064 		mutex_exit(&softmac->smac_mutex);
9655895Syz147064 		return (EBUSY);
9665895Syz147064 	}
9675895Syz147064 
9688275SEric Cheng 	smac_mh = softmac->smac_mh;
9698275SEric Cheng 	smac_flags = softmac->smac_flags;
9708275SEric Cheng 	softmac->smac_state = SOFTMAC_DETACH_INPROG;
9718275SEric Cheng 	mutex_exit(&softmac->smac_mutex);
9725895Syz147064 
9738275SEric Cheng 	if (smac_mh != NULL) {
9748275SEric Cheng 		/*
9758275SEric Cheng 		 * This is the first minor node that is being detached for this
9768275SEric Cheng 		 * softmac.
9778275SEric Cheng 		 */
9788275SEric Cheng 		ASSERT(softmac->smac_attachok_cnt == softmac->smac_cnt);
9798275SEric Cheng 		if (!(smac_flags & SOFTMAC_NOSUPP)) {
9808275SEric Cheng 			if ((err = dls_devnet_destroy(smac_mh, &linkid,
9818275SEric Cheng 			    B_FALSE)) != 0) {
9828275SEric Cheng 				goto error;
9835895Syz147064 			}
9845895Syz147064 		}
9855895Syz147064 		/*
9865895Syz147064 		 * If softmac_mac_register() succeeds in registering the mac
9875895Syz147064 		 * of the legacy device, unregister it.
9885895Syz147064 		 */
9898275SEric Cheng 		if (!(smac_flags & (SOFTMAC_GLDV3 | SOFTMAC_NOSUPP))) {
9908275SEric Cheng 			if ((err = mac_disable_nowait(smac_mh)) != 0) {
9918275SEric Cheng 				(void) dls_devnet_create(smac_mh, linkid);
9928275SEric Cheng 				goto error;
9935895Syz147064 			}
9948275SEric Cheng 			/*
9958275SEric Cheng 			 * Ask softmac_notify_thread to quit, and wait for
9968275SEric Cheng 			 * that to be done.
9978275SEric Cheng 			 */
9988275SEric Cheng 			mutex_enter(&softmac->smac_mutex);
9998275SEric Cheng 			softmac->smac_flags |= SOFTMAC_NOTIFY_QUIT;
10008275SEric Cheng 			cv_broadcast(&softmac->smac_cv);
10018275SEric Cheng 			while (softmac->smac_notify_thread != NULL) {
10028275SEric Cheng 				cv_wait(&softmac->smac_cv,
10038275SEric Cheng 				    &softmac->smac_mutex);
10048275SEric Cheng 			}
10058275SEric Cheng 			mutex_exit(&softmac->smac_mutex);
10068275SEric Cheng 			VERIFY(mac_unregister(smac_mh) == 0);
10075895Syz147064 		}
10085895Syz147064 		softmac->smac_mh = NULL;
10095895Syz147064 	}
10108275SEric Cheng 
10118275SEric Cheng 	/*
10128275SEric Cheng 	 * Free softmac_dev
10138275SEric Cheng 	 */
10148275SEric Cheng 	rw_enter(&softmac_hash_lock, RW_WRITER);
10158275SEric Cheng 	mutex_enter(&softmac->smac_mutex);
10165895Syz147064 
10178275SEric Cheng 	ASSERT(softmac->smac_state == SOFTMAC_DETACH_INPROG &&
10188275SEric Cheng 	    softmac->smac_attachok_cnt != 0);
10198275SEric Cheng 	softmac->smac_mh = NULL;
10208275SEric Cheng 	index = (getmajor(dev) == ddi_name_to_major("clone"));
10218275SEric Cheng 	softmac_dev = softmac->smac_softmac[index];
10228275SEric Cheng 	ASSERT(softmac_dev != NULL);
10238275SEric Cheng 	softmac->smac_softmac[index] = NULL;
10248275SEric Cheng 	kmem_free(softmac_dev, sizeof (softmac_dev_t));
10255895Syz147064 
10268275SEric Cheng 	if (--softmac->smac_attachok_cnt == 0) {
10278275SEric Cheng 		mod_hash_val_t	hashval;
10285895Syz147064 
10298275SEric Cheng 		softmac->smac_state = SOFTMAC_UNINIT;
10308275SEric Cheng 		if (softmac->smac_hold_cnt != 0) {
10318275SEric Cheng 			/*
10328275SEric Cheng 			 * Someone did a softmac_hold_device while we dropped
10338275SEric Cheng 			 * the locks. Leave the softmac itself intact which
10348275SEric Cheng 			 * will be reused by the reattach
10358275SEric Cheng 			 */
10365895Syz147064 			mutex_exit(&softmac->smac_mutex);
10375895Syz147064 			rw_exit(&softmac_hash_lock);
10385895Syz147064 			return (0);
10395895Syz147064 		}
10408275SEric Cheng 		err = mod_hash_remove(softmac_hash,
10418275SEric Cheng 		    (mod_hash_key_t)devname,
10428275SEric Cheng 		    (mod_hash_val_t *)&hashval);
10438275SEric Cheng 		ASSERT(err == 0);
10448275SEric Cheng 
10458275SEric Cheng 		mutex_exit(&softmac->smac_mutex);
10468275SEric Cheng 		rw_exit(&softmac_hash_lock);
1047*9073SCathy.Zhou@Sun.COM 		ASSERT(softmac->smac_fp_disable_clients == 0);
1048*9073SCathy.Zhou@Sun.COM 		softmac->smac_fastpath_admin_disabled = B_FALSE;
1049*9073SCathy.Zhou@Sun.COM 		kmem_cache_free(softmac_cachep, softmac);
10508275SEric Cheng 		return (0);
10515895Syz147064 	}
10525895Syz147064 	mutex_exit(&softmac->smac_mutex);
10535895Syz147064 	rw_exit(&softmac_hash_lock);
10548275SEric Cheng 	return (0);
10558275SEric Cheng 
10568275SEric Cheng error:
10578275SEric Cheng 	mutex_enter(&softmac->smac_mutex);
10588275SEric Cheng 	softmac->smac_attached_left = softmac->smac_attachok_cnt;
10598275SEric Cheng 	softmac->smac_state = SOFTMAC_ATTACH_DONE;
10608275SEric Cheng 	cv_broadcast(&softmac->smac_cv);
10618275SEric Cheng 	mutex_exit(&softmac->smac_mutex);
10625895Syz147064 	return (err);
10635895Syz147064 }
10645895Syz147064 
10655895Syz147064 /*
10665895Syz147064  * This function is called as the result of a newly started dlmgmtd daemon.
10675895Syz147064  *
10685895Syz147064  * We walk through every softmac that was created but failed to notify
10695895Syz147064  * dlmgmtd about it (whose SOFTMAC_NEED_RECREATE flag is set).  This occurs
10705895Syz147064  * when softmacs are created before dlmgmtd is ready.  For example, during
10715895Syz147064  * diskless boot, a network device is used (and therefore attached) before
10725895Syz147064  * the datalink-management service starts dlmgmtd.
10735895Syz147064  */
10745895Syz147064 /* ARGSUSED */
10755895Syz147064 static uint_t
10765895Syz147064 softmac_mac_recreate(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
10775895Syz147064 {
10785895Syz147064 	softmac_t	*softmac = (softmac_t *)val;
10795895Syz147064 	datalink_id_t	linkid;
10805895Syz147064 	int		err;
10818275SEric Cheng 	softmac_walk_t	*smwp = arg;
10825895Syz147064 
10835895Syz147064 	/*
10848275SEric Cheng 	 * The framework itself must not hold any locks across calls to the
10858275SEric Cheng 	 * mac perimeter. Thus this function does not call any framework
10868275SEric Cheng 	 * function that needs to grab the mac perimeter.
10875895Syz147064 	 */
10888275SEric Cheng 	ASSERT(RW_READ_HELD(&softmac_hash_lock));
10898275SEric Cheng 
10908275SEric Cheng 	smwp->smw_retry = B_FALSE;
10915895Syz147064 	mutex_enter(&softmac->smac_mutex);
10928275SEric Cheng 	SOFTMAC_STATE_VERIFY(softmac);
10938275SEric Cheng 	if (softmac->smac_state == SOFTMAC_ATTACH_INPROG) {
10948275SEric Cheng 		/*
10958275SEric Cheng 		 * Wait till softmac_create or softmac_mac_register finishes
10968275SEric Cheng 		 * Hold the softmac to ensure it stays around. The wait itself
10978275SEric Cheng 		 * is done in the caller, since we need to drop all locks
10988275SEric Cheng 		 * including the mod hash's internal lock before calling
10998275SEric Cheng 		 * cv_wait.
11008275SEric Cheng 		 */
11018275SEric Cheng 		smwp->smw_retry = B_TRUE;
11028275SEric Cheng 		smwp->smw_softmac = softmac;
11038275SEric Cheng 		softmac->smac_hold_cnt++;
11048275SEric Cheng 		return (MH_WALK_TERMINATE);
11058275SEric Cheng 	}
11065895Syz147064 
11078275SEric Cheng 	if ((softmac->smac_state != SOFTMAC_ATTACH_DONE) ||
11085895Syz147064 	    !(softmac->smac_flags & SOFTMAC_NEED_RECREATE)) {
11095895Syz147064 		mutex_exit(&softmac->smac_mutex);
11105895Syz147064 		return (MH_WALK_CONTINUE);
11115895Syz147064 	}
11125895Syz147064 
11138833SVenu.Iyer@Sun.COM 	/*
11148833SVenu.Iyer@Sun.COM 	 * Bumping up the smac_hold_cnt allows us to drop the lock. It also
11158833SVenu.Iyer@Sun.COM 	 * makes softmac_destroy() return failure on an attempted device detach.
11168833SVenu.Iyer@Sun.COM 	 * We don't want to hold the lock across calls to other subsystems
11178833SVenu.Iyer@Sun.COM 	 * like kstats, which will happen in the call to dls_devnet_recreate
11188833SVenu.Iyer@Sun.COM 	 */
11198833SVenu.Iyer@Sun.COM 	softmac->smac_hold_cnt++;
11208833SVenu.Iyer@Sun.COM 	mutex_exit(&softmac->smac_mutex);
11218833SVenu.Iyer@Sun.COM 
11225895Syz147064 	if (dls_mgmt_create(softmac->smac_devname,
11235895Syz147064 	    makedevice(softmac->smac_umajor, softmac->smac_uppa + 1),
11245895Syz147064 	    DATALINK_CLASS_PHYS, softmac->smac_media, B_TRUE, &linkid) != 0) {
11258833SVenu.Iyer@Sun.COM 		softmac_rele_device((dls_dev_handle_t)softmac);
11265895Syz147064 		return (MH_WALK_CONTINUE);
11275895Syz147064 	}
11285895Syz147064 
11295895Syz147064 	if ((err = softmac_update_info(softmac, &linkid)) != 0) {
11305895Syz147064 		cmn_err(CE_WARN, "softmac: softmac_update_info() for %s "
11315895Syz147064 		    "failed (%d)", softmac->smac_devname, err);
11328833SVenu.Iyer@Sun.COM 		softmac_rele_device((dls_dev_handle_t)softmac);
11335895Syz147064 		return (MH_WALK_CONTINUE);
11345895Syz147064 	}
11355895Syz147064 
11365895Syz147064 	/*
11375895Syz147064 	 * Create a link for this MAC. The link name will be the same
11385895Syz147064 	 * as the MAC name.
11395895Syz147064 	 */
11405895Syz147064 	if (!(softmac->smac_flags & SOFTMAC_NOSUPP)) {
11415895Syz147064 		err = dls_devnet_recreate(softmac->smac_mh, linkid);
11425895Syz147064 		if (err != 0) {
11435895Syz147064 			cmn_err(CE_WARN, "softmac: dls_devnet_recreate() for "
11445895Syz147064 			    "%s (linkid %d) failed (%d)",
11455895Syz147064 			    softmac->smac_devname, linkid, err);
11465895Syz147064 		}
11475895Syz147064 	}
11485895Syz147064 
11498833SVenu.Iyer@Sun.COM 	mutex_enter(&softmac->smac_mutex);
11505895Syz147064 	softmac->smac_flags &= ~SOFTMAC_NEED_RECREATE;
11518833SVenu.Iyer@Sun.COM 	ASSERT(softmac->smac_hold_cnt != 0);
11528833SVenu.Iyer@Sun.COM 	softmac->smac_hold_cnt--;
11535895Syz147064 	mutex_exit(&softmac->smac_mutex);
11545895Syz147064 
11555895Syz147064 	return (MH_WALK_CONTINUE);
11565895Syz147064 }
11575895Syz147064 
11585895Syz147064 /*
11595895Syz147064  * See comments above softmac_mac_recreate().
11605895Syz147064  */
11615895Syz147064 void
11625895Syz147064 softmac_recreate()
11635895Syz147064 {
11648275SEric Cheng 	softmac_walk_t	smw;
11658275SEric Cheng 	softmac_t	*softmac;
11668275SEric Cheng 
11675895Syz147064 	/*
11685895Syz147064 	 * Walk through the softmac_hash table. Request to create the
11695895Syz147064 	 * [link name, linkid] mapping if we failed to do so.
11705895Syz147064 	 */
11718275SEric Cheng 	do {
11728275SEric Cheng 		smw.smw_retry = B_FALSE;
11738275SEric Cheng 		rw_enter(&softmac_hash_lock, RW_READER);
11748275SEric Cheng 		mod_hash_walk(softmac_hash, softmac_mac_recreate, &smw);
11758275SEric Cheng 		rw_exit(&softmac_hash_lock);
11768275SEric Cheng 		if (smw.smw_retry) {
11778275SEric Cheng 			/*
11788275SEric Cheng 			 * softmac_create or softmac_mac_register hasn't yet
11798275SEric Cheng 			 * finished and the softmac is not yet in the
11808275SEric Cheng 			 * SOFTMAC_ATTACH_DONE state.
11818275SEric Cheng 			 */
11828275SEric Cheng 			softmac = smw.smw_softmac;
11838275SEric Cheng 			cv_wait(&softmac->smac_cv, &softmac->smac_mutex);
11848275SEric Cheng 			softmac->smac_hold_cnt--;
11858275SEric Cheng 			mutex_exit(&softmac->smac_mutex);
11868275SEric Cheng 		}
11878275SEric Cheng 	} while (smw.smw_retry);
11885895Syz147064 }
11895895Syz147064 
11905895Syz147064 static int
11915895Syz147064 softmac_m_start(void *arg)
11925895Syz147064 {
1193*9073SCathy.Zhou@Sun.COM 	softmac_t	*softmac = arg;
1194*9073SCathy.Zhou@Sun.COM 	softmac_lower_t	*slp = softmac->smac_lower;
1195*9073SCathy.Zhou@Sun.COM 	int		err;
1196*9073SCathy.Zhou@Sun.COM 
1197*9073SCathy.Zhou@Sun.COM 	ASSERT(MAC_PERIM_HELD(softmac->smac_mh));
1198*9073SCathy.Zhou@Sun.COM 	/*
1199*9073SCathy.Zhou@Sun.COM 	 * Bind to SAP 2 on token ring, 0 on other interface types.
1200*9073SCathy.Zhou@Sun.COM 	 * (SAP 0 has special significance on token ring).
1201*9073SCathy.Zhou@Sun.COM 	 * Note that the receive-side packets could come anytime after bind.
1202*9073SCathy.Zhou@Sun.COM 	 */
1203*9073SCathy.Zhou@Sun.COM 	err = softmac_send_bind_req(slp, softmac->smac_media == DL_TPR ? 2 : 0);
1204*9073SCathy.Zhou@Sun.COM 	if (err != 0)
1205*9073SCathy.Zhou@Sun.COM 		return (err);
1206*9073SCathy.Zhou@Sun.COM 
1207*9073SCathy.Zhou@Sun.COM 	/*
1208*9073SCathy.Zhou@Sun.COM 	 * Put the lower stream to the DL_PROMISC_SAP mode in order to receive
1209*9073SCathy.Zhou@Sun.COM 	 * all packets of interest.
1210*9073SCathy.Zhou@Sun.COM 	 *
1211*9073SCathy.Zhou@Sun.COM 	 * some driver (e.g. the old legacy eri driver) incorrectly passes up
1212*9073SCathy.Zhou@Sun.COM 	 * packets to DL_PROMISC_SAP stream when the lower stream is not bound,
1213*9073SCathy.Zhou@Sun.COM 	 * so that we send DL_PROMISON_REQ after DL_BIND_REQ.
1214*9073SCathy.Zhou@Sun.COM 	 */
1215*9073SCathy.Zhou@Sun.COM 	err = softmac_send_promisc_req(slp, DL_PROMISC_SAP, B_TRUE);
1216*9073SCathy.Zhou@Sun.COM 	if (err != 0) {
1217*9073SCathy.Zhou@Sun.COM 		(void) softmac_send_unbind_req(slp);
1218*9073SCathy.Zhou@Sun.COM 		return (err);
1219*9073SCathy.Zhou@Sun.COM 	}
1220*9073SCathy.Zhou@Sun.COM 
1221*9073SCathy.Zhou@Sun.COM 	/*
1222*9073SCathy.Zhou@Sun.COM 	 * Enable capabilities the underlying driver claims to support.
1223*9073SCathy.Zhou@Sun.COM 	 * Some driver requires this being called after the stream is bound.
1224*9073SCathy.Zhou@Sun.COM 	 */
1225*9073SCathy.Zhou@Sun.COM 	if ((err = softmac_capab_enable(slp)) != 0) {
1226*9073SCathy.Zhou@Sun.COM 		(void) softmac_send_promisc_req(slp, DL_PROMISC_SAP, B_FALSE);
1227*9073SCathy.Zhou@Sun.COM 		(void) softmac_send_unbind_req(slp);
1228*9073SCathy.Zhou@Sun.COM 	}
1229*9073SCathy.Zhou@Sun.COM 
1230*9073SCathy.Zhou@Sun.COM 	return (err);
12315895Syz147064 }
12325895Syz147064 
12335895Syz147064 /* ARGSUSED */
12345895Syz147064 static void
12355895Syz147064 softmac_m_stop(void *arg)
12365895Syz147064 {
1237*9073SCathy.Zhou@Sun.COM 	softmac_t	*softmac = arg;
1238*9073SCathy.Zhou@Sun.COM 	softmac_lower_t	*slp = softmac->smac_lower;
1239*9073SCathy.Zhou@Sun.COM 
1240*9073SCathy.Zhou@Sun.COM 	ASSERT(MAC_PERIM_HELD(softmac->smac_mh));
1241*9073SCathy.Zhou@Sun.COM 
1242*9073SCathy.Zhou@Sun.COM 	/*
1243*9073SCathy.Zhou@Sun.COM 	 * It is not needed to reset zerocopy, MDT or HCKSUM capabilities.
1244*9073SCathy.Zhou@Sun.COM 	 */
1245*9073SCathy.Zhou@Sun.COM 	(void) softmac_send_promisc_req(slp, DL_PROMISC_SAP, B_FALSE);
1246*9073SCathy.Zhou@Sun.COM 	(void) softmac_send_unbind_req(slp);
12475895Syz147064 }
12485895Syz147064 
12495895Syz147064 /*
1250*9073SCathy.Zhou@Sun.COM  * Set up the lower stream above the legacy device. There are two different
1251*9073SCathy.Zhou@Sun.COM  * type of lower streams:
1252*9073SCathy.Zhou@Sun.COM  *
1253*9073SCathy.Zhou@Sun.COM  * - Shared lower-stream
1254*9073SCathy.Zhou@Sun.COM  *
1255*9073SCathy.Zhou@Sun.COM  * Shared by all GLDv3 MAC clients. Put the lower stream to the DLIOCRAW
1256*9073SCathy.Zhou@Sun.COM  * mode to send and receive the raw data. Further, put the lower stream into
12575895Syz147064  * DL_PROMISC_SAP mode to receive all packets of interest.
1258*9073SCathy.Zhou@Sun.COM  *
1259*9073SCathy.Zhou@Sun.COM  * - Dedicated lower-stream
1260*9073SCathy.Zhou@Sun.COM  *
1261*9073SCathy.Zhou@Sun.COM  * The lower-stream which is dedicated to upper IP/ARP stream. This is used
1262*9073SCathy.Zhou@Sun.COM  * as fast-path for IP. In this case, the second argument is the pointer to
1263*9073SCathy.Zhou@Sun.COM  * the softmac upper-stream.
12645895Syz147064  */
1265*9073SCathy.Zhou@Sun.COM int
1266*9073SCathy.Zhou@Sun.COM softmac_lower_setup(softmac_t *softmac, softmac_upper_t *sup,
1267*9073SCathy.Zhou@Sun.COM     softmac_lower_t **slpp)
12685895Syz147064 {
12695895Syz147064 	ldi_ident_t		li;
12705895Syz147064 	dev_t			dev;
12715895Syz147064 	ldi_handle_t		lh = NULL;
12725895Syz147064 	softmac_lower_t		*slp = NULL;
12735895Syz147064 	smac_ioc_start_t	start_arg;
12745895Syz147064 	struct strioctl		strioc;
12755895Syz147064 	uint32_t		notifications;
12765895Syz147064 	int			err, rval;
12775895Syz147064 
12785895Syz147064 	if ((err = ldi_ident_from_dip(softmac_dip, &li)) != 0)
12795895Syz147064 		return (err);
12805895Syz147064 
1281*9073SCathy.Zhou@Sun.COM 	/*
1282*9073SCathy.Zhou@Sun.COM 	 * The GLDv3 framework makes sure that mac_unregister(), mac_open(),
1283*9073SCathy.Zhou@Sun.COM 	 * and mac_close() cannot be called at the same time. So we don't
1284*9073SCathy.Zhou@Sun.COM 	 * need any protection to access softmac here.
1285*9073SCathy.Zhou@Sun.COM 	 */
12865895Syz147064 	dev = softmac->smac_dev;
1287*9073SCathy.Zhou@Sun.COM 
12885895Syz147064 	err = ldi_open_by_dev(&dev, OTYP_CHR, FREAD|FWRITE, kcred, &lh, li);
12895895Syz147064 	ldi_ident_release(li);
12905895Syz147064 	if (err != 0)
12915895Syz147064 		goto done;
12925895Syz147064 
12935895Syz147064 	/*
12945895Syz147064 	 * Pop all the intermediate modules. The autopushed modules will
12955895Syz147064 	 * be pushed when the softmac node is opened.
12965895Syz147064 	 */
12975895Syz147064 	while (ldi_ioctl(lh, I_POP, 0, FKIOCTL, kcred, &rval) == 0)
12985895Syz147064 		;
12995895Syz147064 
13005895Syz147064 	if ((softmac->smac_style == DL_STYLE2) &&
13015895Syz147064 	    ((err = dl_attach(lh, softmac->smac_uppa, NULL)) != 0)) {
13025895Syz147064 		goto done;
13035895Syz147064 	}
13045895Syz147064 
13055895Syz147064 	/*
1306*9073SCathy.Zhou@Sun.COM 	 * If this is the shared-lower-stream, put the lower stream to
1307*9073SCathy.Zhou@Sun.COM 	 * the DLIOCRAW mode to send/receive raw data.
13085895Syz147064 	 */
1309*9073SCathy.Zhou@Sun.COM 	if ((sup == NULL) && (err = ldi_ioctl(lh, DLIOCRAW, 0, FKIOCTL,
1310*9073SCathy.Zhou@Sun.COM 	    kcred, &rval)) != 0) {
13115895Syz147064 		goto done;
1312*9073SCathy.Zhou@Sun.COM 	}
13135895Syz147064 
13145895Syz147064 	/*
13155895Syz147064 	 * Then push the softmac shim layer atop the lower stream.
13165895Syz147064 	 */
13175895Syz147064 	if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)SOFTMAC_DEV_NAME, FKIOCTL,
13185895Syz147064 	    kcred, &rval)) != 0) {
13195895Syz147064 		goto done;
13205895Syz147064 	}
13215895Syz147064 
13225895Syz147064 	/*
13235895Syz147064 	 * Send the ioctl to get the slp pointer.
13245895Syz147064 	 */
13255895Syz147064 	strioc.ic_cmd = SMAC_IOC_START;
13265895Syz147064 	strioc.ic_timout = INFTIM;
13275895Syz147064 	strioc.ic_len = sizeof (start_arg);
13285895Syz147064 	strioc.ic_dp = (char *)&start_arg;
13295895Syz147064 
13305895Syz147064 	if ((err = ldi_ioctl(lh, I_STR, (intptr_t)&strioc, FKIOCTL,
13315895Syz147064 	    kcred, &rval)) != 0) {
13325895Syz147064 		goto done;
13335895Syz147064 	}
13345895Syz147064 	slp = start_arg.si_slp;
1335*9073SCathy.Zhou@Sun.COM 	slp->sl_sup = sup;
13365895Syz147064 	slp->sl_lh = lh;
13375895Syz147064 	slp->sl_softmac = softmac;
13385895Syz147064 	*slpp = slp;
13395895Syz147064 
1340*9073SCathy.Zhou@Sun.COM 	if (sup != NULL) {
1341*9073SCathy.Zhou@Sun.COM 		slp->sl_rxinfo = &sup->su_rxinfo;
1342*9073SCathy.Zhou@Sun.COM 	} else {
1343*9073SCathy.Zhou@Sun.COM 		/*
1344*9073SCathy.Zhou@Sun.COM 		 * Send DL_NOTIFY_REQ to enable certain DL_NOTIFY_IND.
1345*9073SCathy.Zhou@Sun.COM 		 * We don't have to wait for the ack.
1346*9073SCathy.Zhou@Sun.COM 		 */
1347*9073SCathy.Zhou@Sun.COM 		notifications = DL_NOTE_PHYS_ADDR | DL_NOTE_LINK_UP |
1348*9073SCathy.Zhou@Sun.COM 		    DL_NOTE_LINK_DOWN | DL_NOTE_PROMISC_ON_PHYS |
1349*9073SCathy.Zhou@Sun.COM 		    DL_NOTE_PROMISC_OFF_PHYS;
13505895Syz147064 
1351*9073SCathy.Zhou@Sun.COM 		(void) softmac_send_notify_req(slp,
1352*9073SCathy.Zhou@Sun.COM 		    (notifications & softmac->smac_notifications));
1353*9073SCathy.Zhou@Sun.COM 	}
13545895Syz147064 
13555895Syz147064 done:
13565895Syz147064 	if (err != 0)
13575895Syz147064 		(void) ldi_close(lh, FREAD|FWRITE, kcred);
13585895Syz147064 	return (err);
13595895Syz147064 }
13605895Syz147064 
13615895Syz147064 static int
13625895Syz147064 softmac_m_open(void *arg)
13635895Syz147064 {
13645895Syz147064 	softmac_t	*softmac = arg;
13655895Syz147064 	softmac_lower_t	*slp;
13665895Syz147064 	int		err;
13675895Syz147064 
13688275SEric Cheng 	ASSERT(MAC_PERIM_HELD(softmac->smac_mh));
13695895Syz147064 
1370*9073SCathy.Zhou@Sun.COM 	if ((err = softmac_lower_setup(softmac, NULL, &slp)) != 0)
13715895Syz147064 		return (err);
13725895Syz147064 
13735895Syz147064 	softmac->smac_lower = slp;
13745895Syz147064 	return (0);
13755895Syz147064 }
13765895Syz147064 
13775895Syz147064 static void
13785895Syz147064 softmac_m_close(void *arg)
13795895Syz147064 {
13805895Syz147064 	softmac_t	*softmac = arg;
13815895Syz147064 	softmac_lower_t	*slp;
13825895Syz147064 
13838275SEric Cheng 	ASSERT(MAC_PERIM_HELD(softmac->smac_mh));
13845895Syz147064 	slp = softmac->smac_lower;
13855895Syz147064 	ASSERT(slp != NULL);
13865895Syz147064 
13875895Syz147064 	/*
13885895Syz147064 	 * Note that slp is destroyed when lh is closed.
13895895Syz147064 	 */
13905895Syz147064 	(void) ldi_close(slp->sl_lh, FREAD|FWRITE, kcred);
13915895Syz147064 	softmac->smac_lower = NULL;
13925895Syz147064 }
13935895Syz147064 
1394*9073SCathy.Zhou@Sun.COM /*
1395*9073SCathy.Zhou@Sun.COM  * Softmac supports two priviate link properteis:
1396*9073SCathy.Zhou@Sun.COM  *
1397*9073SCathy.Zhou@Sun.COM  * - "_fastpath"
1398*9073SCathy.Zhou@Sun.COM  *
1399*9073SCathy.Zhou@Sun.COM  *    This is a read-only link property which points out the current data-path
1400*9073SCathy.Zhou@Sun.COM  *    model of the given legacy link. The possible values are "disabled" and
1401*9073SCathy.Zhou@Sun.COM  *    "enabled".
1402*9073SCathy.Zhou@Sun.COM  *
1403*9073SCathy.Zhou@Sun.COM  * - "_disable_fastpath"
1404*9073SCathy.Zhou@Sun.COM  *
1405*9073SCathy.Zhou@Sun.COM  *    This is a read-write link property which can be used to disable or enable
1406*9073SCathy.Zhou@Sun.COM  *    the fast-path of the given legacy link. The possible values are "true"
1407*9073SCathy.Zhou@Sun.COM  *    and "false". Note that even when "_disable_fastpath" is set to be
1408*9073SCathy.Zhou@Sun.COM  *    "false", the fast-path may still not be enabled since there may be
1409*9073SCathy.Zhou@Sun.COM  *    other mac cleints that request the fast-path to be disabled.
1410*9073SCathy.Zhou@Sun.COM  */
1411*9073SCathy.Zhou@Sun.COM /* ARGSUSED */
1412*9073SCathy.Zhou@Sun.COM static int
1413*9073SCathy.Zhou@Sun.COM softmac_m_setprop(void *arg, const char *name, mac_prop_id_t id,
1414*9073SCathy.Zhou@Sun.COM     uint_t valsize, const void *val)
1415*9073SCathy.Zhou@Sun.COM {
1416*9073SCathy.Zhou@Sun.COM 	softmac_t	*softmac = arg;
1417*9073SCathy.Zhou@Sun.COM 
1418*9073SCathy.Zhou@Sun.COM 	if (id != MAC_PROP_PRIVATE || strcmp(name, "_disable_fastpath") != 0)
1419*9073SCathy.Zhou@Sun.COM 		return (ENOTSUP);
1420*9073SCathy.Zhou@Sun.COM 
1421*9073SCathy.Zhou@Sun.COM 	if (strcmp(val, "true") == 0)
1422*9073SCathy.Zhou@Sun.COM 		return (softmac_datapath_switch(softmac, B_TRUE, B_TRUE));
1423*9073SCathy.Zhou@Sun.COM 	else if (strcmp(val, "false") == 0)
1424*9073SCathy.Zhou@Sun.COM 		return (softmac_datapath_switch(softmac, B_FALSE, B_TRUE));
1425*9073SCathy.Zhou@Sun.COM 	else
1426*9073SCathy.Zhou@Sun.COM 		return (EINVAL);
1427*9073SCathy.Zhou@Sun.COM }
1428*9073SCathy.Zhou@Sun.COM 
1429*9073SCathy.Zhou@Sun.COM static int
1430*9073SCathy.Zhou@Sun.COM softmac_m_getprop(void *arg, const char *name, mac_prop_id_t id, uint_t flags,
1431*9073SCathy.Zhou@Sun.COM     uint_t valsize, void *val, uint_t *perm)
1432*9073SCathy.Zhou@Sun.COM {
1433*9073SCathy.Zhou@Sun.COM 	softmac_t	*softmac = arg;
1434*9073SCathy.Zhou@Sun.COM 	char		*fpstr;
1435*9073SCathy.Zhou@Sun.COM 
1436*9073SCathy.Zhou@Sun.COM 	if (id != MAC_PROP_PRIVATE)
1437*9073SCathy.Zhou@Sun.COM 		return (ENOTSUP);
1438*9073SCathy.Zhou@Sun.COM 
1439*9073SCathy.Zhou@Sun.COM 	if (strcmp(name, "_fastpath") == 0) {
1440*9073SCathy.Zhou@Sun.COM 		if ((flags & MAC_PROP_DEFAULT) != 0)
1441*9073SCathy.Zhou@Sun.COM 			return (ENOTSUP);
1442*9073SCathy.Zhou@Sun.COM 
1443*9073SCathy.Zhou@Sun.COM 		*perm = MAC_PROP_PERM_READ;
1444*9073SCathy.Zhou@Sun.COM 		mutex_enter(&softmac->smac_fp_mutex);
1445*9073SCathy.Zhou@Sun.COM 		fpstr = (DATAPATH_MODE(softmac) == SOFTMAC_SLOWPATH) ?
1446*9073SCathy.Zhou@Sun.COM 		    "disabled" : "enabled";
1447*9073SCathy.Zhou@Sun.COM 		mutex_exit(&softmac->smac_fp_mutex);
1448*9073SCathy.Zhou@Sun.COM 	} else if (strcmp(name, "_disable_fastpath") == 0) {
1449*9073SCathy.Zhou@Sun.COM 		*perm = MAC_PROP_PERM_RW;
1450*9073SCathy.Zhou@Sun.COM 		fpstr = ((flags & MAC_PROP_DEFAULT) != 0) ? "false" :
1451*9073SCathy.Zhou@Sun.COM 		    (softmac->smac_fastpath_admin_disabled ? "true" : "false");
1452*9073SCathy.Zhou@Sun.COM 	} else {
1453*9073SCathy.Zhou@Sun.COM 		return (ENOTSUP);
1454*9073SCathy.Zhou@Sun.COM 	}
1455*9073SCathy.Zhou@Sun.COM 
1456*9073SCathy.Zhou@Sun.COM 	return (strlcpy(val, fpstr, valsize) >= valsize ? EINVAL : 0);
1457*9073SCathy.Zhou@Sun.COM }
1458*9073SCathy.Zhou@Sun.COM 
14595895Syz147064 int
14605895Syz147064 softmac_hold_device(dev_t dev, dls_dev_handle_t *ddhp)
14615895Syz147064 {
14625895Syz147064 	dev_info_t	*dip;
14636915Syz147064 	const char	*drvname;
14645895Syz147064 	char		devname[MAXNAMELEN];
14655895Syz147064 	softmac_t	*softmac;
14665895Syz147064 	int		ppa, err;
14675895Syz147064 
14685895Syz147064 	if ((ppa = getminor(dev) - 1) > 1000)
14695895Syz147064 		return (ENOENT);
14705895Syz147064 
14715895Syz147064 	/*
14725895Syz147064 	 * First try to hold this device instance to force the MAC
14735895Syz147064 	 * to be registered.
14745895Syz147064 	 */
14755895Syz147064 	if ((dip = ddi_hold_devi_by_instance(getmajor(dev), ppa, 0)) == NULL)
14765895Syz147064 		return (ENOENT);
14775895Syz147064 
14786915Syz147064 	drvname = ddi_driver_name(dip);
14796915Syz147064 
14806915Syz147064 	/*
14816915Syz147064 	 * Exclude non-physical network device instances, for example, aggr0.
14826915Syz147064 	 */
14835895Syz147064 	if ((ddi_driver_major(dip) != getmajor(dev)) ||
14846915Syz147064 	    !NETWORK_DRV(getmajor(dev)) || (strcmp(drvname, "aggr") == 0) ||
14856915Syz147064 	    (strcmp(drvname, "vnic") == 0)) {
14865895Syz147064 		ddi_release_devi(dip);
14875895Syz147064 		return (ENOENT);
14885895Syz147064 	}
14895895Syz147064 
14905895Syz147064 	/*
14915895Syz147064 	 * This is a network device; wait for its softmac to be registered.
14925895Syz147064 	 */
14936915Syz147064 	(void) snprintf(devname, MAXNAMELEN, "%s%d", drvname, ppa);
14945895Syz147064 again:
14955895Syz147064 	rw_enter(&softmac_hash_lock, RW_READER);
14965895Syz147064 
14975895Syz147064 	if (mod_hash_find(softmac_hash, (mod_hash_key_t)devname,
14985895Syz147064 	    (mod_hash_val_t *)&softmac) != 0) {
14995895Syz147064 		/*
15005895Syz147064 		 * This is rare but possible. It could happen when pre-detach
15015895Syz147064 		 * routine of the device succeeds. But the softmac will then
15025895Syz147064 		 * be recreated when device fails to detach (as this device
15035895Syz147064 		 * is held).
15045895Syz147064 		 */
15058275SEric Cheng 		mutex_enter(&smac_global_lock);
15065895Syz147064 		rw_exit(&softmac_hash_lock);
15078275SEric Cheng 		cv_wait(&smac_global_cv, &smac_global_lock);
15088275SEric Cheng 		mutex_exit(&smac_global_lock);
15095895Syz147064 		goto again;
15105895Syz147064 	}
15115895Syz147064 
15125895Syz147064 	/*
15135895Syz147064 	 * Bump smac_hold_cnt to prevent device detach.
15145895Syz147064 	 */
15155895Syz147064 	mutex_enter(&softmac->smac_mutex);
15165895Syz147064 	softmac->smac_hold_cnt++;
15175895Syz147064 	rw_exit(&softmac_hash_lock);
15185895Syz147064 
15195895Syz147064 	/*
15205895Syz147064 	 * Wait till the device is fully attached.
15215895Syz147064 	 */
15228275SEric Cheng 	while (softmac->smac_state != SOFTMAC_ATTACH_DONE)
15235895Syz147064 		cv_wait(&softmac->smac_cv, &softmac->smac_mutex);
15245895Syz147064 
15258275SEric Cheng 	SOFTMAC_STATE_VERIFY(softmac);
15268275SEric Cheng 
15275909Syz147064 	if ((err = softmac->smac_attacherr) != 0)
15285909Syz147064 		softmac->smac_hold_cnt--;
15295909Syz147064 	else
15305895Syz147064 		*ddhp = (dls_dev_handle_t)softmac;
15315895Syz147064 	mutex_exit(&softmac->smac_mutex);
15325895Syz147064 
15335909Syz147064 	ddi_release_devi(dip);
15345895Syz147064 	return (err);
15355895Syz147064 }
15365895Syz147064 
15375895Syz147064 void
15385895Syz147064 softmac_rele_device(dls_dev_handle_t ddh)
15395895Syz147064 {
1540*9073SCathy.Zhou@Sun.COM 	if (ddh != NULL)
1541*9073SCathy.Zhou@Sun.COM 		softmac_rele((softmac_t *)ddh);
1542*9073SCathy.Zhou@Sun.COM }
1543*9073SCathy.Zhou@Sun.COM 
1544*9073SCathy.Zhou@Sun.COM int
1545*9073SCathy.Zhou@Sun.COM softmac_hold(dev_t dev, softmac_t **softmacp)
1546*9073SCathy.Zhou@Sun.COM {
15475895Syz147064 	softmac_t	*softmac;
1548*9073SCathy.Zhou@Sun.COM 	char		*drv;
1549*9073SCathy.Zhou@Sun.COM 	mac_handle_t	mh;
1550*9073SCathy.Zhou@Sun.COM 	char		mac[MAXNAMELEN];
1551*9073SCathy.Zhou@Sun.COM 	int		err;
1552*9073SCathy.Zhou@Sun.COM 
1553*9073SCathy.Zhou@Sun.COM 	if ((drv = ddi_major_to_name(getmajor(dev))) == NULL)
1554*9073SCathy.Zhou@Sun.COM 		return (EINVAL);
15555895Syz147064 
1556*9073SCathy.Zhou@Sun.COM 	(void) snprintf(mac, MAXNAMELEN, "%s%d", drv, getminor(dev) - 1);
1557*9073SCathy.Zhou@Sun.COM 	if ((err = mac_open(mac, &mh)) != 0)
1558*9073SCathy.Zhou@Sun.COM 		return (err);
1559*9073SCathy.Zhou@Sun.COM 
1560*9073SCathy.Zhou@Sun.COM 	softmac = (softmac_t *)mac_driver(mh);
15615895Syz147064 
1562*9073SCathy.Zhou@Sun.COM 	mutex_enter(&softmac->smac_mutex);
1563*9073SCathy.Zhou@Sun.COM 	softmac->smac_hold_cnt++;
1564*9073SCathy.Zhou@Sun.COM 	mutex_exit(&softmac->smac_mutex);
1565*9073SCathy.Zhou@Sun.COM 	mac_close(mh);
1566*9073SCathy.Zhou@Sun.COM 	*softmacp = softmac;
1567*9073SCathy.Zhou@Sun.COM 	return (0);
1568*9073SCathy.Zhou@Sun.COM }
1569*9073SCathy.Zhou@Sun.COM 
1570*9073SCathy.Zhou@Sun.COM void
1571*9073SCathy.Zhou@Sun.COM softmac_rele(softmac_t *softmac)
1572*9073SCathy.Zhou@Sun.COM {
15735895Syz147064 	mutex_enter(&softmac->smac_mutex);
15745909Syz147064 	softmac->smac_hold_cnt--;
15755895Syz147064 	mutex_exit(&softmac->smac_mutex);
15765895Syz147064 }
1577