xref: /onnv-gate/usr/src/uts/common/io/dls/dls_link.c (revision 2311:2d86e52dcdf0)
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
51502Sericheng  * Common Development and Distribution License (the "License").
61502Sericheng  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
221502Sericheng  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * Data-Link Services Module
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include	<sys/types.h>
330Sstevel@tonic-gate #include	<sys/stream.h>
340Sstevel@tonic-gate #include	<sys/strsun.h>
350Sstevel@tonic-gate #include	<sys/strsubr.h>
360Sstevel@tonic-gate #include	<sys/sysmacros.h>
370Sstevel@tonic-gate #include	<sys/atomic.h>
38269Sericheng #include	<sys/modhash.h>
390Sstevel@tonic-gate #include	<sys/dlpi.h>
400Sstevel@tonic-gate #include	<sys/ethernet.h>
410Sstevel@tonic-gate #include	<sys/byteorder.h>
420Sstevel@tonic-gate #include	<sys/vlan.h>
430Sstevel@tonic-gate #include	<sys/mac.h>
440Sstevel@tonic-gate #include	<sys/sdt.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #include	<sys/dls.h>
470Sstevel@tonic-gate #include	<sys/dld_impl.h>
480Sstevel@tonic-gate #include	<sys/dls_impl.h>
490Sstevel@tonic-gate 
500Sstevel@tonic-gate static kmem_cache_t	*i_dls_link_cachep;
51269Sericheng static mod_hash_t	*i_dls_link_hash;
52269Sericheng static uint_t		i_dls_link_count;
53269Sericheng static krwlock_t	i_dls_link_lock;
540Sstevel@tonic-gate 
550Sstevel@tonic-gate #define		LINK_HASHSZ	67	/* prime */
560Sstevel@tonic-gate #define		IMPL_HASHSZ	67	/* prime */
570Sstevel@tonic-gate 
580Sstevel@tonic-gate /*
590Sstevel@tonic-gate  * Construct a hash key encompassing both DLSAP value and VLAN idenitifier.
600Sstevel@tonic-gate  */
610Sstevel@tonic-gate #define	MAKE_KEY(_sap, _vid)						\
62269Sericheng 	((mod_hash_key_t)(uintptr_t)					\
63269Sericheng 	(((_sap) << VLAN_ID_SIZE) | (_vid) & VLAN_ID_MASK))
640Sstevel@tonic-gate 
650Sstevel@tonic-gate /*
660Sstevel@tonic-gate  * Extract the DLSAP value from the hash key.
670Sstevel@tonic-gate  */
680Sstevel@tonic-gate #define	KEY_SAP(_key)							\
690Sstevel@tonic-gate 	(((uint32_t)(uintptr_t)(_key)) >> VLAN_ID_SIZE)
700Sstevel@tonic-gate 
71*2311Sseb #define	DLS_STRIP_PADDING(pktsize, p) {			\
72*2311Sseb 	if (pktsize != 0) {				\
73*2311Sseb 		ssize_t delta = pktsize - msgdsize(p);	\
74*2311Sseb 							\
75*2311Sseb 		if (delta < 0)				\
76*2311Sseb 			(void) adjmsg(p, delta);	\
77*2311Sseb 	}						\
78*2311Sseb }
79*2311Sseb 
800Sstevel@tonic-gate /*
810Sstevel@tonic-gate  * Private functions.
820Sstevel@tonic-gate  */
830Sstevel@tonic-gate 
840Sstevel@tonic-gate /*ARGSUSED*/
850Sstevel@tonic-gate static int
860Sstevel@tonic-gate i_dls_link_constructor(void *buf, void *arg, int kmflag)
870Sstevel@tonic-gate {
880Sstevel@tonic-gate 	dls_link_t	*dlp = buf;
890Sstevel@tonic-gate 	char		name[MAXNAMELEN];
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	bzero(buf, sizeof (dls_link_t));
920Sstevel@tonic-gate 
93269Sericheng 	(void) sprintf(name, "dls_link_t_%p_hash", buf);
94269Sericheng 	dlp->dl_impl_hash = mod_hash_create_idhash(name, IMPL_HASHSZ,
95269Sericheng 	    mod_hash_null_valdtor);
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	mutex_init(&dlp->dl_lock, NULL, MUTEX_DEFAULT, NULL);
9856Smeem 	mutex_init(&dlp->dl_promisc_lock, NULL, MUTEX_DEFAULT, NULL);
99269Sericheng 	rw_init(&dlp->dl_impl_lock, NULL, RW_DEFAULT, NULL);
1000Sstevel@tonic-gate 	return (0);
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate /*ARGSUSED*/
1040Sstevel@tonic-gate static void
1050Sstevel@tonic-gate i_dls_link_destructor(void *buf, void *arg)
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate 	dls_link_t	*dlp = buf;
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	ASSERT(dlp->dl_ref == 0);
1100Sstevel@tonic-gate 	ASSERT(dlp->dl_mh == NULL);
1110Sstevel@tonic-gate 	ASSERT(dlp->dl_unknowns == 0);
1120Sstevel@tonic-gate 
113269Sericheng 	mod_hash_destroy_idhash(dlp->dl_impl_hash);
114269Sericheng 	dlp->dl_impl_hash = NULL;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	mutex_destroy(&dlp->dl_lock);
11756Smeem 	mutex_destroy(&dlp->dl_promisc_lock);
118269Sericheng 	rw_destroy(&dlp->dl_impl_lock);
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate 
121*2311Sseb /*
122*2311Sseb  * Truncate the chain starting at mp such that all packets in the chain
123*2311Sseb  * have identical source and destination addresses, saps, and VLAN tags (if
124*2311Sseb  * any).  It returns a pointer to the mblk following the chain, NULL if
125*2311Sseb  * there is no further packet following the processed chain.  The countp
126*2311Sseb  * argument is set to the number of valid packets in the chain.  It is set
127*2311Sseb  * to 0 if the function encountered a problem with the first packet.
128*2311Sseb  */
1290Sstevel@tonic-gate static mblk_t *
130*2311Sseb i_dls_link_subchain(dls_link_t *dlp, mblk_t *mp, mac_header_info_t *mhip,
131*2311Sseb     uint16_t *vidp, uint_t *countp)
1320Sstevel@tonic-gate {
133*2311Sseb 	mblk_t		**pp;
134*2311Sseb 	mblk_t		*p;
135*2311Sseb 	uint_t		npacket;
136*2311Sseb 	size_t		addr_size = dlp->dl_mip->mi_addr_length;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	/*
1390Sstevel@tonic-gate 	 * Packets should always be at least 16 bit aligned.
1400Sstevel@tonic-gate 	 */
1410Sstevel@tonic-gate 	ASSERT(IS_P2ALIGNED(mp->b_rptr, sizeof (uint16_t)));
1420Sstevel@tonic-gate 
143*2311Sseb 	if (dls_link_header_info(dlp, mp, mhip, vidp) != 0) {
144*2311Sseb 		/*
145*2311Sseb 		 * Something is wrong with the initial header.  No chain is
146*2311Sseb 		 * possible.
147*2311Sseb 		 */
148*2311Sseb 		p = mp->b_next;
149*2311Sseb 		mp->b_next = NULL;
150*2311Sseb 		*countp = 0;
151*2311Sseb 		return (p);
152*2311Sseb 	}
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	/*
1550Sstevel@tonic-gate 	 * Compare with subsequent headers until we find one that has
1561502Sericheng 	 * differing header information. After checking each packet
1571502Sericheng 	 * strip padding and skip over the header.
1580Sstevel@tonic-gate 	 */
1590Sstevel@tonic-gate 	npacket = 1;
1600Sstevel@tonic-gate 	for (pp = &(mp->b_next); (p = *pp) != NULL; pp = &(p->b_next)) {
161*2311Sseb 		mac_header_info_t cmhi;
162*2311Sseb 		uint16_t cvid;
163*2311Sseb 
164*2311Sseb 		if (dls_link_header_info(dlp, p, &cmhi, &cvid) != 0)
1650Sstevel@tonic-gate 			break;
166*2311Sseb 
167*2311Sseb 		/*
168*2311Sseb 		 * The source, destination, sap, and vlan id must all match
169*2311Sseb 		 * in a given subchain.
170*2311Sseb 		 */
171*2311Sseb 		if (memcmp(mhip->mhi_daddr, cmhi.mhi_daddr, addr_size) != 0 ||
172*2311Sseb 		    memcmp(mhip->mhi_saddr, cmhi.mhi_saddr, addr_size) != 0 ||
173*2311Sseb 		    mhip->mhi_bindsap != cmhi.mhi_bindsap) {
174*2311Sseb 			break;
175*2311Sseb 		}
176*2311Sseb 
177*2311Sseb 		if (cvid != *vidp)
178*2311Sseb 			break;
179*2311Sseb 
180*2311Sseb 		DLS_STRIP_PADDING(cmhi.mhi_pktsize, p);
181*2311Sseb 		p->b_rptr += cmhi.mhi_hdrsize;
1820Sstevel@tonic-gate 		npacket++;
1830Sstevel@tonic-gate 	}
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	/*
1861502Sericheng 	 * Strip padding and skip over the initial packet's header.
1870Sstevel@tonic-gate 	 */
188*2311Sseb 	DLS_STRIP_PADDING(mhip->mhi_pktsize, mp);
189*2311Sseb 	mp->b_rptr += mhip->mhi_hdrsize;
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	/*
1920Sstevel@tonic-gate 	 * Break the chain at this point and return a pointer to the next
1930Sstevel@tonic-gate 	 * sub-chain.
1940Sstevel@tonic-gate 	 */
1950Sstevel@tonic-gate 	*pp = NULL;
1960Sstevel@tonic-gate 	*countp = npacket;
1970Sstevel@tonic-gate 	return (p);
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate static void
201269Sericheng i_dls_head_hold(dls_head_t *dhp)
202269Sericheng {
203269Sericheng 	atomic_inc_32(&dhp->dh_ref);
204269Sericheng }
205269Sericheng 
206269Sericheng static void
207269Sericheng i_dls_head_rele(dls_head_t *dhp)
208269Sericheng {
209269Sericheng 	atomic_dec_32(&dhp->dh_ref);
210269Sericheng }
211269Sericheng 
212269Sericheng static dls_head_t *
213269Sericheng i_dls_head_alloc(mod_hash_key_t key)
214269Sericheng {
215269Sericheng 	dls_head_t	*dhp;
216269Sericheng 
217269Sericheng 	dhp = kmem_zalloc(sizeof (dls_head_t), KM_SLEEP);
218269Sericheng 	dhp->dh_key = key;
219269Sericheng 	return (dhp);
220269Sericheng }
221269Sericheng 
222269Sericheng static void
223269Sericheng i_dls_head_free(dls_head_t *dhp)
224269Sericheng {
225269Sericheng 	ASSERT(dhp->dh_ref == 0);
226269Sericheng 	kmem_free(dhp, sizeof (dls_head_t));
227269Sericheng }
228269Sericheng 
229269Sericheng static void
230*2311Sseb i_dls_link_rx(void *arg, mac_resource_handle_t mrh, mblk_t *mp)
2310Sstevel@tonic-gate {
2320Sstevel@tonic-gate 	dls_link_t			*dlp = arg;
233269Sericheng 	mod_hash_t			*hash = dlp->dl_impl_hash;
2340Sstevel@tonic-gate 	mblk_t				*nextp;
235*2311Sseb 	mac_header_info_t		mhi;
2360Sstevel@tonic-gate 	uint16_t			vid;
237269Sericheng 	dls_head_t			*dhp;
2380Sstevel@tonic-gate 	dls_impl_t			*dip;
2390Sstevel@tonic-gate 	dls_impl_t			*ndip;
2400Sstevel@tonic-gate 	mblk_t				*nmp;
241269Sericheng 	mod_hash_key_t			key;
2420Sstevel@tonic-gate 	uint_t				npacket;
2430Sstevel@tonic-gate 	boolean_t			accepted;
244449Sericheng 	dls_rx_t			di_rx, ndi_rx;
245449Sericheng 	void				*di_rx_arg, *ndi_rx_arg;
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	/*
2480Sstevel@tonic-gate 	 * Walk the packet chain.
2490Sstevel@tonic-gate 	 */
2500Sstevel@tonic-gate 	while (mp != NULL) {
2510Sstevel@tonic-gate 		/*
2520Sstevel@tonic-gate 		 * Wipe the accepted state.
2530Sstevel@tonic-gate 		 */
2540Sstevel@tonic-gate 		accepted = B_FALSE;
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 		/*
2570Sstevel@tonic-gate 		 * Grab the longest sub-chain we can process as a single
2580Sstevel@tonic-gate 		 * unit.
2590Sstevel@tonic-gate 		 */
260*2311Sseb 		nextp = i_dls_link_subchain(dlp, mp, &mhi, &vid, &npacket);
2610Sstevel@tonic-gate 
262*2311Sseb 		if (npacket == 0) {
263*2311Sseb 			/*
264*2311Sseb 			 * The first packet had an unrecognized header.
265*2311Sseb 			 * Modify npacket so that this stray can be
266*2311Sseb 			 * accounted for.
267*2311Sseb 			 */
268*2311Sseb 			npacket = 1;
269*2311Sseb 			freemsg(mp);
270*2311Sseb 			goto loop;
271*2311Sseb 		}
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 		/*
2740Sstevel@tonic-gate 		 * Construct a hash key from the VLAN identifier and the
2750Sstevel@tonic-gate 		 * DLSAP.
2760Sstevel@tonic-gate 		 */
277*2311Sseb 		key = MAKE_KEY(mhi.mhi_bindsap, vid);
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 		/*
2800Sstevel@tonic-gate 		 * Search the has table for dls_impl_t eligible to receive
2810Sstevel@tonic-gate 		 * a packet chain for this DLSAP/VLAN combination.
2820Sstevel@tonic-gate 		 */
283269Sericheng 		rw_enter(&dlp->dl_impl_lock, RW_READER);
284269Sericheng 		if (mod_hash_find(hash, key, (mod_hash_val_t *)&dhp) != 0) {
285269Sericheng 			rw_exit(&dlp->dl_impl_lock);
2860Sstevel@tonic-gate 			freemsgchain(mp);
2870Sstevel@tonic-gate 			goto loop;
2880Sstevel@tonic-gate 		}
289269Sericheng 		i_dls_head_hold(dhp);
290269Sericheng 		rw_exit(&dlp->dl_impl_lock);
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 		/*
2930Sstevel@tonic-gate 		 * Find the first dls_impl_t that will accept the sub-chain.
2940Sstevel@tonic-gate 		 */
295269Sericheng 		for (dip = dhp->dh_list; dip != NULL; dip = dip->di_nextp)
296*2311Sseb 			if (dls_accept(dip, &mhi, &di_rx, &di_rx_arg))
2970Sstevel@tonic-gate 				break;
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 		/*
3000Sstevel@tonic-gate 		 * If we did not find any dls_impl_t willing to accept the
3010Sstevel@tonic-gate 		 * sub-chain then throw it away.
3020Sstevel@tonic-gate 		 */
3030Sstevel@tonic-gate 		if (dip == NULL) {
304269Sericheng 			i_dls_head_rele(dhp);
3050Sstevel@tonic-gate 			freemsgchain(mp);
3060Sstevel@tonic-gate 			goto loop;
3070Sstevel@tonic-gate 		}
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 		/*
3100Sstevel@tonic-gate 		 * We have at least one acceptor.
3110Sstevel@tonic-gate 		 */
3120Sstevel@tonic-gate 		accepted = B_TRUE;
3130Sstevel@tonic-gate 		for (;;) {
3140Sstevel@tonic-gate 			/*
3150Sstevel@tonic-gate 			 * Find the next dls_impl_t that will accept the
3160Sstevel@tonic-gate 			 * sub-chain.
3170Sstevel@tonic-gate 			 */
3180Sstevel@tonic-gate 			for (ndip = dip->di_nextp; ndip != NULL;
3190Sstevel@tonic-gate 			    ndip = ndip->di_nextp)
320*2311Sseb 				if (dls_accept(ndip, &mhi, &ndi_rx,
321449Sericheng 				    &ndi_rx_arg))
3220Sstevel@tonic-gate 					break;
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 			/*
3250Sstevel@tonic-gate 			 * If there are no more dls_impl_t that are willing
3260Sstevel@tonic-gate 			 * to accept the sub-chain then we don't need to dup
3270Sstevel@tonic-gate 			 * it before handing it to the current one.
3280Sstevel@tonic-gate 			 */
3290Sstevel@tonic-gate 			if (ndip == NULL) {
330*2311Sseb 				di_rx(di_rx_arg, mrh, mp, mhi.mhi_hdrsize);
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 				/*
3330Sstevel@tonic-gate 				 * Since there are no more dls_impl_t, we're
3340Sstevel@tonic-gate 				 * done.
3350Sstevel@tonic-gate 				 */
3360Sstevel@tonic-gate 				break;
3370Sstevel@tonic-gate 			}
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 			/*
3400Sstevel@tonic-gate 			 * There are more dls_impl_t so dup the sub-chain.
3410Sstevel@tonic-gate 			 */
3420Sstevel@tonic-gate 			if ((nmp = copymsgchain(mp)) != NULL)
343*2311Sseb 				di_rx(di_rx_arg, mrh, nmp, mhi.mhi_hdrsize);
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 			dip = ndip;
346449Sericheng 			di_rx = ndi_rx;
347449Sericheng 			di_rx_arg = ndi_rx_arg;
3480Sstevel@tonic-gate 		}
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 		/*
3510Sstevel@tonic-gate 		 * Release the hold on the dls_impl_t chain now that we have
3520Sstevel@tonic-gate 		 * finished walking it.
3530Sstevel@tonic-gate 		 */
354269Sericheng 		i_dls_head_rele(dhp);
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate loop:
3570Sstevel@tonic-gate 		/*
3580Sstevel@tonic-gate 		 * If there were no acceptors then add the packet count to the
3590Sstevel@tonic-gate 		 * 'unknown' count.
3600Sstevel@tonic-gate 		 */
3610Sstevel@tonic-gate 		if (!accepted)
3620Sstevel@tonic-gate 			atomic_add_32(&(dlp->dl_unknowns), npacket);
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 		/*
3650Sstevel@tonic-gate 		 * Move onto the next sub-chain.
3660Sstevel@tonic-gate 		 */
3670Sstevel@tonic-gate 		mp = nextp;
3680Sstevel@tonic-gate 	}
3690Sstevel@tonic-gate }
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate static void
372*2311Sseb i_dls_link_rx_promisc(void *arg, mac_resource_handle_t mrh, mblk_t *mp)
3730Sstevel@tonic-gate {
3740Sstevel@tonic-gate 	dls_link_t			*dlp = arg;
375269Sericheng 	mod_hash_t			*hash = dlp->dl_impl_hash;
3760Sstevel@tonic-gate 	mblk_t				*nextp;
377*2311Sseb 	mac_header_info_t		mhi;
3780Sstevel@tonic-gate 	uint16_t			vid;
379269Sericheng 	dls_head_t			*dhp;
3800Sstevel@tonic-gate 	dls_impl_t			*dip;
3810Sstevel@tonic-gate 	dls_impl_t			*ndip;
3820Sstevel@tonic-gate 	mblk_t				*nmp;
383269Sericheng 	mod_hash_key_t			key;
3840Sstevel@tonic-gate 	uint_t				npacket;
3850Sstevel@tonic-gate 	boolean_t			accepted;
386449Sericheng 	dls_rx_t			di_rx, ndi_rx;
387449Sericheng 	void				*di_rx_arg, *ndi_rx_arg;
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	/*
3900Sstevel@tonic-gate 	 * Walk the packet chain.
3910Sstevel@tonic-gate 	 */
3920Sstevel@tonic-gate 	while (mp != NULL) {
3930Sstevel@tonic-gate 		/*
3940Sstevel@tonic-gate 		 * Wipe the accepted state.
3950Sstevel@tonic-gate 		 */
3960Sstevel@tonic-gate 		accepted = B_FALSE;
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 		/*
3990Sstevel@tonic-gate 		 * Grab the longest sub-chain we can process as a single
4000Sstevel@tonic-gate 		 * unit.
4010Sstevel@tonic-gate 		 */
402*2311Sseb 		nextp = i_dls_link_subchain(dlp, mp, &mhi, &vid, &npacket);
403*2311Sseb 
404*2311Sseb 		if (npacket == 0) {
405*2311Sseb 			/*
406*2311Sseb 			 * The first packet had an unrecognized header.
407*2311Sseb 			 * Modify npacket so that this stray can be
408*2311Sseb 			 * accounted for.
409*2311Sseb 			 */
410*2311Sseb 			npacket = 1;
411*2311Sseb 			freemsg(mp);
412*2311Sseb 			goto loop;
413*2311Sseb 		}
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 		/*
4160Sstevel@tonic-gate 		 * Construct a hash key from the VLAN identifier and the
4170Sstevel@tonic-gate 		 * DLSAP that represents dls_impl_t in promiscuous mode.
4180Sstevel@tonic-gate 		 */
4190Sstevel@tonic-gate 		key = MAKE_KEY(DLS_SAP_PROMISC, vid);
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 		/*
4220Sstevel@tonic-gate 		 * Search the has table for dls_impl_t eligible to receive
4230Sstevel@tonic-gate 		 * a packet chain for this DLSAP/VLAN combination.
4240Sstevel@tonic-gate 		 */
425269Sericheng 		rw_enter(&dlp->dl_impl_lock, RW_READER);
426269Sericheng 		if (mod_hash_find(hash, key, (mod_hash_val_t *)&dhp) != 0) {
427269Sericheng 			rw_exit(&dlp->dl_impl_lock);
4280Sstevel@tonic-gate 			goto non_promisc;
4290Sstevel@tonic-gate 		}
430269Sericheng 		i_dls_head_hold(dhp);
431269Sericheng 		rw_exit(&dlp->dl_impl_lock);
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 		/*
4340Sstevel@tonic-gate 		 * Find dls_impl_t that will accept the sub-chain.
4350Sstevel@tonic-gate 		 */
436269Sericheng 		for (dip = dhp->dh_list; dip != NULL; dip = dip->di_nextp) {
437*2311Sseb 			if (!dls_accept(dip, &mhi, &di_rx, &di_rx_arg))
4380Sstevel@tonic-gate 				continue;
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 			/*
4410Sstevel@tonic-gate 			 * We have at least one acceptor.
4420Sstevel@tonic-gate 			 */
4430Sstevel@tonic-gate 			accepted = B_TRUE;
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 			/*
4460Sstevel@tonic-gate 			 * There will normally be at least more dls_impl_t
4470Sstevel@tonic-gate 			 * (since we've yet to check for non-promiscuous
4480Sstevel@tonic-gate 			 * dls_impl_t) so dup the sub-chain.
4490Sstevel@tonic-gate 			 */
4500Sstevel@tonic-gate 			if ((nmp = copymsgchain(mp)) != NULL)
451*2311Sseb 				di_rx(di_rx_arg, mrh, nmp, mhi.mhi_hdrsize);
4520Sstevel@tonic-gate 		}
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 		/*
4550Sstevel@tonic-gate 		 * Release the hold on the dls_impl_t chain now that we have
4560Sstevel@tonic-gate 		 * finished walking it.
4570Sstevel@tonic-gate 		 */
458269Sericheng 		i_dls_head_rele(dhp);
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate non_promisc:
4610Sstevel@tonic-gate 		/*
4620Sstevel@tonic-gate 		 * Construct a hash key from the VLAN identifier and the
4630Sstevel@tonic-gate 		 * DLSAP.
4640Sstevel@tonic-gate 		 */
465*2311Sseb 		key = MAKE_KEY(mhi.mhi_bindsap, vid);
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 		/*
4680Sstevel@tonic-gate 		 * Search the has table for dls_impl_t eligible to receive
4690Sstevel@tonic-gate 		 * a packet chain for this DLSAP/VLAN combination.
4700Sstevel@tonic-gate 		 */
471269Sericheng 		rw_enter(&dlp->dl_impl_lock, RW_READER);
472269Sericheng 		if (mod_hash_find(hash, key, (mod_hash_val_t *)&dhp) != 0) {
473269Sericheng 			rw_exit(&dlp->dl_impl_lock);
4740Sstevel@tonic-gate 			freemsgchain(mp);
4750Sstevel@tonic-gate 			goto loop;
4760Sstevel@tonic-gate 		}
477269Sericheng 		i_dls_head_hold(dhp);
478269Sericheng 		rw_exit(&dlp->dl_impl_lock);
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 		/*
4810Sstevel@tonic-gate 		 * Find the first dls_impl_t that will accept the sub-chain.
4820Sstevel@tonic-gate 		 */
483269Sericheng 		for (dip = dhp->dh_list; dip != NULL; dip = dip->di_nextp)
484*2311Sseb 			if (dls_accept(dip, &mhi, &di_rx, &di_rx_arg))
4850Sstevel@tonic-gate 				break;
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 		/*
4880Sstevel@tonic-gate 		 * If we did not find any dls_impl_t willing to accept the
4890Sstevel@tonic-gate 		 * sub-chain then throw it away.
4900Sstevel@tonic-gate 		 */
4910Sstevel@tonic-gate 		if (dip == NULL) {
492269Sericheng 			i_dls_head_rele(dhp);
4930Sstevel@tonic-gate 			freemsgchain(mp);
4940Sstevel@tonic-gate 			goto loop;
4950Sstevel@tonic-gate 		}
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 		/*
4980Sstevel@tonic-gate 		 * We have at least one acceptor.
4990Sstevel@tonic-gate 		 */
5000Sstevel@tonic-gate 		accepted = B_TRUE;
5010Sstevel@tonic-gate 		for (;;) {
5020Sstevel@tonic-gate 			/*
5030Sstevel@tonic-gate 			 * Find the next dls_impl_t that will accept the
5040Sstevel@tonic-gate 			 * sub-chain.
5050Sstevel@tonic-gate 			 */
5060Sstevel@tonic-gate 			for (ndip = dip->di_nextp; ndip != NULL;
5070Sstevel@tonic-gate 			    ndip = ndip->di_nextp)
508*2311Sseb 				if (dls_accept(ndip, &mhi, &ndi_rx,
509449Sericheng 				    &ndi_rx_arg))
5100Sstevel@tonic-gate 					break;
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 			/*
5130Sstevel@tonic-gate 			 * If there are no more dls_impl_t that are willing
5140Sstevel@tonic-gate 			 * to accept the sub-chain then we don't need to dup
5150Sstevel@tonic-gate 			 * it before handing it to the current one.
5160Sstevel@tonic-gate 			 */
5170Sstevel@tonic-gate 			if (ndip == NULL) {
518*2311Sseb 				di_rx(di_rx_arg, mrh, mp, mhi.mhi_hdrsize);
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 				/*
5210Sstevel@tonic-gate 				 * Since there are no more dls_impl_t, we're
5220Sstevel@tonic-gate 				 * done.
5230Sstevel@tonic-gate 				 */
5240Sstevel@tonic-gate 				break;
5250Sstevel@tonic-gate 			}
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 			/*
5280Sstevel@tonic-gate 			 * There are more dls_impl_t so dup the sub-chain.
5290Sstevel@tonic-gate 			 */
5300Sstevel@tonic-gate 			if ((nmp = copymsgchain(mp)) != NULL)
531*2311Sseb 				di_rx(di_rx_arg, mrh, nmp, mhi.mhi_hdrsize);
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 			dip = ndip;
534449Sericheng 			di_rx = ndi_rx;
535449Sericheng 			di_rx_arg = ndi_rx_arg;
5360Sstevel@tonic-gate 		}
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 		/*
5390Sstevel@tonic-gate 		 * Release the hold on the dls_impl_t chain now that we have
5400Sstevel@tonic-gate 		 * finished walking it.
5410Sstevel@tonic-gate 		 */
542269Sericheng 		i_dls_head_rele(dhp);
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate loop:
5450Sstevel@tonic-gate 		/*
5460Sstevel@tonic-gate 		 * If there were no acceptors then add the packet count to the
5470Sstevel@tonic-gate 		 * 'unknown' count.
5480Sstevel@tonic-gate 		 */
5490Sstevel@tonic-gate 		if (!accepted)
5500Sstevel@tonic-gate 			atomic_add_32(&(dlp->dl_unknowns), npacket);
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 		/*
5530Sstevel@tonic-gate 		 * Move onto the next sub-chain.
5540Sstevel@tonic-gate 		 */
5550Sstevel@tonic-gate 		mp = nextp;
5560Sstevel@tonic-gate 	}
5570Sstevel@tonic-gate }
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate static void
560*2311Sseb i_dls_link_txloop(void *arg, mblk_t *mp)
5610Sstevel@tonic-gate {
5620Sstevel@tonic-gate 	dls_link_t			*dlp = arg;
563269Sericheng 	mod_hash_t			*hash = dlp->dl_impl_hash;
5640Sstevel@tonic-gate 	mblk_t				*nextp;
565*2311Sseb 	mac_header_info_t		mhi;
5660Sstevel@tonic-gate 	uint16_t			vid;
567269Sericheng 	dls_head_t			*dhp;
5680Sstevel@tonic-gate 	dls_impl_t			*dip;
5690Sstevel@tonic-gate 	dls_impl_t			*ndip;
5700Sstevel@tonic-gate 	mblk_t				*nmp;
571269Sericheng 	mod_hash_key_t			key;
5720Sstevel@tonic-gate 	uint_t				npacket;
573449Sericheng 	dls_rx_t			di_rx, ndi_rx;
574449Sericheng 	void				*di_rx_arg, *ndi_rx_arg;
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	/*
5770Sstevel@tonic-gate 	 * Walk the packet chain.
5780Sstevel@tonic-gate 	 */
5790Sstevel@tonic-gate 	while (mp != NULL) {
5800Sstevel@tonic-gate 		/*
5810Sstevel@tonic-gate 		 * Grab the longest sub-chain we can process as a single
5820Sstevel@tonic-gate 		 * unit.
5830Sstevel@tonic-gate 		 */
584*2311Sseb 		nextp = i_dls_link_subchain(dlp, mp, &mhi, &vid, &npacket);
5850Sstevel@tonic-gate 
586*2311Sseb 		if (npacket == 0) {
587*2311Sseb 			freemsg(mp);
588*2311Sseb 			goto loop;
589*2311Sseb 		}
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 		/*
5920Sstevel@tonic-gate 		 * Construct a hash key from the VLAN identifier and the
5930Sstevel@tonic-gate 		 * DLSAP.
5940Sstevel@tonic-gate 		 */
595*2311Sseb 		key = MAKE_KEY(mhi.mhi_bindsap, vid);
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 		/*
5980Sstevel@tonic-gate 		 * Search the has table for dls_impl_t eligible to receive
5990Sstevel@tonic-gate 		 * a packet chain for this DLSAP/VLAN combination.
6000Sstevel@tonic-gate 		 */
601269Sericheng 		rw_enter(&dlp->dl_impl_lock, RW_READER);
602269Sericheng 		if (mod_hash_find(hash, key, (mod_hash_val_t *)&dhp) != 0) {
603269Sericheng 			rw_exit(&dlp->dl_impl_lock);
6040Sstevel@tonic-gate 			goto promisc;
6050Sstevel@tonic-gate 		}
606269Sericheng 		i_dls_head_hold(dhp);
607269Sericheng 		rw_exit(&dlp->dl_impl_lock);
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 		/*
6100Sstevel@tonic-gate 		 * Find dls_impl_t that will accept the sub-chain.
6110Sstevel@tonic-gate 		 */
612269Sericheng 		for (dip = dhp->dh_list; dip != NULL; dip = dip->di_nextp) {
613*2311Sseb 			if (!dls_accept_loopback(dip, &di_rx, &di_rx_arg))
6140Sstevel@tonic-gate 				continue;
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 			/*
6170Sstevel@tonic-gate 			 * There should be at least more dls_impl_t (since
6180Sstevel@tonic-gate 			 * we've yet to check for dls_impl_t in promiscuous
6190Sstevel@tonic-gate 			 * mode) so dup the sub-chain.
6200Sstevel@tonic-gate 			 */
6210Sstevel@tonic-gate 			if ((nmp = copymsgchain(mp)) != NULL)
622*2311Sseb 				di_rx(di_rx_arg, NULL, nmp, mhi.mhi_hdrsize);
6230Sstevel@tonic-gate 		}
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 		/*
6260Sstevel@tonic-gate 		 * Release the hold on the dls_impl_t chain now that we have
6270Sstevel@tonic-gate 		 * finished walking it.
6280Sstevel@tonic-gate 		 */
629269Sericheng 		i_dls_head_rele(dhp);
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate promisc:
6320Sstevel@tonic-gate 		/*
6330Sstevel@tonic-gate 		 * Construct a hash key from the VLAN identifier and the
6340Sstevel@tonic-gate 		 * DLSAP that represents dls_impl_t in promiscuous mode.
6350Sstevel@tonic-gate 		 */
6360Sstevel@tonic-gate 		key = MAKE_KEY(DLS_SAP_PROMISC, vid);
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 		/*
6390Sstevel@tonic-gate 		 * Search the has table for dls_impl_t eligible to receive
6400Sstevel@tonic-gate 		 * a packet chain for this DLSAP/VLAN combination.
6410Sstevel@tonic-gate 		 */
642269Sericheng 		rw_enter(&dlp->dl_impl_lock, RW_READER);
643269Sericheng 		if (mod_hash_find(hash, key, (mod_hash_val_t *)&dhp) != 0) {
644269Sericheng 			rw_exit(&dlp->dl_impl_lock);
6450Sstevel@tonic-gate 			freemsgchain(mp);
6460Sstevel@tonic-gate 			goto loop;
6470Sstevel@tonic-gate 		}
648269Sericheng 		i_dls_head_hold(dhp);
649269Sericheng 		rw_exit(&dlp->dl_impl_lock);
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 		/*
6520Sstevel@tonic-gate 		 * Find the first dls_impl_t that will accept the sub-chain.
6530Sstevel@tonic-gate 		 */
654269Sericheng 		for (dip = dhp->dh_list; dip != NULL; dip = dip->di_nextp)
655*2311Sseb 			if (dls_accept_loopback(dip, &di_rx, &di_rx_arg))
6560Sstevel@tonic-gate 				break;
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 		/*
6590Sstevel@tonic-gate 		 * If we did not find any dls_impl_t willing to accept the
6600Sstevel@tonic-gate 		 * sub-chain then throw it away.
6610Sstevel@tonic-gate 		 */
6620Sstevel@tonic-gate 		if (dip == NULL) {
663269Sericheng 			i_dls_head_rele(dhp);
6640Sstevel@tonic-gate 			freemsgchain(mp);
6650Sstevel@tonic-gate 			goto loop;
6660Sstevel@tonic-gate 		}
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 		for (;;) {
6690Sstevel@tonic-gate 			/*
6700Sstevel@tonic-gate 			 * Find the next dls_impl_t that will accept the
6710Sstevel@tonic-gate 			 * sub-chain.
6720Sstevel@tonic-gate 			 */
6730Sstevel@tonic-gate 			for (ndip = dip->di_nextp; ndip != NULL;
6740Sstevel@tonic-gate 			    ndip = ndip->di_nextp)
675*2311Sseb 				if (dls_accept_loopback(ndip, &ndi_rx,
676*2311Sseb 				    &ndi_rx_arg)) {
6770Sstevel@tonic-gate 					break;
678*2311Sseb 				}
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 			/*
6810Sstevel@tonic-gate 			 * If there are no more dls_impl_t that are willing
6820Sstevel@tonic-gate 			 * to accept the sub-chain then we don't need to dup
6830Sstevel@tonic-gate 			 * it before handing it to the current one.
6840Sstevel@tonic-gate 			 */
6850Sstevel@tonic-gate 			if (ndip == NULL) {
686*2311Sseb 				di_rx(di_rx_arg, NULL, mp, mhi.mhi_hdrsize);
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 				/*
6890Sstevel@tonic-gate 				 * Since there are no more dls_impl_t, we're
6900Sstevel@tonic-gate 				 * done.
6910Sstevel@tonic-gate 				 */
6920Sstevel@tonic-gate 				break;
6930Sstevel@tonic-gate 			}
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 			/*
6960Sstevel@tonic-gate 			 * There are more dls_impl_t so dup the sub-chain.
6970Sstevel@tonic-gate 			 */
6980Sstevel@tonic-gate 			if ((nmp = copymsgchain(mp)) != NULL)
699*2311Sseb 				di_rx(di_rx_arg, NULL, nmp, mhi.mhi_hdrsize);
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 			dip = ndip;
702449Sericheng 			di_rx = ndi_rx;
703449Sericheng 			di_rx_arg = ndi_rx_arg;
7040Sstevel@tonic-gate 		}
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 		/*
7070Sstevel@tonic-gate 		 * Release the hold on the dls_impl_t chain now that we have
7080Sstevel@tonic-gate 		 * finished walking it.
7090Sstevel@tonic-gate 		 */
710269Sericheng 		i_dls_head_rele(dhp);
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate loop:
7130Sstevel@tonic-gate 		/*
7140Sstevel@tonic-gate 		 * Move onto the next sub-chain.
7150Sstevel@tonic-gate 		 */
7160Sstevel@tonic-gate 		mp = nextp;
7170Sstevel@tonic-gate 	}
7180Sstevel@tonic-gate }
7190Sstevel@tonic-gate 
720269Sericheng /*ARGSUSED*/
721269Sericheng static uint_t
722269Sericheng i_dls_link_walk(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
7230Sstevel@tonic-gate {
7240Sstevel@tonic-gate 	boolean_t	*promiscp = arg;
7250Sstevel@tonic-gate 	uint32_t	sap = KEY_SAP(key);
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 	if (sap == DLS_SAP_PROMISC) {
7280Sstevel@tonic-gate 		*promiscp = B_TRUE;
729269Sericheng 		return (MH_WALK_TERMINATE);
7300Sstevel@tonic-gate 	}
7310Sstevel@tonic-gate 
732269Sericheng 	return (MH_WALK_CONTINUE);
7330Sstevel@tonic-gate }
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate static int
736*2311Sseb i_dls_link_create(const char *name, uint_t ddi_instance, dls_link_t **dlpp)
7370Sstevel@tonic-gate {
7380Sstevel@tonic-gate 	dls_link_t		*dlp;
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 	/*
7410Sstevel@tonic-gate 	 * Allocate a new dls_link_t structure.
7420Sstevel@tonic-gate 	 */
7430Sstevel@tonic-gate 	dlp = kmem_cache_alloc(i_dls_link_cachep, KM_SLEEP);
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 	/*
7460Sstevel@tonic-gate 	 * Name the dls_link_t after the MAC interface it represents.
7470Sstevel@tonic-gate 	 */
748*2311Sseb 	(void) strlcpy(dlp->dl_name, name, sizeof (dlp->dl_name));
749*2311Sseb 	dlp->dl_ddi_instance = ddi_instance;
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	/*
7520Sstevel@tonic-gate 	 * Set the packet loopback function for use when the MAC is in
7530Sstevel@tonic-gate 	 * promiscuous mode, and initialize promiscuous bookeeping fields.
7540Sstevel@tonic-gate 	 */
755*2311Sseb 	dlp->dl_txloop = i_dls_link_txloop;
7560Sstevel@tonic-gate 	dlp->dl_npromisc = 0;
7570Sstevel@tonic-gate 	dlp->dl_mth = NULL;
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	*dlpp = dlp;
7600Sstevel@tonic-gate 	return (0);
7610Sstevel@tonic-gate }
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate static void
7640Sstevel@tonic-gate i_dls_link_destroy(dls_link_t *dlp)
7650Sstevel@tonic-gate {
7660Sstevel@tonic-gate 	ASSERT(dlp->dl_npromisc == 0);
7670Sstevel@tonic-gate 	ASSERT(dlp->dl_nactive == 0);
7680Sstevel@tonic-gate 	ASSERT(dlp->dl_mth == NULL);
7690Sstevel@tonic-gate 	ASSERT(dlp->dl_macref == 0);
7700Sstevel@tonic-gate 	ASSERT(dlp->dl_mh == NULL);
7710Sstevel@tonic-gate 	ASSERT(dlp->dl_mip == NULL);
772269Sericheng 	ASSERT(dlp->dl_impl_count == 0);
773269Sericheng 	ASSERT(dlp->dl_mrh == NULL);
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	/*
7760Sstevel@tonic-gate 	 * Free the structure back to the cache.
7770Sstevel@tonic-gate 	 */
7780Sstevel@tonic-gate 	dlp->dl_unknowns = 0;
7790Sstevel@tonic-gate 	kmem_cache_free(i_dls_link_cachep, dlp);
7800Sstevel@tonic-gate }
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate /*
7830Sstevel@tonic-gate  * Module initialization functions.
7840Sstevel@tonic-gate  */
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate void
7870Sstevel@tonic-gate dls_link_init(void)
7880Sstevel@tonic-gate {
7890Sstevel@tonic-gate 	/*
7900Sstevel@tonic-gate 	 * Create a kmem_cache of dls_link_t structures.
7910Sstevel@tonic-gate 	 */
7920Sstevel@tonic-gate 	i_dls_link_cachep = kmem_cache_create("dls_link_cache",
7930Sstevel@tonic-gate 	    sizeof (dls_link_t), 0, i_dls_link_constructor,
7940Sstevel@tonic-gate 	    i_dls_link_destructor, NULL, NULL, NULL, 0);
7950Sstevel@tonic-gate 	ASSERT(i_dls_link_cachep != NULL);
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 	/*
798269Sericheng 	 * Create a dls_link_t hash table and associated lock.
7990Sstevel@tonic-gate 	 */
800269Sericheng 	i_dls_link_hash = mod_hash_create_extended("dls_link_hash",
801269Sericheng 	    IMPL_HASHSZ, mod_hash_null_keydtor, mod_hash_null_valdtor,
802269Sericheng 	    mod_hash_bystr, NULL, mod_hash_strkey_cmp, KM_SLEEP);
803269Sericheng 	rw_init(&i_dls_link_lock, NULL, RW_DEFAULT, NULL);
804269Sericheng 	i_dls_link_count = 0;
8050Sstevel@tonic-gate }
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate int
8080Sstevel@tonic-gate dls_link_fini(void)
8090Sstevel@tonic-gate {
810269Sericheng 	if (i_dls_link_count > 0)
811269Sericheng 		return (EBUSY);
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 	/*
8140Sstevel@tonic-gate 	 * Destroy the kmem_cache.
8150Sstevel@tonic-gate 	 */
8160Sstevel@tonic-gate 	kmem_cache_destroy(i_dls_link_cachep);
817269Sericheng 
818269Sericheng 	/*
819269Sericheng 	 * Destroy the hash table and associated lock.
820269Sericheng 	 */
821269Sericheng 	mod_hash_destroy_hash(i_dls_link_hash);
822269Sericheng 	rw_destroy(&i_dls_link_lock);
8230Sstevel@tonic-gate 	return (0);
8240Sstevel@tonic-gate }
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate /*
8270Sstevel@tonic-gate  * Exported functions.
8280Sstevel@tonic-gate  */
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate int
831*2311Sseb dls_link_hold(const char *name, uint_t ddi_instance, dls_link_t **dlpp)
8320Sstevel@tonic-gate {
8330Sstevel@tonic-gate 	dls_link_t		*dlp;
8340Sstevel@tonic-gate 	int			err;
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	/*
8370Sstevel@tonic-gate 	 * Look up a dls_link_t corresponding to the given mac_handle_t
838269Sericheng 	 * in the global hash table. We need to hold i_dls_link_lock in
839269Sericheng 	 * order to atomically find and insert a dls_link_t into the
840269Sericheng 	 * hash table.
8410Sstevel@tonic-gate 	 */
842269Sericheng 	rw_enter(&i_dls_link_lock, RW_WRITER);
843269Sericheng 	if ((err = mod_hash_find(i_dls_link_hash, (mod_hash_key_t)name,
844269Sericheng 	    (mod_hash_val_t *)&dlp)) == 0)
8450Sstevel@tonic-gate 		goto done;
846269Sericheng 
847269Sericheng 	ASSERT(err == MH_ERR_NOTFOUND);
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 	/*
8500Sstevel@tonic-gate 	 * We didn't find anything so we need to create one.
8510Sstevel@tonic-gate 	 */
852*2311Sseb 	if ((err = i_dls_link_create(name, ddi_instance, &dlp)) != 0) {
853269Sericheng 		rw_exit(&i_dls_link_lock);
8540Sstevel@tonic-gate 		return (err);
8550Sstevel@tonic-gate 	}
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	/*
858269Sericheng 	 * Insert the dls_link_t.
8590Sstevel@tonic-gate 	 */
860*2311Sseb 	err = mod_hash_insert(i_dls_link_hash, (mod_hash_key_t)name,
861269Sericheng 	    (mod_hash_val_t)dlp);
8620Sstevel@tonic-gate 	ASSERT(err == 0);
8630Sstevel@tonic-gate 
864269Sericheng 	i_dls_link_count++;
865269Sericheng 	ASSERT(i_dls_link_count != 0);
866269Sericheng 
8670Sstevel@tonic-gate done:
8680Sstevel@tonic-gate 	/*
8690Sstevel@tonic-gate 	 * Bump the reference count and hand back the reference.
8700Sstevel@tonic-gate 	 */
8710Sstevel@tonic-gate 	dlp->dl_ref++;
8720Sstevel@tonic-gate 	*dlpp = dlp;
873269Sericheng 	rw_exit(&i_dls_link_lock);
874269Sericheng 	return (0);
8750Sstevel@tonic-gate }
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate void
8780Sstevel@tonic-gate dls_link_rele(dls_link_t *dlp)
8790Sstevel@tonic-gate {
880269Sericheng 	mod_hash_val_t	val;
8810Sstevel@tonic-gate 
882269Sericheng 	rw_enter(&i_dls_link_lock, RW_WRITER);
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 	/*
8850Sstevel@tonic-gate 	 * Check if there are any more references.
8860Sstevel@tonic-gate 	 */
8870Sstevel@tonic-gate 	if (--dlp->dl_ref != 0) {
8880Sstevel@tonic-gate 		/*
8890Sstevel@tonic-gate 		 * There are more references so there's nothing more to do.
8900Sstevel@tonic-gate 		 */
8910Sstevel@tonic-gate 		goto done;
8920Sstevel@tonic-gate 	}
8930Sstevel@tonic-gate 
894269Sericheng 	(void) mod_hash_remove(i_dls_link_hash,
895269Sericheng 	    (mod_hash_key_t)dlp->dl_name, &val);
896269Sericheng 	ASSERT(dlp == (dls_link_t *)val);
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 	/*
8990Sstevel@tonic-gate 	 * Destroy the dls_link_t.
9000Sstevel@tonic-gate 	 */
9010Sstevel@tonic-gate 	i_dls_link_destroy(dlp);
902269Sericheng 	ASSERT(i_dls_link_count > 0);
903269Sericheng 	i_dls_link_count--;
9040Sstevel@tonic-gate done:
905269Sericheng 	rw_exit(&i_dls_link_lock);
9060Sstevel@tonic-gate }
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate int
9090Sstevel@tonic-gate dls_mac_hold(dls_link_t *dlp)
9100Sstevel@tonic-gate {
9110Sstevel@tonic-gate 	int err = 0;
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate 	mutex_enter(&dlp->dl_lock);
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 	ASSERT(IMPLY(dlp->dl_macref != 0, dlp->dl_mh != NULL));
9160Sstevel@tonic-gate 	ASSERT(IMPLY(dlp->dl_macref == 0, dlp->dl_mh == NULL));
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 	if (dlp->dl_macref == 0) {
9190Sstevel@tonic-gate 		/*
9200Sstevel@tonic-gate 		 * First reference; hold open the MAC interface.
9210Sstevel@tonic-gate 		 */
922*2311Sseb 		err = mac_open(dlp->dl_name, dlp->dl_ddi_instance, &dlp->dl_mh);
9230Sstevel@tonic-gate 		if (err != 0)
9240Sstevel@tonic-gate 			goto done;
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 		dlp->dl_mip = mac_info(dlp->dl_mh);
9270Sstevel@tonic-gate 	}
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 	dlp->dl_macref++;
9300Sstevel@tonic-gate done:
9310Sstevel@tonic-gate 	mutex_exit(&dlp->dl_lock);
9320Sstevel@tonic-gate 	return (err);
9330Sstevel@tonic-gate }
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate void
9360Sstevel@tonic-gate dls_mac_rele(dls_link_t *dlp)
9370Sstevel@tonic-gate {
9380Sstevel@tonic-gate 	mutex_enter(&dlp->dl_lock);
9390Sstevel@tonic-gate 	ASSERT(dlp->dl_mh != NULL);
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 	if (--dlp->dl_macref == 0) {
9420Sstevel@tonic-gate 		mac_close(dlp->dl_mh);
9430Sstevel@tonic-gate 		dlp->dl_mh = NULL;
9440Sstevel@tonic-gate 		dlp->dl_mip = NULL;
9450Sstevel@tonic-gate 	}
9460Sstevel@tonic-gate 	mutex_exit(&dlp->dl_lock);
9470Sstevel@tonic-gate }
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate void
9500Sstevel@tonic-gate dls_link_add(dls_link_t *dlp, uint32_t sap, dls_impl_t *dip)
9510Sstevel@tonic-gate {
9520Sstevel@tonic-gate 	dls_vlan_t	*dvp = dip->di_dvp;
953269Sericheng 	mod_hash_t	*hash = dlp->dl_impl_hash;
954269Sericheng 	mod_hash_key_t	key;
955269Sericheng 	dls_head_t	*dhp;
9560Sstevel@tonic-gate 	dls_impl_t	*p;
9570Sstevel@tonic-gate 	mac_rx_t	rx;
9580Sstevel@tonic-gate 	int		err;
959269Sericheng 	boolean_t	promisc = B_FALSE;
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 	/*
962*2311Sseb 	 * Generate a hash key based on the sap and the VLAN id.
9630Sstevel@tonic-gate 	 */
9640Sstevel@tonic-gate 	key = MAKE_KEY(sap, dvp->dv_id);
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 	/*
9670Sstevel@tonic-gate 	 * We need dl_lock here because we want to be able to walk
9680Sstevel@tonic-gate 	 * the hash table *and* set the mac rx func atomically. if
9690Sstevel@tonic-gate 	 * these two operations are separate, someone else could
970269Sericheng 	 * insert/remove dls_impl_t from the hash table after we
971269Sericheng 	 * drop the hash lock and this could cause our chosen rx
972269Sericheng 	 * func to be incorrect. note that we cannot call mac_rx_add
973269Sericheng 	 * when holding the hash lock because this can cause deadlock.
9740Sstevel@tonic-gate 	 */
9750Sstevel@tonic-gate 	mutex_enter(&dlp->dl_lock);
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 	/*
978269Sericheng 	 * Search the table for a list head with this key.
9790Sstevel@tonic-gate 	 */
980269Sericheng 	rw_enter(&dlp->dl_impl_lock, RW_WRITER);
9810Sstevel@tonic-gate 
982269Sericheng 	if ((err = mod_hash_find(hash, key, (mod_hash_val_t *)&dhp)) != 0) {
983269Sericheng 		ASSERT(err == MH_ERR_NOTFOUND);
9840Sstevel@tonic-gate 
985269Sericheng 		dhp = i_dls_head_alloc(key);
986269Sericheng 		err = mod_hash_insert(hash, key, (mod_hash_val_t)dhp);
987269Sericheng 		ASSERT(err == 0);
9880Sstevel@tonic-gate 	}
9890Sstevel@tonic-gate 
9900Sstevel@tonic-gate 	/*
991269Sericheng 	 * Add the dls_impl_t to the head of the list.
992269Sericheng 	 */
993269Sericheng 	ASSERT(dip->di_nextp == NULL);
994269Sericheng 	p = dhp->dh_list;
995269Sericheng 	dip->di_nextp = p;
996269Sericheng 	dhp->dh_list = dip;
997269Sericheng 
998269Sericheng 	/*
999269Sericheng 	 * Save a pointer to the list head.
1000269Sericheng 	 */
1001269Sericheng 	dip->di_headp = dhp;
1002269Sericheng 	dlp->dl_impl_count++;
1003269Sericheng 
1004269Sericheng 	/*
1005269Sericheng 	 * Walk the bound dls_impl_t to see if there are any
1006269Sericheng 	 * in promiscuous 'all sap' mode.
10070Sstevel@tonic-gate 	 */
1008269Sericheng 	mod_hash_walk(hash, i_dls_link_walk, (void *)&promisc);
1009269Sericheng 	rw_exit(&dlp->dl_impl_lock);
1010269Sericheng 
1011269Sericheng 	/*
1012269Sericheng 	 * If there are then we need to use a receive routine
1013269Sericheng 	 * which will route packets to those dls_impl_t as well
1014269Sericheng 	 * as ones bound to the  DLSAP of the packet.
1015269Sericheng 	 */
1016269Sericheng 	if (promisc)
1017*2311Sseb 		rx = i_dls_link_rx_promisc;
1018269Sericheng 	else
1019*2311Sseb 		rx = i_dls_link_rx;
1020269Sericheng 
1021269Sericheng 	/* Replace the existing receive function if there is one. */
1022269Sericheng 	if (dlp->dl_mrh != NULL)
1023269Sericheng 		mac_rx_remove(dlp->dl_mh, dlp->dl_mrh);
1024269Sericheng 	dlp->dl_mrh = mac_rx_add(dlp->dl_mh, rx, (void *)dlp);
1025269Sericheng 	mutex_exit(&dlp->dl_lock);
1026269Sericheng }
1027269Sericheng 
1028269Sericheng void
1029269Sericheng dls_link_remove(dls_link_t *dlp, dls_impl_t *dip)
1030269Sericheng {
1031269Sericheng 	mod_hash_t	*hash = dlp->dl_impl_hash;
1032269Sericheng 	dls_impl_t	**pp;
1033269Sericheng 	dls_impl_t	*p;
1034269Sericheng 	dls_head_t	*dhp;
1035269Sericheng 	mac_rx_t	rx;
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 	/*
1038269Sericheng 	 * We need dl_lock here because we want to be able to walk
1039269Sericheng 	 * the hash table *and* set the mac rx func atomically. if
1040269Sericheng 	 * these two operations are separate, someone else could
1041269Sericheng 	 * insert/remove dls_impl_t from the hash table after we
1042269Sericheng 	 * drop the hash lock and this could cause our chosen rx
1043269Sericheng 	 * func to be incorrect. note that we cannot call mac_rx_add
1044269Sericheng 	 * when holding the hash lock because this can cause deadlock.
10450Sstevel@tonic-gate 	 */
1046269Sericheng 	mutex_enter(&dlp->dl_lock);
1047269Sericheng 	rw_enter(&dlp->dl_impl_lock, RW_WRITER);
10480Sstevel@tonic-gate 
1049269Sericheng 	/*
1050269Sericheng 	 * Poll the hash table entry until all references have been dropped.
1051269Sericheng 	 * We need to drop all locks before sleeping because we don't want
1052269Sericheng 	 * the interrupt handler to block. We set di_removing here to
1053269Sericheng 	 * tell the receive callbacks not to pass up packets anymore.
1054269Sericheng 	 * This is only a hint to quicken the decrease of the refcnt so
1055269Sericheng 	 * the assignment need not be protected by any lock.
1056269Sericheng 	 */
1057269Sericheng 	dhp = dip->di_headp;
1058269Sericheng 	dip->di_removing = B_TRUE;
1059269Sericheng 	while (dhp->dh_ref != 0) {
1060269Sericheng 		rw_exit(&dlp->dl_impl_lock);
1061269Sericheng 		mutex_exit(&dlp->dl_lock);
1062269Sericheng 		delay(drv_usectohz(1000));	/* 1ms delay */
1063269Sericheng 		mutex_enter(&dlp->dl_lock);
1064269Sericheng 		rw_enter(&dlp->dl_impl_lock, RW_WRITER);
1065269Sericheng 	}
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 	/*
1068269Sericheng 	 * Walk the list and remove the dls_impl_t.
10690Sstevel@tonic-gate 	 */
1070269Sericheng 	for (pp = &dhp->dh_list; (p = *pp) != NULL; pp = &(p->di_nextp)) {
1071269Sericheng 		if (p == dip)
1072269Sericheng 			break;
1073269Sericheng 	}
1074269Sericheng 	ASSERT(p != NULL);
1075269Sericheng 	*pp = p->di_nextp;
1076269Sericheng 	p->di_nextp = NULL;
1077269Sericheng 
1078269Sericheng 	ASSERT(dlp->dl_impl_count > 0);
1079269Sericheng 	dlp->dl_impl_count--;
10800Sstevel@tonic-gate 
1081269Sericheng 	if (dhp->dh_list == NULL) {
1082269Sericheng 		mod_hash_val_t	val = NULL;
1083269Sericheng 
1084269Sericheng 		/*
1085269Sericheng 		 * The list is empty so remove the hash table entry.
1086269Sericheng 		 */
1087269Sericheng 		(void) mod_hash_remove(hash, dhp->dh_key, &val);
1088269Sericheng 		ASSERT(dhp == (dls_head_t *)val);
1089269Sericheng 		i_dls_head_free(dhp);
1090269Sericheng 	}
1091269Sericheng 	dip->di_removing = B_FALSE;
1092269Sericheng 
10930Sstevel@tonic-gate 	/*
1094269Sericheng 	 * If there are no dls_impl_t then there's no need to register a
1095269Sericheng 	 * receive function with the mac.
10960Sstevel@tonic-gate 	 */
1097269Sericheng 	if (dlp->dl_impl_count == 0) {
1098269Sericheng 		rw_exit(&dlp->dl_impl_lock);
1099269Sericheng 		mac_rx_remove(dlp->dl_mh, dlp->dl_mrh);
1100269Sericheng 		dlp->dl_mrh = NULL;
11010Sstevel@tonic-gate 	} else {
11020Sstevel@tonic-gate 		boolean_t promisc = B_FALSE;
11030Sstevel@tonic-gate 
11040Sstevel@tonic-gate 		/*
11050Sstevel@tonic-gate 		 * Walk the bound dls_impl_t to see if there are any
11060Sstevel@tonic-gate 		 * in promiscuous 'all sap' mode.
11070Sstevel@tonic-gate 		 */
1108269Sericheng 		mod_hash_walk(hash, i_dls_link_walk, (void *)&promisc);
1109269Sericheng 		rw_exit(&dlp->dl_impl_lock);
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 		/*
11120Sstevel@tonic-gate 		 * If there are then we need to use a receive routine
11130Sstevel@tonic-gate 		 * which will route packets to those dls_impl_t as well
11140Sstevel@tonic-gate 		 * as ones bound to the  DLSAP of the packet.
11150Sstevel@tonic-gate 		 */
11160Sstevel@tonic-gate 		if (promisc)
1117*2311Sseb 			rx = i_dls_link_rx_promisc;
11180Sstevel@tonic-gate 		else
1119*2311Sseb 			rx = i_dls_link_rx;
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 		mac_rx_remove(dlp->dl_mh, dlp->dl_mrh);
11220Sstevel@tonic-gate 		dlp->dl_mrh = mac_rx_add(dlp->dl_mh, rx, (void *)dlp);
11230Sstevel@tonic-gate 	}
11240Sstevel@tonic-gate 	mutex_exit(&dlp->dl_lock);
11250Sstevel@tonic-gate }
1126*2311Sseb 
1127*2311Sseb int
1128*2311Sseb dls_link_header_info(dls_link_t *dlp, mblk_t *mp, mac_header_info_t *mhip,
1129*2311Sseb     uint16_t *vidp)
1130*2311Sseb {
1131*2311Sseb 	boolean_t	is_ethernet = (dlp->dl_mip->mi_media == DL_ETHER);
1132*2311Sseb 	int		err = 0;
1133*2311Sseb 
1134*2311Sseb 	if ((err = mac_header_info(dlp->dl_mh, mp, mhip)) != 0)
1135*2311Sseb 		return (err);
1136*2311Sseb 
1137*2311Sseb 	/*
1138*2311Sseb 	 * If this is a VLAN-tagged Ethernet packet, then the SAP in the
1139*2311Sseb 	 * mac_header_info_t as returned by mac_header_info() is VLAN_TPID.
1140*2311Sseb 	 * We need to grab the ethertype from the VLAN header.
1141*2311Sseb 	 */
1142*2311Sseb 	if (is_ethernet && (mhip->mhi_bindsap == VLAN_TPID)) {
1143*2311Sseb 		struct ether_vlan_header *evhp;
1144*2311Sseb 		uint16_t sap;
1145*2311Sseb 
1146*2311Sseb 		evhp = (struct ether_vlan_header *)mp->b_rptr;
1147*2311Sseb 		sap = ntohs(evhp->ether_type);
1148*2311Sseb 		(void) mac_sap_verify(dlp->dl_mh, sap, &mhip->mhi_bindsap);
1149*2311Sseb 		mhip->mhi_hdrsize = sizeof (struct ether_vlan_header);
1150*2311Sseb 		if (vidp != NULL)
1151*2311Sseb 			*vidp = VLAN_ID(ntohs(evhp->ether_tci));
1152*2311Sseb 	} else if (vidp != NULL) {
1153*2311Sseb 		*vidp = VLAN_ID_NONE;
1154*2311Sseb 	}
1155*2311Sseb 	return (0);
1156*2311Sseb }
1157