xref: /onnv-gate/usr/src/uts/sun4u/starfire/io/idn_dlpi.c (revision 11311:639e7bc0b42f)
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
5*11311SSurya.Prakki@Sun.COM  * Common Development and Distribution License (the "License").
6*11311SSurya.Prakki@Sun.COM  * 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 /*
22*11311SSurya.Prakki@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * IDN DLPI support (based on QE implementation).
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/debug.h>
310Sstevel@tonic-gate #include <sys/stropts.h>
320Sstevel@tonic-gate #include <sys/stream.h>
330Sstevel@tonic-gate #include <sys/systm.h>
340Sstevel@tonic-gate #include <sys/cmn_err.h>
350Sstevel@tonic-gate #include <sys/errno.h>
360Sstevel@tonic-gate #ifdef xxx_trace
370Sstevel@tonic-gate #include <sys/vtrace.h>
380Sstevel@tonic-gate #endif /* xxx_trace */
390Sstevel@tonic-gate #include <sys/kmem.h>
400Sstevel@tonic-gate #include <sys/ddi.h>
410Sstevel@tonic-gate #include <sys/sunddi.h>
420Sstevel@tonic-gate #include <sys/strsun.h>
430Sstevel@tonic-gate #include <sys/stat.h>
440Sstevel@tonic-gate #include <sys/kstat.h>
450Sstevel@tonic-gate #include <sys/dlpi.h>
460Sstevel@tonic-gate #include <sys/time.h>
470Sstevel@tonic-gate #include <sys/cpuvar.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate #include <sys/idn.h>
500Sstevel@tonic-gate 
510Sstevel@tonic-gate #ifdef	IPV6
520Sstevel@tonic-gate #define	IS_ETHERTYPE_IPV4(x)	((x) == ETHERTYPE_IP)
530Sstevel@tonic-gate #define	IS_ETHERTYPE_IPV6(x)	((x) == ETHERTYPE_IPV6)
540Sstevel@tonic-gate #define	IS_ETHERTYPE_IP(x)	(IS_ETHERTYPE_IPV4(x) || IS_ETHERTYPE_IPV6(x))
550Sstevel@tonic-gate #else
560Sstevel@tonic-gate #define	IS_ETHERTYPE_IPV4(x)	((x) == ETHERTYPE_IP)
570Sstevel@tonic-gate #define	IS_ETHERTYPE_IPV6(x)	(0)
580Sstevel@tonic-gate #define	IS_ETHERTYPE_IP		IS_ETHERTYPE_IPV4
590Sstevel@tonic-gate #endif /* IPV6 */
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #ifdef IDN_TRACE
620Sstevel@tonic-gate /*
630Sstevel@tonic-gate  * This stuff should go into <sys/vtrace.h>
640Sstevel@tonic-gate  */
650Sstevel@tonic-gate #define	TR_FAC_IDN		100
660Sstevel@tonic-gate /*
670Sstevel@tonic-gate  * TR_FAC_IDN tags
680Sstevel@tonic-gate  */
690Sstevel@tonic-gate #define	TR_IDN_OPEN		0
700Sstevel@tonic-gate #define	TR_IDN_CLOSE		1
710Sstevel@tonic-gate #define	TR_IDN_WPUT_START	2
720Sstevel@tonic-gate #define	TR_IDN_WPUT_END		3
730Sstevel@tonic-gate #define	TR_IDN_WSRV_START	4
740Sstevel@tonic-gate #define	TR_IDN_WSRV_END		5
750Sstevel@tonic-gate #define	TR_IDN_START_START	6
760Sstevel@tonic-gate #define	TR_IDN_START_END	7
770Sstevel@tonic-gate #define	TR_IDN_INTR_START	8
780Sstevel@tonic-gate #define	TR_IDN_INTR_END		9
790Sstevel@tonic-gate #define	TR_IDN_READ_START	10
800Sstevel@tonic-gate #define	TR_IDN_READ_END		11
810Sstevel@tonic-gate #define	TR_IDN_SENDUP_START	12
820Sstevel@tonic-gate #define	TR_IDN_SENDUP_END	13
830Sstevel@tonic-gate #define	TR_IDN_ADDUDIND_START	14
840Sstevel@tonic-gate #define	TR_IDN_ADDUDIND_END	15
850Sstevel@tonic-gate #define	TR_IDN_GETBUF_START	16
860Sstevel@tonic-gate #define	TR_IDN_GETBUF_END	17
870Sstevel@tonic-gate #define	TR_IDN_FREEBUF_START	18
880Sstevel@tonic-gate #define	TR_IDN_FREEBUF_END	19
890Sstevel@tonic-gate #define	TR_IDN_PROTO_START	20
900Sstevel@tonic-gate #define	TR_IDN_PROTO_END	21
910Sstevel@tonic-gate #define	TR_IDN_INIT_START	22
920Sstevel@tonic-gate #define	TR_IDN_INIT_END		23
930Sstevel@tonic-gate #define	TR_IDN_PROTO_IN		24
940Sstevel@tonic-gate #define	TR_IDN_PROTO_OUT	25
950Sstevel@tonic-gate 
960Sstevel@tonic-gate #define	IDNTRACE(fac, tag)	(printf("idn.TRACE: "))
970Sstevel@tonic-gate 
980Sstevel@tonic-gate #define	TRACE_0(fac, tag, name) \
990Sstevel@tonic-gate 	IDNTRACE((fac), (tag)); \
1000Sstevel@tonic-gate 	printf(name); printf("\n");
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate #define	TRACE_1(fac, tag, name, d1) \
1030Sstevel@tonic-gate 	IDNTRACE((fac), (tag)); \
1040Sstevel@tonic-gate 	printf(name, (d1)); printf("\n");
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate #define	TRACE_2(fac, tag, name, d1, d2) \
1070Sstevel@tonic-gate 	IDNTRACE((fac), (tag)); \
1080Sstevel@tonic-gate 	printf(name, (d1), (d2)); printf("\n");
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate #define	TRACE_3(fac, tag, name, d1, d2, d3) \
1110Sstevel@tonic-gate 	IDNTRACE((fac), (tag)); \
1120Sstevel@tonic-gate 	printf(name, (d1), (d2), (d3)); printf("\n");
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate #define	TRACE_4(fac, tag, name, d1, d2, d3, d4) \
1150Sstevel@tonic-gate 	IDNTRACE((fac), (tag)); \
1160Sstevel@tonic-gate 	printf(name, (d1), (d2), (d3), (d4)); printf("\n");
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate #define	TRACE_5(fac, tag, name, d1, d2, d3, d4, d5) \
1190Sstevel@tonic-gate 	IDNTRACE((fac), (tag)); \
1200Sstevel@tonic-gate 	printf(name, (d1), (d2), (d3), (d4), (d5)); printf("\n");
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate #else /* IDN_TRACE */
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate #define	TRACE_0(fac, tag, name) 			{}
1250Sstevel@tonic-gate #define	TRACE_1(fac, tag, name, d1) 			{}
1260Sstevel@tonic-gate #define	TRACE_2(fac, tag, name, d1, d2) 		{}
1270Sstevel@tonic-gate #define	TRACE_3(fac, tag, name, d1, d2, d3) 		{}
1280Sstevel@tonic-gate #define	TRACE_4(fac, tag, name, d1, d2, d3, d4) 	{}
1290Sstevel@tonic-gate #define	TRACE_5(fac, tag, name, d1, d2, d3, d4, d5) 	{}
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate #endif /* IDN_TRACE */
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate #ifdef DEBUG
1340Sstevel@tonic-gate #define	DLERRORACK(qq, mm, cc, ee, xx) \
1350Sstevel@tonic-gate { \
1360Sstevel@tonic-gate 	PR_DLPI("dlpi: ERRORACK: 0x%x(%s), err = 0x%x(%s)\n", \
1370Sstevel@tonic-gate 		(uint_t)(cc), dlprim2str(cc), \
1380Sstevel@tonic-gate 		(uint_t)(ee), dlerr2str((int)(ee))); \
1390Sstevel@tonic-gate 	dlerrorack((qq), (mm), (cc), (ee), (xx)); \
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate #define	DLOKACK(qq, mm, cc) \
1420Sstevel@tonic-gate { \
1430Sstevel@tonic-gate 	PR_DLPI("dlpi: OKACK: 0x%x(%s)\n", (cc), dlprim2str(cc)); \
1440Sstevel@tonic-gate 	dlokack((qq), (mm), (cc)); \
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate #define	DLBINDACK(qq, mm, ss, aa, ll, xx, yy) \
1470Sstevel@tonic-gate { \
1480Sstevel@tonic-gate 	PR_DLPI("dlpi: BINDACK: eth=%x:%x:%x:%x:%x:%x, sap=0x%x, l=%d\n", \
1490Sstevel@tonic-gate 		((struct idndladdr *)(aa))->dl_phys.ether_addr_octet[0], \
1500Sstevel@tonic-gate 		((struct idndladdr *)(aa))->dl_phys.ether_addr_octet[1], \
1510Sstevel@tonic-gate 		((struct idndladdr *)(aa))->dl_phys.ether_addr_octet[2], \
1520Sstevel@tonic-gate 		((struct idndladdr *)(aa))->dl_phys.ether_addr_octet[3], \
1530Sstevel@tonic-gate 		((struct idndladdr *)(aa))->dl_phys.ether_addr_octet[4], \
1540Sstevel@tonic-gate 		((struct idndladdr *)(aa))->dl_phys.ether_addr_octet[5], \
1550Sstevel@tonic-gate 		(uint_t)(ss), (int)(ll)); \
1560Sstevel@tonic-gate 	dlbindack((qq), (mm), (ss), (aa), (ll), (xx), (yy)); \
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate #define	DLPHYSADDRACK(qq, mm, aa, ll) \
1590Sstevel@tonic-gate { \
1600Sstevel@tonic-gate 	PR_DLPI("dlpi: PHYSACK: eth=%x:%x:%x:%x:%x:%x, l=%d\n", \
1610Sstevel@tonic-gate 		((struct idndladdr *)(aa))->dl_phys.ether_addr_octet[0], \
1620Sstevel@tonic-gate 		((struct idndladdr *)(aa))->dl_phys.ether_addr_octet[1], \
1630Sstevel@tonic-gate 		((struct idndladdr *)(aa))->dl_phys.ether_addr_octet[2], \
1640Sstevel@tonic-gate 		((struct idndladdr *)(aa))->dl_phys.ether_addr_octet[3], \
1650Sstevel@tonic-gate 		((struct idndladdr *)(aa))->dl_phys.ether_addr_octet[4], \
1660Sstevel@tonic-gate 		((struct idndladdr *)(aa))->dl_phys.ether_addr_octet[5], \
1670Sstevel@tonic-gate 		(ll)); \
1680Sstevel@tonic-gate 	dlphysaddrack((qq), (mm), (aa), (ll)); \
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate static char *dlerrstr[] = {
1720Sstevel@tonic-gate 	"DL_BADSAP",
1730Sstevel@tonic-gate 	"DL_BADADDR",
1740Sstevel@tonic-gate 	"DL_ACCESS",
1750Sstevel@tonic-gate 	"DL_OUTSTATE",
1760Sstevel@tonic-gate 	"DL_SYSERR",
1770Sstevel@tonic-gate 	"DL_BADCORR",
1780Sstevel@tonic-gate 	"DL_BADDATA",
1790Sstevel@tonic-gate 	"DL_UNSUPPORTED",
1800Sstevel@tonic-gate 	"DL_BADPPA",
1810Sstevel@tonic-gate 	"DL_BADPRIM",
1820Sstevel@tonic-gate 	"DL_BADQOSPARAM",
1830Sstevel@tonic-gate 	"DL_BADQOSTYPE",
1840Sstevel@tonic-gate 	"DL_BADTOKEN",
1850Sstevel@tonic-gate 	"DL_BOUND",
1860Sstevel@tonic-gate 	"DL_INITFAILED",
1870Sstevel@tonic-gate 	"DL_NOADDR",
1880Sstevel@tonic-gate 	"DL_NOTINIT",
1890Sstevel@tonic-gate 	"DL_UNDELIVERABLE",
1900Sstevel@tonic-gate 	"DL_NOTSUPPORTED",
1910Sstevel@tonic-gate 	"DL_TOOMANY",
1920Sstevel@tonic-gate 	"DL_NOTENAB",
1930Sstevel@tonic-gate 	"DL_BUSY",
1940Sstevel@tonic-gate 	"DL_NOAUTO",
1950Sstevel@tonic-gate 	"DL_NOXIDAUTO",
1960Sstevel@tonic-gate 	"DL_NOTESTAUTO",
1970Sstevel@tonic-gate 	"DL_XIDAUTO",
1980Sstevel@tonic-gate 	"DL_TESTAUTO",
1990Sstevel@tonic-gate 	"DL_PENDING"
2000Sstevel@tonic-gate };
2010Sstevel@tonic-gate static int dlerrnum = (sizeof (dlerrstr) / sizeof (char *));
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate static char *
dlerr2str(int err)2040Sstevel@tonic-gate dlerr2str(int err)
2050Sstevel@tonic-gate {
2060Sstevel@tonic-gate 	if ((err < 0) || (err >= dlerrnum))
2070Sstevel@tonic-gate 		return ("unknown");
2080Sstevel@tonic-gate 	else
2090Sstevel@tonic-gate 		return (dlerrstr[err]);
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate static char *
dlprim2str(int prim)2130Sstevel@tonic-gate dlprim2str(int prim)
2140Sstevel@tonic-gate {
2150Sstevel@tonic-gate 	char	*pstr;
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	switch (prim) {
2180Sstevel@tonic-gate 	case DL_UNITDATA_REQ:	pstr = "UNITDATA_REQ";		break;
2190Sstevel@tonic-gate 	case DL_ATTACH_REQ:	pstr = "ATTACH_REQ";		break;
2200Sstevel@tonic-gate 	case DL_DETACH_REQ:	pstr = "DETACH_REQ";		break;
2210Sstevel@tonic-gate 	case DL_BIND_REQ:	pstr = "BIND_REQ";		break;
2220Sstevel@tonic-gate 	case DL_UNBIND_REQ:	pstr = "UNBIND_REQ";		break;
2230Sstevel@tonic-gate 	case DL_INFO_REQ:	pstr = "INFO_REQ";		break;
2240Sstevel@tonic-gate 	case DL_PROMISCON_REQ:	pstr = "PROMISCON_REQ";		break;
2250Sstevel@tonic-gate 	case DL_PROMISCOFF_REQ:	pstr = "PROMISCOFF_REQ";	break;
2260Sstevel@tonic-gate 	case DL_ENABMULTI_REQ:	pstr = "ENABMULTI_REQ";		break;
2270Sstevel@tonic-gate 	case DL_DISABMULTI_REQ:	pstr = "DISABMULTI_REQ";	break;
2280Sstevel@tonic-gate 	case DL_PHYS_ADDR_REQ:	pstr = "PHYS_ADDR_REQ";		break;
2290Sstevel@tonic-gate 	case DL_SET_PHYS_ADDR_REQ:
2300Sstevel@tonic-gate 				pstr = "SET_PHYS_ADDR_REQ";	break;
2310Sstevel@tonic-gate 	default:		pstr = "unsupported";		break;
2320Sstevel@tonic-gate 	}
2330Sstevel@tonic-gate 	return (pstr);
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate #else /* DEBUG */
2360Sstevel@tonic-gate #define	DLERRORACK(qq, mm, cc, ee, xx) \
2370Sstevel@tonic-gate 			(dlerrorack((qq), (mm), (cc), (ee), (xx)))
2380Sstevel@tonic-gate #define	DLOKACK(qq, mm, cc) \
2390Sstevel@tonic-gate 			(dlokack((qq), (mm), (cc)))
2400Sstevel@tonic-gate #define	DLBINDACK(qq, mm, ss, aa, ll, xx, yy) \
2410Sstevel@tonic-gate 			(dlbindack((qq), (mm), (ss), (aa), (ll), (xx), (yy)))
2420Sstevel@tonic-gate #define	DLPHYSADDRACK(qq, mm, aa, ll) \
2430Sstevel@tonic-gate 			(dlphysaddrack((qq), (mm), (aa), (ll)))
2440Sstevel@tonic-gate #endif /* DEBUG */
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate #define	IDNDL_ADDR_IS_MULTICAST(ap)	(((ap)->ether_addr_octet[0] & 01) == 1)
2470Sstevel@tonic-gate /*
2480Sstevel@tonic-gate  * MIB II broadcast/multicast packets
2490Sstevel@tonic-gate  */
2500Sstevel@tonic-gate #define	IS_BROADCAST(ehp) \
2510Sstevel@tonic-gate 		(ether_cmp(&(ehp)->ether_dhost, &etherbroadcastaddr) == 0)
2520Sstevel@tonic-gate #define	IS_MULTICAST(ehp) \
2530Sstevel@tonic-gate 		IDNDL_ADDR_IS_MULTICAST(&(ehp)->ether_dhost)
2540Sstevel@tonic-gate #define	BUMP_InNUcast(sip, ehp)					\
2550Sstevel@tonic-gate 		if (IS_BROADCAST(ehp)) {			\
2560Sstevel@tonic-gate 			(sip)->si_kstat.si_brdcstrcv++;		\
2570Sstevel@tonic-gate 		} else if (IS_MULTICAST(ehp)) {			\
2580Sstevel@tonic-gate 			(sip)->si_kstat.si_multircv++;		\
2590Sstevel@tonic-gate 		}
2600Sstevel@tonic-gate #define	BUMP_OutNUcast(sip, ehp)				\
2610Sstevel@tonic-gate 		if (IS_BROADCAST(ehp)) {			\
2620Sstevel@tonic-gate 			(sip)->si_kstat.si_brdcstxmt++;		\
2630Sstevel@tonic-gate 		} else if (IS_MULTICAST(ehp)) {			\
2640Sstevel@tonic-gate 			(sip)->si_kstat.si_multixmt++;		\
2650Sstevel@tonic-gate 		}
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate /*
2680Sstevel@tonic-gate  * Function prototypes.
2690Sstevel@tonic-gate  */
2700Sstevel@tonic-gate static int	idndl_ioc_hdr_info(queue_t *, mblk_t *, int *);
2710Sstevel@tonic-gate static void	idndl_areq(queue_t *, mblk_t *);
2720Sstevel@tonic-gate static void	idndl_dreq(queue_t *, mblk_t *);
2730Sstevel@tonic-gate static void	idndl_breq(queue_t *, mblk_t *);
2740Sstevel@tonic-gate static void	idndl_ubreq(queue_t *, mblk_t *);
2750Sstevel@tonic-gate static void	idndl_ireq(queue_t *, mblk_t *);
2760Sstevel@tonic-gate static void	idndl_ponreq(queue_t *, mblk_t *);
2770Sstevel@tonic-gate static void	idndl_poffreq(queue_t *, mblk_t *);
2780Sstevel@tonic-gate static void	idndl_emreq(queue_t *, mblk_t *);
2790Sstevel@tonic-gate static void	idndl_dmreq(queue_t *, mblk_t *);
2800Sstevel@tonic-gate static void	idndl_pareq(queue_t *, mblk_t *);
2810Sstevel@tonic-gate #ifdef notdef
2820Sstevel@tonic-gate static void	idndl_spareq(queue_t *, mblk_t *);
2830Sstevel@tonic-gate #endif /* notdef */
2840Sstevel@tonic-gate static void	idndl_udreq(queue_t *, mblk_t *);
2850Sstevel@tonic-gate static void	serror(dev_info_t *dip, int idnerr, char *fmt, ...);
2860Sstevel@tonic-gate static mblk_t	*idndl_addudind(struct idn *, mblk_t *, struct ether_addr *,
2870Sstevel@tonic-gate 				struct ether_addr *, int, ulong_t);
2880Sstevel@tonic-gate static void	idndl_setipq(struct idn *);
2890Sstevel@tonic-gate static int	idndl_mcmatch(struct idnstr *, struct ether_addr *);
2900Sstevel@tonic-gate static int	idndl_stat_kstat_update(kstat_t *ksp, int rw);
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate static int		_idndl_ether2domain(struct ether_addr *eap);
2930Sstevel@tonic-gate static struct idn	*_idndl_ether2sip(struct ether_addr *eap);
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate #define	IDNSAPMATCH(sap, type, flags) ((sap == type)? 1 : \
2970Sstevel@tonic-gate 	((flags & IDNSALLSAP)? 1 : \
2980Sstevel@tonic-gate 	((sap <= ETHERMTU) && sap && (type <= ETHERMTU))? 1 : 0))
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate /*
3010Sstevel@tonic-gate  * Our DL_INFO_ACK template.
3020Sstevel@tonic-gate  */
3030Sstevel@tonic-gate static	dl_info_ack_t idninfoack = {
3040Sstevel@tonic-gate 	DL_INFO_ACK,			/* dl_primitive */
3050Sstevel@tonic-gate 	0,				/* dl_max_sdu (see idndl_dlpi_init()) */
3060Sstevel@tonic-gate 	0,				/* dl_min_sdu */
3070Sstevel@tonic-gate 	IDNADDRL,			/* dl_addr_length */
3080Sstevel@tonic-gate 	DL_ETHER, /* DL_OTHER, */	/* dl_mac_type */
3090Sstevel@tonic-gate 	0,				/* dl_reserved */
3100Sstevel@tonic-gate 	0,				/* dl_current_state */
3110Sstevel@tonic-gate 	-2,				/* dl_sap_length */
3120Sstevel@tonic-gate 	DL_CLDLS, /* DL_CODLS? */	/* dl_service_mode */
3130Sstevel@tonic-gate 	0,				/* dl_qos_length */
3140Sstevel@tonic-gate 	0,				/* dl_qos_offset */
3150Sstevel@tonic-gate 	0,				/* dl_range_length */
3160Sstevel@tonic-gate 	0,				/* dl_range_offset */
3170Sstevel@tonic-gate 	DL_STYLE2,			/* dl_provider_style */
3180Sstevel@tonic-gate 	sizeof (dl_info_ack_t),		/* dl_addr_offset */
3190Sstevel@tonic-gate 	DL_VERSION_2,			/* dl_version */
3200Sstevel@tonic-gate 	ETHERADDRL,			/* dl_brdcst_addr_length */
3210Sstevel@tonic-gate 	sizeof (dl_info_ack_t) + IDNADDRL,	/* dl_brdcst_addr_offset */
3220Sstevel@tonic-gate 	0				/* dl_growth */
3230Sstevel@tonic-gate };
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate /*
3260Sstevel@tonic-gate  * Ethernet broadcast address definition.
3270Sstevel@tonic-gate  */
3280Sstevel@tonic-gate static struct ether_addr	etherbroadcastaddr = {
3290Sstevel@tonic-gate 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff
3300Sstevel@tonic-gate };
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate /*
3330Sstevel@tonic-gate  * --------------------------------------------------
3340Sstevel@tonic-gate  */
3350Sstevel@tonic-gate void
idndl_localetheraddr(struct idn * sip,struct ether_addr * eap)3360Sstevel@tonic-gate idndl_localetheraddr(struct idn *sip, struct ether_addr *eap)
3370Sstevel@tonic-gate {
3380Sstevel@tonic-gate 	int		rv;
3390Sstevel@tonic-gate 	int		instance;
3400Sstevel@tonic-gate 	procname_t	proc = "idndl_localetheraddr";
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	ASSERT(sip && sip->si_dip && eap);
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	instance = ddi_get_instance(sip->si_dip);
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	PR_DLPI("%s: getting local etheraddr...\n", proc);
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	rv = idndl_domain_etheraddr(idn.localid, instance, eap);
3490Sstevel@tonic-gate 	ASSERT(rv == 0);
3500Sstevel@tonic-gate }
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate int
idndl_domain_etheraddr(int domid,int channel,struct ether_addr * eap)3530Sstevel@tonic-gate idndl_domain_etheraddr(int domid, int channel, struct ether_addr *eap)
3540Sstevel@tonic-gate {
3550Sstevel@tonic-gate 	uchar_t		netid;
3560Sstevel@tonic-gate 	procname_t	proc = "idndl_domain_etheraddr";
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	if (idn_domain[domid].dcpu == IDN_NIL_DCPU)
3590Sstevel@tonic-gate 		return (-1);
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	netid = (uchar_t)idn_domain[domid].dnetid;
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	PR_DLPI("%s: dnetid = 0x%x, channel = 0x%x\n",
3640Sstevel@tonic-gate 		proc, (uint_t)netid, channel);
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate #ifdef notdef
3670Sstevel@tonic-gate 	localetheraddr(NULL, eap);
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	PR_DLPI("%s: localetheraddr = %x:%x:%x:%x:%x:%x\n",
3700Sstevel@tonic-gate 		proc, eap->ether_addr_octet[0], eap->ether_addr_octet[1],
3710Sstevel@tonic-gate 		eap->ether_addr_octet[2], eap->ether_addr_octet[3],
3720Sstevel@tonic-gate 		eap->ether_addr_octet[4], eap->ether_addr_octet[5]):
3730Sstevel@tonic-gate #endif /* notdef */
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	eap->ether_addr_octet[IDNETHER_ZERO] = 0;
3760Sstevel@tonic-gate 	eap->ether_addr_octet[IDNETHER_COOKIE1] = IDNETHER_COOKIE1_VAL;
3770Sstevel@tonic-gate 	eap->ether_addr_octet[IDNETHER_COOKIE2] = IDNETHER_COOKIE2_VAL;
3780Sstevel@tonic-gate 	eap->ether_addr_octet[IDNETHER_NETID] = netid;
3790Sstevel@tonic-gate 	eap->ether_addr_octet[IDNETHER_CHANNEL] = (uchar_t)channel;
3800Sstevel@tonic-gate 	eap->ether_addr_octet[IDNETHER_RESERVED] = IDNETHER_RESERVED_VAL;
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	PR_DLPI("%s: domain %d: etheraddr = %x:%x:%x:%x:%x:%x\n",
3830Sstevel@tonic-gate 		proc, domid,
3840Sstevel@tonic-gate 		eap->ether_addr_octet[0], eap->ether_addr_octet[1],
3850Sstevel@tonic-gate 		eap->ether_addr_octet[2], eap->ether_addr_octet[3],
3860Sstevel@tonic-gate 		eap->ether_addr_octet[4], eap->ether_addr_octet[5]);
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	return (0);
3890Sstevel@tonic-gate }
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate #ifdef DEBUG
3920Sstevel@tonic-gate /*
3930Sstevel@tonic-gate  */
3940Sstevel@tonic-gate static int
_idndl_ether2domain(struct ether_addr * eap)3950Sstevel@tonic-gate _idndl_ether2domain(struct ether_addr *eap)
3960Sstevel@tonic-gate {
3970Sstevel@tonic-gate 	uchar_t	*eaop;
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 	eaop = eap->ether_addr_octet;
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 	ASSERT(IDNDL_ADDR_IS_MULTICAST(eap) ||
4020Sstevel@tonic-gate 		((eaop[IDNETHER_COOKIE1] == IDNETHER_COOKIE1_VAL) &&
4030Sstevel@tonic-gate 		    (eaop[IDNETHER_COOKIE2] == IDNETHER_COOKIE2_VAL)) ||
4040Sstevel@tonic-gate 		((eaop[IDNETHER_COOKIE1] == 0xff) &&
4050Sstevel@tonic-gate 		    (eaop[IDNETHER_COOKIE2] == 0xff)));
4060Sstevel@tonic-gate 	/*
4070Sstevel@tonic-gate 	 * Note that (IDN_NIL_DOMID) will be returned if ether address is
4080Sstevel@tonic-gate 	 * a broadcast 0xff.
4090Sstevel@tonic-gate 	 */
4100Sstevel@tonic-gate 	return (IDN_NETID2DOMID(eaop[IDNETHER_NETID]));
4110Sstevel@tonic-gate }
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate /*
4140Sstevel@tonic-gate  */
4150Sstevel@tonic-gate static struct idn *
_idndl_ether2sip(struct ether_addr * eap)4160Sstevel@tonic-gate _idndl_ether2sip(struct ether_addr *eap)
4170Sstevel@tonic-gate {
4180Sstevel@tonic-gate 	int		instance;
4190Sstevel@tonic-gate 	struct idn	*sip;
4200Sstevel@tonic-gate 	uchar_t		*eaop;
4210Sstevel@tonic-gate 	procname_t	proc = "_idndl_ether2sip";
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	eaop = eap->ether_addr_octet;
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	if (!IDNDL_ADDR_IS_MULTICAST(eap) &&
4260Sstevel@tonic-gate 	    (((eaop[IDNETHER_COOKIE1] != IDNETHER_COOKIE1_VAL) ||
4270Sstevel@tonic-gate 	    (eaop[IDNETHER_COOKIE2] != IDNETHER_COOKIE2_VAL)) &&
4280Sstevel@tonic-gate 	    ((eaop[IDNETHER_COOKIE1] != 0xff) ||
4290Sstevel@tonic-gate 		(eaop[IDNETHER_COOKIE2] != 0xff)))) {
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 		cmn_err(CE_WARN,
4320Sstevel@tonic-gate 			"IDN: 400: corrupted MAC header "
4330Sstevel@tonic-gate 			"(exp %x or 0xffff, act 0x%x)",
4340Sstevel@tonic-gate 			(IDNETHER_COOKIE1_VAL << 8) |
4350Sstevel@tonic-gate 				IDNETHER_COOKIE2_VAL,
4360Sstevel@tonic-gate 			(eaop[IDNETHER_COOKIE1] << 8) |
4370Sstevel@tonic-gate 				eaop[IDNETHER_COOKIE2]);
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 		return (NULL);
4400Sstevel@tonic-gate 	}
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	if (IDNDL_ADDR_IS_MULTICAST(eap)) {
4430Sstevel@tonic-gate 		PR_DLPI("%s: MULTICAST ADDR *** ERROR ***\n", proc);
4440Sstevel@tonic-gate 		sip = NULL;
4450Sstevel@tonic-gate 	} else if (eaop[IDNETHER_CHANNEL] == 0xff) {
4460Sstevel@tonic-gate 		/*
4470Sstevel@tonic-gate 		 * Received a broadcast.  Need to manually
4480Sstevel@tonic-gate 		 * find anybody the first running sip and use it.
4490Sstevel@tonic-gate 		 * XXX - kind of kludgy - single threads broadcasts.
4500Sstevel@tonic-gate 		 */
4510Sstevel@tonic-gate 		PR_DLPI("%s: BROADCAST CHANNEL *** ERROR ***\n", proc);
4520Sstevel@tonic-gate 		sip = NULL;
4530Sstevel@tonic-gate 	} else {
4540Sstevel@tonic-gate 		instance = (int)eaop[IDNETHER_CHANNEL];
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 		sip = IDN_INST2SIP(instance);
4570Sstevel@tonic-gate 	}
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	return (sip);
4600Sstevel@tonic-gate }
4610Sstevel@tonic-gate #endif /* DEBUG */
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate void
idndl_dlpi_init()4640Sstevel@tonic-gate idndl_dlpi_init()
4650Sstevel@tonic-gate {
4660Sstevel@tonic-gate 	procname_t	proc = "idndl_dlpi_init";
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	PR_DLPI("%s: setting dl_max_sdu to %ld (0x%lx) bytes\n",
4690Sstevel@tonic-gate 		proc, IDN_MTU, IDN_MTU);
4700Sstevel@tonic-gate 	/*
4710Sstevel@tonic-gate 	 * This field is dynamic because the user may
4720Sstevel@tonic-gate 	 * want to dynamically set it _before_ an IDNnet
4730Sstevel@tonic-gate 	 * has been established via ndd(1M).
4740Sstevel@tonic-gate 	 */
4750Sstevel@tonic-gate 	idninfoack.dl_max_sdu = IDN_MTU;
4760Sstevel@tonic-gate }
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate static int
idndl_stat_kstat_update(kstat_t * ksp,int rw)4790Sstevel@tonic-gate idndl_stat_kstat_update(kstat_t *ksp, int rw)
4800Sstevel@tonic-gate {
4810Sstevel@tonic-gate 	struct idn	*sip;
4820Sstevel@tonic-gate 	struct idn_kstat_named	*skp;
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	sip = (struct idn *)ksp->ks_private;
4850Sstevel@tonic-gate 	skp = (struct idn_kstat_named *)ksp->ks_data;
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	if (rw == KSTAT_WRITE) {
4880Sstevel@tonic-gate #if 0
4890Sstevel@tonic-gate 		bzero(&sg_kstat.gk_kstat, sizeof (sg_kstat.gk_kstat));
4900Sstevel@tonic-gate #endif /* 0 */
4910Sstevel@tonic-gate 		bzero(&sip->si_kstat, sizeof (sip->si_kstat));
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 		sip->si_kstat.si_ipackets 	= skp->sk_ipackets.value.ul;
4940Sstevel@tonic-gate 		sip->si_kstat.si_ierrors	= skp->sk_ierrors.value.ul;
4950Sstevel@tonic-gate 		sip->si_kstat.si_opackets 	= skp->sk_opackets.value.ul;
4960Sstevel@tonic-gate 		sip->si_kstat.si_oerrors	= skp->sk_oerrors.value.ul;
4970Sstevel@tonic-gate 		sip->si_kstat.si_txcoll		= skp->sk_txcoll.value.ul;
4980Sstevel@tonic-gate 		sip->si_kstat.si_rxcoll		= skp->sk_rxcoll.value.ul;
4990Sstevel@tonic-gate 		sip->si_kstat.si_crc		= skp->sk_crc.value.ul;
5000Sstevel@tonic-gate 		sip->si_kstat.si_buff		= skp->sk_buff.value.ul;
5010Sstevel@tonic-gate 		sip->si_kstat.si_nolink		= skp->sk_nolink.value.ul;
5020Sstevel@tonic-gate 		sip->si_kstat.si_linkdown	= skp->sk_linkdown.value.ul;
5030Sstevel@tonic-gate 		sip->si_kstat.si_inits		= skp->sk_inits.value.ul;
5040Sstevel@tonic-gate 		sip->si_kstat.si_nocanput	= skp->sk_nocanput.value.ul;
5050Sstevel@tonic-gate 		sip->si_kstat.si_allocbfail	= skp->sk_allocbfail.value.ul;
5060Sstevel@tonic-gate 		sip->si_kstat.si_notbufs	= skp->sk_notbufs.value.ul;
5070Sstevel@tonic-gate 		sip->si_kstat.si_reclaim	= skp->sk_reclaim.value.ul;
5080Sstevel@tonic-gate 		sip->si_kstat.si_smraddr	= skp->sk_smraddr.value.ul;
5090Sstevel@tonic-gate 		sip->si_kstat.si_txmax		= skp->sk_txmax.value.ul;
5100Sstevel@tonic-gate 		sip->si_kstat.si_txfull		= skp->sk_txfull.value.ul;
5110Sstevel@tonic-gate 		sip->si_kstat.si_xdcall		= skp->sk_xdcall.value.ul;
5120Sstevel@tonic-gate 		sip->si_kstat.si_sigsvr		= skp->sk_sigsvr.value.ul;
5130Sstevel@tonic-gate 		sip->si_kstat.si_mboxcrc	= skp->sk_mboxcrc.value.ul;
5140Sstevel@tonic-gate 		/*
5150Sstevel@tonic-gate 		 * MIB II kstat variables
5160Sstevel@tonic-gate 		 */
5170Sstevel@tonic-gate 		sip->si_kstat.si_rcvbytes	= skp->sk_rcvbytes.value.ul;
5180Sstevel@tonic-gate 		sip->si_kstat.si_xmtbytes	= skp->sk_xmtbytes.value.ul;
5190Sstevel@tonic-gate 		sip->si_kstat.si_multircv	= skp->sk_multircv.value.ul;
5200Sstevel@tonic-gate 		sip->si_kstat.si_multixmt	= skp->sk_multixmt.value.ul;
5210Sstevel@tonic-gate 		sip->si_kstat.si_brdcstrcv	= skp->sk_brdcstrcv.value.ul;
5220Sstevel@tonic-gate 		sip->si_kstat.si_brdcstxmt	= skp->sk_brdcstxmt.value.ul;
5230Sstevel@tonic-gate 		sip->si_kstat.si_norcvbuf	= skp->sk_norcvbuf.value.ul;
5240Sstevel@tonic-gate 		sip->si_kstat.si_noxmtbuf	= skp->sk_noxmtbuf.value.ul;
5250Sstevel@tonic-gate 		/*
5260Sstevel@tonic-gate 		 * PSARC 1997/198 : 64bit kstats
5270Sstevel@tonic-gate 		 */
5280Sstevel@tonic-gate 		sip->si_kstat.si_ipackets64	= skp->sk_ipackets64.value.ull;
5290Sstevel@tonic-gate 		sip->si_kstat.si_opackets64	= skp->sk_opackets64.value.ull;
5300Sstevel@tonic-gate 		sip->si_kstat.si_rbytes64	= skp->sk_rbytes64.value.ull;
5310Sstevel@tonic-gate 		sip->si_kstat.si_obytes64	= skp->sk_obytes64.value.ull;
5320Sstevel@tonic-gate 		/*
5330Sstevel@tonic-gate 		 * PSARC 1997/247 : RFC 1643
5340Sstevel@tonic-gate 		 */
5350Sstevel@tonic-gate 		sip->si_kstat.si_fcs_errors	= skp->sk_fcs_errors.value.ul;
5360Sstevel@tonic-gate 		sip->si_kstat.si_macxmt_errors	=
5370Sstevel@tonic-gate 						skp->sk_macxmt_errors.value.ul;
5380Sstevel@tonic-gate 		sip->si_kstat.si_toolong_errors	=
5390Sstevel@tonic-gate 						skp->sk_toolong_errors.value.ul;
5400Sstevel@tonic-gate 		sip->si_kstat.si_macrcv_errors	=
5410Sstevel@tonic-gate 						skp->sk_macrcv_errors.value.ul;
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 		return (0);
5440Sstevel@tonic-gate 	}
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 	skp->sk_ipackets.value.ul 	= sip->si_kstat.si_ipackets;
5470Sstevel@tonic-gate 	skp->sk_ierrors.value.ul	= sip->si_kstat.si_ierrors;
5480Sstevel@tonic-gate 	skp->sk_opackets.value.ul	= sip->si_kstat.si_opackets;
5490Sstevel@tonic-gate 	skp->sk_oerrors.value.ul	= sip->si_kstat.si_oerrors;
5500Sstevel@tonic-gate 	skp->sk_txcoll.value.ul		= sip->si_kstat.si_txcoll;
5510Sstevel@tonic-gate 	skp->sk_rxcoll.value.ul		= sip->si_kstat.si_rxcoll;
5520Sstevel@tonic-gate 	skp->sk_crc.value.ul		= sip->si_kstat.si_crc;
5530Sstevel@tonic-gate 	skp->sk_buff.value.ul		= sip->si_kstat.si_buff;
5540Sstevel@tonic-gate 	skp->sk_nolink.value.ul		= sip->si_kstat.si_nolink;
5550Sstevel@tonic-gate 	skp->sk_linkdown.value.ul	= sip->si_kstat.si_linkdown;
5560Sstevel@tonic-gate 	skp->sk_inits.value.ul		= sip->si_kstat.si_inits;
5570Sstevel@tonic-gate 	skp->sk_nocanput.value.ul	= sip->si_kstat.si_nocanput;
5580Sstevel@tonic-gate 	skp->sk_allocbfail.value.ul	= sip->si_kstat.si_allocbfail;
5590Sstevel@tonic-gate 	skp->sk_notbufs.value.ul	= sip->si_kstat.si_notbufs;
5600Sstevel@tonic-gate 	skp->sk_reclaim.value.ul	= sip->si_kstat.si_reclaim;
5610Sstevel@tonic-gate 	skp->sk_smraddr.value.ul	= sip->si_kstat.si_smraddr;
5620Sstevel@tonic-gate 	skp->sk_txfull.value.ul		= sip->si_kstat.si_txfull;
5630Sstevel@tonic-gate 	skp->sk_txmax.value.ul		= sip->si_kstat.si_txmax;
5640Sstevel@tonic-gate 	skp->sk_xdcall.value.ul		= sip->si_kstat.si_xdcall;
5650Sstevel@tonic-gate 	skp->sk_sigsvr.value.ul		= sip->si_kstat.si_sigsvr;
5660Sstevel@tonic-gate 	skp->sk_mboxcrc.value.ul	= sip->si_kstat.si_mboxcrc;
5670Sstevel@tonic-gate 	/*
5680Sstevel@tonic-gate 	 * MIB II kstat variables
5690Sstevel@tonic-gate 	 */
5700Sstevel@tonic-gate 	skp->sk_rcvbytes.value.ul	= sip->si_kstat.si_rcvbytes;
5710Sstevel@tonic-gate 	skp->sk_xmtbytes.value.ul	= sip->si_kstat.si_xmtbytes;
5720Sstevel@tonic-gate 	skp->sk_multircv.value.ul	= sip->si_kstat.si_multircv;
5730Sstevel@tonic-gate 	skp->sk_multixmt.value.ul	= sip->si_kstat.si_multixmt;
5740Sstevel@tonic-gate 	skp->sk_brdcstrcv.value.ul	= sip->si_kstat.si_brdcstrcv;
5750Sstevel@tonic-gate 	skp->sk_brdcstxmt.value.ul	= sip->si_kstat.si_brdcstxmt;
5760Sstevel@tonic-gate 	skp->sk_norcvbuf.value.ul	= sip->si_kstat.si_norcvbuf;
5770Sstevel@tonic-gate 	skp->sk_noxmtbuf.value.ul	= sip->si_kstat.si_noxmtbuf;
5780Sstevel@tonic-gate 	/*
5790Sstevel@tonic-gate 	 * PSARC 1997/198 : 64bit kstats
5800Sstevel@tonic-gate 	 */
5810Sstevel@tonic-gate 	skp->sk_ipackets64.value.ull	= sip->si_kstat.si_ipackets64;
5820Sstevel@tonic-gate 	skp->sk_opackets64.value.ull	= sip->si_kstat.si_opackets64;
5830Sstevel@tonic-gate 	skp->sk_rbytes64.value.ull	= sip->si_kstat.si_rbytes64;
5840Sstevel@tonic-gate 	skp->sk_obytes64.value.ull	= sip->si_kstat.si_obytes64;
5850Sstevel@tonic-gate 	/*
5860Sstevel@tonic-gate 	 * PSARC 1997/247 : RFC 1643
5870Sstevel@tonic-gate 	 */
5880Sstevel@tonic-gate 	skp->sk_fcs_errors.value.ul	= sip->si_kstat.si_fcs_errors;
5890Sstevel@tonic-gate 	skp->sk_macxmt_errors.value.ul	= sip->si_kstat.si_macxmt_errors;
5900Sstevel@tonic-gate 	skp->sk_toolong_errors.value.ul	= sip->si_kstat.si_toolong_errors;
5910Sstevel@tonic-gate 	skp->sk_macrcv_errors.value.ul	= sip->si_kstat.si_macrcv_errors;
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 	return (0);
5940Sstevel@tonic-gate }
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate void
idndl_statinit(struct idn * sip)5970Sstevel@tonic-gate idndl_statinit(struct idn *sip)
5980Sstevel@tonic-gate {
5990Sstevel@tonic-gate 	struct	kstat		*ksp;
6000Sstevel@tonic-gate 	struct	idn_kstat_named	*skp;
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate #ifdef	kstat
6030Sstevel@tonic-gate 	if ((ksp = kstat_create(IDNNAME, ddi_get_instance(sip->si_dip),
6040Sstevel@tonic-gate 		NULL, "net", KSTAT_TYPE_NAMED,
6050Sstevel@tonic-gate 		sizeof (struct idn_kstat_named) / sizeof (kstat_named_t),
6060Sstevel@tonic-gate 		KSTAT_FLAG_PERSISTENT)) == NULL) {
6070Sstevel@tonic-gate #else
6080Sstevel@tonic-gate 	if ((ksp = kstat_create(IDNNAME, ddi_get_instance(sip->si_dip),
6090Sstevel@tonic-gate 		NULL, "net", KSTAT_TYPE_NAMED,
6100Sstevel@tonic-gate 		sizeof (struct idn_kstat_named) /
6110Sstevel@tonic-gate 		sizeof (kstat_named_t), 0)) == NULL) {
6120Sstevel@tonic-gate #endif	/* kstat */
6130Sstevel@tonic-gate 		serror(sip->si_dip, 450, "kstat_create failed");
6140Sstevel@tonic-gate 		return;
6150Sstevel@tonic-gate 	}
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 	sip->si_ksp = ksp;
6180Sstevel@tonic-gate 	skp = (struct idn_kstat_named *)(ksp->ks_data);
6190Sstevel@tonic-gate 	kstat_named_init(&skp->sk_ipackets,		"ipackets",
6200Sstevel@tonic-gate 		KSTAT_DATA_ULONG);
6210Sstevel@tonic-gate 	kstat_named_init(&skp->sk_ierrors,		"ierrors",
6220Sstevel@tonic-gate 		KSTAT_DATA_ULONG);
6230Sstevel@tonic-gate 	kstat_named_init(&skp->sk_opackets,		"opackets",
6240Sstevel@tonic-gate 		KSTAT_DATA_ULONG);
6250Sstevel@tonic-gate 	kstat_named_init(&skp->sk_oerrors,		"oerrors",
6260Sstevel@tonic-gate 		KSTAT_DATA_ULONG);
6270Sstevel@tonic-gate 	kstat_named_init(&skp->sk_txcoll,		"collisions",
6280Sstevel@tonic-gate 		KSTAT_DATA_ULONG);
6290Sstevel@tonic-gate 	kstat_named_init(&skp->sk_rxcoll,		"rx_collisions",
6300Sstevel@tonic-gate 		KSTAT_DATA_ULONG);
6310Sstevel@tonic-gate 	kstat_named_init(&skp->sk_crc,			"crc",
6320Sstevel@tonic-gate 		KSTAT_DATA_ULONG);
6330Sstevel@tonic-gate 	kstat_named_init(&skp->sk_buff,			"buff",
6340Sstevel@tonic-gate 		KSTAT_DATA_ULONG);
6350Sstevel@tonic-gate 	kstat_named_init(&skp->sk_nolink,		"nolink",
6360Sstevel@tonic-gate 		KSTAT_DATA_ULONG);
6370Sstevel@tonic-gate 	kstat_named_init(&skp->sk_linkdown,		"linkdown",
6380Sstevel@tonic-gate 		KSTAT_DATA_ULONG);
6390Sstevel@tonic-gate 	kstat_named_init(&skp->sk_inits,		"inits",
6400Sstevel@tonic-gate 		KSTAT_DATA_ULONG);
6410Sstevel@tonic-gate 	kstat_named_init(&skp->sk_nocanput,		"nocanput",
6420Sstevel@tonic-gate 		KSTAT_DATA_ULONG);
6430Sstevel@tonic-gate 	kstat_named_init(&skp->sk_allocbfail,		"allocbfail",
6440Sstevel@tonic-gate 		KSTAT_DATA_ULONG);
6450Sstevel@tonic-gate 	kstat_named_init(&skp->sk_notbufs,		"notbufs",
6460Sstevel@tonic-gate 		KSTAT_DATA_ULONG);
6470Sstevel@tonic-gate 	kstat_named_init(&skp->sk_reclaim,		"reclaim",
6480Sstevel@tonic-gate 		KSTAT_DATA_ULONG);
6490Sstevel@tonic-gate 	kstat_named_init(&skp->sk_smraddr,		"smraddr",
6500Sstevel@tonic-gate 		KSTAT_DATA_ULONG);
6510Sstevel@tonic-gate 	kstat_named_init(&skp->sk_txmax,		"txmax",
6520Sstevel@tonic-gate 		KSTAT_DATA_ULONG);
6530Sstevel@tonic-gate 	kstat_named_init(&skp->sk_txfull,		"txfull",
6540Sstevel@tonic-gate 		KSTAT_DATA_ULONG);
6550Sstevel@tonic-gate 	kstat_named_init(&skp->sk_xdcall,		"xdcall",
6560Sstevel@tonic-gate 		KSTAT_DATA_ULONG);
6570Sstevel@tonic-gate 	kstat_named_init(&skp->sk_sigsvr,		"sigsvr",
6580Sstevel@tonic-gate 		KSTAT_DATA_ULONG);
6590Sstevel@tonic-gate 	kstat_named_init(&skp->sk_mboxcrc,		"mboxcrc",
6600Sstevel@tonic-gate 		KSTAT_DATA_ULONG);
6610Sstevel@tonic-gate 	/*
6620Sstevel@tonic-gate 	 * MIB II kstat variables
6630Sstevel@tonic-gate 	 */
6640Sstevel@tonic-gate 	kstat_named_init(&skp->sk_rcvbytes,		"rbytes",
6650Sstevel@tonic-gate 		KSTAT_DATA_ULONG);
6660Sstevel@tonic-gate 	kstat_named_init(&skp->sk_xmtbytes,		"obytes",
6670Sstevel@tonic-gate 		KSTAT_DATA_ULONG);
6680Sstevel@tonic-gate 	kstat_named_init(&skp->sk_multircv,		"multircv",
6690Sstevel@tonic-gate 		KSTAT_DATA_ULONG);
6700Sstevel@tonic-gate 	kstat_named_init(&skp->sk_multixmt,		"multixmt",
6710Sstevel@tonic-gate 		KSTAT_DATA_ULONG);
6720Sstevel@tonic-gate 	kstat_named_init(&skp->sk_brdcstrcv,		"brdcstrcv",
6730Sstevel@tonic-gate 		KSTAT_DATA_ULONG);
6740Sstevel@tonic-gate 	kstat_named_init(&skp->sk_brdcstxmt,		"brdcstxmt",
6750Sstevel@tonic-gate 		KSTAT_DATA_ULONG);
6760Sstevel@tonic-gate 	kstat_named_init(&skp->sk_norcvbuf,		"norcvbuf",
6770Sstevel@tonic-gate 		KSTAT_DATA_ULONG);
6780Sstevel@tonic-gate 	kstat_named_init(&skp->sk_noxmtbuf,		"noxmtbuf",
6790Sstevel@tonic-gate 		KSTAT_DATA_ULONG);
6800Sstevel@tonic-gate 	/*
6810Sstevel@tonic-gate 	 * PSARC 1997/198 : 64bit kstats
6820Sstevel@tonic-gate 	 */
6830Sstevel@tonic-gate 	kstat_named_init(&skp->sk_ipackets64,		"ipackets64",
6840Sstevel@tonic-gate 		KSTAT_DATA_ULONGLONG);
6850Sstevel@tonic-gate 	kstat_named_init(&skp->sk_opackets64,		"opackets64",
6860Sstevel@tonic-gate 		KSTAT_DATA_ULONGLONG);
6870Sstevel@tonic-gate 	kstat_named_init(&skp->sk_rbytes64,		"rbytes64",
6880Sstevel@tonic-gate 		KSTAT_DATA_ULONGLONG);
6890Sstevel@tonic-gate 	kstat_named_init(&skp->sk_obytes64,		"obytes64",
6900Sstevel@tonic-gate 		KSTAT_DATA_ULONGLONG);
6910Sstevel@tonic-gate 	/*
6920Sstevel@tonic-gate 	 * PSARC 1997/247 : RFC 1643
6930Sstevel@tonic-gate 	 */
6940Sstevel@tonic-gate 	kstat_named_init(&skp->sk_fcs_errors,		"fcs_errors",
6950Sstevel@tonic-gate 		KSTAT_DATA_ULONG);
6960Sstevel@tonic-gate 	kstat_named_init(&skp->sk_macxmt_errors,	"macxmt_errors",
6970Sstevel@tonic-gate 		KSTAT_DATA_ULONG);
6980Sstevel@tonic-gate 	kstat_named_init(&skp->sk_toolong_errors,	"toolong_errors",
6990Sstevel@tonic-gate 		KSTAT_DATA_ULONG);
7000Sstevel@tonic-gate 	kstat_named_init(&skp->sk_macrcv_errors,	"macrcv_errors",
7010Sstevel@tonic-gate 		KSTAT_DATA_ULONG);
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	ksp->ks_update = idndl_stat_kstat_update;
7040Sstevel@tonic-gate 	ksp->ks_private = (void *)sip;
7050Sstevel@tonic-gate 	kstat_install(ksp);
7060Sstevel@tonic-gate }
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate void
7090Sstevel@tonic-gate idndl_proto(queue_t *wq, mblk_t *mp)
7100Sstevel@tonic-gate {
7110Sstevel@tonic-gate 	union DL_primitives	*dlp;
7120Sstevel@tonic-gate 	struct idnstr		*stp;
7130Sstevel@tonic-gate 	t_uscalar_t		prim;
7140Sstevel@tonic-gate 	procname_t		proc = "idndl_proto";
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 	stp = (struct idnstr *)wq->q_ptr;
7170Sstevel@tonic-gate 	if (MBLKL(mp) < sizeof (t_uscalar_t)) {
7180Sstevel@tonic-gate 		/*
7190Sstevel@tonic-gate 		 * Gotta at least have enough room to hold
7200Sstevel@tonic-gate 		 * the primitive!
7210Sstevel@tonic-gate 		 */
7220Sstevel@tonic-gate 		DLERRORACK(wq, mp, -1, DL_BADPRIM, 0);
7230Sstevel@tonic-gate 		return;
7240Sstevel@tonic-gate 	}
7250Sstevel@tonic-gate 	dlp = (union DL_primitives *)mp->b_rptr;
7260Sstevel@tonic-gate 	prim = dlp->dl_primitive;
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 	TRACE_2(TR_FAC_IDN, TR_IDN_PROTO_START,
7290Sstevel@tonic-gate 		"idndl_proto start:  wq %p dlprim %X", wq, prim);
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate #ifdef DEBUG
732931Smathue 	PR_DLPI("%s: stp = 0x%p, wq = 0x%p, dlprim = 0x%x(%s)\n",
733*11311SSurya.Prakki@Sun.COM 		proc, (void *)stp, (void *)wq, prim, dlprim2str(prim));
7340Sstevel@tonic-gate #endif /* DEBUG */
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	rw_enter(&stp->ss_rwlock, RW_WRITER);
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 	switch (prim) {
7390Sstevel@tonic-gate 	case DL_UNITDATA_REQ:
7400Sstevel@tonic-gate 		idndl_udreq(wq, mp);
7410Sstevel@tonic-gate 		break;
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 	case DL_ATTACH_REQ:
7440Sstevel@tonic-gate 		idndl_areq(wq, mp);
7450Sstevel@tonic-gate 		break;
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 	case DL_DETACH_REQ:
7480Sstevel@tonic-gate 		idndl_dreq(wq, mp);
7490Sstevel@tonic-gate 		break;
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	case DL_BIND_REQ:
7520Sstevel@tonic-gate 		idndl_breq(wq, mp);
7530Sstevel@tonic-gate 		break;
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	case DL_UNBIND_REQ:
7560Sstevel@tonic-gate 		idndl_ubreq(wq, mp);
7570Sstevel@tonic-gate 		break;
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	case DL_INFO_REQ:
7600Sstevel@tonic-gate 		idndl_ireq(wq, mp);
7610Sstevel@tonic-gate 		break;
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	case DL_PROMISCON_REQ:
7640Sstevel@tonic-gate 		idndl_ponreq(wq, mp);
7650Sstevel@tonic-gate 		break;
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 	case DL_PROMISCOFF_REQ:
7680Sstevel@tonic-gate 		idndl_poffreq(wq, mp);
7690Sstevel@tonic-gate 		break;
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 	case DL_ENABMULTI_REQ:
7720Sstevel@tonic-gate 		idndl_emreq(wq, mp);
7730Sstevel@tonic-gate 		break;
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	case DL_DISABMULTI_REQ:
7760Sstevel@tonic-gate 		idndl_dmreq(wq, mp);
7770Sstevel@tonic-gate 		break;
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 	case DL_PHYS_ADDR_REQ:
7800Sstevel@tonic-gate 		idndl_pareq(wq, mp);
7810Sstevel@tonic-gate 		break;
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate #ifdef notdef
7840Sstevel@tonic-gate 	/*
7850Sstevel@tonic-gate 	 * We cannot allow this in IDN-land because we
7860Sstevel@tonic-gate 	 * rely on the ethernet (physical) address to determine
7870Sstevel@tonic-gate 	 * where to target the message.  Recall that unlike
7880Sstevel@tonic-gate 	 * ethernet we simply cannot dump junk on the wire and
7890Sstevel@tonic-gate 	 * expect it to automatically find its destination.
7900Sstevel@tonic-gate 	 * In the IDN we need to target the destination.
7910Sstevel@tonic-gate 	 * Note that if we used POINT-TO-POINT then we wouldn't
7920Sstevel@tonic-gate 	 * have to worry about the physical address since each
7930Sstevel@tonic-gate 	 * domain connection would have a separate queue.
7940Sstevel@tonic-gate 	 * However, ptp then requires multiple interfaces at
7950Sstevel@tonic-gate 	 * the appl level as opposed to a single one for all
7960Sstevel@tonic-gate 	 * of idn.  We opt for the simpler single interface (idn0).
7970Sstevel@tonic-gate 	 */
7980Sstevel@tonic-gate 	case DL_SET_PHYS_ADDR_REQ:
7990Sstevel@tonic-gate 		idndl_spareq(wq, mp);
8000Sstevel@tonic-gate 		break;
8010Sstevel@tonic-gate #endif /* notdef */
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	default:
8040Sstevel@tonic-gate 		DLERRORACK(wq, mp, prim, DL_UNSUPPORTED, 0);
8050Sstevel@tonic-gate 		break;
8060Sstevel@tonic-gate 	}
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 	TRACE_2(TR_FAC_IDN, TR_IDN_PROTO_END,
8090Sstevel@tonic-gate 		"idnproto end:  wq %p dlprim %X", wq, prim);
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 	rw_exit(&stp->ss_rwlock);
8120Sstevel@tonic-gate }
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate int
8150Sstevel@tonic-gate idnioc_dlpi(queue_t *wq, mblk_t *mp, int *argsizep)
8160Sstevel@tonic-gate {
8170Sstevel@tonic-gate 	int	rv = 0;
8180Sstevel@tonic-gate 	struct	iocblk	*iocp = (struct iocblk *)mp->b_rptr;
8190Sstevel@tonic-gate 	struct	idnstr	*stp  = (struct idnstr *)wq->q_ptr;
8200Sstevel@tonic-gate 	procname_t	proc = "idnioc_dlpi";
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	*argsizep = 0;
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
8250Sstevel@tonic-gate 	case DLIOCRAW:			/* raw M_DATA mode */
8260Sstevel@tonic-gate 		PR_DLPI("%s: cmd = DLIOCRAW\n", proc);
8270Sstevel@tonic-gate 		stp->ss_flags |= IDNSRAW;
8280Sstevel@tonic-gate 		break;
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 	case DL_IOC_HDR_INFO:		/* M_DATA "fastpath" info request */
8310Sstevel@tonic-gate 		PR_DLPI("%s: cmd = DL_IOC_HDR_INFO\n", proc);
8320Sstevel@tonic-gate 		rv = idndl_ioc_hdr_info(wq, mp, argsizep);
8330Sstevel@tonic-gate 		break;
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 	default:
8360Sstevel@tonic-gate 		PR_DLPI("%s: invalid cmd 0x%x\n", proc, iocp->ioc_cmd);
8370Sstevel@tonic-gate 		rv = EINVAL;
8380Sstevel@tonic-gate 		break;
8390Sstevel@tonic-gate 	}
8400Sstevel@tonic-gate 	return (rv);
8410Sstevel@tonic-gate }
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate /*
8440Sstevel@tonic-gate  * M_DATA "fastpath" info request.
8450Sstevel@tonic-gate  * Following the M_IOCTL mblk should come a DL_UNITDATA_REQ mblk.
8460Sstevel@tonic-gate  * We ack with an M_IOCACK pointing to the original DL_UNITDATA_REQ mblk
8470Sstevel@tonic-gate  * followed by an mblk containing the raw ethernet header corresponding
8480Sstevel@tonic-gate  * to the destination address.  Subsequently, we may receive M_DATA
8490Sstevel@tonic-gate  * msgs which start with this header and may send up
8500Sstevel@tonic-gate  * up M_DATA msgs with b_rptr pointing to a (ulong) group address
8510Sstevel@tonic-gate  * indicator followed by the network-layer data (IP packet header).
8520Sstevel@tonic-gate  * This is all selectable on a per-Stream basis.
8530Sstevel@tonic-gate  */
8540Sstevel@tonic-gate static int
8550Sstevel@tonic-gate idndl_ioc_hdr_info(queue_t *wq, mblk_t *mp, int *argsizep)
8560Sstevel@tonic-gate {
8570Sstevel@tonic-gate 	mblk_t			*nmp;
8580Sstevel@tonic-gate 	struct idnstr		*stp;
8590Sstevel@tonic-gate 	struct idndladdr	*dlap;
8600Sstevel@tonic-gate 	dl_unitdata_req_t	*dludp;
8610Sstevel@tonic-gate 	struct ether_header	*headerp;
8620Sstevel@tonic-gate 	struct idn		*sip;
8630Sstevel@tonic-gate 	int	off, len;
8640Sstevel@tonic-gate 	int	padding = 0;
8650Sstevel@tonic-gate 	int	error;
8660Sstevel@tonic-gate 	procname_t		proc = "idndl_ioc_hdr_info";
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate 	stp = (struct idnstr *)wq->q_ptr;
8690Sstevel@tonic-gate 	sip = stp->ss_sip;
8700Sstevel@tonic-gate 	if (sip == NULL) {
8710Sstevel@tonic-gate 		PR_DLPI("%s: NULL sip (ret EINVAL)\n", proc);
8720Sstevel@tonic-gate 		return (EINVAL);
8730Sstevel@tonic-gate 	}
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 	error = miocpullup(mp, sizeof (dl_unitdata_req_t) + IDNADDRL);
8760Sstevel@tonic-gate 	if (error != 0) {
8770Sstevel@tonic-gate 		PR_DLPI("%s: sanity error (ret %d)\n", proc, error);
8780Sstevel@tonic-gate 		return (error);
8790Sstevel@tonic-gate 	}
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 	/*
8820Sstevel@tonic-gate 	 * Sanity check the DL_UNITDATA_REQ destination address
8830Sstevel@tonic-gate 	 * offset and length values.
8840Sstevel@tonic-gate 	 */
8850Sstevel@tonic-gate 	dludp = (dl_unitdata_req_t *)mp->b_cont->b_rptr;
8860Sstevel@tonic-gate 	off = dludp->dl_dest_addr_offset;
8870Sstevel@tonic-gate 	len = dludp->dl_dest_addr_length;
8880Sstevel@tonic-gate 	if (dludp->dl_primitive != DL_UNITDATA_REQ ||
8890Sstevel@tonic-gate 	    !MBLKIN(mp->b_cont, off, len) || len != IDNADDRL) {
8900Sstevel@tonic-gate 		PR_DLPI("%s: off(0x%x)/len(%d) error (ret EINVAL)\n",
8910Sstevel@tonic-gate 		    proc, off, len);
8920Sstevel@tonic-gate 		return (EINVAL);
8930Sstevel@tonic-gate 	}
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate 	dlap = (struct idndladdr *)(mp->b_cont->b_rptr + off);
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 	/*
8980Sstevel@tonic-gate 	 * Allocate a new mblk to hold the ether header.
8990Sstevel@tonic-gate 	 */
9000Sstevel@tonic-gate 	nmp = allocb(sizeof (struct ether_header) + padding, BPRI_MED);
9010Sstevel@tonic-gate 	if (nmp == NULL) {
9020Sstevel@tonic-gate 		IDN_KSTAT_INC(sip, si_allocbfail);
9030Sstevel@tonic-gate 		return (ENOMEM);
9040Sstevel@tonic-gate 	}
9050Sstevel@tonic-gate 	nmp->b_rptr += padding;
9060Sstevel@tonic-gate 	nmp->b_wptr = nmp->b_rptr + sizeof (struct ether_header);
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 	/*
9090Sstevel@tonic-gate 	 * Fill in the ether header.
9100Sstevel@tonic-gate 	 */
9110Sstevel@tonic-gate 	headerp = (struct ether_header *)nmp->b_rptr;
9120Sstevel@tonic-gate 	ether_copy(&dlap->dl_phys, &headerp->ether_dhost);
9130Sstevel@tonic-gate 	ether_copy(&sip->si_ouraddr, &headerp->ether_shost);
9140Sstevel@tonic-gate 	headerp->ether_type = dlap->dl_sap;
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 	/*
9170Sstevel@tonic-gate 	 * Link new mblk in after the "request" mblks.
9180Sstevel@tonic-gate 	 */
9190Sstevel@tonic-gate 	linkb(mp, nmp);
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 	stp->ss_flags |= IDNSFAST;
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate 	/*
9240Sstevel@tonic-gate 	 * XXX Don't bother calling idndl_setipq() here.
9250Sstevel@tonic-gate 	 */
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate 	if (argsizep)
9280Sstevel@tonic-gate 		*argsizep = msgsize(mp->b_cont);
9290Sstevel@tonic-gate 
9300Sstevel@tonic-gate 	return (0);
9310Sstevel@tonic-gate }
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate static void
9340Sstevel@tonic-gate idndl_areq(queue_t *wq, mblk_t *mp)
9350Sstevel@tonic-gate {
9360Sstevel@tonic-gate 	struct idnstr		*stp;
9370Sstevel@tonic-gate 	union DL_primitives	*dlp;
9380Sstevel@tonic-gate 	struct idn		*sip;
9390Sstevel@tonic-gate 	int	ppa;
9400Sstevel@tonic-gate 	procname_t	proc = "idndl_areq";
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate 	stp = (struct idnstr *)wq->q_ptr;
9430Sstevel@tonic-gate 	dlp = (union DL_primitives *)mp->b_rptr;
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 	if (MBLKL(mp) < DL_ATTACH_REQ_SIZE) {
9460Sstevel@tonic-gate 		DLERRORACK(wq, mp, DL_ATTACH_REQ, DL_BADPRIM, 0);
9470Sstevel@tonic-gate 		return;
9480Sstevel@tonic-gate 	}
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate 	if (stp->ss_state != DL_UNATTACHED) {
9510Sstevel@tonic-gate 		DLERRORACK(wq, mp, DL_ATTACH_REQ, DL_OUTSTATE, 0);
9520Sstevel@tonic-gate 		return;
9530Sstevel@tonic-gate 	}
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 	ppa = dlp->attach_req.dl_ppa;
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate 	/*
9580Sstevel@tonic-gate 	 * Valid ppa?
9590Sstevel@tonic-gate 	 */
9600Sstevel@tonic-gate 	if (ppa == -1 || qassociate(wq, ppa) != 0) {
9610Sstevel@tonic-gate 		PR_DLPI("%s: bad ppa %d\n", proc, ppa);
9620Sstevel@tonic-gate 		DLERRORACK(wq, mp, dlp->dl_primitive, DL_BADPPA, 0);
9630Sstevel@tonic-gate 		return;
9640Sstevel@tonic-gate 	}
9650Sstevel@tonic-gate 	mutex_enter(&idn.siplock);
9660Sstevel@tonic-gate 	for (sip = idn.sip; sip; sip = sip->si_nextp) {
9670Sstevel@tonic-gate 		if (ppa == ddi_get_instance(sip->si_dip))
9680Sstevel@tonic-gate 			break;
9690Sstevel@tonic-gate 	}
9700Sstevel@tonic-gate 	mutex_exit(&idn.siplock);
9710Sstevel@tonic-gate 	ASSERT(sip != NULL);	/* qassociate() succeeded */
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate 	/*
9740Sstevel@tonic-gate 	 * Has device been initialized?  Do so if necessary.
9750Sstevel@tonic-gate 	 */
9760Sstevel@tonic-gate 	if ((sip->si_flags & IDNRUNNING) == 0) {
9770Sstevel@tonic-gate 		if (idndl_init(sip)) {
9780Sstevel@tonic-gate 			DLERRORACK(wq, mp, dlp->dl_primitive,
9790Sstevel@tonic-gate 					DL_INITFAILED, 0);
9800Sstevel@tonic-gate 			(void) qassociate(wq, -1);
9810Sstevel@tonic-gate 			return;
9820Sstevel@tonic-gate 		}
9830Sstevel@tonic-gate 	}
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 	/*
9860Sstevel@tonic-gate 	 * Set link to device and update our state.
9870Sstevel@tonic-gate 	 */
9880Sstevel@tonic-gate 	stp->ss_sip = sip;
9890Sstevel@tonic-gate 	stp->ss_state = DL_UNBOUND;
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate 	DLOKACK(wq, mp, DL_ATTACH_REQ);
9920Sstevel@tonic-gate }
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate static void
9950Sstevel@tonic-gate idndl_dreq(queue_t *wq, mblk_t *mp)
9960Sstevel@tonic-gate {
9970Sstevel@tonic-gate 	struct idnstr	*stp;
9980Sstevel@tonic-gate 
9990Sstevel@tonic-gate 	stp = (struct idnstr *)wq->q_ptr;
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate 	if (MBLKL(mp) < DL_DETACH_REQ_SIZE) {
10020Sstevel@tonic-gate 		DLERRORACK(wq, mp, DL_DETACH_REQ, DL_BADPRIM, 0);
10030Sstevel@tonic-gate 		return;
10040Sstevel@tonic-gate 	}
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 	if (stp->ss_state != DL_UNBOUND) {
10070Sstevel@tonic-gate 		DLERRORACK(wq, mp, DL_DETACH_REQ, DL_OUTSTATE, 0);
10080Sstevel@tonic-gate 		return;
10090Sstevel@tonic-gate 	}
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 	idndl_dodetach(stp);
10120Sstevel@tonic-gate 	(void) qassociate(wq, -1);
10130Sstevel@tonic-gate 	DLOKACK(wq, mp, DL_DETACH_REQ);
10140Sstevel@tonic-gate }
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate /*
10170Sstevel@tonic-gate  * Detach a Stream from an interface.
10180Sstevel@tonic-gate  */
10190Sstevel@tonic-gate void
10200Sstevel@tonic-gate idndl_dodetach(struct idnstr *stp)
10210Sstevel@tonic-gate {
10220Sstevel@tonic-gate 	struct idnstr	*tstp;
10230Sstevel@tonic-gate 	struct idn	*sip;
10240Sstevel@tonic-gate 	int		reinit = 0;
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 	ASSERT(stp->ss_sip);
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 	sip = stp->ss_sip;
10290Sstevel@tonic-gate 	stp->ss_sip = NULL;
10300Sstevel@tonic-gate 
10310Sstevel@tonic-gate 	/*
10320Sstevel@tonic-gate 	 * Disable promiscuous mode if on.
10330Sstevel@tonic-gate 	 */
10340Sstevel@tonic-gate 	if (stp->ss_flags & IDNSALLPHYS) {
10350Sstevel@tonic-gate 		stp->ss_flags &= ~IDNSALLPHYS;
10360Sstevel@tonic-gate 		reinit = 1;
10370Sstevel@tonic-gate 	}
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 	/*
10400Sstevel@tonic-gate 	 * Disable ALLMULTI mode if on.
10410Sstevel@tonic-gate 	 */
10420Sstevel@tonic-gate 	if (stp->ss_flags & IDNSALLMULTI) {
10430Sstevel@tonic-gate 		stp->ss_flags &= ~IDNSALLMULTI;
10440Sstevel@tonic-gate 		reinit = 1;
10450Sstevel@tonic-gate 	}
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 	/*
10480Sstevel@tonic-gate 	 * Disable any Multicast Addresses.
10490Sstevel@tonic-gate 	 */
10500Sstevel@tonic-gate 	stp->ss_mccount = 0;
10510Sstevel@tonic-gate 	if (stp->ss_mctab) {
10520Sstevel@tonic-gate 		kmem_free(stp->ss_mctab, IDNMCALLOC);
10530Sstevel@tonic-gate 		stp->ss_mctab = NULL;
10540Sstevel@tonic-gate 		reinit = 1;
10550Sstevel@tonic-gate 	}
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate 	/*
10580Sstevel@tonic-gate 	 * Detach from device structure.
10590Sstevel@tonic-gate 	 * Uninit the device when no other streams are attached to it.
10600Sstevel@tonic-gate 	 */
10610Sstevel@tonic-gate 	rw_enter(&idn.struprwlock, RW_READER);
10620Sstevel@tonic-gate 	for (tstp = idn.strup; tstp; tstp = tstp->ss_nextp)
10630Sstevel@tonic-gate 		if (tstp->ss_sip == sip)
10640Sstevel@tonic-gate 			break;
10650Sstevel@tonic-gate 	rw_exit(&idn.struprwlock);
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 	if (tstp == NULL)
10680Sstevel@tonic-gate 		idndl_uninit(sip);
10690Sstevel@tonic-gate 	else if (reinit)
10700Sstevel@tonic-gate 		(void) idndl_init(sip);
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 	stp->ss_state = DL_UNATTACHED;
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 	idndl_setipq(sip);
10750Sstevel@tonic-gate }
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate static void
10780Sstevel@tonic-gate idndl_breq(queue_t *wq, mblk_t *mp)
10790Sstevel@tonic-gate {
10800Sstevel@tonic-gate 	struct idnstr		*stp;
10810Sstevel@tonic-gate 	union DL_primitives	*dlp;
10820Sstevel@tonic-gate 	struct idn		*sip;
10830Sstevel@tonic-gate 	struct idndladdr	idnaddr;
10840Sstevel@tonic-gate 	t_uscalar_t		sap;
10850Sstevel@tonic-gate 	int		xidtest;
10860Sstevel@tonic-gate 	procname_t	proc = "idndl_breq";
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 	stp = (struct idnstr *)wq->q_ptr;
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate 	if (MBLKL(mp) < DL_BIND_REQ_SIZE) {
10910Sstevel@tonic-gate 		DLERRORACK(wq, mp, DL_BIND_REQ, DL_BADPRIM, 0);
10920Sstevel@tonic-gate 		return;
10930Sstevel@tonic-gate 	}
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 	if (stp->ss_state != DL_UNBOUND) {
10960Sstevel@tonic-gate 		DLERRORACK(wq, mp, DL_BIND_REQ, DL_OUTSTATE, 0);
10970Sstevel@tonic-gate 		return;
10980Sstevel@tonic-gate 	}
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate 	dlp = (union DL_primitives *)mp->b_rptr;
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate 	if (dlp->bind_req.dl_service_mode != idninfoack.dl_service_mode) {
11030Sstevel@tonic-gate 		DLERRORACK(wq, mp, DL_BIND_REQ, DL_UNSUPPORTED, 0);
11040Sstevel@tonic-gate 		return;
11050Sstevel@tonic-gate 	}
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate 	sip = stp->ss_sip;
11080Sstevel@tonic-gate 	sap = dlp->bind_req.dl_sap;
11090Sstevel@tonic-gate 	xidtest = dlp->bind_req.dl_xidtest_flg;
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 	ASSERT(sip);
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate 	if (xidtest) {
11140Sstevel@tonic-gate 		DLERRORACK(wq, mp, DL_BIND_REQ, DL_NOAUTO, 0);
11150Sstevel@tonic-gate 		return;
11160Sstevel@tonic-gate 	}
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate 	if (sap > ETHERTYPE_MAX) {
11190Sstevel@tonic-gate 		DLERRORACK(wq, mp, dlp->dl_primitive, DL_BADSAP, 0);
11200Sstevel@tonic-gate 		return;
11210Sstevel@tonic-gate 	}
11220Sstevel@tonic-gate 
11230Sstevel@tonic-gate 	/*
11240Sstevel@tonic-gate 	 * Save SAP value for this Stream and change state.
11250Sstevel@tonic-gate 	 */
11260Sstevel@tonic-gate 	stp->ss_sap = sap;
11270Sstevel@tonic-gate 	stp->ss_state = DL_IDLE;
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate 	idnaddr.dl_sap = sap;
11300Sstevel@tonic-gate 	ether_copy(&sip->si_ouraddr, &idnaddr.dl_phys);
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate 	if (IS_ETHERTYPE_IP(sap)) {
11330Sstevel@tonic-gate 		int	channel;
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 		channel =
11360Sstevel@tonic-gate 			(int)sip->si_ouraddr.ether_addr_octet[IDNETHER_CHANNEL];
11370Sstevel@tonic-gate 		PR_DLPI("%s: IP SAP, opening channel %d\n", proc, channel);
11380Sstevel@tonic-gate 		if (idn_open_channel(channel)) {
11390Sstevel@tonic-gate 			PR_DLPI("%s: FAILED TO OPEN CHANNEL %d\n",
11400Sstevel@tonic-gate 				proc, channel);
11410Sstevel@tonic-gate 			DLERRORACK(wq, mp, dlp->dl_primitive, DL_NOADDR, 0);
11420Sstevel@tonic-gate 			return;
11430Sstevel@tonic-gate 		}
11440Sstevel@tonic-gate 	}
11450Sstevel@tonic-gate 	DLBINDACK(wq, mp, sap, &idnaddr, IDNADDRL, 0, 0);
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate 	idndl_setipq(sip);
11480Sstevel@tonic-gate }
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate static void
11510Sstevel@tonic-gate idndl_ubreq(queue_t *wq, mblk_t *mp)
11520Sstevel@tonic-gate {
11530Sstevel@tonic-gate 	struct idnstr	*stp;
11540Sstevel@tonic-gate 	procname_t	proc = "idndl_ubreq";
11550Sstevel@tonic-gate 
11560Sstevel@tonic-gate 	stp = (struct idnstr *)wq->q_ptr;
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 	if (MBLKL(mp) < DL_UNBIND_REQ_SIZE) {
11590Sstevel@tonic-gate 		DLERRORACK(wq, mp, DL_UNBIND_REQ, DL_BADPRIM, 0);
11600Sstevel@tonic-gate 		return;
11610Sstevel@tonic-gate 	}
11620Sstevel@tonic-gate 
11630Sstevel@tonic-gate 	if (stp->ss_state != DL_IDLE) {
11640Sstevel@tonic-gate 		DLERRORACK(wq, mp, DL_UNBIND_REQ, DL_OUTSTATE, 0);
11650Sstevel@tonic-gate 		return;
11660Sstevel@tonic-gate 	}
11670Sstevel@tonic-gate 
11680Sstevel@tonic-gate 	stp->ss_state = DL_UNBOUND;
11690Sstevel@tonic-gate 
11700Sstevel@tonic-gate 	if (IS_ETHERTYPE_IP(stp->ss_sap)) {
11710Sstevel@tonic-gate 		struct idn	*sip;
11720Sstevel@tonic-gate 		int		channel;
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 		sip = stp->ss_sip;
11750Sstevel@tonic-gate 		channel =
11760Sstevel@tonic-gate 			(int)sip->si_ouraddr.ether_addr_octet[IDNETHER_CHANNEL];
11770Sstevel@tonic-gate 		PR_DLPI("%s: IP SAP, unbinding channel %d\n", proc, channel);
11780Sstevel@tonic-gate 		/*
11790Sstevel@tonic-gate 		 * We need to do an "soft" close since there's a
11800Sstevel@tonic-gate 		 * potential that we've been called by one of the
11810Sstevel@tonic-gate 		 * IDN data server/dispatcher threads!  We'll deadlock
11820Sstevel@tonic-gate 		 * if we attempt a "hard" close of the channel from here.
11830Sstevel@tonic-gate 		 */
11840Sstevel@tonic-gate 		idn_close_channel(channel, IDNCHAN_SOFT_CLOSE);
11850Sstevel@tonic-gate 	}
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 	stp->ss_sap = 0;
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate 	DLOKACK(wq, mp, DL_UNBIND_REQ);
11900Sstevel@tonic-gate 
11910Sstevel@tonic-gate 	idndl_setipq(stp->ss_sip);
11920Sstevel@tonic-gate }
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate static void
11950Sstevel@tonic-gate idndl_ireq(queue_t *wq, mblk_t *mp)
11960Sstevel@tonic-gate {
11970Sstevel@tonic-gate 	struct idnstr		*stp;
11980Sstevel@tonic-gate 	dl_info_ack_t		*dlip;
11990Sstevel@tonic-gate 	struct idndladdr	*dlap;
12000Sstevel@tonic-gate 	struct ether_addr	*ep;
12010Sstevel@tonic-gate 	int	size;
12020Sstevel@tonic-gate 
12030Sstevel@tonic-gate 	stp = (struct idnstr *)wq->q_ptr;
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate 	if (MBLKL(mp) < DL_INFO_REQ_SIZE) {
12060Sstevel@tonic-gate 		DLERRORACK(wq, mp, DL_INFO_REQ, DL_BADPRIM, 0);
12070Sstevel@tonic-gate 		return;
12080Sstevel@tonic-gate 	}
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate 	/*
12110Sstevel@tonic-gate 	 * Exchange current msg for a DL_INFO_ACK.
12120Sstevel@tonic-gate 	 */
12130Sstevel@tonic-gate 	size = sizeof (dl_info_ack_t) + IDNADDRL + ETHERADDRL;
12140Sstevel@tonic-gate 	if ((mp = mexchange(wq, mp, size, M_PCPROTO, DL_INFO_ACK)) == NULL)
12150Sstevel@tonic-gate 		return;
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate 	/*
12180Sstevel@tonic-gate 	 * Fill in the DL_INFO_ACK fields and reply.
12190Sstevel@tonic-gate 	 */
12200Sstevel@tonic-gate 	dlip = (dl_info_ack_t *)mp->b_rptr;
12210Sstevel@tonic-gate 	ASSERT(idninfoack.dl_max_sdu);
12220Sstevel@tonic-gate 	*dlip = idninfoack;
12230Sstevel@tonic-gate 	dlip->dl_current_state = stp->ss_state;
12240Sstevel@tonic-gate 	dlap = (struct idndladdr *)(mp->b_rptr + dlip->dl_addr_offset);
12250Sstevel@tonic-gate 	dlap->dl_sap = stp->ss_sap;
12260Sstevel@tonic-gate 	if (stp->ss_sip) {
12270Sstevel@tonic-gate 		ether_copy(&stp->ss_sip->si_ouraddr, &dlap->dl_phys);
12280Sstevel@tonic-gate 	} else {
12290Sstevel@tonic-gate 		bzero(&dlap->dl_phys, ETHERADDRL);
12300Sstevel@tonic-gate 	}
12310Sstevel@tonic-gate 	ep = (struct ether_addr *)(mp->b_rptr + dlip->dl_brdcst_addr_offset);
12320Sstevel@tonic-gate 	ether_copy(&etherbroadcastaddr, ep);
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate 	qreply(wq, mp);
12350Sstevel@tonic-gate }
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate static void
12380Sstevel@tonic-gate idndl_ponreq(queue_t *wq, mblk_t *mp)
12390Sstevel@tonic-gate {
12400Sstevel@tonic-gate 	struct idnstr	*stp;
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate 	stp = (struct idnstr *)wq->q_ptr;
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate 	if (MBLKL(mp) < DL_PROMISCON_REQ_SIZE) {
12450Sstevel@tonic-gate 		DLERRORACK(wq, mp, DL_PROMISCON_REQ, DL_BADPRIM, 0);
12460Sstevel@tonic-gate 		return;
12470Sstevel@tonic-gate 	}
12480Sstevel@tonic-gate 
12490Sstevel@tonic-gate 	switch (((dl_promiscon_req_t *)mp->b_rptr)->dl_level) {
12500Sstevel@tonic-gate 	case DL_PROMISC_PHYS:
12510Sstevel@tonic-gate 		stp->ss_flags |= IDNSALLPHYS;
12520Sstevel@tonic-gate 		break;
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate 	case DL_PROMISC_SAP:
12550Sstevel@tonic-gate 		stp->ss_flags |= IDNSALLSAP;
12560Sstevel@tonic-gate 		break;
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate 	case DL_PROMISC_MULTI:
12590Sstevel@tonic-gate 		stp->ss_flags |= IDNSALLMULTI;
12600Sstevel@tonic-gate 		break;
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate 	default:
12630Sstevel@tonic-gate 		DLERRORACK(wq, mp, DL_PROMISCON_REQ, DL_NOTSUPPORTED, 0);
12640Sstevel@tonic-gate 		return;
12650Sstevel@tonic-gate 	}
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 	if (stp->ss_sip)
12680Sstevel@tonic-gate 		(void) idndl_init(stp->ss_sip);
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate 	if (stp->ss_sip)
12710Sstevel@tonic-gate 		idndl_setipq(stp->ss_sip);
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate 	DLOKACK(wq, mp, DL_PROMISCON_REQ);
12740Sstevel@tonic-gate }
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate static void
12770Sstevel@tonic-gate idndl_poffreq(queue_t *wq, mblk_t *mp)
12780Sstevel@tonic-gate {
12790Sstevel@tonic-gate 	struct idnstr	*stp;
12800Sstevel@tonic-gate 	int		flag;
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate 	stp = (struct idnstr *)wq->q_ptr;
12830Sstevel@tonic-gate 
12840Sstevel@tonic-gate 	if (MBLKL(mp) < DL_PROMISCOFF_REQ_SIZE) {
12850Sstevel@tonic-gate 		DLERRORACK(wq, mp, DL_PROMISCOFF_REQ, DL_BADPRIM, 0);
12860Sstevel@tonic-gate 		return;
12870Sstevel@tonic-gate 	}
12880Sstevel@tonic-gate 
12890Sstevel@tonic-gate 	switch (((dl_promiscoff_req_t *)mp->b_rptr)->dl_level) {
12900Sstevel@tonic-gate 	case DL_PROMISC_PHYS:
12910Sstevel@tonic-gate 		flag = IDNSALLPHYS;
12920Sstevel@tonic-gate 		break;
12930Sstevel@tonic-gate 
12940Sstevel@tonic-gate 	case DL_PROMISC_SAP:
12950Sstevel@tonic-gate 		flag = IDNSALLSAP;
12960Sstevel@tonic-gate 		break;
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate 	case DL_PROMISC_MULTI:
12990Sstevel@tonic-gate 		flag = IDNSALLMULTI;
13000Sstevel@tonic-gate 		break;
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate 	default:
13030Sstevel@tonic-gate 		DLERRORACK(wq, mp, DL_PROMISCOFF_REQ, DL_NOTSUPPORTED, 0);
13040Sstevel@tonic-gate 		return;
13050Sstevel@tonic-gate 	}
13060Sstevel@tonic-gate 
13070Sstevel@tonic-gate 	if ((stp->ss_flags & flag) == 0) {
13080Sstevel@tonic-gate 		DLERRORACK(wq, mp, DL_PROMISCOFF_REQ, DL_NOTENAB, 0);
13090Sstevel@tonic-gate 		return;
13100Sstevel@tonic-gate 	}
13110Sstevel@tonic-gate 
13120Sstevel@tonic-gate 	stp->ss_flags &= ~flag;
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate 	if (stp->ss_sip)
13150Sstevel@tonic-gate 		(void) idndl_init(stp->ss_sip);
13160Sstevel@tonic-gate 
13170Sstevel@tonic-gate 	if (stp->ss_sip)
13180Sstevel@tonic-gate 		idndl_setipq(stp->ss_sip);
13190Sstevel@tonic-gate 
13200Sstevel@tonic-gate 	DLOKACK(wq, mp, DL_PROMISCOFF_REQ);
13210Sstevel@tonic-gate }
13220Sstevel@tonic-gate 
13230Sstevel@tonic-gate static void
13240Sstevel@tonic-gate idndl_emreq(queue_t *wq, mblk_t *mp)
13250Sstevel@tonic-gate {
13260Sstevel@tonic-gate 	struct idnstr		*stp;
13270Sstevel@tonic-gate 	union DL_primitives	*dlp;
13280Sstevel@tonic-gate 	struct ether_addr	*addrp;
13290Sstevel@tonic-gate 	int	off;
13300Sstevel@tonic-gate 	int	len;
13310Sstevel@tonic-gate 	int	i;
13320Sstevel@tonic-gate 
13330Sstevel@tonic-gate 	stp = (struct idnstr *)wq->q_ptr;
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate 	if (MBLKL(mp) < DL_ENABMULTI_REQ_SIZE) {
13360Sstevel@tonic-gate 		DLERRORACK(wq, mp, DL_ENABMULTI_REQ, DL_BADPRIM, 0);
13370Sstevel@tonic-gate 		return;
13380Sstevel@tonic-gate 	}
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate 	if (stp->ss_state == DL_UNATTACHED) {
13410Sstevel@tonic-gate 		DLERRORACK(wq, mp, DL_ENABMULTI_REQ, DL_OUTSTATE, 0);
13420Sstevel@tonic-gate 		return;
13430Sstevel@tonic-gate 	}
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 	dlp = (union DL_primitives *)mp->b_rptr;
13460Sstevel@tonic-gate 	len = dlp->enabmulti_req.dl_addr_length;
13470Sstevel@tonic-gate 	off = dlp->enabmulti_req.dl_addr_offset;
13480Sstevel@tonic-gate 	addrp = (struct ether_addr *)(mp->b_rptr + off);
13490Sstevel@tonic-gate 
13500Sstevel@tonic-gate 	if ((len != ETHERADDRL) ||
13510Sstevel@tonic-gate 		!MBLKIN(mp, off, len) ||
13520Sstevel@tonic-gate 		!IDNDL_ADDR_IS_MULTICAST(addrp)) {
13530Sstevel@tonic-gate 		DLERRORACK(wq, mp, DL_ENABMULTI_REQ, DL_BADADDR, 0);
13540Sstevel@tonic-gate 		return;
13550Sstevel@tonic-gate 	}
13560Sstevel@tonic-gate 
13570Sstevel@tonic-gate 	if ((stp->ss_mccount + 1) >= IDNMAXMC) {
13580Sstevel@tonic-gate 		DLERRORACK(wq, mp, DL_ENABMULTI_REQ, DL_TOOMANY, 0);
13590Sstevel@tonic-gate 		return;
13600Sstevel@tonic-gate 	}
13610Sstevel@tonic-gate 
13620Sstevel@tonic-gate 	/*
13630Sstevel@tonic-gate 	 * Allocate table on first request.
13640Sstevel@tonic-gate 	 */
13650Sstevel@tonic-gate 	if (stp->ss_mctab == NULL)
13660Sstevel@tonic-gate 		stp->ss_mctab = kmem_alloc(IDNMCALLOC, KM_SLEEP);
13670Sstevel@tonic-gate 
13680Sstevel@tonic-gate 	/*
13690Sstevel@tonic-gate 	 * Check to see if the address is already in the table.
13700Sstevel@tonic-gate 	 * Bug 1209733:
13710Sstevel@tonic-gate 	 * If present in the table, add the entry to the end of the table
13720Sstevel@tonic-gate 	 * and return without initializing the hardware.
13730Sstevel@tonic-gate 	 */
13740Sstevel@tonic-gate 	for (i = 0; i < stp->ss_mccount; i++) {
13750Sstevel@tonic-gate 		if (ether_cmp(&stp->ss_mctab[i], addrp) == 0) {
13760Sstevel@tonic-gate 			stp->ss_mctab[stp->ss_mccount++] = *addrp;
13770Sstevel@tonic-gate 			DLOKACK(wq, mp, DL_ENABMULTI_REQ);
13780Sstevel@tonic-gate 			return;
13790Sstevel@tonic-gate 		}
13800Sstevel@tonic-gate 	}
13810Sstevel@tonic-gate 
13820Sstevel@tonic-gate 	stp->ss_mctab[stp->ss_mccount++] = *addrp;
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate 	(void) idndl_init(stp->ss_sip);
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate 	DLOKACK(wq, mp, DL_ENABMULTI_REQ);
13870Sstevel@tonic-gate }
13880Sstevel@tonic-gate 
13890Sstevel@tonic-gate static void
13900Sstevel@tonic-gate idndl_dmreq(queue_t *wq, mblk_t *mp)
13910Sstevel@tonic-gate {
13920Sstevel@tonic-gate 	struct idnstr		*stp;
13930Sstevel@tonic-gate 	union DL_primitives	*dlp;
13940Sstevel@tonic-gate 	struct ether_addr	*addrp;
13950Sstevel@tonic-gate 	int	off;
13960Sstevel@tonic-gate 	int	len;
13970Sstevel@tonic-gate 	int	i;
13980Sstevel@tonic-gate 
13990Sstevel@tonic-gate 	stp = (struct idnstr *)wq->q_ptr;
14000Sstevel@tonic-gate 
14010Sstevel@tonic-gate 	if (MBLKL(mp) < DL_DISABMULTI_REQ_SIZE) {
14020Sstevel@tonic-gate 		DLERRORACK(wq, mp, DL_DISABMULTI_REQ, DL_BADPRIM, 0);
14030Sstevel@tonic-gate 		return;
14040Sstevel@tonic-gate 	}
14050Sstevel@tonic-gate 
14060Sstevel@tonic-gate 	if (stp->ss_state == DL_UNATTACHED) {
14070Sstevel@tonic-gate 		DLERRORACK(wq, mp, DL_DISABMULTI_REQ, DL_OUTSTATE, 0);
14080Sstevel@tonic-gate 		return;
14090Sstevel@tonic-gate 	}
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate 	dlp = (union DL_primitives *)mp->b_rptr;
14120Sstevel@tonic-gate 	len = dlp->disabmulti_req.dl_addr_length;
14130Sstevel@tonic-gate 	off = dlp->disabmulti_req.dl_addr_offset;
14140Sstevel@tonic-gate 	addrp = (struct ether_addr *)(mp->b_rptr + off);
14150Sstevel@tonic-gate 
14160Sstevel@tonic-gate 	if ((len != ETHERADDRL) || !MBLKIN(mp, off, len)) {
14170Sstevel@tonic-gate 		DLERRORACK(wq, mp, DL_DISABMULTI_REQ, DL_BADADDR, 0);
14180Sstevel@tonic-gate 		return;
14190Sstevel@tonic-gate 	}
14200Sstevel@tonic-gate 
14210Sstevel@tonic-gate 	/*
14220Sstevel@tonic-gate 	 * Find the address in the multicast table for this Stream
14230Sstevel@tonic-gate 	 * and delete it by shifting all subsequent multicast
14240Sstevel@tonic-gate 	 * table entries over one.
14250Sstevel@tonic-gate 	 */
14260Sstevel@tonic-gate 	for (i = 0; i < stp->ss_mccount; i++)
14270Sstevel@tonic-gate 		if (ether_cmp(addrp, &stp->ss_mctab[i]) == 0) {
14280Sstevel@tonic-gate 			bcopy(&stp->ss_mctab[i+1],
14290Sstevel@tonic-gate 				&stp->ss_mctab[i],
14300Sstevel@tonic-gate 				((stp->ss_mccount - i) *
14310Sstevel@tonic-gate 				sizeof (struct ether_addr)));
14320Sstevel@tonic-gate 			stp->ss_mccount--;
14330Sstevel@tonic-gate 			(void) idndl_init(stp->ss_sip);
14340Sstevel@tonic-gate 			DLOKACK(wq, mp, DL_DISABMULTI_REQ);
14350Sstevel@tonic-gate 			return;
14360Sstevel@tonic-gate 		}
14370Sstevel@tonic-gate 	DLERRORACK(wq, mp, DL_DISABMULTI_REQ, DL_NOTENAB, 0);
14380Sstevel@tonic-gate }
14390Sstevel@tonic-gate 
14400Sstevel@tonic-gate static void
14410Sstevel@tonic-gate idndl_pareq(queue_t *wq, mblk_t *mp)
14420Sstevel@tonic-gate {
14430Sstevel@tonic-gate 	struct idnstr		*stp;
14440Sstevel@tonic-gate 	union DL_primitives	*dlp;
14450Sstevel@tonic-gate 	int			type;
14460Sstevel@tonic-gate 	struct idn		*sip;
14470Sstevel@tonic-gate 	struct ether_addr	addr;
14480Sstevel@tonic-gate 
14490Sstevel@tonic-gate 	stp = (struct idnstr *)wq->q_ptr;
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate 	if (MBLKL(mp) < DL_PHYS_ADDR_REQ_SIZE) {
14520Sstevel@tonic-gate 		DLERRORACK(wq, mp, DL_PHYS_ADDR_REQ, DL_BADPRIM, 0);
14530Sstevel@tonic-gate 		return;
14540Sstevel@tonic-gate 	}
14550Sstevel@tonic-gate 
14560Sstevel@tonic-gate 	dlp  = (union DL_primitives *)mp->b_rptr;
14570Sstevel@tonic-gate 	type = dlp->physaddr_req.dl_addr_type;
14580Sstevel@tonic-gate 	sip  = stp->ss_sip;
14590Sstevel@tonic-gate 
14600Sstevel@tonic-gate 	if (sip == NULL) {
14610Sstevel@tonic-gate 		DLERRORACK(wq, mp, DL_PHYS_ADDR_REQ, DL_OUTSTATE, 0);
14620Sstevel@tonic-gate 		return;
14630Sstevel@tonic-gate 	}
14640Sstevel@tonic-gate 
14650Sstevel@tonic-gate 	switch (type) {
14660Sstevel@tonic-gate 	case DL_FACT_PHYS_ADDR:
14670Sstevel@tonic-gate 		idndl_localetheraddr(sip, &addr);
14680Sstevel@tonic-gate 		break;
14690Sstevel@tonic-gate 
14700Sstevel@tonic-gate 	case DL_CURR_PHYS_ADDR:
14710Sstevel@tonic-gate 		ether_copy(&sip->si_ouraddr, &addr);
14720Sstevel@tonic-gate 		break;
14730Sstevel@tonic-gate 
14740Sstevel@tonic-gate 	default:
14750Sstevel@tonic-gate 		DLERRORACK(wq, mp, DL_PHYS_ADDR_REQ, DL_NOTSUPPORTED, 0);
14760Sstevel@tonic-gate 		return;
14770Sstevel@tonic-gate 	}
14780Sstevel@tonic-gate 
14790Sstevel@tonic-gate 	DLPHYSADDRACK(wq, mp, &addr, ETHERADDRL);
14800Sstevel@tonic-gate }
14810Sstevel@tonic-gate 
14820Sstevel@tonic-gate #ifdef notdef
14830Sstevel@tonic-gate static void
14840Sstevel@tonic-gate idndl_spareq(queue_t *wq, mblk_t *mp)
14850Sstevel@tonic-gate {
14860Sstevel@tonic-gate 	struct idnstr		*stp;
14870Sstevel@tonic-gate 	union DL_primitives	*dlp;
14880Sstevel@tonic-gate 	int	off;
14890Sstevel@tonic-gate 	int	len;
14900Sstevel@tonic-gate 	struct ether_addr	*addrp;
14910Sstevel@tonic-gate 	struct idn		*sip;
14920Sstevel@tonic-gate 
14930Sstevel@tonic-gate 	stp = (struct idnstr *)wq->q_ptr;
14940Sstevel@tonic-gate 
14950Sstevel@tonic-gate 	if (MBLKL(mp) < DL_SET_PHYS_ADDR_REQ_SIZE) {
14960Sstevel@tonic-gate 		DLERRORACK(wq, mp, DL_SET_PHYS_ADDR_REQ, DL_BADPRIM, 0);
14970Sstevel@tonic-gate 		return;
14980Sstevel@tonic-gate 	}
14990Sstevel@tonic-gate 
15000Sstevel@tonic-gate 	dlp = (union DL_primitives *)mp->b_rptr;
15010Sstevel@tonic-gate 	len = dlp->set_physaddr_req.dl_addr_length;
15020Sstevel@tonic-gate 	off = dlp->set_physaddr_req.dl_addr_offset;
15030Sstevel@tonic-gate 
15040Sstevel@tonic-gate 	if (!MBLKIN(mp, off, len)) {
15050Sstevel@tonic-gate 		DLERRORACK(wq, mp, DL_SET_PHYS_ADDR_REQ, DL_BADPRIM, 0);
15060Sstevel@tonic-gate 		return;
15070Sstevel@tonic-gate 	}
15080Sstevel@tonic-gate 
15090Sstevel@tonic-gate 	addrp = (struct ether_addr *)(mp->b_rptr + off);
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate 	/*
15120Sstevel@tonic-gate 	 * Error if length of address isn't right or the address
15130Sstevel@tonic-gate 	 * specified is a multicast or broadcast address.
15140Sstevel@tonic-gate 	 */
15150Sstevel@tonic-gate 	if ((len != ETHERADDRL) ||
15160Sstevel@tonic-gate 	    IDNDL_ADDR_IS_MULTICAST(addrp) ||
15170Sstevel@tonic-gate 	    (ether_cmp(addrp, &etherbroadcastaddr) == 0)) {
15180Sstevel@tonic-gate 		DLERRORACK(wq, mp, DL_SET_PHYS_ADDR_REQ, DL_BADADDR, 0);
15190Sstevel@tonic-gate 		return;
15200Sstevel@tonic-gate 	}
15210Sstevel@tonic-gate 
15220Sstevel@tonic-gate 	/*
15230Sstevel@tonic-gate 	 * Error if this stream is not attached to a device.
15240Sstevel@tonic-gate 	 */
15250Sstevel@tonic-gate 	if ((sip = stp->ss_sip) == NULL) {
15260Sstevel@tonic-gate 		DLERRORACK(wq, mp, DL_SET_PHYS_ADDR_REQ, DL_OUTSTATE, 0);
15270Sstevel@tonic-gate 		return;
15280Sstevel@tonic-gate 	}
15290Sstevel@tonic-gate 
15300Sstevel@tonic-gate 	/*
15310Sstevel@tonic-gate 	 * Set new interface local address and re-init device.
15320Sstevel@tonic-gate 	 * This is destructive to any other streams attached
15330Sstevel@tonic-gate 	 * to this device.
15340Sstevel@tonic-gate 	 */
15350Sstevel@tonic-gate 	ether_copy(addrp, &sip->si_ouraddr);
15360Sstevel@tonic-gate 	(void) idndl_init(stp->ss_sip);
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate 	DLOKACK(wq, mp, DL_SET_PHYS_ADDR_REQ);
15390Sstevel@tonic-gate }
15400Sstevel@tonic-gate #endif /* notdef */
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate static void
15430Sstevel@tonic-gate idndl_udreq(queue_t *wq, mblk_t *mp)
15440Sstevel@tonic-gate {
15450Sstevel@tonic-gate 	struct idnstr			*stp;
15460Sstevel@tonic-gate 	register struct idn		*sip;
15470Sstevel@tonic-gate 	register dl_unitdata_req_t	*dludp;
15480Sstevel@tonic-gate 	mblk_t				*nmp;
15490Sstevel@tonic-gate 	struct idndladdr	*dlap;
15500Sstevel@tonic-gate 	struct ether_header	*headerp;
15510Sstevel@tonic-gate 	t_uscalar_t		off, len;
15520Sstevel@tonic-gate 	t_uscalar_t		sap;
15530Sstevel@tonic-gate 
15540Sstevel@tonic-gate 	stp = (struct idnstr *)wq->q_ptr;
15550Sstevel@tonic-gate 	sip = stp->ss_sip;
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate 	if (stp->ss_state != DL_IDLE) {
15580Sstevel@tonic-gate 		DLERRORACK(wq, mp, DL_UNITDATA_REQ, DL_OUTSTATE, 0);
15590Sstevel@tonic-gate 		return;
15600Sstevel@tonic-gate 	}
15610Sstevel@tonic-gate 
15620Sstevel@tonic-gate 	dludp = (dl_unitdata_req_t *)mp->b_rptr;
15630Sstevel@tonic-gate 
15640Sstevel@tonic-gate 	off = dludp->dl_dest_addr_offset;
15650Sstevel@tonic-gate 	len = dludp->dl_dest_addr_length;
15660Sstevel@tonic-gate 
15670Sstevel@tonic-gate 	/*
15680Sstevel@tonic-gate 	 * Validate destination address format.
15690Sstevel@tonic-gate 	 */
15700Sstevel@tonic-gate 	if (!MBLKIN(mp, off, len) || (len != IDNADDRL)) {
15710Sstevel@tonic-gate 		dluderrorind(wq, mp, mp->b_rptr + off, len, DL_BADADDR, 0);
15720Sstevel@tonic-gate 		return;
15730Sstevel@tonic-gate 	}
15740Sstevel@tonic-gate 
15750Sstevel@tonic-gate 	/*
15760Sstevel@tonic-gate 	 * Error if no M_DATA follows.
15770Sstevel@tonic-gate 	 */
15780Sstevel@tonic-gate 	nmp = mp->b_cont;
15790Sstevel@tonic-gate 	if (nmp == NULL) {
15800Sstevel@tonic-gate 		dluderrorind(wq, mp, mp->b_rptr + off, len, DL_BADDATA, 0);
15810Sstevel@tonic-gate 		return;
15820Sstevel@tonic-gate 	}
15830Sstevel@tonic-gate 
15840Sstevel@tonic-gate 	dlap = (struct idndladdr *)(mp->b_rptr + off);
15850Sstevel@tonic-gate 
15860Sstevel@tonic-gate 	/*
15870Sstevel@tonic-gate 	 * Create ethernet header by either prepending it onto the
15880Sstevel@tonic-gate 	 * next mblk if potential, or reusing the M_PROTO block if not.
15890Sstevel@tonic-gate 	 */
15900Sstevel@tonic-gate 	if ((DB_REF(nmp) == 1) &&
15910Sstevel@tonic-gate 	    (MBLKHEAD(nmp) >= sizeof (struct ether_header)) &&
15920Sstevel@tonic-gate 	    (((ulong_t)nmp->b_rptr & 0x1) == 0)) {
15930Sstevel@tonic-gate 		nmp->b_rptr -= sizeof (struct ether_header);
15940Sstevel@tonic-gate 		headerp = (struct ether_header *)nmp->b_rptr;
15950Sstevel@tonic-gate 		ether_copy(&dlap->dl_phys, &headerp->ether_dhost);
15960Sstevel@tonic-gate 		ether_copy(&sip->si_ouraddr, &headerp->ether_shost);
15970Sstevel@tonic-gate 		sap = dlap->dl_sap;
15980Sstevel@tonic-gate 		freeb(mp);
15990Sstevel@tonic-gate 		mp = nmp;
16000Sstevel@tonic-gate 	} else {
16010Sstevel@tonic-gate 		DB_TYPE(mp) = M_DATA;
16020Sstevel@tonic-gate 		headerp = (struct ether_header *)mp->b_rptr;
16030Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + sizeof (struct ether_header);
16040Sstevel@tonic-gate 		ether_copy(&dlap->dl_phys, &headerp->ether_dhost);
16050Sstevel@tonic-gate 		ether_copy(&sip->si_ouraddr, &headerp->ether_shost);
16060Sstevel@tonic-gate 		sap = dlap->dl_sap;
16070Sstevel@tonic-gate 	}
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate 	/*
16100Sstevel@tonic-gate 	 * For transmitting, the driver looks at the
16110Sstevel@tonic-gate 	 * sap field of the DL_BIND_REQ being 0 in addition to the type
16120Sstevel@tonic-gate 	 * field in the range [0-1500]. If either is true, then the driver
16130Sstevel@tonic-gate 	 * computes the length of the message, not including initial M_PROTO
16140Sstevel@tonic-gate 	 * mblk (message block), of all subsequent DL_UNITDATA_REQ messages and
16150Sstevel@tonic-gate 	 * transmits 802.3 frames that have this value in the MAC frame header
16160Sstevel@tonic-gate 	 * length field.
16170Sstevel@tonic-gate 	 */
16180Sstevel@tonic-gate 	if ((sap <= ETHERMTU) || (stp->ss_sap == 0))
16190Sstevel@tonic-gate 		headerp->ether_type = (msgsize(mp) -
16200Sstevel@tonic-gate 					sizeof (struct ether_header));
16210Sstevel@tonic-gate 	else
16220Sstevel@tonic-gate 		headerp->ether_type = sap;
16230Sstevel@tonic-gate 
16240Sstevel@tonic-gate 	/*
16250Sstevel@tonic-gate 	 * The data transfer code requires only READ access (idn_wput_data).
16260Sstevel@tonic-gate 	 */
16270Sstevel@tonic-gate 	rw_downgrade(&stp->ss_rwlock);
16280Sstevel@tonic-gate 	(void) idndl_start(wq, mp, sip);
16290Sstevel@tonic-gate }
16300Sstevel@tonic-gate 
16310Sstevel@tonic-gate int
16320Sstevel@tonic-gate idndl_start(queue_t *wq, register mblk_t *mp, register struct idn *sip)
16330Sstevel@tonic-gate {
16340Sstevel@tonic-gate 	int		rv = 0;
16350Sstevel@tonic-gate 	int		flags;
16360Sstevel@tonic-gate 	int		broadcast = 0;
16370Sstevel@tonic-gate 	int		goagain = 0;
16380Sstevel@tonic-gate 	int		goqueue = 0;
16390Sstevel@tonic-gate 	int		msgcount;
16400Sstevel@tonic-gate 	char		channel;
16410Sstevel@tonic-gate 	mblk_t		*nmp = NULL;
16420Sstevel@tonic-gate 	int		domid;
16430Sstevel@tonic-gate 	domainset_t	domset;
16440Sstevel@tonic-gate 	idn_netaddr_t	netaddr;
16450Sstevel@tonic-gate 	struct idnstr	*stp;
16460Sstevel@tonic-gate 	struct ether_header	*ehp;
16470Sstevel@tonic-gate 	procname_t	proc = "idndl_start";
16480Sstevel@tonic-gate 
16490Sstevel@tonic-gate 	ASSERT(DB_TYPE(mp) == M_DATA);
16500Sstevel@tonic-gate 
16510Sstevel@tonic-gate 	stp = (struct idnstr *)wq->q_ptr;
16520Sstevel@tonic-gate 	ASSERT(sip == stp->ss_sip);
16530Sstevel@tonic-gate 	flags = sip->si_flags;
16540Sstevel@tonic-gate 	channel = (char)sip->si_ouraddr.ether_addr_octet[IDNETHER_CHANNEL];
16550Sstevel@tonic-gate 
16560Sstevel@tonic-gate 	ASSERT(RW_READ_HELD(&stp->ss_rwlock));
16570Sstevel@tonic-gate 
16580Sstevel@tonic-gate 	if ((flags & (IDNRUNNING|IDNPROMISC)) != IDNRUNNING) {
16590Sstevel@tonic-gate 		if (!(flags & IDNRUNNING))
16600Sstevel@tonic-gate 			goto requeue;
16610Sstevel@tonic-gate 	}
16620Sstevel@tonic-gate 
16630Sstevel@tonic-gate 	/*
16640Sstevel@tonic-gate 	 * Translate an IDN ethernet address into a domainid
16650Sstevel@tonic-gate 	 * and idnaddr.
16660Sstevel@tonic-gate 	 */
16670Sstevel@tonic-gate 	ehp = (struct ether_header *)mp->b_rptr;
16680Sstevel@tonic-gate 	domid = IDNDL_ETHER2DOMAIN(&ehp->ether_dhost);
16690Sstevel@tonic-gate 
16700Sstevel@tonic-gate 	/*
16710Sstevel@tonic-gate 	 * update MIB II statistics
16720Sstevel@tonic-gate 	 */
16730Sstevel@tonic-gate 	BUMP_OutNUcast(sip, ehp);
16740Sstevel@tonic-gate 
16750Sstevel@tonic-gate 	PR_DLPI("%s: ether %x:%x:%x:%x:%x:%x (domid = %d)\n",
16760Sstevel@tonic-gate 		proc, ehp->ether_dhost.ether_addr_octet[0],
16770Sstevel@tonic-gate 		ehp->ether_dhost.ether_addr_octet[1],
16780Sstevel@tonic-gate 		ehp->ether_dhost.ether_addr_octet[2],
16790Sstevel@tonic-gate 		ehp->ether_dhost.ether_addr_octet[3],
16800Sstevel@tonic-gate 		ehp->ether_dhost.ether_addr_octet[4],
16810Sstevel@tonic-gate 		ehp->ether_dhost.ether_addr_octet[5],
16820Sstevel@tonic-gate 		domid);
16830Sstevel@tonic-gate 
16840Sstevel@tonic-gate 	netaddr.net.chan = channel;
16850Sstevel@tonic-gate 	PR_DLPI("%s: source channel = %d\n", proc, (int)channel);
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate 	if ((ether_cmp(&ehp->ether_dhost, &etherbroadcastaddr) == 0) ||
16880Sstevel@tonic-gate 			IDNDL_ADDR_IS_MULTICAST(&ehp->ether_dhost)) {
16890Sstevel@tonic-gate 		/*
16900Sstevel@tonic-gate 		 * Caller wants to broadcast!
16910Sstevel@tonic-gate 		 * XXX - Send to everybody but ourself???
16920Sstevel@tonic-gate 		 */
16930Sstevel@tonic-gate 		PR_DLPI("%s: broadcast/multicast requested!!!\n", proc);
16940Sstevel@tonic-gate 		domset = ~DOMAINSET(idn.localid);
16950Sstevel@tonic-gate 		broadcast = 1;
16960Sstevel@tonic-gate 		netaddr.net.netid = IDN_BROADCAST_ALLNETID;
16970Sstevel@tonic-gate 		if ((flags & IDNPROMISC) &&
16980Sstevel@tonic-gate 		    ((nmp = copymsg(mp)) == NULL)) {
16990Sstevel@tonic-gate 			IDN_KSTAT_INC(sip, si_allocbfail);
17000Sstevel@tonic-gate 		}
17010Sstevel@tonic-gate 
17020Sstevel@tonic-gate 	} else if (domid != IDN_NIL_DOMID) {
17030Sstevel@tonic-gate 		domset = DOMAINSET(domid);
17040Sstevel@tonic-gate 		netaddr.net.netid = idn_domain[domid].dnetid;
17050Sstevel@tonic-gate 		if ((flags & IDNPROMISC) &&
17060Sstevel@tonic-gate 		    ((nmp = copymsg(mp)) == NULL)) {
17070Sstevel@tonic-gate 			IDN_KSTAT_INC(sip, si_allocbfail);
17080Sstevel@tonic-gate 		}
17090Sstevel@tonic-gate 	} else {
17100Sstevel@tonic-gate #ifdef DEBUG
17110Sstevel@tonic-gate 		int	netid;
17120Sstevel@tonic-gate 
17130Sstevel@tonic-gate 		netid = (int)
17140Sstevel@tonic-gate 			ehp->ether_dhost.ether_addr_octet[IDNETHER_NETID];
17150Sstevel@tonic-gate 		PR_DLPI("%s: no domain found for netid 0x%x\n",
17160Sstevel@tonic-gate 			proc, netid);
17170Sstevel@tonic-gate #endif /* DEBUG */
17180Sstevel@tonic-gate 		goto bad;
17190Sstevel@tonic-gate 	}
17200Sstevel@tonic-gate 
17210Sstevel@tonic-gate 	PR_DLPI("%s: target domainset = 0x%x\n", proc, domset);
17220Sstevel@tonic-gate 
17230Sstevel@tonic-gate 	if ((domset == 0) && (domid == IDN_NIL_DOMID)) {
17240Sstevel@tonic-gate 		PR_DLPI("%s: not connected to any domains!!  Bailing\n",
17250Sstevel@tonic-gate 			proc);
17260Sstevel@tonic-gate 		goto bad;
17270Sstevel@tonic-gate 	}
17280Sstevel@tonic-gate 	/*
17290Sstevel@tonic-gate 	 * XXX - Need to find a better way to handle broadcasting.
17300Sstevel@tonic-gate 	 *	 Should be able to take advantage of the fact that
17310Sstevel@tonic-gate 	 *	 we can broadcast XDC's (xdc_some).  Need to use
17320Sstevel@tonic-gate 	 *	 atomic counter (semaphore) instead of binary
17330Sstevel@tonic-gate 	 *	 "owner" flag, or perhaps domain specific owner bytes.
17340Sstevel@tonic-gate 	 *
17350Sstevel@tonic-gate 	 * Transfer the data.
17360Sstevel@tonic-gate 	 */
17370Sstevel@tonic-gate 	msgcount = 0;
17380Sstevel@tonic-gate 	if (!broadcast)
17390Sstevel@tonic-gate 		goto noloop;
17400Sstevel@tonic-gate 
17410Sstevel@tonic-gate 	for (domid = 0; domid < MAX_DOMAINS; domid++) {
17420Sstevel@tonic-gate 		if (!DOMAIN_IN_SET(domset, domid))
17430Sstevel@tonic-gate 			continue;
17440Sstevel@tonic-gate 
17450Sstevel@tonic-gate noloop:
17460Sstevel@tonic-gate 
17470Sstevel@tonic-gate 		if (idn_domain[domid].dcpu == IDN_NIL_DCPU) {
17480Sstevel@tonic-gate 			if (broadcast)
17490Sstevel@tonic-gate 				continue;
17500Sstevel@tonic-gate 			else
17510Sstevel@tonic-gate 				break;
17520Sstevel@tonic-gate 		}
17530Sstevel@tonic-gate 
17540Sstevel@tonic-gate 		rv = idn_send_data(domid, netaddr, wq, mp);
17550Sstevel@tonic-gate 
17560Sstevel@tonic-gate 		switch (rv) {
17570Sstevel@tonic-gate 		case IDNXMIT_LOOP:	/* handled in loopback */
17580Sstevel@tonic-gate 			msgcount++;
17590Sstevel@tonic-gate 			break;
17600Sstevel@tonic-gate 
17610Sstevel@tonic-gate 		case IDNXMIT_OKAY:	/* handled, okay to free */
17620Sstevel@tonic-gate 			msgcount++;
17630Sstevel@tonic-gate 			break;
17640Sstevel@tonic-gate 
17650Sstevel@tonic-gate 		case IDNXMIT_RETRY:
17660Sstevel@tonic-gate 			if (!broadcast)
17670Sstevel@tonic-gate 				goto tryagain;
17680Sstevel@tonic-gate 			goagain++;
17690Sstevel@tonic-gate 			break;
17700Sstevel@tonic-gate 
17710Sstevel@tonic-gate 		case IDNXMIT_REQUEUE:
17720Sstevel@tonic-gate 			if (!broadcast)
17730Sstevel@tonic-gate 				goto requeue;
17740Sstevel@tonic-gate 			goqueue++;
17750Sstevel@tonic-gate 			break;
17760Sstevel@tonic-gate 
17770Sstevel@tonic-gate 		default:
17780Sstevel@tonic-gate 			if (!broadcast)
17790Sstevel@tonic-gate 				goto bad;
17800Sstevel@tonic-gate 			break;
17810Sstevel@tonic-gate 		}
17820Sstevel@tonic-gate 		if (!broadcast)
17830Sstevel@tonic-gate 			break;
17840Sstevel@tonic-gate 	}
17850Sstevel@tonic-gate 
17860Sstevel@tonic-gate 	if (msgcount == 0)
17870Sstevel@tonic-gate 		if (goqueue)
17880Sstevel@tonic-gate 			goto requeue;
17890Sstevel@tonic-gate 		else if (goagain)
17900Sstevel@tonic-gate 			goto tryagain;
17910Sstevel@tonic-gate 		else
17920Sstevel@tonic-gate 			goto bad;
17930Sstevel@tonic-gate 
17940Sstevel@tonic-gate 	if ((flags & IDNPROMISC) && nmp)
17950Sstevel@tonic-gate 		idndl_sendup(sip, nmp, idndl_paccept);
17960Sstevel@tonic-gate 
17970Sstevel@tonic-gate 	freemsg(mp);
17980Sstevel@tonic-gate 
17990Sstevel@tonic-gate 	PR_DLPI("%s: successful transmit to domainset 0x%x.\n",
18000Sstevel@tonic-gate 		proc, domset);
18010Sstevel@tonic-gate 
18020Sstevel@tonic-gate 	return (0);
18030Sstevel@tonic-gate 
18040Sstevel@tonic-gate bad:
18050Sstevel@tonic-gate 	PR_DLPI("%s: bad transmission to domainset 0x%x, dropping msg.\n",
18060Sstevel@tonic-gate 		proc, domset);
18070Sstevel@tonic-gate 	if (nmp)
18080Sstevel@tonic-gate 		freemsg(nmp);
18090Sstevel@tonic-gate 	freemsg(mp);
18100Sstevel@tonic-gate 	qenable(wq);
18110Sstevel@tonic-gate 	return (1);
18120Sstevel@tonic-gate 
18130Sstevel@tonic-gate requeue:
18140Sstevel@tonic-gate 	PR_DLPI("%s: requeue for domainset 0x%x, no qenable\n",
18150Sstevel@tonic-gate 		proc, domset);
18160Sstevel@tonic-gate 	if (nmp)
18170Sstevel@tonic-gate 		freemsg(nmp);
18180Sstevel@tonic-gate 	if (putbq(wq, mp) == 0)
18190Sstevel@tonic-gate 		freemsg(mp);
18200Sstevel@tonic-gate 	return (1);
18210Sstevel@tonic-gate 
18220Sstevel@tonic-gate tryagain:
18230Sstevel@tonic-gate 	PR_DLPI("%s: try again to domainset 0x%x, putbq.\n",
18240Sstevel@tonic-gate 		proc, domset);
18250Sstevel@tonic-gate 	if (nmp)
18260Sstevel@tonic-gate 		freemsg(nmp);
18270Sstevel@tonic-gate 	if (putbq(wq, mp) == 0)
18280Sstevel@tonic-gate 		freemsg(mp);
18290Sstevel@tonic-gate 	qenable(wq);
18300Sstevel@tonic-gate 	return (1);
18310Sstevel@tonic-gate }
18320Sstevel@tonic-gate 
18330Sstevel@tonic-gate /*
18340Sstevel@tonic-gate  * Called by:	idnh_recv_data, idn_recv_mboxdata.
18350Sstevel@tonic-gate  */
18360Sstevel@tonic-gate void
18370Sstevel@tonic-gate idndl_read(struct idn *sip, mblk_t *mp)
18380Sstevel@tonic-gate {
18390Sstevel@tonic-gate 	struct ether_header	*ehp;
18400Sstevel@tonic-gate 	queue_t			*ip4q;
18410Sstevel@tonic-gate 	queue_t			*ip6q;
18420Sstevel@tonic-gate 	int		pktlen;
18430Sstevel@tonic-gate 	procname_t	proc = "idndl_read";
18440Sstevel@tonic-gate 
1845931Smathue 	PR_DLPI("%s: incoming msgsize = %lu, msgdsize = %lu\n",
18460Sstevel@tonic-gate 		proc, msgsize(mp), msgdsize(mp));
18470Sstevel@tonic-gate 
18480Sstevel@tonic-gate 	ehp = (struct ether_header *)mp->b_rptr;
18490Sstevel@tonic-gate 	if (sip == NULL)
18500Sstevel@tonic-gate 		sip = IDNDL_ETHER2SIP(&ehp->ether_dhost);
18510Sstevel@tonic-gate 	if (sip == NULL) {
18520Sstevel@tonic-gate 		/*
18530Sstevel@tonic-gate 		 * If the sip is NULL, then I don't have a connection
18540Sstevel@tonic-gate 		 * for this network.  No point in sending the message
18550Sstevel@tonic-gate 		 * up.
18560Sstevel@tonic-gate 		 */
18570Sstevel@tonic-gate 		PR_DLPI("%s: no plumbing to send message through.\n",
18580Sstevel@tonic-gate 			proc);
18590Sstevel@tonic-gate 		freemsg(mp);
18600Sstevel@tonic-gate 		return;
18610Sstevel@tonic-gate 	}
18620Sstevel@tonic-gate 	IDN_KSTAT_INC(sip, si_ipackets);
18630Sstevel@tonic-gate 	IDN_KSTAT_INC(sip, si_ipackets64);
18640Sstevel@tonic-gate 	/*
18650Sstevel@tonic-gate 	 * update MIB II statistics
18660Sstevel@tonic-gate 	 */
18670Sstevel@tonic-gate 	pktlen = mp->b_wptr - mp->b_rptr;
18680Sstevel@tonic-gate 	BUMP_InNUcast(sip, ehp);
18690Sstevel@tonic-gate 	IDN_KSTAT_ADD(sip, si_rcvbytes, pktlen);
18700Sstevel@tonic-gate 	IDN_KSTAT_ADD(sip, si_rbytes64, (uint64_t)pktlen);
18710Sstevel@tonic-gate 
18720Sstevel@tonic-gate 	ip4q = sip->si_ip4q;
18730Sstevel@tonic-gate 	ip6q = sip->si_ip6q;
18740Sstevel@tonic-gate 
18750Sstevel@tonic-gate 	if (IS_ETHERTYPE_IPV4(ehp->ether_type) &&
18760Sstevel@tonic-gate 			!IDNDL_ADDR_IS_MULTICAST(&ehp->ether_dhost) &&
18770Sstevel@tonic-gate 			ip4q &&
18780Sstevel@tonic-gate 			canputnext(ip4q)) {
18790Sstevel@tonic-gate 		mp->b_rptr += sizeof (struct ether_header);
18800Sstevel@tonic-gate 		(void) putnext(ip4q, mp);
18810Sstevel@tonic-gate 		/*LINTED*/
18820Sstevel@tonic-gate 	} else if (IS_ETHERTYPE_IPV6(ehp->ether_type) &&
18830Sstevel@tonic-gate 			!IDNDL_ADDR_IS_MULTICAST(&ehp->ether_dhost) &&
18840Sstevel@tonic-gate 			ip6q &&
18850Sstevel@tonic-gate 			canputnext(ip6q)) {
18860Sstevel@tonic-gate 		mp->b_rptr += sizeof (struct ether_header);
18870Sstevel@tonic-gate 		(void) putnext(ip6q, mp);
18880Sstevel@tonic-gate 	} else {
18890Sstevel@tonic-gate 		/*
18900Sstevel@tonic-gate 		 * Strip the PADs for 802.3
18910Sstevel@tonic-gate 		 */
18920Sstevel@tonic-gate 		pktlen = ehp->ether_type + sizeof (struct ether_header);
18930Sstevel@tonic-gate 		PR_DLPI("%s: stripping PADs for 802.3 (pktlen=%d)\n",
18940Sstevel@tonic-gate 			proc, pktlen);
18950Sstevel@tonic-gate 		if (pktlen < ETHERMIN)
18960Sstevel@tonic-gate 			mp->b_wptr = mp->b_rptr + pktlen;
18970Sstevel@tonic-gate 		idndl_sendup(sip, mp, idndl_accept);
18980Sstevel@tonic-gate 	}
18990Sstevel@tonic-gate }
19000Sstevel@tonic-gate 
19010Sstevel@tonic-gate int
19020Sstevel@tonic-gate idndl_init(struct idn *sip)
19030Sstevel@tonic-gate {
19040Sstevel@tonic-gate 	struct idnstr	*stp;
19050Sstevel@tonic-gate 
19060Sstevel@tonic-gate 	if (sip->si_flags & IDNSUSPENDED)
19070Sstevel@tonic-gate 		(void) ddi_dev_is_needed(sip->si_dip, 0, 1);
19080Sstevel@tonic-gate 
19090Sstevel@tonic-gate 	sip->si_flags = 0;
19100Sstevel@tonic-gate 	sip->si_wantw = 0;
19110Sstevel@tonic-gate 
19120Sstevel@tonic-gate 	IDN_KSTAT_INC(sip, si_inits);
19130Sstevel@tonic-gate 
19140Sstevel@tonic-gate 	rw_enter(&idn.struprwlock, RW_WRITER);
19150Sstevel@tonic-gate 
19160Sstevel@tonic-gate 	for (stp = idn.strup; stp; stp = stp->ss_nextp) {
19170Sstevel@tonic-gate 		if ((stp->ss_sip == sip) && (stp->ss_flags & IDNSALLPHYS)) {
19180Sstevel@tonic-gate 			sip->si_flags |= IDNPROMISC;
19190Sstevel@tonic-gate 			break;
19200Sstevel@tonic-gate 		}
19210Sstevel@tonic-gate 	}
19220Sstevel@tonic-gate 
19230Sstevel@tonic-gate 	sip->si_flags |= IDNRUNNING;
19240Sstevel@tonic-gate 
19250Sstevel@tonic-gate 	mutex_enter(&idn.sipwenlock);
19260Sstevel@tonic-gate 	idndl_wenable(sip);
19270Sstevel@tonic-gate 	mutex_exit(&idn.sipwenlock);
19280Sstevel@tonic-gate 
19290Sstevel@tonic-gate 	rw_exit(&idn.struprwlock);
19300Sstevel@tonic-gate 
19310Sstevel@tonic-gate 	return (!(sip->si_flags & IDNRUNNING));
19320Sstevel@tonic-gate }
19330Sstevel@tonic-gate 
19340Sstevel@tonic-gate void
19350Sstevel@tonic-gate idndl_uninit(struct idn *sip)
19360Sstevel@tonic-gate {
19370Sstevel@tonic-gate 	int		channel;
19380Sstevel@tonic-gate 	procname_t	proc = "idndl_uninit";
19390Sstevel@tonic-gate 
19400Sstevel@tonic-gate 	sip->si_flags &= ~IDNRUNNING;
19410Sstevel@tonic-gate 
19420Sstevel@tonic-gate 	channel = (int)sip->si_ouraddr.ether_addr_octet[IDNETHER_CHANNEL];
19430Sstevel@tonic-gate 	PR_DLPI("%s: IP SAP, uninit channel %d\n", proc, channel);
19440Sstevel@tonic-gate 	/*
19450Sstevel@tonic-gate 	 * A uninit is a hard close of an interface.
19460Sstevel@tonic-gate 	 */
19470Sstevel@tonic-gate 	idn_close_channel(channel, IDNCHAN_HARD_CLOSE);
19480Sstevel@tonic-gate }
19490Sstevel@tonic-gate 
19500Sstevel@tonic-gate /*
19510Sstevel@tonic-gate  * Send packet upstream.
19520Sstevel@tonic-gate  * Assume mp->b_rptr points to ether_header.
19530Sstevel@tonic-gate  */
19540Sstevel@tonic-gate void
19550Sstevel@tonic-gate idndl_sendup(struct idn *sip, mblk_t *mp, struct idnstr *(*acceptfunc)())
19560Sstevel@tonic-gate {
19570Sstevel@tonic-gate 	int			type;
19580Sstevel@tonic-gate 	struct ether_addr	*dhostp, *shostp;
19590Sstevel@tonic-gate 	struct idnstr		*stp, *nstp;
19600Sstevel@tonic-gate 	mblk_t 		*nmp;
19610Sstevel@tonic-gate 	ulong_t		isgroupaddr;
19620Sstevel@tonic-gate 
19630Sstevel@tonic-gate 	TRACE_0(TR_FAC_IDN, TR_IDN_SENDUP_START, "idnsendup start");
19640Sstevel@tonic-gate 
19650Sstevel@tonic-gate 	dhostp = &((struct ether_header *)mp->b_rptr)->ether_dhost;
19660Sstevel@tonic-gate 	shostp = &((struct ether_header *)mp->b_rptr)->ether_shost;
19670Sstevel@tonic-gate 	type = ((struct ether_header *)mp->b_rptr)->ether_type;
19680Sstevel@tonic-gate 
19690Sstevel@tonic-gate 	isgroupaddr = IDNDL_ADDR_IS_MULTICAST(dhostp);
19700Sstevel@tonic-gate 
19710Sstevel@tonic-gate 	/*
19720Sstevel@tonic-gate 	 * While holding a reader lock on the linked list of streams structures,
19730Sstevel@tonic-gate 	 * attempt to match the address criteria for each stream
19740Sstevel@tonic-gate 	 * and pass up the raw M_DATA ("fastpath") or a DL_UNITDATA_IND.
19750Sstevel@tonic-gate 	 */
19760Sstevel@tonic-gate 
19770Sstevel@tonic-gate 	rw_enter(&idn.struprwlock, RW_READER);
19780Sstevel@tonic-gate 
19790Sstevel@tonic-gate 	if ((stp = (*acceptfunc)(idn.strup, sip, type, dhostp)) == NULL) {
19800Sstevel@tonic-gate 		rw_exit(&idn.struprwlock);
19810Sstevel@tonic-gate 		freemsg(mp);
19820Sstevel@tonic-gate 		TRACE_0(TR_FAC_IDN, TR_IDN_SENDUP_END, "idnsendup end");
19830Sstevel@tonic-gate 		return;
19840Sstevel@tonic-gate 	}
19850Sstevel@tonic-gate 
19860Sstevel@tonic-gate 	/*
19870Sstevel@tonic-gate 	 * Loop on matching open streams until (*acceptfunc)() returns NULL.
19880Sstevel@tonic-gate 	 */
19890Sstevel@tonic-gate 	for (; nstp = (*acceptfunc)(stp->ss_nextp, sip, type, dhostp);
19900Sstevel@tonic-gate 		stp = nstp) {
19910Sstevel@tonic-gate 
19920Sstevel@tonic-gate 		if (canputnext(stp->ss_rq) == 0) {
19930Sstevel@tonic-gate 			IDN_KSTAT_INC(sip, si_nocanput);
19940Sstevel@tonic-gate 			continue;
19950Sstevel@tonic-gate 		}
19960Sstevel@tonic-gate 		if ((nmp = dupmsg(mp)) == NULL)
19970Sstevel@tonic-gate 			nmp = copymsg(mp);
19980Sstevel@tonic-gate 		if (nmp) {
19990Sstevel@tonic-gate 			if ((stp->ss_flags & IDNSFAST) && !isgroupaddr) {
20000Sstevel@tonic-gate 				nmp->b_rptr += sizeof (struct ether_header);
20010Sstevel@tonic-gate 				(void) putnext(stp->ss_rq, nmp);
20020Sstevel@tonic-gate 			} else if (stp->ss_flags & IDNSRAW) {
20030Sstevel@tonic-gate 				(void) putnext(stp->ss_rq, nmp);
20040Sstevel@tonic-gate 			} else if ((nmp = idndl_addudind(sip, nmp, shostp,
20050Sstevel@tonic-gate 						dhostp, type, isgroupaddr))) {
20060Sstevel@tonic-gate 				(void) putnext(stp->ss_rq, nmp);
20070Sstevel@tonic-gate 			}
20080Sstevel@tonic-gate 		} else {
20090Sstevel@tonic-gate 			IDN_KSTAT_INC(sip, si_allocbfail);
20100Sstevel@tonic-gate 		}
20110Sstevel@tonic-gate 	}
20120Sstevel@tonic-gate 
20130Sstevel@tonic-gate 
20140Sstevel@tonic-gate 	/*
20150Sstevel@tonic-gate 	 * Do the last one.
20160Sstevel@tonic-gate 	 */
20170Sstevel@tonic-gate 	if (canputnext(stp->ss_rq)) {
20180Sstevel@tonic-gate 		if ((stp->ss_flags & IDNSFAST) && !isgroupaddr) {
20190Sstevel@tonic-gate 			mp->b_rptr += sizeof (struct ether_header);
20200Sstevel@tonic-gate 			(void) putnext(stp->ss_rq, mp);
20210Sstevel@tonic-gate 		} else if (stp->ss_flags & IDNSRAW) {
20220Sstevel@tonic-gate 			(void) putnext(stp->ss_rq, mp);
20230Sstevel@tonic-gate 		} else if ((mp = idndl_addudind(sip, mp, shostp, dhostp,
20240Sstevel@tonic-gate 					    type, isgroupaddr))) {
20250Sstevel@tonic-gate 			(void) putnext(stp->ss_rq, mp);
20260Sstevel@tonic-gate 		}
20270Sstevel@tonic-gate 	} else {
20280Sstevel@tonic-gate 		freemsg(mp);
20290Sstevel@tonic-gate 		IDN_KSTAT_INC(sip, si_nocanput);
20300Sstevel@tonic-gate 		IDN_KSTAT_INC(sip, si_norcvbuf);	/* MIB II */
20310Sstevel@tonic-gate 	}
20320Sstevel@tonic-gate 
20330Sstevel@tonic-gate 	rw_exit(&idn.struprwlock);
20340Sstevel@tonic-gate 	TRACE_0(TR_FAC_IDN, TR_IDN_SENDUP_END, "idnsendup end");
20350Sstevel@tonic-gate }
20360Sstevel@tonic-gate 
20370Sstevel@tonic-gate /*
20380Sstevel@tonic-gate  * Test upstream destination sap and address match.
20390Sstevel@tonic-gate  */
20400Sstevel@tonic-gate struct idnstr *
20410Sstevel@tonic-gate idndl_accept(register struct idnstr *stp, register struct idn *sip,
20420Sstevel@tonic-gate 	    int type, struct ether_addr *addrp)
20430Sstevel@tonic-gate {
20440Sstevel@tonic-gate 	t_uscalar_t	sap;
20450Sstevel@tonic-gate 	uint_t		flags;
20460Sstevel@tonic-gate 
20470Sstevel@tonic-gate 	for (; stp; stp = stp->ss_nextp) {
20480Sstevel@tonic-gate 		sap   = stp->ss_sap;
20490Sstevel@tonic-gate 		flags = stp->ss_flags;
20500Sstevel@tonic-gate 
20510Sstevel@tonic-gate 		if ((stp->ss_sip == sip) && IDNSAPMATCH(sap, type, flags))
20520Sstevel@tonic-gate 			if ((ether_cmp(addrp, &sip->si_ouraddr) == 0) ||
20530Sstevel@tonic-gate 			    (ether_cmp(addrp, &etherbroadcastaddr) == 0) ||
20540Sstevel@tonic-gate 			    (flags & IDNSALLPHYS) ||
20550Sstevel@tonic-gate 			    idndl_mcmatch(stp, addrp))
20560Sstevel@tonic-gate 				return (stp);
20570Sstevel@tonic-gate 	}
20580Sstevel@tonic-gate 
20590Sstevel@tonic-gate 	return (NULL);
20600Sstevel@tonic-gate }
20610Sstevel@tonic-gate 
20620Sstevel@tonic-gate /*
20630Sstevel@tonic-gate  * Test upstream destination sap and address match for IDNSALLPHYS only.
20640Sstevel@tonic-gate  */
20650Sstevel@tonic-gate /* ARGSUSED3 */
20660Sstevel@tonic-gate struct idnstr *
20670Sstevel@tonic-gate idndl_paccept(register struct idnstr *stp, register struct idn *sip,
20680Sstevel@tonic-gate 	    int type, struct ether_addr *addrp)
20690Sstevel@tonic-gate {
20700Sstevel@tonic-gate 	t_uscalar_t	sap;
20710Sstevel@tonic-gate 	uint_t		flags;
20720Sstevel@tonic-gate 
20730Sstevel@tonic-gate 	for (; stp; stp = stp->ss_nextp) {
20740Sstevel@tonic-gate 		sap   = stp->ss_sap;
20750Sstevel@tonic-gate 		flags = stp->ss_flags;
20760Sstevel@tonic-gate 
20770Sstevel@tonic-gate 		if ((stp->ss_sip == sip) &&
20780Sstevel@tonic-gate 		    IDNSAPMATCH(sap, type, flags) &&
20790Sstevel@tonic-gate 		    (flags & IDNSALLPHYS))
20800Sstevel@tonic-gate 			return (stp);
20810Sstevel@tonic-gate 	}
20820Sstevel@tonic-gate 
20830Sstevel@tonic-gate 	return (NULL);
20840Sstevel@tonic-gate }
20850Sstevel@tonic-gate 
20860Sstevel@tonic-gate /*
20870Sstevel@tonic-gate  * Set or clear the device ipq pointer.
20880Sstevel@tonic-gate  * Assumes IPv4 and IPv6 are IDNSFAST.
20890Sstevel@tonic-gate  */
20900Sstevel@tonic-gate static void
20910Sstevel@tonic-gate idndl_setipq(struct idn *sip)
20920Sstevel@tonic-gate {
20930Sstevel@tonic-gate 	struct idnstr	*stp;
20940Sstevel@tonic-gate 	int		ok4 = 1;
20950Sstevel@tonic-gate 	int		ok6 = 1;
20960Sstevel@tonic-gate 	queue_t		*ip4q = NULL;
20970Sstevel@tonic-gate 	queue_t		*ip6q = NULL;
20980Sstevel@tonic-gate 
20990Sstevel@tonic-gate 	rw_enter(&idn.struprwlock, RW_READER);
21000Sstevel@tonic-gate 
21010Sstevel@tonic-gate 	for (stp = idn.strup; stp; stp = stp->ss_nextp) {
21020Sstevel@tonic-gate 		if (stp->ss_sip == sip) {
21030Sstevel@tonic-gate 			if (stp->ss_flags & (IDNSALLPHYS|IDNSALLSAP)) {
21040Sstevel@tonic-gate 				ok4 = 0;
21050Sstevel@tonic-gate 				ok6 = 0;
21060Sstevel@tonic-gate 				break;
21070Sstevel@tonic-gate 			}
21080Sstevel@tonic-gate 			if (IS_ETHERTYPE_IPV4(stp->ss_sap)) {
21090Sstevel@tonic-gate 				if (ip4q == NULL)
21100Sstevel@tonic-gate 					ip4q = stp->ss_rq;
21110Sstevel@tonic-gate 				else
21120Sstevel@tonic-gate 					ok4 = 0;
21130Sstevel@tonic-gate 				/*LINTED*/
21140Sstevel@tonic-gate 			} else if (IS_ETHERTYPE_IPV6(stp->ss_sap)) {
21150Sstevel@tonic-gate 				if (ip6q == NULL)
21160Sstevel@tonic-gate 					ip6q = stp->ss_rq;
21170Sstevel@tonic-gate 				else
21180Sstevel@tonic-gate 					ok6 = 0;
21190Sstevel@tonic-gate 			}
21200Sstevel@tonic-gate 		}
21210Sstevel@tonic-gate 	}
21220Sstevel@tonic-gate 
21230Sstevel@tonic-gate 	rw_exit(&idn.struprwlock);
21240Sstevel@tonic-gate 
21250Sstevel@tonic-gate 	if (ok4)
21260Sstevel@tonic-gate 		sip->si_ip4q = ip4q;
21270Sstevel@tonic-gate 	else
21280Sstevel@tonic-gate 		sip->si_ip4q = NULL;
21290Sstevel@tonic-gate 	if (ok6)
21300Sstevel@tonic-gate 		sip->si_ip6q = ip6q;
21310Sstevel@tonic-gate 	else
21320Sstevel@tonic-gate 		sip->si_ip6q = NULL;
21330Sstevel@tonic-gate }
21340Sstevel@tonic-gate 
21350Sstevel@tonic-gate /*
21360Sstevel@tonic-gate  * Prefix msg with a DL_UNITDATA_IND mblk and return the new msg.
21370Sstevel@tonic-gate  */
21380Sstevel@tonic-gate static mblk_t *
21390Sstevel@tonic-gate idndl_addudind(struct idn *sip, mblk_t *mp,
21400Sstevel@tonic-gate 	    struct ether_addr *shostp, struct ether_addr *dhostp,
21410Sstevel@tonic-gate 	    int type, ulong_t isgroupaddr)
21420Sstevel@tonic-gate {
21430Sstevel@tonic-gate 	dl_unitdata_ind_t	*dludindp;
21440Sstevel@tonic-gate 	struct idndladdr	*dlap;
21450Sstevel@tonic-gate 	mblk_t	*nmp;
21460Sstevel@tonic-gate 	int	size;
21470Sstevel@tonic-gate 
21480Sstevel@tonic-gate 	TRACE_0(TR_FAC_IDN, TR_IDN_ADDUDIND_START, "idndl_addudind start");
21490Sstevel@tonic-gate 
21500Sstevel@tonic-gate 	mp->b_rptr += sizeof (struct ether_header);
21510Sstevel@tonic-gate 
21520Sstevel@tonic-gate 	/*
21530Sstevel@tonic-gate 	 * Allocate an M_PROTO mblk for the DL_UNITDATA_IND.
21540Sstevel@tonic-gate 	 */
21550Sstevel@tonic-gate 	size = sizeof (dl_unitdata_ind_t) + IDNADDRL + IDNADDRL;
21560Sstevel@tonic-gate 	nmp = allocb(IDNROUNDUP(IDNHEADROOM + size, sizeof (double)), BPRI_LO);
21570Sstevel@tonic-gate 	if (nmp == NULL) {
21580Sstevel@tonic-gate 		IDN_KSTAT_INC(sip, si_allocbfail);
21590Sstevel@tonic-gate 		IDN_KSTAT_INC(sip, si_ierrors);
21600Sstevel@tonic-gate 		if (idn_debug)
21610Sstevel@tonic-gate 			serror(sip->si_dip, 451, "allocb failed");
21620Sstevel@tonic-gate 		freemsg(mp);
21630Sstevel@tonic-gate 		TRACE_0(TR_FAC_IDN, TR_IDN_ADDUDIND_END, "idndl_addudind end");
21640Sstevel@tonic-gate 		return (NULL);
21650Sstevel@tonic-gate 	}
21660Sstevel@tonic-gate 	DB_TYPE(nmp) = M_PROTO;
21670Sstevel@tonic-gate 	nmp->b_wptr = nmp->b_datap->db_lim;
21680Sstevel@tonic-gate 	nmp->b_rptr = nmp->b_wptr - size;
21690Sstevel@tonic-gate 
21700Sstevel@tonic-gate 	/*
21710Sstevel@tonic-gate 	 * Construct a DL_UNITDATA_IND primitive.
21720Sstevel@tonic-gate 	 */
21730Sstevel@tonic-gate 	dludindp = (dl_unitdata_ind_t *)nmp->b_rptr;
21740Sstevel@tonic-gate 	dludindp->dl_primitive = DL_UNITDATA_IND;
21750Sstevel@tonic-gate 	dludindp->dl_dest_addr_length = IDNADDRL;
21760Sstevel@tonic-gate 	dludindp->dl_dest_addr_offset = sizeof (dl_unitdata_ind_t);
21770Sstevel@tonic-gate 	dludindp->dl_src_addr_length = IDNADDRL;
21780Sstevel@tonic-gate 	dludindp->dl_src_addr_offset = sizeof (dl_unitdata_ind_t) + IDNADDRL;
21790Sstevel@tonic-gate 	dludindp->dl_group_address = isgroupaddr;
21800Sstevel@tonic-gate 
21810Sstevel@tonic-gate 	dlap = (struct idndladdr *)(nmp->b_rptr + sizeof (dl_unitdata_ind_t));
21820Sstevel@tonic-gate 	ether_copy(dhostp, &dlap->dl_phys);
21830Sstevel@tonic-gate 	dlap->dl_sap = (ushort_t)type;
21840Sstevel@tonic-gate 
21850Sstevel@tonic-gate 	dlap = (struct idndladdr *)(nmp->b_rptr + sizeof (dl_unitdata_ind_t)
21860Sstevel@tonic-gate 					+ IDNADDRL);
21870Sstevel@tonic-gate 	ether_copy(shostp, &dlap->dl_phys);
21880Sstevel@tonic-gate 	dlap->dl_sap = (ushort_t)type;
21890Sstevel@tonic-gate 
21900Sstevel@tonic-gate 	/*
21910Sstevel@tonic-gate 	 * Link the M_PROTO and M_DATA together.
21920Sstevel@tonic-gate 	 */
21930Sstevel@tonic-gate 	nmp->b_cont = mp;
21940Sstevel@tonic-gate 	TRACE_0(TR_FAC_IDN, TR_IDN_ADDUDIND_END, "idndl_addudind end");
21950Sstevel@tonic-gate 	return (nmp);
21960Sstevel@tonic-gate }
21970Sstevel@tonic-gate 
21980Sstevel@tonic-gate /*
21990Sstevel@tonic-gate  * Return TRUE if the given multicast address is one
22000Sstevel@tonic-gate  * of those that this particular Stream is interested in.
22010Sstevel@tonic-gate  */
22020Sstevel@tonic-gate static int
22030Sstevel@tonic-gate idndl_mcmatch(register struct idnstr *stp, register struct ether_addr *addrp)
22040Sstevel@tonic-gate {
22050Sstevel@tonic-gate 	register struct ether_addr	*mctab;
22060Sstevel@tonic-gate 	register int	mccount;
22070Sstevel@tonic-gate 	register int	i;
22080Sstevel@tonic-gate 
22090Sstevel@tonic-gate 	/*
22100Sstevel@tonic-gate 	 * Return FALSE if not a multicast address.
22110Sstevel@tonic-gate 	 */
22120Sstevel@tonic-gate 	if (!IDNDL_ADDR_IS_MULTICAST(addrp))
22130Sstevel@tonic-gate 		return (0);
22140Sstevel@tonic-gate 
22150Sstevel@tonic-gate 	/*
22160Sstevel@tonic-gate 	 * Check if all multicasts have been enabled for this Stream
22170Sstevel@tonic-gate 	 */
22180Sstevel@tonic-gate 	if (stp->ss_flags & IDNSALLMULTI)
22190Sstevel@tonic-gate 		return (1);
22200Sstevel@tonic-gate 
22210Sstevel@tonic-gate 	/*
22220Sstevel@tonic-gate 	 * Return FALSE if no multicast addresses enabled for this Stream.
22230Sstevel@tonic-gate 	 */
22240Sstevel@tonic-gate 	if (stp->ss_mccount == 0)
22250Sstevel@tonic-gate 		return (0);
22260Sstevel@tonic-gate 
22270Sstevel@tonic-gate 	/*
22280Sstevel@tonic-gate 	 * Otherwise, find it in the table.
22290Sstevel@tonic-gate 	 */
22300Sstevel@tonic-gate 
22310Sstevel@tonic-gate 	mccount = stp->ss_mccount;
22320Sstevel@tonic-gate 	mctab = stp->ss_mctab;
22330Sstevel@tonic-gate 
22340Sstevel@tonic-gate 	for (i = 0; i < mccount; i++)
22350Sstevel@tonic-gate 		if (!ether_cmp(addrp, &mctab[i]))
22360Sstevel@tonic-gate 			return (1);
22370Sstevel@tonic-gate 
22380Sstevel@tonic-gate 	return (0);
22390Sstevel@tonic-gate }
22400Sstevel@tonic-gate 
22410Sstevel@tonic-gate /*
22420Sstevel@tonic-gate  * Start xmit on any msgs previously enqueued on any write queues.
22430Sstevel@tonic-gate  * If the caller passes NULL, then we need to check all
22440Sstevel@tonic-gate  * our interfaces.
22450Sstevel@tonic-gate  */
22460Sstevel@tonic-gate void
22470Sstevel@tonic-gate idndl_wenable(struct idn *sip)
22480Sstevel@tonic-gate {
22490Sstevel@tonic-gate 	struct idnstr	*stp;
22500Sstevel@tonic-gate 	queue_t		*wq;
22510Sstevel@tonic-gate 
22520Sstevel@tonic-gate 	/*
22530Sstevel@tonic-gate 	 * Order of wantw accesses is important.
22540Sstevel@tonic-gate 	 */
22550Sstevel@tonic-gate 	ASSERT((sip == NULL) ? RW_LOCK_HELD(&idn.struprwlock) : 1);
22560Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&idn.sipwenlock));
22570Sstevel@tonic-gate 
22580Sstevel@tonic-gate 	do {
22590Sstevel@tonic-gate 		if (sip)
22600Sstevel@tonic-gate 			sip->si_wantw = 0;
22610Sstevel@tonic-gate 		for (stp = idn.strup; stp; stp = stp->ss_nextp) {
22620Sstevel@tonic-gate 			if ((!sip || (stp->ss_sip == sip)) &&
22630Sstevel@tonic-gate 			    stp->ss_rq && ((wq = WR(stp->ss_rq))->q_first))
22640Sstevel@tonic-gate 				qenable(wq);
22650Sstevel@tonic-gate 		}
22660Sstevel@tonic-gate 	} while (sip && sip->si_wantw);
22670Sstevel@tonic-gate }
22680Sstevel@tonic-gate 
22690Sstevel@tonic-gate /*VARARGS*/
22700Sstevel@tonic-gate static void
22710Sstevel@tonic-gate serror(dev_info_t *dip, int idnerr, char *fmt, ...)
22720Sstevel@tonic-gate {
22730Sstevel@tonic-gate 	static	long	last;
22740Sstevel@tonic-gate 	static	char	*lastfmt;
22750Sstevel@tonic-gate 	char		msg_buffer[255];
22760Sstevel@tonic-gate 	va_list ap;
22770Sstevel@tonic-gate 	time_t	now;
22780Sstevel@tonic-gate 
22790Sstevel@tonic-gate 	/*
22800Sstevel@tonic-gate 	 * Don't print same error message too often.
22810Sstevel@tonic-gate 	 */
22820Sstevel@tonic-gate 	now = gethrestime_sec();
22830Sstevel@tonic-gate 	if ((last == (now & ~1)) && (lastfmt == fmt))
22840Sstevel@tonic-gate 		return;
22850Sstevel@tonic-gate 
22860Sstevel@tonic-gate 	last = now & ~1;
22870Sstevel@tonic-gate 	lastfmt = fmt;
22880Sstevel@tonic-gate 
22890Sstevel@tonic-gate 	va_start(ap, fmt);
22900Sstevel@tonic-gate 	(void) vsprintf(msg_buffer, fmt, ap);
22910Sstevel@tonic-gate 	cmn_err(CE_CONT, "IDN: %d: %s%d: %s\n",
22920Sstevel@tonic-gate 		idnerr, ddi_get_name(dip),
22930Sstevel@tonic-gate 		ddi_get_instance(dip), msg_buffer);
22940Sstevel@tonic-gate 	va_end(ap);
22950Sstevel@tonic-gate }
2296