xref: /onnv-gate/usr/src/uts/common/io/gld.c (revision 2760:38f12e308f6d)
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
51521Syz147064  * Common Development and Distribution License (the "License").
61521Syz147064  * 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 /*
221521Syz147064  * 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  * gld - Generic LAN Driver Version 2, PSARC/1997/382
300Sstevel@tonic-gate  *
310Sstevel@tonic-gate  * This is a utility module that provides generic facilities for
320Sstevel@tonic-gate  * LAN	drivers.  The DLPI protocol and most STREAMS interfaces
330Sstevel@tonic-gate  * are handled here.
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * It no longer provides compatibility with drivers
360Sstevel@tonic-gate  * implemented according to the GLD v0 documentation published
370Sstevel@tonic-gate  * in 1993. (See PSARC 2003/728)
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include <sys/types.h>
420Sstevel@tonic-gate #include <sys/errno.h>
430Sstevel@tonic-gate #include <sys/stropts.h>
440Sstevel@tonic-gate #include <sys/stream.h>
450Sstevel@tonic-gate #include <sys/kmem.h>
460Sstevel@tonic-gate #include <sys/stat.h>
470Sstevel@tonic-gate #include <sys/modctl.h>
480Sstevel@tonic-gate #include <sys/kstat.h>
490Sstevel@tonic-gate #include <sys/debug.h>
500Sstevel@tonic-gate #include <sys/note.h>
510Sstevel@tonic-gate #include <sys/sysmacros.h>
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #include <sys/byteorder.h>
540Sstevel@tonic-gate #include <sys/strsun.h>
550Sstevel@tonic-gate #include <sys/strsubr.h>
560Sstevel@tonic-gate #include <sys/dlpi.h>
570Sstevel@tonic-gate #include <sys/pattr.h>
580Sstevel@tonic-gate #include <sys/ethernet.h>
590Sstevel@tonic-gate #include <sys/ib/clients/ibd/ibd.h>
600Sstevel@tonic-gate #include <sys/policy.h>
610Sstevel@tonic-gate #include <sys/atomic.h>
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #include <sys/multidata.h>
640Sstevel@tonic-gate #include <sys/gld.h>
650Sstevel@tonic-gate #include <sys/gldpriv.h>
660Sstevel@tonic-gate 
670Sstevel@tonic-gate #include <sys/ddi.h>
680Sstevel@tonic-gate #include <sys/sunddi.h>
690Sstevel@tonic-gate 
700Sstevel@tonic-gate /*
71*2760Sdg199075  * Macros to increment statistics.
72*2760Sdg199075  */
73*2760Sdg199075 
74*2760Sdg199075 /*
75*2760Sdg199075  * Increase kstats. Note this operation is not atomic. It can be used when
76*2760Sdg199075  * GLDM_LOCK_HELD_WRITE(macinfo).
770Sstevel@tonic-gate  */
78*2760Sdg199075 #define	BUMP(stats, vstats, stat, delta)	do {			\
79*2760Sdg199075 	((stats)->stat) += (delta);					\
80*2760Sdg199075 	_NOTE(CONSTANTCONDITION)					\
81*2760Sdg199075 	if ((vstats) != NULL)						\
82*2760Sdg199075 		((struct gld_stats *)(vstats))->stat += (delta);	\
83*2760Sdg199075 	_NOTE(CONSTANTCONDITION)					\
84*2760Sdg199075 } while (0)
85*2760Sdg199075 
86*2760Sdg199075 #define	ATOMIC_BUMP_STAT(stat, delta)	do {			\
870Sstevel@tonic-gate 	_NOTE(CONSTANTCONDITION)				\
88*2760Sdg199075 	if (sizeof ((stat)) == sizeof (uint32_t)) {		\
89*2760Sdg199075 		atomic_add_32((uint32_t *)&(stat), (delta));	\
900Sstevel@tonic-gate 	_NOTE(CONSTANTCONDITION)				\
91*2760Sdg199075 	} else if (sizeof ((stat)) == sizeof (uint64_t)) {	\
92*2760Sdg199075 		atomic_add_64((uint64_t *)&(stat), (delta));	\
930Sstevel@tonic-gate 	}							\
940Sstevel@tonic-gate 	_NOTE(CONSTANTCONDITION)				\
950Sstevel@tonic-gate } while (0)
960Sstevel@tonic-gate 
97*2760Sdg199075 #define	ATOMIC_BUMP(stats, vstats, stat, delta)	do {			\
98*2760Sdg199075 	ATOMIC_BUMP_STAT((stats)->stat, (delta));			\
99*2760Sdg199075 	_NOTE(CONSTANTCONDITION)					\
100*2760Sdg199075 	if ((vstats) != NULL) {						\
101*2760Sdg199075 		ATOMIC_BUMP_STAT(((struct gld_stats *)(vstats))->stat,	\
102*2760Sdg199075 		    (delta));						\
103*2760Sdg199075 	}								\
104*2760Sdg199075 	_NOTE(CONSTANTCONDITION)					\
105*2760Sdg199075 } while (0)
106*2760Sdg199075 
107*2760Sdg199075 #define	UPDATE_STATS(stats, vstats, pktinfo, delta) {			\
108*2760Sdg199075 	if ((pktinfo).isBroadcast) {					\
109*2760Sdg199075 		ATOMIC_BUMP((stats), (vstats),				\
110*2760Sdg199075 		    glds_brdcstxmt, (delta));				\
111*2760Sdg199075 	} else if ((pktinfo).isMulticast) {				\
112*2760Sdg199075 		ATOMIC_BUMP((stats), (vstats), glds_multixmt, (delta));	\
113*2760Sdg199075 	}								\
114*2760Sdg199075 	ATOMIC_BUMP((stats), (vstats), glds_bytexmt64,			\
115*2760Sdg199075 	    ((pktinfo).pktLen));					\
116*2760Sdg199075 	ATOMIC_BUMP((stats), (vstats), glds_pktxmt64, (delta));		\
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate #ifdef GLD_DEBUG
1200Sstevel@tonic-gate int gld_debug = GLDERRS;
1210Sstevel@tonic-gate #endif
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate /* called from gld_register */
1240Sstevel@tonic-gate static int gld_initstats(gld_mac_info_t *);
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate /* called from kstat mechanism, and from wsrv's get_statistics */
1270Sstevel@tonic-gate static int gld_update_kstat(kstat_t *, int);
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate /* statistics for additional vlans */
1300Sstevel@tonic-gate static int gld_init_vlan_stats(gld_vlan_t *);
1310Sstevel@tonic-gate static int gld_update_vlan_kstat(kstat_t *, int);
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate /* called from gld_getinfo */
1340Sstevel@tonic-gate static dev_info_t *gld_finddevinfo(dev_t);
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate /* called from wput, wsrv, unidata, and v0_sched to send a packet */
1370Sstevel@tonic-gate /* also from the source routing stuff for sending RDE protocol packets */
1380Sstevel@tonic-gate static int gld_start(queue_t *, mblk_t *, int, uint32_t);
1390Sstevel@tonic-gate static int gld_start_mdt(queue_t *, mblk_t *, int);
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate /* called from gld_start[_mdt] to loopback packet(s) in promiscuous mode */
142*2760Sdg199075 static void gld_precv(gld_mac_info_t *, mblk_t *, uint32_t, struct gld_stats *);
1430Sstevel@tonic-gate static void gld_precv_mdt(gld_mac_info_t *, gld_vlan_t *, mblk_t *,
1440Sstevel@tonic-gate     pdesc_t *, pktinfo_t *);
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate /* receive group: called from gld_recv and gld_precv* with maclock held */
147*2760Sdg199075 static void gld_sendup(gld_mac_info_t *, pktinfo_t *, mblk_t *,
1480Sstevel@tonic-gate     int (*)());
1490Sstevel@tonic-gate static int gld_accept(gld_t *, pktinfo_t *);
1500Sstevel@tonic-gate static int gld_mcmatch(gld_t *, pktinfo_t *);
1510Sstevel@tonic-gate static int gld_multicast(unsigned char *, gld_t *);
1520Sstevel@tonic-gate static int gld_paccept(gld_t *, pktinfo_t *);
1530Sstevel@tonic-gate static void gld_passon(gld_t *, mblk_t *, pktinfo_t *,
1540Sstevel@tonic-gate     void (*)(queue_t *, mblk_t *));
155*2760Sdg199075 static mblk_t *gld_addudind(gld_t *, mblk_t *, pktinfo_t *, boolean_t);
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate /* wsrv group: called from wsrv, single threaded per queue */
1580Sstevel@tonic-gate static int gld_ioctl(queue_t *, mblk_t *);
1590Sstevel@tonic-gate static void gld_fastpath(gld_t *, queue_t *, mblk_t *);
1600Sstevel@tonic-gate static int gld_cmds(queue_t *, mblk_t *);
1610Sstevel@tonic-gate static mblk_t *gld_bindack(queue_t *, mblk_t *);
1620Sstevel@tonic-gate static int gld_notify_req(queue_t *, mblk_t *);
1630Sstevel@tonic-gate static int gld_udqos(queue_t *, mblk_t *);
1640Sstevel@tonic-gate static int gld_bind(queue_t *, mblk_t *);
1650Sstevel@tonic-gate static int gld_unbind(queue_t *, mblk_t *);
1660Sstevel@tonic-gate static int gld_inforeq(queue_t *, mblk_t *);
1670Sstevel@tonic-gate static int gld_unitdata(queue_t *, mblk_t *);
1680Sstevel@tonic-gate static int gldattach(queue_t *, mblk_t *);
1690Sstevel@tonic-gate static int gldunattach(queue_t *, mblk_t *);
1700Sstevel@tonic-gate static int gld_enable_multi(queue_t *, mblk_t *);
1710Sstevel@tonic-gate static int gld_disable_multi(queue_t *, mblk_t *);
1720Sstevel@tonic-gate static void gld_send_disable_multi(gld_mac_info_t *, gld_mcast_t *);
1730Sstevel@tonic-gate static int gld_promisc(queue_t *, mblk_t *, t_uscalar_t, boolean_t);
1740Sstevel@tonic-gate static int gld_physaddr(queue_t *, mblk_t *);
1750Sstevel@tonic-gate static int gld_setaddr(queue_t *, mblk_t *);
1760Sstevel@tonic-gate static int gld_get_statistics(queue_t *, mblk_t *);
1770Sstevel@tonic-gate static int gld_cap(queue_t *, mblk_t *);
1780Sstevel@tonic-gate static int gld_cap_ack(queue_t *, mblk_t *);
1790Sstevel@tonic-gate static int gld_cap_enable(queue_t *, mblk_t *);
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate /* misc utilities, some requiring various mutexes held */
1820Sstevel@tonic-gate static int gld_start_mac(gld_mac_info_t *);
1830Sstevel@tonic-gate static void gld_stop_mac(gld_mac_info_t *);
1840Sstevel@tonic-gate static void gld_set_ipq(gld_t *);
1850Sstevel@tonic-gate static void gld_flushqueue(queue_t *);
1860Sstevel@tonic-gate static glddev_t *gld_devlookup(int);
1870Sstevel@tonic-gate static int gld_findminor(glddev_t *);
1880Sstevel@tonic-gate static void gldinsque(void *, void *);
1890Sstevel@tonic-gate static void gldremque(void *);
1900Sstevel@tonic-gate void gld_bitrevcopy(caddr_t, caddr_t, size_t);
1910Sstevel@tonic-gate void gld_bitreverse(uchar_t *, size_t);
1920Sstevel@tonic-gate char *gld_macaddr_sprintf(char *, unsigned char *, int);
1930Sstevel@tonic-gate static gld_vlan_t *gld_add_vlan(gld_mac_info_t *, uint32_t vid);
1940Sstevel@tonic-gate static void gld_rem_vlan(gld_vlan_t *);
1950Sstevel@tonic-gate gld_vlan_t *gld_find_vlan(gld_mac_info_t *, uint32_t);
1960Sstevel@tonic-gate gld_vlan_t *gld_get_vlan(gld_mac_info_t *, uint32_t);
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate #ifdef GLD_DEBUG
1990Sstevel@tonic-gate static void gld_check_assertions(void);
2000Sstevel@tonic-gate extern void gld_sr_dump(gld_mac_info_t *);
2010Sstevel@tonic-gate #endif
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate /*
2040Sstevel@tonic-gate  * Allocate and zero-out "number" structures each of type "structure" in
2050Sstevel@tonic-gate  * kernel memory.
2060Sstevel@tonic-gate  */
2070Sstevel@tonic-gate #define	GETSTRUCT(structure, number)   \
2080Sstevel@tonic-gate 	(kmem_zalloc((uint_t)(sizeof (structure) * (number)), KM_NOSLEEP))
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate #define	abs(a) ((a) < 0 ? -(a) : a)
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate uint32_t gld_global_options = GLD_OPT_NO_ETHRXSNAP;
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate /*
2150Sstevel@tonic-gate  * VLANs are only supported on ethernet devices that manipulate VLAN headers
2160Sstevel@tonic-gate  * themselves.
2170Sstevel@tonic-gate  */
2180Sstevel@tonic-gate #define	VLAN_CAPABLE(macinfo) \
2190Sstevel@tonic-gate 	((macinfo)->gldm_type == DL_ETHER && \
2200Sstevel@tonic-gate 	(macinfo)->gldm_send_tagged != NULL)
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate /*
2230Sstevel@tonic-gate  * The set of notifications generatable by GLD itself, the additional
2240Sstevel@tonic-gate  * set that can be generated if the MAC driver provide the link-state
2250Sstevel@tonic-gate  * tracking callback capability, and the set supported by the GLD
2260Sstevel@tonic-gate  * notification code below.
2270Sstevel@tonic-gate  *
2280Sstevel@tonic-gate  * PLEASE keep these in sync with what the code actually does!
2290Sstevel@tonic-gate  */
2300Sstevel@tonic-gate static const uint32_t gld_internal_notes =	DL_NOTE_PROMISC_ON_PHYS |
2310Sstevel@tonic-gate 						DL_NOTE_PROMISC_OFF_PHYS |
2320Sstevel@tonic-gate 						DL_NOTE_PHYS_ADDR;
2330Sstevel@tonic-gate static const uint32_t gld_linkstate_notes =	DL_NOTE_LINK_DOWN |
2340Sstevel@tonic-gate 						DL_NOTE_LINK_UP |
2350Sstevel@tonic-gate 						DL_NOTE_SPEED;
2360Sstevel@tonic-gate static const uint32_t gld_supported_notes =	DL_NOTE_PROMISC_ON_PHYS |
2370Sstevel@tonic-gate 						DL_NOTE_PROMISC_OFF_PHYS |
2380Sstevel@tonic-gate 						DL_NOTE_PHYS_ADDR |
2390Sstevel@tonic-gate 						DL_NOTE_LINK_DOWN |
2400Sstevel@tonic-gate 						DL_NOTE_LINK_UP |
2410Sstevel@tonic-gate 						DL_NOTE_SPEED;
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate /* Media must correspond to #defines in gld.h */
2440Sstevel@tonic-gate static char *gld_media[] = {
2450Sstevel@tonic-gate 	"unknown",	/* GLDM_UNKNOWN - driver cannot determine media */
2460Sstevel@tonic-gate 	"aui",		/* GLDM_AUI */
2470Sstevel@tonic-gate 	"bnc",		/* GLDM_BNC */
2480Sstevel@tonic-gate 	"twpair",	/* GLDM_TP */
2490Sstevel@tonic-gate 	"fiber",	/* GLDM_FIBER */
2500Sstevel@tonic-gate 	"100baseT",	/* GLDM_100BT */
2510Sstevel@tonic-gate 	"100vgAnyLan",	/* GLDM_VGANYLAN */
2520Sstevel@tonic-gate 	"10baseT",	/* GLDM_10BT */
2530Sstevel@tonic-gate 	"ring4",	/* GLDM_RING4 */
2540Sstevel@tonic-gate 	"ring16",	/* GLDM_RING16 */
2550Sstevel@tonic-gate 	"PHY/MII",	/* GLDM_PHYMII */
2560Sstevel@tonic-gate 	"100baseTX",	/* GLDM_100BTX */
2570Sstevel@tonic-gate 	"100baseT4",	/* GLDM_100BT4 */
2580Sstevel@tonic-gate 	"unknown",	/* skip */
2590Sstevel@tonic-gate 	"ipib",		/* GLDM_IB */
2600Sstevel@tonic-gate };
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate /* Must correspond to #defines in gld.h */
2630Sstevel@tonic-gate static char *gld_duplex[] = {
2640Sstevel@tonic-gate 	"unknown",	/* GLD_DUPLEX_UNKNOWN - not known or not applicable */
2650Sstevel@tonic-gate 	"half",		/* GLD_DUPLEX_HALF */
2660Sstevel@tonic-gate 	"full"		/* GLD_DUPLEX_FULL */
2670Sstevel@tonic-gate };
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate /*
2700Sstevel@tonic-gate  * Interface types currently supported by GLD.
2710Sstevel@tonic-gate  * If you add new types, you must check all "XXX" strings in the GLD source
2720Sstevel@tonic-gate  * for implementation issues that may affect the support of your new type.
2730Sstevel@tonic-gate  * In particular, any type with gldm_addrlen > 6, or gldm_saplen != -2, will
2740Sstevel@tonic-gate  * require generalizing this GLD source to handle the new cases.  In other
2750Sstevel@tonic-gate  * words there are assumptions built into the code in a few places that must
2760Sstevel@tonic-gate  * be fixed.  Be sure to turn on DEBUG/ASSERT code when testing a new type.
2770Sstevel@tonic-gate  */
2780Sstevel@tonic-gate static gld_interface_t interfaces[] = {
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	/* Ethernet Bus */
2810Sstevel@tonic-gate 	{
2820Sstevel@tonic-gate 		DL_ETHER,
2830Sstevel@tonic-gate 		(uint_t)-1,
284*2760Sdg199075 		sizeof (struct ether_header),
2850Sstevel@tonic-gate 		gld_interpret_ether,
2860Sstevel@tonic-gate 		NULL,
2870Sstevel@tonic-gate 		gld_fastpath_ether,
2880Sstevel@tonic-gate 		gld_unitdata_ether,
2890Sstevel@tonic-gate 		gld_init_ether,
2900Sstevel@tonic-gate 		gld_uninit_ether,
2910Sstevel@tonic-gate 		"ether"
2920Sstevel@tonic-gate 	},
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	/* Fiber Distributed data interface */
2950Sstevel@tonic-gate 	{
2960Sstevel@tonic-gate 		DL_FDDI,
2970Sstevel@tonic-gate 		4352,
2980Sstevel@tonic-gate 		sizeof (struct fddi_mac_frm),
2990Sstevel@tonic-gate 		gld_interpret_fddi,
3000Sstevel@tonic-gate 		NULL,
3010Sstevel@tonic-gate 		gld_fastpath_fddi,
3020Sstevel@tonic-gate 		gld_unitdata_fddi,
3030Sstevel@tonic-gate 		gld_init_fddi,
3040Sstevel@tonic-gate 		gld_uninit_fddi,
3050Sstevel@tonic-gate 		"fddi"
3060Sstevel@tonic-gate 	},
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	/* Token Ring interface */
3090Sstevel@tonic-gate 	{
3100Sstevel@tonic-gate 		DL_TPR,
3110Sstevel@tonic-gate 		17914,
3120Sstevel@tonic-gate 		-1,			/* variable header size */
3130Sstevel@tonic-gate 		gld_interpret_tr,
3140Sstevel@tonic-gate 		NULL,
3150Sstevel@tonic-gate 		gld_fastpath_tr,
3160Sstevel@tonic-gate 		gld_unitdata_tr,
3170Sstevel@tonic-gate 		gld_init_tr,
3180Sstevel@tonic-gate 		gld_uninit_tr,
3190Sstevel@tonic-gate 		"tpr"
3200Sstevel@tonic-gate 	},
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	/* Infiniband */
3230Sstevel@tonic-gate 	{
3240Sstevel@tonic-gate 		DL_IB,
3250Sstevel@tonic-gate 		4092,
3260Sstevel@tonic-gate 		sizeof (struct ipoib_header),
3270Sstevel@tonic-gate 		gld_interpret_ib,
3280Sstevel@tonic-gate 		gld_interpret_mdt_ib,
3290Sstevel@tonic-gate 		gld_fastpath_ib,
3300Sstevel@tonic-gate 		gld_unitdata_ib,
3310Sstevel@tonic-gate 		gld_init_ib,
3320Sstevel@tonic-gate 		gld_uninit_ib,
3330Sstevel@tonic-gate 		"ipib"
3340Sstevel@tonic-gate 	},
3350Sstevel@tonic-gate };
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate /*
3380Sstevel@tonic-gate  * bit reversal lookup table.
3390Sstevel@tonic-gate  */
3400Sstevel@tonic-gate static	uchar_t bit_rev[] = {
3410Sstevel@tonic-gate 	0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, 0x10, 0x90, 0x50, 0xd0,
3420Sstevel@tonic-gate 	0x30, 0xb0, 0x70, 0xf0, 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
3430Sstevel@tonic-gate 	0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8, 0x04, 0x84, 0x44, 0xc4,
3440Sstevel@tonic-gate 	0x24, 0xa4, 0x64, 0xe4, 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
3450Sstevel@tonic-gate 	0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec, 0x1c, 0x9c, 0x5c, 0xdc,
3460Sstevel@tonic-gate 	0x3c, 0xbc, 0x7c, 0xfc, 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
3470Sstevel@tonic-gate 	0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2, 0x0a, 0x8a, 0x4a, 0xca,
3480Sstevel@tonic-gate 	0x2a, 0xaa, 0x6a, 0xea, 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
3490Sstevel@tonic-gate 	0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6, 0x16, 0x96, 0x56, 0xd6,
3500Sstevel@tonic-gate 	0x36, 0xb6, 0x76, 0xf6, 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
3510Sstevel@tonic-gate 	0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe, 0x01, 0x81, 0x41, 0xc1,
3520Sstevel@tonic-gate 	0x21, 0xa1, 0x61, 0xe1, 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
3530Sstevel@tonic-gate 	0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9, 0x19, 0x99, 0x59, 0xd9,
3540Sstevel@tonic-gate 	0x39, 0xb9, 0x79, 0xf9, 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
3550Sstevel@tonic-gate 	0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5, 0x0d, 0x8d, 0x4d, 0xcd,
3560Sstevel@tonic-gate 	0x2d, 0xad, 0x6d, 0xed, 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
3570Sstevel@tonic-gate 	0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3, 0x13, 0x93, 0x53, 0xd3,
3580Sstevel@tonic-gate 	0x33, 0xb3, 0x73, 0xf3, 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
3590Sstevel@tonic-gate 	0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb, 0x07, 0x87, 0x47, 0xc7,
3600Sstevel@tonic-gate 	0x27, 0xa7, 0x67, 0xe7, 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
3610Sstevel@tonic-gate 	0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, 0x1f, 0x9f, 0x5f, 0xdf,
3620Sstevel@tonic-gate 	0x3f, 0xbf, 0x7f, 0xff,
3630Sstevel@tonic-gate };
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate /*
3660Sstevel@tonic-gate  * User priorities, mapped from b_band.
3670Sstevel@tonic-gate  */
3680Sstevel@tonic-gate static uint32_t user_priority[] = {
3690Sstevel@tonic-gate 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3700Sstevel@tonic-gate 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3710Sstevel@tonic-gate 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
3720Sstevel@tonic-gate 	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
3730Sstevel@tonic-gate 	2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
3740Sstevel@tonic-gate 	2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
3750Sstevel@tonic-gate 	3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3760Sstevel@tonic-gate 	3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3770Sstevel@tonic-gate 	4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
3780Sstevel@tonic-gate 	4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
3790Sstevel@tonic-gate 	5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
3800Sstevel@tonic-gate 	5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
3810Sstevel@tonic-gate 	6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
3820Sstevel@tonic-gate 	6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
3830Sstevel@tonic-gate 	7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
3840Sstevel@tonic-gate 	7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
3850Sstevel@tonic-gate };
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate #define	UPRI(gld, band)	((band != 0) ? user_priority[(band)] : (gld)->gld_upri)
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate static struct glddevice gld_device_list;  /* Per-system root of GLD tables */
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate /*
3920Sstevel@tonic-gate  * Module linkage information for the kernel.
3930Sstevel@tonic-gate  */
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate static struct modldrv modlmisc = {
3960Sstevel@tonic-gate 	&mod_miscops,		/* Type of module - a utility provider */
3970Sstevel@tonic-gate 	"Generic LAN Driver (" GLD_VERSION_STRING ") %I%"
3980Sstevel@tonic-gate #ifdef GLD_DEBUG
3990Sstevel@tonic-gate 	" DEBUG"
4000Sstevel@tonic-gate #endif
4010Sstevel@tonic-gate };
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate static struct modlinkage modlinkage = {
4040Sstevel@tonic-gate 	MODREV_1, &modlmisc, NULL
4050Sstevel@tonic-gate };
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate int
4080Sstevel@tonic-gate _init(void)
4090Sstevel@tonic-gate {
4100Sstevel@tonic-gate 	int e;
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	/* initialize gld_device_list mutex */
4130Sstevel@tonic-gate 	mutex_init(&gld_device_list.gld_devlock, NULL, MUTEX_DRIVER, NULL);
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	/* initialize device driver (per-major) list */
4160Sstevel@tonic-gate 	gld_device_list.gld_next =
4170Sstevel@tonic-gate 	    gld_device_list.gld_prev = &gld_device_list;
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	if ((e = mod_install(&modlinkage)) != 0)
4200Sstevel@tonic-gate 		mutex_destroy(&gld_device_list.gld_devlock);
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	return (e);
4230Sstevel@tonic-gate }
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate int
4260Sstevel@tonic-gate _fini(void)
4270Sstevel@tonic-gate {
4280Sstevel@tonic-gate 	int e;
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	if ((e = mod_remove(&modlinkage)) != 0)
4310Sstevel@tonic-gate 		return (e);
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	ASSERT(gld_device_list.gld_next ==
4340Sstevel@tonic-gate 	    (glddev_t *)&gld_device_list.gld_next);
4350Sstevel@tonic-gate 	ASSERT(gld_device_list.gld_prev ==
4360Sstevel@tonic-gate 	    (glddev_t *)&gld_device_list.gld_next);
4370Sstevel@tonic-gate 	mutex_destroy(&gld_device_list.gld_devlock);
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	return (e);
4400Sstevel@tonic-gate }
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate int
4430Sstevel@tonic-gate _info(struct modinfo *modinfop)
4440Sstevel@tonic-gate {
4450Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
4460Sstevel@tonic-gate }
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate /*
4490Sstevel@tonic-gate  * GLD service routines
4500Sstevel@tonic-gate  */
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate /* So this gld binary maybe can be forward compatible with future v2 drivers */
4530Sstevel@tonic-gate #define	GLD_MAC_RESERVED (16 * sizeof (caddr_t))
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate /*ARGSUSED*/
4560Sstevel@tonic-gate gld_mac_info_t *
4570Sstevel@tonic-gate gld_mac_alloc(dev_info_t *devinfo)
4580Sstevel@tonic-gate {
4590Sstevel@tonic-gate 	gld_mac_info_t *macinfo;
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 	macinfo = kmem_zalloc(sizeof (gld_mac_info_t) + GLD_MAC_RESERVED,
4620Sstevel@tonic-gate 	    KM_SLEEP);
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	/*
4650Sstevel@tonic-gate 	 * The setting of gldm_driver_version will not be documented or allowed
4660Sstevel@tonic-gate 	 * until a future release.
4670Sstevel@tonic-gate 	 */
4680Sstevel@tonic-gate 	macinfo->gldm_driver_version = GLD_VERSION_200;
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	/*
4710Sstevel@tonic-gate 	 * GLD's version.  This also is undocumented for now, but will be
4720Sstevel@tonic-gate 	 * available if needed in the future.
4730Sstevel@tonic-gate 	 */
4740Sstevel@tonic-gate 	macinfo->gldm_GLD_version = GLD_VERSION;
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	return (macinfo);
4770Sstevel@tonic-gate }
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate /*
4800Sstevel@tonic-gate  * gld_mac_free must be called after the driver has removed interrupts
4810Sstevel@tonic-gate  * and completely stopped calling gld_recv() and gld_sched().  At that
4820Sstevel@tonic-gate  * point the interrupt routine is guaranteed by the system to have been
4830Sstevel@tonic-gate  * exited and the maclock is no longer needed.  Of course, it is
4840Sstevel@tonic-gate  * expected (required) that (assuming gld_register() succeeded),
4850Sstevel@tonic-gate  * gld_unregister() was called before gld_mac_free().
4860Sstevel@tonic-gate  */
4870Sstevel@tonic-gate void
4880Sstevel@tonic-gate gld_mac_free(gld_mac_info_t *macinfo)
4890Sstevel@tonic-gate {
4900Sstevel@tonic-gate 	ASSERT(macinfo);
4910Sstevel@tonic-gate 	ASSERT(macinfo->gldm_GLD_version == GLD_VERSION);
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	/*
4940Sstevel@tonic-gate 	 * Assert that if we made it through gld_register, then we must
4950Sstevel@tonic-gate 	 * have unregistered.
4960Sstevel@tonic-gate 	 */
4970Sstevel@tonic-gate 	ASSERT(!GLDM_LOCK_INITED(macinfo) ||
4980Sstevel@tonic-gate 	    (macinfo->gldm_GLD_flags & GLD_UNREGISTERED));
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	GLDM_LOCK_DESTROY(macinfo);
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	kmem_free(macinfo, sizeof (gld_mac_info_t) + GLD_MAC_RESERVED);
5030Sstevel@tonic-gate }
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate /*
5060Sstevel@tonic-gate  * gld_register -- called once per device instance (PPA)
5070Sstevel@tonic-gate  *
5080Sstevel@tonic-gate  * During its attach routine, a real device driver will register with GLD
5090Sstevel@tonic-gate  * so that later opens and dl_attach_reqs will work.  The arguments are the
5100Sstevel@tonic-gate  * devinfo pointer, the device name, and a macinfo structure describing the
5110Sstevel@tonic-gate  * physical device instance.
5120Sstevel@tonic-gate  */
5130Sstevel@tonic-gate int
5140Sstevel@tonic-gate gld_register(dev_info_t *devinfo, char *devname, gld_mac_info_t *macinfo)
5150Sstevel@tonic-gate {
5160Sstevel@tonic-gate 	int mediatype;
5170Sstevel@tonic-gate 	int major = ddi_name_to_major(devname), i;
5180Sstevel@tonic-gate 	glddev_t *glddev;
5190Sstevel@tonic-gate 	gld_mac_pvt_t *mac_pvt;
5200Sstevel@tonic-gate 	char minordev[32];
5210Sstevel@tonic-gate 	char pbuf[3*GLD_MAX_ADDRLEN];
5220Sstevel@tonic-gate 	gld_interface_t *ifp;
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 	ASSERT(devinfo != NULL);
5250Sstevel@tonic-gate 	ASSERT(macinfo != NULL);
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	if (macinfo->gldm_driver_version != GLD_VERSION)
5280Sstevel@tonic-gate 		return (DDI_FAILURE);
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 	mediatype = macinfo->gldm_type;
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	/*
5330Sstevel@tonic-gate 	 * Entry points should be ready for us.
5340Sstevel@tonic-gate 	 * ioctl is optional.
5350Sstevel@tonic-gate 	 * set_multicast and get_stats are optional in v0.
5360Sstevel@tonic-gate 	 * intr is only required if you add an interrupt.
5370Sstevel@tonic-gate 	 */
5380Sstevel@tonic-gate 	ASSERT(macinfo->gldm_reset != NULL);
5390Sstevel@tonic-gate 	ASSERT(macinfo->gldm_start != NULL);
5400Sstevel@tonic-gate 	ASSERT(macinfo->gldm_stop != NULL);
5410Sstevel@tonic-gate 	ASSERT(macinfo->gldm_set_mac_addr != NULL);
5420Sstevel@tonic-gate 	ASSERT(macinfo->gldm_set_promiscuous != NULL);
5430Sstevel@tonic-gate 	ASSERT(macinfo->gldm_send != NULL);
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	ASSERT(macinfo->gldm_maxpkt >= macinfo->gldm_minpkt);
5460Sstevel@tonic-gate 	ASSERT(macinfo->gldm_GLD_version == GLD_VERSION);
5470Sstevel@tonic-gate 	ASSERT(macinfo->gldm_broadcast_addr != NULL);
5480Sstevel@tonic-gate 	ASSERT(macinfo->gldm_vendor_addr != NULL);
5490Sstevel@tonic-gate 	ASSERT(macinfo->gldm_ident != NULL);
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	if (macinfo->gldm_addrlen > GLD_MAX_ADDRLEN) {
5520Sstevel@tonic-gate 		cmn_err(CE_WARN, "GLD: %s driver gldm_addrlen %d > %d not sup"
5530Sstevel@tonic-gate 		    "ported", devname, macinfo->gldm_addrlen, GLD_MAX_ADDRLEN);
5540Sstevel@tonic-gate 		return (DDI_FAILURE);
5550Sstevel@tonic-gate 	}
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	/*
5580Sstevel@tonic-gate 	 * GLD only functions properly with saplen == -2
5590Sstevel@tonic-gate 	 */
5600Sstevel@tonic-gate 	if (macinfo->gldm_saplen != -2) {
5610Sstevel@tonic-gate 		cmn_err(CE_WARN, "GLD: %s driver gldm_saplen %d != -2 "
5620Sstevel@tonic-gate 		    "not supported", devname, macinfo->gldm_saplen);
5630Sstevel@tonic-gate 		return (DDI_FAILURE);
5640Sstevel@tonic-gate 	}
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	/* see gld_rsrv() */
5670Sstevel@tonic-gate 	if (ddi_getprop(DDI_DEV_T_NONE, devinfo, 0, "fast_recv", 0))
5680Sstevel@tonic-gate 		macinfo->gldm_options |= GLDOPT_FAST_RECV;
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 	mutex_enter(&gld_device_list.gld_devlock);
5710Sstevel@tonic-gate 	glddev = gld_devlookup(major);
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	/*
5740Sstevel@tonic-gate 	 *  Allocate per-driver (major) data structure if necessary
5750Sstevel@tonic-gate 	 */
5760Sstevel@tonic-gate 	if (glddev == NULL) {
5770Sstevel@tonic-gate 		/* first occurrence of this device name (major number) */
5780Sstevel@tonic-gate 		glddev = GETSTRUCT(glddev_t, 1);
5790Sstevel@tonic-gate 		if (glddev == NULL) {
5800Sstevel@tonic-gate 			mutex_exit(&gld_device_list.gld_devlock);
5810Sstevel@tonic-gate 			return (DDI_FAILURE);
5820Sstevel@tonic-gate 		}
5830Sstevel@tonic-gate 		(void) strncpy(glddev->gld_name, devname,
5840Sstevel@tonic-gate 		    sizeof (glddev->gld_name) - 1);
5850Sstevel@tonic-gate 		glddev->gld_major = major;
5860Sstevel@tonic-gate 		glddev->gld_nextminor = GLD_MIN_CLONE_MINOR;
5870Sstevel@tonic-gate 		glddev->gld_mac_next = glddev->gld_mac_prev =
5880Sstevel@tonic-gate 			(gld_mac_info_t *)&glddev->gld_mac_next;
5890Sstevel@tonic-gate 		glddev->gld_str_next = glddev->gld_str_prev =
5900Sstevel@tonic-gate 			(gld_t *)&glddev->gld_str_next;
5910Sstevel@tonic-gate 		mutex_init(&glddev->gld_devlock, NULL, MUTEX_DRIVER, NULL);
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 		/* allow increase of number of supported multicast addrs */
5940Sstevel@tonic-gate 		glddev->gld_multisize = ddi_getprop(DDI_DEV_T_NONE,
5950Sstevel@tonic-gate 		    devinfo, 0, "multisize", GLD_MAX_MULTICAST);
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 		/*
5980Sstevel@tonic-gate 		 * Optionally restrict DLPI provider style
5990Sstevel@tonic-gate 		 *
6000Sstevel@tonic-gate 		 * -1 - don't create style 1 nodes
6010Sstevel@tonic-gate 		 * -2 - don't create style 2 nodes
6020Sstevel@tonic-gate 		 */
6030Sstevel@tonic-gate 		glddev->gld_styles = ddi_getprop(DDI_DEV_T_NONE, devinfo, 0,
6040Sstevel@tonic-gate 		    "gld-provider-styles", 0);
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 		/* Stuff that's needed before any PPA gets attached */
6070Sstevel@tonic-gate 		glddev->gld_type = macinfo->gldm_type;
6080Sstevel@tonic-gate 		glddev->gld_minsdu = macinfo->gldm_minpkt;
6090Sstevel@tonic-gate 		glddev->gld_saplen = macinfo->gldm_saplen;
6100Sstevel@tonic-gate 		glddev->gld_addrlen = macinfo->gldm_addrlen;
6110Sstevel@tonic-gate 		glddev->gld_broadcast = kmem_zalloc(macinfo->gldm_addrlen,
6120Sstevel@tonic-gate 		    KM_SLEEP);
6130Sstevel@tonic-gate 		bcopy(macinfo->gldm_broadcast_addr,
6140Sstevel@tonic-gate 		    glddev->gld_broadcast, macinfo->gldm_addrlen);
6150Sstevel@tonic-gate 		glddev->gld_maxsdu = macinfo->gldm_maxpkt;
6160Sstevel@tonic-gate 		gldinsque(glddev, gld_device_list.gld_prev);
6170Sstevel@tonic-gate 	}
6180Sstevel@tonic-gate 	glddev->gld_ndevice++;
6190Sstevel@tonic-gate 	/* Now glddev can't go away until we unregister this mac (or fail) */
6200Sstevel@tonic-gate 	mutex_exit(&gld_device_list.gld_devlock);
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	/*
6230Sstevel@tonic-gate 	 *  Per-instance initialization
6240Sstevel@tonic-gate 	 */
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 	/*
6270Sstevel@tonic-gate 	 * Initialize per-mac structure that is private to GLD.
6280Sstevel@tonic-gate 	 * Set up interface pointer. These are device class specific pointers
6290Sstevel@tonic-gate 	 * used to handle FDDI/TR/ETHER/IPoIB specific packets.
6300Sstevel@tonic-gate 	 */
6310Sstevel@tonic-gate 	for (i = 0; i < sizeof (interfaces)/sizeof (*interfaces); i++) {
6320Sstevel@tonic-gate 		if (mediatype != interfaces[i].mac_type)
6330Sstevel@tonic-gate 			continue;
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 		macinfo->gldm_mac_pvt = kmem_zalloc(sizeof (gld_mac_pvt_t),
6360Sstevel@tonic-gate 		    KM_SLEEP);
6370Sstevel@tonic-gate 		((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->interfacep = ifp =
6380Sstevel@tonic-gate 		    &interfaces[i];
6390Sstevel@tonic-gate 		break;
6400Sstevel@tonic-gate 	}
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	if (ifp == NULL) {
6430Sstevel@tonic-gate 		cmn_err(CE_WARN, "GLD: this version does not support %s driver "
6440Sstevel@tonic-gate 		    "of type %d", devname, mediatype);
6450Sstevel@tonic-gate 		goto failure;
6460Sstevel@tonic-gate 	}
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 	/*
6490Sstevel@tonic-gate 	 * Driver can only register MTU within legal media range.
6500Sstevel@tonic-gate 	 */
6510Sstevel@tonic-gate 	if (macinfo->gldm_maxpkt > ifp->mtu_size) {
6520Sstevel@tonic-gate 		cmn_err(CE_WARN, "GLD: oversize MTU is specified by driver %s",
6530Sstevel@tonic-gate 		    devname);
6540Sstevel@tonic-gate 		goto failure;
6550Sstevel@tonic-gate 	}
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	/*
6580Sstevel@tonic-gate 	 * For now, only Infiniband drivers can use MDT. Do not add
6590Sstevel@tonic-gate 	 * support for Ethernet, FDDI or TR.
6600Sstevel@tonic-gate 	 */
6610Sstevel@tonic-gate 	if (macinfo->gldm_mdt_pre != NULL) {
6620Sstevel@tonic-gate 		if (mediatype != DL_IB) {
6630Sstevel@tonic-gate 			cmn_err(CE_WARN, "GLD: MDT not supported for %s "
6640Sstevel@tonic-gate 			    "driver of type %d", devname, mediatype);
6650Sstevel@tonic-gate 			goto failure;
6660Sstevel@tonic-gate 		}
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 		/*
6690Sstevel@tonic-gate 		 * Validate entry points.
6700Sstevel@tonic-gate 		 */
6710Sstevel@tonic-gate 		if ((macinfo->gldm_mdt_send == NULL) ||
6720Sstevel@tonic-gate 		    (macinfo->gldm_mdt_post == NULL)) {
6730Sstevel@tonic-gate 			cmn_err(CE_WARN, "GLD: invalid MDT entry points for "
6740Sstevel@tonic-gate 			    "%s driver of type %d", devname, mediatype);
6750Sstevel@tonic-gate 			goto failure;
6760Sstevel@tonic-gate 		}
6770Sstevel@tonic-gate 		macinfo->gldm_options |= GLDOPT_MDT;
6780Sstevel@tonic-gate 	}
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	mac_pvt = (gld_mac_pvt_t *)macinfo->gldm_mac_pvt;
6810Sstevel@tonic-gate 	mac_pvt->major_dev = glddev;
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 	mac_pvt->curr_macaddr = kmem_zalloc(macinfo->gldm_addrlen, KM_SLEEP);
6840Sstevel@tonic-gate 	/*
6850Sstevel@tonic-gate 	 * XXX Do bit-reversed devices store gldm_vendor in canonical
6860Sstevel@tonic-gate 	 * format or in wire format?  Also gldm_broadcast.  For now
6870Sstevel@tonic-gate 	 * we are assuming canonical, but I'm not sure that makes the
6880Sstevel@tonic-gate 	 * most sense for ease of driver implementation.
6890Sstevel@tonic-gate 	 */
6900Sstevel@tonic-gate 	bcopy(macinfo->gldm_vendor_addr, mac_pvt->curr_macaddr,
6910Sstevel@tonic-gate 	    macinfo->gldm_addrlen);
6920Sstevel@tonic-gate 	mac_pvt->statistics = kmem_zalloc(sizeof (struct gld_stats), KM_SLEEP);
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 	/*
6950Sstevel@tonic-gate 	 * The available set of notifications is those generatable by GLD
6960Sstevel@tonic-gate 	 * itself, plus those corresponding to the capabilities of the MAC
6970Sstevel@tonic-gate 	 * driver, intersected with those supported by gld_notify_ind() above.
6980Sstevel@tonic-gate 	 */
6990Sstevel@tonic-gate 	mac_pvt->notifications = gld_internal_notes;
7000Sstevel@tonic-gate 	if (macinfo->gldm_capabilities & GLD_CAP_LINKSTATE)
7010Sstevel@tonic-gate 		mac_pvt->notifications |= gld_linkstate_notes;
7020Sstevel@tonic-gate 	mac_pvt->notifications &= gld_supported_notes;
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 	GLDM_LOCK_INIT(macinfo);
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	ddi_set_driver_private(devinfo, macinfo);
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 	/*
7090Sstevel@tonic-gate 	 * Now atomically get a PPA and put ourselves on the mac list.
7100Sstevel@tonic-gate 	 */
7110Sstevel@tonic-gate 	mutex_enter(&glddev->gld_devlock);
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate #ifdef DEBUG
7140Sstevel@tonic-gate 	if (macinfo->gldm_ppa != ddi_get_instance(devinfo))
7150Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d instance != ppa %d",
7160Sstevel@tonic-gate 		    ddi_driver_name(devinfo), ddi_get_instance(devinfo),
7170Sstevel@tonic-gate 		    macinfo->gldm_ppa);
7180Sstevel@tonic-gate #endif
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	/*
7210Sstevel@tonic-gate 	 * Create style 2 node (gated by gld-provider-styles property).
7220Sstevel@tonic-gate 	 *
7230Sstevel@tonic-gate 	 * NOTE: When the CLONE_DEV flag is specified to
7240Sstevel@tonic-gate 	 *	 ddi_create_minor_node() the minor number argument is
7250Sstevel@tonic-gate 	 *	 immaterial. Opens of that node will go via the clone
7260Sstevel@tonic-gate 	 *	 driver and gld_open() will always be passed a dev_t with
7270Sstevel@tonic-gate 	 *	 minor of zero.
7280Sstevel@tonic-gate 	 */
7290Sstevel@tonic-gate 	if (glddev->gld_styles != -2) {
7300Sstevel@tonic-gate 		if (ddi_create_minor_node(devinfo, glddev->gld_name, S_IFCHR,
7310Sstevel@tonic-gate 		    0, DDI_NT_NET, CLONE_DEV) == DDI_FAILURE) {
7320Sstevel@tonic-gate 			mutex_exit(&glddev->gld_devlock);
7330Sstevel@tonic-gate 			goto late_failure;
7340Sstevel@tonic-gate 		}
7350Sstevel@tonic-gate 	}
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 	/*
7380Sstevel@tonic-gate 	 * Create style 1 node (gated by gld-provider-styles property)
7390Sstevel@tonic-gate 	 */
7400Sstevel@tonic-gate 	if (glddev->gld_styles != -1) {
7410Sstevel@tonic-gate 		(void) sprintf(minordev, "%s%d", glddev->gld_name,
7420Sstevel@tonic-gate 		    macinfo->gldm_ppa);
7430Sstevel@tonic-gate 		if (ddi_create_minor_node(devinfo, minordev, S_IFCHR,
7440Sstevel@tonic-gate 		    GLD_STYLE1_PPA_TO_MINOR(macinfo->gldm_ppa), DDI_NT_NET,
7450Sstevel@tonic-gate 		    0) != DDI_SUCCESS) {
7460Sstevel@tonic-gate 			mutex_exit(&glddev->gld_devlock);
7470Sstevel@tonic-gate 			goto late_failure;
7480Sstevel@tonic-gate 		}
7490Sstevel@tonic-gate 	}
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	/* add ourselves to this major device's linked list of instances */
7520Sstevel@tonic-gate 	gldinsque(macinfo, glddev->gld_mac_prev);
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 	mutex_exit(&glddev->gld_devlock);
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate 	/*
7570Sstevel@tonic-gate 	 * Unfortunately we need the ppa before we call gld_initstats();
7580Sstevel@tonic-gate 	 * otherwise we would like to do this just above the mutex_enter
7590Sstevel@tonic-gate 	 * above.  In which case we could have set MAC_READY inside the
7600Sstevel@tonic-gate 	 * mutex and we wouldn't have needed to check it in open and
7610Sstevel@tonic-gate 	 * DL_ATTACH.  We wouldn't like to do the initstats/kstat_create
7620Sstevel@tonic-gate 	 * inside the mutex because it might get taken in our kstat_update
7630Sstevel@tonic-gate 	 * routine and cause a deadlock with kstat_chain_lock.
7640Sstevel@tonic-gate 	 */
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 	/* gld_initstats() calls (*ifp->init)() */
7670Sstevel@tonic-gate 	if (gld_initstats(macinfo) != GLD_SUCCESS) {
7680Sstevel@tonic-gate 		mutex_enter(&glddev->gld_devlock);
7690Sstevel@tonic-gate 		gldremque(macinfo);
7700Sstevel@tonic-gate 		mutex_exit(&glddev->gld_devlock);
7710Sstevel@tonic-gate 		goto late_failure;
7720Sstevel@tonic-gate 	}
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 	/*
7750Sstevel@tonic-gate 	 * Need to indicate we are NOW ready to process interrupts;
7760Sstevel@tonic-gate 	 * any interrupt before this is set is for someone else.
7770Sstevel@tonic-gate 	 * This flag is also now used to tell open, et. al. that this
7780Sstevel@tonic-gate 	 * mac is now fully ready and available for use.
7790Sstevel@tonic-gate 	 */
7800Sstevel@tonic-gate 	GLDM_LOCK(macinfo, RW_WRITER);
7810Sstevel@tonic-gate 	macinfo->gldm_GLD_flags |= GLD_MAC_READY;
7820Sstevel@tonic-gate 	GLDM_UNLOCK(macinfo);
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 	/* log local ethernet address -- XXX not DDI compliant */
7850Sstevel@tonic-gate 	if (macinfo->gldm_addrlen == sizeof (struct ether_addr))
7860Sstevel@tonic-gate 		(void) localetheraddr(
7870Sstevel@tonic-gate 		    (struct ether_addr *)macinfo->gldm_vendor_addr, NULL);
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 	/* now put announcement into the message buffer */
7900Sstevel@tonic-gate 	cmn_err(CE_CONT, "!%s%d: %s: type \"%s\" mac address %s\n",
7910Sstevel@tonic-gate 	    glddev->gld_name,
7920Sstevel@tonic-gate 	    macinfo->gldm_ppa, macinfo->gldm_ident,
7930Sstevel@tonic-gate 	    mac_pvt->interfacep->mac_string,
7940Sstevel@tonic-gate 	    gld_macaddr_sprintf(pbuf, macinfo->gldm_vendor_addr,
7950Sstevel@tonic-gate 	    macinfo->gldm_addrlen));
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 	ddi_report_dev(devinfo);
7980Sstevel@tonic-gate 	return (DDI_SUCCESS);
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate late_failure:
8010Sstevel@tonic-gate 	ddi_remove_minor_node(devinfo, NULL);
8020Sstevel@tonic-gate 	GLDM_LOCK_DESTROY(macinfo);
8030Sstevel@tonic-gate 	if (mac_pvt->curr_macaddr != NULL)
8040Sstevel@tonic-gate 	    kmem_free(mac_pvt->curr_macaddr, macinfo->gldm_addrlen);
8050Sstevel@tonic-gate 	if (mac_pvt->statistics != NULL)
8060Sstevel@tonic-gate 	    kmem_free(mac_pvt->statistics, sizeof (struct gld_stats));
8070Sstevel@tonic-gate 	kmem_free(macinfo->gldm_mac_pvt, sizeof (gld_mac_pvt_t));
8080Sstevel@tonic-gate 	macinfo->gldm_mac_pvt = NULL;
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate failure:
8110Sstevel@tonic-gate 	mutex_enter(&gld_device_list.gld_devlock);
8120Sstevel@tonic-gate 	glddev->gld_ndevice--;
8130Sstevel@tonic-gate 	/*
8140Sstevel@tonic-gate 	 * Note that just because this goes to zero here does not necessarily
8150Sstevel@tonic-gate 	 * mean that we were the one who added the glddev above.  It's
8160Sstevel@tonic-gate 	 * possible that the first mac unattached while were were in here
8170Sstevel@tonic-gate 	 * failing to attach the second mac.  But we're now the last.
8180Sstevel@tonic-gate 	 */
8190Sstevel@tonic-gate 	if (glddev->gld_ndevice == 0) {
8200Sstevel@tonic-gate 		/* There should be no macinfos left */
8210Sstevel@tonic-gate 		ASSERT(glddev->gld_mac_next ==
8220Sstevel@tonic-gate 		    (gld_mac_info_t *)&glddev->gld_mac_next);
8230Sstevel@tonic-gate 		ASSERT(glddev->gld_mac_prev ==
8240Sstevel@tonic-gate 		    (gld_mac_info_t *)&glddev->gld_mac_next);
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 		/*
8270Sstevel@tonic-gate 		 * There should be no DL_UNATTACHED streams: the system
8280Sstevel@tonic-gate 		 * should not have detached the "first" devinfo which has
8290Sstevel@tonic-gate 		 * all the open style 2 streams.
8300Sstevel@tonic-gate 		 *
8310Sstevel@tonic-gate 		 * XXX This is not clear.  See gld_getinfo and Bug 1165519
8320Sstevel@tonic-gate 		 */
8330Sstevel@tonic-gate 		ASSERT(glddev->gld_str_next == (gld_t *)&glddev->gld_str_next);
8340Sstevel@tonic-gate 		ASSERT(glddev->gld_str_prev == (gld_t *)&glddev->gld_str_next);
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 		gldremque(glddev);
8370Sstevel@tonic-gate 		mutex_destroy(&glddev->gld_devlock);
8380Sstevel@tonic-gate 		if (glddev->gld_broadcast != NULL)
8390Sstevel@tonic-gate 			kmem_free(glddev->gld_broadcast, glddev->gld_addrlen);
8400Sstevel@tonic-gate 		kmem_free(glddev, sizeof (glddev_t));
8410Sstevel@tonic-gate 	}
8420Sstevel@tonic-gate 	mutex_exit(&gld_device_list.gld_devlock);
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 	return (DDI_FAILURE);
8450Sstevel@tonic-gate }
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate /*
8480Sstevel@tonic-gate  * gld_unregister (macinfo)
8490Sstevel@tonic-gate  * remove the macinfo structure from local structures
8500Sstevel@tonic-gate  * this is cleanup for a driver to be unloaded
8510Sstevel@tonic-gate  */
8520Sstevel@tonic-gate int
8530Sstevel@tonic-gate gld_unregister(gld_mac_info_t *macinfo)
8540Sstevel@tonic-gate {
8550Sstevel@tonic-gate 	gld_mac_pvt_t *mac_pvt = (gld_mac_pvt_t *)macinfo->gldm_mac_pvt;
8560Sstevel@tonic-gate 	glddev_t *glddev = mac_pvt->major_dev;
8570Sstevel@tonic-gate 	gld_interface_t *ifp;
8580Sstevel@tonic-gate 	int multisize = sizeof (gld_mcast_t) * glddev->gld_multisize;
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 	mutex_enter(&glddev->gld_devlock);
8610Sstevel@tonic-gate 	GLDM_LOCK(macinfo, RW_WRITER);
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 	if (mac_pvt->nvlan > 0) {
8640Sstevel@tonic-gate 		GLDM_UNLOCK(macinfo);
8650Sstevel@tonic-gate 		mutex_exit(&glddev->gld_devlock);
8660Sstevel@tonic-gate 		return (DDI_FAILURE);
8670Sstevel@tonic-gate 	}
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate #ifdef	GLD_DEBUG
8700Sstevel@tonic-gate 	{
8710Sstevel@tonic-gate 		int i;
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 		for (i = 0; i < VLAN_HASHSZ; i++) {
8740Sstevel@tonic-gate 			if ((mac_pvt->vlan_hash[i] != NULL))
8750Sstevel@tonic-gate 				cmn_err(CE_PANIC,
8760Sstevel@tonic-gate 				    "%s, line %d: "
8770Sstevel@tonic-gate 				    "mac_pvt->vlan_hash[%d] != NULL",
8780Sstevel@tonic-gate 				    __FILE__, __LINE__, i);
8790Sstevel@tonic-gate 		}
8800Sstevel@tonic-gate 	}
8810Sstevel@tonic-gate #endif
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	/* Delete this mac */
8840Sstevel@tonic-gate 	gldremque(macinfo);
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 	/* Disallow further entries to gld_recv() and gld_sched() */
8870Sstevel@tonic-gate 	macinfo->gldm_GLD_flags |= GLD_UNREGISTERED;
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 	GLDM_UNLOCK(macinfo);
8900Sstevel@tonic-gate 	mutex_exit(&glddev->gld_devlock);
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 	ifp = ((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->interfacep;
8930Sstevel@tonic-gate 	(*ifp->uninit)(macinfo);
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate 	ASSERT(mac_pvt->kstatp);
8960Sstevel@tonic-gate 	kstat_delete(mac_pvt->kstatp);
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 	ASSERT(GLDM_LOCK_INITED(macinfo));
8990Sstevel@tonic-gate 	kmem_free(mac_pvt->curr_macaddr, macinfo->gldm_addrlen);
9000Sstevel@tonic-gate 	kmem_free(mac_pvt->statistics, sizeof (struct gld_stats));
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 	if (mac_pvt->mcast_table != NULL)
9030Sstevel@tonic-gate 		kmem_free(mac_pvt->mcast_table, multisize);
9040Sstevel@tonic-gate 	kmem_free(macinfo->gldm_mac_pvt, sizeof (gld_mac_pvt_t));
9050Sstevel@tonic-gate 	macinfo->gldm_mac_pvt = (caddr_t)NULL;
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 	/* We now have one fewer instance for this major device */
9080Sstevel@tonic-gate 	mutex_enter(&gld_device_list.gld_devlock);
9090Sstevel@tonic-gate 	glddev->gld_ndevice--;
9100Sstevel@tonic-gate 	if (glddev->gld_ndevice == 0) {
9110Sstevel@tonic-gate 		/* There should be no macinfos left */
9120Sstevel@tonic-gate 		ASSERT(glddev->gld_mac_next ==
9130Sstevel@tonic-gate 		    (gld_mac_info_t *)&glddev->gld_mac_next);
9140Sstevel@tonic-gate 		ASSERT(glddev->gld_mac_prev ==
9150Sstevel@tonic-gate 		    (gld_mac_info_t *)&glddev->gld_mac_next);
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 		/*
9180Sstevel@tonic-gate 		 * There should be no DL_UNATTACHED streams: the system
9190Sstevel@tonic-gate 		 * should not have detached the "first" devinfo which has
9200Sstevel@tonic-gate 		 * all the open style 2 streams.
9210Sstevel@tonic-gate 		 *
9220Sstevel@tonic-gate 		 * XXX This is not clear.  See gld_getinfo and Bug 1165519
9230Sstevel@tonic-gate 		 */
9240Sstevel@tonic-gate 		ASSERT(glddev->gld_str_next == (gld_t *)&glddev->gld_str_next);
9250Sstevel@tonic-gate 		ASSERT(glddev->gld_str_prev == (gld_t *)&glddev->gld_str_next);
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate 		ddi_remove_minor_node(macinfo->gldm_devinfo, NULL);
9280Sstevel@tonic-gate 		gldremque(glddev);
9290Sstevel@tonic-gate 		mutex_destroy(&glddev->gld_devlock);
9300Sstevel@tonic-gate 		if (glddev->gld_broadcast != NULL)
9310Sstevel@tonic-gate 			kmem_free(glddev->gld_broadcast, glddev->gld_addrlen);
9320Sstevel@tonic-gate 		kmem_free(glddev, sizeof (glddev_t));
9330Sstevel@tonic-gate 	}
9340Sstevel@tonic-gate 	mutex_exit(&gld_device_list.gld_devlock);
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate 	return (DDI_SUCCESS);
9370Sstevel@tonic-gate }
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate /*
9400Sstevel@tonic-gate  * gld_initstats
9410Sstevel@tonic-gate  * called from gld_register
9420Sstevel@tonic-gate  */
9430Sstevel@tonic-gate static int
9440Sstevel@tonic-gate gld_initstats(gld_mac_info_t *macinfo)
9450Sstevel@tonic-gate {
9460Sstevel@tonic-gate 	gld_mac_pvt_t *mac_pvt = (gld_mac_pvt_t *)macinfo->gldm_mac_pvt;
9470Sstevel@tonic-gate 	struct gldkstats *sp;
9480Sstevel@tonic-gate 	glddev_t *glddev;
9490Sstevel@tonic-gate 	kstat_t *ksp;
9500Sstevel@tonic-gate 	gld_interface_t *ifp;
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 	glddev = mac_pvt->major_dev;
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 	if ((ksp = kstat_create(glddev->gld_name, macinfo->gldm_ppa,
9550Sstevel@tonic-gate 	    NULL, "net", KSTAT_TYPE_NAMED,
9560Sstevel@tonic-gate 	    sizeof (struct gldkstats) / sizeof (kstat_named_t), 0)) == NULL) {
9570Sstevel@tonic-gate 		cmn_err(CE_WARN,
9580Sstevel@tonic-gate 		    "GLD: failed to create kstat structure for %s%d",
9590Sstevel@tonic-gate 		    glddev->gld_name, macinfo->gldm_ppa);
9600Sstevel@tonic-gate 		return (GLD_FAILURE);
9610Sstevel@tonic-gate 	}
9620Sstevel@tonic-gate 	mac_pvt->kstatp = ksp;
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 	ksp->ks_update = gld_update_kstat;
9650Sstevel@tonic-gate 	ksp->ks_private = (void *)macinfo;
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 	sp = ksp->ks_data;
9680Sstevel@tonic-gate 	kstat_named_init(&sp->glds_pktrcv, "ipackets", KSTAT_DATA_UINT32);
9690Sstevel@tonic-gate 	kstat_named_init(&sp->glds_pktxmt, "opackets", KSTAT_DATA_UINT32);
9700Sstevel@tonic-gate 	kstat_named_init(&sp->glds_errrcv, "ierrors", KSTAT_DATA_ULONG);
9710Sstevel@tonic-gate 	kstat_named_init(&sp->glds_errxmt, "oerrors", KSTAT_DATA_ULONG);
9720Sstevel@tonic-gate 	kstat_named_init(&sp->glds_bytexmt, "obytes", KSTAT_DATA_UINT32);
9730Sstevel@tonic-gate 	kstat_named_init(&sp->glds_bytercv, "rbytes", KSTAT_DATA_UINT32);
9740Sstevel@tonic-gate 	kstat_named_init(&sp->glds_multixmt, "multixmt", KSTAT_DATA_ULONG);
9750Sstevel@tonic-gate 	kstat_named_init(&sp->glds_multircv, "multircv", KSTAT_DATA_ULONG);
9760Sstevel@tonic-gate 	kstat_named_init(&sp->glds_brdcstxmt, "brdcstxmt", KSTAT_DATA_ULONG);
9770Sstevel@tonic-gate 	kstat_named_init(&sp->glds_brdcstrcv, "brdcstrcv", KSTAT_DATA_ULONG);
9780Sstevel@tonic-gate 	kstat_named_init(&sp->glds_blocked, "blocked", KSTAT_DATA_ULONG);
9790Sstevel@tonic-gate 	kstat_named_init(&sp->glds_noxmtbuf, "noxmtbuf", KSTAT_DATA_ULONG);
9800Sstevel@tonic-gate 	kstat_named_init(&sp->glds_norcvbuf, "norcvbuf", KSTAT_DATA_ULONG);
9810Sstevel@tonic-gate 	kstat_named_init(&sp->glds_xmtretry, "xmtretry", KSTAT_DATA_ULONG);
9820Sstevel@tonic-gate 	kstat_named_init(&sp->glds_intr, "intr", KSTAT_DATA_ULONG);
9830Sstevel@tonic-gate 	kstat_named_init(&sp->glds_pktrcv64, "ipackets64", KSTAT_DATA_UINT64);
9840Sstevel@tonic-gate 	kstat_named_init(&sp->glds_pktxmt64, "opackets64", KSTAT_DATA_UINT64);
9850Sstevel@tonic-gate 	kstat_named_init(&sp->glds_bytexmt64, "obytes64", KSTAT_DATA_UINT64);
9860Sstevel@tonic-gate 	kstat_named_init(&sp->glds_bytercv64, "rbytes64", KSTAT_DATA_UINT64);
9870Sstevel@tonic-gate 	kstat_named_init(&sp->glds_unknowns, "unknowns", KSTAT_DATA_ULONG);
9880Sstevel@tonic-gate 	kstat_named_init(&sp->glds_speed, "ifspeed", KSTAT_DATA_UINT64);
9890Sstevel@tonic-gate 	kstat_named_init(&sp->glds_media, "media", KSTAT_DATA_CHAR);
9900Sstevel@tonic-gate 	kstat_named_init(&sp->glds_prom, "promisc", KSTAT_DATA_CHAR);
9910Sstevel@tonic-gate 
9920Sstevel@tonic-gate 	kstat_named_init(&sp->glds_overflow, "oflo", KSTAT_DATA_ULONG);
9930Sstevel@tonic-gate 	kstat_named_init(&sp->glds_underflow, "uflo", KSTAT_DATA_ULONG);
9940Sstevel@tonic-gate 	kstat_named_init(&sp->glds_missed, "missed", KSTAT_DATA_ULONG);
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 	kstat_named_init(&sp->glds_xmtbadinterp, "xmt_badinterp",
9970Sstevel@tonic-gate 	    KSTAT_DATA_UINT32);
9980Sstevel@tonic-gate 	kstat_named_init(&sp->glds_rcvbadinterp, "rcv_badinterp",
9990Sstevel@tonic-gate 	    KSTAT_DATA_UINT32);
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate 	ifp = ((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->interfacep;
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 	(*ifp->init)(macinfo);
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 	kstat_install(ksp);
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate 	return (GLD_SUCCESS);
10080Sstevel@tonic-gate }
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate /* called from kstat mechanism, and from wsrv's get_statistics_req */
10110Sstevel@tonic-gate static int
10120Sstevel@tonic-gate gld_update_kstat(kstat_t *ksp, int rw)
10130Sstevel@tonic-gate {
10140Sstevel@tonic-gate 	gld_mac_info_t	*macinfo;
10150Sstevel@tonic-gate 	gld_mac_pvt_t	*mac_pvt;
10160Sstevel@tonic-gate 	struct gldkstats *gsp;
10170Sstevel@tonic-gate 	struct gld_stats *stats;
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
10200Sstevel@tonic-gate 		return (EACCES);
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	macinfo = (gld_mac_info_t *)ksp->ks_private;
10230Sstevel@tonic-gate 	ASSERT(macinfo != NULL);
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 	GLDM_LOCK(macinfo, RW_WRITER);
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate 	if (!(macinfo->gldm_GLD_flags & GLD_MAC_READY)) {
10280Sstevel@tonic-gate 		GLDM_UNLOCK(macinfo);
10290Sstevel@tonic-gate 		return (EIO);	/* this one's not ready yet */
10300Sstevel@tonic-gate 	}
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 	if (macinfo->gldm_GLD_flags & GLD_UNREGISTERED) {
10330Sstevel@tonic-gate 		GLDM_UNLOCK(macinfo);
10340Sstevel@tonic-gate 		return (EIO);	/* this one's not ready any more */
10350Sstevel@tonic-gate 	}
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 	mac_pvt = (gld_mac_pvt_t *)macinfo->gldm_mac_pvt;
10380Sstevel@tonic-gate 	gsp = mac_pvt->kstatp->ks_data;
10390Sstevel@tonic-gate 	ASSERT(gsp);
10400Sstevel@tonic-gate 	stats = mac_pvt->statistics;
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate 	if (macinfo->gldm_get_stats)
10430Sstevel@tonic-gate 		(void) (*macinfo->gldm_get_stats)(macinfo, stats);
10440Sstevel@tonic-gate 
10450Sstevel@tonic-gate 	gsp->glds_pktxmt.value.ui32 = stats->glds_pktxmt64 & 0xffffffff;
10460Sstevel@tonic-gate 	gsp->glds_bytexmt.value.ui32 = stats->glds_bytexmt64 & 0xffffffff;
10470Sstevel@tonic-gate 	gsp->glds_multixmt.value.ul = stats->glds_multixmt;
10480Sstevel@tonic-gate 	gsp->glds_brdcstxmt.value.ul = stats->glds_brdcstxmt;
10490Sstevel@tonic-gate 	gsp->glds_noxmtbuf.value.ul = stats->glds_noxmtbuf;	/* 0 for now */
10500Sstevel@tonic-gate 	gsp->glds_xmtretry.value.ul = stats->glds_xmtretry;
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	gsp->glds_pktxmt64.value.ui64 = stats->glds_pktxmt64;
10530Sstevel@tonic-gate 	gsp->glds_bytexmt64.value.ui64 = stats->glds_bytexmt64;
10540Sstevel@tonic-gate 	gsp->glds_xmtbadinterp.value.ui32 = stats->glds_xmtbadinterp;
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate 	gsp->glds_pktrcv.value.ui32 = stats->glds_pktrcv64 & 0xffffffff;
10570Sstevel@tonic-gate 	gsp->glds_errxmt.value.ul = stats->glds_errxmt;
10580Sstevel@tonic-gate 	gsp->glds_errrcv.value.ul = stats->glds_errrcv;
10590Sstevel@tonic-gate 	gsp->glds_bytercv.value.ui32 = stats->glds_bytercv64 & 0xffffffff;
10600Sstevel@tonic-gate 	gsp->glds_multircv.value.ul = stats->glds_multircv;
10610Sstevel@tonic-gate 	gsp->glds_brdcstrcv.value.ul = stats->glds_brdcstrcv;
10620Sstevel@tonic-gate 	gsp->glds_blocked.value.ul = stats->glds_blocked;
10630Sstevel@tonic-gate 	gsp->glds_overflow.value.ul = stats->glds_overflow;
10640Sstevel@tonic-gate 	gsp->glds_underflow.value.ul = stats->glds_underflow;
10650Sstevel@tonic-gate 	gsp->glds_missed.value.ul = stats->glds_missed;
10660Sstevel@tonic-gate 	gsp->glds_norcvbuf.value.ul = stats->glds_norcvbuf +
10670Sstevel@tonic-gate 	    stats->glds_gldnorcvbuf;
10680Sstevel@tonic-gate 	gsp->glds_intr.value.ul = stats->glds_intr;
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate 	gsp->glds_speed.value.ui64 = stats->glds_speed;
10710Sstevel@tonic-gate 	gsp->glds_unknowns.value.ul = stats->glds_unknowns;
10720Sstevel@tonic-gate 	gsp->glds_pktrcv64.value.ui64 = stats->glds_pktrcv64;
10730Sstevel@tonic-gate 	gsp->glds_bytercv64.value.ui64 = stats->glds_bytercv64;
10740Sstevel@tonic-gate 	gsp->glds_rcvbadinterp.value.ui32 = stats->glds_rcvbadinterp;
10750Sstevel@tonic-gate 
10760Sstevel@tonic-gate 	if (mac_pvt->nprom)
10770Sstevel@tonic-gate 		(void) strcpy(gsp->glds_prom.value.c, "phys");
10780Sstevel@tonic-gate 	else if (mac_pvt->nprom_multi)
10790Sstevel@tonic-gate 		(void) strcpy(gsp->glds_prom.value.c, "multi");
10800Sstevel@tonic-gate 	else
10810Sstevel@tonic-gate 		(void) strcpy(gsp->glds_prom.value.c, "off");
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 	(void) strcpy(gsp->glds_media.value.c, gld_media[
10840Sstevel@tonic-gate 	    stats->glds_media < sizeof (gld_media) / sizeof (gld_media[0])
10850Sstevel@tonic-gate 	    ? stats->glds_media : 0]);
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate 	switch (macinfo->gldm_type) {
10880Sstevel@tonic-gate 	case DL_ETHER:
10890Sstevel@tonic-gate 		gsp->glds_frame.value.ul = stats->glds_frame;
10900Sstevel@tonic-gate 		gsp->glds_crc.value.ul = stats->glds_crc;
10910Sstevel@tonic-gate 		gsp->glds_collisions.value.ul = stats->glds_collisions;
10920Sstevel@tonic-gate 		gsp->glds_excoll.value.ul = stats->glds_excoll;
10930Sstevel@tonic-gate 		gsp->glds_defer.value.ul = stats->glds_defer;
10940Sstevel@tonic-gate 		gsp->glds_short.value.ul = stats->glds_short;
10950Sstevel@tonic-gate 		gsp->glds_xmtlatecoll.value.ul = stats->glds_xmtlatecoll;
10960Sstevel@tonic-gate 		gsp->glds_nocarrier.value.ul = stats->glds_nocarrier;
10970Sstevel@tonic-gate 		gsp->glds_dot3_first_coll.value.ui32 =
10980Sstevel@tonic-gate 		    stats->glds_dot3_first_coll;
10990Sstevel@tonic-gate 		gsp->glds_dot3_multi_coll.value.ui32 =
11000Sstevel@tonic-gate 		    stats->glds_dot3_multi_coll;
11010Sstevel@tonic-gate 		gsp->glds_dot3_sqe_error.value.ui32 =
11020Sstevel@tonic-gate 		    stats->glds_dot3_sqe_error;
11030Sstevel@tonic-gate 		gsp->glds_dot3_mac_xmt_error.value.ui32 =
11040Sstevel@tonic-gate 		    stats->glds_dot3_mac_xmt_error;
11050Sstevel@tonic-gate 		gsp->glds_dot3_mac_rcv_error.value.ui32 =
11060Sstevel@tonic-gate 		    stats->glds_dot3_mac_rcv_error;
11070Sstevel@tonic-gate 		gsp->glds_dot3_frame_too_long.value.ui32 =
11080Sstevel@tonic-gate 		    stats->glds_dot3_frame_too_long;
11090Sstevel@tonic-gate 		(void) strcpy(gsp->glds_duplex.value.c, gld_duplex[
11100Sstevel@tonic-gate 		    stats->glds_duplex <
11110Sstevel@tonic-gate 		    sizeof (gld_duplex) / sizeof (gld_duplex[0]) ?
11120Sstevel@tonic-gate 		    stats->glds_duplex : 0]);
11130Sstevel@tonic-gate 		break;
11140Sstevel@tonic-gate 	case DL_TPR:
11150Sstevel@tonic-gate 		gsp->glds_dot5_line_error.value.ui32 =
11160Sstevel@tonic-gate 		    stats->glds_dot5_line_error;
11170Sstevel@tonic-gate 		gsp->glds_dot5_burst_error.value.ui32 =
11180Sstevel@tonic-gate 		    stats->glds_dot5_burst_error;
11190Sstevel@tonic-gate 		gsp->glds_dot5_signal_loss.value.ui32 =
11200Sstevel@tonic-gate 		    stats->glds_dot5_signal_loss;
11210Sstevel@tonic-gate 		gsp->glds_dot5_ace_error.value.ui32 =
11220Sstevel@tonic-gate 		    stats->glds_dot5_ace_error;
11230Sstevel@tonic-gate 		gsp->glds_dot5_internal_error.value.ui32 =
11240Sstevel@tonic-gate 		    stats->glds_dot5_internal_error;
11250Sstevel@tonic-gate 		gsp->glds_dot5_lost_frame_error.value.ui32 =
11260Sstevel@tonic-gate 		    stats->glds_dot5_lost_frame_error;
11270Sstevel@tonic-gate 		gsp->glds_dot5_frame_copied_error.value.ui32 =
11280Sstevel@tonic-gate 		    stats->glds_dot5_frame_copied_error;
11290Sstevel@tonic-gate 		gsp->glds_dot5_token_error.value.ui32 =
11300Sstevel@tonic-gate 		    stats->glds_dot5_token_error;
11310Sstevel@tonic-gate 		gsp->glds_dot5_freq_error.value.ui32 =
11320Sstevel@tonic-gate 		    stats->glds_dot5_freq_error;
11330Sstevel@tonic-gate 		break;
11340Sstevel@tonic-gate 	case DL_FDDI:
11350Sstevel@tonic-gate 		gsp->glds_fddi_mac_error.value.ui32 =
11360Sstevel@tonic-gate 		    stats->glds_fddi_mac_error;
11370Sstevel@tonic-gate 		gsp->glds_fddi_mac_lost.value.ui32 =
11380Sstevel@tonic-gate 		    stats->glds_fddi_mac_lost;
11390Sstevel@tonic-gate 		gsp->glds_fddi_mac_token.value.ui32 =
11400Sstevel@tonic-gate 		    stats->glds_fddi_mac_token;
11410Sstevel@tonic-gate 		gsp->glds_fddi_mac_tvx_expired.value.ui32 =
11420Sstevel@tonic-gate 		    stats->glds_fddi_mac_tvx_expired;
11430Sstevel@tonic-gate 		gsp->glds_fddi_mac_late.value.ui32 =
11440Sstevel@tonic-gate 		    stats->glds_fddi_mac_late;
11450Sstevel@tonic-gate 		gsp->glds_fddi_mac_ring_op.value.ui32 =
11460Sstevel@tonic-gate 		    stats->glds_fddi_mac_ring_op;
11470Sstevel@tonic-gate 		break;
11480Sstevel@tonic-gate 	case DL_IB:
11490Sstevel@tonic-gate 		break;
11500Sstevel@tonic-gate 	default:
11510Sstevel@tonic-gate 		break;
11520Sstevel@tonic-gate 	}
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 	GLDM_UNLOCK(macinfo);
11550Sstevel@tonic-gate 
11560Sstevel@tonic-gate #ifdef GLD_DEBUG
11570Sstevel@tonic-gate 	gld_check_assertions();
11580Sstevel@tonic-gate 	if (gld_debug & GLDRDE)
11590Sstevel@tonic-gate 		gld_sr_dump(macinfo);
11600Sstevel@tonic-gate #endif
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate 	return (0);
11630Sstevel@tonic-gate }
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate static int
11660Sstevel@tonic-gate gld_init_vlan_stats(gld_vlan_t *vlan)
11670Sstevel@tonic-gate {
11680Sstevel@tonic-gate 	gld_mac_info_t *mac = vlan->gldv_mac;
11690Sstevel@tonic-gate 	gld_mac_pvt_t *mac_pvt = (gld_mac_pvt_t *)mac->gldm_mac_pvt;
11700Sstevel@tonic-gate 	struct gldkstats *sp;
11710Sstevel@tonic-gate 	glddev_t *glddev;
11720Sstevel@tonic-gate 	kstat_t *ksp;
11730Sstevel@tonic-gate 	char *name;
11740Sstevel@tonic-gate 	int instance;
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate 	glddev = mac_pvt->major_dev;
11770Sstevel@tonic-gate 	name = glddev->gld_name;
11780Sstevel@tonic-gate 	instance = (vlan->gldv_id * GLD_VLAN_SCALE) + mac->gldm_ppa;
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate 	if ((ksp = kstat_create(name, instance,
11810Sstevel@tonic-gate 	    NULL, "net", KSTAT_TYPE_NAMED,
11820Sstevel@tonic-gate 	    sizeof (struct gldkstats) / sizeof (kstat_named_t), 0)) == NULL) {
11830Sstevel@tonic-gate 		cmn_err(CE_WARN,
11840Sstevel@tonic-gate 		    "GLD: failed to create kstat structure for %s%d",
11850Sstevel@tonic-gate 		    name, instance);
11860Sstevel@tonic-gate 		return (GLD_FAILURE);
11870Sstevel@tonic-gate 	}
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate 	vlan->gldv_kstatp = ksp;
11900Sstevel@tonic-gate 
11910Sstevel@tonic-gate 	ksp->ks_update = gld_update_vlan_kstat;
11920Sstevel@tonic-gate 	ksp->ks_private = (void *)vlan;
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate 	sp = ksp->ks_data;
11950Sstevel@tonic-gate 	kstat_named_init(&sp->glds_pktrcv, "ipackets", KSTAT_DATA_UINT32);
11960Sstevel@tonic-gate 	kstat_named_init(&sp->glds_pktxmt, "opackets", KSTAT_DATA_UINT32);
11970Sstevel@tonic-gate 	kstat_named_init(&sp->glds_errrcv, "ierrors", KSTAT_DATA_ULONG);
11980Sstevel@tonic-gate 	kstat_named_init(&sp->glds_errxmt, "oerrors", KSTAT_DATA_ULONG);
11990Sstevel@tonic-gate 	kstat_named_init(&sp->glds_bytexmt, "obytes", KSTAT_DATA_UINT32);
12000Sstevel@tonic-gate 	kstat_named_init(&sp->glds_bytercv, "rbytes", KSTAT_DATA_UINT32);
12010Sstevel@tonic-gate 	kstat_named_init(&sp->glds_multixmt, "multixmt", KSTAT_DATA_ULONG);
12020Sstevel@tonic-gate 	kstat_named_init(&sp->glds_multircv, "multircv", KSTAT_DATA_ULONG);
12030Sstevel@tonic-gate 	kstat_named_init(&sp->glds_brdcstxmt, "brdcstxmt", KSTAT_DATA_ULONG);
12040Sstevel@tonic-gate 	kstat_named_init(&sp->glds_brdcstrcv, "brdcstrcv", KSTAT_DATA_ULONG);
12050Sstevel@tonic-gate 	kstat_named_init(&sp->glds_blocked, "blocked", KSTAT_DATA_ULONG);
12060Sstevel@tonic-gate 	kstat_named_init(&sp->glds_noxmtbuf, "noxmtbuf", KSTAT_DATA_ULONG);
12070Sstevel@tonic-gate 	kstat_named_init(&sp->glds_norcvbuf, "norcvbuf", KSTAT_DATA_ULONG);
12080Sstevel@tonic-gate 	kstat_named_init(&sp->glds_xmtretry, "xmtretry", KSTAT_DATA_ULONG);
12090Sstevel@tonic-gate 	kstat_named_init(&sp->glds_intr, "intr", KSTAT_DATA_ULONG);
12100Sstevel@tonic-gate 	kstat_named_init(&sp->glds_pktrcv64, "ipackets64", KSTAT_DATA_UINT64);
12110Sstevel@tonic-gate 	kstat_named_init(&sp->glds_pktxmt64, "opackets64", KSTAT_DATA_UINT64);
12120Sstevel@tonic-gate 	kstat_named_init(&sp->glds_bytexmt64, "obytes64", KSTAT_DATA_UINT64);
12130Sstevel@tonic-gate 	kstat_named_init(&sp->glds_bytercv64, "rbytes64", KSTAT_DATA_UINT64);
12140Sstevel@tonic-gate 	kstat_named_init(&sp->glds_unknowns, "unknowns", KSTAT_DATA_ULONG);
12150Sstevel@tonic-gate 	kstat_named_init(&sp->glds_speed, "ifspeed", KSTAT_DATA_UINT64);
12160Sstevel@tonic-gate 	kstat_named_init(&sp->glds_media, "media", KSTAT_DATA_CHAR);
12170Sstevel@tonic-gate 	kstat_named_init(&sp->glds_prom, "promisc", KSTAT_DATA_CHAR);
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate 	kstat_named_init(&sp->glds_overflow, "oflo", KSTAT_DATA_ULONG);
12200Sstevel@tonic-gate 	kstat_named_init(&sp->glds_underflow, "uflo", KSTAT_DATA_ULONG);
12210Sstevel@tonic-gate 	kstat_named_init(&sp->glds_missed, "missed", KSTAT_DATA_ULONG);
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 	kstat_named_init(&sp->glds_xmtbadinterp, "xmt_badinterp",
12240Sstevel@tonic-gate 	    KSTAT_DATA_UINT32);
12250Sstevel@tonic-gate 	kstat_named_init(&sp->glds_rcvbadinterp, "rcv_badinterp",
12260Sstevel@tonic-gate 	    KSTAT_DATA_UINT32);
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate 	kstat_install(ksp);
12290Sstevel@tonic-gate 	return (GLD_SUCCESS);
12300Sstevel@tonic-gate }
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate static int
12330Sstevel@tonic-gate gld_update_vlan_kstat(kstat_t *ksp, int rw)
12340Sstevel@tonic-gate {
12350Sstevel@tonic-gate 	gld_vlan_t	*vlan;
12360Sstevel@tonic-gate 	gld_mac_info_t	*macinfo;
12370Sstevel@tonic-gate 	struct gldkstats *gsp;
12380Sstevel@tonic-gate 	struct gld_stats *stats;
12392163Syz147064 	gld_mac_pvt_t *mac_pvt;
12402163Syz147064 	uint32_t media;
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
12430Sstevel@tonic-gate 		return (EACCES);
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate 	vlan = (gld_vlan_t *)ksp->ks_private;
12460Sstevel@tonic-gate 	ASSERT(vlan != NULL);
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate 	macinfo = vlan->gldv_mac;
12490Sstevel@tonic-gate 	GLDM_LOCK(macinfo, RW_WRITER);
12500Sstevel@tonic-gate 
12512163Syz147064 	mac_pvt = (gld_mac_pvt_t *)macinfo->gldm_mac_pvt;
12522163Syz147064 
12530Sstevel@tonic-gate 	gsp = vlan->gldv_kstatp->ks_data;
12540Sstevel@tonic-gate 	ASSERT(gsp);
12550Sstevel@tonic-gate 	stats = vlan->gldv_stats;
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate 	gsp->glds_pktxmt.value.ui32 = stats->glds_pktxmt64 & 0xffffffff;
12580Sstevel@tonic-gate 	gsp->glds_bytexmt.value.ui32 = stats->glds_bytexmt64 & 0xffffffff;
12590Sstevel@tonic-gate 	gsp->glds_errxmt.value.ul = stats->glds_errxmt;
12600Sstevel@tonic-gate 	gsp->glds_multixmt.value.ul = stats->glds_multixmt;
12610Sstevel@tonic-gate 	gsp->glds_brdcstxmt.value.ul = stats->glds_brdcstxmt;
12620Sstevel@tonic-gate 	gsp->glds_noxmtbuf.value.ul = stats->glds_noxmtbuf;
12630Sstevel@tonic-gate 	gsp->glds_xmtretry.value.ul = stats->glds_xmtretry;
12640Sstevel@tonic-gate 	gsp->glds_pktxmt64.value.ui64 = stats->glds_pktxmt64;
12650Sstevel@tonic-gate 	gsp->glds_bytexmt64.value.ui64 = stats->glds_bytexmt64;
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 	gsp->glds_pktrcv.value.ui32 = stats->glds_pktrcv64 & 0xffffffff;
12680Sstevel@tonic-gate 	gsp->glds_bytercv.value.ui32 = stats->glds_bytercv64 & 0xffffffff;
12690Sstevel@tonic-gate 	gsp->glds_errrcv.value.ul = stats->glds_errrcv;
12700Sstevel@tonic-gate 	gsp->glds_multircv.value.ul = stats->glds_multircv;
12710Sstevel@tonic-gate 	gsp->glds_brdcstrcv.value.ul = stats->glds_brdcstrcv;
12720Sstevel@tonic-gate 	gsp->glds_blocked.value.ul = stats->glds_blocked;
12730Sstevel@tonic-gate 	gsp->glds_pktrcv64.value.ui64 = stats->glds_pktrcv64;
12740Sstevel@tonic-gate 	gsp->glds_bytercv64.value.ui64 = stats->glds_bytercv64;
12752163Syz147064 	gsp->glds_unknowns.value.ul = stats->glds_unknowns;
12762163Syz147064 	gsp->glds_xmtbadinterp.value.ui32 = stats->glds_xmtbadinterp;
12772163Syz147064 	gsp->glds_rcvbadinterp.value.ui32 = stats->glds_rcvbadinterp;
12782163Syz147064 
12792163Syz147064 	gsp->glds_speed.value.ui64 = mac_pvt->statistics->glds_speed;
12802163Syz147064 	media = mac_pvt->statistics->glds_media;
12812163Syz147064 	(void) strcpy(gsp->glds_media.value.c,
12822163Syz147064 	    gld_media[media < sizeof (gld_media) / sizeof (gld_media[0]) ?
12832163Syz147064 	    media : 0]);
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 	GLDM_UNLOCK(macinfo);
12860Sstevel@tonic-gate 	return (0);
12870Sstevel@tonic-gate }
12880Sstevel@tonic-gate 
12890Sstevel@tonic-gate /*
12900Sstevel@tonic-gate  * The device dependent driver specifies gld_getinfo as its getinfo routine.
12910Sstevel@tonic-gate  */
12920Sstevel@tonic-gate /*ARGSUSED*/
12930Sstevel@tonic-gate int
12940Sstevel@tonic-gate gld_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **resultp)
12950Sstevel@tonic-gate {
12960Sstevel@tonic-gate 	dev_info_t	*devinfo;
12970Sstevel@tonic-gate 	minor_t		minor = getminor((dev_t)arg);
12980Sstevel@tonic-gate 	int		rc = DDI_FAILURE;
12990Sstevel@tonic-gate 
13000Sstevel@tonic-gate 	switch (cmd) {
13010Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
13020Sstevel@tonic-gate 		if ((devinfo = gld_finddevinfo((dev_t)arg)) != NULL) {
13030Sstevel@tonic-gate 			*(dev_info_t **)resultp = devinfo;
13040Sstevel@tonic-gate 			rc = DDI_SUCCESS;
13050Sstevel@tonic-gate 		}
13060Sstevel@tonic-gate 		break;
13070Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
13080Sstevel@tonic-gate 		/* Need static mapping for deferred attach */
13090Sstevel@tonic-gate 		if (minor == GLD_USE_STYLE2) {
13100Sstevel@tonic-gate 			/*
13110Sstevel@tonic-gate 			 * Style 2:  this minor number does not correspond to
13120Sstevel@tonic-gate 			 * any particular instance number.
13130Sstevel@tonic-gate 			 */
13140Sstevel@tonic-gate 			rc = DDI_FAILURE;
13150Sstevel@tonic-gate 		} else if (minor <= GLD_MAX_STYLE1_MINOR) {
13160Sstevel@tonic-gate 			/* Style 1:  calculate the PPA from the minor */
13171717Swesolows 			*resultp = (void *)(uintptr_t)
13181717Swesolows 			    GLD_STYLE1_MINOR_TO_PPA(minor);
13190Sstevel@tonic-gate 			rc = DDI_SUCCESS;
13200Sstevel@tonic-gate 		} else {
13210Sstevel@tonic-gate 			/* Clone:  look for it.  Not a static mapping */
13220Sstevel@tonic-gate 			if ((devinfo = gld_finddevinfo((dev_t)arg)) != NULL) {
13231717Swesolows 				*resultp = (void *)(uintptr_t)
13241717Swesolows 				    ddi_get_instance(devinfo);
13250Sstevel@tonic-gate 				rc = DDI_SUCCESS;
13260Sstevel@tonic-gate 			}
13270Sstevel@tonic-gate 		}
13280Sstevel@tonic-gate 		break;
13290Sstevel@tonic-gate 	}
13300Sstevel@tonic-gate 
13310Sstevel@tonic-gate 	return (rc);
13320Sstevel@tonic-gate }
13330Sstevel@tonic-gate 
13340Sstevel@tonic-gate /* called from gld_getinfo */
13350Sstevel@tonic-gate dev_info_t *
13360Sstevel@tonic-gate gld_finddevinfo(dev_t dev)
13370Sstevel@tonic-gate {
13380Sstevel@tonic-gate 	minor_t		minor = getminor(dev);
13390Sstevel@tonic-gate 	glddev_t	*device;
13400Sstevel@tonic-gate 	gld_mac_info_t	*mac;
13410Sstevel@tonic-gate 	gld_vlan_t	*vlan;
13420Sstevel@tonic-gate 	gld_t		*str;
13430Sstevel@tonic-gate 	dev_info_t	*devinfo = NULL;
13440Sstevel@tonic-gate 	int		i;
13450Sstevel@tonic-gate 
13460Sstevel@tonic-gate 	if (minor == GLD_USE_STYLE2) {
13470Sstevel@tonic-gate 		/*
13480Sstevel@tonic-gate 		 * Style 2:  this minor number does not correspond to
13490Sstevel@tonic-gate 		 * any particular instance number.
13500Sstevel@tonic-gate 		 *
13510Sstevel@tonic-gate 		 * XXX We don't know what to say.  See Bug 1165519.
13520Sstevel@tonic-gate 		 */
13530Sstevel@tonic-gate 		return (NULL);
13540Sstevel@tonic-gate 	}
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate 	mutex_enter(&gld_device_list.gld_devlock);	/* hold the device */
13570Sstevel@tonic-gate 
13580Sstevel@tonic-gate 	device = gld_devlookup(getmajor(dev));
13590Sstevel@tonic-gate 	if (device == NULL) {
13600Sstevel@tonic-gate 		/* There are no attached instances of this device */
13610Sstevel@tonic-gate 		mutex_exit(&gld_device_list.gld_devlock);
13620Sstevel@tonic-gate 		return (NULL);
13630Sstevel@tonic-gate 	}
13640Sstevel@tonic-gate 
13650Sstevel@tonic-gate 	/*
13660Sstevel@tonic-gate 	 * Search all attached macs and streams.
13670Sstevel@tonic-gate 	 *
13680Sstevel@tonic-gate 	 * XXX We don't bother checking the DL_UNATTACHED streams since
13690Sstevel@tonic-gate 	 * we don't know what devinfo we should report back even if we
13700Sstevel@tonic-gate 	 * found the minor.  Maybe we should associate streams that are
13710Sstevel@tonic-gate 	 * not currently attached to a PPA with the "first" devinfo node
13720Sstevel@tonic-gate 	 * of the major device to attach -- the one that created the
13730Sstevel@tonic-gate 	 * minor node for the generic device.
13740Sstevel@tonic-gate 	 */
13750Sstevel@tonic-gate 	mutex_enter(&device->gld_devlock);
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate 	for (mac = device->gld_mac_next;
13780Sstevel@tonic-gate 	    mac != (gld_mac_info_t *)&device->gld_mac_next;
13790Sstevel@tonic-gate 	    mac = mac->gldm_next) {
13800Sstevel@tonic-gate 		gld_mac_pvt_t *pvt = (gld_mac_pvt_t *)mac->gldm_mac_pvt;
13810Sstevel@tonic-gate 
13820Sstevel@tonic-gate 		if (!(mac->gldm_GLD_flags & GLD_MAC_READY))
13830Sstevel@tonic-gate 			continue;	/* this one's not ready yet */
13840Sstevel@tonic-gate 		if (minor <= GLD_MAX_STYLE1_MINOR) {
13850Sstevel@tonic-gate 			/* Style 1 -- look for the corresponding PPA */
13860Sstevel@tonic-gate 			if (minor == GLD_STYLE1_PPA_TO_MINOR(mac->gldm_ppa)) {
13870Sstevel@tonic-gate 				devinfo = mac->gldm_devinfo;
13880Sstevel@tonic-gate 				goto out;	/* found it! */
13890Sstevel@tonic-gate 			} else
13900Sstevel@tonic-gate 				continue;	/* not this PPA */
13910Sstevel@tonic-gate 		}
13920Sstevel@tonic-gate 
13930Sstevel@tonic-gate 		/* We are looking for a clone */
13940Sstevel@tonic-gate 		for (i = 0; i < VLAN_HASHSZ; i++) {
13950Sstevel@tonic-gate 			for (vlan = pvt->vlan_hash[i];
13960Sstevel@tonic-gate 			    vlan != NULL; vlan = vlan->gldv_next) {
13970Sstevel@tonic-gate 				for (str = vlan->gldv_str_next;
13980Sstevel@tonic-gate 				    str != (gld_t *)&vlan->gldv_str_next;
13990Sstevel@tonic-gate 				    str = str->gld_next) {
14000Sstevel@tonic-gate 					ASSERT(str->gld_mac_info == mac);
14010Sstevel@tonic-gate 					if (minor == str->gld_minor) {
14020Sstevel@tonic-gate 						devinfo = mac->gldm_devinfo;
14030Sstevel@tonic-gate 						goto out;
14040Sstevel@tonic-gate 					}
14050Sstevel@tonic-gate 				}
14060Sstevel@tonic-gate 			}
14070Sstevel@tonic-gate 		}
14080Sstevel@tonic-gate 	}
14090Sstevel@tonic-gate out:
14100Sstevel@tonic-gate 	mutex_exit(&device->gld_devlock);
14110Sstevel@tonic-gate 	mutex_exit(&gld_device_list.gld_devlock);
14120Sstevel@tonic-gate 	return (devinfo);
14130Sstevel@tonic-gate }
14140Sstevel@tonic-gate 
14150Sstevel@tonic-gate /*
14160Sstevel@tonic-gate  * STREAMS open routine.  The device dependent driver specifies this as its
14170Sstevel@tonic-gate  * open entry point.
14180Sstevel@tonic-gate  */
14190Sstevel@tonic-gate /*ARGSUSED2*/
14200Sstevel@tonic-gate int
14210Sstevel@tonic-gate gld_open(queue_t *q, dev_t *dev, int flag, int sflag, cred_t *cred)
14220Sstevel@tonic-gate {
14230Sstevel@tonic-gate 	gld_mac_pvt_t *mac_pvt;
14240Sstevel@tonic-gate 	gld_t *gld;
14250Sstevel@tonic-gate 	glddev_t *glddev;
14260Sstevel@tonic-gate 	gld_mac_info_t *macinfo;
14270Sstevel@tonic-gate 	minor_t minor = getminor(*dev);
14280Sstevel@tonic-gate 	gld_vlan_t *vlan;
14290Sstevel@tonic-gate 	t_uscalar_t ppa;
14300Sstevel@tonic-gate 
14310Sstevel@tonic-gate 	ASSERT(q != NULL);
14320Sstevel@tonic-gate 
14330Sstevel@tonic-gate 	if (minor > GLD_MAX_STYLE1_MINOR)
14340Sstevel@tonic-gate 		return (ENXIO);
14350Sstevel@tonic-gate 
14360Sstevel@tonic-gate 	ASSERT(q->q_ptr == NULL);	/* Clone device gives us a fresh Q */
14370Sstevel@tonic-gate 
14380Sstevel@tonic-gate 	/* Find our per-major glddev_t structure */
14390Sstevel@tonic-gate 	mutex_enter(&gld_device_list.gld_devlock);
14400Sstevel@tonic-gate 	glddev = gld_devlookup(getmajor(*dev));
14410Sstevel@tonic-gate 
14420Sstevel@tonic-gate 	/*
14430Sstevel@tonic-gate 	 * This glddev will hang around since detach (and therefore
14440Sstevel@tonic-gate 	 * gld_unregister) can't run while we're here in the open routine.
14450Sstevel@tonic-gate 	 */
14460Sstevel@tonic-gate 	mutex_exit(&gld_device_list.gld_devlock);
14470Sstevel@tonic-gate 
14480Sstevel@tonic-gate 	if (glddev == NULL)
14490Sstevel@tonic-gate 		return (ENXIO);
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate #ifdef GLD_DEBUG
14520Sstevel@tonic-gate 	if (gld_debug & GLDPROT) {
14530Sstevel@tonic-gate 		if (minor == GLD_USE_STYLE2)
14540Sstevel@tonic-gate 			cmn_err(CE_NOTE, "gld_open(%p, Style 2)", (void *)q);
14550Sstevel@tonic-gate 		else
14560Sstevel@tonic-gate 			cmn_err(CE_NOTE, "gld_open(%p, Style 1, minor = %d)",
14570Sstevel@tonic-gate 			    (void *)q, minor);
14580Sstevel@tonic-gate 	}
14590Sstevel@tonic-gate #endif
14600Sstevel@tonic-gate 
14610Sstevel@tonic-gate 	/*
14620Sstevel@tonic-gate 	 * get a per-stream structure and link things together so we
14630Sstevel@tonic-gate 	 * can easily find them later.
14640Sstevel@tonic-gate 	 */
14650Sstevel@tonic-gate 	gld = kmem_zalloc(sizeof (gld_t), KM_SLEEP);
14660Sstevel@tonic-gate 
14670Sstevel@tonic-gate 	/*
14680Sstevel@tonic-gate 	 * fill in the structure and state info
14690Sstevel@tonic-gate 	 */
14700Sstevel@tonic-gate 	gld->gld_qptr = q;
14710Sstevel@tonic-gate 	gld->gld_device = glddev;
14720Sstevel@tonic-gate 	gld->gld_state = DL_UNATTACHED;
14730Sstevel@tonic-gate 
14740Sstevel@tonic-gate 	/*
14750Sstevel@tonic-gate 	 * we must atomically find a free minor number and add the stream
14760Sstevel@tonic-gate 	 * to a list, because gld_findminor has to traverse the lists to
14770Sstevel@tonic-gate 	 * determine which minor numbers are free.
14780Sstevel@tonic-gate 	 */
14790Sstevel@tonic-gate 	mutex_enter(&glddev->gld_devlock);
14800Sstevel@tonic-gate 
14810Sstevel@tonic-gate 	/* find a free minor device number for the clone */
14820Sstevel@tonic-gate 	gld->gld_minor = gld_findminor(glddev);
14830Sstevel@tonic-gate 	if (gld->gld_minor == 0) {
14840Sstevel@tonic-gate 		mutex_exit(&glddev->gld_devlock);
14850Sstevel@tonic-gate 		kmem_free(gld, sizeof (gld_t));
14860Sstevel@tonic-gate 		return (ENOSR);
14870Sstevel@tonic-gate 	}
14880Sstevel@tonic-gate 
14890Sstevel@tonic-gate #ifdef GLD_VERBOSE_DEBUG
14900Sstevel@tonic-gate 	if (gld_debug & GLDPROT)
14910Sstevel@tonic-gate 		cmn_err(CE_NOTE, "gld_open() gld ptr: %p minor: %d",
14920Sstevel@tonic-gate 		    (void *)gld, gld->gld_minor);
14930Sstevel@tonic-gate #endif
14940Sstevel@tonic-gate 
14950Sstevel@tonic-gate 	if (minor == GLD_USE_STYLE2) {
14960Sstevel@tonic-gate 		gld->gld_style = DL_STYLE2;
14970Sstevel@tonic-gate 		*dev = makedevice(getmajor(*dev), gld->gld_minor);
14980Sstevel@tonic-gate 		WR(q)->q_ptr = q->q_ptr = (caddr_t)gld;
14990Sstevel@tonic-gate 		gldinsque(gld, glddev->gld_str_prev);
15000Sstevel@tonic-gate #ifdef GLD_VERBOSE_DEBUG
15010Sstevel@tonic-gate 		if (gld_debug & GLDPROT)
15020Sstevel@tonic-gate 			cmn_err(CE_NOTE, "GLDstruct added to device list");
15030Sstevel@tonic-gate #endif
15040Sstevel@tonic-gate 		(void) qassociate(q, -1);
15050Sstevel@tonic-gate 		goto done;
15060Sstevel@tonic-gate 	}
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate 	gld->gld_style = DL_STYLE1;
15090Sstevel@tonic-gate 
15100Sstevel@tonic-gate 	/* the PPA is actually 1 less than the minordev */
15110Sstevel@tonic-gate 	ppa = GLD_STYLE1_MINOR_TO_PPA(minor);
15120Sstevel@tonic-gate 
15130Sstevel@tonic-gate 	for (macinfo = glddev->gld_mac_next;
15140Sstevel@tonic-gate 	    macinfo != (gld_mac_info_t *)(&glddev->gld_mac_next);
15150Sstevel@tonic-gate 	    macinfo = macinfo->gldm_next) {
15160Sstevel@tonic-gate 		ASSERT(macinfo != NULL);
15170Sstevel@tonic-gate 		if (macinfo->gldm_ppa != ppa)
15180Sstevel@tonic-gate 			continue;
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 		if (!(macinfo->gldm_GLD_flags & GLD_MAC_READY))
15210Sstevel@tonic-gate 			continue;	/* this one's not ready yet */
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate 		/*
15240Sstevel@tonic-gate 		 * we found the correct PPA
15250Sstevel@tonic-gate 		 */
15260Sstevel@tonic-gate 		GLDM_LOCK(macinfo, RW_WRITER);
15270Sstevel@tonic-gate 
15280Sstevel@tonic-gate 		gld->gld_mac_info = macinfo;
15290Sstevel@tonic-gate 
15300Sstevel@tonic-gate 		if (macinfo->gldm_send_tagged != NULL)
15310Sstevel@tonic-gate 			gld->gld_send = macinfo->gldm_send_tagged;
15320Sstevel@tonic-gate 		else
15330Sstevel@tonic-gate 			gld->gld_send = macinfo->gldm_send;
15340Sstevel@tonic-gate 
15350Sstevel@tonic-gate 		/* now ready for action */
15360Sstevel@tonic-gate 		gld->gld_state = DL_UNBOUND;
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate 		if ((vlan = gld_get_vlan(macinfo, VLAN_VID_NONE)) == NULL) {
15390Sstevel@tonic-gate 			GLDM_UNLOCK(macinfo);
15400Sstevel@tonic-gate 			mutex_exit(&glddev->gld_devlock);
15410Sstevel@tonic-gate 			kmem_free(gld, sizeof (gld_t));
15420Sstevel@tonic-gate 			return (EIO);
15430Sstevel@tonic-gate 		}
15440Sstevel@tonic-gate 
15450Sstevel@tonic-gate 		mac_pvt = (gld_mac_pvt_t *)macinfo->gldm_mac_pvt;
15460Sstevel@tonic-gate 		if (!mac_pvt->started) {
15470Sstevel@tonic-gate 			if (gld_start_mac(macinfo) != GLD_SUCCESS) {
15481521Syz147064 				gld_rem_vlan(vlan);
15490Sstevel@tonic-gate 				GLDM_UNLOCK(macinfo);
15500Sstevel@tonic-gate 				mutex_exit(&glddev->gld_devlock);
15510Sstevel@tonic-gate 				kmem_free(gld, sizeof (gld_t));
15520Sstevel@tonic-gate 				return (EIO);
15530Sstevel@tonic-gate 			}
15540Sstevel@tonic-gate 		}
15550Sstevel@tonic-gate 
15560Sstevel@tonic-gate 		gld->gld_vlan = vlan;
15570Sstevel@tonic-gate 		vlan->gldv_nstreams++;
15580Sstevel@tonic-gate 		gldinsque(gld, vlan->gldv_str_prev);
15590Sstevel@tonic-gate 		*dev = makedevice(getmajor(*dev), gld->gld_minor);
15600Sstevel@tonic-gate 		WR(q)->q_ptr = q->q_ptr = (caddr_t)gld;
15610Sstevel@tonic-gate 
15620Sstevel@tonic-gate 		GLDM_UNLOCK(macinfo);
15630Sstevel@tonic-gate #ifdef GLD_VERBOSE_DEBUG
15640Sstevel@tonic-gate 		if (gld_debug & GLDPROT)
15650Sstevel@tonic-gate 			cmn_err(CE_NOTE,
15660Sstevel@tonic-gate 			    "GLDstruct added to instance list");
15670Sstevel@tonic-gate #endif
15680Sstevel@tonic-gate 		break;
15690Sstevel@tonic-gate 	}
15700Sstevel@tonic-gate 
15710Sstevel@tonic-gate 	if (gld->gld_state == DL_UNATTACHED) {
15720Sstevel@tonic-gate 		mutex_exit(&glddev->gld_devlock);
15730Sstevel@tonic-gate 		kmem_free(gld, sizeof (gld_t));
15740Sstevel@tonic-gate 		return (ENXIO);
15750Sstevel@tonic-gate 	}
15760Sstevel@tonic-gate 
15770Sstevel@tonic-gate done:
15780Sstevel@tonic-gate 	mutex_exit(&glddev->gld_devlock);
15790Sstevel@tonic-gate 	noenable(WR(q));	/* We'll do the qenables manually */
15800Sstevel@tonic-gate 	qprocson(q);		/* start the queues running */
15810Sstevel@tonic-gate 	qenable(WR(q));
15820Sstevel@tonic-gate 	return (0);
15830Sstevel@tonic-gate }
15840Sstevel@tonic-gate 
15850Sstevel@tonic-gate /*
15860Sstevel@tonic-gate  * normal stream close call checks current status and cleans up
15870Sstevel@tonic-gate  * data structures that were dynamically allocated
15880Sstevel@tonic-gate  */
15890Sstevel@tonic-gate /*ARGSUSED1*/
15900Sstevel@tonic-gate int
15910Sstevel@tonic-gate gld_close(queue_t *q, int flag, cred_t *cred)
15920Sstevel@tonic-gate {
15930Sstevel@tonic-gate 	gld_t	*gld = (gld_t *)q->q_ptr;
15940Sstevel@tonic-gate 	glddev_t *glddev = gld->gld_device;
15950Sstevel@tonic-gate 
15960Sstevel@tonic-gate 	ASSERT(q);
15970Sstevel@tonic-gate 	ASSERT(gld);
15980Sstevel@tonic-gate 
15990Sstevel@tonic-gate #ifdef GLD_DEBUG
16000Sstevel@tonic-gate 	if (gld_debug & GLDPROT) {
16010Sstevel@tonic-gate 		cmn_err(CE_NOTE, "gld_close(%p, Style %d)",
16020Sstevel@tonic-gate 		    (void *)q, (gld->gld_style & 0x1) + 1);
16030Sstevel@tonic-gate 	}
16040Sstevel@tonic-gate #endif
16050Sstevel@tonic-gate 
16060Sstevel@tonic-gate 	/* Hold all device streams lists still while we check for a macinfo */
16070Sstevel@tonic-gate 	mutex_enter(&glddev->gld_devlock);
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate 	if (gld->gld_mac_info != NULL) {
16100Sstevel@tonic-gate 		/* If there's a macinfo, block recv while we change state */
16110Sstevel@tonic-gate 		GLDM_LOCK(gld->gld_mac_info, RW_WRITER);
16120Sstevel@tonic-gate 		gld->gld_flags |= GLD_STR_CLOSING; /* no more rcv putnexts */
16130Sstevel@tonic-gate 		GLDM_UNLOCK(gld->gld_mac_info);
16140Sstevel@tonic-gate 	} else {
16150Sstevel@tonic-gate 		/* no mac DL_ATTACHED right now */
16160Sstevel@tonic-gate 		gld->gld_flags |= GLD_STR_CLOSING;
16170Sstevel@tonic-gate 	}
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate 	mutex_exit(&glddev->gld_devlock);
16200Sstevel@tonic-gate 
16210Sstevel@tonic-gate 	/*
16220Sstevel@tonic-gate 	 * qprocsoff before we call gld_unbind/gldunattach, so that
16230Sstevel@tonic-gate 	 * we know wsrv isn't in there trying to undo what we're doing.
16240Sstevel@tonic-gate 	 */
16250Sstevel@tonic-gate 	qprocsoff(q);
16260Sstevel@tonic-gate 
16270Sstevel@tonic-gate 	ASSERT(gld->gld_wput_count == 0);
16280Sstevel@tonic-gate 	gld->gld_wput_count = 0;	/* just in case */
16290Sstevel@tonic-gate 
16300Sstevel@tonic-gate 	if (gld->gld_state == DL_IDLE) {
16310Sstevel@tonic-gate 		/* Need to unbind */
16320Sstevel@tonic-gate 		ASSERT(gld->gld_mac_info != NULL);
16330Sstevel@tonic-gate 		(void) gld_unbind(WR(q), NULL);
16340Sstevel@tonic-gate 	}
16350Sstevel@tonic-gate 
16360Sstevel@tonic-gate 	if (gld->gld_state == DL_UNBOUND) {
16370Sstevel@tonic-gate 		/*
16380Sstevel@tonic-gate 		 * Need to unattach
16390Sstevel@tonic-gate 		 * For style 2 stream, gldunattach also
16400Sstevel@tonic-gate 		 * associate queue with NULL dip
16410Sstevel@tonic-gate 		 */
16420Sstevel@tonic-gate 		ASSERT(gld->gld_mac_info != NULL);
16430Sstevel@tonic-gate 		(void) gldunattach(WR(q), NULL);
16440Sstevel@tonic-gate 	}
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate 	/* disassociate the stream from the device */
16470Sstevel@tonic-gate 	q->q_ptr = WR(q)->q_ptr = NULL;
16480Sstevel@tonic-gate 
16490Sstevel@tonic-gate 	/*
16500Sstevel@tonic-gate 	 * Since we unattached above (if necessary), we know that we're
16510Sstevel@tonic-gate 	 * on the per-major list of unattached streams, rather than a
16520Sstevel@tonic-gate 	 * per-PPA list.  So we know we should hold the devlock.
16530Sstevel@tonic-gate 	 */
16540Sstevel@tonic-gate 	mutex_enter(&glddev->gld_devlock);
16550Sstevel@tonic-gate 	gldremque(gld);			/* remove from Style 2 list */
16560Sstevel@tonic-gate 	mutex_exit(&glddev->gld_devlock);
16570Sstevel@tonic-gate 
16580Sstevel@tonic-gate 	kmem_free(gld, sizeof (gld_t));
16590Sstevel@tonic-gate 
16600Sstevel@tonic-gate 	return (0);
16610Sstevel@tonic-gate }
16620Sstevel@tonic-gate 
16630Sstevel@tonic-gate /*
16640Sstevel@tonic-gate  * gld_rsrv (q)
16650Sstevel@tonic-gate  *	simple read service procedure
16660Sstevel@tonic-gate  *	purpose is to avoid the time it takes for packets
16670Sstevel@tonic-gate  *	to move through IP so we can get them off the board
16680Sstevel@tonic-gate  *	as fast as possible due to limited PC resources.
16690Sstevel@tonic-gate  *
16700Sstevel@tonic-gate  *	This is not normally used in the current implementation.  It
16710Sstevel@tonic-gate  *	can be selected with the undocumented property "fast_recv".
16720Sstevel@tonic-gate  *	If that property is set, gld_recv will send the packet
16730Sstevel@tonic-gate  *	upstream with a putq() rather than a putnext(), thus causing
16740Sstevel@tonic-gate  *	this routine to be scheduled.
16750Sstevel@tonic-gate  */
16760Sstevel@tonic-gate int
16770Sstevel@tonic-gate gld_rsrv(queue_t *q)
16780Sstevel@tonic-gate {
16790Sstevel@tonic-gate 	mblk_t *mp;
16800Sstevel@tonic-gate 
16810Sstevel@tonic-gate 	while ((mp = getq(q)) != NULL) {
16820Sstevel@tonic-gate 		if (canputnext(q)) {
16830Sstevel@tonic-gate 			putnext(q, mp);
16840Sstevel@tonic-gate 		} else {
16850Sstevel@tonic-gate 			freemsg(mp);
16860Sstevel@tonic-gate 		}
16870Sstevel@tonic-gate 	}
16880Sstevel@tonic-gate 	return (0);
16890Sstevel@tonic-gate }
16900Sstevel@tonic-gate 
16910Sstevel@tonic-gate /*
16920Sstevel@tonic-gate  * gld_wput (q, mp)
16930Sstevel@tonic-gate  * general gld stream write put routine. Receives fastpath data from upper
16940Sstevel@tonic-gate  * modules and processes it immediately.  ioctl and M_PROTO/M_PCPROTO are
16950Sstevel@tonic-gate  * queued for later processing by the service procedure.
16960Sstevel@tonic-gate  */
16970Sstevel@tonic-gate 
16980Sstevel@tonic-gate int
16990Sstevel@tonic-gate gld_wput(queue_t *q, mblk_t *mp)
17000Sstevel@tonic-gate {
17010Sstevel@tonic-gate 	gld_t  *gld = (gld_t *)(q->q_ptr);
17020Sstevel@tonic-gate 	int	rc;
17030Sstevel@tonic-gate 	boolean_t multidata = B_TRUE;
1704*2760Sdg199075 	uint32_t upri;
17050Sstevel@tonic-gate 
17060Sstevel@tonic-gate #ifdef GLD_DEBUG
17070Sstevel@tonic-gate 	if (gld_debug & GLDTRACE)
17080Sstevel@tonic-gate 		cmn_err(CE_NOTE, "gld_wput(%p %p): type %x",
17090Sstevel@tonic-gate 		    (void *)q, (void *)mp, DB_TYPE(mp));
17100Sstevel@tonic-gate #endif
17110Sstevel@tonic-gate 	switch (DB_TYPE(mp)) {
17120Sstevel@tonic-gate 
17130Sstevel@tonic-gate 	case M_DATA:
17140Sstevel@tonic-gate 		/* fast data / raw support */
17150Sstevel@tonic-gate 		/* we must be DL_ATTACHED and DL_BOUND to do this */
17160Sstevel@tonic-gate 		/* Tricky to access memory without taking the mutex */
17170Sstevel@tonic-gate 		if ((gld->gld_flags & (GLD_RAW | GLD_FAST)) == 0 ||
17180Sstevel@tonic-gate 		    gld->gld_state != DL_IDLE) {
17190Sstevel@tonic-gate 			merror(q, mp, EPROTO);
17200Sstevel@tonic-gate 			break;
17210Sstevel@tonic-gate 		}
1722*2760Sdg199075 		/*
1723*2760Sdg199075 		 * Cleanup MBLK_VTAG in case it is set by other
1724*2760Sdg199075 		 * modules. MBLK_VTAG is used to save the vtag information.
1725*2760Sdg199075 		 */
1726*2760Sdg199075 		GLD_CLEAR_MBLK_VTAG(mp);
17270Sstevel@tonic-gate 		multidata = B_FALSE;
17280Sstevel@tonic-gate 		/* LINTED: E_CASE_FALLTHRU */
17290Sstevel@tonic-gate 	case M_MULTIDATA:
17300Sstevel@tonic-gate 		/* Only call gld_start() directly if nothing queued ahead */
17310Sstevel@tonic-gate 		/* No guarantees about ordering with different threads */
17320Sstevel@tonic-gate 		if (q->q_first)
17330Sstevel@tonic-gate 			goto use_wsrv;
17340Sstevel@tonic-gate 
17350Sstevel@tonic-gate 		/*
17360Sstevel@tonic-gate 		 * This can happen if wsrv has taken off the last mblk but
17370Sstevel@tonic-gate 		 * is still processing it.
17380Sstevel@tonic-gate 		 */
17390Sstevel@tonic-gate 		membar_consumer();
17400Sstevel@tonic-gate 		if (gld->gld_in_wsrv)
17410Sstevel@tonic-gate 			goto use_wsrv;
17420Sstevel@tonic-gate 
17430Sstevel@tonic-gate 		/*
17440Sstevel@tonic-gate 		 * Keep a count of current wput calls to start.
17450Sstevel@tonic-gate 		 * Nonzero count delays any attempted DL_UNBIND.
17460Sstevel@tonic-gate 		 * See comments above gld_start().
17470Sstevel@tonic-gate 		 */
17480Sstevel@tonic-gate 		atomic_add_32((uint32_t *)&gld->gld_wput_count, 1);
17490Sstevel@tonic-gate 		membar_enter();
17500Sstevel@tonic-gate 
17510Sstevel@tonic-gate 		/* Recheck state now wput_count is set to prevent DL_UNBIND */
17520Sstevel@tonic-gate 		/* If this Q is in process of DL_UNBIND, don't call start */
17530Sstevel@tonic-gate 		if (gld->gld_state != DL_IDLE || gld->gld_in_unbind) {
17540Sstevel@tonic-gate 			/* Extremely unlikely */
17550Sstevel@tonic-gate 			atomic_add_32((uint32_t *)&gld->gld_wput_count, -1);
17560Sstevel@tonic-gate 			goto use_wsrv;
17570Sstevel@tonic-gate 		}
17580Sstevel@tonic-gate 
1759*2760Sdg199075 		/*
1760*2760Sdg199075 		 * Get the priority value. Note that in raw mode, the
1761*2760Sdg199075 		 * per-packet priority value kept in b_band is ignored.
1762*2760Sdg199075 		 */
1763*2760Sdg199075 		upri = (gld->gld_flags & GLD_RAW) ? gld->gld_upri :
1764*2760Sdg199075 		    UPRI(gld, mp->b_band);
1765*2760Sdg199075 
17660Sstevel@tonic-gate 		rc = (multidata) ? gld_start_mdt(q, mp, GLD_WPUT) :
1767*2760Sdg199075 		    gld_start(q, mp, GLD_WPUT, upri);
17680Sstevel@tonic-gate 
17690Sstevel@tonic-gate 		/* Allow DL_UNBIND again */
17700Sstevel@tonic-gate 		membar_exit();
17710Sstevel@tonic-gate 		atomic_add_32((uint32_t *)&gld->gld_wput_count, -1);
17720Sstevel@tonic-gate 
17730Sstevel@tonic-gate 		if (rc == GLD_NORESOURCES)
17740Sstevel@tonic-gate 			qenable(q);
17750Sstevel@tonic-gate 		break;	/*  Done with this packet */
17760Sstevel@tonic-gate 
17770Sstevel@tonic-gate use_wsrv:
17780Sstevel@tonic-gate 		/* Q not empty, in DL_DETACH, or start gave NORESOURCES */
17790Sstevel@tonic-gate 		(void) putq(q, mp);
17800Sstevel@tonic-gate 		qenable(q);
17810Sstevel@tonic-gate 		break;
17820Sstevel@tonic-gate 
17830Sstevel@tonic-gate 	case M_IOCTL:
17840Sstevel@tonic-gate 		/* ioctl relies on wsrv single threading per queue */
17850Sstevel@tonic-gate 		(void) putq(q, mp);
17860Sstevel@tonic-gate 		qenable(q);
17870Sstevel@tonic-gate 		break;
17880Sstevel@tonic-gate 
17890Sstevel@tonic-gate 	case M_CTL:
17900Sstevel@tonic-gate 		(void) putq(q, mp);
17910Sstevel@tonic-gate 		qenable(q);
17920Sstevel@tonic-gate 		break;
17930Sstevel@tonic-gate 
17940Sstevel@tonic-gate 	case M_FLUSH:		/* canonical flush handling */
17950Sstevel@tonic-gate 		/* XXX Should these be FLUSHALL? */
17960Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW)
17970Sstevel@tonic-gate 			flushq(q, 0);
17980Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHR) {
17990Sstevel@tonic-gate 			flushq(RD(q), 0);
18000Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHW;
18010Sstevel@tonic-gate 			qreply(q, mp);
18020Sstevel@tonic-gate 		} else
18030Sstevel@tonic-gate 			freemsg(mp);
18040Sstevel@tonic-gate 		break;
18050Sstevel@tonic-gate 
18060Sstevel@tonic-gate 	case M_PROTO:
18070Sstevel@tonic-gate 	case M_PCPROTO:
18080Sstevel@tonic-gate 		/* these rely on wsrv single threading per queue */
18090Sstevel@tonic-gate 		(void) putq(q, mp);
18100Sstevel@tonic-gate 		qenable(q);
18110Sstevel@tonic-gate 		break;
18120Sstevel@tonic-gate 
18130Sstevel@tonic-gate 	default:
18140Sstevel@tonic-gate #ifdef GLD_DEBUG
18150Sstevel@tonic-gate 		if (gld_debug & GLDETRACE)
18160Sstevel@tonic-gate 			cmn_err(CE_WARN,
18170Sstevel@tonic-gate 			    "gld: Unexpected packet type from queue: 0x%x",
18180Sstevel@tonic-gate 			    DB_TYPE(mp));
18190Sstevel@tonic-gate #endif
18200Sstevel@tonic-gate 		freemsg(mp);
18210Sstevel@tonic-gate 	}
18220Sstevel@tonic-gate 	return (0);
18230Sstevel@tonic-gate }
18240Sstevel@tonic-gate 
18250Sstevel@tonic-gate /*
18260Sstevel@tonic-gate  * gld_wsrv - Incoming messages are processed according to the DLPI protocol
18270Sstevel@tonic-gate  * specification.
18280Sstevel@tonic-gate  *
18290Sstevel@tonic-gate  * wsrv is single-threaded per Q.  We make use of this to avoid taking the
18300Sstevel@tonic-gate  * lock for reading data items that are only ever written by us.
18310Sstevel@tonic-gate  */
18320Sstevel@tonic-gate 
18330Sstevel@tonic-gate int
18340Sstevel@tonic-gate gld_wsrv(queue_t *q)
18350Sstevel@tonic-gate {
18360Sstevel@tonic-gate 	mblk_t *mp;
18370Sstevel@tonic-gate 	gld_t *gld = (gld_t *)q->q_ptr;
18380Sstevel@tonic-gate 	gld_mac_info_t *macinfo;
18390Sstevel@tonic-gate 	union DL_primitives *prim;
18400Sstevel@tonic-gate 	int err;
18410Sstevel@tonic-gate 	boolean_t multidata;
1842*2760Sdg199075 	uint32_t upri;
18430Sstevel@tonic-gate 
18440Sstevel@tonic-gate #ifdef GLD_DEBUG
18450Sstevel@tonic-gate 	if (gld_debug & GLDTRACE)
18460Sstevel@tonic-gate 		cmn_err(CE_NOTE, "gld_wsrv(%p)", (void *)q);
18470Sstevel@tonic-gate #endif
18480Sstevel@tonic-gate 
18490Sstevel@tonic-gate 	ASSERT(!gld->gld_in_wsrv);
18500Sstevel@tonic-gate 
18510Sstevel@tonic-gate 	gld->gld_xwait = B_FALSE; /* We are now going to process this Q */
18520Sstevel@tonic-gate 
18530Sstevel@tonic-gate 	if (q->q_first == NULL)
18540Sstevel@tonic-gate 		return (0);
18550Sstevel@tonic-gate 
18560Sstevel@tonic-gate 	macinfo = gld->gld_mac_info;
18570Sstevel@tonic-gate 
18580Sstevel@tonic-gate 	/*
18590Sstevel@tonic-gate 	 * Help wput avoid a call to gld_start if there might be a message
18600Sstevel@tonic-gate 	 * previously queued by that thread being processed here.
18610Sstevel@tonic-gate 	 */
18620Sstevel@tonic-gate 	gld->gld_in_wsrv = B_TRUE;
18630Sstevel@tonic-gate 	membar_enter();
18640Sstevel@tonic-gate 
18650Sstevel@tonic-gate 	while ((mp = getq(q)) != NULL) {
18660Sstevel@tonic-gate 		switch (DB_TYPE(mp)) {
18670Sstevel@tonic-gate 		case M_DATA:
18680Sstevel@tonic-gate 		case M_MULTIDATA:
18690Sstevel@tonic-gate 			multidata = (DB_TYPE(mp) == M_MULTIDATA);
18700Sstevel@tonic-gate 
18710Sstevel@tonic-gate 			/*
18720Sstevel@tonic-gate 			 * retry of a previously processed UNITDATA_REQ
18730Sstevel@tonic-gate 			 * or is a RAW or FAST message from above.
18740Sstevel@tonic-gate 			 */
18750Sstevel@tonic-gate 			if (macinfo == NULL) {
18760Sstevel@tonic-gate 				/* No longer attached to a PPA, drop packet */
18770Sstevel@tonic-gate 				freemsg(mp);
18780Sstevel@tonic-gate 				break;
18790Sstevel@tonic-gate 			}
18800Sstevel@tonic-gate 
18810Sstevel@tonic-gate 			gld->gld_sched_ran = B_FALSE;
18820Sstevel@tonic-gate 			membar_enter();
1883*2760Sdg199075 
1884*2760Sdg199075 			/*
1885*2760Sdg199075 			 * Get the priority value. Note that in raw mode, the
1886*2760Sdg199075 			 * per-packet priority value kept in b_band is ignored.
1887*2760Sdg199075 			 */
1888*2760Sdg199075 			upri = (gld->gld_flags & GLD_RAW) ? gld->gld_upri :
1889*2760Sdg199075 			    UPRI(gld, mp->b_band);
1890*2760Sdg199075 
18910Sstevel@tonic-gate 			err = (multidata) ? gld_start_mdt(q, mp, GLD_WSRV) :
1892*2760Sdg199075 			    gld_start(q, mp, GLD_WSRV, upri);
18930Sstevel@tonic-gate 			if (err == GLD_NORESOURCES) {
18940Sstevel@tonic-gate 				/* gld_sched will qenable us later */
18950Sstevel@tonic-gate 				gld->gld_xwait = B_TRUE; /* want qenable */
18960Sstevel@tonic-gate 				membar_enter();
18970Sstevel@tonic-gate 				/*
18980Sstevel@tonic-gate 				 * v2:  we're not holding the lock; it's
18990Sstevel@tonic-gate 				 * possible that the driver could have already
19000Sstevel@tonic-gate 				 * called gld_sched (following up on its
19010Sstevel@tonic-gate 				 * return of GLD_NORESOURCES), before we got a
19020Sstevel@tonic-gate 				 * chance to do the putbq() and set gld_xwait.
19030Sstevel@tonic-gate 				 * So if we saw a call to gld_sched that
19040Sstevel@tonic-gate 				 * examined this queue, since our call to
19050Sstevel@tonic-gate 				 * gld_start() above, then it's possible we've
19060Sstevel@tonic-gate 				 * already seen the only call to gld_sched()
19070Sstevel@tonic-gate 				 * we're ever going to see.  So we better retry
19080Sstevel@tonic-gate 				 * transmitting this packet right now.
19090Sstevel@tonic-gate 				 */
19100Sstevel@tonic-gate 				if (gld->gld_sched_ran) {
19110Sstevel@tonic-gate #ifdef GLD_DEBUG
19120Sstevel@tonic-gate 					if (gld_debug & GLDTRACE)
19130Sstevel@tonic-gate 						cmn_err(CE_NOTE, "gld_wsrv: "
19140Sstevel@tonic-gate 						    "sched was called");
19150Sstevel@tonic-gate #endif
19160Sstevel@tonic-gate 					break;	/* try again right now */
19170Sstevel@tonic-gate 				}
19180Sstevel@tonic-gate 				gld->gld_in_wsrv = B_FALSE;
19190Sstevel@tonic-gate 				return (0);
19200Sstevel@tonic-gate 			}
19210Sstevel@tonic-gate 			break;
19220Sstevel@tonic-gate 
19230Sstevel@tonic-gate 		case M_IOCTL:
19240Sstevel@tonic-gate 			(void) gld_ioctl(q, mp);
19250Sstevel@tonic-gate 			break;
19260Sstevel@tonic-gate 
19270Sstevel@tonic-gate 		case M_CTL:
19280Sstevel@tonic-gate 			if (macinfo == NULL) {
19290Sstevel@tonic-gate 				freemsg(mp);
19300Sstevel@tonic-gate 				break;
19310Sstevel@tonic-gate 			}
19320Sstevel@tonic-gate 
19330Sstevel@tonic-gate 			if (macinfo->gldm_mctl != NULL) {
19340Sstevel@tonic-gate 				GLDM_LOCK(macinfo, RW_WRITER);
19350Sstevel@tonic-gate 				(void) (*macinfo->gldm_mctl) (macinfo, q, mp);
19360Sstevel@tonic-gate 				GLDM_UNLOCK(macinfo);
19370Sstevel@tonic-gate 			} else {
19380Sstevel@tonic-gate 				/* This driver doesn't recognize, just drop */
19390Sstevel@tonic-gate 				freemsg(mp);
19400Sstevel@tonic-gate 			}
19410Sstevel@tonic-gate 			break;
19420Sstevel@tonic-gate 
19430Sstevel@tonic-gate 		case M_PROTO:	/* Will be an DLPI message of some type */
19440Sstevel@tonic-gate 		case M_PCPROTO:
19450Sstevel@tonic-gate 			if ((err = gld_cmds(q, mp)) != GLDE_OK) {
19460Sstevel@tonic-gate 				if (err == GLDE_RETRY) {
19470Sstevel@tonic-gate 					gld->gld_in_wsrv = B_FALSE;
19480Sstevel@tonic-gate 					return (0); /* quit while we're ahead */
19490Sstevel@tonic-gate 				}
19500Sstevel@tonic-gate 				prim = (union DL_primitives *)mp->b_rptr;
19510Sstevel@tonic-gate 				dlerrorack(q, mp, prim->dl_primitive, err, 0);
19520Sstevel@tonic-gate 			}
19530Sstevel@tonic-gate 			break;
19540Sstevel@tonic-gate 
19550Sstevel@tonic-gate 		default:
19560Sstevel@tonic-gate 			/* This should never happen */
19570Sstevel@tonic-gate #ifdef GLD_DEBUG
19580Sstevel@tonic-gate 			if (gld_debug & GLDERRS)
19590Sstevel@tonic-gate 				cmn_err(CE_WARN,
19600Sstevel@tonic-gate 				    "gld_wsrv: db_type(%x) not supported",
19610Sstevel@tonic-gate 				    mp->b_datap->db_type);
19620Sstevel@tonic-gate #endif
19630Sstevel@tonic-gate 			freemsg(mp);	/* unknown types are discarded */
19640Sstevel@tonic-gate 			break;
19650Sstevel@tonic-gate 		}
19660Sstevel@tonic-gate 	}
19670Sstevel@tonic-gate 
19680Sstevel@tonic-gate 	membar_exit();
19690Sstevel@tonic-gate 	gld->gld_in_wsrv = B_FALSE;
19700Sstevel@tonic-gate 	return (0);
19710Sstevel@tonic-gate }
19720Sstevel@tonic-gate 
19730Sstevel@tonic-gate /*
19740Sstevel@tonic-gate  * gld_start() can get called from gld_wput(), gld_wsrv(), or gld_unitdata().
19750Sstevel@tonic-gate  *
19760Sstevel@tonic-gate  * We only come directly from wput() in the GLD_FAST (fastpath) or RAW case.
19770Sstevel@tonic-gate  *
19780Sstevel@tonic-gate  * In particular, we must avoid calling gld_precv*() if we came from wput().
19790Sstevel@tonic-gate  * gld_precv*() is where we, on the transmit side, loop back our outgoing
19800Sstevel@tonic-gate  * packets to the receive side if we are in physical promiscuous mode.
19810Sstevel@tonic-gate  * Since the receive side holds a lock across its call to the upstream
19820Sstevel@tonic-gate  * putnext, and that upstream module could well have looped back to our
19830Sstevel@tonic-gate  * wput() routine on the same thread, we cannot call gld_precv* from here
19840Sstevel@tonic-gate  * for fear of causing a recursive lock entry in our receive code.
19850Sstevel@tonic-gate  *
19860Sstevel@tonic-gate  * There is a problem here when coming from gld_wput().  While wput
19870Sstevel@tonic-gate  * only comes here if the queue is attached to a PPA and bound to a SAP
19880Sstevel@tonic-gate  * and there are no messages on the queue ahead of the M_DATA that could
19890Sstevel@tonic-gate  * change that, it is theoretically possible that another thread could
19900Sstevel@tonic-gate  * now wput a DL_UNBIND and a DL_DETACH message, and the wsrv() routine
19910Sstevel@tonic-gate  * could wake up and process them, before we finish processing this
19920Sstevel@tonic-gate  * send of the M_DATA.  This can only possibly happen on a Style 2 RAW or
19930Sstevel@tonic-gate  * FAST (fastpath) stream:  non RAW/FAST streams always go through wsrv(),
19940Sstevel@tonic-gate  * and Style 1 streams only DL_DETACH in the close routine, where
19950Sstevel@tonic-gate  * qprocsoff() protects us.  If this happens we could end up calling
19960Sstevel@tonic-gate  * gldm_send() after we have detached the stream and possibly called
19970Sstevel@tonic-gate  * gldm_stop().  Worse, once the number of attached streams goes to zero,
19980Sstevel@tonic-gate  * detach/unregister could be called, and the macinfo could go away entirely.
19990Sstevel@tonic-gate  *
20000Sstevel@tonic-gate  * No one has ever seen this happen.
20010Sstevel@tonic-gate  *
20020Sstevel@tonic-gate  * It is some trouble to fix this, and we would rather not add any mutex
20030Sstevel@tonic-gate  * logic into the wput() routine, which is supposed to be a "fast"
20040Sstevel@tonic-gate  * path.
20050Sstevel@tonic-gate  *
20060Sstevel@tonic-gate  * What I've done is use an atomic counter to keep a count of the number
20070Sstevel@tonic-gate  * of threads currently calling gld_start() from wput() on this stream.
20080Sstevel@tonic-gate  * If DL_DETACH sees this as nonzero, it putbqs the request back onto
20090Sstevel@tonic-gate  * the queue and qenables, hoping to have better luck next time.  Since
20100Sstevel@tonic-gate  * people shouldn't be trying to send after they've asked to DL_DETACH,
20110Sstevel@tonic-gate  * hopefully very soon all the wput=>start threads should have returned
20120Sstevel@tonic-gate  * and the DL_DETACH will succeed.  It's hard to test this since the odds
20130Sstevel@tonic-gate  * of the failure even trying to happen are so small.  I probably could
20140Sstevel@tonic-gate  * have ignored the whole issue and never been the worse for it.
2015*2760Sdg199075  *
2016*2760Sdg199075  * Because some GLDv2 Ethernet drivers do not allow the size of transmitted
2017*2760Sdg199075  * packet to be greater than ETHERMAX, we must first strip the VLAN tag
2018*2760Sdg199075  * from a tagged packet before passing it to the driver's gld_send() entry
2019*2760Sdg199075  * point function, and pass the VLAN tag as a separate argument. The
2020*2760Sdg199075  * gld_send() function may fail. In that case, the packet will need to be
2021*2760Sdg199075  * queued in order to be processed again in GLD's service routine. As the
2022*2760Sdg199075  * VTAG has already been stripped at that time, we save the VTAG information
2023*2760Sdg199075  * in (the unused fields of) dblk using GLD_SAVE_MBLK_VTAG(), so that the
2024*2760Sdg199075  * VTAG can also be queued and be able to be got when gld_start() is called
2025*2760Sdg199075  * next time from gld_wsrv().
2026*2760Sdg199075  *
2027*2760Sdg199075  * Some rules to use GLD_{CLEAR|SAVE}_MBLK_VTAG macros:
2028*2760Sdg199075  *
2029*2760Sdg199075  * - GLD_SAVE_MBLK_VTAG() must be called to save the VTAG information each time
2030*2760Sdg199075  *   the message is queued by putbq().
2031*2760Sdg199075  *
2032*2760Sdg199075  * - GLD_CLEAR_MBLK_VTAG() must be called to clear the bogus VTAG information
2033*2760Sdg199075  *   (if any) in dblk before the message is passed to the gld_start() function.
20340Sstevel@tonic-gate  */
20350Sstevel@tonic-gate static int
20360Sstevel@tonic-gate gld_start(queue_t *q, mblk_t *mp, int caller, uint32_t upri)
20370Sstevel@tonic-gate {
20380Sstevel@tonic-gate 	mblk_t *nmp;
20390Sstevel@tonic-gate 	gld_t *gld = (gld_t *)q->q_ptr;
20400Sstevel@tonic-gate 	gld_mac_info_t *macinfo;
20410Sstevel@tonic-gate 	gld_mac_pvt_t *mac_pvt;
20420Sstevel@tonic-gate 	int rc;
20430Sstevel@tonic-gate 	gld_interface_t *ifp;
20440Sstevel@tonic-gate 	pktinfo_t pktinfo;
2045*2760Sdg199075 	uint32_t vtag, vid;
2046*2760Sdg199075 	uint32_t raw_vtag = 0;
20470Sstevel@tonic-gate 	gld_vlan_t *vlan;
2048*2760Sdg199075 	struct gld_stats *stats0, *stats = NULL;
20490Sstevel@tonic-gate 
20500Sstevel@tonic-gate 	ASSERT(DB_TYPE(mp) == M_DATA);
20510Sstevel@tonic-gate 	macinfo = gld->gld_mac_info;
20520Sstevel@tonic-gate 	mac_pvt = (gld_mac_pvt_t *)macinfo->gldm_mac_pvt;
20530Sstevel@tonic-gate 	ifp = mac_pvt->interfacep;
20540Sstevel@tonic-gate 	vlan = (gld_vlan_t *)gld->gld_vlan;
2055*2760Sdg199075 	vid = vlan->gldv_id;
2056*2760Sdg199075 
2057*2760Sdg199075 	/*
2058*2760Sdg199075 	 * If this interface is a VLAN, the kstats of corresponding
2059*2760Sdg199075 	 * "VLAN 0" should also be updated. Note that the gld_vlan_t
2060*2760Sdg199075 	 * structure for VLAN 0 might not exist if there are no DLPI
2061*2760Sdg199075 	 * consumers attaching on VLAN 0. Fortunately we can directly
2062*2760Sdg199075 	 * access VLAN 0's kstats from macinfo.
2063*2760Sdg199075 	 *
2064*2760Sdg199075 	 * Therefore, stats0 (VLAN 0's kstats) must always be
2065*2760Sdg199075 	 * updated, and stats must to be updated if it is not NULL.
2066*2760Sdg199075 	 */
2067*2760Sdg199075 	stats0 = mac_pvt->statistics;
2068*2760Sdg199075 	if (vid != VLAN_VID_NONE)
2069*2760Sdg199075 		stats = vlan->gldv_stats;
20700Sstevel@tonic-gate 
20710Sstevel@tonic-gate 	if ((*ifp->interpreter)(macinfo, mp, &pktinfo, GLD_TX) != 0) {
20720Sstevel@tonic-gate #ifdef GLD_DEBUG
20730Sstevel@tonic-gate 		if (gld_debug & GLDERRS)
20740Sstevel@tonic-gate 			cmn_err(CE_WARN,
20750Sstevel@tonic-gate 			    "gld_start: failed to interpret outbound packet");
20760Sstevel@tonic-gate #endif
2077*2760Sdg199075 		goto badarg;
2078*2760Sdg199075 	}
2079*2760Sdg199075 
2080*2760Sdg199075 	vtag = VLAN_VID_NONE;
2081*2760Sdg199075 	raw_vtag = GLD_GET_MBLK_VTAG(mp);
2082*2760Sdg199075 	if (GLD_VTAG_TCI(raw_vtag) != 0) {
2083*2760Sdg199075 		uint16_t raw_pri, raw_vid, evid;
2084*2760Sdg199075 
2085*2760Sdg199075 		/*
2086*2760Sdg199075 		 * Tagged packet.
2087*2760Sdg199075 		 */
2088*2760Sdg199075 		raw_pri = GLD_VTAG_PRI(raw_vtag);
2089*2760Sdg199075 		raw_vid = GLD_VTAG_VID(raw_vtag);
2090*2760Sdg199075 		GLD_CLEAR_MBLK_VTAG(mp);
2091*2760Sdg199075 
2092*2760Sdg199075 		if (gld->gld_flags & GLD_RAW) {
2093*2760Sdg199075 			/*
2094*2760Sdg199075 			 * In raw mode, we only expect untagged packets or
2095*2760Sdg199075 			 * special priority-tagged packets on a VLAN stream.
2096*2760Sdg199075 			 * Drop the packet if its VID is not zero.
2097*2760Sdg199075 			 */
2098*2760Sdg199075 			if (vid != VLAN_VID_NONE && raw_vid != VLAN_VID_NONE)
2099*2760Sdg199075 				goto badarg;
2100*2760Sdg199075 
2101*2760Sdg199075 			/*
2102*2760Sdg199075 			 * If it is raw mode, use the per-stream priority if
2103*2760Sdg199075 			 * the priority is not specified in the packet.
2104*2760Sdg199075 			 * Otherwise, ignore the priority bits in the packet.
2105*2760Sdg199075 			 */
2106*2760Sdg199075 			upri = (raw_pri != 0) ? raw_pri : upri;
2107*2760Sdg199075 		}
2108*2760Sdg199075 
2109*2760Sdg199075 		if (vid == VLAN_VID_NONE && vid != raw_vid) {
2110*2760Sdg199075 			gld_vlan_t *tmp_vlan;
2111*2760Sdg199075 
2112*2760Sdg199075 			/*
2113*2760Sdg199075 			 * This link is a physical link but the packet is
2114*2760Sdg199075 			 * a VLAN tagged packet, the kstats of corresponding
2115*2760Sdg199075 			 * VLAN (if any) should also be updated.
2116*2760Sdg199075 			 */
2117*2760Sdg199075 			tmp_vlan = gld_find_vlan(macinfo, raw_vid);
2118*2760Sdg199075 			if (tmp_vlan != NULL)
2119*2760Sdg199075 				stats = tmp_vlan->gldv_stats;
2120*2760Sdg199075 		}
2121*2760Sdg199075 
2122*2760Sdg199075 		evid = (vid == VLAN_VID_NONE) ? raw_vid : vid;
2123*2760Sdg199075 		if (evid != VLAN_VID_NONE || upri != 0)
2124*2760Sdg199075 			vtag = GLD_MAKE_VTAG(upri, VLAN_CFI_ETHER, evid);
2125*2760Sdg199075 	} else {
2126*2760Sdg199075 		/*
2127*2760Sdg199075 		 * Untagged packet:
2128*2760Sdg199075 		 * Get vtag from the attached PPA of this stream.
2129*2760Sdg199075 		 */
2130*2760Sdg199075 		if ((vid != VLAN_VID_NONE) ||
2131*2760Sdg199075 		    ((macinfo->gldm_type == DL_ETHER) && (upri != 0))) {
2132*2760Sdg199075 			vtag = GLD_MAKE_VTAG(upri, VLAN_CFI_ETHER, vid);
2133*2760Sdg199075 		}
21340Sstevel@tonic-gate 	}
21350Sstevel@tonic-gate 
21360Sstevel@tonic-gate 	/*
21370Sstevel@tonic-gate 	 * We're not holding the lock for this check.  If the promiscuous
21380Sstevel@tonic-gate 	 * state is in flux it doesn't matter much if we get this wrong.
21390Sstevel@tonic-gate 	 */
21400Sstevel@tonic-gate 	if (mac_pvt->nprom > 0) {
21410Sstevel@tonic-gate 		/*
21420Sstevel@tonic-gate 		 * We want to loopback to the receive side, but to avoid
21430Sstevel@tonic-gate 		 * recursive lock entry:  if we came from wput(), which
21440Sstevel@tonic-gate 		 * could have looped back via IP from our own receive
21450Sstevel@tonic-gate 		 * interrupt thread, we decline this request.  wput()
21460Sstevel@tonic-gate 		 * will then queue the packet for wsrv().  This means
21470Sstevel@tonic-gate 		 * that when snoop is running we don't get the advantage
21480Sstevel@tonic-gate 		 * of the wput() multithreaded direct entry to the
21490Sstevel@tonic-gate 		 * driver's send routine.
21500Sstevel@tonic-gate 		 */
21510Sstevel@tonic-gate 		if (caller == GLD_WPUT) {
2152*2760Sdg199075 			GLD_SAVE_MBLK_VTAG(mp, raw_vtag);
21530Sstevel@tonic-gate 			(void) putbq(q, mp);
21540Sstevel@tonic-gate 			return (GLD_NORESOURCES);
21550Sstevel@tonic-gate 		}
21560Sstevel@tonic-gate 		if (macinfo->gldm_capabilities & GLD_CAP_ZEROCOPY)
21570Sstevel@tonic-gate 			nmp = dupmsg_noloan(mp);
21580Sstevel@tonic-gate 		else
21590Sstevel@tonic-gate 			nmp = dupmsg(mp);
21600Sstevel@tonic-gate 	} else
21610Sstevel@tonic-gate 		nmp = NULL;		/* we need no loopback */
21620Sstevel@tonic-gate 
21630Sstevel@tonic-gate 	if (ifp->hdr_size > 0 &&
21640Sstevel@tonic-gate 	    pktinfo.pktLen > ifp->hdr_size + (vtag == 0 ? 0 : VTAG_SIZE) +
21650Sstevel@tonic-gate 	    macinfo->gldm_maxpkt) {
21660Sstevel@tonic-gate 		if (nmp)
21670Sstevel@tonic-gate 			freemsg(nmp);	/* free the duped message */
21680Sstevel@tonic-gate #ifdef GLD_DEBUG
21690Sstevel@tonic-gate 		if (gld_debug & GLDERRS)
21700Sstevel@tonic-gate 			cmn_err(CE_WARN,
21710Sstevel@tonic-gate 			    "gld_start: oversize outbound packet, size %d,"
21720Sstevel@tonic-gate 			    "max %d", pktinfo.pktLen,
2173*2760Sdg199075 			    ifp->hdr_size + (vtag == 0 ? 0 : VTAG_SIZE) +
2174*2760Sdg199075 			    macinfo->gldm_maxpkt);
21750Sstevel@tonic-gate #endif
2176*2760Sdg199075 		goto badarg;
21770Sstevel@tonic-gate 	}
21780Sstevel@tonic-gate 
21790Sstevel@tonic-gate 	rc = (*gld->gld_send)(macinfo, mp, vtag);
21800Sstevel@tonic-gate 
21810Sstevel@tonic-gate 	if (rc != GLD_SUCCESS) {
21820Sstevel@tonic-gate 		if (rc == GLD_NORESOURCES) {
2183*2760Sdg199075 			ATOMIC_BUMP(stats0, stats, glds_xmtretry, 1);
2184*2760Sdg199075 			GLD_SAVE_MBLK_VTAG(mp, raw_vtag);
21850Sstevel@tonic-gate 			(void) putbq(q, mp);
21860Sstevel@tonic-gate 		} else {
21870Sstevel@tonic-gate 			/* transmit error; drop the packet */
21880Sstevel@tonic-gate 			freemsg(mp);
21890Sstevel@tonic-gate 			/* We're supposed to count failed attempts as well */
2190*2760Sdg199075 			UPDATE_STATS(stats0, stats, pktinfo, 1);
21910Sstevel@tonic-gate #ifdef GLD_DEBUG
21920Sstevel@tonic-gate 			if (gld_debug & GLDERRS)
21930Sstevel@tonic-gate 				cmn_err(CE_WARN,
21940Sstevel@tonic-gate 				    "gld_start: gldm_send failed %d", rc);
21950Sstevel@tonic-gate #endif
21960Sstevel@tonic-gate 		}
21970Sstevel@tonic-gate 		if (nmp)
21980Sstevel@tonic-gate 			freemsg(nmp);	/* free the dupped message */
21990Sstevel@tonic-gate 		return (rc);
22000Sstevel@tonic-gate 	}
22010Sstevel@tonic-gate 
2202*2760Sdg199075 	UPDATE_STATS(stats0, stats, pktinfo, 1);
22030Sstevel@tonic-gate 
22040Sstevel@tonic-gate 	/*
22050Sstevel@tonic-gate 	 * Loopback case. The message needs to be returned back on
2206*2760Sdg199075 	 * the read side. This would silently fail if the dupmsg fails
22070Sstevel@tonic-gate 	 * above. This is probably OK, if there is no memory to dup the
22080Sstevel@tonic-gate 	 * block, then there isn't much we could do anyway.
22090Sstevel@tonic-gate 	 */
22100Sstevel@tonic-gate 	if (nmp) {
22110Sstevel@tonic-gate 		GLDM_LOCK(macinfo, RW_WRITER);
2212*2760Sdg199075 		gld_precv(macinfo, nmp, vtag, stats);
22130Sstevel@tonic-gate 		GLDM_UNLOCK(macinfo);
22140Sstevel@tonic-gate 	}
22150Sstevel@tonic-gate 
22160Sstevel@tonic-gate 	return (GLD_SUCCESS);
2217*2760Sdg199075 
2218*2760Sdg199075 badarg:
2219*2760Sdg199075 	freemsg(mp);
2220*2760Sdg199075 
2221*2760Sdg199075 	ATOMIC_BUMP(stats0, stats, glds_xmtbadinterp, 1);
2222*2760Sdg199075 	return (GLD_BADARG);
22230Sstevel@tonic-gate }
22240Sstevel@tonic-gate 
22250Sstevel@tonic-gate /*
22260Sstevel@tonic-gate  * With MDT V.2 a single message mp can have one header area and multiple
22270Sstevel@tonic-gate  * payload areas. A packet is described by dl_pkt_info, and each packet can
22280Sstevel@tonic-gate  * span multiple payload areas (currently with TCP, each packet will have one
22290Sstevel@tonic-gate  * header and at the most two payload areas). MACs might have a limit on the
22300Sstevel@tonic-gate  * number of payload segments (i.e. per packet scatter-gather limit), and
22310Sstevel@tonic-gate  * MDT V.2 has a way of specifying that with mdt_span_limit; the MAC driver
22320Sstevel@tonic-gate  * might also have a limit on the total number of payloads in a message, and
22330Sstevel@tonic-gate  * that is specified by mdt_max_pld.
22340Sstevel@tonic-gate  */
22350Sstevel@tonic-gate static int
22360Sstevel@tonic-gate gld_start_mdt(queue_t *q, mblk_t *mp, int caller)
22370Sstevel@tonic-gate {
22380Sstevel@tonic-gate 	mblk_t *nextmp;
22390Sstevel@tonic-gate 	gld_t *gld = (gld_t *)q->q_ptr;
22400Sstevel@tonic-gate 	gld_mac_info_t *macinfo = gld->gld_mac_info;
22410Sstevel@tonic-gate 	gld_mac_pvt_t *mac_pvt = (gld_mac_pvt_t *)macinfo->gldm_mac_pvt;
22420Sstevel@tonic-gate 	int numpacks, mdtpacks;
22430Sstevel@tonic-gate 	gld_interface_t *ifp = mac_pvt->interfacep;
22440Sstevel@tonic-gate 	pktinfo_t pktinfo;
22450Sstevel@tonic-gate 	gld_vlan_t *vlan = (gld_vlan_t *)gld->gld_vlan;
22460Sstevel@tonic-gate 	boolean_t doloop = B_FALSE;
22470Sstevel@tonic-gate 	multidata_t *dlmdp;
22480Sstevel@tonic-gate 	pdescinfo_t pinfo;
22490Sstevel@tonic-gate 	pdesc_t *dl_pkt;
22500Sstevel@tonic-gate 	void *cookie;
22510Sstevel@tonic-gate 	uint_t totLen = 0;
22520Sstevel@tonic-gate 
22530Sstevel@tonic-gate 	ASSERT(DB_TYPE(mp) == M_MULTIDATA);
22540Sstevel@tonic-gate 
22550Sstevel@tonic-gate 	/*
22560Sstevel@tonic-gate 	 * We're not holding the lock for this check.  If the promiscuous
22570Sstevel@tonic-gate 	 * state is in flux it doesn't matter much if we get this wrong.
22580Sstevel@tonic-gate 	 */
22590Sstevel@tonic-gate 	if (mac_pvt->nprom > 0) {
22600Sstevel@tonic-gate 		/*
22610Sstevel@tonic-gate 		 * We want to loopback to the receive side, but to avoid
22620Sstevel@tonic-gate 		 * recursive lock entry:  if we came from wput(), which
22630Sstevel@tonic-gate 		 * could have looped back via IP from our own receive
22640Sstevel@tonic-gate 		 * interrupt thread, we decline this request.  wput()
22650Sstevel@tonic-gate 		 * will then queue the packet for wsrv().  This means
22660Sstevel@tonic-gate 		 * that when snoop is running we don't get the advantage
22670Sstevel@tonic-gate 		 * of the wput() multithreaded direct entry to the
22680Sstevel@tonic-gate 		 * driver's send routine.
22690Sstevel@tonic-gate 		 */
22700Sstevel@tonic-gate 		if (caller == GLD_WPUT) {
22710Sstevel@tonic-gate 			(void) putbq(q, mp);
22720Sstevel@tonic-gate 			return (GLD_NORESOURCES);
22730Sstevel@tonic-gate 		}
22740Sstevel@tonic-gate 		doloop = B_TRUE;
22750Sstevel@tonic-gate 
22760Sstevel@tonic-gate 		/*
22770Sstevel@tonic-gate 		 * unlike the M_DATA case, we don't have to call
22780Sstevel@tonic-gate 		 * dupmsg_noloan here because mmd_transform
22790Sstevel@tonic-gate 		 * (called by gld_precv_mdt) will make a copy of
22800Sstevel@tonic-gate 		 * each dblk.
22810Sstevel@tonic-gate 		 */
22820Sstevel@tonic-gate 	}
22830Sstevel@tonic-gate 
22840Sstevel@tonic-gate 	while (mp != NULL) {
22850Sstevel@tonic-gate 		/*
22860Sstevel@tonic-gate 		 * The lower layer driver only gets a single multidata
22870Sstevel@tonic-gate 		 * message; this also makes it easier to handle noresources.
22880Sstevel@tonic-gate 		 */
22890Sstevel@tonic-gate 		nextmp = mp->b_cont;
22900Sstevel@tonic-gate 		mp->b_cont = NULL;
22910Sstevel@tonic-gate 
22920Sstevel@tonic-gate 		/*
22930Sstevel@tonic-gate 		 * Get number of packets in this message; if nothing
22940Sstevel@tonic-gate 		 * to transmit, go to next message.
22950Sstevel@tonic-gate 		 */
22960Sstevel@tonic-gate 		dlmdp = mmd_getmultidata(mp);
22970Sstevel@tonic-gate 		if ((mdtpacks = (int)mmd_getcnt(dlmdp, NULL, NULL)) == 0) {
22980Sstevel@tonic-gate 			freemsg(mp);
22990Sstevel@tonic-gate 			mp = nextmp;
23000Sstevel@tonic-gate 			continue;
23010Sstevel@tonic-gate 		}
23020Sstevel@tonic-gate 
23030Sstevel@tonic-gate 		/*
23040Sstevel@tonic-gate 		 * Run interpreter to populate media specific pktinfo fields.
23050Sstevel@tonic-gate 		 * This collects per MDT message information like sap,
23060Sstevel@tonic-gate 		 * broad/multicast etc.
23070Sstevel@tonic-gate 		 */
23080Sstevel@tonic-gate 		(void) (*ifp->interpreter_mdt)(macinfo, mp, NULL, &pktinfo,
23090Sstevel@tonic-gate 		    GLD_MDT_TX);
23100Sstevel@tonic-gate 
23110Sstevel@tonic-gate 		numpacks = (*macinfo->gldm_mdt_pre)(macinfo, mp, &cookie);
23120Sstevel@tonic-gate 
23130Sstevel@tonic-gate 		if (numpacks > 0) {
23140Sstevel@tonic-gate 			/*
23150Sstevel@tonic-gate 			 * Driver indicates it can transmit at least 1, and
23160Sstevel@tonic-gate 			 * possibly all, packets in MDT message.
23170Sstevel@tonic-gate 			 */
23180Sstevel@tonic-gate 			int count = numpacks;
23190Sstevel@tonic-gate 
23200Sstevel@tonic-gate 			for (dl_pkt = mmd_getfirstpdesc(dlmdp, &pinfo);
23210Sstevel@tonic-gate 			    (dl_pkt != NULL);
23220Sstevel@tonic-gate 			    dl_pkt = mmd_getnextpdesc(dl_pkt, &pinfo)) {
23230Sstevel@tonic-gate 				/*
23240Sstevel@tonic-gate 				 * Format this packet by adding link header and
23250Sstevel@tonic-gate 				 * adjusting pdescinfo to include it; get
23260Sstevel@tonic-gate 				 * packet length.
23270Sstevel@tonic-gate 				 */
23280Sstevel@tonic-gate 				(void) (*ifp->interpreter_mdt)(macinfo, NULL,
23290Sstevel@tonic-gate 				    &pinfo, &pktinfo, GLD_MDT_TXPKT);
23300Sstevel@tonic-gate 
23310Sstevel@tonic-gate 				totLen += pktinfo.pktLen;
23320Sstevel@tonic-gate 
23330Sstevel@tonic-gate 				/*
23340Sstevel@tonic-gate 				 * Loop back packet before handing to the
23350Sstevel@tonic-gate 				 * driver.
23360Sstevel@tonic-gate 				 */
23370Sstevel@tonic-gate 				if (doloop &&
23380Sstevel@tonic-gate 				    mmd_adjpdesc(dl_pkt, &pinfo) != NULL) {
23390Sstevel@tonic-gate 					GLDM_LOCK(macinfo, RW_WRITER);
23400Sstevel@tonic-gate 					gld_precv_mdt(macinfo, vlan, mp,
23410Sstevel@tonic-gate 					    dl_pkt, &pktinfo);
23420Sstevel@tonic-gate 					GLDM_UNLOCK(macinfo);
23430Sstevel@tonic-gate 				}
23440Sstevel@tonic-gate 
23450Sstevel@tonic-gate 				/*
23460Sstevel@tonic-gate 				 * And send off to driver.
23470Sstevel@tonic-gate 				 */
23480Sstevel@tonic-gate 				(*macinfo->gldm_mdt_send)(macinfo, cookie,
23490Sstevel@tonic-gate 				    &pinfo);
23500Sstevel@tonic-gate 
23510Sstevel@tonic-gate 				/*
23520Sstevel@tonic-gate 				 * Be careful not to invoke getnextpdesc if we
23530Sstevel@tonic-gate 				 * already sent the last packet, since driver
23540Sstevel@tonic-gate 				 * might have posted it to hardware causing a
23550Sstevel@tonic-gate 				 * completion and freemsg() so the MDT data
23560Sstevel@tonic-gate 				 * structures might not be valid anymore.
23570Sstevel@tonic-gate 				 */
23580Sstevel@tonic-gate 				if (--count == 0)
23590Sstevel@tonic-gate 					break;
23600Sstevel@tonic-gate 			}
23610Sstevel@tonic-gate 			(*macinfo->gldm_mdt_post)(macinfo, mp, cookie);
23620Sstevel@tonic-gate 			pktinfo.pktLen = totLen;
2363*2760Sdg199075 			UPDATE_STATS(vlan->gldv_stats, NULL, pktinfo, numpacks);
23640Sstevel@tonic-gate 
23650Sstevel@tonic-gate 			/*
23660Sstevel@tonic-gate 			 * In the noresources case (when driver indicates it
23670Sstevel@tonic-gate 			 * can not transmit all packets in the MDT message),
23680Sstevel@tonic-gate 			 * adjust to skip the first few packets on retrial.
23690Sstevel@tonic-gate 			 */
23700Sstevel@tonic-gate 			if (numpacks != mdtpacks) {
23710Sstevel@tonic-gate 				/*
23720Sstevel@tonic-gate 				 * Release already processed packet descriptors.
23730Sstevel@tonic-gate 				 */
23740Sstevel@tonic-gate 				for (count = 0; count < numpacks; count++) {
23750Sstevel@tonic-gate 					dl_pkt = mmd_getfirstpdesc(dlmdp,
23760Sstevel@tonic-gate 					    &pinfo);
23770Sstevel@tonic-gate 					mmd_rempdesc(dl_pkt);
23780Sstevel@tonic-gate 				}
2379*2760Sdg199075 				ATOMIC_BUMP(vlan->gldv_stats, NULL,
2380*2760Sdg199075 				    glds_xmtretry, 1);
23810Sstevel@tonic-gate 				mp->b_cont = nextmp;
23820Sstevel@tonic-gate 				(void) putbq(q, mp);
23830Sstevel@tonic-gate 				return (GLD_NORESOURCES);
23840Sstevel@tonic-gate 			}
23850Sstevel@tonic-gate 		} else if (numpacks == 0) {
23860Sstevel@tonic-gate 			/*
23870Sstevel@tonic-gate 			 * Driver indicates it can not transmit any packets
23880Sstevel@tonic-gate 			 * currently and will request retrial later.
23890Sstevel@tonic-gate 			 */
2390*2760Sdg199075 			ATOMIC_BUMP(vlan->gldv_stats, NULL, glds_xmtretry, 1);
23910Sstevel@tonic-gate 			mp->b_cont = nextmp;
23920Sstevel@tonic-gate 			(void) putbq(q, mp);
23930Sstevel@tonic-gate 			return (GLD_NORESOURCES);
23940Sstevel@tonic-gate 		} else {
23950Sstevel@tonic-gate 			ASSERT(numpacks == -1);
23960Sstevel@tonic-gate 			/*
23970Sstevel@tonic-gate 			 * We're supposed to count failed attempts as well.
23980Sstevel@tonic-gate 			 */
23990Sstevel@tonic-gate 			dl_pkt = mmd_getfirstpdesc(dlmdp, &pinfo);
24000Sstevel@tonic-gate 			while (dl_pkt != NULL) {
24010Sstevel@tonic-gate 				/*
24020Sstevel@tonic-gate 				 * Call interpreter to determine total packet
24030Sstevel@tonic-gate 				 * bytes that are being dropped.
24040Sstevel@tonic-gate 				 */
24050Sstevel@tonic-gate 				(void) (*ifp->interpreter_mdt)(macinfo, NULL,
24060Sstevel@tonic-gate 				    &pinfo, &pktinfo, GLD_MDT_TXPKT);
24070Sstevel@tonic-gate 
24080Sstevel@tonic-gate 				totLen += pktinfo.pktLen;
24090Sstevel@tonic-gate 
24100Sstevel@tonic-gate 				dl_pkt = mmd_getnextpdesc(dl_pkt, &pinfo);
24110Sstevel@tonic-gate 			}
24120Sstevel@tonic-gate 			pktinfo.pktLen = totLen;
2413*2760Sdg199075 			UPDATE_STATS(vlan->gldv_stats, NULL, pktinfo, mdtpacks);
24140Sstevel@tonic-gate 
24150Sstevel@tonic-gate 			/*
24160Sstevel@tonic-gate 			 * Transmit error; drop the message, move on
24170Sstevel@tonic-gate 			 * to the next one.
24180Sstevel@tonic-gate 			 */
24190Sstevel@tonic-gate 			freemsg(mp);
24200Sstevel@tonic-gate 		}
24210Sstevel@tonic-gate 
24220Sstevel@tonic-gate 		/*
24230Sstevel@tonic-gate 		 * Process the next multidata block, if there is one.
24240Sstevel@tonic-gate 		 */
24250Sstevel@tonic-gate 		mp = nextmp;
24260Sstevel@tonic-gate 	}
24270Sstevel@tonic-gate 
24280Sstevel@tonic-gate 	return (GLD_SUCCESS);
24290Sstevel@tonic-gate }
24300Sstevel@tonic-gate 
24310Sstevel@tonic-gate /*
24320Sstevel@tonic-gate  * gld_intr (macinfo)
24330Sstevel@tonic-gate  */
24340Sstevel@tonic-gate uint_t
24350Sstevel@tonic-gate gld_intr(gld_mac_info_t *macinfo)
24360Sstevel@tonic-gate {
24370Sstevel@tonic-gate 	ASSERT(macinfo != NULL);
24380Sstevel@tonic-gate 
24390Sstevel@tonic-gate 	if (!(macinfo->gldm_GLD_flags & GLD_MAC_READY))
24400Sstevel@tonic-gate 		return (DDI_INTR_UNCLAIMED);
24410Sstevel@tonic-gate 
24420Sstevel@tonic-gate 	return ((*macinfo->gldm_intr)(macinfo));
24430Sstevel@tonic-gate }
24440Sstevel@tonic-gate 
24450Sstevel@tonic-gate /*
24460Sstevel@tonic-gate  * gld_sched (macinfo)
24470Sstevel@tonic-gate  *
24480Sstevel@tonic-gate  * This routine scans the streams that refer to a specific macinfo
24490Sstevel@tonic-gate  * structure and causes the STREAMS scheduler to try to run them if
24500Sstevel@tonic-gate  * they are marked as waiting for the transmit buffer.
24510Sstevel@tonic-gate  */
24520Sstevel@tonic-gate void
24530Sstevel@tonic-gate gld_sched(gld_mac_info_t *macinfo)
24540Sstevel@tonic-gate {
24550Sstevel@tonic-gate 	gld_mac_pvt_t *mac_pvt;
24560Sstevel@tonic-gate 	gld_t *gld;
24570Sstevel@tonic-gate 	gld_vlan_t *vlan;
24580Sstevel@tonic-gate 	int i;
24590Sstevel@tonic-gate 
24600Sstevel@tonic-gate 	ASSERT(macinfo != NULL);
24610Sstevel@tonic-gate 
24620Sstevel@tonic-gate 	GLDM_LOCK(macinfo, RW_WRITER);
24630Sstevel@tonic-gate 
24640Sstevel@tonic-gate 	if (macinfo->gldm_GLD_flags & GLD_UNREGISTERED) {
24650Sstevel@tonic-gate 		/* We're probably being called from a leftover interrupt */
24660Sstevel@tonic-gate 		GLDM_UNLOCK(macinfo);
24670Sstevel@tonic-gate 		return;
24680Sstevel@tonic-gate 	}
24690Sstevel@tonic-gate 
24700Sstevel@tonic-gate 	mac_pvt = (gld_mac_pvt_t *)macinfo->gldm_mac_pvt;
24710Sstevel@tonic-gate 
24720Sstevel@tonic-gate 	for (i = 0; i < VLAN_HASHSZ; i++) {
24730Sstevel@tonic-gate 		for (vlan = mac_pvt->vlan_hash[i];
24740Sstevel@tonic-gate 		    vlan != NULL; vlan = vlan->gldv_next) {
24750Sstevel@tonic-gate 			for (gld = vlan->gldv_str_next;
24760Sstevel@tonic-gate 			    gld != (gld_t *)&vlan->gldv_str_next;
24770Sstevel@tonic-gate 			    gld = gld->gld_next) {
24780Sstevel@tonic-gate 				ASSERT(gld->gld_mac_info == macinfo);
24790Sstevel@tonic-gate 				gld->gld_sched_ran = B_TRUE;
24800Sstevel@tonic-gate 				membar_enter();
24810Sstevel@tonic-gate 				if (gld->gld_xwait) {
24820Sstevel@tonic-gate 					gld->gld_xwait = B_FALSE;
24830Sstevel@tonic-gate 					qenable(WR(gld->gld_qptr));
24840Sstevel@tonic-gate 				}
24850Sstevel@tonic-gate 			}
24860Sstevel@tonic-gate 		}
24870Sstevel@tonic-gate 	}
24880Sstevel@tonic-gate 
24890Sstevel@tonic-gate 	GLDM_UNLOCK(macinfo);
24900Sstevel@tonic-gate }
24910Sstevel@tonic-gate 
24920Sstevel@tonic-gate /*
2493*2760Sdg199075  * gld_precv (macinfo, mp, vtag, stats)
24940Sstevel@tonic-gate  * called from gld_start to loopback a packet when in promiscuous mode
2495*2760Sdg199075  *
2496*2760Sdg199075  * VLAN 0's statistics need to be updated. If stats is not NULL,
2497*2760Sdg199075  * it needs to be updated as well.
24980Sstevel@tonic-gate  */
24990Sstevel@tonic-gate static void
2500*2760Sdg199075 gld_precv(gld_mac_info_t *macinfo, mblk_t *mp, uint32_t vtag,
2501*2760Sdg199075     struct gld_stats *stats)
25020Sstevel@tonic-gate {
25030Sstevel@tonic-gate 	gld_mac_pvt_t *mac_pvt;
25040Sstevel@tonic-gate 	gld_interface_t *ifp;
25050Sstevel@tonic-gate 	pktinfo_t pktinfo;
25060Sstevel@tonic-gate 
25070Sstevel@tonic-gate 	ASSERT(GLDM_LOCK_HELD_WRITE(macinfo));
25080Sstevel@tonic-gate 
25090Sstevel@tonic-gate 	mac_pvt = (gld_mac_pvt_t *)macinfo->gldm_mac_pvt;
25100Sstevel@tonic-gate 	ifp = mac_pvt->interfacep;
25110Sstevel@tonic-gate 
25120Sstevel@tonic-gate 	/*
25130Sstevel@tonic-gate 	 * call the media specific packet interpreter routine
25140Sstevel@tonic-gate 	 */
25150Sstevel@tonic-gate 	if ((*ifp->interpreter)(macinfo, mp, &pktinfo, GLD_RXLOOP) != 0) {
25160Sstevel@tonic-gate 		freemsg(mp);
2517*2760Sdg199075 		BUMP(mac_pvt->statistics, stats, glds_rcvbadinterp, 1);
25180Sstevel@tonic-gate #ifdef GLD_DEBUG
25190Sstevel@tonic-gate 		if (gld_debug & GLDERRS)
25200Sstevel@tonic-gate 			cmn_err(CE_WARN,
25210Sstevel@tonic-gate 			    "gld_precv: interpreter failed");
25220Sstevel@tonic-gate #endif
25230Sstevel@tonic-gate 		return;
25240Sstevel@tonic-gate 	}
25250Sstevel@tonic-gate 
2526*2760Sdg199075 	/*
2527*2760Sdg199075 	 * Update the vtag information.
2528*2760Sdg199075 	 */
2529*2760Sdg199075 	pktinfo.isTagged = (vtag != VLAN_VID_NONE);
2530*2760Sdg199075 	pktinfo.vid = GLD_VTAG_VID(vtag);
2531*2760Sdg199075 	pktinfo.cfi = GLD_VTAG_CFI(vtag);
2532*2760Sdg199075 	pktinfo.user_pri = GLD_VTAG_PRI(vtag);
2533*2760Sdg199075 
2534*2760Sdg199075 	gld_sendup(macinfo, &pktinfo, mp, gld_paccept);
25350Sstevel@tonic-gate }
25360Sstevel@tonic-gate 
25370Sstevel@tonic-gate /*
2538*2760Sdg199075  * Called from gld_start_mdt to loopback packet(s) when in promiscuous mode.
2539*2760Sdg199075  * Note that 'vlan' is always a physical link, because MDT can only be
2540*2760Sdg199075  * enabled on non-VLAN streams.
25410Sstevel@tonic-gate  */
2542*2760Sdg199075 /*ARGSUSED*/
25430Sstevel@tonic-gate static void
25440Sstevel@tonic-gate gld_precv_mdt(gld_mac_info_t *macinfo, gld_vlan_t *vlan, mblk_t *mp,
25450Sstevel@tonic-gate     pdesc_t *dl_pkt, pktinfo_t *pktinfo)
25460Sstevel@tonic-gate {
25470Sstevel@tonic-gate 	mblk_t *adjmp;
25480Sstevel@tonic-gate 	gld_mac_pvt_t *mac_pvt = (gld_mac_pvt_t *)macinfo->gldm_mac_pvt;
25490Sstevel@tonic-gate 	gld_interface_t *ifp = mac_pvt->interfacep;
25500Sstevel@tonic-gate 
25510Sstevel@tonic-gate 	ASSERT(GLDM_LOCK_HELD_WRITE(macinfo));
25520Sstevel@tonic-gate 
25530Sstevel@tonic-gate 	/*
25540Sstevel@tonic-gate 	 * Get source/destination.
25550Sstevel@tonic-gate 	 */
25560Sstevel@tonic-gate 	(void) (*ifp->interpreter_mdt)(macinfo, mp, NULL, pktinfo,
25570Sstevel@tonic-gate 	    GLD_MDT_RXLOOP);
25580Sstevel@tonic-gate 	if ((adjmp = mmd_transform(dl_pkt)) != NULL)
2559*2760Sdg199075 		gld_sendup(macinfo, pktinfo, adjmp, gld_paccept);
25600Sstevel@tonic-gate }
25610Sstevel@tonic-gate 
25620Sstevel@tonic-gate /*
25630Sstevel@tonic-gate  * gld_recv (macinfo, mp)
25640Sstevel@tonic-gate  * called with an mac-level packet in a mblock; take the maclock,
25650Sstevel@tonic-gate  * try the ip4q and ip6q hack, and otherwise call gld_sendup.
25660Sstevel@tonic-gate  *
25670Sstevel@tonic-gate  * V0 drivers already are holding the mutex when they call us.
25680Sstevel@tonic-gate  */
25690Sstevel@tonic-gate void
25700Sstevel@tonic-gate gld_recv(gld_mac_info_t *macinfo, mblk_t *mp)
25710Sstevel@tonic-gate {
25720Sstevel@tonic-gate 	gld_recv_tagged(macinfo, mp, VLAN_VTAG_NONE);
25730Sstevel@tonic-gate }
25740Sstevel@tonic-gate 
25750Sstevel@tonic-gate void
25760Sstevel@tonic-gate gld_recv_tagged(gld_mac_info_t *macinfo, mblk_t *mp, uint32_t vtag)
25770Sstevel@tonic-gate {
25780Sstevel@tonic-gate 	gld_mac_pvt_t *mac_pvt;
25790Sstevel@tonic-gate 	char pbuf[3*GLD_MAX_ADDRLEN];
25800Sstevel@tonic-gate 	pktinfo_t pktinfo;
25810Sstevel@tonic-gate 	gld_interface_t *ifp;
25820Sstevel@tonic-gate 	queue_t *ipq = NULL;
2583*2760Sdg199075 	gld_vlan_t *vlan = NULL, *vlan0 = NULL, *vlann = NULL;
2584*2760Sdg199075 	struct gld_stats *stats0, *stats = NULL;
25850Sstevel@tonic-gate 	uint32_t vid;
2586*2760Sdg199075 	int err;
25870Sstevel@tonic-gate 
25880Sstevel@tonic-gate 	ASSERT(macinfo != NULL);
25890Sstevel@tonic-gate 	ASSERT(mp->b_datap->db_ref);
25900Sstevel@tonic-gate 
25910Sstevel@tonic-gate 	GLDM_LOCK(macinfo, RW_READER);
25920Sstevel@tonic-gate 
25930Sstevel@tonic-gate 	if (macinfo->gldm_GLD_flags & GLD_UNREGISTERED) {
25940Sstevel@tonic-gate 		/* We're probably being called from a leftover interrupt */
25950Sstevel@tonic-gate 		freemsg(mp);
25960Sstevel@tonic-gate 		goto done;
25970Sstevel@tonic-gate 	}
25980Sstevel@tonic-gate 
2599*2760Sdg199075 	/*
2600*2760Sdg199075 	 * If this packet is a VLAN tagged packet, the kstats of corresponding
2601*2760Sdg199075 	 * "VLAN 0" should also be updated. We can directly access VLAN 0's
2602*2760Sdg199075 	 * kstats from macinfo.
2603*2760Sdg199075 	 *
2604*2760Sdg199075 	 * Further, the packets needs to be passed to VLAN 0 if there is
2605*2760Sdg199075 	 * any DLPI consumer on VLAN 0 who is interested in tagged packets
2606*2760Sdg199075 	 * (DL_PROMISC_SAP is on or is bounded to ETHERTYPE_VLAN SAP).
2607*2760Sdg199075 	 */
2608*2760Sdg199075 	mac_pvt = (gld_mac_pvt_t *)macinfo->gldm_mac_pvt;
2609*2760Sdg199075 	stats0 = mac_pvt->statistics;
2610*2760Sdg199075 
26110Sstevel@tonic-gate 	vid = GLD_VTAG_VID(vtag);
2612*2760Sdg199075 	vlan0 = gld_find_vlan(macinfo, VLAN_VID_NONE);
2613*2760Sdg199075 	if (vid != VLAN_VID_NONE) {
2614*2760Sdg199075 		/*
2615*2760Sdg199075 		 * If there are no physical DLPI consumers interested in the
2616*2760Sdg199075 		 * VLAN packet, clear vlan0.
2617*2760Sdg199075 		 */
2618*2760Sdg199075 		if ((vlan0 != NULL) && (vlan0->gldv_nvlan_sap == 0))
2619*2760Sdg199075 			vlan0 = NULL;
2620*2760Sdg199075 		/*
2621*2760Sdg199075 		 * vlann is the VLAN with the same VID as the VLAN packet.
2622*2760Sdg199075 		 */
2623*2760Sdg199075 		vlann = gld_find_vlan(macinfo, vid);
2624*2760Sdg199075 		if (vlann != NULL)
2625*2760Sdg199075 			stats = vlann->gldv_stats;
2626*2760Sdg199075 	}
2627*2760Sdg199075 
2628*2760Sdg199075 	vlan = (vid == VLAN_VID_NONE) ? vlan0 : vlann;
2629*2760Sdg199075 
2630*2760Sdg199075 	ifp = mac_pvt->interfacep;
2631*2760Sdg199075 	err = (*ifp->interpreter)(macinfo, mp, &pktinfo, GLD_RXQUICK);
2632*2760Sdg199075 
2633*2760Sdg199075 	BUMP(stats0, stats, glds_bytercv64, pktinfo.pktLen);
2634*2760Sdg199075 	BUMP(stats0, stats, glds_pktrcv64, 1);
2635*2760Sdg199075 
2636*2760Sdg199075 	if ((vlann == NULL) && (vlan0 == NULL)) {
26370Sstevel@tonic-gate 		freemsg(mp);
26380Sstevel@tonic-gate 		goto done;
26390Sstevel@tonic-gate 	}
26400Sstevel@tonic-gate 
26410Sstevel@tonic-gate 	/*
2642*2760Sdg199075 	 * Check whether underlying media code supports the IPQ hack:
2643*2760Sdg199075 	 *
2644*2760Sdg199075 	 * - the interpreter could quickly parse the packet
2645*2760Sdg199075 	 * - the device type supports IPQ (ethernet and IPoIB)
2646*2760Sdg199075 	 * - there is one, and only one, IP stream bound (to this VLAN)
2647*2760Sdg199075 	 * - that stream is a "fastpath" stream
2648*2760Sdg199075 	 * - the packet is of type ETHERTYPE_IP or ETHERTYPE_IPV6
2649*2760Sdg199075 	 * - there are no streams in promiscuous mode (on this VLAN)
2650*2760Sdg199075 	 * - if this packet is tagged, there is no need to send this
2651*2760Sdg199075 	 *   packet to physical streams
26520Sstevel@tonic-gate 	 */
2653*2760Sdg199075 	if ((err != 0) && ((vlan != NULL) && (vlan->gldv_nprom == 0)) &&
2654*2760Sdg199075 	    (vlan == vlan0 || vlan0 == NULL)) {
26550Sstevel@tonic-gate 		switch (pktinfo.ethertype) {
26560Sstevel@tonic-gate 		case ETHERTYPE_IP:
26570Sstevel@tonic-gate 			ipq = vlan->gldv_ipq;
26580Sstevel@tonic-gate 			break;
26590Sstevel@tonic-gate 		case ETHERTYPE_IPV6:
26600Sstevel@tonic-gate 			ipq = vlan->gldv_ipv6q;
26610Sstevel@tonic-gate 			break;
26620Sstevel@tonic-gate 		}
26630Sstevel@tonic-gate 	}
26640Sstevel@tonic-gate 
26650Sstevel@tonic-gate 	/*
26660Sstevel@tonic-gate 	 * Special case for IP; we can simply do the putnext here, if:
2667*2760Sdg199075 	 * o The IPQ hack is possible (ipq != NULL).
26680Sstevel@tonic-gate 	 * o the packet is specifically for me, and therefore:
26690Sstevel@tonic-gate 	 * - the packet is not multicast or broadcast (fastpath only
26700Sstevel@tonic-gate 	 *   wants unicast packets).
26710Sstevel@tonic-gate 	 *
26720Sstevel@tonic-gate 	 * o the stream is not asserting flow control.
26730Sstevel@tonic-gate 	 */
26740Sstevel@tonic-gate 	if (ipq != NULL &&
26750Sstevel@tonic-gate 	    pktinfo.isForMe &&
26760Sstevel@tonic-gate 	    canputnext(ipq)) {
26770Sstevel@tonic-gate 		/*
26780Sstevel@tonic-gate 		 * Skip the mac header. We know there is no LLC1/SNAP header
26790Sstevel@tonic-gate 		 * in this packet
26800Sstevel@tonic-gate 		 */
26810Sstevel@tonic-gate 		mp->b_rptr += pktinfo.macLen;
26820Sstevel@tonic-gate 		putnext(ipq, mp);
26830Sstevel@tonic-gate 		goto done;
26840Sstevel@tonic-gate 	}
26850Sstevel@tonic-gate 
26860Sstevel@tonic-gate 	/*
26870Sstevel@tonic-gate 	 * call the media specific packet interpreter routine
26880Sstevel@tonic-gate 	 */
26890Sstevel@tonic-gate 	if ((*ifp->interpreter)(macinfo, mp, &pktinfo, GLD_RX) != 0) {
2690*2760Sdg199075 		BUMP(stats0, stats, glds_rcvbadinterp, 1);
26910Sstevel@tonic-gate #ifdef GLD_DEBUG
26920Sstevel@tonic-gate 		if (gld_debug & GLDERRS)
26930Sstevel@tonic-gate 			cmn_err(CE_WARN,
26940Sstevel@tonic-gate 			    "gld_recv_tagged: interpreter failed");
26950Sstevel@tonic-gate #endif
26960Sstevel@tonic-gate 		freemsg(mp);
26970Sstevel@tonic-gate 		goto done;
26980Sstevel@tonic-gate 	}
26990Sstevel@tonic-gate 
27000Sstevel@tonic-gate 	/*
27010Sstevel@tonic-gate 	 * This is safe even if vtag is VLAN_VTAG_NONE
27020Sstevel@tonic-gate 	 */
27030Sstevel@tonic-gate 	pktinfo.vid = vid;
27040Sstevel@tonic-gate 	pktinfo.cfi = GLD_VTAG_CFI(vtag);
27050Sstevel@tonic-gate #ifdef GLD_DEBUG
27060Sstevel@tonic-gate 	if (pktinfo.cfi != VLAN_CFI_ETHER)
27070Sstevel@tonic-gate 		cmn_err(CE_WARN, "gld_recv_tagged: non-ETHER CFI");
27080Sstevel@tonic-gate #endif
27090Sstevel@tonic-gate 	pktinfo.user_pri = GLD_VTAG_PRI(vtag);
2710*2760Sdg199075 	pktinfo.isTagged = (vtag != VLAN_VID_NONE);
27110Sstevel@tonic-gate 
27120Sstevel@tonic-gate #ifdef GLD_DEBUG
27130Sstevel@tonic-gate 	if ((gld_debug & GLDRECV) &&
27140Sstevel@tonic-gate 	    (!(gld_debug & GLDNOBR) ||
27150Sstevel@tonic-gate 	    (!pktinfo.isBroadcast && !pktinfo.isMulticast))) {
27160Sstevel@tonic-gate 		char pbuf2[3*GLD_MAX_ADDRLEN];
27170Sstevel@tonic-gate 
27180Sstevel@tonic-gate 		cmn_err(CE_CONT, "gld_recv_tagged: machdr=<%s -> %s>\n",
27190Sstevel@tonic-gate 		    gld_macaddr_sprintf(pbuf, pktinfo.shost,
27200Sstevel@tonic-gate 		    macinfo->gldm_addrlen), gld_macaddr_sprintf(pbuf2,
27210Sstevel@tonic-gate 		    pktinfo.dhost, macinfo->gldm_addrlen));
27220Sstevel@tonic-gate 		cmn_err(CE_CONT, "gld_recv_tagged: VlanId %d UserPri %d\n",
27230Sstevel@tonic-gate 		    pktinfo.vid,
27240Sstevel@tonic-gate 		    pktinfo.user_pri);
27250Sstevel@tonic-gate 		cmn_err(CE_CONT, "gld_recv_tagged: ethertype: %4x Len: %4d "
27260Sstevel@tonic-gate 		    "Hdr: %d,%d isMulticast: %s\n",
27270Sstevel@tonic-gate 		    pktinfo.ethertype,
27280Sstevel@tonic-gate 		    pktinfo.pktLen,
27290Sstevel@tonic-gate 		    pktinfo.macLen,
27300Sstevel@tonic-gate 		    pktinfo.hdrLen,
27310Sstevel@tonic-gate 		    pktinfo.isMulticast ? "Y" : "N");
27320Sstevel@tonic-gate 	}
27330Sstevel@tonic-gate #endif
27340Sstevel@tonic-gate 
2735*2760Sdg199075 	gld_sendup(macinfo, &pktinfo, mp, gld_accept);
27360Sstevel@tonic-gate 
27370Sstevel@tonic-gate done:
27380Sstevel@tonic-gate 	GLDM_UNLOCK(macinfo);
27390Sstevel@tonic-gate }
27400Sstevel@tonic-gate 
27410Sstevel@tonic-gate /* =================================================================== */
27420Sstevel@tonic-gate /* receive group: called from gld_recv and gld_precv* with maclock held */
27430Sstevel@tonic-gate /* =================================================================== */
27440Sstevel@tonic-gate 
27450Sstevel@tonic-gate /*
2746*2760Sdg199075  * Search all the streams attached to the specified VLAN looking for
2747*2760Sdg199075  * those eligible to receive the packet.
2748*2760Sdg199075  * Note that in order to avoid an extra dupmsg(), if this is the first
2749*2760Sdg199075  * eligible stream, remember it (in fgldp) so that we can send up the
2750*2760Sdg199075  * message after this function.
2751*2760Sdg199075  *
2752*2760Sdg199075  * Return errno if fails. Currently the only error is ENOMEM.
27530Sstevel@tonic-gate  */
2754*2760Sdg199075 static int
2755*2760Sdg199075 gld_sendup_vlan(gld_vlan_t *vlan, pktinfo_t *pktinfo, mblk_t *mp,
2756*2760Sdg199075     int (*acceptfunc)(), void (*send)(), int (*cansend)(), gld_t **fgldp)
27570Sstevel@tonic-gate {
27580Sstevel@tonic-gate 	mblk_t *nmp;
2759*2760Sdg199075 	gld_t *gld;
2760*2760Sdg199075 	int err = 0;
2761*2760Sdg199075 
27620Sstevel@tonic-gate 	ASSERT(vlan != NULL);
2763*2760Sdg199075 	for (gld = vlan->gldv_str_next; gld != (gld_t *)&vlan->gldv_str_next;
2764*2760Sdg199075 	    gld = gld->gld_next) {
27650Sstevel@tonic-gate #ifdef GLD_VERBOSE_DEBUG
2766*2760Sdg199075 		cmn_err(CE_NOTE, "gld_sendup: SAP: %4x QPTR: %p "QSTATE: %s",
27670Sstevel@tonic-gate 		    gld->gld_sap, (void *)gld->gld_qptr,
27680Sstevel@tonic-gate 		    gld->gld_state == DL_IDLE ? "IDLE": "NOT IDLE");
27690Sstevel@tonic-gate #endif
27700Sstevel@tonic-gate 		ASSERT(gld->gld_qptr != NULL);
27710Sstevel@tonic-gate 		ASSERT(gld->gld_state == DL_IDLE ||
27720Sstevel@tonic-gate 		    gld->gld_state == DL_UNBOUND);
27730Sstevel@tonic-gate 		ASSERT(gld->gld_vlan == vlan);
27740Sstevel@tonic-gate 
27750Sstevel@tonic-gate 		if (gld->gld_state != DL_IDLE)
27760Sstevel@tonic-gate 			continue;	/* not eligible to receive */
27770Sstevel@tonic-gate 		if (gld->gld_flags & GLD_STR_CLOSING)
27780Sstevel@tonic-gate 			continue;	/* not eligible to receive */
27790Sstevel@tonic-gate 
27800Sstevel@tonic-gate #ifdef GLD_DEBUG
27810Sstevel@tonic-gate 		if ((gld_debug & GLDRECV) &&
27820Sstevel@tonic-gate 		    (!(gld_debug & GLDNOBR) ||
27830Sstevel@tonic-gate 		    (!pktinfo->isBroadcast && !pktinfo->isMulticast)))
27840Sstevel@tonic-gate 			cmn_err(CE_NOTE,
27850Sstevel@tonic-gate 			    "gld_sendup: queue sap: %4x promis: %s %s %s",
27860Sstevel@tonic-gate 			    gld->gld_sap,
27870Sstevel@tonic-gate 			    gld->gld_flags & GLD_PROM_PHYS ? "phys " : "     ",
27880Sstevel@tonic-gate 			    gld->gld_flags & GLD_PROM_SAP  ? "sap  " : "     ",
27890Sstevel@tonic-gate 			    gld->gld_flags & GLD_PROM_MULT ? "multi" : "     ");
27900Sstevel@tonic-gate #endif
27910Sstevel@tonic-gate 
27920Sstevel@tonic-gate 		/*
27930Sstevel@tonic-gate 		 * The accept function differs depending on whether this is
27940Sstevel@tonic-gate 		 * a packet that we received from the wire or a loopback.
27950Sstevel@tonic-gate 		 */
27960Sstevel@tonic-gate 		if ((*acceptfunc)(gld, pktinfo)) {
27970Sstevel@tonic-gate 			/* sap matches */
2798*2760Sdg199075 			pktinfo->wasAccepted = 1; /* known protocol */
27990Sstevel@tonic-gate 
28000Sstevel@tonic-gate 			if (!(*cansend)(gld->gld_qptr)) {
28010Sstevel@tonic-gate 				/*
28020Sstevel@tonic-gate 				 * Upper stream is not accepting messages, i.e.
28030Sstevel@tonic-gate 				 * it is flow controlled, therefore we will
28040Sstevel@tonic-gate 				 * forgo sending the message up this stream.
28050Sstevel@tonic-gate 				 */
28060Sstevel@tonic-gate #ifdef GLD_DEBUG
28070Sstevel@tonic-gate 				if (gld_debug & GLDETRACE)
28080Sstevel@tonic-gate 					cmn_err(CE_WARN,
28090Sstevel@tonic-gate 					    "gld_sendup: canput failed");
28100Sstevel@tonic-gate #endif
2811*2760Sdg199075 				BUMP(vlan->gldv_stats, NULL, glds_blocked, 1);
28120Sstevel@tonic-gate 				qenable(gld->gld_qptr);
28130Sstevel@tonic-gate 				continue;
28140Sstevel@tonic-gate 			}
28150Sstevel@tonic-gate 
28160Sstevel@tonic-gate 			/*
2817*2760Sdg199075 			 * In order to avoid an extra dupmsg(), remember this
2818*2760Sdg199075 			 * gld if this is the first eligible stream.
28190Sstevel@tonic-gate 			 */
2820*2760Sdg199075 			if (*fgldp == NULL) {
2821*2760Sdg199075 				*fgldp = gld;
28220Sstevel@tonic-gate 				continue;
28230Sstevel@tonic-gate 			}
28240Sstevel@tonic-gate 
28250Sstevel@tonic-gate 			/* duplicate the packet for this stream */
28260Sstevel@tonic-gate 			nmp = dupmsg(mp);
28270Sstevel@tonic-gate 			if (nmp == NULL) {
2828*2760Sdg199075 				BUMP(vlan->gldv_stats, NULL,
2829*2760Sdg199075 				    glds_gldnorcvbuf, 1);
28300Sstevel@tonic-gate #ifdef GLD_DEBUG
28310Sstevel@tonic-gate 				if (gld_debug & GLDERRS)
28320Sstevel@tonic-gate 					cmn_err(CE_WARN,
28330Sstevel@tonic-gate 					    "gld_sendup: dupmsg failed");
28340Sstevel@tonic-gate #endif
2835*2760Sdg199075 				/* couldn't get resources; drop it */
2836*2760Sdg199075 				err = ENOMEM;
2837*2760Sdg199075 				break;
28380Sstevel@tonic-gate 			}
28390Sstevel@tonic-gate 			/* pass the message up the stream */
28400Sstevel@tonic-gate 			gld_passon(gld, nmp, pktinfo, send);
28410Sstevel@tonic-gate 		}
28420Sstevel@tonic-gate 	}
2843*2760Sdg199075 	return (err);
2844*2760Sdg199075 }
2845*2760Sdg199075 
2846*2760Sdg199075 /*
2847*2760Sdg199075  * gld_sendup (macinfo, pktinfo, mp, acceptfunc)
2848*2760Sdg199075  * called with an ethernet packet in an mblk; must decide whether
2849*2760Sdg199075  * packet is for us and which streams to queue it to.
2850*2760Sdg199075  */
2851*2760Sdg199075 static void
2852*2760Sdg199075 gld_sendup(gld_mac_info_t *macinfo, pktinfo_t *pktinfo,
2853*2760Sdg199075     mblk_t *mp, int (*acceptfunc)())
2854*2760Sdg199075 {
2855*2760Sdg199075 	gld_t *fgld = NULL;
2856*2760Sdg199075 	void (*send)(queue_t *qp, mblk_t *mp);
2857*2760Sdg199075 	int (*cansend)(queue_t *qp);
2858*2760Sdg199075 	gld_vlan_t *vlan0, *vlann = NULL;
2859*2760Sdg199075 	struct gld_stats *stats0, *stats = NULL;
2860*2760Sdg199075 	int err = 0;
2861*2760Sdg199075 
2862*2760Sdg199075 #ifdef GLD_DEBUG
2863*2760Sdg199075 	if (gld_debug & GLDTRACE)
2864*2760Sdg199075 		cmn_err(CE_NOTE, "gld_sendup(%p, %p)", (void *)mp,
2865*2760Sdg199075 		    (void *)macinfo);
2866*2760Sdg199075 #endif
2867*2760Sdg199075 
2868*2760Sdg199075 	ASSERT(mp != NULL);
2869*2760Sdg199075 	ASSERT(macinfo != NULL);
2870*2760Sdg199075 	ASSERT(pktinfo != NULL);
2871*2760Sdg199075 	ASSERT(GLDM_LOCK_HELD(macinfo));
2872*2760Sdg199075 
2873*2760Sdg199075 	/*
2874*2760Sdg199075 	 * The tagged packets should also be looped back (transmit-side)
2875*2760Sdg199075 	 * or sent up (receive-side) to VLAN 0 if VLAN 0 is set to
2876*2760Sdg199075 	 * DL_PROMISC_SAP or there is any DLPI consumer bind to the
2877*2760Sdg199075 	 * ETHERTYPE_VLAN SAP. The kstats of VLAN 0 needs to be updated
2878*2760Sdg199075 	 * as well.
2879*2760Sdg199075 	 */
2880*2760Sdg199075 	stats0 = ((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->statistics;
2881*2760Sdg199075 	vlan0 = gld_find_vlan(macinfo, VLAN_VID_NONE);
2882*2760Sdg199075 	if (pktinfo->vid != VLAN_VID_NONE) {
2883*2760Sdg199075 		if ((vlan0 != NULL) && (vlan0->gldv_nvlan_sap == 0))
2884*2760Sdg199075 			vlan0 = NULL;
2885*2760Sdg199075 		vlann = gld_find_vlan(macinfo, pktinfo->vid);
2886*2760Sdg199075 		if (vlann != NULL)
2887*2760Sdg199075 			stats = vlann->gldv_stats;
2888*2760Sdg199075 	}
2889*2760Sdg199075 
2890*2760Sdg199075 	ASSERT((vlan0 != NULL) || (vlann != NULL));
2891*2760Sdg199075 
2892*2760Sdg199075 	/*
2893*2760Sdg199075 	 * The "fast" in "GLDOPT_FAST_RECV" refers to the speed at which
2894*2760Sdg199075 	 * gld_recv returns to the caller's interrupt routine.  The total
2895*2760Sdg199075 	 * network throughput would normally be lower when selecting this
2896*2760Sdg199075 	 * option, because we putq the messages and process them later,
2897*2760Sdg199075 	 * instead of sending them with putnext now.  Some time critical
2898*2760Sdg199075 	 * device might need this, so it's here but undocumented.
2899*2760Sdg199075 	 */
2900*2760Sdg199075 	if (macinfo->gldm_options & GLDOPT_FAST_RECV) {
2901*2760Sdg199075 		send = (void (*)(queue_t *, mblk_t *))putq;
2902*2760Sdg199075 		cansend = canput;
2903*2760Sdg199075 	} else {
2904*2760Sdg199075 		send = (void (*)(queue_t *, mblk_t *))putnext;
2905*2760Sdg199075 		cansend = canputnext;
2906*2760Sdg199075 	}
2907*2760Sdg199075 
2908*2760Sdg199075 	/*
2909*2760Sdg199075 	 * Send the packets for all eligible streams.
2910*2760Sdg199075 	 */
2911*2760Sdg199075 	if (vlan0 != NULL) {
2912*2760Sdg199075 		err = gld_sendup_vlan(vlan0, pktinfo, mp, acceptfunc, send,
2913*2760Sdg199075 		    cansend, &fgld);
2914*2760Sdg199075 	}
2915*2760Sdg199075 	if ((err == 0) && (vlann != NULL)) {
2916*2760Sdg199075 		err = gld_sendup_vlan(vlann, pktinfo, mp, acceptfunc, send,
2917*2760Sdg199075 		    cansend, &fgld);
2918*2760Sdg199075 	}
29190Sstevel@tonic-gate 
29200Sstevel@tonic-gate 	ASSERT(mp);
29210Sstevel@tonic-gate 	/* send the original dup of the packet up the first stream found */
29220Sstevel@tonic-gate 	if (fgld)
29230Sstevel@tonic-gate 		gld_passon(fgld, mp, pktinfo, send);
29240Sstevel@tonic-gate 	else
29250Sstevel@tonic-gate 		freemsg(mp);	/* no streams matched */
29260Sstevel@tonic-gate 
29270Sstevel@tonic-gate 	/* We do not count looped back packets */
29280Sstevel@tonic-gate 	if (acceptfunc == gld_paccept)
29290Sstevel@tonic-gate 		return;		/* transmit loopback case */
29300Sstevel@tonic-gate 
29310Sstevel@tonic-gate 	if (pktinfo->isBroadcast)
2932*2760Sdg199075 		BUMP(stats0, stats, glds_brdcstrcv, 1);
29330Sstevel@tonic-gate 	else if (pktinfo->isMulticast)
2934*2760Sdg199075 		BUMP(stats0, stats, glds_multircv, 1);
29350Sstevel@tonic-gate 
29360Sstevel@tonic-gate 	/* No stream accepted this packet */
29370Sstevel@tonic-gate 	if (!pktinfo->wasAccepted)
2938*2760Sdg199075 		BUMP(stats0, stats, glds_unknowns, 1);
29390Sstevel@tonic-gate }
29400Sstevel@tonic-gate 
2941*2760Sdg199075 #define	GLD_IS_PHYS(gld)	\
2942*2760Sdg199075 	(((gld_vlan_t *)gld->gld_vlan)->gldv_id == VLAN_VID_NONE)
2943*2760Sdg199075 
29440Sstevel@tonic-gate /*
29450Sstevel@tonic-gate  * A packet matches a stream if:
2946*2760Sdg199075  *      The stream's VLAN id is the same as the one in the packet.
2947*2760Sdg199075  *  and the stream accepts EtherType encoded packets and the type matches
2948*2760Sdg199075  *  or  the stream accepts LLC packets and the packet is an LLC packet
29490Sstevel@tonic-gate  */
29500Sstevel@tonic-gate #define	MATCH(stream, pktinfo) \
2951*2760Sdg199075 	((((gld_vlan_t *)stream->gld_vlan)->gldv_id == pktinfo->vid) && \
29520Sstevel@tonic-gate 	((stream->gld_ethertype && stream->gld_sap == pktinfo->ethertype) || \
2953*2760Sdg199075 	(!stream->gld_ethertype && pktinfo->isLLC)))
29540Sstevel@tonic-gate 
29550Sstevel@tonic-gate /*
29560Sstevel@tonic-gate  * This function validates a packet for sending up a particular
29570Sstevel@tonic-gate  * stream. The message header has been parsed and its characteristic
29580Sstevel@tonic-gate  * are recorded in the pktinfo data structure. The streams stack info
29590Sstevel@tonic-gate  * are presented in gld data structures.
29600Sstevel@tonic-gate  */
29610Sstevel@tonic-gate static int
29620Sstevel@tonic-gate gld_accept(gld_t *gld, pktinfo_t *pktinfo)
29630Sstevel@tonic-gate {
29640Sstevel@tonic-gate 	/*
29650Sstevel@tonic-gate 	 * if there is no match do not bother checking further.
2966*2760Sdg199075 	 * Note that it is okay to examine gld_vlan because
2967*2760Sdg199075 	 * macinfo->gldm_lock is held.
2968*2760Sdg199075 	 *
2969*2760Sdg199075 	 * Because all tagged packets have SAP value ETHERTYPE_VLAN,
2970*2760Sdg199075 	 * these packets will pass the SAP filter check if the stream
2971*2760Sdg199075 	 * is a ETHERTYPE_VLAN listener.
29720Sstevel@tonic-gate 	 */
2973*2760Sdg199075 	if ((!MATCH(gld, pktinfo) && !(gld->gld_flags & GLD_PROM_SAP) &&
2974*2760Sdg199075 	    !(GLD_IS_PHYS(gld) && gld->gld_sap == ETHERTYPE_VLAN &&
2975*2760Sdg199075 	    pktinfo->isTagged)))
29760Sstevel@tonic-gate 		return (0);
29770Sstevel@tonic-gate 
29780Sstevel@tonic-gate 	/*
29790Sstevel@tonic-gate 	 * We don't accept any packet from the hardware if we originated it.
29800Sstevel@tonic-gate 	 * (Contrast gld_paccept, the send-loopback accept function.)
29810Sstevel@tonic-gate 	 */
29820Sstevel@tonic-gate 	if (pktinfo->isLooped)
29830Sstevel@tonic-gate 		return (0);
29840Sstevel@tonic-gate 
29850Sstevel@tonic-gate 	/*
29860Sstevel@tonic-gate 	 * If the packet is broadcast or sent to us directly we will accept it.
29870Sstevel@tonic-gate 	 * Also we will accept multicast packets requested by the stream.
29880Sstevel@tonic-gate 	 */
29890Sstevel@tonic-gate 	if (pktinfo->isForMe || pktinfo->isBroadcast ||
29900Sstevel@tonic-gate 	    gld_mcmatch(gld, pktinfo))
29910Sstevel@tonic-gate 		return (1);
29920Sstevel@tonic-gate 
29930Sstevel@tonic-gate 	/*
29940Sstevel@tonic-gate 	 * Finally, accept anything else if we're in promiscuous mode
29950Sstevel@tonic-gate 	 */
29960Sstevel@tonic-gate 	if (gld->gld_flags & GLD_PROM_PHYS)
29970Sstevel@tonic-gate 		return (1);
29980Sstevel@tonic-gate 
29990Sstevel@tonic-gate 	return (0);
30000Sstevel@tonic-gate }
30010Sstevel@tonic-gate 
30020Sstevel@tonic-gate /*
30030Sstevel@tonic-gate  * Return TRUE if the given multicast address is one
30040Sstevel@tonic-gate  * of those that this particular Stream is interested in.
30050Sstevel@tonic-gate  */
30060Sstevel@tonic-gate static int
30070Sstevel@tonic-gate gld_mcmatch(gld_t *gld, pktinfo_t *pktinfo)
30080Sstevel@tonic-gate {
30090Sstevel@tonic-gate 	/*
30100Sstevel@tonic-gate 	 * Return FALSE if not a multicast address.
30110Sstevel@tonic-gate 	 */
30120Sstevel@tonic-gate 	if (!pktinfo->isMulticast)
30130Sstevel@tonic-gate 		return (0);
30140Sstevel@tonic-gate 
30150Sstevel@tonic-gate 	/*
30160Sstevel@tonic-gate 	 * Check if all multicasts have been enabled for this Stream
30170Sstevel@tonic-gate 	 */
30180Sstevel@tonic-gate 	if (gld->gld_flags & GLD_PROM_MULT)
30190Sstevel@tonic-gate 		return (1);
30200Sstevel@tonic-gate 
30210Sstevel@tonic-gate 	/*
30220Sstevel@tonic-gate 	 * Return FALSE if no multicast addresses enabled for this Stream.
30230Sstevel@tonic-gate 	 */
30240Sstevel@tonic-gate 	if (!gld->gld_mcast)
30250Sstevel@tonic-gate 		return (0);
30260Sstevel@tonic-gate 
30270Sstevel@tonic-gate 	/*
30280Sstevel@tonic-gate 	 * Otherwise, look for it in the table.
30290Sstevel@tonic-gate 	 */
30300Sstevel@tonic-gate 	return (gld_multicast(pktinfo->dhost, gld));
30310Sstevel@tonic-gate }
30320Sstevel@tonic-gate 
30330Sstevel@tonic-gate /*
30340Sstevel@tonic-gate  * gld_multicast determines if the address is a multicast address for
30350Sstevel@tonic-gate  * this stream.
30360Sstevel@tonic-gate  */
30370Sstevel@tonic-gate static int
30380Sstevel@tonic-gate gld_multicast(unsigned char *macaddr, gld_t *gld)
30390Sstevel@tonic-gate {
30400Sstevel@tonic-gate 	int i;
30410Sstevel@tonic-gate 
30420Sstevel@tonic-gate 	ASSERT(GLDM_LOCK_HELD(gld->gld_mac_info));
30430Sstevel@tonic-gate 
30440Sstevel@tonic-gate 	if (!gld->gld_mcast)
30450Sstevel@tonic-gate 		return (0);
30460Sstevel@tonic-gate 
30470Sstevel@tonic-gate 	for (i = 0; i < gld->gld_multicnt; i++) {
30480Sstevel@tonic-gate 		if (gld->gld_mcast[i]) {
30490Sstevel@tonic-gate 			ASSERT(gld->gld_mcast[i]->gldm_refcnt);
30500Sstevel@tonic-gate 			if (mac_eq(gld->gld_mcast[i]->gldm_addr, macaddr,
30510Sstevel@tonic-gate 			    gld->gld_mac_info->gldm_addrlen))
30520Sstevel@tonic-gate 				return (1);
30530Sstevel@tonic-gate 		}
30540Sstevel@tonic-gate 	}
30550Sstevel@tonic-gate 
30560Sstevel@tonic-gate 	return (0);
30570Sstevel@tonic-gate }
30580Sstevel@tonic-gate 
30590Sstevel@tonic-gate /*
30600Sstevel@tonic-gate  * accept function for looped back packets
30610Sstevel@tonic-gate  */
30620Sstevel@tonic-gate static int
30630Sstevel@tonic-gate gld_paccept(gld_t *gld, pktinfo_t *pktinfo)
30640Sstevel@tonic-gate {
3065*2760Sdg199075 	/*
3066*2760Sdg199075 	 * Note that it is okay to examine gld_vlan because macinfo->gldm_lock
3067*2760Sdg199075 	 * is held.
3068*2760Sdg199075 	 *
3069*2760Sdg199075 	 * If a stream is a ETHERTYPE_VLAN listener, it must
3070*2760Sdg199075 	 * accept all tagged packets as those packets have SAP value
3071*2760Sdg199075 	 * ETHERTYPE_VLAN.
3072*2760Sdg199075 	 */
30730Sstevel@tonic-gate 	return (gld->gld_flags & GLD_PROM_PHYS &&
3074*2760Sdg199075 	    (MATCH(gld, pktinfo) || gld->gld_flags & GLD_PROM_SAP ||
3075*2760Sdg199075 	    (GLD_IS_PHYS(gld) && gld->gld_sap == ETHERTYPE_VLAN &&
3076*2760Sdg199075 	    pktinfo->isTagged)));
3077*2760Sdg199075 
30780Sstevel@tonic-gate }
30790Sstevel@tonic-gate 
30800Sstevel@tonic-gate static void
30810Sstevel@tonic-gate gld_passon(gld_t *gld, mblk_t *mp, pktinfo_t *pktinfo,
30820Sstevel@tonic-gate 	void (*send)(queue_t *qp, mblk_t *mp))
30830Sstevel@tonic-gate {
3084*2760Sdg199075 	boolean_t is_phys = GLD_IS_PHYS(gld);
30850Sstevel@tonic-gate 	int skiplen;
3086*2760Sdg199075 	boolean_t addtag = B_FALSE;
3087*2760Sdg199075 	uint32_t vtag = 0;
30880Sstevel@tonic-gate 
30890Sstevel@tonic-gate #ifdef GLD_DEBUG
30900Sstevel@tonic-gate 	if (gld_debug & GLDTRACE)
30910Sstevel@tonic-gate 		cmn_err(CE_NOTE, "gld_passon(%p, %p, %p)", (void *)gld,
30920Sstevel@tonic-gate 		    (void *)mp, (void *)pktinfo);
30930Sstevel@tonic-gate 
30940Sstevel@tonic-gate 	if ((gld_debug & GLDRECV) && (!(gld_debug & GLDNOBR) ||
30950Sstevel@tonic-gate 	    (!pktinfo->isBroadcast && !pktinfo->isMulticast)))
30960Sstevel@tonic-gate 		cmn_err(CE_NOTE, "gld_passon: q: %p mblk: %p minor: %d sap: %x",
30970Sstevel@tonic-gate 		    (void *)gld->gld_qptr->q_next, (void *)mp, gld->gld_minor,
30980Sstevel@tonic-gate 		    gld->gld_sap);
30990Sstevel@tonic-gate #endif
31000Sstevel@tonic-gate 	/*
31010Sstevel@tonic-gate 	 * Figure out how much of the packet header to throw away.
31020Sstevel@tonic-gate 	 *
31030Sstevel@tonic-gate 	 * Normal DLPI (non RAW/FAST) streams also want the
31040Sstevel@tonic-gate 	 * DL_UNITDATA_IND M_PROTO message block prepended to the M_DATA.
31050Sstevel@tonic-gate 	 */
31060Sstevel@tonic-gate 	if (gld->gld_flags & GLD_RAW) {
3107*2760Sdg199075 		/*
3108*2760Sdg199075 		 * The packet will be tagged in the following cases:
3109*2760Sdg199075 		 *   - if priority is not 0
3110*2760Sdg199075 		 *   - a tagged packet sent on a physical link
3111*2760Sdg199075 		 */
3112*2760Sdg199075 		if ((pktinfo->isTagged && is_phys) || (pktinfo->user_pri != 0))
3113*2760Sdg199075 			addtag = B_TRUE;
31140Sstevel@tonic-gate 		skiplen = 0;
31150Sstevel@tonic-gate 	} else {
3116*2760Sdg199075 		/*
3117*2760Sdg199075 		 * The packet will be tagged if it meets all below conditions:
3118*2760Sdg199075 		 *   -  this is a physical stream
3119*2760Sdg199075 		 *   -  this packet is tagged packet
3120*2760Sdg199075 		 *   -  the stream is either a DL_PROMISC_SAP listener or a
3121*2760Sdg199075 		 *	ETHERTYPE_VLAN listener
3122*2760Sdg199075 		 */
3123*2760Sdg199075 		if (is_phys && pktinfo->isTagged &&
3124*2760Sdg199075 		    ((gld->gld_sap == ETHERTYPE_VLAN) ||
3125*2760Sdg199075 		    (gld->gld_flags & GLD_PROM_SAP))) {
3126*2760Sdg199075 			addtag = B_TRUE;
3127*2760Sdg199075 		}
3128*2760Sdg199075 
31290Sstevel@tonic-gate 		skiplen = pktinfo->macLen;		/* skip mac header */
31300Sstevel@tonic-gate 		if (gld->gld_ethertype)
31310Sstevel@tonic-gate 			skiplen += pktinfo->hdrLen;	/* skip any extra */
31320Sstevel@tonic-gate 	}
31330Sstevel@tonic-gate 	if (skiplen >= pktinfo->pktLen) {
31340Sstevel@tonic-gate 		/*
31350Sstevel@tonic-gate 		 * If the interpreter did its job right, then it cannot be
31360Sstevel@tonic-gate 		 * asking us to skip more bytes than are in the packet!
31370Sstevel@tonic-gate 		 * However, there could be zero data bytes left after the
31380Sstevel@tonic-gate 		 * amount to skip.  DLPI specifies that passed M_DATA blocks
31390Sstevel@tonic-gate 		 * should contain at least one byte of data, so if we have
31400Sstevel@tonic-gate 		 * none we just drop it.
31410Sstevel@tonic-gate 		 */
31420Sstevel@tonic-gate 		ASSERT(!(skiplen > pktinfo->pktLen));
31430Sstevel@tonic-gate 		freemsg(mp);
31440Sstevel@tonic-gate 		return;
31450Sstevel@tonic-gate 	}
31460Sstevel@tonic-gate 
3147*2760Sdg199075 	if (addtag) {
3148*2760Sdg199075 		mblk_t *savemp = mp;
3149*2760Sdg199075 
3150*2760Sdg199075 		vtag = GLD_MAKE_VTAG(pktinfo->user_pri, pktinfo->cfi,
3151*2760Sdg199075 		    is_phys ? pktinfo->vid : VLAN_VID_NONE);
3152*2760Sdg199075 		if ((mp = gld_insert_vtag_ether(mp, vtag)) == NULL) {
3153*2760Sdg199075 			freemsg(savemp);
3154*2760Sdg199075 			return;
3155*2760Sdg199075 		}
3156*2760Sdg199075 	}
3157*2760Sdg199075 
31580Sstevel@tonic-gate 	/*
31590Sstevel@tonic-gate 	 * Skip over the header(s), taking care to possibly handle message
31600Sstevel@tonic-gate 	 * fragments shorter than the amount we need to skip.  Hopefully
31610Sstevel@tonic-gate 	 * the driver will put the entire packet, or at least the entire
31620Sstevel@tonic-gate 	 * header, into a single message block.  But we handle it if not.
31630Sstevel@tonic-gate 	 */
31640Sstevel@tonic-gate 	while (skiplen >= MBLKL(mp)) {
3165*2760Sdg199075 		mblk_t *savemp = mp;
31660Sstevel@tonic-gate 		skiplen -= MBLKL(mp);
31670Sstevel@tonic-gate 		mp = mp->b_cont;
31680Sstevel@tonic-gate 		ASSERT(mp != NULL);	/* because skiplen < pktinfo->pktLen */
3169*2760Sdg199075 		freeb(savemp);
31700Sstevel@tonic-gate 	}
31710Sstevel@tonic-gate 	mp->b_rptr += skiplen;
31720Sstevel@tonic-gate 
31730Sstevel@tonic-gate 	/* Add M_PROTO if necessary, and pass upstream */
31740Sstevel@tonic-gate 	if (((gld->gld_flags & GLD_FAST) && !pktinfo->isMulticast &&
31750Sstevel@tonic-gate 	    !pktinfo->isBroadcast) || (gld->gld_flags & GLD_RAW)) {
31760Sstevel@tonic-gate 		/* RAW/FAST: just send up the M_DATA */
31770Sstevel@tonic-gate 		(*send)(gld->gld_qptr, mp);
31780Sstevel@tonic-gate 	} else {
31790Sstevel@tonic-gate 		/* everybody else wants to see a unitdata_ind structure */
3180*2760Sdg199075 		mp = gld_addudind(gld, mp, pktinfo, addtag);
31810Sstevel@tonic-gate 		if (mp)
31820Sstevel@tonic-gate 			(*send)(gld->gld_qptr, mp);
31830Sstevel@tonic-gate 		/* if it failed, gld_addudind already bumped statistic */
31840Sstevel@tonic-gate 	}
31850Sstevel@tonic-gate }
31860Sstevel@tonic-gate 
31870Sstevel@tonic-gate /*
31880Sstevel@tonic-gate  * gld_addudind(gld, mp, pktinfo)
31890Sstevel@tonic-gate  * format a DL_UNITDATA_IND message to be sent upstream to the user
31900Sstevel@tonic-gate  */
31910Sstevel@tonic-gate static mblk_t *
3192*2760Sdg199075 gld_addudind(gld_t *gld, mblk_t *mp, pktinfo_t *pktinfo, boolean_t tagged)
31930Sstevel@tonic-gate {
31940Sstevel@tonic-gate 	gld_mac_info_t		*macinfo = gld->gld_mac_info;
31950Sstevel@tonic-gate 	gld_vlan_t		*vlan = (gld_vlan_t *)gld->gld_vlan;
31960Sstevel@tonic-gate 	dl_unitdata_ind_t	*dludindp;
31970Sstevel@tonic-gate 	mblk_t			*nmp;
31980Sstevel@tonic-gate 	int			size;
31990Sstevel@tonic-gate 	int			type;
32000Sstevel@tonic-gate 
32010Sstevel@tonic-gate #ifdef GLD_DEBUG
32020Sstevel@tonic-gate 	if (gld_debug & GLDTRACE)
32030Sstevel@tonic-gate 		cmn_err(CE_NOTE, "gld_addudind(%p, %p, %p)", (void *)gld,
32040Sstevel@tonic-gate 		    (void *)mp, (void *)pktinfo);
32050Sstevel@tonic-gate #endif
32060Sstevel@tonic-gate 	ASSERT(macinfo != NULL);
32070Sstevel@tonic-gate 
32080Sstevel@tonic-gate 	/*
32090Sstevel@tonic-gate 	 * Allocate the DL_UNITDATA_IND M_PROTO header, if allocation fails
32100Sstevel@tonic-gate 	 * might as well discard since we can't go further
32110Sstevel@tonic-gate 	 */
32120Sstevel@tonic-gate 	size = sizeof (dl_unitdata_ind_t) +
32130Sstevel@tonic-gate 	    2 * (macinfo->gldm_addrlen + abs(macinfo->gldm_saplen));
32140Sstevel@tonic-gate 	if ((nmp = allocb(size, BPRI_MED)) == NULL) {
32150Sstevel@tonic-gate 		freemsg(mp);
3216*2760Sdg199075 		BUMP(vlan->gldv_stats, NULL, glds_gldnorcvbuf, 1);
32170Sstevel@tonic-gate #ifdef GLD_DEBUG
32180Sstevel@tonic-gate 		if (gld_debug & GLDERRS)
32190Sstevel@tonic-gate 			cmn_err(CE_WARN,
32200Sstevel@tonic-gate 			    "gld_addudind: allocb failed");
32210Sstevel@tonic-gate #endif
32220Sstevel@tonic-gate 		return ((mblk_t *)NULL);
32230Sstevel@tonic-gate 	}
32240Sstevel@tonic-gate 	DB_TYPE(nmp) = M_PROTO;
32250Sstevel@tonic-gate 	nmp->b_rptr = nmp->b_datap->db_lim - size;
32260Sstevel@tonic-gate 
3227*2760Sdg199075 	if (tagged)
3228*2760Sdg199075 		type = ETHERTYPE_VLAN;
3229*2760Sdg199075 	else
3230*2760Sdg199075 		type = (gld->gld_ethertype) ? pktinfo->ethertype : 0;
3231*2760Sdg199075 
32320Sstevel@tonic-gate 
32330Sstevel@tonic-gate 	/*
32340Sstevel@tonic-gate 	 * now setup the DL_UNITDATA_IND header
32350Sstevel@tonic-gate 	 *
32360Sstevel@tonic-gate 	 * XXX This looks broken if the saps aren't two bytes.
32370Sstevel@tonic-gate 	 */
32380Sstevel@tonic-gate 	dludindp = (dl_unitdata_ind_t *)nmp->b_rptr;
32390Sstevel@tonic-gate 	dludindp->dl_primitive = DL_UNITDATA_IND;
32400Sstevel@tonic-gate 	dludindp->dl_src_addr_length =
32410Sstevel@tonic-gate 	    dludindp->dl_dest_addr_length = macinfo->gldm_addrlen +
32420Sstevel@tonic-gate 					abs(macinfo->gldm_saplen);
32430Sstevel@tonic-gate 	dludindp->dl_dest_addr_offset = sizeof (dl_unitdata_ind_t);
32440Sstevel@tonic-gate 	dludindp->dl_src_addr_offset = dludindp->dl_dest_addr_offset +
32450Sstevel@tonic-gate 					dludindp->dl_dest_addr_length;
32460Sstevel@tonic-gate 
32470Sstevel@tonic-gate 	dludindp->dl_group_address = (pktinfo->isMulticast ||
32480Sstevel@tonic-gate 					pktinfo->isBroadcast);
32490Sstevel@tonic-gate 
32500Sstevel@tonic-gate 	nmp->b_wptr = nmp->b_rptr + dludindp->dl_dest_addr_offset;
32510Sstevel@tonic-gate 
32520Sstevel@tonic-gate 	mac_copy(pktinfo->dhost, nmp->b_wptr, macinfo->gldm_addrlen);
32530Sstevel@tonic-gate 	nmp->b_wptr += macinfo->gldm_addrlen;
32540Sstevel@tonic-gate 
32550Sstevel@tonic-gate 	ASSERT(macinfo->gldm_saplen == -2);	/* XXX following code assumes */
32560Sstevel@tonic-gate 	*(ushort_t *)(nmp->b_wptr) = type;
32570Sstevel@tonic-gate 	nmp->b_wptr += abs(macinfo->gldm_saplen);
32580Sstevel@tonic-gate 
32590Sstevel@tonic-gate 	ASSERT(nmp->b_wptr == nmp->b_rptr + dludindp->dl_src_addr_offset);
32600Sstevel@tonic-gate 
32610Sstevel@tonic-gate 	mac_copy(pktinfo->shost, nmp->b_wptr, macinfo->gldm_addrlen);
32620Sstevel@tonic-gate 	nmp->b_wptr += macinfo->gldm_addrlen;
32630Sstevel@tonic-gate 
32640Sstevel@tonic-gate 	*(ushort_t *)(nmp->b_wptr) = type;
32650Sstevel@tonic-gate 	nmp->b_wptr += abs(macinfo->gldm_saplen);
32660Sstevel@tonic-gate 
32670Sstevel@tonic-gate 	if (pktinfo->nosource)
32680Sstevel@tonic-gate 		dludindp->dl_src_addr_offset = dludindp->dl_src_addr_length = 0;
32690Sstevel@tonic-gate 	linkb(nmp, mp);
32700Sstevel@tonic-gate 	return (nmp);
32710Sstevel@tonic-gate }
32720Sstevel@tonic-gate 
32730Sstevel@tonic-gate /* ======================================================= */
32740Sstevel@tonic-gate /* wsrv group: called from wsrv, single threaded per queue */
32750Sstevel@tonic-gate /* ======================================================= */
32760Sstevel@tonic-gate 
32770Sstevel@tonic-gate /*
32780Sstevel@tonic-gate  * We go to some trouble to avoid taking the same lock during normal
32790Sstevel@tonic-gate  * transmit processing as we do during normal receive processing.
32800Sstevel@tonic-gate  *
32810Sstevel@tonic-gate  * Elements of the per-instance macinfo and per-stream gld_t structures
32820Sstevel@tonic-gate  * are for the most part protected by the GLDM_LOCK rwlock/mutex.
32830Sstevel@tonic-gate  * (Elements of the gld_mac_pvt_t structure are considered part of the
32840Sstevel@tonic-gate  * macinfo structure for purposes of this discussion).
32850Sstevel@tonic-gate  *
32860Sstevel@tonic-gate  * However, it is more complicated than that:
32870Sstevel@tonic-gate  *
32880Sstevel@tonic-gate  *	Elements of the macinfo structure that are set before the macinfo
32890Sstevel@tonic-gate  *	structure is added to its device list by gld_register(), and never
32900Sstevel@tonic-gate  *	thereafter modified, are accessed without requiring taking the lock.
32910Sstevel@tonic-gate  *	A similar rule applies to those elements of the gld_t structure that
32920Sstevel@tonic-gate  *	are written by gld_open() before the stream is added to any list.
32930Sstevel@tonic-gate  *
32940Sstevel@tonic-gate  *	Most other elements of the macinfo structure may only be read or
32950Sstevel@tonic-gate  *	written while holding the maclock.
32960Sstevel@tonic-gate  *
32970Sstevel@tonic-gate  *	Most writable elements of the gld_t structure are written only
32980Sstevel@tonic-gate  *	within the single-threaded domain of wsrv() and subsidiaries.
32990Sstevel@tonic-gate  *	(This domain includes open/close while qprocs are not on.)
33000Sstevel@tonic-gate  *	The maclock need not be taken while within that domain
33010Sstevel@tonic-gate  *	simply to read those elements.  Writing to them, even within
33020Sstevel@tonic-gate  *	that domain, or reading from it outside that domain, requires
33030Sstevel@tonic-gate  *	holding the maclock.  Exception:  if the stream is not
33040Sstevel@tonic-gate  *	presently attached to a PPA, there is no associated macinfo,
33050Sstevel@tonic-gate  *	and no maclock need be taken.
33060Sstevel@tonic-gate  *
33070Sstevel@tonic-gate  *	The curr_macaddr element of the mac private structure is also
33080Sstevel@tonic-gate  *      protected by the GLDM_LOCK rwlock/mutex, like most other members
33090Sstevel@tonic-gate  *      of that structure. However, there are a few instances in the
33100Sstevel@tonic-gate  *      transmit path where we choose to forgo lock protection when
33110Sstevel@tonic-gate  *      reading this variable. This is to avoid lock contention between
33120Sstevel@tonic-gate  *      threads executing the DL_UNITDATA_REQ case and receive threads.
33130Sstevel@tonic-gate  *      In doing so we will take a small risk or a few corrupted packets
33140Sstevel@tonic-gate  *      during the short an rare times when someone is changing the interface's
33150Sstevel@tonic-gate  *      physical address. We consider the small cost in this rare case to be
33160Sstevel@tonic-gate  *      worth the benefit of reduced lock contention under normal operating
33170Sstevel@tonic-gate  *      conditions. The risk/cost is small because:
33180Sstevel@tonic-gate  *          1. there is no guarantee at this layer of uncorrupted delivery.
33190Sstevel@tonic-gate  *          2. the physaddr doesn't change very often - no performance hit.
33200Sstevel@tonic-gate  *          3. if the physaddr changes, other stuff is going to be screwed
33210Sstevel@tonic-gate  *             up for a while anyway, while other sites refigure ARP, etc.,
33220Sstevel@tonic-gate  *             so losing a couple of packets is the least of our worries.
33230Sstevel@tonic-gate  *
33240Sstevel@tonic-gate  *	The list of streams associated with a macinfo is protected by
33250Sstevel@tonic-gate  *	two locks:  the per-macinfo maclock, and the per-major-device
33260Sstevel@tonic-gate  *	gld_devlock.  Both must be held to modify the list, but either
33270Sstevel@tonic-gate  *	may be held to protect the list during reading/traversing.  This
33280Sstevel@tonic-gate  *	allows independent locking for multiple instances in the receive
33290Sstevel@tonic-gate  *	path (using macinfo), while facilitating routines that must search
33300Sstevel@tonic-gate  *	the entire set of streams associated with a major device, such as
33310Sstevel@tonic-gate  *	gld_findminor(), gld_finddevinfo(), close().  The "nstreams"
33320Sstevel@tonic-gate  *	macinfo	element, and the gld_mac_info gld_t element, are similarly
33330Sstevel@tonic-gate  *	protected, since they change at exactly the same time macinfo
33340Sstevel@tonic-gate  *	streams list does.
33350Sstevel@tonic-gate  *
33360Sstevel@tonic-gate  *	The list of macinfo structures associated with a major device
33370Sstevel@tonic-gate  *	structure is protected by the gld_devlock, as is the per-major
33380Sstevel@tonic-gate  *	list of Style 2 streams in the DL_UNATTACHED state.
33390Sstevel@tonic-gate  *
33400Sstevel@tonic-gate  *	The list of major devices is kept on a module-global list
33410Sstevel@tonic-gate  *	gld_device_list, which has its own lock to protect the list.
33420Sstevel@tonic-gate  *
33430Sstevel@tonic-gate  *	When it is necessary to hold more than one lock at a time, they
33440Sstevel@tonic-gate  *	are acquired in this "outside in" order:
33450Sstevel@tonic-gate  *		gld_device_list.gld_devlock
33460Sstevel@tonic-gate  *		glddev->gld_devlock
33470Sstevel@tonic-gate  *		GLDM_LOCK(macinfo)
33480Sstevel@tonic-gate  *
33490Sstevel@tonic-gate  *	Finally, there are some "volatile" elements of the gld_t structure
33500Sstevel@tonic-gate  *	used for synchronization between various routines that don't share
33510Sstevel@tonic-gate  *	the same mutexes.  See the routines for details.  These are:
33520Sstevel@tonic-gate  *		gld_xwait	between gld_wsrv() and gld_sched()
33530Sstevel@tonic-gate  *		gld_sched_ran	between gld_wsrv() and gld_sched()
33540Sstevel@tonic-gate  *		gld_in_unbind	between gld_wput() and wsrv's gld_unbind()
33550Sstevel@tonic-gate  *		gld_wput_count	between gld_wput() and wsrv's gld_unbind()
33560Sstevel@tonic-gate  *		gld_in_wsrv	between gld_wput() and gld_wsrv()
33570Sstevel@tonic-gate  *				(used in conjunction with q->q_first)
33580Sstevel@tonic-gate  */
33590Sstevel@tonic-gate 
33600Sstevel@tonic-gate /*
33610Sstevel@tonic-gate  * gld_ioctl (q, mp)
33620Sstevel@tonic-gate  * handles all ioctl requests passed downstream. This routine is
33630Sstevel@tonic-gate  * passed a pointer to the message block with the ioctl request in it, and a
33640Sstevel@tonic-gate  * pointer to the queue so it can respond to the ioctl request with an ack.
33650Sstevel@tonic-gate  */
33660Sstevel@tonic-gate int
33670Sstevel@tonic-gate gld_ioctl(queue_t *q, mblk_t *mp)
33680Sstevel@tonic-gate {
33690Sstevel@tonic-gate 	struct iocblk *iocp;
33700Sstevel@tonic-gate 	gld_t *gld;
33710Sstevel@tonic-gate 	gld_mac_info_t *macinfo;
33720Sstevel@tonic-gate 
33730Sstevel@tonic-gate #ifdef GLD_DEBUG
33740Sstevel@tonic-gate 	if (gld_debug & GLDTRACE)
33750Sstevel@tonic-gate 		cmn_err(CE_NOTE, "gld_ioctl(%p %p)", (void *)q, (void *)mp);
33760Sstevel@tonic-gate #endif
33770Sstevel@tonic-gate 	gld = (gld_t *)q->q_ptr;
33780Sstevel@tonic-gate 	iocp = (struct iocblk *)mp->b_rptr;
33790Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
33800Sstevel@tonic-gate 	case DLIOCRAW:		/* raw M_DATA mode */
33810Sstevel@tonic-gate 		gld->gld_flags |= GLD_RAW;
33820Sstevel@tonic-gate 		DB_TYPE(mp) = M_IOCACK;
33830Sstevel@tonic-gate 		qreply(q, mp);
33840Sstevel@tonic-gate 		break;
33850Sstevel@tonic-gate 
33860Sstevel@tonic-gate 	case DL_IOC_HDR_INFO:	/* fastpath */
3387*2760Sdg199075 		/*
3388*2760Sdg199075 		 * DL_IOC_HDR_INFO should only come from IP. The one
3389*2760Sdg199075 		 * initiated from user-land should not be allowed.
3390*2760Sdg199075 		 */
3391*2760Sdg199075 		if ((gld_global_options & GLD_OPT_NO_FASTPATH) ||
3392*2760Sdg199075 		    (iocp->ioc_cr != kcred)) {
33930Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
33940Sstevel@tonic-gate 			break;
33950Sstevel@tonic-gate 		}
33960Sstevel@tonic-gate 		gld_fastpath(gld, q, mp);
33970Sstevel@tonic-gate 		break;
33980Sstevel@tonic-gate 
33990Sstevel@tonic-gate 	default:
34000Sstevel@tonic-gate 		macinfo	 = gld->gld_mac_info;
34010Sstevel@tonic-gate 		if (macinfo == NULL || macinfo->gldm_ioctl == NULL) {
34020Sstevel@tonic-gate 			miocnak(q, mp, 0, EINVAL);
34030Sstevel@tonic-gate 			break;
34040Sstevel@tonic-gate 		}
34050Sstevel@tonic-gate 
34060Sstevel@tonic-gate 		GLDM_LOCK(macinfo, RW_WRITER);
34070Sstevel@tonic-gate 		(void) (*macinfo->gldm_ioctl) (macinfo, q, mp);
34080Sstevel@tonic-gate 		GLDM_UNLOCK(macinfo);
34090Sstevel@tonic-gate 		break;
34100Sstevel@tonic-gate 	}
34110Sstevel@tonic-gate 	return (0);
34120Sstevel@tonic-gate }
34130Sstevel@tonic-gate 
34140Sstevel@tonic-gate /*
34150Sstevel@tonic-gate  * Since the rules for "fastpath" mode don't seem to be documented
34160Sstevel@tonic-gate  * anywhere, I will describe GLD's rules for fastpath users here:
34170Sstevel@tonic-gate  *
34180Sstevel@tonic-gate  * Once in this mode you remain there until close.
34190Sstevel@tonic-gate  * If you unbind/rebind you should get a new header using DL_IOC_HDR_INFO.
34200Sstevel@tonic-gate  * You must be bound (DL_IDLE) to transmit.
34210Sstevel@tonic-gate  * There are other rules not listed above.
34220Sstevel@tonic-gate  */
34230Sstevel@tonic-gate static void
34240Sstevel@tonic-gate gld_fastpath(gld_t *gld, queue_t *q, mblk_t *mp)
34250Sstevel@tonic-gate {
34260Sstevel@tonic-gate 	gld_interface_t *ifp;
34270Sstevel@tonic-gate 	gld_mac_info_t *macinfo;
34280Sstevel@tonic-gate 	dl_unitdata_req_t *dludp;
34290Sstevel@tonic-gate 	mblk_t *nmp;
34300Sstevel@tonic-gate 	t_scalar_t off, len;
34310Sstevel@tonic-gate 	uint_t maclen;
34320Sstevel@tonic-gate 	int error;
34330Sstevel@tonic-gate 
34340Sstevel@tonic-gate 	if (gld->gld_state != DL_IDLE) {
34350Sstevel@tonic-gate 		miocnak(q, mp, 0, EINVAL);
34360Sstevel@tonic-gate 		return;
34370Sstevel@tonic-gate 	}
34380Sstevel@tonic-gate 
34390Sstevel@tonic-gate 	macinfo = gld->gld_mac_info;
34400Sstevel@tonic-gate 	ASSERT(macinfo != NULL);
34410Sstevel@tonic-gate 	maclen = macinfo->gldm_addrlen + abs(macinfo->gldm_saplen);
34420Sstevel@tonic-gate 
34430Sstevel@tonic-gate 	error = miocpullup(mp, sizeof (dl_unitdata_req_t) + maclen);
34440Sstevel@tonic-gate 	if (error != 0) {
34450Sstevel@tonic-gate 		miocnak(q, mp, 0, error);
34460Sstevel@tonic-gate 		return;
34470Sstevel@tonic-gate 	}
34480Sstevel@tonic-gate 
34490Sstevel@tonic-gate 	dludp = (dl_unitdata_req_t *)mp->b_cont->b_rptr;
34500Sstevel@tonic-gate 	off = dludp->dl_dest_addr_offset;
34510Sstevel@tonic-gate 	len = dludp->dl_dest_addr_length;
34520Sstevel@tonic-gate 	if (dludp->dl_primitive != DL_UNITDATA_REQ ||
34530Sstevel@tonic-gate 	    !MBLKIN(mp->b_cont, off, len) || len != maclen) {
34540Sstevel@tonic-gate 		miocnak(q, mp, 0, EINVAL);
34550Sstevel@tonic-gate 		return;
34560Sstevel@tonic-gate 	}
34570Sstevel@tonic-gate 
34580Sstevel@tonic-gate 	/*
34590Sstevel@tonic-gate 	 * We take his fastpath request as a declaration that he will accept
34600Sstevel@tonic-gate 	 * M_DATA messages from us, whether or not we are willing to accept
34610Sstevel@tonic-gate 	 * them from him.  This allows us to have fastpath in one direction
34620Sstevel@tonic-gate 	 * (flow upstream) even on media with Source Routing, where we are
34630Sstevel@tonic-gate 	 * unable to provide a fixed MAC header to be prepended to downstream
34640Sstevel@tonic-gate 	 * flowing packets.  So we set GLD_FAST whether or not we decide to
34650Sstevel@tonic-gate 	 * allow him to send M_DATA down to us.
34660Sstevel@tonic-gate 	 */
34670Sstevel@tonic-gate 	GLDM_LOCK(macinfo, RW_WRITER);
34680Sstevel@tonic-gate 	gld->gld_flags |= GLD_FAST;
34690Sstevel@tonic-gate 	GLDM_UNLOCK(macinfo);
34700Sstevel@tonic-gate 
34710Sstevel@tonic-gate 	ifp = ((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->interfacep;
34720Sstevel@tonic-gate 
34730Sstevel@tonic-gate 	/* This will fail for Source Routing media */
34740Sstevel@tonic-gate 	/* Also on Ethernet on 802.2 SAPs */
34750Sstevel@tonic-gate 	if ((nmp = (*ifp->mkfastpath)(gld, mp)) == NULL) {
34760Sstevel@tonic-gate 		miocnak(q, mp, 0, ENOMEM);
34770Sstevel@tonic-gate 		return;
34780Sstevel@tonic-gate 	}
34790Sstevel@tonic-gate 
34800Sstevel@tonic-gate 	/*
34810Sstevel@tonic-gate 	 * Link new mblk in after the "request" mblks.
34820Sstevel@tonic-gate 	 */
34830Sstevel@tonic-gate 	linkb(mp, nmp);
34840Sstevel@tonic-gate 	miocack(q, mp, msgdsize(mp->b_cont), 0);
34850Sstevel@tonic-gate }
34860Sstevel@tonic-gate 
34870Sstevel@tonic-gate /*
34880Sstevel@tonic-gate  * gld_cmds (q, mp)
34890Sstevel@tonic-gate  *	process the DL commands as defined in dlpi.h
34900Sstevel@tonic-gate  *	note that the primitives return status which is passed back
34910Sstevel@tonic-gate  *	to the service procedure.  If the value is GLDE_RETRY, then
34920Sstevel@tonic-gate  *	it is assumed that processing must stop and the primitive has
34930Sstevel@tonic-gate  *	been put back onto the queue.  If the value is any other error,
34940Sstevel@tonic-gate  *	then an error ack is generated by the service procedure.
34950Sstevel@tonic-gate  */
34960Sstevel@tonic-gate static int
34970Sstevel@tonic-gate gld_cmds(queue_t *q, mblk_t *mp)
34980Sstevel@tonic-gate {
34990Sstevel@tonic-gate 	union DL_primitives *dlp = (union DL_primitives *)mp->b_rptr;
35000Sstevel@tonic-gate 	gld_t *gld = (gld_t *)(q->q_ptr);
35010Sstevel@tonic-gate 	int result = DL_BADPRIM;
35020Sstevel@tonic-gate 	int mblkl = MBLKL(mp);
35030Sstevel@tonic-gate 	t_uscalar_t dlreq;
35040Sstevel@tonic-gate 
35050Sstevel@tonic-gate 	/* Make sure we have at least dlp->dl_primitive */
35060Sstevel@tonic-gate 	if (mblkl < sizeof (dlp->dl_primitive))
35070Sstevel@tonic-gate 		return (DL_BADPRIM);
35080Sstevel@tonic-gate 
35090Sstevel@tonic-gate 	dlreq = dlp->dl_primitive;
35100Sstevel@tonic-gate #ifdef	GLD_DEBUG
35110Sstevel@tonic-gate 	if (gld_debug & GLDTRACE)
35120Sstevel@tonic-gate 		cmn_err(CE_NOTE,
35130Sstevel@tonic-gate 		    "gld_cmds(%p, %p):dlp=%p, dlp->dl_primitive=%d",
35140Sstevel@tonic-gate 		    (void *)q, (void *)mp, (void *)dlp, dlreq);
35150Sstevel@tonic-gate #endif
35160Sstevel@tonic-gate 
35170Sstevel@tonic-gate 	switch (dlreq) {
35180Sstevel@tonic-gate 	case DL_UDQOS_REQ:
35190Sstevel@tonic-gate 		if (mblkl < DL_UDQOS_REQ_SIZE)
35200Sstevel@tonic-gate 			break;
35210Sstevel@tonic-gate 		result = gld_udqos(q, mp);
35220Sstevel@tonic-gate 		break;
35230Sstevel@tonic-gate 
35240Sstevel@tonic-gate 	case DL_BIND_REQ:
35250Sstevel@tonic-gate 		if (mblkl < DL_BIND_REQ_SIZE)
35260Sstevel@tonic-gate 			break;
35270Sstevel@tonic-gate 		result = gld_bind(q, mp);
35280Sstevel@tonic-gate 		break;
35290Sstevel@tonic-gate 
35300Sstevel@tonic-gate 	case DL_UNBIND_REQ:
35310Sstevel@tonic-gate 		if (mblkl < DL_UNBIND_REQ_SIZE)
35320Sstevel@tonic-gate 			break;
35330Sstevel@tonic-gate 		result = gld_unbind(q, mp);
35340Sstevel@tonic-gate 		break;
35350Sstevel@tonic-gate 
35360Sstevel@tonic-gate 	case DL_UNITDATA_REQ:
35370Sstevel@tonic-gate 		if (mblkl < DL_UNITDATA_REQ_SIZE)
35380Sstevel@tonic-gate 			break;
35390Sstevel@tonic-gate 		result = gld_unitdata(q, mp);
35400Sstevel@tonic-gate 		break;
35410Sstevel@tonic-gate 
35420Sstevel@tonic-gate 	case DL_INFO_REQ:
35430Sstevel@tonic-gate 		if (mblkl < DL_INFO_REQ_SIZE)
35440Sstevel@tonic-gate 			break;
35450Sstevel@tonic-gate 		result = gld_inforeq(q, mp);
35460Sstevel@tonic-gate 		break;
35470Sstevel@tonic-gate 
35480Sstevel@tonic-gate 	case DL_ATTACH_REQ:
35490Sstevel@tonic-gate 		if (mblkl < DL_ATTACH_REQ_SIZE)
35500Sstevel@tonic-gate 			break;
35510Sstevel@tonic-gate 		if (gld->gld_style == DL_STYLE2)
35520Sstevel@tonic-gate 			result = gldattach(q, mp);
35530Sstevel@tonic-gate 		else
35540Sstevel@tonic-gate 			result = DL_NOTSUPPORTED;
35550Sstevel@tonic-gate 		break;
35560Sstevel@tonic-gate 
35570Sstevel@tonic-gate 	case DL_DETACH_REQ:
35580Sstevel@tonic-gate 		if (mblkl < DL_DETACH_REQ_SIZE)
35590Sstevel@tonic-gate 			break;
35600Sstevel@tonic-gate 		if (gld->gld_style == DL_STYLE2)
35610Sstevel@tonic-gate 			result = gldunattach(q, mp);
35620Sstevel@tonic-gate 		else
35630Sstevel@tonic-gate 			result = DL_NOTSUPPORTED;
35640Sstevel@tonic-gate 		break;
35650Sstevel@tonic-gate 
35660Sstevel@tonic-gate 	case DL_ENABMULTI_REQ:
35670Sstevel@tonic-gate 		if (mblkl < DL_ENABMULTI_REQ_SIZE)
35680Sstevel@tonic-gate 			break;
35690Sstevel@tonic-gate 		result = gld_enable_multi(q, mp);
35700Sstevel@tonic-gate 		break;
35710Sstevel@tonic-gate 
35720Sstevel@tonic-gate 	case DL_DISABMULTI_REQ:
35730Sstevel@tonic-gate 		if (mblkl < DL_DISABMULTI_REQ_SIZE)
35740Sstevel@tonic-gate 			break;
35750Sstevel@tonic-gate 		result = gld_disable_multi(q, mp);
35760Sstevel@tonic-gate 		break;
35770Sstevel@tonic-gate 
35780Sstevel@tonic-gate 	case DL_PHYS_ADDR_REQ:
35790Sstevel@tonic-gate 		if (mblkl < DL_PHYS_ADDR_REQ_SIZE)
35800Sstevel@tonic-gate 			break;
35810Sstevel@tonic-gate 		result = gld_physaddr(q, mp);
35820Sstevel@tonic-gate 		break;
35830Sstevel@tonic-gate 
35840Sstevel@tonic-gate 	case DL_SET_PHYS_ADDR_REQ:
35850Sstevel@tonic-gate 		if (mblkl < DL_SET_PHYS_ADDR_REQ_SIZE)
35860Sstevel@tonic-gate 			break;
35870Sstevel@tonic-gate 		result = gld_setaddr(q, mp);
35880Sstevel@tonic-gate 		break;
35890Sstevel@tonic-gate 
35900Sstevel@tonic-gate 	case DL_PROMISCON_REQ:
35910Sstevel@tonic-gate 		if (mblkl < DL_PROMISCON_REQ_SIZE)
35920Sstevel@tonic-gate 			break;
35930Sstevel@tonic-gate 		result = gld_promisc(q, mp, dlreq, B_TRUE);
35940Sstevel@tonic-gate 		break;
35950Sstevel@tonic-gate 
35960Sstevel@tonic-gate 	case DL_PROMISCOFF_REQ:
35970Sstevel@tonic-gate 		if (mblkl < DL_PROMISCOFF_REQ_SIZE)
35980Sstevel@tonic-gate 			break;
35990Sstevel@tonic-gate 		result = gld_promisc(q, mp, dlreq, B_FALSE);
36000Sstevel@tonic-gate 		break;
36010Sstevel@tonic-gate 
36020Sstevel@tonic-gate 	case DL_GET_STATISTICS_REQ:
36030Sstevel@tonic-gate 		if (mblkl < DL_GET_STATISTICS_REQ_SIZE)
36040Sstevel@tonic-gate 			break;
36050Sstevel@tonic-gate 		result = gld_get_statistics(q, mp);
36060Sstevel@tonic-gate 		break;
36070Sstevel@tonic-gate 
36080Sstevel@tonic-gate 	case DL_CAPABILITY_REQ:
36090Sstevel@tonic-gate 		if (mblkl < DL_CAPABILITY_REQ_SIZE)
36100Sstevel@tonic-gate 			break;
36110Sstevel@tonic-gate 		result = gld_cap(q, mp);
36120Sstevel@tonic-gate 		break;
36130Sstevel@tonic-gate 
36140Sstevel@tonic-gate 	case DL_NOTIFY_REQ:
36150Sstevel@tonic-gate 		if (mblkl < DL_NOTIFY_REQ_SIZE)
36160Sstevel@tonic-gate 			break;
36170Sstevel@tonic-gate 		result = gld_notify_req(q, mp);
36180Sstevel@tonic-gate 		break;
36190Sstevel@tonic-gate 
36200Sstevel@tonic-gate 	case DL_XID_REQ:
36210Sstevel@tonic-gate 	case DL_XID_RES:
36220Sstevel@tonic-gate 	case DL_TEST_REQ:
36230Sstevel@tonic-gate 	case DL_TEST_RES:
36240Sstevel@tonic-gate 	case DL_CONTROL_REQ:
362556Smeem 	case DL_PASSIVE_REQ:
36260Sstevel@tonic-gate 		result = DL_NOTSUPPORTED;
36270Sstevel@tonic-gate 		break;
36280Sstevel@tonic-gate 
36290Sstevel@tonic-gate 	default:
36300Sstevel@tonic-gate #ifdef	GLD_DEBUG
36310Sstevel@tonic-gate 		if (gld_debug & GLDERRS)
36320Sstevel@tonic-gate 			cmn_err(CE_WARN,
36330Sstevel@tonic-gate 			    "gld_cmds: unknown M_PROTO message: %d",
36340Sstevel@tonic-gate 			    dlreq);
36350Sstevel@tonic-gate #endif
36360Sstevel@tonic-gate 		result = DL_BADPRIM;
36370Sstevel@tonic-gate 	}
36380Sstevel@tonic-gate 
36390Sstevel@tonic-gate 	return (result);
36400Sstevel@tonic-gate }
36410Sstevel@tonic-gate 
36420Sstevel@tonic-gate static int
36430Sstevel@tonic-gate gld_cap(queue_t *q, mblk_t *mp)
36440Sstevel@tonic-gate {
36450Sstevel@tonic-gate 	gld_t *gld = (gld_t *)q->q_ptr;
36460Sstevel@tonic-gate 	dl_capability_req_t *dlp = (dl_capability_req_t *)mp->b_rptr;
36470Sstevel@tonic-gate 
36480Sstevel@tonic-gate 	if (gld->gld_state == DL_UNATTACHED)
36490Sstevel@tonic-gate 		return (DL_OUTSTATE);
36500Sstevel@tonic-gate 
36510Sstevel@tonic-gate 	if (dlp->dl_sub_length == 0)
36520Sstevel@tonic-gate 		return (gld_cap_ack(q, mp));
36530Sstevel@tonic-gate 
36540Sstevel@tonic-gate 	return (gld_cap_enable(q, mp));
36550Sstevel@tonic-gate }
36560Sstevel@tonic-gate 
36570Sstevel@tonic-gate static int
36580Sstevel@tonic-gate gld_cap_ack(queue_t *q, mblk_t *mp)
36590Sstevel@tonic-gate {
36600Sstevel@tonic-gate 	gld_t *gld = (gld_t *)q->q_ptr;
36610Sstevel@tonic-gate 	gld_mac_info_t *macinfo = gld->gld_mac_info;
36620Sstevel@tonic-gate 	gld_interface_t *ifp;
36630Sstevel@tonic-gate 	dl_capability_ack_t *dlap;
36640Sstevel@tonic-gate 	dl_capability_sub_t *dlsp;
36650Sstevel@tonic-gate 	size_t size = sizeof (dl_capability_ack_t);
36660Sstevel@tonic-gate 	size_t subsize = 0;
36670Sstevel@tonic-gate 
36680Sstevel@tonic-gate 	ifp = ((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->interfacep;
36690Sstevel@tonic-gate 
36700Sstevel@tonic-gate 	if (macinfo->gldm_capabilities & GLD_CAP_CKSUM_ANY)
36710Sstevel@tonic-gate 		subsize += sizeof (dl_capability_sub_t) +
36720Sstevel@tonic-gate 		    sizeof (dl_capab_hcksum_t);
36730Sstevel@tonic-gate 	if (macinfo->gldm_capabilities & GLD_CAP_ZEROCOPY)
36740Sstevel@tonic-gate 		subsize += sizeof (dl_capability_sub_t) +
36750Sstevel@tonic-gate 		    sizeof (dl_capab_zerocopy_t);
36760Sstevel@tonic-gate 	if (macinfo->gldm_options & GLDOPT_MDT)
36770Sstevel@tonic-gate 		subsize += (sizeof (dl_capability_sub_t) +
36780Sstevel@tonic-gate 		    sizeof (dl_capab_mdt_t));
36790Sstevel@tonic-gate 
36800Sstevel@tonic-gate 	if ((mp = mexchange(q, mp, size + subsize, M_PROTO,
36810Sstevel@tonic-gate 	    DL_CAPABILITY_ACK)) == NULL)
36820Sstevel@tonic-gate 		return (GLDE_OK);
36830Sstevel@tonic-gate 
36840Sstevel@tonic-gate 	dlap = (dl_capability_ack_t *)mp->b_rptr;
36850Sstevel@tonic-gate 	dlap->dl_sub_offset = 0;
36860Sstevel@tonic-gate 	if ((dlap->dl_sub_length = subsize) != 0)
36870Sstevel@tonic-gate 		dlap->dl_sub_offset = sizeof (dl_capability_ack_t);
36880Sstevel@tonic-gate 	dlsp = (dl_capability_sub_t *)&dlap[1];
36890Sstevel@tonic-gate 
36900Sstevel@tonic-gate 	if (macinfo->gldm_capabilities & GLD_CAP_CKSUM_ANY) {
36910Sstevel@tonic-gate 		dl_capab_hcksum_t *dlhp = (dl_capab_hcksum_t *)&dlsp[1];
36920Sstevel@tonic-gate 
36930Sstevel@tonic-gate 		dlsp->dl_cap = DL_CAPAB_HCKSUM;
36940Sstevel@tonic-gate 		dlsp->dl_length = sizeof (dl_capab_hcksum_t);
36950Sstevel@tonic-gate 
36960Sstevel@tonic-gate 		dlhp->hcksum_version = HCKSUM_VERSION_1;
36970Sstevel@tonic-gate 
36980Sstevel@tonic-gate 		dlhp->hcksum_txflags = 0;
36990Sstevel@tonic-gate 		if (macinfo->gldm_capabilities & GLD_CAP_CKSUM_PARTIAL)
37000Sstevel@tonic-gate 			dlhp->hcksum_txflags |= HCKSUM_INET_PARTIAL;
37010Sstevel@tonic-gate 		if (macinfo->gldm_capabilities & GLD_CAP_CKSUM_FULL_V4)
37020Sstevel@tonic-gate 			dlhp->hcksum_txflags |= HCKSUM_INET_FULL_V4;
3703741Smasputra 		if (macinfo->gldm_capabilities & GLD_CAP_CKSUM_FULL_V6)
3704741Smasputra 			dlhp->hcksum_txflags |= HCKSUM_INET_FULL_V6;
37050Sstevel@tonic-gate 		if (macinfo->gldm_capabilities & GLD_CAP_CKSUM_IPHDR)
37060Sstevel@tonic-gate 			dlhp->hcksum_txflags |= HCKSUM_IPHDRCKSUM;
37070Sstevel@tonic-gate 
37080Sstevel@tonic-gate 		dlcapabsetqid(&(dlhp->hcksum_mid), RD(q));
37090Sstevel@tonic-gate 		dlsp = (dl_capability_sub_t *)&dlhp[1];
37100Sstevel@tonic-gate 	}
37110Sstevel@tonic-gate 
37120Sstevel@tonic-gate 	if (macinfo->gldm_capabilities & GLD_CAP_ZEROCOPY) {
37130Sstevel@tonic-gate 		dl_capab_zerocopy_t *dlzp = (dl_capab_zerocopy_t *)&dlsp[1];
37140Sstevel@tonic-gate 
37150Sstevel@tonic-gate 		dlsp->dl_cap = DL_CAPAB_ZEROCOPY;
37160Sstevel@tonic-gate 		dlsp->dl_length = sizeof (dl_capab_zerocopy_t);
37170Sstevel@tonic-gate 		dlzp->zerocopy_version = ZEROCOPY_VERSION_1;
37180Sstevel@tonic-gate 		dlzp->zerocopy_flags = DL_CAPAB_VMSAFE_MEM;
37190Sstevel@tonic-gate 
37200Sstevel@tonic-gate 		dlcapabsetqid(&(dlzp->zerocopy_mid), RD(q));
37210Sstevel@tonic-gate 		dlsp = (dl_capability_sub_t *)&dlzp[1];
37220Sstevel@tonic-gate 	}
37230Sstevel@tonic-gate 
37240Sstevel@tonic-gate 	if (macinfo->gldm_options & GLDOPT_MDT) {
37250Sstevel@tonic-gate 		dl_capab_mdt_t *dlmp = (dl_capab_mdt_t *)&dlsp[1];
37260Sstevel@tonic-gate 
37270Sstevel@tonic-gate 		dlsp->dl_cap = DL_CAPAB_MDT;
37280Sstevel@tonic-gate 		dlsp->dl_length = sizeof (dl_capab_mdt_t);
37290Sstevel@tonic-gate 
37300Sstevel@tonic-gate 		dlmp->mdt_version = MDT_VERSION_2;
37310Sstevel@tonic-gate 		dlmp->mdt_max_pld = macinfo->gldm_mdt_segs;
37320Sstevel@tonic-gate 		dlmp->mdt_span_limit = macinfo->gldm_mdt_sgl;
37330Sstevel@tonic-gate 		dlcapabsetqid(&dlmp->mdt_mid, OTHERQ(q));
37340Sstevel@tonic-gate 		dlmp->mdt_flags = DL_CAPAB_MDT_ENABLE;
37350Sstevel@tonic-gate 		dlmp->mdt_hdr_head = ifp->hdr_size;
37360Sstevel@tonic-gate 		dlmp->mdt_hdr_tail = 0;
37370Sstevel@tonic-gate 	}
37380Sstevel@tonic-gate 
37390Sstevel@tonic-gate 	qreply(q, mp);
37400Sstevel@tonic-gate 	return (GLDE_OK);
37410Sstevel@tonic-gate }
37420Sstevel@tonic-gate 
37430Sstevel@tonic-gate static int
37440Sstevel@tonic-gate gld_cap_enable(queue_t *q, mblk_t *mp)
37450Sstevel@tonic-gate {
37460Sstevel@tonic-gate 	dl_capability_req_t *dlp;
37470Sstevel@tonic-gate 	dl_capability_sub_t *dlsp;
37480Sstevel@tonic-gate 	dl_capab_hcksum_t *dlhp;
37490Sstevel@tonic-gate 	offset_t off;
37500Sstevel@tonic-gate 	size_t len;
37510Sstevel@tonic-gate 	size_t size;
37520Sstevel@tonic-gate 	offset_t end;
37530Sstevel@tonic-gate 
37540Sstevel@tonic-gate 	dlp = (dl_capability_req_t *)mp->b_rptr;
37550Sstevel@tonic-gate 	dlp->dl_primitive = DL_CAPABILITY_ACK;
37560Sstevel@tonic-gate 
37570Sstevel@tonic-gate 	off = dlp->dl_sub_offset;
37580Sstevel@tonic-gate 	len = dlp->dl_sub_length;
37590Sstevel@tonic-gate 
37600Sstevel@tonic-gate 	if (!MBLKIN(mp, off, len))
37610Sstevel@tonic-gate 		return (DL_BADPRIM);
37620Sstevel@tonic-gate 
37630Sstevel@tonic-gate 	end = off + len;
37640Sstevel@tonic-gate 	while (off < end) {
37650Sstevel@tonic-gate 		dlsp = (dl_capability_sub_t *)(mp->b_rptr + off);
37660Sstevel@tonic-gate 		size = sizeof (dl_capability_sub_t) + dlsp->dl_length;
37670Sstevel@tonic-gate 		if (off + size > end)
37680Sstevel@tonic-gate 			return (DL_BADPRIM);
37690Sstevel@tonic-gate 
37700Sstevel@tonic-gate 		switch (dlsp->dl_cap) {
37710Sstevel@tonic-gate 		case DL_CAPAB_HCKSUM:
37720Sstevel@tonic-gate 			dlhp = (dl_capab_hcksum_t *)&dlsp[1];
37730Sstevel@tonic-gate 			/* nothing useful we can do with the contents */
37740Sstevel@tonic-gate 			dlcapabsetqid(&(dlhp->hcksum_mid), RD(q));
37750Sstevel@tonic-gate 			break;
37760Sstevel@tonic-gate 		default:
37770Sstevel@tonic-gate 			break;
37780Sstevel@tonic-gate 		}
37790Sstevel@tonic-gate 
37800Sstevel@tonic-gate 		off += size;
37810Sstevel@tonic-gate 	}
37820Sstevel@tonic-gate 
37830Sstevel@tonic-gate 	qreply(q, mp);
37840Sstevel@tonic-gate 	return (GLDE_OK);
37850Sstevel@tonic-gate }
37860Sstevel@tonic-gate 
37870Sstevel@tonic-gate /*
37880Sstevel@tonic-gate  * Send a copy of the DL_NOTIFY_IND message <mp> to each stream that has
37890Sstevel@tonic-gate  * requested the specific <notification> that the message carries AND is
37900Sstevel@tonic-gate  * eligible and ready to receive the notification immediately.
37910Sstevel@tonic-gate  *
37920Sstevel@tonic-gate  * This routine ignores flow control. Notifications will be sent regardless.
37930Sstevel@tonic-gate  *
37940Sstevel@tonic-gate  * In all cases, the original message passed in is freed at the end of
37950Sstevel@tonic-gate  * the routine.
37960Sstevel@tonic-gate  */
37970Sstevel@tonic-gate static void
37980Sstevel@tonic-gate gld_notify_qs(gld_mac_info_t *macinfo, mblk_t *mp, uint32_t notification)
37990Sstevel@tonic-gate {
38000Sstevel@tonic-gate 	gld_mac_pvt_t *mac_pvt;
38010Sstevel@tonic-gate 	gld_vlan_t *vlan;
38020Sstevel@tonic-gate 	gld_t *gld;
38030Sstevel@tonic-gate 	mblk_t *nmp;
38040Sstevel@tonic-gate 	int i;
38050Sstevel@tonic-gate 
38060Sstevel@tonic-gate 	ASSERT(GLDM_LOCK_HELD_WRITE(macinfo));
38070Sstevel@tonic-gate 
38080Sstevel@tonic-gate 	mac_pvt = (gld_mac_pvt_t *)macinfo->gldm_mac_pvt;
38090Sstevel@tonic-gate 
38100Sstevel@tonic-gate 	/*
38110Sstevel@tonic-gate 	 * Search all the streams attached to this macinfo looking
38120Sstevel@tonic-gate 	 * for those eligible to receive the present notification.
38130Sstevel@tonic-gate 	 */
38140Sstevel@tonic-gate 	for (i = 0; i < VLAN_HASHSZ; i++) {
38150Sstevel@tonic-gate 		for (vlan = mac_pvt->vlan_hash[i];
38160Sstevel@tonic-gate 		    vlan != NULL; vlan = vlan->gldv_next) {
38170Sstevel@tonic-gate 			for (gld = vlan->gldv_str_next;
38180Sstevel@tonic-gate 			    gld != (gld_t *)&vlan->gldv_str_next;
38190Sstevel@tonic-gate 			    gld = gld->gld_next) {
38200Sstevel@tonic-gate 				ASSERT(gld->gld_qptr != NULL);
38210Sstevel@tonic-gate 				ASSERT(gld->gld_state == DL_IDLE ||
38220Sstevel@tonic-gate 				    gld->gld_state == DL_UNBOUND);
38230Sstevel@tonic-gate 				ASSERT(gld->gld_mac_info == macinfo);
38240Sstevel@tonic-gate 
38250Sstevel@tonic-gate 				if (gld->gld_flags & GLD_STR_CLOSING)
38260Sstevel@tonic-gate 					continue; /* not eligible - skip */
38270Sstevel@tonic-gate 				if (!(notification & gld->gld_notifications))
38280Sstevel@tonic-gate 					continue; /* not wanted - skip */
38290Sstevel@tonic-gate 				if ((nmp = dupmsg(mp)) == NULL)
38300Sstevel@tonic-gate 					continue; /* can't copy - skip */
38310Sstevel@tonic-gate 
38320Sstevel@tonic-gate 				/*
38330Sstevel@tonic-gate 				 * All OK; send dup'd notification up this
38340Sstevel@tonic-gate 				 * stream
38350Sstevel@tonic-gate 				 */
38360Sstevel@tonic-gate 				qreply(WR(gld->gld_qptr), nmp);
38370Sstevel@tonic-gate 			}
38380Sstevel@tonic-gate 		}
38390Sstevel@tonic-gate 	}
38400Sstevel@tonic-gate 
38410Sstevel@tonic-gate 	/*
38420Sstevel@tonic-gate 	 * Drop the original message block now
38430Sstevel@tonic-gate 	 */
38440Sstevel@tonic-gate 	freemsg(mp);
38450Sstevel@tonic-gate }
38460Sstevel@tonic-gate 
38470Sstevel@tonic-gate /*
38480Sstevel@tonic-gate  * For each (understood) bit in the <notifications> argument, contruct
38490Sstevel@tonic-gate  * a DL_NOTIFY_IND message and send it to the specified <q>, or to all
38500Sstevel@tonic-gate  * eligible queues if <q> is NULL.
38510Sstevel@tonic-gate  */
38520Sstevel@tonic-gate static void
38530Sstevel@tonic-gate gld_notify_ind(gld_mac_info_t *macinfo, uint32_t notifications, queue_t *q)
38540Sstevel@tonic-gate {
38550Sstevel@tonic-gate 	gld_mac_pvt_t *mac_pvt;
38560Sstevel@tonic-gate 	dl_notify_ind_t *dlnip;
38570Sstevel@tonic-gate 	struct gld_stats *stats;
38580Sstevel@tonic-gate 	mblk_t *mp;
38590Sstevel@tonic-gate 	size_t size;
38600Sstevel@tonic-gate 	uint32_t bit;
38610Sstevel@tonic-gate 
38620Sstevel@tonic-gate 	GLDM_LOCK(macinfo, RW_WRITER);
38630Sstevel@tonic-gate 
38640Sstevel@tonic-gate 	/*
38650Sstevel@tonic-gate 	 * The following cases shouldn't happen, but just in case the
38660Sstevel@tonic-gate 	 * MAC driver calls gld_linkstate() at an inappropriate time, we
38670Sstevel@tonic-gate 	 * check anyway ...
38680Sstevel@tonic-gate 	 */
38690Sstevel@tonic-gate 	if (!(macinfo->gldm_GLD_flags & GLD_MAC_READY)) {
38700Sstevel@tonic-gate 		GLDM_UNLOCK(macinfo);
38710Sstevel@tonic-gate 		return;				/* not ready yet	*/
38720Sstevel@tonic-gate 	}
38730Sstevel@tonic-gate 
38740Sstevel@tonic-gate 	if (macinfo->gldm_GLD_flags & GLD_UNREGISTERED) {
38750Sstevel@tonic-gate 		GLDM_UNLOCK(macinfo);
38760Sstevel@tonic-gate 		return;				/* not ready anymore	*/
38770Sstevel@tonic-gate 	}
38780Sstevel@tonic-gate 
38790Sstevel@tonic-gate 	/*
38800Sstevel@tonic-gate 	 * Make sure the kstats are up to date, 'cos we use some of
38810Sstevel@tonic-gate 	 * the kstat values below, specifically the link speed ...
38820Sstevel@tonic-gate 	 */
38830Sstevel@tonic-gate 	mac_pvt = (gld_mac_pvt_t *)macinfo->gldm_mac_pvt;
38840Sstevel@tonic-gate 	stats = mac_pvt->statistics;
38850Sstevel@tonic-gate 	if (macinfo->gldm_get_stats)
38860Sstevel@tonic-gate 		(void) (*macinfo->gldm_get_stats)(macinfo, stats);
38870Sstevel@tonic-gate 
38880Sstevel@tonic-gate 	for (bit = 1; notifications != 0; bit <<= 1) {
38890Sstevel@tonic-gate 		if ((notifications & bit) == 0)
38900Sstevel@tonic-gate 			continue;
38910Sstevel@tonic-gate 		notifications &= ~bit;
38920Sstevel@tonic-gate 
38930Sstevel@tonic-gate 		size = DL_NOTIFY_IND_SIZE;
38940Sstevel@tonic-gate 		if (bit == DL_NOTE_PHYS_ADDR)
38950Sstevel@tonic-gate 			size += macinfo->gldm_addrlen;
38960Sstevel@tonic-gate 		if ((mp = allocb(size, BPRI_MED)) == NULL)
38970Sstevel@tonic-gate 			continue;
38980Sstevel@tonic-gate 
38990Sstevel@tonic-gate 		mp->b_datap->db_type = M_PROTO;
39000Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + size;
39010Sstevel@tonic-gate 		dlnip = (dl_notify_ind_t *)mp->b_rptr;
39020Sstevel@tonic-gate 		dlnip->dl_primitive = DL_NOTIFY_IND;
39030Sstevel@tonic-gate 		dlnip->dl_notification = 0;
39040Sstevel@tonic-gate 		dlnip->dl_data = 0;
39050Sstevel@tonic-gate 		dlnip->dl_addr_length = 0;
39060Sstevel@tonic-gate 		dlnip->dl_addr_offset = 0;
39070Sstevel@tonic-gate 
39080Sstevel@tonic-gate 		switch (bit) {
39090Sstevel@tonic-gate 		case DL_NOTE_PROMISC_ON_PHYS:
39100Sstevel@tonic-gate 		case DL_NOTE_PROMISC_OFF_PHYS:
39110Sstevel@tonic-gate 			if (mac_pvt->nprom != 0)
39120Sstevel@tonic-gate 				dlnip->dl_notification = bit;
39130Sstevel@tonic-gate 			break;
39140Sstevel@tonic-gate 
39150Sstevel@tonic-gate 		case DL_NOTE_LINK_DOWN:
39160Sstevel@tonic-gate 			if (macinfo->gldm_linkstate == GLD_LINKSTATE_DOWN)
39170Sstevel@tonic-gate 				dlnip->dl_notification = bit;
39180Sstevel@tonic-gate 			break;
39190Sstevel@tonic-gate 
39200Sstevel@tonic-gate 		case DL_NOTE_LINK_UP:
39210Sstevel@tonic-gate 			if (macinfo->gldm_linkstate == GLD_LINKSTATE_UP)
39220Sstevel@tonic-gate 				dlnip->dl_notification = bit;
39230Sstevel@tonic-gate 			break;
39240Sstevel@tonic-gate 
39250Sstevel@tonic-gate 		case DL_NOTE_SPEED:
39260Sstevel@tonic-gate 			/*
39270Sstevel@tonic-gate 			 * Conversion required here:
39280Sstevel@tonic-gate 			 *	GLD keeps the speed in bit/s in a uint64
39290Sstevel@tonic-gate 			 *	DLPI wants it in kb/s in a uint32
39300Sstevel@tonic-gate 			 * Fortunately this is still big enough for 10Gb/s!
39310Sstevel@tonic-gate 			 */
39320Sstevel@tonic-gate 			dlnip->dl_notification = bit;
39330Sstevel@tonic-gate 			dlnip->dl_data = stats->glds_speed/1000ULL;
39340Sstevel@tonic-gate 			break;
39350Sstevel@tonic-gate 
39360Sstevel@tonic-gate 		case DL_NOTE_PHYS_ADDR:
39370Sstevel@tonic-gate 			dlnip->dl_notification = bit;
39380Sstevel@tonic-gate 			dlnip->dl_data = DL_CURR_PHYS_ADDR;
39390Sstevel@tonic-gate 			dlnip->dl_addr_offset = sizeof (dl_notify_ind_t);
39400Sstevel@tonic-gate 			dlnip->dl_addr_length = macinfo->gldm_addrlen +
39410Sstevel@tonic-gate 			    abs(macinfo->gldm_saplen);
39420Sstevel@tonic-gate 			mac_pvt = (gld_mac_pvt_t *)macinfo->gldm_mac_pvt;
39430Sstevel@tonic-gate 			mac_copy(mac_pvt->curr_macaddr,
39440Sstevel@tonic-gate 			    mp->b_rptr + sizeof (dl_notify_ind_t),
39450Sstevel@tonic-gate 			    macinfo->gldm_addrlen);
39460Sstevel@tonic-gate 			break;
39470Sstevel@tonic-gate 
39480Sstevel@tonic-gate 		default:
39490Sstevel@tonic-gate 			break;
39500Sstevel@tonic-gate 		}
39510Sstevel@tonic-gate 
39520Sstevel@tonic-gate 		if (dlnip->dl_notification == 0)
39530Sstevel@tonic-gate 			freemsg(mp);
39540Sstevel@tonic-gate 		else if (q != NULL)
39550Sstevel@tonic-gate 			qreply(q, mp);
39560Sstevel@tonic-gate 		else
39570Sstevel@tonic-gate 			gld_notify_qs(macinfo, mp, bit);
39580Sstevel@tonic-gate 	}
39590Sstevel@tonic-gate 
39600Sstevel@tonic-gate 	GLDM_UNLOCK(macinfo);
39610Sstevel@tonic-gate }
39620Sstevel@tonic-gate 
39630Sstevel@tonic-gate /*
39640Sstevel@tonic-gate  * gld_notify_req - handle a DL_NOTIFY_REQ message
39650Sstevel@tonic-gate  */
39660Sstevel@tonic-gate static int
39670Sstevel@tonic-gate gld_notify_req(queue_t *q, mblk_t *mp)
39680Sstevel@tonic-gate {
39690Sstevel@tonic-gate 	gld_t *gld = (gld_t *)q->q_ptr;
39700Sstevel@tonic-gate 	gld_mac_info_t *macinfo;
39710Sstevel@tonic-gate 	gld_mac_pvt_t *pvt;
39720Sstevel@tonic-gate 	dl_notify_req_t *dlnrp;
39730Sstevel@tonic-gate 	dl_notify_ack_t *dlnap;
39740Sstevel@tonic-gate 
39750Sstevel@tonic-gate 	ASSERT(gld != NULL);
39760Sstevel@tonic-gate 	ASSERT(gld->gld_qptr == RD(q));
39770Sstevel@tonic-gate 
39780Sstevel@tonic-gate 	dlnrp = (dl_notify_req_t *)mp->b_rptr;
39790Sstevel@tonic-gate 
39800Sstevel@tonic-gate #ifdef GLD_DEBUG
39810Sstevel@tonic-gate 	if (gld_debug & GLDTRACE)
39820Sstevel@tonic-gate 		cmn_err(CE_NOTE, "gld_notify_req(%p %p)",
39830Sstevel@tonic-gate 			(void *)q, (void *)mp);
39840Sstevel@tonic-gate #endif
39850Sstevel@tonic-gate 
39860Sstevel@tonic-gate 	if (gld->gld_state == DL_UNATTACHED) {
39870Sstevel@tonic-gate #ifdef GLD_DEBUG
39880Sstevel@tonic-gate 		if (gld_debug & GLDERRS)
39890Sstevel@tonic-gate 			cmn_err(CE_NOTE, "gld_notify_req: wrong state (%d)",
39900Sstevel@tonic-gate 				gld->gld_state);
39910Sstevel@tonic-gate #endif
39920Sstevel@tonic-gate 		return (DL_OUTSTATE);
39930Sstevel@tonic-gate 	}
39940Sstevel@tonic-gate 
39950Sstevel@tonic-gate 	/*
39960Sstevel@tonic-gate 	 * Remember what notifications are required by this stream
39970Sstevel@tonic-gate 	 */
39980Sstevel@tonic-gate 	macinfo = gld->gld_mac_info;
39990Sstevel@tonic-gate 	pvt = (gld_mac_pvt_t *)macinfo->gldm_mac_pvt;
40000Sstevel@tonic-gate 
40010Sstevel@tonic-gate 	gld->gld_notifications = dlnrp->dl_notifications & pvt->notifications;
40020Sstevel@tonic-gate 
40030Sstevel@tonic-gate 	/*
40040Sstevel@tonic-gate 	 * The return DL_NOTIFY_ACK carries the bitset of notifications
40050Sstevel@tonic-gate 	 * that this driver can provide, independently of which ones have
40060Sstevel@tonic-gate 	 * previously been or are now being requested.
40070Sstevel@tonic-gate 	 */
40080Sstevel@tonic-gate 	if ((mp = mexchange(q, mp, sizeof (dl_notify_ack_t), M_PCPROTO,
40090Sstevel@tonic-gate 	    DL_NOTIFY_ACK)) == NULL)
40100Sstevel@tonic-gate 		return (DL_SYSERR);
40110Sstevel@tonic-gate 
40120Sstevel@tonic-gate 	dlnap = (dl_notify_ack_t *)mp->b_rptr;
40130Sstevel@tonic-gate 	dlnap->dl_notifications = pvt->notifications;
40140Sstevel@tonic-gate 	qreply(q, mp);
40150Sstevel@tonic-gate 
40160Sstevel@tonic-gate 	/*
40170Sstevel@tonic-gate 	 * A side effect of a DL_NOTIFY_REQ is that after the DL_NOTIFY_ACK
40180Sstevel@tonic-gate 	 * reply, the the requestor gets zero or more DL_NOTIFY_IND messages
40190Sstevel@tonic-gate 	 * that provide the current status.
40200Sstevel@tonic-gate 	 */
40210Sstevel@tonic-gate 	gld_notify_ind(macinfo, gld->gld_notifications, q);
40220Sstevel@tonic-gate 
40230Sstevel@tonic-gate 	return (GLDE_OK);
40240Sstevel@tonic-gate }
40250Sstevel@tonic-gate 
40260Sstevel@tonic-gate /*
40270Sstevel@tonic-gate  * gld_linkstate()
40280Sstevel@tonic-gate  *	Called by driver to tell GLD the state of the physical link.
40290Sstevel@tonic-gate  *	As a side effect, sends a DL_NOTE_LINK_UP or DL_NOTE_LINK_DOWN
40300Sstevel@tonic-gate  *	notification to each client that has previously requested such
40310Sstevel@tonic-gate  *	notifications
40320Sstevel@tonic-gate  */
40330Sstevel@tonic-gate void
40340Sstevel@tonic-gate gld_linkstate(gld_mac_info_t *macinfo, int32_t newstate)
40350Sstevel@tonic-gate {
40360Sstevel@tonic-gate 	uint32_t notification;
40370Sstevel@tonic-gate 
40380Sstevel@tonic-gate 	switch (newstate) {
40390Sstevel@tonic-gate 	default:
40400Sstevel@tonic-gate 		return;
40410Sstevel@tonic-gate 
40420Sstevel@tonic-gate 	case GLD_LINKSTATE_DOWN:
40430Sstevel@tonic-gate 		notification = DL_NOTE_LINK_DOWN;
40440Sstevel@tonic-gate 		break;
40450Sstevel@tonic-gate 
40460Sstevel@tonic-gate 	case GLD_LINKSTATE_UP:
40470Sstevel@tonic-gate 		notification = DL_NOTE_LINK_UP | DL_NOTE_SPEED;
40480Sstevel@tonic-gate 		break;
40490Sstevel@tonic-gate 
40500Sstevel@tonic-gate 	case GLD_LINKSTATE_UNKNOWN:
40510Sstevel@tonic-gate 		notification = 0;
40520Sstevel@tonic-gate 		break;
40530Sstevel@tonic-gate 	}
40540Sstevel@tonic-gate 
40550Sstevel@tonic-gate 	GLDM_LOCK(macinfo, RW_WRITER);
40560Sstevel@tonic-gate 	if (macinfo->gldm_linkstate == newstate)
40570Sstevel@tonic-gate 		notification = 0;
40580Sstevel@tonic-gate 	else
40590Sstevel@tonic-gate 		macinfo->gldm_linkstate = newstate;
40600Sstevel@tonic-gate 	GLDM_UNLOCK(macinfo);
40610Sstevel@tonic-gate 
40620Sstevel@tonic-gate 	if (notification)
40630Sstevel@tonic-gate 		gld_notify_ind(macinfo, notification, NULL);
40640Sstevel@tonic-gate }
40650Sstevel@tonic-gate 
40660Sstevel@tonic-gate /*
40670Sstevel@tonic-gate  * gld_udqos - set the current QoS parameters (priority only at the moment).
40680Sstevel@tonic-gate  */
40690Sstevel@tonic-gate static int
40700Sstevel@tonic-gate gld_udqos(queue_t *q, mblk_t *mp)
40710Sstevel@tonic-gate {
40720Sstevel@tonic-gate 	dl_udqos_req_t *dlp;
40730Sstevel@tonic-gate 	gld_t  *gld = (gld_t *)q->q_ptr;
40740Sstevel@tonic-gate 	int off;
40750Sstevel@tonic-gate 	int len;
40760Sstevel@tonic-gate 	dl_qos_cl_sel1_t *selp;
40770Sstevel@tonic-gate 
40780Sstevel@tonic-gate 	ASSERT(gld);
40790Sstevel@tonic-gate 	ASSERT(gld->gld_qptr == RD(q));
40800Sstevel@tonic-gate 
40810Sstevel@tonic-gate #ifdef GLD_DEBUG
40820Sstevel@tonic-gate 	if (gld_debug & GLDTRACE)
40830Sstevel@tonic-gate 		cmn_err(CE_NOTE, "gld_udqos(%p %p)", (void *)q, (void *)mp);
40840Sstevel@tonic-gate #endif
40850Sstevel@tonic-gate 
40860Sstevel@tonic-gate 	if (gld->gld_state != DL_IDLE) {
40870Sstevel@tonic-gate #ifdef GLD_DEBUG
40880Sstevel@tonic-gate 		if (gld_debug & GLDERRS)
40890Sstevel@tonic-gate 			cmn_err(CE_NOTE, "gld_udqos: wrong state (%d)",
40900Sstevel@tonic-gate 			    gld->gld_state);
40910Sstevel@tonic-gate #endif
40920Sstevel@tonic-gate 		return (DL_OUTSTATE);
40930Sstevel@tonic-gate 	}
40940Sstevel@tonic-gate 
40950Sstevel@tonic-gate 	dlp = (dl_udqos_req_t *)mp->b_rptr;
40960Sstevel@tonic-gate 	off = dlp->dl_qos_offset;
40970Sstevel@tonic-gate 	len = dlp->dl_qos_length;
40980Sstevel@tonic-gate 
40990Sstevel@tonic-gate 	if (len != sizeof (dl_qos_cl_sel1_t) || !MBLKIN(mp, off, len))
41000Sstevel@tonic-gate 		return (DL_BADQOSTYPE);
41010Sstevel@tonic-gate 
41020Sstevel@tonic-gate 	selp = (dl_qos_cl_sel1_t *)(mp->b_rptr + off);
41030Sstevel@tonic-gate 	if (selp->dl_qos_type != DL_QOS_CL_SEL1)
41040Sstevel@tonic-gate 		return (DL_BADQOSTYPE);
41050Sstevel@tonic-gate 
41060Sstevel@tonic-gate 	if (selp->dl_trans_delay != 0 &&
41070Sstevel@tonic-gate 	    selp->dl_trans_delay != DL_QOS_DONT_CARE)
41080Sstevel@tonic-gate 		return (DL_BADQOSPARAM);
41090Sstevel@tonic-gate 	if (selp->dl_protection != 0 &&
41100Sstevel@tonic-gate 	    selp->dl_protection != DL_QOS_DONT_CARE)
41110Sstevel@tonic-gate 		return (DL_BADQOSPARAM);
41120Sstevel@tonic-gate 	if (selp->dl_residual_error != 0 &&
41130Sstevel@tonic-gate 	    selp->dl_residual_error != DL_QOS_DONT_CARE)
41140Sstevel@tonic-gate 		return (DL_BADQOSPARAM);
41150Sstevel@tonic-gate 	if (selp->dl_priority < 0 || selp->dl_priority > 7)
41160Sstevel@tonic-gate 		return (DL_BADQOSPARAM);
41170Sstevel@tonic-gate 
41180Sstevel@tonic-gate 	gld->gld_upri = selp->dl_priority;
41190Sstevel@tonic-gate 
41200Sstevel@tonic-gate 	dlokack(q, mp, DL_UDQOS_REQ);
41210Sstevel@tonic-gate 	return (GLDE_OK);
41220Sstevel@tonic-gate }
41230Sstevel@tonic-gate 
41240Sstevel@tonic-gate static mblk_t *
41250Sstevel@tonic-gate gld_bindack(queue_t *q, mblk_t *mp)
41260Sstevel@tonic-gate {
41270Sstevel@tonic-gate 	gld_t *gld = (gld_t *)q->q_ptr;
41280Sstevel@tonic-gate 	gld_mac_info_t *macinfo = gld->gld_mac_info;
41290Sstevel@tonic-gate 	gld_mac_pvt_t *mac_pvt = (gld_mac_pvt_t *)macinfo->gldm_mac_pvt;
41300Sstevel@tonic-gate 	dl_bind_ack_t *dlp;
41310Sstevel@tonic-gate 	size_t size;
41320Sstevel@tonic-gate 	t_uscalar_t addrlen;
41330Sstevel@tonic-gate 	uchar_t *sapp;
41340Sstevel@tonic-gate 
41350Sstevel@tonic-gate 	addrlen = macinfo->gldm_addrlen + abs(macinfo->gldm_saplen);
41360Sstevel@tonic-gate 	size = sizeof (dl_bind_ack_t) + addrlen;
41370Sstevel@tonic-gate 	if ((mp = mexchange(q, mp, size, M_PCPROTO, DL_BIND_ACK)) == NULL)
41380Sstevel@tonic-gate 		return (NULL);
41390Sstevel@tonic-gate 
41400Sstevel@tonic-gate 	dlp = (dl_bind_ack_t *)mp->b_rptr;
41410Sstevel@tonic-gate 	dlp->dl_sap = gld->gld_sap;
41420Sstevel@tonic-gate 	dlp->dl_addr_length = addrlen;
41430Sstevel@tonic-gate 	dlp->dl_addr_offset = sizeof (dl_bind_ack_t);
41440Sstevel@tonic-gate 	dlp->dl_max_conind = 0;
41450Sstevel@tonic-gate 	dlp->dl_xidtest_flg = 0;
41460Sstevel@tonic-gate 
41470Sstevel@tonic-gate 	mac_copy(mac_pvt->curr_macaddr, (uchar_t *)&dlp[1],
41480Sstevel@tonic-gate 	    macinfo->gldm_addrlen);
41490Sstevel@tonic-gate 	sapp = mp->b_rptr + dlp->dl_addr_offset + macinfo->gldm_addrlen;
41500Sstevel@tonic-gate 	*(ushort_t *)sapp = gld->gld_sap;
41510Sstevel@tonic-gate 
41520Sstevel@tonic-gate 	return (mp);
41530Sstevel@tonic-gate }
41540Sstevel@tonic-gate 
41550Sstevel@tonic-gate /*
41560Sstevel@tonic-gate  * gld_bind - determine if a SAP is already allocated and whether it is legal
41570Sstevel@tonic-gate  * to do the bind at this time
41580Sstevel@tonic-gate  */
41590Sstevel@tonic-gate static int
41600Sstevel@tonic-gate gld_bind(queue_t *q, mblk_t *mp)
41610Sstevel@tonic-gate {
41620Sstevel@tonic-gate 	ulong_t	sap;
41630Sstevel@tonic-gate 	dl_bind_req_t *dlp;
41640Sstevel@tonic-gate 	gld_t *gld = (gld_t *)q->q_ptr;
41650Sstevel@tonic-gate 	gld_mac_info_t *macinfo = gld->gld_mac_info;
41660Sstevel@tonic-gate 
41670Sstevel@tonic-gate 	ASSERT(gld);
41680Sstevel@tonic-gate 	ASSERT(gld->gld_qptr == RD(q));
41690Sstevel@tonic-gate 
41700Sstevel@tonic-gate #ifdef GLD_DEBUG
41710Sstevel@tonic-gate 	if (gld_debug & GLDTRACE)
41720Sstevel@tonic-gate 		cmn_err(CE_NOTE, "gld_bind(%p %p)", (void *)q, (void *)mp);
41730Sstevel@tonic-gate #endif
41740Sstevel@tonic-gate 
41750Sstevel@tonic-gate 	dlp = (dl_bind_req_t *)mp->b_rptr;
41760Sstevel@tonic-gate 	sap = dlp->dl_sap;
41770Sstevel@tonic-gate 
41780Sstevel@tonic-gate #ifdef GLD_DEBUG
41790Sstevel@tonic-gate 	if (gld_debug & GLDPROT)
41800Sstevel@tonic-gate 		cmn_err(CE_NOTE, "gld_bind: lsap=%lx", sap);
41810Sstevel@tonic-gate #endif
41820Sstevel@tonic-gate 
41830Sstevel@tonic-gate 	if (gld->gld_state != DL_UNBOUND) {
41840Sstevel@tonic-gate #ifdef GLD_DEBUG
41850Sstevel@tonic-gate 		if (gld_debug & GLDERRS)
41860Sstevel@tonic-gate 			cmn_err(CE_NOTE, "gld_bind: bound or not attached (%d)",
41870Sstevel@tonic-gate 				gld->gld_state);
41880Sstevel@tonic-gate #endif
41890Sstevel@tonic-gate 		return (DL_OUTSTATE);
41900Sstevel@tonic-gate 	}
41910Sstevel@tonic-gate 	ASSERT(macinfo);
41920Sstevel@tonic-gate 
41930Sstevel@tonic-gate 	if (dlp->dl_service_mode != DL_CLDLS) {
41940Sstevel@tonic-gate 		return (DL_UNSUPPORTED);
41950Sstevel@tonic-gate 	}
41960Sstevel@tonic-gate 	if (dlp->dl_xidtest_flg & (DL_AUTO_XID | DL_AUTO_TEST)) {
41970Sstevel@tonic-gate 		return (DL_NOAUTO);
41980Sstevel@tonic-gate 	}
41990Sstevel@tonic-gate 
42000Sstevel@tonic-gate 	/*
42010Sstevel@tonic-gate 	 * Check sap validity and decide whether this stream accepts
42020Sstevel@tonic-gate 	 * IEEE 802.2 (LLC) packets.
42030Sstevel@tonic-gate 	 */
42040Sstevel@tonic-gate 	if (sap > ETHERTYPE_MAX)
42050Sstevel@tonic-gate 		return (DL_BADSAP);
42060Sstevel@tonic-gate 
42070Sstevel@tonic-gate 	/*
42080Sstevel@tonic-gate 	 * Decide whether the SAP value selects EtherType encoding/decoding.
42090Sstevel@tonic-gate 	 * For compatibility with monolithic ethernet drivers, the range of
42100Sstevel@tonic-gate 	 * SAP values is different for DL_ETHER media.
42110Sstevel@tonic-gate 	 */
42120Sstevel@tonic-gate 	switch (macinfo->gldm_type) {
42130Sstevel@tonic-gate 	case DL_ETHER:
42140Sstevel@tonic-gate 		gld->gld_ethertype = (sap > ETHERMTU);
42150Sstevel@tonic-gate 		break;
42160Sstevel@tonic-gate 	default:
42170Sstevel@tonic-gate 		gld->gld_ethertype = (sap > GLD_MAX_802_SAP);
42180Sstevel@tonic-gate 		break;
42190Sstevel@tonic-gate 	}
42200Sstevel@tonic-gate 
42210Sstevel@tonic-gate 	/* if we get to here, then the SAP is legal enough */
42220Sstevel@tonic-gate 	GLDM_LOCK(macinfo, RW_WRITER);
42230Sstevel@tonic-gate 	gld->gld_state = DL_IDLE;	/* bound and ready */
42240Sstevel@tonic-gate 	gld->gld_sap = sap;
4225*2760Sdg199075 	if ((macinfo->gldm_type == DL_ETHER) && (sap == ETHERTYPE_VLAN))
4226*2760Sdg199075 		((gld_vlan_t *)gld->gld_vlan)->gldv_nvlan_sap++;
42270Sstevel@tonic-gate 	gld_set_ipq(gld);
42280Sstevel@tonic-gate 
42290Sstevel@tonic-gate #ifdef GLD_DEBUG
42300Sstevel@tonic-gate 	if (gld_debug & GLDPROT)
42310Sstevel@tonic-gate 		cmn_err(CE_NOTE, "gld_bind: ok - sap = %d", gld->gld_sap);
42320Sstevel@tonic-gate #endif
42330Sstevel@tonic-gate 
42340Sstevel@tonic-gate 	/* ACK the BIND */
42350Sstevel@tonic-gate 	mp = gld_bindack(q, mp);
42360Sstevel@tonic-gate 	GLDM_UNLOCK(macinfo);
42370Sstevel@tonic-gate 
42380Sstevel@tonic-gate 	if (mp != NULL) {
42390Sstevel@tonic-gate 		qreply(q, mp);
42400Sstevel@tonic-gate 		return (GLDE_OK);
42410Sstevel@tonic-gate 	}
42420Sstevel@tonic-gate 
42430Sstevel@tonic-gate 	return (DL_SYSERR);
42440Sstevel@tonic-gate }
42450Sstevel@tonic-gate 
42460Sstevel@tonic-gate /*
42470Sstevel@tonic-gate  * gld_unbind - perform an unbind of an LSAP or ether type on the stream.
42480Sstevel@tonic-gate  * The stream is still open and can be re-bound.
42490Sstevel@tonic-gate  */
42500Sstevel@tonic-gate static int
42510Sstevel@tonic-gate gld_unbind(queue_t *q, mblk_t *mp)
42520Sstevel@tonic-gate {
42530Sstevel@tonic-gate 	gld_t *gld = (gld_t *)q->q_ptr;
42540Sstevel@tonic-gate 	gld_mac_info_t *macinfo = gld->gld_mac_info;
42550Sstevel@tonic-gate 
42560Sstevel@tonic-gate 	ASSERT(gld);
42570Sstevel@tonic-gate 
42580Sstevel@tonic-gate #ifdef GLD_DEBUG
42590Sstevel@tonic-gate 	if (gld_debug & GLDTRACE)
42600Sstevel@tonic-gate 		cmn_err(CE_NOTE, "gld_unbind(%p %p)", (void *)q, (void *)mp);
42610Sstevel@tonic-gate #endif
42620Sstevel@tonic-gate 
42630Sstevel@tonic-gate 	if (gld->gld_state != DL_IDLE) {
42640Sstevel@tonic-gate #ifdef GLD_DEBUG
42650Sstevel@tonic-gate 		if (gld_debug & GLDERRS)
42660Sstevel@tonic-gate 			cmn_err(CE_NOTE, "gld_unbind: wrong state (%d)",
42670Sstevel@tonic-gate 				gld->gld_state);
42680Sstevel@tonic-gate #endif
42690Sstevel@tonic-gate 		return (DL_OUTSTATE);
42700Sstevel@tonic-gate 	}
42710Sstevel@tonic-gate 	ASSERT(macinfo);
42720Sstevel@tonic-gate 
42730Sstevel@tonic-gate 	/*
42740Sstevel@tonic-gate 	 * Avoid unbinding (DL_UNBIND_REQ) while FAST/RAW is inside wput.
42750Sstevel@tonic-gate 	 * See comments above gld_start().
42760Sstevel@tonic-gate 	 */
42770Sstevel@tonic-gate 	gld->gld_in_unbind = B_TRUE;	/* disallow wput=>start */
42780Sstevel@tonic-gate 	membar_enter();
42790Sstevel@tonic-gate 	if (gld->gld_wput_count != 0) {
42800Sstevel@tonic-gate 		gld->gld_in_unbind = B_FALSE;
42810Sstevel@tonic-gate 		ASSERT(mp);		/* we didn't come from close */
42820Sstevel@tonic-gate #ifdef GLD_DEBUG
42830Sstevel@tonic-gate 		if (gld_debug & GLDETRACE)
42840Sstevel@tonic-gate 			cmn_err(CE_NOTE, "gld_unbind: defer for wput");
42850Sstevel@tonic-gate #endif
42860Sstevel@tonic-gate 		(void) putbq(q, mp);
42870Sstevel@tonic-gate 		qenable(q);		/* try again soon */
42880Sstevel@tonic-gate 		return (GLDE_RETRY);
42890Sstevel@tonic-gate 	}
42900Sstevel@tonic-gate 
42910Sstevel@tonic-gate 	GLDM_LOCK(macinfo, RW_WRITER);
4292*2760Sdg199075 	if ((macinfo->gldm_type == DL_ETHER) &&
4293*2760Sdg199075 	    (gld->gld_sap == ETHERTYPE_VLAN)) {
4294*2760Sdg199075 		((gld_vlan_t *)gld->gld_vlan)->gldv_nvlan_sap--;
4295*2760Sdg199075 	}
42960Sstevel@tonic-gate 	gld->gld_state = DL_UNBOUND;
42970Sstevel@tonic-gate 	gld->gld_sap = 0;
42980Sstevel@tonic-gate 	gld_set_ipq(gld);
42990Sstevel@tonic-gate 	GLDM_UNLOCK(macinfo);
43000Sstevel@tonic-gate 
43010Sstevel@tonic-gate 	membar_exit();
43020Sstevel@tonic-gate 	gld->gld_in_unbind = B_FALSE;
43030Sstevel@tonic-gate 
43040Sstevel@tonic-gate 	/* mp is NULL if we came from close */
43050Sstevel@tonic-gate 	if (mp) {
43060Sstevel@tonic-gate 		gld_flushqueue(q);	/* flush the queues */
43070Sstevel@tonic-gate 		dlokack(q, mp, DL_UNBIND_REQ);
43080Sstevel@tonic-gate 	}
43090Sstevel@tonic-gate 	return (GLDE_OK);
43100Sstevel@tonic-gate }
43110Sstevel@tonic-gate 
43120Sstevel@tonic-gate /*
43130Sstevel@tonic-gate  * gld_inforeq - generate the response to an info request
43140Sstevel@tonic-gate  */
43150Sstevel@tonic-gate static int
43160Sstevel@tonic-gate gld_inforeq(queue_t *q, mblk_t *mp)
43170Sstevel@tonic-gate {
43180Sstevel@tonic-gate 	gld_t		*gld;
43190Sstevel@tonic-gate 	dl_info_ack_t	*dlp;
43200Sstevel@tonic-gate 	int		bufsize;
43210Sstevel@tonic-gate 	glddev_t	*glddev;
43220Sstevel@tonic-gate 	gld_mac_info_t	*macinfo;
43230Sstevel@tonic-gate 	gld_mac_pvt_t	*mac_pvt;
43240Sstevel@tonic-gate 	int		sel_offset = 0;
43250Sstevel@tonic-gate 	int		range_offset = 0;
43260Sstevel@tonic-gate 	int		addr_offset;
43270Sstevel@tonic-gate 	int		addr_length;
43280Sstevel@tonic-gate 	int		sap_length;
43290Sstevel@tonic-gate 	int		brdcst_offset;
43300Sstevel@tonic-gate 	int		brdcst_length;
43310Sstevel@tonic-gate 	uchar_t		*sapp;
43320Sstevel@tonic-gate 
43330Sstevel@tonic-gate #ifdef GLD_DEBUG
43340Sstevel@tonic-gate 	if (gld_debug & GLDTRACE)
43350Sstevel@tonic-gate 		cmn_err(CE_NOTE, "gld_inforeq(%p %p)", (void *)q, (void *)mp);
43360Sstevel@tonic-gate #endif
43370Sstevel@tonic-gate 	gld = (gld_t *)q->q_ptr;
43380Sstevel@tonic-gate 	ASSERT(gld);
43390Sstevel@tonic-gate 	glddev = gld->gld_device;
43400Sstevel@tonic-gate 	ASSERT(glddev);
43410Sstevel@tonic-gate 
43420Sstevel@tonic-gate 	if (gld->gld_state == DL_IDLE || gld->gld_state == DL_UNBOUND) {
43430Sstevel@tonic-gate 		macinfo = gld->gld_mac_info;
43440Sstevel@tonic-gate 		ASSERT(macinfo != NULL);
43450Sstevel@tonic-gate 
43460Sstevel@tonic-gate 		mac_pvt = (gld_mac_pvt_t *)macinfo->gldm_mac_pvt;
43470Sstevel@tonic-gate 
43480Sstevel@tonic-gate 		addr_length = macinfo->gldm_addrlen;
43490Sstevel@tonic-gate 		sap_length = macinfo->gldm_saplen;
43500Sstevel@tonic-gate 		brdcst_length = macinfo->gldm_addrlen;
43510Sstevel@tonic-gate 	} else {
43520Sstevel@tonic-gate 		addr_length = glddev->gld_addrlen;
43530Sstevel@tonic-gate 		sap_length = glddev->gld_saplen;
43540Sstevel@tonic-gate 		brdcst_length = glddev->gld_addrlen;
43550Sstevel@tonic-gate 	}
43560Sstevel@tonic-gate 
43570Sstevel@tonic-gate 	bufsize = sizeof (dl_info_ack_t);
43580Sstevel@tonic-gate 
43590Sstevel@tonic-gate 	addr_offset = bufsize;
43600Sstevel@tonic-gate 	bufsize += addr_length;
43610Sstevel@tonic-gate 	bufsize += abs(sap_length);
43620Sstevel@tonic-gate 
43630Sstevel@tonic-gate 	brdcst_offset = bufsize;
43640Sstevel@tonic-gate 	bufsize += brdcst_length;
43650Sstevel@tonic-gate 
4366*2760Sdg199075 	if (((gld_vlan_t *)gld->gld_vlan) != NULL) {
43670Sstevel@tonic-gate 		sel_offset = P2ROUNDUP(bufsize, sizeof (int64_t));
43680Sstevel@tonic-gate 		bufsize = sel_offset + sizeof (dl_qos_cl_sel1_t);
43690Sstevel@tonic-gate 
43700Sstevel@tonic-gate 		range_offset = P2ROUNDUP(bufsize, sizeof (int64_t));
43710Sstevel@tonic-gate 		bufsize = range_offset + sizeof (dl_qos_cl_range1_t);
43720Sstevel@tonic-gate 	}
43730Sstevel@tonic-gate 
43740Sstevel@tonic-gate 	if ((mp = mexchange(q, mp, bufsize, M_PCPROTO, DL_INFO_ACK)) == NULL)
43750Sstevel@tonic-gate 		return (GLDE_OK);	/* nothing more to be done */
43760Sstevel@tonic-gate 
43770Sstevel@tonic-gate 	bzero(mp->b_rptr, bufsize);
43780Sstevel@tonic-gate 
43790Sstevel@tonic-gate 	dlp = (dl_info_ack_t *)mp->b_rptr;
43800Sstevel@tonic-gate 	dlp->dl_primitive = DL_INFO_ACK;
43810Sstevel@tonic-gate 	dlp->dl_version = DL_VERSION_2;
43820Sstevel@tonic-gate 	dlp->dl_service_mode = DL_CLDLS;
43830Sstevel@tonic-gate 	dlp->dl_current_state = gld->gld_state;
43840Sstevel@tonic-gate 	dlp->dl_provider_style = gld->gld_style;
43850Sstevel@tonic-gate 
43860Sstevel@tonic-gate 	if (sel_offset != 0) {
43870Sstevel@tonic-gate 		dl_qos_cl_sel1_t	*selp;
43880Sstevel@tonic-gate 		dl_qos_cl_range1_t	*rangep;
43890Sstevel@tonic-gate 
43900Sstevel@tonic-gate 		ASSERT(range_offset != 0);
43910Sstevel@tonic-gate 
43920Sstevel@tonic-gate 		dlp->dl_qos_offset = sel_offset;
43930Sstevel@tonic-gate 		dlp->dl_qos_length = sizeof (dl_qos_cl_sel1_t);
43940Sstevel@tonic-gate 		dlp->dl_qos_range_offset = range_offset;
43950Sstevel@tonic-gate 		dlp->dl_qos_range_length = sizeof (dl_qos_cl_range1_t);
43960Sstevel@tonic-gate 
43970Sstevel@tonic-gate 		selp = (dl_qos_cl_sel1_t *)(mp->b_rptr + sel_offset);
43980Sstevel@tonic-gate 		selp->dl_qos_type = DL_QOS_CL_SEL1;
43990Sstevel@tonic-gate 		selp->dl_priority = gld->gld_upri;
44000Sstevel@tonic-gate 
44010Sstevel@tonic-gate 		rangep = (dl_qos_cl_range1_t *)(mp->b_rptr + range_offset);
44020Sstevel@tonic-gate 		rangep->dl_qos_type = DL_QOS_CL_RANGE1;
44030Sstevel@tonic-gate 		rangep->dl_priority.dl_min = 0;
44040Sstevel@tonic-gate 		rangep->dl_priority.dl_max = 7;
44050Sstevel@tonic-gate 	}
44060Sstevel@tonic-gate 
44070Sstevel@tonic-gate 	if (gld->gld_state == DL_IDLE || gld->gld_state == DL_UNBOUND) {
44080Sstevel@tonic-gate 		dlp->dl_min_sdu = macinfo->gldm_minpkt;
44090Sstevel@tonic-gate 		dlp->dl_max_sdu = macinfo->gldm_maxpkt;
44100Sstevel@tonic-gate 		dlp->dl_mac_type = macinfo->gldm_type;
44110Sstevel@tonic-gate 		dlp->dl_addr_length = addr_length + abs(sap_length);
44120Sstevel@tonic-gate 		dlp->dl_sap_length = sap_length;
44130Sstevel@tonic-gate 
44140Sstevel@tonic-gate 		if (gld->gld_state == DL_IDLE) {
44150Sstevel@tonic-gate 			/*
44160Sstevel@tonic-gate 			 * If we are bound to a non-LLC SAP on any medium
44170Sstevel@tonic-gate 			 * other than Ethernet, then we need room for a
44180Sstevel@tonic-gate 			 * SNAP header.  So we have to adjust the MTU size
44190Sstevel@tonic-gate 			 * accordingly.  XXX I suppose this should be done
44200Sstevel@tonic-gate 			 * in gldutil.c, but it seems likely that this will
44210Sstevel@tonic-gate 			 * always be true for everything GLD supports but
44220Sstevel@tonic-gate 			 * Ethernet.  Check this if you add another medium.
44230Sstevel@tonic-gate 			 */
44240Sstevel@tonic-gate 			if ((macinfo->gldm_type == DL_TPR ||
44250Sstevel@tonic-gate 			    macinfo->gldm_type == DL_FDDI) &&
44260Sstevel@tonic-gate 			    gld->gld_ethertype)
44270Sstevel@tonic-gate 				dlp->dl_max_sdu -= LLC_SNAP_HDR_LEN;
44280Sstevel@tonic-gate 
44290Sstevel@tonic-gate 			/* copy macaddr and sap */
44300Sstevel@tonic-gate 			dlp->dl_addr_offset = addr_offset;
44310Sstevel@tonic-gate 
44320Sstevel@tonic-gate 			mac_copy(mac_pvt->curr_macaddr, mp->b_rptr +
44330Sstevel@tonic-gate 			    addr_offset, macinfo->gldm_addrlen);
44340Sstevel@tonic-gate 			sapp = mp->b_rptr + addr_offset +
44350Sstevel@tonic-gate 			    macinfo->gldm_addrlen;
44360Sstevel@tonic-gate 			*(ushort_t *)sapp = gld->gld_sap;
44370Sstevel@tonic-gate 		} else {
44380Sstevel@tonic-gate 			dlp->dl_addr_offset = 0;
44390Sstevel@tonic-gate 		}
44400Sstevel@tonic-gate 
44410Sstevel@tonic-gate 		/* copy broadcast addr */
44420Sstevel@tonic-gate 		dlp->dl_brdcst_addr_length = macinfo->gldm_addrlen;
44430Sstevel@tonic-gate 		dlp->dl_brdcst_addr_offset = brdcst_offset;
44440Sstevel@tonic-gate 		mac_copy((caddr_t)macinfo->gldm_broadcast_addr,
44450Sstevel@tonic-gate 		    mp->b_rptr + brdcst_offset, brdcst_length);
44460Sstevel@tonic-gate 	} else {
44470Sstevel@tonic-gate 		/*
44480Sstevel@tonic-gate 		 * No PPA is attached.
44490Sstevel@tonic-gate 		 * The best we can do is use the values provided
44500Sstevel@tonic-gate 		 * by the first mac that called gld_register.
44510Sstevel@tonic-gate 		 */
44520Sstevel@tonic-gate 		dlp->dl_min_sdu = glddev->gld_minsdu;
44530Sstevel@tonic-gate 		dlp->dl_max_sdu = glddev->gld_maxsdu;
44540Sstevel@tonic-gate 		dlp->dl_mac_type = glddev->gld_type;
44550Sstevel@tonic-gate 		dlp->dl_addr_length = addr_length + abs(sap_length);
44560Sstevel@tonic-gate 		dlp->dl_sap_length = sap_length;
44570Sstevel@tonic-gate 		dlp->dl_addr_offset = 0;
44580Sstevel@tonic-gate 		dlp->dl_brdcst_addr_offset = brdcst_offset;
44590Sstevel@tonic-gate 		dlp->dl_brdcst_addr_length = brdcst_length;
44600Sstevel@tonic-gate 		mac_copy((caddr_t)glddev->gld_broadcast,
44610Sstevel@tonic-gate 		    mp->b_rptr + brdcst_offset, brdcst_length);
44620Sstevel@tonic-gate 	}
44630Sstevel@tonic-gate 	qreply(q, mp);
44640Sstevel@tonic-gate 	return (GLDE_OK);
44650Sstevel@tonic-gate }
44660Sstevel@tonic-gate 
44670Sstevel@tonic-gate /*
44680Sstevel@tonic-gate  * gld_unitdata (q, mp)
44690Sstevel@tonic-gate  * send a datagram.  Destination address/lsap is in M_PROTO
44700Sstevel@tonic-gate  * message (first mblock), data is in remainder of message.
44710Sstevel@tonic-gate  *
44720Sstevel@tonic-gate  */
44730Sstevel@tonic-gate static int
44740Sstevel@tonic-gate gld_unitdata(queue_t *q, mblk_t *mp)
44750Sstevel@tonic-gate {
44760Sstevel@tonic-gate 	gld_t *gld = (gld_t *)q->q_ptr;
44770Sstevel@tonic-gate 	dl_unitdata_req_t *dlp = (dl_unitdata_req_t *)mp->b_rptr;
44780Sstevel@tonic-gate 	gld_mac_info_t *macinfo = gld->gld_mac_info;
44790Sstevel@tonic-gate 	size_t	msglen;
44800Sstevel@tonic-gate 	mblk_t	*nmp;
44810Sstevel@tonic-gate 	gld_interface_t *ifp;
44820Sstevel@tonic-gate 	uint32_t start;
44830Sstevel@tonic-gate 	uint32_t stuff;
44840Sstevel@tonic-gate 	uint32_t end;
44850Sstevel@tonic-gate 	uint32_t value;
44860Sstevel@tonic-gate 	uint32_t flags;
44870Sstevel@tonic-gate 	uint32_t upri;
44880Sstevel@tonic-gate 
44890Sstevel@tonic-gate #ifdef GLD_DEBUG
44900Sstevel@tonic-gate 	if (gld_debug & GLDTRACE)
44910Sstevel@tonic-gate 		cmn_err(CE_NOTE, "gld_unitdata(%p %p)", (void *)q, (void *)mp);
44920Sstevel@tonic-gate #endif
44930Sstevel@tonic-gate 
44940Sstevel@tonic-gate 	if (gld->gld_state != DL_IDLE) {
44950Sstevel@tonic-gate #ifdef GLD_DEBUG
44960Sstevel@tonic-gate 		if (gld_debug & GLDERRS)
44970Sstevel@tonic-gate 			cmn_err(CE_NOTE, "gld_unitdata: wrong state (%d)",
44980Sstevel@tonic-gate 				gld->gld_state);
44990Sstevel@tonic-gate #endif
45000Sstevel@tonic-gate 		dluderrorind(q, mp, mp->b_rptr + dlp->dl_dest_addr_offset,
45010Sstevel@tonic-gate 		    dlp->dl_dest_addr_length, DL_OUTSTATE, 0);
45020Sstevel@tonic-gate 		return (GLDE_OK);
45030Sstevel@tonic-gate 	}
45040Sstevel@tonic-gate 	ASSERT(macinfo != NULL);
45050Sstevel@tonic-gate 
45060Sstevel@tonic-gate 	if (!MBLKIN(mp, dlp->dl_dest_addr_offset, dlp->dl_dest_addr_length) ||
45070Sstevel@tonic-gate 	    dlp->dl_dest_addr_length !=
45080Sstevel@tonic-gate 	    macinfo->gldm_addrlen + abs(macinfo->gldm_saplen)) {
45090Sstevel@tonic-gate 		dluderrorind(q, mp, mp->b_rptr + dlp->dl_dest_addr_offset,
45100Sstevel@tonic-gate 		    dlp->dl_dest_addr_length, DL_BADADDR, 0);
45110Sstevel@tonic-gate 		return (GLDE_OK);
45120Sstevel@tonic-gate 	}
45130Sstevel@tonic-gate 
45140Sstevel@tonic-gate 	upri = dlp->dl_priority.dl_max;
45150Sstevel@tonic-gate 
45160Sstevel@tonic-gate 	msglen = msgdsize(mp);
45170Sstevel@tonic-gate 	if (msglen == 0 || msglen > macinfo->gldm_maxpkt) {
45180Sstevel@tonic-gate #ifdef GLD_DEBUG
45190Sstevel@tonic-gate 		if (gld_debug & GLDERRS)
45200Sstevel@tonic-gate 			cmn_err(CE_NOTE, "gld_unitdata: bad msglen (%d)",
45210Sstevel@tonic-gate 				(int)msglen);
45220Sstevel@tonic-gate #endif
45230Sstevel@tonic-gate 		dluderrorind(q, mp, mp->b_rptr + dlp->dl_dest_addr_offset,
45240Sstevel@tonic-gate 		    dlp->dl_dest_addr_length, DL_BADDATA, 0);
45250Sstevel@tonic-gate 		return (GLDE_OK);
45260Sstevel@tonic-gate 	}
45270Sstevel@tonic-gate 
45280Sstevel@tonic-gate 	ASSERT(mp->b_cont != NULL);	/* because msgdsize(mp) is nonzero */
45290Sstevel@tonic-gate 
45300Sstevel@tonic-gate 	ifp = ((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->interfacep;
45310Sstevel@tonic-gate 
45320Sstevel@tonic-gate 	/* grab any checksum information that may be present */
45330Sstevel@tonic-gate 	hcksum_retrieve(mp->b_cont, NULL, NULL, &start, &stuff, &end,
45340Sstevel@tonic-gate 	    &value, &flags);
45350Sstevel@tonic-gate 
45360Sstevel@tonic-gate 	/*
45370Sstevel@tonic-gate 	 * Prepend a valid header for transmission
45380Sstevel@tonic-gate 	 */
45390Sstevel@tonic-gate 	if ((nmp = (*ifp->mkunitdata)(gld, mp)) == NULL) {
45400Sstevel@tonic-gate #ifdef GLD_DEBUG
45410Sstevel@tonic-gate 		if (gld_debug & GLDERRS)
45420Sstevel@tonic-gate 			cmn_err(CE_NOTE, "gld_unitdata: mkunitdata failed.");
45430Sstevel@tonic-gate #endif
45440Sstevel@tonic-gate 		dluderrorind(q, mp, mp->b_rptr + dlp->dl_dest_addr_offset,
45450Sstevel@tonic-gate 		    dlp->dl_dest_addr_length, DL_SYSERR, ENOSR);
45460Sstevel@tonic-gate 		return (GLDE_OK);
45470Sstevel@tonic-gate 	}
45480Sstevel@tonic-gate 
45490Sstevel@tonic-gate 	/* apply any checksum information to the first block in the chain */
45500Sstevel@tonic-gate 	(void) hcksum_assoc(nmp, NULL, NULL, start, stuff, end, value,
45510Sstevel@tonic-gate 	    flags, 0);
45520Sstevel@tonic-gate 
4553*2760Sdg199075 	GLD_CLEAR_MBLK_VTAG(nmp);
45540Sstevel@tonic-gate 	if (gld_start(q, nmp, GLD_WSRV, upri) == GLD_NORESOURCES) {
45550Sstevel@tonic-gate 		qenable(q);
45560Sstevel@tonic-gate 		return (GLDE_RETRY);
45570Sstevel@tonic-gate 	}
45580Sstevel@tonic-gate 
45590Sstevel@tonic-gate 	return (GLDE_OK);
45600Sstevel@tonic-gate }
45610Sstevel@tonic-gate 
45620Sstevel@tonic-gate /*
45630Sstevel@tonic-gate  * gldattach(q, mp)
45640Sstevel@tonic-gate  * DLPI DL_ATTACH_REQ
45650Sstevel@tonic-gate  * this attaches the stream to a PPA
45660Sstevel@tonic-gate  */
45670Sstevel@tonic-gate static int
45680Sstevel@tonic-gate gldattach(queue_t *q, mblk_t *mp)
45690Sstevel@tonic-gate {
45700Sstevel@tonic-gate 	dl_attach_req_t *at;
45710Sstevel@tonic-gate 	gld_mac_info_t *macinfo;
45720Sstevel@tonic-gate 	gld_t  *gld = (gld_t *)q->q_ptr;
45730Sstevel@tonic-gate 	glddev_t *glddev;
45740Sstevel@tonic-gate 	gld_mac_pvt_t *mac_pvt;
45750Sstevel@tonic-gate 	uint32_t ppa;
45760Sstevel@tonic-gate 	uint32_t vid;
45770Sstevel@tonic-gate 	gld_vlan_t *vlan;
45780Sstevel@tonic-gate 
45790Sstevel@tonic-gate 	at = (dl_attach_req_t *)mp->b_rptr;
45800Sstevel@tonic-gate 
45810Sstevel@tonic-gate 	if (gld->gld_state != DL_UNATTACHED)
45820Sstevel@tonic-gate 		return (DL_OUTSTATE);
45830Sstevel@tonic-gate 
45840Sstevel@tonic-gate 	ASSERT(!gld->gld_mac_info);
45850Sstevel@tonic-gate 
45860Sstevel@tonic-gate 	ppa = at->dl_ppa % GLD_VLAN_SCALE;	/* 0 .. 999	*/
45870Sstevel@tonic-gate 	vid = at->dl_ppa / GLD_VLAN_SCALE;	/* 0 .. 4094	*/
45880Sstevel@tonic-gate 	if (vid > VLAN_VID_MAX)
45890Sstevel@tonic-gate 		return (DL_BADPPA);
45900Sstevel@tonic-gate 
45910Sstevel@tonic-gate 	glddev = gld->gld_device;
45920Sstevel@tonic-gate 	mutex_enter(&glddev->gld_devlock);
45930Sstevel@tonic-gate 	for (macinfo = glddev->gld_mac_next;
45940Sstevel@tonic-gate 	    macinfo != (gld_mac_info_t *)&glddev->gld_mac_next;
45950Sstevel@tonic-gate 	    macinfo = macinfo->gldm_next) {
45960Sstevel@tonic-gate 		int inst;
45970Sstevel@tonic-gate 
45980Sstevel@tonic-gate 		ASSERT(macinfo != NULL);
45990Sstevel@tonic-gate 		if (macinfo->gldm_ppa != ppa)
46000Sstevel@tonic-gate 			continue;
46010Sstevel@tonic-gate 
46020Sstevel@tonic-gate 		if (!(macinfo->gldm_GLD_flags & GLD_MAC_READY))
46030Sstevel@tonic-gate 			continue;	/* this one's not ready yet */
46040Sstevel@tonic-gate 
46050Sstevel@tonic-gate 		/*
46060Sstevel@tonic-gate 		 * VLAN sanity check
46070Sstevel@tonic-gate 		 */
46080Sstevel@tonic-gate 		if (vid != VLAN_VID_NONE && !VLAN_CAPABLE(macinfo)) {
46090Sstevel@tonic-gate 			mutex_exit(&glddev->gld_devlock);
46100Sstevel@tonic-gate 			return (DL_BADPPA);
46110Sstevel@tonic-gate 		}
46120Sstevel@tonic-gate 
46130Sstevel@tonic-gate 		/*
46140Sstevel@tonic-gate 		 * We found the correct PPA, hold the instance
46150Sstevel@tonic-gate 		 */
46160Sstevel@tonic-gate 		inst = ddi_get_instance(macinfo->gldm_devinfo);
46170Sstevel@tonic-gate 		if (inst == -1 || qassociate(q, inst) != 0) {
46180Sstevel@tonic-gate 			mutex_exit(&glddev->gld_devlock);
46190Sstevel@tonic-gate 			return (DL_BADPPA);
46200Sstevel@tonic-gate 		}
46210Sstevel@tonic-gate 
46220Sstevel@tonic-gate 		/* Take the stream off the per-driver-class list */
46230Sstevel@tonic-gate 		gldremque(gld);
46240Sstevel@tonic-gate 
46250Sstevel@tonic-gate 		/*
46260Sstevel@tonic-gate 		 * We must hold the lock to prevent multiple calls
46270Sstevel@tonic-gate 		 * to the reset and start routines.
46280Sstevel@tonic-gate 		 */
46290Sstevel@tonic-gate 		GLDM_LOCK(macinfo, RW_WRITER);
46300Sstevel@tonic-gate 
46310Sstevel@tonic-gate 		gld->gld_mac_info = macinfo;
46320Sstevel@tonic-gate 
46330Sstevel@tonic-gate 		if (macinfo->gldm_send_tagged != NULL)
46340Sstevel@tonic-gate 			gld->gld_send = macinfo->gldm_send_tagged;
46350Sstevel@tonic-gate 		else
46360Sstevel@tonic-gate 			gld->gld_send = macinfo->gldm_send;
46370Sstevel@tonic-gate 
46380Sstevel@tonic-gate 		if ((vlan = gld_get_vlan(macinfo, vid)) == NULL) {
46390Sstevel@tonic-gate 			GLDM_UNLOCK(macinfo);
46400Sstevel@tonic-gate 			gldinsque(gld, glddev->gld_str_prev);
46410Sstevel@tonic-gate 			mutex_exit(&glddev->gld_devlock);
46420Sstevel@tonic-gate 			(void) qassociate(q, -1);
46430Sstevel@tonic-gate 			return (DL_BADPPA);
46440Sstevel@tonic-gate 		}
46450Sstevel@tonic-gate 
46460Sstevel@tonic-gate 		mac_pvt = (gld_mac_pvt_t *)macinfo->gldm_mac_pvt;
46470Sstevel@tonic-gate 		if (!mac_pvt->started) {
46480Sstevel@tonic-gate 			if (gld_start_mac(macinfo) != GLD_SUCCESS) {
46490Sstevel@tonic-gate 				gld_rem_vlan(vlan);
46500Sstevel@tonic-gate 				GLDM_UNLOCK(macinfo);
46510Sstevel@tonic-gate 				gldinsque(gld, glddev->gld_str_prev);
46520Sstevel@tonic-gate 				mutex_exit(&glddev->gld_devlock);
46530Sstevel@tonic-gate 				dlerrorack(q, mp, DL_ATTACH_REQ, DL_SYSERR,
46540Sstevel@tonic-gate 				    EIO);
46550Sstevel@tonic-gate 				(void) qassociate(q, -1);
46560Sstevel@tonic-gate 				return (GLDE_OK);
46570Sstevel@tonic-gate 			}
46580Sstevel@tonic-gate 		}
46590Sstevel@tonic-gate 
46600Sstevel@tonic-gate 		gld->gld_vlan = vlan;
46610Sstevel@tonic-gate 		vlan->gldv_nstreams++;
46620Sstevel@tonic-gate 		gldinsque(gld, vlan->gldv_str_prev);
46630Sstevel@tonic-gate 		gld->gld_state = DL_UNBOUND;
46640Sstevel@tonic-gate 		GLDM_UNLOCK(macinfo);
46650Sstevel@tonic-gate 
46660Sstevel@tonic-gate #ifdef GLD_DEBUG
46670Sstevel@tonic-gate 		if (gld_debug & GLDPROT) {
46680Sstevel@tonic-gate 			cmn_err(CE_NOTE, "gldattach(%p, %p, PPA = %d)",
46690Sstevel@tonic-gate 			    (void *)q, (void *)mp, macinfo->gldm_ppa);
46700Sstevel@tonic-gate 		}
46710Sstevel@tonic-gate #endif
46720Sstevel@tonic-gate 		mutex_exit(&glddev->gld_devlock);
46730Sstevel@tonic-gate 		dlokack(q, mp, DL_ATTACH_REQ);
46740Sstevel@tonic-gate 		return (GLDE_OK);
46750Sstevel@tonic-gate 	}
46760Sstevel@tonic-gate 	mutex_exit(&glddev->gld_devlock);
46770Sstevel@tonic-gate 	return (DL_BADPPA);
46780Sstevel@tonic-gate }
46790Sstevel@tonic-gate 
46800Sstevel@tonic-gate /*
46810Sstevel@tonic-gate  * gldunattach(q, mp)
46820Sstevel@tonic-gate  * DLPI DL_DETACH_REQ
46830Sstevel@tonic-gate  * detaches the mac layer from the stream
46840Sstevel@tonic-gate  */
46850Sstevel@tonic-gate int
46860Sstevel@tonic-gate gldunattach(queue_t *q, mblk_t *mp)
46870Sstevel@tonic-gate {
46880Sstevel@tonic-gate 	gld_t  *gld = (gld_t *)q->q_ptr;
46890Sstevel@tonic-gate 	glddev_t *glddev = gld->gld_device;
46900Sstevel@tonic-gate 	gld_mac_info_t *macinfo = gld->gld_mac_info;
46910Sstevel@tonic-gate 	int	state = gld->gld_state;
46920Sstevel@tonic-gate 	int	i;
46930Sstevel@tonic-gate 	gld_mac_pvt_t *mac_pvt;
46940Sstevel@tonic-gate 	gld_vlan_t *vlan;
46950Sstevel@tonic-gate 	boolean_t phys_off;
46960Sstevel@tonic-gate 	boolean_t mult_off;
46970Sstevel@tonic-gate 	int op = GLD_MAC_PROMISC_NOOP;
46980Sstevel@tonic-gate 
46990Sstevel@tonic-gate 	if (state != DL_UNBOUND)
47000Sstevel@tonic-gate 		return (DL_OUTSTATE);
47010Sstevel@tonic-gate 
47020Sstevel@tonic-gate 	ASSERT(macinfo != NULL);
47030Sstevel@tonic-gate 	ASSERT(gld->gld_sap == 0);
47040Sstevel@tonic-gate 	mac_pvt = (gld_mac_pvt_t *)macinfo->gldm_mac_pvt;
47050Sstevel@tonic-gate 
47060Sstevel@tonic-gate #ifdef GLD_DEBUG
47070Sstevel@tonic-gate 	if (gld_debug & GLDPROT) {
47080Sstevel@tonic-gate 		cmn_err(CE_NOTE, "gldunattach(%p, %p, PPA = %d)",
47090Sstevel@tonic-gate 		    (void *)q, (void *)mp, macinfo->gldm_ppa);
47100Sstevel@tonic-gate 	}
47110Sstevel@tonic-gate #endif
47120Sstevel@tonic-gate 
47130Sstevel@tonic-gate 	GLDM_LOCK(macinfo, RW_WRITER);
47140Sstevel@tonic-gate 
47150Sstevel@tonic-gate 	if (gld->gld_mcast) {
47160Sstevel@tonic-gate 		for (i = 0; i < gld->gld_multicnt; i++) {
47170Sstevel@tonic-gate 			gld_mcast_t *mcast;
47180Sstevel@tonic-gate 
47190Sstevel@tonic-gate 			if ((mcast = gld->gld_mcast[i]) != NULL) {
47200Sstevel@tonic-gate 				ASSERT(mcast->gldm_refcnt);
47210Sstevel@tonic-gate 				gld_send_disable_multi(macinfo, mcast);
47220Sstevel@tonic-gate 			}
47230Sstevel@tonic-gate 		}
47240Sstevel@tonic-gate 		kmem_free(gld->gld_mcast,
47250Sstevel@tonic-gate 		    sizeof (gld_mcast_t *) * gld->gld_multicnt);
47260Sstevel@tonic-gate 		gld->gld_mcast = NULL;
47270Sstevel@tonic-gate 		gld->gld_multicnt = 0;
47280Sstevel@tonic-gate 	}
47290Sstevel@tonic-gate 
47300Sstevel@tonic-gate 	/* decide if we need to turn off any promiscuity */
47310Sstevel@tonic-gate 	phys_off = (gld->gld_flags & GLD_PROM_PHYS &&
47320Sstevel@tonic-gate 	    --mac_pvt->nprom == 0);
47330Sstevel@tonic-gate 	mult_off = (gld->gld_flags & GLD_PROM_MULT &&
47340Sstevel@tonic-gate 	    --mac_pvt->nprom_multi == 0);
47350Sstevel@tonic-gate 
47360Sstevel@tonic-gate 	if (phys_off) {
47370Sstevel@tonic-gate 		op = (mac_pvt->nprom_multi == 0) ? GLD_MAC_PROMISC_NONE :
47380Sstevel@tonic-gate 		    GLD_MAC_PROMISC_MULTI;
47390Sstevel@tonic-gate 	} else if (mult_off) {
47400Sstevel@tonic-gate 		op = (mac_pvt->nprom == 0) ? GLD_MAC_PROMISC_NONE :
47410Sstevel@tonic-gate 		    GLD_MAC_PROMISC_NOOP;	/* phys overrides multi */
47420Sstevel@tonic-gate 	}
47430Sstevel@tonic-gate 
47440Sstevel@tonic-gate 	if (op != GLD_MAC_PROMISC_NOOP)
47450Sstevel@tonic-gate 		(void) (*macinfo->gldm_set_promiscuous)(macinfo, op);
47460Sstevel@tonic-gate 
4747*2760Sdg199075 	vlan = (gld_vlan_t *)gld->gld_vlan;
4748*2760Sdg199075 	if (gld->gld_flags & GLD_PROM_PHYS)
4749*2760Sdg199075 		vlan->gldv_nprom--;
4750*2760Sdg199075 	if (gld->gld_flags & GLD_PROM_MULT)
4751*2760Sdg199075 		vlan->gldv_nprom--;
4752*2760Sdg199075 	if (gld->gld_flags & GLD_PROM_SAP) {
4753*2760Sdg199075 		vlan->gldv_nprom--;
4754*2760Sdg199075 		vlan->gldv_nvlan_sap--;
4755*2760Sdg199075 	}
4756*2760Sdg199075 
4757*2760Sdg199075 	gld->gld_flags &= ~(GLD_PROM_PHYS | GLD_PROM_SAP | GLD_PROM_MULT);
4758*2760Sdg199075 
47590Sstevel@tonic-gate 	GLDM_UNLOCK(macinfo);
47600Sstevel@tonic-gate 
47610Sstevel@tonic-gate 	if (phys_off)
47620Sstevel@tonic-gate 		gld_notify_ind(macinfo, DL_NOTE_PROMISC_OFF_PHYS, NULL);
47630Sstevel@tonic-gate 
47640Sstevel@tonic-gate 	/*
47650Sstevel@tonic-gate 	 * We need to hold both locks when modifying the mac stream list
47660Sstevel@tonic-gate 	 * to protect findminor as well as everyone else.
47670Sstevel@tonic-gate 	 */
47680Sstevel@tonic-gate 	mutex_enter(&glddev->gld_devlock);
47690Sstevel@tonic-gate 	GLDM_LOCK(macinfo, RW_WRITER);
47700Sstevel@tonic-gate 
47710Sstevel@tonic-gate 	/* disassociate this stream with its vlan and underlying mac */
47720Sstevel@tonic-gate 	gldremque(gld);
47730Sstevel@tonic-gate 
47740Sstevel@tonic-gate 	if (--vlan->gldv_nstreams == 0) {
47750Sstevel@tonic-gate 		gld_rem_vlan(vlan);
47760Sstevel@tonic-gate 		gld->gld_vlan = NULL;
47770Sstevel@tonic-gate 	}
47780Sstevel@tonic-gate 
47790Sstevel@tonic-gate 	gld->gld_mac_info = NULL;
47800Sstevel@tonic-gate 	gld->gld_state = DL_UNATTACHED;
47810Sstevel@tonic-gate 
47820Sstevel@tonic-gate 	/* cleanup mac layer if last vlan */
47830Sstevel@tonic-gate 	if (mac_pvt->nvlan == 0) {
47840Sstevel@tonic-gate 		gld_stop_mac(macinfo);
47850Sstevel@tonic-gate 		macinfo->gldm_GLD_flags &= ~GLD_INTR_WAIT;
47860Sstevel@tonic-gate 	}
47870Sstevel@tonic-gate 
47880Sstevel@tonic-gate 	/* make sure no references to this gld for gld_v0_sched */
47890Sstevel@tonic-gate 	if (mac_pvt->last_sched == gld)
47900Sstevel@tonic-gate 		mac_pvt->last_sched = NULL;
47910Sstevel@tonic-gate 
47920Sstevel@tonic-gate 	GLDM_UNLOCK(macinfo);
47930Sstevel@tonic-gate 
47940Sstevel@tonic-gate 	/* put the stream on the unattached Style 2 list */
47950Sstevel@tonic-gate 	gldinsque(gld, glddev->gld_str_prev);
47960Sstevel@tonic-gate 
47970Sstevel@tonic-gate 	mutex_exit(&glddev->gld_devlock);
47980Sstevel@tonic-gate 
47990Sstevel@tonic-gate 	/* There will be no mp if we were called from close */
48000Sstevel@tonic-gate 	if (mp) {
48010Sstevel@tonic-gate 		dlokack(q, mp, DL_DETACH_REQ);
48020Sstevel@tonic-gate 	}
48030Sstevel@tonic-gate 	if (gld->gld_style == DL_STYLE2)
48040Sstevel@tonic-gate 		(void) qassociate(q, -1);
48050Sstevel@tonic-gate 	return (GLDE_OK);
48060Sstevel@tonic-gate }
48070Sstevel@tonic-gate 
48080Sstevel@tonic-gate /*
48090Sstevel@tonic-gate  * gld_enable_multi (q, mp)
48100Sstevel@tonic-gate  * Enables multicast address on the stream.  If the mac layer
48110Sstevel@tonic-gate  * isn't enabled for this address, enable at that level as well.
48120Sstevel@tonic-gate  */
48130Sstevel@tonic-gate static int
48140Sstevel@tonic-gate gld_enable_multi(queue_t *q, mblk_t *mp)
48150Sstevel@tonic-gate {
48160Sstevel@tonic-gate 	gld_t  *gld = (gld_t *)q->q_ptr;
48170Sstevel@tonic-gate 	glddev_t *glddev;
48180Sstevel@tonic-gate 	gld_mac_info_t *macinfo = gld->gld_mac_info;
48190Sstevel@tonic-gate 	unsigned char *maddr;
48200Sstevel@tonic-gate 	dl_enabmulti_req_t *multi;
48210Sstevel@tonic-gate 	gld_mcast_t *mcast;
48220Sstevel@tonic-gate 	int	i, rc;
48230Sstevel@tonic-gate 	gld_mac_pvt_t *mac_pvt;
48240Sstevel@tonic-gate 
48250Sstevel@tonic-gate #ifdef GLD_DEBUG
48260Sstevel@tonic-gate 	if (gld_debug & GLDPROT) {
48270Sstevel@tonic-gate 		cmn_err(CE_NOTE, "gld_enable_multi(%p, %p)", (void *)q,
48280Sstevel@tonic-gate 		    (void *)mp);
48290Sstevel@tonic-gate 	}
48300Sstevel@tonic-gate #endif
48310Sstevel@tonic-gate 
48320Sstevel@tonic-gate 	if (gld->gld_state == DL_UNATTACHED)
48330Sstevel@tonic-gate 		return (DL_OUTSTATE);
48340Sstevel@tonic-gate 
48350Sstevel@tonic-gate 	ASSERT(macinfo != NULL);
48360Sstevel@tonic-gate 	mac_pvt = (gld_mac_pvt_t *)macinfo->gldm_mac_pvt;
48370Sstevel@tonic-gate 
48380Sstevel@tonic-gate 	if (macinfo->gldm_set_multicast == NULL) {
48390Sstevel@tonic-gate 		return (DL_UNSUPPORTED);
48400Sstevel@tonic-gate 	}
48410Sstevel@tonic-gate 
48420Sstevel@tonic-gate 	multi = (dl_enabmulti_req_t *)mp->b_rptr;
48430Sstevel@tonic-gate 
48440Sstevel@tonic-gate 	if (!MBLKIN(mp, multi->dl_addr_offset, multi->dl_addr_length) ||
48450Sstevel@tonic-gate 	    multi->dl_addr_length != macinfo->gldm_addrlen)
48460Sstevel@tonic-gate 		return (DL_BADADDR);
48470Sstevel@tonic-gate 
48480Sstevel@tonic-gate 	/* request appears to be valid */
48490Sstevel@tonic-gate 
48500Sstevel@tonic-gate 	glddev = mac_pvt->major_dev;
48510Sstevel@tonic-gate 	ASSERT(glddev == gld->gld_device);
48520Sstevel@tonic-gate 
48530Sstevel@tonic-gate 	maddr = mp->b_rptr + multi->dl_addr_offset;
48540Sstevel@tonic-gate 
48550Sstevel@tonic-gate 	/*
48560Sstevel@tonic-gate 	 * The multicast addresses live in a per-device table, along
48570Sstevel@tonic-gate 	 * with a reference count.  Each stream has a table that
48580Sstevel@tonic-gate 	 * points to entries in the device table, with the reference
48590Sstevel@tonic-gate 	 * count reflecting the number of streams pointing at it.  If
48600Sstevel@tonic-gate 	 * this multicast address is already in the per-device table,
48610Sstevel@tonic-gate 	 * all we have to do is point at it.
48620Sstevel@tonic-gate 	 */
48630Sstevel@tonic-gate 	GLDM_LOCK(macinfo, RW_WRITER);
48640Sstevel@tonic-gate 
48650Sstevel@tonic-gate 	/* does this address appear in current table? */
48660Sstevel@tonic-gate 	if (gld->gld_mcast == NULL) {
48670Sstevel@tonic-gate 		/* no mcast addresses -- allocate table */
48680Sstevel@tonic-gate 		gld->gld_mcast = GETSTRUCT(gld_mcast_t *,
48690Sstevel@tonic-gate 					    glddev->gld_multisize);
48700Sstevel@tonic-gate 		if (gld->gld_mcast == NULL) {
48710Sstevel@tonic-gate 			GLDM_UNLOCK(macinfo);
48720Sstevel@tonic-gate 			dlerrorack(q, mp, DL_ENABMULTI_REQ, DL_SYSERR, ENOSR);
48730Sstevel@tonic-gate 			return (GLDE_OK);
48740Sstevel@tonic-gate 		}
48750Sstevel@tonic-gate 		gld->gld_multicnt = glddev->gld_multisize;
48760Sstevel@tonic-gate 	} else {
48770Sstevel@tonic-gate 		for (i = 0; i < gld->gld_multicnt; i++) {
48780Sstevel@tonic-gate 			if (gld->gld_mcast[i] &&
48790Sstevel@tonic-gate 			    mac_eq(gld->gld_mcast[i]->gldm_addr,
48800Sstevel@tonic-gate 				maddr, macinfo->gldm_addrlen)) {
48810Sstevel@tonic-gate 				/* this is a match -- just succeed */
48820Sstevel@tonic-gate 				ASSERT(gld->gld_mcast[i]->gldm_refcnt);
48830Sstevel@tonic-gate 				GLDM_UNLOCK(macinfo);
48840Sstevel@tonic-gate 				dlokack(q, mp, DL_ENABMULTI_REQ);
48850Sstevel@tonic-gate 				return (GLDE_OK);
48860Sstevel@tonic-gate 			}
48870Sstevel@tonic-gate 		}
48880Sstevel@tonic-gate 	}
48890Sstevel@tonic-gate 
48900Sstevel@tonic-gate 	/*
48910Sstevel@tonic-gate 	 * it wasn't in the stream so check to see if the mac layer has it
48920Sstevel@tonic-gate 	 */
48930Sstevel@tonic-gate 	mcast = NULL;
48940Sstevel@tonic-gate 	if (mac_pvt->mcast_table == NULL) {
48950Sstevel@tonic-gate 		mac_pvt->mcast_table = GETSTRUCT(gld_mcast_t,
48960Sstevel@tonic-gate 						glddev->gld_multisize);
48970Sstevel@tonic-gate 		if (mac_pvt->mcast_table == NULL) {
48980Sstevel@tonic-gate 			GLDM_UNLOCK(macinfo);
48990Sstevel@tonic-gate 			dlerrorack(q, mp, DL_ENABMULTI_REQ, DL_SYSERR, ENOSR);
49000Sstevel@tonic-gate 			return (GLDE_OK);
49010Sstevel@tonic-gate 		}
49020Sstevel@tonic-gate 	} else {
49030Sstevel@tonic-gate 		for (i = 0; i < glddev->gld_multisize; i++) {
49040Sstevel@tonic-gate 			if (mac_pvt->mcast_table[i].gldm_refcnt &&
49050Sstevel@tonic-gate 			    mac_eq(mac_pvt->mcast_table[i].gldm_addr,
49060Sstevel@tonic-gate 			    maddr, macinfo->gldm_addrlen)) {
49070Sstevel@tonic-gate 				mcast = &mac_pvt->mcast_table[i];
49080Sstevel@tonic-gate 				break;
49090Sstevel@tonic-gate 			}
49100Sstevel@tonic-gate 		}
49110Sstevel@tonic-gate 	}
49120Sstevel@tonic-gate 	if (mcast == NULL) {
49130Sstevel@tonic-gate 		/* not in mac layer -- find an empty mac slot to fill in */
49140Sstevel@tonic-gate 		for (i = 0; i < glddev->gld_multisize; i++) {
49150Sstevel@tonic-gate 			if (mac_pvt->mcast_table[i].gldm_refcnt == 0) {
49160Sstevel@tonic-gate 				mcast = &mac_pvt->mcast_table[i];
49170Sstevel@tonic-gate 				mac_copy(maddr, mcast->gldm_addr,
49180Sstevel@tonic-gate 				    macinfo->gldm_addrlen);
49190Sstevel@tonic-gate 				break;
49200Sstevel@tonic-gate 			}
49210Sstevel@tonic-gate 		}
49220Sstevel@tonic-gate 	}
49230Sstevel@tonic-gate 	if (mcast == NULL) {
49240Sstevel@tonic-gate 		/* couldn't get a mac layer slot */
49250Sstevel@tonic-gate 		GLDM_UNLOCK(macinfo);
49260Sstevel@tonic-gate 		return (DL_TOOMANY);
49270Sstevel@tonic-gate 	}
49280Sstevel@tonic-gate 
49290Sstevel@tonic-gate 	/* now we have a mac layer slot in mcast -- get a stream slot */
49300Sstevel@tonic-gate 	for (i = 0; i < gld->gld_multicnt; i++) {
49310Sstevel@tonic-gate 		if (gld->gld_mcast[i] != NULL)
49320Sstevel@tonic-gate 			continue;
49330Sstevel@tonic-gate 		/* found an empty slot */
49340Sstevel@tonic-gate 		if (!mcast->gldm_refcnt) {
49350Sstevel@tonic-gate 			/* set mcast in hardware */
49360Sstevel@tonic-gate 			unsigned char cmaddr[GLD_MAX_ADDRLEN];
49370Sstevel@tonic-gate 
49380Sstevel@tonic-gate 			ASSERT(sizeof (cmaddr) >= macinfo->gldm_addrlen);
49390Sstevel@tonic-gate 			cmac_copy(maddr, cmaddr,
49400Sstevel@tonic-gate 			    macinfo->gldm_addrlen, macinfo);
49410Sstevel@tonic-gate 
49420Sstevel@tonic-gate 			rc = (*macinfo->gldm_set_multicast)
49430Sstevel@tonic-gate 			    (macinfo, cmaddr, GLD_MULTI_ENABLE);
49440Sstevel@tonic-gate 			if (rc == GLD_NOTSUPPORTED) {
49450Sstevel@tonic-gate 				GLDM_UNLOCK(macinfo);
49460Sstevel@tonic-gate 				return (DL_NOTSUPPORTED);
49470Sstevel@tonic-gate 			} else if (rc == GLD_NORESOURCES) {
49480Sstevel@tonic-gate 				GLDM_UNLOCK(macinfo);
49490Sstevel@tonic-gate 				return (DL_TOOMANY);
49500Sstevel@tonic-gate 			} else if (rc == GLD_BADARG) {
49510Sstevel@tonic-gate 				GLDM_UNLOCK(macinfo);
49520Sstevel@tonic-gate 				return (DL_BADADDR);
49530Sstevel@tonic-gate 			} else if (rc == GLD_RETRY) {
49540Sstevel@tonic-gate 				/*
49550Sstevel@tonic-gate 				 * The putbq and gld_xwait must be
49560Sstevel@tonic-gate 				 * within the lock to prevent races
49570Sstevel@tonic-gate 				 * with gld_sched.
49580Sstevel@tonic-gate 				 */
49590Sstevel@tonic-gate 				(void) putbq(q, mp);
49600Sstevel@tonic-gate 				gld->gld_xwait = B_TRUE;
49610Sstevel@tonic-gate 				GLDM_UNLOCK(macinfo);
49620Sstevel@tonic-gate 				return (GLDE_RETRY);
49630Sstevel@tonic-gate 			} else if (rc != GLD_SUCCESS) {
49640Sstevel@tonic-gate 				GLDM_UNLOCK(macinfo);
49650Sstevel@tonic-gate 				dlerrorack(q, mp, DL_ENABMULTI_REQ,
49660Sstevel@tonic-gate 				    DL_SYSERR, EIO);
49670Sstevel@tonic-gate 				return (GLDE_OK);
49680Sstevel@tonic-gate 			}
49690Sstevel@tonic-gate 		}
49700Sstevel@tonic-gate 		gld->gld_mcast[i] = mcast;
49710Sstevel@tonic-gate 		mcast->gldm_refcnt++;
49720Sstevel@tonic-gate 		GLDM_UNLOCK(macinfo);
49730Sstevel@tonic-gate 		dlokack(q, mp, DL_ENABMULTI_REQ);
49740Sstevel@tonic-gate 		return (GLDE_OK);
49750Sstevel@tonic-gate 	}
49760Sstevel@tonic-gate 
49770Sstevel@tonic-gate 	/* couldn't get a stream slot */
49780Sstevel@tonic-gate 	GLDM_UNLOCK(macinfo);
49790Sstevel@tonic-gate 	return (DL_TOOMANY);
49800Sstevel@tonic-gate }
49810Sstevel@tonic-gate 
49820Sstevel@tonic-gate 
49830Sstevel@tonic-gate /*
49840Sstevel@tonic-gate  * gld_disable_multi (q, mp)
49850Sstevel@tonic-gate  * Disable the multicast address on the stream.  If last
49860Sstevel@tonic-gate  * reference for the mac layer, disable there as well.
49870Sstevel@tonic-gate  */
49880Sstevel@tonic-gate static int
49890Sstevel@tonic-gate gld_disable_multi(queue_t *q, mblk_t *mp)
49900Sstevel@tonic-gate {
49910Sstevel@tonic-gate 	gld_t  *gld;
49920Sstevel@tonic-gate 	gld_mac_info_t *macinfo;
49930Sstevel@tonic-gate 	unsigned char *maddr;
49940Sstevel@tonic-gate 	dl_disabmulti_req_t *multi;
49950Sstevel@tonic-gate 	int i;
49960Sstevel@tonic-gate 	gld_mcast_t *mcast;
49970Sstevel@tonic-gate 
49980Sstevel@tonic-gate #ifdef GLD_DEBUG
49990Sstevel@tonic-gate 	if (gld_debug & GLDPROT) {
50000Sstevel@tonic-gate 		cmn_err(CE_NOTE, "gld_disable_multi(%p, %p)", (void *)q,
50010Sstevel@tonic-gate 		    (void *)mp);
50020Sstevel@tonic-gate 	}
50030Sstevel@tonic-gate #endif
50040Sstevel@tonic-gate 
50050Sstevel@tonic-gate 	gld = (gld_t *)q->q_ptr;
50060Sstevel@tonic-gate 	if (gld->gld_state == DL_UNATTACHED)
50070Sstevel@tonic-gate 		return (DL_OUTSTATE);
50080Sstevel@tonic-gate 
50090Sstevel@tonic-gate 	macinfo = gld->gld_mac_info;
50100Sstevel@tonic-gate 	ASSERT(macinfo != NULL);
50110Sstevel@tonic-gate 	if (macinfo->gldm_set_multicast == NULL) {
50120Sstevel@tonic-gate 		return (DL_UNSUPPORTED);
50130Sstevel@tonic-gate 	}
50140Sstevel@tonic-gate 
50150Sstevel@tonic-gate 	multi = (dl_disabmulti_req_t *)mp->b_rptr;
50160Sstevel@tonic-gate 
50170Sstevel@tonic-gate 	if (!MBLKIN(mp, multi->dl_addr_offset, multi->dl_addr_length) ||
50180Sstevel@tonic-gate 	    multi->dl_addr_length != macinfo->gldm_addrlen)
50190Sstevel@tonic-gate 		return (DL_BADADDR);
50200Sstevel@tonic-gate 
50210Sstevel@tonic-gate 	maddr = mp->b_rptr + multi->dl_addr_offset;
50220Sstevel@tonic-gate 
50230Sstevel@tonic-gate 	/* request appears to be valid */
50240Sstevel@tonic-gate 	/* does this address appear in current table? */
50250Sstevel@tonic-gate 	GLDM_LOCK(macinfo, RW_WRITER);
50260Sstevel@tonic-gate 	if (gld->gld_mcast != NULL) {
50270Sstevel@tonic-gate 		for (i = 0; i < gld->gld_multicnt; i++)
50280Sstevel@tonic-gate 			if (((mcast = gld->gld_mcast[i]) != NULL) &&
50290Sstevel@tonic-gate 			    mac_eq(mcast->gldm_addr,
50300Sstevel@tonic-gate 			    maddr, macinfo->gldm_addrlen)) {
50310Sstevel@tonic-gate 				ASSERT(mcast->gldm_refcnt);
50320Sstevel@tonic-gate 				gld_send_disable_multi(macinfo, mcast);
50330Sstevel@tonic-gate 				gld->gld_mcast[i] = NULL;
50340Sstevel@tonic-gate 				GLDM_UNLOCK(macinfo);
50350Sstevel@tonic-gate 				dlokack(q, mp, DL_DISABMULTI_REQ);
50360Sstevel@tonic-gate 				return (GLDE_OK);
50370Sstevel@tonic-gate 			}
50380Sstevel@tonic-gate 	}
50390Sstevel@tonic-gate 	GLDM_UNLOCK(macinfo);
50400Sstevel@tonic-gate 	return (DL_NOTENAB); /* not an enabled address */
50410Sstevel@tonic-gate }
50420Sstevel@tonic-gate 
50430Sstevel@tonic-gate /*
50440Sstevel@tonic-gate  * gld_send_disable_multi(macinfo, mcast)
50450Sstevel@tonic-gate  * this function is used to disable a multicast address if the reference
50460Sstevel@tonic-gate  * count goes to zero. The disable request will then be forwarded to the
50470Sstevel@tonic-gate  * lower stream.
50480Sstevel@tonic-gate  */
50490Sstevel@tonic-gate static void
50500Sstevel@tonic-gate gld_send_disable_multi(gld_mac_info_t *macinfo, gld_mcast_t *mcast)
50510Sstevel@tonic-gate {
50520Sstevel@tonic-gate 	ASSERT(macinfo != NULL);
50530Sstevel@tonic-gate 	ASSERT(GLDM_LOCK_HELD_WRITE(macinfo));
50540Sstevel@tonic-gate 	ASSERT(mcast != NULL);
50550Sstevel@tonic-gate 	ASSERT(mcast->gldm_refcnt);
50560Sstevel@tonic-gate 
50570Sstevel@tonic-gate 	if (!mcast->gldm_refcnt) {
50580Sstevel@tonic-gate 		return;			/* "cannot happen" */
50590Sstevel@tonic-gate 	}
50600Sstevel@tonic-gate 
50610Sstevel@tonic-gate 	if (--mcast->gldm_refcnt > 0) {
50620Sstevel@tonic-gate 		return;
50630Sstevel@tonic-gate 	}
50640Sstevel@tonic-gate 
50650Sstevel@tonic-gate 	/*
50660Sstevel@tonic-gate 	 * This must be converted from canonical form to device form.
50670Sstevel@tonic-gate 	 * The refcnt is now zero so we can trash the data.
50680Sstevel@tonic-gate 	 */
50690Sstevel@tonic-gate 	if (macinfo->gldm_options & GLDOPT_CANONICAL_ADDR)
50700Sstevel@tonic-gate 		gld_bitreverse(mcast->gldm_addr, macinfo->gldm_addrlen);
50710Sstevel@tonic-gate 
50720Sstevel@tonic-gate 	/* XXX Ought to check for GLD_NORESOURCES or GLD_FAILURE */
50730Sstevel@tonic-gate 	(void) (*macinfo->gldm_set_multicast)
50740Sstevel@tonic-gate 	    (macinfo, mcast->gldm_addr, GLD_MULTI_DISABLE);
50750Sstevel@tonic-gate }
50760Sstevel@tonic-gate 
50770Sstevel@tonic-gate /*
50780Sstevel@tonic-gate  * gld_promisc (q, mp, req, on)
50790Sstevel@tonic-gate  *	enable or disable the use of promiscuous mode with the hardware
50800Sstevel@tonic-gate  */
50810Sstevel@tonic-gate static int
50820Sstevel@tonic-gate gld_promisc(queue_t *q, mblk_t *mp, t_uscalar_t req, boolean_t on)
50830Sstevel@tonic-gate {
50840Sstevel@tonic-gate 	gld_t *gld;
50850Sstevel@tonic-gate 	gld_mac_info_t *macinfo;
50860Sstevel@tonic-gate 	gld_mac_pvt_t *mac_pvt;
50870Sstevel@tonic-gate 	gld_vlan_t *vlan;
50880Sstevel@tonic-gate 	union DL_primitives *prim;
50890Sstevel@tonic-gate 	int macrc = GLD_SUCCESS;
50900Sstevel@tonic-gate 	int dlerr = GLDE_OK;
50910Sstevel@tonic-gate 	int op = GLD_MAC_PROMISC_NOOP;
50920Sstevel@tonic-gate 
50930Sstevel@tonic-gate #ifdef GLD_DEBUG
50940Sstevel@tonic-gate 	if (gld_debug & GLDTRACE)
50950Sstevel@tonic-gate 		cmn_err(CE_NOTE, "gld_promisc(%p, %p, %d, %d)",
50960Sstevel@tonic-gate 		    (void *)q, (void *)mp, req, on);
50970Sstevel@tonic-gate #endif
50980Sstevel@tonic-gate 
50990Sstevel@tonic-gate 	ASSERT(mp != NULL);
51000Sstevel@tonic-gate 	prim = (union DL_primitives *)mp->b_rptr;
51010Sstevel@tonic-gate 
51020Sstevel@tonic-gate 	/* XXX I think spec allows promisc in unattached state */
51030Sstevel@tonic-gate 	gld = (gld_t *)q->q_ptr;
51040Sstevel@tonic-gate 	if (gld->gld_state == DL_UNATTACHED)
51050Sstevel@tonic-gate 		return (DL_OUTSTATE);
51060Sstevel@tonic-gate 
51070Sstevel@tonic-gate 	macinfo = gld->gld_mac_info;
51080Sstevel@tonic-gate 	ASSERT(macinfo != NULL);
51090Sstevel@tonic-gate 	mac_pvt = (gld_mac_pvt_t *)macinfo->gldm_mac_pvt;
51100Sstevel@tonic-gate 
51110Sstevel@tonic-gate 	vlan = (gld_vlan_t *)gld->gld_vlan;
51120Sstevel@tonic-gate 	ASSERT(vlan != NULL);
51130Sstevel@tonic-gate 
51140Sstevel@tonic-gate 	GLDM_LOCK(macinfo, RW_WRITER);
51150Sstevel@tonic-gate 
51160Sstevel@tonic-gate 	/*
51170Sstevel@tonic-gate 	 * Work out what request (if any) has to be made to the MAC layer
51180Sstevel@tonic-gate 	 */
51190Sstevel@tonic-gate 	if (on) {
51200Sstevel@tonic-gate 		switch (prim->promiscon_req.dl_level) {
51210Sstevel@tonic-gate 		default:
51220Sstevel@tonic-gate 			dlerr = DL_UNSUPPORTED;	/* this is an error */
51230Sstevel@tonic-gate 			break;
51240Sstevel@tonic-gate 
51250Sstevel@tonic-gate 		case DL_PROMISC_PHYS:
51260Sstevel@tonic-gate 			if (mac_pvt->nprom == 0)
51270Sstevel@tonic-gate 				op = GLD_MAC_PROMISC_PHYS;
51280Sstevel@tonic-gate 			break;
51290Sstevel@tonic-gate 
51300Sstevel@tonic-gate 		case DL_PROMISC_MULTI:
51310Sstevel@tonic-gate 			if (mac_pvt->nprom_multi == 0)
51320Sstevel@tonic-gate 				if (mac_pvt->nprom == 0)
51330Sstevel@tonic-gate 					op = GLD_MAC_PROMISC_MULTI;
51340Sstevel@tonic-gate 			break;
51350Sstevel@tonic-gate 
51360Sstevel@tonic-gate 		case DL_PROMISC_SAP:
51370Sstevel@tonic-gate 			/* We can do this without reference to the MAC */
51380Sstevel@tonic-gate 			break;
51390Sstevel@tonic-gate 		}
51400Sstevel@tonic-gate 	} else {
51410Sstevel@tonic-gate 		switch (prim->promiscoff_req.dl_level) {
51420Sstevel@tonic-gate 		default:
51430Sstevel@tonic-gate 			dlerr = DL_UNSUPPORTED;	/* this is an error */
51440Sstevel@tonic-gate 			break;
51450Sstevel@tonic-gate 
51460Sstevel@tonic-gate 		case DL_PROMISC_PHYS:
51470Sstevel@tonic-gate 			if (!(gld->gld_flags & GLD_PROM_PHYS))
51480Sstevel@tonic-gate 				dlerr = DL_NOTENAB;
51490Sstevel@tonic-gate 			else if (mac_pvt->nprom == 1)
51500Sstevel@tonic-gate 				if (mac_pvt->nprom_multi)
51510Sstevel@tonic-gate 					op = GLD_MAC_PROMISC_MULTI;
51520Sstevel@tonic-gate 				else
51530Sstevel@tonic-gate 					op = GLD_MAC_PROMISC_NONE;
51540Sstevel@tonic-gate 			break;
51550Sstevel@tonic-gate 
51560Sstevel@tonic-gate 		case DL_PROMISC_MULTI:
51570Sstevel@tonic-gate 			if (!(gld->gld_flags & GLD_PROM_MULT))
51580Sstevel@tonic-gate 				dlerr = DL_NOTENAB;
51590Sstevel@tonic-gate 			else if (mac_pvt->nprom_multi == 1)
51600Sstevel@tonic-gate 				if (mac_pvt->nprom == 0)
51610Sstevel@tonic-gate 					op = GLD_MAC_PROMISC_NONE;
51620Sstevel@tonic-gate 			break;
51630Sstevel@tonic-gate 
51640Sstevel@tonic-gate 		case DL_PROMISC_SAP:
51650Sstevel@tonic-gate 			if (!(gld->gld_flags & GLD_PROM_SAP))
51660Sstevel@tonic-gate 				dlerr = DL_NOTENAB;
51670Sstevel@tonic-gate 
51680Sstevel@tonic-gate 			/* We can do this without reference to the MAC */
51690Sstevel@tonic-gate 			break;
51700Sstevel@tonic-gate 		}
51710Sstevel@tonic-gate 	}
51720Sstevel@tonic-gate 
51730Sstevel@tonic-gate 	/*
51740Sstevel@tonic-gate 	 * The request was invalid in some way so no need to continue.
51750Sstevel@tonic-gate 	 */
51760Sstevel@tonic-gate 	if (dlerr != GLDE_OK) {
51770Sstevel@tonic-gate 		GLDM_UNLOCK(macinfo);
51780Sstevel@tonic-gate 		return (dlerr);
51790Sstevel@tonic-gate 	}
51800Sstevel@tonic-gate 
51810Sstevel@tonic-gate 	/*
51820Sstevel@tonic-gate 	 * Issue the request to the MAC layer, if required
51830Sstevel@tonic-gate 	 */
51840Sstevel@tonic-gate 	if (op != GLD_MAC_PROMISC_NOOP) {
51850Sstevel@tonic-gate 		macrc = (*macinfo->gldm_set_promiscuous)(macinfo, op);
51860Sstevel@tonic-gate 	}
51870Sstevel@tonic-gate 
51880Sstevel@tonic-gate 	/*
51890Sstevel@tonic-gate 	 * On success, update the appropriate flags & refcounts
51900Sstevel@tonic-gate 	 */
51910Sstevel@tonic-gate 	if (macrc == GLD_SUCCESS) {
51920Sstevel@tonic-gate 		if (on) {
51930Sstevel@tonic-gate 			switch (prim->promiscon_req.dl_level) {
51940Sstevel@tonic-gate 			case DL_PROMISC_PHYS:
51950Sstevel@tonic-gate 				mac_pvt->nprom++;
5196*2760Sdg199075 				vlan->gldv_nprom++;
51970Sstevel@tonic-gate 				gld->gld_flags |= GLD_PROM_PHYS;
51980Sstevel@tonic-gate 				break;
51990Sstevel@tonic-gate 
52000Sstevel@tonic-gate 			case DL_PROMISC_MULTI:
52010Sstevel@tonic-gate 				mac_pvt->nprom_multi++;
5202*2760Sdg199075 				vlan->gldv_nprom++;
52030Sstevel@tonic-gate 				gld->gld_flags |= GLD_PROM_MULT;
52040Sstevel@tonic-gate 				break;
52050Sstevel@tonic-gate 
52060Sstevel@tonic-gate 			case DL_PROMISC_SAP:
52070Sstevel@tonic-gate 				gld->gld_flags |= GLD_PROM_SAP;
5208*2760Sdg199075 				vlan->gldv_nprom++;
5209*2760Sdg199075 				vlan->gldv_nvlan_sap++;
52100Sstevel@tonic-gate 				break;
52110Sstevel@tonic-gate 
52120Sstevel@tonic-gate 			default:
52130Sstevel@tonic-gate 				break;
52140Sstevel@tonic-gate 			}
52150Sstevel@tonic-gate 		} else {
52160Sstevel@tonic-gate 			switch (prim->promiscoff_req.dl_level) {
52170Sstevel@tonic-gate 			case DL_PROMISC_PHYS:
52180Sstevel@tonic-gate 				mac_pvt->nprom--;
5219*2760Sdg199075 				vlan->gldv_nprom--;
52200Sstevel@tonic-gate 				gld->gld_flags &= ~GLD_PROM_PHYS;
52210Sstevel@tonic-gate 				break;
52220Sstevel@tonic-gate 
52230Sstevel@tonic-gate 			case DL_PROMISC_MULTI:
52240Sstevel@tonic-gate 				mac_pvt->nprom_multi--;
5225*2760Sdg199075 				vlan->gldv_nprom--;
52260Sstevel@tonic-gate 				gld->gld_flags &= ~GLD_PROM_MULT;
52270Sstevel@tonic-gate 				break;
52280Sstevel@tonic-gate 
52290Sstevel@tonic-gate 			case DL_PROMISC_SAP:
52300Sstevel@tonic-gate 				gld->gld_flags &= ~GLD_PROM_SAP;
5231*2760Sdg199075 				vlan->gldv_nvlan_sap--;
5232*2760Sdg199075 				vlan->gldv_nprom--;
52330Sstevel@tonic-gate 				break;
52340Sstevel@tonic-gate 
52350Sstevel@tonic-gate 			default:
52360Sstevel@tonic-gate 				break;
52370Sstevel@tonic-gate 			}
52380Sstevel@tonic-gate 		}
52390Sstevel@tonic-gate 	} else if (macrc == GLD_RETRY) {
52400Sstevel@tonic-gate 		/*
52410Sstevel@tonic-gate 		 * The putbq and gld_xwait must be within the lock to
52420Sstevel@tonic-gate 		 * prevent races with gld_sched.
52430Sstevel@tonic-gate 		 */
52440Sstevel@tonic-gate 		(void) putbq(q, mp);
52450Sstevel@tonic-gate 		gld->gld_xwait = B_TRUE;
52460Sstevel@tonic-gate 	}
52470Sstevel@tonic-gate 
52480Sstevel@tonic-gate 	GLDM_UNLOCK(macinfo);
52490Sstevel@tonic-gate 
52500Sstevel@tonic-gate 	/*
52510Sstevel@tonic-gate 	 * Finally, decide how to reply.
52520Sstevel@tonic-gate 	 *
52530Sstevel@tonic-gate 	 * If <macrc> is not GLD_SUCCESS, the request was put to the MAC
52540Sstevel@tonic-gate 	 * layer but failed.  In such cases, we can return a DL_* error
52550Sstevel@tonic-gate 	 * code and let the caller send an error-ack reply upstream, or
52560Sstevel@tonic-gate 	 * we can send a reply here and then return GLDE_OK so that the
52570Sstevel@tonic-gate 	 * caller doesn't also respond.
52580Sstevel@tonic-gate 	 *
52590Sstevel@tonic-gate 	 * If physical-promiscuous mode was (successfully) switched on or
52600Sstevel@tonic-gate 	 * off, send a notification (DL_NOTIFY_IND) to anyone interested.
52610Sstevel@tonic-gate 	 */
52620Sstevel@tonic-gate 	switch (macrc) {
52630Sstevel@tonic-gate 	case GLD_NOTSUPPORTED:
52640Sstevel@tonic-gate 		return (DL_NOTSUPPORTED);
52650Sstevel@tonic-gate 
52660Sstevel@tonic-gate 	case GLD_NORESOURCES:
52670Sstevel@tonic-gate 		dlerrorack(q, mp, req, DL_SYSERR, ENOSR);
52680Sstevel@tonic-gate 		return (GLDE_OK);
52690Sstevel@tonic-gate 
52700Sstevel@tonic-gate 	case GLD_RETRY:
52710Sstevel@tonic-gate 		return (GLDE_RETRY);
52720Sstevel@tonic-gate 
52730Sstevel@tonic-gate 	default:
52740Sstevel@tonic-gate 		dlerrorack(q, mp, req, DL_SYSERR, EIO);
52750Sstevel@tonic-gate 		return (GLDE_OK);
52760Sstevel@tonic-gate 
52770Sstevel@tonic-gate 	case GLD_SUCCESS:
52780Sstevel@tonic-gate 		dlokack(q, mp, req);
52790Sstevel@tonic-gate 		break;
52800Sstevel@tonic-gate 	}
52810Sstevel@tonic-gate 
52820Sstevel@tonic-gate 	switch (op) {
52830Sstevel@tonic-gate 	case GLD_MAC_PROMISC_NOOP:
52840Sstevel@tonic-gate 		break;
52850Sstevel@tonic-gate 
52860Sstevel@tonic-gate 	case GLD_MAC_PROMISC_PHYS:
52870Sstevel@tonic-gate 		gld_notify_ind(macinfo, DL_NOTE_PROMISC_ON_PHYS, NULL);
52880Sstevel@tonic-gate 		break;
52890Sstevel@tonic-gate 
52900Sstevel@tonic-gate 	default:
52910Sstevel@tonic-gate 		gld_notify_ind(macinfo, DL_NOTE_PROMISC_OFF_PHYS, NULL);
52920Sstevel@tonic-gate 		break;
52930Sstevel@tonic-gate 	}
52940Sstevel@tonic-gate 
52950Sstevel@tonic-gate 	return (GLDE_OK);
52960Sstevel@tonic-gate }
52970Sstevel@tonic-gate 
52980Sstevel@tonic-gate /*
52990Sstevel@tonic-gate  * gld_physaddr()
53000Sstevel@tonic-gate  *	get the current or factory physical address value
53010Sstevel@tonic-gate  */
53020Sstevel@tonic-gate static int
53030Sstevel@tonic-gate gld_physaddr(queue_t *q, mblk_t *mp)
53040Sstevel@tonic-gate {
53050Sstevel@tonic-gate 	gld_t *gld = (gld_t *)q->q_ptr;
53060Sstevel@tonic-gate 	gld_mac_info_t *macinfo;
53070Sstevel@tonic-gate 	union DL_primitives *prim = (union DL_primitives *)mp->b_rptr;
53080Sstevel@tonic-gate 	unsigned char addr[GLD_MAX_ADDRLEN];
53090Sstevel@tonic-gate 
53100Sstevel@tonic-gate 	if (gld->gld_state == DL_UNATTACHED)
53110Sstevel@tonic-gate 		return (DL_OUTSTATE);
53120Sstevel@tonic-gate 
53130Sstevel@tonic-gate 	macinfo = (gld_mac_info_t *)gld->gld_mac_info;
53140Sstevel@tonic-gate 	ASSERT(macinfo != NULL);
53150Sstevel@tonic-gate 	ASSERT(macinfo->gldm_addrlen <= GLD_MAX_ADDRLEN);
53160Sstevel@tonic-gate 
53170Sstevel@tonic-gate 	switch (prim->physaddr_req.dl_addr_type) {
53180Sstevel@tonic-gate 	case DL_FACT_PHYS_ADDR:
53190Sstevel@tonic-gate 		mac_copy((caddr_t)macinfo->gldm_vendor_addr,
53200Sstevel@tonic-gate 		    (caddr_t)addr, macinfo->gldm_addrlen);
53210Sstevel@tonic-gate 		break;
53220Sstevel@tonic-gate 	case DL_CURR_PHYS_ADDR:
53230Sstevel@tonic-gate 		/* make a copy so we don't hold the lock across qreply */
53240Sstevel@tonic-gate 		GLDM_LOCK(macinfo, RW_WRITER);
53250Sstevel@tonic-gate 		mac_copy((caddr_t)
53260Sstevel@tonic-gate 		    ((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->curr_macaddr,
53270Sstevel@tonic-gate 		    (caddr_t)addr, macinfo->gldm_addrlen);
53280Sstevel@tonic-gate 		GLDM_UNLOCK(macinfo);
53290Sstevel@tonic-gate 		break;
53300Sstevel@tonic-gate 	default:
53310Sstevel@tonic-gate 		return (DL_BADPRIM);
53320Sstevel@tonic-gate 	}
53330Sstevel@tonic-gate 	dlphysaddrack(q, mp, (caddr_t)addr, macinfo->gldm_addrlen);
53340Sstevel@tonic-gate 	return (GLDE_OK);
53350Sstevel@tonic-gate }
53360Sstevel@tonic-gate 
53370Sstevel@tonic-gate /*
53380Sstevel@tonic-gate  * gld_setaddr()
53390Sstevel@tonic-gate  *	change the hardware's physical address to a user specified value
53400Sstevel@tonic-gate  */
53410Sstevel@tonic-gate static int
53420Sstevel@tonic-gate gld_setaddr(queue_t *q, mblk_t *mp)
53430Sstevel@tonic-gate {
53440Sstevel@tonic-gate 	gld_t *gld = (gld_t *)q->q_ptr;
53450Sstevel@tonic-gate 	gld_mac_info_t *macinfo;
53460Sstevel@tonic-gate 	gld_mac_pvt_t *mac_pvt;
53470Sstevel@tonic-gate 	union DL_primitives *prim = (union DL_primitives *)mp->b_rptr;
53480Sstevel@tonic-gate 	unsigned char *addr;
53490Sstevel@tonic-gate 	unsigned char cmaddr[GLD_MAX_ADDRLEN];
53500Sstevel@tonic-gate 	int rc;
53510Sstevel@tonic-gate 	gld_vlan_t *vlan;
53520Sstevel@tonic-gate 
53530Sstevel@tonic-gate 	if (gld->gld_state == DL_UNATTACHED)
53540Sstevel@tonic-gate 		return (DL_OUTSTATE);
53550Sstevel@tonic-gate 
53560Sstevel@tonic-gate 	vlan = (gld_vlan_t *)gld->gld_vlan;
53570Sstevel@tonic-gate 	ASSERT(vlan != NULL);
53580Sstevel@tonic-gate 
53590Sstevel@tonic-gate 	if (vlan->gldv_id != VLAN_VID_NONE)
53600Sstevel@tonic-gate 		return (DL_NOTSUPPORTED);
53610Sstevel@tonic-gate 
53620Sstevel@tonic-gate 	macinfo = (gld_mac_info_t *)gld->gld_mac_info;
53630Sstevel@tonic-gate 	ASSERT(macinfo != NULL);
53640Sstevel@tonic-gate 	mac_pvt = (gld_mac_pvt_t *)macinfo->gldm_mac_pvt;
53650Sstevel@tonic-gate 
53660Sstevel@tonic-gate 	if (!MBLKIN(mp, prim->set_physaddr_req.dl_addr_offset,
53670Sstevel@tonic-gate 	    prim->set_physaddr_req.dl_addr_length) ||
53680Sstevel@tonic-gate 	    prim->set_physaddr_req.dl_addr_length != macinfo->gldm_addrlen)
53690Sstevel@tonic-gate 		return (DL_BADADDR);
53700Sstevel@tonic-gate 
53710Sstevel@tonic-gate 	GLDM_LOCK(macinfo, RW_WRITER);
53720Sstevel@tonic-gate 
53730Sstevel@tonic-gate 	/* now do the set at the hardware level */
53740Sstevel@tonic-gate 	addr = mp->b_rptr + prim->set_physaddr_req.dl_addr_offset;
53750Sstevel@tonic-gate 	ASSERT(sizeof (cmaddr) >= macinfo->gldm_addrlen);
53760Sstevel@tonic-gate 	cmac_copy(addr, cmaddr, macinfo->gldm_addrlen, macinfo);
53770Sstevel@tonic-gate 
53780Sstevel@tonic-gate 	rc = (*macinfo->gldm_set_mac_addr)(macinfo, cmaddr);
53790Sstevel@tonic-gate 	if (rc == GLD_SUCCESS)
53800Sstevel@tonic-gate 		mac_copy(addr, mac_pvt->curr_macaddr,
53810Sstevel@tonic-gate 		    macinfo->gldm_addrlen);
53820Sstevel@tonic-gate 
53830Sstevel@tonic-gate 	GLDM_UNLOCK(macinfo);
53840Sstevel@tonic-gate 
53850Sstevel@tonic-gate 	switch (rc) {
53860Sstevel@tonic-gate 	case GLD_SUCCESS:
53870Sstevel@tonic-gate 		break;
53880Sstevel@tonic-gate 	case GLD_NOTSUPPORTED:
53890Sstevel@tonic-gate 		return (DL_NOTSUPPORTED);
53900Sstevel@tonic-gate 	case GLD_BADARG:
53910Sstevel@tonic-gate 		return (DL_BADADDR);
53920Sstevel@tonic-gate 	case GLD_NORESOURCES:
53930Sstevel@tonic-gate 		dlerrorack(q, mp, DL_SET_PHYS_ADDR_REQ, DL_SYSERR, ENOSR);
53940Sstevel@tonic-gate 		return (GLDE_OK);
53950Sstevel@tonic-gate 	default:
53960Sstevel@tonic-gate 		dlerrorack(q, mp, DL_SET_PHYS_ADDR_REQ, DL_SYSERR, EIO);
53970Sstevel@tonic-gate 		return (GLDE_OK);
53980Sstevel@tonic-gate 	}
53990Sstevel@tonic-gate 
54000Sstevel@tonic-gate 	gld_notify_ind(macinfo, DL_NOTE_PHYS_ADDR, NULL);
54010Sstevel@tonic-gate 
54020Sstevel@tonic-gate 	dlokack(q, mp, DL_SET_PHYS_ADDR_REQ);
54030Sstevel@tonic-gate 	return (GLDE_OK);
54040Sstevel@tonic-gate }
54050Sstevel@tonic-gate 
54060Sstevel@tonic-gate int
54070Sstevel@tonic-gate gld_get_statistics(queue_t *q, mblk_t *mp)
54080Sstevel@tonic-gate {
54090Sstevel@tonic-gate 	dl_get_statistics_ack_t *dlsp;
54100Sstevel@tonic-gate 	gld_t  *gld = (gld_t *)q->q_ptr;
54110Sstevel@tonic-gate 	gld_mac_info_t *macinfo = gld->gld_mac_info;
54120Sstevel@tonic-gate 	gld_mac_pvt_t *mac_pvt;
54130Sstevel@tonic-gate 
54140Sstevel@tonic-gate 	if (gld->gld_state == DL_UNATTACHED)
54150Sstevel@tonic-gate 		return (DL_OUTSTATE);
54160Sstevel@tonic-gate 
54170Sstevel@tonic-gate 	ASSERT(macinfo != NULL);
54180Sstevel@tonic-gate 
54190Sstevel@tonic-gate 	mac_pvt = (gld_mac_pvt_t *)macinfo->gldm_mac_pvt;
54200Sstevel@tonic-gate 	(void) gld_update_kstat(mac_pvt->kstatp, KSTAT_READ);
54210Sstevel@tonic-gate 
54220Sstevel@tonic-gate 	mp = mexchange(q, mp, DL_GET_STATISTICS_ACK_SIZE +
54230Sstevel@tonic-gate 	    sizeof (struct gldkstats), M_PCPROTO, DL_GET_STATISTICS_ACK);
54240Sstevel@tonic-gate 
54250Sstevel@tonic-gate 	if (mp == NULL)
54260Sstevel@tonic-gate 		return (GLDE_OK);	/* mexchange already sent merror */
54270Sstevel@tonic-gate 
54280Sstevel@tonic-gate 	dlsp = (dl_get_statistics_ack_t *)mp->b_rptr;
54290Sstevel@tonic-gate 	dlsp->dl_primitive = DL_GET_STATISTICS_ACK;
54300Sstevel@tonic-gate 	dlsp->dl_stat_length = sizeof (struct gldkstats);
54310Sstevel@tonic-gate 	dlsp->dl_stat_offset = DL_GET_STATISTICS_ACK_SIZE;
54320Sstevel@tonic-gate 
54330Sstevel@tonic-gate 	GLDM_LOCK(macinfo, RW_WRITER);
54340Sstevel@tonic-gate 	bcopy(mac_pvt->kstatp->ks_data,
54350Sstevel@tonic-gate 	    (mp->b_rptr + DL_GET_STATISTICS_ACK_SIZE),
54360Sstevel@tonic-gate 	    sizeof (struct gldkstats));
54370Sstevel@tonic-gate 	GLDM_UNLOCK(macinfo);
54380Sstevel@tonic-gate 
54390Sstevel@tonic-gate 	qreply(q, mp);
54400Sstevel@tonic-gate 	return (GLDE_OK);
54410Sstevel@tonic-gate }
54420Sstevel@tonic-gate 
54430Sstevel@tonic-gate /* =================================================== */
54440Sstevel@tonic-gate /* misc utilities, some requiring various mutexes held */
54450Sstevel@tonic-gate /* =================================================== */
54460Sstevel@tonic-gate 
54470Sstevel@tonic-gate /*
54480Sstevel@tonic-gate  * Initialize and start the driver.
54490Sstevel@tonic-gate  */
54500Sstevel@tonic-gate static int
54510Sstevel@tonic-gate gld_start_mac(gld_mac_info_t *macinfo)
54520Sstevel@tonic-gate {
54530Sstevel@tonic-gate 	int	rc;
54540Sstevel@tonic-gate 	unsigned char cmaddr[GLD_MAX_ADDRLEN];
54550Sstevel@tonic-gate 	gld_mac_pvt_t *mac_pvt = (gld_mac_pvt_t *)macinfo->gldm_mac_pvt;
54560Sstevel@tonic-gate 
54570Sstevel@tonic-gate 	ASSERT(GLDM_LOCK_HELD_WRITE(macinfo));
54580Sstevel@tonic-gate 	ASSERT(!mac_pvt->started);
54590Sstevel@tonic-gate 
54600Sstevel@tonic-gate 	rc = (*macinfo->gldm_reset)(macinfo);
54610Sstevel@tonic-gate 	if (rc != GLD_SUCCESS)
54620Sstevel@tonic-gate 		return (GLD_FAILURE);
54630Sstevel@tonic-gate 
54640Sstevel@tonic-gate 	/* set the addr after we reset the device */
54650Sstevel@tonic-gate 	ASSERT(sizeof (cmaddr) >= macinfo->gldm_addrlen);
54660Sstevel@tonic-gate 	cmac_copy(((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)
54670Sstevel@tonic-gate 	    ->curr_macaddr, cmaddr, macinfo->gldm_addrlen, macinfo);
54680Sstevel@tonic-gate 
54690Sstevel@tonic-gate 	rc = (*macinfo->gldm_set_mac_addr)(macinfo, cmaddr);
54700Sstevel@tonic-gate 	ASSERT(rc != GLD_BADARG);  /* this address was good before */
54710Sstevel@tonic-gate 	if (rc != GLD_SUCCESS && rc != GLD_NOTSUPPORTED)
54720Sstevel@tonic-gate 		return (GLD_FAILURE);
54730Sstevel@tonic-gate 
54740Sstevel@tonic-gate 	rc = (*macinfo->gldm_start)(macinfo);
54750Sstevel@tonic-gate 	if (rc != GLD_SUCCESS)
54760Sstevel@tonic-gate 		return (GLD_FAILURE);
54770Sstevel@tonic-gate 
54780Sstevel@tonic-gate 	mac_pvt->started = B_TRUE;
54790Sstevel@tonic-gate 	return (GLD_SUCCESS);
54800Sstevel@tonic-gate }
54810Sstevel@tonic-gate 
54820Sstevel@tonic-gate /*
54830Sstevel@tonic-gate  * Stop the driver.
54840Sstevel@tonic-gate  */
54850Sstevel@tonic-gate static void
54860Sstevel@tonic-gate gld_stop_mac(gld_mac_info_t *macinfo)
54870Sstevel@tonic-gate {
54880Sstevel@tonic-gate 	gld_mac_pvt_t *mac_pvt = (gld_mac_pvt_t *)macinfo->gldm_mac_pvt;
54890Sstevel@tonic-gate 
54900Sstevel@tonic-gate 	ASSERT(GLDM_LOCK_HELD_WRITE(macinfo));
54910Sstevel@tonic-gate 	ASSERT(mac_pvt->started);
54920Sstevel@tonic-gate 
54930Sstevel@tonic-gate 	(void) (*macinfo->gldm_stop)(macinfo);
54940Sstevel@tonic-gate 
54950Sstevel@tonic-gate 	mac_pvt->started = B_FALSE;
54960Sstevel@tonic-gate }
54970Sstevel@tonic-gate 
54980Sstevel@tonic-gate 
54990Sstevel@tonic-gate /*
55000Sstevel@tonic-gate  * gld_set_ipq will set a pointer to the queue which is bound to the
55010Sstevel@tonic-gate  * IP sap if:
55020Sstevel@tonic-gate  * o the device type is ethernet or IPoIB.
55030Sstevel@tonic-gate  * o there is no stream in SAP promiscuous mode.
55040Sstevel@tonic-gate  * o there is exactly one stream bound to the IP sap.
55050Sstevel@tonic-gate  * o the stream is in "fastpath" mode.
55060Sstevel@tonic-gate  */
55070Sstevel@tonic-gate static void
55080Sstevel@tonic-gate gld_set_ipq(gld_t *gld)
55090Sstevel@tonic-gate {
55100Sstevel@tonic-gate 	gld_vlan_t	*vlan;
55110Sstevel@tonic-gate 	gld_mac_info_t	*macinfo = gld->gld_mac_info;
55120Sstevel@tonic-gate 	gld_t		*ip_gld = NULL;
55130Sstevel@tonic-gate 	uint_t		ipq_candidates = 0;
55140Sstevel@tonic-gate 	gld_t		*ipv6_gld = NULL;
55150Sstevel@tonic-gate 	uint_t		ipv6q_candidates = 0;
55160Sstevel@tonic-gate 
55170Sstevel@tonic-gate 	ASSERT(GLDM_LOCK_HELD_WRITE(macinfo));
55180Sstevel@tonic-gate 
55190Sstevel@tonic-gate 	/* The ipq code in gld_recv() is intimate with ethernet/IPoIB */
55200Sstevel@tonic-gate 	if (((macinfo->gldm_type != DL_ETHER) &&
55210Sstevel@tonic-gate 	    (macinfo->gldm_type != DL_IB)) ||
55220Sstevel@tonic-gate 	    (gld_global_options & GLD_OPT_NO_IPQ))
55230Sstevel@tonic-gate 		return;
55240Sstevel@tonic-gate 
55250Sstevel@tonic-gate 	vlan = (gld_vlan_t *)gld->gld_vlan;
55260Sstevel@tonic-gate 	ASSERT(vlan != NULL);
55270Sstevel@tonic-gate 
55280Sstevel@tonic-gate 	/* clear down any previously defined ipqs */
55290Sstevel@tonic-gate 	vlan->gldv_ipq = NULL;
55300Sstevel@tonic-gate 	vlan->gldv_ipv6q = NULL;
55310Sstevel@tonic-gate 
55320Sstevel@tonic-gate 	/* Try to find a single stream eligible to receive IP packets */
55330Sstevel@tonic-gate 	for (gld = vlan->gldv_str_next;
55340Sstevel@tonic-gate 	    gld != (gld_t *)&vlan->gldv_str_next; gld = gld->gld_next) {
55350Sstevel@tonic-gate 		if (gld->gld_state != DL_IDLE)
55360Sstevel@tonic-gate 			continue;	/* not eligible to receive */
55370Sstevel@tonic-gate 		if (gld->gld_flags & GLD_STR_CLOSING)
55380Sstevel@tonic-gate 			continue;	/* not eligible to receive */
55390Sstevel@tonic-gate 
55400Sstevel@tonic-gate 		if (gld->gld_sap == ETHERTYPE_IP) {
55410Sstevel@tonic-gate 			ip_gld = gld;
55420Sstevel@tonic-gate 			ipq_candidates++;
55430Sstevel@tonic-gate 		}
55440Sstevel@tonic-gate 
55450Sstevel@tonic-gate 		if (gld->gld_sap == ETHERTYPE_IPV6) {
55460Sstevel@tonic-gate 			ipv6_gld = gld;
55470Sstevel@tonic-gate 			ipv6q_candidates++;
55480Sstevel@tonic-gate 		}
55490Sstevel@tonic-gate 	}
55500Sstevel@tonic-gate 
55510Sstevel@tonic-gate 	if (ipq_candidates == 1) {
55520Sstevel@tonic-gate 		ASSERT(ip_gld != NULL);
55530Sstevel@tonic-gate 
55540Sstevel@tonic-gate 		if (ip_gld->gld_flags & GLD_FAST)	/* eligible for ipq */
55550Sstevel@tonic-gate 			vlan->gldv_ipq = ip_gld->gld_qptr;
55560Sstevel@tonic-gate 	}
55570Sstevel@tonic-gate 
55580Sstevel@tonic-gate 	if (ipv6q_candidates == 1) {
55590Sstevel@tonic-gate 		ASSERT(ipv6_gld != NULL);
55600Sstevel@tonic-gate 
55610Sstevel@tonic-gate 		if (ipv6_gld->gld_flags & GLD_FAST)	/* eligible for ipq */
55620Sstevel@tonic-gate 			vlan->gldv_ipv6q = ipv6_gld->gld_qptr;
55630Sstevel@tonic-gate 	}
55640Sstevel@tonic-gate }
55650Sstevel@tonic-gate 
55660Sstevel@tonic-gate /*
55670Sstevel@tonic-gate  * gld_flushqueue (q)
55680Sstevel@tonic-gate  *	used by DLPI primitives that require flushing the queues.
55690Sstevel@tonic-gate  *	essentially, this is DL_UNBIND_REQ.
55700Sstevel@tonic-gate  */
55710Sstevel@tonic-gate static void
55720Sstevel@tonic-gate gld_flushqueue(queue_t *q)
55730Sstevel@tonic-gate {
55740Sstevel@tonic-gate 	/* flush all data in both queues */
55750Sstevel@tonic-gate 	/* XXX Should these be FLUSHALL? */
55760Sstevel@tonic-gate 	flushq(q, FLUSHDATA);
55770Sstevel@tonic-gate 	flushq(WR(q), FLUSHDATA);
55780Sstevel@tonic-gate 	/* flush all the queues upstream */
55790Sstevel@tonic-gate 	(void) putctl1(q, M_FLUSH, FLUSHRW);
55800Sstevel@tonic-gate }
55810Sstevel@tonic-gate 
55820Sstevel@tonic-gate /*
55830Sstevel@tonic-gate  * gld_devlookup (major)
55840Sstevel@tonic-gate  * search the device table for the device with specified
55850Sstevel@tonic-gate  * major number and return a pointer to it if it exists
55860Sstevel@tonic-gate  */
55870Sstevel@tonic-gate static glddev_t *
55880Sstevel@tonic-gate gld_devlookup(int major)
55890Sstevel@tonic-gate {
55900Sstevel@tonic-gate 	struct glddevice *dev;
55910Sstevel@tonic-gate 
55920Sstevel@tonic-gate 	ASSERT(mutex_owned(&gld_device_list.gld_devlock));
55930Sstevel@tonic-gate 
55940Sstevel@tonic-gate 	for (dev = gld_device_list.gld_next;
55950Sstevel@tonic-gate 	    dev != &gld_device_list;
55960Sstevel@tonic-gate 	    dev = dev->gld_next) {
55970Sstevel@tonic-gate 		ASSERT(dev);
55980Sstevel@tonic-gate 		if (dev->gld_major == major)
55990Sstevel@tonic-gate 			return (dev);
56000Sstevel@tonic-gate 	}
56010Sstevel@tonic-gate 	return (NULL);
56020Sstevel@tonic-gate }
56030Sstevel@tonic-gate 
56040Sstevel@tonic-gate /*
56050Sstevel@tonic-gate  * gld_findminor(device)
56060Sstevel@tonic-gate  * Returns a minor number currently unused by any stream in the current
56070Sstevel@tonic-gate  * device class (major) list.
56080Sstevel@tonic-gate  */
56090Sstevel@tonic-gate static int
56100Sstevel@tonic-gate gld_findminor(glddev_t *device)
56110Sstevel@tonic-gate {
56120Sstevel@tonic-gate 	gld_t		*next;
56130Sstevel@tonic-gate 	gld_mac_info_t	*nextmac;
56140Sstevel@tonic-gate 	gld_vlan_t	*nextvlan;
56150Sstevel@tonic-gate 	int		minor;
56160Sstevel@tonic-gate 	int		i;
56170Sstevel@tonic-gate 
56180Sstevel@tonic-gate 	ASSERT(mutex_owned(&device->gld_devlock));
56190Sstevel@tonic-gate 
56200Sstevel@tonic-gate 	/* The fast way */
56210Sstevel@tonic-gate 	if (device->gld_nextminor >= GLD_MIN_CLONE_MINOR &&
56220Sstevel@tonic-gate 	    device->gld_nextminor <= GLD_MAX_CLONE_MINOR)
56230Sstevel@tonic-gate 		return (device->gld_nextminor++);
56240Sstevel@tonic-gate 
56250Sstevel@tonic-gate 	/* The steady way */
56260Sstevel@tonic-gate 	for (minor = GLD_MIN_CLONE_MINOR; minor <= GLD_MAX_CLONE_MINOR;
56270Sstevel@tonic-gate 	    minor++) {
56280Sstevel@tonic-gate 		/* Search all unattached streams */
56290Sstevel@tonic-gate 		for (next = device->gld_str_next;
56300Sstevel@tonic-gate 		    next != (gld_t *)&device->gld_str_next;
56310Sstevel@tonic-gate 		    next = next->gld_next) {
56320Sstevel@tonic-gate 			if (minor == next->gld_minor)
56330Sstevel@tonic-gate 				goto nextminor;
56340Sstevel@tonic-gate 		}
56350Sstevel@tonic-gate 		/* Search all attached streams; we don't need maclock because */
56360Sstevel@tonic-gate 		/* mac stream list is protected by devlock as well as maclock */
56370Sstevel@tonic-gate 		for (nextmac = device->gld_mac_next;
56380Sstevel@tonic-gate 		    nextmac != (gld_mac_info_t *)&device->gld_mac_next;
56390Sstevel@tonic-gate 		    nextmac = nextmac->gldm_next) {
56400Sstevel@tonic-gate 			gld_mac_pvt_t *pvt =
56410Sstevel@tonic-gate 			    (gld_mac_pvt_t *)nextmac->gldm_mac_pvt;
56420Sstevel@tonic-gate 
56430Sstevel@tonic-gate 			if (!(nextmac->gldm_GLD_flags & GLD_MAC_READY))
56440Sstevel@tonic-gate 				continue;	/* this one's not ready yet */
56450Sstevel@tonic-gate 
56460Sstevel@tonic-gate 			for (i = 0; i < VLAN_HASHSZ; i++) {
56470Sstevel@tonic-gate 				for (nextvlan = pvt->vlan_hash[i];
56480Sstevel@tonic-gate 				    nextvlan != NULL;
56490Sstevel@tonic-gate 				    nextvlan = nextvlan->gldv_next) {
56500Sstevel@tonic-gate 					for (next = nextvlan->gldv_str_next;
56510Sstevel@tonic-gate 					    next !=
56520Sstevel@tonic-gate 					    (gld_t *)&nextvlan->gldv_str_next;
56530Sstevel@tonic-gate 					    next = next->gld_next) {
56540Sstevel@tonic-gate 						if (minor == next->gld_minor)
56550Sstevel@tonic-gate 							goto nextminor;
56560Sstevel@tonic-gate 					}
56570Sstevel@tonic-gate 				}
56580Sstevel@tonic-gate 			}
56590Sstevel@tonic-gate 		}
56600Sstevel@tonic-gate 
56610Sstevel@tonic-gate 		return (minor);
56620Sstevel@tonic-gate nextminor:
56630Sstevel@tonic-gate 		/* don't need to do anything */
56640Sstevel@tonic-gate 		;
56650Sstevel@tonic-gate 	}
56660Sstevel@tonic-gate 	cmn_err(CE_WARN, "GLD ran out of minor numbers for %s",
56670Sstevel@tonic-gate 		device->gld_name);
56680Sstevel@tonic-gate 	return (0);
56690Sstevel@tonic-gate }
56700Sstevel@tonic-gate 
56710Sstevel@tonic-gate /*
56720Sstevel@tonic-gate  * version of insque/remque for use by this driver
56730Sstevel@tonic-gate  */
56740Sstevel@tonic-gate struct qelem {
56750Sstevel@tonic-gate 	struct qelem *q_forw;
56760Sstevel@tonic-gate 	struct qelem *q_back;
56770Sstevel@tonic-gate 	/* rest of structure */
56780Sstevel@tonic-gate };
56790Sstevel@tonic-gate 
56800Sstevel@tonic-gate static void
56810Sstevel@tonic-gate gldinsque(void *elem, void *pred)
56820Sstevel@tonic-gate {
56830Sstevel@tonic-gate 	struct qelem *pelem = elem;
56840Sstevel@tonic-gate 	struct qelem *ppred = pred;
56850Sstevel@tonic-gate 	struct qelem *pnext = ppred->q_forw;
56860Sstevel@tonic-gate 
56870Sstevel@tonic-gate 	pelem->q_forw = pnext;
56880Sstevel@tonic-gate 	pelem->q_back = ppred;
56890Sstevel@tonic-gate 	ppred->q_forw = pelem;
56900Sstevel@tonic-gate 	pnext->q_back = pelem;
56910Sstevel@tonic-gate }
56920Sstevel@tonic-gate 
56930Sstevel@tonic-gate static void
56940Sstevel@tonic-gate gldremque(void *arg)
56950Sstevel@tonic-gate {
56960Sstevel@tonic-gate 	struct qelem *pelem = arg;
56970Sstevel@tonic-gate 	struct qelem *elem = arg;
56980Sstevel@tonic-gate 
56990Sstevel@tonic-gate 	pelem->q_forw->q_back = pelem->q_back;
57000Sstevel@tonic-gate 	pelem->q_back->q_forw = pelem->q_forw;
57010Sstevel@tonic-gate 	elem->q_back = elem->q_forw = NULL;
57020Sstevel@tonic-gate }
57030Sstevel@tonic-gate 
57040Sstevel@tonic-gate static gld_vlan_t *
57050Sstevel@tonic-gate gld_add_vlan(gld_mac_info_t *macinfo, uint32_t vid)
57060Sstevel@tonic-gate {
57070Sstevel@tonic-gate 	gld_mac_pvt_t	*mac_pvt = (gld_mac_pvt_t *)macinfo->gldm_mac_pvt;
57080Sstevel@tonic-gate 	gld_vlan_t	**pp;
57090Sstevel@tonic-gate 	gld_vlan_t	*p;
57100Sstevel@tonic-gate 
57110Sstevel@tonic-gate 	pp = &(mac_pvt->vlan_hash[vid % VLAN_HASHSZ]);
57120Sstevel@tonic-gate 	while ((p = *pp) != NULL) {
57130Sstevel@tonic-gate 		ASSERT(p->gldv_id != vid);
57140Sstevel@tonic-gate 		pp = &(p->gldv_next);
57150Sstevel@tonic-gate 	}
57160Sstevel@tonic-gate 
57170Sstevel@tonic-gate 	if ((p = kmem_zalloc(sizeof (gld_vlan_t), KM_NOSLEEP)) == NULL)
57180Sstevel@tonic-gate 		return (NULL);
57190Sstevel@tonic-gate 
57200Sstevel@tonic-gate 	p->gldv_mac = macinfo;
57210Sstevel@tonic-gate 	p->gldv_id = vid;
57220Sstevel@tonic-gate 
57230Sstevel@tonic-gate 	if (vid == VLAN_VID_NONE) {
57240Sstevel@tonic-gate 		p->gldv_ptag = VLAN_VTAG_NONE;
57250Sstevel@tonic-gate 		p->gldv_stats = mac_pvt->statistics;
57260Sstevel@tonic-gate 		p->gldv_kstatp = NULL;
57270Sstevel@tonic-gate 	} else {
57280Sstevel@tonic-gate 		p->gldv_ptag = GLD_MK_PTAG(VLAN_CFI_ETHER, vid);
57290Sstevel@tonic-gate 		p->gldv_stats = kmem_zalloc(sizeof (struct gld_stats),
57300Sstevel@tonic-gate 		    KM_SLEEP);
57310Sstevel@tonic-gate 
57320Sstevel@tonic-gate 		if (gld_init_vlan_stats(p) != GLD_SUCCESS) {
57330Sstevel@tonic-gate 			kmem_free(p->gldv_stats, sizeof (struct gld_stats));
57340Sstevel@tonic-gate 			kmem_free(p, sizeof (gld_vlan_t));
57350Sstevel@tonic-gate 			return (NULL);
57360Sstevel@tonic-gate 		}
57370Sstevel@tonic-gate 	}
57380Sstevel@tonic-gate 
57390Sstevel@tonic-gate 	p->gldv_str_next = p->gldv_str_prev = (gld_t *)&p->gldv_str_next;
57400Sstevel@tonic-gate 	mac_pvt->nvlan++;
57410Sstevel@tonic-gate 	*pp = p;
57420Sstevel@tonic-gate 
57430Sstevel@tonic-gate 	return (p);
57440Sstevel@tonic-gate }
57450Sstevel@tonic-gate 
57460Sstevel@tonic-gate static void
57470Sstevel@tonic-gate gld_rem_vlan(gld_vlan_t *vlan)
57480Sstevel@tonic-gate {
57490Sstevel@tonic-gate 	gld_mac_info_t	*macinfo = vlan->gldv_mac;
57500Sstevel@tonic-gate 	gld_mac_pvt_t	*mac_pvt = (gld_mac_pvt_t *)macinfo->gldm_mac_pvt;
57510Sstevel@tonic-gate 	gld_vlan_t	**pp;
57520Sstevel@tonic-gate 	gld_vlan_t	*p;
57530Sstevel@tonic-gate 
57540Sstevel@tonic-gate 	pp = &(mac_pvt->vlan_hash[vlan->gldv_id % VLAN_HASHSZ]);
57550Sstevel@tonic-gate 	while ((p = *pp) != NULL) {
57560Sstevel@tonic-gate 		if (p->gldv_id == vlan->gldv_id)
57570Sstevel@tonic-gate 			break;
57580Sstevel@tonic-gate 		pp = &(p->gldv_next);
57590Sstevel@tonic-gate 	}
57600Sstevel@tonic-gate 	ASSERT(p != NULL);
57610Sstevel@tonic-gate 
57620Sstevel@tonic-gate 	*pp = p->gldv_next;
57630Sstevel@tonic-gate 	mac_pvt->nvlan--;
57640Sstevel@tonic-gate 	if (p->gldv_id != VLAN_VID_NONE) {
57650Sstevel@tonic-gate 		ASSERT(p->gldv_kstatp != NULL);
57660Sstevel@tonic-gate 		kstat_delete(p->gldv_kstatp);
57670Sstevel@tonic-gate 		kmem_free(p->gldv_stats, sizeof (struct gld_stats));
57680Sstevel@tonic-gate 	}
57690Sstevel@tonic-gate 	kmem_free(p, sizeof (gld_vlan_t));
57700Sstevel@tonic-gate }
57710Sstevel@tonic-gate 
57720Sstevel@tonic-gate gld_vlan_t *
57730Sstevel@tonic-gate gld_find_vlan(gld_mac_info_t *macinfo, uint32_t vid)
57740Sstevel@tonic-gate {
57750Sstevel@tonic-gate 	gld_mac_pvt_t	*mac_pvt = (gld_mac_pvt_t *)macinfo->gldm_mac_pvt;
57760Sstevel@tonic-gate 	gld_vlan_t	*p;
57770Sstevel@tonic-gate 
57780Sstevel@tonic-gate 	p = mac_pvt->vlan_hash[vid % VLAN_HASHSZ];
57790Sstevel@tonic-gate 	while (p != NULL) {
57800Sstevel@tonic-gate 		if (p->gldv_id == vid)
57810Sstevel@tonic-gate 			return (p);
57820Sstevel@tonic-gate 		p = p->gldv_next;
57830Sstevel@tonic-gate 	}
57840Sstevel@tonic-gate 	return (NULL);
57850Sstevel@tonic-gate }
57860Sstevel@tonic-gate 
57870Sstevel@tonic-gate gld_vlan_t *
57880Sstevel@tonic-gate gld_get_vlan(gld_mac_info_t *macinfo, uint32_t vid)
57890Sstevel@tonic-gate {
57900Sstevel@tonic-gate 	gld_vlan_t	*vlan;
57910Sstevel@tonic-gate 
57920Sstevel@tonic-gate 	if ((vlan = gld_find_vlan(macinfo, vid)) == NULL)
57930Sstevel@tonic-gate 		vlan = gld_add_vlan(macinfo, vid);
57940Sstevel@tonic-gate 
57950Sstevel@tonic-gate 	return (vlan);
57960Sstevel@tonic-gate }
57970Sstevel@tonic-gate 
57980Sstevel@tonic-gate /*
57990Sstevel@tonic-gate  * gld_bitrevcopy()
58000Sstevel@tonic-gate  * This is essentially bcopy, with the ability to bit reverse the
58010Sstevel@tonic-gate  * the source bytes. The MAC addresses bytes as transmitted by FDDI
58020Sstevel@tonic-gate  * interfaces are bit reversed.
58030Sstevel@tonic-gate  */
58040Sstevel@tonic-gate void
58050Sstevel@tonic-gate gld_bitrevcopy(caddr_t src, caddr_t target, size_t n)
58060Sstevel@tonic-gate {
58070Sstevel@tonic-gate 	while (n--)
58080Sstevel@tonic-gate 		*target++ = bit_rev[(uchar_t)*src++];
58090Sstevel@tonic-gate }
58100Sstevel@tonic-gate 
58110Sstevel@tonic-gate /*
58120Sstevel@tonic-gate  * gld_bitreverse()
58130Sstevel@tonic-gate  * Convert the bit order by swaping all the bits, using a
58140Sstevel@tonic-gate  * lookup table.
58150Sstevel@tonic-gate  */
58160Sstevel@tonic-gate void
58170Sstevel@tonic-gate gld_bitreverse(uchar_t *rptr, size_t n)
58180Sstevel@tonic-gate {
58190Sstevel@tonic-gate 	while (n--) {
58200Sstevel@tonic-gate 		*rptr = bit_rev[*rptr];
58210Sstevel@tonic-gate 		rptr++;
58220Sstevel@tonic-gate 	}
58230Sstevel@tonic-gate }
58240Sstevel@tonic-gate 
58250Sstevel@tonic-gate char *
58260Sstevel@tonic-gate gld_macaddr_sprintf(char *etherbuf, unsigned char *ap, int len)
58270Sstevel@tonic-gate {
58280Sstevel@tonic-gate 	int i;
58290Sstevel@tonic-gate 	char *cp = etherbuf;
58300Sstevel@tonic-gate 	static char digits[] = "0123456789abcdef";
58310Sstevel@tonic-gate 
58320Sstevel@tonic-gate 	for (i = 0; i < len; i++) {
58330Sstevel@tonic-gate 		*cp++ = digits[*ap >> 4];
58340Sstevel@tonic-gate 		*cp++ = digits[*ap++ & 0xf];
58350Sstevel@tonic-gate 		*cp++ = ':';
58360Sstevel@tonic-gate 	}
58370Sstevel@tonic-gate 	*--cp = 0;
58380Sstevel@tonic-gate 	return (etherbuf);
58390Sstevel@tonic-gate }
58400Sstevel@tonic-gate 
58410Sstevel@tonic-gate #ifdef GLD_DEBUG
58420Sstevel@tonic-gate static void
58430Sstevel@tonic-gate gld_check_assertions()
58440Sstevel@tonic-gate {
58450Sstevel@tonic-gate 	glddev_t	*dev;
58460Sstevel@tonic-gate 	gld_mac_info_t	*mac;
58470Sstevel@tonic-gate 	gld_t		*str;
58480Sstevel@tonic-gate 	gld_vlan_t	*vlan;
58490Sstevel@tonic-gate 	int		i;
58500Sstevel@tonic-gate 
58510Sstevel@tonic-gate 	mutex_enter(&gld_device_list.gld_devlock);
58520Sstevel@tonic-gate 
58530Sstevel@tonic-gate 	for (dev = gld_device_list.gld_next;
58540Sstevel@tonic-gate 	    dev != (glddev_t *)&gld_device_list.gld_next;
58550Sstevel@tonic-gate 	    dev = dev->gld_next) {
58560Sstevel@tonic-gate 		mutex_enter(&dev->gld_devlock);
58570Sstevel@tonic-gate 		ASSERT(dev->gld_broadcast != NULL);
58580Sstevel@tonic-gate 		for (str = dev->gld_str_next;
58590Sstevel@tonic-gate 		    str != (gld_t *)&dev->gld_str_next;
58600Sstevel@tonic-gate 		    str = str->gld_next) {
58610Sstevel@tonic-gate 			ASSERT(str->gld_device == dev);
58620Sstevel@tonic-gate 			ASSERT(str->gld_mac_info == NULL);
58630Sstevel@tonic-gate 			ASSERT(str->gld_qptr != NULL);
58640Sstevel@tonic-gate 			ASSERT(str->gld_minor >= GLD_MIN_CLONE_MINOR);
58650Sstevel@tonic-gate 			ASSERT(str->gld_multicnt == 0);
58660Sstevel@tonic-gate 			ASSERT(str->gld_mcast == NULL);
58670Sstevel@tonic-gate 			ASSERT(!(str->gld_flags &
58680Sstevel@tonic-gate 			    (GLD_PROM_PHYS|GLD_PROM_MULT|GLD_PROM_SAP)));
58690Sstevel@tonic-gate 			ASSERT(str->gld_sap == 0);
58700Sstevel@tonic-gate 			ASSERT(str->gld_state == DL_UNATTACHED);
58710Sstevel@tonic-gate 		}
58720Sstevel@tonic-gate 		for (mac = dev->gld_mac_next;
58730Sstevel@tonic-gate 		    mac != (gld_mac_info_t *)&dev->gld_mac_next;
58740Sstevel@tonic-gate 		    mac = mac->gldm_next) {
58750Sstevel@tonic-gate 			int nvlan = 0;
58760Sstevel@tonic-gate 			gld_mac_pvt_t *pvt = (gld_mac_pvt_t *)mac->gldm_mac_pvt;
58770Sstevel@tonic-gate 
58780Sstevel@tonic-gate 			if (!(mac->gldm_GLD_flags & GLD_MAC_READY))
58790Sstevel@tonic-gate 				continue;	/* this one's not ready yet */
58800Sstevel@tonic-gate 
58810Sstevel@tonic-gate 			GLDM_LOCK(mac, RW_WRITER);
58820Sstevel@tonic-gate 			ASSERT(mac->gldm_devinfo != NULL);
58830Sstevel@tonic-gate 			ASSERT(mac->gldm_mac_pvt != NULL);
58840Sstevel@tonic-gate 			ASSERT(pvt->interfacep != NULL);
58850Sstevel@tonic-gate 			ASSERT(pvt->kstatp != NULL);
58860Sstevel@tonic-gate 			ASSERT(pvt->statistics != NULL);
58870Sstevel@tonic-gate 			ASSERT(pvt->major_dev == dev);
58880Sstevel@tonic-gate 
58890Sstevel@tonic-gate 			for (i = 0; i < VLAN_HASHSZ; i++) {
58900Sstevel@tonic-gate 				for (vlan = pvt->vlan_hash[i];
58910Sstevel@tonic-gate 				    vlan != NULL; vlan = vlan->gldv_next) {
58920Sstevel@tonic-gate 					int nstr = 0;
58930Sstevel@tonic-gate 
58940Sstevel@tonic-gate 					ASSERT(vlan->gldv_mac == mac);
58950Sstevel@tonic-gate 
58960Sstevel@tonic-gate 					for (str = vlan->gldv_str_next;
58970Sstevel@tonic-gate 					    str !=
58980Sstevel@tonic-gate 					    (gld_t *)&vlan->gldv_str_next;
58990Sstevel@tonic-gate 					    str = str->gld_next) {
59000Sstevel@tonic-gate 						ASSERT(str->gld_device == dev);
59010Sstevel@tonic-gate 						ASSERT(str->gld_mac_info ==
59020Sstevel@tonic-gate 						    mac);
59030Sstevel@tonic-gate 						ASSERT(str->gld_qptr != NULL);
59040Sstevel@tonic-gate 						ASSERT(str->gld_minor >=
59050Sstevel@tonic-gate 						    GLD_MIN_CLONE_MINOR);
59060Sstevel@tonic-gate 						ASSERT(
59070Sstevel@tonic-gate 						    str->gld_multicnt == 0 ||
59080Sstevel@tonic-gate 						    str->gld_mcast);
59090Sstevel@tonic-gate 						nstr++;
59100Sstevel@tonic-gate 					}
59110Sstevel@tonic-gate 					ASSERT(vlan->gldv_nstreams == nstr);
59120Sstevel@tonic-gate 					nvlan++;
59130Sstevel@tonic-gate 				}
59140Sstevel@tonic-gate 			}
59150Sstevel@tonic-gate 			ASSERT(pvt->nvlan == nvlan);
59160Sstevel@tonic-gate 			GLDM_UNLOCK(mac);
59170Sstevel@tonic-gate 		}
59180Sstevel@tonic-gate 		mutex_exit(&dev->gld_devlock);
59190Sstevel@tonic-gate 	}
59200Sstevel@tonic-gate 	mutex_exit(&gld_device_list.gld_devlock);
59210Sstevel@tonic-gate }
59220Sstevel@tonic-gate #endif
5923